chartml-chart-scatter 3.0.0

ChartML scatter and bubble chart renderers
Documentation
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use chartml_core::plugin::{ChartRenderer, ChartConfig};
use chartml_core::data::DataTable;
use chartml_core::element::*;
use chartml_core::error::ChartError;
use chartml_core::scales::{ScaleLinear, ScaleSqrt};
use chartml_core::spec::{VisualizeSpec, FieldRef, MarkEncoding};
use chartml_core::layout::margins::Margins;
use chartml_core::layout::legend::{LegendMark, LegendConfig, calculate_legend_layout, generate_legend_elements};

pub struct ScatterRenderer;

impl ScatterRenderer {
    pub fn new() -> Self {
        Self
    }
}

impl Default for ScatterRenderer {
    fn default() -> Self {
        Self::new()
    }
}

impl ChartRenderer for ScatterRenderer {
    fn render(&self, data: &DataTable, config: &ChartConfig) -> Result<ChartElement, ChartError> {
        let x_field = get_field_name(&config.visualize.columns)?;
        let y_field = get_field_name(&config.visualize.rows)?;
        let color_field = get_color_field(config);
        let size_field = get_size_field(config);

        let width = config.width;
        let height = config.height;

        let has_legend = color_field.is_some();
        let margins = if has_legend {
            // Add 30px bottom margin for the legend row
            Margins::new(30.0, 20.0, 70.0, 60.0)
        } else {
            Margins::default()
        };
        let inner_width = margins.inner_width(width);
        let inner_height = margins.inner_height(height);

        // Compute domains
        let x_extent = data.extent(&x_field)
            .ok_or_else(|| ChartError::DataError(format!("No numeric data for field '{}'", x_field)))?;
        let y_extent = data.extent(&y_field)
            .ok_or_else(|| ChartError::DataError(format!("No numeric data for field '{}'", y_field)))?;

        // Use actual data extent for axis domains so tightly-clustered data
        // fills the plot area instead of being crammed near a forced zero origin.
        let x_domain = (x_extent.0, x_extent.1);
        let y_domain = (y_extent.0, y_extent.1);
        let x_scale = ScaleLinear::new(x_domain, (margins.left, margins.left + inner_width)).nice(5);
        let y_scale = ScaleLinear::new(y_domain, (margins.top + inner_height, margins.top)).nice(5); // inverted for SVG

        // Size scale (if marks.size present)
        let size_scale = size_field.as_ref().and_then(|f| {
            data.extent(f).map(|ext| ScaleSqrt::new(ext, (3.0, 20.0))) // radius 3-20px
        });

        // Color mapping
        let color_categories: Vec<String> = if let Some(ref cf) = color_field {
            data.unique_values(cf)
        } else {
            vec![]
        };

        // Generate scatter points
        let mut point_elements = Vec::new();
        for i in 0..data.num_rows() {
            let x_val = data.get_f64(i, &x_field);
            let y_val = data.get_f64(i, &y_field);

            if let (Some(x), Some(y)) = (x_val, y_val) {
                let cx = x_scale.map(x);
                let cy = y_scale.map(y);

                let r = match (&size_field, &size_scale) {
                    (Some(sf), Some(ss)) => {
                        data.get_f64(i, sf).map(|v| ss.map(v)).unwrap_or(5.0)
                    }
                    _ => 5.0,
                };

                let color_idx = if let Some(ref cf) = color_field {
                    data.get_string(i, cf)
                        .and_then(|v| color_categories.iter().position(|c| c == &v))
                        .unwrap_or(0)
                } else {
                    0
                };
                let fill = config.colors.get(color_idx % config.colors.len())
                    .cloned()
                    .unwrap_or_else(|| "#2E7D9A".to_string());

                let label = data.get_string(i, &x_field).unwrap_or_default();
                let value = format!("{}", y);
                let el_data = ElementData::new(label, value);

                point_elements.push(ChartElement::Circle {
                    cx,
                    cy,
                    r,
                    fill,
                    stroke: Some("#fff".to_string()),
                    class: "chartml-scatter-point".to_string(),
                    data: Some(el_data),
                });
            }
        }

        // Build SVG
        let mut children = Vec::new();

        // Grid lines + axes
        let x_ticks = x_scale.ticks(((inner_width / 50.0).floor() as usize).clamp(4, 10));
        let y_ticks = y_scale.ticks(((inner_height / 50.0).floor() as usize).clamp(4, 10));
        let mut axis_elements = Vec::new();

        // Compute tick steps for formatting
        let y_tick_step = compute_tick_step(&y_ticks);
        let x_tick_step = compute_tick_step(&x_ticks);

        // Horizontal grid lines + y-axis ticks
        for &val in &y_ticks {
            let y = y_scale.map(val);
            // Grid line
            axis_elements.push(ChartElement::Line {
                x1: margins.left, y1: y, x2: margins.left + inner_width, y2: y,
                stroke: "#e0e0e0".to_string(), stroke_width: Some(1.0),
                stroke_dasharray: None, class: "grid-line".to_string(),
            });
            // Tick
            axis_elements.push(ChartElement::Line {
                x1: margins.left - 5.0, y1: y, x2: margins.left, y2: y,
                stroke: "#999".to_string(), stroke_width: Some(1.0),
                stroke_dasharray: None, class: "tick".to_string(),
            });
            // Label
            let label = format_tick_value(val, y_tick_step);
            axis_elements.push(ChartElement::Text {
                x: margins.left - 8.0, y,
                content: label, anchor: TextAnchor::End,
                dominant_baseline: Some("middle".to_string()),
                transform: None, font_size: Some("11px".to_string()),
                font_weight: None,
                fill: Some("#666".to_string()), class: "tick-label".to_string(), data: None,
            });
        }

        // Vertical grid lines + x-axis ticks
        let x_axis_y = margins.top + inner_height;
        for &val in &x_ticks {
            let x = x_scale.map(val);
            // Grid line
            axis_elements.push(ChartElement::Line {
                x1: x, y1: margins.top, x2: x, y2: x_axis_y,
                stroke: "#e0e0e0".to_string(), stroke_width: Some(1.0),
                stroke_dasharray: None, class: "grid-line".to_string(),
            });
            // Tick
            axis_elements.push(ChartElement::Line {
                x1: x, y1: x_axis_y, x2: x, y2: x_axis_y + 5.0,
                stroke: "#999".to_string(), stroke_width: Some(1.0),
                stroke_dasharray: None, class: "tick".to_string(),
            });
            // Label
            let label = format_tick_value(val, x_tick_step);
            axis_elements.push(ChartElement::Text {
                x, y: x_axis_y + 18.0,
                content: label, anchor: TextAnchor::Middle,
                dominant_baseline: None, transform: None,
                font_size: Some("11px".to_string()), font_weight: None,
                fill: Some("#666".to_string()),
                class: "tick-label".to_string(), data: None,
            });
        }

        // Axis lines
        axis_elements.push(ChartElement::Line {
            x1: margins.left, y1: margins.top, x2: margins.left, y2: x_axis_y,
            stroke: "#ccc".to_string(), stroke_width: Some(1.0),
            stroke_dasharray: None, class: "axis-line".to_string(),
        });
        axis_elements.push(ChartElement::Line {
            x1: margins.left, y1: x_axis_y, x2: margins.left + inner_width, y2: x_axis_y,
            stroke: "#ccc".to_string(), stroke_width: Some(1.0),
            stroke_dasharray: None, class: "axis-line".to_string(),
        });

        children.push(ChartElement::Group {
            class: "axes".to_string(),
            transform: None,
            children: axis_elements,
        });

        // Title is rendered as HTML outside the SVG — not added here.

        // Points group
        children.push(ChartElement::Group {
            class: "chartml-scatter-points".to_string(),
            transform: None,
            children: point_elements,
        });

        // Legend
        if let Some(ref cf) = color_field {
            let series_names = data.unique_values(cf);
            if series_names.len() > 1 {
                let legend_config = LegendConfig::default();
                let legend_layout = calculate_legend_layout(&series_names, &config.colors, width, &legend_config);
                let legend_y = height - legend_layout.total_height - 8.0;
                let legend_elements = generate_legend_elements(
                    &series_names,
                    &config.colors,
                    width,
                    legend_y,
                    LegendMark::Circle,
                );
                children.push(ChartElement::Group {
                    class: "legend".to_string(),
                    transform: None,
                    children: legend_elements,
                });
            }
        }

        Ok(ChartElement::Svg {
            viewbox: ViewBox::new(0.0, 0.0, width, height),
            width: Some(width),
            height: Some(height),
            class: "chartml-chart chartml-scatter-chart".to_string(),
            children,
        })
    }

    fn default_dimensions(&self, _spec: &VisualizeSpec) -> Option<Dimensions> {
        Some(Dimensions::new(400.0))
    }
}

/// Extract the field name from an optional FieldRef.
fn get_field_name(field_ref: &Option<FieldRef>) -> Result<String, ChartError> {
    match field_ref {
        Some(FieldRef::Simple(name)) => Ok(name.clone()),
        Some(FieldRef::Detailed(spec)) => Ok(spec.field.clone()),
        Some(FieldRef::Multiple(items)) => {
            // Use the first item
            match items.first() {
                Some(chartml_core::spec::FieldRefItem::Simple(name)) => Ok(name.clone()),
                Some(chartml_core::spec::FieldRefItem::Detailed(spec)) => Ok(spec.field.clone()),
                None => Err(ChartError::InvalidSpec("Empty field reference list".into())),
            }
        }
        None => Err(ChartError::InvalidSpec("Missing required field reference".into())),
    }
}

/// Extract the color field name from marks.color encoding, if present.
fn get_color_field(config: &ChartConfig) -> Option<String> {
    config.visualize.marks.as_ref().and_then(|marks| {
        marks.color.as_ref().map(|enc| match enc {
            MarkEncoding::Simple(name) => name.clone(),
            MarkEncoding::Detailed(spec) => spec.field.clone(),
        })
    })
}

/// Extract the size field name from marks.size encoding, if present.
fn get_size_field(config: &ChartConfig) -> Option<String> {
    config.visualize.marks.as_ref().and_then(|marks| {
        marks.size.as_ref().map(|enc| match enc {
            MarkEncoding::Simple(name) => name.clone(),
            MarkEncoding::Detailed(spec) => spec.field.clone(),
        })
    })
}

/// Compute the tick step from a slice of ticks.
fn compute_tick_step(ticks: &[f64]) -> f64 {
    if ticks.len() >= 2 {
        (ticks[1] - ticks[0]).abs()
    } else {
        1.0
    }
}

/// Format a numeric value for use as an axis tick label, with comma separators.
///
/// Mirrors the D3-style `format_tick_value` from the cartesian helpers: computes
/// decimal precision from the tick step and inserts commas into the integer part.
fn format_tick_value(value: f64, tick_step: f64) -> String {
    // D3's precisionFixed(step): max(0, -floor(log10(abs(step))))
    let precision = if tick_step.abs() < 1e-15 {
        0usize
    } else {
        let p = -(tick_step.abs().log10().floor()) as i64;
        p.max(0) as usize
    };

    let formatted = format!("{:.prec$}", value, prec = precision);

    // Split on decimal point
    let (int_part, dec_part) = if let Some(dot_pos) = formatted.find('.') {
        (&formatted[..dot_pos], Some(&formatted[dot_pos..]))
    } else {
        (formatted.as_str(), None)
    };

    // Handle negative sign
    let (sign, digits) = if let Some(stripped) = int_part.strip_prefix('-') {
        ("-", stripped)
    } else {
        ("", int_part)
    };

    let with_commas = insert_commas(digits);

    match dec_part {
        Some(dec) => format!("{}{}{}", sign, with_commas, dec),
        None => format!("{}{}", sign, with_commas),
    }
}

/// Insert comma separators into a string of digits.
fn insert_commas(digits: &str) -> String {
    let len = digits.len();
    if len <= 3 {
        return digits.to_string();
    }
    let mut result = String::with_capacity(len + len / 3);
    for (i, ch) in digits.chars().enumerate() {
        if i > 0 && (len - i).is_multiple_of(3) {
            result.push(',');
        }
        result.push(ch);
    }
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use chartml_core::data::Row;
    use chartml_core::spec::{VisualizeSpec, MarksSpec, MarkEncoding};

    fn make_row(pairs: &[(&str, serde_json::Value)]) -> Row {
        let mut map = HashMap::new();
        for (k, v) in pairs {
            map.insert(k.to_string(), v.clone());
        }
        map
    }

    fn make_scatter_data() -> DataTable {
        let rows = vec![
            make_row(&[("price", serde_json::json!(10.0)), ("units", serde_json::json!(100.0)), ("category", serde_json::json!("A"))]),
            make_row(&[("price", serde_json::json!(20.0)), ("units", serde_json::json!(200.0)), ("category", serde_json::json!("B"))]),
            make_row(&[("price", serde_json::json!(30.0)), ("units", serde_json::json!(150.0)), ("category", serde_json::json!("A"))]),
            make_row(&[("price", serde_json::json!(40.0)), ("units", serde_json::json!(300.0)), ("category", serde_json::json!("B"))]),
        ];
        DataTable::from_rows(&rows).unwrap()
    }

    fn make_scatter_config() -> ChartConfig {
        ChartConfig {
            visualize: VisualizeSpec {
                chart_type: "scatter".to_string(),
                mode: None,
                orientation: None,
                columns: Some(FieldRef::Simple("price".to_string())),
                rows: Some(FieldRef::Simple("units".to_string())),
                marks: Some(MarksSpec {
                    color: Some(MarkEncoding::Simple("category".to_string())),
                    size: None,
                    shape: None,
                    text: None,
                }),
                axes: None,
                annotations: None,
                style: None,
                value: None,
                label: None,
                format: None,
                compare_with: None,
                invert_trend: None,
                data_labels: None,
            },
            title: Some("Scatter Test".to_string()),
            width: 800.0,
            height: 400.0,
            colors: vec![
                "#2E7D9A".to_string(),
                "#E8533E".to_string(),
                "#4CAF50".to_string(),
            ],
        }
    }

    fn make_bubble_data() -> DataTable {
        let rows = vec![
            make_row(&[("x", serde_json::json!(5.0)), ("y", serde_json::json!(10.0)), ("size", serde_json::json!(100.0))]),
            make_row(&[("x", serde_json::json!(15.0)), ("y", serde_json::json!(20.0)), ("size", serde_json::json!(400.0))]),
            make_row(&[("x", serde_json::json!(25.0)), ("y", serde_json::json!(15.0)), ("size", serde_json::json!(200.0))]),
        ];
        DataTable::from_rows(&rows).unwrap()
    }

    fn make_bubble_config() -> ChartConfig {
        ChartConfig {
            visualize: VisualizeSpec {
                chart_type: "scatter".to_string(),
                mode: None,
                orientation: None,
                columns: Some(FieldRef::Simple("x".to_string())),
                rows: Some(FieldRef::Simple("y".to_string())),
                marks: Some(MarksSpec {
                    color: None,
                    size: Some(MarkEncoding::Simple("size".to_string())),
                    shape: None,
                    text: None,
                }),
                axes: None,
                annotations: None,
                style: None,
                value: None,
                label: None,
                format: None,
                compare_with: None,
                invert_trend: None,
                data_labels: None,
            },
            title: None,
            width: 600.0,
            height: 400.0,
            colors: vec!["#2E7D9A".to_string()],
        }
    }

    #[test]
    fn scatter_chart_renders() {
        let renderer = ScatterRenderer::new();
        let result = renderer.render(&make_scatter_data(), &make_scatter_config());
        assert!(result.is_ok(), "render failed: {:?}", result.err());
        let element = result.unwrap();
        let circle_count = count_elements(&element, &|e| matches!(e, ChartElement::Circle { .. }));
        assert_eq!(circle_count, 6); // 4 data points + 2 legend circles (categories A, B)
    }

    #[test]
    fn scatter_with_size_encoding() {
        let renderer = ScatterRenderer::new();
        let result = renderer.render(&make_bubble_data(), &make_bubble_config());
        assert!(result.is_ok(), "render failed: {:?}", result.err());
        let element = result.unwrap();
        let circle_count = count_elements(&element, &|e| matches!(e, ChartElement::Circle { .. }));
        assert!(circle_count > 0);
    }

    #[test]
    fn scatter_empty_data_errors() {
        let renderer = ScatterRenderer::new();
        let data = DataTable::from_rows(&Vec::<Row>::new()).unwrap();
        let result = renderer.render(&data, &make_scatter_config());
        assert!(result.is_err());
    }
}