Graphviz

Today I found something cool for drawing graphs. Its called GraphViz, You can download it from

http://www.graphviz.org/

It can output in various formats including SVG, PNG and GIF. But PNG and GIF dont have aliasing enabled, so they are pretty crappy.

For an example, look here. An image generated using PNG output

http://www.linkulu.com/random/?format=png&numnodes=10&size=5,5

Now look @ an another random graph with SVG output

http://www.linkulu.com/random/?format=svg&numnodes=10&size=5,5

Here is the python code to generate the graphs. I used Django ofcourse


def randomGraph(request):
 #Defaults for Generating   random graphs
 random.seed()
 numnodes=5
 format="svg"
 size="2,2"
 mime="image/svg xml"
 
 
 if "format" in request.GET:
 infor=request.GET["format"]
 if(infor=="png"):
 mime="image/png"
 format="png"
 
 elif(infor=="gif"):
 mime="image/gif"
 format="gif"
 
 if "size" in request.GET:
 size=request.GET["size"]
 
 
 if "numnodes" in request.GET:
 numnodes=int(request.GET["numnodes"])
 
 
 dot="""digraph G{
 size ="%s";
 orientation=portrait;
 {{{dot}}}
 }""" % (size)
 
 nodes=[]
 shapes=["ellipse","box","circle","record","triangle","doublecircle"]
 styles=["bold","dotted","normal"]
 colors=["cadetblue2","dimgray","dodgerblue1","beige","aliceblue","ghostwhite",
 "greenyellow","hotpink4","lightgoldenrod2"]
 
 st=""
 for i in range(numnodes):
 nodes.append("a" str(i));
 
 for node in nodes:
 s1=random.randint(0,len(shapes)-1)
 c1=random.randint(0,len(colors)-1)
 st=st " %s [shape=%s fillcolor=%s style=filled];\n" %(node,shapes[s1],colors[c1])
 
 for i in range(numnodes):
 r1=random.randint(0,numnodes-1)
 r2=random.randint(0,numnodes-1)
 s2=random.randint(0,len(styles)-1)
 st=st nodes[r1] "->" nodes[r2] " [style=%s];\n" %(styles[s2])
 
 dot=dot.replace("{{{dot}}}",st)
 
 tmpout, filename = mkstemp()
 
 pobj=Popen("dot -T%s -o %s" % (format,filename),shell=True,stdin=PIPE,stdout=PIPE)
 
 fin = pobj.stdin
 fout = pobj.stdout
 fin.write(dot)
 fin.flush()
 fin.close()
 pobj.wait()
 
 fout = os.fdopen(tmpout, "r")
 jpeg = fout.read()
 fout.close()
 
 os.remove(filename)
 
 #jpeg=dot
 
 return HttpResponse(content=jpeg, mimetype="%s" % (mime))


By: gavi on: