Skip to content

Commit

Permalink
Develop RectangularGeometry
Browse files Browse the repository at this point in the history
  • Loading branch information
CiaranOMara committed Aug 1, 2018
1 parent 675a586 commit a2648b1
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 10 deletions.
110 changes: 110 additions & 0 deletions src/geom/rect.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using Compose: x_measure, y_measure

struct RectangularGeometry <: Gadfly.GeometryElement
default_statistic::Gadfly.StatisticElement
tag::Symbol
end

function RectangularGeometry(
default_statistic::Gadfly.StatisticElement=Gadfly.Stat.identity();
tag=empty_tag)
RectangularGeometry(default_statistic, tag)
end

"""
Geom.rect
Draw colored rectangles with the corners specified by the
`xmin`, `xmax`, `ymin` and `ymax` aesthetics. Optionally
specify their `color`.
"""
const rect = RectangularGeometry

default_statistic(geom::RectangularGeometry) = geom.default_statistic

element_aesthetics(::RectangularGeometry) =
[:xmin, :xmax, :ymin, :ymax, :color]

# Render rectangle geometry.
#
# Args:
# geom: rect geometry
# theme: the plot's theme
# aes: some aesthetics
#
# Returns
# A compose form.
#
function render(geom::RectangularGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)

default_aes = Gadfly.Aesthetics()
default_aes.color = discretize_make_ia(RGBA{Float32}[theme.default_color])
aes = inherit(aes, default_aes)

Gadfly.assert_aesthetics_defined("RectangularGeometry", aes, :xmin, :xmax, :ymin, :ymax)
Gadfly.assert_aesthetics_equal_length("RectangularGeometry", aes, :xmin, :xmax, :ymin, :ymax)

nx = length(aes.xmin)
ny = length(aes.ymin)
n = nx

xmin = aes.xmin
xwidths = [(x1 - x0)*cx
for (x0, x1) in zip(aes.xmin, aes.xmax)]

ymin = aes.ymin
ywidths = [(y1 - y0)*cy
for (y0, y1) in zip(aes.ymin, aes.ymax)]

if length(aes.color) == n
cs = aes.color
else
cs = Array{RGBA{Float32}}(n)
for i in 1:n
cs[i] = aes.color[((i - 1) % length(aes.color)) + 1]
end
end

allvisible = true
for c in cs
if c == nothing
allvisible = false
break
end
end

if !allvisible
visibility = cs .!= nothing
cs = cs[visibility]
xmin = xmin[visibility]
xmax = xmax[visibility]
ymin = ymin[visibility]
ymax = ymax[visibility]
end

return compose!(
context(),
rectangle(xmin, ymin, xwidths, ywidths, geom.tag),
fill(cs),
stroke(nothing),
svgclass("geometry"),
svgattribute("shape-rendering", "crispEdges"))

# TODO: determine performance hit of using polygons.
# polys = Vector{Vector{Tuple{Measure, Measure}}}(n)
# for i in 1:n
# x0 = x_measure(aes.xmin[i])
# x1 = x_measure(aes.xmax[i])
# y0 = y_measure(aes.ymin[i])
# y1 = y_measure(aes.ymax[i])
# polys[i] = Tuple{Measure, Measure}[(x0, y0), (x0, y1), (x1, y1), (x1, y0)]
# end
#
# return compose!(
# context(),
# Compose.polygon(polys),
# fill(color),
# stroke(nothing),
# svgclass("geometry"),
# svgattribute("shape-rendering", "crispEdges"))
end
16 changes: 8 additions & 8 deletions src/geom/rectbin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ specify their `color`.
"""
const rectbin = RectangularBinGeometry

"""
Geom.rect
Draw colored rectangles with the corners specified by the
`xmin`, `xmax`, `ymin` and `ymax` aesthetics. Optionally
specify their `color`.
"""
rect() = RectangularBinGeometry(Gadfly.Stat.Identity())
# """
# Geom.rect
#
# Draw colored rectangles with the corners specified by the
# `xmin`, `xmax`, `ymin` and `ymax` aesthetics. Optionally
# specify their `color`.
# """
# rect() = RectangularBinGeometry(Gadfly.Stat.Identity())

"""
Geom.histogram2d[(; xbincount=nothing, xminbincount=3, xmaxbincount=150,
Expand Down
1 change: 1 addition & 0 deletions src/geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ include("geom/hvband.jl")
include("geom/label.jl")
include("geom/line.jl")
include("geom/point.jl")
include("geom/rect.jl")
include("geom/rectbin.jl")
include("geom/subplot.jl")
include("geom/ribbon.jl")
Expand Down
6 changes: 6 additions & 0 deletions test/testscripts/rect.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using DataFrames, Gadfly

set_default_plot_size(6inch, 3inch)

D = DataFrame(x1=[0,0.5], y1=[0,0.5], x2=[1,1.5], y2=[1,1.5])
pb = plot(D, xmin=:x1, ymin=:y1, xmax=:x2, ymax=:y2, Geom.rect)
2 changes: 0 additions & 2 deletions test/testscripts/rectbin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ set_default_plot_size(6inch, 3inch)

D = DataFrame(x=[0.5,1], y=[0.5,1], x1=[0,0.5], y1=[0,0.5], x2=[1,1.5], y2=[1,1.5])
pa = plot(D, x=:x, y=:y, Geom.rectbin)
pb = plot(D, xmin=:x1, ymin=:y1, xmax=:x2, ymax=:y2, Geom.rect)
hstack(pa,pb)

0 comments on commit a2648b1

Please sign in to comment.