简体   繁体   English

谷歌图表获取列标签值

[英]Google Charts get column label value

How can i retrieve the value for the grouped columns?如何检索分组列的值? In this case i want the value that is seen both at the bottom of the graph and the tooltip.在这种情况下,我想要在图表底部和工具提示中看到的值。 I'm not quite sure to access it.我不太确定要访问它。 I know this value is in the first row of the dataTable when i initially set it.当我最初设置它时,我知道这个值在 dataTable 的第一行。 But how do i retrieve it based on which column is clicked?但是我如何根据点击的列来检索它?

在此处输入图片说明

 google.charts.load('current', {packages: ['corechart', 'bar']}); google.charts.setOnLoadCallback(drawGraph); var theChart; var dataTable; function drawGraph() { var jsonData = ([ ["Something", "Else", "here"], [8, 1, 0.25], [9, 2, 0.5], [10, 3, 1], [11, 4, 2.25], [12, 5, 2.25], [13, 6, 3], [14, 7, 3.25], [15, 8, 5], [16, 9, 6.5], [17, 10, 10], ]); var options = {}; dataTable = new google.visualization.arrayToDataTable(jsonData); theChart = new google.visualization.ColumnChart(document.getElementById('chart_div')); google.visualization.events.addListener(theChart, 'select', selectHandler); theChart.draw(dataTable, options); } function selectHandler() { var selection = theChart.getSelection(); for (var i = 0; i < selection.length; i++) { var item = selection[i]; console.log(item) var selectedData = dataTable.getValue(item.row, item.column); console.log(selectedData); console.log(dataTable.getColumnProperties(item.column)); console.log(dataTable.getRowProperties(item.row)); console.log(dataTable.getColumnLabel(item.column)); console.log(dataTable.toJSON()); console.log('>>>>'); } }
 <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div>

you have the code to get the value of the column ( 7 ),你有代码来获取列( 7 )的值,

var selectedData = dataTable.getValue(item.row, item.column);

the other value you need is the x-axis value ( 14 ).您需要的另一个值是 x 轴值 ( 14 )。
this can be found in the first column of the data table.这可以在数据表的第一列中找到。
just use 0 as the column index.只需使用0作为列索引。

var selectedLabel = dataTable.getValue(item.row, 0);

see following working snippet...请参阅以下工作片段...

 google.charts.load('current', {packages: ['corechart', 'bar']}); google.charts.setOnLoadCallback(drawGraph); var theChart; var dataTable; function drawGraph() { var jsonData = ([ ["Something", "Else", "here"], [8, 1, 0.25], [9, 2, 0.5], [10, 3, 1], [11, 4, 2.25], [12, 5, 2.25], [13, 6, 3], [14, 7, 3.25], [15, 8, 5], [16, 9, 6.5], [17, 10, 10], ]); var options = {}; dataTable = new google.visualization.arrayToDataTable(jsonData); theChart = new google.visualization.ColumnChart(document.getElementById('chart_div')); google.visualization.events.addListener(theChart, 'select', selectHandler); theChart.draw(dataTable, options); } function selectHandler() { var selection = theChart.getSelection(); for (var i = 0; i < selection.length; i++) { var item = selection[i]; console.log(item) var selectedData = dataTable.getValue(item.row, item.column); var selectedLabel = dataTable.getValue(item.row, 0); console.log(selectedLabel, selectedData); } }
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM