Skip to content

Commit b3b2bd3

Browse files
Alfie Hopkinsimonbrunel
authored andcommitted
Enhance legend label color point when usePointStyle is true (chartjs#6006)
1 parent e4b8c7c commit b3b2bd3

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

docs/configuration/legend.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The legend label configuration is nested below the legend configuration using th
3636
| `padding` | `number` | `10` | Padding between rows of colored boxes.
3737
| `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.
3838
| `filter` | `function` | `null` | Filters legend items out of the legend. Receives 2 parameters, a [Legend Item](#legend-item-interface) and the chart data.
39-
| `usePointStyle` | `boolean` | `false` | Label style will match corresponding point style (size is based on fontSize, boxWidth is not used in this case).
39+
| `usePointStyle` | `boolean` | `false` | Label style will match corresponding point style (size is based on the mimimum value between boxWidth and fontSize).
4040

4141
## Legend Item Interface
4242

src/plugins/plugin.legend.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ defaults._set('global', {
9090
* @return {Number} width of the color box area
9191
*/
9292
function getBoxWidth(labelOpts, fontSize) {
93-
return labelOpts.usePointStyle ?
94-
fontSize * Math.SQRT2 :
93+
return labelOpts.usePointStyle && labelOpts.boxWidth > fontSize ?
94+
fontSize :
9595
labelOpts.boxWidth;
9696
}
9797

@@ -369,10 +369,9 @@ var Legend = Element.extend({
369369
if (opts.labels && opts.labels.usePointStyle) {
370370
// Recalculate x and y for drawPoint() because its expecting
371371
// x and y to be center of figure (instead of top left)
372-
var radius = fontSize * Math.SQRT2 / 2;
373-
var offSet = radius / Math.SQRT2;
374-
var centerX = x + offSet;
375-
var centerY = y + offSet;
372+
var radius = boxWidth * Math.SQRT2 / 2;
373+
var centerX = x + boxWidth / 2;
374+
var centerY = y + fontSize / 2;
376375

377376
// Draw pointStyle as legend symbol
378377
helpers.canvas.drawPoint(ctx, legendItem.pointStyle, radius, centerX, centerY);

0 commit comments

Comments
 (0)