Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geom.shape #887

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/geom/shape.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@


# Geometry which displays arbitrary shapes at given (x, y) positions.
immutable ShapeGeometry{T} <: Gadfly.GeometryElement
vertices::T
tag::Symbol
end

function ShapeGeometry(shape; tag::Symbol=Gadfly.Geom.empty_tag)
ShapeGeometry(shape, tag)
end

const shape = ShapeGeometry


function Gadfly.element_aesthetics(::ShapeGeometry)
[:x, :y, :size, :color]
end


# Generate a form for a shape geometry.
#
# Args:
# geom: shape geometry.
# theme: the plot's theme.
# aes: aesthetics.
#
# Returns:
# A compose Form.
#
function Gadfly.render(geom::ShapeGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)

Gadfly.assert_aesthetics_defined("Geom.shape", aes, :x, :y)
Gadfly.assert_aesthetics_equal_length("Geom.shape", aes,
element_aesthetics(geom)...)

default_aes = Gadfly.Aesthetics()
default_aes.color = Gadfly.DataFrames.PooledDataArray(RGBA{Float32}[theme.default_color])
default_aes.size = Compose.Measure[theme.default_point_size]
aes = Gadfly.inherit(aes, default_aes)

lw_hover_scale = 10
lw_ratio = theme.line_width / aes.size[1]

aes_x, aes_y = Gadfly.concretize(aes.x, aes.y)

ctx = Compose.compose!(
Compose.context(),
make_polygon(geom, aes.x, aes.y, aes.size),
Compose.fill(aes.color),
Compose.linewidth(theme.highlight_width))

if aes.color_key_continuous != nothing && aes.color_key_continuous
Compose.compose!(ctx,
Compose.stroke(map(theme.continuous_highlight_color, aes.color)))
else
Compose.compose!(ctx,
Compose.stroke(map(theme.discrete_highlight_color, aes.color)),
Compose.svgclass([Gadfly.svg_color_class_from_label(Gadfly.escape_id(aes.color_label([c])[1]))
for c in aes.color]))
end

return Compose.compose!(Compose.context(order=4), Compose.svgclass("geometry"), ctx)
end


# create a Compose context given a ShapeGeometry and the xs/ys/sizes
function make_polygon(geom::ShapeGeometry, xs::AbstractArray, ys::AbstractArray, rs::AbstractArray)
n = max(length(xs), length(ys), length(rs))
T = Tuple{Compose.Measure, Compose.Measure}
polys = Array(Vector{T}, 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] = T[(x + r * sx, y + r * sy) for (sx,sy) in geom.vertices]
end
Gadfly.polygon(polys, geom.tag)
end


# ---------------------------------------------------------------------------------------------
1 change: 1 addition & 0 deletions src/geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ include("geom/ribbon.jl")
include("geom/violin.jl")
include("geom/polygon.jl")
include("geom/beeswarm.jl")
include("geom/shape.jl")

end # module Geom