TX_
TX_

Reputation: 5466

Questions about Graphviz API (Graphviz as a library)

I'm using Graphviz as a library (C++). I need Graphviz to calculate coordinates for my nodes and respective edges. I'm doing rendering myself (i.e. not using Graphviz's renderers). My nodes are rectangles, with defined width and height (in pixels).

I create graph with agopen. Then I create nodes with agnode and edges with agedge. Then gvLayout should be used, as far as I know, for calculating the coordinates.

Now I need to know:

  1. How to specify nodes' rectangles' width and height, before using gvLayout?

  2. After gvLayout, how to get calculated coordinates for nodes and splines for edges?

  3. How to delete node (and corresponding edges) or edges from graph (Agraph_t), can this be done at all?

  4. When I create nodes and edges with agnode and agedge, don't I need to free/delete them for cleanup (when I'm done with graph)? Or does agclose handle this implicitly?

Important: I don't want to use text-based interface (i.e. get parameters as text), I want to access mentioned parameters (width and height of node, splines of edge, coordinates of node...) directly as a data variables.

Upvotes: 3

Views: 1862

Answers (3)

Steffen
Steffen

Reputation: 163

Q1: you have to set the width and fix it:

agsafeset(node, "width", "1");
agsafeset(node, "fixedsize", "1", "");

If you change the width after fixing it, make sure to call "fixed size" with "0" before setting "width" with the new value.

Q2: instead of using ND_coord(n) the following will work too:

node->u.coord.x
node->graph->u.bb.UR.y - node->u.coord.y

To wrap edges to bezier lines, have a look at e.g. http://mupuf.org/blog/article/34/, I've done a converting to CGMutablePathRef based on it (if you are interested in).

For Q3 & Q4 look at Emden's answer.

Upvotes: 1

Emden R. Gansner
Emden R. Gansner

Reputation: 261

Adding a bit more detail to the previous response:

1) The width of a node is determined by multiple properties: the "width" attribute specifies the minimum width; the width of the label or image in the node; the "margin" attribute for the margin around the label; the number of peripheries; etc. If you really want to fix the width, set the "width" attribute to the desired value and set "fixedsize" to 1. As noted above, you set attributes with the agset function, which assumes the attribute was previously declared using the agnodeattr function, for nodes. Or just use agsafeset, which combines the operations.

2) If n is a node pointer, its position is stored as a (x,y) pair in ND_coord(n). If e is an edge pointer, the value ED_spl(e) points to a structure containing an array of structures describing cubic B-splines. (Usually, there is only one.) If bz = ED_spl(e)->list[0], then *bz has fields for the number of points, the points, and information as to whether the edge has arrows at the head and/or tail. This meaning of these values is described here

3) To delete node n from graph g, use agdelete(g, n);

4) Calling agclose on the graph will automatically free all of the nodes and edges. If you have called gvLayout, you first need to call gvFreeLayout to free the data allocated for doing the layout.

You may find this document helpful.

Upvotes: 4

synthesizerpatel
synthesizerpatel

Reputation: 28036

Q1

agset (void* pointer_to_component, char* name_of_attribute, char* value_of_attribute)
gsafeset which has the same signature.

Q2

for (n = agfstnode(g); n; n = agnxtnode(n)) { /* do something with n */ }

Q3

Agnode_t *n;
n = agnode(g, "some_specific_node_name_to_find", FALSE);
agdelnode(n);

For more info check out the Agraph API tutorial

Upvotes: 1

Related Questions