Skip to content

Commit f67448c

Browse files
committed
added d3 css styling and updated d3 layout algorithm to change angle of node text
1 parent feff6b9 commit f67448c

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

src/app/components/Chart.jsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import React, { Component } from 'react';
66
import PropTypes from 'prop-types';
77
import * as d3 from 'd3';
88

9+
910
let root = {};
1011
let duration = 750;
1112
class Chart extends Component {
@@ -38,8 +39,8 @@ class Chart extends Component {
3839

3940
maked3Tree() {
4041
this.removed3Tree();
41-
let width = 900;
42-
let height = 1000;
42+
let width = 960;
43+
let height = 1060;
4344
let chartContainer = d3.select(this.chartRef.current)
4445
.append('svg') // chartContainer is now pointing to svg
4546
.attr('width', width)
@@ -48,7 +49,7 @@ class Chart extends Component {
4849
let g = chartContainer
4950
.append("g")
5051
// this is changing where the graph is located physically
51-
.attr("transform", `translate(${width / 2.5}, ${height / 2 + 90})`);
52+
.attr("transform", `translate(${width / 2 + 4}, ${height / 2 + 2})`);
5253

5354
// if we consider the container for our radial node graph as a box encapsulating, half of this container width is essentially the radius of our radial node graph
5455
let radius = width / 2;
@@ -59,6 +60,7 @@ class Chart extends Component {
5960
let tree = d3.tree()
6061
// this assigns width of tree to be 2pi
6162
.size([2 * Math.PI, radius])
63+
.separation(function (a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth });
6264

6365
let d3root = tree(hierarchy);
6466

@@ -77,6 +79,7 @@ class Chart extends Component {
7779
.data(d3root.descendants())
7880
.enter()
7981
.append("g")
82+
// assigning class to the node based on whether node has children or not
8083
.attr("class", function (d) {
8184
return "node" + (d.children ? " node--internal" : " node--leaf")
8285
})
@@ -85,18 +88,19 @@ class Chart extends Component {
8588
});
8689

8790
node.append("circle")
88-
.attr("r", 5.5)
91+
.attr("r", 40.5)
8992

9093
node
9194
.append("text")
9295
.attr("dy", "0.1em")
9396
.attr("x", function (d) {
9497
// this positions how far the text is from leaf nodes (ones without children)
95-
return d.x < Math.PI === !d.children ? 10 : -10;
98+
// negative number before the colon moves the text of rightside nodes, positive number moves the text for the leftside nodes
99+
return d.x < Math.PI === !d.children ? -20 : 20;
96100
})
97101
.attr("text-anchor", function (d) { return d.x < Math.PI === !d.children ? "start" : "end"; })
98102
// this arranges the angle of the text
99-
.attr("transform", function (d) { return "rotate(" + (d.x < Math.PI ? d.x - Math.PI / 2 : d.x + Math.PI / 2) * 180 / Math.PI + ")"; })
103+
.attr("transform", function (d) { return "rotate(" + (d.x < Math.PI ? d.x - Math.PI / 2 : d.x + Math.PI / 2) * 1 / Math.PI + ")"; })
100104
.text(function (d) {
101105
return "state" + d.data.index;
102106
});

src/app/styles/components/_d3Tree.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.d3Container{
1+
/* .d3Container{
22
height: 100%;
33
width: 100%;
44
}
@@ -37,4 +37,4 @@ div.tooltip {
3737
background: #ffff;
3838
border: solid 1px #aaa;
3939
border-radius: 8px;
40-
}
40+
} */

src/app/styles/components/d3graph.css

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* this represents leaf nodes aka no children */
2+
.node circle {
3+
fill: #2e3634;
4+
}
5+
.node text {
6+
font: 15px sans-serif;
7+
}
8+
9+
/* this represents those nodes that have children */
10+
.node--internal circle {
11+
fill: teal;
12+
}
13+
/* modifies text of parent nodes (has children) */
14+
.node--internal text {
15+
fill: white;
16+
}
17+
.link {
18+
fill: none;
19+
stroke: #bfe3da;
20+
stroke-opacity: 0.2;
21+
stroke-width: 50px;
22+
}

src/app/styles/main.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
@import './components/rc-slider', './components/sliderHandle';
1919

2020
// d3 chart component
21-
@import './components/d3Tree';
21+
@import './components/d3graph.css';
2222

2323
// diff component
2424
@import './components/diff';

0 commit comments

Comments
 (0)