forked from GiovineItalia/Gadfly.jl
/
misc.jl
183 lines (141 loc) · 3.54 KB
/
misc.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
# Work around differences in julia 0.2 and 0.3 set constructors.
function set(T::Type, itr)
S = Set{T}()
union!(S, itr)
end
function set(itr)
set(Any, itr)
end
# Is this usable data?
function isconcrete{T<:Number}(x::T)
!isna(x) && isfinite(x)
end
function isconcrete(x)
!isna(x)
end
# How many concrete elements in an iterable
function concrete_length(xs)
n = 0
for x in xs
if isconcrete(x)
n += 1
end
end
n
end
function concrete_minimum(xs)
if done(xs, start(xs))
error("argument must not be empty")
end
x_min = first(xs)
for x in xs
if Gadfly.isconcrete(x) && isfinite(x)
x_min = x
break
end
end
for x in xs
if Gadfly.isconcrete(x) && isfinite(x) && x < x_min
x_min = x
end
end
return x_min
end
function concrete_maximum(xs)
if done(xs, start(xs))
error("argument must not be empty")
end
x_max = first(xs)
for x in xs
if Gadfly.isconcrete(x) && isfinite(x)
x_max = x
break
end
end
for x in xs
if Gadfly.isconcrete(x) && isfinite(x) && x > x_max
x_max = x
end
end
return x_max
end
function nonzero_length(xs)
n = 0
for x in xs
if x != 0
n += 1
end
end
n
end
function default_formatter(xs)
[string(x) for x in xs]
end
# Create a new object of type T from a with missing values (i.e., those set to
# nothing) inherited from b.
function inherit{T}(a::T, b::T)
c = copy(a)
inherit!(c, b)
c
end
function inherit!{T}(a::T, b::T)
for field in T.names
aval = getfield(a, field)
bval = getfield(b, field)
# TODO: this is a hack to let non-default labelers overide the defaults
if aval === nothing || aval === string || aval == default_formatter
setfield!(a, field, bval)
elseif typeof(aval) <: Dict && typeof(bval) <: Dict
merge!(aval, getfield(b, field))
end
end
nothing
end
isnothing(u) = is(u, nothing)
issomething(u) = !isnothing(u)
negate(f) = x -> !f(x)
function has{T,N}(xs::AbstractArray{T,N}, y::T)
for x in xs
if x == y
return true
end
end
return false
end
Maybe(T) = Union(T, Nothing)
function lerp(x::Float64, a::Float64, b::Float64)
a + (b - a) * max(min(x, 1.0), 0.0)
end
# Generate a unique id, primarily for assigning IDs to SVG elements.
let next_unique_svg_id_num = 0
global unique_svg_id
function unique_svg_id()
uid = @sprintf("id%d", next_unique_svg_id_num)
next_unique_svg_id_num += 1
uid
end
end
# Remove any markup or whitespace from a string.
function escape_id(s::String)
s = replace(s, r"<[^>]*>", "")
s = replace(s, r"\s", "_")
s
end
# Can a numerical value be treated as an integer
is_int_compatable(::Integer) = true
is_int_compatable{T <: FloatingPoint}(x::T) = abs(x) < maxintfloat(T) && float(int(x)) == x
is_int_compatable(::Any) = false
# Construct a jscall to store arbitrary data in the element
function jsdata(key::String, value::String, arg::Vector{Measure}=Measure[])
return jscall(
"""
data("$(escape_string(key))", $(value))
""", arg)
end
# Construct jscall properties to store arbitrary data in plotroot elements.
function jsplotdata(key::String, value::String, arg::Vector{Measure}=Measure[])
return jscall(
"""
plotroot().data("$(escape_string(key))", $(value))
""", arg)
end