How to Add and Use Google Line Chart in Vue Js App

Last updated on: by Editorial Team

Vue Js Google line chart tutorial; this agile guide will help you understand how to integrate the Google line chart in the Vue js application using the notable vue-google-charts package from absolute scratch.

Imagine you have a requirement to track the more minor changes; specifically, you have to keep an eye on changes that occur over a short or long period.

What will be the pragmatic solution?

In the UI world, the table plays a vital role in displaying data more eloquently; however, tables are not enough to deal with a specific type of problem. You can truly rely on Google charts for data visualization purposes.

Google charts offer an agile way to visualize data on your site, not just line charts, but it also provides tons of other charts or graphs that efficiently help you deal with data visualization.

This quick tutorial will explain every process that helps you implement a google line chart in the Vue js application. Moreover, we will cover how to set up the vue development environment, add or install the google chart package in vue, and create reusable vue google chart components.

Vue Js Google Line Chart Integration Example

  • Step 1: Create Vue Environment
  • Step 2: Create Vue Js Project
  • Step 3: Add Google Chart Library
  • Step 4: Register New Component
  • Step 5: Implement Google Line Chart in Vue
  • Step 6: Start Vue Application

Create Vue Environment

First, execute the command to install the Vue command-line interface on your system.

npm install -g @vue/cli

Create Vue Js Project

Once the vue cli is installed, you are ready to download the Vue app.

vue create vue-charts-app

After installing the app, move to the project folder.

cd vue-charts-app

This step is optional, but if you want to create some user interface module with more agility, then the Bootstrap package is for you.

npm install bootstrap

Next, add the bootstrap CSS path in the src/main.js file.

import Vue from 'vue'
import App from './App.vue'

// Import Bootstrap
import 'bootstrap/dist/css/bootstrap.css'

new Vue({
  render: h => h(App),
}).$mount('#app')

Add Google Chart Package

Next, use the following command to install the vue google charts plugin in the vue app.

npm install vue-google-charts

Create and Register New Component in App

In this section, we will create a new component, so head over to the components folder and create GoogleChart.vue file, then add the following code in the file.

Also, import the GChart from the “vue-google-charts” package and register the GChart module in the components object.

<template>
  <div>
    <h2>Add Google line chart in Vue Js</h2>
  </div>
</template>
 
<script>
import { GChart } from "vue-google-charts";
export default {
  name: "App",
  components: {
    GChart
  },
  data() {
    return {
  
    };
  }
};
</script>

Next, add the GoogleChart component to the src/App.vue file.

<template>
  <div class="container mt-5 text-center">
    <GoogleChart />
  </div>
</template>

<script>
import GoogleChart from './components/GoogleChart.vue'

export default {
  name: 'App',
  components: {
    GoogleChart
  }
}
</script>

Implement Google Line Chart in Vue

The GChart directive helps to display the chart, pass the options to GChart syntax to configure the line chart; similarly, the data property holds the data that needs to be shown in the line chart.

Next, open and update the src/components/GoogleChart.vue file with the suggested code.

<template>
  <div>
    <h2>Vue Js Google line Charts Demo</h2>
    <GChart
      type="LineChart"
      :options="options"
      :data="collectionData"
    />    
  </div>
</template>
 
<script>
import { GChart } from "vue-google-charts";
export default {
  name: "App",
  components: {
    GChart
  },
  data() {
    return {
      collectionData: [
        ["Day", "Guardians of the Galaxy", "The Avengers", "Transformers"],
        [1,  40.45, 90.5, 42.8],
        [2,  22.5, 68.4, 33.4],
        [3,  36.5, 47, 55.5],
        [4,  12.7, 23.8, 14.5],
        [5,  10.9, 44.5, 10.3],
        [6,   7.8, 14.5,  6.7],
        [7,   8.6, 11.2,  19.6]
      ],
      options: {
        chart: {
          title: "First 7 days movies collection",
          subtitle: "In millions of dollars (USD)"
        },
        width: 1000,
        height: 400
      }
    };
  }
};
</script>

Start Vue Application

Start the app executing the npm run serve command and view the app using the following url in the browser.

npm run serve
http://localhost:8080

Vue Js Google Line Chart

Conclusion

Data visualization is an eloquent work, and Google charts understand the importance of it. In this example, we comprehended the nuances of google line chart implementation in vue with example.

We hope this tutorial showed you the right and profound path and will undoubtedly help you integrate google charts in vue with agility; thanks for reading.