2020-09-13 13:06:59 +00:00
|
|
|
//! This example demonstrates how scatter plots are to be used. For more general
|
|
|
|
//! features of the libray, see the line_plots example.
|
|
|
|
|
|
|
|
use imgui::{im_str, CollapsingHeader, Condition, Ui, Window};
|
2020-10-04 12:43:18 +00:00
|
|
|
use implot::{
|
2020-10-11 14:25:48 +00:00
|
|
|
push_style_var_f32, push_style_var_i32, Context, Marker, Plot, PlotScatter, PlotUi, StyleVar,
|
2020-10-04 12:43:18 +00:00
|
|
|
};
|
2020-09-13 13:06:59 +00:00
|
|
|
|
|
|
|
mod support;
|
|
|
|
|
2020-10-11 14:25:48 +00:00
|
|
|
fn show_basic_plot(ui: &Ui, plot_ui: &PlotUi) {
|
2020-09-13 13:06:59 +00:00
|
|
|
ui.text(im_str!(
|
2020-09-13 14:09:21 +00:00
|
|
|
"This header just draws a scatter plot with as little code as possible."
|
2020-09-13 13:06:59 +00:00
|
|
|
));
|
|
|
|
let content_width = ui.window_content_region_width();
|
|
|
|
Plot::new("Simple scatter 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)
|
2020-10-11 14:25:48 +00:00
|
|
|
.build(plot_ui, || {
|
2020-09-13 13:06:59 +00:00
|
|
|
// If this is called outside a plot build callback, the program will panic.
|
|
|
|
let x_positions = vec![0.1, 0.2, 0.1, 0.5, 0.9];
|
|
|
|
let y_positions = vec![0.1, 0.1, 0.3, 0.3, 0.9];
|
|
|
|
PlotScatter::new("legend label").plot(&x_positions, &y_positions);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-11 14:25:48 +00:00
|
|
|
fn show_custom_markers_plot(ui: &Ui, plot_ui: &PlotUi) {
|
2020-09-13 13:06:59 +00:00
|
|
|
ui.text(im_str!(
|
2020-09-13 14:11:57 +00:00
|
|
|
"This header shows how markers can be used in scatter plots."
|
2020-09-13 13:06:59 +00:00
|
|
|
));
|
|
|
|
let content_width = ui.window_content_region_width();
|
2020-10-04 12:43:18 +00:00
|
|
|
Plot::new("Multi-marker scatter plot")
|
2020-09-13 13:06:59 +00:00
|
|
|
// 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)
|
2020-10-11 14:25:48 +00:00
|
|
|
.build(plot_ui, || {
|
2020-09-13 13:06:59 +00:00
|
|
|
// Change to cross marker for one scatter plot call
|
|
|
|
let x_positions = vec![0.1, 0.2, 0.1, 0.5, 0.9];
|
|
|
|
let y_positions = vec![0.1, 0.1, 0.3, 0.3, 0.9];
|
2020-10-04 12:43:18 +00:00
|
|
|
let markerchoice = push_style_var_i32(&StyleVar::Marker, Marker::Cross as i32);
|
2020-09-13 13:06:59 +00:00
|
|
|
PlotScatter::new("legend label 1").plot(&x_positions, &y_positions);
|
|
|
|
markerchoice.pop();
|
|
|
|
|
|
|
|
// One can combine things like marker size and markor choice
|
|
|
|
let x_positions = vec![0.4, 0.1];
|
|
|
|
let y_positions = vec![0.5, 0.3];
|
2020-10-04 12:43:18 +00:00
|
|
|
let marker_choice = push_style_var_i32(&StyleVar::Marker, Marker::Diamond as i32);
|
2020-09-13 13:06:59 +00:00
|
|
|
let marker_size = push_style_var_f32(&StyleVar::MarkerSize, 12.0);
|
|
|
|
PlotScatter::new("legend label 2").plot(&x_positions, &y_positions);
|
|
|
|
|
2020-09-13 14:11:57 +00:00
|
|
|
// TODO(4bb4) check if these have to be in reverse push order. Does not
|
|
|
|
// seem to be the case.
|
2020-09-13 13:06:59 +00:00
|
|
|
marker_size.pop();
|
|
|
|
marker_choice.pop();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let system = support::init(file!());
|
|
|
|
let mut showing_demo = false;
|
2020-10-11 14:25:48 +00:00
|
|
|
let plotcontext = Context::create();
|
2020-09-13 13:06:59 +00:00
|
|
|
system.main_loop(move |_, ui| {
|
2020-10-11 14:25:48 +00:00
|
|
|
// The context is moved into the closure after creation so plot_ui is valid.
|
|
|
|
let plot_ui = plotcontext.get_plot_ui();
|
|
|
|
|
2020-09-13 14:11:57 +00:00
|
|
|
Window::new(im_str!("Scatter plots example"))
|
2020-09-13 13:06:59 +00:00
|
|
|
.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 scatter 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 scatter plot")).build(&ui) {
|
2020-10-11 14:25:48 +00:00
|
|
|
show_basic_plot(&ui, &plot_ui);
|
2020-09-13 13:06:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if CollapsingHeader::new(im_str!("Custom markers")).build(&ui) {
|
2020-10-11 14:25:48 +00:00
|
|
|
show_custom_markers_plot(&ui, &plot_ui);
|
2020-09-13 13:06:59 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if showing_demo {
|
|
|
|
implot::show_demo_window(&mut showing_demo);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|