Skip to content

Commit 3259adb

Browse files
authored
Internal code cleanup: values (#211)
1 parent b495883 commit 3259adb

21 files changed

Lines changed: 223 additions & 237 deletions

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ build_wasm:
8282
cargo build -p egui_plot -p demo --all-features --lib --tests --bins --examples --target $(TARGET_WASM) $(CARGO_OPTS) $(BUILD_PROFILE)
8383

8484
trunk:
85-
trunk build $(TRUNK_OPTS)
85+
cd demo && trunk build $(TRUNK_OPTS)
8686

8787
# -------------------------------- test ----------------------------------------
8888

egui_plot/src/aesthetics.rs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
use egui::Shape;
2+
use egui::Stroke;
3+
use egui::epaint::ColorMode;
4+
use egui::epaint::PathStroke;
5+
use emath::Pos2;
6+
use emath::Rect;
7+
use emath::pos2;
8+
9+
/// Solid, dotted, dashed, etc.
10+
#[derive(Debug, PartialEq, Clone, Copy)]
11+
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
12+
pub enum LineStyle {
13+
Solid,
14+
Dotted { spacing: f32 },
15+
Dashed { length: f32 },
16+
}
17+
18+
impl LineStyle {
19+
pub fn dashed_loose() -> Self {
20+
Self::Dashed { length: 10.0 }
21+
}
22+
23+
pub fn dashed_dense() -> Self {
24+
Self::Dashed { length: 5.0 }
25+
}
26+
27+
pub fn dotted_loose() -> Self {
28+
Self::Dotted { spacing: 10.0 }
29+
}
30+
31+
pub fn dotted_dense() -> Self {
32+
Self::Dotted { spacing: 5.0 }
33+
}
34+
35+
pub(crate) fn style_line(&self, line: Vec<Pos2>, mut stroke: PathStroke, highlight: bool, shapes: &mut Vec<Shape>) {
36+
let path_stroke_color = match &stroke.color {
37+
ColorMode::Solid(c) => *c,
38+
ColorMode::UV(callback) => callback(Rect::from_min_max(pos2(0., 0.), pos2(0., 0.)), pos2(0., 0.)),
39+
};
40+
match line.len() {
41+
0 => {}
42+
1 => {
43+
let mut radius = stroke.width / 2.0;
44+
if highlight {
45+
radius *= 2f32.sqrt();
46+
}
47+
shapes.push(Shape::circle_filled(line[0], radius, path_stroke_color));
48+
}
49+
_ => {
50+
match self {
51+
Self::Solid => {
52+
if highlight {
53+
stroke.width *= 2.0;
54+
}
55+
shapes.push(Shape::line(line, stroke));
56+
}
57+
Self::Dotted { spacing } => {
58+
// Take the stroke width for the radius even though it's not "correct",
59+
// otherwise the dots would become too small.
60+
let mut radius = stroke.width;
61+
if highlight {
62+
radius *= 2f32.sqrt();
63+
}
64+
shapes.extend(Shape::dotted_line(&line, path_stroke_color, *spacing, radius));
65+
}
66+
Self::Dashed { length } => {
67+
if highlight {
68+
stroke.width *= 2.0;
69+
}
70+
let golden_ratio = (5.0_f32.sqrt() - 1.0) / 2.0; // 0.61803398875
71+
shapes.extend(Shape::dashed_line(
72+
&line,
73+
Stroke::new(stroke.width, path_stroke_color),
74+
*length,
75+
length * golden_ratio,
76+
));
77+
}
78+
}
79+
}
80+
}
81+
}
82+
}
83+
84+
impl std::fmt::Display for LineStyle {
85+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86+
match self {
87+
Self::Solid => write!(f, "Solid"),
88+
Self::Dotted { spacing } => write!(f, "Dotted({spacing} px)"),
89+
Self::Dashed { length } => write!(f, "Dashed({length} px)"),
90+
}
91+
}
92+
}
93+
94+
/// Determines whether a plot element is vertically or horizontally oriented.
95+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
96+
pub enum Orientation {
97+
Horizontal,
98+
Vertical,
99+
}
100+
101+
impl Default for Orientation {
102+
fn default() -> Self {
103+
Self::Vertical
104+
}
105+
}
106+
107+
/// Circle, Diamond, Square, Cross, …
108+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
109+
pub enum MarkerShape {
110+
Circle,
111+
Diamond,
112+
Square,
113+
Cross,
114+
Plus,
115+
Up,
116+
Down,
117+
Left,
118+
Right,
119+
Asterisk,
120+
}
121+
122+
impl MarkerShape {
123+
/// Get a vector containing all marker shapes.
124+
pub fn all() -> impl ExactSizeIterator<Item = Self> {
125+
[
126+
Self::Circle,
127+
Self::Diamond,
128+
Self::Square,
129+
Self::Cross,
130+
Self::Plus,
131+
Self::Up,
132+
Self::Down,
133+
Self::Left,
134+
Self::Right,
135+
Self::Asterisk,
136+
]
137+
.iter()
138+
.copied()
139+
}
140+
}
Lines changed: 8 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
use std::ops::Bound;
1+
use std::collections::Bound;
2+
use std::iter::FromIterator;
23
use std::ops::RangeBounds;
34
use std::ops::RangeInclusive;
45

5-
use egui::Pos2;
6-
use egui::Rect;
7-
use egui::Shape;
8-
use egui::Stroke;
9-
use egui::Vec2;
10-
use egui::epaint::ColorMode;
11-
use egui::epaint::PathStroke;
12-
use egui::lerp;
13-
use egui::pos2;
6+
use emath::Pos2;
7+
use emath::Vec2;
8+
use emath::lerp;
149

1510
use crate::bounds::PlotBounds;
1611

@@ -55,110 +50,6 @@ impl PlotPoint {
5550
}
5651
}
5752

58-
// ----------------------------------------------------------------------------
59-
60-
/// Solid, dotted, dashed, etc.
61-
#[derive(Debug, PartialEq, Clone, Copy)]
62-
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
63-
pub enum LineStyle {
64-
Solid,
65-
Dotted { spacing: f32 },
66-
Dashed { length: f32 },
67-
}
68-
69-
impl LineStyle {
70-
pub fn dashed_loose() -> Self {
71-
Self::Dashed { length: 10.0 }
72-
}
73-
74-
pub fn dashed_dense() -> Self {
75-
Self::Dashed { length: 5.0 }
76-
}
77-
78-
pub fn dotted_loose() -> Self {
79-
Self::Dotted { spacing: 10.0 }
80-
}
81-
82-
pub fn dotted_dense() -> Self {
83-
Self::Dotted { spacing: 5.0 }
84-
}
85-
86-
pub(super) fn style_line(&self, line: Vec<Pos2>, mut stroke: PathStroke, highlight: bool, shapes: &mut Vec<Shape>) {
87-
let path_stroke_color = match &stroke.color {
88-
ColorMode::Solid(c) => *c,
89-
ColorMode::UV(callback) => callback(Rect::from_min_max(pos2(0., 0.), pos2(0., 0.)), pos2(0., 0.)),
90-
};
91-
match line.len() {
92-
0 => {}
93-
1 => {
94-
let mut radius = stroke.width / 2.0;
95-
if highlight {
96-
radius *= 2f32.sqrt();
97-
}
98-
shapes.push(Shape::circle_filled(line[0], radius, path_stroke_color));
99-
}
100-
_ => {
101-
match self {
102-
Self::Solid => {
103-
if highlight {
104-
stroke.width *= 2.0;
105-
}
106-
shapes.push(Shape::line(line, stroke));
107-
}
108-
Self::Dotted { spacing } => {
109-
// Take the stroke width for the radius even though it's not "correct",
110-
// otherwise the dots would become too small.
111-
let mut radius = stroke.width;
112-
if highlight {
113-
radius *= 2f32.sqrt();
114-
}
115-
shapes.extend(Shape::dotted_line(&line, path_stroke_color, *spacing, radius));
116-
}
117-
Self::Dashed { length } => {
118-
if highlight {
119-
stroke.width *= 2.0;
120-
}
121-
let golden_ratio = (5.0_f32.sqrt() - 1.0) / 2.0; // 0.61803398875
122-
shapes.extend(Shape::dashed_line(
123-
&line,
124-
Stroke::new(stroke.width, path_stroke_color),
125-
*length,
126-
length * golden_ratio,
127-
));
128-
}
129-
}
130-
}
131-
}
132-
}
133-
}
134-
135-
impl std::fmt::Display for LineStyle {
136-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
137-
match self {
138-
Self::Solid => write!(f, "Solid"),
139-
Self::Dotted { spacing } => write!(f, "Dotted({spacing} px)"),
140-
Self::Dashed { length } => write!(f, "Dashed({length} px)"),
141-
}
142-
}
143-
}
144-
145-
// ----------------------------------------------------------------------------
146-
147-
/// Determines whether a plot element is vertically or horizontally oriented.
148-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
149-
pub enum Orientation {
150-
Horizontal,
151-
Vertical,
152-
}
153-
154-
impl Default for Orientation {
155-
fn default() -> Self {
156-
Self::Vertical
157-
}
158-
}
159-
160-
// ----------------------------------------------------------------------------
161-
16253
/// Represents many [`PlotPoint`]s.
16354
///
16455
/// These can be an owned `Vec`
@@ -286,7 +177,7 @@ impl<'a> PlotPoints<'a> {
286177

287178
/// Returns true if there are no data points available and there is no
288179
/// function to generate any.
289-
pub(crate) fn is_empty(&self) -> bool {
180+
pub fn is_empty(&self) -> bool {
290181
match self {
291182
Self::Owned(points) => points.is_empty(),
292183
Self::Generator(_) => false,
@@ -296,7 +187,7 @@ impl<'a> PlotPoints<'a> {
296187

297188
/// If initialized with a generator function, this will generate `n` evenly
298189
/// spaced points in the given range.
299-
pub(super) fn generate_points(&mut self, x_range: RangeInclusive<f64>) {
190+
pub fn generate_points(&mut self, x_range: RangeInclusive<f64>) {
300191
if let Self::Generator(generator) = self {
301192
*self = Self::range_intersection(&x_range, &generator.x_range)
302193
.map(|intersection| {
@@ -320,7 +211,7 @@ impl<'a> PlotPoints<'a> {
320211
(start < end).then_some(start..=end)
321212
}
322213

323-
pub(super) fn bounds(&self) -> PlotBounds {
214+
pub fn bounds(&self) -> PlotBounds {
324215
match self {
325216
Self::Owned(points) => {
326217
let mut bounds = PlotBounds::NOTHING;
@@ -341,62 +232,6 @@ impl<'a> PlotPoints<'a> {
341232
}
342233
}
343234

344-
// ----------------------------------------------------------------------------
345-
346-
/// Circle, Diamond, Square, Cross, …
347-
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
348-
pub enum MarkerShape {
349-
Circle,
350-
Diamond,
351-
Square,
352-
Cross,
353-
Plus,
354-
Up,
355-
Down,
356-
Left,
357-
Right,
358-
Asterisk,
359-
}
360-
361-
impl MarkerShape {
362-
/// Get a vector containing all marker shapes.
363-
pub fn all() -> impl ExactSizeIterator<Item = Self> {
364-
[
365-
Self::Circle,
366-
Self::Diamond,
367-
Self::Square,
368-
Self::Cross,
369-
Self::Plus,
370-
Self::Up,
371-
Self::Down,
372-
Self::Left,
373-
Self::Right,
374-
Self::Asterisk,
375-
]
376-
.iter()
377-
.copied()
378-
}
379-
}
380-
381-
// ----------------------------------------------------------------------------
382-
383-
/// Query the points of the plot, for geometric relations like closest checks
384-
pub enum PlotGeometry<'a> {
385-
/// No geometry based on single elements (examples: text, image,
386-
/// horizontal/vertical line)
387-
None,
388-
389-
/// Point values (X-Y graphs)
390-
Points(&'a [PlotPoint]),
391-
392-
/// Rectangles (examples: boxes or bars)
393-
// Has currently no data, as it would require copying rects or iterating a list of pointers.
394-
// Instead, geometry-based functions are directly implemented in the respective PlotItem impl.
395-
Rects,
396-
}
397-
398-
// ----------------------------------------------------------------------------
399-
400235
/// Describes a function y = f(x) with an optional range for x and a number of
401236
/// points.
402237
pub struct ExplicitGenerator<'a> {
@@ -447,16 +282,3 @@ impl ExplicitGenerator<'_> {
447282
bounds
448283
}
449284
}
450-
451-
// ----------------------------------------------------------------------------
452-
453-
/// Result of [`super::PlotItem::find_closest()`] search, identifies an element
454-
/// inside the item for immediate use
455-
pub struct ClosestElem {
456-
/// Position of hovered-over value (or bar/box-plot/…) in `PlotItem`
457-
pub index: usize,
458-
459-
/// Squared distance from the mouse cursor (needed to compare against other
460-
/// `PlotItems`, which might be nearer)
461-
pub dist_sq: f32,
462-
}

0 commit comments

Comments
 (0)