Added relatime button which keeps reloading every 2 sec

This commit is contained in:
2023-06-03 13:22:53 +02:00
parent b760ef87ec
commit e4edc6c78e
2 changed files with 20 additions and 10 deletions

View File

@@ -94,17 +94,16 @@
function renderChart() { function renderChart() {
const context: CanvasRenderingContext2D | null = chartCanvas.getContext('2d'); const context: CanvasRenderingContext2D | null = chartCanvas.getContext('2d');
if (!context) return if (!context) return;
// create labels and singular dataset (data) // create labels and singular dataset (data)
const labels: string[] = dateLabelsFormatedBasedOnResolution(dataFrames); const labels: string[] = dateLabelsFormatedBasedOnResolution(dataFrames);
const data: ChartDataset = { const data: ChartDataset = {
data: dataFrames.map((frame) => frame.value), data: dataFrames.map((frame) => frame.value),
borderWidth: 3, borderWidth: 3
}; };
// based on name, add label and color options to dataset // based on name, add label and color options to dataset
setDataColorAndName(data) setDataColorAndName(data);
// create chart instance, most here is chart options // create chart instance, most here is chart options
chart = new Chart(context, { chart = new Chart(context, {

View File

@@ -21,7 +21,14 @@
return fetch(`/api/graph/${unit}`, options).then((resp) => resp.json()); return fetch(`/api/graph/${unit}`, options).then((resp) => resp.json());
} }
const buttonMinutes = [ interface IButtonMinute {
value: number;
name: string;
}
let timeout: ReturnType<typeof setTimeout>;
const buttonMinutes: Array<IButtonMinute> = [
{ value: 2.4, name: 'Realtime' },
{ value: 15, name: 'Last 15 minutes' }, { value: 15, name: 'Last 15 minutes' },
{ value: 60, name: 'Last hour' }, { value: 60, name: 'Last hour' },
{ value: 360, name: 'Last 6 hours' }, { value: 360, name: 'Last 6 hours' },
@@ -33,8 +40,9 @@
{ value: 518400, name: 'Last year' } { value: 518400, name: 'Last year' }
]; ];
function reload(mins: number) { function reload(button: IButtonMinute) {
minutes = mins; minutes = button.value;
clearTimeout(timeout);
const to: Date = new Date(); const to: Date = new Date();
const from = new Date(to.getTime() - minutes * 60 * 1000); const from = new Date(to.getTime() - minutes * 60 * 1000);
const size = 40; const size = 40;
@@ -42,6 +50,10 @@
fetchData('temperature', from, to, size).then((resp) => (temperatureData = resp?.data)); fetchData('temperature', from, to, size).then((resp) => (temperatureData = resp?.data));
fetchData('humidity', from, to, size).then((resp) => (humidityData = resp?.data)); fetchData('humidity', from, to, size).then((resp) => (humidityData = resp?.data));
fetchData('pressure', from, to, size).then((resp) => (pressureData = resp?.data)); fetchData('pressure', from, to, size).then((resp) => (pressureData = resp?.data));
if (button.name === 'Realtime') {
timeout = setTimeout(() => reload(button), 2000);
}
} }
function scrollSelectedButtonIntoView() { function scrollSelectedButtonIntoView() {
@@ -63,9 +75,8 @@
<div class="button-wrapper"> <div class="button-wrapper">
{#each buttonMinutes as button} {#each buttonMinutes as button}
<button <button on:click="{() => reload(button)}" class="{button.value === minutes ? 'selected' : ''}"
on:click="{() => reload(button.value)}" >{button.name}</button
class="{button.value === minutes ? 'selected' : ''}">{button.name}</button
> >
{/each} {/each}
</div> </div>