svelte-plotly.js 1.2.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Plot.svelte CHANGED
@@ -1,141 +1,317 @@
1
- <script context="module"></script>
2
-
3
- <script>import { onMount, onDestroy, createEventDispatcher } from "svelte";
4
- import { debounce as debouncify } from "lodash-es";
5
- const browser = typeof window === "object";
6
- const nextFrame = browser ? requestAnimationFrame : () => void 0;
7
- async function loadPlotly() {
8
- if (!browser) return;
9
- if (libPlotly === void 0) {
1
+ <script context="module" lang="ts">
2
+ import type {
3
+ Data,
4
+ Layout,
5
+ Config,
6
+ PlotlyHTMLElement,
7
+ BeforePlotEvent,
8
+ ClickAnnotationEvent,
9
+ FrameAnimationEvent,
10
+ LegendClickEvent,
11
+ PlotMouseEvent,
12
+ PlotHoverEvent,
13
+ PlotRelayoutEvent,
14
+ PlotRestyleEvent,
15
+ PlotSelectionEvent,
16
+ SliderEndEvent,
17
+ SliderChangeEvent,
18
+ SliderStartEvent,
19
+ SunburstClickEvent
20
+ } from 'plotly.js';
21
+
22
+ export type {
23
+ Data,
24
+ Layout,
25
+ Config,
26
+ PlotlyHTMLElement,
27
+ BeforePlotEvent,
28
+ ClickAnnotationEvent,
29
+ FrameAnimationEvent,
30
+ LegendClickEvent,
31
+ PlotMouseEvent,
32
+ PlotHoverEvent,
33
+ PlotRelayoutEvent,
34
+ PlotRestyleEvent,
35
+ PlotSelectionEvent,
36
+ SliderChangeEvent,
37
+ SliderStartEvent,
38
+ SunburstClickEvent
39
+ };
40
+
41
+ export type FillParent = boolean | 'width' | 'height';
42
+ import type { DebounceSettings } from 'lodash-es';
43
+
44
+ export interface DebounceOptions extends DebounceSettings {
45
+ wait: number;
46
+ }
47
+
48
+ export interface ButtonClickedEvent {
49
+ menu: any;
50
+ button: any;
51
+ active: any;
52
+ }
53
+
54
+ export interface PlotUpdateEvent {
55
+ data: Data;
56
+ layout: Layout;
57
+ }
58
+ </script>
59
+
60
+ <script lang="ts">
61
+ import { onMount, onDestroy, createEventDispatcher } from 'svelte';
62
+ import { debounce as debouncify } from 'lodash-es';
63
+ const browser = typeof window === 'object';
64
+
65
+ const nextFrame = browser ? requestAnimationFrame : () => void 0;
66
+
67
+ async function loadPlotly() {
68
+ if (!browser || libPlotly) return;
69
+
10
70
  if (window.Plotly) {
11
71
  libPlotly = window.Plotly;
12
72
  } else {
13
- const p = await import("plotly.js-dist");
14
- if (libPlotly === void 0) libPlotly = "default" in p ? p.default : p;
73
+ const p = await import('plotly.js-dist');
74
+ if (libPlotly === undefined) libPlotly = 'default' in p ? p.default : p;
15
75
  }
16
76
  }
17
- }
18
- const DEFAULT_WIDTH = 500;
19
- const DEFAULT_HEIGHT = 300;
20
- const events = {
21
- plotly_afterexport: "afterExport",
22
- plotly_afterplot: "afterPlot",
23
- plotly_animated: "animated",
24
- plotly_animating: "animating",
25
- plotly_animatingframe: "animatingFrame",
26
- plotly_animationinterrupted: "animationInterrupted",
27
- plotly_autosize: "autoSize",
28
- plotly_beforeexport: "beforeExport",
29
- plotly_beforehover: "beforeHover",
30
- plotly_beforeplot: "beforePlot",
31
- plotly_buttonclicked: "buttonClicked",
32
- plotly_click: "click",
33
- plotly_clickannotation: "clickAnnotation",
34
- plotly_deselect: "deselect",
35
- plotly_doubleclick: "doubleClick",
36
- plotly_framework: "framework",
37
- plotly_hover: "hover",
38
- plotly_legendclick: "legendClick",
39
- plotly_legenddoubleclick: "legendDoubleClick",
40
- plotly_react: "react",
41
- plotly_redraw: "redraw",
42
- plotly_relayout: "relayout",
43
- plotly_relayouting: "relayouting",
44
- plotly_restyle: "restyle",
45
- plotly_selected: "selected",
46
- plotly_selecting: "selecting",
47
- plotly_sliderchange: "sliderChange",
48
- plotly_sliderend: "sliderEnd",
49
- plotly_sliderstart: "sliderStart",
50
- plotly_sunburstclick: "sunburstClick",
51
- plotly_transitioned: "transitioned",
52
- plotly_transitioning: "transitioning",
53
- plotly_transitioninterrupted: "transitionInterrupted",
54
- plotly_unhover: "unhover",
55
- plotly_update: "update",
56
- plotly_webglcontextlost: "webGLContextLost"
57
- // TODO add all plotly_${traceType}click
58
- };
59
- const dispatch = createEventDispatcher();
60
- export let element = void 0;
61
- export let plot = void 0;
62
- export let libPlotly = void 0;
63
- export let data;
64
- export let layout = void 0;
65
- export let config = void 0;
66
- export let fillParent = false;
67
- export let debounce = 0;
68
- export let configReactivityStrategy = "static-plot";
69
- let className = "";
70
- export { className as class };
71
- onMount(async () => {
72
- window.global = window;
73
- await loadPlotly();
74
- });
75
- let datarevision = 0;
76
- let previousLib = libPlotly;
77
- let previousPlot = plot;
78
- let width = DEFAULT_WIDTH;
79
- let height = DEFAULT_HEIGHT;
80
- $: debounceWait = typeof debounce === "object" ? debounce.wait : debounce ?? 0;
81
- $: debounceOptions = typeof debounce === "object" ? debounce : {};
82
- $: data, datarevision = (datarevision + 1) % 1e3;
83
- $: layout_ = { datarevision, width, height, ...layout };
84
- $: config_ = { displaylogo: false, ...config };
85
- let configUpdated = false;
86
- $: config_, configUpdated = true;
87
- $: {
88
- if (element && previousLib !== libPlotly) {
89
- previousLib?.purge(element);
90
- plot = void 0;
77
+
78
+ const DEFAULT_WIDTH = 500;
79
+ const DEFAULT_HEIGHT = 300;
80
+
81
+ // events
82
+ interface Events {
83
+ afterExport: undefined;
84
+ afterPlot: undefined;
85
+ animated: undefined;
86
+ animating: undefined;
87
+ animatingFrame: FrameAnimationEvent;
88
+ animationInterrupted: undefined;
89
+ autoSize: undefined;
90
+ beforeExport: undefined;
91
+ beforeHover: PlotMouseEvent;
92
+ beforePlot: BeforePlotEvent;
93
+ buttonClicked: ButtonClickedEvent;
94
+ click: PlotMouseEvent;
95
+ clickAnnotation: ClickAnnotationEvent;
96
+ deselect: undefined;
97
+ doubleClick: undefined;
98
+ framework: undefined;
99
+ hover: PlotHoverEvent;
100
+ legendClick: LegendClickEvent;
101
+ legendDoubleClick: LegendClickEvent;
102
+ react: PlotUpdateEvent;
103
+ redraw: undefined;
104
+ relayout: PlotRelayoutEvent;
105
+ relayouting: PlotRelayoutEvent;
106
+ restyle: PlotRestyleEvent;
107
+ selected: PlotSelectionEvent;
108
+ selecting: PlotSelectionEvent;
109
+ sliderChange: SliderChangeEvent;
110
+ sliderEnd: SliderEndEvent;
111
+ sliderStart: SliderStartEvent;
112
+ sunburstClick: SunburstClickEvent;
113
+ transitioned: undefined;
114
+ transitioning: undefined;
115
+ transitionInterrupted: undefined;
116
+ unhover: PlotMouseEvent;
117
+ update: PlotUpdateEvent;
118
+ webGLContextLost: undefined;
91
119
  }
92
- previousLib = libPlotly;
93
- loadPlotly();
94
- }
95
- $: if (previousPlot !== plot) {
96
- for (const [plotlyEvent, svelteEvent] of Object.entries(events)) {
97
- previousPlot?.removeAllListeners?.(plotlyEvent);
98
- plot?.on(plotlyEvent, (e) => dispatch(svelteEvent, e));
120
+ const events = <const>{
121
+ plotly_afterexport: 'afterExport',
122
+ plotly_afterplot: 'afterPlot',
123
+ plotly_animated: 'animated',
124
+ plotly_animating: 'animating',
125
+ plotly_animatingframe: 'animatingFrame',
126
+ plotly_animationinterrupted: 'animationInterrupted',
127
+ plotly_autosize: 'autoSize',
128
+ plotly_beforeexport: 'beforeExport',
129
+ plotly_beforehover: 'beforeHover',
130
+ plotly_beforeplot: 'beforePlot',
131
+ plotly_buttonclicked: 'buttonClicked',
132
+ plotly_click: 'click',
133
+ plotly_clickannotation: 'clickAnnotation',
134
+ plotly_deselect: 'deselect',
135
+ plotly_doubleclick: 'doubleClick',
136
+ plotly_framework: 'framework',
137
+ plotly_hover: 'hover',
138
+ plotly_legendclick: 'legendClick',
139
+ plotly_legenddoubleclick: 'legendDoubleClick',
140
+ plotly_react: 'react',
141
+ plotly_redraw: 'redraw',
142
+ plotly_relayout: 'relayout',
143
+ plotly_relayouting: 'relayouting',
144
+ plotly_restyle: 'restyle',
145
+ plotly_selected: 'selected',
146
+ plotly_selecting: 'selecting',
147
+ plotly_sliderchange: 'sliderChange',
148
+ plotly_sliderend: 'sliderEnd',
149
+ plotly_sliderstart: 'sliderStart',
150
+ plotly_sunburstclick: 'sunburstClick',
151
+ plotly_transitioned: 'transitioned',
152
+ plotly_transitioning: 'transitioning',
153
+ plotly_transitioninterrupted: 'transitionInterrupted',
154
+ plotly_unhover: 'unhover',
155
+ plotly_update: 'update',
156
+ plotly_webglcontextlost: 'webGLContextLost'
157
+ // TODO add all plotly_${traceType}click
158
+ };
159
+ const dispatch = createEventDispatcher<Events>();
160
+
161
+ //
162
+ // Bind Props
163
+ /** The HTML element wrapping the plot. */
164
+ export let element: HTMLDivElement | undefined = undefined;
165
+ /** The inner HTML element containing the plot. */
166
+ export let plot: PlotlyHTMLElement | undefined = undefined;
167
+
168
+ //
169
+ // Input Props
170
+
171
+ /**
172
+ * Alternative Plotly bundle to use. If `undefined`, it defaults to
173
+ * the `plotly.js-dist` package; if null, no plot will be drawn and
174
+ * no library will be downloaded.
175
+ */
176
+ export let libPlotly: typeof import('plotly.js-dist') | null | undefined = undefined;
177
+
178
+ /**
179
+ * Array of trace data, see https://plot.ly/javascript/reference/
180
+ */
181
+ export let data: Data[];
182
+
183
+ /**
184
+ * Layout of the plot, see https://plot.ly/javascript/reference/#layout
185
+ */
186
+ export let layout: Partial<Layout> | undefined = undefined;
187
+
188
+ /**
189
+ * Configuration, see https://plot.ly/javascript/configuration-options/
190
+ */
191
+ export let config: Partial<Config> | undefined = undefined;
192
+
193
+ /**
194
+ * Automatically resize the plot to fill the width and/or
195
+ * height of its parent element.
196
+ */
197
+ export let fillParent: FillParent = false;
198
+
199
+ /**
200
+ * Debounce all changes to the plot.
201
+ */
202
+ export let debounce: number | DebounceOptions = 0;
203
+
204
+ /**
205
+ * Because of an [upstream bug](https://github.com/m93a/svelte-plotly.js/issues/10),
206
+ * changes in the `config` prop don't always render.
207
+ * This prop enables a walkaround, which makes sure
208
+ * that changes in `config` just work.
209
+ *
210
+ * - `'static-plot'` – when changing `config`, briefly disable
211
+ * chart interactivity to force update
212
+ *
213
+ * - `'none'` – disable the walkaround; updating mode bar
214
+ * buttons might not work
215
+ */
216
+ export let configReactivityStrategy: 'none' | 'static-plot' = 'static-plot';
217
+
218
+ /**
219
+ * Class attribute that will be passed to the HTML element
220
+ * wrapping the plot
221
+ */
222
+ let className = '';
223
+ export { className as class };
224
+
225
+ // set up
226
+ onMount(async () => {
227
+ (window as any).global = window;
228
+ await loadPlotly();
229
+ });
230
+
231
+ // state props
232
+ let datarevision = 0;
233
+ let previousLib = libPlotly;
234
+ let previousPlot = plot;
235
+ let width: number = DEFAULT_WIDTH;
236
+ let height: number = DEFAULT_HEIGHT;
237
+
238
+ // updates
239
+ $: debounceWait = typeof debounce === 'object' ? debounce.wait : (debounce ?? 0);
240
+ $: debounceOptions = typeof debounce === 'object' ? debounce : {};
241
+ $: (data, (datarevision = (datarevision + 1) % 1000));
242
+ $: layout_ = { datarevision, width, height, ...layout };
243
+ $: config_ = { displaylogo: false, ...config };
244
+
245
+ let configUpdated = false;
246
+ $: (config_, (configUpdated = true));
247
+
248
+ $: {
249
+ if (element && previousLib !== libPlotly) {
250
+ previousLib?.purge(element);
251
+ plot = undefined;
252
+ }
253
+ previousLib = libPlotly;
254
+ loadPlotly();
99
255
  }
100
- previousPlot = plot;
101
- }
102
- const drawUndebounced = async () => {
103
- if (!libPlotly || !element) return;
104
- if (configUpdated && configReactivityStrategy === "static-plot") {
105
- configUpdated = false;
106
- await libPlotly.react(element, data, layout_, {
107
- ...config_,
108
- staticPlot: !config_.staticPlot
109
- });
256
+ $: if (previousPlot !== plot) {
257
+ for (const [plotlyEvent, svelteEvent] of Object.entries(events)) {
258
+ previousPlot?.removeAllListeners?.(plotlyEvent);
259
+ plot?.on(plotlyEvent as any, (e: any) => dispatch(svelteEvent, e));
260
+ }
261
+ previousPlot = plot;
262
+ }
263
+
264
+ const drawUndebounced = async () => {
265
+ if (!libPlotly || !element) return;
266
+
267
+ if (configUpdated && configReactivityStrategy === 'static-plot') {
268
+ configUpdated = false;
269
+ await libPlotly.react(element, data, layout_, {
270
+ ...config_,
271
+ staticPlot: !config_.staticPlot
272
+ });
273
+ }
274
+
275
+ plot = await libPlotly.react(element, data, layout_, config_);
276
+ };
277
+
278
+ $: draw = debouncify(drawUndebounced, debounceWait, debounceOptions);
279
+ $: (libPlotly, element, data, layout_, config_, configUpdated, draw());
280
+
281
+ // destroy
282
+ onDestroy(() => element && libPlotly?.purge(element));
283
+
284
+ // autosizing
285
+ $: (fillParent, nextFrame(onResize));
286
+ $: fillParentWidth = fillParent === true || fillParent === 'width';
287
+ $: fillParentHeight = fillParent === true || fillParent === 'height';
288
+ $: parent = element?.parentElement ?? undefined;
289
+ let lastParent: typeof parent = undefined;
290
+ $: {
291
+ parentMounted(parent);
292
+ parentUnmounted(lastParent);
293
+ lastParent = parent;
294
+ }
295
+
296
+ let resizeObserver: ResizeObserver | undefined;
297
+ onMount(() => (resizeObserver = new ResizeObserver(onResize)));
298
+ const parentMounted = (parent: Element | undefined) => parent && resizeObserver?.observe(parent);
299
+ const parentUnmounted = (parent: Element | undefined) =>
300
+ parent && resizeObserver?.unobserve(parent);
301
+
302
+ function onResize() {
303
+ if (!parent || !element) return;
304
+
305
+ const { offsetHeight, offsetWidth } = parent;
306
+ const { paddingLeft, paddingTop, paddingRight, paddingBottom } =
307
+ window.getComputedStyle(parent);
308
+
309
+ const maxWidth = offsetWidth - parseInt(paddingLeft) - parseInt(paddingRight);
310
+ const maxHeight = offsetHeight - parseInt(paddingTop) - parseInt(paddingBottom);
311
+
312
+ width = fillParentWidth ? maxWidth : DEFAULT_WIDTH;
313
+ height = fillParentHeight ? maxHeight : DEFAULT_HEIGHT;
110
314
  }
111
- plot = await libPlotly.react(element, data, layout_, config_);
112
- };
113
- $: draw = debouncify(drawUndebounced, debounceWait, debounceOptions);
114
- $: libPlotly, element, data, layout_, config_, configUpdated, draw();
115
- onDestroy(() => element && libPlotly?.purge(element));
116
- $: fillParent, nextFrame(onResize);
117
- $: fillParentWidth = fillParent === true || fillParent === "width";
118
- $: fillParentHeight = fillParent === true || fillParent === "height";
119
- $: parent = element?.parentElement ?? void 0;
120
- let lastParent = void 0;
121
- $: {
122
- parentMounted(parent);
123
- parentUnmounted(lastParent);
124
- lastParent = parent;
125
- }
126
- let resizeObserver;
127
- onMount(() => resizeObserver = new ResizeObserver(onResize));
128
- const parentMounted = (parent2) => parent2 && resizeObserver?.observe(parent2);
129
- const parentUnmounted = (parent2) => parent2 && resizeObserver?.unobserve(parent2);
130
- function onResize() {
131
- if (!parent || !element) return;
132
- const { offsetHeight, offsetWidth } = parent;
133
- const { paddingLeft, paddingTop, paddingRight, paddingBottom } = window.getComputedStyle(parent);
134
- const maxWidth = offsetWidth - parseInt(paddingLeft) - parseInt(paddingRight);
135
- const maxHeight = offsetHeight - parseInt(paddingTop) - parseInt(paddingBottom);
136
- width = fillParentWidth ? maxWidth : DEFAULT_WIDTH;
137
- height = fillParentHeight ? maxHeight : DEFAULT_HEIGHT;
138
- }
139
315
  </script>
140
316
 
141
317
  <div
@@ -144,7 +320,7 @@ function onResize() {
144
320
  class:fillParentWidth
145
321
  class:fillParentHeight
146
322
  bind:this={element}
147
- />
323
+ ></div>
148
324
 
149
325
  <style>.fillParent {
150
326
  overflow: visible;
@@ -1,4 +1,3 @@
1
- import { SvelteComponent } from "svelte";
2
1
  import type { Data, Layout, Config, PlotlyHTMLElement, BeforePlotEvent, ClickAnnotationEvent, FrameAnimationEvent, LegendClickEvent, PlotMouseEvent, PlotHoverEvent, PlotRelayoutEvent, PlotRestyleEvent, PlotSelectionEvent, SliderEndEvent, SliderChangeEvent, SliderStartEvent, SunburstClickEvent } from 'plotly.js';
3
2
  export type { Data, Layout, Config, PlotlyHTMLElement, BeforePlotEvent, ClickAnnotationEvent, FrameAnimationEvent, LegendClickEvent, PlotMouseEvent, PlotHoverEvent, PlotRelayoutEvent, PlotRestyleEvent, PlotSelectionEvent, SliderChangeEvent, SliderStartEvent, SunburstClickEvent };
4
3
  export type FillParent = boolean | 'width' | 'height';
@@ -15,94 +14,98 @@ export interface PlotUpdateEvent {
15
14
  data: Data;
16
15
  layout: Layout;
17
16
  }
18
- declare const __propDef: {
19
- props: {
20
- /** The HTML element wrapping the plot. */ element?: HTMLDivElement | undefined;
21
- /** The inner HTML element containing the plot. */ plot?: PlotlyHTMLElement | undefined;
22
- /**
23
- * Alternative Plotly bundle to use. If `undefined`, it defaults to
24
- * the `plotly.js-dist` package; if null, no plot will be drawn and
25
- * no library will be downloaded.
26
- */ libPlotly?: typeof import("plotly.js-dist") | null | undefined;
27
- /**
28
- * Array of trace data, see https://plot.ly/javascript/reference/
29
- */ data: Data[];
30
- /**
31
- * Layout of the plot, see https://plot.ly/javascript/reference/#layout
32
- */ layout?: Partial<Layout> | undefined;
33
- /**
34
- * Configuration, see https://plot.ly/javascript/configuration-options/
35
- */ config?: Partial<Config> | undefined;
36
- /**
37
- * Automatically resize the plot to fill the width and/or
38
- * height of its parent element.
39
- */ fillParent?: FillParent;
40
- /**
41
- * Debounce all changes to the plot.
42
- */ debounce?: number | DebounceOptions;
43
- /**
44
- * Because of an [upstream bug](https://github.com/m93a/svelte-plotly.js/issues/10),
45
- * changes in the `config` prop don't always render.
46
- * This prop enables a walkaround, which makes sure
47
- * that changes in `config` just work.
48
- *
49
- * - `'static-plot'` – when changing `config`, briefly disable
50
- * chart interactivity to force update
51
- *
52
- * - `'none'` – disable the walkaround; updating mode bar
53
- * buttons might not work
54
- */ configReactivityStrategy?: "none" | "static-plot";
55
- /**
56
- * Class attribute that will be passed to the HTML element
57
- * wrapping the plot
58
- */ class?: string;
17
+ interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
18
+ new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
19
+ $$bindings?: Bindings;
20
+ } & Exports;
21
+ (internal: unknown, props: Props & {
22
+ $$events?: Events;
23
+ $$slots?: Slots;
24
+ }): Exports & {
25
+ $set?: any;
26
+ $on?: any;
59
27
  };
60
- events: {
61
- afterExport: CustomEvent<undefined>;
62
- afterPlot: CustomEvent<undefined>;
63
- animated: CustomEvent<undefined>;
64
- animating: CustomEvent<undefined>;
65
- animatingFrame: CustomEvent<FrameAnimationEvent>;
66
- animationInterrupted: CustomEvent<undefined>;
67
- autoSize: CustomEvent<undefined>;
68
- beforeExport: CustomEvent<undefined>;
69
- beforeHover: CustomEvent<PlotMouseEvent>;
70
- beforePlot: CustomEvent<BeforePlotEvent>;
71
- buttonClicked: CustomEvent<ButtonClickedEvent>;
72
- click: CustomEvent<PlotMouseEvent>;
73
- clickAnnotation: CustomEvent<ClickAnnotationEvent>;
74
- deselect: CustomEvent<undefined>;
75
- doubleClick: CustomEvent<undefined>;
76
- framework: CustomEvent<undefined>;
77
- hover: CustomEvent<PlotHoverEvent>;
78
- legendClick: CustomEvent<LegendClickEvent>;
79
- legendDoubleClick: CustomEvent<LegendClickEvent>;
80
- react: CustomEvent<PlotUpdateEvent>;
81
- redraw: CustomEvent<undefined>;
82
- relayout: CustomEvent<PlotRelayoutEvent>;
83
- relayouting: CustomEvent<PlotRelayoutEvent>;
84
- restyle: CustomEvent<PlotRestyleEvent>;
85
- selected: CustomEvent<PlotSelectionEvent>;
86
- selecting: CustomEvent<PlotSelectionEvent>;
87
- sliderChange: CustomEvent<SliderChangeEvent>;
88
- sliderEnd: CustomEvent<SliderEndEvent>;
89
- sliderStart: CustomEvent<SliderStartEvent>;
90
- sunburstClick: CustomEvent<SunburstClickEvent>;
91
- transitioned: CustomEvent<undefined>;
92
- transitioning: CustomEvent<undefined>;
93
- transitionInterrupted: CustomEvent<undefined>;
94
- unhover: CustomEvent<PlotMouseEvent>;
95
- update: CustomEvent<PlotUpdateEvent>;
96
- webGLContextLost: CustomEvent<undefined>;
97
- } & {
98
- [evt: string]: CustomEvent<any>;
99
- };
100
- slots: {};
101
- exports?: {} | undefined;
102
- bindings?: string | undefined;
103
- };
104
- export type PlotProps = typeof __propDef.props;
105
- export type PlotEvents = typeof __propDef.events;
106
- export type PlotSlots = typeof __propDef.slots;
107
- export default class Plot extends SvelteComponent<PlotProps, PlotEvents, PlotSlots> {
28
+ z_$$bindings?: Bindings;
108
29
  }
30
+ declare const Plot: $$__sveltets_2_IsomorphicComponent<{
31
+ /** The HTML element wrapping the plot. */ element?: HTMLDivElement | undefined;
32
+ /** The inner HTML element containing the plot. */ plot?: PlotlyHTMLElement | undefined;
33
+ /**
34
+ * Alternative Plotly bundle to use. If `undefined`, it defaults to
35
+ * the `plotly.js-dist` package; if null, no plot will be drawn and
36
+ * no library will be downloaded.
37
+ */ libPlotly?: typeof import("plotly.js-dist") | null | undefined;
38
+ /**
39
+ * Array of trace data, see https://plot.ly/javascript/reference/
40
+ */ data: Data[];
41
+ /**
42
+ * Layout of the plot, see https://plot.ly/javascript/reference/#layout
43
+ */ layout?: Partial<Layout> | undefined;
44
+ /**
45
+ * Configuration, see https://plot.ly/javascript/configuration-options/
46
+ */ config?: Partial<Config> | undefined;
47
+ /**
48
+ * Automatically resize the plot to fill the width and/or
49
+ * height of its parent element.
50
+ */ fillParent?: FillParent;
51
+ /**
52
+ * Debounce all changes to the plot.
53
+ */ debounce?: number | DebounceOptions;
54
+ /**
55
+ * Because of an [upstream bug](https://github.com/m93a/svelte-plotly.js/issues/10),
56
+ * changes in the `config` prop don't always render.
57
+ * This prop enables a walkaround, which makes sure
58
+ * that changes in `config` just work.
59
+ *
60
+ * - `'static-plot'` – when changing `config`, briefly disable
61
+ * chart interactivity to force update
62
+ *
63
+ * - `'none'` – disable the walkaround; updating mode bar
64
+ * buttons might not work
65
+ */ configReactivityStrategy?: "none" | "static-plot";
66
+ /**
67
+ * Class attribute that will be passed to the HTML element
68
+ * wrapping the plot
69
+ */ class?: string;
70
+ }, {
71
+ afterExport: CustomEvent<undefined>;
72
+ afterPlot: CustomEvent<undefined>;
73
+ animated: CustomEvent<undefined>;
74
+ animating: CustomEvent<undefined>;
75
+ animatingFrame: CustomEvent<FrameAnimationEvent>;
76
+ animationInterrupted: CustomEvent<undefined>;
77
+ autoSize: CustomEvent<undefined>;
78
+ beforeExport: CustomEvent<undefined>;
79
+ beforeHover: CustomEvent<PlotMouseEvent>;
80
+ beforePlot: CustomEvent<BeforePlotEvent>;
81
+ buttonClicked: CustomEvent<ButtonClickedEvent>;
82
+ click: CustomEvent<PlotMouseEvent>;
83
+ clickAnnotation: CustomEvent<ClickAnnotationEvent>;
84
+ deselect: CustomEvent<undefined>;
85
+ doubleClick: CustomEvent<undefined>;
86
+ framework: CustomEvent<undefined>;
87
+ hover: CustomEvent<PlotHoverEvent>;
88
+ legendClick: CustomEvent<LegendClickEvent>;
89
+ legendDoubleClick: CustomEvent<LegendClickEvent>;
90
+ react: CustomEvent<PlotUpdateEvent>;
91
+ redraw: CustomEvent<undefined>;
92
+ relayout: CustomEvent<PlotRelayoutEvent>;
93
+ relayouting: CustomEvent<PlotRelayoutEvent>;
94
+ restyle: CustomEvent<PlotRestyleEvent>;
95
+ selected: CustomEvent<PlotSelectionEvent>;
96
+ selecting: CustomEvent<PlotSelectionEvent>;
97
+ sliderChange: CustomEvent<SliderChangeEvent>;
98
+ sliderEnd: CustomEvent<SliderEndEvent>;
99
+ sliderStart: CustomEvent<SliderStartEvent>;
100
+ sunburstClick: CustomEvent<SunburstClickEvent>;
101
+ transitioned: CustomEvent<undefined>;
102
+ transitioning: CustomEvent<undefined>;
103
+ transitionInterrupted: CustomEvent<undefined>;
104
+ unhover: CustomEvent<PlotMouseEvent>;
105
+ update: CustomEvent<PlotUpdateEvent>;
106
+ webGLContextLost: CustomEvent<undefined>;
107
+ } & {
108
+ [evt: string]: CustomEvent<any>;
109
+ }, {}, {}, string>;
110
+ type Plot = InstanceType<typeof Plot>;
111
+ export default Plot;
@@ -0,0 +1,250 @@
1
+ <script module lang="ts">
2
+ import { debounce as debouncify } from 'lodash-es';
3
+
4
+ import type { Data, Layout, Config, PlotlyHTMLElement } from 'plotly.js';
5
+
6
+ export type FillParent = boolean | 'width' | 'height';
7
+ import type { DebounceSettings } from 'lodash-es';
8
+
9
+ export interface DebounceOptions extends DebounceSettings {
10
+ wait: number;
11
+ }
12
+
13
+ export interface ButtonClickedEvent {
14
+ menu: any;
15
+ button: any;
16
+ active: any;
17
+ }
18
+
19
+ export interface PlotUpdateEvent {
20
+ data: Data;
21
+ layout: Layout;
22
+ }
23
+
24
+ export const plotlyEvents = [
25
+ 'plotly_afterexport',
26
+ 'plotly_afterplot',
27
+ 'plotly_animated',
28
+ 'plotly_animating',
29
+ 'plotly_animatingframe',
30
+ 'plotly_animationinterrupted',
31
+ 'plotly_autosize',
32
+ 'plotly_beforeexport',
33
+ 'plotly_beforehover',
34
+ 'plotly_beforeplot',
35
+ 'plotly_buttonclicked',
36
+ 'plotly_click',
37
+ 'plotly_clickannotation',
38
+ 'plotly_deselect',
39
+ 'plotly_doubleclick',
40
+ 'plotly_framework',
41
+ 'plotly_hover',
42
+ 'plotly_legendclick',
43
+ 'plotly_legenddoubleclick',
44
+ 'plotly_react',
45
+ 'plotly_redraw',
46
+ 'plotly_relayout',
47
+ 'plotly_relayouting',
48
+ 'plotly_restyle',
49
+ 'plotly_selected',
50
+ 'plotly_selecting',
51
+ 'plotly_sliderchange',
52
+ 'plotly_sliderend',
53
+ 'plotly_sliderstart',
54
+ 'plotly_sunburstclick',
55
+ 'plotly_transitioned',
56
+ 'plotly_transitioning',
57
+ 'plotly_transitioninterrupted',
58
+ 'plotly_unhover',
59
+ 'plotly_update',
60
+ 'plotly_webglcontextlost'
61
+ ] as const;
62
+ </script>
63
+
64
+ <script lang="ts">
65
+ const browser = typeof window === 'object';
66
+
67
+ const nextFrame = browser ? requestAnimationFrame : () => void 0;
68
+
69
+ function loadPlotly() {
70
+ if (!browser || libPlotly) return;
71
+
72
+ if (window.Plotly) {
73
+ libPlotly = window.Plotly;
74
+ } else {
75
+ import('plotly.js-dist').then(p => {
76
+ if (libPlotly === undefined) libPlotly = 'default' in p ? p.default : p;
77
+ });
78
+ }
79
+ }
80
+
81
+ const DEFAULT_WIDTH = 500;
82
+ const DEFAULT_HEIGHT = 300;
83
+
84
+ interface Props {
85
+ /** The HTML element wrapping the plot. */
86
+ element?: HTMLDivElement;
87
+ /** The inner HTML element containing the plot. */
88
+ plot?: PlotlyHTMLElement;
89
+
90
+ /**
91
+ * Alternative Plotly bundle to use. If `undefined`, it defaults to
92
+ * the `plotly.js-dist` package; if null, no plot will be drawn and
93
+ * no library will be downloaded.
94
+ */
95
+ libPlotly: typeof import('plotly.js-dist') | null | undefined;
96
+
97
+ /**
98
+ * Array of trace data, see https://plot.ly/javascript/reference/
99
+ */
100
+ data: Data[];
101
+
102
+ /**
103
+ * Layout of the plot, see https://plot.ly/javascript/reference/#layout
104
+ */
105
+ layout?: Partial<Layout>;
106
+
107
+ /**
108
+ * Configuration, see https://plot.ly/javascript/configuration-options/
109
+ */
110
+ config?: Partial<Config>;
111
+
112
+ /**
113
+ * Automatically resize the plot to fill the width and/or
114
+ * height of its parent element.
115
+ */
116
+ fillParent: FillParent;
117
+
118
+ /**
119
+ * Debounce all changes to the plot.
120
+ */
121
+ debounce?: number | DebounceOptions;
122
+
123
+ /**
124
+ * Class attribute that will be passed to the HTML element
125
+ * wrapping the plot
126
+ */
127
+ class?: string;
128
+
129
+ onEvent: ((event: (typeof plotlyEvents)[number], data: unknown) => void) | undefined;
130
+ }
131
+
132
+ let {
133
+ element = $bindable(),
134
+ plot = $bindable(),
135
+
136
+ libPlotly,
137
+ data,
138
+ layout,
139
+ config,
140
+ fillParent,
141
+ debounce,
142
+ class: className,
143
+
144
+ onEvent
145
+ }: Props = $props();
146
+
147
+ // set up
148
+ $effect(() => {
149
+ (window as any).global = window;
150
+ loadPlotly();
151
+
152
+ return () => {
153
+ if (element) libPlotly?.purge(element);
154
+ };
155
+ });
156
+
157
+ // state props
158
+ let width: number = $state(DEFAULT_WIDTH);
159
+ let height: number = $state(DEFAULT_HEIGHT);
160
+
161
+ // updates
162
+ const debounceWait = $derived(typeof debounce === 'object' ? debounce.wait : (debounce ?? 0));
163
+ const debounceOptions = $derived(typeof debounce === 'object' ? debounce : {});
164
+
165
+ const layout_ = $derived({ width, height, ...layout });
166
+ const config_ = $derived({ displaylogo: false, ...config });
167
+
168
+ $effect(() => {
169
+ if (!plot || !onEvent) return;
170
+
171
+ for (const event of plotlyEvents) {
172
+ plot?.on(event as any, e => onEvent(event, e));
173
+ }
174
+
175
+ return () => {
176
+ for (const event of plotlyEvents) {
177
+ plot?.removeAllListeners?.(event);
178
+ }
179
+ };
180
+ });
181
+
182
+ const drawUndebounced = async () => {
183
+ if (!libPlotly || !element) return;
184
+
185
+ plot = await libPlotly.react(element, data, layout_, config_);
186
+ };
187
+
188
+ const draw = $derived(debouncify(drawUndebounced, debounceWait, debounceOptions));
189
+
190
+ $effect(() => {
191
+ void (void libPlotly, element, data, layout_, config_);
192
+ draw();
193
+ });
194
+
195
+ // autosizing
196
+ const fillParentWidth = $derived(fillParent === true || fillParent === 'width');
197
+ const fillParentHeight = $derived(fillParent === true || fillParent === 'height');
198
+ const parent = $derived(element?.parentElement ?? undefined);
199
+
200
+ function onResize() {
201
+ if (!parent || !element) return;
202
+
203
+ const { offsetHeight, offsetWidth } = parent;
204
+ const { paddingLeft, paddingTop, paddingRight, paddingBottom } =
205
+ window.getComputedStyle(parent);
206
+
207
+ const maxWidth = offsetWidth - parseInt(paddingLeft) - parseInt(paddingRight);
208
+ const maxHeight = offsetHeight - parseInt(paddingTop) - parseInt(paddingBottom);
209
+
210
+ width = fillParentWidth ? maxWidth : DEFAULT_WIDTH;
211
+ height = fillParentHeight ? maxHeight : DEFAULT_HEIGHT;
212
+ }
213
+
214
+ $effect(() => {
215
+ void fillParent;
216
+
217
+ nextFrame(onResize);
218
+ });
219
+
220
+ $effect(() => {
221
+ if (!parent) return;
222
+
223
+ const resizeObserver = new ResizeObserver(onResize);
224
+
225
+ if (parent) resizeObserver.observe(parent);
226
+ return () => resizeObserver.disconnect();
227
+ });
228
+ </script>
229
+
230
+ <div
231
+ class={className}
232
+ class:fillParent
233
+ class:fillParentWidth
234
+ class:fillParentHeight
235
+ bind:this={element}
236
+ ></div>
237
+
238
+ <style>.fillParent {
239
+ overflow: visible;
240
+ }
241
+
242
+ .fillParentWidth {
243
+ width: 0 !important;
244
+ max-width: 0 !important;
245
+ }
246
+
247
+ .fillParentHeight {
248
+ height: 0 !important;
249
+ max-height: 0 !important;
250
+ }</style>
@@ -0,0 +1,58 @@
1
+ import type { Data, Layout, Config, PlotlyHTMLElement } from 'plotly.js';
2
+ export type FillParent = boolean | 'width' | 'height';
3
+ import type { DebounceSettings } from 'lodash-es';
4
+ export interface DebounceOptions extends DebounceSettings {
5
+ wait: number;
6
+ }
7
+ export interface ButtonClickedEvent {
8
+ menu: any;
9
+ button: any;
10
+ active: any;
11
+ }
12
+ export interface PlotUpdateEvent {
13
+ data: Data;
14
+ layout: Layout;
15
+ }
16
+ export declare const plotlyEvents: readonly ["plotly_afterexport", "plotly_afterplot", "plotly_animated", "plotly_animating", "plotly_animatingframe", "plotly_animationinterrupted", "plotly_autosize", "plotly_beforeexport", "plotly_beforehover", "plotly_beforeplot", "plotly_buttonclicked", "plotly_click", "plotly_clickannotation", "plotly_deselect", "plotly_doubleclick", "plotly_framework", "plotly_hover", "plotly_legendclick", "plotly_legenddoubleclick", "plotly_react", "plotly_redraw", "plotly_relayout", "plotly_relayouting", "plotly_restyle", "plotly_selected", "plotly_selecting", "plotly_sliderchange", "plotly_sliderend", "plotly_sliderstart", "plotly_sunburstclick", "plotly_transitioned", "plotly_transitioning", "plotly_transitioninterrupted", "plotly_unhover", "plotly_update", "plotly_webglcontextlost"];
17
+ interface Props {
18
+ /** The HTML element wrapping the plot. */
19
+ element?: HTMLDivElement;
20
+ /** The inner HTML element containing the plot. */
21
+ plot?: PlotlyHTMLElement;
22
+ /**
23
+ * Alternative Plotly bundle to use. If `undefined`, it defaults to
24
+ * the `plotly.js-dist` package; if null, no plot will be drawn and
25
+ * no library will be downloaded.
26
+ */
27
+ libPlotly: typeof import('plotly.js-dist') | null | undefined;
28
+ /**
29
+ * Array of trace data, see https://plot.ly/javascript/reference/
30
+ */
31
+ data: Data[];
32
+ /**
33
+ * Layout of the plot, see https://plot.ly/javascript/reference/#layout
34
+ */
35
+ layout?: Partial<Layout>;
36
+ /**
37
+ * Configuration, see https://plot.ly/javascript/configuration-options/
38
+ */
39
+ config?: Partial<Config>;
40
+ /**
41
+ * Automatically resize the plot to fill the width and/or
42
+ * height of its parent element.
43
+ */
44
+ fillParent: FillParent;
45
+ /**
46
+ * Debounce all changes to the plot.
47
+ */
48
+ debounce?: number | DebounceOptions;
49
+ /**
50
+ * Class attribute that will be passed to the HTML element
51
+ * wrapping the plot
52
+ */
53
+ class?: string;
54
+ onEvent: ((event: (typeof plotlyEvents)[number], data: unknown) => void) | undefined;
55
+ }
56
+ declare const PlotV2: import("svelte").Component<Props, {}, "element" | "plot">;
57
+ type PlotV2 = ReturnType<typeof PlotV2>;
58
+ export default PlotV2;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './Plot.svelte';
2
2
  export { default } from './Plot.svelte';
3
+ export { default as PlotV2 } from './PlotV2.svelte';
3
4
  export * from './utils.js';
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './Plot.svelte';
2
2
  export { default } from './Plot.svelte';
3
+ export { default as PlotV2 } from './PlotV2.svelte';
3
4
  export * from './utils.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-plotly.js",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "author": {
5
5
  "name": "Michal Grňo (m93a)",
6
6
  "url": "https://github.com/m93a/"
@@ -24,34 +24,35 @@
24
24
  "!dist/**/*.spec.*"
25
25
  ],
26
26
  "dependencies": {
27
- "@fortawesome/fontawesome-common-types": "^6.6.0",
27
+ "@fortawesome/fontawesome-common-types": "^7.2.0",
28
28
  "@types/lodash-es": "^4.17.12",
29
- "@types/plotly.js": "^2.33.3",
30
- "lodash-es": "^4.17.21"
29
+ "@types/plotly.js": "^3.0.10",
30
+ "lodash-es": "^4.18.1"
31
31
  },
32
32
  "devDependencies": {
33
- "@fortawesome/free-solid-svg-icons": "^6.6.0",
34
- "@sveltejs/adapter-auto": "^3.2.2",
35
- "@sveltejs/kit": "^2.5.18",
36
- "@sveltejs/package": "^2.3.2",
37
- "@sveltejs/vite-plugin-svelte": "^3.1.1",
38
- "@types/eslint": "^9.6.0",
39
- "eslint": "^9.8.0",
40
- "eslint-plugin-svelte": "^2.43.0",
41
- "globals": "^15.8.0",
42
- "plotly.js-dist": "^2.34.0",
43
- "prettier": "^3.3.3",
44
- "prettier-plugin-svelte": "^3.2.6",
45
- "publint": "^0.2.9",
46
- "sass": "^1.77.8",
47
- "svelte-check": "^3.8.5",
48
- "typescript": "^5.5.4",
49
- "typescript-eslint": "^7.18.0",
50
- "vite": "^5.3.5"
33
+ "@eslint/js": "^10.0.1",
34
+ "@fortawesome/free-solid-svg-icons": "^7.2.0",
35
+ "@sveltejs/adapter-auto": "^7.0.1",
36
+ "@sveltejs/kit": "^2.56.1",
37
+ "@sveltejs/package": "^2.5.7",
38
+ "@sveltejs/vite-plugin-svelte": "^7.0.0",
39
+ "@types/eslint": "^9.6.1",
40
+ "eslint": "^10.2.0",
41
+ "eslint-plugin-svelte": "^3.17.0",
42
+ "globals": "^17.4.0",
43
+ "plotly.js-dist": "^3.5.0",
44
+ "prettier": "^3.8.1",
45
+ "prettier-plugin-svelte": "^3.5.1",
46
+ "publint": "^0.3.18",
47
+ "sass": "^1.99.0",
48
+ "svelte-check": "^4.4.6",
49
+ "typescript": "^6.0.2",
50
+ "typescript-eslint": "^8.58.0",
51
+ "vite": "^8.0.7"
51
52
  },
52
53
  "peerDependencies": {
53
- "plotly.js-dist": "^2.34.0",
54
- "svelte": "^4.2.0"
54
+ "plotly.js-dist": "^3.0.0",
55
+ "svelte": "^5.0.0"
55
56
  },
56
57
  "scripts": {
57
58
  "dev": "vite dev",