Skip to content

Commit

Permalink
fix: fix chart recreation (#257)
Browse files Browse the repository at this point in the history
## Summary

Fix charts recreation.

The charts were not being destroyed when the component was unmounted,
causing an error in the chart recreation process.
  • Loading branch information
joaotomaspinheiro authored Jul 7, 2024
1 parent 5e4070d commit 3bf7fcb
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { onDestroy } from "svelte";
import { Chart } from "chart.js";
import Card from "../../components/Card.svelte";
import type { Container } from "../../../../domain/container";
Expand All @@ -21,7 +22,12 @@
/**
* The canvas element where the chart is rendered.
*/
let canvas: HTMLCanvasElement;
let canvas: HTMLCanvasElement | undefined;
/**
* The chart being rendered.
*/
let chart: Chart<"line"> | undefined;
/**
* Retrieves a map with the month index and the corresponding names.
Expand All @@ -45,6 +51,11 @@
* @param containers Containers.
*/
function buildChart(containers: Container[]) {
// Exit if canvas is not bound to DOM element.
if (!canvas) {
return;
}
// Build a map with the amount of containers added per month.
const containersPerMonth = new Map<number, number>();
const monthNames = getMonths();
Expand Down Expand Up @@ -79,7 +90,7 @@
return acc;
}, data);
new Chart(canvas, {
chart = new Chart(canvas, {
type: "line",
data: {
labels,
Expand Down Expand Up @@ -113,6 +124,11 @@
loading = false;
}
onDestroy(() => {
// Destroy chart before the component destruction.
chart?.destroy();
});
containersPromise.then(containers => buildChart(containers));
</script>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { onDestroy } from "svelte";
import { Chart } from "chart.js";
import Card from "../../components/Card.svelte";
import type {
Expand All @@ -22,7 +23,12 @@
/**
* The canvas element where the chart is rendered.
*/
let canvas: HTMLCanvasElement;
let canvas: HTMLCanvasElement | undefined;
/**
* The chart being rendered.
*/
let chart: Chart<"doughnut"> | undefined;
/**
* The color for each respective container category.
Expand All @@ -42,6 +48,11 @@
* @param containers Containers.
*/
function buildChart(containers: Container[]) {
// Exit if canvas is not bound to DOM element.
if (!canvas) {
return;
}
// Build a map with the amount of containers per container category.
const containerAmountPerCategory = new Map<ContainerCategory, number>();
for (const container of containers) {
Expand All @@ -65,7 +76,7 @@
category => categoryColors[category],
);
new Chart(canvas, {
chart = new Chart(canvas, {
type: "doughnut",
data: {
labels,
Expand All @@ -88,6 +99,11 @@
loading = false;
}
onDestroy(() => {
// Destroy chart before the component destruction.
chart?.destroy();
});
containersPromise.then(containers => buildChart(containers));
</script>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script lang="ts">
import { onDestroy } from "svelte";
import { Chart } from "chart.js";
import Card from "../../components/Card.svelte";
import type { Container } from "../../../../domain/container";
Expand All @@ -19,13 +20,23 @@
/**
* The canvas element where the chart is rendered.
*/
let canvas: HTMLCanvasElement;
let canvas: HTMLCanvasElement | undefined;
/**
* The chart being rendered.
*/
let chart: Chart<"bar"> | undefined;
/**
* Builds a chart with the containers.
* @param containers Containers.
*/
function buildChart(containers: Container[]) {
// Exit if canvas is not bound to DOM element.
if (!canvas) {
return;
}
// Build a map with the amount of containers per municipality.
const containersPerMunicipality = new Map<string, number>();
for (const container of containers) {
Expand All @@ -44,7 +55,7 @@
// Get chart data.
const data = Array.from(containersPerMunicipality.values());
new Chart(canvas, {
chart = new Chart(canvas, {
type: "bar",
data: {
labels,
Expand Down Expand Up @@ -72,6 +83,11 @@
loading = false;
}
onDestroy(() => {
// Destroy chart before the component destruction.
chart?.destroy();
});
containersPromise.then(containers => buildChart(containers));
</script>

Expand Down

0 comments on commit 3bf7fcb

Please sign in to comment.