diff --git a/README.md b/README.md index e5dd530..897efcf 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,7 @@ gplot(h) + `arrowangleoffset` Angular width in radians for the arrows. Default: `π/9 (20 degrees)` + `linetype` Type of line used for edges ("straight", "curve"). Default: "straight" + `outangle` Angular width in radians for the edges (only used if `linetype = "curve`). Default: `π/5 (36 degrees)` ++ `background_color` Color for the plot background. Default: `nothing` # Reporting Bugs diff --git a/src/plot.jl b/src/plot.jl index af438b7..2f710f2 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -42,10 +42,10 @@ Distances for the node labels from center of nodes. Default: `0.0` Angle offset for the node labels. Default: `π/4.0` `NODELABELSIZE` -Largest fontsize for the vertice labels. Default: `4.0` +Largest fontsize for the vertex labels. Default: `4.0` `nodelabelsize` -Relative fontsize for the vertice labels, can be a Vector. Default: `1.0` +Relative fontsize for the vertex labels, can be a Vector. Default: `1.0` `nodefillc` Color to fill the nodes with, can be a Vector. Default: `colorant"turquoise"` @@ -94,6 +94,9 @@ Type of line used for edges ("straight", "curve"). Default: "straight" Angular width in radians for the edges (only used if `linetype = "curve`). Default: `π/5 (36 degrees)` +`background_color` +Color for the plot background. Default: `nothing` + """ function gplot(g::AbstractGraph{T}, locs_x_in::Vector{R1}, locs_y_in::Vector{R2}; @@ -120,7 +123,8 @@ function gplot(g::AbstractGraph{T}, arrowlengthfrac = is_directed(g) ? 0.1 : 0.0, arrowangleoffset = π / 9, linetype = "straight", - outangle = π / 5) where {T <:Integer, R1 <: Real, R2 <: Real} + outangle = π / 5, + background_color = nothing) where {T <:Integer, R1 <: Real, R2 <: Real} length(locs_x_in) != length(locs_y_in) && error("Vectors must be same length") N = nv(g) @@ -169,19 +173,19 @@ function gplot(g::AbstractGraph{T}, # Create nodes nodecircle = fill(0.4Compose.w, length(locs_x)) if isa(nodesize, Real) - for i = 1:length(locs_x) - nodecircle[i] *= nodesize - end - else - for i = 1:length(locs_x) - nodecircle[i] *= nodesize[i] - end - end + for i = 1:length(locs_x) + nodecircle[i] *= nodesize + end + else + for i = 1:length(locs_x) + nodecircle[i] *= nodesize[i] + end + end nodes = circle(locs_x, locs_y, nodecircle) # Create node labels if provided texts = nothing - if nodelabel != nothing + if !isnothing(nodelabel) text_locs_x = deepcopy(locs_x) text_locs_y = deepcopy(locs_y) texts = text(text_locs_x .+ nodesize .* (nodelabeldist * cos(nodelabelangleoffset)), @@ -232,7 +236,8 @@ function gplot(g::AbstractGraph{T}, compose(context(), nodes, fill(nodefillc), stroke(nodestrokec), linewidth(nodestrokelw)), compose(context(), edgetexts, fill(edgelabelc), stroke(nothing), fontsize(edgelabelsize)), compose(context(), arrows, stroke(edgestrokec), linewidth(edgelinewidth)), - compose(context(), lines, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth))) + compose(context(), lines, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)), + compose(context(), rectangle(-1.2, -1.2, +2.4, +2.4), fill(background_color))) end function gplot(g; layout::Function=spring_layout, keyargs...) diff --git a/test/data/karate_background_color.png b/test/data/karate_background_color.png new file mode 100644 index 0000000..430e108 Binary files /dev/null and b/test/data/karate_background_color.png differ diff --git a/test/runtests.jl b/test/runtests.jl index f38f8d6..b9aaa65 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -78,6 +78,11 @@ end plot_and_save3(fname) = plot_and_save(fname, g, nodelabel=nodelabel, nodefillc=nodefillc) refimg3 = joinpath(datadir, "karate_groups.png") @test test_images(VisualTest(plot_and_save3, refimg3), popup=!istravis) |> save_comparison |> success + + # test background color + plot_and_save4(fname) = plot_and_save(fname, g, background_color=colorant"lightyellow") + refimg4 = joinpath(datadir, "karate_background_color.png") + @test test_images(VisualTest(plot_and_save4, refimg4), popup=!istravis) |> save_comparison |> success end @testset "WheelGraph" begin