-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from cooljingle/step-line
Add step line
- Loading branch information
Showing
8 changed files
with
255 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]), | ||
) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.