Export a GraphML graph as image with Java

In my post „How to parse GraphML files with a cool Java library“ I showed how to parse a GraphML XML file. In this post I want to show you, how you can visualize it. All you need is the yEd – Graph Editor and some extra markup in your XML file.

At the beginning we had this XML file:

something.xml

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns">
  <!-- Properties -->
  <key id="name" for="node" attr.name="name" attr.type="string"/>
  <key id="since" for="edge" attr.name="since" attr.type="int"/>
  <!-- Graph -->
  <graph id="G" edgedefault="directed">
    <!-- Vertices -->
    <node id="1">
      <data key="name">Benny</data>
    </node>
    <node id="2">
      <data key="name">Daniel</data>
    </node>
    <node id="3">
      <data key="name">Robert</data>
    </node>
    <node id="4">
      <data key="name">Jan</data>
    </node>
    <node id="5">
      <data key="name">Melanie</data>
    </node>
    <node id="6">
      <data key="name">Julia</data>
    </node>
    <node id="7">
      <data key="name">Josefine</data>
    </node>
    <node id="8">
      <data key="name">Kristin</data>
    </node>
    <!-- Edges -->
    <edge id="9" source="1" target="5" label="knows">
      <data key="since">2008</data>
    </edge>
    <edge id="10" source="2" target="6" label="knows" >
      <data key="since">2011</data>
    </edge>
    <edge id="11" source="3" target="7" label="knows">
      <data key="since">2010</data>
    </edge>
    <edge id="12" source="4" target="8" label="knows">
      <data key="since">2009</data>
    </edge>
    <edge id="13" source="2" target="5" label="knows">
      <data key="since">2011</data>
    </edge>
    <edge id="14" source="8" target="1" label="knows">
      <data key="since">2010</data>
    </edge>
  </graph>
</graphml>

Now we need some extra data keys (d5 and d6, don’t forget to define them in the properties area!) and an extended XML namespace. We also have to rename the file from something.xml to something.graphml. The final file should look like this:

something.graphml

<?xml version="1.0" encoding="UTF-8"?>
<graphml 
  xmlns="http://graphml.graphdrawing.org/xmlns" 
  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"
>
  <!-- Properties -->
  <key id="name" for="node" attr.name="name" attr.type="string"/>
  <key id="since" for="edge" attr.name="since" attr.type="int"/>
  <key for="node" id="d5" attr.name="description" attr.type="string" />
  <key for="node" id="d6" yfiles.type="nodegraphics"/>  
  <!-- Graph -->
  <graph id="G" edgedefault="directed">
    <!-- Vertices -->
    <node id="1">
      <data key="name">Benny</data>
      <data key="d5"/>
      <data key="d6">
        <y:ShapeNode>
          <y:NodeLabel>Benny</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <node id="2">
      <data key="name">Daniel</data>
      <data key="d5"/>
      <data key="d6">
        <y:ShapeNode>
          <y:NodeLabel>Daniel</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <node id="3">
      <data key="name">Robert</data>
      <data key="d5"/>
      <data key="d6">
        <y:ShapeNode>
          <y:NodeLabel>Robert</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <node id="4">
      <data key="name">Jan</data>
      <data key="d5"/>
      <data key="d6">
        <y:ShapeNode>
          <y:NodeLabel>Jan</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <node id="5">
      <data key="name">Melanie</data>
      <data key="d5"/>
      <data key="d6">
        <y:ShapeNode>
          <y:NodeLabel>Melanie</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <node id="6">
      <data key="name">Julia</data>
      <data key="d5"/>
      <data key="d6">
        <y:ShapeNode>
          <y:NodeLabel>Julia</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <node id="7">
      <data key="name">Josefine</data>
      <data key="d5"/>
      <data key="d6">
        <y:ShapeNode>
          <y:NodeLabel>Josefine</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <node id="8">
      <data key="name">Kristin</data>
      <data key="d5"/>
      <data key="d6">
        <y:ShapeNode>
          <y:NodeLabel>Kristin</y:NodeLabel>
          <y:Shape type="rectangle"/>
        </y:ShapeNode>
      </data>
    </node>
    <!-- Edges -->
    <edge id="9" source="1" target="5" label="knows">
      <data key="since">2008</data>
    </edge>
    <edge id="10" source="2" target="6" label="knows" >
      <data key="since">2011</data>
    </edge>
    <edge id="11" source="3" target="7" label="knows">
      <data key="since">2010</data>
    </edge>
    <edge id="12" source="4" target="8" label="knows">
      <data key="since">2009</data>
    </edge>
    <edge id="13" source="2" target="5" label="knows">
      <data key="since">2011</data>
    </edge>
    <edge id="14" source="8" target="1" label="knows">
      <data key="since">2010</data>
    </edge>
  </graph>
</graphml>

After doing that, we can open the file in yEd. There we can click on „Layout“ – „Circular…“ to see a beautiful graph. We can save this as an image with „File“ – „Export…“.

The result will look like this:

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.