Chart Export
boldkit/chart-exportFreeUtility
BoldKit chart export helpers — framework-agnostic, SSR-safe CSV / PNG / SVG download and fullscreen toggle. Works with any chart engine that renders an <svg> (Recharts) or <canvas> (ECharts).
Live preview
live · rendered by the registry
Rendered by boldkit on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://boldkit.dev/r/chart-export.jsonSource
1/**2 * BoldKit Chart Export — framework-agnostic chart toolbar helpers.3 *4 * Operates on a chart's container element, so it works regardless of the5 * rendering engine: Recharts (SVG) and ECharts (canvas) are both handled.6 * Consumed by the React <ChartToolbar> and the Vue <ChartToolbar>.7 *8 * All functions are SSR-safe: they no-op on the server.9 */1011const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined'1213function triggerDownload(href: string, filename: string) {14 const a = document.createElement('a')15 a.href = href16 a.download = filename17 document.body.appendChild(a)18 a.click()19 a.remove()20}2122// ──────────────────────────────────────────────────────────────────23// CSV — from the same data array you feed the chart24// ──────────────────────────────────────────────────────────────────2526/** Escape a single CSV cell per RFC 4180 (quote if it contains ,"\n). */27function csvCell(value: unknown): string {28 const s = value == null ? '' : String(value)29 return /[",\n]/.test(s) ? `"${s.replace(/"/g, '""')}"` : s30}3132/**33 * Serialize an array of row objects to a CSV string. Columns are the union34 * of keys across all rows, in first-seen order. Pure — no DOM access.35 */36export function toCSV(rows: Array<Record<string, unknown>>): string {37 if (!rows.length) return ''3839 const columns: string[] = []40 for (const row of rows) {41 for (const key of Object.keys(row)) {42 if (!columns.includes(key)) columns.push(key)43 }44 }4546 const lines = [47 columns.map(csvCell).join(','),48 ...rows.map((row) => columns.map((col) => csvCell(row[col])).join(',')),49 ]50 return lines.join('\n')51}5253/**54 * Serialize an array of row objects to CSV and download it.55 * Columns are the union of keys across all rows, in first-seen order.56 */57export function downloadCSV(rows: Array<Record<string, unknown>>, filename = 'chart.csv'): void {58 if (!isBrowser || !rows.length) return5960 const blob = new Blob([toCSV(rows)], { type: 'text/csv;charset=utf-8;' })61 const url = URL.createObjectURL(blob)62 triggerDownload(url, filename)63 URL.revokeObjectURL(url)64}6566// … truncated
Files it writes
More from boldkit
All 94 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Math Curvesmath-curves | boldkit | Utilities | Free | no deps | |
| Motion Coremotion-core | boldkit | Utilities | Free | no deps | |
| Utilsutils | boldkit | Utilities | Free | 2 deps |
