Skip to content

Commit f63a7b7

Browse files
committed
bar: refactor code into function setBaseAndSize
1 parent 3dd6ed0 commit f63a7b7

File tree

1 file changed

+98
-88
lines changed

1 file changed

+98
-88
lines changed

src/traces/bar/set_positions.js

Lines changed: 98 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ function setGroupPositions(gd, pa, sa, traces) {
5454

5555
var fullLayout = gd._fullLayout,
5656
pLetter = pa._id.charAt(0),
57-
sLetter = sa._id.charAt(0),
58-
i, j;
57+
sLetter = sa._id.charAt(0);
5958

6059
if(fullLayout.barmode === 'overlay') {
6160
traces.forEach(function(trace) {
@@ -64,6 +63,83 @@ function setGroupPositions(gd, pa, sa, traces) {
6463
}
6564
else setOffsetAndWidth(gd, pa, pLetter, traces);
6665

66+
setBaseAndSize(gd, sa, sLetter, traces);
67+
}
68+
69+
70+
// bar position offset and width calculation
71+
// traces is a list of traces (in calcdata) to look at together
72+
// to find the maximum width bars that won't overlap
73+
// for stacked or grouped bars, this is all vertical or horizontal
74+
// bars for overlaid bars, call this individually on each trace.
75+
function setOffsetAndWidth(gd, pa, pLetter, traces) {
76+
var fullLayout = gd._fullLayout,
77+
i, trace,
78+
j, bar;
79+
80+
// make list of bar positions
81+
var positions = [];
82+
for(i = 0; i < traces.length; i++) {
83+
trace = traces[i];
84+
for(j = 0; j < trace.length; j++) {
85+
bar = trace[j];
86+
positions.push(bar.p);
87+
}
88+
}
89+
90+
var dv = Lib.distinctVals(positions),
91+
distinctPositions = dv.vals,
92+
minDiff = dv.minDiff;
93+
94+
// check if there are any overlapping positions;
95+
// if there aren't, let them have full width even if mode is group
96+
var overlap = false;
97+
if(fullLayout.barmode === 'group') {
98+
overlap = (positions.length !== distinctPositions.length);
99+
}
100+
101+
// check forced minimum dtick
102+
Axes.minDtick(pa, minDiff, distinctPositions[0], overlap);
103+
104+
// position axis autorange - always tight fitting
105+
Axes.expand(pa, distinctPositions, {vpad: minDiff / 2});
106+
107+
// computer bar widths and position offsets
108+
var barWidth = minDiff * (1 - fullLayout.bargap);
109+
if(overlap) barWidth /= traces.length;
110+
111+
var barWidthMinusGroupGap = barWidth * (1 - fullLayout.bargroupgap);
112+
113+
for(i = 0; i < traces.length; i++) {
114+
trace = traces[i];
115+
116+
// computer bar group center and bar offset
117+
var offsetFromCenter = (
118+
(overlap ? (2 * i + 1 - traces.length) * barWidth : 0) -
119+
barWidthMinusGroupGap
120+
) / 2,
121+
barCenter = offsetFromCenter + barWidthMinusGroupGap / 2;
122+
123+
// store bar width and offset for this trace
124+
var t = trace[0].t;
125+
t.barwidth = barWidthMinusGroupGap;
126+
t.poffset = offsetFromCenter;
127+
t.dbar = minDiff;
128+
129+
// store the bar center in each calcdata item
130+
for(j = 0; j < trace.length; j++) {
131+
bar = trace[j];
132+
bar[pLetter] = bar.p + barCenter;
133+
}
134+
}
135+
}
136+
137+
138+
function setBaseAndSize(gd, sa, sLetter, traces) {
139+
var fullLayout = gd._fullLayout,
140+
i, trace,
141+
j, bar;
142+
67143
var stack = (fullLayout.barmode === 'stack'),
68144
relative = (fullLayout.barmode === 'relative'),
69145
norm = fullLayout.barnorm;
@@ -85,32 +161,32 @@ function setGroupPositions(gd, pa, sa, traces) {
85161
sv = 0,
86162
padded = true,
87163
barEnd,
88-
ti,
89164
scale;
90165

91166
for(i = 0; i < traces.length; i++) { // trace index
92-
ti = traces[i];
167+
trace = traces[i];
93168

94-
for(j = 0; j < ti.length; j++) {
169+
for(j = 0; j < trace.length; j++) {
170+
bar = trace[j];
95171

96172
// skip over bars with no size,
97173
// so that we don't try to stack them
98-
if(!isNumeric(ti[j].s)) continue;
174+
if(!isNumeric(bar.s)) continue;
99175

100-
sv = Math.round(ti[j].p / sumround);
176+
sv = Math.round(bar.p / sumround);
101177

102178
// store the negative sum value for p at the same key,
103179
// with sign flipped using string to ensure -0 !== 0.
104-
if(relative && ti[j].s < 0) sv = '-' + sv;
180+
if(relative && bar.s < 0) sv = '-' + sv;
105181

106182
var previousSum = sums[sv] || 0;
107-
if(stack || relative) ti[j].b = previousSum;
108-
barEnd = ti[j].b + ti[j].s;
109-
sums[sv] = previousSum + ti[j].s;
183+
if(stack || relative) bar.b = previousSum;
184+
barEnd = bar.b + bar.s;
185+
sums[sv] = previousSum + bar.s;
110186

111187
// store the bar top in each calcdata item
112188
if(stack || relative) {
113-
ti[j][sLetter] = barEnd;
189+
bar[sLetter] = barEnd;
114190
if(!norm && isNumeric(sa.c2l(barEnd))) {
115191
sMax = Math.max(sMax, barEnd);
116192
sMin = Math.min(sMin, barEnd);
@@ -129,12 +205,14 @@ function setGroupPositions(gd, pa, sa, traces) {
129205
sMax = stack ? top : 0;
130206

131207
for(i = 0; i < traces.length; i++) { // trace index
132-
ti = traces[i];
208+
trace = traces[i];
133209

134-
for(j = 0; j < ti.length; j++) {
135-
relAndNegative = (relative && ti[j].s < 0);
210+
for(j = 0; j < trace.length; j++) {
211+
bar = trace[j];
136212

137-
sv = Math.round(ti[j].p / sumround);
213+
relAndNegative = (relative && bar.s < 0);
214+
215+
sv = Math.round(bar.p / sumround);
138216

139217
// locate negative sum amount for this p val
140218
if(relAndNegative) sv = '-' + sv;
@@ -143,10 +221,10 @@ function setGroupPositions(gd, pa, sa, traces) {
143221

144222
// preserve sign if negative
145223
if(relAndNegative) scale *= -1;
146-
ti[j].b *= scale;
147-
ti[j].s *= scale;
148-
barEnd = ti[j].b + ti[j].s;
149-
ti[j][sLetter] = barEnd;
224+
bar.b *= scale;
225+
bar.s *= scale;
226+
barEnd = bar.b + bar.s;
227+
bar[sLetter] = barEnd;
150228

151229
if(isNumeric(sa.c2l(barEnd))) {
152230
if(barEnd < sMin - tiny) {
@@ -175,71 +253,3 @@ function setGroupPositions(gd, pa, sa, traces) {
175253
}
176254
}
177255
}
178-
179-
180-
// bar position offset and width calculation
181-
// traces is a list of traces (in calcdata) to look at together
182-
// to find the maximum width bars that won't overlap
183-
// for stacked or grouped bars, this is all vertical or horizontal
184-
// bars for overlaid bars, call this individually on each trace.
185-
function setOffsetAndWidth(gd, pa, pLetter, traces) {
186-
var fullLayout = gd._fullLayout,
187-
i, trace,
188-
j, bar;
189-
190-
// make list of bar positions
191-
var positions = [];
192-
for(i = 0; i < traces.length; i++) {
193-
trace = traces[i];
194-
for(j = 0; j < trace.length; j++) {
195-
bar = trace[j];
196-
positions.push(bar.p);
197-
}
198-
}
199-
200-
var dv = Lib.distinctVals(positions),
201-
distinctPositions = dv.vals,
202-
minDiff = dv.minDiff;
203-
204-
// check if there are any overlapping positions;
205-
// if there aren't, let them have full width even if mode is group
206-
var overlap = false;
207-
if(fullLayout.barmode === 'group') {
208-
overlap = (positions.length !== distinctPositions.length);
209-
}
210-
211-
// check forced minimum dtick
212-
Axes.minDtick(pa, minDiff, distinctPositions[0], overlap);
213-
214-
// position axis autorange - always tight fitting
215-
Axes.expand(pa, distinctPositions, {vpad: minDiff / 2});
216-
217-
// computer bar widths and position offsets
218-
var barWidth = minDiff * (1 - fullLayout.bargap);
219-
if(overlap) barWidth /= traces.length;
220-
221-
var barWidthMinusGroupGap = barWidth * (1 - fullLayout.bargroupgap);
222-
223-
for(i = 0; i < traces.length; i++) {
224-
trace = traces[i];
225-
226-
// computer bar group center and bar offset
227-
var offsetFromCenter = (
228-
(overlap ? (2 * i + 1 - traces.length) * barWidth : 0) -
229-
barWidthMinusGroupGap
230-
) / 2,
231-
barCenter = offsetFromCenter + barWidthMinusGroupGap / 2;
232-
233-
// store bar width and offset for this trace
234-
var t = trace[0].t;
235-
t.barwidth = barWidthMinusGroupGap;
236-
t.poffset = offsetFromCenter;
237-
t.dbar = minDiff;
238-
239-
// store the bar center in each calcdata item
240-
for(j = 0; j < trace.length; j++) {
241-
bar = trace[j];
242-
bar[pLetter] = bar.p + barCenter;
243-
}
244-
}
245-
}

0 commit comments

Comments
 (0)