Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement S.Colorbar(plotspec) #4520

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/specapi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,43 @@ function Base.getproperty(p::BlockSpec, k::Symbol)
end
Base.propertynames(p::BlockSpec) = Tuple(keys(p.kwargs))

function get_numeric_colors(plot::PlotSpec)
if plot.type in [:Heatmap, :Image, :Surface]
z = plot.args[end]
if z isa AbstractMatrix{<: Real}
return z
end
else
if haskey(plot.kwargs, :color) && plot.kwargs[:color] isa AbstractArray{<:Real}
return plot.kwargs[:color]
end
end
return nothing
end


# TODO it's really hard to get from PlotSpec -> Plot object in the
# Colorbar constructor (to_layoutable),
# since the plot may not be created yet and may change when calling
# update_layoutable!. So for now, we manually extract the Colorbar arguments from the spec
# Which is a bit brittle and won't work for Recipes which overload the Colorbar api (extract_colormap)
# We hope to improve the situation after the observable refactor, which may bring us a bit closer to
# Being able to use the Plot object itself instead of a spec.
function extract_colorbar_kw!(attr, plot::PlotSpec)
for k in [:colorrange, :colormap, :lowclip, :highclip, :nan_color]
if haskey(plot.kwargs, k)
attr[k] = plot.kwargs[k]
end
end
if !haskey(plot.kwargs, :colorrange)
color = get_numeric_colors(plot)
if !isnothing(color)
attr[:colorrange] = nan_extrema(color)
end
end
return attr
end

function BlockSpec(typ::Symbol, args...; plots::Vector{PlotSpec}=PlotSpec[], kw...)
attr = Dict{Symbol,Any}(kw)
if typ == :Legend
Expand All @@ -201,8 +238,16 @@ function BlockSpec(typ::Symbol, args...; plots::Vector{PlotSpec}=PlotSpec[], kw.
attr[:entrygroups] = entrygroups
return BlockSpec(typ, attr, plots)
else
if typ == :Colorbar && !isempty(args)
if length(args) == 1 && args[1] isa PlotSpec
extract_colorbar_kw!(attr, args[1])
args = ()
else
error("Only one argument `arg::PlotSpec` is supported for Colorbar. Found: $(args)")
end
end
if !isempty(args)
error("BlockSpecs, with an exception for Legend, don't support positional arguments yet.")
error("BlockSpecs, with an exception for Legend and Colorbar, don't support positional arguments yet.")
end
return BlockSpec(typ, attr, plots)
end
Expand Down
Loading