Skip to content

Commit e2f8352

Browse files
authored
Refactor/d3 tree shaking (#16)
* Tree shake import in Node/helper.js * d3-force tree-shaking on Graph/helper.jsx * Many d3 module tree shaking in Graph/index.jsx * Always add prefix d3 in imports. Organize imports order
1 parent aabd93e commit e2f8352

File tree

3 files changed

+49
-28
lines changed

3 files changed

+49
-28
lines changed

src/components/Graph/helper.jsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@
55
*/
66
import React from 'react';
77

8-
import * as d3 from 'd3';
8+
import {
9+
forceX as d3ForceX,
10+
forceY as d3ForceY,
11+
forceSimulation as d3ForceSimulation,
12+
forceManyBody as d3ForceManyBody
13+
} from 'd3-force';
914

1015
import CONST from './const';
1116

@@ -206,13 +211,13 @@ function buildGraph(nodes, nodeCallbacks, links, linkCallbacks, config, someNode
206211
* @memberof Graph/helper
207212
*/
208213
function createForceSimulation(width, height) {
209-
const forceX = d3.forceX(width / 2).strength(CONST.FORCE_X);
210-
const forceY = d3.forceY(height / 2).strength(CONST.FORCE_Y);
214+
const frx = d3ForceX(width / 2).strength(CONST.FORCE_X);
215+
const fry = d3ForceY(height / 2).strength(CONST.FORCE_Y);
211216

212-
const simulation = d3.forceSimulation()
213-
.force('charge', d3.forceManyBody().strength(CONST.FORCE_IDEAL_STRENGTH))
214-
.force('x', forceX)
215-
.force('y', forceY);
217+
const simulation = d3ForceSimulation()
218+
.force('charge', d3ForceManyBody().strength(CONST.FORCE_IDEAL_STRENGTH))
219+
.force('x', frx)
220+
.force('y', fry);
216221

217222
return simulation;
218223
}

src/components/Graph/index.jsx

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import React from 'react';
22

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';
411

512
import CONST from './const';
613
import DEFAULT_CONFIG from './config';
@@ -88,8 +95,8 @@ export default class Graph extends React.Component {
8895
// This is where d3 and react bind
8996
let draggedNode = this.state.nodes[this.state.nodeIndexMapping[index]];
9097

91-
draggedNode.x += d3.event.dx;
92-
draggedNode.y += d3.event.dy;
98+
draggedNode.x += d3Event.dx;
99+
draggedNode.y += d3Event.dy;
93100

94101
// Set nodes fixing coords fx and fy
95102
draggedNode['fx'] = draggedNode.x;
@@ -134,18 +141,18 @@ export default class Graph extends React.Component {
134141
* {@link https://github.com/d3/d3-zoom#zoom}
135142
* @return {undefined}
136143
*/
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])
139146
.on('zoom', this._zoomed));
140147

141148
/**
142149
* Handler for 'zoom' event within zoom config.
143150
* @return {Object} returns the transformed elements within the svg graph area.
144151
*/
145152
_zoomed = () => {
146-
const transform = d3.event.transform;
153+
const transform = d3Event.transform;
147154

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);
149156

150157
this.state.config.panAndZoom && this.setState({ transform: transform.k });
151158
}
@@ -278,19 +285,19 @@ export default class Graph extends React.Component {
278285
_graphForcesConfig() {
279286
this.state.simulation.nodes(this.state.d3Nodes).on('tick', this._tick);
280287

281-
const forceLink = d3.forceLink(this.state.d3Links)
288+
const forceLink = d3ForceLink(this.state.d3Links)
282289
.id(l => l.id)
283290
.distance(CONST.LINK_IDEAL_DISTANCE)
284291
.strength(1);
285292

286293
this.state.simulation.force(CONST.LINK_CLASS_NAME, forceLink);
287294

288-
const customNodeDrag = d3.drag()
295+
const customNodeDrag = d3Drag()
289296
.on('start', this._onDragStart)
290297
.on('drag', this._onDragMove)
291298
.on('end', this._onDragEnd);
292299

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);
294301
}
295302

296303
constructor(props) {

src/components/Node/helper.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@
33
* @description
44
* Some methods that help no the process of rendering a node.
55
*/
6-
import * as d3 from 'd3';
6+
import {
7+
symbolCircle as d3SymbolCircle,
8+
symbolCross as d3SymbolCross,
9+
symbolDiamond as d3SymbolDiamond,
10+
symbolSquare as d3SymbolSquare,
11+
symbolStar as d3SymbolStar,
12+
symbolTriangle as d3SymbolTriangle,
13+
symbolWye as d3SymbolWye,
14+
symbol as d3Symbol
15+
} from 'd3-shape';
716

817
import CONST from './const';
918

@@ -18,21 +27,21 @@ import CONST from './const';
1827
function _convertTypeToD3Symbol(typeName) {
1928
switch (typeName) {
2029
case CONST.SYMBOLS.CIRCLE:
21-
return d3.symbolCircle;
30+
return d3SymbolCircle;
2231
case CONST.SYMBOLS.CROSS:
23-
return d3.symbolCross;
32+
return d3SymbolCross;
2433
case CONST.SYMBOLS.DIAMOND:
25-
return d3.symbolDiamond;
34+
return d3SymbolDiamond;
2635
case CONST.SYMBOLS.SQUARE:
27-
return d3.symbolSquare;
36+
return d3SymbolSquare;
2837
case CONST.SYMBOLS.STAR:
29-
return d3.symbolStar;
38+
return d3SymbolStar;
3039
case CONST.SYMBOLS.TRIANGLE:
31-
return d3.symbolTriangle;
40+
return d3SymbolTriangle;
3241
case CONST.SYMBOLS.WYE:
33-
return d3.symbolWye;
42+
return d3SymbolWye;
3443
default:
35-
return d3.symbolCircle;
44+
return d3SymbolCircle;
3645
}
3746
}
3847

@@ -45,9 +54,9 @@ function _convertTypeToD3Symbol(typeName) {
4554
* @memberof Node/helper
4655
*/
4756
function buildSvgSymbol(size=80, symbolTypeDesc=CONST.SYMBOLS.CIRCLE) {
48-
return d3.symbol()
57+
return d3Symbol()
4958
.size(() => size)
50-
.type(() => _convertTypeToD3Symbol(symbolTypeDesc))(); // @todo: Strange behavior d3.Symbol ret function
59+
.type(() => _convertTypeToD3Symbol(symbolTypeDesc))(); // @todo: Strange behavior Symbol ret function
5160
}
5261

5362
export default {

0 commit comments

Comments
 (0)