Mayank Saxena
Mayank Saxena

Reputation: 152

How to detect double click event on google chart (pie chart)

How do I detect a double click event on google charts? Here is my code:

    var options = {
      title: 'My Daily Activities'
    };

    var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
    console.log(chart);
    chart.is3D = true;
    chart.draw(data, options);
    google.visualization.events.addListener(chart, 'click', function(e){
        console.log(e);
        console.log(chart.getSelection());
        var data = chart.getSelection();
        if(data.length > 0) {
            alert(0);
            chart.setSelection([]);
        }
        //window.open("http://classicmayank.limewebs.com", "mywindow1", "status=1,width=350,height=150");
    });
  }

Upvotes: 4

Views: 4836

Answers (5)

jsa
jsa

Reputation: 399

I did this by listening to the double click event of chart_div (the container).

conatiner = document.getElementById('chart_div');

container.addEventListener('dblclick', (e)=>console.log('double clicked'));

Upvotes: 0

Shaheed ulHaq
Shaheed ulHaq

Reputation: 516

I did the following

    var clicks = 0;

    function selectHandler() {

      clicks++;

      setTimeout(function() {
          if (clicks >= 2) {
              alert("double click");

          }
          clicks = 0;
      }, 250);



    }

    //bind the select event with chart
    google.visualization.events.addListener(chart, 'select', selectHandler);

Upvotes: 1

Martin
Martin

Reputation: 141

I know it is an old question but I have tried a little hack how to detect a double-click. Just by measuring it. It is for sure not a nice or robust solution but it seems to work.

var firstClick = 0;
var secondClick = 0;

google.visualization.events.addListener(chart, 'click', function () {
    var date = new Date();
    var millis = date.getTime();

    if (millis - secondClick > 1000) {        
        // add delayed check if a single click occured
        setTimeout(function() {
            // no second click fast enough, it is a single click
            if (secondClick == 0) {
                alert("click");
            }
        }, 250);
    }

    // try to measure if double-clicked
    if (millis - firstClick < 250) {        
        firstClick = 0;
        secondClick = millis;

        alert("doubleClick");

    } else {
        firstClick = millis;
        secondClick = 0;
    }
});    

JSFiddle to it is here.

Upvotes: 4

Eric Dunham
Eric Dunham

Reputation: 1

Its a little late i guess but use something along the lines of

   google.visualization.events.addListener(chart, 'select', columnselectHandler)
   var status = 0;

   function columnselectHandler(e) {
            var selection = chart.getSelection();
            var selectedItem = selection[0];
            var data2 = getDDData(k);
            var columndata = [] 
      if (selectedItem) {
    if (status == 0){
    var name = data.getValue(selectedItem.row, 0);
    columndata[0] =  data2[0] 
    columndata[1] =  data2[selectedItem.row+1]                          
              var chartData = new google.visualization.arrayToDataTable(columndata);
        chart.draw(chartData, {width: 1200, height:500}, options);
        var button = document.getElementsByClassName("backToMain")[0];        
          button.setAttribute("id",chartType)
         button.setAttribute("style","display:inline; margin-top:150px; font-size:30px; position:absolute;")` 

Upvotes: 0

Răzvan Ciocănel
Răzvan Ciocănel

Reputation: 446

Jquery provides a double click event that you can use. As you can see in the example here:

$("p").dblclick( function () { alert("Hello World!"); });

The jQuery page where you can find more it's here: http://api.jquery.com/dblclick/

Upvotes: -3

Related Questions