Skip to content

Commit 05c762d

Browse files
committedNov 10, 2016
date interval milliseconds constants
1 parent d807259 commit 05c762d

File tree

4 files changed

+57
-34
lines changed

4 files changed

+57
-34
lines changed
 

‎src/constants/numerical.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,18 @@ module.exports = {
2323
* to avoid glitches: Make sure that even when you multiply it by the
2424
* number of pixels on a giant screen it still works
2525
*/
26-
FP_SAFE: Number.MAX_VALUE / 10000
26+
FP_SAFE: Number.MAX_VALUE / 10000,
27+
28+
/*
29+
* conversion of date units to milliseconds
30+
* year and month constants are marked "AVG"
31+
* to remind us that not all years and months
32+
* have the same length
33+
*/
34+
ONEAVGYEAR: 31557600000, // 365.25 days
35+
ONEAVGMONTH: 2629800000, // 1/12 of ONEAVGYEAR
36+
ONEDAY: 86400000,
37+
ONEHOUR: 3600000,
38+
ONEMIN: 60000,
39+
ONESEC: 1000
2740
};

‎src/lib/dates.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111

1212
var d3 = require('d3');
1313
var isNumeric = require('fast-isnumeric');
14-
var BADNUM = require('../constants/numerical').BADNUM;
14+
1515
var logError = require('./loggers').error;
1616

17+
var constants = require('../constants/numerical');
18+
var BADNUM = constants.BADNUM;
19+
var ONEDAY = constants.ONEDAY;
20+
var ONEHOUR = constants.ONEHOUR;
21+
var ONEMIN = constants.ONEMIN;
22+
var ONESEC = constants.ONESEC;
23+
1724
// is an object a javascript date?
1825
exports.isJSDate = function(v) {
1926
return typeof v === 'object' && v !== null && typeof v.getTime === 'function';
@@ -140,14 +147,14 @@ exports.dateTime2ms = function(s) {
140147
// minute - must be 2 digits
141148
m = Number(p[1]);
142149
if(p[1].length !== 2 || !(m >= 0 && m <= 59)) return BADNUM;
143-
d += 60000 * m;
150+
d += ONEMIN * m;
144151
if(p.length === 2) return d;
145152

146153
// second (and milliseconds) - must have 2-digit seconds
147154
if(p[2].split('.')[0].length !== 2) return BADNUM;
148155
s = Number(p[2]);
149156
if(!(s >= 0 && s < 60)) return BADNUM;
150-
return d + s * 1000;
157+
return d + s * ONESEC;
151158
}
152159
}
153160
}
@@ -176,6 +183,9 @@ function lpad(val, digits) {
176183
* Optional range r is the data range that applies, also in ms.
177184
* If rng is big, the later parts of time will be omitted
178185
*/
186+
var NINETYDAYS = 90 * ONEDAY;
187+
var THREEHOURS = 3 * ONEHOUR;
188+
var FIVEMIN = 5 * ONEMIN;
179189
exports.ms2DateTime = function(ms, r) {
180190
if(typeof ms !== 'number' || !(ms >= MIN_MS && ms <= MAX_MS)) return BADNUM;
181191

@@ -184,12 +194,12 @@ exports.ms2DateTime = function(ms, r) {
184194
var d = new Date(Math.floor(ms)),
185195
dateStr = d3.time.format('%Y-%m-%d')(d),
186196
// <90 days: add hours and minutes - never *only* add hours
187-
h = (r < 7776000000) ? d.getHours() : 0,
188-
m = (r < 7776000000) ? d.getMinutes() : 0,
197+
h = (r < NINETYDAYS) ? d.getHours() : 0,
198+
m = (r < NINETYDAYS) ? d.getMinutes() : 0,
189199
// <3 hours: add seconds
190-
s = (r < 10800000) ? d.getSeconds() : 0,
200+
s = (r < THREEHOURS) ? d.getSeconds() : 0,
191201
// <5 minutes: add ms (plus one extra digit, this is msec*10)
192-
msec10 = (r < 300000) ? Math.round((d.getMilliseconds() + (((ms % 1) + 1) % 1)) * 10) : 0;
202+
msec10 = (r < FIVEMIN) ? Math.round((d.getMilliseconds() + (((ms % 1) + 1) % 1)) * 10) : 0;
193203

194204
// include each part that has nonzero data in or after it
195205
if(h || m || s || msec10) {

‎src/plots/cartesian/axes.js

+24-25
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ var Titles = require('../../components/titles');
1919
var Color = require('../../components/color');
2020
var Drawing = require('../../components/drawing');
2121

22-
var FP_SAFE = require('../../constants/numerical').FP_SAFE;
22+
var constants = require('../../constants/numerical');
23+
var FP_SAFE = constants.FP_SAFE;
24+
var ONEAVGYEAR = constants.ONEAVGYEAR;
25+
var ONEAVGMONTH = constants.ONEAVGMONTH;
26+
var ONEDAY = constants.ONEDAY;
27+
var ONEHOUR = constants.ONEHOUR;
28+
var ONEMIN = constants.ONEMIN;
29+
var ONESEC = constants.ONESEC;
2330

2431

2532
var axes = module.exports = {};
@@ -727,7 +734,7 @@ function roundDTick(roughDTick, base, roundingSet) {
727734
// outputs (into ax):
728735
// tick0: starting point for ticks (not necessarily on the graph)
729736
// usually 0 for numeric (=10^0=1 for log) or jan 1, 2000 for dates
730-
// dtick: the actual, nice round tick spacing, somewhat larger than roughDTick
737+
// dtick: the actual, nice round tick spacing, usually a little larger than roughDTick
731738
// if the ticks are spaced linearly (linear scale, categories,
732739
// log with only full powers, date ticks < month),
733740
// this will just be a number
@@ -741,37 +748,34 @@ axes.autoTicks = function(ax, roughDTick) {
741748

742749
if(ax.type === 'date') {
743750
ax.tick0 = '2000-01-01';
751+
// the criteria below are all based on the rough spacing we calculate
752+
// being > half of the final unit - so precalculate twice the rough val
753+
var roughX2 = 2 * roughDTick;
744754

745-
if(roughDTick > 15778800000) {
746-
// years if roughDTick > 6mo
747-
roughDTick /= 31557600000;
755+
if(roughX2 > ONEAVGYEAR) {
756+
roughDTick /= ONEAVGYEAR;
748757
base = Math.pow(10, Math.floor(Math.log(roughDTick) / Math.LN10));
749758
ax.dtick = 'M' + (12 * roundDTick(roughDTick, base, roundBase10));
750759
}
751-
else if(roughDTick > 1209600000) {
752-
// months if roughDTick > 2wk
753-
roughDTick /= 2629800000;
760+
else if(roughX2 > ONEAVGMONTH) {
761+
roughDTick /= ONEAVGMONTH;
754762
ax.dtick = 'M' + roundDTick(roughDTick, 1, roundBase24);
755763
}
756-
else if(roughDTick > 43200000) {
757-
// days if roughDTick > 12h
758-
ax.dtick = roundDTick(roughDTick, 86400000, roundDays);
764+
else if(roughX2 > ONEDAY) {
765+
ax.dtick = roundDTick(roughDTick, ONEDAY, roundDays);
759766
// get week ticks on sunday
760767
// this will also move the base tick off 2000-01-01 if dtick is
761768
// 2 or 3 days... but that's a weird enough case that we'll ignore it.
762769
ax.tick0 = '2000-01-02';
763770
}
764-
else if(roughDTick > 1800000) {
765-
// hours if roughDTick > 30m
766-
ax.dtick = roundDTick(roughDTick, 3600000, roundBase24);
771+
else if(roughX2 > ONEHOUR) {
772+
ax.dtick = roundDTick(roughDTick, ONEHOUR, roundBase24);
767773
}
768-
else if(roughDTick > 30000) {
769-
// minutes if roughDTick > 30sec
770-
ax.dtick = roundDTick(roughDTick, 60000, roundBase60);
774+
else if(roughX2 > ONEMIN) {
775+
ax.dtick = roundDTick(roughDTick, ONEMIN, roundBase60);
771776
}
772-
else if(roughDTick > 500) {
773-
// seconds if roughDTick > 0.5sec
774-
ax.dtick = roundDTick(roughDTick, 1000, roundBase60);
777+
else if(roughX2 > ONESEC) {
778+
ax.dtick = roundDTick(roughDTick, ONESEC, roundBase60);
775779
}
776780
else {
777781
// milliseconds
@@ -826,11 +830,6 @@ axes.autoTicks = function(ax, roughDTick) {
826830
}
827831
};
828832

829-
var ONEDAY = 86400000,
830-
ONEHOUR = 3600000,
831-
ONEMIN = 60000,
832-
ONESEC = 1000;
833-
834833
// after dtick is already known, find tickround = precision
835834
// to display in tick labels
836835
// for numeric ticks, integer # digits after . to round to

‎src/plots/cartesian/tick_value_defaults.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var isNumeric = require('fast-isnumeric');
1313
var Lib = require('../../lib');
14+
var ONEDAY = require('../../constants/numerical').ONEDAY;
1415

1516

1617
module.exports = function handleTickValueDefaults(containerIn, containerOut, coerce, axType) {
@@ -32,7 +33,7 @@ module.exports = function handleTickValueDefaults(containerIn, containerOut, coe
3233
// dtick is usually a positive number, but there are some
3334
// special strings available for log or date axes
3435
// default is 1 day for dates, otherwise 1
35-
var dtickDflt = (axType === 'date') ? 86400000 : 1;
36+
var dtickDflt = (axType === 'date') ? ONEDAY : 1;
3637
var dtick = coerce('dtick', dtickDflt);
3738
if(isNumeric(dtick)) {
3839
containerOut.dtick = (dtick > 0) ? Number(dtick) : dtickDflt;

0 commit comments

Comments
 (0)
Please sign in to comment.