1
1
import Ember from 'ember' ;
2
2
3
- import fetch from "ember-network/fetch" ;
4
-
5
3
// Import the D3 packages we want to use
6
4
import { select , event } from 'd3-selection' ;
7
5
import { tree , cluster , hierarchy } from 'd3-hierarchy' ;
8
6
import { zoom , zoomIdentity } from 'd3-zoom' ;
9
7
10
- const { $, run, get } = Ember ;
8
+ const { run, get } = Ember ;
9
+
10
+ const DURATION = 500 ;
11
11
12
12
// copied these functions temporarily from `broccoli-viz` here:
13
13
// https://github.com/ember-cli/broccoli-viz/blob/master/lib/node-by-id.js
@@ -30,23 +30,10 @@ export default Ember.Component.extend({
30
30
} ,
31
31
32
32
didReceiveAttrs ( ) {
33
- let graphPath = get ( this , 'graphPath' ) ;
34
33
let graphData = get ( this , 'graphData' ) ;
35
34
36
- if ( this . _lastGraphPath !== graphPath && graphPath ) {
37
- fetch ( graphPath )
38
- . then ( ( response ) => {
39
- return response . json ( ) ;
40
- } )
41
- . then ( ( response ) => this . processRawData ( response ) ) ;
42
-
43
- this . _lastGraphPath = graphPath ;
44
- }
45
-
46
35
if ( this . _lastGraphData !== graphData && graphData ) {
47
- let response = JSON . parse ( graphData ) ;
48
-
49
- this . processRawData ( response ) ;
36
+ this . processRawData ( graphData ) ;
50
37
51
38
this . _lastGraphData = graphData ;
52
39
}
@@ -78,39 +65,31 @@ export default Ember.Component.extend({
78
65
return node . children
79
66
. map ( ( childId ) => data . nodesById [ childId ] )
80
67
. filter ( this . nodeFilter ) ;
81
- } ) . sum ( d => d . stats . time . self ) ;
68
+ } )
69
+ . sum ( d => d . stats . time . self ) ;
70
+
71
+ // make debugging easier, this is dumb though
72
+ self . root = root ;
82
73
83
- let graph = tree ( )
74
+ let graph = cluster ( )
84
75
. separation ( ( a , b ) => {
85
76
return a . parent == b . parent ? 4 : 8 ;
86
77
} )
87
78
. nodeSize ( [ 6 , 180 ] ) ;
88
79
graph ( root ) ;
89
80
90
81
function update ( source ) {
91
- let link = g . selectAll ( ".link" )
92
- . data ( root . descendants ( ) . slice ( 1 ) ) ;
93
-
94
- link . enter ( )
95
- . append ( "path" )
96
- . attr ( "class" , "link" )
97
- . attr ( "d" , function ( d ) {
98
- return "M" + d . y + "," + d . x
99
- + "C" + ( d . parent . y + 50 ) + "," + d . x
100
- + " " + ( d . parent . y + 50 ) + "," + d . parent . x
101
- + " " + d . parent . y + "," + d . parent . x ;
102
- } ) ;
103
-
104
- let node = g . selectAll ( ".node" )
105
- . data ( root . descendants ( ) ) ;
82
+ let nodes = root . descendants ( ) ;
83
+ let links = root . links ( ) ;
84
+ let node = g
85
+ . selectAll ( ".node" )
86
+ . data ( nodes , d => d . data . _id ) ;
106
87
107
88
let nodeEnter = node
108
89
. enter ( )
109
90
. append ( "g" )
110
91
. attr ( "class" , 'node' )
111
- . attr ( "transform" , function ( d ) {
112
- return "translate(" + d . y + "," + d . x + ")" ;
113
- } )
92
+ . attr ( "transform" , d => `translate(${ d . y } ,${ d . x } )` )
114
93
. on ( 'click' , ( d ) => {
115
94
// Toggle children on click.
116
95
if ( d . children ) {
@@ -135,44 +114,65 @@ export default Ember.Component.extend({
135
114
// .attr('stroke-width', 1)
136
115
// .style('fill', "#fff");
137
116
138
- nodeEnter . append ( 'text' )
117
+ nodeEnter
118
+ . append ( 'text' )
139
119
. attr ( 'dy' , '-1.1em' )
140
120
. style ( "text-anchor" , function ( d ) { return d . children ? "end" : "start" ; } )
141
121
. text ( d => d . data . _id ) ;
142
122
143
- nodeEnter . append ( "text" )
123
+ nodeEnter
124
+ . append ( "text" )
144
125
. attr ( "dy" , '0.1em' )
145
126
. style ( "text-anchor" , function ( d ) { return d . children ? "end" : "start" ; } )
146
127
. text ( function ( d ) {
147
128
return d . data . id . name ;
148
129
} ) ;
149
130
150
- nodeEnter . append ( "text" )
131
+ nodeEnter
132
+ . append ( "text" )
151
133
. attr ( "dy" , '1.1em' )
152
134
. style ( "text-anchor" , function ( d ) { return d . children ? "end" : "start" ; } )
153
135
. text ( function ( d ) {
154
136
return `total: ${ ( d . value / 1000000 ) . toFixed ( 2 ) } ` ;
155
137
} ) ;
156
138
157
- nodeEnter . append ( "text" )
139
+ nodeEnter
140
+ . append ( "text" )
158
141
. attr ( "dy" , '2.1em' )
159
142
. style ( "text-anchor" , function ( d ) { return d . children ? "end" : "start" ; } )
160
143
. text ( function ( d ) {
161
144
return `self: ${ ( d . data . stats . time . self / 1000000 ) . toFixed ( 2 ) } ` ;
162
145
} ) ;
163
146
164
147
// Transition exiting nodes to the parent's new position.
165
- node . exit ( )
148
+ node
149
+ . exit ( )
166
150
. transition ( )
167
- . duration ( 50 )
151
+ . duration ( DURATION )
168
152
. attr ( "transform" , function ( ) {
169
153
return "translate(" + source . x + "," + source . y + ")" ;
170
154
} )
171
155
. remove ( ) ;
172
156
173
- link . exit ( )
157
+ let link = g
158
+ . selectAll ( ".link" )
159
+ . data ( links , d => d . target . data . _id ) ;
160
+
161
+ link
162
+ . enter ( )
163
+ . append ( "path" )
164
+ . attr ( "class" , "link" )
165
+ . attr ( "d" , function ( d ) {
166
+ return "M" + d . target . y + "," + d . target . x
167
+ + "C" + ( d . source . y + 50 ) + "," + d . target . x
168
+ + " " + ( d . source . y + 50 ) + "," + d . source . x
169
+ + " " + d . source . y + "," + d . source . x ;
170
+ } ) ;
171
+
172
+ link
173
+ . exit ( )
174
174
. transition ( )
175
- . duration ( 50 )
175
+ . duration ( DURATION / 2 )
176
176
. attr ( "transform" , function ( ) {
177
177
return "translate(" + source . x + "," + source . y + ")" ;
178
178
} )
0 commit comments