Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

3d-force-graph

Package Overview
Dependencies
Maintainers
1
Versions
321
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

3d-force-graph - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4-d3-force

src/d3-force.js

7

package.json
{
"name": "3d-force-graph",
"version": "0.0.3",
"version": "0.0.4-d3-force",
"description": "UI component for a 3D force-directed graph using ThreeJS and ngraph.forcelayout3d layout engine",

@@ -34,4 +34,5 @@ "main": "dist/3d-force-graph.js",

"dependencies": {
"ngraph.forcelayout3d": "~0.0.16",
"ngraph.graph": "~0.0.12",
"d3": "^4.7.0",
"d3-binarytree": "^0.1",
"d3-octree": "^0.1",
"three": "~0.84",

@@ -38,0 +39,0 @@ "three-trackballcontrols": "~0.0.5"

@@ -6,5 +6,4 @@ import './3d-force-graph.css';

import graph from 'ngraph.graph';
import forcelayout3d from 'ngraph.forcelayout3d';
const ngraph = { graph, forcelayout3d };
import * as d3Force from './d3-force';
var d3 = d3Force.__moduleExports;

@@ -133,22 +132,26 @@ export default function() {

// Build graph with data
const graph = ngraph.graph();
for (let nodeId in env.graphData.nodes) {
graph.addNode(nodeId, env.graphData.nodes[nodeId]);
const d3Nodes = [];
for (let nodeId in env.graphData.nodes) { // Turn nodes into array
const node = env.graphData.nodes[nodeId];
node._id = nodeId;
d3Nodes.push(node);
}
for (let link of env.graphData.links) {
graph.addLink(...link, {});
}
const d3Links = env.graphData.links.map(link => {
return { source: link[0], target: link[1] };
});
if (!d3Nodes.length) { return; }
// Add WebGL objects
graph.forEachNode(node => {
const nodeMaterial = new THREE.MeshBasicMaterial({ color: env.colorAccessor(node.data) || 0xffffaa, transparent: true });
d3Nodes.forEach(node => {
const nodeMaterial = new THREE.MeshBasicMaterial({ color: env.colorAccessor(node) || 0xffffaa, transparent: true });
nodeMaterial.opacity = 0.75;
const sphere = new THREE.Mesh(
new THREE.SphereGeometry(Math.cbrt(env.valAccessor(node.data) || 1) * env.nodeRelSize),
new THREE.SphereGeometry(Math.cbrt(env.valAccessor(node) || 1) * env.nodeRelSize),
nodeMaterial
);
sphere.name = env.nameAccessor(node.data) || '';
sphere.name = env.nameAccessor(node) || '';
env.scene.add(node.data.sphere = sphere)
env.scene.add(node._sphere = sphere)
});

@@ -158,10 +161,11 @@

lineMaterial.opacity = env.lineOpacity;
graph.forEachLink(link => {
d3Links.forEach(link => {
const line = new THREE.Line(new THREE.Geometry(), lineMaterial);
line.name = `${getNodeName(link.fromId)} > ${getNodeName(link.toId)}`;
line.geometry.vertices=[new THREE.Vector3(0,0,0), new THREE.Vector3(0,0,0)];
line.name = `${getNodeName(link.source)} > ${getNodeName(link.target)}`;
env.scene.add(link.data.line = line)
env.scene.add(link._line = line);
function getNodeName(nodeId) {
return env.nameAccessor(graph.getNode(nodeId).data);
return env.nameAccessor(env.graphData.nodes[nodeId]);
}

@@ -171,12 +175,18 @@ });

env.camera.lookAt(env.scene.position);
env.camera.position.z = Math.cbrt(Object.keys(env.graphData.nodes).length) * CAMERA_DISTANCE2NODES_FACTOR;
env.camera.position.z = Math.cbrt(d3Nodes.length) * CAMERA_DISTANCE2NODES_FACTOR;
// Add force-directed layout
const layout = ngraph.forcelayout3d(graph);
const layout = d3.forceSimulation()
.numDimensions(3)
.nodes(d3Nodes)
.force('link', d3.forceLink().id(d => d._id).links(d3Links))
.force('charge', d3.forceManyBody())
.force('center', d3.forceCenter())
.stop();
for (let i=0; i<env.initialEngineTicks; i++) { layout.step(); } // Initial ticks before starting to render
for (let i=0; i<env.initialEngineTicks; i++) { layout.tick(); } // Initial ticks before starting to render
let cntTicks = 0;
const startTickTime = new Date();
env.onFrame = layoutTick;
layout.on("tick", layoutTick).restart();

@@ -194,30 +204,22 @@ //

function layoutTick() {
if (cntTicks++ > env.maxConvergeFrames || (new Date()) - startTickTime > env.maxConvergeTime) {
env.onFrame = ()=>{}; // Stop ticking graph
}
layout.step(); // Tick it
// Update nodes position
graph.forEachNode(node => {
const sphere = node.data.sphere,
pos = layout.getNodePosition(node.id);
sphere.position.x = pos.x;
sphere.position.y = pos.y;
sphere.position.z = pos.z;
d3Nodes.forEach(node => {
const sphere = node._sphere;
sphere.position.x = node.x;
sphere.position.y = node.y || 0;
sphere.position.z = node.z || 0;
});
// Update links position
graph.forEachLink(link => {
const line = link.data.line,
pos = layout.getLinkPosition(link.id);
d3Links.forEach(link => {
const line = link._line;
line.geometry.vertices = [
new THREE.Vector3(pos.from.x, pos.from.y, pos.from.z),
new THREE.Vector3(pos.to.x, pos.to.y, pos.to.z)
new THREE.Vector3(link.source.x, link.source.y || 0, link.source.z || 0),
new THREE.Vector3(link.target.x, link.target.y || 0, link.target.z || 0)
];
line.geometry.verticesNeedUpdate = true;
})
line.geometry.computeBoundingSphere();
});
}

@@ -224,0 +226,0 @@ }

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc