Skip to content

Commit

Permalink
WIP: add Geom.candlestick
Browse files Browse the repository at this point in the history
  • Loading branch information
iblislin committed Oct 24, 2018
1 parent 036dde2 commit 69b9a51
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/aesthetics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ const aesthetic_aliases =
:x_tick => :xtick,
:y_tick => :ytick,
:x_grid => :xgrid,
:y_grid => :ygrid)
:y_grid => :ygrid,
:open => :lower_hinge,
:high => :upper_fence,
:low => :lower_fence,
:close => :upper_hinge)


# Index as if this were a data frame
Expand Down
37 changes: 28 additions & 9 deletions src/geom/boxplot.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
struct BoxplotGeometry <: Gadfly.GeometryElement
default_statistic::Gadfly.StatisticElement
suppress_outliers::Bool
suppress_fences::Bool
candlestick::Bool
tag::Symbol
end

BoxplotGeometry(; method=:tukey, suppress_outliers=false, tag=empty_tag) =
BoxplotGeometry(Gadfly.Stat.boxplot(method=method), suppress_outliers, tag)
function BoxplotGeometry(; method=:tukey, suppress_outliers=false, suppress_fences=false,
candlestick=false, tag=empty_tag)
stat = candlestick ? Gadfly.Stat.identity() : Gadfly.Stat.boxplot(method=method)
BoxplotGeometry(stat, suppress_outliers, suppress_fences, candlestick, tag)
end

"""
Geom.boxplot[(; method=:tukey, suppress_outliers=false)]
Geom.boxplot[(; method=:tukey, suppress_outliers=false, suppress_fences=false)]
Draw box plots of the `middle`, `lower_hinge`, `upper_hinge`, `lower_fence`,
`upper_fence`, and `outliers` aesthetics. The categorical `x` aesthetic is
Expand All @@ -20,6 +25,16 @@ fences, and outliers aesthetics will be computed using [`Stat.boxplot`](@ref).
"""
const boxplot = BoxplotGeometry

"""
Geom.candlestick[()]
Draw candlestick plot from `open`, `high`, `low`, `close`.
`x` denotes a vector of `TimeType`,
e.g. `x = Date(2018, 1, 1):Day(1):Date(2018, 1, 5)`.
"""
candlestick() =
BoxplotGeometry(suppress_outliers=true, suppress_fences=true, candlestick=true)

element_aesthetics(::BoxplotGeometry) = [:x, :color,
:middle,
:upper_fence, :lower_fence,
Expand Down Expand Up @@ -83,6 +98,15 @@ function render(geom::BoxplotGeometry, theme::Gadfly.Theme, aes::Gadfly.Aestheti
subtags(geom.tag, :box, :lower_whisker, :upper_whisker,
:lower_fence, :upper_fence, :outliers, :middle)

fences = if !geom.suppress_fences
(Compose.line([[(x - fw/2, lf), (x + fw/2, lf)]
for (x, lf) in zip(xs, lower_fence)], tlf),
Compose.line([[(x - fw/2, uf), (x + fw/2, uf)]
for (x, uf) in zip(xs, upper_fence)], tuf))
else
()
end

ctx = compose!(
context(tag=geom.tag),
fill(collect(cs)),
Expand All @@ -107,12 +131,7 @@ function render(geom::BoxplotGeometry, theme::Gadfly.Theme, aes::Gadfly.Aestheti
Compose.line([[(x, uh), (x, uf)]
for (x, uh, uf) in zip(xs, upper_hinge, upper_fence)], tuw),

# Fences
Compose.line([[(x - fw/2, lf), (x + fw/2, lf)]
for (x, lf) in zip(xs, lower_fence)], tlf),

Compose.line([[(x - fw/2, uf), (x + fw/2, uf)]
for (x, uf) in zip(xs, upper_fence)], tuf),
fences...,

stroke(collect(cs))
),
Expand Down

0 comments on commit 69b9a51

Please sign in to comment.