2011-03-13 194 views
3

我有一些圖,我想使用JUNG2來顯示,如下圖所示。JUNG佈局問題

enter image description here

我已經打了JUNG2的一些佈局,但我總是得到這樣的圖像:

enter image description here

是否有可能,我想鋪陳圖不寫一個新的佈局?

在此先感謝

德米特里

UPD:這裏是我用於可視化圖形代碼:

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

現在我可以得出一個曲線圖像這樣:

enter image description here

+0

您的圖片並沒有露面......他們成了1×1圖片...是http://i.imgur.com/個人STOR年齡網站? – eee

+0

沒關係...似乎我的辦公室防火牆阻止鏈接到i.imgur.com – eee

回答

1

它與你如何在JUNG中創建頂點和邊緣有關。

對於領先地位,建議你指createVertices()和createEdges()在JUNG示例方法WorldMapGraphDemo.classedu.uci.ics.jung.samples榮樣品下-2.0.1.jar從您的JUNG 2.0框架庫。

與使用Map對象存儲用戶定義的頂點和邊緣信息的其他示例相比,方法中使用的方法更加清晰。其餘的則使用隨機生成的,庫生成的或文件生成的信息。

一旦明確給你,你可以參考PluggableRendererDemo.class提高你的圖形(如相應的包裝edu.uci.ics.jung.visualization.decorators下裝飾變壓器渲染edu.uci.ics.jung.visualization.renderers用於裝飾和渲染頂點,邊,方向箭頭,形狀,大小等)

+0

謝謝。我瞭解JUNG2的基礎知識,但我不明白我需要將哪些變換應用到我的圖中,例如:a)IE節點位於圖的最左側位置並且垂直居中到除FE外的所有其他節點); b)FE節點位於最右側位置並垂直居中(相對於除IE以外的所有其他節點)。我怎樣才能做到這一點? –

1

好吧......現在,我明白你真的想解決。德米特里,你可以檢查另一個JUNG的例子,L2RTreeLayoutDemo.class ...它看起來如此接近你想達到的目標。如果您想要鎖定特定頂點的位置,請爲每一個下面創建Layout之後將其添加到VisualizationViewer/VisualizationImageServerCan Jung graphics appear in the same place every time?

+0

感謝您的提示。我實際上看了L2RTreeLayoutDemo,現在我可以看見樹木(請參閱我的消息中的最後一張圖片)。但是,問題是項目網絡不是一棵樹。所有「最終」節點都連接到「最終事件」節點。有沒有辦法以某種方式重新使用TreeLayout的代碼非樹? –

+0

@Dmitri:在這種情況下,你不能使用'Tree'或'Forest'類...可能,你可以像使用** WorldMapGraphDemo.class **例子那樣使用'StaticLayout'佈局,並修改它來定位節點一個M×N網格(其中每個節點位置將位於佈局的相應網格點) – eee

+0

是否還有其他解決方案(如配置FRLayout以使其符合我的需求)?我知道那個網格算法,但我不想重新發明輪子。 –