IrisVega
6db2ee1434
modified: static/js/sensor-2.js modified: static/js/sensor-3.js modified: static/js/sensor-4.js
79 lines
2.6 KiB
JavaScript
79 lines
2.6 KiB
JavaScript
//折线图
|
|
document.addEventListener('DOMContentLoaded', function () {
|
|
var chartContainer = document.getElementById('chart-container');
|
|
var myChart = echarts.init(chartContainer);
|
|
|
|
var timestamps = [];
|
|
var co2 = [];
|
|
|
|
function fetchAndUpdateData() {
|
|
fetch('http://111.230.197.156:8000/get_sensor_data')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
timestamps = data.timestamps.slice(-200); // 只保留最新的200个数据
|
|
co2 = data.co2.slice(-200); // 只保留最新的200个数据
|
|
var option = {
|
|
title: {
|
|
text: 'co2数据',
|
|
textStyle: {
|
|
color: 'white'
|
|
}
|
|
},
|
|
tooltip: {
|
|
trigger: 'axis'
|
|
},
|
|
legend: {
|
|
data: ['co2'],
|
|
textStyle: {
|
|
color: 'white'
|
|
}
|
|
},
|
|
xAxis: {
|
|
type: 'category',
|
|
data: timestamps,
|
|
axisLabel: {
|
|
color: 'white'
|
|
}
|
|
},
|
|
yAxis: {
|
|
type: 'value',
|
|
axisLabel: {
|
|
color: 'white'
|
|
}
|
|
},
|
|
series: [
|
|
{
|
|
name: 'co2',
|
|
type: 'line',
|
|
data: co2
|
|
|
|
}
|
|
]
|
|
};
|
|
myChart.setOption(option);
|
|
});
|
|
fetch('http://111.230.197.156:8000/get_latest_sensor_data')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
document.getElementById('co2').textContent = `当前co2: ${co2.slice(-1)}%`;
|
|
});
|
|
}
|
|
// 每5秒刷新一次数据
|
|
setInterval(fetchAndUpdateData, 1000);
|
|
fetchAndUpdateData(); // 初始加载数据
|
|
});
|
|
|
|
|
|
//获取当前的湿度
|
|
async function fetchLatestSensorData() {
|
|
const response = await fetch('http://111.230.197.156:8000/get_latest_sensor_data');
|
|
const data = await response.json();
|
|
document.getElementById('co2').textContent = `当前co2: ${co2.slice(-1)}%`;
|
|
}
|
|
// 每5秒刷新一次数据
|
|
fetch('http://111.230.197.156:8000/get_latest_sensor_data')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
fetchLatestSensorData(); // 初始加载数据
|
|
document.addEventListener('DOMContentLoaded', fetchLatestSensorData);
|
|
}) |