fixes; basic layout works

This commit is contained in:
Thomas Breloff 2016-05-17 23:37:47 -04:00
parent 33d9664df5
commit df7fc0c4df
3 changed files with 31 additions and 18 deletions

View File

@ -260,7 +260,9 @@ py_bbox_fig(plt::Plot) = py_bbox_fig(plt.o)
# compute a bounding box (with origin top-left), however pyplot gives coords with origin bottom-left
function py_bbox(obj)
fl, fr, fb, ft = get_extents(obj[:get_figure]())
# @show fl, fr, fb, ft
l, r, b, t = get_extents(obj)
# @show l, r, b, t, obj
BoundingBox(l*px, (ft-t)*px, (r-l)*px, (t-b)*px)
end
@ -276,6 +278,7 @@ function py_bbox_ticks(ax, letter)
# @show lab,lab[:get_text]()
bbox = py_bbox(lab)
bbox_union = bbox_union + bbox
# @show letter,bbox bbox_union
# @show bbox_union
end
bbox_union
@ -288,7 +291,10 @@ end
# get a bounding box for the whole axis
function py_bbox_axis(ax, letter)
py_bbox_ticks(ax, letter) + py_bbox_axislabel(ax, letter)
ticks = py_bbox_ticks(ax, letter)
labels = py_bbox_axislabel(ax, letter)
# @show ticks labels ticks+labels
ticks + labels
end
# get a bounding box for the title area
@ -301,9 +307,9 @@ end
# and aggregation (minimum or maximum) into a method to do this.
min_padding_left(layout::Subplot{PyPlotBackend}) = compute_min_padding(layout, left, 1)
min_padding_top(layout::Subplot{PyPlotBackend}) = compute_min_padding(layout, top, -1)
min_padding_top(layout::Subplot{PyPlotBackend}) = compute_min_padding(layout, top, 1)
min_padding_right(layout::Subplot{PyPlotBackend}) = compute_min_padding(layout, right, -1)
min_padding_bottom(layout::Subplot{PyPlotBackend}) = compute_min_padding(layout, bottom, 1)
min_padding_bottom(layout::Subplot{PyPlotBackend}) = compute_min_padding(layout, bottom,-1)
# loop over the guides and axes and compute how far they "stick out" from the plot area,
# so that we know the minimum padding we need to avoid cropping and overlapping text.
@ -311,12 +317,17 @@ min_padding_bottom(layout::Subplot{PyPlotBackend}) = compute_min_padding(layout,
function compute_min_padding(sp::Subplot{PyPlotBackend}, func::Function, mult::Number)
ax = sp.o
plotbb = py_bbox(ax)
# @show func, mult plotbb
padding = 0mm
for bb in (py_bbox_axis(ax, "x"),
py_bbox_axis(ax, "y"),
py_bbox_title(ax))
diff = func(plotbb) - func(bb)
padding = max(padding, mult * diff)
# @show diff,bb
if ispositive(width(bb)) && ispositive(height(bb))
padding = max(padding, mult * diff)
end
# @show padding
end
padding
end
@ -355,14 +366,16 @@ function update_position!(sp::Subplot{PyPlotBackend})
# plot_bb = plotarea_bbox(sp)
plot_bb = sp.plotarea
@show sp.bbox plot_bb
# @show sp.bbox plot_bb
# l = float(left(plot_bb) / px) / figw
# b = float(bottom(plot_bb) / px) / figh
# w = float(width(plot_bb) / px) / figw
mms = Float64[f(plot_bb).value for f in (left, bottom, width, height)]
@show mms
mms[2] = figh.value - mms[2]
# @show mms
pcts = mms ./ Float64[figw.value, figh.value, figw.value, figh.value]
@show pcts
# @show pcts
ax[:set_position](pcts)
end

View File

@ -127,7 +127,7 @@ function update_child_bboxes!(layout::GridLayout)
minpad_top = map(min_padding_top, layout.grid)
minpad_right = map(min_padding_right, layout.grid)
minpad_bottom = map(min_padding_bottom, layout.grid)
@show minpad_left minpad_top minpad_right minpad_bottom
# @show minpad_left minpad_top minpad_right minpad_bottom
# get the max horizontal (left and right) padding over columns,
# and max vertical (bottom and top) padding over rows
@ -136,22 +136,22 @@ function update_child_bboxes!(layout::GridLayout)
pad_top = maximum(minpad_top, 2)
pad_right = maximum(minpad_right, 1)
pad_bottom = maximum(minpad_bottom, 2)
@show pad_left pad_top pad_right pad_bottom
# @show pad_left pad_top pad_right pad_bottom
# scale this up to the total padding in each direction
total_pad_horizontal = (pad_left + pad_right) .* nc
total_pad_vertical = (pad_top + pad_bottom) .* nr
@show total_pad_horizontal total_pad_vertical
# @show total_pad_horizontal total_pad_vertical
# now we can compute the total plot area in each direction
total_plotarea_horizontal = width(layout) - total_pad_horizontal
total_plotarea_vertical = height(layout) - total_pad_vertical
@show total_plotarea_horizontal total_plotarea_vertical
# @show total_plotarea_horizontal total_plotarea_vertical
# normalize widths/heights so they sum to 1
denom_w = sum(layout.widths)
denom_h = sum(layout.heights)
@show denom_w, denom_h
# @show denom_w, denom_h
# we have all the data we need... lets compute the plot areas
for r=1:nr, c=1:nc
@ -159,7 +159,7 @@ function update_child_bboxes!(layout::GridLayout)
# get the top-left corner of this child
child_left = (c == 1 ? 0mm : right(layout[r, c-1].bbox))
child_top = (r == 1 ? 0mm : top(layout[r-1, c].bbox))
child_top = (r == 1 ? 0mm : bottom(layout[r-1, c].bbox))
# compute plot area
plotarea_left = child_left + pad_left[c]
@ -172,12 +172,12 @@ function update_child_bboxes!(layout::GridLayout)
child_width = pad_left[c] + plotarea_width + pad_right[c]
child_height = pad_top[r] + plotarea_height + pad_bottom[r]
child_bbox = BoundingBox(child_left, child_top, child_width, child_height)
@show (r,c) child_plotarea child_bbox
# @show (r,c) child_plotarea child_bbox
# the bounding boxes are currently relative to the parent, but we need them relative to the canvas
plotarea!(child, crop(layout.bbox, child_plotarea))
bbox!(child, crop(layout.bbox, child_bbox))
@show child_plotarea child_bbox
# @show child_plotarea child_bbox
# recursively update the child's children
update_child_bboxes!(child)

View File

@ -105,10 +105,10 @@ function Base.(:+)(bb1::BoundingBox, bb2::BoundingBox)
# return bb1
# end
l = min(left(bb1), left(bb2))
b = min(bottom(bb1), bottom(bb2))
t = min(top(bb1), top(bb2))
r = max(right(bb1), right(bb2))
t = max(top(bb1), top(bb2))
BoundingBox(l, t, r-l, t-b)
b = max(bottom(bb1), bottom(bb2))
BoundingBox(l, t, r-l, b-t)
end
# this creates a bounding box in the parent's scope, where the child bounding box