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

Websolutionstuff | Jul-26-2024 | Categories : VueJs

Hello, web developers! In this article, we'll see how to create a line chart in vue 3 using vue-chartjs. Here, we'll install vue-chartjs and chart.js for dynamic line 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 Line Chart in Vue 3 using vue-chartjs

create line 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>
  <Line :data="data" :options="options" />
</template>

<script lang="ts">
import {
  Chart as ChartJS,
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend
} from 'chart.js'
import { Line } from 'vue-chartjs'
import * as chartConfig from './chartConfig.js'

ChartJS.register(
  CategoryScale,
  LinearScale,
  PointElement,
  LineElement,
  Title,
  Tooltip,
  Legend
)

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

src/chartConfig.js

export const data = {
  labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
  datasets: [
    {
      label: 'Data One',
      backgroundColor: '#f87979',
      data: [40, 39, 10, 40, 39, 80, 40]
    }
  ]
}

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 Line 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:

line_chart_in_vue_3_using_vue_chartjs

 


You might also like:

Recommended Post
Featured Post
Laravel 11 Send Email with Attachment
Laravel 11 Send Email with Att...

Hello, laravel web developers! In this article, we'll see how to send emails with attachments in laravel 11. In lara...

Read More

Sep-06-2024

Laravel 9 Livewire CRUD Operation
Laravel 9 Livewire CRUD Operat...

In this article, we will see the laravel 9 livewire crud operation. we will learn about livewire crud operation in larav...

Read More

Nov-24-2022

Datatables Expand/Collapse Columns
Datatables Expand/Collapse Col...

In this article, we will see how to expand/collapse columns in datatable. The Datatables API has a number of method...

Read More

Jun-05-2022

How To Disable Future Date In jQuery Datepicker
How To Disable Future Date In...

In this tutorial, we will see how to disable future dates in jquery datepicker. In the date picker, today's dat...

Read More

Jun-17-2022