forked from MattWoelk/shmotg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmsToCentury.js
360 lines (299 loc) · 12.9 KB
/
msToCentury.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
///////////////////////////////////////
// Custom Time Axis //
// ---------------- //
// - Allows for smooth scrolling at //
// smaller-than-millisecond zoom //
// levels //
// - Provides time axis labeling //
// see customTimeFormat //
// - Depends on underscore.js, d3.js //
///////////////////////////////////////
// USAGE
// ----------------
// xAxis = d3.svg.axis()
// .tickFormat(msToCentury.TickFormat)
// .tickValues(msToCentury.TickValues(xScale, width))
// .tickSubdivide(msToCentury.TickSubDivide(xScale, width))
// .scale(xScale).orient("bottom");
///////////////////////////////////////
msToCentury = function() {
var msToCentury = {};
var MIN_DISTANCE_BETWEEN_X_AXIS_LABELS = 100;
function dt (num) {
var newdate = new Date();
newdate.setTime(num);
return newdate;
}
function millisecond(val) {
var newdate = new Date();
newdate.setTime(roundDownToNearestTime(val, times.ms));
return newdate;
}
function roundUpToNearestTime(val, tim) {
return Math.ceil(val/tim) * tim;
}
function roundDownToNearestTime(val, tim) {
return Math.floor(val/tim) * tim;
}
function getNumberOfDaysInCurrentMonth(dat) {
var curmo = dat.getMonth();
var addYear;
if (( curmo + 1 ) / 12.0 >= 1.0) {
// we rolled over to the next year
addYear = dat.getFullYear() + 1;
} else {
addYear = dat.getFullYear();
}
var newdate = new Date(
addYear,
(curmo + 1) % 12,
1,
1,
1,
1,
1);
newdate = dt(newdate.getTime() - 4000000);
return newdate.getDate();
}
var times = {
ms: 1, //milliseconds
s: 1000, //seconds
m: 6e4, //minutes
h: 36e5, //hours
d: 864e5, //days
// These are approximations:
mo: 2592e6, //months
y: 31536e6, //years
};
msToCentury.times = times;
msToCentury.NumberOfDaysInYear = function (dat) {
var newdateStart = new Date(dat.getFullYear() , 0, 0);
var newdateEnd = new Date(dat.getFullYear() + 1, 0, 0);
var diff = newdateEnd.getTime() - newdateStart.getTime();
var oneDay = 1000 * 60 * 60 * 24;
return Math.floor(diff / oneDay);
};
msToCentury.TickFormat = function (ti) {
// custom formatting for x axis time
function timeFormat(formats) {
return function(date) {
var newdate = new Date();
newdate.setTime(date);
var i = formats.length - 1, f = formats[i];
while (!f[1](newdate)) f = formats[--i];
return f[0](newdate);
};
}
var customTimeFormat = timeFormat([
[ d3.time.format("%Y") , function() { return true; } ],
[ d3.time.format("%b") , function(d) { return d.getMonth(); } ],
[ function (d) { return d3.time.format("%a %-d")(d).toLowerCase(); } ,
function(d) { return d.getDate() != 1; } ],
[ d3.time.format("%H:00") , function(d) { return d.getHours(); } ],
[ d3.time.format("%H:%M") , function(d) { return d.getMinutes(); } ],
[ d3.time.format("%Ss") , function(d) { return d.getSeconds(); } ],
[ d3.time.format("%Lms") , function(d) { return d.getMilliseconds(); } ]
]);
return function() { return customTimeFormat(ti); }();
};
function onScreenSizeOfLabels(millisecondsPerLabel, screenWidth, distanceBtwnLabels) {
return millisecondsPerLabel * screenWidth / distanceBtwnLabels;
}
function findLevel(dom, wid) {
var numInDom = function (num) {
return num < dom[1] && num > dom[0];
};
for (var i = 0; i < rounding_scales.length; i++) {
var ro = rounding_scales[i];
var compr = onScreenSizeOfLabels(ro[0]*ro[1], wid, MIN_DISTANCE_BETWEEN_X_AXIS_LABELS);
if (dom[1] - dom[0] <= compr ) {
var result = makeTickRange(dom[0], dom[1], ro[1], ro[3], ro[2], ro[0]*ro[1], wid);
// filter this for only what is actually on-screen.
result = _.filter(result, numInDom);
return i;
}
}
return -1;
}
msToCentury.TickValues = function (scal, wid) {
var dom = scal.domain();
var lvl = findLevel(dom, wid);
// This should never occur if the zoom limits are correct
if (lvl === -1) {
return [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14];
}
var ro = rounding_scales[lvl];
var rng = makeTickRange(dom[0], dom[1], ro[1], ro[3], ro[2], ro[0]*ro[1], wid);
// filter this for only what is actually on-screen.
var result = _.filter(rng, function (num) {
return num < dom[1] && num > dom[0];
});
return result;
};
msToCentury.SubTickValues = function (scal, wid) {
var dom = scal.domain();
var lvl = findLevel(dom, wid);
// This should never occur if the zoom limits are correct
if (lvl === -1) {
return [1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14];
}
var ro = rounding_scales[lvl];
var rng = makeTickRange(dom[0], dom[1], ro[1], ro[3], ro[2], ro[0]*ro[1], wid);
var res = [];
var numToInterpolate = msToCenturyTickSubDivide(scal, wid);
var whichToGet = d3.range(1, numToInterpolate);
var spaceBetweenMajorTicks = ro[0]*ro[1];
for (var b = 0; b < whichToGet.length; b++) {
res.push(rng[0] - whichToGet[b]*(spaceBetweenMajorTicks / numToInterpolate));
}
for (var i = 0; i < rng.length; i++) {
for (var c = 0; c < whichToGet.length; c++) {
res.push(rng[i] + whichToGet[c]*(spaceBetweenMajorTicks / numToInterpolate));
}
}
// filter this for only what is actually on-screen.
var result = _.filter(res, function (num) {
return num < dom[1] && num > dom[0];
});
return result;
};
function msToCenturyTickSubDivide(scal, wid) {
var dom = scal.domain();
var lvl = findLevel(dom, wid);
if (lvl === -1) { return 0; }
var baseSize = rounding_scales[lvl][1];
var tickSpace = rounding_ticks[baseSize];
return (baseSize / tickSpace);
}
function makeTickRange(start, end, increment, incrementOf, baseFunc, smallInc, wid) {
var startyear, endyear, curange;
if ( incrementOf === d3.time.year ) {
startyear = d3.time.year.floor(dt(start));
endyear = d3.time.year.ceil( dt(end ));
curange = d3.range(startyear.getFullYear(), endyear.getFullYear());
// Filter for proper increments
curange = _.filter(curange, function (d) {
return d % increment === 0;
});
curange = _.map(curange, function (d) { return (new Date(d, 0)).getTime(); });
return curange;
} else if ( incrementOf === d3.time.month ) {
startyear = d3.time.year.floor(dt(start));
endyear = d3.time.year.ceil( dt(end ));
curange = d3.range(startyear.getFullYear(), endyear.getFullYear());
// for each year, get all of the months for it
curange = _.map(curange, function (d) {
return _.map([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11], function (f) {
// For each month of the year
return (new Date(d, f)).getTime();
});
});
curange = _.flatten(curange);
curange = _.filter(curange, function (d, i) {
// Filter for proper increments
return i % increment === 0;
});
return curange;
} else if (baseFunc === d3.time.month){
startyear = d3.time.year.floor(dt(start));
endyear = d3.time.year.ceil( dt(end ));
curange = d3.range(startyear.getFullYear(), endyear.getFullYear());
// For each year, get all of the months for it
curange = _.map(curange, function (year) {
return _.map([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,11], function (month) {
// For each month of the year
var monthDays = getNumberOfDaysInCurrentMonth(new Date(year, month));
return _.map(d3.range(1, monthDays + 1), function (day) {
// For each day of the month
// Filter for proper increments
// and remove ones which are too close
// together near the ends of the months
if ((day - 1) % increment === 0 && monthDays + 1 - day >= increment ) {
return (new Date(year, month, day)).getTime();
} else {
return [];
}
});
});
});
curange = _.flatten(curange);
return curange;
} else { // For everything smaller than days
return d3.range( baseFunc.floor( dt(start) ).getTime(),
baseFunc.ceil( dt( end ) ).getTime(),
roundUpToNearestTime(
smallInc*MIN_DISTANCE_BETWEEN_X_AXIS_LABELS/wid,
smallInc));
}
}
// for major ticks every 'a' units,
// have a minor tick every 'b' units
// [a, b]
// b should always be a factor of a
var rounding_ticks = {
1 : 0.5, // when we are showing 1 of something, display a tick every 0.5
2 : 1 ,
3 : 1 ,
5 : 1 ,
6 : 3 , // when we are showing 6 of something, display a tick every 3
10 : 5 ,
12 : 3 ,
15 : 5 ,
20 : 5 ,
25 : 5 ,
30 : 10 ,
50 : 10 ,
100 : 50 ,
200 : 50 ,
500 : 100,
};
// Data object to help make custom axis' tick values
// [ estimate size in milliseconds,
// how many to increment,
// precise time rounder for anchoring,
// precise time rounder ]
var rounding_scales = [
[ times.ms , 1 , d3.time.second , millisecond],
[ times.ms , 2 , d3.time.second , millisecond],
[ times.ms , 5 , d3.time.second , millisecond],
[ times.ms , 10 , d3.time.second , millisecond],
[ times.ms , 20 , d3.time.second , millisecond],
[ times.ms , 50 , d3.time.second , millisecond],
[ times.ms , 100 , d3.time.second , millisecond],
[ times.ms , 200 , d3.time.second , millisecond],
[ times.ms , 500 , d3.time.second , millisecond],
[ times.s , 1 , d3.time.minute , d3.time.second],
[ times.s , 2 , d3.time.minute , d3.time.second],
[ times.s , 5 , d3.time.minute , d3.time.second],
[ times.s , 15 , d3.time.minute , d3.time.second],
[ times.s , 30 , d3.time.minute , d3.time.second],
[ times.m , 1 , d3.time.hour , d3.time.minute],
[ times.m , 2 , d3.time.hour , d3.time.minute],
[ times.m , 5 , d3.time.hour , d3.time.minute],
[ times.m , 15 , d3.time.hour , d3.time.minute],
[ times.m , 30 , d3.time.hour , d3.time.minute],
[ times.h , 1 , d3.time.day , d3.time.hour],
[ times.h , 3 , d3.time.day , d3.time.hour],
[ times.h , 6 , d3.time.day , d3.time.hour],
[ times.h , 12 , d3.time.day , d3.time.hour],
[ times.d , 1 , d3.time.month , d3.time.day],
[ times.d , 2 , d3.time.month , d3.time.day],
[ times.d , 5 , d3.time.month , d3.time.day],
[ times.d , 10 , d3.time.month , d3.time.day],
[ times.d , 15 , d3.time.month , d3.time.day],
[ times.mo , 1 , d3.time.year , d3.time.month],
[ times.mo , 2 , d3.time.year , d3.time.month],
[ times.mo , 3 , d3.time.year , d3.time.month],
[ times.mo , 6 , d3.time.year , d3.time.month],
[ times.mo , 12 , d3.time.year , d3.time.month],
[ times.y , 1 , d3.time.year , d3.time.year],
[ times.y , 2 , d3.time.year , d3.time.year],
[ times.y , 5 , d3.time.year , d3.time.year],
[ times.y , 10 , d3.time.year , d3.time.year],
[ times.y , 25 , d3.time.year , d3.time.year],
[ times.y , 50 , d3.time.year , d3.time.year],
[ times.y , 100, d3.time.year , d3.time.year],
[ times.y , 100, d3.time.year , d3.time.year],
];
return msToCentury;
}();