Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Title location and alignment #4410

Merged
merged 11 commits into from
Oct 2, 2022
35 changes: 27 additions & 8 deletions src/backends/gr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1814,17 +1814,36 @@ 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 = sp[:titlefonthalign]
valign = sp[:titlefontvalign]
if halign == :left
halign = GR.TEXT_HALIGN_LEFT
elseif halign == :hcenter || halign == :center
halign = GR.TEXT_HALIGN_CENTER
elseif halign == :right
halign = GR.TEXT_HALIGN_RIGHT
end
if valign == :top
valign = GR.TEXT_VALIGN_TOP
elseif valign == :vcenter || valign == :center
valign = GR.TEXT_VALIGN_HALF
elseif valign == :bottom
valign = GR.TEXT_VALIGN_BOTTOM
end
ivan-boikov marked this conversation as resolved.
Show resolved Hide resolved
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
9 changes: 9 additions & 0 deletions src/backends/pgfplotsx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend})
title_cstr = plot_color(sp[:titlefontcolor])
bgc_inside = plot_color(sp[:background_color_inside])
update_clims(sp)
title_halign = sp[:titlefonthalign]
if title_halign === :left
title_halign = "left"
elseif title_halign === :hcenter || title_halign === :center
title_halign = "center"
elseif title_halign === :right
title_halign = "right"
end
axis_opt = Options(
"point meta max" => get_clims(sp)[2],
"point meta min" => get_clims(sp)[1],
Expand All @@ -150,6 +158,7 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend})
"color" => title_cstr,
"draw opacity" => alpha(title_cstr),
"rotate" => sp[:titlefontrotation],
"align" => title_halign,
),
"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