I have got some issues using the DAGLayout algorithm of JUNG and subsequently reading out the layout coordinates into my own data structure again.
I have got a Network class with lists of Nodes and Edges. To convert this to a JUNG data structure, I create a DirectedSparseMultigraph object and add the edges. e.getSrc() and e.getDest() return Node objects.
DirectedSparseMultigraph<Node, Edge> graph;
for (Edge e : net.getEdges()) {
graph.addEdge(e, e.getSrc(), e.getDest());
}
Then, I apply the layout algorithm.
Layout<Node, Point2D> layout;
layout = new DAGLayout(graph);
After that, I use to layout to get the vertex coordinates.
for (Node node : net.getNodes()) {
Point2D coord = layout.transform(node);
node.setPos((float)coord.getX(), (float)coord.getY());
}
But the Node objects always have (0,0) as (x,y).
Why does this not work this way, and how do I fix it?
I'm not too familiar with JUNG, but I think you have to first specify size of the layout, for example:
layout.setSize(new Dimension(800,600));
Related
I am trying to create a JGraphXAdapter which receives a directed graph as a constructor argument and applied a mxHierarchicalLayout to it. Then, I use mxCellRenderer to create a BufferedImage and write the visualization to a png file.
The problem is that I do not want the edge labels to be visible. I tried (incorrectly) using this command, but it turns off all the labels.
JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<>(directedGraph);
graphAdapter.setLabelsVisible(false);
Is there any way to just turn off the edge labels, but not the vertex labels? Thanks
graphAdapter.getEdgeToCellMap().forEach((edge, cell) -> cell.setValue(null));
Edit: there is not much to comment there... "cells" in JGraphX terminology are objects representing visuals of both edges and vertices. So, if you want to change how one specific element is drawn, you should first find its cell through edge->cell or vertex->cell map.
Cell's "value" is its reference to a represented object. Strings that you see on vertices and edges after JGraphX renders everything are basically cell.value == null ? "" : cell.value.toString() (might look a bit different in the source code).
Is there a graph visualization library which supports laying out the graph in a way such that
the X(or Y) coordinates of nodes are topologically sorted?
Alternatively, I could assign a number to every node, it could lay out the nodes in a way that their Y coordinates respect the ordering of the associated number.
Can JGraph or Jung do that?
JUNG does not provide such a layout but you can define your own layout algorithms.
I am trying to allocate fixed coordinate positions for vertices using static layout. Normally we can get vertex coordinate as Point2D object using layout.transform(Vertex);
Now i wanted to initialize a layout and set the vertices at specified positions but somehow i am stuck. I read here on StackOverflow if i implement Transformer<Vertex, Point2D> interface:
Transformer<Vertex, Point2D> locationTransformer =
new Transformer<Vertex, Point2D>() {
#Override
public Point2D transform(Vertex) {
Point2D p2d = //here i calculate the position
return p2D;
}
};
I have tried this. On fixed graph it works but i have an editable graph and on it nothing happens. I was expecting if i have fixed the position in above, then whichever node i add by mouse click, it should go to the fixed position irrespective where i put it?
can you please give me some idea what could be the reason?
Or with editable graph, is it Overriding the position again somewhere?
UPDATE:
If i remove e.g these implementations from visualizaton viewer:
vv.getRenderContext().setVertexFontTransformer(fontTransfoer);
vv.getRenderContext().setVertexFillPaintTransformer(colorTransformer);
vv.getRenderContext().setVertexShapeTransformer(shapeTransformer);
It starts working, but then the nodes have default shape circular red ones. I want to completely redraw the structure that i had drawn last time. So is there something that can be done with this problem?
I'm using the JUNG API for graph visualization. I cannot figure out how to change the edge label of an edge in the graph.
The situation is that the graph has already been created in the program. I keep dropping edges and nodes and I've found a way to animate those things and update them in the graph. Some of the demos online are helpful. But is there no way to change an edge label of an edge in the graph later?
I understand that JUNG requires the edge labels to be unique.
The basics of edge labelling in JUNG are demonstrated by this snippet of code:
vv.getRenderContext().setEdgeLabelTransformer(new Transformer<MyEdge, String>() {
public String transform(MyEdge e) {
return (e.toString() + " " + e.getWeight() + "/" + e.getCapacity());
}
});
Here, vv is your VisualizationViewer and MyEdge refers to your custom edge class. In my case, I've defined the functions getWeight() and getCapacity() to return the weight and capacity of my edge.
Then I created a popup menu for each edge that allows the user to enter the edge weight and capacity and then used the setWeight() and setCapacity() functions to update my edge. I picked up how exactly to create the edge popups from http://www.grotto-networking.com/JUNG/
You could borrow from this example to set your own edge labels.
I'm new to JUNG. I tried to draw a graph of a tree using the TreeLayout but the tree never comes out like a real tree. Every time the tree looks different. How can I make the tree look like a normal tree with the root on top & the rest of the nodes descending from it?
You have to Initialize the TreeLayout after adding the Vertexes to the graph, I tried that and it worked for me.
You have to do something like the following: (please note that this is a 1 year old code that i had, you might find it to be a little out dated)
Layout<GraphVertex, GraphEdge> layout; //create a layout
layout = new TreeLayout<GraphVertex, GraphEdge>((Forest<GraphVertex, GraphEdge>) g);
// initialize your layout using the graph you created, which has to be of type forest
vv.setGraphLayout(layout);
// set the layout of the visualization viewer you are using to be the layout you just created (the tree layout)
GraphVertex Is the class which represents a vertex in the graph, GraphEdge represents the edges in your graph.