# Comparison period select

Previous period, previous year and the like: what the current figures are compared with.

- Component: `<ComparisonPeriodSelect />`, imported from `em-kit`
- Required props: `onChange`
- HTML page, with live previews: https://emkit.embeddable.com/docs/components/comparison-period-select
- Playground (needs a browser): https://emkit.embeddable.com/docs/components/comparison-period-select/playground

## 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 control is as wide as its parent and as tall as its title and field, so it needs no 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 { useState } from 'react';
import { ChartsProvider, ComparisonPeriodSelect } from 'em-kit';

export const Example = () => {
  const [period, setPeriod] = useState<string | undefined>('Previous period');
  return (
    <ChartsProvider>
      <div style={{ width: 320 }}>
        <ComparisonPeriodSelect
          title="Compare to"
          comparisonPeriod={period}
          onChange={setPeriod}
        />
      </div>
    </ChartsProvider>
  );
};
```

- The value is the option’s name, such as “Previous period”.

## Examples

### With dates

Each option shows the dates it covers, worked out from the range being compared. `primaryDateRange` is the range your date picker holds: pass its state here.

```tsx
import { useState } from 'react';
import {
  ChartsProvider,
  ComparisonPeriodSelect,
  dateRangePreset,
} from 'em-kit';

export const Example = () => {
  const [period, setPeriod] = useState<string | undefined>('Previous period');
  return (
    <ChartsProvider>
      <div style={{ width: 320 }}>
        <ComparisonPeriodSelect
          title="Compare to"
          comparisonPeriod={period}
          onChange={setPeriod}
          primaryDateRange={dateRangePreset('Last 30 days')}
        />
      </div>
    </ChartsProvider>
  );
};
```

## Props

Required props come first. A blank default means the prop is unset unless you pass it.

| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
| `onChange` | `(newComparisonPeriod: string) => void` | yes |  | Called with the name of the comparison the reader picks, or with `undefined` when the reader clears it. |
| `placeholder` | `string` | no | `"Select a date-comparison"` | Text shown while nothing is chosen. Translates like `title`. |
| `primaryDateRange` | `TimeRange` | no |  | The range being compared: `{ from: Date, to: Date }`, with an optional `relativeTimeString` naming a preset. With it, each option shows the dates it covers. |
| `comparisonPeriod` | `string` | no |  | The chosen comparison, by name: 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"`. The list, and how each works out its dates, is the provider’s `theme.defaults.comparisonPeriodsOptions`. |
| `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 title over the field. 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. |

```ts
type TimeRange = { from: Date | undefined; to: Date | undefined; relativeTimeString?: string; } | undefined
```

Each is exported from `em-kit` as a type, for example `import type { TimeRange } 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`. [Field options](/docs/describing-your-data.md#field-options) describes them.
