首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >离子3+角4+ chart.js -阵列加载数据

离子3+角4+ chart.js -阵列加载数据
EN

Stack Overflow用户
提问于 2017-10-31 15:16:01
回答 1查看 2.5K关注 0票数 1

这是我在"graf.ts“中的代码,我想要做的就是显示”数组“,即chart.js图中的浮点数数组,当我在图表”数据“中使用"this.testni”时,它会工作,但是当我使用" this.array“时,它就不起作用了,因为在下面的代码中,当我使用console.log this.array时,它会显示正常的数组,应该可以工作。此外,它在标签下工作-标签: this.lista_valuta。谢谢!

代码语言:javascript
运行
复制
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { HnbProvider } from '../../providers/hnb/hnb';
import { Chart } from 'chart.js';
import { ViewChild } from '@angular/core';


@Component({
  selector: 'page-graf',
  templateUrl: 'graf.html'
})
export class GrafPage {
  @ViewChild('lineCanvas') lineCanvas;

  barChart: any;

  tecaj: any;
  lista_valuta: string[] =[
    "AUD", "CAD", "CZK", "DKK", "HUF",
    "JPY", "NOK", "SEK", "CHF", "GBP",
    "USD", "EUR", "PLN"
  ];
  array = [];
  testni = [1, 2, 3, 4];

  datum_poc: Date;
  datum_kraj: Date;
  o_valuta: string;

  constructor(
    public navCtrl: NavController,
    private hnbProvider:HnbProvider
  ) {
  }

  ionViewDidLoad() {
  }


  dobavi_podatke(datum_poc: Date, datum_kraj: Date, o_valuta: string){
    this.hnbProvider.getTecajRazdoblje(datum_poc, datum_kraj).subscribe(tecaj => {
      this.tecaj = tecaj;
     });

     var i = 0;
     for (var key in this.tecaj) {
       if (this.tecaj[key].valuta == this.o_valuta){
        this.array[i] = this.tecaj[key].srednji_tecaj;
        i++;
       }   
     }
     this.array.forEach(element => {
      console.log(element);
     });

     this.lineChart = new Chart(this.lineCanvas.nativeElement, 
      {
                 type: 'line',
                 data: {
                     labels: this.lista_valuta,
                     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.array,
                             spanGaps: false,
                         }
                     ]
                 }
             });
     //console.log(this.tecaj);
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-31 15:47:55

这里的问题是使用async方法getTecajRazdoblje()。但是,您正在尝试填充数组的sync方式。所以你需要纠正这一点。试试这个:

代码语言:javascript
运行
复制
 this.hnbProvider.getTecajRazdoblje(datum_poc, datum_kraj).subscribe(tecaj => 
     {
       this.tecaj = tecaj;
       var i = 0;
       for (var key in this.tecaj) {
       if (this.tecaj[key].valuta == this.o_valuta){
        this.array[i] = this.tecaj[key].srednji_tecaj;
        i++;
       }   

      this.lineChart = new Chart(this.lineCanvas.nativeElement, 
       {
             type: 'line',
             data: {
                 labels: this.lista_valuta,
                 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.array,
                         spanGaps: false,
                     }
                 ]
             }
         });
       }
   });
  }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47038806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档