charton 0.5.2

A high-performance, layered charting system for Rust, featuring a flexible data core and multi-backend rendering.
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
use super::backend::svg::SvgBackend;
use crate::Precision;
use crate::core::context::PanelContext;
use crate::core::guide::{GuideKind, GuideSize, GuideSpec, LegendPosition};
use crate::core::layer::{
    CircleConfig, GradientRectConfig, LineConfig, PolygonConfig, RectConfig, RenderBackend,
    TextConfig,
};
use crate::scale::ScaleDomain;
use crate::scale::mapper::VisualMapper;
use crate::theme::Theme;
use crate::visual::color::SingleColor;
use crate::visual::shape::PointShape;

/// LegendRenderer translates abstract GuideSpecs into visual SVG representations.
///
/// It operates globally on the chart canvas but uses a PanelContext to anchor itself
/// relative to the primary plotting area.
pub struct LegendRenderer;

impl LegendRenderer {
    /// The primary entry point for rendering all legends and colorbars.
    ///
    /// It coordinates the layout flow (wrapping blocks) based on the available space
    /// around the provided PanelContext.
    pub fn render_legend(
        buffer: &mut String,
        specs: &[GuideSpec],
        theme: &Theme,
        ctx: &PanelContext,
    ) {
        // Resolve the legend position from the theme.
        let position = theme.legend_position;

        if specs.is_empty() || matches!(position, LegendPosition::None) {
            return;
        }

        let mut backend = SvgBackend::new(buffer, None);
        let font_size = theme.legend_label_size;
        let font_family = &theme.legend_label_family;

        // Layout orientation: Top/Bottom positions are horizontal; Left/Right are vertical.
        let is_horizontal = matches!(position, LegendPosition::Top | LegendPosition::Bottom);

        // Determine starting coordinates relative to the panel's bounding box.
        let (start_x, start_y) = Self::calculate_initial_anchor(ctx, theme, is_horizontal);

        let mut current_x = start_x;
        let mut current_y = start_y;
        let mut max_dim_in_row_col = 0.0;
        let block_gap = theme.legend_block_gap;

        for spec in specs {
            // Estimate size for wrapping calculations.
            // In faceted plots, we typically use the full height of the plot area.
            let block_size = spec.estimate_size(
                theme,
                if is_horizontal {
                    150.0
                } else {
                    ctx.panel.height
                },
            );

            // --- Macro-Layout Wrapping ---
            // If the next legend block exceeds the panel's bounds, wrap to a new row/column.
            if !is_horizontal {
                if current_y + block_size.height > start_y + ctx.panel.height && current_y > start_y
                {
                    current_x += max_dim_in_row_col + block_gap;
                    current_y = start_y;
                    max_dim_in_row_col = block_size.width;
                } else {
                    max_dim_in_row_col = f64::max(max_dim_in_row_col, block_size.width);
                }
            } else if current_x + block_size.width > start_x + ctx.panel.width
                && current_x > start_x
            {
                current_y += max_dim_in_row_col + block_gap;
                current_x = start_x;
                max_dim_in_row_col = block_size.height;
            } else {
                max_dim_in_row_col = f64::max(max_dim_in_row_col, block_size.height);
            }

            // 1. Draw Legend Block Title
            let text_config = TextConfig {
                text: spec.title.clone(),
                x: current_x as Precision,
                y: (current_y + (font_size * 0.8)) as Precision,
                font_size: (font_size * 1.1) as Precision,
                font_family: font_family.clone(),
                color: theme.title_color,
                text_anchor: "start".to_string(),
                font_weight: "bold".to_string(),
                opacity: 1.0,
            };
            backend.draw_text(text_config);

            let content_y_offset = current_y + (font_size * 1.1) + theme.legend_title_gap;

            // 2. Render content based on GuideKind (Continuous Gradient vs. Discrete Symbols)
            let actual_block_size = match spec.kind {
                GuideKind::ColorBar => {
                    Self::draw_colorbar(&mut backend, spec, ctx, current_x, content_y_offset, theme)
                }
                GuideKind::Legend => {
                    let (labels, colors, shapes, sizes) = Self::resolve_mappings(spec, ctx);
                    Self::draw_spec_group(
                        &mut backend,
                        spec,
                        &labels,
                        &colors,
                        shapes.as_deref(),
                        sizes.as_deref(),
                        current_x,
                        content_y_offset,
                        font_size,
                        theme,
                        if is_horizontal {
                            150.0
                        } else {
                            ctx.panel.height
                        },
                    )
                }
            };

            // 3. Advance the cursor
            if !is_horizontal {
                current_y += actual_block_size.height + block_gap;
            } else {
                current_x += actual_block_size.width + block_gap;
            }
        }
    }

    /// Renders a continuous color gradient bar (ColorBar).
    fn draw_colorbar(
        backend: &mut dyn RenderBackend,
        spec: &GuideSpec,
        ctx: &PanelContext,
        x: f64,
        y: f64,
        theme: &Theme,
    ) -> GuideSize {
        let bar_w = 15.0;
        let bar_h = 150.0;
        let font_size = theme.legend_label_size;
        let font_family = &theme.legend_label_family;

        let mut stops = Vec::new();

        // Access the color aesthetics from the central spec
        if let Some(ref mapping) = ctx.spec.aesthetics.color
            && let Some(mapper) = mapping.scale_impl.mapper()
        {
            let n_samples = 15;
            let l_max = mapping.scale_impl.logical_max();

            for i in 0..=n_samples {
                let ratio = i as f64 / n_samples as f64;
                // Reverse sampling so higher values appear at the top.
                let color = mapper.map_to_color(1.0 - ratio, l_max);
                stops.push((ratio as Precision, color));
            }
        }

        let gradient_rect_config = GradientRectConfig {
            x: x as Precision,
            y: y as Precision,
            width: bar_w as Precision,
            height: bar_h as Precision,
            stops,
            is_vertical: true,
            id_suffix: spec.field.clone(),
        };
        backend.draw_gradient_rect(gradient_rect_config);

        let rect_config = RectConfig {
            x: x as Precision,
            y: y as Precision,
            width: bar_w as Precision,
            height: bar_h as Precision,
            fill: SingleColor::new("none"),
            stroke: theme.title_color,
            stroke_width: 1.0,
            opacity: 1.0,
        };
        backend.draw_rect(rect_config);

        let mut max_label_w = 0.0;
        if let Some(mapping) = spec.mappings.first() {
            let ticks = mapping.scale_impl.suggest_ticks(5);
            for tick in ticks {
                let norm = mapping.scale_impl.normalize(tick.value);
                let tick_y = y + (bar_h * (1.0 - norm));

                let line_config = LineConfig {
                    x1: x as Precision,
                    y1: tick_y as Precision,
                    x2: (x + 3.0) as Precision,
                    y2: tick_y as Precision,
                    color: "#FFFFFF".into(),
                    width: 1.0,
                    opacity: 1.0,
                    dash: vec![],
                };
                backend.draw_line(line_config);

                let line_config = LineConfig {
                    x1: (x + bar_w - 3.0) as Precision,
                    y1: tick_y as Precision,
                    x2: (x + bar_w) as Precision,
                    y2: tick_y as Precision,
                    color: "#FFFFFF".into(),
                    width: 1.0,
                    opacity: 1.0,
                    dash: vec![],
                };
                backend.draw_line(line_config);

                let text_config = TextConfig {
                    text: tick.label.clone(),
                    x: (x + bar_w + theme.legend_marker_text_gap) as Precision,
                    y: (tick_y + font_size * 0.3) as Precision,
                    font_size: font_size as Precision,
                    font_family: font_family.clone(),
                    color: theme.legend_label_color,
                    text_anchor: "start".to_string(),
                    font_weight: "normal".to_string(),
                    opacity: 1.0,
                };
                backend.draw_text(text_config);

                let lw = crate::core::utils::estimate_text_width(&tick.label, font_size);
                max_label_w = f64::max(max_label_w, lw);
            }
        }

        GuideSize {
            width: bar_w + theme.legend_marker_text_gap + max_label_w,
            height: bar_h,
        }
    }

    /// Renders a group of categorical symbols and labels.
    #[allow(clippy::too_many_arguments)]
    fn draw_spec_group(
        backend: &mut dyn RenderBackend,
        _spec: &GuideSpec,
        labels: &[String],
        colors: &[SingleColor],
        shapes: Option<&[PointShape]>,
        sizes: Option<&[f64]>,
        x: f64,
        y: f64,
        font_size: f64,
        theme: &Theme,
        max_h: f64,
    ) -> GuideSize {
        let mut col_x = x;
        let mut item_y = y;
        let mut current_col_w = 0.0;
        let mut total_w = 0.0;

        let font_family = &theme.legend_label_family;
        let fixed_container_size = 18.0;

        for (i, label) in labels.iter().enumerate() {
            let r = sizes.and_then(|s| s.get(i)).cloned().unwrap_or(5.0);
            let text_w = crate::core::utils::estimate_text_width(label, font_size);
            let row_w = fixed_container_size + theme.legend_marker_text_gap + text_w;
            let row_h = f64::max(fixed_container_size, font_size);

            if item_y + row_h > y + max_h && i > 0 {
                total_w += current_col_w + theme.legend_col_h_gap;
                col_x += current_col_w + theme.legend_col_h_gap;
                item_y = y;
                current_col_w = row_w;
            } else {
                current_col_w = f64::max(current_col_w, row_w);
            }

            let shape = shapes.and_then(|s| s.get(i)).unwrap_or(&PointShape::Circle);

            Self::draw_symbol(
                backend,
                shape,
                col_x + (fixed_container_size / 2.0),
                item_y + (row_h / 2.0),
                r,
                colors.get(i).unwrap_or(&"#333333".into()),
            );

            let text_config = TextConfig {
                text: label.clone(),
                x: (col_x + fixed_container_size + theme.legend_marker_text_gap) as Precision,
                y: (item_y + row_h * 0.75) as Precision,
                font_size: font_size as Precision,
                font_family: font_family.clone(),
                color: theme.legend_label_color,
                text_anchor: "start".to_string(),
                font_weight: "normal".to_string(),
                opacity: 1.0,
            };
            backend.draw_text(text_config);

            item_y += row_h + theme.legend_item_v_gap;
        }

        GuideSize {
            width: total_w + current_col_w,
            height: if total_w > 0.0 { max_h } else { item_y - y },
        }
    }

    /// Maps data values into visual properties using the GlobalAesthetics context.
    #[allow(clippy::type_complexity)] // A type alias isn't needed for a single usage.
    fn resolve_mappings(
        spec: &GuideSpec,
        ctx: &PanelContext,
    ) -> (
        Vec<String>,
        Vec<SingleColor>,
        Option<Vec<PointShape>>,
        Option<Vec<f64>>,
    ) {
        let (labels, values_f64): (Vec<String>, Vec<f64>) = match &spec.domain {
            ScaleDomain::Discrete(values) => (values.clone(), Vec::new()),
            _ => {
                let ticks = spec.get_sampling_ticks();
                let l = ticks.iter().map(|t| t.label.clone()).collect();
                let v = ticks.iter().map(|t| t.value).collect();
                (l, v)
            }
        };

        let mut colors = Vec::new();
        let mut shapes = Vec::new();
        let mut sizes = Vec::new();

        // Check availability of specific mappers
        let has_color = spec.mappings.iter().any(|m| {
            m.scale_impl.mapper().is_some_and(|v| {
                matches!(
                    v,
                    VisualMapper::DiscreteColor { .. } | VisualMapper::ContinuousColor { .. }
                )
            })
        });
        let has_shape = spec.mappings.iter().any(|m| {
            m.scale_impl
                .mapper()
                .is_some_and(|v| matches!(v, VisualMapper::Shape { .. }))
        });
        let has_size = spec.mappings.iter().any(|m| {
            m.scale_impl
                .mapper()
                .is_some_and(|v| matches!(v, VisualMapper::Size { .. }))
        });

        for (i, label_str) in labels.iter().enumerate() {
            let val_f64 = values_f64.get(i).cloned();

            // Resolve Color
            if has_color {
                if let Some(ref mapping) = ctx.spec.aesthetics.color {
                    let norm = val_f64
                        .map(|v| mapping.scale_impl.normalize(v))
                        .unwrap_or_else(|| mapping.scale_impl.normalize_string(label_str));

                    let color = mapping
                        .scale_impl
                        .mapper()
                        .map(|m| m.map_to_color(norm, mapping.scale_impl.logical_max()))
                        .unwrap_or_else(|| "#333333".into());
                    colors.push(color);
                }
            } else {
                colors.push("#333333".into());
            }

            // Resolve Shape
            if has_shape {
                if let Some(ref mapping) = ctx.spec.aesthetics.shape {
                    let norm = val_f64
                        .map(|v| mapping.scale_impl.normalize(v))
                        .unwrap_or_else(|| mapping.scale_impl.normalize_string(label_str));

                    let shape = mapping
                        .scale_impl
                        .mapper()
                        .map(|m| m.map_to_shape(norm, mapping.scale_impl.logical_max()))
                        .unwrap_or(PointShape::Circle);
                    shapes.push(shape);
                }
            } else {
                shapes.push(PointShape::Circle);
            }

            // Resolve Size
            if has_size {
                if let Some(ref mapping) = ctx.spec.aesthetics.size {
                    let norm = val_f64
                        .map(|v| mapping.scale_impl.normalize(v))
                        .unwrap_or_else(|| mapping.scale_impl.normalize_string(label_str));

                    let size = mapping
                        .scale_impl
                        .mapper()
                        .map(|m| m.map_to_size(norm))
                        .unwrap_or(5.0);
                    sizes.push(size);
                }
            } else {
                sizes.push(5.0);
            }
        }

        (
            labels,
            colors,
            if has_shape { Some(shapes) } else { None },
            if has_size { Some(sizes) } else { None },
        )
    }

    fn draw_symbol(
        backend: &mut dyn RenderBackend,
        shape: &PointShape,
        cx: f64,
        cy: f64,
        r: f64,
        color: &SingleColor,
    ) {
        match shape {
            PointShape::Circle => {
                let circle_config = CircleConfig {
                    x: cx as Precision,
                    y: cy as Precision,
                    radius: r as Precision,
                    fill: *color,
                    stroke: SingleColor::new("none"),
                    stroke_width: 0.0,
                    opacity: 1.0,
                };
                backend.draw_circle(circle_config);
            }
            PointShape::Square => {
                let rect_config = RectConfig {
                    x: (cx - r) as Precision,
                    y: (cy - r) as Precision,
                    width: (r * 2.0) as Precision,
                    height: (r * 2.0) as Precision,
                    fill: *color,
                    stroke: SingleColor::new("none"),
                    stroke_width: 0.0,
                    opacity: 1.0,
                };
                backend.draw_rect(rect_config);
            }
            PointShape::Triangle => {
                let pts = vec![
                    (cx as Precision, (cy - r) as Precision),
                    ((cx - r) as Precision, (cy + r) as Precision),
                    ((cx + r) as Precision, (cy + r) as Precision),
                ];
                let polygon_config = PolygonConfig {
                    points: pts,
                    fill: *color,
                    stroke: SingleColor::new("none"),
                    stroke_width: 0.0,
                    fill_opacity: 1.0,
                    stroke_opacity: 1.0,
                };
                backend.draw_polygon(polygon_config);
            }
            PointShape::Diamond => {
                let pts = vec![
                    (cx as Precision, (cy - r) as Precision),
                    ((cx + r) as Precision, cy as Precision),
                    (cx as Precision, (cy + r) as Precision),
                    ((cx - r) as Precision, cy as Precision),
                ];
                let polygon_config = PolygonConfig {
                    points: pts,
                    fill: *color,
                    stroke: SingleColor::new("none"),
                    stroke_width: 0.0,
                    fill_opacity: 1.0,
                    stroke_opacity: 1.0,
                };
                backend.draw_polygon(polygon_config);
            }
            _ => {
                let circle_config = CircleConfig {
                    x: cx as Precision,
                    y: cy as Precision,
                    radius: r as Precision,
                    fill: *color,
                    stroke: SingleColor::new("none"),
                    stroke_width: 0.0,
                    opacity: 1.0,
                };
                backend.draw_circle(circle_config);
            }
        }
    }

    /// Calculates the initial (x, y) anchor for the legend block based on the panel position.
    fn calculate_initial_anchor(ctx: &PanelContext, theme: &Theme, _: bool) -> (f64, f64) {
        let mut x = ctx.panel.x;
        let mut y = ctx.panel.y;
        let margin = theme.legend_margin;
        let axis_buffer = theme.axis_reserve_buffer;

        match theme.legend_position {
            LegendPosition::Right => x = ctx.panel.x + ctx.panel.width + margin,
            LegendPosition::Left => x = (ctx.panel.x - margin - axis_buffer).max(10.0),
            LegendPosition::Top => y = (ctx.panel.y - margin - (axis_buffer * 0.8)).max(10.0),
            LegendPosition::Bottom => y = ctx.panel.y + ctx.panel.height + margin,
            _ => {}
        }
        (x, y)
    }
}