Skip to content

Commit

Permalink
apply AlexJ's suggestion to use Lib.incrementMonth and simplify case …
Browse files Browse the repository at this point in the history
…of milliseconds
  • Loading branch information
archmoj committed Sep 25, 2020
1 parent 5ae9e1e commit 83b22a0
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 76 deletions.
72 changes: 35 additions & 37 deletions src/plots/cartesian/align_period.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

var isNumeric = require('fast-isnumeric');
var Lib = require('../../lib');
var ms2DateTime = Lib.ms2DateTime;
var dateTime2ms = Lib.dateTime2ms;
var incrementMonth = Lib.incrementMonth;
var ONEDAY = require('../../constants/numerical').ONEDAY;
var constants = require('../../constants/numerical');
var ONEAVGMONTH = constants.ONEAVGMONTH;

module.exports = function alignPeriod(trace, ax, axLetter, vals) {
if(ax.type !== 'date') return vals;
Expand All @@ -22,12 +22,10 @@ module.exports = function alignPeriod(trace, ax, axLetter, vals) {
if(!alignment) return vals;

var period = trace[axLetter + 'period'];
var mPeriod, dPeriod;
var mPeriod;
if(isNumeric(period)) {
dPeriod = +period;
dPeriod /= ONEDAY; // convert milliseconds to days
dPeriod = Math.round(dPeriod);
if(dPeriod <= 0) return vals;
period = +period;
if(period <= 0) return vals;
} else if(typeof period === 'string' && period.charAt(0) === 'M') {
var n = +(period.substring(1));
if(n > 0 && Math.round(n) === n) {
Expand All @@ -47,46 +45,46 @@ module.exports = function alignPeriod(trace, ax, axLetter, vals) {
var newVals = [];
var len = vals.length;
for(var i = 0; i < len; i++) {
var v = vals[i] - base;

var dateStr = ms2DateTime(v, 0, calendar);
var d = new Date(dateStr);
var year = d.getUTCFullYear();
var month = d.getUTCMonth();
var day = d.getUTCDate();

var startTime, endTime;
if(dPeriod) {
startTime = Date.UTC(year, month, day);
endTime = startTime + dPeriod * ONEDAY;
}
var v = vals[i];

var nEstimated, startTime, endTime;
if(mPeriod) {
var nYears = Math.floor(mPeriod / 12);
var nMonths = mPeriod % 12;
// guess at how many periods away from base we are
nEstimated = Math.round((v - base) / (mPeriod * ONEAVGMONTH));
endTime = incrementMonth(base, mPeriod * nEstimated, calendar);

// iterate to get the exact bounds before and after v
// there may be ways to make this faster, but most of the time
// we'll only execute each loop zero or one time.
while(endTime > v) {
endTime = incrementMonth(endTime, -mPeriod, calendar);
}
while(endTime <= v) {
endTime = incrementMonth(endTime, mPeriod, calendar);
}

// now we know endTime is the boundary immediately after v
// so startTime is obtained by incrementing backward one period.
startTime = incrementMonth(endTime, -mPeriod, calendar);
} else { // case of ms
nEstimated = Math.round((v - base) / period);
endTime = base + nEstimated * period;

if(nMonths) {
startTime = Date.UTC(year, nYears ? month : roundMonth(month, nMonths));
endTime = incrementMonth(startTime, mPeriod, calendar);
} else {
startTime = Date.UTC(year, 0);
endTime = Date.UTC(year + nYears, 0);
while(endTime > v) {
endTime -= period;
}
while(endTime <= v) {
endTime += period;
}

startTime = endTime - period;
}

var newD = new Date(
newVals[i] = (
isStart ? startTime :
isEnd ? endTime :
(startTime + endTime) / 2
);

newVals[i] = newD.getTime() + base;
}
return newVals;
};

var monthSteps = [2, 3, 4, 6];

function roundMonth(month, step) {
return (monthSteps.indexOf(step) === -1) ? month : Math.floor(month / step) * step;
}
3 changes: 1 addition & 2 deletions src/traces/scatter/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ function axisPeriod(axis) {
'Only relevant when the axis `type` is *date*.',
'Sets the period positioning in milliseconds or *M<n>* on the ' + axis + ' axis.',
'Special values in the form of *M<n>* could be used to declare',
'the number of "average" months. In this case `n` must be a positive integer.',
'When using milliseconds the period is rounded and applied as the number of days.'
'the number of "average" months. In this case `n` must be a positive integer.'
].join(' ')
};
}
Expand Down
Binary file modified test/image/baselines/period_positioning3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/period_positioning4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/period_positioning5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/period_positioning6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 15 additions & 9 deletions test/image/mocks/period_positioning3.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,31 +158,34 @@
{
"xaxis": "x6",
"yaxis": "y6",
"xperiod": 31557600000,
"name": "start (AVG-YEAR)",
"xperiod": 604800000,
"name": "start (W1)",
"type": "scatter",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01-01", "2002-01-01", "2003-01-01", "2004-01-01", "2005-01-01", "2006-01-01"],
"x": ["2001-01-01", "2001-01-08", "2001-01-15", "2001-01-22", "2001-01-29", "2001-02-05"],
"xperiod0": "2001-01-01",
"xperiodalignment": "start"
},
{
"xaxis": "x6",
"yaxis": "y6",
"xperiod": 31557600000,
"name": "middle (AVG-YEAR)",
"xperiod": 604800000,
"name": "bar (W1)",
"type": "bar",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01-01", "2002-01-01", "2003-01-01", "2004-01-01", "2005-01-01", "2006-01-01"],
"x": ["2001-01-01", "2001-01-08", "2001-01-15", "2001-01-22", "2001-01-29", "2001-02-05"],
"xperiod0": "2001-01-01",
"xperiodalignment": "middle"
},
{
"xaxis": "x6",
"yaxis": "y6",
"xperiod": 31557600000,
"name": "end (AVG-YEAR)",
"xperiod": 604800000,
"name": "start (W1)",
"type": "scatter",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01-01", "2002-01-01", "2003-01-01", "2004-01-01", "2005-01-01", "2006-01-01"],
"x": ["2001-01-01", "2001-01-08", "2001-01-15", "2001-01-22", "2001-01-29", "2001-02-05"],
"xperiod0": "2001-01-01",
"xperiodalignment": "end"
}
],
Expand Down Expand Up @@ -248,7 +251,10 @@
]
},
"xaxis6": {
"dtick": 604800000,
"tick0": "2001-01-01",
"ticklabelmode": "period",
"tickformat": "W%V",
"tickcolor": "black",
"anchor": "y6",
"domain": [
Expand Down
32 changes: 23 additions & 9 deletions test/image/mocks/period_positioning4.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"marker": { "opacity": 0.5 }, "type": "bar",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01-01", "2001-01-15", "2001-02-01", "2001-02-15", "2001-03-01", "2001-03-15"],
"textposition": "inside", "textangle": 0, "texttemplate": "%{x|%b-%e}",
"xperiodalignment": "middle"
},
{
Expand Down Expand Up @@ -49,6 +50,7 @@
"marker": { "opacity": 0.5 }, "type": "bar",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01-01", "2001-02-01", "2001-03-01", "2001-04-01", "2001-05-01", "2001-06-01"],
"textposition": "inside", "textangle": 0, "texttemplate": "%{x|%b-%e}",
"xperiodalignment": "middle"
},
{
Expand Down Expand Up @@ -80,6 +82,7 @@
"marker": { "opacity": 0.5 }, "type": "bar",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01-01", "2001-02-15", "2001-04-01", "2001-05-15", "2001-07-01", "2001-08-15"],
"textposition": "inside", "textangle": 0, "texttemplate": "%{x|%b-%e}",
"xperiodalignment": "middle"
},
{
Expand Down Expand Up @@ -111,6 +114,7 @@
"marker": { "opacity": 0.5 }, "type": "bar",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01-01", "2001-04-01", "2001-07-01", "2001-10-01", "2002-01-01", "2002-04-01"],
"textposition": "inside", "textangle": 0, "texttemplate": "%{x|%b-%e}",
"xperiodalignment": "middle"
},
{
Expand Down Expand Up @@ -142,6 +146,7 @@
"marker": { "opacity": 0.5 }, "type": "bar",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01-01", "2001-07-01", "2002-01-01", "2002-07-01", "2003-01-01", "2003-07-01"],
"textposition": "inside", "textangle": 0, "texttemplate": "%{x|%b-%e}",
"xperiodalignment": "middle"
},
{
Expand All @@ -158,31 +163,37 @@
{
"xaxis": "x6",
"yaxis": "y6",
"xperiod": 31557600000,
"name": "start (AVG-YEAR)",
"xperiod": 604800000,
"name": "start (W1)",
"marker": { "opacity": 0.5 }, "type": "scatter",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01-01", "2001-07-01", "2002-01-01", "2002-07-01", "2003-01-01", "2003-07-01"],
"x": ["2001-01-01", "2001-01-05", "2001-01-08", "2001-01-12", "2001-01-15", "2001-01-19"],
"xperiod0": "2001-01-01",
"xperiodalignment": "start"
},
{
"xaxis": "x6",
"yaxis": "y6",
"xperiod": 31557600000,
"name": "middle (AVG-YEAR)",
"xperiod": 604800000,
"name": "middle (W1)",
"marker": { "opacity": 0.5 }, "type": "bar",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01-01", "2001-07-01", "2002-01-01", "2002-07-01", "2003-01-01", "2003-07-01"],
"x": ["2001-01-01", "2001-01-05", "2001-01-08", "2001-01-12", "2001-01-15", "2001-01-19"],
"textposition": "inside", "textangle": 0,
"texttemplate": "%{x|%A}",
"hovertemplate": "%{x|%A}, %{y}",
"xperiod0": "2001-01-01",
"xperiodalignment": "middle"
},
{
"xaxis": "x6",
"yaxis": "y6",
"xperiod": 31557600000,
"name": "end (AVG-YEAR)",
"xperiod": 604800000,
"name": "end (W1)",
"marker": { "opacity": 0.5 }, "type": "scatter",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01-01", "2001-07-01", "2002-01-01", "2002-07-01", "2003-01-01", "2003-07-01"],
"x": ["2001-01-01", "2001-01-05", "2001-01-08", "2001-01-12", "2001-01-15", "2001-01-19"],
"xperiod0": "2001-01-01",
"xperiodalignment": "end"
}
],
Expand Down Expand Up @@ -249,7 +260,10 @@
]
},
"xaxis6": {
"dtick": 604800000,
"tick0": "2001-01-01",
"ticklabelmode": "period",
"tickformat": "W%V",
"tickcolor": "black",
"anchor": "y6",
"domain": [
Expand Down
12 changes: 6 additions & 6 deletions test/image/mocks/period_positioning5.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"name": "start (M9)",
"type": "scatter",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01", "2001-10", "2002-07", "2003-04", "2004-01", "2004-09"],
"x": ["2001-01", "2001-10", "2002-07", "2003-04", "2004-01", "2004-10"],
"xperiod0": "2001-01-01",
"xperiodalignment": "start"
},
Expand All @@ -18,7 +18,7 @@
"name": "middle (M9)",
"type": "bar",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01", "2001-10", "2002-07", "2003-04", "2004-01", "2004-09"],
"x": ["2001-01", "2001-10", "2002-07", "2003-04", "2004-01", "2004-10"],
"xperiod0": "2001-01-01",
"xperiodalignment": "middle"
},
Expand All @@ -29,7 +29,7 @@
"name": "end (M9)",
"type": "scatter",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01", "2001-10", "2002-07", "2003-04", "2004-01", "2004-09"],
"x": ["2001-01", "2001-10", "2002-07", "2003-04", "2004-01", "2004-10"],
"xperiod0": "2001-01-01",
"xperiodalignment": "end"
},
Expand Down Expand Up @@ -75,7 +75,7 @@
"name": "start (Y1.5)",
"type": "scatter",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01", "2002-06", "2004-01", "2005-06", "2007-01", "2008-06"],
"x": ["2001-01", "2002-07", "2004-01", "2005-07", "2007-01", "2008-07"],
"xperiod0": "2001-01-01",
"xperiodalignment": "start"
},
Expand All @@ -86,7 +86,7 @@
"name": "middle (Y1.5)",
"type": "bar",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01", "2002-06", "2004-01", "2005-06", "2007-01", "2008-06"],
"x": ["2001-01", "2002-07", "2004-01", "2005-07", "2007-01", "2008-07"],
"xperiod0": "2001-01-01",
"xperiodalignment": "middle"
},
Expand All @@ -97,7 +97,7 @@
"name": "end (Y1.5)",
"type": "scatter",
"y": [1, 2, 3, 4, 5, 6],
"x": ["2001-01", "2002-06", "2004-01", "2005-06", "2007-01", "2008-06"],
"x": ["2001-01", "2002-07", "2004-01", "2005-07", "2007-01", "2008-07"],
"xperiod0": "2001-01-01",
"xperiodalignment": "end"
},
Expand Down
Loading

0 comments on commit 83b22a0

Please sign in to comment.