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 axis labels formatting; Height and width limits changed; Axis are now rendered before the graph to prevent artifacts on colored diagrams; #47

Merged
merged 10 commits into from
Sep 11, 2023

Conversation

uandere
Copy link
Contributor

@uandere uandere commented Sep 8, 2023

This pull request allows a user to format the labels of the axis:

    Chart::new_with_y_range(200, 50, 0.0, (end - start).num_days() as f32, 0.0, 25_000.0)
        .linecolorplot(&Shape::Continuous(Box::new(|x| 1000.0 * (5.0 * (0.5 * x).sin() + 0.05 * x) + 9000.0)), rgb::RGB { r: 10, g: 100, b: 200 })
        .x_label_format(LabelFormat::Custom(Box::new(move |val| {format!("{}", start + Duration::days(val as i64))})))
        .y_label_format(LabelFormat::Value)
        .display();

This will result in following chart:

image

It was accomplished using Builder Pattern: the implementation of LabelBuilder trait adds two methods to a Chart, namely x_label_format() and y_label_format(), which allow to specify the format of the corresponding label.

The enum of label formats is:

/// Specifies label format.
/// Default value is `LabelFormat::Value`.
pub enum LabelFormat {
    /// Label is not displayed.
    None,
    /// Label is shown as a value.
    Value,
    /// Label is shown as a custom string.
    Custom(Box<dyn Fn(f32) -> String>),
}

This pull also changes height limit to 3. This will allow user to build spark-lines in the future.

I also changed the order of axis/graph rendering so that axis are rendered first.

The inspiration for the pull request was taken from here.

@uandere uandere changed the title Added axis labels formatting; Height and width limits removed; Added axis labels formatting; Height and width limits removed; Axis are now rendered before the graph to prevent artifacts on colored diagrams; Sep 8, 2023
@uandere uandere changed the title Added axis labels formatting; Height and width limits removed; Axis are now rendered before the graph to prevent artifacts on colored diagrams; Added axis labels formatting; Height and width limits changed; Axis are now rendered before the graph to prevent artifacts on colored diagrams; Sep 8, 2023
@loony-bean loony-bean merged commit a0ce929 into loony-bean:master Sep 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants