-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathset_positions.js
594 lines (478 loc) · 17.4 KB
/
set_positions.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/**
* Copyright 2012-2016, 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 isNumeric = require('fast-isnumeric');
var Registry = require('../../registry');
var Axes = require('../../plots/cartesian/axes');
var Sieve = require('./sieve.js');
/*
* Bar chart stacking/grouping positioning and autoscaling calculations
* for each direction separately calculate the ranges and positions
* note that this handles histograms too
* now doing this one subplot at a time
*/
module.exports = function setPositions(gd, plotinfo) {
var xa = plotinfo.xaxis,
ya = plotinfo.yaxis;
var fullTraces = gd._fullData,
calcTraces = gd.calcdata,
calcTracesHorizontal = [],
calcTracesVertical = [],
i;
for(i = 0; i < fullTraces.length; i++) {
var fullTrace = fullTraces[i];
if(
fullTrace.visible === true &&
Registry.traceIs(fullTrace, 'bar') &&
fullTrace.xaxis === xa._id &&
fullTrace.yaxis === ya._id
) {
if(fullTrace.orientation === 'h') {
calcTracesHorizontal.push(calcTraces[i]);
}
else {
calcTracesVertical.push(calcTraces[i]);
}
}
}
setGroupPositions(gd, xa, ya, calcTracesVertical);
setGroupPositions(gd, ya, xa, calcTracesHorizontal);
};
function setGroupPositions(gd, pa, sa, calcTraces) {
if(!calcTraces.length) return;
var barmode = gd._fullLayout.barmode,
overlay = (barmode === 'overlay'),
group = (barmode === 'group'),
excluded,
included,
i, calcTrace, fullTrace;
if(overlay) {
setGroupPositionsInOverlayMode(gd, pa, sa, calcTraces);
}
else if(group) {
// exclude from the group those traces for which the user set an offset
excluded = [];
included = [];
for(i = 0; i < calcTraces.length; i++) {
calcTrace = calcTraces[i];
fullTrace = calcTrace[0].trace;
if(fullTrace.offset === undefined) included.push(calcTrace);
else excluded.push(calcTrace);
}
if(included.length) {
setGroupPositionsInGroupMode(gd, pa, sa, included);
}
if(excluded.length) {
setGroupPositionsInOverlayMode(gd, pa, sa, excluded);
}
}
else {
// exclude from the stack those traces for which the user set a base
excluded = [];
included = [];
for(i = 0; i < calcTraces.length; i++) {
calcTrace = calcTraces[i];
fullTrace = calcTrace[0].trace;
if(fullTrace.base === undefined) included.push(calcTrace);
else excluded.push(calcTrace);
}
if(included.length) {
setGroupPositionsInStackOrRelativeMode(gd, pa, sa, included);
}
if(excluded.length) {
setGroupPositionsInOverlayMode(gd, pa, sa, excluded);
}
}
}
function setGroupPositionsInOverlayMode(gd, pa, sa, calcTraces) {
var barnorm = gd._fullLayout.barnorm,
separateNegativeValues = false,
dontMergeOverlappingData = !barnorm;
// update position axis and set bar offsets and widths
for(var i = 0; i < calcTraces.length; i++) {
var calcTrace = calcTraces[i];
var sieve = new Sieve(
[calcTrace], separateNegativeValues, dontMergeOverlappingData
);
// set bar offsets and widths, and update position axis
setOffsetAndWidth(gd, pa, sieve);
// set bar bases and sizes, and update size axis
//
// (note that `setGroupPositionsInOverlayMode` handles the case barnorm
// is defined, because this function is also invoked for traces that
// can't be grouped or stacked)
if(barnorm) {
sieveBars(gd, sa, sieve);
normalizeBars(gd, sa, sieve);
}
else {
setBaseAndTop(gd, sa, sieve);
}
}
}
function setGroupPositionsInGroupMode(gd, pa, sa, calcTraces) {
var fullLayout = gd._fullLayout,
barnorm = fullLayout.barnorm,
separateNegativeValues = false,
dontMergeOverlappingData = !barnorm,
sieve = new Sieve(
calcTraces, separateNegativeValues, dontMergeOverlappingData
);
// set bar offsets and widths, and update position axis
setOffsetAndWidthInGroupMode(gd, pa, sieve);
// set bar bases and sizes, and update size axis
if(barnorm) {
sieveBars(gd, sa, sieve);
normalizeBars(gd, sa, sieve);
}
else {
setBaseAndTop(gd, sa, sieve);
}
}
function setGroupPositionsInStackOrRelativeMode(gd, pa, sa, calcTraces) {
var fullLayout = gd._fullLayout,
barmode = fullLayout.barmode,
stack = (barmode === 'stack'),
relative = (barmode === 'relative'),
barnorm = gd._fullLayout.barnorm,
separateNegativeValues = relative,
dontMergeOverlappingData = !(barnorm || stack || relative),
sieve = new Sieve(
calcTraces, separateNegativeValues, dontMergeOverlappingData
);
// set bar offsets and widths, and update position axis
setOffsetAndWidth(gd, pa, sieve);
// set bar bases and sizes, and update size axis
stackBars(gd, sa, sieve);
}
function setOffsetAndWidth(gd, pa, sieve) {
var fullLayout = gd._fullLayout,
bargap = fullLayout.bargap,
bargroupgap = fullLayout.bargroupgap,
minDiff = sieve.minDiff,
calcTraces = sieve.traces,
i, calcTrace, calcTrace0,
t;
// set bar offsets and widths
var barGroupWidth = minDiff * (1 - bargap),
barWidthPlusGap = barGroupWidth,
barWidth = barWidthPlusGap * (1 - bargroupgap);
// computer bar group center and bar offset
var offsetFromCenter = -barWidth / 2;
for(i = 0; i < calcTraces.length; i++) {
calcTrace = calcTraces[i];
calcTrace0 = calcTrace[0];
// store bar width and offset for this trace
t = calcTrace0.t;
t.barwidth = barWidth;
t.poffset = offsetFromCenter;
t.bargroupwidth = barGroupWidth;
}
// stack bars that only differ by rounding
sieve.binWidth = calcTraces[0][0].t.barwidth / 100;
// if defined, apply trace offset and width
applyAttributes(sieve);
// store the bar center in each calcdata item
setBarCenter(gd, pa, sieve);
// update position axes
updatePositionAxis(gd, pa, sieve);
}
function setOffsetAndWidthInGroupMode(gd, pa, sieve) {
var fullLayout = gd._fullLayout,
bargap = fullLayout.bargap,
bargroupgap = fullLayout.bargroupgap,
positions = sieve.positions,
distinctPositions = sieve.distinctPositions,
minDiff = sieve.minDiff,
calcTraces = sieve.traces,
i, calcTrace, calcTrace0,
t;
// if there aren't any overlapping positions,
// let them have full width even if mode is group
var overlap = (positions.length !== distinctPositions.length);
var nTraces = calcTraces.length,
barGroupWidth = minDiff * (1 - bargap),
barWidthPlusGap = (overlap) ? barGroupWidth / nTraces : barGroupWidth,
barWidth = barWidthPlusGap * (1 - bargroupgap);
for(i = 0; i < nTraces; i++) {
calcTrace = calcTraces[i];
calcTrace0 = calcTrace[0];
// computer bar group center and bar offset
var offsetFromCenter = (overlap) ?
((2 * i + 1 - nTraces) * barWidthPlusGap - barWidth) / 2 :
-barWidth / 2;
// store bar width and offset for this trace
t = calcTrace0.t;
t.barwidth = barWidth;
t.poffset = offsetFromCenter;
t.bargroupwidth = barGroupWidth;
}
// stack bars that only differ by rounding
sieve.binWidth = calcTraces[0][0].t.barwidth / 100;
// if defined, apply trace width
applyAttributes(sieve);
// store the bar center in each calcdata item
setBarCenter(gd, pa, sieve);
// update position axes
updatePositionAxis(gd, pa, sieve, overlap);
}
function applyAttributes(sieve) {
var calcTraces = sieve.traces,
i, calcTrace, calcTrace0, fullTrace,
j,
t;
for(i = 0; i < calcTraces.length; i++) {
calcTrace = calcTraces[i];
calcTrace0 = calcTrace[0];
fullTrace = calcTrace0.trace;
t = calcTrace0.t;
var offset = fullTrace.offset,
initialPoffset = t.poffset,
newPoffset;
if(Array.isArray(offset)) {
// if offset is an array, then clone it into t.poffset.
newPoffset = offset.slice(0, calcTrace.length);
// guard against non-numeric items
for(j = 0; j < newPoffset.length; j++) {
if(!isNumeric(newPoffset[j])) {
newPoffset[j] = initialPoffset;
}
}
// if the length of the array is too short,
// then extend it with the initial value of t.poffset
for(j = newPoffset.length; j < calcTrace.length; j++) {
newPoffset.push(initialPoffset);
}
t.poffset = newPoffset;
}
else if(offset !== undefined) {
t.poffset = offset;
}
var width = fullTrace.width,
initialBarwidth = t.barwidth;
if(Array.isArray(width)) {
// if width is an array, then clone it into t.barwidth.
var newBarwidth = width.slice(0, calcTrace.length);
// guard against non-numeric items
for(j = 0; j < newBarwidth.length; j++) {
if(!isNumeric(newBarwidth[j])) newBarwidth[j] = initialBarwidth;
}
// if the length of the array is too short,
// then extend it with the initial value of t.barwidth
for(j = newBarwidth.length; j < calcTrace.length; j++) {
newBarwidth.push(initialBarwidth);
}
t.barwidth = newBarwidth;
// if user didn't set offset,
// then correct t.poffset to ensure bars remain centered
if(offset === undefined) {
newPoffset = [];
for(j = 0; j < calcTrace.length; j++) {
newPoffset.push(
initialPoffset + (initialBarwidth - newBarwidth[j]) / 2
);
}
t.poffset = newPoffset;
}
}
else if(width !== undefined) {
t.barwidth = width;
// if user didn't set offset,
// then correct t.poffset to ensure bars remain centered
if(offset === undefined) {
t.poffset = initialPoffset + (initialBarwidth - width) / 2;
}
}
}
}
function setBarCenter(gd, pa, sieve) {
var calcTraces = sieve.traces,
pLetter = getAxisLetter(pa);
for(var i = 0; i < calcTraces.length; i++) {
var calcTrace = calcTraces[i],
t = calcTrace[0].t,
poffset = t.poffset,
poffsetIsArray = Array.isArray(poffset),
barwidth = t.barwidth,
barwidthIsArray = Array.isArray(barwidth);
for(var j = 0; j < calcTrace.length; j++) {
var calcBar = calcTrace[j];
calcBar[pLetter] = calcBar.p +
((poffsetIsArray) ? poffset[j] : poffset) +
((barwidthIsArray) ? barwidth[j] : barwidth) / 2;
}
}
}
function updatePositionAxis(gd, pa, sieve, allowMinDtick) {
var calcTraces = sieve.traces,
distinctPositions = sieve.distinctPositions,
distinctPositions0 = distinctPositions[0],
minDiff = sieve.minDiff,
vpad = minDiff / 2;
Axes.minDtick(pa, minDiff, distinctPositions0, allowMinDtick);
// If the user set the bar width or the offset,
// then bars can be shifted away from their positions
// and widths can be larger than minDiff.
//
// Here, we compute pMin and pMax to expand the position axis,
// so that all bars are fully within the axis range.
var pMin = Math.min.apply(Math, distinctPositions) - vpad,
pMax = Math.max.apply(Math, distinctPositions) + vpad;
for(var i = 0; i < calcTraces.length; i++) {
var calcTrace = calcTraces[i],
calcTrace0 = calcTrace[0],
fullTrace = calcTrace0.trace;
if(fullTrace.width === undefined && fullTrace.offset === undefined) {
continue;
}
var t = calcTrace0.t,
poffset = t.poffset,
barwidth = t.barwidth,
poffsetIsArray = Array.isArray(poffset),
barwidthIsArray = Array.isArray(barwidth);
for(var j = 0; j < calcTrace.length; j++) {
var calcBar = calcTrace[j],
calcBarOffset = (poffsetIsArray) ? poffset[j] : poffset,
calcBarWidth = (barwidthIsArray) ? barwidth[j] : barwidth,
p = calcBar.p,
l = p + calcBarOffset,
r = l + calcBarWidth;
pMin = Math.min(pMin, l);
pMax = Math.max(pMax, r);
}
}
Axes.expand(pa, [pMin, pMax], {padded: false});
}
function setBaseAndTop(gd, sa, sieve) {
// store these bar bases and tops in calcdata
// and make sure the size axis includes zero,
// along with the bases and tops of each bar.
var traces = sieve.traces,
sLetter = getAxisLetter(sa),
sMax = sa.l2c(sa.c2l(0)),
sMin = sMax;
for(var i = 0; i < traces.length; i++) {
var trace = traces[i];
for(var j = 0; j < trace.length; j++) {
var bar = trace[j],
barBase = bar.b,
barTop = barBase + bar.s;
bar[sLetter] = barTop;
if(isNumeric(sa.c2l(barTop))) {
sMax = Math.max(sMax, barTop);
sMin = Math.min(sMin, barTop);
}
if(isNumeric(sa.c2l(barBase))) {
sMax = Math.max(sMax, barBase);
sMin = Math.min(sMin, barBase);
}
}
}
Axes.expand(sa, [sMin, sMax], {tozero: true, padded: true});
}
function stackBars(gd, sa, sieve) {
var fullLayout = gd._fullLayout,
barnorm = fullLayout.barnorm,
sLetter = getAxisLetter(sa),
traces = sieve.traces,
i, trace,
j, bar;
var sMax = sa.l2c(sa.c2l(0)),
sMin = sMax;
for(i = 0; i < traces.length; i++) {
trace = traces[i];
for(j = 0; j < trace.length; j++) {
bar = trace[j];
if(!isNumeric(bar.s)) continue;
// stack current bar and get previous sum
var barBase = sieve.put(bar.p, bar.b + bar.s),
barTop = barBase + bar.b + bar.s;
// store the bar base and top in each calcdata item
bar.b = barBase;
bar[sLetter] = barTop;
if(!barnorm) {
if(isNumeric(sa.c2l(barTop))) {
sMax = Math.max(sMax, barTop);
sMin = Math.min(sMin, barTop);
}
if(isNumeric(sa.c2l(barBase))) {
sMax = Math.max(sMax, barBase);
sMin = Math.min(sMin, barBase);
}
}
}
}
// if barnorm is set, let normalizeBars update the axis range
if(barnorm) {
normalizeBars(gd, sa, sieve);
}
else {
Axes.expand(sa, [sMin, sMax], {tozero: true, padded: true});
}
}
function sieveBars(gd, sa, sieve) {
var traces = sieve.traces;
for(var i = 0; i < traces.length; i++) {
var trace = traces[i];
for(var j = 0; j < trace.length; j++) {
var bar = trace[j];
if(isNumeric(bar.s)) sieve.put(bar.p, bar.b + bar.s);
}
}
}
function normalizeBars(gd, sa, sieve) {
// Note:
//
// normalizeBars requires that either sieveBars or stackBars has been
// previously invoked.
var traces = sieve.traces,
sLetter = getAxisLetter(sa),
sTop = (gd._fullLayout.barnorm === 'fraction') ? 1 : 100,
sTiny = sTop / 1e9, // in case of rounding error in sum
sMin = 0,
sMax = (gd._fullLayout.barmode === 'stack') ? sTop : 0,
padded = false;
for(var i = 0; i < traces.length; i++) {
var trace = traces[i];
for(var j = 0; j < trace.length; j++) {
var bar = trace[j];
if(!isNumeric(bar.s)) continue;
var scale = Math.abs(sTop / sieve.get(bar.p, bar.s));
bar.b *= scale;
bar.s *= scale;
var barBase = bar.b,
barTop = barBase + bar.s;
bar[sLetter] = barTop;
if(isNumeric(sa.c2l(barTop))) {
if(barTop < sMin - sTiny) {
padded = true;
sMin = barTop;
}
if(barTop > sMax + sTiny) {
padded = true;
sMax = barTop;
}
}
if(isNumeric(sa.c2l(barBase))) {
if(barBase < sMin - sTiny) {
padded = true;
sMin = barBase;
}
if(barBase > sMax + sTiny) {
padded = true;
sMax = barBase;
}
}
}
}
// update range of size axis
Axes.expand(sa, [sMin, sMax], {tozero: true, padded: padded});
}
function getAxisLetter(ax) {
return ax._id.charAt(0);
}