Skip to content

Commit af98bb9

Browse files
committed
Drag datamarkers
1 parent f78f4f4 commit af98bb9

File tree

6 files changed

+66
-18
lines changed

6 files changed

+66
-18
lines changed

Images/datamarker_drag.gif

278 KB
Loading

Images/datamarkers.gif

-245 KB
Binary file not shown.

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ This project originated as an exploration of the interactive possibilities that
77

88
**Note**: Some of the features are experimental. They might not function fully as expected or could be subject to changes in future versions.
99

10-
**Note**: It is possible that currently there are too many keywords. Options are being considered to simplify some aspects.
11-
1210
## Usage
1311

1412
```julia
@@ -120,9 +118,9 @@ fig
120118

121119
## Interactive Data Markers
122120

123-
Interactive data markers can be added to your Smith chart using the `datamarkers(sc::SmithAxis, gp::GridPosition)` function. Double-click on lines or scatter plots to place a marker. To remove a marker, double-click on it. Future enhancements may include marker dragging and real-time information updates.
121+
Interactive data markers can be added to your Smith chart using the `datamarkers(sc::SmithAxis, gp::GridPosition)` function. Double-click on lines or scatter plots to place a marker. To remove a marker, double-click on it. It is possible to move the marker by dragging it with the left click.
124122

125-
![datamarkergif](Images/datamarkers.gif)
123+
![datamarkergif](Images/datamarker_drag.gif.gif)
126124

127125

128126
## Stability, Gain and Noise Circles
@@ -277,6 +275,6 @@ There are multiple keywords to modify the position of the ticks. Some of them ar
277275

278276
### Subgrid split
279277

280-
The `splitgrid` keyword controls the number of cuts for each zoomlevel. The value should be a
278+
The `splitgrid` keyword controls the number of cuts for each zoomlevel.
281279

282280
![keywordexample](Images/splitminor.png)

src/blockinteractivity.jl

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,6 @@ function ztotext(zi, reflection, freq, idx)
223223
return txt
224224
end
225225

226-
#TODO: Dragg markers
227-
#TODO: Associate markers to a specific plot.
228-
#TODO: save marker's DATA
229226
"""
230227
datamarkers(ax::SmithAxis, gp::GridPosition, priority = 100; fontsize = 10.0, title = true, kwargs...)
231228
@@ -256,18 +253,18 @@ function datamarkers(ax::SmithAxis, gp::GridPosition, markerdict = Dict{Int, Com
256253
for (plt, txt) in tplots
257254
plt[2].val = string(i)
258255
notify(plt[2])
259-
lbl_txt.val *= "[$i]: " * txt * "\n"
256+
lbl_txt.val *= "[$i]: " * txt[] * "\n"
260257
i = i + 1
261258
end
262-
259+
263260
sortedkeys = sort(collect(keys(markerdict)))
264261
for (id, k) in enumerate(sortedkeys)
265262
vk = markerdict[k]
266263
delete!(markerdict, k)
267264
markerdict[id] = vk
268265
end
269266

270-
resize_to_layout!(ax.parent)
267+
#resize_to_layout!(ax.parent)
271268
notify(lbl_txt)
272269
end
273270

@@ -304,10 +301,11 @@ function datamarkers(ax::SmithAxis, gp::GridPosition, markerdict = Dict{Int, Com
304301
freq = posvectors[id][4]
305302
zi = posvectors[id][2]
306303
markerdict[length(keys(markerdict))+1] = zi[idx]
307-
txt = ztotext(zi[idx], reflection, freq, idx)
308-
tp = tooltip!(ax, Observable(plt[1][][idx]), string(length(ax.temp_plots.val) + 1), inspectable = true)
304+
txt = Observable(ztotext(zi[idx], reflection, freq, idx))
305+
tooltiposition = Observable(plt[1][][idx])
306+
tp = tooltip!(ax, tooltiposition, string(length(ax.temp_plots.val) + 1), inspectable = true)
309307
translate!(tp, 0, 0, 8000)
310-
push!(ax.temp_plots.val, (tp, txt))
308+
push!(ax.temp_plots.val, (tp, txt, plt, Observable(idx), reflection, freq))
311309
notify(ax.temp_plots)
312310
end
313311
end
@@ -316,8 +314,8 @@ function datamarkers(ax::SmithAxis, gp::GridPosition, markerdict = Dict{Int, Com
316314
# Use tooltip's bounding box
317315
for (id, ttips) in enumerate(ax.temp_plots.val)
318316
bb = boundingbox(ttips[1])
319-
ox, oy, oz = bb.origin
320-
wx, wy, wz = bb.widths
317+
ox, oy, _ = bb.origin
318+
wx, wy, _ = bb.widths
321319
if (ox <= position[1] <= ox+wx) & (oy <= position[2] <= oy+wy)
322320
delete!(ax, ax.temp_plots[][id][1])
323321
popat!(ax.temp_plots[], id)
@@ -329,5 +327,55 @@ function datamarkers(ax::SmithAxis, gp::GridPosition, markerdict = Dict{Int, Com
329327
end
330328
end
331329
end
330+
331+
dragactive = Observable(false)
332+
temp_plot_id = Observable(1)
333+
# Maybe i shold not check de next and previous one and go directly to the closest one
334+
# Register interaction related to drag
335+
register_interaction!(ax, :leftdragtooltip) do event::MouseEvent, ax
336+
337+
if event.type === MouseEventTypes.leftdragstart
338+
position = mouseposition_px(ax.scene)
339+
# Get the tooltip id on temp_plots
340+
for (id, ttips) in enumerate(ax.temp_plots.val)
341+
bb = boundingbox(ttips[1])
342+
ox, oy, _ = bb.origin
343+
wx, wy, _ = bb.widths
344+
if (ox <= position[1] <= ox+wx) & (oy <= position[2] <= oy+wy)
345+
dragactive[] = true
346+
temp_plot_id[] = id
347+
break
348+
end
349+
end
350+
351+
elseif event.type === MouseEventTypes.leftdrag
352+
if dragactive[] == false
353+
return
354+
end
355+
356+
# Now we need to check the direction of the movement and change the
357+
# idx (index of the plot) that the tooltip is inspecting.
358+
# I need to keep the tooltip on the same line
359+
pos_act = event.data
360+
pos_prev = event.prev_data
361+
362+
if pos_act == pos_prev
363+
return
364+
end
365+
tooltip, _, plt, act_idx, reflection, freq = ax.temp_plots.val[temp_plot_id[]]
366+
new_idx = argmin(map(plt[1][]) do pos
367+
sqrt(sum(abs2, pos_act - pos))
368+
end)
369+
new_position = plt[1][][new_idx]
370+
act_idx[] = new_idx
371+
tooltip[1][] = new_position
372+
zi = reflection == true ? Complex(new_position...) : coords_to_z(new_position...)
373+
ax.temp_plots.val[temp_plot_id[]][2][] = ztotext(zi, reflection, freq, new_idx)
374+
notify(ax.temp_plots)
375+
376+
elseif event.type === MouseEventTypes.leftdragstop
377+
dragactive[] = false
378+
end
379+
end
332380
return nothing
333381
end

src/smithaxis.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ mouseeventhandle::Makie.MouseEventHandle
77
scrollevents::Observable{ScrollEvent}
88
keysevents::Observable{KeysEvent}
99
interactions::Dict{Symbol, Tuple{Bool, Any}}
10-
temp_plots::Observable{Vector{Tuple{Makie.Plot, String}}}
10+
temp_plots::Observable{Vector{Any}}
11+
#temp_plots::Observable{Vector{Tuple{Makie.Plot, String, Makie.Plot, Int64, Bool, Vector{<:Real}}}}
1112

1213
@attributes begin
1314
"""

src/smithaxisblock.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
function Makie.initialize_block!(sc::SmithAxis; palette=nothing)
22

3-
setfield!(sc, :temp_plots, Observable(Tuple{Makie.Plot, String}[]) )
3+
#setfield!(sc, :temp_plots, Observable(Tuple{Makie.Plot, String, Makie.Plot, Int64, Bool, Vector{<:Real}}[]) )
4+
setfield!(sc, :temp_plots, Observable([]))
45
targetlimits = Observable{Rect2f}(scdefaultlimits(sc.limits[]))
56
setfield!(sc, :targetlimits, targetlimits)
67

0 commit comments

Comments
 (0)