Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(label): add label transform feature exceedAdjust #6209

Merged
merged 11 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,622 changes: 3,622 additions & 0 deletions __tests__/integration/snapshots/static/stocksLineLabelExceedAdjust.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions __tests__/plots/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ export { alphabetIntervalViewStyle } from './alphabet-interval-view-style';
export { vennBasic } from './venn-basic';
export { vennHollow } from './venn-hollow';
export { stocksLineVarSize } from './stocks-line-var-size';
export { stocksLineLabelExceedAdjust } from './stocks-line-label-exceed-adjust';
export { barleyLineTrail } from './barley-line-trail';
export { mockAxisX } from './mock-axisX';
export { mockAxisY } from './mock-axisY';
Expand Down
26 changes: 26 additions & 0 deletions __tests__/plots/static/stocks-line-label-exceed-adjust.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { G2Spec } from '../../../src';

export function stocksLineLabelExceedAdjust(): G2Spec {
return {
type: 'line',
data: {
type: 'fetch',
value: 'data/stocks.csv',
},
transform: [{ type: 'groupX', y: 'mean' }],
encode: {
x: (d) => new Date(d.date).getFullYear(),
y: 'price',
color: 'symbol',
},
labels: [
{
text: 'price',
transform: [{ type: 'exceedAdjust' }],
style: {
fontSize: 10,
},
},
],
};
}
2 changes: 2 additions & 0 deletions __tests__/unit/lib/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ import {
ContrastReverse,
OverlapHide,
OverlapDodgeY,
ExceedAdjust,
} from '../../../src/label-transform';

describe('corelib', () => {
Expand Down Expand Up @@ -315,6 +316,7 @@ describe('corelib', () => {
'composition.repeatMatrix': RepeatMatrix,
'composition.facetCircle': FacetCircle,
'composition.timingKeyframe': TimingKeyframe,
'labelTransform.exceedAdjust': ExceedAdjust,
'labelTransform.overlapHide': OverlapHide,
'labelTransform.overlapDodgeY': OverlapDodgeY,
'labelTransform.overflowHide': OverflowHide,
Expand Down
2 changes: 2 additions & 0 deletions __tests__/unit/lib/std.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ import {
ContrastReverse,
OverlapHide,
OverlapDodgeY,
ExceedAdjust,
} from '../../../src/label-transform';

describe('stdlib', () => {
Expand Down Expand Up @@ -343,6 +344,7 @@ describe('stdlib', () => {
'composition.timingKeyframe': TimingKeyframe,
'labelTransform.overlapHide': OverlapHide,
'labelTransform.overlapDodgeY': OverlapDodgeY,
'labelTransform.exceedAdjust': ExceedAdjust,
'labelTransform.overflowHide': OverflowHide,
'labelTransform.contrastReverse': ContrastReverse,
'composition.geoView': GeoView,
Expand Down
6 changes: 6 additions & 0 deletions site/docs/spec/label/exceedAdjust.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
title: exceedAdjust
order: 1
---

<embed src="@/docs/spec/label/exceedAdjust.zh.md"></embed>
31 changes: 31 additions & 0 deletions site/docs/spec/label/exceedAdjust.zh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: exceedAdjust
order: 1
---

`exceedAdjust` 会自动对标签做溢出检测和矫正,即当标签超出视图区域时,会对标签自动做反方向的位移。

## 开始使用

<img alt="contrastReverse" src="https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*B2GwQbqkH_kAAAAAAAAAAAAADmJ7AQ/original" width="100%" style="max-width: 800px" />

```ts
chart
.line()
.encode('x', 'letter')
.encode('y', 'frequency')
.encode('color', 'type')
/* ... */
.label({
text: 'frequency',
transform: [
{
type: 'exceedAdjust',
},
],
});
```

## 选项

* 暂无
61 changes: 61 additions & 0 deletions src/label-transform/exceedAdjust.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { DisplayObject } from '@antv/g';
import { LabelTransformComponent as LLC } from '../runtime';
import { ExceedAdjustLabel } from '../spec';
import { Bounds } from '../utils/bounds';
import { show } from '../utils/style';

const adjustPosition = (target: Bounds, edge: Bounds) => {
const [[minEdgeX, minEdgeY], [maxEdgeX, maxEdgeY]] = edge;
const [[minX, minY], [maxX, maxY]] = target;

let changeX = 0,
changeY = 0;

// x-axis
if (minX < minEdgeX) {
changeX = minEdgeX - minX;
} else if (maxX > maxEdgeX) {
changeX = maxEdgeX - maxX;
}

// y-axis
if (minY < minEdgeY) {
changeY = minEdgeY - minY;
} else if (maxY > maxEdgeY) {
changeY = maxEdgeY - maxY;
}

return [changeX, changeY];
};

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();

labels.forEach((l) => {
show(l);
const { max, min } = l.getRenderBounds();
const [xMax, yMax] = max,
[xMin, yMin] = min;
const changeValue = adjustPosition(
[
[xMin, yMin],
[xMax, yMax],
],
[
[0, 0],
[width, height],
pearmini marked this conversation as resolved.
Show resolved Hide resolved
],
);
l.style.x += changeValue[0];
l.style.y += changeValue[1];
});

return labels;
};
};
2 changes: 2 additions & 0 deletions src/label-transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ export { OverlapHide } from './overlapHide';
export { OverlapDodgeY } from './overlapDodgeY';
export { ContrastReverse } from './contrastReverse';
export { OverflowHide } from './overflowHide';
export { ExceedAdjust } from './exceedAdjust';

export type { OverlapHideOptions } from './overlapHide';
export type { OverlapDodgeYOptions } from './overlapDodgeY';
export type { ContrastReverseOptions } from './contrastReverse';
export type { OverflowHideOptions } from './overflowHide';
export type { ExceedAdjustOptions } from './exceedAdjust';
2 changes: 2 additions & 0 deletions src/lib/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ import {
OverflowHide,
ContrastReverse,
OverlapHide,
ExceedAdjust,
} from '../label-transform';

export function corelib() {
Expand Down Expand Up @@ -317,5 +318,6 @@ export function corelib() {
'labelTransform.overlapDodgeY': OverlapDodgeY,
'labelTransform.overflowHide': OverflowHide,
'labelTransform.contrastReverse': ContrastReverse,
'labelTransform.exceedAdjust': ExceedAdjust,
} as const;
}
4 changes: 4 additions & 0 deletions src/spec/labelTransform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ export type ContrastReverseLabelTransform = {
export type OverflowHideLabelTransform = {
type: 'overflowHide';
};

export type ExceedAdjustLabel = {
type: 'exceedAdjust';
};
Loading