这是我在"graf.ts“中的代码,我想要做的就是显示”数组“,即chart.js图中的浮点数数组,当我在图表”数据“中使用"this.testni”时,它会工作,但是当我使用" this.array“时,它就不起作用了,因为在下面的代码中,当我使用console.log this.array时,它会显示正常的数组,应该可以工作。此外,它在标签下工作-标签: this.lista_valuta。谢谢!
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);
}
}发布于 2017-10-31 15:47:55
这里的问题是使用async方法getTecajRazdoblje()。但是,您正在尝试填充数组的sync方式。所以你需要纠正这一点。试试这个:
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,
}
]
}
});
}
});
}https://stackoverflow.com/questions/47038806
复制相似问题