]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix Maximum call stack size exception in computeLabelSizes (#7883)
authorMatthew Crumley <email@matthewcrumley.com>
Sat, 17 Oct 2020 20:35:00 +0000 (16:35 -0400)
committerGitHub <noreply@github.com>
Sat, 17 Oct 2020 20:35:00 +0000 (23:35 +0300)
Calling Math.max with a large number of values was throwing an exception.
Tracking the current largest width and height as the widths and heights are
built up should be faster and avoids the exception.

src/core/core.scale.js

index 0c1413d4a0398ac2c126c8f63acb63bda9f8c412..cc45a0be645002f9e20a6fbb6e935d5593446c43 100644 (file)
@@ -130,6 +130,8 @@ function computeLabelSizes(ctx, tickFonts, ticks, caches) {
        var widths = [];
        var heights = [];
        var offsets = [];
+       var widestLabelSize = 0;
+       var highestLabelSize = 0;
        var i, j, jlen, label, tickFont, fontString, cache, lineHeight, width, height, nestedLabel, widest, highest;
 
        for (i = 0; i < length; ++i) {
@@ -157,11 +159,13 @@ function computeLabelSizes(ctx, tickFonts, ticks, caches) {
                widths.push(width);
                heights.push(height);
                offsets.push(lineHeight / 2);
+               widestLabelSize = Math.max(width, widestLabelSize);
+               highestLabelSize = Math.max(height, highestLabelSize);
        }
        garbageCollect(caches, length);
 
-       widest = widths.indexOf(Math.max.apply(null, widths));
-       highest = heights.indexOf(Math.max.apply(null, heights));
+       widest = widths.indexOf(widestLabelSize);
+       highest = heights.indexOf(highestLabelSize);
 
        function valueAt(idx) {
                return {