Skip to main content

crisol_plot/plot/three/
builder.rs

1//! The [`Plot3D`] builder methods.
2
3use egui::Id;
4
5use super::super::axis::Scale;
6use super::super::{DirectionArrowConfig, LegendAnimation, LegendHoverConfig, LegendPosition};
7use super::{Bounds3D, Plot3D, Plot3DConfig, Projection, Series3D, Surface3DConfig};
8
9impl<'a> Plot3D<'a> {
10    /// A 3D plot with a stable id.
11    pub fn new(id: impl std::hash::Hash + std::fmt::Debug) -> Self {
12        Self {
13            id: Id::unique(("workbench_plot3d", id)),
14            series: Vec::new(),
15            config: Plot3DConfig::default(),
16            width: None,
17            height: None,
18            include: Bounds3D::NOTHING,
19            settings_button: false,
20            export_button: false,
21        }
22    }
23
24    /// Select the CPU or GPU filled-surface layer.
25    pub fn renderer(mut self, renderer: super::super::Renderer) -> Self {
26        self.config.renderer = renderer;
27        self
28    }
29
30    /// Replace the whole appearance.
31    pub fn config(mut self, config: Plot3DConfig) -> Self {
32        self.config = config;
33        self
34    }
35
36    /// The current appearance.
37    pub fn current_config(&self) -> &Plot3DConfig {
38        &self.config
39    }
40
41    /// The names of the series, in drawing order.
42    pub fn series_names(&self) -> Vec<String> {
43        self.series.iter().map(|series| series.name.clone()).collect()
44    }
45
46    /// Add a series. Series draw in order; the palette color follows the order too.
47    pub fn series(mut self, series: Series3D<'a>) -> Self {
48        self.series.push(series);
49        self
50    }
51
52    /// A title above the frame.
53    pub fn title(mut self, title: impl Into<String>) -> Self {
54        self.config.title = Some(title.into());
55        self
56    }
57
58    /// The x axis title.
59    pub fn x_label(mut self, label: impl Into<String>) -> Self {
60        self.config.x.label = Some(label.into());
61        self
62    }
63
64    /// The y axis title.
65    pub fn y_label(mut self, label: impl Into<String>) -> Self {
66        self.config.y.label = Some(label.into());
67        self
68    }
69
70    /// The z axis title.
71    pub fn z_label(mut self, label: impl Into<String>) -> Self {
72        self.config.z.label = Some(label.into());
73        self
74    }
75
76    /// Fixed x limits in data units.
77    pub fn x_limits(mut self, min: Option<f64>, max: Option<f64>) -> Self {
78        (self.config.x.min, self.config.x.max) = (min, max);
79        self
80    }
81
82    /// Fixed y limits in data units.
83    pub fn y_limits(mut self, min: Option<f64>, max: Option<f64>) -> Self {
84        (self.config.y.min, self.config.y.max) = (min, max);
85        self
86    }
87
88    /// Fixed z limits in data units.
89    pub fn z_limits(mut self, min: Option<f64>, max: Option<f64>) -> Self {
90        (self.config.z.min, self.config.z.max) = (min, max);
91        self
92    }
93
94    /// Linear or log10 x axis.
95    pub fn x_scale(mut self, scale: Scale) -> Self {
96        self.config.x.scale = scale;
97        self
98    }
99
100    /// Linear or log10 y axis.
101    pub fn y_scale(mut self, scale: Scale) -> Self {
102        self.config.y.scale = scale;
103        self
104    }
105
106    /// Linear or log10 z axis.
107    pub fn z_scale(mut self, scale: Scale) -> Self {
108        self.config.z.scale = scale;
109        self
110    }
111
112    /// Fixed width; fills the available width otherwise.
113    pub fn width(mut self, width: f32) -> Self {
114        self.width = Some(width);
115        self
116    }
117
118    /// Fixed height; fills the available height otherwise (320 pt in scroll areas).
119    pub fn height(mut self, height: f32) -> Self {
120        self.height = Some(height);
121        self
122    }
123
124    /// Legend corner, or [`LegendPosition::Hidden`].
125    pub fn legend(mut self, legend: LegendPosition) -> Self {
126        self.config.legend = legend;
127        self
128    }
129
130    /// Replace the legend hover animation settings.
131    pub fn legend_hover(mut self, config: LegendHoverConfig) -> Self {
132        self.config.legend_hover = config;
133        self
134    }
135
136    /// The animation played while the pointer rests on a legend row.
137    pub fn legend_animation(mut self, animation: LegendAnimation) -> Self {
138        self.config.legend_hover.animation = animation;
139        self
140    }
141
142    /// Replace the direction-arrow settings.
143    pub fn direction(mut self, config: DirectionArrowConfig) -> Self {
144        self.config.direction = config;
145        self
146    }
147
148    /// Show or hide the direction arrow beside the newest line point.
149    pub fn direction_arrow(mut self, show: bool) -> Self {
150        self.config.direction.enabled = show;
151        self
152    }
153
154    /// How many trailing samples measure the direction arrow.
155    pub fn direction_points(mut self, points: usize) -> Self {
156        self.config.direction.points = points.max(2);
157        self
158    }
159
160    /// Perspective or orthographic.
161    pub fn projection(mut self, projection: Projection) -> Self {
162        self.config.projection = projection;
163        self
164    }
165
166    /// Initial yaw and pitch in radians.
167    pub fn camera_angles(mut self, yaw: f32, pitch: f32) -> Self {
168        (self.config.yaw, self.config.pitch) = (yaw, pitch);
169        self
170    }
171
172    /// Vertical field of view in degrees.
173    pub fn fov(mut self, fov: f32) -> Self {
174        self.config.fov = fov;
175        self
176    }
177
178    /// Replace the surface fill and wireframe settings.
179    pub fn surface(mut self, config: Surface3DConfig) -> Self {
180        self.config.surface = config;
181        self
182    }
183
184    /// Draw the grid (on by default).
185    pub fn grid(mut self, grid: bool) -> Self {
186        for axis in [&mut self.config.x, &mut self.config.y, &mut self.config.z] {
187            axis.grid_major = grid;
188            axis.grid_minor = grid;
189        }
190        self
191    }
192
193    /// Show the tool buttons in the top-right corner (on by default).
194    pub fn toolbar(mut self, toolbar: bool) -> Self {
195        self.config.toolbar = toolbar;
196        self
197    }
198
199    /// Keep `point` inside the automatic view.
200    pub fn include(mut self, point: [f64; 3]) -> Self {
201        self.include.extend(point);
202        self
203    }
204
205    /// Show a settings button in the toolbar.
206    pub fn settings_button(mut self, show: bool) -> Self {
207        self.settings_button = show;
208        self
209    }
210
211    /// Show an SVG/PNG export menu in the toolbar.
212    pub fn export_button(mut self, show: bool) -> Self {
213        self.export_button = show;
214        self
215    }
216}