[Chartjs]-How to set color for each datapoint in chartjs-chart-treemap

1👍

To control the colors assigned to each rectangle (datapoint) in the treemap. The index for the dataset array is buried in ctx.raw._data._idx. So you can create your own array of colors and assign each datapoint a specific color with:

  datasets.backgroundColor: (ctx)=>this.getColor(ctx)

in getColor

   getColor(ctx) { 
   const colors = [arrray of colors];
   if (ctx.type==='data') {
      return colors[ctx.raw._data._idx];  
   }
  else return 'transparent';
}

Leave a comment