Merge 7264c01b85dccf1d5e4c45c3a4e1bb374f2c7968 into 8b2a83838894dc6d7f903a75e209362137e4d237

This commit is contained in:
Josef Heinen 2016-03-16 17:28:10 +00:00
commit ac5fb47503
12 changed files with 616 additions and 516 deletions

View File

@ -126,7 +126,8 @@ export
# recipes # recipes
PlotRecipe, PlotRecipe,
# EllipseRecipe, # EllipseRecipe,
spy spy,
arcdiagram
# corrplot # corrplot

View File

@ -11,7 +11,7 @@ const _3dTypes = [:path3d, :scatter3d, :surface, :wireframe]
const _allTypes = vcat([ const _allTypes = vcat([
:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :none, :line, :path, :steppre, :steppost, :sticks, :scatter,
:heatmap, :hexbin, :hist, :hist2d, :hist3d, :density, :bar, :hline, :vline, :ohlc, :heatmap, :hexbin, :hist, :hist2d, :hist3d, :density, :bar, :hline, :vline, :ohlc,
:contour, :pie :contour, :pie, :shape
], _3dTypes) ], _3dTypes)
@compat const _typeAliases = Dict( @compat const _typeAliases = Dict(
:n => :none, :n => :none,
@ -35,6 +35,9 @@ const _allTypes = vcat([
:line3d => :path3d, :line3d => :path3d,
:surf => :surface, :surf => :surface,
:wire => :wireframe, :wire => :wireframe,
:shapes => :shape,
:poly => :shape,
:polygon => :shape,
) )
ishistlike(lt::Symbol) = lt in (:hist, :density) ishistlike(lt::Symbol) = lt in (:hist, :density)
@ -801,5 +804,3 @@ function getSeriesArgs(pkg::AbstractBackend, plotargs::Dict, kw, commandIndex::I
d d
end end

View File

@ -34,7 +34,7 @@ end
function getLineGeom(d::Dict) function getLineGeom(d::Dict)
lt = d[:linetype] lt = d[:linetype]
xbins, ybins = maketuple(d[:nbins]) xbins, ybins = maketuple(d[:nbins])
if lt == :hexbin if lt == :hexb
Gadfly.Geom.hexbin(xbincount = xbins, ybincount = ybins) Gadfly.Geom.hexbin(xbincount = xbins, ybincount = ybins)
elseif lt == :hist2d elseif lt == :hist2d
Gadfly.Geom.histogram2d(xbincount = xbins, ybincount = ybins) Gadfly.Geom.histogram2d(xbincount = xbins, ybincount = ybins)
@ -54,6 +54,8 @@ function getLineGeom(d::Dict)
Gadfly.Geom.vline Gadfly.Geom.vline
elseif lt == :contour elseif lt == :contour
Gadfly.Geom.contour(levels = d[:levels]) Gadfly.Geom.contour(levels = d[:levels])
# elseif lt == :shape
# Gadfly.Geom.polygon(fill = true, preserve_order = true)
else else
nothing nothing
end end
@ -75,21 +77,8 @@ function get_extra_theme_args(d::Dict, k::Symbol)
end end
function getGadflyLineTheme(d::Dict) function getGadflyLineTheme(d::Dict)
lc = convertColor(getColor(d[:linecolor]), d[:linealpha]) lc = convertColor(getColor(d[:linecolor]), d[:linealpha])
# lc = getColor(d[:linecolor])
# α = d[:linealpha]
# if α != nothing
# lc = RGBA(lc, α)
# end
fc = convertColor(getColor(d[:fillcolor]), d[:fillalpha]) fc = convertColor(getColor(d[:fillcolor]), d[:fillalpha])
# fc = getColor(d[:fillcolor])
# α = d[:fillalpha]
# if α != nothing
# fc = RGBA(fc, α)
# end
Gadfly.Theme(; Gadfly.Theme(;
default_color = lc, default_color = lc,
@ -105,8 +94,7 @@ end
# add a line as a new layer # add a line as a new layer
function addGadflyLine!(plt::Plot, numlayers::Int, d::Dict, geoms...) function addGadflyLine!(plt::Plot, numlayers::Int, d::Dict, geoms...)
gplt = getGadflyContext(plt) gplt = getGadflyContext(plt)
gfargs = vcat(geoms..., gfargs = vcat(geoms..., getGadflyLineTheme(d))
getGadflyLineTheme(d))
kwargs = Dict() kwargs = Dict()
lt = d[:linetype] lt = d[:linetype]
@ -128,7 +116,6 @@ function addGadflyLine!(plt::Plot, numlayers::Int, d::Dict, geoms...)
kwargs[:xmin] = d[:x] - w kwargs[:xmin] = d[:x] - w
kwargs[:xmax] = d[:x] + w kwargs[:xmax] = d[:x] + w
elseif lt == :contour elseif lt == :contour
# d[:y] = reverse(d[:y])
kwargs[:z] = d[:z].surf kwargs[:z] = d[:z].surf
addGadflyContColorScale(plt, d[:linecolor]) addGadflyContColorScale(plt, d[:linecolor])
end end
@ -138,30 +125,7 @@ function addGadflyLine!(plt::Plot, numlayers::Int, d::Dict, geoms...)
end end
# # h/vlines
# if lt == :hline
# kwargs[:yintercept] = d[:y]
# elseif lt == :vline
# kwargs[:xintercept] = d[:y]
# elseif lt == :sticks
# w = 0.01 * mean(diff(d[:x]))
# kwargs[:xmin] = d[:x] - w
# kwargs[:xmax] = d[:x] + w
# elseif lt == :contour
# d[:y] = reverse(d[:y])
# kwargs[:z] = d[:surface]
# end
# if lt == :hist
# kwargs[:x] = kwargs[:y] = d[:y]
# elseif lt != :hline && lt != :vline
# kwargs[:x] = d[:x]
# kwargs[:y] = d[:y]
# end
# # add the layer # # add the layer
# x = d[d[:linetype] == :hist ? :y : :x]
# Gadfly.layer(gfargs...; x = x, y = d[:y], order=numlayers, kwargs...)
Gadfly.layer(gfargs...; order=numlayers, kwargs...) Gadfly.layer(gfargs...; order=numlayers, kwargs...)
end end
@ -172,13 +136,13 @@ end
getMarkerGeom(shape::Shape) = gadflyshape(shape) getMarkerGeom(shape::Shape) = gadflyshape(shape)
getMarkerGeom(shape::Symbol) = gadflyshape(_shapes[shape]) getMarkerGeom(shape::Symbol) = gadflyshape(_shapes[shape])
getMarkerGeom(shapes::AVec) = map(getMarkerGeom, shapes) getMarkerGeom(shapes::AVec) = map(getMarkerGeom, shapes)
getMarkerGeom(d::Dict) = getMarkerGeom(d[:markershape]) function getMarkerGeom(d::Dict)
if d[:linetype] == :shape
# function getMarkerGeom(d::Dict) Gadfly.Geom.polygon(fill = true, preserve_order = true)
# shape = d[:markershape] else
# gadflyshape(isa(shape, Shape) ? shape : _shapes[shape]) getMarkerGeom(d[:markershape])
# end end
end
function getGadflyMarkerTheme(d::Dict, plotargs::Dict) function getGadflyMarkerTheme(d::Dict, plotargs::Dict)
c = getColor(d[:markercolor]) c = getColor(d[:markercolor])
@ -195,12 +159,12 @@ function getGadflyMarkerTheme(d::Dict, plotargs::Dict)
ms * Gadfly.px ms * Gadfly.px
end end
# fg = getColor(plotargs[:foreground_color])
Gadfly.Theme(; Gadfly.Theme(;
default_color = c, default_color = c,
default_point_size = ms, default_point_size = ms,
discrete_highlight_color = c -> RGB(getColor(d[:markerstrokecolor])), discrete_highlight_color = c -> RGB(getColor(d[:markerstrokecolor])),
highlight_width = d[:markerstrokewidth] * Gadfly.px, highlight_width = d[:markerstrokewidth] * Gadfly.px,
line_width = d[:markerstrokewidth] * Gadfly.px,
# get_extra_theme_args(d, :markerstrokestyle)... # get_extra_theme_args(d, :markerstrokestyle)...
) )
end end
@ -214,9 +178,7 @@ function addGadflyContColorScale(plt::Plot{GadflyBackend}, c)
end end
function addGadflyMarker!(plt::Plot, numlayers::Int, d::Dict, plotargs::Dict, geoms...) function addGadflyMarker!(plt::Plot, numlayers::Int, d::Dict, plotargs::Dict, geoms...)
gfargs = vcat(geoms..., gfargs = vcat(geoms..., getGadflyMarkerTheme(d, plotargs), getMarkerGeom(d))
getGadflyMarkerTheme(d, plotargs),
getMarkerGeom(d))
kwargs = Dict() kwargs = Dict()
# handle continuous color scales for the markers # handle continuous color scales for the markers
@ -224,10 +186,6 @@ function addGadflyMarker!(plt::Plot, numlayers::Int, d::Dict, plotargs::Dict, ge
if zcolor != nothing && typeof(zcolor) <: AVec if zcolor != nothing && typeof(zcolor) <: AVec
kwargs[:color] = zcolor kwargs[:color] = zcolor
addGadflyContColorScale(plt, d[:markercolor]) addGadflyContColorScale(plt, d[:markercolor])
# if !isa(d[:markercolor], ColorGradient)
# d[:markercolor] = colorscheme(:bluesreds)
# end
# push!(getGadflyContext(plt).scales, Gadfly.Scale.ContinuousColorScale(p -> RGB(getColorZ(d[:markercolor], p))))
end end
Gadfly.layer(gfargs...; x = d[:x], y = d[:y], order=numlayers, kwargs...) Gadfly.layer(gfargs...; x = d[:x], y = d[:y], order=numlayers, kwargs...)
@ -237,8 +195,6 @@ end
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
function addToGadflyLegend(plt::Plot, d::Dict) function addToGadflyLegend(plt::Plot, d::Dict)
# add the legend?
if plt.plotargs[:legend] != :none && d[:label] != "" if plt.plotargs[:legend] != :none && d[:label] != ""
gplt = getGadflyContext(plt) gplt = getGadflyContext(plt)
@ -272,9 +228,7 @@ function addToGadflyLegend(plt::Plot, d::Dict)
end end
end end
end end
end end
end end
getGadflySmoothing(smooth::Bool) = smooth ? [Gadfly.Geom.smooth(method=:lm)] : Any[] getGadflySmoothing(smooth::Bool) = smooth ? [Gadfly.Geom.smooth(method=:lm)] : Any[]
@ -282,7 +236,6 @@ getGadflySmoothing(smooth::Real) = [Gadfly.Geom.smooth(method=:loess, smoothing=
function addGadflySeries!(plt::Plot, d::Dict) function addGadflySeries!(plt::Plot, d::Dict)
layers = Gadfly.Layer[] layers = Gadfly.Layer[]
gplt = getGadflyContext(plt) gplt = getGadflyContext(plt)
@ -294,9 +247,7 @@ function addGadflySeries!(plt::Plot, d::Dict)
geom = getLineGeom(d) geom = getLineGeom(d)
if geom != nothing if geom != nothing
prepend!(layers, addGadflyLine!(plt, length(gplt.layers), d, geom, smooth...)) prepend!(layers, addGadflyLine!(plt, length(gplt.layers), d, geom, smooth...))
smooth = Any[] # don't add a regression for markers too
# don't add a regression for markers too
smooth = Any[]
end end
# special handling for ohlc and scatter # special handling for ohlc and scatter
@ -310,7 +261,7 @@ function addGadflySeries!(plt::Plot, d::Dict)
end end
# markers # markers
if d[:markershape] != :none if d[:markershape] != :none || lt == :shape
prepend!(layers, addGadflyMarker!(plt, length(gplt.layers), d, plt.plotargs, smooth...)) prepend!(layers, addGadflyMarker!(plt, length(gplt.layers), d, plt.plotargs, smooth...))
end end
@ -401,8 +352,6 @@ end
function addGadflyLimitsScale(gplt, d::Dict, isx::Bool) function addGadflyLimitsScale(gplt, d::Dict, isx::Bool)
# get the correct scale function
gfunc, hasScaleKey = getGadflyScaleFunction(d, isx) gfunc, hasScaleKey = getGadflyScaleFunction(d, isx)
# do we want to add min/max limits for the axis? # do we want to add min/max limits for the axis?
@ -411,7 +360,6 @@ function addGadflyLimitsScale(gplt, d::Dict, isx::Bool)
# map :auto to nothing, otherwise add to limargs # map :auto to nothing, otherwise add to limargs
lims = get(d, limsym, :auto) lims = get(d, limsym, :auto)
# lims == :auto && return
if lims == :auto if lims == :auto
lims = nothing lims = nothing
else else
@ -485,7 +433,6 @@ function updateGadflyGuides(plt::Plot, d::Dict)
else else
addGadflyTicksGuide(gplt, ticks, false) addGadflyTicksGuide(gplt, ticks, false)
end end
# haskey(d, :yticks) && addGadflyTicksGuide(gplt, d[:yticks], false)
updateGadflyAxisFlips(gplt, d, xlims, ylims) updateGadflyAxisFlips(gplt, d, xlims, ylims)
end end
@ -493,14 +440,7 @@ end
function updateGadflyPlotTheme(plt::Plot, d::Dict) function updateGadflyPlotTheme(plt::Plot, d::Dict)
kwargs = Dict() kwargs = Dict()
# # get the full plotargs, overriding any new settings
# # TODO: should this be part of the main `plot` command in plot.jl???
# d = merge!(plt.plotargs, d)
# # hide the legend? # # hide the legend?
# if !get(d, :legend, true)
# kwargs[:key_position] = :none
# end
leg = d[d[:legend] == :none ? :colorbar : :legend] leg = d[d[:legend] == :none ? :colorbar : :legend]
if leg != :best if leg != :best
kwargs[:key_position] = leg == :inside ? :right : leg kwargs[:key_position] = leg == :inside ? :right : leg
@ -576,7 +516,6 @@ end
# plot one data series # plot one data series
function _add_series(::GadflyBackend, plt::Plot; kw...) function _add_series(::GadflyBackend, plt::Plot; kw...)
# first clear out the temporary layer # first clear out the temporary layer
gplt = getGadflyContext(plt) gplt = getGadflyContext(plt)
if gplt.layers[1].geom.tag == :remove if gplt.layers[1].geom.tag == :remove
@ -704,12 +643,10 @@ function Base.display(::PlotsDisplay, plt::Plot{GadflyBackend})
end end
function Base.display(::PlotsDisplay, subplt::Subplot{GadflyBackend}) function Base.display(::PlotsDisplay, subplt::Subplot{GadflyBackend})
setGadflyDisplaySize(getplotargs(subplt,1)[:size]...) setGadflyDisplaySize(getplotargs(subplt,1)[:size]...)
ctx = buildGadflySubplotContext(subplt) ctx = buildGadflySubplotContext(subplt)
# taken from Gadfly since I couldn't figure out how to do it directly # taken from Gadfly since I couldn't figure out how to do it directly
filename = string(Gadfly.tempname(), ".html") filename = string(Gadfly.tempname(), ".html")

View File

@ -98,36 +98,36 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
mwidth, mheight, width, height = GR.inqdspsize() mwidth, mheight, width, height = GR.inqdspsize()
w, h = d[:size] w, h = d[:size]
viewport = zeros(4) viewport = zeros(4)
vp = float(subplot)
if w > h if w > h
ratio = float(h) / w ratio = float(h) / w
msize = mwidth * w / width msize = mwidth * w / width
GR.setwsviewport(0, msize, 0, msize * ratio) GR.setwsviewport(0, msize, 0, msize * ratio)
GR.setwswindow(0, 1, 0, ratio) GR.setwswindow(0, 1, 0, ratio)
viewport[1] = subplot[1] + 0.125 * (subplot[2] - subplot[1]) vp[3] *= ratio
viewport[2] = subplot[1] + 0.95 * (subplot[2] - subplot[1]) vp[4] *= ratio
viewport[3] = ratio * (subplot[3] + 0.125 * (subplot[4] - subplot[3]))
viewport[4] = ratio * (subplot[3] + 0.95 * (subplot[4] - subplot[3]))
else else
ratio = float(w) / h ratio = float(w) / h
msize = mheight * h / height msize = mheight * h / height
GR.setwsviewport(0, msize * ratio, 0, msize) GR.setwsviewport(0, msize * ratio, 0, msize)
GR.setwswindow(0, ratio, 0, 1) GR.setwswindow(0, ratio, 0, 1)
viewport[1] = ratio * (subplot[1] + 0.125 * (subplot[2] - subplot[1])) vp[1] *= ratio
viewport[2] = ratio * (subplot[1] + 0.95 * (subplot[2] - subplot[1])) vp[2] *= ratio
viewport[3] = subplot[3] + 0.125 * (subplot[4] - subplot[3])
viewport[4] = subplot[3] + 0.95 * (subplot[4] - subplot[3])
end end
viewport[1] = vp[1] + 0.125 * (vp[2] - vp[1])
viewport[2] = vp[1] + 0.95 * (vp[2] - vp[1])
viewport[3] = vp[3] + 0.125 * (vp[4] - vp[3])
if w > h
viewport[3] += (1 - (subplot[4] - subplot[3])^2) * 0.02
end
viewport[4] = vp[3] + 0.95 * (vp[4] - vp[3])
if haskey(d, :background_color) if haskey(d, :background_color)
GR.savestate() GR.savestate()
GR.selntran(0) GR.selntran(0)
GR.setfillintstyle(GR.INTSTYLE_SOLID) GR.setfillintstyle(GR.INTSTYLE_SOLID)
GR.setfillcolorind(gr_getcolorind(d[:background_color])) GR.setfillcolorind(gr_getcolorind(d[:background_color]))
if w > h GR.fillrect(vp[1], vp[2], vp[3], vp[4])
GR.fillrect(subplot[1], subplot[2], ratio*subplot[3], ratio*subplot[4])
else
GR.fillrect(ratio*subplot[1], ratio*subplot[2], subplot[3], subplot[4])
end
GR.selntran(1) GR.selntran(1)
GR.restorestate() GR.restorestate()
c = getColor(d[:background_color]) c = getColor(d[:background_color])
@ -145,6 +145,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
cmap = false cmap = false
axes_2d = true axes_2d = true
grid_flag = get(d, :grid, true) grid_flag = get(d, :grid, true)
outside_ticks = false
for axis = 1:2 for axis = 1:2
xmin = ymin = typemax(Float64) xmin = ymin = typemax(Float64)
@ -176,12 +177,15 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
xmin, xmax, ymin, ymax = 0, 1, 0, 1 xmin, xmax, ymin, ymax = 0, 1, 0, 1
x, y = p[:x], p[:y] x, y = p[:x], p[:y]
else else
if p[:linetype] in [:contour, :surface] if p[:linetype] in [:contour, :surface, :heatmap]
cmap = true cmap = true
end end
if p[:linetype] in [:surface, :wireframe, :path3d, :scatter3d] if p[:linetype] in [:surface, :wireframe, :path3d, :scatter3d]
axes_2d = false axes_2d = false
end end
if p[:linetype] == :heatmap
outside_ticks = true
end
x, y = p[:x], p[:y] x, y = p[:x], p[:y]
end end
if p[:linetype] != :pie if p[:linetype] != :pie
@ -267,6 +271,9 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
GR.setlinewidth(1) GR.setlinewidth(1)
GR.setlinecolorind(fg) GR.setlinecolorind(fg)
ticksize = 0.0075 * diag ticksize = 0.0075 * diag
if outside_ticks
ticksize = -ticksize
end
if grid_flag && fg == 1 if grid_flag && fg == 1
GR.grid(xtick, ytick, 0, 0, majorx, majory) GR.grid(xtick, ytick, 0, 0, majorx, majory)
end end
@ -285,14 +292,14 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
GR.savestate() GR.savestate()
GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_TOP) GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_TOP)
GR.settextcolorind(fg) GR.settextcolorind(fg)
GR.text(0.5 * (viewport[1] + viewport[2]), min(ratio, 1), d[:title]) GR.text(0.5 * (viewport[1] + viewport[2]), vp[4], d[:title])
GR.restorestate() GR.restorestate()
end end
if get(d, :xlabel, "") != "" if get(d, :xlabel, "") != ""
GR.savestate() GR.savestate()
GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_BOTTOM) GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_BOTTOM)
GR.settextcolorind(fg) GR.settextcolorind(fg)
GR.text(0.5 * (viewport[1] + viewport[2]), 0, d[:xlabel]) GR.text(0.5 * (viewport[1] + viewport[2]), vp[3], d[:xlabel])
GR.restorestate() GR.restorestate()
end end
if get(d, :ylabel, "") != "" if get(d, :ylabel, "") != ""
@ -300,7 +307,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_TOP) GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_TOP)
GR.setcharup(-1, 0) GR.setcharup(-1, 0)
GR.settextcolorind(fg) GR.settextcolorind(fg)
GR.text(0, 0.5 * (viewport[3] + viewport[4]), d[:ylabel]) GR.text(vp[1], 0.5 * (viewport[3] + viewport[4]), d[:ylabel])
GR.restorestate() GR.restorestate()
end end
if get(d, :yrightlabel, "") != "" if get(d, :yrightlabel, "") != ""
@ -308,7 +315,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_TOP) GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_TOP)
GR.setcharup(1, 0) GR.setcharup(1, 0)
GR.settextcolorind(fg) GR.settextcolorind(fg)
GR.text(1, 0.5 * (viewport[3] + viewport[4]), d[:yrightlabel]) GR.text(vp[2], 0.5 * (viewport[3] + viewport[4]), d[:yrightlabel])
GR.restorestate() GR.restorestate()
end end
@ -497,6 +504,18 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
viewport[3], viewport[4]) viewport[3], viewport[4])
GR.colormap() GR.colormap()
end end
elseif p[:linetype] == :heatmap
x, y, z = p[:x], p[:y], p[:z].surf
zmin, zmax = GR.adjustrange(minimum(z), maximum(z))
GR.setspace(zmin, zmax, 0, 90)
GR.setcolormap(GR.COLORMAP_COOLWARM)
z = reshape(z, length(x) * length(y))
GR.surface(x, y, z, GR.OPTION_CELL_ARRAY)
if cmap
GR.setviewport(viewport[2] + 0.02, viewport[2] + 0.05,
viewport[3], viewport[4])
GR.colormap()
end
elseif p[:linetype] in [:path3d, :scatter3d] elseif p[:linetype] in [:path3d, :scatter3d]
x, y, z = p[:x], p[:y], p[:z] x, y, z = p[:x], p[:y], p[:z]
zmin, zmax = GR.adjustrange(minimum(z), maximum(z)) zmin, zmax = GR.adjustrange(minimum(z), maximum(z))

View File

@ -6,26 +6,30 @@ function _initialize_backend(::PlotlyBackend; kw...)
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, '"')
############################ _js_path = Pkg.dir("Plots", "deps", "plotly-latest.min.js")
# borrowed from https://github.com/spencerlyon2/Plotlyjs.jl/blob/master/src/display.jl _js_code = open(readall, _js_path, "r")
_js_path = joinpath(Pkg.dir("Plots"), "deps", "plotly-latest.min.js")
# borrowed from https://github.com/plotly/plotly.py/blob/2594076e29584ede2d09f2aa40a8a195b3f3fc66/plotly/offline/offline.py#L64-L71 c/o @spencerlyon2
_js_script = """
<script type='text/javascript'>
define('plotly', function(require, exports, module) {
$(_js_code)
});
require(['plotly'], function(Plotly) {
window.Plotly = Plotly;
});
</script>
"""
# if we're in IJulia call setupnotebook to load js and css # if we're in IJulia call setupnotebook to load js and css
if isijulia() if isijulia()
# the first script is some hack I needed to do in order for the notebook display("text/html", _js_script)
# 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
# end borrowing (thanks :)
########################### # if isatom()
# import Atom
# Atom.@msg evaljs(_js_code)
# end
end end
# TODO: other initialization # TODO: other initialization
@ -436,15 +440,23 @@ function html_body(plt::Plot{PlotlyBackend}, style = nothing)
style = "width:$(w)px;height:$(h)px;" style = "width:$(w)px;height:$(h)px;"
end end
uuid = Base.Random.uuid4() uuid = Base.Random.uuid4()
""" html = """
<div id=\"$(uuid)\" style=\"$(style)\"></div> <div id=\"$(uuid)\" style=\"$(style)\"></div>
<script> <script>
PLOT = document.getElementById('$(uuid)'); PLOT = document.getElementById('$(uuid)');
Plotly.plot(PLOT, $(get_series_json(plt)), $(get_plot_json(plt))); Plotly.plot(PLOT, $(get_series_json(plt)), $(get_plot_json(plt)));
</script> </script>
""" """
# @show html
html
end 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}) function html_body(subplt::Subplot{PlotlyBackend})
@ -480,6 +492,7 @@ end
function Base.writemime(io::IO, ::MIME"text/html", plt::AbstractPlot{PlotlyBackend}) 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 end
function Base.display(::PlotsDisplay, plt::AbstractPlot{PlotlyBackend}) function Base.display(::PlotsDisplay, plt::AbstractPlot{PlotlyBackend})

View File

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

View File

@ -81,7 +81,7 @@ supportedArgs(::GadflyBackend) = [
supportedAxes(::GadflyBackend) = [:auto, :left] supportedAxes(::GadflyBackend) = [:auto, :left]
supportedTypes(::GadflyBackend) = [:none, :line, :path, :steppre, :steppost, :sticks, supportedTypes(::GadflyBackend) = [:none, :line, :path, :steppre, :steppost, :sticks,
:scatter, :hist2d, :hexbin, :hist, :bar, :scatter, :hist2d, :hexbin, :hist, :bar,
:hline, :vline, :contour] :hline, :vline, :contour, :shape]
supportedStyles(::GadflyBackend) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] supportedStyles(::GadflyBackend) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot]
supportedMarkers(::GadflyBackend) = vcat(_allMarkers, Shape) supportedMarkers(::GadflyBackend) = vcat(_allMarkers, Shape)
supportedScales(::GadflyBackend) = [:identity, :ln, :log2, :log10, :asinh, :sqrt] supportedScales(::GadflyBackend) = [:identity, :ln, :log2, :log10, :asinh, :sqrt]
@ -241,7 +241,7 @@ supportedArgs(::GRBackend) = [
supportedAxes(::GRBackend) = _allAxes supportedAxes(::GRBackend) = _allAxes
supportedTypes(::GRBackend) = [:none, :line, :path, :steppre, :steppost, :sticks, supportedTypes(::GRBackend) = [:none, :line, :path, :steppre, :steppost, :sticks,
:scatter, :hist2d, :hexbin, :hist, :density, :bar, :scatter, :hist2d, :hexbin, :hist, :density, :bar,
:hline, :vline, :contour, :path3d, :scatter3d, :surface, :hline, :vline, :contour, :heatmap, :path3d, :scatter3d, :surface,
:wireframe, :ohlc, :pie] :wireframe, :ohlc, :pie]
supportedStyles(::GRBackend) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] supportedStyles(::GRBackend) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot]
supportedMarkers(::GRBackend) = vcat(_allMarkers, Shape) supportedMarkers(::GRBackend) = vcat(_allMarkers, Shape)
@ -770,4 +770,3 @@ supportedStyles(::PGFPlotsBackend) = [:auto, :solid] #, :dash, :dot, :dashdot, :
supportedMarkers(::PGFPlotsBackend) = [:none, :auto, :ellipse] #, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5] #vcat(_allMarkers, Shape) supportedMarkers(::PGFPlotsBackend) = [:none, :auto, :ellipse] #, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5] #vcat(_allMarkers, Shape)
supportedScales(::PGFPlotsBackend) = [:identity] #, :log, :log2, :log10, :asinh, :sqrt] supportedScales(::PGFPlotsBackend) = [:identity] #, :log, :log2, :log10, :asinh, :sqrt]
subplotSupported(::PGFPlotsBackend) = false subplotSupported(::PGFPlotsBackend) = false

View File

@ -3,6 +3,9 @@ immutable Shape
vertices::AVec vertices::AVec
end end
get_xs(shape::Shape) = Float64[v[1] for v in shape.vertices]
get_ys(shape::Shape) = Float64[v[2] for v in shape.vertices]
"get an array of tuples of points on a circle with radius `r`" "get an array of tuples of points on a circle with radius `r`"
function partialcircle(start_θ, end_θ, n = 20, r=1) function partialcircle(start_θ, end_θ, n = 20, r=1)
@compat(Tuple{Float64,Float64})[(r*cos(u),r*sin(u)) for u in linspace(start_θ, end_θ, n)] @compat(Tuple{Float64,Float64})[(r*cos(u),r*sin(u)) for u in linspace(start_θ, end_θ, n)]

View File

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

View File

@ -444,6 +444,37 @@ function createKWargsList{T<:Real}(plt::AbstractPlot, x::AMat{T}, y::AMat{T}, zm
createKWargsList(plt, Any[x], Any[y]; d...) #kw..., z = surf, linetype = :contour) createKWargsList(plt, Any[x], Any[y]; d...) #kw..., z = surf, linetype = :contour)
end end
# plotting arbitrary shapes/polygons
function createKWargsList(plt::AbstractPlot, shape::Shape; kw...)
x, y = unzip(shape.vertices)
createKWargsList(plt, x, y; linetype = :shape, kw...)
end
function shape_coords(shapes::AVec{Shape})
xs = map(get_xs, shapes)
ys = map(get_ys, shapes)
x, y = unzip(shapes[1].vertices)
for shape in shapes[2:end]
tmpx, tmpy = unzip(shape.vertices)
x = vcat(x, NaN, tmpx)
y = vcat(y, NaN, tmpy)
end
x, y
end
function createKWargsList(plt::AbstractPlot, shapes::AVec{Shape}; kw...)
x, y = shape_coords(shapes)
createKWargsList(plt, x, y; linetype = :shape, kw...)
end
function createKWargsList(plt::AbstractPlot, shapes::AMat{Shape}; kw...)
x, y = [], []
for j in 1:size(shapes, 2)
tmpx, tmpy = shape_coords(vec(shapes[:,j]))
push!(x, tmpx)
push!(y, tmpy)
end
createKWargsList(plt, x, y; linetype = :shape, kw...)
end
function createKWargsList(plt::AbstractPlot, surf::Surface; kw...) function createKWargsList(plt::AbstractPlot, surf::Surface; kw...)
createKWargsList(plt, 1:size(surf.surf,1), 1:size(surf.surf,2), convert(Matrix{Float64}, surf.surf); kw...) createKWargsList(plt, 1:size(surf.surf,1), 1:size(surf.surf,2), convert(Matrix{Float64}, surf.surf); kw...)

View File

@ -137,3 +137,92 @@ function abline!(plt::Plot, a, b; kw...)
end end
abline!(args...; kw...) = abline!(current(), args...; kw...) abline!(args...; kw...) = abline!(current(), args...; kw...)
# -------------------------------------------------
# Arc Diagram
curvecolor(value, min, max, grad) = getColorZ(grad, (value-min)/(max-min))
"Plots a clockwise arc, from source to destiny, colored by weight"
function arc!(source, destiny, weight, min, max, grad)
radius = (destiny - source) / 2
arc = Plots.partialcircle(0, π, 30, radius)
x, y = Plots.unzip(arc)
plot!(x .+ radius .+ source, y, line = (curvecolor(weight, min, max, grad), 0.5, 2))
end
"""
`arcdiagram(source, destiny, weight[, grad])`
Plots an arc diagram, form `source` to `destiny` (clockwise), using `weight` to determine the colors.
"""
function arcdiagram(source, destiny, weight, grad=ColorGradient(:bluesreds))
if length(source) == length(destiny) == length(weight)
vertices = vcat(source, destiny)
xmin, xmax = extrema(vertices)
plot(xlim=(xmin - 0.5, xmax + 0.5))
wmin,wmax = extrema(weight)
for (i, j, value) in zip(source,destiny,weight)
arc!(i, j, value, wmin, wmax, grad)
end
scatter!(vertices, zeros(length(vertices)), leg=false)
else
throw(ArgumentError("source, destiny and weight should have the same length"))
end
end
"""
`arcdiagram(mat[, grad])`
Plots an arc diagram of a matrix, form rows to columns (clockwise),
using the values on the matrix as weights to determine the colors.
Doesn't show edges with value zero if the input is sparse.
For simmetric matrices, only the upper triangular values are used.
"""
function arcdiagram{T}(mat::AbstractArray{T,2}, grad=ColorGradient(:bluesreds))
nrow, ncol = size(mat) # rows are sources and columns are destinies
nosymmetric = !issym(mat) # plots only triu for symmetric matrices
nosparse = !issparse(mat) # doesn't plot zeros from a sparse matrix
L = length(mat)
source = Array(Int, L)
destiny = Array(Int, L)
weight = Array(T, L)
idx = 1
for i in 1:nrow, j in 1:ncol
value = mat[i, j]
if !isnan(value) && ( nosparse || value != zero(T) ) # TODO: deal with Nullable
if i < j
source[idx] = i
destiny[idx] = j
weight[idx] = value
idx += 1
elseif nosymmetric && (i > j)
source[idx] = i
destiny[idx] = j
weight[idx] = value
idx += 1
end
end
end
resize!(source, idx-1)
resize!(destiny, idx-1)
resize!(weight, idx-1)
arcdiagram(source, destiny, weight, grad)
end

View File

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