Skip to content
This repository has been archived by the owner on Mar 31, 2024. It is now read-only.

Commit

Permalink
Clarify the usage of alter()
Browse files Browse the repository at this point in the history
  • Loading branch information
Rashid Khan committed Sep 1, 2015
1 parent af429c2 commit edba8af
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 59 deletions.
2 changes: 1 addition & 1 deletion lib/alter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var _ = require('lodash');
/* @param {Array} args
* - args[0] must be a seriesList
* @params {Function} fn - Function used to combine points at same index in each array of each series in the seriesList.
* @params {Function} fn - Function to apply to each series in the seriesList
* @return {seriesList}
*/

Expand Down
8 changes: 4 additions & 4 deletions series_functions/abs.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ module.exports = new Chainable('abs', {
],
help: 'Return the absolute value of each value in the series list',
fn: function absFn(args) {
return alter(args, function (inputSeries) {
var data = _.map(inputSeries.data, function (point) {
return alter(args, function (eachSeries) {
var data = _.map(eachSeries.data, function (point) {
return [point[0], Math.abs(point[1])];
});
inputSeries.data = data;
return inputSeries;
eachSeries.data = data;
return eachSeries;
});
}
});
10 changes: 5 additions & 5 deletions series_functions/bars.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ module.exports = new Chainable('bars', {
],
help: 'Show the seriesList as bars',
fn: function barsFn(args) {
return alter(args, function (inputSeries, width) {
inputSeries.bars = inputSeries.bars || {};
inputSeries.bars.show = width == null ? 1 : width;
inputSeries.bars.lineWidth = width == null ? 6 : width;
return inputSeries;
return alter(args, function (eachSeries, width) {
eachSeries.bars = eachSeries.bars || {};
eachSeries.bars.show = width == null ? 1 : width;
eachSeries.bars.lineWidth = width == null ? 6 : width;
return eachSeries;
});
}
});
6 changes: 3 additions & 3 deletions series_functions/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ module.exports = new Chainable('color', {
],
help: 'Change the color of the series',
fn: function colorFn(args) {
return alter(args, function (inputSeries, color) {
inputSeries.color = color;
return inputSeries;
return alter(args, function (eachSeries, color) {
eachSeries.color = color;
return eachSeries;
});
}
});
8 changes: 4 additions & 4 deletions series_functions/derivative.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ module.exports = new Chainable('derivative', {
],
help: 'Show the seriesList as bars',
fn: function derivativeFn(args) {
return alter(args, function (inputSeries) {
var pairs = inputSeries.data;
inputSeries.data = _.map(pairs, function (point, i) {
return alter(args, function (eachSeries) {
var pairs = eachSeries.data;
eachSeries.data = _.map(pairs, function (point, i) {
if (i === 0 || pairs[i - 1][1] == null || point[1] == null) { return [point[0], null]; }
return [point[0], point[1] - pairs[i - 1][1]];
});

return inputSeries;
return eachSeries;
});
}
});
4 changes: 2 additions & 2 deletions series_functions/first.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ module.exports = new Chainable('first', {
],
help: 'This is an internal function that simply returns the input series. Don\'t use this',
fn: function firstFn(args) {
return alter(args, function (inputSeries) {
return inputSeries;
return alter(args, function (eachSeries) {
return eachSeries;
});
}
});
6 changes: 3 additions & 3 deletions series_functions/hide.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ module.exports = new Chainable('hide', {
],
help: 'Hide the series by default',
fn: function hideFn(args) {
return alter(args, function (inputSeries, hide) {
inputSeries._hide = hide == null ? true : hide;
return inputSeries;
return alter(args, function (eachSeries, hide) {
eachSeries._hide = hide == null ? true : hide;
return eachSeries;
});
}
});
8 changes: 4 additions & 4 deletions series_functions/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ module.exports = new Chainable('label', {
],
help: 'Change the label of the series. Use %s reference the existing label',
fn: function labelFn(args) {
return alter(args, function (inputSeries, label) {
return alter(args, function (eachSeries, label) {
if (label.indexOf('%s') !== -1) {
inputSeries.label = util.format(label, inputSeries.label);
eachSeries.label = util.format(label, eachSeries.label);
} else {
inputSeries.label = label;
eachSeries.label = label;
}

return inputSeries;
return eachSeries;
});
}
});
12 changes: 6 additions & 6 deletions series_functions/legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ module.exports = new Chainable('legend', {
],
help: 'Set the position and style of the legend on the plot',
fn: function yaxisFn(args) {
return alter(args, function (inputSeries, position, columns) {
inputSeries._global = inputSeries._global || {};
inputSeries._global.legend = inputSeries._global.legend || {};
inputSeries._global.legend.position = position;
inputSeries._global.legend.noColumns = columns;
return alter(args, function (eachSeries, position, columns) {
eachSeries._global = eachSeries._global || {};
eachSeries._global.legend = eachSeries._global.legend || {};
eachSeries._global.legend.position = position;
eachSeries._global.legend.noColumns = columns;

return inputSeries;
return eachSeries;
});
}
});
16 changes: 8 additions & 8 deletions series_functions/lines.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ module.exports = new Chainable('lines', {
],
help: 'Show the seriesList as lines',
fn: function linesFn(args) {
return alter(args, function (inputSeries, width, fill, show, steps) {
inputSeries.lines = inputSeries.lines || {};
return alter(args, function (eachSeries, width, fill, show, steps) {
eachSeries.lines = eachSeries.lines || {};

// Defaults
if (inputSeries.lines.lineWidth == null) inputSeries.lines.lineWidth = 3;
if (eachSeries.lines.lineWidth == null) eachSeries.lines.lineWidth = 3;

if (width != null) inputSeries.lines.lineWidth = width;
if (fill != null) inputSeries.lines.fill = fill / 10;
if (show != null) inputSeries.lines.show = show;
if (steps != null) inputSeries.lines.steps = steps;
if (width != null) eachSeries.lines.lineWidth = width;
if (fill != null) eachSeries.lines.fill = fill / 10;
if (show != null) eachSeries.lines.show = show;
if (steps != null) eachSeries.lines.steps = steps;

return inputSeries;
return eachSeries;
});
}
});
8 changes: 4 additions & 4 deletions series_functions/movingaverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ module.exports = new Chainable('movingaverage', {
aliases: ['mvavg'],
help: 'Calculate the moving average over a given window. Nice for smoothing noisey series',
fn: function movingaverageFn(args) {
return alter(args, function (inputSeries, _window) {
return alter(args, function (eachSeries, _window) {

var pairs = inputSeries.data;
var pairs = eachSeries.data;

inputSeries.data = _.map(pairs, function (point, i) {
eachSeries.data = _.map(pairs, function (point, i) {
if (i < _window) { return [point[0], null]; }

var average = _.chain(pairs.slice(i - _window, i))
Expand All @@ -31,7 +31,7 @@ module.exports = new Chainable('movingaverage', {

return [point[0], average];
});
return inputSeries;
return eachSeries;
});
}
});
16 changes: 8 additions & 8 deletions series_functions/points.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ module.exports = new Chainable('points', {
],
help: 'Show the series as points',
fn: function pointsFn(args) {
return alter(args, function (inputSeries, radius, weight, fill, fillColor, show) {
inputSeries.points = inputSeries.points || {};
inputSeries.points.radius = radius == null ? undefined : radius;
return alter(args, function (eachSeries, radius, weight, fill, fillColor, show) {
eachSeries.points = eachSeries.points || {};
eachSeries.points.radius = radius == null ? undefined : radius;

if (fill) {
inputSeries.points.fillColor = fillColor == null ? false : fillColor;
eachSeries.points.fillColor = fillColor == null ? false : fillColor;
}

if (fill != null) {
inputSeries.points.fill = fill / 10;
eachSeries.points.fill = fill / 10;
}

if (weight != null) {
inputSeries.points.lineWidth = weight;
eachSeries.points.lineWidth = weight;
}

inputSeries.points.show = show == null ? true : show;
eachSeries.points.show = show == null ? true : show;



return inputSeries;
return eachSeries;
});
}
});
2 changes: 0 additions & 2 deletions series_functions/worldbank.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ module.exports = new Datasource ('worldbank', {
max: moment(tlConfig.time.to).format('YYYY')
};

console.log(time);

var URL = 'http://api.worldbank.org/' + config.code +
'?date=' + time.min + ':' + time.max +
'&format=json' +
Expand Down
10 changes: 5 additions & 5 deletions series_functions/yaxis.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ module.exports = new Chainable('yaxis', {
],
help: 'This is an internal function that simply returns the input series. Don\'t use this',
fn: function yaxisFn(args) {
return alter(args, function (inputSeries, yaxis, min, max, position) {
return alter(args, function (eachSeries, yaxis, min, max, position) {
yaxis = yaxis || 1;

inputSeries.yaxis = yaxis;
inputSeries._global = inputSeries._global || {};
eachSeries.yaxis = yaxis;
eachSeries._global = eachSeries._global || {};

var yaxes = inputSeries._global.yaxes = inputSeries._global.yaxes || [];
var yaxes = eachSeries._global.yaxes = eachSeries._global.yaxes || [];
var myAxis = yaxes[yaxis - 1] = yaxes[yaxis - 1] || {};
myAxis.position = position;
myAxis.min = min == null ? 0 : min;
myAxis.max = max;


return inputSeries;
return eachSeries;
});
}
});

0 comments on commit edba8af

Please sign in to comment.