Skip to content

Commit e39d447

Browse files
committed
filter: auto-type filter target arrays
- use auto-typed axis data-to-calc method to filter by target array
1 parent c80f144 commit e39d447

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

src/transforms/filter.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
var Lib = require('../lib');
12+
var Plots = require('../plots/plots');
1213
var axisIds = require('../plots/cartesian/axis_ids');
1314

1415
var INEQUALITY_OPS = ['=', '<', '>=', '>', '<='];
@@ -31,6 +32,7 @@ exports.attributes = {
3132
valType: 'string',
3233
strict: true,
3334
noBlank: true,
35+
arrayOk: true,
3436
dflt: 'x',
3537
description: [
3638
'Sets the filter target by which the filter is applied.',
@@ -165,12 +167,29 @@ function getFilterArray(trace, target) {
165167

166168
return Array.isArray(array) ? array : [];
167169
}
170+
else if(Array.isArray(target)) return target.slice();
168171

169172
return false;
170173
}
171174

172175
function getDataToCoordFunc(gd, trace, target) {
173-
var ax = axisIds.getFromTrace(gd, trace, target);
176+
var ax;
177+
178+
// In the case of an array target, make a mock data array
179+
// and call supplyDefaults to the data type and
180+
// setup the data-to-calc method.
181+
if(Array.isArray(target)) {
182+
var mockGd = {
183+
data: [{ x: target }],
184+
layout: {}
185+
};
186+
187+
Plots.supplyDefaults(mockGd);
188+
ax = mockGd._fullLayout.xaxis;
189+
}
190+
else {
191+
ax = axisIds.getFromTrace(gd, trace, target);
192+
}
174193

175194
// if 'target' has corresponding axis
176195
// -> use setConvert method
@@ -180,7 +199,7 @@ function getDataToCoordFunc(gd, trace, target) {
180199
// -> cast to String
181200
if(target === 'ids') return function(v) { return String(v); };
182201

183-
// otherwise
202+
// otherwise (e.g. numeric-array of 'marker.color' or 'marker.size')
184203
// -> cast to Number
185204
return function(v) { return +v; };
186205
}

test/jasmine/tests/transform_filter_test.js

+68
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,74 @@ describe('filter transforms calc:', function() {
614614
expect(out[0].y).toEqual([2, 2, 3]);
615615
expect(out[0].ids).toEqual(['n1', 'p1', 'p2']);
616616
});
617+
618+
describe('filters should handle array *target* values', function() {
619+
var _base = Lib.extendDeep({}, base);
620+
621+
function _assert(out, x, y, markerColor) {
622+
expect(out[0].x).toEqual(x, '- x coords');
623+
expect(out[0].y).toEqual(y, '- y coords');
624+
expect(out[0].marker.color).toEqual(markerColor, '- marker.color arrayOk');
625+
expect(out[0].marker.size).toEqual(20, '- marker.size style');
626+
}
627+
628+
it('with numeric items', function() {
629+
var out = _transform([Lib.extendDeep({}, _base, {
630+
transforms: [{
631+
target: [1, 1, 0, 0, 1, 0, 1],
632+
operation: '{}',
633+
value: 0
634+
}]
635+
})]);
636+
637+
_assert(out, [-2, 0, 2], [3, 1, 3], [0.3, 0.1, 0.3]);
638+
expect(out[0].transforms[0].target).toEqual([0, 0, 0]);
639+
});
640+
641+
it('with categorical items', function() {
642+
var out = _transform([Lib.extendDeep({}, _base, {
643+
transforms: [{
644+
target: ['a', 'a', 'b', 'b', 'a', 'b', 'a'],
645+
operation: '{}',
646+
value: 'b'
647+
}]
648+
})]);
649+
650+
_assert(out, [-2, 0, 2], [3, 1, 3], [0.3, 0.1, 0.3]);
651+
expect(out[0].transforms[0].target).toEqual(['b', 'b', 'b']);
652+
});
653+
654+
it('with dates items', function() {
655+
var out = _transform([Lib.extendDeep({}, _base, {
656+
transforms: [{
657+
target: ['2015-07-20', '2016-08-01', '2016-09-01', '2016-10-21', '2016-12-02'],
658+
operation: '<',
659+
value: '2016-01-01'
660+
}]
661+
})]);
662+
663+
_assert(out, [-2], [1], [0.1]);
664+
expect(out[0].transforms[0].target).toEqual(['2015-07-20']);
665+
});
666+
667+
it('with multiple transforms (dates) ', function() {
668+
var out = _transform([Lib.extendDeep({}, _base, {
669+
transforms: [{
670+
target: ['2015-07-20', '2016-08-01', '2016-09-01', '2016-10-21', '2016-12-02'],
671+
operation: '>',
672+
value: '2016-01-01'
673+
}, {
674+
type: 'filter',
675+
target: ['2015-07-20', '2016-08-01', '2016-09-01', '2016-10-21', '2016-12-02'],
676+
operation: '<',
677+
value: '2016-09-01'
678+
}]
679+
})]);
680+
681+
_assert(out, [-1], [2], [0.2]);
682+
expect(out[0].transforms[0].target).toEqual(['2016-08-01']);
683+
});
684+
});
617685
});
618686

619687
describe('filter transforms interactions', function() {

0 commit comments

Comments
 (0)