Chartjs-Inserting an array in Chart.JS

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.

Leave a comment