changed :log to map to :log10, switched :log to :ln; better support for plotly axis attributes, ticks, scale, etc; fix plotly bg/fg colors

This commit is contained in:
Thomas Breloff 2015-12-17 14:59:50 -05:00
parent 002603c388
commit fe0f22dfc0
7 changed files with 152 additions and 66 deletions

View File

@ -94,10 +94,10 @@ const _allMarkers = vcat(:none, :auto, sort(collect(keys(_shapes))))
:spike => :vline,
)
const _allScales = [:identity, :log, :log2, :log10, :asinh, :sqrt]
const _allScales = [:identity, :ln, :log2, :log10, :asinh, :sqrt]
@compat const _scaleAliases = Dict(
:none => :identity,
:ln => :log,
:log => :log10,
)
# -----------------------------------------------------------------------------

View File

@ -68,8 +68,8 @@ function _create_plot(pkg::BokehPackage; kw...)
filename = tempname() * ".html"
title = d[:title]
w, h = d[:size]
xaxis_type = d[:xscale] == :log ? :log : :auto
yaxis_type = d[:yscale] == :log ? :log : :auto
xaxis_type = d[:xscale] == :log10 ? :log : :auto
yaxis_type = d[:yscale] == :log10 ? :log : :auto
# legend = d[:legend] ? xxxx : nothing
legend = nothing
extra_args = Dict() # TODO: we'll put extra settings (xlim, etc) here

View File

@ -375,7 +375,7 @@ function getGadflyScaleFunction(d::Dict, isx::Bool)
hasScaleKey = haskey(d, scalekey)
if hasScaleKey
scale = d[scalekey]
scale == :log && return isx ? Gadfly.Scale.x_log : Gadfly.Scale.y_log, hasScaleKey
scale == :ln && return isx ? Gadfly.Scale.x_log : Gadfly.Scale.y_log, hasScaleKey
scale == :log2 && return isx ? Gadfly.Scale.x_log2 : Gadfly.Scale.y_log2, hasScaleKey
scale == :log10 && return isx ? Gadfly.Scale.x_log10 : Gadfly.Scale.y_log10, hasScaleKey
scale == :asinh && return isx ? Gadfly.Scale.x_asinh : Gadfly.Scale.y_asinh, hasScaleKey

View File

@ -82,11 +82,11 @@ end
# _plotDefaults[:xflip] = false
# _plotDefaults[:yflip] = false
function plotlyfont(font::Font)
function plotlyfont(font::Font, color = font.color)
Dict(
:family => font.family,
:size => round(Int, font.pointsize*1.4),
:color => webcolor(font.color),
:color => webcolor(color),
)
end
@ -111,7 +111,7 @@ function get_annotation_dict(x, y, ptxt::PlotText)
end
function plotlyscale(scale::Symbol)
if scale == :log
if scale == :log10
"log"
else
"-"
@ -120,6 +120,62 @@ end
use_axis_field(ticks) = !(ticks in (nothing, :none))
tickssym(isx::Bool) = symbol((isx ? "x" : "y") * "ticks")
limssym(isx::Bool) = symbol((isx ? "x" : "y") * "lims")
flipsym(isx::Bool) = symbol((isx ? "x" : "y") * "flip")
scalesym(isx::Bool) = symbol((isx ? "x" : "y") * "scale")
labelsym(isx::Bool) = symbol((isx ? "x" : "y") * "label")
function plotlyaxis(d::Dict, isx::Bool)
ax = Dict(
:title => d[labelsym(isx)],
:showgrid => d[:grid],
:zeroline => false,
)
fgcolor = webcolor(d[:foreground_color])
tsym = tickssym(isx)
if use_axis_field(d[tsym])
ax[:titlefont] = plotlyfont(d[:guidefont], fgcolor)
ax[:type] = plotlyscale(d[scalesym(isx)])
ax[:tickfont] = plotlyfont(d[:tickfont], fgcolor)
ax[:tickcolor] = fgcolor
ax[:linecolor] = fgcolor
# xlims
lims = d[limssym(isx)]
if lims != :auto && limsType(lims) == :limits
ax[:range] = lims
end
# xflip
if d[flipsym(isx)]
ax[:autorange] = "reversed"
end
# xticks
ticks = d[tsym]
if ticks != :auto
ttype = ticksType(ticks)
if ttype == :ticks
ax[:tickmode] = "array"
ax[:tickvals] = ticks
elseif ttype == :ticks_and_labels
ax[:tickmode] = "array"
ax[:tickvals], ax[:ticktext] = ticks
end
end
ax
else
ax[:showticklabels] = false
ax[:showgrid] = false
end
ax
end
function get_plot_json(plt::Plot{PlotlyPackage})
d = plt.plotargs
d_out = Dict()
@ -129,64 +185,92 @@ function get_plot_json(plt::Plot{PlotlyPackage})
# set the fields for the plot
d_out[:title] = d[:title]
d_out[:titlefont] = plotlyfont(d[:guidefont])
d_out[:titlefont] = plotlyfont(d[:guidefont], fgcolor)
d_out[:margin] = Dict(: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
# TODO: x/y axis range
d_out[:xaxis] = plotlyaxis(d, true)
d_out[:yaxis] = plotlyaxis(d, false)
# x-axis
d_out[:xaxis] = Dict(
:title => d[:xlabel],
:showgrid => d[:grid],
:zeroline => false,
)
merge!(d_out[:xaxis], if use_axis_field(d[:xticks])
Dict(
:titlefont => plotlyfont(d[:guidefont]),
:type => plotlyscale(d[:xscale]),
:tickfont => plotlyfont(d[:tickfont]),
:tickcolor => fgcolor,
:linecolor => fgcolor,
)
else
Dict(
:showticklabels => false,
:showgrid => false,
)
end)
# # x-axis
# d_out[:xaxis] = Dict(
# :title => d[:xlabel],
# :showgrid => d[:grid],
# :zeroline => false,
# )
# merge!(d_out[:xaxis], if use_axis_field(d[:xticks])
# ax = Dict(
# :titlefont => plotlyfont(d[:guidefont]),
# :type => plotlyscale(d[:xscale]),
# :tickfont => plotlyfont(d[:tickfont]),
# :tickcolor => fgcolor,
# :linecolor => fgcolor,
# )
lims = d[:xlims]
if lims != :auto && limsType(lims) == :limits
d_out[:xaxis][:range] = lims
end
# # xlims
# lims = d[:xlims]
# if lims != :auto && limsType(lims) == :limits
# ax[:range] = lims
# end
# y-axis
d_out[:yaxis] = Dict(
:title => d[:ylabel],
:showgrid => d[:grid],
:zeroline => false,
)
merge!(d_out[:yaxis], if use_axis_field(d[:yticks])
Dict(
:titlefont => plotlyfont(d[:guidefont]),
:type => plotlyscale(d[:yscale]),
:tickfont => plotlyfont(d[:tickfont]),
:tickcolor => fgcolor,
:linecolor => fgcolor,
)
else
Dict(
:showticklabels => false,
:showgrid => false,
)
end)
# # xflip
# if d[:xflip]
# ax[:autorange] = "reversed"
# end
lims = d[:ylims]
if lims != :auto && limsType(lims) == :limits
d_out[:yaxis][:range] = lims
end
# # xticks
# ticks = d[:xticks]
# if ticks != :auto
# ttype = ticksType(ticks)
# if ttype == :ticks
# ax[:tickmode] = "array"
# ax[:tickvals] = ticks
# elseif ttype == :ticks_and_labels
# ax[:tickmode] = "array"
# ax[:tickvals], ax[:ticktext] = ticks
# end
# end
# ax
# else
# Dict(
# :showticklabels => false,
# :showgrid => false,
# )
# end)
# # y-axis
# d_out[:yaxis] = Dict(
# :title => d[:ylabel],
# :showgrid => d[:grid],
# :zeroline => false,
# )
# merge!(d_out[:yaxis], if use_axis_field(d[:yticks])
# Dict(
# :titlefont => plotlyfont(d[:guidefont]),
# :type => plotlyscale(d[:yscale]),
# :tickfont => plotlyfont(d[:tickfont]),
# :tickcolor => fgcolor,
# :linecolor => fgcolor,
# )
# else
# Dict(
# :showticklabels => false,
# :showgrid => false,
# )
# end)
# lims = d[:ylims]
# if lims != :auto && limsType(lims) == :limits
# d_out[:yaxis][:range] = lims
# end
# if d[:yflip]
# d_out[:yaxis][:autorange] = "reversed"
# end
# legend
d_out[:showlegend] = d[:legend]
@ -304,7 +388,8 @@ function get_series_json(d::Dict; plot_index = nothing)
d_out[:z] = collect(d[:z])
else
error("Plotly: linetype $lt isn't supported.")
warn("Plotly: linetype $lt isn't supported.")
return Dict()
end
# add "marker"

View File

@ -523,7 +523,7 @@ end
function applyPyPlotScale(ax, scaleType::Symbol, isx::Bool)
func = ax[isx ? :set_xscale : :set_yscale]
scaleType == :identity && return func("linear")
scaleType == :log && return func("log", basex = e, basey = e)
scaleType == :ln && return func("log", basex = e, basey = e)
scaleType == :log2 && return func("log", basex = 2, basey = 2)
scaleType == :log10 && return func("log", basex = 10, basey = 10)
warn("Unhandled scaleType: ", scaleType)

View File

@ -82,7 +82,7 @@ supportedTypes(::GadflyPackage) = [:none, :line, :path, :steppre, :steppost, :st
:hline, :vline, :contour]
supportedStyles(::GadflyPackage) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot]
supportedMarkers(::GadflyPackage) = vcat(_allMarkers, Shape)
supportedScales(::GadflyPackage) = [:identity, :log, :log2, :log10, :asinh, :sqrt]
supportedScales(::GadflyPackage) = [:identity, :ln, :log2, :log10, :asinh, :sqrt]
subplotSupported(::GadflyPackage) = true
@ -166,7 +166,7 @@ supportedTypes(::PyPlotPackage) = [:none, :line, :path, :steppre, :steppost, :st
supportedStyles(::PyPlotPackage) = [:auto, :solid, :dash, :dot, :dashdot]
# supportedMarkers(::PyPlotPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5, :hexagon]
supportedMarkers(::PyPlotPackage) = vcat(_allMarkers, Shape)
supportedScales(::PyPlotPackage) = [:identity, :log, :log2, :log10]
supportedScales(::PyPlotPackage) = [:identity, :ln, :log2, :log10]
subplotSupported(::PyPlotPackage) = true
@ -419,7 +419,7 @@ supportedAxes(::BokehPackage) = [:auto, :left]
supportedTypes(::BokehPackage) = [:none, :path, :scatter] #,:steppre, :steppost, :sticks, :heatmap, :hexbin, :hist, :bar, :hline, :vline, :contour]
supportedStyles(::BokehPackage) = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot]
supportedMarkers(::BokehPackage) = [:none, :auto, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5] #vcat(_allMarkers, Shape)
supportedScales(::BokehPackage) = [:identity, :log] #, :log, :log2, :log10, :asinh, :sqrt]
supportedScales(::BokehPackage) = [:identity, :ln] #, :ln, :log2, :log10, :asinh, :sqrt]
subplotSupported(::BokehPackage) = false
@ -471,8 +471,8 @@ supportedArgs(::PlotlyPackage) = [
:yticks,
:xscale,
:yscale,
# :xflip,
# :yflip,
:xflip,
:yflip,
:z,
:zcolor,
:tickfont,
@ -488,7 +488,7 @@ supportedTypes(::PlotlyPackage) = [:none, :line, :path, :scatter, :steppre, :ste
supportedStyles(::PlotlyPackage) = [:auto, :solid, :dash, :dot, :dashdot]
supportedMarkers(::PlotlyPackage) = [:none, :auto, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross,
:pentagon, :hexagon, :octagon, :vline, :hline] #vcat(_allMarkers, Shape)
supportedScales(::PlotlyPackage) = [:identity, :log] #, :log, :log2, :log10, :asinh, :sqrt]
supportedScales(::PlotlyPackage) = [:identity, :log10] #, :ln, :log2, :log10, :asinh, :sqrt]
subplotSupported(::PlotlyPackage) = true
stringsSupported(::PlotlyPackage) = true

View File

@ -335,6 +335,7 @@ 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))
webcolor(c) = webcolor(convertColor(c))
webcolor(c, α) = webcolor(convertColor(getColor(c), α))
# ----------------------------------------------------------------------------------