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

Enable custom legend box heights #7459

Merged
merged 7 commits into from
Jun 4, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/docs/configuration/legend.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The legend label configuration is nested below the legend configuration using th
| Name | Type | Default | Description
| ---- | ---- | ------- | -----------
| `boxWidth` | `number` | `40` | Width of coloured box.
| `boxHeight` | `number` | fontSize | Height of the coloured box.
| `font` | `Font` | `defaults.font` | See [Fonts](fonts.md)
| `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.
Expand Down
42 changes: 30 additions & 12 deletions src/plugins/plugin.legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import defaults from '../core/core.defaults';
import Element from '../core/core.element';
import layouts from '../core/core.layouts';
import {drawPoint} from '../helpers/helpers.canvas';
import {callback as call, mergeIf, valueOrDefault} from '../helpers/helpers.core';
import {callback as call, mergeIf, valueOrDefault, isNullOrUndef} from '../helpers/helpers.core';
import {toFont, toPadding} from '../helpers/helpers.options';
import {getRtlAdapter, overrideTextDirection, restoreTextDirection} from '../helpers/helpers.rtl';

Expand Down Expand Up @@ -95,6 +95,19 @@ function getBoxWidth(labelOpts, fontSize) {
labelOpts.boxWidth;
}

/**
* Helper function to get the box height
* @param {object} labelOpts - the label options on the legend
* @param {*} fontSize - the label font size
* @return {number} height of the color box area
*/
function getBoxHeight(labelOpts, fontSize) {
const {boxHeight} = labelOpts;
return (labelOpts.usePointStyle && boxHeight > fontSize) || isNullOrUndef(boxHeight) ?
fontSize :
labelOpts.boxHeight;
etimberg marked this conversation as resolved.
Show resolved Hide resolved
}

export class Legend extends Element {

constructor(config) {
Expand Down Expand Up @@ -239,6 +252,9 @@ export class Legend extends Element {
const ctx = me.ctx;
const labelFont = toFont(labelOpts.font);
const fontSize = labelFont.size;
const boxWidth = getBoxWidth(labelOpts, fontSize);
const boxHeight = getBoxHeight(labelOpts, fontSize);
const itemHeight = Math.max(boxHeight, fontSize);

// Reset hit boxes
const hitboxes = me.legendHitBoxes = [];
Expand Down Expand Up @@ -271,11 +287,10 @@ export class Legend extends Element {
ctx.textBaseline = 'middle';

me.legendItems.forEach((legendItem, i) => {
const boxWidth = getBoxWidth(labelOpts, fontSize);
const width = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width;

if (i === 0 || lineWidths[lineWidths.length - 1] + width + 2 * labelOpts.padding > minSize.width) {
totalHeight += fontSize + labelOpts.padding;
totalHeight += itemHeight + labelOpts.padding;
lineWidths[lineWidths.length - (i > 0 ? 0 : 1)] = 0;
}

Expand All @@ -284,7 +299,7 @@ export class Legend extends Element {
left: 0,
top: 0,
width,
height: fontSize
height: itemHeight
};

lineWidths[lineWidths.length - 1] += width + labelOpts.padding;
Expand All @@ -302,7 +317,6 @@ export class Legend extends Element {

const heightLimit = minSize.height - titleHeight;
me.legendItems.forEach((legendItem, i) => {
const boxWidth = getBoxWidth(labelOpts, fontSize);
const itemWidth = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width;

// If too tall, go to new column
Expand All @@ -323,7 +337,7 @@ export class Legend extends Element {
left: 0,
top: 0,
width: itemWidth,
height: fontSize
height: itemHeight,
};
});

Expand Down Expand Up @@ -377,11 +391,13 @@ export class Legend extends Element {
ctx.font = labelFont.string;

const boxWidth = getBoxWidth(labelOpts, fontSize);
const boxHeight = getBoxHeight(labelOpts, fontSize);
const height = Math.max(fontSize, boxHeight);
const hitboxes = me.legendHitBoxes;

// current position
const drawLegendBox = function(x, y, legendItem) {
if (isNaN(boxWidth) || boxWidth <= 0) {
if (isNaN(boxWidth) || boxWidth <= 0 || isNaN(boxHeight) || boxHeight < 0) {
kurkle marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand Down Expand Up @@ -417,9 +433,12 @@ export class Legend extends Element {
drawPoint(ctx, drawOptions, centerX, centerY);
} else {
// Draw box as legend symbol
ctx.fillRect(rtlHelper.leftForLtr(x, boxWidth), y, boxWidth, fontSize);
// Adjust position when boxHeight < fontSize (want it centered)
const yBoxTop = y + Math.max((fontSize - boxHeight) / 2, 0);

ctx.fillRect(rtlHelper.leftForLtr(x, boxWidth), yBoxTop, boxWidth, boxHeight);
if (lineWidth !== 0) {
ctx.strokeRect(rtlHelper.leftForLtr(x, boxWidth), y, boxWidth, fontSize);
ctx.strokeRect(rtlHelper.leftForLtr(x, boxWidth), yBoxTop, boxWidth, boxHeight);
}
}

Expand All @@ -429,8 +448,7 @@ export class Legend extends Element {
const fillText = function(x, y, legendItem, textWidth) {
const halfFontSize = fontSize / 2;
const xLeft = rtlHelper.xPlus(x, boxWidth + halfFontSize);
const yMiddle = y + halfFontSize;

const yMiddle = y + (height / 2);
ctx.fillText(legendItem.text, xLeft, yMiddle);

if (legendItem.hidden) {
Expand Down Expand Up @@ -473,7 +491,7 @@ export class Legend extends Element {

overrideTextDirection(me.ctx, opts.textDirection);

const itemHeight = fontSize + labelOpts.padding;
const itemHeight = height + labelOpts.padding;
me.legendItems.forEach((legendItem, i) => {
const textWidth = ctx.measureText(legendItem.text).width;
const width = boxWidth + (fontSize / 2) + textWidth;
Expand Down
31 changes: 31 additions & 0 deletions test/specs/plugin.legend.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,37 @@ describe('Legend block tests', function() {
});
});

it('should draw items with a custom boxHeight', function() {
var chart = window.acquireChart(
{
type: 'line',
data: {
datasets: [{
label: 'dataset1',
data: []
}],
labels: []
},
options: {
legend: {
position: 'right',
labels: {
boxHeight: 40
}
}
}
},
{
canvas: {
width: 512,
height: 105
}
}
);
const hitBox = chart.legend.legendHitBoxes[0];
expect(hitBox.height).toBe(40);
});

it('should pick up the first item when the property is an array', function() {
var chart = window.acquireChart({
type: 'bar',
Expand Down