forked from GiovineItalia/Gadfly.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coord.jl
184 lines (152 loc) · 4.63 KB
/
coord.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
module Coord
using Gadfly
using Compose
using DataArrays
import Gadfly.Maybe
import Iterators: cycle
export cartesian
# Cartesian coordinates with position given by the x and y (and similar)
# aesthetics.
#
# Args:
# xvars: Aesthetics to consider when choosing x bounds.
# yvars: Aesthetics to consider when choosing y bounds.
# xmin, xmax, ymin, ymax:
# Force a particular x or y bound, rather than trying to choose it
# from the data.
#
#
immutable Cartesian <: Gadfly.CoordinateElement
xvars::Vector{Symbol}
yvars::Vector{Symbol}
xmin
xmax
ymin
ymax
xflip
yflip
function Cartesian(
xvars=[:x, :xviewmin, :xviewmax, :xmin, :xmax],
yvars=[:y, :yviewmin, :yviewmax, :ymin, :ymax, :middle,
:lower_hinge, :upper_hinge, :lower_fence, :upper_fence, :outliers];
xflip::Bool=false, yflip::Bool=false,
xmin=nothing, xmax=nothing, ymin=nothing, ymax=nothing)
new(xvars, yvars, xmin, xmax, ymin, ymax, xflip, yflip)
end
end
const cartesian = Cartesian
# Produce a canvas with suitable cartesian coordinates.
#
# Args:
# coord: cartesian coordinate instance.
#
# Returns:
# A compose Canvas.
#
function apply_coordinate(coord::Cartesian, aess::Gadfly.Aesthetics...)
xmin = nothing
xmax = nothing
for var in coord.xvars
for aes in aess
if getfield(aes, var) === nothing
continue
end
vals = getfield(aes, var)
if !isa(vals, AbstractArray)
vals = {vals}
end
for val in vals
if !Gadfly.isconcrete(val)
continue
end
if xmin === nothing || val < xmin
xmin = val
end
if xmax === nothing || val > xmax
xmax = val
end
end
end
end
ymin = nothing
ymax = nothing
for var in coord.yvars
for aes in aess
if getfield(aes, var) === nothing
continue
end
# Outliers is an odd aesthetic that needs special treatment.
if var == :outliers
for vals in aes.outliers
for val in vals
if !Gadfly.isconcrete(val)
continue
end
if ymin === nothing || val < ymin
ymin = val
end
if ymax === nothing || val > ymax
ymax = val
end
end
end
continue
end
vals = getfield(aes, var)
if !isa(vals, AbstractArray)
vals = {vals}
end
for val in vals
if !Gadfly.isconcrete(val)
continue
end
if ymin === nothing || val < ymin
ymin = val
end
if ymax === nothing || val > ymax
ymax = val
end
end
end
end
xmin = coord.xmin === nothing ? xmin : coord.xmin
xmax = coord.xmax === nothing ? xmax : coord.xmax
ymin = coord.ymin === nothing ? ymin : coord.ymin
ymax = coord.ymax === nothing ? ymax : coord.ymax
if xmin === nothing || !isfinite(xmin)
xmin = 0.0
xmax = 1.0
end
if ymin === nothing || !isfinite(ymin)
ymin = 0.0
ymax = 1.0
end
# A bit of kludge. We need to extend a bit to be able to fit bars when
# using discrete scales. TODO: Think more carefully about this. Is there a
# way for the geometry to let the coordinates know that a little extra room
# is needed to draw everything?
if all([aes.x === nothing || typeof(aes.x) <: PooledDataArray for aes in aess])
xmin -= 0.5
xmax += 0.5
xpadding = 0.0
else
xpadding = 0.03 * (xmax - xmin)
end
if all([aes.y === nothing || typeof(aes.y) <: PooledDataArray for aes in aess])
ymin -= 0.5
ymax += 0.5
ypadding = 0.0
else
ypadding = 0.03 * (ymax - ymin)
end
width = xmax - xmin + 2.0 * xpadding
height = ymax - ymin + 2.0 * ypadding
compose(
canvas(unit_box=UnitBox(
coord.xflip ? xmax + xpadding : xmin - xpadding,
coord.yflip ? ymin - ypadding : ymax + ypadding,
coord.xflip ? -width : width,
coord.yflip ? height : -height)),
svgclass("plotpanel"))
end
end # module Coord