Skip to content
GRAPHIC WALKER
Guides
Export de graphiques

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éTypeDescription
mode'svg' | 'data-url'Format d'export
titlestringNom du graphique
nColsnumberNombre de colonnes de facettes
nRowsnumberNombre de lignes de facettes
chartsArrayTableau de panneaux de graphiques
charts[].datastringBalisage SVG ou chaîne d'URL de données
charts[].widthnumberLargeur du graphique
charts[].heightnumberHauteur du graphique
charts[].canvas()FunctionRetourne l'élément DOM canvas/SVG
container()FunctionRetourne l'élément conteneur du graphique
chartTypestringLe 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();