I’m trying to create a small interactive plot/ dashboard to showcase different network models during a presentation. My goal is to have a menu in the top left to select a model, a series of sliders below it to adjust the model parameters, and a plotting area on the right. In the MWE below, I select the model Watts-Strogatz
from the dropdown and everything works fine, the SliderGrid updates as it should. However, once I switch back to Erdos–Renyi
, the sliders no longer update visually (even though you can change their values).
Any ideas on how to fix this?
NOTE: I’m only using GLMakie
& Observables
because that’s what my google searches have led me to, but if anybody knows a better approach I’m all ears!
MWE
using Makie, GLMakie, Observables, GraphMakie, Graphs, Graphs.SimpleGraphs
import NetworkLayout
GLMakie.activate!()
function updateNetwork(ax, method, value)
@show method, value
if method == "Erdos–Renyi"
n, p = value
g = erdos_renyi(n, p)
elseif method == "Watts-Strogatz"
n, k, β = value
k = min(n - 1, k)
g = watts_strogatz(n, k, β)
end
empty!(ax)
xlims!(ax, -1.1, 1.1)
ylims!(ax, -1.1, 1.1)
graphplot!(ax, g; layout = NetworkLayout.Shell())
# in a presentation I'd probably hide the axes but for now I keep them fixed.
# Makie.hidedecorations!(ax)
# Makie.hidespines!(ax)
# ax.aspect = Makie.DataAspect()
end
function set_sliders!(fig, m)
@show m
@show contents(fig[2, 1])
!isempty(contents(fig[2, 1])) && delete!(contents(fig[2, 1])[1])
empty!(contents(fig[2, 1]))
if m == "Erdos–Renyi"
sg = SliderGrid(
fig[2, 1],
(label = "n", range = 3:7, format = "{:d}", startvalue = 3),
(label = "p", range = 0:.05:1, format = "{:.2f}", startvalue = .1),
width = 250,
tellheight = false
)
elseif m == "Watts-Strogatz"
sg = SliderGrid(
fig[2, 1],
(label = "n", range = 3:7, format = "{:d}", startvalue = 3),
(label = "k", range = 0:8, format = "{:d}", startvalue = 1),
(label = "β", range = 0:.05:10, format = "{:.2f}", startvalue = .1),
width = 250,
tellheight = false
)
end
return sg
end
fig = Figure(fontsize = 30, resolution=(1000, 1000))
menu = Menu(fig[1, 1],
options = ["Erdos–Renyi", "Watts-Strogatz"],
default = "Erdos–Renyi"
)
slidergrid = map(menu.selection) do m
println("changing slidergrid")
sg = set_sliders!(fig, m)
return sg
end
ax = Axis(fig[1:2, 2])
map(slidergrid) do sg
sliderobservables = [async_latest(s.value) for s in sg.sliders]
map(sliderobservables...) do value...
println("changing plot")
@show value
m = menu.selection[]
updateNetwork(ax, m, value)
end
sliderobservables
end
fig