Here is how I solved it.
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);
}
}
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>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…