Skip to content

Commit db1f692

Browse files
committed
zoom fix for Chart.tsx and Map.tsx
1 parent d5b8aad commit db1f692

File tree

2 files changed

+36
-43
lines changed

2 files changed

+36
-43
lines changed

src/app/components/Chart.tsx

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ class Chart extends Component {
8484
// };
8585
const width = 600; // - margin.right - margin.left;
8686
const height = 600; // 700 - margin.top - margin.bottom;
87-
const chartContainer = d3
87+
const svgContainer = d3
8888
.select(this.chartRef.current)
89-
.append('svg') // chartContainer is now pointing to svg
89+
.append('svg') // svgContainer is now pointing to svg
9090
.attr('width', width)
9191
.attr('height', height);
9292

93-
const g = chartContainer
93+
const g = svgContainer
9494
.append('g')
9595
// this is changing where the graph is located physically
9696
.attr('transform', `translate(${width / 2 + 4}, ${height / 2 + 2})`);
@@ -159,21 +159,6 @@ class Chart extends Component {
159159
return colors[indexColors];
160160
})
161161
.attr('class', 'node--internal')
162-
// })
163-
// assigning class to the node based on whether node has children or not
164-
// .attr('class', function (d) {
165-
// return 'node' + (d.children ? ' node--internal' : ' node--leaf');
166-
// })
167-
// .style('fill', function (d) {
168-
169-
// if (loadTime!== undefined ){
170-
171-
// if (loadTime > 16){
172-
// return '#ff0000'
173-
// }
174-
// }
175-
176-
// })
177162
.attr('transform', function (d: { x: number; y: number }) {
178163
return 'translate(' + reinfeldTidierAlgo(d.x, d.y) + ')';
179164
});
@@ -258,17 +243,24 @@ class Chart extends Component {
258243
.on('end', dragended)
259244
);
260245

261-
chartContainer.call(
246+
// d3 zoom functionality
247+
let zoom = d3.zoom().on('zoom', zoomed);
248+
svgContainer.call(
249+
zoom.transform,
250+
// Changes the initial view, (left, top)
251+
d3.zoomIdentity.translate(width/2, height/2).scale(1)
252+
);
253+
// allows the canvas to be zoom-able
254+
svgContainer.call(
262255
d3
263256
.zoom()
264-
.extent([
265-
[0, 0],
266-
[width, height],
267-
])
268-
.scaleExtent([0, 8]) // scaleExtent([minimum scale factor, maximum scale factor])
257+
.scaleExtent([0, 0.9]) // [zoomOut, zoomIn]
269258
.on('zoom', zoomed)
270259
);
271-
260+
// helper function that allows for zooming
261+
function zoomed(d: any) {
262+
g.attr('transform', d3.event.transform);
263+
}
272264
function dragstarted() {
273265
d3.select(this).raise();
274266
g.attr('cursor', 'grabbing');
@@ -284,10 +276,6 @@ class Chart extends Component {
284276
g.attr('cursor', 'grab');
285277
}
286278

287-
function zoomed() {
288-
g.attr('transform', d3.event.transform);
289-
}
290-
291279
// define the div for the tooltip
292280
const tooltipDiv = d3
293281
.select('body')

src/app/components/Map.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,24 @@ const Map = (props) => {
142142
);
143143

144144
// allows the canvas to be zoom-able
145-
svgContainer.call(
146-
d3
147-
.zoom()
148-
.extent([
149-
[0, 0],
150-
[width, height],
151-
])
152-
.scaleExtent([0, 5])
153-
.on('zoom', zoomed)
154-
);
145+
// d3 zoom functionality
146+
let zoom = d3.zoom().on('zoom', zoomed);
147+
svgContainer.call(
148+
zoom.transform,
149+
// Changes the initial view, (left, top)
150+
d3.zoomIdentity.translate(150, 250).scale(0.2),
151+
);
152+
// allows the canvas to be zoom-able
153+
svgContainer.call(
154+
d3
155+
.zoom()
156+
.scaleExtent([0.05, 0.9]) // [zoomOut, zoomIn]
157+
.on('zoom', zoomed),
158+
);
159+
// helper function that allows for zooming
160+
function zoomed(d: any) {
161+
g.attr('transform', d3.event.transform);
162+
}
155163

156164
// helper functions that help with dragging functionality
157165
function dragStarted(): any {
@@ -169,10 +177,7 @@ const Map = (props) => {
169177
g.attr('cursor', 'grab');
170178
}
171179

172-
// helper function that allows for zooming
173-
function zoomed(): any {
174-
g.attr('transform', d3.event.transform);
175-
}
180+
176181
});
177182

178183
return (

0 commit comments

Comments
 (0)