titles and sub-layouts

This commit is contained in:
Thomas Breloff 2016-05-18 15:53:00 -04:00
parent 327f235af9
commit bfc3fc1dec
5 changed files with 106 additions and 38 deletions

View File

@ -161,8 +161,7 @@ _series_defaults[:subplot] = :auto # which subplot(s) does this se
const _plot_defaults = KW()
_plot_defaults[:title] = ""
_plot_defaults[:titlefont] = font(14)
_plot_defaults[:plot_title] = ""
_plot_defaults[:background_color] = colorant"white" # default for all backgrounds
_plot_defaults[:background_color_outside] = :match # background outside grid
_plot_defaults[:foreground_color] = :auto # default for all foregrounds, and title color
@ -184,6 +183,8 @@ _plot_defaults[:overwrite_figure] = true
const _subplot_defaults = KW()
_subplot_defaults[:title] = ""
_subplot_defaults[:title_location] = :center # also :left or :right
_subplot_defaults[:titlefont] = font(14)
_subplot_defaults[:background_color_subplot] = :match # default for other bg colors... match takes plot default
_subplot_defaults[:background_color_legend] = :match # background of legend
_subplot_defaults[:background_color_inside] = :match # background inside grid
@ -378,6 +379,7 @@ add_aliases(:aspect_ratio, :aspectratio, :axis_ratio, :axisratio, :ratio)
add_aliases(:match_dimensions, :transpose, :transpose_z)
add_aliases(:subplot, :sp, :subplt, :splt)
add_aliases(:projection, :proj)
add_aliases(:title_location, :title_loc, :titleloc)
# add all pluralized forms to the _keyAliases dict
@ -625,6 +627,12 @@ function preprocessArgs!(d::KW)
# # end
# end
# # if title is just a single string, then assume we want plot_title
# # TODO: make a decision if this is correct
# if haskey(d, :title) && typeof(d[:title]) <: AbstractString
# d[:plot_title] = pop!(d, :title)
# end
# handle line args
for arg in wraptuple(pop!(d, :line, ()))
processLineArg(d, arg)

View File

@ -293,13 +293,17 @@ end
function py_bbox_axis(ax, letter)
ticks = py_bbox_ticks(ax, letter)
labels = py_bbox_axislabel(ax, letter)
# @show ticks labels ticks+labels
letter == "x" && @show ticks labels ticks+labels
ticks + labels
end
# get a bounding box for the title area
function py_bbox_title(ax)
py_bbox(ax[:title])
bb = defaultbox
for s in (:title, :_left_title, :_right_title)
bb = bb + py_bbox(ax[s])
end
bb
end
# TODO: need to compute each of these by subtracting the plotarea position from
@ -318,6 +322,7 @@ function compute_min_padding(sp::Subplot{PyPlotBackend}, func::Function, mult::N
ax = sp.o
plotbb = py_bbox(ax)
# @show func, mult plotbb
# @show func, py_bbox_axis(ax, "x")
padding = 0mm
for bb in (py_bbox_axis(ax, "x"),
py_bbox_axis(ax, "y"),
@ -329,6 +334,13 @@ function compute_min_padding(sp::Subplot{PyPlotBackend}, func::Function, mult::N
end
# @show padding
end
# if func == top
# titlebbox = py_bbox_title(ax)
# padding = max(padding, height(titlebbox))
# @show titlebbox height(titlebbox),padding
# end
padding
end
@ -582,7 +594,7 @@ function _add_series(plt::Plot{PyPlotBackend}, series::Series)
# ax = getAxis(plt, d[:axis])
ax = getAxis(plt, series)
x, y, z = d[:x], d[:y], d[:z]
@show typeof((x,y,z))
# @show typeof((x,y,z))
xyargs = (st in _3dTypes ? (x,y,z) : (x,y))
# handle zcolor and get c/cmap
@ -1058,18 +1070,30 @@ end
function addPyPlotTicks(ax, ticks, letter)
ticks == :auto && return
axis = ax[symbol(letter,"axis")]
# tickfunc = symbol("set_", letter, "ticks")
# labfunc = symbol("set_", letter, "ticklabels")
if ticks == :none || ticks == nothing
ticks = zeros(0)
# ax[][:set_major_locator]
# ax[tickfunc]([])
# ax[labfunc]([])
kw = KW()
for dir in (:top,:bottom,:left,:right)
kw[dir] = kw[symbol(:label,dir)] = "off"
end
axis[:set_tick_params](;which="both", kw...)
return
end
ttype = ticksType(ticks)
tickfunc = symbol("set_", letter, "ticks")
labfunc = symbol("set_", letter, "ticklabels")
if ttype == :ticks
ax[tickfunc](ticks)
# ax[tickfunc](ticks)
axis[:set_ticks](ticks)
elseif ttype == :ticks_and_labels
ax[tickfunc](ticks[1])
ax[labfunc](ticks[2])
# ax[tickfunc](ticks[1])
# ax[labfunc](ticks[2])
axis[:set_ticks](ticks[1])
axis[:set_ticklabels](ticks[2])
else
error("Invalid input for $(isx ? "xticks" : "yticks"): ", ticks)
end
@ -1119,17 +1143,30 @@ function _update_plot(plt::Plot{PyPlotBackend}, d::KW)
# guidesz = get(d, :guidefont, spargs[:guidefont]).pointsize
# title
haskey(d, :title) && ax[:set_title](d[:title])
ax[:title][:set_fontsize](plt.plotargs[:titlefont].pointsize)
if haskey(spargs, :title)
loc = lowercase(string(spargs[:title_location]))
field = if loc == "left"
:_left_title
elseif loc == "right"
:_right_title
else
:title
end
ax[field][:set_text](spargs[:title])
ax[field][:set_fontsize](spargs[:titlefont].pointsize)
ax[field][:set_color](getPyPlotColor(spargs[:titlefont].color))
ax[:set_title](spargs[:title], loc = loc)
# TODO: set other font attributes
end
# axes = [ax]
# # handle right y axis
# axes = [getLeftAxis(figorax)]
# if usingRightAxis(plt)
# push!(axes, getRightAxis(figorax))
# if get(d, :yrightlabel, "") != ""
# if get(spargs, :yrightlabel, "") != ""
# rightax = getRightAxis(figorax)
# rightax[:set_ylabel](d[:yrightlabel])
# rightax[:set_ylabel](spargs[:yrightlabel])
# end
# end
@ -1137,7 +1174,7 @@ function _update_plot(plt::Plot{PyPlotBackend}, d::KW)
axissym = symbol(letter, :axis)
axis = spargs[axissym]
# @show axis
# DD(axis.d, "updateplot")
# DD(axis.spargs, "updateplot")
# @show haskey(ax, axissym)
haskey(ax, axissym) || continue
applyPyPlotScale(ax, axis[:scale], letter)
@ -1154,7 +1191,7 @@ function _update_plot(plt::Plot{PyPlotBackend}, d::KW)
lab[:set_fontsize](axis[:tickfont].pointsize)
lab[:set_rotation](axis[:rotation])
end
if get(d, :grid, false)
if get(spargs, :grid, false)
fgcolor = getPyPlotColor(spargs[:foreground_color_grid])
tmpax[axissym][:grid](true, color = fgcolor)
tmpax[:set_axisbelow](true)
@ -1169,20 +1206,20 @@ function _update_plot(plt::Plot{PyPlotBackend}, d::KW)
# axis, scale, lims, ticks, flip, lab, rotation =
# axis_symbols(letter, "axis", "scale", "lims", "ticks", "flip", "label", "rotation")
# haskey(ax, axis) || continue
# haskey(d, scale) && applyPyPlotScale(ax, d[scale], letter)
# haskey(d, lims) && addPyPlotLims(ax, d[lims], letter)
# haskey(d, ticks) && addPyPlotTicks(ax, d[ticks], letter)
# haskey(d, lab) && ax[symbol("set_", letter, "label")](d[lab])
# if get(d, flip, false)
# haskey(spargs, scale) && applyPyPlotScale(ax, spargs[scale], letter)
# haskey(spargs, lims) && addPyPlotLims(ax, spargs[lims], letter)
# haskey(spargs, ticks) && addPyPlotTicks(ax, spargs[ticks], letter)
# haskey(spargs, lab) && ax[symbol("set_", letter, "label")](spargs[lab])
# if get(spargs, flip, false)
# ax[symbol("invert_", letter, "axis")]()
# end
# for tmpax in axes
# tmpax[axis][:label][:set_fontsize](guidesz)
# for lab in tmpax[symbol("get_", letter, "ticklabels")]()
# lab[:set_fontsize](ticksz)
# haskey(d, rotation) && lab[:set_rotation](d[rotation])
# haskey(spargs, rotation) && lab[:set_rotation](spargs[rotation])
# end
# if get(d, :grid, false)
# if get(spargs, :grid, false)
# fgcolor = getPyPlotColor(plt.plotargs[:foreground_color_grid])
# tmpax[axis][:grid](true, color = fgcolor)
# tmpax[:set_axisbelow](true)
@ -1191,7 +1228,7 @@ function _update_plot(plt::Plot{PyPlotBackend}, d::KW)
# end
# do we want to change the aspect ratio?
aratio = get(d, :aspect_ratio, :none)
aratio = get(spargs, :aspect_ratio, :none)
if aratio != :none
ax[:set_aspect](isa(aratio, Symbol) ? string(aratio) : aratio, anchor = "C")
end
@ -1367,7 +1404,6 @@ function finalizePlot(plt::Plot{PyPlotBackend})
for asym in (:xaxis, :yaxis, :zaxis)
updateAxisColors(ax, sp.subplotargs[asym])
end
ax[:title][:set_color](getPyPlotColor(plt.plotargs[:titlefont].color))
end
drawfig(plt.o)
plt.layout.bbox = py_bbox_fig(plt)

View File

@ -52,7 +52,7 @@ function plot(args...; kw...)
# to the backend to finish backend-specific initialization
plt = Plot()
_update_plot_args(plt, d)
DD(plt.plotargs,"pargs")
# DD(plt.plotargs,"pargs")
plt.o = _create_backend_figure(plt)
# create the layout and subplots from the inputs
@ -61,7 +61,7 @@ function plot(args...; kw...)
# update the subplot/axis args from inputs, then pass to backend to init further
sp.plt = plt
_update_subplot_args(plt, sp, copy(d), idx)
DD(sp.subplotargs[:xaxis].d,"$idx")
# DD(sp.subplotargs[:xaxis].d,"$idx")
# TODO: i'd like to know what projection we're using by this point... can I push this off until later??
# I won't easily be able to auto-determine what series types are coming...

View File

@ -88,7 +88,7 @@ immutable SliceIt end
# the catch-all recipes
@recipe function f(::Type{SliceIt}, x, y, z)
@show "HERE", typeof((x,y,z))
# @show "HERE", typeof((x,y,z))
xs, _ = convertToAnyVector(x, d)
ys, _ = convertToAnyVector(y, d)
zs, _ = convertToAnyVector(z, d)
@ -111,9 +111,9 @@ immutable SliceIt end
# add a new series
di = copy(d)
xi, yi, zi = xs[mod1(i,mx)], ys[mod1(i,my)], zs[mod1(i,mz)]
@show i, typeof((xi, yi, zi))
# @show i, typeof((xi, yi, zi))
di[:x], di[:y], di[:z] = compute_xyz(xi, yi, zi)
@show i, typeof((di[:x], di[:y], di[:z]))
# @show i, typeof((di[:x], di[:y], di[:z]))
# handle fillrange
fr = fillranges[mod1(i,mf)]

View File

@ -262,6 +262,15 @@ end
# end
# end
calc_num_subplots(layout::AbstractLayout) = 1
function calc_num_subplots(layout::GridLayout)
tot = 0
for l in layout.grid
tot += calc_num_subplots(l)
end
tot
end
# pass the layout arg through
function build_layout(d::KW)
@ -286,8 +295,12 @@ end
# compute number of subplots
function build_layout(layout::GridLayout)
nr, nc = size(layout)
build_layout(layout, nr*nc)
# nr, nc = size(layout)
# build_layout(layout, nr*nc)
# recursively get the size of the grid
n = calc_num_subplots(layout)
build_layout(layout, n)
end
# n is the number of subplots
@ -298,11 +311,22 @@ function build_layout(layout::GridLayout, n::Integer)
i = 1
for r=1:nr, c=1:nc
i > n && break # only add n subplots
sp = Subplot(backend(), parent=layout)
layout[r,c] = sp
push!(subplots, sp)
spmap[(r,c)] = sp
i += 1
l = layout[r,c]
if isa(l, EmptyLayout)
sp = Subplot(backend(), parent=layout)
layout[r,c] = sp
push!(subplots, sp)
spmap[length(subplots)] = sp
i += 1
elseif isa(l, GridLayout)
# sub-grid
l, sps, m = build_layout(l)
append!(subplots, sps)
for (k,v) in m
spmap[k+length(subplots)] = v
end
i += length(sps)
end
end
layout, subplots, spmap
end