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(transform): add pack padding and direction #5964

Merged
merged 4 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
16,538 changes: 16,538 additions & 0 deletions __tests__/integration/snapshots/static/titanicPointPackSharedDataPadding.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16,538 changes: 16,538 additions & 0 deletions __tests__/integration/snapshots/static/titanicPointPackSharedRowData.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions __tests__/plots/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,5 @@ export { premierLeagueTable } from './premier-league-table';
export { singlePointBasic } from './single-point-basic';
export { populationFlowChordDefault } from './population-flow-chord-default';
export { aaplLineRrangeXY } from './aapl-line-rangeXY';
export { titanicPointPackSharedRowData } from './titanic-point-pack-shared-row-data';
export { titanicPointPackSharedDataPadding } from './titanic-point-pack-shared-data-padding';
41 changes: 41 additions & 0 deletions __tests__/plots/static/titanic-point-pack-shared-data-padding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { G2Spec } from '../../../src';

export function titanicPointPackSharedDataPadding(): G2Spec {
return {
type: 'facetRect',
data: {
type: 'fetch',
value: 'data/titanic.csv',
transform: [
{
type: 'sortBy',
fields: ['survived'],
},
{
type: 'map',
callback: ({ survived, ...d }) => ({
...d,
survived: survived + '',
}),
},
],
},
shareData: true,
encode: {
x: 'pclass',
},
children: [
{
type: 'point',
transform: [{ type: 'pack', padding: 2 }],
legend: {
color: { labelFormatter: (d) => (d === '1' ? 'Yes' : 'No') },
},
encode: {
color: 'survived',
shape: 'point',
},
},
],
};
}
41 changes: 41 additions & 0 deletions __tests__/plots/static/titanic-point-pack-shared-row-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { G2Spec } from '../../../src';

export function titanicPointPackSharedRowData(): G2Spec {
return {
type: 'facetRect',
data: {
type: 'fetch',
value: 'data/titanic.csv',
transform: [
{
type: 'sortBy',
fields: ['survived'],
},
{
type: 'map',
callback: ({ survived, ...d }) => ({
...d,
survived: survived + '',
}),
},
],
},
shareData: true,
encode: {
x: 'pclass',
},
children: [
{
type: 'point',
transform: [{ type: 'pack', direction: 'row' }],
legend: {
color: { labelFormatter: (d) => (d === '1' ? 'Yes' : 'No') },
},
encode: {
color: 'survived',
shape: 'point',
},
},
],
};
}
7 changes: 7 additions & 0 deletions site/docs/spec/transform/pack.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ facetRect

chart.render();
```

## 选项

| 属性 | 描述 | 类型 | 默认值 |
|-------------------|------------------------------------------------|---------------------|-----------------------|
| padding | 每个元素之间的间距,单位为px | `number` | `0` |
| direction | 元素的堆叠方向 | `'row' \| 'col'` | `col` |
3 changes: 2 additions & 1 deletion src/runtime/plot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ function initializeState(
// This is for unit visualization to sync data domain.
dataDomain,
modifier,
transform,
pearmini marked this conversation as resolved.
Show resolved Hide resolved
key: markKey,
} = mark;
const { index, channels, tooltip } = state;
Expand All @@ -761,7 +762,7 @@ function initializeState(
calcPoints(index, markScaleInstance, value, coordinate),
);
const count = dataDomain || I.length;
const T = modifier ? modifier(P, count, layout) : [];
const T = modifier ? modifier(P, count, layout, transform) : [];
const titleOf = (i) => tooltip.title?.[i]?.value;
const itemsOf = (i) => tooltip.items.map((V) => V[i]);
const visualData: Record<string, any>[] = I.map((d, i) => {
Expand Down
2 changes: 2 additions & 0 deletions src/spec/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ export type FlexXTransform = {

export type PackTransform = {
type?: 'pack';
padding?: number;
direction?: 'row' | 'col';
};

export type Reducer =
Expand Down
21 changes: 12 additions & 9 deletions src/transform/pack.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { deepMix } from '@antv/util';
import { TransformComponent as TC } from '../runtime';
import { calcBBox } from '../utils/vector';
import { PackTransform } from '../spec';

export type PackOptions = Record<string, unknown>;
export type PackOptions = Omit<PackTransform, 'type'>;

function modifier(P, count, layout, transform) {
const style = transform.find((item) => item.type === 'pack');
const { padding = 0, direction = 'col' } = style;

function modifier(P, count, layout) {
const pcount = P.length;
if (pcount === 0) return [];

Expand Down Expand Up @@ -37,20 +41,19 @@ function modifier(P, count, layout) {

return P.map((points, m) => {
const [x, y, width, height] = calcBBox(points);

const i = m % col;
const j = Math.floor(m / col);
const i = direction === 'col' ? m % col : Math.floor(m / row);
const j = direction === 'col' ? Math.floor(m / col) : m % row;

const newX = i * size;
const newY = (row - j - 1) * size + space;

const sx = size / width;
const sy = size / height;
const sx = (size - padding) / width;
const sy = (size - padding) / height;

// Translate the shape and mark to make sure the center of
// shape is overlap before and after scale transformation.
const tx = newX - x + offsetX * i;
const ty = newY - y - intervalY * j - offsetY;
const tx = newX - x + offsetX * i + (1 / 2) * padding;
const ty = newY - y - intervalY * j - offsetY + (1 / 2) * padding;
return `translate(${tx}, ${ty}) scale(${sx}, ${sy})`;
});
}
Expand Down
Loading