Skip to content

Commit d7bdf5b

Browse files
committed
fix: empty axes no longer cause infinite renders
1 parent 7def987 commit d7bdf5b

File tree

4 files changed

+44
-83
lines changed

4 files changed

+44
-83
lines changed

src/components/AxisLinear.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
} from '../utils/Constants.js'
1717
import useChartContext from '../hooks/useChartContext'
1818
import useMeasure from './AxisLinear.useMeasure'
19-
import useChartState from '../hooks/useChartState'
2019

2120
const defaultStyles = {
2221
line: {
@@ -59,10 +58,6 @@ export default function AxisLinear(axis) {
5958

6059
useMeasure({ ...axis, elRef, rotation, gridWidth, gridHeight, setRotation })
6160

62-
const [tickLabelSkipIndices] = useChartState(
63-
state => state.tickLabelSkipIndices[id]
64-
)
65-
6661
// Not ready? Render null
6762
if (!show) {
6863
return null
@@ -190,11 +185,6 @@ export default function AxisLinear(axis) {
190185
vertical ? directionMultiplier * spacing : tickOffset,
191186
vertical ? tickOffset : directionMultiplier * spacing
192187
)} rotate(${-rotation}deg)`,
193-
opacity:
194-
tickLabelSkipIndices?.length &&
195-
tickLabelSkipIndices.includes(i)
196-
? 0
197-
: 1,
198188
}}
199189
dominantBaseline={
200190
rotation

src/components/AxisLinear.useMeasure.js

Lines changed: 44 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ export default function useMeasure({
3232
gridHeight,
3333
show,
3434
}) {
35-
const [tickLabelSkipIndices, setChartState] = useChartState(
36-
state => state.tickLabelSkipIndices[id]
37-
)
38-
const [axisDimension] = useChartState(
35+
const [axisDimension, setChartState] = useChartState(
3936
state => state.axisDimensions?.[position]?.[id]
4037
)
4138

@@ -46,6 +43,7 @@ export default function useMeasure({
4643

4744
if (!elRef.current) {
4845
if (axisDimension) {
46+
console.log('remove axis')
4947
// If the entire axis is hidden, then we need to remove the axis dimensions
5048
setChartState(state => {
5149
const newAxes = state.axisDimensions[position] || {}
@@ -113,21 +111,6 @@ export default function useMeasure({
113111
}, false)
114112
}
115113

116-
let newTickLabelSkipIndices = []
117-
118-
if (
119-
JSON.stringify(newTickLabelSkipIndices) !==
120-
JSON.stringify(tickLabelSkipIndices)
121-
) {
122-
setChartState(old => ({
123-
...old,
124-
tickLabelSkipIndices: {
125-
...old.tickLabelSkipIndices,
126-
[id]: newTickLabelSkipIndices,
127-
},
128-
}))
129-
}
130-
131114
// Rotate ticks for non-time horizontal axes
132115
if (!vertical) {
133116
const newRotation =
@@ -136,6 +119,7 @@ export default function useMeasure({
136119
: 0
137120

138121
if (newRotation !== rotation) {
122+
console.log('rotation')
139123
setRotation(position === 'top' ? -newRotation : newRotation)
140124
}
141125
}
@@ -151,63 +135,51 @@ export default function useMeasure({
151135

152136
// Axis overflow measurements
153137
if (!vertical) {
154-
const leftMostLabelDim = realLabelDims.length
155-
? realLabelDims.reduce((d, labelDim) =>
156-
labelDim.left < d.left ? labelDim : d
157-
)
158-
: null
159-
const rightMostLabelDim = realLabelDims.length
160-
? realLabelDims.reduce((d, labelDim) =>
161-
labelDim.right > d.right ? labelDim : d
162-
)
163-
: null
164-
165-
newDimensions.left = round(
166-
Math.max(0, domainDims.left - leftMostLabelDim?.left),
167-
5,
168-
Math.ceil
169-
)
170-
171-
newDimensions.right = round(
172-
Math.max(0, rightMostLabelDim?.right - domainDims.right),
173-
5,
174-
Math.ceil
175-
)
138+
if (realLabelDims.length) {
139+
const leftMostLabelDim = realLabelDims.reduce((d, labelDim) =>
140+
labelDim.left < d.left ? labelDim : d
141+
)
142+
const rightMostLabelDim = realLabelDims.reduce((d, labelDim) =>
143+
labelDim.right > d.right ? labelDim : d
144+
)
145+
146+
newDimensions.left = Math.round(
147+
Math.max(0, domainDims.left - leftMostLabelDim?.left)
148+
)
149+
150+
newDimensions.right = Math.round(
151+
Math.max(0, rightMostLabelDim?.right - domainDims.right)
152+
)
153+
}
176154

177-
newDimensions.height = round(
155+
newDimensions.height = Math.round(
178156
Math.max(tickSizeInner, tickSizeOuter) +
179157
tickPadding +
180-
(tallestRealLabel?.height || 0),
181-
5,
182-
Math.ceil
158+
(tallestRealLabel?.height ?? 0)
183159
)
184160
} else {
185-
const topMostLabelDim = realLabelDims.reduce((d, labelDim) =>
186-
labelDim.top < d.top ? labelDim : d
187-
)
188-
189-
const bottomMostLabelDim = realLabelDims.reduce((d, labelDim) =>
190-
labelDim.bottom > d.bottom ? labelDim : d
191-
)
192-
193-
newDimensions.top = round(
194-
Math.max(0, domainDims.top - topMostLabelDim?.top),
195-
5,
196-
Math.ceil
197-
)
198-
199-
newDimensions.bottom = round(
200-
Math.max(0, bottomMostLabelDim?.bottom - domainDims.bottom),
201-
5,
202-
Math.ceil
203-
)
161+
if (realLabelDims.length) {
162+
const topMostLabelDim = realLabelDims.reduce((d, labelDim) =>
163+
labelDim.top < d.top ? labelDim : d
164+
)
165+
166+
const bottomMostLabelDim = realLabelDims.reduce((d, labelDim) =>
167+
labelDim.bottom > d.bottom ? labelDim : d
168+
)
169+
170+
newDimensions.top = Math.round(
171+
Math.max(0, domainDims.top - topMostLabelDim?.top)
172+
)
173+
174+
newDimensions.bottom = Math.round(
175+
Math.max(0, bottomMostLabelDim?.bottom - domainDims.bottom)
176+
)
177+
}
204178

205-
newDimensions.width = round(
179+
newDimensions.width = Math.round(
206180
Math.max(tickSizeInner, tickSizeOuter) +
207181
tickPadding +
208-
(widestRealLabel?.width || 0),
209-
5,
210-
Math.ceil
182+
(widestRealLabel?.width ?? 0)
211183
)
212184
}
213185

@@ -216,6 +188,8 @@ export default function useMeasure({
216188
!axisDimension ||
217189
Object.keys(newDimensions).some(key => {
218190
return newDimensions[key] !== axisDimension[key]
191+
? console.log(id, key, newDimensions[key], axisDimension[key]) || true
192+
: false
219193
})
220194
) {
221195
setChartState(state => ({
@@ -230,6 +204,7 @@ export default function useMeasure({
230204
}))
231205
}
232206
}, [
207+
axisDimension,
233208
elRef,
234209
gridHeight,
235210
gridWidth,
@@ -240,16 +215,14 @@ export default function useMeasure({
240215
setChartState,
241216
setRotation,
242217
show,
243-
tickLabelSkipIndices,
244218
tickPadding,
245219
tickSizeInner,
246220
tickSizeOuter,
247221
vertical,
248-
axisDimension,
249222
])
250223

251224
// Measure after if needed
252-
useIsomorphicLayoutEffect(() => {
225+
React.useEffect(() => {
253226
measureDimensions()
254227
}, [measureDimensions])
255228
}

src/components/Chart.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ export default function ChartState(options) {
9999
hovered: null,
100100
element: null,
101101
axisDimensions: {},
102-
tickLabelSkipIndices: {},
103102
offset: {},
104103
pointer: {},
105104
setOffset,

src/utils/buildAxis.linear.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ export default function buildAxisLinear({
6161
materializedData,
6262
gridHeight,
6363
gridWidth,
64-
estimatedTickCounts,
6564
}) {
6665
if (!position) {
6766
throw new Error(`Chart axes must have a valid 'position' property`)

0 commit comments

Comments
 (0)