Skip to content

Commit f1f2760

Browse files
committed
Fix JuliaGraphs#160 make self-loop edges curved
behavior is regardless of the linetype
1 parent 52f4aae commit f1f2760

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

src/GraphPlot.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module GraphPlot
22

33
using Compose # for plotting features
44
using Graphs
5+
using LinearAlgebra
6+
using SparseArrays
57

68
const gadflyjs = joinpath(dirname(Base.source_path()), "gadfly.js")
79

src/lines.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,49 @@ function curveedge(x1, y1, x2, y2, θ, outangle, d; k=0.5)
201201

202202
return [(x1,y1) (xc1, yc1) (xc2, yc2) (x2, y2)]
203203
end
204+
205+
function build_curved_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
206+
if arrowlengthfrac > 0.0
207+
curves_cord, arrows_cord = graphcurve(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
208+
curves = curve(curves_cord[:,1], curves_cord[:,2], curves_cord[:,3], curves_cord[:,4])
209+
carrows = line(arrows_cord)
210+
else
211+
curves_cord = graphcurve(g, locs_x, locs_y, nodesize, outangle)
212+
curves = curve(curves_cord[:,1], curves_cord[:,2], curves_cord[:,3], curves_cord[:,4])
213+
carrows = nothing
214+
end
215+
216+
return curves, carrows
217+
end
218+
219+
function build_straight_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
220+
if arrowlengthfrac > 0.0
221+
lines_cord, arrows_cord = graphline(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
222+
lines = line(lines_cord)
223+
larrows = line(arrows_cord)
224+
else
225+
lines_cord = graphline(g, locs_x, locs_y, nodesize)
226+
lines = line(lines_cord)
227+
larrows = nothing
228+
end
229+
230+
return lines, larrows
231+
end
232+
233+
function build_straight_curved_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
234+
A = adjacency_matrix(g) #adjacency matrix
235+
B = spdiagm(diag(A)) #diagonal matrix (self-loops)
236+
A[diagind(A)] .= 0 #set diagonal elements to 0 (remove self-loops)
237+
if is_directed(g)
238+
g1 = SimpleDiGraph(A)
239+
g2 = SimpleDiGraph(B)
240+
else
241+
g1 = SimpleGraph(A)
242+
g2 = SimpleGraph(B)
243+
end
244+
245+
lines, larrows = build_straight_edges(g1, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
246+
curves, carrows = build_curved_edges(g2, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
247+
248+
return lines, larrows, curves, carrows
249+
end

src/plot.jl

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -206,33 +206,24 @@ function gplot(g::AbstractGraph{T},
206206
end
207207

208208
# Create lines and arrow heads
209-
lines, arrows = nothing, nothing
209+
lines, larrows = nothing, nothing
210+
curves, carrows = nothing, nothing
210211
if linetype == "curve"
211-
if arrowlengthfrac > 0.0
212-
curves_cord, arrows_cord = graphcurve(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
213-
lines = curve(curves_cord[:,1], curves_cord[:,2], curves_cord[:,3], curves_cord[:,4])
214-
arrows = line(arrows_cord)
215-
else
216-
curves_cord = graphcurve(g, locs_x, locs_y, nodesize, outangle)
217-
lines = curve(curves_cord[:,1], curves_cord[:,2], curves_cord[:,3], curves_cord[:,4])
218-
end
212+
curves, carrows = build_curved_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
213+
elseif has_self_loops(g)
214+
lines, larrows, curves, carrows = build_straight_curved_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
219215
else
220-
if arrowlengthfrac > 0.0
221-
lines_cord, arrows_cord = graphline(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
222-
lines = line(lines_cord)
223-
arrows = line(arrows_cord)
224-
else
225-
lines_cord = graphline(g, locs_x, locs_y, nodesize)
226-
lines = line(lines_cord)
227-
end
216+
lines, larrows = build_straight_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
228217
end
229218

230219
compose(context(units=UnitBox(-1.2, -1.2, +2.4, +2.4)),
231220
compose(context(), texts, fill(nodelabelc), stroke(nothing), fontsize(nodelabelsize)),
232221
compose(context(), nodes, fill(nodefillc), stroke(nodestrokec), linewidth(nodestrokelw)),
233222
compose(context(), edgetexts, fill(edgelabelc), stroke(nothing), fontsize(edgelabelsize)),
234-
compose(context(), arrows, stroke(edgestrokec), linewidth(edgelinewidth)),
235-
compose(context(), lines, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)))
223+
compose(context(), larrows, stroke(edgestrokec), linewidth(edgelinewidth)),
224+
compose(context(), carrows, stroke(edgestrokec), linewidth(edgelinewidth)),
225+
compose(context(), lines, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)),
226+
compose(context(), curves, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)))
236227
end
237228

238229
function gplot(g; layout::Function=spring_layout, keyargs...)

0 commit comments

Comments
 (0)