Skip to content

Commit 1ed1061

Browse files
committed
fix: make scale changes utc/local safe
1 parent 69209ec commit 1ed1061

File tree

1 file changed

+43
-18
lines changed

1 file changed

+43
-18
lines changed

src/utils/buildAxis.linear.ts

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ import {
1818
timeDay,
1919
timeMonth,
2020
timeYear,
21+
utcYear,
22+
utcMonth,
23+
utcDay,
24+
utcHour,
25+
utcMinute,
26+
utcSecond,
2127
} from 'd3-time'
2228

2329
import { timeFormat } from 'd3-time-format'
@@ -118,6 +124,7 @@ function buildTimeAxis<TDatum>(
118124
range: [number, number],
119125
outerRange: [number, number]
120126
): AxisTime<TDatum> {
127+
const isLocal = options.scaleType !== 'localTime'
121128
const scaleFn = options.scaleType === 'localTime' ? scaleTime : scaleUtc
122129

123130
let isInvalid = false
@@ -147,62 +154,80 @@ function buildTimeAxis<TDatum>(
147154

148155
let autoFormatStr: string
149156

157+
const units = isLocal
158+
? {
159+
year: timeYear,
160+
month: timeMonth,
161+
day: timeDay,
162+
hour: timeHour,
163+
minute: timeMinute,
164+
second: timeSecond,
165+
}
166+
: {
167+
year: utcYear,
168+
month: utcMonth,
169+
day: utcDay,
170+
hour: utcHour,
171+
minute: utcMinute,
172+
second: utcSecond,
173+
}
174+
150175
if (minValue && maxValue) {
151176
if (
152-
timeYear.count(minValue, maxValue) > 0 ||
153-
+timeYear.floor(maxValue) < +timeYear()
177+
units.year.count(minValue, maxValue) > 0 ||
178+
+units.year.floor(maxValue) < +units.year()
154179
) {
155180
autoFormatStr = '%b %-d, %Y %-I:%M:%S.%L %p'
156181
} else if (
157-
timeMonth.count(minValue, maxValue) > 0 ||
158-
+timeMonth.floor(maxValue) < +timeMonth()
182+
units.month.count(minValue, maxValue) > 0 ||
183+
+units.month.floor(maxValue) < +units.month()
159184
) {
160185
autoFormatStr = '%b %-d, %-I:%M:%S.%L %p'
161186
} else if (
162-
timeDay.count(minValue, maxValue) > 0 ||
163-
+timeDay.floor(maxValue) < +timeDay()
187+
units.day.count(minValue, maxValue) > 0 ||
188+
+units.day.floor(maxValue) < +units.day()
164189
) {
165190
autoFormatStr = '%b %-d, %-I:%M:%S.%L %p'
166191
} else if (
167-
timeHour.count(minValue, maxValue) > 0 ||
168-
+timeHour.floor(maxValue) < +timeHour()
192+
units.hour.count(minValue, maxValue) > 0 ||
193+
+units.hour.floor(maxValue) < +units.hour()
169194
) {
170195
autoFormatStr = '%-I:%M:%S.%L %p'
171196
} else if (
172-
timeMinute.count(minValue, maxValue) > 0 ||
173-
+timeMinute.floor(maxValue) < +timeMinute()
197+
units.minute.count(minValue, maxValue) > 0 ||
198+
+units.minute.floor(maxValue) < +units.minute()
174199
) {
175200
autoFormatStr = '%-I:%M:%S.%L'
176201
} else if (
177-
timeSecond.count(minValue, maxValue) > 0 ||
178-
+timeSecond.floor(maxValue) < +timeSecond()
202+
units.second.count(minValue, maxValue) > 0 ||
203+
+units.second.floor(maxValue) < +units.second()
179204
) {
180205
autoFormatStr = '%L'
181206
}
182207
}
183208

184209
const contextFormat = (format: string, date: Date) => {
185-
if (timeSecond(date) < date) {
210+
if (units.second(date) < date) {
186211
// milliseconds - Do not remove any context
187212
return timeFormat(format)(date)
188213
}
189-
if (timeMinute(date) < date) {
214+
if (units.minute(date) < date) {
190215
// seconds - remove potential milliseconds
191216
return timeFormat(format.replace(/\.%L.*?(\s|$)/, ' '))(date)
192217
}
193-
if (timeHour(date) < date) {
218+
if (units.hour(date) < date) {
194219
// minutes - remove potential seconds and milliseconds
195220
return timeFormat(format.replace(/:%S.*?(\s|$)/, ' '))(date)
196221
}
197-
if (timeDay(date) < date) {
222+
if (units.day(date) < date) {
198223
// hours - remove potential minutes and seconds and milliseconds
199224
return timeFormat(format.replace(/:%M.*?(\s|$)/, ' '))(date)
200225
}
201-
if (timeMonth(date) < date) {
226+
if (units.month(date) < date) {
202227
// days - remove potential hours, minutes, seconds and milliseconds
203228
return timeFormat(format.replace(/%I.*/, ''))(date)
204229
}
205-
if (timeYear(date) < date) {
230+
if (units.year(date) < date) {
206231
// months - remove potential days, hours, minutes, seconds and milliseconds
207232
return timeFormat(format.replace(/%e, .*?(\s|$)/, ''))(date)
208233
}

0 commit comments

Comments
 (0)