diff --git a/draftlogs/8219_fix.md b/draftlogs/8219_fix.md new file mode 100644 index 00000000000..355dd7ba825 --- /dev/null +++ b/draftlogs/8219_fix.md @@ -0,0 +1 @@ +- Fix shape label `texttemplate` on category and multicategory axes [[#8219](https://github.com/plotly/plotly.js/pull/8219)] diff --git a/src/components/shapes/label_texttemplate.js b/src/components/shapes/label_texttemplate.js index cce3abfdecb..3b6174c15b4 100644 --- a/src/components/shapes/label_texttemplate.js +++ b/src/components/shapes/label_texttemplate.js @@ -3,7 +3,11 @@ // Wrapper functions to handle paper-referenced shapes, which have no axis function d2l(v, axis) { - return axis ? axis.d2l(v) : v; + if(!axis) return v; + // Category axes expose d2l_noadd, which maps positions without appending + // new categories; multicategory axes only define that variant. + var conv = axis.d2l_noadd || axis.d2l; + return conv(v); } function l2d(v, axis) { diff --git a/test/jasmine/tests/shapes_test.js b/test/jasmine/tests/shapes_test.js index 683d3bb3e59..d07376ab423 100644 --- a/test/jasmine/tests/shapes_test.js +++ b/test/jasmine/tests/shapes_test.js @@ -2170,3 +2170,46 @@ describe('Shape labels with pixel sizemode', () => { .then(done, done.fail); }); }); + +describe('Shape labels with texttemplate on a multicategory axis', () => { + let gd; + beforeEach(() => { + gd = createGraphDiv(); + }); + afterEach(destroyGraphDiv); + + it('renders the template without adding categories', (done) => { + const data = [{ + type: 'bar', + x: [ + ['2023', '2023', '2024', '2024'], + ['H1', 'H2', 'H1', 'H2'] + ], + y: [3, 5, 4, 6] + }]; + const layout = { + shapes: [{ + type: 'rect', + xref: 'x', + x0: 1.5, + x1: 3.5, + yref: 'paper', + y0: 0, + y1: 1, + label: { texttemplate: '2024' } + }] + }; + + Plotly.newPlot(gd, data, layout) + .then(() => { + expect(gd._fullLayout.xaxis._categories.length).toBe(4); + expect(d3SelectAll('.shape-label-text').text()).toBe('2024'); + + return Plotly.relayout(gd, 'shapes[0].label.texttemplate', '%{width}'); + }) + .then(() => { + expect(d3SelectAll('.shape-label-text').text()).toBe('2'); + }) + .then(done, done.fail); + }); +});