Skip to main content

crisol_plot/plot/
series_charts.rs

1//! Constructors for the chart families beyond lines, bars, and scatter.
2
3use std::borrow::Cow;
4
5use super::series::{Kind, Series};
6
7impl<'a> Series<'a> {
8    /// One slice of a pie or donut chart. Add one series per slice; the values are
9    /// normalized to the visible slices.
10    ///
11    /// ```
12    /// # use crisol_plot::plot::{PieConfig, Plot, Series};
13    /// # let ctx = egui::Context::default();
14    /// # let mut icons = crisol_plot::Icons::new();
15    /// # let mut state = crisol_plot::plot::PlotState::default();
16    /// # let mut output = ctx.run_ui(egui::RawInput::default(), |ui| {
17    /// let mut config = crisol_plot::plot::PlotConfig::default();
18    /// config.pie.inner_radius = 0.55;
19    /// Plot::new("share")
20    ///     .config(config)
21    ///     .series(Series::pie("Desktop", 62.0))
22    ///     .series(Series::pie("Mobile", 38.0))
23    ///     .show(ui, &mut icons, &mut state);
24    /// # });
25    /// # output.textures_delta.clear();
26    /// ```
27    pub fn pie(name: impl Into<String>, value: f64) -> Series<'static> {
28        Series::new(name, Cow::Owned(Vec::new()), Kind::Pie { value })
29    }
30
31    /// Open–high–low–close candles: `[x, open, high, low, close]` rows.
32    pub fn candles(name: impl Into<String>, rows: impl Into<Cow<'a, [[f64; 5]]>>) -> Series<'a> {
33        let rows = rows.into();
34        let close = rows.iter().map(|row| [row[0], row[4]]).collect();
35        let mut series = Series::new(name, Cow::Owned(close), Kind::Candles { width: None });
36        series.ohlc = Some(rows);
37        series
38    }
39
40    /// A heatmap from `[x, y, value]` cells. Cell size follows the smallest gap on each
41    /// axis; the color scale spans the visible values.
42    pub fn heatmap(name: impl Into<String>, cells: impl Into<Cow<'a, [[f64; 3]]>>) -> Series<'a> {
43        let cells = cells.into();
44        let points = cells.iter().map(|cell| [cell[0], cell[1]]).collect();
45        let mut series = Series::new(name, Cow::Owned(points), Kind::Heatmap);
46        series.cells = Some(cells);
47        series
48    }
49
50    /// A radar polygon over the axes named in [`RadarConfig::labels`](super::RadarConfig::labels).
51    /// Values are indexed in order around the ring; zero is the center.
52    pub fn radar(name: impl Into<String>, values: impl AsRef<[f64]>) -> Series<'static> {
53        let data = values
54            .as_ref()
55            .iter()
56            .enumerate()
57            .map(|(index, value)| [index as f64, *value])
58            .collect();
59        Series::new(name, Cow::Owned(data), Kind::Radar)
60    }
61
62    /// One gauge with its own range.
63    pub fn gauge(name: impl Into<String>, value: f64, range: std::ops::RangeInclusive<f64>) -> Series<'static> {
64        let (mut min, mut max) = (*range.start(), *range.end());
65        if !(min.is_finite() && max.is_finite()) || min >= max {
66            min = min.min(0.0);
67            max = min + 1.0;
68        }
69        let value = if value.is_finite() { value } else { min };
70        Series::new(name, Cow::Owned(Vec::new()), Kind::Gauge { value, min, max })
71    }
72
73    /// A waterfall: each `[x, change]` bar starts where the previous one ended.
74    pub fn waterfall(name: impl Into<String>, data: impl Into<Cow<'a, [[f64; 2]]>>) -> Series<'a> {
75        Series::new(name, data.into(), Kind::Waterfall { width: None })
76    }
77}