简体   繁体   English

Highcharts React 饼图多次渲染

[英]Highcharts React Pie Chart rendering multiple time

My code this我的代码这个

import React, { useEffect, useRef, useState } from "react";
import * as Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";

export const PieChart = (props: any) => {
  const [chartIsLoaded, setChartIsLoaded] = useState(false);
  const series: any = [{
    innerSize: '80%',
    name: props.name,
    colorByPoint: true,
    data: props.chartData
  }]
  useEffect(() => {
    setChartIsLoaded(true);
  }, [])

  const chartComponentRef = useRef<HighchartsReact.RefObject>(null);
  const options: Highcharts.Options = {
    chart: {
        backgroundColor: "#0c0c0c",
        borderColor: "#0c0c0c",
        plotBorderWidth: 0,
        plotShadow: false,
        type: 'pie',
        height: "70%",
    },
    title: {
        style : {
            display : 'none'
          }
    },
    tooltip: {
      pointFormat: '<b>{point.percentage:.1f}%</b>',
      backgroundColor: "#1B1B1B",
      borderColor: "transparent",
      valueDecimals: 2,
      borderRadius: 0,
      style: {
        color: "#fff",
        fontSize: "15px"

      }
    },
    accessibility: {
        point: {
            valueSuffix: '%'
        }
    },
    legend: {
        itemStyle: {color: "#fff", fontWeight: "400", fontFamily: "teragon-sans"}
    },
    plotOptions: {
        pie: {
            borderColor: "#0c0c0c",
            borderWidth: 6,
            allowPointSelect: true,
            color: "#fff",
            cursor: 'pointer',
            dataLabels: {
                enabled: false,
            },
            showInLegend: true
        }
    },
    series: series
  };
  return (
    <div>
      {chartIsLoaded &&
        <HighchartsReact
          highcharts={Highcharts}
          options={options}
          ref={chartComponentRef}
          oneToOne={true}
        />
      }
    </div>
  );
};

chartData coming from this code: chartData 来自此代码:

let data = sheetData[0].data;
let invesment = await groupData(data, "Investment Type");

Problem: Chart rendering multiple times.问题:图表渲染多次。 Also, I have Bar Chart it's happening on that.另外,我有条形图,它正在发生。 There is no problem with Line Chart.折线图没有问题。 The data is preparing with reduce function but its async waiting awaits.数据正在准备减少 function 但其异步等待等待。 Moreover I tried with promises.此外,我尝试了承诺。 Unfortunately, It was rendered multiple times again.不幸的是,它又被渲染了多次。 How can I fix this situation?我该如何解决这种情况?

Your chart options are initiated after every component update which directs to the chart update on every component update.您的图表选项在每次组件更新后启动,它指向每次组件更新时的图表更新。 I recommend you to keep chart options in a stare or memorize them.我建议您盯着图表选项或记住它们。 For example:例如:

const PieChart = ({ name, chartData }) => {
  const [chartOptions, setChartOptions] = useState(false);

  useEffect(() => {
    const series = [
      {
        innerSize: "80%",
        name,
        colorByPoint: true,
        data: chartData
      }
    ];
    const options = {
      ...,
      series
    };

    if (chartData) {
      setChartOptions(options);
    }
  }, [chartData]);

  return (
    <div>
      {chartOptions && (
        <HighchartsReact highcharts={Highcharts} options={chartOptions} />
      )}
    </div>
  );
};

Live demo: https://codesandbox.io/s/highcharts-react-demo-fz6rr6?file=/demo.jsx现场演示: https://codesandbox.io/s/highcharts-react-demo-fz6rr6?file=/demo.jsx

Docs: https://www.npmjs.com/package/highcharts-react-official#optimal-way-to-update文档: https://www.npmjs.com/package/highcharts-react-official#optimal-way-to-update

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

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