Skip to content

Commit

Permalink
fix(sankey): 修复桑基图 rawFields 在 label & tooltip 处不生效 (#2756)
Browse files Browse the repository at this point in the history
* fix(sankey): 修复桑基图 rawFields 在 label & tooltip 处不生效

* fix: 修改 lint 问题
  • Loading branch information
visiky authored Aug 4, 2021
1 parent e1c5974 commit 669f881
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
52 changes: 52 additions & 0 deletions __tests__/bugs/issue-2755-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Sankey } from '../../src';
import { createDiv, removeDom } from '../utils/dom';
import { PARALLEL_SET } from '../data/parallel-set';

describe('sankey', () => {
const data = [];
const keys = ['Survived', 'Sex', 'Age', 'Class'];
PARALLEL_SET.forEach((d) => {
keys.reduce((a, b) => {
if (a && b) {
data.push({
source: d[a],
target: d[b],
value: d.value,
path: `${d[keys[0]]} -> ${d[keys[1]]} -> ${d[keys[2]]} -> ${d[keys[3]]}`,
});
}
return b;
});
});

const dom = createDiv();
const sankey = new Sankey(dom, {
data: data.map((d) => ({ ...d, append: 'hello' })),
sourceField: 'source',
targetField: 'target',
weightField: 'value',
rawFields: ['append'],
nodeWidthRatio: 0.01,
nodePaddingRatio: 0.03,
nodeDraggable: true,
});

sankey.render();

it('label', () => {
sankey.update({ label: { formatter: () => 'HELLO' } });
expect(sankey.chart.views[1].geometries[0].labelsContainer.getChildByIndex(0).cfg.children[0].attr('text')).toBe(
'HELLO'
);
// with rawFields
sankey.update({ label: { formatter: ({ append }) => append } });
expect(sankey.chart.views[1].geometries[0].labelsContainer.getChildByIndex(0).cfg.children[0].attr('text')).toBe(
'hello'
);
});

afterAll(() => {
sankey.destroy();
removeDom(dom);
});
});
30 changes: 28 additions & 2 deletions src/plots/sankey/adaptor.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,35 @@
import { uniq } from '@antv/util';
import { interaction, theme } from '../../adaptor/common';
import { Params } from '../../core/adaptor';
import { flow } from '../../utils';
import { deepAssign, flow } from '../../utils';
import { polygon, edge } from '../../adaptor/geometries';
import { transformToViewsData } from './helper';
import { SankeyOptions } from './types';
import { X_FIELD, Y_FIELD, COLOR_FIELD, EDGES_VIEW_ID, NODES_VIEW_ID } from './constant';
import { transformToViewsData } from './helper';

/**
* 默认配置项 处理
* @param params
*/
function defaultOptions(params: Params<SankeyOptions>): Params<SankeyOptions> {
const { options } = params;
const { rawFields = [] } = options;

return deepAssign(
{},
{
options: {
tooltip: {
fields: uniq(['name', 'source', 'target', 'value', 'isNode', ...rawFields]),
},
label: {
fields: uniq(['x', 'name', ...rawFields]),
},
},
},
params
);
}

/**
* geometry 处理
Expand Down Expand Up @@ -135,6 +160,7 @@ export function nodeDraggable(params: Params<SankeyOptions>): Params<SankeyOptio
export function adaptor(params: Params<SankeyOptions>) {
// flow 的方式处理所有的配置到 G2 API
return flow(
defaultOptions,
geometry,
interaction,
nodeDraggable,
Expand Down
6 changes: 2 additions & 4 deletions src/plots/sankey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ export class Sankey extends Plot<SankeyOptions> {
lineWidth: 0,
},
label: {
fields: ['x', 'name'],
callback: (x: number[], name: string) => {
formatter: ({ name }) => name,
callback: (x: number[]) => {
const isLast = x[1] === 1; // 最后一列靠边的节点
return {
style: {
fill: '#545454',
textAlign: isLast ? 'end' : 'start',
},
offsetX: isLast ? -8 : 8,
content: name,
};
},
layout: [
Expand All @@ -55,7 +54,6 @@ export class Sankey extends Plot<SankeyOptions> {
showTitle: false,
showMarkers: false,
shared: false,
fields: ['name', 'source', 'target', 'value', 'isNode'],
// 内置:node 不显示 tooltip,edge 显示 tooltip
showContent: (items) => {
return !get(items, [0, 'data', 'isNode']);
Expand Down

0 comments on commit 669f881

Please sign in to comment.