Synertech Web/ui v0.57.5 Site Storybook

Documentation / Dashboards

ChartExportButton

Client@synertech/uiOpen in Storybook

A chart’s data table, as a file: CSV or JSON. DataGrid has always exported, and a chart holds an exact table of the same shape: a reader who can see a number should be able to take it away. It is a component rather than a prop on thirteen charts, because it belongs in a chart’s actions slot, or in the one of the ChartFilterBar above a whole dashboard.

In a chart’s actions it needs nothing: it writes the chart’s own table, rows in the order it sorts them, without the series the reader hid from the legend. Elsewhere (a ChartFilterBar) it takes data, series and xKey; a field passed wins over the chart’s. Measurements are written raw, never formatted: whatever opens the file will compute on them, and 1 234,5 € is a string. Only the category can be rewritten (formatX); a date goes out as ISO, as its day when it falls on midnight, with its offset otherwise. The columns keep the table’s order, even for a series named “2025”, and two series of one label stay two columns. With two formats it is a menu (Dropdown), with one a plain button; it is disabled when it has no row.

A "use client" component. The pure half is exported: toChartRows, toChartCsv (RFC 4180, delimiter of your choice), toChartJson and downloadChartData, which does nothing outside a browser.

Examples

Basic usage

Both formats in a menu; filename names the file without its extension, xLabel titles the first column.

Open in a tab
import { ChartExportButton } from "@synertech/ui";

const rows = [
  { month: "Jan", revenue: 38400, cost: 21800 },
  { month: "Fév", revenue: 41200, cost: 22600 },
  { month: "Mar", revenue: 39800, cost: 23100 },
  { month: "Avr", revenue: 46500, cost: 24000 },
];

const series = [
  { key: "revenue", label: "Revenu" },
  { key: "cost", label: "Coût" },
];

export function Basic() {
  return (
    <ChartExportButton data={rows} series={series} xKey="month" xLabel="Mois" filename="revenus" />
  );
}

In a chart’s caption

Where it really belongs: the actions slot of the chart whose table it exports. It is handed no data there — hide “Coût” in the legend and the file leaves it out. formats={["csv"]} turns it into a plain button.

Open in a tab
import { ChartExportButton, LineChart } from "@synertech/ui";

const rows = [
  { month: "Jan", revenue: 38400, cost: 21800 },
  { month: "Fév", revenue: 41200, cost: 22600 },
  { month: "Mar", revenue: 39800, cost: 23100 },
  { month: "Avr", revenue: 46500, cost: 24000 },
];

const series = [
  { key: "revenue", label: "Revenu" },
  { key: "cost", label: "Coût" },
];

export function InChartActions() {
  return (
    <LineChart
      title="Revenu et coût"
      data={rows}
      series={series}
      x={{ key: "month", label: "Mois" }}
      tableView="toggle"
      height={220}
      actions={<ChartExportButton formats={["csv"]} label="Télécharger" size="sm" />}
    />
  );
}

Taking over the export

onExport replaces the download: the application receives the format and the options, for instance to name the file after the current filters.

Open in a tab
import { ChartExportButton, downloadChartData } from "@synertech/ui";

const rows = [
  { month: "Jan", revenue: 38400, cost: 21800 },
  { month: "Fév", revenue: 41200, cost: 22600 },
  { month: "Mar", revenue: 39800, cost: 23100 },
  { month: "Avr", revenue: 46500, cost: 24000 },
];

const series = [
  { key: "revenue", label: "Revenu" },
  { key: "cost", label: "Coût" },
];

export function CustomExport() {
  return (
    <ChartExportButton
      data={rows}
      series={series}
      xKey="month"
      size="sm"
      onExport={(format, options) => {
        const stamp = new Date().toISOString().slice(0, 10);
        downloadChartData(format, options, `revenus-quebec-${stamp}`);
      }}
    />
  );
}

What goes into the file

toChartCsv and toChartJson produce the content without downloading anything; the ; delimiter suits spreadsheets set up for French.

Open in a tab
import { toChartCsv, toChartJson } from "@synertech/ui";

const rows = [
  { month: "Jan", revenue: 38400, cost: 21800 },
  { month: "Fév", revenue: 41200, cost: 22600 },
  { month: "Mar", revenue: 39800, cost: 23100 },
  { month: "Avr", revenue: 46500, cost: 24000 },
];

const series = [
  { key: "revenue", label: "Revenu" },
  { key: "cost", label: "Coût" },
];

export function FileContents() {
  const options = { data: rows, series, xKey: "month", xLabel: "Mois" };

  return (
    <div className="grid grid-cols-1 gap-3 md:grid-cols-2">
      <pre className="overflow-x-auto rounded-lg bg-gray-50 p-3 text-xs dark:bg-gray-900">
        {toChartCsv(options, ";")}
      </pre>
      <pre className="overflow-x-auto rounded-lg bg-gray-50 p-3 text-xs dark:bg-gray-900">
        {toChartJson({ ...options, series: [series[0]] })}
      </pre>
    </div>
  );
}

Using the rows in the application

toChartRows returns one object per row, keyed by series label: enough to compute a margin or feed another component.

Open in a tab
import { toChartRows } from "@synertech/ui";

const rows = [
  { month: "Jan", revenue: 38400, cost: 21800 },
  { month: "Fév", revenue: 41200, cost: 22600 },
  { month: "Mar", revenue: 39800, cost: 23100 },
  { month: "Avr", revenue: 46500, cost: 24000 },
];

const series = [
  { key: "revenue", label: "Revenu" },
  { key: "cost", label: "Coût" },
];

export function RowsInApp() {
  const exported = toChartRows({ data: rows, series, xKey: "month", xLabel: "Mois" });

  return (
    <ul className="space-y-1 text-sm text-gray-700 dark:text-gray-200">
      {exported.map((row) => (
        <li key={String(row.Mois)}>
          {row.Mois} : marge de {Number(row.Revenu) - Number(row.Coût)} €
        </li>
      ))}
    </ul>
  );
}

API reference

Components

ChartExportButton

ComponentClient

The chart's own table, as a file.

The small gap the chart kit carried from the start: DataGrid exports CSV and JSON, and a chart holds an exact table of the very same shape without ever offering it. A reader who can see a number should be able to take it.

A component rather than a prop on thirteen charts, because it belongs where the other controls already are — the actions slot of a ChartContainer, or of the ChartFilterBar above a whole dashboard.

In the actions of a chart it needs nothing: it writes the chart's own table, so a series hidden from the legend is left out of the file and the rows come in the order the table sorts them. Outside a chart — in a ChartFilterBar — it is handed data, series and xKey. A field passed wins over the chart's.

<LineChart
  data={rows}
  series={series}
  x={{ key: "month" }}
  actions={<ChartExportButton filename="revenue" />}
/>

The measurements are written raw, not formatted: whatever opens the file is going to compute on them.

import { ChartExportButton } from "@synertech/ui";

Props — ChartExportButtonProps

PropTypeDefaultDescription
className string —
data readonly ChartDatum[] —
disabled boolean —
filename string "chart"

Name of the downloaded file, without its extension.

formats readonly ChartExportFormat[] ["csv", "json"]

Which files to offer, in the order they are listed.

formatX (value: ChartXValue, index: number) => string —

Formats the category of each row.

Only the category: the measurements are written raw on purpose. A spreadsheet is going to compute on them, and 1 234,5 € is a string.

label string —

Replaces the default caption — the translated "Export".

neutralizeFormulas boolean —

Keeps a category or a series label from running as a formula.

A file is opened by someone other than whoever named the series: text starting with =, +, - or @ is written to the CSV with an apostrophe in front, so a spreadsheet shows it instead of computing it. The measurements are numbers and are never touched. On unless it is false; the JSON file is not concerned.

onExport (format: ChartExportFormat, options: ChartExportOptions) => void —

Runs instead of the download.

For an application that already owns its own export — a server-side report, a file named by the current filters — and only wants the control and the rows.

series readonly ChartSeries[] —
size ChartSize "md"
xKey string —

Field the rows are keyed on — the chart's x axis.

xLabel string —

Header of the first column. Defaults to xKey.

Functions

Hands the chart's rows to the browser as a file.

Does nothing outside a browser rather than throwing: a chart is rendered on the server too, and a download is the one thing that cannot happen there.

import { downloadChartData } from "@synertech/ui";
downloadChartData(format: ChartExportFormat, options: ChartExportOptions, filename?: string): void

toChartCsv

Function

The chart's data table as a CSV document.

RFC 4180 rather than the usual "does it contain a comma": a label holding a newline or a quote breaks a file just as thoroughly, and silently.

import { toChartCsv } from "@synertech/ui";
toChartCsv(options: ChartExportOptions, delimiter?: string): string

toChartJson

Function

The chart's data table as a JSON document.

Written key by key rather than through JSON.stringify of the rows, which would put a series named "2025" before the category again.

import { toChartJson } from "@synertech/ui";
toChartJson(options: ChartExportOptions): string

toChartRows

Function

The chart's rows, in the shape a file wants: one object per row.

An object lists a key that reads as a number (a series named "2025") before the others, whatever order it was written in: a file of these rows is written by toChartCsv and toChartJson, which keep the order of the table.

import { toChartRows } from "@synertech/ui";
toChartRows(options: ChartExportOptions): ChartExportRow[]

Types

What a chart hands to a file.

The same rows its data table draws, which is the point: DataGrid has exported CSV and JSON since it shipped, and a chart holds an exact table of its own already. Every value a reader can see is a value they can take away.

FieldTypeDefaultDescription
data• readonly ChartDatum[] —
series• readonly ChartSeries[] —
xKey• string —

Field the rows are keyed on — the chart's x axis.

formatX (value: ChartXValue, index: number) => string —

Formats the category of each row.

Only the category: the measurements are written raw on purpose. A spreadsheet is going to compute on them, and 1 234,5 € is a string.

neutralizeFormulas boolean —

Keeps a category or a series label from running as a formula.

A file is opened by someone other than whoever named the series: text starting with =, +, - or @ is written to the CSV with an apostrophe in front, so a spreadsheet shows it instead of computing it. The measurements are numbers and are never touched. On unless it is false; the JSON file is not concerned.

xLabel string —

Header of the first column. Defaults to xKey.

The table a chart holds: what its data table lists, and what a file of it writes.

Every chart of the kit hands one to its ChartContainer, which is how a ChartExportButton placed in its actions with no data of its own writes the rows the table lists — in the order the table sorts them, with the series the legend shows.

FieldTypeDefaultDescription
data• readonly ChartDatum[] —
series• readonly ChartSeries[] —
xKey• string —

Field the rows are keyed on — the chart's x axis.

formatX (value: ChartXValue, index: number) => string —

Formats the category of each row.

Only the category: the measurements are written raw on purpose. A spreadsheet is going to compute on them, and 1 234,5 € is a string.

xLabel string —

Header of the first column. Defaults to xKey.

type ChartExportFormat = "csv" | "json"

One exported row: the category, then one column per visible series.

type ChartExportRow = Record<string, string | number | null>