Skip to main content

crisol_plot/plot/
chart_config.rs

1//! Layout settings for the chart families that are not plain lines.
2
3use egui::Color32;
4
5/// Settings for the direction arrow drawn beside the newest point of a line.
6///
7/// The arrow uses the last `points` finite samples (5 by default), so a noisy stream
8/// still shows the direction the buffer is growing. Only cartesian lines, areas, steps,
9/// and candlesticks draw it; pie, radar, gauge, heatmap, and reference series do not.
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[derive(Clone, Copy, Debug, PartialEq)]
12pub struct DirectionArrowConfig {
13    /// Draw the arrow.
14    pub enabled: bool,
15    /// Samples used to measure the direction; 5 by default.
16    pub points: usize,
17    /// Shaft length in points.
18    pub length: f32,
19    /// Shaft width in points.
20    pub width: f32,
21    /// Head length in points.
22    pub head: f32,
23    /// Arrow color; the series color when `None`.
24    pub color: Option<Color32>,
25}
26
27impl Default for DirectionArrowConfig {
28    fn default() -> Self {
29        Self {
30            enabled: false,
31            points: 5,
32            length: 26.0,
33            width: 2.0,
34            head: 8.0,
35            color: None,
36        }
37    }
38}
39
40#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
41/// Layout of pie and donut charts.
42#[derive(Clone, Copy, Debug, PartialEq)]
43pub struct PieConfig {
44    /// Inner radius as a fraction of the outer radius, 0 for a pie.
45    pub inner_radius: f32,
46    /// Where the first slice starts, in radians; -π/2 is 12 o'clock.
47    pub start_angle: f32,
48    /// Draw the value or percentage on slices with room for it.
49    pub labels: bool,
50    /// Show percentages instead of raw values.
51    pub percentages: bool,
52    /// Write the total in the middle of a donut.
53    pub total: bool,
54}
55
56impl Default for PieConfig {
57    fn default() -> Self {
58        Self {
59            inner_radius: 0.0,
60            start_angle: -std::f32::consts::FRAC_PI_2,
61            labels: true,
62            percentages: true,
63            total: true,
64        }
65    }
66}
67
68#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
69/// Layout of radar charts. One series draws one closed polygon; several series overlay.
70#[derive(Clone, Debug, PartialEq)]
71pub struct RadarConfig {
72    /// Names around the ring; indexed by each point's x. Numbers are used when empty.
73    pub labels: Vec<String>,
74    /// Fixed maximum; automatic when `None`.
75    pub max: Option<f64>,
76    /// Grid rings.
77    pub rings: u8,
78    /// Fill opacity of each series, 0–1.
79    pub fill_opacity: f32,
80}
81
82impl Default for RadarConfig {
83    fn default() -> Self {
84        Self {
85            labels: Vec::new(),
86            max: None,
87            rings: 4,
88            fill_opacity: 0.16,
89        }
90    }
91}
92
93impl RadarConfig {
94    /// A radar with `labels` around the ring.
95    pub fn new(labels: impl IntoIterator<Item = impl Into<String>>) -> Self {
96        Self {
97            labels: labels.into_iter().map(Into::into).collect(),
98            ..Self::default()
99        }
100    }
101}
102
103#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
104/// Layout of gauge charts.
105#[derive(Clone, Copy, Debug, PartialEq)]
106pub struct GaugeConfig {
107    /// Start of the arc in radians.
108    pub start_angle: f32,
109    /// End of the arc in radians.
110    pub end_angle: f32,
111    /// Arc thickness in points.
112    pub thickness: f32,
113    /// Draw the value under the center.
114    pub show_value: bool,
115    /// Draw the range ends.
116    pub show_range: bool,
117}
118
119impl Default for GaugeConfig {
120    fn default() -> Self {
121        Self {
122            start_angle: 0.75 * std::f32::consts::PI,
123            end_angle: 2.25 * std::f32::consts::PI,
124            thickness: 10.0,
125            show_value: true,
126            show_range: true,
127        }
128    }
129}
130
131#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
132/// Layout of heatmaps.
133#[derive(Clone, Copy, Debug, PartialEq)]
134pub struct HeatmapConfig {
135    /// Fill opacity, 0–1.
136    pub opacity: f32,
137    /// Draw a value scale at the right edge.
138    pub colorbar: bool,
139    /// Color scale.
140    pub colormap: super::Colormap,
141}
142
143impl Default for HeatmapConfig {
144    fn default() -> Self {
145        Self {
146            opacity: 1.0,
147            colorbar: true,
148            colormap: super::Colormap::Theme,
149        }
150    }
151}
152
153#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
154/// Layout of waterfall charts.
155#[derive(Clone, Copy, Debug, PartialEq)]
156pub struct WaterfallConfig {
157    /// Bar width in data units; 80 % of the smallest x spacing when `None`.
158    pub bar_width: Option<f64>,
159    /// Dashed connectors between consecutive bars.
160    pub connectors: bool,
161}
162
163impl Default for WaterfallConfig {
164    fn default() -> Self {
165        Self {
166            bar_width: None,
167            connectors: true,
168        }
169    }
170}