Open Graph Drawing Framework
current version:
v.2015.05 (Baobab)
     

Manual creation and layout

Topic

Create a graph and its layout manually.

Source-Code

#include <ogdf/basic/Graph.h>
#include <ogdf/basic/GraphAttributes.h>
#include <ogdf/fileformats/GraphIO.h>
 
using namespace ogdf;
 
int main()
{
	Graph G;
	GraphAttributes GA(G, GraphAttributes::nodeGraphics | GraphAttributes::edgeGraphics );
 
	const int LEN = 11;
	for(int i = 1; i<LEN; ++i) {
		node left = G.newNode();
		GA.x(left) = -5*(i+1);
		GA.y(left) = -20*i;
		GA.width(left) = 10*(i+1);
		GA.height(left) = 15;
 
		node bottom = G.newNode();
		GA.x(bottom) = 20*(LEN-i);
		GA.y(bottom) = 5*(LEN+1-i);
		GA.width(bottom) = 15;
		GA.height(bottom) = 10*(LEN+1-i);
 
		edge e = G.newEdge(left,bottom);
		DPolyline &p = GA.bends(e);
		p.pushBack(DPoint(10,-20*i));
		p.pushBack(DPoint(20*(LEN-i),-10));
	}
 
	GraphIO::writeGML(GA, "manual_graph.gml");
 
	return 0;
}

Explanation

We first declare a Graph G. The class Graph only stored structural data of a graphs, i.e., edges and nodes, but not attrobutes like x/y positions, etc. Such information can be maintained in GraphAttributes, of which we generate an instance GA, associated to Graph G. The constructor of GraphAttributes allows to select which attributes shall be stored. We will use node position and sizes (nodeGraphics) and bend points for edges (edgeGraphics).

Within the loop, we generate 2 nodes per iteration, placing the first on the left-hand side of the drawing (from bottom to top), and the second node on the bottom of the drawing (from right to left). Note that in OGDF the coordinate systems' origin is in the top-left corner, hence the coordinated increase towards the right and towards the bottom. Furthermore, we give the nodes differing sizes, and note that the x/y coordinates of a node are specifying that node's center.

We also see that, as a general rule in GraphAttributes, we always obtain references to the different entries, hence we can get and set an attribute using virtually the same function (only differentiated by the use of const, which is transparent to the user).

Finally, we add an edge in each iteration of the loop, and set bend points for that edge.

In the end, GraphIO::writeGML(GA, “manual_graph.gml”) writes all information saved in G and GA to file “manual_graph.gml”. Try GraphIO::drawSVG(GA, “manual_graph.svg”) instead to output an SVG file.

Output

Graph layout:
manual_graph.gml

Drawing:
manual_graph.svg

 
tech/howto/manual.txt · Last modified: 2015/05/31 16:48 by stephan
This page is driven by DokuWiki