Skip to content

Commit

Permalink
feat(sunburst): 旭日图增加 activeDepth 配置,允许配置默认展示的层级深度 (#2902)
Browse files Browse the repository at this point in the history
  • Loading branch information
visiky committed Oct 9, 2021
1 parent d92443e commit b3920c9
Show file tree
Hide file tree
Showing 12 changed files with 123 additions and 12 deletions.
58 changes: 58 additions & 0 deletions __tests__/unit/plots/sunburst/drill-down-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,63 @@ describe('sunburst: drill-down', () => {
textShape1.emit('mouseenter', { target: textShape1 });
expect(textShape1.attr('cursor')).toBe('default');
expect(textShape.attr('fill')).toBe('rgba(255,0,0,1)');

drillDownAction.reset();
});

it('activeDepth when drilldown interaction', () => {
plot.update({
data: {
name: 'root',
children: [
{
name: '1',
value: 1,
children: [
{
name: '1-1',
value: 1,
children: [
{ name: '1-1-1', value: 1, children: [] },
{ name: '1-1-2', value: 1, children: [] },
],
},
{ name: '1-2', value: 1, children: [] },
],
},
],
},
});
const context = new InteractionContext(plot.chart);
const drillDownAction = new DrillDownAction(context);

// 模拟一次点击(数据是升序的)
context.event = {
type: 'click',
data: {
data: plot.chart.getData()[0],
},
};

drillDownAction.click();
expect(plot.chart.getData().length).toBe(4);
expect(plot.chart.geometries[0].elements.length).toBe(4);

plot.update({ hierarchyConfig: { activeDepth: 1 } });

// 模拟一次点击(数据是升序的)
context.event = {
type: 'click',
data: {
data: plot.chart.getData()[0],
},
};
drillDownAction.click();
expect(plot.chart.getData().length).toBe(2);
expect(plot.chart.geometries[0].elements.length).toBe(2);
});

afterAll(() => {
plot.destroy();
});
});
5 changes: 5 additions & 0 deletions __tests__/unit/plots/sunburst/index-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ describe('sunburst', () => {
expect(coordinate.radius).toBe(1);
});

it('active-depth', () => {
plot.update({ hierarchyConfig: { activeDepth: 1 } });
expect(plot.chart.geometries[0].elements.length).toBe(SIMPLE_SUNBURST_DATA.children.length);
});

it('defaultOptions 保持从 constants 中获取', () => {
expect(Sunburst.getDefaultOptions()).toEqual(DEFAULT_OPTIONS);
});
Expand Down
14 changes: 13 additions & 1 deletion __tests__/unit/utils/padding-spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getAdjustAppendPadding } from '../../../src/utils/padding';
import { getAdjustAppendPadding, resolveAllPadding } from '../../../src/utils/padding';

describe('getAdjustAppendPadding', () => {
it('default position: bottom', () => {
Expand Down Expand Up @@ -46,3 +46,15 @@ describe('getAdjustAppendPadding', () => {
expect(getAdjustAppendPadding([10, 20, 30, 40], 'top', 5)).toStrictEqual([15, 20, 30, 40]);
});
});

describe('resolveAllPadding', () => {
expect(resolveAllPadding([2, 4])).toEqual([6, 6, 6, 6]);
expect(resolveAllPadding([[1, 2, 2, 1], 4])).toEqual([5, 6, 6, 5]);
expect(resolveAllPadding([[1, 2, 2, 1], 'auto'])).toEqual([1, 2, 2, 1]);
expect(
resolveAllPadding([
[1, 2, 2, 1],
[2, 3, 3, 3],
])
).toEqual([3, 5, 5, 4]);
});
3 changes: 2 additions & 1 deletion docs/api/plots/sunburst.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ Hierarchy configuration, such as' size ', 'padding', etc., refer to [D3-Hierarch

| Properties | Type | Description |
| ----------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------ |
| field | _string_ | 数据节点权重映射字段,默认为:`value`. 当你的节点数据格式不是:`{ name: 'xx', value: 'xx' }`, 可以通过该字段来指定,详细见:图表示例 |
| field | _string_ | 数据节点权重映射字段,默认为:`value`. 当你的节点数据格式不是:`{ name: 'xx', value: 'xx' }`, 可以通过该字段来指定,详细见: [图表示例](/zh/examples/more-plots/sunburst#basic2) |
| activeDepth | _number_ | 默认展示的层级深度。默认空,代表全部展示。 取值范围为: [1, ∞),详细见:[图表示例](/zh/examples/more-plots/sunburst#active-depth) |
| padding | _number\|number[]_ | 默认:`0`。参考:[d3-hierarchy#partition_padding](https://github.com/d3/d3-hierarchy#partition_padding) |
| size | _number[]_ | 默认:`[1, 1]`。参考:[d3-hierarchy#partition_size](https://github.com/d3/d3-hierarchy#partition_size) |
| round | _boolean_ | 默认:`false`。参考:[d3-hierarchy#partition_round](https://github.com/d3/d3-hierarchy#partition_round) |
Expand Down
3 changes: 2 additions & 1 deletion docs/api/plots/sunburst.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ meta: {

| Properties | Type | Description |
| ----------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------ |
| field | _string_ | 数据节点权重映射字段,默认为:`value`. 当你的节点数据格式不是:`{ name: 'xx', value: 'xx' }`, 可以通过该字段来指定,详细见:图表示例 |
| field | _string_ | 数据节点权重映射字段,默认为:`value`. 当你的节点数据格式不是:`{ name: 'xx', value: 'xx' }`, 可以通过该字段来指定,详细见:[图表示例](/zh/examples/more-plots/sunburst#basic2) |
| activeDepth | _number_ | 默认展示的层级深度。默认空,代表全部展示。 取值范围为: [1, ∞),详细见:[图表示例](/zh/examples/more-plots/sunburst#active-depth) |
| padding | _number\|number[]_ | 默认:`0`。参考:[d3-hierarchy#partition_padding](https://github.com/d3/d3-hierarchy#partition_padding) |
| size | _number[]_ | 默认:`[1, 1]`。参考:[d3-hierarchy#partition_size](https://github.com/d3/d3-hierarchy#partition_size) |
| round | _boolean_ | 默认:`false`。参考:[d3-hierarchy#partition_round](https://github.com/d3/d3-hierarchy#partition_round) |
Expand Down
2 changes: 0 additions & 2 deletions examples/case/statistical-scenario/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"zh": "自定义分析漏斗图",
"en": "Funnel analysis"
},
"new": true,
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/3jrUoywaYN/339d1657-af7b-47d0-8434-da69979d597d.png"
},
{
Expand All @@ -35,7 +34,6 @@
"zh": "对比漏斗图",
"en": "Compare Funnel Diagram"
},
"new": true,
"screenshot": "https://gw.alipayobjects.com/mdn/rms_2274c3/afts/img/A*gS12TpN1MN8AAAAAAAAAAAAAARQnAQ"
},
{
Expand Down
4 changes: 0 additions & 4 deletions examples/component/annotation/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"zh": "文本标注",
"en": "Text annotation"
},
"new": true,
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/gAMlNVZrIU/b9cc7e10-2a1a-41f4-b217-4559b4f888d9.png"
},
{
Expand All @@ -19,7 +18,6 @@
"zh": "文本标注带包围框",
"en": "Text annotation with background"
},
"new": true,
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/DhczAubJSh/3c7d4891-1150-4b47-ae9d-26851dec3e16.png"
},
{
Expand All @@ -28,7 +26,6 @@
"zh": "辅助线标注",
"en": "Line annotation with preset-value"
},
"new": true,
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/qFApvvldG3/f67b4331-1db5-471a-8fd1-35f665a9039e.png"
},
{
Expand All @@ -45,7 +42,6 @@
"zh": "添加或删除图表标注",
"en": "Add / Remove annotations"
},
"new": true,
"screenshot": ""
},
{
Expand Down
1 change: 0 additions & 1 deletion examples/component/label/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"zh": "自定义数据标签",
"en": "Custom Label"
},
"new": true,
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/P7ARYNBdol/3988b5ab-b286-4b2c-befa-eac77afd5bac.png"
}
]
Expand Down
25 changes: 25 additions & 0 deletions examples/more-plots/sunburst/demo/active-depth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Sunburst } from '@antv/g2plot';

fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/sunburst.json')
.then((res) => res.json())
.then((data) => {
const plot = new Sunburst('container', {
data,
innerRadius: 0.3,
interactions: [{ type: 'element-active' }],
hierarchyConfig: {
field: 'sum',
// 配置展示的层级数
activeDepth: 1,
},
drilldown: {
breadCrumb: {
rootText: '起始',
},
},
label: {
autoRotate: false,
},
});
plot.render();
});
9 changes: 9 additions & 0 deletions examples/more-plots/sunburst/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@
"en": "Custom tooltip items"
},
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/yBxQkbPBXF/2a71c7de-1971-4651-bcba-dd18ddd8732e.png"
},
{
"filename": "active-depth.ts",
"title": {
"zh": "配置激活展示的层级数",
"en": "Config active display depth"
},
"new": true,
"screenshot": "https://gw.alipayobjects.com/zos/antfincdn/y9BD5CSBrF/sunburst-active-level.gif"
}
]
}
4 changes: 3 additions & 1 deletion src/plots/sunburst/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ export interface SunburstOptions extends Omit<Options, 'data' | 'legend' | 'slid
readonly hierarchyConfig?: Omit<HierarchyOption, 'as' | 'type'> & {
/** default: 'value', required data to be like: { name: 'xx', [field]: 12, children: [] } */
readonly field?: string;
/** 是否忽略 */
/** 是否忽略父节点的权重, 默认: false, 其父节点的权重不由子节点权重总和决定 */
readonly ignoreParentValue?: boolean;
/** 默认展示的层级深度. 默认空, 代表全部展示. 取值 > 0 */
readonly activeDepth?: number;
};

// 其他
Expand Down
7 changes: 6 additions & 1 deletion src/plots/sunburst/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { omit } from '@antv/util';
import { HIERARCHY_DATA_TRANSFORM_PARAMS } from '../../interactions/actions/drill-down';
import { pick } from '../../utils';
import { partition } from '../../utils/hierarchy/partition';
Expand All @@ -11,6 +12,7 @@ import { SunburstOptions } from './types';
*/
export function transformData(options: Pick<SunburstOptions, 'data' | 'colorField' | 'rawFields' | 'hierarchyConfig'>) {
const { data, colorField, rawFields, hierarchyConfig = {} } = options;
const { activeDepth } = hierarchyConfig;
const transform = {
partition: partition,
treemap: treemap,
Expand All @@ -22,7 +24,7 @@ export function transformData(options: Pick<SunburstOptions, 'data' | 'colorFiel

const nodes = transform[type](data, {
field: seriesField || 'value',
...hierarchyConfig,
...omit(hierarchyConfig, ['activeDepth']),
// @ts-ignore
type: `hierarchy.${type}`,
as: ['x', 'y'],
Expand All @@ -34,6 +36,9 @@ export function transformData(options: Pick<SunburstOptions, 'data' | 'colorFiel
if (node.depth === 0) {
return null;
}
if (activeDepth > 0 && node.depth > activeDepth) {
return null;
}

let path = node.data.name;
let ancestorNode = { ...node };
Expand Down

0 comments on commit b3920c9

Please sign in to comment.