From eabe670ae815a054a897e9cae3a43bf9c9ce3404 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Fri, 18 Dec 2015 17:33:57 -0500 Subject: [PATCH] working on plotlyjs integration --- src/backends/plotly.jl | 18 ++++++++------ src/backends/plotly_blink.jl | 48 ++++++++++++++++++++++++++++++++++++ src/plotter.jl | 6 +++++ 3 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 src/backends/plotly_blink.jl diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index bb7af4d7..9f9482c3 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -176,8 +176,9 @@ function plotlyaxis(d::Dict, isx::Bool) ax end -function get_plot_json(plt::Plot{PlotlyPackage}) - d = plt.plotargs +# function get_plot_json(plt::Plot{PlotlyPackage}) +# d = plt.plotargs +function plotly_layout(d::Dict) d_out = Dict() bgcolor = webcolor(d[:background_color]) @@ -210,8 +211,11 @@ function get_plot_json(plt::Plot{PlotlyPackage}) d_out[:annotations] = [get_annotation_dict(ann...) for ann in anns] end - # finally build and return the json - JSON.json(d_out) + d_out +end + +function get_plot_json(plt::Plot{PlotlyPackage}) + JSON.json(plotly_layout(plt.plotargs)) end @@ -231,7 +235,7 @@ const _plotly_markers = Dict( ) # get a dictionary representing the series params (d is the Plots-dict, d_out is the Plotly-dict) -function get_series_json(d::Dict; plot_index = nothing) +function plotly_series(d::Dict; plot_index = nothing) d_out = Dict() x, y = collect(d[:x]), collect(d[:y]) @@ -360,14 +364,14 @@ end # get a list of dictionaries, each representing the series params function get_series_json(plt::Plot{PlotlyPackage}) - JSON.json(map(get_series_json, plt.seriesargs)) + JSON.json(map(plotly_series, plt.seriesargs)) end function get_series_json(subplt::Subplot{PlotlyPackage}) ds = Dict[] for (i,plt) in enumerate(subplt.plts) for d in plt.seriesargs - push!(ds, get_series_json(d, plot_index = i)) + push!(ds, plotly_series(d, plot_index = i)) end end JSON.json(ds) diff --git a/src/backends/plotly_blink.jl b/src/backends/plotly_blink.jl new file mode 100644 index 00000000..c5f5a6f5 --- /dev/null +++ b/src/backends/plotly_blink.jl @@ -0,0 +1,48 @@ + +# override some methods to use Plotlyjs/Blink + +import Plotlyjs + +function _create_plot(pkg::PlotlyPackage; kw...) + d = Dict(kw) + # TODO: create the window/canvas/context that is the plot within the backend (call it `o`) + # TODO: initialize the plot... title, xlabel, bgcolor, etc + o = Plotlyjs.Plot() + + Plot(o, pkg, 0, d, Dict[]) +end + + +function _add_series(::PlotlyPackage, plt::Plot; kw...) + d = Dict(kw) + + # add to the data array + pdict = plotly_series(d) + gt = Plotlyjs.GenericTrace(pdict[:type], pdict) + push!(plt.o.data, gt) + if !isnull(plt.o.window) + Plotlyjs.addtraces!(plt.o, gt) + end + + push!(plt.seriesargs, d) + plt +end + +# TODO: override this to update plot items (title, xlabel, etc) after creation +function _update_plot(plt::Plot{PlotlyPackage}, d::Dict) + pdict = plotly_layout(d) + plt.o.layout = Plotlyjs.Layout(pdict) + if !isnull(plt.o.window) + Plotlyjs.relayout!(plt.o, pdict...) + end +end + + +function Base.display(::PlotsDisplay, plt::Plot{PlotlyPackage}) + dump(plt.o) + show(plt.o) +end + +function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage}) + error() +end diff --git a/src/plotter.jl b/src/plotter.jl index 3ca93d15..1963acfb 100644 --- a/src/plotter.jl +++ b/src/plotter.jl @@ -235,6 +235,12 @@ function backend() # end borrowing (thanks :) ########################### + try + include(joinpath(Pkg.dir("Plots"), "src", "backends", "plotly_blink.jl")) + catch err + warn("Error including Plotlyjs: $err\n Note: Will fall back to built-in display.") + end + end catch err warn("Couldn't setup Plotly")