Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto Rotate & Skip Labels based on "scaleXMaxHeight" #864

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 3 additions & 0 deletions docs/00-Getting-Started.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ Chart.defaults.global = {
// String - Scale label font colour
scaleFontColor: "#666",

// Number - Set the max height of the x-scale. Rotation and Label Skipping will auto fit to this height.
scaleXMaxHeight: false,

// Boolean - whether or not the chart should be responsive and resize when the browser does.
responsive: false,

Expand Down
3 changes: 2 additions & 1 deletion src/Chart.Bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@
gridLineColor : (this.options.scaleShowGridLines) ? this.options.scaleGridLineColor : "rgba(0,0,0,0)",
padding : (this.options.showScale) ? 0 : (this.options.barShowStroke) ? this.options.barStrokeWidth : 0,
showLabels : this.options.scaleShowLabels,
display : this.options.showScale
display : this.options.showScale,
scaleXMaxHeight : this.options.scaleXMaxHeight
};

if (this.options.scaleOverride){
Expand Down
14 changes: 13 additions & 1 deletion src/Chart.Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@
// String - Scale label font colour
scaleFontColor: "#666",

// Number - Set the max height of the x-scale. Rotation and Label Skipping will auto fit to this height.
scaleXMaxHeight: false,

// Boolean - whether or not the chart should be responsive and resize when the browser does.
responsive: false,

Expand Down Expand Up @@ -1516,24 +1519,30 @@

var firstWidth = this.ctx.measureText(this.xLabels[0]).width,
lastWidth = this.ctx.measureText(this.xLabels[this.xLabels.length - 1]).width,
maxRotation = 90,
firstRotated,
lastRotated;


this.xScalePaddingRight = lastWidth/2 + 3;
this.xScalePaddingLeft = (firstWidth/2 > this.yLabelWidth + 10) ? firstWidth/2 : this.yLabelWidth + 10;


this.xLabelRotation = 0;
if (this.display){
var originalLabelWidth = longestText(this.ctx,this.font,this.xLabels),
cosRotation,
firstRotatedWidth;
this.xLabelWidth = originalLabelWidth;
if(this.xLabelWidth > this.scaleXMaxHeight){
this.labelFrequency = this.xLabelWidth / this.scaleXMaxHeight;
maxRotation = maxRotation / this.labelFrequency;
}
//Allow 3 pixels x2 padding either side for label readability
var xGridWidth = Math.floor(this.calculateX(1) - this.calculateX(0)) - 6;

//Max label rotate should be 90 - also act as a loop counter
while ((this.xLabelWidth > xGridWidth && this.xLabelRotation === 0) || (this.xLabelWidth > xGridWidth && this.xLabelRotation <= 90 && this.xLabelRotation > 0)){
while ((this.xLabelWidth > xGridWidth && this.xLabelRotation === 0) || (this.xLabelWidth > xGridWidth && this.xLabelRotation <= maxRotation && this.xLabelRotation > 0)){
cosRotation = Math.cos(toRadians(this.xLabelRotation));

firstRotated = cosRotation * firstWidth;
Expand Down Expand Up @@ -1645,6 +1654,9 @@
},this);

each(this.xLabels,function(label,index){
if(index % Math.ceil(this.labelFrequency)){
return;
}
var xPos = this.calculateX(index) + aliasPixel(this.lineWidth),
// Check to see if line/bar here and decide where to place the line
linePos = this.calculateX(index - (this.offsetGridLines ? 0.5 : 0)) + aliasPixel(this.lineWidth),
Expand Down
3 changes: 2 additions & 1 deletion src/Chart.Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@
gridLineColor : (this.options.scaleShowGridLines) ? this.options.scaleGridLineColor : "rgba(0,0,0,0)",
padding: (this.options.showScale) ? 0 : this.options.pointDotRadius + this.options.pointDotStrokeWidth,
showLabels : this.options.scaleShowLabels,
display : this.options.showScale
display : this.options.showScale,
scaleXMaxHeight : this.options.scaleXMaxHeight
};

if (this.options.scaleOverride){
Expand Down