score:1

Accepted answer

given the data is available in the variable health, you can extract the labels through object.keys() as follows.

labels: object.keys(health),

the data of individual datasets can be extracted through object.values(), followed by array.map(). the data of the first dataset for example is defined as follows.

data: object.values(health).map(v => v[0]),

please have a look at your amended and runnable code below.

const health = {
  "2020": [1, 2, 3],
  "2021": [4, 5, 6]
}

var mylinechart = new chart('mychart', {
  type: 'line',
  data: {
    labels: object.keys(health),
    datasets: [{
        label: "pass",
        data: object.values(health).map(v => v[0]),
        backgroundcolor: 'rgba(71,193,28, 0.71)',
        bordercolor: 'rgb(71,193,28)',
        fill: false
      },
      {
        label: "failed",
        data: object.values(health).map(v => v[1]),
        backgroundcolor: 'rgba(212,0,13,0.71)',
        bordercolor: 'rgb(212,0,13)',
        fill: false
      },
      {
        label: "skipped",
        data: object.values(health).map(v => v[2]),
        backgroundcolor: 'rgba(228,78,231,0.56)',
        bordercolor: 'rgb(228,78,231)',
        fill: false
      }
    ]
  },
  options: {
    responsive: true,
    scales: {
      yaxes: [{
        ticks: {
          beginatzero: true,
          stepsize: 1
        }
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.3/chart.min.js"></script>
<canvas id="mychart" height="80"></canvas>


Related Query

More Query from same tag