Skip to content

Commit

Permalink
Merge branch 'bugfixes' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
pissang committed Oct 14, 2020
2 parents 2f566e9 + f7ac75e commit a77894c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 58 deletions.
72 changes: 66 additions & 6 deletions src/chart/line/LineView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,59 @@ function canShowAllSymbolForCategory(
return true;
}


function isPointNull(x: number, y: number) {
return isNaN(x) || isNaN(y);
}

function getLastIndexNotNull(points: ArrayLike<number>) {
let len = points.length / 2;
for (; len > 0; len--) {
if (!isPointNull(points[len * 2 - 2], points[len * 2 - 1])) {
break;
}
}

return len - 1;
}

function getPointAtIndex(points: ArrayLike<number>, idx: number) {
return [points[idx * 2], points[idx * 2 + 1]];
}

function getIndexRange(points: ArrayLike<number>, xOrY: number, dim: 'x' | 'y') {
const len = points.length / 2;

const dimIdx = dim === 'x' ? 0 : 1;
let a;
let b;
let prevIndex = 0;
let nextIndex = -1;
for (let i = 0; i < len; i++) {
b = points[i * 2 + dimIdx];
if (isNaN(b) || isNaN(points[i * 2 + 1 - dimIdx])) {
continue;
}
if (i === 0) {
a = b;
continue;
}
if (a <= xOrY && b >= xOrY || a >= xOrY && b <= xOrY) {
nextIndex = i;
break;
}

prevIndex = i;
a = b;
}

return {
range: [prevIndex, nextIndex],
t: (xOrY - a) / (b - a)
};
}


interface EndLabelAnimationRecord {
lastFrameIndex: number
originalX?: number
Expand Down Expand Up @@ -393,8 +446,14 @@ function createLineClipPath(

const isHorizontal = coordSys.getBaseAxis().isHorizontal();
const clipPath = createGridClipPath(coordSys, hasAnimation, seriesModel, () => {
if (lineView._endLabel && labelAnimationRecord.originalX != null) {
lineView._endLabel.attr({x: labelAnimationRecord.originalX, y: labelAnimationRecord.originalY});
const endLabel = lineView._endLabel;
if (endLabel && hasAnimation) {
if (labelAnimationRecord.originalX != null) {
endLabel.attr({
x: labelAnimationRecord.originalX,
y: labelAnimationRecord.originalY
});
}
}
}, during);
// Expand clip shape to avoid clipping when line value exceeds axis
Expand Down Expand Up @@ -995,7 +1054,7 @@ class LineView extends ChartView {
}

// Find last non-NaN data to display data
const dataIndex = this._polyline.getLastIndexNotNull();
const dataIndex = getLastIndexNotNull(data.getLayout('points'));
if (dataIndex >= 0) {
setLabelStyle(
endLabel,
Expand Down Expand Up @@ -1039,6 +1098,7 @@ class LineView extends ChartView {
animationRecord.originalY = endLabel.y;
}

const points = data.getLayout('points');
const seriesModel = data.hostModel as LineSeriesModel;
const connectNulls = seriesModel.get('connectNulls');
const precision = endLabelModel.get('precision');
Expand All @@ -1053,15 +1113,15 @@ class LineView extends ChartView {
: isHorizontal ? (clipShape.x + clipShape.width) : clipShape.y;
const dim = isHorizontal ? 'x' : 'y';

const dataIndexRange = polyline.getIndexRange(xOrY, dim);
const dataIndexRange = getIndexRange(points, xOrY, dim);
const indices = dataIndexRange.range;

const diff = indices[1] - indices[0];
let value: ParsedValue;
if (diff >= 1) {
// diff > 1 && connectNulls, which is on the null data.
if (diff > 1 && !connectNulls) {
const pt = polyline.getPointAtIndex(indices[0]);
const pt = getPointAtIndex(points, indices[0]);
endLabel.attr({ x: pt[0], y: pt[1] });
valueAnimation && (value = seriesModel.getRawValue(indices[0]) as ParsedValue);
}
Expand All @@ -1081,7 +1141,7 @@ class LineView extends ChartView {
// If diff <= 0, which is the range is not found(Include NaN)
// Choose the first point or last point.
const idx = (percent === 1 || animationRecord.lastFrameIndex > 0) ? indices[0] : 0;
const pt = polyline.getPointAtIndex(idx);
const pt = getPointAtIndex(points, idx);
valueAnimation && (value = seriesModel.getRawValue(idx) as ParsedValue);
endLabel.attr({ x: pt[0], y: pt[1] });
}
Expand Down
50 changes: 0 additions & 50 deletions src/chart/line/poly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,56 +260,6 @@ export class ECPolyline extends Path<ECPolylineProps> {
}
}

getLastIndexNotNull() {
const points = this.shape.points;
let len = points.length / 2;
for (; len > 0; len--) {
if (!isPointNull(points[len * 2 - 2], points[len * 2 - 1])) {
break;
}
}

return len - 1;
}

getPointAtIndex(idx: number) {
const points = this.shape.points;
return [points[idx * 2], points[idx * 2 + 1]];
}

getIndexRange(xOrY: number, dim: 'x' | 'y') {
const points = this.shape.points;
const len = points.length / 2;

const dimIdx = dim === 'x' ? 0 : 1;
let a;
let b;
let prevIndex = 0;
let nextIndex = -1;
for (let i = 0; i < len; i++) {
b = points[i * 2 + dimIdx];
if (isNaN(b) || isNaN(points[i * 2 + 1 - dimIdx])) {
continue;
}
if (i === 0) {
a = b;
continue;
}
if (a <= xOrY && b >= xOrY || a >= xOrY && b <= xOrY) {
nextIndex = i;
break;
}

prevIndex = i;
a = b;
}

return {
range: [prevIndex, nextIndex],
t: (xOrY - a) / (b - a)
};
}

getPointOn(xOrY: number, dim: 'x' | 'y'): number[] {
if (!this.path) {
this.createPathProxy();
Expand Down
8 changes: 6 additions & 2 deletions src/util/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,19 @@ function singleEnterEmphasis(el: ECElement) {
function singleLeaveEmphasis(el: ECElement) {
// Only mark the flag.
// States will be applied in the echarts.ts in next frame.
doChangeHoverState(el, 'normal', HOVER_STATE_NORMAL);
if (el.hoverState === HOVER_STATE_EMPHASIS) {
doChangeHoverState(el, 'normal', HOVER_STATE_NORMAL);
}
}

function singleEnterBlur(el: ECElement) {
doChangeHoverState(el, 'blur', HOVER_STATE_BLUR);
}

function singleLeaveBlur(el: ECElement) {
doChangeHoverState(el, 'normal', HOVER_STATE_NORMAL);
if (el.hoverState === HOVER_STATE_BLUR) {
doChangeHoverState(el, 'normal', HOVER_STATE_NORMAL);
}
}

function singleEnterSelect(el: ECElement) {
Expand Down

0 comments on commit a77894c

Please sign in to comment.