Skip to content

Commit e7a1265

Browse files
committed
fixed dragging so that it is smoother without the bugginess
1 parent 60fbbd7 commit e7a1265

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/app/components/Chart.jsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,6 @@ class Chart extends Component {
5151
.append('svg') // chartContainer is now pointing to svg
5252
.attr('width', width)
5353
.attr('height', height);
54-
55-
chartContainer.call(d3.zoom()
56-
.on("zoom", function () {
57-
chartContainer.attr("transform", d3.event.transform)
58-
}))
59-
.append("g")
6054

6155
let g = chartContainer.append("g")
6256
// this is changing where the graph is located physically
@@ -70,7 +64,7 @@ class Chart extends Component {
7064

7165
let tree = d3.tree()
7266
// this assigns width of tree to be 2pi
73-
.size([2 * Math.PI, radius / 1.5])
67+
.size([2 * Math.PI, radius / 1.4])
7468
.separation(function (a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth });
7569

7670
let d3root = tree(hierarchy);
@@ -99,7 +93,7 @@ class Chart extends Component {
9993
});
10094

10195
node.append("circle")
102-
.attr("r", 12)
96+
.attr("r", 10)
10397

10498
//creating a d3.tip method where the html has a function that returns the data we passed into tip.show from line 120
10599
let tip = d3Tip()
@@ -125,6 +119,34 @@ class Chart extends Component {
125119
return d.data.index;
126120
});
127121

122+
// allows svg to be dragged around
123+
node.call(d3.drag()
124+
.on("start", dragstarted)
125+
.on("drag", dragged)
126+
.on("end", dragended));
127+
128+
chartContainer.call(d3.zoom()
129+
.extent([[0, 0], [width, height]])
130+
.scaleExtent([1, 8])
131+
.on("zoom", zoomed));
132+
133+
function dragstarted() {
134+
d3.select(this).raise();
135+
g.attr("cursor", "grabbing");
136+
}
137+
138+
function dragged(d) {
139+
d3.select(this).attr("dx", d.x = d3.event.x).attr("dy", d.y = d3.event.y);
140+
}
141+
142+
function dragended() {
143+
g.attr("cursor", "grab");
144+
}
145+
146+
function zoomed() {
147+
g.attr("transform", d3.event.transform);
148+
}
149+
128150
//applying tooltip on mouseover and removes it when mouse cursor moves away
129151
node
130152
.on('mouseover', function (d) {

src/app/styles/components/d3graph.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ body {
1818
}
1919

2020
.node text {
21-
font-size: 12px;
21+
font-size: 10px;
2222
font-family: "Overpass Mono", monospace;
2323
}
2424
/* this represents text for leaf nodes aka the ones with no children */
@@ -33,7 +33,7 @@ body {
3333
/* modifies text of parent nodes (has children) */
3434
.node--internal text {
3535
fill: #fae6e4;
36-
font-size: 12px;
36+
font-size: 11px;
3737
}
3838
.link {
3939
fill: none;

0 commit comments

Comments
 (0)