3

I have some graph, which I want to display using JUNG2 like in the image below.

enter image description here

I've played with some layouts of JUNG2, but I'm always getting an image like this:

enter image description here

Is it possible to lay out the graph as I wish without writing a new layout?

Thanks in advance

Dmitri

UPD: Here is the code I used for visualizing the graph:

private Embedded createSampleGraph() {
Embedded imageComponent = null;

try {
    final DocumentBuilderFactory docBuilderFactory =
            DocumentBuilderFactory
                    .newInstance();
    final DocumentBuilder docBuilder =
            docBuilderFactory.newDocumentBuilder();
    final Document document = docBuilder.newDocument();
    final Element svgelem = document.createElement("svg");
    document.appendChild(svgelem);

    final SVGGraphics2D graphic2d = new SVGGraphics2D(document);

    final Graph<String, String> graph = createGraph();
    final VisualizationImageServer<String, String> server =
            createServer(graph);

    server.printAll(graphic2d);

    final Element el = graphic2d.getRoot();
    el.setAttributeNS(null, "viewBox", "0 0 350 350");
    el.setAttributeNS(null, "style", "width:100%;height:100%;");

    final ByteArrayOutputStream bout = new ByteArrayOutputStream();

    final Writer out = new OutputStreamWriter(bout, "UTF-8");
    graphic2d.stream(el, out);

    final JungResource source = new JungResource(bout);

    TPTApplication.getCurrentApplication().addResource(source);

    imageComponent = new Embedded("", source);

    imageComponent.setWidth(DEFAULT_WIDTH_PIXELS, UNITS_PIXELS);
    imageComponent.setHeight(DEFAULT_HEIGHT_PIXELS, UNITS_PIXELS);
    imageComponent.setMimeType("image/svg+xml");
    addComponent(imageComponent);
} catch (final UnsupportedEncodingException exception) {
    LOGGER.error(ErrorCodes.M_001_UNSUPPORTED_ENCONDING, exception);
} catch (final SVGGraphics2DIOException exception) {
    LOGGER.error(ErrorCodes.M_002_SVG_GRAPHICS_2D_IO, exception);
} catch (final ParserConfigurationException exception) {
    LOGGER.error(ErrorCodes.M_003_PARSER_CONFIGURATION, exception);
}
return imageComponent;
}

private VisualizationImageServer<String, String> createServer(
    final Graph<String, String> aGraph) {
final Layout<String, String> layout = new FRLayout<String, String>(
        aGraph);

layout.setSize(new Dimension(300, 300));
final VisualizationImageServer<String, String> vv =
        new VisualizationImageServer<String, String>(
                layout, new Dimension(350, 350));
vv.getRenderContext().setVertexLabelTransformer(
        new ToStringLabeller<String>());
return vv;
}

private Graph<String, String> createGraph() {
final Graph<String, String> graph =
        new DirectedSparseMultigraph<String, String>();
final String vertex1 = "IE";
final String vertex2 = "P1";
final String vertex3 = "P2";
final String vertex4 = "P3";
final String vertex5 = "FE";

graph.addVertex(vertex1);
graph.addVertex(vertex2);
graph.addVertex(vertex3);
graph.addVertex(vertex4);
graph.addVertex(vertex5);

graph.addEdge("1", vertex1, vertex2, EdgeType.DIRECTED);
graph.addEdge("2", vertex2, vertex3, EdgeType.DIRECTED);
graph.addEdge("3", vertex3, vertex5, EdgeType.DIRECTED);
graph.addEdge("4", vertex1, vertex4, EdgeType.DIRECTED);
graph.addEdge("5", vertex4, vertex5, EdgeType.DIRECTED);
return graph;
}

UPD 17.03.2011

Now I can draw a graph like this:

enter image description here

Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
  • your images didn't show up...they became 1 x 1 images... is http://i.imgur.com/ a personal storage website? – eee Mar 14 '11 at 01:25
  • nevermind... it seems that my office firewall blocks links to i.imgur.com – eee Mar 14 '11 at 01:47

3 Answers3

4

If you want to pin the locations of specific vertices, do the following for each one after creating the Layout and before adding it to the VisualizationViewer/VisualizationImageServer:

layout.setLocation(v, location);
layout.lock(v, true);

http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/algorithms/layout/Layout.html

sys13
  • 148
  • 9
Joshua O'Madadhain
  • 2,704
  • 1
  • 14
  • 18
1

It has to do with how you create your vertices and edges in JUNG.

For a headstart, I suggest you to refer to createVertices() and createEdges() methods in the JUNG example WorldMapGraphDemo.class in edu.uci.ics.jung.samples package under the jung-samples-2.0.1.jar from your JUNG 2.0 Framework library.

The approach used in the methods are clearer compared to other provided examples where it uses the Map objects to store user-defined vertices and edges information. The rest uses random-generated, library-generated or file-generated information.

Once it is clear to you, you can refer to PluggableRendererDemo.class to improve your graph (like decorating Transformer and Renderer classes under respective packages edu.uci.ics.jung.visualization.decorators and edu.uci.ics.jung.visualization.renderers for decorating and rendering vertex, edge, direction arrow, shape, size, etc.)

eee
  • 1,043
  • 7
  • 5
  • Thanks. I understand the basics of JUNG2, but I don't understand what transforms I need to apply to my graph so that, for example, a) the IE node is placed at the left-most position of the graph and vertically centered (with respect to all other nodes, except FE) and b) the FE node is placed at the right-most position and vertically centered (with respect to all other nodes, except IE). How can I do that? – Glory to Russia Mar 14 '11 at 11:27
1

Ok...Now, I understand what you really want to solve. Dmitri, you can check another JUNG example, L2RTreeLayoutDemo.class...it looks so close to what you want to achieve.

Or, you can study SO post below: Can Jung graphics appear in the same place every time?

Community
  • 1
  • 1
eee
  • 1,043
  • 7
  • 5
  • Thanks for your hint. I actually looked at L2RTreeLayoutDemo and now I can visualize trees (see the last image in my message). However, the problem is that a project network is not a tree. All "final" nodes are connected to the "final event" node. Is there a way to somehow re-use the code of TreeLayout for non-trees? – Glory to Russia Mar 17 '11 at 09:26
  • @Dmitri: In that case, you cannot use `Tree` or `Forest` class...Possibly, you can use the `StaticLayout` layout as in **WorldMapGraphDemo.class** example and modify it to position your nodes on a M x N grid (where each node position will be at its respective grid point of the layout) – eee Mar 17 '11 at 14:52
  • Are there any other solutions (like configuring FRLayout so that it does what I want) ? I know about that grid algorithm, but I'd prefer not to re-invent the wheel. – Glory to Russia Mar 17 '11 at 15:14
  • @Dmitri: you can modify any of the layout classes from JUNG sources and create your own but it will take a little bit of study...I do find out some open-source projects create their own JUNG layout algorithms. I haven't explored fully JUNG to be able to come up with my own layout algorithm yet... – eee Mar 18 '11 at 09:44
  • @Dmitri please did you find solution for this ! I need this for my Project – Héla Apr 08 '16 at 08:07
  • @Hedouda I don't remember any more. But if you need really badly, you can look at my code and figure out for yourself, whether and how I solved it back then. Here's the link: https://altruix.wordpress.com/portfolio/project-control-center/ – Glory to Russia Apr 08 '16 at 10:24