Skip to content

Commit

Permalink
enable alpha for Geom.rect (#1439)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattriks authored May 19, 2020
1 parent 05bf50f commit 44d9ca8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
19 changes: 11 additions & 8 deletions docs/src/gallery/geometries.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ De = dataset("ggplot2","economics")
De.Unemploy /= 10^3
plot(De, x=:Date, y=:Unemploy, Geom.line,
layer(Dp, xmin=:Start, xmax=:End, Geom.vband, color=:Party),
layer(Dp, xmin=:Start, xmax=:End, Geom.vband, color=:Party, alpha=[0.6]),
Scale.color_discrete_manual("deepskyblue", "lightcoral"),
Coord.cartesian(xmin=Date("1965-01-01"), ymax=12),
Guide.xlabel("Time"), Guide.ylabel("Unemployment (x10³)"), Guide.colorkey(title=""),
Guide.xlabel("Time"), Guide.ylabel("Unemployment (x10³)"), Guide.colorkey(title=""),
Theme(default_color="black", key_position=:top))
```

Expand Down Expand Up @@ -434,13 +434,16 @@ plot(x=[0, 1, 1, 2, 2, 3, 3, 2, 2, 1, 1, 0, 4, 5, 5, 4],
## [`Geom.rect`](@ref), [`Geom.rectbin`](@ref)

```@example
using Gadfly, Colors, DataFrames, RDatasets
using Gadfly, DataFrames
set_default_plot_size(21cm, 8cm)
theme1 = Theme(default_color=RGBA(0, 0.75, 1.0, 0.5))
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, theme1)
pb = plot(D, xmin=:x1, ymin=:y1, xmax=:x2, ymax=:y2, Geom.rect, theme1)
hstack(pa, pb)
x1, y1, w1 = 0.5:10, rand(10), 0.09.+0.4*rand(10)
D = DataFrame(x=x1, y=rand(x1, 10), y1=y1, x2=x1.+w1, y2=y1.+w1, c=0:9)
p1 = plot(D, xmin=:x, ymin=:y1, xmax=:x2, ymax=:y2, color=[colorant"green"],
alpha=1:10, Geom.rect, Scale.alpha_discrete)
p2 = plot(D, xmin=:x, ymin=:y1, xmax=:x2, ymax=:y2, color=:c, alpha=[0.7],
Geom.rect, Guide.ylabel(nothing))
p3 = plot(D, x=:x, y=:y, color=:c, alpha=[0.5], Geom.rectbin, Scale.color_discrete)
hstack(p1, p2, p3)
```

```@example
Expand Down
32 changes: 17 additions & 15 deletions src/geom/rectbin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ end
default_statistic(geom::RectangularBinGeometry) = geom.default_statistic

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

# Render rectangular bin (e.g., heatmap) geometry.
#
Expand All @@ -65,7 +65,8 @@ element_aesthetics(::RectangularBinGeometry) =
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])
default_aes.color = RGBA{Float32}[theme.default_color]
default_aes.alpha = Float64[theme.alphas[1]]
aes = inherit(aes, default_aes)

Gadfly.assert_aesthetics_defined("RectangularBinGeometry", aes, :xmin, :xmax, :ymin, :ymax)
Expand All @@ -83,36 +84,37 @@ function render(geom::RectangularBinGeometry, theme::Gadfly.Theme, aes::Gadfly.A
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
AT, CT = eltype(aes.alpha), eltype(aes.color)
aes_color = Vector{CT}(undef, n)
aes_alpha = Vector{Float64}(undef, n)
alphav = AT <: Int ? theme.alphas[aes.alpha] : aes.alpha
for (i, (_, c, a)) in enumerate(Compose.cyclezip(xmin, aes.color, alphav))
aes_color[i] = c
aes_alpha[i] = a
end

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

if !allvisible
visibility = cs .!= nothing
cs = cs[visibility]
visibility = aes_color .!== nothing
aes_color = aes_color[visibility]
aes_alpha = aes_alpha[visibility]
xmin = xmin[visibility]
xmax = xmax[visibility]
xwidths = xwidths[visibility]
ymin = ymin[visibility]
ymax = ymax[visibility]
ywidths = ywidths[visibility]
end

return compose!(
context(),
rectangle(xmin, ymin, xwidths, ywidths, geom.tag),
fill(cs),
fill(aes_color), fillopacity(aes_alpha),
stroke(nothing),
svgclass("geometry"),
svgattribute("shape-rendering", "crispEdges"))
Expand Down
4 changes: 2 additions & 2 deletions test/testscripts/rectbin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ set_default_plot_size(6inch, 3inch)
# plot(dataset("Zelig", "macro"), x="Year", y="Country", color="GDP", Geom.rectbin)

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)
pa = plot(D, x=:x, y=:y, alpha=[0.5], Geom.rectbin)
pb = plot(D, xmin=:x1, ymin=:y1, xmax=:x2, ymax=:y2, alpha=[0.5], Geom.rect)
hstack(pa,pb)

0 comments on commit 44d9ca8

Please sign in to comment.