sharataka
sharataka

Reputation: 5132

How to implement custom events using Google Visualization?

I have the following code below using Google's Visualization library for charts. Currently, I have a selectHandler function that returns an alert for the column row.

Instead of an alert for the column number, I am trying to implement some Javascript that sends an alert of the 'key' of item shown below. How do I get this?

<% @frequency.each do |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 do |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);

      // The select handler. Call the chart's getSelection() method
       function selectHandler() {
          var selection = chart.getSelection();
          alert('That\'s column no. '+selection[0].row);
      }



  }


</script>

Upvotes: 0

Views: 1263

Answers (1)

jmac
jmac

Reputation: 7128

Here is a simple example that shows how to get the value of that row in column 0 (the 'key' for the selected item) with a custom handler using data.getValue():

function drawVisualization() {
  // Create and populate the 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 and draw the visualization.
  var table = new google.visualization.Table(document.getElementById('visualization'));

  var formatter = new google.visualization.TableArrowFormat();
  formatter.format(data, 1); // Apply formatter to second column

  table.draw(data, {allowHtml: true, showRowNumber: true});

  //setup listener
  google.visualization.events.addListener(table, 'select', selectHandler);

  // The select handler. Call the chart's getSelection() method
  function selectHandler() {
    var selection = table.getSelection();
    alert('That\'s row '+ data.getValue(selection[0].row, 0));
  }

}

You should be able to do the same for your own code just by changing this row:

alert('That\'s column no. '+selection[0].row);

To this:

alert('That\'s row '+ data.getValue(selection[0].row, 0));

Upvotes: 1

Related Questions