Sie sind auf Seite 1von 11

DrawingGraphsusingDotandGraphviz

TableofContents
1.License
2.Introduction
2.1.WhatisDOT?
2.2.WhatisGraphviz?
2.3.Whoisthisdocumentfor?
2.4.RelatedMaterials
3.Setup
4.BasicExamples
4.1.SimpleGraph
4.2.SameGraph,DifferentLayout
4.3.SimpleDigraph(DirectionalGraph)
4.4.SimpleDigraphwithLabels
4.5.SameGraph,DifferentShapeandColour
4.6.Summary
5.MoreAdvanced
5.1.SavingTime
5.2.Records
6.SomeExampleGraphs
6.1.FiniteStateMachine
6.2.DataFlowDiagrams
6.3.DataFlowDiagrams2
6.4.ObjectInheritance
6.5.EntityRelationship
7.Reference
7.1.GraphAttributes
7.2.VertexAttributes
7.3.EdgeAttributes
7.4.Size,BackgroundColour
8.Appendices
8.1.FurtherReading
8.2.UsingEmacsOrgMode
8.2.1.Setup
8.2.2.EmbeddingDotinEmacs
8.2.3.TheCommandLine
tup]]:HowIuseEmacsOrgModetowritenovelsandshortstories

1License
Copyright(C)2013,2014TonyBallantyne.Permissionisgrantedtocopy,distributeand/ormodifythis
documentunderthetermsoftheGNUFreeDocumentationLicense,Version1.3oranylaterversion
publishedbytheFreeSoftwareFoundationwithnoInvariantSections,noFrontCoverTexts,andnoBack
CoverTexts.
Codeinthisdocumentisfreesoftware:youcanredistributeitand/ormodifyitunderthetermsoftheGNU
GeneralPublicLicenseaspublishedbytheFreeSoftwareFoundation,eitherversion3oftheLicense,or(at
youroption)anylaterversion.
Thiscodeisdistributedinthehopethatitwillbeuseful,butWITHOUTANYWARRANTYwithouteven
theimpliedwarrantyofMERCHANTABILITYorFITNESSFORAPARTICULARPURPOSE.Seethe
GNUGeneralPublicLicenseformoredetails.

2Introduction
2.1WhatisDOT?

DOTisaplaintextgraphdescriptionlanguage.Itisasimplewayofdescribinggraphsthatbothhumansandcomputer
programscanread.

2.2WhatisGraphviz?
Graphvizisopensourcegraphvisualizationsoftware.Graphvisualizationisawayofrepresentingstructuralinformation
asdiagramsofabstractgraphsandnetworks.

2.3Whoisthisdocumentfor?
Thisdocumentwasoriginallywrittenasquickreferenceformyself.Itwasthenextendedtobecomeatutorialfor
Computingstudents.It'snowofferedtoanyonewhowantstolearnDOTbyexample.

2.4RelatedMaterials
YoucanfindlinkstosimilardocumentspostsatmyblogTonyBallantyne.com/tech
IfyouhavestumbledacrossthisdocumentbyaccidentwhilstlookingformyworkasanSFandFantasywriter,the
followinglinksmaybemoreusefultoyou
TonyBallantyne.com:Blogaboutmynovelsandshortstories
EmacsTutorial:AbriefintroductiontoEmacsforwriters
MyEmacsWritingSetup:HowIuseEmacsOrgModetowritenovelsandshortstories

3Setup
YouwillneedtohavetheGraphvizsuiteofprogramsinstalledonyourcomputertofollowthistutorial.Graphizcanbe
downloadedforfreefromtheGraphvizsite:http://www.graphviz.org/Home.php

4BasicExamples
4.1SimpleGraph
graphgraphname{
ab;
bc;
bd;
da;
}

4.2SameGraph,DifferentLayout

graphgraphname{
rankdir=LR;//RankDirectionLefttoRight
ab;
bc;
bd;
da;
}

4.3SimpleDigraph(DirectionalGraph)
digraphgraphname{
a>b;
b>c;
a>c;
}

4.4SimpleDigraphwithLabels
digraphgraphname{
T[label="Teacher"]//nodeT
P[label="Pupil"]//nodeP
T>P[label="Instructions",fontcolor=darkgreen]//edgeT>P
}

4.5SameGraph,DifferentShapeandColour
digraphgraphname{
T[label="Teacher"color=Blue,fontcolor=Red,fontsize=24,shape=box]//nodeT
P[label="Pupil"color=Blue,fontcolor=Red,fontsize=24,shape=box]//nodeP
T>P[label="Instructions",fontcolor=darkgreen]//edgeT>P
}

Herearesomeoftheshapesyoucanusebox,polygon,ellipse,oval,circle,point,egg,triangle,plaintext,diamond,
trapezium,parallelogram,house,pentagon,hexagon,septagon,octagon,doublecircle,doubleoctagon,tripleoctagon
Therearelotsmoreavailableherehttp://www.graphviz.org/content/nodeshapes

4.6Summary

digraphsummary{
start[label="StartwithaNode"]
next[label="Chooseyourshape",shape=box]
warning[label="Don'tgooverboard",color=Blue,fontcolor=Red,fontsize=24,style=filled,fillcol
end[label="Drawyourgraph!",shape=box,style=filled,fillcolor=yellow]
start>next
start>warning
next>end[label="GettingBetter...",fontcolor=darkblue]
}

5MoreAdvanced
5.1SavingTime
Ittakestimedefiningeachnodeindividually.Thefollowingwayisquicker
digraphhierarchy{
nodesep=1.0//increasestheseparationbetweennodes

node[color=Red,fontname=Courier,shape=box]//Allnodeswillthisshapeandcolour
edge[color=Blue,style=dashed]//Allthelineslooklikethis
Headteacher>{Deputy1Deputy2BusinessManager}
Deputy1>{Teacher1Teacher2}
BusinessManager>ITManager
{rank=same;ITManagerTeacher1Teacher2}//Putthemonthesamelevel
}

5.2Records
YoucannowuseHTMLtodefinethesesortofblocks.Findoutmoreathttp://www.graphviz.org/doc/info/shapes.html

digraphstructs{
node[shape=record]
struct1[label="<f0>left|<f1>mid\dle|<f2>right"];
struct2[label="{<f0>one|<f1>two\n\n\n}"shape=Mrecord];
struct3[label="hello\nworld|{b|{c|<here>d|e}|f}|g|h"];
struct1:f1>struct2:f0;
struct1:f0>struct3:f1;
}

6SomeExampleGraphs
6.1FiniteStateMachine
digraphfinite_state_machine{
rankdir=LR;
size="8,5"
node[shape=circle];
S0>S1[label="LiftNozzle"]
S1>S0[label="ReplaceNozzle"]
S1>S2[label="AuthorizePump"]
S2>S0[label="ReplaceNozzle"]
S2>S3[label="PullTrigger"]
S3>S2[label="ReleaseTrigger"]
}

6.2DataFlowDiagrams
digraphdfd{
node[shape=record]
store1[label="<f0>left|<f1>Somedatastore"];
proc1[label="{<f0>1.0|<f1>Someprocesshere\n\n\n}"shape=Mrecord];
enti1[label="Customer"shape=box];
store1:f1>proc1:f0;
enti1>proc1:f0;
}

6.3DataFlowDiagrams2
Thefollowingusessubgraphstodisplaydifferentlevels.Notethatsubgraphsmuststartwiththeprefixcluster_orthey
won'twork.Itwillonlyworkwithdotlayout.
digraphdfd2{
node[shape=record]
subgraphlevel0{
enti1[label="Customer"shape=box];
enti2[label="Manager"shape=box];
}
subgraphcluster_level1{
label="Level1";
proc1[label="{<f0>1.0|<f1>Oneprocesshere\n\n\n}"shape=Mrecord];
proc2[label="{<f0>2.0|<f1>Otherprocesshere\n\n\n}"shape=Mrecord];
store1[label="<f0>|<f1>Datastoreone"];
store2[label="<f0>|<f1>Datastoretwo"];
{rank=same;store1,store2}
}
enti1>proc1
enti2>proc2
store1>proc1
store2>proc2
proc1>store2
store2>proc1
}

6.4ObjectInheritance
digraphobj{
node[shape=record];
rankdir="BT";

teacher[label="{<f0>Teacher|<f1>\n|<f2>\n}"];
course[label="{<f0>Course|<f1>\n|<f2>\n}"];
student[label="{<f0>Student|<f1>\n|<f2>\n}"];
lesson[label="{<f0>Lesson|<f1>\n|<f2>\n}"];
tutorial[label="{<f0>Tutorial|<f1>\n|<f2>\n}"];
assessment[label="{<f0>Assessment|<f1>\n|<f2>\n}"];
coursework[label="{<f0>Coursework|<f1>\n|<f2>\n}"];
exam[label="{<f0>Exam|<f1>\n|<f2>\n}"];
{rank=same;teachercoursestudent}
teacher>course[dir="forward",arrowhead="none",arrowtail="normal",headlabel="1",taillabel="1.."];
student>course[dir="forward",arrowhead="none",arrowtail="normal",headlabel="1",taillabel="1.."];
lesson>course[dir="forward",arrowhead="diamond",arrowtail="normal"];
tutorial>course[dir="forward",arrowhead="diamond",arrowtail="normal"];
assessment>course[dir="forward",arrowhead="diamond",arrowtail="normal"];
coursework>assessment;
exam>assessment;
}

6.5EntityRelationship
digraphER{
node[shape=box];
Book;
Customer;
Loan;
{rank=same;Book,Customer,Loan}
Book>Loan[dir="forward",arrowhead="crow",arrowtail="normal"];
Customer>Loan[dir="forward",arrowhead="crow",arrowtail="normal"];
}

7Reference
Herearethemostusefulattributesyouwillneedwhendrawinggraphs.Thefulllistcanbefoundhere:
http://graphviz.org/doc/info/attrs.html

7.1GraphAttributes
label="MyGraph"Labelagraphitself
rankdir=LRLaythegraphoutfromLefttoRight,insteadofToptoBottom
{rank=samea,b,c}Groupnodestogetheratthesamelevelofagraph
splines="line"Forceedgestobestraight,nocurvesorangles
K=0.6Usedtoinfluencethe'spring'usedinthelayout,Canbeusedtopushnodesfurtherapart,whichis
especiallyusefulfortwopiandsfdplayouts

7.2VertexAttributes

[label="SomeLabel"]LabelstheVertex
[color="red"]ColorstheVertex
[fillcolor="blue"]FillstheVertexwiththespecifiedcolour

7.3EdgeAttributes
[label="SomeLabel"]LabelstheEdge(UsefulforWeights)
[color="red"]ColorstheVertex(UsefulforPaths)
[penwidth=2.0]Adjuststhethicknessoftheedgeline,VeryusefulforPaths

7.4Size,BackgroundColour
fixedsize=truesize="1,1"resolution=72bgcolor="#C6CFD532"

8Appendices
8.1FurtherReading
http://linuxdevcenter.com/pub/a/linux/2004/05/06/graphviz_dot.html
http://graphs.grevian.org/index.html

8.2UsingEmacsOrgMode
Emacsorgmodeisanidealenvironmentforwriting,executingandexportingDotgraphics
8.2.1Setup
Downloadandinstallgraphvizandaddthepathtotheexecpathvariable
Youwillneedtoupdateyour.emacsfiletoloaddotasababellanguage.Thefollowingisausefulbabelsetupfordot
andotherlanguages
(orgbabeldoloadlanguages
(quoteorgbabelloadlanguages)
(quote((emacslisp.t)
(java.t)
(dot.t)
(ditaa.t)
(R.t)
(python.t)
(ruby.t)
(gnuplot.t)
(clojure.t)
(sh.t)
(ledger.t)
(org.t)
(plantuml.t)
(latex.t))))

8.2.2EmbeddingDotinEmacs
OrgmodecaninterpretdifferentlanguagesbyusingtheLibraryOfBabel.Todoso,enclosethecodeinbegin_srcand
end_srctagsasbelow.You'llneedtoaddcommandlineargumentsasshown.
Ashortcuttomakeabegin_srcblockistotype<s[TAB]
#+begin_srcdot:file./img/example1.png:cmdlineKdotTpng
graphgraphname{
ab;
bc;
bd;
da;
}

#+end_src

8.2.3TheCommandLine
Thesection:cmdlineKdotTpnginthe#+begin_srcdot:file./img/example1.png:cmdlineKdotTpngsectionare
commandlinearguments.Theytelldothowtorenderanddisplay.
Kdotusedotlayout.TheotherlayoutsyoucantryareKneato,Kcirco,Ktwopi,KfdpandKsfdpfordifferent
layouts
Tpngrenderaspng
Thefullcommandlineargumentscanbefoundhere:http://graphviz.org/content/commandlineinvocation
Date:<20131021Mon>
Author:TonyBallantyne
Created:20140412Sat10:13
Emacs23.3.1(Orgmode8.0.2)
ValidateXHTML1.0

Das könnte Ihnen auch gefallen