Skip to content

Commit

Permalink
revise period lengths on axes with rangebreaks
Browse files Browse the repository at this point in the history
  • Loading branch information
archmoj committed Aug 19, 2020
1 parent 6fb0133 commit f907f2a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
37 changes: 22 additions & 15 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -781,42 +781,49 @@ axes.calcTicks = function calcTicks(ax, opts) {

var A = tickVals[a].value;
var B = tickVals[b].value;

var actualDelta = Math.abs(B - A);
var delta = definedDelta || actualDelta;
var periodLength = 0;

if(delta >= ONEMINYEAR) {
if(actualDelta >= ONEMINYEAR && actualDelta <= ONEMAXYEAR) {
v += actualDelta / 2;
periodLength = actualDelta;
} else {
v += ONEAVGYEAR / 2;
periodLength = ONEAVGYEAR;
}
} else if(delta >= ONEMINQUARTER) {
if(
definedDelta && // case of specified by tickfomat
actualDelta >= ONEMINQUARTER && actualDelta <= ONEMAXQUARTER
) {
v += actualDelta / 2;
periodLength = actualDelta;
} else {
v += ONEAVGQUARTER / 2;
periodLength = ONEAVGQUARTER;
}
} else if(delta >= ONEMINMONTH) {
if(actualDelta >= ONEMINMONTH && actualDelta <= ONEMAXMONTH) {
v += actualDelta / 2;
periodLength = actualDelta;
} else {
v += ONEAVGMONTH / 2;
periodLength = ONEAVGMONTH;
}
} else if(delta >= ONEWEEK) {
v += ONEWEEK / 2;
if(
definedDelta && // case of specified by tickfomat
actualDelta === ONEWEEK && ax._hasDayOfWeekBreaks
) {
v -= ONEDAY; // half of two days which is a good approximation for the number of week-end days
}
periodLength = ONEWEEK;
} else if(delta >= ONEDAY) {
v += ONEDAY / 2;
periodLength = ONEDAY;
}

if(ax.rangebreaks) {
var nOut = 0;
var nAll = 2 * 3 * 5 * 7; // number of samples
for(var c = 0; c < nAll; c++) {
var r = c / nAll;
if(ax.maskBreaks(A * (1 - r) + B * r) === BADNUM) nOut++;
}
periodLength *= 1 - nOut / nAll;
}

v += periodLength / 2;

ticksOut[i].periodX = v;

if(v > maxRange || v < minRange) { // hide label if outside the range
Expand Down
8 changes: 5 additions & 3 deletions test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5432,9 +5432,11 @@ describe('Test axes', function() {
})
.then(function() {
_assert('', [
'2020-01-01 12:00', '2020-01-08 12:00', '2020-01-15 12:00', '2020-01-22 12:00', '2020-01-29 12:00'
], [
['Dec-52', 'Jan-01', 'Jan-02', 'Jan-03', 'Jan-04'],
['2019-12-31 04:24', '2020-01-08 12:00', '2020-01-15 12:00', '2020-01-22 12:00', '2020-01-29 12:00'],
['2020-01-01 12:00', '2020-01-08 12:00', '2020-01-15 12:00', '2020-01-22 12:00', '2020-01-29 12:00'],
['2020-01-01 12:00', '2020-01-08 12:00', '2020-01-15 12:00', '2020-01-22 12:00', '2020-01-29 12:00']
][i], [
['', 'Jan-01', 'Jan-02', 'Jan-03', 'Jan-04'],
['Dec-01', 'Jan-02', 'Jan-03', 'Jan-04', 'Jan-05'],
['Dec-52', 'Jan-01', 'Jan-02', 'Jan-03', 'Jan-04']
][i]);
Expand Down

0 comments on commit f907f2a

Please sign in to comment.