loony-bean / textplots-rs

Terminal plotting library for Rust
238 stars 24 forks source link

Add axes formatting callbacks #37

Closed KoffeinFlummi closed 1 year ago

KoffeinFlummi commented 2 years ago

This adds the ability to specify a callback for formatting the min/max labels for both the x and y axis. This allows formatting the x axis as dates (#9) or adding units, and including colored strings.

I have also included a commit that changes the rendering order so that the axes are printed behind the plots. This prevents the white of the axes resetting the color of the plots. If you want, I can split this up into multiple PRs.

Example Code (from and to are chrono::NaiveDate):

        let mut chart = Chart::new(200, 40, 0.0, (to - from).num_days() as f32)
            .xaxis_formatter(move |x| format!("{}", from.clone() + Duration::days(x as i64)))
            .yaxis_formatter(move |y| format!("{:.1} {}", y, unit.clone().white()));

Result (without reordering commit):

Result (with reordering commit):

loony-bean commented 1 year ago

This looks totally amazing @KoffeinFlummi! By any chance, could you include fully working example (into examples dir) that will render the picture similar to what you have put in this PR?

loony-bean commented 1 year ago

Requested features added as part of #47. Thanks again @KoffeinFlummi for this PR and inspiration.

KoffeinFlummi commented 1 year ago

Thanks to @uandere and apologies for the silence, I've unfortunately been pretty busy these past few weeks.