forked from GiovineItalia/Gadfly.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoord.jl
280 lines (232 loc) · 8.12 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
module Coord
using Gadfly
using Compat
using Compose
import Gadfly.Maybe
export cartesian
struct Cartesian <: Gadfly.CoordinateElement
xvars::Vector{Symbol}
yvars::Vector{Symbol}
xmin
xmax
ymin
ymax
xflip::Bool
yflip::Bool
fixed::Bool
aspect_ratio::Union{(Nothing), Float64}
raster::Bool
Cartesian(xvars, yvars, xmin, xmax, ymin, ymax, xflip, yflip, fixed, aspect_ratio, raster) =
new(xvars, yvars, xmin, xmax, ymin, ymax, xflip, yflip, fixed,
isa(aspect_ratio, Real) ? Float64(aspect_ratio) : aspect_ratio, raster)
end
function Cartesian(
xvars=[:x, :xmin, :xmax, :xintercept],
yvars=[:y, :ymin, :ymax, :yintercept, :middle,
:lower_hinge, :upper_hinge, :lower_fence, :upper_fence, :outliers];
xflip=false, yflip=false,
xmin=nothing, xmax=nothing, ymin=nothing, ymax=nothing,
fixed=false, aspect_ratio=nothing, raster=false)
Cartesian(xvars, yvars, xmin, xmax, ymin, ymax, xflip, yflip, fixed, aspect_ratio, raster)
end
"""
Coord.cartesian(; xmin=nothing, xmax=nothing, ymin=nothing, ymax=nothing,
xflip=false, yflip=false,
aspect_ratio=nothing, fixed=false,
raster=false)
`xmin`, `xmax`, `ymin`, and `ymax` specify hard minimum and maximum values on
the x and y axes, and override the soft limits in [`Scale.x_continuous`](@ref)
and [`Scale.y_continuous`](@ref). if `xflip` or `yflip` are `true` the
respective axis is flipped. `aspect_ratio` fulfills its namesake if not
`nothing`, unless overridden by a `fixed` value of `true`, in which case the
aspect ratio follows the units of the plot (e.g. if the y-axis is 5 units high
and the x-axis in 10 units across, the plot will be drawn at an aspect ratio of
2).
"""
const cartesian = Cartesian
# Return the first concrete aesthetic value in one of the given aesthetics
#
# Args:
# aess: An array of Aesthetics to search through.
# vars: Aesthetic variables to search though.
#
# Returns:
# A concrete value if one is found, otherwise nothing.
#
function first_concrete_aesthetic_value(aess::Vector{Gadfly.Aesthetics}, vars::Vector{Symbol})
T = aesthetics_type(aess, vars)
for var in vars
for aes in aess
vals = getfield(aes, var)
vals === nothing && continue
if !isa(vals, AbstractArray)
vals = [vals]
end
if var == :outliers
for outlier_vals in aes.outliers
for val in outlier_vals
Gadfly.isconcrete(val) && return convert(T, val)
end
end
continue
end
for val in vals
Gadfly.isconcrete(val) && return convert(T, val)
end
end
end
return nothing
end
# Find a common type among a group of aesthetics.
#
# Args:
# aess: An array of Aesthetics to search through.
# vars: Aesthetic variables to search though.
#
# Returns:
# A common type.
function aesthetics_type(aess::Vector{Gadfly.Aesthetics},
vars::Vector{Symbol})
T = Union{}
for var in vars
for aes in aess
vals = getfield(aes, var)
vals === nothing && continue
if var == :outliers
if !isempty(vals)
T = promote_type(T, eltype(first(vals)))
end
else
T = promote_type(T, eltype(vals))
end
end
end
return T
end
# Produce a context with suitable cartesian coordinates.
#
# Args:
# coord: cartesian coordinate instance.
#
# Returns:
# A compose Canvas.
#
function apply_coordinate(coord::Cartesian, aess::Vector{Gadfly.Aesthetics},
scales::Dict{Symbol, Gadfly.ScaleElement})
pad_categorical_x = missing
pad_categorical_y = missing
for aes in aess
if aes.pad_categorical_x !== missing
if pad_categorical_x === missing
pad_categorical_x = aes.pad_categorical_x
else
pad_categorical_x = pad_categorical_x || aes.pad_categorical_x
end
end
if aes.pad_categorical_y !== missing
if pad_categorical_y === missing
pad_categorical_y = aes.pad_categorical_y
else
pad_categorical_y = pad_categorical_y || aes.pad_categorical_y
end
end
end
xmin = xmax = first_concrete_aesthetic_value(aess, coord.xvars)
if xmin != nothing
for var in coord.xvars
for aes in aess
vals = getfield(aes, var)
vals === nothing && continue
if !isa(vals, AbstractArray)
vals = [vals]
end
xmin, xmax = Gadfly.concrete_minmax(vals, xmin, xmax)
end
end
end
ymin = ymax = first_concrete_aesthetic_value(aess, coord.yvars)
if ymin != nothing
for var in coord.yvars
for aes in aess
vals = getfield(aes, var)
vals === nothing && continue
# Outliers is an odd aesthetic that needs special treatment.
if var == :outliers
for outlier_vals in aes.outliers
ymin, ymax = Gadfly.concrete_minmax(outlier_vals, ymin, ymax)
end
continue
end
if !isa(vals, AbstractArray)
vals = [vals]
end
ymin, ymax = Gadfly.concrete_minmax(vals, ymin, ymax)
end
end
end
xviewmin = xviewmax = yviewmin = yviewmax = nothing
# viewmin/max that is set explicitly should override min/max
for aes in aess
if aes.xviewmin != nothing
xviewmin = xviewmin === nothing ? aes.xviewmin : min(xviewmin, aes.xviewmin)
end
if aes.xviewmax != nothing
xviewmax = xviewmax === nothing ? aes.xviewmax : max(xviewmax, aes.xviewmax)
end
if aes.yviewmin != nothing
yviewmin = yviewmin === nothing ? aes.yviewmin : min(yviewmin, aes.yviewmin)
end
if aes.yviewmax != nothing
yviewmax = yviewmax === nothing ? aes.yviewmax : max(yviewmax, aes.yviewmax)
end
end
xmax = xviewmax === nothing ? xmax : max(xmax, xviewmax)
xmin = xviewmin === nothing ? xmin : min(xmin, xviewmin)
ymax = yviewmax === nothing ? ymax : max(ymax, yviewmax)
ymin = yviewmin === nothing ? ymin : min(ymin, yviewmin)
# Hard limits set in Coord should override everything else
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
xpadding = Scale.iscategorical(scales, :x) ? 0mm : 2mm
ypadding = Scale.iscategorical(scales, :y) ? 0mm : 2mm
if Scale.iscategorical(scales, :x) && (pad_categorical_x===missing || pad_categorical_x)
xmin -= 0.5
xmax += 0.5
end
if Scale.iscategorical(scales, :y) && (pad_categorical_y===missing || pad_categorical_y)
ymin -= 0.5
ymax += 0.5
end
width = xmax - xmin
height = ymax - ymin
compose!(
context(units=UnitBox(
coord.xflip ? xmax : xmin,
coord.yflip ? ymin : ymax,
coord.xflip ? -width : width,
coord.yflip ? height : -height,
leftpad=xpadding,
rightpad=xpadding,
toppad=ypadding,
bottompad=ypadding),
raster=coord.raster),
svgclass("plotpanel"))
end
struct SubplotGrid <: Gadfly.CoordinateElement
end
function apply_coordinate(coord::SubplotGrid, aess::Vector{Gadfly.Aesthetics},
scales::Dict{Symbol, Gadfly.ScaleElement})
compose!(context(), svgclass("plotpanel"))
end
const subplot_grid = SubplotGrid
end # module Coord