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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
use crate::Precision;
use crate::chart::Chart;
use crate::core::context::PanelContext;
use crate::core::layer::{
CircleConfig, MarkRenderer, PointElementConfig, PolygonConfig, RectConfig, RenderBackend,
};
use crate::core::utils::IntoParallelizable;
use crate::error::ChartonError;
use crate::mark::point::MarkPoint;
use crate::visual::color::SingleColor;
use crate::visual::shape::PointShape;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
// ============================================================================
// MARK RENDERING (High-Performance Parallel Implementation)
// ============================================================================
impl MarkRenderer for Chart<MarkPoint> {
/// Orchestrates the transformation of raw data into visual geometries.
/// Uses group-based parallel processing to ensure deterministic Z-indexing
/// and appearance-based color mapping.
fn render_marks(
&self,
backend: &mut dyn RenderBackend,
context: &PanelContext,
) -> Result<(), ChartonError> {
let df_source = &self.data;
let row_count = df_source.height();
if row_count == 0 {
return Ok(());
}
// --- STEP 1: SPECIFICATION VALIDATION ---
let x_enc = self
.encoding
.x
.as_ref()
.ok_or_else(|| ChartonError::Encoding("X-axis encoding is missing".into()))?;
let y_enc = self
.encoding
.y
.as_ref()
.ok_or_else(|| ChartonError::Encoding("Y-axis encoding is missing".into()))?;
let mark_config = self
.mark
.as_ref()
.ok_or_else(|| ChartonError::Mark("MarkPoint configuration is missing".into()))?;
// --- STEP 2: POSITION & AESTHETIC NORMALIZATION ---
// Vectorized normalization via Dataset columns.
let x_norms = context
.coord
.get_x_scale()
.scale_type()
.normalize_column(context.coord.get_x_scale(), df_source.column(&x_enc.field)?);
let y_norms = context
.coord
.get_y_scale()
.scale_type()
.normalize_column(context.coord.get_y_scale(), df_source.column(&y_enc.field)?);
// Pre-normalize aesthetics if mappings exist.
let color_norms = context.spec.aesthetics.color.as_ref().map(|m| {
let s = m.scale_impl.as_ref();
s.scale_type()
.normalize_column(s, df_source.column(&m.field).unwrap())
});
let size_norms = context.spec.aesthetics.size.as_ref().map(|m| {
let s = m.scale_impl.as_ref();
s.scale_type()
.normalize_column(s, df_source.column(&m.field).unwrap())
});
let shape_norms = context.spec.aesthetics.shape.as_ref().map(|m| {
let s = m.scale_impl.as_ref();
s.scale_type()
.normalize_column(s, df_source.column(&m.field).unwrap())
});
// --- STEP 3: Calculate geometries in parallel. ---
let render_configs: Vec<PointElementConfig> = (0..row_count)
.maybe_into_par_iter()
.filter_map(|i| {
let x_n = x_norms[i]?;
let y_n = y_norms[i]?;
// 1. Position: convert normalized [0,1] to screen pixels.
let (px, py) = context.coord.transform(x_n, y_n, &context.panel);
// 2. Aesthetics - Directly resolved from pre-calculated norms
// The ScaleTrait and VisualMapper already handle discrete vs continuous.
let fill = self.resolve_color_from_value(
color_norms.as_ref().and_then(|n| n[i]),
context,
&mark_config.color,
);
let size = self.resolve_size_from_value(
size_norms.as_ref().and_then(|n| n[i]),
context,
mark_config.size,
);
let shape = self.resolve_shape_from_value(
shape_norms.as_ref().and_then(|n| n[i]),
context,
mark_config.shape,
);
Some(PointElementConfig {
x: px,
y: py,
shape,
size,
fill,
stroke: mark_config.stroke,
stroke_width: mark_config.stroke_width,
opacity: mark_config.opacity,
})
})
.collect();
// --- STEP 4: SEQUENTIAL DRAW DISPATCH ---
// Render the points for this group onto the backend.
for config in render_configs {
self.emit_draw_call(backend, config);
}
Ok(())
}
}
// ============================================================================
// HELPER METHODS & GEOMETRY DISPATCH
// ============================================================================
impl Chart<MarkPoint> {
/// Maps a normalized value to a color using the registered scale mapper.
fn resolve_color_from_value(
&self,
val: Option<f64>,
context: &PanelContext,
fallback: &SingleColor,
) -> SingleColor {
if let (Some(v), Some(mapping)) = (val, &context.spec.aesthetics.color) {
let s_trait = mapping.scale_impl.as_ref();
s_trait
.mapper()
.as_ref()
.map(|m| m.map_to_color(v, s_trait.logical_max()))
.unwrap_or(*fallback)
} else {
*fallback
}
}
/// Maps a normalized value to a point size.
fn resolve_size_from_value(
&self,
val: Option<f64>,
context: &PanelContext,
fallback: f64,
) -> f64 {
if let (Some(v), Some(mapping)) = (val, &context.spec.aesthetics.size) {
mapping
.scale_impl
.mapper()
.as_ref()
.map(|m| m.map_to_size(v))
.unwrap_or(fallback)
} else {
fallback
}
}
/// Maps a normalized value to a specific PointShape.
fn resolve_shape_from_value(
&self,
val: Option<f64>,
context: &PanelContext,
fallback: PointShape,
) -> PointShape {
if let (Some(v), Some(mapping)) = (val, &context.spec.aesthetics.shape) {
let s_trait = mapping.scale_impl.as_ref();
mapping
.scale_impl
.mapper()
.as_ref()
.map(|m| m.map_to_shape(v, s_trait.logical_max()))
.unwrap_or(fallback)
} else {
fallback
}
}
/// Dispatches the appropriate backend draw call for the given PointShape.
fn emit_draw_call(&self, backend: &mut dyn RenderBackend, config: PointElementConfig) {
let PointElementConfig {
x,
y,
shape,
size,
fill,
stroke,
stroke_width,
opacity,
} = config;
match shape {
PointShape::Circle => {
backend.draw_circle(CircleConfig {
x: x as Precision,
y: y as Precision,
radius: size as Precision,
fill,
stroke,
stroke_width: stroke_width as Precision,
opacity: opacity as Precision,
});
}
PointShape::Square => {
let side = size * 2.0;
backend.draw_rect(RectConfig {
x: (x - size) as Precision,
y: (y - size) as Precision,
width: side as Precision,
height: side as Precision,
fill,
stroke,
stroke_width: stroke_width as Precision,
opacity: opacity as Precision,
});
}
_ => {
let (sides, rotation, scale_adj) = match shape {
PointShape::Diamond => (4, 0.0, 1.2),
PointShape::Triangle => (3, -std::f64::consts::FRAC_PI_2, 1.1),
PointShape::Pentagon => (5, -std::f64::consts::FRAC_PI_2, 1.0),
PointShape::Hexagon => (6, 0.0, 1.0),
PointShape::Octagon => (8, std::f64::consts::FRAC_PI_8, 1.0),
_ => (0, 0.0, 0.0),
};
let points = if shape == PointShape::Star {
self.calculate_star(x, y, size * 1.2, size * 0.5, 5)
} else {
self.calculate_polygon(x, y, size * scale_adj, sides, rotation)
};
backend.draw_polygon(PolygonConfig {
points: points
.iter()
.map(|p| (p.0 as Precision, p.1 as Precision))
.collect(),
fill,
stroke,
stroke_width: stroke_width as Precision,
fill_opacity: opacity as Precision,
stroke_opacity: 1.0,
});
}
}
}
fn calculate_polygon(
&self,
cx: f64,
cy: f64,
r: f64,
sides: usize,
rot: f64,
) -> Vec<(f64, f64)> {
(0..sides)
.map(|i| {
let angle = rot + 2.0 * std::f64::consts::PI * (i as f64) / (sides as f64);
(cx + r * angle.cos(), cy + r * angle.sin())
})
.collect()
}
fn calculate_star(
&self,
cx: f64,
cy: f64,
out_r: f64,
in_r: f64,
pts: usize,
) -> Vec<(f64, f64)> {
(0..(pts * 2))
.map(|i| {
let angle =
-std::f64::consts::FRAC_PI_2 + std::f64::consts::PI * (i as f64) / (pts as f64);
let r = if i % 2 == 0 { out_r } else { in_r };
(cx + r * angle.cos(), cy + r * angle.sin())
})
.collect()
}
}