Skip to content

Commit

Permalink
add selected to legend
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and yuankunzhang committed Oct 7, 2024
1 parent 9356afb commit ebd3116
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
14 changes: 14 additions & 0 deletions charming/src/component/legend.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use serde::Serialize;

use crate::{
Expand Down Expand Up @@ -158,6 +160,9 @@ pub struct Legend {
#[serde(skip_serializing_if = "Option::is_none")]
formatter: Option<String>,

#[serde(skip_serializing_if = "Option::is_none")]
selected: Option<HashMap<String, bool>>,

#[serde(skip_serializing_if = "Option::is_none")]
selected_mode: Option<LegendSelectedMode>,

Expand Down Expand Up @@ -202,6 +207,7 @@ impl Legend {
text_style: None,
symbol_rotate: None,
formatter: None,
selected: None,
selected_mode: None,
border_color: None,
inactive_color: None,
Expand Down Expand Up @@ -314,6 +320,14 @@ impl Legend {
self
}

pub fn selected<S: Into<String>, I: IntoIterator<Item = (S, bool)>>(
mut self,
selected: I,
) -> Self {
self.selected = Some(selected.into_iter().map(|(k, v)| (k.into(), v)).collect());
self
}

pub fn selected_mode<S: Into<LegendSelectedMode>>(mut self, selected_mode: S) -> Self {
self.selected_mode = Some(selected_mode.into());
self
Expand Down
25 changes: 18 additions & 7 deletions gallery/src/line/stacked_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ pub fn chart() -> Chart {
Chart::new()
.title(Title::new().text("Stacked Line"))
.tooltip(Tooltip::new().trigger(Trigger::Axis))
.legend(Legend::new().data(vec![
"Email",
"Union Ads",
"Video Ads",
"Direct",
"Search Engine",
]))
.legend(
Legend::new()
.data(vec![
"Email",
"Union Ads",
"Video Ads",
"Direct",
"Search Engine",
"Affiliate Marketing",
])
.selected([("Affiliate Marketing", false)]),
)
.grid(
Grid::new()
.left("3%")
Expand Down Expand Up @@ -61,4 +66,10 @@ pub fn chart() -> Chart {
.stack("Total")
.data(vec![820, 932, 901, 934, 1290, 1330, 1320]),
)
.series(
Line::new()
.name("Affiliate Marketing")
.stack("Total")
.data(vec![180, 232, 210, 290, 250, 400, 370]),
)
}

0 comments on commit ebd3116

Please sign in to comment.