aboutsummaryrefslogtreecommitdiff
path: root/fe/src/lib/Chart.svelte
blob: b19d932fe8e9e3ee1475b6f68f703237463f7210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<script lang="ts">
import { onDestroy } from "svelte";
import ChartJS from "chart.js/auto";

export let data;
export let labels;
export let type = 'bar';

let ref: HTMLCanvasElement;
let chart

function setupChart(result) {
    [labels, data] = result;
    chart = new ChartJS(ref, {
        type,
        data: {
            labels,
            datasets: [
                {
                    label: "Totals",
                    data,
                    backgroundColor: "rgba(255, 192, 192, 0.2)"
                }
            ]
        },
        options: {
            responsive: true,
            maintainAspectRatio: false,
            scales: {
                y: {
                    suggestedMax: 30,
                    beginAtZero: true,
                    ticks: {
                        autoSkip: true,
                        stepSize: 5
                    }
                }
            },
            plugins: {
                legend: {
                    display: false
                },
                title: {
                    display: true,
                    text: "Weekly Breakdown"
                },
                subtitle: {
                    display: true,
                    text: "Water consumption over the last week",
                    padding: {bottom: 10}
                }
            }
        }
    });
    
    onDestroy(() => {
        if (chart) chart.destroy();
        chart = null;
    })
}
</script>

<canvas bind:this={ref} />