【问题标题】:How to plot Firebase data with Chartjs in angular如何使用 Chartjs 以角度绘制 Firebase 数据
【发布时间】:2018-10-16 07:34:14
【问题描述】:

我的 Firebase 数据库中有一个结构为 的基本数据。在我的 Angular 应用程序中,我想使用这些数据绘制图表。 这是我的代码:

    import { Component, ViewChild } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Chart } from 'chart.js';
import {AngularFireDatabase} from 'angularfire2/database'






@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
chart = []; 
arraytoPassChart = []

  @ViewChild('lineCanvas') lineCanvas;




  constructor(public navCtrl: NavController, private db: AngularFireDatabase) { }


  ionViewDidLoad() {

      this.db.list('/percentage')
        .valueChanges()
        .map(res => {
          // do some calculations here if you want to
          return res.map(eachlLabel => eachlLabel)
        })
        .subscribe(res => {
          console.log(res)//should give you the array of percentage. 
          this.arraytoPassChart.push(res);
        })

    this.chart = new Chart(this.lineCanvas.nativeElement, {

        type: 'line',
        data: {
          labels: ["January", "February", "March", "April", "May", "June", "July"],
          datasets: [
            {
              label: "My First dataset",
              fill: false,
              lineTension: 0.1,
              backgroundColor: "rgba(75,192,192,0.4)",
              borderColor: "rgba(75,192,192,1)",
              borderCapStyle: 'butt',
              borderDash: [],
              borderDashOffset: 0.0,
              borderJoinStyle: 'miter',
              pointBorderColor: "rgba(75,192,192,1)",
              pointBackgroundColor: "#fff",
              pointBorderWidth: 1,
              pointHoverRadius: 5,
              pointHoverBackgroundColor: "rgba(75,192,192,1)",
              pointHoverBorderColor: "rgba(220,220,220,1)",
              pointHoverBorderWidth: 2,
              pointRadius: 1,
              pointHitRadius: 10,
              data: this.arraytoPassChart,// my data here, work when I type an array myself
              spanGaps: false,
            }
          ]
        }

      });



    }}

this question,有人向我解释了从 firebase 检索数据的正确方法。在我的代码中,当我将结果记录到控制台时,我将数据作为一个数组,我想用 Chartjs 绘制图表。

console.log(res)//should give you the array of percentage.

但是当页面加载时它不显示数据。 但是当我自己输入数据而不是传递数组时它可以工作。

我真的很好奇我错过了什么,我怀疑 Chartjs 试图在它拥有数据之前绘制图表,但我也想不出解决这个问题的方法。如果有解决方案,我会很高兴。

【问题讨论】:

    标签: angular firebase ionic-framework firebase-realtime-database chart.js


    【解决方案1】:

    在订阅中构造图表,

    ionViewDidLoad() {
    
      this.db.list('/percentage')
        .valueChanges()
        .map(res => {
          // do some calculations here if you want to
          return res.map(eachlLabel => eachlLabel)
        })
        .subscribe(res => {
          console.log(res)//should give you the array of percentage. 
          this.arraytoPassChart.push(res);
          this.makeChart();
        })
    }
    
    makeChart() {
      this.chart = new Chart(this.lineCanvas.nativeElement, {
        type: 'line',
        data: {
          labels: ["January", ...],
        }
        ...
    }
    

    【讨论】:

    • makeChart(){... 返回“找不到名称 makeChart”
    • 你是否在组件上添加了 makeChart 作为方法?
    • 你不必这样做,只是更整洁一些。您可以将所有图表代码放在this.makeChart 所在的位置。
    • 重点是,您目前正在subscribe 之外构建图表,并且您需要在订阅内部进行,因为在订阅触发之前数据将不可用。
    • 查看@CozyAzure 对昨天问题的第二条评论you can do it in the subscribe callBack - 这适用于您在获取数据后所做的任何事情,这一切都必须在订阅中触发。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多