forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.js
214 lines (179 loc) · 5.54 KB
/
filter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
* 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('../lib');
/* eslint no-unused-vars: 0*/
// so that Plotly.register knows what to do with it
exports.moduleType = 'transform';
// determines to link between transform type and transform module
exports.name = 'filter';
// ... as trace attributes
exports.attributes = {
operation: {
valType: 'enumerated',
values: ['=', '<', '>', 'within', 'notwithin', 'in', 'notin'],
dflt: '='
},
value: {
valType: 'any',
dflt: 0
},
filtersrc: {
valType: 'enumerated',
values: ['x', 'y'],
dflt: 'x'
}
};
/**
* Supply transform attributes defaults
*
* @param {object} transformIn
* object linked to trace.transforms[i] with 'type' set to exports.name
* @param {object} fullData
* the plot's full data
* @param {object} layout
* the plot's (not-so-full) layout
*
* @return {object} transformOut
* copy of transformIn that contains attribute defaults
*/
exports.supplyDefaults = function(transformIn, fullData, layout) {
var transformOut = {};
function coerce(attr, dflt) {
return Lib.coerce(transformIn, transformOut, exports.attributes, attr, dflt);
}
coerce('operation');
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;
};
/**
* Apply transform !!!
*
* @param {array} data
* array of transformed traces (is [fullTrace] upon first transform)
*
* @param {object} state
* state object which includes:
* - transform {object} full transform attributes
* - fullTrace {object} full trace object which is being transformed
* - fullData {array} full pre-transform(s) data array
* - layout {object} the plot's (not-so-full) layout
*
* @return {object} newData
* array of transformed traces
*/
exports.transform = function(data, state) {
// one-to-one case
var newData = data.map(function(trace) {
return transformOne(trace, state);
});
return newData;
};
function transformOne(trace, state) {
var newTrace = Lib.extendDeep({}, trace);
var opts = state.transform;
var src = opts.filtersrc;
var filterFunc = getFilterFunc(opts);
var len = trace[src].length;
var arrayAttrs = findArrayAttributes(trace);
arrayAttrs.forEach(function(attr) {
Lib.nestedProperty(newTrace, attr).set([]);
});
function fill(attr, i) {
var arr = Lib.nestedProperty(trace, attr).get();
var newArr = Lib.nestedProperty(newTrace, attr).get();
newArr.push(arr[i]);
}
for(var i = 0; i < len; i++) {
var v = trace[src][i];
if(!filterFunc(v)) continue;
for(var j = 0; j < arrayAttrs.length; j++) {
fill(arrayAttrs[j], i);
}
}
return newTrace;
}
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 '=':
return function(v) { return v === value; };
case '<':
return function(v) { return v < value; };
case '>':
return function(v) { return v > value; };
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; };
}
}
function findArrayAttributes(obj, root) {
root = root || '';
var list = [];
Object.keys(obj).forEach(function(k) {
var val = obj[k];
if(k.charAt(0) === '_') return;
if(k === 'transforms') {
val.forEach(function(item, i) {
list = list.concat(
findArrayAttributes(item, root + k + '[' + i + ']' + '.')
);
});
}
else if(Lib.isPlainObject(val)) {
list = list.concat(findArrayAttributes(val, root + k + '.'));
}
else if(Array.isArray(val)) {
list.push(root + k);
}
});
return list;
}