Skip to content

Commit 2d11275

Browse files
committed
Move labelY utility function to utils
1 parent d28141d commit 2d11275

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

src/components/Charts/LineChart/LineChartContent.tsx

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import fontSource from '@components/Charts/font';
1313
import type {HitTestArgs} from '@components/Charts/hooks';
1414
import {useChartInteractions, useChartLabelFormats, useChartLabelLayout, useDynamicYDomain, useTooltipData} from '@components/Charts/hooks';
1515
import type {CartesianChartProps, ChartDataPoint} from '@components/Charts/types';
16-
import {calculateMinDomainPadding, DEFAULT_CHART_COLOR, measureTextWidth, rotatedLabelCenterCorrection} from '@components/Charts/utils';
16+
import {calculateMinDomainPadding, DEFAULT_CHART_COLOR, measureTextWidth, rotatedLabelCenterCorrection, rotatedLabelYOffset} from '@components/Charts/utils';
1717
import useResponsiveLayout from '@hooks/useResponsiveLayout';
1818
import useTheme from '@hooks/useTheme';
1919
import useThemeStyles from '@hooks/useThemeStyles';
@@ -163,19 +163,7 @@ function LineChartContent({data, title, titleIcon, isLoading, yAxisUnit, yAxisUn
163163
const fontMetrics = font.getMetrics();
164164
const ascent = Math.abs(fontMetrics.ascent);
165165
const descent = Math.abs(fontMetrics.descent);
166-
167-
// Calculate labelY to maintain consistent LABEL_GAP from axis to closest point of text
168-
// At 0°: closest point is top of text (baseline - ascent)
169-
// At 45°: closest point is top-right corner, ascent projects as ascent * cos(45°)
170-
// At 90°: text is vertical, closest point is at descent from baseline
171-
let labelY: number;
172-
if (angleRad === 0) {
173-
labelY = args.chartBounds.bottom + LABEL_GAP + ascent;
174-
} else if (angleRad >= Math.PI / 2) {
175-
labelY = args.chartBounds.bottom + LABEL_GAP + descent;
176-
} else {
177-
labelY = args.chartBounds.bottom + LABEL_GAP + ascent * Math.cos(angleRad);
178-
}
166+
const labelY = args.chartBounds.bottom + LABEL_GAP + rotatedLabelYOffset(ascent, descent, angleRad);
179167

180168
return truncatedLabels.map((label, i) => {
181169
if (i % labelSkipInterval !== 0) {

src/components/Charts/utils.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ function rotatedLabelCenterCorrection(ascent: number, descent: number, angleRad:
6262
return ((ascent - descent) * Math.sin(angleRad)) / 2;
6363
}
6464

65+
/**
66+
* Calculate the vertical offset from axis to text baseline that maintains
67+
* a consistent visual gap regardless of label rotation.
68+
*
69+
* At 0°: gap to top of text (ascent above baseline)
70+
* At 45°: gap to top-right corner after rotation (ascent projects as ascent * cos(45°))
71+
* At 90°: gap to closest point of vertical text (descent from baseline)
72+
*/
73+
function rotatedLabelYOffset(ascent: number, descent: number, angleRad: number): number {
74+
if (angleRad === 0) {
75+
return ascent;
76+
}
77+
if (angleRad >= Math.PI / 2) {
78+
return descent;
79+
}
80+
return ascent * Math.cos(angleRad);
81+
}
82+
6583
/**
6684
* Calculate minimum horizontal domainPadding so that edge data points
6785
* (and their centered labels) aren't clipped by the chart boundary.
@@ -78,4 +96,4 @@ function calculateMinDomainPadding(chartWidth: number, pointCount: number, inner
7896
return Math.ceil(chartWidth * minPaddingRatio);
7997
}
8098

81-
export {getChartColor, DEFAULT_CHART_COLOR, measureTextWidth, rotatedLabelCenterCorrection, calculateMinDomainPadding};
99+
export {getChartColor, DEFAULT_CHART_COLOR, measureTextWidth, rotatedLabelCenterCorrection, rotatedLabelYOffset, calculateMinDomainPadding};

0 commit comments

Comments
 (0)