figgy-model 0.10.0

figgy chart model — option SSoT, interaction contracts, preset policies.
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Selectable style presets — axis frame styles and series color cycles.
//!
//! Both enums are **fieldless** on purpose: they cross the wasm boundary as
//! plain integers (`wasm_bindgen` exposes C-like enums directly), and a host
//! UI can list variants in a dropdown without knowing anything else about the
//! model. Names describe the *style*, not the tool that popularized it.
//!
//! Applying a preset only mutates `Config` / `SeriesConfig` — the usual dirty
//! rules apply on top: wrap axis-preset application in
//! `Chart::with_decoration_change` (raster only), and rebuild each affected
//! series' GPU style (`Renderer::create_style_for_series`) after recoloring.

use crate::color::Color;
use crate::config::{AxisOptions, Config, TickVisibility};
use crate::data_config::{DataRenderType, SeriesConfig};

const fn rgb8(r: u8, g: u8, b: u8) -> Color {
    Color::new(r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0)
}

// ============================================================================
// Axis presets.
// ============================================================================

/// Whole-chart axis frame styles, applied to all four axes at once via
/// [`Config::apply_axis_preset`].
///
/// Every preset keeps labels and titles on the bottom/left axes only and
/// leaves data ranges, margins, tick lengths, colors, and title text
/// untouched — it only switches which axis lines are drawn, the tick
/// direction, and which sides show tick-value labels.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AxisPreset {
    /// Closed box frame with ticks pointing **inward** on all four sides —
    /// the classic lab-instrument / scientific-plotting default.
    BoxedInward,
    /// Closed box frame, ticks pointing **outward** on the labeled
    /// bottom/left axes only — the common plotting-library default.
    BoxedOutward,
    /// Open L-frame: bottom/left axes only, ticks outward — the typical
    /// publication / biostatistics style.
    OpenOutward,
    /// Open L-frame with inward ticks — journals that want an open frame
    /// without ticks protruding into the margins.
    OpenInward,
    /// No axis lines and no ticks; tick-value labels only. Pair with a grid
    /// for a minimal, web-dashboard look.
    Minimal,
}

/// Per-side knobs a preset controls.
#[derive(Clone)]
struct SideStyle {
    line: bool,
    tick: TickVisibility,
    labels: bool,
}

fn apply_side(axis: &mut AxisOptions, s: SideStyle) {
    axis.line_visible = s.line;
    axis.tick = s.tick;
    axis.label_style.label_visible = s.labels;
}

impl Config {
    /// Apply `preset` to all four axes at once. See [`AxisPreset`] for what
    /// is (and is not) touched.
    pub fn apply_axis_preset(&mut self, preset: AxisPreset) {
        use TickVisibility::{Inside, None as NoTick, Outside};

        // (labeled = bottom/left, unlabeled = top/right) side styles.
        let (labeled, unlabeled) = match preset {
            AxisPreset::BoxedInward => (
                SideStyle {
                    line: true,
                    tick: Inside,
                    labels: true,
                },
                SideStyle {
                    line: true,
                    tick: Inside,
                    labels: false,
                },
            ),
            AxisPreset::BoxedOutward => (
                SideStyle {
                    line: true,
                    tick: Outside,
                    labels: true,
                },
                SideStyle {
                    line: true,
                    tick: NoTick,
                    labels: false,
                },
            ),
            AxisPreset::OpenOutward => (
                SideStyle {
                    line: true,
                    tick: Outside,
                    labels: true,
                },
                SideStyle {
                    line: false,
                    tick: NoTick,
                    labels: false,
                },
            ),
            AxisPreset::OpenInward => (
                SideStyle {
                    line: true,
                    tick: Inside,
                    labels: true,
                },
                SideStyle {
                    line: false,
                    tick: NoTick,
                    labels: false,
                },
            ),
            AxisPreset::Minimal => (
                SideStyle {
                    line: false,
                    tick: NoTick,
                    labels: true,
                },
                SideStyle {
                    line: false,
                    tick: NoTick,
                    labels: false,
                },
            ),
        };

        apply_side(&mut self.bottom_x, labeled.clone());
        apply_side(&mut self.left_y, labeled);
        apply_side(&mut self.top_x, unlabeled.clone());
        apply_side(&mut self.right_y, unlabeled);
    }
}

// ============================================================================
// Series color cycles.
// ============================================================================

/// Color rotation rules for multi-series plots. `color(i)` wraps around, so
/// any number of series can be colored consistently.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorCycle {
    /// Strong primary rotation starting at black — the classic
    /// lab-instrument increment list.
    Classic,
    /// High-saturation, high-contrast rotation — the look of biostatistics
    /// plotting tools, optimized for telling few series apart at a glance.
    Vivid,
    /// Ten balanced mid-saturation hues (the public "tab10" palette) — the
    /// modern general-purpose default.
    Balanced,
    /// The Okabe–Ito eight-color palette, designed to stay distinguishable
    /// under the common forms of color-vision deficiency.
    ColorblindSafe,
    /// Grayscale steps for print-safe single-ink figures.
    Monochrome,
}

const CLASSIC: &[Color] = &[
    rgb8(0, 0, 0),      // black
    rgb8(255, 0, 0),    // red
    rgb8(0, 128, 0),    // green
    rgb8(0, 0, 255),    // blue
    rgb8(255, 0, 255),  // magenta
    rgb8(0, 139, 139),  // dark cyan
    rgb8(184, 134, 11), // dark yellow
    rgb8(0, 0, 128),    // navy
    rgb8(128, 0, 128),  // purple
    rgb8(128, 0, 0),    // wine
    rgb8(128, 128, 0),  // olive
    rgb8(255, 102, 0),  // orange
];

const VIVID: &[Color] = &[
    rgb8(0, 32, 240),  // blue
    rgb8(230, 16, 16), // red
    rgb8(0, 153, 0),   // green
    rgb8(128, 0, 192), // purple
    rgb8(255, 128, 0), // orange
    rgb8(0, 0, 0),     // black
    rgb8(230, 0, 172), // pink
    rgb8(0, 153, 153), // teal
];

const BALANCED: &[Color] = &[
    rgb8(0x1f, 0x77, 0xb4),
    rgb8(0xff, 0x7f, 0x0e),
    rgb8(0x2c, 0xa0, 0x2c),
    rgb8(0xd6, 0x27, 0x28),
    rgb8(0x94, 0x67, 0xbd),
    rgb8(0x8c, 0x56, 0x4b),
    rgb8(0xe3, 0x77, 0xc2),
    rgb8(0x7f, 0x7f, 0x7f),
    rgb8(0xbc, 0xbd, 0x22),
    rgb8(0x17, 0xbe, 0xcf),
];

const COLORBLIND_SAFE: &[Color] = &[
    rgb8(0xe6, 0x9f, 0x00), // orange
    rgb8(0x56, 0xb4, 0xe9), // sky blue
    rgb8(0x00, 0x9e, 0x73), // bluish green
    rgb8(0xf0, 0xe4, 0x42), // yellow
    rgb8(0x00, 0x72, 0xb2), // blue
    rgb8(0xd5, 0x5e, 0x00), // vermillion
    rgb8(0xcc, 0x79, 0xa7), // reddish purple
    rgb8(0x00, 0x00, 0x00), // black
];

const MONOCHROME: &[Color] = &[
    rgb8(0, 0, 0),
    rgb8(89, 89, 89),
    rgb8(140, 140, 140),
    rgb8(191, 191, 191),
];

impl ColorCycle {
    /// The full palette in rotation order.
    pub fn colors(self) -> &'static [Color] {
        match self {
            Self::Classic => CLASSIC,
            Self::Vivid => VIVID,
            Self::Balanced => BALANCED,
            Self::ColorblindSafe => COLORBLIND_SAFE,
            Self::Monochrome => MONOCHROME,
        }
    }

    /// Color for the `index`-th series — wraps past the palette end, so it is
    /// total over any series count.
    pub fn color(self, index: usize) -> Color {
        let palette = self.colors();
        palette[index % palette.len()]
    }

    pub fn len(self) -> usize {
        self.colors().len()
    }

    pub fn is_empty(self) -> bool {
        false
    }

    /// Recolor one series as the `index`-th member of this cycle: line,
    /// scatter, and errorbar sub-styles all take the same cycle color.
    /// Rebuild the series' GPU style afterwards
    /// (`Renderer::create_style_for_series`).
    pub fn apply_to_series(self, series: &mut SeriesConfig, index: usize) {
        let c = self.color(index);
        match &mut series.render_type {
            DataRenderType::Scatter { scatter } => {
                scatter.point_color = c;
            }
            DataRenderType::Line { line } => {
                line.line_color = c;
            }
            DataRenderType::ScatterLine { scatter, line } => {
                scatter.point_color = c;
                line.line_color = c;
            }
            DataRenderType::ScatterErrorbarX {
                scatter, err_style, ..
            }
            | DataRenderType::ScatterErrorbarY {
                scatter, err_style, ..
            }
            | DataRenderType::ScatterErrorbarXY {
                scatter, err_style, ..
            } => {
                scatter.point_color = c;
                err_style.error_bar_color = c;
            }
            DataRenderType::LineScatterErrorbarX {
                scatter,
                line,
                err_style,
                ..
            }
            | DataRenderType::LineScatterErrorbarY {
                scatter,
                line,
                err_style,
                ..
            }
            | DataRenderType::LineScatterErrorbarXY {
                scatter,
                line,
                err_style,
                ..
            } => {
                scatter.point_color = c;
                line.line_color = c;
                err_style.error_bar_color = c;
            }
            DataRenderType::Histogram { bar } => {
                bar.fill_color = c;
            }
            // The contour line joins the cycle like any other line. The fill
            // does not: its colours come from the colormap, and overwriting one
            // of them with a cycle entry would make the colourbar lie.
            DataRenderType::Contour { contour, .. }
            | DataRenderType::HeatmapContour { contour, .. } => {
                contour.line.line_color = c;
            }
            DataRenderType::Heatmap { .. } => {}
        }
    }

    /// Recolor a whole series list in order: series `i` gets `color(i)`.
    pub fn apply_to_all(self, series: &mut [SeriesConfig]) {
        for (i, s) in series.iter_mut().enumerate() {
            self.apply_to_series(s, i);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::data_config::{
        DataErrorBarStyleConfig, DataLineStyleConfig, DataScatterStyleConfig, ErrorRef,
        ScatterShape,
    };
    use crate::default::default_config;
    use crate::line::LineStylePreset;

    #[test]
    fn boxed_inward_shows_full_frame_with_inside_ticks() {
        let mut cfg = default_config();
        cfg.apply_axis_preset(AxisPreset::BoxedInward);
        for axis in [&cfg.top_x, &cfg.bottom_x, &cfg.left_y, &cfg.right_y] {
            assert!(axis.line_visible);
            assert_eq!(axis.tick, TickVisibility::Inside);
        }
        assert!(cfg.bottom_x.label_style.label_visible);
        assert!(cfg.left_y.label_style.label_visible);
        assert!(!cfg.top_x.label_style.label_visible);
        assert!(!cfg.right_y.label_style.label_visible);
    }

    #[test]
    fn open_outward_hides_top_right_entirely() {
        let mut cfg = default_config();
        cfg.apply_axis_preset(AxisPreset::OpenOutward);
        assert!(cfg.bottom_x.line_visible);
        assert_eq!(cfg.bottom_x.tick, TickVisibility::Outside);
        assert!(!cfg.top_x.line_visible);
        assert_eq!(cfg.top_x.tick, TickVisibility::None);
        assert!(!cfg.right_y.line_visible);
    }

    #[test]
    fn minimal_drops_lines_and_ticks_but_keeps_labels() {
        let mut cfg = default_config();
        cfg.apply_axis_preset(AxisPreset::Minimal);
        for axis in [&cfg.top_x, &cfg.bottom_x, &cfg.left_y, &cfg.right_y] {
            assert!(!axis.line_visible);
            assert_eq!(axis.tick, TickVisibility::None);
        }
        assert!(cfg.bottom_x.label_style.label_visible);
    }

    // A preset must not disturb anything outside frame/tick/label visibility.
    #[test]
    fn preset_leaves_ranges_margins_titles_untouched() {
        let mut cfg = default_config();
        let before = cfg.clone();
        cfg.apply_axis_preset(AxisPreset::OpenOutward);

        assert_eq!(cfg.bottom_x.min, before.bottom_x.min);
        assert_eq!(cfg.bottom_x.max, before.bottom_x.max);
        assert_eq!(cfg.bottom_x.out_margin, before.bottom_x.out_margin);
        assert_eq!(
            cfg.left_y.major_tick_length,
            before.left_y.major_tick_length
        );
        assert_eq!(cfg.chart_title, before.chart_title);
        assert_eq!(cfg.grid, before.grid);
        assert_eq!(cfg.data_area().unwrap(), before.data_area().unwrap());
    }

    #[test]
    fn cycle_color_wraps_around() {
        for cycle in [
            ColorCycle::Classic,
            ColorCycle::Vivid,
            ColorCycle::Balanced,
            ColorCycle::ColorblindSafe,
            ColorCycle::Monochrome,
        ] {
            let n = cycle.len();
            assert!(n >= 4, "{cycle:?} palette too small");
            assert_eq!(cycle.color(0), cycle.color(n));
            assert_eq!(cycle.color(3), cycle.color(3 + n * 2));
            // Adjacent rotation entries must differ.
            assert_ne!(cycle.color(0), cycle.color(1));
        }
    }

    fn full_series() -> SeriesConfig {
        SeriesConfig {
            series_id: "s".into(),
            source_id: None,
            label: None,
            x_column: "x".into(),
            y_column: "y".into(),
            render_type: DataRenderType::LineScatterErrorbarXY {
                scatter: DataScatterStyleConfig {
                    point_color: Color::BLACK,
                    point_shape: ScatterShape::Circle,
                    point_size: 4.0,
                    point_style_table: None,
                    point_style_index_column: None,
                    point_style_overrides: None,
                },
                line: DataLineStyleConfig {
                    line_style: LineStylePreset::Solid,
                    line_color: Color::BLACK,
                    line_width: 1.0,
                },
                err_x: ErrorRef::Symmetric {
                    column: "ex".into(),
                },
                err_y: ErrorRef::Symmetric {
                    column: "ey".into(),
                },
                err_style: DataErrorBarStyleConfig {
                    error_bar_color: Color::BLACK,
                    error_bar_width: 1.0,
                    error_bar_cap_size: 3.0,
                    cap_width: 1.0,
                    error_bar_style_table: None,
                    error_bar_style_index_column: None,
                    error_bar_style_overrides: None,
                },
            },
        }
    }

    #[test]
    fn apply_to_series_recolors_every_sub_style() {
        let mut s = full_series();
        ColorCycle::Balanced.apply_to_series(&mut s, 2);
        let expected = ColorCycle::Balanced.color(2);
        let DataRenderType::LineScatterErrorbarXY {
            scatter,
            line,
            err_style,
            ..
        } = &s.render_type
        else {
            unreachable!()
        };
        assert_eq!(scatter.point_color, expected);
        assert_eq!(line.line_color, expected);
        assert_eq!(err_style.error_bar_color, expected);
    }

    #[test]
    fn apply_to_all_assigns_sequential_rotation() {
        let mut list = vec![full_series(), full_series(), full_series()];
        ColorCycle::Classic.apply_to_all(&mut list);
        for (i, s) in list.iter().enumerate() {
            let DataRenderType::LineScatterErrorbarXY { line, .. } = &s.render_type else {
                unreachable!()
            };
            assert_eq!(line.line_color, ColorCycle::Classic.color(i));
        }
    }

    // The contour line joins the cycle like any other line; the field's colours
    // come from the colormap and the cycle must not reach into them.
    #[test]
    fn the_cycle_recolors_bars_and_contour_lines_but_never_a_field() {
        use crate::data_config::{
            BarOrientation, ContourConfig, DataBarStyleConfig, FieldFillConfig, FillMode,
            GridLayout, MatrixOrientation, MatrixRef, Shading,
        };

        let matrix = || MatrixRef {
            columns: vec!["z0".into()],
            orientation: MatrixOrientation::ColumnsAreX,
            grid_layout: GridLayout::Centers,
        };
        let fill = || FieldFillConfig {
            mode: FillMode::Continuous,
            shading: Shading::Flat,
            opacity: 1.0,
        };
        let contour = || ContourConfig {
            levels: vec![1.0],
            line: DataLineStyleConfig {
                line_style: LineStylePreset::Solid,
                line_color: Color::BLACK,
                line_width: 1.0,
            },
            per_level_color: None,
            labels: None,
        };
        let series = |render_type| SeriesConfig {
            series_id: "s".into(),
            source_id: None,
            label: None,
            x_column: "x".into(),
            y_column: "y".into(),
            render_type,
        };
        let expected = ColorCycle::Classic.color(2);

        let mut histogram = series(DataRenderType::Histogram {
            bar: DataBarStyleConfig {
                fill_color: Color::BLACK,
                border_color: Color::BLACK,
                border_width: 1.0,
                baseline: 0.0,
                gap_px: 1.0,
                width_ratio: 1.0,
                orientation: BarOrientation::Vertical,
                bar_style_overrides: None,
            },
        });
        ColorCycle::Classic.apply_to_series(&mut histogram, 2);
        let DataRenderType::Histogram { bar } = &histogram.render_type else {
            unreachable!()
        };
        assert_eq!(bar.fill_color, expected);

        for render_type in [
            DataRenderType::Contour {
                matrix: matrix(),
                contour: contour(),
            },
            DataRenderType::HeatmapContour {
                matrix: matrix(),
                fill: fill(),
                contour: contour(),
            },
        ] {
            let mut item = series(render_type);
            ColorCycle::Classic.apply_to_series(&mut item, 2);
            let line_color = match &item.render_type {
                DataRenderType::Contour { contour, .. }
                | DataRenderType::HeatmapContour { contour, .. } => contour.line.line_color,
                _ => unreachable!(),
            };
            assert_eq!(line_color, expected);
        }

        // A fill is left exactly as declared — including inside the combined
        // variant, where only the line was recoloured above.
        let mut heatmap = series(DataRenderType::Heatmap {
            matrix: matrix(),
            fill: fill(),
        });
        let before = heatmap.clone();
        ColorCycle::Classic.apply_to_series(&mut heatmap, 2);
        assert_eq!(heatmap, before);
    }
}