@@ -12,15 +12,27 @@ const AbstractGraph = Union{Graph, DiGraph}
12
12
using TikzPictures
13
13
14
14
module Layouts
15
- export Layered, Spring, SimpleNecklace
15
+ export Layered, Spring, SpringElectrical, SimpleNecklace
16
16
17
17
abstract type Layout end
18
18
19
- struct Layered <: Layout end
19
+ struct Layered <: Layout
20
+ sib_dist
21
+ lev_dist
22
+ Layered (;sib_dist= - 1 ,lev_dist= - 1 ) = new (sib_dist,lev_dist)
23
+ end
20
24
21
25
struct Spring <: Layout
22
26
randomSeed
23
- Spring (;randomSeed= 42 ) = new (randomSeed)
27
+ dist
28
+ Spring (;randomSeed= 42 ,dist= - 1 ) = new (randomSeed,dist)
29
+ end
30
+
31
+ struct SpringElectrical <: Layout
32
+ randomSeed
33
+ charge
34
+ dist
35
+ SpringElectrical (;randomSeed= 42 ,charge= 1. ,dist= - 1 ) = new (randomSeed,charge,dist)
24
36
end
25
37
26
38
struct SimpleNecklace <: Layout
55
67
edge_str (g:: DiGraph ) = " ->"
56
68
edge_str (g:: Graph ) = " --"
57
69
58
- function plot (g:: AbstractGraph ; layout:: Layouts.Layout = Layered (), labels:: Vector{T} = map (string, vertices (g)), edge_labels:: Dict = Dict (), node_styles:: Dict = Dict (), node_style= " " , edge_styles:: Dict = Dict (), edge_style= " " , options= " " ) where T<: AbstractString
70
+ function plot (g:: AbstractGraph ; layout:: Layouts.Layout = Layered (), labels:: Vector{T} = map (string, vertices (g)), edge_labels:: Dict = Dict (), node_styles:: Dict = Dict (), node_style= " " , edge_styles:: Dict = Dict (), edge_style= " " , options= " " ,prepend_preamble :: String = " " ) where T<: AbstractString
59
71
o = IOBuffer ()
60
72
println (o, " \\ graph [$(layoutname (layout)) , $(options_str (layout)) ] {" )
61
73
for v in vertices (g)
@@ -70,20 +82,45 @@ function plot(g::AbstractGraph; layout::Layouts.Layout = Layered(), labels::Vect
70
82
println (o, " $b ;" )
71
83
end
72
84
println (o, " };" )
73
- mypreamble = preamble * " \n\\ usegdlibrary{$(libraryname (layout)) }"
85
+ mypreamble = prepend_preamble * preamble * " \n\\ usegdlibrary{$(libraryname (layout)) }"
74
86
TikzPicture (String (take! (o)), preamble= mypreamble, options= options)
75
87
end
76
88
77
89
for (_layout, _libraryname, _layoutname) in [
78
90
(:Layered , " layered" , " layered layout" ),
79
91
(:Spring , " force" , " spring layout" ),
92
+ (:SpringElectrical , " force" , " spring electrical layout" ),
80
93
(:SimpleNecklace , " circular" , " simple necklace layout" )
81
94
]
82
95
@eval libraryname (p:: $ (_layout)) = $ _libraryname
83
96
@eval layoutname (p:: $ (_layout)) = $ _layoutname
84
97
end
85
98
86
99
options_str (p:: Layouts.Layout ) = " "
87
- options_str (p:: Spring ) = " random seed = $(p. randomSeed) "
100
+ function options_str (p:: Layouts.Layered )
101
+ sib_str = " "
102
+ lev_str = " "
103
+ if p. sib_dist > 0
104
+ sib_str= " sibling distance=$(p. sib_dist) mm,"
105
+ end
106
+ if p. lev_dist > 0
107
+ lev_str = " level distance=$(p. lev_dist) mm,"
108
+ end
109
+ return sib_str* lev_str
110
+ end
111
+ function options_str (p:: Spring )
112
+ if p. dist == - 1
113
+ return " random seed = $(p. randomSeed) ,"
114
+ else
115
+ return " random seed = $(p. randomSeed) , node distance=$(p. dist) ,"
116
+ end
117
+ end
118
+ function options_str (p:: SpringElectrical )
119
+ if p. dist == - 1
120
+ return " random seed = $(p. randomSeed) , electric charge=$(p. charge) ,"
121
+ else
122
+ return " random seed = $(p. randomSeed) , electric charge=$(p. charge) , node distance=$(p. dist) ,"
123
+ end
124
+ end
88
125
89
126
end # module
0 commit comments