Skip to content

Commit 0912978

Browse files
author
aiyin.lzy
committed
feat: 下钻数据可设置 hierarchyConfig
1 parent 27d9b84 commit 0912978

File tree

8 files changed

+139
-31
lines changed

8 files changed

+139
-31
lines changed

__tests__/unit/plots/treemap/interactions/treemap-drill-down-spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ describe('drill-down intera', () => {
7272
const treemapPlot = new Treemap(createDiv(), {
7373
data,
7474
colorField: 'name',
75+
hierarchyConfig: {
76+
tile: 'treemapDice',
77+
},
7578
interactions: [
7679
{
7780
type: 'treemap-drill-down',
@@ -99,6 +102,8 @@ describe('drill-down intera', () => {
99102
expect(nowData.length).toBe(2);
100103
expect(nowData[0].name).toBe('东欧');
101104
expect(nowData[1].name).toBe('西欧');
105+
expect(nowData[0].y).toEqual([1, 1, 0, 0]);
106+
expect(nowData[1].y).toEqual([1, 1, 0, 0]);
102107

103108
await delay(1000);
104109

@@ -109,5 +114,8 @@ describe('drill-down intera', () => {
109114
expect(nowData1.length).toBe(2);
110115
expect(nowData1[0].name).toBe('欧洲');
111116
expect(nowData1[1].name).toBe('亚洲');
117+
118+
119+
treemapPlot.destroy();
112120
});
113121
});

__tests__/unit/plots/treemap/interactions/util-spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { noHistoryDrill, isTopParentNode } from '../../../../../src/plots/treemap/interactions/util';
1+
import { hasHistoryDrill, isTopParentNode } from '../../../../../src/plots/treemap/interactions/util';
22

33
describe('treemap interactions utl', () => {
44
it('isTopParentNode', () => {
@@ -54,12 +54,12 @@ describe('treemap interactions utl', () => {
5454
).toBeTruthy();
5555
});
5656

57-
it('noHistoryDrill', () => {
58-
expect(noHistoryDrill(undefined)).toBeFalsy();
59-
expect(noHistoryDrill({})).toBeFalsy();
57+
it('hasHistoryDrill', () => {
58+
expect(hasHistoryDrill(undefined)).toBeFalsy();
59+
expect(hasHistoryDrill({})).toBeFalsy();
6060

6161
expect(
62-
noHistoryDrill({
62+
hasHistoryDrill({
6363
getAction: () => {
6464
return {
6565
name: 'treemap-element-drill-action',
@@ -71,17 +71,17 @@ describe('treemap interactions utl', () => {
7171
};
7272
},
7373
})
74-
).toBeFalsy();
74+
).toBeTruthy();
7575

7676
expect(
77-
noHistoryDrill({
77+
hasHistoryDrill({
7878
getAction: () => {
7979
return {
8080
name: 'treemap-element-drill-action',
8181
cacheDataStack: [],
8282
};
8383
},
8484
})
85-
).toBeTruthy();
85+
).toBeFalsy();
8686
});
8787
});

__tests__/unit/plots/treemap/utils-spec.ts

+69-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { transformData, isDrillDown } from '../../../../src/plots/treemap/utils';
1+
import { transformData, isDrillDown, getFommatInteractions} from '../../../../src/plots/treemap/utils';
22

33
const data1 = {
44
name: 'root',
@@ -52,10 +52,10 @@ const data2 = {
5252
],
5353
};
5454

55-
// 自己有分类,父类有分类
56-
// 没有,父类有
57-
// 自己有,父类没有
58-
// 自己没有,父类没有
55+
// 叶节点有分类,父节点有分类
56+
// 叶节点没有,父节点有
57+
// 叶节点有,父节点没有
58+
// 叶节点没有,父节点没有
5959

6060
const data3 = {
6161
name: 'root',
@@ -110,8 +110,12 @@ describe('treemap transformData', () => {
110110
it('isDrillDown', () => {
111111
expect(isDrillDown(undefined)).toBeFalsy();
112112
expect(isDrillDown([])).toBeFalsy();
113-
expect(isDrillDown([{ type: 'asas' }])).toBeFalsy();
114-
expect(isDrillDown([{ type: 'treemap-drill-down' }])).toBeTruthy();
113+
expect(isDrillDown([{
114+
type: 'asas'
115+
}])).toBeFalsy();
116+
expect(isDrillDown([{
117+
type: 'treemap-drill-down'
118+
}])).toBeTruthy();
115119
});
116120

117121
it('transformData, basic treemap', () => {
@@ -176,6 +180,7 @@ describe('treemap transformData', () => {
176180
const data = transformData({
177181
data: data1,
178182
colorField: 'name',
183+
openDrillDown: false,
179184
hierarchyConfig: {
180185
tile: 'treemapDice',
181186
},
@@ -199,4 +204,61 @@ describe('treemap transformData', () => {
199204
});
200205
expect(data.length).toEqual(2);
201206
});
207+
208+
it('getFommatInteractions', () => {
209+
expect(getFommatInteractions(undefined, undefined)).toEqual(undefined);
210+
expect(getFommatInteractions(undefined, {
211+
tile: 'treemapSlice'
212+
})).toEqual(undefined);
213+
expect(getFommatInteractions([], {
214+
tile: 'treemapSlice'
215+
})).toEqual([]);
216+
expect(getFommatInteractions([{
217+
type: 'treemap'
218+
}], {
219+
tile: 'treemapSlice'
220+
}))
221+
.toEqual([{
222+
type: 'treemap'
223+
}]);
224+
expect(getFommatInteractions(
225+
[{
226+
type: 'treemap'
227+
},
228+
{
229+
type: 'treemap-drill-down'
230+
}
231+
], {
232+
tile: 'treemapSlice'
233+
}))
234+
.toEqual([{
235+
type: 'treemap'
236+
},
237+
{
238+
type: 'treemap-drill-down',
239+
cfg: {
240+
hierarchyConfig: {
241+
tile: 'treemapSlice'
242+
}
243+
}
244+
}]);
245+
246+
expect(getFommatInteractions(
247+
[{
248+
type: 'treemap'
249+
},
250+
{
251+
type: 'treemap-drill-down'
252+
}
253+
], undefined))
254+
.toEqual([{
255+
type: 'treemap'
256+
},
257+
{
258+
type: 'treemap-drill-down',
259+
cfg: {
260+
hierarchyConfig: undefined
261+
}
262+
}]);
263+
});
202264
});

src/plots/treemap/adaptor.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { polygon as basePolygon } from '../../adaptor/geometries/polygon';
22
import { Params } from '../../core/adaptor';
3-
import { interaction, animation, theme, legend, annotation, tooltip } from '../../adaptor/common';
3+
import { interaction as commonInteraction, animation, theme, legend, annotation, tooltip } from '../../adaptor/common';
44
import { flow, deepAssign } from '../../utils';
5-
import { transformData, isDrillDown } from './utils';
5+
import { transformData, isDrillDown, getFommatInteractions } from './utils';
66
import { TreemapOptions } from './types';
77

88
/**
@@ -94,6 +94,24 @@ function axis(params: Params<TreemapOptions>): Params<TreemapOptions> {
9494
return params;
9595
}
9696

97+
/**
98+
* Interaction 配置
99+
* @param params
100+
*/
101+
export function interaction(params: Params<TreemapOptions>): Params<TreemapOptions> {
102+
const { chart, options } = params;
103+
const { interactions, hierarchyConfig } = options;
104+
105+
commonInteraction({
106+
chart,
107+
options: {
108+
interactions: getFommatInteractions(interactions, hierarchyConfig),
109+
}
110+
})
111+
112+
return params;
113+
}
114+
97115
/**
98116
* 矩形树图
99117
* @param chart

src/plots/treemap/interactions/actions/treemap-drill-down-action.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Action } from '@antv/g2';
1+
import { Action, getInteraction } from '@antv/g2';
22
import { get, isArray } from '@antv/util';
33
import { transformData } from '../../utils';
44

@@ -11,12 +11,14 @@ export class TreemapDrillDownAction extends Action {
1111
const { view } = this.context;
1212
const currentData = view.getData();
1313
const groupScales = view.getGroupScales();
14+
const hierarchyConfig = get(view, ['interactions', 'treemap-drill-down', 'cfg', 'hierarchyConfig'], {})
1415

1516
// 重新 update 数据
1617
const drillData = transformData({
1718
data,
1819
colorField: get(groupScales, [0, 'field']),
1920
openDrillDown: true,
21+
hierarchyConfig,
2022
});
2123

2224
view.changeData(drillData);

src/plots/treemap/interactions/treemap-drill-down.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { registerInteraction, registerAction } from '@antv/g2';
22
import { TreemapDrillDownAction } from './actions/treemap-drill-down-action';
3-
import { isTopParentNode, noHistoryDrill } from './util';
3+
import { isTopParentNode, hasHistoryDrill } from './util';
44

55
registerAction('treemap-drill-down-action', TreemapDrillDownAction);
66

@@ -21,7 +21,7 @@ registerInteraction('treemap-drill-down', {
2121
{
2222
trigger: 'reset-button:click',
2323
action: ['reset-button:hide'],
24-
isEnable: noHistoryDrill,
24+
isEnable: (context) => !hasHistoryDrill(context),
2525
},
2626
],
2727
});

src/plots/treemap/interactions/util.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ import { get, isArray } from '@antv/util';
55
*/
66
export function isTopParentNode(context) {
77
const data = get(context, ['event', 'data', 'data'], {});
8-
98
return isArray(data.children) && data.children.length > 0 && data.depth === 1;
109
}
1110

1211
/**
13-
* 判断是否已无历史下钻
12+
* 判断是否仍有历史下钻
1413
*/
15-
export function noHistoryDrill(context) {
16-
if (!context || !context.getAction) return;
17-
const treemapElementDrillAction = context.getAction('treemap-element-drill-action');
14+
export function hasHistoryDrill(context) {
15+
if (!context || !context.getAction) return false;
16+
17+
const treemapElementDrillAction = context.getAction('treemap-drill-down-action');
18+
19+
if (!treemapElementDrillAction) return false;
20+
1821
// @ts-ignore
1922
const { cacheDataStack } = treemapElementDrillAction;
2023

21-
if (!isArray(cacheDataStack) || cacheDataStack.length <= 0) {
22-
return true;
23-
}
24-
return false;
24+
return isArray(cacheDataStack) && cacheDataStack.length > 0
2525
}

src/plots/treemap/utils.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
1-
import { get, isArray } from '@antv/util';
1+
import { isArray } from '@antv/util';
22
import { treemap } from '../../utils/hierarchy/treemap';
3-
3+
import { deepAssign } from '../../utils';
44
import { TreemapOptions } from './types';
55

66
export function isDrillDown(interactions: TreemapOptions['interactions']) {
77
if (!isArray(interactions)) return false;
88
return interactions.findIndex((i) => i.type === 'treemap-drill-down') > -1;
99
}
1010

11+
export function getFommatInteractions(interactions: TreemapOptions['interactions'], hierarchyConfig: TreemapOptions['hierarchyConfig']) : TreemapOptions['interactions'] {
12+
const openDrillDown = isDrillDown(interactions);
13+
if (openDrillDown) {
14+
return interactions.map(i => {
15+
if (i.type === 'treemap-drill-down') {
16+
return deepAssign({}, i, {
17+
cfg: {
18+
hierarchyConfig,
19+
}
20+
})
21+
}
22+
return i
23+
});
24+
}
25+
return interactions;
26+
}
27+
1128
interface TransformDataOptions {
1229
data: TreemapOptions['data'];
1330
colorField: TreemapOptions['colorField'];
1431
openDrillDown: boolean;
32+
hierarchyConfig?: TreemapOptions['hierarchyConfig'];
1533
}
1634

1735
export function transformData(options: TransformDataOptions) {

0 commit comments

Comments
 (0)