Search code examples
reactjsgoogle-visualization

react-google-charts click event not working


According to the documentation, you can Set the chart-specific events you want to listen to and the corresponding callback. The example is given uses select which works fine (I've setup an example here. The problem comes when I try to use any other chart type.

From the google charts documentation, for a bar chart, I should be able to use a click event: Screen Shot 2019-04-19 at 10 30 25 AM

When I add a click event like so:

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

Nothing happens, but the select event still works. I've setup a code sandbox here to demonstrate this behavior. This also happens for animationfinish, onmouseover, and every other event I've checked.


Solution

  • Looks like rakannimer already answered this in #263 on the GitHub repository, but figured I'd answer this anyway in case anyone else was having this issue.

    As this stack overflow answer does a great job of explaining, the ready event must be fired before chart events (like those in the screenshot) can be triggered. Therefore, if you want to use any other event, you have to initiate them in a callback like this:

    <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 });
            });
          }
        }
      ]}
    />