【问题标题】:Render bar chart with chart.js and react and es6使用 chart.js 和 react 和 es6 渲染条形图
【发布时间】:2016-11-15 02:18:58
【问题描述】:

我正在尝试将 chart.js 条形图与 react 和 es6 一起使用。

由于我使用的是import 而不是require,所以我的项目看起来与documentation on GitHub 有点不同。

这是我的项目的一个例子:

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

这是我的控制台出现的两个错误:

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.

还有……

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.

【问题讨论】:

    标签: reactjs charts ecmascript-6 react-chartjs


    【解决方案1】:

    这应该可以解决您的问题:

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

    或者,

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

    试试这个:

    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;
    

    【讨论】:

    猜你喜欢
    • 2017-06-30
    • 2019-10-02
    • 1970-01-01
    • 2014-03-10
    • 2011-08-06
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多