plotlyjs fixes

This commit is contained in:
Thomas Breloff 2016-05-24 12:38:52 -04:00
parent cf635cbdd9
commit 851fe9f72c
3 changed files with 54 additions and 44 deletions

View File

@ -59,6 +59,7 @@ is3d(d::KW) = trueOrAllTrue(is3d, d[:seriestype])
is3d(sp::Subplot) = string(sp.attr[:projection]) == "3d" is3d(sp::Subplot) = string(sp.attr[:projection]) == "3d"
ispolar(sp::Subplot) = string(sp.attr[:projection]) == "polar" ispolar(sp::Subplot) = string(sp.attr[:projection]) == "polar"
ispolar(series::Series) = ispolar(series.d[:subplot])
const _allStyles = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot] const _allStyles = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot]
@compat const _styleAliases = KW( @compat const _styleAliases = KW(

View File

@ -218,7 +218,7 @@ use_axis_field(ticks) = !(ticks in (nothing, :none))
# labelsym(letter) = symbol(letter * "label") # labelsym(letter) = symbol(letter * "label")
# rotationsym(letter) = symbol(letter * "rotation") # rotationsym(letter) = symbol(letter * "rotation")
function plotlyaxis(axis::Axis, sp::Subplot{PlotlyBackend}) function plotly_axis(axis::Axis, sp::Subplot)
letter = axis[:letter] letter = axis[:letter]
d = axis.d d = axis.d
ax = KW( ax = KW(
@ -273,10 +273,10 @@ function plotlyaxis(axis::Axis, sp::Subplot{PlotlyBackend})
ax ax
end end
# function get_plot_json(plt::Plot{PlotlyBackend}) # function plotly_layout_json(plt::Plot{PlotlyBackend})
# d = plt.attr # d = plt.attr
# function plotly_layout(d::KW, seriesargs::AVec{KW}) # function plotly_layout(d::KW, seriesargs::AVec{KW})
function plotly_layout(plt::Plot{PlotlyBackend}) function plotly_layout(plt::Plot)
d_out = KW() d_out = KW()
# for now, we only support 1 subplot # for now, we only support 1 subplot
@ -302,13 +302,13 @@ function plotly_layout(plt::Plot{PlotlyBackend})
# if any(is3d, seriesargs) # if any(is3d, seriesargs)
if is3d(sp) if is3d(sp)
d_out[:scene] = KW( d_out[:scene] = KW(
:xaxis => plotlyaxis(sp.attr[:xaxis], sp), :xaxis => plotly_axis(sp.attr[:xaxis], sp),
:yaxis => plotlyaxis(sp.attr[:yaxis], sp), :yaxis => plotly_axis(sp.attr[:yaxis], sp),
:xzxis => plotlyaxis(sp.attr[:zaxis], sp), :xzxis => plotly_axis(sp.attr[:zaxis], sp),
) )
else else
d_out[:xaxis] = plotlyaxis(sp.attr[:xaxis], sp) d_out[:xaxis] = plotly_axis(sp.attr[:xaxis], sp)
d_out[:yaxis] = plotlyaxis(sp.attr[:yaxis], sp) d_out[:yaxis] = plotly_axis(sp.attr[:yaxis], sp)
end end
# legend # legend
@ -354,7 +354,7 @@ function plotly_layout(plt::Plot{PlotlyBackend})
d_out d_out
end end
function get_plot_json(plt::Plot{PlotlyBackend}) function plotly_layout_json(plt::Plot)
JSON.json(plotly_layout(plt)) JSON.json(plotly_layout(plt))
end end
@ -375,7 +375,7 @@ const _plotly_markers = KW(
) )
# get a dictionary representing the series params (d is the Plots-dict, d_out is the Plotly-dict) # get a dictionary representing the series params (d is the Plots-dict, d_out is the Plotly-dict)
function plotly_series(plt::Plot{PlotlyBackend}, series::Series) function plotly_series(plt::Plot, series::Series)
d = series.d d = series.d
d_out = KW() d_out = KW()
@ -520,7 +520,7 @@ function plotly_series(plt::Plot{PlotlyBackend}, series::Series)
end end
# get a list of dictionaries, each representing the series params # get a list of dictionaries, each representing the series params
function get_series_json(plt::Plot{PlotlyBackend}) function plotly_series_json(plt::Plot)
JSON.json(map(series -> plotly_series(plt, series), plt.series_list)) JSON.json(map(series -> plotly_series(plt, series), plt.series_list))
end end
@ -540,7 +540,7 @@ function html_body(plt::Plot{PlotlyBackend}, style = nothing)
<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, $(plotly_series_json(plt)), $(plotly_layout_json(plt)));
</script> </script>
""" """
html html
@ -549,7 +549,7 @@ end
function js_body(plt::Plot{PlotlyBackend}, uuid) function js_body(plt::Plot{PlotlyBackend}, uuid)
js = """ js = """
PLOT = document.getElementById('$(uuid)'); PLOT = document.getElementById('$(uuid)');
Plotly.plot(PLOT, $(get_series_json(plt)), $(get_plot_json(plt))); Plotly.plot(PLOT, $(plotly_series_json(plt)), $(plotly_layout_json(plt)));
""" """
end end

View File

@ -114,11 +114,11 @@ end
function _series_added(plt::Plot{PlotlyJSBackend}, series::Series) function _series_added(plt::Plot{PlotlyJSBackend}, series::Series)
d = series.d # d = series.d
syncplot = plt.o syncplot = plt.o
# add to the data array # add to the data array
pdict = plotly_series(d, plt.attr) pdict = plotly_series(plt, series)
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)
@ -131,30 +131,30 @@ end
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
function _add_annotations{X,Y,V}(plt::Plot{PlotlyJSBackend}, anns::AVec{@compat(Tuple{X,Y,V})}) # function _add_annotations{X,Y,V}(plt::Plot{PlotlyJSBackend}, anns::AVec{@compat(Tuple{X,Y,V})})
# set or add to the annotation_list # # set or add to the annotation_list
if !haskey(plt.attr, :annotation_list) # if !haskey(plt.attr, :annotation_list)
plt.attr[:annotation_list] = Any[] # plt.attr[:annotation_list] = Any[]
end # end
append!(plt.attr[:annotation_list], anns) # append!(plt.attr[:annotation_list], anns)
end # end
# ---------------------------------------------------------------- # ----------------------------------------------------------------
function _before_update_plot(plt::Plot{PlotlyJSBackend}) # function _before_update_plot(plt::Plot{PlotlyJSBackend})
end # end
# TODO: override this to update plot items (title, xlabel, etc) after creation # TODO: override this to update plot items (title, xlabel, etc) after creation
function _update_plot(plt::Plot{PlotlyJSBackend}, d::KW) function _update_plot(plt::Plot{PlotlyJSBackend}, d::KW)
pdict = plotly_layout(plt.attr, plt.seriesargs) pdict = plotly_layout(plt)
syncplot = plt.o syncplot = plt.o
w,h = d[:size] w,h = plt.attr[:size]
PlotlyJS.relayout!(syncplot, pdict, width = w, height = h) PlotlyJS.relayout!(syncplot, pdict, width = w, height = h)
end end
function _update_plot_pos_size(plt::AbstractPlot{PlotlyJSBackend}, d::KW) # function _update_plot_pos_size(plt::AbstractPlot{PlotlyJSBackend}, d::KW)
end # end
# ---------------------------------------------------------------- # ----------------------------------------------------------------
@ -165,17 +165,26 @@ end
# d[:x], d[:y] # d[:x], d[:y]
# end # end
function setxy!{X,Y}(plt::Plot{PlotlyJSBackend}, xy::Tuple{X,Y}, i::Integer) function _series_updated(plt::Plot{PlotlyJSBackend}, series::Series)
d = plt.seriesargs[i] xsym, ysym = (ispolar(series) ? (:t,:r) : (:x,:y))
ispolar = get(plt.attr, :polar, false) PlotlyJS.restyle!(
xsym = ispolar ? :t : :x plt.o,
ysym = ispolar ? :r : :y findfirst(plt.series_list, series),
d[xsym], d[ysym] = xy KW(xsym => series.d[:x], ysym => series.d[:y])
# TODO: this is likely ineffecient... we should make a call that ONLY changes the plot data )
PlotlyJS.restyle!(plt.o, i, KW(xsym=>(d[xsym],), ysym=>(d[ysym],)))
plt
end end
# function setxy!{X,Y}(plt::Plot{PlotlyJSBackend}, xy::Tuple{X,Y}, i::Integer)
# d = plt.seriesargs[i]
# ispolar = get(plt.attr, :polar, false)
# xsym = ispolar ? :t : :x
# ysym = ispolar ? :r : :y
# d[xsym], d[ysym] = xy
# # TODO: this is likely ineffecient... we should make a call that ONLY changes the plot data
# PlotlyJS.restyle!(plt.o, i, KW(xsym=>(d[xsym],), ysym=>(d[ysym],)))
# plt
# end
# ---------------------------------------------------------------- # ----------------------------------------------------------------
# function _create_subplot(subplt::Subplot{PlotlyJSBackend}, isbefore::Bool) # function _create_subplot(subplt::Subplot{PlotlyJSBackend}, isbefore::Bool)
@ -183,13 +192,13 @@ end
# true # true
# end # end
function _expand_limits(lims, plt::Plot{PlotlyJSBackend}, isx::Bool) # function _expand_limits(lims, plt::Plot{PlotlyJSBackend}, isx::Bool)
# TODO: call expand limits for each plot data # # TODO: call expand limits for each plot data
end # end
#
function _remove_axis(plt::Plot{PlotlyJSBackend}, isx::Bool) # function _remove_axis(plt::Plot{PlotlyJSBackend}, isx::Bool)
# TODO: if plot is inner subplot, might need to remove ticks or axis labels # # TODO: if plot is inner subplot, might need to remove ticks or axis labels
end # end
# ---------------------------------------------------------------- # ----------------------------------------------------------------