Skip to content

Commit c9dbe38

Browse files
authored
Merge pull request #52 from oslabs-beta/finalbranchkelvin
Finalbranchkelvin
2 parents d75815d + 9d247c1 commit c9dbe38

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

src/app/components/StateRoute/History.tsx

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { changeView, changeSlider, setCurrentTabInApp } from '../../slices/mainS
1616
const defaultMargin: DefaultMargin = {
1717
top: 30,
1818
left: 30,
19-
right: 55,
19+
right: 55,
2020
bottom: 70,
2121
};
2222

@@ -26,7 +26,7 @@ function History(props: Record<string, unknown>): JSX.Element {
2626
const {
2727
width: totalWidth, // from ParentSize provided in StateRoute
2828
height: totalHeight, // from ParentSize provided in StateRoute
29-
margin = defaultMargin,
29+
margin = defaultMargin, //default margin is used when margins aren't passed into props
3030
hierarchy, // from 'tabs[currentTab]' object in 'MainContainer'
3131
currLocation, // from 'tabs[currentTab]' object in 'MainContainer'
3232
snapshots, // from 'tabs[currentTab].snapshotDisplay' object in 'MainContainer'
@@ -146,7 +146,7 @@ function History(props: Record<string, unknown>): JSX.Element {
146146

147147
const makeD3Tree = () => {
148148
const svg = 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.
149-
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
149+
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.
150150

151151
const tree = (data) => { // function that takes in data and turns it into a d3 tree.
152152
const treeRoot = d3.hierarchy(data); // 'd3.hierarchy' constructs a root node from the specified hierarchical data.
@@ -156,36 +156,43 @@ function History(props: Record<string, unknown>): JSX.Element {
156156
const d3root = tree(root); // create a d3. tree from our root
157157
const currNode = labelCurrentNode(d3root); // iterate through our nodes and apply a color property
158158

159-
const g = svg
159+
const g = svg //serves as a container for the nodes and links within the D3 Visualization of the tree
160160
.append('g') // create an element 'g' on svg
161161
.attr(
162162
'transform',
163-
`translate(${margin.left},${d3root.height === 0 ? totalHeight / 2 : margin.top})`,
163+
`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.
164164
);
165165

166-
const link = g
166+
const link = g //responsible for rendering the links or connectors between the nodes in the d3 Tree
167167
.selectAll('.link') // select all elements that contain the string '.link' and return a selection
168168
.data(d3root.descendants().slice(1))
169169
.enter()
170170
.append('path')
171171
.attr('class', 'link')
172-
.attr(
172+
.attr( //defines the path attribute (d) for each link (edge) between nodes, using a Bézier curve (C) to connect the source node's coordinates (d.x, d.y) to the midpoint between the source and target nodes and then to the target node's coordinates (d.parent.x, d.parent.y)
173173
'd',
174-
(d) =>
174+
(d) =>
175175
`M${d.x},${d.y}C${d.x},${(d.y + d.parent.y) / 2} ${d.parent.x},${
176176
(d.y + d.parent.y) / 2
177177
} ${d.parent.x},${d.parent.y}`,
178-
);
178+
)
179+
.attr('class', (d) => {
180+
// Adding a class based on the current node's data
181+
if (d.data.index === currLocation.index) {
182+
return 'link current-link'; // Apply both 'link' and 'current-link' classes
183+
}
184+
return 'link'; // Apply only the 'link' class
185+
});
179186

180-
const node = g
187+
const node = g //responsible for rendering nodes in d3 visualization tree
181188
.selectAll('.node')
182189
.data(d3root.descendants())
183190
.enter()
184191
.append('g')
185192
.style('cursor', 'pointer')
186193
.attr('class', `snapshotNode`)
187194
.on('click', (event, d) => {
188-
dispatch(changeView(d.data.index));
195+
dispatch(changeView(d.data.index));
189196
dispatch(changeSlider(d.data.index));
190197
/*
191198
created popup div and appended it to display div(returned in this function)
@@ -207,11 +214,11 @@ function History(props: Record<string, unknown>): JSX.Element {
207214
}
208215

209216
if (d3.selectAll('.tooltip')._groups['0'].length === 0) {
210-
renderToolTip();
217+
renderToolTip(); //if there are no tooltips left in the doc, we call the function to create a new tooltip
211218
} else {
212-
if (d3.selectAll(`#tt-${d.data.index}`)._groups['0'].length === 0) {
213-
d3.selectAll('.tooltip').remove();
214-
renderToolTip();
219+
if (d3.selectAll(`#tt-${d.data.index}`)._groups['0'].length === 0) { // if there is no tooltip with the specific id
220+
d3.selectAll('.tooltip').remove(); //remove any existing tooltips
221+
renderToolTip(); //call the function again to create a new tooltip
215222
}
216223
}
217224
})
@@ -256,14 +263,13 @@ function History(props: Record<string, unknown>): JSX.Element {
256263

257264
node
258265
.append('circle')
259-
260266
.attr('fill', (d) => {
261267
if (d.data.index === currLocation.index) {
262268
return 'red';
263269
}
264270
return d.color ? d.color : '#555';
265271
})
266-
.attr('r', 14);
272+
.attr('r', 18);
267273

268274
node
269275
.append('text')

src/app/styles/components/d3graph.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,13 @@ div.tooltip {
9393
pointer-events: none;
9494
text-anchor: middle;
9595
}
96+
97+
/* .link {
98+
stroke: #ccc;
99+
stroke-width: 1.5;
100+
} */
101+
102+
/* .current-link {
103+
stroke: red;
104+
stroke-width: 2.5; /* Adjust the width as needed */
105+
/* } */

src/app/styles/layout/_stateContainer.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,5 @@
263263
.tooltipData-JSONTree::-webkit-scrollbar-track {
264264
background: #51565e;
265265
}
266+
267+

0 commit comments

Comments
 (0)