Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: Graphviz wrapper  (Read 1141 times)

iLya2IK

• New Member
• Posts: 28
Graphviz wrapper
« on: January 29, 2022, 10:15:03 am »
Hello! Sorry if this question has already been asked, but I didn't find anything suitable on the forum. Tell me please if there is a pascal header for Graphvis library.

iLya2IK

• New Member
• Posts: 28
Re: Graphviz wrapper
« Reply #1 on: February 14, 2022, 03:25:52 pm »
Out of desperation, I had to write my own wrapper  . Who is interested, I invite you to the project page.

Here is a code example of how to use the wrapper
Code: Pascal  [Select][+][-]
1. (* Create and draw finite state machine example     *)
2. (* <https://graphviz.org/Gallery/directed/fsm.html> *)
3.
4. program simpletest;
5.
6. uses Classes, OGLGraphvizWrapper;
7.
8. var
9.   // wrapper
10.   Cntx : TGVContext;
11.   Gv  : IGVCntxGraph;
12.   Nv : Array [0..8] of IGVNode;
13.   Ev : IGVEdge;
14.   Av : TGVAttrId;
15.   NI, EI : IGVIterator;
16.   FS : TFileStream;
17. begin
18.   if TGVContext.GVLibsLoad then // initalize gvc and cgraph libs
19.   begin
20.     Cntx := TGVContext.Create;
21.     try
22.       Gv := Cntx.NewGraph('finite_state_machine', [gvoDirected]);
23.       try
24.         Gv.GraphAttr('rankdir', 'LR');
25.         Gv.GraphAttr('size', '8, 5');
26.         Gv.NodeAttr('shape', 'doublecircle');
27.         Nv[0] := Gv.Node('0');
28.         Nv[3] := Gv.Node('3');
29.         Nv[4] := Gv.Node('4');
30.         Nv[8] := Gv.Node('8');
31.         Gv.NodeAttr('shape', 'circle');
32.         Nv[2] := Gv.Node('2');
33.         Nv[1] := Gv.Node('1');
34.         Nv[6] := Gv.Node('6');
35.         Nv[5] := Gv.Node('5');
36.         Nv[7] := Gv.Node('7');
37.
38.         Av := Gv.EdgeAttr('label', '');
39.         Ev := Gv.Edge(Gv.FindNode('0'), Gv.FindNode('2'));
40.         Ev.SetExAttr(Av, 'SS(B)');
41.         Gv.Edge(Nv[0], Nv[1]).SetExAttr(Av, 'SS(S)');
42.         Gv.Edge(Nv[1], Nv[3]).SetExAttr(Av, 'S(\$end)');
43.         Gv.Edge(Nv[2], Nv[6]).SetExAttr(Av, 'SS(b)');
44.         Gv.Edge(Nv[2], Nv[5]).SetExAttr(Av, 'SS(a)');
45.         Gv.Edge(Nv[2], Nv[4]).SetExAttr(Av, 'S(A)');
46.         Gv.Edge(Nv[5], Nv[7]).SetExAttr(Av, 'S(b)');
47.         Gv.Edge(Nv[5], Nv[5]).SetExAttr(Av, 'S(a)');
48.         Gv.Edge(Nv[6], Nv[6]).SetExAttr(Av, 'S(b)');
49.         Gv.Edge(Nv[6], Nv[5]).SetExAttr(Av, 'S(a)');
50.         Gv.Edge(Nv[7], Nv[8]).SetExAttr(Av, 'S(b)');
51.         Gv.Edge(Nv[7], Nv[5]).SetExAttr(Av, 'S(a)');
52.         Gv.Edge(Nv[8], Nv[6]).SetExAttr(Av, 'S(b)');
53.         Gv.Edge(Nv[8], Nv[5]).SetExAttr(Av, 'S(a)');
54.
55.         // place the graph in a contextual layout to initialize
56.         // drawing interfaces using a specific engine (dot)
57.         Gv.Layout(Cntx, 'dot');
58.
59.         // iterating through all nodes and edges of the graph
60.         NI := Gv.GetNodeIterator;
61.         while NI.HasNext do
62.         begin
63.           Nv[0] := NI.Next as IGVNode;
64.           WriteLn('Node ', Nv[0].Name);
65.           EI := Gv.GetEdgeIterator(Nv[0]);
66.           while EI.HasNext do
67.           begin
68.             Ev := EI.Next as IGVEdge;
69.             WriteLn('  Edge ', Ev.GetExAttr(Av).AsString);
70.           end;
71.         end;
72.
73.         // draw the graph in an SVG file
74.         Gv.RenderFilename('svg', 'out3.svg');
75.
76.         // example of drawing the graph into a stream
77.         FS := TFileStream.Create('out4.bmp', fmCreate or fmOpenWrite);
78.         try
79.           Gv.RenderStream('bmp', FS);
80.         finally
81.           FS.Free;
82.         end;
83.
84.       finally
85.         Gv.Close;
86.       end;
87.     finally
88.       Cntx.Free;
89.     end;
90.
92.   end else begin
93.     WriteLn('Cant initialize Graphviz');
94.   end;
96. end.
« Last Edit: February 18, 2022, 09:55:23 am by iLya2IK »

avra

• Hero Member
• Posts: 2330
Re: Graphviz wrapper
« Reply #2 on: February 15, 2022, 02:13:32 am »
Thanks! I will have a need for this in the future...
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib