Skip to content

Commit 7def987

Browse files
committed
fix: remove auto tick mode
1 parent 1dc7241 commit 7def987

File tree

4 files changed

+39
-123
lines changed

4 files changed

+39
-123
lines changed

src/components/AxisLinear.useMeasure.js

Lines changed: 31 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from 'react'
22
import useChartState from '../hooks/useChartState'
33
import useIsomorphicLayoutEffect from '../hooks/useIsomorphicLayoutEffect'
4-
import { axisTypeOrdinal, axisTypeTime, axisTypeUtc } from '../utils/Constants'
54
import { round } from '../utils/Utils'
65

76
const getElBox = el => {
@@ -23,24 +22,17 @@ export default function useMeasure({
2322
rotation,
2423
setRotation,
2524
id,
26-
type,
2725
position,
2826
tickSizeInner,
2927
tickSizeOuter,
3028
labelRotation,
3129
tickPadding,
32-
tickCount,
33-
minTickCount,
34-
maxTickCount,
3530
vertical,
3631
gridWidth,
3732
gridHeight,
3833
show,
3934
}) {
40-
const [estimatedTickCount, setChartState] = useChartState(
41-
state => state.estimatedTickCounts[id]
42-
)
43-
const [tickLabelSkipIndices] = useChartState(
35+
const [tickLabelSkipIndices, setChartState] = useChartState(
4436
state => state.tickLabelSkipIndices[id]
4537
)
4638
const [axisDimension] = useChartState(
@@ -71,12 +63,6 @@ export default function useMeasure({
7163
}
7264

7365
let gridSize = !vertical ? gridWidth : gridHeight
74-
let width = 0
75-
let height = 0
76-
let top = 0
77-
let bottom = 0
78-
let left = 0
79-
let right = 0
8066

8167
const domainDims = getElBox(elRef.current.querySelector('.domain'))
8268

@@ -89,19 +75,13 @@ export default function useMeasure({
8975
).map(el => getElBox(el))
9076

9177
// Determine the largest labels on the axis
92-
const [widestMeasureLabel, tallestMeasureLabel] = measureLabelDims.reduce(
93-
(labels, d) => {
94-
let [largestW = d, largestH = d] = labels
95-
if (d.width > 0 && d.width > largestW.width) {
96-
largestW = d
97-
}
98-
if (d.height > 0 && d.height > largestH.height) {
99-
largestH = d
100-
}
101-
return [largestW, largestH]
102-
},
103-
[]
104-
)
78+
const widestMeasureLabel = measureLabelDims.reduce((label, d) => {
79+
label = label || d
80+
if (d.width > 0 && d.width > label.width) {
81+
label = d
82+
}
83+
return label
84+
}, null)
10585

10686
// Determine the largest labels on the axis
10787
const [widestRealLabel, tallestRealLabel] = realLabelDims.reduce(
@@ -133,55 +113,8 @@ export default function useMeasure({
133113
}, false)
134114
}
135115

136-
const largestMeasureLabelSize = !vertical
137-
? widestMeasureLabel?.width || 0
138-
: tallestMeasureLabel?.height || 0
139-
140-
// Auto-fit ticks in "auto" tick mode for non-ordinal scales
141-
if (tickCount === 'auto' && type !== 'ordinal') {
142-
// if it's on, determine how many ticks we could display if they were all flat
143-
// How many ticks can we fit in the available axis space?
144-
let calculatedTickCount = Math.floor(
145-
(gridSize + largestMeasureLabelSize + tickPadding * 2) /
146-
(largestMeasureLabelSize + tickPadding * 2)
147-
)
148-
149-
calculatedTickCount = Math.max(
150-
Math.min(calculatedTickCount, maxTickCount),
151-
minTickCount
152-
)
153-
154-
if (calculatedTickCount !== estimatedTickCount) {
155-
setChartState(old => ({
156-
...old,
157-
estimatedTickCounts: {
158-
...old.estimatedTickCounts,
159-
[id]: calculatedTickCount,
160-
},
161-
}))
162-
}
163-
}
164-
165116
let newTickLabelSkipIndices = []
166117

167-
// Visual Skipping of time-based axis labels if they overlap (rotation not included)
168-
if (
169-
!rotation &&
170-
[axisTypeTime, axisTypeUtc].includes(type) &&
171-
realLabelDims?.length
172-
) {
173-
realLabelDims.reduce((last, d) => {
174-
if (
175-
round(last.right, 5, Math.ceil) >
176-
round(d.left - tickPadding, 5, Math.ceil)
177-
) {
178-
newTickLabelSkipIndices.push(realLabelDims.indexOf(d))
179-
return last
180-
}
181-
return d
182-
})
183-
}
184-
185118
if (
186119
JSON.stringify(newTickLabelSkipIndices) !==
187120
JSON.stringify(tickLabelSkipIndices)
@@ -196,7 +129,7 @@ export default function useMeasure({
196129
}
197130

198131
// Rotate ticks for non-time horizontal axes
199-
if (!vertical && type === axisTypeOrdinal) {
132+
if (!vertical) {
200133
const newRotation =
201134
(widestMeasureLabel?.width || 0) + tickPadding > smallestTickGap
202135
? labelRotation
@@ -207,6 +140,15 @@ export default function useMeasure({
207140
}
208141
}
209142

143+
const newDimensions = {
144+
width: 0,
145+
height: 0,
146+
top: 0,
147+
bottom: 0,
148+
left: 0,
149+
right: 0,
150+
}
151+
210152
// Axis overflow measurements
211153
if (!vertical) {
212154
const leftMostLabelDim = realLabelDims.length
@@ -220,23 +162,23 @@ export default function useMeasure({
220162
)
221163
: null
222164

223-
left = round(
165+
newDimensions.left = round(
224166
Math.max(0, domainDims.left - leftMostLabelDim?.left),
225-
10,
167+
5,
226168
Math.ceil
227169
)
228170

229-
right = round(
171+
newDimensions.right = round(
230172
Math.max(0, rightMostLabelDim?.right - domainDims.right),
231-
10,
173+
5,
232174
Math.ceil
233175
)
234176

235-
height = round(
177+
newDimensions.height = round(
236178
Math.max(tickSizeInner, tickSizeOuter) +
237179
tickPadding +
238180
(tallestRealLabel?.height || 0),
239-
10,
181+
5,
240182
Math.ceil
241183
)
242184
} else {
@@ -248,36 +190,27 @@ export default function useMeasure({
248190
labelDim.bottom > d.bottom ? labelDim : d
249191
)
250192

251-
top = round(
193+
newDimensions.top = round(
252194
Math.max(0, domainDims.top - topMostLabelDim?.top),
253-
10,
195+
5,
254196
Math.ceil
255197
)
256198

257-
bottom = round(
199+
newDimensions.bottom = round(
258200
Math.max(0, bottomMostLabelDim?.bottom - domainDims.bottom),
259-
10,
201+
5,
260202
Math.ceil
261203
)
262204

263-
width = round(
205+
newDimensions.width = round(
264206
Math.max(tickSizeInner, tickSizeOuter) +
265207
tickPadding +
266208
(widestRealLabel?.width || 0),
267-
10,
209+
5,
268210
Math.ceil
269211
)
270212
}
271213

272-
const newDimensions = {
273-
width,
274-
height,
275-
top,
276-
bottom,
277-
left,
278-
right,
279-
}
280-
281214
// Only update the axisDimensions if something has changed
282215
if (
283216
!axisDimension ||
@@ -297,27 +230,22 @@ export default function useMeasure({
297230
}))
298231
}
299232
}, [
300-
axisDimension,
301233
elRef,
302-
estimatedTickCount,
303234
gridHeight,
304235
gridWidth,
305236
id,
306237
labelRotation,
307-
maxTickCount,
308-
minTickCount,
309238
position,
310239
rotation,
311240
setChartState,
312241
setRotation,
313242
show,
314-
tickCount,
315243
tickLabelSkipIndices,
316244
tickPadding,
317245
tickSizeInner,
318246
tickSizeOuter,
319-
type,
320247
vertical,
248+
axisDimension,
321249
])
322250

323251
// Measure after if needed

src/components/Chart.js

Lines changed: 1 addition & 6 deletions
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-
estimatedTickCounts: {},
103102
tickLabelSkipIndices: {},
104103
offset: {},
105104
pointer: {},
@@ -138,14 +137,11 @@ export function Chart(options) {
138137
...rest
139138
} = applyDefaults(options)
140139

141-
const [
142-
{ hovered, element, axisDimensions, estimatedTickCounts, pointer },
143-
] = useChartState(
140+
const [{ hovered, element, axisDimensions, pointer }] = useChartState(
144141
d => ({
145142
hovered: d.hovered,
146143
element: d.element,
147144
axisDimensions: d.axisDimensions,
148-
estimatedTickCounts: d.estimatedTickCounts,
149145
pointer: d.pointer,
150146
}),
151147
'shallow'
@@ -182,7 +178,6 @@ export function Chart(options) {
182178
materializedData,
183179
gridHeight,
184180
gridWidth,
185-
estimatedTickCounts,
186181
})
187182

188183
const stackData = useStackData({

src/components/pipeline/useAxes.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ import React from 'react'
22
//
33
import buildAxis from '../../utils/buildAxis'
44

5-
export default ({
6-
axes,
7-
materializedData,
8-
gridHeight,
9-
gridWidth,
10-
estimatedTickCounts,
11-
}) => {
5+
export default ({ axes, materializedData, gridHeight, gridWidth }) => {
126
// Detect axes changes and build axes
137
let prePrimaryAxes = axes.filter(d => d.primary)
148
let preSecondaryAxes = axes.filter(d => !d.primary)
@@ -25,7 +19,6 @@ export default ({
2519
materializedData,
2620
gridWidth,
2721
gridHeight,
28-
estimatedTickCounts,
2922
})
3023
})
3124
},
@@ -42,7 +35,6 @@ export default ({
4235
materializedData,
4336
gridWidth,
4437
gridHeight,
45-
estimatedTickCounts,
4638
})
4739
})
4840
},

src/utils/buildAxis.linear.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export default function buildAxisLinear({
3939
hardMin = undefined,
4040
hardMax = undefined,
4141
base = undefined,
42-
tickCount = 'auto',
42+
tickCount = 10,
4343
minTickCount = 1,
4444
maxTickCount = 99999999,
4545
tickValues = null,
@@ -56,6 +56,7 @@ export default function buildAxisLinear({
5656
show = true,
5757
stacked = false,
5858
id: userId,
59+
estimatedTickSize: userEstimatedTickSize,
5960
},
6061
materializedData,
6162
gridHeight,
@@ -71,8 +72,10 @@ export default function buildAxisLinear({
7172
const AxisIdKey = `${valueKey}AxisId`
7273
const vertical = detectVertical(position)
7374
const RTL = detectRTL(position) // Right to left OR top to bottom
75+
const estimatedTickSize = userEstimatedTickSize ?? vertical ? 20 : 50
7476

7577
const id = userId || `${position}_${type}`
78+
const isTimeType = [axisTypeTime, axisTypeUtc].includes(type)
7679

7780
// TODO: Any sorting needs to happen here, else the min/max's might not line up correctly
7881

@@ -258,10 +261,6 @@ export default function buildAxisLinear({
258261

259262
let resolvedTickCount = tickCount
260263

261-
if (tickCount === 'auto') {
262-
resolvedTickCount = estimatedTickCounts[id] ?? 10
263-
}
264-
265264
const ticks = filterTicks(
266265
tickValues ||
267266
(scale.ticks ? scale.ticks(resolvedTickCount) : scale.domain())
@@ -294,6 +293,7 @@ export default function buildAxisLinear({
294293
hardMin,
295294
hardMax,
296295
base,
296+
isTimeType,
297297
tickCount,
298298
minTickCount,
299299
maxTickCount,
@@ -325,6 +325,7 @@ export default function buildAxisLinear({
325325
ticks,
326326
format,
327327
spacing,
328+
estimatedTickSize,
328329
}
329330

330331
if (type === axisTypeOrdinal) {

0 commit comments

Comments
 (0)