Skip to content

Commit e0ab211

Browse files
emilyklcamdecoster
andcommitted
Update src/traces/bar/cross_trace_calc.js
Co-authored-by: Cameron DeCoster <[email protected]>
1 parent 2294dbd commit e0ab211

File tree

1 file changed

+49
-44
lines changed

1 file changed

+49
-44
lines changed

src/traces/bar/cross_trace_calc.js

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
'use strict';
22

3-
var isNumeric = require('fast-isnumeric');
4-
var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;
5-
var BADNUM = require('../../constants/numerical').BADNUM;
3+
const isNumeric = require('fast-isnumeric');
4+
const { isArrayOrTypedArray } = require('../../lib');
5+
const { BADNUM } = require('../../constants/numerical');
66

7-
var Registry = require('../../registry');
8-
var Axes = require('../../plots/cartesian/axes');
9-
var getAxisGroup = require('../../plots/cartesian/constraints').getAxisGroup;
10-
var Sieve = require('./sieve.js');
7+
const Registry = require('../../registry');
8+
const Axes = require('../../plots/cartesian/axes');
9+
const { getAxisGroup } = require('../../plots/cartesian/constraints');
10+
const Sieve = require('./sieve.js');
1111

12-
var TEXTPAD = require('./constants').TEXTPAD;
13-
var LINE_SPACING = require('../../constants/alignment').LINE_SPACING;
14-
var BR_TAG_ALL = require('../../lib/svg_text_utils').BR_TAG_ALL;
12+
const { TEXTPAD } = require('./constants');
13+
const { LINE_SPACING } = require('../../constants/alignment');
14+
const { BR_TAG_ALL } = require('../../lib/svg_text_utils');
1515

1616
/*
1717
* Bar chart stacking/grouping positioning and autoscaling calculations
@@ -569,12 +569,12 @@ function setBaseAndTop(sa, sieve) {
569569
}
570570
}
571571

572-
const extraPad = estimateAxisPaddingForText(fullTrace, calcTrace);
572+
const { ppadminus, ppadplus } = estimateAxisPaddingForText(fullTrace, calcTrace);
573573
fullTrace._extremes[sa._id] = Axes.findExtremes(sa, pts, {
574-
tozero: tozero,
574+
tozero,
575575
padded: true,
576-
ppadplus: extraPad.ppadplus,
577-
ppadminus: extraPad.ppadminus
576+
ppadplus,
577+
ppadminus
578578
});
579579
}
580580
}
@@ -646,14 +646,14 @@ function stackBars(sa, sieve, opts) {
646646

647647
// if barnorm is set, let normalizeBars update the axis range
648648
if (!opts.norm) {
649-
const extraPad = estimateAxisPaddingForText(fullTrace, calcTrace);
649+
const { ppadminus, ppadplus } = estimateAxisPaddingForText(fullTrace, calcTrace);
650650
fullTrace._extremes[sa._id] = Axes.findExtremes(sa, pts, {
651651
// N.B. we don't stack base with 'base',
652652
// so set tozero:true always!
653653
tozero: true,
654654
padded: true,
655-
ppadplus: extraPad.ppadplus,
656-
ppadminus: extraPad.ppadminus
655+
ppadplus,
656+
ppadminus
657657
});
658658
}
659659
}
@@ -757,51 +757,56 @@ function normalizeBars(sa, sieve, opts) {
757757
}
758758
}
759759

760-
const extraPad = estimateAxisPaddingForText(fullTrace, calcTrace);
760+
const { ppadminus, ppadplus } = estimateAxisPaddingForText(fullTrace, calcTrace);
761761
fullTrace._extremes[sa._id] = Axes.findExtremes(sa, pts, {
762-
tozero: tozero,
763-
padded: padded,
764-
ppadplus: extraPad.ppadplus,
765-
ppadminus: extraPad.ppadminus
762+
tozero,
763+
padded,
764+
ppadplus,
765+
ppadminus
766766
});
767767
}
768768
}
769769

770-
// Returns a very lightweight estimate of extra padding (in pixels)
771-
// needed to accommodate outside text labels on bars. Only adds padding
772-
// vertical bars with textposition 'outside' and textangle 0 or 'auto'
773-
// for now.
774-
//
775-
// This mitigates the most common scenario where a simple vertical
776-
// bar chart with textposition set to 'outside' experiences text
777-
// labels being cut off at the edge of the plot area.
778-
//
779-
// More complex scenarios (horizontal bars, various text angles)
780-
// are not (yet) handled here, but could be in the future.
781-
// Returns an object with ppadplus and ppadminus values,
782-
// to be passed into Axes.findExtremes.
770+
/*
771+
* Returns a very lightweight estimate of extra padding (in pixels)
772+
* needed to accommodate outside text labels on bars. Only adds padding
773+
* for vertical bars with textposition 'outside' and textangle 0 or 'auto'
774+
* for now.
775+
*
776+
* This mitigates the most common scenario where a simple vertical
777+
* bar chart with textposition set to 'outside' experiences text
778+
* labels being cut off at the edge of the plot area.
779+
*
780+
* More complex scenarios (horizontal bars, various text angles)
781+
* are not (yet) handled here, but could be in the future.
782+
* Returns an object with ppadplus and ppadminus values,
783+
* to be passed into Axes.findExtremes.
784+
*/
783785
function estimateAxisPaddingForText(trace, calcTrace) {
784786
if (
785787
trace.orientation === 'v' &&
786788
(trace.text || trace.texttemplate) &&
787-
trace.textposition == 'outside' &&
788-
(trace.textangle == 'auto' || trace.textangle == 0)
789+
trace.textposition === 'outside' &&
790+
(trace.textangle === 'auto' || trace.textangle === 0)
789791
) {
790792
// count number of lines by counting <br> elements
791793
function countLines(text) {
792794
if (!text || typeof text !== 'string') return 0;
793795
return (text.match(BR_TAG_ALL) || []).length + 1;
794796
}
795-
var nLines = trace.texttemplate
796-
? countLines(trace.texttemplate)
797-
: isArrayOrTypedArray(trace.text)
798-
? Math.max(...trace.text.map((t) => countLines(t)))
799-
: countLines(trace.text);
797+
var nLines;
798+
if (trace.texttemplate) {
799+
nLines = countLines(trace.texttemplate);
800+
} else {
801+
nLines = isArrayOrTypedArray(trace.text)
802+
? Math.max(...trace.text.map((t) => countLines(t)))
803+
: countLines(trace.text);
804+
}
800805

801806
const padAmount = trace.outsidetextfont.size * LINE_SPACING * nLines + TEXTPAD;
802807
return {
803-
// Yes, I know this looks backwards from what it should be,
804-
// but it works like this
808+
// ppadplus corresponds to the negative-direction bars and
809+
// ppadminus corresponds to the positive-direction bars (for some reason)
805810
ppadplus: calcTrace.some((bar) => bar.s < 0) ? padAmount : 0,
806811
ppadminus: calcTrace.some((bar) => bar.s >= 0) ? padAmount : 0
807812
};

0 commit comments

Comments
 (0)