File size: 4,847 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
---
title: useChart
description: Provides utilities to manage and format data for charts
---

The `useChart` hook provides a set of utilities to manage and format data for
charts efficiently. It offers various helpers for:

- aggregating the series data to compute values such as totals, minimums,
  maximums, and percentages
- formatting **numbers and dates**
- querying **color, size, and spacing** tokens

## Usage

```tsx
import { useChart } from "@chakra-ui/charts"
```

```tsx
const chart = useChart({
  data: [
    { date: "2024-01-01", revenue: 1000 },
    { date: "2024-01-02", revenue: 2000 },
    { date: "2024-01-03", revenue: 3000 },
  ],
  series: [{ name: "revenue", color: "blue.500" }],
})
```

## Series

### `getKey`

Returns the key for a given series item. It's an identity function but provides
a **typesafe** way to access the series data.

```tsx
const key = chart.getKey("revenue")
```

## Aggregation

### `getTotal`

Computes the **total sum** of a given series across all entries.

```tsx
console.log(chart.getTotal("revenue")) // 6000
```

### `getMin`

Finds the **minimum value** for a given key in the dataset.

```tsx
console.log(chart.getMin("revenue")) // 1000
```

### `getMax`

Finds the **maximum value** for a given key in the dataset.

```tsx
console.log(chart.getMax("revenue")) // 3000
```

### `getValuePercent`

Calculates **the percentage** of a value relative to the dataset or a given
domain.

```tsx
const percentage = chart.getValuePercent("revenue", 5000)
console.log(percentage) // 0.5
```

## Formatting

### `formatNumber`

Formats numbers using the current locale from `EnvironmentProvider` and
`Intl.NumberFormatOptions` provided.

```tsx
const format = chart.formatNumber({ style: "currency", currency: "USD" })
console.log(format(1000)) // "$1,000.00"
```

### `formatDate`

Formats a date string based on **locale settings**.

```tsx
const formatDate = chart.formatDate({ year: "numeric", month: "short" })
console.log(formatDate("2024-03-28")) // "Mar 2024"
```

## Design Tokens

### `color`

Retrieves a **Chakra UI color token**.

```tsx
const barColor = chart.color("blue.500") // var(--chakra-colors-blue-500)
```

### `size`

Retrieves a **Chakra UI size token**.

```tsx
const chartSize = chart.size("4") // var(--chakra-sizes-4)
```

### `spacing`

Retrieves a **Chakra UI spacing token**.

```tsx
const barColor = chart.color("blue.500") // var(--chakra-colors-blue-500)
const chartPadding = chart.spacing("4") // var(--chakra-spacing-4)
```

## Sorting

Automatically sorts the chart data based on a specified key and direction.

```tsx /sort: { by: "date", direction: "asc" }/
const chart = useChart({
  data: [
    { date: "2024-01-01", revenue: 1000 },
    { date: "2024-01-02", revenue: 2000 },
    { date: "2024-01-03", revenue: 3000 },
  ],
  sort: { by: "date", direction: "asc" },
  series: [{ name: "revenue", color: "blue.500" }],
})
```

## Highlighting

When interacting with the chart legends, the series can be highlighted based on
`click` or `hover` events. The `highlightedSeries` state is used to track which
series is currently highlighted.

> This is mostly useful when you have multiple series in the chart.

### `highlightedSeries`

Tracks which series is currently highlighted.

### `setHighlightedSeries`

Sets the highlighted series.

```tsx
chart.setHighlightedSeries("revenue")
```

### `isHighlightedSeries`

Checks if a series is highlighted.

```tsx
const isActive = chart.isHighlightedSeries("revenue")
```

## API Table

| Helper                                | Purpose                           |
| ------------------------------------- | --------------------------------- |
| `getSeries(item)`                     | Finds details of a series item    |
| `getSeriesOpacity(name, fallback)`    | Controls series opacity           |
| `getTotal(key)`                       | Computes total sum of a key       |
| `getMin(key)`                         | Gets minimum value for a key      |
| `getMax(key)`                         | Gets maximum value for a key      |
| `getValuePercent(key, value, domain)` | Calculates percentage of a value  |
| `formatNumber(options)`               | Formats numbers based on locale   |
| `formatDate(options)`                 | Formats dates based on locale     |
| `color(key)`                          | Retrieves Chakra UI color token   |
| `size(key)`                           | Retrieves Chakra UI size token    |
| `spacing(key)`                        | Retrieves Chakra UI spacing token |
| `data`                                | The resolved chart data           |
| `highlightedSeries`                   | Tracks highlighted series         |
| `setHighlightedSeries(name)`          | Sets highlighted series           |
| `isHighlightedSeries(name)`           | Checks if a series is highlighted |