Command Palette

Search for a command to run...

Chart

Composable charts using Recharts.

Component

The chart component uses Recharts under the hood.

It is designed with composition in mind. You can use the components provided by the chart component or use Recharts components directly to build your charts.

import { Chart, Recharts } from "@smartacteam/ambient-web/chart";
 
export function MyChart() {
  return (
    <Recharts.Root>
      <Recharts.BarChart data={data}>
        <Recharts.Bar dataKey="value" />
        <Recharts.Tooltip content={<Chart.TooltipContent />} />
      </Recharts.BarChart>
    </Recharts.Root>
  );
}

Your First Chart

Let's build your first chart. We'll build a bar chart, add a grid, axis, tooltip and legend.

Define your chart config

import { ChartColor, type ChartConfig } from "@smartacteam/ambient-web/chart";
 
const chartConfig = {
  desktop: {
    label: "Desktop",
    color: ChartColor.SERIES_1_AREA,
  },
  mobile: {
    label: "Mobile",
    color: ChartColor.SERIES_2_AREA,
  },
} satisfies ChartConfig;

Build your chart

You can now build your chart using Recharts components.

Add a grid

Let's add a grid to the chart.

<Chart.Root config={chartConfig} className="h-100 w-full">
  <Recharts.BarChart accessibilityLayer data={chartData}>
    <Recharts.CartesianGrid syncWithTicks vertical={false} />
    <Recharts.Bar radius={4} dataKey="desktop" fill="var(--color-desktop)" />
    <Recharts.Bar radius={4} dataKey="mobile" fill="var(--color-mobile)" />
  </Recharts.BarChart>
</Chart.Root>

Add an axis

To add an x-axis to the chart, we'll use the XAxis component.

<Chart.Root config={chartConfig} className="min-100 w-full">
  <Recharts.BarChart accessibilityLayer data={chartData}>
    <Recharts.CartesianGrid syncWithTicks vertical={false} />
    <Recharts.XAxis
      dataKey="month"
      tickLine={false}
      tickMargin={10}
      axisLine={false}
      tickFormatter={(value) => value.slice(0, 3)}
    />
    <Recharts.Bar radius={4} dataKey="desktop" fill="var(--color-desktop)" />
    <Recharts.Bar radius={4} dataKey="mobile" fill="var(--color-mobile)" />
  </Recharts.BarChart>
</Chart.Root>

Add a tooltip

So far we've only used components from Recharts. They look great out of the box thanks to some customization in the chart component.

To add a tooltip, we'll use <Recharts.Tooltip /> and the custom <Chart.TooltipContent /> component from chart.

<Chart.Root config={chartConfig} className="min-100 w-full">
  <Recharts.BarChart accessibilityLayer data={chartData}>
    <Recharts.CartesianGrid syncWithTicks vertical={false} />
    <Recharts.XAxis
      dataKey="month"
      tickLine={false}
      tickMargin={10}
      axisLine={false}
      tickFormatter={(value) => value.slice(0, 3)}
    />
 
    <Recharts.Bar radius={4} dataKey="desktop" fill="var(--color-desktop)" />
    <Recharts.Bar radius={4} dataKey="mobile" fill="var(--color-mobile)" />
 
    <Recharts.Tooltip
      content={<Chart.TooltipContent labelKey="month" />}
      cursor={{ stroke: "none", fill: ChartColor.REGION, fillOpacity: 0.1 }}
    />
  </Recharts.BarChart>
</Chart.Root>

Add a legend

We'll do the same for the legend. We'll use <Recharts.Legend /> and the custom <Chart.LegendContent /> component from chart.

<Chart.Root config={chartConfig} className="min-100 w-full">
  <Recharts.BarChart accessibilityLayer data={chartData}>
    <Recharts.CartesianGrid syncWithTicks vertical={false} />
 
    <Recharts.XAxis
      dataKey="month"
      tickLine={false}
      tickMargin={10}
      axisLine={false}
      tickFormatter={(value) => value.slice(0, 3)}
    />
 
    <Recharts.Bar radius={4} dataKey="desktop" fill="var(--color-desktop)" />
    <Recharts.Bar radius={4} dataKey="mobile" fill="var(--color-mobile)" />
 
    <Recharts.Tooltip
      content={<Chart.TooltipContent labelKey="month" />}
      cursor={{ stroke: "none", fill: ChartColor.REGION, fillOpacity: 0.1 }}
    />
 
    <Recharts.Legend content={<Chart.LegendContent />} />
  </Recharts.BarChart>
</Chart.Root>

Chart Config

The chart config is where you define the labels, icons, units, colors, etc. for a chart.

It is intentionally decoupled from chart data.

This allows you to share config and color tokens between charts. It can also work independently for cases where your data or color tokens live remotely or in a different format.

import { ChartColor, type ChartConfig } from "@smartacteam/ambient-web/chart";
 
const chartConfig = {
  desktop: {
    label: "Desktop",
    icon: "IconTelevision",
    // A color like 'hsl(220, 98%, 61%)' or 'var(--color-name)'
    color: ChartColor.SERIES_1_AREA,
    // A string that will be appended to the value in tooltip and legend
    unit: " units",
    // The number of decimal places to show in tooltip and legend values
    decimalPlaces: 0,
    // OR a theme object with 'light' and 'dark' keys
    theme: {
      light: ChartColor.SERIES_1_AREA,
      dark: ChartColor.SERIES_2_AREA,
    },
  },
} satisfies ChartConfig;

Theming

Charts have built-in support for theming. You can use css variables (recommended) or color values in any color format, such as hex, hsl or oklch.

CSS Variables

Define your colors in your css file

@layer base {
  :root {
    --chart-1: oklch(0.646 0.222 41.116);
    --chart-2: oklch(0.6 0.118 184.704);
  }
 
  .dark: {
    --chart-1: oklch(0.488 0.243 264.376);
    --chart-2: oklch(0.696 0.17 162.48);
  }
}

Add the color to your chartConfig

const chartConfig = {
  desktop: {
    label: "Desktop",
    color: "var(--chart-1)",
  },
  mobile: {
    label: "Mobile",
    color: "var(--chart-2)",
  },
} satisfies ChartConfig;

hex, hsl or oklch

You can also define your colors directly in the chart config. Use the color format you prefer.

const chartConfig = {
  desktop: {
    label: "Desktop",
    color: "#2563eb",
  },
  mobile: {
    label: "Mobile",
    color: "hsl(220, 98%, 61%)",
  },
  tablet: {
    label: "Tablet",
    color: "oklch(0.5 0.2 240)",
  },
  laptop: {
    label: "Laptop",
    color: "var(--chart-2)",
  },
} satisfies ChartConfig;

Using colors

To use the theme colors in your chart, reference the colors using the format var(--color-KEY).

Components

<Recharts.Bar dataKey="desktop" fill="var(--color-desktop)" />

Chart Data

const chartData = [
  { browser: "chrome", visitors: 275, fill: "var(--color-chrome)" },
  { browser: "safari", visitors: 200, fill: "var(--color-safari)" },
];

Tailwind

<Recharts.LabelList className="fill-[--color-desktop]" />

Tooltip

A chart tooltip contains a label, name, indicator and value. You can use a combination of these to customize your tooltip.

You can turn on/off any of these using the hideLabel, hideIndicator props and customize the indicator style using the indicator prop.

Use labelKey and nameKey to use a custom key for the tooltip label and name.

Chart comes with the <Chart.TooltipContent /> component. You can use this component to add custom tooltips to your chart.

<Recharts.Tooltip content={<Chart.TooltipContent />} />

Tooltip Content Props

PropTypeDescription
labelKeystringThe key to use for the tooltip label.
useValuebooleanWhether to use the value as label.
nameKeystringThe key to use for the tooltip name.
hideLabelbooleanWhether to hide the tooltip label.
hideIndicatorbooleanWhether to hide the tooltip indicator.
indicatordot line or dashedThe style of the tooltip indicator.

Custom

To use a custom key for tooltip label and names, use the labelKey and nameKey props.

const chartData = [
  { browser: "chrome", visitors: 187, fill: "var(--color-chrome)" },
  { browser: "safari", visitors: 200, fill: "var(--color-safari)" },
];
 
const chartConfig = {
  visitors: {
    label: "Total Visitors",
  },
  chrome: {
    label: "Chrome",
    color: "hsl(var(--chart-1))",
  },
  safari: {
    label: "Safari",
    color: "hsl(var(--chart-2))",
  },
} satisfies ChartConfig;
<Recharts.Tooltip
  content={<Chart.TooltipContent labelKey="visitors" nameKey="browser" />}
/>

This will use Total Visitors for label and Chrome and Safari for the tooltip names.

Legend

You can use the custom <Chart.LegendContent> component to add a legend to your chart.

<Recharts.Legend content={<Chart.LegendContent />} />

Custom

To use a custom key for legend names, use the nameKey prop.

const chartData = [
  { browser: "chrome", visitors: 187, fill: "var(--color-chrome)" },
  { browser: "safari", visitors: 200, fill: "var(--color-safari)" },
];
 
const chartConfig = {
  chrome: {
    label: "Chrome",
    color: "hsl(var(--chart-1))",
  },
  safari: {
    label: "Safari",
    color: "hsl(var(--chart-2))",
  },
} satisfies ChartConfig;
<Chart.Legend content={<Chart.LegendContent nameKey="browser" />} />

This will use Chrome and Safari for the legend names.

Accessibility

You can turn on the accessibilityLayer prop to add an accessible layer to your chart.

This prop adds keyboard access and screen reader support to your charts.

<Recharts.LineChart accessibilityLayer />

Examples

Simple Line Chart

Line Chart with Annotations

Simple Bar Chart

Area Chart

Scatter Chart