proper grid layout in macro; pyplot isx fix

This commit is contained in:
Thomas Breloff 2016-05-30 14:56:32 -05:00
parent 625c92a985
commit e9d95a85b7
3 changed files with 107 additions and 84 deletions

View File

@ -963,7 +963,7 @@ function addPyPlotTicks(ax, ticks, letter)
axis[:set_ticks](ticks[1])
axis[:set_ticklabels](ticks[2])
else
error("Invalid input for $(isx ? "xticks" : "yticks"): ", ticks)
error("Invalid input for $(letter)ticks: $ticks")
end
end

View File

@ -417,7 +417,7 @@ function build_layout(layout::GridLayout, n::Integer)
i = 0
for r=1:nr, c=1:nc
l = layout[r,c]
if isa(l, EmptyLayout)
if isa(l, EmptyLayout) && !get(l.attr, :blank, false)
sp = Subplot(backend(), parent=layout)
layout[r,c] = sp
push!(subplots, sp)
@ -451,7 +451,7 @@ function build_layout(layout::GridLayout, numsp::Integer, plts::AVec{Plot})
i = 0
for r=1:nr, c=1:nc
l = layout[r,c]
if isa(l, EmptyLayout)
if isa(l, EmptyLayout) && !get(l.attr, :blank, false)
plt = shift!(plts) # grab the first plot out of the list
layout[r,c] = plt.layout
append!(subplots, plt.subplots)
@ -506,40 +506,120 @@ function add_layout_pct!(kw::KW, v::Number, idx::Integer)
(idx == 2 || nidx == 1) && (kw[:h] = v*pct)
end
isrow(v) = isa(v, Expr) && v.head in (:hcat,:row)
iscol(v) = isa(v, Expr) && v.head == :vcat
rowsize(v) = isrow(v) ? length(v.args) : 1
function create_grid(expr::Expr)
cellsym = gensym(:cell)
constructor = if expr.head == :vcat
:(let
$cellsym = GridLayout($(length(expr.args)), 1)
$([:($cellsym[$i,1] = $(create_grid(expr.args[i]))) for i=1:length(expr.args)]...)
$cellsym
end)
elseif expr.head in (:hcat,:row)
:(let
$cellsym = GridLayout(1, $(length(expr.args)))
$([:($cellsym[1,$i] = $(create_grid(expr.args[i]))) for i=1:length(expr.args)]...)
$cellsym
# cellsym = gensym(:cell)
@show expr
if iscol(expr)
create_grid_vcat(expr)
# rowsizes = map(rowsize, expr.args)
# rmin, rmax = extrema(rowsizes)
# if rmin > 0 && rmin == rmax
# # we have a grid... build the whole thing
# # note: rmin is the number of columns
# nr = length(expr.args)
# nc = rmin
#
# :(let cell = GridLayout($nr, $nc)
# $([:(cell[$r,$c] = $(create_grid(expr.args[r], c))) for r=1:nr, c=1:nc]...)
# for r=1:nr
# layout = $(create_grid(expr.args[r])
# cell[r,]
# $([:($cellsym[$r,1] = $(create_grid(expr.args[r]))) for r=1:length(expr.args)]...)
# $cellsym
# end)
# else
# # otherwise just build one row at a time
# :(let
# $cellsym = GridLayout($(length(expr.args)), 1)
# $([:($cellsym[$i,1] = $(create_grid(expr.args[i]))) for i=1:length(expr.args)]...)
# $cellsym
# end)
# end
elseif isrow(expr)
:(let cell = GridLayout(1, $(length(expr.args)))
$([:(cell[1,$i] = $(create_grid(v))) for (i,v) in enumerate(expr.args)]...)
cell
end)
# :(let
# $cellsym = GridLayout(1, $(length(expr.args)))
# $([:($cellsym[1,$i] = $(create_grid(expr.args[i]))) for i=1:length(expr.args)]...)
# $cellsym
# end)
elseif expr.head == :curly
# length(expr.args) == 3 || error("Should be width and height in curly. Got: ", expr.args)
# s,w,h = expr.args
s = expr.args[1]
kw = KW()
for (i,arg) in enumerate(expr.args[2:end])
add_layout_pct!(kw, arg, i, length(expr.args)-1)
end
# @show kw
:(EmptyLayout(label = $(QuoteNode(s)), width = $(get(kw, :w, QuoteNode(:auto))), height = $(get(kw, :h, QuoteNode(:auto)))))
create_grid_curly(expr)
else
# if it's something else, just return that (might be an existing layout?)
expr
end
end
function create_grid_vcat(expr::Expr)
rowsizes = map(rowsize, expr.args)
rmin, rmax = extrema(rowsizes)
@show rmin, rmax
if rmin > 0 && rmin == rmax
# we have a grid... build the whole thing
# note: rmin is the number of columns
nr = length(expr.args)
nc = rmin
@show nr, nc
body = Expr(:block)
for r=1:nr
arg = expr.args[r]
@show r, arg
if isrow(arg)
for (c,item) in enumerate(arg.args)
push!(body.args, :(cell[$r,$c] = $(create_grid(item))))
end
else
push!(body.args, :(cell[$r,1] = $(create_grid(arg))))
end
end
@show body
:(let cell = GridLayout($nr, $nc)
$body
cell
end)
# :(let cell = GridLayout($nr, $nc)
# $([:(cell[$r,$c] = $(create_grid(expr.args[r], c))) for r=1:nr, c=1:nc]...)
# for r=1:nr
# layout = $(create_grid(expr.args[r])
# cell[r,]
# $([:($cellsym[$r,1] = $(create_grid(expr.args[r]))) for r=1:length(expr.args)]...)
# $cellsym
# end)
else
# otherwise just build one row at a time
:(let cell = GridLayout($(length(expr.args)), 1)
$([:(cell[$i,1] = $(create_grid(v))) for (i,v) in enumerate(expr.args)]...)
cell
end)
# :(let
# $cellsym = GridLayout($(length(expr.args)), 1)
# $([:($cellsym[$i,1] = $(create_grid(expr.args[i]))) for i=1:length(expr.args)]...)
# $cellsym
# end)
end
end
function create_grid_curly(expr::Expr)
s = expr.args[1]
kw = KW()
for (i,arg) in enumerate(expr.args[2:end])
add_layout_pct!(kw, arg, i, length(expr.args)-1)
end
# @show kw
:(EmptyLayout(label = $(QuoteNode(s)), width = $(get(kw, :w, QuoteNode(:auto))), height = $(get(kw, :h, QuoteNode(:auto)))))
end
function create_grid(s::Symbol)
:(EmptyLayout(label = $(QuoteNode(s))))
:(EmptyLayout(label = $(QuoteNode(s)), blank = $(s == :_)))
end
macro layout(mat::Expr)

View File

@ -71,24 +71,17 @@ function plot(plt1::Plot, plts_tail::Plot...; kw...)
d = KW(kw)
preprocessArgs!(d)
# create a layout, but don't add subplots... we expect nplts == layout capacity
# TODO: move this to layouts.jl
# plts = vcat(plt1, plts)
# build our plot vector
# build our plot vector from the args
n = length(plts_tail) + 1
plts = Array(Plot, n)
plts[1] = plt1
for (i,plt) in enumerate(plts_tail)
plts[i+1] = plt
end
# plts[2:end] = plts_tail
# @show typeof(plts),n
# compute the layout
layout = layout_args(d, n)[1]
num_sp = sum([length(p.subplots) for p in plts])
# @show typeof(layout), num_sp
# create a new plot object, with subplot list/map made of existing subplots.
# note: we create a new backend figure for this new plot object
@ -108,7 +101,6 @@ function plot(plt1::Plot, plts_tail::Plot...; kw...)
# create the layout and initialize the subplots
plt.layout, plt.subplots, plt.spmap = build_layout(layout, num_sp, copy(plts))
# @show map(typeof, (plt.layout, plt.subplots, plt.spmap))
for (idx, sp) in enumerate(plt.subplots)
_initialize_subplot(plt, sp)
serieslist = series_list(sp)
@ -126,55 +118,6 @@ function plot(plt1::Plot, plts_tail::Plot...; kw...)
gui()
end
plt
# _update_plot_args(plt, d)
# plt.o = _create_backend_figure(plt)
#
# # create the layout and subplots from the inputs
# plt.layout, plt.subplots, plt.spmap = build_layout(plt.attr)
# for (idx,sp) in enumerate(plt.subplots)
# sp.plt = plt
# sp.attr[:subplot_index] = idx
# # _update_subplot_args(plt, sp, copy(d), idx)
# end
#
# plt.init = true
#
# nr, nc = size(layout)
# subplots = Subplot[]
# spmap = SubplotMap()
# i = 0
# for r=1:nr, c=1:nc
# l = layout[r,c]
# if isa(l, EmptyLayout)
# i += 1
# plt = plts[i]
# layout[r,c] = plt.layout
# append!(subplots, plt.subplots)
# merge!(spmap, plt.spmap)
# # if init_sp
# # sp = Subplot(backend(), parent=layout)
# # layout[r,c] = sp
# # push!(subplots, sp)
# # spmap[attr(l,:label,gensym())] = sp
# # end
# if hasattr(l,:width)
# layout.widths[c] = attr(l,:width)
# end
# if hasattr(l,:height)
# layout.heights[r] = attr(l,:height)
# end
# elseif isa(l, GridLayout)
# # sub-grid
# l, sps, m = build_layout(l, n-i)
# append!(subplots, sps)
# merge!(spmap, m)
# i += length(sps)
# end
# i >= n && break # only add n subplots
# end
# layout, subplots, spmap
end