Search code examples
javascriptchartsgoogle-visualizationdom-eventszooming

Google chart line with zoom and select event


I built a line chart that fire an alert when the points are clicked, that works fine.

The problem is when I add the 'explorer' option (commented line, below) to enable the scroll zoom on the chart: the select event doesn't fire and the click doesn't work anymore (fiddle)...

    options = {
      legend: 'none',
      format: 'none',
      hAxis: { textPosition: 'none', gridlines: { count: 0 } },
      vAxis: { textPosition: 'none', gridlines: { count: 1 } }, 
      curveType: 'function',
      pointSize: 20,
      
     
    };

    chart = new google.visualization.LineChart(document.getElementById('chart_div'));        
   
   //If I enable this line, ZOOM works fine but the 'select' event don't work....
   //options['explorer'] = {axis: 'horizontal',keepInBounds: true,maxZoomIn: 5.0};        
   
   chart.draw(data_estruturas, options);
   
    //select event
    google.visualization.events.addListener(chart, 'select', function(e) {
      var selection = chart.getSelection();     
      if (selection.length > 0) {
        var estrutura = data_estruturas.getValue(selection[0].row, 0)
        alert(estrutura);
      }
    });


}        
   
 

Please check this fiddle


Solution

  • Put the draw method after register the select event.

    //select event
    google.visualization.events.addListener(chart, 'select', function(e) {
      var selection = chart.getSelection();     
      if (selection.length > 0) {
        var estrutura = data_estruturas.getValue(selection[0].row, 0)
        alert(estrutura);
      }
    });
    
    //After set all options and register events draw the chart
    chart.draw(data_estruturas, options);
    

    I updated your fiddle