# Pivot charts

> Add an optional chart of the pivot with the pivot chart plugin, or place a pivot chart anywhere with createPivotChart, through the @birchui/pivot-studio/charts entry point.

A pivot chart shows the same numbers as the table and follows every change to the report: add a field, filter a member or collapse a region and the chart updates with the table. Pivot charts are drawn by [Birch UI Charts](https://docs.birchui.com/charts/overview.md) and live in a separate entry point, so reports without charts never load the charting code.

> **License**
>
> Pivot charts are drawn by Birch UI Charts, which checks its own `charts` license. A Pivot Studio license does not cover charts: you need a Birch UI Charts license or the Birch UI Complete bundle. Without one, the charts work fully and show the Charts watermark on non-local hosts.

## Install

```bash
npm install @birchui/charts
```

`@birchui/charts` is an optional peer dependency of Pivot Studio. The charts entry point adds about 1 KB on top of Birch UI Charts.

## The chart plugin

`pivotChartPlugin()` adds a **Chart** toggle button to the toolbar and a chart panel above or below the table:

```ts title="sales-report-with-chart.ts"
import { createPivotStudio } from '@birchui/pivot-studio';
import { pivotChartPlugin } from '@birchui/pivot-studio/charts';

createPivotStudio(document.querySelector<HTMLElement>('#sales-report')!, {
  data: sales,
  report,
  licenseKey: COMPLETE_LICENSE_KEY,
  plugins: [pivotChartPlugin({ type: 'column', visible: true, position: 'top' })],
});
```

| Option | Default | Meaning |
| --- | --- | --- |
| `type` | `'column'` | `'column'`, `'bar'`, `'line'`, `'area'`, `'pie'` or `'donut'` |
| `stacked` | `false` | Stack column, bar and area series |
| `visible` | `false` | Show the chart when the widget opens; users toggle it with the Chart button |
| `position` | `'top'` | Panel above (`'top'`) or below (`'bottom'`) the table |
| `height` | `280` | Chart height in pixels or any CSS length |
| `measure` | the first measure | The measure to chart, by ID or field name |
| `maxCategories` | `50` | Most categories drawn |
| `maxSeries` | `12` | Most series drawn |
| `title` | generated | Defaults to "Revenue by Region and Store", built from the measure and row fields |
| `theme`, `locale` | the widget's | Passed to the chart |
| `licenseKey` | the widget's `licenseKey` | The key used for the Charts license check |
| `chart` | none | Any other Birch UI Charts option except `data` and `series`, such as `axes` or `tooltip` |

Plugins are read when the widget is created; pass them in the initial options.

## What the chart shows

- **Categories** are the visible row members at the lowest visible level: with Region › Store expanded, each store is a category, labelled "North › Leeds Trinity"; collapse North and it becomes one category. With no row fields, the grand total is the only category.
- **Series** are the column members for the charted measure: with quarters in Columns, each quarter is a series. With no column fields, each measure is a series.
- **Pie and donut** charts draw the first series only.
- The legend is shown when there is more than one series.

Because categories follow expansion, collapsing and expanding rows is the way to switch between an overview chart and a detailed one.

## A chart anywhere: createPivotChart

To place the chart outside the widget, for example in a dashboard tile next to it, call `createPivotChart(container, studio, options)`. It takes the same options as the plugin, except `position` and `visible`:

```ts title="dashboard-tile.ts"
import { createPivotStudio } from '@birchui/pivot-studio';
import { createPivotChart } from '@birchui/pivot-studio/charts';

const studio = createPivotStudio(document.querySelector<HTMLElement>('#sales-report')!, { data: sales, report });

const tile = createPivotChart(document.querySelector<HTMLElement>('#revenue-tile')!, studio, {
  type: 'line',
  measure: 'sum(revenue)',
  height: 320,
  licenseKey: CHARTS_LICENSE_KEY,
  chart: { tooltip: { shared: true } },
});

tile.update({ type: 'area', stacked: true });
tile.chart.download('png', 'revenue-by-store');
tile.destroy();
```

The handle has:

| Member | Description |
| --- | --- |
| `chart` | The underlying `ChartApi`, with every [Birch UI Charts method](https://docs.birchui.com/charts/api.md) such as `download()` and `setZoom()` |
| `update(options)` | Change the pivot chart options |
| `refresh()` | Re-read the pivot; this happens automatically on every `pivotUpdated` |
| `destroy()` | Stop following the pivot and remove the chart |

## Chart data without a chart

`pivotChartOptions(view, options)` returns complete Birch UI Charts options for a `PivotView`, and `pivotChartData(view, options)` (also exported from the main entry point) returns just `{ categories, series }`. Use them to feed another chart library or to build a chart on the server:

```ts
import { pivotChartData } from '@birchui/pivot-studio';

const view = studio.getView();
if (view) {
  const { categories, series } = pivotChartData(view, { measure: 'sum(units)', maxCategories: 20 });
  renderSparkTable(categories, series);
}
```

## License keys for charts

The Charts license check uses the plugin's `licenseKey`, or the widget's `licenseKey` when the plugin has none, or the key set with `setLicenseKey()`. Choose one of:

- A **Birch UI Complete** key, which covers both products: set it once as the widget's `licenseKey` or with `setLicenseKey()`.
- A key that lists both `pivot-studio` and `charts`, used the same way.
- Separate keys: the Pivot Studio key as the widget's `licenseKey` and the Charts key as the plugin's `licenseKey`.
