From f05d681d259775d1171f05d47984e6785fdc450c Mon Sep 17 00:00:00 2001 From: 4bb4 <67376761+4bb4@users.noreply.github.com> Date: Sun, 13 Sep 2020 14:40:53 +0200 Subject: [PATCH] Added a text plot example --- implot-examples/examples/line_plots.rs | 19 ++++---- implot-examples/examples/text_plots.rs | 62 ++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 implot-examples/examples/text_plots.rs diff --git a/implot-examples/examples/line_plots.rs b/implot-examples/examples/line_plots.rs index da24833..1932280 100644 --- a/implot-examples/examples/line_plots.rs +++ b/implot-examples/examples/line_plots.rs @@ -1,5 +1,5 @@ -// This example demonstrates how line plots are to be used, along with some querying features -// that will be applicable to all kinds of plots. +//! This example demonstrates how line plots are to be used, along with some querying features +//! that will be applicable to all kinds of plots. use imgui::{im_str, CollapsingHeader, Condition, Ui, Window}; use implot::{ @@ -16,12 +16,14 @@ fn show_basic_plot(ui: &Ui) { )); let content_width = ui.window_content_region_width(); Plot::new("Simple line plot") - // The size call could also be omitted, though the defaults don't consider content + // 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(|| { // If this is called outside a plot build callback, the program will panic. - PlotLine::new("A line").plot(&vec![0.1, 0.9], &vec![0.1, 0.9]); + let x_positions = vec![0.1, 0.9]; + let y_positions = vec![0.1, 0.9]; + PlotLine::new("legend label").plot(&x_positions, &y_positions); }); } @@ -171,18 +173,19 @@ fn main() { let system = support::init(file!()); let mut showing_demo = false; system.main_loop(move |_, ui| { - Window::new(im_str!("Hello world")) + Window::new(im_str!("Line 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 some of the features of the library. \ - Have a look at the example source code to see how they are - implemented.\n\ + "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 \ (by enabling the 'Show demo' checkbox) for instructions \ on how to interact with ImPlot plots." )); + ui.checkbox(im_str!("Show demo"), &mut showing_demo); // Show individual examples in collapsed headers diff --git a/implot-examples/examples/text_plots.rs b/implot-examples/examples/text_plots.rs new file mode 100644 index 0000000..2a3f928 --- /dev/null +++ b/implot-examples/examples/text_plots.rs @@ -0,0 +1,62 @@ +//! This example demonstrates how the text plotting features are to be used. For more general +//! features of the libray, see the line_plots example. + +use imgui::{im_str, CollapsingHeader, Condition, Ui, Window}; +use implot::{Plot, PlotText}; + +mod support; + +fn show_basic_plot(ui: &Ui) { + ui.text(im_str!( + "This header just plots some text with as little code as possible." + )); + let content_width = ui.window_content_region_width(); + Plot::new("Simple text 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(|| { + // The text passed to "new" is what gets displayed. + let x_position: f64 = 0.5; + let y_position: f64 = 0.2; + let vertical: bool = false; + PlotText::new("horizontal displayed text").plot(x_position, y_position, vertical); + + // The text passed to "new" is what gets displayed. + let x_position: f64 = 0.2; + let y_position: f64 = 0.2; + let vertical: bool = true; + PlotText::new("vertical displayed text").plot(x_position, y_position, vertical); + }); +} + +fn main() { + let system = support::init(file!()); + let mut showing_demo = false; + system.main_loop(move |_, ui| { + Window::new(im_str!("Text 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 text 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 \ + (by enabling the 'Show demo' checkbox) for instructions \ + on how to interact with ImPlot plots." + )); + + ui.checkbox(im_str!("Show demo"), &mut showing_demo); + + // Show individual examples in collapsed headers + if CollapsingHeader::new(im_str!("Basic text plot")).build(&ui) { + show_basic_plot(&ui); + } + }); + + if showing_demo { + implot::show_demo_window(&mut showing_demo); + } + }); +}