Skip to content

Commit fe4576e

Browse files
committed
Refactor label variable filtering to use arrays from label_texttemplate
1 parent 1736f46 commit fe4576e

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/components/shapes/display_labels.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@ module.exports = function drawLabel(gd, index, options, shapeGroup) {
3030
const isMultiAxisX = Array.isArray(options.xref);
3131
const isMultiAxisY = Array.isArray(options.yref);
3232

33-
// For multi-axis shapes, derived variables are meaningless
34-
// Skip them and let texttemplatefallback handle those cases.
35-
const derivedX = ['dx', 'width', 'xcenter', 'slope', 'length'];
36-
const derivedY = ['dy', 'height', 'ycenter', 'slope', 'length'];
37-
3833
for (var key in shapeLabelTexttemplateVars) {
39-
if (isMultiAxisX && derivedX.includes(key)) continue;
40-
if (isMultiAxisY && derivedY.includes(key)) continue;
34+
// For multi-axis shapes, skip variables that require single-axis calculations
35+
// and let texttemplatefallback handle those cases.
36+
var isFunction = typeof shapeLabelTexttemplateVars[key] === 'function';
37+
var isValidForX = !isMultiAxisX || shapeLabelTexttemplateVars.simpleXVariables.includes(key);
38+
var isValidForY = !isMultiAxisY || shapeLabelTexttemplateVars.simpleYVariables.includes(key);
4139

42-
var val = shapeLabelTexttemplateVars[key](options, _xa, _ya);
43-
if (val !== undefined) templateValues[key] = val;
40+
if (isFunction && isValidForX && isValidForY) {
41+
var val = shapeLabelTexttemplateVars[key](options, _xa, _ya);
42+
if (val !== undefined) templateValues[key] = val;
43+
}
4444
}
4545
}
4646
text = Lib.texttemplateStringForShapes({

src/components/shapes/label_texttemplate.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ function slopeFn(shape, xa, ya) {
5959
);
6060
}
6161

62+
// Variables valid for multi-axis shape references (xref/yref as arrays).
63+
// When a shape spans multiple axes, only these variables can be computed
64+
// because other variables require single-axis calculations.
65+
var simpleXVariables = ['x0', 'x1', 'y0', 'y1', 'dy', 'height', 'ycenter'];
66+
var simpleYVariables = ['x0', 'x1', 'y0', 'y1', 'dx', 'width', 'xcenter'];
67+
6268
module.exports = {
6369
x0: x0Fn,
6470
x1: x1Fn,
@@ -72,4 +78,6 @@ module.exports = {
7278
length: lengthFn,
7379
xcenter: xcenterFn,
7480
ycenter: ycenterFn,
81+
simpleXVariables: simpleXVariables,
82+
simpleYVariables: simpleYVariables,
7583
};

0 commit comments

Comments
 (0)