diff --git a/src/backends/pgfplots.jl b/src/backends/pgfplots.jl index 1b71568a..92dc628c 100644 --- a/src/backends/pgfplots.jl +++ b/src/backends/pgfplots.jl @@ -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 diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index e7c24383..583c2f8e 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -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 = """ + + + """ # 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", _js_script) # display("text/html", "

Plotly javascript loaded.

") 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 = """
""" + # @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}) @@ -479,7 +496,8 @@ function Base.writemime(io::IO, ::MIME"image/png", plt::AbstractPlot{PlotlyBacke end function Base.writemime(io::IO, ::MIME"text/html", plt::AbstractPlot{PlotlyBackend}) - write(io, html_head(plt) * html_body(plt)) + write(io, html_head(plt) * html_body(plt)) + # write(io, html_body(plt)) end function Base.display(::PlotsDisplay, plt::AbstractPlot{PlotlyBackend}) diff --git a/src/backends/plotlyjs.jl b/src/backends/plotlyjs.jl index 1f12358f..ca972d98 100644 --- a/src/backends/plotlyjs.jl +++ b/src/backends/plotlyjs.jl @@ -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 - diff --git a/src/backends/supported.jl b/src/backends/supported.jl index 11168686..6b868f58 100644 --- a/src/backends/supported.jl +++ b/src/backends/supported.jl @@ -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 - diff --git a/src/output.jl b/src/output.jl index aeb3adfb..f3659a0d 100644 --- a/src/output.jl +++ b/src/output.jl @@ -124,18 +124,23 @@ end # --------------------------------------------------------- function setup_atom() - # @require Atom begin - # @eval begin - # import Atom - # - # 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 - # end + @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(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 diff --git a/src/utils.jl b/src/utils.jl index 3bdd2c2f..30ea6f99 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -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 @@ -498,4 +499,4 @@ xmin(plt::Plot) = minimum([minimum(d[:x]) for d in plt.seriesargs]) xmax(plt::Plot) = maximum([maximum(d[:x]) for d in plt.seriesargs]) "Extrema of x-values in plot" -Base.extrema(plt::Plot) = (xmin(plt), xmax(plt)) \ No newline at end of file +Base.extrema(plt::Plot) = (xmin(plt), xmax(plt))