Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
jk/ticklabel auto alignment (#633)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrumbiegel authored Feb 7, 2021
1 parent e69d9d8 commit e5f5a34
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 67 deletions.
11 changes: 5 additions & 6 deletions docs/src/makielayout/layoutables_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,20 @@ Axis(fig[1, 1])
# vertical colorbars
Colorbar(fig[1, 2], width = 25, limits = (0, 10), colormap = :viridis,
flipaxisposition = false, ticklabelalign = (:right, :center))
flipaxis = false)
Colorbar(fig[1, 3], width = 25, limits = (0, 5),
colormap = cgrad(:Spectral, 5, categorical = true))
Colorbar(fig[1, 4], width = 25, limits = (-1, 1), colormap = :heat,
highclip = :cyan, lowclip = :red, label = "Temperature")
# horizontal colorbars
Colorbar(fig[2, 1], height = 25, limits = (0, 10), colormap = :viridis,
vertical = false, ticklabelalign = (:center, :bottom))
vertical = false)
Colorbar(fig[3, 1], height = 25, limits = (0, 5),
colormap = cgrad(:Spectral, 5, categorical = true), vertical = false,
ticklabelalign = (:center, :bottom))
colormap = cgrad(:Spectral, 5, categorical = true), vertical = false)
Colorbar(fig[4, 1], height = 25, limits = (-1, 1), colormap = :heat,
label = "Temperature", vertical = false, flipaxisposition = false,
ticklabelalign = (:center, :top), highclip = :cyan, lowclip = :red)
label = "Temperature", vertical = false, flipaxis = false,
highclip = :cyan, lowclip = :red)
fig
Expand Down
1 change: 0 additions & 1 deletion src/makielayout/MakieLayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export hidexdecorations!, hideydecorations!, hidedecorations!, hidespines!
export tight_xticklabel_spacing!, tight_yticklabel_spacing!, tight_ticklabel_spacing!, tightlimits!
export layoutscene
export set_close_to!
export xaxis_bottom!, xaxis_top!, yaxis_left!, yaxis_right!
export labelslider!, labelslidergrid!
export addmouseevents!
export interactions, register_interaction!, deregister_interaction!, activate_interaction!, deactivate_interaction!
Expand Down
10 changes: 6 additions & 4 deletions src/makielayout/defaultattributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ function default_attributes(::Type{Axis}, scene)
"The counterclockwise rotation of the yticklabels in radians."
yticklabelrotation = 0f0
"The horizontal and vertical alignment of the xticklabels."
xticklabelalign = (:center, :top)
xticklabelalign = AbstractPlotting.automatic
"The horizontal and vertical alignment of the yticklabels."
yticklabelalign = (:right, :center)
yticklabelalign = AbstractPlotting.automatic
"The size of the xtick marks."
xticksize = 10f0
"The size of the ytick marks."
Expand Down Expand Up @@ -302,7 +302,9 @@ function default_attributes(::Type{Colorbar}, scene)
"The color of the tick marks."
tickcolor = RGBf0(0, 0, 0)
"The horizontal and vertical alignment of the tick labels."
ticklabelalign = (:left, :center)
ticklabelalign = AbstractPlotting.automatic
"The rotation of the ticklabels"
ticklabelrotation = 0f0
"The line width of the spines."
spinewidth = 1f0
"Controls if the top spine is visible."
Expand All @@ -328,7 +330,7 @@ function default_attributes(::Type{Colorbar}, scene)
"Controls if the colorbar is oriented vertically."
vertical = true
"Flips the axis to the right if vertical and to the top if horizontal."
flipaxisposition = true
flipaxis = true
"Flips the colorbar label if the axis is vertical."
flip_vertical_label = false
"The width setting of the colorbar."
Expand Down
43 changes: 0 additions & 43 deletions src/makielayout/helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,49 +323,6 @@ function subtheme(scene, key::Symbol)
sub
end

"""
xaxis_top!(la::Axis)
Move the x-axis to the top, while correctly aligning the tick labels at the bottom.
"""
function xaxis_top!(la::Axis)
la.xaxisposition = :top
la.xticklabelalign = (la.xticklabelalign[][1], :bottom)
nothing
end

"""
xaxis_bottom!(la::Axis)
Move the x-axis to the bottom, while correctly aligning the tick labels at the top.
"""
function xaxis_bottom!(la::Axis)
la.xaxisposition = :bottom
la.xticklabelalign = (la.xticklabelalign[][1], :top)
nothing
end

"""
yaxis_left!(la::Axis)
Move the y-axis to the left, while correctly aligning the tick labels at the right.
"""
function yaxis_left!(la::Axis)
la.yaxisposition = :left
la.yticklabelalign = (:right, la.yticklabelalign[][2])
nothing
end

"""
yaxis_right!(la::Axis)
Move the y-axis to the right, while correctly aligning the tick labels at the left.
"""
function yaxis_right!(la::Axis)
la.yaxisposition = :right
la.yticklabelalign = (:left, la.yticklabelalign[][2])
nothing
end

"""
labelslider!(scene, label, range; format = string, sliderkw = Dict(), labelkw = Dict(), valuekw = Dict(), layoutkw...)
Expand Down
23 changes: 12 additions & 11 deletions src/makielayout/layoutables/colorbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ function layoutable(::Type{<:Colorbar}, fig_or_scene; bbox = nothing, kwargs...)

@extract attrs (
label, labelcolor, labelsize, labelvisible, labelpadding, ticklabelsize,
ticklabelspace, labelfont, ticklabelfont, ticklabelcolor,
ticklabelspace, labelfont, ticklabelfont, ticklabelcolor, ticklabelrotation,
ticklabelsvisible, ticks, tickformat, ticksize, ticksvisible, ticklabelpad, tickalign,
tickwidth, tickcolor, spinewidth, topspinevisible,
rightspinevisible, leftspinevisible, bottomspinevisible, topspinecolor,
leftspinecolor, rightspinecolor, bottomspinecolor, colormap, limits,
halign, valign, vertical, flipaxisposition, ticklabelalign, flip_vertical_label,
halign, valign, vertical, flipaxis, ticklabelalign, flip_vertical_label,
nsteps, highclip, lowclip,
minorticksvisible, minortickalign, minorticksize, minortickwidth, minortickcolor, minorticks)

Expand Down Expand Up @@ -258,17 +258,17 @@ function layoutable(::Type{<:Colorbar}, fig_or_scene; bbox = nothing, kwargs...)

decorations[:spines] = lines!(topscene, borderpoints, linewidth = spinewidth, color = topspinecolor)

axispoints = lift(barbox, vertical, flipaxisposition) do scenearea,
vertical, flipaxisposition
axispoints = lift(barbox, vertical, flipaxis) do scenearea,
vertical, flipaxis

if vertical
if flipaxisposition
if flipaxis
(bottomright(scenearea), topright(scenearea))
else
(bottomleft(scenearea), topleft(scenearea))
end
else
if flipaxisposition
if flipaxis
(topleft(scenearea), topright(scenearea))
else
(bottomleft(scenearea), bottomright(scenearea))
Expand All @@ -277,13 +277,14 @@ function layoutable(::Type{<:Colorbar}, fig_or_scene; bbox = nothing, kwargs...)

end

axis = LineAxis(topscene, endpoints = axispoints, flipped = flipaxisposition,
axis = LineAxis(topscene, endpoints = axispoints, flipped = flipaxis,
limits = limits, ticklabelalign = ticklabelalign, label = label,
labelpadding = labelpadding, labelvisible = labelvisible, labelsize = labelsize,
labelcolor = labelcolor,
labelfont = labelfont, ticklabelfont = ticklabelfont, ticks = ticks, tickformat = tickformat,
ticklabelsize = ticklabelsize, ticklabelsvisible = ticklabelsvisible, ticksize = ticksize,
ticksvisible = ticksvisible, ticklabelpad = ticklabelpad, tickalign = tickalign,
ticklabelrotation = ticklabelrotation,
tickwidth = tickwidth, tickcolor = tickcolor, spinewidth = spinewidth,
ticklabelspace = ticklabelspace, ticklabelcolor = ticklabelcolor,
spinecolor = :transparent, spinevisible = :false, flip_vertical_label = flip_vertical_label,
Expand All @@ -293,20 +294,20 @@ function layoutable(::Type{<:Colorbar}, fig_or_scene; bbox = nothing, kwargs...)

decorations[:axis] = axis

onany(axis.protrusion, vertical, flipaxisposition) do axprotrusion,
vertical, flipaxisposition
onany(axis.protrusion, vertical, flipaxis) do axprotrusion,
vertical, flipaxis


left, right, top, bottom = 0f0, 0f0, 0f0, 0f0

if vertical
if flipaxisposition
if flipaxis
right += axprotrusion
else
left += axprotrusion
end
else
if flipaxisposition
if flipaxis
top += axprotrusion
else
bottom += axprotrusion
Expand Down
39 changes: 38 additions & 1 deletion src/makielayout/lineaxis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,48 @@ function LineAxis(parent::Scene; kwargs...)
)
decorations[:minorticklines] = minorticklines

realticklabelalign = lift(ticklabelalign, pos_extents_horizontal, flipped, ticklabelrotation, typ = Any) do al, (pos, ex, hor), fl, rot
if al !== AbstractPlotting.automatic
return al
end
if rot == 0 || !(rot isa Real)
if hor
(:center, fl ? :bottom : :top)
else
(fl ? :left : :right, :center)
end
elseif rot pi/2
if hor
(fl ? :left : :right, :center)
else
(:center, fl ? :top : :bottom)
end
elseif rot -pi/2
if hor
(fl ? :right : :left, :center)
else
(:center, fl ? :bottom : :top)
end
elseif rot > 0
if hor
(fl ? :left : :right, fl ? :bottom : :top)
else
(fl ? :left : :right, :center)
end
elseif rot < 0
if hor
(fl ? :right : :left, fl ? :bottom : :top)
else
(fl ? :left : :right, :center)
end
end
end

ticklabelannosnode = Node(Tuple{String, Point2f0}[])
ticklabels = annotations!(
parent,
ticklabelannosnode,
align = ticklabelalign,
align = realticklabelalign,
rotation = ticklabelrotation,
textsize = ticklabelsize,
font = ticklabelfont,
Expand Down
2 changes: 1 addition & 1 deletion test/ReferenceTests/src/tests/figures_and_makielayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ end
lines!(fig[2, 1:2][1, 2], cumsum(rand(30)), color = :red, linewidth = 10)
surface(fig[1, 2], collect(1.0:40), collect(1.0:40), (x, y) -> 10 * cos(x) * sin(y))
fig[2, 1:2][2, :] = Colorbar(fig, vertical = false,
height = 20, ticklabelalign = (:center, :top), flipaxisposition = false)
height = 20, ticklabelalign = (:center, :top), flipaxis = false)
fig[3, :] = Menu(fig, options = ["A", "B", "C"])
lt = fig[0, :] = Label(fig, "Figure Demo")
fig[5, :] = Textbox(fig)
Expand Down

0 comments on commit e5f5a34

Please sign in to comment.