def gv_layout(nodes,edges,mode="dot"): G = gv.graph("root") s = gv.graph(G,"test") for i in nodes: sg = "%02x %s"%(i,nodes[i][0]) n = gv.node(s,sg) if nodes[i][0] in gv_colors: gv.setv(n,"color",gv_colors[nodes[i][0]]) gv.setv(n,"style","filled") for i in edges: if i[0] in nodes and i[1] in nodes: e = gv.edge(G,"%02x %s"%(i[0],nodes[i[0]][0]),"%02x %s"%(i[1],nodes[i[1]][0])) gv.setv(e,"dir","none") gv.layout(G, mode) gv.render(G) # for debugging purposes gv.render(G,'svg','test.svg') devs = {} fn = gv.firstnode(G) try: devs[gv.nameof(fn)] = gv.getv(fn,"pos").split(",") except: print 'Failed in gv_render' for i in range(len(nodes)-1): fn = gv.nextnode(G,fn) devs[gv.nameof(fn)] = gv.getv(fn,"pos").split(",") return devs
def gv_layout(nodes, edges, mode="dot"): G = gv.graph("root") s = gv.graph(G, "test") for i in nodes: sg = "%02x %s" % (i, nodes[i][0]) n = gv.node(s, sg) if nodes[i][0] in gv_colors: gv.setv(n, "color", gv_colors[nodes[i][0]]) gv.setv(n, "style", "filled") for i in edges: if i[0] in nodes and i[1] in nodes: e = gv.edge(G, "%02x %s" % (i[0], nodes[i[0]][0]), "%02x %s" % (i[1], nodes[i[1]][0])) gv.setv(e, "dir", "none") gv.layout(G, mode) gv.render(G) # for debugging purposes gv.render(G, 'svg', 'test.svg') devs = {} fn = gv.firstnode(G) try: devs[gv.nameof(fn)] = gv.getv(fn, "pos").split(",") except: print 'Failed in gv_render' for i in range(len(nodes) - 1): fn = gv.nextnode(G, fn) devs[gv.nameof(fn)] = gv.getv(fn, "pos").split(",") return devs
def __iter__(self): cur = gv.firstedge(self.parent.handle) while gv.ok(cur): yield (decode_page(gv.nameof(gv.tailof(cur))), decode_page(gv.nameof(gv.headof(cur)))), \ dict(self.graph._iterattrs(cur)) cur = gv.nextedge(self.parent.handle, cur)
def __iter__(self): """ Iterates over item attributes. """ attr = gv.firstattr(self.handle) while gv.ok(attr): yield gv.nameof(attr), \ decode_page(gv.getv(self.handle, attr)) attr = gv.nextattr(self.handle, attr)
def GetAttrs(obj): attrs={} gsym=gv.firstattr(obj) while gsym: name=gv.nameof(gsym) attrs[name]=gv.getv(obj,gsym) gsym=gv.nextattr(obj,gsym) return attrs
def __iter__(self): """ Iterate over all items, yielding Graphviz.<type> items """ handle = self.parent.handle cur = getattr(gv, "first%s" % self.type)(handle) nextitem = getattr(gv, "next%s" % self.type) while gv.ok(cur): yield self.get(gv.nameof(cur)) cur = nextitem(handle, cur)
def main(): # create a new empty graph G = gv.digraph('G') # define a simple graph ( A->B ) gv.edge(gv.node(G, 'A'), gv.node(G, 'B')) # compute a directed graph layout gv.layout(G, 'dot') # annotate the graph with the layout information gv.render(G) # do something with the layout n = gv.firstnode(G) while n: print 'node ' + gv.nameof(n) + ' is at ' + gv.getv(n, 'pos') e = gv.firstout(n) while e: print 'edge ' + gv.nameof(gv.tailof(e)) + '->' + gv.nameof( gv.headof(e)) + ' is at ' + gv.getv(e, 'pos') e = gv.nextout(n, e) n = gv.nextnode(G, n)
def initialise_nodes(self): # Bake in the node attributes from 'dot' layout gv.layout(self.gvo, 'dot') gv.render(self.gvo) # iterate over node attributes to get/set node positions # see gv.3python.pdf for more info # as well as https://mailman.research.att.com/pipermail/graphviz-interest/2006q1/003182.html n = gv.firstnode(self.gvo) #store min and max x and y minx = 0 miny = 0 maxx = None maxy = None # store the node label and position as reported by Dot layout nodepos = {} # {<node object>:(x,y)} while gv.ok(n) : # check that the iterator returned by firstnode is ok label = gv.nameof(n) spos = gv.getv(n,'pos').split(',') # list of strings (xpos,ypos) = [float(i) for i in spos] # convert to float node = self.dag.get_node_from_label(label) pos = node.get_position() if pos != None: # Set xpos and ypos if they are already defined in node.get_position() (xpos,ypos) = pos print xpos, ypos # set min and max values if minx > xpos: minx = xpos if maxx < xpos: maxx = xpos if miny > ypos: miny = ypos if maxy < ypos: maxy = ypos nodepos[node] = (xpos, ypos) #change node before iteration n = gv.nextnode(self.gvo, n) print "min", minx, miny print "max", maxx, maxy # Set the position in all nodes for node, pos in nodepos.iteritems(): node.set_position(pos)
def _iterattrs(self, handle=""): """ Iterate over the attributes of a graph item. If no handle attribute is given, iterates over the attributes of the root graph """ if not handle: handle = self.handle attr = gv.firstattr(handle) while gv.ok(attr): yield gv.nameof(attr), decode_page(gv.getv(handle, attr)) attr = gv.nextattr(handle, attr)
def _getNodesFromDAG(self): # Get the dotfile from the DAG dot = self.dag.get_dot() gvo = gv.readstring(dot) # Bake in the node attributes from 'dot' layout gv.layout(gvo, 'dot') gv.render(gvo) # iterate over node attributes to get/set node positions # see gv.3python.pdf for more info # as well as https://mailman.research.att.com/pipermail/graphviz-interest/2006q1/003182.html n = gv.firstnode(gvo) #store min and max x and y minx = 0 miny = 0 maxx = None maxy = None # store the node label and position as reported by Dot layout nodepos = {} # {<node object>:(x,y)} while gv.ok(n) : # check that the iterator returned by firstnode is ok label = gv.nameof(n) spos = gv.getv(n,'pos').split(',') # list of strings (xpos,ypos) = [float(i) for i in spos] # convert to float node = self.dag.get_node_from_label(label) pos = node.get_position() if pos != None: # Set xpos and ypos if they are already defined in node.get_position() (xpos,ypos) = pos # set min and max values if minx > xpos: minx = xpos if maxx < xpos: maxx = xpos if miny > ypos: miny = ypos if maxy < ypos: maxy = ypos nodepos[node] = (xpos, ypos) #change node before iteration n = gv.nextnode(gvo, n) # Set the position in all nodes and add them to the graph for node, pos in nodepos.iteritems(): node.set_position(pos) label = self.dag.get_label_from_node(node) v_node = v_Node(label) v_node.setPos(*pos) self.graphview.add(v_node) bounding = self.graphview.scene().itemsBoundingRect() #self.graphview.fitInView(bounding, QtCore.Qt.IgnoreAspectRatio) self.graphview.centerOn(bounding.center())
def name(self): return gv.nameof(self.handle)
#!/usr/bin/python import sys import gv # create a new empty graph G = gv.digraph('G') # define a simple graph ( A->B ) gv.edge(gv.node(G, 'A'), gv.node(G, 'B')) # compute a directed graph layout gv.layout(G, 'dot') # annotate the graph with the layout information gv.render(G) # do something with the layout n = gv.firstnode(G) while n: print 'node ' + gv.nameof(n) + ' is at ' + gv.getv(n, 'pos') e = gv.firstout(n) while e: print 'edge ' + gv.nameof(gv.tailof(e)) + '->' + gv.nameof( gv.headof(e)) + ' is at ' + gv.getv(e, 'pos') e = gv.nextout(n, e) n = gv.nextnode(G, n)
def __str__(self): return "(u'" + decode_page(gv.nameof(gv.tailof(self.handle))) + \ "', u'" + decode_page(gv.nameof(gv.headof(self.handle))) + "')"
def __str__(self): """ Returns item name. """ return "u'" + decode_page(gv.nameof(self.handle)) + "'"
#!/usr/bin/python import sys import gv # create a new empty graph G = gv.digraph('G') # define a simple graph ( A->B ) gv.edge(gv.node(G, 'A'),gv.node(G, 'B')) # compute a directed graph layout gv.layout(G, 'dot') # annotate the graph with the layout information gv.render(G) # do something with the layout n = gv.firstnode(G) while n : print 'node '+gv.nameof(n)+' is at '+gv.getv(n,'pos') e = gv.firstout(n) while e : print 'edge '+gv.nameof(gv.tailof(e))+'->'+gv.nameof(gv.headof(e))+' is at '+gv.getv(e,'pos') e = gv.nextout(n,e) n = gv.nextnode(G,n)