Skip to content

Commit 80f47d6

Browse files
committed
adjust tick0 for weekly dticks
1 parent b521323 commit 80f47d6

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

src/lib/dates.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,31 @@ function isWorldCalendar(calendar) {
4545
/*
4646
* dateTick0: get the canonical tick for this calendar
4747
*
48+
* integer weekdays : Saturday: 0, Sunday: 1, Monday: 2, etc.
49+
*/
50+
exports.dateTick0 = function(calendar, dayOfWeek) {
51+
var tick0 = _dateTick0(calendar, !!dayOfWeek);
52+
if(dayOfWeek < 2) return tick0;
53+
54+
var v = exports.dateTime2ms(tick0, calendar);
55+
v += ONEDAY * (dayOfWeek - 1); // shift Sunday to Monday, etc.
56+
return exports.ms2DateTime(v, 0, calendar);
57+
};
58+
59+
/*
60+
* _dateTick0: get the canonical tick for this calendar
61+
*
4862
* bool sunday is for week ticks, shift it to a Sunday.
4963
*/
50-
exports.dateTick0 = function(calendar, sunday) {
64+
function _dateTick0(calendar, sunday) {
5165
if(isWorldCalendar(calendar)) {
5266
return sunday ?
5367
Registry.getComponentMethod('calendars', 'CANONICAL_SUNDAY')[calendar] :
5468
Registry.getComponentMethod('calendars', 'CANONICAL_TICK')[calendar];
5569
} else {
5670
return sunday ? '2000-01-02' : '2000-01-01';
5771
}
58-
};
72+
}
5973

6074
/*
6175
* dfltRange: for each calendar, give a valid default range

src/plots/cartesian/axes.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,7 @@ axes.autoTicks = function(ax, roughDTick) {
965965
}
966966

967967
if(ax.type === 'date') {
968-
ax.tick0 = Lib.dateTick0(ax.calendar);
968+
ax.tick0 = Lib.dateTick0(ax.calendar, 0);
969969
// the criteria below are all based on the rough spacing we calculate
970970
// being > half of the final unit - so precalculate twice the rough val
971971
var roughX2 = 2 * roughDTick;
@@ -982,14 +982,11 @@ axes.autoTicks = function(ax, roughDTick) {
982982
// get week ticks on sunday
983983
// this will also move the base tick off 2000-01-01 if dtick is
984984
// 2 or 3 days... but that's a weird enough case that we'll ignore it.
985-
ax.tick0 = Lib.dateTick0(ax.calendar, true);
986-
987985
var tickformat = axes.getTickFormat(ax);
988986
if(/%[uVW]/.test(tickformat)) {
989-
// replace Sunday with Monday for ISO and Monday-based formats
990-
var len = ax.tick0.length;
991-
var lastD = +ax.tick0[len - 1];
992-
ax.tick0 = ax.tick0.substring(0, len - 2) + String(lastD + 1);
987+
ax.tick0 = Lib.dateTick0(ax.calendar, 2); // Monday
988+
} else {
989+
ax.tick0 = Lib.dateTick0(ax.calendar, 1); // Sunday
993990
}
994991
} else if(roughX2 > ONEHOUR) {
995992
ax.dtick = roundDTick(roughDTick, ONEHOUR, roundBase24);

src/plots/cartesian/clean_ticks.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
var isNumeric = require('fast-isnumeric');
1212
var Lib = require('../../lib');
13-
var ONEDAY = require('../../constants/numerical').ONEDAY;
13+
var constants = require('../../constants/numerical');
14+
var ONEDAY = constants.ONEDAY;
15+
var ONEWEEK = constants.ONEWEEK;
1416

1517
/**
1618
* Return a validated dtick value for this axis
@@ -75,7 +77,9 @@ exports.dtick = function(dtick, axType) {
7577
*/
7678
exports.tick0 = function(tick0, axType, calendar, dtick) {
7779
if(axType === 'date') {
78-
return Lib.cleanDate(tick0, Lib.dateTick0(calendar));
80+
return Lib.cleanDate(tick0,
81+
Lib.dateTick0(calendar, (dtick % ONEWEEK === 0) ? 1 : 0)
82+
);
7983
}
8084
if(dtick === 'D1' || dtick === 'D2') {
8185
// D1 and D2 modes ignore tick0 entirely

src/traces/scatter/period_defaults.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ var numConstants = require('../../constants/numerical');
1313
var ONEWEEK = numConstants.ONEWEEK;
1414

1515
function getPeriod0Dflt(period, calendar) {
16-
var n = period / ONEWEEK;
17-
return dateTick0(calendar, Math.round(n) === n);
16+
if(period % ONEWEEK === 0) {
17+
return dateTick0(calendar, 1); // Sunday
18+
}
19+
return dateTick0(calendar, 0);
1820
}
1921

2022
module.exports = function handlePeriodDefaults(traceIn, traceOut, layout, coerce, opts) {

0 commit comments

Comments
 (0)