我试图从我创建的数据数组中绘制图形并失败。 我尝试了我在这里看到的所有相同主题的内容,并没有帮助。请指教。在同一页面中有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>
答案 0 :(得分:4)
在您使用这些库之前,您需要等待谷歌可视化已完成加载其库。而不是
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'));
})