Skip to content

Edge label middle #8

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

Merged
merged 4 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/lines.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ function midpoint(pt1,pt2)
return x,y
end

function interpolate_bezier(x::Vector,t)
n = length(x)-1
x_loc = sum(binomial(n,i)*(1-t)^(n-i)*t^i*x[i+1][1] for i in 0:n)
y_loc = sum(binomial(n,i)*(1-t)^(n-i)*t^i*x[i+1][2] for i in 0:n)
return x_loc.value, y_loc.value
end

interpolate_bezier(x::Compose.CurvePrimitive,t) =
interpolate_bezier([x.anchor0, x.ctrl0, x.ctrl1, x.anchor1], t)

function interpolate_line(locs_x,locs_y,i,j,t)
x_loc = locs_x[i] + (locs_x[j]-locs_x[i])*t
y_loc = locs_y[i] + (locs_y[j]-locs_y[i])*t
return x_loc, y_loc
end

function graphline(edge_list, locs_x, locs_y, nodesize::Vector{T}, arrowlength, angleoffset) where {T<:Real}
num_edges = length(edge_list)
lines = Array{Vector{Tuple{Float64,Float64}}}(undef, num_edges)
Expand Down
38 changes: 22 additions & 16 deletions src/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,22 +223,6 @@ function gplot(g::AbstractGraph{T},
text_locs_y .- nodesize .* (nodelabeldist * sin(nodelabelangleoffset)),
map(string, nodelabel), [hcenter], [vcenter])
end
# Create edge labels if provided
edgetexts = nothing
if !isempty(edgelabel)
edge_locs_x = zeros(R1, NE)
edge_locs_y = zeros(R2, NE)
for (e_idx, e) in enumerate(edges(g))
i = src(e)
j = dst(e)
mid_x = (locs_x[i]+locs_x[j]) / 2.0
mid_y = (locs_y[i]+locs_y[j]) / 2.0
edge_locs_x[e_idx] = (is_directed(g) ? (mid_x+locs_x[j]) / 2.0 : mid_x) + edgelabeldistx * NODESIZE
edge_locs_y[e_idx] = (is_directed(g) ? (mid_y+locs_y[j]) / 2.0 : mid_y) + edgelabeldisty * NODESIZE

end
edgetexts = text(edge_locs_x, edge_locs_y, map(string, edgelabel), [hcenter], [vcenter])
end

# Create lines and arrow heads
lines, larrows = nothing, nothing
Expand All @@ -251,6 +235,28 @@ function gplot(g::AbstractGraph{T},
lines, larrows = build_straight_edges(edges(g), locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
end

# Create edge labels if provided
edgetexts = nothing
if !isempty(edgelabel)
edge_locs_x = zeros(R1, NE)
edge_locs_y = zeros(R2, NE)
self_loop_idx = 1
for (e_idx, e) in enumerate(edges(g))
i, j = src(e), dst(e)
if linetype == "curve"
mid_x, mid_y = interpolate_bezier(curves.primitives[e_idx], 0.5)
elseif src(e) == dst(e)
mid_x, mid_y = interpolate_bezier(curves.primitives[self_loop_idx], 0.5)
self_loop_idx += 1
else
mid_x, mid_y = interpolate_line(locs_x,locs_y,i,j,0.5)
end
edge_locs_x[e_idx] = mid_x + edgelabeldistx * NODESIZE
edge_locs_y[e_idx] = mid_y + edgelabeldisty * NODESIZE
end
edgetexts = text(edge_locs_x, edge_locs_y, map(string, edgelabel), [hcenter], [vcenter])
end

# Set plot_size
if length(plot_size) != 2 || !isa(plot_size[1], Compose.AbsoluteLength) || !isa(plot_size[2], Compose.AbsoluteLength)
error("`plot_size` must be a Tuple of lengths")
Expand Down