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"
ispolar(sp::Subplot) = string(sp.attr[:projection]) == "polar"
ispolar(series::Series) = ispolar(series.d[:subplot])
const _allStyles = [:auto, :solid, :dash, :dot, :dashdot, :dashdotdot]
@compat const _styleAliases = KW(

View File

@ -218,7 +218,7 @@ use_axis_field(ticks) = !(ticks in (nothing, :none))
# labelsym(letter) = symbol(letter * "label")
# rotationsym(letter) = symbol(letter * "rotation")
function plotlyaxis(axis::Axis, sp::Subplot{PlotlyBackend})
function plotly_axis(axis::Axis, sp::Subplot)
letter = axis[:letter]
d = axis.d
ax = KW(
@ -273,10 +273,10 @@ function plotlyaxis(axis::Axis, sp::Subplot{PlotlyBackend})
ax
end
# function get_plot_json(plt::Plot{PlotlyBackend})
# function plotly_layout_json(plt::Plot{PlotlyBackend})
# d = plt.attr
# function plotly_layout(d::KW, seriesargs::AVec{KW})
function plotly_layout(plt::Plot{PlotlyBackend})
function plotly_layout(plt::Plot)
d_out = KW()
# for now, we only support 1 subplot
@ -302,13 +302,13 @@ function plotly_layout(plt::Plot{PlotlyBackend})
# if any(is3d, seriesargs)
if is3d(sp)
d_out[:scene] = KW(
:xaxis => plotlyaxis(sp.attr[:xaxis], sp),
:yaxis => plotlyaxis(sp.attr[:yaxis], sp),
:xzxis => plotlyaxis(sp.attr[:zaxis], sp),
:xaxis => plotly_axis(sp.attr[:xaxis], sp),
:yaxis => plotly_axis(sp.attr[:yaxis], sp),
:xzxis => plotly_axis(sp.attr[:zaxis], sp),
)
else
d_out[:xaxis] = plotlyaxis(sp.attr[:xaxis], sp)
d_out[:yaxis] = plotlyaxis(sp.attr[:yaxis], sp)
d_out[:xaxis] = plotly_axis(sp.attr[:xaxis], sp)
d_out[:yaxis] = plotly_axis(sp.attr[:yaxis], sp)
end
# legend
@ -354,7 +354,7 @@ function plotly_layout(plt::Plot{PlotlyBackend})
d_out
end
function get_plot_json(plt::Plot{PlotlyBackend})
function plotly_layout_json(plt::Plot)
JSON.json(plotly_layout(plt))
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)
function plotly_series(plt::Plot{PlotlyBackend}, series::Series)
function plotly_series(plt::Plot, series::Series)
d = series.d
d_out = KW()
@ -520,7 +520,7 @@ function plotly_series(plt::Plot{PlotlyBackend}, series::Series)
end
# 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))
end
@ -540,7 +540,7 @@ function html_body(plt::Plot{PlotlyBackend}, style = nothing)
<div id=\"$(uuid)\" style=\"$(style)\"></div>
<script>
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>
"""
html
@ -549,7 +549,7 @@ end
function js_body(plt::Plot{PlotlyBackend}, uuid)
js = """
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

View File

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