可视化:未捕获类型错误:无法读取未定义的属性“arrayToDataTable”

Visualization : Uncaught TypeError: Cannot read property 'arrayToDataTable' of undefined

本文关键字:未定义 属性 arrayToDataTable 读取 类型 可视化 错误      更新时间:2023-09-26

我正在尝试从我创建的数据数组中绘制图形,但失败了。我尝试了我在这里看到的相同主题的所有内容,但没有帮助。请指教。同一页面中有 HTML,但我觉得显示它并不重要。

<script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src = "http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"> </script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
function getData(dateRange){
    var date = new Date();
    var currentDateInFormat = date.getFullYear() + "-" + date.getDate() + "-" + date.getMonth() + 1;
    var BASE_URL = 'https://query.yahooapis.com/v1/public/yql?q=';
    var yql_query_for_table_data;
    var dataArray = ['Date' , 'Stock Value'];
    if(dateRange === "year"){
        yql_query_for_table_data = 'select * from yahoo.finance.historicaldata where symbol = "YHOO" and startDate = "2009-09-11" and endDate = "2010-03-01"';
    }
    else if(dateRange === "halfYear"){
        yql_query_for_table_data = 'select * from yahoo.finance.historicaldata where symbol = "'+ symbol + '" and startDate = "2015-30-06" and endDate = "'+ currentDateInFormat + '"';
    }
    var yql_query_str_for_table_data = encodeURI(BASE_URL + yql_query_for_table_data);
    var query_str_final_for_table_data = yql_query_str_for_table_data + "&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
    $.getJSON(query_str_final_for_table_data , function(data){
        var stockArray = data.query.results.quote;
        for (var i=0; i<stockArray.length ; i++){
            var currentObject = stockArray[i];
            var pushArray =  [currentObject.Date , parseFloat(currentObject.Close)];
            dataArray[i+1] = pushArray;     
        }
        google.load('visualization' , '1.0' , {'packages':['corechart']});
        var chartData = google.visualization.arrayToDataTable(dataArray);
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        return dataArray;
    });
}
function buttonPressed(buttonTitle){
    console.log(buttonTitle);
    getData(buttonTitle);       

}

</script>

您需要等待谷歌可视化完成加载其库,然后才能使用这些库。而不是

google.load('visualization', '1.0', {'packages':['corechart']});
var chartData = google.visualization.arrayToDataTable(dataArray);
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

使用回调来确保arrayToDataTable在使用之前确实存在:

google.load('visualization', '1.0', {'packages':['corechart']});
google.charts.setOnLoadCallback(function() {
   var chartData = google.visualization.arrayToDataTable(dataArray);
   var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
})