# 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.

```json
{
  "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
    },
    {
      "year": "2017",
      "source": "Coal",
      "share": 30.7
    },
    {
      "year": "2017",
      "source": "Gas",
      "share": 33
    },
    {
      "year": "2017",
      "source": "Nuclear",
      "share": 20.5
    },
    {
      "year": "2017",
      "source": "Hydro",
      "share": 7.5
    },
    {
      "year": "2017",
      "source": "Wind",
      "share": 6.5
    },
    {
      "year": "2017",
      "source": "Solar",
      "share": 2
    },
    {
      "year": "2018",
      "source": "Coal",
      "share": 28.2
    },
    {
      "year": "2018",
      "source": "Gas",
      "share": 36
    },
    {
      "year": "2018",
      "source": "Nuclear",
      "share": 19.8
    },
    {
      "year": "2018",
      "source": "Hydro",
      "share": 7
    },
    {
      "year": "2018",
      "source": "Wind",
      "share": 6.7
    },
    {
      "year": "2018",
      "source": "Solar",
      "share": 2.3
    },
    {
      "year": "2019",
      "source": "Coal",
      "share": 23.9
    },
    {
      "year": "2019",
      "source": "Gas",
      "share": 39.2
    },
    {
      "year": "2019",
      "source": "Nuclear",
      "share": 20
    },
    {
      "year": "2019",
      "source": "Hydro",
      "share": 7
    },
    {
      "year": "2019",
      "source": "Wind",
      "share": 7.3
    },
    {
      "year": "2019",
      "source": "Solar",
      "share": 2.6
    },
    {
      "year": "2020",
      "source": "Coal",
      "share": 19.6
    },
    {
      "year": "2020",
      "source": "Gas",
      "share": 41.3
    },
    {
      "year": "2020",
      "source": "Nuclear",
      "share": 20.1
    },
    {
      "year": "2020",
      "source": "Hydro",
      "share": 7.1
    },
    {
      "year": "2020",
      "source": "Wind",
      "share": 8.6
    },
    {
      "year": "2020",
      "source": "Solar",
      "share": 3.3
    },
    {
      "year": "2021",
      "source": "Coal",
      "share": 22.2
    },
    {
      "year": "2021",
      "source": "Gas",
      "share": 39
    },
    {
      "year": "2021",
      "source": "Nuclear",
      "share": 19.3
    },
    {
      "year": "2021",
      "source": "Hydro",
      "share": 6.1
    },
    {
      "year": "2021",
      "source": "Wind",
      "share": 9.3
    },
    {
      "year": "2021",
      "source": "Solar",
      "share": 4.1
    },
    {
      "year": "2022",
      "source": "Coal",
      "share": 19.9
    },
    {
      "year": "2022",
      "source": "Gas",
      "share": 40.4
    },
    {
      "year": "2022",
      "source": "Nuclear",
      "share": 18.5
    },
    {
      "year": "2022",
      "source": "Hydro",
      "share": 6
    },
    {
      "year": "2022",
      "source": "Wind",
      "share": 10.4
    },
    {
      "year": "2022",
      "source": "Solar",
      "share": 4.9
    },
    {
      "year": "2023",
      "source": "Coal",
      "share": 16.2
    },
    {
      "year": "2023",
      "source": "Gas",
      "share": 43.5
    },
    {
      "year": "2023",
      "source": "Nuclear",
      "share": 18.6
    },
    {
      "year": "2023",
      "source": "Hydro",
      "share": 5.8
    },
    {
      "year": "2023",
      "source": "Wind",
      "share": 10.1
    },
    {
      "year": "2023",
      "source": "Solar",
      "share": 5.8
    }
  ],
  "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 (%)"
  }
}
```

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

```json
{
  "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)"
  }
}
```

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:

```ts title="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](/docs/api/export/) covers both, with their options. To make SVGs on a server or at build time, with no browser, use `renderStaticSVG` from [static rendering](/docs/get-started/static-rendering/).

## Playback charts

A chart with `encoding.time` exports the frame on screen. Pause a [bubble playback](/docs/correlation/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:

```ts title="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. |

## Related

- [Export API reference](/docs/api/export/) documents every format and its options.
- [Heatmap](/docs/correlation/heatmap/) and [Error bars](/docs/distribution/error-bars/) cover the two chart types on this page.
- [Static rendering](/docs/get-started/static-rendering/) draws SVGs at build time.
