Skip to content

Commit aa5ef3b

Browse files
authored
fix: y axis categorical height data issue (#2470)
1 parent 222c1bb commit aa5ef3b

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

packages/chart/index.html

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!DOCTYPE html>
1+
<!DOCTYPE html>
22
<html lang="en">
33

44
<head>
@@ -14,19 +14,17 @@
1414
min-height: 100vh;
1515
}
1616

17-
/* .react-container + .react-container {
18-
margin-top: 3rem;
19-
} */
17+
/* Add 1rem padding to mimic DFE when editor is not visible */
18+
.cdc-open-viz-module:not(.isEditor) {
19+
padding: 1rem;
20+
}
2021
</style>
2122
<link rel="stylesheet prefetch" href="https://www.cdc.gov/TemplatePackage/5.0/css/app.min.css?_=71669" />
2223
</head>
2324

2425
<body>
25-
2626
<!-- GENERIC CHART TYPES -->
27-
<div class="react-container" data-config="/examples/feature/combo/planet-combo-example-config.json"></div>
28-
29-
27+
<div class="react-container" data-config="/examples/private/height.json"></div>
3028

3129
<noscript>You need to enable JavaScript to run this app.</noscript>
3230

packages/chart/src/components/Axis/Categorical.Axis.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useContext } from 'react'
22
import { BarStack, Line } from '@visx/shape'
3-
import { scaleBand, scaleLinear, scaleOrdinal } from '@visx/scale'
3+
import { scaleBand, scaleOrdinal } from '@visx/scale'
44
import { Group } from '@visx/group'
55
import { Text } from '@visx/text'
66
import ConfigContext from '../../ConfigContext'
@@ -9,7 +9,7 @@ import createBarElement from '@cdc/core/components/createBarElement'
99
import { getTextWidth } from '@cdc/core/helpers/getTextWidth'
1010
import { APP_FONT_SIZE } from '@cdc/core/helpers/constants'
1111

12-
const CategoricalYAxis = ({ yMax, leftSize, max, xMax }) => {
12+
const CategoricalYAxis = ({ yScale, yMax, leftSize, xMax }) => {
1313
const { config } = useContext(ConfigContext)
1414

1515
const { orientation } = config
@@ -24,6 +24,9 @@ const CategoricalYAxis = ({ yMax, leftSize, max, xMax }) => {
2424

2525
const categories = config.yAxis?.categories
2626

27+
// Get max from the yScale domain
28+
const max = yScale.domain()[1]
29+
2730
const createDataShape = categories => {
2831
const categoryObj = [...categories].reduce((acc, item) => {
2932
acc[item.label] = item.height
@@ -68,11 +71,7 @@ const CategoricalYAxis = ({ yMax, leftSize, max, xMax }) => {
6871
range: [0, leftSize]
6972
})
7073

71-
const yScale = scaleLinear({
72-
domain: [0, max],
73-
range: [yMax, 0],
74-
clamp: true
75-
})
74+
// Use the yScale passed from useScales instead of creating a new one
7675

7776
const colorScale = scaleOrdinal({
7877
domain: categories.map(d => d?.label),

packages/chart/src/components/LinearChart.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,9 +1310,7 @@ const LinearChart = forwardRef<SVGAElement, LinearChartProps>(({ parentHeight, p
13101310
)}
13111311
{config.yAxis.type === 'categorical' && config.orientation === 'vertical' && (
13121312
<CategoricalYAxis
1313-
max={max}
1314-
maxValue={maxValue}
1315-
height={initialHeight}
1313+
yScale={yScale}
13161314
xMax={xMax}
13171315
yMax={yMax}
13181316
leftSize={Number(runtime.yAxis.size) - config.yAxis.axisPadding}

packages/chart/src/hooks/useScales.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,17 @@ const useScales = (properties: useScaleProps) => {
133133
// handle Vertical bars
134134
if (!isHorizontal) {
135135
xScale = composeScaleBand(xAxisDataMapped, [0, xMax], paddingRange)
136-
yScale = composeYScale({ min, max, yMax, config, leftMax })
136+
// For categorical y-axis, use [0, max] domain and [yMax, 0] range to match CategoricalYAxis
137+
// This ensures line data aligns with categorical bars and bars go to 100% height
138+
if (config.yAxis.type === 'categorical') {
139+
yScale = scaleLinear({
140+
domain: [0, max],
141+
range: [yMax, 0],
142+
clamp: true
143+
})
144+
} else {
145+
yScale = composeYScale({ min, max, yMax, config, leftMax })
146+
}
137147
seriesScale = composeScaleBand(seriesDomain, [0, xScale.bandwidth()], 0)
138148
}
139149

0 commit comments

Comments
 (0)