How do you integrate google diagram in angular 4 app?
I read the answer to the SO question here , but I think it is incomplete. Basically, I am trying to use the same strategy as the previous answer using the GoogleChartComponent and another component that extends it. Two errors occur: the first is the lack of a call to super () for the child component, and the second is the call of the "new" in this code
createBarChart(element: any): any { return new google.visualization.BarChart(element); }
I get the error "google.visualization.BarChart is not a constructor."
I see that one comment also mentions the use of <ng-content> to project data, but this is clearly not outlined.
In an attempt to ask a “good” question, here is my GoogleChartComponent:
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': ['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); } }
And my child component that extends it:
@Component({ selector: 'app-bitcoin-chart', template: ` <div id="barchart_material" style="width: 700px; height: 500px;"></div> `, styles: [] }) export class BitcoinChartComponent extends GoogleChartComponent { private options; private data; private chart; // constructor() { // super(); // console.log('Bitcoin Chart Component'); // } drawGraph() { console.log('Drawing Bitcoin Graph'); this.data = this.createDataTable([ ['Price', 'Coinbase', 'Bitfinex', 'Poloniex', 'Kraken'], ['*', 1000, 400, 200, 500] ]); this.options = { chart: { title: 'Bitcoin Price', subtitle: 'Real time price data across exchanges', }, bars: 'vertical' // Required for Material Bar Charts. }; this.chart = this.createBarChart(document.getElementById('barchart_material')); this.chart.draw(this.data, this.options); } }