I am trying to build a force directed graph in JUNG, similar to the ones you can create (very easily) in D3.js.
Most of it is very simple but the docs are a bit old and the examples dont include a force directed one (although there is a java applet on there that claims to be a demo but it shows nothing)
Anyway, using a DirectedSparseGraph
I have achieve most of the functionality:
directedGraph = new DirectedSparseGraph();
directedGraph.addVertex("someVertex");
directedGraph.addVertex("someOtherVertex");
// etc
directedGraph.addEdge("someVertex", "someOtherVertex");
// etc
This gives me almost exactly what I want but I can't figure out how to assign a length to the edges to show differing forces. I've looked through the docs and Google but can't find anything.
I'd expect something like
directedGraph.addEdge([edge 1], [edge 2], [edge length]);
but it doesn't seem to exist in the api.
Anyone have any idea how to achieve this?
N.b. jung version is 2.0.1
It looks like you are looking at jung-1.x docs. In jung-2.x
DirectedSparseGraph<V, E>
is a generic class with type parameters for verticesV
and edgesE
.This is also the method to define vertices and edges with arbitrary properties. E.g. if you want to have edge weights, create a suitable edge class with a weight:
Note that you usually also want to implement
equals()
andhashCode()
at least for vertices.If you want to use the weight for layouting (e.g. in
SpringLayout
, one of the constructors takes alengthFunction
.