Bar Chart - Custom Label Colors
See original GitHub issueHi, 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.
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:
- Created 7 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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 thebackgroundColor
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:
Hope this helps someone else.
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 😃