working on plotly; added contour and pie examples

This commit is contained in:
Thomas Breloff 2015-11-23 17:46:30 -05:00
parent 4b1f127eaf
commit f0e728c316
10 changed files with 81 additions and 24 deletions

View File

@ -167,6 +167,23 @@ const examples = PlotExample[
m=(Shape(verts),30,RGBA(0,0,0,0.2)),
bg=:pink, fg=:darkblue,
xlim = (0,1), ylim=(0,1), leg=false))
]),
PlotExample("Contours",
"",
[
:(x = 1:0.3:20),
:(y = x),
:(f(x,y) = sin(x)+cos(y)),
:(contour(x, y, f, fill=true))
]),
PlotExample("Pie",
"",
[
:(x = ["Nerds", "Hackers", "Scientists"]),
:(y = [0.4, 0.35, 0.25]),
:(pie(x, y, title="The Julia Community", l=0.5))
])
]

View File

@ -49,6 +49,12 @@ export
vline!,
ohlc,
ohlc!,
pie,
pie!,
contour,
contour!,
surface,
surface!,
title!,
xlabel!,
@ -152,6 +158,12 @@ vline(args...; kw...) = plot(args...; kw..., linetype = :vline)
vline!(args...; kw...) = plot!(args...; kw..., linetype = :vline)
ohlc(args...; kw...) = plot(args...; kw..., linetype = :ohlc)
ohlc!(args...; kw...) = plot!(args...; kw..., linetype = :ohlc)
pie(args...; kw...) = plot(args...; kw..., linetype = :pie)
pie!(args...; kw...) = plot!(args...; kw..., linetype = :pie)
contour(args...; kw...) = plot(args...; kw..., linetype = :contour)
contour!(args...; kw...) = plot!(args...; kw..., linetype = :contour)
surface(args...; kw...) = plot(args...; kw..., linetype = :surface)
surface!(args...; kw...) = plot!(args...; kw..., linetype = :surface)
title!(s::@compat(AbstractString); kw...) = plot!(; title = s, kw...)

View File

@ -10,7 +10,8 @@ const _allAxes = [:auto, :left, :right]
const _3dTypes = [:path3d, :scatter3d]
const _allTypes = vcat([
:none, :line, :path, :steppre, :steppost, :sticks, :scatter,
:heatmap, :hexbin, :hist, :density, :bar, :hline, :vline, :ohlc, :contour
:heatmap, :hexbin, :hist, :density, :bar, :hline, :vline, :ohlc,
:contour, :pie
], _3dTypes)
@compat const _typeAliases = Dict(
:n => :none,

View File

@ -82,7 +82,7 @@ function plotlyfont(font::Font)
Dict(
:family => font.family,
:size => font.pointsize,
:color => font.color,
:color => webcolor(font.color),
)
end
@ -105,7 +105,8 @@ function get_plot_html(plt::Plot{PlotlyPackage})
d_out[:title] = d[:title]
d_out[:titlefont] = plotlyfont(d[:guidefont])
d_out[:width], d_out[:height] = d[:size]
d_out[:margin] = Dict(:l=>20, :b=>20, :r=>10, :t=>10)
# d_out[:margin] = Dict(:l=>20, :b=>20, :r=>10, :t=>10)
d_out[:margin] = Dict(:t=>20)
d_out[:paper_bgcolor] = bgcolor
d_out[:plot_bgcolor] = bgcolor
@ -179,6 +180,11 @@ end
# :scatter, :heatmap, :hexbin, :hist, :density, :bar,
# :hline, :vline, :contour, :path3d, :scatter3d]
function plotly_colorscale(grad::ColorGradient)
[[grad.values[i], webcolor(grad.colors[i])] for i in 1:length(grad.colors)]
end
plotly_colorscale(c) = plotly_colorscale(ColorGradient(:bluesreds))
# get a dictionary representing the series params (d is the Plots-dict, d_out is the Plotly-dict)
function get_series_html(d::Dict)
d_out = Dict()
@ -219,6 +225,12 @@ function get_series_html(d::Dict)
d_out[:ncontours] = d[:nlevels]
d_out[:contours] = Dict(:coloring => d[:fillrange] != nothing ? "fill" : "lines")
# TODO: colorscale: [[0, 'rgb(166,206,227)'], [0.25, 'rgb(31,120,180)'], [0.45, 'rgb(178,223,138)'], [0.65, 'rgb(51,160,44)'], [0.85, 'rgb(251,154,153)'], [1, 'rgb(227,26,28)']]
d_out[:colorscale] = plotly_colorscale(d[:linecolor])
elseif lt == :pie
d_out[:type] = "pie"
d_out[:labels] = x
d_out[:values] = y
d_out[:hoverinfo] = "label+percent+name"
else
error("Plotly: linetype $lt isn't supported.")
end

View File

@ -376,29 +376,30 @@ function Base.getindex(plt::Plot{PyPlotPackage}, i::Integer)
# mapping[:x], mapping[:y]
end
function Base.setindex!(plt::Plot{PyPlotPackage}, xy::Tuple, i::Integer)
function Base.setindex!{X,Y}(plt::Plot{PyPlotPackage}, xy::Tuple{X,Y}, i::Integer)
series = plt.seriesargs[i][:serieshandle]
x, y = xy
try
series[:set_data](xy...)
series[:set_data](x, y)
catch
series[:set_offsets](hcat(xy...))
series[:set_offsets](hcat(x, y))
end
ax = series[:axes]
if plt.plotargs[:xlims] == :auto
xmin, xmax = ax[:get_xlim]()
ax[:set_xlim](min(xmin, minimum(xy[1])), max(xmax, maximum(xy[1])))
ax[:set_xlim](min(xmin, minimum(x)), max(xmax, maximum(x)))
end
if plt.plotargs[:ylims] == :auto
ymin, ymax = ax[:get_ylim]()
ax[:set_ylim](min(ymin, minimum(xy[2])), max(ymax, maximum(xy[2])))
ax[:set_ylim](min(ymin, minimum(y)), max(ymax, maximum(y)))
end
# getLeftAxis(plt)[:relim]()
# getRightAxis(plt)[:relim]()
# for mapping in getGadflyMappings(plt, i)
# mapping[:x], mapping[:y] = xy
# end
plt
end
function Base.setindex!{X,Y,Z}(plt::Plot{PyPlotPackage}, xyz::Tuple{X,Y,Z}, i::Integer)
warn("setindex not implemented for xyz")
plt
end

View File

@ -5,6 +5,7 @@ supportedStyles(::PlottingPackage) = [:solid]
supportedMarkers(::PlottingPackage) = [:none]
supportedScales(::PlottingPackage) = [:identity]
subplotSupported(::PlottingPackage) = false
stringsSupported(::PlottingPackage) = false
supportedAxes() = supportedAxes(backend())
supportedTypes() = supportedTypes(backend())
@ -12,6 +13,7 @@ supportedStyles() = supportedStyles(backend())
supportedMarkers() = supportedMarkers(backend())
supportedScales() = supportedScales(backend())
subplotSupported() = subplotSupported(backend())
stringsSupported() = stringsSupported(backend())
# --------------------------------------------------------------------------------------
@ -428,7 +430,7 @@ supportedArgs(::PlotlyPackage) = [
# :axis,
:background_color,
:color_palette,
# :fillrange,
:fillrange,
# :fillcolor,
# :fillalpha,
:foreground_color,
@ -471,19 +473,18 @@ supportedArgs(::PlotlyPackage) = [
:yscale,
# :xflip,
# :yflip,
# :z,
:z,
:tickfont,
:guidefont,
:legendfont,
:grid,
# :surface,
# :nlevels,
:nlevels,
]
supportedAxes(::PlotlyPackage) = [:auto, :left]
supportedTypes(::PlotlyPackage) = [:none, :path, :scatter] #,:steppre, :steppost, :sticks, :heatmap, :hexbin, :hist, :bar, :hline, :vline, :contour]
supportedTypes(::PlotlyPackage) = [:none, :path, :scatter, :heatmap, :hist, :bar, :contour, :pie] #,:steppre, :steppost, :sticks, :heatmap, :hexbin, :hist, :bar, :hline, :vline, :contour]
supportedStyles(::PlotlyPackage) = [:auto, :solid] #, :dash, :dot, :dashdot, :dashdotdot]
supportedMarkers(::PlotlyPackage) = [:none, :auto, :ellipse] #, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5] #vcat(_allMarkers, Shape)
supportedScales(::PlotlyPackage) = [:identity, :log] #, :log, :log2, :log10, :asinh, :sqrt]
subplotSupported(::PlotlyPackage) = false
stringsSupported(::PlotlyPackage) = true

View File

@ -28,7 +28,7 @@ end
function standalone_html_window(plt::PlottingObject; kw...)
html = standalone_html(plt; kw...)
println(html)
# println(html)
filename = string(tempname(), ".html")
output = open(filename, "w")
write(output, html)

View File

@ -328,7 +328,10 @@ end
make255(x) = round(Int, 255 * x)
function webcolor(c::Colorant)
function webcolor(c::Color)
@sprintf("rgb(%d, %d, %d)", [make255(f(c)) for f in [red,green,blue]]...)
end
function webcolor(c::TransparentColor)
@sprintf("rgba(%d, %d, %d, %1.3f)", [make255(f(c)) for f in [red,green,blue]]..., alpha(c))
end
webcolor(cs::ColorScheme) = webcolor(getColor(cs))

View File

@ -101,8 +101,10 @@ function plot!(plt::Plot, args...; kw...)
for (i,di) in enumerate(seriesArgList)
plt.n += 1
setTicksFromStringVector(d, di, :x, :xticks)
setTicksFromStringVector(d, di, :y, :yticks)
if !stringsSupported()
setTicksFromStringVector(d, di, :x, :xticks)
setTicksFromStringVector(d, di, :y, :yticks)
end
# remove plot args
for k in keys(_plotDefaults)
@ -238,6 +240,9 @@ convertToAnyVector(s::Surface; kw...) = Any[s], nothing
# vector of OHLC
convertToAnyVector(v::AVec{OHLC}; kw...) = Any[v], nothing
# dates
convertToAnyVector{D<:Union{Date,DateTime}}(dts::AVec{D}; kw...) = Any[dts], nothing
# list of things (maybe other vectors, functions, or something else)
function convertToAnyVector(v::AVec; kw...)
if all(x -> typeof(x) <: Real, v)

View File

@ -240,7 +240,12 @@ function backend()
try
# @eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "web.jl"))
# @eval include(joinpath(Pkg.dir("Plots"), "src", "backends", "plotly.jl"))
@eval import JSON
@eval begin
import JSON
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)
end
catch err
warn("Couldn't setup Plotly")
rethrow(err)