チャートエクスポート
Graphic Walkerは、IGWHandler refを通じてプログラムによるチャートエクスポートを提供します。個別のチャートをエクスポートしたり、ワークブック内のすべてのチャートをイテレートすることができます。
セットアップ
React refを通じてエクスポートAPIにアクセスします:
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}
/>
);
}現在のチャートをエクスポート
exportChart()を使用して、現在表示されているチャートをエクスポートします:
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');
}エクスポート結果の構造
IChartExportResultには以下が含まれます:
| プロパティ | 型 | 説明 |
|---|---|---|
mode | 'svg' | 'data-url' | エクスポート形式 |
title | string | チャート名 |
nCols | number | ファセット列数 |
nRows | number | ファセット行数 |
charts | Array | チャートパネルの配列 |
charts[].data | string | SVGマークアップまたはデータURL文字列 |
charts[].width | number | チャートの幅 |
charts[].height | number | チャートの高さ |
charts[].canvas() | Function | canvas/SVG DOM要素を返す |
container() | Function | チャートコンテナ要素を返す |
chartType | string | ジオメトリタイプ |
すべてのチャートをエクスポート
exportChartList()を使用して、ワークブック内のすべてのチャートをイテレートします。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
}
}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);
}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();
}レンダリング状態の確認
エクスポート前にチャートの準備ができているか確認できます:
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');
}エクスポート前のチャートナビゲーション
エクスポート前に特定のチャートタブに切り替えます:
// Export the third chart
gwRef.current.openChart(2); // 0-indexed
// Wait for render, then export
const result = await safeExport();