Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom smoothness #92

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions charming/src/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 49 additions & 0 deletions charming/src/element/smoothness.rs
Original file line number Diff line number Diff line change
@@ -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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Smoothness::Single(smoothness) => serializer.serialize_f64(*smoothness),
Smoothness::Boolean(smoothness) => serializer.serialize_bool(*smoothness),
}
}
}

impl From<f64> for Smoothness {
fn from(value: f64) -> Self {
Smoothness::Single(value)
}
}

impl From<f32> for Smoothness {
fn from(value: f32) -> Self {
Smoothness::Single(value as f64)
}
}

impl From<i32> for Smoothness {
fn from(value: i32) -> Self {
Smoothness::Single(value as f64)
}
}

impl From<u32> for Smoothness {
fn from(value: u32) -> Self {
Smoothness::Single(value as f64)
}
}

impl From<bool> for Smoothness {
fn from(value: bool) -> Self {
Smoothness::Boolean(value)
}
}
8 changes: 4 additions & 4 deletions charming/src/series/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};

Expand Down Expand Up @@ -51,7 +51,7 @@ pub struct Line {
emphasis: Option<Emphasis>,

#[serde(skip_serializing_if = "Option::is_none")]
smooth: Option<f64>,
smooth: Option<Smoothness>,

#[serde(skip_serializing_if = "Option::is_none")]
connect_nulls: Option<bool>,
Expand Down Expand Up @@ -178,7 +178,7 @@ impl Line {
}

/// Smoothness.
pub fn smooth<F: Into<f64>>(mut self, smooth: F) -> Self {
pub fn smooth<S: Into<Smoothness>>(mut self, smooth: S) -> Self {
self.smooth = Some(smooth.into());
self
}
Expand Down
6 changes: 3 additions & 3 deletions charming/src/series/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -49,7 +49,7 @@ pub struct Parallel {
realtime: Option<bool>,

#[serde(skip_serializing_if = "Option::is_none")]
smooth: Option<f64>,
smooth: Option<Smoothness>,

#[serde(skip_serializing_if = "Option::is_none")]
progressive: Option<f64>,
Expand Down Expand Up @@ -142,7 +142,7 @@ impl Parallel {
self
}

pub fn smooth<F: Into<f64>>(mut self, smooth: F) -> Self {
pub fn smooth<S: Into<Smoothness>>(mut self, smooth: S) -> Self {
self.smooth = Some(smooth.into());
self
}
Expand Down
2 changes: 1 addition & 1 deletion gallery/src/line/area_pieces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion gallery/src/line/distribution_of_electricity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
10 changes: 5 additions & 5 deletions gallery/src/line/gradient_stacked_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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]),
)
}
2 changes: 1 addition & 1 deletion gallery/src/line/smoothed_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
)
}
Loading