Skip to content

Commit 14c5512

Browse files
committed
improve monthly and yearly periods and implement (x|y)period0
1 parent 1948e03 commit 14c5512

31 files changed

+1232
-81
lines changed

src/plots/cartesian/align_period.js

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,61 +9,103 @@
99
'use strict';
1010

1111
var isNumeric = require('fast-isnumeric');
12-
var ms2DateTime = require('../../lib').ms2DateTime;
13-
var ONEDAY = require('../../constants/numerical').ONEDAY;
12+
var Lib = require('../../lib');
13+
var ms2DateTime = Lib.ms2DateTime;
14+
var dateTime2ms = Lib.dateTime2ms;
15+
var constants = require('../../constants/numerical');
16+
var ONEDAY = constants.ONEDAY;
17+
var ONEAVGMONTH = constants.ONEAVGMONTH;
18+
var ONEAVGYEAR = constants.ONEAVGYEAR;
1419

1520
module.exports = function alignPeriod(trace, ax, axLetter, vals) {
1621
var alignment = trace[axLetter + 'periodalignment'];
17-
if(!alignment || alignment === 'start') return vals;
22+
if(!alignment) return vals;
1823

19-
var dynamic = false;
2024
var period = trace[axLetter + 'period'];
2125
if(isNumeric(period)) {
22-
period = +period; // milliseconds
26+
period = +period;
2327
if(period <= 0) return vals;
2428
} else if(typeof period === 'string' && period.charAt(0) === 'M') {
25-
var v = +(period.substring(1));
26-
if(v > 0 && Math.round(v) === v) period = v; // positive integer months
29+
var n = +(period.substring(1));
30+
if(n > 0 && Math.round(n) === n) period = n * ONEAVGMONTH;
2731
else return vals;
28-
29-
dynamic = true;
3032
}
3133

3234
if(period > 0) {
33-
var ratio = (alignment === 'end') ? 1 : 0.5;
35+
var calendar = ax.calendar;
36+
37+
var isStart = 'start' === alignment;
38+
// var isMiddle = 'middle' === alignment;
39+
var isEnd = 'end' === alignment;
40+
41+
var offset = (new Date()).getTimezoneOffset() * 60000;
42+
var period0 = trace[axLetter + 'period0'];
43+
var base = (dateTime2ms(period0, calendar) || 0) - offset;
3444

3545
var len = vals.length;
3646
for(var i = 0; i < len; i++) {
37-
var delta;
38-
39-
if(dynamic) {
40-
var dateStr = ms2DateTime(vals[i], 0, ax.calendar);
41-
var d = new Date(dateStr);
42-
var year = d.getFullYear();
43-
var month = d.getMonth() + 1;
44-
45-
var totalDaysInMonths = 0;
46-
for(var k = 0; k < period; k++) {
47-
month += 1;
48-
if(month > 12) {
49-
month = 1;
50-
year++;
51-
}
47+
var v = vals[i] - base;
48+
49+
var dateStr = ms2DateTime(v, 0, calendar);
50+
var O = new Date(dateStr);
51+
var year = O.getFullYear();
52+
var month = O.getMonth();
53+
var day = O.getDate();
54+
55+
var newD;
56+
var startTime;
57+
var endTime;
5258

53-
var monthDays = (
54-
new Date(year, month, 0)
55-
).getDate();
59+
var nYears = Math.floor(period / ONEAVGYEAR);
60+
var nMonths = Math.floor(period / ONEAVGMONTH) % 12;
61+
var nDays = Math.floor((period - nYears * ONEAVGYEAR - nMonths * ONEAVGMONTH) / ONEDAY);
62+
if(nYears && nMonths) nDays = 0;
5663

57-
totalDaysInMonths += monthDays;
64+
var y1 = year + nYears;
65+
var m1 = month + nMonths;
66+
var d1 = day + nDays;
67+
if(nDays || nMonths || nYears) {
68+
if(nDays) {
69+
startTime = (new Date(year, month, day)).getTime();
70+
var monthDays = new Date(y1, m1, 0).getDate();
71+
if(d1 > monthDays) {
72+
d1 -= monthDays;
73+
m1 += 1;
74+
if(m1 > 11) {
75+
m1 -= 12;
76+
y1 += 1;
77+
}
78+
}
79+
endTime = (new Date(y1, m1, d1)).getTime();
80+
} else if(nMonths) {
81+
startTime = (new Date(year, nYears ? month : roundMonth(month, nMonths))).getTime();
82+
if(m1 > 11) {
83+
m1 -= 12;
84+
y1 += 1;
85+
}
86+
endTime = (new Date(y1, nYears ? m1 : roundMonth(m1, nMonths))).getTime();
87+
} else {
88+
startTime = (new Date(year, 0)).getTime();
89+
endTime = (new Date(y1, 0)).getTime();
5890
}
5991

60-
delta = ONEDAY * totalDaysInMonths; // convert to ms
61-
} else {
62-
delta = period;
92+
newD = new Date(
93+
isStart ? startTime :
94+
isEnd ? endTime :
95+
(startTime + endTime) / 2
96+
);
6397
}
6498

65-
vals[i] += ratio * delta;
99+
if(newD) {
100+
vals[i] = newD.getTime() + base;
101+
}
66102
}
67103
}
68104
return vals;
69105
};
106+
107+
var monthSteps = [2, 3, 4, 6];
108+
109+
function roundMonth(month, step) {
110+
return (monthSteps.indexOf(step) === -1) ? month : Math.floor(month / step) * step;
111+
}

src/traces/bar/attributes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ module.exports = {
6161

6262
xperiod: scatterAttrs.xperiod,
6363
yperiod: scatterAttrs.yperiod,
64+
xperiod0: scatterAttrs.xperiod0,
65+
yperiod0: scatterAttrs.yperiod0,
6466
xperiodalignment: scatterAttrs.xperiodalignment,
6567
yperiodalignment: scatterAttrs.yperiodalignment,
6668

src/traces/box/attributes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ module.exports = {
7878

7979
xperiod: scatterAttrs.xperiod,
8080
yperiod: scatterAttrs.yperiod,
81+
xperiod0: scatterAttrs.xperiod0,
82+
yperiod0: scatterAttrs.yperiod0,
8183
xperiodalignment: scatterAttrs.xperiodalignment,
8284
yperiodalignment: scatterAttrs.yperiodalignment,
8385

src/traces/candlestick/attributes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ function directionAttrs(lineColorDefault) {
2828

2929
module.exports = {
3030
xperiod: OHLCattrs.xperiod,
31+
xperiod0: OHLCattrs.xperiod0,
3132
xperiodalignment: OHLCattrs.xperiodalignment,
3233

3334
x: OHLCattrs.x,

src/traces/contour/attributes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ module.exports = extendFlat({
3434

3535
xperiod: heatmapAttrs.xperiod,
3636
yperiod: heatmapAttrs.yperiod,
37+
xperiod0: scatterAttrs.xperiod0,
38+
yperiod0: scatterAttrs.yperiod0,
3739
xperiodalignment: heatmapAttrs.xperiodalignment,
3840
yperiodalignment: heatmapAttrs.yperiodalignment,
3941

src/traces/funnel/attributes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module.exports = {
2727

2828
xperiod: barAttrs.xperiod,
2929
yperiod: barAttrs.yperiod,
30+
xperiod0: barAttrs.xperiod0,
31+
yperiod0: barAttrs.yperiod0,
3032
xperiodalignment: barAttrs.xperiodalignment,
3133
yperiodalignment: barAttrs.yperiodalignment,
3234

src/traces/heatmap/attributes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ module.exports = extendFlat({
3131

3232
xperiod: extendFlat({}, scatterAttrs.xperiod, {impliedEdits: {xtype: 'scaled'}}),
3333
yperiod: extendFlat({}, scatterAttrs.yperiod, {impliedEdits: {ytype: 'scaled'}}),
34+
xperiod0: extendFlat({}, scatterAttrs.xperiod0, {impliedEdits: {xtype: 'scaled'}}),
35+
yperiod0: extendFlat({}, scatterAttrs.yperiod0, {impliedEdits: {ytype: 'scaled'}}),
3436
xperiodalignment: extendFlat({}, scatterAttrs.xperiodalignment, {impliedEdits: {xtype: 'scaled'}}),
3537
yperiodalignment: extendFlat({}, scatterAttrs.yperiodalignment, {impliedEdits: {ytype: 'scaled'}}),
3638

src/traces/histogram/attributes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ module.exports = {
3232

3333
xperiod: barAttrs.xperiod,
3434
yperiod: barAttrs.yperiod,
35+
xperiod0: barAttrs.xperiod0,
36+
yperiod0: barAttrs.yperiod0,
3537
xperiodalignment: barAttrs.xperiodalignment,
3638
yperiodalignment: barAttrs.yperiodalignment,
3739

src/traces/histogram2d/attributes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ module.exports = extendFlat(
2424

2525
xperiod: histogramAttrs.xperiod,
2626
yperiod: histogramAttrs.yperiod,
27+
xperiod0: histogramAttrs.xperiod0,
28+
yperiod0: histogramAttrs.yperiod0,
2729
xperiodalignment: histogramAttrs.xperiodalignment,
2830
yperiodalignment: histogramAttrs.yperiodalignment,
2931

src/traces/histogram2dcontour/attributes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ var extendFlat = require('../../lib/extend').extendFlat;
1717
module.exports = extendFlat({
1818
xperiod: histogram2dAttrs.xperiod,
1919
yperiod: histogram2dAttrs.yperiod,
20+
xperiod0: histogram2dAttrs.xperiod0,
21+
yperiod0: histogram2dAttrs.yperiod0,
2022
xperiodalignment: histogram2dAttrs.xperiodalignment,
2123
yperiodalignment: histogram2dAttrs.yperiodalignment,
2224

0 commit comments

Comments
 (0)