Skip to main content

crisol_plot/plot/three/
config.rs

1//! Everything about a 3D plot that is not data: axes, camera, text, colors, and
2//! per-series overrides. Edit a [`Plot3DConfig`] and pass it to [`super::Plot3D`].
3
4use egui::Color32;
5
6use super::super::animation::LegendHoverConfig;
7use super::super::chart_config::DirectionArrowConfig;
8use super::super::config::{AxisConfig, PlotColors, TitleAlign};
9use super::super::series::{LineStyle, Marker};
10use super::super::types::LegendPosition;
11use super::camera::Projection;
12
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14/// Overrides for one 3D series, matched by name.
15#[derive(Clone, Debug, Default, PartialEq)]
16pub struct Series3DStyle {
17    /// The series name this applies to.
18    pub name: String,
19    /// Stroke and fill color.
20    pub color: Option<Color32>,
21    /// Line width in points.
22    pub width: Option<f32>,
23    /// Solid, dashed, or dotted lines.
24    pub line_style: Option<LineStyle>,
25    /// Marker shape for scatter series or line vertices.
26    pub marker: Option<Marker>,
27    /// Marker radius in points.
28    pub marker_size: Option<f32>,
29    /// Opacity of bars and surface cells, 0–1.
30    pub fill_opacity: Option<f32>,
31    /// Show the direction arrow for this line; [`Plot3DConfig::direction`] when `None`.
32    pub direction_arrow: Option<bool>,
33}
34
35impl Series3DStyle {
36    /// Whether nothing is overridden.
37    pub fn is_empty(&self) -> bool {
38        self.color.is_none()
39            && self.width.is_none()
40            && self.line_style.is_none()
41            && self.marker.is_none()
42            && self.marker_size.is_none()
43            && self.fill_opacity.is_none()
44            && self.direction_arrow.is_none()
45    }
46}
47
48#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
49/// Fill, lines, and color scale of a surface.
50#[derive(Clone, Copy, Debug, PartialEq)]
51pub struct Surface3DConfig {
52    /// Draw the grid lines over the cells.
53    pub wireframe: bool,
54    /// Fill opacity, 0–1.
55    pub opacity: f32,
56    /// Color cells from the height scale instead of the series color.
57    pub color_scale: bool,
58}
59
60impl Default for Surface3DConfig {
61    fn default() -> Self {
62        Self {
63            wireframe: true,
64            opacity: 0.92,
65            color_scale: true,
66        }
67    }
68}
69
70/// The complete appearance of a 3D plot.
71#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
72#[derive(Clone, Debug, PartialEq)]
73pub struct Plot3DConfig {
74    /// Title above the frame.
75    pub title: Option<String>,
76    /// The horizontal x axis.
77    pub x: AxisConfig,
78    /// The horizontal y axis (depth).
79    pub y: AxisConfig,
80    /// The vertical z axis.
81    pub z: AxisConfig,
82    /// Title size in points.
83    pub title_size: f32,
84    /// Title color; [`PlotColors::text`] (or the theme) when `None`.
85    pub title_color: Option<Color32>,
86    /// Title alignment over the frame.
87    pub title_align: TitleAlign,
88    /// Axis title size in points.
89    pub label_size: f32,
90    /// Tick label size in points.
91    pub tick_size: f32,
92    /// Legend text size in points.
93    pub legend_size: f32,
94    /// Frame line width in points.
95    pub frame_width: f32,
96    /// Grid line width in points.
97    pub grid_width: f32,
98    /// Opacity of major grid lines, 0–1.
99    pub grid_major_opacity: f32,
100    /// Opacity of minor grid lines, 0–1.
101    pub grid_minor_opacity: f32,
102    /// Length of tick marks.
103    pub tick_length: f32,
104    /// Multiplies every line width.
105    pub line_scale: f32,
106    /// Multiplies every marker size.
107    pub marker_scale: f32,
108    /// Legend corner or hidden.
109    pub legend: LegendPosition,
110    /// Draw the legend's background box.
111    pub legend_frame: bool,
112    /// Show the tool buttons.
113    pub toolbar: bool,
114    /// Dashed drop line from the hovered point to the floor.
115    pub hover_guides: bool,
116    /// The hover popup.
117    pub hover_popup: bool,
118    /// The animation played while a legend row is hovered.
119    pub legend_hover: LegendHoverConfig,
120    /// The arrow beside the newest point of a line.
121    pub direction: DirectionArrowConfig,
122    /// Surface fill and wireframe.
123    pub surface: Surface3DConfig,
124    /// Perspective or orthographic.
125    pub projection: Projection,
126    /// Initial rotation around the vertical axis, in radians.
127    pub yaw: f32,
128    /// Initial elevation, in radians.
129    pub pitch: f32,
130    /// Vertical field of view in degrees.
131    pub fov: f32,
132    /// Theme overrides.
133    pub colors: PlotColors,
134    /// Per-series overrides.
135    pub series: Vec<Series3DStyle>,
136    /// CPU or GPU filled surfaces.
137    pub renderer: super::super::Renderer,
138}
139
140impl Default for Plot3DConfig {
141    fn default() -> Self {
142        Self {
143            title: None,
144            x: AxisConfig::default(),
145            y: AxisConfig::default(),
146            z: AxisConfig::default(),
147            title_size: 14.0,
148            title_color: None,
149            title_align: TitleAlign::Center,
150            label_size: 12.0,
151            tick_size: 11.0,
152            legend_size: 11.5,
153            frame_width: 1.0,
154            grid_width: 1.0,
155            grid_major_opacity: 0.5,
156            grid_minor_opacity: 0.18,
157            tick_length: 4.0,
158            line_scale: 1.0,
159            marker_scale: 1.0,
160            legend: LegendPosition::TopLeft,
161            legend_frame: true,
162            toolbar: true,
163            hover_guides: true,
164            hover_popup: true,
165            legend_hover: LegendHoverConfig::default(),
166            direction: DirectionArrowConfig::default(),
167            surface: Surface3DConfig::default(),
168            projection: Projection::Perspective,
169            yaw: -0.72,
170            pitch: 0.42,
171            fov: 45.0,
172            colors: PlotColors::default(),
173            series: Vec::new(),
174            renderer: super::super::Renderer::Cpu,
175        }
176    }
177}
178
179impl Plot3DConfig {
180    /// The overrides for `name`, if any.
181    pub fn series_style(&self, name: &str) -> Option<&Series3DStyle> {
182        self.series.iter().find(|style| style.name == name)
183    }
184
185    /// The overrides for `name`, created empty when missing.
186    pub fn series_style_mut(&mut self, name: &str) -> &mut Series3DStyle {
187        if let Some(index) = self.series.iter().position(|style| style.name == name) {
188            return &mut self.series[index];
189        }
190        self.series.push(Series3DStyle {
191            name: name.to_owned(),
192            ..Series3DStyle::default()
193        });
194        let last = self.series.len() - 1;
195        &mut self.series[last]
196    }
197
198    /// Clamp every size, angle, and opacity to a sensible range.
199    pub fn sanitize(&mut self) {
200        self.title_size = self.title_size.clamp(6.0, 48.0);
201        self.label_size = self.label_size.clamp(6.0, 36.0);
202        self.tick_size = self.tick_size.clamp(6.0, 30.0);
203        self.legend_size = self.legend_size.clamp(6.0, 30.0);
204        self.frame_width = self.frame_width.clamp(0.0, 6.0);
205        self.grid_width = self.grid_width.clamp(0.5, 6.0);
206        self.grid_major_opacity = self.grid_major_opacity.clamp(0.0, 1.0);
207        self.grid_minor_opacity = self.grid_minor_opacity.clamp(0.0, 1.0);
208        self.tick_length = self.tick_length.clamp(0.0, 16.0);
209        self.line_scale = self.line_scale.clamp(0.25, 8.0);
210        self.marker_scale = self.marker_scale.clamp(0.25, 8.0);
211        self.legend_hover.period = self.legend_hover.period.clamp(0.2, 10.0);
212        self.direction.points = self.direction.points.clamp(2, 64);
213        self.direction.length = self.direction.length.clamp(4.0, 160.0);
214        self.direction.width = self.direction.width.clamp(0.5, 8.0);
215        self.direction.head = self.direction.head.clamp(2.0, 40.0);
216        self.surface.opacity = self.surface.opacity.clamp(0.0, 1.0);
217        if !self.yaw.is_finite() {
218            self.yaw = Plot3DConfig::default().yaw;
219        }
220        if !self.pitch.is_finite() {
221            self.pitch = Plot3DConfig::default().pitch;
222        }
223        self.pitch = self.pitch.clamp(-1.5, 1.5);
224        self.fov = if self.fov.is_finite() {
225            self.fov.clamp(15.0, 120.0)
226        } else {
227            45.0
228        };
229        for axis in [&mut self.x, &mut self.y, &mut self.z] {
230            axis.label_size = axis.label_size.map(|size| size.clamp(6.0, 36.0));
231            if let (Some(min), Some(max)) = (axis.min, axis.max)
232                && min >= max
233            {
234                axis.max = None;
235            }
236            if axis.scale == super::super::axis::Scale::Log10 {
237                axis.min = axis.min.filter(|min| *min > 0.0);
238                axis.max = axis.max.filter(|max| *max > 0.0);
239            }
240            axis.major_step = axis.major_step.filter(|step| step.is_finite() && *step > 0.0);
241            if let super::super::config::Minor::Divisions(divisions) = axis.minor {
242                axis.minor = super::super::config::Minor::Divisions(divisions.clamp(2, 20));
243            }
244            axis.decimals = axis.decimals.map(|decimals| decimals.min(10));
245        }
246        for style in &mut self.series {
247            style.width = style.width.map(|width| width.clamp(0.25, 12.0));
248            style.marker_size = style.marker_size.map(|size| size.clamp(0.5, 20.0));
249            style.fill_opacity = style.fill_opacity.map(|opacity| opacity.clamp(0.0, 1.0));
250        }
251    }
252}