Skip to content

Commit 1e17697

Browse files
authored
Merge pull request #5211 from LoganWlv/fix/#5200/auto-range-extreme-eq-category
Fix autorange computation when a category match a range extreme
2 parents 8b76d46 + f4a6b82 commit 1e17697

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

src/plots/cartesian/set_convert.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ module.exports = function setConvert(ax, fullLayout) {
185185
if(isNumeric(v)) return +v;
186186
}
187187

188+
function getRangePosition(v) {
189+
return isNumeric(v) ? +v : getCategoryIndex(v);
190+
}
191+
188192
// include 2 fractional digits on pixel, for PDF zooming etc
189193
function _l2p(v, m, b) { return d3.round(b + m * v, 2); }
190194

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

318322
ax.r2c = function(v) {
319-
var index = getCategoryPosition(v);
323+
var index = getRangePosition(v);
320324
return index !== undefined ? index : ax.fraction2r(0.5);
321325
};
322326

323327
ax.l2r = ax.c2r = ensureNumber;
324-
ax.r2l = getCategoryPosition;
328+
ax.r2l = getRangePosition;
325329

326330
ax.d2p = function(v) { return ax.l2p(ax.r2c(v)); };
327331
ax.p2d = function(px) { return getCategoryName(p2l(px)); };
Loading
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"data": [
3+
{
4+
"x": ["1.5", "2.5", "3.5", "Not Known"],
5+
"y": ["1.0", "1.0", "2.0", "1.0"],
6+
"type": "bar"
7+
}
8+
],
9+
"layout": {
10+
"xaxis": {
11+
"type": "category",
12+
"autorange": true
13+
},
14+
"annotations": [
15+
{
16+
"x": "Not Known",
17+
"y": 1,
18+
"text": "Should point to category",
19+
"showarrow": true
20+
},
21+
{
22+
"x": 1.5,
23+
"y": 1.0,
24+
"text": "Should point to serial number",
25+
"showarrow": true
26+
}
27+
]
28+
}
29+
}

test/jasmine/tests/axes_test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,25 @@ describe('Test axes', function() {
17811781
});
17821782
});
17831783

1784+
describe('bar category autorange', function() {
1785+
var gd;
1786+
1787+
beforeEach(function() {
1788+
gd = createGraphDiv();
1789+
});
1790+
1791+
afterEach(destroyGraphDiv);
1792+
1793+
describe('a category has the same value of one of the auto range computed extreme', function() {
1794+
it('should compute the right range for X axis', function() {
1795+
Plotly.plot(gd, [{x: ['0', '-0.5', '3.5', 'Not Known'], y: [ '1.0', '1.0', '2.0', '1.0'], type: 'bar'}], {
1796+
xaxis: {type: 'category', autorange: true}
1797+
});
1798+
expect(gd._fullLayout.xaxis._rl).toEqual([-0.5, 3.5]);
1799+
});
1800+
});
1801+
});
1802+
17841803
describe('handleTickDefaults', function() {
17851804
var data = [{ x: [1, 2, 3], y: [3, 4, 5] }];
17861805
var gd;

test/jasmine/tests/mock_test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ var list = [
118118
'bar_hide_nulls',
119119
'bar_line',
120120
'bar_marker_array',
121+
'bar_annotation_max_range_eq_category',
121122
'bar_multiline_labels',
122123
'bar_nonnumeric_sizes',
123124
'bar_show_narrow',
@@ -1181,6 +1182,7 @@ figs['bar_group_percent'] = require('@mocks/bar_group_percent');
11811182
figs['bar_hide_nulls'] = require('@mocks/bar_hide_nulls');
11821183
figs['bar_line'] = require('@mocks/bar_line');
11831184
figs['bar_marker_array'] = require('@mocks/bar_marker_array');
1185+
figs['bar_annotation_max_range_eq_category'] = require('@mocks/bar_annotation_max_range_eq_category');
11841186
figs['bar_multiline_labels'] = require('@mocks/bar_multiline_labels');
11851187
figs['bar_nonnumeric_sizes'] = require('@mocks/bar_nonnumeric_sizes');
11861188
figs['bar_show_narrow'] = require('@mocks/bar_show_narrow');

0 commit comments

Comments
 (0)