Skip to content

Commit 65277bd

Browse files
authored
feat(sankey): support null to represent loss. (#3222)
* feat(sankey): sankey support not to display node and edge which is null * chore: v2.4.17 & changelog
1 parent 20d4b8e commit 65277bd

File tree

5 files changed

+63
-23
lines changed

5 files changed

+63
-23
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#### 2.4.17 (2022-05-24)
2+
3+
##### New Features
4+
5+
* **sankey:** sankey support not to display node and edge which is null ([89a4a14b](https://github.com/antvis/G2plot/commit/89a4a14b547b406c84cbeb6fc0b5b85290b3e929))
6+
17
#### 2.4.16 (2022-04-22)
28

39
##### Chores

__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
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@antv/g2plot",
3-
"version": "2.4.16",
3+
"version": "2.4.17",
44
"description": "An interactive and responsive charting library",
55
"keywords": [
66
"chart",

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const version = '2.4.16';
1+
export const version = '2.4.17';
22

33
// G2 自定义能力透出
44
import * as G2 from '@antv/g2';

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)