From 92ec093ed9622d8c331267c17528a88c5e4c95db Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Fri, 20 Nov 2015 18:36:50 -0500 Subject: [PATCH] working on plotly --- src/backends/plotly.jl | 170 ++++++++++++++++++++++++++--------------- src/backends/web.jl | 40 ++++++---- src/plotter.jl | 4 +- 3 files changed, 138 insertions(+), 76 deletions(-) diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 544b7284..ea72c00b 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -67,83 +67,133 @@ end # ---------------------------------------------------------------- -function Base.writemime(io::IO, ::MIME"image/png", plt::PlottingObject{PlotlyPackage}) - # TODO: write a png to io + +# _plotDefaults[:title] = "" +# _plotDefaults[:xlabel] = "" +# _plotDefaults[:ylabel] = "" +# _plotDefaults[:yrightlabel] = "" +# _plotDefaults[:legend] = true +# _plotDefaults[:background_color] = colorant"white" +# _plotDefaults[:foreground_color] = :auto +# _plotDefaults[:xlims] = :auto +# _plotDefaults[:ylims] = :auto +# _plotDefaults[:xticks] = :auto +# _plotDefaults[:yticks] = :auto +# _plotDefaults[:xscale] = :identity +# _plotDefaults[:yscale] = :identity +# _plotDefaults[:xflip] = false +# _plotDefaults[:yflip] = false +# _plotDefaults[:size] = (600,400) +# _plotDefaults[:pos] = (0,0) +# _plotDefaults[:windowtitle] = "Plots.jl" +# _plotDefaults[:show] = false +# _plotDefaults[:layout] = nothing +# _plotDefaults[:n] = -1 +# _plotDefaults[:nr] = -1 +# _plotDefaults[:nc] = -1 +# _plotDefaults[:color_palette] = :auto +# _plotDefaults[:link] = false +# _plotDefaults[:linkx] = false +# _plotDefaults[:linky] = false +# _plotDefaults[:linkfunc] = nothing +# _plotDefaults[:tickfont] = font(8) +# _plotDefaults[:guidefont] = font(11) +# _plotDefaults[:legendfont] = font(8) +# _plotDefaults[:grid] = true + + +function get_plot_html(plt::Plot{PlotlyPackage}) + d = plt.plotargs + d_out = Dict() + + # TODO: set the fields for the plot + d_out[:title] = d[:title] + d_out[:width], d_out[:height] = d[:size] + # d_out[:margin] = 0 + d_out[:showlegend] = d[:legend] + d_out[:paper_bgcolor] = d_out[:plot_bgcolor] = plotlycolor(d[:background_color]) + d_out[:titlefont] = plotlyfont(d[:guidefont]) + + JSON.json(d_out) end +# _seriesDefaults[:axis] = :left +# _seriesDefaults[:label] = "AUTO" +# _seriesDefaults[:linetype] = :path +# _seriesDefaults[:linestyle] = :solid +# _seriesDefaults[:linewidth] = 1 +# _seriesDefaults[:linecolor] = :auto +# _seriesDefaults[:linealpha] = nothing +# _seriesDefaults[:fillrange] = nothing # ribbons, areas, etc +# _seriesDefaults[:fillcolor] = :match +# _seriesDefaults[:fillalpha] = nothing +# _seriesDefaults[:markershape] = :none +# _seriesDefaults[:markercolor] = :match +# _seriesDefaults[:markeralpha] = nothing +# _seriesDefaults[:markersize] = 6 +# _seriesDefaults[:markerstrokestyle] = :solid +# _seriesDefaults[:markerstrokewidth] = 1 +# _seriesDefaults[:markerstrokecolor] = :match +# _seriesDefaults[:markerstrokealpha] = nothing +# _seriesDefaults[:nbins] = 30 # number of bins for heatmaps and hists +# _seriesDefaults[:smooth] = false # regression line? +# _seriesDefaults[:group] = nothing # groupby vector +# _seriesDefaults[:annotation] = nothing # annotation tuple(s)... (x,y,annotation) +# _seriesDefaults[:x] = nothing +# _seriesDefaults[:y] = nothing +# _seriesDefaults[:z] = nothing # depth for contour, surface, etc +# _seriesDefaults[:zcolor] = nothing # value for color scale +# _seriesDefaults[:surface] = nothing +# _seriesDefaults[:nlevels] = 15 -# function build_plotly_json() -# end +function get_series_html(plt::Plot{PlotlyPackage}) + JSON.json([begin + d_out = Dict() -const _pyplot_head_template = mt""" - -""" - -const _pyplot_body_template = mt""" -
- -""" + # TODO: set the fields for each series + for k in (:x, :y) + d_out[k] = collect(d[k]) + end + + d_out + end for d in plt.seriesargs]) +end +# ---------------------------------------------------------------- function html_head(plt::Plot{PlotlyPackage}) - render(_pyplot_head_template, src = Pkg.dir("Plots","deps","plotly-latest.min.js")) + "" end function html_body(plt::Plot{PlotlyPackage}) w, h = plt.plotargs[:size] - # TODO: get the real ones - seriesargs = [Dict(:x => collect(1:5), :y => rand(5))] - plotargs = Dict(:margin => 0) + # # TODO: get the real ones + # seriesargs = [Dict(:x => collect(1:5), :y => rand(5))] + # plotargs = Dict(:margin => 0) - render(_pyplot_body_template, - width=w, - height=h, - seriesargs = JSON.json(seriesargs), - plotargs = JSON.json(plotargs) - ) + # series_html = JSON.json(seriesargs) + # plot_html = JSON.json(plotargs) + + """ +
+ + """ +end + + +# ---------------------------------------------------------------- + + +function Base.writemime(io::IO, ::MIME"image/png", plt::PlottingObject{PlotlyPackage}) + # TODO: write a png to io end function Base.display(::PlotsDisplay, plt::Plot{PlotlyPackage}) standalone_html_window(plt) - - # filename = string(tempname(), ".html") - # output = open(filename, "w") - # w, h = plt.plotargs[:size] - - # write(output, - # """ - # - # - # - # Plots.jl (Plotly) - # - # - # - #
- # - # - # """) - # close(output) - - # open_browser_window(filename) end function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage}) diff --git a/src/backends/web.jl b/src/backends/web.jl index 1d7ffefd..cc31a435 100644 --- a/src/backends/web.jl +++ b/src/backends/web.jl @@ -3,24 +3,36 @@ # CREDIT: parts of this implementation were inspired by @joshday's PlotlyLocal.jl -using Mustache, JSON +using JSON -const _html_template = mt""" - - - - {{:title}} - {{:head}} - - - {{:body}} - - -""" +# const _html_template = mt""" +# +# +# +# {{:title}} +# {{:head}} +# +# +# {{:body}} +# +# +# """ function standalone_html(plt::PlottingObject; title::AbstractString = "Plots.jl ($(backend_name(plt.backend)))") - render(_html_template, title = title, body = html_body(plt), head = html_head(plt)) + # render(_html_template, title = title, body = html_body(plt), head = html_head(plt)) + """ + + + + $title + $(html_head(plt)) + + + $(html_body(plt)) + + + """ end function open_browser_window(filename::AbstractString) diff --git a/src/plotter.jl b/src/plotter.jl index 6ec45ad2..261cfe03 100644 --- a/src/plotter.jl +++ b/src/plotter.jl @@ -236,8 +236,8 @@ function backend() elseif currentBackendSymbol == :plotly try - @eval include("src/backends/web.jl") - @eval include("src/backends/plotly.jl") + @eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "web.jl")) + @eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "plotly.jl")) catch err warn("Couldn't setup Plotly") rethrow(err)