-
Notifications
You must be signed in to change notification settings - Fork 5
/
io.jl
299 lines (268 loc) · 7.34 KB
/
io.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
struct Dependency
name::String
path::String
has_export::Bool
end
function write_html_head(io::IO)
print(io, """
<link href="https://cdnjs.cloudflare.com/ajax/libs/dc/1.7.5/dc.css" rel="stylesheet">
<style>
.fake-link {
color: #337ab7;
text-decoration: underline;
cursor: pointer;
font-size: 12px;
}
.button-label {
color: #337ab7;
text-decoration: underline;
cursor: pointer;
margin-right: 20px;
}
</style>
""")
end
function write_html_chart_entry(io::IO, chart::DCChart, indent::Int=0)
tabbing = " "^indent
println(io, tabbing, "<div style=\"float:left;\">")
println(io, tabbing, " <h3 id=\"heading_", chart.parent, "\">", chart.title, " </h3>")
println(io, tabbing, " <div id=\"", chart.parent, "\"></div>")
println(io, tabbing, "</div>")
end
function write_html_widget_entry(io::IO, widget::DCWidget, indent::Int=0)
tabbing = " "^indent
println(io, tabbing, widget.html, "<br/>")
end
function write_html_body(io::IO, dcout::DCOut)
print(io, "<body>
<div id=\"reset_all_well_", dcout.output_id, "\"style=\"width: 100%; text-align: right;\"> </div>
")
for chart in dcout.charts
write_html_chart_entry(io, chart, 1)
end
print(io, """
<div style="clear:both; height: 1px"></div>
""")
for widget in dcout.widgets
write_html_widget_entry(io, widget, 0)
end
println(io, "</body>")
end
function write_script_dependencies(io::IO, dependencies::Vector{Dependency})
print(io, """require.config({paths: {""")
for i in 1 : length(dependencies)
#=
@printf(io, "<script src='%s'></script>\n", src)
=#
@printf(io, """ "%s": "%s" """, dependencies[i].name, dependencies[i].path)
if i < length(dependencies)
print(io, ",\n")
end
end
print(io, """}});\n""")
end
function write_json_entry(io, names::Vector{Symbol}, values::Vector{Any})
print(io, "{")
for i in 1 : length(names)
print(io, "\"", names[i], "\": ")
v = values[i]
if isa(v, AbstractString)
print(io, "\"", v, "\"")
else
print(io, v)
end
if i < length(names)
print(io, ", ")
end
end
print(io, "}")
end
function write_data(io::IO, dcout::DCOut)
df = dcout.df
values = Vector{Any}(undef, length(dcout.dims))
colnames = map(dim->dim.name, dcout.dims)
print(io, "data = [")
for i in 1:nrow(df)
for (j,dim) in enumerate(dcout.dims)
values[j] = df[i,dim.name]
end
write_json_entry(io, colnames, values)
if i < nrow(df)
print(io, ",")
end
end
println(io, "];")
end
function write_reset_script(io::IO, dcout::DCOut)
print(io, "var charts = [")
for i in 1 : length(dcout.charts)
print(io, dcout.charts[i].parent)
if i < length(dcout.charts)
print(io, ", ")
end
end
println(io, "];")
print(io, "var chart_names = [")
for i in 1 : length(dcout.charts)
print(io, "\"", dcout.charts[i].parent, "\"")
if i < length(dcout.charts)
print(io, ", ")
end
end
println(io, "];")
print(io, """
var update_reset_buttons = function(chart) {
var filter_in_use = false;
for (var i = 0; i < charts.length; i++) {
if (charts[i].filters().length > 0) {
filter_in_use = true;
break;
}
}
var idx = charts.indexOf(chart);
d3.select("#reset_all_btn_""", dcout.output_id,
"""").remove();
d3.select("#heading_" + chart_names[idx]).select(".chart-reset").remove();
if (filter_in_use) {
d3.select("#reset_all_well_""", dcout.output_id, """")
.append("a")
.attr("class", "button-label")
.attr("id", "reset_all_btn_""", dcout.output_id, """")
.text(function(d) {
return "Reset All";
})
.on("click", function() {
for (var i = 0; i < charts.length; i++) {
charts[i].filter(null);
}
dc.redrawAll();
});
if (chart.filters().length > 0) {
d3.select("#heading_" + chart_names[idx])
.append("span")
.attr("class", "chart-reset fake-link")
.text(function(d) {
return "Reset";
})
.on("click", function(d) {
chart.filter(null);
dc.redrawAll();
});
}
}
};
for (var i = 0; i < charts.length; i++) {
charts[i].on('filtered', function(chart) {
update_reset_buttons(chart);
});
}
""")
end
function quote_corrector() # doesn't actually do anything, just corrects quotes on sublime
x = """
a"c
"""
end
function write_script(io::IO, dcout::DCOut, dependencies::Vector{Dependency})
println(io, """<script type="text/javascript">""")
write_script_dependencies(io, dependencies)
print(io, "require([")
for i in 1 : length(dependencies)
print(io, "\"", dependencies[i].name, "\"")
if i < length(dependencies)
print(io, ", ")
end
end
print(io, "], function(")
unused_counter = 1;
for i in 1 : length(dependencies)
if (dependencies[i].has_export)
print(io, dependencies[i].name)
else
print(io, "unused", unused_counter)
unused_counter += 1
end
if i < length(dependencies)
print(io, ", ")
end
end
print(io, ") {\n")
write_data(io, dcout)
# crossfilter
println(io, "var cf = crossfilter(data);")
println(io, "var all = cf.groupAll();")
# dimensions
for dim in dcout.dims
write(io, dim)
print(io, "\n")
end
# groups
for group in dcout.groups
write(io, group)
print(io, "\n")
end
# charts
for chart in dcout.charts
write(io, chart, 1)
end
# widget
for widget in dcout.widgets
write(io, widget, 1)
end
println(io, "dc.renderAll();")
write_reset_script(io, dcout)
println(io, "});\n</script>")
end
function write_source_html(io::IO, dcout::DCOut)
write_html_head(io)
write_html_body(io, dcout)
# Note: dependencies must be ordered! (hence why this is not a dictionary)
dependencies = [Dependency("d3","https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min", true),
Dependency("crossfilter","https://cdnjs.cloudflare.com/ajax/libs/crossfilter/1.3.7/crossfilter", false),
Dependency("dc","https://cdnjs.cloudflare.com/ajax/libs/dc/1.7.1/dc", true)]
write_script(io, dcout, dependencies)
end
function Base.show(io::IO, ::MIME"text/html", dcout::DCOut)
# If IJulia is present, go for it
if isdefined(Main, :IJulia)
#=
1 - determine chart layout
2 - generate iframe html + js page
3 - write link to it
=#
iframe_width = 975 # [pix]
row_height = 275 # [pix]x`
ncharts = length(dcout.charts)
widget_height_addition = 0
for widget in dcout.widgets
if widget.typ.concreteName == "dataCount"
widget_height_addition += 15
else
widget_height_addition += 150
end
end
has_widget = length(dcout.widgets) > 0
nrows = ceil(Int, ncharts/3)
iframe_height = nrows*275 + widget_height_addition + 15
randomize_ids!(dcout)
if dcout.elastic_height
write(io, """<div style="width:$(iframe_width)px;">""")
else
write(io, """<div style="width:$(iframe_width)px; height: $(iframe_height)px; overflow-y: auto;">""")
end
write_source_html(io, dcout)
write(io, """</div>""")
if dcout.html_debug
f = open("output.html", "w")
write(f, """<script type="text/javascript" src="http://requirejs.org/docs/release/2.2.0/minified/require.js"></script>
""")
write(f, """<div style="width:$(iframe_width)px; height: $(iframe_height)px; overflow-y: auto;">""")
write_source_html(f, dcout)
write(f, """</div>""")
close(f)
end
else
# TODO
# decide what to do in the absence of IJulia
end
end