Skip to content

Commit

Permalink
Added showXLabels for Line and Bar charts.
Browse files Browse the repository at this point in the history
It enables controlling the number of labels shown on x axis. Really helpful in case there are huge number of labels.

Chart.Core.js has been edited  so, showXLabels option is available to any other chart as well if anybody wants to extend this feature.
  • Loading branch information
Prashant committed Jan 29, 2015
1 parent 87b7f80 commit 40f0305
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/01-Line-Chart.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ These are the customisation options specific to Line charts. These options are m
```javascript
{

//Boolean or Number - Number of labels to be shown on X-Axis. false: no label displayed, true: all labels
displayed, 10: only 10 labels displayed
showXLabels: 10,

///Boolean - Whether grid lines are shown across the chart
scaleShowGridLines : true,

Expand Down
4 changes: 4 additions & 0 deletions docs/02-Bar-Chart.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ These are the customisation options specific to Bar charts. These options are me

```javascript
{
//Boolean or Number - Number of labels to be shown on X-Axis. false: no label displayed, true: all labels
displayed, 10: only 10 labels displayed
showXLabels: 10,

//Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
scaleBeginAtZero : true,

Expand Down
1 change: 1 addition & 0 deletions src/Chart.Bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
helpers.extend(this, updatedRanges);
},
xLabels : labels,
showXLabels: (this.options.showXLabels) ? this.options.showXLabels : true,
font : helpers.fontString(this.options.scaleFontSize, this.options.scaleFontStyle, this.options.scaleFontFamily),
lineWidth : this.options.scaleLineWidth,
lineColor : this.options.scaleLineColor,
Expand Down
10 changes: 9 additions & 1 deletion src/Chart.Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@
// Boolean - Whether to show labels on the scale
scaleShowLabels: true,

// Boolean or a positive integer denoting number of labels to be shown on x axis
showXLabels: true,

// Interpolated JS string - can access value
scaleLabel: "<%=value%>",

Expand Down Expand Up @@ -1644,6 +1647,9 @@

},this);

// if showXLabels is a number then divide and determine how many xLabels to skip before showing next label
// else, if showXLabels is true, print all labels, else never print
this.xLabelsSkipper = isNumber(this.showXLabels) ? Math.ceil(this.xLabels.length/this.showXLabels) : (this.showXLabels === true) ? 1 : this.xLabels.length+1;
each(this.xLabels,function(label,index){
var xPos = this.calculateX(index) + aliasPixel(this.lineWidth),
// Check to see if line/bar here and decide where to place the line
Expand Down Expand Up @@ -1695,7 +1701,9 @@
ctx.font = this.font;
ctx.textAlign = (isRotated) ? "right" : "center";
ctx.textBaseline = (isRotated) ? "middle" : "top";
ctx.fillText(label, 0, 0);
if(index % this.xLabelsSkipper === 0) {
ctx.fillText(label, 0, 0);
}
ctx.restore();
},this);

Expand Down
1 change: 1 addition & 0 deletions src/Chart.Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
helpers.extend(this, updatedRanges);
},
xLabels : labels,
showXLabels: (this.options.showXLabels) ? this.options.showXLabels : true,
font : helpers.fontString(this.options.scaleFontSize, this.options.scaleFontStyle, this.options.scaleFontFamily),
lineWidth : this.options.scaleLineWidth,
lineColor : this.options.scaleLineColor,
Expand Down

0 comments on commit 40f0305

Please sign in to comment.