Skip to content

Commit cf2ac87

Browse files
committed
pointStyleYOffset
1 parent ab79c3b commit cf2ac87

7 files changed

Lines changed: 116 additions & 6 deletions

File tree

docs/configuration/legend.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Namespace: `options.plugins.legend.labels`
7171
| `textAlign` | `string` | `'center'` | Horizontal alignment of the label text. Options are: `'left'`, `'right'` or `'center'`.
7272
| `usePointStyle` | `boolean` | `false` | Label style will match corresponding point style (size is based on pointStyleWidth or the minimum value between boxWidth and font.size).
7373
| `pointStyleWidth` | `number` | `null` | If `usePointStyle` is true, the width of the point style used for the legend.
74+
| `pointStyleYOffset` | `number` | `0` | If `usePointStyle` is true, the vertical offset in pixels applied to the point style drawn for the legend item. Useful for visually centering asymmetric point styles such as `line` or `dash`, or for problematic fonts.
7475
| `useBorderRadius` | `boolean` | `false` | Label borderRadius will match corresponding borderRadius.
7576
| `borderRadius` | `number` | `undefined` | Override the borderRadius to use.
7677

docs/samples/legend/point-style.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@ module.exports = {
5858
};
5959
```
6060

61-
## Docs
61+
For asymmetric point styles such as `line` or `dash`, you can nudge the symbol vertically using the `pointStyleYOffset` option set to a small positive number (e.g., `4` pixels).
62+
63+
## Docs
6264
* [Data structures (`labels`)](../../general/data-structures.md)
6365
* [Line](../../charts/line.md)
6466
* [Legend](../../configuration/legend.md)
65-
* [Legend Label Configuration](../../configuration/legend.md#legend-label-configuration)
66-
* `usePointStyle`
67+
* [Legend Label Configuration](../../configuration/legend.md#legend-label-configuration)
68+
* `usePointStyle`
69+
* `pointStyleYOffset`
6770
* [Elements](../../configuration/elements.md)
68-
* [Point Configuration](../../configuration/elements.md#point-configuration)
69-
* [Point Styles](../../configuration/elements.md#point-styles)
71+
* [Point Configuration](../../configuration/elements.md#point-configuration)
72+
* [Point Styles](../../configuration/elements.md#point-styles)

src/plugins/plugin.legend.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ export class Legend extends Element {
417417

418418
const realX = rtlHelper.x(x);
419419

420-
drawLegendBox(realX, y, legendItem);
420+
drawLegendBox(realX, y + (labelOpts.pointStyleYOffset || 0), legendItem);
421421

422422
x = _textX(textAlign, x + boxWidth + halfFontSize, isHorizontal ? x + width : this.right, opts.rtl);
423423

src/types/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2496,6 +2496,10 @@ export interface LegendOptions<TType extends ChartType> {
24962496
* If usePointStyle is true, the width of the point style used for the legend.
24972497
*/
24982498
pointStyleWidth: number;
2499+
/**
2500+
* If you are using a font that causes incorrect alignment, adjust this value to ensure proper alignment.
2501+
*/
2502+
datasetRadiusBuffer: number;
24992503
/**
25002504
* Generates legend items for each thing in the legend. Default implementation returns the text + styling for the color box. See Legend Item for details.
25012505
*/
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"config": {
3+
"type": "line",
4+
"data": {
5+
"labels": ["A"],
6+
"datasets": [
7+
{
8+
"label": "Line",
9+
"data": [10],
10+
"backgroundColor": "#4bc0c0",
11+
"borderColor": "#4bc0c0",
12+
"borderWidth": 2,
13+
"pointStyle": "line"
14+
},
15+
{
16+
"label": "Dash",
17+
"data": [20],
18+
"backgroundColor": "#ff6384",
19+
"borderColor": "#ff6384",
20+
"borderWidth": 2,
21+
"pointStyle": "dash"
22+
}
23+
]
24+
},
25+
"options": {
26+
"plugins": {
27+
"legend": {
28+
"display": true,
29+
"labels": {
30+
"usePointStyle": true,
31+
"pointStyleWidth": 30,
32+
"pointStyleYOffset": 4
33+
}
34+
}
35+
},
36+
"scales": {
37+
"x": {
38+
"display": false
39+
},
40+
"y": {
41+
"display": false
42+
}
43+
}
44+
}
45+
},
46+
"options": {
47+
"canvas": {
48+
"width": 512,
49+
"height": 128
50+
}
51+
}
52+
}
53+
2.75 KB
Loading

test/specs/plugin.legend.tests.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,55 @@ describe('Legend block tests', function() {
877877
}]);
878878
});
879879

880+
it('should apply pointStyleYOffset to the legend point symbol only', function() {
881+
function drawWithOffset(offset) {
882+
var chart = window.acquireChart({
883+
type: 'line',
884+
data: {
885+
labels: ['A'],
886+
datasets: [{
887+
label: 'dataset1',
888+
data: [1],
889+
pointStyle: 'circle'
890+
}]
891+
},
892+
options: {
893+
plugins: {
894+
legend: {
895+
labels: {
896+
usePointStyle: true,
897+
pointStyleYOffset: offset
898+
}
899+
}
900+
}
901+
}
902+
});
903+
904+
var mockContext = window.createMockContext();
905+
chart.legend.ctx = mockContext;
906+
chart.legend.draw();
907+
908+
var calls = mockContext.getCalls();
909+
var arcCall = calls.filter(function(call) {
910+
return call.name === 'arc';
911+
})[0];
912+
var fillTextCall = calls.filter(function(call) {
913+
return call.name === 'fillText';
914+
})[0];
915+
916+
return {
917+
arcY: arcCall.args[1],
918+
textY: fillTextCall.args[2]
919+
};
920+
}
921+
922+
var base = drawWithOffset(0);
923+
var shifted = drawWithOffset(6);
924+
925+
expect(shifted.arcY - base.arcY).toBeCloseTo(6, 6);
926+
expect(shifted.textY).toBeCloseTo(base.textY, 6);
927+
});
928+
880929
it('should not crash when the legend defaults are false', function() {
881930
const oldDefaults = Chart.defaults.plugins.legend;
882931

0 commit comments

Comments
 (0)