10
10
11
11
var Lib = require ( '../lib' ) ;
12
12
var axisIds = require ( '../plots/cartesian/axis_ids' ) ;
13
+ var autoType = require ( '../plots/cartesian/axis_autotype' ) ;
14
+ var setConvert = require ( '../plots/cartesian/set_convert' ) ;
13
15
14
16
var INEQUALITY_OPS = [ '=' , '<' , '>=' , '>' , '<=' ] ;
15
17
var INTERVAL_OPS = [ '[]' , '()' , '[)' , '(]' , '][' , ')(' , '](' , ')[' ] ;
@@ -27,18 +29,22 @@ exports.attributes = {
27
29
'Determines whether this filter transform is enabled or disabled.'
28
30
] . join ( ' ' )
29
31
} ,
30
- filtersrc : {
32
+ target : {
31
33
valType : 'string' ,
32
34
strict : true ,
33
35
noBlank : true ,
36
+ arrayOk : true ,
34
37
dflt : 'x' ,
35
38
description : [
36
- 'Sets the variable in the parent trace object' ,
37
- 'by which the filter will be applied.' ,
39
+ 'Sets the filter target by which the filter is applied.' ,
38
40
41
+ 'If a string, *target* is assumed to be a reference to a data array' ,
42
+ 'in the parent trace object.' ,
39
43
'To filter about nested variables, use *.* to access them.' ,
40
- 'For example, set `filtersrc` to *marker.color* to filter' ,
41
- 'about the marker color array.'
44
+ 'For example, set `target` to *marker.color* to filter' ,
45
+ 'about the marker color array.' ,
46
+
47
+ 'If an array, *target* is then the data array by which the filter is applied.'
42
48
] . join ( ' ' )
43
49
} ,
44
50
operation : {
@@ -77,7 +83,7 @@ exports.attributes = {
77
83
'Sets the value or values by which to filter by.' ,
78
84
79
85
'Values are expected to be in the same type as the data linked' ,
80
- 'to *filtersrc *.' ,
86
+ 'to *target *.' ,
81
87
82
88
'When `operation` is set to one of the inequality values' ,
83
89
'(' + INEQUALITY_OPS + ')' ,
@@ -108,25 +114,24 @@ exports.supplyDefaults = function(transformIn) {
108
114
if ( enabled ) {
109
115
coerce ( 'operation' ) ;
110
116
coerce ( 'value' ) ;
111
- coerce ( 'filtersrc ' ) ;
117
+ coerce ( 'target ' ) ;
112
118
}
113
119
114
120
return transformOut ;
115
121
} ;
116
122
117
123
exports . calcTransform = function ( gd , trace , opts ) {
118
- var filtersrc = opts . filtersrc ,
119
- filtersrcOk = filtersrc && Array . isArray ( Lib . nestedProperty ( trace , filtersrc ) . get ( ) ) ;
120
-
121
- if ( ! opts . enabled || ! filtersrcOk ) return ;
124
+ if ( ! opts . enabled ) return ;
122
125
123
- var dataToCoord = getDataToCoordFunc ( gd , trace , filtersrc ) ,
124
- filterFunc = getFilterFunc ( opts , dataToCoord ) ;
126
+ var target = opts . target ,
127
+ filterArray = getFilterArray ( trace , target ) ,
128
+ len = filterArray . length ;
125
129
126
- var filterArr = Lib . nestedProperty ( trace , filtersrc ) . get ( ) ,
127
- len = filterArr . length ;
130
+ if ( ! len ) return ;
128
131
129
- var arrayAttrs = Lib . findArrayAttributes ( trace ) ,
132
+ var dataToCoord = getDataToCoordFunc ( gd , trace , target ) ,
133
+ filterFunc = getFilterFunc ( opts , dataToCoord ) ,
134
+ arrayAttrs = Lib . findArrayAttributes ( trace ) ,
130
135
originalArrays = { } ;
131
136
132
137
// copy all original array attribute values,
@@ -147,7 +152,7 @@ exports.calcTransform = function(gd, trace, opts) {
147
152
}
148
153
149
154
for ( var i = 0 ; i < len ; i ++ ) {
150
- var v = filterArr [ i ] ;
155
+ var v = filterArray [ i ] ;
151
156
152
157
if ( ! filterFunc ( v ) ) continue ;
153
158
@@ -157,18 +162,43 @@ exports.calcTransform = function(gd, trace, opts) {
157
162
}
158
163
} ;
159
164
160
- function getDataToCoordFunc ( gd , trace , filtersrc ) {
161
- var ax = axisIds . getFromTrace ( gd , trace , filtersrc ) ;
165
+ function getFilterArray ( trace , target ) {
166
+ if ( typeof target === 'string' && target ) {
167
+ var array = Lib . nestedProperty ( trace , target ) . get ( ) ;
168
+
169
+ return Array . isArray ( array ) ? array : [ ] ;
170
+ }
171
+ else if ( Array . isArray ( target ) ) return target . slice ( ) ;
172
+
173
+ return false ;
174
+ }
175
+
176
+ function getDataToCoordFunc ( gd , trace , target ) {
177
+ var ax ;
178
+
179
+ // In the case of an array target, make a mock data array
180
+ // and call supplyDefaults to the data type and
181
+ // setup the data-to-calc method.
182
+ if ( Array . isArray ( target ) ) {
183
+ ax = {
184
+ type : autoType ( target ) ,
185
+ _categories : [ ]
186
+ } ;
187
+ setConvert ( ax ) ;
188
+ }
189
+ else {
190
+ ax = axisIds . getFromTrace ( gd , trace , target ) ;
191
+ }
162
192
163
- // if 'filtersrc ' has corresponding axis
193
+ // if 'target ' has corresponding axis
164
194
// -> use setConvert method
165
195
if ( ax ) return ax . d2c ;
166
196
167
197
// special case for 'ids'
168
198
// -> cast to String
169
- if ( filtersrc === 'ids' ) return function ( v ) { return String ( v ) ; } ;
199
+ if ( target === 'ids' ) return function ( v ) { return String ( v ) ; } ;
170
200
171
- // otherwise
201
+ // otherwise (e.g. numeric-array of 'marker.color' or 'marker.size')
172
202
// -> cast to Number
173
203
return function ( v ) { return + v ; } ;
174
204
}
0 commit comments