Change name of chart #36

Closed
novito opened this Issue Jun 6, 2012 · 5 comments

Comments

Projects
None yet
4 participants

novito commented Jun 6, 2012

I see that the JS generated is like this:

var chart = new google.visualization.#{class_name}(document.getElementById('#{element_id}'));"

This means that if you generate several charts in the same page, it would be impossible to fetch those objects individually (imagine if you want to add an event to each of the charts?).

It might be a good idea to make the name unique, maybe let the user name it when creating the chart?

Owner

winston commented Oct 15, 2012

There's a pull request to add listeners to a chart as you create them. Can that solve your problem?

Thank you!

Hm. How do I get the object that triggered the event? I mean inside the listener code?

I.e. how to get the example code working:

var selectedItem = chart.getSelection()[0];
if (selectedItem) {
var value = data.getValue(selectedItem.row, selectedItem.column);
console.log('The user selected ' + value);
}

'chart ' is not recognized here.

Contributor

kandadaboggu commented Oct 29, 2012

Your callback code has access to chart and data_table variables. These variables are created within the context of a specific chart. So you should be able to safely use them without worrying about name conflicts. This is how I handle such scenarios in my code:

# rails code.  You have access to data_table and chart variables.
chart.add_listener("select", "function(e) {pieUrlLauncher(e, chart, data_table, '#{url_template}'); }")

// JS code      
function pieUrlLauncher(e, chart, data, url_template) {
  var selection = chart.getSelection();
  if (selection.length > 0) {
    var row = selection[0].row;
    var department = data.getValue(row, 0);
    window.open(url_template + department, '_self');
  }
}

PS: You need to make sure you have the event registration patch

Thanks man, that worked for me, too!
This example should be in the documentation.

Owner

winston commented Oct 30, 2012

Good call. I'll tag this and add it to the doc when I have the time. Thanks!

@winston winston closed this Aug 22, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment