From 47f99388aedb80198747d99d969af1d7ebaa29d3 Mon Sep 17 00:00:00 2001 From: Brett Israelsen Date: Fri, 31 Aug 2018 14:44:16 -0600 Subject: [PATCH] added ability to append to preamble.tex, added SpringElectrical layout, added some options to other layouts --- src/TikzGraphs.jl | 49 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/src/TikzGraphs.jl b/src/TikzGraphs.jl index 579458e..2c9ea4a 100644 --- a/src/TikzGraphs.jl +++ b/src/TikzGraphs.jl @@ -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 @@ -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) @@ -70,13 +82,14 @@ 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 @@ -84,6 +97,30 @@ for (_layout, _libraryname, _layoutname) in [ 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