React Chart.js 2

5.3.1 · active · verified Sun Apr 19

react-chartjs-2 provides React components that wrap the popular Chart.js library, enabling declarative data visualization within React applications. It supports Chart.js v4 and v3 (though v4 is recommended) and React versions 16.8.0 through 19.0.0. The current stable version is 5.3.1. Releases appear to be driven by Chart.js and React compatibility updates, with minor versions released every few months, and patch fixes as needed. Its key differentiator is providing a native React component API for Chart.js, abstracting away direct Chart.js instance management, and offering a robust integration with the React component lifecycle for dynamic chart updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic line chart using `react-chartjs-2`, including the necessary Chart.js component registrations for tree-shaking.

import React from 'react';
import { Line } from 'react-chartjs-2';
import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend } from 'chart.js';

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

export const MyLineChart = () => {
  const data = {
    labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
    datasets: [
      {
        label: 'My First dataset',
        fill: false,
        lineTension: 0.1,
        backgroundColor: 'rgba(75,192,192,0.4)',
        borderColor: 'rgba(75,192,192,1)',
        borderCapStyle: 'butt',
        borderDash: [],
        borderDashOffset: 0.0,
        borderJoinStyle: 'miter',
        pointBorderColor: 'rgba(75,192,192,1)',
        pointBackgroundColor: '#fff',
        pointBorderWidth: 1,
        pointHoverRadius: 5,
        pointHoverBackgroundColor: 'rgba(75,192,192,1)',
        pointHoverBorderColor: 'rgba(220,220,220,1)',
        pointHoverBorderWidth: 2,
        pointRadius: 1,
        pointHitRadius: 10,
        data: [65, 59, 80, 81, 56, 55, 40]
      }
    ]
  };

  const options = {
    responsive: true,
    plugins: {
      legend: {
        position: 'top'
      },
      title: {
        display: true,
        text: 'Chart.js Line Chart'
      }
    }
  };

  return <Line data={data} options={options} />;
};

view raw JSON →