plotly support for ijulia and subplots
This commit is contained in:
parent
a0af2fc997
commit
e28b85d8b6
@ -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
|
# TODO: build the underlying Subplot object. this is where you might layout the panes within a GUI window, for example
|
||||||
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
function _expand_limits(lims, plt::Plot{PlotlyPackage}, isx::Bool)
|
function _expand_limits(lims, plt::Plot{PlotlyPackage}, isx::Bool)
|
||||||
@ -94,7 +95,7 @@ function plotlyscale(scale::Symbol)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function get_plot_html(plt::Plot{PlotlyPackage})
|
function get_plot_json(plt::Plot{PlotlyPackage})
|
||||||
d = plt.plotargs
|
d = plt.plotargs
|
||||||
d_out = Dict()
|
d_out = Dict()
|
||||||
|
|
||||||
@ -104,7 +105,7 @@ function get_plot_html(plt::Plot{PlotlyPackage})
|
|||||||
# TODO: set the fields for the plot
|
# TODO: set the fields for the plot
|
||||||
d_out[:title] = d[:title]
|
d_out[:title] = d[:title]
|
||||||
d_out[:titlefont] = plotlyfont(d[:guidefont])
|
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(:l=>30, :b=>30, :r=>15, :t=>15)
|
||||||
# d_out[:margin] = Dict(:t=>20)
|
# d_out[:margin] = Dict(:t=>20)
|
||||||
d_out[:paper_bgcolor] = bgcolor
|
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)
|
# 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()
|
d_out = Dict()
|
||||||
|
|
||||||
x, y = collect(d[:x]), collect(d[:y])
|
x, y = collect(d[:x]), collect(d[:y])
|
||||||
@ -313,49 +314,95 @@ function get_series_html(d::Dict)
|
|||||||
)
|
)
|
||||||
end
|
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
|
d_out
|
||||||
end
|
end
|
||||||
|
|
||||||
# get a list of dictionaries, each representing the series params
|
# get a list of dictionaries, each representing the series params
|
||||||
function get_series_html(plt::Plot{PlotlyPackage})
|
function get_series_json(plt::Plot{PlotlyPackage})
|
||||||
JSON.json(map(get_series_html, plt.seriesargs))
|
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
|
end
|
||||||
|
|
||||||
# ----------------------------------------------------------------
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
function html_head(plt::Plot{PlotlyPackage})
|
function html_head(plt::PlottingObject{PlotlyPackage})
|
||||||
"<script src=\"$(Pkg.dir("Plots","deps","plotly-latest.min.js"))\"></script>"
|
"<script src=\"$(Pkg.dir("Plots","deps","plotly-latest.min.js"))\"></script>"
|
||||||
end
|
end
|
||||||
|
|
||||||
function html_body(plt::Plot{PlotlyPackage})
|
function html_body(plt::Plot{PlotlyPackage}, style = nothing)
|
||||||
w, h = plt.plotargs[:size]
|
if style == nothing
|
||||||
|
w, h = plt.plotargs[:size]
|
||||||
|
style = "width:$(w)px;height:$(h)px;"
|
||||||
|
end
|
||||||
|
uuid = Base.Random.uuid4()
|
||||||
"""
|
"""
|
||||||
<div id=\"myplot\" style=\"width:$(w)px;height:$(h)px;\"></div>
|
<div id=\"$(uuid)\" style=\"$(style)\"></div>
|
||||||
<script>
|
<script>
|
||||||
PLOT = document.getElementById('myplot');
|
PLOT = document.getElementById('$(uuid)');
|
||||||
Plotly.plot(PLOT, $(get_series_html(plt)), $(get_plot_html(plt)));
|
Plotly.plot(PLOT, $(get_series_json(plt)), $(get_plot_json(plt)));
|
||||||
</script>
|
</script>
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function html_body(subplt::Subplot{PlotlyPackage})
|
||||||
|
w, h = subplt.plts[1].plotargs[:size]
|
||||||
|
html = ["<div style=\"width:$(w)px;height:$(h)px;\">"]
|
||||||
|
nr = nrows(subplt.layout)
|
||||||
|
ph = h / nr
|
||||||
|
|
||||||
|
for r in 1:nr
|
||||||
|
push!(html, "<div style=\"clear:both;\">")
|
||||||
|
|
||||||
|
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, "</div>")
|
||||||
|
end
|
||||||
|
push!(html, "</div>")
|
||||||
|
|
||||||
|
join(html)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
function Base.writemime(io::IO, ::MIME"image/png", plt::PlottingObject{PlotlyPackage})
|
function Base.writemime(io::IO, ::MIME"image/png", plt::PlottingObject{PlotlyPackage})
|
||||||
|
isijulia() && return
|
||||||
# TODO: write a png to io
|
# TODO: write a png to io
|
||||||
println("png")
|
println("todo: png")
|
||||||
end
|
end
|
||||||
|
|
||||||
function Base.writemime(io::IO, ::MIME"text/html", plt::PlottingObject{PlotlyPackage})
|
function Base.writemime(io::IO, ::MIME"text/html", plt::PlottingObject{PlotlyPackage})
|
||||||
println("html")
|
write(io, html_head(plt) * html_body(plt))
|
||||||
html_head(plt) * html_body(plt)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Base.display(::PlotsDisplay, plt::Plot{PlotlyPackage})
|
function Base.display(::PlotsDisplay, plt::PlottingObject{PlotlyPackage})
|
||||||
standalone_html_window(plt)
|
standalone_html_window(plt)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage})
|
# function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage})
|
||||||
# TODO: display/show the subplot
|
# # TODO: display/show the subplot
|
||||||
end
|
# end
|
||||||
|
|||||||
@ -489,6 +489,6 @@ supportedStyles(::PlotlyPackage) = [:auto, :solid, :dash, :dot, :dashdot]
|
|||||||
supportedMarkers(::PlotlyPackage) = [:none, :auto, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross,
|
supportedMarkers(::PlotlyPackage) = [:none, :auto, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross,
|
||||||
:pentagon, :hexagon, :octagon] #vcat(_allMarkers, Shape)
|
:pentagon, :hexagon, :octagon] #vcat(_allMarkers, Shape)
|
||||||
supportedScales(::PlotlyPackage) = [:identity, :log] #, :log, :log2, :log10, :asinh, :sqrt]
|
supportedScales(::PlotlyPackage) = [:identity, :log] #, :log, :log2, :log10, :asinh, :sqrt]
|
||||||
subplotSupported(::PlotlyPackage) = false
|
subplotSupported(::PlotlyPackage) = true
|
||||||
stringsSupported(::PlotlyPackage) = true
|
stringsSupported(::PlotlyPackage) = true
|
||||||
|
|
||||||
|
|||||||
@ -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
|
# TODO: build the underlying Subplot object. this is where you might layout the panes within a GUI window, for example
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -238,13 +238,31 @@ function backend()
|
|||||||
|
|
||||||
elseif currentBackendSymbol == :plotly
|
elseif currentBackendSymbol == :plotly
|
||||||
try
|
try
|
||||||
# @eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "web.jl"))
|
|
||||||
# @eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "plotly.jl"))
|
|
||||||
@eval begin
|
@eval begin
|
||||||
import JSON
|
import JSON
|
||||||
JSON._print(io::IO, state::JSON.State, dt::Union{Date,DateTime}) = print(io, '"', dt, '"')
|
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", """
|
||||||
|
<script type="text/javascript">
|
||||||
|
require=requirejs=define=undefined;
|
||||||
|
</script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
$(open(readall, _js_path, "r"))
|
||||||
|
</script>
|
||||||
|
""")
|
||||||
|
# display("text/html", "<p>Plotly javascript loaded.</p>")
|
||||||
|
end
|
||||||
|
# end borrowing (thanks :)
|
||||||
|
###########################
|
||||||
|
|
||||||
end
|
end
|
||||||
catch err
|
catch err
|
||||||
warn("Couldn't setup Plotly")
|
warn("Couldn't setup Plotly")
|
||||||
|
|||||||
@ -188,6 +188,7 @@ function fakedata(sz...)
|
|||||||
y
|
y
|
||||||
end
|
end
|
||||||
|
|
||||||
|
isijulia() = isdefined(Main, :IJulia) && Main.IJulia.inited
|
||||||
|
|
||||||
|
|
||||||
# ticksType{T<:Real,S<:Real}(ticks::@compat(Tuple{T,S})) = :limits
|
# ticksType{T<:Real,S<:Real}(ticks::@compat(Tuple{T,S})) = :limits
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user