From acc0fc0ce36bd5ed63aa793568ce55590b619882 Mon Sep 17 00:00:00 2001 From: hustcc Date: Tue, 7 May 2024 19:54:53 +0800 Subject: [PATCH] feat: add context for customize label transform (#6222) * feat: add context for customize label transform * fix: type define * chore: fix typo --- src/runtime/plot.ts | 9 +++++++-- src/runtime/types/component.ts | 12 ++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/runtime/plot.ts b/src/runtime/plot.ts index dbd12efd43..93a039d972 100644 --- a/src/runtime/plot.ts +++ b/src/runtime/plot.ts @@ -1221,15 +1221,20 @@ function plotLabel( labelDescriptor.get(d.__data__), ); const { coordinate } = view; + + const labelTransformContext = { + canvas: context.canvas, + coordinate, + }; for (const [label, shapes] of labelGroups) { const { transform = [] } = label; const transformFunction = compose(transform.map(useLabelTransform)); - transformFunction(shapes, coordinate); + transformFunction(shapes, labelTransformContext); } // Apply view-level transform. if (labelTransform) { - labelTransform(labelShapes, coordinate); + labelTransform(labelShapes, labelTransformContext); } } diff --git a/src/runtime/types/component.ts b/src/runtime/types/component.ts index 9cb5be00b2..3d54483126 100644 --- a/src/runtime/types/component.ts +++ b/src/runtime/types/component.ts @@ -1,6 +1,11 @@ import { Coordinate, Transformation } from '@antv/coord'; import EventEmitter from '@antv/event-emitter'; -import { DisplayObject, IAnimation as GAnimation, IDocument } from '@antv/g'; +import { + DisplayObject, + IAnimation as GAnimation, + IDocument, + Canvas, +} from '@antv/g'; import { G2Theme, G2ViewInstance, @@ -225,7 +230,10 @@ export type CompositionComponent> = G2BaseComponent< export type LabelTransform = ( labels: DisplayObject[], - coordinate: Coordinate, + context: { + coordinate: Coordinate; + canvas: Canvas; + }, ) => DisplayObject[]; export type LabelTransformComponent> = G2BaseComponent;