Search code examples
javagraphjgraphxgraphml

Importing GraphML to create graph in JGraphX


For a project that I working on, I am getting information in the form of GraphML format(I can change the graphML file as well) and I want to create the desired graph from that information, I am able to get the correct layout, with appropriate edges and nodes by using the mxGraphMlCodec.decode(doc, graph) method, but the generated graph lacks the additional information of edge labels as provided with the graphML file.

I could not find any example of a graphML file which is converted in this way to a graph. I have just followed the guidelines for a graphML file as stated in the GraphML primer to create the graphML file.

Here is the GraphML code:

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns">
<key id="d1" for="edge" attr.name="edgeData" attr.type="double"/>
<graph id="DTMC" edgedefault="directed">
<node id="0"/>
<edge source="0" target="1">
<data key="d1">0.5</data>
</edge>
<edge source="0" target="2">
<data key="d1">0.5</data>
</edge>
<node id="1"/>
<edge source="1" target="3">
<data key="d1">0.5</data>
</edge>
<edge source="1" target="4">
<data key="d1">0.5</data>
</edge>
<node id="2"/>
<edge source="2" target="5">
<data key="d1">0.5</data>
</edge>
<edge source="2" target="6">
<data key="d1">0.5</data>
</edge>
<node id="3"/>
<edge source="3" target="1">
<data key="d1">0.5</data>
</edge>
<edge source="3" target="7">
<data key="d1">0.5</data>
</edge>
<node id="4"/>
<edge source="4" target="8">
<data key="d1">0.5</data>
</edge>
<edge source="4" target="9">
<data key="d1">0.5</data>
</edge>
<node id="5"/>
<edge source="5" target="10">
<data key="d1">0.5</data>
</edge>
<edge source="5" target="11">
<data key="d1">0.5</data>
</edge>
<node id="6"/>
<edge source="6" target="2">
<data key="d1">0.5</data>
</edge>
<edge source="6" target="12">
<data key="d1">0.5</data>
</edge>
<node id="7"/>
<edge source="7" target="7">
<data key="d1">1.0</data>
</edge>
<node id="8"/>
<edge source="8" target="8">
<data key="d1">1.0</data>
</edge>
<node id="9"/>
<edge source="9" target="9">
<data key="d1">1.0</data>
</edge>
<node id="10"/>
<edge source="10" target="10">
<data key="d1">1.0</data>
</edge>
<node id="11"/>
<edge source="11" target="11">
<data key="d1">1.0</data>
</edge>
<node id="12"/>
<edge source="12" target="12">
<data key="d1">1.0</data>
</edge>
</graph>
</graphml>

I am just modifying the ClickHandle.java file from the JGraphX examples folder in order to visualize the graph.


Solution

  • jgraphx uses "value" attribute for the label of an edge:

    <mxCell edge="1" id="4" parent="1" style="defaultEdge" source="2" target="3" value="test label">
      <mxGeometry>
        ...
      </mxGeometry>
    </mxCell>
    

    You could try adding an attibute named "value".