Skip to content

Commit

Permalink
Add the ability to change font color per legend item (#8800)
Browse files Browse the repository at this point in the history
  • Loading branch information
etimberg committed Apr 3, 2021
1 parent 74f1188 commit 238bfa5
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/configuration/legend.md
Expand Up @@ -90,6 +90,9 @@ Items passed to the legend `onClick` function are the ones returned from `labels
// Fill style of the legend box
fillStyle: Color,

// Text color
fontColor: Color,

// If true, this item represents a hidden dataset. Label will be rendered with a strike-through effect
hidden: boolean,

Expand Down
9 changes: 6 additions & 3 deletions src/plugins/plugin.legend.js
Expand Up @@ -300,8 +300,6 @@ export class Legend extends Element {
ctx.textAlign = rtlHelper.textAlign('left');
ctx.textBaseline = 'middle';
ctx.lineWidth = 0.5;
ctx.strokeStyle = fontColor; // for strikethrough effect
ctx.fillStyle = fontColor; // render in correct colour
ctx.font = labelFont.string;

const {boxWidth, boxHeight, itemHeight} = getBoxSize(labelOpts, fontSize);
Expand Down Expand Up @@ -381,6 +379,10 @@ export class Legend extends Element {

const lineHeight = itemHeight + padding;
me.legendItems.forEach((legendItem, i) => {
// TODO: Remove fallbacks at v4
ctx.strokeStyle = legendItem.fontColor || fontColor; // for strikethrough effect
ctx.fillStyle = legendItem.fontColor || fontColor; // render in correct colour

const textWidth = ctx.measureText(legendItem.text).width;
const textAlign = rtlHelper.textAlign(legendItem.textAlign || (legendItem.textAlign = labelOpts.textAlign));
const width = boxWidth + (fontSize / 2) + textWidth;
Expand Down Expand Up @@ -631,7 +633,7 @@ export default {
// lineWidth :
generateLabels(chart) {
const datasets = chart.data.datasets;
const {labels: {usePointStyle, pointStyle, textAlign}} = chart.legend.options;
const {labels: {usePointStyle, pointStyle, textAlign, color}} = chart.legend.options;

return chart._getSortedDatasetMetas().map((meta) => {
const style = meta.controller.getStyle(usePointStyle ? 0 : undefined);
Expand All @@ -640,6 +642,7 @@ export default {
return {
text: datasets[meta.index].label,
fillStyle: style.backgroundColor,
fontColor: color,
hidden: !meta.visible,
lineCap: style.borderCapStyle,
lineDash: style.borderDash,
Expand Down
20 changes: 20 additions & 0 deletions test/specs/plugin.legend.tests.js
Expand Up @@ -62,6 +62,7 @@ describe('Legend block tests', function() {
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
lineCap: undefined,
lineDash: undefined,
Expand All @@ -76,6 +77,7 @@ describe('Legend block tests', function() {
}, {
text: 'dataset2',
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: true,
lineCap: undefined,
lineDash: undefined,
Expand All @@ -90,6 +92,7 @@ describe('Legend block tests', function() {
}, {
text: 'dataset3',
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
lineCap: undefined,
lineDash: undefined,
Expand Down Expand Up @@ -135,6 +138,7 @@ describe('Legend block tests', function() {
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
lineCap: 'round',
lineDash: [2, 2],
Expand All @@ -149,6 +153,7 @@ describe('Legend block tests', function() {
}, {
text: 'dataset2',
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: true,
lineCap: 'butt',
lineDash: [],
Expand All @@ -163,6 +168,7 @@ describe('Legend block tests', function() {
}, {
text: 'dataset3',
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
lineCap: 'butt',
lineDash: [],
Expand Down Expand Up @@ -215,6 +221,7 @@ describe('Legend block tests', function() {
expect(chart.legend.legendItems).toEqual([{
text: 'dataset3',
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
lineCap: 'butt',
lineDash: [],
Expand All @@ -229,6 +236,7 @@ describe('Legend block tests', function() {
}, {
text: 'dataset2',
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: true,
lineCap: 'butt',
lineDash: [],
Expand All @@ -243,6 +251,7 @@ describe('Legend block tests', function() {
}, {
text: 'dataset1',
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
lineCap: 'round',
lineDash: [2, 2],
Expand Down Expand Up @@ -300,6 +309,7 @@ describe('Legend block tests', function() {
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
lineCap: undefined,
lineDash: undefined,
Expand All @@ -314,6 +324,7 @@ describe('Legend block tests', function() {
}, {
text: 'dataset3',
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
lineCap: undefined,
lineDash: undefined,
Expand Down Expand Up @@ -370,6 +381,7 @@ describe('Legend block tests', function() {
expect(chart.legend.legendItems).toEqual([{
text: 'dataset3',
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
lineCap: 'butt',
lineDash: [],
Expand All @@ -384,6 +396,7 @@ describe('Legend block tests', function() {
}, {
text: 'dataset2',
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: true,
lineCap: 'butt',
lineDash: [],
Expand All @@ -398,6 +411,7 @@ describe('Legend block tests', function() {
}, {
text: 'dataset1',
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
lineCap: 'round',
lineDash: [2, 2],
Expand Down Expand Up @@ -529,6 +543,7 @@ describe('Legend block tests', function() {
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
lineCap: undefined,
lineDash: undefined,
Expand Down Expand Up @@ -571,6 +586,7 @@ describe('Legend block tests', function() {
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
fillStyle: 'rgb(50, 0, 0)',
fontColor: '#666',
hidden: false,
lineCap: undefined,
lineDash: undefined,
Expand Down Expand Up @@ -628,6 +644,7 @@ describe('Legend block tests', function() {
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
lineCap: undefined,
lineDash: undefined,
Expand All @@ -642,6 +659,7 @@ describe('Legend block tests', function() {
}, {
text: 'dataset2',
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
lineCap: undefined,
lineDash: undefined,
Expand Down Expand Up @@ -700,6 +718,7 @@ describe('Legend block tests', function() {
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
lineCap: undefined,
lineDash: undefined,
Expand All @@ -714,6 +733,7 @@ describe('Legend block tests', function() {
}, {
text: 'dataset2',
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
lineCap: undefined,
lineDash: undefined,
Expand Down
6 changes: 6 additions & 0 deletions types/index.esm.d.ts
Expand Up @@ -2002,6 +2002,12 @@ export interface LegendItem {
*/
fillStyle?: Color;

/**
* Font color for the text
* Defaults to LegendOptions.labels.color
*/
fontColor?: Color;

/**
* If true, this item represents a hidden dataset. Label will be rendered with a strike-through effect
*/
Expand Down

0 comments on commit 238bfa5

Please sign in to comment.