Skip to content

Commit 95b34a6

Browse files
authored
Fix/set initial static graph (#26)
* Update README * Small fix on Graph/config docs description * Create function so set initial nodes positions in Sanbdox component * 🐛 fix initializeNodes method typo n -> node * Small fix on Graph/helper.jsx _buildLinkProps * Update Graph.test.js.snap. Fix regression * Sandbox improvements to easy support initial graph override configs * Update docs for staticGraph config * Add .editorconfig * Add eslint-plugin-jest. Add lint npm script for unit tests
1 parent f688b8a commit 95b34a6

File tree

9 files changed

+309
-227
lines changed

9 files changed

+309
-227
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
root = true
3+
4+
[*]
5+
end_of_line = lf
6+
insert_final_newline = true
7+
8+
[*.{js}]
9+
charset = utf-8
10+
11+
[*.js]
12+
indent_style = space
13+
indent_size = 4
14+
15+
[Makefile]
16+
indent_style = tab
17+
18+
[{package.json,package-lock.json,yarn.lock,.travis.yml}]
19+
indent_style = space
20+
indent_size = 4

.eslintrc.test.config.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = {
2+
"extends": [ "plugin:jest/recommended" ],
3+
"globals": {
4+
"document": true,
5+
"Reflect": true,
6+
"window": true
7+
},
8+
"parser": "babel-eslint",
9+
"parserOptions": {
10+
"ecmaVersion": 6,
11+
"ecmaFeatures": {
12+
"jsx": true
13+
}
14+
},
15+
"plugins": [
16+
"standard",
17+
"promise",
18+
"react",
19+
"jest"
20+
],
21+
"rules": {
22+
"jest/no-disabled-tests": "warn",
23+
"jest/no-focused-tests": "error",
24+
"jest/no-identical-title": "error",
25+
"jest/valid-expect": "error",
26+
"max-len": ["error", 180, 4, { "ignoreComments": true }]
27+
},
28+
"env": {
29+
"jest/globals": true
30+
}
31+
};

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# react-d3-graph · [![Build Status](https://travis-ci.com/danielcaldas/react-d3-graph.svg?token=fb6uSENok5Y3gSSi5yjE&branch=master)](https://travis-ci.com/danielcaldas/react-d3-graph) [![npm version](https://img.shields.io/badge/npm-v0.2.1-blue.svg)](https://www.npmjs.com/package/react-d3-graph) [![npm stats](https://img.shields.io/badge/downloads->600-brightgreen.svg)](https://npm-stat.com/) [![probot enabled](https://img.shields.io/badge/probot:stale-enabled-yellow.svg)](https://probot.github.io/)
2+
[:book:](https://danielcaldas.github.io/react-d3-graph/docs/index.html)
23

34
### *Interactive and configurable graphs with react and d3 effortlessly*
45

56
![react-d3-graph gif sample](https://github.com/danielcaldas/react-d3-graph/blob/master/sandbox/rd3g.gif?raw=true)
67

78
## Playground
8-
Here a live playground (https://danielcaldas.github.io/react-d3-graph/sandbox/index.html) page where you can interactively config your own graph, and generate a ready to use configuration! :sunglasses:
9+
[Here a live playground](https://danielcaldas.github.io/react-d3-graph/sandbox/index.html) page where you can interactively config your own graph, and generate a ready to use configuration! :sunglasses:
910

10-
## Documentation
11+
## Documentation :book:
1112
Full documentation [here](https://danielcaldas.github.io/react-d3-graph/docs/index.html).
1213

1314
## Compatibility

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"docs": "npm run docs:lint && node_modules/documentation/bin/documentation.js build src/**/*.js -f html -o gen-docs && node_modules/documentation/bin/documentation.js build src/**/*.js -f md > gen-docs/DOCUMENTATION.md",
1515
"docs:lint": "node_modules/documentation/bin/documentation.js lint src/**/*.js",
1616
"docs:watch": "node_modules/documentation/bin/documentation.js build src/**/*.js -f html -o gen-docs --watch",
17-
"lint": "node_modules/eslint/bin/eslint.js --config=.eslintrc.js \"src/**/*.js*\"",
17+
"lint": "npm run lint:src && npm run lint:test",
18+
"lint:src": "node_modules/eslint/bin/eslint.js --config=.eslintrc.js \"src/**/*.js*\"",
19+
"lint:test": "node_modules/eslint/bin/eslint.js --config=.eslintrc.test.config.js \"test/**/*.test.js\"",
1820
"test": "jest --verbose --coverage",
1921
"test:clean": "jest --no-cache --updateSnapshot --verbose --coverage",
2022
"test:watch": "jest --verbose --watchAll"
@@ -39,6 +41,7 @@
3941
"documentation": "5.3.2",
4042
"eslint": "3.18.0",
4143
"eslint-config-recommended": "1.5.0",
44+
"eslint-plugin-jest": "21.2.0",
4245
"eslint-plugin-promise": "3.5.0",
4346
"eslint-plugin-standard": "2.1.1",
4447
"html-webpack-plugin": "2.30.1",

sandbox/Sandbox.jsx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class Sandbox extends React.Component {
2929
this.uiSchema = uiSchema;
3030

3131
this.state = {
32-
config: defaultConfig,
32+
config: {}, // Override initial configs here. e.g: {staticGraph: true}
3333
generatedConfig: {},
3434
schema,
3535
data
@@ -121,7 +121,7 @@ export default class Sandbox extends React.Component {
121121

122122
/**
123123
* ==============================================================
124-
* The methods below (**besides render method) is Sandbox specific it will not
124+
* The methods below (besides render method) is Sandbox specific it will not
125125
* be usefull in any way to your application in terms
126126
* of integrating react-d3-graph in your app.
127127
* ==============================================================
@@ -185,10 +185,35 @@ export default class Sandbox extends React.Component {
185185
});
186186
}
187187

188+
/**
189+
* This function decorates nodes and links with positions. The motivation
190+
* for this function its to set `config.staticGraph` to true on the first render
191+
* call, and to get nodes and links statically set to their initial positions.
192+
* @param {Object} nodes nodes and links with minimalist structure.
193+
* @return {Object} the graph where now nodes containing (x,y) coords.
194+
*/
195+
decorateGraphNodesWithInitialPositioning = (nodes) => {
196+
return nodes.map(n => ({
197+
id: n.id,
198+
x: Math.floor(Math.random() * 500),
199+
y: Math.floor(Math.random() * 500),
200+
symbolType: n.symbolType || 'circle'
201+
}));
202+
}
203+
188204
render() {
205+
// This does not happens in this sandbox scenario running time, but if we set staticGraph config
206+
// to true in the constructor
207+
const data = this.state.config.staticGraph ?
208+
{
209+
nodes: this.decorateGraphNodesWithInitialPositioning(this.state.data.nodes),
210+
links: this.state.data.links
211+
}
212+
: this.state.data;
213+
189214
const graphProps = {
190215
id: 'graph',
191-
data: this.state.data,
216+
data,
192217
config: this.state.config,
193218
onClickNode: this.onClickNode,
194219
onClickLink: this.onClickLink,
@@ -200,6 +225,7 @@ export default class Sandbox extends React.Component {
200225
cursor: this.state.config.staticGraph ? 'not-allowed' : 'pointer'
201226
};
202227

228+
// @TODO: Only show configs that differ from default ones in "Your config" box
203229
return (
204230
<div className='container'>
205231
<div className='container__graph'>

src/components/Graph/config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@
3939
* @param {boolean} [panAndZoom=false] - 🚅🚅🚅 pan and zoom effect when performing zoom in the graph,
4040
* a similar functionality may be consulted {@link https://bl.ocks.org/mbostock/2a39a768b1d4bc00a09650edef75ad39|here}.
4141
* @param {boolean} [staticGraph=false] - when setting this value to true the graph will be completely static, thus
42-
* all forces and drag events upon nodes will be disabled. **Plus**, if this value is true the nodes will be rendered
43-
* in the provided **x and y coordinates**, no coordinates will be calculated by react-d3-graph.
42+
* all forces and drag events upon nodes will produce not effect. Note that, if this value is true the nodes will be
43+
* rendered with the initial provided **x and y coordinates** (links positions will be automatically set
44+
* from the given nodes positions by rd3g), no coordinates will be calculated by rd3g or subjacent d3 modules.
4445
* @param {number} [width=800] - the width of the (svg) area where the graph will be rendered.
4546
* <br/>
4647
* @param {Object} node node object is explained in next section.

src/components/Graph/helper.jsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ import Node from '../Node/';
3131
* @memberof Graph/helper
3232
*/
3333
function _buildLinkProps(source, target, nodes, links, config, linkCallbacks, someNodeHighlighted, transform) {
34-
const x1 = nodes[source] && nodes[source].x || '0';
35-
const y1 = nodes[source] && nodes[source].y || '0';
36-
const x2 = nodes[target] && nodes[target].x || '0';
37-
const y2 = nodes[target] && nodes[target].y || '0';
34+
const x1 = nodes[source] && nodes[source].x || 0;
35+
const y1 = nodes[source] && nodes[source].y || 0;
36+
const x2 = nodes[target] && nodes[target].x || 0;
37+
const y2 = nodes[target] && nodes[target].y || 0;
3838

3939
let opacity = config.link.opacity;
4040

@@ -304,8 +304,8 @@ function initializeNodes(graphNodes) {
304304

305305
node['highlighted'] = false;
306306

307-
if (!n.hasOwnProperty('x')) { node['x'] = 0; }
308-
if (!n.hasOwnProperty('y')) { node['y'] = 0; }
307+
if (!node.hasOwnProperty('x')) { node['x'] = 0; }
308+
if (!node.hasOwnProperty('y')) { node['y'] = 0; }
309309

310310
nodes[node.id.toString()] = node;
311311
nodeIndexMapping[i] = node.id;

test/Graph.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import renderer from 'react-test-renderer';
33

44
import Graph from '../src/components/Graph';
5-
import graphMock from './graph.mock.js'
5+
import graphMock from './graph.mock.js';
66

77
describe('Graph Component', () => {
88
let that = {};

0 commit comments

Comments
 (0)