Skip to content

filters use valuecalendar and targetcalendar instead of calendar #1253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/components/calendars/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,14 @@ module.exports = {
},
transforms: {
filter: {
calendar: makeAttrs([
'Sets the calendar system to use for `value`, if it is a date.',
'Note that this is not necessarily the same calendar as is used',
'for the target data; that is set by its own calendar attribute,',
'ie `trace.x` uses `trace.xcalendar` etc.'
valuecalendar: makeAttrs([
'Sets the calendar system to use for `value`, if it is a date.'
].join(' ')),
targetcalendar: makeAttrs([
'Sets the calendar system to use for `target`, if it is an',
'array of dates. If `target` is a string (eg *x*) we use the',
'corresponding trace attribute (eg `xcalendar`) if it exists,',
'even if `targetcalendar` is provided.'
].join(' '))
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/plot_api/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,13 @@ exports.cleanData = function(data, existingData) {
transform.target = transform.filtersrc;
delete transform.filtersrc;
}

if(transform.calendar) {
if(!transform.valuecalendar) {
transform.valuecalendar = transform.calendar;
}
delete transform.calendar;
}
}
}
}
Expand Down
22 changes: 18 additions & 4 deletions src/transforms/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ exports.supplyDefaults = function(transformIn) {
coerce('target');

var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleDefaults');
handleCalendarDefaults(transformIn, transformOut, 'calendar', null);
handleCalendarDefaults(transformIn, transformOut, 'valuecalendar', null);
handleCalendarDefaults(transformIn, transformOut, 'targetcalendar', null);
}

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

if(!len) return;

var targetCalendar = Lib.nestedProperty(trace, target + 'calendar').get(),
dataToCoord = getDataToCoordFunc(gd, trace, target),
var targetCalendar = opts.targetcalendar;

// even if you provide targetcalendar, if target is a string and there
// is a calendar attribute matching target it will get used instead.
if(typeof target === 'string') {
var attrTargetCalendar = Lib.nestedProperty(trace, target + 'calendar').get();
if(attrTargetCalendar) targetCalendar = attrTargetCalendar;
}

// if target points to an axis, use the type we already have for that
// axis to find the data type. Otherwise use the values to autotype.
var d2cTarget = (target === 'x' || target === 'y' || target === 'z') ?
target : filterArray;

var dataToCoord = getDataToCoordFunc(gd, trace, d2cTarget),
filterFunc = getFilterFunc(opts, dataToCoord, targetCalendar),
arrayAttrs = PlotSchema.findArrayAttributes(trace),
originalArrays = {};
Expand Down Expand Up @@ -226,7 +240,7 @@ function getFilterFunc(opts, d2c, targetCalendar) {
return array.indexOf(operation) !== -1;
}

var d2cValue = function(v) { return d2c(v, 0, opts.calendar); },
var d2cValue = function(v) { return d2c(v, 0, opts.valuecalendar); },
d2cTarget = function(v) { return d2c(v, 0, targetCalendar); };

var coercedValue;
Expand Down
2 changes: 1 addition & 1 deletion test/image/mocks/world-cals.json
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@
{
"type": "filter",
"operation": "[]",
"calendar": "jalali",
"valuecalendar": "jalali",
"value": [
"0818-08",
"0819-06"
Expand Down
30 changes: 30 additions & 0 deletions test/jasmine/tests/plot_api_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,36 @@ describe('Test plot api', function() {
expect(trace1.transforms[0].target).toEqual('y');
});

it('should rename *calendar* to *valuecalendar* in filter transforms', function() {
var data = [{
transforms: [{
type: 'filter',
target: 'y',
calendar: 'hebrew'
}, {
type: 'filter',
operation: '<'
}]
}, {
transforms: [{
type: 'filter',
valuecalendar: 'jalali'
}]
}];

Plotly.plot(gd, data);

var trace0 = gd.data[0],
trace1 = gd.data[1];

expect(trace0.transforms.length).toEqual(2);
expect(trace0.transforms[0].calendar).toBeUndefined();
expect(trace0.transforms[0].valuecalendar).toEqual('hebrew');

expect(trace1.transforms.length).toEqual(1);
expect(trace1.transforms[0].valuecalendar).toEqual('jalali');
});

it('should cleanup annotations / shapes refs', function() {
var data = [{}];

Expand Down
3 changes: 2 additions & 1 deletion test/jasmine/tests/plotschema_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ describe('plot schema', function() {
expect(plotSchema.layout.layoutAttributes.xaxis.calendar.valType).toEqual('enumerated');
expect(plotSchema.layout.layoutAttributes.scene.xaxis.calendar.valType).toEqual('enumerated');

expect(plotSchema.transforms.filter.attributes.calendar.valType).toEqual('enumerated');
expect(plotSchema.transforms.filter.attributes.valuecalendar.valType).toEqual('enumerated');
expect(plotSchema.transforms.filter.attributes.targetcalendar.valType).toEqual('enumerated');
});

it('should list correct defs', function() {
Expand Down
69 changes: 69 additions & 0 deletions test/jasmine/tests/transform_filter_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,75 @@ describe('filter transforms calc:', function() {
expect(out[0].z).toEqual(['2016-10-21', '2016-12-02']);
});

it('should use the calendar from the target attribute if target is a string', function() {
// this is the same data as in "filters should handle 3D *z* data"
// but with different calendars
var out = _transform([Lib.extendDeep({}, base, {
type: 'scatter3d',
// the same array as above but in nanakshahi dates
z: ['0547-05-05', '0548-05-17', '0548-06-17', '0548-08-07', '0548-09-19'],
zcalendar: 'nanakshahi',
transforms: [{
type: 'filter',
operation: '>',
value: '5776-06-28',
valuecalendar: 'hebrew',
target: 'z',
// targetcalendar is ignored!
targetcalendar: 'taiwan'
}]
})]);

expect(out[0].x).toEqual([0, 1]);
expect(out[0].y).toEqual([1, 2]);
expect(out[0].z).toEqual(['0548-08-07', '0548-09-19']);
});

it('should use targetcalendar anyway if there is no matching calendar attribute', function() {
// this is the same data as in "filters should handle 3D *z* data"
// but with different calendars
var out = _transform([Lib.extendDeep({}, base, {
type: 'scatter',
// the same array as above but in taiwanese dates
text: ['0104-07-20', '0105-08-01', '0105-09-01', '0105-10-21', '0105-12-02'],
transforms: [{
type: 'filter',
operation: '>',
value: '5776-06-28',
valuecalendar: 'hebrew',
target: 'text',
targetcalendar: 'taiwan'
}]
})]);

expect(out[0].x).toEqual([0, 1]);
expect(out[0].y).toEqual([1, 2]);
expect(out[0].text).toEqual(['0105-10-21', '0105-12-02']);
});

it('should use targetcalendar if target is an array', function() {
// this is the same data as in "filters should handle 3D *z* data"
// but with different calendars
var out = _transform([Lib.extendDeep({}, base, {
type: 'scatter3d',
// the same array as above but in nanakshahi dates
z: ['0547-05-05', '0548-05-17', '0548-06-17', '0548-08-07', '0548-09-19'],
zcalendar: 'nanakshahi',
transforms: [{
type: 'filter',
operation: '>',
value: '5776-06-28',
valuecalendar: 'hebrew',
target: ['0104-07-20', '0105-08-01', '0105-09-01', '0105-10-21', '0105-12-02'],
targetcalendar: 'taiwan'
}]
})]);

expect(out[0].x).toEqual([0, 1]);
expect(out[0].y).toEqual([1, 2]);
expect(out[0].z).toEqual(['0548-08-07', '0548-09-19']);
});

it('filters should handle geographical *lon* data', function() {
var trace0 = {
type: 'scattergeo',
Expand Down