visx draws charts as SVG inside your React app, but data has to come in from somewhere and finished charts often need to leave as files. This article covers the formats you'll meet on both sides and how to handle each one.
Input formats: getting data into visx
visx doesn't parse files itself. It expects a JavaScript array. Convert your source data into objects first:
| Format | How to load it | Good for |
|---|---|---|
| JSON | await fetch(url).then(r => r.json()) or a direct import | APIs, config-style data |
| CSV | A CSV parser such as d3-dsv's csvParse, then convert numbers | Spreadsheet exports |
| TSV | tsvParse from the same package | Tab-separated database dumps |
| GeoJSON / TopoJSON | Fetch as JSON, then pass features to @visx/geo | Maps |
CSV values arrive as strings, so convert them before charting:
import { csvParse } from 'd3-dsv';
const text = await fetch('/data/sales.csv').then((r) => r.text());
const data = csvParse(text, (row) => ({
label: row.label ?? '',
value: Number(row.value),
}));
Our downloads page has a sample CSV and JSON file in the same { label, value } shape the starter components use.
Exporting a chart as SVG
SVG is the native format and the best choice for print, design tools and anything that needs to scale. Grab the SVG element, serialise it and trigger a download:
function downloadSvg(svg: SVGSVGElement, name = 'chart.svg') {
const clone = svg.cloneNode(true) as SVGSVGElement;
clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
const source = new XMLSerializer().serializeToString(clone);
const url = URL.createObjectURL(new Blob([source], { type: 'image/svg+xml' }));
const a = Object.assign(document.createElement('a'), { href: url, download: name });
a.click();
URL.revokeObjectURL(url);
}
Styles applied through external CSS classes won't travel with the file. Set colours and fonts as SVG attributes or inline styles, as the visx components in our starter kit do, so the exported file looks the same everywhere.
Exporting as PNG
PNG is better for chat apps, email and social posts. Draw the serialised SVG onto a canvas at 2× scale for a crisp image:
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
canvas.width = width * 2;
canvas.height = height * 2;
canvas.getContext('2d')!.drawImage(img, 0, 0, canvas.width, canvas.height);
canvas.toBlob((blob) => { /* save blob as chart.png */ });
};
img.src = svgObjectUrl;
Add a white background rectangle before exporting. Otherwise transparent areas may show up black in some viewers. The playground on our homepage uses exactly this approach for its SVG and PNG buttons.
Which output format should you use?
| Destination | Best format | Why |
|---|---|---|
| Web page | Live SVG (the React component) | Interactive, accessible, sharp |
| Figma, Illustrator, print | SVG | Editable vectors, infinite scaling |
| Slides, docs, email | PNG at 2× | Works everywhere |
| Data hand-off | CSV or JSON | Lets others rebuild or audit the chart |
Source files: .tsx and .ts
The components you download from visx.app are .tsx files: TypeScript with JSX. If your project uses plain JavaScript, rename the file to .jsx and remove the type annotations, or keep TypeScript, since Vite and Next.js both compile it out of the box.
Next, read about the visx license and safe downloads before you ship exported charts or code in a commercial product.