Skip to content

A Swift package for working with GraphViz

License

Notifications You must be signed in to change notification settings

elliottwilliams/GraphViz

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GraphViz

CI Documentation

A Swift package for working with GraphViz.

Requirements

  • Swift 5.2+
  • GraphViz (only for rendering)

Usage

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)

// Render image to SVG using dot layout algorithm
graph.render(using: .dot, to: .svg) { result in 
  guard .success(let data) = result,
        let svg = String(data: data, encoding: .utf8)
  else { return }

  print(svg)
}

Example GraphViz Output

digraph {
  a -> b
  a -> c
  b -> c [constraint=false]
}

Note: render(using:to:) and related methods require GraphViz to be installed on your system.

Using Function Builders, Custom Operators, and Fluent Attribute Setters

import GraphViz

let graph = Graph(directed: true) {
    "a" --> "b"
    "a" --> "c"
    ("b" --> "c").constraint(false)
}

Note: Swift 5.1 may require explicit typecast expressions in order to reconcile use of custom edge operators like -->. (error: ambiguous reference to member '-->')

Installation

Swift Package Manager

Add the GraphViz package to your target dependencies in Package.swift:

import PackageDescription

let package = Package(
  name: "YourProject",
  dependencies: [
    .package(
        url: "https://github.com/SwiftDocOrg/GraphViz",
        from: "0.3.0"
    ),
  ]
)

Add GraphViz as a dependency to your target(s):

targets: [
.target(
    name: "YourTarget",
    dependencies: ["GraphViz"]),

To render graphs to SVG, PNG, and other formats, you must have GraphViz executables (e.g. dot) installed on your system and accessible from $PATH. You can install GraphViz from the command line:

# macOS
$ brew install graphviz

# Linux (Ubuntu)
$ sudo apt-get install graphviz

License

MIT

Contact

Mattt (@mattt)

About

A Swift package for working with GraphViz

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Swift 61.0%
  • C 29.6%
  • C++ 8.3%
  • Objective-C 1.1%