1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 06:10:06 -04:00

More plot items (#471)

* Added plot items:

* Arrows, also called "Quiver plots" in matplotlib etc.
* Convex polygons
* Text
* Images

Other changes:

* Make HLine/VLine into PlotItems as well.
* Add a "fill" property to Line so that we can fill/shade the area between a line and a horizontal reference line.
* Add stems to Points, which are lines between the points and a horizontal reference line.
* Allow using .. when specifying ranges for values generated by explicit callback functions, as an alias for f64::NEG_INFINITY..f64::INFINITY
* Allow using ranges with exclusive end bounds for values generated by parametric callback functions to generate values where the first and last value are not the same.

* update changelog

* add legend background
This commit is contained in:
Sven Niederberger
2021-06-24 12:29:51 +02:00
committed by GitHub
parent e22c242d17
commit 147e7a47aa
5 changed files with 971 additions and 117 deletions

View File

@@ -1,5 +1,8 @@
use egui::*;
use plot::{Corner, Legend, Line, MarkerShape, Plot, Points, Value, Values};
use plot::{
Arrows, Corner, HLine, Legend, Line, MarkerShape, Plot, PlotImage, Points, Polygon, Text,
VLine, Value, Values,
};
use std::f64::consts::TAU;
#[derive(PartialEq)]
@@ -44,7 +47,7 @@ impl LineDemo {
ui.add(
egui::DragValue::new(circle_radius)
.speed(0.1)
.clamp_range(0.0..=f32::INFINITY)
.clamp_range(0.0..=f64::INFINITY)
.prefix("r: "),
);
ui.horizontal(|ui| {
@@ -90,7 +93,7 @@ impl LineDemo {
let time = self.time;
Line::new(Values::from_explicit_callback(
move |x| 0.5 * (2.0 * x).sin() * time.sin(),
f64::NEG_INFINITY..=f64::INFINITY,
..,
512,
))
.color(Color32::from_rgb(200, 100, 100))
@@ -187,7 +190,7 @@ impl Widget for &mut MarkerDemo {
ui.add(
egui::DragValue::new(&mut self.marker_radius)
.speed(0.1)
.clamp_range(0.0..=f32::INFINITY)
.clamp_range(0.0..=f64::INFINITY)
.prefix("marker radius: "),
);
ui.checkbox(&mut self.custom_marker_color, "custom marker color");
@@ -221,25 +224,13 @@ impl Default for LegendDemo {
impl LegendDemo {
fn line_with_slope(slope: f64) -> Line {
Line::new(Values::from_explicit_callback(
move |x| slope * x,
f64::NEG_INFINITY..=f64::INFINITY,
100,
))
Line::new(Values::from_explicit_callback(move |x| slope * x, .., 100))
}
fn sin() -> Line {
Line::new(Values::from_explicit_callback(
move |x| x.sin(),
f64::NEG_INFINITY..=f64::INFINITY,
100,
))
Line::new(Values::from_explicit_callback(move |x| x.sin(), .., 100))
}
fn cos() -> Line {
Line::new(Values::from_explicit_callback(
move |x| x.cos(),
f64::NEG_INFINITY..=f64::INFINITY,
100,
))
Line::new(Values::from_explicit_callback(move |x| x.cos(), .., 100))
}
}
@@ -259,6 +250,12 @@ impl Widget for &mut LegendDemo {
ui.selectable_value(&mut config.position, position, format!("{:?}", position));
});
});
ui.label("Background alpha:");
ui.add(
egui::DragValue::new(&mut config.background_alpha)
.speed(0.02)
.clamp_range(0.0..=1.0),
);
let legend_plot = Plot::new("Legend Demo")
.line(LegendDemo::line_with_slope(0.5).name("lines"))
.line(LegendDemo::line_with_slope(1.0).name("lines"))
@@ -270,11 +267,82 @@ impl Widget for &mut LegendDemo {
ui.add(legend_plot)
}
}
#[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),
[
ui.fonts().texture().width as f32 / 100.0,
ui.fonts().texture().height as f32 / 100.0,
],
);
let plot = Plot::new("Items Demo")
.hline(HLine::new(9.0).name("Lines horizontal"))
.hline(HLine::new(-9.0).name("Lines horizontal"))
.vline(VLine::new(9.0).name("Lines vertical"))
.vline(VLine::new(-9.0).name("Lines vertical"))
.line(line.name("Line with fill"))
.polygon(polygon.name("Convex polygon"))
.points(points.name("Points with stems"))
.text(Text::new(Value::new(-3.0, -3.0), "wow").name("Text"))
.text(Text::new(Value::new(-2.0, 2.5), "so graph").name("Text"))
.text(Text::new(Value::new(3.0, 3.0), "much color").name("Text"))
.text(Text::new(Value::new(2.5, -2.0), "such plot").name("Text"))
.image(image.name("Image"))
.arrows(arrows.name("Arrows"))
.legend(Legend::default().position(Corner::RightBottom))
.show_x(false)
.show_y(false)
.data_aspect(1.0);
ui.add(plot)
}
}
#[derive(PartialEq, Eq)]
enum Panel {
Lines,
Markers,
Legend,
Items,
}
impl Default for Panel {
@@ -288,6 +356,7 @@ pub struct PlotDemo {
line_demo: LineDemo,
marker_demo: MarkerDemo,
legend_demo: LegendDemo,
items_demo: ItemsDemo,
open_panel: Panel,
}
@@ -326,6 +395,7 @@ impl super::View for PlotDemo {
ui.selectable_value(&mut self.open_panel, Panel::Lines, "Lines");
ui.selectable_value(&mut self.open_panel, Panel::Markers, "Markers");
ui.selectable_value(&mut self.open_panel, Panel::Legend, "Legend");
ui.selectable_value(&mut self.open_panel, Panel::Items, "Items");
});
ui.separator();
@@ -339,6 +409,9 @@ impl super::View for PlotDemo {
Panel::Legend => {
ui.add(&mut self.legend_demo);
}
Panel::Items => {
ui.add(&mut self.items_demo);
}
}
}
}