From 87c44e231b26bf844248f6b5ed94e934bc57741c Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Thu, 17 Sep 2020 17:34:16 +0200 Subject: [PATCH] fix vline & vspan (#2985) * add tests * fix vline, vspan with dates * add limit tests for vline and vspan * fix 1.3 tests --- src/recipes.jl | 10 +++++----- test/integration_dates.jl | 16 ++++++++++++++++ test/runtests.jl | 1 + test/test_recipes.jl | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 test/integration_dates.jl diff --git a/src/recipes.jl b/src/recipes.jl index dd8d2dc5..5760a6c1 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -122,8 +122,8 @@ end @deps hline straightline @recipe function f(::Type{Val{:vline}}, x, y, z) - n = length(x) - newx = vec(Float64[xi for i = 1:3, xi in x]) + n = length(y) + newx = vec(Float64[yi for i = 1:3, yi in y]) x := newx y := repeat(Float64[1, 2, NaN], n) seriestype := :straightline @@ -133,7 +133,7 @@ end @recipe function f(::Type{Val{:hspan}}, x, y, z) n = div(length(y), 2) - newx = repeat([-Inf, Inf, Inf, -Inf, NaN], outer = n) + newx = repeat([1, 2, 2, 1, NaN], outer = n) newy = vcat([[y[2i - 1], y[2i - 1], y[2i], y[2i], NaN] for i = 1:n]...) linewidth --> 0 x := newx @@ -144,8 +144,8 @@ end @deps hspan shape @recipe function f(::Type{Val{:vspan}}, x, y, z) - n = div(length(x), 2) - newx = vcat([[x[2i - 1], x[2i - 1], x[2i], x[2i], NaN] for i = 1:n]...) + n = div(length(y), 2) + newx = vcat([[y[2i - 1], y[2i - 1], y[2i], y[2i], NaN] for i = 1:n]...) newy = repeat([-Inf, Inf, Inf, -Inf, NaN], outer = n) linewidth --> 0 x := newx diff --git a/test/integration_dates.jl b/test/integration_dates.jl new file mode 100644 index 00000000..6a5756c7 --- /dev/null +++ b/test/integration_dates.jl @@ -0,0 +1,16 @@ +using Plots, Test, Dates + +@testset "Limits" begin + y=[1.0*i*i for i in 1:10] + x=[Date(2019,11,i) for i in 1:10] + + rx=[x[3],x[5]] + + p = plot(x,y, widen = false) + vspan!(p, rx, label="", alpha=0.2) + + ref_ylims = (y[1], y[end]) + ref_xlims = (x[1].instant.periods.value, x[end].instant.periods.value) + @test Plots.ylims(p) == ref_ylims + @test Plots.xlims(p) == ref_xlims +end # testset diff --git a/test/runtests.jl b/test/runtests.jl index 5190f709..4e4ba79c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -16,6 +16,7 @@ include("test_axis_letter.jl") include("test_recipes.jl") include("test_hdf5plots.jl") include("test_pgfplotsx.jl") +include("integration_dates.jl") reference_dir(args...) = joinpath(homedir(), ".julia", "dev", "PlotReferenceImages", args...) diff --git a/test/test_recipes.jl b/test/test_recipes.jl index b1f34351..f03a7d2a 100644 --- a/test/test_recipes.jl +++ b/test/test_recipes.jl @@ -4,3 +4,21 @@ using Plots, Test lens!(pl, [1,2], [1,2], inset = (1, bbox(0.0,0.0,0.2,0.2))) @test length(pl.series_list) == 4 end # testset + +@testset "vline, vspan" begin + vl = vline([1], widen = false) + @test Plots.xlims(vl) == (1,2) + @test Plots.ylims(vl) == (1,2) + vl = vline([1], xlims=(0,2), widen = false) + @test Plots.xlims(vl) == (0,2) + vl = vline([1], ylims=(-3,5), widen = false) + @test Plots.ylims(vl) == (-3,5) + + vsp = vspan([1,3], widen = false) + @test Plots.xlims(vsp) == (1,3) + @test Plots.ylims(vsp) == (0,1) # TODO: might be problematic on log-scales + vsp = vspan([1,3], xlims=(-2,5), widen = false) + @test Plots.xlims(vsp) == (-2,5) + vsp = vspan([1,3], ylims=(-2,5), widen = false) + @test Plots.ylims(vsp) == (-2,5) +end # testset