react-chartjs-2未捕获的TypeError:无法读取未定义的属性'getBoundingClientRect'

时间:2019-02-04 23:44:42

标签: javascript react-chartjs

我正在使用react-chartjs-2 2.7.4

我有一个带有REF的图表:

<Doughnut data={directoryData}
          ref={component.directoryDataRef}

           onElementsClick={(evt, evt2) => {
                console.log("Dataset at event",
                component.directoryDataRef.current.chartInstance.getDatasetAtEvent(evt))
                           }}
 />

这引发异常:

core.helpers.js:415 Uncaught TypeError: Cannot read property 'getBoundingClientRect' of undefined
    at Object.helpers.getRelativePosition (core.helpers.js:415)
    at getRelativePosition (core.interaction.js:20)
    at Object.dataset (core.interaction.js:199)
    at Chart.getDatasetAtEvent (core.controller.js:665)
    at onElementsClick (App.js:274)
    at ChartComponent._this.handleOnClick (index.js:87)

如何获取被单击的饼图的一部分?

这是一个简单的饼图:

{
  "labels": [
    "2018 asdfasdf",
    "asdf",
    "dfdasdf",
    "2018 Rasdfasdf",
    "asdfasdf",
    "linode"
  ],
  "datasets": [
    {
      "data": [
        1721508,
        911213,
        215171,
        92643,
        86494,
        2276
      ],
      "backgroundColor": [
        "#332288",
        "#88ccee",
        "#44aa99",
        "#117733",
        "#999933",
        "#ddcc77",
        "#cc6677",
        "#aa4499",
        "#b58900",
        "#cb4b16",
        "#dc322f",
        "#d33682",
        "#6c71c4",
        "#268bd2",
        "#2aa198",
        "#859900"
      ]
    }
  ]
}

0 个答案:

没有答案