Create a Bar Chart in NuxtJS

Tosin Moronfolu

Introduction

According to Wikipedia, a bar chart is a chart that presents categorical data using rectangular bars with heights or lengths proportional to the values that they represent. On one axis of the chart are the categories under comparison, while on the other is the measured value.

At the end of the article, we will have understood how to implement a bar chart into our Nuxt.js application.

Prerequisites

To follow along with the article, a basic understanding of Nuxt.js is required.

Repository

This article's source code is available on GitHub:

https://github.com/folucode/nuxtjs-barchart

Sandbox

The completed project is on CodeSandbox. Fork it and run the code.

# Project Setup

Node has to be installed on our computer to set up the Nuxt.js application. To install Node, we'll head over to the Nodejs website and follow the instructions to install the software compatible with our operating system.

We can verify the Node.js installation by running the command below:

1node -v
2v16.10.0 //node version installed

To create the Nuxt.js app, run the command below. It will automatically set up a boilerplate Nuxt.js app.

1$ npm init nuxt-app <project-name>
2# or
3$ yarn create nuxt-app <project-name>

After running the command, we see different options to configure our Nuxt.js app. Make sure to choose the options listed in the image above for each prompt.

  1. For Nuxt.js modules, we'll pick Axios as our option.
  2. For Linting tools, we'll pick Prettier as our option.
  3. For development tools, we'll pick jsconfig.json as our option.
  4. For GitHub username, make sure to use GitHub username.

After the installation is complete, change the directory to the app we just created:

1cd <app-name>

Run npm run dev or yarn dev to start the development server on http://localhost:3000.

Create Bar Chart

We'll be using an npm package called [Chart.js](https://www.npmjs.com/package/chart.js) to create the bar chart in our application. Install it using the command below:

1$ npm i chart.js

After installing the package, we'll create a new file called BarChart.vue in the components folder and then copy-paste the code below:

1<template>
2 <div>
3 <canvas id="bar-chart"></canvas>
4 </div>
5</template>
6<script>
7import { Chart, registerables } from 'chart.js';
8
9export default {
10 name: "BarChart",
11};
12</script>

In the code above, we create a Nuxt.js template and a parent div. Inside the parent div, we create a canvas element that will later be used to render our chart. Then we import the Chart.js package and export the BarChart component.

Next, we create a new folder called data at the root of our app, and inside it, create a file called chart-data.js. Inside the chart-data.js, paste the code below in it:

1const barChartData = {
2 type: "bar",
3 data: {
4 labels: ["Lions", "Monkeys", "Zebras", "Eagles", "Horses"],
5 datasets: [
6 {
7 label: "Dataset 1",
8 data: [12, 19, 3, 2, 3],
9 backgroundColor: [
10 "rgba(255, 99, 132, 0.2)",
11 "rgba(54, 162, 235, 0.2)",
12 "rgba(255, 206, 86, 0.2)",
13 "rgba(75, 192, 192, 0.2)",
14 "rgba(153, 102, 255, 0.2)",
15 "rgba(255, 159, 64, 0.2)",
16 ],
17 borderColor: [
18 "rgba(255, 99, 132, 1)",
19 "rgba(54, 162, 235, 1)",
20 "rgba(255, 206, 86, 1)",
21 "rgba(75, 192, 192, 1)",
22 "rgba(153, 102, 255, 1)",
23 "rgba(255, 159, 64, 1)",
24 ],
25 borderWidth: 1,
26 },
27 ],
28 },
29 options: {
30 responsive: true,
31 plugins: {
32 legend: {
33 position: "top",
34 },
35 title: {
36 display: true,
37 text: "Number of animals in the zoo",
38 },
39 },
40 },
41};
42
43export default barChartData;

In the code above:

  1. Chart type is set to bar in the configuration object
  2. Configurations are set in the data property and contains charts' labels, datasets used to define specific data to be shown, and background color to distinguish one dataset from another, also contains a border-color and border width property to set the border's thickness
  3. options object to define the responsiveness of the chart, positioning, and title

Rendering the Bar Chart

To render our bar chart, we will change the content of the BarChart.vue component and index.vue page.

  1. First, in the BarChart.vue component, we'll import the barChartData object from the chart-data file and then export the data property, which returns the barChartData object so we can use it in our component.

  2. Then export the mounted lifecycle method, and in it, we get the canvas element. All the chart components are registered in the mounted lifecycle method, and a new Chart object is initialized. The Chart object takes in two parameters; the canvas element we want to render the chart on and a configurations object, which in this case is barChartData. Vue calls the mounted hook when our component is added to the DOM.

See the completed BarChart.vue component code below:

1<template>
2 <div>
3 <canvas id="bar-chart"></canvas>
4 </div>
5</template>
6
7<script>
8import { Chart, registerables } from 'chart.js';
9import barChartData from "../data/chart-data";
10
11export default {
12 name: "BarChart",
13 data() {
14 return {
15 barChartData
16 }
17 },
18 mounted() {
19 const ctx = document.getElementById("bar-chart");
20 Chart.register(...registerables);
21 new Chart(ctx, this.barChartData);
22 }
23};
24</script>

Next, in the index.vue file, we will replace the boilerplate code with the code below:

1<template>
2 <BarChart />
3</template>
4
5<script>
6import BarChart from '../components/BarChart.vue';
7
8export default {
9 name: "IndexPage",
10 components: { BarChart }
11}
12</script>

We import the BarChart component and set it in between the template element. We then export our component name and the components object, which lists all the components we use in the index.vue file.

After successfully following the steps above, we run the command below in our terminal:

1$ npm run dev

The above command will spin up a development server on http://localhost:3000, which we can use to view the application. Check the image below to see the outcome:

We can hover over each bar to get more information about that section.

Conclusion

This article showed how to create bar charts in Nuxt.js applications using chart.js.

Resources

You may find these resources helpful:

Tosin Moronfolu

Software Engineer

I'm a software engineer with hands-on experience in building robust and complex applications improving performance and scalability. I love everything tech and science, especially physics and maths. I also love to read books, mainly non-fiction, and I love music; I play the guitar.