Skip to main content

crisol_plot/plot/
builder.rs

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