crisol_plot/plot/
builder.rs1use 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 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 pub fn measurement_mode(mut self, mode: super::MeasurementMode) -> Self {
32 self.config.measurement.mode = mode;
33 self
34 }
35
36 pub fn measurement_area_style(mut self, style: super::MeasurementAreaStyle) -> Self {
38 self.config.measurement.area_style = style;
39 self
40 }
41
42 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 pub fn measurement_labels(mut self, show: bool) -> Self {
50 self.config.measurement.show_labels = show;
51 self
52 }
53
54 pub fn config(mut self, config: PlotConfig) -> Self {
57 self.config = config;
58 self
59 }
60
61 pub fn current_config(&self) -> &PlotConfig {
63 &self.config
64 }
65
66 pub fn series_names(&self) -> Vec<String> {
68 self.series.iter().map(|series| series.name.clone()).collect()
69 }
70
71 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 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 pub fn settings_button(mut self, show: bool) -> Self {
85 self.settings_button = show;
86 self
87 }
88
89 pub fn export_button(mut self, show: bool) -> Self {
92 self.export_button = show;
93 self
94 }
95
96 pub fn series(mut self, series: Series<'a>) -> Self {
98 self.series.push(series);
99 self
100 }
101
102 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 pub fn title(mut self, title: impl Into<String>) -> Self {
113 self.config.title = Some(title.into());
114 self
115 }
116
117 pub fn x_label(mut self, label: impl Into<String>) -> Self {
119 self.config.x.label = Some(label.into());
120 self
121 }
122
123 pub fn y_label(mut self, label: impl Into<String>) -> Self {
125 self.config.y.label = Some(label.into());
126 self
127 }
128
129 pub fn x_scale(mut self, scale: Scale) -> Self {
131 self.config.x.scale = scale;
132 self
133 }
134
135 pub fn y_scale(mut self, scale: Scale) -> Self {
137 self.config.y.scale = scale;
138 self
139 }
140
141 pub fn y2_label(mut self, label: impl Into<String>) -> Self {
144 self.config.y2.label = Some(label.into());
145 self
146 }
147
148 pub fn y2_scale(mut self, scale: Scale) -> Self {
150 self.config.y2.scale = scale;
151 self
152 }
153
154 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 pub fn width(mut self, width: f32) -> Self {
162 self.width = Some(width);
163 self
164 }
165
166 pub fn height(mut self, height: f32) -> Self {
168 self.height = Some(height);
169 self
170 }
171
172 pub fn legend(mut self, legend: LegendPosition) -> Self {
174 self.config.legend = legend;
175 self
176 }
177
178 pub fn legend_hover(mut self, config: LegendHoverConfig) -> Self {
180 self.config.legend_hover = config;
181 self
182 }
183
184 pub fn renderer(mut self, renderer: super::Renderer) -> Self {
186 self.config.renderer = renderer;
187 self
188 }
189
190 pub fn legend_animation(mut self, animation: LegendAnimation) -> Self {
192 self.config.legend_hover.animation = animation;
193 self
194 }
195
196 pub fn series_all(mut self, series: impl IntoIterator<Item = super::Series<'a>>) -> Self {
198 self.series.extend(series);
199 self
200 }
201
202 pub fn direction(mut self, config: DirectionArrowConfig) -> Self {
204 self.config.direction = config;
205 self
206 }
207
208 pub fn direction_arrow(mut self, show: bool) -> Self {
210 self.config.direction.enabled = show;
211 self
212 }
213
214 pub fn direction_points(mut self, points: usize) -> Self {
216 self.config.direction.points = points.max(2);
217 self
218 }
219
220 pub fn pie(mut self, config: PieConfig) -> Self {
222 self.config.pie = config;
223 self
224 }
225
226 pub fn radar(mut self, config: RadarConfig) -> Self {
228 self.config.radar = config;
229 self
230 }
231
232 pub fn gauge(mut self, config: GaugeConfig) -> Self {
234 self.config.gauge = config;
235 self
236 }
237
238 pub fn heatmap(mut self, config: HeatmapConfig) -> Self {
240 self.config.heatmap = config;
241 self
242 }
243
244 pub fn waterfall(mut self, config: WaterfallConfig) -> Self {
246 self.config.waterfall = config;
247 self
248 }
249
250 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 pub fn toolbar(mut self, toolbar: bool) -> Self {
261 self.config.toolbar = toolbar;
262 self
263 }
264
265 pub fn x_window(mut self, width: f64) -> Self {
268 self.x_window = Some(width);
269 self
270 }
271
272 pub fn include(mut self, point: [f64; 2]) -> Self {
274 self.include.extend(point);
275 self
276 }
277}