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.
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.
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.
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:
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.
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.
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.
//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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With