【发布时间】:2021-07-26 01:28:54
【问题描述】:
我从 Firestore 中检索要用于 echarts 图表的数据。
最后得到seagulls: Observable<SeagullId[]>;
export interface Seagull { date: string; amount: number; name: string; }
export interface SeagullId extends Seagull { id: string; }
export class ZonesComponent implements OnInit {
private seagullCollection: AngularFirestoreCollection<Seagull>;
seagulls: Observable<SeagullId[]>;
constructor(private readonly afs: AngularFirestore) {
this.seagullCollection = afs.collection<Seagull>('mowe');
// .snapshotChanges() returns a DocumentChangeAction[], which contains
// a lot of information about "what happened" with each change. If you want to
// get the data and the id use the map operator.
this.seagulls = this.seagullCollection.snapshotChanges().pipe(
map(actions => actions.map(a => {
const data = a.payload.doc.data() as Seagull;
const id = a.payload.doc.id;
return { id, ...data };
}))
);
}
现在我需要将seagulls: Observable<SeagullId[]> 的每个元素转换为[date, value, name]。
硬编码示例:
series: [
{
type: 'themeRiver',
emphasis: {
itemStyle: {
shadowBlur: 20,
shadowColor: 'rgba(0, 0, 0, 0.8)'
}
},
data: [['2015/11/08',10,'Lachmöwe'],['2015/11/09',15,'Lachmöwe'],['2015/11/10',35,'Lachmöwe'],
['2015/11/11',38,'Lachmöwe'],['2015/11/12',22,'Lachmöwe'],['2015/11/13',16,'Lachmöwe']..
我的方法如下:
series: [
{
type: 'themeRiver',
emphasis: {
itemStyle: {
shadowBlur: 20,
shadowColor: 'rgba(0, 0, 0, 0.8)'
}
},
data: this.seagulls
.pipe(map(data => {
data.map(elem => {
return [elem.date, elem.amount, elem.name]
})
}))
}
]
但数据类型映射不正确...我如何使用seagulls: Observable<SeagullId[]> 作为我的数据源?
【问题讨论】:
标签: angular typescript google-cloud-firestore rxjs observable