Changed endpoints and db queries, chart now shows suggested mins and maxes

This commit is contained in:
Daniel Ledda
2020-11-07 00:50:36 +01:00
parent 69ee100edd
commit 40b23ebcde
3 changed files with 35 additions and 28 deletions

View File

@@ -15,11 +15,18 @@
const tempColor = 'rgb(0,134,222)';
const co2Color = 'rgb(194,30,30)';
async function getData() {
let urlEndpoint = "data/";
if (window.location.includes("since")) {
urlEndpoint += "since/" + window.location.slice(window.location.lastIndexOf("/") + 1);
}
const data = await fetch(urlEndpoint);
return transformData(await data.json());
}
async function initChart() {
const ctx = document.getElementById('myChart').getContext('2d');
const data = await fetch("data/");
console.log(data);
const {humidity, temp, co2} = transformData(await data.json());
const {humidity, temp, co2} = getData();
const myChart = Chart.Line(ctx, {
data: {
datasets: [{
@@ -65,8 +72,8 @@
id: 'y-axis-1',
ticks: {
fontColor: co2Color,
min: 400,
max: 1100,
suggestedMin: 400,
suggestedMax: 1100,
},
}, {
type: 'linear',
@@ -75,8 +82,8 @@
id: 'y-axis-2',
ticks: {
fontColor: tempColor,
min: 10,
max: 35,
suggestedMin: 10,
suggestedMax: 35,
},
gridLines: {
drawOnChartArea: false,
@@ -88,8 +95,8 @@
id: 'y-axis-3',
ticks: {
fontColor: humidityColor,
min: 15,
max: 85,
suggestedMin: 15,
suggestedMax: 85,
},
gridLines: {
drawOnChartArea: false,
@@ -117,9 +124,9 @@
setInterval(async () => {
const newDatum = await fetch("data/1");
const snapshot = (await newDatum.json()).snapshots[0];
chart.data.datasets[0].data.push({x: snapshot.time, y: snapshot.humidity});
chart.data.datasets[1].data.push({x: snapshot.time, y: snapshot.temp});
chart.data.datasets[2].data.push({x: snapshot.time, y: snapshot.co2});
chart.data.datasets[0].data.splice(0, 1, {x: snapshot.time, y: snapshot.humidity});
chart.data.datasets[1].data.splice(0, 1, {x: snapshot.time, y: snapshot.temp});
chart.data.datasets[2].data.splice(0, 1, {x: snapshot.time, y: snapshot.co2});
chart.update(0);
}, 10 * 1000);
}