2017-02-14 13:30:57 +02:00
|
|
|
const d3 = require('d3');
|
|
|
|
|
|
|
|
const hypotenuse = (a, b) =>
|
|
|
|
Math.sqrt(a*a + b*b);
|
|
|
|
|
|
|
|
const rectRadius = (size) => {
|
|
|
|
|
|
|
|
const {
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
} = size;
|
|
|
|
|
|
|
|
return Math.round(hypotenuse(width, height)/2);
|
|
|
|
};
|
|
|
|
|
2017-02-22 17:18:18 +02:00
|
|
|
const createLinks = (services) =>
|
|
|
|
services.reduce((acc, service, index) =>
|
|
|
|
service.connections ?
|
|
|
|
acc.concat(
|
|
|
|
service.connections.map((connection, index) => ({
|
|
|
|
source: service.uuid,
|
|
|
|
target: connection
|
|
|
|
}))
|
|
|
|
) : acc
|
|
|
|
, []);
|
|
|
|
|
2017-02-14 13:30:57 +02:00
|
|
|
const createSimulation = (
|
2017-02-22 17:18:18 +02:00
|
|
|
services,
|
2017-02-14 13:30:57 +02:00
|
|
|
nodeSize,
|
|
|
|
svgSize,
|
|
|
|
onTick,
|
|
|
|
onEnd
|
|
|
|
) => {
|
|
|
|
// This is not going to work given that as well as the d3 layout stuff, other things might be at play too
|
|
|
|
// We should pass two objects to the components - one for positioning and one for data
|
2017-02-22 17:18:18 +02:00
|
|
|
const nodes = services.map((service, index) => ({
|
|
|
|
id: service.uuid,
|
2017-02-14 13:30:57 +02:00
|
|
|
index: index
|
|
|
|
}));
|
2017-02-22 17:18:18 +02:00
|
|
|
|
|
|
|
const links = createLinks(services);
|
2017-02-14 13:30:57 +02:00
|
|
|
|
|
|
|
const {
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
} = svgSize;
|
|
|
|
|
|
|
|
const nodeRadius = rectRadius(nodeSize);
|
|
|
|
|
2017-02-15 12:44:16 +02:00
|
|
|
return ({
|
2017-02-22 17:18:18 +02:00
|
|
|
simulation: d3.forceSimulation(nodes)
|
|
|
|
.force('link', d3.forceLink(links).id(d => d.id))
|
2017-02-15 12:44:16 +02:00
|
|
|
.force('collide', d3.forceCollide(nodeRadius))
|
|
|
|
.force('center', d3.forceCenter(width/2, height/2))
|
|
|
|
.on('tick', onTick)
|
|
|
|
.on('end', onEnd),
|
2017-02-22 17:18:18 +02:00
|
|
|
nodes: nodes,
|
|
|
|
links: links
|
2017-02-15 12:44:16 +02:00
|
|
|
});
|
2017-02-14 13:30:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// TODO we need to kill the previous simulation
|
|
|
|
const updateSimulation = (
|
|
|
|
simulation,
|
2017-02-22 17:18:18 +02:00
|
|
|
services,
|
2017-02-15 12:44:16 +02:00
|
|
|
simNodes,
|
|
|
|
simLinks,
|
2017-02-14 13:30:57 +02:00
|
|
|
nodeSize,
|
|
|
|
svgSize,
|
|
|
|
onTick,
|
|
|
|
onEnd
|
|
|
|
) => {
|
2017-02-22 17:18:18 +02:00
|
|
|
const nodes = services.map((service, index) => {
|
2017-02-15 12:44:16 +02:00
|
|
|
const simNode = simNodes.reduce((acc, n, i) => {
|
2017-02-22 17:18:18 +02:00
|
|
|
return service.uuid === n.id ? n : acc;
|
2017-02-15 12:44:16 +02:00
|
|
|
}, null);
|
|
|
|
|
|
|
|
return simNode ? {
|
|
|
|
id: simNode.id,
|
|
|
|
// fx: simNode.x,
|
|
|
|
// fy: simNode.y,
|
2017-02-14 13:30:57 +02:00
|
|
|
index: index
|
|
|
|
} : {
|
2017-02-22 17:18:18 +02:00
|
|
|
id: service.uuid,
|
2017-02-14 13:30:57 +02:00
|
|
|
index: index
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2017-02-22 17:18:18 +02:00
|
|
|
const links = createLinks(services);
|
2017-02-14 13:30:57 +02:00
|
|
|
|
|
|
|
const {
|
|
|
|
width,
|
|
|
|
height
|
|
|
|
} = svgSize;
|
|
|
|
|
|
|
|
const nodeRadius = rectRadius(nodeSize);
|
|
|
|
|
2017-02-15 12:44:16 +02:00
|
|
|
return ({
|
2017-02-22 17:18:18 +02:00
|
|
|
simulation: d3.forceSimulation(nodes)
|
|
|
|
.force('link', d3.forceLink(links).id(d => d.id))
|
2017-02-15 12:44:16 +02:00
|
|
|
.force('collide', d3.forceCollide(nodeRadius))
|
|
|
|
.force('center', d3.forceCenter(width/2, height/2))
|
|
|
|
.on('tick', onTick)
|
|
|
|
.on('end', onEnd),
|
2017-02-22 17:18:18 +02:00
|
|
|
nodes: nodes,
|
|
|
|
links: links
|
2017-02-15 12:44:16 +02:00
|
|
|
});
|
2017-02-14 13:30:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
createSimulation,
|
|
|
|
updateSimulation
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
const simulation = d3.forceSimulation(dataNodes)
|
|
|
|
// .alpha(1).alphaDecay(0.1)
|
|
|
|
// .force('charge', d3.forceManyBody())
|
|
|
|
.force('link', d3.forceLink(dataLinks)
|
|
|
|
//.distance(() => linkDistance)
|
|
|
|
.id(d => d.id))
|
|
|
|
.force('collide', d3.forceCollide(nodeRadius))
|
|
|
|
.force('center', d3.forceCenter(1024/2, 860/2))
|
|
|
|
.on('tick', () => {
|
|
|
|
console.log('SIMULATION TICK');
|
|
|
|
console.log('tickCounter = ', tickCounter);
|
|
|
|
tickCounter++;
|
|
|
|
this.forceUpdate();
|
|
|
|
})
|
|
|
|
.on('end', () => {
|
|
|
|
console.log('SIMULATION END');
|
|
|
|
console.log('tickCounter = ', tickCounter);
|
|
|
|
// this.forceUpdate();
|
|
|
|
})
|
|
|
|
*/
|