From 5dff00e2a31ae8864b46a7cb7739814a9b3bf79e Mon Sep 17 00:00:00 2001 From: Chris Rackauckas Date: Wed, 21 Aug 2019 09:02:10 -0400 Subject: [PATCH] start first time to plot investigation --- src/Plots.jl | 3 ++- src/backends.jl | 23 +++++++++++++++++------ src/output.jl | 2 +- src/pipeline.jl | 9 ++++----- src/plot.jl | 10 +++++----- src/types.jl | 12 ++++++------ 6 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/Plots.jl b/src/Plots.jl index 54d34615..dbfbc334 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -220,6 +220,7 @@ end # --------------------------------------------------------- -const CURRENT_BACKEND = CurrentBackend(:none) +const CURRENT_BACKEND = Plots.CurrentBackend(:gr) +gr() end # module diff --git a/src/backends.jl b/src/backends.jl index 01226e78..caa7a666 100644 --- a/src/backends.jl +++ b/src/backends.jl @@ -128,11 +128,23 @@ _update_plot_object(plt::Plot) = nothing # --------------------------------------------------------- -mutable struct CurrentBackend - sym::Symbol - pkg::AbstractBackend +mutable struct CurrentBackend{sym,T} + pkg::T +end +function CurrentBackend(sym::Symbol) + bkend = _backend_instance(sym) + CurrentBackend{sym,typeof(bkend)}(bkend) +end + +function Base.getproperty(bkend::CurrentBackend{sym,T},x::Symbol) where {sym,T} + if x === :sym + return sym + elseif x === :pkg + return getfield(bkend,:pkg) + else + error("Must be sym or pkg") + end end -CurrentBackend(sym::Symbol) = CurrentBackend(sym, _backend_instance(sym)) # --------------------------------------------------------- @@ -177,8 +189,7 @@ function backend(pkg::AbstractBackend) _initialize_backend(pkg) push!(_initialized_backends, sym) end - CURRENT_BACKEND.sym = sym - CURRENT_BACKEND.pkg = pkg + CURRENT_BACKEND = Plots.CurrentBackend(sym) pkg end diff --git a/src/output.jl b/src/output.jl index bbc7b80e..cb7fe8cb 100644 --- a/src/output.jl +++ b/src/output.jl @@ -140,7 +140,7 @@ end function Base.display(::PlotsDisplay, plt::Plot) prepare_output(plt) - _display(plt) + #_display(plt) end _do_plot_show(plt, showval::Bool) = showval && gui(plt) diff --git a/src/pipeline.jl b/src/pipeline.jl index 98f06bf9..e3615536 100644 --- a/src/pipeline.jl +++ b/src/pipeline.jl @@ -203,7 +203,7 @@ end # ------------------------------------------------------------------ # setup plot and subplot -function _plot_setup(plt::Plot, plotattributes::KW, kw_list::Vector{KW}) +function _plot_setup(plt::Plot{T}, plotattributes::KW, kw_list::Vector{KW}) where {T} # merge in anything meant for the Plot for kw in kw_list, (k,v) in kw haskey(_plot_defaults, k) && (plotattributes[k] = pop!(kw, k)) @@ -252,18 +252,17 @@ function _plot_setup(plt::Plot, plotattributes::KW, kw_list::Vector{KW}) plt[:inset_subplots] = nothing end -function _subplot_setup(plt::Plot, plotattributes::KW, kw_list::Vector{KW}) +function _subplot_setup(plt::Plot{T}, plotattributes::KW, kw_list::Vector{KW}) where T # we'll keep a map of subplot to an attribute override dict. # Subplot/Axis attributes set by a user/series recipe apply only to the # Subplot object which they belong to. # TODO: allow matrices to still apply to all subplots - sp_attrs = Dict{Subplot,Any}() + sp_attrs = Dict{Subplot{T},Any}() for kw in kw_list # get the Subplot object to which the series belongs. sps = get(kw, :subplot, :auto) - sp = get_subplot(plt, _cycle(sps == :auto ? plt.subplots : plt.subplots[sps], command_idx(kw_list,kw))) + sp::Subplot{T} = get_subplot(plt, _cycle(sps == :auto ? plt.subplots : plt.subplots[sps], command_idx(kw_list,kw))) kw[:subplot] = sp - # extract subplot/axis attributes from kw and add to sp_attr attr = KW() for (k,v) in collect(kw) diff --git a/src/plot.jl b/src/plot.jl index 4352403a..644beb8d 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -50,7 +50,6 @@ function plot(args...; kw...) # this creates a new plot with args/kw and sets it to be the current plot plotattributes = KW(kw) preprocessArgs!(plotattributes) - # create an empty Plot then process plt = Plot() # plt.user_attr = plotattributes @@ -163,7 +162,7 @@ end # this is the core plotting function. recursively apply recipes to build # a list of series KW dicts. # note: at entry, we only have those preprocessed args which were passed in... no default values yet -function _plot!(plt::Plot, plotattributes::KW, args::Tuple) +function _plot!(plt::Plot{T}, plotattributes::KW, args::Tuple) where {T} plotattributes[:plot_object] = plt if !isempty(args) && !isdefined(Main, :StatsPlots) && @@ -202,7 +201,10 @@ function _plot!(plt::Plot, plotattributes::KW, args::Tuple) # -------------------------------- # Plot/Subplot/Layout setup # -------------------------------- + _plot_setup(plt, plotattributes, kw_list) + + # 6 seconds _subplot_setup(plt, plotattributes, kw_list) # !!! note: At this point, kw_list is fully decomposed into individual series... one KW per series. !!! @@ -216,7 +218,7 @@ function _plot!(plt::Plot, plotattributes::KW, args::Tuple) # map(DD, kw_list) for kw in kw_list - sp::Subplot = kw[:subplot] + sp::Subplot{T} = kw[:subplot] # idx = get_subplot_index(plt, sp) # # we update subplot args in case something like the color palatte is part of the recipe @@ -233,7 +235,6 @@ function _plot!(plt::Plot, plotattributes::KW, args::Tuple) # be able to support step, bar, and histogram plots (and any recipes that use those components). _process_seriesrecipe(plt, kw) end - # -------------------------------- current(plt) @@ -243,7 +244,6 @@ function _plot!(plt::Plot, plotattributes::KW, args::Tuple) # gui(plt) # end _do_plot_show(plt, plt[:show]) - plt end diff --git a/src/types.jl b/src/types.jl index 887245e5..39c26a80 100644 --- a/src/types.jl +++ b/src/types.jl @@ -71,17 +71,17 @@ mutable struct Plot{T<:AbstractBackend} <: AbstractPlot{T} user_attr::KW # raw arg inputs (after aliases). these are used as the input dict in `_plot!` series_list::Vector{Series} # arguments for each series o # the backend's plot object - subplots::Vector{Subplot} + subplots::Vector{Subplot{T}} spmap::SubplotMap # provide any label as a map to a subplot layout::AbstractLayout - inset_subplots::Vector{Subplot} # list of inset subplots + inset_subplots::Vector{Subplot{T}} # list of inset subplots init::Bool end -function Plot() - Plot(backend(), 0, KW(), KW(), Series[], nothing, - Subplot[], SubplotMap(), EmptyLayout(), - Subplot[], false) +function Plot(_backend = CURRENT_BACKEND) + Plot(_backend.pkg, 0, KW(), KW(), Series[], nothing, + Subplot{typeof(_backend.pkg)}[], SubplotMap(), EmptyLayout(), + Subplot{typeof(_backend.pkg)}[], false) end # -----------------------------------------------------------------------