Skip to content

Commit

Permalink
can center labels using uniform mode as they'll get hidden if too big
Browse files Browse the repository at this point in the history
  • Loading branch information
schellj committed Jul 3, 2024
1 parent 8c47c16 commit f28b5b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/traces/pie/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function plot(gd, cdModule) {
if(textPosition === 'outside') {
transform = transformOutsideText(textBB, pt);
} else {
transform = transformInsideText(textBB, pt, cd0);
transform = transformInsideText(textBB, pt, cd0, fullLayout);
if(textPosition === 'auto' && transform.scale < 1) {
var newFont = Lib.ensureUniformFontSize(gd, trace.outsidetextfont);

Expand Down Expand Up @@ -635,7 +635,7 @@ function prerenderTitles(cdModule, gd) {
}
}

function transformInsideText(textBB, pt, cd0) {
function transformInsideText(textBB, pt, cd0, fullLayout) {
var r = cd0.r || pt.rpx1;
var rInscribed = pt.rInscribed;

Expand All @@ -660,6 +660,7 @@ function transformInsideText(textBB, pt, cd0) {
var isTangential = orientation === 'tangential';
var isRadial = orientation === 'radial';
var isAuto = orientation === 'auto';
var isUniformModeHide = fullLayout.uniformtext.mode === 'hide';

var allTransforms = [];
var newT;
Expand Down Expand Up @@ -723,7 +724,7 @@ function transformInsideText(textBB, pt, cd0) {
}

if(isAuto || isRadial) {
newT = calcRadTransform(textBB, r, ring, halfAngle, midAngle);
newT = calcRadTransform(textBB, r, ring, halfAngle, midAngle, isUniformModeHide);
newT.textPosAngle = (pt.startangle + pt.stopangle) / 2;
allTransforms.push(newT);
}
Expand Down Expand Up @@ -760,15 +761,15 @@ function isCrossing(pt, angle) {
);
}

function calcRadTransform(textBB, r, ring, halfAngle, midAngle) {
function calcRadTransform(textBB, r, ring, halfAngle, midAngle, isUniformModeHide) {
r = Math.max(0, r - 2 * TEXTPAD);

// max size if text is rotated radially
var a = textBB.width / textBB.height;
var s = calcMaxHalfSize(a, halfAngle, r, ring);
return {
scale: s * 2 / textBB.height,
rCenter: calcRCenter(a, s / r),
rCenter: isUniformModeHide ? calcRingCenter(ring) : calcRCenter(a, s / r),
rotate: calcRotate(midAngle)
};
}
Expand All @@ -790,6 +791,10 @@ function calcRCenter(a, b) {
return Math.cos(b) - a * b;
}

function calcRingCenter(ring) {
return 1 - ring / 2;
}

function calcRotate(t) {
return (180 / Math.PI * t + 720) % 180 - 90;
}
Expand Down
2 changes: 1 addition & 1 deletion src/traces/sunburst/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ function plotOne(gd, cd, element, transitionOpts) {

// position the text relative to the slice
var textBB = Drawing.bBox(sliceText.node());
pt.transform = transformInsideText(textBB, pt, cd0);
pt.transform = transformInsideText(textBB, pt, cd0, fullLayout);
pt.transform.targetX = getTargetX(pt);
pt.transform.targetY = getTargetY(pt);

Expand Down

0 comments on commit f28b5b8

Please sign in to comment.