Skip to content

Commit

Permalink
make tangential go towards noon and 6 and make radial go towards 3 and 9
Browse files Browse the repository at this point in the history
  • Loading branch information
archmoj committed Dec 18, 2019
1 parent a27dc29 commit 1479943
Show file tree
Hide file tree
Showing 12 changed files with 956 additions and 28 deletions.
7 changes: 7 additions & 0 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,13 @@ lib.isHidden = function(gd) {

/** Return transform text for bar bar-like rectangles and pie-like slices
* @param {object} transform
* - targetX: desired position on the x-axis
* - targetY: desired position on the y-axis
* - textX: width of text
* - textY: height of text
* - scale: (optional) scale applied after translate
* - rotate: (optional) rotation applied after scale
* - noCenter: when defined no extra arguments needed in rotation
*/
lib.getTextTransform = function(transform) {
var noCenter = transform.noCenter;
Expand Down
79 changes: 51 additions & 28 deletions src/traces/pie/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,25 +568,7 @@ function transformInsideText(textBB, pt, cd0) {
var isCircle = (ring === 1) && (Math.abs(pt.startangle - pt.stopangle) === Math.PI * 2);
var allTransforms = [];

if(isCircle || isAuto || isHorizontal) {
// max size text can be inserted inside without rotating it
// this inscribes the text rectangle in a circle, which is then inscribed
// in the slice, so it will be an underestimate, which some day we may want
// to improve so this case can get more use
var transform = {
scale: rInscribed * r * 2 / textDiameter,

// and the center position and rotation in this case
rCenter: 1 - rInscribed,
rotate: 0
};

if(transform.scale >= 1) return transform;

allTransforms.push(transform);
}

if(isHorizontal) {
if(!isAuto) {
// max size if text is placed (horizontally) at the top or bottom of the arc

var considerCrossing = function(angle, key) {
Expand All @@ -608,12 +590,40 @@ function transformInsideText(textBB, pt, cd0) {
}
};

for(var i = 3; i >= -3; i--) { // to cover all cases with trace.rotation added
considerCrossing(Math.PI * i, 'tan');
considerCrossing(Math.PI * (i + 0.5), 'rad');
// to cover all cases with trace.rotation added
var i;
if(isHorizontal || isTangential) {
// top
for(i = 4; i >= -4; i -= 2) considerCrossing(Math.PI * i, 'tan');
// bottom
for(i = 4; i >= -4; i -= 2) considerCrossing(Math.PI * (i + 1), 'tan');
}
if(isHorizontal || isRadial) {
// left
for(i = 4; i >= -4; i -= 2) considerCrossing(Math.PI * (i + 1.5), 'rad');
// right
for(i = 4; i >= -4; i -= 2) considerCrossing(Math.PI * (i + 0.5), 'rad');
}
}

if(isCircle || isAuto || isHorizontal) {
// max size text can be inserted inside without rotating it
// this inscribes the text rectangle in a circle, which is then inscribed
// in the slice, so it will be an underestimate, which some day we may want
// to improve so this case can get more use
var transform = {
scale: rInscribed * r * 2 / textDiameter,

// and the center position and rotation in this case
rCenter: 1 - rInscribed,
rotate: 0
};

if(transform.scale >= 1) return transform;

allTransforms.push(transform);
}

if(isAuto || isRadial) {
allTransforms.push(calcRadTransform(textBB, r, ring, halfAngle, midAngle));
}
Expand All @@ -622,16 +632,29 @@ function transformInsideText(textBB, pt, cd0) {
allTransforms.push(calcTanTransform(textBB, r, ring, halfAngle, midAngle));
}

var maxScaleTransform = allTransforms.sort(function(a, b) {
return b.scale - a.scale;
})[0];
var id = 0;
var maxScale = 0;
for(var k = 0; k < allTransforms.length; k++) {
var s = allTransforms[k].scale;
if(maxScale < s) {
maxScale = s;
id = k;
}

if(!isAuto && maxScale >= 1) {
// respect test order for non-auto options
break;
}
}

var selTransform = allTransforms[id];

if(maxScaleTransform.pxtxt) {
if(selTransform.pxtxt) {
// copy text position if not at the middle
pt.pxtxt = maxScaleTransform.pxtxt;
pt.pxtxt = selTransform.pxtxt;
}

return maxScaleTransform;
return selTransform;
}

function isCrossing(pt, angle) {
Expand Down
Binary file modified test/image/baselines/sunburst_inside-text-orientation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/sunburst_with-without_values.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/uniformtext_pie_8_horizontal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/uniformtext_pie_8_radial.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/uniformtext_sunburst_treemap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1479943

Please sign in to comment.