09. January 2022
Create Beautiful Charts with Svelte and Chart js
Svelte is a JavaScript compiler that compiles its code to HTML, CSS and vanilla JavaScript hence creating a very performant website. On the official website, there is a tutorial that explains Svelte step-by-step. If you just want to see the completed code and figure it out by yourself, go to the code for this article.
Now that the introduction and pleasantries are out of the way, it is time for the main event. I'll be assuming that the desktop environment being used in this tutorial is Linux based for the initial setup of a svelte application and you already have node and npm installed.
Prerequisites
So as to be able to fully work on the application at hand, you'll need:
- Familiarity with HTML, CSS and JavaScript(ES6+),
- Node.js and npm installed on your development machine.
- Working machine
For this tutorial, we shall be using the Chart js library for creating basic charts.
Why Chart.js
That's a pretty good question actually, why would anyone decide to use chart JS whereas there are very many other charting libraries out there that are more performant? The simple answer is that Chart JS(According to sources called “this is my opinion”) is easier to use. Some of the alternative charting libraries include:
- pancake which has very scarce documentation and is in thorough experimentation(at the time of writing). Since it has been created by Rich Harris, you can rest assured that it might probably never get documentation or a stable release just like our fallen soldier sapper (a moment of silence in remembrance)
- Chartist - Really impressive charting library that is only 10KB (Gzip) with no dependencies. Round of applause for this awesome library that should play nice with svelte since it does not have any dependencies. I honestly can't remember why I didn't go with this for this tutorial but there's always time for another tutorial, am I right 😉?
- d3.js - Great library, but it would take me to long to explain whereas I'm trying to write a super simple tutorial. I would also recommend this library, though those examples they present are thoroughly intimidating, like this example where you create a chart of Obama's face using Voronoi Stippling(not a clue what that is and my furniture just started floating around once I spoke those words out loud)
Getting Started
You could use codesandbox for your initial setup or create a local svelte application using the degit tool. Open a new terminal and run the following command:
|
|
Navigate into the newly created project folder and install the dependencies locally using:
|
|
Run the development server using:
|
|
The dev server will be listening from the http://localhost:5000 address. Any changes made to the application will be automatically rebuilt and reloaded into the running app as long as the dev server is still running.
You'll also need to install Chart js using:
|
|
In the App.svelte file, delete everything leaving only the script tags. Since this is a relatively simple app, this is where all our code will reside in.
|
|
And there you have it folks, a very good looking doughnut chart that showcases your “sample budget”.
We add the creation of the chart on the svelte onMount
function so that the chart is created and added to the layout before it is mounted on the page. Note that the value of canvas
will be undefined
until the component has mounted, so we put the logic inside the onMount
lifecycle function.
The read-only
this
binding applies to every element (and component) and allows you to obtain a reference to rendered elements. For example, we can get a reference to a<canvas>
element:
Chart JS receives the data from the data
variable we created. We have customized the labels as well as the background color of the doughnut lines.
The config
variable is what we've used to define the type of chart and to style the chart we've created. The type
can be either bar, line, pie, radar etc etc depending on the type of chart you want to render. You can play around with the sample data we've provided to see what kind of chart will be formed.
In a real life situation, the data to be displayed would be provided from an external service such as an API rather than have it hard coded as a variable.
Reducing the cutout
percentage will make the doughnut chart slightly less hollow and a 0% cutout will make it a pie chart.
If you need to make multiple charts on the same page then it would be simpler and easier to copy the code and convert it into a component where you export defaults. But that is all the time I have for this week folks, stay tuned for more next week.