Pie charts in reactjs

亡梦爱人 提交于 2019-12-22 14:17:16

问题


I'm trying to implement a pie chart in my web application and i found this as a good source.

https://github.com/reactjs/react-chartjs

But it does not provide the way to give chartData and chartOptions inorder to make it work. How can i do this?

my code

import React, {Component} from 'react';
var LineChart = require("react-chartjs").Line;

class PieChart extends Component {
    constructor() {
        super();
    }

    render() {
        return (
            <div className="">
                <LineChart data={chartData} options={chartOptions} width="600" height="250"/>
            </div>
        );
    }
}
export default PieChart;

I get these errors

12:34  error    'chartData' is not defined     no-undef
12:54  error    'chartOptions' is not defined  no-undef

回答1:


You need to initialise chatData and chartOptions and also react-chartjs has a dependency on chartjs, so you need to install that too

npm install --save chart.js@^1.1.1

Code

import React, {Component} from 'react';
import Chartjs from 'chart.js'
var LineChart = require("react-chartjs").Line;

class PieChart extends Component {
    constructor() {
        super();
    }

    render() {
       var chartOptions: {


     // Boolean - If we should show the scale at all


    showScale: true,
    // Boolean - Whether to show a dot for each point
    pointDot: true,
    showLines: false,
    title: {
        display: true,
        text: 'Chart.js Line Chart'
    },
    legend: {
        display: true,
        labels: {
           boxWidth: 50,
           fontSize: 10,
           fontColor: '#bbb',
           padding: 5,

        }
    }
    }

    var chartData= {
        labels: ['1', '2', '3', '4'],
        datasets: [
            {
                label: 'Current lag',
                fill: false,
                lineTension: 0.1,
                backgroundColor: "rgba(75,192,192,0.4)",
                borderColor: "rgba(75,192,192,1)",
                borderCapStyle: 'butt',
                borderDashOffset: 0.0,
                borderJoinStyle: 'miter',
                data: [50, 35, 60, 67]
            }
        ]
    }


        return (
            <div className="">
                <LineChart data={chartData} options={chartOptions} width="600" height="250"/>
            </div>
        );
    }
}
export default PieChart;



回答2:


I use chart.js in my project and it works really well. You need install chart.js module via npm

npm install chart.js --save

Then import it to your component file

import Chart from 'chart.js'

And it provides 'data' field in its function for you to populate data.

let myDoughnutChart = new Chart(document.getElementById('my_chart'), {
  type: 'pie',
  data: {
    datasets: [{
      data: [percentage, 100 - percentage],
      backgroundColor: ['rgba(82,199,197,1)', '#E5E5E5'],
      borderWidth: 1
    }]
  },
  options: {
    tooltips: {
      enabled: false
    }
  }
});

For more details please check the their doc (http://www.chartjs.org/docs/)



来源:https://stackoverflow.com/questions/42709925/pie-charts-in-reactjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!