2019-06-13
Select event not fired on Google chart API for Sankey
stackoverflow
Question

I'm trying to hook an event to a click on a Google Sankey diagram. The Events claim to include select but it doesn't fire on Chrome or Safari. onmouseover/onmouseout/ready seem to be hooked up if the event is switched in the code below -- I get something in the console log. That is switching the line:

google.visualization.events.addListener(chart, 'select', selectHandler);

to

google.visualization.events.addListener(chart, 'onmouseover', selectHandler);

shows the event listener is connected.

I've tried this on other newer chart types like word-trees and select is connected. What have I missed?

E.g.

<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'</script>   
<script type='text/javascript'>

google.load('visualization', '1.1', {packages:['sankey']});
google.setOnLoadCallback(drawChart);      
function drawChart() {
    var data = new google.visualization.DataTable();
        data.addColumn('string', 'A');
        data.addColumn('string', 'B');
        data.addColumn('number', 'Mails');
        data.addRows([
            ['from elvis','frank', 285],
            ['frank', 'to wendy', 61],
        ]);
   var options = {width: 600};
   var chart = new google.visualization.Sankey(document.getElementById('thechart'));        

google.visualization.events.addListener(chart, 'select', selectHandler);

function selectHandler() {
    console.log('You selected ' + JSON.stringify(chart.getSelection()));
};

chart.draw(data, options);  
} 
</script>
</head>
<body>
<div id='thechart' style='width: 600px; height: 300px;'></div>  
</body>
</html>
Answer
1

You have to set correct options:

 // Sets chart options.
 var options = {
        width: 600,
        sankey: {
            node: {
                interactivity: true
            }
        }
    };

I used this example https://jsfiddle.net/5mvx6bdr/ Works perfect :)

sankey: node: interactivity is set to false by default.

Select event not fired on Google chart API for Sankey
See more ...