Skip to content

Commit 4ce088e

Browse files
authored
Merge pull request #7790 from plotly/cam/7749/shape-text-position-pixel-size-mode
fix: Handle 'pixel' size mode for shape labels
2 parents df144bb + 5d4dae8 commit 4ce088e

6 files changed

Lines changed: 1251 additions & 977 deletions

File tree

draftlogs/7790_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Handle 'pixel' size mode for shape labels [[#7790](https://github.com/plotly/plotly.js/pull/7790)]

src/components/shapes/display_labels.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,33 @@ module.exports = function drawLabel(gd, index, options, shapeGroup) {
104104
const xRefType1 = Axes.getRefType(isArrayXref ? options.xref[1] : options.xref);
105105
const yRefType0 = Axes.getRefType(isArrayYref ? options.yref[0] : options.yref);
106106
const yRefType1 = Axes.getRefType(isArrayYref ? options.yref[1] : options.yref);
107-
const x2p = function(v, shift, xa, xRefType) {
108-
return helpers.getDataToPixel(gd, xa, shift, false, xRefType)(v);
109-
};
110-
const y2p = function(v, shift, ya, yRefType) {
111-
return helpers.getDataToPixel(gd, ya, shift, true, yRefType)(v);
112-
};
113-
shapex0 = x2p(options.x0, options.x0shift, xa0, xRefType0);
114-
shapex1 = x2p(options.x1, options.x1shift, xa1, xRefType1);
115-
shapey0 = y2p(options.y0, options.y0shift, ya0, yRefType0);
116-
shapey1 = y2p(options.y1, options.y1shift, ya1, yRefType1);
107+
const x2p = (v, shift, xa, xRefType) => helpers.getDataToPixel(gd, xa, shift, false, xRefType)(v);
108+
const y2p = (v, shift, ya, yRefType) => helpers.getDataToPixel(gd, ya, shift, true, yRefType)(v);
109+
// When using pixel offset mode it's necessary to add the anchor position for the
110+
// correct final value
111+
if (options.xsizemode === 'pixel') {
112+
const xAnchorPos = x2p(options.xanchor, undefined, xa0, xRefType0);
113+
// Use xa0 for both shifts because in pixel mode x0/x1 are offsets from the
114+
// anchor, which is always on xa0 (pixel mode with array xref is unsupported)
115+
const xShift0 = helpers.getPixelShift(xa0, options.x0shift);
116+
const xShift1 = helpers.getPixelShift(xa0, options.x1shift);
117+
shapex0 = xAnchorPos + options.x0 + xShift0;
118+
shapex1 = xAnchorPos + options.x1 + xShift1;
119+
} else {
120+
shapex0 = x2p(options.x0, options.x0shift, xa0, xRefType0);
121+
shapex1 = x2p(options.x1, options.x1shift, xa1, xRefType1);
122+
}
123+
if (options.ysizemode === 'pixel') {
124+
const yAnchorPos = y2p(options.yanchor, undefined, ya0, yRefType0);
125+
// Both shifts use ya0 for the same reason as above
126+
const yShift0 = helpers.getPixelShift(ya0, options.y0shift);
127+
const yShift1 = helpers.getPixelShift(ya0, options.y1shift);
128+
shapey0 = yAnchorPos - options.y0 + yShift0;
129+
shapey1 = yAnchorPos - options.y1 + yShift1;
130+
} else {
131+
shapey0 = y2p(options.y0, options.y0shift, ya0, yRefType0);
132+
shapey1 = y2p(options.y1, options.y1shift, ya1, yRefType1);
133+
}
117134
}
118135

119136
// Handle `auto` angle

src/components/shapes/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ function convertPath(options, x2p, y2p) {
352352
});
353353
}
354354

355+
exports.getPixelShift = getPixelShift;
355356
function getPixelShift(axis, shift) {
356357
shift = shift || 0;
357358
var shiftPixels = 0;
3.97 KB
Loading

test/image/mocks/text_on_shapes_basic.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@
154154
"y0": 0.2,
155155
"y1": 0.0,
156156
"line": { "color": "#339", "width": 3 }
157+
},
158+
{
159+
"label": { "text": "Circle" },
160+
"showlegend": true,
161+
"legendgroup": "g1",
162+
"type": "circle",
163+
"x0": -25,
164+
"x1": 25,
165+
"xanchor": 1,
166+
"xref": "x",
167+
"xsizemode": "pixel",
168+
"y0": 25,
169+
"y1": 75,
170+
"yanchor": 1,
171+
"yref": "paper",
172+
"ysizemode": "pixel"
157173
}
158174
],
159175
"newshape": {

0 commit comments

Comments
 (0)