Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

228
Views
ChartJS different border color for legend

I need to set a different border color for the legend than the actual chart; whenever I add border color to the chart data, it gets applied to both legend and chart. And I couldn't find any options for setting different border colors for the legend in documentation

Here's a screenshot of what I'm trying to achieve, any idea?: enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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]

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!