[Chartjs]-React Js and Chart Js (Line Chart)

3๐Ÿ‘

โœ…

You should register the chart from chart.js

See an example here

import {
  Chart as ChartJS,
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend,
} from 'chart.js';

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

0๐Ÿ‘

You can try to add this line in the beginning of your file, it seems to fix this issue:
import Chart from "chart.js/auto";

0๐Ÿ‘

You could create the chart in the render() method.

render() {
  return (
    <Line 
      data={this.state.data}
      options={this.state.options}
    />
  );
}

Please take a look at the following StackBlitz and see how it works with your amended code.

Leave a comment