Skip to content

Commit 82fbae2

Browse files
committed
layout.grid
1 parent f01db07 commit 82fbae2

25 files changed

+805
-42
lines changed

src/lib/regex.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88

99
'use strict';
1010

11-
// Simple helper functions
12-
// none of these need any external deps
13-
1411
/*
1512
* make a regex for matching counter ids/names ie xaxis, xaxis2, xaxis10...
16-
* eg: regexCounter('x')
17-
* tail is an optional piece after the id
18-
* eg regexCounter('scene', '.annotations') for scene2.annotations etc.
13+
*
14+
* @param {string} head: the head of the pattern, eg 'x' matches 'x', 'x2', 'x10' etc.
15+
* 'xy' is a special case for cartesian subplots: it matches 'x2y3' etc
16+
* @param {Optional(string)} tail: a fixed piece after the id
17+
* eg counterRegex('scene', '.annotations') for scene2.annotations etc.
18+
* @param {boolean} openEnded: if true, the string may continue past the match.
1919
*/
2020
exports.counter = function(head, tail, openEnded) {
21-
return new RegExp('^' + head + '([2-9]|[1-9][0-9]+)?' +
22-
(tail || '') + (openEnded ? '' : '$'));
21+
var fullTail = (tail || '') + (openEnded ? '' : '$');
22+
if(head === 'xy') {
23+
return new RegExp('^x([2-9]|[1-9][0-9]+)?y([2-9]|[1-9][0-9]+)?' + fullTail);
24+
}
25+
return new RegExp('^' + head + '([2-9]|[1-9][0-9]+)?' + fullTail);
2326
};

src/plots/cartesian/constants.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
'use strict';
10-
var counterRegex = require('../../lib').counterRegex;
10+
var counterRegex = require('../../lib/regex').counter;
1111

1212

1313
module.exports = {

src/plots/cartesian/layout_defaults.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
161161
var positioningOptions = {
162162
letter: axLetter,
163163
counterAxes: counterAxes[axLetter],
164-
overlayableAxes: overlayableAxes
164+
overlayableAxes: overlayableAxes,
165+
grid: layoutOut.grid
165166
};
166167

167168
handlePositionDefaults(axLayoutIn, axLayoutOut, coerce, positioningOptions);

src/plots/cartesian/position_defaults.js

+27-10
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,43 @@ var Lib = require('../../lib');
1515

1616

1717
module.exports = function handlePositionDefaults(containerIn, containerOut, coerce, options) {
18-
var counterAxes = options.counterAxes || [],
19-
overlayableAxes = options.overlayableAxes || [],
20-
letter = options.letter;
18+
var counterAxes = options.counterAxes || [];
19+
var overlayableAxes = options.overlayableAxes || [];
20+
var letter = options.letter;
21+
var grid = options.grid;
22+
23+
var dfltAnchor, dfltDomain, dfltSide, dfltPosition;
24+
25+
if(grid) {
26+
dfltDomain = grid._domains[letter][grid._axisMap[containerOut._id]];
27+
dfltAnchor = grid._anchors[containerOut._id];
28+
if(dfltDomain) {
29+
dfltSide = grid[letter + 'side'].split(' ')[0];
30+
dfltPosition = grid.domain[letter][dfltSide === 'right' || dfltSide === 'top' ? 1 : 0];
31+
}
32+
}
33+
34+
// Even if there's a grid, this axis may not be in it - fall back on non-grid defaults
35+
dfltDomain = dfltDomain || [0, 1];
36+
dfltAnchor = dfltAnchor || (isNumeric(containerIn.position) ? 'free' : (counterAxes[0] || 'free'));
37+
dfltSide = dfltSide || (letter === 'x' ? 'bottom' : 'left');
38+
dfltPosition = dfltPosition || 0;
2139

2240
var anchor = Lib.coerce(containerIn, containerOut, {
2341
anchor: {
2442
valType: 'enumerated',
2543
values: ['free'].concat(counterAxes),
26-
dflt: isNumeric(containerIn.position) ? 'free' :
27-
(counterAxes[0] || 'free')
44+
dflt: dfltAnchor
2845
}
2946
}, 'anchor');
3047

31-
if(anchor === 'free') coerce('position');
48+
if(anchor === 'free') coerce('position', dfltPosition);
3249

3350
Lib.coerce(containerIn, containerOut, {
3451
side: {
3552
valType: 'enumerated',
3653
values: letter === 'x' ? ['bottom', 'top'] : ['left', 'right'],
37-
dflt: letter === 'x' ? 'bottom' : 'left'
54+
dflt: dfltSide
3855
}
3956
}, 'side');
4057

@@ -54,9 +71,9 @@ module.exports = function handlePositionDefaults(containerIn, containerOut, coer
5471
// in ax.setscale()... but this means we still need (imperfect) logic
5572
// in the axes popover to hide domain for the overlaying axis.
5673
// perhaps I should make a private version _domain that all axes get???
57-
var domain = coerce('domain');
58-
if(domain[0] > domain[1] - 0.01) containerOut.domain = [0, 1];
59-
Lib.noneOrAll(containerIn.domain, containerOut.domain, [0, 1]);
74+
var domain = coerce('domain', dfltDomain);
75+
if(domain[0] > domain[1] - 0.01) containerOut.domain = dfltDomain;
76+
Lib.noneOrAll(containerIn.domain, containerOut.domain, dfltDomain);
6077
}
6178

6279
coerce('layer');

src/plots/domain_attributes.js renamed to src/plots/domain.js

+60-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ var extendFlat = require('../lib/extend').extendFlat;
2020
* opts.trace: set to true for trace containers
2121
* @param {string}
2222
* opts.editType: editType for all pieces
23+
* @param {boolean}
24+
* opts.noGridCell: set to true to omit `row` and `column`
2325
*
2426
* @param {object} extra
2527
* @param {string}
@@ -29,7 +31,7 @@ var extendFlat = require('../lib/extend').extendFlat;
2931
*
3032
* @return {object} attributes object containing {x,y} as specified
3133
*/
32-
module.exports = function(opts, extra) {
34+
exports.attributes = function(opts, extra) {
3335
opts = opts || {};
3436
extra = extra || {};
3537

@@ -48,7 +50,7 @@ module.exports = function(opts, extra) {
4850
var contPart = opts.trace ? 'trace ' : 'subplot ';
4951
var descPart = extra.description ? ' ' + extra.description : '';
5052

51-
return {
53+
var out = {
5254
x: extendFlat({}, base, {
5355
description: [
5456
'Sets the horizontal domain of this ',
@@ -69,4 +71,60 @@ module.exports = function(opts, extra) {
6971
}),
7072
editType: opts.editType
7173
};
74+
75+
if(!opts.noGridCell) {
76+
out.row = {
77+
valType: 'integer',
78+
min: 0,
79+
role: 'info',
80+
editType: opts.editType,
81+
description: [
82+
'If there is a layout grid, use the domain ',
83+
'for this row in the grid for this ',
84+
namePart,
85+
contPart,
86+
'.',
87+
descPart
88+
].join('')
89+
};
90+
out.column = {
91+
valType: 'integer',
92+
min: 0,
93+
role: 'info',
94+
editType: opts.editType,
95+
description: [
96+
'If there is a layout grid, use the domain ',
97+
'for this column in the grid for this ',
98+
namePart,
99+
contPart,
100+
'.',
101+
descPart
102+
].join('')
103+
};
104+
}
105+
106+
return out;
107+
};
108+
109+
exports.defaults = function(containerOut, layout, coerce, dfltDomains) {
110+
var dfltX = (dfltDomains && dfltDomains.x) || [0, 1];
111+
var dfltY = (dfltDomains && dfltDomains.y) || [0, 1];
112+
113+
var grid = layout.grid;
114+
if(grid) {
115+
var column = coerce('domain.column');
116+
if(column !== undefined) {
117+
if(column < grid.columns) dfltX = grid._domains.x[column];
118+
else delete containerOut.domain.column;
119+
}
120+
121+
var row = coerce('domain.row');
122+
if(row !== undefined) {
123+
if(row < grid.rows) dfltY = grid._domains.y[row];
124+
else delete containerOut.domain.row;
125+
}
126+
}
127+
128+
coerce('domain.x', dfltX);
129+
coerce('domain.y', dfltY);
72130
};

src/plots/geo/layout/layout_attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'use strict';
1010

1111
var colorAttrs = require('../../../components/color/attributes');
12-
var domainAttrs = require('../../domain_attributes');
12+
var domainAttrs = require('../../domain').attributes;
1313
var constants = require('../constants');
1414
var overrideAll = require('../../../plot_api/edit_types').overrideAll;
1515

src/plots/gl3d/layout/layout_attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
var gl3dAxisAttrs = require('./axis_attributes');
13-
var domainAttrs = require('../../domain_attributes');
13+
var domainAttrs = require('../../domain').attributes;
1414
var extendFlat = require('../../../lib/extend').extendFlat;
1515
var counterRegex = require('../../../lib').counterRegex;
1616

0 commit comments

Comments
 (0)