Skip to content

Commit cec4ba0

Browse files
committed
bar: set bar center after applying attributes
* Refactored `setWidthAndOffset` and `setWidthAndOffsetInGroupMode` so that the bar center is computed after applying the trace attributes offset and width.
1 parent 339f5c9 commit cec4ba0

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

src/traces/bar/set_positions.js

+31-20
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,11 @@ function setGroupPositionsInStackOrRelativeMode(gd, pa, sa, calcTraces) {
184184

185185
function setOffsetAndWidth(gd, pa, sieve) {
186186
var fullLayout = gd._fullLayout,
187-
pLetter = getAxisLetter(pa),
188187
bargap = fullLayout.bargap,
189188
bargroupgap = fullLayout.bargroupgap,
190189
minDiff = sieve.minDiff,
191190
calcTraces = sieve.traces,
192191
i, calcTrace, calcTrace0,
193-
j, calcBar,
194192
t;
195193

196194
// set bar offsets and widths
@@ -199,8 +197,7 @@ function setOffsetAndWidth(gd, pa, sieve) {
199197
barWidth = barWidthPlusGap * (1 - bargroupgap);
200198

201199
// computer bar group center and bar offset
202-
var offsetFromCenter = -barWidth / 2,
203-
barCenter = 0;
200+
var offsetFromCenter = -barWidth / 2;
204201

205202
for(i = 0; i < calcTraces.length; i++) {
206203
calcTrace = calcTraces[i];
@@ -211,12 +208,6 @@ function setOffsetAndWidth(gd, pa, sieve) {
211208
t.barwidth = barWidth;
212209
t.poffset = offsetFromCenter;
213210
t.bargroupwidth = barGroupWidth;
214-
215-
// store the bar center in each calcdata item
216-
for(j = 0; j < calcTrace.length; j++) {
217-
calcBar = calcTrace[j];
218-
calcBar[pLetter] = calcBar.p + barCenter;
219-
}
220211
}
221212

222213
// stack bars that only differ by rounding
@@ -225,22 +216,23 @@ function setOffsetAndWidth(gd, pa, sieve) {
225216
// if defined, apply trace offset and width
226217
applyAttributes(sieve);
227218

219+
// store the bar center in each calcdata item
220+
setBarCenter(gd, pa, sieve);
221+
228222
// update position axes
229223
updatePositionAxis(gd, pa, sieve);
230224
}
231225

232226

233227
function setOffsetAndWidthInGroupMode(gd, pa, sieve) {
234228
var fullLayout = gd._fullLayout,
235-
pLetter = getAxisLetter(pa),
236229
bargap = fullLayout.bargap,
237230
bargroupgap = fullLayout.bargroupgap,
238231
positions = sieve.positions,
239232
distinctPositions = sieve.distinctPositions,
240233
minDiff = sieve.minDiff,
241234
calcTraces = sieve.traces,
242235
i, calcTrace, calcTrace0,
243-
j, calcBar,
244236
t;
245237

246238
// if there aren't any overlapping positions,
@@ -259,20 +251,13 @@ function setOffsetAndWidthInGroupMode(gd, pa, sieve) {
259251
// computer bar group center and bar offset
260252
var offsetFromCenter = (overlap) ?
261253
((2 * i + 1 - nTraces) * barWidthPlusGap - barWidth) / 2 :
262-
-barWidth / 2,
263-
barCenter = offsetFromCenter + barWidth / 2;
254+
-barWidth / 2;
264255

265256
// store bar width and offset for this trace
266257
t = calcTrace0.t;
267258
t.barwidth = barWidth;
268259
t.poffset = offsetFromCenter;
269260
t.bargroupwidth = barGroupWidth;
270-
271-
// store the bar center in each calcdata item
272-
for(j = 0; j < calcTrace.length; j++) {
273-
calcBar = calcTrace[j];
274-
calcBar[pLetter] = calcBar.p + barCenter;
275-
}
276261
}
277262

278263
// stack bars that only differ by rounding
@@ -281,6 +266,9 @@ function setOffsetAndWidthInGroupMode(gd, pa, sieve) {
281266
// if defined, apply trace width
282267
applyAttributes(sieve);
283268

269+
// store the bar center in each calcdata item
270+
setBarCenter(gd, pa, sieve);
271+
284272
// update position axes
285273
updatePositionAxis(gd, pa, sieve, overlap);
286274
}
@@ -370,6 +358,29 @@ function applyAttributes(sieve) {
370358
}
371359

372360

361+
function setBarCenter(gd, pa, sieve) {
362+
var calcTraces = sieve.traces,
363+
pLetter = getAxisLetter(pa);
364+
365+
for(var i = 0; i < calcTraces.length; i++) {
366+
var calcTrace = calcTraces[i],
367+
t = calcTrace[0].t,
368+
poffset = t.poffset,
369+
poffsetIsArray = Array.isArray(poffset),
370+
barwidth = t.barwidth,
371+
barwidthIsArray = Array.isArray(barwidth);
372+
373+
for(var j = 0; j < calcTrace.length; j++) {
374+
var calcBar = calcTrace[j];
375+
376+
calcBar[pLetter] = calcBar.p +
377+
((poffsetIsArray) ? poffset[j] : poffset) +
378+
((barwidthIsArray) ? barwidth[j] : barwidth) / 2;
379+
}
380+
}
381+
}
382+
383+
373384
function updatePositionAxis(gd, pa, sieve, allowMinDtick) {
374385
var calcTraces = sieve.traces,
375386
distinctPositions = sieve.distinctPositions,

0 commit comments

Comments
 (0)