Charts.js: how to get the legend colors of the labels

1👍

Okey you can intercept the automated legend label creation call, and save the results somewhere before returning it, which gives you the colors.

change your labels block to this, and you can see in the console log how it looks:


    legend: {
      labels: {
        fontColor: 'white', //set your desired color
        generateLabels: (ctx) => {
          var labels = Chart.defaults.global.legend.labels.generateLabels(ctx);
          console.log('created labels', labels);
          return labels;
        }
      }
    },

Leave a comment