question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bar Chart - Custom Label Colors

See original GitHub issue

Hi, as you can see in the image below and in this example on codepen, i can’t find a way to customize label’s background colors, when there are more colors for bars. example

Function generateLabels use “dataset.backgroundColor”, that in my case is an Array.

generateLabels: function(chart) {
    var data = chart.data;
    return helpers.isArray(data.datasets) ? data.datasets.map(function(dataset, i) {
        return {
            text: dataset.label,
            fillStyle: dataset.backgroundColor,
            hidden: !chart.isDatasetVisible(i),
            lineCap: dataset.borderCapStyle,
            lineDash: dataset.borderDash,
            lineDashOffset: dataset.borderDashOffset,
            lineJoin: dataset.borderJoinStyle,
            lineWidth: dataset.borderWidth,
            strokeStyle: dataset.borderColor,
            // Below is extra data used for toggling the datasets
            datasetIndex: i
        };
    }, this) : [];

Then, in draw() function, “fillStyle” property is used as value for ctx.fillStyle ctx.fillStyle = itemOrDefault(legendItem.fillStyle, Chart.defaults.global.defaultColor); But it can be only strings, CanvasGradients, or CanvasPatterns.

Is there any other way to customize label’s rect background?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

20reactions
cabdesignscommented, Jul 24, 2016

Unless I’ve misread, I think I’ve hit the same scenario; wanting to effectively create an “active” state on the data sets for one of the elements.

I achieved this, by first giving the “active” element a different backgroundColor on the data set. This only got me as far as the above issue - causing the legend fillStyle to be naively populated by the first rgb string in the backgroundColor array (inside dataset).

To get around this, I wrapped the default generateLabels function, and applied the specific fillStyle afterwards. Example below using a male and female dataset:

var MALE_BAR_COLOUR = 'rgba(0, 51, 78, 0.3)';
var MALE_BAR_ACTIVE_COLOUR = 'rgba(0, 51, 78, 1)';
var FEMALE_BAR_COLOUR = 'rgba(248, 142, 40, 0.3)';
var FEMALE_BAR_ACTIVE_COLOUR = 'rgba(248, 142, 40, 1)';

var ctx = $("#myChart");
var myChart = new Chart(ctx, {
  type: 'bar',
  data: {
    datasets: [{
      label: 'Male',
      data: [0, 0, 0, 0, 0],
      backgroundColor: MALE_BAR_COLOUR
    }, {
      label: 'Female',
      data: [0, 0, 0, 0, 0],
      backgroundColor: FEMALE_BAR_COLOUR
    }]
  },
  options: {
    legend: {
      labels: {
        generateLabels: function(chart) {
          labels = Chart.defaults.global.legend.labels.generateLabels(chart);
          labels[0].fillStyle = MALE_BAR_ACTIVE_COLOUR;
          labels[1].fillStyle = FEMALE_BAR_ACTIVE_COLOUR;
          return labels;
        }
      }
    }
  }
});

// Another block of code will update the dataset backgroundColor for the "active" element to X_BAR_ACTIVE_COLOUR

Hope this helps someone else.

1reaction
tyrex1975commented, Aug 16, 2016

Thanks cabdesigns - you just solved a similar problem to the one I was having.

My problem was that I wanted to highlight some of my dataset elements, however the legend labels always inherit the first colour specified in the dataset. So if I cannot highlight element 1 it will affect the legend colour too, even if all of the other elements are a different colour.

I wanted a solution where the legend colour was consistent regardless of the colour of the elements. Your code achieves this for me.

So, thank you 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chart.js label color - Stack Overflow
I'm using chart.js to create a bar chart and can't seem to change the label colors or the legend colors. I figured out...
Read more >
Custom Data Labels with Colors and Symbols in Excel ...
Custom Data Labels with Colors and Symbols in Excel Charts – [How To] ... Step 3: Click inside the formula bar, Hit “=”...
Read more >
Excel Custom Data Labels with Symbols that change Colors ...
In this tutorial we will learn how to format Data labels in Excel Charts to make them dynamically change their colors.
Read more >
How to change the color of mark labels according to the color ...
When we create a stacked bar chart with the default color palette, Tableau automatically assigns colors to the mark labels.
Read more >
Solved: Give and change color to axis lines and labels in
Is it possible to give / change colors to axis lines inside bar charts ... request [1] and elaborate in detail as to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found