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

fix(sankey): 修复桑基图 rawFields 在 label & tooltip 处不生效 #2756

Merged
merged 2 commits into from
Aug 4, 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
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