Skip to content
Open
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
1 change: 1 addition & 0 deletions draftlogs/8219_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix shape label `texttemplate` on category and multicategory axes [[#8219](https://github.com/plotly/plotly.js/pull/8219)]
6 changes: 5 additions & 1 deletion src/components/shapes/label_texttemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
43 changes: 43 additions & 0 deletions test/jasmine/tests/shapes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Loading