|
1 | 1 | 'use strict'; |
2 | 2 |
|
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'); |
6 | 6 |
|
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'); |
11 | 11 |
|
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'); |
15 | 15 |
|
16 | 16 | /* |
17 | 17 | * Bar chart stacking/grouping positioning and autoscaling calculations |
@@ -569,12 +569,12 @@ function setBaseAndTop(sa, sieve) { |
569 | 569 | } |
570 | 570 | } |
571 | 571 |
|
572 | | - const extraPad = estimateAxisPaddingForText(fullTrace, calcTrace); |
| 572 | + const { ppadminus, ppadplus } = estimateAxisPaddingForText(fullTrace, calcTrace); |
573 | 573 | fullTrace._extremes[sa._id] = Axes.findExtremes(sa, pts, { |
574 | | - tozero: tozero, |
| 574 | + tozero, |
575 | 575 | padded: true, |
576 | | - ppadplus: extraPad.ppadplus, |
577 | | - ppadminus: extraPad.ppadminus |
| 576 | + ppadplus, |
| 577 | + ppadminus |
578 | 578 | }); |
579 | 579 | } |
580 | 580 | } |
@@ -646,14 +646,14 @@ function stackBars(sa, sieve, opts) { |
646 | 646 |
|
647 | 647 | // if barnorm is set, let normalizeBars update the axis range |
648 | 648 | if (!opts.norm) { |
649 | | - const extraPad = estimateAxisPaddingForText(fullTrace, calcTrace); |
| 649 | + const { ppadminus, ppadplus } = estimateAxisPaddingForText(fullTrace, calcTrace); |
650 | 650 | fullTrace._extremes[sa._id] = Axes.findExtremes(sa, pts, { |
651 | 651 | // N.B. we don't stack base with 'base', |
652 | 652 | // so set tozero:true always! |
653 | 653 | tozero: true, |
654 | 654 | padded: true, |
655 | | - ppadplus: extraPad.ppadplus, |
656 | | - ppadminus: extraPad.ppadminus |
| 655 | + ppadplus, |
| 656 | + ppadminus |
657 | 657 | }); |
658 | 658 | } |
659 | 659 | } |
@@ -757,51 +757,56 @@ function normalizeBars(sa, sieve, opts) { |
757 | 757 | } |
758 | 758 | } |
759 | 759 |
|
760 | | - const extraPad = estimateAxisPaddingForText(fullTrace, calcTrace); |
| 760 | + const { ppadminus, ppadplus } = estimateAxisPaddingForText(fullTrace, calcTrace); |
761 | 761 | 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 |
766 | 766 | }); |
767 | 767 | } |
768 | 768 | } |
769 | 769 |
|
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 | + */ |
783 | 785 | function estimateAxisPaddingForText(trace, calcTrace) { |
784 | 786 | if ( |
785 | 787 | trace.orientation === 'v' && |
786 | 788 | (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) |
789 | 791 | ) { |
790 | 792 | // count number of lines by counting <br> elements |
791 | 793 | function countLines(text) { |
792 | 794 | if (!text || typeof text !== 'string') return 0; |
793 | 795 | return (text.match(BR_TAG_ALL) || []).length + 1; |
794 | 796 | } |
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 | + } |
800 | 805 |
|
801 | 806 | const padAmount = trace.outsidetextfont.size * LINE_SPACING * nLines + TEXTPAD; |
802 | 807 | 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) |
805 | 810 | ppadplus: calcTrace.some((bar) => bar.s < 0) ? padAmount : 0, |
806 | 811 | ppadminus: calcTrace.some((bar) => bar.s >= 0) ? padAmount : 0 |
807 | 812 | }; |
|
0 commit comments