apps/www/content/docs/charts/use-chart.mdx
The useChart hook provides a set of utilities to manage and format data for
charts efficiently. It offers various helpers for:
import { useChart } from "@chakra-ui/charts"
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" }],
})
getKeyReturns the key for a given series item. It's an identity function but provides a typesafe way to access the series data.
const key = chart.getKey("revenue")
getTotalComputes the total sum of a given series across all entries.
console.log(chart.getTotal("revenue")) // 6000
getMinFinds the minimum value for a given key in the dataset.
console.log(chart.getMin("revenue")) // 1000
getMaxFinds the maximum value for a given key in the dataset.
console.log(chart.getMax("revenue")) // 3000
getValuePercentCalculates the percentage of a value relative to the dataset or a given domain.
const percentage = chart.getValuePercent("revenue", 5000)
console.log(percentage) // 0.5
formatNumberFormats numbers using the current locale from EnvironmentProvider and
Intl.NumberFormatOptions provided.
const format = chart.formatNumber({ style: "currency", currency: "USD" })
console.log(format(1000)) // "$1,000.00"
formatDateFormats a date string based on locale settings.
const formatDate = chart.formatDate({ year: "numeric", month: "short" })
console.log(formatDate("2024-03-28")) // "Mar 2024"
colorRetrieves a Chakra UI color token.
const barColor = chart.color("blue.500") // var(--chakra-colors-blue-500)
sizeRetrieves a Chakra UI size token.
const chartSize = chart.size("4") // var(--chakra-sizes-4)
spacingRetrieves a Chakra UI spacing token.
const barColor = chart.color("blue.500") // var(--chakra-colors-blue-500)
const chartPadding = chart.spacing("4") // var(--chakra-spacing-4)
Automatically sorts the chart data based on a specified key and direction.
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" }],
})
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.
highlightedSeriesTracks which series is currently highlighted.
setHighlightedSeriesSets the highlighted series.
chart.setHighlightedSeries("revenue")
isHighlightedSeriesChecks if a series is highlighted.
const isActive = chart.isHighlightedSeries("revenue")
| 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 |