Here is the problem: I'm trying to dynamically call data from my api to put it in my chart.js. Chart.js needs Json to work, when I send it a local Json everything works fine but when I try to send it a json from the api I have difficulty ... the code below:
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>
Res console:
As you can see everything is clearly visible in the console (object and Json) and no error is displayed. But the Chart is empty ... Does anyone have an idea ?? thank you

this.mercure.filter()suggests that the incoming data is a JS array and not an object. Moreover you're serializing the array usingJSON.stringify. It'll make the array a string, so expressionsthis.mercureJson.tempMoyandthis.mercureJson.tempMaxare invalid. Please include a screenshot of the complete output of the console log.