From 65368aac22821965f27564d1395d8e4135bf6445 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Mon, 5 Jul 2021 15:22:33 +0200 Subject: [PATCH] custom implementation for pgfplotsx backend --- src/args.jl | 2 ++ src/backends/pgfplotsx.jl | 45 ++++++++++++++++++++++++++------------- src/pipeline.jl | 6 +----- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/args.jl b/src/args.jl index c69c712e..a271a990 100644 --- a/src/args.jl +++ b/src/args.jl @@ -331,6 +331,7 @@ const _series_defaults = KW( const _plot_defaults = KW( :plot_title => "", + :plot_titleindex => 0, :plot_titlefontsize => 16, :plot_titlelocation => :center, # also :left or :right :plot_titlefontfamily => :match, @@ -1365,6 +1366,7 @@ const _match_map = KW( :colorbar_tickfontcolor => :foreground_color_subplot, :plot_titlefontfamily => :fontfamily, :plot_titlefontcolor => :foreground_color, + :plot_titleindex => 0, :tickfontcolor => :foreground_color_text, :guidefontcolor => :foreground_color_guide, :annotationfontfamily => :fontfamily_subplot, diff --git a/src/backends/pgfplotsx.jl b/src/backends/pgfplotsx.jl index 438c007e..a5a6408a 100644 --- a/src/backends/pgfplotsx.jl +++ b/src/backends/pgfplotsx.jl @@ -118,11 +118,24 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) end for sp in plt.subplots - bb1 = sp.plotarea bb2 = bbox(sp) + dx, dy = bb2.x0 + sp_width = width(bb2) + sp_height = height(bb2) + if sp[:subplot_index] == plt[:plot_titleindex] + x = dx + sp_width / 2 - 10mm # FIXME: get rid of magic constant + y = dy + sp_height / 2 + @show width(bbox(plt.layout.grid[2])) + @show sp_width + pgfx_add_annotation!(the_plot, x, y, PlotText(plt[:plot_title], plottitlefont(plt)), pgfx_thickness_scaling(plt); + cs = "", + options = PGFPlotsX.Options("anchor" => "center") + ) + continue + end + sp_width = width(bb2) sp_height = height(bb2) - dx, dy = bb2.x0 lpad = leftpad(sp) + sp[:left_margin] rpad = rightpad(sp) + sp[:right_margin] tpad = toppad(sp) + sp[:top_margin] @@ -131,7 +144,6 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) dy += tpad axis_height = sp_height - (tpad + bpad) axis_width = sp_width - (rpad + lpad) - title_cstr = plot_color(sp[:titlefontcolor]) title_a = alpha(title_cstr) title_loc = sp[:titlelocation] @@ -999,24 +1011,27 @@ function pgfx_marker(plotattributes, i = 1) ) end -function pgfx_add_annotation!(o, x, y, val, thickness_scaling = 1) +function pgfx_add_annotation!(o, x, y, val, thickness_scaling = 1; cs = "axis cs:", options = PGFPlotsX.Options()) # Construct the style string. cstr = val.font.color a = alpha(cstr) push!( o, - [ + join([ "\\node", - PGFPlotsX.Options( - get((center = "", left = "right", right = "left"), val.font.halign, "") => - nothing, - "color" => cstr, - "draw opacity" => convert(Float16, a), - "rotate" => val.font.rotation, - "font" => pgfx_font(val.font.pointsize, thickness_scaling), - ), - " at (axis cs:$x, $y) {$(val.str)};", - ], + sprint(PGFPlotsX.print_tex, merge( + PGFPlotsX.Options( + get((hcenter = "", left = "right", right = "left"), val.font.halign, "") => + nothing, + "color" => cstr, + "draw opacity" => convert(Float16, a), + "rotate" => val.font.rotation, + "font" => pgfx_font(val.font.pointsize, thickness_scaling), + ), + options + )), + string(" at (", cs, x, ",", y, ") {", val.str, "};"), + ]), ) end diff --git a/src/pipeline.jl b/src/pipeline.jl index bf84c051..2f37a29b 100644 --- a/src/pipeline.jl +++ b/src/pipeline.jl @@ -287,11 +287,6 @@ function _add_plot_title!(plt) if plot_title != "" the_layout = plt.layout vspan = plt[:plot_titlevspan] - if plt.backend isa PGFPlotsXBackend - # FIXME: we hit a 45pt height limit: - # https://github.com/pgf-tikz/pgfplots/blob/3bc2f42258fbfac9fe50b2978459da7e76fc046c/tex/generic/pgfplots/pgfplots.scaling.code.tex#L153 - vspan = max(vspan, .16) - end plt.layout = grid(2, 1, heights=(vspan, 1 - vspan)) plt.layout.grid[1, 1] = subplot = Subplot(plt.backend, parent=plt.layout[1, 1]) plt.layout.grid[2, 1] = the_layout @@ -302,6 +297,7 @@ function _add_plot_title!(plt) end plt[:force_minpad] = nothing, 0px, nothing, 0px subplot[:subplot_index] = last(plt.subplots)[:subplot_index] + 1 + plt[:plot_titleindex] = subplot[:subplot_index] subplot[:framestyle] = :none subplot[:margin] = 0px push!(plt.subplots, subplot)