使用Chartjs with reactjs

时间:2017-04-16 06:56:41

标签: javascript reactjs chart.js

我想将ReadtFauxDom与chartJS一起使用,但chartJS不会渲染任何东西(FauxDom为空),我认为这个pb是由于ReactFauxDOM.element('canvas')。getContext('2d')不存在 enter image description here 你怎么看 ? (如果这是真的,我怎样才能创建一个dom并用它来生成我的图表)

这里是代码:

var Chart = require('chart.js');
class BarReact extends React.Component {
    constructor() {
        super()
    }
    componentWillMount() {
        var ctx = new ReactFauxDOM.Element('canvas');

        var myChart = new Chart(ctx, {

            type: 'bar',
            data: {
                labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
                datasets: [{
                    label: '# of Votes',
                    data: [12, 19, 3, 5, 2, 3],
                    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
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero: true
                        }
                    }]
                }
            }
        });
        this.setState({ chart: ctx })
    }

    render() {

        return (

                        {this.state.chart.toReact()}

        )
    }
}

注意:我正在使用 “chart.js”:“^ 2.5.0”, “react-faux-dom”:“^ 3.0.1”

3 个答案:

答案 0 :(得分:1)

不确定这是否有用(未使用react-faux-dom)但我会做以下事情;使用react渲染canvas元素然后当组件安装时使用该时刻来创建图形

import React from 'react';
import Chart from 'chart.js';
import ReactDOM from 'react-dom';

class ChartComponent extends React.Component {


    componentDidMount() {
        this.initializeChart(this.props.config);
    }

    initializeChart(options) {
        let el = ReactDOM.findDOMNode(this.refs.chart);
        let ctx = el.getContext("2d");
        let chart = new Chart(ctx, options);
    }

    render() {
        return (<canvas ref="chart" height="400px" />);
    }

}

export default ChartComponent;

答案 1 :(得分:0)

您还可以在React16中使用React.createRef()

class Chart extends Component {
  constructor(props) {
    super(props);

    this.chartRef = React.createRef();
  }

  componentDidMount() {
    const ctx = this.chartRef.current.getContext('2d');
    new ChartJS(ctx, options); // eslint-disable-line no-new
  }

  render() {
    return (
        <canvas ref={this.chartRef} />
      </div>
    );
  }
}

答案 2 :(得分:0)

import React,{Component} from 'react';
import Chart from "chart.js";
​
​
class GraphChart extends Component {
    constructor(props) {
        super(props);
        this.myRef = React.createRef();
​
      }
​
      componentDidMount() {
        const ctx = this.ctx;

        new Chart(ctx, {
          type: "bar",
          data: {
            labels: ["Red", "Blue", "Yellow"],
            datasets: [
              {
                label: "# of Likes",
                data: [12, 19, 3],
                backgroundColor: [
                  "rgba(255, 99, 132, 0.2)",
                  "rgba(54, 162, 235, 0.2)",
                  "rgba(255, 206, 86, 0.2)"
                ]
              },
              {
                label: "# of Likes",
                data: [-12, -19, -3],
                backgroundColor: [
                  "rgba(255, 99, 132, 0.2)",
                  "rgba(54, 162, 235, 0.2)",
                  "rgba(255, 206, 86, 0.2)"
                ]
              }
            ]
          }
        });
      }

      render() {

        return (
            <div>
                <canvas width='800' height='300' ref={ctx => (this.ctx = ctx)}/>
            </div>
        )
    }
}
export default GraphChart;