Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(markLine): provide new layouts for markLine labels #11843

Merged
merged 3 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 84 additions & 26 deletions src/chart/helper/Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,47 +141,99 @@ 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;
var distanceX = distance[0] * invScale;
var distanceY = distance[1] * 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 = -distanceY;
textVerticalAlign = 'bottom';
break;

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

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

switch (label.__position) {
case 'end':
textPosition = [d[0] * distanceX + toPos[0], d[1] * distanceY + 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] * distanceX + fromPos[0], -d[1] * distanceY + 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 = [distanceX * dir + fromPos[0], fromPos[1] + dy];
textAlign = tangent[0] < 0 ? 'right' : 'left';
textOrigin = [-distanceX * 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 = [-distanceX * dir + toPos[0], toPos[1] + dy];
textAlign = tangent[0] >= 0 ? 'right' : 'left';
textOrigin = [distanceX * 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 +409,12 @@ lineProto._updateCommonStl = function (lineData, idx, seriesScope) {
label.__verticalAlign = labelStyle.textVerticalAlign;
// 'start', 'middle', 'end'
label.__position = labelModel.get('position') || 'middle';

var distance = labelModel.get('distance');
if (!zrUtil.isArray(distance)) {
distance = [distance, distance];
}
label.__labelDistance = 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
96 changes: 61 additions & 35 deletions test/markLine.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,61 @@
data2.push(+Math.random().toFixed(2));
}

var markLine1 = [];
var markLine2 = [];
var positions = [
'start', 'middle', 'end',
'insideStart', 'insideStartTop', 'insideStartBottom',
'insideMiddle', 'insideMiddleTop', 'insideMiddleBottom',
'insideEnd', 'insideEndTop', 'insideEndBottom'
];
for (var i = 0; i < positions.length; ++i) {
markLine1.push({
name: positions[i],
yAxis: 1.8 - 0.2 * Math.floor(i / 3),
label: {
formatter: '{b}',
position: positions[i]
}
});
markLine1.push([{
name: 'start: ' + positions[i],
type: 'min',
label: {
formatter: positions[i],
position: positions[i]
}
}, {
name: 'end: ' + positions[i],
type: 'max'
}]);

markLine2.push({
name: positions[i],
xAxis: Math.floor(i / 3),
label: {
formatter: '{b}',
position: positions[i]
}
});
markLine2.push([{
name: 'start: ' + positions[i],
type: 'min',
valueIndex: 0,
label: {
formatter: positions[i],
position: positions[i]
}
}, {
name: 'end: ' + positions[i],
valueIndex: 0,
type: 'max'
}]);
}

chart.setOption({
animation: false,
color: ['#f60', '#0c6'],
legend: {
data: ['line', 'line2']
},
Expand All @@ -77,7 +131,8 @@
yAxis: {
splitLine: {
// show: false
}
},
max: 2
},
series: [{
name: 'line',
Expand All @@ -87,26 +142,7 @@
data: data1,

markLine: {
data: [{
name: '平均值',
type: 'average',
valueIndex: 1
}, {
name: '指定值',
yAxis: 1
}, [{
name: '标签位置为中间',
type: 'min',
label: {
normal: {
formatter: '{b}',
position: 'middle'
}
}
}, {
name: '标签位置为中间',
type: 'max'
}]]
data: markLine1
}
}, {
name: 'line2',
Expand All @@ -116,20 +152,10 @@
data: data2,

markLine: {
data: [{
name: '平均值',
type: 'average',
valueIndex: 0
}, {
name: '指定值',
xAxis: 3
}, [{
name: '最大点',
type: 'max'
}, {
x: '90%',
yAxis: 'max'
}]]
data: markLine2,
label: {
distance: [20, 5]
}
}
}]
});
Expand Down