Skip to content

Commit 80ff9b2

Browse files
committedApr 10, 2021
adopt d3-time’s ticks
1 parent 8afe6bd commit 80ff9b2

File tree

4 files changed

+20
-78
lines changed

4 files changed

+20
-78
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"d3-array": "^2.3.0",
3838
"d3-format": "1 - 2",
3939
"d3-interpolate": "1.2.0 - 2",
40-
"d3-time": "1 - 2",
40+
"d3-time": "^2.1.1",
4141
"d3-time-format": "2 - 3"
4242
},
4343
"devDependencies": {

‎src/time.js

+8-73
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
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";
32
import {timeFormat} from "d3-time-format";
43
import continuous, {copy} from "./continuous.js";
54
import {initRange} from "./init.js";
65
import nice from "./nice.js";
76

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-
167
function date(t) {
178
return new Date(t);
189
}
@@ -21,7 +12,7 @@ function number(t) {
2112
return t instanceof Date ? +t : +new Date(+t);
2213
}
2314

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) {
2516
var scale = continuous(),
2617
invert = scale.invert,
2718
domain = scale.domain;
@@ -35,27 +26,6 @@ export function calendar(year, month, week, day, hour, minute, second, milliseco
3526
formatMonth = format("%B"),
3627
formatYear = format("%Y");
3728

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-
5929
function tickFormat(date) {
6030
return (second(date) < date ? formatMillisecond
6131
: minute(date) < date ? formatSecond
@@ -66,33 +36,6 @@ export function calendar(year, month, week, day, hour, minute, second, milliseco
6636
: formatYear)(date);
6737
}
6838

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-
9639
scale.invert = function(y) {
9740
return new Date(invert(y));
9841
};
@@ -102,15 +45,8 @@ export function calendar(year, month, week, day, hour, minute, second, milliseco
10245
};
10346

10447
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);
11450
};
11551

11652
scale.tickFormat = function(count, specifier) {
@@ -119,18 +55,17 @@ export function calendar(year, month, week, day, hour, minute, second, milliseco
11955

12056
scale.nice = function(interval) {
12157
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;
12560
};
12661

12762
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));
12964
};
13065

13166
return scale;
13267
}
13368

13469
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);
13671
}

‎src/utcTime.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {calendar} from "./time.js";
1+
import {utcYear, utcMonth, utcWeek, utcDay, utcHour, utcMinute, utcSecond, utcTicks, utcTickInterval} from "d3-time";
22
import {utcFormat} from "d3-time-format";
3-
import {utcYear, utcMonth, utcWeek, utcDay, utcHour, utcMinute, utcSecond, utcMillisecond} from "d3-time";
3+
import {calendar} from "./time.js";
44
import {initRange} from "./init.js";
55

66
export default function utcTime() {
7-
return initRange.apply(calendar(utcYear, utcMonth, utcWeek, utcDay, utcHour, utcMinute, utcSecond, utcMillisecond, utcFormat).domain([Date.UTC(2000, 0, 1), Date.UTC(2000, 0, 2)]), arguments);
7+
return initRange.apply(calendar(utcTicks, utcTickInterval, utcYear, utcMonth, utcWeek, utcDay, utcHour, utcMinute, utcSecond, utcFormat).domain([Date.UTC(2000, 0, 1), Date.UTC(2000, 0, 2)]), arguments);
88
}

‎yarn.lock

+8-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ cross-spawn@^7.0.2:
207207
shebang-command "^2.0.0"
208208
which "^2.0.1"
209209

210-
d3-array@^2.3.0:
210+
d3-array@2, d3-array@^2.3.0:
211211
version "2.12.1"
212212
resolved "https://registry.yarnpkg.com/d3-array/-/d3-array-2.12.1.tgz#e20b41aafcdffdf5d50928004ececf815a465e81"
213213
integrity sha512-B0ErZK/66mHtEsR1TkPEEkwdy+WDesimkM5gpZr5Dsg54BiTA5RXtYW5qTLIAcekaS9xfZrzBLF/OAkB3Qn1YQ==
@@ -243,6 +243,13 @@ d3-array@^2.3.0:
243243
resolved "https://registry.yarnpkg.com/d3-time/-/d3-time-2.0.0.tgz#ad7c127d17c67bd57a4c61f3eaecb81108b1e0ab"
244244
integrity sha512-2mvhstTFcMvwStWd9Tj3e6CEqtOivtD8AUiHT8ido/xmzrI9ijrUUihZ6nHuf/vsScRBonagOdj0Vv+SEL5G3Q==
245245

246+
d3-time@^2.1.1:
247+
version "2.1.1"
248+
resolved "https://registry.yarnpkg.com/d3-time/-/d3-time-2.1.1.tgz#e9d8a8a88691f4548e68ca085e5ff956724a6682"
249+
integrity sha512-/eIQe/eR4kCQwq7yxi7z4c6qEXf2IYGcjoWB5OOQy4Tq9Uv39/947qlDcN2TLkiTzQWzvnsuYPB9TrWaNfipKQ==
250+
dependencies:
251+
d3-array "2"
252+
246253
debug@^4.0.1, debug@^4.1.1:
247254
version "4.3.1"
248255
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.1.tgz#f0d229c505e0c6d8c49ac553d1b13dc183f6b2ee"

3 commit comments

Comments
 (3)

Pringels commented on May 11, 2021

@Pringels

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 commented on May 11, 2021

@curran
Contributor

@Pringels In what way would they break?

Pringels commented on May 18, 2021

@Pringels

@curran because d3-scale is now incompatible with versions of d3-time lower than 2.1.x

Please sign in to comment.