1
1
import NetJSONGraphDefaultConfig from "./netjsongraph.config" ;
2
2
import NetJSONGraphUpdate from "./netjsongraph.update" ;
3
3
4
+ class Selection {
5
+ constructor ( ) {
6
+ this . selected = new Set ( ) ;
7
+ }
8
+
9
+ getSetId ( item ) {
10
+ if ( item . node ) {
11
+ // map node
12
+ return item . node . id ;
13
+ } else if ( item . link ) {
14
+ // map link
15
+ return ( item . link . source + "=>" + item . link . target ) ;
16
+ } else if ( item . id ) {
17
+ // graph node
18
+ return item . id ;
19
+ } else {
20
+ // graph link
21
+ return ( item . source + "=>" + item . target ) ;
22
+ }
23
+ }
24
+
25
+ isSelected ( item ) {
26
+ let id = this . getSetId ( item ) ;
27
+ return this . selected . has ( id ) ;
28
+ }
29
+
30
+ toggleSelection ( item ) {
31
+ let id = this . getSetId ( item ) ;
32
+
33
+ if ( this . selected . has ( id ) ) {
34
+ this . selected . delete ( id )
35
+ return false ;
36
+ } else {
37
+ this . selected . add ( id ) ;
38
+ return true ;
39
+ }
40
+ }
41
+
42
+ changeSelection ( echarts , params ) {
43
+ const multiSelectKey = ( window . event . ctrlKey || window . event . metaKey ) ;
44
+ const isSelectionEmpty = ( this . selected . size === 0 ) ;
45
+
46
+ if ( multiSelectKey ) {
47
+ if ( this . toggleSelection ( params . data ) ) {
48
+ echarts . dispatchAction (
49
+ { type : 'highlight' , seriesIndex : params . seriesIndex , dataType : params . dataType , dataIndex : params . dataIndex }
50
+ )
51
+ } else {
52
+ echarts . dispatchAction (
53
+ { type : 'downplay' , seriesIndex : params . seriesIndex , dataType : params . dataType , dataIndex : params . dataIndex }
54
+ )
55
+ }
56
+ } else if ( ! isSelectionEmpty ) {
57
+ const option = echarts . getOption ( ) ;
58
+ let nodeIndexes = [ ] ;
59
+ let linkIndexes = [ ] ;
60
+ if ( option . leaflet ) {
61
+ // map
62
+ const nodeData = option . leaflet [ 0 ] . mapOptions . nodeConfig . data ;
63
+ const linkData = option . leaflet [ 0 ] . mapOptions . linkConfig . data ;
64
+ nodeIndexes = nodeData . map ( ( node , index ) => index ) ;
65
+ linkIndexes = linkData . map ( ( link , index ) => index ) ;
66
+ } else {
67
+ // graph
68
+ const nodeData = option . series [ 0 ] . nodes ;
69
+ const linkData = option . series [ 0 ] . links ;
70
+ nodeIndexes = nodeData . map ( ( node , index ) => index ) ;
71
+ linkIndexes = linkData . map ( ( link , index ) => index ) ;
72
+ }
73
+
74
+ // downplay all items
75
+ echarts . dispatchAction (
76
+ { type : 'downplay' , seriesIndex : 0 , batch : [
77
+ { dataType : "node" , dataIndex : nodeIndexes } ,
78
+ { dataType : "edge" , dataIndex : linkIndexes } ,
79
+ ]
80
+ }
81
+ )
82
+
83
+ this . selected . clear ( ) ;
84
+ }
85
+ }
86
+
87
+ // called when switching between graph/map view
88
+ highlightSelected ( echarts ) {
89
+ const option = echarts . getOption ( ) ;
90
+
91
+ let nodeIndexes = [ ] ;
92
+ let linkIndexes = [ ] ;
93
+ if ( option . leaflet ) {
94
+ // map
95
+ const nodeData = option . leaflet [ 0 ] . mapOptions . nodeConfig . data ;
96
+ const linkData = option . leaflet [ 0 ] . mapOptions . linkConfig . data ;
97
+ nodeIndexes = nodeData . map ( ( node , index ) => this . isSelected ( node ) ? index : - 1 ) ;
98
+ linkIndexes = linkData . map ( ( link , index ) => this . isSelected ( link ) ? index : - 1 ) ;
99
+ } else {
100
+ // graph
101
+ const nodeData = option . series [ 0 ] . nodes ;
102
+ const linkData = option . series [ 0 ] . links ;
103
+ nodeIndexes = nodeData . map ( ( node , index ) => this . isSelected ( node ) ? index : - 1 ) ;
104
+ linkIndexes = linkData . map ( ( link , index ) => this . isSelected ( link ) ? index : - 1 ) ;
105
+ }
106
+
107
+ echarts . dispatchAction (
108
+ { type : 'highlight' , seriesIndex : 0 , batch : [
109
+ { dataType : "node" , dataIndex : nodeIndexes } ,
110
+ { dataType : "edge" , dataIndex : linkIndexes } ,
111
+ ]
112
+ }
113
+ )
114
+ }
115
+ }
116
+
4
117
class NetJSONGraph {
5
118
/**
6
119
* @constructor
@@ -10,6 +123,7 @@ class NetJSONGraph {
10
123
*/
11
124
constructor ( JSONParam ) {
12
125
this . utils = new NetJSONGraphUpdate ( ) ;
126
+ this . selection = new Selection ( ) ;
13
127
this . config = { ...NetJSONGraphDefaultConfig } ;
14
128
this . JSONParam = this . utils . isArray ( JSONParam ) ? JSONParam : [ JSONParam ] ;
15
129
}
0 commit comments