【问题标题】:Highcharts doesnot reload or re-render chart on value changeHighcharts 不会在值更改时重新加载或重新渲染图表
【发布时间】:2021-04-20 06:23:35
【问题描述】:

我在 highcharts 中创建了圆环图,其中数据值在选择下拉列表时发生变化。

Codesandbox code

从下拉列表中选择时,数据会发生变化,但图表不会重新呈现,更新后的数据也不会显示在图表中。任何数据的图表都保持不变。

我尝试包含 oneToOne={true}。但这也无济于事。

有一些类似的问题,但它们对我的情况没有帮助。

如果有人有任何想法或需要任何进一步的信息,请告诉我。

【问题讨论】:

    标签: reactjs highcharts react-hooks


    【解决方案1】:

    一种解决方案是使用useEffect钩子并在useEffect依赖中传递details,所以每次细节都会改变他的状态,useEffect将被触发并且你的dataValues将被更新:

    import React, { useState, useEffect } from "react";
    import { render } from "react-dom";
    // Import Highcharts
    import Highcharts from "highcharts/highstock";
    import PieChart from "highcharts-react-official";
    
    const App = () => {
      const [details, setDetails] = useState("sup");
      let dataValue = [
        ["Firefox", 45.0],
        ["IE", 26.8],
        ["Chrome", 12.8],
        ["Safari", 8.5],
        ["Opera", 6.2],
        ["Others", 0.7]
      ]; //pass default "sup" to datavalue
      const [chartOptions, setChartOptions] = useState({
        chart: {
          type: "pie"
        },
        title: {
          text: ""
        },
        plotOptions: {
          pie: {
            size: "50%",
            allowPointSelect: true,
            cursor: "pointer",
            depth: 35,
            innerSize: 100,
            dataLabels: {
              enabled: false
            },
            showInLegend: true
          }
        },
        series: [
          {
            type: "pie",
            name: "Browser share",
            data: dataValue
          }
        ]
      });
      useEffect(() => {
        console.log(details);
        if (details === "sup") {
          dataValue = [
            ["Firefox", 45.0],
            ["IE", 26.8],
            ["Chrome", 12.8],
            ["Safari", 8.5],
            ["Opera", 6.2],
            ["Others", 0.7]
          ];
        } else if (details === "hola") {
          dataValue = [
            ["Apple", 35],
            ["Guava", 40],
            ["Grapes", 22.5]
          ];
        }
        setChartOptions((prev) => {
          return {
            ...prev,
            series: [
              {
                type: "pie",
                name: "Browser share",
                data: dataValue
              }
            ]
          };
        });
      }, [details]);
    
      async function newfetchVendor(e) {
        if (!!e) {
          setDetails(e.target.value);
        }
      }
    
      return (
        <div>
          <select onChange={newfetchVendor}>
            <option value="sup">sup</option>
            <option value="hola">hola</option>
          </select>
          <PieChart
            highcharts={Highcharts}
            options={chartOptions}
            oneToOne={true}
          />
        </div>
      );
    };
    
    render(<App />, document.getElementById("root"));
    
    
    

    【讨论】:

      猜你喜欢
      • 2018-11-29
      • 2017-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2021-11-19
      相关资源
      最近更新 更多