-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
display.jl
353 lines (293 loc) · 10.7 KB
/
display.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# ----------------------------------------- #
# SyncPlot -- sync Plot object with display #
# ----------------------------------------- #
mutable struct SyncPlot
plot::PlotlyBase.Plot
scope::Scope
window::Union{Nothing,Blink.Window}
end
Base.getindex(p::SyncPlot, key) = p.scope[key] # look up Observables
@WebIO.register_renderable(SyncPlot) do plot
return WebIO.render(plot.scope)
end
function Base.show(io::IO, mm::MIME"text/html", p::SyncPlot)
# if we are rendering docs -- short circuit and display html
if get_renderer() == DOCS
return show(io, mm, p.plot, full_html=false, include_plotlyjs="require-loaded", include_mathjax=missing)
end
show(io, mm, p.scope)
end
Base.show(io::IO, mm::MIME"application/prs.juno.plotpane+html", p::SyncPlot) = show(io, mm, p.scope)
function SyncPlot(
p::Plot;
kwargs...
)
lowered = JSON.lower(p)
id = string("#plot-", p.divid)
# setup scope
deps = [
"Plotly" => _js_path,
joinpath(@__DIR__, "..", "assets", "plotly_webio.bundle.js")
]
scope = Scope(imports=deps)
scope.dom = dom"div"(id=string("plot-", p.divid))
# INPUT: Observables for plot events
scope["hover"] = Observable(Dict())
scope["selected"] = Observable(Dict())
scope["click"] = Observable(Dict())
scope["relayout"] = Observable(Dict())
scope["image"] = Observable("")
scope["__gd_contents"] = Observable{Any}(Dict()) # for testing
# OUTPUT: setup an observable which sends modify commands
scope["_commands"] = Observable{Any}([])
scope["_toImage"] = Observable(Dict())
scope["_downloadImage"] = Observable(Dict())
scope["__get_gd_contents"] = Observable("")
onjs(scope["_toImage"], @js function (options)
this.Plotly.toImage(this.plotElem, options).then(function (data)
$(scope["image"])[] = data
end)
end)
onjs(scope["__get_gd_contents"], @js function (prop)
if prop == "data"
$(scope["__gd_contents"])[] = this.plotElem.data
end
if prop == "layout"
$(scope["__gd_contents"])[] = this.plotElem.layout
end
end)
onjs(scope["_downloadImage"], @js function (options)
this.Plotly.downloadImage(this.plotElem, options)
end)
# Do the respective action when _commands is triggered
onjs(scope["_commands"], @js function (args)
@var fn = args.shift()
@var elem = this.plotElem
@var Plotly = this.Plotly
args.unshift(elem) # use div as first argument
Plotly[fn].apply(this, args)
end)
onimport(scope, JSExpr.@js function (Plotly, PlotlyWebIO)
# Add the PlotlyCommands object to the WebIO JS instance
PlotlyWebIO.init(WebIO)
# set up container element
@var gd = this.dom.querySelector($id);
# save some vars for later
this.plotElem = gd
this.Plotly = Plotly
if (window.Blink !== undefined)
# set css style for auto-resize
gd.style.width = "100%";
gd.style.height = "100vh";
gd.style.marginLeft = "0%";
gd.style.marginTop = "0vh";
end
window.onresize = () -> Plotly.Plots.resize(gd)
# Draw plot in container
Plotly.newPlot(
gd,
$(lowered[:data]),
$(lowered[:layout]),
$(lowered[:config]),
)
# hook into plotly events
gd.on("plotly_hover", function (data)
@var filtered_data = WebIO.PlotlyCommands.filterEventData(gd, data, "hover");
if !(filtered_data.isnil)
$(scope["hover"])[] = filtered_data.out
end
end)
gd.on("plotly_unhover", () -> $(scope["hover"])[] = Dict())
gd.on("plotly_selected", function (data)
@var filtered_data = WebIO.PlotlyCommands.filterEventData(gd, data, "selected");
if !(filtered_data.isnil)
$(scope["selected"])[] = filtered_data.out
end
end)
gd.on("plotly_deselect", () -> $(scope["selected"])[] = Dict())
gd.on("plotly_relayout", function (data)
@var filtered_data = WebIO.PlotlyCommands.filterEventData(gd, data, "relayout");
if !(filtered_data.isnil)
$(scope["relayout"])[] = filtered_data.out
end
end)
gd.on("plotly_click", function (data)
@var filtered_data = WebIO.PlotlyCommands.filterEventData(gd, data, "click");
if !(filtered_data.isnil)
$(scope["click"])[] = filtered_data.out
end
end)
end)
# create no-op `on` callback for image so it is _always_ sent
# to us
on(scope["image"]) do x end
SyncPlot(p, scope, nothing)
end
function plot(args...; kwargs...)
SyncPlot(Plot(args...; kwargs...))
end
# Add some basic Julia API methods on SyncPlot that just forward onto the Plot
Base.size(sp::SyncPlot) = size(sp.plot)
Base.copy(sp::SyncPlot) = SyncPlot(copy(sp.plot))
Base.display(::PlotlyJSDisplay, p::SyncPlot) = display_blink(p::SyncPlot)
function display_blink(p::SyncPlot)
sizeBuffer = 1.15
plotSize = size(p.plot)
windowOptions = Dict(
"width" => floor(Int, plotSize[1] * sizeBuffer),
"height" => floor(Int, plotSize[2] * sizeBuffer)
)
p.window = Blink.Window(windowOptions)
Blink.body!(p.window, p.scope)
end
function Base.close(p::SyncPlot)
if p.window !== nothing && Blink.active(p.window)
close(p.window)
end
end
function send_command(scope, cmd, args...)
# The handler for _commands is set up when plot is constructed
scope["_commands"][] = [cmd, args...]
nothing
end
function add_trace!(p::SyncPlot, trace::GenericTrace; kw...)
add_trace!(p.plot, trace; kw...)
send_command(p.scope, :react, p.plot.data, p.plot.layout)
end
# ----------------------- #
# Plotly.js api functions #
# ----------------------- #
# for each of these we first update the Julia object, then update the display
function restyle!(
plt::SyncPlot, ind::Union{Int,AbstractVector{Int}},
update::AbstractDict=Dict();
kwargs...
)
restyle!(plt.plot, ind, update; kwargs...)
send_command(plt.scope, :restyle, merge(update, prep_kwargs(kwargs)), ind .- 1)
end
function restyle!(plt::SyncPlot, update::AbstractDict=Dict(); kwargs...)
restyle!(plt.plot, update; kwargs...)
send_command(plt.scope, :restyle, merge(update, prep_kwargs(kwargs)))
end
function relayout!(plt::SyncPlot, update::AbstractDict=Dict(); kwargs...)
relayout!(plt.plot, update; kwargs...)
update!(plt, layout=plt.plot.layout)
end
function react!(plt::SyncPlot, data::AbstractVector{<:AbstractTrace}, layout::Layout)
react!(plt.plot, data, layout)
send_command(plt.scope, :react, data, layout)
end
function update!(
plt::SyncPlot, ind::Union{Int,AbstractVector{Int}},
update::AbstractDict=Dict();
layout::Layout=Layout(),
kwargs...)
update!(plt.plot, ind, update; layout=layout, kwargs...)
send_command(plt.scope, :update, merge(update, prep_kwargs(kwargs)), layout, ind .- 1)
end
function update!(
plt::SyncPlot, update::AbstractDict=Dict(); layout::Layout=Layout(),
kwargs...
)
update!(plt.plot, update; layout=layout, kwargs...)
send_command(plt.scope, :update, merge(update, prep_kwargs(kwargs)), layout)
end
function addtraces!(plt::SyncPlot, traces::AbstractTrace...)
addtraces!(plt.plot, traces...)
send_command(plt.scope, :addTraces, traces)
end
function addtraces!(plt::SyncPlot, i::Int, traces::AbstractTrace...)
addtraces!(plt.plot, i, traces...)
send_command(plt.scope, :addTraces, traces, i - 1)
end
function deletetraces!(plt::SyncPlot, inds::Int...)
deletetraces!(plt.plot, inds...)
send_command(plt.scope, :deleteTraces, collect(inds) .- 1)
end
function movetraces!(plt::SyncPlot, to_end::Int...)
movetraces!(plt.plot, to_end...)
send_command(plt.scope, :moveTraces, traces, collect(to_end) .- 1)
end
function movetraces!(
plt::SyncPlot, src::AbstractVector{Int}, dest::AbstractVector{Int}
)
movetraces!(plt.plot, src, dest)
send_command(plt.scope, :moveTraces, src .- 1, dest .- 1)
end
function redraw!(plt::SyncPlot)
redraw!(plt.plot)
send_command(plt.scope, :redraw)
end
function purge!(plt::SyncPlot)
purge!(plt.plot)
send_command(plt.scope, :purge)
end
function to_image(plt::SyncPlot; kwargs...)
to_image(plt.plot)
plt.scope["image"][] = "" # reset
plt.scope["_toImage"][] = Dict(kwargs)
tries = 0
while length(plt.scope["image"][]) == 0
tries == 10 && error("Could not get image")
sleep(0.25)
tries += 1
end
return plt["image"].val
end
function download_image(plt::SyncPlot; kwargs...)
download_image(plt.plot)
plt.scope["_downloadImage"][] = Dict(kwargs)
nothing
end
# unexported (by plotly.js) api methods
function extendtraces!(plt::SyncPlot, update::AbstractDict,
indices::AbstractVector{Int}=[1], maxpoints=-1;)
extendtraces!(plt.plot, update, indices, maxpoints)
send_command(
plt.scope, :extendTraces, prep_kwargs(update), indices .- 1, maxpoints
)
end
function prependtraces!(plt::SyncPlot, update::AbstractDict,
indices::AbstractVector{Int}=[1], maxpoints=-1;)
prependtraces!(plt.plot, update, indices, maxpoints)
send_command(
plt.scope, :prependTraces, prep_kwargs(update), indices .- 1, maxpoints
)
end
for f in [:restyle, :relayout, :update, :addtraces, :deletetraces,
:movetraces, :redraw, :extendtraces, :prependtraces, :purge, :react]
f! = Symbol(f, "!")
@eval function $(f)(plt::SyncPlot, args...; kwargs...)
out = SyncPlot(deepcopy(plt.plot))
$(f!)(out, args...; kwargs...)
out
end
end
# add extra same extra methods we have on ::Plot for these functions
for f in (:extendtraces!, :prependtraces!)
@eval begin
function $(f)(p::SyncPlot, inds::Vector{Int}=[0],
maxpoints=-1; update...)
d = Dict()
for (k, v) in update
d[k] = _tovec(v)
end
($f)(p, d, inds, maxpoints)
end
function $(f)(p::SyncPlot, ind::Int, maxpoints=-1;
update...)
($f)(p, [ind], maxpoints; update...)
end
function $(f)(p::SyncPlot, update::AbstractDict, ind::Int,
maxpoints=-1)
($f)(p, update, [ind], maxpoints)
end
end
end
for mime in ["text/plain", "application/vnd.plotly.v1+json", "application/prs.juno.plotpane+html"]
function Base.show(io::IO, m::MIME{Symbol(mime)}, p::SyncPlot, args...)
show(io, m, p.plot, args...)
end
end
PlotlyBase.savejson(sp::SyncPlot, fn::String) = PlotlyBase.savejson(sp.plot, fn)