Giter Club home page Giter Club logo

graphviz's Introduction

GraphViz

CI Documentation

A Swift package for working with GraphViz.

Requirements

  • Swift 5.1+
  • 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)

// 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)!

Example GraphViz Output

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

Note: The render(using:to:) method requires that the GraphViz binary corresponding to the specified layout algorithm is accessible from the current $PATH.

Using Function Builders, Custom Operators, and Fluent Attribute Setters

To use the following interface, add "GraphVizBuilder" to your package's dependencies and replace import GraphViz with import GraphVizBuilder as needed.

import GraphVizBuilder

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.1.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)

graphviz's People

Contributors

mattt avatar

Stargazers

Acampbell avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.