-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sankey): update d3-sankey layout for depth customize (#2378)
* feat(depth): update di-sankey layout for depth customize * chore: remove unused * test: fix ci * chore: for cr * chore: update ci * chore: remove unused action
- Loading branch information
Showing
14 changed files
with
776 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
name: build | ||
|
||
on: ["push", "pull_request"] | ||
on: ["pull_request"] | ||
|
||
jobs: | ||
build: | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Sankey } from '../../../../src'; | ||
import { createDiv } from '../../../utils/dom'; | ||
|
||
describe('sankey', () => { | ||
it('nodeDepth', () => { | ||
const ALIPAY_DATA = [ | ||
{ source: 'A', target: 'B', value: 160 }, | ||
{ source: 'B', target: 'C', value: 10 }, | ||
{ source: 'C', target: 'D', value: 8 }, | ||
{ source: 'E', target: 'D', value: 27 }, | ||
]; | ||
|
||
const sankey = new Sankey(createDiv(), { | ||
height: 500, | ||
data: ALIPAY_DATA, | ||
sourceField: 'source', | ||
targetField: 'target', | ||
weightField: 'value', | ||
nodeDepth: (node) => { | ||
switch (node.name) { | ||
case 'A': | ||
return 1; | ||
case 'B': | ||
return 0; | ||
case 'C': | ||
return 2; | ||
case 'D': | ||
return 2; | ||
// case 'E': | ||
// return 2; | ||
} | ||
return node.depth; | ||
}, | ||
}); | ||
|
||
sankey.render(); | ||
|
||
const elements = sankey.chart.views[1].geometries[0].elements; | ||
// a 在 b 的右侧 | ||
// @ts-ignore | ||
expect(elements[0].data.x[0]).toBeGreaterThan(elements[1].data.x[0]); | ||
|
||
// a === e | ||
// @ts-ignore | ||
expect(elements[4].data.x[0]).toBe(elements[1].data.x[0]); | ||
|
||
// c === d | ||
// @ts-ignore | ||
expect(elements[2].data.x[0]).toBe(elements[3].data.x[0]); | ||
|
||
sankey.destroy(); | ||
}); | ||
}); |
16 changes: 8 additions & 8 deletions
16
...sts__/unit/utils/transform/sankey-spec.ts → __tests__/unit/plots/sankey/layout-spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { M, randomFloat } from 'miz'; | ||
import { sankey } from '../../../../../src/plots/sankey/sankey'; | ||
import { cutoffCircle } from '../../../../../src/plots/sankey/circle'; | ||
import { transformDataToNodeLinkData } from '../../../../../src/utils/data'; | ||
|
||
const C = [ | ||
'A', | ||
'B', | ||
'C', | ||
'D', | ||
'E', | ||
'F', | ||
'G', | ||
'H', | ||
'I', | ||
'J', | ||
'K', | ||
'L', | ||
'M', | ||
'N', | ||
'O', | ||
'P', | ||
'K', | ||
'R', | ||
'S', | ||
'T', | ||
'U', | ||
'V', | ||
'W', | ||
'Z', | ||
'Y', | ||
'Z', | ||
]; | ||
|
||
describe('sankey', () => { | ||
it('monkey for deep', () => { | ||
for (let i = 0; i < 100; i++) { | ||
const layout = sankey() | ||
.nodeWidth(0.008) | ||
// @ts-ignore | ||
.nodePadding(0.02) | ||
.nodeAlign((_, maxDepth) => randomFloat(0, maxDepth, 0)) | ||
.extent([ | ||
[0, 0], | ||
[1, 1], | ||
]); | ||
|
||
const data = M.arrayOf( | ||
M.shape({ | ||
source: M.oneOf(C), | ||
target: M.oneOf(C), | ||
value: M.number(1, 10), | ||
}), | ||
10, | ||
50 | ||
).mock(); | ||
|
||
const sankeyLayoutInputData = transformDataToNodeLinkData( | ||
cutoffCircle(data, 'source', 'target'), | ||
'source', | ||
'target', | ||
'value' | ||
); | ||
|
||
// 不报错即可 | ||
expect(() => { | ||
layout(sankeyLayoutInputData); | ||
}).not.toThrow(); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.