[Chartjs]-Map through datasets in chartjs

1๐Ÿ‘

โœ…

So this is how I ended up doing it

created an empty object structure that takes in the datasets

         let color = '#d6d6d6'
         let data = {
            labels:sortedData,
            datasets:[]
            }

crated a forEach function that loops through the years and push in data into the datasets

          years.forEach(function(a,i) {
            switch(i) {
              case 0:
                color='#D0D1E6'
                break;
              case 1:
                color='#74A9CF'
                break;
              case 2:
                color='#0570B0'
                break;
              case 3:
                color='#023858'
                break;
              default:
                color='#d6d6d6'
            }
                data.datasets.push({
                label: a,
                stack: 'Stack '+i,
                data: final_water_supply[i],
                backgroundColor: color,
                borderColor: color,
              })
            })

Leave a comment