Merge pull request #144 from chriserin/refactor_ybounds

break the ymin/ymax logic out into one method
This commit is contained in:
Olly Smith 2013-01-24 15:56:45 -08:00
commit 24306b8b00
2 changed files with 36 additions and 58 deletions

View file

@ -151,30 +151,9 @@ class Morris.Grid extends Morris.EventEmitter
@xmin -= 1 @xmin -= 1
@xmax += 1 @xmax += 1
# Compute the vertical range of the graph if desired @ymin = @yboundary('min', ymin)
if typeof @options.ymax is 'string' @ymax = @yboundary('max', ymax)
if @options.ymax[0..3] is 'auto'
# use Array.concat to flatten arrays and find the max y value
if @options.ymax.length > 5
@ymax = parseInt(@options.ymax[5..], 10)
@ymax = Math.max(ymax, @ymax) if ymax?
else
@ymax = if ymax? then ymax else 0
else
@ymax = parseInt(@options.ymax, 10)
else
@ymax = @options.ymax
if typeof @options.ymin is 'string'
if @options.ymin[0..3] is 'auto'
if @options.ymin.length > 5
@ymin = parseInt(@options.ymin[5..], 10)
@ymin = Math.min(ymin, @ymin) if ymin?
else
@ymin = if ymin isnt null then ymin else 0
else
@ymin = parseInt(@options.ymin, 10)
else
@ymin = @options.ymin
if @ymin is @ymax if @ymin is @ymax
@ymin -= 1 if ymin @ymin -= 1 if ymin
@ymax += 1 @ymax += 1
@ -188,6 +167,21 @@ class Morris.Grid extends Morris.EventEmitter
@dirty = true @dirty = true
@redraw() if redraw @redraw() if redraw
yboundary: (boundaryType, currentValue) ->
boundaryOption = @options["y#{boundaryType}"]
if typeof boundaryOption is 'string'
if boundaryOption[0..3] is 'auto'
if boundaryOption.length > 5
suggestedValue = parseInt(boundaryOption[5..], 10)
return suggestedValue unless currentValue?
Math[boundaryType](currentValue, suggestedValue)
else
if currentValue? then currentValue else 0
else
parseInt(boundaryOption, 10)
else
boundaryOption
_calc: -> _calc: ->
w = @el.width() w = @el.width()
h = @el.height() h = @el.height()

View file

@ -12,14 +12,16 @@ describe 'Morris.Grid#setData', ->
my_data.should.deep.equal expected_data my_data.should.deep.equal expected_data
describe 'ymin/ymax', -> describe 'ymin/ymax', ->
beforeEach ->
it 'should use a user-specified minimum and maximum value', -> @defaults =
line = Morris.Line
element: 'graph' element: 'graph'
data: [{x: 1, y: 1}]
xkey: 'x' xkey: 'x'
ykeys: ['y', 'z'] ykeys: ['y', 'z']
labels: ['y', 'z'] labels: ['y', 'z']
it 'should use a user-specified minimum and maximum value', ->
line = Morris.Line $.extend @defaults,
data: [{x: 1, y: 1}]
ymin: 10 ymin: 10
ymax: 20 ymax: 20
line.ymin.should.equal 10 line.ymin.should.equal 10
@ -28,22 +30,16 @@ describe 'Morris.Grid#setData', ->
describe 'auto', -> describe 'auto', ->
it 'should automatically calculate the minimum and maximum value', -> it 'should automatically calculate the minimum and maximum value', ->
line = Morris.Line line = Morris.Line $.extend @defaults,
element: 'graph'
data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}] data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}]
xkey: 'x'
ykeys: ['y', 'z']
labels: ['y', 'z']
ymin: 'auto' ymin: 'auto'
ymax: 'auto' ymax: 'auto'
line.ymin.should.equal 10 line.ymin.should.equal 10
line.ymax.should.equal 15 line.ymax.should.equal 15
line = Morris.Line
element: 'graph' it 'should automatically calculate the minimum and maximum value given no y data', ->
line = Morris.Line $.extend @defaults,
data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}] data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}]
xkey: 'x'
ykeys: ['y', 'z']
labels: ['y', 'z']
ymin: 'auto' ymin: 'auto'
ymax: 'auto' ymax: 'auto'
line.ymin.should.equal 0 line.ymin.should.equal 0
@ -52,44 +48,32 @@ describe 'Morris.Grid#setData', ->
describe 'auto [n]', -> describe 'auto [n]', ->
it 'should automatically calculate the minimum and maximum value', -> it 'should automatically calculate the minimum and maximum value', ->
line = Morris.Line line = Morris.Line $.extend @defaults,
element: 'graph'
data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}] data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}]
xkey: 'x'
ykeys: ['y', 'z']
labels: ['y', 'z']
ymin: 'auto 11' ymin: 'auto 11'
ymax: 'auto 13' ymax: 'auto 13'
line.ymin.should.equal 10 line.ymin.should.equal 10
line.ymax.should.equal 15 line.ymax.should.equal 15
line = Morris.Line
element: 'graph' it 'should automatically calculate the minimum and maximum value given no data', ->
line = Morris.Line $.extend @defaults,
data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}] data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}]
xkey: 'x'
ykeys: ['y', 'z']
labels: ['y', 'z']
ymin: 'auto 11' ymin: 'auto 11'
ymax: 'auto 13' ymax: 'auto 13'
line.ymin.should.equal 11 line.ymin.should.equal 11
line.ymax.should.equal 13 line.ymax.should.equal 13
it 'should use a user-specified minimum and maximum value', -> it 'should use a user-specified minimum and maximum value', ->
line = Morris.Line line = Morris.Line $.extend @defaults,
element: 'graph'
data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}] data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}]
xkey: 'x'
ykeys: ['y', 'z']
labels: ['y', 'z']
ymin: 'auto 5' ymin: 'auto 5'
ymax: 'auto 20' ymax: 'auto 20'
line.ymin.should.equal 5 line.ymin.should.equal 5
line.ymax.should.equal 20 line.ymax.should.equal 20
line = Morris.Line
element: 'graph' it 'should use a user-specified minimum and maximum value given no data', ->
line = Morris.Line $.extend @defaults,
data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}] data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}]
xkey: 'x'
ykeys: ['y', 'z']
labels: ['y', 'z']
ymin: 'auto 5' ymin: 'auto 5'
ymax: 'auto 20' ymax: 'auto 20'
line.ymin.should.equal 5 line.ymin.should.equal 5