-
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.
* fix(Transform): add jitterY param * docs(Transform): add jitterY.md --------- Co-authored-by: xionghe.hx <[email protected]>
- Loading branch information
Showing
14 changed files
with
6,802 additions
and
611 deletions.
There are no files selected for viewing
6,073 changes: 6,073 additions & 0 deletions
6,073
__tests__/integration/snapshots/static/cars2PointJitterX.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,213 changes: 606 additions & 607 deletions
1,213
__tests__/integration/snapshots/static/cars2PointJitterY.svg
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import * as d3 from 'd3-random'; | ||
import { G2Spec } from '../../../src'; | ||
|
||
export function cars2PointJitterX(): G2Spec { | ||
const random = d3.randomUniform.source(d3.randomLcg(42))(0, 1); | ||
return { | ||
type: 'point', | ||
data: { | ||
type: 'fetch', | ||
value: 'data/cars2.csv', | ||
}, | ||
scale: { | ||
x: { type: 'point' }, | ||
color: { type: 'ordinal' }, | ||
}, | ||
transform: [{ type: 'sortX' }, { type: 'jitterX', random }], | ||
encode: { | ||
y: 'Horsepower', | ||
x: 'Cylinders', | ||
color: 'Cylinders', | ||
}, | ||
}; | ||
} |
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
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: jitterY | ||
order: 1 | ||
--- | ||
|
||
<embed src="@/docs/spec/transform/jitterY.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,42 @@ | ||
--- | ||
title: jitterY | ||
order: 1 | ||
--- | ||
|
||
根据离散的 y 比例尺,生成 dy 通道,实现在某个区域的 y 方向散开的效果。 | ||
|
||
## 开始使用 | ||
|
||
<img alt="jitterY" src="https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*8JbhTLgg5ucAAAAAAAAAAAAADmJ7AQ/original" width="500" /> | ||
|
||
```ts | ||
import { Chart } from '@antv/g2'; | ||
|
||
const chart = new Chart({ | ||
container: 'container', | ||
}); | ||
|
||
chart | ||
.point() | ||
.data({ | ||
type: 'fetch', | ||
value: | ||
'https://gw.alipayobjects.com/os/bmw-prod/2c813e2d-2276-40b9-a9af-cf0a0fb7e942.csv', | ||
}) | ||
.transform({ type: 'sortY' }) | ||
.transform({ type: 'jitterY' }) | ||
.encode('x', 'Horsepower') | ||
.encode('y', 'Cylinders') | ||
.encode('color', 'Cylinders') | ||
.scale('y', { type: 'point' }) | ||
.scale('color', { type: 'ordinal' }); | ||
|
||
chart.render(); | ||
``` | ||
|
||
## 选项 | ||
|
||
| 属性 | 描述 | 类型 | 默认值 | | ||
|-------------------|------------------------------------------------|---------------------|-----------------------| | ||
| padding | 每个分组之间的间距 [0 ~ 1] | `number` | `0` | | ||
| random | 随机函数,返回值为 [0, 1) | `() => number` | `Math.random` | |
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
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,30 @@ | ||
import { deepMix } from '@antv/util'; | ||
import { TransformComponent as TC } from '../runtime'; | ||
import { JitterYTransform } from '../spec'; | ||
import { column, columnOf } from './utils/helper'; | ||
import { rangeOf, interpolate } from './jitter'; | ||
|
||
export type JitterYOptions = Omit<JitterYTransform, 'type'>; | ||
|
||
/** | ||
* The JitterY transform produce dy channels for marks (especially for point) | ||
* with ordinal x and y dimension, say to make them jitter in their own space. | ||
*/ | ||
export const JitterY: TC<JitterYOptions> = (options = {}) => { | ||
const { padding = 0, random = Math.random } = options; | ||
return (I, mark) => { | ||
const { encode, scale } = mark; | ||
const { y: scaleY } = scale; | ||
const [Y] = columnOf(encode, 'y'); | ||
const rangeY = rangeOf(Y, scaleY, padding); | ||
const DY = I.map(() => interpolate(random(), ...rangeY)); | ||
return [ | ||
I, | ||
deepMix({ scale: { y: { padding: 0.5 } } }, mark, { | ||
encode: { dy: column(DY) }, | ||
}), | ||
]; | ||
}; | ||
}; | ||
|
||
JitterY.props = {}; |