]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Simplify and cleanup the scale _autoskip method (#6043)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Fri, 8 Feb 2019 06:10:02 +0000 (22:10 -0800)
committerSimon Brunel <simonbrunel@users.noreply.github.com>
Fri, 8 Feb 2019 06:10:02 +0000 (07:10 +0100)
src/core/core.scale.js

index ff73d98115fc95e868964152e7063a4505ae76da..5bbf6ebcbdde9d8f78ef41d3b312c7415181e1b6 100644 (file)
@@ -625,31 +625,16 @@ module.exports = Element.extend({
         * @private
         */
        _autoSkip: function(ticks) {
-               var skipRatio;
                var me = this;
                var isHorizontal = me.isHorizontal();
                var optionTicks = me.options.ticks.minor;
                var tickCount = ticks.length;
-
-               // Calculate space needed by label in axis direction.
-               var rot = helpers.toRadians(me.labelRotation);
-               var cos = Math.abs(Math.cos(rot));
-               var sin = Math.abs(Math.sin(rot));
-
-               var padding = optionTicks.autoSkipPadding;
-               var w = me.longestLabelWidth + padding || 0;
-
-               var tickFont = helpers.options._parseFont(optionTicks);
-               var h = me._maxLabelLines * tickFont.lineHeight + padding;
-
-               // Calculate space needed for 1 tick in axis direction.
-               var tickSize = isHorizontal
-                       ? h * cos > w * sin ? w / cos : h / sin
-                       : h * sin < w * cos ? h / cos : w / sin;
+               var skipRatio = false;
+               var maxTicks = optionTicks.maxTicksLimit;
 
                // Total space needed to display all ticks. First and last ticks are
                // drawn as their center at end of axis, so tickCount-1
-               var ticksLength = tickSize * (tickCount - 1);
+               var ticksLength = me._tickSize() * (tickCount - 1);
 
                // Axis length
                var axisLength = isHorizontal
@@ -659,21 +644,13 @@ module.exports = Element.extend({
                var result = [];
                var i, tick;
 
-               // figure out the maximum number of gridlines to show
-               var maxTicks;
-               if (optionTicks.maxTicksLimit) {
-                       maxTicks = optionTicks.maxTicksLimit;
-               }
-
-               skipRatio = false;
-
                if (ticksLength > axisLength) {
                        skipRatio = 1 + Math.floor(ticksLength / axisLength);
                }
 
                // if they defined a max number of optionTicks,
                // increase skipRatio until that number is met
-               if (maxTicks && tickCount > maxTicks) {
+               if (tickCount > maxTicks) {
                        skipRatio = Math.max(skipRatio, 1 + Math.floor(tickCount / maxTicks));
                }
 
@@ -689,6 +666,31 @@ module.exports = Element.extend({
                return result;
        },
 
+       /**
+        * @private
+        */
+       _tickSize: function() {
+               var me = this;
+               var isHorizontal = me.isHorizontal();
+               var optionTicks = me.options.ticks.minor;
+
+               // Calculate space needed by label in axis direction.
+               var rot = helpers.toRadians(me.labelRotation);
+               var cos = Math.abs(Math.cos(rot));
+               var sin = Math.abs(Math.sin(rot));
+
+               var padding = optionTicks.autoSkipPadding;
+               var w = me.longestLabelWidth + padding || 0;
+
+               var tickFont = helpers.options._parseFont(optionTicks);
+               var h = me._maxLabelLines * tickFont.lineHeight + padding;
+
+               // Calculate space needed for 1 tick in axis direction.
+               return isHorizontal
+                       ? h * cos > w * sin ? w / cos : h / sin
+                       : h * sin < w * cos ? h / cos : w / sin;
+       },
+
        /**
         * @private
         */