@@ -19,17 +19,58 @@ class RouteTrace {
1919
2020 updateDatapoints = ( ) => {
2121 const { svg, props : { data, width, height } } = this ;
22- svg . selectAll ( 'circle' )
23- . data ( data )
24- . enter ( )
25- . append ( 'circle' )
26- . style ( 'fill' , 'red' )
22+ console . log ( 'data: ' , data )
23+ console . log ( 'nodes: ' , data [ 0 ] )
24+ console . log ( 'links: ' , data [ 1 ] )
25+
26+
27+ var simulation = d3 . forceSimulation ( data [ 0 ] )
28+ . force ( 'link' , d3 . forceLink ( ) )
29+ . force ( 'charge' , d3 . forceManyBody ( ) )
30+ . force ( 'center' , d3 . forceCenter ( width / 2 , height / 2 ) )
31+
32+ const node = svg
33+ . selectAll ( "circle" )
34+ . data ( data [ 0 ] )
35+ . enter ( )
36+ . append ( "circle" )
37+ . attr ( "r" , 20 )
2738 . attr ( 'cx' , ( ) => Math . random ( ) * width )
2839 . attr ( 'cy' , ( ) => Math . random ( ) * height )
29- . attr ( 'r' , 20 )
40+ . style ( "fill" , "#00eda0" )
3041 . on ( 'mouseup' , ( d , i , nodes ) => this . setActiveDatapoint ( d , nodes [ i ] ) ) ;
31- }
3242
43+ node . append ( "title" )
44+ . text ( function ( d ) { return d . id ; } ) ;
45+
46+ const link = svg
47+ . selectAll ( "line" )
48+ . data ( data [ 1 ] )
49+ . enter ( )
50+ . append ( "line" )
51+ . style ( "stroke" , "black" )
52+
53+ simulation
54+ . nodes ( data [ 0 ] )
55+ . on ( 'tick' , ticked ) ;
56+
57+ simulation . force ( 'link' )
58+ . links ( link ) ;
59+
60+
61+ function ticked ( ) {
62+ 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 ; } ) ;
67+
68+ node
69+ . attr ( "cx" , function ( d ) { return d . x ; } )
70+ . attr ( "cy" , function ( d ) { return d . y ; } ) ;
71+ }
72+
73+ }
3374
3475 setActiveDatapoint = ( d , node ) => {
3576 d3 . select ( node ) . style ( 'fill' , 'yellow' ) ;
0 commit comments