Skip to content

Commit 0e1929e

Browse files
committed
Improve chart label alignment and spacing
1 parent 620eeaa commit 0e1929e

5 files changed

Lines changed: 9 additions & 14 deletions

File tree

src/components/Charts/BarChart/BarChartContent.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,6 @@ function BarChartContent({data, isLoading, yAxisUnit, yAxisUnitPosition = 'left'
232232
labelColor={theme.textSupporting}
233233
xScale={args.xScale}
234234
chartBoundsBottom={args.chartBounds.bottom}
235-
centerRotatedLabels
236235
/>
237236
<ChartYAxisLabels
238237
yTicks={args.yTicks}

src/components/Charts/components/ChartXAxisLabels.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,6 @@ type ChartXAxisLabelsProps = {
4646

4747
/** Y-pixel coordinate of the bottom edge of the chart plot area. */
4848
chartBoundsBottom: number;
49-
50-
/** When true, rotated labels are centered on the tick. When false, they are right-aligned (end of text at tick). */
51-
centerRotatedLabels?: boolean;
5249
};
5350

5451
function ChartXAxisLabels({
@@ -65,7 +62,6 @@ function ChartXAxisLabels({
6562
labelColor,
6663
xScale,
6764
chartBoundsBottom,
68-
centerRotatedLabels = false,
6965
}: ChartXAxisLabelsProps) {
7066
const angleRad = (Math.abs(labelRotation) * Math.PI) / 180;
7167
const truncatedLabels = (() => {
@@ -89,8 +85,7 @@ function ChartXAxisLabels({
8985
const {ascent, descent} = getFontLineMetrics(fontMgr, fontSize);
9086

9187
const correction = rotatedLabelCenterCorrection(ascent, descent, angleRad);
92-
const centeredUpwardOffset = centerRotatedLabels && angleRad > 0 ? (Math.max(...renderedWidths) / 2) * Math.sin(angleRad) : 0;
93-
const labelY = chartBoundsBottom + AXIS_LABEL_GAP + rotatedLabelYOffset(ascent, descent, angleRad) + centeredUpwardOffset;
88+
const labelY = chartBoundsBottom + AXIS_LABEL_GAP + rotatedLabelYOffset(ascent, descent, angleRad);
9489

9590
return truncatedLabels.map((label, i) => {
9691
if (i % labelSkipInterval !== 0 || label.length === 0) {
@@ -117,7 +112,7 @@ function ChartXAxisLabels({
117112
);
118113
}
119114

120-
const textX = centerRotatedLabels ? tickX - renderWidth / 2 : tickX - renderWidth;
115+
const textX = tickX - renderWidth;
121116
const origin = vec(tickX, labelY);
122117

123118
return (

src/components/Charts/hooks/useChartLabelLayout.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ function useChartLabelLayout({
122122
// Approximate truncated widths for hit-testing: exact for non-truncated labels,
123123
// at most ellipsisWidth px over for truncated ones — acceptable for bounding boxes.
124124
const truncatedLabelWidths = labelMaxWidths.map((maxW, i) => Math.min(labelWidths.at(i) ?? 0, maxW));
125-
const finalMaxWidth = Math.max(...truncatedLabelWidths);
126125

127126
let skipInterval = 1;
128127
if (rotation === LABEL_ROTATIONS.VERTICAL) {
129-
const verticalWidth = effectiveWidth(finalMaxWidth, lineHeight, rotation);
130-
const visibleCount = maxVisibleCount(labelAreaWidth, verticalWidth);
128+
const visibleCount = maxVisibleCount(labelAreaWidth, lineHeight);
131129
skipInterval = visibleCount >= data.length ? 1 : Math.ceil(data.length / Math.max(1, visibleCount));
132130
}
133131

132+
const finalMaxWidth = Math.max(...truncatedLabelWidths.filter((_, i) => i % skipInterval === 0));
133+
134134
const lastIndex = data.length - 1;
135135

136136
return {

src/components/Search/SearchChartView.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Log from '@libs/Log';
55
import Navigation from '@libs/Navigation/Navigation';
66
import {formatToParts} from '@libs/NumberFormatUtils';
77
import {buildSearchQueryJSON, buildSearchQueryString} from '@libs/SearchQueryUtils';
8+
import StringUtils from '@libs/StringUtils';
89
import CONST from '@src/CONST';
910
import ROUTES from '@src/ROUTES';
1011
import CHART_GROUP_BY_CONFIG from './chartGroupByConfig';
@@ -78,7 +79,7 @@ function SearchChartView({queryJSON, view, groupBy, data, isLoading}: SearchChar
7879
return (
7980
<ChartComponent
8081
data={data}
81-
getLabel={getLabel}
82+
getLabel={(item) => StringUtils.normalize(getLabel(item))}
8283
getFilterQuery={getFilterQuery}
8384
onItemPress={handleItemPress}
8485
isLoading={isLoading}

src/styles/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6007,10 +6007,10 @@ const staticStyles = (theme: ThemeColors) =>
60076007
borderRadius: variables.componentBorderRadiusLarge,
60086008
},
60096009
chartContent: {
6010-
minHeight: CHART_CONTENT_MIN_HEIGHT,
6010+
height: CHART_CONTENT_MIN_HEIGHT,
60116011
},
60126012
chartActivityIndicator: {
6013-
minHeight: CHART_CONTENT_MIN_HEIGHT,
6013+
height: CHART_CONTENT_MIN_HEIGHT,
60146014
justifyContent: 'center',
60156015
alignItems: 'center',
60166016
},

0 commit comments

Comments
 (0)