From ebf92d30332ea2e5d0ecd3eab83671e77694fc1b Mon Sep 17 00:00:00 2001 From: MA Laforge Date: Mon, 23 Jan 2017 21:25:55 -0500 Subject: [PATCH] Add support for layout & plot size. Also: Add basic support for polar plots. Add support for plot title & background color of entire figure. --- src/backends/inspectdr.jl | 61 ++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 11 deletions(-) diff --git a/src/backends/inspectdr.jl b/src/backends/inspectdr.jl index fea44064..af95c76a 100644 --- a/src/backends/inspectdr.jl +++ b/src/backends/inspectdr.jl @@ -38,7 +38,7 @@ const _inspectdr_attr = merge_with_base_supported([ # :ribbon, :quiver, :arrow, # :orientation, :overwrite_figure, -# :polar, + :polar, # :normalize, :weights, # :contours, :aspect_ratio, :match_dimensions, @@ -66,6 +66,9 @@ const _inspectdr_scale = [:identity, :ln, :log2, :log10] is_marker_supported(::InspectDRBackend, shape::Shape) = true +_inspectdr_to_pixels(bb::BoundingBox) = + InspectDR.BoundingBox(to_pixels(left(bb)), to_pixels(right(bb)), to_pixels(top(bb)), to_pixels(bottom(bb))) + #Do we avoid Map to avoid possible pre-comile issues? function _inspectdr_mapglyph(s::Symbol) s == :rect && return :square @@ -237,6 +240,12 @@ function _series_added(plt::Plot{InspectDRBackend}, series::Series) _vectorize(v) = isa(v, Vector)? v: collect(v) #InspectDR only supports vectors x = _vectorize(series[:x]); y = _vectorize(series[:y]) + #No support for polar grid... but can still perform polar transformation: + if ispolar(sp) + Θ = x; r = y + x = r.*cos(Θ); y = r.*sin(Θ) + end + # doesn't handle mismatched x/y - wrap data (pyplot behaviour): nx = length(x); ny = length(y) if nx < ny @@ -338,6 +347,12 @@ function _inspectdr_setupsubplot(sp::Subplot{InspectDRBackend}) plot.axes = InspectDR.AxesRect(xscale, yscale) xmin, xmax = axis_limits(xaxis) ymin, ymax = axis_limits(yaxis) + if ispolar(sp) + #Plots.jl appears to give (xmin,xmax) ≜ (Θmin,Θmax) & (ymin,ymax) ≜ (rmin,rmax) + rmax = max(abs(ymin), abs(ymax)) + xmin, xmax = -rmax, rmax + ymin, ymax = -rmax, rmax + end plot.ext = InspectDR.PExtents2D() #reset plot.ext_full = InspectDR.PExtents2D(xmin, xmax, ymin, ymax) a = plot.annotation @@ -345,6 +360,7 @@ function _inspectdr_setupsubplot(sp::Subplot{InspectDRBackend}) a.xlabel = xaxis[:guide]; a.ylabel = yaxis[:guide] l = plot.layout + l.frame.fillcolor = _inspectdr_mapcolor(sp[:background_color_subplot]) l.framedata.fillcolor = _inspectdr_mapcolor(sp[:background_color_inside]) l.framedata.line.color = _inspectdr_mapcolor(xaxis[:foreground_color_axis]) l.fnttitle = InspectDR.Font(sp[:titlefont].family, @@ -378,6 +394,13 @@ function _before_layout_calcs(plt::Plot{InspectDRBackend}) const mplot = _inspectdr_getmplot(plt.o) if nothing == mplot; return; end + mplot.title = plt[:plot_title] + if "" == mplot.title + #Don't use window_title... probably not what you want. + #mplot.title = plt[:window_title] + end + mplot.frame.fillcolor = _inspectdr_mapcolor(plt[:background_color_outside]) + resize!(mplot.subplots, length(plt.subplots)) nsubplots = length(plt.subplots) for (i, sp) in enumerate(plt.subplots) @@ -385,15 +408,15 @@ function _before_layout_calcs(plt::Plot{InspectDRBackend}) mplot.subplots[i] = InspectDR.Plot2D() end sp.o = mplot.subplots[i] + plot = sp.o _initialize_subplot(plt, sp) _inspectdr_setupsubplot(sp) - - sp.o.layout.frame.fillcolor = - _inspectdr_mapcolor(plt[:background_color_outside]) + graphbb = _inspectdr_to_pixels(plotarea(sp)) + plot.plotbb = InspectDR.plotbounds(plot.layout, graphbb) # add the annotations for ann in sp[:annotations] - _inspectdr_add_annotations(mplot.subplots[i], ann...) + _inspectdr_add_annotations(plot, ann...) end end @@ -422,8 +445,19 @@ end # Set the (left, top, right, bottom) minimum padding around the plot area # to fit ticks, tick labels, guides, colorbars, etc. function _update_min_padding!(sp::Subplot{InspectDRBackend}) - sp.minpad = (20mm, 5mm, 2mm, 10mm) - #TODO: Add support for padding. + plot = sp.o + if !isa(plot, InspectDR.Plot2D); return sp.minpad; end + #Computing plotbounds with 0-BoundingBox returns required padding: + bb = InspectDR.plotbounds(plot.layout, InspectDR.BoundingBox(0,0,0,0)) + #NOTE: plotbounds always pads for titles, legends, etc. even if not in use. + #TODO: possibly zero-out items not in use?? + + # add in the user-specified margin to InspectDR padding: + leftpad = abs(bb.xmin)*px + sp[:left_margin] + toppad = abs(bb.ymax)*px + sp[:top_margin] + rightpad = abs(bb.xmax)*px + sp[:right_margin] + bottompad = abs(bb.ymin)*px + sp[:bottom_margin] + sp.minpad = (leftpad, toppad, rightpad, bottompad) end # ---------------------------------------------------------------- @@ -432,6 +466,9 @@ end function _update_plot_object(plt::Plot{InspectDRBackend}) mplot = _inspectdr_getmplot(plt.o) if nothing == mplot; return; end + + #TODO: should plotbb be computed here?? + gplot = _inspectdr_getgui(plt.o) if nothing == gplot; return; end @@ -452,19 +489,21 @@ const _inspectdr_mimeformats_nodpi = Dict( # "application/postscript" => "ps", #TODO: support once Cairo supports PSSurface "application/pdf" => "pdf" ) -_inspectdr_show(io::IO, mime::MIME, ::Void) = +_inspectdr_show(io::IO, mime::MIME, ::Void, w, h) = throw(ErrorException("Cannot show(::IO, ...) plot - not yet generated")) -_inspectdr_show(io::IO, mime::MIME, mplot) = show(io, mime, mplot) +function _inspectdr_show(io::IO, mime::MIME, mplot, w, h) + InspectDR._show(io, mime, mplot, Float64(w), Float64(h)) +end for (mime, fmt) in _inspectdr_mimeformats_dpi @eval function _show(io::IO, mime::MIME{Symbol($mime)}, plt::Plot{InspectDRBackend}) dpi = plt[:dpi]#TODO: support - _inspectdr_show(io, mime, _inspectdr_getmplot(plt.o)) + _inspectdr_show(io, mime, _inspectdr_getmplot(plt.o), plt[:size]...) end end for (mime, fmt) in _inspectdr_mimeformats_nodpi @eval function _show(io::IO, mime::MIME{Symbol($mime)}, plt::Plot{InspectDRBackend}) - _inspectdr_show(io, mime, _inspectdr_getmplot(plt.o)) + _inspectdr_show(io, mime, _inspectdr_getmplot(plt.o), plt[:size]...) end end _show(io::IO, mime::MIME"text/plain", plt::Plot{InspectDRBackend}) = nothing #Don't show