From 2d947f4a9779e03f14ead8b76511b1748a29c5f3 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Wed, 20 Nov 2019 17:28:23 +0100 Subject: [PATCH] native fillrange --- src/backends/pgfplotsx.jl | 96 +++++++++++++++++++-------------------- test/test_pgfplotsx.jl | 12 +++-- 2 files changed, 55 insertions(+), 53 deletions(-) diff --git a/src/backends/pgfplotsx.jl b/src/backends/pgfplotsx.jl index 90b823d8..c0c6e05e 100644 --- a/src/backends/pgfplotsx.jl +++ b/src/backends/pgfplotsx.jl @@ -88,6 +88,10 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) end end @label colorbar_end + # detect fillranges + # if any(series->series[:fillrange] != nothing, series_list(sp)) + # PGFPlotsX.push_preamble!(pgfx_plot.the_plot, "\\usepgfplotslibrary{fillbetween},\n") + # end push!(axis_opt, "colorbar style" => PGFPlotsX.Options( "title" => sp[:colorbar_title] @@ -125,71 +129,61 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) series_opt = PGFPlotsX.Options( "color" => opt[:linecolor], ) - if st == :shape - push!(series_opt, "area legend" => nothing) - end if opt[:marker_z] !== nothing push!(series_opt, "point meta" => "explicit") push!(series_opt, "scatter" => nothing) end - segments = iter_segments(series) - segment_opt = PGFPlotsX.Options() - for (i, rng) in enumerate(segments) - segment_opt = merge( segment_opt, pgfx_linestyle(opt, i) ) - segment_opt = merge( segment_opt, pgfx_marker(opt, i) ) - if st == :shape - segment_opt = merge( segment_opt, pgfx_fillstyle(opt, i) ) - end - seg_args = (arg[rng] for arg in args) - # add fillrange - if series[:fillrange] !== nothing && st != :shape - push!(axis, pgfx_fillrange_series(series, i, _cycle(series[:fillrange], rng), seg_args...)) - end - - # add to legend? - if i == 1 && sp[:legend] != :none && should_add_to_legend(series) - if opt[:fillrange] !== nothing - push!(segment_opt, "forget plot" => nothing) - push!(axis, pgfx_fill_legend_hack(opt, args)) - else - if st == :shape - push!(segment_opt, "area legend" => nothing) - end - end - push!( axis, PGFPlotsX.LegendEntry( opt[:label] )) - else - push!(segment_opt, "forget plot" => nothing) - end - end - #include additional style - if haskey(_pgfx_series_extrastyle, st) - push!(series_opt, _pgfx_series_extrastyle[st] => nothing) - end - # TODO: different seriestypes, histogramms, contours, etc. - # TODO: colorbars - # TODO: gradients if is3d(series) series_func = PGFPlotsX.Plot3 else series_func = PGFPlotsX.Plot end - if st == :scatter - push!(series_opt, "only marks" => nothing) + if series[:fillrange] !== nothing + series_opt = merge(series_opt, pgfx_fillstyle(opt)) + push!(series_opt, "area legend" => nothing) end - series_plot = series_func( - merge(series_opt, segment_opt), - PGFPlotsX.Coordinates(args..., meta = opt[:marker_z]) - ) + # include additional style + if haskey(_pgfx_series_extrastyle, st) + push!(series_opt, _pgfx_series_extrastyle[st] => nothing) + end + # treat segments + segments = iter_segments(series) + segment_opt = PGFPlotsX.Options() + # @show get_markerstrokecolor(opt, 1) + # @show get_markercolor(opt,1) + for (i, rng) in enumerate(segments) + seg_args = (arg[rng] for arg in args) + segment_opt = merge( segment_opt, pgfx_linestyle(opt, i) ) + segment_opt = merge( segment_opt, pgfx_marker(opt, i) ) + if st == :shape || series[:fillrange] !== nothing + segment_opt = merge( segment_opt, pgfx_fillstyle(opt, i) ) + end + segment_plot = series_func( + merge(series_opt, segment_opt), + PGFPlotsX.Coordinates(seg_args..., + meta = if !isnothing(opt[:marker_z]) + opt[:marker_z][rng] + else + nothing + end + ), + series[:fillrange] !== nothing ? "\\closedcycle" : "{}" + ) + push!(axis, segment_plot) + # add to legend? + if i == 1 && opt[:label] != "" && sp[:legend] != :none && should_add_to_legend(series) + push!( axis, PGFPlotsX.LegendEntry( opt[:label] ) + ) + end + end + # TODO: different seriestypes, histogramms, contours, etc. + # TODO: colorbars + # TODO: gradients # add series annotations anns = series[:series_annotations] for (xi,yi,str,fnt) in EachAnn(anns, series[:x], series[:y]) pgfx_add_annotation!(series_plot, xi, yi, PlotText(str, fnt), pgfx_thickness_scaling(series)) end - push!( axis, series_plot ) - if opt[:label] != "" && sp[:legend] != :none && should_add_to_legend(series) - push!( axis, PGFPlotsX.LegendEntry( opt[:label] ) - ) - end end push!( the_plot.elements[1], axis ) if length(plt.o.the_plot.elements) > 0 @@ -247,6 +241,8 @@ const _pgfx_series_extrastyle = KW( :sticks => "ycomb", :ysticks => "ycomb", :xsticks => "xcomb", + :scatter => "only marks", + :shape => "area legends" ) const _pgfx_framestyles = [:box, :axes, :origin, :zerolines, :grid, :none] diff --git a/test/test_pgfplotsx.jl b/test/test_pgfplotsx.jl index 03e9dce2..7a8c320a 100644 --- a/test/test_pgfplotsx.jl +++ b/test/test_pgfplotsx.jl @@ -36,9 +36,15 @@ end Plots._update_plot_object(pl) axis = Plots.pgfx_axes(pl.o)[1] @test count( x->x isa PGFPlotsX.LegendEntry, axis.contents ) == 5 - @test count( x->x isa PGFPlotsX.Plot, axis.contents ) == 5 - # TODO: marker size does not adjust - # TODO: marker stroke is incorrect + @test count( x->x isa PGFPlotsX.Plot, axis.contents ) == 104 # each marker is its own plot + marker = axis.contents[5] + @test marker isa PGFPlotsX.Plot + @show marker.options.dict |> keys + @test marker.options["mark"] == "none" + @test marker.options["mark options"]["color"] == convert(RGBA{Float64}, colorant"green") + @test marker.options["mark options"]["line width"] == 1 + + # TODO: marker stroke color is incorrect end # testset @testset "Plot in pieces" begin plot(rand(100) / 3, reg=true, fill=(0, :green))