-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcross_trace_defaults.js
123 lines (106 loc) · 4.08 KB
/
cross_trace_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
/**
* Copyright 2012-2019, 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 Lib = require('../../lib');
var nestedProperty = Lib.nestedProperty;
var attributes = require('./attributes');
var BINATTRS = {
x: [
{aStr: 'xbins.start', name: 'start'},
{aStr: 'xbins.end', name: 'end'},
{aStr: 'xbins.size', name: 'size'},
{aStr: 'nbinsx', name: 'nbins'}
],
y: [
{aStr: 'ybins.start', name: 'start'},
{aStr: 'ybins.end', name: 'end'},
{aStr: 'ybins.size', name: 'size'},
{aStr: 'nbinsy', name: 'nbins'}
]
};
// handle bin attrs and relink auto-determined values so fullData is complete
module.exports = function crossTraceDefaults(fullData, fullLayout) {
var allBinOpts = fullLayout._histogramBinOpts = {};
var isOverlay = fullLayout.barmode === 'overlay';
var i, j, traceOut, traceIn, binDirection, group, binOpts;
function coerce(attr) {
return Lib.coerce(traceOut._input, traceOut, attributes, attr);
}
for(i = 0; i < fullData.length; i++) {
traceOut = fullData[i];
if(traceOut.type !== 'histogram') continue;
// TODO: this shouldn't be relinked as it's only used within calc
// https://github.com/plotly/plotly.js/issues/749
delete traceOut._autoBinFinished;
binDirection = traceOut.orientation === 'v' ? 'x' : 'y';
// in overlay mode make a separate group for each trace
// otherwise collect all traces of the same subplot & orientation
group = traceOut._groupName = isOverlay ? traceOut.uid : (
getAxisGroup(fullLayout, traceOut.xaxis) +
getAxisGroup(fullLayout, traceOut.yaxis) +
binDirection
);
binOpts = allBinOpts[group];
if(binOpts) {
binOpts.traces.push(traceOut);
} else {
binOpts = allBinOpts[group] = {
traces: [traceOut],
direction: binDirection
};
}
}
for(group in allBinOpts) {
binOpts = allBinOpts[group];
binDirection = binOpts.direction;
var attrs = BINATTRS[binDirection];
for(j = 0; j < attrs.length; j++) {
var attrSpec = attrs[j];
var attr = attrSpec.name;
// nbins(x|y) is moot if we have a size. This depends on
// nbins coming after size in binAttrs.
if(attr === 'nbins' && binOpts.sizeFound) continue;
var aStr = attrSpec.aStr;
for(i = 0; i < binOpts.traces.length; i++) {
traceOut = binOpts.traces[i];
traceIn = traceOut._input;
if(nestedProperty(traceIn, aStr).get() !== undefined) {
binOpts[attr] = coerce(aStr);
binOpts[attr + 'Found'] = true;
break;
}
var autoVals = traceOut._autoBin;
if(autoVals && autoVals[attr]) {
// if this is the *first* autoval
nestedProperty(traceOut, aStr).set(autoVals[attr]);
}
}
// start and end we need to coerce anyway, after having collected the
// first of each into binOpts, in case a trace wants to restrict its
// data to a certain range
if(attr === 'start' || attr === 'end') {
for(; i < binOpts.traces.length; i++) {
traceOut = binOpts.traces[i];
coerce(aStr, (traceOut._autoBin || {})[attr]);
}
}
if(attr === 'nbins' && !binOpts.sizeFound && !binOpts.nbinsFound) {
traceOut = binOpts.traces[0];
binOpts[attr] = coerce(aStr);
}
}
}
};
function getAxisGroup(fullLayout, axId) {
var matchGroups = fullLayout._axisMatchGroups;
for(var i = 0; i < matchGroups.length; i++) {
var group = matchGroups[i];
if(group[axId]) return 'g' + i;
}
return axId;
}