Skip to content

Commit d364012

Browse files
committed
Leave space for y-axis labels
1 parent 1a4a112 commit d364012

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

packages/chart/src/components/LinearChart.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { isLegendWrapViewport, isMobileFontViewport } from '@cdc/core/helpers/vi
3434
import { getTextWidth } from '@cdc/core/helpers/getTextWidth'
3535
import { calcInitialHeight, handleAutoPaddingRight } from '../helpers/sizeHelpers'
3636
import { filterAndShiftLinearDateTicks } from '../helpers/filterAndShiftLinearDateTicks'
37-
import { calculateHorizontalBarYAxisWidth } from '../helpers/calculateHorizontalBarYAxisWidth'
37+
import { calculateHorizontalBarCategoryLabelWidth } from '../helpers/calculateHorizontalBarCategoryLabelWidth'
3838

3939
// Hooks
4040
import useReduceData from '../hooks/useReduceData'
@@ -248,9 +248,9 @@ const LinearChart = forwardRef<SVGAElement, LinearChartProps>(({ parentHeight, p
248248
currentViewport
249249
})
250250

251-
// Calculate dynamic Y-axis width for horizontal bar charts
252-
const calculatedYAxisWidth = useMemo(() => {
253-
return calculateHorizontalBarYAxisWidth({
251+
// Calculate category label space for horizontal bar charts
252+
const categoryLabelSpace = useMemo(() => {
253+
return calculateHorizontalBarCategoryLabelWidth({
254254
yScale,
255255
parentWidth,
256256
formatDate,
@@ -261,8 +261,9 @@ const LinearChart = forwardRef<SVGAElement, LinearChartProps>(({ parentHeight, p
261261
})
262262
}, [isHorizontal, config.visualizationType, config.yAxis.labelPlacement, yScale, parentWidth])
263263

264+
const horizontalYAxisLabelSpace = runtime.yAxis.label && !config.hideYAxisLabel ? 30 : 0
264265
if (isHorizontal && config.visualizationType === 'Bar' && !config.isLollipopChart) {
265-
runtime.yAxis.size = calculatedYAxisWidth
266+
runtime.yAxis.size = categoryLabelSpace + horizontalYAxisLabelSpace
266267
}
267268

268269
const [yTickCount, xTickCount] = ['yAxis', 'xAxis'].map(axis =>
@@ -1184,12 +1185,12 @@ const LinearChart = forwardRef<SVGAElement, LinearChartProps>(({ parentHeight, p
11841185

11851186
return (
11861187
<Text
1187-
x={tick.to.x - Number(runtime.yAxis.size) + 8}
1188+
x={tick.from.x - Number(runtime.yAxis.size) + horizontalYAxisLabelSpace}
11881189
y={labelCenterY}
11891190
verticalAnchor={'middle'}
11901191
textAnchor={'start'}
11911192
fontSize={tickLabelFontSize}
1192-
width={Number(runtime.yAxis.size)}
1193+
width={categoryLabelSpace}
11931194
lineHeight={'1.2em'}
11941195
>
11951196
{tick.formattedValue}

packages/chart/src/helpers/calculateHorizontalBarYAxisWidth.ts renamed to packages/chart/src/helpers/calculateHorizontalBarCategoryLabelWidth.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { getTextWidth } from '@cdc/core/helpers/getTextWidth'
22

3-
interface CalculateHorizontalBarYAxisWidthProps {
3+
interface CalculateHorizontalBarCategoryLabelWidthProps {
44
yScale: any
55
parentWidth: number
66
formatDate: Function
@@ -11,20 +11,20 @@ interface CalculateHorizontalBarYAxisWidthProps {
1111
}
1212

1313
/**
14-
* Helper function to calculate dynamic Y-axis width for horizontal bar charts
14+
* Helper function to calculate category label space for horizontal bar charts
1515
*
1616
* @param props Configuration object with chart properties
17-
* @returns Calculated Y-axis width, capped at 30% of parent width
17+
* @returns Calculated category label space, capped at 30% of parent width
1818
*/
19-
export const calculateHorizontalBarYAxisWidth = ({
19+
export const calculateHorizontalBarCategoryLabelWidth = ({
2020
yScale,
2121
parentWidth,
2222
formatDate,
2323
parseDate,
2424
tickLabelFont,
2525
xAxisType,
2626
labelPlacement
27-
}: CalculateHorizontalBarYAxisWidthProps): number => {
27+
}: CalculateHorizontalBarCategoryLabelWidthProps): number => {
2828
if (labelPlacement !== 'On Date/Category Axis') return 0
2929

3030
const categoryValues = yScale.domain()

0 commit comments

Comments
 (0)