Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d218bab

Browse files
committed
add early return for dates - remove unnecessary early return in linear - and refactor
1 parent 0224fa0 commit d218bab

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/plots/cartesian/axis_autotype.js

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ var Lib = require('../../lib');
1515
var BADNUM = require('../../constants/numerical').BADNUM;
1616

1717
module.exports = function autoType(array, calendar, opts) {
18-
var convertNumeric = opts.autotypenumbers !== 'strict'; // compare against strict, just in case autotypenumbers was not provided in opts
19-
2018
if(!opts.noMultiCategory && multiCategory(array)) return 'multicategory';
2119
if(moreDates(array, calendar)) return 'date';
20+
21+
var convertNumeric = opts.autotypenumbers !== 'strict'; // compare against strict, just in case autotypenumbers was not provided in opts
2222
if(category(array, convertNumeric)) return 'category';
2323
if(linearOK(array, convertNumeric)) return 'linear';
24-
else return '-';
24+
25+
return '-';
2526
};
2627

2728
function hasTypeNumber(v, convertNumeric) {
@@ -30,11 +31,11 @@ function hasTypeNumber(v, convertNumeric) {
3031

3132
// is there at least one number in array? If not, we should leave
3233
// ax.type empty so it can be autoset later
33-
function linearOK(array, convertNumeric) {
34-
if(!array) return false;
34+
function linearOK(a, convertNumeric) {
35+
var len = a.length;
3536

36-
for(var i = 0; i < array.length; i++) {
37-
if(hasTypeNumber(array[i], convertNumeric)) return true;
37+
for(var i = 0; i < len; i++) {
38+
if(hasTypeNumber(a[i], convertNumeric)) return true;
3839
}
3940

4041
return false;
@@ -47,23 +48,31 @@ function linearOK(array, convertNumeric) {
4748
// numbers and a few dates
4849
// as with categories, consider DISTINCT values only.
4950
function moreDates(a, calendar) {
50-
// test at most 1000 points, evenly spaced
51-
var inc = Math.max(1, (a.length - 1) / 1000);
52-
var dcnt = 0;
53-
var ncnt = 0;
51+
var len = a.length;
52+
if(!len) return false;
53+
54+
var inc = getIncrement(len);
55+
var dats = 0;
56+
var nums = 0;
5457
var seen = {};
5558

56-
for(var i = 0; i < a.length; i += inc) {
57-
var ai = a[Math.round(i)];
59+
for(var f = 0; f < len; f += inc) {
60+
var i = Math.round(f);
61+
var ai = a[i];
5862
var stri = String(ai);
5963
if(seen[stri]) continue;
6064
seen[stri] = 1;
6165

62-
if(Lib.isDateTime(ai, calendar)) dcnt += 1;
63-
if(isNumeric(ai)) ncnt += 1;
66+
if(Lib.isDateTime(ai, calendar)) dats++;
67+
if(isNumeric(ai)) nums++;
6468
}
6569

66-
return (dcnt > ncnt * 2);
70+
return dats > nums * 2;
71+
}
72+
73+
// return increment to test at most 1000 points, evenly spaced
74+
function getIncrement(len) {
75+
return Math.max(1, (len - 1) / 1000);
6776
}
6877

6978
// are the (x,y)-values in gd.data mostly text?
@@ -72,10 +81,9 @@ function category(a, convertNumeric) {
7281
var len = a.length;
7382
if(!len) return false;
7483

75-
// test at most 1000 points
76-
var inc = Math.max(1, (len - 1) / 1000);
77-
var curvenums = 0;
78-
var curvecats = 0;
84+
var inc = getIncrement(len);
85+
var nums = 0;
86+
var cats = 0;
7987
var seen = {};
8088

8189
for(var f = 0; f < len; f += inc) {
@@ -86,12 +94,12 @@ function category(a, convertNumeric) {
8694
seen[stri] = 1;
8795

8896
var t = typeof ai;
89-
if(t === 'boolean') curvecats++;
90-
else if(convertNumeric ? Lib.cleanNumber(ai) !== BADNUM : t === 'number') curvenums++;
91-
else if(t === 'string') curvecats++;
97+
if(t === 'boolean') cats++;
98+
else if(convertNumeric ? Lib.cleanNumber(ai) !== BADNUM : t === 'number') nums++;
99+
else if(t === 'string') cats++;
92100
}
93101

94-
return curvecats > curvenums * 2;
102+
return cats > nums * 2;
95103
}
96104

97105
// very-loose requirements for multicategory,

0 commit comments

Comments
 (0)