Skip to content

Commit

Permalink
Position internet nodes above/below graph
Browse files Browse the repository at this point in the history
  • Loading branch information
davkal authored and tomwilkie committed Feb 29, 2016
1 parent 82d960a commit 3587d83
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions client/app/scripts/charts/nodes-layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,78 @@ function shiftLayoutToCenter(layout, opts) {
return layout;
}

/**
* Position the internet nodes above/below the rest of the graph
* @param {Object} layout Layout with nodes and edges
* @param {Object} opts Options with scale
* @return {Object} modified layout
*/
function positionInternet(layout, opts) {
const options = opts || {};
const scale = options.scale || (val => val * 2);
const delta = scale(2.5);
const IN = 'in-theinternet';
const OUT = 'out-theinternet';
let nodes = layout.nodes;
let edges = layout.edges;

if (nodes.has(IN)) {
// shift IN node to the top
const minYNode = nodes.minBy(node => node.get('y'));
nodes = nodes.setIn([IN, 'y'], minYNode.get('y') - delta);

// adjust negative coordinates
const minY = minYNode.get('y') - delta;
if (minY < 0) {
nodes = nodes.map(node => {
return node.set('y', node.get('y') - minY + delta);
});
}

// adjust edges that connect to IN node
const inNode = nodes.get(IN);
edges = edges.map(edge => {
if (edge.get('source') === IN) {
const points = edge.get('points');
points[0] = {x: inNode.get('x'), y: inNode.get('y')};
return edge.set('points', points);
}
if (edge.get('target') === IN) {
const points = edge.get('points');
points[points.length - 1] = {x: inNode.get('x'), y: inNode.get('y')};
return edge.set('points', points);
}
return edge;
});
}

if (nodes.has(OUT)) {
// shift OUT node to the bottom
const maxYNode = nodes.maxBy(node => node.get('y'));
nodes = nodes.setIn([OUT, 'y'], maxYNode.get('y') + delta);

// adjust edges that connect to OUT node
const outNode = nodes.get(OUT);
edges = edges.map(edge => {
if (edge.get('source') === OUT) {
const points = edge.get('points');
points[0] = {x: outNode.get('x'), y: outNode.get('y')};
return edge.set('points', points);
}
if (edge.get('target') === OUT) {
const points = edge.get('points');
points[points.length - 1] = {x: outNode.get('x'), y: outNode.get('y')};
return edge.set('points', points);
}
return edge;
});
}

layout.edges = edges;
layout.nodes = nodes;
return layout;
}

/**
* Adds `points` array to edge based on location of source and target
* @param {Map} edge new edge
Expand Down Expand Up @@ -373,6 +445,7 @@ export function doLayout(immNodes, immEdges, opts) {
if (!layout) {
return layout;
}
layout = positionInternet(layout, opts);
layout = layoutSingleNodes(layout, opts);
layout = shiftLayoutToCenter(layout, opts);
}
Expand Down

0 comments on commit 3587d83

Please sign in to comment.