Skip to content

Commit a7cdb76

Browse files
authored
Merge pull request #23 from oslabs-beta/d3-implementation
D3 implementation
2 parents 71591e3 + 5cfe1b1 commit a7cdb76

File tree

10 files changed

+244
-32
lines changed

10 files changed

+244
-32
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ src/extension/build.zip
99
src/extension/build.crx
1010
src/extension/build/key.pem
1111
package/__tests__
12+
bower_components

assets/reactime with website svg.svg

Lines changed: 38 additions & 0 deletions
Loading

assets/readme-logo-svg.svg

Lines changed: 38 additions & 0 deletions
Loading

package-lock.json

Lines changed: 33 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@
5959
"webpack-cli": "^3.3.6"
6060
},
6161
"dependencies": {
62-
"acorn": "^7.1.0",
63-
"acorn-jsx": "^5.0.2",
62+
"bower": "^1.8.8",
6463
"d3": "^4.13.0",
64+
"d3-tip": "^0.9.1",
65+
"d3-zoom": "^1.8.3",
6566
"immer": "^3.2.0",
6667
"jsondiffpatch": "^0.3.11",
6768
"prop-types": "^15.7.2",

src/app/components/Chart.jsx

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import React, { Component } from 'react';
66
import PropTypes from 'prop-types';
77
import * as d3 from 'd3';
8+
import d3Tip from "d3-tip";
89

910
let root = {};
1011
class Chart extends Component {
@@ -35,17 +36,31 @@ class Chart extends Component {
3536

3637
maked3Tree() {
3738
this.removed3Tree();
38-
let width = 900;
39-
let height = 1000;
39+
let width = 600;
40+
// let height = 1060;
41+
const margin = {
42+
top: 0,
43+
right: 60,
44+
bottom: 80,
45+
left: 120,
46+
};
47+
// const width = 600 - margin.right - margin.left;
48+
const height = 700 - margin.top - margin.bottom;
49+
4050
let chartContainer = d3.select(this.chartRef.current)
4151
.append('svg') // chartContainer is now pointing to svg
4252
.attr('width', width)
4353
.attr('height', height);
44-
45-
let g = chartContainer
54+
55+
chartContainer.call(d3.zoom()
56+
.on("zoom", function () {
57+
chartContainer.attr("transform", d3.event.transform)
58+
}))
4659
.append("g")
60+
61+
let g = chartContainer.append("g")
4762
// this is changing where the graph is located physically
48-
.attr("transform", `translate(${width / 2.5}, ${height / 2 + 90})`);
63+
.attr("transform", `translate(${width / 2 + 4}, ${height / 2 + 2})`);
4964

5065
// 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
5166
let radius = width / 2;
@@ -55,7 +70,8 @@ class Chart extends Component {
5570

5671
let tree = d3.tree()
5772
// this assigns width of tree to be 2pi
58-
.size([2 * Math.PI, radius])
73+
.size([2 * Math.PI, radius / 1.5])
74+
.separation(function (a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth });
5975

6076
let d3root = tree(hierarchy);
6177

@@ -74,6 +90,7 @@ class Chart extends Component {
7490
.data(d3root.descendants())
7591
.enter()
7692
.append("g")
93+
// assigning class to the node based on whether node has children or not
7794
.attr("class", function (d) {
7895
return "node" + (d.children ? " node--internal" : " node--leaf")
7996
})
@@ -82,21 +99,41 @@ class Chart extends Component {
8299
});
83100

84101
node.append("circle")
85-
.attr("r", 5.5)
102+
.attr("r", 12)
103+
104+
//creating a d3.tip method where the html has a function that returns the data we passed into tip.show from line 120
105+
let tip = d3Tip()
106+
.attr("class", "d3-tip")
107+
.html(function (d) { return "State: " + d; })
108+
109+
//invoking tooltip for nodes
110+
node.call(tip)
86111

87112
node
88113
.append("text")
89-
.attr("dy", "0.1em")
114+
// adjusts the y coordinates for the node text
115+
.attr("dy", "0.35em")
90116
.attr("x", function (d) {
91117
// this positions how far the text is from leaf nodes (ones without children)
92-
return d.x < Math.PI === !d.children ? 10 : -10;
118+
// negative number before the colon moves the text of rightside nodes, positive number moves the text for the leftside nodes
119+
return d.x < Math.PI === !d.children ? -4 : 5;
93120
})
94121
.attr("text-anchor", function (d) { return d.x < Math.PI === !d.children ? "start" : "end"; })
95122
// this arranges the angle of the text
96-
.attr("transform", function (d) { return "rotate(" + (d.x < Math.PI ? d.x - Math.PI / 2 : d.x + Math.PI / 2) * 180 / Math.PI + ")"; })
123+
.attr("transform", function (d) { return "rotate(" + (d.x < Math.PI ? d.x - Math.PI / 2 : d.x + Math.PI / 2) * 1 / Math.PI + ")"; })
97124
.text(function (d) {
98-
return "state" + d.data.index;
125+
return d.data.index;
99126
});
127+
128+
//applying tooltip on mouseover and removes it when mouse cursor moves away
129+
node
130+
.on('mouseover', function (d) {
131+
// without JSON.stringify, data will display as object Object
132+
// console.log('d.data --> ', JSON.stringify(d.data))
133+
let displayedState;
134+
tip.show(JSON.stringify(d.data.stateSnapshot.children[0].state), this)
135+
})
136+
.on('mouseout', tip.hide)
100137

101138
function reinfeldTidierAlgo(x, y) {
102139
return [(y = +y) * Math.cos(x -= Math.PI / 2), y * Math.sin(x)];

src/app/components/ChartOLD.jsx renamed to src/app/components/ChartLEGACY.jsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ class Chart extends Component {
107107
.on('mouseover', mouseover)
108108
.on('mouseout', mouseout)
109109
.on('mousemove', d => mousemove(d));
110+
111+
function mousemove(d) {
112+
div
113+
.text(!d.state ? 'No state found' : JSON.stringify(d.state, null, 4))
114+
}
110115

111116
nodeEnter
112117
.append('circle')
@@ -228,13 +233,6 @@ class Chart extends Component {
228233
.style('display', 'none');
229234
}
230235

231-
function mousemove(d) {
232-
div
233-
.text(!d.state ? 'No state found' : JSON.stringify(d.state, null, 4))
234-
.style('left', `${d3.event.pageX}px`)
235-
.style('top', `${d3.event.pageY}px`);
236-
}
237-
238236
update(root);
239237
duration = 750;
240238

src/app/styles/components/_d3Tree.css renamed to src/app/styles/components/_d3TreeLEGACY.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+
} */

0 commit comments

Comments
 (0)