UIComponentsReact
Bar and line charts drawn by Unovis, with a tooltip on hover and keyboard focus, a legend and a table of the data.
Rendered by React 19.3.1
Installation
npx shadcn@4.23.0 add https://hamur.dev/r/react/chart.jsonIt also adds utils. The app needs its setup first: Installation.
Usage
Describe the series in a ChartConfig (a label and a color or a light and dark theme per key) and pass it to ChartContainer with data and index, the key of each datum's category. Each series colour becomes --color-<key>, and a series without one takes the next of the --chart-1 to --chart-5 tokens. Inside the container, put the Unovis XY components (VisGroupedBar, VisStackedBar, VisLine, VisArea, VisAxis from @unovis/react) with x={(_, i) => i} and colours such as color="var(--color-desktop)"; give the x axis tickValues={data.map((_, i) => i)} and a tickFormat that returns data[i][index]. Add ChartTooltip (with ChartTooltipContent) for the tooltip: one column per datum covers the plot, the pointer or the keyboard opens the headless Tooltip on it, and the arrow keys move between data in a Toolbar. cursor is band (bars), line (lines) or false. A legend appears below the chart for two or more series; add ChartLegend with ChartLegendContent to change it, or verticalAlign="top" to move it up. Every chart has a visually hidden table of its values for screen readers.
import {
ChartContainer,
ChartTooltip,
ChartTooltipContent,
ChartLegend,
ChartLegendContent,
ChartStyle,
} from '@/components/ui/chart'Examples
Default
A bar chart of two series with a tooltip and legend.
Differences from shadcn's: the charts are drawn by Unovis (@unovis/react and @unovis/ts 1.7.1), not Recharts. ChartContainer renders Unovis's XY container itself, so it takes the chart's data, index (the category key) and an optional margin (the plot's inset in pixels, 8, 12, 24 and 40 from the top clockwise); Unovis's automatic margin is off, so the axes draw into that margin. Each datum's x is its position, so marks use x={(_, i) => i} and the x axis needs tickValues. ChartTooltip is not Recharts' tooltip but a layer of Base UI Tooltip triggers, one per datum, in a Base UI Toolbar: it opens on keyboard focus as well as on hover, takes content and a cursor of band, line or false, and is labelled "Chart data" unless given an aria-label. ChartTooltipContent and ChartLegendContent keep shadcn's props; their payload holds one item per config key. With no ChartLegend child, a chart of two or more series gets the default legend, and a chart of one series never shows one. The container adds a visually hidden <table> of the data (data-slot="chart-table"), and maps Unovis's default colours (--vis-color0 to --vis-color4) and axis colours to the theme's tokens. Pie, radar and radial charts (Unovis's single container) are not covered.
API reference
Chart wraps no headless component of its own: its parts are elements with classes, and their props are the element's.