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
File diff suppressed because it is too large Load Diff
+28 -6
View File
@@ -33,6 +33,7 @@ impl Corner {
#[derive(Clone, Copy, PartialEq)]
pub struct Legend {
pub text_style: TextStyle,
pub background_alpha: f32,
pub position: Corner,
}
@@ -40,17 +41,26 @@ impl Default for Legend {
fn default() -> Self {
Self {
text_style: TextStyle::Body,
background_alpha: 0.75,
position: Corner::RightTop,
}
}
}
impl Legend {
/// Which text style to use for the legend. Default: `TextStyle::Body`.
pub fn text_style(mut self, style: TextStyle) -> Self {
self.text_style = style;
self
}
/// The alpha of the legend background. Default: `0.75`.
pub fn background_alpha(mut self, alpha: f32) -> Self {
self.background_alpha = alpha;
self
}
/// In which corner to place the legend. Default: `Corner::RightTop`.
pub fn position(mut self, corner: Corner) -> Self {
self.position = corner;
self
@@ -220,17 +230,29 @@ impl Widget for &mut LegendWidget {
Corner::RightTop | Corner::RightBottom => Align::RIGHT,
};
let layout = Layout::from_main_dir_and_cross_align(main_dir, cross_align);
let legend_pad = 2.0;
let legend_pad = 4.0;
let legend_rect = rect.shrink(legend_pad);
let mut legend_ui = ui.child_ui(legend_rect, layout);
legend_ui
.scope(|ui| {
ui.style_mut().body_text_style = config.text_style;
entries
.iter_mut()
.map(|(name, entry)| entry.ui(ui, name.clone()))
.reduce(|r1, r2| r1.union(r2))
.unwrap()
let background_frame = Frame {
margin: vec2(8.0, 4.0),
corner_radius: ui.style().visuals.window_corner_radius,
shadow: epaint::Shadow::default(),
fill: ui.style().visuals.extreme_bg_color,
stroke: ui.style().visuals.window_stroke(),
}
.multiply_with_opacity(config.background_alpha);
background_frame
.show(ui, |ui| {
entries
.iter_mut()
.map(|(name, entry)| entry.ui(ui, name.clone()))
.reduce(|r1, r2| r1.union(r2))
.unwrap()
})
.inner
})
.inner
}
+61 -48
View File
@@ -7,8 +7,8 @@ mod transform;
use std::collections::HashSet;
use items::PlotItem;
pub use items::{Arrows, Line, MarkerShape, PlotImage, Points, Polygon, Text, Value, Values};
pub use items::{HLine, VLine};
pub use items::{Line, MarkerShape, Points, Value, Values};
use legend::LegendWidget;
pub use legend::{Corner, Legend};
use transform::{Bounds, ScreenTransform};
@@ -51,8 +51,6 @@ pub struct Plot {
next_auto_color_idx: usize,
items: Vec<Box<dyn PlotItem>>,
hlines: Vec<HLine>,
vlines: Vec<VLine>,
center_x_axis: bool,
center_y_axis: bool,
@@ -80,8 +78,6 @@ impl Plot {
next_auto_color_idx: 0,
items: Default::default(),
hlines: Default::default(),
vlines: Default::default(),
center_x_axis: false,
center_y_axis: false,
@@ -111,7 +107,6 @@ impl Plot {
}
/// Add a data lines.
/// You can add multiple lines.
pub fn line(mut self, mut line: Line) -> Self {
if line.series.is_empty() {
return self;
@@ -122,12 +117,34 @@ impl Plot {
line.stroke.color = self.auto_color();
}
self.items.push(Box::new(line));
self
}
/// Add a polygon. The polygon has to be convex.
pub fn polygon(mut self, mut polygon: Polygon) -> Self {
if polygon.series.is_empty() {
return self;
};
// Give the stroke an automatic color if no color has been assigned.
if polygon.stroke.color == Color32::TRANSPARENT {
polygon.stroke.color = self.auto_color();
}
self.items.push(Box::new(polygon));
self
}
/// Add a text.
pub fn text(mut self, text: Text) -> Self {
if text.text.is_empty() {
return self;
};
self.items.push(Box::new(text));
self
}
/// Add data points.
/// You can add multiple sets of points.
pub fn points(mut self, mut points: Points) -> Self {
if points.series.is_empty() {
return self;
@@ -138,7 +155,26 @@ impl Plot {
points.color = self.auto_color();
}
self.items.push(Box::new(points));
self
}
/// Add arrows.
pub fn arrows(mut self, mut arrows: Arrows) -> Self {
if arrows.origins.is_empty() || arrows.tips.is_empty() {
return self;
};
// Give the arrows an automatic color if no color has been assigned.
if arrows.color == Color32::TRANSPARENT {
arrows.color = self.auto_color();
}
self.items.push(Box::new(arrows));
self
}
/// Add an image.
pub fn image(mut self, image: PlotImage) -> Self {
self.items.push(Box::new(image));
self
}
@@ -149,7 +185,7 @@ impl Plot {
if hline.stroke.color == Color32::TRANSPARENT {
hline.stroke.color = self.auto_color();
}
self.hlines.push(hline);
self.items.push(Box::new(hline));
self
}
@@ -160,7 +196,7 @@ impl Plot {
if vline.stroke.color == Color32::TRANSPARENT {
vline.stroke.color = self.auto_color();
}
self.vlines.push(vline);
self.items.push(Box::new(vline));
self
}
@@ -284,8 +320,6 @@ impl Widget for Plot {
name,
next_auto_color_idx: _,
mut items,
hlines,
vlines,
center_x_axis,
center_y_axis,
allow_zoom,
@@ -381,11 +415,9 @@ impl Widget for Plot {
// Set bounds automatically based on content.
if auto_bounds || !bounds.is_valid() {
bounds = min_auto_bounds;
hlines.iter().for_each(|line| bounds.extend_with_y(line.y));
vlines.iter().for_each(|line| bounds.extend_with_x(line.x));
items
.iter()
.for_each(|item| bounds.merge(&item.series().get_bounds()));
.for_each(|item| bounds.merge(&item.get_bounds()));
bounds.add_relative_margin(margin_fraction);
}
// Make sure they are not empty.
@@ -436,17 +468,14 @@ impl Widget for Plot {
}
// Initialize values from functions.
items.iter_mut().for_each(|item| {
item.series_mut()
.generate_points(transform.bounds().range_x())
});
items
.iter_mut()
.for_each(|item| item.initialize(transform.bounds().range_x()));
let bounds = *transform.bounds();
let prepared = Prepared {
items,
hlines,
vlines,
show_x,
show_y,
transform,
@@ -479,8 +508,6 @@ impl Widget for Plot {
struct Prepared {
items: Vec<Box<dyn PlotItem>>,
hlines: Vec<HLine>,
vlines: Vec<VLine>,
show_x: bool,
show_y: bool,
transform: ScreenTransform,
@@ -496,26 +523,10 @@ impl Prepared {
let transform = &self.transform;
for &hline in &self.hlines {
let HLine { y, stroke } = hline;
let points = [
transform.position_from_value(&Value::new(transform.bounds().min[0], y)),
transform.position_from_value(&Value::new(transform.bounds().max[0], y)),
];
shapes.push(Shape::line_segment(points, stroke));
}
for &vline in &self.vlines {
let VLine { x, stroke } = vline;
let points = [
transform.position_from_value(&Value::new(x, transform.bounds().min[1])),
transform.position_from_value(&Value::new(x, transform.bounds().max[1])),
];
shapes.push(Shape::line_segment(points, stroke));
}
let mut plot_ui = ui.child_ui(*transform.frame(), Layout::default());
plot_ui.set_clip_rect(*transform.frame());
for item in &self.items {
item.get_shapes(transform, &mut shapes);
item.get_shapes(&mut plot_ui, transform, &mut shapes);
}
if let Some(pointer) = response.hover_pos() {
@@ -632,13 +643,15 @@ impl Prepared {
let mut closest_item = None;
let mut closest_dist_sq = interact_radius.powi(2);
for item in items {
for value in &item.series().values {
let pos = transform.position_from_value(value);
let dist_sq = pointer.distance_sq(pos);
if dist_sq < closest_dist_sq {
closest_dist_sq = dist_sq;
closest_value = Some(value);
closest_item = Some(item.name());
if let Some(values) = item.values() {
for value in &values.values {
let pos = transform.position_from_value(value);
let dist_sq = pointer.distance_sq(pos);
if dist_sq < closest_dist_sq {
closest_dist_sq = dist_sq;
closest_value = Some(value);
closest_item = Some(item.name());
}
}
}
}