graphvix v0.2.0 Graphvix

Use Graphvix to create a directed graph utilizing GenServer.

Graphvix allows:

  • creating a new graph
graph = Graphvix.new
  • adding/updating nodes and edges
node1 = Graphvix.add_node(graph, label: "Start")
node2 = Graphvix.add_node(graph, label: "End")

edge = Graphvix.add_edge(graph, node1, node2, color: "red")

Graphvix.update(graph, node1.id, color: "blue")
  • saving to a DOT file
Graphvix.save(graph, "my_graph")
  • compiling to PDF (also saves the graph as an intermediate step)
Graphvix.graph(graph)

Summary

Functions

Creates a new cluster with the listed nodes

Adds a new edge to the graph between node1 and node2 with the provided attrs and a generated unique id

Adds a new node to the graph with the provided attrs and a generated unique id

Add one or more nodes to the cluster in the graph

Saves the graph and compiles to the provided filetype

Saves the graph and compiles to the provided filename and filetype

Returns the node or edge in the graph with the provided id

Returns the intetrnal structure of the graph, a map with nested maps for nodes and edges

Save the graph to a DOT file, compile it to the provided filetype, and open the file

Save the graph to a DOT file, compile it to PDF, and open the PDF file

Creates a new graph, and returns a PID pointing to the process managing the graph

Removes the node, edge, or cluster with the provided id from the graph

Remove one or more nodes from the cluster in the graph

Saves the graph to a DOT file with the provided filename (“G” by default)

Updates the attributes for the node or edge in the graph with the provided id

Returns a string representation in .dot format of the graph

Types

node_or_id()
node_or_id :: map | pos_integer

Functions

add_cluster(graph, nodes \\ [])
add_cluster(pid, list) :: map

Creates a new cluster with the listed nodes.

Examples

iex> graph = Graphvix.new
iex> node1 = Graphvix.add_node(graph, label: "Start")
iex> node2 = Graphvix.add_node(graph, label: "End")
iex> cluster = Graphvix.add_cluster(graph, [node1, node2])

You can also pass node ids instead of nodes

iex> graph = Graphvix.new
iex> node1 = Graphvix.add_node(graph, label: "Start")
iex> node2 = Graphvix.add_node(graph, label: "End")
iex> cluster = Graphvix.add_cluster(graph, [node1.id, node2.id])
add_edge(graph, n1, n2, attrs \\ [])
add_edge(pid, node_or_id, node_or_id, Keyword.t) :: map

Adds a new edge to the graph between node1 and node2 with the provided attrs and a generated unique id.

Returns the map containing the edge data

Examples

iex> graph = Graphvix.new
iex> node1 = Graphvix.add_node(graph, label: "Start")
iex> node2 = Graphvix.add_node(graph, label: "End")
iex> Graphvix.add_edge(graph, node1, node2)
%{ id: 3, start_node: 1, end_node: 2, attrs: [] }

You can also pass node ids instead of nodes as the 2nd and 3rd parameters.

iex> graph = Graphvix.new
iex> node1 = Graphvix.add_node(graph, label: "Start")
iex> node2 = Graphvix.add_node(graph, label: "End")
iex> Graphvix.add_edge(graph, node1.id, node2.id)
%{ id: 3, start_node: 1, end_node: 2, attrs: [] }
add_node(graph, attrs \\ [])
add_node(pid, Keyword.t) :: map

Adds a new node to the graph with the provided attrs and a generated unique id.

Returns the map containing the id and attributes for the new node.

Examples

iex> graph = Graphvix.new
iex> Graphvix.add_node(graph, label: "First Node", color: "red")
%{ id: 1, attrs: [label: "First Node", color: "red"] }
add_to_cluster(graph, cluster_id, node_ids)
add_to_cluster(pid, pos_integer, [node_or_id] | node_or_id) :: map

Add one or more nodes to the cluster in the graph.

The third argument can be a single node id or a list of node ids.

Examples

iex> graph = Graphvix.new
iex> node1 = Graphvix.add_node(graph, label: "Start")
iex> node2 = Graphvix.add_node(graph, label: "End")
iex> node3 = Graphvix.add_node(graph, label: "Epilogue")
iex> cluster = Graphvix.add_cluster(graph, [node1, node2])
iex> Graphvix.add_to_cluster(graph, cluster.id, node3.id)
compile(graph, filetype)
compile(pid, atom) :: {String.t, atom}

Saves the graph and compiles to the provided filetype.

Examples

iex> graph = Graphvix.new
iex> ...
iex> ...
iex> Graphvix.compile(graph)
compile(graph, filename \\ "G", filetype \\ :pdf)
compile(pid, String.t | nil, atom | nil) :: {String.t, atom}

Saves the graph and compiles to the provided filename and filetype.

Defaults are G.dot and :pdf

Examples

iex> graph = Graphvix.new
iex> ...
iex> ...
iex> Graphvix.compile(graph)
find(graph, id)
find(pid, pos_integer) :: map

Returns the node or edge in the graph with the provided id.

Examples

iex> graph = Graphvix.new
iex> node1 = Graphvix.add_node(graph, label: "Start")
iex> Graphvix.find(graph, node1.id) == node1
true

Returns nil if no node or edge is found.

iex> graph = Graphvix.new
iex> Graphvix.find(graph, 3)
nil
get(graph)
get(pid) :: map

Returns the intetrnal structure of the graph, a map with nested maps for nodes and edges.

Examples

iex> graph = Graphvix.new
iex> Graphvix.get(graph)
%{ nodes: %{}, edges: %{} }
graph(graph, filetype)
graph(pid, atom) :: {String.t, atom}

Save the graph to a DOT file, compile it to the provided filetype, and open the file.

Examples

iex> graph = Graphvix.new
iex> ...
iex> ...
iex> Graphvix.graph(graph)
:ok

After running this, G.dot and G.pdf will exist in your working directory, and G.pdf will open in your preferred PDF viewer.

graph(graph, filename \\ "G", filetype \\ :pdf)
graph(pid, String.to | nil, atom | nil) :: {String.t, atom}

Save the graph to a DOT file, compile it to PDF, and open the PDF file.

Examples

iex> graph = Graphvix.new
iex> ...
iex> ...
iex> Graphvix.graph(graph)
:ok

After running this, G.dot and G.pdf will exist in your working directory, and G.pdf will open in your preferred PDF viewer.

new()
new :: pid

Creates a new graph, and returns a PID pointing to the process managing the graph.

Examples

iex> graph = Graphvix.new
#PID<0.522.0>
remove(graph, id)
remove(pid, pos_integer) :: :ok

Removes the node, edge, or cluster with the provided id from the graph

iex> graph = Graphvix.new
iex> node1 = Graphvix.add_node(graph, label: "Start")
iex> Graphvix.remove(graph, node1.id)
remove_from_cluster(graph, cluster_id, node_ids)
remove_from_cluster(pid, pos_integer, [node_or_id] | node_or_id) :: map

Remove one or more nodes from the cluster in the graph.

The third argument can be a single node id or a list of node ids.

Examples

iex> graph = Graphvix.new
iex> node1 = Graphvix.add_node(graph, label: "Start")
iex> node2 = Graphvix.add_node(graph, label: "End")
iex> node3 = Graphvix.add_node(graph, label: "Epilogue")
iex> cluster = Graphvix.add_cluster(graph, [node1, node2, node3])
iex> Graphvix.remove_from_cluster(graph, cluster.id, [node1.id, node3.id])
save(graph, filename \\ "G")
save(pid, String.t | nil) :: String.t

Saves the graph to a DOT file with the provided filename (“G” by default).

Examples

iex> graph = Graphvix.new
iex> ...
iex> ...
iex> Graphvix.save(graph)
:ok

After running this, G.dot will exist in your working directory.

update(graph, id, attrs)
update(pid, pos_integer, Keyword.t) :: :ok

Updates the attributes for the node or edge in the graph with the provided id.

Examples

iex> graph = Graphvix.new
iex> node1 = Graphvix.add_node(graph, label: "Start")
iex> Graphvix.update(graph, node1.id, color: "blue")
iex> Graphvix.find(graph, node1.id)
%{ id: 1, attrs: [label: "Start", color: "blue"] }
write(graph)
write(pid) :: String.t

Returns a string representation in .dot format of the graph.

Examples

iex> graph = Graphvix.new
iex> ...
iex> ...
iex> Graphvix.write(graph)
"digraph G { ... }"