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

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 :)
###########################
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")