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(sunburst): 旭日图增加 ignoreParentValue 配置 #2637

Merged
merged 1 commit into from
Jun 15, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions docs/api/plots/sunburst.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Hierarchy configuration, such as' size ', 'padding', etc., refer to [D3-Hierarch
| 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) |
| sort | _Function_ | 数据节点排序方式,默认:降序。参考: [d3-hierarchy#node_sort](https://github.com/d3/d3-hierarchy#node_sort) |
| ignoreParentValue | _boolean_ | 是否忽略 parentValue, 默认:true。 当设置为 true 时,父节点的权重由子元素决定 |

#### radius

Expand Down
1 change: 1 addition & 0 deletions docs/api/plots/sunburst.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ meta: {
| 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) |
| sort | _Function_ | 数据节点排序方式,默认:降序。参考: [d3-hierarchy#node_sort](https://github.com/d3/d3-hierarchy#node_sort) |
| ignoreParentValue | _boolean_ | 是否忽略 parentValue, 默认:true。 当设置为 true 时,父节点的权重由子元素决定 |

#### radius

Expand Down
6 changes: 5 additions & 1 deletion examples/more-plots/sunburst/demo/tooltip-fields.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Sunburst } from '@antv/g2plot';

fetch('https://gw.alipayobjects.com/os/antfincdn/ryp44nvUYZ/coffee.json')
fetch('https://gw.alipayobjects.com/os/antfincdn/%24ixDFx9%248M/coffee-data.json')
.then((data) => data.json())
.then((data) => {
const plot = new Sunburst('container', {
Expand All @@ -13,6 +13,10 @@ fetch('https://gw.alipayobjects.com/os/antfincdn/ryp44nvUYZ/coffee.json')
alias: '国家',
},
},
hierarchyConfig: {
// the weight of parent node depends on itself
ignoreParentValue: false,
},
tooltip: {
fields: ['path', 'symbol', 'value'],
formatter: (datum) => ({
Expand Down
4 changes: 3 additions & 1 deletion src/plots/sunburst/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ 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: [] } */
field?: string;
readonly field?: string;
/** 是否忽略 */
readonly ignoreParentValue?: boolean;
};

// 其他
Expand Down
14 changes: 12 additions & 2 deletions src/utils/hierarchy/partition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as d3Hierarchy from 'd3-hierarchy';
import { assign, isArray, reduce } from '@antv/util';
import { assign, isArray, reduce, size } from '@antv/util';
import { getField, getAllNodes } from './util';

const DEFAULT_OPTIONS: Options = {
Expand All @@ -10,6 +10,8 @@ const DEFAULT_OPTIONS: Options = {
// 默认降序
sort: (a, b) => b.value - a.value,
as: ['x', 'y'],
// 是否忽略 parentValue, 当设置为 true 时,父节点的权重由子元素决定
ignoreParentValue: true,
};

export interface Options {
Expand All @@ -20,6 +22,8 @@ export interface Options {
padding?: number;
sort?: Function;
as?: [string, string];

ignoreParentValue?: boolean;
}

export function partition(data: any, options: Options): any[] {
Expand Down Expand Up @@ -47,7 +51,13 @@ export function partition(data: any, options: Options): any[] {
*/
d3Hierarchy
.hierarchy(data)
.sum((d) => (d.children ? d[field] - reduce(d.children, (a, b) => a + b[field], 0) : d[field]))
.sum((d) =>
size(d.children)
? options.ignoreParentValue
? 0
: d[field] - reduce(d.children, (a, b) => a + b[field], 0)
: d[field]
)
.sort(options.sort)
);
const root = partition(data);
Expand Down