0

I'm using Gephi to create a graphmodel and then export that into a .graphml file. However when I create the .graphml file the colour attribute is not maintained.

The following is how I create a gephi graphmodel from my GraphClass (comprised of edges of type E, and nodes of type V) and export it to a .graphml file. In this case I create a random K-tree

    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();
        }

    }

I want to set the colour of the nodes to red but in yED It just shows them as the default yellowenter image description here

A Node from the GraphML looks as follows:

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

Is there some way in Gephi to directly change the value of a specific data tag? I believe my problem is the format of the graphml data tag is incorrect and not recognized by yED, and that this incorrect format may have to do with how it the file is written.

TCulos
  • 183
  • 3
  • 13

1 Answers1

0

For graphical information yed uses an additional namespace. You have to use it. For just one node your GraphMl File should look like followed.

The way you can create this with gephi and java I don't know at the moment.

But if its possible for you to create somehow all the lines arround the "node" tag, then you only have to change the < node id="YourLabel" > and < y:Fill color="#FF0000" > to get a node with your color. The node id will be changed by yed to "n0" when you save the file using yed. So you additionally should explicitly set also the real node label, which you will find just before "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>
gequest
  • 59
  • 4