Skip to content

Commit

Permalink
_getLabelSize()
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed May 8, 2019
1 parent 5f009ef commit 1f63c38
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/scales/scale.time.js
Expand Up @@ -753,10 +753,9 @@ module.exports = Scale.extend({
},

/**
* Crude approximation of what the label width might be
* @private
*/
getLabelWidth: function(label) {
_getLabelSize: function(label) {
var me = this;
var ticksOpts = me.options.ticks;
var tickLabelWidth = me.ctx.measureText(label).width;
Expand All @@ -765,9 +764,18 @@ module.exports = Scale.extend({
var sinRotation = Math.sin(angle);
var tickFontSize = valueOrDefault(ticksOpts.fontSize, defaults.global.defaultFontSize);

return me.isHorizontal()
? (tickLabelWidth * cosRotation) + (tickFontSize * sinRotation)
: (tickLabelWidth * sinRotation) + (tickFontSize * cosRotation);
return {
w: (tickLabelWidth * cosRotation) + (tickFontSize * sinRotation),
h: (tickLabelWidth * sinRotation) + (tickFontSize * cosRotation)
};
},

/**
* Crude approximation of what the label width might be
* @private
*/
getLabelWidth: function(label) {
return this._getLabelSize(label).w;
},

/**
Expand All @@ -777,14 +785,10 @@ module.exports = Scale.extend({
var me = this;
var timeOpts = me.options.time;
var displayFormats = timeOpts.displayFormats;

// pick the longest format (milliseconds) for guestimation
var format = displayFormats[timeOpts.unit] || displayFormats.millisecond;

var exampleLabel = me.tickFormatFunction(exampleTime, 0, [], format);
var tickLabelWidth = me.getLabelWidth(exampleLabel);
var innerWidth = me.isHorizontal() ? me.width : me.height;
var capacity = Math.floor(innerWidth / tickLabelWidth);
var size = me._getLabelSize(exampleLabel);
var capacity = Math.floor(me.isHorizontal() ? me.width / size.w : me.height / size.h);

return capacity > 0 ? capacity : 1;
}
Expand Down

0 comments on commit 1f63c38

Please sign in to comment.