-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathdefaults.js
248 lines (201 loc) · 7.47 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
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
'use strict';
var Registry = require('../../registry');
var Lib = require('../../lib');
var Template = require('../../plot_api/plot_template');
var plotsAttrs = require('../../plots/attributes');
var attributes = require('./attributes');
var basePlotLayoutAttributes = require('../../plots/layout_attributes');
var helpers = require('./helpers');
function groupDefaults(legendId, layoutIn, layoutOut, fullData) {
var containerIn = layoutIn[legendId] || {};
var containerOut = Template.newContainer(layoutOut, legendId);
function coerce(attr, dflt) {
return Lib.coerce(containerIn, containerOut, attributes, attr, dflt);
}
// N.B. unified hover needs to inherit from font, bgcolor & bordercolor even when legend.visible is false
var itemFont = Lib.coerceFont(coerce, 'font', layoutOut.font);
coerce('bgcolor', layoutOut.paper_bgcolor);
coerce('bordercolor');
var visible = coerce('visible');
if(!visible) return;
var trace;
var traceCoerce = function(attr, dflt) {
var traceIn = trace._input;
var traceOut = trace;
return Lib.coerce(traceIn, traceOut, plotsAttrs, attr, dflt);
};
var globalFont = layoutOut.font || {};
var grouptitlefont = Lib.coerceFont(coerce, 'grouptitlefont', globalFont, { overrideDflt: {
size: Math.round(globalFont.size * 1.1)
}});
var legendTraceCount = 0;
var legendReallyHasATrace = false;
var defaultOrder = 'normal';
var shapesWithLegend = (layoutOut.shapes || []).filter(function(d) { return d.showlegend; });
var allLegendItems = fullData.concat(shapesWithLegend).filter(function(d) {
return legendId === (d.legend || 'legend');
});
for(var i = 0; i < allLegendItems.length; i++) {
trace = allLegendItems[i];
if(!trace.visible) continue;
var isShape = trace._isShape;
// 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(!isShape && Registry.traceIs(trace, 'pie-like') ||
trace._input.showlegend === true
) {
legendTraceCount++;
}
}
Lib.coerceFont(traceCoerce, 'legendgrouptitle.font', grouptitlefont);
}
if((!isShape && 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 > (legendId === 'legend' ? 1 : 0)));
// delete legend
if(showLegend === false) layoutOut[legendId] = undefined;
if(showLegend === false && !containerIn.uirevision) return;
coerce('uirevision', layoutOut.uirevision);
if(showLegend === false) return;
coerce('borderwidth');
var orientation = coerce('orientation');
var yref = coerce('yref');
var xref = coerce('xref');
var isHorizontal = orientation === 'h';
var isPaperY = yref === 'paper';
var isPaperX = xref === 'paper';
var defaultX, defaultY, defaultYAnchor;
var defaultXAnchor = 'left';
if(isHorizontal) {
defaultX = 0;
if(Registry.getComponentMethod('rangeslider', 'isVisible')(layoutIn.xaxis)) {
if(isPaperY) {
defaultY = 1.1;
defaultYAnchor = 'bottom';
} else {
defaultY = 1;
defaultYAnchor = 'top';
}
} else {
// maybe use y=1.1 / yanchor=bottom as above
// to avoid https://github.com/plotly/plotly.js/issues/1199
// in v3
if(isPaperY) {
defaultY = -0.1;
defaultYAnchor = 'top';
} else {
defaultY = 0;
defaultYAnchor = 'bottom';
}
}
} else {
defaultY = 1;
defaultYAnchor = 'auto';
if(isPaperX) {
defaultX = 1.02;
} else {
defaultX = 1;
defaultXAnchor = 'right';
}
}
Lib.coerce(containerIn, containerOut, {
x: {
valType: 'number',
editType: 'legend',
min: isPaperX ? -2 : 0,
max: isPaperX ? 3 : 1,
dflt: defaultX,
}
}, 'x');
Lib.coerce(containerIn, containerOut, {
y: {
valType: 'number',
editType: 'legend',
min: isPaperY ? -2 : 0,
max: isPaperY ? 3 : 1,
dflt: defaultY,
}
}, 'y');
coerce('traceorder', defaultOrder);
if(helpers.isGrouped(layoutOut[legendId])) coerce('tracegroupgap');
coerce('entrywidth');
coerce('entrywidthmode');
coerce('indentation');
coerce('itemsizing');
coerce('itemwidth');
coerce('itemclick');
coerce('itemdoubleclick');
coerce('groupclick');
coerce('xanchor', defaultXAnchor);
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: Lib.bigFont(itemFont.size)
});
Lib.coerceFont(coerce, 'title.font', dfltTitleFont);
}
}
module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
var i;
var allLegendsData = fullData.slice();
// shapes could also show up in legends
var shapes = layoutOut.shapes;
if(shapes) {
for(i = 0; i < shapes.length; i++) {
var shape = shapes[i];
if(!shape.showlegend) continue;
var mockTrace = {
_input: shape._input,
visible: shape.visible,
showlegend: shape.showlegend,
legend: shape.legend
};
allLegendsData.push(mockTrace);
}
}
var legends = ['legend'];
for(i = 0; i < allLegendsData.length; i++) {
Lib.pushUnique(legends, allLegendsData[i].legend);
}
layoutOut._legends = [];
for(i = 0; i < legends.length; i++) {
var legendId = legends[i];
groupDefaults(legendId, layoutIn, layoutOut, allLegendsData);
if(
layoutOut[legendId] &&
layoutOut[legendId].visible
) {
layoutOut[legendId]._id = legendId;
}
layoutOut._legends.push(legendId);
}
};