I get all the values of expirationdate and price in the loop. How do I write them to a variable and how do I specify them in labels: [],data: []?
success: function (data) {
if (data.result) {
for (let i = 0; i < data.result.length; i++) {
console.log(data.result[i].expirationdate);
console.log(data.result[i].price);
}
var ctx = document.getElementById("myChart").getContext("2d");
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: [{
label: 'Payment',
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: [],
}]
},
options: {}
});
}
}
1 Answer 1
This should work:
success: function (data) {
if (data.result) {
var expirationdates = [];
var prices = [];
for (let i = 0; i < data.result.length; i++) {
labels.push(data.result[i].expirationdate);
prices.push(data.result[i].price);
}
var ctx = document.getElementById("myChart").getContext("2d");
var chart = new Chart(ctx, {
type: 'line',
data: {
labels: expirationdates,
datasets: [{
label: 'Payment',
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: prices,
}]
},
options: {}
});
}
}
However, I can't test it without an example of the data object.
answered May 7, 2021 at 10:32
LaytonGB
1,4041 gold badge10 silver badges23 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
LaytonGB
Edited to make the assignment much clearer.
lang-js
expirationdateto fill thelabelsproperty, andpriceto fill thedataproperty? If not, please edit your question to better explain.