working on plotlyjs integration

This commit is contained in:
Thomas Breloff 2015-12-18 17:33:57 -05:00
parent 9fffef6f53
commit eabe670ae8
3 changed files with 65 additions and 7 deletions

View File

@ -176,8 +176,9 @@ function plotlyaxis(d::Dict, isx::Bool)
ax ax
end end
function get_plot_json(plt::Plot{PlotlyPackage}) # function get_plot_json(plt::Plot{PlotlyPackage})
d = plt.plotargs # d = plt.plotargs
function plotly_layout(d::Dict)
d_out = Dict() d_out = Dict()
bgcolor = webcolor(d[:background_color]) 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] d_out[:annotations] = [get_annotation_dict(ann...) for ann in anns]
end end
# finally build and return the json d_out
JSON.json(d_out) end
function get_plot_json(plt::Plot{PlotlyPackage})
JSON.json(plotly_layout(plt.plotargs))
end 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) # 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() d_out = Dict()
x, y = collect(d[:x]), collect(d[:y]) x, y = collect(d[:x]), collect(d[:y])
@ -360,14 +364,14 @@ end
# get a list of dictionaries, each representing the series params # get a list of dictionaries, each representing the series params
function get_series_json(plt::Plot{PlotlyPackage}) function get_series_json(plt::Plot{PlotlyPackage})
JSON.json(map(get_series_json, plt.seriesargs)) JSON.json(map(plotly_series, plt.seriesargs))
end end
function get_series_json(subplt::Subplot{PlotlyPackage}) function get_series_json(subplt::Subplot{PlotlyPackage})
ds = Dict[] ds = Dict[]
for (i,plt) in enumerate(subplt.plts) for (i,plt) in enumerate(subplt.plts)
for d in plt.seriesargs for d in plt.seriesargs
push!(ds, get_series_json(d, plot_index = i)) push!(ds, plotly_series(d, plot_index = i))
end end
end end
JSON.json(ds) JSON.json(ds)

View File

@ -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

View File

@ -235,6 +235,12 @@ function backend()
# end borrowing (thanks :) # 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 end
catch err catch err
warn("Couldn't setup Plotly") warn("Couldn't setup Plotly")