javascript - How to implement custom events using Google Visualization? -
i have following code below using google's visualization library charts. currently, have selecthandler function returns alert column row.
instead of alert column number, trying implement javascript sends alert of 'key' of item shown below. how this?
<% @frequency.each |key,value| %> ['<%= key %>', <%= value %>], <% end %>
javascript
<script type="text/javascript"> google.load("visualization", "1", {packages:["corechart"]}); google.setonloadcallback(drawchart); function drawchart() { // options var options = { title: 'most common phrases in pro-microsoft reviews (<%= @reviews.count %> reviews analyzed)', vaxis: {title: 'phrases', titletextstyle: {color: 'red'}}, tooltip: {ishtml: true}, animation:{ duration: 2000, easing: 'out', } }; // data var data = google.visualization.arraytodatatable([ ['phrase', 'frequency'], <% @frequency.each |key,value| %> ['<%= key %>', <%= value %>], <% end %> ]); // chart drawing var chart = new google.visualization.barchart(document.getelementbyid('chart_div')); chart.draw(data, options); google.load("visualization", "1", {packages:["corechart"]}); google.setonloadcallback(drawchart); //setup listener google.visualization.events.addlistener(chart, 'select', selecthandler); // select handler. call chart's getselection() method function selecthandler() { var selection = chart.getselection(); alert('that\'s column no. '+selection[0].row); } } </script>
here simple example shows how value of row in column 0 (the 'key' selected item) custom handler using data.getvalue()
:
function drawvisualization() { // create , populate data table. var data = new google.visualization.datatable(); data.addcolumn('string', 'department'); data.addcolumn('number', 'revenues change'); data.addrows([ ['computer', {v: 12, f: '12.0%'}], ['sports', {v: -7.3, f: '-7.3%'}], ['toys', {v: 0, f: '0%'}], ['electronics', {v: -2.1, f: '-2.1%'}], ['food', {v: 22, f: '22.0%'}] ]); // create , draw visualization. var table = new google.visualization.table(document.getelementbyid('visualization')); var formatter = new google.visualization.tablearrowformat(); formatter.format(data, 1); // apply formatter second column table.draw(data, {allowhtml: true, showrownumber: true}); //setup listener google.visualization.events.addlistener(table, 'select', selecthandler); // select handler. call chart's getselection() method function selecthandler() { var selection = table.getselection(); alert('that\'s row '+ data.getvalue(selection[0].row, 0)); } }
you should able same own code changing row:
alert('that\'s column no. '+selection[0].row);
to this:
alert('that\'s row '+ data.getvalue(selection[0].row, 0));
Comments
Post a Comment