gradio-pr-bot commited on
Commit
480fa83
·
verified ·
1 Parent(s): fd7b7d9

Upload folder using huggingface_hub

Browse files
5.49.1/plot/Index.svelte ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script context="module" lang="ts">
2
+ export { default as BasePlot } from "./shared/Plot.svelte";
3
+ </script>
4
+
5
+ <script lang="ts">
6
+ import type { Gradio, SelectData } from "@gradio/utils";
7
+ import Plot from "./shared/Plot.svelte";
8
+
9
+ import {
10
+ Block,
11
+ BlockLabel,
12
+ FullscreenButton,
13
+ IconButtonWrapper
14
+ } from "@gradio/atoms";
15
+ import { Plot as PlotIcon } from "@gradio/icons";
16
+
17
+ import { StatusTracker } from "@gradio/statustracker";
18
+ import type { LoadingStatus } from "@gradio/statustracker";
19
+
20
+ type ThemeMode = "system" | "light" | "dark";
21
+
22
+ export let value: null | string = null;
23
+ export let elem_id = "";
24
+ export let elem_classes: string[] = [];
25
+ export let visible: boolean | "hidden" = true;
26
+ export let loading_status: LoadingStatus;
27
+ export let label: string;
28
+ export let show_label: boolean;
29
+ export let container = true;
30
+ export let scale: number | null = null;
31
+ export let min_width: number | undefined = undefined;
32
+ export let theme_mode: ThemeMode;
33
+ export let caption: string;
34
+ export let bokeh_version: string | null;
35
+ export let gradio: Gradio<{
36
+ change: never;
37
+ clear_status: LoadingStatus;
38
+ select: SelectData;
39
+ }>;
40
+ export let show_actions_button = false;
41
+ export let _selectable = false;
42
+ export let x_lim: [number, number] | null = null;
43
+ export let show_fullscreen_button = false;
44
+ let fullscreen = false;
45
+ </script>
46
+
47
+ <Block
48
+ padding={false}
49
+ {elem_id}
50
+ {elem_classes}
51
+ {visible}
52
+ {container}
53
+ {scale}
54
+ {min_width}
55
+ allow_overflow={false}
56
+ bind:fullscreen
57
+ >
58
+ <BlockLabel
59
+ {show_label}
60
+ label={label || gradio.i18n("plot.plot")}
61
+ Icon={PlotIcon}
62
+ />
63
+ {#if show_fullscreen_button}
64
+ <IconButtonWrapper>
65
+ <FullscreenButton
66
+ {fullscreen}
67
+ on:fullscreen={({ detail }) => {
68
+ fullscreen = detail;
69
+ }}
70
+ />
71
+ </IconButtonWrapper>
72
+ {/if}
73
+ <StatusTracker
74
+ autoscroll={gradio.autoscroll}
75
+ i18n={gradio.i18n}
76
+ {...loading_status}
77
+ on:clear_status={() => gradio.dispatch("clear_status", loading_status)}
78
+ />
79
+ <Plot
80
+ {value}
81
+ {theme_mode}
82
+ {caption}
83
+ {bokeh_version}
84
+ {show_actions_button}
85
+ {gradio}
86
+ {show_label}
87
+ {_selectable}
88
+ {x_lim}
89
+ on:change={() => gradio.dispatch("change")}
90
+ on:select={(e) => gradio.dispatch("select", e.detail)}
91
+ />
92
+ </Block>
5.49.1/plot/package.json ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/plot",
3
+ "version": "0.9.24",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "dependencies": {
10
+ "@gradio/atoms": "workspace:^",
11
+ "@gradio/icons": "workspace:^",
12
+ "@gradio/statustracker": "workspace:^",
13
+ "@gradio/theme": "workspace:^",
14
+ "@gradio/utils": "workspace:^",
15
+ "@rollup/plugin-json": "^6.0.0",
16
+ "plotly.js-dist-min": "^3.0.0",
17
+ "vega": "^5.23.0",
18
+ "vega-embed": "^6.25.0",
19
+ "vega-lite": "^5.12.0"
20
+ },
21
+ "devDependencies": {
22
+ "@gradio/preview": "workspace:^"
23
+ },
24
+ "main": "./Index.svelte",
25
+ "main_changeset": true,
26
+ "exports": {
27
+ "./package.json": "./package.json",
28
+ ".": {
29
+ "gradio": "./Index.svelte",
30
+ "svelte": "./dist/Index.svelte",
31
+ "types": "./dist/Index.svelte.d.ts"
32
+ },
33
+ "./base": {
34
+ "gradio": "./shared/Plot.svelte",
35
+ "svelte": "./dist/shared/Plot.svelte",
36
+ "types": "./dist/shared/Plot.svelte.d.ts"
37
+ }
38
+ },
39
+ "peerDependencies": {
40
+ "svelte": "^4.0.0"
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/gradio-app/gradio.git",
45
+ "directory": "js/plot"
46
+ }
47
+ }
5.49.1/plot/shared/Plot.svelte ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ //@ts-nocheck
3
+ import { Plot as PlotIcon } from "@gradio/icons";
4
+ import { Empty } from "@gradio/atoms";
5
+ import type { ThemeMode } from "js/core/src/components/types";
6
+ import type { Gradio, SelectData } from "@gradio/utils";
7
+ import { createEventDispatcher } from "svelte";
8
+
9
+ export let value;
10
+ let _value;
11
+ export let colors: string[] = [];
12
+ export let show_label: boolean;
13
+ export let theme_mode: ThemeMode;
14
+ export let caption: string;
15
+ export let bokeh_version: string | null;
16
+ export let show_actions_button: bool;
17
+ export let gradio: Gradio<{
18
+ select: SelectData;
19
+ }>;
20
+ export let x_lim: [number, number] | null = null;
21
+ export let _selectable: boolean;
22
+
23
+ let PlotComponent: any = null;
24
+ let _type = value?.type;
25
+ let loaded_plotly_css = false;
26
+
27
+ const dispatch = createEventDispatcher<{
28
+ change: undefined;
29
+ }>();
30
+
31
+ const plotTypeMapping = {
32
+ plotly: () => import("./plot_types/PlotlyPlot.svelte"),
33
+ bokeh: () => import("./plot_types/BokehPlot.svelte"),
34
+ altair: () => import("./plot_types/AltairPlot.svelte"),
35
+ matplotlib: () => import("./plot_types/MatplotlibPlot.svelte")
36
+ };
37
+
38
+ let loadedPlotTypeMapping = {};
39
+
40
+ const is_browser = typeof window !== "undefined";
41
+ let key = 0;
42
+
43
+ $: if (value !== _value) {
44
+ key += 1;
45
+ let type = value?.type;
46
+ if (type !== _type) {
47
+ PlotComponent = null;
48
+ }
49
+ if (type && type in plotTypeMapping && is_browser) {
50
+ if (loadedPlotTypeMapping[type]) {
51
+ PlotComponent = loadedPlotTypeMapping[type];
52
+ } else {
53
+ plotTypeMapping[type]().then((module) => {
54
+ PlotComponent = module.default;
55
+ loadedPlotTypeMapping[type] = PlotComponent;
56
+ });
57
+ }
58
+ }
59
+ _value = value;
60
+ _type = type;
61
+ dispatch("change");
62
+ }
63
+ </script>
64
+
65
+ {#if value && PlotComponent}
66
+ {#key key}
67
+ <svelte:component
68
+ this={PlotComponent}
69
+ {value}
70
+ {colors}
71
+ {theme_mode}
72
+ {show_label}
73
+ {caption}
74
+ {bokeh_version}
75
+ {show_actions_button}
76
+ {gradio}
77
+ {_selectable}
78
+ {x_lim}
79
+ bind:loaded_plotly_css
80
+ on:load
81
+ on:select
82
+ />
83
+ {/key}
84
+ {:else}
85
+ <Empty unpadded_box={true} size="large"><PlotIcon /></Empty>
86
+ {/if}
5.49.1/plot/shared/plot_types/AltairPlot.svelte ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ //@ts-nocheck
3
+ import { set_config } from "./altair_utils";
4
+ import { onMount, onDestroy } from "svelte";
5
+ import type { TopLevelSpec as Spec } from "vega-lite";
6
+ import vegaEmbed from "vega-embed";
7
+ import type { Gradio, SelectData } from "@gradio/utils";
8
+ import type { View } from "vega";
9
+
10
+ export let value;
11
+ export let colors: string[] = [];
12
+ export let caption: string;
13
+ export let show_actions_button: bool;
14
+ export let gradio: Gradio<{
15
+ select: SelectData;
16
+ }>;
17
+ let element: HTMLElement;
18
+ let parent_element: HTMLElement;
19
+ let view: View;
20
+ export let _selectable: bool;
21
+
22
+ let computed_style = window.getComputedStyle(document.body);
23
+
24
+ let old_spec: Spec;
25
+ let spec_width: number;
26
+ $: plot = value?.plot;
27
+ $: spec = JSON.parse(plot) as Spec;
28
+ $: if (spec && spec.params && !_selectable) {
29
+ spec.params = spec.params.filter((param) => param.name !== "brush");
30
+ }
31
+ $: if (old_spec !== spec) {
32
+ old_spec = spec;
33
+ spec_width = spec.width;
34
+ }
35
+
36
+ $: if (value.chart) {
37
+ spec = set_config(spec, computed_style, value.chart as string, colors);
38
+ }
39
+ $: fit_width_to_parent =
40
+ spec.encoding?.column?.field ||
41
+ spec.encoding?.row?.field ||
42
+ value.chart === undefined
43
+ ? false
44
+ : true; // vega seems to glitch with width when orientation is set
45
+
46
+ const get_width = (): number => {
47
+ return Math.min(
48
+ parent_element.offsetWidth,
49
+ spec_width || parent_element.offsetWidth
50
+ );
51
+ };
52
+ let resize_callback = (): void => {};
53
+ const renderPlot = (): void => {
54
+ if (fit_width_to_parent) {
55
+ spec.width = get_width();
56
+ }
57
+ vegaEmbed(element, spec, { actions: show_actions_button }).then(
58
+ function (result): void {
59
+ view = result.view;
60
+ resize_callback = () => {
61
+ view.signal("width", get_width()).run();
62
+ };
63
+
64
+ if (!_selectable) return;
65
+ const callback = (event, item): void => {
66
+ const brushValue = view.signal("brush");
67
+ if (brushValue) {
68
+ if (Object.keys(brushValue).length === 0) {
69
+ gradio.dispatch("select", {
70
+ value: null,
71
+ index: null,
72
+ selected: false
73
+ });
74
+ } else {
75
+ const key = Object.keys(brushValue)[0];
76
+ let range: [number, number] = brushValue[key].map(
77
+ (x) => x / 1000
78
+ );
79
+ gradio.dispatch("select", {
80
+ value: brushValue,
81
+ index: range,
82
+ selected: true
83
+ });
84
+ }
85
+ }
86
+ };
87
+ view.addEventListener("mouseup", callback);
88
+ view.addEventListener("touchup", callback);
89
+ }
90
+ );
91
+ };
92
+ let resizeObserver = new ResizeObserver(() => {
93
+ if (fit_width_to_parent && spec.width !== parent_element.offsetWidth) {
94
+ resize_callback();
95
+ }
96
+ });
97
+ onMount(() => {
98
+ renderPlot();
99
+ resizeObserver.observe(parent_element);
100
+ });
101
+ onDestroy(() => {
102
+ resizeObserver.disconnect();
103
+ });
104
+ </script>
105
+
106
+ <div data-testid={"altair"} class="altair layout" bind:this={parent_element}>
107
+ <div bind:this={element}></div>
108
+ {#if caption}
109
+ <div class="caption layout">
110
+ {caption}
111
+ </div>
112
+ {/if}
113
+ </div>
114
+
115
+ <style>
116
+ .altair :global(canvas) {
117
+ padding: 6px;
118
+ }
119
+ .altair :global(.vega-embed) {
120
+ padding: 0px !important;
121
+ }
122
+ .altair :global(.vega-actions) {
123
+ right: 0px !important;
124
+ }
125
+ .layout {
126
+ display: flex;
127
+ flex-direction: column;
128
+ justify-content: center;
129
+ align-items: center;
130
+ width: var(--size-full);
131
+ height: var(--size-full);
132
+ color: var(--body-text-color);
133
+ }
134
+ .altair {
135
+ display: flex;
136
+ flex-direction: column;
137
+ justify-content: center;
138
+ align-items: center;
139
+ width: var(--size-full);
140
+ height: var(--size-full);
141
+ }
142
+ .caption {
143
+ font-size: var(--text-sm);
144
+ margin-bottom: 6px;
145
+ }
146
+ :global(#vg-tooltip-element) {
147
+ font-family: var(--font) !important;
148
+ font-size: var(--text-xs) !important;
149
+ box-shadow: none !important;
150
+ background-color: var(--block-background-fill) !important;
151
+ border: 1px solid var(--border-color-primary) !important;
152
+ color: var(--body-text-color) !important;
153
+ }
154
+ :global(#vg-tooltip-element .key) {
155
+ color: var(--body-text-color-subdued) !important;
156
+ }
157
+ </style>
5.49.1/plot/shared/plot_types/BokehPlot.svelte ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ //@ts-nocheck
3
+ import { onDestroy, createEventDispatcher } from "svelte";
4
+
5
+ export let value;
6
+ export let bokeh_version: string | null;
7
+ const div_id = `bokehDiv-${Math.random().toString(5).substring(2)}`;
8
+ const dispatch = createEventDispatcher<{ load: undefined }>();
9
+ $: plot = value?.plot;
10
+
11
+ async function embed_bokeh(_plot: Record<string, any>): void {
12
+ if (document) {
13
+ if (document.getElementById(div_id)) {
14
+ document.getElementById(div_id).innerHTML = "";
15
+ }
16
+ }
17
+ if (window.Bokeh) {
18
+ load_bokeh();
19
+ let plotObj = JSON.parse(_plot);
20
+ const y = await window.Bokeh.embed.embed_item(plotObj, div_id);
21
+ y._roots.forEach(async (p) => {
22
+ await p.ready;
23
+ dispatch("load");
24
+ });
25
+ }
26
+ }
27
+
28
+ $: loaded && embed_bokeh(plot);
29
+
30
+ const main_src = `https://cdn.bokeh.org/bokeh/release/bokeh-${bokeh_version}.min.js`;
31
+
32
+ const plugins_src = [
33
+ `https://cdn.pydata.org/bokeh/release/bokeh-widgets-${bokeh_version}.min.js`,
34
+ `https://cdn.pydata.org/bokeh/release/bokeh-tables-${bokeh_version}.min.js`,
35
+ `https://cdn.pydata.org/bokeh/release/bokeh-gl-${bokeh_version}.min.js`,
36
+ `https://cdn.pydata.org/bokeh/release/bokeh-api-${bokeh_version}.min.js`
37
+ ];
38
+
39
+ let loaded = false;
40
+ async function load_plugins(): HTMLScriptElement[] {
41
+ await Promise.all(
42
+ plugins_src.map((src, i) => {
43
+ return new Promise((resolve) => {
44
+ const script = document.createElement("script");
45
+ script.onload = resolve;
46
+ script.src = src;
47
+ document.head.appendChild(script);
48
+ return script;
49
+ });
50
+ })
51
+ );
52
+
53
+ loaded = true;
54
+ }
55
+
56
+ let plugin_scripts = [];
57
+
58
+ function handle_bokeh_loaded(): void {
59
+ plugin_scripts = load_plugins();
60
+ }
61
+
62
+ function load_bokeh(): HTMLScriptElement {
63
+ const script = document.createElement("script");
64
+ script.onload = handle_bokeh_loaded;
65
+ script.src = main_src;
66
+ const is_bokeh_script_present = document.head.querySelector(
67
+ `script[src="${main_src}"]`
68
+ );
69
+ if (!is_bokeh_script_present) {
70
+ document.head.appendChild(script);
71
+ } else {
72
+ handle_bokeh_loaded();
73
+ }
74
+ return script;
75
+ }
76
+
77
+ const main_script = bokeh_version ? load_bokeh() : null;
78
+
79
+ onDestroy(() => {
80
+ if (main_script in document.children) {
81
+ document.removeChild(main_script);
82
+ plugin_scripts.forEach((child) => document.removeChild(child));
83
+ }
84
+ });
85
+ </script>
86
+
87
+ <div data-testid={"bokeh"} id={div_id} class="gradio-bokeh" />
88
+
89
+ <style>
90
+ .gradio-bokeh {
91
+ display: flex;
92
+ justify-content: center;
93
+ }
94
+ </style>
5.49.1/plot/shared/plot_types/MatplotlibPlot.svelte ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let value;
3
+
4
+ $: plot = value?.plot;
5
+ </script>
6
+
7
+ <div data-testid={"matplotlib"} class="matplotlib layout">
8
+ <img
9
+ src={plot}
10
+ alt={`${value.chart} plot visualising provided data`}
11
+ on:load
12
+ />
13
+ </div>
14
+
15
+ <style>
16
+ .layout {
17
+ display: flex;
18
+ flex-direction: column;
19
+ justify-content: center;
20
+ align-items: center;
21
+ width: var(--size-full);
22
+ height: var(--size-full);
23
+ color: var(--body-text-color);
24
+ }
25
+ .matplotlib img {
26
+ object-fit: contain;
27
+ }
28
+ </style>
5.49.1/plot/shared/plot_types/PlotlyPlot.svelte ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ //@ts-nocheck
3
+ import Plotly from "plotly.js-dist-min";
4
+ import { afterUpdate, createEventDispatcher } from "svelte";
5
+
6
+ export let value;
7
+ export let show_label: boolean;
8
+ export let loaded_plotly_css = false;
9
+
10
+ $: plot = value?.plot;
11
+
12
+ let plot_div;
13
+ let plotly_global_style;
14
+
15
+ const dispatch = createEventDispatcher<{ load: undefined }>();
16
+
17
+ function load_plotly_css(): void {
18
+ if (!loaded_plotly_css) {
19
+ plotly_global_style = document.getElementById("plotly.js-style-global");
20
+ const plotly_style_clone = plotly_global_style.cloneNode();
21
+ plot_div.appendChild(plotly_style_clone);
22
+ for (const rule of plotly_global_style.sheet.cssRules) {
23
+ plotly_style_clone.sheet.insertRule(rule.cssText);
24
+ }
25
+ loaded_plotly_css = true;
26
+ }
27
+ }
28
+
29
+ afterUpdate(async () => {
30
+ load_plotly_css();
31
+
32
+ let plotObj = JSON.parse(plot);
33
+
34
+ // the docs aren't very good but this works
35
+ plotObj.config = plotObj.config || {};
36
+ plotObj.config.responsive = true;
37
+ plotObj.responsive = true;
38
+ plotObj.layout.autosize = true;
39
+
40
+ if (plotObj.layout.margin == undefined) {
41
+ plotObj.layout.margin = {};
42
+ }
43
+ if (plotObj.layout.title && show_label) {
44
+ // so title does not get cut off by label
45
+ plotObj.layout.margin.t = Math.max(100, plotObj.layout.margin.t || 0);
46
+ }
47
+ plotObj.layout.margin.autoexpand = true;
48
+
49
+ Plotly.react(plot_div, plotObj.data, plotObj.layout, plotObj.config);
50
+ Plotly.Plots.resize(plot_div);
51
+
52
+ plot_div.on("plotly_afterplot", () => {
53
+ dispatch("load");
54
+ });
55
+ });
56
+ </script>
57
+
58
+ <div data-testid={"plotly"} bind:this={plot_div} />
5.49.1/plot/shared/plot_types/altair_utils.ts ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { colors as color_palette } from "@gradio/theme";
2
+ import { get_next_color } from "@gradio/utils";
3
+ import type { Config, TopLevelSpec as Spec } from "vega-lite";
4
+
5
+ export function set_config(
6
+ spec: Spec,
7
+ computed_style: CSSStyleDeclaration,
8
+ chart_type: string,
9
+ colors: string[]
10
+ ): Spec {
11
+ let accentColor = computed_style.getPropertyValue("--color-accent");
12
+ let bodyTextColor = computed_style.getPropertyValue("--body-text-color");
13
+ let borderColorPrimary = computed_style.getPropertyValue(
14
+ "--border-color-primary"
15
+ );
16
+ let fontFamily = computed_style.fontFamily;
17
+ let titleWeight = computed_style.getPropertyValue(
18
+ "--block-title-text-weight"
19
+ ) as "bold" | "normal" | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900;
20
+ const fontToPxVal = (font: string): number => {
21
+ return font.endsWith("px") ? parseFloat(font.slice(0, -2)) : 12;
22
+ };
23
+ let textSizeMd = fontToPxVal(computed_style.getPropertyValue("--text-md"));
24
+ let textSizeSm = fontToPxVal(computed_style.getPropertyValue("--text-sm"));
25
+ let config: Config = {
26
+ autosize: { type: "fit", contains: "padding" },
27
+ axis: {
28
+ labelFont: fontFamily,
29
+ labelColor: bodyTextColor,
30
+ titleFont: fontFamily,
31
+ titleColor: bodyTextColor,
32
+ tickColor: borderColorPrimary,
33
+ labelFontSize: textSizeSm,
34
+ gridColor: borderColorPrimary,
35
+ titleFontWeight: "normal",
36
+ titleFontSize: textSizeSm,
37
+ labelFontWeight: "normal",
38
+ domain: false,
39
+ labelAngle: 0
40
+ },
41
+ legend: {
42
+ labelColor: bodyTextColor,
43
+ labelFont: fontFamily,
44
+ titleColor: bodyTextColor,
45
+ titleFont: fontFamily,
46
+ titleFontWeight: "normal",
47
+ titleFontSize: textSizeSm,
48
+ labelFontWeight: "normal",
49
+ offset: 2
50
+ },
51
+ title: {
52
+ color: bodyTextColor,
53
+ font: fontFamily,
54
+ fontSize: textSizeMd,
55
+ fontWeight: titleWeight,
56
+ anchor: "middle"
57
+ },
58
+ view: {
59
+ stroke: borderColorPrimary
60
+ }
61
+ };
62
+ spec.config = config;
63
+ // @ts-ignore (unsure why the following are not typed in Spec)
64
+ let encoding: any = spec.encoding;
65
+ // @ts-ignore
66
+ let layer: any = spec.layer;
67
+ switch (chart_type) {
68
+ case "scatter":
69
+ spec.config.mark = { stroke: accentColor };
70
+ if (encoding.color && encoding.color.type == "nominal") {
71
+ encoding.color.scale.range = encoding.color.scale.range.map(
72
+ (_: string, i: number) => get_color(colors, i)
73
+ );
74
+ } else if (encoding.color && encoding.color.type == "quantitative") {
75
+ encoding.color.scale.range = ["#eff6ff", "#1e3a8a"];
76
+ encoding.color.scale.range.interpolate = "hsl";
77
+ }
78
+ break;
79
+ case "line":
80
+ spec.config.mark = { stroke: accentColor, cursor: "crosshair" };
81
+ layer.forEach((d: any) => {
82
+ if (d.encoding.color) {
83
+ d.encoding.color.scale.range = d.encoding.color.scale.range.map(
84
+ (_: any, i: any) => get_color(colors, i)
85
+ );
86
+ }
87
+ });
88
+ break;
89
+ case "bar":
90
+ spec.config.mark = { opacity: 0.8, fill: accentColor };
91
+ if (encoding.color) {
92
+ encoding.color.scale.range = encoding.color.scale.range.map(
93
+ (_: any, i: any) => get_color(colors, i)
94
+ );
95
+ }
96
+ break;
97
+ }
98
+ return spec;
99
+ }
100
+
101
+ function get_color(colors: string[], index: number): string {
102
+ let current_color = colors[index % colors.length];
103
+
104
+ if (current_color && current_color in color_palette) {
105
+ return color_palette[current_color as keyof typeof color_palette]?.primary;
106
+ } else if (!current_color) {
107
+ return color_palette[get_next_color(index) as keyof typeof color_palette]
108
+ .primary;
109
+ }
110
+ return current_color;
111
+ }