Dive into the options of chart.js to create stunning charts.

Interactive charts can provide a cool way to visualize your data.

However most out of the box solutions are not as beautiful as they could be, with default options.

I will show you how to customize your chart.js options to make some cool charts!

⚡ Quick Start

What we will use:

We use vue-cli to create a basic structure. So I hope you got it installed already. And we use vue-chart.js as a wrapper for chart.js.

vue init webpack awesome-charts

Then we go into our project folder and install our dependencies.

cd awesome-charts && yarn install

And we add vue-chartjs :

yarn add vue-chartjs chart.js

Our first chart

So, let’s create our first line chart.

touch src/components/LineChart.js && subl .

Now we need to import the Line BaseChart from vue-chartjs and create our component.

In the mount() function we need to call the renderChart() method with our data and options.

LineChart.js

We pass in a basic chart.js data object with some sample data and in the option parameter, we pass responsive: true. So the chart will grow brased on our outer container.

☝ We can call the method renderChart() because we extended the BaseChart, were this method and some props are defined

Mount & Test it

Now we delete the Hello.vue component from our App.vue and import our chart.

App.vue

And after we run the dev script in our terminal, we should see our chart.

yarn run dev

💄 Make me beautiful

Okay, now it is time for some beautification 💅. There are a few cool tricks in chart.js. We can pass a color hex value to backgroundColor; But we can also pass a rgba() value. So we can make our color transparent. And as chart.js is using html canvas to draw, we can utilize createLinearGradient()

This is where the fun starts. 🎢 But to use it we need the canvas object. But this is not a big deal, as vue-chartjs holds a reference to it. We can access it over this.$refs.canvas

So in our LineChart.js we create two variables to store a gradient. Because we have to datasets.

Then we create two gradients:

this.gradient = this.$refs.canvas

.getContext(‘2d’)

.createLinearGradient(0, 0, 0, 450) this.gradient2 = this.$refs.canvas

.getContext(‘2d’)

.createLinearGradient(0, 0, 0, 450)

There is another cool function we can use: addColorStop()

We create three colorStops for each gradient. For 0%, 50% and 100%.

this.gradient.addColorStop(0, ‘rgba(255, 0,0, 0.5)’)

this.gradient.addColorStop(0.5, ‘rgba(255, 0, 0, 0.25)’);

this.gradient.addColorStop(1, ‘rgba(255, 0, 0, 0)’);



this.gradient2.addColorStop(0, ‘rgba(0, 231, 255, 0.9)’)

this.gradient2.addColorStop(0.5, ‘rgba(0, 231, 255, 0.25)’);

this.gradient2.addColorStop(1, ‘rgba(0, 231, 255, 0)’);

Now we can pass this.gradient to backgroundColor. And we have a very nice gradient. To get a nicer effect we also set the borderColor to the individual color with an alpha of 1. (or we use the hex value) And set the borderWidth to 1 and last but not least the pointColor.

borderColor: ‘#FC2525’,

pointBackgroundColor: ‘white’,

borderWidth: 1,

pointBorderColor: ‘white’,

Presentation

Last step is to add some styling to the container in our App.vue

👏 Final Result