Javascript 用谷歌图表以Angular6显示动态数据
我使用AngularCli6、angularfire2和firebase。我想用谷歌图表创建一个时间线 //谷歌图表服务Javascript 用谷歌图表以Angular6显示动态数据,javascript,angular,typescript,charts,Javascript,Angular,Typescript,Charts,我使用AngularCli6、angularfire2和firebase。我想用谷歌图表创建一个时间线 //谷歌图表服务 declare var google: any; export class GoogleChartsBaseService { constructor() { google.charts.load('current', {'packages':["timeline"]}); } protected buildChart(data: any[], chartFu
declare var google: any;
export class GoogleChartsBaseService
{
constructor() { google.charts.load('current', {'packages':["timeline"]}); }
protected buildChart(data: any[], chartFunc: any, options: any) : void {
var func = (chartFunc, options) =>
{
var datatable = google.visualization.arrayToDataTable(data);
chartFunc().draw(datatable, options);
};
var callback = () => func(chartFunc, options);
google.charts.setOnLoadCallback(callback);
}
}
时间线图服务
import { GoogleChartsBaseService } from './google-charts-base.service';
import { Injectable } from '@angular/core';
import { GanttChartConfig } from './../models/GanttChartConfig.model';
declare var google: any;
@Injectable()
export class GoogleGanttChartService extends GoogleChartsBaseService {
constructor() { super(); }
public BuildPieChart(elementId: string, data: any[], config: GanttChartConfig) : void {
var chartFunc = () => { return new google.visualization.Timeline(document.getElementById(elementId)); };
var options = {
traitement: config.traitement,
datedebut: config.datedebut,
datefin: config.datefin,
};
this.buildChart(data, chartFunc, options);
}
}
Timeline.html
<div id="{{elementId}}" ></div>
以及要显示图形的组件:
Component.html
<div class="full"><app-gantt [data]="data1" [config]="config1" [elementId]="elementId1"></app-gantt></div>
现在,我可以在组件中写入数据,轻松显示图形
但我的数据存储在firebase中,如下所示:
我用angularfire2的可观察值显示数据
DATA.Service.TS
getPPSByPatientid(Patientid: string){
return this.database.list('/ppss', ref => ref.orderByChild("Patientid").equalTo(Patientid)).valueChanges();
}
我在我的组件.ts中尝试此操作,以便为this.data1提供良好的数组,但没有成功
Console.log(this.data1)
发送一个未定义的
let interestingFields = [ 'treatement','dateA', 'dateB'];
this.ppssToDisplay.subscribe(obj => {
this.data1 = [
interestingFields,
interestingFields.map(field => obj[field]),
];
console.log(this.data1);
});
Error:
core.js:1598 ERROR Error: Uncaught (in promise): Error: Not an array Error: Not an array
我想把我所有的代码都放进去,这样你就可以完全可视化我想做的事情
我的问题是:我是否选择了正确的解决方案,还是应该在模板中使用循环来填充图表
有没有人能给我看看这个声音,这样我就可以再睡一次(5天哈哈)看来你找到了正确的解决办法。将数据获取到
data1
时只有一个问题
为了使检索到的数据与硬写数据上的模式匹配,您必须从检索到的数据的角度对其进行循环:
this.ppssToDisplay.subscribe(ppsList => {
this.data1 = [
interestingFields,
...ppsList.map(pps => interestingFields.map(field => pps[field]))
];
});
就这样。那应该能解决你的问题
let interestingFields = [ 'treatement','dateA', 'dateB'];
this.ppssToDisplay.subscribe(obj => {
this.data1 = [
interestingFields,
interestingFields.map(field => obj[field]),
];
console.log(this.data1);
});
Error:
core.js:1598 ERROR Error: Uncaught (in promise): Error: Not an array Error: Not an array
this.ppssToDisplay.subscribe(ppsList => {
this.data1 = [
interestingFields,
...ppsList.map(pps => interestingFields.map(field => pps[field]))
];
});