我正在使用Google饼图,当用户点击它时,应显示图表上的数据。我尝试使用下面的代码,但它给了我一个错误无法读取属性getValue。
我尝试显示selectedItem.row值并且它工作正常但data.getValues没有从图表中获取任何所需的值。
google.load('visualization', '1', {'packages':['corechart','table']});
google.charts.load('current', {'packages':['corechart']});
google.setOnLoadCallback(drawItems(num));
var data;
var chart;
// Load the Visualization API and the piechart,table package.
google.load('visualization', '1', {'packages':['corechart','table']});
google.charts.load('current', {'packages':['corechart']});
google.setOnLoadCallback(drawItems(num));
var data;
var chart;
function drawItems(num)
{
var jsonPieChartData = $.ajax({
url: "getpiechartdata.php",
data: "q="+num,
dataType:"json",
async: false,
}).responseText;
var piechartdata = new google.visualization.DataTable(jsonPieChartData);
var options = {
width: 700,
height: 200,
is3D: true,
chartArea: { left:"5%",top:"5%",width:"90%",height:"90%" }
};
// Instantiate and draw our pie chart, passing in some options.
var chart = new
google.visualization.PieChart(document.getElementById('chart_div'));
function selectHandler() {
try
{
var selectedItem = chart.getSelection()[0];
if (selectedItem) {
var topping = data.getValue(selectedItem.row, 0);
alert('The user selected ' + topping);
}
}
catch (err)
{
alert(err.message);
}}
chart.draw(piechartdata, options);
google.visualization.events.addListener(chart, 'select',
selectHandler);
}
答案 0 :(得分:0)
getValue
是google.visualization.DataTable
对象的一种方法。您已将此分配给变量piechartdata
,而非data
,因此它应为:
var topping = piechartdata.getValue(selectedItem.row, 0);
请参阅您链接到的文档页面上的Hello Charts
示例。