forked from GiovineItalia/Gadfly.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
violin.jl
50 lines (35 loc) · 1.58 KB
/
violin.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
immutable ViolinGeometry <: Gadfly.GeometryElement
order::Int
tag::Symbol
function ViolinGeometry(; order=1, tag::Symbol=empty_tag)
new(order, tag)
end
end
const violin = ViolinGeometry
element_aesthetics(::ViolinGeometry) = [:x, :y, :color]
default_statistic(::ViolinGeometry) = Gadfly.Stat.violin()
function render(geom::ViolinGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)
# TODO: What should we do with the color aesthetic?
Gadfly.assert_aesthetics_defined("Geom.violin", aes, :y, :width)
Gadfly.assert_aesthetics_equal_length("Geom.violin", aes, :y, :width)
default_aes = Gadfly.Aesthetics()
default_aes.color = PooledDataArray(RGB{Float32}[theme.default_color])
default_aes.x = Float64[0.5]
aes = inherit(aes, default_aes)
n = length(aes.y)
# Group y and width by x
grouped_y = DefaultDict(eltype(aes.x), typeof(aes.y), () -> similar(aes.y, 0))
grouped_width = DefaultDict(eltype(aes.x), typeof(aes.width), () -> similar(aes.width, 0))
for (x, y, w) in zip(cycle(aes.x), aes.y, aes.width)
push!(grouped_y[x], y)
push!(grouped_width[x], w)
end
ctx = context(order=geom.order)
compose!(ctx,
Compose.polygon([vcat([(x - w/2, y) for (y, w) in zip(grouped_y[x], grouped_width[x])],
reverse!([(x + w/2, y) for (y, w) in zip(grouped_y[x], grouped_width[x])]))
for x in keys(grouped_y)], geom.tag))
return compose!(ctx,
fill(theme.default_color),
svgclass("geometry"))
end