# KPI number

A single headline figure, formatted with the measure’s currency, prefix, suffix and locale.

- Component: `<KpiNumber />`, imported from `em-kit`
- Required props: `measure` and `results`
- HTML page, with live previews: https://emkit.embeddable.com/docs/components/kpi-number
- Playground (needs a browser): https://emkit.embeddable.com/docs/components/kpi-number/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. The card fills its parent, so the parent needs a height; the number scales to fit. 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, KpiNumber, measure, results } from 'em-kit';

const customers = measure({ name: 'customers', title: 'Customers' });

export const Example = () => (
  <ChartsProvider>
    <div style={{ width: 360, height: 160 }}>
      <KpiNumber
        title="Customers, last 30 days"
        measure={customers}
        results={results(totalRows)}
      />
    </div>
  </ChartsProvider>
);

const totalRows = [{ customers: 476 }];
```

## States

The card 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

### Compact

A large figure shortened to “1.2M”, still in the reader’s locale. `abbreviateLargeNumber` is a field option on the measure; the currency stays.

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

const revenue = measure({
  name: 'revenue',
  title: 'Revenue',
  inputs: { currency: 'USD', abbreviateLargeNumber: true },
});

export const Example = () => (
  <ChartsProvider>
    <div style={{ width: 360, height: 160 }}>
      <KpiNumber
        title="Revenue, year to date"
        measure={revenue}
        results={results(totalRows)}
      />
    </div>
  </ChartsProvider>
);

const totalRows = [{ revenue: 837479 }];
```

### Fixed font size

A set font size instead of auto-fitting the card, for a row of KPIs that should match. Without `fontSize` the number grows and shrinks to fill the card.

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

const customers = measure({ name: 'customers', title: 'Customers' });

export const Example = () => (
  <ChartsProvider>
    <div style={{ width: 360, height: 160 }}>
      <KpiNumber
        title="Customers, last 30 days"
        measure={customers}
        results={results(totalRows)}
        fontSize={40}
      />
    </div>
  </ChartsProvider>
);

const totalRows = [{ customers: 476 }];
```

## Props

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

| Prop | Type | Required | Default | Description |
|---|---|---|---|---|
| `measure` | `Measure` | yes |  | The number to show. Its `inputs` (`prefix`, `suffix`, `decimalPlaces`) 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. |
| `fontSize` | `number` | no |  | Font size of the headline number in pixels. By default it grows and shrinks to fit the card. |
| `displayNullAs` | `string` | no |  | Text shown where a value is null, for example `"–"`. |
| `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. |

## 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.
