shape linetype; supported in gadfly; gadfly cleanup

This commit is contained in:
Thomas Breloff 2016-03-16 13:12:01 -04:00
parent 16799d8b75
commit 7531c48e38
5 changed files with 430 additions and 459 deletions

View File

@ -11,7 +11,7 @@ const _3dTypes = [:path3d, :scatter3d, :surface, :wireframe]
const _allTypes = vcat([ const _allTypes = vcat([
:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :none, :line, :path, :steppre, :steppost, :sticks, :scatter,
:heatmap, :hexbin, :hist, :hist2d, :hist3d, :density, :bar, :hline, :vline, :ohlc, :heatmap, :hexbin, :hist, :hist2d, :hist3d, :density, :bar, :hline, :vline, :ohlc,
:contour, :pie :contour, :pie, :shape
], _3dTypes) ], _3dTypes)
@compat const _typeAliases = Dict( @compat const _typeAliases = Dict(
:n => :none, :n => :none,
@ -35,6 +35,9 @@ const _allTypes = vcat([
:line3d => :path3d, :line3d => :path3d,
:surf => :surface, :surf => :surface,
:wire => :wireframe, :wire => :wireframe,
:shapes => :shape,
:poly => :shape,
:polygon => :shape,
) )
ishistlike(lt::Symbol) = lt in (:hist, :density) ishistlike(lt::Symbol) = lt in (:hist, :density)
@ -801,5 +804,3 @@ function getSeriesArgs(pkg::AbstractBackend, plotargs::Dict, kw, commandIndex::I
d d
end end

View File

@ -34,7 +34,7 @@ end
function getLineGeom(d::Dict) function getLineGeom(d::Dict)
lt = d[:linetype] lt = d[:linetype]
xbins, ybins = maketuple(d[:nbins]) xbins, ybins = maketuple(d[:nbins])
if lt == :hexbin if lt == :hexb
Gadfly.Geom.hexbin(xbincount = xbins, ybincount = ybins) Gadfly.Geom.hexbin(xbincount = xbins, ybincount = ybins)
elseif lt == :hist2d elseif lt == :hist2d
Gadfly.Geom.histogram2d(xbincount = xbins, ybincount = ybins) Gadfly.Geom.histogram2d(xbincount = xbins, ybincount = ybins)
@ -54,6 +54,8 @@ function getLineGeom(d::Dict)
Gadfly.Geom.vline Gadfly.Geom.vline
elseif lt == :contour elseif lt == :contour
Gadfly.Geom.contour(levels = d[:levels]) Gadfly.Geom.contour(levels = d[:levels])
# elseif lt == :shape
# Gadfly.Geom.polygon(fill = true, preserve_order = true)
else else
nothing nothing
end end
@ -75,21 +77,8 @@ function get_extra_theme_args(d::Dict, k::Symbol)
end end
function getGadflyLineTheme(d::Dict) function getGadflyLineTheme(d::Dict)
lc = convertColor(getColor(d[:linecolor]), d[:linealpha]) lc = convertColor(getColor(d[:linecolor]), d[:linealpha])
# lc = getColor(d[:linecolor])
# α = d[:linealpha]
# if α != nothing
# lc = RGBA(lc, α)
# end
fc = convertColor(getColor(d[:fillcolor]), d[:fillalpha]) fc = convertColor(getColor(d[:fillcolor]), d[:fillalpha])
# fc = getColor(d[:fillcolor])
# α = d[:fillalpha]
# if α != nothing
# fc = RGBA(fc, α)
# end
Gadfly.Theme(; Gadfly.Theme(;
default_color = lc, default_color = lc,
@ -105,8 +94,7 @@ end
# add a line as a new layer # add a line as a new layer
function addGadflyLine!(plt::Plot, numlayers::Int, d::Dict, geoms...) function addGadflyLine!(plt::Plot, numlayers::Int, d::Dict, geoms...)
gplt = getGadflyContext(plt) gplt = getGadflyContext(plt)
gfargs = vcat(geoms..., gfargs = vcat(geoms..., getGadflyLineTheme(d))
getGadflyLineTheme(d))
kwargs = Dict() kwargs = Dict()
lt = d[:linetype] lt = d[:linetype]
@ -128,7 +116,6 @@ function addGadflyLine!(plt::Plot, numlayers::Int, d::Dict, geoms...)
kwargs[:xmin] = d[:x] - w kwargs[:xmin] = d[:x] - w
kwargs[:xmax] = d[:x] + w kwargs[:xmax] = d[:x] + w
elseif lt == :contour elseif lt == :contour
# d[:y] = reverse(d[:y])
kwargs[:z] = d[:z].surf kwargs[:z] = d[:z].surf
addGadflyContColorScale(plt, d[:linecolor]) addGadflyContColorScale(plt, d[:linecolor])
end end
@ -138,30 +125,7 @@ function addGadflyLine!(plt::Plot, numlayers::Int, d::Dict, geoms...)
end end
# # h/vlines
# if lt == :hline
# kwargs[:yintercept] = d[:y]
# elseif lt == :vline
# kwargs[:xintercept] = d[:y]
# elseif lt == :sticks
# w = 0.01 * mean(diff(d[:x]))
# kwargs[:xmin] = d[:x] - w
# kwargs[:xmax] = d[:x] + w
# elseif lt == :contour
# d[:y] = reverse(d[:y])
# kwargs[:z] = d[:surface]
# end
# if lt == :hist
# kwargs[:x] = kwargs[:y] = d[:y]
# elseif lt != :hline && lt != :vline
# kwargs[:x] = d[:x]
# kwargs[:y] = d[:y]
# end
# # add the layer # # add the layer
# x = d[d[:linetype] == :hist ? :y : :x]
# Gadfly.layer(gfargs...; x = x, y = d[:y], order=numlayers, kwargs...)
Gadfly.layer(gfargs...; order=numlayers, kwargs...) Gadfly.layer(gfargs...; order=numlayers, kwargs...)
end end
@ -172,13 +136,13 @@ end
getMarkerGeom(shape::Shape) = gadflyshape(shape) getMarkerGeom(shape::Shape) = gadflyshape(shape)
getMarkerGeom(shape::Symbol) = gadflyshape(_shapes[shape]) getMarkerGeom(shape::Symbol) = gadflyshape(_shapes[shape])
getMarkerGeom(shapes::AVec) = map(getMarkerGeom, shapes) getMarkerGeom(shapes::AVec) = map(getMarkerGeom, shapes)
getMarkerGeom(d::Dict) = getMarkerGeom(d[:markershape]) function getMarkerGeom(d::Dict)
if d[:linetype] == :shape
# function getMarkerGeom(d::Dict) Gadfly.Geom.polygon(fill = true, preserve_order = true)
# shape = d[:markershape] else
# gadflyshape(isa(shape, Shape) ? shape : _shapes[shape]) getMarkerGeom(d[:markershape])
# end end
end
function getGadflyMarkerTheme(d::Dict, plotargs::Dict) function getGadflyMarkerTheme(d::Dict, plotargs::Dict)
c = getColor(d[:markercolor]) c = getColor(d[:markercolor])
@ -195,12 +159,12 @@ function getGadflyMarkerTheme(d::Dict, plotargs::Dict)
ms * Gadfly.px ms * Gadfly.px
end end
# fg = getColor(plotargs[:foreground_color])
Gadfly.Theme(; Gadfly.Theme(;
default_color = c, default_color = c,
default_point_size = ms, default_point_size = ms,
discrete_highlight_color = c -> RGB(getColor(d[:markerstrokecolor])), discrete_highlight_color = c -> RGB(getColor(d[:markerstrokecolor])),
highlight_width = d[:markerstrokewidth] * Gadfly.px, highlight_width = d[:markerstrokewidth] * Gadfly.px,
line_width = d[:markerstrokewidth] * Gadfly.px,
# get_extra_theme_args(d, :markerstrokestyle)... # get_extra_theme_args(d, :markerstrokestyle)...
) )
end end
@ -214,9 +178,7 @@ function addGadflyContColorScale(plt::Plot{GadflyBackend}, c)
end end
function addGadflyMarker!(plt::Plot, numlayers::Int, d::Dict, plotargs::Dict, geoms...) function addGadflyMarker!(plt::Plot, numlayers::Int, d::Dict, plotargs::Dict, geoms...)
gfargs = vcat(geoms..., gfargs = vcat(geoms..., getGadflyMarkerTheme(d, plotargs), getMarkerGeom(d))
getGadflyMarkerTheme(d, plotargs),
getMarkerGeom(d))
kwargs = Dict() kwargs = Dict()
# handle continuous color scales for the markers # handle continuous color scales for the markers
@ -224,10 +186,6 @@ function addGadflyMarker!(plt::Plot, numlayers::Int, d::Dict, plotargs::Dict, ge
if zcolor != nothing && typeof(zcolor) <: AVec if zcolor != nothing && typeof(zcolor) <: AVec
kwargs[:color] = zcolor kwargs[:color] = zcolor
addGadflyContColorScale(plt, d[:markercolor]) addGadflyContColorScale(plt, d[:markercolor])
# if !isa(d[:markercolor], ColorGradient)
# d[:markercolor] = colorscheme(:bluesreds)
# end
# push!(getGadflyContext(plt).scales, Gadfly.Scale.ContinuousColorScale(p -> RGB(getColorZ(d[:markercolor], p))))
end end
Gadfly.layer(gfargs...; x = d[:x], y = d[:y], order=numlayers, kwargs...) Gadfly.layer(gfargs...; x = d[:x], y = d[:y], order=numlayers, kwargs...)
@ -237,8 +195,6 @@ end
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
function addToGadflyLegend(plt::Plot, d::Dict) function addToGadflyLegend(plt::Plot, d::Dict)
# add the legend?
if plt.plotargs[:legend] != :none && d[:label] != "" if plt.plotargs[:legend] != :none && d[:label] != ""
gplt = getGadflyContext(plt) gplt = getGadflyContext(plt)
@ -272,9 +228,7 @@ function addToGadflyLegend(plt::Plot, d::Dict)
end end
end end
end end
end end
end end
getGadflySmoothing(smooth::Bool) = smooth ? [Gadfly.Geom.smooth(method=:lm)] : Any[] getGadflySmoothing(smooth::Bool) = smooth ? [Gadfly.Geom.smooth(method=:lm)] : Any[]
@ -282,7 +236,6 @@ getGadflySmoothing(smooth::Real) = [Gadfly.Geom.smooth(method=:loess, smoothing=
function addGadflySeries!(plt::Plot, d::Dict) function addGadflySeries!(plt::Plot, d::Dict)
layers = Gadfly.Layer[] layers = Gadfly.Layer[]
gplt = getGadflyContext(plt) gplt = getGadflyContext(plt)
@ -294,9 +247,7 @@ function addGadflySeries!(plt::Plot, d::Dict)
geom = getLineGeom(d) geom = getLineGeom(d)
if geom != nothing if geom != nothing
prepend!(layers, addGadflyLine!(plt, length(gplt.layers), d, geom, smooth...)) prepend!(layers, addGadflyLine!(plt, length(gplt.layers), d, geom, smooth...))
smooth = Any[] # don't add a regression for markers too
# don't add a regression for markers too
smooth = Any[]
end end
# special handling for ohlc and scatter # special handling for ohlc and scatter
@ -310,7 +261,7 @@ function addGadflySeries!(plt::Plot, d::Dict)
end end
# markers # markers
if d[:markershape] != :none if d[:markershape] != :none || lt == :shape
prepend!(layers, addGadflyMarker!(plt, length(gplt.layers), d, plt.plotargs, smooth...)) prepend!(layers, addGadflyMarker!(plt, length(gplt.layers), d, plt.plotargs, smooth...))
end end
@ -401,8 +352,6 @@ end
function addGadflyLimitsScale(gplt, d::Dict, isx::Bool) function addGadflyLimitsScale(gplt, d::Dict, isx::Bool)
# get the correct scale function
gfunc, hasScaleKey = getGadflyScaleFunction(d, isx) gfunc, hasScaleKey = getGadflyScaleFunction(d, isx)
# do we want to add min/max limits for the axis? # do we want to add min/max limits for the axis?
@ -411,7 +360,6 @@ function addGadflyLimitsScale(gplt, d::Dict, isx::Bool)
# map :auto to nothing, otherwise add to limargs # map :auto to nothing, otherwise add to limargs
lims = get(d, limsym, :auto) lims = get(d, limsym, :auto)
# lims == :auto && return
if lims == :auto if lims == :auto
lims = nothing lims = nothing
else else
@ -485,7 +433,6 @@ function updateGadflyGuides(plt::Plot, d::Dict)
else else
addGadflyTicksGuide(gplt, ticks, false) addGadflyTicksGuide(gplt, ticks, false)
end end
# haskey(d, :yticks) && addGadflyTicksGuide(gplt, d[:yticks], false)
updateGadflyAxisFlips(gplt, d, xlims, ylims) updateGadflyAxisFlips(gplt, d, xlims, ylims)
end end
@ -493,14 +440,7 @@ end
function updateGadflyPlotTheme(plt::Plot, d::Dict) function updateGadflyPlotTheme(plt::Plot, d::Dict)
kwargs = Dict() kwargs = Dict()
# # get the full plotargs, overriding any new settings
# # TODO: should this be part of the main `plot` command in plot.jl???
# d = merge!(plt.plotargs, d)
# # hide the legend? # # hide the legend?
# if !get(d, :legend, true)
# kwargs[:key_position] = :none
# end
leg = d[d[:legend] == :none ? :colorbar : :legend] leg = d[d[:legend] == :none ? :colorbar : :legend]
if leg != :best if leg != :best
kwargs[:key_position] = leg == :inside ? :right : leg kwargs[:key_position] = leg == :inside ? :right : leg
@ -576,7 +516,6 @@ end
# plot one data series # plot one data series
function _add_series(::GadflyBackend, plt::Plot; kw...) function _add_series(::GadflyBackend, plt::Plot; kw...)
# first clear out the temporary layer # first clear out the temporary layer
gplt = getGadflyContext(plt) gplt = getGadflyContext(plt)
if gplt.layers[1].geom.tag == :remove if gplt.layers[1].geom.tag == :remove
@ -704,12 +643,10 @@ function Base.display(::PlotsDisplay, plt::Plot{GadflyBackend})
end end
function Base.display(::PlotsDisplay, subplt::Subplot{GadflyBackend}) function Base.display(::PlotsDisplay, subplt::Subplot{GadflyBackend})
setGadflyDisplaySize(getplotargs(subplt,1)[:size]...) setGadflyDisplaySize(getplotargs(subplt,1)[:size]...)
ctx = buildGadflySubplotContext(subplt) ctx = buildGadflySubplotContext(subplt)
# taken from Gadfly since I couldn't figure out how to do it directly # taken from Gadfly since I couldn't figure out how to do it directly
filename = string(Gadfly.tempname(), ".html") filename = string(Gadfly.tempname(), ".html")

View File

@ -81,7 +81,7 @@ supportedArgs(::GadflyBackend) = [
supportedAxes(::GadflyBackend) = [:auto, :left] supportedAxes(::GadflyBackend) = [:auto, :left]
supportedTypes(::GadflyBackend) = [:none, :line, :path, :steppre, :steppost, :sticks, supportedTypes(::GadflyBackend) = [:none, :line, :path, :steppre, :steppost, :sticks,
:scatter, :hist2d, :hexbin, :hist, :bar, :scatter, :hist2d, :hexbin, :hist, :bar,
:hline, :vline, :contour] :hline, :vline, :contour, :shape]
supportedStyles(::GadflyBackend) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] supportedStyles(::GadflyBackend) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot]
supportedMarkers(::GadflyBackend) = vcat(_allMarkers, Shape) supportedMarkers(::GadflyBackend) = vcat(_allMarkers, Shape)
supportedScales(::GadflyBackend) = [:identity, :ln, :log2, :log10, :asinh, :sqrt] supportedScales(::GadflyBackend) = [:identity, :ln, :log2, :log10, :asinh, :sqrt]
@ -770,4 +770,3 @@ supportedStyles(::PGFPlotsBackend) = [:auto, :solid] #, :dash, :dot, :dashdot, :
supportedMarkers(::PGFPlotsBackend) = [:none, :auto, :ellipse] #, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5] #vcat(_allMarkers, Shape) supportedMarkers(::PGFPlotsBackend) = [:none, :auto, :ellipse] #, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5] #vcat(_allMarkers, Shape)
supportedScales(::PGFPlotsBackend) = [:identity] #, :log, :log2, :log10, :asinh, :sqrt] supportedScales(::PGFPlotsBackend) = [:identity] #, :log, :log2, :log10, :asinh, :sqrt]
subplotSupported(::PGFPlotsBackend) = false subplotSupported(::PGFPlotsBackend) = false

View File

@ -3,6 +3,9 @@ immutable Shape
vertices::AVec vertices::AVec
end end
get_xs(shape::Shape) = Float64[v[1] for v in shape.vertices]
get_ys(shape::Shape) = Float64[v[2] for v in shape.vertices]
"get an array of tuples of points on a circle with radius `r`" "get an array of tuples of points on a circle with radius `r`"
function partialcircle(start_θ, end_θ, n = 20, r=1) function partialcircle(start_θ, end_θ, n = 20, r=1)
@compat(Tuple{Float64,Float64})[(r*cos(u),r*sin(u)) for u in linspace(start_θ, end_θ, n)] @compat(Tuple{Float64,Float64})[(r*cos(u),r*sin(u)) for u in linspace(start_θ, end_θ, n)]

View File

@ -444,6 +444,37 @@ function createKWargsList{T<:Real}(plt::AbstractPlot, x::AMat{T}, y::AMat{T}, zm
createKWargsList(plt, Any[x], Any[y]; d...) #kw..., z = surf, linetype = :contour) createKWargsList(plt, Any[x], Any[y]; d...) #kw..., z = surf, linetype = :contour)
end end
# plotting arbitrary shapes/polygons
function createKWargsList(plt::AbstractPlot, shape::Shape; kw...)
x, y = unzip(shape.vertices)
createKWargsList(plt, x, y; linetype = :shape, kw...)
end
function shape_coords(shapes::AVec{Shape})
xs = map(get_xs, shapes)
ys = map(get_ys, shapes)
x, y = unzip(shapes[1].vertices)
for shape in shapes[2:end]
tmpx, tmpy = unzip(shape.vertices)
x = vcat(x, NaN, tmpx)
y = vcat(y, NaN, tmpy)
end
x, y
end
function createKWargsList(plt::AbstractPlot, shapes::AVec{Shape}; kw...)
x, y = shape_coords(shapes)
createKWargsList(plt, x, y; linetype = :shape, kw...)
end
function createKWargsList(plt::AbstractPlot, shapes::AMat{Shape}; kw...)
x, y = [], []
for j in 1:size(shapes, 2)
tmpx, tmpy = shape_coords(vec(shapes[:,j]))
push!(x, tmpx)
push!(y, tmpy)
end
createKWargsList(plt, x, y; linetype = :shape, kw...)
end
function createKWargsList(plt::AbstractPlot, surf::Surface; kw...) function createKWargsList(plt::AbstractPlot, surf::Surface; kw...)
createKWargsList(plt, 1:size(surf.surf,1), 1:size(surf.surf,2), convert(Matrix{Float64}, surf.surf); kw...) createKWargsList(plt, 1:size(surf.surf,1), 1:size(surf.surf,2), convert(Matrix{Float64}, surf.surf); kw...)