Skip to content

Visualization Using Graphviz

Alexandre Rabérin edited this page May 31, 2020 · 1 revision

Visualization Using Graphviz

QuikGraph supports generating dot files or string, the file format of Graphviz, based on a given graph. All Graphviz related features are located in the QuikGraph.Graphviz module.

This format allows to call external dot executable or any other executable supporting dot format to render the graph. QuikGraph.Graphviz is the bridge to this format to make it easy to make a render of a graph.

Basic graph rendering (dot)

Graphviz algorithm

The GraphvizAlgorithm<TVertex, TEdge> class contains the logic to render an IEdgeListGraph<TVertex, TEdge> to dot format using the Generate method. During the generation process you can control some properties of graph elements to customize the way they will be rendered. To do so, there are:

  • GraphFormat, CommonVertexFormat and CommonEdgeFormat control the default rendering of the generated graph.
  • FormatCluster, FormatVertex and FormatEdge events let you customize the display of individual cluster, vertices and edges.
IEdgeListGraph<TVertex, TEdge> graph = ...;
var graphviz = new GraphvizAlgorithm<TVertex, TEdge>(graph);

// Render
string dotGraph = graphviz.Generate();
// Or
string outputFilePath = graphviz.Generate(new FileDotEngine(), "dotOutputGraph");

The generated dot can then be used to call dot executable or any other tool supporting dot format. A nice online tool to test the rendering of dot graph is available there.

Graphviz algorithm by extension

To make it even more easy to convert a QuikGraph graph to dot, the QuikGraph.Graphviz module provides useful extensions to make the conversion like the followings:

IEdgeListGraph<TVertex, TEdge> graph = ...;
string dotGraph = graph.ToGraphviz();

// Or using a custom init for the algorithm
string dotGraph = graph.ToGraphviz(algorithm =>
{
    // Custom init example
    algorithm.CommonVertexFormat.Shape = GraphvizVertexShape.Diamond;
    algorithm.CommonEdgeFormat.ToolTip = "Edge tooltip";
    algorithm.FormatVertex += (sender, args) =>
    {
        args.VertexFormat.Label = $"Vertex {args.Vertex}";
    };
});

Custom graph rendering

QuikGraph sends the generated dot code to a 'dot engine' interface which is supposed to do the rest of the work. This is where you can plugin and add any additional steps.

QuikGraph provides a simple rendering engine that writes the dot file to disk but you are free to do other things by implementing the IDotEngine in your own way.

Basic graph rendering (svg)

QuikGraph.Graphviz module also provides a feature to export a graph as an SVG image. Like the ToGraphviz extension there is one called ToSvg that performs the conversion.

IEdgeListGraph<TVertex, TEdge> graph = ...;
string graphAsSvg = graph.ToSvg();

// Or using a custom init for the algorithm
string graphAsSvg = graph.ToSvg(algorithm =>
{
    // Custom init example
    algorithm.CommonVertexFormat.Shape = GraphvizVertexShape.Diamond;
    algorithm.CommonEdgeFormat.ToolTip = "Edge tooltip";
    algorithm.FormatVertex += (sender, args) =>
    {
        args.VertexFormat.Label = $"Vertex {args.Vertex}";
    };
});

Note: Since the conversion is using a REST web service, it requires an access to internet to properly work.

Clone this wiki locally