@@ -6,11 +6,8 @@ import { Grid } from "@visx/grid";
6
6
import { AxisBottom , AxisLeft } from "@visx/axis" ;
7
7
import { scaleBand , scaleLinear , scaleOrdinal } from "@visx/scale" ;
8
8
import { useTooltip , useTooltipInPortal , defaultStyles } from "@visx/tooltip" ;
9
- // import { LegendOrdinal } from "@visx/legend";
10
9
import { Text } from '@visx/text' ;
11
10
import { schemeSet1 , schemeSet3 } from "d3-scale-chromatic" ;
12
- // import snapshots from "./snapshots";
13
- import useForceUpdate from './useForceUpdate' ;
14
11
15
12
16
13
/* TYPESCRIPT */
@@ -46,16 +43,18 @@ const tooltipStyles = {
46
43
color : "white"
47
44
} ;
48
45
49
- /* DATA PREP FUNCTIONS */
46
+ /* DATA HANDLING FUNCTIONS */
50
47
const getPerfMetrics = ( snapshots , snapshotsIds ) => {
51
- return snapshots . reduce ( ( perfSnapshots , curSnapshot , idx ) => {
48
+ return snapshots . reduce ( ( perfSnapshots , curSnapshot , idx ) => {
52
49
return perfSnapshots . concat ( traverse ( curSnapshot , { snapshotId :snapshotsIds [ idx ] } ) )
53
50
} , [ ] )
54
51
}
55
52
53
+ // traverse the snapshot for all components and their rendering time
56
54
const traverse = ( snapshot , perfSnapshot ) => {
57
55
if ( ! snapshot . children [ 0 ] ) return
58
56
for ( let i = 0 ; i < snapshot . children . length ; i ++ ) {
57
+ // perfSnapshot.state = snapshot.children[i].state
59
58
if ( snapshot . children [ i ] . componentData . actualDuration ) {
60
59
const renderTime = Number ( Number . parseFloat ( snapshot . children [ i ] . componentData . actualDuration ) . toPrecision ( 5 ) )
61
60
perfSnapshot [ snapshot . children [ i ] . name + - [ i + 1 ] ] = renderTime
@@ -75,6 +74,8 @@ if (obj.children) {
75
74
return snapshotIds
76
75
} ;
77
76
77
+
78
+
78
79
/* EXPORT COMPONENT */
79
80
export default function PerformanceVisx ( {
80
81
width,
@@ -100,7 +101,8 @@ let tooltipTimeout: number;
100
101
// filter and structure incoming data for VISX
101
102
const data = getPerfMetrics ( snapshots , getSnapshotIds ( hierarchy ) )
102
103
const keys = Object . keys ( data [ 0 ] ) . filter ( ( d ) => d !== "snapshotId" ) as CityName [ ] ;
103
- console . log ( data )
104
+ console . log ( 'data' , data )
105
+ console . log ( 'snapshots' , snapshots )
104
106
105
107
// create array of total render times for each snapshot
106
108
const totalRenderArr = data . reduce ( ( totalRender , curSnapshot ) => {
@@ -117,7 +119,7 @@ const getSnapshotId = (d: snapshot) => d.snapshotId;
117
119
const formatSnapshotId = id => 'Snapshot ID: ' + id ;
118
120
const formatRenderTime = time => time + ' ms ' ;
119
121
120
- // create visualization scales with filtered data
122
+ // create visualization SCALES with filtered data
121
123
const snapshotIdScale = scaleBand < string > ( {
122
124
domain : data . map ( getSnapshotId ) ,
123
125
padding : 0.2
@@ -142,6 +144,12 @@ const yMax = height - margin.top - 150;
142
144
snapshotIdScale . rangeRound ( [ 0 , xMax ] ) ;
143
145
renderingScale . range ( [ yMax , 0 ] ) ;
144
146
147
+ // console.log('totalRenderArr', totalRenderArr)
148
+ // console.log('renderingScale domain', renderingScale.domain)
149
+ // console.log('renderingscale range', renderingScale)
150
+ console . log ( 'height' , height )
151
+ // console.log(yMax)
152
+
145
153
return width < 10 ? null : (
146
154
// relative position is needed for correct tooltip positioning
147
155
@@ -175,23 +183,23 @@ renderingScale.range([yMax, 0]);
175
183
yScale = { renderingScale }
176
184
color = { colorScale }
177
185
>
178
- { ( barStacks ) => barStacks . map ( barStack => barStack . bars . map ( ( bar => (
186
+ { ( barStacks ) => barStacks . map ( barStack => barStack . bars . map ( ( ( bar , idx ) => (
179
187
< rect
180
188
key = { `bar-stack-${ barStack . index } -${ bar . index } ` }
181
189
x = { bar . x }
182
190
y = { bar . y }
183
- height = { bar . height }
191
+ height = { bar . height === 0 ? idx + 1 : bar . height }
184
192
width = { bar . width }
185
193
fill = { bar . color }
186
194
/* TIP TOOL EVENT HANDLERS */
187
- onClick = { ( ) => {
188
- if ( events ) alert ( `clicked: ${ JSON . stringify ( bar ) } ` ) ;
189
- } }
195
+
196
+ // Hides tool tip once cursor moves off the current rect
190
197
onMouseLeave = { ( ) => {
191
198
tooltipTimeout = window . setTimeout ( ( ) => {
192
199
hideTooltip ( ) ;
193
200
} , 300 ) ;
194
201
} }
202
+ // Cursor position in window updates position of the tool tip
195
203
onMouseMove = { event => {
196
204
if ( tooltipTimeout ) clearTimeout ( tooltipTimeout ) ;
197
205
const top = event . clientY - margin . top - bar . height ;
@@ -208,7 +216,7 @@ renderingScale.range([yMax, 0]);
208
216
}
209
217
</ BarStack >
210
218
</ Group >
211
- < AxisLeft
219
+ < AxisLeft
212
220
top = { margin . top }
213
221
left = { margin . left }
214
222
scale = { renderingScale }
0 commit comments