Skip to content

Commit 9d2c59e

Browse files
committed
drag very buggy
1 parent c31d450 commit 9d2c59e

File tree

1 file changed

+32
-42
lines changed

1 file changed

+32
-42
lines changed

src/app/components/Map.tsx

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,26 @@ const Map = (props) => {
1212
const { snapshot, snapshots } = props;
1313
const lastSnap = snapshots.length - 1;
1414

15-
let width = 1800;
16-
let height = 900;
17-
let margin = { top: 10, right: 120, bottom: 10, left: 120 };
18-
let dy = 100;
19-
let dx = 76;
20-
let data = snapshots[lastSnap];
21-
let tree = d3.tree().nodeSize([dx, dy]);
22-
let diagonal = d3
15+
const width = 2000;
16+
const height = 600;
17+
const margin = { top: 10, right: 120, bottom: 10, left: 120 };
18+
const dy = 100;
19+
const dx = 76;
20+
const data = snapshots[lastSnap];
21+
const tree = d3.tree().nodeSize([dx, dy]);
22+
const diagonal = d3
2323
.linkHorizontal()
2424
.x((d) => d.y)
2525
.y((d) => d.x);
2626

2727

2828

2929
useEffect(() => {
30-
30+
document.getElementById('canvas').innerHTML = '_';
3131
return makeChart();
3232
});
3333

3434
const makeChart = () => {
35-
document.getElementById('canvas').innerHTML = '';
3635

3736
const root = d3.hierarchy(data);
3837

@@ -41,23 +40,13 @@ const Map = (props) => {
4140
root.descendants().forEach((d, i) => {
4241
d.id = i;
4342
d._children = d.children;
44-
// if (d.depth && d.data.name.length !== 7) d.children = null;
43+
if (d.depth === 9) d.children = null;
4544
});
4645

4746
console.log("root", root)
48-
// const svgContainer: any = d3
49-
// .select('#canvas')
50-
// .attr('width', width)
51-
// .attr('height', height);
52-
53-
// const svg = d3
54-
// .create('svg')
55-
// .attr('viewBox', [-margin.left, -margin.top, width, dx])
56-
// .style('font', '10px sans-serif')
57-
// .style('user-select', 'none');
58-
47+
5948
const svg = d3
60-
.select('#canvas')
49+
.select('#canvas')
6150
.attr('width', width)
6251
.attr('height', height)
6352
// .attr('viewBox', [-margin.left, -margin.top, width, dx])
@@ -68,7 +57,7 @@ const Map = (props) => {
6857
.append('g')
6958
.attr('fill', 'none')
7059
.attr('stroke', '#555')
71-
.attr('stroke-opacity', 0.4)
60+
.attr('stroke-opacity', 0.9)
7261
.attr('stroke-width', 1.5);
7362

7463
const gNode = svg
@@ -92,7 +81,7 @@ const Map = (props) => {
9281
if (node.x > right.x) right = node;
9382
});
9483

95-
const height = right.x - left.x + margin.top + margin.bottom;
84+
const height = right.x - left.x + margin.top + margin.bottom;
9685

9786
const transition = svg
9887
.transition()
@@ -109,7 +98,7 @@ const Map = (props) => {
10998
.append('g')
11099
.attr('transform', (d) => `translate(${source.y0},${source.x0})`)
111100
.attr('fill-opacity', 0)
112-
.attr('stroke-linejoin', 'round')
101+
// .attr('stroke-linejoin', 'round')
113102
.attr('stroke-opacity', 1)
114103
.on('click', (d) => {
115104
d.children = d.children ? null : d._children;
@@ -118,10 +107,11 @@ const Map = (props) => {
118107

119108
nodeEnter
120109
.append('circle')
121-
.attr('r', 15)
110+
.attr('r', 10)
122111
.attr('fill', (d) => (d._children ? '#46edf2': '#95B6B7' ))
123-
.attr('stroke-linejoin', 'round')
124-
.attr('stroke-width', 10);
112+
//.attr('stroke-linejoin', 'round')
113+
.attr('stroke-width', 10)
114+
.attr('stroke-opacity', 1);
125115

126116

127117
nodeEnter
@@ -130,15 +120,15 @@ const Map = (props) => {
130120
.attr('x', (d: any) => (d.children ? -50 : 50))
131121
.attr('text-anchor', (d: any) => (d.children ? 'end' : 'start'))
132122
.text((d: any) => d.data.name)
133-
.style('font-size', `1rem`)
123+
.style('font-size', `.8rem`)
134124
.style('fill', 'white')
135125
.clone(true)
136126
.lower()
127+
.attr("stroke-linejoin", "round")
137128
.attr('stroke', '#646464')
138129
.attr('stroke-width', 2);
139130

140131

141-
console.log("119")
142132
// Transition nodes to their new position.
143133
const nodeUpdate = node
144134
.merge(nodeEnter)
@@ -163,7 +153,7 @@ const Map = (props) => {
163153
const linkEnter = link
164154
.enter()
165155
.append('path')
166-
.selectAll('path')
156+
//.selectAll('path')
167157
.attr('d', (d) => {
168158
const o = { x: source.x0, y: source.y0 };
169159
return diagonal({ source: o, target: o });
@@ -187,6 +177,9 @@ const Map = (props) => {
187177
d.x0 = d.x;
188178
d.y0 = d.y;
189179
});
180+
181+
}
182+
190183
//______________ZOOM______________\\
191184

192185
// Sets starting zoom but breaks keeping currents zoom on state change
@@ -198,20 +191,19 @@ const Map = (props) => {
198191
// d3.zoomIdentity.translate(150, 250).scale(0.2)
199192
// );
200193

201-
// allows the canvas to be zoom-able
194+
// allows the canvas to be zoom-able
202195
svg.call(
203196
d3
204197
.zoom()
205-
.scaleExtent([0.05, 0.9]) // [zoomOut, zoomIn]
198+
.scaleExtent([0.15, 1.5]) // [zoomOut, zoomIn]
206199
.on('zoom', zoomed)
207200
);
208201
function zoomed(d: any) {
209202
svg.attr('transform', d3.event.transform);
210203
}
211204

212-
//_____________DRAG_____________\\
213-
// allows the canvas to be draggable
214-
svg.call(
205+
// allows the canvas to be draggable
206+
svg.call(
215207
d3
216208
.drag()
217209
.on('start', dragStarted)
@@ -221,7 +213,7 @@ const Map = (props) => {
221213

222214
function dragStarted(): any {
223215
d3.select(this).raise();
224-
svg.attr('cursor', 'grabbing');
216+
svg.attr('cursor', 'grabbing');
225217
}
226218
function dragged(d: any): any {
227219
d3.select(this)
@@ -231,8 +223,8 @@ const Map = (props) => {
231223
function dragEnded(): any {
232224
svg.attr('cursor', 'grab');
233225
}
234-
}
235-
226+
227+
236228

237229
update(root);
238230
};
@@ -390,12 +382,10 @@ const Map = (props) => {
390382
// }
391383
// });
392384

393-
console.log("Return")
394385
return (
395386
<div data-testid="canvas">
396387

397388
<svg id="canvas"></svg>
398-
hi
399389
</div>
400390

401391
);

0 commit comments

Comments
 (0)