11import NetJSONGraphDefaultConfig from "./netjsongraph.config" ;
22import NetJSONGraphUpdate from "./netjsongraph.update" ;
33
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 nodeData = [ ] ;
59+ let linkData = [ ] ;
60+ if ( option . leaflet ) {
61+ // map data
62+ nodeData = option . leaflet [ 0 ] . mapOptions . nodeConfig . data ;
63+ linkData = option . leaflet [ 0 ] . mapOptions . linkConfig . data ;
64+ } else {
65+ // graph data
66+ nodeData = option . series [ 0 ] . nodes ;
67+ linkData = option . series [ 0 ] . links ;
68+ }
69+ const nodeIndexes = nodeData . map ( ( node , index ) => index ) ;
70+ const linkIndexes = linkData . map ( ( link , index ) => index ) ;
71+
72+ // downplay all items
73+ echarts . dispatchAction (
74+ { type : 'downplay' , seriesIndex : 0 , batch : [
75+ { dataType : "node" , dataIndex : nodeIndexes } ,
76+ { dataType : "edge" , dataIndex : linkIndexes } ,
77+ ]
78+ }
79+ )
80+
81+ this . selected . clear ( ) ;
82+ }
83+ }
84+
85+ // called when switching between graph/map view
86+ highlightSelected ( echarts ) {
87+ const option = echarts . getOption ( ) ;
88+ let nodeData = [ ] ;
89+ let linkData = [ ] ;
90+ if ( option . leaflet ) {
91+ // map data
92+ nodeData = option . leaflet [ 0 ] . mapOptions . nodeConfig . data ;
93+ linkData = option . leaflet [ 0 ] . mapOptions . linkConfig . data ;
94+ } else {
95+ // graph data
96+ nodeData = option . series [ 0 ] . nodes ;
97+ linkData = option . series [ 0 ] . links ;
98+ }
99+
100+ const nodeIndexes = nodeData . map ( ( node , index ) => this . isSelected ( node ) ? index : - 1 ) . filter ( ( index ) => index !== - 1 ) ;
101+ const linkIndexes = linkData . map ( ( link , index ) => this . isSelected ( link ) ? index : - 1 ) . filter ( ( index ) => index !== - 1 ) ;
102+
103+ echarts . dispatchAction (
104+ { type : 'highlight' , seriesIndex : 0 , batch : [
105+ { dataType : "node" , dataIndex : nodeIndexes } ,
106+ { dataType : "edge" , dataIndex : linkIndexes } ,
107+ ]
108+ }
109+ )
110+ }
111+ }
112+
4113class NetJSONGraph {
5114 /**
6115 * @constructor
@@ -10,6 +119,7 @@ class NetJSONGraph {
10119 */
11120 constructor ( JSONParam ) {
12121 this . utils = new NetJSONGraphUpdate ( ) ;
122+ this . selection = new Selection ( ) ;
13123 this . config = { ...NetJSONGraphDefaultConfig } ;
14124 this . JSONParam = this . utils . isArray ( JSONParam ) ? JSONParam : [ JSONParam ] ;
15125 }
0 commit comments