From 03392a5f0344f24874521d97453dd2df9decd634 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Fri, 16 Oct 2015 10:58:44 -0400 Subject: [PATCH] gadfly cleanup --- src/backends/gadfly.jl | 206 +---------------------- src/backends/gadfly_shapes.jl | 309 ---------------------------------- 2 files changed, 2 insertions(+), 513 deletions(-) diff --git a/src/backends/gadfly.jl b/src/backends/gadfly.jl index 4eff2324..fc352d2b 100644 --- a/src/backends/gadfly.jl +++ b/src/backends/gadfly.jl @@ -10,17 +10,14 @@ # supportedArgs(::GadflyPackage) = setdiff(_allArgs, [:heatmap_c, :pos, :screen, :yrightlabel]) supportedArgs(::GadflyPackage) = [ :annotation, - # :args, # :axis, :background_color, :color, :color_palette, :fillrange, :fillcolor, - # :foreground_color, + :foreground_color, :group, - # :heatmap_c, - # :kwargs, :label, :layout, :legend, @@ -54,8 +51,6 @@ supportedArgs(::GadflyPackage) = [ :xflip, :yflip, :z, - # :linkx, - # :linky, :tickfont, :guidefont, :legendfont, @@ -63,14 +58,12 @@ supportedArgs(::GadflyPackage) = [ supportedAxes(::GadflyPackage) = [:auto, :left] supportedTypes(::GadflyPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline, :ohlc] supportedStyles(::GadflyPackage) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] -# supportedMarkers(::GadflyPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star1, :star2, :hexagon, :octagon, Shape] supportedMarkers(::GadflyPackage) = vcat(_allMarkers, Shape) supportedScales(::GadflyPackage) = [:identity, :log, :log2, :log10, :asinh, :sqrt] # --------------------------------------------------------------------------- -# include("gadfly_shapes.jl") function createGadflyPlotObject(d::Dict) @eval import DataFrames @@ -118,75 +111,6 @@ function createGadflyPlotObject(d::Dict) end -# --------------------------------------------------------------------------- - -# function getLineGeoms_old(d::Dict) -# lt = d[:linetype] -# xbins, ybins = maketuple(d[:nbins]) -# lt == :hexbin && return [Gadfly.Geom.hexbin(xbincount = xbins, ybincount = ybins)] -# lt == :heatmap && return [Gadfly.Geom.histogram2d(xbincount = xbins, ybincount = ybins)] -# lt == :hist && return [Gadfly.Geom.histogram(bincount = xbins)] -# lt == :path && return [Gadfly.Geom.path] -# lt == :bar && return [Gadfly.Geom.bar] -# lt == :steppost && return [Gadfly.Geom.step] - -# # NOTE: we won't actually show this (we'll set linewidth to 0 later), but we need a geom so that Gadfly doesn't complain -# if lt in (:none, :ohlc, :scatter) -# # return [Gadfly.Geom.path] -# return Any[] -# end - -# # lt == :sticks && return [Gadfly.Geom.bar] -# error("linetype $lt not currently supported with Gadfly") -# end - - - -# # serious hack (I think?) to draw my own shapes as annotations... will it work? who knows... -# function getMarkerGeomsAndGuides(d::Dict, initargs::Dict) -# return Any[], Any[] # don't do this step anymore -# # marker = d[:markershape] - -# # if marker == :none && d[:linetype] != :ohlc -# # return Any[], Any[] - -# # elseif marker == :diamond -# # geom = DIAMOND -# # @show geom -# # return Any[geom], Any[] - -# # else -# # return Any[], [createGadflyAnnotation(d, initargs)] -# # end -# end - -# function getMarkerGeoms(d::Dict) -# shape = d[:markershape] -# isa(shape, Shape) && return [gadflyshape(shape)] -# shape == :none && return Any[] -# if !haskey(_shapes, shape) -# error("unhandled marker: ", shape) -# end -# [gadflyshape(_shapes[shape])] -# end - - -# function addGadflyFixedLines!(gplt, d::Dict, theme) - -# sz = d[:linewidth] * Gadfly.px -# c = d[:color] - -# if d[:linetype] == :hline -# geom = Gadfly.Geom.hline(color=c, size=sz) -# layer = Gadfly.layer(yintercept = d[:y], geom, theme) -# else -# geom = Gadfly.Geom.vline(color=c, size=sz) -# layer = Gadfly.layer(xintercept = d[:y], geom, theme) -# end - -# prepend!(gplt.layers, layer) -# end - # --------------------------------------------------------------------------- @@ -385,64 +309,7 @@ end # --------------------------------------------------------------------------- - - -# function addGadflySeries_old!(gplt, d::Dict, initargs::Dict) - -# gfargs = Any[] - -# # if my PR isn't present, don't set the line_style -# local extra_theme_args -# try -# extra_theme_args = Any[(:line_style, Gadfly.get_stroke_vector(d[:linestyle]))] -# catch -# extra_theme_args = Any[] -# end - -# # set theme: color, line linewidth, and point size -# line_width = d[:linewidth] * (d[:linetype] in (:none, :ohlc, :scatter) ? 0 : 1) * Gadfly.px # 0 linewidth when we don't show a line -# line_color = getColor(d[:color]) -# fillcolor = getColor(d[:fillcolor]) -# # marker_stroke_color = d[:linewidth] == 0 ? RGBA(0,0,0,0) : line_color -# # fg = initargs[:foreground_color] -# theme = Gadfly.Theme(; default_color = line_color, -# line_width = line_width, -# default_point_size = d[:markersize] * Gadfly.px, -# # grid_color = fg, -# # minor_label_color = fg, -# # major_label_color = fg, -# # key_title_color = fg, -# # key_label_color = fg, -# # discrete_highlight_color = marker_stroke_color, # border of markers -# highlight_width = d[:linewidth] * Gadfly.px, # width of marker border -# lowlight_color = x->RGB(fillcolor), # fill/ribbon -# lowlight_opacity = alpha(fillcolor), # fill/ribbon -# bar_highlight = RGB(line_color), # bars -# extra_theme_args...) -# push!(gfargs, theme) - -# # first things first... lets do the sticks hack -# if d[:linetype] == :sticks -# d, dScatter = sticksHack(;d...) - -# # add the annotation -# if dScatter[:markershape] != :none -# push!(gplt.guides, createGadflyAnnotation(dScatter, initargs)) -# end - -# elseif d[:linetype] in (:hline, :vline) -# addGadflyFixedLines!(gplt, d, theme) -# return - -# end - -# if d[:linetype] == :scatter -# d[:linetype] = :none -# end - -# # add the Geoms -# append!(gfargs, getLineGeoms_old(d)) -# append!(gfargs, getMarkerGeoms(d)) +# NOTE: I'm leaving this here and commented out just in case I want to implement again... it was hacky code to create multi-colored line segments # # colorgroup # z = d[:z] @@ -462,75 +329,6 @@ end # d[:x], d[:y] = map(createSegments, (d[:x], d[:y])) # colorgroup = [(:color, cs), (:group, grp)] -# # handle continuous color scales for the markers -# elseif z != nothing && typeof(z) <: AVec -# colorgroup = [(:color, z)] -# if !isa(d[:markercolor], ColorGradient) -# d[:markercolor] = colorscheme(:bluesreds) -# end -# push!(gplt.scales, Gadfly.Scale.ContinuousColorScale(p -> RGB(getColorZ(d[:markercolor], p)))) # minz + p * (maxz - minz)))) - -# # nothing special... -# else -# colorgroup = Any[] -# end - -# # fills/ribbons -# yminmax = Any[] -# if d[:fillrange] != nothing -# fillmin, fillmax = map(makevec, maketuple(d[:fillrange])) -# nmin, nmax = length(fillmin), length(fillmax) -# push!(yminmax, (:ymin, Float64[min(y, fillmin[mod1(i, nmin)], fillmax[mod1(i, nmax)]) for (i,y) in enumerate(d[:y])])) -# push!(yminmax, (:ymax, Float64[max(y, fillmin[mod1(i, nmin)], fillmax[mod1(i, nmax)]) for (i,y) in enumerate(d[:y])])) -# push!(gfargs, Gadfly.Geom.ribbon) -# end - -# # elseif ribbon != nothing -# # ribbon = makevec(ribbon) -# # n = length(ribbon) -# # @show ribbon -# # push!(yminmax, (:ymin, Float64[y - ribbon[mod1(i,n)] for (i,y) in enumerate(d[:y])])) -# # push!(yminmax, (:ymax, Float64[y + ribbon[mod1(i,n)] for (i,y) in enumerate(d[:y])])) -# # push!(gfargs, Gadfly.Geom.ribbon) -# # end - -# # handle markers -# geoms, guides = getMarkerGeomsAndGuides(d, initargs) -# append!(gfargs, geoms) -# append!(gplt.guides, guides) - -# # add a regression line? -# if d[:smooth] -# # TODO: make more flexible -# push!(gfargs, Gadfly.Geom.smooth(method=:lm)) -# # push!(gfargs, Gadfly.Geom.smooth(method=:loess, smoothing=0.95)) -# end - -# # add to the legend, but only without the continuous scale -# for guide in gplt.guides -# if isa(guide, Gadfly.Guide.ManualColorKey) -# # TODO: there's a BUG in gadfly if you pass in the same color more than once, -# # since gadfly will call unique(colors), but doesn't also merge the rows that match -# # Should ensure from this side that colors which are the same are merged together - -# push!(guide.labels, d[:label]) -# push!(guide.colors, getColor(d[d[:markershape] == :none ? :color : :markercolor])) -# end -# end - -# # for histograms, set x=y -# x = d[d[:linetype] == :hist ? :y : :x] - -# if d[:axis] != :left -# warn("Gadfly only supports one y axis") -# end - - -# # add the layer to the Gadfly.Plot -# prepend!(gplt.layers, Gadfly.layer(unique(gfargs)...; x = x, y = d[:y], colorgroup..., yminmax...)) -# nothing -# end - # --------------------------------------------------------------------------- diff --git a/src/backends/gadfly_shapes.jl b/src/backends/gadfly_shapes.jl index e61f34d3..c162142e 100644 --- a/src/backends/gadfly_shapes.jl +++ b/src/backends/gadfly_shapes.jl @@ -70,31 +70,6 @@ function gadflyshape(sv::Shape) ShapeGeometry(sv.vertices) end -# const _square = ShapeGeometry(@compat(Tuple{Float64,Float64})[ -# ( 1.0, -1.0), -# ( 1.0, 1.0), -# (-1.0, 1.0), -# (-1.0, -1.0) -# ]) - -# const _diamond = ShapeGeometry(@compat(Tuple{Float64,Float64})[ -# ( 0.0, -1.0), -# ( 1.0, 0.0), -# ( 0.0, 1.0), -# (-1.0, 0.0) -# ]) - -# const _cross = ShapeGeometry(@compat(Tuple{Float64,Float64})[ -# (-1.0, -0.4), (-1.0, 0.4), # L edge -# (-0.4, 0.4), # BL inside -# (-0.4, 1.0), ( 0.4, 1.0), # B edge -# ( 0.4, 0.4), # BR inside -# ( 1.0, 0.4), ( 1.0, -0.4), # R edge -# ( 0.4, -0.4), # TR inside -# ( 0.4, -1.0), (-0.4, -1.0), # T edge -# (-0.4, -0.4) # TL inside -# ]) - # create a Compose context given a ShapeGeometry and the xs/ys/sizes function makeGadflyShapeContext(geom::ShapeGeometry, xs::AbstractArray, ys::AbstractArray, rs::AbstractArray) @@ -114,287 +89,3 @@ end # --------------------------------------------------------------------------------------------- - - -# Compose pseudo-forms for simple symbols, all parameterized by center and size - -# using Compose: x_measure, y_measure - - -# function createGadflyAnnotation(d::Dict, initargs::Dict) -# sz = [d[:markersize] * Gadfly.px] - -# x, y = d[:x], d[:y] -# marker = d[:markershape] - -# if d[:linetype] == :ohlc -# shape = ohlcshape(x, y, d[:markersize]) -# d[:y] = Float64[z.open for z in y] -# d[:linetype] = :none -# return Gadfly.Guide.annotation(Gadfly.compose(Gadfly.context(), shape, Gadfly.fill(nothing), Gadfly.stroke(getColor(d[:color])))) - -# elseif marker == :rect -# shape = square(x, y, sz) - -# elseif marker == :diamond -# shape = diamond(x, y, sz) - -# elseif marker == :utriangle -# shape = utriangle(x, y, sz) - -# elseif marker == :dtriangle -# shape = utriangle(x, y, sz, -1) - -# elseif marker == :cross -# shape = cross(x, y, sz) - -# elseif marker == :xcross -# shape = xcross(x, y, sz) - -# elseif marker == :star1 -# shape = star1(x, y, sz) - -# elseif marker == :star2 -# shape = star2(x, y, sz) - -# elseif marker == :hexagon -# shape = hexagon(x, y, sz) - -# elseif marker == :octagon -# shape = octagon(x, y, sz) - -# else -# # make circles -# sz = 0.8 * d[:markersize] * Gadfly.px -# xs = collect(float(d[:x])) -# ys = collect(float(d[:y])) -# shape = Gadfly.circle(xs,ys,[sz]) -# end - -# Gadfly.Guide.annotation(Gadfly.compose(Gadfly.context(), shape, Gadfly.fill(getColorVector(d[:markercolor])), Gadfly.stroke(getColor(initargs[:foreground_color])))) -# end - - -# function square(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray) -# n = max(length(xs), length(ys), length(rs)) -# rect_xs = Array(Compose.Measure, n) -# rect_ys = Array(Compose.Measure, n) -# rect_ws = Array(Compose.Measure, n) -# s = 1/sqrt(2) -# for i in 1:n -# x = Compose.x_measure(xs[mod1(i, length(xs))]) -# y = Compose.y_measure(ys[mod1(i, length(ys))]) -# r = rs[mod1(i, length(rs))] - -# rect_xs[i] = x - s*r -# rect_ys[i] = y + s*r -# rect_ws[i] = 2*s*r -# end - -# return Gadfly.rectangle(rect_xs, rect_ys, rect_ws, rect_ws) -# end - - -# function diamond(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray) -# n = max(length(xs), length(ys), length(rs)) -# polys = Array(Vector{@compat(Tuple{Compose.Measure, Compose.Measure})}, n) -# for i in 1:n -# x = Compose.x_measure(xs[mod1(i, length(xs))]) -# y = Compose.y_measure(ys[mod1(i, length(ys))]) -# r = rs[mod1(i, length(rs))] -# polys[i] = [(x, y - r), (x + r, y), (x, y + r), (x - r, y)] -# end - -# return Gadfly.polygon(polys) -# end - - - -# function cross(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray) -# n = max(length(xs), length(ys), length(rs)) -# polys = Array(Vector{@compat(Tuple{Compose.Measure, Compose.Measure})}, n) -# for i in 1:n -# x = Compose.x_measure(xs[mod1(i, length(xs))]) -# y = Compose.y_measure(ys[mod1(i, length(ys))]) -# r = rs[mod1(i, length(rs))] -# u = 0.4r - -# # make a "plus sign" -# polys[i] = [ -# (x-r, y-u), (x-r, y+u), # L edge -# (x-u, y+u), # BL inside -# (x-u, y+r), (x+u, y+r), # B edge -# (x+u, y+u), # BR inside -# (x+r, y+u), (x+r, y-u), # R edge -# (x+u, y-u), # TR inside -# (x+u, y-r), (x-u, y-r), # T edge -# (x-u, y-u) # TL inside -# ] -# end - -# return Gadfly.polygon(polys) -# end - -# function xcross(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray) -# n = max(length(xs), length(ys), length(rs)) -# polys = Array(Vector{@compat(Tuple{Compose.Measure, Compose.Measure})}, n) -# s = 1/sqrt(5) -# for i in 1:n -# x = Compose.x_measure(xs[mod1(i, length(xs))]) -# y = Compose.y_measure(ys[mod1(i, length(ys))]) -# r = rs[mod1(i, length(rs))] -# u = s*r -# polys[i] = [ -# (x, y - u), (x + u, y - 2u), (x + 2u, y - u), -# (x + u, y), (x + 2u, y + u), (x + u, y + 2u), -# (x, y + u), (x - u, y + 2u), (x - 2u, y + u), -# (x - u, y), (x - 2u, y - u), (x - u, y - 2u) -# ] -# end - -# return Gadfly.polygon(polys) -# end - - -# function utriangle(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray, scalar = 1) -# n = max(length(xs), length(ys), length(rs)) -# polys = Array(Vector{@compat(Tuple{Compose.Measure, Compose.Measure})}, n) -# s = 0.8 -# for i in 1:n -# x = Compose.x_measure(xs[mod1(i, length(xs))]) -# y = Compose.y_measure(ys[mod1(i, length(ys))]) -# r = rs[mod1(i, length(rs))] -# u = 0.8 * scalar * r -# polys[i] = [ -# (x - r, y + u), -# (x + r, y + u), -# (x, y - u) -# ] -# end - -# return Gadfly.polygon(polys) -# end - - - -# function star1(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray, scalar = 1) -# n = max(length(xs), length(ys), length(rs)) -# polys = Array(Vector{@compat(Tuple{Compose.Measure, Compose.Measure})}, n) - -# # some magic scalars -# sx1, sx2, sx3 = 0.7, 0.4, 0.2 -# sy1, sy2, sy3 = 1.2, 0.45, 0.1 - -# for i in 1:n -# x = Compose.x_measure(xs[mod1(i, length(xs))]) -# y = Compose.y_measure(ys[mod1(i, length(ys))]) -# r = rs[mod1(i, length(rs))] -# polys[i] = [ -# (x-sx1*r, y+ r), # BL -# (x, y+sy2*r), -# (x+sx1*r, y+ r), # BR -# (x+sx2*r, y+sy3*r), -# (x+ r, y-sy2*r), # R -# (x+sx3*r, y-sy2*r), -# (x, y-sy1*r), # T -# (x-sx3*r, y-sy2*r), -# (x- r, y-sy2*r), # L -# (x-sx2*r, y+sy3*r) -# ] -# end - -# return Gadfly.polygon(polys) -# end - - - -# function star2(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray, scalar = 1) -# n = max(length(xs), length(ys), length(rs)) -# polys = Array(Vector{@compat(Tuple{Compose.Measure, Compose.Measure})}, n) -# for i in 1:n -# x = Compose.x_measure(xs[mod1(i, length(xs))]) -# y = Compose.y_measure(ys[mod1(i, length(ys))]) -# r = rs[mod1(i, length(rs))] -# u = 0.4r -# polys[i] = [ -# (x-u, y), (x-r, y-r), # TL -# (x, y-u), (x+r, y-r), # TR -# (x+u, y), (x+r, y+r), # BR -# (x, y+u), (x-r, y+r) # BL -# ] -# end - -# return Gadfly.polygon(polys) -# end - - - -# function hexagon(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray) -# n = max(length(xs), length(ys), length(rs)) - -# polys = Array(Vector{@compat(Tuple{Compose.Measure, Compose.Measure})}, n) -# for i in 1:n -# x = Compose.x_measure(xs[mod1(i, length(xs))]) -# y = Compose.y_measure(ys[mod1(i, length(ys))]) -# r = rs[mod1(i, length(rs))] -# u = 0.6r - -# polys[i] = [ -# (x-r, y-u), (x-r, y+u), # L edge -# (x, y+r), # B -# (x+r, y+u), (x+r, y-u), # R edge -# (x, y-r) # T -# ] -# end - -# return Gadfly.polygon(polys) -# end - - -# function octagon(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray) -# n = max(length(xs), length(ys), length(rs)) - -# polys = Array(Vector{@compat(Tuple{Compose.Measure, Compose.Measure})}, n) -# for i in 1:n -# x = Compose.x_measure(xs[mod1(i, length(xs))]) -# y = Compose.y_measure(ys[mod1(i, length(ys))]) -# r = rs[mod1(i, length(rs))] -# u = 0.4r - -# polys[i] = [ -# (x-r, y-u), (x-r, y+u), # L edge -# (x-u, y+r), (x+u, y+r), # B edge -# (x+r, y+u), (x+r, y-u), # R edge -# (x+u, y-r), (x-u, y-r), # T edge -# ] -# end - -# return Gadfly.polygon(polys) -# end - - -# # --------------------------- - -# function ohlcshape(xs::AVec, ys::AVec{OHLC}, tickwidth::Real) -# @assert length(xs) == length(ys) -# n = length(xs) -# u = tickwidth * Compose.px -# polys = Array(Vector{@compat(Tuple{Compose.Measure, Compose.Measure})}, n) -# for i in 1:n -# x = Compose.x_measure(xs[i]) -# o = Compose.y_measure(ys[i].open) -# h = Compose.y_measure(ys[i].high) -# l = Compose.y_measure(ys[i].low) -# c = Compose.y_measure(ys[i].close) -# # o,h,l,c = map(Compose.y_measure, ys[i]) -# polys[i] = [ -# (x, o), (x - u, o), (x, o), # open tick -# (x, l), (x, h), (x, c), # high/low bar -# (x + u, c), (x, c) # close tick -# ] -# end -# return Gadfly.polygon(polys) -# end - - -