Skip to content

add fallback in autobin routine #1284

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 3 commits into from
Jan 9, 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
5 changes: 5 additions & 0 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,11 @@ axes.autoBin = function(data, ax, nbins, is2d, calendar) {
distinctData.minDiff / msexp, [0.9, 1.9, 4.9, 9.9], true);
size0 = Math.max(minSize, 2 * Lib.stdev(data) /
Math.pow(data.length, is2d ? 0.25 : 0.4));

// fallback if ax.d2c output BADNUMs
// e.g. when user try to plot categorical bins
// on a layout.xaxis.type: 'linear'
if(!isNumeric(size0)) size0 = 1;
}

// piggyback off autotick code to make "nice" bin sizes
Expand Down
91 changes: 91 additions & 0 deletions test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1738,4 +1738,95 @@ describe('Test axes', function() {
]);
});
});

describe('autoBin', function() {

function _autoBin(x, ax, nbins) {
ax._categories = [];
Axes.setConvert(ax);

var d = ax.makeCalcdata({ x: x }, 'x');

return Axes.autoBin(d, ax, nbins, false, 'gregorian');
}

it('should auto bin categories', function() {
var out = _autoBin(
['apples', 'oranges', 'bananas'],
{ type: 'category' }
);

expect(out).toEqual({
start: -0.5,
end: 2.5,
size: 1
});
});

it('should not error out for categories on linear axis', function() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This case and the two below resulted in errors previously.

var out = _autoBin(
['apples', 'oranges', 'bananas'],
{ type: 'linear' }
);

expect(out).toEqual({
start: undefined,
end: undefined,
size: 2
});
});

it('should not error out for categories on log axis', function() {
var out = _autoBin(
['apples', 'oranges', 'bananas'],
{ type: 'log' }
);

expect(out).toEqual({
start: undefined,
end: undefined,
size: 2
});
});

it('should not error out for categories on date axis', function() {
var out = _autoBin(
['apples', 'oranges', 'bananas'],
{ type: 'date' }
);

expect(out).toEqual({
start: undefined,
end: undefined,
size: 2
});
});

it('should auto bin linear data', function() {
var out = _autoBin(
[1, 1, 2, 2, 3, 3, 4, 4],
{ type: 'linear' }
);

expect(out).toEqual({
start: -0.5,
end: 4.5,
size: 1
});
});

it('should auto bin linear data with nbins constraint', function() {
var out = _autoBin(
[1, 1, 2, 2, 3, 3, 4, 4],
{ type: 'linear' },
2
);

expect(out).toEqual({
start: -0.5,
end: 5.5,
size: 2
});
});
});
});