diff --git a/src/backends.jl b/src/backends.jl index 94199b00..6c346b5d 100644 --- a/src/backends.jl +++ b/src/backends.jl @@ -276,6 +276,7 @@ end # @init_backend Bokeh @init_backend Plotly @init_backend PlotlyJS +@init_backend PlotlyWebIO @init_backend GR @init_backend GLVisualize @init_backend PGFPlots diff --git a/src/backends/plotlywebio.jl b/src/backends/plotlywebio.jl new file mode 100644 index 00000000..a62e8049 --- /dev/null +++ b/src/backends/plotlywebio.jl @@ -0,0 +1,130 @@ +@require Revise begin + Revise.track(Plots, joinpath(Pkg.dir("Plots"), "src", "backends", "plotlywebio.jl")) +end + +# https://github.com/spencerlyon2/PlotlyWebIO.jl + +const _plotlywebio_attr = _plotly_attr +const _plotlywebio_seriestype = _plotly_seriestype +const _plotlywebio_style = _plotly_style +const _plotlywebio_marker = _plotly_marker +const _plotlywebio_scale = _plotly_scale + +# -------------------------------------------------------------------------------------- + + +function add_backend_string(::PlotlyWebIOBackend) + """ + if !Plots.is_installed("PlotlyWebIO") + Pkg.add("PlotlyWebIO") + end + if !Plots.is_installed("Rsvg") + Pkg.add("Rsvg") + end + import Blink + Blink.AtomShell.install() + """ +end + + +function _initialize_backend(::PlotlyWebIOBackend; kw...) + @eval begin + import PlotlyWebIO + export PlotlyWebIO + end + + # # override IJulia inline display + # if isijulia() + # IJulia.display_dict(plt::AbstractPlot{PlotlyWebIOBackend}) = IJulia.display_dict(plt.o) + # end +end + +# --------------------------------------------------------------------------- + + +function _create_backend_figure(plt::Plot{PlotlyWebIOBackend}) + if !isplotnull() && plt[:overwrite_figure] && isa(current().o, PlotlyWebIO.WebIOPlot) + #PlotlyWebIO.WebIOPlot(PlotlyWebIO.Plot(), current().o.view) + current().o + else + PlotlyWebIO.WebIOPlot() + end +end + + +function _series_added(plt::Plot{PlotlyWebIOBackend}, series::Series) + syncplot = plt.o + pdicts = plotly_series(plt, series) + for pdict in pdicts + typ = pop!(pdict, :type) + gt = PlotlyWebIO.GenericTrace(typ; pdict...) + #PlotlyWebIO.addtraces!(syncplot, gt) + end +end + +function _series_updated(plt::Plot{PlotlyWebIOBackend}, series::Series) + xsym, ysym = (ispolar(series) ? (:t,:r) : (:x,:y)) + kw = KW(xsym => (series.d[:x],), ysym => (series.d[:y],)) + z = series[:z] + if z != nothing + kw[:z] = (isa(z,Surface) ? transpose_z(series, series[:z].surf, false) : z,) + end + PlotlyWebIO.restyle!( + plt.o, + findfirst(plt.series_list, series), + kw + ) +end + + +# ---------------------------------------------------------------- + +function _update_plot_object(plt::Plot{PlotlyWebIOBackend}) + pdict = plotly_layout(plt) + syncplot = plt.o + w,h = plt[:size] + PlotlyWebIO.relayout!(syncplot, pdict, width = w, height = h) +end + + +# ---------------------------------------------------------------- + +function Base.show(io::IO, ::MIME"text/html", plt::Plot{PlotlyWebIOBackend}) + prepare_output(plt) + if isijulia() && !_use_remote[] + write(io, PlotlyWebIO.html_body(PlotlyWebIO.JupyterPlot(plt.o))) + else + show(io, MIME("text/html"), plt.o) + end +end + +function plotlywebio_save_hack(io::IO, plt::Plot{PlotlyWebIOBackend}, ext::String) + # tmpfn = tempname() * "." * ext + # PlotlyWebIO.savefig(plt.o, tmpfn) + # write(io, read(open(tmpfn))) +end +_show(io::IO, ::MIME"image/svg+xml", plt::Plot{PlotlyWebIOBackend}) = plotlywebio_save_hack(io, plt, "svg") +_show(io::IO, ::MIME"image/png", plt::Plot{PlotlyWebIOBackend}) = plotlywebio_save_hack(io, plt, "png") +_show(io::IO, ::MIME"application/pdf", plt::Plot{PlotlyWebIOBackend}) = plotlywebio_save_hack(io, plt, "pdf") +_show(io::IO, ::MIME"image/eps", plt::Plot{PlotlyWebIOBackend}) = plotlywebio_save_hack(io, plt, "eps") + +function write_temp_html(plt::Plot{PlotlyWebIOBackend}) + filename = string(tempname(), ".html") + savefig(plt, filename) + filename +end + +function _display(plt::Plot{PlotlyWebIOBackend}) + if get(ENV, "PLOTS_USE_ATOM_PLOTPANE", true) in (true, 1, "1", "true", "yes") + display(plt.o) + else + standalone_html_window(plt) + end +end + + +function closeall(::PlotlyWebIOBackend) + if !isplotnull() && isa(current().o, PlotlyWebIO.WebIOPlot) + close(current().o) + end +end