diff --git a/charming/src/component/legend.rs b/charming/src/component/legend.rs index 7959aa8..114842a 100644 --- a/charming/src/component/legend.rs +++ b/charming/src/component/legend.rs @@ -7,6 +7,25 @@ use crate::{ element::{Color, Icon, ItemStyle, LabelAlign, LineStyle, Orient, Padding, TextStyle}, }; +#[derive(Debug, PartialEq, Serialize, Clone)] +#[serde(untagged)] +pub enum LegendConfig { + Single(Legend), + Multiple(Vec), +} + +impl From for LegendConfig { + fn from(legend: Legend) -> Self { + LegendConfig::Single(legend) + } +} + +impl From> for LegendConfig { + fn from(legends: Vec) -> Self { + LegendConfig::Multiple(legends) + } +} + #[derive(Serialize, Debug, PartialEq, PartialOrd, Clone, Copy)] #[serde(rename_all = "snake_case")] pub enum LegendType { diff --git a/charming/src/lib.rs b/charming/src/lib.rs index 1dd6234..3d965af 100644 --- a/charming/src/lib.rs +++ b/charming/src/lib.rs @@ -93,7 +93,7 @@ pub mod theme; pub use renderer::*; use component::{ - AngleAxis, Aria, Axis, Axis3D, DataZoom, GeoMap, Grid, Grid3D, Legend, ParallelAxis, + AngleAxis, Aria, Axis, Axis3D, DataZoom, GeoMap, Grid, Grid3D, LegendConfig, ParallelAxis, ParallelCoordinate, PolarCoordinate, RadarCoordinate, RadiusAxis, SaveAsImageType, SingleAxis, Title, Toolbox, VisualMap, }; @@ -247,7 +247,7 @@ pub struct Chart { tooltip: Option, #[serde(skip_serializing_if = "Option::is_none")] - legend: Option, + legend: Option, #[serde(skip_serializing_if = "Option::is_none")] toolbox: Option, @@ -381,8 +381,8 @@ impl Chart { self } - pub fn legend(mut self, legend: Legend) -> Self { - self.legend = Some(legend); + pub fn legend>(mut self, legend: L) -> Self { + self.legend = Some(legend.into()); self } diff --git a/gallery/src/lib.rs b/gallery/src/lib.rs index c8d2db8..025c798 100644 --- a/gallery/src/lib.rs +++ b/gallery/src/lib.rs @@ -123,6 +123,7 @@ lazy_static! { insert!(m, line, smoothed_line); insert!(m, line, stacked_area); insert!(m, line, stacked_line); + insert!(m, line, split_legend); insert!(m, line, step_line); insert!(m, line, temperature_change); insert!(m, line, two_value_axes_in_polar); diff --git a/gallery/src/line/mod.rs b/gallery/src/line/mod.rs index e36751c..98b052b 100644 --- a/gallery/src/line/mod.rs +++ b/gallery/src/line/mod.rs @@ -11,6 +11,7 @@ pub mod line_gradient; pub mod rainfall; pub mod rainfall_vs_evaporation; pub mod smoothed_line; +pub mod split_legend; pub mod stacked_area; pub mod stacked_line; pub mod step_line; diff --git a/gallery/src/line/split_legend.rs b/gallery/src/line/split_legend.rs new file mode 100644 index 0000000..2bdd47c --- /dev/null +++ b/gallery/src/line/split_legend.rs @@ -0,0 +1,63 @@ +use charming::{ + component::{Axis, Feature, Grid, Legend, SaveAsImage, Title, Toolbox}, + element::{AxisType, Tooltip, Trigger}, + series::Line, + Chart, +}; + +pub fn chart() -> Chart { + Chart::new() + .title(Title::new().text("Split Legend")) + .tooltip(Tooltip::new().trigger(Trigger::Axis)) + .legend(vec![ + Legend::new().data(vec![ + "Penguins", + "Giraffes", + "Lions", + "Elephants", + "Monkeys", + ]), + Legend::new().data(vec!["Total"]).right("50"), + ]) + .grid( + Grid::new() + .left("3%") + .right("4%") + .bottom("3%") + .contain_label(true), + ) + .toolbox(Toolbox::new().feature(Feature::new().save_as_image(SaveAsImage::new()))) + .x_axis( + Axis::new() + .type_(AxisType::Category) + .boundary_gap(false) + .data(vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]), + ) + .y_axis(Axis::new().type_(AxisType::Value).name("Sightings")) + .series( + Line::new() + .name("Penguins") + .data(vec![8, 12, 6, 9, 15, 16, 18]), + ) + .series( + Line::new() + .name("Giraffes") + .data(vec![5, 4, 6, 7, 8, 10, 9]), + ) + .series(Line::new().name("Lions").data(vec![3, 2, 4, 3, 5, 6, 4])) + .series( + Line::new() + .name("Elephants") + .data(vec![4, 5, 3, 6, 7, 8, 6]), + ) + .series( + Line::new() + .name("Monkeys") + .data(vec![12, 15, 10, 14, 16, 18, 20]), + ) + .series( + Line::new() + .name("Total") + .data(vec![32, 38, 29, 39, 51, 58, 57]), + ) +}