2

I am trying to use the chart.js bar chart with react and es6.

As I am using import instead of require my project looks a bit different than the documentation on GitHub.

Here is an example of my project:

import React from 'react';
import { BarChart } from 'react-chartjs';

class HelloChart extends React.Component {
  constructor() {
    super();
    let data = {
      labels: ["January", "February", "March", "April", "May", "June", "July"],
      datasets: [{
        label: "My First dataset",
        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)'
        ],
        borderColor: [
          'rgba(255,99,132,1)',
          'rgba(54, 162, 235, 1)',
          'rgba(255, 206, 86, 1)',
          'rgba(75, 192, 192, 1)',
          'rgba(153, 102, 255, 1)',
          'rgba(255, 159, 64, 1)'
        ],
        borderWidth: 1,
        data: [65, 59, 80, 81, 56, 55, 40],
      }]
    };
    let options = {
      scales: {
        xAxes: [{
          stacked: true
        }],
        yAxes: [{
          stacked: true
        }]
      }
    };
    this.state = {
      chartData: data,
      chartOptions: options,
    };
  }
  render() {
    let chartData = this.state.chartData;
    let chartOptions = this.state.chartOptions;
    return (
      <div>
        <BarChart data={chartData} options={chartOptions} width="600" height="250" />
      </div>
    )
  }
}

export default HelloChart

These are the two errors in am getting in my console:

Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method ofHelloChart.

And ...

Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. Check the render method ofHelloChart.

1 Answer 1

3

This should solve your problem:

const BarChart = require("react-chartjs").Bar;

or,

import { Bar as BarChart } from 'react-chartjs';

Try this:

import React from 'react';

const BarChart = require('react-chartjs').Bar;

class HelloChart extends React.Component {
  constructor() {
    super();
    const data = {
      labels: ["January", "February", "March", "April", "May", "June", "July"],
      datasets: [{
        label: "My First dataset",
        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)'
        ],
        borderColor: [
          'rgba(255,99,132,1)',
          'rgba(54, 162, 235, 1)',
          'rgba(255, 206, 86, 1)',
          'rgba(75, 192, 192, 1)',
          'rgba(153, 102, 255, 1)',
          'rgba(255, 159, 64, 1)'
        ],
        borderWidth: 1,
        data: [65, 59, 80, 81, 56, 55, 40],
      }]
    };
    const options = {
      scales: {
        xAxes: [{
          stacked: true
        }],
        yAxes: [{
          stacked: true
        }]
      }
    };
    this.state = {
      chartData: data,
      chartOptions: options,
    };
  }
  render() {
    const { chartData, chartOptions } = this.state;
    return (
      <div>
        <BarChart data={chartData} options={chartOptions} width="600" height="250" />
      </div>
    )
  }
}

export default HelloChart;
Sign up to request clarification or add additional context in comments.

1 Comment

Per the example, this is code I am using now: gist.github.com/SeanPlusPlus/c39d4f25d76dbbebfffaa10f9193d136 And the error I get is: "Uncaught TypeError: (intermediate value)[chartType] is not a function(…)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.