我試圖用兩個不同的數據源顯示兩個不同的圖表。我的第二個函數drawChart3()覆蓋了第一個圖表及其數據源。我嘗試添加一個時間監聽器來減輕,但我一直不成功。我很新的JavaScript,所以我很欣賞任何提示我的錯誤可能駐留在哪裏。 謝謝谷歌圖表第二個數據覆蓋第一個數據和圖表
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
google.setOnLoadCallback(drawChart3);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
var query = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1ODIBoKW3H9bLNR2RnyfEqd9c0fzWaql1FfRp_JCWCyY/edit#gid=0');
query.setQuery('select *');
query.send(handleQueryResponse);
}
function drawChart3() {
var query3 = new google.visualization.Query('https://docs.google.com/spreadsheets/d/1yiNn3oQYYK4Z1aCl5R2pGqFcZLU66uFA1tqW69sGrOg/edit#gid=0');
query3.setQuery('select *');
query3.send(handleQueryResponse);
}
//Set chart options
var options = {'title': '^VIX Close & XX Correlation Coefficient',
'legend': {position: 'none'},
'width': 600,
'height': 300};
//Set chart options
var options3 = {'title': 'Search Queries: "XX", "Algorithmic Trading", "Trading", "Options"',
'legend': 'bottom',
chartArea:{left:60,top:50,width:'98%',height:'75%'},
'width': 1300,
'height': 500};
function handleQueryResponse(response) {
var data = response.getDataTable();
var chart = new google.visualization.AreaChart(document.getElementById('chart-container'));
google.visualization.events.addOneTimeListener(chart, 'ready', function() {
var chart3 = new google.visualization.AreaChart(document.getElementById('chart3-container'));
var data3 = response.getDataTable();
chart3.draw(data3, options3);
});
chart.draw(data, options);
}
</script>
</head>
<body>
<td><div id="chart-container" style="border: 1px solid #ccc"></div></td>
<td><div id="chart3-container" style="border: 1px solid #ccc"></div></td>
</body>
</html>
我認爲你的邏輯是關閉的。它的覆蓋是因爲'response.getDataTable()'在'data'和'data3'之間不會改變。你需要重新修改你的邏輯。 –