Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add tooltips to a Google Line Chart with multiple data series - with simplified test case and screenshot

I have a Google Line Chart with 2 data series - Row A and Row B:

enter image description here

Here is the very simple test code - just open it in the browser and it will work:

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['corechart']}]}"></script>
<script type="text/javascript">

        var data = {"rows":[
        {"c":[{"v":"C"},{"v":-43},{"v":-42}]},
        {"c":[{"v":"D"},{"v":-49},{"v":-39}]},
        {"c":[{"v":"E"},{"v":-49},{"v":-48}]},
        {"c":[{"v":"F"},{"v":-50},{"v":-49}]},
        {"c":[{"v":"G"},{"v":-57},{"v":-56}]}],

        "cols":[
        {"p":{"role":"domain"},"label":"MEASUREMENT","type":"string"},
        {"p":{"role":"data"},"label":"Row A","type":"number"},
        {"p":{"role":"data"},"label":"Row B","type":"number"}]};

        function drawCharts() {
            var x = new google.visualization.DataTable(data);

            var options = {
                title: 'How to add tooltips?',
                width: 800,
                height: 600
            };

            var chart = new google.visualization.LineChart(document.getElementById('test'));
            chart.draw(x, options);
        }

        $(function() {
            google.setOnLoadCallback(drawCharts);
        });

</script>
</head>
<body>
<div id="test"></div>
</body>
</html>

I would like to add tooltips to each data point, which would for example display:

Row A: x=D y=-49

on mouse hover. And I can not use dataTable.addColumn, because my chart is generated at once by a perl script and I just use a data Object with cols and rows as above.

Does anybody please know, how to do it here?

like image 398
Alexander Farber Avatar asked Jul 29 '13 13:07

Alexander Farber


People also ask

How do I add a tooltip to a Google chart?

Specifying the tooltip typeisHtml: true in the chart options passed to the draw() call, which will also allow you to create Tooltip Actions.

How do I add a chart in tooltips?

To achieve this, we first add a table within the tooltip. The first column contains the categories ("women", "men"), and the second one contains the bars. In this second column, we then add HTML <div> tags and define the width of these boxes with our numerical columns.

How do I edit tooltip in Google Data Studio?

To add tooltip annotations to your own dashboard, start by entering Edit mode in Google Data Studio. Next, go to Community Visualizations → Explore more → Build your own visualisation.


1 Answers

You can use a DataView to create the tooltip columns for you. This code snippet will dynamically create a tooltip column in the DataView for every data series:

var columns = [0];
for (var i = 1; i < x.getNumberOfColumns(); i++) {
    columns.push(i);
    columns.push({
        type: 'string',
        properties: {
            role: 'tooltip'
        },
        calc: (function (j) {
            return function (dt, row) {
                return dt.getColumnLabel(j) + ': x=' + dt.getValue(row, 0) + ' y=' + dt.getValue(row, j)
            }
        })(i)
    });
}
var view = new google.visualization.DataView(x);
view.setColumns(columns);

See the working example here: http://jsfiddle.net/asgallant/xWwxP/

like image 117
asgallant Avatar answered Sep 27 '22 01:09

asgallant