reactjs react-google-charts点击事件不工作

iezvtpos  于 2023-02-08  发布在  React
关注(0)|答案(3)|浏览(156)

根据文档,您可以Set the chart-specific events you want to listen to and the corresponding callback.给出的示例使用select,它工作正常(我已经设置了一个示例here。当我尝试使用 * 任何 * 其他图表类型时,问题出现了。
根据google charts文档,对于bar chart,我应该能够使用click事件:

当我像这样添加一个click事件时:

{
    eventName: "click",
    callback({}) {
      console.log("clicked");
    }
  }

什么都没有发生,但是select事件仍然有效。我已经设置了一个代码沙箱here来演示这个行为。animationfinishonmouseover和我检查过的其他事件也是如此。

2jcobegt

2jcobegt1#

看起来rakannimer已经在GitHub repository上的#263中回答了这个问题,但是我想我还是要回答这个问题,以防其他人遇到这个问题。
正如this stack overflow answer所解释的那样,ready事件必须在图表事件(如屏幕截图中的事件)被触发之前被触发,因此,如果你想使用任何其他事件,你必须在回调中启动它们,如下所示:

<Chart
  chartType="ScatterChart"
  width="80%"
  height="400px"
  data={data}
  options={options}
  legendToggle
  chartEvents={[
    {
      eventName: "ready",
      callback: ({ chartWrapper, google }) => {
        const chart = chartWrapper.getChart();
        google.visualization.events.addListener(chart, "onmouseover", e => {
          const { row, column } = e;
          console.warn("MOUSE OVER ", { row, column });
        });
        google.visualization.events.addListener(chart, "onmouseout", e => {
          const { row, column } = e;
          console.warn("MOUSE OUT ", { row, column });
        });
      }
    }
  ]}
/>
dgsult0t

dgsult0t2#

请检查以下代码片段以添加单击事件:

import * as React from "react";
import { Chart } from "react-google-charts";

const chartEvents = [
      {
        callback: ({ chartWrapper, google }) => {
          const chart = chartWrapper.getChart();
          chart.container.addEventListener("click", (ev) => console.log(ev))
        },
        eventName: "ready"
      }
    ];

const data = [
  ["age", "weight"],
  [8, 12],
  [4, 5.5],
  [11, 14],
  [4, 5],
  [3, 3.5],
  [6.5, 7]
];

const options = {
  title: "Age vs. Weight comparison",
  hAxis: { title: "Age", viewWindow: { min: 0, max: 15 } },
  vAxis: { title: "Weight", viewWindow: { min: 0, max: 15 } },
  legend: "none"
};
const ExampleChart = () => {
  return (
    <Chart
      chartType="ScatterChart"
      data={data}
      options={options}
      graphID="ScatterChart"
      width="100%"
      height="400px"
      chartEvents={chartEvents}
    />
  );
};

export default ExampleChart;
dgtucam1

dgtucam13#

除了@jake提供的解决方案之外,chartWrapper在回调事件中不再可用,在我的例子中,用wrapper替换它是有效的。

<Chart
  chartType="ScatterChart"
  width="80%"
  height="400px"
  data={data}
  options={options}
  legendToggle
  chartEvents={[
    {
      eventName: "ready",
      callback: ({ wrapper, google }) => {
        const chart = wrapper.getChart();
        google.visualization.events.addListener(chart, "onmouseover", e => {
          const { row, column } = e;
          console.warn("MOUSE OVER ", { row, column });
        });
        google.visualization.events.addListener(chart, "onmouseout", e => {
          const { row, column } = e;
          console.warn("MOUSE OUT ", { row, column });
        });
      }
    }
  ]}
/>```

相关问题