将Gephi导出为GraphML而不维护Node颜色

时间:2016-01-06 00:42:40

标签: java filewriter gephi graphml

我使用Gephi创建了一个graphmodel,然后将其导出到.graphml文件中。但是,当我创建.graphml文件时,不会维护颜色属性。

以下是我如何从GraphClass创建gephi graphmodel(由E类型的边和V类型的节点组成)并将其导出到.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中是否有某种方法可以直接更改特定数据标记的值?我相信我的问题是graphml数据标签的格式不正确并且yED无法识别,并且这种不正确的格式可能与文件的编写方式有关。

1 个答案:

答案 0 :(得分:0)

对于图形信息,yed使用额外的命名空间。你必须使用它。 对于一个节点,您的GraphMl文件应该如下所示。

你可以用gephi和java创建这个方法,我现在还不知道。

但是,如果您可以以某种方式创建“node”标记周围的所有行,那么您只需要更改&lt; node id =“YourLabel”&gt;和&lt; y:填充颜色=“#FF0000”&gt;用你的颜色获得一个节点。 使用yed保存文件时,节点ID将被改为“n0”。所以你还应该明确地设置真正的节点标签,你可以在“YourLabel&lt; y:LabelModel&gt;”之前找到它。

<?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>