Skip to content

smarter tick count limit for cartesian axes #1898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -804,13 +804,18 @@ axes.calcTicks = function calcTicks(ax) {
endtick = (axrev) ? Math.max(-0.5, endtick) :
Math.min(ax._categories.length - 0.5, endtick);
}

var xPrevious = null;
var maxTicks = Math.max(1000, ax._length || 0);
for(var x = ax._tmin;
(axrev) ? (x >= endtick) : (x <= endtick);
x = axes.tickIncrement(x, ax.dtick, axrev, ax.calendar)) {
vals.push(x);
// prevent infinite loops - no more than one tick per pixel,
// and make sure each value is different from the previous
if(vals.length > maxTicks || x === xPrevious) break;
xPrevious = x;

// prevent infinite loops
if(vals.length > 1000) break;
vals.push(x);
}

// save the last tick as well as first, so we can
Expand Down
32 changes: 32 additions & 0 deletions test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2225,6 +2225,38 @@ describe('Test axes', function() {
['2000-01-01 11:00:00.0001', 'Jan 1, 2000, 11:00:00.0001']
]);
});

it('avoids infinite loops due to rounding errors', function() {
var textOut = mockCalc({
type: 'linear',
tickmode: 'linear',
tick0: 1e200,
dtick: 1e-200,
range: [1e200, 2e200]
});

// This actually gives text '-Infinity' because it can't
// calculate the first tick properly, but since it's not going to
// be able to do any better with the rest, we don't much care.
expect(textOut.length).toBe(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test!

});

it('truncates at the greater of 1001 ticks or one per pixel', function() {
var ax = {
type: 'linear',
tickmode: 'linear',
tick0: 0,
dtick: 1,
range: [0, 1e6],
_length: 100
};

expect(mockCalc(ax).length).toBe(1001);

ax._length = 10000;

expect(mockCalc(ax).length).toBe(10001);
});
});

describe('autoBin', function() {
Expand Down