egui/egui_demo_lib/src/apps/demo/plot_demo.rs

710 lines
22 KiB
Rust
Raw Normal View History

use std::f64::consts::TAU;
2021-02-14 20:39:04 +00:00
use egui::*;
use plot::{
Arrows, Bar, BarChart, BoxElem, BoxPlot, BoxSpread, Corner, HLine, Legend, Line, LineStyle,
MarkerShape, Plot, PlotImage, Points, Polygon, Text, VLine, Value, Values,
};
2021-02-14 20:39:04 +00:00
#[derive(PartialEq)]
struct LineDemo {
2021-02-14 20:39:04 +00:00
animate: bool,
time: f64,
circle_radius: f64,
2021-02-14 20:39:04 +00:00
circle_center: Pos2,
square: bool,
proportional: bool,
line_style: LineStyle,
2021-02-14 20:39:04 +00:00
}
impl Default for LineDemo {
2021-02-14 20:39:04 +00:00
fn default() -> Self {
Self {
2021-06-24 13:20:31 +00:00
animate: !cfg!(debug_assertions),
2021-02-14 20:39:04 +00:00
time: 0.0,
circle_radius: 1.5,
2021-02-14 20:39:04 +00:00
circle_center: Pos2::new(0.0, 0.0),
square: false,
proportional: true,
line_style: LineStyle::Solid,
2021-02-14 20:39:04 +00:00
}
}
}
impl LineDemo {
2021-02-14 20:39:04 +00:00
fn options_ui(&mut self, ui: &mut Ui) {
let Self {
animate,
time: _,
circle_radius,
circle_center,
square,
proportional,
line_style,
..
2021-02-14 20:39:04 +00:00
} = self;
ui.horizontal(|ui| {
ui.group(|ui| {
ui.vertical(|ui| {
ui.label("Circle:");
ui.add(
egui::DragValue::new(circle_radius)
.speed(0.1)
.clamp_range(0.0..=f64::INFINITY)
.prefix("r: "),
2021-02-14 20:39:04 +00:00
);
ui.horizontal(|ui| {
ui.add(
egui::DragValue::new(&mut circle_center.x)
.speed(0.1)
.prefix("x: "),
);
ui.add(
egui::DragValue::new(&mut circle_center.y)
.speed(1.0)
.prefix("y: "),
);
});
2021-02-14 20:39:04 +00:00
});
});
ui.vertical(|ui| {
ui.style_mut().wrap = Some(false);
ui.checkbox(animate, "animate");
2021-06-24 13:20:31 +00:00
ui.checkbox(square, "square view")
.on_hover_text("Always keep the viewport square.");
ui.checkbox(proportional, "Proportional data axes")
.on_hover_text("Tick are the same size on both axes.");
ComboBox::from_label("Line style")
.selected_text(line_style.to_string())
.show_ui(ui, |ui| {
for style in [
LineStyle::Solid,
LineStyle::dashed_dense(),
LineStyle::dashed_loose(),
LineStyle::dotted_dense(),
LineStyle::dotted_loose(),
]
.iter()
{
ui.selectable_value(line_style, *style, style.to_string());
}
});
});
2021-02-14 20:39:04 +00:00
});
}
fn circle(&self) -> Line {
2021-02-14 20:39:04 +00:00
let n = 512;
let circle = (0..=n).map(|i| {
let t = remap(i as f64, 0.0..=(n as f64), 0.0..=TAU);
let r = self.circle_radius;
2021-02-14 20:39:04 +00:00
Value::new(
r * t.cos() + self.circle_center.x as f64,
r * t.sin() + self.circle_center.y as f64,
)
});
Line::new(Values::from_values_iter(circle))
2021-02-14 20:39:04 +00:00
.color(Color32::from_rgb(100, 200, 100))
.style(self.line_style)
2021-02-14 20:39:04 +00:00
.name("circle")
}
fn sin(&self) -> Line {
let time = self.time;
Line::new(Values::from_explicit_callback(
move |x| 0.5 * (2.0 * x).sin() * time.sin(),
..,
512,
))
.color(Color32::from_rgb(200, 100, 100))
.style(self.line_style)
.name("wave")
2021-02-14 20:39:04 +00:00
}
fn thingy(&self) -> Line {
let time = self.time;
Line::new(Values::from_parametric_callback(
move |t| ((2.0 * t + time).sin(), (3.0 * t).sin()),
0.0..=TAU,
256,
))
.color(Color32::from_rgb(100, 150, 250))
.style(self.line_style)
.name("x = sin(2t), y = sin(3t)")
2021-02-14 20:39:04 +00:00
}
}
impl Widget for &mut LineDemo {
fn ui(self, ui: &mut Ui) -> Response {
2021-02-14 20:39:04 +00:00
self.options_ui(ui);
if self.animate {
ui.ctx().request_repaint();
self.time += ui.input().unstable_dt.at_most(1.0 / 30.0) as f64;
};
let mut plot = Plot::new("lines_demo").legend(Legend::default());
2021-02-14 20:39:04 +00:00
if self.square {
plot = plot.view_aspect(1.0);
}
if self.proportional {
plot = plot.data_aspect(1.0);
}
plot.show(ui, |plot_ui| {
plot_ui.line(self.circle());
plot_ui.line(self.sin());
plot_ui.line(self.thingy());
})
.response
}
}
#[derive(PartialEq)]
struct MarkerDemo {
fill_markers: bool,
marker_radius: f32,
2021-06-24 13:20:31 +00:00
automatic_colors: bool,
marker_color: Color32,
}
impl Default for MarkerDemo {
fn default() -> Self {
Self {
fill_markers: true,
marker_radius: 5.0,
2021-06-24 13:20:31 +00:00
automatic_colors: true,
marker_color: Color32::GREEN,
}
}
}
impl MarkerDemo {
fn markers(&self) -> Vec<Points> {
MarkerShape::all()
.enumerate()
.map(|(i, marker)| {
let y_offset = i as f32 * 0.5 + 1.0;
let mut points = Points::new(Values::from_values(vec![
Value::new(1.0, 0.0 + y_offset),
Value::new(2.0, 0.5 + y_offset),
Value::new(3.0, 0.0 + y_offset),
Value::new(4.0, 0.5 + y_offset),
Value::new(5.0, 0.0 + y_offset),
Value::new(6.0, 0.5 + y_offset),
]))
.name(format!("{:?}", marker))
.filled(self.fill_markers)
.radius(self.marker_radius)
.shape(marker);
2021-06-24 13:20:31 +00:00
if !self.automatic_colors {
points = points.color(self.marker_color);
}
points
})
.collect()
}
}
impl Widget for &mut MarkerDemo {
fn ui(self, ui: &mut Ui) -> Response {
ui.horizontal(|ui| {
2021-06-24 13:20:31 +00:00
ui.checkbox(&mut self.fill_markers, "Fill");
ui.add(
egui::DragValue::new(&mut self.marker_radius)
.speed(0.1)
.clamp_range(0.0..=f64::INFINITY)
2021-06-24 13:20:31 +00:00
.prefix("Radius: "),
);
2021-06-24 13:20:31 +00:00
ui.checkbox(&mut self.automatic_colors, "Automatic colors");
if !self.automatic_colors {
ui.color_edit_button_srgba(&mut self.marker_color);
}
});
let markers_plot = Plot::new("markers_demo")
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
.data_aspect(1.0)
.legend(Legend::default());
markers_plot
.show(ui, |plot_ui| {
for marker in self.markers() {
plot_ui.points(marker);
}
})
.response
}
}
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
#[derive(PartialEq)]
struct LegendDemo {
config: Legend,
}
impl Default for LegendDemo {
fn default() -> Self {
Self {
config: Legend::default(),
}
}
}
impl LegendDemo {
fn line_with_slope(slope: f64) -> Line {
Line::new(Values::from_explicit_callback(move |x| slope * x, .., 100))
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
}
fn sin() -> Line {
Line::new(Values::from_explicit_callback(move |x| x.sin(), .., 100))
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
}
fn cos() -> Line {
Line::new(Values::from_explicit_callback(move |x| x.cos(), .., 100))
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
}
}
impl Widget for &mut LegendDemo {
fn ui(self, ui: &mut Ui) -> Response {
let LegendDemo { config } = self;
2021-06-24 13:20:31 +00:00
egui::Grid::new("settings").show(ui, |ui| {
ui.label("Text style:");
ui.horizontal(|ui| {
TextStyle::all().for_each(|style| {
ui.selectable_value(&mut config.text_style, style, format!("{:?}", style));
});
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
});
2021-06-24 13:20:31 +00:00
ui.end_row();
ui.label("Position:");
ui.horizontal(|ui| {
Corner::all().for_each(|position| {
ui.selectable_value(&mut config.position, position, format!("{:?}", position));
});
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
});
2021-06-24 13:20:31 +00:00
ui.end_row();
ui.label("Opacity:");
ui.add(
egui::DragValue::new(&mut config.background_alpha)
.speed(0.02)
.clamp_range(0.0..=1.0),
);
ui.end_row();
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
});
2021-06-24 13:20:31 +00:00
let legend_plot = Plot::new("legend_demo").legend(*config).data_aspect(1.0);
legend_plot
.show(ui, |plot_ui| {
plot_ui.line(LegendDemo::line_with_slope(0.5).name("lines"));
plot_ui.line(LegendDemo::line_with_slope(1.0).name("lines"));
plot_ui.line(LegendDemo::line_with_slope(2.0).name("lines"));
plot_ui.line(LegendDemo::sin().name("sin(x)"));
plot_ui.line(LegendDemo::cos().name("cos(x)"));
})
.response
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
}
}
#[derive(PartialEq, Default)]
struct ItemsDemo {}
impl ItemsDemo {}
impl Widget for &mut ItemsDemo {
fn ui(self, ui: &mut Ui) -> Response {
let n = 100;
let mut sin_values: Vec<_> = (0..=n)
.map(|i| remap(i as f64, 0.0..=n as f64, -TAU..=TAU))
.map(|i| Value::new(i, i.sin()))
.collect();
let line = Line::new(Values::from_values(sin_values.split_off(n / 2))).fill(-1.5);
let polygon = Polygon::new(Values::from_parametric_callback(
|t| (4.0 * t.sin() + 2.0 * t.cos(), 4.0 * t.cos() + 2.0 * t.sin()),
0.0..TAU,
100,
));
let points = Points::new(Values::from_values(sin_values))
.stems(-1.5)
.radius(1.0);
let arrows = {
let pos_radius = 8.0;
let tip_radius = 7.0;
let arrow_origins = Values::from_parametric_callback(
|t| (pos_radius * t.sin(), pos_radius * t.cos()),
0.0..TAU,
36,
);
let arrow_tips = Values::from_parametric_callback(
|t| (tip_radius * t.sin(), tip_radius * t.cos()),
0.0..TAU,
36,
);
Arrows::new(arrow_origins, arrow_tips)
};
let image = PlotImage::new(
TextureId::Egui,
Value::new(0.0, 10.0),
[
2021-12-28 20:02:23 +00:00
ui.fonts().font_image().width as f32 / 100.0,
ui.fonts().font_image().height as f32 / 100.0,
],
);
let plot = Plot::new("items_demo")
.legend(Legend::default().position(Corner::RightBottom))
.show_x(false)
.show_y(false)
.data_aspect(1.0);
plot.show(ui, |plot_ui| {
plot_ui.hline(HLine::new(9.0).name("Lines horizontal"));
plot_ui.hline(HLine::new(-9.0).name("Lines horizontal"));
plot_ui.vline(VLine::new(9.0).name("Lines vertical"));
plot_ui.vline(VLine::new(-9.0).name("Lines vertical"));
plot_ui.line(line.name("Line with fill"));
plot_ui.polygon(polygon.name("Convex polygon"));
plot_ui.points(points.name("Points with stems"));
plot_ui.text(Text::new(Value::new(-3.0, -3.0), "wow").name("Text"));
plot_ui.text(Text::new(Value::new(-2.0, 2.5), "so graph").name("Text"));
plot_ui.text(Text::new(Value::new(3.0, 3.0), "much color").name("Text"));
plot_ui.text(Text::new(Value::new(2.5, -2.0), "such plot").name("Text"));
plot_ui.image(image.name("Image"));
plot_ui.arrows(arrows.name("Arrows"));
})
.response
}
}
#[derive(PartialEq)]
struct InteractionDemo {}
impl Default for InteractionDemo {
fn default() -> Self {
Self {}
}
}
impl Widget for &mut InteractionDemo {
fn ui(self, ui: &mut Ui) -> Response {
let plot = Plot::new("interaction_demo").height(300.0);
let InnerResponse {
response,
inner: (screen_pos, pointer_coordinate, pointer_coordinate_drag_delta, bounds, hovered),
} = plot.show(ui, |plot_ui| {
(
plot_ui.screen_from_plot(Value::new(0.0, 0.0)),
plot_ui.pointer_coordinate(),
plot_ui.pointer_coordinate_drag_delta(),
plot_ui.plot_bounds(),
plot_ui.plot_hovered(),
)
});
ui.label(format!(
"plot bounds: min: {:.02?}, max: {:.02?}",
bounds.min(),
bounds.max()
));
ui.label(format!(
"origin in screen coordinates: x: {:.02}, y: {:.02}",
screen_pos.x, screen_pos.y
));
ui.label(format!("plot hovered: {}", hovered));
let coordinate_text = if let Some(coordinate) = pointer_coordinate {
format!("x: {:.02}, y: {:.02}", coordinate.x, coordinate.y)
} else {
"None".to_string()
};
ui.label(format!("pointer coordinate: {}", coordinate_text));
let coordinate_text = format!(
"x: {:.02}, y: {:.02}",
pointer_coordinate_drag_delta.x, pointer_coordinate_drag_delta.y
);
ui.label(format!(
"pointer coordinate drag delta: {}",
coordinate_text
));
response
}
}
#[derive(PartialEq, Eq)]
enum Chart {
GaussBars,
StackedBars,
BoxPlot,
}
impl Default for Chart {
fn default() -> Self {
Self::GaussBars
}
}
#[derive(PartialEq)]
struct ChartsDemo {
chart: Chart,
vertical: bool,
}
impl Default for ChartsDemo {
fn default() -> Self {
Self {
vertical: true,
chart: Chart::default(),
}
}
}
impl ChartsDemo {
fn bar_gauss(&self, ui: &mut Ui) -> Response {
let mut chart = BarChart::new(
(-395..=395)
.step_by(10)
.map(|x| x as f64 * 0.01)
.map(|x| {
(
x,
(-x * x / 2.0).exp() / (2.0 * std::f64::consts::PI).sqrt(),
)
})
// The 10 factor here is purely for a nice 1:1 aspect ratio
.map(|(x, f)| Bar::new(x, f * 10.0).width(0.095))
.collect(),
)
.color(Color32::LIGHT_BLUE)
.name("Normal Distribution");
if !self.vertical {
chart = chart.horizontal();
}
Plot::new("Normal Distribution Demo")
.legend(Legend::default())
.data_aspect(1.0)
.show(ui, |plot_ui| plot_ui.bar_chart(chart))
.response
}
fn bar_stacked(&self, ui: &mut Ui) -> Response {
let mut chart1 = BarChart::new(vec![
Bar::new(0.5, 1.0).name("Day 1"),
Bar::new(1.5, 3.0).name("Day 2"),
Bar::new(2.5, 1.0).name("Day 3"),
Bar::new(3.5, 2.0).name("Day 4"),
Bar::new(4.5, 4.0).name("Day 5"),
])
.width(0.7)
.name("Set 1");
let mut chart2 = BarChart::new(vec![
Bar::new(0.5, 1.0),
Bar::new(1.5, 1.5),
Bar::new(2.5, 0.1),
Bar::new(3.5, 0.7),
Bar::new(4.5, 0.8),
])
.width(0.7)
.name("Set 2")
.stack_on(&[&chart1]);
let mut chart3 = BarChart::new(vec![
Bar::new(0.5, -0.5),
Bar::new(1.5, 1.0),
Bar::new(2.5, 0.5),
Bar::new(3.5, -1.0),
Bar::new(4.5, 0.3),
])
.width(0.7)
.name("Set 3")
.stack_on(&[&chart1, &chart2]);
let mut chart4 = BarChart::new(vec![
Bar::new(0.5, 0.5),
Bar::new(1.5, 1.0),
Bar::new(2.5, 0.5),
Bar::new(3.5, -0.5),
Bar::new(4.5, -0.5),
])
.width(0.7)
.name("Set 4")
.stack_on(&[&chart1, &chart2, &chart3]);
if !self.vertical {
chart1 = chart1.horizontal();
chart2 = chart2.horizontal();
chart3 = chart3.horizontal();
chart4 = chart4.horizontal();
}
Plot::new("Stacked Bar Chart Demo")
.legend(Legend::default())
.data_aspect(1.0)
.show(ui, |plot_ui| {
plot_ui.bar_chart(chart1);
plot_ui.bar_chart(chart2);
plot_ui.bar_chart(chart3);
plot_ui.bar_chart(chart4);
})
.response
}
fn box_plot(&self, ui: &mut Ui) -> Response {
let yellow = Color32::from_rgb(248, 252, 168);
let mut box1 = BoxPlot::new(vec![
BoxElem::new(0.5, BoxSpread::new(1.5, 2.2, 2.5, 2.6, 3.1)).name("Day 1"),
BoxElem::new(2.5, BoxSpread::new(0.4, 1.0, 1.1, 1.4, 2.1)).name("Day 2"),
BoxElem::new(4.5, BoxSpread::new(1.7, 2.0, 2.2, 2.5, 2.9)).name("Day 3"),
])
.name("Experiment A");
let mut box2 = BoxPlot::new(vec![
BoxElem::new(1.0, BoxSpread::new(0.2, 0.5, 1.0, 2.0, 2.7)).name("Day 1"),
BoxElem::new(3.0, BoxSpread::new(1.5, 1.7, 2.1, 2.9, 3.3))
.name("Day 2: interesting")
.stroke(Stroke::new(1.5, yellow))
.fill(yellow.linear_multiply(0.2)),
BoxElem::new(5.0, BoxSpread::new(1.3, 2.0, 2.3, 2.9, 4.0)).name("Day 3"),
])
.name("Experiment B");
let mut box3 = BoxPlot::new(vec![
BoxElem::new(1.5, BoxSpread::new(2.1, 2.2, 2.6, 2.8, 3.0)).name("Day 1"),
BoxElem::new(3.5, BoxSpread::new(1.3, 1.5, 1.9, 2.2, 2.4)).name("Day 2"),
BoxElem::new(5.5, BoxSpread::new(0.2, 0.4, 1.0, 1.3, 1.5)).name("Day 3"),
])
.name("Experiment C");
if !self.vertical {
box1 = box1.horizontal();
box2 = box2.horizontal();
box3 = box3.horizontal();
}
Plot::new("Box Plot Demo")
.legend(Legend::default())
.show(ui, |plot_ui| {
plot_ui.box_plot(box1);
plot_ui.box_plot(box2);
plot_ui.box_plot(box3);
})
.response
}
}
impl Widget for &mut ChartsDemo {
fn ui(self, ui: &mut Ui) -> Response {
ui.label("Type:");
ui.horizontal(|ui| {
ui.selectable_value(&mut self.chart, Chart::GaussBars, "Histogram");
ui.selectable_value(&mut self.chart, Chart::StackedBars, "Stacked Bar Chart");
ui.selectable_value(&mut self.chart, Chart::BoxPlot, "Box Plot");
});
ui.label("Orientation:");
ui.horizontal(|ui| {
ui.selectable_value(&mut self.vertical, true, "Vertical");
ui.selectable_value(&mut self.vertical, false, "Horizontal");
});
match self.chart {
Chart::GaussBars => self.bar_gauss(ui),
Chart::StackedBars => self.bar_stacked(ui),
Chart::BoxPlot => self.box_plot(ui),
}
}
}
#[derive(PartialEq, Eq)]
enum Panel {
Lines,
Markers,
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
Legend,
Charts,
Items,
Interaction,
}
impl Default for Panel {
fn default() -> Self {
Self::Charts
}
}
#[derive(PartialEq, Default)]
pub struct PlotDemo {
line_demo: LineDemo,
marker_demo: MarkerDemo,
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
legend_demo: LegendDemo,
charts_demo: ChartsDemo,
items_demo: ItemsDemo,
interaction_demo: InteractionDemo,
open_panel: Panel,
}
impl super::Demo for PlotDemo {
fn name(&self) -> &'static str {
"🗠 Plot"
}
fn show(&mut self, ctx: &Context, open: &mut bool) {
2021-10-07 19:39:04 +00:00
use super::View as _;
Window::new(self.name())
.open(open)
.default_size(vec2(400.0, 400.0))
.vscroll(false)
.show(ctx, |ui| self.ui(ui));
}
}
impl super::View for PlotDemo {
fn ui(&mut self, ui: &mut Ui) {
2021-06-24 13:20:31 +00:00
ui.horizontal(|ui| {
egui::reset_button(ui, self);
2021-06-24 13:20:31 +00:00
ui.collapsing("Instructions", |ui| {
ui.label("Pan by dragging, or scroll (+ shift = horizontal).");
if cfg!(target_arch = "wasm32") {
ui.label("Zoom with ctrl / ⌘ + pointer wheel, or with pinch gesture.");
2021-06-24 13:20:31 +00:00
} else if cfg!(target_os = "macos") {
ui.label("Zoom with ctrl / ⌘ + scroll.");
} else {
ui.label("Zoom with ctrl + scroll.");
}
ui.label("Reset view with double-click.");
ui.add(crate::__egui_github_link_file!());
});
});
ui.separator();
ui.horizontal(|ui| {
ui.selectable_value(&mut self.open_panel, Panel::Lines, "Lines");
ui.selectable_value(&mut self.open_panel, Panel::Markers, "Markers");
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
ui.selectable_value(&mut self.open_panel, Panel::Legend, "Legend");
ui.selectable_value(&mut self.open_panel, Panel::Charts, "Charts");
ui.selectable_value(&mut self.open_panel, Panel::Items, "Items");
ui.selectable_value(&mut self.open_panel, Panel::Interaction, "Interaction");
});
ui.separator();
match self.open_panel {
Panel::Lines => {
ui.add(&mut self.line_demo);
}
Panel::Markers => {
ui.add(&mut self.marker_demo);
}
Plot: Legend improvements (#410) * initial work on markers * clippy fix * simplify marker * use option for color * prepare for more demo plots * more improvements for markers * some small adjustments * better highlighting * don't draw transparent lines * use transparent color instead of option * don't brighten curves when highlighting * Initial changes to lengend: * Font options * Position options * Internal cleanup * draw legend on top of curves * update changelog * fix legend checkboxes * simplify legend * remove unnecessary derives * remove config from legend entries * avoid allocations and use line_segment * compare against transparent color * create new Points primitive * fix doctest * some cleanup and fix hover * common interface for lines and points * clippy fixes * reduce visibilities * update legend * clippy fix * change instances of "curve" to "item" * change visibility * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/mod.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui_demo_lib/src/apps/demo/plot_demo.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add legend to demo * fix test * move highlighted items to front * dynamic plot size * add legend again * remove height * clippy fix * update changelog * minor changes * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * Update egui/src/widgets/plot/legend.rs Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com> * changes based on review * add functions to mutate legend config * use horizontal_align Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
2021-06-07 20:36:13 +00:00
Panel::Legend => {
ui.add(&mut self.legend_demo);
}
Panel::Charts => {
ui.add(&mut self.charts_demo);
}
Panel::Items => {
ui.add(&mut self.items_demo);
}
Panel::Interaction => {
ui.add(&mut self.interaction_demo);
}
}
2021-02-14 20:39:04 +00:00
}
}