From 3db6d2145276e4bab80db29f2706a4f2904b6ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20T=C3=A9treault-Pinard?= Date: Tue, 6 Sep 2016 17:13:02 -0400 Subject: [PATCH] dry: pull trace indices input -> list logic in helpers - use it in Plotly.restyle and Plotly.animate - call trace indices argument 'traces' consistently in restyle, update, and transition (to be consistent with the frame attribute 'traces'). --- src/plot_api/helpers.js | 13 +++++++++++++ src/plot_api/plot_api.js | 14 +++++--------- src/plots/plots.js | 15 +++------------ 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/plot_api/helpers.js b/src/plot_api/helpers.js index 06e1b31594c..35d9a0495f1 100644 --- a/src/plot_api/helpers.js +++ b/src/plot_api/helpers.js @@ -9,6 +9,7 @@ 'use strict'; +var isNumeric = require('fast-isnumeric'); var m4FromQuat = require('gl-mat4/fromQuat'); var Registry = require('../registry'); @@ -399,3 +400,15 @@ exports.swapXYData = function(trace) { trace.hoverinfo = hoverInfoParts.join('+'); } }; + +// coerce traceIndices input to array of trace indices +exports.coerceTraceIndices = function(gd, traceIndices) { + if(isNumeric(traceIndices)) { + return [traceIndices]; + } + else if(!Array.isArray(traceIndices) || !traceIndices.length) { + return gd.data.map(function(_, i) { return i; }); + } + + return traceIndices; +}; diff --git a/src/plot_api/plot_api.js b/src/plot_api/plot_api.js index 1e2227ca71d..2a7f445708b 100644 --- a/src/plot_api/plot_api.js +++ b/src/plot_api/plot_api.js @@ -1192,17 +1192,13 @@ Plotly.restyle = function restyle(gd, astr, val, traces) { }); }; -function _restyle(gd, aobj, traces) { +function _restyle(gd, aobj, _traces) { var fullLayout = gd._fullLayout, fullData = gd._fullData, data = gd.data, i; - // fill up traces - if(isNumeric(traces)) traces = [traces]; - else if(!Array.isArray(traces) || !traces.length) { - traces = data.map(function(_, i) { return i; }); - } + var traces = helpers.coerceTraceIndices(gd, _traces); // initialize flags var flags = { @@ -2035,7 +2031,7 @@ function _relayout(gd, aobj) { * integer or array of integers for the traces to alter (all if omitted) * */ -Plotly.update = function update(gd, traceUpdate, layoutUpdate, indices) { +Plotly.update = function update(gd, traceUpdate, layoutUpdate, traces) { gd = helpers.getGraphDiv(gd); helpers.clearPromiseQueue(gd); @@ -2049,7 +2045,7 @@ Plotly.update = function update(gd, traceUpdate, layoutUpdate, indices) { if(Object.keys(traceUpdate).length) gd.changed = true; if(Object.keys(layoutUpdate).length) gd.changed = true; - var restyleSpecs = _restyle(gd, traceUpdate, indices), + var restyleSpecs = _restyle(gd, traceUpdate, traces), restyleFlags = restyleSpecs.flags; var relayoutSpecs = _relayout(gd, layoutUpdate), @@ -2286,7 +2282,7 @@ Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) { Plots.transition(gd, newFrame.frame.data, newFrame.frame.layout, - newFrame.frame.traces, + helpers.coerceTraceIndices(gd, newFrame.frame.traces), newFrame.frameOpts, newFrame.transitionOpts ); diff --git a/src/plots/plots.js b/src/plots/plots.js index b4948875f61..a38c9a13744 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -1348,27 +1348,18 @@ plots.computeFrame = function(gd, frameName) { * an array of data objects following the normal Plotly data definition format * @param {Object} layout * a layout object, following normal Plotly layout format - * @param {Number[]} traceIndices + * @param {Number[]} traces * indices of the corresponding traces specified in `data` * @param {Object} frameOpts * options for the frame (i.e. whether to redraw post-transition) * @param {Object} transitionOpts * options for the transition */ -plots.transition = function(gd, data, layout, traceIndices, frameOpts, transitionOpts) { +plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts) { var i, traceIdx; var dataLength = Array.isArray(data) ? data.length : 0; - - // Select which traces will be updated: - if(isNumeric(traceIndices)) traceIndices = [traceIndices]; - else if(!Array.isArray(traceIndices) || !traceIndices.length) { - traceIndices = gd.data.map(function(v, i) { return i; }); - } - - if(traceIndices.length > dataLength) { - traceIndices = traceIndices.slice(0, dataLength); - } + var traceIndices = traces.slice(0, dataLength); var transitionedTraces = [];