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

Add step line #107

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,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 @@ -40,6 +40,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 @@ -87,6 +88,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
9 changes: 9 additions & 0 deletions charming/src/element/step.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use serde::Serialize;

#[derive(Serialize, Debug, PartialEq, PartialOrd, Clone)]
#[serde(rename_all = "snake_case")]
pub enum Step {
Start,
Middle,
End,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just about to create a PR with the support for this same feature =)
We could've made it fully compatible with Echarts by adding bool variants:

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
        }
    }
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, although maybe I'm missing the serialization part, true and false would have to become boolean literals rather than strings, anyway, just a suggestion

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be correct. It should be fairly easy and examples of it are in the repo.

I will take a look at this PR and merge it at the start of next week.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cooljingle Do you want to implement the suggestion from grinya, the implementation From<bool> and the correct serialization of Step::True and Step::False?

}
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 @@ -107,6 +110,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 @@ -187,6 +191,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 @@ -122,6 +122,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.
Loading