score:1

the only way i have found to do this and some other customisation on the legend is to implement the legend item interface.

the accepted answer here in combination with the legend item interface should be a good start.

here's a jsfiddle that i made from that answer, but updated for chartjs 3.7.1. the main part to focus on is this:

options: {
plugins: {
  legend: {
    labels: {
      usepointstyle: true,

      generatelabels: function(chart) {
        var data = chart.data;
        if (data.labels.length && data.datasets.length) {
          return data.labels.map(function(label, i) {
          
            var meta = chart.getdatasetmeta(0);
            var ds = data.datasets[0];
            return {
              text: label,
              fillstyle: ds.backgroundcolor[i],
              strokestyle: ds.legendoutline[i],
              linewidth: ds.borderwidth,
              hidden: isnan(ds.data[i]) || meta.data[i].hidden,
              index: i
            };
          });
        } else {
          return [];
        }
      }
    }
  }
}

strokestyle is the outline property, and i've made a field in the dataset called legendoutline. legendoutline and the data from the dataset are both referenced with i, so the legendoutline[i] points to the label representing data[i]


Related Query

More Query from same tag