Pass objects from javascript to Chart.JS through EJS

0👍

Data is an array of objects. You can access the emp_id in following manner using backtick:

labels: `<%= data[0].emp_id %>`, `<%= data[1].emp_id %>`, ... , `<%= data[n].emp_id %>`

you can also push the emp_ids into another array and then use that array as labels.

var Data = JSON.parse(`<%- JSON.stringify(data) %>`);
var labelsData = [];
for(var i = 0; i < Data.length; i++){
   labelsData.push(Data[i].emp_id);
}

let pchart = new Chart(myChart, {
    type:'bar',
    data:{
      labels: labelsData,
      datasets:[{
        label:'p',
        data:[123,234,533,232]
      }]
    }
})

Leave a comment