Skip to content

Commit

Permalink
Merge pull request #133 from cooljingle/multiple-legends
Browse files Browse the repository at this point in the history
Support for multiple legends
  • Loading branch information
LukaOber authored Dec 19, 2024
2 parents 613ca30 + 4f1dc10 commit 9c078c6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
19 changes: 19 additions & 0 deletions charming/src/component/legend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(Box<Legend>),
Multiple(Vec<Legend>),
}

impl From<Legend> for LegendConfig {
fn from(legend: Legend) -> Self {
Self::Single(Box::new(legend))
}
}

impl From<Vec<Legend>> for LegendConfig {
fn from(legends: Vec<Legend>) -> Self {
LegendConfig::Multiple(legends)
}
}

#[derive(Serialize, Debug, PartialEq, PartialOrd, Clone, Copy)]
#[serde(rename_all = "snake_case")]
pub enum LegendType {
Expand Down
8 changes: 4 additions & 4 deletions charming/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -247,7 +247,7 @@ pub struct Chart {
tooltip: Option<Tooltip>,

#[serde(skip_serializing_if = "Option::is_none")]
legend: Option<Legend>,
legend: Option<LegendConfig>,

#[serde(skip_serializing_if = "Option::is_none")]
toolbox: Option<Toolbox>,
Expand Down Expand Up @@ -381,8 +381,8 @@ impl Chart {
self
}

pub fn legend(mut self, legend: Legend) -> Self {
self.legend = Some(legend);
pub fn legend<L: Into<LegendConfig>>(mut self, legend: L) -> Self {
self.legend = Some(legend.into());
self
}

Expand Down
1 change: 1 addition & 0 deletions gallery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions gallery/src/line/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
63 changes: 63 additions & 0 deletions gallery/src/line/split_legend.rs
Original file line number Diff line number Diff line change
@@ -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]),
)
}

0 comments on commit 9c078c6

Please sign in to comment.