GraphViz

CI Documentation

用于 GraphViz 的 Swift 包。

要求

用法

import GraphViz
import DOT

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 using dot layout algorithm
let data = try! DOTRenderer(using: .dot, to: .svg).render(graph: graph)
let svg = String(data: data, encoding: .utf8)!

Example GraphViz Output

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

注意: render(using:to:) 方法要求可以从当前的 $PATH 访问与指定布局算法对应的 GraphViz 二进制文件。

使用函数构建器、自定义运算符和流畅属性设置器

要使用以下接口,请将 “GraphVizBuilder” 添加到你的包依赖项中,并根据需要将 import GraphViz 替换为 import GraphVizBuilder

import GraphVizBuilder

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

注意: Swift 5.1 可能需要显式类型转换表达式,以便协调使用自定义边运算符(如 -->)。(错误:对成员 --> 的引用不明确)

安装

Swift Package Manager

将 GraphViz 包添加到你的目标依赖项中的 Package.swift 文件中

import PackageDescription

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

GraphViz 作为依赖项添加到你的目标中

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

要将图形渲染为 SVG、PNG 和其他格式,你必须在你的系统上安装 GraphViz 可执行文件(例如 dot)并使其可以从 $PATH 访问。你可以从命令行安装 GraphViz

# macOS
$ brew install graphviz

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

许可

MIT

联系方式

Mattt (@mattt)