forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayout_defaults.js
278 lines (221 loc) · 9.02 KB
/
layout_defaults.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Registry = require('../../registry');
var Lib = require('../../lib');
var Color = require('../../components/color');
var basePlotLayoutAttributes = require('../layout_attributes');
var constants = require('./constants');
var layoutAttributes = require('./layout_attributes');
var handleTypeDefaults = require('./type_defaults');
var handleAxisDefaults = require('./axis_defaults');
var handleConstraintDefaults = require('./constraint_defaults');
var handlePositionDefaults = require('./position_defaults');
var axisIds = require('./axis_ids');
module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
var layoutKeys = Object.keys(layoutIn),
xaListCartesian = [],
yaListCartesian = [],
xaListGl2d = [],
yaListGl2d = [],
xaListCheater = [],
xaListNonCheater = [],
outerTicks = {},
noGrids = {},
i;
// look for axes in the data
for(i = 0; i < fullData.length; i++) {
var trace = fullData[i];
var listX, listY;
if(Registry.traceIs(trace, 'cartesian')) {
listX = xaListCartesian;
listY = yaListCartesian;
}
else if(Registry.traceIs(trace, 'gl2d')) {
listX = xaListGl2d;
listY = yaListGl2d;
}
else continue;
var xaName = axisIds.id2name(trace.xaxis),
yaName = axisIds.id2name(trace.yaxis);
// Two things trigger axis visibility:
// 1. is not carpet
// 2. carpet that's not cheater
if(!Registry.traceIs(trace, 'carpet') || (trace.type === 'carpet' && !trace._cheater)) {
if(xaName) Lib.pushUnique(xaListNonCheater, xaName);
}
// The above check for definitely-not-cheater is not adequate. This
// second list tracks which axes *could* be a cheater so that the
// full condition triggering hiding is:
// *could* be a cheater and *is not definitely visible*
if(trace.type === 'carpet' && trace._cheater) {
if(xaName) Lib.pushUnique(xaListCheater, xaName);
}
// add axes implied by traces
if(xaName && listX.indexOf(xaName) === -1) listX.push(xaName);
if(yaName && listY.indexOf(yaName) === -1) listY.push(yaName);
// check for default formatting tweaks
if(Registry.traceIs(trace, '2dMap')) {
outerTicks[xaName] = true;
outerTicks[yaName] = true;
}
if(Registry.traceIs(trace, 'oriented')) {
var positionAxis = trace.orientation === 'h' ? yaName : xaName;
noGrids[positionAxis] = true;
}
}
// N.B. Ignore orphan axes (i.e. axes that have no data attached to them)
// if gl3d or geo is present on graph. This is retain backward compatible.
//
// TODO drop this in version 2.0
var ignoreOrphan = (layoutOut._has('gl3d') || layoutOut._has('geo'));
if(!ignoreOrphan) {
for(i = 0; i < layoutKeys.length; i++) {
var key = layoutKeys[i];
// orphan layout axes are considered cartesian subplots
if(xaListGl2d.indexOf(key) === -1 &&
xaListCartesian.indexOf(key) === -1 &&
constants.xAxisMatch.test(key)) {
xaListCartesian.push(key);
}
else if(yaListGl2d.indexOf(key) === -1 &&
yaListCartesian.indexOf(key) === -1 &&
constants.yAxisMatch.test(key)) {
yaListCartesian.push(key);
}
}
}
// make sure that plots with orphan cartesian axes
// are considered 'cartesian'
if(xaListCartesian.length && yaListCartesian.length) {
Lib.pushUnique(layoutOut._basePlotModules, Registry.subplotsRegistry.cartesian);
}
function axSort(a, b) {
var aNum = Number(a.substr(5) || 1),
bNum = Number(b.substr(5) || 1);
return aNum - bNum;
}
var xaList = xaListCartesian.concat(xaListGl2d).sort(axSort),
yaList = yaListCartesian.concat(yaListGl2d).sort(axSort),
axesList = xaList.concat(yaList);
// plot_bgcolor only makes sense if there's a (2D) plot!
// TODO: bgcolor for each subplot, to inherit from the main one
var plot_bgcolor = Color.background;
if(xaList.length && yaList.length) {
plot_bgcolor = Lib.coerce(layoutIn, layoutOut, basePlotLayoutAttributes, 'plot_bgcolor');
}
var bgColor = Color.combine(plot_bgcolor, layoutOut.paper_bgcolor);
var axName, axLetter, axLayoutIn, axLayoutOut;
function coerce(attr, dflt) {
return Lib.coerce(axLayoutIn, axLayoutOut, layoutAttributes, attr, dflt);
}
function getCounterAxes(axLetter) {
var list = {x: yaList, y: xaList}[axLetter];
return Lib.simpleMap(list, axisIds.name2id);
}
var counterAxes = {x: getCounterAxes('x'), y: getCounterAxes('y')};
function getOverlayableAxes(axLetter, axName) {
var list = {x: xaList, y: yaList}[axLetter];
var out = [];
for(var j = 0; j < list.length; j++) {
var axName2 = list[j];
if(axName2 !== axName && !(layoutIn[axName2] || {}).overlaying) {
out.push(axisIds.name2id(axName2));
}
}
return out;
}
// first pass creates the containers, determines types, and handles most of the settings
for(i = 0; i < axesList.length; i++) {
axName = axesList[i];
if(!Lib.isPlainObject(layoutIn[axName])) {
layoutIn[axName] = {};
}
axLayoutIn = layoutIn[axName];
axLayoutOut = layoutOut[axName] = {};
handleTypeDefaults(axLayoutIn, axLayoutOut, coerce, fullData, axName);
axLetter = axName.charAt(0);
var overlayableAxes = getOverlayableAxes(axLetter, axName);
var defaultOptions = {
letter: axLetter,
font: layoutOut.font,
outerTicks: outerTicks[axName],
showGrid: !noGrids[axName],
data: fullData,
bgColor: bgColor,
calendar: layoutOut.calendar,
cheateronly: axLetter === 'x' && (xaListCheater.indexOf(axName) !== -1 && xaListNonCheater.indexOf(axName) === -1)
};
handleAxisDefaults(axLayoutIn, axLayoutOut, coerce, defaultOptions, layoutOut);
var showCrossline = coerce('showcrossline');
if(showCrossline) {
coerce('crosslinecolor');
coerce('crosslinethickness');
coerce('crosslinedash');
}
var showSpikes = coerce('showspikes');
if(showSpikes) {
coerce('spikecolor');
coerce('spikethickness');
coerce('spikedash');
coerce('spikemode');
}
var positioningOptions = {
letter: axLetter,
counterAxes: counterAxes[axLetter],
overlayableAxes: overlayableAxes
};
handlePositionDefaults(axLayoutIn, axLayoutOut, coerce, positioningOptions);
axLayoutOut._input = axLayoutIn;
}
// quick second pass for range slider and selector defaults
var rangeSliderDefaults = Registry.getComponentMethod('rangeslider', 'handleDefaults'),
rangeSelectorDefaults = Registry.getComponentMethod('rangeselector', 'handleDefaults');
for(i = 0; i < xaList.length; i++) {
axName = xaList[i];
axLayoutIn = layoutIn[axName];
axLayoutOut = layoutOut[axName];
rangeSliderDefaults(layoutIn, layoutOut, axName);
if(axLayoutOut.type === 'date') {
rangeSelectorDefaults(
axLayoutIn,
axLayoutOut,
layoutOut,
yaList,
axLayoutOut.calendar
);
}
coerce('fixedrange');
}
for(i = 0; i < yaList.length; i++) {
axName = yaList[i];
axLayoutIn = layoutIn[axName];
axLayoutOut = layoutOut[axName];
var anchoredAxis = layoutOut[axisIds.id2name(axLayoutOut.anchor)];
var fixedRangeDflt = (
anchoredAxis &&
anchoredAxis.rangeslider &&
anchoredAxis.rangeslider.visible
);
coerce('fixedrange', fixedRangeDflt);
}
// Finally, handle scale constraints. We need to do this after all axes have
// coerced both `type` (so we link only axes of the same type) and
// `fixedrange` (so we can avoid linking from OR TO a fixed axis).
// sets of axes linked by `scaleanchor` along with the scaleratios compounded
// together, populated in handleConstraintDefaults
layoutOut._axisConstraintGroups = [];
var allAxisIds = counterAxes.x.concat(counterAxes.y);
for(i = 0; i < axesList.length; i++) {
axName = axesList[i];
axLetter = axName.charAt(0);
axLayoutIn = layoutIn[axName];
axLayoutOut = layoutOut[axName];
handleConstraintDefaults(axLayoutIn, axLayoutOut, coerce, allAxisIds, layoutOut);
}
};