Chartjs-How can i display my data in a chart using chart js

0👍

this is how you can do a basic iteration over your db and initialize label and data array

var db; // your db object
var your_labels, your_data;  //array that will hold your data

db.forEach((item, index, arr) => {
  your_labels.push(item.product);
  your_data.push(item.stock);
})

pass them in your chart in initialization like this.

var myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: your_labels, 
                datasets: [{
                    label: '# of stocks',
                    data: your_data, 
                    backgroundColor: color_background_array,  //create them the same way. with same size 1 for each data entry
                    borderColor: color_border_array,
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            beginAtZero:true
                        }
                    }]
                }
            }
        });

The important point to remember is you will have to see the iteration of db object on your own. I don’t know what and how are you using it.

Leave a comment