Skip to content

Commit 889044a

Browse files
authored
Misc improvements cleanup from previous PRs (#327)
* Misc improvements cleanup from previous PRs - Revert DOCUMENTATION.md to previous version - Adjustments in documentation - Add copy to clipboard button to sandbox for graph configuration field - Remove eslint rule new-line-after-var - Small refactor around graph.builder and graph.helper - Small refactor marker.helper * Small improvements in sandbox improve copy to clipboard * Fix unit test * Add sample for new size API @terahn #342 * Allow users to select the node.labelPosition in the form * Fix #322 * Fix #323
1 parent 3e1af4a commit 889044a

File tree

12 files changed

+136
-80
lines changed

12 files changed

+136
-80
lines changed

.eslintrc.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ module.exports = {
2929
"keyword-spacing": "error",
3030
"max-len": ["error", 120, 4, { ignoreComments: true }],
3131
"max-lines": ["error", { max: 450, skipComments: true }],
32-
"newline-after-var": ["error", "always"],
3332
"no-nested-ternary": "error",
3433
"no-useless-constructor": "error",
3534
semi: "error",

docs/DOCUMENTATION.md

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -394,28 +394,27 @@ const graph = {
394394
out of the box provide the look and feel of a directed graph and add directional semantic to links. You can see a sample in the image below.
395395
</br>
396396
<img src="https://github.com/danielcaldas/react-d3-graph/blob/master/docs/rd3g-directed.gif?raw=true" width="820" height="480"/> (optional, default `false`)
397-
- `focusZoom` **[number][96]** zoom that will be applied when the graph view is focused in a node. Its value must be between
398-
_minZoom_ and _maxZoom_. If the specified _focusZoom_ is out of this range, _minZoom_ or _maxZoom_ will be applied instead.
399-
**NOTE:** This animation is not trigger by default. In order to trigger it you need to pass down to `react-d3-graph` the
400-
node that you want to focus via prop `focusedNodeId` along side with nodes and links:`javascript const data = { nodes: this.state.data.nodes, links: this.state.data.links, focusedNodeId: "nodeIdToTriggerZoomAnimation" };`<img src="https://github.com/danielcaldas/react-d3-graph/blob/master/docs/rd3g-zoom-animation.gif?raw=true" width="820" height="480"/> (optional, default `1`)
401-
- `focusAnimationDuration` **[number][96]** duration (in seconds) for the animation that takes place when focusing the graph on a node. (optional, default `0.75`)
402-
- `height` **[number][96]** the height of the (svg) area where the graph will be rendered. (optional, default `400`)
403-
- `nodeHighlightBehavior` **[boolean][97]** 🚅🚅🚅 when user mouse hovers a node that node and adjacent common
404-
connections will be highlighted (depending on the _highlightDegree_ value). All the remaining nodes and links assume opacity value equal to **highlightOpacity**. (optional, default `false`)
405-
- `linkHighlightBehavior` **[boolean][97]** 🚅🚅🚅 when the user mouse hovers some link that link and the correspondent nodes will be highlighted, this is a similar behavior
406-
to _nodeHighlightBehavior_ but for links <small>(just for historical reference this property was introduced in **v1.0.0**)</small>. (optional, default `false`)
407-
- `highlightDegree` **[number][96]** **Possible values: 0, 1 or 2**. This value represents the range of the
408-
highlight behavior when some node is highlighted. If the value is set to **0** only the selected node will be
409-
highlighted. If the value is set to **1** the selected node and his 1st degree connections will be highlighted. If
410-
the value is set to **2** the selected node will be highlighted as well as the 1st and 2nd common degree connections. (optional, default `1`)
411-
- `highlightOpacity` **[number][96]** this value is used to highlight nodes in the network. The lower
412-
the value the more the less highlighted nodes will be visible (related to _nodeHighlightBehavior_). (optional, default `1`)
413-
- `maxZoom` **[number][96]** max zoom that can be performed against the graph. (optional, default `8`)
414-
- `minZoom` **[number][96]** min zoom that can be performed against the graph. (optional, default `0.1`)
415-
- `initialZoom` **[number][96]** initial zoom that can be set on the graph. (optional, default `null`)
416-
- `panAndZoom` **[boolean][97]** 🚅🚅🚅 pan and zoom effect when performing zoom in the graph,
417-
a similar functionality may be consulted [here][104]. (optional, default `false`)
418-
- `staticGraph` **[boolean][97]** when setting this value to true the graph will be completely static, thus
397+
- `focusZoom` **[number][173]** <a id="focus-zoom" href="#focus-zoom">🔗</a> zoom that will be applied when the graph view is focused in a node. Its value must be between
398+
<i>minZoom</i> and <i>maxZoom</i>. If the specified <i>focusZoom</i> is out of this range, <i>minZoom</i> or <i>maxZoom</i> will be applied instead.</br>
399+
<b>NOTE</b>: This animation is not trigger by default. In order to trigger it you need to pass down to <code>react-d3-graph</code> the
400+
node that you want to focus via prop <code>focusedNodeId</code> along side with nodes and links:`javascript const data = { nodes: this.state.data.nodes, links: this.state.data.links, focusedNodeId: "nodeIdToTriggerZoomAnimation" };`<img src="https://github.com/danielcaldas/react-d3-graph/blob/master/docs/rd3g-zoom-animation.gif?raw=true" width="820" height="480"/> (optional, default `1`)
401+
- `focusAnimationDuration` **[number][173]** <a id="focus-animation-duration" href="#focus-animation-duration">🔗</a> duration (in seconds) for the animation that takes place when focusing the graph on a node. (optional, default `0.75`)
402+
- `height` **[number][173]** <a id="height" href="#height">🔗</a> the height of the (svg) area where the graph will be rendered. (optional, default `400`)
403+
- `nodeHighlightBehavior` **[boolean][174]** <a id="node-highlight-behavior" href="#node-highlight-behavior">🔗</a> 🚅🚅🚅 when user mouse hovers a node that node and adjacent common
404+
connections will be highlighted (depending on the <i>highlightDegree</i> value). All the remaining nodes and links assume opacity value equal to <b>highlightOpacity</b>. (optional, default `false`)
405+
- `linkHighlightBehavior` **[boolean][174]** <a id="link-highlight-behavior" href="#link-highlight-behavior">🔗</a> 🚅🚅🚅 when the user mouse hovers some link that link and the correspondent nodes will be highlighted, this is a similar behavior
406+
to <i>nodeHighlightBehavior</i> but for links <small>(just for historical reference this property was introduced in <a target="_blank" href="https://github.com/danielcaldas/react-d3-graph/releases/tag/1.0.0">v1.0.0</a>)</small>. (optional, default `false`)
407+
- `highlightDegree` **[number][173]** <a id="highlight-degree" href="#highlight-degree">🔗</a> <b>Possible values: 0, 1 or 2</b>. This value represents the range of the
408+
highlight behavior when some node is highlighted. If the value is set to <b>0</b> only the selected node will be
409+
highlighted. If the value is set to <b>1</b> the selected node and his 1st degree connections will be highlighted. If
410+
the value is set to <b>2</b> the selected node will be highlighted as well as the 1st and 2nd common degree connections. (optional, default `1`)
411+
- `highlightOpacity` **[number][173]** <a id="highlight-opacity" href="#highlight-opacity">🔗</a> this value is used to highlight nodes in the network. The lower
412+
the value the more the less highlighted nodes will be visible (related to <i>nodeHighlightBehavior</i>). (optional, default `1`)
413+
- `maxZoom` **[number][173]** <a id="max-zoom" href="#max-zoom">🔗</a> max zoom that can be performed against the graph. (optional, default `8`)
414+
- `minZoom` **[number][173]** <a id="min-zoom" href="#min-zoom">🔗</a> min zoom that can be performed against the graph. (optional, default `0.1`)
415+
- `panAndZoom` **[boolean][174]** <a id="pan-and-zoom" href="#pan-and-zoom">🔗</a> 🚅🚅🚅 pan and zoom effect when performing zoom in the graph,
416+
a similar functionality may be consulted <a target="_blank" href="https://bl.ocks.org/mbostock/2a39a768b1d4bc00a09650edef75ad39">here</a>. (optional, default `false`)
417+
- `staticGraph` **[boolean][174]** <a id="static-graph" href="#static-graph">🔗</a> when setting this value to true the graph will be completely static, thus
419418
all forces and drag events upon nodes will produce not effect. Note that, if this value is true the nodes will be
420419
rendered with the initial provided <b>x and y coordinates</b> (links positions will be automatically set
421420
from the given nodes positions by rd3g), no coordinates will be calculated by rd3g or subjacent d3 modules. (optional, default `false`)

sandbox/Sandbox.jsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ export default class Sandbox extends React.Component {
3333

3434
const { config: configOverride, data, fullscreen } = sandboxData;
3535
const config = Object.assign(defaultConfig, configOverride);
36+
// TODO: refactor this labelPosition assignment, move to somewhere
37+
// in generateFormSchema
38+
if (config.node.labelPosition === null) {
39+
config.node.labelPosition = "";
40+
}
3641
const schemaProps = generateFormSchema(config, "", {});
3742
const schema = {
3843
type: "object",
@@ -351,6 +356,23 @@ export default class Sandbox extends React.Component {
351356
);
352357
};
353358

359+
copyConfigToClipboard = () => {
360+
if (!this.state.generatedConfig || !Object.keys(this.state.generatedConfig).length) {
361+
return;
362+
}
363+
364+
try {
365+
navigator.clipboard
366+
.writeText(JSON.stringify(this.state.generatedConfig, null, 2))
367+
.then(() => {
368+
toast("✔️ Configuration copied to clipboard!");
369+
})
370+
.catch(console.error);
371+
} catch (error) {
372+
console.error(error);
373+
}
374+
};
375+
354376
componentDidMount() {
355377
toast.configure();
356378
}
@@ -444,7 +466,12 @@ export default class Sandbox extends React.Component {
444466
</button>
445467
</div>
446468
<div className="container__graph-config">
447-
<h4>Your config</h4>
469+
<h4>
470+
Your config
471+
<small className="btn-clipboard" onClick={this.copyConfigToClipboard}>
472+
📋 copy to clipboard
473+
</small>
474+
</h4>
448475
<JSONContainer data={this.state.generatedConfig} staticData={false} />
449476
</div>
450477
<div className="container__graph-data">

sandbox/data/custom-node/custom-node.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ export default {
2828
mouseCursor: "pointer",
2929
opacity: 1,
3030
renderLabel: false,
31-
size: 700,
31+
size: {
32+
width: 700,
33+
height: 900,
34+
},
3235
strokeColor: "none",
3336
strokeWidth: 1.5,
3437
svg: "",

sandbox/styles.css

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
.container {
1414
display: grid;
1515
width: 100%;
16-
grid-template-columns: 450px 1fr;
16+
grid-template-columns: 380px 400px 1fr;
1717
grid-auto-rows: minmax(100px, auto);
1818
}
1919

@@ -41,14 +41,14 @@
4141
}
4242

4343
.container__graph-data {
44-
grid-column: 1 / 2;
44+
grid-column: 1 / 1;
4545
grid-row: 2 / 3;
4646
margin-bottom: 4px;
4747
z-index: 2;
4848
}
4949

5050
.container__graph-config {
51-
grid-column: 2 / 3;
51+
grid-column: 2 / 5;
5252
grid-row: 2 / 3;
5353
margin-bottom: 4px;
5454
z-index: 2;
@@ -75,7 +75,8 @@
7575
}
7676

7777
.json-data-container {
78-
max-height: 250px;
78+
height: 250px;
79+
min-width: 360px;
7980
overflow: scroll;
8081
}
8182

@@ -97,3 +98,15 @@
9798
.tooltip-help {
9899
cursor: help;
99100
}
101+
102+
.btn-clipboard {
103+
border: none;
104+
background-color: transparent;
105+
cursor: pointer;
106+
margin-left: 20px;
107+
user-select: none;
108+
}
109+
110+
.btn-clipboard:hover {
111+
background-color: lightskyblue;
112+
}

sandbox/utils.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import DEFAULT_CONFIG from "../src/components/graph/graph.config";
66
import { merge } from "../src/utils";
77
import { tooltips } from "./graph-config-tooltips";
88

9+
const LABEL_POSITION_OPTIONS = ["left", "right", "top", "bottom", "center"];
10+
911
/**
1012
* This two functions generate the react-jsonschema-form
1113
* schema from some passed graph configuration.
@@ -23,6 +25,16 @@ function formMap(k, v) {
2325
uniqueItems: true,
2426
};
2527
}
28+
case "node.labelPosition": {
29+
return {
30+
type: "array",
31+
title: "node.labelPosition",
32+
items: {
33+
enum: LABEL_POSITION_OPTIONS,
34+
},
35+
uniqueItems: true,
36+
};
37+
}
2638
}
2739

2840
return {
@@ -83,6 +95,10 @@ function loadDataset() {
8395
};
8496
}
8597

98+
function isArray(o) {
99+
return o && typeof o === "object" && Object.prototype.hasOwnProperty.call(o, "length");
100+
}
101+
86102
function setValue(obj, access, value) {
87103
if (typeof access == "string") {
88104
access = access.split(".");
@@ -93,7 +109,13 @@ function setValue(obj, access, value) {
93109
obj[access[0]] = {};
94110
}
95111

96-
access.length > 1 ? setValue(obj[access.shift()], access, value) : (obj[access[0]] = value);
112+
const v = isArray(value) ? value[0] : value;
113+
114+
if (access.length > 1) {
115+
setValue(obj[access.shift()], access, v);
116+
} else {
117+
obj[access[0]] = v;
118+
}
97119
}
98120

99121
function tooltipReducer(schemaProps, key) {

src/components/graph/Graph.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,12 @@ export default class Graph extends React.Component {
283283
.scaleExtent([this.state.config.minZoom, this.state.config.maxZoom])
284284
.on("zoom", this._zoomed);
285285

286-
// set initial zoom
287286
if (this.state.config.initialZoom !== null) {
288287
zoomObject.scaleTo(selector, this.state.config.initialZoom);
289288
}
290289

290+
// avoid double click on graph to trigger zoom
291+
// for more details consult: https://github.com/danielcaldas/react-d3-graph/pull/202
291292
selector.call(zoomObject).on("dblclick.zoom", null);
292293
};
293294

src/components/graph/graph.builder.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,9 @@ function _getNodeOpacity(node, highlightedNode, highlightedLink, config) {
5454
*/
5555
function buildLinkProps(link, nodes, links, config, linkCallbacks, highlightedNode, highlightedLink, transform) {
5656
const { source, target } = link;
57-
5857
let x1 = nodes?.[source]?.x || 0;
59-
6058
let y1 = nodes?.[source]?.y || 0;
61-
6259
let x2 = nodes?.[target]?.x || 0;
63-
6460
let y2 = nodes?.[target]?.y || 0;
6561

6662
const type = link.type || config.link.type;
@@ -218,6 +214,11 @@ function buildNodeProps(node, config, nodeCallbacks = {}, highlightedNode, highl
218214
const svg = node.svg || config.node.svg;
219215
const fontColor = node.fontColor || config.node.fontColor;
220216

217+
let renderLabel = config.node.renderLabel;
218+
if (node.renderLabel !== undefined && typeof node.renderLabel === "boolean") {
219+
renderLabel = node.renderLabel;
220+
}
221+
221222
return {
222223
...node,
223224
className: CONST.NODE_CLASS_NAME,
@@ -234,7 +235,7 @@ function buildNodeProps(node, config, nodeCallbacks = {}, highlightedNode, highl
234235
labelPosition,
235236
opacity,
236237
overrideGlobalViewGenerator: !node.viewGenerator && node.svg,
237-
renderLabel: node.renderLabel || config.node.renderLabel,
238+
renderLabel,
238239
size: isSizeNumericValue ? nodeSize * t : { height: nodeSize.height * t, width: nodeSize.width * t },
239240
stroke,
240241
strokeWidth: strokeWidth * t,

src/components/graph/graph.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@
7878
* the value is set to <b>2</b> the selected node will be highlighted as well as the 1st and 2nd common degree connections.
7979
* @param {number} [highlightOpacity=1] - <a id="highlight-opacity" href="#highlight-opacity">🔗</a> this value is used to highlight nodes in the network. The lower
8080
* the value the more the less highlighted nodes will be visible (related to <i>nodeHighlightBehavior</i>).
81-
* @param {number} [maxZoom=8] - max zoom that can be performed against the graph.
82-
* @param {number} [minZoom=0.1] - min zoom that can be performed against the graph.
83-
* @param {number} [initialZoom=null] - initial zoom that can be set on the graph.
84-
* @param {boolean} [panAndZoom=false] - 🚅🚅🚅 pan and zoom effect when performing zoom in the graph,
81+
* @param {number} [initialZoom=null] - <a id="max-zoom" href="#initial-zoom">🔗</a> initial zoom that can be set on the graph.
82+
* @param {number} [maxZoom=8] - <a id="max-zoom" href="#max-zoom">🔗</a> max zoom that can be performed against the graph.
83+
* @param {number} [minZoom=0.1] - <a id="min-zoom" href="#min-zoom">🔗</a> min zoom that can be performed against the graph.
84+
* @param {boolean} [panAndZoom=false] - <a id="pan-and-zoom" href="#pan-and-zoom">🔗</a> 🚅🚅🚅 pan and zoom effect when performing zoom in the graph,
8585
* a similar functionality may be consulted <a target="_blank" href="https://bl.ocks.org/mbostock/2a39a768b1d4bc00a09650edef75ad39">here</a>.
8686
* @param {boolean} [staticGraph=false] - <a id="static-graph" href="#static-graph">🔗</a> when setting this value to true the graph will be completely static, thus
8787
* all forces and drag events upon nodes will produce not effect. Note that, if this value is true the nodes will be

0 commit comments

Comments
 (0)