From 0e598cc51d514bb2444de91a7d039129bfad6512 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Mon, 13 Jun 2016 17:46:19 -0400 Subject: [PATCH] resolve relative coords for inset subplots --- src/Plots.jl | 4 ++-- src/colors.jl | 2 ++ src/layouts.jl | 29 +++++++++++++---------------- src/plot.jl | 44 +++++++++++++++++++++++--------------------- src/utils.jl | 2 ++ 5 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/Plots.jl b/src/Plots.jl index b5725c07..163ca115 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -124,9 +124,9 @@ export # --------------------------------------------------------- import Measures -import Measures: Length, AbsoluteLength, Measure, BoundingBox, mm, cm, inch, pt, width, height +import Measures: Length, AbsoluteLength, Measure, BoundingBox, mm, cm, inch, pt, width, height, w, h typealias BBox Measures.Absolute2DBox -export BBox, BoundingBox, mm, cm, inch, pt, px, pct +export BBox, BoundingBox, mm, cm, inch, pt, px, pct, w, h # --------------------------------------------------------- diff --git a/src/colors.jl b/src/colors.jl index 6d215e2c..d48c7c11 100644 --- a/src/colors.jl +++ b/src/colors.jl @@ -23,6 +23,8 @@ convertColor(c::Symbol) = parse(Colorant, string(c)) convertColor(c::Colorant) = c convertColor(cvec::AbstractVector) = map(convertColor, cvec) convertColor(c::ColorScheme) = c +convertColor(v::Void) = RGBA(0,0,0,0) +convertColor(b::Bool) = b ? RGBA(0,0,0,1) : RGBA(0,0,0,0) function convertColor(c, α::Real) c = convertColor(c) diff --git a/src/layouts.jl b/src/layouts.jl index bfc68830..cc1590be 100644 --- a/src/layouts.jl +++ b/src/layouts.jl @@ -138,8 +138,9 @@ bottom(layout::AbstractLayout) = bottom(bbox(layout)) width(layout::AbstractLayout) = width(bbox(layout)) height(layout::AbstractLayout) = height(bbox(layout)) -plotarea(layout::AbstractLayout) = defaultbox -plotarea!(layout::AbstractLayout, bbox::BoundingBox) = nothing +# pass these through to the bbox methods if there's no plotarea +plotarea(layout::AbstractLayout) = bbox(layout) +plotarea!(layout::AbstractLayout, bb::BoundingBox) = bbox!(layout, bb) attr(layout::AbstractLayout, k::Symbol) = layout.attr[k] attr(layout::AbstractLayout, k::Symbol, v) = get(layout.attr, k, v) @@ -354,23 +355,19 @@ function update_child_bboxes!(layout::GridLayout, minimum_perimeter = [0mm,0mm,0 end end +# for each inset (floating) subplot, resolve the relative position +# to absolute canvas coordinates, relative to the parent's plotarea function update_inset_bboxes!(plt::Plot) for sp in plt.inset_subplots - relative_bbox = sp[:relative_bbox] - # TODO: need to handle percentages... right now only AbsoluteLength works + p_area = Measures.resolve(plotarea(sp.parent), sp[:relative_bbox]) + # @show bbox(sp.parent) sp[:relative_bbox] p_area + plotarea!(sp, p_area) - bb = bbox!(sp, bbox( - left(sp.parent) + left(relative_bbox), - top(sp.parent) + top(relative_bbox), - width(relative_bbox), - height(relative_bbox) - )) - - plotarea!(sp, bbox( - left(bb) + leftpad(sp), - top(bb) + toppad(sp), - width(bb) - leftpad(sp) - rightpad(sp), - height(bb) - toppad(sp) - bottompad(sp) + bbox!(sp, bbox( + left(p_area) - leftpad(sp), + top(p_area) - toppad(sp), + width(p_area) + leftpad(sp) + rightpad(sp), + height(p_area) + toppad(sp) + bottompad(sp) )) end end diff --git a/src/plot.jl b/src/plot.jl index 3f4fbc99..52181c75 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -353,29 +353,31 @@ function _plot!(plt::Plot, d::KW, args...) sp.attr[:subplot_index] = idx end - # handle inset subplots - insets = plt[:inset_subplots] - if insets != nothing - for (parent, bb) in insets - P = typeof(parent) - if P <: Integer - parent = plt.subplots[parent] - elseif P == Symbol - parent = plt.spmap[parent] - else - parent = plt.layout - end - sp = Subplot(backend(), parent=parent) - sp.plt = plt - sp.attr[:relative_bbox] = bb - push!(plt.subplots, sp) - sp.attr[:subplot_index] = length(plt.subplots) - push!(plt.inset_subplots, sp) - end - end - plt.init = true end + + + # handle inset subplots + insets = plt[:inset_subplots] + if insets != nothing + for inset in insets + parent, bb = is_2tuple(inset) ? inset : (nothing, inset) + P = typeof(parent) + if P <: Integer + parent = plt.subplots[parent] + elseif P == Symbol + parent = plt.spmap[parent] + else + parent = plt.layout + end + sp = Subplot(backend(), parent=parent) + sp.plt = plt + sp.attr[:relative_bbox] = bb + push!(plt.subplots, sp) + sp.attr[:subplot_index] = length(plt.subplots) + push!(plt.inset_subplots, sp) + end + end # just in case the backend needs to set up the plot (make it current or something) _prepare_plot_object(plt) diff --git a/src/utils.jl b/src/utils.jl index 66bc8d53..b9dbf5cd 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -281,6 +281,8 @@ ismatrix(::Any) = false isscalar(::Real) = true isscalar(::Any) = false +is_2tuple(v) = typeof(v) <: Tuple && length(v) == 2 + isvertical(d::KW) = get(d, :orientation, :vertical) in (:vertical, :v, :vert)