Skip to content

Commit 3db6d21

Browse files
committed
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').
1 parent e8dbf55 commit 3db6d21

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

src/plot_api/helpers.js

+13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
'use strict';
1111

12+
var isNumeric = require('fast-isnumeric');
1213
var m4FromQuat = require('gl-mat4/fromQuat');
1314

1415
var Registry = require('../registry');
@@ -399,3 +400,15 @@ exports.swapXYData = function(trace) {
399400
trace.hoverinfo = hoverInfoParts.join('+');
400401
}
401402
};
403+
404+
// coerce traceIndices input to array of trace indices
405+
exports.coerceTraceIndices = function(gd, traceIndices) {
406+
if(isNumeric(traceIndices)) {
407+
return [traceIndices];
408+
}
409+
else if(!Array.isArray(traceIndices) || !traceIndices.length) {
410+
return gd.data.map(function(_, i) { return i; });
411+
}
412+
413+
return traceIndices;
414+
};

src/plot_api/plot_api.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -1192,17 +1192,13 @@ Plotly.restyle = function restyle(gd, astr, val, traces) {
11921192
});
11931193
};
11941194

1195-
function _restyle(gd, aobj, traces) {
1195+
function _restyle(gd, aobj, _traces) {
11961196
var fullLayout = gd._fullLayout,
11971197
fullData = gd._fullData,
11981198
data = gd.data,
11991199
i;
12001200

1201-
// fill up traces
1202-
if(isNumeric(traces)) traces = [traces];
1203-
else if(!Array.isArray(traces) || !traces.length) {
1204-
traces = data.map(function(_, i) { return i; });
1205-
}
1201+
var traces = helpers.coerceTraceIndices(gd, _traces);
12061202

12071203
// initialize flags
12081204
var flags = {
@@ -2035,7 +2031,7 @@ function _relayout(gd, aobj) {
20352031
* integer or array of integers for the traces to alter (all if omitted)
20362032
*
20372033
*/
2038-
Plotly.update = function update(gd, traceUpdate, layoutUpdate, indices) {
2034+
Plotly.update = function update(gd, traceUpdate, layoutUpdate, traces) {
20392035
gd = helpers.getGraphDiv(gd);
20402036
helpers.clearPromiseQueue(gd);
20412037

@@ -2049,7 +2045,7 @@ Plotly.update = function update(gd, traceUpdate, layoutUpdate, indices) {
20492045
if(Object.keys(traceUpdate).length) gd.changed = true;
20502046
if(Object.keys(layoutUpdate).length) gd.changed = true;
20512047

2052-
var restyleSpecs = _restyle(gd, traceUpdate, indices),
2048+
var restyleSpecs = _restyle(gd, traceUpdate, traces),
20532049
restyleFlags = restyleSpecs.flags;
20542050

20552051
var relayoutSpecs = _relayout(gd, layoutUpdate),
@@ -2286,7 +2282,7 @@ Plotly.animate = function(gd, frameOrGroupNameOrFrameList, animationOpts) {
22862282
Plots.transition(gd,
22872283
newFrame.frame.data,
22882284
newFrame.frame.layout,
2289-
newFrame.frame.traces,
2285+
helpers.coerceTraceIndices(gd, newFrame.frame.traces),
22902286
newFrame.frameOpts,
22912287
newFrame.transitionOpts
22922288
);

src/plots/plots.js

+3-12
Original file line numberDiff line numberDiff line change
@@ -1348,27 +1348,18 @@ plots.computeFrame = function(gd, frameName) {
13481348
* an array of data objects following the normal Plotly data definition format
13491349
* @param {Object} layout
13501350
* a layout object, following normal Plotly layout format
1351-
* @param {Number[]} traceIndices
1351+
* @param {Number[]} traces
13521352
* indices of the corresponding traces specified in `data`
13531353
* @param {Object} frameOpts
13541354
* options for the frame (i.e. whether to redraw post-transition)
13551355
* @param {Object} transitionOpts
13561356
* options for the transition
13571357
*/
1358-
plots.transition = function(gd, data, layout, traceIndices, frameOpts, transitionOpts) {
1358+
plots.transition = function(gd, data, layout, traces, frameOpts, transitionOpts) {
13591359
var i, traceIdx;
13601360

13611361
var dataLength = Array.isArray(data) ? data.length : 0;
1362-
1363-
// Select which traces will be updated:
1364-
if(isNumeric(traceIndices)) traceIndices = [traceIndices];
1365-
else if(!Array.isArray(traceIndices) || !traceIndices.length) {
1366-
traceIndices = gd.data.map(function(v, i) { return i; });
1367-
}
1368-
1369-
if(traceIndices.length > dataLength) {
1370-
traceIndices = traceIndices.slice(0, dataLength);
1371-
}
1362+
var traceIndices = traces.slice(0, dataLength);
13721363

13731364
var transitionedTraces = [];
13741365

0 commit comments

Comments
 (0)