2016-01-06 478 views
0

我使用Gephi來創建一個graphmodel,然後將其導出到一個.graphml文件中。但是,當我創建.graphml文件時,color屬性不被維護。將Gephi導出爲GraphML而不是維護節點顏色

以下是我如何從我的GraphClass(由類型E的邊和類型V的節點組成)創建gephi圖模型並將其導出到.graphml文件。在這種情況下,我創建一個隨機K-樹

public void testCreateGraph() throws Exception { 
     KTree K1 = new KTree(4); 

     for (int i = 0; i < 95; i++) { 
      K1.addRandomVertex(); 
     } 
     GraphMLCreator<KVertex,KEdge<KVertex>> creator = new GraphMLCreator(); 
     creator.create(K1); 
     creator.sendToDB(); 
    } 

public void create(GraphClass<V,E> G){ 

     graph = graphModel.getUndirectedGraph(); 
     this.addNodes(G.getVertices()); 
     this.addEdges(G.getEdges()); 

    } 

    private void addEdges(Collection<E> edges){ 

     for (E e: edges){ 
      Node n0 = graph.getNode(e.getEndPoints().getFirst().getLabel()); 
      Node n1 = graph.getNode(e.getEndPoints().getSecond().getLabel()); 

      org.gephi.graph.api.Edge e1 = graphModel.factory().newEdge(n0, n1, 1f, false); 
      graph.addEdge(e1); 
     } 

    } 

    private void addNodes(Collection<V> nodes){ 

     for (V v:nodes){ 
      Node n0 = graphModel.factory().newNode(v.getLabel()); 
      n0.getNodeData().setLabel(v.getLabel()); 
      n0.getNodeData().setColor(255,0,0); 

      graph.addNode(n0); 
     } 

    } 

    public void createGraphML(){ 

     //Export full graph 
     ExportController ec = Lookup.getDefault().lookup(ExportController.class); 
     try { 
      ec.exportFile(new File("io_gexf.gexf")); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
      return; 
     } 

     //Export only visible graph 
     GraphExporter exporter = (GraphExporter) ec.getExporter("gexf");  //Get GEXF exporter 
     exporter.setExportVisible(true); //Only exports the visible (filtered) graph 
     exporter.setWorkspace(workspace); 
     try { 
      ec.exportFile(new File("io_gexf.gexf"), exporter); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
      return; 
     } 

     //Export to Writer 
     Exporter exporterGraphML = ec.getExporter("graphml");  //Get GraphML exporter 
     exporterGraphML.setWorkspace(workspace); 
     StringWriter stringWriter = new StringWriter(); 
     ec.exportWriter(stringWriter, (CharacterExporter) exporterGraphML); 

     FileWriter fw = null; 
     try { 
      fw = new FileWriter("my-file.graphml"); 
      fw.write(stringWriter.toString()); 
      fw.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

我想節點的顏色設置爲紅色,但在yED這只是表明他們作爲默認的黃色enter image description here

來自GraphML的節點看起來如下:

<node id="V0"> 
    <data key="label">V0</data> 
    <data key="size">1.0</data> 
    <data key="r">65025</data> 
    <data key="g">0</data> 
    <data key="b">0</data> 
    <data key="x">418.6446</data> 
    <data key="y">-191.08676</data> 
</node> 

是否有某種方式在Gephi中直接y更改特定數據標籤的值?我相信我的問題是graphml數據標籤的格式不正確,不能被yED識別,並且這種不正確的格式可能與它如何寫入文件有關。

回答

0

對於yed的圖形信息使用額外的名稱空間。你必須使用它。 對於只有一個節點,GraphMl文件應該如下所示。

你可以用gephi和java創建這個方法我現在還不知道。但是,如果它可能爲您創建以某種方式圍繞「節點」標籤的所有行,那麼您只需更改<節點id =「YourLabel」>和< y:Fill color =「#FF0000」>用你的顏色來獲得一個節點。 使用yed保存文件時,節點ID將被更改爲「n0」。所以你另外應該明確地設置真正的節點標籤,你會在「YourLabel < y:LabelModel>」之前找到它。

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:java="http://www.yworks.com/xml/yfiles-common/1.0/java" xmlns:sys="http://www.yworks.com/xml/yfiles-common/markup/primitives/2.0" xmlns:x="http://www.yworks.com/xml/yfiles-common/markup/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xmlns:yed="http://www.yworks.com/xml/yed/3" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd"> 
    <key attr.name="Beschreibung" attr.type="string" for="graph" id="d0"/> 
    <key for="port" id="d1" yfiles.type="portgraphics"/> 
    <key for="port" id="d2" yfiles.type="portgeometry"/> 
    <key for="port" id="d3" yfiles.type="portuserdata"/> 
    <key attr.name="url" attr.type="string" for="node" id="d4"/> 
    <key attr.name="description" attr.type="string" for="node" id="d5"/> 
    <key for="node" id="d6" yfiles.type="nodegraphics"/> 
    <key for="graphml" id="d7" yfiles.type="resources"/> 
    <key attr.name="url" attr.type="string" for="edge" id="d8"/> 
    <key attr.name="description" attr.type="string" for="edge" id="d9"/> 
    <key for="edge" id="d10" yfiles.type="edgegraphics"/> 
    <graph edgedefault="directed" id="G"> 
    <data key="d0"/> 
    <node id="YourLabel"> 
     <data key="d6"> 
     <y:ShapeNode> 
      <y:Geometry height="30.0" width="30.0" x="415.0" y="203.0"/> 
      <y:Fill color="#FF0000" transparent="false"/> 
      <y:BorderStyle color="#000000" type="line" width="1.0"/> 
      <y:NodeLabel alignment="center" autoSizePolicy="content" fontFamily="Dialog" fontSize="12" fontStyle="plain" hasBackgroundColor="false" hasLineColor="false" height="18.701171875" modelName="custom" textColor="#000000" visible="true" width="58.708984375" x="-14.3544921875" y="5.6494140625">YourLabel<y:LabelModel> 
       <y:SmartNodeLabelModel distance="4.0"/> 
      </y:LabelModel> 
      <y:ModelParameter> 
       <y:SmartNodeLabelModelParameter labelRatioX="0.0" labelRatioY="0.0" nodeRatioX="0.0" nodeRatioY="0.0" offsetX="0.0" offsetY="0.0" upX="0.0" upY="-1.0"/> 
      </y:ModelParameter> 
      </y:NodeLabel> 
      <y:Shape type="rectangle"/> 
     </y:ShapeNode> 
     </data> 
    </node> 
    </graph> 
    <data key="d7"> 
    <y:Resources/> 
    </data> 
</graphml>