Skip to content

Commit

Permalink
add bind_z! and bind_text!
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Apr 9, 2024
1 parent 5eafd9b commit fc2b59c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 24 deletions.
9 changes: 0 additions & 9 deletions src/Base.jl

This file was deleted.

42 changes: 42 additions & 0 deletions src/Layers/Base.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export gap!
export bind_text!, bind_z!, add_labels!

Base.size(x::Observable) = size(x[])
Base.size(x::Observable, i) = size(x[], i)

function gap!(fig, gap=0)
rowgap!(fig.layout, gap)
colgap!(fig.layout, gap)
end


function bind_text!(plts::Vector, labels::Observable{Vector{String}})
on(labels) do labels
for i in eachindex(plts)
plts[i].text[] = labels[i]
end
end
end

# for not Observable
bind_z!(plts, z) = nothing

function bind_z!(plts::Vector, z::Observable{<:AbstractArray{<:Real,3}})
on(z) do z
for i in eachindex(plts)
plts[i][3] = z[:, :, i]
end
end
end


function add_labels!(axs::Vector, labels::Observable, x, y;
fontsize=30, align=(:left, :top), offset=(10, -5))
plts = []
for i in eachindex(axs)
label = labels[][i]
plt = text!(axs[i], x, y, text=label; fontsize, align, offset)
push!(plts, plt)
end
bind_text!(plts, labels)
end
1 change: 1 addition & 0 deletions src/Layers/Layers.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export dateseries!
export imagesc!, imagesc

include("Base.jl")
include("dateseries.jl")
include("imagesc.jl")
# include("datevspan.jl")
19 changes: 6 additions & 13 deletions src/Layers/imagesc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function imagesc!(fig::Union{Figure,GridPosition,GridSubposition},
(fun_axis!)=nothing,
kw...) where {R<:AbstractArray{<:Real,3}}

length(gap) == 1 && (gap = (gap, gap))
n = size(z, 3)
if layout === nothing
ncol = ceil(Int, sqrt(n))
Expand All @@ -82,10 +83,10 @@ function imagesc!(fig::Union{Figure,GridPosition,GridSubposition},
end

titles === nothing && (titles = fill("", n))
k = 0
# ax, plt = nothing, nothing
axs = []
plts = []

k = 0
for i = 1:nrow, j = 1:ncol
k += 1
k > n && break
Expand All @@ -102,22 +103,14 @@ function imagesc!(fig::Union{Figure,GridPosition,GridSubposition},
push!(axs, ax)
push!(plts, plt)
end

# update plot
if isa(z, Observable)
on(z) do z
for i = 1:n
plts[i][3] = z[:, :, i]
end
end
end
bind_z!(plts, z)

# unify the legend
(colorrange != automatic && !force_show_legend) &&
Colorbar(fig[1:nrow, ncol+1], plts[1])

rowgap!(fig.layout, gap)
colgap!(fig.layout, gap)
rowgap!(fig.layout, gap[1])
colgap!(fig.layout, gap[2])
axs, plts
end

Expand Down
1 change: 0 additions & 1 deletion src/MakieLayers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export automatic
export map_on_keyboard, map_on_mouse
export my_theme!

include("Base.jl")
include("convert_arguments.jl")
include("Layers/Layers.jl")
include("events.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/colormap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using Serialization: serialize, deserialize
using Colors
using ColorSchemes

nan_color = RGBA(1.0, 1.0, 1.0, 0.2)
nan_color = RGBA(1.0, 1.0, 1.0, 0.0)

project_path(f...) = normpath(joinpath(@__DIR__, "..", f...))
load(f) = deserialize(project_path(f))
Expand Down
10 changes: 10 additions & 0 deletions test/test-Observable.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
begin
z = Observable(rand(4, 4, 4))

fig = Figure()
imagesc!(fig, z)
fig
end

z[] = rand(4, 4, 4)
# if z changed, fig will update

0 comments on commit fc2b59c

Please sign in to comment.