gradio-pr-bot commited on
Commit
babfda6
·
verified ·
1 Parent(s): 27b0d61

Upload folder using huggingface_hub

Browse files
6.0.0-dev.5/nativeplot/Example.svelte ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let title: string | null;
3
+ export let x: string;
4
+ export let y: string;
5
+ </script>
6
+
7
+ {#if title}
8
+ {title}
9
+ {:else}
10
+ {x} x {y}
11
+ {/if}
6.0.0-dev.5/nativeplot/Index.svelte ADDED
@@ -0,0 +1,845 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { Gradio } from "@gradio/utils";
3
+ import { BlockTitle } from "@gradio/atoms";
4
+ import { Block } from "@gradio/atoms";
5
+ import {
6
+ FullscreenButton,
7
+ IconButtonWrapper,
8
+ IconButton
9
+ } from "@gradio/atoms";
10
+ import { StatusTracker } from "@gradio/statustracker";
11
+ import { onMount, untrack } from "svelte";
12
+ import { Download } from "@gradio/icons";
13
+
14
+ import type { TopLevelSpec as Spec } from "vega-lite";
15
+ import type { View } from "vega";
16
+ import { LineChart as LabelIcon } from "@gradio/icons";
17
+ import { Empty } from "@gradio/atoms";
18
+ import type { NativePlotProps, NativePlotEvents, PlotData } from "./types";
19
+
20
+ let props = $props();
21
+ const gradio = new Gradio<NativePlotEvents, NativePlotProps>(props);
22
+
23
+ let unique_colors = $derived(
24
+ gradio.props.color &&
25
+ gradio.props.value &&
26
+ gradio.props.value.datatypes[gradio.props.color] === "nominal"
27
+ ? Array.from(new Set(_data.map((d) => d[gradio.props.color!])))
28
+ : []
29
+ );
30
+
31
+ let x_lim = $derived(gradio.props.x_lim || null); // for some unknown reason, x_lim was getting set to undefined when used in re-render, so this line is needed
32
+ let y_lim = $derived(gradio.props.y_lim || null);
33
+ let x_start = $derived(x_lim?.[0] !== null ? x_lim?.[0] : undefined);
34
+ let x_end = $derived(x_lim?.[1] !== null ? x_lim?.[1] : undefined);
35
+ let y_start = $derived(y_lim?.[0] !== null ? y_lim?.[0] : undefined);
36
+ let y_end = $derived(y_lim?.[1] !== null ? y_lim?.[1] : undefined);
37
+
38
+ let fullscreen = $state(false);
39
+
40
+ function reformat_sort(
41
+ _sort: NativePlotProps["sort"]
42
+ ):
43
+ | string
44
+ | "ascending"
45
+ | "descending"
46
+ | { field: string; order: "ascending" | "descending" }
47
+ | string[]
48
+ | null
49
+ | undefined {
50
+ if (_sort === "x") {
51
+ return "ascending";
52
+ } else if (_sort === "-x") {
53
+ return "descending";
54
+ } else if (_sort === "y") {
55
+ return { field: gradio.props.y, order: "ascending" };
56
+ } else if (_sort === "-y") {
57
+ return { field: gradio.props.y, order: "descending" };
58
+ } else if (_sort === null) {
59
+ return null;
60
+ } else if (Array.isArray(_sort)) {
61
+ return _sort;
62
+ }
63
+ }
64
+ let _sort = $derived(reformat_sort(gradio.props.sort));
65
+
66
+ let _data: {
67
+ [x: string]: string | number;
68
+ }[] = $state([]);
69
+
70
+ function escape_field_name(fieldName: string): string {
71
+ // Escape special characters in field names according to Vega-Lite spec:
72
+ // https://vega.github.io/vega-lite/docs/field.html
73
+ return fieldName
74
+ .replace(/\./g, "\\.")
75
+ .replace(/\[/g, "\\[")
76
+ .replace(/\]/g, "\\]");
77
+ }
78
+
79
+ let x_temporal = $derived(
80
+ gradio.props.value &&
81
+ gradio.props.value.datatypes[gradio.props.x] === "temporal"
82
+ );
83
+ let _x_lim = $derived(
84
+ x_temporal
85
+ ? [
86
+ x_start !== undefined ? x_start * 1000 : null,
87
+ x_end !== undefined ? x_end * 1000 : null
88
+ ]
89
+ : x_lim
90
+ );
91
+ let mouse_down_on_chart = $state(false);
92
+ const SUFFIX_DURATION: Record<string, number> = {
93
+ s: 1,
94
+ m: 60,
95
+ h: 60 * 60,
96
+ d: 24 * 60 * 60
97
+ };
98
+ let _x_bin = $derived(
99
+ gradio.props.x_bin
100
+ ? typeof gradio.props.x_bin === "string"
101
+ ? 1000 *
102
+ parseInt(
103
+ gradio.props.x_bin.substring(0, gradio.props.x_bin.length - 1)
104
+ ) *
105
+ SUFFIX_DURATION[gradio.props.x_bin[gradio.props.x_bin.length - 1]]
106
+ : gradio.props.x_bin
107
+ : undefined
108
+ );
109
+
110
+ let _y_aggregate = $derived.by(() => {
111
+ if (gradio.props.value) {
112
+ if (gradio.props.value.mark === "point") {
113
+ const aggregating = _x_bin !== undefined;
114
+ return gradio.props.y_aggregate || aggregating ? "sum" : undefined;
115
+ } else {
116
+ return gradio.props.y_aggregate ? gradio.props.y_aggregate : "sum";
117
+ }
118
+ }
119
+ return undefined;
120
+ });
121
+
122
+ let aggregating = $derived.by(() => {
123
+ if (gradio.props.value) {
124
+ if (gradio.props.value.mark === "point") {
125
+ return _x_bin !== undefined;
126
+ } else {
127
+ return (
128
+ _x_bin !== undefined ||
129
+ gradio.props.value.datatypes[gradio.props.x] === "nominal"
130
+ );
131
+ }
132
+ }
133
+ return false;
134
+ });
135
+
136
+ function downsample(
137
+ data: PlotData["data"],
138
+ x_index: number,
139
+ y_index: number,
140
+ color_index: number | null,
141
+ x_start: number | undefined,
142
+ x_end: number | undefined
143
+ ): PlotData["data"] {
144
+ if (
145
+ data.length < 1000 ||
146
+ gradio.props.x_bin !== null ||
147
+ gradio.props.value?.mark !== "line" ||
148
+ gradio.props.value?.datatypes[gradio.props.x] === "nominal"
149
+ ) {
150
+ return data;
151
+ }
152
+ const bin_count = 250;
153
+ let min_max_bins_per_color: Record<
154
+ string,
155
+ [number | null, number, number | null, number][]
156
+ > = {};
157
+ if (x_start === undefined || x_end === undefined) {
158
+ data.forEach((row) => {
159
+ let x_value = row[x_index] as number;
160
+ if (x_start === undefined || x_value < x_start) {
161
+ x_start = x_value;
162
+ }
163
+ if (x_end === undefined || x_value > x_end) {
164
+ x_end = x_value;
165
+ }
166
+ });
167
+ }
168
+ if (x_start === undefined || x_end === undefined) {
169
+ return data;
170
+ }
171
+ const x_range = x_end - x_start;
172
+ const bin_size = x_range / bin_count;
173
+ data.forEach((row, i) => {
174
+ const x_value = row[x_index] as number;
175
+ const y_value = row[y_index] as number;
176
+ const color_value =
177
+ color_index !== null ? (row[color_index] as string) : "any";
178
+ const bin_index = Math.floor((x_value - (x_start as number)) / bin_size);
179
+ if (min_max_bins_per_color[color_value] === undefined) {
180
+ min_max_bins_per_color[color_value] = [];
181
+ }
182
+ min_max_bins_per_color[color_value][bin_index] = min_max_bins_per_color[
183
+ color_value
184
+ ][bin_index] || [
185
+ null,
186
+ Number.POSITIVE_INFINITY,
187
+ null,
188
+ Number.NEGATIVE_INFINITY
189
+ ];
190
+ if (y_value < min_max_bins_per_color[color_value][bin_index][1]) {
191
+ min_max_bins_per_color[color_value][bin_index][0] = i;
192
+ min_max_bins_per_color[color_value][bin_index][1] = y_value;
193
+ }
194
+ if (y_value > min_max_bins_per_color[color_value][bin_index][3]) {
195
+ min_max_bins_per_color[color_value][bin_index][2] = i;
196
+ min_max_bins_per_color[color_value][bin_index][3] = y_value;
197
+ }
198
+ });
199
+ const downsampled_data: PlotData["data"] = [];
200
+ Object.values(min_max_bins_per_color).forEach((bins) => {
201
+ bins.forEach(([min_index, _, max_index, __]) => {
202
+ let indices: number[] = [];
203
+ if (min_index !== null && max_index !== null) {
204
+ indices = [
205
+ Math.min(min_index, max_index),
206
+ Math.max(min_index, max_index)
207
+ ];
208
+ } else if (min_index !== null) {
209
+ indices = [min_index];
210
+ } else if (max_index !== null) {
211
+ indices = [max_index];
212
+ }
213
+ indices.forEach((index) => {
214
+ downsampled_data.push(data[index]);
215
+ });
216
+ });
217
+ });
218
+ return downsampled_data;
219
+ }
220
+ function reformat_data(
221
+ data: PlotData,
222
+ x_start: number | undefined,
223
+ x_end: number | undefined
224
+ ): {
225
+ [x: string]: string | number;
226
+ }[] {
227
+ let x_index = data.columns.indexOf(gradio.props.x);
228
+ let y_index = data.columns.indexOf(gradio.props.y);
229
+ let color_index = gradio.props.color
230
+ ? data.columns.indexOf(gradio.props.color)
231
+ : null;
232
+ let datatable = data.data;
233
+
234
+ if (x_start !== undefined && x_end !== undefined) {
235
+ const time_factor =
236
+ data.datatypes[gradio.props.x] === "temporal" ? 1000 : 1;
237
+ const _x_start = x_start * time_factor;
238
+ const _x_end = x_end * time_factor;
239
+ let largest_before_start: Record<string, [number, number]> = {};
240
+ let smallest_after_end: Record<string, [number, number]> = {};
241
+ const _datatable = datatable.filter((row, i) => {
242
+ const x_value = row[x_index] as number;
243
+ const color_value =
244
+ color_index !== null ? (row[color_index] as string) : "any";
245
+ if (
246
+ x_value < _x_start &&
247
+ (largest_before_start[color_value] === undefined ||
248
+ x_value > largest_before_start[color_value][1])
249
+ ) {
250
+ largest_before_start[color_value] = [i, x_value];
251
+ }
252
+ if (
253
+ x_value > _x_end &&
254
+ (smallest_after_end[color_value] === undefined ||
255
+ x_value < smallest_after_end[color_value][1])
256
+ ) {
257
+ smallest_after_end[color_value] = [i, x_value];
258
+ }
259
+ return x_value >= _x_start && x_value <= _x_end;
260
+ });
261
+ datatable = [
262
+ ...Object.values(largest_before_start).map(([i, _]) => datatable[i]),
263
+ ...downsample(
264
+ _datatable,
265
+ x_index,
266
+ y_index,
267
+ color_index,
268
+ _x_start,
269
+ _x_end
270
+ ),
271
+ ...Object.values(smallest_after_end).map(([i, _]) => datatable[i])
272
+ ];
273
+ } else {
274
+ datatable = downsample(
275
+ datatable,
276
+ x_index,
277
+ y_index,
278
+ color_index,
279
+ undefined,
280
+ undefined
281
+ );
282
+ }
283
+
284
+ if (gradio.props.tooltip == "all" || Array.isArray(gradio.props.tooltip)) {
285
+ return datatable.map((row) => {
286
+ const obj: { [x: string]: string | number } = {};
287
+ data.columns.forEach((col, i) => {
288
+ obj[col] = row[i];
289
+ });
290
+ return obj;
291
+ });
292
+ }
293
+ return datatable.map((row) => {
294
+ const obj = {
295
+ [gradio.props.x]: row[x_index],
296
+ [gradio.props.y]: row[y_index]
297
+ };
298
+ if (gradio.props.color && color_index !== null) {
299
+ obj[gradio.props.color] = row[color_index];
300
+ }
301
+ return obj;
302
+ });
303
+ }
304
+
305
+ $effect(() => {
306
+ console.log("effect 0 run");
307
+ _data = gradio.props.value
308
+ ? reformat_data(gradio.props.value, x_start, x_end)
309
+ : [];
310
+ });
311
+
312
+ let old_value = $state<PlotData | null>(gradio.props.value);
313
+ $effect(() => {
314
+ console.log("effect 1 run");
315
+ if (old_value !== gradio.props.value && view) {
316
+ old_value = gradio.props.value;
317
+ view.data("data", _data).runAsync();
318
+ }
319
+ });
320
+
321
+ const is_browser = typeof window !== "undefined";
322
+ let chart_element = $state<HTMLDivElement>();
323
+ let computed_style = $derived(
324
+ chart_element ? window.getComputedStyle(chart_element) : null
325
+ );
326
+ let view = $state<View>();
327
+ let mounted = $state(false);
328
+ let old_width = $state<number>(0);
329
+ let old_height = $state<number>(0);
330
+ let resizeObserver = $state<ResizeObserver>();
331
+
332
+ let vegaEmbed: typeof import("vega-embed").default;
333
+
334
+ async function load_chart(): Promise<void> {
335
+ if (mouse_down_on_chart) {
336
+ refresh_pending = true;
337
+ return;
338
+ }
339
+ if (view) {
340
+ view.finalize();
341
+ }
342
+ if (!gradio.props.value || !chart_element) return;
343
+ old_width = chart_element.offsetWidth;
344
+ old_height = chart_element.offsetHeight;
345
+ const spec = create_vega_lite_spec();
346
+ if (!spec) return;
347
+ resizeObserver = new ResizeObserver((el) => {
348
+ if (!el[0].target || !(el[0].target instanceof HTMLElement)) return;
349
+ if (
350
+ old_width === 0 &&
351
+ chart_element!.offsetWidth !== 0 &&
352
+ gradio.props.value!.datatypes[gradio.props.x] === "nominal"
353
+ ) {
354
+ // a bug where when a nominal chart is first loaded, the width is 0, it doesn't resize
355
+ load_chart();
356
+ } else {
357
+ const width_change = Math.abs(old_width - el[0].target.offsetWidth);
358
+ const height_change = Math.abs(old_height - el[0].target.offsetHeight);
359
+ if (width_change > 100 || height_change > 100) {
360
+ old_width = el[0].target.offsetWidth;
361
+ old_height = el[0].target.offsetHeight;
362
+ load_chart();
363
+ } else {
364
+ view.signal("width", el[0].target.offsetWidth).run();
365
+ if (fullscreen) {
366
+ view.signal("height", el[0].target.offsetHeight).run();
367
+ }
368
+ }
369
+ }
370
+ });
371
+
372
+ if (!vegaEmbed) {
373
+ vegaEmbed = (await import("vega-embed")).default;
374
+ }
375
+ vegaEmbed(chart_element, spec, { actions: false }).then(function (result) {
376
+ view = result.view;
377
+ resizeObserver!.observe(chart_element!);
378
+ var debounceTimeout: NodeJS.Timeout;
379
+ var lastSelectTime = 0;
380
+ view.addEventListener("dblclick", () => {
381
+ gradio.dispatch("double_click");
382
+ });
383
+ // prevent double-clicks from highlighting text
384
+ chart_element!.addEventListener(
385
+ "mousedown",
386
+ function (e) {
387
+ if (e.detail > 1) {
388
+ e.preventDefault();
389
+ }
390
+ },
391
+ false
392
+ );
393
+ if (gradio.props._selectable) {
394
+ view.addSignalListener("brush", function (_, value) {
395
+ if (Date.now() - lastSelectTime < 1000) return;
396
+ mouse_down_on_chart = true;
397
+ if (Object.keys(value).length === 0) return;
398
+ clearTimeout(debounceTimeout);
399
+ let range: [number, number] = value[Object.keys(value)[0]];
400
+ if (x_temporal) {
401
+ range = [range[0] / 1000, range[1] / 1000];
402
+ }
403
+ debounceTimeout = setTimeout(function () {
404
+ mouse_down_on_chart = false;
405
+ lastSelectTime = Date.now();
406
+ gradio.dispatch("select", {
407
+ value: range,
408
+ index: range,
409
+ selected: true
410
+ });
411
+ if (refresh_pending) {
412
+ refresh_pending = false;
413
+ load_chart();
414
+ }
415
+ }, 250);
416
+ });
417
+ }
418
+ });
419
+ }
420
+
421
+ let refresh_pending = $state(false);
422
+
423
+ onMount(() => {
424
+ mounted = true;
425
+ return () => {
426
+ mounted = false;
427
+ if (view) {
428
+ view.finalize();
429
+ }
430
+ if (resizeObserver) {
431
+ resizeObserver.disconnect();
432
+ }
433
+ };
434
+ });
435
+
436
+ function export_chart(): void {
437
+ if (!view || !computed_style) return;
438
+
439
+ const block_background = computed_style.getPropertyValue(
440
+ "--block-background-fill"
441
+ );
442
+ const export_background = block_background || "white";
443
+
444
+ view.background(export_background).run();
445
+
446
+ view
447
+ .toImageURL("png", 2)
448
+ .then(function (url) {
449
+ view.background("transparent").run();
450
+
451
+ const link = document.createElement("a");
452
+ link.setAttribute("href", url);
453
+ link.setAttribute("download", "chart.png");
454
+ link.style.display = "none";
455
+ document.body.appendChild(link);
456
+ link.click();
457
+ document.body.removeChild(link);
458
+ })
459
+ .catch(function (err) {
460
+ console.error("Export failed:", err);
461
+ view.background("transparent").run();
462
+ });
463
+ }
464
+
465
+ let _color_map = $derived(JSON.stringify(gradio.props.color_map));
466
+
467
+ $effect(() => {
468
+ // Track dependencies to trigger chart reload
469
+ void gradio.props.title;
470
+ void gradio.props.x_title;
471
+ void gradio.props.y_title;
472
+ void gradio.props.color_title;
473
+ void gradio.props.x;
474
+ void gradio.props.y;
475
+ void gradio.props.color;
476
+ void gradio.props.x_bin;
477
+ void _y_aggregate;
478
+ void _color_map;
479
+ void gradio.props.colors_in_legend;
480
+ void x_start;
481
+ void x_end;
482
+ void y_start;
483
+ void y_end;
484
+ void gradio.props.caption;
485
+ void gradio.props.sort;
486
+ void mounted;
487
+ void chart_element;
488
+ void fullscreen;
489
+ void computed_style;
490
+
491
+ if (mounted && chart_element) {
492
+ console.log("Reloading chart due to prop change");
493
+ untrack(() => {
494
+ load_chart();
495
+ });
496
+ }
497
+ });
498
+
499
+ function create_vega_lite_spec(): Spec | null {
500
+ if (!gradio.props.value || !computed_style) return null;
501
+ let accent_color = computed_style.getPropertyValue("--color-accent");
502
+ let body_text_color = computed_style.getPropertyValue("--body-text-color");
503
+ let borderColorPrimary = computed_style.getPropertyValue(
504
+ "--border-color-primary"
505
+ );
506
+ let font_family = computed_style.fontFamily;
507
+ let title_weight = computed_style.getPropertyValue(
508
+ "--block-title-text-weight"
509
+ ) as
510
+ | "bold"
511
+ | "normal"
512
+ | 100
513
+ | 200
514
+ | 300
515
+ | 400
516
+ | 500
517
+ | 600
518
+ | 700
519
+ | 800
520
+ | 900;
521
+ const font_to_px_val = (font: string): number => {
522
+ return font.endsWith("px") ? parseFloat(font.slice(0, -2)) : 12;
523
+ };
524
+ let text_size_md = font_to_px_val(
525
+ computed_style.getPropertyValue("--text-md")
526
+ );
527
+ let text_size_sm = font_to_px_val(
528
+ computed_style.getPropertyValue("--text-sm")
529
+ );
530
+
531
+ /* eslint-disable complexity */
532
+ return {
533
+ $schema: "https://vega.github.io/schema/vega-lite/v5.17.0.json",
534
+ background: "transparent",
535
+ config: {
536
+ autosize: { type: "fit", contains: "padding" },
537
+ axis: {
538
+ labelFont: font_family,
539
+ labelColor: body_text_color,
540
+ titleFont: font_family,
541
+ titleColor: body_text_color,
542
+ titlePadding: 8,
543
+ tickColor: borderColorPrimary,
544
+ labelFontSize: text_size_sm,
545
+ gridColor: borderColorPrimary,
546
+ titleFontWeight: "normal",
547
+ titleFontSize: text_size_sm,
548
+ labelFontWeight: "normal",
549
+ domain: false,
550
+ labelAngle: 0,
551
+ titleLimit: chart_element.offsetHeight * 0.8
552
+ },
553
+ legend: {
554
+ labelColor: body_text_color,
555
+ labelFont: font_family,
556
+ titleColor: body_text_color,
557
+ titleFont: font_family,
558
+ titleFontWeight: "normal",
559
+ titleFontSize: text_size_sm,
560
+ labelFontWeight: "normal",
561
+ offset: 2
562
+ },
563
+ title: {
564
+ color: body_text_color,
565
+ font: font_family,
566
+ fontSize: text_size_md,
567
+ fontWeight: title_weight,
568
+ anchor: "middle"
569
+ },
570
+ view: { stroke: borderColorPrimary },
571
+ mark: {
572
+ stroke: gradio.props.value.mark !== "bar" ? accent_color : undefined,
573
+ fill: gradio.props.value.mark === "bar" ? accent_color : undefined,
574
+ cursor: "crosshair"
575
+ }
576
+ },
577
+ data: { name: "data" },
578
+ datasets: {
579
+ data: _data
580
+ },
581
+ layer: [
582
+ "plot",
583
+ ...(gradio.props.value.mark === "line" ? ["hover"] : [])
584
+ ].map((mode) => {
585
+ return {
586
+ encoding: {
587
+ size:
588
+ gradio.props.value!.mark === "line"
589
+ ? mode == "plot"
590
+ ? {
591
+ condition: {
592
+ empty: false,
593
+ param: "hoverPlot",
594
+ value: 3
595
+ },
596
+ value: 2
597
+ }
598
+ : {
599
+ condition: { empty: false, param: "hover", value: 100 },
600
+ value: 0
601
+ }
602
+ : undefined,
603
+ opacity:
604
+ mode === "plot"
605
+ ? undefined
606
+ : {
607
+ condition: { empty: false, param: "hover", value: 1 },
608
+ value: 0
609
+ },
610
+ x: {
611
+ axis: {
612
+ ...(gradio.props.x_label_angle !== null && {
613
+ labelAngle: gradio.props.x_label_angle
614
+ }),
615
+ labels: gradio.props.x_axis_labels_visible,
616
+ ticks: gradio.props.x_axis_labels_visible
617
+ },
618
+ field: escape_field_name(gradio.props.x),
619
+ title: gradio.props.x_title || gradio.props.x,
620
+ type: gradio.props.value!.datatypes[gradio.props.x],
621
+ scale: {
622
+ zero: false,
623
+ domainMin: _x_lim?.[0] !== null ? _x_lim?.[0] : undefined,
624
+ domainMax: _x_lim?.[1] !== null ? _x_lim?.[1] : undefined
625
+ },
626
+ bin: _x_bin ? { step: _x_bin } : undefined,
627
+ sort: _sort
628
+ },
629
+ y: {
630
+ axis: gradio.props.y_label_angle
631
+ ? { labelAngle: gradio.props.y_label_angle }
632
+ : {},
633
+ field: escape_field_name(gradio.props.y),
634
+ title: gradio.props.y_title || gradio.props.y,
635
+ type: gradio.props.value!.datatypes[gradio.props.y],
636
+ scale: {
637
+ zero: false,
638
+ domainMin: y_start ?? undefined,
639
+ domainMax: y_end ?? undefined
640
+ },
641
+ aggregate: aggregating ? _y_aggregate : undefined
642
+ },
643
+ color: gradio.props.color
644
+ ? {
645
+ field: escape_field_name(gradio.props.color),
646
+ legend: {
647
+ orient: "bottom",
648
+ title: gradio.props.color_title,
649
+ values: gradio.props.colors_in_legend || undefined
650
+ },
651
+ scale:
652
+ gradio.props.value!.datatypes[gradio.props.color] ===
653
+ "nominal"
654
+ ? {
655
+ domain: unique_colors,
656
+ range: gradio.props.color_map
657
+ ? unique_colors.map(
658
+ (c) => gradio.props.color_map![c]
659
+ )
660
+ : undefined
661
+ }
662
+ : {
663
+ range: [
664
+ 100, 200, 300, 400, 500, 600, 700, 800, 900
665
+ ].map((n) =>
666
+ computed_style!.getPropertyValue("--primary-" + n)
667
+ ),
668
+ interpolate: "hsl"
669
+ },
670
+ type: gradio.props.value!.datatypes[gradio.props.color]
671
+ }
672
+ : undefined,
673
+ tooltip:
674
+ gradio.props.tooltip == "none"
675
+ ? undefined
676
+ : [
677
+ {
678
+ field: escape_field_name(gradio.props.y),
679
+ type: gradio.props.value!.datatypes[gradio.props.y],
680
+ aggregate: aggregating ? _y_aggregate : undefined,
681
+ title: gradio.props.y_title || gradio.props.y
682
+ },
683
+ {
684
+ field: escape_field_name(gradio.props.x),
685
+ type: gradio.props.value!.datatypes[gradio.props.x],
686
+ title: gradio.props.x_title || gradio.props.x,
687
+ format: x_temporal ? "%Y-%m-%d %H:%M:%S" : undefined,
688
+ bin: _x_bin ? { step: _x_bin } : undefined
689
+ },
690
+ ...(gradio.props.color
691
+ ? [
692
+ {
693
+ field: gradio.props.color,
694
+ type: gradio.props.value!.datatypes[
695
+ gradio.props.color
696
+ ]
697
+ }
698
+ ]
699
+ : []),
700
+ ...(gradio.props.tooltip === "axis"
701
+ ? []
702
+ : gradio.props.value?.columns
703
+ .filter(
704
+ (col) =>
705
+ col !== gradio.props.x &&
706
+ col !== gradio.props.y &&
707
+ col !== gradio.props.color &&
708
+ (gradio.props.tooltip === "all" ||
709
+ gradio.props.tooltip.includes(col))
710
+ )
711
+ .map((column) => ({
712
+ field: column,
713
+ type: gradio.props.value!.datatypes[column]
714
+ })))
715
+ ]
716
+ },
717
+ strokeDash: {},
718
+ mark: {
719
+ clip: true,
720
+ type: mode === "hover" ? "point" : gradio.props.value.mark
721
+ },
722
+ name: mode
723
+ };
724
+ }),
725
+ // @ts-ignore
726
+ params: [
727
+ ...(gradio.props.value!.mark === "line"
728
+ ? [
729
+ {
730
+ name: "hoverPlot",
731
+ select: {
732
+ clear: "mouseout",
733
+ fields: gradio.props.color ? [gradio.props.color] : [],
734
+ nearest: true,
735
+ on: "mouseover",
736
+ type: "point" as "point"
737
+ },
738
+ views: ["hover"]
739
+ },
740
+ {
741
+ name: "hover",
742
+ select: {
743
+ clear: "mouseout",
744
+ nearest: true,
745
+ on: "mouseover",
746
+ type: "point" as "point"
747
+ },
748
+ views: ["hover"]
749
+ }
750
+ ]
751
+ : []),
752
+ ...(gradio.props._selectable
753
+ ? [
754
+ {
755
+ name: "brush",
756
+ select: {
757
+ encodings: ["x"],
758
+ mark: { fill: "gray", fillOpacity: 0.3, stroke: "none" },
759
+ type: "interval" as "interval"
760
+ },
761
+ views: ["plot"]
762
+ }
763
+ ]
764
+ : [])
765
+ ],
766
+ width: chart_element!.offsetWidth,
767
+ height: gradio.props.height || fullscreen ? "container" : undefined,
768
+ title: gradio.props.title || undefined
769
+ } as Spec;
770
+ }
771
+ /* eslint-enable complexity */
772
+ </script>
773
+
774
+ <Block
775
+ visible={gradio.shared.visible}
776
+ elem_id={gradio.shared.elem_id}
777
+ elem_classes={gradio.shared.elem_classes}
778
+ scale={gradio.shared.scale}
779
+ min_width={gradio.shared.min_width}
780
+ allow_overflow={false}
781
+ padding={true}
782
+ height={gradio.props.height}
783
+ bind:fullscreen
784
+ >
785
+ {#if gradio.shared.loading_status}
786
+ <StatusTracker
787
+ autoscroll={gradio.shared.autoscroll}
788
+ i18n={gradio.i18n}
789
+ {...gradio.shared.loading_status}
790
+ on:clear_status={() =>
791
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
792
+ />
793
+ {/if}
794
+ {#if gradio.props.buttons?.length}
795
+ <IconButtonWrapper>
796
+ {#if gradio.props.buttons?.includes("export")}
797
+ <IconButton Icon={Download} label="Export" on:click={export_chart} />
798
+ {/if}
799
+ {#if gradio.props.buttons?.includes("fullscreen")}
800
+ <FullscreenButton
801
+ {fullscreen}
802
+ on:fullscreen={({ detail }) => {
803
+ fullscreen = detail;
804
+ }}
805
+ />
806
+ {/if}
807
+ </IconButtonWrapper>
808
+ {/if}
809
+ <BlockTitle show_label={gradio.props.show_label} info={undefined}
810
+ >{gradio.props.label}</BlockTitle
811
+ >
812
+
813
+ {#if gradio.props.value && is_browser}
814
+ <div bind:this={chart_element}></div>
815
+
816
+ {#if gradio.props.caption}
817
+ <p class="caption">{gradio.props.caption}</p>
818
+ {/if}
819
+ {:else}
820
+ <Empty unpadded_box={true}><LabelIcon /></Empty>
821
+ {/if}
822
+ </Block>
823
+
824
+ <style>
825
+ div {
826
+ width: 100%;
827
+ height: 100%;
828
+ }
829
+ :global(#vg-tooltip-element) {
830
+ font-family: var(--font) !important;
831
+ font-size: var(--text-xs) !important;
832
+ box-shadow: none !important;
833
+ background-color: var(--block-background-fill) !important;
834
+ border: 1px solid var(--border-color-primary) !important;
835
+ color: var(--body-text-color) !important;
836
+ }
837
+ :global(#vg-tooltip-element .key) {
838
+ color: var(--body-text-color-subdued) !important;
839
+ }
840
+ .caption {
841
+ padding: 0 4px;
842
+ margin: 0;
843
+ text-align: center;
844
+ }
845
+ </style>
6.0.0-dev.5/nativeplot/package.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/nativeplot",
3
+ "version": "0.9.0-dev.1",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "main_changeset": true,
10
+ "exports": {
11
+ ".": {
12
+ "gradio": "./Index.svelte",
13
+ "svelte": "./dist/Index.svelte",
14
+ "types": "./dist/Index.svelte.d.ts"
15
+ },
16
+ "./example": {
17
+ "gradio": "./Example.svelte",
18
+ "svelte": "./dist/Example.svelte",
19
+ "types": "./dist/Example.svelte.d.ts"
20
+ },
21
+ "./package.json": "./package.json"
22
+ },
23
+ "dependencies": {
24
+ "@gradio/atoms": "workspace:^",
25
+ "@gradio/icons": "workspace:^",
26
+ "@gradio/statustracker": "workspace:^",
27
+ "@gradio/theme": "workspace:^",
28
+ "@gradio/utils": "workspace:^",
29
+ "vega": "^6.2.0",
30
+ "vega-embed": "^7.0.2",
31
+ "vega-lite": "^6.4.1"
32
+ },
33
+ "devDependencies": {
34
+ "@gradio/preview": "workspace:^"
35
+ },
36
+ "peerDependencies": {
37
+ "svelte": "^5.43.4"
38
+ },
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/gradio-app/gradio.git",
42
+ "directory": "js/nativeplot"
43
+ }
44
+ }
6.0.0-dev.5/nativeplot/types.ts ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { Gradio, SelectData } from "@gradio/utils";
2
+ import type { LoadingStatus } from "@gradio/statustracker";
3
+
4
+ export interface PlotData {
5
+ columns: string[];
6
+ data: [string | number][];
7
+ datatypes: Record<string, "quantitative" | "temporal" | "nominal">;
8
+ mark: "line" | "point" | "bar";
9
+ }
10
+
11
+ export interface NativePlotProps {
12
+ value: PlotData | null;
13
+ x: string;
14
+ y: string;
15
+ color: string | null;
16
+ title: string | null;
17
+ x_title: string | null;
18
+ y_title: string | null;
19
+ color_title: string | null;
20
+ x_bin: string | number | null;
21
+ y_aggregate: "sum" | "mean" | "median" | "min" | "max" | undefined;
22
+ color_map: Record<string, string> | null;
23
+ colors_in_legend: string[] | null;
24
+ x_lim: [number | null, number | null] | null;
25
+ y_lim: [number | null, number | null] | null;
26
+ x_label_angle: number | null;
27
+ y_label_angle: number | null;
28
+ x_axis_labels_visible: boolean;
29
+ caption: string | null;
30
+ sort: "x" | "y" | "-x" | "-y" | string[] | null;
31
+ tooltip: "axis" | "none" | "all" | string[];
32
+ buttons: string[] | null;
33
+ _selectable: boolean;
34
+ label: string;
35
+ elem_id: string;
36
+ elem_classes: string[];
37
+ visible: boolean | "hidden";
38
+ show_label: boolean;
39
+ scale: number | null;
40
+ min_width: number | undefined;
41
+ loading_status: LoadingStatus | undefined;
42
+ height: number | undefined;
43
+ }
44
+
45
+ export interface NativePlotEvents {
46
+ select: SelectData;
47
+ double_click: undefined;
48
+ clear_status: LoadingStatus;
49
+ }