美文网首页
java生成gexf的工具gexf4j的用法

java生成gexf的工具gexf4j的用法

作者: 数据萌新 | 来源:发表于2018-09-26 17:05 被阅读0次

github地址

gexf4j

maven依赖

 <dependency>
      <groupId>it.uniroma1.dis.wsngroup.gexf4j</groupId>
      <artifactId>gexf4j</artifactId>
      <version>1.0.0</version>
    </dependency>

demo代码

package com.neusoft;

import it.uniroma1.dis.wsngroup.gexf4j.core.EdgeType;
import it.uniroma1.dis.wsngroup.gexf4j.core.Gexf;
import it.uniroma1.dis.wsngroup.gexf4j.core.Graph;
import it.uniroma1.dis.wsngroup.gexf4j.core.Mode;
import it.uniroma1.dis.wsngroup.gexf4j.core.Node;
import it.uniroma1.dis.wsngroup.gexf4j.core.data.Attribute;
import it.uniroma1.dis.wsngroup.gexf4j.core.data.AttributeClass;
import it.uniroma1.dis.wsngroup.gexf4j.core.data.AttributeList;
import it.uniroma1.dis.wsngroup.gexf4j.core.data.AttributeType;
import it.uniroma1.dis.wsngroup.gexf4j.core.impl.GexfImpl;
import it.uniroma1.dis.wsngroup.gexf4j.core.impl.StaxGraphWriter;
import it.uniroma1.dis.wsngroup.gexf4j.core.impl.data.AttributeListImpl;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Calendar;


public class StaticGexfGraph {

    public static void main(String[] args) {
        Gexf gexf = new GexfImpl();
        Calendar date = Calendar.getInstance();
        
        gexf.getMetadata()
            .setLastModified(date.getTime())
            .setCreator("Gephi.org")
            .setDescription("A Web network");
        gexf.setVisualization(true);

        Graph graph = gexf.getGraph();
        graph.setDefaultEdgeType(EdgeType.UNDIRECTED).setMode(Mode.STATIC);
        
        AttributeList attrList = new AttributeListImpl(AttributeClass.NODE);
        graph.getAttributeLists().add(attrList);
        
        Attribute attUrl = attrList.createAttribute("class", AttributeType.INTEGER, "Class");
        Attribute attIndegree = attrList.createAttribute("pageranks", AttributeType.DOUBLE, "PageRank");

     
        
        Node gephi = graph.createNode("0");
        gephi
            .setLabel("郝大通")
            .getAttributeValues()
                .addValue(attUrl, "3")
                .addValue(attIndegree, "0.14658");

        
        Node webatlas = graph.createNode("1");
        webatlas
            .setLabel("郝大通")
                .getAttributeValues()
                .addValue(attUrl, "3")
                .addValue(attIndegree, "0.14658");

        gephi.connectTo("0", webatlas).setWeight(0.8f);

        StaxGraphWriter graphWriter = new StaxGraphWriter();
        File f = new File("static_graph_sample.gexf");
        Writer out;
        try {
            out =  new FileWriter(f, false);
            graphWriter.writeToStream(gexf, out, "UTF-8");
            System.out.println(f.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
}

生成的gexf文件

<?xml version='1.0' encoding='UTF-8'?>
<gexf xmlns="http://www.gexf.net/1.2draft" xmlns:viz="http://www.gexf.net/1.2draft/viz" version="1.2">
 <meta lastmodifieddate="2018-09-17">
  <creator>Gephi.org</creator>
  <description>A Web network</description>
 </meta>
 <graph defaultedgetype="undirected" idtype="string" mode="static">
  <attributes class="node" mode="static">
   <attribute id="class" title="Class" type="integer"/>
   <attribute id="pageranks" title="PageRank" type="double"/>
  </attributes>
  <nodes count="2">
   <node id="0" label="郝大通">
    <attvalues>
     <attvalue for="class" value="3"/>
     <attvalue for="pageranks" value="0.14658"/>
    </attvalues>
   </node>
   <node id="1" label="郝大通">
    <attvalues>
     <attvalue for="class" value="3"/>
     <attvalue for="pageranks" value="0.14658"/>
    </attvalues>
   </node>
  </nodes>
  <edges count="1">
   <edge id="0" source="0" target="1" type="undirected" weight="0.8"/>
  </edges>
 </graph>
</gexf>

相关文章

网友评论

      本文标题:java生成gexf的工具gexf4j的用法

      本文链接:https://www.haomeiwen.com/subject/isljoftx.html