1
- import { bisector , tickStep } from "d3-array" ;
2
- import { timeYear , timeMonth , timeWeek , timeDay , timeHour , timeMinute , timeSecond , timeMillisecond } from "d3-time" ;
1
+ import { timeYear , timeMonth , timeWeek , timeDay , timeHour , timeMinute , timeSecond , timeTicks , timeTickInterval } from "d3-time" ;
3
2
import { timeFormat } from "d3-time-format" ;
4
3
import continuous , { copy } from "./continuous.js" ;
5
4
import { initRange } from "./init.js" ;
6
5
import nice from "./nice.js" ;
7
6
8
- var durationSecond = 1000 ,
9
- durationMinute = durationSecond * 60 ,
10
- durationHour = durationMinute * 60 ,
11
- durationDay = durationHour * 24 ,
12
- durationWeek = durationDay * 7 ,
13
- durationMonth = durationDay * 30 ,
14
- durationYear = durationDay * 365 ;
15
-
16
7
function date ( t ) {
17
8
return new Date ( t ) ;
18
9
}
@@ -21,7 +12,7 @@ function number(t) {
21
12
return t instanceof Date ? + t : + new Date ( + t ) ;
22
13
}
23
14
24
- export function calendar ( year , month , week , day , hour , minute , second , millisecond , format ) {
15
+ export function calendar ( ticks , tickInterval , year , month , week , day , hour , minute , second , format ) {
25
16
var scale = continuous ( ) ,
26
17
invert = scale . invert ,
27
18
domain = scale . domain ;
@@ -35,27 +26,6 @@ export function calendar(year, month, week, day, hour, minute, second, milliseco
35
26
formatMonth = format ( "%B" ) ,
36
27
formatYear = format ( "%Y" ) ;
37
28
38
- var tickIntervals = [
39
- [ second , 1 , durationSecond ] ,
40
- [ second , 5 , 5 * durationSecond ] ,
41
- [ second , 15 , 15 * durationSecond ] ,
42
- [ second , 30 , 30 * durationSecond ] ,
43
- [ minute , 1 , durationMinute ] ,
44
- [ minute , 5 , 5 * durationMinute ] ,
45
- [ minute , 15 , 15 * durationMinute ] ,
46
- [ minute , 30 , 30 * durationMinute ] ,
47
- [ hour , 1 , durationHour ] ,
48
- [ hour , 3 , 3 * durationHour ] ,
49
- [ hour , 6 , 6 * durationHour ] ,
50
- [ hour , 12 , 12 * durationHour ] ,
51
- [ day , 1 , durationDay ] ,
52
- [ day , 2 , 2 * durationDay ] ,
53
- [ week , 1 , durationWeek ] ,
54
- [ month , 1 , durationMonth ] ,
55
- [ month , 3 , 3 * durationMonth ] ,
56
- [ year , 1 , durationYear ]
57
- ] ;
58
-
59
29
function tickFormat ( date ) {
60
30
return ( second ( date ) < date ? formatMillisecond
61
31
: minute ( date ) < date ? formatSecond
@@ -66,33 +36,6 @@ export function calendar(year, month, week, day, hour, minute, second, milliseco
66
36
: formatYear ) ( date ) ;
67
37
}
68
38
69
- function tickInterval ( interval , start , stop ) {
70
- if ( interval == null ) interval = 10 ;
71
-
72
- // If a desired tick count is specified, pick a reasonable tick interval
73
- // based on the extent of the domain and a rough estimate of tick size.
74
- // Otherwise, assume interval is already a time interval and use it.
75
- if ( typeof interval === "number" ) {
76
- var target = Math . abs ( stop - start ) / interval ,
77
- i = bisector ( function ( i ) { return i [ 2 ] ; } ) . right ( tickIntervals , target ) ,
78
- step ;
79
- if ( i === tickIntervals . length ) {
80
- step = tickStep ( start / durationYear , stop / durationYear , interval ) ;
81
- interval = year ;
82
- } else if ( i ) {
83
- i = tickIntervals [ target / tickIntervals [ i - 1 ] [ 2 ] < tickIntervals [ i ] [ 2 ] / target ? i - 1 : i ] ;
84
- step = i [ 1 ] ;
85
- interval = i [ 0 ] ;
86
- } else {
87
- step = Math . max ( tickStep ( start , stop , interval ) , 1 ) ;
88
- interval = millisecond ;
89
- }
90
- return interval . every ( step ) ;
91
- }
92
-
93
- return interval ;
94
- }
95
-
96
39
scale . invert = function ( y ) {
97
40
return new Date ( invert ( y ) ) ;
98
41
} ;
@@ -102,15 +45,8 @@ export function calendar(year, month, week, day, hour, minute, second, milliseco
102
45
} ;
103
46
104
47
scale . ticks = function ( interval ) {
105
- var d = domain ( ) ,
106
- t0 = d [ 0 ] ,
107
- t1 = d [ d . length - 1 ] ,
108
- r = t1 < t0 ,
109
- t ;
110
- if ( r ) t = t0 , t0 = t1 , t1 = t ;
111
- t = tickInterval ( interval , t0 , t1 ) ;
112
- t = t ? t . range ( t0 , t1 + 1 ) : [ ] ; // inclusive stop
113
- return r ? t . reverse ( ) : t ;
48
+ var d = domain ( ) ;
49
+ return ticks ( d [ 0 ] , d [ d . length - 1 ] , interval == null ? 10 : interval ) ;
114
50
} ;
115
51
116
52
scale . tickFormat = function ( count , specifier ) {
@@ -119,18 +55,17 @@ export function calendar(year, month, week, day, hour, minute, second, milliseco
119
55
120
56
scale . nice = function ( interval ) {
121
57
var d = domain ( ) ;
122
- return ( interval = tickInterval ( interval , d [ 0 ] , d [ d . length - 1 ] ) )
123
- ? domain ( nice ( d , interval ) )
124
- : scale ;
58
+ if ( ! interval || typeof interval . range !== "function" ) interval = tickInterval ( d [ 0 ] , d [ d . length - 1 ] , interval == null ? 10 : interval ) ;
59
+ return interval ? domain ( nice ( d , interval ) ) : scale ;
125
60
} ;
126
61
127
62
scale . copy = function ( ) {
128
- return copy ( scale , calendar ( year , month , week , day , hour , minute , second , millisecond , format ) ) ;
63
+ return copy ( scale , calendar ( ticks , tickInterval , year , month , week , day , hour , minute , second , format ) ) ;
129
64
} ;
130
65
131
66
return scale ;
132
67
}
133
68
134
69
export default function time ( ) {
135
- return initRange . apply ( calendar ( timeYear , timeMonth , timeWeek , timeDay , timeHour , timeMinute , timeSecond , timeMillisecond , timeFormat ) . domain ( [ new Date ( 2000 , 0 , 1 ) , new Date ( 2000 , 0 , 2 ) ] ) , arguments ) ;
70
+ return initRange . apply ( calendar ( timeTicks , timeTickInterval , timeYear , timeMonth , timeWeek , timeDay , timeHour , timeMinute , timeSecond , timeFormat ) . domain ( [ new Date ( 2000 , 0 , 1 ) , new Date ( 2000 , 0 , 2 ) ] ) , arguments ) ;
136
71
}
3 commit comments
Pringels commentedon May 11, 2021
I might be mistaken but this definitely seems like a breaking change even though the version bump was minor:
3.2 -> 3.3
Any libraries making use of
d3-scale
will break if they declare the dependency using^@3.2.4
Is this a correct assumption? Would you be willing to revert this and re-release it as part of a major version? Or at least provide some backward compatibility?
curran commentedon May 11, 2021
@Pringels In what way would they break?
Pringels commentedon May 18, 2021
@curran because
d3-scale
is now incompatible with versions ofd3-time
lower than 2.1.x