Added stem plots and legend entry hover checking

This commit is contained in:
4bb4 2021-01-31 12:40:13 +01:00
parent 2c56bd9803
commit 21aa28591c
7 changed files with 113 additions and 10 deletions

View file

@ -20,6 +20,7 @@ use implot_sys as sys;
// TODO(4bb4) facade-wrap these?
pub use self::{context::*, plot::*, plot_elements::*};
use imgui::im_str;
pub use sys::{ImPlotLimits, ImPlotPoint, ImPlotRange, ImVec2, ImVec4};
mod context;
@ -572,6 +573,11 @@ pub fn is_plot_y_axis_hovered(y_axis_choice: Option<YAxisChoice>) -> bool {
unsafe { sys::ImPlot_IsPlotYAxisHovered(y_axis_choice_i32) }
}
/// Returns true if the given item in the legend of the current plot is hovered.
pub fn is_legend_entry_hovered(legend_entry: &str) -> bool {
unsafe { sys::ImPlot_IsLegendEntryHovered(im_str!("{}", legend_entry).as_ptr() as *const i8) }
}
// --- Demo window -------------------------------------------------------------------------------
/// Show the demo window for poking around what functionality implot has to
/// offer. Note that not all of this is necessarily implemented in implot-rs

View file

@ -147,7 +147,7 @@ impl PlotBars {
/// Draw a previously-created bar plot. Use this in closures passed to
/// [`Plot::build()`](struct.Plot.html#method.build). The `axis_positions`
/// specify where on the corersponding axis (X for vertical mode, Y for horizontal mode) the
/// specify where on the corresponding axis (X for vertical mode, Y for horizontal mode) the
/// bar is drawn, and the `bar_values` specify what values the bars have.
pub fn plot(&self, axis_positions: &[f64], bar_values: &[f64]) {
let number_of_points = axis_positions.len().min(bar_values.len());
@ -332,3 +332,51 @@ impl PlotHeatmap {
}
}
}
/// Struct to provide stem plotting functionality.
pub struct PlotStems {
/// Label to show in the legend for this line
label: String,
/// Reference value for the y value, which the stems are "with respect to"
reference_y: f64,
}
impl PlotStems {
/// Create a new stem plot to be shown. Does not draw anything by itself, call
/// [`PlotStems::plot`] on the struct for that.
pub fn new(label: &str) -> Self {
Self {
label: label.to_owned(),
reference_y: 0.0, // Default value taken from C++ implot
}
}
/// Set the reference y value for the stems
pub fn with_reference_y(mut self, reference_y: f64) -> Self {
self.reference_y = reference_y;
self
}
/// Draw a previously-created stem plot. Use this in closures passed to
/// [`Plot::build()`](struct.Plot.html#method.build). The `axis_positions` specify where on the
/// X axis the stems are drawn, and the `stem_values` specify what values the stems have.
pub fn plot(&self, axis_positions: &[f64], stem_values: &[f64]) {
let number_of_points = axis_positions.len().min(stem_values.len());
// If there is no data to plot, we stop here
if number_of_points == 0 {
return;
}
unsafe {
sys::ImPlot_PlotStemsdoublePtrdoublePtr(
im_str!("{}", self.label).as_ptr() as *const i8,
axis_positions.as_ptr(),
stem_values.as_ptr(),
number_of_points as i32, // "as" casts saturate as of Rust 1.45. This is safe here.
self.reference_y,
0, // No offset
std::mem::size_of::<f64>() as i32, // Stride, set to one f64 for the standard use case
);
}
}
}