@@ -16,6 +16,7 @@ var Lib = require('../../lib');
16
16
var cleanNumber = Lib . cleanNumber ;
17
17
var ms2DateTime = Lib . ms2DateTime ;
18
18
var dateTime2ms = Lib . dateTime2ms ;
19
+ var ensureNumber = Lib . ensureNumber ;
19
20
20
21
var numConstants = require ( '../../constants/numerical' ) ;
21
22
var FP_SAFE = numConstants . FP_SAFE ;
@@ -28,13 +29,6 @@ function fromLog(v) {
28
29
return Math . pow ( 10 , v ) ;
29
30
}
30
31
31
- function num ( v ) {
32
- if ( ! isNumeric ( v ) ) return BADNUM ;
33
- v = Number ( v ) ;
34
- if ( v < - FP_SAFE || v > FP_SAFE ) return BADNUM ;
35
- return isNumeric ( v ) ? Number ( v ) : BADNUM ;
36
- }
37
-
38
32
/**
39
33
* Define the conversion functions for an axis data is used in 5 ways:
40
34
*
@@ -152,7 +146,7 @@ module.exports = function setConvert(ax, fullLayout) {
152
146
if ( index !== undefined ) return index ;
153
147
}
154
148
155
- if ( typeof v === 'number' ) { return v ; }
149
+ if ( isNumeric ( v ) ) return + v ;
156
150
}
157
151
158
152
function l2p ( v ) {
@@ -165,8 +159,8 @@ module.exports = function setConvert(ax, fullLayout) {
165
159
function p2l ( px ) { return ( px - ax . _b ) / ax . _m ; }
166
160
167
161
// conversions among c/l/p are fairly simple - do them together for all axis types
168
- ax . c2l = ( ax . type === 'log' ) ? toLog : num ;
169
- ax . l2c = ( ax . type === 'log' ) ? fromLog : num ;
162
+ ax . c2l = ( ax . type === 'log' ) ? toLog : ensureNumber ;
163
+ ax . l2c = ( ax . type === 'log' ) ? fromLog : ensureNumber ;
170
164
171
165
ax . l2p = l2p ;
172
166
ax . p2l = p2l ;
@@ -182,18 +176,20 @@ module.exports = function setConvert(ax, fullLayout) {
182
176
if ( [ 'linear' , '-' ] . indexOf ( ax . type ) !== - 1 ) {
183
177
// all are data vals, but d and r need cleaning
184
178
ax . d2r = ax . r2d = ax . d2c = ax . r2c = ax . d2l = ax . r2l = cleanNumber ;
185
- ax . c2d = ax . c2r = ax . l2d = ax . l2r = num ;
179
+ ax . c2d = ax . c2r = ax . l2d = ax . l2r = ensureNumber ;
186
180
187
181
ax . d2p = ax . r2p = function ( v ) { return ax . l2p ( cleanNumber ( v ) ) ; } ;
188
182
ax . p2d = ax . p2r = p2l ;
183
+
184
+ ax . cleanPos = ensureNumber ;
189
185
}
190
186
else if ( ax . type === 'log' ) {
191
187
// d and c are data vals, r and l are logged (but d and r need cleaning)
192
188
ax . d2r = ax . d2l = function ( v , clip ) { return toLog ( cleanNumber ( v ) , clip ) ; } ;
193
189
ax . r2d = ax . r2c = function ( v ) { return fromLog ( cleanNumber ( v ) ) ; } ;
194
190
195
191
ax . d2c = ax . r2l = cleanNumber ;
196
- ax . c2d = ax . l2r = num ;
192
+ ax . c2d = ax . l2r = ensureNumber ;
197
193
198
194
ax . c2r = toLog ;
199
195
ax . l2d = fromLog ;
@@ -203,6 +199,8 @@ module.exports = function setConvert(ax, fullLayout) {
203
199
204
200
ax . r2p = function ( v ) { return ax . l2p ( cleanNumber ( v ) ) ; } ;
205
201
ax . p2r = p2l ;
202
+
203
+ ax . cleanPos = ensureNumber ;
206
204
}
207
205
else if ( ax . type === 'date' ) {
208
206
// r and d are date strings, l and c are ms
@@ -222,24 +220,31 @@ module.exports = function setConvert(ax, fullLayout) {
222
220
223
221
ax . d2p = ax . r2p = function ( v , _ , calendar ) { return ax . l2p ( dt2ms ( v , 0 , calendar ) ) ; } ;
224
222
ax . p2d = ax . p2r = function ( px , r , calendar ) { return ms2dt ( p2l ( px ) , r , calendar ) ; } ;
223
+
224
+ ax . cleanPos = function ( v ) { return Lib . cleanDate ( v , BADNUM , ax . calendar ) ; } ;
225
225
}
226
226
else if ( ax . type === 'category' ) {
227
- // d is categories; r, c, and l are indices
228
- // TODO: should r accept category names too?
229
- // ie r2c and r2l would be getCategoryIndex (and r2p would change)
227
+ // d is categories (string)
228
+ // c and l are indices (numbers)
229
+ // r is categories or numbers
230
230
231
- ax . d2r = ax . d2c = ax . d2l = setCategoryIndex ;
231
+ ax . d2c = ax . d2l = setCategoryIndex ;
232
232
ax . r2d = ax . c2d = ax . l2d = getCategoryName ;
233
233
234
- // special d2l variant that won't add categories
235
- ax . d2l_noadd = getCategoryIndex ;
234
+ ax . d2r = ax . d2l_noadd = getCategoryIndex ;
236
235
237
- ax . r2l = ax . l2r = ax . r2c = ax . c2r = num ;
236
+ ax . l2r = ax . r2c = ax . c2r = ensureNumber ;
237
+ ax . r2l = getCategoryIndex ;
238
238
239
239
ax . d2p = function ( v ) { return ax . l2p ( getCategoryIndex ( v ) ) ; } ;
240
240
ax . p2d = function ( px ) { return getCategoryName ( p2l ( px ) ) ; } ;
241
- ax . r2p = ax . l2p ;
241
+ ax . r2p = ax . d2p ;
242
242
ax . p2r = p2l ;
243
+
244
+ ax . cleanPos = function ( v ) {
245
+ if ( typeof v === 'string' && v !== '' ) return v ;
246
+ return ensureNumber ( v ) ;
247
+ } ;
243
248
}
244
249
245
250
// find the range value at the specified (linear) fraction of the axis
0 commit comments