简体   繁体   English

为 chart.js 饼图创建一个道具

[英]Create a prop for a chart.js pie chart

I'm using Chart.js inside my react project.我在我的反应项目中使用 Chart.js 。 and i want to structure my project as neatly as possible by creating components and calling them on my App.js我想通过创建组件并在我的 App.js 上调用它们来尽可能简洁地构建我的项目

How can I go about creating a react prop and calling it from another component without Actually calling my pie chart from the App.js我怎么能 go 关于创建一个反应道具并从另一个组件调用它而不实际从 App.js 调用我的饼图

import React from 'react';
import {Pie} from 'react-chartjs-2';

const state = {
  labels: ['January', 'February', 'March',
           'April', 'May'],
  datasets: [
    {
      label: 'Rainfall',
      backgroundColor: [
        '#B21F00',
        '#C9DE00',
        '#2FDE00',
        '#00A6B4',
        '#6800B4'
      ],
      hoverBackgroundColor: [
      '#501800',
      '#4B5000',
      '#175000',
      '#003350',
      '#35014F'
      ],
      data: [65, 59, 80, 81, 56]
    }
  ]
}

export default class App extends React.Component {
  render() {
    return (
      <div>
        <Pie
          data={state}
              options={{
              title:{
              display:true,
              text:'Average Rainfall per month',
              fontSize:20
            },
              legend:{
              display:true,
              position:'left',
             }
          }}
        />
      </div>
    );
  }
}

If you create a component named PieChart in the PieChart.js file like this:如果您在PieChart.js文件中创建一个名为PieChart的组件,如下所示:

import React from 'react';
import { Pie } from 'react-chartjs-2';

export default class PieChart extends React.Component {
    render() {
        return (
            <div>
                <Pie
                    data={this.props.data}
                    options={{
                        title: {
                            display: true,
                            text: 'Average Rainfall per month',
                            fontSize: 20
                        },
                        legend: {
                            display: true,
                            position: 'left',
                        }
                    }}
                />
            </div>
        );
    }
}

Then you can import it from the App.js in this way:然后你可以通过这种方式从App.js中导入它:

import React from 'react';

import PieChart from './PieChart';

const state = {
  labels: ['January', 'February', 'March',
    'April', 'May'],
  datasets: [
    {
      label: 'Rainfall',
      backgroundColor: [
        '#B21F00',
        '#C9DE00',
        '#2FDE00',
        '#00A6B4',
        '#6800B4'
      ],
      hoverBackgroundColor: [
        '#501800',
        '#4B5000',
        '#175000',
        '#003350',
        '#35014F'
      ],
      data: [65, 59, 80, 81, 56]
    }
  ]
}

export default class App extends React.Component {
  render() {
    return (
      <PieChart data={state} />
    )
  }
}

Here I've created a custom props named data and passed it to the PieChart component which is being accessed by this.props.data in the PieChart component.在这里,我创建了一个名为data的自定义props ,并将其传递给PieChart组件,该组件由this.props.data PieChart You can create multiple props as you want.您可以根据需要创建多个道具。 Like you can pass options as well in this way.就像您也可以通过这种方式传递options

Also you can keep your data(which is passed from the App as data props) in the PieChart component and call it from anywhere you want by <PieChart /> .您还可以将数据(从App作为data道具传递)保留在PieChart组件中,并通过<PieChart />从您想要的任何地方调用它

Note: I've kept App.js and PieChart.js both files in the same directory.注意:我将App.jsPieChart.js两个文件保存在同一个目录中。

This how I used piechart in my project: in another file, you can create a custom component and add your code like below这就是我在项目中使用饼图的方式:在另一个文件中,您可以创建一个自定义组件并添加您的代码,如下所示

export const StatusChart = ({ data }) => {
  const [pieData, setPieData] = useState({});

  useEffect(() => {
    setPieData({
      labels: ['declined', 'accepted', 'goterror', 'inprogress'],
      datasets: [
        {
          data: [data.inprogress, data.got_error, data.accepted, data.declined],
          backgroundColor: ['#F7464A', '#46BFBD', '#FDB45C', '#949FB1', '#4D5360'],
          hoverBackgroundColor: ['#FF5A5E', '#5AD3D1', '#FFC870', '#A8B3C5', '#616774'],
        },
      ],
    });
  }, [data]);

  return (
    <MDBContainer>
      <Pie data={pieData} options={{ responsive: true }} />
    </MDBContainer>
  );
};

and in App.js you can use useState to save data like below:在 App.js 中,您可以使用 useState 来保存数据,如下所示:

  const [generalData, setGeneralData] = useState(null);

and use it in your code:并在您的代码中使用它:

{generalData ? <StatusChart data={generalData.status_chart} /> : <Spinner animation="border" />}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM