1.2.0 • Published 11 months ago

svelte-plotly.js v1.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Plotly.js for Svelte

This is an unofficial package that lets you efficiently use plotly.js inside your Svelte or SvelteKit app.

Usage

  1. Install using pnpm add svelte-plotly.js or npm install svelte-plotly.js.
  2. Choose a Plotly distribution:
    • If you want to use plotly.js-dist, just install it.
    • If you want to use a different distribution, take a look at this section.
  3. Use in your app:
<script lang="ts">
  import Plot from 'svelte-plotly.js';

  const data = [
    {
      x: [1, 2, 3, 4, 5],
      y: [1, 2, 4, 8, 16]
    }
  ];
</script>

<Plot
  {data}
  layout={{
    margin: { t: 0 }
  }}
  fillParent="width"
  debounce={250}
/>

Custom Plotly distribution

Let's say you want to use svelte-plotly.js with a custom Plotly.js distribution, for example plotly.js-basic-dist-min. If you use Vite for bundling, the recommended way to do this would be to install plotly.js-basic-dist-min and then register it as an alias for plotly.js-dist by adding the following to your vite.config.js:

resolve: {
  alias: {
    "plotly.js-dist": "plotly.js-basic-dist-min",
    lodash: "lodash-es",
  },
},

If you don't use Vite, or this approach doesn't work for you, you can also use the libPlotly prop of the Plotly component:

<script lang="ts">
  import Plot from 'svelte-plotly.js';
  import libPlotly from 'plotly.js-basic-dist-min';
</script>

<Plot {libPlotly} ... />

Using FontAwesome icons

Plotly allows you to modify the chart's mode bar (ie. the buttons in the chart's top right corner). However, you can't use any svelte component – it must be an icon in Plotly's specified format. This library includes a helper function faToPlotly that converts a FontAwesome icon to exactly what Plotly needs.

<script lang="ts">
  import Plot, { faToPlotly } from 'svelte-plotly.js';
  import { faExpand, faCompress } from '@fortawesome/free-solid-svg-icons';
  let fullscreen = false;
</script>

<Plot
    config={{
      modeBarButtonsToAdd: [
        {
          name: 'fullscreen',
          title: fullscreen ? 'Disable Fullscreen' : 'Enable Fullscreen',
          icon: fullscreen ? faToPlotly(faCompress) : faToPlotly(faExpand),
          click: () => (fullscreen = !fullscreen)
        }
      ],
    }}
    ...
/>

Properties

PropTypeDescription
required dataData[]array of trace data, see https://plot.ly/javascript/reference/
layoutPartial<Layout>layout of the plot, see https://plot.ly/javascript/reference/#layout
configPartial<Config>configuration, see https://plot.ly/javascript/configuration-options/
classstringclass that will be passed to the HTML element wrapping the plot
fillParentboolean \| 'width' \| 'height'automatically resize the plot to fill the width and/or height of its parent element
debouncenumber \| DebounceOptionsdebounce all changes to the plot
libPlotlyPlotly \| null \| undefinedan 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
configReactivityStrategy'none' \| 'static-plot'walkaround for an upstream bug causing config not to update, enabled by default
bind:elementHTMLDivElementthe HTML element wrapping the plot
bind:plotPlotlyHTMLElementthe inner HTML element containing the plot

Events

NOTE: Due to Plotly's atrocious documentation, most events aren't even mentioned anywhere. Links to source code are provided as the bare minimum information about each event.

PropCallback argumentDescription
on:afterExport?
on:afterPlottriggers each time a chart is plotted, or re-plotted after the restyling or relayout of a plot (docs), (src)
on:animatedtriggers once an animation finished playing (demo), (docs), (src 1), (src 2)
on:animating? (src)
on:animatingFrameFrameAnimationEvent? (src)
on:animationInterrupted? (src)
on:autoSize? (docs)
on:beforeExport?
on:beforeHoverPlotMouseEvent?
on:beforePlotBeforePlotEvent? (src)
on:buttonClickedButtonClickedEvent?
on:clickPlotMouseEvent? (docs 1), (docs 2)
on:clickAnnotationClickAnnotationEvent?
on:deselect? (docs)
on:doubleClick? (docs)
on:framework?
on:hoverPlotHoverEvent? (docs)
on:legendClickLegendClickEvent? (docs)
on:legendDoubleClickLegendClickEvent? (docs)
on:reactPlotUpdateEvent?
on:redraw? (docs)
on:relayoutPlotRelayoutEvent? (docs)
on:relayoutingPlotRelayoutEvent? (docs)
on:restylePlotRestyleEvent? (docs)
on:selectedPlotSelectionEvent? (docs)
on:selectingPlotSelectionEvent? (docs)
on:sliderChangeSliderChangeEvent?
on:sliderEndSliderEndEvent?
on:sliderStartSliderStartEvent?
on:sunburstClickSunburstClickEvent?
on:transitioned?
on:transitioning?
on:transitionInterrupted?
on:unhoverPlotMouseEvent?
on:updatePlotUpdateEvent?
on:webGLContextLost? (docs)

Roadmap

  • add autosizing
  • add debouncing
  • add event redirecting
  • add unit tests
  • add SSR rendering to an image
1.2.0

11 months ago

1.1.0

11 months ago

1.0.0

11 months ago

1.1.0-rc1

11 months ago

0.5.0

11 months ago

0.4.0

1 year ago

0.5.1

11 months ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.1

3 years ago

0.1.2

3 years ago

0.2.0

3 years ago

0.1.7

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago