Export de graphiques
Graphic Walker fournit un export programmatique de graphiques via la ref IGWHandler. Exportez des graphiques individuels ou parcourez tous les graphiques d'un classeur.
Configuration
Accédez à l'API d'export via une ref React :
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}
/>
);
}Exporter le graphique actuel
Utilisez exportChart() pour exporter le graphique actuellement visible :
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');
}Structure du résultat d'export
Le IChartExportResult contient :
| Propriété | Type | Description |
|---|---|---|
mode | 'svg' | 'data-url' | Format d'export |
title | string | Nom du graphique |
nCols | number | Nombre de colonnes de facettes |
nRows | number | Nombre de lignes de facettes |
charts | Array | Tableau de panneaux de graphiques |
charts[].data | string | Balisage SVG ou chaîne d'URL de données |
charts[].width | number | Largeur du graphique |
charts[].height | number | Hauteur du graphique |
charts[].canvas() | Function | Retourne l'élément DOM canvas/SVG |
container() | Function | Retourne l'élément conteneur du graphique |
chartType | string | Le type de géométrie |
Exporter tous les graphiques
Utilisez exportChartList() pour parcourir tous les graphiques du classeur. Elle retourne un 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
}
}Télécharger en fichier SVG
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);
}Télécharger en fichier PNG
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();
}Vérification du statut de rendu
Avant d'exporter, vous pouvez vérifier si le graphique est prêt :
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');
}Navigation entre les graphiques avant l'export
Basculez vers un onglet de graphique spécifique avant d'exporter :
// Export the third chart
gwRef.current.openChart(2); // 0-indexed
// Wait for render, then export
const result = await safeExport();