Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/core/core.layoutService.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@
bottom: 0,
};

box.update(box.options.fullWidth ? chartWidth : maxChartAreaWidth, minBoxSize.minSize.height, scaleMargin);
// Don't use min size here because of label rotation. When the labels are rotated, their rotation highly depends
// on the margin. Sometimes they need to increase in size slightly
box.update(box.options.fullWidth ? chartWidth : maxChartAreaWidth, chartHeight / 2, scaleMargin);
} else {
box.update(minBoxSize.minSize.width, maxChartAreaHeight);
}
Expand Down
85 changes: 36 additions & 49 deletions src/core/core.scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,62 +194,54 @@
var firstWidth = this.ctx.measureText(this.ticks[0]).width;
var lastWidth = this.ctx.measureText(this.ticks[this.ticks.length - 1]).width;
var firstRotated;
var lastRotated;

this.paddingRight = lastWidth / 2 + 3;
this.paddingLeft = firstWidth / 2 + 3;

this.labelRotation = 0;
this.paddingRight = 0;
this.paddingLeft = 0;

if (this.options.display && this.isHorizontal()) {
var originalLabelWidth = helpers.longestText(this.ctx, labelFont, this.ticks);
var cosRotation;
var sinRotation;
if (this.options.display) {
if (this.isHorizontal()) {
this.paddingRight = lastWidth / 2 + 3;
this.paddingLeft = firstWidth / 2 + 3;

this.labelWidth = originalLabelWidth;
var originalLabelWidth = helpers.longestText(this.ctx, labelFont, this.ticks);
var labelWidth = originalLabelWidth;
var cosRotation;
var sinRotation;

// Allow 3 pixels x2 padding either side for label readability
// only the index matters for a dataset scale, but we want a consistent interface between scales
// Allow 3 pixels x2 padding either side for label readability
// only the index matters for a dataset scale, but we want a consistent interface between scales
var tickWidth = this.getPixelForTick(1) - this.getPixelForTick(0) - 6;

var tickWidth = this.getPixelForTick(1) - this.getPixelForTick(0) - 6;
//Max label rotation can be set or default to 90 - also act as a loop counter
while (labelWidth > tickWidth && this.labelRotation < this.options.ticks.maxRotation) {
cosRotation = Math.cos(helpers.toRadians(this.labelRotation));
sinRotation = Math.sin(helpers.toRadians(this.labelRotation));

//Max label rotation can be set or default to 90 - also act as a loop counter
while (this.labelWidth > tickWidth && this.labelRotation < this.options.ticks.maxRotation) {
cosRotation = Math.cos(helpers.toRadians(this.labelRotation));
sinRotation = Math.sin(helpers.toRadians(this.labelRotation));
firstRotated = cosRotation * firstWidth;

firstRotated = cosRotation * firstWidth;
lastRotated = cosRotation * lastWidth;
// We're right aligning the text now.
if (firstRotated + this.options.ticks.fontSize / 2 > this.yLabelWidth) {
this.paddingLeft = firstRotated + this.options.ticks.fontSize / 2;
}

// We're right aligning the text now.
if (firstRotated + this.options.ticks.fontSize / 2 > this.yLabelWidth) {
this.paddingLeft = firstRotated + this.options.ticks.fontSize / 2;
}
this.paddingRight = this.options.ticks.fontSize / 2;

this.paddingRight = this.options.ticks.fontSize / 2;
if (sinRotation * originalLabelWidth > this.maxHeight) {
// go back one step
this.labelRotation--;
break;
}

if (sinRotation * originalLabelWidth > this.maxHeight) {
// go back one step
this.labelRotation--;
break;
this.labelRotation++;
labelWidth = cosRotation * originalLabelWidth;
}

this.labelRotation++;
this.labelWidth = cosRotation * originalLabelWidth;

}
} else {
this.labelWidth = 0;
this.paddingRight = 0;
this.paddingLeft = 0;
}

if (this.margins) {
this.paddingLeft -= this.margins.left;
this.paddingRight -= this.margins.right;

this.paddingLeft = Math.max(this.paddingLeft, 0);
this.paddingRight = Math.max(this.paddingRight, 0);
this.paddingLeft = Math.max(this.paddingLeft - this.margins.left, 0);
this.paddingRight = Math.max(this.paddingRight - this.margins.right, 0);
}
},
afterCalculateTickRotation: function() {
Expand Down Expand Up @@ -342,15 +334,10 @@
}

if (this.margins) {
this.paddingLeft -= this.margins.left;
this.paddingTop -= this.margins.top;
this.paddingRight -= this.margins.right;
this.paddingBottom -= this.margins.bottom;

this.paddingLeft = Math.max(this.paddingLeft, 0);
this.paddingTop = Math.max(this.paddingTop, 0);
this.paddingRight = Math.max(this.paddingRight, 0);
this.paddingBottom = Math.max(this.paddingBottom, 0);
this.paddingLeft = Math.max(this.paddingLeft - this.margins.left, 0);
this.paddingTop = Math.max(this.paddingTop - this.margins.top, 0);
this.paddingRight = Math.max(this.paddingRight - this.margins.right, 0);
this.paddingBottom = Math.max(this.paddingBottom - this.margins.bottom, 0);
}

this.width = this.minSize.width;
Expand Down
20 changes: 10 additions & 10 deletions test/core.layoutService.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,25 +196,25 @@ describe('Test the layout service', function() {
left: 115,
right: 245,
top: 5,
bottom: 68.48521368620018,
bottom: 75,
});

// Is xScale at the right spot
expect(xScale.left).toBe(115);
expect(xScale.right).toBe(245);
expect(xScale.top).toBe(68.48521368620018);
expect(xScale.top).toBe(75);
expect(xScale.bottom).toBe(145);

// Are yScales at the right spot
expect(yScale1.left).toBe(5);
expect(yScale1.right).toBe(55);
expect(yScale1.top).toBe(5);
expect(yScale1.bottom).toBe(68.48521368620018);
expect(yScale1.bottom).toBe(75);

expect(yScale2.left).toBe(55);
expect(yScale2.right).toBe(115);
expect(yScale2.top).toBe(5);
expect(yScale2.bottom).toBe(68.48521368620018);
expect(yScale2.bottom).toBe(75);
});

// This is an oddball case. What happens is, when the scales are fit the first time they must fit within the assigned size. In this case,
Expand Down Expand Up @@ -336,25 +336,25 @@ describe('Test the layout service', function() {
expect(chartInstance.chartArea).toEqual({
left: 45,
right: 245,
top: 45,
bottom: 105,
top: 63.78307376628291,
bottom: 76.0423977855504,
});

// Are xScales at the right spot
expect(xScale1.left).toBe(45);
expect(xScale1.right).toBe(245);
expect(xScale1.top).toBe(105);
expect(xScale1.top).toBeCloseTo(76.04, 1e-3);
expect(xScale1.bottom).toBe(145);

expect(xScale2.left).toBe(5);
expect(xScale2.right).toBe(245);
expect(xScale2.top).toBe(5);
expect(xScale2.bottom).toBe(45);
expect(xScale2.bottom).toBeCloseTo(63.78, 1e-3);

// Is yScale at the right spot
expect(yScale.left).toBe(5);
expect(yScale.right).toBe(45);
expect(yScale.top).toBe(45);
expect(yScale.bottom).toBe(105);
expect(yScale.top).toBeCloseTo(63.78, 1e-3);
expect(yScale.bottom).toBeCloseTo(76.04, 1e-3);
});
});