Skip to content

Commit a4e6bba

Browse files
committed
handle monthly periods
1 parent dd05f3c commit a4e6bba

File tree

2 files changed

+58
-58
lines changed

2 files changed

+58
-58
lines changed

src/plots/cartesian/align_period.js

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,31 @@
88

99
'use strict';
1010

11+
var isNumeric = require('fast-isnumeric');
12+
var ONEAVGMONTH = require('../../constants/numerical').ONEAVGMONTH;
13+
14+
var M = {};
15+
for(var n = 1; n <= 12; n++) {
16+
M['M' + n] = n * ONEAVGMONTH;
17+
}
18+
1119
module.exports = function alignPeriod(trace, axLetter, vals) {
12-
var periodAlignment = trace[axLetter + 'periodalignment'];
13-
if(!periodAlignment || periodAlignment === 'start') return vals;
20+
var alignment = trace[axLetter + 'periodalignment'];
21+
if(!alignment || alignment === 'start') return vals;
22+
23+
var period = trace[axLetter + 'period'];
24+
if(isNumeric(period)) {
25+
period = +period;
26+
} else if(typeof period === 'string') {
27+
period = M[period];
28+
}
1429

15-
var delta = (periodAlignment === 'end' ? 1 : 0.5) * trace[axLetter + 'period'];
16-
for(var i = 0; i < vals.length; i++) {
17-
vals[i] += delta;
30+
if(period > 0) {
31+
var delta = (alignment === 'end' ? 1 : 0.5) * period;
32+
var len = vals.length;
33+
for(var i = 0; i < len; i++) {
34+
vals[i] += delta;
35+
}
1836
}
1937
return vals;
2038
};

src/traces/scatter/attributes.js

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,37 @@ var ONEAVGYEAR = require('../../constants/numerical').ONEAVGYEAR;
2020

2121
var extendFlat = require('../../lib/extend').extendFlat;
2222

23+
function axisPeriod(axis) {
24+
return {
25+
valType: 'any',
26+
dflt: ONEAVGYEAR,
27+
role: 'info',
28+
editType: 'calc',
29+
description: [
30+
'Only relevant when the axis `type` is *date*.',
31+
'Sets the period positioning in milliseconds or *M<n>* on the ' + axis + ' axis.',
32+
'Special values in the form of *M<n>* could be used to declare',
33+
'the number of "average" months. In this case `n` must be a positive integer.'
34+
].join(' ')
35+
};
36+
}
37+
38+
function axisPeriodAlignment(axis) {
39+
return {
40+
valType: 'enumerated',
41+
values: [
42+
'start', 'middle', 'end'
43+
],
44+
dflt: 'start',
45+
role: 'style',
46+
editType: 'calc',
47+
description: [
48+
'Only relevant when the axis `type` is *date*.',
49+
'Sets the alignment of data points on the ' + axis + ' axis.'
50+
].join(' ')
51+
};
52+
}
53+
2354
module.exports = {
2455
x: {
2556
valType: 'data_array',
@@ -82,59 +113,10 @@ module.exports = {
82113
].join(' ')
83114
},
84115

85-
xperiod: {
86-
valType: 'number',
87-
dflt: ONEAVGYEAR,
88-
min: 0,
89-
role: 'info',
90-
editType: 'calc',
91-
anim: true,
92-
description: [
93-
'Only relevant when the axis `type` is *date*.',
94-
'Sets the period positioning in milliseconds',
95-
'on the x axis.'
96-
].join(' ')
97-
},
98-
xperiodalignment: {
99-
valType: 'enumerated',
100-
values: [
101-
'start', 'middle', 'end'
102-
],
103-
dflt: 'start',
104-
role: 'style',
105-
editType: 'calc',
106-
description: [
107-
'Only relevant when the axis `type` is *date*.',
108-
'Sets the alignment of data points',
109-
'on the x axis.'
110-
].join(' ')
111-
},
112-
yperiod: {
113-
valType: 'number',
114-
dflt: ONEAVGYEAR,
115-
min: 0,
116-
role: 'info',
117-
editType: 'calc',
118-
description: [
119-
'Only relevant when the axis `type` is *date*.',
120-
'Sets the period positioning in milliseconds',
121-
'on the y axis.'
122-
].join(' ')
123-
},
124-
yperiodalignment: {
125-
valType: 'enumerated',
126-
values: [
127-
'start', 'middle', 'end'
128-
],
129-
dflt: 'start',
130-
role: 'style',
131-
editType: 'calc',
132-
description: [
133-
'Only relevant when the axis `type` is *date*.',
134-
'Sets the alignment of data points',
135-
'on the y axis.'
136-
].join(' ')
137-
},
116+
xperiod: axisPeriod('x'),
117+
yperiod: axisPeriod('y'),
118+
xperiodalignment: axisPeriodAlignment('x'),
119+
yperiodalignment: axisPeriodAlignment('y'),
138120

139121
stackgroup: {
140122
valType: 'string',

0 commit comments

Comments
 (0)