Skip to content

Commit

Permalink
dry: pull trace indices input -> list logic in helpers
Browse files Browse the repository at this point in the history
- 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').
  • Loading branch information
etpinard committed Sep 6, 2016
1 parent e8dbf55 commit 3db6d21
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
13 changes: 13 additions & 0 deletions src/plot_api/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

'use strict';

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

var Registry = require('../registry');
Expand Down Expand Up @@ -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;
};
14 changes: 5 additions & 9 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);

Expand All @@ -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),
Expand Down Expand Up @@ -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
);
Expand Down
15 changes: 3 additions & 12 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down

0 comments on commit 3db6d21

Please sign in to comment.