reactjschartsgoogle-visualizationreact-google-charts

How can I getSelection value on the TreeMap in reactjs react-google-charts


i want to get the value for the node which is selected by the user in graph. I have tried with this solution but not getting the values.

const ChartEvents = [
  { eventName: "select",callback({ chartWrapper }) {console.log("Selected ", chartWrapper.getChart().getSelection());}];


<Chart chartType="TreeMap" data={data} graphID="TreeMap" options={options} width="100%" height="400px" chartEvents={ChartEvents} />

codesandbox code


Solution

  • you need to use the 'ready' event on the chartwrapper,
    then assign the 'select' event on the chart,
    as follows...

    const ChartEvents = [
      {
        eventName: "ready",
        callback: ({ chartWrapper, google }) => {
          const chart = chartWrapper.getChart();
          const data = chartWrapper.getDataTable();
          google.visualization.events.addListener(chart, "select", function () {
            var selection = chart.getSelection();
            console.log("Selected ", selection);
            if (selection.length > 0) {
              console.log("Market trade volume (size) ", data.getValue(selection[0].row, 2));
              console.log("Market increase/decrease (color) ", data.getValue(selection[0].row, 3));
            }
          });
        }    
      }
    ];