Skip to content

Commit 1e983dd

Browse files
committed
fix pie scoot labels with array inside/outside text
1 parent acafe63 commit 1e983dd

File tree

1 file changed

+35
-16
lines changed
  • shelly/plotlyjs/static/plotlyjs/src

1 file changed

+35
-16
lines changed

shelly/plotlyjs/static/plotlyjs/src/pie.js

+35-16
Original file line numberDiff line numberDiff line change
@@ -434,17 +434,20 @@ pie.plot = function(gd, cdpie) {
434434
.classed('slice', true);
435435
slices.exit().remove();
436436

437-
var outsideTextQuadrants = [
438-
[[],[]], // y<0: x<0, x>=0
439-
[[],[]] // y>=0: x<0, x>=0
440-
];
437+
var quadrants = [
438+
[[],[]], // y<0: x<0, x>=0
439+
[[],[]] // y>=0: x<0, x>=0
440+
],
441+
hasOutsideText = false;
441442

442443
slices.each(function(pt) {
443444
if(pt.hidden) {
444445
d3.select(this).selectAll('path,g').remove();
445446
return;
446447
}
447448

449+
quadrants[pt.pxmid[1] < 0 ? 0 : 1][pt.pxmid[0] < 0 ? 0 : 1].push(pt);
450+
448451
var cx = cd0.cx + depthVector[0],
449452
cy = cd0.cy + depthVector[1],
450453
sliceTop = d3.select(this),
@@ -504,6 +507,9 @@ pie.plot = function(gd, cdpie) {
504507
}
505508
}
506509

510+
pt.cxFinal = cx;
511+
pt.cyFinal = cy;
512+
507513
function arc(start, finish, cw, scale) {
508514
return 'a' + (scale * cd0.r) + ',' + (scale * rSmall) + ' ' + tiltAxis + ' ' +
509515
pt.largeArc + (cw ? ' 1 ' : ' 0 ') +
@@ -602,14 +608,12 @@ pie.plot = function(gd, cdpie) {
602608

603609
// save some stuff to use later ensure no labels overlap
604610
if(transform.outside) {
605-
pt.cxFinal = cx;
606-
pt.cyFinal = cy;
607611
pt.yLabelMin = translateY - textBB.height / 2;
608612
pt.yLabelMid = translateY;
609613
pt.yLabelMax = translateY + textBB.height / 2;
610614
pt.labelExtraX = 0;
611615
pt.labelExtraY = 0;
612-
outsideTextQuadrants[transform.y < 0 ? 0 : 1][transform.x < 0 ? 0 : 1].push(pt);
616+
hasOutsideText = true;
613617
}
614618

615619
sliceText.attr('transform',
@@ -624,7 +628,7 @@ pie.plot = function(gd, cdpie) {
624628
});
625629

626630
// now make sure no labels overlap (at least within one pie)
627-
scootLabels(outsideTextQuadrants, trace);
631+
if(hasOutsideText) scootLabels(quadrants, trace);
628632
slices.each(function(pt) {
629633
if(pt.labelExtraX || pt.labelExtraY) {
630634
// first move the text to its new location
@@ -767,7 +771,7 @@ function transformOutsideText(textBB, pt) {
767771
};
768772
}
769773

770-
function scootLabels(outsideTextQuadrants, trace) {
774+
function scootLabels(quadrants, trace) {
771775
var xHalf,
772776
yHalf,
773777
equatorFirst,
@@ -778,7 +782,9 @@ function scootLabels(outsideTextQuadrants, trace) {
778782
thisQuad,
779783
oppositeQuad,
780784
wholeSide,
781-
i;
785+
i,
786+
thisQuadOutside,
787+
firstOppositeOutsidePt;
782788

783789
function topFirst (a, b) { return a.pxmid[1] - b.pxmid[1]; }
784790
function bottomFirst (a, b) { return b.pxmid[1] - a.pxmid[1]; }
@@ -847,20 +853,33 @@ function scootLabels(outsideTextQuadrants, trace) {
847853
// first sort the array
848854
// note this is a copy of cd, so cd itself doesn't get sorted
849855
// but we can still modify points in place.
850-
thisQuad = outsideTextQuadrants[yHalf][xHalf];
856+
thisQuad = quadrants[yHalf][xHalf];
851857
thisQuad.sort(equatorFirst);
852858

853-
oppositeQuad = outsideTextQuadrants[1 - yHalf][xHalf];
859+
oppositeQuad = quadrants[1 - yHalf][xHalf];
854860
wholeSide = oppositeQuad.concat(thisQuad);
855861

856-
// each needs to avoid the previous
862+
thisQuadOutside = [];
857863
for(i = 0; i < thisQuad.length; i++) {
858-
var prevPt = i && thisQuad[i - 1];
864+
if(thisQuad[i].yLabelMid !== undefined) thisQuadOutside.push(thisQuad[i]);
865+
}
866+
867+
firstOppositeOutsidePt = false;
868+
for(i = 0; yHalf && i < oppositeQuad.length; i++) {
869+
if(oppositeQuad[i].yLabelMid !== undefined) {
870+
firstOppositeOutsidePt = oppositeQuad[i];
871+
break;
872+
}
873+
}
874+
875+
// each needs to avoid the previous
876+
for(i = 0; i < thisQuadOutside.length; i++) {
877+
var prevPt = i && thisQuadOutside[i - 1];
859878
// bottom half needs to avoid the first label of the top half
860879
// top half we still need to call scootOneLabel on the first slice
861880
// so we can avoid other slices, but we don't pass a prevPt
862-
if(yHalf && !i) prevPt = oppositeQuad[0];
863-
scootOneLabel(thisQuad[i], prevPt);
881+
if(firstOppositeOutsidePt && !i) prevPt = firstOppositeOutsidePt;
882+
scootOneLabel(thisQuadOutside[i], prevPt);
864883
}
865884
}
866885
}

0 commit comments

Comments
 (0)