Merge pull request #2131 from daschw/plotlyjs
PlotlyJS backend rewrite (fix #1721 #1756 #1934 #2003)
This commit is contained in:
commit
96e1b1d105
@ -804,9 +804,12 @@ plotly_series_json(plt::Plot) = JSON.json(plotly_series(plt), 4)
|
|||||||
|
|
||||||
# ----------------------------------------------------------------
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
|
html_head(plt::Plot{PlotlyBackend}) = plotly_html_head(plt)
|
||||||
|
html_body(plt::Plot{PlotlyBackend}) = plotly_html_body(plt)
|
||||||
|
|
||||||
const ijulia_initialized = Ref(false)
|
const ijulia_initialized = Ref(false)
|
||||||
|
|
||||||
function html_head(plt::Plot{PlotlyBackend})
|
function plotly_html_head(plt::Plot)
|
||||||
local_file = ("file://" * plotly_local_file_path)
|
local_file = ("file://" * plotly_local_file_path)
|
||||||
plotly = use_local_dependencies[] ? local_file : plotly_remote_file_path
|
plotly = use_local_dependencies[] ? local_file : plotly_remote_file_path
|
||||||
if isijulia() && !ijulia_initialized[]
|
if isijulia() && !ijulia_initialized[]
|
||||||
@ -825,7 +828,7 @@ function html_head(plt::Plot{PlotlyBackend})
|
|||||||
return "<script src=$(repr(plotly))></script>"
|
return "<script src=$(repr(plotly))></script>"
|
||||||
end
|
end
|
||||||
|
|
||||||
function html_body(plt::Plot{PlotlyBackend}, style = nothing)
|
function plotly_html_body(plt, style = nothing)
|
||||||
if style == nothing
|
if style == nothing
|
||||||
w, h = plt[:size]
|
w, h = plt[:size]
|
||||||
style = "width:$(w)px;height:$(h)px;"
|
style = "width:$(w)px;height:$(h)px;"
|
||||||
@ -841,17 +844,14 @@ function html_body(plt::Plot{PlotlyBackend}, style = nothing)
|
|||||||
html
|
html
|
||||||
end
|
end
|
||||||
|
|
||||||
function js_body(plt::Plot{PlotlyBackend}, uuid)
|
function js_body(plt::Plot, uuid)
|
||||||
js = """
|
js = """
|
||||||
PLOT = document.getElementById('$(uuid)');
|
PLOT = document.getElementById('$(uuid)');
|
||||||
Plotly.plot(PLOT, $(plotly_series_json(plt)), $(plotly_layout_json(plt)));
|
Plotly.plot(PLOT, $(plotly_series_json(plt)), $(plotly_layout_json(plt)));
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function plotly_show_js(io::IO, plot::Plot)
|
||||||
# ----------------------------------------------------------------
|
|
||||||
|
|
||||||
function _show(io::IO, ::MIME"application/vnd.plotly.v1+json", plot::Plot{PlotlyBackend})
|
|
||||||
data = []
|
data = []
|
||||||
for series in plot.series_list
|
for series in plot.series_list
|
||||||
append!(data, plotly_series(plot, series))
|
append!(data, plotly_series(plot, series))
|
||||||
@ -860,6 +860,12 @@ function _show(io::IO, ::MIME"application/vnd.plotly.v1+json", plot::Plot{Plotly
|
|||||||
JSON.print(io, Dict(:data => data, :layout => layout))
|
JSON.print(io, Dict(:data => data, :layout => layout))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
|
function _show(io::IO, ::MIME"application/vnd.plotly.v1+json", plot::Plot{PlotlyBackend})
|
||||||
|
plotly_show_js(io, plot)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function _show(io::IO, ::MIME"text/html", plt::Plot{PlotlyBackend})
|
function _show(io::IO, ::MIME"text/html", plt::Plot{PlotlyBackend})
|
||||||
write(io, standalone_html(plt))
|
write(io, standalone_html(plt))
|
||||||
|
|||||||
@ -1,78 +1,44 @@
|
|||||||
# https://github.com/sglyon/PlotlyJS.jl
|
# https://github.com/sglyon/PlotlyJS.jl
|
||||||
|
|
||||||
# --------------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
function plotlyjs_syncplot(plt::Plot{PlotlyJSBackend})
|
||||||
function _create_backend_figure(plt::Plot{PlotlyJSBackend})
|
plt[:overwrite_figure] && closeall()
|
||||||
if !isplotnull() && plt[:overwrite_figure] && isa(current().o, PlotlyJS.SyncPlot)
|
plt.o = PlotlyJS.plot()
|
||||||
PlotlyJS.SyncPlot(PlotlyJS.Plot(), options = current().o.options)
|
traces = PlotlyJS.GenericTrace[]
|
||||||
else
|
for series_dict in plotly_series(plt)
|
||||||
PlotlyJS.plot()
|
plotly_type = pop!(series_dict, :type)
|
||||||
|
push!(traces, PlotlyJS.GenericTrace(plotly_type; series_dict...))
|
||||||
end
|
end
|
||||||
|
PlotlyJS.addtraces!(plt.o, traces...)
|
||||||
|
layout = plotly_layout(plt)
|
||||||
|
w, h = plt[:size]
|
||||||
|
PlotlyJS.relayout!(plt.o, layout, width = w, height = h)
|
||||||
|
return plt.o
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------------------
|
||||||
|
|
||||||
function _series_added(plt::Plot{PlotlyJSBackend}, series::Series)
|
const _plotlyjs_mimeformats = Dict(
|
||||||
syncplot = plt.o
|
"application/pdf" => "pdf",
|
||||||
pdicts = plotly_series(plt, series)
|
"image/png" => "png",
|
||||||
for pdict in pdicts
|
"image/svg+xml" => "svg",
|
||||||
typ = pop!(pdict, :type)
|
"image/eps" => "eps",
|
||||||
gt = PlotlyJS.GenericTrace(typ; pdict...)
|
)
|
||||||
PlotlyJS.addtraces!(syncplot, gt)
|
|
||||||
end
|
for (mime, fmt) in _plotlyjs_mimeformats
|
||||||
|
@eval _show(io::IO, ::MIME{Symbol($mime)}, plt::Plot{PlotlyJSBackend}) = PlotlyJS.savefig(io, plotlyjs_syncplot(plt), format = $fmt)
|
||||||
end
|
end
|
||||||
|
|
||||||
function _series_updated(plt::Plot{PlotlyJSBackend}, series::Series)
|
# Use the Plotly implementation for json and html:
|
||||||
xsym, ysym = (ispolar(series) ? (:t,:r) : (:x,:y))
|
_show(io::IO, mime::MIME"application/vnd.plotly.v1+json", plt::Plot{PlotlyJSBackend}) = plotly_show_js(io, plt)
|
||||||
kw = KW(xsym => (series.plotattributes[:x],), ysym => (series.plotattributes[:y],))
|
|
||||||
z = series[:z]
|
|
||||||
if z != nothing
|
|
||||||
kw[:z] = (isa(z,Surface) ? transpose_z(series, series[:z].surf, false) : z,)
|
|
||||||
end
|
|
||||||
PlotlyJS.restyle!(
|
|
||||||
plt.o,
|
|
||||||
findfirst(isequal(series), plt.series_list),
|
|
||||||
kw
|
|
||||||
)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
html_head(plt::Plot{PlotlyJSBackend}) = plotly_html_head(plt)
|
||||||
|
html_body(plt::Plot{PlotlyJSBackend}) = plotly_html_body(plt)
|
||||||
|
|
||||||
# ----------------------------------------------------------------
|
_show(io::IO, ::MIME"text/html", plt::Plot{PlotlyJSBackend}) = write(io, standalone_html(plt))
|
||||||
|
|
||||||
function _update_plot_object(plt::Plot{PlotlyJSBackend})
|
_display(plt::Plot{PlotlyJSBackend}) = display(plotlyjs_syncplot(plt))
|
||||||
pdict = plotly_layout(plt)
|
|
||||||
syncplot = plt.o
|
|
||||||
w,h = plt[:size]
|
|
||||||
PlotlyJS.relayout!(syncplot, pdict, width = w, height = h)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------
|
|
||||||
|
|
||||||
_show(io::IO, ::MIME"text/html", plt::Plot{PlotlyJSBackend}) = show(io, MIME("text/html"), plt.o)
|
|
||||||
_show(io::IO, ::MIME"image/svg+xml", plt::Plot{PlotlyJSBackend}) = PlotlyJS.savefig(io, plt.o, format="svg")
|
|
||||||
_show(io::IO, ::MIME"image/png", plt::Plot{PlotlyJSBackend}) = PlotlyJS.savefig(io, plt.o, format="png")
|
|
||||||
_show(io::IO, ::MIME"application/pdf", plt::Plot{PlotlyJSBackend}) = PlotlyJS.savefig(io, plt.o, format="pdf")
|
|
||||||
_show(io::IO, ::MIME"image/eps", plt::Plot{PlotlyJSBackend}) = PlotlyJS.savefig(io, plt.o, format="eps")
|
|
||||||
|
|
||||||
function _show(io::IO, m::MIME"application/vnd.plotly.v1+json", plt::Plot{PlotlyJSBackend})
|
|
||||||
show(io, m, plt.o)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
function write_temp_html(plt::Plot{PlotlyJSBackend})
|
|
||||||
filename = string(tempname(), ".html")
|
|
||||||
savefig(plt, filename)
|
|
||||||
filename
|
|
||||||
end
|
|
||||||
|
|
||||||
function _display(plt::Plot{PlotlyJSBackend})
|
|
||||||
if get(ENV, "PLOTS_USE_ATOM_PLOTPANE", true) in (true, 1, "1", "true", "yes")
|
|
||||||
display(plt.o)
|
|
||||||
else
|
|
||||||
standalone_html_window(plt)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
@require WebIO = "0f1e0344-ec1d-5b48-a673-e5cf874b6c29" begin
|
@require WebIO = "0f1e0344-ec1d-5b48-a673-e5cf874b6c29" begin
|
||||||
function WebIO.render(plt::Plot{PlotlyJSBackend})
|
function WebIO.render(plt::Plot{PlotlyJSBackend})
|
||||||
|
|||||||
@ -20,7 +20,10 @@ frontends like jupyterlab and nteract.
|
|||||||
_ijulia__extra_mime_info!(plt::Plot, out::Dict) = out
|
_ijulia__extra_mime_info!(plt::Plot, out::Dict) = out
|
||||||
|
|
||||||
function _ijulia__extra_mime_info!(plt::Plot{PlotlyJSBackend}, out::Dict)
|
function _ijulia__extra_mime_info!(plt::Plot{PlotlyJSBackend}, out::Dict)
|
||||||
out["application/vnd.plotly.v1+json"] = JSON.lower(plt.o)
|
out["application/vnd.plotly.v1+json"] = Dict(
|
||||||
|
:data => plotly_series(plt),
|
||||||
|
:layout => plotly_layout(plt)
|
||||||
|
)
|
||||||
out
|
out
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user