From a8bc0e23f80e62a48a8c218e45b8d58f2a67afc5 Mon Sep 17 00:00:00 2001 From: kkxxkk2019 Date: Thu, 11 Jul 2024 15:02:41 +0800 Subject: [PATCH] fix: mark-point position type error fix --- .../marker/mark-point/cartesian-mark-point.ts | 2 +- packages/vchart/src/component/marker/utils.ts | 67 +++++++++---------- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/packages/vchart/src/component/marker/mark-point/cartesian-mark-point.ts b/packages/vchart/src/component/marker/mark-point/cartesian-mark-point.ts index cc9c4f4fd1..d7ce90120b 100644 --- a/packages/vchart/src/component/marker/mark-point/cartesian-mark-point.ts +++ b/packages/vchart/src/component/marker/mark-point/cartesian-mark-point.ts @@ -37,7 +37,7 @@ export class CartesianMarkPoint extends BaseMarkPoint { (spec as IMarkPointCoordinateSpec).coordinatesOffset )[0]; } else if (isPositionLayout) { - point = positionLayout([spec.position], relativeSeries, spec.regionRelative)[0]; + point = positionLayout(spec.position, relativeSeries, spec.regionRelative)[0]; } return { point }; diff --git a/packages/vchart/src/component/marker/utils.ts b/packages/vchart/src/component/marker/utils.ts index 5a312cc91d..3b7429b0c8 100644 --- a/packages/vchart/src/component/marker/utils.ts +++ b/packages/vchart/src/component/marker/utils.ts @@ -389,36 +389,45 @@ export function polarCoordinateLayout(data: DataView, relativeSeries: IMarkerSup return points; } -export function positionLayout(positions: MarkerPositionPoint[], series: ISeries, regionRelative: boolean): IPoint[] { - if (isFunction(positions)) { - const userPositions = positions(series.getData().getLatestData(), series); - if (regionRelative) { - const region = series.getRegion(); - const { x: regionStartX, y: regionStartY } = region.getLayoutStartPoint(); - return userPositions.map((position: IPointLike) => { - return { - x: position.x + regionStartX, - y: position.y + regionStartY - }; - }); - } +function convertPosition(position: MarkerPositionPoint, relativeWidth: number, relativeHeight: number): IPoint { + let { x, y } = position; + if (isPercent(x)) { + x = convertPercentToValue(x, relativeWidth); + } + if (isPercent(y)) { + y = convertPercentToValue(y, relativeHeight); + } + + return { + x, + y + }; +} - return userPositions; +export function positionLayout( + positions: + | MarkerPositionPoint + | MarkerPositionPoint[] + | ((seriesData: Datum[], relativeSeries: IMarkerSupportSeries) => MarkerPositionPoint) + | ((seriesData: Datum[], relativeSeries: IMarkerSupportSeries) => MarkerPositionPoint[]), + series: IMarkerSupportSeries, + regionRelative: boolean +): IPoint[] { + let transformPositions; + if (isFunction(positions)) { + transformPositions = array(positions(series.getData().getLatestData(), series)); + } else { + transformPositions = array(positions); } if (regionRelative) { const region = series.getRegion(); const { x: regionStartX, y: regionStartY } = region.getLayoutStartPoint(); const { width: regionWidth, height: regionHeight } = region.getLayoutRect(); - return positions.map(position => { - let { x, y } = position; - if (isPercent(x)) { - x = convertPercentToValue(x, regionWidth); - } + return transformPositions.map(position => { + let { x, y } = convertPosition(position, regionWidth, regionHeight); + x = (x as number) + regionStartX; - if (isPercent(y)) { - y = convertPercentToValue(y, regionHeight); - } y = (y as number) + regionStartY; return { @@ -429,18 +438,8 @@ export function positionLayout(positions: MarkerPositionPoint[], series: ISeries } const { width: canvasWidth, height: canvasHeight } = series.getOption().getChart().getViewRect(); - return positions.map(position => { - let { x, y } = position; - if (isPercent(x)) { - x = convertPercentToValue(x, canvasWidth); - } - if (isPercent(y)) { - y = convertPercentToValue(y, canvasHeight); - } - return { - x: x as number, - y: y as number - }; + return transformPositions.map(position => { + return convertPosition(position, canvasWidth, canvasHeight); }); }