Skip to content

Commit a9c9589

Browse files
committed
keep text pad consistent when uniformtext rescales at start/end anchor positions
- also revise code related to autorotate
1 parent a827bff commit a9c9589

29 files changed

+560
-39
lines changed

src/lib/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,8 +1186,10 @@ lib.isHidden = function(gd) {
11861186
* @param {object} transform
11871187
* - targetX: desired position on the x-axis
11881188
* - targetY: desired position on the y-axis
1189-
* - textX: width of text
1190-
* - textY: height of text
1189+
* - textX: text middle position on the x-axis
1190+
* - textY: text middle position on the y-axis
1191+
* - anchorX: (optional) text anchor position on the x-axis (computed from textX), zero for middle anchor
1192+
* - anchorY: (optional) text anchor position on the y-axis (computed from textY), zero for middle anchor
11911193
* - scale: (optional) scale applied after translate
11921194
* - rotate: (optional) rotation applied after scale
11931195
* - noCenter: when defined no extra arguments needed in rotation
@@ -1198,15 +1200,17 @@ lib.getTextTransform = function(transform) {
11981200
var textY = transform.textY;
11991201
var targetX = transform.targetX;
12001202
var targetY = transform.targetY;
1203+
var anchorX = transform.anchorX || 0;
1204+
var anchorY = transform.anchorY || 0;
12011205
var rotate = transform.rotate;
12021206
var scale = transform.scale;
12031207
if(!scale) scale = 0;
12041208
else if(scale > 1) scale = 1;
12051209

12061210
return (
12071211
'translate(' +
1208-
(targetX - scale * textX) + ',' +
1209-
(targetY - scale * textY) +
1212+
(targetX - scale * (textX + anchorX)) + ',' +
1213+
(targetY - scale * (textY + anchorY)) +
12101214
')' +
12111215
(scale < 1 ?
12121216
'scale(' + scale + ')' : ''

src/traces/bar/plot.js

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -457,25 +457,16 @@ function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
457457
lx -= 2 * textpad;
458458
ly -= 2 * textpad;
459459

460-
var autoRotate = (angle === 'auto');
461-
var isAutoRotated = false;
462-
if(autoRotate &&
460+
var rotate = getRotateFromAngle(angle);
461+
if((angle === 'auto') &&
463462
!(textWidth <= lx && textHeight <= ly) &&
464463
(textWidth > lx || textHeight > ly) && (
465464
!(textWidth > ly || textHeight > lx) ||
466465
((textWidth < textHeight) !== (lx < ly))
467466
)) {
468-
isAutoRotated = true;
469-
}
470-
471-
if(isAutoRotated) {
472-
// don't rotate yet only swap bar width with height
473-
var tmp = ly;
474-
ly = lx;
475-
lx = tmp;
467+
rotate += 90;
476468
}
477469

478-
var rotate = getRotateFromAngle(angle);
479470
var t = getRotatedTextSize(textBB, rotate);
480471

481472
var scale = 1;
@@ -488,31 +479,42 @@ function toMoveInsideBar(x0, x1, y0, y1, textBB, opts) {
488479
}
489480

490481
// compute text and target positions
482+
var textX = (textBB.left + textBB.right) / 2;
483+
var textY = (textBB.top + textBB.bottom) / 2;
491484
var targetX = (x0 + x1) / 2;
492485
var targetY = (y0 + y1) / 2;
486+
var anchorX = 0;
487+
var anchorY = 0;
493488
if(isStart || isEnd) {
494-
textpad += 0.5 * scale * (isHorizontal !== isAutoRotated ? t.x : t.y);
489+
var extrapad = (isHorizontal ? t.x : t.y) / 2;
490+
var dir = isHorizontal ? dirSign(x0, x1) : dirSign(y0, y1);
495491

496492
if(isHorizontal) {
497-
textpad *= dirSign(x0, x1);
498-
targetX = isStart ? x0 + textpad : x1 - textpad;
493+
if(isStart) {
494+
targetX = x0 + dir * textpad;
495+
anchorX = -dir * extrapad;
496+
} else {
497+
targetX = x1 - dir * textpad;
498+
anchorX = dir * extrapad;
499+
}
499500
} else {
500-
textpad *= dirSign(y0, y1);
501-
targetY = isStart ? y0 + textpad : y1 - textpad;
501+
if(isStart) {
502+
targetY = y0 + dir * textpad;
503+
anchorY = -dir * extrapad;
504+
} else {
505+
targetY = y1 - dir * textpad;
506+
anchorY = dir * extrapad;
507+
}
502508
}
503509
}
504510

505-
var textX = (textBB.left + textBB.right) / 2;
506-
var textY = (textBB.top + textBB.bottom) / 2;
507-
508-
// lastly apply auto rotation
509-
if(isAutoRotated) rotate += 90;
510-
511511
return {
512512
textX: textX,
513513
textY: textY,
514514
targetX: targetX,
515515
targetY: targetY,
516+
anchorX: anchorX,
517+
anchorY: anchorY,
516518
scale: scale,
517519
rotate: rotate
518520
};
@@ -549,24 +551,30 @@ function toMoveOutsideBar(x0, x1, y0, y1, textBB, opts) {
549551
var t = getRotatedTextSize(textBB, rotate);
550552

551553
// compute text and target positions
552-
textpad += 0.5 * scale * (isHorizontal ? t.x : t.y);
554+
var extrapad = (isHorizontal ? t.x : t.y) / 2;
555+
var textX = (textBB.left + textBB.right) / 2;
556+
var textY = (textBB.top + textBB.bottom) / 2;
553557
var targetX = (x0 + x1) / 2;
554558
var targetY = (y0 + y1) / 2;
559+
var anchorX = 0;
560+
var anchorY = 0;
555561

562+
var dir = isHorizontal ? dirSign(x1, x0) : dirSign(y0, y1);
556563
if(isHorizontal) {
557-
targetX = x1 - textpad * dirSign(x1, x0);
564+
targetX = x1 - dir * textpad;
565+
anchorX = dir * extrapad;
558566
} else {
559-
targetY = y1 + textpad * dirSign(y0, y1);
567+
targetY = y1 + dir * textpad;
568+
anchorY = -dir * extrapad;
560569
}
561570

562-
var textX = (textBB.left + textBB.right) / 2;
563-
var textY = (textBB.top + textBB.bottom) / 2;
564-
565571
return {
566572
textX: textX,
567573
textY: textY,
568574
targetX: targetX,
569575
targetY: targetY,
576+
anchorX: anchorX,
577+
anchorY: anchorY,
570578
scale: scale,
571579
rotate: rotate
572580
};

src/traces/treemap/plot.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,10 @@ function plotOne(gd, cd, element, transitionOpts) {
368368
else if(offsetDir === 'right') transform.targetX += deltaX;
369369
}
370370

371-
transform.targetX = viewMapX(transform.targetX);
372-
transform.targetY = viewMapY(transform.targetY);
371+
transform.targetX = viewMapX(transform.targetX - transform.anchorX * transform.scale);
372+
transform.targetY = viewMapY(transform.targetY - transform.anchorY * transform.scale);
373+
transform.anchorX = 0;
374+
transform.anchorY = 0;
373375

374376
if(isNaN(transform.targetX) || isNaN(transform.targetY)) {
375377
return {};
60 Bytes
Loading
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)