Skip to content

Commit

Permalink
Merge pull request #27 from bisraelsen/dev
Browse files Browse the repository at this point in the history
modify preamble, new layouts
Merging so I can fix any further edits and release
  • Loading branch information
Shushman authored Sep 2, 2018
2 parents 1d490dd + 47f9938 commit 36f9720
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/TikzGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,27 @@ const AbstractGraph = Union{Graph, DiGraph}
using TikzPictures

module Layouts
export Layered, Spring, SimpleNecklace
export Layered, Spring, SpringElectrical, SimpleNecklace

abstract type Layout end

struct Layered <: Layout end
struct Layered <: Layout
sib_dist
lev_dist
Layered(;sib_dist=-1,lev_dist=-1) = new(sib_dist,lev_dist)
end

struct Spring <: Layout
randomSeed
Spring(;randomSeed=42) = new(randomSeed)
dist
Spring(;randomSeed=42,dist=-1) = new(randomSeed,dist)
end

struct SpringElectrical <: Layout
randomSeed
charge
dist
SpringElectrical(;randomSeed=42,charge=1.,dist=-1) = new(randomSeed,charge,dist)
end

struct SimpleNecklace <: Layout
Expand Down Expand Up @@ -55,7 +67,7 @@ end
edge_str(g::DiGraph) = "->"
edge_str(g::Graph) = "--"

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

for (_layout, _libraryname, _layoutname) in [
(:Layered, "layered", "layered layout"),
(:Spring, "force", "spring layout"),
(:SpringElectrical, "force", "spring electrical layout"),
(:SimpleNecklace, "circular", "simple necklace layout")
]
@eval libraryname(p::$(_layout)) = $_libraryname
@eval layoutname(p::$(_layout)) = $_layoutname
end

options_str(p::Layouts.Layout) = ""
options_str(p::Spring) = "random seed = $(p.randomSeed)"
function options_str(p::Layouts.Layered)
sib_str = ""
lev_str = ""
if p.sib_dist > 0
sib_str="sibling distance=$(p.sib_dist)mm,"
end
if p.lev_dist > 0
lev_str = "level distance=$(p.lev_dist)mm,"
end
return sib_str*lev_str
end
function options_str(p::Spring)
if p.dist == -1
return "random seed = $(p.randomSeed),"
else
return "random seed = $(p.randomSeed), node distance=$(p.dist),"
end
end
function options_str(p::SpringElectrical)
if p.dist == -1
return "random seed = $(p.randomSeed), electric charge=$(p.charge),"
else
return "random seed = $(p.randomSeed), electric charge=$(p.charge), node distance=$(p.dist),"
end
end

end # module

0 comments on commit 36f9720

Please sign in to comment.