diff --git a/README.md b/README.md index 831253a..54bacc1 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,8 @@ is still being built. - [x] Basic hello world - [x] Plot flags - [ ] Plotting functionality - - [ ] Line plot - - [x] Single y axis - - [ ] Multiple y axes + - [x] Line plot + - [x] Text plot - [ ] Scatter plot - [ ] Bar plot - [ ] Vertical @@ -34,7 +33,6 @@ is still being built. - [ ] Error bar plot - [ ] Vertical - [ ] Horizontal - - [ ] Text - [ ] Heatmap - [ ] Pie chart - [ ] Digital data diff --git a/implot-examples/examples/hello_world.rs b/implot-examples/examples/hello_world.rs index bf8de18..441b948 100644 --- a/implot-examples/examples/hello_world.rs +++ b/implot-examples/examples/hello_world.rs @@ -1,5 +1,5 @@ use imgui::*; -use implot::{AxisFlags, Plot, PlotFlags, PlotLine}; +use implot::{AxisFlags, Plot, PlotFlags, PlotLine, PlotText}; mod support; @@ -28,14 +28,21 @@ fn main() { .x_limits(0.0, 6.0, Condition::FirstUseEver) .y_limits(-1.0, 3.0, Condition::FirstUseEver) .with_plot_flags(&(PlotFlags::DEFAULT)) - .with_y_axis_flags(&(AxisFlags::INVERT)) + .with_y_axis_flags(&(AxisFlags::DEFAULT | AxisFlags::INVERT)) .build(|| { + // Line plotting PlotLine::new("Left eye").plot(&vec![2.0, 2.0], &vec![2.0, 1.0]); PlotLine::new("Right eye").plot(&vec![4.0, 4.0], &vec![2.0, 1.0]); - let x_values = vec![1.0, 2.0, 4.0, 5.0]; let y_values = vec![1.0, 0.0, 0.0, 1.0]; PlotLine::new("Mouth").plot(&x_values, &y_values); + + // Text + PlotText::new("Text!").plot(2.0, 2.0, false); + PlotText::new("Text with offset!") + .with_pixel_offset(10.0, 30.0) + .plot(2.0, 2.0, false); + PlotText::new("Vertical Text!").plot(0.1, 2.5, true); }); }); diff --git a/src/lib.rs b/src/lib.rs index 70bcfbc..08605de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -320,8 +320,9 @@ pub struct PlotLine { } impl PlotLine { + /// Create a new line to be plotted. Does not draw anything yet. pub fn new(label: &str) -> Self { - PlotLine { + Self { label: label.to_owned(), } } @@ -333,7 +334,7 @@ impl PlotLine { return; } unsafe { - implot_sys::ImPlot_PlotLinedoublePtrdoublePtr( + sys::ImPlot_PlotLinedoublePtrdoublePtr( im_str!("{}", self.label).as_ptr() as *const i8, x.as_ptr(), y.as_ptr(), @@ -345,6 +346,61 @@ impl PlotLine { } } +/// Struct to provide functionality for adding text within a plot +pub struct PlotText { + /// Label to show in plot + label: String, + + /// X component of the pixel offset to be used. Will be used independently of the actual plot + /// scaling. Defaults to 0. + pixel_offset_x: f32, + + /// Y component of the pixel offset to be used. Will be used independently of the actual plot + /// scaling. Defaults to 0. + pixel_offset_y: f32, +} + +impl PlotText { + /// Create a new text label to be shown. Does not draw anything yet. + pub fn new(label: &str) -> Self { + Self { + label: label.into(), + pixel_offset_x: 0.0, + pixel_offset_y: 0.0, + } + } + + /// Add a pixel offset to the text to be plotted. This offset will be independent of the + /// scaling of the plot itself. + pub fn with_pixel_offset(mut self, offset_x: f32, offset_y: f32) -> Self { + self.pixel_offset_x = offset_x; + self.pixel_offset_y = offset_y; + self + } + + /// Draw the text label in the plot at the given position, optionally vertically. Use this in + /// closures passed to [`Plot::build()`](struct.Plot.html#method.build) + pub fn plot(&self, x: f64, y: f64, vertical: bool) { + // If there is nothing to show, don't do anything + if self.label == "" { + return; + } + + unsafe { + sys::ImPlot_PlotTextdouble( + im_str!("{}", self.label).as_ptr() as *const i8, + x, + y, + vertical, + sys::ImVec2 { + x: self.pixel_offset_x, + y: self.pixel_offset_y, + }, + ); + } + } +} + /// 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 /// already - if you find something missing you'd really like, raise an issue.