Merge 85068d82d9ff9502fb79e8d9deee883a43b6ab62 into 8b2a83838894dc6d7f903a75e209362137e4d237

This commit is contained in:
Patrick Kofod Mogensen 2016-03-15 15:26:36 +00:00
commit af7f02788a
6 changed files with 70 additions and 33 deletions

View File

@ -85,11 +85,24 @@ end
################# This is the important method to implement!!! #################
function _make_pgf_plot(plt::Plot{PGFPlotsBackend})
line_type = plt.seriesargs[1][:linetype]
if line_type == :path
plt.o = PGFPlots.Linear(plt.seriesargs[1][:x], plt.seriesargs[1][:y])
elseif line_type == :scatter
plt.o = PGFPlots.Scatter(plt.seriesargs[1][:x], plt.seriesargs[1][:y])
elseif line_type == :steppost
plt.o = PGFPlots.Linear(plt.seriesargs[1][:x], plt.seriesargs[1][:y], style="const plot")
elseif line_type == :hist
plt_hist = hist(plt.seriesargs[1][:y])
plt.o = PGFPlots.Linear(plt_hist[1][1:end-1], plt_hist[2], style="ybar,fill=green", mark="none")
elseif line_type == :bar
plt.o = PGFPlots.Linear(plt.seriesargs[1][:x], plt.seriesargs[1][:y], style="ybar,fill=green", mark="none")
end
# TODO: convert plt.plotargs and plt.seriesargs into PGFPlots calls
# TODO: return the PGFPlots object
end
function Base.writemime(io::IO, mime::MIME"image/png", plt::AbstractPlot{PGFPlotsBackend})
function Base.writemime(io::IO, mime::MIME"image/svg+xml", plt::AbstractPlot{PGFPlotsBackend})
plt.o = _make_pgf_plot(plt)
writemime(io, mime, plt.o)
end

View File

@ -8,25 +8,34 @@ function _initialize_backend(::PlotlyBackend; kw...)
############################
# borrowed from https://github.com/spencerlyon2/Plotlyjs.jl/blob/master/src/display.jl
_js_path = joinpath(Pkg.dir("Plots"), "deps", "plotly-latest.min.js")
_js_path = Pkg.dir("Plots", "deps", "plotly-latest.min.js")
_js_code = open(readall, _js_path, "r")
_js_script = """
<script type="text/javascript">
require=requirejs=define=undefined;
</script>
<script type="text/javascript">
$(_js_code)
</script>
"""
# 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", """
<script type="text/javascript">
require=requirejs=define=undefined;
</script>
<script type="text/javascript">
$(open(readall, _js_path, "r"))
</script>
""")
display("text/html", _js_script)
# display("text/html", "<p>Plotly javascript loaded.</p>")
end
# end borrowing (thanks :)
###########################
# if isatom()
# import Atom
# Atom.@msg evaljs(_js_code)
# end
end
# TODO: other initialization
end
@ -436,15 +445,23 @@ function html_body(plt::Plot{PlotlyBackend}, style = nothing)
style = "width:$(w)px;height:$(h)px;"
end
uuid = Base.Random.uuid4()
"""
html = """
<div id=\"$(uuid)\" style=\"$(style)\"></div>
<script>
PLOT = document.getElementById('$(uuid)');
Plotly.plot(PLOT, $(get_series_json(plt)), $(get_plot_json(plt)));
</script>
"""
# @show html
html
end
function js_body(plt::Plot{PlotlyBackend}, uuid)
js = """
PLOT = document.getElementById('$(uuid)');
Plotly.plot(PLOT, $(get_series_json(plt)), $(get_plot_json(plt)));
"""
end
function html_body(subplt::Subplot{PlotlyBackend})
@ -480,6 +497,7 @@ end
function Base.writemime(io::IO, ::MIME"text/html", plt::AbstractPlot{PlotlyBackend})
write(io, html_head(plt) * html_body(plt))
# write(io, html_body(plt))
end
function Base.display(::PlotsDisplay, plt::AbstractPlot{PlotlyBackend})

View File

@ -44,6 +44,7 @@ function _add_series(::PlotlyJSBackend, plt::Plot; kw...)
typ = pop!(pdict, :type)
gt = PlotlyJS.GenericTrace(typ; pdict...)
PlotlyJS.addtraces!(syncplot, gt)
# PlotlyJS.addtraces!(syncplot.plot, gt)
push!(plt.seriesargs, d)
plt
@ -73,6 +74,7 @@ function _update_plot(plt::Plot{PlotlyJSBackend}, d::Dict)
syncplot = plt.o
w,h = d[:size]
PlotlyJS.relayout!(syncplot, pdict, width = w, height = h)
# PlotlyJS.relayout!(syncplot.plot, pdict, width = w, height = h)
end
@ -125,4 +127,3 @@ end
function Base.display(::PlotsDisplay, plt::Subplot{PlotlyJSBackend})
error()
end

View File

@ -723,7 +723,7 @@ supportedArgs(::PGFPlotsBackend) = [
# :legend,
# :linecolor,
# :linestyle,
# :linetype,
:linetype,
# :linewidth,
# :linealpha,
# :markershape,
@ -743,11 +743,11 @@ supportedArgs(::PGFPlotsBackend) = [
# :size,
# :title,
# :windowtitle,
# :x,
:x,
# :xlabel,
# :xlims,
# :xticks,
# :y,
:y,
# :ylabel,
# :ylims,
# :yrightlabel,
@ -765,9 +765,8 @@ supportedArgs(::PGFPlotsBackend) = [
# :levels,
]
supportedAxes(::PGFPlotsBackend) = [:auto, :left]
supportedTypes(::PGFPlotsBackend) = [:contour] #, :path, :scatter ,:steppre, :steppost, :sticks, :hist2d, :hexbin, :hist, :bar, :hline, :vline, :contour]
supportedTypes(::PGFPlotsBackend) = [:path, :scatter, :line, :steppost, :hist, :bar] #, :steppre, :sticks, :hist2d, :hexbin, :bar, :hline, :vline, :contour]
supportedStyles(::PGFPlotsBackend) = [:auto, :solid] #, :dash, :dot, :dashdot, :dashdotdot]
supportedMarkers(::PGFPlotsBackend) = [:none, :auto, :ellipse] #, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5] #vcat(_allMarkers, Shape)
supportedScales(::PGFPlotsBackend) = [:identity] #, :log, :log2, :log10, :asinh, :sqrt]
subplotSupported(::PGFPlotsBackend) = false

View File

@ -124,18 +124,23 @@ end
# ---------------------------------------------------------
function setup_atom()
# @require Atom begin
# @eval begin
# import Atom
#
@require Atom begin
import Atom, Media
# connects the render function
Media.media{T <: Union{GadflyBackend,ImmerseBackend,PyPlotBackend,GRBackend}}(Plot{T}, Media.Plot)
# Atom.displaysize(::AbstractPlot) = (535, 379)
# Atom.displaytitle(::AbstractPlot) = "Plots.jl"
#
# Atom.@render Atom.PlotPane p::Plot begin
# x, y = Atom.@rpc Atom.plotsize()
# plot!(p, size=(x,y)) # changes the size of the Plots.Plot
# Atom.div(Dict(:style=>"background: white"), Atom.HTML(stringmime("text/html", p)))
# end
# end
# Atom.displaytitle(plt::AbstractPlot) = "Plots.jl (backend: $(backend(plt)))"
# this is like "display"... sends an html div with the plot to the PlotPane
function Media.render(pane::Atom.PlotPane, plt::Plot)
Media.render(pane, Atom.div(Atom.d(), Atom.HTML(stringmime(MIME("text/html"), plt))))
end
# function Media.render(pane::Atom.PlotPane, plt::Plot{PlotlyBackend})
# html = Media.render(pane, Atom.div(Atom.d(), Atom.HTML(stringmime(MIME("text/html"), plt))))
# end
end
end

View File

@ -198,6 +198,7 @@ function fakedata(sz...)
end
isijulia() = isdefined(Main, :IJulia) && Main.IJulia.inited
isatom() = isdefined(Main, :Atom) && Atom.isconnected()
istuple(::Tuple) = true
istuple(::Any) = false