From fdecc8db5dbfec0dd56dd910225d6f39318ff633 Mon Sep 17 00:00:00 2001 From: "am@pa.dev" Date: Mon, 9 Sep 2024 13:39:42 +0800 Subject: [PATCH] add connect_nulls to line; allow optional numeric values --- charming/src/datatype/value.rs | 10 ++++++++ charming/src/series/line.rs | 9 ++++++++ .../src/line/distribution_of_electricity.rs | 23 +++++++++++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/charming/src/datatype/value.rs b/charming/src/datatype/value.rs index 1f0f4dd..1e690f8 100644 --- a/charming/src/datatype/value.rs +++ b/charming/src/datatype/value.rs @@ -35,6 +35,7 @@ impl From for NumericValue { #[serde(untagged)] pub enum CompositeValue { Number(NumericValue), + OptionalNumber(Option), String(String), Array(Vec), } @@ -48,6 +49,15 @@ where } } +impl From> for CompositeValue +where + N: Into, +{ + fn from(n: Option) -> Self { + CompositeValue::OptionalNumber(n.map(Into::into)) + } +} + impl From<&str> for CompositeValue { fn from(s: &str) -> Self { CompositeValue::String(s.to_string()) diff --git a/charming/src/series/line.rs b/charming/src/series/line.rs index aff6409..99f03e5 100644 --- a/charming/src/series/line.rs +++ b/charming/src/series/line.rs @@ -53,6 +53,9 @@ pub struct Line { #[serde(skip_serializing_if = "Option::is_none")] smooth: Option, + #[serde(skip_serializing_if = "Option::is_none")] + connect_nulls: Option, + #[serde(skip_serializing_if = "Option::is_none")] mark_point: Option, @@ -101,6 +104,7 @@ impl Line { item_style: None, emphasis: None, smooth: None, + connect_nulls: None, mark_point: None, mark_line: None, mark_area: None, @@ -179,6 +183,11 @@ impl Line { self } + pub fn connect_nulls(mut self, connect_nulls: bool) -> Self { + self.connect_nulls = Some(connect_nulls); + self + } + pub fn mark_point>(mut self, mark_point: M) -> Self { self.mark_point = Some(mark_point.into()); self diff --git a/gallery/src/line/distribution_of_electricity.rs b/gallery/src/line/distribution_of_electricity.rs index 1bdbafa..326a0a6 100644 --- a/gallery/src/line/distribution_of_electricity.rs +++ b/gallery/src/line/distribution_of_electricity.rs @@ -52,9 +52,28 @@ pub fn chart() -> Chart { Line::new() .name("Electricity") .smooth(0.5) + .connect_nulls(true) .data(vec![ - 300, 280, 250, 260, 270, 300, 550, 500, 400, 390, 380, 390, 400, 500, 600, 750, - 800, 700, 600, 400, + Some(300), + Some(280), + Some(250), + None, + Some(270), + Some(300), + Some(550), + Some(500), + Some(400), + Some(390), + Some(380), + Some(390), + Some(400), + Some(500), + Some(600), + Some(750), + Some(800), + Some(700), + Some(600), + Some(400), ]) .mark_area( MarkArea::new()