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
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ 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). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

mod agg_depth_selector;
pub(crate) mod primitive_field;
mod symbol;

use std::collections::{BTreeMap, HashMap};
use std::rc::Rc;

use itertools::Itertools;
use perspective_client::config::ColumnType;
use yew::{Callback, Html, Properties, function_component, html};

use self::agg_depth_selector::*;
use self::primitive_field::{
    BoolField, ColorField, EnumField, GradientStopsField, NumberFieldPrimitive, PaletteField,
};
use crate::components::column_settings_sidebar::style_tab::symbol::SymbolStyle;
use crate::components::containers::control_group::ControlGroup;
use crate::components::datetime_column_style::DatetimeColumnStyle;
use crate::components::number_series_style::NumberSeriesStyle;
use crate::components::string_column_style::StringColumnStyle;
use crate::components::style_controls::CustomNumberFormat;
use crate::config::{
    ColumnConfigFieldUpdate, ControlSpec, CssKind, CustomNumberFormatConfig,
    DatetimeColumnStyleConfig, NamedValue, NumberFormatDefaults, NumberSeriesStyleConfig,
    StringColumnStyleConfig,
};
use crate::presentation::Presentation;
use crate::queries::{fetch_column_abs_max, get_column_config_schema, named_values};
use crate::renderer::Renderer;
use crate::session::Session;
use crate::tasks::send_column_config;
use crate::utils::PtrEqRc;
use crate::workspace::Workspace;

#[derive(Clone, PartialEq, Properties)]
pub struct StyleTabProps {
    pub ty: Option<ColumnType>,
    pub column_name: String,
    pub group_by_depth: u32,

    /// View config snapshot — threaded from parent.
    pub view_config: PtrEqRc<perspective_client::config::ViewConfig>,

    /// Session metadata snapshot — threaded from parent.
    pub metadata: PtrEqRc<crate::session::SessionMetadata>,

    /// Per-column stats snapshot — threaded from `SessionProps`.
    pub column_stats: PtrEqRc<HashMap<String, crate::session::ColumnStats>>,

    /// Selected theme name, threaded for PortalModal consumers.
    pub selected_theme: Option<String>,

    // State
    pub presentation: Presentation,
    pub renderer: Renderer,
    pub session: Session,
    pub workspace: Workspace,
}

#[function_component]
pub fn StyleTab(props: &StyleTabProps) -> Html {
    // Bumped on every primitive field change so Yew re-renders the tab
    // and re-queries `column_config_schema` with the new value. Without
    // this, dynamic field gating (e.g. show `Color` only when
    // `string_color_mode != none`) wouldn't surface until the user
    // closed and reopened the sidebar.
    let revision = yew::use_state(|| 0u32);

    // `abs_max` lives in `Session`'s shared cache, propagated as a
    // value-semantic snapshot through `props.column_stats`. The cache
    // is cleared on `view_config_changed`; on cache miss we spawn the
    // fetch and let `column_stats_changed` drive the re-render via
    // `SessionProps`.
    let abs_max = props
        .column_stats
        .get(&props.column_name)
        .and_then(|s| s.abs_max);

    yew::use_effect_with((props.column_name.clone(), props.view_config.clone()), {
        let session = props.session.clone();
        let column_name = props.column_name.clone();
        move |_| {
            fetch_column_abs_max(&session, column_name);
            || ()
        }
    });

    let raw_config = props.renderer.get_columns_config(&props.column_name);

    let named_for = |kind| Rc::new(named_values(&props.workspace, &props.presentation, kind));
    let named_gradients = named_for(CssKind::Gradient);
    let named_palettes = named_for(CssKind::Palette);
    let named_colors = named_for(CssKind::Color);

    let restored = Rc::new(props.presentation.palette());
    let on_pin = {
        let presentation = props.presentation.clone();
        let revision = revision.clone();
        yew::Callback::from(move |(kind, literal): (CssKind, String)| {
            if let Err(error) = presentation.pin_style(kind, &literal) {
                tracing::warn!("Failed to pin named {kind:?}: {error:?}");
            }

            revision.set(*revision + 1);
        })
    };
    let schema = get_column_config_schema(
        &props.renderer,
        &props.view_config,
        &props.metadata,
        &props.column_name,
        raw_config.as_ref(),
        abs_max,
    );

    let on_change = {
        let state = props.clone();
        let column_name = props.column_name.clone();
        let revision = revision.clone();
        yew::Callback::from(move |config: crate::config::ColumnConfigFieldUpdate| {
            send_column_config(&state.session, &state.renderer, &column_name, config);
            revision.set(*revision + 1);
        })
    };

    let on_group_toggle = {
        let presentation = props.presentation.clone();
        yew::Callback::from(move |(key, open): (String, bool)| {
            presentation.set_control_group_collapsed(&key, !open);
        })
    };

    let ctx = FieldRenderCtx {
        props,
        raw_config: &raw_config,
        on_change: &on_change,
        on_group_toggle: &on_group_toggle,
        on_pin: &on_pin,
        named_colors: &named_colors,
        named_palettes: &named_palettes,
        named_gradients: &named_gradients,
        restored: &restored,
    };

    let components = schema
        .map(|schema| render_specs(schema.fields, &ctx))
        .unwrap_or_else(|error| {
            tracing::error!("{}", error);
            vec![]
        });

    html! {
        <div id="style-tab">
            <div id="column-style-container" class="tab-section">{ components }</div>
        </div>
    }
}

fn deser_sub<T: serde::de::DeserializeOwned>(
    raw: &Option<serde_json::Map<String, serde_json::Value>>,
) -> Option<T> {
    raw.as_ref()
        .and_then(|m| serde_json::from_value::<T>(serde_json::Value::Object(m.clone())).ok())
}

struct FieldRenderCtx<'a> {
    props: &'a StyleTabProps,
    raw_config: &'a Option<serde_json::Map<String, serde_json::Value>>,
    on_change: &'a Callback<ColumnConfigFieldUpdate>,
    on_group_toggle: &'a Callback<(String, bool)>,
    on_pin: &'a Callback<(CssKind, String)>,
    named_colors: &'a Rc<Vec<NamedValue>>,
    named_palettes: &'a Rc<Vec<NamedValue>>,
    named_gradients: &'a Rc<Vec<NamedValue>>,
    restored: &'a Rc<BTreeMap<String, String>>,
}

fn render_specs(specs: Vec<ControlSpec>, ctx: &FieldRenderCtx) -> Vec<Html> {
    specs
        .into_iter()
        .filter_map(|spec| match spec {
            ControlSpec::Group { key, fields } => {
                let children = render_specs(fields, ctx);
                (!children.is_empty()).then(|| {
                    let open = !ctx.props.presentation.is_control_group_collapsed(&key);

                    html! {
                        <ControlGroup
                            key={format!("{}::group::{}", ctx.props.column_name, key)}
                            group_key={key.clone()}
                            {open}
                            on_toggle={ctx.on_group_toggle.clone()}
                        >
                            { children }
                        </ControlGroup>
                    }
                })
            },
            spec => {
                let keys: Vec<String> = spec
                    .serialized_keys()
                    .into_iter()
                    .map(|s| s.to_string())
                    .collect();

                let component = render_leaf(spec, &keys, ctx)?;
                let key = format!("{}::{}", ctx.props.column_name, keys.join("+"));
                Some(html! { <fieldset class="style-control" {key}>{ component }</fieldset> })
            },
        })
        .collect_vec()
}

fn render_leaf(spec: ControlSpec, keys: &[String], ctx: &FieldRenderCtx) -> Option<Html> {
    let props = ctx.props;
    let raw_config = ctx.raw_config;
    let on_change = ctx.on_change;
    let component = match spec {
        ControlSpec::AggregateDepth => {
            let aggregate_depth = raw_config
                .as_ref()
                .and_then(|m| m.get("aggregate_depth"))
                .and_then(|v| v.as_u64())
                .unwrap_or(0) as u32;
            html! {
                <AggregateDepthSelector
                    group_by_depth={props.group_by_depth}
                    on_change={on_change.clone()}
                    column_name={props.column_name.to_owned()}
                    value={aggregate_depth}
                    keys={keys.to_vec()}
                />
            }
        },
        ControlSpec::NumberSeriesStyle {
            default: default_config,
        } => {
            let config: Option<NumberSeriesStyleConfig> = deser_sub(raw_config);
            html! {
                <NumberSeriesStyle
                    {config}
                    {default_config}
                    on_change={on_change.clone()}
                    keys={keys.to_vec()}
                />
            }
        },
        ControlSpec::DatetimeFormat { default } => {
            let config: Option<DatetimeColumnStyleConfig> = deser_sub(raw_config);
            let enable_time_config = props.ty.unwrap() == ColumnType::Datetime;
            let default_format = Rc::new(default.unwrap_or_default());
            html! {
                <DatetimeColumnStyle
                    {enable_time_config}
                    {config}
                    {default_format}
                    on_change={on_change.clone()}
                    keys={keys.to_vec()}
                />
            }
        },
        ControlSpec::StringFormat => {
            let config: Option<StringColumnStyleConfig> = deser_sub(raw_config);
            html! {
                <StringColumnStyle {config} on_change={on_change.clone()} keys={keys.to_vec()} />
            }
        },
        ControlSpec::Symbols {
            default: default_config,
        } => {
            let restored_config: HashMap<String, String> = raw_config
                .as_ref()
                .and_then(|m| m.get("symbols"))
                .and_then(|v| serde_json::from_value(v.clone()).ok())
                .unwrap_or_default();

            html! {
                <SymbolStyle
                    {default_config}
                    restored_config={Some(restored_config)}
                    on_change={on_change.clone()}
                    column_name={props.column_name.clone()}
                    selected_theme={props.selected_theme.clone()}
                    session={props.session.clone()}
                    keys={keys.to_vec()}
                />
            }
        },
        ControlSpec::NumberFormat { default } => {
            let restored_config: CustomNumberFormatConfig = raw_config
                .as_ref()
                .and_then(|m| m.get("number_format"))
                .and_then(|v| serde_json::from_value(v.clone()).ok())
                .unwrap_or_default();

            let view_type = props.ty.unwrap();
            let defaults = Rc::new(NumberFormatDefaults::resolve(
                default.as_ref(),
                view_type == ColumnType::Float,
            ));

            html! {
                <CustomNumberFormat
                    {restored_config}
                    {defaults}
                    on_change={on_change.clone()}
                    {view_type}
                    column_name={props.column_name.clone()}
                    keys={keys.to_vec()}
                />
            }
        },
        ControlSpec::Enum {
            key,
            variants,
            default,
        } => {
            let current = raw_config
                .as_ref()
                .and_then(|m| m.get(&key))
                .and_then(|v| v.as_str().map(|s| s.to_string()));

            html! {
                <EnumField
                    field_key={key}
                    {variants}
                    {default}
                    {current}
                    on_change={on_change.clone()}
                />
            }
        },
        ControlSpec::Bool { key, default } => {
            let current = raw_config
                .as_ref()
                .and_then(|m| m.get(&key))
                .and_then(|v| v.as_bool());
            html! { <BoolField field_key={key} {default} {current} on_change={on_change.clone()} /> }
        },
        ControlSpec::Color { key, default } => {
            let current = raw_config
                .as_ref()
                .and_then(|m| m.get(&key))
                .and_then(|v| v.as_str().map(|s| s.to_string()));
            html! {
                <ColorField
                    field_key={key}
                    {default}
                    {current}
                    named={ctx.named_colors.clone()}
                    restored={ctx.restored.clone()}
                    on_pin={ctx.on_pin.clone()}
                    on_change={on_change.clone()}
                />
            }
        },
        ControlSpec::Palette { key, default, max } => {
            let current = raw_config
                .as_ref()
                .and_then(|m| m.get(&key))
                .and_then(|v| v.as_str().map(|s| s.to_string()));
            html! {
                <PaletteField
                    field_key={key}
                    {default}
                    {max}
                    {current}
                    named={ctx.named_palettes.clone()}
                    restored={ctx.restored.clone()}
                    on_pin={ctx.on_pin.clone()}
                    on_change={on_change.clone()}
                />
            }
        },
        ControlSpec::GradientStops {
            key,
            default,
            discrete,
        } => {
            let current = raw_config
                .as_ref()
                .and_then(|m| m.get(&key))
                .and_then(|v| v.as_str().map(|s| s.to_string()));
            html! {
                <GradientStopsField
                    field_key={key}
                    {default}
                    {discrete}
                    {current}
                    named={ctx.named_gradients.clone()}
                    restored={ctx.restored.clone()}
                    on_pin={ctx.on_pin.clone()}
                    on_change={on_change.clone()}
                />
            }
        },
        ControlSpec::Number {
            key,
            default,
            min,
            max,
            step,
            include,
        } => {
            let current = raw_config
                .as_ref()
                .and_then(|m| m.get(&key))
                .and_then(|v| v.as_f64());
            html! {
                <NumberFieldPrimitive
                    field_key={key}
                    {default}
                    {current}
                    {min}
                    {max}
                    {step}
                    {include}
                    on_change={on_change.clone()}
                />
            }
        },
        ControlSpec::String { .. } | ControlSpec::Group { .. } => {
            return None;
        },
    };

    Some(component)
}