[Chartjs]-Type can not be assigned in react-chartjs-2

7👍

Firstly you are missing a required data attribute i.e.

<Pie data={data} />

You can then change your code to:

import React from 'react';
import * as ReactDOM from "react-dom"
import { Pie } from 'react-chartjs-2';
import { ChartOptions } from 'chart.js'

export default class Chart extends React.Component {
  constructor(props) {
    super(props);
    this.state = {};
  }

  render() {
    const data = {
      labels: [
        'Red',
        'Green',
        'Yellow'
      ],
      datasets: [{
        data: [300, 50, 100],
        backgroundColor: [
          '#FF6384',
          '#36A2EB',
          '#FFCE56'
        ],
        hoverBackgroundColor: [
          '#FF6384',
          '#36A2EB',
          '#FFCE56'
        ]
      }]
    };

    const options: ChartOptions = {
      legend: {
        position: 'bottom',
      }
    };

    return (
      <Pie data={data} options={options} />
    );
  }
}

ReactDOM.render(<Chart />, document.getElementById("root"))

You can see this example working here.

0👍

When we are using Typescript with we can use legend and it’s properties and it’s values like as below.

<Pie                         
   data={data}          
   options={{   
     responsive: true,  
     maintainAspectRatio: true, 
     aspectRatio: 2, 
      plugins: {
         legend: {                                    
          display: true,
              position:'bottom', 
              labels:{
                 padding: 40
              },                                  
           },
         },                           
      }} 
  />

Leave a comment