Priya
Priya

Reputation: 1481

Get tooltip data of column chart in google charts

I am using following code to plot column chart using google visualization API.

<html>
<head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("visualization", "1", {packages: ["corechart"]});
        google.setOnLoadCallback(drawChart);
        function drawChart() {
            var data = google.visualization.arrayToDataTable([
                ['Year', 'Expenses'],
                ['2004', 400],
                ['2005', 460],
                ['2006', 1120],
                ['2007', 540]
            ]);

            var options = {
                hAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
            };

            var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
            chart.draw(data, options);
        }
    </script>
</head>
<body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>

When I click on one of bar it is displaying data of x and y axis in tooltip. I want data which is displayed in tooltip(2006 and 460) as alert. How to find it.

enter image description here

Upvotes: 0

Views: 1147

Answers (1)

asgallant
asgallant

Reputation: 26340

Use a "select" event handler, and grab the data from the DataTable based on the selected element:

google.visaulization.events.addListener(chart, 'select', function () {
    var selection = chart.getSelection();
    if (selection.length) {
        alert(data.getValue(selection[0].row, 0) + ' ' + data.getValue(selection[0].row, selection[0].column));
    }
});

Upvotes: 2

Related Questions