Skip to content

Commit 8d61ece

Browse files
committed
fix #1645 - ensure we don't draw ticks if there are none to draw
1 parent bd0b903 commit 8d61ece

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/plots/cartesian/axes.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -522,22 +522,27 @@ axes.calcTicks = function calcTicks(ax) {
522522
// find the first tick
523523
ax._tmin = axes.tickFirst(ax);
524524

525+
// add a tiny bit so we get ticks which may have rounded out
526+
var startTick = rng[0] * 1.0001 - rng[1] * 0.0001;
527+
var endTick = rng[1] * 1.0001 - rng[0] * 0.0001;
525528
// check for reversed axis
526529
var axrev = (rng[1] < rng[0]);
527530

531+
// No visible ticks? Quit.
532+
// I've only seen this on category axes with all categories off the edge.
533+
if((ax._tmin < startTick) !== axrev) return [];
534+
528535
// return the full set of tick vals
529-
var vals = [],
530-
// add a tiny bit so we get ticks which may have rounded out
531-
endtick = rng[1] * 1.0001 - rng[0] * 0.0001;
536+
var vals = [];
532537
if(ax.type === 'category') {
533-
endtick = (axrev) ? Math.max(-0.5, endtick) :
534-
Math.min(ax._categories.length - 0.5, endtick);
538+
endTick = (axrev) ? Math.max(-0.5, endTick) :
539+
Math.min(ax._categories.length - 0.5, endTick);
535540
}
536541

537542
var xPrevious = null;
538543
var maxTicks = Math.max(1000, ax._length || 0);
539544
for(var x = ax._tmin;
540-
(axrev) ? (x >= endtick) : (x <= endtick);
545+
(axrev) ? (x >= endTick) : (x <= endTick);
541546
x = axes.tickIncrement(x, ax.dtick, axrev, ax.calendar)) {
542547
// prevent infinite loops - no more than one tick per pixel,
543548
// and make sure each value is different from the previous

test/jasmine/tests/axes_test.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,6 +2226,29 @@ describe('Test axes', function() {
22262226
expect(ax._categories).toEqual(['a', 'b', 'c', 'd']);
22272227
});
22282228

2229+
it('notices when all categories are off the edge', function() {
2230+
var ax = {
2231+
type: 'category',
2232+
_categories: ['a', 'b', 'c', 'd'],
2233+
_categoriesMap: {'a': 0, 'b': 1, 'c': 2, 'd': 3},
2234+
tickmode: 'linear',
2235+
tick0: 0,
2236+
dtick: 1,
2237+
range: [-0.5, 3.5]
2238+
};
2239+
2240+
// baseline
2241+
expect(mockCalc(ax)).toEqual(['a', 'b', 'c', 'd']);
2242+
// reversed baseline
2243+
ax.range = [3.5, -0.5];
2244+
expect(mockCalc(ax)).toEqual(['d', 'c', 'b', 'a']);
2245+
2246+
[[-5, -1], [-1, -5], [5, 10], [10, 5]].forEach(function(rng) {
2247+
ax.range = rng;
2248+
expect(mockCalc(ax).length).toBe(0, rng);
2249+
});
2250+
});
2251+
22292252
it('should always start at year for date axis hover', function() {
22302253
var ax = {
22312254
type: 'date',
@@ -2321,10 +2344,8 @@ describe('Test axes', function() {
23212344
range: [1e200, 2e200]
23222345
});
23232346

2324-
// This actually gives text '-Infinity' because it can't
2325-
// calculate the first tick properly, but since it's not going to
2326-
// be able to do any better with the rest, we don't much care.
2327-
expect(textOut.length).toBe(1);
2347+
// with the fix for #1645 we're not even getting the '-Infinity' we used to :tada:
2348+
expect(textOut.length).toBe(0);
23282349
});
23292350

23302351
it('truncates at the greater of 1001 ticks or one per pixel', function() {

0 commit comments

Comments
 (0)