-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathdefaults.js
130 lines (104 loc) · 4.15 KB
/
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
'use strict';
var Registry = require('../../registry');
var Lib = require('../../lib');
var Template = require('../../plot_api/plot_template');
var attributes = require('./attributes');
var basePlotLayoutAttributes = require('../../plots/layout_attributes');
var helpers = require('./helpers');
module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
var containerIn = layoutIn.legend || {};
var legendTraceCount = 0;
var legendReallyHasATrace = false;
var defaultOrder = 'normal';
for(var i = 0; i < fullData.length; i++) {
var trace = fullData[i];
if(!trace.visible) continue;
// Note that we explicitly count any trace that is either shown or
// *would* be shown by default, toward the two traces you need to
// ensure the legend is shown by default, because this can still help
// disambiguate.
if(trace.showlegend || (
trace._dfltShowLegend && !(
trace._module &&
trace._module.attributes &&
trace._module.attributes.showlegend &&
trace._module.attributes.showlegend.dflt === false
)
)) {
legendTraceCount++;
if(trace.showlegend) {
legendReallyHasATrace = true;
// Always show the legend by default if there's a pie,
// or if there's only one trace but it's explicitly shown
if(Registry.traceIs(trace, 'pie-like') ||
trace._input.showlegend === true
) {
legendTraceCount++;
}
}
}
if((Registry.traceIs(trace, 'bar') && layoutOut.barmode === 'stack') ||
['tonextx', 'tonexty'].indexOf(trace.fill) !== -1) {
defaultOrder = helpers.isGrouped({traceorder: defaultOrder}) ?
'grouped+reversed' : 'reversed';
}
if(trace.legendgroup !== undefined && trace.legendgroup !== '') {
defaultOrder = helpers.isReversed({traceorder: defaultOrder}) ?
'reversed+grouped' : 'grouped';
}
}
var showLegend = Lib.coerce(layoutIn, layoutOut,
basePlotLayoutAttributes, 'showlegend',
legendReallyHasATrace && legendTraceCount > 1);
if(showLegend === false && !containerIn.uirevision) return;
var containerOut = Template.newContainer(layoutOut, 'legend');
function coerce(attr, dflt) {
return Lib.coerce(containerIn, containerOut, attributes, attr, dflt);
}
coerce('uirevision', layoutOut.uirevision);
if(showLegend === false) return;
coerce('bgcolor', layoutOut.paper_bgcolor);
coerce('bordercolor');
coerce('borderwidth');
var itemFont = Lib.coerceFont(coerce, 'font', layoutOut.font);
var orientation = coerce('orientation');
var isHorizontal = orientation === 'h';
var defaultX, defaultY, defaultYAnchor;
if(isHorizontal) {
defaultX = 0;
if(Registry.getComponentMethod('rangeslider', 'isVisible')(layoutIn.xaxis)) {
defaultY = 1.1;
defaultYAnchor = 'bottom';
} else {
// maybe use y=1.1 / yanchor=bottom as above
// to avoid https://github.com/plotly/plotly.js/issues/1199
// in v3
defaultY = -0.1;
defaultYAnchor = 'top';
}
} else {
defaultX = 1.02;
defaultY = 1;
defaultYAnchor = 'auto';
}
coerce('traceorder', defaultOrder);
if(helpers.isGrouped(layoutOut.legend)) coerce('tracegroupgap');
coerce('itemsizing');
coerce('itemwidth');
coerce('itemclick');
coerce('itemdoubleclick');
coerce('x', defaultX);
coerce('xanchor');
coerce('y', defaultY);
coerce('yanchor', defaultYAnchor);
coerce('valign');
Lib.noneOrAll(containerIn, containerOut, ['x', 'y']);
var titleText = coerce('title.text');
if(titleText) {
coerce('title.side', isHorizontal ? 'left' : 'top');
var dfltTitleFont = Lib.extendFlat({}, itemFont, {
size: itemFont.size * (isHorizontal ? 1.2 : 1)
});
Lib.coerceFont(coerce, 'title.font', dfltTitleFont);
}
};