// Label that will be displayed
text: string,
+ // Border radius of the legend item.
+ // Introduced in 3.1.0
+ borderRadius?: number | BorderRadius,
+
// Index of the associated dataset
datasetIndex: number,
import defaults from '../core/core.defaults';
import Element from '../core/core.element';
import layouts from '../core/core.layouts';
-import {drawPoint, renderText} from '../helpers/helpers.canvas';
+import {addRoundedRectPath, drawPoint, renderText} from '../helpers/helpers.canvas';
import {
callback as call, valueOrDefault, toFont,
toPadding, getRtlAdapter, overrideTextDirection, restoreTextDirection,
clipArea, unclipArea
} from '../helpers/index';
import {_toLeftRightCenter, _alignStartEnd, _textX} from '../helpers/helpers.extras';
+import {toTRBLCorners} from '../helpers/helpers.options';
/**
* @typedef { import("../platform/platform.base").ChartEvent } ChartEvent
*/
// Draw box as legend symbol
// Adjust position when boxHeight < fontSize (want it centered)
const yBoxTop = y + Math.max((fontSize - boxHeight) / 2, 0);
+ const xBoxLeft = rtlHelper.leftForLtr(x, boxWidth);
+ const borderRadius = toTRBLCorners(legendItem.borderRadius);
+
+ ctx.beginPath();
+
+ if (Object.values(borderRadius).some(v => v !== 0)) {
+ addRoundedRectPath(ctx, {
+ x: xBoxLeft,
+ y: yBoxTop,
+ w: boxWidth,
+ h: boxHeight,
+ radius: borderRadius,
+ });
+ } else {
+ ctx.rect(xBoxLeft, yBoxTop, boxWidth, boxHeight);
+ }
- ctx.fillRect(rtlHelper.leftForLtr(x, boxWidth), yBoxTop, boxWidth, boxHeight);
+ ctx.fill();
if (lineWidth !== 0) {
- ctx.strokeRect(rtlHelper.leftForLtr(x, boxWidth), yBoxTop, boxWidth, boxHeight);
+ ctx.stroke();
}
}
pointStyle: pointStyle || style.pointStyle,
rotation: style.rotation,
textAlign: textAlign || style.textAlign,
+ borderRadius: 0, // TODO: v4, default to style.borderRadius
// Below is extra data used for toggling the datasets
datasetIndex: meta.index
--- /dev/null
+module.exports = {
+ config: {
+ type: 'line',
+ data: {
+ labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
+ datasets: [
+ {
+ label: '# of Votes',
+ data: [12, 19, 3, 5, 2, 3],
+ borderWidth: 1,
+ borderColor: '#FF0000',
+ backgroundColor: '#00FF00',
+ },
+ {
+ label: '# of Points',
+ data: [7, 11, 5, 8, 3, 7],
+ borderWidth: 2,
+ borderColor: '#FF00FF',
+ backgroundColor: '#0000FF',
+ }
+ ]
+ },
+ options: {
+ scales: {
+ x: {display: false},
+ y: {display: false}
+ },
+ plugins: {
+ title: false,
+ tooltip: false,
+ filler: false,
+ legend: {
+ labels: {
+ generateLabels: (chart) => {
+ const items = Chart.defaults.plugins.legend.labels.generateLabels(chart);
+
+ for (const item of items) {
+ item.borderRadius = 5;
+ }
+
+ return items;
+ }
+ }
+ }
+ }
+ }
+ },
+ options: {
+ spriteText: true,
+ canvas: {
+ width: 512,
+ height: 256
+ }
+ }
+};
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
+ borderRadius: 0,
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
datasetIndex: 0
}, {
text: 'dataset2',
+ borderRadius: 0,
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: true,
datasetIndex: 1
}, {
text: 'dataset3',
+ borderRadius: 0,
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
+ borderRadius: 0,
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
datasetIndex: 0
}, {
text: 'dataset2',
+ borderRadius: 0,
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: true,
datasetIndex: 1
}, {
text: 'dataset3',
+ borderRadius: 0,
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
expect(chart.legend.legendItems).toEqual([{
text: 'dataset3',
+ borderRadius: 0,
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
datasetIndex: 2
}, {
text: 'dataset2',
+ borderRadius: 0,
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: true,
datasetIndex: 1
}, {
text: 'dataset1',
+ borderRadius: 0,
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
+ borderRadius: 0,
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
datasetIndex: 0
}, {
text: 'dataset3',
+ borderRadius: 0,
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
expect(chart.legend.legendItems).toEqual([{
text: 'dataset3',
+ borderRadius: 0,
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
datasetIndex: 2
}, {
text: 'dataset2',
+ borderRadius: 0,
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: true,
datasetIndex: 1
}, {
text: 'dataset1',
+ borderRadius: 0,
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
+ borderRadius: 0,
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
+ borderRadius: 0,
fillStyle: 'rgb(50, 0, 0)',
fontColor: '#666',
hidden: false,
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
+ borderRadius: 0,
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
datasetIndex: 0
}, {
text: 'dataset2',
+ borderRadius: 0,
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
expect(chart.legend.legendItems).toEqual([{
text: 'dataset1',
+ borderRadius: 0,
fillStyle: 'rgba(0,0,0,0.1)',
fontColor: '#666',
hidden: false,
datasetIndex: 0
}, {
text: 'dataset2',
+ borderRadius: 0,
fillStyle: '#f31',
fontColor: '#666',
hidden: false,
*/
text: string;
+ /**
+ * Border radius of the legend box
+ * @since 3.1.0
+ */
+ borderRadius?: number | BorderRadius;
+
/**
* Index of the associated dataset
*/