Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added writemime methods for plotly_blink #113

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export
path3d!,
scatter3d,
scatter3d!,
abline!,

title!,
xlabel!,
Expand Down
40 changes: 20 additions & 20 deletions src/backends/plotly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
# ---------------------------------------------------------------------------

function _create_plot(pkg::PlotlyPackage; kw...)
d = Dict(kw)
d = Dict{Symbol,Any}(kw)
# TODO: create the window/canvas/context that is the plot within the backend (call it `o`)
# TODO: initialize the plot... title, xlabel, bgcolor, etc
Plot(nothing, pkg, 0, d, Dict[])
end


function _add_series(::PlotlyPackage, plt::Plot; kw...)
d = Dict(kw)
d = Dict{Symbol,Any}(kw)
# TODO: add one series to the underlying package
push!(plt.seriesargs, d)
plt
Expand Down Expand Up @@ -83,15 +83,15 @@ end
# _plotDefaults[:yflip] = false

function plotlyfont(font::Font, color = font.color)
Dict(
Dict{Symbol,Any}(
:family => font.family,
:size => round(Int, font.pointsize*1.4),
:color => webcolor(color),
)
end

function get_annotation_dict(x, y, val::Union{AbstractString,Symbol})
Dict(
Dict{Symbol,Any}(
:text => val,
:xref => "x",
:x => x,
Expand All @@ -102,7 +102,7 @@ function get_annotation_dict(x, y, val::Union{AbstractString,Symbol})
end

function get_annotation_dict(x, y, ptxt::PlotText)
merge(get_annotation_dict(x, y, ptxt.str), Dict(
merge(get_annotation_dict(x, y, ptxt.str), Dict{Symbol,Any}(
:font => plotlyfont(ptxt.font),
:xanchor => ptxt.font.halign == :hcenter ? :center : ptxt.font.halign,
:yanchor => ptxt.font.valign == :vcenter ? :middle : ptxt.font.valign,
Expand All @@ -127,7 +127,7 @@ scalesym(isx::Bool) = symbol((isx ? "x" : "y") * "scale")
labelsym(isx::Bool) = symbol((isx ? "x" : "y") * "label")

function plotlyaxis(d::Dict, isx::Bool)
ax = Dict(
ax = Dict{Symbol,Any}(
:title => d[labelsym(isx)],
:showgrid => d[:grid],
:zeroline => false,
Expand Down Expand Up @@ -179,26 +179,26 @@ end
# function get_plot_json(plt::Plot{PlotlyPackage})
# d = plt.plotargs
function plotly_layout(d::Dict)
d_out = Dict()
d_out = Dict{Symbol,Any}()

bgcolor = webcolor(d[:background_color])
fgcolor = webcolor(d[:foreground_color])

# set the fields for the plot
d_out[:title] = d[:title]
d_out[:titlefont] = plotlyfont(d[:guidefont], fgcolor)
d_out[:margin] = Dict(:l=>35, :b=>30, :r=>8, :t=>20)
d_out[:margin] = Dict{Symbol,Any}(:l=>35, :b=>30, :r=>8, :t=>20)
d_out[:plot_bgcolor] = bgcolor
d_out[:paper_bgcolor] = bgcolor

# TODO: x/y axis tick values/labels
d_out[:xaxis] = plotlyaxis(d, true)
d_out[:yaxis] = plotlyaxis(d, false)

# legend
d_out[:showlegend] = d[:legend]
if d[:legend]
d_out[:legend] = Dict(
d_out[:legend] = Dict{Symbol,Any}(
:bgcolor => bgcolor,
:bordercolor => fgcolor,
:font => plotlyfont(d[:legendfont]),
Expand All @@ -224,7 +224,7 @@ function plotly_colorscale(grad::ColorGradient)
end
plotly_colorscale(c) = plotly_colorscale(ColorGradient(:bluesreds))

const _plotly_markers = Dict(
const _plotly_markers = Dict{Symbol,Any}(
:rect => "square",
:xcross => "x",
:utriangle => "triangle-up",
Expand All @@ -236,7 +236,7 @@ const _plotly_markers = Dict(

# get a dictionary representing the series params (d is the Plots-dict, d_out is the Plotly-dict)
function plotly_series(d::Dict; plot_index = nothing)
d_out = Dict()
d_out = Dict{Symbol,Any}()

x, y = collect(d[:x]), collect(d[:y])
d_out[:name] = d[:label]
Expand Down Expand Up @@ -293,7 +293,7 @@ function plotly_series(d::Dict; plot_index = nothing)
# d_out[:showscale] = d[:legend]
if lt == :contour
d_out[:ncontours] = d[:levels]
d_out[:contours] = Dict(:coloring => d[:fillrange] != nothing ? "fill" : "lines")
d_out[:contours] = Dict{Symbol,Any}(:coloring => d[:fillrange] != nothing ? "fill" : "lines")
end
d_out[:colorscale] = plotly_colorscale(d[lt == :contour ? :linecolor : :fillcolor])

Expand All @@ -315,17 +315,17 @@ function plotly_series(d::Dict; plot_index = nothing)

else
warn("Plotly: linetype $lt isn't supported.")
return Dict()
return Dict{Symbol,Any}()
end

# add "marker"
if hasmarker
d_out[:marker] = Dict(
d_out[:marker] = Dict{Symbol,Any}(
:symbol => get(_plotly_markers, d[:markershape], string(d[:markershape])),
:opacity => d[:markeralpha],
:size => 2 * d[:markersize],
:color => webcolor(d[:markercolor], d[:markeralpha]),
:line => Dict(
:line => Dict{Symbol,Any}(
:color => webcolor(d[:markerstrokecolor], d[:markerstrokealpha]),
:width => d[:markerstrokewidth],
),
Expand All @@ -338,7 +338,7 @@ function plotly_series(d::Dict; plot_index = nothing)

# add "line"
if hasline
d_out[:line] = Dict(
d_out[:line] = Dict{Symbol,Any}(
:color => webcolor(d[:linecolor], d[:linealpha]),
:width => d[:linewidth],
:shape => if lt == :steppre
Expand Down Expand Up @@ -405,18 +405,18 @@ function html_body(subplt::Subplot{PlotlyPackage})
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>")
Expand Down
24 changes: 15 additions & 9 deletions src/backends/plotly_blink.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

# override some methods to use Plotlyjs/Blink
# override some methods to use PlotlyJS/Blink

import Plotlyjs
import PlotlyJS

function _create_plot(pkg::PlotlyPackage; kw...)
d = Dict(kw)
# TODO: create the window/canvas/context that is the plot within the backend (call it `o`)
# TODO: initialize the plot... title, xlabel, bgcolor, etc
o = Plotlyjs.Plot()
o = PlotlyJS.Plot()

Plot(o, pkg, 0, d, Dict[])
end
Expand All @@ -18,10 +18,11 @@ function _add_series(::PlotlyPackage, plt::Plot; kw...)

# add to the data array
pdict = plotly_series(d)
gt = Plotlyjs.GenericTrace(pdict[:type], pdict)
typ = pop!(pdict, :type)
gt = PlotlyJS.GenericTrace(typ; pdict...)
push!(plt.o.data, gt)
if !isnull(plt.o.window)
Plotlyjs.addtraces!(plt.o, gt)
PlotlyJS.addtraces!(plt.o, gt)
end

push!(plt.seriesargs, d)
Expand All @@ -31,18 +32,23 @@ end
# TODO: override this to update plot items (title, xlabel, etc) after creation
function _update_plot(plt::Plot{PlotlyPackage}, d::Dict)
pdict = plotly_layout(d)
plt.o.layout = Plotlyjs.Layout(pdict)
plt.o.layout = PlotlyJS.Layout(pdict)
if !isnull(plt.o.window)
Plotlyjs.relayout!(plt.o, pdict...)
PlotlyJS.relayout!(plt.o; pdict...)
end
end


function Base.display(::PlotsDisplay, plt::Plot{PlotlyPackage})
dump(plt.o)
show(plt.o)
display(plt.o)
end

function Base.display(::PlotsDisplay, plt::Subplot{PlotlyPackage})
error()
error()
end

for (mime, fmt) in PlotlyJS._mimeformats
@eval Base.writemime(io::IO, m::MIME{symbol($mime)}, p::Plot{PlotlyPackage}) =
writemime(io, m, p.o)
end
10 changes: 5 additions & 5 deletions src/plotter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ function backend()
# end borrowing (thanks :)
###########################

# try
# include(joinpath(Pkg.dir("Plots"), "src", "backends", "plotly_blink.jl"))
# catch err
# warn("Error including Plotlyjs: $err\n Note: Will fall back to built-in display.")
# end
try
include(joinpath(Pkg.dir("Plots"), "src", "backends", "plotly_blink.jl"))
catch err
warn("Error including PlotlyJS: $err\n Note: Will fall back to built-in display.")
end

end
catch err
Expand Down
6 changes: 6 additions & 0 deletions src/recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,9 @@ function spy{T<:Real}(y::AMat{T}; kw...)
heatmap(J, I; leg=false, yflip=true, nbins=size(y), kw...)
end

"Adds a+bx... straight line over the current plot"
function abline!(plt::Plot, a, b; kw...)
plot!(plt, [extrema(plt)...], x -> b + a*x; kw...)
end

abline!(args...; kw...) = abline!(current(), args...; kw...)
14 changes: 11 additions & 3 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ function with(f::Function, args...; kw...)
oldbackend = CURRENT_BACKEND.sym

for arg in args

# change backend?
if arg in backends()
backend(arg)
Expand Down Expand Up @@ -421,10 +421,10 @@ function supportGraph(allvals, func)
push!(x, string(b))
push!(y, string(val))
end
end
end
end
n = length(vals)

scatter(x,y,
m=:rect,
ms=10,
Expand Down Expand Up @@ -465,3 +465,11 @@ mm2inch(mm::Real) = float(mm / MM_PER_INCH)
px2mm(px::Real) = float(px * MM_PER_PX)
mm2px(mm::Real) = float(px / MM_PER_PX)


"Smallest x in plot"
xmin(plt::Plot) = minimum([minimum(d[:x]) for d in plt.seriesargs])
"Largest x in plot"
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))