From adde1e8499956e49719a9050dbfa412d0c666800 Mon Sep 17 00:00:00 2001 From: LukaOber Date: Mon, 7 Oct 2024 13:42:27 +0200 Subject: [PATCH 1/2] added type for custom smoothness --- charming/src/element/mod.rs | 1 + charming/src/element/smoothness.rs | 49 ++++++++++++++++++++++++++++++ charming/src/series/line.rs | 8 ++--- charming/src/series/parallel.rs | 6 ++-- 4 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 charming/src/element/smoothness.rs diff --git a/charming/src/element/mod.rs b/charming/src/element/mod.rs index 14ed0b6..69819ab 100644 --- a/charming/src/element/mod.rs +++ b/charming/src/element/mod.rs @@ -36,6 +36,7 @@ pub mod pointer; pub mod scale_limit; pub mod select; pub mod shape; +pub mod smoothness; pub mod sort; pub mod split_area; pub mod split_line; diff --git a/charming/src/element/smoothness.rs b/charming/src/element/smoothness.rs new file mode 100644 index 0000000..7a8aae5 --- /dev/null +++ b/charming/src/element/smoothness.rs @@ -0,0 +1,49 @@ +use serde::Serialize; + +#[derive(Debug, PartialEq, PartialOrd, Clone)] +pub enum Smoothness { + Single(f64), + Boolean(bool), +} + +impl Serialize for Smoothness { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match self { + Smoothness::Single(smoothness) => serializer.serialize_f64(*smoothness), + Smoothness::Boolean(smoothness) => serializer.serialize_bool(*smoothness), + } + } +} + +impl From for Smoothness { + fn from(value: f64) -> Self { + Smoothness::Single(value) + } +} + +impl From for Smoothness { + fn from(value: f32) -> Self { + Smoothness::Single(value as f64) + } +} + +impl From for Smoothness { + fn from(value: i32) -> Self { + Smoothness::Single(value as f64) + } +} + +impl From for Smoothness { + fn from(value: u32) -> Self { + Smoothness::Single(value as f64) + } +} + +impl From for Smoothness { + fn from(value: bool) -> Self { + Smoothness::Boolean(value) + } +} diff --git a/charming/src/series/line.rs b/charming/src/series/line.rs index 99f03e5..100fffd 100644 --- a/charming/src/series/line.rs +++ b/charming/src/series/line.rs @@ -3,8 +3,8 @@ use serde::Serialize; use crate::{ datatype::{DataFrame, DataPoint}, element::{ - AreaStyle, CoordinateSystem, DimensionEncode, Emphasis, ItemStyle, Label, LineStyle, - MarkArea, MarkLine, MarkPoint, Symbol, + smoothness::Smoothness, AreaStyle, CoordinateSystem, DimensionEncode, Emphasis, ItemStyle, + Label, LineStyle, MarkArea, MarkLine, MarkPoint, Symbol, }, }; @@ -51,7 +51,7 @@ pub struct Line { emphasis: Option, #[serde(skip_serializing_if = "Option::is_none")] - smooth: Option, + smooth: Option, #[serde(skip_serializing_if = "Option::is_none")] connect_nulls: Option, @@ -178,7 +178,7 @@ impl Line { } /// Smoothness. - pub fn smooth>(mut self, smooth: F) -> Self { + pub fn smooth>(mut self, smooth: S) -> Self { self.smooth = Some(smooth.into()); self } diff --git a/charming/src/series/parallel.rs b/charming/src/series/parallel.rs index 0754b5d..fe6c2b9 100644 --- a/charming/src/series/parallel.rs +++ b/charming/src/series/parallel.rs @@ -2,7 +2,7 @@ use serde::Serialize; use crate::{ datatype::{DataFrame, DataPoint}, - element::{ColorBy, CoordinateSystem, Emphasis, LineStyle}, + element::{smoothness::Smoothness, ColorBy, CoordinateSystem, Emphasis, LineStyle}, }; #[derive(Serialize)] @@ -49,7 +49,7 @@ pub struct Parallel { realtime: Option, #[serde(skip_serializing_if = "Option::is_none")] - smooth: Option, + smooth: Option, #[serde(skip_serializing_if = "Option::is_none")] progressive: Option, @@ -142,7 +142,7 @@ impl Parallel { self } - pub fn smooth>(mut self, smooth: F) -> Self { + pub fn smooth>(mut self, smooth: S) -> Self { self.smooth = Some(smooth.into()); self } From 8553b68754e3eb1e4065cdbc868ec151f12b77f9 Mon Sep 17 00:00:00 2001 From: LukaOber Date: Mon, 7 Oct 2024 13:45:24 +0200 Subject: [PATCH 2/2] fixed gallery with wrong smoothness --- gallery/src/line/area_pieces.rs | 2 +- gallery/src/line/distribution_of_electricity.rs | 2 +- gallery/src/line/gradient_stacked_area.rs | 10 +++++----- gallery/src/line/smoothed_line.rs | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gallery/src/line/area_pieces.rs b/gallery/src/line/area_pieces.rs index 5b9f152..e563ffd 100644 --- a/gallery/src/line/area_pieces.rs +++ b/gallery/src/line/area_pieces.rs @@ -35,7 +35,7 @@ pub fn chart() -> Chart { ) .series( Line::new() - .smooth(0.6) + .smooth(true) .symbol(Symbol::None) .line_style(LineStyle::new().width(5).color("#5470C6")) .area_style(AreaStyle::new()) diff --git a/gallery/src/line/distribution_of_electricity.rs b/gallery/src/line/distribution_of_electricity.rs index 326a0a6..45ceae3 100644 --- a/gallery/src/line/distribution_of_electricity.rs +++ b/gallery/src/line/distribution_of_electricity.rs @@ -51,7 +51,7 @@ pub fn chart() -> Chart { .series( Line::new() .name("Electricity") - .smooth(0.5) + .smooth(true) .connect_nulls(true) .data(vec![ Some(300), diff --git a/gallery/src/line/gradient_stacked_area.rs b/gallery/src/line/gradient_stacked_area.rs index cbf15c3..ad90574 100644 --- a/gallery/src/line/gradient_stacked_area.rs +++ b/gallery/src/line/gradient_stacked_area.rs @@ -61,7 +61,7 @@ pub fn chart() -> Chart { }) .opacity(0.8), ) - .smooth(0.5) + .smooth(true) .data(vec![140, 232, 101, 264, 90, 340, 250]), ) .series( @@ -84,7 +84,7 @@ pub fn chart() -> Chart { }) .opacity(0.8), ) - .smooth(0.5) + .smooth(true) .data(vec![120, 282, 111, 234, 220, 340, 310]), ) .series( @@ -107,7 +107,7 @@ pub fn chart() -> Chart { }) .opacity(0.8), ) - .smooth(0.5) + .smooth(true) .data(vec![320, 132, 201, 334, 190, 130, 220]), ) .series( @@ -130,7 +130,7 @@ pub fn chart() -> Chart { }) .opacity(0.8), ) - .smooth(0.5) + .smooth(true) .data(vec![220, 402, 231, 134, 190, 230, 120]), ) .series( @@ -153,7 +153,7 @@ pub fn chart() -> Chart { }) .opacity(0.8), ) - .smooth(0.5) + .smooth(true) .data(vec![220, 302, 181, 234, 210, 290, 150]), ) } diff --git a/gallery/src/line/smoothed_line.rs b/gallery/src/line/smoothed_line.rs index e23cf8e..e173d7b 100644 --- a/gallery/src/line/smoothed_line.rs +++ b/gallery/src/line/smoothed_line.rs @@ -10,7 +10,7 @@ pub fn chart() -> Chart { .y_axis(Axis::new().type_(AxisType::Value)) .series( Line::new() - .smooth(0.5) + .smooth(true) .data(vec![820, 932, 901, 934, 1290, 1330, 1320]), ) }