meshviewer/lib/forcegraph.js

247 lines
6.4 KiB
JavaScript
Raw Normal View History

2017-02-23 21:32:25 +00:00
define(['d3-selection', 'd3-force', 'd3-zoom', 'd3-drag', 'utils/math', 'forcegraph/draw'],
function (d3Selection, d3Force, d3Zoom, d3Drag, math, draw) {
'use strict';
2015-03-31 15:22:36 +00:00
2017-02-23 21:32:25 +00:00
return function (config, linkScale, sidebar, router) {
var self = this;
var el;
var canvas;
var ctx;
var force;
var forceLink;
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
var transform = d3Zoom.zoomIdentity;
var intNodes = [];
var dictNodes = {};
var intLinks = [];
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
var NODE_RADIUS_DRAG = 10;
var NODE_RADIUS_SELECT = 15;
var LINK_RADIUS_SELECT = 12;
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
var ZOOM_MIN = 1 / 8;
var ZOOM_MAX = 3;
2015-07-08 16:03:19 +00:00
2017-02-23 21:32:25 +00:00
var FORCE_ALPHA = 0.3;
2015-03-31 15:22:36 +00:00
2017-02-23 21:32:25 +00:00
draw.setTransform(transform);
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
function resizeCanvas() {
canvas.width = el.offsetWidth;
canvas.height = el.offsetHeight;
draw.setMaxArea(canvas.width, canvas.height);
2015-04-15 23:29:17 +00:00
}
2017-02-23 21:32:25 +00:00
function moveTo(x, y) {
transform.x = (canvas.width + sidebar()) / 2 - x * transform.k;
transform.y = canvas.height / 2 - y * transform.k;
2015-04-10 18:47:11 +00:00
}
2017-02-23 21:32:25 +00:00
function onClick() {
if (d3Selection.event.defaultPrevented) {
return;
}
2015-03-31 19:18:06 +00:00
2017-02-23 21:32:25 +00:00
var e = transform.invert([d3Selection.event.clientX, d3Selection.event.clientY]);
var n = force.find(e[0], e[1], NODE_RADIUS_SELECT);
2015-03-31 19:29:47 +00:00
2017-02-23 21:32:25 +00:00
if (n !== undefined) {
router.node(n.o.node)();
return;
}
2015-04-10 18:47:11 +00:00
2017-02-23 21:32:25 +00:00
e = { x: e[0], y: e[1] };
2017-02-23 21:32:25 +00:00
var closedLink;
var radius = LINK_RADIUS_SELECT;
intLinks
.forEach(function (d) {
var distance = math.distanceLink(e, d.source, d.target);
if (distance < radius) {
closedLink = d;
radius = distance;
}
2017-02-23 21:32:25 +00:00
});
2017-02-23 21:32:25 +00:00
if (closedLink !== undefined) {
router.link(closedLink.o)();
}
}
2017-02-23 21:32:25 +00:00
function redraw() {
ctx.save();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.translate(transform.x, transform.y);
ctx.scale(transform.k, transform.k);
2015-04-10 11:50:39 +00:00
2017-02-23 21:32:25 +00:00
intLinks.forEach(draw.drawLink);
intNodes.forEach(draw.drawNode);
2015-04-10 11:50:39 +00:00
2017-02-23 21:32:25 +00:00
ctx.restore();
}
2015-04-10 11:50:39 +00:00
2017-02-23 21:32:25 +00:00
el = document.createElement('div');
el.classList.add('graph');
2015-04-19 00:16:06 +00:00
2017-02-23 21:32:25 +00:00
forceLink = d3Force.forceLink()
.distance(function (d) {
if (d.o.vpn) {
2017-02-23 21:32:25 +00:00
return 0;
}
return 75;
})
.strength(function (d) {
if (d.o.vpn) {
2017-02-23 21:32:25 +00:00
return 0.02;
}
return Math.max(0.5, 1 / d.o.tq);
});
2017-02-23 21:32:25 +00:00
var zoom = d3Zoom.zoom()
.scaleExtent([ZOOM_MIN, ZOOM_MAX])
.on('zoom', function () {
transform = d3Selection.event.transform;
draw.setTransform(transform);
redraw();
});
2015-07-30 21:47:58 +00:00
2017-02-23 21:32:25 +00:00
force = d3Force.forceSimulation()
.force('link', forceLink)
.force('charge', d3Force.forceManyBody())
.force('x', d3Force.forceX().strength(0.02))
.force('y', d3Force.forceY().strength(0.02))
.force('collide', d3Force.forceCollide())
.on('tick', redraw);
var drag = d3Drag.drag()
.subject(function () {
var e = transform.invert([d3Selection.event.x, d3Selection.event.y]);
var n = force.find(e[0], e[1], NODE_RADIUS_DRAG);
if (n !== undefined) {
n.x = d3Selection.event.x;
n.y = d3Selection.event.y;
return n;
}
return undefined;
})
.on('start', function () {
if (!d3Selection.event.active) {
force.alphaTarget(FORCE_ALPHA).restart();
}
d3Selection.event.subject.fx = transform.invertX(d3Selection.event.subject.x);
d3Selection.event.subject.fy = transform.invertY(d3Selection.event.subject.y);
})
.on('drag', function () {
d3Selection.event.subject.fx = transform.invertX(d3Selection.event.x);
d3Selection.event.subject.fy = transform.invertY(d3Selection.event.y);
})
.on('end', function () {
if (!d3Selection.event.active) {
force.alphaTarget(0);
}
d3Selection.event.subject.fx = null;
d3Selection.event.subject.fy = null;
});
2015-04-10 18:47:11 +00:00
2017-02-23 21:32:25 +00:00
canvas = d3Selection.select(el)
.append('canvas')
.on('click', onClick)
.call(drag)
.call(zoom)
.node();
ctx = canvas.getContext('2d');
draw.setCTX(ctx);
window.addEventListener('resize', function () {
resizeCanvas();
redraw();
});
self.setData = function setData(data) {
intNodes = data.graph.nodes.map(function (d) {
var e;
if (d.id in dictNodes) {
e = dictNodes[d.id];
} else {
e = {};
dictNodes[d.id] = e;
}
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
e.o = d;
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
return e;
});
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
intLinks = data.graph.links.map(function (d) {
var e = {};
e.o = d;
e.source = dictNodes[d.source.id];
e.target = dictNodes[d.target.id];
2017-03-09 23:53:23 +00:00
e.color = linkScale(1 / d.tq);
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
return e;
});
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
force.nodes(intNodes);
forceLink.links(intLinks);
2015-04-15 23:29:17 +00:00
2017-02-23 21:32:25 +00:00
force.alpha(1).restart();
resizeCanvas();
};
2015-07-07 08:19:38 +00:00
2017-02-23 21:32:25 +00:00
self.resetView = function resetView() {
draw.setHighlight(null);
transform.k = (ZOOM_MIN + 1) / 2;
moveTo(0, 0);
redraw();
};
2017-02-23 21:32:25 +00:00
self.gotoNode = function gotoNode(d) {
for (var i = 0; i < intNodes.length; i++) {
var n = intNodes[i];
if (n.o.node.nodeinfo.node_id !== d.nodeinfo.node_id) {
2017-02-23 21:32:25 +00:00
continue;
}
draw.setHighlight({ type: 'node', o: n.o.node });
2017-02-23 21:32:25 +00:00
transform.k = (ZOOM_MAX + 1) / 2;
moveTo(n.x, n.y);
break;
}
2017-02-23 21:32:25 +00:00
redraw();
};
2015-04-03 00:32:32 +00:00
2017-02-23 21:32:25 +00:00
self.gotoLink = function gotoLink(d) {
draw.setHighlight({ type: 'link', o: d });
for (var i = 0; i < intLinks.length; i++) {
var l = intLinks[i];
if (l.o !== d) {
continue;
}
moveTo((l.source.x + l.target.x) / 2, (l.source.y + l.target.y) / 2);
break;
}
2017-02-23 21:32:25 +00:00
redraw();
};
2015-04-07 23:21:52 +00:00
2017-02-23 21:32:25 +00:00
self.destroy = function destroy() {
force.stop();
canvas.remove();
force = null;
2017-02-23 21:32:25 +00:00
if (el.parentNode) {
el.parentNode.removeChild(el);
}
2017-02-23 21:32:25 +00:00
};
2017-02-23 21:32:25 +00:00
self.render = function render(d) {
d.appendChild(el);
resizeCanvas();
};
2015-07-11 22:11:18 +00:00
2017-02-23 21:32:25 +00:00
return self;
};
2017-02-23 21:32:25 +00:00
});