# Line chart with comparison and KPI tabs

KPI tabs showing each measure’s change against the previous period, over a line chart that overlays both.

- Component: `<LineChartComparisonWithKpiTabs />`, imported from `em-kit`
- Required props: `xAxis`, `measures`, `results`, `resultsComparison`, `comparisonDateRange`, `primaryDateRange` and `resultsKpis`
- HTML page, with live previews: https://emkit.embeddable.com/docs/components/line-chart-comparison-with-kpi-tabs
- Playground (needs a browser): https://emkit.embeddable.com/docs/components/line-chart-comparison-with-kpi-tabs/playground

`LineChartComparisonWithKpiTabs` takes `xAxis` + `measures`. Its siblings name their fields differently: `LineChartGrouped` and `AreaChart` take `xAxis` + `groupBy` + `measure`.

## Install

```bash
npm install em-kit
pnpm add em-kit   # or, with pnpm
yarn add em-kit   # or, with yarn
bun add em-kit   # or, with bun
```

`react` and `react-dom` 19.1.1 or later are the only peer dependencies; Chart.js installs with the package. Wrap your app, or the part with charts, in `<ChartsProvider>` once: without it every chart throws `EmKit components must be rendered inside <ChartsProvider>.` Every example on this page includes `<ChartsProvider>` so that it runs on its own: if your app already has one, delete it from the example. A chart fills its parent, so the parent needs a height. In the Next.js App Router, every file that renders `ChartsProvider` or a chart starts with `'use client'`, and the provider pins the locale, `theme={{ formatter: { locale: 'en-US' } }}`, so the server and the browser format numbers alike. [The quick start](/docs/quick-start.md#in-the-nextjs-app-router) has the Next.js files.

## Usage

```tsx
import {
  ChartsProvider,
  LineChartComparisonWithKpiTabs,
  dimension,
  measure,
  results,
} from 'em-kit';

const startDate = dimension({
  name: 'startDate',
  title: 'Start date',
  type: 'time',
  granularity: 'month',
});
const signups = measure({
  name: 'signups',
  title: 'Signups',
  inputs: {
    abbreviateLargeNumber: true,
    previousLineDashed: true,
    previousLineColor: '#a1a1aa',
  },
});
const customers = measure({
  name: 'customers',
  title: 'Customers',
  inputs: { previousLineDashed: true, previousLineColor: '#a1a1aa' },
});

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 460 }}>
      <LineChartComparisonWithKpiTabs
        title="Signups and customers vs previous period"
        xAxis={startDate}
        measures={[signups, customers]}
        results={results(rows)}
        resultsComparison={results(comparisonRows)}
        resultsKpis={results(kpiRows)}
        resultsKpisComparison={results(kpiComparisonRows)}
        comparisonPeriod="Previous period"
        primaryDateRange={currentRange}
        comparisonDateRange={previousRange}
        showValueLabels={false}
      />
    </div>
  </ChartsProvider>
);

const currentRange = {
  from: new Date('2026-03-01'),
  to: new Date('2026-06-30'),
};
const previousRange = {
  from: new Date('2025-11-01'),
  to: new Date('2026-02-28'),
};

const rows = [
  { startDate: '2026-03-01T00:00:00.000', customers: 469, signups: 2266 },
  { startDate: '2026-04-01T00:00:00.000', customers: 410, signups: 2474 },
  { startDate: '2026-05-01T00:00:00.000', customers: 428, signups: 2945 },
  { startDate: '2026-06-01T00:00:00.000', customers: 476, signups: 2634 },
];

const comparisonRows = [
  { startDate: '2025-11-01T00:00:00.000', customers: 478, signups: 1543 },
  { startDate: '2025-12-01T00:00:00.000', customers: 498, signups: 2282 },
  { startDate: '2026-01-01T00:00:00.000', customers: 406, signups: 2385 },
  { startDate: '2026-02-01T00:00:00.000', customers: 385, signups: 2191 },
];

const kpiRows = [{ signups: 10319, customers: 1783 }];

const kpiComparisonRows = [{ signups: 8401, customers: 1767 }];
```

## States

The chart draws its own states from `results`: a skeleton while `isLoading` is true and it has no rows, a spinner in the header while it reloads rows it already has, an empty state when `data` is `[]`, and the message when `error` is set. Pass your query’s state straight through.

## Examples

### One date axis

Both lines against the current period’s dates, without the second x-axis. `showComparisonAxis={false}` drops the axis that carries the comparison period’s own dates.

```tsx
import {
  ChartsProvider,
  LineChartComparisonWithKpiTabs,
  dimension,
  measure,
  results,
} from 'em-kit';

const startDate = dimension({
  name: 'startDate',
  title: 'Start date',
  type: 'time',
  granularity: 'month',
});
const signups = measure({
  name: 'signups',
  title: 'Signups',
  inputs: {
    abbreviateLargeNumber: true,
    previousLineDashed: true,
    previousLineColor: '#a1a1aa',
  },
});
const customers = measure({
  name: 'customers',
  title: 'Customers',
  inputs: { previousLineDashed: true, previousLineColor: '#a1a1aa' },
});

export const Example = () => (
  <ChartsProvider>
    <div style={{ height: 460 }}>
      <LineChartComparisonWithKpiTabs
        title="Signups and customers vs previous period"
        xAxis={startDate}
        measures={[signups, customers]}
        results={results(rows)}
        resultsComparison={results(comparisonRows)}
        resultsKpis={results(kpiRows)}
        resultsKpisComparison={results(kpiComparisonRows)}
        comparisonPeriod="Previous period"
        primaryDateRange={currentRange}
        comparisonDateRange={previousRange}
        showValueLabels={false}
        showComparisonAxis={false}
      />
    </div>
  </ChartsProvider>
);

const currentRange = {
  from: new Date('2026-03-01'),
  to: new Date('2026-06-30'),
};
const previousRange = {
  from: new Date('2025-11-01'),
  to: new Date('2026-02-28'),
};

const rows = [
  { startDate: '2026-03-01T00:00:00.000', customers: 469, signups: 2266 },
  { startDate: '2026-04-01T00:00:00.000', customers: 410, signups: 2474 },
  { startDate: '2026-05-01T00:00:00.000', customers: 428, signups: 2945 },
  { startDate: '2026-06-01T00:00:00.000', customers: 476, signups: 2634 },
];

const comparisonRows = [
  { startDate: '2025-11-01T00:00:00.000', customers: 478, signups: 1543 },
  { startDate: '2025-12-01T00:00:00.000', customers: 498, signups: 2282 },
  { startDate: '2026-01-01T00:00:00.000', customers: 406, signups: 2385 },
  { startDate: '2026-02-01T00:00:00.000', customers: 385, signups: 2191 },
];

const kpiRows = [{ signups: 10319, customers: 1783 }];

const kpiComparisonRows = [{ signups: 8401, customers: 1767 }];
```

## Props

Required props come first. A blank default means the prop is unset unless you pass it. Not listed: `granularity`, which the type accepts and the component does not read.

| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
| `xAxis` | `Dimension` | yes |  | The dimension along the x-axis. A time dimension gets gap filling and date labels. |
| `measures` | `Measure[]` | yes |  | The numeric fields to plot, one series each. Their `inputs` set the formatting. |
| `results` | `DataResponse` | yes |  | Your query’s state: `{ isLoading, data, error }`, where `data` is your rows. `results(rows)` builds it from an array. |
| `resultsComparison` | `DataResponse` | yes |  | Rows for the comparison period, with the same keys as `results`. |
| `comparisonDateRange` | `TimeRange` | yes |  | The dates `resultsComparison` covers: `{ from: Date, to: Date }`, with an optional `relativeTimeString` naming a preset. The comparison’s axis runs from `from` to `to`. |
| `primaryDateRange` | `TimeRange` | yes |  | The dates `results` covers: `{ from: Date, to: Date }`, with an optional `relativeTimeString` naming a preset. The x-axis runs from `from` to `to`. |
| `resultsKpis` | `DataResponse` | yes |  | One row holding each measure’s total, shown as tabs above the chart. Clicking a tab plots that measure. |
| `reverseXAxis` | `boolean` | no | `false` | Reverse the order of the x-axis. |
| `showLegend` | `boolean` | no | `true` | Show the legend. A chart with one series shows none; entries wrap onto more lines when they do not fit. |
| `showLogarithmicScale` | `boolean` | no | `false` | Use a logarithmic value axis, for values that span several orders of magnitude. |
| `showTooltips` | `boolean` | no | `true` | Show a formatted tooltip on hover. |
| `showValueLabels` | `boolean` | no | `true` | Print each value on the chart. |
| `xAxisLabel` | `string` | no |  | A title under the x-axis. |
| `yAxisLabel` | `string` | no |  | A title beside the y-axis. |
| `yAxisRangeMax` | `number` | no |  | Fix the highest value on the y-axis. When unset, it fits the data. |
| `yAxisRangeMin` | `number` | no |  | Fix the lowest value on the y-axis. When unset, a bar, line or area chart’s axis includes zero, and a scatter or bubble chart’s fits the data. |
| `comparisonPeriod` | `string` | no |  | Which period `resultsComparison` holds. One of `"Previous period"`, `"Previous week"`, `"Previous month"`, `"Previous quarter"`, `"Previous year"`, `"Same period last week"`, `"Same period last month"`, `"Same period last quarter"`, `"Same period last year"`. Without it the comparison is not drawn. With `setComparisonDateRange` the comparison dates are worked out from it, and any other string gives no range. |
| `showComparisonAxis` | `boolean` | no | `true` | Show a second x-axis with the comparison period’s dates. Set it to `false` to draw both lines against the current dates. |
| `setGranularity` | `(granularity: Granularity) => void` | no |  | Called with the granularity the reader picks in the card. The picker only shows when the time field sets `showGranularityDropdown: true` in its `inputs`. Store the value, set it as the field’s `granularity` and pass rows at that granularity. |
| `setComparisonDateRange` | `(dateRange: TimeRange) => void` | no |  | If set, the chart works out the comparison dates from `primaryDateRange` and `comparisonPeriod` and calls this with them, so you can query them. When it cannot work them out, it calls this with no range. |
| `onLineClicked` | `(arg: LineChartOptionsClickArg) => void` | no |  | Called with the clicked point’s dimension value and date range. |
| `componentName` | `string` | no |  | A name for this component, included in the interaction events it dispatches on the DOM, for your analytics. |
| `trackingId` | `string` | no |  | An id included in the same events, to tell two instances apart. |
| `title` | `string` | no |  | The card’s title. A `"key\|fallback"` string is looked up in your translations. |
| `description` | `string` | no |  | Smaller text under the title. Translates like `title`. |
| `tooltip` | `string` | no |  | Text for an info icon beside the title. |
| `hideMenu` | `boolean` | no | `false` | Hide the card’s header: the title, description and menu. |
| `menuOptions` | `("csv" \| "xlsx" \| "png" \| "maximize")[]` | no | `["csv", "xlsx", "png", "maximize"]` | Which entries the card menu offers. Pass `[]` for none. |
| `resultsKpisComparison` | `DataResponse` | no |  | The same one row for the comparison period, so each tab can show its change. |

```ts
type LineChartOptionsClickArg = { dimensionValue: string | number | undefined; dimensionTimeRange: TimeRange; measureValue?: number | undefined; measureValues?: Record<string, unknown> | undefined; }
type Granularity = "second" | "minute" | "hour" | "day" | "week" | "month" | "quarter" | "year"
type TimeRange = { from: Date | undefined; to: Date | undefined; relativeTimeString?: string; } | undefined
```

Each is exported from `em-kit` as a type, for example `import type { LineChartOptionsClickArg } from 'em-kit'`.

A click payload’s `TimeRange` is `undefined` unless the clicked field is a time dimension, and its `relativeTimeString` is always `undefined`, so read it as `range?.from`. When you pass one, `from` and `to` are enough.

## Field options

Options go in a field’s `inputs`, for example `measure({ name: 'revenue', inputs: { prefix: '$' } })`. Every field takes `prefix`, `suffix`, `decimalPlaces`, `currency`, `abbreviateLargeNumber`, `displayName` and `maxCharacters`. A time dimension in `xAxis` also takes `granularity`, `showGranularityDropdown`, `dateBounds` and `ignoreEmptyDate`. Its `granularity` is what labels the axis as dates and fills periods with no row; `dimension()` takes it in `inputs` or at the top level, `dimension({ granularity: 'month' })`. [Field options](/docs/describing-your-data.md#field-options) describes them. `LineChartComparisonWithKpiTabs` also reads:

| Option | Type | Set on | Default | Description |
|---|---|---|---|---|
| `fillUnderLine` | `boolean` | `measures` |  | Shade the area under the line. |
| `lineColor` | `string` | `measures` |  | A colour for this measure’s line, in place of the theme’s. |
| `previousLineColor` | `string` | `measures` |  | A colour for the comparison period’s line. |
| `lineDashed` | `boolean` | `measures` | `false` | Draw the current period’s line dashed. |
| `previousLineDashed` | `boolean` | `measures` | `false` | Draw the comparison period’s line dashed. When off, it is solid like the current line. |
| `connectGaps` | `boolean` | `measures` | `false` | Draw a missing value as zero, so the line carries on. When off, the line breaks at the gap. |
| `invertChangeColors` | `boolean` | `measures` | `false` | Colour an increase as bad and a decrease as good, for costs or churn. |
| `reverseTrendDirection` | `boolean` | `measures` |  | Point the trend arrow the other way. When unset, the arrow follows `invertChangeColors`. |
| `displayChangeAsPercentage` | `boolean` | `measures` | `false` | Show the change as a percentage instead of a difference. |
| `percentageDecimalPlaces` | `number` | `measures` | `1` | Decimal places on the percentage change. Only read with `displayChangeAsPercentage`. |
