One of the most straightforward methods for building a visualizations datastructure is through utilizing the GraphML reader included with the prefuse package. More detailed information on the GraphML format is available
Here, a quick overview is provided below.
GraphML Format
The GraphML File format is a relatively straightforward format for representing a group of objects with attributes and directed or undirected edges. All GraphML files begin with this header :
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" >
The next step is to define the structure of the graph itself. The attributes of the graph nodes are defined with these tags :
<key id="key1" for="node" attr.name="name" attr.type="string"/>
<key id="key2" for="node" attr.name="age" attr.type="int"/>
These tags define two attributes for each node. the first is a Name attribute, which is attached to a node and is a string. The second is an age attribute, also attached to a node and is an integer. The accepted attribute types are :
- boolean
- int
- long
- float
- double
- string
After this, a graph is opened with various options included in the tag. The opening tag shown below sets the edges to be undirected by default :
<graph edgedefault="undirected">
The next step is to define the individual nodes as follows :
<node id="Node1">
<data key="key1">Jake</data>
<data key="key2">32</data>
</node>
<node id="Node2">
<data key="key1">Laura</data>
<data key="key2">24</data>
</node>
<node id="Node3">
<data key="key1">Marcus</data>
<data key="key2">12</data>
</node>
Each node has a specific ID which is used to differentiate it from the others, and the attributes are defined through tags which use the key id to refer to the specific attributes. The last step is to define any edges between the nodes :
<edge source="Node1" target="Node2" />
<edge source="Node2" target="Node3" />
<edge source="Node3" target="Node1" />
Since we've defined the edges as undirected by default, the source and target attributes are interchangable. However, if the edges are directed then this is where the of the edge is defined. All that's left is to close the graph :
</graph>
</graphml>
Note that in order for prefuse to successfully read the GraphML file, it must be saved with a .xml extension.
Loading the GraphML File
Loading a GraphML File is incredibly straightforward. You create a GraphML Reader (prefuse.data.io.GraphMLReader) and use it to load the GraphML File into the graph. This can throw a DataIOException, so it needs to be wrapped in a try/catch loop :
String datafile = "myGraph.xml";
Graph g = null;
try {
g = new GraphMLReader().readGraph(datafile);
}catch (DataIOException e){
System.err.println("Couldn't load graph");
e.printStackTrace();
System.exit(1);
}