gradio-pr-bot commited on
Commit
23fc80d
·
verified ·
1 Parent(s): eaf330d

Upload folder using huggingface_hub

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