2016-01-06 96 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>