Created
February 4, 2010 10:13
-
-
Save melborne/294499 to your computer and use it in GitHub Desktop.
Graphviz Sampler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding:utf-8 -*- | |
require "graphaz" | |
class Sampler | |
@@q = Hash.new{[]} | |
def self.initialize | |
flag, color = nil, nil | |
data = DATA.readlines.map { |line| line.chomp } | |
data.each do |item| | |
next if item.empty? | |
case item | |
when /^[A-Z]:([\w_]+)$/ then flag = $1.to_sym; next | |
when /^:(\w+)$/ then color = $1.to_sym; next | |
end | |
@@q[flag] += [item] | |
end | |
end | |
initialize | |
class << self | |
@@q.keys.each do |name| #name: Shapes, Arrows, Colors, Codes | |
define_method(name.downcase) { @@q[name] } | |
end | |
end | |
COLORS, SHAPES, ARROWS, CODES = colors, shapes, arrows, codes | |
DARKS = %w(black urlywood navyblue navy indigo darkslateblue mediumblue midnightblue blue darkslategray) | |
LIGHTS = %w(azure cornsilk floralwhite ghostwhite honeydew ivory lavender lavenderblush lemonchiffon linen mintcream oldlace seashell snow white whitesmoke lightgoldenrodyellow lightyellow beige khaki lightcyan aliceblue) | |
def color_sample(format=nil) | |
ga = sample(COLORS, GraphAz.new) do |name, color| | |
{:style => 'filled', :color => color} | |
end | |
ga.print_graph(format) | |
end | |
def shape_sample(format=nil) | |
ga = shape_core(GraphAz.new) | |
ga.print_graph(format) | |
end | |
def shape_animation_sample(time=5, format={}) | |
ga = shape_core(GraphAz.new) | |
time.times do | |
SHAPES.each do |name| | |
color = COLORS.sample.sub(/\[.*$/, '') | |
ga.node(name, :fillcolor => color) | |
ga.node(name, :fontcolor => 'white') if DARKS.include?(color) | |
ga.node(name, :fontcolor => 'black') if LIGHTS.include?(color) | |
end | |
ga.lap | |
end | |
ga.write(format) | |
end | |
def attr_list(name, format={}) #name: node_attr, edge_attr, graph_attr | |
ga = GraphAz.new | |
ga.gnode[:shape] = 'record' | |
ga[:bgcolor] = format.delete(:color) | |
attrs = self.class.send(name) | |
data = | |
attrs.map do |line| | |
line.sub!(/^\w+/, '\0 ( )') unless line =~ /^\w+?\s*\(.*\)/ | |
line.split(/[()]/, 3).map { |item| item.strip } | |
end. | |
transpose.map { |items| items.join("\n\n") }.join("|") | |
ga.add(name.to_s, :label => data) | |
ga.print_graph(format) | |
end | |
private | |
def sample(sample, ga) | |
route = [] | |
sample.each_with_index do |name, i| | |
color = (sample.include?('polygon') ? COLORS.sample : name).sub(/\[.*$/, '') | |
ga.add(name, yield(name, color)) | |
ga.node(name, :fontcolor => 'white') if DARKS.include?(color) | |
route << name | |
next unless (i+1)%(sample.length/4.0).ceil == 0 or i >= sample.length-1 | |
ga.add route.join(" => ") | |
route = [] | |
end | |
ga | |
end | |
def shape_core(ga) | |
ga = sample(SHAPES, ga) do |name, color| | |
{:shape => name, :style => 'filled', :fillcolor => color} | |
end | |
ga.edges.keys.each_with_index do |name, i| | |
arrow = ARROWS[i%ARROWS.length] | |
ga.edge(name, :arrowhead => arrow, :label => arrow) | |
end | |
ga | |
end | |
end | |
s = Sampler.new | |
#s.color_sample(:png => 'colors.png') | |
#s.shape_sample(:png => 'shape.png') | |
#s.shape_animation_sample | |
s.attr_list(:node_attr, :color => 'powderblue', :png => 'node.png') | |
s.attr_list(:edge_attr, :color => 'plum', :png => 'edge.png') | |
s.attr_list(:graph_attr, :color => 'yellowgreen', :png => 'graph.png') | |
__END__ | |
A:Node_attr | |
Name ( Default ) Values | |
bottomlabel auxiliary label for nodes of shape M* | |
color (black) node shape color | |
comment any string (format-dependent) | |
distortion (0.0) node distortion for shape=polygon | |
fillcolor ( lightgrey/black ) node fill color | |
fixedsize ( false ) label text has no affect on node size | |
fontcolor ( black ) type face color | |
fontname ( Times-Roman ) font family | |
fontsize ( 14 ) point size of label | |
group name of node’s group | |
height ( .5 ) height in inches | |
label ( node name ) any string | |
layer ( overlay range ) all, id or id:id | |
orientation ( 0.0 ) node rotation angle | |
peripheries ( shape-dependent ) number of node boundaries | |
regular ( false ) force polygon to be regular | |
shape ( ellipse ) node shape; see Section 2.1 and Appendix E | |
shapefile external EPSF or SVG custom shape file | |
sides ( 4 ) number of sides for shape=polygon | |
skew ( 0.0 ) skewing of node for shape=polygon | |
style graphics options, e.g. bold, dotted, filled; cf. Section 2.3 | |
toplabel auxiliary label for nodes of shape M* | |
URL URL associated with node (format-dependent) | |
width ( .75 ) width in inches | |
z ( 0.0 ) z coordinate for VRML output | |
B:Edge_attr | |
Name ( Default ) Values | |
arrowhead ( normal ) style of arrowhead at head end | |
arrowsize ( 1.0 ) scaling factor for arrowheads | |
arrowtail ( normal ) style of arrowhead at tail end | |
color ( black ) edge stroke color | |
comment any string (format-dependent) | |
constraint ( true ) use edge to affect node ranking | |
decorate if set, draws a line connecting labels with their edges | |
dir ( forward ) forward, back, both, or none | |
fontcolor ( black ) type face color | |
fontname ( Times-Roman ) font family | |
fontsize ( 14 ) point size of label | |
headlabel label placed near head of edge | |
headport n,ne,e,se,s,sw,w,nw | |
headURL URL attached to head label if output format is ismap | |
label edge label | |
labelangle ( -25.0 ) angle in degrees which head or tail label is rotated off edge | |
labeldistance ( 1.0 ) scaling factor for distance of head or tail label from node | |
labelfloat ( false ) lessen constraints on edge label placement | |
labelfontcolor ( black ) type face color for head and tail labels | |
labelfontname ( Times-Roman ) font family for head and tail labels | |
labelfontsize ( 14 ) point size for head and tail labels | |
layer ( overlay range ) all, id or id:id | |
lhead name of cluster to use as head of edge | |
ltail name of cluster to use as tail of edge | |
minlen ( 1 ) minimum rank distance between head and tail | |
samehead tag for head node; edge heads with the same tag are merged onto the same port | |
sametail tag for tail node; edge tails with the same tag are merged onto the same port | |
style graphics options, e.g. bold, dotted, filled; cf. Section 2.3 | |
taillabel label placed near tail of edge | |
tailport n,ne,e,se,s,sw,w,nw | |
tailURL URL attached to tail label if output format is ismap | |
weight ( 1 ) integer cost of stretching an edge | |
C:Graph_attr | |
Name ( Default ) Values | |
bgcolor background color for drawing, plus initial fill color | |
center ( false ) center drawing on page | |
clusterrank ( local ) may be global or none | |
color ( black ) for clusters, outline color, and fill color if fillcolor not defined | |
comment any string (format-dependent) | |
compound ( false ) allow edges between clusters | |
concentrate ( false ) enables edge concentrators | |
fillcolor ( black ) cluster fill color | |
fontcolor ( black ) type face color | |
fontname ( Times-Roman ) font family | |
fontpath list of directories to such for fonts | |
fontsize ( 14 ) point size of label | |
label any string | |
labeljust ( centered ) ”l” and ”r” for left- and right-justified cluster labels, respectively | |
labelloc ( top ) ”t” and ”b” for top- and bottom-justified cluster labels, respectively | |
layers id:id:id... | |
margin ( .5 ) margin included in page, inches | |
mclimit ( 1.0 ) scale factor for mincross iterations | |
nodesep ( .25 ) separation between nodes, in inches. | |
nslimit if set to f, bounds network simplex iterations by (f)(number of nodes) when setting x-coordinates | |
nslimit1 if set to f, bounds network simplex iterations by (f)(number of nodes) when ranking nodes | |
ordering if out out edge order is preserved | |
orientation ( portrait ) if rotate is not used and the value is landscape, use landscape orientation | |
page unit of pagination, e.g. "8.5,11" | |
pagedir ( BL ) traversal order of pages | |
quantum if quantum ¿ 0.0, node label dimensions will be rounded to integral multiples of quantum | |
rank same, min, max, source or sink | |
rankdir ( TB ) LR (left to right) or TB (top to bottom) | |
ranksep ( .75 ) separation between ranks, in inches. | |
ratio approximate aspect ratio desired, fill or auto | |
remincross if true and there are multiple clusters, re-run crossing minimization | |
rotate If 90, set orientation to landscape | |
samplepoints ( 8 ) number of points used to represent ellipses and circles on output (cf. Appendix C | |
searchsize ( 30 ) maximum edges with negative cut values to check when looking for a minimum one during network simplex | |
size maximum drawing size, in inches | |
style graphics options, e.g. filled for clusters | |
URL URL associated with graph (format-dependent) | |
D:Shapes | |
box | |
polygon | |
ellipse | |
circle | |
point | |
egg | |
triangle | |
plaintext | |
diamond | |
trapezium | |
parallelogram | |
house | |
hexagon | |
octagon | |
doublecircle | |
doubleoctagon | |
tripleoctagon | |
invtriangle | |
invtrapezium | |
invhouse | |
Mdiamond | |
Msquare | |
Mcircle | |
record | |
Mrecord | |
E:Arrows | |
normal | |
dot | |
odot | |
inv | |
invdot | |
invodot | |
none | |
G:Colors | |
:Whites | |
antiquewhite[1-4] | |
azure[1-4] | |
bisque[1-4] | |
blanchedalmond | |
cornsilk[1-4] | |
floralwhite | |
gainsboro | |
ghostwhite | |
honeydew[1-4] | |
ivory[1-4] | |
lavender | |
lavenderblush[1-4] | |
lemonchiffon[1-4] | |
linen | |
mintcream | |
mistyrose[1-4] | |
moccasin | |
navajowhite[1-4] | |
oldlace | |
papayawhip | |
peachpuff[1-4] | |
seashell[1-4] | |
snow[1-4] | |
thistle[1-4] | |
wheat[1-4] | |
white | |
whitesmoke | |
:Greys | |
darkslategray[1-4] | |
dimgray | |
gray[0-100] | |
lightgray | |
lightslategray | |
slategray[1-4] | |
:Blacks | |
black | |
:Reds | |
coral[1-4] | |
crimson | |
darksalmon | |
deeppink[1-4] | |
firebrick[1-4] | |
hotpink[1-4] | |
indianred[1-4] | |
lightpink[1-4] | |
lightsalmon[1-4] | |
maroon[1-4] | |
pink[1-4] | |
red[1-4] | |
salmon[1-4] | |
tomato[1-4] | |
:Browns | |
beige | |
brown[1-4] | |
urlywood[1-4] | |
chocolate[1-4] | |
darkkhaki | |
khaki[1-4] | |
peru | |
rosybrown[1-4] | |
saddlebrown | |
sandybrown | |
sienna[1-4] | |
tan[1-4] | |
:Oranges | |
darkorange[1-4] | |
orange[1-4] | |
orangered[1-4] | |
:Yellows | |
darkgoldenrod[1-4] | |
gold[1-4] | |
goldenrod[1-4] | |
greenyellow | |
lightgoldenrod[1-4] | |
lightgoldenrodyellow | |
lightyellow[1-4] | |
palegoldenrod | |
yellow[1-4] | |
yellowgreen | |
:Greens | |
chartreuse[1-4] | |
darkgreen | |
darkolivegreen[1-4] | |
darkseagreen[1-4] | |
forestgreen | |
green[1-4] | |
lawngreen | |
lightseagreen | |
limegreen | |
mediumseagreen | |
mediumspringgreen | |
olivedrab[1-4] | |
palegreen[1-4] | |
seagreen[1-4] | |
springgreen[1-4] | |
mediumorchid[1-4] | |
:Cyans | |
aquamarine[1-4] | |
cyan[1-4] | |
darkturquoise | |
lightcyan[1-4] | |
mediumaquamarine | |
mediumturquoise | |
paleturquoise[1-4] | |
turquoise[1-4] | |
:Blues | |
aliceblue | |
blue[1-4] | |
cadetblue[1-4] | |
cornflowerblue | |
darkslateblue | |
deepskyblue[1-4] | |
dodgerblue[1-4] | |
indigo | |
lightblue[1-4] | |
lightskyblue[1-4] | |
lightslateblue[1-4] | |
mediumblue | |
mediumslateblue | |
midnightblue | |
navy | |
navyblue | |
powderblue | |
royalblue[1-4] | |
skyblue[1-4] | |
slateblue[1-4] | |
steelblue[1-4] | |
:Magentas | |
blueviolet | |
darkorchid[1-4] | |
darkviolet | |
magenta[1-4] | |
mediumpurple[1-4] | |
mediumvioletred | |
orchid[1-4] | |
palevioletred[1-4] | |
plum[1-4] | |
purple[1-4] | |
violet | |
violetred[1-4] | |
Z:Codes | |
ALFA | |
BRAVO | |
CHARLIE | |
DELTA | |
ECHO | |
FOXTROT | |
GOLF | |
HOTEL | |
INDIA | |
JULIETT | |
KILO | |
LIMA | |
MIKE | |
NOVEMBER | |
OSCAR | |
PAPA | |
QUEBEC | |
ROMEO | |
SIERRA | |
TANGO | |
UNIFORM | |
VICTOR | |
WHISKY | |
X-RAY | |
YANKEE | |
ZULU |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment