You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -163,23 +167,43 @@ function History(props: Record<string, unknown>): JSX.Element {
163
167
*/
164
168
165
169
constmakeD3Tree=()=>{
166
-
constsvg=d3.select(svgRef.current);// d3.select Selects the first element/node that matches svgRef.current. If no element/node match returns an empty selection. If multiple elements/nodes match the selector, only the first matching element/node (in document order) will be selected.
167
-
svg.selectAll('*').remove();// Selects all elements. The elements will be selected in document order (top-to-bottom). We then remove the selected elements/nodes from the DOM. This is important as to ensure that the SVG is empty before rendering the D3 based visualization to avoid interference/overlap with any previously rendered content.
168
-
consttree=(data)=>{
169
-
// function that takes in data and turns it into a d3 tree.
170
-
consttreeRoot=d3.hierarchy(data);// 'd3.hierarchy' constructs a root node from the specified hierarchical data.
171
-
returnd3.tree().size([innerWidth,innerHeight])(treeRoot);// d3.tree creates a new tree layout with a size option of innerWidth (~line 41) and innerHeight (~line 42). We specify our new tree layout's root as 'treeRoot' which assigns an x and y property to each node to represent an [x, y] coordinate system.
172
-
};
170
+
constsvg=d3.select(svgRef.current);
171
+
svg.selectAll('*').remove();
172
+
173
+
// Create tree layout with fixed node separation
174
+
consttreeLayout=d3.tree().nodeSize([FIXED_NODE_WIDTH,FIXED_NODE_HEIGHT]);// Set fixed sizes between nodes
175
+
176
+
// Calculate the tree structure
177
+
constd3root=treeLayout(d3.hierarchy(root));
178
+
179
+
// Calculate total required height and width
180
+
letminX=Infinity;
181
+
letmaxX=-Infinity;
182
+
letminY=Infinity;
183
+
letmaxY=-Infinity;
184
+
185
+
d3root.each((d)=>{
186
+
minX=Math.min(minX,d.x);
187
+
maxX=Math.max(maxX,d.x);
188
+
minY=Math.min(minY,d.y);
189
+
maxY=Math.max(maxY,d.y);
190
+
});
173
191
174
-
constd3root=tree(root);// create a d3. tree from our root
175
-
constcurrNode=labelCurrentNode(d3root);// iterate through our nodes and apply a color property
192
+
constactualWidth=maxX-minX+FIXED_NODE_WIDTH;
193
+
constactualHeight=maxY-minY+FIXED_NODE_HEIGHT;
176
194
177
-
constg=svg//serves as a container for the nodes and links within the D3 Visualization of the tree
178
-
.append('g')// create an element 'g' on svg
179
-
.attr(
180
-
'transform',
181
-
`translate(${margin.left},${d3root.height===0 ? totalHeight/2 : margin.top})`,//Set the position of the group 'g' by translating it horizontally by 'margin.left' pixels and vertically based on the conditional expression.
0 commit comments