1use egui::Color32;
6
7use super::LegendPosition;
8use super::animation::LegendHoverConfig;
9use super::axis::Scale;
10use super::chart_config::{DirectionArrowConfig, GaugeConfig, HeatmapConfig, PieConfig, RadarConfig, WaterfallConfig};
11use super::measure::MeasurementConfig;
12use super::series::{LineStyle, Marker, Smoothing, YAxis};
13
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
16#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
17#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
18#[non_exhaustive]
19pub enum Renderer {
20 #[default]
22 Cpu,
23 Gpu,
25}
26
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29#[cfg_attr(feature = "serde", serde(rename_all = "snake_case"))]
30#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
31#[non_exhaustive]
32pub enum TitleAlign {
33 Left,
35 #[default]
37 Center,
38 Right,
40}
41
42pub use super::axis::Minor;
43
44#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
45#[derive(Clone, Debug, PartialEq)]
47pub struct AxisConfig {
48 pub label: Option<String>,
50 pub scale: Scale,
52 pub min: Option<f64>,
54 pub max: Option<f64>,
56 pub major_step: Option<f64>,
59 pub minor: Minor,
61 pub grid_major: bool,
63 pub grid_minor: bool,
65 pub decimals: Option<u8>,
67 pub tick_labels: bool,
69 pub label_size: Option<f32>,
71 pub label_color: Option<Color32>,
73 pub tick_color: Option<Color32>,
76 pub categories: Option<Vec<String>>,
78}
79
80impl Default for AxisConfig {
81 fn default() -> Self {
82 Self {
83 label: None,
84 scale: Scale::Linear,
85 min: None,
86 max: None,
87 major_step: None,
88 minor: Minor::Auto,
89 grid_major: true,
90 grid_minor: true,
91 decimals: None,
92 tick_labels: true,
93 label_size: None,
94 label_color: None,
95 tick_color: None,
96 categories: None,
97 }
98 }
99}
100
101#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
102#[derive(Clone, Copy, Debug, Default, PartialEq)]
104pub struct PlotColors {
105 pub background: Option<Color32>,
107 pub canvas: Option<Color32>,
109 pub frame: Option<Color32>,
111 pub grid: Option<Color32>,
113 pub text: Option<Color32>,
115 pub tick_text: Option<Color32>,
117 pub selection: Option<Color32>,
119}
120
121#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
122#[derive(Clone, Debug, Default, PartialEq)]
124pub struct SeriesStyle {
125 pub name: String,
127 pub color: Option<Color32>,
129 pub width: Option<f32>,
131 pub line_style: Option<LineStyle>,
133 pub marker: Option<Marker>,
135 pub marker_size: Option<f32>,
137 pub fill_opacity: Option<f32>,
139 pub smoothing: Option<Smoothing>,
141 pub y_axis: Option<YAxis>,
143 pub direction_arrow: Option<bool>,
145}
146
147impl SeriesStyle {
148 pub fn is_empty(&self) -> bool {
150 self.color.is_none()
151 && self.width.is_none()
152 && self.line_style.is_none()
153 && self.marker.is_none()
154 && self.marker_size.is_none()
155 && self.fill_opacity.is_none()
156 && self.smoothing.is_none()
157 && self.y_axis.is_none()
158 && self.direction_arrow.is_none()
159 }
160}
161
162#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
163#[derive(Clone, Debug, PartialEq)]
165pub struct PlotConfig {
166 pub title: Option<String>,
168 pub x: AxisConfig,
170 pub y: AxisConfig,
172 pub y2: AxisConfig,
175 pub title_size: f32,
177 pub title_color: Option<Color32>,
179 pub title_align: TitleAlign,
181 pub label_size: f32,
183 pub tick_size: f32,
185 pub legend_size: f32,
187 pub frame_width: f32,
189 pub grid_width: f32,
191 pub grid_major_opacity: f32,
193 pub grid_minor_opacity: f32,
195 pub tick_length: f32,
197 pub line_scale: f32,
199 pub marker_scale: f32,
201 pub legend: LegendPosition,
203 pub legend_frame: bool,
205 pub toolbar: bool,
207 pub hover_guides: bool,
209 pub hover_popup: bool,
211 pub legend_hover: LegendHoverConfig,
213 pub direction: DirectionArrowConfig,
215 pub pie: PieConfig,
217 pub radar: RadarConfig,
219 pub gauge: GaugeConfig,
221 pub heatmap: HeatmapConfig,
223 pub waterfall: WaterfallConfig,
225 pub colors: PlotColors,
227 pub series: Vec<SeriesStyle>,
229 pub measurement: MeasurementConfig,
231 pub renderer: Renderer,
233}
234
235impl Default for PlotConfig {
236 fn default() -> Self {
237 Self {
238 title: None,
239 x: AxisConfig::default(),
240 y: AxisConfig::default(),
241 y2: AxisConfig {
242 grid_major: false,
243 grid_minor: false,
244 ..AxisConfig::default()
245 },
246 title_size: 14.0,
247 title_color: None,
248 title_align: TitleAlign::Center,
249 label_size: 12.0,
250 tick_size: 11.0,
251 legend_size: 11.5,
252 frame_width: 1.0,
253 grid_width: 1.0,
254 grid_major_opacity: 0.55,
255 grid_minor_opacity: 0.22,
256 tick_length: 4.0,
257 line_scale: 1.0,
258 marker_scale: 1.0,
259 legend: LegendPosition::TopLeft,
260 legend_frame: true,
261 toolbar: true,
262 hover_guides: true,
263 hover_popup: true,
264 legend_hover: LegendHoverConfig::default(),
265 direction: DirectionArrowConfig::default(),
266 pie: PieConfig::default(),
267 radar: RadarConfig::default(),
268 gauge: GaugeConfig::default(),
269 heatmap: HeatmapConfig::default(),
270 waterfall: WaterfallConfig::default(),
271 colors: PlotColors::default(),
272 series: Vec::new(),
273 measurement: MeasurementConfig::default(),
274 renderer: Renderer::Cpu,
275 }
276 }
277}
278
279impl PlotConfig {
280 pub fn series_style(&self, name: &str) -> Option<&SeriesStyle> {
282 self.series.iter().find(|style| style.name == name)
283 }
284
285 pub fn series_style_mut(&mut self, name: &str) -> &mut SeriesStyle {
287 if let Some(index) = self.series.iter().position(|style| style.name == name) {
288 return &mut self.series[index];
289 }
290 self.series.push(SeriesStyle {
291 name: name.to_owned(),
292 ..SeriesStyle::default()
293 });
294 let last = self.series.len() - 1;
295 &mut self.series[last]
296 }
297
298 pub fn sanitize(&mut self) {
300 self.title_size = self.title_size.clamp(6.0, 48.0);
301 self.label_size = self.label_size.clamp(6.0, 36.0);
302 self.tick_size = self.tick_size.clamp(6.0, 30.0);
303 self.legend_size = self.legend_size.clamp(6.0, 30.0);
304 self.frame_width = self.frame_width.clamp(0.0, 6.0);
305 self.grid_width = self.grid_width.clamp(0.5, 6.0);
306 self.grid_major_opacity = self.grid_major_opacity.clamp(0.0, 1.0);
307 self.grid_minor_opacity = self.grid_minor_opacity.clamp(0.0, 1.0);
308 self.tick_length = self.tick_length.clamp(0.0, 16.0);
309 self.line_scale = self.line_scale.clamp(0.25, 8.0);
310 self.marker_scale = self.marker_scale.clamp(0.25, 8.0);
311 self.legend_hover.period = self.legend_hover.period.clamp(0.2, 10.0);
312 self.direction.points = self.direction.points.clamp(2, 64);
313 self.direction.length = self.direction.length.clamp(4.0, 160.0);
314 self.direction.width = self.direction.width.clamp(0.5, 8.0);
315 self.direction.head = self.direction.head.clamp(2.0, 40.0);
316 self.pie.inner_radius = self.pie.inner_radius.clamp(0.0, 0.9);
317 if !self.pie.start_angle.is_finite() {
318 self.pie.start_angle = -std::f32::consts::FRAC_PI_2;
319 }
320 self.radar.rings = self.radar.rings.clamp(1, 12);
321 self.radar.fill_opacity = self.radar.fill_opacity.clamp(0.0, 1.0);
322 self.radar.max = self.radar.max.filter(|max| max.is_finite() && *max > 0.0);
323 self.gauge.thickness = self.gauge.thickness.clamp(2.0, 40.0);
324 if !(self.gauge.start_angle.is_finite() && self.gauge.end_angle.is_finite())
325 || (self.gauge.end_angle - self.gauge.start_angle).abs() < 0.1
326 {
327 self.gauge.start_angle = 0.75 * std::f32::consts::PI;
328 self.gauge.end_angle = 2.25 * std::f32::consts::PI;
329 }
330 self.heatmap.opacity = self.heatmap.opacity.clamp(0.0, 1.0);
331 self.waterfall.bar_width = self
332 .waterfall
333 .bar_width
334 .filter(|width| width.is_finite() && *width > 0.0);
335 for axis in [&mut self.x, &mut self.y, &mut self.y2] {
336 axis.label_size = axis.label_size.map(|size| size.clamp(6.0, 36.0));
337 if let (Some(min), Some(max)) = (axis.min, axis.max)
338 && min >= max
339 {
340 axis.max = None;
341 }
342 if axis.scale == Scale::Log10 {
343 axis.min = axis.min.filter(|min| *min > 0.0);
344 axis.max = axis.max.filter(|max| *max > 0.0);
345 }
346 axis.major_step = axis.major_step.filter(|step| step.is_finite() && *step > 0.0);
347 if let Minor::Divisions(divisions) = axis.minor {
348 axis.minor = Minor::Divisions(divisions.clamp(2, 20));
349 }
350 axis.decimals = axis.decimals.map(|decimals| decimals.min(10));
351 }
352 for style in &mut self.series {
353 style.width = style.width.map(|width| width.clamp(0.25, 12.0));
354 style.marker_size = style.marker_size.map(|size| size.clamp(0.5, 20.0));
355 style.fill_opacity = style.fill_opacity.map(|opacity| opacity.clamp(0.0, 1.0));
356 }
357 }
358}
359
360#[cfg(test)]
361mod tests {
362 use super::*;
363
364 #[test]
365 fn sanitize_drops_impossible_limits() {
366 let mut config = PlotConfig::default();
367 config.x.min = Some(5.0);
368 config.x.max = Some(1.0);
369 config.y.scale = Scale::Log10;
370 config.y.min = Some(-1.0);
371 config.y.major_step = Some(0.0);
372 config.title_size = 400.0;
373 config.legend_hover.period = 0.0;
374 config.direction.points = 0;
375 config.pie.inner_radius = 4.0;
376 config.radar.rings = 0;
377 config.gauge.thickness = 900.0;
378 config.heatmap.opacity = 4.0;
379 config.waterfall.bar_width = Some(-1.0);
380 config.sanitize();
381 assert_eq!((config.x.min, config.x.max), (Some(5.0), None));
382 assert_eq!(config.y.min, None);
383 assert_eq!(config.y.major_step, None);
384 assert_eq!(config.title_size, 48.0);
385 assert_eq!(config.legend_hover.period, 0.2);
386 assert_eq!(config.direction.points, 2);
387 assert_eq!(config.pie.inner_radius, 0.9);
388 assert_eq!(config.radar.rings, 1);
389 assert_eq!(config.gauge.thickness, 40.0);
390 assert_eq!(config.heatmap.opacity, 1.0);
391 assert_eq!(config.waterfall.bar_width, None);
392 }
393
394 #[test]
395 fn series_styles_are_found_or_created() {
396 let mut config = PlotConfig::default();
397 config.series_style_mut("a").width = Some(3.0);
398 config.series_style_mut("a").color = Some(Color32::RED);
399 assert_eq!(config.series.len(), 1);
400 assert_eq!(config.series_style("a").and_then(|style| style.width), Some(3.0));
401 }
402}