Skip to content

Commit c33b49f

Browse files
authored
Millisecond granularity (#893)
* Add milliseconds granularity wtih changes from #777 with suggestions taken into account and mergeable with master. * Fix granularities according to original PR. * Add assertions for millisecond granularities * Add an example for labels below SECONDLY granularity
1 parent 64f1c4d commit c33b49f

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

auto_tests/tests/date_ticker.js

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dygraph-tickers.js

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -220,29 +220,36 @@ export var dateTicker = function(a, b, pixels, opts, dygraph, vals) {
220220

221221
// Time granularity enumeration
222222
export var Granularity = {
223-
SECONDLY: 0,
224-
TWO_SECONDLY: 1,
225-
FIVE_SECONDLY: 2,
226-
TEN_SECONDLY: 3,
227-
THIRTY_SECONDLY : 4,
228-
MINUTELY: 5,
229-
TWO_MINUTELY: 6,
230-
FIVE_MINUTELY: 7,
231-
TEN_MINUTELY: 8,
232-
THIRTY_MINUTELY: 9,
233-
HOURLY: 10,
234-
TWO_HOURLY: 11,
235-
SIX_HOURLY: 12,
236-
DAILY: 13,
237-
TWO_DAILY: 14,
238-
WEEKLY: 15,
239-
MONTHLY: 16,
240-
QUARTERLY: 17,
241-
BIANNUAL: 18,
242-
ANNUAL: 19,
243-
DECADAL: 20,
244-
CENTENNIAL: 21,
245-
NUM_GRANULARITIES: 22
223+
MILLISECONDLY: 0,
224+
TWO_MILLISECONDLY: 1,
225+
FIVE_MILLISECONDLY: 2,
226+
TEN_MILLISECONDLY: 3,
227+
FIFTY_MILLISECONDLY: 4,
228+
HUNDRED_MILLISECONDLY: 5,
229+
FIVE_HUNDRED_MILLISECONDLY: 6,
230+
SECONDLY: 7,
231+
TWO_SECONDLY: 8,
232+
FIVE_SECONDLY: 9,
233+
TEN_SECONDLY: 10,
234+
THIRTY_SECONDLY: 11,
235+
MINUTELY: 12,
236+
TWO_MINUTELY: 13,
237+
FIVE_MINUTELY: 14,
238+
TEN_MINUTELY: 15,
239+
THIRTY_MINUTELY: 16,
240+
HOURLY: 17,
241+
TWO_HOURLY: 18,
242+
SIX_HOURLY: 19,
243+
DAILY: 20,
244+
TWO_DAILY: 21,
245+
WEEKLY: 22,
246+
MONTHLY: 23,
247+
QUARTERLY: 24,
248+
BIANNUAL: 25,
249+
ANNUAL: 26,
250+
DECADAL: 27,
251+
CENTENNIAL: 28,
252+
NUM_GRANULARITIES: 29
246253
}
247254

248255
// Date components enumeration (in the order of the arguments in Date)
@@ -273,6 +280,13 @@ var DateField = {
273280
* @type {Array.<{datefield:number, step:number, spacing:number}>}
274281
*/
275282
var TICK_PLACEMENT = [];
283+
TICK_PLACEMENT[Granularity.MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 1, spacing: 1};
284+
TICK_PLACEMENT[Granularity.TWO_MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 2, spacing: 2};
285+
TICK_PLACEMENT[Granularity.FIVE_MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 5, spacing: 5};
286+
TICK_PLACEMENT[Granularity.TEN_MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 10, spacing: 10};
287+
TICK_PLACEMENT[Granularity.FIFTY_MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 50, spacing: 50};
288+
TICK_PLACEMENT[Granularity.HUNDRED_MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 100, spacing: 100};
289+
TICK_PLACEMENT[Granularity.FIVE_HUNDRED_MILLISECONDLY] = {datefield: DateField.DATEFIELD_MS, step: 500, spacing: 500};
276290
TICK_PLACEMENT[Granularity.SECONDLY] = {datefield: DateField.DATEFIELD_SS, step: 1, spacing: 1000 * 1};
277291
TICK_PLACEMENT[Granularity.TWO_SECONDLY] = {datefield: DateField.DATEFIELD_SS, step: 2, spacing: 1000 * 2};
278292
TICK_PLACEMENT[Granularity.FIVE_SECONDLY] = {datefield: DateField.DATEFIELD_SS, step: 5, spacing: 1000 * 5};

src/dygraph-utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,12 @@ export function dateAxisLabelFormatter(date, granularity, opts) {
12141214
if (frac === 0 || granularity >= DygraphTickers.Granularity.DAILY) {
12151215
// e.g. '21 Jan' (%d%b)
12161216
return zeropad(day) + '&#160;' + SHORT_MONTH_NAMES_[month];
1217+
} else if (granularity < DygraphTickers.Granularity.SECONDLY) {
1218+
// e.g. 40.310 (meaning 40 seconds and 310 milliseconds)
1219+
var str = "" + millis;
1220+
return zeropad(secs) + "." + ('000'+str).substring(str.length);
1221+
} else if (granularity > DygraphTickers.Granularity.MINUTELY) {
1222+
return hmsString_(hours, mins, secs, 0);
12171223
} else {
12181224
return hmsString_(hours, mins, secs, millis);
12191225
}

0 commit comments

Comments
 (0)