Skip to content

Commit cbac3ae

Browse files
committed
issue 5195 - implement convertnumeric for axes and inherit from layout
1 parent 0fce11b commit cbac3ae

17 files changed

+72
-18
lines changed

src/plots/cartesian/axes.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ var getDataConversions = axes.getDataConversions = function(gd, trace, target, t
216216
// setup the data-to-calc method.
217217
if(Array.isArray(d2cTarget)) {
218218
ax = {
219-
type: autoType(targetArray),
219+
type: autoType(targetArray, undefined, {
220+
convertNumeric: gd._fullLayout.axesconvertnumeric
221+
}),
220222
_categories: []
221223
};
222224
axes.setConvert(ax);

src/plots/cartesian/axis_autotype.js

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

1717
module.exports = function autoType(array, calendar, opts) {
18-
opts = opts || {};
18+
var convertNumeric = opts.convertNumeric;
1919

2020
if(!opts.noMultiCategory && multiCategory(array)) return 'multicategory';
21-
if(moreDates(array, calendar)) return 'date';
21+
if(moreDates(array, calendar, convertNumeric)) return 'date';
2222
if(category(array)) return 'category';
23-
if(linearOK(array)) return 'linear';
23+
if(linearOK(array, convertNumeric)) return 'linear';
2424
else return '-';
2525
};
2626

27+
function hasTypeNumber(v, convertNumeric) {
28+
return convertNumeric ? isNumeric(v) : typeof v === 'number';
29+
}
30+
2731
// is there at least one number in array? If not, we should leave
2832
// ax.type empty so it can be autoset later
29-
function linearOK(array) {
33+
function linearOK(array, convertNumeric) {
3034
if(!array) return false;
3135

3236
for(var i = 0; i < array.length; i++) {
33-
if(isNumeric(array[i])) return true;
37+
if(hasTypeNumber(array[i], convertNumeric)) return true;
3438
}
3539

3640
return false;
@@ -42,7 +46,7 @@ function linearOK(array) {
4246
// dates as non-dates, to exclude cases with mostly 2 & 4 digit
4347
// numbers and a few dates
4448
// as with categories, consider DISTINCT values only.
45-
function moreDates(a, calendar) {
49+
function moreDates(a, calendar, convertNumeric) {
4650
// test at most 1000 points, evenly spaced
4751
var inc = Math.max(1, (a.length - 1) / 1000);
4852
var dcnt = 0;
@@ -56,7 +60,7 @@ function moreDates(a, calendar) {
5660
seen[stri] = 1;
5761

5862
if(Lib.isDateTime(ai, calendar)) dcnt += 1;
59-
if(isNumeric(ai)) ncnt += 1;
63+
if(hasTypeNumber(ai, convertNumeric)) ncnt += 1;
6064
}
6165

6266
return (dcnt > ncnt * 2);

src/plots/cartesian/layout_attributes.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,21 @@ module.exports = {
9797
_noTemplating: true,
9898
description: [
9999
'Sets the axis type.',
100-
'By default, plotly attempts to determined the axis type',
100+
'By default, plotly.js attempts to determined the axis type',
101101
'by looking into the data of the traces that referenced',
102102
'the axis in question.'
103103
].join(' ')
104104
},
105+
convertnumeric: {
106+
valType: 'boolean',
107+
role: 'info',
108+
editType: 'calc',
109+
description: [
110+
'Determines whether or not a numeric string in this axis may be',
111+
'treated as a number during automatic axis `type` detection.',
112+
'Defaults to layout.axesconvertnumeric.'
113+
].join(' ')
114+
},
105115
autorange: {
106116
valType: 'enumerated',
107117
values: [true, false, 'reversed'],

src/plots/cartesian/layout_defaults.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ function appendList(cont, k, item) {
3838
}
3939

4040
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
41+
var convertnumericDflt = layoutOut.axesconvertnumeric;
42+
4143
var ax2traces = {};
4244
var xaMayHide = {};
4345
var yaMayHide = {};
@@ -246,6 +248,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
246248
automargin: true,
247249
visibleDflt: visibleDflt,
248250
reverseDflt: reverseDflt,
251+
convertnumericDflt: convertnumericDflt,
249252
splomStash: ((layoutOut._splomAxes || {})[axLetter] || {})[axId]
250253
};
251254

@@ -310,6 +313,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
310313
automargin: true,
311314
visibleDflt: false,
312315
reverseDflt: false,
316+
convertnumericDflt: convertnumericDflt,
313317
splomStash: ((layoutOut._splomAxes || {})[axLetter] || {})[axId]
314318
};
315319

src/plots/cartesian/type_defaults.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var autoType = require('./axis_autotype');
1616
* name: axis object name (ie 'xaxis') if one should be stored
1717
*/
1818
module.exports = function handleTypeDefaults(containerIn, containerOut, coerce, options) {
19+
coerce('convertnumeric', options.convertnumericDflt);
1920
var axType = coerce('type', (options.splomStash || {}).type);
2021

2122
if(axType === '-') {
@@ -68,6 +69,8 @@ function setAutoType(ax, data) {
6869
opts.noMultiCategory = true;
6970
}
7071

72+
opts.convertNumeric = ax.convertnumeric;
73+
7174
// check all boxes on this x axis to see
7275
// if they're dates, numbers, or categories
7376
if(isBoxWithoutPositionCoords(d0, axLetter)) {

src/plots/gl3d/layout/axis_attributes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ module.exports = overrideAll({
7878
type: extendFlat({}, axesAttrs.type, {
7979
values: ['-', 'linear', 'log', 'date', 'category']
8080
}),
81+
convertnumeric: axesAttrs.convertnumeric,
8182
autorange: axesAttrs.autorange,
8283
rangemode: axesAttrs.rangemode,
8384
range: extendFlat({}, axesAttrs.range, {

src/plots/gl3d/layout/defaults.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
4040
font: layoutOut.font,
4141
fullData: fullData,
4242
getDfltFromLayout: getDfltFromLayout,
43+
convertnumericDflt: layoutOut.axesconvertnumeric,
4344
paper_bgcolor: layoutOut.paper_bgcolor,
4445
calendar: layoutOut.calendar
4546
});
@@ -109,6 +110,7 @@ function handleGl3dDefaults(sceneLayoutIn, sceneLayoutOut, coerce, opts) {
109110
data: fullGl3dData,
110111
bgColor: bgColorCombined,
111112
calendar: opts.calendar,
113+
convertnumericDflt: opts.convertnumericDflt,
112114
fullLayout: opts.fullLayout
113115
});
114116

src/plots/layout_attributes.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ module.exports = {
292292
'Sets the background color of the plotting area in-between x and y axes.'
293293
].join(' ')
294294
},
295+
axesconvertnumeric: {
296+
valType: 'boolean',
297+
dflt: true,
298+
role: 'info',
299+
editType: 'calc',
300+
description: [
301+
'Determines whether or not a numeric string in axes of the plot may be',
302+
'treated as a number during automatic axis `type` detection.'
303+
].join(' ')
304+
},
295305
separators: {
296306
valType: 'string',
297307
role: 'style',

src/plots/plots.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,8 @@ plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut, formatObj) {
14761476
layoutOut._dataTemplate = template.data;
14771477
}
14781478

1479+
coerce('axesconvertnumeric');
1480+
14791481
var globalFont = Lib.coerceFont(coerce, 'font');
14801482

14811483
coerce('title.text', layoutOut._dfltTitle.plot);

src/plots/polar/layout_attributes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ var radialAxisAttrs = {
6161
type: extendFlat({}, axesAttrs.type, {
6262
values: ['-', 'linear', 'log', 'date', 'category']
6363
}),
64+
convertnumeric: axesAttrs.convertnumeric,
6465

6566
autorange: extendFlat({}, axesAttrs.autorange, {editType: 'plot'}),
6667
rangemode: {

0 commit comments

Comments
 (0)