Skip to content

Commit 89a4a14

Browse files
committed
feat(sankey): sankey support not to display node and edge which is null
1 parent 20d4b8e commit 89a4a14

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

__tests__/unit/plots/sankey/index-spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,25 @@ describe('sankey', () => {
202202
sankey.destroy();
203203
removeDom(dom);
204204
});
205+
206+
it('new Sankey({...}) supports not to display null.', () => {
207+
const DATA = [
208+
{ source: 'a', target: null, value: 9 },
209+
{ source: 'a', target: 'b', value: 40 },
210+
];
211+
212+
const sankey = new Sankey(createDiv(), {
213+
data: DATA,
214+
sourceField: 'source',
215+
targetField: 'target',
216+
weightField: 'value',
217+
});
218+
219+
sankey.render();
220+
221+
expect(sankey.chart.views[0].geometries[0].elements.length).toBe(1);
222+
expect(sankey.chart.views[1].geometries[0].elements.length).toBe(2);
223+
224+
sankey.destroy();
225+
});
205226
});

src/plots/sankey/layout.ts

+34-21
Original file line numberDiff line numberDiff line change
@@ -150,25 +150,38 @@ export function sankeyLayout(
150150
const layoutData: SankeyLayoutOutputData = sankeyProcessor(data);
151151

152152
// post process (x, y), etc.
153-
layoutData.nodes.forEach((node) => {
154-
const { x0, x1, y0, y1 } = node;
155-
/* points
156-
* 3---2
157-
* | |
158-
* 0---1
159-
*/
160-
node.x = [x0, x1, x1, x0];
161-
node.y = [y0, y0, y1, y1];
162-
});
163-
164-
layoutData.links.forEach((edge) => {
165-
const { source, target } = edge;
166-
const sx = source.x1;
167-
const tx = target.x0;
168-
edge.x = [sx, sx, tx, tx];
169-
const offset = edge.width / 2;
170-
edge.y = [edge.y0 + offset, edge.y0 - offset, edge.y1 + offset, edge.y1 - offset];
171-
});
172-
173-
return layoutData;
153+
const nodes = layoutData.nodes
154+
.map((node) => {
155+
const { x0, x1, y0, y1 } = node;
156+
/* points
157+
* 3---2
158+
* | |
159+
* 0---1
160+
*/
161+
node.x = [x0, x1, x1, x0];
162+
node.y = [y0, y0, y1, y1];
163+
164+
return node;
165+
})
166+
.filter((node) => {
167+
return node.name !== null;
168+
});
169+
170+
const links = layoutData.links
171+
.map((edge) => {
172+
const { source, target } = edge;
173+
const sx = source.x1;
174+
const tx = target.x0;
175+
edge.x = [sx, sx, tx, tx];
176+
const offset = edge.width / 2;
177+
edge.y = [edge.y0 + offset, edge.y0 - offset, edge.y1 + offset, edge.y1 - offset];
178+
179+
return edge;
180+
})
181+
.filter((edge) => {
182+
const { source, target } = edge;
183+
return source.name !== null && target.name !== null;
184+
});
185+
186+
return { nodes, links };
174187
}

0 commit comments

Comments
 (0)