Skip to content

Commit

Permalink
Merge pull request #106 from yg0x01/main
Browse files Browse the repository at this point in the history
Add missing field to boxplot
  • Loading branch information
LukaOber authored Oct 28, 2024
2 parents 439643c + ecc3d40 commit 8d669d5
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
32 changes: 31 additions & 1 deletion charming/src/series/boxplot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use serde::Serialize;

use crate::element::{ColorBy, CoordinateSystem, Tooltip};
use crate::{
datatype::{DataFrame, DataPoint},
element::{ColorBy, CoordinateSystem, ItemStyle, Tooltip},
};

#[derive(Serialize, Debug, PartialEq, PartialOrd, Clone)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -31,6 +34,15 @@ pub struct Boxplot {

#[serde(skip_serializing_if = "Option::is_none")]
tooltip: Option<Tooltip>,

#[serde(skip_serializing_if = "Option::is_none")]
item_style: Option<ItemStyle>,

#[serde(skip_serializing_if = "Option::is_none")]
z: Option<usize>,

#[serde(skip_serializing_if = "Vec::is_empty")]
data: DataFrame,
}

impl Default for Boxplot {
Expand All @@ -51,6 +63,9 @@ impl Boxplot {
hover_animation: None,
dataset_index: None,
tooltip: None,
data: vec![],
item_style: None,
z: None,
}
}

Expand Down Expand Up @@ -93,4 +108,19 @@ impl Boxplot {
self.tooltip = Some(tooltip);
self
}

pub fn item_style<S: Into<ItemStyle>>(mut self, item_style: S) -> Self {
self.item_style = Some(item_style.into());
self
}

pub fn data<D: Into<DataPoint>>(mut self, data: Vec<D>) -> Self {
self.data = data.into_iter().map(|d| d.into()).collect();
self
}

pub fn z(mut self, z: usize) -> Self {
self.z = Some(z);
self
}
}
66 changes: 66 additions & 0 deletions gallery/src/boxplot/basic_boxplot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use charming::{
component::{Axis, Grid, Title},
datatype::{DataFrame, DataPoint, DataPointItem},
element::{
AxisPointer, AxisPointerType, AxisType, ItemStyle, SplitArea, SplitLine, TextStyle,
Tooltip, Trigger,
},
series::{Boxplot, Scatter},
Chart,
};

pub fn chart() -> Chart {
let data: DataFrame = vec![
vec![655, 850, 940, 980, 1175].into(),
vec![672.5, 800.0, 845.0, 885.0, 1012.5].into(),
vec![780, 840, 855, 880, 940].into(),
vec![621.25, 767.5, 815.0, 865.0, 1011.25].into(),
DataPoint::Item(
DataPointItem::new(vec![713.75, 807.5, 830.0, 870.0, 963.75])
.item_style(ItemStyle::new().border_color("red")),
),
];
let outlier = vec![vec![2, 960], vec![2, 980], vec![2, 750], vec![4, 980]];
Chart::new()
.title(
Title::new()
.text("Michelson-Morley Experiment")
.left("center"),
)
.title(
Title::new()
.text("upper: Q3 + 1.5 * IQR \nlower: Q1 - 1.5 * IQR")
.border_color("#999")
.border_width(1)
.text_style(
TextStyle::new()
.font_weight("normal")
.font_size(14)
.line_height(20),
)
.left("10%")
.top("90%"),
)
.tooltip(
Tooltip::new()
.trigger(Trigger::Item)
.axis_pointer(AxisPointer::new().type_(AxisPointerType::Shadow)),
)
.grid(Grid::new().left("10%").right("10%").bottom("15%"))
.x_axis(
Axis::new()
.type_(AxisType::Category)
.boundary_gap(true)
.name_gap(30)
.split_area(SplitArea::new().show(false))
.split_line(SplitLine::new().show(false)),
)
.y_axis(
Axis::new()
.type_(AxisType::Value)
.name("km/s minus 299,000")
.split_area(SplitArea::new().show(true)),
)
.series(Boxplot::new().name("boxplot").data(data))
.series(Scatter::new().name("outlier").data(outlier))
}
1 change: 1 addition & 0 deletions gallery/src/boxplot/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod basic_boxplot;
pub mod boxplot_light_velocity;
pub mod boxplot_light_velocity2;
pub mod data_transform_simple_aggregate;
Expand Down
1 change: 1 addition & 0 deletions gallery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ lazy_static! {
};
static ref BOXPLOT_CHARTS: BTreeMap<&'static str, fn() -> Chart> = {
let mut m = BTreeMap::new();
insert!(m, boxplot, basic_boxplot);
insert!(m, boxplot, boxplot_light_velocity);
insert!(m, boxplot, boxplot_light_velocity2);
insert!(m, boxplot, data_transform_simple_aggregate);
Expand Down

0 comments on commit 8d669d5

Please sign in to comment.