最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select event not fired on Google chart API for Sankey - Stack Overflow

programmeradmin0浏览0评论

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=''</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>

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./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>
Share Improve this question edited Aug 26, 2019 at 10:49 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked May 19, 2015 at 22:45 irwinjirwinj 1138 bronze badges 2
  • 1 Appears I'm not the first to ask this (some time ago) but still it's broken: stackoverflow./questions/24552612/… – irwinj Commented May 19, 2015 at 23:03
  • Appears assigned as a known issue github./google/google-visualization-issues/issues/1472 – irwinj Commented May 20, 2015 at 7:50
Add a ment  | 

3 Answers 3

Reset to default 8

You have to set correct options:

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

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

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

I think the issue here is that you are using the getSelection method for the Sankey chart event which returns an object. That object contains the node in the chart that you clicked on but you need to query the object in the right way to extract this information. I don't think it is possible to do things in a single line as attempted in the question above.

This is what worked for me when I had the same problem:

google.visualization.events.addListener(chart, 'select', function(){
var selection = chart.getSelection();
for(var key in selection) {
        var value = selection[key];
    console.log(value.name);    
    }   
alert('you selected '+value.name);
});

The top answer did not work for me. However, setting link interactivity to true did the trick.

var options = {
 sankey: {
   node: {
    interactivity: true,
    colors: this.colors
   },
   link: {
    interactivity: true
   }
  }
}
发布评论

评论列表(0)

  1. 暂无评论