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
//! Constructors for the chart families beyond lines, bars, and scatter.
use std::borrow::Cow;
use super::series::{Kind, Series};
impl<'a> Series<'a> {
/// One slice of a pie or donut chart. Add one series per slice; the values are
/// normalized to the visible slices.
///
/// ```
/// # use crisol_plot::plot::{PieConfig, Plot, Series};
/// # let ctx = egui::Context::default();
/// # let mut icons = crisol_plot::Icons::new();
/// # let mut state = crisol_plot::plot::PlotState::default();
/// # let mut output = ctx.run_ui(egui::RawInput::default(), |ui| {
/// let mut config = crisol_plot::plot::PlotConfig::default();
/// config.pie.inner_radius = 0.55;
/// Plot::new("share")
/// .config(config)
/// .series(Series::pie("Desktop", 62.0))
/// .series(Series::pie("Mobile", 38.0))
/// .show(ui, &mut icons, &mut state);
/// # });
/// # output.textures_delta.clear();
/// ```
pub fn pie(name: impl Into<String>, value: f64) -> Series<'static> {
Series::new(name, Cow::Owned(Vec::new()), Kind::Pie { value })
}
/// Open–high–low–close candles: `[x, open, high, low, close]` rows.
pub fn candles(name: impl Into<String>, rows: impl Into<Cow<'a, [[f64; 5]]>>) -> Series<'a> {
let rows = rows.into();
let close = rows.iter().map(|row| [row[0], row[4]]).collect();
let mut series = Series::new(name, Cow::Owned(close), Kind::Candles { width: None });
series.ohlc = Some(rows);
series
}
/// A heatmap from `[x, y, value]` cells. Cell size follows the smallest gap on each
/// axis; the color scale spans the visible values.
pub fn heatmap(name: impl Into<String>, cells: impl Into<Cow<'a, [[f64; 3]]>>) -> Series<'a> {
let cells = cells.into();
let points = cells.iter().map(|cell| [cell[0], cell[1]]).collect();
let mut series = Series::new(name, Cow::Owned(points), Kind::Heatmap);
series.cells = Some(cells);
series
}
/// A radar polygon over the axes named in [`RadarConfig::labels`](super::RadarConfig::labels).
/// Values are indexed in order around the ring; zero is the center.
pub fn radar(name: impl Into<String>, values: impl AsRef<[f64]>) -> Series<'static> {
let data = values
.as_ref()
.iter()
.enumerate()
.map(|(index, value)| [index as f64, *value])
.collect();
Series::new(name, Cow::Owned(data), Kind::Radar)
}
/// One gauge with its own range.
pub fn gauge(name: impl Into<String>, value: f64, range: std::ops::RangeInclusive<f64>) -> Series<'static> {
let (mut min, mut max) = (*range.start(), *range.end());
if !(min.is_finite() && max.is_finite()) || min >= max {
min = min.min(0.0);
max = min + 1.0;
}
let value = if value.is_finite() { value } else { min };
Series::new(name, Cow::Owned(Vec::new()), Kind::Gauge { value, min, max })
}
/// A waterfall: each `[x, change]` bar starts where the previous one ended.
pub fn waterfall(name: impl Into<String>, data: impl Into<Cow<'a, [[f64; 2]]>>) -> Series<'a> {
Series::new(name, data.into(), Kind::Waterfall { width: None })
}
}