Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

Expenses Chart using Svelte, TS, Chart.js, and Tailwind

Fred Campo 330

@fredcamp

Desktop design screenshot for the Expenses chart component coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
2junior
View challenge

Design comparison


SolutionDesign

Solution retrospective


To integrate Chart.js with Svelte, you have to make sure you imported the correct path when using Chart.js with npm.

import { Chart, type ChartItem } from 'chart.js/auto'

Since the chartCanvas would return undefined when the component is rendered, we have to use Svelte's onMount() to make sure that the chart would be displayed after the component is rendered.

import { onMount } from 'svelte'

let ctx: ChartItem
let chartCanvas: HTMLCanvasElement
let myChart: Chart

onMount(() => {
  displayChart()

  return () => {
    myChart.destroy()
  }
})

function displayChart(): void {
  //chart initialization goes here.....
}
<canvas class="cursor-pointer" bind:this={chartCanvas} />

Community feedback

Please log in to post a comment

Log in with GitHub
Discord logo

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord