Skip to content

Commit 3e1af4a

Browse files
authored
Added ability to configure a node's width and height separately (#342)
* allow for nested objects of depth > 2 * added ability to configure node height and width * updated graph config docs * added comment about node size unit of measurement to docs * added size type checking variable for simplification, use logWarning
1 parent 90e0a15 commit 3e1af4a

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

sandbox/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function generateFormSchema(o, rootSpreadProp, accum = {}) {
3737
const kk = rootSpreadProp ? `${rootSpreadProp}.${k}` : k;
3838

3939
if (o[k] !== undefined && o[k] !== null && typeof o[k] !== "function") {
40-
typeof o[k] === "object" ? generateFormSchema(o[kk], kk, accum) : (accum[kk] = formMap(kk, o[k]));
40+
typeof o[k] === "object" ? generateFormSchema(o[k], kk, accum) : (accum[kk] = formMap(kk, o[k]));
4141
}
4242
}
4343

src/components/graph/graph.builder.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ function buildLinkProps(link, nodes, links, config, linkCallbacks, highlightedNo
169169
function buildNodeProps(node, config, nodeCallbacks = {}, highlightedNode, highlightedLink, transform) {
170170
const highlight =
171171
node.highlighted ||
172-
(node.id === (highlightedLink && highlightedLink.source) ||
173-
node.id === (highlightedLink && highlightedLink.target));
172+
node.id === (highlightedLink && highlightedLink.source) ||
173+
node.id === (highlightedLink && highlightedLink.target);
174174
const opacity = _getNodeOpacity(node, highlightedNode, highlightedLink, config);
175175

176176
let fill = node.color || config.node.color;
@@ -201,8 +201,20 @@ function buildNodeProps(node, config, nodeCallbacks = {}, highlightedNode, highl
201201

202202
const t = 1 / transform;
203203
const nodeSize = node.size || config.node.size;
204+
205+
let offset;
206+
const isSizeNumericValue = typeof nodeSize !== "object";
207+
208+
if (isSizeNumericValue) {
209+
offset = nodeSize;
210+
} else if (labelPosition === "top" || labelPosition === "bottom") {
211+
offset = nodeSize.height;
212+
} else {
213+
nodeSize.width;
214+
}
215+
204216
const fontSize = highlight ? config.node.highlightFontSize : config.node.fontSize;
205-
const dx = fontSize * t + nodeSize / 100 + 1.5;
217+
const dx = fontSize * t + offset / 100 + 1.5;
206218
const svg = node.svg || config.node.svg;
207219
const fontColor = node.fontColor || config.node.fontColor;
208220

@@ -223,7 +235,7 @@ function buildNodeProps(node, config, nodeCallbacks = {}, highlightedNode, highl
223235
opacity,
224236
overrideGlobalViewGenerator: !node.viewGenerator && node.svg,
225237
renderLabel: node.renderLabel || config.node.renderLabel,
226-
size: nodeSize * t,
238+
size: isSizeNumericValue ? nodeSize * t : { height: nodeSize.height * t, width: nodeSize.width * t },
227239
stroke,
228240
strokeWidth: strokeWidth * t,
229241
svg,

src/components/graph/graph.config.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,17 @@
146146
* @param {number} [node.opacity=1] - <a id="node-opacity" href="#node-opacity">🔗</a> 🔍🔍🔍 by default all nodes will have this opacity value.
147147
* @param {boolean} [node.renderLabel=true] - <a id="node-render-label" href="#node-render-label">🔗</a> 🔍🔍🔍 when set to false no labels will appear along side nodes in the
148148
* graph.
149-
* @param {number} [node.size=200] - <a id="node-size" href="#node-size">🔗</a> 🔍🔍🔍 defines the size of all nodes.
149+
* @param {number|Object} [node.size=200] - <a id="node-size" href="#node-size">🔗</a> 🔍🔍🔍 defines the size of all nodes. When set to a number, the node will have equal height and width.</br>
150+
* This can also be an object with a height and width property <b>when using custom nodes</b>.
151+
* ```javascript
152+
* size: 200
153+
* // or
154+
* size: {
155+
* height: 200,
156+
* width: 300,
157+
* }
158+
* ```
159+
* The actual node dimensions (in px) rendered on screen will be the size value divided by 10. For example, a node size of 200 will result in a node with a height and width of 20px.
150160
* @param {string} [node.strokeColor="none"] - <a id="node-stroke-color" href="#node-stroke-color">🔗</a> 🔍🔍🔍 this is the stroke color that will be applied to the node if no <b>strokeColor property</b> is found inside the node itself (yes <b>you can pass a property "strokeColor" inside the node and that stroke color will override this default one</b>).
151161
* @param {number} [node.strokeWidth=1.5] - <a id="node-stroke-width" href="#node-stroke-width">🔗</a> 🔍🔍🔍 the width of the all node strokes.
152162
* @param {string} [node.svg=""] - <a id="node-svg" href="#node-svg">🔗</a> 🔍🔍🔍 render custom svg for nodes in alternative to <b>node.symbolType</b>. This svg can

src/components/node/Node.jsx

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

33
import nodeHelper from "./node.helper";
4+
import CONST from "./node.const";
5+
import { logWarning } from "../../utils";
46

57
/**
68
* Node component is responsible for encapsulating node render.
@@ -94,16 +96,17 @@ export default class Node extends React.Component {
9496
opacity: this.props.opacity,
9597
};
9698

97-
const size = this.props.size;
99+
let size = this.props.size;
100+
const isSizeNumericalValue = typeof size !== "object";
98101

99102
let gtx = this.props.cx,
100103
gty = this.props.cy,
101104
label = null,
102105
node = null;
103106

104107
if (this.props.svg || this.props.viewGenerator) {
105-
const height = size / 10;
106-
const width = size / 10;
108+
const height = isSizeNumericalValue ? size / 10 : size.height / 10;
109+
const width = isSizeNumericalValue ? size / 10 : size.width / 10;
107110
const tx = width / 2;
108111
const ty = height / 2;
109112
const transform = `translate(${tx},${ty})`;
@@ -133,6 +136,10 @@ export default class Node extends React.Component {
133136
gtx -= tx;
134137
gty -= ty;
135138
} else {
139+
if (!isSizeNumericalValue) {
140+
logWarning("node.size should be a number when not using custom nodes.");
141+
size = CONST.DEFAULT_NODE_SIZE;
142+
}
136143
nodeProps.d = nodeHelper.buildSvgSymbol(size, this.props.type);
137144
nodeProps.fill = this.props.fill;
138145
nodeProps.stroke = this.props.stroke;

0 commit comments

Comments
 (0)