Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions src/__tests__/treemap.visual.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,25 @@ test.describe('Treemap series', () => {
const component = await mount(<ChartTestStory data={data} />);
await expect(component.locator('svg')).toHaveScreenshot();
});

test('Not enough space to display the labels', async ({mount}) => {
const data: ChartData = {
series: {
data: [
{
type: 'treemap',
name: '',
layoutAlgorithm: 'dice',
data: [
{name: 'Value 1', value: 1},
{name: 'Value 10', value: 10},
{name: 'Value 100', value: 100},
],
},
],
},
};
const component = await mount(<ChartTestStory data={data} />);
await expect(component.locator('svg')).toHaveScreenshot();
});
});
18 changes: 12 additions & 6 deletions src/hooks/useShapes/treemap/prepare-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,31 @@ function getLabels(args: {

texts.forEach((text, index) => {
const label = getFormattedValue({value: text, ...args.options});
const {maxHeight: lineHeight, maxWidth: labelWidth} =
const {maxHeight: lineHeight, maxWidth: labelMaxWidth} =
getLabelsSize({labels: [label], html}) ?? {};
const left = d.x0 + padding;
const right = d.x1 - padding;
const width = Math.max(0, right - left);
const spaceWidth = Math.max(0, right - left);
const spaceHeight = Math.max(0, d.y1 - d.y0 - padding);
let x = left;
const y = index * lineHeight + d.y0 + padding;
const labelWidth = Math.min(labelMaxWidth, spaceWidth);

if (!labelWidth || lineHeight > spaceHeight) {
return;
}

switch (align) {
case 'left': {
x = left;
break;
}
case 'center': {
x = Math.max(left, left + (width - labelWidth) / 2);
x = Math.max(left, left + (spaceWidth - labelMaxWidth) / 2);
break;
}
case 'right': {
x = Math.max(left, right - labelWidth);
x = Math.max(left, right - labelMaxWidth);
break;
}
}
Expand All @@ -63,13 +69,13 @@ function getLabels(args: {
content: label,
x,
y,
size: {width, height: lineHeight},
size: {width: labelWidth, height: lineHeight},
}
: {
text: label,
x,
y,
width,
width: labelWidth,
nodeData: d.data,
};

Expand Down
Loading