|
| 1 | +function rectselect(ax) |
| 2 | + selrect, h = select_vspan(ax.scene; color = (0.9)) |
| 3 | + translate!(h, 0, 0, -1) # move to background |
| 4 | + return selrect |
| 5 | +end |
| 6 | + |
| 7 | +function Bonito.jsrender(s::Session, selector::SelectSet) |
| 8 | + rows = map(selector.items[]) do value |
| 9 | + c = Bonito.Checkbox(true; class = "p-1 m-1") |
| 10 | + on(s, c.value) do x |
| 11 | + values = selector.value[] |
| 12 | + has_item = (value in values) |
| 13 | + if x |
| 14 | + !has_item && push!(values, value) |
| 15 | + else |
| 16 | + has_item && filter!(x -> x != value, values) |
| 17 | + end |
| 18 | + notify(selector.value) |
| 19 | + end |
| 20 | + return Row(value, c) |
| 21 | + end |
| 22 | + return Bonito.jsrender(s, Card(Col(rows...))) |
| 23 | +end |
| 24 | + |
| 25 | +function style_map(::AbstractRange{<:Number}) |
| 26 | + return Dict( |
| 27 | + :color => identity, |
| 28 | + :colormap => RGBAf.(Colors.color.(to_colormap(:lighttest)), 0.5), |
| 29 | + ) |
| 30 | +end |
| 31 | + |
| 32 | +function style_map(values::Set) |
| 33 | + mpalette = [:circle, :star4, :xcross, :diamond] |
| 34 | + dict = Dict(v => mpalette[i] for (i, v) in enumerate(values)) |
| 35 | + mcmap = Makie.wong_colors(0.5) |
| 36 | + mcolor_lookup = Dict(v => mcmap[i] for (i, v) in enumerate(values)) |
| 37 | + return Dict(:marker => v -> dict[v], :marker_color => mcolor_lookup) |
| 38 | +end |
| 39 | + |
| 40 | +function select_vspan(scene; blocking = false, priority = 2, kwargs...) |
| 41 | + key = Mouse.left |
| 42 | + waspressed = Observable(false) |
| 43 | + rect = Observable(Rectf(0, 0, 1, 1)) # plotted rectangle |
| 44 | + rect_ret = Observable(Rectf(0, 0, 1, 1)) # returned rectangle |
| 45 | + |
| 46 | + # Create an initially hidden rectangle |
| 47 | + low = Observable(0.0f0) |
| 48 | + high = Observable(0.0f0) |
| 49 | + |
| 50 | + on(rect) do r |
| 51 | + low.val = r.origin[1] |
| 52 | + high[] = r.origin[1] + r.widths[1] |
| 53 | + end |
| 54 | + plotted_span = vspan!( |
| 55 | + scene, |
| 56 | + low, |
| 57 | + high, |
| 58 | + visible = false, |
| 59 | + kwargs..., |
| 60 | + transparency = true, |
| 61 | + color = (:black, 0.1), |
| 62 | + ) |
| 63 | + |
| 64 | + on(events(scene).mousebutton, priority = priority) do event |
| 65 | + if event.button == key |
| 66 | + if event.action == Mouse.press && is_mouseinside(scene) |
| 67 | + mp = mouseposition(scene) |
| 68 | + waspressed[] = true |
| 69 | + plotted_span[:visible] = true # start displaying |
| 70 | + rect[] = Rectf(mp, 0.0, 0.0) |
| 71 | + return Consume(blocking) |
| 72 | + end |
| 73 | + end |
| 74 | + if !(event.button == key && event.action == Mouse.press) |
| 75 | + if waspressed[] # User has selected the rectangle |
| 76 | + waspressed[] = false |
| 77 | + r = Makie.absrect(rect[]) |
| 78 | + w, h = widths(r) |
| 79 | + rect_ret[] = r |
| 80 | + end |
| 81 | + # always hide if not the right key is pressed |
| 82 | + #plotted_span[:visible] = false # make the plotted rectangle invisible |
| 83 | + return Consume(blocking) |
| 84 | + end |
| 85 | + |
| 86 | + return Consume(false) |
| 87 | + end |
| 88 | + on(events(scene).mouseposition, priority = priority) do event |
| 89 | + if waspressed[] |
| 90 | + mp = mouseposition(scene) |
| 91 | + mini = minimum(rect[]) |
| 92 | + rect[] = Rectf(mini, mp - mini) |
| 93 | + return Consume(blocking) |
| 94 | + end |
| 95 | + return Consume(false) |
| 96 | + end |
| 97 | + |
| 98 | + return rect_ret, plotted_span |
| 99 | +end |
| 100 | + |
| 101 | +function Bonito.jsrender(s::Session, selector::SelectSet) |
| 102 | + rows = map(selector.items[]) do value |
| 103 | + c = Bonito.Checkbox(true; class = "p-1 m-1") |
| 104 | + on(s, c.value) do x |
| 105 | + values = selector.value[] |
| 106 | + has_item = (value in values) |
| 107 | + if x |
| 108 | + !has_item && push!(values, value) |
| 109 | + else |
| 110 | + has_item && filter!(x -> x != value, values) |
| 111 | + end |
| 112 | + notify(selector.value) |
| 113 | + end |
| 114 | + return Row(value, c) |
| 115 | + end |
| 116 | + return Bonito.jsrender(s, Card(Col(rows...))) |
| 117 | +end |
| 118 | + |
| 119 | +function variable_legend(name, values::AbstractRange{<:Number}, palette::Dict) |
| 120 | + range, cmap = palette[:colormap] |
| 121 | + return S.Colorbar(limits = range, colormap = cmap, label = string(name)) |
| 122 | +end |
| 123 | + |
| 124 | +function variable_legend(name, values::Set, palette::Dict) |
| 125 | + marker_color_lookup = (x) -> begin |
| 126 | + if haskey(palette, :color) |
| 127 | + return get(palette[:color], x, :black) |
| 128 | + else |
| 129 | + return :black |
| 130 | + end |
| 131 | + end |
| 132 | + marker_lookup = (x) -> begin |
| 133 | + if haskey(palette, :marker) |
| 134 | + return palette[:marker][x] |
| 135 | + else |
| 136 | + return :rect |
| 137 | + end |
| 138 | + end |
| 139 | + conditions = collect(values) |
| 140 | + elements = map(conditions) do c |
| 141 | + return MarkerElement(marker = marker_lookup(c), color = marker_color_lookup(c)) |
| 142 | + end |
| 143 | + return S.Legend(elements, conditions) |
| 144 | +end |
| 145 | + |
| 146 | + |
| 147 | +widget_value(w::Vector{<:String}; resolution = 1) = w |
| 148 | +widget_value(x::Vector; resolution = 1) = |
| 149 | + x[1] ≈ x[end] ? Float64[] : range(Float64(x[1]), Float64(x[end]), length = 5) |
0 commit comments