无法读取未定义的属性getValue - Google Piechart

时间:2016-08-15 06:37:11

标签: javascript google-visualization pie-chart

我正在使用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);  
}

1 个答案:

答案 0 :(得分:0)

getValuegoogle.visualization.DataTable对象的一种方法。您已将此分配给变量piechartdata,而非data,因此它应为:

var topping = piechartdata.getValue(selectedItem.row, 0);

请参阅您链接到的文档页面上的Hello Charts示例。