Skip to content

Commit

Permalink
refactor: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
kkxxkk2019 committed May 13, 2024
1 parent 60e5c8a commit 43d9c6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 45 deletions.
11 changes: 8 additions & 3 deletions packages/vchart/src/component/marker/mark-line/base-mark-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export abstract class BaseMarkLine extends BaseMarker<IMarkLineSpec> implements
return markLine as unknown as IGroup;
}

protected _markerLayout() {
protected _getUpdateMarkerAttrs() {
const spec = this._spec;
const data = this._markerData;
const startRelativeSeries = this._startRelativeSeries;
Expand Down Expand Up @@ -150,13 +150,18 @@ export abstract class BaseMarkLine extends BaseMarker<IMarkLineSpec> implements
: markerComponentAttr.label?.text
};

this._markerComponent?.setAttributes({
return {
...pointsAttr,
label: labelAttrs as MarkLineComponent['attribute']['label'],
limitRect,
dx: this._layoutOffsetX,
dy: this._layoutOffsetY
});
};
}

protected _markerLayout() {
const updateAttrs = this._getUpdateMarkerAttrs();
this._markerComponent?.setAttributes(updateAttrs);
}

protected _initDataView(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,41 +74,12 @@ export class CartesianMarkLine extends BaseMarkLine {
}

protected _markerLayout() {
const spec = this._spec as any;
const data = this._markerData;
const startRelativeSeries = this._startRelativeSeries;
const endRelativeSeries = this._endRelativeSeries;
const relativeSeries = this._relativeSeries;

const { points } = this._computePointsAttr();

const seriesData = this._relativeSeries.getViewData().latestData;
const dataPoints =
data.latestData[0] && data.latestData[0].latestData ? data.latestData[0].latestData : data.latestData;

let limitRect;
if (spec.clip || spec.label?.confine) {
const { minX, maxX, minY, maxY } = computeClipRange([
startRelativeSeries.getRegion(),
endRelativeSeries.getRegion(),
relativeSeries.getRegion()
]);
limitRect = {
x: minX,
y: minY,
width: maxX - minX,
height: maxY - minY
};
}
const markerComponentAttr = this._markerComponent?.attribute ?? {};
const labelAttrs = {
...markerComponentAttr.label,
text: this._spec.label.formatMethod
? this._spec.label.formatMethod(dataPoints, seriesData)
: markerComponentAttr.label?.text
};
const updateAttrs = this._getUpdateMarkerAttrs();

if ((this._spec as IStepMarkLineSpec).type === 'type-step') {
const startRelativeSeries = this._startRelativeSeries;
const endRelativeSeries = this._endRelativeSeries;

const { multiSegment, mainSegmentIndex } = (this._spec as IStepMarkLineSpec).line || {};
const { connectDirection, expandDistance = 0 } = this._spec as IStepMarkLineSpec;

Expand Down Expand Up @@ -141,6 +112,7 @@ export class CartesianMarkLine extends BaseMarkLine {
} else {
expandDistanceValue = expandDistance as number;
}
const { points, label, limitRect } = updateAttrs;

const joinPoints = getInsertPoints(
(points as IPoint[])[0],
Expand All @@ -167,7 +139,7 @@ export class CartesianMarkLine extends BaseMarkLine {
refY: 0
};
}

const markerComponentAttr = this._markerComponent?.attribute ?? {};
this._markerComponent?.setAttributes({
points: multiSegment
? [
Expand All @@ -177,7 +149,7 @@ export class CartesianMarkLine extends BaseMarkLine {
]
: joinPoints,
label: {
...labelAttrs,
...label,
...labelPositionAttrs,
textStyle: {
...markerComponentAttr.label.textStyle,
Expand All @@ -192,13 +164,7 @@ export class CartesianMarkLine extends BaseMarkLine {
dy: this._layoutOffsetY
} as any);
} else {
this._markerComponent?.setAttributes({
points,
label: labelAttrs as MarkLineComponent['attribute']['label'],
limitRect,
dx: this._layoutOffsetX,
dy: this._layoutOffsetY
} as any);
this._markerComponent?.setAttributes(updateAttrs);
}
}

Expand Down

0 comments on commit 43d9c6d

Please sign in to comment.