euv_ui/component/chart/engine/impl.rs
1use super::*;
2
3/// Default neutral interactive state spanning `0%..=100%` with all series visible.
4impl Default for EuvChartState {
5 /// Creates an initial neutral chart interaction state.
6 ///
7 /// # Returns
8 ///
9 /// - `Self` - Default chart state.
10 fn default() -> Self {
11 Self {
12 hidden_series: Vec::new(),
13 zoom_start_pct: 0.0,
14 zoom_end_pct: 100.0,
15 magic_type: EuvChartMagicType::Default,
16 data_view_open: false,
17 selected_pie_slice: None,
18 hover_pos: None,
19 }
20 }
21}
22
23/// Constructors and hit-testing helpers for `EuvChartDatumHit`.
24impl EuvChartDatumHit {
25 /// Creates a circular point datum hit target.
26 ///
27 /// # Arguments
28 ///
29 /// - `(usize, String, usize)` - Tuple `(series_index, series_name, data_index)`.
30 /// - `(String, f32, String)` - Tuple `(category_label, value, detail)`.
31 /// - `Color` - Swatch color.
32 /// - `Point` - Anchor coordinate.
33 /// - `f32` - Hit radius in logical pixels.
34 ///
35 /// # Returns
36 ///
37 /// - `Self` - Point hit record.
38 #[must_use]
39 pub(crate) fn point_hit(
40 id: (usize, String, usize),
41 info: (String, f32, String),
42 color: Color,
43 anchor: Point,
44 hit_radius: f32,
45 ) -> Self {
46 let (series_index, series_name, data_index): (usize, String, usize) = id;
47 let (category_label, value, detail): (String, f32, String) = info;
48 Self {
49 series_index,
50 series_name,
51 data_index,
52 category_label,
53 value,
54 detail,
55 color,
56 anchor,
57 hit_radius,
58 hit_rect: None,
59 }
60 }
61
62 /// Creates a rectangular box datum hit target.
63 ///
64 /// # Arguments
65 ///
66 /// - `(usize, String, usize)` - Tuple `(series_index, series_name, data_index)`.
67 /// - `(String, f32, String)` - Tuple `(category_label, value, detail)`.
68 /// - `Color` - Swatch color.
69 /// - `Point` - Tooltip anchor point.
70 /// - `Rect` - Bounding hit rectangle.
71 ///
72 /// # Returns
73 ///
74 /// - `Self` - Rectangular hit record.
75 #[must_use]
76 pub(crate) fn rect_hit(
77 id: (usize, String, usize),
78 info: (String, f32, String),
79 color: Color,
80 anchor: Point,
81 rect: Rect,
82 ) -> Self {
83 let (series_index, series_name, data_index): (usize, String, usize) = id;
84 let (category_label, value, detail): (String, f32, String) = info;
85 Self {
86 series_index,
87 series_name,
88 data_index,
89 category_label,
90 value,
91 detail,
92 color,
93 anchor,
94 hit_radius: 12.0,
95 hit_rect: Some(rect),
96 }
97 }
98
99 /// Returns the optional rectangular hit bounds.
100 ///
101 /// # Arguments
102 ///
103 /// - `&Self` - Datum hit record.
104 ///
105 /// # Returns
106 ///
107 /// - `Option<Rect>` - Hit box when rectangular.
108 #[must_use]
109 pub fn hit_rect_opt(&self) -> Option<Rect> {
110 let Self { hit_rect, .. } = *self;
111 hit_rect
112 }
113
114 /// Tests whether `pt` falls inside this datum's hit box or circular hit radius.
115 ///
116 /// # Arguments
117 ///
118 /// - `&Self` - Datum hit record.
119 /// - `Point` - Probe pointer coordinate.
120 ///
121 /// # Returns
122 ///
123 /// - `bool` - True when `pt` hits this datum.
124 #[must_use]
125 pub fn contains(&self, point_coords: Point) -> bool {
126 if let Some(red_val) = self.hit_rect_opt() {
127 return point_coords.get_x() >= red_val.get_x()
128 && point_coords.get_x() <= red_val.get_x() + red_val.get_width()
129 && point_coords.get_y() >= red_val.get_y()
130 && point_coords.get_y() <= red_val.get_y() + red_val.get_height();
131 }
132 let dx: f32 = point_coords.get_x() - self.get_anchor().get_x();
133 let dy: f32 = point_coords.get_y() - self.get_anchor().get_y();
134 (dx * dx + dy * dy) <= self.get_hit_radius() * self.get_hit_radius()
135 }
136}
137
138/// Interactive state transitions for `EuvChartState`.
139impl EuvChartState {
140 /// Initializes chart interaction state from a declarative `EuvChartOption`.
141 ///
142 /// # Arguments
143 ///
144 /// - `&EuvChartOption` - Declarative chart option.
145 ///
146 /// # Returns
147 ///
148 /// - `Self` - Initialized state honoring initial series visibility and data-zoom range.
149 #[must_use]
150 pub fn from_option(option: &EuvChartOption) -> Self {
151 let mut node_style: Self = Self::default();
152 for source_val in option.get_series().iter() {
153 if !source_val.get_visible() {
154 node_style
155 .get_mut_hidden_series()
156 .push(source_val.get_name().clone());
157 }
158 }
159 let initial_dz: Option<&EuvChartDataZoom> = option
160 .get_data_zoom()
161 .iter()
162 .find(|dz: &&EuvChartDataZoom| dz.get_kind() == EuvChartDataZoomKind::Slider)
163 .or_else(|| option.get_data_zoom().first());
164 if let Some(dz) = initial_dz {
165 node_style.set_zoom_start_pct(dz.get_start_pct().clamp(0.0, 99.0));
166 node_style.set_zoom_end_pct(
167 dz.get_end_pct()
168 .clamp(node_style.get_zoom_start_pct() + 1.0, 100.0),
169 );
170 }
171 node_style
172 }
173
174 /// Returns whether the series named `name` is currently visible.
175 ///
176 /// # Arguments
177 ///
178 /// - `&Self` - Chart state.
179 /// - `&str` - Series name.
180 ///
181 /// # Returns
182 ///
183 /// - `bool` - True when visible.
184 #[must_use]
185 pub fn is_series_visible(&self, name: &str) -> bool {
186 !self
187 .get_hidden_series()
188 .iter()
189 .any(|height_val: &String| height_val.as_str() == name)
190 }
191
192 /// Toggles visibility for `name` and returns its new visible boolean state.
193 ///
194 /// # Arguments
195 ///
196 /// - `&Self` - Mutable chart state.
197 /// - `&str` - Series name to toggle.
198 ///
199 /// # Returns
200 ///
201 /// - `bool` - New visibility (`true` = now visible, `false` = now hidden).
202 pub fn toggle_series(&mut self, name: &str) -> bool {
203 if let Some(pos) = self
204 .get_hidden_series()
205 .iter()
206 .position(|height_val: &String| height_val.as_str() == name)
207 {
208 let _: String = self.get_mut_hidden_series().remove(pos);
209 true
210 } else {
211 self.get_mut_hidden_series().push(name.to_string());
212 false
213 }
214 }
215
216 /// Sets the active zoom percentage window `[start_pct, end_pct]` respecting `min_span_pct`.
217 ///
218 /// # Arguments
219 ///
220 /// - `&Self` - Mutable chart state.
221 /// - `f32` - Requested lower percentage.
222 /// - `f32` - Requested upper percentage.
223 /// - `f32` - Minimum allowed span percentage.
224 pub fn set_zoom(&mut self, start_pct: f32, end_pct: f32, min_span_pct: f32) {
225 let min_span: f32 = min_span_pct.clamp(2.0, 95.0);
226 let lo: f32 = start_pct.clamp(0.0, 100.0 - min_span);
227 let hi: f32 = end_pct.clamp(lo + min_span, 100.0);
228 self.set_zoom_start_pct(lo);
229 self.set_zoom_end_pct(hi);
230 }
231
232 /// Zooms the active window centered around normalized plot cursor `anchor_frac` in `[0.0, 1.0]`.
233 ///
234 /// # Arguments
235 ///
236 /// - `&Self` - Mutable chart state.
237 /// - `f32` - Wheel delta (negative zooms in, positive zooms out).
238 /// - `f32` - Normalized horizontal cursor position in `[0.0, 1.0]`.
239 /// - `f32` - Minimum allowed span percentage.
240 ///
241 /// # Returns
242 ///
243 /// - `bool` - True when the zoom window changed.
244 pub fn zoom_by_wheel(&mut self, wheel_delta: f32, anchor_frac: f32, min_span_pct: f32) -> bool {
245 if wheel_delta.abs() < 1e-4 {
246 return false;
247 }
248 let cur_start: f32 = self.get_zoom_start_pct();
249 let cur_end: f32 = self.get_zoom_end_pct();
250 let cur_span: f32 = (cur_end - cur_start).max(1.0);
251 let factor: f32 = if wheel_delta < 0.0 { 0.85 } else { 1.18 };
252 let min_span: f32 = min_span_pct.clamp(2.0, 95.0);
253 let next_span: f32 = (cur_span * factor).clamp(min_span, 100.0);
254 let pivot: f32 = cur_start + cur_span * anchor_frac.clamp(0.0, 1.0);
255 let mut next_start: f32 = pivot - next_span * anchor_frac.clamp(0.0, 1.0);
256 let mut next_end: f32 = next_start + next_span;
257 if next_start < 0.0 {
258 next_end = (next_end - next_start).min(100.0);
259 next_start = 0.0;
260 }
261 if next_end > 100.0 {
262 next_start = (next_start - (next_end - 100.0)).max(0.0);
263 next_end = 100.0;
264 }
265 let changed: bool =
266 (next_start - cur_start).abs() > 0.05 || (next_end - cur_end).abs() > 0.05;
267 self.set_zoom_start_pct(next_start);
268 self.set_zoom_end_pct(next_end);
269 changed
270 }
271
272 /// Pans the active zoomed window horizontally by `delta_pct` while preserving span length.
273 ///
274 /// # Arguments
275 ///
276 /// - `&Self` - Mutable chart state.
277 /// - `f32` - Percentage shift along the axis.
278 ///
279 /// # Returns
280 ///
281 /// - `bool` - True when the zoom window moved.
282 pub fn pan_by_pct(&mut self, delta_pct: f32) -> bool {
283 let cur_start: f32 = self.get_zoom_start_pct();
284 let cur_end: f32 = self.get_zoom_end_pct();
285 let span: f32 = cur_end - cur_start;
286 if span >= 99.9 {
287 return false;
288 }
289 let next_start: f32 = (cur_start + delta_pct).clamp(0.0, 100.0 - span);
290 let next_end: f32 = next_start + span;
291 let changed: bool = (next_start - cur_start).abs() > 0.05;
292 self.set_zoom_start_pct(next_start);
293 self.set_zoom_end_pct(next_end);
294 changed
295 }
296
297 /// Cycles the toolbox `MagicType` override (`Default -> Line -> Bar -> Stack -> Default`).
298 ///
299 /// # Arguments
300 ///
301 /// - `&Self` - Mutable chart state.
302 ///
303 /// # Returns
304 ///
305 /// - `EuvChartMagicType` - Newly active MagicType mode.
306 pub fn cycle_magic_type(&mut self) -> EuvChartMagicType {
307 let next: EuvChartMagicType = match self.get_magic_type() {
308 EuvChartMagicType::Default => EuvChartMagicType::Line,
309 EuvChartMagicType::Line => EuvChartMagicType::Bar,
310 EuvChartMagicType::Bar => EuvChartMagicType::Stack,
311 EuvChartMagicType::Stack => EuvChartMagicType::Default,
312 };
313 self.set_magic_type(next);
314 next
315 }
316
317 /// Toggles the tabular `DataView` panel open or closed.
318 ///
319 /// # Arguments
320 ///
321 /// - `&Self` - Mutable chart state.
322 ///
323 /// # Returns
324 ///
325 /// - `bool` - New open state.
326 pub fn toggle_data_view(&mut self) -> bool {
327 let next: bool = !self.get_data_view_open();
328 self.set_data_view_open(next);
329 next
330 }
331
332 /// Toggles radial pop-out selection for pie slice `idx`.
333 ///
334 /// # Arguments
335 ///
336 /// - `&Self` - Mutable chart state.
337 /// - `usize` - Clicked slice index.
338 pub fn toggle_pie_slice(&mut self, idx: usize) {
339 let next: Option<usize> = if self.selected_pie_slice_opt() == Some(idx) {
340 None
341 } else {
342 Some(idx)
343 };
344 self.set_selected_pie_slice(next);
345 }
346
347 /// Restores legend visibility, zoom bounds, magic type, pie selection, and data-view back to `option` defaults.
348 ///
349 /// # Arguments
350 ///
351 /// - `&Self` - Mutable chart state.
352 /// - `&EuvChartOption` - Declarative chart option defaults.
353 pub fn restore(&mut self, option: &EuvChartOption) {
354 *self = Self::from_option(option);
355 }
356
357 /// Returns a copy of this chart state with `hover_pos` cleared for static base-layer paint caching.
358 ///
359 /// # Arguments
360 ///
361 /// - `&Self` - Chart state reference.
362 ///
363 /// # Returns
364 ///
365 /// - `Self` - Hover-independent chart state key.
366 #[must_use]
367 pub fn without_hover(&self) -> Self {
368 let mut copy: Self = self.clone();
369 let _: &mut Self = copy.set_hover_pos(None);
370 copy
371 }
372
373 /// Returns the optional popped-out pie slice index.
374 ///
375 /// # Arguments
376 ///
377 /// - `&Self` - Chart state.
378 ///
379 /// # Returns
380 ///
381 /// - `Option<usize>` - Active popped-out slice index.
382 #[must_use]
383 pub fn selected_pie_slice_opt(&self) -> Option<usize> {
384 let Self {
385 selected_pie_slice, ..
386 } = *self;
387 selected_pie_slice
388 }
389
390 /// Returns the optional active hover pointer coordinate.
391 ///
392 /// # Arguments
393 ///
394 /// - `&Self` - Chart state.
395 ///
396 /// # Returns
397 ///
398 /// - `Option<Point>` - Pointer coordinate in logical canvas space.
399 #[must_use]
400 pub fn hover_pos_opt(&self) -> Option<Point> {
401 let Self { hover_pos, .. } = *self;
402 hover_pos
403 }
404}
405
406/// Safe optional field accessor for `EuvChartRenderResult`.
407impl EuvChartRenderResult {
408 /// Returns the bottom interactive slider geometry when a `DataZoom::Slider` was drawn.
409 ///
410 /// # Arguments
411 ///
412 /// - `&Self` - Chart render result.
413 ///
414 /// # Returns
415 ///
416 /// - `Option<EuvChartSliderRects>` - Slider hit geometry.
417 #[must_use]
418 pub fn slider_rects_opt(&self) -> Option<EuvChartSliderRects> {
419 let Self { slider_rects, .. } = *self;
420 slider_rects
421 }
422}