chartlet 0.23.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
//! Drawing a planned diagram: lanes and frames, every edge and step in a group of its own, and
//! the classes that tie a drawn part to the steps whose focus brings it forward.

use std::fmt::Write as _;

use super::{
    BRANCH, GRID, GROUP_HEAD, GROUP_PAD, LANE_HEAD, SUBLABEL_SIZE, WEIGHT_STEPS, blocks, down,
    measure::Model,
    model::{Diagram, Shape},
    place::Plan,
    route::Route,
    trunks, up,
};
use crate::{
    error::ChartWarning,
    heat,
    metrics::TextMetrics,
    roles,
    scene::{Circle, Element, Hook, Hotspot, Rect, Text, TextAnchor},
};

impl Model<'_> {
    /// The focus classes of a step: the step itself and every step it shares an edge with.
    pub(super) fn node_classes(&self, node: usize) -> String {
        // A diagram that draws part of a model names the nodes of the whole.
        let (node, ends) = match &self.diagram.whole {
            Some(whole) => (whole.nodes[node], &whole.all_ends),
            None => (node, &self.ends),
        };
        let mut related: Vec<usize> = ends
            .iter()
            .filter_map(|&(from, to)| match (from == node, to == node) {
                (true, _) => Some(to),
                (_, true) => Some(from),
                _ => None,
            })
            .chain(std::iter::once(node))
            .collect();
        related.sort_unstable();
        related.dedup();
        focus_classes(Some(node), &related)
    }

    /// The focus classes of an edge: the steps at both its ends.
    pub(super) fn edge_classes(&self, edge: usize) -> String {
        let (from, to) = self
            .diagram
            .whole
            .as_ref()
            .map_or(self.ends[edge], |whole| whole.ends[edge]);
        focus_classes(None, &[from.min(to), from.max(to)])
    }
}

/// The width step of every edge with a weight: in proportion to its weight against the heaviest
/// edge, from 0 to [`WEIGHT_STEPS`]. Edges without a weight keep the usual line.
fn weight_widths(diagram: &Diagram) -> Vec<Option<u8>> {
    weight_widths_of(diagram.edges.iter().map(|edge| edge.weight))
}

/// [`weight_widths`] for any edges, given by their weights.
pub(crate) fn weight_widths_of(
    weights: impl Iterator<Item = Option<f64>> + Clone,
) -> Vec<Option<u8>> {
    let heaviest = weights.clone().flatten().fold(0.0_f64, f64::max);
    weights
        .map(|weight| {
            weight.map(|weight| {
                let share = if heaviest > 0.0 {
                    weight / heaviest
                } else {
                    0.0
                };
                // At most WEIGHT_STEPS, which fits in a u8.
                #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
                let step = (share * WEIGHT_STEPS).round() as u8;
                step
            })
        })
        .collect()
}

/// The classes that tie a drawn part to the nodes whose focus brings it forward, and to its own
/// node when it is one: `chartlet-f chartlet-n-3 chartlet-f-1 chartlet-f-3`.
pub(crate) fn focus_classes(own: Option<usize>, related: &[usize]) -> String {
    let mut classes = String::from("chartlet-f");
    if let Some(own) = own {
        write!(classes, " chartlet-n-{own}").expect("writing to String cannot fail");
    }
    let mut last = None;
    for node in related {
        if last != Some(*node) {
            write!(classes, " chartlet-f-{node}").expect("writing to String cannot fail");
        }
        last = Some(*node);
    }
    classes
}

impl Plan {
    /// Draws the diagram and returns how far right and down the edge labels reached, which the
    /// canvas must hold too.
    pub(super) fn draw(
        &self,
        model: &Model,
        metrics: &impl TextMetrics,
        warnings: &mut Vec<ChartWarning>,
        elements: &mut Vec<Element>,
    ) -> (f64, f64) {
        self.draw_lanes(model, elements);
        self.draw_groups(model, metrics, warnings, elements);
        let mut labels = Vec::new();
        // Every edge, loop and step sits in a group whose classes name the steps it belongs to,
        // so that focusing a step can bring them forward.
        for (edge, width) in weight_widths(model.diagram).into_iter().enumerate() {
            let mut classes = model.edge_classes(edge);
            if let Some(width) = width {
                write!(classes, " chartlet-flow-w{width}").expect("writing to String cannot fail");
            }
            if let Some(category) = model.diagram.edges[edge].category {
                classes.push_str(category);
            }
            if let Some(role) = model.diagram.edges[edge].role {
                classes.push_str(roles::class(role));
            }
            elements.push(match &model.diagram.edges[edge].id {
                Some(id) => Element::EdgeGroup(classes, id.clone()),
                None => Element::Group(classes),
            });
            self.draw_edge(model, edge, elements, &mut labels);
            elements.push(Element::GroupEnd);
        }
        self.draw_branches(model, elements);
        for node in 0..model.diagram.nodes.len() {
            if model.diagram.nodes[node].shape == Shape::Block {
                continue;
            }
            elements.push(Element::Group(model.node_classes(node)));
            self.draw_loop(model, node, elements, &mut labels);
            elements.push(Element::GroupEnd);
        }
        let reach = self.draw_labels(model, labels, metrics, elements);
        let heat = heat::steps(
            &model
                .diagram
                .nodes
                .iter()
                .map(|node| node.value)
                .collect::<Vec<_>>(),
        );
        for (node, step) in heat.into_iter().enumerate() {
            // A block draws what it holds, each node with its own groups and hotspot.
            if let Some(block) = model.diagram.blocks.iter().find(|block| block.node == node) {
                let (x, y, _, _) = self.node_box(model, node);
                elements.extend(blocks::placed(&block.elements, (x, y)));
                continue;
            }
            let mut classes = model.node_classes(node);
            if let Some(step) = step {
                classes.push_str(&heat::class(step));
            }
            let spec = &model.diagram.nodes[node];
            if let Some(role) = spec.role {
                classes.push_str(roles::class(role));
            }
            elements.push(match &spec.id {
                Some(id) => Element::NodeGroup(classes, id.clone()),
                None => Element::Group(classes),
            });
            self.draw_node(model, node, elements);
            if let Some(role) = spec.role {
                let (x, y, _, _) = self.node_box(model, node);
                roles::push_badge(role, (x, y), elements);
            }
            elements.push(Element::GroupEnd);
            if spec.shape != Shape::Initial {
                let (x, y, width, height) = self.node_box(model, node);
                elements.push(Element::Hotspot(Hotspot {
                    node: model
                        .diagram
                        .whole
                        .as_ref()
                        .map_or(node, |whole| whole.nodes[node]),
                    label: spec.label.clone(),
                    x,
                    y,
                    width,
                    height,
                }));
            }
        }
        reach
    }

    /// A dot at every branch of a shared trunk, tied to the steps of all its edges.
    fn draw_branches(&self, model: &Model, elements: &mut Vec<Element>) {
        for trunk in &model.trunks.shared {
            let routes: Vec<&Route> = trunk
                .edges
                .iter()
                .filter_map(|&edge| self.routes[edge].as_ref())
                .collect();
            let Some(bus) = routes
                .iter()
                .find(|route| route.points.len() == 4)
                .map(|route| route.points[1].0)
            else {
                continue;
            };
            let shared = if trunk.leaving {
                routes[0].points[0].1
            } else {
                routes[0].points[routes[0].points.len() - 1].1
            };
            let others: Vec<f64> = routes
                .iter()
                .map(|route| match (route.points.len(), trunk.leaving) {
                    (4, true) => route.points[2].1,
                    (4, false) => route.points[1].1,
                    _ => shared,
                })
                .collect();
            let points = trunks::branches(shared, bus, &others);
            if points.is_empty() {
                continue;
            }
            let mut related: Vec<usize> = trunk
                .edges
                .iter()
                .flat_map(|&edge| [model.ends[edge].0, model.ends[edge].1])
                .collect();
            related.sort_unstable();
            elements.push(Element::Group(focus_classes(None, &related)));
            for point in points {
                let (cx, cy) = self.page(point);
                elements.push(Element::Circle(Circle {
                    cx,
                    cy,
                    radius: BRANCH,
                    class: "chartlet-flow-head",
                    series_index: None,
                    style_index: None,
                    tooltip: None,
                }));
            }
            elements.push(Element::GroupEnd);
        }
    }

    fn draw_lanes(&self, model: &Model, elements: &mut Vec<Element>) {
        let lanes = &model.diagram.lanes;
        for (index, (lane, band)) in lanes.iter().zip(&self.bands).enumerate() {
            elements.push(Element::Hook(Hook::Object(
                "data-chartlet-lane",
                lane.id.clone(),
            )));
            let (x1, y1) = self.page((0.0, band.0));
            let (x2, y2) = self.page((self.main_length, band.1));
            elements.push(Element::Rect(Rect {
                x: x1.min(x2),
                y: y1.min(y2),
                width: (x2 - x1).abs(),
                height: (y2 - y1).abs(),
                class: if index % 2 == 0 {
                    "chartlet-flow-lane"
                } else {
                    "chartlet-flow-lane chartlet-flow-lane-alt"
                },
                series_index: None,
                style_index: None,
                tooltip: None,
            }));
            // A strip along the lane's start holds its name.
            let (h1, h2) = if self.landscape {
                (
                    self.page((0.0, band.0)),
                    self.page((self.main_length, band.0 + LANE_HEAD)),
                )
            } else {
                (self.page((0.0, band.0)), self.page((LANE_HEAD, band.1)))
            };
            elements.push(Element::Rect(Rect {
                x: h1.0.min(h2.0) + 0.5,
                y: h1.1.min(h2.1) + 0.5,
                width: (h2.0 - h1.0).abs() - 1.0,
                height: (h2.1 - h1.1).abs() - 1.0,
                class: "chartlet-flow-lane-head",
                series_index: None,
                style_index: None,
                tooltip: None,
            }));
            let (x, y, anchor) = if self.landscape {
                (x1 + 10.0, y1.min(y2) + 17.0, TextAnchor::Start)
            } else {
                (f64::midpoint(x1, x2), y1.min(y2) + 17.0, TextAnchor::Middle)
            };
            elements.push(Element::Text(Text {
                x,
                y,
                class: "chartlet-flow-lane-label",
                anchor,
                content: lane.label.clone(),
            }));
            elements.push(Element::Hook(Hook::End));
        }
    }

    /// The frame of every group: around its own steps and the frames inside it.
    pub(super) fn group_frames(
        &self,
        model: &Model,
        metrics: &impl TextMetrics,
    ) -> Vec<(f64, f64, f64, f64)> {
        let diagram = model.diagram;
        let groups = diagram.groups.len();
        let depth = |group: usize| diagram.chain(Some(group)).len();
        // Frames from the innermost out.
        let mut frames: Vec<(f64, f64, f64, f64)> = vec![(0.0, 0.0, 0.0, 0.0); groups];
        let mut inner_first: Vec<usize> = (0..groups).collect();
        crate::sort::by_key(&mut inner_first, |group| {
            (std::cmp::Reverse(depth(*group)), *group)
        });
        for &group in &inner_first {
            let boxes = (0..diagram.nodes.len())
                .filter(|node| diagram.nodes[*node].group == Some(group))
                .map(|node| {
                    let (x, y, width, height) = self.node_box(model, node);
                    (x, y, x + width, y + height)
                })
                .chain(
                    (0..groups)
                        .filter(|child| diagram.groups[*child].parent == Some(group))
                        .map(|child| frames[child]),
                );
            let (left, top, right, bottom) = boxes.fold(
                (
                    f64::INFINITY,
                    f64::INFINITY,
                    f64::NEG_INFINITY,
                    f64::NEG_INFINITY,
                ),
                |(left, top, right, bottom), b| {
                    (left.min(b.0), top.min(b.1), right.max(b.2), bottom.max(b.3))
                },
            );
            // A frame is at least as wide as its own name, which sits in its top left corner.
            let named = up(
                metrics.width(&diagram.groups[group].label, SUBLABEL_SIZE) + 16.0,
                GRID,
            );
            let extra = (named - (right - left + 2.0 * GROUP_PAD)).max(0.0);
            // Along the flow the frame grows to the right only, away from the frames around it;
            // across it, on both sides.
            let (before, after) = if self.landscape {
                (0.0, extra)
            } else {
                let before = down(extra / 2.0, GRID);
                (before, extra - before)
            };
            frames[group] = (
                left - GROUP_PAD - before,
                top - GROUP_PAD - GROUP_HEAD,
                right + GROUP_PAD + after,
                bottom + GROUP_PAD,
            );
        }
        frames
    }

    fn draw_groups(
        &self,
        model: &Model,
        metrics: &impl TextMetrics,
        warnings: &mut Vec<ChartWarning>,
        elements: &mut Vec<Element>,
    ) {
        let diagram = model.diagram;
        let groups = diagram.groups.len();
        let frames = self.group_frames(model, metrics);
        let depth = |group: usize| diagram.chain(Some(group)).len();
        let mut inner_first: Vec<usize> = (0..groups).collect();
        crate::sort::by_key(&mut inner_first, |group| {
            (std::cmp::Reverse(depth(*group)), *group)
        });
        for &group in inner_first.iter().rev() {
            let (left, top, right, bottom) = frames[group];
            let intruder = (0..diagram.nodes.len())
                .filter(|node| !diagram.inside(*node, group))
                .map(|node| self.node_box(model, node))
                .any(|(x, y, width, height)| {
                    x < right && x + width > left && y < bottom && y + height > top
                });
            if intruder {
                warnings.push(ChartWarning::new(
                    "group_overlap",
                    diagram.groups[group].path.clone(),
                    "a step outside the frame lies inside it; move it elsewhere or regroup the steps",
                ));
            }
            if let Some(id) = &diagram.groups[group].id {
                elements.push(Element::Hook(Hook::Object(
                    "data-chartlet-frame",
                    id.clone(),
                )));
            }
            elements.push(Element::Rect(Rect {
                x: left,
                y: top,
                width: right - left,
                height: bottom - top,
                class: "chartlet-flow-group",
                series_index: None,
                style_index: None,
                tooltip: None,
            }));
            let path = format!("{}/label", diagram.groups[group].path);
            elements.push(Element::Text(Text {
                x: left + 8.0,
                y: top + 13.0,
                class: "chartlet-flow-group-label",
                anchor: TextAnchor::Start,
                content: crate::layout::fit_text(
                    &diagram.groups[group].label,
                    right - left - 16.0,
                    SUBLABEL_SIZE,
                    metrics,
                    warnings,
                    &path,
                ),
            }));
            if diagram.groups[group].id.is_some() {
                elements.push(Element::Hook(Hook::End));
            }
        }
    }
}