Home > front end >  My react app is just not displaying the graph
My react app is just not displaying the graph

Time:01-20

I am trying to make a bar chart using react-chartjs-2. whenever I run my react app using the following code it shows a black page only. What am I doing wrong here. Here is my code for Chart.js

import React,{Component} from "react";
import { Bar } from 'react-chartjs-2';

class Chart extends Component{

    constructor(props){
        super(props);
        this.state = {
            chartData:{
              labels: ['Boston','Worcestor',
            'Springfield','Lowell','Cambridge','New bedford'],
              datasets: [
                  {
                      label: 'Population',
                      data: [
                          641712,147895,12479546,1457897,127955,98745
                      ],
                      backgroundColor: [
                        'rgba(255, 99, 132, 0.2)',
                        'rgba(54, 162, 235, 0.2)',
                        'rgba(255, 206, 86, 0.2)',
                        'rgba(75, 192, 192, 0.2)',
                        'rgba(153, 102, 255, 0.2)',
                        'rgba(255, 159, 64, 0.2)',
                        'rgba(255, 99, 131, 0.2)'
                      ]
                  }
              ]
            }
         }
    }

    render(){
        return(
            <div className="chart">
                <Bar
                    data={this.state.chartData}
                    options={{
                        maintainAspectRatio: false
                    }}
                />
            </div>
        )
    }
}

export default Chart;

And this is main App.js

CodePudding user response:

You need to add this line to your imports: import Chart from 'chart.js/auto';

See Chart.js Integration

  •  Tags:  
  • Related