-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
3,762 additions
and
0 deletions.
There are no files selected for viewing
3,622 changes: 3,622 additions & 0 deletions
3,622
__tests__/integration/snapshots/static/stocksLineLabelExceedAdjust.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
], | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}); | ||
``` | ||
|
||
## 选项 | ||
|
||
* 暂无 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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[], { coordinate, canvas }) => { | ||
console.log(coordinate.getOptions()); | ||
const { width, height } = canvas.getConfig(); | ||
const x = 0, | ||
y = 0; | ||
|
||
labels.forEach((l) => { | ||
show(l); | ||
const { max, min } = l.getRenderBounds(); | ||
const [xMax, yMax] = max, | ||
[xMin, yMin] = min; | ||
const changeValue = adjustPosition( | ||
[ | ||
[xMin, yMin], | ||
[xMax, yMax], | ||
], | ||
[ | ||
[x, y], | ||
[x + width, y + height], | ||
], | ||
); | ||
l.style.x += changeValue[0]; | ||
l.style.y += changeValue[1]; | ||
}); | ||
|
||
return labels; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters