Skip to content

Commit 4eafdb2

Browse files
tree displaying correctly
1 parent 24d0044 commit 4eafdb2

File tree

3 files changed

+51
-37
lines changed

3 files changed

+51
-37
lines changed

app/src/components/bottom/CodePreview.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const useStyles = makeStyles(theme => ({
2222
}
2323
}));
2424

25+
const optionColor = '#252526';
26+
2527
const CodePreview = () => {
2628
const [state, dispatch] = useContext(stateContext);
2729
const [theme, setTheme] = useState('monokai');
@@ -60,11 +62,11 @@ const CodePreview = () => {
6062
value={theme}
6163
onChange={changeTheme}
6264
>
63-
<option value={'monokai'}>Monokai</option>
64-
<option value={'github'}>Github</option>
65-
<option value={'solarized_dark'}>Solarized Dark</option>
66-
<option value={'terminal'}>Terminal</option>
67-
<option value={'solarized_light'}>Solarized Light</option>
65+
<option value={'monokai'} style={{backgroundColor: optionColor}}>Monokai</option>
66+
<option value={'github'} style={{backgroundColor: optionColor}}>Github</option>
67+
<option value={'solarized_dark'} style={{backgroundColor: optionColor}}>Solarized Dark</option>
68+
<option value={'terminal'} style={{backgroundColor: optionColor}}>Terminal</option>
69+
<option value={'solarized_light'} style={{backgroundColor: optionColor}}>Solarized Light</option>
6870
</NativeSelect>
6971
</FormControl>
7072
<AceEditor

app/src/tree/TreeChart.tsx

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import React, { useRef, useEffect, useContext } from "react";
1+
import React, { useRef, useEffect } from "react";
22
import { select, hierarchy, tree, linkHorizontal } from "d3";
3-
import { stateContext } from '../context/context';
4-
5-
// i don't think this does what i need it to do
63
import useResizeObserver from "./useResizeObserver";
74

85
function usePrevious(value) {
@@ -14,14 +11,14 @@ function usePrevious(value) {
1411
}
1512

1613
function TreeChart({ data }) {
17-
const [state, dispatch] = useContext(stateContext);
1814
const svgRef = useRef();
1915
const wrapperRef = useRef();
16+
17+
const xPosition = 50;
18+
const textAndBorderColor = 'rgb(51, 235, 145)';
2019

21-
// this needs to be modified
2220
const dimensions = useResizeObserver(wrapperRef);
23-
console.log('dimensions', dimensions);
24-
21+
2522
// we save data to see if it changed
2623
const previouslyRenderedData = usePrevious(data);
2724

@@ -34,14 +31,10 @@ function TreeChart({ data }) {
3431
// (dimensions are null for the first render)
3532
const { width, height } =
3633
dimensions || wrapperRef.current.getBoundingClientRect();
37-
38-
console.log('width', width);
39-
console.log('height', height);
40-
4134
// transform hierarchical data
4235
const root = hierarchy(data[0]);
43-
const treeLayout = tree().size([height, width]);
44-
36+
const treeLayout = tree().size([height, width-125]);
37+
4538
// Returns a new link generator with horizontal display.
4639
// To visualize links in a tree diagram rooted on the left edge of the display
4740
const linkGenerator = linkHorizontal()
@@ -62,10 +55,13 @@ function TreeChart({ data }) {
6255
cx: The position of the centre of the element in the x axis measured from the left side of the screen.
6356
cy: The position of the centre of the element in the y axis measured from the top of the screen.
6457
*/
65-
.attr("cx", node => node.y)
66-
.attr("cy", node => node.x)
67-
.attr("r", 4) // radius of circle
68-
.attr("opacity", 1);
58+
// translate (x, y)
59+
.attr("cx", node => node.y)
60+
.attr("cy", node => node.x)
61+
.attr("r", 4) // radius of circle
62+
.attr("opacity", 1)
63+
.style('fill', 'white')
64+
.attr("transform", `translate(${xPosition}, 0)`);
6965

7066
// link - lines that connect the nodes
7167
const enteringAndUpdatingLinks = svg
@@ -74,14 +70,11 @@ function TreeChart({ data }) {
7470
.join("path")
7571
.attr("class", "link")
7672
.attr("d", linkGenerator)
77-
.attr("stroke-dasharray", function() {
78-
const length = this.getTotalLength();
79-
return `${length} ${length}`;
80-
})
81-
.attr("stroke", "black")
73+
.attr("stroke", "white")
8274
.attr("fill", "none")
83-
.attr("opacity", 1);
84-
75+
.attr("opacity", 1)
76+
.attr("transform", `translate(${xPosition}, 0)`);
77+
8578
if (data !== previouslyRenderedData) {
8679
enteringAndUpdatingLinks
8780
.attr("stroke-dashoffset", function() {
@@ -93,20 +86,37 @@ function TreeChart({ data }) {
9386
// label - the names of each html element (node)
9487
svg
9588
.selectAll(".label")
96-
.data(root.descendants())
89+
.data(root.descendants())
9790
.join(enter => enter.append("text").attr("opacity", 0))
9891
.attr("class", "label")
9992
.attr("x", node => node.y)
10093
.attr("y", node => node.x - 12)
10194
.attr("text-anchor", "middle")
102-
.attr("font-size", 24)
95+
.attr("font-size", 18)
96+
.style('fill', textAndBorderColor)
10397
.text(node => node.data.name)
104-
.attr("opacity", 1);
98+
.attr("opacity", 1)
99+
.attr("transform", `translate(${xPosition}, 0)`);
105100
}, [data, dimensions, previouslyRenderedData]);
106101

102+
const treeStyles = {
103+
height: '100%',
104+
width: `100%`,
105+
margin: '10px 10px 10px 10px',
106+
overflow: 'auto'
107+
}
108+
109+
const wrapperStyles = {
110+
border: `2px solid ${textAndBorderColor}`,
111+
borderRadius: '8px',
112+
width: '100%',
113+
height: '90%',
114+
display: 'flex',
115+
justifyContent: 'center',
116+
}
107117
return (
108-
<div ref={wrapperRef} style={{ marginBottom: "2rem" }}>
109-
<svg ref={svgRef}></svg>
118+
<div ref={wrapperRef} style={wrapperStyles}>
119+
<svg ref={svgRef} style={treeStyles}></svg>
110120
</div>
111121
);
112122
}

app/src/tree/useResizeObserver.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import ResizeObserver from "resize-observer-polyfill";
44
const useResizeObserver = ref => {
55
const [dimensions, setDimensions] = useState(null);
66
useEffect(() => {
7+
// the element being observed (div with green border)
78
const observeTarget = ref.current;
89
const resizeObserver = new ResizeObserver(entries => {
9-
entries.forEach(entry => {
10+
entries.forEach(entry => {
11+
// contentRect is an object containing the dimensions of the observed element
1012
setDimensions(entry.contentRect);
1113
});
1214
});
@@ -15,7 +17,7 @@ const useResizeObserver = ref => {
1517
resizeObserver.unobserve(observeTarget);
1618
};
1719
}, [ref]);
18-
return dimensions;
20+
return dimensions; // { height, width }
1921
};
2022

2123
export default useResizeObserver;

0 commit comments

Comments
 (0)