Skip to content

Commit

Permalink
feat: add context for customize label transform (#6222)
Browse files Browse the repository at this point in the history
* feat: add context for customize label transform

* fix: type define

* chore: fix typo
  • Loading branch information
hustcc authored May 7, 2024
1 parent 8ecb185 commit acc0fc0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/runtime/plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/runtime/types/component.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -225,7 +230,10 @@ export type CompositionComponent<O = Record<string, unknown>> = G2BaseComponent<

export type LabelTransform = (
labels: DisplayObject[],
coordinate: Coordinate,
context: {
coordinate: Coordinate;
canvas: Canvas;
},
) => DisplayObject[];
export type LabelTransformComponent<O = Record<string, unknown>> =
G2BaseComponent<LabelTransform, O>;

0 comments on commit acc0fc0

Please sign in to comment.