Skip to content
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

(Default)Dict for some attributes #89

Merged
merged 9 commits into from
Jun 20, 2023
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Simon Danisch", "Hans Würfel"]
version = "0.5.3"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
1 change: 1 addition & 0 deletions src/GraphMakie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Makie
using LinearAlgebra

import Makie: DocThemer, ATTRIBUTES, project, automatic
import DataStructures: DefaultDict, DefaultOrderedDict

include("beziercurves.jl")
include("recipes.jl")
Expand Down
23 changes: 19 additions & 4 deletions src/recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ end

function Makie.plot!(gp::GraphPlot)
graph = gp[:graph]

dfth = default_theme(gp.parent, GraphPlot)

# create initial vertex positions, will be updated on changes to graph or layout
# make node_position-Observable available as named attribute from the outside
Expand Down Expand Up @@ -245,11 +247,13 @@ function Makie.plot!(gp::GraphPlot)
visible = arrow_show,
gp.arrow_attr...)

nodesize = prep_arguments(gp.node_size, @lift(length(vertices($graph))), dfth.node_size)
filchristou marked this conversation as resolved.
Show resolved Hide resolved

# plot vertices
vertex_plot = scatter!(gp, node_pos;
color=gp.node_color,
marker=gp.node_marker,
markersize=gp.node_size,
markersize=nodesize,
gp.node_attr...)

# plot node labels
Expand All @@ -268,12 +272,14 @@ function Makie.plot!(gp::GraphPlot)
$(gp.nlabels_distance) .* align_to_dir($(gp.nlabels_align))
end

nlabelsfontsize = prep_arguments(gp.nlabels_fontsize, @lift(length(vertices($graph))), dfth.nlabels_fontsize)
filchristou marked this conversation as resolved.
Show resolved Hide resolved

nlabels_plot = text!(gp, positions;
text=gp.nlabels,
align=gp.nlabels_align,
color=gp.nlabels_color,
offset=offset,
fontsize=gp.nlabels_fontsize,
fontsize=nlabelsfontsize,
gp.nlabels_attr...)
end

Expand Down Expand Up @@ -317,7 +323,6 @@ function Makie.plot!(gp::GraphPlot)
offsets = map(p -> Point(-p.data[2], p.data[1])/norm(p), tangent_px)
offsets .= elabels_distance_offset(graph[], gp.attributes) .* offsets
end

elabels_plot = text!(gp, positions;
text=gp.elabels,
rotation=rotation,
Expand All @@ -331,6 +336,16 @@ function Makie.plot!(gp::GraphPlot)
return gp
end

function prep_arguments(o::Observable, size::Observable, default::Observable)
filchristou marked this conversation as resolved.
Show resolved Hide resolved
@lift begin
if isscalar($o)
isnothing($o) ? $default : $o
else
[getattr(o, i, $default) for i in 1:$size]
filchristou marked this conversation as resolved.
Show resolved Hide resolved
end
end
end

filchristou marked this conversation as resolved.
Show resolved Hide resolved
"""
elabels_distance_offset(g, attrs)

Expand Down Expand Up @@ -711,4 +726,4 @@ function update_arrow_shift(g, gp, edge_paths::Vector{<:AbstractPath{<:Point3}},
end

return arrow_shift
end
end
11 changes: 10 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,22 @@ Else return the one and only element.
function getattr(o::Observable, idx, default=nothing)
if o[] isa AbstractVector && !isa(o[], Point)
return o[][idx]
elseif o[] isa DefaultDict || o[] isa DefaultOrderedDict
return getindex(o[], idx)
elseif o[] isa AbstractDict
return get(o[], idx, default)
hexaeder marked this conversation as resolved.
Show resolved Hide resolved
else
return o[] === nothing ? default : o[]
end
end

"""
isscalar(o::Observable)

Return `true` if `Observable` is not an `AbstractVector` or an `AbstractDict`
"""
isscalar(o) = !isa(o, AbstractVector) && !isa(o, AbstractDict)
filchristou marked this conversation as resolved.
Show resolved Hide resolved

"""
Pointf(p::Point{N, T})

Expand Down Expand Up @@ -182,4 +191,4 @@ function point_near_dst(edge_path, p0::PT, d, to_px) where {PT}
p1 = p0 - d*normalize(r)*scale_px

return p1
end
end