Skip to content

Commit

Permalink
feat(markLine): provide new layouts for markLine labels #11569
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovilia committed Dec 19, 2019
1 parent ea2bcb3 commit 89221c8
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 27 deletions.
103 changes: 77 additions & 26 deletions src/chart/helper/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,47 +141,97 @@ function updateSymbolAndLabelBeforeLineUpdate() {
var textPosition;
var textAlign;
var textVerticalAlign;

var distance = 5 * invScale;
// End
if (label.__position === 'end') {
textPosition = [d[0] * distance + toPos[0], d[1] * distance + toPos[1]];
textAlign = d[0] > 0.8 ? 'left' : (d[0] < -0.8 ? 'right' : 'center');
textVerticalAlign = d[1] > 0.8 ? 'top' : (d[1] < -0.8 ? 'bottom' : 'middle');
var textOrigin;

var distance = label.__labelDistance * invScale;
var halfPercent = percent / 2;
var tangent = line.tangentAt(halfPercent);
var n = [tangent[1], -tangent[0]];
var cp = line.pointAt(halfPercent);
if (n[1] > 0) {
n[0] = -n[0];
n[1] = -n[1];
}
// Middle
else if (label.__position === 'middle') {
var halfPercent = percent / 2;
var tangent = line.tangentAt(halfPercent);
var n = [tangent[1], -tangent[0]];
var cp = line.pointAt(halfPercent);
if (n[1] > 0) {
n[0] = -n[0];
n[1] = -n[1];
}
textPosition = [cp[0] + n[0] * distance, cp[1] + n[1] * distance];
textAlign = 'center';
textVerticalAlign = 'bottom';
var dir = tangent[0] < 0 ? -1 : 1;

if (label.__position !== 'start' && label.__position !== 'end') {
var rotation = -Math.atan2(tangent[1], tangent[0]);
if (toPos[0] < fromPos[0]) {
rotation = Math.PI + rotation;
}
label.attr('rotation', rotation);
}
// Start
else {
textPosition = [-d[0] * distance + fromPos[0], -d[1] * distance + fromPos[1]];
textAlign = d[0] > 0.8 ? 'right' : (d[0] < -0.8 ? 'left' : 'center');
textVerticalAlign = d[1] > 0.8 ? 'bottom' : (d[1] < -0.8 ? 'top' : 'middle');

var dy;
switch (label.__position) {
case 'insideStartTop':
case 'insideMiddleTop':
case 'insideEndTop':
case 'middle':
dy = -distance;
textVerticalAlign = 'bottom';
break;

case 'insideStartBottom':
case 'insideMiddleBottom':
case 'insideEndBottom':
dy = distance;
textVerticalAlign = 'top';
break;

default:
dy = 0;
textVerticalAlign = 'middle';
}

switch (label.__position) {
case 'end':
textPosition = [d[0] * distance + toPos[0], d[1] * distance + toPos[1]];
textAlign = d[0] > 0.8 ? 'left' : (d[0] < -0.8 ? 'right' : 'center');
textVerticalAlign = d[1] > 0.8 ? 'top' : (d[1] < -0.8 ? 'bottom' : 'middle');
break;

case 'start':
textPosition = [-d[0] * distance + fromPos[0], -d[1] * distance + fromPos[1]];
textAlign = d[0] > 0.8 ? 'right' : (d[0] < -0.8 ? 'left' : 'center');
textVerticalAlign = d[1] > 0.8 ? 'bottom' : (d[1] < -0.8 ? 'top' : 'middle');
break;

case 'insideStartTop':
case 'insideStart':
case 'insideStartBottom':
textPosition = [distance * dir + fromPos[0], fromPos[1] + dy];
textAlign = tangent[0] < 0 ? 'right' : 'left';
textOrigin = [-distance * dir, -dy];
break;

case 'insideMiddleTop':
case 'insideMiddle':
case 'insideMiddleBottom':
case 'middle':
textPosition = [cp[0], cp[1] + dy];
textAlign = 'center';
textOrigin = [0, -dy];
break;

case 'insideEndTop':
case 'insideEnd':
case 'insideEndBottom':
textPosition = [-distance * dir + toPos[0], toPos[1] + dy];
textAlign = tangent[0] >= 0 ? 'right' : 'left';
textOrigin = [distance * dir, -dy];
break;
}

label.attr({
style: {
// Use the user specified text align and baseline first
textVerticalAlign: label.__verticalAlign || textVerticalAlign,
textAlign: label.__textAlign || textAlign
},
position: textPosition,
scale: [invScale, invScale]
scale: [invScale, invScale],
origin: textOrigin
});
}
}
Expand Down Expand Up @@ -357,6 +407,7 @@ lineProto._updateCommonStl = function (lineData, idx, seriesScope) {
label.__verticalAlign = labelStyle.textVerticalAlign;
// 'start', 'middle', 'end'
label.__position = labelModel.get('position') || 'middle';
label.__labelDistance = labelModel.get('distance');
}

if (emphasisText != null) {
Expand Down
3 changes: 2 additions & 1 deletion src/component/marker/MarkLineModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export default MarkerModel.extend({
},
label: {
show: true,
position: 'end'
position: 'end',
distance: 5
},
lineStyle: {
type: 'dashed'
Expand Down

0 comments on commit 89221c8

Please sign in to comment.