Skip to content

Commit

Permalink
Use margins instead of assuming padding of 1 label
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Mar 6, 2019
1 parent 5f31185 commit b5ffb4f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/core/core.scale.js
Expand Up @@ -325,6 +325,7 @@ module.exports = Element.extend({
var context = me.ctx;
var tickOpts = me.options.ticks;
var labels = labelsFromTicks(me._ticks);
var margins = me.margins;

// Get the width of each grid by calculating the difference
// between x offsets between 0 and 1.
Expand All @@ -339,7 +340,8 @@ module.exports = Element.extend({
var cosRotation, sinRotation;

// Allow 3 pixels x2 padding either side for label readability
var tickWidth = me.getPixelForTick(1) - me.getPixelForTick(0) - 6;
var numLabels = labels.length - (tickOpts.offset ? 0 : 1);
var tickWidth = (me.width - (margins.left + margins.right)) / numLabels;

// Max label rotation can be set or default to 90 - also act as a loop counter
while (labelWidth > tickWidth && labelRotation < tickOpts.maxRotation) {
Expand Down
7 changes: 5 additions & 2 deletions src/scales/scale.time.js
Expand Up @@ -752,13 +752,16 @@ module.exports = Scale.extend({
*/
getLabelCapacity: function(exampleTime) {
var me = this;
var margins = me.margins;

// pick the longest format (milliseconds) for guestimation
var format = me.options.time.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) - 1;
var innerWidth = me.isHorizontal()
? me.width - (margins.left + margins.right)
: me.height - (margins.top + margins.bottom);
var capacity = Math.floor(innerWidth / tickLabelWidth);

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

0 comments on commit b5ffb4f

Please sign in to comment.