Skip to content

Commit

Permalink
feat: treemap layout optimize (#2337)
Browse files Browse the repository at this point in the history
* feat(treemap): add sort layout option, add default sort is desc

* test: fix ci
  • Loading branch information
hustcc authored Feb 19, 2021
1 parent 967c1e6 commit 312bcdb
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 9 deletions.
4 changes: 2 additions & 2 deletions __tests__/data/treemap.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const TREEMAP = {
name: 'root',
children: [
{ name: '分类 1', value: 560, ext: '自定义数据' },
{ name: '分类 2', value: 500, ext: '自定义数据' },
{ name: '分类 1', value: 500, ext: '自定义数据' },
{ name: '分类 2', value: 800, ext: '自定义数据' },
{ name: '分类 3', value: 150, ext: '自定义数据' },
{ name: '分类 4', value: 140, ext: '自定义数据' },
{ name: '分类 5', value: 115, ext: '自定义数据' },
Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/plots/sunburst/index-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('sunburst', () => {
sunburstPlot.chart.showTooltip({ x: bbox.maxX, y: bbox.maxY });
expect(
document.getElementById('sunburst-id-one').getElementsByClassName('g2-tooltip-list-item-value')[0].innerHTML
).toBe('0.0153');
).toBe('0.0211');
sunburstPlot.chart.hideTooltip();

sunburstPlot.destroy();
Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/plots/treemap/color-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('treemap color', () => {
});

const elements = treemapPlot.chart.geometries[0].elements;
expect(elements[0].model.color).toBe('#f00');
expect(elements[1].model.color).toBe('#f00');
expect(elements[5].model.color).toBe('#0f0');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ describe('drill-down intera', () => {
// @ts-ignore
const nowData = treemapPlot.chart.getData();
expect(nowData.length).toBe(2);
expect(nowData[0].name).toBe('东欧');
expect(nowData[1].name).toBe('西欧');
expect(nowData[0].name).toBe('西欧');
expect(nowData[1].name).toBe('东欧');
expect(nowData[0].y).toEqual([1, 1, 0, 0]);
expect(nowData[1].y).toEqual([1, 1, 0, 0]);

Expand Down
2 changes: 1 addition & 1 deletion __tests__/unit/plots/treemap/lable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('treemap basic', () => {
// @ts-ignore
expect(treemapPlot.chart.geometries[0].labelOption.cfg.position).toEqual('top');
expect(treemapPlot.chart.geometries[0].labelsContainer.getChildren()[0].cfg.children[0].attrs.text).toBe(
TREEMAP.children[0].name + TREEMAP.children[0].ext
'分类 2自定义数据'
);

// label with custom
Expand Down
30 changes: 30 additions & 0 deletions __tests__/unit/plots/treemap/sort-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Treemap } from '../../../../src';
import { createDiv } from '../../../utils/dom';
import { TREEMAP } from '../../../data/treemap';

describe('treemap sort', () => {
it('treemap sort', () => {
const treemapPlot = new Treemap(createDiv(), {
data: TREEMAP,
colorField: 'name',
});

treemapPlot.render();

// 1. 图形经过排序
// @ts-ignore
expect(treemapPlot.chart.geometries[0].elements[0].getModel().data.name).toBe('分类 2');
// @ts-ignore
expect(treemapPlot.chart.geometries[0].elements[0].getModel().data.value).toBe(800);
// @ts-ignore
expect(treemapPlot.chart.geometries[0].elements[1].getModel().data.name).toBe('分类 1');
// @ts-ignore
expect(treemapPlot.chart.geometries[0].elements[1].getModel().data.value).toBe(500);

// 2. reflect y
// @ts-ignore
expect(treemapPlot.chart.getCoordinate().isReflectY).toBe(true);

treemapPlot.destroy();
});
});
2 changes: 1 addition & 1 deletion __tests__/unit/plots/treemap/tooltip-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('treemap tooltip', () => {
expect(tooltipOption.showMarkers).toBe(false);
expect(tooltipOption.showTitle).toBe(false);
expect(tooltipOption.fields).toEqual(['name', 'value', 'name']);
expect(tooltipOption.formatter(TREEMAP.children[0])).toEqual({ name: '分类 1', value: 560 });
expect(tooltipOption.formatter(TREEMAP.children[0])).toEqual({ name: '分类 1', value: 500 });
});

it('treemap tooltip custom', () => {
Expand Down
3 changes: 3 additions & 0 deletions __tests__/unit/plots/treemap/utils-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ describe('treemap transformData', () => {
data: data2,
colorField: 'name',
openDrillDown: false,
hierarchyConfig: {
sort: () => 1,
},
});

const areaArr = data.map((dt) => {
Expand Down
3 changes: 3 additions & 0 deletions src/plots/treemap/adaptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ function geometry(params: Params<TreemapOptions>): Params<TreemapOptions> {
})
);

// 做一个反转,这样配合排序,可以将最大值放到左上角,最小值放到右下角
chart.coordinate().reflect('y');

return params;
}

Expand Down
7 changes: 6 additions & 1 deletion src/utils/hierarchy/treemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const DEFAULT_OPTIONS: HierarchyOption = {
paddingBottom: 0,
paddingLeft: 0,
as: ['x', 'y'],
// 默认降序
sort: (a, b) => b.value - a.value,
};

export function treemap(data: any, options: HierarchyOption): any[] {
Expand Down Expand Up @@ -55,7 +57,10 @@ export function treemap(data: any, options: HierarchyOption): any[] {
* ignoreParentValue 为 false 时,父元素的值由当前节点 及子元素累加而来,该值为 10 + 5 + 5 = 20
* sum 函数中,d 为用户传入的 data, children 为保留字段
*/
d3Hierarchy.hierarchy(data).sum((d) => (options.ignoreParentValue && d.children ? 0 : d[field]))
d3Hierarchy
.hierarchy(data)
.sum((d) => (options.ignoreParentValue && d.children ? 0 : d[field]))
.sort(options.sort)
);
const root = partition(data);

Expand Down
1 change: 1 addition & 0 deletions src/utils/hierarchy/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ export interface HierarchyOption {
paddingBottom?: number;
paddingLeft?: number;
as: [string, string];
sort?(a: any, b: any): number;
}

0 comments on commit 312bcdb

Please sign in to comment.