Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customize color for a link #79

Merged
merged 2 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions src/components/graph/graph.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ function _validateGraphData(data) {

/**
* Build some Link properties based on given parameters.
* @param {string} source - the id of the source node (from).
* @param {string} target - the id of the target node (to).
* @param {Object} link - the link object for which we will generate properties.
* @param {Object.<string, Object>} nodes - same as {@link #buildGraph|nodes in buildGraph}.
* @param {Object.<string, Object>} links - same as {@link #buildGraph|links in buildGraph}.
* @param {Object} config - same as {@link #buildGraph|config in buildGraph}.
Expand All @@ -188,17 +187,9 @@ function _validateGraphData(data) {
* @returns {Object} returns an object that aggregates all props for creating respective Link component instance.
* @memberof Graph/helper
*/
function buildLinkProps(
source,
target,
nodes,
links,
config,
linkCallbacks,
highlightedNode,
highlightedLink,
transform
) {
function buildLinkProps(link, nodes, links, config, linkCallbacks, highlightedNode, highlightedLink, transform) {
const { source, target } = link;

const x1 = (nodes[source] && nodes[source].x) || 0;
const y1 = (nodes[source] && nodes[source].y) || 0;
const x2 = (nodes[target] && nodes[target].x) || 0;
Expand Down Expand Up @@ -230,7 +221,7 @@ function buildLinkProps(
opacity = highlight ? config.link.opacity : config.highlightOpacity;
}

let stroke = config.link.color;
let stroke = link.color || config.link.color;

if (highlight) {
stroke = config.link.highlightColor === CONST.KEYWORDS.SAME ? config.link.color : config.link.highlightColor;
Expand Down
3 changes: 1 addition & 2 deletions src/components/graph/graph.renderer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ function _buildLinks(nodes, links, linksMatrix, config, linkCallbacks, highlight
const targetId = target.id || target;
const key = `${sourceId}${CONST.COORDS_SEPARATOR}${targetId}`;
const props = buildLinkProps(
`${sourceId}`,
`${targetId}`,
{ ...link, source: `${sourceId}`, target: `${targetId}` },
nodes,
linksMatrix,
config,
Expand Down
49 changes: 49 additions & 0 deletions test/component/graph/graph.helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,55 @@ describe('Graph Helper', () => {
utils.throwErr = jest.fn();
});

describe('#buildLinkProps', () => {
let that = {};

beforeAll(() => {
const linkConfig = Object.assign({}, config.link);

that = {
config: { link: linkConfig },
link: { source: 'source', target: 'target' }
};
});

describe('when building props for a link', () => {
describe('and no custom color is set', () => {
test('should return default color defined in link config', () => {
const props = graphHelper.buildLinkProps(
that.link,
{},
{},
that.config,
[],
undefined,
undefined,
1
);

expect(props.stroke).toEqual(that.config.link.color);
});
});

describe('and custom color is set to green', () => {
test('should return green color in the props', () => {
const props = graphHelper.buildLinkProps(
{ ...that.link, color: 'green' },
{},
{},
that.config,
[],
undefined,
undefined,
1
);

expect(props.stroke).toEqual('green');
});
});
});
});

describe('#buildNodeProps', () => {
let that = {};

Expand Down