Export a chart as PNG, SVG or GIF from JavaScript
Save an OpenChart chart as SVG, PNG or GIF with chart.export, and see how large each file gets for one real chart.
Made with OpenChart. How to build this bar chart
Turnout rose in every state and DC from 2016 to 2020, and most in Hawaii. To put that chart in a slide deck or a newsletter you need it as a file: SVG for web pages and vector editors, PNG for slides and social posts, and GIF where video and embedded charts are blocked. All three come from chart.export(format) on the chart that createChart returns, and the buttons under this chart call it.
Made with OpenChart. How to build this bar chart
SVG
chart.export("svg") returns the chart as a string, immediately. The SVG is finished, with title, subtitle, axes and legend, and has no animation, so it opens in an editor or a CMS as it looks on screen. It names its fonts without including them. await chart.export("svg-with-fonts") embeds the web fonts the page loaded, as base64, so the file renders the same on a machine without them; a font the browser can’t fetch is skipped.
In React, Vue and Svelte the useChart hook or composable returns the same chart instance, so export works the same way. To make SVGs on a server with no browser, use renderStaticSVG from static rendering. The export docs list the other formats, including CSV.
The library has no download helper. Wrap the result in a Blob and click a temporary link:
function save(file: Blob, name: string) {
const url = URL.createObjectURL(file);
const link = document.createElement("a");
link.href = url;
link.download = name;
link.click();
setTimeout(() => URL.revokeObjectURL(url), 10_000);
}
save(new Blob([chart.export("svg")], { type: "image/svg+xml" }), "turnout.svg");PNG
await chart.export("png", { dpi }) returns a Blob. The chart is laid out at its current width and drawn at dpi times the resolution, so the setting changes the pixel count and leaves the layout alone. The default of 2 suits slides and posts; raise it for print or a large display.
A PNG leaves the page’s text behind, so the image has to carry its own context. The export includes chrome.title, subtitle, source and byline, so give every chart you plan to export a source, and a byline if you credit an author. Both charts here set chrome.source, which is why the source line sits inside the chart as well as in the caption.
A PNG is also transparent unless the theme sets a background, which makes dark text unreadable on a dark slide. Set theme.colors.background in the spec to get an opaque image.
This arrow chart works as an image because its title states the finding, so a reader needs nothing outside the picture.
Made with OpenChart. How to build this range chart
Animated GIF
await chart.export("gif") records the chart’s entrance as an animated GIF. It needs the optional gifenc package, which you install yourself with npm install gifenc. Without it the call throws an error that says so. You can call it on a finished chart, because the library rebuilds the entrance frame by frame.
A GIF plays once and holds its last frame unless you pass loop: true, and it is limited to 256 colors. A chart with no entrance animation exports as a single frame. Lowering dpi and fps (frames per second, 25 by default) is how you bring the file size down.
A chart with encoding.time records its playback instead of an entrance: every period from first to last, then the end hold, at the length its playback block sets, with loop taken from that block too. Pass durationMs for a shorter file.
File sizes
These are the turnout chart’s exports from Chromium, with the chart 656px wide and 420px tall, the size it has in this article’s column. Your sizes will differ with the chart.
| Call | Pixels | Size |
|---|---|---|
export("svg") |
vector | 23 KB |
export("png", { dpi: 1 }) |
656 × 420 | 61 KB |
export("png") (dpi 2) |
1312 × 840 | 140 KB |
export("png", { dpi: 3 }) |
1968 × 1260 | 230 KB |
export("gif") (dpi 2, 25 fps) |
1312 × 840 | 1.7 MB |
export("gif", { dpi: 1 }) |
656 × 420 | 700 KB |
export("gif", { dpi: 1, fps: 12 }) |
656 × 420 | 340 KB |
Charts with many points
A scatter of thousands of points draws its dots on a canvas layer, and exports still re-render the whole chart. A PNG is drawn from an all-vector copy of the chart at any point count. An SVG stays all vector up to 5,000 points, and above that the dots go in as one embedded image while the text and gridlines remain vector.
The spec
mark: { type: "range", style: "arrow" } draws one arrow per row, from the x value (the 2005 rate) to the x2 value (the 2024 rate), so the arrowhead shows the direction of the change. y.sort orders the states by the drop field, and categories run from top to bottom in sort order, so "descending" puts the largest drop at the top.
The title on x and x2 names each end of the arrow in the tooltip, chrome.source puts the source line inside the exported image, and theme.colors.categorical gives the arrows a lighter teal on a dark page.
Image size and dark mode
Export at the container width you want the image to have, because the image takes the chart’s current size. A chart shown in dark mode exports with its dark colors.
To make a GIF that steps through several specs instead of one entrance, see exportSpecSequence in the export API reference.