Skip to content

Commit

Permalink
CRT-378: Treemap - Improve label truncating
Browse files Browse the repository at this point in the history
  • Loading branch information
iMoses committed Jun 27, 2024
1 parent f8c3344 commit 8f60e20
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/ag-charts-community/src/module-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from './util/object';
export * from './util/properties';
export * from './util/proxy';
export * from './util/timeFormatDefaults';
export * from './util/textMeasurer';
export * from './util/timeFormat';
export * from './util/types';
export * from './util/type-guards';
Expand Down
34 changes: 17 additions & 17 deletions packages/ag-charts-enterprise/src/series/util/labelFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type OverflowStrategy, type TextWrap, _ModuleSupport, _Scene, _Util } from 'ag-charts-community';

const { Validate, NUMBER, TEXT_WRAP, OVERFLOW_STRATEGY } = _ModuleSupport;
const { Validate, TextMeasurer, NUMBER, TEXT_WRAP, OVERFLOW_STRATEGY } = _ModuleSupport;
const { Logger } = _Util;
const { Text, Label } = _Scene;

Expand Down Expand Up @@ -89,7 +89,7 @@ export function maximumValueSatisfying<T>(
let found: T | undefined;

while (max >= min) {
const index = ((max + min) / 2) | 0;
const index = Math.floor((max + min) / 2);
const value = iteratee(index);
if (value == null) {
max = index - 1;
Expand Down Expand Up @@ -317,9 +317,7 @@ export function formatSingleLabel<Meta, FormatterParams>(
const availableWidth = sizeFitting.width - sizeAdjust;
const availableHeight = sizeFitting.height - sizeAdjust;

if (lineHeight > availableHeight) {
return;
}
if (lineHeight > availableHeight) return;

textSizeProps.fontSize = fontSize;
const lines = Text.wrapLines(
Expand All @@ -331,23 +329,25 @@ export function formatSingleLabel<Meta, FormatterParams>(
allowTruncation ? props.overflowStrategy : 'hide'
);

if (lines == null) {
return;
}
if (lines == null) return;

const text = lines.join('\n');
textNode.text = text;
textNode.fontSize = fontSize;
textNode.lineHeight = lineHeight;
const size = textNode.computeBBox();
const width = textNode.computeBBox().width;
const height = lineHeight * lines.length;

if (size.width > availableWidth || height > availableHeight) {
return;
textNode.text = lines.join('\n');

let height = lineHeight * lines.length;
while (height > availableHeight) {
if (lines.length === 1) return;
lines.pop();
lines[lines.length - 1] += TextMeasurer.EllipsisChar;
textNode.text = lines.join('\n');
height = lineHeight * lines.length;
}

return [{ text, fontSize, lineHeight, width, height }, sizeFitting.meta];
const { width } = textNode.computeBBox();
if (width > availableWidth) return;

return [{ text: textNode.text, fontSize, lineHeight, width, height }, sizeFitting.meta];
});
}

Expand Down

0 comments on commit 8f60e20

Please sign in to comment.