1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use super::*;
/// Projection and inverse-mapping methods for `EuvChartAxisScale`.
impl EuvChartAxisScale {
/// Normalizes a domain value into `[0.0, 1.0]` respecting `Category`, `Value`, `Time`, `Log`, and `inverse`.
///
/// # Arguments
///
/// - `&Self` - Computed axis scale.
/// - `f32` - Domain value or category index.
///
/// # Returns
///
/// - `f32` - Normalized coordinate in `[0.0, 1.0]` (unclamped when outside range).
#[must_use]
pub fn normalize(&self, value: f32) -> f32 {
let min_v: f32 = self.get_min();
let max_v: f32 = self.get_max();
let raw_t: f32 = match self.get_axis_type() {
EuvChartAxisType::Log => {
let lo: f32 = min_v.max(SCALE_LOG_MIN_POSITIVE).log10();
let hi: f32 = max_v.max(SCALE_LOG_MIN_POSITIVE * 10.0).log10();
let span: f32 = (hi - lo).max(1e-6);
let v_log: f32 = value.max(SCALE_LOG_MIN_POSITIVE).log10();
(v_log - lo) / span
}
EuvChartAxisType::Category => {
let win_span: f32 = self.get_window_span();
if win_span > 1e-4 {
(value + 0.5 - self.get_window_start()) / win_span
} else {
let count: usize = self.get_categories().len();
if count == 0 {
0.5
} else {
(value + 0.5) / (count as f32)
}
}
}
EuvChartAxisType::Value | EuvChartAxisType::Time => {
let span: f32 = max_v - min_v;
if span.abs() < 1e-6 {
0.5
} else {
(value - min_v) / span
}
}
};
if self.get_inverse() {
1.0 - raw_t
} else {
raw_t
}
}
/// Projects a domain value onto a horizontal pixel coordinate inside `plot_rect`.
///
/// # Arguments
///
/// - `&Self` - Horizontal axis scale.
/// - `f32` - Domain value or category index.
/// - `Rect` - Target plot rectangle.
///
/// # Returns
///
/// - `f32` - Logical X pixel coordinate.
#[must_use]
pub fn map_x(&self, value: f32, plot_rect: Rect) -> f32 {
plot_rect.get_x() + self.normalize(value) * plot_rect.get_width()
}
/// Projects a domain value onto a vertical pixel coordinate inside `plot_rect` (bottom-up for numeric axes, top-down for category bars).
///
/// # Arguments
///
/// - `&Self` - Vertical axis scale.
/// - `f32` - Domain value or category index.
/// - `Rect` - Target plot rectangle.
///
/// # Returns
///
/// - `f32` - Logical Y pixel coordinate.
#[must_use]
pub fn map_y(&self, value: f32, plot_rect: Rect) -> f32 {
let t: f32 = self.normalize(value);
match self.get_axis_type() {
EuvChartAxisType::Category => plot_rect.get_y() + t * plot_rect.get_height(),
EuvChartAxisType::Value | EuvChartAxisType::Time | EuvChartAxisType::Log => {
plot_rect.get_y() + (1.0 - t) * plot_rect.get_height()
}
}
}
/// Returns the baseline pixel Y corresponding to `0.0` (clamped inside `plot_rect`).
///
/// # Arguments
///
/// - `&Self` - Vertical numeric axis scale.
/// - `Rect` - Target plot rectangle.
///
/// # Returns
///
/// - `f32` - Baseline Y coordinate in logical pixels.
#[must_use]
pub fn baseline_y(&self, plot_rect: Rect) -> f32 {
let zero_val: f32 = if self.get_axis_type() == EuvChartAxisType::Log {
self.get_min().max(SCALE_LOG_MIN_POSITIVE)
} else {
0.0_f32.clamp(self.get_min(), self.get_max())
};
self.map_y(zero_val, plot_rect).clamp(
plot_rect.get_y(),
plot_rect.get_y() + plot_rect.get_height(),
)
}
/// Returns the baseline pixel X corresponding to `0.0` (clamped inside `plot_rect`) for horizontal bar charts.
///
/// # Arguments
///
/// - `&Self` - Horizontal numeric axis scale.
/// - `Rect` - Target plot rectangle.
///
/// # Returns
///
/// - `f32` - Baseline X coordinate in logical pixels.
#[must_use]
pub fn baseline_x(&self, plot_rect: Rect) -> f32 {
let zero_val: f32 = if self.get_axis_type() == EuvChartAxisType::Log {
self.get_min().max(SCALE_LOG_MIN_POSITIVE)
} else {
0.0_f32.clamp(self.get_min(), self.get_max())
};
self.map_x(zero_val, plot_rect)
.clamp(plot_rect.get_x(), plot_rect.get_x() + plot_rect.get_width())
}
/// Returns the continuous visible category slot count for stable candle/bar width computation during panning.
///
/// # Arguments
///
/// - `&Self` - Axis scale reference.
///
/// # Returns
///
/// - `f32` - Positive visible category slot count.
#[must_use]
pub fn visible_slot_count(&self) -> f32 {
let win_span: f32 = self.get_window_span();
if win_span > 1e-4 {
win_span
} else {
(self.get_categories().len().max(1)) as f32
}
}
/// Resolves the category label for a global sample index when inside the active slice.
///
/// # Arguments
///
/// - `&Self` - Category axis scale reference.
/// - `usize` - Global zero-based category index.
///
/// # Returns
///
/// - `Option<&str>` - Matching category label when available.
#[must_use]
pub fn category_label_at(&self, global_idx: usize) -> Option<&str> {
let local_idx: usize = global_idx.checked_sub(self.get_category_base_index())?;
self.get_categories().get(local_idx).map(String::as_str)
}
}
/// Continuous window helpers for `EuvChartZoomSlice`.
impl EuvChartZoomSlice {
/// Returns `true` when the active zoom window covers strictly less than the full `[0%, 100%]` domain.
///
/// # Arguments
///
/// - `&Self` - Zoom slice reference.
///
/// # Returns
///
/// - `bool` - True when zoomed in.
#[must_use]
pub fn is_zoomed(&self) -> bool {
(self.get_end_pct() - self.get_start_pct()) < 99.9
}
/// Returns the padded `[render_start, render_end)` index range covering visible plus one neighbor on each side for smooth clipped panning.
///
/// # Arguments
///
/// - `&Self` - Zoom slice reference.
/// - `usize` - Total series sample count.
///
/// # Returns
///
/// - `(usize, usize)` - Half-open global index range clamped to `[0, total_len]`.
#[must_use]
pub fn render_index_range(&self, total_len: usize) -> (usize, usize) {
if total_len == 0 {
return (0, 0);
}
let s: usize = (self.get_start_f().floor() as usize)
.saturating_sub(1)
.min(total_len.saturating_sub(1));
let e: usize = ((self.get_end_f().ceil() as usize) + 1)
.min(total_len)
.max((s + 1).min(total_len));
(s, e)
}
/// Computes the smooth fractional overlap weight in `[0.0, 1.0]` of sample slot `[idx, idx + 1]` against `[start_f, end_f]`.
///
/// # Arguments
///
/// - `&Self` - Zoom slice reference.
/// - `usize` - Global zero-based sample index.
///
/// # Returns
///
/// - `f32` - Smoothstep visibility weight in `[0.0, 1.0]`.
#[must_use]
pub fn sample_visibility_weight(&self, idx: usize) -> f32 {
let a: f32 = idx as f32;
let b: f32 = a + 1.0;
let overlap: f32 = (b.min(self.get_end_f()) - a.max(self.get_start_f())).clamp(0.0, 1.0);
overlap * overlap * (3.0 - 2.0 * overlap)
}
}
/// Color interpolation methods for `EuvChartVisualEvaluator`.
impl EuvChartVisualEvaluator {
/// Resolves a numeric `value` into its mapped `Color` under `Continuous` or `Piecewise` rules.
///
/// # Arguments
///
/// - `&Self` - Visual-map evaluator.
/// - `f32` - Scalar data value.
///
/// # Returns
///
/// - `Color` - Interpolated or bucketed RGBA color.
#[must_use]
pub fn map_color(&self, value: f32) -> Color {
if self.get_kind() == EuvChartVisualMapKind::Piecewise {
for piece in self.get_pieces().iter() {
let ge_min: bool = piece.min_opt().is_none_or(|lo: f32| value >= lo);
let le_max: bool = piece.max_opt().is_none_or(|hi: f32| value <= hi);
if ge_min && le_max {
return piece.get_color();
}
}
if let Some(last) = self.get_pieces().last() {
return last.get_color();
}
}
let stops: &[Color] = self.get_stops().as_slice();
if stops.is_empty() {
return Color::hex(ECHART_PALETTE_LIGHT[0]);
}
if stops.len() == 1 {
return stops[0];
}
let span: f32 = (self.get_max() - self.get_min()).max(1e-6);
let t: f32 = ((value - self.get_min()) / span).clamp(0.0, 1.0);
let scaled: f32 = t * ((stops.len() - 1) as f32);
let idx: usize = (scaled.floor() as usize).min(stops.len() - 2);
let local_t: f32 = (scaled - (idx as f32)).clamp(0.0, 1.0);
stops[idx].lerp(stops[idx + 1], local_t)
}
}