Create a Pie Chart in NuxtJS

Teri Eyenike

Data visualization organizes complex, unorganized datasets in an organized and visual way that is easy for the users to understand. Presenting data comes in charts - bar, line, bubble, doughnut, etc.

In this article, you will learn how to build the graphical representation of a pie chart using provided datasets with the JavaScript library, Chart.js, to visualize the data.

Nuxt.js is a framework built upon Vue.js that makes building frontend web interfaces performant and powerful.

Sandbox

The completed project demo is in a CodeSandbox. Fork it and run the code to get started.

For your reference, check the source code:

Prerequisites

Understanding this article requires the following:

  • Basic knowledge of Vue.js
  • Installation of Node.js v10 and above on your local machine

Setup a Nuxt.js Project

The installation for a new Nuxt.js project begins with the Nuxt.js command-line interface (CLI), which will scaffold the files and folders with a list of pre-defined options to select.

Run the following command in your terminal:

1npx create-nuxt-app nuxtjs-pie-chart

The above command interface should look like this:

To confirm the setup, run the development server, which is accessible on http://localhost:3000, with the following command:

1cd nuxtjs-pie-chart
2
3npm run dev

Finally, install the JavaScript library Chart.js, which is necessary for creating the pie chart for the visualized data.

Run this command:

1npm install chart.js

chart.js - A simple yet flexible JavaScript charting library

Creating a Pie Chart

Before you create the chart, in the created components folder, copy and paste the following code in the PieChart.vue file:

1// components/PieChart.vue
2
3<template>
4 <div>
5 <canvas id="pieChart"></canvas>
6 </div>
7</template>

The <canvas> element with an id piechart is a single node that will render the chart and give it its container for responsiveness.

Next, create another file, data.js, in a data folder at the root of the project directory that will contain the following code.

1// data/data.js
2
3 const pieChartData = {
4 type: "pie",
5 data: {
6 labels: ["Remote only", "Hybrid", "Office"],
7 datasets: [
8 {
9 data: [59.7, 35.3, 5],
10 backgroundColor: [
11 "rgb(46,176,250)",
12 "rgb(240,163,196)",
13 "rgb(179,185,201)",
14 ],
15 hoverOffset: 5,
16 },
17 ],
18 },
19 options: {
20 responsive: true,
21 plugins: {
22 legend: {
23 position: "bottom",
24 },
25 title: {
26 display: true,
27 text: "How does your work look like now?",
28 },
29 },
30 },
31 };
32 export default pieChartData;

In the code block above, the different properties specified in the declared variable are as follows:

  • Chart type is declared a value of pie. Changing the value will display a different chart
  • Array of labels is specified in the data object to show the tooltips in the legend, datasets with properties such as the data points, background color for the arc, and the hoverOffset property that moves the arc when hovered
  • options object defines the chart's responsiveness, its plugins with the legend set to the bottom of the pie chart, and the title set to true to display the chart's details

Rendering the Pie Chart

In the PieChart.vue component, copy and paste the following code:

1// components/PieChart.vue
2
3 <!-- template for the canvas container -->
4
5 <script>
6 import { Chart, registerables } from "chart.js";
7 import pieChartData from "@/data/data";
8 export default {
9 data() {
10 return {
11 pieChartData,
12 };
13 },
14 mounted() {
15 const ctx = document.getElementById("pieChart");
16 Chart.register(...registerables);
17 new Chart(ctx, this.pieChartData);
18 },
19 };
20 </script>

The code above does the following:

  • Imports the pieChartData properties and passes in the reactive data in the data property used by the Vue.js app
  • mounted() lifecycle hook is called after the app is mounted on the DOM, using this method, Chart.js is integrated and registered with the new initialized chart with the pieChartData and the variable ctx containing the canvas id

Displaying the Pie Chart

The last step of this tutorial is to display the pie chart in the browser. In the index.vue, import the PieChart component:

1// pages/index.vue
2
3 <template>
4 <div class="flex items-center justify-center h-screen">
5 <PieChart />
6 </div>
7 </template>

From the code snippet above, Tailwind CSS classes are used for styling to display the chart's layout in the center of the browser using CSS flexbox layout properties.

The final result of the page should look like this:

Conclusion

This post explains the steps in creating a pie chart to visualize data intuitively and interactively.

Resource

Teri Eyenike

Software Developer

Teri Eyenike is a software developer interested in making things simple and usable.