JUNG (Java Graph): How to prevent overlapping vertex and boundary labels?

I am currently using Jung to draw graphs (I really mean graphs not diagrams!). The problem is that the vertex and edge labels overlap with the vertices and edges. This leads to weird schedules. The problem is not related to the specific layout algorithm (although I used FRLayout).

Is there any way to tell JUNG about preventing these overlaps? I was hoping this was already implemented, and the only problem was finding the right options!

+6
java graph label
source share
5 answers

(The following answer suggests Jung2, I am not familiar with pre-Jung2).

One of Jung’s strengths is that it is very extensible and easy to expand. Jung allows you to connect various transformers (simple rendering properties), visualization tools (more complex rendering), etc., to customize the behavior when the default is not entirely correct. They are usually set to a RenderContext (which you can get from your VisualizationViewer).

On the other hand, its drawbacks are its complexity and lack of decent documentation. If you are going to make any moderate or heavy tuning, you need to delve into Jung's source.

For example, the rendering of vertex labels can be configured by connecting a new vertex label visualizer (Renderer.VertexLabel interface). For example, you can create an instance of BasicVertexLabelRenderer and specify a different position (north, west, center, etc.). You can put your label in the center if you want to change the shape to something more than this circle (for this, install your own vertex shape transformer - an instance of Transformer). Alternatively, you can create your own implementation of this interface, which creates a background under the label text.

You can also set your own modified version of Renderer.EdgeLabel (see Jung BasicEdgeLabelRenderer) to adjust the position of the edge label.

+5
source share

You just need to set the label offset:

vv.getRenderContext().setLabelOffset(20); 

Where vv is your VisualizationViewer object .

+7
source share

By the way, if you want to change the color, at first it seems to be a big pain. but I recently discovered this little trick:

 Transformer labelTransformer = new ChainedTransformer<String,String>(new Transformer[]{ new ToStringLabeller<String>(), new Transformer<String,String>() { public String transform(String input) { return "<html><font color=\"yellow\">"+input; }}}); context.setVertexLabelTransformer(labelTransformer); 
+6
source share

I think this is exactly how it is implemented, the authors of the code probably do not consider it as a problem. The JUNG library is open source, so you can make changes to it as you wish.

If you don't want to change the code, a simple fix to make the labels more readable would be to simply change the color of the labels so that they differ from the edges.

+2
source share

I ran into the same issue of overlapping Edge Labels. The quick and dirty solution that I used to place border labels was to fill a specific length with spaces in the string value.

 //pad some white spaces to value of edge when adding edges to the graph graph.addEdge(String.format("%-35s", "Edge-A"), 1, 2); 

This will avoid edging the edges and overlap in the center, especially when building a bipartite graph. Hope this helps some of you there!

+1
source share

All Articles