Chartjs-Assigning data structures to chartjs properties, NOT individually

1👍

First you need to check if the xList, yList, rList and labelList have the same length otherwise obviously we can’t do this.

After that, we can generate the necessary data structure using a simple code like below:

labelList = ['China', 'Denmark', 'Germany', 'Japan']
xList = [21269017, 258702, 3979083, 4931877]
yList = [5.245, 7.526, 6.994, 5.921]
rList = [15, 10, 15, 15]

if ([labelList.length, xList.length, yList.length, rList.length].every((v, i, arr) => v == arr[0])) {
  datasets = [...Array(labelList.length).keys()].map(i => ({
    label: labelList[i],
    data: [{
      x: xList[i],
      y: yList[i],
      r: rList[i]
    }]
  }))

  console.log(datasets);
}

Leave a comment