Skip to content

Commit c6c4db7

Browse files
benmccannsimonbrunel
authored andcommitted
Simplify and cleanup the scale _autoskip method (#6043)
1 parent 7c11c81 commit c6c4db7

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

src/core/core.scale.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -625,31 +625,16 @@ module.exports = Element.extend({
625625
* @private
626626
*/
627627
_autoSkip: function(ticks) {
628-
var skipRatio;
629628
var me = this;
630629
var isHorizontal = me.isHorizontal();
631630
var optionTicks = me.options.ticks.minor;
632631
var tickCount = ticks.length;
633-
634-
// Calculate space needed by label in axis direction.
635-
var rot = helpers.toRadians(me.labelRotation);
636-
var cos = Math.abs(Math.cos(rot));
637-
var sin = Math.abs(Math.sin(rot));
638-
639-
var padding = optionTicks.autoSkipPadding;
640-
var w = me.longestLabelWidth + padding || 0;
641-
642-
var tickFont = helpers.options._parseFont(optionTicks);
643-
var h = me._maxLabelLines * tickFont.lineHeight + padding;
644-
645-
// Calculate space needed for 1 tick in axis direction.
646-
var tickSize = isHorizontal
647-
? h * cos > w * sin ? w / cos : h / sin
648-
: h * sin < w * cos ? h / cos : w / sin;
632+
var skipRatio = false;
633+
var maxTicks = optionTicks.maxTicksLimit;
649634

650635
// Total space needed to display all ticks. First and last ticks are
651636
// drawn as their center at end of axis, so tickCount-1
652-
var ticksLength = tickSize * (tickCount - 1);
637+
var ticksLength = me._tickSize() * (tickCount - 1);
653638

654639
// Axis length
655640
var axisLength = isHorizontal
@@ -659,21 +644,13 @@ module.exports = Element.extend({
659644
var result = [];
660645
var i, tick;
661646

662-
// figure out the maximum number of gridlines to show
663-
var maxTicks;
664-
if (optionTicks.maxTicksLimit) {
665-
maxTicks = optionTicks.maxTicksLimit;
666-
}
667-
668-
skipRatio = false;
669-
670647
if (ticksLength > axisLength) {
671648
skipRatio = 1 + Math.floor(ticksLength / axisLength);
672649
}
673650

674651
// if they defined a max number of optionTicks,
675652
// increase skipRatio until that number is met
676-
if (maxTicks && tickCount > maxTicks) {
653+
if (tickCount > maxTicks) {
677654
skipRatio = Math.max(skipRatio, 1 + Math.floor(tickCount / maxTicks));
678655
}
679656

@@ -689,6 +666,31 @@ module.exports = Element.extend({
689666
return result;
690667
},
691668

669+
/**
670+
* @private
671+
*/
672+
_tickSize: function() {
673+
var me = this;
674+
var isHorizontal = me.isHorizontal();
675+
var optionTicks = me.options.ticks.minor;
676+
677+
// Calculate space needed by label in axis direction.
678+
var rot = helpers.toRadians(me.labelRotation);
679+
var cos = Math.abs(Math.cos(rot));
680+
var sin = Math.abs(Math.sin(rot));
681+
682+
var padding = optionTicks.autoSkipPadding;
683+
var w = me.longestLabelWidth + padding || 0;
684+
685+
var tickFont = helpers.options._parseFont(optionTicks);
686+
var h = me._maxLabelLines * tickFont.lineHeight + padding;
687+
688+
// Calculate space needed for 1 tick in axis direction.
689+
return isHorizontal
690+
? h * cos > w * sin ? w / cos : h / sin
691+
: h * sin < w * cos ? h / cos : w / sin;
692+
},
693+
692694
/**
693695
* @private
694696
*/

0 commit comments

Comments
 (0)