Skip to content

Commit

Permalink
Enhance legend label color point when usePointStyle is true (chartjs#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfie Hopkin authored and jonrimmer committed Feb 14, 2019
1 parent 4e80692 commit 24c52e0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/configuration/legend.md
Expand Up @@ -36,7 +36,7 @@ The legend label configuration is nested below the legend configuration using th
| `padding` | `number` | `10` | Padding between rows of colored boxes.
| `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.
| `filter` | `function` | `null` | Filters legend items out of the legend. Receives 2 parameters, a [Legend Item](#legend-item-interface) and the chart data.
| `usePointStyle` | `boolean` | `false` | Label style will match corresponding point style (size is based on fontSize, boxWidth is not used in this case).
| `usePointStyle` | `boolean` | `false` | Label style will match corresponding point style (size is based on the mimimum value between boxWidth and fontSize).

## Legend Item Interface

Expand Down
11 changes: 5 additions & 6 deletions src/plugins/plugin.legend.js
Expand Up @@ -91,8 +91,8 @@ defaults._set('global', {
* @return {Number} width of the color box area
*/
function getBoxWidth(labelOpts, fontSize) {
return labelOpts.usePointStyle ?
fontSize * Math.SQRT2 :
return labelOpts.usePointStyle && labelOpts.boxWidth > fontSize ?
fontSize :
labelOpts.boxWidth;
}

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

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

0 comments on commit 24c52e0

Please sign in to comment.