chartlet 0.22.0

Compile structured data into deterministic, accessible static charts
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
//! The SVG of a scene: its root with title, description and stylesheet, the hatch patterns, and
//! every element.

use std::fmt::Write;

use super::{
    escape,
    hooks::{Hooks, emit_data_block, emit_hook},
    number,
    sketch::Sketch,
    styles::{base_style, layer_style, print_stylesheet, scope_stylesheet, type_class},
};
use crate::{
    Styles,
    scene::{Element, Scene, TextAnchor},
    spec::{ChartSpec, ChartType, StrokeStyle, Theme},
};

/// Where a chart's stylesheet goes: all of it inline, only its own declared colors inline with the
/// rest in the shared stylesheet, or all of it inline and resolved for the print variant.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum StyleMode {
    Inline,
    External,
    /// Resolved for renderers outside the browser, see [`print_stylesheet`].
    Print,
}

impl From<Styles> for StyleMode {
    fn from(styles: Styles) -> Self {
        match styles {
            Styles::Inline => Self::Inline,
            Styles::External => Self::External,
        }
    }
}

/// Serializes a laid-out chart with its stylesheet placed as `styles` says.
pub(crate) fn svg(
    scene: &Scene,
    spec: &ChartSpec,
    description: &str,
    id_prefix: &str,
    styles: StyleMode,
    hooks: Hooks,
) -> String {
    let title_id = format!("{id_prefix}-title");
    let description_id = format!("{id_prefix}-description");
    let is_multiples = spec.chart_type == ChartType::Multiples;
    let is_blueprint = spec.theme == Theme::Blueprint;
    let is_dark = spec.theme == Theme::Dark || is_blueprint;
    let mut output = String::new();
    write!(
        output,
        "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{}\" height=\"{}\" viewBox=\"0 0 {} {}\" id=\"{id_prefix}\" role=\"img\" aria-labelledby=\"{title_id} {description_id}\" class=\"chartlet-root{}{}\"{}{}>",
        scene.width,
        scene.height,
        scene.width,
        scene.height,
        match (is_dark, is_multiples) {
            (true, true) => " chartlet-theme-dark chartlet-multiples",
            (true, false) if is_blueprint => " chartlet-theme-dark chartlet-blueprint",
            (true, false) => " chartlet-theme-dark",
            (false, true) => " chartlet-multiples",
            (false, false) => "",
        },
        // The shared stylesheet addresses each chart type by this class.
        if styles == StyleMode::External {
            format!(" {}", type_class(spec.chart_type))
        } else {
            String::new()
        },
        if hooks == Hooks::Off {
            String::new()
        } else {
            format!(
                " data-chartlet-type=\"{}\" data-chartlet-id=\"{id_prefix}\" data-chartlet-locale=\"{}\"",
                crate::spec::type_name(spec.chart_type),
                spec.locale.code()
            )
        },
        // Stretched stripes take whatever box the page gives them.
        if spec.stripes.as_ref().is_some_and(|stripes| stripes.stretch) {
            " preserveAspectRatio=\"none\""
        } else {
            ""
        }
    )
    .expect("writing to String cannot fail");
    let print = styles == StyleMode::Print;
    let stylesheet = match styles {
        StyleMode::Inline => base_style(spec, false) + &layer_style(spec),
        StyleMode::External => layer_style(spec),
        StyleMode::Print => print_stylesheet(
            &(base_style(spec, true) + &layer_style(spec)),
            (is_dark, is_blueprint),
            &spec.tokens,
        ),
    };
    // The tokens of the chart come last and are addressed by ID: they beat the dark palette's
    // doubled class, and a host page's rule is the chart's neighbor, not its override.
    let tokens = if print || spec.tokens.is_empty() {
        String::new()
    } else {
        format!(
            "#{id_prefix}{{{}}}",
            crate::theme::declarations(&spec.tokens)
        )
    };
    write!(
        output,
        "<title id=\"{title_id}\">{}</title><desc id=\"{description_id}\">{}</desc>",
        escape(&spec.title),
        escape(description),
    )
    .expect("writing to String cannot fail");
    let typography = spec
        .typography
        .as_ref()
        .map_or_else(String::new, |typography| typography.css(id_prefix));
    if !stylesheet.is_empty() || !tokens.is_empty() || !typography.is_empty() {
        write!(
            output,
            "<style>{}{tokens}{typography}</style>",
            scope_stylesheet(&stylesheet, &format!("#{id_prefix}"), print),
        )
        .expect("writing to String cannot fail");
    }
    if hooks == Hooks::WithData {
        emit_data_block(spec, &mut output);
    }
    emit_hatches(spec, id_prefix, &mut output);
    emit_phase_patterns(spec, id_prefix, &mut output);
    crate::symbols::emit_defs(scene, id_prefix, &mut output);

    // A dark chart cannot know the color of the page behind it, so it paints its own surface;
    // so does a chart that names its background.
    if is_dark || spec.tokens.contains_key("background") {
        write!(
            output,
            "<rect class=\"chartlet-background\" x=\"0\" y=\"0\" width=\"{}\" height=\"{}\"/>",
            scene.width, scene.height
        )
        .expect("writing to String cannot fail");
    }
    let hook_spec = (hooks != Hooks::Off).then_some(spec);
    // The print variant and PNG draw clean strokes.
    let mut sketch = (spec.stroke == StrokeStyle::Sketch && !print).then(|| Sketch::new(spec));
    for element in &scene.elements {
        emit_element(element, id_prefix, hook_spec, sketch.as_mut(), &mut output);
    }
    output.push_str("</svg>");
    output
}

/// The hatch patterns a chart refers to: one per modeled band, drawn in the color of its line,
/// and one for the modeled spans of a range chart. Pattern content cannot inherit the color of
/// the shape that uses it, which is why every band has its own.
fn emit_hatches(spec: &ChartSpec, id_prefix: &str, output: &mut String) {
    let mut patterns = Vec::new();
    for entry in spec.data_layers() {
        if entry.layer.modeled && entry.layer.has_band() {
            let class = if entry.layer.resolved_color().is_some() {
                format!("chartlet-style-{}", entry.global)
            } else {
                format!(
                    "chartlet-line-series-{}",
                    spec.palette_index(entry.layer) + 1
                )
            };
            patterns.push((format!("hatch-{}", entry.global), class));
        }
    }
    if spec.chart_type == ChartType::Rangebar && spec.ranges.iter().any(|range| range.modeled) {
        patterns.push(("hatch-range".to_owned(), String::new()));
    }
    if patterns.is_empty() {
        return;
    }
    output.push_str("<defs>");
    for (suffix, class) in patterns {
        let class = if class.is_empty() {
            String::new()
        } else {
            format!(" {class}")
        };
        write!(
            output,
            "<pattern id=\"{id_prefix}-{suffix}\" width=\"6\" height=\"6\" patternUnits=\"userSpaceOnUse\" patternTransform=\"rotate(45)\"><line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"6\" class=\"chartlet-hatch-line{class}\"/></pattern>"
        )
        .expect("writing to String cannot fail");
    }
    output.push_str("</defs>");
}

/// Writes a scene element into the SVG with optional `data-series` and `<title>` child.
/// The patterns of the phases of a plan after the first: diagonal lines, dots and a grid, in
/// the color of the text, so that a phase reads without color.
fn emit_phase_patterns(spec: &ChartSpec, id_prefix: &str, output: &mut String) {
    let phases = spec.plan.as_ref().map_or(0, |plan| plan.phases().len());
    if phases < 2 {
        return;
    }
    output.push_str("<defs>");
    for (number, content) in [
        (1, "<line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"8\" class=\"chartlet-plan-mark\"/>"),
        (2, "<circle cx=\"4\" cy=\"4\" r=\"1.4\" class=\"chartlet-plan-mark-dot\"/>"),
        (
            3,
            "<line x1=\"0\" y1=\"4\" x2=\"8\" y2=\"4\" class=\"chartlet-plan-mark\"/><line x1=\"4\" y1=\"0\" x2=\"4\" y2=\"8\" class=\"chartlet-plan-mark\"/>",
        ),
    ]
    .into_iter()
    .take(phases - 1)
    {
        let turn = if number == 1 {
            " patternTransform=\"rotate(45)\""
        } else {
            ""
        };
        write!(
            output,
            "<pattern id=\"{id_prefix}-plan-phase-{number}\" width=\"8\" height=\"8\" patternUnits=\"userSpaceOnUse\"{turn}>{content}</pattern>"
        )
        .expect("writing to String cannot fail");
    }
    output.push_str("</defs>");
}

/// A hatched shape refers to its pattern by an attribute rather than by a stylesheet rule: the
/// pattern id carries the chart's `idPrefix`, and a rule would reach every chart on the page.
fn hatch_fill(class: &str, style_index: Option<usize>, id_prefix: &str) -> String {
    match (class, style_index) {
        ("chartlet-hatch", Some(index)) => format!(" fill=\"url(#{id_prefix}-hatch-{index})\""),
        ("chartlet-range-hatch", _) => format!(" fill=\"url(#{id_prefix}-hatch-range)\""),
        (class, _) if class.starts_with("chartlet-plan-phase-") => {
            format!(
                " fill=\"url(#{id_prefix}-{})\"",
                &class["chartlet-".len()..]
            )
        }
        _ => String::new(),
    }
}

#[allow(clippy::too_many_lines)]
fn emit_element(
    element: &Element,
    id_prefix: &str,
    hooks: Option<&ChartSpec>,
    mut sketch: Option<&mut Sketch>,
    output: &mut String,
) {
    match element {
        Element::Circle(circle) => {
            open("circle", circle.series_index, output);
            write!(
                output,
                " cx=\"{}\" cy=\"{}\" r=\"{}\" class=\"{}{}\"",
                number(circle.cx),
                number(circle.cy),
                number(circle.radius),
                circle.class,
                point_class(circle.style_index),
            )
            .expect("write");
            close("circle", circle.tooltip.as_deref(), output);
        }
        Element::Line(line) => {
            if let Some(path) = sketch
                .as_deref_mut()
                .and_then(|sketch| sketch.line(line.x1, line.y1, line.x2, line.y2))
            {
                write!(output, "<path d=\"{path}\" class=\"{}\"/>", line.class).expect("write");
                return;
            }
            write!(
            output,
            "<line x1=\"{}\" y1=\"{}\" x2=\"{}\" y2=\"{}\" class=\"{}\"/>",
            number(line.x1),
            number(line.y1),
            number(line.x2),
            number(line.y2),
            line.class
        )
        .expect("write");
        }
        Element::Rect(rect) => {
            let path = if rect.class.contains("hit") || rect.class.contains("background") {
                None
            } else {
                sketch
                    .as_deref_mut()
                    .and_then(|sketch| sketch.rect(rect.x, rect.y, rect.width, rect.height))
            };
            if let Some(path) = path {
                open("path", rect.series_index, output);
                write!(
                    output,
                    " d=\"{path}\"{} class=\"{}{}\"",
                    hatch_fill(rect.class, rect.style_index, id_prefix),
                    rect.class,
                    swatch_class(rect.style_index),
                )
                .expect("write");
                close("path", rect.tooltip.as_deref(), output);
                return;
            }
            open("rect", rect.series_index, output);
            write!(
                output,
                " x=\"{}\" y=\"{}\" width=\"{}\" height=\"{}\"{} class=\"{}{}\"",
                number(rect.x),
                number(rect.y),
                number(rect.width),
                number(rect.height),
                hatch_fill(rect.class, rect.style_index, id_prefix),
                rect.class,
                swatch_class(rect.style_index),
            )
            .expect("write");
            close("rect", rect.tooltip.as_deref(), output);
        }
        Element::SeriesText(text, series_index) => {
            emit_text(text, Some(*series_index), output);
        }
        Element::Polyline(polyline) => emit_polyline(polyline, id_prefix, sketch, output),
        Element::Text(text) => emit_text(text, None, output),
        Element::Hook(hook) => {
            if let Some(spec) = hooks {
                emit_hook(hook, spec, output);
            }
        }
        Element::Group(classes) => {
            write!(output, "<g class=\"{classes}\">").expect("write");
        }
        Element::NodeGroup(classes, id) => {
            write!(output, "<g class=\"{classes}\"").expect("write");
            if hooks.is_some() {
                write!(output, " data-chartlet-node=\"{}\"", escape(id)).expect("write");
            }
            output.push('>');
        }
        Element::EdgeGroup(classes, id) => {
            write!(output, "<g class=\"{classes}\"").expect("write");
            if hooks.is_some() {
                write!(output, " data-chartlet-edge=\"{}\"", escape(id)).expect("write");
            }
            output.push('>');
        }
        Element::GroupEnd => output.push_str("</g>"),
        Element::Hotspot(_) => {}
        Element::Symbol(symbol) => write!(
            output,
            "<use href=\"#{id_prefix}-symbol-{}\" x=\"{}\" y=\"{}\" width=\"{size}\" height=\"{size}\" class=\"chartlet-arch-symbol\"/>",
            symbol.name,
            number(symbol.x),
            number(symbol.y),
            size = number(crate::symbols::SIZE),
        )
        .expect("write"),
    }
}

/// Writes a polyline.
fn emit_polyline(
    polyline: &crate::scene::Polyline,
    id_prefix: &str,
    sketch: Option<&mut Sketch>,
    output: &mut String,
) {
    open("polyline", polyline.series_index, output);
    let displaced;
    let source = match sketch {
        Some(sketch) if !polyline.class.contains("hit") => {
            displaced = sketch.points(&polyline.points);
            &displaced
        }
        _ => &polyline.points,
    };
    let points = source
        .iter()
        .map(|(x, y)| format!("{},{}", number(*x), number(*y)))
        .collect::<Vec<_>>()
        .join(" ");
    write!(
        output,
        " points=\"{}\"{} class=\"{}{}\"",
        points,
        hatch_fill(polyline.class, polyline.style_index, id_prefix),
        polyline.class,
        style_class(polyline.style_index),
    )
    .expect("write");
    close("polyline", polyline.tooltip.as_deref(), output);
}

fn emit_text(text: &crate::scene::Text, series_index: Option<usize>, output: &mut String) {
    open("text", series_index, output);
    write!(
        output,
        " x=\"{}\" y=\"{}\" text-anchor=\"{}\" class=\"{}\">{}</text>",
        number(text.x),
        number(text.y),
        anchor(text.anchor),
        text.class,
        escape(&text.content)
    )
    .expect("write");
}

fn open(tag: &str, series_index: Option<usize>, output: &mut String) {
    write!(output, "<{tag}").expect("write");
    if let Some(i) = series_index {
        write!(output, " data-series=\"{i}\"").expect("write");
    }
}

/// The class that carries a specification-declared stroke color, with the leading space, or
/// nothing at all when the element has none.
fn style_class(style_index: Option<usize>) -> String {
    style_index.map_or_else(String::new, |index| format!(" chartlet-style-{index}"))
}

/// The same, for the fill of a legend swatch.
fn swatch_class(style_index: Option<usize>) -> String {
    style_index.map_or_else(String::new, |index| {
        format!(" chartlet-style-{index}-swatch")
    })
}

/// The same, for the fill of a data point.
fn point_class(style_index: Option<usize>) -> String {
    style_index.map_or_else(String::new, |index| {
        format!(" chartlet-style-{index}-point")
    })
}

fn close(tag: &str, tooltip: Option<&str>, output: &mut String) {
    if let Some(text) = tooltip {
        write!(output, "><title>{}</title></{tag}>", escape(text)).expect("write");
    } else {
        output.push_str("/>");
    }
}

const fn anchor(anchor: TextAnchor) -> &'static str {
    match anchor {
        TextAnchor::Start => "start",
        TextAnchor::Middle => "middle",
        TextAnchor::End => "end",
    }
}