Skip to content

Commit 36f9720

Browse files
authored
Merge pull request #27 from bisraelsen/dev
modify preamble, new layouts Merging so I can fix any further edits and release
2 parents 1d490dd + 47f9938 commit 36f9720

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

src/TikzGraphs.jl

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,27 @@ const AbstractGraph = Union{Graph, DiGraph}
1212
using TikzPictures
1313

1414
module Layouts
15-
export Layered, Spring, SimpleNecklace
15+
export Layered, Spring, SpringElectrical, SimpleNecklace
1616

1717
abstract type Layout end
1818

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
2024

2125
struct Spring <: Layout
2226
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)
2436
end
2537

2638
struct SimpleNecklace <: Layout
@@ -55,7 +67,7 @@ end
5567
edge_str(g::DiGraph) = "->"
5668
edge_str(g::Graph) = "--"
5769

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
5971
o = IOBuffer()
6072
println(o, "\\graph [$(layoutname(layout)), $(options_str(layout))] {")
6173
for v in vertices(g)
@@ -70,20 +82,45 @@ function plot(g::AbstractGraph; layout::Layouts.Layout = Layered(), labels::Vect
7082
println(o, "$b;")
7183
end
7284
println(o, "};")
73-
mypreamble = preamble * "\n\\usegdlibrary{$(libraryname(layout))}"
85+
mypreamble = prepend_preamble * preamble * "\n\\usegdlibrary{$(libraryname(layout))}"
7486
TikzPicture(String(take!(o)), preamble=mypreamble, options=options)
7587
end
7688

7789
for (_layout, _libraryname, _layoutname) in [
7890
(:Layered, "layered", "layered layout"),
7991
(:Spring, "force", "spring layout"),
92+
(:SpringElectrical, "force", "spring electrical layout"),
8093
(:SimpleNecklace, "circular", "simple necklace layout")
8194
]
8295
@eval libraryname(p::$(_layout)) = $_libraryname
8396
@eval layoutname(p::$(_layout)) = $_layoutname
8497
end
8598

8699
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
88125

89126
end # module

0 commit comments

Comments
 (0)