Add xaxis and yaxis options for disabling axes individually

This commit is contained in:
Omar Khan 2013-06-20 12:56:10 +01:00
parent b98ae8ab1d
commit cccf319f38
5 changed files with 29 additions and 15 deletions

View File

@ -48,7 +48,7 @@ class Morris.Bar extends Morris.Grid
# Draws the bar chart. # Draws the bar chart.
# #
draw: -> draw: ->
@drawXAxis() if @options.axes @drawXAxis() if @options.xaxis
@drawSeries() @drawSeries()
# draw the x-axis labels # draw the x-axis labels

View File

@ -21,6 +21,11 @@ class Morris.Grid extends Morris.EventEmitter
if typeof @options.units is 'string' if typeof @options.units is 'string'
@options.postUnits = options.units @options.postUnits = options.units
# backwards compatibility for axes -> xaxis, yaxis
if @options.axes is false
@options.xaxis = false
@options.yaxis = false
# the raphael drawing instance # the raphael drawing instance
@raphael = new Raphael(@el[0]) @raphael = new Raphael(@el[0])
@ -59,7 +64,8 @@ class Morris.Grid extends Morris.EventEmitter
# #
gridDefaults: gridDefaults:
dateFormat: null dateFormat: null
axes: true xaxis: true
yaxis: true
grid: true grid: true
gridLineColor: '#aaa' gridLineColor: '#aaa'
gridStrokeWidth: 0.5 gridStrokeWidth: 0.5
@ -172,7 +178,7 @@ class Morris.Grid extends Morris.EventEmitter
@ymin -= 1 if ymin @ymin -= 1 if ymin
@ymax += 1 @ymax += 1
if @options.axes is true or @options.grid is true if @options.yaxis is true or @options.grid is true
if (@options.ymax == @gridDefaults.ymax and if (@options.ymax == @gridDefaults.ymax and
@options.ymin == @gridDefaults.ymin) @options.ymin == @gridDefaults.ymin)
# calculate 'magic' grid placement # calculate 'magic' grid placement
@ -241,10 +247,11 @@ class Morris.Grid extends Morris.EventEmitter
@right = @elementWidth - @options.padding @right = @elementWidth - @options.padding
@top = @options.padding @top = @options.padding
@bottom = @elementHeight - @options.padding @bottom = @elementHeight - @options.padding
if @options.axes if @options.yaxis
yLabelWidths = for gridLine in @grid yLabelWidths = for gridLine in @grid
@measureText(@yAxisFormat(gridLine)).width @measureText(@yAxisFormat(gridLine)).width
@left += Math.max(yLabelWidths...) @left += Math.max(yLabelWidths...)
if @options.xaxis
bottomOffsets = for i in [0...@data.length] bottomOffsets = for i in [0...@data.length]
@measureText(@data[i].text, -@options.xLabelAngle).height @measureText(@data[i].text, -@options.xLabelAngle).height
@bottom -= Math.max(bottomOffsets...) @bottom -= Math.max(bottomOffsets...)
@ -307,10 +314,10 @@ class Morris.Grid extends Morris.EventEmitter
# draw y axis labels, horizontal lines # draw y axis labels, horizontal lines
# #
drawGrid: -> drawGrid: ->
return if @options.grid is false and @options.axes is false return if @options.grid is false and @options.yaxis is false
for lineY in @grid for lineY in @grid
y = @transY(lineY) y = @transY(lineY)
if @options.axes if @options.yaxis
@drawYAxisLabel(@left - @options.padding / 2, y, @yAxisFormat(lineY)) @drawYAxisLabel(@left - @options.padding / 2, y, @yAxisFormat(lineY))
if @options.grid if @options.grid
@drawGridLine("M#{@left},#{y}H#{@left + @width}") @drawGridLine("M#{@left},#{y}H#{@left + @width}")

View File

@ -133,7 +133,7 @@ class Morris.Line extends Morris.Grid
# Draws the line chart. # Draws the line chart.
# #
draw: -> draw: ->
@drawXAxis() if @options.axes @drawXAxis() if @options.xaxis
@drawSeries() @drawSeries()
if @options.hideHover is false if @options.hideHover is false
@displayHoverForRow(@data.length - 1) @displayHoverForRow(@data.length - 1)

View File

@ -85,6 +85,10 @@
if (typeof this.options.units === 'string') { if (typeof this.options.units === 'string') {
this.options.postUnits = options.units; this.options.postUnits = options.units;
} }
if (this.options.axes === false) {
this.options.xaxis = false;
this.options.yaxis = false;
}
this.raphael = new Raphael(this.el[0]); this.raphael = new Raphael(this.el[0]);
this.elementWidth = null; this.elementWidth = null;
this.elementHeight = null; this.elementHeight = null;
@ -120,7 +124,8 @@
Grid.prototype.gridDefaults = { Grid.prototype.gridDefaults = {
dateFormat: null, dateFormat: null,
axes: true, xaxis: true,
yaxis: true,
grid: true, grid: true,
gridLineColor: '#aaa', gridLineColor: '#aaa',
gridStrokeWidth: 0.5, gridStrokeWidth: 0.5,
@ -260,7 +265,7 @@
} }
this.ymax += 1; this.ymax += 1;
} }
if (this.options.axes === true || this.options.grid === true) { if (this.options.yaxis === true || this.options.grid === true) {
if (this.options.ymax === this.gridDefaults.ymax && this.options.ymin === this.gridDefaults.ymin) { if (this.options.ymax === this.gridDefaults.ymax && this.options.ymin === this.gridDefaults.ymin) {
this.grid = this.autoGridLines(this.ymin, this.ymax, this.options.numLines); this.grid = this.autoGridLines(this.ymin, this.ymax, this.options.numLines);
this.ymin = Math.min(this.ymin, this.grid[0]); this.ymin = Math.min(this.ymin, this.grid[0]);
@ -360,7 +365,7 @@
this.right = this.elementWidth - this.options.padding; this.right = this.elementWidth - this.options.padding;
this.top = this.options.padding; this.top = this.options.padding;
this.bottom = this.elementHeight - this.options.padding; this.bottom = this.elementHeight - this.options.padding;
if (this.options.axes) { if (this.options.yaxis) {
yLabelWidths = (function() { yLabelWidths = (function() {
var _i, _len, _ref, _results; var _i, _len, _ref, _results;
_ref = this.grid; _ref = this.grid;
@ -372,6 +377,8 @@
return _results; return _results;
}).call(this); }).call(this);
this.left += Math.max.apply(Math, yLabelWidths); this.left += Math.max.apply(Math, yLabelWidths);
}
if (this.options.xaxis) {
bottomOffsets = (function() { bottomOffsets = (function() {
var _i, _ref, _results; var _i, _ref, _results;
_results = []; _results = [];
@ -448,7 +455,7 @@
Grid.prototype.drawGrid = function() { Grid.prototype.drawGrid = function() {
var lineY, y, _i, _len, _ref, _results; var lineY, y, _i, _len, _ref, _results;
if (this.options.grid === false && this.options.axes === false) { if (this.options.grid === false && this.options.yaxis === false) {
return; return;
} }
_ref = this.grid; _ref = this.grid;
@ -456,7 +463,7 @@
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
lineY = _ref[_i]; lineY = _ref[_i];
y = this.transY(lineY); y = this.transY(lineY);
if (this.options.axes) { if (this.options.yaxis) {
this.drawYAxisLabel(this.left - this.options.padding / 2, y, this.yAxisFormat(lineY)); this.drawYAxisLabel(this.left - this.options.padding / 2, y, this.yAxisFormat(lineY));
} }
if (this.options.grid) { if (this.options.grid) {
@ -828,7 +835,7 @@
}; };
Line.prototype.draw = function() { Line.prototype.draw = function() {
if (this.options.axes) { if (this.options.xaxis) {
this.drawXAxis(); this.drawXAxis();
} }
this.drawSeries(); this.drawSeries();
@ -1349,7 +1356,7 @@
}; };
Bar.prototype.draw = function() { Bar.prototype.draw = function() {
if (this.options.axes) { if (this.options.xaxis) {
this.drawXAxis(); this.drawXAxis();
} }
return this.drawSeries(); return this.drawSeries();

2
morris.min.js vendored

File diff suppressed because one or more lines are too long