From f674bba8610066b79e79c153301dcf58d7952cec Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Thu, 17 Sep 2015 15:28:54 -0400 Subject: [PATCH] added gadfly shapes utriangle, dtriangle, and xcross --- docs/example_generation.jl | 4 +- src/backends/gadfly.jl | 2 +- src/backends/gadfly_shapes.jl | 90 +++++++++++++++++++++++++++++------ 3 files changed, 79 insertions(+), 17 deletions(-) diff --git a/docs/example_generation.jl b/docs/example_generation.jl index 92b97a39..541a37b5 100644 --- a/docs/example_generation.jl +++ b/docs/example_generation.jl @@ -61,7 +61,7 @@ const examples = PlotExample[ [:(styles = setdiff(supportedStyles(), [:auto])), :(plot(rand(20,length(styles)); linestyle=:auto, labels=map(string,styles)))]), PlotExample("Supported marker types", "All options: (:none, :auto, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star1, :star2, :hexagon)", - [:(markers = setdiff(supportedMarkers(), [:none,:auto])), :(plot([fill(i,10) for i=1:length(markers)]; marker=:auto, labels=map(string,markers), markersize=10))]), + [:(markers = setdiff(supportedMarkers(), [:none,:auto])), :(plot([fill(i-1,10) for i=1:length(markers)]; marker=:auto, labels=map(string,markers), markersize=10))]), PlotExample("Bar", "x is the midpoint of the bar. (todo: allow passing of edges instead of midpoints)", [:(bar(randn(1000)))]), @@ -133,7 +133,7 @@ function generate_markdown(pkgname::Symbol) imgname = "$(pkgname)_example_$i.png" # NOTE: uncomment this to overwrite the images as well - savepng("$IMGDIR/$pkgname/$imgname") + # savepng("$IMGDIR/$pkgname/$imgname") # write out the header, description, code block, and image link write(md, "### $(example.header)\n\n") diff --git a/src/backends/gadfly.jl b/src/backends/gadfly.jl index cdd232cd..fbe61f79 100644 --- a/src/backends/gadfly.jl +++ b/src/backends/gadfly.jl @@ -11,7 +11,7 @@ supportedArgs(::GadflyPackage) = setdiff(ARGS, [:heatmap_c, :fillto, :pos]) supportedAxes(::GadflyPackage) = setdiff(ALL_AXES, [:right]) supportedTypes(::GadflyPackage) = [:none, :line, :step, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline, :ohlc] supportedStyles(::GadflyPackage) = [:auto, :solid] -supportedMarkers(::GadflyPackage) = [:none, :auto, :rect, :ellipse, :diamond, :cross] +supportedMarkers(::GadflyPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross] include("gadfly_shapes.jl") diff --git a/src/backends/gadfly_shapes.jl b/src/backends/gadfly_shapes.jl index 109e42ad..66bcd028 100644 --- a/src/backends/gadfly_shapes.jl +++ b/src/backends/gadfly_shapes.jl @@ -22,9 +22,18 @@ function createGadflyAnnotation(d::Dict) 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) + else # make circles sz = 0.8 * d[:markersize] * Gadfly.px @@ -45,9 +54,9 @@ function square(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray) rect_ws = Vector{Compose.Measure}(n) s = 1/sqrt(2) for i in 1:n - x = Compose.x_measure(xs[1 + i % length(xs)]) - y = Compose.y_measure(ys[1 + i % length(ys)]) - r = rs[1 + i % length(rs)] + 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 @@ -63,9 +72,9 @@ function diamond(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray) polys = Vector{Vector{Tuple{Compose.Measure, Compose.Measure}}}(n) for i in 1:n - x = Compose.x_measure(xs[1 + i % length(xs)]) - y = Compose.y_measure(ys[1 + i % length(ys)]) - r = rs[1 + i % length(rs)] + 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] = Tuple{Compose.Measure, Compose.Measure}[(x, y - r), (x + r, y), (x, y + r), (x - r, y)] end @@ -79,22 +88,73 @@ function cross(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray) polys = Vector{Vector{Tuple{Compose.Measure, Compose.Measure}}}(n) s = 1/sqrt(5) for i in 1:n - x = Compose.x_measure(xs[1 + i % length(xs)]) - y = Compose.y_measure(ys[1 + i % length(ys)]) - r = rs[1 + i % length(rs)] + 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] = Tuple{Compose.Measure, Compose.Measure}[ - (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) ] + (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 -# Base.isfinite{T<:Real}(x::Tuple{T,T,T,T}) = isfinite(x[1]) && isfinite(x[2]) && isfinite(x[3]) && isfinite(x[4]) +function xcross(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray) + n = max(length(xs), length(ys), length(rs)) + + polys = Vector{Vector{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" + polys[i] = Tuple{Compose.Measure, Compose.Measure}[ + (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 utriangle(xs::AbstractArray, ys::AbstractArray, rs::AbstractArray, scalar = 1) + n = max(length(xs), length(ys), length(rs)) + + polys = Vector{Vector{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] = Tuple{Compose.Measure, Compose.Measure}[ + (x - r, y + u), + (x + r, y + u), + (x, y - u) + ] + end + + return Gadfly.polygon(polys) +end + + + + +# --------------------------- function ohlcshape{T}(xs::AVec, ys::AVec{Tuple{T,T,T,T}}, tickwidth::Real) @assert length(xs) == length(ys) @@ -113,3 +173,5 @@ function ohlcshape{T}(xs::AVec, ys::AVec{Tuple{T,T,T,T}}, tickwidth::Real) return Gadfly.polygon(polys) end + +