Skip to content

Commit ad341f9

Browse files
committed
got working version
1 parent 01fd71d commit ad341f9

File tree

4 files changed

+56
-57
lines changed

4 files changed

+56
-57
lines changed

NetworkDynamicsInspector/src/NetworkDynamicsInspector.jl

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ include("utils.jl")
2222
function apptheme()
2323
Theme(
2424
fontsize=10,
25+
palette = (;
26+
linestyle = [:solid, :dot, :dash, :dashdot, :dashdotdot],
27+
),
28+
Lines = (;
29+
cycle = Cycle([:color, :linestyle], covary=true)
30+
)
2531
)
2632
end
2733

@@ -60,7 +66,7 @@ function graphplot_card(app; kwargs...)
6066
end;
6167

6268
edge_state = Observable(Vector{Float32}(undef, NE))
63-
onany(app.sol, app.t, app.graphplot.estate, app.graphplot.nstate_rel) do _sol, _t, _state, _rel
69+
onany(app.sol, app.t, app.graphplot.estate, app.graphplot.estate_rel) do _sol, _t, _state, _rel
6470
@debug "GP: app.sol, app.t, app.gp.estate, app.gp.estate_rel => edge_state"
6571
if length(_state) == 0
6672
fill!(edge_state[], NaN)
@@ -229,10 +235,12 @@ function gpstate_control_card(app, type)
229235
nothing
230236
end
231237
multisel = MultiSelect(options, stateobs; placeholder="Select state for coloring", multi=false, T=Symbol)
238+
reltoggle = ToggleSwitch(value=stateobs_rel, label="Rel to u0")
232239
selector = Grid(
233240
DOM.span(label),
234-
multisel;
235-
columns = "70px 1fr",
241+
multisel,
242+
reltoggle;
243+
columns = "70px 1fr auto",
236244
align_items = "center"
237245
)
238246

@@ -345,12 +353,17 @@ function timeseries_card(app, session)
345353
multi=true,
346354
option_to_string=_sidx_to_str,
347355
T=SymbolicIndex)
348-
comp_sel_dom = Grid(DOM.span("States"), comp_sel; columns = "70px 1fr", align_items = "center")
356+
# comp_sel_dom = Grid(DOM.span("Components"), comp_sel; columns = "70px 1fr", align_items = "center")
349357
state_sel = MultiSelect(state_options, app.tsplot.states;
350358
placeholder="Select states",
351359
multi=true,
352360
T=Symbol)
353-
state_sel_dom = Grid(DOM.span("Components"), state_sel; columns = "70px 1fr", align_items = "center")
361+
362+
comp_state_sel_dom = Grid(
363+
DOM.span("Components"), comp_sel,
364+
DOM.span("States"), state_sel;
365+
columns = "auto 1fr", align_items = "center"
366+
)
354367

355368
# hl choice of elements in graphplot
356369
on(app.tsplot.selcomp; update=true) do _sel
@@ -366,7 +379,7 @@ function timeseries_card(app, session)
366379
####
367380
color_cache = Dict{Union{EIndex{Int,Nothing},VIndex{Int,Nothing}}, Int}()
368381
linestyle_cache = Dict{Symbol,Int}()
369-
on(app.tsplot.selcomp) do _sel
382+
on(app.tsplot.selcomp; update=true) do _sel
370383
@debug "TS: comp selection => update color_cache"
371384
for unused in setdiff(keys(color_cache), _sel)
372385
delete!(color_cache, unused)
@@ -377,7 +390,7 @@ function timeseries_card(app, session)
377390
end
378391
nothing
379392
end
380-
on(app.tsplot.states) do _states
393+
on(app.tsplot.states; update=true) do _states
381394
@debug "TS: state selection => update linestyle_cache"
382395
for unused in setdiff(keys(linestyle_cache), _states)
383396
delete!(linestyle_cache, unused)
@@ -388,20 +401,13 @@ function timeseries_card(app, session)
388401
end
389402
nothing
390403
end
391-
getcolor = (s) -> begin
392-
key = s isa VIndex ? VIndex(s.compidx) : EIndex(s.compidx)
393-
Cycled(color_cache[key])
394-
Cycled(ckey)
395-
end
396-
getlinestyle = (s) -> Cycled(linestyle_cache[s.subidx])
397404

398405

399406
fig, ax = with_theme(apptheme()) do
400407
fig = Figure()
401408
ax = Axis(fig[1, 1])
402409
fig, ax
403410
end
404-
Main.axref[] = ax
405411

406412
# set axis limits according to time range
407413
onany(app.tmin, app.tmax, update=true) do _tmin, _tmax
@@ -462,25 +468,27 @@ function timeseries_card(app, session)
462468
for i in 1:length(_valid_idxs)
463469
data.val[i] = _dat[i,:]
464470
end
465-
@info "Data updated" data[]
466471
notify(data)
467472
end
468473

469474
# plot the thing
470475
on(data; update=true) do _dat
471-
@debug "TS: Data => Plotting"
472-
println("befor empty")
473-
empty!(ax)
474-
println("after empty")
475-
# vlines!(ax, app.t; color=:black)
476-
# for (idx, y) in zip(valid_idxs[], data[])
477-
for i in 1:length(valid_idxs[])
478-
# @info "plot $idx" y
479-
# lines!(ax, ts[], y; label=string(idx), color=getcolor(idx), linestyle=getlinestyle(idx))
480-
# lines!(ax, ts[], y; label=string(idx))
481-
lines!(ax, rand(3))
476+
@async begin
477+
try
478+
empty!(ax)
479+
vlines!(ax, app.t; color=:black)
480+
for (idx, y) in zip(valid_idxs[], data[])
481+
color = begin
482+
key = idx isa VIndex ? VIndex(idx.compidx) : EIndex(idx.compidx)
483+
Cycled(color_cache[key])
484+
end
485+
linestyle = Cycled(linestyle_cache[idx.subidx])
486+
lines!(ax, ts[], y; label=string(idx), color, linestyle)
487+
end
488+
catch e
489+
@error "Plotting failed" e
490+
end
482491
end
483-
@debug "End of plotting"
484492
nothing
485493
end
486494

@@ -500,8 +508,7 @@ function timeseries_card(app, session)
500508

501509
Card(
502510
Grid(
503-
comp_sel_dom,
504-
state_sel_dom,
511+
comp_state_sel_dom,
505512
fig
506513
)
507514
)

NetworkDynamicsInspector/src/widgets.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,13 @@ function Bonito.jsrender(session::Session, toggle::ToggleSwitch)
637637
)
638638
)
639639

640+
# wrap slider and slider::before in another div with rel pos
641+
# otherwise the slider::before will be positioned relative to
642+
# slider_grid
643+
slider_and_thumb_container_style = Styles(
644+
"position" => "relative",
645+
)
646+
640647
jscode = js"""
641648
(input) => {
642649
input.onchange = function() {
@@ -658,7 +665,11 @@ function Bonito.jsrender(session::Session, toggle::ToggleSwitch)
658665
container = DOM.label(
659666
inputdom,
660667
DOM.div(
661-
DOM.div(; class="slider", style=slider_style),
668+
DOM.div(
669+
DOM.div(; class="slider", style=slider_style);
670+
class="slider-and-thumb-container",
671+
style=slider_and_thumb_container_style
672+
),
662673
DOM.div(toggle.label; class="sliederlabel");
663674
class="switch-label-grid", style=slider_grid_style
664675
);

NetworkDynamicsInspector/test/toggle_test.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ let
88
on(toggle.value) do state
99
@info "value = $state"
1010
end
11+
toggle2 = NetworkDynamicsInspector.ToggleSwitch(label="long labeel")
1112
DOM.div(
1213
DOM.span("text before"),
1314
toggle,
15+
DOM.div(toggle2, style=Styles("width"=>"100px"))
1416
)
1517
end;
1618
serve_app(_app)

test/Inspector_test.jl

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ using GraphMakie
88
using Graphs: SimpleGraph
99
using OrdinaryDiffEqTsit5
1010
using Graphs: Graphs
11+
include(joinpath(pkgdir(NetworkDynamics), "test", "ComponentLibrary.jl"))
1112

1213
sol = let
13-
include(joinpath(pkgdir(NetworkDynamics), "test", "ComponentLibrary.jl"))
14-
1514
g = SimpleGraph([0 1 1 0 1;
16-
1 0 1 1 0;
17-
1 1 0 1 0;
18-
0 1 1 0 1;
19-
1 0 0 1 0])
15+
1 0 1 1 0;
16+
1 1 0 1 0;
17+
0 1 1 0 1;
18+
1 0 0 1 0])
2019
vs = [Lib.swing_mtk() for _ in 1:5];
2120
set_default!(vs[1], :Pmech, -1)
2221
set_default!(vs[2], :Pmech, 1.5)
@@ -55,7 +54,7 @@ sol = let
5554
sol = solve(prob, Tsit5());
5655
end;
5756

58-
ENV["JULIA_DEBUG"] = NetworkDynamicsInspector
57+
# ENV["JULIA_DEBUG"] = NetworkDynamicsInspector
5958
# ENV["JULIA_DEBUG"] = ""
6059

6160
app = (;
@@ -104,23 +103,3 @@ let
104103
end;
105104
serve_app(_app)
106105
end
107-
108-
server_ref = Ref(nothing)
109-
let
110-
_app = App() do session
111-
fig = Figure()
112-
ax = Axis(fig[1,1])
113-
button = Bonito.Button("add plot")
114-
115-
on(button.value) do _
116-
scatter!(ax, rand(3))
117-
end
118-
119-
Grid(Card(fig), button)
120-
end;
121-
if !isnothing(server_ref[]) && Bonito.HTTPServer.isrunning(server_ref[])
122-
@info "Stop running server..."
123-
close(SERVER[])
124-
end
125-
server_ref[] = Bonito.Server(_app, "0.0.0.0", 8080)
126-
end

0 commit comments

Comments
 (0)