Skip to content
OpenChart

Export API

Export charts as SVG, PNG, JPG, WebP, CSV or an animated GIF, from a chart instance, a standalone function or a framework component.

Every mounted chart can export itself as SVG, PNG, JPG, WebP, CSV or an animated GIF through chart.export(). Tables export CSV. The standalone exportSVG, exportSVGWithFonts, exportPNG, exportJPG, exportWEBP and exportCSV functions take an SVG element (or rows, for CSV) instead of an instance. exportSpecSequence(specs, options) mounts an offscreen chart, tweens it through a list of specs and returns a GIF Blob; options needs width and height, and like GIF export it needs the optional gifenc package. The React, Vue and Svelte packages re-export all of them, along with ExportFormatUnsupportedError, which WebP export rejects with in browsers whose canvas cannot encode it.

See it: Export.

Export

Vanilla API

chart.export() on the ChartInstance returned by createChart takes 'svg', 'svg-with-fonts', 'png', 'jpg', 'webp', 'gif' or 'csv'. The common ones:

const chart = createChart(container, spec);

// SVG: synchronous, returns an XML string
const svgString = chart.export("svg");

// PNG: async, returns a Blob. Optional DPI scaling (default: 2x for retina).
const pngBlob = await chart.export("png");
const hiResPng = await chart.export("png", { dpi: 3 });

// WebP: async, returns a Blob. Keeps transparency like PNG; quality 0 to 1 (default 0.92).
// Rejects with ExportFormatUnsupportedError where the canvas cannot encode WebP (Safari returns a PNG).
const webpBlob = await chart.export("webp", { quality: 0.8 });

// CSV: synchronous, returns the data as comma-separated text
const csvString = chart.export("csv");

Tables support CSV export through TableInstance:

const table = createTable(container, tableSpec);
const csvString = table.export("csv");

Table CSV export respects the current sort and search state but exports all rows (ignores pagination), so you get the full filtered dataset.

Supplied export fonts

Pass fonts to SVG-with-fonts, PNG, JPG or WebP export when the font bytes are available to your application. A supplied family, weight and style takes precedence over page stylesheets. format defaults to 'woff2', style to 'normal', and weight to 400.

const svg = await chart.export('svg-with-fonts', {
  fonts: [{ family: 'Example Sans', weight: '100 900', data: fontBytes }],
  onFontWarning: (warning) => console.warn(warning),
});

A two-number weight string is an inclusive variable-font range (from 1 through 1000, lower bound first). The export embeds it once with that font-weight range and uses it for every covered text weight. A matching single weight takes precedence over a range; among overlapping ranges, the narrowest wins. Equal spans use the first supplied match. Faces are emitted widest first and singles last so CSS uses the same precedence. Invalid weights such as 'bold-ish' or '900 100' fall back to a single weight of 400. Text outside the supplied weights still needs a page font source or produces an onFontWarning callback.

Animated GIF export

GIF export captures a chart’s entrance animation as a looping (or play-once) image. It’s the format to reach for when you need an animated chart to autoplay inline somewhere that blocks iframes, video players, and animated SVG, like a Substack or newsletter article.

The setup is a little more involved than the other formats, so read this before wiring it up.

Install the optional encoder. GIF encoding uses gifenc, which is an optional peer dependency, it is not bundled with openchart and not pulled in unless you export GIFs. Install it yourself:

npm install gifenc

Calling chart.export("gif") without it throws a clear error telling you to install it. The encoder loads lazily (dynamic import), so it never weighs down your main bundle, only the code path that actually exports a GIF pays for it.

How it works (and why the chart doesn’t need to be mid-animation). OpenChart’s entrance animations are pure CSS, and CSS animation state lives in computed style, which doesn’t survive SVG serialization. So GIF export does not screen-record the live animation. Instead it reproduces the entrance deterministically from the chart’s final, settled render: it computes each mark’s clip-path/opacity at every frame in JS, stamps them inline, rasterizes, and encodes. The upshot for you: just call export("gif") on a normally-rendered chart. No offscreen mounting, no forcing the animation to replay.

const chart = createChart(container, spec);

// Async, returns a Blob. Reproduces the entrance animation, then holds the
// final frame (play-once is the default).
const gifBlob = await chart.export("gif");

// Loop forever instead of holding the last frame:
const looping = await chart.export("gif", { loop: true });

Options (GIFExportOptions):

Option Default Meaning
loop false false plays once and holds the final frame; true loops forever; a positive number sets an explicit loop count. Note 0 maps to gifenc’s loop-forever (same as true), so use false, not 0, for play-once.
fps 25 Frames per second captured.
dpi 2 Resolution scaling, same as PNG. 2 keeps text crisp.
durationMs animation total Override the capture window length.
backgroundColor the SVG’s root background (or white) Opaque fill. GIF can’t carry partial alpha. A transparent, currentColor or url() root background falls back to white.
embedFonts true Embed fonts so text matches on-screen.

Scope and caveats:

  • Every instance that animates an entrance on screen exports one: charts, bar lists, tile maps, sankeys and geo maps. Tables and graphs have no GIF export.
  • Frames follow the same keyframes, curves and timing the browser plays: bar and stack reveals, label slides, line draws, map fill colour sweeps and point scale-ups, bar list row slides. A map that has a focus keeps its dimmed features at the dim.
  • It evaluates the author’s ease (including snappy, gentle and spring), not just smooth.
  • The motion is a faithful JS re-creation of the CSS entrance (same durations, stagger), not a pixel-for-pixel screen capture.
  • GIF is a 256-color format. Solid fills and text hold up well at dpi: 2; heavy gradients will band. File size grows with duration, dimensions, and fps.
  • A chart with no entrance animation configured produces a single-frame (effectively static) GIF.
  • Some browsers restart a play-once GIF from the first frame when it scrolls out of view and back. For a scroll-past article this is a non-issue.

You can also import exportGIF directly from the subpath if you’re not going through a chart instance:

import { exportGIF } from "@opendata-ai/openchart-vanilla/gif";

const gifBlob = await exportGIF(svgElement, resolvedAnimation, { loop: false });

Download helper

The library doesn’t include download utilities, but wiring one up is straightforward:

function downloadBlob(blob: Blob, filename: string) {
  const url = URL.createObjectURL(blob);
  const a = document.createElement("a");
  a.href = url;
  a.download = filename;
  a.click();
  URL.revokeObjectURL(url);
}

function downloadString(content: string, filename: string, mime: string) {
  const blob = new Blob([content], { type: mime });
  downloadBlob(blob, filename);
}

// Usage
const svgString = chart.export("svg");
downloadString(svgString, "chart.svg", "image/svg+xml");

const pngBlob = await chart.export("png");
downloadBlob(pngBlob, "chart.png");

const csvString = chart.export("csv");
downloadString(csvString, "data.csv", "text/csv");

const gifBlob = await chart.export("gif", { loop: false }); // needs `gifenc` installed
downloadBlob(gifBlob, "chart.gif");

Framework export

Each <Chart> component exposes its instance: in React through a ref (ref.current.instance), in Vue through a template ref (chartRef.value.instance) and in Svelte through bind:this (chart.getInstance()). The useChart hook or composable also returns it:

React:

import { useChart, type ChartSpec } from "@opendata-ai/openchart-react";

function ExportableChart({ spec }: { spec: ChartSpec }) {
  const { ref, chart } = useChart(spec, { darkMode: "off" });

  const handleExport = async (format: "svg" | "png" | "csv") => {
    if (!chart) return;
    if (format === "png") {
      const blob = await chart.export("png");
      downloadBlob(blob, "chart.png");
    } else {
      const str = chart.export(format);
      downloadString(str, `chart.${format}`, format === "svg" ? "image/svg+xml" : "text/csv");
    }
  };

  return (
    <div>
      <button onClick={() => handleExport("svg")}>Export SVG</button>
      <button onClick={() => handleExport("png")}>Export PNG</button>
      <div ref={ref} style={{ width: "100%", height: 400 }} />
    </div>
  );
}

Vue:

<script setup lang="ts">
import { useChart } from "@opendata-ai/openchart-vue";

const { containerRef, chart } = useChart(spec, { darkMode: "off" });

async function handleExport(format: "svg" | "png" | "csv") {
  if (!chart.value) return;
  if (format === "png") {
    const blob = await chart.value.export("png");
    downloadBlob(blob, "chart.png");
  } else {
    const str = chart.value.export(format);
    downloadString(str, `chart.${format}`, format === "svg" ? "image/svg+xml" : "text/csv");
  }
}
</script>

<template>
  <button @click="handleExport('svg')">Export SVG</button>
  <button @click="handleExport('png')">Export PNG</button>
  <div ref="containerRef" style="width: 100%; height: 400px" />
</template>

Svelte:

<script lang="ts">
import { useChart } from "@opendata-ai/openchart-svelte";

// Keep the object: `chart` is a getter, and destructuring would copy its first value, null.
const c = useChart(() => spec, () => ({ darkMode: "off" }));

async function handleExport(format: "svg" | "png" | "csv") {
  if (!c.chart) return;
  if (format === "png") {
    const blob = await c.chart.export("png");
    downloadBlob(blob, "chart.png");
  } else {
    const str = c.chart.export(format);
    downloadString(str, `chart.${format}`, format === "svg" ? "image/svg+xml" : "text/csv");
  }
}
</script>

<button onclick={() => handleExport("svg")}>Export SVG</button>
<button onclick={() => handleExport("png")}>Export PNG</button>
<div use:c.action style="width: 100%; height: 400px"></div>