Charts
Charts ship in @leverui/components. Once Lever UI is
set up, there is nothing more to install.
Usage
Import the charts component
import { BarChart, useChart } from '@leverui/components';
Define chart data
More in useChart.
const chart = useChart({
data: [
{ month: 'January', value: 100 },
{ month: 'February', value: 200 },
],
series: [{ name: 'value', color: 'teal.solid' }],
});
Render the chart
<BarChart
data={chart.data}
xKey="month"
series={chart.series.map((item) => ({
name: chart.key(item.name),
color: chart.color(item.color) ?? 'teal.solid',
}))}
/>
The hook is optional. Most charts also take plain data and series arrays.
Chart types
- Area Chart: amounts over time, filled.
- Bar Chart: amounts side by side.
- Bar List: a ranked list.
- Bar Segment: parts of a total on one bar.
- Donut Chart: parts of a whole, with a center title.
- Line Chart: a trend over time.
- Pie Chart: parts of a whole.
- Radar Chart: several measures on one shape.
- Scatter Chart: how two numbers relate.
- Sparkline: a small trend line.
Customization
Each chart's page lists its props and CSS variables.
Colors
chart.color turns a color token into its CSS variable.
<BarChart
style={{ '--leverui-chart-grid-color': chart.color('border.muted') }}
data={chart.data}
xKey="month"
series={[{ name: 'value', color: 'teal.solid' }]}
/>
Formatters
formatDate and formatNumber use the locale from
LocaleProvider. formatDate needs date strings such
as 2024-01-01 in the X-axis field.
<LineChart
data={chart.data}
xKey="date"
series={[{ name: 'revenue', color: 'teal.solid' }]}
// format the X-axis labels
xTickFormatter={chart.formatDate({ month: 'short', day: '2-digit' })}
// format the Y-axis labels
yTickFormatter={chart.formatNumber({ maximumFractionDigits: 1 })}
/>