svelte-plotly.js 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Michal Grňo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/Plot.svelte ADDED
@@ -0,0 +1,108 @@
1
+ <script context="module">export {};
2
+ </script>
3
+
4
+ <script>import { onMount, onDestroy } from 'svelte';
5
+ import { debounce as debouncify } from 'lodash';
6
+ import { browser } from '$app/env';
7
+ const nextFrame = browser ? requestAnimationFrame : () => void 0;
8
+ async function loadPlotly() {
9
+ if (!browser)
10
+ return;
11
+ if (libPlotly === undefined) {
12
+ const p = await import('plotly.js-dist');
13
+ if (libPlotly === undefined)
14
+ libPlotly = p;
15
+ }
16
+ }
17
+ const DEFAULT_WIDTH = 500;
18
+ const DEFAULT_HEIGHT = 300;
19
+ // bind props
20
+ export let element = undefined;
21
+ export let plot = undefined;
22
+ // input props
23
+ export let libPlotly = undefined;
24
+ export let data;
25
+ export let layout = undefined;
26
+ export let config = undefined;
27
+ export let fillParent = false;
28
+ export let debounce = 0;
29
+ let className = '';
30
+ export { className as class };
31
+ // set up
32
+ onMount(async () => {
33
+ window.global = window;
34
+ loadPlotly();
35
+ });
36
+ // state props
37
+ let datarevision = 0;
38
+ let previousLib = libPlotly;
39
+ let width = DEFAULT_WIDTH;
40
+ let height = DEFAULT_HEIGHT;
41
+ // updates
42
+ $: debounceWait = typeof debounce === 'object' ? debounce.wait : debounce ?? 0;
43
+ $: debounceOptions = typeof debounce === 'object' ? debounce : {};
44
+ $: data, (datarevision = (datarevision + 1) % 1000);
45
+ $: layout_ = { datarevision, width, height, ...layout };
46
+ $: config_ = { displaylogo: false, ...config };
47
+ $: draw(libPlotly, element, data, layout_, config_);
48
+ $: {
49
+ if (element && previousLib !== libPlotly)
50
+ previousLib?.purge(element);
51
+ previousLib = libPlotly;
52
+ loadPlotly();
53
+ }
54
+ const drawUndebounced = (lib, e, d, l, c) => {
55
+ if (e)
56
+ lib?.react(e, d, l, c).then(p => (plot = p));
57
+ };
58
+ $: draw = debouncify(drawUndebounced, debounceWait, debounceOptions);
59
+ // destroy
60
+ onDestroy(() => element && libPlotly?.purge(element));
61
+ // autosizing
62
+ $: fillParent, nextFrame(onResize);
63
+ $: fillParentWidth = fillParent === true || fillParent === 'width';
64
+ $: fillParentHeight = fillParent === true || fillParent === 'height';
65
+ $: parent = element?.parentElement;
66
+ let lastParent = null;
67
+ $: {
68
+ parentMounted(parent);
69
+ parentUnmounted(lastParent);
70
+ lastParent = parent;
71
+ }
72
+ let resizeObserver;
73
+ onMount(() => (resizeObserver = new ResizeObserver(onResize)));
74
+ const parentMounted = (parent) => parent && resizeObserver?.observe(parent);
75
+ const parentUnmounted = (parent) => parent && resizeObserver?.unobserve(parent);
76
+ function onResize() {
77
+ if (!parent || !element)
78
+ return;
79
+ const { offsetHeight, offsetWidth } = parent;
80
+ const { paddingLeft, paddingTop, paddingRight, paddingBottom } = window.getComputedStyle(parent);
81
+ const maxWidth = offsetWidth - parseInt(paddingLeft) - parseInt(paddingRight);
82
+ const maxHeight = offsetHeight - parseInt(paddingTop) - parseInt(paddingRight);
83
+ width = fillParentWidth ? maxWidth : DEFAULT_WIDTH;
84
+ height = fillParentHeight ? maxHeight : DEFAULT_HEIGHT;
85
+ }
86
+ </script>
87
+
88
+ <div
89
+ class={className}
90
+ class:fillParent
91
+ class:fillParentWidth
92
+ class:fillParentHeight
93
+ bind:this={element}
94
+ />
95
+
96
+ <style>.fillParent {
97
+ overflow: visible;
98
+ }
99
+
100
+ .fillParentWidth {
101
+ width: 0 !important;
102
+ max-width: 0 !important;
103
+ }
104
+
105
+ .fillParentHeight {
106
+ height: 0 !important;
107
+ max-height: 0 !important;
108
+ }</style>
@@ -0,0 +1,30 @@
1
+ import { SvelteComponentTyped } from "svelte";
2
+ import type { Data, Layout, Config, PlotlyHTMLElement } from 'plotly.js';
3
+ export type { Data, Layout, Config, PlotlyHTMLElement };
4
+ export declare type FillParent = boolean | 'width' | 'height';
5
+ import type { DebounceSettings } from 'lodash';
6
+ export interface DebounceOptions extends DebounceSettings {
7
+ wait: number;
8
+ }
9
+ declare const __propDef: {
10
+ props: {
11
+ element?: (null | undefined) | HTMLDivElement;
12
+ plot?: (null | undefined) | PlotlyHTMLElement;
13
+ libPlotly?: typeof import('plotly.js-dist') | null | undefined;
14
+ data: Data[];
15
+ layout?: Partial<Layout> | undefined;
16
+ config?: Partial<Config> | undefined;
17
+ fillParent?: FillParent | undefined;
18
+ debounce?: number | DebounceOptions | undefined;
19
+ class?: string | undefined;
20
+ };
21
+ events: {
22
+ [evt: string]: CustomEvent<any>;
23
+ };
24
+ slots: {};
25
+ };
26
+ export declare type PlotProps = typeof __propDef.props;
27
+ export declare type PlotEvents = typeof __propDef.events;
28
+ export declare type PlotSlots = typeof __propDef.slots;
29
+ export default class Plot extends SvelteComponentTyped<PlotProps, PlotEvents, PlotSlots> {
30
+ }
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Plotly.js for Svelte
2
+ This is an unofficial package that lets you efficiently use [plotly.js](https://plotly.com/javascript/) inside your Svelte or SvelteKit app.
3
+
4
+ ## Usage
5
+
6
+ 1. Install using `yarn add svelte-plotly.js` or `npm install svelte-plotly.js`.
7
+ 2. Use in your app
8
+
9
+ ```svelte
10
+ <script lang="ts">
11
+ import Plot from 'svelte-plotly.js';
12
+
13
+ const data = [
14
+ {
15
+ x: [1, 2, 3, 4, 5],
16
+ y: [y0, 2, 4, 8, 16]
17
+ }
18
+ ];
19
+ </script>
20
+
21
+ <Plot
22
+ {data}
23
+ layout={{
24
+ margin: { t: 0 }
25
+ }}
26
+ fillParent='width'
27
+ debounce=250
28
+ />
29
+ ```
30
+
31
+ ## Properties
32
+ | Prop | Type | Description
33
+ | --------- | ----------- | -------------
34
+ | **required `data`** | `Data[]`| array of trace data, see https://plot.ly/javascript/reference/
35
+ | `layout` | `Partial<Layout>` | layout of the plot, see https://plot.ly/javascript/reference/#layout
36
+ | `config` | `Partial<Config>` | configuration, see https://plot.ly/javascript/configuration-options/
37
+ | `class` | `string` | class that will be passed to the HTML element wrapping the plot
38
+ | `fillParent` | `boolean | 'width' | 'height'` | automatically resize the plot to fill the width and/or height of its parent element
39
+ | `debounce` | `number|DebounceOptions` | debounce all changes to the plot
40
+ | `libPlotly` | `Plotly|null|undefined` | an alternative Plotly bundle to use; if undefined, it defaults to the `plotly.js-dist` package; if null, no plot will be drawn and no library will be downloaded
41
+ | `bind:element` | `HTMLDivElement` | the HTML element wrapping the plot
42
+ | `bind:plot` | `PlotlyHTMLElement` | the inner HTML element containing the plot
43
+
44
+
45
+ # Roadmap
46
+ * [x] add autosizing
47
+ * [x] add debouncing
48
+ * [ ] add unit tests
49
+ * [ ] add SSR rendering to an image
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "svelte-plotly.js",
3
+ "version": "0.1.0",
4
+ "author": {
5
+ "name": "Michal Grňo (m93a)",
6
+ "url": "https://github.com/m93a/"
7
+ },
8
+ "description": "Unoficial Plotly package for Svelte and SvelteKit",
9
+ "bugs": {
10
+ "url": "https://github.com/m93a/svelte-plotly.js"
11
+ },
12
+ "packageManager": "yarn@1.22.5",
13
+ "license": "MIT",
14
+ "paths": {
15
+ "*": [
16
+ "src/types/*.d.ts"
17
+ ]
18
+ },
19
+ "devDependencies": {
20
+ "@sveltejs/adapter-auto": "next",
21
+ "@sveltejs/kit": "next",
22
+ "@types/lodash": "^4.14.182",
23
+ "@types/plotly.js": "^1.54.20",
24
+ "@typescript-eslint/eslint-plugin": "^5.10.1",
25
+ "@typescript-eslint/parser": "^5.10.1",
26
+ "eslint": "^8.12.0",
27
+ "eslint-config-prettier": "^8.3.0",
28
+ "eslint-plugin-svelte3": "^4.0.0",
29
+ "prettier": "^2.5.1",
30
+ "prettier-plugin-svelte": "^2.5.0",
31
+ "sass": "^1.51.0",
32
+ "svelte": "^3.44.0",
33
+ "svelte-check": "^2.2.6",
34
+ "svelte-preprocess": "^4.10.1",
35
+ "svelte2tsx": "^0.5.9",
36
+ "tslib": "^2.3.1",
37
+ "typescript": "~4.6.2"
38
+ },
39
+ "type": "module",
40
+ "dependencies": {
41
+ "lodash": "^4.17.21",
42
+ "plotly.js-dist": "^2.12.1"
43
+ },
44
+ "exports": {
45
+ "./package.json": "./package.json",
46
+ "./Plot.svelte": "./Plot.svelte"
47
+ }
48
+ }