Skip to content

Commit

Permalink
added custom smoothness for bool support
Browse files Browse the repository at this point in the history
  • Loading branch information
LukaOber committed Oct 3, 2024
1 parent be38cae commit 32d90c1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
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
36 changes: 36 additions & 0 deletions charming/src/element/smoothness.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use serde::Serialize;

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(smoothness: f64) -> Self {
Smoothness::Single(smoothness)
}
}

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

impl From<bool> for Smoothness {
fn from(smoothness: bool) -> Self {
Smoothness::Boolean(smoothness)
}
}
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")]
mark_point: Option<MarkPoint>,
Expand Down Expand Up @@ -174,7 +174,7 @@ impl Line {
}

/// Smoothness.
pub fn smooth<F: Into<f64>>(mut self, smooth: F) -> Self {
pub fn smooth<F: Into<Smoothness>>(mut self, smooth: F) -> 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<F: Into<Smoothness>>(mut self, smooth: F) -> Self {
self.smooth = Some(smooth.into());
self
}
Expand Down

0 comments on commit 32d90c1

Please sign in to comment.