diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 25b88793..797c0245 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -53,8 +53,9 @@ end # ---------------------------------------------------------------- -function _create_subplot(subplt::Subplot{PlotlyPackage}) +function _create_subplot(subplt::Subplot{PlotlyPackage}, isbefore::Bool) # TODO: build the underlying Subplot object. this is where you might layout the panes within a GUI window, for example + true end function _expand_limits(lims, plt::Plot{PlotlyPackage}, isx::Bool) @@ -94,7 +95,7 @@ function plotlyscale(scale::Symbol) end end -function get_plot_html(plt::Plot{PlotlyPackage}) +function get_plot_json(plt::Plot{PlotlyPackage}) d = plt.plotargs d_out = Dict() @@ -104,7 +105,7 @@ function get_plot_html(plt::Plot{PlotlyPackage}) # TODO: set the fields for the plot d_out[:title] = d[:title] d_out[:titlefont] = plotlyfont(d[:guidefont]) - d_out[:width], d_out[:height] = d[:size] + # d_out[:width], d_out[:height] = d[:size] d_out[:margin] = Dict(:l=>30, :b=>30, :r=>15, :t=>15) # d_out[:margin] = Dict(:t=>20) d_out[:paper_bgcolor] = bgcolor @@ -194,7 +195,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_html(d::Dict) +function get_series_json(d::Dict; plot_index = nothing) d_out = Dict() x, y = collect(d[:x]), collect(d[:y]) @@ -313,49 +314,95 @@ function get_series_html(d::Dict) ) end + # # for subplots, we need to add the xaxis/yaxis fields + # if plot_index != nothing + # d_out[:xaxis] = "x$(plot_index)" + # d_out[:yaxis] = "y$(plot_index)" + # end + d_out end # get a list of dictionaries, each representing the series params -function get_series_html(plt::Plot{PlotlyPackage}) - JSON.json(map(get_series_html, plt.seriesargs)) +function get_series_json(plt::Plot{PlotlyPackage}) + JSON.json(map(get_series_json, 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)) + end + end + JSON.json(ds) end # ---------------------------------------------------------------- -function html_head(plt::Plot{PlotlyPackage}) +function html_head(plt::PlottingObject{PlotlyPackage}) "" end -function html_body(plt::Plot{PlotlyPackage}) - w, h = plt.plotargs[:size] +function html_body(plt::Plot{PlotlyPackage}, style = nothing) + if style == nothing + w, h = plt.plotargs[:size] + style = "width:$(w)px;height:$(h)px;" + end + uuid = Base.Random.uuid4() """ -
+
""" end + +function html_body(subplt::Subplot{PlotlyPackage}) + w, h = subplt.plts[1].plotargs[:size] + html = ["
"] + nr = nrows(subplt.layout) + ph = h / nr + + for r in 1:nr + push!(html, "
") + + nc = ncols(subplt.layout, r) + pw = w / nc + + for c in 1:nc + plt = subplt[r,c] + push!(html, html_body(plt, "float:left; width:$(pw)px; height:$(ph)px;")) + end + + push!(html, "
") + end + push!(html, "
") + + join(html) +end + + # ---------------------------------------------------------------- function Base.writemime(io::IO, ::MIME"image/png", plt::PlottingObject{PlotlyPackage}) + isijulia() && return # TODO: write a png to io - println("png") + println("todo: png") end function Base.writemime(io::IO, ::MIME"text/html", plt::PlottingObject{PlotlyPackage}) - println("html") - html_head(plt) * html_body(plt) + write(io, html_head(plt) * html_body(plt)) end -function Base.display(::PlotsDisplay, plt::Plot{PlotlyPackage}) +function Base.display(::PlotsDisplay, plt::PlottingObject{PlotlyPackage}) standalone_html_window(plt) end -function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage}) - # TODO: display/show the subplot -end +# function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage}) +# # TODO: display/show the subplot +# end diff --git a/src/backends/supported.jl b/src/backends/supported.jl index c166be53..ac435ec3 100644 --- a/src/backends/supported.jl +++ b/src/backends/supported.jl @@ -489,6 +489,6 @@ supportedStyles(::PlotlyPackage) = [:auto, :solid, :dash, :dot, :dashdot] supportedMarkers(::PlotlyPackage) = [:none, :auto, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :pentagon, :hexagon, :octagon] #vcat(_allMarkers, Shape) supportedScales(::PlotlyPackage) = [:identity, :log] #, :log, :log2, :log10, :asinh, :sqrt] -subplotSupported(::PlotlyPackage) = false +subplotSupported(::PlotlyPackage) = true stringsSupported(::PlotlyPackage) = true diff --git a/src/backends/template.jl b/src/backends/template.jl index 0c96cde1..450f264f 100644 --- a/src/backends/template.jl +++ b/src/backends/template.jl @@ -55,7 +55,7 @@ end # ---------------------------------------------------------------- -function _create_subplot(subplt::Subplot{[PkgName]Package}) +function _create_subplot(subplt::Subplot{[PkgName]Package}, isbefore::Bool) # TODO: build the underlying Subplot object. this is where you might layout the panes within a GUI window, for example end diff --git a/src/plotter.jl b/src/plotter.jl index a5c22aec..86444be6 100644 --- a/src/plotter.jl +++ b/src/plotter.jl @@ -238,13 +238,31 @@ function backend() elseif currentBackendSymbol == :plotly try - # @eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "web.jl")) - # @eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "plotly.jl")) @eval begin import JSON JSON._print(io::IO, state::JSON.State, dt::Union{Date,DateTime}) = print(io, '"', dt, '"') - # JSON.json(dt::Union{Date,DateTime}) = string(dt) - # JSON.json{D<:Union{Date,DateTime}}(dts::AVec{D}) = map(string, dts) + + ############################ + # borrowed from https://github.com/spencerlyon2/Plotlyjs.jl/blob/master/src/display.jl + _js_path = joinpath(Pkg.dir("Plots"), "deps", "plotly-latest.min.js") + + # if we're in IJulia call setupnotebook to load js and css + if isijulia() + # the first script is some hack I needed to do in order for the notebook + # to not complain about Plotly being undefined + display("text/html", """ + + + """) + # display("text/html", "

Plotly javascript loaded.

") + end + # end borrowing (thanks :) + ########################### + end catch err warn("Couldn't setup Plotly") diff --git a/src/utils.jl b/src/utils.jl index f253318b..167910b4 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -188,6 +188,7 @@ function fakedata(sz...) y end +isijulia() = isdefined(Main, :IJulia) && Main.IJulia.inited # ticksType{T<:Real,S<:Real}(ticks::@compat(Tuple{T,S})) = :limits