Diagramm-Export
Graphic Walker bietet programmatischen Diagramm-Export ueber die IGWHandler-Ref. Exportieren Sie einzelne Diagramme oder iterieren Sie durch alle Diagramme in einer Arbeitsmappe.
Einrichtung
Greifen Sie ueber eine React-Ref auf die Export-API zu:
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}
/>
);
}Aktuelles Diagramm exportieren
Verwenden Sie exportChart(), um das aktuell sichtbare Diagramm zu exportieren:
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');
}Struktur des Export-Ergebnisses
Das IChartExportResult enthaelt:
| Eigenschaft | Typ | Beschreibung |
|---|---|---|
mode | 'svg' | 'data-url' | Exportformat |
title | string | Diagrammname |
nCols | number | Anzahl der Facettenspalten |
nRows | number | Anzahl der Facettenzeilen |
charts | Array | Array von Diagramm-Panels |
charts[].data | string | SVG-Markup oder Data-URL-String |
charts[].width | number | Diagrammbreite |
charts[].height | number | Diagrammhoehe |
charts[].canvas() | Function | Gibt das Canvas-/SVG-DOM-Element zurueck |
container() | Function | Gibt das Diagramm-Container-Element zurueck |
chartType | string | Der Geometrietyp |
Alle Diagramme exportieren
Verwenden Sie exportChartList(), um durch alle Diagramme in der Arbeitsmappe zu iterieren. Es gibt einen AsyncGenerator zurueck:
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
}
}Als SVG-Datei herunterladen
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);
}Als PNG-Datei herunterladen
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();
}Render-Status pruefen
Vor dem Exportieren koennen Sie pruefen, ob das Diagramm bereit ist:
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');
}Diagramme vor dem Export navigieren
Wechseln Sie zu einem bestimmten Diagramm-Tab vor dem Exportieren:
// Export the third chart
gwRef.current.openChart(2); // 0-indexed
// Wait for render, then export
const result = await safeExport();