0

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

AWolf
  • 8,770
  • 5
  • 33
  • 39
Vara
  • 3
  • 2
  • 2
    Are there any errors on the browser's development console? – David Jan 18 '22 at 20:20
  • 1
    Are you getting errors? Have you looked at the dev tool console in your browser? – Andy Jan 18 '22 at 20:20
  • Your question could be related to this [SO question](https://stackoverflow.com/questions/67727603/error-category-is-not-a-registered-scale) but it's difficult without the error message. Adding `import "chart.js/auto";` could help. Please have a look at the following [Codesandbox](https://codesandbox.io/s/priceless-stonebraker-dw7kj) – AWolf Jan 18 '22 at 22:27
  • @David the console shows no errors. It just says 'Compiled successfully!' – Vara Jan 19 '22 at 13:14

1 Answers1

0

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

See Chart.js Integration

juicy-g
  • 413
  • 1
  • 3
  • 9