import layouts from '../core/core.layouts';
import {addRoundedRectPath, drawPointLegend, renderText} from '../helpers/helpers.canvas';
import {
- callback as call, valueOrDefault, toFont,
- toPadding, getRtlAdapter, overrideTextDirection, restoreTextDirection,
- clipArea, unclipArea, _isBetween
+ _isBetween,
+ callback as call,
+ clipArea,
+ getRtlAdapter,
+ overrideTextDirection,
+ restoreTextDirection,
+ toFont,
+ toPadding,
+ unclipArea,
+ valueOrDefault,
} from '../helpers/index';
-import {_toLeftRightCenter, _alignStartEnd, _textX} from '../helpers/helpers.extras';
+import {_alignStartEnd, _textX, _toLeftRightCenter} from '../helpers/helpers.extras';
import {toTRBLCorners} from '../helpers/helpers.options';
+
/**
* @typedef { import("../../types").ChartEvent } ChartEvent
*/
height = this._fitRows(titleHeight, fontSize, boxWidth, itemHeight) + 10;
} else {
height = this.maxHeight; // fill all the height
- width = this._fitCols(titleHeight, fontSize, boxWidth, itemHeight) + 10;
+ width = this._fitCols(titleHeight, labelFont, boxWidth, itemHeight) + 10;
}
this.width = Math.min(width, options.maxWidth || this.maxWidth);
return totalHeight;
}
- _fitCols(titleHeight, fontSize, boxWidth, itemHeight) {
+ _fitCols(titleHeight, labelFont, boxWidth, _itemHeight) {
const {ctx, maxHeight, options: {labels: {padding}}} = this;
const hitboxes = this.legendHitBoxes = [];
const columnSizes = this.columnSizes = [];
let col = 0;
this.legendItems.forEach((legendItem, i) => {
- const itemWidth = boxWidth + (fontSize / 2) + ctx.measureText(legendItem.text).width;
+ const {itemWidth, itemHeight} = calculateItemSize(boxWidth, labelFont, ctx, legendItem, _itemHeight);
// If too tall, go to new column
if (i > 0 && currentColHeight + itemHeight + 2 * padding > heightLimit) {
if (isHorizontal) {
cursor.x += width + padding;
+ } else if (typeof legendItem.text !== 'string') {
+ const fontLineHeight = labelFont.lineHeight;
+ cursor.y += calculateLegendItemHeight(legendItem, fontLineHeight);
} else {
cursor.y += lineHeight;
}
}
}
+function calculateItemSize(boxWidth, labelFont, ctx, legendItem, _itemHeight) {
+ const itemWidth = calculateItemWidth(legendItem, boxWidth, labelFont, ctx);
+ const itemHeight = calculateItemHeight(_itemHeight, legendItem, labelFont.lineHeight);
+ return {itemWidth, itemHeight};
+}
+
+function calculateItemWidth(legendItem, boxWidth, labelFont, ctx) {
+ let legendItemText = legendItem.text;
+ if (legendItemText && typeof legendItemText !== 'string') {
+ legendItemText = legendItemText.reduce((a, b) => a.length > b.length ? a : b);
+ }
+ return boxWidth + (labelFont.size / 2) + ctx.measureText(legendItemText).width;
+}
+
+function calculateItemHeight(_itemHeight, legendItem, fontLineHeight) {
+ let itemHeight = _itemHeight;
+ if (typeof legendItem.text !== 'string') {
+ itemHeight = calculateLegendItemHeight(legendItem, fontLineHeight);
+ }
+ return itemHeight;
+}
+
+function calculateLegendItemHeight(legendItem, fontLineHeight) {
+ const labelHeight = legendItem.text ? legendItem.text.length + 0.5 : 0;
+ return fontLineHeight * labelHeight;
+}
+
function isListened(type, opts) {
if ((type === 'mousemove' || type === 'mouseout') && (opts.onHover || opts.onLeave)) {
return true;