Skip to content

Commit

Permalink
Merge pull request #107 from cooljingle/step-line
Browse files Browse the repository at this point in the history
Add step line
  • Loading branch information
LukaOber authored Dec 14, 2024
2 parents 21250ae + 8fb2611 commit 47ad138
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ You can also clone the repo and run `cargo run --bin gallery` to view the intera
<a href="./gallery/src/line/smoothed_line.rs"><img src="./img/line/smoothed_line.svg" width="40%" alt="Smoothed Line" /></a>
<a href="./gallery/src/line/stacked_area.rs"><img src="./img/line/stacked_area.svg" width="40%" alt="Stacked Area" /></a>
<a href="./gallery/src/line/stacked_line.rs"><img src="./img/line/stacked_line.svg" width="40%" alt="Stacked Line" /></a>
<a href="./gallery/src/line/step_line.rs"><img src="./img/line/step_line.svg" width="40%" alt="Step Line" /></a>
<a href="./gallery/src/line/temperature_change.rs"><img src="./img/line/temperature_change.svg" width="40%" alt="Temperature Change" /></a>
<a href="./gallery/src/line/two_value_axes_in_polar.rs"><img src="./img/line/two_value_axes_in_polar.svg" width="40%" alt="Two Value-Axes in Polar" /></a>
</div>
Expand Down
2 changes: 2 additions & 0 deletions charming/src/element/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub mod smoothness;
pub mod sort;
pub mod split_area;
pub mod split_line;
pub mod step;
pub mod symbol;
pub mod symbol_size;
pub mod text_align;
Expand Down Expand Up @@ -88,6 +89,7 @@ pub use shape::*;
pub use sort::*;
pub use split_area::*;
pub use split_line::*;
pub use step::*;
pub use symbol::*;
pub use symbol_size::*;
pub use text_align::*;
Expand Down
77 changes: 77 additions & 0 deletions charming/src/element/step.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use serde::{de::Visitor, Deserialize, Serialize};

#[derive(Debug, PartialEq, PartialOrd, Clone)]
pub enum Step {
True,
False,
Start,
Middle,
End,
}

impl From<bool> for Step {
fn from(value: bool) -> Self {
if value {
Step::True
} else {
Step::False
}
}
}

impl Serialize for Step {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match self {
Step::True => serializer.serialize_bool(true),
Step::False => serializer.serialize_bool(false),
Step::Start => serializer.serialize_str("start"),
Step::Middle => serializer.serialize_str("middle"),
Step::End => serializer.serialize_str("end"),
}
}
}

impl<'de> Deserialize<'de> for Step {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct StepVisitor;

impl Visitor<'_> for StepVisitor {
type Value = Step;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str(
"a boolean or a string containing either \"start\", \"middle\" or \"end\"",
)
}

fn visit_bool<E>(self, v: bool) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match v {
true => Ok(Step::True),
false => Ok(Step::False),
}
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match v {
"start" => Ok(Step::Start),
"middle" => Ok(Step::Middle),
"end" => Ok(Step::End),
_ => Err(E::custom("unable to parse step type, invalid string")),
}
}
}
deserializer.deserialize_any(StepVisitor)
}
}
11 changes: 10 additions & 1 deletion charming/src/series/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
datatype::{DataFrame, DataPoint},
element::{
smoothness::Smoothness, AreaStyle, CoordinateSystem, DimensionEncode, Emphasis, ItemStyle,
Label, LineStyle, MarkArea, MarkLine, MarkPoint, Symbol, SymbolSize, Tooltip,
Label, LineStyle, MarkArea, MarkLine, MarkPoint, Step, Symbol, SymbolSize, Tooltip,
},
};

Expand Down Expand Up @@ -53,6 +53,9 @@ pub struct Line {
#[serde(skip_serializing_if = "Option::is_none")]
smooth: Option<Smoothness>,

#[serde(skip_serializing_if = "Option::is_none")]
step: Option<Step>,

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

Expand Down Expand Up @@ -113,6 +116,7 @@ impl Line {
item_style: None,
emphasis: None,
smooth: None,
step: None,
connect_nulls: None,
mark_point: None,
mark_line: None,
Expand Down Expand Up @@ -195,6 +199,11 @@ impl Line {
self
}

pub fn step<S: Into<Step>>(mut self, step: S) -> Self {
self.step = Some(step.into());
self
}

pub fn connect_nulls(mut self, connect_nulls: bool) -> Self {
self.connect_nulls = Some(connect_nulls);
self
Expand Down
1 change: 1 addition & 0 deletions gallery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ lazy_static! {
insert!(m, line, smoothed_line);
insert!(m, line, stacked_area);
insert!(m, line, stacked_line);
insert!(m, line, step_line);
insert!(m, line, temperature_change);
insert!(m, line, two_value_axes_in_polar);
m
Expand Down
1 change: 1 addition & 0 deletions gallery/src/line/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ pub mod rainfall_vs_evaporation;
pub mod smoothed_line;
pub mod stacked_area;
pub mod stacked_line;
pub mod step_line;
pub mod temperature_change;
pub mod two_value_axes_in_polar;
46 changes: 46 additions & 0 deletions gallery/src/line/step_line.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use charming::{
component::{Axis, Feature, Grid, Legend, SaveAsImage, Title, Toolbox},
element::{AxisType, Step, Tooltip, Trigger},
series::Line,
Chart,
};

pub fn chart() -> Chart {
Chart::new()
.title(Title::new().text("Step Line"))
.tooltip(Tooltip::new().trigger(Trigger::Axis))
.legend(Legend::new())
.grid(
Grid::new()
.left("3%")
.right("4%")
.bottom("3%")
.contain_label(true),
)
.toolbox(Toolbox::new().feature(Feature::new().save_as_image(SaveAsImage::new())))
.x_axis(
Axis::new()
.type_(AxisType::Category)
.boundary_gap(false)
.data(vec!["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]),
)
.y_axis(Axis::new().type_(AxisType::Value))
.series(
Line::new()
.name("Step Start")
.step(Step::Start)
.data(vec![120, 132, 101, 134, 90, 230, 210]),
)
.series(
Line::new()
.name("Step Middle")
.step(Step::Middle)
.data(vec![220, 282, 201, 234, 290, 430, 410]),
)
.series(
Line::new()
.name("Step End")
.step(Step::End)
.data(vec![450, 432, 401, 454, 590, 530, 510]),
)
}
117 changes: 117 additions & 0 deletions img/line/step_line.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 47ad138

Please sign in to comment.