|
1 | 1 | import React from 'react'; |
2 | 2 |
|
3 | | -import * as d3 from 'd3'; |
| 3 | +import { drag as d3Drag } from 'd3-drag'; |
| 4 | +import { forceLink as d3ForceLink } from 'd3-force'; |
| 5 | +import { |
| 6 | + select as d3Select, |
| 7 | + selectAll as d3SelectAll, |
| 8 | + event as d3Event |
| 9 | +} from 'd3-selection'; |
| 10 | +import { zoom as d3Zoom } from 'd3-zoom'; |
4 | 11 |
|
5 | 12 | import CONST from './const'; |
6 | 13 | import DEFAULT_CONFIG from './config'; |
@@ -88,8 +95,8 @@ export default class Graph extends React.Component { |
88 | 95 | // This is where d3 and react bind |
89 | 96 | let draggedNode = this.state.nodes[this.state.nodeIndexMapping[index]]; |
90 | 97 |
|
91 | | - draggedNode.x += d3.event.dx; |
92 | | - draggedNode.y += d3.event.dy; |
| 98 | + draggedNode.x += d3Event.dx; |
| 99 | + draggedNode.y += d3Event.dy; |
93 | 100 |
|
94 | 101 | // Set nodes fixing coords fx and fy |
95 | 102 | draggedNode['fx'] = draggedNode.x; |
@@ -134,18 +141,18 @@ export default class Graph extends React.Component { |
134 | 141 | * {@link https://github.com/d3/d3-zoom#zoom} |
135 | 142 | * @return {undefined} |
136 | 143 | */ |
137 | | - _zoomConfig = () => d3.select(`#${this.state.id}-${CONST.GRAPH_WRAPPER_ID}`) |
138 | | - .call(d3.zoom().scaleExtent([this.state.config.minZoom, this.state.config.maxZoom]) |
| 144 | + _zoomConfig = () => d3Select(`#${this.state.id}-${CONST.GRAPH_WRAPPER_ID}`) |
| 145 | + .call(d3Zoom().scaleExtent([this.state.config.minZoom, this.state.config.maxZoom]) |
139 | 146 | .on('zoom', this._zoomed)); |
140 | 147 |
|
141 | 148 | /** |
142 | 149 | * Handler for 'zoom' event within zoom config. |
143 | 150 | * @return {Object} returns the transformed elements within the svg graph area. |
144 | 151 | */ |
145 | 152 | _zoomed = () => { |
146 | | - const transform = d3.event.transform; |
| 153 | + const transform = d3Event.transform; |
147 | 154 |
|
148 | | - d3.selectAll(`#${this.state.id}-${CONST.GRAPH_CONTAINER_ID}`).attr('transform', transform); |
| 155 | + d3SelectAll(`#${this.state.id}-${CONST.GRAPH_CONTAINER_ID}`).attr('transform', transform); |
149 | 156 |
|
150 | 157 | this.state.config.panAndZoom && this.setState({ transform: transform.k }); |
151 | 158 | } |
@@ -278,19 +285,19 @@ export default class Graph extends React.Component { |
278 | 285 | _graphForcesConfig() { |
279 | 286 | this.state.simulation.nodes(this.state.d3Nodes).on('tick', this._tick); |
280 | 287 |
|
281 | | - const forceLink = d3.forceLink(this.state.d3Links) |
| 288 | + const forceLink = d3ForceLink(this.state.d3Links) |
282 | 289 | .id(l => l.id) |
283 | 290 | .distance(CONST.LINK_IDEAL_DISTANCE) |
284 | 291 | .strength(1); |
285 | 292 |
|
286 | 293 | this.state.simulation.force(CONST.LINK_CLASS_NAME, forceLink); |
287 | 294 |
|
288 | | - const customNodeDrag = d3.drag() |
| 295 | + const customNodeDrag = d3Drag() |
289 | 296 | .on('start', this._onDragStart) |
290 | 297 | .on('drag', this._onDragMove) |
291 | 298 | .on('end', this._onDragEnd); |
292 | 299 |
|
293 | | - d3.select(`#${this.state.id}-${CONST.GRAPH_WRAPPER_ID}`).selectAll('.node').call(customNodeDrag); |
| 300 | + d3Select(`#${this.state.id}-${CONST.GRAPH_WRAPPER_ID}`).selectAll('.node').call(customNodeDrag); |
294 | 301 | } |
295 | 302 |
|
296 | 303 | constructor(props) { |
|
0 commit comments