Skip to content

Commit 4c16357

Browse files
authored
Merge pull request #4 from hdavid16/self-loops-curve
Fix JuliaGraphs#160 make self-loop edges curved
2 parents bbdc0d3 + d2e7f22 commit 4c16357

File tree

3 files changed

+59
-20
lines changed

3 files changed

+59
-20
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: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -222,35 +222,26 @@ function gplot(g::AbstractGraph{T},
222222
end
223223

224224
# Create lines and arrow heads
225-
lines, arrows = nothing, nothing
225+
lines, larrows = nothing, nothing
226+
curves, carrows = nothing, nothing
226227
if linetype == "curve"
227-
if arrowlengthfrac > 0.0
228-
curves_cord, arrows_cord = graphcurve(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
229-
lines = curve(curves_cord[:,1], curves_cord[:,2], curves_cord[:,3], curves_cord[:,4])
230-
arrows = line(arrows_cord)
231-
else
232-
curves_cord = graphcurve(g, locs_x, locs_y, nodesize, outangle)
233-
lines = curve(curves_cord[:,1], curves_cord[:,2], curves_cord[:,3], curves_cord[:,4])
234-
end
228+
curves, carrows = build_curved_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
229+
elseif has_self_loops(g)
230+
lines, larrows, curves, carrows = build_straight_curved_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, outangle)
235231
else
236-
if arrowlengthfrac > 0.0
237-
lines_cord, arrows_cord = graphline(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
238-
lines = line(lines_cord)
239-
arrows = line(arrows_cord)
240-
else
241-
lines_cord = graphline(g, locs_x, locs_y, nodesize)
242-
lines = line(lines_cord)
243-
end
232+
lines, larrows = build_straight_edges(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
244233
end
245234

246235
title_offset = isempty(title) ? 0 : 0.1*title_size/4
247236
compose(context(units=UnitBox(-1.2, -1.2 - title_offset, +2.4, +2.4 + title_offset)),
248237
compose(context(), text(0, -1.2 - title_offset/2, title, hcenter, vcenter), fill(title_color), fontsize(title_size), font(font_family)),
249238
compose(context(), texts, fill(nodelabelc), fontsize(nodelabelsize), font(font_family)),
250239
compose(context(), nodes, fill(nodefillc), stroke(nodestrokec), linewidth(nodestrokelw)),
251-
compose(context(), edgetexts, fill(edgelabelc), fontsize(edgelabelsize), font(font_family)),
252-
compose(context(), arrows, stroke(edgestrokec), linewidth(edgelinewidth)),
253-
compose(context(), lines, stroke(edgestrokec), linewidth(edgelinewidth)))
240+
compose(context(), edgetexts, fill(edgelabelc), stroke(nothing), fontsize(edgelabelsize)),
241+
compose(context(), larrows, stroke(edgestrokec), linewidth(edgelinewidth)),
242+
compose(context(), carrows, stroke(edgestrokec), linewidth(edgelinewidth)),
243+
compose(context(), lines, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)),
244+
compose(context(), curves, stroke(edgestrokec), fill(nothing), linewidth(edgelinewidth)))
254245
end
255246

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

0 commit comments

Comments
 (0)