Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
CiaranOMara committed Mar 24, 2019
1 parent 6e1e238 commit 53e456f
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/geom/rect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const rect = RectangularGeometry
default_statistic(geom::RectangularGeometry) = geom.default_statistic

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

# Render rectangle geometry.
#
Expand Down
82 changes: 3 additions & 79 deletions src/geom/rectbin.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
struct RectangularBinGeometry <: Gadfly.GeometryElement
default_statistic::Gadfly.StatisticElement
tag::Symbol
end

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

"""
Expand All @@ -28,82 +23,11 @@ information.
"""
function histogram2d(; xbincount=nothing, xminbincount=3, xmaxbincount=150,
ybincount=nothing, yminbincount=3, ymaxbincount=150)
RectangularBinGeometry(
RectangularGeometry(
Gadfly.Stat.histogram2d(xbincount = xbincount,
xminbincount = xminbincount,
xmaxbincount = xmaxbincount,
ybincount = ybincount,
yminbincount = yminbincount,
ymaxbincount = ymaxbincount))
end

default_statistic(geom::RectangularBinGeometry) = geom.default_statistic

element_aesthetics(::RectangularBinGeometry) =
[:x, :y, :xmin, :xmax, :ymin, :ymax, :color]

# Render rectangular bin (e.g., heatmap) geometry.
#
# Args:
# geom: rectbin geometry
# theme: the plot's theme
# aes: some aesthetics
#
# Returns
# A compose form.
#
function render(geom::RectangularBinGeometry, 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("RectangularBinGeometry", aes, :xmin, :xmax, :ymin, :ymax)
Gadfly.assert_aesthetics_equal_length("RectangularBinGeometry", aes, :xmin, :xmax, :ymin, :ymax)

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

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

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

if length(aes.color) == n
cs = aes.color
else
cs = Array{RGBA{Float32}}(undef, 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"))
end
end

0 comments on commit 53e456f

Please sign in to comment.