Skip to content

Commit beb77e4

Browse files
authored
Sync Doughnut chart legend options to legend plugin (#12096)
Fixes #12060
1 parent 5feebdf commit beb77e4

File tree

2 files changed

+67
-19
lines changed

2 files changed

+67
-19
lines changed

src/controllers/controller.doughnut.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,25 @@ export default class DoughnutController extends DatasetController {
9191
labels: {
9292
generateLabels(chart) {
9393
const data = chart.data;
94+
const {labels: {pointStyle, textAlign, color, useBorderRadius, borderRadius}} = chart.legend.options;
9495
if (data.labels.length && data.datasets.length) {
95-
const {labels: {pointStyle, color}} = chart.legend.options;
96-
9796
return data.labels.map((label, i) => {
9897
const meta = chart.getDatasetMeta(0);
9998
const style = meta.controller.getStyle(i);
10099

101100
return {
102101
text: label,
103102
fillStyle: style.backgroundColor,
104-
strokeStyle: style.borderColor,
105103
fontColor: color,
104+
hidden: !chart.getDataVisibility(i),
105+
lineDash: style.borderDash,
106+
lineDashOffset: style.borderDashOffset,
107+
lineJoin: style.borderJoinStyle,
106108
lineWidth: style.borderWidth,
109+
strokeStyle: style.borderColor,
110+
textAlign: textAlign,
107111
pointStyle: pointStyle,
108-
hidden: !chart.getDataVisibility(i),
109-
112+
borderRadius: useBorderRadius && (borderRadius || style.borderRadius),
110113
// Extra data used for toggling the correct item
111114
index: i
112115
};

test/specs/global.defaults.tests.js

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,81 @@ describe('Default Configs', function() {
1414
},
1515
});
1616

17-
var expected = [{
18-
text: 'label1',
19-
fillStyle: 'red',
17+
var expectedCommon = {
2018
fontColor: '#666',
2119
hidden: false,
22-
index: 0,
2320
strokeStyle: '#000',
2421
textAlign: undefined,
2522
lineWidth: 2,
26-
pointStyle: undefined
23+
pointStyle: undefined,
24+
lineDash: [],
25+
lineDashOffset: 0,
26+
lineJoin: undefined,
27+
borderRadius: undefined,
28+
};
29+
30+
var expected = [{
31+
text: 'label1',
32+
fillStyle: 'red',
33+
index: 0,
34+
...expectedCommon,
2735
}, {
2836
text: 'label2',
2937
fillStyle: 'green',
30-
fontColor: '#666',
31-
hidden: false,
3238
index: 1,
33-
strokeStyle: '#000',
34-
textAlign: undefined,
35-
lineWidth: 2,
36-
pointStyle: undefined
39+
...expectedCommon,
3740
}, {
3841
text: 'label3',
3942
fillStyle: 'blue',
43+
index: 2,
44+
...expectedCommon,
45+
}];
46+
expect(chart.legend.legendItems).toEqual(expected);
47+
});
48+
49+
it('should return correct legend label objects with border radius', function() {
50+
var chart = window.acquireChart({
51+
type: 'doughnut',
52+
data: {
53+
labels: ['label1'],
54+
datasets: [{
55+
data: [10],
56+
backgroundColor: ['red'],
57+
borderWidth: 2,
58+
borderColor: '#000',
59+
borderDash: [1, 2, 3],
60+
borderDashOffset: 1,
61+
borderJoinStyle: 'miter',
62+
borderRadius: 3,
63+
}]
64+
},
65+
options: {
66+
plugins: {
67+
legend: {
68+
labels: {
69+
useBorderRadius: true,
70+
borderRadius: 5,
71+
textAlign: 'left',
72+
}
73+
}
74+
}
75+
}
76+
});
77+
78+
var expected = [{
79+
text: 'label1',
80+
fillStyle: 'red',
81+
index: 0,
4082
fontColor: '#666',
4183
hidden: false,
42-
index: 2,
4384
strokeStyle: '#000',
44-
textAlign: undefined,
85+
textAlign: 'left',
4586
lineWidth: 2,
46-
pointStyle: undefined
87+
pointStyle: undefined,
88+
lineDash: [1, 2, 3],
89+
lineDashOffset: 1,
90+
lineJoin: 'miter',
91+
borderRadius: 5
4792
}];
4893
expect(chart.legend.legendItems).toEqual(expected);
4994
});

0 commit comments

Comments
 (0)