diff --git a/Project.toml b/Project.toml index edcf2d34d..de846f9fc 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Plots" uuid = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" author = ["Tom Breloff (@tbreloff)"] -version = "1.36.4" +version = "1.36.5" [deps] Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f" @@ -21,6 +21,7 @@ NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" PlotThemes = "ccf2f8ad-2431-5c83-bf29-c5338b663b6a" PlotUtils = "995b91a9-d308-5afd-9ec6-746e21dbc043" +Preferences = "21216c6a-2e73-6563-6e65-726566657250" Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" @@ -62,6 +63,7 @@ PyCall = "1" PyPlot = "2" RecipesBase = "1.3.1" RecipesPipeline = "0.6.10" +Preferences = "1" Reexport = "0.2, 1" RelocatableFolders = "0.3, 1" Requires = "1" diff --git a/src/Plots.jl b/src/Plots.jl index 25afe8117..ff7d20239 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -35,6 +35,7 @@ using Base.Meta using Requires using Reexport using Unzip +@reexport using Preferences @reexport using RecipesBase @reexport using PlotThemes @reexport using PlotUtils @@ -204,9 +205,11 @@ include("init.jl") include("backends/plotly.jl") include("backends/web.jl") -include("backends/gr.jl") include("shorthands.jl") -include("precompile.jl") +@static if backend(_pick_default_backend()) == GRBackend() + include("backends/gr.jl") + include("precompile.jl") +end end diff --git a/src/backends.jl b/src/backends.jl index 588467918..cb81e57c6 100644 --- a/src/backends.jl +++ b/src/backends.jl @@ -36,8 +36,6 @@ macro init_backend(s) end |> esc end -# include("backends/web.jl") - # --------------------------------------------------------- # don't do anything as a default @@ -131,20 +129,19 @@ CurrentBackend(sym::Symbol) = CurrentBackend(sym, _backend_instance(sym)) # --------------------------------------------------------- -_fallback_default_backend() = backend(GRBackend()) +_fallback_default_backend() = "gr" +function _default_backend() + Symbol(lowercase(@load_preference("default_backend", _fallback_default_backend()))) +end function _pick_default_backend() - if (env_default = get(ENV, "PLOTS_DEFAULT_BACKEND", "")) != "" - if (sym = Symbol(lowercase(env_default))) in _backends - backend(sym) - else - @warn """You have set PLOTS_DEFAULT_BACKEND=$env_default, but it is not a valid backend package. - Choose from: \n\t$(join(sort(_backends), "\n\t")) - """ - _fallback_default_backend() - end + if (sym = _default_backend()) in _backends + backend(sym) else - _fallback_default_backend() + @warn """You have set $sym as the default backend, but it is not a valid backend package. + Choose from: \n\t$(join(sort(_backends), "\n\t")) + """ + backend(_fallback_default_backend()) end end diff --git a/src/backends/gr.jl b/src/backends/gr.jl index ae70cf3a0..3090f392c 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -1231,8 +1231,8 @@ function gr_get_legend_geometry(vp, sp) span_hspace = span + pad # part of the horizontal increment dx = (textw + (vertical ? 0 : span_hspace)) * get(ekw, :legend_wfactor, 1) - # This is to prevent that linestyle is obscured by large markers. - # We are trying to get markers to not be larger than half the line length. + # This is to prevent that linestyle is obscured by large markers. + # We are trying to get markers to not be larger than half the line length. # 1 / leg.dy translates base_factor to line length units (important in the context of size kwarg) # gr_legend_marker_to_line_factor is an empirical constant to translate between line length unit and marker size unit base_markersize = gr_legend_marker_to_line_factor[] * span / dy # NOTE: arbitrarily based on horizontal measures ! diff --git a/src/init.jl b/src/init.jl index 8314ee6c9..e3fb7ab18 100644 --- a/src/init.jl +++ b/src/init.jl @@ -3,6 +3,7 @@ using Scratch using REPL const plotly_local_file_path = Ref{Union{Nothing,String}}(nothing) +const BACKEND_PATH_GR = @path joinpath(@__DIR__, "backends", "gr.jl") const BACKEND_PATH_GASTON = @path joinpath(@__DIR__, "backends", "gaston.jl") const BACKEND_PATH_HDF5 = @path joinpath(@__DIR__, "backends", "hdf5.jl") const BACKEND_PATH_INSPECTDR = @path joinpath(@__DIR__, "backends", "inspectdr.jl") @@ -63,6 +64,11 @@ function __init__() ) end, ) + if _default_backend() !== :gr + @require GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71" begin + include(BACKEND_PATH_GR) + end + end @require HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" begin include(BACKEND_PATH_HDF5) diff --git a/test/runtests.jl b/test/runtests.jl index 356fcc522..77531d35f 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -30,6 +30,7 @@ hdf5() gr() for name in ( + "init", "quality", "misc", "utils", diff --git a/test/test_backends.jl b/test/test_backends.jl index 404eef160..2339b4851 100644 --- a/test/test_backends.jl +++ b/test/test_backends.jl @@ -220,16 +220,6 @@ end @test Plots.CurrentBackend(:gr).sym === :gr @test Plots.merge_with_base_supported([:annotations, :guide]) isa Set - @test_logs (:warn, r".*not a valid backend package") withenv( - "PLOTS_DEFAULT_BACKEND" => "invalid", - ) do - Plots._pick_default_backend() - end - @test withenv("PLOTS_DEFAULT_BACKEND" => "unicodeplots") do - Plots._pick_default_backend() - end == Plots.UnicodePlotsBackend() - @test_logs (:warn, r".*is not a supported backend") backend(:invalid) - @test Plots._pick_default_backend() == Plots.GRBackend() end end diff --git a/test/test_init.jl b/test/test_init.jl new file mode 100644 index 000000000..c680f08be --- /dev/null +++ b/test/test_init.jl @@ -0,0 +1,23 @@ +using Plots, Test + +const plots_path = escape_string(pkgdir(Plots)) + +@testset "Default Backend" begin + set_preferences!(Plots, "default_backend" => "plotly") + out = run(``` + $(Base.julia_cmd()) -E """ + using Pkg + Pkg.activate(; temp = true) + Pkg.develop(path = \"$(plots_path)\") + using Test + using Plots + @test backend() == Plots.PlotlyBackend() + """ + ```) + @test out.exitcode == 0 + set_preferences!( + Plots, + "default_backend" => Plots._fallback_default_backend(), + force = true, + ) +end