Graphviz Introduction
Graphviz was designed by several coorators at Bell Labs.Open Source
Charts (charts in the data structure in computer science) Visualization project, mainly implemented in C language, mainly to achieve some Graph Layout algorithms. Using these algorithms, You can evenly divide the nodes in the graph on the canvas.
To shorten the edge length between nodes and minimize the intersection of edges.
Graphviz provides an imperative plotting method, which providesDot Language
It is used to write a drawing script, parse the script, analyze the fixed points, edges, and subgraphs, and then draw based on the attributes. For details, refer to an example from the official document.
Digraph G {<br/> main-> parse-> execute; <br/> main-> Init; <br/> main-> cleanup; <br/> execute-> make_string; <br/> execute-> printf <br/> init-> make_string; <br/> main-> printf; <br/> execute-> compare; <br/>}
Digraph specifies that the graph is a directed graph (directed
Graph),-> indicates an edge. Main, parse, and execute are vertices. The running results are nice, such:
Note that I did not specify any information about the graph position in this dot script. The layout tool automatically la s based on the graph type and finally displays it.
Let's take a look at a complex and frequently used function, data structure diagram:
Digraph G {<br/> node [shape = record, Height =. 1]; <br/> node0 [label = "<F0> | <F1> G | <F2>"]; <br/> node1 [label = "<F0> | <F1> E | <F2>"]; <br/> node2 [label = "<F0> | <F1> B | <F2>"]; <br/> node3 [label = "<F0> | <F1> F | <F2>"]; <br/> node4 [label = "<F0> | <F1> r | <F2>"]; <br/> node5 [label = "<F0> | <F1> H | <F2>"]; <br/> node6 [label = "<F0> | <F1> Y | <F2>"]; <br/> node7 [label = "<F0> | <F1> A | <F2>"]; <br/> node8 [label = "<F0> | <F1> C | <F2>"]; <br/> "node0": F2-> "node4 ": f1; <br/> "node0": F0-> "node1": F1; <br/> "node1": F0-> "node2": F1; <br/> "node1": F2-> "node3": F1; <br/> "node2": F2-> "node8": F1; <br/> "node2": F0-> "node7": F1; <br/> "node4": F2-> "node6": F1; <br/> "node4": F0-> "node5": F1; <br/>}
Shows the effect after running:
I don't know what other programmers are like. I have a special liking for command line and prefer this type of tool. The first computer system to be exposed was a BSD without a graphic system.
Programs with interfaces are particularly interested.
Related ideas
Since graphviz was used, I have been thinking about porting this good thing to Java. The general idea is similar to graphviz:
- Parse the dot script to generate graph objects, including nodes, edges, subgraphs, and other objects. These objects are bound with corresponding attributes.
- Send parsed graph objects to Layout Engine for processing. Layout Engine can select layout policies, such as flow layout.
- Obtain the layout graph object from the Layout Engine and hand it to the image engine for processing. The final result is displayed or saved.
Dot
Syntax definition is relatively simple. I have constructed a dot analyzer using javacc. Now we can build a plot object from the dot file, but we need to further improve it.
BNF definition:
Graph-> [strict] (digraph | graph) id' {'stmt-list'} '<br/> stmt-list-> [stmt ['; '] [stmt-list] <br/> stmt-> ATTR-stmt | node-stmt | edge-stmt | subgraph | id' = 'id <br/> ATTR- stmt-> (graph | node | edge) ATTR-list <br/> ATTR-list-> '[' [A-list] '[ATTR-list] <br/> A-list-> id' = 'id [', '] [A-list] <br/> node-stmt-> node-ID [ATTR-list] <br/> node-ID-> ID [port] <br/> port-location [port-angle] | Port-angle [port-location] <br/> port-location-> ': 'id | ': ''('id', 'id ') '<br/> port-angle->' @ 'id <br/> edge-stmt-> (node-ID | subgraph) edgerhs [ATTR-list] <br/> edgerhs-> edgeop (node-ID | subgraph) [edgerhs] <br/> subgraph-> [subgraph id] '{'stmt-list'}' | subgraph ID
(Recently, I have always felt that time is not enough. There are a lot of interesting projects to do, such as the bbms (bus based message service) mentioned earlier ),
For example, modifying the client jtalk using swing and smack as a jabber is very interesting .)
Of course, graphviz does not provide this function. It provides a lib that can be used to embed the Drawing Engine into your own applications. This is a very meaningful thing. We don't have to grasp the layout part.
The complex algorithm, focus on the business logic part, the final graph object is handed over to this engine for processing. Of course, if you are just like me and want to know its amazing layout algorithm, you may as well flip through its source code.
Welcome to the discussion.
I analyzed the internal structure of graphviz over the weekend and established a Java project.Jraph
For simple transplantation, it is mainly used for learning. In fact, Java libraries are quite rich, such as jgraph.
, Jgrapht, prefuse, touchgraph, etc. It is certainly very convenient to use for projects, but I still prefer to watch an algorithm implemented by myself, especially this
Amazing algorithms, haha.
The expected API is as follows:
Public static void main (string [] ARGs) {<br/> parser = new gcodeparser ("file. G "); <br/> graphset GS = parser. parse (); <br/> graphset layouted = <br/> New graphlayoutengine (GS ). layout (); <br/> imageengine imgengine = <br/> New graphimageengine (layouted); <br/> imgengine. export (0); <br/>}
Like graphviz, the analyzer is called to construct the graphset of the plot (including multiple graphs, each of which includes vertex, edge, and subgraph ),
Then, the layout will be completed. Finally, the results will be drawn through the graphset after the layout. Currently, the framework design is complete, the analyzer is basically complete, and the layout part only implements one policy, that isForce-based
Layout algorithms. However, Layout Engine is designed as a pluggable model. It can be easily integrated with new algorithms.
Dot.exe-tpng-O new.png-kdot new. Dot
P.s. If time permits, I will host this project jraph (tentative name) on Google Code. At that time, you will be welcomed to attend this project.