PlotlyJS blink backend working

This commit is contained in:
Spencer Lyon 2016-01-21 09:57:22 -05:00
parent 863ad22b48
commit a37a23bcfc
3 changed files with 34 additions and 33 deletions

View File

@ -4,7 +4,7 @@
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
function _create_plot(pkg::PlotlyPackage; kw...) function _create_plot(pkg::PlotlyPackage; kw...)
d = Dict(kw) d = Dict{Symbol,Any}(kw)
# TODO: create the window/canvas/context that is the plot within the backend (call it `o`) # TODO: create the window/canvas/context that is the plot within the backend (call it `o`)
# TODO: initialize the plot... title, xlabel, bgcolor, etc # TODO: initialize the plot... title, xlabel, bgcolor, etc
Plot(nothing, pkg, 0, d, Dict[]) Plot(nothing, pkg, 0, d, Dict[])
@ -12,7 +12,7 @@ end
function _add_series(::PlotlyPackage, plt::Plot; kw...) function _add_series(::PlotlyPackage, plt::Plot; kw...)
d = Dict(kw) d = Dict{Symbol,Any}(kw)
# TODO: add one series to the underlying package # TODO: add one series to the underlying package
push!(plt.seriesargs, d) push!(plt.seriesargs, d)
plt plt
@ -83,7 +83,7 @@ end
# _plotDefaults[:yflip] = false # _plotDefaults[:yflip] = false
function plotlyfont(font::Font, color = font.color) function plotlyfont(font::Font, color = font.color)
Dict( Dict{Symbol,Any}(
:family => font.family, :family => font.family,
:size => round(Int, font.pointsize*1.4), :size => round(Int, font.pointsize*1.4),
:color => webcolor(color), :color => webcolor(color),
@ -91,7 +91,7 @@ function plotlyfont(font::Font, color = font.color)
end end
function get_annotation_dict(x, y, val::Union{AbstractString,Symbol}) function get_annotation_dict(x, y, val::Union{AbstractString,Symbol})
Dict( Dict{Symbol,Any}(
:text => val, :text => val,
:xref => "x", :xref => "x",
:x => x, :x => x,
@ -102,7 +102,7 @@ function get_annotation_dict(x, y, val::Union{AbstractString,Symbol})
end end
function get_annotation_dict(x, y, ptxt::PlotText) function get_annotation_dict(x, y, ptxt::PlotText)
merge(get_annotation_dict(x, y, ptxt.str), Dict( merge(get_annotation_dict(x, y, ptxt.str), Dict{Symbol,Any}(
:font => plotlyfont(ptxt.font), :font => plotlyfont(ptxt.font),
:xanchor => ptxt.font.halign == :hcenter ? :center : ptxt.font.halign, :xanchor => ptxt.font.halign == :hcenter ? :center : ptxt.font.halign,
:yanchor => ptxt.font.valign == :vcenter ? :middle : ptxt.font.valign, :yanchor => ptxt.font.valign == :vcenter ? :middle : ptxt.font.valign,
@ -127,7 +127,7 @@ scalesym(isx::Bool) = symbol((isx ? "x" : "y") * "scale")
labelsym(isx::Bool) = symbol((isx ? "x" : "y") * "label") labelsym(isx::Bool) = symbol((isx ? "x" : "y") * "label")
function plotlyaxis(d::Dict, isx::Bool) function plotlyaxis(d::Dict, isx::Bool)
ax = Dict( ax = Dict{Symbol,Any}(
:title => d[labelsym(isx)], :title => d[labelsym(isx)],
:showgrid => d[:grid], :showgrid => d[:grid],
:zeroline => false, :zeroline => false,
@ -179,7 +179,7 @@ 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) function plotly_layout(d::Dict)
d_out = Dict() d_out = Dict{Symbol,Any}()
bgcolor = webcolor(d[:background_color]) bgcolor = webcolor(d[:background_color])
fgcolor = webcolor(d[:foreground_color]) fgcolor = webcolor(d[:foreground_color])
@ -187,10 +187,10 @@ function plotly_layout(d::Dict)
# set the fields for the plot # set the fields for the plot
d_out[:title] = d[:title] d_out[:title] = d[:title]
d_out[:titlefont] = plotlyfont(d[:guidefont], fgcolor) d_out[:titlefont] = plotlyfont(d[:guidefont], fgcolor)
d_out[:margin] = Dict(:l=>35, :b=>30, :r=>8, :t=>20) d_out[:margin] = Dict{Symbol,Any}(:l=>35, :b=>30, :r=>8, :t=>20)
d_out[:plot_bgcolor] = bgcolor d_out[:plot_bgcolor] = bgcolor
d_out[:paper_bgcolor] = bgcolor d_out[:paper_bgcolor] = bgcolor
# TODO: x/y axis tick values/labels # TODO: x/y axis tick values/labels
d_out[:xaxis] = plotlyaxis(d, true) d_out[:xaxis] = plotlyaxis(d, true)
d_out[:yaxis] = plotlyaxis(d, false) d_out[:yaxis] = plotlyaxis(d, false)
@ -198,7 +198,7 @@ function plotly_layout(d::Dict)
# legend # legend
d_out[:showlegend] = d[:legend] d_out[:showlegend] = d[:legend]
if d[:legend] if d[:legend]
d_out[:legend] = Dict( d_out[:legend] = Dict{Symbol,Any}(
:bgcolor => bgcolor, :bgcolor => bgcolor,
:bordercolor => fgcolor, :bordercolor => fgcolor,
:font => plotlyfont(d[:legendfont]), :font => plotlyfont(d[:legendfont]),
@ -224,7 +224,7 @@ function plotly_colorscale(grad::ColorGradient)
end end
plotly_colorscale(c) = plotly_colorscale(ColorGradient(:bluesreds)) plotly_colorscale(c) = plotly_colorscale(ColorGradient(:bluesreds))
const _plotly_markers = Dict( const _plotly_markers = Dict{Symbol,Any}(
:rect => "square", :rect => "square",
:xcross => "x", :xcross => "x",
:utriangle => "triangle-up", :utriangle => "triangle-up",
@ -236,7 +236,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 plotly_series(d::Dict; plot_index = nothing) function plotly_series(d::Dict; plot_index = nothing)
d_out = Dict() d_out = Dict{Symbol,Any}()
x, y = collect(d[:x]), collect(d[:y]) x, y = collect(d[:x]), collect(d[:y])
d_out[:name] = d[:label] d_out[:name] = d[:label]
@ -293,7 +293,7 @@ function plotly_series(d::Dict; plot_index = nothing)
# d_out[:showscale] = d[:legend] # d_out[:showscale] = d[:legend]
if lt == :contour if lt == :contour
d_out[:ncontours] = d[:levels] d_out[:ncontours] = d[:levels]
d_out[:contours] = Dict(:coloring => d[:fillrange] != nothing ? "fill" : "lines") d_out[:contours] = Dict{Symbol,Any}(:coloring => d[:fillrange] != nothing ? "fill" : "lines")
end end
d_out[:colorscale] = plotly_colorscale(d[lt == :contour ? :linecolor : :fillcolor]) d_out[:colorscale] = plotly_colorscale(d[lt == :contour ? :linecolor : :fillcolor])
@ -315,17 +315,17 @@ function plotly_series(d::Dict; plot_index = nothing)
else else
warn("Plotly: linetype $lt isn't supported.") warn("Plotly: linetype $lt isn't supported.")
return Dict() return Dict{Symbol,Any}()
end end
# add "marker" # add "marker"
if hasmarker if hasmarker
d_out[:marker] = Dict( d_out[:marker] = Dict{Symbol,Any}(
:symbol => get(_plotly_markers, d[:markershape], string(d[:markershape])), :symbol => get(_plotly_markers, d[:markershape], string(d[:markershape])),
:opacity => d[:markeralpha], :opacity => d[:markeralpha],
:size => 2 * d[:markersize], :size => 2 * d[:markersize],
:color => webcolor(d[:markercolor], d[:markeralpha]), :color => webcolor(d[:markercolor], d[:markeralpha]),
:line => Dict( :line => Dict{Symbol,Any}(
:color => webcolor(d[:markerstrokecolor], d[:markerstrokealpha]), :color => webcolor(d[:markerstrokecolor], d[:markerstrokealpha]),
:width => d[:markerstrokewidth], :width => d[:markerstrokewidth],
), ),
@ -338,7 +338,7 @@ function plotly_series(d::Dict; plot_index = nothing)
# add "line" # add "line"
if hasline if hasline
d_out[:line] = Dict( d_out[:line] = Dict{Symbol,Any}(
:color => webcolor(d[:linecolor], d[:linealpha]), :color => webcolor(d[:linecolor], d[:linealpha]),
:width => d[:linewidth], :width => d[:linewidth],
:shape => if lt == :steppre :shape => if lt == :steppre
@ -405,10 +405,10 @@ function html_body(subplt::Subplot{PlotlyPackage})
html = ["<div style=\"width:$(w)px;height:$(h)px;\">"] html = ["<div style=\"width:$(w)px;height:$(h)px;\">"]
nr = nrows(subplt.layout) nr = nrows(subplt.layout)
ph = h / nr ph = h / nr
for r in 1:nr for r in 1:nr
push!(html, "<div style=\"clear:both;\">") push!(html, "<div style=\"clear:both;\">")
nc = ncols(subplt.layout, r) nc = ncols(subplt.layout, r)
pw = w / nc pw = w / nc
@ -416,7 +416,7 @@ function html_body(subplt::Subplot{PlotlyPackage})
plt = subplt[r,c] plt = subplt[r,c]
push!(html, html_body(plt, "float:left; width:$(pw)px; height:$(ph)px;")) push!(html, html_body(plt, "float:left; width:$(pw)px; height:$(ph)px;"))
end end
push!(html, "</div>") push!(html, "</div>")
end end
push!(html, "</div>") push!(html, "</div>")

View File

@ -1,13 +1,13 @@
# override some methods to use Plotlyjs/Blink # override some methods to use PlotlyJS/Blink
import Plotlyjs import PlotlyJS
function _create_plot(pkg::PlotlyPackage; kw...) function _create_plot(pkg::PlotlyPackage; kw...)
d = Dict(kw) d = Dict(kw)
# TODO: create the window/canvas/context that is the plot within the backend (call it `o`) # TODO: create the window/canvas/context that is the plot within the backend (call it `o`)
# TODO: initialize the plot... title, xlabel, bgcolor, etc # TODO: initialize the plot... title, xlabel, bgcolor, etc
o = Plotlyjs.Plot() o = PlotlyJS.Plot()
Plot(o, pkg, 0, d, Dict[]) Plot(o, pkg, 0, d, Dict[])
end end
@ -18,10 +18,11 @@ function _add_series(::PlotlyPackage, plt::Plot; kw...)
# add to the data array # add to the data array
pdict = plotly_series(d) pdict = plotly_series(d)
gt = Plotlyjs.GenericTrace(pdict[:type], pdict) typ = pop!(pdict, :type)
gt = PlotlyJS.GenericTrace(typ; pdict...)
push!(plt.o.data, gt) push!(plt.o.data, gt)
if !isnull(plt.o.window) if !isnull(plt.o.window)
Plotlyjs.addtraces!(plt.o, gt) PlotlyJS.addtraces!(plt.o, gt)
end end
push!(plt.seriesargs, d) push!(plt.seriesargs, d)
@ -31,16 +32,16 @@ end
# TODO: override this to update plot items (title, xlabel, etc) after creation # TODO: override this to update plot items (title, xlabel, etc) after creation
function _update_plot(plt::Plot{PlotlyPackage}, d::Dict) function _update_plot(plt::Plot{PlotlyPackage}, d::Dict)
pdict = plotly_layout(d) pdict = plotly_layout(d)
plt.o.layout = Plotlyjs.Layout(pdict) plt.o.layout = PlotlyJS.Layout(pdict)
if !isnull(plt.o.window) if !isnull(plt.o.window)
Plotlyjs.relayout!(plt.o, pdict...) PlotlyJS.relayout!(plt.o; pdict...)
end end
end end
function Base.display(::PlotsDisplay, plt::Plot{PlotlyPackage}) function Base.display(::PlotsDisplay, plt::Plot{PlotlyPackage})
dump(plt.o) dump(plt.o)
show(plt.o) display(plt.o)
end end
function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage}) function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage})

View File

@ -241,11 +241,11 @@ function backend()
# end borrowing (thanks :) # end borrowing (thanks :)
########################### ###########################
# try try
# include(joinpath(Pkg.dir("Plots"), "src", "backends", "plotly_blink.jl")) include(joinpath(Pkg.dir("Plots"), "src", "backends", "plotly_blink.jl"))
# catch err catch err
# warn("Error including Plotlyjs: $err\n Note: Will fall back to built-in display.") warn("Error including PlotlyJS: $err\n Note: Will fall back to built-in display.")
# end end
end end
catch err catch err