@elastic/charts
Version:
Elastic-Charts data visualization library
58 lines • 2.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.computeHorizontalLegendRowCount = computeHorizontalLegendRowCount;
const default_theme_attributes_1 = require("../../common/default_theme_attributes");
const legend_1 = require("../../common/legend");
function computeHorizontalLegendRowCount(args) {
const { items, availableWidth, columnGap, spacingBuffer, actionDimension, markerWidth, sharedMargin, widthLimit, showValueTitle, textMeasure, maxFormattedValue, } = args;
const font = {
fontFamily: default_theme_attributes_1.DEFAULT_FONT_FAMILY,
fontVariant: 'normal',
fontWeight: 400,
fontStyle: 'normal',
};
let rows = 1;
let currentRowWidth = 0;
for (const item of items) {
const { width: labelOnlyWidth } = textMeasure(item.label, font, 12, 1.5);
const cappedLabelWidth = widthLimit > 0 ? Math.min(labelOnlyWidth, widthLimit) : labelOnlyWidth;
const valuesWidth = item.values.map(({ type, label }) => {
if (type !== legend_1.LegendValue.CurrentAndLastValue && !label)
return 0;
const valueLabel = type === legend_1.LegendValue.CurrentAndLastValue ? maxFormattedValue ?? (label || '—') : label;
const valueText = !showValueTitle
? valueLabel
: `${legend_1.legendValueTitlesMap[type]?.toUpperCase() ?? ''}: ${valueLabel}`;
return sharedMargin + textMeasure(valueText, font, 12, 1.5).width;
});
const itemWidths = [
sharedMargin +
markerWidth +
spacingBuffer +
cappedLabelWidth +
spacingBuffer +
(actionDimension ? spacingBuffer + actionDimension : 0) +
sharedMargin,
...valuesWidth,
];
for (const [index, itemWidth] of itemWidths.entries()) {
if (itemWidth === 0)
continue;
const isFirst = index === 0;
const nextRowWidth = currentRowWidth === 0 ? itemWidth : currentRowWidth + (isFirst ? 0 : columnGap) + itemWidth;
if (nextRowWidth > availableWidth) {
if (currentRowWidth > 0) {
rows++;
if (rows > 2)
return { isSingleLine: false, isMoreThanTwoLines: true };
}
currentRowWidth = availableWidth > 0 ? Math.min(itemWidth, availableWidth) : itemWidth;
}
else {
currentRowWidth = nextRowWidth;
}
}
}
return { isSingleLine: rows <= 1, isMoreThanTwoLines: false };
}
//# sourceMappingURL=legend_row_count.js.map