From 1643032c7a71ef88c298d38835a36d5d636fb9a3 Mon Sep 17 00:00:00 2001 From: 4bb4 <67376761+4bb4@users.noreply.github.com> Date: Sun, 8 Nov 2020 19:41:51 +0100 Subject: [PATCH] Added stairs plot support --- README.md | 2 +- implot-examples/examples-shared/src/lib.rs | 2 + .../examples-shared/src/line_plots.rs | 6 +-- .../examples-shared/src/stairs_plots.rs | 40 +++++++++++++++++++ src/plot_elements.rs | 34 ++++++++++++++++ 5 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 implot-examples/examples-shared/src/stairs_plots.rs diff --git a/README.md b/README.md index a9d9c47..6a6e80f 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ created for 64-bit floats. - [x] Bar plot - [x] Vertical - [x] Horizontal + - [x] Stairs plot - [ ] Shaded plot - [ ] Stem plots - [ ] Images @@ -70,7 +71,6 @@ created for 64-bit floats. - [ ] Heatmap - [ ] Pie chart - [ ] Digital data - - [ ] Stairs plot - [ ] Annotations - [ ] Dragline - [ ] Dragpoint diff --git a/implot-examples/examples-shared/src/lib.rs b/implot-examples/examples-shared/src/lib.rs index fcda920..3476475 100644 --- a/implot-examples/examples-shared/src/lib.rs +++ b/implot-examples/examples-shared/src/lib.rs @@ -1,6 +1,7 @@ pub mod bar_plots; pub mod line_plots; pub mod scatter_plots; +pub mod stairs_plots; pub mod text_plots; use imgui::Ui; @@ -11,4 +12,5 @@ pub fn show_demos(ui: &Ui, plot_ui: &PlotUi) { line_plots::show_demo_window(ui, plot_ui); scatter_plots::show_demo_window(ui, plot_ui); text_plots::show_demo_window(ui, plot_ui); + stairs_plots::show_demo_window(ui, plot_ui); } diff --git a/implot-examples/examples-shared/src/line_plots.rs b/implot-examples/examples-shared/src/line_plots.rs index b19a453..6858930 100644 --- a/implot-examples/examples-shared/src/line_plots.rs +++ b/implot-examples/examples-shared/src/line_plots.rs @@ -309,9 +309,9 @@ pub fn show_demo_window(ui: &Ui, plot_ui: &PlotUi) { ui.text(im_str!("Hello from implot-rs!")); ui.text_wrapped(im_str!( "The headers here demo the line plotting features of the library. \ - Have a look at the example source code to see how they are implemented.\n\ - Check out the demo from ImPlot itself first \ - for instructions on how to interact with ImPlot plots." + Have a look at the example source code to see how they are implemented.\n\ + Check out the demo from ImPlot itself first \ + for instructions on how to interact with ImPlot plots." )); // Show individual examples in collapsed headers diff --git a/implot-examples/examples-shared/src/stairs_plots.rs b/implot-examples/examples-shared/src/stairs_plots.rs new file mode 100644 index 0000000..7710397 --- /dev/null +++ b/implot-examples/examples-shared/src/stairs_plots.rs @@ -0,0 +1,40 @@ +//! This example demonstrates how stairs plots are to be used. They are almost the same as line +//! plots, so head over to the line plots example for more info. +//! +use imgui::{im_str, CollapsingHeader, Condition, Ui, Window}; +use implot::{Plot, PlotStairs, PlotUi}; + +pub fn show_basic_plot(ui: &Ui, plot_ui: &PlotUi) { + ui.text_wrapped(im_str!( + "This header just plots a stairs-style line with as little code as possible." + )); + let content_width = ui.window_content_region_width(); + Plot::new("Simple stairs plot") + // The size call could also be omitted, though the defaults don't consider window + // width, which is why we're not doing so here. + .size(content_width, 300.0) + .build(plot_ui, || { + // If this is called outside a plot build callback, the program will panic. + let x_positions = vec![0.1, 0.2, 0.5]; + let y_positions = vec![0.1, 0.3, 0.9]; + PlotStairs::new("legend label").plot(&x_positions, &y_positions); + }); +} + +pub fn show_demo_window(ui: &Ui, plot_ui: &PlotUi) { + Window::new(im_str!("Stairs plots example")) + .size([430.0, 450.0], Condition::FirstUseEver) + .build(ui, || { + ui.text(im_str!("Hello from implot-rs!")); + ui.text_wrapped(im_str!( + "The headers here demo the stairs plotting features of the library. \ + Have a look at the example source code to see how they are implemented.\n\ + Check out the demo from ImPlot itself first \ + for instructions on how to interact with ImPlot plots." + )); + + if CollapsingHeader::new(im_str!("Basic stairs plot")).build(&ui) { + show_basic_plot(&ui, &plot_ui); + } + }); +} diff --git a/src/plot_elements.rs b/src/plot_elements.rs index c844001..f2e4bc0 100644 --- a/src/plot_elements.rs +++ b/src/plot_elements.rs @@ -40,6 +40,40 @@ impl PlotLine { } } +/// Struct to provide functionality for plotting a line in a plot with stairs style. +pub struct PlotStairs { + /// Label to show in the legend for this line + label: String, +} + +impl PlotStairs { + /// Create a new line to be plotted. Does not draw anything yet. + pub fn new(label: &str) -> Self { + Self { + label: label.to_owned(), + } + } + + /// Plot a stairs style line. Use this in closures passed to + /// [`Plot::build()`](struct.Plot.html#method.build) + pub fn plot(&self, x: &Vec, y: &Vec) { + // If there is no data to plot, we stop here + if x.len().min(y.len()) == 0 { + return; + } + unsafe { + sys::ImPlot_PlotStairsdoublePtrdoublePtr( + im_str!("{}", self.label).as_ptr() as *const i8, + x.as_ptr(), + y.as_ptr(), + x.len().min(y.len()) as i32, // "as" casts saturate as of Rust 1.45. This is safe here. + 0, // No offset + std::mem::size_of::() as i32, // Stride, set to one f64 for the standard use case + ); + } + } +} + /// Struct to provide functionality for creating a scatter plot pub struct PlotScatter { /// Label to show in the legend for this scatter plot