21

I have got line graph that shows value change in time. It works but I thought it would be great if I could add points that shows tooltip on hover. Something like this: enter image description here But I cannot use tooltip directly on one of those points.

var data = google.visualization.arrayToDataTable([
    ['time', 'value'],
    ['12:00',   1],
    ['13:00',   5],
    ['14:00',   8],
    ['15:00',   12],
    ['16:00',   11],
    ['17:00',   15],

]);

new google.visualization.LineChart(document.getElementById('visualization')).
  draw(data, {});
2
  • Have you looked at annotations? Or does it need to be a tooltip? And what do you mean you can't use a tooltip directly?
    – jmac
    Commented Jul 25, 2013 at 0:12
  • Yup, annotations is what I need. don't know why I haven't found it before. Please add answer so I could give you points :)
    – Andy
    Commented Jul 25, 2013 at 10:50

2 Answers 2

21

As stated in my comment you can use the annotation role to do this.

Your original code:

var data = google.visualization.arrayToDataTable([
    ['time', 'value'],
    ['12:00',   1],
    ['13:00',   5],
    ['14:00',   8],
    ['15:00',   12],
    ['16:00',   11],
    ['17:00',   15],

]);

new google.visualization.LineChart(document.getElementById('visualization')).
  draw(data, {});

You need to add two columns -- one for the annotation marker, one for the annotation text. Assuming you want two comments at 14:00 and 16:00 for the sake of example:

function drawVisualization() {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'time');
  data.addColumn('number', 'value');
  data.addColumn({type: 'string', role:'annotation'});
  data.addColumn({type: 'string', role:'annotationText'});
  data.addRows([
    ['12:00',   1, null, null],
    ['13:00',   5, null, null],
    ['14:00',   8, 'A', 'This is Point A'],
    ['15:00',   12, null, null],
    ['16:00',   11, 'B', 'This is Point B'],
    ['17:00',   15, null, null],
  ]);

  new google.visualization.LineChart(document.getElementById('visualization')).
    draw(data, {});
}

This is the result:

Sample Annotation

To add asgallant's solution for adding points to the chart, you can do as follows:

function drawVisualization() {
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'time');
  data.addColumn('number', 'value');
  data.addColumn('number', 'points');
  data.addColumn({type: 'string', role:'annotation'});
  data.addColumn({type: 'string', role:'annotationText'});
  data.addRows([
    ['12:00',   1, null, null, null],
    ['13:00',   5, null, null, null],
    ['14:00',   8, 8, 'A', 'This is Point A'],
    ['15:00',   12, null, null, null],
    ['16:00',   11, 11, 'B', 'This is Point B'],
    ['17:00',   15, null, null, null],
  ]);

  new google.visualization.LineChart(document.getElementById('visualization')).
    draw(data, {
      series: {
        0: {
          // set any applicable options on the first series
        },
        1: {
          // set the options on the second series
          lineWidth: 0,
          pointSize: 5,
          visibleInLegend: false
        }
      }
    });
}

Here is the result of that:

asgallant sample

0
17

If I read your question correctly, you want points to appear on the line for each data point, and hovering over these points should spawn a tooltip. If that is what you are after, the chart already does both of those things, you just can't see the points because by default they have a size of 0. Set the "pointSize" option in your LineChart to make the points larger:

new google.visualization.LineChart(document.getElementById('visualization')).
    draw(data, {
        pointSize: 5
    });

Edit:

To emphasize only some of the points in the chart, you need to add a new data series that contains only those values (you can either add this series directly to the DataTable, or create it on the fly with a DataView if you can somehow distinguish the points you want to emphasize from the others). Then you want to set the chart's series option to hide the line in the second series, remove the second series from the legend, and make its points larger (you can also set its color here if you want to match colors), like this:

series: {
    0: {
        // set any applicable options on the first series
    },
    1: {
        // set the options on the second series
        lineWidth: 0,
        pointSize: 5,
        visibleInLegend: false
    }
} 
4
  • Yeah, but the problem is I have got many, many points. With bigger pointSize graph is unreadable, I wanted to mark only some specific points with bigger circle on the graph.
    – Andy
    Commented Jul 25, 2013 at 10:52
  • Ok. The way to handle this is to add a second series of data to the chart that contains only the data points that you want to emphasize like this (they should also be present in the base data series). Set the chart's series option to turn off the line in the second series, make the points larger, and remove it from the legend (you can also the set color of the series here if you want to match colors). See edit to answer above.
    – asgallant
    Commented Jul 25, 2013 at 14:53
  • 1
    I am using material line chart. Setting the pointSize has no effect.
    – Ajoy
    Commented Mar 30, 2015 at 8:23
  • Exactly what I was looking for
    – Mordechai
    Commented Mar 12, 2019 at 17:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.