Skip to content

Commit 823375c

Browse files
committed
mv getDataToCoordFunc in axes.js
- use it filter and sort modules.
1 parent 1a21ad9 commit 823375c

File tree

3 files changed

+49
-107
lines changed

3 files changed

+49
-107
lines changed

src/plots/cartesian/axes.js

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ var ONEMIN = constants.ONEMIN;
2929
var ONESEC = constants.ONESEC;
3030
var BADNUM = constants.BADNUM;
3131

32-
3332
var axes = module.exports = {};
3433

3534
axes.layoutAttributes = require('./layout_attributes');
3635
axes.supplyLayoutDefaults = require('./layout_defaults');
3736

3837
axes.setConvert = require('./set_convert');
38+
var autoType = require('./axis_autotype');
3939

4040
var axisIds = require('./axis_ids');
4141
axes.id2name = axisIds.id2name;
@@ -45,7 +45,6 @@ axes.listIds = axisIds.listIds;
4545
axes.getFromId = axisIds.getFromId;
4646
axes.getFromTrace = axisIds.getFromTrace;
4747

48-
4948
/*
5049
* find the list of possible axes to reference with an xref or yref attribute
5150
* and coerce it to that list
@@ -130,6 +129,48 @@ axes.coercePosition = function(containerOut, gd, coerce, axRef, attr, dflt) {
130129
containerOut[attr] = isNumeric(pos) ? Number(pos) : dflt;
131130
};
132131

132+
axes.getDataToCoordFunc = function(gd, trace, target, targetArray) {
133+
var ax;
134+
135+
// If target points to an axis, use the type we already have for that
136+
// axis to find the data type. Otherwise use the values to autotype.
137+
var d2cTarget = (target === 'x' || target === 'y' || target === 'z') ?
138+
target :
139+
targetArray;
140+
141+
// In the case of an array target, make a mock data array
142+
// and call supplyDefaults to the data type and
143+
// setup the data-to-calc method.
144+
if(Array.isArray(d2cTarget)) {
145+
ax = {
146+
type: autoType(targetArray),
147+
_categories: []
148+
};
149+
axes.setConvert(ax);
150+
151+
// build up ax._categories (usually done during ax.makeCalcdata()
152+
if(ax.type === 'category') {
153+
for(var i = 0; i < targetArray.length; i++) {
154+
ax.d2c(targetArray[i]);
155+
}
156+
}
157+
} else {
158+
ax = axes.getFromTrace(gd, trace, d2cTarget);
159+
}
160+
161+
// if 'target' has corresponding axis
162+
// -> use setConvert method
163+
if(ax) return ax.d2c;
164+
165+
// special case for 'ids'
166+
// -> cast to String
167+
if(d2cTarget === 'ids') return function(v) { return String(v); };
168+
169+
// otherwise (e.g. numeric-array of 'marker.color' or 'marker.size')
170+
// -> cast to Number
171+
return function(v) { return +v; };
172+
};
173+
133174
// empty out types for all axes containing these traces
134175
// so we auto-set them again
135176
axes.clearTypes = function(gd, traces) {

src/transforms/filter.js

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
var Lib = require('../lib');
1212
var Registry = require('../registry');
1313
var PlotSchema = require('../plot_api/plot_schema');
14-
var axisIds = require('../plots/cartesian/axis_ids');
15-
var autoType = require('../plots/cartesian/axis_autotype');
16-
var setConvert = require('../plots/cartesian/set_convert');
14+
var Axes = require('../plots/cartesian/axes');
1715

1816
var COMPARISON_OPS = ['=', '!=', '<', '>=', '>', '<='];
1917
var INTERVAL_OPS = ['[]', '()', '[)', '(]', '][', ')(', '](', ')['];
@@ -159,13 +157,8 @@ exports.calcTransform = function(gd, trace, opts) {
159157
if(attrTargetCalendar) targetCalendar = attrTargetCalendar;
160158
}
161159

162-
// if target points to an axis, use the type we already have for that
163-
// axis to find the data type. Otherwise use the values to autotype.
164-
var d2cTarget = (target === 'x' || target === 'y' || target === 'z') ?
165-
target : filterArray;
166-
167-
var dataToCoord = getDataToCoordFunc(gd, trace, d2cTarget);
168-
var filterFunc = getFilterFunc(opts, dataToCoord, targetCalendar);
160+
var d2c = Axes.getDataToCoordFunc(gd, trace, target, targetArray);
161+
var filterFunc = getFilterFunc(opts, d2c, targetCalendar);
169162
var arrayAttrs = PlotSchema.findArrayAttributes(trace);
170163
var originalArrays = {};
171164

@@ -203,60 +196,11 @@ exports.calcTransform = function(gd, trace, opts) {
203196

204197
// loop through filter array, fill trace arrays if passed
205198
for(var i = 0; i < len; i++) {
206-
var passed = filterFunc(filterArray[i]);
199+
var passed = filterFunc(targetArray[i]);
207200
if(passed) forAllAttrs(fillFn, i);
208201
}
209202
};
210203

211-
function getFilterArray(trace, target) {
212-
if(typeof target === 'string' && target) {
213-
var array = Lib.nestedProperty(trace, target).get();
214-
215-
return Array.isArray(array) ? array : [];
216-
}
217-
else if(Array.isArray(target)) return target.slice();
218-
219-
return false;
220-
}
221-
222-
function getDataToCoordFunc(gd, trace, target) {
223-
var ax;
224-
225-
// In the case of an array target, make a mock data array
226-
// and call supplyDefaults to the data type and
227-
// setup the data-to-calc method.
228-
if(Array.isArray(target)) {
229-
ax = {
230-
type: autoType(target),
231-
_categories: []
232-
};
233-
234-
setConvert(ax);
235-
236-
if(ax.type === 'category') {
237-
// build up ax._categories (usually done during ax.makeCalcdata()
238-
for(var i = 0; i < target.length; i++) {
239-
ax.d2c(target[i]);
240-
}
241-
}
242-
}
243-
else {
244-
ax = axisIds.getFromTrace(gd, trace, target);
245-
}
246-
247-
// if 'target' has corresponding axis
248-
// -> use setConvert method
249-
if(ax) return ax.d2c;
250-
251-
// special case for 'ids'
252-
// -> cast to String
253-
if(target === 'ids') return function(v) { return String(v); };
254-
255-
// otherwise (e.g. numeric-array of 'marker.color' or 'marker.size')
256-
// -> cast to Number
257-
return function(v) { return +v; };
258-
}
259-
260204
function getFilterFunc(opts, d2c, targetCalendar) {
261205
var operation = opts.operation,
262206
value = opts.value,

src/transforms/sort.js

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
var Lib = require('../lib');
1212
var PlotSchema = require('../plot_api/plot_schema');
13-
var axisIds = require('../plots/cartesian/axis_ids');
14-
var autoType = require('../plots/cartesian/axis_autotype');
15-
var setConvert = require('../plots/cartesian/set_convert');
13+
var Axes = require('../plots/cartesian/axes');
1614

1715
exports.moduleType = 'transform';
1816

@@ -82,7 +80,7 @@ exports.calcTransform = function(gd, trace, opts) {
8280
if(!len) return;
8381

8482
var arrayAttrs = PlotSchema.findArrayAttributes(trace);
85-
var d2c = getDataToCoordFunc(gd, trace, target, targetArray);
83+
var d2c = Axes.getDataToCoordFunc(gd, trace, target, targetArray);
8684
var indices = getIndices(opts, targetArray, d2c);
8785

8886
for(var i = 0; i < arrayAttrs.length; i++) {
@@ -98,47 +96,6 @@ exports.calcTransform = function(gd, trace, opts) {
9896
}
9997
};
10098

101-
// TODO reuse for filter.js
102-
function getDataToCoordFunc(gd, trace, target, targetArray) {
103-
var ax;
104-
105-
// If target points to an axis, use the type we already have for that
106-
// axis to find the data type. Otherwise use the values to autotype.
107-
if(target === 'x' || target === 'y' || target === 'z') {
108-
ax = axisIds.getFromTrace(gd, trace, target);
109-
}
110-
// In the case of an array target, make a mock data array
111-
// and call supplyDefaults to the data type and
112-
// setup the data-to-calc method.
113-
else if(Array.isArray(target)) {
114-
ax = {
115-
type: autoType(targetArray),
116-
// TODO does this still work with the new hash object
117-
_categories: []
118-
};
119-
setConvert(ax);
120-
121-
// build up ax._categories (usually done during ax.makeCalcdata()
122-
if(ax.type === 'category') {
123-
for(var i = 0; i < targetArray.length; i++) {
124-
ax.d2c(targetArray[i]);
125-
}
126-
}
127-
}
128-
129-
// if 'target' has corresponding axis
130-
// -> use setConvert method
131-
if(ax) return ax.d2c;
132-
133-
// special case for 'ids'
134-
// -> cast to String
135-
if(target === 'ids') return function(v) { return String(v); };
136-
137-
// otherwise (e.g. numeric-array of 'marker.color' or 'marker.size')
138-
// -> cast to Number
139-
return function(v) { return +v; };
140-
}
141-
14299
function getIndices(opts, targetArray, d2c) {
143100
var len = targetArray.length;
144101
var indices = new Array(len);

0 commit comments

Comments
 (0)