Skip to content

Commit 7440346

Browse files
committed
bar: add function applyAttributes
* Moved shared code between `setOffsetAndWidth` and `setOffsetAndWidthInGroupMode` to handle offset and width attributes into function `applyAttributes`.
1 parent b4732da commit 7440346

File tree

1 file changed

+62
-91
lines changed

1 file changed

+62
-91
lines changed

src/traces/bar/set_positions.js

Lines changed: 62 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function setOffsetAndWidth(gd, pa, sieve) {
189189
bargroupgap = fullLayout.bargroupgap,
190190
minDiff = sieve.minDiff,
191191
calcTraces = sieve.traces,
192-
i, calcTrace, calcTrace0, fullTrace,
192+
i, calcTrace, calcTrace0,
193193
j, calcBar,
194194
t;
195195

@@ -223,77 +223,7 @@ function setOffsetAndWidth(gd, pa, sieve) {
223223
sieve.binWidth = calcTraces[0][0].t.barwidth / 100;
224224

225225
// if defined, apply trace offset and width
226-
for(i = 0; i < calcTraces.length; i++) {
227-
calcTrace = calcTraces[i];
228-
calcTrace0 = calcTrace[0];
229-
fullTrace = calcTrace0.trace;
230-
t = calcTrace0.t;
231-
232-
var offset = fullTrace.offset,
233-
initialPoffset = t.poffset,
234-
newPoffset;
235-
if(Array.isArray(offset)) {
236-
// if offset is an array, then clone it into t.poffset.
237-
newPoffset = offset.slice(0, calcTrace.length);
238-
239-
// guard against non-numeric items
240-
for(j = 0; j < newPoffset.length; j++) {
241-
if(!isNumeric(newPoffset[j])) newPoffset[j] = initialPoffset;
242-
}
243-
244-
// if the length of the array is too short,
245-
// then extend it with the initial value of t.poffset
246-
for(j = newPoffset.length; j < calcTrace.length; j++) {
247-
newPoffset.push(initialPoffset);
248-
}
249-
250-
t.poffset = newPoffset;
251-
}
252-
else if(offset !== undefined) {
253-
t.poffset = offset;
254-
}
255-
256-
var width = fullTrace.width,
257-
initialBarwidth = t.barwidth;
258-
if(Array.isArray(width)) {
259-
// if width is an array, then clone it into t.barwidth.
260-
var newBarwidth = width.slice(0, calcTrace.length);
261-
262-
// guard against non-numeric items
263-
for(j = 0; j < newBarwidth.length; j++) {
264-
if(!isNumeric(newBarwidth[j])) newBarwidth[j] = initialBarwidth;
265-
}
266-
267-
// if the length of the array is too short,
268-
// then extend it with the initial value of t.barwidth
269-
for(j = newBarwidth.length; j < calcTrace.length; j++) {
270-
newBarwidth.push(initialBarwidth);
271-
}
272-
273-
t.barwidth = newBarwidth;
274-
275-
// if user didn't set offset,
276-
// then correct t.poffset to ensure bars remain centered
277-
if(offset === undefined) {
278-
newPoffset = [];
279-
for(j = 0; j < calcTrace.length; j++) {
280-
newPoffset.push(
281-
initialPoffset + (initialBarwidth - newBarwidth[j]) / 2
282-
);
283-
}
284-
t.poffset = newPoffset;
285-
}
286-
}
287-
else if(width !== undefined) {
288-
t.barwidth = width;
289-
290-
// if user didn't set offset,
291-
// then correct t.poffset to ensure bars remain centered
292-
if(offset === undefined) {
293-
t.poffset = initialPoffset + (initialBarwidth - width) / 2;
294-
}
295-
}
296-
}
226+
applyAttributes(sieve);
297227

298228
// update position axes
299229
updatePositionAxis(gd, pa, sieve);
@@ -309,7 +239,7 @@ function setOffsetAndWidthInGroupMode(gd, pa, sieve) {
309239
distinctPositions = sieve.distinctPositions,
310240
minDiff = sieve.minDiff,
311241
calcTraces = sieve.traces,
312-
i, calcTrace, calcTrace0, fullTrace,
242+
i, calcTrace, calcTrace0,
313243
j, calcBar,
314244
t;
315245

@@ -349,17 +279,55 @@ function setOffsetAndWidthInGroupMode(gd, pa, sieve) {
349279
sieve.binWidth = calcTraces[0][0].t.barwidth / 100;
350280

351281
// if defined, apply trace width
282+
applyAttributes(sieve);
283+
284+
// update position axes
285+
updatePositionAxis(gd, pa, sieve, overlap);
286+
}
287+
288+
289+
function applyAttributes(sieve) {
290+
var calcTraces = sieve.traces,
291+
i, calcTrace, calcTrace0, fullTrace,
292+
j,
293+
t;
294+
352295
for(i = 0; i < calcTraces.length; i++) {
353296
calcTrace = calcTraces[i];
354297
calcTrace0 = calcTrace[0];
355298
fullTrace = calcTrace0.trace;
299+
t = calcTrace0.t;
356300

357-
var width = fullTrace.width;
358-
if(width === undefined) continue;
301+
var offset = fullTrace.offset,
302+
initialPoffset = t.poffset,
303+
newPoffset;
304+
305+
if(Array.isArray(offset)) {
306+
// if offset is an array, then clone it into t.poffset.
307+
newPoffset = offset.slice(0, calcTrace.length);
308+
309+
// guard against non-numeric items
310+
for(j = 0; j < newPoffset.length; j++) {
311+
if(!isNumeric(newPoffset[j])) {
312+
newPoffset[j] = initialPoffset;
313+
}
314+
}
315+
316+
// if the length of the array is too short,
317+
// then extend it with the initial value of t.poffset
318+
for(j = newPoffset.length; j < calcTrace.length; j++) {
319+
newPoffset.push(initialPoffset);
320+
}
321+
322+
t.poffset = newPoffset;
323+
}
324+
else if(offset !== undefined) {
325+
t.poffset = offset;
326+
}
327+
328+
var width = fullTrace.width,
329+
initialBarwidth = t.barwidth;
359330

360-
t = calcTrace0.t;
361-
var initialBarwidth = t.barwidth,
362-
initialPoffset = t.poffset;
363331
if(Array.isArray(width)) {
364332
// if width is an array, then clone it into t.barwidth.
365333
var newBarwidth = width.slice(0, calcTrace.length);
@@ -377,25 +345,28 @@ function setOffsetAndWidthInGroupMode(gd, pa, sieve) {
377345

378346
t.barwidth = newBarwidth;
379347

380-
// correct t.poffset to ensure bars remain centered
381-
var newPoffset = [];
382-
for(j = 0; j < calcTrace.length; j++) {
383-
newPoffset.push(
384-
initialPoffset + (initialBarwidth - newBarwidth[j]) / 2
385-
);
348+
// if user didn't set offset,
349+
// then correct t.poffset to ensure bars remain centered
350+
if(offset === undefined) {
351+
newPoffset = [];
352+
for(j = 0; j < calcTrace.length; j++) {
353+
newPoffset.push(
354+
initialPoffset + (initialBarwidth - newBarwidth[j]) / 2
355+
);
356+
}
357+
t.poffset = newPoffset;
386358
}
387-
t.poffset = newPoffset;
388359
}
389-
else {
360+
else if(width !== undefined) {
390361
t.barwidth = width;
391362

392-
// correct t.poffset to ensure bars remain centered
393-
t.poffset = initialPoffset + (initialBarwidth - width) / 2;
363+
// if user didn't set offset,
364+
// then correct t.poffset to ensure bars remain centered
365+
if(offset === undefined) {
366+
t.poffset = initialPoffset + (initialBarwidth - width) / 2;
367+
}
394368
}
395369
}
396-
397-
// update position axes
398-
updatePositionAxis(gd, pa, sieve, overlap);
399370
}
400371

401372

0 commit comments

Comments
 (0)