Added text plot support

This commit is contained in:
4bb4 2020-08-23 20:31:28 +02:00
parent 84d10092be
commit 90f7dba1a7
3 changed files with 70 additions and 9 deletions

View file

@ -24,9 +24,8 @@ is still being built.
- [x] Basic hello world - [x] Basic hello world
- [x] Plot flags - [x] Plot flags
- [ ] Plotting functionality - [ ] Plotting functionality
- [ ] Line plot - [x] Line plot
- [x] Single y axis - [x] Text plot
- [ ] Multiple y axes
- [ ] Scatter plot - [ ] Scatter plot
- [ ] Bar plot - [ ] Bar plot
- [ ] Vertical - [ ] Vertical
@ -34,7 +33,6 @@ is still being built.
- [ ] Error bar plot - [ ] Error bar plot
- [ ] Vertical - [ ] Vertical
- [ ] Horizontal - [ ] Horizontal
- [ ] Text
- [ ] Heatmap - [ ] Heatmap
- [ ] Pie chart - [ ] Pie chart
- [ ] Digital data - [ ] Digital data

View file

@ -1,5 +1,5 @@
use imgui::*; use imgui::*;
use implot::{AxisFlags, Plot, PlotFlags, PlotLine}; use implot::{AxisFlags, Plot, PlotFlags, PlotLine, PlotText};
mod support; mod support;
@ -28,14 +28,21 @@ fn main() {
.x_limits(0.0, 6.0, Condition::FirstUseEver) .x_limits(0.0, 6.0, Condition::FirstUseEver)
.y_limits(-1.0, 3.0, Condition::FirstUseEver) .y_limits(-1.0, 3.0, Condition::FirstUseEver)
.with_plot_flags(&(PlotFlags::DEFAULT)) .with_plot_flags(&(PlotFlags::DEFAULT))
.with_y_axis_flags(&(AxisFlags::INVERT)) .with_y_axis_flags(&(AxisFlags::DEFAULT | AxisFlags::INVERT))
.build(|| { .build(|| {
// Line plotting
PlotLine::new("Left eye").plot(&vec![2.0, 2.0], &vec![2.0, 1.0]); 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]); 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 x_values = vec![1.0, 2.0, 4.0, 5.0];
let y_values = vec![1.0, 0.0, 0.0, 1.0]; let y_values = vec![1.0, 0.0, 0.0, 1.0];
PlotLine::new("Mouth").plot(&x_values, &y_values); 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);
}); });
}); });

View file

@ -320,8 +320,9 @@ pub struct PlotLine {
} }
impl PlotLine { impl PlotLine {
/// Create a new line to be plotted. Does not draw anything yet.
pub fn new(label: &str) -> Self { pub fn new(label: &str) -> Self {
PlotLine { Self {
label: label.to_owned(), label: label.to_owned(),
} }
} }
@ -333,7 +334,7 @@ impl PlotLine {
return; return;
} }
unsafe { unsafe {
implot_sys::ImPlot_PlotLinedoublePtrdoublePtr( sys::ImPlot_PlotLinedoublePtrdoublePtr(
im_str!("{}", self.label).as_ptr() as *const i8, im_str!("{}", self.label).as_ptr() as *const i8,
x.as_ptr(), x.as_ptr(),
y.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 /// 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 /// 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. /// already - if you find something missing you'd really like, raise an issue.