Skip to content

(WIP) Filter and GroupBy Transforms #859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
9 changes: 9 additions & 0 deletions lib/filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = require('../src/transforms/filter');
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ Plotly.register([
require('./scattermapbox')
]);

// add transforms
Plotly.register([
require('./filter')
]);

module.exports = Plotly;
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

var isNumeric = require('fast-isnumeric');

// var Lib = require('@src/lib');
var Lib = require('../../../../src/lib');
var Lib = require('../lib');

/*eslint no-unused-vars: 0*/


// so that Plotly.register knows what to do with it
exports.moduleType = 'transform';

Expand All @@ -16,11 +25,11 @@ exports.name = 'filter';
exports.attributes = {
operation: {
valType: 'enumerated',
values: ['=', '<', '>'],
values: ['=', '<', '>', 'within', 'notwithin', 'in', 'notin'],
dflt: '='
},
value: {
valType: 'number',
valType: 'any',
dflt: 0
},
filtersrc: {
Expand Down Expand Up @@ -54,6 +63,16 @@ exports.supplyDefaults = function(transformIn, fullData, layout) {
coerce('value');
coerce('filtersrc');

// numeric values as character should be converted to numeric
if(Array.isArray(transformOut.value)) {
transformOut.value = transformOut.value.map(function(v) {
if(isNumeric(v)) v = +v;
return v;
});
} else {
if(isNumeric(transformOut.value)) transformOut.value = +transformOut.value;
}

// or some more complex logic using fullData and layout

return transformOut;
Expand Down Expand Up @@ -121,6 +140,16 @@ function transformOne(trace, state) {

function getFilterFunc(opts) {
var value = opts.value;
// if value is not array then coerce to
// an array of [value,value] so the
// filter function will work
// but perhaps should just error out
var valueArr = [];
if(!Array.isArray(value)) {
valueArr = [value, value];
} else {
valueArr = value;
}

switch(opts.operation) {
case '=':
Expand All @@ -129,6 +158,30 @@ function getFilterFunc(opts) {
return function(v) { return v < value; };
case '>':
return function(v) { return v > value; };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work for datetime strings too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will try to get an example up, but yes it does work with datetime strings. It does not seem to work though with Date and datetime strings.

case 'within':
return function(v) {
// if character then ignore with no side effect
function notDateNumber(d) {
return !(isNumeric(d) || Lib.isDateTime(d));
}
if(valueArr.some(notDateNumber)) {
return true;
}

// keep the = ?
return v >= Math.min.apply(null, valueArr) &&
v <= Math.max.apply(null, valueArr);
};
case 'notwithin':
return function(v) {
// keep the = ?
return !(v >= Math.min.apply(null, valueArr) &&
v <= Math.max.apply(null, valueArr));
};
case 'in':
return function(v) { return valueArr.indexOf(v) >= 0; };
case 'notin':
return function(v) { return valueArr.indexOf(v) === -1; };
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

// var Lib = require('@src/lib');
var Lib = require('../../../../src/lib');
var Lib = require('../lib');

/*eslint no-unused-vars: 0*/

Expand Down
Loading