Skip to content

Commit

Permalink
Refactor attempt
Browse files Browse the repository at this point in the history
Attempt to apply_statistic to band.  But receive LoadError: MethodError: no method matching isfinite(::Measures.Length{:h,Float64}) (I need to look into this further). 

There may be a case for adding  an apply_aesthetics_transform in the method "cascade".
  • Loading branch information
CiaranOMara committed Aug 1, 2018
1 parent a2648b1 commit d3a2aa8
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 65 deletions.
115 changes: 54 additions & 61 deletions src/geom/hvband.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using Compose: x_measure, y_measure

# Band geometry summarizes data as vertical or horizontal bands.
struct BandGeometry <: Gadfly.GeometryElement
orientation::Symbol
color::Union{Vector, Color, (Void)}
tag::Symbol
BandGeometry(orientation, color, tag) = new(orientation, color === nothing ? nothing : Gadfly.parse_colorant(color), tag)
end
# # Band geometry summarizes data as vertical or horizontal bands.
# struct BandGeometry <: Gadfly.GeometryElement
# default_statistic::Gadfly.StatisticElement
# orientation::Symbol
# color::Union{Vector, Color, (Void)}
# tag::Symbol
# BandGeometry(orientation, color, tag) = new(orientation, color === nothing ? nothing : Gadfly.parse_colorant(color), tag)
# end

band(;orientation=:vertical) = RectangularGeometry(Stat.band(orientation))

HBandGeometry(; color = nothing, tag = empty_tag) = BandGeometry(:horizontal, color, tag)
# HBandGeometry(; color = nothing, tag = empty_tag) = BandGeometry(Stat.:horizontal, color, tag)

"""
Geom.hband[(; color=nothing)]
Expand All @@ -20,10 +22,10 @@ Draw horizontal bands across the plot canvas with a vertical span specified by `
- `color`:
Default is `Theme.default_color`.
"""
const hband = HBandGeometry
hband() = band(orientation=:horizontal)


VBandGeometry(; color = nothing, tag = empty_tag) = BandGeometry(:vertical, color, tag)
# VBandGeometry(; color = nothing, tag = empty_tag) = BandGeometry(:vertical, color, tag)

"""
Geom.vband[(; color=nothing)]
Expand All @@ -34,60 +36,51 @@ Draw vertical bands across the plot canvas with a horizontal span specified by `
- `color`:
Default is `Theme.default_color`.
"""
const vband = VBandGeometry
const vband = band


element_aesthetics(geom::BandGeometry) = geom.orientation == :vertical ?
[:xmin, :xmax, :color] : [:ymin, :ymax, :color]
# element_aesthetics(geom::BandGeometry) = geom.orientation == :vertical ?
# [:xmin, :xmax, :color] : [:ymin, :ymax, :color]


# Generate a form for the bad geometry

# # Generate a form for the bad geometry
# #
# # Args:
# # geom: band geometry.
# # theme: the plot's theme.
# # aes: aesthetics.
# #
# # Returns:
# # A compose Form.
# #
# function render(geom::BandGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)
#
# Args:
# geom: band geometry.
# theme: the plot's theme.
# aes: aesthetics.
# if geom.orientation == :horizontal
# Gadfly.assert_aesthetics_defined("BandGeometry", aes, :ymin, :ymax)
# Gadfly.assert_aesthetics_equal_length("BandGeometry", aes, :ymin, :ymax)
#
# Returns:
# A compose Form.
# n = max(length(aes.ymin)) #Note: already passed check for equal lengths.
#
function render(geom::BandGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)

if geom.orientation == :horizontal
Gadfly.assert_aesthetics_defined("BandGeometry", aes, :ymin, :ymax)
Gadfly.assert_aesthetics_equal_length("BandGeometry", aes, :ymin, :ymax)

n = max(length(aes.ymin)) #Note: already passed check for equal lengths.

aes.xmin = fill(0w, n)
xwidths = fill(1w, n)

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

elseif geom.orientation == :vertical
Gadfly.assert_aesthetics_defined("BandGeometry", aes, :xmin, :xmax)
Gadfly.assert_aesthetics_equal_length("BandGeometry", aes, :xmin, :xmax)

n = max(length(aes.xmin)) #Note: already passed check for equal lengths.

aes.ymin = fill(0h, n)
ywidths = fill(1h, n)

xwidths = [(x1 - x0) * cx
for (x0, x1) in zip(aes.xmin, aes.xmax)]
else
error("Orientation must be :horizontal or :vertical")
end

color = geom.color === nothing ? theme.default_color : geom.color

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

end
# aes.xmin = fill(0w, n)
# aes.xmax = fill(1w, n)
#
# elseif geom.orientation == :vertical
# Gadfly.assert_aesthetics_defined("BandGeometry", aes, :xmin, :xmax)
# Gadfly.assert_aesthetics_equal_length("BandGeometry", aes, :xmin, :xmax)
#
# n = max(length(aes.xmin)) #Note: already passed check for equal lengths.
#
# aes.ymin = fill(0h, n)
# aes.ymax = fill(1h, n)
#
# else
# error("Orientation must be :horizontal or :vertical")
# end
#
# color = geom.color === nothing ? theme.default_color : geom.color #TODO: merge with or overwrite aes.color and determine if a deepcopy of aes needs to be sent.
#
# aes.color = typeof(color) <: Vector ? color : [color]
#
# return render(RectangularGeometry(geom.default_statistic, geom.tag), theme, aes)
#
# end
51 changes: 49 additions & 2 deletions src/statistics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ data with `coverage_weight`; and of having a nice numbering with
granularity_weight=1/4,
simplicity_weight=1/6,
coverage_weight=1/3,
niceness_weight=1/4) =
niceness_weight=1/4) =
TickStatistic("x",
granularity_weight, simplicity_weight, coverage_weight, niceness_weight, ticks)

Expand Down Expand Up @@ -1654,7 +1654,7 @@ function apply_statistic(stat::ViolinStatistic,

uxflag && (grouped_y = Dict(x=>aes.y[aes.x.==x] for x in ux))

grouped_color = (colorflag ? Dict(x=>first(aes.color[aes.x.==x]) for x in ux) :
grouped_color = (colorflag ? Dict(x=>first(aes.color[aes.x.==x]) for x in ux) :
uxflag && Dict(x=>nothing for x in ux) )

aes.x = Array{Float64}(0)
Expand Down Expand Up @@ -2037,4 +2037,51 @@ function Gadfly.Stat.apply_statistic(stat::EllipseStatistic,
aes.y = ellipse_y
end


struct BandStatistic <: Gadfly.StatisticElement
orientation::Symbol # :horizontal or :vertical like BarStatistic or HairStatistic
end

function BandStatistic(;orientation=:vertical)
return BandStatistic(orientation)
end

input_aesthetics(stat::BandStatistic) = [:xmin, :xmax, :ymin, :ymax]
output_aesthetics(stat::BandStatistic) = [:xmin, :xmax, :ymin, :ymax]
default_scales(stat::BandStatistic) = [Scale.x_continuous(), Scale.y_continuous()]

"""
Stat.band[(; orientation=:vertical)]
Transform points in $(aes2str(input_aesthetics(band()))) into lines in
$(aes2str(output_aesthetics(band()))). Used by [`Geom.band`](@ref Gadfly.Geom.band), [`Geom.hband`](@ref Gadfly.Geom.band), [`Geom.vband`](@ref Gadfly.Geom.band).
"""
const band = BandStatistic

function apply_statistic(stat::BandStatistic,
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)

if stat.orientation == :horizontal
# aes.xmin = fill(stat.intercept, length(aes.ymin))
# aes.xmax = fill(Inf, length(aes.ymax))

n = max(length(aes.ymin)) #Note: already passed check for equal lengths.

aes.xmin = fill(0w, n)
aes.xmax = fill(1w, n)
elseif stat.orientation == :vertical
# aes.ymin = fill(stat.intercept, length(aes.xmin))
# aes.ymax = fill(Inf, length(aes.xmax))

n = max(length(aes.xmin)) #Note: already passed check for equal lengths.

aes.ymin = fill(0h, n)
aes.ymax = fill(1h, n)
else
error("Orientation must be :horizontal or :vertical")
end
end

end # module Stat
6 changes: 4 additions & 2 deletions test/testscripts/hvband.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using Gadfly, RDatasets
set_default_plot_size(21cm, 8cm)
p1 = plot(dataset("datasets", "iris"), x="SepalLength", y="SepalWidth", Geom.point,
layer(xmin=[5.0, 7.0], xmax=[6.5, 8.0] , Geom.vband(color="green")))
# layer(xmin=[5.0, 7.0], xmax=[6.5, 8.0] , Geom.vband(), color="green"))
layer(xmin=[5.0, 7.0], xmax=[6.5, 8.0] , Geom.vband()))
p2 = plot(dataset("datasets", "iris"), x="SepalLength", y="SepalWidth", Geom.point,
layer(ymin=[2.5, 3.6], ymax=[3.0, 4.0], Geom.hband(color="red")))
# layer(ymin=[2.5, 3.6], ymax=[3.0, 4.0], Geom.hband(), color="red"))
layer(ymin=[2.5, 3.6], ymax=[3.0, 4.0], Geom.hband()))
hstack(p1,p2)

0 comments on commit d3a2aa8

Please sign in to comment.