Skip to content

Commit 5bce249

Browse files
authored
Merge pull request #1253 from plotly/filter-cals
filters use valuecalendar and targetcalendar instead of calendar
2 parents ef7a965 + 3764bcc commit 5bce249

File tree

7 files changed

+135
-11
lines changed

7 files changed

+135
-11
lines changed

src/components/calendars/index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,14 @@ module.exports = {
230230
},
231231
transforms: {
232232
filter: {
233-
calendar: makeAttrs([
234-
'Sets the calendar system to use for `value`, if it is a date.',
235-
'Note that this is not necessarily the same calendar as is used',
236-
'for the target data; that is set by its own calendar attribute,',
237-
'ie `trace.x` uses `trace.xcalendar` etc.'
233+
valuecalendar: makeAttrs([
234+
'Sets the calendar system to use for `value`, if it is a date.'
235+
].join(' ')),
236+
targetcalendar: makeAttrs([
237+
'Sets the calendar system to use for `target`, if it is an',
238+
'array of dates. If `target` is a string (eg *x*) we use the',
239+
'corresponding trace attribute (eg `xcalendar`) if it exists,',
240+
'even if `targetcalendar` is provided.'
238241
].join(' '))
239242
}
240243
}

src/plot_api/helpers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,13 @@ exports.cleanData = function(data, existingData) {
353353
transform.target = transform.filtersrc;
354354
delete transform.filtersrc;
355355
}
356+
357+
if(transform.calendar) {
358+
if(!transform.valuecalendar) {
359+
transform.valuecalendar = transform.calendar;
360+
}
361+
delete transform.calendar;
362+
}
356363
}
357364
}
358365
}

src/transforms/filter.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ exports.supplyDefaults = function(transformIn) {
119119
coerce('target');
120120

121121
var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleDefaults');
122-
handleCalendarDefaults(transformIn, transformOut, 'calendar', null);
122+
handleCalendarDefaults(transformIn, transformOut, 'valuecalendar', null);
123+
handleCalendarDefaults(transformIn, transformOut, 'targetcalendar', null);
123124
}
124125

125126
return transformOut;
@@ -134,8 +135,21 @@ exports.calcTransform = function(gd, trace, opts) {
134135

135136
if(!len) return;
136137

137-
var targetCalendar = Lib.nestedProperty(trace, target + 'calendar').get(),
138-
dataToCoord = getDataToCoordFunc(gd, trace, target),
138+
var targetCalendar = opts.targetcalendar;
139+
140+
// even if you provide targetcalendar, if target is a string and there
141+
// is a calendar attribute matching target it will get used instead.
142+
if(typeof target === 'string') {
143+
var attrTargetCalendar = Lib.nestedProperty(trace, target + 'calendar').get();
144+
if(attrTargetCalendar) targetCalendar = attrTargetCalendar;
145+
}
146+
147+
// if target points to an axis, use the type we already have for that
148+
// axis to find the data type. Otherwise use the values to autotype.
149+
var d2cTarget = (target === 'x' || target === 'y' || target === 'z') ?
150+
target : filterArray;
151+
152+
var dataToCoord = getDataToCoordFunc(gd, trace, d2cTarget),
139153
filterFunc = getFilterFunc(opts, dataToCoord, targetCalendar),
140154
arrayAttrs = PlotSchema.findArrayAttributes(trace),
141155
originalArrays = {};
@@ -226,7 +240,7 @@ function getFilterFunc(opts, d2c, targetCalendar) {
226240
return array.indexOf(operation) !== -1;
227241
}
228242

229-
var d2cValue = function(v) { return d2c(v, 0, opts.calendar); },
243+
var d2cValue = function(v) { return d2c(v, 0, opts.valuecalendar); },
230244
d2cTarget = function(v) { return d2c(v, 0, targetCalendar); };
231245

232246
var coercedValue;

test/image/mocks/world-cals.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@
350350
{
351351
"type": "filter",
352352
"operation": "[]",
353-
"calendar": "jalali",
353+
"valuecalendar": "jalali",
354354
"value": [
355355
"0818-08",
356356
"0819-06"

test/jasmine/tests/plot_api_test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,36 @@ describe('Test plot api', function() {
10511051
expect(trace1.transforms[0].target).toEqual('y');
10521052
});
10531053

1054+
it('should rename *calendar* to *valuecalendar* in filter transforms', function() {
1055+
var data = [{
1056+
transforms: [{
1057+
type: 'filter',
1058+
target: 'y',
1059+
calendar: 'hebrew'
1060+
}, {
1061+
type: 'filter',
1062+
operation: '<'
1063+
}]
1064+
}, {
1065+
transforms: [{
1066+
type: 'filter',
1067+
valuecalendar: 'jalali'
1068+
}]
1069+
}];
1070+
1071+
Plotly.plot(gd, data);
1072+
1073+
var trace0 = gd.data[0],
1074+
trace1 = gd.data[1];
1075+
1076+
expect(trace0.transforms.length).toEqual(2);
1077+
expect(trace0.transforms[0].calendar).toBeUndefined();
1078+
expect(trace0.transforms[0].valuecalendar).toEqual('hebrew');
1079+
1080+
expect(trace1.transforms.length).toEqual(1);
1081+
expect(trace1.transforms[0].valuecalendar).toEqual('jalali');
1082+
});
1083+
10541084
it('should cleanup annotations / shapes refs', function() {
10551085
var data = [{}];
10561086

test/jasmine/tests/plotschema_test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ describe('plot schema', function() {
196196
expect(plotSchema.layout.layoutAttributes.xaxis.calendar.valType).toEqual('enumerated');
197197
expect(plotSchema.layout.layoutAttributes.scene.xaxis.calendar.valType).toEqual('enumerated');
198198

199-
expect(plotSchema.transforms.filter.attributes.calendar.valType).toEqual('enumerated');
199+
expect(plotSchema.transforms.filter.attributes.valuecalendar.valType).toEqual('enumerated');
200+
expect(plotSchema.transforms.filter.attributes.targetcalendar.valType).toEqual('enumerated');
200201
});
201202

202203
it('should list correct defs', function() {

test/jasmine/tests/transform_filter_test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,75 @@ describe('filter transforms calc:', function() {
142142
expect(out[0].z).toEqual(['2016-10-21', '2016-12-02']);
143143
});
144144

145+
it('should use the calendar from the target attribute if target is a string', function() {
146+
// this is the same data as in "filters should handle 3D *z* data"
147+
// but with different calendars
148+
var out = _transform([Lib.extendDeep({}, base, {
149+
type: 'scatter3d',
150+
// the same array as above but in nanakshahi dates
151+
z: ['0547-05-05', '0548-05-17', '0548-06-17', '0548-08-07', '0548-09-19'],
152+
zcalendar: 'nanakshahi',
153+
transforms: [{
154+
type: 'filter',
155+
operation: '>',
156+
value: '5776-06-28',
157+
valuecalendar: 'hebrew',
158+
target: 'z',
159+
// targetcalendar is ignored!
160+
targetcalendar: 'taiwan'
161+
}]
162+
})]);
163+
164+
expect(out[0].x).toEqual([0, 1]);
165+
expect(out[0].y).toEqual([1, 2]);
166+
expect(out[0].z).toEqual(['0548-08-07', '0548-09-19']);
167+
});
168+
169+
it('should use targetcalendar anyway if there is no matching calendar attribute', function() {
170+
// this is the same data as in "filters should handle 3D *z* data"
171+
// but with different calendars
172+
var out = _transform([Lib.extendDeep({}, base, {
173+
type: 'scatter',
174+
// the same array as above but in taiwanese dates
175+
text: ['0104-07-20', '0105-08-01', '0105-09-01', '0105-10-21', '0105-12-02'],
176+
transforms: [{
177+
type: 'filter',
178+
operation: '>',
179+
value: '5776-06-28',
180+
valuecalendar: 'hebrew',
181+
target: 'text',
182+
targetcalendar: 'taiwan'
183+
}]
184+
})]);
185+
186+
expect(out[0].x).toEqual([0, 1]);
187+
expect(out[0].y).toEqual([1, 2]);
188+
expect(out[0].text).toEqual(['0105-10-21', '0105-12-02']);
189+
});
190+
191+
it('should use targetcalendar if target is an array', function() {
192+
// this is the same data as in "filters should handle 3D *z* data"
193+
// but with different calendars
194+
var out = _transform([Lib.extendDeep({}, base, {
195+
type: 'scatter3d',
196+
// the same array as above but in nanakshahi dates
197+
z: ['0547-05-05', '0548-05-17', '0548-06-17', '0548-08-07', '0548-09-19'],
198+
zcalendar: 'nanakshahi',
199+
transforms: [{
200+
type: 'filter',
201+
operation: '>',
202+
value: '5776-06-28',
203+
valuecalendar: 'hebrew',
204+
target: ['0104-07-20', '0105-08-01', '0105-09-01', '0105-10-21', '0105-12-02'],
205+
targetcalendar: 'taiwan'
206+
}]
207+
})]);
208+
209+
expect(out[0].x).toEqual([0, 1]);
210+
expect(out[0].y).toEqual([1, 2]);
211+
expect(out[0].z).toEqual(['0548-08-07', '0548-09-19']);
212+
});
213+
145214
it('filters should handle geographical *lon* data', function() {
146215
var trace0 = {
147216
type: 'scattergeo',

0 commit comments

Comments
 (0)