Pages

Wednesday, August 3, 2011

Create a graph visualization using ANTLR

You can use Graphviz software to generate a graphical version of a tree or graph.
Download link: http://www.graphviz.org/Download..php


Create the parse tree
  1. First, create ANTLR StringTemplates for tree and edges. (design specification for the image)
  2. Get the file content and create an AntlrFileStream to be given as an input to the lexer.
  3. Then create a token stream using the lexer and invoke the rules to parse the tree.
Generate the dot file

Create an instance of ANTLR DotTreeGenerator.
Create the output file.
 StringTemplate st = dotTreeGenerator.ToDOT(ParsedTree, new CommonTreeAdaptor(), _treeStringTemplate, _edgeStringTemplate);
You can create .dot file using this string template. (Ex: test.dot)


Generate image of the graph

Give the .dot file as input and specify the output format. (Ex: .png)
Execute the following command:
dot.exe -Tpng test.dot -O test.png

An image of the input graph will be created with the given design specifications.

Sample image generated for SQL query is given below:

CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255)
)

1 comment:

  1. Atree map is a data visualization that uses the area of the branches of a tree to represent data. One very common use for a tree map is to show relative proportions of different groups. These proportions are usually represented by the colors of the individual rectangles: the more rectangles of one color in the tree map, the higher the proportion of that group.

    ReplyDelete