File size: 2,423 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
title: Charts
description: Creating beautiful charts with recharts and Chakra UI
---

<Iframe
  title="Chakra UI Charts Dashboard"
  src="https://www.youtube.com/embed/GYgqlv6DBs8?si=hBuIjDffeXUzZ1Qj?rel=0&modestbranding=1&showinfo=0"
  allowFullScreen
/>

Charts are designed to look great out of the box, seamlessly integrating with
other Chakra UI's theming system. The charts are built on top of
[recharts](https://recharts.org)

## Installation

Run the following command to install the charts and its peer dependencies.

```bash
npm i @chakra-ui/charts recharts
```

## Usage

:::steps

### Import the charts component

In most cases, you need to import the `Chart` and `useChart` hook from the
`@chakra-ui/charts` package, then combine them with the components `recharts`

```tsx
import { Chart, useChart } from "@chakra-ui/charts"
import { Bar, BarChart, XAxis, YAxis } from "recharts"
```

### Define chart data

Pass the chart data to the `useChart` hook to create a chart instance.

> Learn more about the [`useChart`](/docs/charts/use-chart) hook.

```tsx
const chart = useChart({
  data: [
    { month: "January", value: 100 },
    { month: "February", value: 200 },
  ],
})
```

### Render the chart

Depending on the chart type you need from the `recharts` library, wrap the chart
component within the `Chart.Root` component.

```tsx
<Chart.Root chart={chart}>
  <BarChart data={chart.data}>
    {chart.series.map((item) => (
      <Bar
        key={item.name}
        dataKey={chart.key(item.name)}
        fill={chart.color(item.color)}
      />
    ))}
  </BarChart>
</Chart.Root>
```

:::

## Customization

The charts component is built on top of [Recharts](https://recharts.org), so you
can use all the customization options that Recharts provides.

### Colors

The `useChart` hook provides a `color` function that you can use to query
semantic colors for the chart component from `recharts`.

```tsx
<CartesianGrid stroke={chart.color("border.muted")} />
```

### Formatters

The `useChart` hook provides a `formatDate` and `formatNumber` function that you
can use to format the date and number respectively. This is useful for
formatting the x, y axis labels and tooltips.

```tsx
// format the x-axis labels
<XAxis tickFormatter={chart.formatDate({ month: "short", day: "2-digit" })} />

// format the y-axis labels
<YAxis tickFormatter={chart.formatNumber({ maximumFractionDigits: 1 })} />
```