Getting started

Quick start

From an empty React app to a chart on screen, in three steps.

The components take rows you already have, so there is nothing to fetch or configure.

1. Install

bash
npm install em-kit

The only peer dependencies are react and react-dom, version 19.1.1 or later. Chart.js and the other runtime dependencies install with the package, and there is no stylesheet to import.

2. Add the provider

Wrap your app in ChartsProvider once. Charts read their theme, language and time zone from it: see Theming and Localisation. RevenueChart is the file you add in step 3.

App.tsx
import { ChartsProvider } from 'em-kit';
import { RevenueChart } from './RevenueChart';

export default function App() {
  return (
    <ChartsProvider>
      <RevenueChart />
    </ChartsProvider>
  );
}

The provider loads the Inter font from Google Fonts; to serve it yourself or turn it off, see fonts.

In the Next.js App Router

Next.js renders a file on the server unless it starts with 'use client', and the components use React context. Put the provider in a client file that pins the locale, so the server and the browser format numbers alike, and render it from the root layout.

app/ClientProvider.tsx
'use client';

import type { ReactNode } from 'react';
import { ChartsProvider } from 'em-kit';

// Render this from app/layout.tsx. Files that render charts start with
// 'use client' too. The fixed locale makes the server format numbers as the
// browser will; use the reader's locale from the request if you have it.
export function ClientProvider({ children }: { children: ReactNode }) {
  return <ChartsProvider theme={{ formatter: { locale: 'en-US' } }}>{children}</ChartsProvider>;
}
app/layout.tsx
import type { ReactNode } from 'react';
import { ClientProvider } from './ClientProvider';

export default function RootLayout({ children }: { children: ReactNode }) {
  return (
    <html lang="en">
      <body>
        <ClientProvider>{children}</ClientProvider>
      </body>
    </html>
  );
}

Start RevenueChart.tsx from step 3 with 'use client'; as well, and render it from a page.

app/page.tsx
import { RevenueChart } from '../RevenueChart';

export default function Page() {
  return <RevenueChart />;
}

3. Render a chart

Describe each field once, then pass your rows to results. The chart fills its parent, so give the parent a height, and a width it can fill.

RevenueChart.tsx
import { BarChart, dimension, measure, results } from 'em-kit';

const region = dimension({ name: 'region', title: 'Region' });
const revenue = measure({ name: 'revenue', title: 'Revenue' });

// One row per bar. Each key is the `name` of a field above.
const rows = [
  { region: 'North America', revenue: 48200 },
  { region: 'Europe', revenue: 39500 },
  { region: 'Asia Pacific', revenue: 27800 },
  { region: 'Latin America', revenue: 12100 },
];

export function RevenueChart() {
  return (
    <div style={{ height: 360 }}>
      <BarChart
        title="Revenue by region"
        dimension={region}
        measures={[revenue]}
        results={results(rows)}
      />
    </div>
  );
}
Result

Revenue by region

The numbers are formatted for your browser’s locale, and the menu in the card’s corner exports CSV, Excel and PNG.

Next steps

  • Describing your data: dimensions, measures and field options, such as currency: 'USD' for $48,200
  • Time series: dates on the axis, the timestamp format and missing periods
  • Components: every chart, with examples and props
  • Troubleshooting: what to check if the chart does not appear or looks wrong