[Chartjs]-How do I display chart on my HTML page based on JSON data sent by Node JS

2👍

You are creating a new Chart() every time through your $.each() loop.

Your logic goes like this:

for each (key, value) in res:
  create a new Chart containing just this (key, value)

You almost certainly want this:

create empty arrays myLabels[] and myData[]

for each (key, value) in res:
  add key to myLabels[]
  add value to myData[]

then
  create one (and only one) new Chart using myLabels[] and myData[]

Your data property for new Chart() will then look like this:

data: {
  labels: myLabels,
  datasets: [{
    label: 'Labels',
    data: myData,

    backgroundColor: [
      'rgba(75, 192, 192, 0.2)',
      'rgba(255, 99, 132, 0.2)'
    ],

    borderColor: [
      'rgba(75, 192, 192, 1)',
      'rgba(255,99,132,1)'
    ],
    borderWidth: 1
  }]
}

Leave a comment