I am trying to display a chart by passing in arrays of data to Chart.js. If I pass arrays, the chart does not render at all; it's as if the arrays are all empty. If I hard code the values into the location where the arrays are, the chart renders fine. I'm new to JavaScript/TypeScript... I found these similar questions, but they don't seem to work in my situation:
Array not working correctly in Chartjs
How to add datas to chart js from javascript array itself?
Here is my code. The chart does not render if I remove the hard coded arrays and un-comment the array variables.
buildChart() {
var momentArr = new Array(); var weightArr = new Array(); var bmiArr = new Array();
this.weightList.forEach(elements => {
elements.forEach(element => {
momentArr.push(element.moment);
weightArr.push(element.weight);
bmiArr.push(element.bmi);
});
})
console.log(momentArr);
console.log(weightArr);
console.log(bmiArr);
this.weightChart = new Chart(this.weightCanvas.nativeElement, {
type: 'line',
data: {
labels: [1500948156623,1500948161541], //momentArr,
datasets: [
{
label: 'Weight',
borderColor: "#3cba9f",
fill: false,
data: [300, 280] //weightArr
},
{
label: 'BMI',
borderColor: "#8e5ea2",
fill: false,
data: [150, 140] //bmiArr
}
]
},
options: {
scales: {
xAxes: [{
type: 'time',
time: {
displayFormats: {
quarter: 'MMM YYYY'
}
}
}],
yAxes: [{
type: 'linear',
ticks: { beginAtZero: false}
}]
}
}
});
}
FYI, weightList is a FirebaseListObservable.
console.logstatements look like? Is there a chance that the data inweightListis strings and not numbers?