运行时不会使前面运行过的时间数据消失,而是会叠加,希望可以移除之前运行的时间数据,每次运行都会出现另一组数据,不要叠加出现
以下是错误图片。希望效果是每次都是从一开始重新开始而不是叠加
还有个问题是抓数据的问题,Now的时候希望抓出25个数据但只能抓出20个,求解答
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Direction</title>
<link rel="stylesheet" href="index.css">
<!-- <script src="path/to/chartjs/dist/chart.js"></script> -->
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.7.1/dist/chart.min.js"></script>
</head>
<body onload="loadingTable('1')">
<div id="nav">
<ul>
<li class="center">
<a href="index.html" target="_top">Measurements</a>
</li>
<li class="center">
<a href="temperatures.html" target="_top">Temperatures</a>
<li class="center">
<a href="direction.html" target="_top">Direction</a>
</li>
<li class="center dropdown">
<a href="#" target="_top">allType</a>
<div class="center_dropdown">
<a href="allType1.html" target="_top">rain</a>
<a href="allType2.html" target="_top">wind_speed</a>
<a href="allType3.html" target="_top">wind_direction</a>
<a href="allType4.html" target="_top">light</a>
<a href="allType5.html" target="_top">Temperature</a>
</div>
</li>
<li class="center">
<a href="message.html" target="_top">Message</a>
</li>
</ul>
</div>
<div class="control">
<select name="select" id="select" onchange="loadingTable(this.value)">
<option value="1">Now</option>
<option value="23">24 hours</option>
<option value="47">48 hours</option>
<option value="71">72 hours</option>
<option value="167">1 week</option>
</select>
</div>
<table id="myTable" cellspacing="0">
<tr>
<th>number</th>
<th>time</th>
<th>value</th>
</tr>
</table>
<canvas id="myChart" height="400"></canvas>
<script src="getData.js"></script>
<script>
let myChart = null;
function loadingTable(timeID) {
let table = document.getElementById('myTable').firstElementChild
let getSource = 'http://webapi19sa-1.course.tamk.cloud/v1/weather/wind_direction/';
let sourceID;
if (timeID==1){
sourceID = getSource;
}
else{
sourceID = getSource + timeID;
}
console.log(sourceID);
let direction = getDate(sourceID);
const ctx = document.getElementById('myChart').getContext('2d');
if (myChart != null) {
myChart.destroy();
}
renderTable(direction, table);
createChart(ctx, direction);
}
function renderTable(direction, table) {
direction.forEach((ele, ii) => {
let val = null;
let text = null;
let tr = document.createElement('tr')
for (let i = 0; i < 3; i++) {
let td = document.createElement('td')
let val = i == 0 ? ii + 1 : i == 1 ? ele.date_time.replace('T', ' ').slice(0, 18) : i == 2 ? ele.wind_direction : 'null'
let text = document.createTextNode(val)
td.appendChild(text)
tr.appendChild(td)
}
table.appendChild(tr)
});
}
function createChart(ctx, direction) {
if (myChart != null) {
myChart.destroy();
}
myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: direction.map(item => item.date_time.slice(11, 19)),
datasets: [{
label: 'direction',
data: direction.map(item => item.wind_direction),
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
}
</script>
</body>
</html>
调用renderTable方法之前,先使用deleteRow清空之前的数据,参考:
https://www.php.cn/js-tutorial-479003.html
1.table中的表头用thead标签包住
2.给tbody里面appendChild
3.renderTable方法里要先清空tbody
document.querySelector("#myTable tbody").html("")
getData.js 贴出来
还有个问题是抓数据的问题,Now的时候希望抓出25个数据但只能抓出20个,求解答