Skip to content

Commit d8227f1

Browse files
committed
simplify and adjust to show first tick on axes with rangebreaks
1 parent ab6b57a commit d8227f1

File tree

4 files changed

+58
-52
lines changed

4 files changed

+58
-52
lines changed

src/plots/cartesian/axes.js

+31-25
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ axes.prepTicks = function(ax, opts) {
522522
var rng = Lib.simpleMap(ax.range, ax.r2l, undefined, undefined, opts);
523523

524524
ax._dtickInit = ax.dtick;
525+
ax._tick0Init = ax.tick0;
525526

526527
// calculate max number of (auto) ticks to display based on plot size
527528
if(ax.tickmode === 'auto' || !ax.dtick) {
@@ -559,10 +560,7 @@ axes.prepTicks = function(ax, opts) {
559560

560561
// check for missing tick0
561562
if(!ax.tick0) {
562-
ax.tick0 = 0;
563-
if(ax.type === 'date') {
564-
ax.tick0 = '2000-01-01';
565-
}
563+
ax.tick0 = (ax.type === 'date') ? '2000-01-01' : 0;
566564
}
567565

568566
// ensure we don't try to make ticks below our minimum precision
@@ -595,9 +593,7 @@ axes.calcTicks = function calcTicks(ax, opts) {
595593
var maxRange = Math.max(rng[0], rng[1]);
596594

597595
// find the first tick
598-
var firstTick = axes.tickFirst(ax, opts);
599-
if(ax.rangebreaks) firstTick = moveOutsideBreak(firstTick, ax, axrev);
600-
ax._tmin = firstTick;
596+
ax._tmin = axes.tickFirst(ax, opts);
601597

602598
// No visible ticks? Quit.
603599
// I've only seen this on category axes with all categories off the edge.
@@ -615,7 +611,7 @@ axes.calcTicks = function calcTicks(ax, opts) {
615611
var isPeriod = ax.ticklabelmode === 'period';
616612
var definedDelta;
617613
if(isPeriod && tickformat) {
618-
var noDtick = !ax._dtickInit;
614+
var noDtick = ax._dtickInit !== ax.dtick;
619615
if(
620616
!(/%[fLQsSMX]/.test(tickformat))
621617
// %f: microseconds as a decimal number [000000, 999999]
@@ -684,16 +680,34 @@ axes.calcTicks = function calcTicks(ax, opts) {
684680
}
685681
}
686682

683+
var maxTicks = Math.max(1000, ax._length || 0);
687684
var tickVals = [];
688-
689685
var xPrevious = null;
690-
var maxTicks = Math.max(1000, ax._length || 0);
691-
tickVals = [];
692-
for(var x = ax._tmin;
693-
(axrev) ? (x >= endTick) : (x <= endTick);
694-
x = axes.tickIncrement(x, ax.dtick, axrev, ax.calendar)
686+
var x = ax._tmin;
687+
688+
if(ax.rangebreaks && ax._tick0Init !== ax.tick0) {
689+
// adjust tick0
690+
x = moveOutsideBreak(x, ax);
691+
if(!axrev) {
692+
x = axes.tickIncrement(x, ax.dtick, !axrev, ax.calendar);
693+
}
694+
}
695+
696+
if(isPeriod) {
697+
// add one item to label period before tick0
698+
x = axes.tickIncrement(x, ax.dtick, !axrev, ax.calendar);
699+
}
700+
701+
for(;
702+
(axrev) ? (x >= endTick) : (x <= endTick);
703+
x = axes.tickIncrement(x, ax.dtick, axrev, ax.calendar)
695704
) {
696-
if(ax.rangebreaks && moveOutsideBreak(x, ax) === maxRange) continue;
705+
if(ax.rangebreaks) {
706+
if(!axrev) {
707+
if(x < startTick) continue;
708+
if(ax.maskBreaks(x) === BADNUM && moveOutsideBreak(x, ax) >= maxRange) break;
709+
}
710+
}
697711

698712
// prevent infinite loops - no more than one tick per pixel,
699713
// and make sure each value is different from the previous
@@ -713,14 +727,6 @@ axes.calcTicks = function calcTicks(ax, opts) {
713727

714728
var i;
715729
if(isPeriod) {
716-
if(tickVals[0]) {
717-
// add one label to show pre tick0 period
718-
tickVals.unshift({
719-
minor: false,
720-
value: axes.tickIncrement(tickVals[0].value, ax.dtick, !axrev, ax.calendar)
721-
});
722-
}
723-
724730
for(i = 0; i < tickVals.length; i++) {
725731
var v = tickVals[i].value;
726732

@@ -3389,12 +3395,12 @@ function isAngular(ax) {
33893395
return ax._id === 'angularaxis';
33903396
}
33913397

3392-
function moveOutsideBreak(v, ax, isStart) {
3398+
function moveOutsideBreak(v, ax) {
33933399
var len = ax._rangebreaks.length;
33943400
for(var k = 0; k < len; k++) {
33953401
var brk = ax._rangebreaks[k];
33963402
if(v >= brk.min && v < brk.max) {
3397-
return isStart ? brk.min : brk.max;
3403+
return brk.max;
33983404
}
33993405
}
34003406
return v;
Loading
Loading

test/jasmine/tests/axes_test.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -5118,7 +5118,7 @@ describe('Test axes', function() {
51185118
}],
51195119
layout: {
51205120
width: 1600,
5121-
height: 1600
5121+
height: 400
51225122
}
51235123
};
51245124

@@ -5148,7 +5148,7 @@ describe('Test axes', function() {
51485148
'1970-01-09 08:00', '1970-01-09 12:00', '1970-01-09 16:00',
51495149
'1970-01-12 08:00', '1970-01-12 12:00', '1970-01-12 16:00',
51505150
'1970-01-13 08:00', '1970-01-13 12:00', '1970-01-13 16:00',
5151-
'1970-01-14 08:00', '1970-01-14 12:00'
5151+
'1970-01-14 08:00', '1970-01-14 12:00', '1970-01-14 16:00'
51525152
]
51535153
}, autorange);
51545154
})
@@ -5210,7 +5210,7 @@ describe('Test axes', function() {
52105210
'1970-01-09 08:00', '1970-01-09 10:00', '1970-01-09 12:00', '1970-01-09 14:00', '1970-01-09 16:00',
52115211
'1970-01-12 08:00', '1970-01-12 10:00', '1970-01-12 12:00', '1970-01-12 14:00', '1970-01-12 16:00',
52125212
'1970-01-13 08:00', '1970-01-13 10:00', '1970-01-13 12:00', '1970-01-13 14:00', '1970-01-13 16:00',
5213-
'1970-01-14 08:00', '1970-01-14 10:00', '1970-01-14 12:00', '1970-01-14 14:00'
5213+
'1970-01-14 08:00', '1970-01-14 10:00', '1970-01-14 12:00', '1970-01-14 14:00', '1970-01-14 16:00'
52145214
]
52155215
}, autorange);
52165216
})
@@ -5528,13 +5528,13 @@ describe('Test axes', function() {
55285528
})
55295529
.then(function() {
55305530
_assert('', [
5531-
['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'],
5532-
['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'],
5533-
['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']
5531+
['2020-01-08 12:00', '2020-01-15 12:00', '2020-01-22 12:00', '2020-01-29 12:00'],
5532+
['2020-01-08 12:00', '2020-01-15 12:00', '2020-01-22 12:00', '2020-01-29 12:00'],
5533+
['2020-01-08 12:00', '2020-01-15 12:00', '2020-01-22 12:00', '2020-01-29 12:00']
55345534
][i], [
5535-
['Dec-W52', 'Jan-W01', 'Jan-W02', 'Jan-W03', 'Jan-W04'],
5536-
['Dec-W01', 'Jan-W02', 'Jan-W03', 'Jan-W04', 'Jan-W05'],
5537-
['Dec-W52', 'Jan-W01', 'Jan-W02', 'Jan-W03', 'Jan-W04']
5535+
['Jan-W01', 'Jan-W02', 'Jan-W03', 'Jan-W04'],
5536+
['Jan-W02', 'Jan-W03', 'Jan-W04', 'Jan-W05'],
5537+
['Jan-W01', 'Jan-W02', 'Jan-W03', 'Jan-W04']
55385538
][i]);
55395539
})
55405540
.catch(failTest)
@@ -5926,48 +5926,48 @@ describe('Test axes', function() {
59265926
[
59275927
{
59285928
range: ['2020-12-14 08:00', '2022-12-14 08:00'],
5929-
positions: ['2020-10-06 07:45', '2021-01-06 07:45', '2021-04-06 07:45', '2021-07-06 07:45', '2021-10-06 07:45', '2022-01-06 07:45', '2022-04-06 07:45', '2022-07-06 07:45', '2022-10-06 07:45'],
5930-
labels: [' ', 'Jan 2021', 'Apr 2021', 'Jul 2021', 'Oct 2021', 'Jan 2022', 'Apr 2022', 'Jul 2022', 'Oct 2022']
5929+
positions: ['2021-01-06 07:45', '2021-04-06 07:45', '2021-07-06 07:45', '2021-10-06 07:45', '2022-01-06 07:45', '2022-04-06 07:45', '2022-07-06 07:45', '2022-10-06 07:45'],
5930+
labels: ['Jan 2021', 'Apr 2021', 'Jul 2021', 'Oct 2021', 'Jan 2022', 'Apr 2022', 'Jul 2022', 'Oct 2022']
59315931
},
59325932
{
59335933
range: ['2020-12-14 08:00', '2021-08-14 08:00'],
5934-
positions: ['2020-12-16 18:00', '2021-01-16 18:00', '2021-02-15 06:00', '2021-03-16 18:00', '2021-04-16 06:00', '2021-05-16 18:00', '2021-06-16 06:00', '2021-07-16 18:00', '2021-08-16 18:00'],
5935-
labels: ['Dec 2020', 'Jan 2021', 'Feb 2021', 'Mar 2021', 'Apr 2021', 'May 2021', 'Jun 2021', 'Jul 2021', ' ']
5934+
positions: ['2021-01-16 18:00', '2021-02-15 06:00', '2021-03-16 18:00', '2021-04-16 06:00', '2021-05-16 18:00', '2021-06-16 06:00', '2021-07-16 18:00', '2021-08-16 18:00'],
5935+
labels: ['Jan 2021', 'Feb 2021', 'Mar 2021', 'Apr 2021', 'May 2021', 'Jun 2021', 'Jul 2021', ' ']
59365936
},
59375937
{
59385938
range: ['2020-12-14 08:00', '2021-04-14 08:00'],
5939-
positions: ['2020-12-07 12:00', '2020-12-21 12:00', '2021-01-04 12:00', '2021-01-18 12:00', '2021-02-01 12:00', '2021-02-15 12:00', '2021-03-01 12:00', '2021-03-15 12:00', '2021-03-29 12:00', '2021-04-12 12:00'],
5940-
labels: [' ', 'Dec 21<br>2020', 'Jan 4<br>2021', 'Jan 18', 'Feb 1', 'Feb 15', 'Mar 1', 'Mar 15', 'Mar 29', 'Apr 12']
5939+
positions: ['2020-12-21 12:00', '2021-01-04 12:00', '2021-01-18 12:00', '2021-02-01 12:00', '2021-02-15 12:00', '2021-03-01 12:00', '2021-03-15 12:00', '2021-03-29 12:00', '2021-04-12 12:00'],
5940+
labels: ['Dec 21<br>2020', 'Jan 4<br>2021', 'Jan 18', 'Feb 1', 'Feb 15', 'Mar 1', 'Mar 15', 'Mar 29', 'Apr 12']
59415941
},
59425942
{
59435943
range: ['2020-12-14 08:00', '2021-02-14 08:00'],
5944-
positions: ['2020-12-14 12:00', '2020-12-21 12:00', '2020-12-28 12:00', '2021-01-04 12:00', '2021-01-11 12:00', '2021-01-18 12:00', '2021-01-25 12:00', '2021-02-01 12:00', '2021-02-08 12:00'],
5945-
labels: ['Dec 14<br>2020', 'Dec 21', 'Dec 28', 'Jan 4<br>2021', 'Jan 11', 'Jan 18', 'Jan 25', 'Feb 1', 'Feb 8']
5944+
positions: ['2020-12-21 12:00', '2020-12-28 12:00', '2021-01-04 12:00', '2021-01-11 12:00', '2021-01-18 12:00', '2021-01-25 12:00', '2021-02-01 12:00', '2021-02-08 12:00'],
5945+
labels: ['Dec 21<br>2020', 'Dec 28', 'Jan 4<br>2021', 'Jan 11', 'Jan 18', 'Jan 25', 'Feb 1', 'Feb 8']
59465946
},
59475947
{
59485948
range: ['2020-12-14 08:00', '2021-01-14 08:00'],
5949-
positions: ['2020-12-14 12:00', '2020-12-21 12:00', '2020-12-28 12:00', '2021-01-04 12:00', '2021-01-11 12:00'],
5950-
labels: ['Dec 14<br>2020', 'Dec 21', 'Dec 28', 'Jan 4<br>2021', 'Jan 11']
5949+
positions: ['2020-12-21 12:00', '2020-12-28 12:00', '2021-01-04 12:00', '2021-01-11 12:00'],
5950+
labels: ['Dec 21<br>2020', 'Dec 28', 'Jan 4<br>2021', 'Jan 11']
59515951
},
59525952
{
59535953
range: ['2020-12-14 08:00', '2021-01-01 08:00'],
5954-
positions: ['2020-12-14 12:00', '2020-12-16 12:00', '2020-12-18 12:00', '2020-12-22 12:00', '2020-12-24 12:00', '2020-12-28 12:00', '2020-12-30 12:00', '2021-01-01 12:00'],
5955-
labels: ['Dec 14<br>2020', 'Dec 16', 'Dec 18', 'Dec 22', 'Dec 24', 'Dec 28', 'Dec 30', ' ']
5954+
positions: ['2020-12-16 12:00', '2020-12-18 12:00', '2020-12-22 12:00', '2020-12-24 12:00', '2020-12-28 12:00', '2020-12-30 12:00', '2021-01-01 12:00'],
5955+
labels: ['Dec 16<br>2020', 'Dec 18', 'Dec 22', 'Dec 24', 'Dec 28', 'Dec 30', ' ']
59565956
},
59575957
{
59585958
range: ['2020-12-14 08:00', '2020-12-22 08:00'],
5959-
positions: ['2020-12-14 12:00', '2020-12-15 12:00', '2020-12-16 12:00', '2020-12-17 12:00', '2020-12-18 12:00', '2020-12-21 12:00', '2020-12-22 12:00'],
5960-
labels: ['Dec 14<br>2020', 'Dec 15', 'Dec 16', 'Dec 17', 'Dec 18', 'Dec 21', ' ']
5959+
positions: ['2020-12-15 12:00', '2020-12-16 12:00', '2020-12-17 12:00', '2020-12-18 12:00', '2020-12-21 12:00', '2020-12-22 12:00'],
5960+
labels: ['Dec 15<br>2020', 'Dec 16', 'Dec 17', 'Dec 18', 'Dec 21', ' ']
59615961
},
59625962
{
59635963
range: ['2020-12-14 08:00', '2020-12-18 08:00'],
5964-
positions: ['2020-12-14', '2020-12-14 12:00', '2020-12-15 06:00', '2020-12-15 12:00', '2020-12-16 06:00', '2020-12-16 12:00', '2020-12-17 06:00', '2020-12-17 12:00', '2020-12-18 06:00'],
5965-
labels: [' ', '12:00<br>Dec 14, 2020', '06:00<br>Dec 15, 2020', '12:00', '06:00<br>Dec 16, 2020', '12:00', '06:00<br>Dec 17, 2020', '12:00', '06:00<br>Dec 18, 2020']
5964+
positions: ['2020-12-14 12:00', '2020-12-15 06:00', '2020-12-15 12:00', '2020-12-16 06:00', '2020-12-16 12:00', '2020-12-17 06:00', '2020-12-17 12:00', '2020-12-18 06:00'],
5965+
labels: ['12:00<br>Dec 14, 2020', '06:00<br>Dec 15, 2020', '12:00', '06:00<br>Dec 16, 2020', '12:00', '06:00<br>Dec 17, 2020', '12:00', '06:00<br>Dec 18, 2020']
59665966
},
59675967
{
59685968
range: ['2020-12-14 08:00', '2020-12-16 08:00'],
5969-
positions: ['2020-12-14 06:00', '2020-12-14 12:00', '2020-12-15 06:00', '2020-12-15 12:00', '2020-12-16 06:00'],
5970-
labels: [' ', '12:00<br>Dec 14, 2020', '06:00<br>Dec 15, 2020', '12:00', '06:00<br>Dec 16, 2020']
5969+
positions: ['2020-12-14 12:00', '2020-12-15 06:00', '2020-12-15 12:00', '2020-12-16 06:00'],
5970+
labels: ['12:00<br>Dec 14, 2020', '06:00<br>Dec 15, 2020', '12:00', '06:00<br>Dec 16, 2020']
59715971
}
59725972
].forEach(function(t) {
59735973
it('should position auto labels with rangebreaks | range:' + t.range, function(done) {

0 commit comments

Comments
 (0)