Charts

Charts ship in @leverui/components. Once Lever UI is set up, there is nothing more to install.

Usage

Import the charts component

Code
import { BarChart, useChart } from '@leverui/components';

Define chart data

More in useChart.

Code
const chart = useChart({
  data: [
    { month: 'January', value: 100 },
    { month: 'February', value: 200 },
  ],
  series: [{ name: 'value', color: 'teal.solid' }],
});

Render the chart

Code
<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

Customization

Each chart's page lists its props and CSS variables.

Colors

chart.color turns a color token into its CSS variable.

Code
<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.

Code
<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 })}
/>