gpt4 book ai didi

angular - 我如何在 Angular 4 中放置 Google 图表

转载 作者:太空狗 更新时间:2023-10-29 17:10:06 24 4
gpt4 key购买 nike

如何将 google 图表集成到 angular 4 应用程序中?

我阅读了 SO 问题的答案 here ,但我认为它是不完整的。基本上,我正在尝试与之前的答案相同的策略,使用 GoogleChartComponent 和另一个扩展它的组件。出现两个错误,第一个是子组件缺少对 super() 的调用,第二个是此代码中对“new”的调用

  createBarChart(element: any): any {
return new google.visualization.BarChart(element);
}

我收到错误“google.visualization.BarChart 不是构造函数”。

我看到其中一条评论还提到了 <ng-content> 的使用用于数据投影,但没有明确概述。

在尝试提出一个“好”问题时,这是我的 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);
}
}

以及扩展它的子组件:

@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);
}
}

最佳答案

我相信有更好的方法可以将 Google Chart 集成到 Angular 4 中。库 ng2-google-charts已经有 GoogleChartComponent。 npm 页面的链接很好地描述了如何使用它。以下是组件示例:

import {Component, ViewEncapsulation, OnInit} from '@angular/core';
import {Component, ViewEncapsulation, OnInit} from '@angular/core';
import {Component, ViewEncapsulation, OnInit} from '@angular/core';
import {ViewChild} from '@angular/core';

import {GoogleChartComponent} from 'ng2-google-charts';

// needed for advanced usage of ng2-google-charts
import {HostListener} from '@angular/core';

@Component({
selector: '[your-widget]',
templateUrl: './your-widget.html',
encapsulation: ViewEncapsulation.None
})
export class YourWidget implements OnInit {

// type GoogleChartComponent and import for it can be ommited
@ViewChild('your_chart') chart: GoogleChartComponent;

// shows spinner while data is loading
showSpinner: boolean;

public chartData = {
chartType: 'AnyChartType', // your type
dataTable: [
['Col1', 'Col2']
],
options: {}
};

constructor() {
}

ngOnInit(): void {

this.showSpinner = true;
this.yourService.getData()
.subscribe((data) => {
//this.data = data;
this.processYourData();
this.showSpinner = false;
});
}

private processYourData() {
}

// advanced usage of chart
// in this case redraw function on window:resize event
@HostListener('window:resize', ['$event'])
onWindowResize(event: any) {
// console.log(event.target.innerWidth);
// Make sure you don't call redraw() in ngOnInit()
// - chart would not be initialised by that time, and
// - this would cause chart being drawn twice
this.chart.redraw();
}
}

你的标记看起来像这样:

<header class="widget-handle">
<h5>Widget Title</h5>
<div class="widget-controls">
<a title="Refresh">
<i *ngIf="showSpinner" class="fa fa-refresh fa-spin"></i>
</a>
</div>
</header>

<div class="widget-body">
<google-chart #your_chart [data]="chartData" *ngIf="!showSpinner">
</google-chart>
</div>

关于angular - 我如何在 Angular 4 中放置 Google 图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45044101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com