Google Charts: One Tooltip for Entire Column

Andrew picture Andrew · Mar 12, 2013 · Viewed 7k times · Source

I have a basic Area Chart using Google Charts. I am able to set up tooltips for each point on the graph, but is there a way to have a single tooltip for all the points in a column.

Here's a screenshot of the desired behavior:

One tooltip for entire column as per Highcharts

You can see that when the cursor is over a point on the graph, a vertical line is drawn and one tooltip displays to describe all the data in that column (data for both lines). See a live example here if needed.

Can this be achieved with Google Charts?

Answer

jmac picture jmac · Mar 15, 2013

All you need to do is add the following to your options (in the case of a line chart): focusTarget: 'category'

Here is an example (just open Google Playground and add the above line to the options at the end):

function drawVisualization() {
  // Create and populate the data table.
  var data = google.visualization.arrayToDataTable([
    ['x', 'Cats', 'Blanket 1', 'Blanket 2'],
    ['A',   1,       1,           0.5],
    ['B',   2,       0.5,         1],
    ['C',   4,       1,           0.5],
    ['D',   8,       0.5,         1],
    ['E',   7,       1,           0.5],
    ['F',   7,       0.5,         1],
    ['G',   8,       1,           0.5],
    ['H',   4,       0.5,         1],
    ['I',   2,       1,           0.5],
    ['J',   3.5,     0.5,         1],
    ['K',   3,       1,           0.5],
    ['L',   3.5,     0.5,         1],
    ['M',   1,       1,           0.5],
    ['N',   1,       0.5,         1]
  ]);

  // Create and draw the visualization.
  new google.visualization.LineChart(document.getElementById('visualization')).
      draw(data, {curveType: "function",
                  width: 500, height: 400,
                  vAxis: {maxValue: 10},
  // This line will make you select an entire row of data at a time
                  focusTarget: 'category'
                 }
          );
}

Easy as pie! For more details, see focusTarget in the Google Documentation

If you want something more complex, you can fiddle around with the domain Data Role

Here is a sample bit of code:

 google.load('visualization', '1.1', {'packages':['corechart']});

 google.setOnLoadCallback(drawChart_C6);

  function drawChart_C6() {
    var data = new google.visualization.DataTable();
    data.addColumn({type: 'string', role: 'domain'}, '2009 Quarter');
    data.addColumn('number', '2009 Sales');
    data.addColumn('number', '2009 Expenses');
    data.addColumn({type: 'string', role: 'domain'}, '2008 Quarter');
    data.addColumn('number', '2008 Sales');
    data.addColumn('number', '2008 Expenses');
    data.addRows([
      ['Q1 \'09', 1000, 400, 'Q1 \'08', 800, 300],
      ['Q2 \'09', 1170, 460, 'Q2 \'08', 750, 400],
      ['Q3 \'09', 660, 1120, 'Q3 \'08', 700, 540],
      ['Q4 \'09', 1030, 540, 'Q4 \'08', 820, 620]
    ]);

    var chart = new google.visualization.LineChart(document.getElementById('chart_C6'));
    chart.draw(data, {width: 400, height: 240, legend:'right', focusTarget: 'category'});
  }