Skip to content

Commit 5cb011d

Browse files
committed
donut labels
1 parent 7c5739b commit 5cb011d

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

src/web/components/chart/donut/Labels.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ interface LabelsProps<TData extends LabelData> {
2626
}
2727

2828
const LABEL_RADIUS_OFFSET = 10;
29-
const LABEL_X = 85;
29+
const LABEL_EDGE_OFFSET = 15;
3030
const LABEL_GAP = 16;
3131

3232
interface LabelPosition {
@@ -115,7 +115,8 @@ const Labels = <TData extends LabelData = LabelData>({
115115
const arcPoint = arc.centroid(currentArc);
116116
const outerArcPoint = outerArc.centroid(currentArc);
117117
const {isRightSide, y} = labelPositions[index];
118-
const labelPoint = [isRightSide ? LABEL_X : -LABEL_X, y];
118+
const labelX = outerRadiusX + LABEL_EDGE_OFFSET;
119+
const labelPoint = [isRightSide ? labelX : -labelX, y];
119120
const points = [arcPoint, outerArcPoint, labelPoint]
120121
.map(point => point.join(','))
121122
.join(' ');

src/web/components/chart/donut/__tests__/Labels.test.tsx

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,32 @@ describe('Labels', () => {
4545
expect(document.querySelectorAll('.pie-label')).toHaveLength(2);
4646
});
4747

48-
test('should keep external labels inside the chart edge', () => {
48+
test('should keep external labels outside the donut edge', () => {
4949
renderLabels([{toolTip: 'Single slice', value: 100}]);
5050

5151
expect(screen.getByText('100')).toHaveAttribute(
5252
'transform',
53-
'translate(-85,110)',
53+
'translate(-115,110)',
54+
);
55+
});
56+
57+
test('should move labels outside the donut when it grows', () => {
58+
const {render} = rendererWith();
59+
render(
60+
<Labels
61+
centerX={150}
62+
centerY={150}
63+
data={[{toolTip: 'Single slice', value: 100}]}
64+
innerRadiusX={191}
65+
innerRadiusY={191}
66+
outerRadiusX={294}
67+
outerRadiusY={294}
68+
/>,
69+
);
70+
71+
expect(screen.getByText('100')).toHaveAttribute(
72+
'transform',
73+
'translate(-309,304)',
5474
);
5575
});
5676

0 commit comments

Comments
 (0)