Skip to content

Commit

Permalink
fix: prevent label overlap in multiple charts
Browse files Browse the repository at this point in the history
  • Loading branch information
interstellarmt committed Oct 12, 2024
1 parent 06266eb commit df6e64c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/label-transform/exceedAdjust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ export type ExceedAdjustOptions = Omit<ExceedAdjustLabel, 'type'>;
* adjust the label when exceed the plot
*/
export const ExceedAdjust: LLC<ExceedAdjustOptions> = () => {
return (labels: DisplayObject[], { canvas }) => {
const { width, height } = canvas.getConfig();

return (labels: DisplayObject[], { canvas, layout }) => {
labels.forEach((l) => {
show(l);
const { max, min } = l.getRenderBounds();
Expand All @@ -47,9 +45,10 @@ export const ExceedAdjust: LLC<ExceedAdjustOptions> = () => {
[xMin, yMin],
[xMax, yMax],
],
// Prevent label overlap in multiple charts by calculating layouts separately to avoid collisions.
[
[0, 0],
[width, height],
[layout.x, layout.y],
[layout.x + layout.width, layout.y + layout.height],
],
);
// For label with connectorPoints
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1229,11 +1229,12 @@ function plotLabel(
const labelGroups = group(labelShapes, (d) =>
labelDescriptor.get(d.__data__),
);
const { coordinate } = view;
const { coordinate, layout } = view;

const labelTransformContext = {
canvas: context.canvas,
coordinate,
layout,
};
for (const [label, shapes] of labelGroups) {
const { transform = [] } = label;
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/types/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Vector2,
G2MarkState,
GuideComponentPlane,
Layout,
} from './common';
import { DataComponent } from './data';
import { Encode, EncodeComponent } from './encode';
Expand Down Expand Up @@ -233,6 +234,7 @@ export type LabelTransform = (
context: {
coordinate: Coordinate;
canvas: Canvas;
layout: Layout;
},
) => DisplayObject[];
export type LabelTransformComponent<O = Record<string, unknown>> =
Expand Down

0 comments on commit df6e64c

Please sign in to comment.