Search in sources :

Example 1 with Graph

use of edu.uci.ics.jung.graph.Graph in project opennms by OpenNMS.

the class GraphUtils method renderGraphToFile.

public static void renderGraphToFile(Graph<VertexRef, Edge> jungGraph, File file) {
    final edu.uci.ics.jung.algorithms.layout.Layout<VertexRef, Edge> jungLayout = new KKLayout<>(jungGraph);
    // Size of the layout
    jungLayout.setSize(new Dimension(1800, 1800));
    final Set<VertexRef> roots = jungGraph.getVertices().stream().filter(v -> jungGraph.getInEdges(v).isEmpty()).collect(Collectors.toSet());
    VisualizationImageServer<VertexRef, Edge> vv = new VisualizationImageServer<>(jungLayout, jungLayout.getSize());
    // Viewing area size
    vv.setPreferredSize(new Dimension(2000, 2000));
    vv.getRenderContext().setVertexLabelTransformer(vertex -> vertex.getLabel());
    vv.getRenderContext().setEdgeLabelTransformer(edge -> edge.getLabel());
    vv.getRenderContext().setVertexFillPaintTransformer(vertexRef -> {
        if (roots.contains(vertexRef)) {
            return Color.RED;
        }
        return Color.BLUE;
    });
    // Draw vertices according to in/out edge count. The more edges, the bigger the vertex
    vv.getRenderContext().setVertexShapeTransformer(vertexRef -> {
        Collection<Edge> inEdges = jungGraph.getInEdges(vertexRef);
        Collection<Edge> outEdges = jungGraph.getOutEdges(vertexRef);
        int edgeCount = inEdges.size() + outEdges.size();
        int widthHeight = (edgeCount / 4 + 1) * 20;
        return new Ellipse2D.Float(-1 * widthHeight / 2, -1 * widthHeight / 2, widthHeight, widthHeight);
    });
    // Create the buffered image
    BufferedImage image = (BufferedImage) vv.getImage(new Point2D.Double(vv.getGraphLayout().getSize().getWidth() / 2, vv.getGraphLayout().getSize().getHeight() / 2), new Dimension(vv.getGraphLayout().getSize()));
    // Render
    try {
        ImageIO.write(image, "png", file);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
Also used : Color(java.awt.Color) Point2D(java.awt.geom.Point2D) BufferedImage(java.awt.image.BufferedImage) Edge(org.opennms.features.topology.api.topo.Edge) Collection(java.util.Collection) Set(java.util.Set) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) Dimension(java.awt.Dimension) Ellipse2D(java.awt.geom.Ellipse2D) KKLayout(edu.uci.ics.jung.algorithms.layout.KKLayout) ImageIO(javax.imageio.ImageIO) Graph(edu.uci.ics.jung.graph.Graph) VertexRef(org.opennms.features.topology.api.topo.VertexRef) VisualizationImageServer(edu.uci.ics.jung.visualization.VisualizationImageServer) KKLayout(edu.uci.ics.jung.algorithms.layout.KKLayout) Dimension(java.awt.Dimension) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) VisualizationImageServer(edu.uci.ics.jung.visualization.VisualizationImageServer) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Edge(org.opennms.features.topology.api.topo.Edge)

Aggregations

Throwables (com.google.common.base.Throwables)1 KKLayout (edu.uci.ics.jung.algorithms.layout.KKLayout)1 Graph (edu.uci.ics.jung.graph.Graph)1 VisualizationImageServer (edu.uci.ics.jung.visualization.VisualizationImageServer)1 Color (java.awt.Color)1 Dimension (java.awt.Dimension)1 Ellipse2D (java.awt.geom.Ellipse2D)1 Point2D (java.awt.geom.Point2D)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 ImageIO (javax.imageio.ImageIO)1 Edge (org.opennms.features.topology.api.topo.Edge)1 VertexRef (org.opennms.features.topology.api.topo.VertexRef)1