perspective-viewer 5.3.1

A data visualization and analytics component, especially well-suited for large and/or streaming datasets.
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

use std::collections::HashSet;

use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::{
    CssKind, CustomNumberFormatConfig, DatetimeFormatType, KeyValueOpts,
    NumberSeriesStyleDefaultConfig,
};

/// The full schema for one column at one point in time. Plugins may return
/// different schemas for the same column based on the column's current
/// stored value (e.g. to hide dependent fields), so this is re-queried on
/// every field update.
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ColumnConfigSchema {
    pub fields: Vec<ControlSpec>,
}

impl ColumnConfigSchema {
    /// Union of every JSON key any control in this schema knows how to
    /// read or write. Used to build the schema-filtered view of
    /// `columns_config` passed to `plugin.restore()` — keys not in this
    /// set are "ghost" state from a different plugin and stay invisible
    /// to the active one.
    pub fn active_keys(&self) -> HashSet<String> {
        let mut out = HashSet::new();
        for spec in &self.fields {
            for k in spec.serialized_keys() {
                out.insert(k.to_string());
            }
        }
        out
    }

    pub fn leaf_fields(&self) -> Vec<&ControlSpec> {
        fn collect<'a>(fields: &'a [ControlSpec], out: &mut Vec<&'a ControlSpec>) {
            for spec in fields {
                match spec {
                    ControlSpec::Group { fields, .. } => collect(fields, out),
                    leaf => out.push(leaf),
                }
            }
        }

        let mut out = vec![];
        collect(&self.fields, &mut out);
        out
    }
}

/// Discriminated union of widget kinds the viewer can render. Composite
/// variants wrap an existing rich Yew component and carry only the
/// component's `*DefaultConfig`. Primitive variants render generic scalar
/// widgets and carry their own `key` inline; the visible label is
/// resolved at CSS time via `--psp-label--<key>--content`.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(tag = "kind")]
pub enum ControlSpec {
    Enum {
        key: String,
        variants: Vec<EnumVariant>,
        default: String,
    },
    Bool {
        key: String,
        default: bool,
    },
    Number {
        key: String,
        default: f64,

        /// If `true`, always serialize this values even if it is the default.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        include: Option<bool>,

        #[serde(default, skip_serializing_if = "Option::is_none")]
        min: Option<f64>,

        #[serde(default, skip_serializing_if = "Option::is_none")]
        max: Option<f64>,

        #[serde(default, skip_serializing_if = "Option::is_none")]
        step: Option<f64>,
    },
    String {
        key: String,
        default: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        placeholder: Option<String>,
    },
    Color {
        key: String,
        default: String,
    },
    Palette {
        key: String,
        default: String,

        #[serde(default, skip_serializing_if = "Option::is_none")]
        max: Option<usize>,
    },
    GradientStops {
        key: String,
        default: String,

        #[serde(default, skip_serializing_if = "std::ops::Not::not")]
        discrete: bool,
    },
    DatetimeFormat {
        /// Plugin-declared default `date_format`, shown by the editor in
        /// unedited fields and elided from serialized configs.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        default: Option<DatetimeFormatType>,
    },
    StringFormat,
    NumberSeriesStyle {
        default: NumberSeriesStyleDefaultConfig,
    },
    Symbols {
        default: KeyValueOpts,
    },
    NumberFormat {
        /// Plugin-declared default format, keyed like `number_format`
        /// itself.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        default: Option<CustomNumberFormatConfig>,
    },
    AggregateDepth,

    Group {
        key: String,
        #[serde(default)]
        fields: Vec<ControlSpec>,
    },
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct EnumVariant {
    pub value: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
}

/// One stop of a [`ControlSpec::GradientStops`] value in its in-memory
/// form: a `#rrggbb` color at `offset` ∈ `[0, 1]`.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct GradientStopSpec {
    pub color: String,
    pub offset: f64,
}

/// The [`ControlSpec::GradientStops`] canonical stop order: offsets
/// clamped to `[0, 1]` and rounded to 3 decimals, stops sorted stably
/// by offset.
pub fn canonicalize_gradient_stops(mut stops: Vec<GradientStopSpec>) -> Vec<GradientStopSpec> {
    for stop in &mut stops {
        stop.offset = (stop.offset.clamp(0.0, 1.0) * 1000.0).round() / 1000.0;
    }

    stops.sort_by(|a, b| {
        a.offset
            .partial_cmp(&b.offset)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    stops
}

/// Fit `stops` to a `discrete` field's fixed pair: an over-length value
/// keeps only its two end colors, pinned to `0`/`1`.
pub fn discrete_pair(stops: Vec<GradientStopSpec>) -> Vec<GradientStopSpec> {
    let stops = canonicalize_gradient_stops(stops);
    match (stops.first(), stops.last()) {
        (Some(first), Some(last)) if stops.len() > 2 => vec![
            GradientStopSpec {
                color: first.color.clone(),
                offset: 0.0,
            },
            GradientStopSpec {
                color: last.color.clone(),
                offset: 1.0,
            },
        ],
        _ => stops,
    }
}

impl ColumnConfigSchema {
    pub fn canonicalize(self) -> Self {
        self.canonicalize_defaults().group_format_controls()
    }

    pub fn group_format_controls(mut self) -> Self {
        fn is_format(spec: &ControlSpec) -> bool {
            matches!(
                spec,
                ControlSpec::NumberFormat { .. }
                    | ControlSpec::DatetimeFormat { .. }
                    | ControlSpec::StringFormat
            )
        }

        fn walk(fields: &mut Vec<ControlSpec>) {
            for spec in fields.iter_mut() {
                if let ControlSpec::Group { key, fields } = spec
                    && key != "format"
                {
                    walk(fields);
                }
            }

            let first = fields.iter().position(is_format);
            if let Some(first) = first {
                let mut formats = vec![];
                let mut i = first;
                while i < fields.len() {
                    if is_format(&fields[i]) {
                        formats.push(fields.remove(i));
                    } else {
                        i += 1;
                    }
                }

                fields.insert(first, ControlSpec::Group {
                    key: "format".to_owned(),
                    fields: formats,
                });
            }
        }

        walk(&mut self.fields);
        self
    }

    /// Canonicalize every CSS-valued default at schema ingest, dropping
    /// (and logging) fields whose default fails its kind's reader.
    pub fn canonicalize_defaults(mut self) -> Self {
        fn canonicalize_specs(fields: &mut Vec<ControlSpec>) {
            fields.retain_mut(|spec| {
                let (kind, key, default) = match spec {
                    ControlSpec::Group { fields, .. } => {
                        canonicalize_specs(fields);
                        return !fields.is_empty();
                    },
                    ControlSpec::Color { key, default } => (CssKind::Color, key, default),
                    ControlSpec::Palette { key, default, .. } => (CssKind::Palette, key, default),
                    ControlSpec::GradientStops { key, default, .. } => {
                        (CssKind::Gradient, key, default)
                    },
                    _ => return true,
                };

                match kind.canonicalize(default) {
                    Ok(canonical) => {
                        *default = canonical;
                        true
                    },
                    Err(error) => {
                        tracing::error!("Dropping `{key}` — invalid schema default: {error}");
                        false
                    },
                }
            });
        }

        canonicalize_specs(&mut self.fields);
        self
    }

    /// The CSS kind of the control owning `key`, if it is CSS-valued.
    pub fn css_kind_of(&self, key: &str) -> Option<CssKind> {
        self.leaf_fields().into_iter().find_map(|spec| match spec {
            ControlSpec::Color { key: k, .. } if k == key => Some(CssKind::Color),
            ControlSpec::Palette { key: k, .. } if k == key => Some(CssKind::Palette),
            ControlSpec::GradientStops { key: k, .. } if k == key => Some(CssKind::Gradient),
            _ => None,
        })
    }
}

impl ControlSpec {
    /// Top-level JSON keys this control owns when its value is serialized
    /// into a column's config map. For primitives this is just `[key]`;
    /// for composites it's the set of fields the wrapped sub-struct
    /// flattens. Used by [`ColumnConfigSchema::active_keys`] to filter the
    /// `columns_config` blob passed to `plugin.restore()`.
    pub fn serialized_keys(&self) -> Vec<&str> {
        match self {
            ControlSpec::DatetimeFormat { .. } => vec!["date_format"],
            ControlSpec::StringFormat => vec!["format"],
            ControlSpec::NumberSeriesStyle { .. } => vec!["chart_type", "stack"],
            ControlSpec::Symbols { .. } => vec!["symbols"],
            ControlSpec::NumberFormat { .. } => vec!["number_format"],
            ControlSpec::AggregateDepth => vec!["aggregate_depth"],
            ControlSpec::Enum { key, .. }
            | ControlSpec::Bool { key, .. }
            | ControlSpec::Number { key, .. }
            | ControlSpec::String { key, .. }
            | ControlSpec::Color { key, .. }
            | ControlSpec::Palette { key, .. }
            | ControlSpec::GradientStops { key, .. } => vec![key.as_str()],
            ControlSpec::Group { fields, .. } => {
                fields.iter().flat_map(|f| f.serialized_keys()).collect()
            },
        }
    }
}

/// One UI-emitted change to a single schema field. The emitting widget
/// declares which top-level keys the update is allowed to write
/// (`keys` — equivalent to the field's [`ControlSpec::serialized_keys`])
/// and a partial new sub-state (`value`).
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ColumnConfigFieldUpdate {
    pub keys: Vec<String>,
    pub value: serde_json::Map<String, Value>,
}

/// Filter a per-column config map to only the keys advertised by the
/// active plugin's schema. Foreign keys (left over from a previous plugin)
/// stay in the unfiltered presentation state but never reach `restore()`.
pub fn filter_to_schema(
    config: &serde_json::Map<String, Value>,
    active_keys: &HashSet<String>,
) -> serde_json::Map<String, Value> {
    config
        .iter()
        .filter(|(k, _)| active_keys.contains(k.as_str()))
        .map(|(k, v)| (k.clone(), v.clone()))
        .collect()
}

#[cfg(test)]
mod tests {
    use serde_json::json;

    use super::*;

    fn color(key: &str, default: &str) -> ControlSpec {
        ControlSpec::Color {
            key: key.to_owned(),
            default: default.to_owned(),
        }
    }

    fn flag(key: &str) -> ControlSpec {
        ControlSpec::Bool {
            key: key.to_owned(),
            default: false,
        }
    }

    fn group(key: &str, fields: Vec<ControlSpec>) -> ControlSpec {
        ControlSpec::Group {
            key: key.to_owned(),
            fields,
        }
    }

    #[test]
    fn group_deserializes_recursively() {
        let schema: ColumnConfigSchema = serde_json::from_value(json!({
            "fields": [{
                "kind": "Group",
                "key": "legend",
                "fields": [
                    { "kind": "Bool", "key": "legend_on", "default": false },
                    {
                        "kind": "Group",
                        "key": "inner",
                        "fields": [{ "kind": "Color", "key": "color", "default": "#ff0000" }]
                    }
                ]
            }]
        }))
        .unwrap();

        let keys = schema.active_keys();
        assert_eq!(
            keys,
            HashSet::from(["legend_on".to_owned(), "color".to_owned()])
        );

        let leaves = schema.leaf_fields();
        assert_eq!(leaves.len(), 2);
        assert!(
            leaves
                .iter()
                .all(|s| !matches!(s, ControlSpec::Group { .. }))
        );
    }

    #[test]
    fn grouped_schema_is_equivalent_to_flat() {
        let flat = ColumnConfigSchema {
            fields: vec![flag("stack"), color("color", "#0366d6")],
        };

        let grouped = ColumnConfigSchema {
            fields: vec![group("series", vec![
                flag("stack"),
                color("color", "#0366d6"),
            ])],
        };

        assert_eq!(flat.active_keys(), grouped.active_keys());
        assert_eq!(flat.css_kind_of("color"), grouped.css_kind_of("color"));
        assert_eq!(flat.css_kind_of("stack"), grouped.css_kind_of("stack"));
    }

    #[test]
    fn format_controls_group_and_merge() {
        let schema = ColumnConfigSchema {
            fields: vec![
                ControlSpec::NumberFormat { default: None },
                flag("flag"),
                ControlSpec::StringFormat,
            ],
        }
        .group_format_controls();

        assert_eq!(schema.fields.len(), 2);
        let ControlSpec::Group { key, fields } = &schema.fields[0] else {
            panic!("expected format group first");
        };

        assert_eq!(key, "format");
        assert!(matches!(fields[0], ControlSpec::NumberFormat { .. }));
        assert!(matches!(fields[1], ControlSpec::StringFormat));
        assert!(matches!(&schema.fields[1], ControlSpec::Bool { .. }));

        assert_eq!(
            schema.active_keys(),
            HashSet::from([
                "number_format".to_owned(),
                "format".to_owned(),
                "flag".to_owned()
            ])
        );
    }

    #[test]
    fn format_grouping_recurses_but_never_double_wraps() {
        let schema = ColumnConfigSchema {
            fields: vec![
                group("format", vec![ControlSpec::NumberFormat { default: None }]),
                group("styling", vec![flag("x"), ControlSpec::DatetimeFormat {
                    default: None,
                }]),
            ],
        }
        .group_format_controls();

        let ControlSpec::Group { key, fields } = &schema.fields[0] else {
            panic!("expected group");
        };

        assert_eq!(key, "format");
        assert!(matches!(fields[0], ControlSpec::NumberFormat { .. }));

        let ControlSpec::Group { fields, .. } = &schema.fields[1] else {
            panic!("expected group");
        };

        assert!(matches!(
            &fields[1],
            ControlSpec::Group { key, fields }
                if key == "format" && matches!(fields[0], ControlSpec::DatetimeFormat { .. })
        ));
    }

    #[test]
    fn format_controls_deserialize_without_default_payload() {
        let schema: ColumnConfigSchema = serde_json::from_value(json!({
            "fields": [{ "kind": "NumberFormat" }, { "kind": "DatetimeFormat" }]
        }))
        .unwrap();

        assert!(matches!(&schema.fields[0], ControlSpec::NumberFormat {
            default: None
        }));
        assert!(matches!(&schema.fields[1], ControlSpec::DatetimeFormat {
            default: None
        }));
    }

    #[test]
    fn number_format_default_payload_deserializes_flattened_families() {
        let schema: ColumnConfigSchema = serde_json::from_value(json!({
            "fields": [{
                "kind": "NumberFormat",
                "default": {
                    "notation": "compact",
                    "compactDisplay": "short",
                    "minimumFractionDigits": 0,
                    "maximumFractionDigits": 1
                }
            }]
        }))
        .unwrap();

        let ControlSpec::NumberFormat {
            default: Some(default),
        } = &schema.fields[0]
        else {
            panic!("expected NumberFormat with default");
        };

        assert_eq!(
            default._notation,
            Some(crate::config::Notation::Compact(
                crate::config::CompactDisplay::Short
            ))
        );
        assert_eq!(default._style, None);
        assert_eq!(default.minimum_fraction_digits, Some(0.));
        assert_eq!(default.maximum_fraction_digits, Some(1.));
        assert_eq!(
            schema.active_keys(),
            HashSet::from(["number_format".to_owned()])
        );
    }

    #[test]
    fn datetime_format_default_payload_deserializes_simple_arm() {
        let schema: ColumnConfigSchema = serde_json::from_value(json!({
            "fields": [{
                "kind": "DatetimeFormat",
                "default": { "dateStyle": "medium", "timeStyle": "disabled" }
            }]
        }))
        .unwrap();

        let ControlSpec::DatetimeFormat {
            default: Some(DatetimeFormatType::Simple(simple)),
        } = &schema.fields[0]
        else {
            panic!("expected DatetimeFormat with Simple default");
        };

        assert_eq!(
            simple.date_style,
            crate::config::SimpleDatetimeFormat::Medium
        );
        assert_eq!(
            simple.time_style,
            crate::config::SimpleDatetimeFormat::Disabled
        );
    }

    #[test]
    fn canonicalize_defaults_recurses_and_drops_empty_groups() {
        let schema = ColumnConfigSchema {
            fields: vec![
                group("ok", vec![color("good", "RGB(255,0,0)"), flag("flag")]),
                group("doomed", vec![color("bad", "not-a-color")]),
            ],
        }
        .canonicalize_defaults();

        assert_eq!(schema.fields.len(), 1);
        let ControlSpec::Group { key, fields } = &schema.fields[0] else {
            panic!("expected group");
        };

        assert_eq!(key, "ok");
        assert!(matches!(
            &fields[0],
            ControlSpec::Color { default, .. } if default == "#ff0000"
        ));
    }
}