Skip to content
OpenChart

Export

Download a chart as SVG, PNG or CSV in the browser, and where static rendering takes over.

A live chart exports itself: chart.export("svg") returns the chart as SVG markup, "png" as an image and "csv" as its data rows. Each download button under these charts makes one of those calls and saves the result.

20162017201820192020202120222023CoalGasNuclearHydroWindSolar1.443.5Coal's share fell from 31% to 16% between 2016and 2023Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)20162017201820192020202120222023CoalGasNuclearHydroWindSolar1.443.5Coal's share fell from 31% to 16% between 2016and 2023Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)
Darker cells are a larger share of the year's generation. Source: Our World in Data (owid/electricity-mix).
{
  "mark": "rect",
  "data": [
    {
      "year": "2016",
      "source": "Coal",
      "share": 31.2
    },
    {
      "year": "2016",
      "source": "Gas",
      "share": 34.8
    },
    {
      "year": "2016",
      "source": "Nuclear",
      "share": 20.3
    },
    {
      "year": "2016",
      "source": "Hydro",
      "share": 6.6
    },
    {
      "year": "2016",
      "source": "Wind",
      "share": 5.7
    },
    {
      "year": "2016",
      "source": "Solar",
      "share": 1.4
    },
    // … 42 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "source",
      "type": "nominal"
    },
    "color": {
      "field": "share",
      "type": "quantitative",
      "scale": {
        "scheme": "blues"
      },
      "format": ".1f"
    }
  },
  "chrome": {
    "title": "Coal's share fell from 31% to 16% between 2016 and 2023",
    "subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "rect",
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "source",
      "type": "nominal"
    },
    "color": {
      "field": "share",
      "type": "quantitative",
      "scale": {
        "scheme": "blues"
      },
      "format": ".1f"
    }
  },
  "chrome": {
    "title": "Coal's share fell from 31% to 16% between 2016 and 2023",
    "subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)"
  }
};

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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "rect",
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "source",
      "type": "nominal"
    },
    "color": {
      "field": "share",
      "type": "quantitative",
      "scale": {
        "scheme": "blues"
      },
      "format": ".1f"
    }
  },
  "chrome": {
    "title": "Coal's share fell from 31% to 16% between 2016 and 2023",
    "subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)"
  }
};
</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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "rect",
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "source",
      "type": "nominal"
    },
    "color": {
      "field": "share",
      "type": "quantitative",
      "scale": {
        "scheme": "blues"
      },
      "format": ".1f"
    }
  },
  "chrome": {
    "title": "Coal's share fell from 31% to 16% between 2016 and 2023",
    "subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)"
  }
};
</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 = [/* 48 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "rect",
  "encoding": {
    "x": {
      "field": "year",
      "type": "nominal"
    },
    "y": {
      "field": "source",
      "type": "nominal"
    },
    "color": {
      "field": "share",
      "type": "quantitative",
      "scale": {
        "scheme": "blues"
      },
      "format": ".1f"
    }
  },
  "chrome": {
    "title": "Coal's share fell from 31% to 16% between 2016 and 2023",
    "subtitle": "Share of US generation from coal, gas, nuclear, hydro, wind and solar, 2016 to 2023 (%)"
  }
};

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

The SVG is the chart with its title, subtitle, axes and legend, and with no animation, so it opens finished in a vector editor or a CMS. The PNG is the same image at twice its on-screen size by default. The CSV holds the chart’s rows with a header line: one row per year and source here.

export returns the chart as it is drawn, so a chart in dark mode exports in dark mode; with this site’s chart theme that is light text on a transparent background. These buttons render a separate light copy on white paper and export that, so a file saved from this site in dark mode still reads on a white page.

The calls behind the buttons

JanMarMayJulSepNov05101520253035Temperature (°C)New York's average daily high peaked at 30.2°Cin July 2023Average daily low to average daily high by month, Central Park, 2023 (°C)JanMarMayJulSepNov05101520253035Temperature (°C)New York's average daily high peaked at 30.2°Cin July 2023Average daily low to average daily high by month, Central Park, 2023 (°C)
Each bar spans a month's average daily low to its average daily high. Source: NOAA GHCN Daily, Central Park (USW00094728).
{
  "mark": {
    "type": "errorbar",
    "ticks": true
  },
  "data": [
    {
      "month": "Jan",
      "low": 3.5,
      "high": 9.3
    },
    {
      "month": "Feb",
      "low": 0.9,
      "high": 9.2
    },
    {
      "month": "Mar",
      "low": 3.2,
      "high": 10.8
    },
    {
      "month": "Apr",
      "low": 9.4,
      "high": 19
    },
    {
      "month": "May",
      "low": 12,
      "high": 22.1
    },
    {
      "month": "Jun",
      "low": 16.8,
      "high": 25.4
    },
    {
      "month": "Jul",
      "low": 22.1,
      "high": 30.2
    },
    {
      "month": "Aug",
      "low": 20.2,
      "high": 27.6
    },
    {
      "month": "Sep",
      "low": 17.3,
      "high": 24.3
    },
    {
      "month": "Oct",
      "low": 12.4,
      "high": 19.3
    },
    {
      "month": "Nov",
      "low": 4.5,
      "high": 11.8
    },
    {
      "month": "Dec",
      "low": 4.2,
      "high": 9.8
    }
  ],
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "low",
      "type": "quantitative",
      "axis": {
        "title": "Temperature (°C)"
      }
    },
    "y2": {
      "field": "high",
      "type": "quantitative"
    }
  },
  "chrome": {
    "title": "New York's average daily high peaked at 30.2°C in July 2023",
    "subtitle": "Average daily low to average daily high by month, Central Park, 2023 (°C)"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

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

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "errorbar",
    "ticks": true
  },
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "low",
      "type": "quantitative",
      "axis": {
        "title": "Temperature (°C)"
      }
    },
    "y2": {
      "field": "high",
      "type": "quantitative"
    }
  },
  "chrome": {
    "title": "New York's average daily high peaked at 30.2°C in July 2023",
    "subtitle": "Average daily low to average daily high by month, Central Park, 2023 (°C)"
  }
};

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 = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "errorbar",
    "ticks": true
  },
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "low",
      "type": "quantitative",
      "axis": {
        "title": "Temperature (°C)"
      }
    },
    "y2": {
      "field": "high",
      "type": "quantitative"
    }
  },
  "chrome": {
    "title": "New York's average daily high peaked at 30.2°C in July 2023",
    "subtitle": "Average daily low to average daily high by month, Central Park, 2023 (°C)"
  }
};
</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 = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "errorbar",
    "ticks": true
  },
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "low",
      "type": "quantitative",
      "axis": {
        "title": "Temperature (°C)"
      }
    },
    "y2": {
      "field": "high",
      "type": "quantitative"
    }
  },
  "chrome": {
    "title": "New York's average daily high peaked at 30.2°C in July 2023",
    "subtitle": "Average daily low to average daily high by month, Central Park, 2023 (°C)"
  }
};
</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 = [/* 12 rows, as in the Spec tab; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "errorbar",
    "ticks": true
  },
  "encoding": {
    "x": {
      "field": "month",
      "type": "nominal"
    },
    "y": {
      "field": "low",
      "type": "quantitative",
      "axis": {
        "title": "Temperature (°C)"
      }
    },
    "y2": {
      "field": "high",
      "type": "quantitative"
    }
  },
  "chrome": {
    "title": "New York's average daily high peaked at 30.2°C in July 2023",
    "subtitle": "Average daily low to average daily high by month, Central Park, 2023 (°C)"
  }
};

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

SVG and CSV come back as strings straight away; PNG takes a moment to render, so it returns a promise of a Blob. The library has no download helper, so a page saves each file itself:

download.ts
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" }), "chart.svg");
save(await chart.export("png", { dpi: 3 }), "chart.png");
save(new Blob([chart.export("csv")], { type: "text/csv" }), "chart.csv");

The demos name each file after the demo, such as output-export-range.svg. In React, Vue and Svelte the useChart hook or composable returns the chart instance, and its export method is the same.

Other formats

chart.export("svg-with-fonts") embeds the fonts, so the file renders the same on a machine without them. chart.export("gif") turns the entrance into an animated GIF for newsletters and other pages that block video and embedded charts; it needs the optional gifenc package. The Export API reference covers both, with their options. To make SVGs on a server or at build time, with no browser, use renderStaticSVG from static rendering.

Playback charts

A chart with encoding.time exports the frame on screen. Pause a bubble playback on 1990 and chart.export("svg") or "png" gives the 1990 bubbles on the same axes as every other frame. The player below the chart is not part of the file. chart.export("gif") records the whole run instead, from the first period through the final hold, with trails and the period label. chart.export("csv") returns every row of the spec’s data, one per key and period, whichever period is showing.

Static rendering draws the final period. Pass t, a period index counted from zero, to draw another one:

static-frame.ts
import { renderStaticSVG } from "@opendata-ai/openchart-vanilla/static";

const first = renderStaticSVG(spec, { t: 0 });

Fields that matter

Call What it returns
chart.export("svg") The chart as an SVG string, finished and without animation.
chart.export("png", { dpi }) A promise of a PNG Blob. dpi scales the image; the default is 2.
chart.export("csv") The chart’s data as comma-separated text with a header row.
chart.export("svg-with-fonts") A promise of an SVG string with its fonts embedded.
chart.export("gif", { loop }) A promise of an animated GIF Blob of the entrance.
table.export("csv") A table’s rows in its current sort and search, ignoring pagination.