Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting a background color for a gplot (included in PR #186) #178

Closed
wants to merge 11 commits into from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 18 additions & 13 deletions src/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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};
Expand All @@ -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)
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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...)
Expand Down
Binary file added test/data/karate_background_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down