Skip to content

Commit bcb4516

Browse files
authored
fix(scales): fix timeScale min/max values and typings (pmndrs#743)
* Support for min/max in the TimeScale. * Min/max take same format as input data If the input data is formatted and the format property of the scale is set - exp. '%Y-%m-%d' - the min/max properties provided explicitly were not taken into account. Instead the actual range was used - this was crashing as the values `values.max` and `values.min` are already converted to native format.
1 parent cbba0f2 commit bcb4516

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

packages/scales/index.d.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,21 @@ declare module '@nivo/scales' {
1919
type: 'point'
2020
}
2121

22+
export interface TimeScaleFormatted {
23+
type: 'time'
24+
format: string
25+
precision?: 'millisecond' | 'second' | 'minute' | 'hour' | 'month' | 'year' | 'day'
26+
useUTC?: boolean
27+
min?: 'auto' | string
28+
max?: 'auto' | string
29+
}
30+
2231
export interface TimeScale {
2332
type: 'time'
24-
format?: string
2533
precision?: 'millisecond' | 'second' | 'minute' | 'hour' | 'month' | 'year' | 'day'
2634
useUTC?: boolean
35+
min?: 'auto' | Date
36+
max?: 'auto' | Date
2737
}
2838

2939
export interface LogScale {
@@ -33,7 +43,7 @@ declare module '@nivo/scales' {
3343
max?: 'auto' | number
3444
}
3545

36-
export type Scale = LinearScale | PointScale | TimeScale | LogScale
46+
export type Scale = LinearScale | PointScale | TimeScale | TimeScaleFormatted | LogScale
3747

3848
export type ScaleFunc = (value: string | number | Date) => number
3949
}

packages/scales/src/timeScale.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ export const timeScale = (
3232
if (min === 'auto') {
3333
minValue = values.min
3434
} else if (format !== 'native') {
35-
minValue = normalize(values.min)
35+
minValue = normalize(min)
3636
}
3737

3838
let maxValue = max
3939
if (max === 'auto') {
4040
maxValue = values.max
4141
} else if (format !== 'native') {
42-
maxValue = normalize(values.max)
42+
maxValue = normalize(max)
4343
}
4444

4545
const scale = useUTC ? scaleUtc() : scaleTime()

0 commit comments

Comments
 (0)