Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change name of chart #36

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

Change name of chart #36

novito opened this issue Jun 6, 2012 · 5 comments
Labels

Comments

@novito
Copy link

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?

@winston
Copy link
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!

@letstalk
Copy link

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.

@kandadaboggu
Copy link
Contributor

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

@letstalk
Copy link

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

@winston
Copy link
Owner

winston commented Oct 30, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants