Build a simple data visualization dashboard with Vue.js and Chart.js
You will need Node 8.9+ and the Vue CLI installed on your machine.
Introduction
You all know that data has become an important app of every single app, and even of our lives. Right?! It’s not interesting to have data and not be able to visualize them in a proper way. That’s why in this tutorial we focus on building a simple but useful data visualization app using Vue.js and Chart.js
Demo
Here is the final result of our app:
Nice isn’t it ?! 😁
Prerequisites
In order to follow this tutorial, knowledge of JavaScript and Node.js is required. You should also have the following installed on your machine:
- Node.js (>=8.9.0)
- npm (bundled with Node.js installer) or Yarn
- Vue-CLI
Create the project
In this tutorial, we’ll use the new Vue CLI to scaffold our project. It is such a great tool that enhances the developer productivity. Head over to your terminal and type this command to create your project: vue create your-project-name
. Feel free to name your project as you want 🙃 .
You will be prompted to pick a preset. You can either choose the default preset which comes with a basic Babel + ESLint setup, or select “Manually select features” to pick the features you need.
For the sake of the tutorial, we’ll choose the default preset as it best fits our needs.
At the end of the process, you should be left with a fresh project. But it’s not all, we also have to install some dependencies required to make our app work well. So run this command in your terminal to download them:
yarn add vue-chartjs chart.js
or
npm install vue-chartjs chart.js --save
Build the app
Now create a Chart.js
file and paste this code inside:
//../src/Chart.js
import {
Line,
mixins
} from 'vue-chartjs'
export default {
extends: Line,
mixins: [mixins.reactiveProp],
props: ['chartData', 'options'],
data() {
return {
options: { //Chart.js options
scales: {
yAxes: [{
ticks: {
beginAtZero: true
},
gridLines: {
display: true
}
}],
xAxes: [{
gridLines: {
display: false
}
}]
},
legend: {
display: true
},
responsive: true,
maintainAspectRatio: false
}
}
},
mounted() {
this.renderChart(this.chartdata, this.options)
}
}
Let’s have a look at what’s happening here.
In the code block above, we imported the Line Chart from vue-chartjs and the mixins module. Chart.js basically does not provide an option for an automatic update whenever a dataset changes but that can be done in vue-chartjs with the help of the following mixins:
- reactiveProp
- reactiveData
These mixins automatically create chartData
as a prop or data and add a watcher. If data has changed, the chart will update. You can find more here.
Also, the this.renderChart()
function inside the mounted
hook function is responsible for rendering the chart. this.chartData
is an object containing the dataset needed for the chart and we’ll get that by including it as a prop in the Visualisation.vue
component we’ll create soon, this.options
contains the options object that determines the appearance and configuration of the chart.
Now create the Visualisation.vue
file and paste this code inside:
//../src/components/Visualisation.vue
<template>
<chart :chart-data="datacollection"></chart>
</template>
<script>
import Chart from "./../Chart.js";
export default {
components: {
Chart
},
data() {
return {
datacollection: null
};
},
mounted() {
this.fillData();
},
methods: {
fillData() {
this.datacollection = {
labels: [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
],
datasets: [
{
label: "GitHub Commits",
backgroundColor: "#f87979",
data: [70, 20, 12, 39, 100, 40, 95, 80, 80, 20, 12, 101]
},
{
label: "Monthly incomes",
backgroundColor: "#A5CC82",
data: [205, 408, 188, 190, 58, 200, 190, 400, 164, 254, 290, 201]
}
]
};
},
}
};
</script>
<style>
</style>
In this component, we import our Chart.js
file; the chart-data
property of our chart component permits us to provide it with some data to render in the form of a chart.The datacollection
object is populated with some mock data within the fillData
method, and as soon as the component is mounted we execute it. It’s that simple 🙃 .
Now import your Visualisation.vue
component inside the ../src/App.vue
file and change the msg
property to the following: Simple data visualisation with Vue.js and Chart.js or whatever you want, and comment the img line. After these changes your App.vue
should look like the following:
//../src/components/App.vue
<template>
<div id="app">
<!-- <img alt="Vue logo" src="./assets/logo.png"> -->
<HelloWorld msg="Simple data visualisation with Vue.js and Chart.js"/>
<visualisation></visualisation>
</div>
</template>
<script>
/* eslint-disable */
import Visualisation from './components/Visualisation.vue';
import HelloWorld from './components/HelloWorld.vue'
export default {
name: 'app',
components: {
Visualisation,
HelloWorld
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
Run the app
Now, you can run your app with the following command:
yarn run serve
or
npm run serve
If you open your browser at the following location: localhost:8080
you’ll get a nice working data visualization app like mine:
Conclusion
I hope this tutorial has been useful to you and you’ll make use the knowledge you’ve acquired in an awesome app of yours. Feel free to grab the source code of the app here.
10 December 2018
by Ethiel Adiassa