Skip to content

Commit 3daa9fa

Browse files
committed
add button to add ts plots
1 parent 6c1cc06 commit 3daa9fa

File tree

2 files changed

+72
-35
lines changed

2 files changed

+72
-35
lines changed

NetworkDynamicsInspector/src/NetworkDynamicsInspector.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,22 @@ function AppState(sol::SciMLBase.AbstractODESolution)
9999
graphplot = GraphPlot(sol)
100100
tsplots = OrderedDict(
101101
"ts-1" => TimeseriesPlot(),
102-
"ts-2" => TimeseriesPlot()
103102
)
104103
active_tsplot = "ts-1"
105104
AppState(sol, t, tmin, tmax, active_tsplot, graphplot, tsplots)
106105
end
107106

107+
function free_ts_key()
108+
tskeys = keys(APPSTATE[].tsplots[])
109+
used = Int[parse(Int, s[4:end]) for s in tskeys]
110+
i = 1
111+
while i used
112+
i += 1
113+
end
114+
return "ts-$i"
115+
end
116+
117+
108118
const APPSTATE = Ref{Union{Nothing,AppState}}(nothing)
109119
const SERVER = Ref{Any}(nothing)
110120
const SESSION = Ref{Union{Nothing,Session}}(nothing)
@@ -222,11 +232,7 @@ function get_webapp()
222232
element_info_card(app, session),
223233
class="graphplot-col"
224234
),
225-
DOM.div(
226-
timeslider_card(app),
227-
timeseries_cards(app, session),
228-
class="timeseries-col"
229-
),
235+
timeseries_col(app, session),
230236
class="maingrid"
231237
)
232238
end;

NetworkDynamicsInspector/src/timeseries.jl

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,48 @@
1+
function timeseries_col(app, session)
2+
col = DOM.div(
3+
timeslider_card(app),
4+
timeseries_cards(app, session),
5+
add_timeseries_button(app),
6+
class="timeseries-col"
7+
)
8+
9+
# add js to update the active ts plot
10+
onload_js = js"""
11+
(tscol) => {
12+
tscol.addEventListener("click", function(event) {
13+
// Find the closest .timeseries-card ancestor of the clicked element
14+
const clickedCard = event.target.closest('.timeseries-card');
15+
16+
// If a .timeseries-card was found, handle the click
17+
if (clickedCard) {
18+
// console.log("click on timeseries card", clickedCard.id);
19+
20+
// Notify the app with the id of the clicked card
21+
$(app.active_tsplot).notify(clickedCard.id);
22+
23+
// Remove "active-tseries" class from all .timeseries-card elements
24+
document.querySelectorAll(".timeseries-card").forEach(element => {
25+
element.classList.remove("active-tseries");
26+
});
27+
28+
// Add "active-tseries" class to the clicked card
29+
clickedCard.classList.add("active-tseries");
30+
}
31+
}, { capture: true });
32+
33+
function triggerWindowResize() {
34+
window.dispatchEvent(new Event('resize'));
35+
}
36+
const triggerWindowResize_throttled = Bonito.throttle_function(triggerWindowResize, 100);
37+
const resizeObserver = new ResizeObserver(triggerWindowResize_throttled);
38+
resizeObserver.observe(tscol);
39+
}
40+
"""
41+
Bonito.onload(session, col, onload_js)
42+
43+
return col
44+
end
45+
146
function timeseries_cards(app, session)
247
cards = OrderedDict{String,Bonito.Hyperscript.Node{Bonito.Hyperscript.HTMLSVG}}()
348
container = Observable{Bonito.Hyperscript.Node{Bonito.Hyperscript.HTMLSVG}}()
@@ -27,7 +72,18 @@ function timeseries_cards(app, session)
2772
app.graphplot._selcomp[] = activesel
2873
end
2974

30-
return container[]
75+
return container
76+
end
77+
78+
function add_timeseries_button(app)
79+
button = Bonito.Button("Add Timeseries", class="add-ts-button")
80+
on(button.value) do _
81+
newkey = free_ts_key()
82+
@info "TS: Add Timeseries button clicked. Add $newkey"
83+
app.tsplots[][newkey] = TimeseriesPlot()
84+
notify(app.tsplots)
85+
end
86+
return button
3187
end
3288

3389
function timeseries_card(app, key, session)
@@ -213,7 +269,7 @@ function timeseries_card(app, key, session)
213269
Bonito.evaljs(session, js)
214270

215271
fig, ax = with_theme(apptheme()) do
216-
fig = Figure()
272+
fig = Figure(size=(100, 400))
217273
ax = Axis(fig[1, 1])
218274
fig, ax
219275
end
@@ -333,10 +389,10 @@ function timeseries_card(app, key, session)
333389
DOM.div(fig; class="timeseries-axis-container");
334390
class="timeseries-card-container"
335391
);
336-
class=cardclass
392+
class=cardclass,
393+
id=key
337394
)
338395

339-
340396
# trigger plot on document ready
341397
jqdocument = Bonito.JSString(raw"$(document)")
342398
trigger_plot = js"""
@@ -348,31 +404,6 @@ function timeseries_card(app, key, session)
348404
"""
349405
Bonito.evaljs(session, trigger_plot)
350406

351-
# on click set active-tseries class
352-
onload_js = js"""
353-
(card) => {
354-
card.addEventListener("click", function(event) {
355-
$(app.active_tsplot).notify($(key));
356-
357-
document.querySelectorAll(".timeseries-card").forEach(element => {
358-
element.classList.remove("active-tseries");
359-
});
360-
361-
// Add "active-tseries" to the given target element
362-
card.classList.add("active-tseries");
363-
}, { capture: true });
364-
365-
366-
function triggerWindowResize() {
367-
window.dispatchEvent(new Event('resize'));
368-
}
369-
const triggerWindowResize_throttled = Bonito.throttle_function(triggerWindowResize, 100);
370-
const resizeObserver = new ResizeObserver(triggerWindowResize_throttled);
371-
resizeObserver.observe(card);
372-
}
373-
"""
374-
Bonito.onload(session, card, onload_js)
375-
376407
return card
377408
end
378409

0 commit comments

Comments
 (0)