Migrating from Chart.js

Move react-chartjs-2 charts to Bklit UI React components. Turn datasets into components, map options to props, and replace canvas plugins with JSX.

Chart.js describes a chart as one configuration object (data and options) and draws it to a canvas. In React you usually reach it through react-chartjs-2. Bklit UI charts are React components rendered as SVG: each series is a component, and styling comes from your CSS theme. This guide shows how the two map onto each other.

Weighing up the switch? Read Bklit UI vs Chart.js.

What changes

  • Datasets become components. Each entry in datasets becomes a <Line>, <Area> or <Bar>.
  • Data becomes one object per point. Instead of a labels array plus a data array per dataset, every row holds its x value and all its series values.
  • Options become props and children. Grids, axes and tooltips are components you add or leave out, not nested option objects.
  • No registration step. You don't call Chart.register(...); the shadcn CLI installs what each chart needs.
  • Styling uses CSS. Colours come from CSS variables such as var(--chart-line-primary), so charts follow your theme and dark mode. See Theming.

Migrate with an AI agent

Install the Bklit UI agent skills, then paste this prompt into your coding agent:

Migrate every Chart.js chart in this project to Bklit UI.

1. Find files that import from "react-chartjs-2" or "chart.js".
2. For each chart type used, install the Bklit chart with
   `npx shadcn@latest add @bklit/<chart>-chart` (for example @bklit/line-chart).
3. Rewrite each chart following https://bklit.com/docs/migrating-from-chartjs:
   - Turn `labels` + `datasets[].data` into one object per point.
   - Line/area charts: put a Date in `date` for each row.
   - Each dataset becomes <Line dataKey>, <Area dataKey> or <Bar dataKey>.
   - Grid lines -> <Grid horizontal />, tooltip -> <ChartTooltip />.
   - Use CSS variables for colours instead of borderColor/backgroundColor.
   - Remove Chart.register(...) calls.
4. Keep the same data, series and labels. Don't change unrelated code.
5. When nothing imports chart.js any more, remove chart.js and
   react-chartjs-2 and run the type checker.

Migrate by hand

1. Add the Bklit charts you use

Make sure your project is set up for shadcn/ui (see Installation), then add each chart type you need:

npx shadcn@latest add @bklit/line-chart @bklit/bar-chart @bklit/pie-chart

2. Reshape the data

// Chart.js
const data = {
  labels: ["2026-01-01", "2026-01-02", "2026-01-03"],
  datasets: [
    { label: "Revenue", data: [12000, 13500, 11000] },
    { label: "Costs", data: [8500, 9200, 7800] },
  ],
};

// Bklit UI
const data = [
  { date: new Date("2026-01-01"), revenue: 12000, costs: 8500 },
  { date: new Date("2026-01-02"), revenue: 13500, costs: 9200 },
  { date: new Date("2026-01-03"), revenue: 11000, costs: 7800 },
];

Line and area charts use a time scale, so each row needs a Date. For category labels such as month names, use a bar chart with xDataKey.

3. Map chart types and options

Chart.jsBklit UINotes
<Line> (type line)LineChart + LineOne Line per dataset
fill: true on a line datasetAreaChart + Area
<Bar> (type bar)BarChart + BarCategory key via xDataKey
indexAxis: "y"BarChart orientation="horizontal"
scales.x.stacked / scales.y.stackedBarChart stacked
<Pie>, <Doughnut>PieChart + PieSliceSet innerRadius for a doughnut
<Radar>RadarChart
<Scatter>ScatterChartTime-series scatter
Mixed type per datasetComposedChartSeriesBar, Line and Area on one axis
borderColor, backgroundColorstroke, fillAny CSS colour or variable
tensioncurveA d3-shape curve, such as curveMonotoneX
scales.*.gridGridhorizontal / vertical
scales.x, scales.yXAxis, YAxisBarXAxis, BarYAxis in bar charts
plugins.tooltipChartTooltipFormat with rows, or render your own with content
plugins.legendLegendSee Legend
responsive, maintainAspectRatioaspectRatioCharts fill their parent's width
animation—Built in. Turn it off per series with animate={false}

4. Remove Chart.js

When nothing imports it any more:

npm uninstall chart.js react-chartjs-2

Before and after

Line chart

// Chart.js (react-chartjs-2)
import {
  CategoryScale,
  Chart as ChartJS,
  LinearScale,
  LineElement,
  PointElement,
  Tooltip,
} from "chart.js";
import { Line } from "react-chartjs-2";

ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Tooltip);

<Line
  data={{
    labels,
    datasets: [
      { label: "Revenue", data: revenue, borderColor: "#2563eb", tension: 0.4 },
      { label: "Costs", data: costs, borderColor: "#a855f7", tension: 0.4 },
    ],
  }}
  options={{ responsive: true }}
/>
// Bklit UI
import { ChartTooltip, Grid, Line, LineChart, XAxis } from "@bklitui/ui/charts";

<LineChart data={data}>
  <Grid horizontal />
  <Line dataKey="revenue" />
  <Line dataKey="costs" stroke="var(--chart-line-secondary)" />
  <XAxis />
  <ChartTooltip />
</LineChart>

Bar chart

// Chart.js
<Bar
  data={{
    labels: ["Jan", "Feb", "Mar"],
    datasets: [{ label: "Revenue", data: [12000, 15500, 11000] }],
  }}
/>
// Bklit UI
const data = [
  { month: "Jan", revenue: 12000 },
  { month: "Feb", revenue: 15500 },
  { month: "Mar", revenue: 11000 },
];

<BarChart data={data} xDataKey="month">
  <Grid horizontal />
  <Bar dataKey="revenue" />
  <BarXAxis />
  <ChartTooltip />
</BarChart>

Doughnut chart

// Chart.js
<Doughnut
  data={{
    labels: ["Electronics", "Clothing", "Food"],
    datasets: [{ data: [4250, 3120, 2100] }],
  }}
/>
// Bklit UI
const data = [
  { label: "Electronics", value: 4250 },
  { label: "Clothing", value: 3120 },
  { label: "Food", value: 2100 },
];

<PieChart data={data} innerRadius={70}>
  {data.map((item, index) => (
    <PieSlice key={item.label} index={index} />
  ))}
</PieChart>

Troubleshooting

My custom Chart.js plugin has no equivalent

Most plugins draw something extra on the chart: annotations, labels or reference lines. In Bklit UI these are components, such as Markers, Reference Area and Trend Line, or your own SVG built with the useChart hook.

Charts with many thousands of points feel slower

Bklit line and area charts downsample long series to about 1.5 points per pixel, so typical time-series data stays fast in SVG. If you draw very large scatter plots, sample the data before passing it in.

The line chart is empty

Line and area charts need a Date on each row under date, or the key you pass to xDataKey.