# Chakra UI Charts Reference Charts live in a separate package. Install it alongside `@chakra-ui/react`: ```bash # npm npm install @chakra-ui/charts recharts # pnpm pnpm add @chakra-ui/charts recharts # yarn yarn add @chakra-ui/charts recharts ``` Imports come from `@chakra-ui/charts`, not `@chakra-ui/react`: ```ts import { BarList, BarSegment, Chart, useChart } from "@chakra-ui/charts" import type { BarListData, BarSegmentData } from "@chakra-ui/charts" ``` --- ## Three chart types | Component | Best for | Recharts? | | ----------------------- | ---------------------------------------------- | --------- | | `BarList` | Rankings, comparisons (horizontal bar per row) | No | | `BarSegment` | Composition / parts of a whole (stacked bar) | No | | `Chart.Root` + Recharts | Line, area, bar, pie, radar, and more | Yes | --- ## `useChart` — the core hook Every chart starts with `useChart`. It processes data, resolves color tokens, and provides utilities used throughout the chart's compound components. ```ts const chart = useChart({ data: [...], // array of data objects series: [...], // which keys to plot and how to color them sort: { by: "value", direction: "desc" }, // optional }) ``` **`series` items:** ```ts { name: keyof T // data key to read values from color: ChartColor // Chakra token ("teal.solid") or CSS color ("#FF0000") label?: ReactNode // display name in legend/tooltip (defaults to name) stackId?: string // for stacking in Recharts charts yAxisId?: string // for dual-axis charts } ``` **Color type** — accepts any Chakra color token or CSS color: ```ts type ChartColor = Tokens["colors"] | CSSProperties["color"] // "teal.solid" | "blue.subtle" | "red.500" | "#FF0000" | "hsl(...)" ``` **Useful return values:** ```ts chart.data // sorted data array chart.series // resolved series config chart.color("teal.solid") // resolves token → CSS color chart.key("revenue") // safe key accessor chart.formatNumber({ notation: "compact" })(1234567) // "1.2M" chart.formatDate({ month: "short" })("2024-01-15") // locale-aware chart.getTotal("revenue") // sum all values chart.getMin("revenue") / chart.getMax("revenue") chart.getValuePercent("revenue", value) // % of total ``` --- ## BarList Horizontal bar chart — one bar per data row. Good for leaderboards, traffic sources, top items. **Data shape:** ```ts interface BarListData { name: string // label shown on the bar value: number // determines bar width href?: string // optional link } ``` **Basic example:** ```tsx import { BarList, type BarListData, useChart } from "@chakra-ui/charts" const chart = useChart({ data: [ { name: "Google", value: 1_200_000 }, { name: "Direct", value: 840_000 }, { name: "Twitter", value: 320_000 }, { name: "LinkedIn", value: 95_000 }, ], sort: { by: "value", direction: "desc" }, series: [{ name: "name", color: "teal.subtle" }], }) return ( ) ``` **With a custom value formatter:** ```tsx `${(v / 1000).toFixed(0)}K`} /> ``` **With multiple value columns:** ```tsx `${chart.getValuePercent("value", v).toFixed(0)}%` } /> ``` --- ## BarSegment Single horizontal bar broken into colored segments. Good for showing how a total is split across categories (budget, traffic, inventory). **Data shape:** ```ts interface BarSegmentData { name: string // segment label value: number // determines segment width color: string // Chakra color token for this segment } ``` **Basic example:** ```tsx import { BarSegment, useChart } from "@chakra-ui/charts" const chart = useChart({ data: [ { name: "Google", value: 500_000, color: "teal.solid" }, { name: "Direct", value: 300_000, color: "blue.solid" }, { name: "Twitter", value: 150_000, color: "purple.solid" }, { name: "LinkedIn", value: 50_000, color: "orange.solid" }, ], sort: { by: "value", direction: "desc" }, }) return ( ) ``` **With a reference line (e.g. target):** ```tsx ``` --- ## Chart.Root + Recharts Wrapper for any Recharts chart. Handles sizing, token resolution, and styled tooltips and legends. You still compose the chart using standard Recharts components — `BarChart`, `AreaChart`, `LineChart`, `PieChart`, etc. ### Bar chart ```tsx import { Chart, useChart } from "@chakra-ui/charts" import { Bar, BarChart, CartesianGrid, Legend, Tooltip, XAxis, YAxis, } from "recharts" const chart = useChart({ data: [ { month: "Jan", revenue: 186, expenses: 80 }, { month: "Feb", revenue: 165, expenses: 95 }, { month: "Mar", revenue: 210, expenses: 110 }, ], series: [ { name: "revenue", color: "teal.solid" }, { name: "expenses", color: "orange.solid" }, ], }) return ( } /> } /> {chart.series.map((item) => ( ))} ) ``` ### Area chart ```tsx import { Area, AreaChart, CartesianGrid, Tooltip, XAxis } from "recharts" return ( {chart.series.map((item) => ( ))} } /> {chart.series.map((item) => ( ))} ) ``` ### Pie / donut chart ```tsx import { Pie, PieChart, Tooltip } from "recharts" const chart = useChart({ data: [ { browser: "Chrome", visitors: 275, color: "teal.solid" }, { browser: "Safari", visitors: 200, color: "blue.solid" }, { browser: "Firefox", visitors: 120, color: "orange.solid" }, { browser: "Edge", visitors: 80, color: "purple.solid" }, ], series: [{ name: "visitors" }], }) return ( } /> {chart.data.map((item, index) => ( ))} {/* Center text for donut */} ) ``` --- ## Chart sub-components ### `Chart.Tooltip` ```tsx [`$${value}`, name]} /> } /> ``` ### `Chart.Legend` ```tsx } /> ``` Legend interaction highlights/dims series on hover or click. ### `Chart.Gradient` Defines an SVG gradient for area fills. Must be inside a `` block: ```tsx ``` ### `Chart.RadialText` Centers a title + description inside a pie or donut chart: ```tsx