下面是我的代码,其中包含 3 个图表,只有在我刷新后才会在页面上加载,有没有人知道如何在我第一次单击此页面时加载它?PS 我已经尝试了很多成功的建议:function() 并尝试使用 ajax 直接加载 div 但没有运气
<script src="https://www.google.com/jsapi" type="text/javascript"></script>
<script>
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Date', 'Temp'],
{% for graphreading in graphreadings %}
['{{ graphreading.timeformatted }}', {{ graphreading.temperature }}],
{% endfor %}
]);
var options = {
hAxis: {title: 'Timestamp',
titleTextStyle: {color: 'black'}},
vAxis: {title: 'Temperature in °F'},
colors: ['red']
};
var chart = new google.visualization.LineChart(document.getElementById('chart_temp'));
chart.draw(data, options);
}
</script>
<script>
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Date', 'CO2'],
{% for graphreading in graphreadings %}
['{{ graphreading.timeformatted }}', {{ graphreading.co2 }}],
{% endfor %}
]);
var options = {
hAxis: {title: 'Timestamp', titleTextStyle: {color: 'black'}},
vAxis: {title: 'CO2 in ppm'},
colors: ['green']
};
var chart = new google.visualization.LineChart(document.getElementById('chart_co2'));
chart.draw(data, options);
}
</script>
<script>
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Date', 'Humidity'],
{% for graphreading in graphreadings %}
['{{ graphreading.timeformatted }}', {{ graphreading.humidity }}],
{% endfor %}
]);
var options = {
hAxis: {title: 'Timestamp', titleTextStyle: {color: 'black'}},
vAxis: {title: 'Humidity in %'},
colors: ['blue']
};
var chart = new google.visualization.LineChart(document.getElementById('chart_humidity'));
chart.draw(data, options);
}
</script>
<div class="middlepages" >
<ul data-role="listview" data-inset="true" data-dividertheme="a" >
<li><div style="text-align: center; font-size: x-large;"> Readings for {{ zone.description }}</div><div id="chart_temp"></div></li>
<li><div id="chart_co2"></div> </li>
<li><div id="chart_humidity"></div> </li>
{% for reading in readings %}
<li>Time Created: {{ reading.timeformatted }} Temp:{{ reading.temperature }}
CO2:{{ reading.co2 }} Humidity:{{ reading.humidity }} Fan: {{ reading.fan }}
Mode:{{ reading.mode }}</li>
{% endfor %}
</ul>
</div>