Skip to content

Commit 491fa99

Browse files
committed
move id-to-name, get and clean utils to utils.js
1 parent 0ea1d9b commit 491fa99

File tree

2 files changed

+135
-110
lines changed

2 files changed

+135
-110
lines changed

src/plots/cartesian/axes.js

Lines changed: 7 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ var axes = module.exports = {};
1818
axes.layoutAttributes = require('./layout_attributes');
1919

2020

21+
var utils = require('./utils');
22+
axes.id2name = utils.id2name;
23+
axes.cleanId = utils.cleanId;
24+
axes.list = utils.list;
25+
axes.listIds = utils.listIds;
26+
axes.getFromId = utils.getFromId;
27+
axes.getFromTrace = utils.getFromTrace;
2128
axes.supplyLayoutDefaults = function(layoutIn, layoutOut, fullData) {
2229
// get the full list of axes already defined
2330
var layoutKeys = Object.keys(layoutIn),
@@ -366,41 +373,6 @@ axes.clearTypes = function(gd, traces) {
366373
});
367374
};
368375

369-
// convert between axis names (xaxis, xaxis2, etc, elements of td.layout)
370-
// and axis id's (x, x2, etc). Would probably have ditched 'xaxis'
371-
// completely in favor of just 'x' if it weren't ingrained in the API etc.
372-
axes.id2name = function(id) {
373-
if(typeof id !== 'string' || !id.match(AX_ID_PATTERN)) return;
374-
var axNum = id.substr(1);
375-
if(axNum==='1') axNum = '';
376-
return id.charAt(0) + 'axis' + axNum;
377-
};
378-
379-
axes.name2id = function(name) {
380-
if(!name.match(AX_NAME_PATTERN)) return;
381-
var axNum = name.substr(5);
382-
if(axNum==='1') axNum = '';
383-
return name.charAt(0)+axNum;
384-
};
385-
386-
axes.cleanId = function(id, axLetter) {
387-
if(!id.match(AX_ID_PATTERN)) return;
388-
if(axLetter && id.charAt(0)!==axLetter) return;
389-
390-
var axNum = id.substr(1).replace(/^0+/,'');
391-
if(axNum==='1') axNum = '';
392-
return id.charAt(0) + axNum;
393-
};
394-
395-
axes.cleanName = function(name, axLetter) {
396-
if(!name.match(AX_ID_PATTERN)) return;
397-
if(axLetter && name.charAt(0)!==axLetter) return;
398-
399-
var axNum = name.substr(5).replace(/^0+/,'');
400-
if(axNum==='1') axNum = '';
401-
return name.charAt(0) + 'axis' + axNum;
402-
};
403-
404376
// get counteraxis letter for this axis (name or id)
405377
// this can also be used as the id for default counter axis
406378
axes.counterLetter = function(id) {
@@ -1862,81 +1834,6 @@ function numSeparate(nStr, separators) {
18621834
return x1 + x2;
18631835
}
18641836

1865-
// get all axis object names
1866-
// optionally restricted to only x or y or z by string axLetter
1867-
// and optionally 2D axes only, not those inside 3D scenes
1868-
function listNames(td, axLetter, only2d) {
1869-
var fullLayout = td._fullLayout;
1870-
if(!fullLayout) return [];
1871-
1872-
function filterAxis(obj, extra) {
1873-
var keys = Object.keys(obj),
1874-
axMatch = /^[xyz]axis[0-9]*/,
1875-
out = [];
1876-
1877-
for(var i = 0; i < keys.length; i++) {
1878-
var k = keys[i];
1879-
if(axLetter && k.charAt(0) !== axLetter) continue;
1880-
if(axMatch.test(k)) out.push(extra + k);
1881-
}
1882-
1883-
return out.sort();
1884-
}
1885-
1886-
var names = filterAxis(fullLayout, '');
1887-
if(only2d) return names;
1888-
1889-
var sceneIds3D = Plotly.Plots.getSubplotIds(fullLayout, 'gl3d') || [];
1890-
for(var i = 0; i < sceneIds3D.length; i++) {
1891-
var sceneId = sceneIds3D[i];
1892-
names = names.concat(
1893-
filterAxis(fullLayout[sceneId], sceneId + '.')
1894-
);
1895-
}
1896-
1897-
return names;
1898-
}
1899-
1900-
// get all axis objects, as restricted in listNames
1901-
axes.list = function(td, axletter, only2d) {
1902-
return listNames(td, axletter, only2d)
1903-
.map(function(axName) {
1904-
return Plotly.Lib.nestedProperty(td._fullLayout, axName).get();
1905-
});
1906-
};
1907-
1908-
// get all axis ids, optionally restricted by letter
1909-
// this only makes sense for 2d axes
1910-
axes.listIds = function(td, axletter) {
1911-
return listNames(td, axletter, true).map(axes.name2id);
1912-
};
1913-
1914-
// get an axis object from its id 'x','x2' etc
1915-
// optionally, id can be a subplot (ie 'x2y3') and type gets x or y from it
1916-
axes.getFromId = function(td, id, type) {
1917-
var fullLayout = td._fullLayout;
1918-
1919-
if(type==='x') id = id.replace(/y[0-9]*/,'');
1920-
else if(type==='y') id = id.replace(/x[0-9]*/,'');
1921-
1922-
return fullLayout[axes.id2name(id)];
1923-
};
1924-
1925-
// get an axis object of specified type from the containing trace
1926-
axes.getFromTrace = function(td, fullTrace, type) {
1927-
var fullLayout = td._fullLayout;
1928-
var ax = null;
1929-
if (Plotly.Plots.traceIs(fullTrace, 'gl3d')) {
1930-
var scene = fullTrace.scene;
1931-
if (scene.substr(0,5)==='scene') {
1932-
ax = fullLayout[scene][type + 'axis'];
1933-
}
1934-
} else {
1935-
ax = axes.getFromId(td, fullTrace[type + 'axis'] || type);
1936-
}
1937-
1938-
return ax;
1939-
};
19401837

19411838
axes.subplotMatch = /^x([0-9]*)y([0-9]*)$/;
19421839

src/plots/cartesian/utils.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
'use strict';
10+
11+
var Plots = require('../plots');
12+
var Lib = require('../../lib');
13+
14+
var constants = require('./constants');
15+
16+
17+
// convert between axis names (xaxis, xaxis2, etc, elements of td.layout)
18+
// and axis id's (x, x2, etc). Would probably have ditched 'xaxis'
19+
// completely in favor of just 'x' if it weren't ingrained in the API etc.
20+
exports.id2name = function id2name(id) {
21+
if(typeof id !== 'string' || !id.match(constants.AX_ID_PATTERN)) return;
22+
var axNum = id.substr(1);
23+
if(axNum === '1') axNum = '';
24+
return id.charAt(0) + 'axis' + axNum;
25+
};
26+
27+
exports.name2id = function name2id(name) {
28+
if(!name.match(constants.AX_NAME_PATTERN)) return;
29+
var axNum = name.substr(5);
30+
if(axNum === '1') axNum = '';
31+
return name.charAt(0) + axNum;
32+
};
33+
34+
exports.cleanId = function cleanId(id, axLetter) {
35+
if(!id.match(constants.AX_ID_PATTERN)) return;
36+
if(axLetter && id.charAt(0) !== axLetter) return;
37+
38+
var axNum = id.substr(1).replace(/^0+/,'');
39+
if(axNum === '1') axNum = '';
40+
return id.charAt(0) + axNum;
41+
};
42+
43+
exports.cleanName = function cleanName(name, axLetter) {
44+
if(!name.match(constants.AX_ID_PATTERN)) return;
45+
if(axLetter && name.charAt(0) !== axLetter) return;
46+
47+
var axNum = name.substr(5).replace(/^0+/,'');
48+
if(axNum === '1') axNum = '';
49+
return name.charAt(0) + 'axis' + axNum;
50+
};
51+
52+
// get all axis object names
53+
// optionally restricted to only x or y or z by string axLetter
54+
// and optionally 2D axes only, not those inside 3D scenes
55+
function listNames(td, axLetter, only2d) {
56+
var fullLayout = td._fullLayout;
57+
if(!fullLayout) return [];
58+
59+
function filterAxis(obj, extra) {
60+
var keys = Object.keys(obj),
61+
axMatch = /^[xyz]axis[0-9]*/,
62+
out = [];
63+
64+
for(var i = 0; i < keys.length; i++) {
65+
var k = keys[i];
66+
if(axLetter && k.charAt(0) !== axLetter) continue;
67+
if(axMatch.test(k)) out.push(extra + k);
68+
}
69+
70+
return out.sort();
71+
}
72+
73+
var names = filterAxis(fullLayout, '');
74+
if(only2d) return names;
75+
76+
var sceneIds3D = Plots.getSubplotIds(fullLayout, 'gl3d') || [];
77+
for(var i = 0; i < sceneIds3D.length; i++) {
78+
var sceneId = sceneIds3D[i];
79+
names = names.concat(
80+
filterAxis(fullLayout[sceneId], sceneId + '.')
81+
);
82+
}
83+
84+
return names;
85+
}
86+
87+
// get all axis objects, as restricted in listNames
88+
exports.list = function(td, axletter, only2d) {
89+
return listNames(td, axletter, only2d)
90+
.map(function(axName) {
91+
return Lib.nestedProperty(td._fullLayout, axName).get();
92+
});
93+
};
94+
95+
// get all axis ids, optionally restricted by letter
96+
// this only makes sense for 2d axes
97+
exports.listIds = function(td, axletter) {
98+
return listNames(td, axletter, true).map(exports.name2id);
99+
};
100+
101+
// get an axis object from its id 'x','x2' etc
102+
// optionally, id can be a subplot (ie 'x2y3') and type gets x or y from it
103+
exports.getFromId = function(td, id, type) {
104+
var fullLayout = td._fullLayout;
105+
106+
if(type === 'x') id = id.replace(/y[0-9]*/,'');
107+
else if(type === 'y') id = id.replace(/x[0-9]*/,'');
108+
109+
return fullLayout[exports.id2name(id)];
110+
};
111+
112+
// get an axis object of specified type from the containing trace
113+
exports.getFromTrace = function(td, fullTrace, type) {
114+
var fullLayout = td._fullLayout;
115+
var ax = null;
116+
117+
if(Plots.traceIs(fullTrace, 'gl3d')) {
118+
var scene = fullTrace.scene;
119+
if(scene.substr(0, 5) === 'scene') {
120+
ax = fullLayout[scene][type + 'axis'];
121+
}
122+
}
123+
else {
124+
ax = exports.getFromId(td, fullTrace[type + 'axis'] || type);
125+
}
126+
127+
return ax;
128+
};

0 commit comments

Comments
 (0)