Skip to content

Commit ff5ef9d

Browse files
add in \ notin and within \ notwithin filter transforms; register filter with plotly
1 parent 9e41556 commit ff5ef9d

File tree

5 files changed

+447
-91
lines changed

5 files changed

+447
-91
lines changed

lib/filter.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
module.exports = require('../src/transforms/filter');

lib/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ Plotly.register([
2929
require('./scattermapbox')
3030
]);
3131

32+
// add transforms
33+
Plotly.register([
34+
require('./filter')
35+
]);
36+
3237
module.exports = Plotly;

src/transforms/filter.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict';
22

33
// var Lib = require('@src/lib');
4-
var Lib = require('../../../../src/lib');
4+
var Lib = require('../lib');
55

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

8-
98
// so that Plotly.register knows what to do with it
109
exports.moduleType = 'transform';
1110

@@ -16,11 +15,11 @@ exports.name = 'filter';
1615
exports.attributes = {
1716
operation: {
1817
valType: 'enumerated',
19-
values: ['=', '<', '>'],
18+
values: ['=', '<', '>', 'within', 'notwithin', 'in', 'notin'],
2019
dflt: '='
2120
},
2221
value: {
23-
valType: 'number',
22+
valType: 'any',
2423
dflt: 0
2524
},
2625
filtersrc: {
@@ -121,6 +120,16 @@ function transformOne(trace, state) {
121120

122121
function getFilterFunc(opts) {
123122
var value = opts.value;
123+
// if value is not array then coerce to
124+
// an array of [value,value] so the
125+
// filter function will work
126+
// but perhaps should just error out
127+
var value_arr = [];
128+
if (!Array.isArray(value)) {
129+
value_arr = [value, value];
130+
} else {
131+
value_arr = value;
132+
}
124133

125134
switch(opts.operation) {
126135
case '=':
@@ -129,6 +138,22 @@ function getFilterFunc(opts) {
129138
return function(v) { return v < value; };
130139
case '>':
131140
return function(v) { return v > value; };
141+
case 'within':
142+
return function(v) {
143+
// keep the = ?
144+
return v >= Math.min.apply( null, value ) &&
145+
v <= Math.max.apply( null, value );
146+
};
147+
case 'notwithin':
148+
return function(v) {
149+
// keep the = ?
150+
return !(v >= Math.min.apply( null, value ) &&
151+
v <= Math.max.apply( null, value ));
152+
};
153+
case 'in':
154+
return function(v) { return value.indexOf(v) >= 0 };
155+
case 'notin':
156+
return function(v) { return value.indexOf(v) === -1 };
132157
}
133158
}
134159

src/transforms/groupby.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
// var Lib = require('@src/lib');
4-
var Lib = require('../../../../src/lib');
4+
var Lib = require('../lib');
55

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

0 commit comments

Comments
 (0)