Skip to content

Commit 103e710

Browse files
committed
compute the exact number of days for mothly periods
1 parent 8de4021 commit 103e710

File tree

5 files changed

+216
-9
lines changed

5 files changed

+216
-9
lines changed

src/plots/cartesian/align_period.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,61 @@
99
'use strict';
1010

1111
var isNumeric = require('fast-isnumeric');
12-
var ONEAVGMONTH = require('../../constants/numerical').ONEAVGMONTH;
12+
var ms2DateTime = require('../../lib').ms2DateTime;
13+
var ONEDAY = require('../../constants/numerical').ONEDAY;
1314

1415
var M = {};
1516
for(var n = 1; n <= 12; n++) {
16-
M['M' + n] = n * ONEAVGMONTH;
17+
M['M' + n] = n;
1718
}
1819

19-
module.exports = function alignPeriod(trace, axLetter, vals) {
20+
module.exports = function alignPeriod(trace, ax, axLetter, vals) {
2021
var alignment = trace[axLetter + 'periodalignment'];
2122
if(!alignment || alignment === 'start') return vals;
2223

24+
var dynamic = false;
2325
var period = trace[axLetter + 'period'];
2426
if(isNumeric(period)) {
25-
period = +period;
27+
period = +period; // milliseconds
2628
} else if(typeof period === 'string') {
27-
period = M[period];
29+
period = M[period]; // months
30+
dynamic = true;
2831
}
2932

3033
if(period > 0) {
31-
var delta = (alignment === 'end' ? 1 : 0.5) * period;
34+
var ratio = (alignment === 'end') ? 1 : 0.5;
35+
3236
var len = vals.length;
3337
for(var i = 0; i < len; i++) {
34-
vals[i] += delta;
38+
var delta;
39+
40+
if(dynamic) {
41+
var dateStr = ms2DateTime(vals[i], 0, ax.calendar);
42+
var d = new Date(dateStr);
43+
var year = d.getFullYear();
44+
var month = d.getMonth();
45+
46+
var totalDaysInMonths = 0;
47+
for(var k = 0; k < period; k++) {
48+
month += 1;
49+
if(month > 12) {
50+
month = 1;
51+
year++;
52+
}
53+
54+
var monthDays = (
55+
new Date(year, month, 0)
56+
).getDate();
57+
58+
totalDaysInMonths += monthDays;
59+
}
60+
61+
delta = ONEDAY * totalDaysInMonths; // convert to ms
62+
} else {
63+
delta = period;
64+
}
65+
66+
vals[i] += ratio * delta;
3567
}
3668
}
3769
return vals;

src/plots/cartesian/set_convert.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ module.exports = function setConvert(ax, fullLayout) {
849849
}
850850

851851
if(axType === 'date') {
852-
arrayOut = alignPeriod(trace, axLetter, arrayOut);
852+
arrayOut = alignPeriod(trace, ax, axLetter, arrayOut);
853853
}
854854

855855
return arrayOut;

src/traces/scatter/attributes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var extendFlat = require('../../lib/extend').extendFlat;
2323
function axisPeriod(axis) {
2424
return {
2525
valType: 'any',
26-
dflt: ONEAVGYEAR,
26+
dflt: ONEAVGYEAR, // maybe better to use 12M which is more accurate here?
2727
role: 'info',
2828
editType: 'calc',
2929
description: [
42.4 KB
Loading
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
{
2+
"data": [
3+
{
4+
"xaxis": "x",
5+
"yaxis": "y",
6+
"xperiod": "M1",
7+
"name": "bar (M1)",
8+
"type": "bar",
9+
"y": [1, 2, 3, 4],
10+
"x": ["2001-01-01", "2001-02-01", "2001-03-01", "2001-04-01"],
11+
"xperiodalignment": "middle"
12+
},
13+
{
14+
"xaxis": "x2",
15+
"yaxis": "y2",
16+
"xperiod": "M2",
17+
"name": "bar (M2)",
18+
"type": "bar",
19+
"y": [1, 2, 3, 4],
20+
"x": ["2001-01-01", "2001-03-01", "2001-05-01", "2001-07-01"],
21+
"xperiodalignment": "middle"
22+
},
23+
24+
{
25+
"xaxis": "x3",
26+
"yaxis": "y3",
27+
"xperiod": "M3",
28+
"name": "bar (M3)",
29+
"type": "bar",
30+
"y": [1, 2, 3, 4],
31+
"x": ["2001-01-01", "2001-04-01", "2001-07-01", "2001-10-01"],
32+
"xperiodalignment": "middle"
33+
},
34+
{
35+
"xaxis": "x4",
36+
"yaxis": "y4",
37+
"xperiod": "M6",
38+
"name": "bar (M6)",
39+
"type": "bar",
40+
"y": [1, 2, 3, 4],
41+
"x": ["2001-01-01", "2001-07-01", "2002-01-01", "2002-07-01"],
42+
"xperiodalignment": "middle"
43+
},
44+
{
45+
"xaxis": "x5",
46+
"yaxis": "y5",
47+
"xperiod": "M12",
48+
"name": "bar (M12)",
49+
"type": "bar",
50+
"y": [1, 2, 3, 4],
51+
"x": ["2001-01-01", "2002-01-01", "2003-01-01", "2004-01-01"],
52+
"xperiodalignment": "middle"
53+
},
54+
{
55+
"xaxis": "x6",
56+
"yaxis": "y6",
57+
"xperiod": 31557600000,
58+
"name": "bar (AVG-YEAR)",
59+
"type": "bar",
60+
"y": [1, 2, 3, 4],
61+
"x": ["2001-01-01", "2002-01-01", "2003-01-01", "2004-01-01"],
62+
"xperiodalignment": "middle"
63+
}
64+
],
65+
"layout": {
66+
"width": 1000,
67+
"height": 500,
68+
"showlegend": true,
69+
"hovermode": "closest",
70+
71+
"xaxis": {
72+
"ticklabelmode": "period",
73+
"tickcolor": "black",
74+
"domain": [
75+
0,
76+
0.3
77+
]
78+
},
79+
"xaxis2": {
80+
"dtick": "M2",
81+
"tickcolor": "black",
82+
"anchor": "y2",
83+
"domain": [
84+
0,
85+
0.3
86+
]
87+
},
88+
"xaxis3": {
89+
"dtick": "M3",
90+
"ticklabelmode": "period",
91+
"tickcolor": "black",
92+
"anchor": "y3",
93+
"domain": [
94+
0.35,
95+
0.65
96+
]
97+
},
98+
"xaxis4": {
99+
"dtick": "M6",
100+
"ticklabelmode": "period",
101+
"tickcolor": "black",
102+
"anchor": "y4",
103+
"domain": [
104+
0.35,
105+
0.65
106+
]
107+
},
108+
"xaxis5": {
109+
"ticklabelmode": "period",
110+
"tickcolor": "black",
111+
"anchor": "y5",
112+
"domain": [
113+
0.7,
114+
1
115+
]
116+
},
117+
"xaxis6": {
118+
"ticklabelmode": "period",
119+
"tickcolor": "black",
120+
"anchor": "y6",
121+
"domain": [
122+
0.7,
123+
1
124+
]
125+
},
126+
127+
"yaxis": {
128+
"tickcolor": "black",
129+
"domain": [
130+
0,
131+
0.45
132+
]
133+
},
134+
"yaxis2": {
135+
"tickcolor": "black",
136+
"anchor": "x2",
137+
"domain": [
138+
0.55,
139+
1
140+
]
141+
},
142+
"yaxis3": {
143+
"tickcolor": "black",
144+
"anchor": "x3",
145+
"domain": [
146+
0,
147+
0.45
148+
]
149+
},
150+
"yaxis4": {
151+
"tickcolor": "black",
152+
"anchor": "x4",
153+
"domain": [
154+
0.55,
155+
1
156+
]
157+
},
158+
"yaxis5": {
159+
"tickcolor": "black",
160+
"anchor": "x5",
161+
"domain": [
162+
0,
163+
0.45
164+
]
165+
},
166+
"yaxis6": {
167+
"tickcolor": "black",
168+
"anchor": "x6",
169+
"domain": [
170+
0.55,
171+
1
172+
]
173+
}
174+
}
175+
}

0 commit comments

Comments
 (0)