Skip to content

Commit 4ed650a

Browse files
authored
Enable overriding the legend pointStyle using new pointStyle option (#7959)
1 parent c428797 commit 4ed650a

File tree

4 files changed

+78
-3
lines changed

4 files changed

+78
-3
lines changed

docs/docs/configuration/legend.md

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ The legend label configuration is nested below the legend configuration using th
5757
| `generateLabels` | `function` | | Generates legend items for each thing in the legend. Default implementation returns the text + styling for the color box. See [Legend Item](#legend-item-interface) for details.
5858
| `filter` | `function` | `null` | Filters legend items out of the legend. Receives 2 parameters, a [Legend Item](#legend-item-interface) and the chart data.
5959
| `sort` | `function` | `null` | Sorts legend items. Receives 3 parameters, two [Legend Items](#legend-item-interface) and the chart data.
60+
| `pointStyle` | | | If specified, this style of point is used for the legend. Only used if `usePointStyle` is true.
6061
| `usePointStyle` | `boolean` | `false` | Label style will match corresponding point style (size is based on the minimum value between boxWidth and font.size).
6162

6263
## Legend Title Configuration

src/plugins/plugin.legend.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -740,8 +740,9 @@ export default {
740740
// lineWidth :
741741
generateLabels(chart) {
742742
const datasets = chart.data.datasets;
743-
const options = chart.options.legend || {};
744-
const usePointStyle = options.labels && options.labels.usePointStyle;
743+
const {labels} = chart.legend.options;
744+
const usePointStyle = labels.usePointStyle;
745+
const overrideStyle = labels.pointStyle;
745746

746747
return chart._getSortedDatasetMetas().map((meta) => {
747748
const style = meta.controller.getStyle(usePointStyle ? 0 : undefined);
@@ -756,7 +757,7 @@ export default {
756757
lineJoin: style.borderJoinStyle,
757758
lineWidth: style.borderWidth,
758759
strokeStyle: style.borderColor,
759-
pointStyle: style.pointStyle,
760+
pointStyle: overrideStyle || style.pointStyle,
760761
rotation: style.rotation,
761762

762763
// Below is extra data used for toggling the datasets

test/specs/plugin.legend.tests.js

+68
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,74 @@ describe('Legend block tests', function() {
622622
}]);
623623
});
624624

625+
it('should draw correctly when usePointStyle is true and pointStyle override is set', function() {
626+
var chart = window.acquireChart({
627+
type: 'line',
628+
data: {
629+
datasets: [{
630+
label: 'dataset1',
631+
backgroundColor: '#f31',
632+
borderCapStyle: 'butt',
633+
borderDash: [2, 2],
634+
borderDashOffset: 5.5,
635+
borderWidth: 0,
636+
borderColor: '#f31',
637+
pointStyle: 'crossRot',
638+
pointBackgroundColor: 'rgba(0,0,0,0.1)',
639+
pointBorderWidth: 5,
640+
pointBorderColor: 'green',
641+
data: []
642+
}, {
643+
label: 'dataset2',
644+
backgroundColor: '#f31',
645+
borderJoinStyle: 'miter',
646+
borderWidth: 2,
647+
borderColor: '#f31',
648+
pointStyle: 'crossRot',
649+
pointRotation: 15,
650+
data: []
651+
}],
652+
labels: []
653+
},
654+
options: {
655+
legend: {
656+
labels: {
657+
usePointStyle: true,
658+
pointStyle: 'star'
659+
}
660+
}
661+
}
662+
});
663+
664+
expect(chart.legend.legendItems).toEqual([{
665+
text: 'dataset1',
666+
fillStyle: 'rgba(0,0,0,0.1)',
667+
hidden: false,
668+
lineCap: undefined,
669+
lineDash: undefined,
670+
lineDashOffset: undefined,
671+
lineJoin: undefined,
672+
lineWidth: 5,
673+
strokeStyle: 'green',
674+
pointStyle: 'star',
675+
rotation: undefined,
676+
datasetIndex: 0
677+
}, {
678+
text: 'dataset2',
679+
fillStyle: '#f31',
680+
hidden: false,
681+
lineCap: undefined,
682+
lineDash: undefined,
683+
lineDashOffset: undefined,
684+
lineJoin: undefined,
685+
lineWidth: 2,
686+
strokeStyle: '#f31',
687+
pointStyle: 'star',
688+
rotation: 15,
689+
datasetIndex: 1
690+
}]);
691+
});
692+
625693
describe('config update', function() {
626694
it ('should update the options', function() {
627695
var chart = acquireChart({

types/plugins/index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ export interface ILegendOptions {
170170
*/
171171
sort(a: ILegendItem, b: ILegendItem, data: IChartData): number;
172172

173+
/**
174+
* Override point style for the legend. Only applies if usePointStyle is true
175+
*/
176+
pointStyle: PointStyle;
177+
173178
/**
174179
* Label style will match corresponding point style (size is based on the mimimum value between boxWidth and font.size).
175180
* @default false

0 commit comments

Comments
 (0)