1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//! The [`Plot`] builder methods.
use egui::Id;
use super::axis::{Bounds, Scale};
use super::config::PlotConfig;
use super::fit::{Fit, FitKind};
use super::series::Series;
use super::{
DirectionArrowConfig, GaugeConfig, HeatmapConfig, LegendAnimation, LegendHoverConfig, LegendPosition, PieConfig,
Plot, RadarConfig, WaterfallConfig,
};
impl<'a> Plot<'a> {
/// A plot with a stable id.
pub fn new(id: impl std::hash::Hash + std::fmt::Debug) -> Self {
Self {
id: Id::unique(("workbench_plot", id)),
series: Vec::new(),
config: PlotConfig::default(),
width: None,
height: None,
x_window: None,
include: Bounds::NOTHING,
settings_button: false,
export_button: false,
}
}
/// Set the area tool mode: rectangle or curve-vs-chord.
pub fn measurement_mode(mut self, mode: super::MeasurementMode) -> Self {
self.config.measurement.mode = mode;
self
}
/// Set which area the curve-segment label emphasizes.
pub fn measurement_area_style(mut self, style: super::MeasurementAreaStyle) -> Self {
self.config.measurement.area_style = style;
self
}
/// Set the measurement snap radius in points.
pub fn measurement_snap_radius(mut self, radius: f32) -> Self {
self.config.measurement.snap_radius = radius.max(2.0);
self
}
/// Show or hide measurement labels.
pub fn measurement_labels(mut self, show: bool) -> Self {
self.config.measurement.show_labels = show;
self
}
/// Replace the whole appearance (text, sizes, colors, axes, grid, legend, and
/// per-series styles). Call it first; the other builder methods edit it.
pub fn config(mut self, config: PlotConfig) -> Self {
self.config = config;
self
}
/// The current appearance.
pub fn current_config(&self) -> &PlotConfig {
&self.config
}
/// The names of the series, in drawing order (for a settings dialog).
pub fn series_names(&self) -> Vec<String> {
self.series.iter().map(|series| series.name.clone()).collect()
}
/// Fixed x limits in data units (`None` keeps that side automatic).
pub fn x_limits(mut self, min: Option<f64>, max: Option<f64>) -> Self {
(self.config.x.min, self.config.x.max) = (min, max);
self
}
/// Fixed y limits in data units (`None` keeps that side automatic).
pub fn y_limits(mut self, min: Option<f64>, max: Option<f64>) -> Self {
(self.config.y.min, self.config.y.max) = (min, max);
self
}
/// Show a settings button in the toolbar; see [`PlotResponse::settings_requested`](super::PlotResponse::settings_requested).
pub fn settings_button(mut self, show: bool) -> Self {
self.settings_button = show;
self
}
/// Show an SVG/PNG export menu in the toolbar; see
/// [`PlotResponse::export_requested`](super::PlotResponse::export_requested).
pub fn export_button(mut self, show: bool) -> Self {
self.export_button = show;
self
}
/// Add a series. Series draw in order; the palette color follows the order too.
pub fn series(mut self, series: Series<'a>) -> Self {
self.series.push(series);
self
}
/// Fit `kind` to `data` and add the dashed curve, named with its equation and R².
/// Nothing is added when the fit fails (too few points, singular system).
pub fn fit(mut self, kind: FitKind, data: &[[f64; 2]]) -> Self {
if let Some(fit) = Fit::new(kind, data) {
self.series.push(fit.series(fit.equation(), 200));
}
self
}
/// A title above the frame.
pub fn title(mut self, title: impl Into<String>) -> Self {
self.config.title = Some(title.into());
self
}
/// The x axis label (also used in the hover popup).
pub fn x_label(mut self, label: impl Into<String>) -> Self {
self.config.x.label = Some(label.into());
self
}
/// The y axis label, drawn vertically.
pub fn y_label(mut self, label: impl Into<String>) -> Self {
self.config.y.label = Some(label.into());
self
}
/// Linear or log10 x axis.
pub fn x_scale(mut self, scale: Scale) -> Self {
self.config.x.scale = scale;
self
}
/// Linear or log10 y axis.
pub fn y_scale(mut self, scale: Scale) -> Self {
self.config.y.scale = scale;
self
}
/// The right y axis title, drawn vertically (the axis shows when a series uses
/// [`YAxis::Right`](super::YAxis::Right)).
pub fn y2_label(mut self, label: impl Into<String>) -> Self {
self.config.y2.label = Some(label.into());
self
}
/// Linear or log10 right y axis.
pub fn y2_scale(mut self, scale: Scale) -> Self {
self.config.y2.scale = scale;
self
}
/// Fixed right y limits in data units (`None` keeps that side automatic).
pub fn y2_limits(mut self, min: Option<f64>, max: Option<f64>) -> Self {
(self.config.y2.min, self.config.y2.max) = (min, max);
self
}
/// Fixed width; fills the available width otherwise.
pub fn width(mut self, width: f32) -> Self {
self.width = Some(width);
self
}
/// Fixed height; fills the available height otherwise (320 pt in scroll areas).
pub fn height(mut self, height: f32) -> Self {
self.height = Some(height);
self
}
/// Legend corner, or [`LegendPosition::Hidden`].
pub fn legend(mut self, legend: LegendPosition) -> Self {
self.config.legend = legend;
self
}
/// Replace the legend hover animation settings.
pub fn legend_hover(mut self, config: LegendHoverConfig) -> Self {
self.config.legend_hover = config;
self
}
/// Select the CPU or GPU data layer.
pub fn renderer(mut self, renderer: super::Renderer) -> Self {
self.config.renderer = renderer;
self
}
/// The animation played while the pointer rests on a legend row (enabled by default).
pub fn legend_animation(mut self, animation: LegendAnimation) -> Self {
self.config.legend_hover.animation = animation;
self
}
/// Add every series from an iterator, for multi-series families such as box plots.
pub fn series_all(mut self, series: impl IntoIterator<Item = super::Series<'a>>) -> Self {
self.series.extend(series);
self
}
/// Replace the direction-arrow settings.
pub fn direction(mut self, config: DirectionArrowConfig) -> Self {
self.config.direction = config;
self
}
/// Show or hide the direction arrow beside the newest point of line-like series.
pub fn direction_arrow(mut self, show: bool) -> Self {
self.config.direction.enabled = show;
self
}
/// How many trailing samples measure the direction arrow (5 by default).
pub fn direction_points(mut self, points: usize) -> Self {
self.config.direction.points = points.max(2);
self
}
/// Replace the pie/donut layout.
pub fn pie(mut self, config: PieConfig) -> Self {
self.config.pie = config;
self
}
/// Replace the radar layout (labels, maximum, rings, fill).
pub fn radar(mut self, config: RadarConfig) -> Self {
self.config.radar = config;
self
}
/// Replace the gauge layout.
pub fn gauge(mut self, config: GaugeConfig) -> Self {
self.config.gauge = config;
self
}
/// Replace the heatmap layout.
pub fn heatmap(mut self, config: HeatmapConfig) -> Self {
self.config.heatmap = config;
self
}
/// Replace the waterfall layout.
pub fn waterfall(mut self, config: WaterfallConfig) -> Self {
self.config.waterfall = config;
self
}
/// Draw the grid (on by default).
pub fn grid(mut self, grid: bool) -> Self {
for axis in [&mut self.config.x, &mut self.config.y] {
axis.grid_major = grid;
axis.grid_minor = grid;
}
self
}
/// Show the tool buttons in the top-right corner (on by default).
pub fn toolbar(mut self, toolbar: bool) -> Self {
self.config.toolbar = toolbar;
self
}
/// For streaming data: while the view is automatic, show only the last `width`
/// x units.
pub fn x_window(mut self, width: f64) -> Self {
self.x_window = Some(width);
self
}
/// Keep `point` inside the automatic view (for example `[0.0, 0.0]`).
pub fn include(mut self, point: [f64; 2]) -> Self {
self.include.extend(point);
self
}
}