簡體   English   中英

獲取谷歌圖表中柱形圖的工具提示數據

[英]Get tooltip data of column chart in google charts

我使用以下代碼使用谷歌可視化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>

當我點擊其中一個欄時,它會在工具提示中顯示x和y軸的數據。 我希望工具提示(2006和460)中顯示的數據為警報。 如何找到它。

在此輸入圖像描述

使用“select”事件處理程序,並根據所選元素從DataTable中獲取數據:

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));
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM