use of edu.uci.ics.jung.visualization.VisualizationImageServer in project opennms by OpenNMS.
the class DefaultBusinessServiceStateMachine method renderGraphToPng.
@Override
public void renderGraphToPng(File tempFile) {
m_rwLock.readLock().lock();
try {
Layout<GraphVertex, GraphEdge> layout = new KKLayout<GraphVertex, GraphEdge>(m_g);
// Size of the layout
layout.setSize(new Dimension(1024, 1024));
VisualizationImageServer<GraphVertex, GraphEdge> vv = new VisualizationImageServer<GraphVertex, GraphEdge>(layout, layout.getSize());
// Viewing area size
vv.setPreferredSize(new Dimension(1200, 1200));
vv.getRenderContext().setVertexLabelTransformer(new Transformer<GraphVertex, String>() {
@Override
public String transform(GraphVertex vertex) {
if (vertex.getBusinessService() != null) {
return String.format("BS[%s]", vertex.getBusinessService().getName());
}
if (vertex.getIpService() != null) {
IpService ipService = vertex.getIpService();
return String.format("IP_SERVICE[%s,%s]", ipService.getId(), ipService.getServiceName());
}
if (vertex.getReductionKey() != null) {
return String.format("RK[%s]", vertex.getReductionKey());
}
return "UNKNOWN";
}
});
vv.getRenderContext().setEdgeLabelTransformer(new Transformer<GraphEdge, String>() {
@Override
public String transform(GraphEdge edge) {
return String.format("%s", edge.getMapFunction().getClass().getSimpleName());
}
});
// 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", tempFile);
} catch (IOException e) {
throw Throwables.propagate(e);
}
} finally {
m_rwLock.readLock().unlock();
}
}
use of edu.uci.ics.jung.visualization.VisualizationImageServer 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);
}
}
Aggregations