【发布时间】:2020-11-23 08:50:45
【问题描述】:
问题来了: 我正在尝试从我的 api 动态调用数据以将其放入我的 chart.js 中。 Chart.js 需要 Json 才能工作,当我向它发送本地 Json 时,一切正常,但是当我尝试从 api 向它发送 json 时,我遇到了困难...... 下面的代码:
Ts:
import {Component, OnDestroy, OnInit} from '@angular/core';
import {NbColorHelper, NbThemeService} from '@nebular/theme';
import {Planet} from '../../../../../@core/models/planet.model';
import {PlanetService} from '../../../../../@core/services/planet.service';
@Component({
selector: 'ngx-chart-comparatif',
templateUrl: './chart-comparatif.component.html',
styleUrls: ['./chart-comparatif.component.scss'],
})
export class ChartComparatifComponent implements OnInit, OnDestroy {
data: any;
options: any;
themeSubscription: any;
mercure: Array<Planet>;
mercureJson: any = <Planet> {}; // I think the problem is from here.I've tried lots of solutions but none of them work
jsonLocal = {'posi': 5, 'size': 8, 'long': 8}; // Json local for tests (it works correctly)
constructor(private theme: NbThemeService,
private planetService: PlanetService) {
this.themeSubscription = this.theme.getJsTheme().subscribe(config => {
const colors: any = config.variables;
const chartjs: any = config.variables.chartjs;
this.data = {
labels: ['Temperature Maximum', 'Temperature Moyenne', 'Temperature Minimum'],
datasets: [{
data: [this.mercureJson.tempMin, this.mercureJson.tempMoy, this.mercureJson.tempMax], // test
label: 'Mercure',
backgroundColor: 'rgba(143, 155, 179, 0.24)',
borderColor: '#c5cee0',
}, {
data: [this.mercureJson.tempMin, this.mercureJson.tempMoy, this.mercureJson.tempMax], // test
label: 'Venus',
backgroundColor: 'rgba(255, 170, 0, 0.24)',
borderColor: '#ffaa00',
}, {
data: [this.mercureJson.tempMin, this.mercureJson.tempMoy, this.mercureJson.tempMax], // test
label: 'Terre',
backgroundColor: 'rgba(0, 149, 255, 0.48)',
borderColor: '#0095ff',
}, {
data: [this.mercureJson.tempMin, this.mercureJson.tempMoy, this.mercureJson.tempMax], // test
label: 'Mars',
backgroundColor: 'rgba(0, 214, 143, 0.24)',
borderColor: '#00d68f',
},
],
};
this.options = {
tooltips: {
enabled: true,
mode: 'single',
position: 'nearest',
callbacks: {
label: function (tooltipItems) {
return tooltipItems.yLabel + ' c°';
},
},
},
responsive: true,
maintainAspectRatio: false,
scales: {
xAxes: [
{
gridLines: {
display: true,
color: chartjs.axisLineColor,
},
ticks: {
fontColor: chartjs.textColor,
},
},
],
yAxes: [
{
gridLines: {
display: true,
color: chartjs.axisLineColor,
},
ticks: {
fontColor: chartjs.textColor,
},
},
],
},
legend: {
labels: {
fontColor: chartjs.textColor,
},
},
};
});
}
getPlanet() {
this.planetService.getPlanets().subscribe(res => {
this.mercure = res.data;
this.mercure = this.mercure.filter(e =>
e.name === 'Mercure'); // the mercure array is filtered for the planet mercury only
const format = JSON.stringify(this.mercure); // Format Json
this.mercureJson = format;
// tslint:disable-next-line:no-console
console.log('test', this.mercure, 'test2', this.mercureJson );
});
}
ngOnInit() {
this.getPlanet();
}
ngOnDestroy(): void {
this.themeSubscription.unsubscribe();
}
}
HTML:
<h5 class="text-center">Température des planètes tellurique</h5>
<chart type="line" [data]="data" [options]="options"></chart>
资源控制台:
如您所见,控制台中的所有内容(对象和 Json)都清晰可见,并且没有显示错误。 但是图表是空的... 有没有人有想法? 谢谢
【问题讨论】:
-
除了 async 变量及其用法的问题外,尝试
this.mercure.filter()表明传入的数据是 JS 数组而不是对象。此外,您正在使用JSON.stringify序列化数组。它将使数组成为字符串,因此表达式this.mercureJson.tempMoy和this.mercureJson.tempMax无效。请附上控制台日志完整输出的屏幕截图。 -
屏幕正下方
标签: node.js angular mongodb charts chart.js