How to Create Pie Chart in Vue 3 using vue-chartjs

Websolutionstuff | Jul-29-2024 | Categories : VueJs

Hello, web developers! In this article, we'll see how to create a pie chart in vue 3 using vue-chartjs. Here, we'll install vue-chartjs and chart.js for dynamic pie charts in vue 3. vue-chartjs is a wrapper for Chart.js in Vue. You can easily create reusable chart components.

You can install it using the pnpm, yarn, and npm commands. It provides every chart type that is available in Chart.js is exported as a named component.

Create Pie Chart in Vue 3 using vue-chartjs

Create Pie Chart in Vue 3 using vue-chartjs

 

Step 1: Install Vue 3

In this step, we'll install vue 3 using the following command.

npm init vue@latest
cd your-project-name
npm install

Then, we'll install vue-chartjs and chart.js dependency using the following command.

npm install vue-chartjs chart.js

 

Step 2: Create the Necessary Files

Next, we'll create a component and import a Line chart. Also, import the line chart configuration from the chartConfig.js file.
src/App.vue

<template>
  <Pie :data="data" :options="options" />
</template>

<script lang="ts">
import { Chart as ChartJS, ArcElement, Tooltip, Legend } from 'chart.js'
import { Pie } from 'vue-chartjs'
import * as chartConfig from './chartConfig.js'

ChartJS.register(ArcElement, Tooltip, Legend)

export default {
  name: 'App',
  components: {
    Pie
  },
  data() {
    return chartConfig
  }
}
</script>

src/chartConfig.js

export const data = {
  labels: ['VueJs', 'EmberJs', 'ReactJs', 'AngularJs'],
  datasets: [
    {
      backgroundColor: ['#34495e', '#d1dce6', '#597e9f', '#39516b'],
      data: [60, 20, 50, 30],
    },
  ],
};

export const options = {
  responsive: true,
  maintainAspectRatio: false,
};

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script type="module" defer src="./index.ts"></script>
    <title>How to Create Pie Chart in Vue 3 using vue-chartjs - Websolutionstuff</title>
  </head>
  <body>
    <div id="app"></div>
  </body>
</html>

 

Step 3: Run Vue 3 Application

Now, run the vue 3 application using the following command.

npm run dev

Output:

create_pie_chart_in_vue_3_using_vue_chart_js

 


You might also like:

Recommended Post
Featured Post
Node.js MySQL Create Database
Node.js MySQL Create Database

In this tutorial we will see Node.js MySQL Create Database. For any kind data store or run query then we need database l...

Read More

Sep-22-2021

Laravel 11 Livewire Sweetalert Example
Laravel 11 Livewire Sweetalert...

Hello, laravel web developers! In this article, we'll see how to add sweetalert in laravel 11 Livewire. In laravel 1...

Read More

May-29-2024

How To Open Datepicker Popup In Angular 15 Material
How To Open Datepicker Popup I...

In this tutorial, I will guide you through the process of opening the datepicker popup in the Angular 15 Material framew...

Read More

Jul-10-2023

How To Import CSV File In MySQL Using Node.js
How To Import CSV File In MySQ...

In this tutorial we will see how to import CSV file in MySQL using Node.js. Import and export CSV/EXCEL file in Nod...

Read More

Jul-30-2021