Skip to content

Commit

Permalink
GR - Plotly: improve title location and alignment (#4410)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-boikov authored Oct 2, 2022
1 parent 341348a commit 179cbc2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
35 changes: 25 additions & 10 deletions src/backends/gr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,18 @@ gr_markertype(k) = (
hline = -31,
)[k]

gr_halign(k) = (left = 1, hcenter = 2, right = 3)[k]
gr_valign(k) = (top = 1, vcenter = 3, bottom = 5)[k]
gr_halign(k) = (
left = GR.TEXT_HALIGN_LEFT,
hcenter = GR.TEXT_HALIGN_CENTER,
center = GR.TEXT_HALIGN_CENTER,
right = GR.TEXT_HALIGN_RIGHT,
)[k]
gr_valign(k) = (
top = GR.TEXT_VALIGN_TOP,
vcenter = GR.TEXT_VALIGN_HALF,
center = GR.TEXT_VALIGN_HALF,
bottom = GR.TEXT_VALIGN_BOTTOM,
)[k]

const gr_font_family = Dict(
# compat
Expand Down Expand Up @@ -1814,17 +1824,22 @@ function gr_add_title(sp, viewport_plotarea, viewport_subplot)
gr_set_font(titlefont(sp), sp)
loc = sp[:titlelocation]
if loc === :left
xpos = viewport_plotarea[1]
halign = GR.TEXT_HALIGN_LEFT
xpos, ypos = viewport_plotarea[1], viewport_subplot[4]
halign, valign = GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_TOP
elseif loc === :center
xpos, ypos = +(viewport_plotarea[1:2]...) / 2, viewport_subplot[4]
halign, valign = GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_TOP
elseif loc === :right
xpos = viewport_plotarea[2]
halign = GR.TEXT_HALIGN_RIGHT
xpos, ypos = viewport_plotarea[2], viewport_subplot[4]
halign, valign = GR.TEXT_HALIGN_RIGHT, GR.TEXT_VALIGN_TOP
else
xpos = gr_view_xcenter(viewport_plotarea)
halign = GR.TEXT_HALIGN_CENTER
xpos = gr_view_xposition(viewport_plotarea, loc[1])
ypos = gr_view_yposition(viewport_plotarea, loc[2])
halign = gr_halign(sp[:titlefonthalign])
valign = gr_valign(sp[:titlefontvalign])
end
GR.settextalign(halign, GR.TEXT_VALIGN_TOP)
gr_text(xpos, viewport_subplot[4], sp[:title])
GR.settextalign(halign, valign)
gr_text(xpos, ypos, sp[:title])
GR.restorestate()
end
end
Expand Down
3 changes: 3 additions & 0 deletions src/backends/pgfplotsx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ curly(obj) = "{$(string(obj))}"
latex_formatter(formatter::Symbol) = formatter in (:plain, :latex) ? formatter : :latex
latex_formatter(formatter::Function) = formatter

pgfx_halign(k) = (left = "left", hcenter = "center", center = "center", right = "right")[k]

function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend})
if !pgfx_plot.is_created || pgfx_plot.was_shown
pgfx_sanitize_plot!(plt)
Expand Down Expand Up @@ -150,6 +152,7 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend})
"color" => title_cstr,
"draw opacity" => alpha(title_cstr),
"rotate" => sp[:titlefontrotation],
"align" => pgfx_halign(sp[:titlefonthalign]),
),
"legend style" => pgfx_get_legend_style(sp),
"axis background/.style" =>
Expand Down
24 changes: 16 additions & 8 deletions src/backends/plotly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,28 @@ function plotly_layout(plt::Plot)
for sp in plt.subplots
spidx = multiple_subplots ? sp[:subplot_index] : ""
x_idx, y_idx = multiple_subplots ? plotly_link_indicies(plt, sp) : ("", "")
# add an annotation for the title... positioned horizontally relative to plotarea,
# but vertically just below the top of the subplot bounding box

# add an annotation for the title
if sp[:title] != ""
bb = plotarea(sp)
tpos = sp[:titlelocation]
xmm = if tpos === :left
left(bb)
if tpos === :left
xmm, ymm = left(bb), top(bbox(sp))
halign, valign = :left, :top
elseif tpos === :center
xmm, ymm = 0.5 * (left(bb) + right(bb)), top(bbox(sp))
halign, valign = :hcenter, :top
elseif tpos === :right
right(bb)
xmm, ymm = right(bb), top(bbox(sp))
halign, valign = :right, :top
else
0.5 * (left(bb) + right(bb))
xmm = left(bb) + tpos[1] * width(bb)
# inverting to make consistent with GR
ymm = bottom(bb) - tpos[2] * height(bb)
halign, valign = sp[:titlefonthalign], sp[:titlefontvalign]
end
titlex, titley = xy_mm_to_pcts(xmm, top(bbox(sp)), w * px, h * px)
title_font = font(titlefont(sp), :top)
titlex, titley = xy_mm_to_pcts(xmm, ymm, w * px, h * px)
title_font = font(titlefont(sp), halign = halign, valign = valign)
push!(
plotattributes_out[:annotations],
plotly_annotation_dict(titlex, titley, text(sp[:title], title_font)),
Expand Down

0 comments on commit 179cbc2

Please sign in to comment.