ProductPromotion
Logo

Clojure

made by https://0x3d.site

GitHub - daveray/dorothy: Hiccup-style generation of Graphviz graphs in Clojure
Hiccup-style generation of Graphviz graphs in Clojure - daveray/dorothy
Visit Site

GitHub - daveray/dorothy: Hiccup-style generation of Graphviz graphs in Clojure

GitHub - daveray/dorothy: Hiccup-style generation of Graphviz graphs in Clojure

dorothy Travis CI status

Hiccup-style generation of Graphviz graphs in Clojure and ClojureScript.

Dorothy is extremely alpha and subject to radical change. Release Notes Here

Usage

Dorothy assumes you have an understanding of Graphviz and DOT. The text below describes the mechanics of Dorothy's DSL, but you'll need to refer to the Graphviz documentation for specifics on node shapes, valid attributes, etc.

The Graphviz dot tool executable must be on the system path to render

Dorothy is on Clojars. In Leiningen:

[dorothy "x.y.z"]

A graph consists of a vector of statements. The following sections describe the format for all the types of statements. If you're bored, skip ahead to the "Defining Graphs" section below.

Node Statement

A node statement defines a node in the graph. It can take two forms:

node-id

[node-id]

[node-id { attr map }]

where node-id is a string, number or keyword with optional trailing port and compass-point. Here are some node statement examples:

:node0          ; Define a node called "node0"

:node0:port0    ; Define a node called "node0" with port "port0"

:node0:port0:sw ; Similarly a node with southwest compass point

the node's attr map is a map of attributes for the node. For example,

[:start {:shape :Mdiamond}]
; => start [shape=Mdiamond];

Dorothy will correctly escape and quote node-ids as required by dot.

A node id can also be auto-generated with (gen-id object). For example,

[(gen-id some-object) {:label (.getText some-object)}]

It allows you to use arbitrary objects as nodes.

Edge Statement

An edge statement defines an edge in the graph. It is expressed as a vector with two or more node-ids followed optional attribute map:

[node-id0 node-id1 ... node-idN { attr map }]
; => "node-id0" -> "node-id1" -> ... -> "node-idN" [attrs ...];

In addition to node ids, an edge statement may also contain subgraphs:

[:start (subgraph [... subgraph statements ...])]

For readability, :> delimiters may be optionally included in an edge statement:

[:start :> :middle :> :end]

Graph Attribute Statement

A graph attribute statement sets graph-wide attributes. It is expressed as a single map:

{:label "process #1", :style :filled, :color :lightgrey}
; => graph [label="process #1",style=filled,color=lightgrey];

alternatively, this can be expressed with the (graph-attrs) function like this:

(graph-attrs {:label "process #1", :style :filled, :color :lightgrey})
; => graph [label="process #1",style=filled,color=lightgrey];

Node and Edge Attribute Statement

A node attribute or edge attribute statement sets node or edge attributes respectively for all nodes and edge statements that follow. It is expressed with (node-attrs) and (edge-attrs) statements:

(node-attrs {:style :filled, :color :white})
; => node [style=filled,color=white];

or:

(edge-attrs {:color :black})
; => edge [color=black];

Defining Graphs

As mentioned above, a graph consists of a series of statements. These statements are passed to the graph, digraph, or subgraph functions. Each takes an optional set of attributes followed by a vector of statements:

; From http://www.graphviz.org/content/cluster
(digraph [
  (subgraph :cluster_0 [
    {:style :filled, :color :lightgrey, :label "process #1"}
    (node-attrs {:style :filled, :color :white})

    [:a0 :> :a1 :> :a2 :> :a3]])

  (subgraph :cluster_1 [
    {:color :blue, :label "process #2"}
    (node-attrs {:style :filled})

    [:b0 :> :b1 :> :b2 :> :b3]])

  [:start :a0]
  [:start :b0]
  [:a1    :b3]
  [:b2    :a3]
  [:a3    :a0]
  [:a3    :end]
  [:b3    :end]

  [:start {:shape :Mdiamond}]
  [:end   {:shape :Msquare}]])

Similarly for (graph) (undirected graph) and (subgraph). A second form of these functions takes an initial option map, or a string or keyword id for the graph:

(graph :graph-id ...)
; => graph "graph-id" { ... }

(digraph { :id :G :strict? true } ...)
; => strict graph G { ... }

Generate Graphviz dot format

Given a graph built with the functions described above, use the (dot) function to generate Graphviz DOT output.

(require '[dorothy.core :as dot])
(def g (dot/graph [ ... ]))
(dot/dot g)
"graph { ... }"

Rendering images (ClojureScript)

Dorothy currently doesn't include any facilities for rendering dot-format output to images. However, you can pull in viz.cljc or viz.js, both of which will allow you to produce png, svg, and other image formats from your dorothy-generated dot-formatted graph content.

Wanted: pull requests to implement node equivalents to the rendering functions available for Clojure/JVM in the dorothy.jvm namespace. link to github issue here

Render images via graphviz (Clojure/JVM)

Once you have DOT language output, you can render it as an image using the (render) function:

(require '[dorothy.jvm :refer (render save! show!)])

; This produces a png as an array of bytes
(render graph {:format :png})

; This produces an SVG string
(render graph {:format :svg})

; A one-liner with a very simple 4 node digraph.
(-> (dot/digraph [ [:a :b :c] [:b :d] ])
    dot/dot
    (render {:format :svg}))

The dot tool executable must be on the system path

other formats include :pdf, :gif, etc. The result will be either a java byte array, or String depending on whether the format is binary or not. (render) returns a string or a byte array depending on whether the output format is binary or not.

Alternatively, use the (save!) function to write to a file or output stream.

; A one-liner with a very simple 4 node digraph
(-> (dot/digraph [ [:a :b :c] [:b :d] ])
    dot/dot
    (save! "out.png" {:format :png}))

Finally, for simple tests, use the (show!) function to view the result in a simple Swing viewer:

; This opens a simple Swing viewer with the graph
(show! graph)

; A one-liner with a very simple 4 node digraph
(-> (dot/digraph [ [:a :b :c] [:b :d] ])
    dot/dot
    show!)

which shows:

License

Copyright (C) 2011-2017 Dave Ray and contributors

Distributed under the Eclipse Public License, the same as Clojure.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory