Skip to content

Commit a515806

Browse files
LonelyPrincessdanielcaldas
authored andcommitted
Customize color for a link (#79)
* Customizable color for individual links When adding links to a dataset now, we can add optionally include a 'color' property. If it's set, said color will override the default color defined in the configuration object. In order for this to work, the 'buildLinkProps' is now receiving a 'link' object, which could contain any additional data aside from the source and target nodes. In this case, we'll be only considering a 'color' property that will be taken into account when setting the stroke color of the line to draw. The former 'source' and 'target' parameters that used to be stand-alone parameters in the function, are now data contained inside of the new 'link' object. * Add tests for custom link feature Added some new tests in the 'graph.helper.test' suite to check whether the customization of the color for a certain link is working correctly. We will be checking whether the stroke color matches the expected one in both scenarios: when a custom color is set and when no color has been passed along the link data.
1 parent f4405a6 commit a515806

File tree

3 files changed

+55
-16
lines changed

3 files changed

+55
-16
lines changed

src/components/graph/graph.helper.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ function _validateGraphData(data) {
176176

177177
/**
178178
* Build some Link properties based on given parameters.
179-
* @param {string} source - the id of the source node (from).
180-
* @param {string} target - the id of the target node (to).
179+
* @param {Object} link - the link object for which we will generate properties.
181180
* @param {Object.<string, Object>} nodes - same as {@link #buildGraph|nodes in buildGraph}.
182181
* @param {Object.<string, Object>} links - same as {@link #buildGraph|links in buildGraph}.
183182
* @param {Object} config - same as {@link #buildGraph|config in buildGraph}.
@@ -188,17 +187,9 @@ function _validateGraphData(data) {
188187
* @returns {Object} returns an object that aggregates all props for creating respective Link component instance.
189188
* @memberof Graph/helper
190189
*/
191-
function buildLinkProps(
192-
source,
193-
target,
194-
nodes,
195-
links,
196-
config,
197-
linkCallbacks,
198-
highlightedNode,
199-
highlightedLink,
200-
transform
201-
) {
190+
function buildLinkProps(link, nodes, links, config, linkCallbacks, highlightedNode, highlightedLink, transform) {
191+
const { source, target } = link;
192+
202193
const x1 = (nodes[source] && nodes[source].x) || 0;
203194
const y1 = (nodes[source] && nodes[source].y) || 0;
204195
const x2 = (nodes[target] && nodes[target].x) || 0;
@@ -230,7 +221,7 @@ function buildLinkProps(
230221
opacity = highlight ? config.link.opacity : config.highlightOpacity;
231222
}
232223

233-
let stroke = config.link.color;
224+
let stroke = link.color || config.link.color;
234225

235226
if (highlight) {
236227
stroke = config.link.highlightColor === CONST.KEYWORDS.SAME ? config.link.color : config.link.highlightColor;

src/components/graph/graph.renderer.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ function _buildLinks(nodes, links, linksMatrix, config, linkCallbacks, highlight
3232
const targetId = target.id || target;
3333
const key = `${sourceId}${CONST.COORDS_SEPARATOR}${targetId}`;
3434
const props = buildLinkProps(
35-
`${sourceId}`,
36-
`${targetId}`,
35+
{ ...link, source: `${sourceId}`, target: `${targetId}` },
3736
nodes,
3837
linksMatrix,
3938
config,

test/component/graph/graph.helper.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,55 @@ describe('Graph Helper', () => {
2020
utils.throwErr = jest.fn();
2121
});
2222

23+
describe('#buildLinkProps', () => {
24+
let that = {};
25+
26+
beforeAll(() => {
27+
const linkConfig = Object.assign({}, config.link);
28+
29+
that = {
30+
config: { link: linkConfig },
31+
link: { source: 'source', target: 'target' }
32+
};
33+
});
34+
35+
describe('when building props for a link', () => {
36+
describe('and no custom color is set', () => {
37+
test('should return default color defined in link config', () => {
38+
const props = graphHelper.buildLinkProps(
39+
that.link,
40+
{},
41+
{},
42+
that.config,
43+
[],
44+
undefined,
45+
undefined,
46+
1
47+
);
48+
49+
expect(props.stroke).toEqual(that.config.link.color);
50+
});
51+
});
52+
53+
describe('and custom color is set to green', () => {
54+
test('should return green color in the props', () => {
55+
const props = graphHelper.buildLinkProps(
56+
{ ...that.link, color: 'green' },
57+
{},
58+
{},
59+
that.config,
60+
[],
61+
undefined,
62+
undefined,
63+
1
64+
);
65+
66+
expect(props.stroke).toEqual('green');
67+
});
68+
});
69+
});
70+
});
71+
2372
describe('#buildNodeProps', () => {
2473
let that = {};
2574

0 commit comments

Comments
 (0)