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

View File

@ -34,7 +34,7 @@ end
function getLineGeom(d::Dict)
lt = d[:linetype]
xbins, ybins = maketuple(d[:nbins])
if lt == :hexbin
if lt == :hexb
Gadfly.Geom.hexbin(xbincount = xbins, ybincount = ybins)
elseif lt == :hist2d
Gadfly.Geom.histogram2d(xbincount = xbins, ybincount = ybins)
@ -54,6 +54,8 @@ function getLineGeom(d::Dict)
Gadfly.Geom.vline
elseif lt == :contour
Gadfly.Geom.contour(levels = d[:levels])
# elseif lt == :shape
# Gadfly.Geom.polygon(fill = true, preserve_order = true)
else
nothing
end
@ -75,21 +77,8 @@ function get_extra_theme_args(d::Dict, k::Symbol)
end
function getGadflyLineTheme(d::Dict)
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 = getColor(d[:fillcolor])
# α = d[:fillalpha]
# if α != nothing
# fc = RGBA(fc, α)
# end
Gadfly.Theme(;
default_color = lc,
@ -105,8 +94,7 @@ end
# add a line as a new layer
function addGadflyLine!(plt::Plot, numlayers::Int, d::Dict, geoms...)
gplt = getGadflyContext(plt)
gfargs = vcat(geoms...,
getGadflyLineTheme(d))
gfargs = vcat(geoms..., getGadflyLineTheme(d))
kwargs = Dict()
lt = d[:linetype]
@ -128,7 +116,6 @@ function addGadflyLine!(plt::Plot, numlayers::Int, d::Dict, geoms...)
kwargs[:xmin] = d[:x] - w
kwargs[:xmax] = d[:x] + w
elseif lt == :contour
# d[:y] = reverse(d[:y])
kwargs[:z] = d[:z].surf
addGadflyContColorScale(plt, d[:linecolor])
end
@ -138,30 +125,7 @@ function addGadflyLine!(plt::Plot, numlayers::Int, d::Dict, geoms...)
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
# x = d[d[:linetype] == :hist ? :y : :x]
# Gadfly.layer(gfargs...; x = x, y = d[:y], order=numlayers, kwargs...)
Gadfly.layer(gfargs...; order=numlayers, kwargs...)
end
@ -172,13 +136,13 @@ end
getMarkerGeom(shape::Shape) = gadflyshape(shape)
getMarkerGeom(shape::Symbol) = gadflyshape(_shapes[shape])
getMarkerGeom(shapes::AVec) = map(getMarkerGeom, shapes)
getMarkerGeom(d::Dict) = getMarkerGeom(d[:markershape])
# function getMarkerGeom(d::Dict)
# shape = d[:markershape]
# gadflyshape(isa(shape, Shape) ? shape : _shapes[shape])
# end
function getMarkerGeom(d::Dict)
if d[:linetype] == :shape
Gadfly.Geom.polygon(fill = true, preserve_order = true)
else
getMarkerGeom(d[:markershape])
end
end
function getGadflyMarkerTheme(d::Dict, plotargs::Dict)
c = getColor(d[:markercolor])
@ -195,12 +159,12 @@ function getGadflyMarkerTheme(d::Dict, plotargs::Dict)
ms * Gadfly.px
end
# fg = getColor(plotargs[:foreground_color])
Gadfly.Theme(;
default_color = c,
default_point_size = ms,
discrete_highlight_color = c -> RGB(getColor(d[:markerstrokecolor])),
highlight_width = d[:markerstrokewidth] * Gadfly.px,
line_width = d[:markerstrokewidth] * Gadfly.px,
# get_extra_theme_args(d, :markerstrokestyle)...
)
end
@ -214,9 +178,7 @@ function addGadflyContColorScale(plt::Plot{GadflyBackend}, c)
end
function addGadflyMarker!(plt::Plot, numlayers::Int, d::Dict, plotargs::Dict, geoms...)
gfargs = vcat(geoms...,
getGadflyMarkerTheme(d, plotargs),
getMarkerGeom(d))
gfargs = vcat(geoms..., getGadflyMarkerTheme(d, plotargs), getMarkerGeom(d))
kwargs = Dict()
# 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
kwargs[:color] = zcolor
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
Gadfly.layer(gfargs...; x = d[:x], y = d[:y], order=numlayers, kwargs...)
@ -237,8 +195,6 @@ end
# ---------------------------------------------------------------------------
function addToGadflyLegend(plt::Plot, d::Dict)
# add the legend?
if plt.plotargs[:legend] != :none && d[:label] != ""
gplt = getGadflyContext(plt)
@ -272,9 +228,7 @@ function addToGadflyLegend(plt::Plot, d::Dict)
end
end
end
end
end
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)
layers = Gadfly.Layer[]
gplt = getGadflyContext(plt)
@ -294,9 +247,7 @@ function addGadflySeries!(plt::Plot, d::Dict)
geom = getLineGeom(d)
if geom != nothing
prepend!(layers, addGadflyLine!(plt, length(gplt.layers), d, geom, smooth...))
# don't add a regression for markers too
smooth = Any[]
smooth = Any[] # don't add a regression for markers too
end
# special handling for ohlc and scatter
@ -310,7 +261,7 @@ function addGadflySeries!(plt::Plot, d::Dict)
end
# markers
if d[:markershape] != :none
if d[:markershape] != :none || lt == :shape
prepend!(layers, addGadflyMarker!(plt, length(gplt.layers), d, plt.plotargs, smooth...))
end
@ -401,8 +352,6 @@ end
function addGadflyLimitsScale(gplt, d::Dict, isx::Bool)
# get the correct scale function
gfunc, hasScaleKey = getGadflyScaleFunction(d, isx)
# 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
lims = get(d, limsym, :auto)
# lims == :auto && return
if lims == :auto
lims = nothing
else
@ -485,7 +433,6 @@ function updateGadflyGuides(plt::Plot, d::Dict)
else
addGadflyTicksGuide(gplt, ticks, false)
end
# haskey(d, :yticks) && addGadflyTicksGuide(gplt, d[:yticks], false)
updateGadflyAxisFlips(gplt, d, xlims, ylims)
end
@ -493,14 +440,7 @@ end
function updateGadflyPlotTheme(plt::Plot, d::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?
# if !get(d, :legend, true)
# kwargs[:key_position] = :none
# end
leg = d[d[:legend] == :none ? :colorbar : :legend]
if leg != :best
kwargs[:key_position] = leg == :inside ? :right : leg
@ -576,7 +516,6 @@ end
# plot one data series
function _add_series(::GadflyBackend, plt::Plot; kw...)
# first clear out the temporary layer
gplt = getGadflyContext(plt)
if gplt.layers[1].geom.tag == :remove
@ -704,12 +643,10 @@ function Base.display(::PlotsDisplay, plt::Plot{GadflyBackend})
end
function Base.display(::PlotsDisplay, subplt::Subplot{GadflyBackend})
setGadflyDisplaySize(getplotargs(subplt,1)[:size]...)
ctx = buildGadflySubplotContext(subplt)
# taken from Gadfly since I couldn't figure out how to do it directly
filename = string(Gadfly.tempname(), ".html")

View File

@ -81,7 +81,7 @@ supportedArgs(::GadflyBackend) = [
supportedAxes(::GadflyBackend) = [:auto, :left]
supportedTypes(::GadflyBackend) = [:none, :line, :path, :steppre, :steppost, :sticks,
:scatter, :hist2d, :hexbin, :hist, :bar,
:hline, :vline, :contour]
:hline, :vline, :contour, :shape]
supportedStyles(::GadflyBackend) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot]
supportedMarkers(::GadflyBackend) = vcat(_allMarkers, Shape)
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)
supportedScales(::PGFPlotsBackend) = [:identity] #, :log, :log2, :log10, :asinh, :sqrt]
subplotSupported(::PGFPlotsBackend) = false

View File

@ -3,6 +3,9 @@ immutable Shape
vertices::AVec
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`"
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)]

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)
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...)
createKWargsList(plt, 1:size(surf.surf,1), 1:size(surf.surf,2), convert(Matrix{Float64}, surf.surf); kw...)