Skip to content

Commit d05239d

Browse files
committed
Lazily compute label sizes
1 parent db6a6d5 commit d05239d

File tree

1 file changed

+46
-31
lines changed

1 file changed

+46
-31
lines changed

src/core/core.scale.js

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ var Scale = Element.extend({
257257
bottom: 0
258258
}, margins);
259259

260+
me._labelSizes = null;
260261
me._maxLabelLines = 0;
261262
me.longestLabelWidth = 0;
262263
me.longestTextCache = me.longestTextCache || {};
@@ -419,31 +420,31 @@ var Scale = Element.extend({
419420
var labelRotation = minRotation;
420421
var labelSizes, maxLabelWidth, maxLabelHeight, maxWidth, tickWidth, maxHeight, maxLabelDiagonal;
421422

422-
if (me._isVisible() && tickOpts.display) {
423-
labelSizes = me._labelSizes = computeLabelSizes(me.ctx, parseTickFontOptions(tickOpts), ticks, me.longestTextCache);
424-
425-
if (minRotation < maxRotation && ticks.length > 1 && me.isHorizontal()) {
426-
maxLabelWidth = labelSizes.widest.width;
427-
maxLabelHeight = labelSizes.highest.height - labelSizes.highest.offset;
428-
429-
// Estimate the width of each grid based on the canvas width, the maximum
430-
// label width and the number of tick intervals
431-
maxWidth = Math.min(me.maxWidth, me.chart.width - maxLabelWidth);
432-
tickWidth = options.offset ? me.maxWidth / ticks.length : maxWidth / (ticks.length - 1);
433-
434-
// Allow 3 pixels x2 padding either side for label readability
435-
if (maxLabelWidth + 6 > tickWidth) {
436-
tickWidth = maxWidth / (ticks.length - (options.offset ? 0.5 : 1));
437-
maxHeight = me.maxHeight - getTickMarkLength(options.gridLines)
438-
- tickOpts.padding - getScaleLabelHeight(options.scaleLabel);
439-
maxLabelDiagonal = Math.sqrt(maxLabelWidth * maxLabelWidth + maxLabelHeight * maxLabelHeight);
440-
labelRotation = helpers.toDegrees(Math.min(
441-
Math.asin(Math.min((labelSizes.highest.height + 6) / tickWidth, 1)),
442-
Math.asin(Math.min(maxHeight / maxLabelDiagonal, 1)) - Math.asin(maxLabelHeight / maxLabelDiagonal)
443-
));
444-
labelRotation = Math.max(minRotation, Math.min(maxRotation, labelRotation));
445-
}
446-
}
423+
if (!me._isVisible() || !tickOpts.display || minRotation >= maxRotation || ticks.length <= 1 || !me.isHorizontal()) {
424+
me.labelRotation = minRotation;
425+
return;
426+
}
427+
428+
labelSizes = me._getLabelSizes();
429+
maxLabelWidth = labelSizes.widest.width;
430+
maxLabelHeight = labelSizes.highest.height - labelSizes.highest.offset;
431+
432+
// Estimate the width of each grid based on the canvas width, the maximum
433+
// label width and the number of tick intervals
434+
maxWidth = Math.min(me.maxWidth, me.chart.width - maxLabelWidth);
435+
tickWidth = options.offset ? me.maxWidth / ticks.length : maxWidth / (ticks.length - 1);
436+
437+
// Allow 3 pixels x2 padding either side for label readability
438+
if (maxLabelWidth + 6 > tickWidth) {
439+
tickWidth = maxWidth / (ticks.length - (options.offset ? 0.5 : 1));
440+
maxHeight = me.maxHeight - getTickMarkLength(options.gridLines)
441+
- tickOpts.padding - getScaleLabelHeight(options.scaleLabel);
442+
maxLabelDiagonal = Math.sqrt(maxLabelWidth * maxLabelWidth + maxLabelHeight * maxLabelHeight);
443+
labelRotation = helpers.toDegrees(Math.min(
444+
Math.asin(Math.min((labelSizes.highest.height + 6) / tickWidth, 1)),
445+
Math.asin(Math.min(maxHeight / maxLabelDiagonal, 1)) - Math.asin(maxLabelHeight / maxLabelDiagonal)
446+
));
447+
labelRotation = Math.max(minRotation, Math.min(maxRotation, labelRotation));
447448
}
448449

449450
me.labelRotation = labelRotation;
@@ -491,7 +492,7 @@ var Scale = Element.extend({
491492
// Don't bother fitting the ticks if we are not showing the labels
492493
if (tickOpts.display && display) {
493494
var tickFonts = parseTickFontOptions(tickOpts);
494-
var labelSizes = me._labelSizes;
495+
var labelSizes = me._getLabelSizes();
495496
var firstLabelSize = labelSizes.first;
496497
var lastLabelSize = labelSizes.last;
497498
var widestLabelSize = labelSizes.widest;
@@ -501,8 +502,6 @@ var Scale = Element.extend({
501502

502503
if (isHorizontal) {
503504
// A horizontal axis is more constrained by the height.
504-
me.longestLabelWidth = widestLabelSize.width;
505-
506505
var isRotated = me.labelRotation !== 0;
507506
var angleRadians = helpers.toRadians(me.labelRotation);
508507
var cosRotation = Math.cos(angleRadians);
@@ -610,8 +609,24 @@ var Scale = Element.extend({
610609
},
611610

612611
/**
613-
* @private
614-
*/
612+
* @private
613+
*/
614+
_getLabelSizes: function() {
615+
var me = this;
616+
var labelSizes;
617+
618+
if (me._labelSizes) {
619+
return me._labelSizes;
620+
}
621+
622+
me._labelSizes = labelSizes = computeLabelSizes(me.ctx, parseTickFontOptions(me.options.ticks), me.getTicks(), me.longestTextCache);
623+
me.longestLabelWidth = labelSizes.widest.width;
624+
return labelSizes;
625+
},
626+
627+
/**
628+
* @private
629+
*/
615630
_parseValue: function(value) {
616631
var start, end, min, max;
617632

@@ -782,7 +797,7 @@ var Scale = Element.extend({
782797
var cos = Math.abs(Math.cos(rot));
783798
var sin = Math.abs(Math.sin(rot));
784799

785-
var labelSizes = me._labelSizes;
800+
var labelSizes = me._getLabelSizes();
786801
var padding = optionTicks.autoSkipPadding || 0;
787802
var w = labelSizes ? labelSizes.widest.width + padding : 0;
788803
var h = labelSizes ? labelSizes.highest.height + padding : 0;

0 commit comments

Comments
 (0)