Add support for layout & plot size.

Also:
Add basic support for polar plots.
Add support for plot title & background color of entire figure.
This commit is contained in:
MA Laforge 2017-01-23 21:25:55 -05:00
parent 0421fe9a82
commit ebf92d3033

View File

@ -38,7 +38,7 @@ const _inspectdr_attr = merge_with_base_supported([
# :ribbon, :quiver, :arrow, # :ribbon, :quiver, :arrow,
# :orientation, # :orientation,
:overwrite_figure, :overwrite_figure,
# :polar, :polar,
# :normalize, :weights, # :normalize, :weights,
# :contours, :aspect_ratio, # :contours, :aspect_ratio,
:match_dimensions, :match_dimensions,
@ -66,6 +66,9 @@ const _inspectdr_scale = [:identity, :ln, :log2, :log10]
is_marker_supported(::InspectDRBackend, shape::Shape) = true 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? #Do we avoid Map to avoid possible pre-comile issues?
function _inspectdr_mapglyph(s::Symbol) function _inspectdr_mapglyph(s::Symbol)
s == :rect && return :square 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 _vectorize(v) = isa(v, Vector)? v: collect(v) #InspectDR only supports vectors
x = _vectorize(series[:x]); y = _vectorize(series[:y]) 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): # doesn't handle mismatched x/y - wrap data (pyplot behaviour):
nx = length(x); ny = length(y) nx = length(x); ny = length(y)
if nx < ny if nx < ny
@ -338,6 +347,12 @@ function _inspectdr_setupsubplot(sp::Subplot{InspectDRBackend})
plot.axes = InspectDR.AxesRect(xscale, yscale) plot.axes = InspectDR.AxesRect(xscale, yscale)
xmin, xmax = axis_limits(xaxis) xmin, xmax = axis_limits(xaxis)
ymin, ymax = axis_limits(yaxis) 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 = InspectDR.PExtents2D() #reset
plot.ext_full = InspectDR.PExtents2D(xmin, xmax, ymin, ymax) plot.ext_full = InspectDR.PExtents2D(xmin, xmax, ymin, ymax)
a = plot.annotation a = plot.annotation
@ -345,6 +360,7 @@ function _inspectdr_setupsubplot(sp::Subplot{InspectDRBackend})
a.xlabel = xaxis[:guide]; a.ylabel = yaxis[:guide] a.xlabel = xaxis[:guide]; a.ylabel = yaxis[:guide]
l = plot.layout l = plot.layout
l.frame.fillcolor = _inspectdr_mapcolor(sp[:background_color_subplot])
l.framedata.fillcolor = _inspectdr_mapcolor(sp[:background_color_inside]) l.framedata.fillcolor = _inspectdr_mapcolor(sp[:background_color_inside])
l.framedata.line.color = _inspectdr_mapcolor(xaxis[:foreground_color_axis]) l.framedata.line.color = _inspectdr_mapcolor(xaxis[:foreground_color_axis])
l.fnttitle = InspectDR.Font(sp[:titlefont].family, l.fnttitle = InspectDR.Font(sp[:titlefont].family,
@ -378,6 +394,13 @@ function _before_layout_calcs(plt::Plot{InspectDRBackend})
const mplot = _inspectdr_getmplot(plt.o) const mplot = _inspectdr_getmplot(plt.o)
if nothing == mplot; return; end 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)) resize!(mplot.subplots, length(plt.subplots))
nsubplots = length(plt.subplots) nsubplots = length(plt.subplots)
for (i, sp) in enumerate(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() mplot.subplots[i] = InspectDR.Plot2D()
end end
sp.o = mplot.subplots[i] sp.o = mplot.subplots[i]
plot = sp.o
_initialize_subplot(plt, sp) _initialize_subplot(plt, sp)
_inspectdr_setupsubplot(sp) _inspectdr_setupsubplot(sp)
graphbb = _inspectdr_to_pixels(plotarea(sp))
sp.o.layout.frame.fillcolor = plot.plotbb = InspectDR.plotbounds(plot.layout, graphbb)
_inspectdr_mapcolor(plt[:background_color_outside])
# add the annotations # add the annotations
for ann in sp[:annotations] for ann in sp[:annotations]
_inspectdr_add_annotations(mplot.subplots[i], ann...) _inspectdr_add_annotations(plot, ann...)
end end
end end
@ -422,8 +445,19 @@ end
# Set the (left, top, right, bottom) minimum padding around the plot area # Set the (left, top, right, bottom) minimum padding around the plot area
# to fit ticks, tick labels, guides, colorbars, etc. # to fit ticks, tick labels, guides, colorbars, etc.
function _update_min_padding!(sp::Subplot{InspectDRBackend}) function _update_min_padding!(sp::Subplot{InspectDRBackend})
sp.minpad = (20mm, 5mm, 2mm, 10mm) plot = sp.o
#TODO: Add support for padding. 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 end
# ---------------------------------------------------------------- # ----------------------------------------------------------------
@ -432,6 +466,9 @@ end
function _update_plot_object(plt::Plot{InspectDRBackend}) function _update_plot_object(plt::Plot{InspectDRBackend})
mplot = _inspectdr_getmplot(plt.o) mplot = _inspectdr_getmplot(plt.o)
if nothing == mplot; return; end if nothing == mplot; return; end
#TODO: should plotbb be computed here??
gplot = _inspectdr_getgui(plt.o) gplot = _inspectdr_getgui(plt.o)
if nothing == gplot; return; end if nothing == gplot; return; end
@ -452,19 +489,21 @@ const _inspectdr_mimeformats_nodpi = Dict(
# "application/postscript" => "ps", #TODO: support once Cairo supports PSSurface # "application/postscript" => "ps", #TODO: support once Cairo supports PSSurface
"application/pdf" => "pdf" "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")) 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 for (mime, fmt) in _inspectdr_mimeformats_dpi
@eval function _show(io::IO, mime::MIME{Symbol($mime)}, plt::Plot{InspectDRBackend}) @eval function _show(io::IO, mime::MIME{Symbol($mime)}, plt::Plot{InspectDRBackend})
dpi = plt[:dpi]#TODO: support 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
end end
for (mime, fmt) in _inspectdr_mimeformats_nodpi for (mime, fmt) in _inspectdr_mimeformats_nodpi
@eval function _show(io::IO, mime::MIME{Symbol($mime)}, plt::Plot{InspectDRBackend}) @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
end end
_show(io::IO, mime::MIME"text/plain", plt::Plot{InspectDRBackend}) = nothing #Don't show _show(io::IO, mime::MIME"text/plain", plt::Plot{InspectDRBackend}) = nothing #Don't show