Skip to content

Commit d4a1510

Browse files
committed
chart tracking time jumps correctly; final update before PR
1 parent f10a39a commit d4a1510

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/app/components/PerfView.jsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable react/no-this-in-sfc */
12
/* eslint-disable no-unused-vars */
23
/* eslint-disable react/prop-types */
34
/* eslint-disable arrow-body-style */
@@ -14,7 +15,7 @@
1415
/* eslint-disable indent */
1516
/* eslint-disable no-console */
1617

17-
import React, { useEffect, useState, useRef } from 'react';
18+
import React, { useEffect, useState, useRef, useCallback } from 'react';
1819
import * as d3 from 'd3';
1920
// import { addNewSnapshots } from '../actions/actions';
2021

@@ -46,7 +47,8 @@ const PerfView = ({ snapshots, viewIndex }) => {
4647
.interpolate(d3.interpolateHcl);
4748

4849
// set up circle-packing layout function
49-
const packFunc = data => d3.pack()
50+
const packFunc = useCallback(data => {
51+
return d3.pack()
5052
.size([width, height])
5153
.padding(3)(d3.hierarchy(data)
5254
.sum(d => {
@@ -56,30 +58,31 @@ const PerfView = ({ snapshots, viewIndex }) => {
5658
.sort((a, b) => {
5759
// console.log('in sort func. a&b=', a, b);
5860
return b.value - a.value;
59-
}));
60-
61-
console.log('packFunc', packFunc);
61+
}));
62+
}, [width, height]);
6263

64+
// first run, or user has made changes in their app; clear old tree and get current chartData
6365
useEffect(() => {
6466
console.log('PerfView -> snapshots', snapshots);
6567
console.log('Current viewIndex: ', viewIndex);
6668
for (let i = 0; i < snapshots.length; i++) {
6769
console.log(`SNAPSHOT[${i}] App actualDuration:`, snapshots[i].children[0].componentData.actualDuration);
6870
}
6971

70-
// empty old tree
72+
// clear old tree
7173
while (svgRef.current.hasChildNodes()) {
7274
svgRef.current.removeChild(svgRef.current.lastChild);
7375
}
7476

75-
if (viewIndex < 0) {
76-
updateChartData(snapshots[snapshots.length - 1]);
77-
console.log(`Using snapshots[${snapshots.length - 1}]`);
78-
} else {
79-
updateChartData(snapshots[viewIndex]);
80-
console.log(`Using snapshots[${viewIndex}]`);
81-
}
77+
let indexToDisplay = null;
78+
if (viewIndex < 0) indexToDisplay = snapshots.length - 1;
79+
else indexToDisplay = viewIndex;
80+
81+
updateChartData(snapshots[indexToDisplay]);
82+
console.log(`Using snapshots[${indexToDisplay}]`);
83+
}, [svgRef, viewIndex, snapshots, chartData]);
8284

85+
useEffect(() => {
8386
console.log('PerfView -> chartData', chartData);
8487

8588
// generate tree with our data
@@ -118,19 +121,18 @@ const PerfView = ({ snapshots, viewIndex }) => {
118121
.style('fill-opacity', d => (d.parent === packedRoot ? 1 : 0))
119122
.style('display', d => (d.parent === packedRoot ? 'inline' : 'none'))
120123
.text(d => {
121-
console.log('generating text label for d: ', d);
124+
// console.log('generating text label for d: ', d);
122125
return `${d.data.name}: ${Number.parseFloat(d.data.componentData.actualDuration).toFixed(2)}ms`;
123126
});
124127

125128
label.exit().remove();
126129
node.exit().remove();
127130

128-
// console.log('PerfView -> label', label);
129-
130131
// jump to default zoom state
131132
zoomTo([packedRoot.x, packedRoot.y, packedRoot.r * 2]);
132133

133134
function zoomTo(v) {
135+
// console.log("zoomTo -> v", v);
134136
const k = width / v[2];
135137
view = v;
136138
label.attr('transform', d => `translate(${(d.x - v[0]) * k},${(d.y - v[1]) * k})`);
@@ -139,6 +141,7 @@ const PerfView = ({ snapshots, viewIndex }) => {
139141
}
140142

141143
function zoom(d) {
144+
// console.log("zoom -> d", d);
142145
const focus0 = focus;
143146
focus = d;
144147

@@ -156,7 +159,7 @@ const PerfView = ({ snapshots, viewIndex }) => {
156159
.on('start', function (d) { if (d.parent === focus) this.style.display = 'inline'; })
157160
.on('end', function (d) { if (d.parent !== focus) this.style.display = 'none'; });
158161
}
159-
}, [snapshots.length, height, width, viewIndex]);
162+
}, [chartData, color, packFunc, width, height]);
160163

161164
return <svg className="perfContainer" ref={svgRef} />;
162165
};

0 commit comments

Comments
 (0)