Added stairs plot support

This commit is contained in:
4bb4 2020-11-08 19:41:51 +01:00
parent 7c477ec551
commit 1643032c7a
5 changed files with 80 additions and 4 deletions

View file

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

View file

@ -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);
}

View file

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

View file

@ -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);
}
});
}

View file

@ -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<f64>, y: &Vec<f64>) {
// 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::<f64>() 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