11import * as d3 from 'd3' ;
2- class RouteTrace {
32
3+ let text = [ {
4+ t : 'reverseproxy' ,
5+ } , {
6+ t : 'books' ,
7+ } , {
8+ t : 'orders' ,
9+ } , {
10+ t : 'customers'
11+ } ]
12+ class RouteTrace {
413 containerEl ;
514 props ;
615 svg ;
7-
16+
17+ // creating D3 container for the nodes
818 constructor ( containerEl , props ) {
919 this . containerEl = containerEl ;
1020 this . props = props ;
@@ -17,38 +27,52 @@ class RouteTrace {
1727 this . updateDatapoints ( ) ;
1828 }
1929
30+
2031 updateDatapoints = ( ) => {
2132 const { svg, props : { data, width, height } } = this ;
2233 console . log ( 'data: ' , data )
2334 console . log ( 'nodes: ' , data [ 0 ] )
2435 console . log ( 'links: ' , data [ 1 ] )
36+
37+ console . log ( '1st data point: ' , data [ 0 ] [ 0 ] )
38+ console . log ( '4th data point: ' , data [ 0 ] [ 3 ] )
2539
2640
2741 var simulation = d3 . forceSimulation ( data [ 0 ] )
2842 . force ( 'link' , d3 . forceLink ( ) )
2943 . force ( 'charge' , d3 . forceManyBody ( ) )
3044 . force ( 'center' , d3 . forceCenter ( width / 2 , height / 2 ) )
31-
45+
3246 const node = svg
33- . selectAll ( "circle" )
34- . data ( data [ 0 ] )
35- . enter ( )
36- . append ( "circle" )
37- . attr ( "r" , 20 )
38- . attr ( 'cx' , ( ) => Math . random ( ) * width )
39- . attr ( 'cy' , ( ) => Math . random ( ) * height )
40- . style ( "fill" , "#00eda0" )
41- . on ( 'mouseup' , ( d , i , nodes ) => this . setActiveDatapoint ( d , nodes [ i ] ) ) ;
47+ . selectAll ( 'circle' )
48+ . data ( data [ 0 ] )
49+ . enter ( )
50+ . append ( 'circle' )
51+ . on ( 'mouseover' , ( ) => d3 . select ( this ) . text ( d . id ) )
52+
53+ const circleAttr = node
54+ . attr ( 'r' , 20 )
55+ . attr ( 'cx' , 200 )
56+ . attr ( 'cy' , 200 )
57+ . style ( 'fill' , '#00eda0' )
58+ . on ( 'mouseup' , ( d , i , nodes ) => this . setActiveDatapoint ( d , nodes [ i ] ) )
4259
43- node . append ( "title" )
44- . text ( function ( d ) { return d . id ; } ) ;
60+ const text = svg . selectAll ( 'text' )
61+ . data ( data [ 0 ] ) . enter ( ) . append ( 'text' )
62+
63+ const textLabels = text
64+ . attr ( 'x' , d => d . x )
65+ . attr ( 'y' , d => d . y )
66+ . attr ( 'dx' , 100 )
67+ . attr ( 'dy' , '10em' )
68+ . text ( d => d . id ) ;
4569
4670 const link = svg
47- . selectAll ( " line" )
71+ . selectAll ( ' line' )
4872 . data ( data [ 1 ] )
4973 . enter ( )
50- . append ( " line" )
51- . style ( " stroke" , " black" )
74+ . append ( ' line' )
75+ . style ( ' stroke' , ' black' )
5276
5377 simulation
5478 . nodes ( data [ 0 ] )
@@ -57,19 +81,17 @@ class RouteTrace {
5781 simulation . force ( 'link' )
5882 . links ( link ) ;
5983
60-
6184 function ticked ( ) {
6285 link
63- . attr ( "x1" , function ( d ) { return d . source . x ; } )
64- . attr ( "y1" , function ( d ) { return d . source . y ; } )
65- . attr ( "x2" , function ( d ) { return d . target . x ; } )
66- . attr ( "y2" , function ( d ) { return d . target . y ; } ) ;
86+ . attr ( 'x1' , function ( d ) { return d . source . x ; } )
87+ . attr ( 'y1' , function ( d ) { return d . source . y ; } )
88+ . attr ( 'x2' , function ( d ) { return d . target . x ; } )
89+ . attr ( 'y2' , function ( d ) { return d . target . y ; } ) ;
6790
6891 node
69- . attr ( "cx" , function ( d ) { return d . x ; } )
70- . attr ( "cy" , function ( d ) { return d . y ; } ) ;
92+ . attr ( 'cx' , function ( d ) { return d . x ; } )
93+ . attr ( 'cy' , function ( d ) { return d . y ; } ) ;
7194 }
72-
7395 }
7496
7597 setActiveDatapoint = ( d , node ) => {
0 commit comments