Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
graphstruct.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook
4/// Draw a simple graph structure.
5/// The graph layout is made using graphviz. This macro creates some
6/// nodes and edges and change a few graphical attributes on some of them.
7///
8/// \macro_image
9/// \macro_code
10/// \note For this to work, ROOT has to be compiled with gviz ON
11/// \author Olivier Couet
12
13TCanvas* graphstruct()
14{
15 #if __has_include("TGraphStruct.h") // handy check on whether gviz was installed
16 TGraphStruct *gs = new TGraphStruct();
17
18 // create some nodes and put them in the graph in one go ...
19 TGraphNode *n0 = gs->AddNode("n0","Node 0");
20 TGraphNode *n1 = gs->AddNode("n1","First node");
21 TGraphNode *n2 = gs->AddNode("n2","Second node");
22 TGraphNode *n3 = gs->AddNode("n3","Third node");
23 TGraphNode *n4 = gs->AddNode("n4","Fourth node");
24 TGraphNode *n5 = gs->AddNode("n5","5th node");
25 TGraphNode *n6 = gs->AddNode("n6","Node number six");
26 TGraphNode *n7 = gs->AddNode("n7","Node 7");
27 TGraphNode *n8 = gs->AddNode("n8","Node 8");
28 TGraphNode *n9 = gs->AddNode("n9","Node 9");
29
30 n4->SetTextSize(0.03);
31 n6->SetTextSize(0.03);
32 n2->SetTextSize(0.04);
33
34 n3->SetTextFont(132);
35
36 n0->SetTextColor(kRed);
37
38 n9->SetFillColor(kRed-10);
39 n0->SetFillColor(kYellow-9);
40 n7->SetFillColor(kViolet-9);
41
42 // some edges ...
43 gs->AddEdge(n0,n1)->SetLineColor(kRed);
44 TGraphEdge *e06 = gs->AddEdge(n0,n6);
45 e06->SetLineColor(kRed-3);
46 e06->SetLineWidth(4);
47 gs->AddEdge(n1,n7);
48 gs->AddEdge(n4,n6);
49 gs->AddEdge(n3,n9);
50 gs->AddEdge(n6,n8);
51 gs->AddEdge(n7,n2);
52 gs->AddEdge(n8,n3);
53 gs->AddEdge(n2,n3);
54 gs->AddEdge(n9,n0);
55 gs->AddEdge(n1,n4);
56 gs->AddEdge(n1,n6);
57 gs->AddEdge(n2,n5);
58 gs->AddEdge(n3,n6);
59 gs->AddEdge(n4,n5);
60
61 TCanvas *c = new TCanvas("c","c",800,600);
62 c->SetFillColor(38);
63 gs->Draw();
64 return c;
65 #else
66 return new TCanvas("c","c",800,600);
67 #endif
68}
#define c(i)
Definition RSha256.hxx:101
@ kRed
Definition Rtypes.h:66
@ kYellow
Definition Rtypes.h:66
@ kViolet
Definition Rtypes.h:67
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:37
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition TAttLine.h:43
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition TAttLine.h:40
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition TAttText.h:44
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition TAttText.h:46
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition TAttText.h:47
The Canvas class.
Definition TCanvas.h:23
An edge object connecting two nodes which can be added in a TGraphStruct.
Definition TGraphEdge.h:25
A graph node object which can be added in a TGraphStruct.
Definition TGraphNode.h:27
The Graph Structure is an interface to the graphviz package.
void AddNode(TGraphNode *node)
Add the node "node" in this TGraphStruct.
void Draw(Option_t *option="") override
Draw the graph.
void AddEdge(TGraphEdge *edge)
Add the edge "edge" in this TGraphStruct.