Skip to content

Commit d610f15

Browse files
committed
update arg syntax for gplot: (address JuliaGraphs#177)
-breaking change: linetype changed from String to Symbol -breaking change: default for edgelabel is nothing (not []) to make consistent with nodelabel default -remove all caps args -improve some of the code in gplot (use isnothing, etc.)
1 parent 14d62b4 commit d610f15

File tree

3 files changed

+35
-59
lines changed

3 files changed

+35
-59
lines changed

README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ gplot(g, locs_x, locs_y, nodelabel=nodelabel)
129129

130130
## Curve edge
131131
```julia
132-
gplot(g, linetype="curve")
132+
gplot(g, linetype=:curve)
133133
```
134134

135135
## Show plot
@@ -160,28 +160,28 @@ gplot(h)
160160

161161
# Keyword Arguments
162162
+ `layout` Layout algorithm: `random_layout`, `circular_layout`, `spring_layout`, `shell_layout`, `stressmajorize_layout`, `spectral_layout`. Default: `spring_layout`
163-
+ `NODESIZE` Max size for the nodes. Default: `3.0/sqrt(N)`
163+
+ `max_nodesize` Max size for the nodes. Default: `3.0/sqrt(N)`
164164
+ `nodesize` Relative size for the nodes, can be a Vector. Default: `1.0`
165165
+ `nodelabel` Labels for the vertices, a Vector or nothing. Default: `nothing`
166166
+ `nodelabelc` Color for the node labels, can be a Vector. Default: `colorant"black"`
167167
+ `nodelabeldist` Distances for the node labels from center of nodes. Default: `0.0`
168168
+ `nodelabelangleoffset` Angle offset for the node labels. Default: `π/4.0`
169-
+ `NODELABELSIZE` Largest fontsize for the vertice labels. Default: `4.0`
169+
+ `max_nodelabelsize` Largest fontsize for the vertice labels. Default: `4.0`
170170
+ `nodelabelsize` Relative fontsize for the vertice labels, can be a Vector. Default: `1.0`
171171
+ `nodefillc` Color to fill the nodes with, can be a Vector. Default: `colorant"turquoise"`
172172
+ `nodestrokec` Color for the nodes stroke, can be a Vector. Default: `nothing`
173173
+ `nodestrokelw` Line width for the nodes stroke, can be a Vector. Default: `0.0`
174-
+ `edgelabel` Labels for the edges, a Vector or nothing. Default: `[]`
174+
+ `edgelabel` Labels for the edges, a Vector or nothing. Default: `nothing`
175175
+ `edgelabelc` Color for the edge labels, can be a Vector. Default: `colorant"black"`
176176
+ `edgelabeldistx, edgelabeldisty` Distance for the edge label from center of edge. Default: `0.0`
177-
+ `EDGELABELSIZE` Largest fontsize for the edge labels. Default: `4.0`
177+
+ `max_edgelabelsize` Largest fontsize for the edge labels. Default: `4.0`
178178
+ `edgelabelsize` Relative fontsize for the edge labels, can be a Vector. Default: `1.0`
179-
+ `EDGELINEWIDTH` Max line width for the edges. Default: `0.25/sqrt(N)`
179+
+ `max_edgelinewidth` Max line width for the edges. Default: `0.25/sqrt(N)`
180180
+ `edgelinewidth` Relative line width for the edges, can be a Vector. Default: `1.0`
181181
+ `edgestrokec` Color for the edge strokes, can be a Vector. Default: `colorant"lightgray"`
182182
+ `arrowlengthfrac` Fraction of line length to use for arrows. Equal to 0 for undirected graphs. Default: `0.1` for the directed graphs
183183
+ `arrowangleoffset` Angular width in radians for the arrows. Default: `π/9 (20 degrees)`
184-
+ `linetype` Type of line used for edges ("straight", "curve"). Default: "straight"
184+
+ `linetype` Type of line used for edges (:straight, :curve). Default: :straight
185185
+ `outangle` Angular width in radians for the edges (only used if `linetype = "curve`). Default: `π/5 (36 degrees)`
186186
+ `backgroundc` Color for the plot background. Default: `nothing`
187187

src/plot.jl

+26-50
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Layout algorithm. Currently can be one of [`random_layout`,
2323
`spectral_layout`].
2424
Default: `spring_layout`
2525
26-
`NODESIZE`
26+
`max_nodesize`
2727
Max size for the nodes. Default: `3.0/sqrt(N)`
2828
2929
`nodesize`
@@ -41,7 +41,7 @@ Distances for the node labels from center of nodes. Default: `0.0`
4141
`nodelabelangleoffset`
4242
Angle offset for the node labels. Default: `π/4.0`
4343
44-
`NODELABELSIZE`
44+
`max_nodelabelsize`
4545
Largest fontsize for the vertex labels. Default: `4.0`
4646
4747
`nodelabelsize`
@@ -57,21 +57,21 @@ Color for the nodes stroke, can be a Vector. Default: `nothing`
5757
Line width for the nodes stroke, can be a Vector. Default: `0.0`
5858
5959
`edgelabel`
60-
Labels for the edges, a Vector or nothing. Default: `[]`
60+
Labels for the edges, a Vector or nothing. Default: `nothing`
6161
6262
`edgelabelc`
6363
Color for the edge labels, can be a Vector. Default: `colorant"black"`
6464
6565
`edgelabeldistx, edgelabeldisty`
6666
Distance for the edge label from center of edge. Default: `0.0`
6767
68-
`EDGELABELSIZE`
68+
`max_edgelabelsize`
6969
Largest fontsize for the edge labels. Default: `4.0`
7070
7171
`edgelabelsize`
7272
Relative fontsize for the edge labels, can be a Vector. Default: `1.0`
7373
74-
`EDGELINEWIDTH`
74+
`max_edgelinewidth`
7575
Max line width for the edges. Default: `0.25/sqrt(N)`
7676
7777
`edgelinewidth`
@@ -88,7 +88,7 @@ Equal to 0 for undirected graphs. Default: `0.1` for the directed graphs
8888
Angular width in radians for the arrows. Default: `π/9 (20 degrees)`
8989
9090
`linetype`
91-
Type of line used for edges ("straight", "curve"). Default: "straight"
91+
Type of line used for edges (:straight, :curve). Default: :straight
9292
9393
`outangle`
9494
Angular width in radians for the edges (only used if `linetype = "curve`).
@@ -103,38 +103,32 @@ function gplot(g::AbstractGraph{T},
103103
nodelabel = nothing,
104104
nodelabelc = colorant"black",
105105
nodelabelsize = 1.0,
106-
NODELABELSIZE = 4.0,
106+
max_nodelabelsize = 4.0,
107107
nodelabeldist = 0.0,
108108
nodelabelangleoffset = π / 4.0,
109-
edgelabel = [],
109+
edgelabel = nothing,
110110
edgelabelc = colorant"black",
111111
edgelabelsize = 1.0,
112-
EDGELABELSIZE = 4.0,
112+
max_edgelabelsize = 4.0,
113113
edgestrokec = colorant"lightgray",
114114
edgelinewidth = 1.0,
115-
EDGELINEWIDTH = 3.0 / sqrt(nv(g)),
115+
max_edgelinewidth = 3.0 / sqrt(nv(g)),
116116
edgelabeldistx = 0.0,
117117
edgelabeldisty = 0.0,
118118
nodesize = 1.0,
119-
NODESIZE = 0.25 / sqrt(nv(g)),
119+
max_nodesize = 0.25 / sqrt(nv(g)),
120120
nodefillc = colorant"turquoise",
121121
nodestrokec = nothing,
122122
nodestrokelw = 0.0,
123123
arrowlengthfrac = is_directed(g) ? 0.1 : 0.0,
124124
arrowangleoffset = π / 9,
125-
linetype = "straight",
125+
linetype = :straight,
126126
outangle = π / 5,
127127
backgroundc = nothing) where {T <:Integer, R1 <: Real, R2 <: Real}
128128

129-
length(locs_x_in) != length(locs_y_in) && error("Vectors must be same length")
130-
N = nv(g)
131-
NE = ne(g)
132-
if nodelabel != nothing && length(nodelabel) != N
133-
error("Must have one label per node (or none)")
134-
end
135-
if !isempty(edgelabel) && length(edgelabel) != NE
136-
error("Must have one label per edge (or none)")
137-
end
129+
@assert length(locs_x_in) == length(locs_y_in) == nv(g) "Position vectors must be of the same length as the number of nodes"
130+
@assert isnothing(nodelabel) || length(nodelabel) == nv(g) "`nodelabel` must either be `nothing` or a vector of the same length as the number of nodes"
131+
@assert isnothing(edgelabel) || length(edgelabel) == ne(g) "`edgelabel` must either be `nothing` or a vector of the same length as the number of edges"
138132

139133
locs_x = Float64.(locs_x_in)
140134
locs_y = Float64.(locs_y_in)
@@ -152,35 +146,18 @@ function gplot(g::AbstractGraph{T},
152146
map!(z -> scaler(z, min_x, max_x), locs_x, locs_x)
153147
map!(z -> scaler(z, min_y, max_y), locs_y, locs_y)
154148

155-
# Determine sizes
156-
#NODESIZE = 0.25/sqrt(N)
157-
#LINEWIDTH = 3.0/sqrt(N)
158-
159-
max_nodesize = NODESIZE / maximum(nodesize)
160-
nodesize *= max_nodesize
161-
max_edgelinewidth = EDGELINEWIDTH / maximum(edgelinewidth)
162-
edgelinewidth *= max_edgelinewidth
163-
max_edgelabelsize = EDGELABELSIZE / maximum(edgelabelsize)
164-
edgelabelsize *= max_edgelabelsize
165-
max_nodelabelsize = NODELABELSIZE / maximum(nodelabelsize)
166-
nodelabelsize *= max_nodelabelsize
149+
# Scale sizes
150+
nodesize *= (max_nodesize / maximum(nodesize))
151+
edgelinewidth *= (max_edgelinewidth / maximum(edgelinewidth))
152+
edgelabelsize *= (max_edgelabelsize / maximum(edgelabelsize))
153+
nodelabelsize *= (max_nodelabelsize / maximum(nodelabelsize))
167154
max_nodestrokelw = maximum(nodestrokelw)
168155
if max_nodestrokelw > 0.0
169-
max_nodestrokelw = EDGELINEWIDTH / max_nodestrokelw
170-
nodestrokelw *= max_nodestrokelw
156+
nodestrokelw *= (max_edgelinewidth / max_nodestrokelw)
171157
end
172158

173159
# Create nodes
174-
nodecircle = fill(0.4Compose.w, length(locs_x))
175-
if isa(nodesize, Real)
176-
for i = 1:length(locs_x)
177-
nodecircle[i] *= nodesize
178-
end
179-
else
180-
for i = 1:length(locs_x)
181-
nodecircle[i] *= nodesize[i]
182-
end
183-
end
160+
nodecircle = fill(0.4Compose.w, nv(g)) .* nodesize
184161
nodes = circle(locs_x, locs_y, nodecircle)
185162

186163
# Create node labels if provided
@@ -194,24 +171,23 @@ function gplot(g::AbstractGraph{T},
194171
end
195172
# Create edge labels if provided
196173
edgetexts = nothing
197-
if !isempty(edgelabel)
174+
if !isnothing(edgelabel)
198175
edge_locs_x = zeros(R, NE)
199176
edge_locs_y = zeros(R, NE)
200177
for (e_idx, e) in enumerate(edges(g))
201178
i = src(e)
202179
j = dst(e)
203180
mid_x = (locs_x[i]+locs_x[j]) / 2.0
204181
mid_y = (locs_y[i]+locs_y[j]) / 2.0
205-
edge_locs_x[e_idx] = (is_directed(g) ? (mid_x+locs_x[j]) / 2.0 : mid_x) + edgelabeldistx * NODESIZE
206-
edge_locs_y[e_idx] = (is_directed(g) ? (mid_y+locs_y[j]) / 2.0 : mid_y) + edgelabeldisty * NODESIZE
207-
182+
edge_locs_x[e_idx] = (is_directed(g) ? (mid_x+locs_x[j]) / 2.0 : mid_x) + edgelabeldistx * max_nodesize
183+
edge_locs_y[e_idx] = (is_directed(g) ? (mid_y+locs_y[j]) / 2.0 : mid_y) + edgelabeldisty * max_nodesize
208184
end
209185
edgetexts = text(edge_locs_x, edge_locs_y, map(string, edgelabel), [hcenter], [vcenter])
210186
end
211187

212188
# Create lines and arrow heads
213189
lines, arrows = nothing, nothing
214-
if linetype == "curve"
190+
if linetype == :curve
215191
if arrowlengthfrac > 0.0
216192
curves_cord, arrows_cord = graphcurve(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
217193
lines = curve(curves_cord[:,1], curves_cord[:,2], curves_cord[:,3], curves_cord[:,4])

test/runtests.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ end
9393
add_edge!(g2, 1,2)
9494
add_edge!(g2, 2,1)
9595

96-
plot_and_save1(fname) = plot_and_save(fname, g2, linetype="curve")
96+
plot_and_save1(fname) = plot_and_save(fname, g2, linetype=:curve)
9797
refimg1 = joinpath(datadir, "curve.png")
9898
@test test_images(VisualTest(plot_and_save1, refimg1), popup=!istravis) |> save_comparison |> success
9999

@@ -102,7 +102,7 @@ end
102102
add_edge!(g3, 1,2)
103103
add_edge!(g3, 2,1)
104104

105-
plot_and_save2(fname) = plot_and_save(fname, g3, linetype="curve")
105+
plot_and_save2(fname) = plot_and_save(fname, g3, linetype=:curve)
106106
refimg2 = joinpath(datadir, "self_directed.png")
107107
@test test_images(VisualTest(plot_and_save2, refimg2), popup=!istravis) |> save_comparison |> success
108108

0 commit comments

Comments
 (0)