Skip to content
Snippets Groups Projects
Commit 9ef90cb8 authored by Matthias Konitzny's avatar Matthias Konitzny :fire:
Browse files

Added node highlights and focus on node functions.

parent af80cef5
No related branches found
No related tags found
No related merge requests found
const highlightNodes = new Set();
const highlightLinks = new Set();
let firstHover = true;
let hoverNode = null;
const Graph = ForceGraph3D()
(document.getElementById('3d-graph'))
.jsonUrl(dataset)
.nodeLabel('id')
.nodeAutoColorBy('group');
\ No newline at end of file
.nodeAutoColorBy('group')
.nodeColor(node => highlightNodes.has(node) ? node === hoverNode ? 'rgb(255,0,0,1)' : 'rgba(255,160,0,0.8)' : 'rgba(0,255,255,0.6)')
.linkWidth(link => highlightLinks.has(link) ? 4 : 1)
.onNodeClick(focusOnNode)
.onNodeHover(nodeHover)
.onLinkHover(linkHover);
function updateLinks() {
gData = Graph.graphData();
// cross-link node objects
gData.links.forEach(link => {
const a = link.source;
const b = link.target;
if (!a.neighbors) a.neighbors = [];
if (!b.neighbors) b.neighbors = [];
a.neighbors.push(b);
b.neighbors.push(a);
if (!a.links) a.links = [];
if (!b.links) b.links = [];
a.links.push(link);
b.links.push(link);
});
Graph.graphData(gData)
}
function focusOnNode(node) {
// Aim at node from outside it
const distance = 250;
const distRatio = 1 + distance/Math.hypot(node.x, node.y, node.z);
Graph.cameraPosition(
{ x: node.x * distRatio, y: node.y * distRatio, z: node.z * distRatio }, // new position
node, // lookAt ({ x, y, z })
1000 // ms transition duration
);
}
function nodeHover(node) {
// no state change
if ((!node && !highlightNodes.size) || (node && hoverNode === node)) return;
if (firstHover) {
updateLinks();
firstHover = false;
}
highlightNodes.clear();
highlightLinks.clear();
if (node) {
highlightNodes.add(node);
node.neighbors.forEach(neighbor => highlightNodes.add(neighbor));
node.links.forEach(link => highlightLinks.add(link));
}
hoverNode = node || null;
updateHighlight();
}
function linkHover(link) {
highlightNodes.clear();
highlightLinks.clear();
if (link) {
highlightLinks.add(link);
highlightNodes.add(link.source);
highlightNodes.add(link.target);
}
updateHighlight();
}
function updateHighlight() {
// trigger update of highlighted objects in scene
Graph
.nodeColor(Graph.nodeColor())
.linkWidth(Graph.linkWidth())
.linkDirectionalParticles(Graph.linkDirectionalParticles());
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment