Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 + Google Charts. How to integrate Google Charts in Angular2?

I want to integrate Google Charts in my Angular2 application. What is the way to do this? Is there any examples available?

I tried to load the package like Google Charts is demoed but I had problems with the loading. I tried angular2-google-chart... but did not manage to get it to work.

Thanks for your help.

like image 263
BuckBazooka Avatar asked May 31 '16 09:05

BuckBazooka


People also ask

How do I integrate a chart in Google?

The most common way to use Google Charts is with simple JavaScript that you embed in your web page. You load some Google Chart libraries, list the data to be charted, select options to customize your chart, and finally create a chart object with an id that you choose.

How do I insert a Google graph into my website?

Click the Embed tab, and click the Publish button. You should now see an embeddable iframe code snippet. Copy this iframe code, and navigate to your website. Paste this code into an HTML/source view, and click save.

How do I add charts to my angular project?

From your project folder, run the following command to install chart.js: npm install chart.js @2.9.4 ng2-charts @2.4.2; Next, add chart.js to your Angular application by opening angular.json in your code editor and modifying it to include Chart.min.js:

What is ng2-charts in angular?

Chart.js is a popular JavaScript charting library and ng2-charts is a wrapper for Angular 2+ that makes it easy to integrate Chart.js in Angular. Let’s go over the basic usage.

What is @angular-Google-Charts?

angular-google-charts is a open source angular based wrapper for Google Charts to provides an elegant and feature rich Google Charts visualizations within an Angular application and can be used along with Angular components seamlessly.

Why add Google Maps to your angular application?

Adding Google Maps to your application can provide users with more contextual information than a street address or set of coordinates. Angular Google Maps are components that provide Angular applications with tools to utilize the Google Maps APIs.


Video Answer


1 Answers

//in index.html add
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>

33.5KB loaded from google servers to your app.

now add

declare var google:any;

to your component and add your google chart code in ngOnInit.

import { Component, OnInit } from '@angular/core';
declare var google:any;

@Component({
  selector: 'app-charts',
  templateUrl: './charts.component.html',
  styleUrls: ['./charts.component.css']
})
export class ChartsComponent implements OnInit {

constructor() { 

}

ngOnInit() {

  google.charts.load('current', {'packages':['corechart']});
  google.charts.setOnLoadCallback(drawChart);

  function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Task', 'Hours per Day'],
      ['Work',     11],
      ['Eat',      2],
      ['Commute',  2],
      ['Watch TV', 2],
      ['Sleep',    7]
    ]);

    var options = {
      title: 'My Daily Activities'
    };

    var chart = new google.visualization.PieChart(document.getElementById('piechart'));

    chart.draw(data, options);
  }
}

}

additional 35.8KB loaded from google servers when 'corechart' is loaded.

add this to your components html

<div id="piechart" style="width: 900px; height: 500px;"></div>

A better approach would be to use ViewChild instead of document.getElementById('piechart') in component ts file.

like image 59
George Itty Avatar answered Oct 19 '22 08:10

George Itty