diff --git a/src/Plots.jl b/src/Plots.jl index feca5ce9b..5dcd596d7 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -59,6 +59,7 @@ export path3d!, scatter3d, scatter3d!, + abline!, title!, xlabel!, diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 00db4c62f..aa1141cbd 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -4,7 +4,7 @@ # --------------------------------------------------------------------------- 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[]) @@ -12,7 +12,7 @@ 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 @@ -83,7 +83,7 @@ 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), @@ -91,7 +91,7 @@ function plotlyfont(font::Font, color = font.color) end function get_annotation_dict(x, y, val::Union{AbstractString,Symbol}) - Dict( + Dict{Symbol,Any}( :text => val, :xref => "x", :x => x, @@ -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, @@ -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, @@ -179,7 +179,7 @@ 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]) @@ -187,10 +187,10 @@ function plotly_layout(d::Dict) # 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) @@ -198,7 +198,7 @@ function plotly_layout(d::Dict) # 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]), @@ -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", @@ -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] @@ -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]) @@ -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], ), @@ -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 @@ -405,10 +405,10 @@ function html_body(subplt::Subplot{PlotlyPackage}) html = ["
"] nr = nrows(subplt.layout) ph = h / nr - + for r in 1:nr push!(html, "
") - + nc = ncols(subplt.layout, r) pw = w / nc @@ -416,7 +416,7 @@ function html_body(subplt::Subplot{PlotlyPackage}) plt = subplt[r,c] push!(html, html_body(plt, "float:left; width:$(pw)px; height:$(ph)px;")) end - + push!(html, "
") end push!(html, "
") diff --git a/src/backends/plotly_blink.jl b/src/backends/plotly_blink.jl index c5f5a6f54..2c7abed9c 100644 --- a/src/backends/plotly_blink.jl +++ b/src/backends/plotly_blink.jl @@ -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 @@ -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) @@ -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 diff --git a/src/plotter.jl b/src/plotter.jl index da50d1bbd..ebf3295eb 100644 --- a/src/plotter.jl +++ b/src/plotter.jl @@ -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 diff --git a/src/recipes.jl b/src/recipes.jl index 44ba5d856..adf598729 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -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...) \ No newline at end of file diff --git a/src/utils.jl b/src/utils.jl index 68f205837..904310429 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -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) @@ -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, @@ -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)) \ No newline at end of file