Angular2 + Google图表.如何在Angular2中集成Google图表? [英] Angular2 + Google Charts. How to integrate Google Charts in Angular2?
本文介绍了Angular2 + Google图表.如何在Angular2中集成Google图表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将Google Charts集成到我的Angular2应用程序中. 有什么办法做到这一点? 有没有可用的示例?
I want to integrate Google Charts in my Angular2 application. What is the way to do this? Is there any examples available?
我尝试加载该软件包,就像Google Charts被演示一样,但是加载时遇到了问题. 我尝试了angular2-google-chart ...,但没有设法使其正常工作.
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.
感谢您的帮助.
推荐答案
这是我解决的方法.
import { Component} from '@angular/core';
import { GoogleChartComponent} from './ChartComponent.js';
@Component({
selector: 'evolution',
template: `
<div class="four wide column center aligned">
<div id="chart_divEvolution" style="width: 900px; height: 500px;"></div>
</div>
`
})
export class EvolutionComponent extends GoogleChartComponent {
private options;
private data;
private chart;
constructor(){
console.log("Here is EvolutionComponent")
}
drawGraph(){
console.log("DrawGraph Evolution...");
this.data = this.createDataTable([
['Evolution', 'Imports', 'Exports'],
['A', 8695000, 6422800],
['B', 3792000, 3694000],
['C', 8175000, 800800]
]);
this.options = {
title: 'Evolution, 2014',
chartArea: {width: '50%'},
hAxis: {
title: 'Value in USD',
minValue: 0
},
vAxis: {
title: 'Members'
}
};
this.chart = this.createBarChart(document.getElementById('chart_divEvolution'));
this.chart.draw(this.data, this.options);
}
}
import { Component, OnInit} from '@angular/core';
declare var google:any;
@Component({
selector: 'chart'
})
export class GoogleChartComponent implements OnInit {
private static googleLoaded:any;
constructor(){
console.log("Here is GoogleChartComponent")
}
getGoogle() {
return google;
}
ngOnInit() {
console.log('ngOnInit');
if(!GoogleChartComponent.googleLoaded) {
GoogleChartComponent.googleLoaded = true;
google.charts.load('current', {packages: ['corechart', 'bar']});
}
google.charts.setOnLoadCallback(() => this.drawGraph());
}
drawGraph(){
console.log("DrawGraph base class!!!! ");
}
createBarChart(element:any):any {
return new google.visualization.BarChart(element);
}
createDataTable(array:any[]):any {
return google.visualization.arrayToDataTable(array);
}
}
现在您需要做的就是将其添加到index.html
Now all you need to do is add this to your index.html
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
这篇关于Angular2 + Google图表.如何在Angular2中集成Google图表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文