Skip to content
OpenChart

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.

024681012Change, pointsHawaiiArizonaCaliforniaWashingtonUtahDistrict of ColumbiaTexasMichiganSouth CarolinaTennessee+12.2+10.7+10.5+9.7+9.2+8.5+8.4+7.7+7.4+7.4Turnout rose in all 51 jurisdictions from 2016 to 2020, most inHawaiiThe 10 largest gains in turnout of the voting-eligible population, percentage pointsSource: University of Florida Election Lab, Voting-Eligible Population turnout (Michael McDonald)024681012Change, pointsHawaiiArizonaCaliforniaWashingtonUtahDistrict of ColumbiaTexasMichiganSouth CarolinaTennessee+12.2+10.7+10.5+9.7+9.2+8.5+8.4+7.7+7.4+7.4Turnout rose in all 51 jurisdictions from 2016 to 2020, most inHawaiiThe 10 largest gains in turnout of the voting-eligible population, percentage pointsSource: University of Florida Election Lab, Voting-Eligible Population turnout (Michael McDonald)
Largest turnout gains, 2016 to 2020, with export buttons. Source: University of Florida Election Lab, Voting-Eligible Population turnout (Michael McDonald).

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.

024681012Change, pointsHawaiiArizonaCaliforniaWashingtonUtahDistrict of ColumbiaTexasMichiganSouth CarolinaTennessee+12.2+10.7+10.5+9.7+9.2+8.5+8.4+7.7+7.4+7.4Turnout rose in all 51 jurisdictions from 2016 to 2020, most inHawaiiThe 10 largest gains in turnout of the voting-eligible population, percentage pointsSource: University of Florida Election Lab, Voting-Eligible Population turnout (Michael McDonald)024681012Change, pointsHawaiiArizonaCaliforniaWashingtonUtahDistrict of ColumbiaTexasMichiganSouth CarolinaTennessee+12.2+10.7+10.5+9.7+9.2+8.5+8.4+7.7+7.4+7.4Turnout rose in all 51 jurisdictions from 2016 to 2020, most inHawaiiThe 10 largest gains in turnout of the voting-eligible population, percentage pointsSource: University of Florida Election Lab, Voting-Eligible Population turnout (Michael McDonald)
Largest turnout gains, 2016 to 2020, with export buttons. Source: University of Florida Election Lab, Voting-Eligible Population turnout (Michael McDonald).

Made with OpenChart. How to build this bar chart

{
  "mark": {
    "type": "bar",
    "fill": "#0e7490"
  },
  "data": [
    {
      "state": "Hawaii",
      "gain": 12.2
    },
    {
      "state": "Arizona",
      "gain": 10.7
    },
    {
      "state": "California",
      "gain": 10.5
    },
    {
      "state": "Washington",
      "gain": 9.7
    },
    {
      "state": "Utah",
      "gain": 9.2
    },
    {
      "state": "District of Columbia",
      "gain": 8.5
    },
    {
      "state": "Texas",
      "gain": 8.4
    },
    {
      "state": "Michigan",
      "gain": 7.7
    },
    {
      "state": "South Carolina",
      "gain": 7.4
    },
    {
      "state": "Tennessee",
      "gain": 7.4
    }
  ],
  "encoding": {
    "x": {
      "field": "gain",
      "type": "quantitative",
      "axis": {
        "title": "Change, points",
        "format": ".0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": "-x"
    }
  },
  "labels": {
    "format": "+.1f"
  },
  "chrome": {
    "title": "Turnout rose in all 51 jurisdictions from 2016 to 2020, most in Hawaii",
    "subtitle": "The 10 largest gains in turnout of the voting-eligible population, percentage points",
    "source": "Source: University of Florida Election Lab, Voting-Eligible Population turnout (Michael McDonald)"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar",
    "fill": "#0e7490"
  },
  "encoding": {
    "x": {
      "field": "gain",
      "type": "quantitative",
      "axis": {
        "title": "Change, points",
        "format": ".0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": "-x"
    }
  },
  "labels": {
    "format": "+.1f"
  },
  "chrome": {
    "title": "Turnout rose in all 51 jurisdictions from 2016 to 2020, most in Hawaii",
    "subtitle": "The 10 largest gains in turnout of the voting-eligible population, percentage points",
    "source": "Source: University of Florida Election Lab, Voting-Eligible Population turnout (Michael McDonald)"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar",
    "fill": "#0e7490"
  },
  "encoding": {
    "x": {
      "field": "gain",
      "type": "quantitative",
      "axis": {
        "title": "Change, points",
        "format": ".0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": "-x"
    }
  },
  "labels": {
    "format": "+.1f"
  },
  "chrome": {
    "title": "Turnout rose in all 51 jurisdictions from 2016 to 2020, most in Hawaii",
    "subtitle": "The 10 largest gains in turnout of the voting-eligible population, percentage points",
    "source": "Source: University of Florida Election Lab, Voting-Eligible Population turnout (Michael McDonald)"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar",
    "fill": "#0e7490"
  },
  "encoding": {
    "x": {
      "field": "gain",
      "type": "quantitative",
      "axis": {
        "title": "Change, points",
        "format": ".0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": "-x"
    }
  },
  "labels": {
    "format": "+.1f"
  },
  "chrome": {
    "title": "Turnout rose in all 51 jurisdictions from 2016 to 2020, most in Hawaii",
    "subtitle": "The 10 largest gains in turnout of the voting-eligible population, percentage points",
    "source": "Source: University of Florida Election Lab, Voting-Eligible Population turnout (Michael McDonald)"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "bar",
    "fill": "#0e7490"
  },
  "encoding": {
    "x": {
      "field": "gain",
      "type": "quantitative",
      "axis": {
        "title": "Change, points",
        "format": ".0f"
      }
    },
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": "-x"
    }
  },
  "labels": {
    "format": "+.1f"
  },
  "chrome": {
    "title": "Turnout rose in all 51 jurisdictions from 2016 to 2020, most in Hawaii",
    "subtitle": "The 10 largest gains in turnout of the voting-eligible population, percentage points",
    "source": "Source: University of Florida Election Lab, Voting-Eligible Population turnout (Michael McDonald)"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

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.

405060708090Births per 1,000 womenUtahArizonaNevadaNew MexicoCaliforniaIdahoDistrict of ColumbiaTexasColoradoIllinoisThe fertility rate fell in all 51 jurisdictions, from 92.8 to 59.9in UtahThe 10 largest drops in the general fertility rate (births per 1,000 women ages 15-44), 2005 to 2024Source: CDC NCHS, Stats of the States: General Fertility Rate405060708090Births per 1,000 womenUtahArizonaNevadaNew MexicoCaliforniaIdahoDistrict of ColumbiaTexasColoradoIllinoisThe fertility rate fell in all 51 jurisdictions, from 92.8 to 59.9in UtahThe 10 largest drops in the general fertility rate (births per 1,000 women ages 15-44), 2005 to 2024Source: CDC NCHS, Stats of the States: General Fertility Rate
Live example. Source: CDC NCHS, Stats of the States: General Fertility Rate.

Made with OpenChart. How to build this range chart

{
  "mark": {
    "type": "range",
    "style": "arrow"
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "data": [
    {
      "state": "Utah",
      "rate_2005": 92.8,
      "rate_2024": 59.9,
      "drop": 32.9
    },
    {
      "state": "Arizona",
      "rate_2005": 80.6,
      "rate_2024": 53.2,
      "drop": 27.4
    },
    {
      "state": "Nevada",
      "rate_2005": 73.9,
      "rate_2024": 50.1,
      "drop": 23.8
    },
    {
      "state": "New Mexico",
      "rate_2005": 73,
      "rate_2024": 51.9,
      "drop": 21.1
    },
    {
      "state": "California",
      "rate_2005": 70.5,
      "rate_2024": 49.8,
      "drop": 20.7
    },
    {
      "state": "Idaho",
      "rate_2005": 78.9,
      "rate_2024": 58.6,
      "drop": 20.3
    },
    {
      "state": "District of Columbia",
      "rate_2005": 59.8,
      "rate_2024": 39.8,
      "drop": 20
    },
    {
      "state": "Texas",
      "rate_2005": 77.4,
      "rate_2024": 59.3,
      "drop": 18.1
    },
    {
      "state": "Colorado",
      "rate_2005": 69.6,
      "rate_2024": 51.6,
      "drop": 18
    },
    {
      "state": "Illinois",
      "rate_2005": 67,
      "rate_2024": 49.7,
      "drop": 17.3
    }
  ],
  "encoding": {
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": {
        "field": "drop",
        "order": "descending"
      }
    },
    "x": {
      "field": "rate_2005",
      "type": "quantitative",
      "title": "2005",
      "axis": {
        "title": "Births per 1,000 women"
      }
    },
    "x2": {
      "field": "rate_2024",
      "type": "quantitative",
      "title": "2024"
    }
  },
  "chrome": {
    "title": "The fertility rate fell in all 51 jurisdictions, from 92.8 to 59.9 in Utah",
    "subtitle": "The 10 largest drops in the general fertility rate (births per 1,000 women ages 15-44), 2005 to 2024",
    "source": "Source: CDC NCHS, Stats of the States: General Fertility Rate"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "arrow"
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "encoding": {
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": {
        "field": "drop",
        "order": "descending"
      }
    },
    "x": {
      "field": "rate_2005",
      "type": "quantitative",
      "title": "2005",
      "axis": {
        "title": "Births per 1,000 women"
      }
    },
    "x2": {
      "field": "rate_2024",
      "type": "quantitative",
      "title": "2024"
    }
  },
  "chrome": {
    "title": "The fertility rate fell in all 51 jurisdictions, from 92.8 to 59.9 in Utah",
    "subtitle": "The 10 largest drops in the general fertility rate (births per 1,000 women ages 15-44), 2005 to 2024",
    "source": "Source: CDC NCHS, Stats of the States: General Fertility Rate"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "arrow"
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "encoding": {
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": {
        "field": "drop",
        "order": "descending"
      }
    },
    "x": {
      "field": "rate_2005",
      "type": "quantitative",
      "title": "2005",
      "axis": {
        "title": "Births per 1,000 women"
      }
    },
    "x2": {
      "field": "rate_2024",
      "type": "quantitative",
      "title": "2024"
    }
  },
  "chrome": {
    "title": "The fertility rate fell in all 51 jurisdictions, from 92.8 to 59.9 in Utah",
    "subtitle": "The 10 largest drops in the general fertility rate (births per 1,000 women ages 15-44), 2005 to 2024",
    "source": "Source: CDC NCHS, Stats of the States: General Fertility Rate"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "arrow"
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "encoding": {
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": {
        "field": "drop",
        "order": "descending"
      }
    },
    "x": {
      "field": "rate_2005",
      "type": "quantitative",
      "title": "2005",
      "axis": {
        "title": "Births per 1,000 women"
      }
    },
    "x2": {
      "field": "rate_2024",
      "type": "quantitative",
      "title": "2024"
    }
  },
  "chrome": {
    "title": "The fertility rate fell in all 51 jurisdictions, from 92.8 to 59.9 in Utah",
    "subtitle": "The 10 largest drops in the general fertility rate (births per 1,000 women ages 15-44), 2005 to 2024",
    "source": "Source: CDC NCHS, Stats of the States: General Fertility Rate"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 10 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "range",
    "style": "arrow"
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "encoding": {
    "y": {
      "field": "state",
      "type": "nominal",
      "sort": {
        "field": "drop",
        "order": "descending"
      }
    },
    "x": {
      "field": "rate_2005",
      "type": "quantitative",
      "title": "2005",
      "axis": {
        "title": "Births per 1,000 women"
      }
    },
    "x2": {
      "field": "rate_2024",
      "type": "quantitative",
      "title": "2024"
    }
  },
  "chrome": {
    "title": "The fertility rate fell in all 51 jurisdictions, from 92.8 to 59.9 in Utah",
    "subtitle": "The 10 largest drops in the general fertility rate (births per 1,000 women ages 15-44), 2005 to 2024",
    "source": "Source: CDC NCHS, Stats of the States: General Fertility Rate"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

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.