@@ -12,7 +12,7 @@ import {
1212 pickWithout ,
1313 toValidNumber
1414} from '@visactor/vutils' ;
15- import { makeHierarchicNodes } from './hierarchy' ;
15+ import { computeHierarchicNodeLinks , computeNodeValues , makeHierarchicNodes } from './hierarchy' ;
1616import type {
1717 SankeyData ,
1818 SankeyOptions ,
@@ -243,46 +243,7 @@ export class SankeyLayout {
243243 }
244244
245245 computeHierarchicNodeLinks ( originalNodes : HierarchyNodeDatum [ ] ) {
246- const nodes : SankeyNodeElement [ ] = [ ] ;
247- const links : SankeyLinkElement [ ] = [ ] ;
248- const nodeMap : Record < string | number , SankeyNodeElement > = { } ;
249- const linkMap : Record < string | number , SankeyLinkElement > = { } ;
250- const originalLinks : ( SankeyLinkDatum & { parents ?: SankeyNodeElement [ ] } ) [ ] = [ ] ;
251-
252- makeHierarchicNodes ( originalNodes , this . _getNodeKey , nodes , nodeMap , originalLinks ) ;
253-
254- originalLinks . forEach ( ( link , index ) => {
255- const key = `${ link . source } -${ link . target } ` ;
256- const linkDatum = pickWithout ( link , [ 'parents' ] ) ;
257-
258- ( linkDatum as any ) . parents = link . parents . map ( node => {
259- return pickWithout ( node , [ 'sourceLinks' , 'targetLinks' ] ) ;
260- } ) ;
261-
262- if ( linkMap [ key ] ) {
263- linkMap [ key ] . value += toValidNumber ( link . value ) ;
264-
265- ( linkMap [ key ] . datum as SankeyLinkDatum [ ] ) . push ( linkDatum as SankeyLinkDatum ) ;
266-
267- return ;
268- }
269- const linkElement = {
270- index,
271- key : `${ link . source } -${ link . target } ` ,
272- source : link . source ,
273- target : link . target ,
274- datum : [ linkDatum ] as any ,
275- value : link . value ,
276- parents : link . parents . map ( parent => parent . key )
277- } ;
278-
279- links . push ( linkElement ) ;
280- nodeMap [ link . source ] . sourceLinks . push ( linkElement ) ;
281- nodeMap [ link . target ] . targetLinks . push ( linkElement ) ;
282- linkMap [ key ] = linkElement ;
283- } ) ;
284-
285- return { nodes, links, nodeMap } ;
246+ return computeHierarchicNodeLinks ( originalNodes , this . _getNodeKey ) ;
286247 }
287248
288249 computeSourceTargetNodeLinks ( data : { nodes ?: SankeyNodeDatum [ ] ; links : SankeyLinkDatum [ ] } ) {
@@ -413,19 +374,7 @@ export class SankeyLayout {
413374 }
414375
415376 computeNodeValues ( nodes : SankeyNodeElement [ ] ) {
416- for ( let i = 0 , len = nodes . length ; i < len ; i ++ ) {
417- const node = nodes [ i ] ;
418-
419- node . value = Math . max (
420- isNil ( node . value ) ? 0 : toValidNumber ( node . value ) ,
421- node . sourceLinks . reduce ( ( sum , link : SankeyLinkElement ) => {
422- return sum + ( toValidNumber ( link . value ) ?? 0 ) ;
423- } , 0 ) ,
424- node . targetLinks . reduce ( ( sum , link : SankeyLinkElement ) => {
425- return sum + ( toValidNumber ( link . value ) ?? 0 ) ;
426- } , 0 )
427- ) ;
428- }
377+ return computeNodeValues ( nodes ) ;
429378 }
430379
431380 computeNodeDepths ( nodes : SankeyNodeElement [ ] ) {
0 commit comments