working on plotly

This commit is contained in:
Thomas Breloff 2015-11-20 18:36:50 -05:00
parent 6d2a1aafd2
commit 92ec093ed9
3 changed files with 138 additions and 76 deletions

View File

@ -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"""
<script src="{{:src}}"></script>
"""
const _pyplot_body_template = mt"""
<div id="myplot" style="width:{{:width}}px;height:{{:height}}px;"></div>
<script charset="utf-8">
PLOT = document.getElementById('myplot');
Plotly.plot(PLOT, {{:seriesargs}}, {{:plotargs}});
</script>
"""
# 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"))
"<script src=\"$(Pkg.dir("Plots","deps","plotly-latest.min.js"))\"></script>"
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)
"""
<div id=\"myplot\" style=\"width:$(w)px;height:$(h)px;\"></div>
<script>
PLOT = document.getElementById('myplot');
Plotly.plot(PLOT, $(get_series_html(plt)), $(get_plot_html(plt)));
</script>
"""
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,
# """
# <!DOCTYPE html>
# <html>
# <head>
# <title>Plots.jl (Plotly)</title>
# <meta charset="utf-8">
# <script src="$(Pkg.dir("Plots"))/src/backends/plotly-latest.min.js"></script>
# </head>
# <div id="myplot" style="width:$(w)px;height:$(h)px;"></div>
# <script charset="utf-8">
# PLOT = document.getElementById('myplot');
# Plotly.plot(PLOT, [{
# x: [1, 2, 3, 4, 5],
# y: [1, 2, 4, 8, 16]
# }],
# {margin: { t: 0 }});
# """)
# # build_plotly_json(plt)
# # print(output, json)
# write(output,
# """
# </script>
# </html>
# """)
# close(output)
# open_browser_window(filename)
end
function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage})

View File

@ -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"""
<!DOCTYPE html>
<html>
<head>
<title>{{:title}}</title>
{{:head}}
</head>
<body>
{{:body}}
</body>
</html>
"""
# const _html_template = mt"""
# <!DOCTYPE html>
# <html>
# <head>
# <title>{{:title}}</title>
# {{:head}}
# </head>
# <body>
# {{:body}}
# </body>
# </html>
# """
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))
"""
<!DOCTYPE html>
<html>
<head>
<title>$title</title>
$(html_head(plt))
</head>
<body>
$(html_body(plt))
</body>
</html>
"""
end
function open_browser_window(filename::AbstractString)

View File

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