Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update dot option definitions to latest (2015) #89

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 43 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,47 @@ This graph shows all loaded RGL modules:
Look for more in
[examples](https://github.com/monora/rgl/tree/master/examples) directory.

## (Optional) Configuring DOT output options

The default graph will use standard DOT output visuals.

If you wish to configure the styling of the diagram we provide the methods `set_edge_options` and `set_vertex_options` for this purpose. You can use any options from the `NODE_OPTS` and `EDGE_OPTS` constants in `lib/rgl/rdot.rb`. Simply use the exact option name as an argument in your method call.

You can also configure the overall appearance of the graph by passing a hash of options from `GRAPH_OPTS` to the output method. The example below shows an example of styling vertices, edges and setting some basic graph options.

![colored diagram](images/styled_graph.png "Colored DOT graph")

```ruby
require 'rgl/adjacency'
require 'rgl/dot'

graph = RGL::DirectedAdjacencyGraph['a', 'b', 'b', 'c']

graph.add_edge('a', 'b')
graph.add_edge('a', 'c')

# Vertex Settings
graph.set_vertex_options('a', label: 'This is A', shape: 'box3d', fontcolor: 'green', fontsize: 16)
graph.set_vertex_options('b', label: 'This is B', shape: 'tab', fontcolor: 'red', fontsize: 14)
graph.set_vertex_options('c', shape: 'tab', fontcolor: 'blue')

# Edge Settings
graph.set_edge_options('a', 'b', label: 'NotCapitalEdge', style: 'dotted', direction: 'back', color: 'magenta')
graph.set_edge_options('a', 'c', weight: 5, color: 'blue')

# This hash contains the configuration for the overall graph
# Applicable values are listed in lib/rgl/rdot.rb GRAPH_OPTS
# The values for edge and vertex will make use of the hashes above
graph_options = {
"rankdir" => "LR",
"labelloc" => "t",
"label" => "Graph\n (generated #{Time.now.utc})"
}

graph.write_to_graphic_file('png', 'graph', graph_options)

```

## Credits

Many thanks to Robert Feldt which also worked on a graph library
Expand All @@ -249,14 +290,14 @@ the module {RGL::DOT} which is used instead of Roberts module to visualize
graphs.

Jeremy Bopp, John Carter, Sascha Doerdelmann, Shawn Garbett, Andreas
Schörk, Dan Čermák and Kirill Lashuk for contributing additions, test
Schörk, Dan Čermák, Kirill Lashuk and Markus Napp for contributing additions, test
cases and bugfixes.

See also: https://github.com/monora/rgl/contributors

## Copying

RGL is Copyright (c) 2002,2004,2005,2008,2013,2015,2019,2020,2022 by Horst
RGL is Copyright (c) 2002,2004,2005,2008,2013,2015,2019,2020,2022,2023 by Horst
Duchene. It is free software, and may be redistributed under the [Ruby
license](https://en.wikipedia.org/wiki/Ruby_License) and terms specified in
the LICENSE file.
Binary file added images/styled_graph.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 23 additions & 36 deletions lib/rgl/dot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,55 +52,42 @@ def to_dot_graph(params = {})
fontsize = params['fontsize'] ? params['fontsize'] : '8'
graph = (directed? ? DOT::Digraph : DOT::Graph).new(params)
edge_class = directed? ? DOT::DirectedEdge : DOT::Edge
vertex_options = params['vertex'] || {}
edge_options = params['edge'] || {}

each_vertex do |v|
default_vertex_options = {
'name' => vertex_id(v),
'fontsize' => fontsize,
'label' => vertex_label(v)
}
each_vertex_options = default_vertex_options.merge(vertex_options)



vertex_options.each do |option, val|
each_vertex_options[option] = if val.is_a?(Proc)
if val.call(v).nil?
nil
elsif val.call(v).key?(:"#{option}")
val.call(v)[:"#{option}"]
end
else
val
end
end

graph << DOT::Node.new(each_vertex_options)
each_vertex_options = default_vertex_options

if @vertex_options && @vertex_options[v]
RGL::DOT::NODE_OPTS.each do |opt|
if @vertex_options[v].key?(:"#{opt}")
each_vertex_options["#{opt}"] = @vertex_options[v].fetch(:"#{opt}")
end
end
end
graph << DOT::Node.new(each_vertex_options)
end

each_edge do |u, v|
edges.each do |edge|
default_edge_options = {
'from' => vertex_id(u),
'to' => vertex_id(v),
'from' => edge.source,
'to' => edge.target,
'fontsize' => fontsize
}
each_edge_options = default_edge_options.merge(edge_options)

edge_options.each do |option, val|
each_edge_options[option] = if val.is_a?(Proc)
if val.call(u, v).nil?
nil
elsif val.call(u, v).key?(:"#{option}")
val.call(u, v)[:"#{option}"]
end
else
val
end
end

graph << edge_class.new(each_edge_options)
each_edge_options = default_edge_options

if @edge_options && @edge_options[edge]
RGL::DOT::EDGE_OPTS.each do |opt|
if @edge_options[edge].key?(:"#{opt}")
each_edge_options["#{opt}"] = @edge_options[edge].fetch(:"#{opt}")
end
end
end
graph << edge_class.new(each_edge_options)
end

graph
Expand Down