Swift Package for GraphViz and DOT

GraphViz is one of my favorite technologies, and I’ve been very happy to incorporate it into my work on SwiftDoc, a new documentation generator for the Swift programming language.

In the course of my work, I made an open source package for defining graphs, encoding them to DOT, and rendering with GraphViz. You can find it here on GitHub:

Here’s an example of how it’s used:

import GraphViz

var graph = Graph(directed: true)

let a = Node("a"), b = Node("b"), c = Node("c")

graph.append(Edge(from: a, to: b))
graph.append(Edge(from: a, to: c))

var b_c = Edge(from: b, to: c)
b_c.constraint = false
graph.append(b_c)

// Produce DOT language representation
let dot = DOTEncoder().encode(graph)

// Render image using dot layout algorithm
let data = try! graph.render(using: .dot, to: .svg)
let svg = String(data: data, encoding: .utf8)!

I tried my best to support all of the available node, edge, and graph attributes in a coherent way, but please let me know if I missed a few or if anything looks out of place.

Anyway, If you’re developing a macOS or iPhone app, or perhaps using Swift server-side, I’d encourage you to take a look and let me know what you think. Thanks!

1 Like

Would you like us to add this to the Resources page?

1 Like

Sure, that’d be great! Feel free to use the following entry:

  • swift: GraphViz a Swift package for working with GraphViz [Mattt]

I’ve added an item for this today. It should appear on the web site soon.

1 Like