Skip to content

Commit 3366a89

Browse files
authored
Merge pull request #7 from hdavid16/background_color
Background color
2 parents 935aae9 + 04fa157 commit 3366a89

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ gplot(h)
187187
+ `arrowangleoffset` Angular width in radians for the arrows. Default: `π/9 (20 degrees)`
188188
+ `linetype` Type of line used for edges ("straight", "curve"). Default: "straight"
189189
+ `outangle` Angular width in radians for the edges (only used if `linetype = "curve`). Default: `π/5 (36 degrees)`
190+
+ `background_color` Color for the plot background. Default: `nothing`
190191
+ `plot_size` Tuple of measures for width x height of plot area. Default: `(sqrt(2)*10cm, 10cm)`
191192
+ `leftpad, rightpad, toppad, bottompad` Padding for the plot margins. Default: `0mm`
192193

src/plot.jl

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ Distances for the node labels from center of nodes. Default: `0.0`
5454
Angle offset for the node labels. Default: `π/4.0`
5555
5656
`NODELABELSIZE`
57-
Largest fontsize for the vertice labels. Default: `4.0`
57+
Largest fontsize for the vertex labels. Default: `4.0`
5858
5959
`nodelabelsize`
60-
Relative fontsize for the vertice labels, can be a Vector. Default: `1.0`
60+
Relative fontsize for the vertex labels, can be a Vector. Default: `1.0`
6161
6262
`nodefillc`
6363
Color to fill the nodes with, can be a Vector. Default: `colorant"turquoise"`
@@ -106,6 +106,9 @@ Type of line used for edges ("straight", "curve"). Default: "straight"
106106
Angular width in radians for the edges (only used if `linetype = "curve`).
107107
Default: `π/5 (36 degrees)`
108108
109+
`background_color`
110+
Color for the plot background. Default: `nothing`
111+
109112
`plot_size`
110113
Tuple of measures for width x height for plot area. Default: `(sqrt(2)*10cm, 10cm)`
111114
@@ -142,6 +145,7 @@ function gplot(g::AbstractGraph{T},
142145
arrowangleoffset = π / 9,
143146
linetype = "straight",
144147
outangle = π / 5,
148+
background_color = nothing,
145149
plot_size = (sqrt(2)*10cm, 10cm),
146150
leftpad = 0mm,
147151
rightpad = 0mm,
@@ -196,19 +200,19 @@ function gplot(g::AbstractGraph{T},
196200
# Create nodes
197201
nodecircle = fill(0.4Compose.w, length(locs_x))
198202
if isa(nodesize, Real)
199-
for i = 1:length(locs_x)
200-
nodecircle[i] *= nodesize
201-
end
202-
else
203-
for i = 1:length(locs_x)
204-
nodecircle[i] *= nodesize[i]
205-
end
206-
end
203+
for i = 1:length(locs_x)
204+
nodecircle[i] *= nodesize
205+
end
206+
else
207+
for i = 1:length(locs_x)
208+
nodecircle[i] *= nodesize[i]
209+
end
210+
end
207211
nodes = circle(locs_x, locs_y, nodecircle)
208212

209213
# Create node labels if provided
210214
texts = nothing
211-
if nodelabel != nothing
215+
if !isnothing(nodelabel)
212216
text_locs_x = deepcopy(locs_x)
213217
text_locs_y = deepcopy(locs_y)
214218
texts = text(text_locs_x .+ nodesize .* (nodelabeldist * cos(nodelabelangleoffset)),
@@ -252,16 +256,20 @@ function gplot(g::AbstractGraph{T},
252256
# Fix title offset
253257
title_offset = isempty(title) ? 0 : 0.1*title_size/4
254258

259+
# Plot area size
260+
plot_area = (-1.2, -1.2 - title_offset, +2.4, +2.4 + title_offset)
261+
255262
# Build figure
256-
compose(context(units=UnitBox(-1.2, -1.2 - title_offset, +2.4, +2.4 + title_offset; leftpad, rightpad, toppad, bottompad)),
263+
compose(context(units=UnitBox(plot_area...; leftpad, rightpad, toppad, bottompad)),
257264
compose(context(), text(0, -1.2 - title_offset/2, title, hcenter, vcenter), fill(title_color), fontsize(title_size), font(font_family)),
258265
compose(context(), texts, fill(nodelabelc), fontsize(nodelabelsize), font(font_family)),
259266
compose(context(), nodes, fill(nodefillc), stroke(nodestrokec), linewidth(nodestrokelw)),
260267
compose(context(), edgetexts, fill(edgelabelc), stroke(nothing), fontsize(edgelabelsize)),
261268
compose(context(), larrows, stroke(edgestrokec), linewidth(edgelinewidth)),
262269
compose(context(), carrows, stroke(edgestrokec), linewidth(edgelinewidth)),
263270
compose(context(), lines, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)),
264-
compose(context(), curves, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)))
271+
compose(context(), curves, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)),
272+
compose(context(), rectangle(plot_area...), fill(background_color)))
265273
end
266274

267275
function gplot(g; layout::Function=spring_layout, keyargs...)
71.3 KB
Loading

test/runtests.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ end
7878
plot_and_save3(fname) = plot_and_save(fname, g, nodelabel=nodelabel, nodefillc=nodefillc)
7979
refimg3 = joinpath(datadir, "karate_groups.png")
8080
@test test_images(VisualTest(plot_and_save3, refimg3), popup=!istravis) |> save_comparison |> success
81+
82+
# test background color
83+
plot_and_save4(fname) = plot_and_save(fname, g, background_color=colorant"lightyellow")
84+
refimg4 = joinpath(datadir, "karate_background_color.png")
85+
@test test_images(VisualTest(plot_and_save4, refimg4), popup=!istravis) |> save_comparison |> success
8186
end
8287

8388
@testset "WheelGraph" begin

0 commit comments

Comments
 (0)