Skip to content

Fix autorange computation when a category match a range extreme #5211

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
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
8 changes: 6 additions & 2 deletions src/plots/cartesian/set_convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ module.exports = function setConvert(ax, fullLayout) {
if(isNumeric(v)) return +v;
}

function getRangePosition(v) {
return isNumeric(v) ? +v : getCategoryIndex(v);
}

// include 2 fractional digits on pixel, for PDF zooming etc
function _l2p(v, m, b) { return d3.round(b + m * v, 2); }

Expand Down Expand Up @@ -316,12 +320,12 @@ module.exports = function setConvert(ax, fullLayout) {
ax.d2r = ax.d2l_noadd = getCategoryPosition;

ax.r2c = function(v) {
var index = getCategoryPosition(v);
var index = getRangePosition(v);
return index !== undefined ? index : ax.fraction2r(0.5);
};

ax.l2r = ax.c2r = ensureNumber;
ax.r2l = getCategoryPosition;
ax.r2l = getRangePosition;

ax.d2p = function(v) { return ax.l2p(ax.r2c(v)); };
ax.p2d = function(px) { return getCategoryName(p2l(px)); };
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions test/image/mocks/bar_annotation_max_range_eq_category.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"data": [
{
"x": ["1.5", "2.5", "3.5", "Not Known"],
"y": ["1.0", "1.0", "2.0", "1.0"],
"type": "bar"
}
],
"layout": {
"xaxis": {
"type": "category",
"autorange": true
},
"annotations": [
{
"x": "Not Known",
"y": 1,
"text": "Should point to category",
"showarrow": true
},
{
"x": 1.5,
"y": 1.0,
"text": "Should point to serial number",
"showarrow": true
}
]
}
}
19 changes: 19 additions & 0 deletions test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,25 @@ describe('Test axes', function() {
});
});

describe('bar category autorange', function() {
var gd;

beforeEach(function() {
gd = createGraphDiv();
});

afterEach(destroyGraphDiv);

describe('a category has the same value of one of the auto range computed extreme', function() {
it('should compute the right range for X axis', function() {
Plotly.plot(gd, [{x: ['0', '-0.5', '3.5', 'Not Known'], y: [ '1.0', '1.0', '2.0', '1.0'], type: 'bar'}], {
xaxis: {type: 'category', autorange: true}
});
expect(gd._fullLayout.xaxis._rl).toEqual([-0.5, 3.5]);
});
});
});

describe('handleTickDefaults', function() {
var data = [{ x: [1, 2, 3], y: [3, 4, 5] }];
var gd;
Expand Down
2 changes: 2 additions & 0 deletions test/jasmine/tests/mock_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ var list = [
'bar_hide_nulls',
'bar_line',
'bar_marker_array',
'bar_annotation_max_range_eq_category',
'bar_multiline_labels',
'bar_nonnumeric_sizes',
'bar_show_narrow',
Expand Down Expand Up @@ -1181,6 +1182,7 @@ figs['bar_group_percent'] = require('@mocks/bar_group_percent');
figs['bar_hide_nulls'] = require('@mocks/bar_hide_nulls');
figs['bar_line'] = require('@mocks/bar_line');
figs['bar_marker_array'] = require('@mocks/bar_marker_array');
figs['bar_annotation_max_range_eq_category'] = require('@mocks/bar_annotation_max_range_eq_category');
figs['bar_multiline_labels'] = require('@mocks/bar_multiline_labels');
figs['bar_nonnumeric_sizes'] = require('@mocks/bar_nonnumeric_sizes');
figs['bar_show_narrow'] = require('@mocks/bar_show_narrow');
Expand Down