Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance legend label color point when usePointStyle is true #6006

Merged
merged 10 commits into from Feb 10, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/configuration/legend.md
Expand Up @@ -36,7 +36,8 @@ 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 fontSize if pointSize is not provided, boxWidth is not used in this case).
| `pointSize` | `Number` | `null` | Used only when usePointStyle is true. Independently change the size of the legend box (if not given, size will be based on fontSize).

## Legend Item Interface

Expand Down
10 changes: 5 additions & 5 deletions src/plugins/plugin.legend.js
Expand Up @@ -90,7 +90,7 @@ defaults._set('global', {
* @return {Number} width of the color box area
*/
function getBoxWidth(labelOpts, fontSize) {
return labelOpts.usePointStyle ?
return labelOpts.usePointStyle && labelOpts.boxWidth > fontSize ?
fontSize * Math.SQRT2 :
alfiehopkin marked this conversation as resolved.
Show resolved Hide resolved
labelOpts.boxWidth;
}
Expand Down Expand Up @@ -369,10 +369,10 @@ 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 pointWidth = Math.min(boxWidth, fontSize);
alfiehopkin marked this conversation as resolved.
Show resolved Hide resolved
var radius = pointWidth * Math.SQRT2 / 2;
var centerX = x + pointWidth / 2;
var centerY = y + fontSize / 2;

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