Chart Export
Graphic Walker provides programmatic chart export through the IGWHandler ref. Export individual charts or iterate through all charts in a workbook.
Setup
Access the export API through a React ref:
import { useRef } from 'react';
import { GraphicWalker } from '@kanaries/graphic-walker';
import type { IGWHandler } from '@kanaries/graphic-walker';
function App() {
const gwRef = useRef<IGWHandler>(null);
return (
<GraphicWalker
ref={gwRef}
data={data}
fields={fields}
/>
);
}Export Current Chart
Use exportChart() to export the currently visible chart:
async function handleExport() {
if (!gwRef.current) return;
// Export as SVG (default)
const svgResult = await gwRef.current.exportChart('svg');
// Export as data URL (base64 PNG)
const pngResult = await gwRef.current.exportChart('data-url');
}Export Result Structure
The IChartExportResult contains:
| Property | Type | Description |
|---|---|---|
mode | 'svg' | 'data-url' | Export format |
title | string | Chart name |
nCols | number | Number of facet columns |
nRows | number | Number of facet rows |
charts | Array | Array of chart panels |
charts[].data | string | SVG markup or data URL string |
charts[].width | number | Chart width |
charts[].height | number | Chart height |
charts[].canvas() | Function | Returns the canvas/SVG DOM element |
container() | Function | Returns the chart container element |
chartType | string | The geometry type |
Export All Charts
Use exportChartList() to iterate through all charts in the workbook. It returns an AsyncGenerator:
async function exportAll() {
if (!gwRef.current) return;
for await (const item of gwRef.current.exportChartList('svg')) {
console.log(`Exporting chart ${item.index + 1} of ${item.total}`);
console.log('Title:', item.data.title);
console.log('SVG:', item.data.charts[0]?.data);
// item.hasNext tells you if more charts follow
}
}Download as SVG File
async function downloadSVG() {
if (!gwRef.current) return;
const result = await gwRef.current.exportChart('svg');
const svgData = result.charts[0]?.data;
if (!svgData) return;
const blob = new Blob([svgData], { type: 'image/svg+xml' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `${result.title || 'chart'}.svg`;
link.click();
URL.revokeObjectURL(url);
}Download as PNG File
async function downloadPNG() {
if (!gwRef.current) return;
const result = await gwRef.current.exportChart('data-url');
const dataUrl = result.charts[0]?.data;
if (!dataUrl) return;
const link = document.createElement('a');
link.href = dataUrl;
link.download = `${result.title || 'chart'}.png`;
link.click();
}Checking Render Status
Before exporting, you can check if the chart is ready:
async function safeExport() {
if (!gwRef.current) return;
// Wait for idle status
if (gwRef.current.renderStatus !== 'idle') {
await new Promise<void>(resolve => {
const dispose = gwRef.current!.onRenderStatusChange(status => {
if (status === 'idle') {
dispose();
resolve();
}
});
});
}
return gwRef.current.exportChart('svg');
}Navigating Charts Before Export
Switch to a specific chart tab before exporting:
// Export the third chart
gwRef.current.openChart(2); // 0-indexed
// Wait for render, then export
const result = await safeExport();