Skip to content

Commit 3dd6ed0

Browse files
committed
bar: rename barpositions to setOffsetAndWidth
* Converted closure `barpositions` into function `setOffsetAndWidth`. * This change will help split setPositions into functions for each barmode.
1 parent 2a72e97 commit 3dd6ed0

File tree

1 file changed

+73
-65
lines changed

1 file changed

+73
-65
lines changed

src/traces/bar/set_positions.js

Lines changed: 73 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ module.exports = function setPositions(gd, plotinfo) {
4848
setGroupPositions(gd, ya, xa, tracesHorizontal);
4949
};
5050

51+
5152
function setGroupPositions(gd, pa, sa, traces) {
5253
if(!traces.length) return;
5354

@@ -56,73 +57,12 @@ function setGroupPositions(gd, pa, sa, traces) {
5657
sLetter = sa._id.charAt(0),
5758
i, j;
5859

59-
// bar position offset and width calculation
60-
// traces is a list of traces (in calcdata) to look at together
61-
// to find the maximum size bars that won't overlap
62-
// for stacked or grouped bars, this is all vertical or horizontal
63-
// bars for overlaid bars, call this individually on each trace.
64-
function barposition(traces) {
65-
var positions = [],
66-
i, trace,
67-
j, bar;
68-
for(i = 0; i < traces.length; i++) {
69-
trace = traces[i];
70-
for(j = 0; j < trace.length; j++) {
71-
bar = trace[j];
72-
}
73-
}
74-
75-
var dv = Lib.distinctVals(positions),
76-
distinctPositions = dv.vals,
77-
minDiff = dv.minDiff;
78-
79-
// check if there are any overlapping positions;
80-
// if there aren't, let them have full width even if mode is group
81-
var overlap = false;
82-
if(fullLayout.barmode === 'group') {
83-
overlap = (positions.length !== distinctPositions.length);
84-
}
85-
86-
// check forced minimum dtick
87-
Axes.minDtick(pa, minDiff, distinctPositions[0], overlap);
88-
89-
// position axis autorange - always tight fitting
90-
Axes.expand(pa, distinctPositions, {vpad: minDiff / 2});
91-
92-
// computer bar widths and position offsets
93-
var barWidth = minDiff * (1 - fullLayout.bargap);
94-
if(overlap) barWidth /= traces.length;
95-
96-
var barWidthMinusGroupGap = barWidth * (1 - fullLayout.bargroupgap);
97-
98-
for(i = 0; i < traces.length; i++) {
99-
trace = traces[i];
100-
101-
// computer bar group center and bar offset
102-
var offsetFromCenter = (
103-
(overlap ? (2 * i + 1 - traces.length) * barWidth : 0) -
104-
barWidthMinusGroupGap
105-
) / 2,
106-
barCenter = offsetFromCenter + barWidthMinusGroupGap / 2;
107-
108-
// store bar width and offset for this trace
109-
var t = trace[0].t;
110-
t.barwidth = barWidthMinusGroupGap;
111-
t.poffset = offsetFromCenter;
112-
t.dbar = minDiff;
113-
114-
// store the bar center in each calcdata item
115-
for(j = 0; j < trace.length; j++) {
116-
bar = trace[j];
117-
bar[pLetter] = bar.p + barCenter;
118-
}
119-
}
120-
}
121-
12260
if(fullLayout.barmode === 'overlay') {
123-
traces.forEach(function(trace) { barposition([trace]); });
61+
traces.forEach(function(trace) {
62+
setOffsetAndWidth(gd, pa, pLetter, [trace]);
63+
});
12464
}
125-
else barposition(traces);
65+
else setOffsetAndWidth(gd, pa, pLetter, traces);
12666

12767
var stack = (fullLayout.barmode === 'stack'),
12868
relative = (fullLayout.barmode === 'relative'),
@@ -235,3 +175,71 @@ function setGroupPositions(gd, pa, sa, traces) {
235175
}
236176
}
237177
}
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)