Skip to content

Commit 390e5d5

Browse files
dgautschdanielcaldas
authored andcommitted
Add Support to pass a function to node.labelProperty (#135)
* Checking for function in labelProperty and assigning output as new value, otherwise defaulting to original method * Applying marvel config to demonstrate function usage. * Updating sandbox bundle * Adding try catch in case user function fails * Applying string template suggested changes * Removing try catch and renaming variable assignment to utilize short hand * Reverting sandbox changes * Update graph.config labelProperty jsdoc * Improve labelProperty function in sandbox marvel.config
1 parent c814510 commit 390e5d5

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

sandbox/data/marvel/marvel.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ module.exports = {
3030
highlightFontWeight: 'bold',
3131
highlightStrokeColor: 'red',
3232
highlightStrokeWidth: 1.5,
33-
labelProperty: 'id',
33+
labelProperty: n => (n.name ? `${n.id} - ${n.name}` : n.id),
3434
mouseCursor: 'crosshair',
3535
opacity: 0.9,
3636
renderLabel: true,

src/components/graph/graph.config.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,19 @@
9292
* @param {string} [node.highlightFontWeight='normal'] - fontWeight in highlighted state.
9393
* @param {string} [node.highlightStrokeColor='SAME'] - strokeColor in highlighted state.
9494
* @param {number} [node.highlightStrokeWidth=1.5] - strokeWidth in highlighted state.
95-
* @param {string} [node.labelProperty='id'] - this is the node property that will be used in runtime to
95+
* @param {string|Function} [node.labelProperty='id'] - this is the node property that will be used in runtime to
9696
* fetch the label content. You just need to add some property (e.g. firstName) to the node payload and then set
97-
* node.labelProperty to be **'firstName'**.
97+
* node.labelProperty to be **'firstName'**. **This can also be a function!**, if you pass a function here it will be called
98+
* to obtain the `label` value on the fly, as a client you will receive all the node information that you passed down into react-d3-graph,
99+
* so the signature of the function would be:
100+
* ```javascript
101+
* function myCustomLabelBuilder(node) {
102+
* // do stuff to get the final result...
103+
* return 'label string';
104+
* }
105+
* ```
106+
* Then you just need to make sure that you pass this function in the config in `config.node.labelProperty`.
107+
* <br/>
98108
* @param {string} [node.mouseCursor='pointer'] - {@link https://developer.mozilla.org/en/docs/Web/CSS/cursor?v=control|cursor}
99109
* property for when some node is mouse hovered.
100110
* @param {number} [node.opacity=1] - by default all nodes will have this opacity value.

src/components/graph/graph.helper.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ function buildNodeProps(node, config, nodeCallbacks = {}, highlightedNode, highl
342342
stroke = config.node.highlightStrokeColor;
343343
}
344344

345+
let label = node[config.node.labelProperty] || node.id;
346+
347+
if (typeof config.node.labelProperty === 'function') {
348+
label = config.node.labelProperty(node);
349+
}
350+
345351
const t = 1 / transform;
346352
const nodeSize = node.size || config.node.size;
347353
const fontSize = highlight ? config.node.highlightFontSize : config.node.fontSize;
@@ -362,7 +368,7 @@ function buildNodeProps(node, config, nodeCallbacks = {}, highlightedNode, highl
362368
dx,
363369
fontWeight: highlight ? config.node.highlightFontWeight : config.node.fontWeight,
364370
id: node.id,
365-
label: node[config.node.labelProperty] || node.id,
371+
label,
366372
onClickNode: nodeCallbacks.onClickNode,
367373
onRightClickNode: nodeCallbacks.onRightClickNode,
368374
onMouseOverNode: nodeCallbacks.onMouseOverNode,

0 commit comments

Comments
 (0)