diff --git a/src/plots/cartesian/set_convert.js b/src/plots/cartesian/set_convert.js index c477b13c395..af34c0febe1 100644 --- a/src/plots/cartesian/set_convert.js +++ b/src/plots/cartesian/set_convert.js @@ -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); } @@ -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)); }; diff --git a/test/image/baselines/bar_annotation_max_range_eq_category.png b/test/image/baselines/bar_annotation_max_range_eq_category.png new file mode 100644 index 00000000000..f919fffe294 Binary files /dev/null and b/test/image/baselines/bar_annotation_max_range_eq_category.png differ diff --git a/test/image/mocks/bar_annotation_max_range_eq_category.json b/test/image/mocks/bar_annotation_max_range_eq_category.json new file mode 100644 index 00000000000..3585b6f479a --- /dev/null +++ b/test/image/mocks/bar_annotation_max_range_eq_category.json @@ -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 + } + ] + } +} diff --git a/test/jasmine/tests/axes_test.js b/test/jasmine/tests/axes_test.js index bd1c5c4a1cd..08c55a20360 100644 --- a/test/jasmine/tests/axes_test.js +++ b/test/jasmine/tests/axes_test.js @@ -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; diff --git a/test/jasmine/tests/mock_test.js b/test/jasmine/tests/mock_test.js index 00ead5f8508..86825fc7e0d 100644 --- a/test/jasmine/tests/mock_test.js +++ b/test/jasmine/tests/mock_test.js @@ -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', @@ -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');