Svelte Charts: Quick Charts in SvelteKit with Chart.js Svelte Charts: Quick Charts in SvelteKit with Chart.js
In this Svelte charts post post, we see how you can quickly add customisable charts to your Svelte app using Chart.js. You should find this post useful, in fact, if you are creating a dashboard or console and need to throw in some charts. Alternatively, you might be writing an article summarising some data. You should find the post equally useful for these use cases and many others. Although we focus on SvelteKit, we have tested the approach on Astro and it should be easily adapted to other Svelte frameworks .
We will create a bar chart from the State of JS 2021 survey results. Locally, we will source the data from a JSON file. After importing the data, we will then add some custom styling and see configuration changes which you need for Chart.js to work smoothly with SvelteKit. You will see that with minimal effort you can knock up something with Chart.js and Svelte which does not look too bad at all!
There’s not all that much to do. You will probably find you spend most time on customising the charts to show your research or data in its best light. To get going why don’t we spin up a new SvelteKit project from the Terminal:
I chose the TypeScript option along with ESLint and Prettier , but no Browser testing. If this is your first time using TypeScript with SvelteKit, then you might want to check out the Starting out Svelte and SvelteKit tutorial which gives a little more colour on using SvelteKit with TypeScript. We use the Merriweather font self-hosted below, so you might also want to install that now, along with the Chart.js package (and its types):
Probably the most important part of this project are the data! We will add them in a JSON file,
then use Vite’s JSON import feature to import them onto our chart page. Create a new
src/lib/data folder and in there, make a
satisfaction.json file and past in this
Chart.js uses an HTML canvas element to create the chart in. Typically you can just create a div
use that approach. Replace the code in
This will not work out-of-the-box, so let’s first see how to fix it then take a closer look at the code.
You will probably get a
registerables is not iterable error in your
browser. This is just because although Chart.js ships with ESM support it does not currently work out-of-the-box
with SvelteKit. No sweat though, we can fix this with a few lines of config
Add vite SSR config
vite.config.js to sort this issue:
Give that a save then jump back to you browser and it should all be right as rain.
src/routes/indx.svelte we import the top level JSON data as a named import. If you didn’t know you could do this
with Vite or SvelteKit, check out the
short video on Svelte JSON imports
Jumping down to line
81 we see where we include the chart in our markup.
bind:this on the
38 we get our data
into a format which Chart.js expects. As examples,
method to extract just the
framework field from each data point in
our input JSON. We do something very similar in line
17 to extract
the actual value for each data point. Then,
backgroundColor is just
an array of colours to use for the bars. I generated some tints and shades using the What-a-colour
app. You can create and customise this app yourself in the Starting out Svelte and SvelteKit tutorial
. We also look at converting colours between hex and hsl and generating contrast ratios for a
chosen colour palette.
onMount method is for any setting up we need to do after
the initial render of our page. That works here because we need an initial render in order to have
canvas element to attach the chart to. In line
43, we create a Chart object, attaching it to
canvas element). We add minimal customisation in the following lines.
This is just to give you a flavour of what is possible. Go to town on your own app. You will find a
mountain of material on customisation in the Chart.js docs
The chart looks a little plain, albeit with the pink bars! We will see how to change the chart
background colour in a moment. Before that though, here are some styles for the app itself. Paste
this content at the end of
As a final step we will see how to change the chart background colour, then the customary test!
For Chart.js charts, you can change the background colour by updating the canvas element’s background. You could push the boat out and add a picture for the background by tweaking the method we use here. We will keep it simple though and go for a solid colour. To do that we create a Chart.js plugin. Although that sounds overwhelming, it reality it’s just a few extra lines of code.
We actually set the background colour to “Irish Coffee” using HSL in line
53. That’s all there is to creating a chart in Svelte using Chart.js.
This is how your page should look:
Hope you are as impressed as I am with how little effort we needed to create something fairly respectable.
In this Svelte Charts post we have seen how quickly to create charts in Svelte. In particular we have touched on:
- using Chart.js with Svelte including initialising a chart and customising styles ,
- adding a background colour to a Chart.js bar chart,
- Vite configuration for using Chart.js with SvelteKit.
This has just been an introduction with using Svelte with Chart.js. You can use the code here as a stepping stone for creating Svelte Pie Charts, Line Charts and so much more. Check Chart.js docs for more on the config for these other chart types, then just drop it into your Svelte code. Let me know what you end up creating. You can drop a comment below or reach out for a chat on Element as well as Twitter @mention
You can see the full code for this Svelte Chart project, in all its glory in the Rodney Lab Git Hub repo .
- A promising solution for adding charts to your Svelte app is Pancake.js from Rich Harris; the Svelte creator. That library is quite new at the time of writing though and there is limited documentation. A nice alternative is Chart.js. There is extensive documentation, for example on creating bar charts, pie charts, line charts and more. You can also customise the charts to present your data in their best light with styling to match your own taste.
- Yes, Chart.js has ESM support and works with SvelteKit. However, it does not work out of the box. Despite that, you only need a few lines of config to have the two paying nicely together. Open up your `svelte.config.js` files and under the `kit` options add `vite.ssr.noExternal: ['chart.js']`, That's it. You can now spin up your dev server as normal or run a local build then preview.
- Chart.js is quite customisable and you can change fonts and styling in the Chart.js configuration object. Chart.js creates your charts on an HTML5 Canvas. To change background colour, one option is actually just to change the colour of the canvas which the chart is drawn on. We have seen you can add a Chart.js plugin to do this. In reality this can just be an extra few lines in the Chart.js configuration object. Here just get the canvas context using `const ctx = chart.canvas.getContext('2d')`, then be sure to set `ctx.fillStyle = 'some-colour';`. In fact you can even tweak this to set an image as the background, instead of a solid colour.
If you have found this video useful, see links below for further related content on this site. I do hope you learned one new thing from the video. Let me know if there are any ways I can improve on it. I hope you will use the code or starter in your own projects. Be sure to share your work on Twitter, giving me a mention so I can see what you did. Finally be sure to let me know ideas for other short videos you would like to see. Read on to find ways to get in touch, further below. If you have found this post useful, even though you can only afford even a tiny contribution, please consider supporting me through Buy me a Coffee .
Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab . I post regularly on SvelteKit as well as Search Engine Optimisation among other topics. Also subscribe to the newsletter to keep up-to-date with our latest projects.