I am using JUNG library for network-graphs. I also found an implementation of the sugiyama layout: http://sourceforge.net/tracker/?func=detail&aid=2944336&group_id=73840&atid=539121
But unfortunatly its edge-crossing method seems not to work and I can't solve it. The vertical alignment of the nodes is all but correct.
(Unless theres no error free version of this JUNG algorithm)
Does anyone know of of another implementation? As long as it's free and possible to wrap the it, any Java code (so not necessarily JUNG) would be sufficient.
If theres a very good Library in another language that would take a graph and return a graphml file with fixed positions for nodes it would help also :)
This layout works fine for me:
http://code.google.com/p/daglayout/
I had to make a modification to the code that I couldn't check in : line 275 should be "continue" instead of "return". Other than that, the algorithm seems to work if you give it enough space for your particular graph. I have a heuristic I use based on total nodes and total tree depth.
There is one in this project:
It contains the classic Sugiyama (with all of the generated vertices), the Eiglsperger optimization (pvertex and qvertex), a splay tree, brandes kopf horizontal alignment, 4 kinds of layering:
(top down, longest path, coffman-graham, network simplex)
The code is in the jungrapht-layout module and jar, which has no java.awt dependencies, so it would be easier to use with non-awt rendering (like JavaFX).
scroll down for a picture. The code is here
Related
To simplify the problem, I have a graph that contains nodes and edges which are on a 2D plane.
What I want to be able to do is click a button and it make the automatically layout the graph to look clean. By that I mean minimal crossing of edges, nice space between nodes, maybe even represent the graph scale (weighted edges).
I know this is completely subjective of what is a clean looking graph, but does anyone know of an algorithm to start with, rather than reinventing the wheel?
Thanks.
You will find http://graphdrawing.org/ and this tutorial, by Roberto Tamassia, professor at Brown University, quite helpful.
I like a lot Force-Directed Techniques (pp. 66-72 in the tutorial) like the Spring Embedder.
You assume there is a spring or other force between any two adjacent nodes and let nature (simulation) do the work :)
I would suggest that you take a look at at graphviz. The dot program can take a specification of a graph and generate an image of the network for you somewhat "cleanly". I've linked to the "theory" page that gives you some links that might be relevant if you're interested in the theoretical background. The library and tools themselves are mature enough if you simply want a solution to a problem with layout that you're facing.
I would say as Noufal Ibrahim, but you could also look more precisely at the C API of the graphviz project. It includes a lib to build your graph (libgraph.pdf) with all the nodes and edges, and a lib to layout the graph (libgvc.pdf) (just compute each nodes position), so you can then display it in your own UI for example.
Also JGraph if you want the layouts in Java (I work on the project).
A good visual guide how the most popular layouts actually look: follow the link
Does anyone know a mean to "render" plots or at least trees in console mode (draw it in the console)?
I would be able to render small plots at end of a very long process, by drawing some figures in ASCII mode, in order to have a geeky & fun view of some stats collected into the process.
I would be pleased to discover a library which does that, and I would keep the process 100% java, no shell-hack or third-party software.
-- EDIT
#lbalazscs and #Fortega made interesting answers, but the background of my question is to know if it exists such a library, and I will add some details I missed the first time :
The output should be able to display trees, binary trees (linked by #lbalazscs here), but also simple graphs such as bargraphs or so.
I will let this question "unanswered" for a while, and if there is no probant answer, #lbalazscs will get the point ;)
You can print ascii trees with minimal code. See the second answer to this question: How to print binary tree diagram?
(the second answer because this one is not only for binary trees)
For people coming here looking for a pure Java tree drawing library: I recommend text-tree, which draws trees like this (and other styles, a lot of possible config if you need it):
some text
├─── more text
├─── and more
│ ├─── still more
│ ╰─── more
╰─── the end
Full disclosure: I am the author of text-tree.
If I remember well, you can draw ascii graphs with javaplot
I've been away from Java for several years, so forgive my rust. I inherited some code targeting Java SE 1.5. When building under Java 1.7.0, there are several build warnings with the text:
Crossings is internal proprietary API and may be removed in a future
release.
I would like to remedy this build warning, probably by replacing this code with my own. On examining the code, I see that the full class in question is sun.awt.geom.Crossings. The OpenJDK code is available, of course, but I do not understand the overall purpose of the class or its concrete implementations. What is the purpose of sun.awt.geom.Crossings? Where can I find more documentation?
Since #ee. has not come back to rewrite his or her comments as an answer, I will do so here. #ee., if you do stop by, I'd be happy to put the checkmark by your answer instead.
Check this http://docstore.mik.ua/orelly/java/awt/ch02_01.htm#JAWT-CH-2-FIG-9:
Filling polygons is a complex topic. It is not as easy as filling rectangles or ovals because a polygon may not be closed and its edges may cross. AWT uses an even-odd rule to fill polygons. This algorithm works by counting the number of times each scan line crosses an edge of the polygon. If the total number of crossings to the left of the current point is odd, the point is colored. If it is even, the point is left alone.
You can see Crossings class is used here; for example: Area.contains() to check the crossings of a rectangular area inside an area. in http://kickjava.com/src/java/awt/geom/Area.java.htm. Since its application is mostly used inside other commonly used classes, you don't have to worry so much. But, if you use it directly, then you may have a problem in the future!
I am wondering if there exist libraries that could help me draw such figures on screen quickly using JAVA.
The dataset and number of nodes etc need to be parametrized.
If no such libraries exist, which tools in Swing would get me started. I want a quick and dirty way to represent this information.
Edit :
Also it would help if you could tell me what to search on google to get results for such a tailored query.
You can call GraphViz from within Java, converting any Java-based tree structure into the necessary GraphViz formats, and then reading the resulting .png image back into Java. That is probably the easiest approach, in terms of code-to-write (credit goes to SyntaxT3rr0r for proposing it first).
Customizing JGraph would also work, but I doubt that any of the default node-types would cut it. There are examples in the manual covering how to code your own node types and representations. JGraph allows easy graphical editing of node labels and positions, has hierarchical layouts (the type you use for trees); and it supports "ports" of origin (and also destination) for your parent-child edges. You can try their editor demo (included in their default download) if you just want a quick test.
I am using JUNG for a project and when I am displaying relatively large graphs eg 1500 nodes, my pc would not be able to handle it (graphs are rendered but If I want to navigate the graph the system become very slow). Any Suggestions.
So, there are two things that JUNG visualization doesn't always scale very well right now:
iterative force-directed layouts
interaction: figuring out which node or edge (if any) is being referenced for hover and click events.
It sounds like it's the latter that you're running into right now.
Depending on your requirements, you have a couple of options:
(a) turn off mouse events, or at least hover events
(b) hack the visualization system so that lookups of event targets aren't O(m+n).
Simple solutions for (b) basically just partition the viewing area into smallish chunks and only sends events to elements that are in the same chunk as the pointer. (Obviously, the smaller you make the chunks, the more memory is required.)
We've had plans to do (b) (and a design sketched out) for some time but have been working on other things. Anyone that wants to help with a more permanent solution, please contact me.
How much memory are you starting your VM with? Assuming your working on windows, looking at the Task Manager, does the VM hit the maximum amount of allocated memory and start using swap?
The problem probably lies with the calculation of your vertices' positions. The only layout that I've found fairly easy to calculate was the Tree Layout and obviously that's not suitable for all data sets.
The solution probably is to write your own custom layout with a lot less calculations than say an FRLayout.