diff --git a/src/lib/components/Graph.svelte b/src/lib/components/Graph.svelte index 3ab10d6..5954838 100644 --- a/src/lib/components/Graph.svelte +++ b/src/lib/components/Graph.svelte @@ -94,17 +94,16 @@ function renderChart() { const context: CanvasRenderingContext2D | null = chartCanvas.getContext('2d'); - if (!context) return + if (!context) return; // create labels and singular dataset (data) const labels: string[] = dateLabelsFormatedBasedOnResolution(dataFrames); const data: ChartDataset = { data: dataFrames.map((frame) => frame.value), - borderWidth: 3, + borderWidth: 3 }; // based on name, add label and color options to dataset - setDataColorAndName(data) - + setDataColorAndName(data); // create chart instance, most here is chart options chart = new Chart(context, { diff --git a/src/routes/graphs/+page.svelte b/src/routes/graphs/+page.svelte index 4208fba..a5cc28c 100644 --- a/src/routes/graphs/+page.svelte +++ b/src/routes/graphs/+page.svelte @@ -21,7 +21,14 @@ return fetch(`/api/graph/${unit}`, options).then((resp) => resp.json()); } - const buttonMinutes = [ + interface IButtonMinute { + value: number; + name: string; + } + + let timeout: ReturnType; + const buttonMinutes: Array = [ + { value: 2.4, name: 'Realtime' }, { value: 15, name: 'Last 15 minutes' }, { value: 60, name: 'Last hour' }, { value: 360, name: 'Last 6 hours' }, @@ -33,8 +40,9 @@ { value: 518400, name: 'Last year' } ]; - function reload(mins: number) { - minutes = mins; + function reload(button: IButtonMinute) { + minutes = button.value; + clearTimeout(timeout); const to: Date = new Date(); const from = new Date(to.getTime() - minutes * 60 * 1000); const size = 40; @@ -42,6 +50,10 @@ fetchData('temperature', from, to, size).then((resp) => (temperatureData = resp?.data)); fetchData('humidity', from, to, size).then((resp) => (humidityData = resp?.data)); fetchData('pressure', from, to, size).then((resp) => (pressureData = resp?.data)); + + if (button.name === 'Realtime') { + timeout = setTimeout(() => reload(button), 2000); + } } function scrollSelectedButtonIntoView() { @@ -63,9 +75,8 @@
{#each buttonMinutes as button} - {button.name} {/each}