Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/controllers/controller.doughnut.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,25 @@ export default class DoughnutController extends DatasetController {
labels: {
generateLabels(chart) {
const data = chart.data;
const {labels: {pointStyle, textAlign, color, useBorderRadius, borderRadius}} = chart.legend.options;
if (data.labels.length && data.datasets.length) {
const {labels: {pointStyle, color}} = chart.legend.options;

return data.labels.map((label, i) => {
const meta = chart.getDatasetMeta(0);
const style = meta.controller.getStyle(i);

return {
text: label,
fillStyle: style.backgroundColor,
strokeStyle: style.borderColor,
fontColor: color,
hidden: !chart.getDataVisibility(i),
lineDash: style.borderDash,
lineDashOffset: style.borderDashOffset,
lineJoin: style.borderJoinStyle,
lineWidth: style.borderWidth,
strokeStyle: style.borderColor,
textAlign: textAlign,
pointStyle: pointStyle,
hidden: !chart.getDataVisibility(i),

borderRadius: useBorderRadius && (borderRadius || style.borderRadius),
// Extra data used for toggling the correct item
index: i
};
Expand Down
73 changes: 59 additions & 14 deletions test/specs/global.defaults.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,81 @@ describe('Default Configs', function() {
},
});

var expected = [{
text: 'label1',
fillStyle: 'red',
var expectedCommon = {
fontColor: '#666',
hidden: false,
index: 0,
strokeStyle: '#000',
textAlign: undefined,
lineWidth: 2,
pointStyle: undefined
pointStyle: undefined,
lineDash: [],
lineDashOffset: 0,
lineJoin: undefined,
borderRadius: undefined,
};

var expected = [{
text: 'label1',
fillStyle: 'red',
index: 0,
...expectedCommon,
}, {
text: 'label2',
fillStyle: 'green',
fontColor: '#666',
hidden: false,
index: 1,
strokeStyle: '#000',
textAlign: undefined,
lineWidth: 2,
pointStyle: undefined
...expectedCommon,
}, {
text: 'label3',
fillStyle: 'blue',
index: 2,
...expectedCommon,
}];
expect(chart.legend.legendItems).toEqual(expected);
});

it('should return correct legend label objects with border radius', function() {
var chart = window.acquireChart({
type: 'doughnut',
data: {
labels: ['label1'],
datasets: [{
data: [10],
backgroundColor: ['red'],
borderWidth: 2,
borderColor: '#000',
borderDash: [1, 2, 3],
borderDashOffset: 1,
borderJoinStyle: 'miter',
borderRadius: 3,
}]
},
options: {
plugins: {
legend: {
labels: {
useBorderRadius: true,
borderRadius: 5,
textAlign: 'left',
}
}
}
}
});

var expected = [{
text: 'label1',
fillStyle: 'red',
index: 0,
fontColor: '#666',
hidden: false,
index: 2,
strokeStyle: '#000',
textAlign: undefined,
textAlign: 'left',
lineWidth: 2,
pointStyle: undefined
pointStyle: undefined,
lineDash: [1, 2, 3],
lineDashOffset: 1,
lineJoin: 'miter',
borderRadius: 5
}];
expect(chart.legend.legendItems).toEqual(expected);
});
Expand Down