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, pt: Point) -> bool {
126 if let Some(r) = self.hit_rect_opt() {
127 return pt.get_x() >= r.get_x()
128 && pt.get_x() <= r.get_x() + r.get_width()
129 && pt.get_y() >= r.get_y()
130 && pt.get_y() <= r.get_y() + r.get_height();
131 }
132 let dx: f32 = pt.get_x() - self.get_anchor().get_x();
133 let dy: f32 = pt.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 st: Self = Self::default();
152 for s in option.get_series().iter() {
153 if !s.get_visible() {
154 st.get_mut_hidden_series().push(s.get_name().clone());
155 }
156 }
157 let initial_dz: Option<&EuvChartDataZoom> = option
158 .get_data_zoom()
159 .iter()
160 .find(|dz: &&EuvChartDataZoom| dz.get_kind() == EuvChartDataZoomKind::Slider)
161 .or_else(|| option.get_data_zoom().first());
162 if let Some(dz) = initial_dz {
163 st.set_zoom_start_pct(dz.get_start_pct().clamp(0.0, 99.0));
164 st.set_zoom_end_pct(dz.get_end_pct().clamp(st.get_zoom_start_pct() + 1.0, 100.0));
165 }
166 st
167 }
168
169 /// Returns whether the series named `name` is currently visible.
170 ///
171 /// # Arguments
172 ///
173 /// - `&Self` - Chart state.
174 /// - `&str` - Series name.
175 ///
176 /// # Returns
177 ///
178 /// - `bool` - True when visible.
179 #[must_use]
180 pub fn is_series_visible(&self, name: &str) -> bool {
181 !self
182 .get_hidden_series()
183 .iter()
184 .any(|h: &String| h.as_str() == name)
185 }
186
187 /// Toggles visibility for `name` and returns its new visible boolean state.
188 ///
189 /// # Arguments
190 ///
191 /// - `&Self` - Mutable chart state.
192 /// - `&str` - Series name to toggle.
193 ///
194 /// # Returns
195 ///
196 /// - `bool` - New visibility (`true` = now visible, `false` = now hidden).
197 pub fn toggle_series(&mut self, name: &str) -> bool {
198 if let Some(pos) = self
199 .get_hidden_series()
200 .iter()
201 .position(|h: &String| h.as_str() == name)
202 {
203 let _: String = self.get_mut_hidden_series().remove(pos);
204 true
205 } else {
206 self.get_mut_hidden_series().push(name.to_string());
207 false
208 }
209 }
210
211 /// Sets the active zoom percentage window `[start_pct, end_pct]` respecting `min_span_pct`.
212 ///
213 /// # Arguments
214 ///
215 /// - `&Self` - Mutable chart state.
216 /// - `f32` - Requested lower percentage.
217 /// - `f32` - Requested upper percentage.
218 /// - `f32` - Minimum allowed span percentage.
219 pub fn set_zoom(&mut self, start_pct: f32, end_pct: f32, min_span_pct: f32) {
220 let min_span: f32 = min_span_pct.clamp(2.0, 95.0);
221 let lo: f32 = start_pct.clamp(0.0, 100.0 - min_span);
222 let hi: f32 = end_pct.clamp(lo + min_span, 100.0);
223 self.set_zoom_start_pct(lo);
224 self.set_zoom_end_pct(hi);
225 }
226
227 /// Zooms the active window centered around normalized plot cursor `anchor_frac` in `[0.0, 1.0]`.
228 ///
229 /// # Arguments
230 ///
231 /// - `&Self` - Mutable chart state.
232 /// - `f32` - Wheel delta (negative zooms in, positive zooms out).
233 /// - `f32` - Normalized horizontal cursor position in `[0.0, 1.0]`.
234 /// - `f32` - Minimum allowed span percentage.
235 ///
236 /// # Returns
237 ///
238 /// - `bool` - True when the zoom window changed.
239 pub fn zoom_by_wheel(&mut self, wheel_delta: f32, anchor_frac: f32, min_span_pct: f32) -> bool {
240 if wheel_delta.abs() < 1e-4 {
241 return false;
242 }
243 let cur_start: f32 = self.get_zoom_start_pct();
244 let cur_end: f32 = self.get_zoom_end_pct();
245 let cur_span: f32 = (cur_end - cur_start).max(1.0);
246 let factor: f32 = if wheel_delta < 0.0 { 0.85 } else { 1.18 };
247 let min_span: f32 = min_span_pct.clamp(2.0, 95.0);
248 let next_span: f32 = (cur_span * factor).clamp(min_span, 100.0);
249 let pivot: f32 = cur_start + cur_span * anchor_frac.clamp(0.0, 1.0);
250 let mut next_start: f32 = pivot - next_span * anchor_frac.clamp(0.0, 1.0);
251 let mut next_end: f32 = next_start + next_span;
252 if next_start < 0.0 {
253 next_end = (next_end - next_start).min(100.0);
254 next_start = 0.0;
255 }
256 if next_end > 100.0 {
257 next_start = (next_start - (next_end - 100.0)).max(0.0);
258 next_end = 100.0;
259 }
260 let changed: bool =
261 (next_start - cur_start).abs() > 0.05 || (next_end - cur_end).abs() > 0.05;
262 self.set_zoom_start_pct(next_start);
263 self.set_zoom_end_pct(next_end);
264 changed
265 }
266
267 /// Pans the active zoomed window horizontally by `delta_pct` while preserving span length.
268 ///
269 /// # Arguments
270 ///
271 /// - `&Self` - Mutable chart state.
272 /// - `f32` - Percentage shift along the axis.
273 ///
274 /// # Returns
275 ///
276 /// - `bool` - True when the zoom window moved.
277 pub fn pan_by_pct(&mut self, delta_pct: f32) -> bool {
278 let cur_start: f32 = self.get_zoom_start_pct();
279 let cur_end: f32 = self.get_zoom_end_pct();
280 let span: f32 = cur_end - cur_start;
281 if span >= 99.9 {
282 return false;
283 }
284 let next_start: f32 = (cur_start + delta_pct).clamp(0.0, 100.0 - span);
285 let next_end: f32 = next_start + span;
286 let changed: bool = (next_start - cur_start).abs() > 0.05;
287 self.set_zoom_start_pct(next_start);
288 self.set_zoom_end_pct(next_end);
289 changed
290 }
291
292 /// Cycles the toolbox `MagicType` override (`Default -> Line -> Bar -> Stack -> Default`).
293 ///
294 /// # Arguments
295 ///
296 /// - `&Self` - Mutable chart state.
297 ///
298 /// # Returns
299 ///
300 /// - `EuvChartMagicType` - Newly active MagicType mode.
301 pub fn cycle_magic_type(&mut self) -> EuvChartMagicType {
302 let next: EuvChartMagicType = match self.get_magic_type() {
303 EuvChartMagicType::Default => EuvChartMagicType::Line,
304 EuvChartMagicType::Line => EuvChartMagicType::Bar,
305 EuvChartMagicType::Bar => EuvChartMagicType::Stack,
306 EuvChartMagicType::Stack => EuvChartMagicType::Default,
307 };
308 self.set_magic_type(next);
309 next
310 }
311
312 /// Toggles the tabular `DataView` panel open or closed.
313 ///
314 /// # Arguments
315 ///
316 /// - `&Self` - Mutable chart state.
317 ///
318 /// # Returns
319 ///
320 /// - `bool` - New open state.
321 pub fn toggle_data_view(&mut self) -> bool {
322 let next: bool = !self.get_data_view_open();
323 self.set_data_view_open(next);
324 next
325 }
326
327 /// Toggles radial pop-out selection for pie slice `idx`.
328 ///
329 /// # Arguments
330 ///
331 /// - `&Self` - Mutable chart state.
332 /// - `usize` - Clicked slice index.
333 pub fn toggle_pie_slice(&mut self, idx: usize) {
334 let next: Option<usize> = if self.selected_pie_slice_opt() == Some(idx) {
335 None
336 } else {
337 Some(idx)
338 };
339 self.set_selected_pie_slice(next);
340 }
341
342 /// Restores legend visibility, zoom bounds, magic type, pie selection, and data-view back to `option` defaults.
343 ///
344 /// # Arguments
345 ///
346 /// - `&Self` - Mutable chart state.
347 /// - `&EuvChartOption` - Declarative chart option defaults.
348 pub fn restore(&mut self, option: &EuvChartOption) {
349 *self = Self::from_option(option);
350 }
351
352 /// Returns a copy of this chart state with `hover_pos` cleared for static base-layer paint caching.
353 ///
354 /// # Arguments
355 ///
356 /// - `&Self` - Chart state reference.
357 ///
358 /// # Returns
359 ///
360 /// - `Self` - Hover-independent chart state key.
361 #[must_use]
362 pub fn without_hover(&self) -> Self {
363 let mut copy: Self = self.clone();
364 let _: &mut Self = copy.set_hover_pos(None);
365 copy
366 }
367
368 /// Returns the optional popped-out pie slice index.
369 ///
370 /// # Arguments
371 ///
372 /// - `&Self` - Chart state.
373 ///
374 /// # Returns
375 ///
376 /// - `Option<usize>` - Active popped-out slice index.
377 #[must_use]
378 pub fn selected_pie_slice_opt(&self) -> Option<usize> {
379 let Self {
380 selected_pie_slice, ..
381 } = *self;
382 selected_pie_slice
383 }
384
385 /// Returns the optional active hover pointer coordinate.
386 ///
387 /// # Arguments
388 ///
389 /// - `&Self` - Chart state.
390 ///
391 /// # Returns
392 ///
393 /// - `Option<Point>` - Pointer coordinate in logical canvas space.
394 #[must_use]
395 pub fn hover_pos_opt(&self) -> Option<Point> {
396 let Self { hover_pos, .. } = *self;
397 hover_pos
398 }
399}
400
401/// Safe optional field accessor for `EuvChartRenderResult`.
402impl EuvChartRenderResult {
403 /// Returns the bottom interactive slider geometry when a `DataZoom::Slider` was drawn.
404 ///
405 /// # Arguments
406 ///
407 /// - `&Self` - Chart render result.
408 ///
409 /// # Returns
410 ///
411 /// - `Option<EuvChartSliderRects>` - Slider hit geometry.
412 #[must_use]
413 pub fn slider_rects_opt(&self) -> Option<EuvChartSliderRects> {
414 let Self { slider_rects, .. } = *self;
415 slider_rects
416 }
417}