@@ -4,7 +4,8 @@ import { toggleLoader } from "../utils";
4
4
5
5
let SIZE_CRITERIA = "frequency" ,
6
6
FOLDING_CRITERIA = "frequency" ,
7
- MAX_CHILDREN = 20 ;
7
+ MAX_CHILDREN = 20 ,
8
+ MIN_RADIUS = 5 ;
8
9
9
10
function fold ( tree ) {
10
11
if ( ! tree . children || tree . children . length <= MAX_CHILDREN )
@@ -25,8 +26,17 @@ function fold (tree) {
25
26
return tree ;
26
27
}
27
28
29
+ /**
30
+ * Converts flat input data into a tree using implicit parent-children relationships. This
31
+ * function also sorts the children data by relevance.
32
+ * @param graph
33
+ * @param parent
34
+ * @returns {* }
35
+ */
28
36
function toTree ( graph , parent ) {
37
+
29
38
parent . children = [ ] ;
39
+
30
40
for ( let edge of graph . edges ) {
31
41
if ( edge . source !== parent . id )
32
42
continue ;
@@ -42,12 +52,29 @@ function toTree (graph, parent) {
42
52
parent . children . push ( toTree ( graph , t ) ) ;
43
53
}
44
54
}
45
- parent . children . sort ( ( a , b ) => a [ FOLDING_CRITERIA ] > b [ FOLDING_CRITERIA ] ? 1 : - 1 ) ;
55
+
56
+ try {
57
+ parent . children . sort (
58
+ ( a , b ) => a . entities [ 0 ] [ FOLDING_CRITERIA ] > b . entities [ 0 ] [ FOLDING_CRITERIA ] ? - 1 : 1
59
+ ) ;
60
+ } catch ( e ) {
61
+ console . error ( `Error! Most likely, one of the graph nodes does not has any entities.`
62
+ + ` The folding criteria displayed on the diagram would be random.` ) ;
63
+ }
64
+
46
65
return parent ;
66
+
47
67
}
48
68
69
+ /**
70
+ * This function converts graph model data to internal application's format.
71
+ * @param graph
72
+ * @returns {{graph: *, tree: *, foldedTree: *} }
73
+ */
49
74
function preprocess ( graph ) {
75
+
50
76
let zeroID = null ;
77
+
51
78
graph . nodes . forEach ( node => { if ( ! zeroID && node . id === 0 ) zeroID = node ; } ) ;
52
79
if ( ! zeroID ) {
53
80
graph . nodes . unshift ( zeroID = {
@@ -56,23 +83,24 @@ function preprocess (graph) {
56
83
type : "entity" ,
57
84
entities : [ {
58
85
id : - 1 ,
59
- value : getOption ( "seed" )
86
+ value : getOption ( "seed" ) ,
87
+ [ SIZE_CRITERIA ] : 10000
60
88
} ]
61
89
} ) ;
62
90
}
63
91
graph . nodes . forEach ( node =>
64
- node . radius = 5 + Math . sqrt ( node . entities [ 0 ] [ SIZE_CRITERIA ] / 4 || 25 ) ) ;
65
- console . log ( graph ) ;
66
- // fold(graph, zeroID);
92
+ node . radius = MIN_RADIUS + Math . sqrt ( node . entities [ 0 ] [ SIZE_CRITERIA ] / 4 || 25 ) ) ;
93
+ console . log ( `Graph:` , graph ) ;
67
94
let tree = toTree ( graph , zeroID ) ,
68
95
foldedTree = fold ( tree ) ;
69
- console . log ( tree ) ;
96
+ console . log ( `Tree:` , tree ) ;
70
97
71
98
return {
72
99
graph : graph ,
73
100
tree : tree ,
74
101
foldedTree : foldedTree
75
102
} ;
103
+
76
104
}
77
105
78
106
let graph = preprocess ( sampleData . graph ) ;
0 commit comments