Skip to content

Commit 63bb5d9

Browse files
committed
fix line xanchor behavior when x0 == x1
1 parent 1325b7c commit 63bb5d9

File tree

1 file changed

+39
-18
lines changed

1 file changed

+39
-18
lines changed

src/components/shapes/draw.js

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var svgTextUtils = require('../../lib/svg_text_utils');
2323
var constants = require('./constants');
2424
var helpers = require('./helpers');
2525
var getPathString = helpers.getPathString;
26+
var FROM_TL = require('../../constants/alignment').FROM_TL;
2627

2728

2829
// Shapes are stored in gd.layout.shapes, an array of objects
@@ -604,7 +605,7 @@ function drawLabel(gd, index, options, shapeGroup) {
604605
shapeGroup.selectAll('.shape-label').remove();
605606

606607
// If no label, return
607-
if(!options.label) return;
608+
if(!options.label.text) return;
608609

609610
var labelGroupAttrs = {
610611
'data-index': index,
@@ -743,38 +744,58 @@ function calcTextPosition(shapex0, shapey0, shapex1, shapey1, shapeOptions, actu
743744
// Text position functions differently for lines vs. other shapes
744745
if(shapeType === 'line') {
745746
// Set base position for start vs. center vs. end of line (default is 'center')
746-
if(textPosition.indexOf('start') !== -1) {
747+
if(textPosition === 'start') {
747748
textx = shapex0;
748749
texty = shapey0;
749-
if(xanchor === 'auto') {
750-
if(textAngle === 'auto') xanchor = (shapex1 >= shapex0) ? 'left' : 'right';
751-
else xanchor = (shapex1 >= shapex0) ? 'right' : 'left';
752-
}
753-
} else if(textPosition.indexOf('end') !== -1) {
750+
} else if(textPosition === 'end') {
754751
textx = shapex1;
755752
texty = shapey1;
756-
if(xanchor === 'auto') {
757-
if(textAngle === 'auto') xanchor = (shapex1 >= shapex0) ? 'right' : 'left';
758-
else xanchor = (shapex1 >= shapex0) ? 'left' : 'right';
759-
}
760753
} else { // Default: center
761754
textx = (shapex0 + shapex1) / 2;
762755
texty = (shapey0 + shapey1) / 2;
763-
if(xanchor === 'auto') xanchor = 'center';
756+
}
757+
758+
// Set xanchor if xanchor is 'auto'
759+
if(xanchor === 'auto') {
760+
if(textPosition === 'start') {
761+
if(textAngle === 'auto') {
762+
if(shapex1 > shapex0) xanchor = 'left';
763+
else if(shapex1 < shapex0) xanchor = 'right';
764+
else xanchor = 'center';
765+
} else {
766+
if(shapex1 > shapex0) xanchor = 'right';
767+
else if(shapex1 < shapex0) xanchor = 'left';
768+
else xanchor = 'center';
769+
}
770+
} else if(textPosition === 'end') {
771+
if(textAngle === 'auto') {
772+
if(shapex1 > shapex0) xanchor = 'right';
773+
else if(shapex1 < shapex0) xanchor = 'left';
774+
else xanchor = 'center';
775+
} else {
776+
if(shapex1 > shapex0) xanchor = 'left';
777+
else if(shapex1 < shapex0) xanchor = 'right';
778+
else xanchor = 'center';
779+
}
780+
} else {
781+
xanchor = 'center';
782+
}
764783
}
765784

766785
// Special case for padding when angle is 'auto' for lines
767786
// Padding should be treated as an orthogonal offset in this case
768787
// Otherwise, padding is just a simple x and y offset
788+
var paddingConstantsX = { left: 1, center: 0, right: -1 };
789+
var paddingConstantsY = { bottom: -1, middle: 0, top: 1 };
769790
if(textAngle === 'auto') {
770791
// Set direction to apply padding (based on `yanchor` only)
771-
var paddingDirection = { bottom: 1, middle: 0, top: -1 }[yanchor];
772-
paddingX = textPadding * Math.sin(textAngleRad) * paddingDirection;
773-
paddingY = -textPadding * Math.cos(textAngleRad) * paddingDirection;
792+
var paddingDirection = paddingConstantsY[yanchor];
793+
paddingX = -textPadding * Math.sin(textAngleRad) * paddingDirection;
794+
paddingY = textPadding * Math.cos(textAngleRad) * paddingDirection;
774795
} else {
775796
// Set direction to apply padding (based on `xanchor` and `yanchor`)
776-
var paddingDirectionX = { left: 1, center: 0, right: -1 }[xanchor];
777-
var paddingDirectionY = { bottom: -1, middle: 0, top: 1 }[yanchor];
797+
var paddingDirectionX = paddingConstantsX[xanchor];
798+
var paddingDirectionY = paddingConstantsY[yanchor];
778799
paddingX = textPadding * paddingDirectionX;
779800
paddingY = textPadding * paddingDirectionY;
780801
}
@@ -814,7 +835,7 @@ function calcTextPosition(shapex0, shapey0, shapex1, shapey1, shapeOptions, actu
814835
}
815836

816837
// Shift vertical (& horizontal) position according to `yanchor`
817-
var shiftFraction = { middle: 0.5, bottom: 1, top: 0 }[yanchor];
838+
var shiftFraction = FROM_TL[yanchor];
818839
// Adjust so that text is anchored at top of first line rather than at baseline of first line
819840
var baselineAdjust = shapeOptions.label.font.size;
820841
var textHeight = textBB.height;

0 commit comments

Comments
 (0)