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
@xmax += 1
# Compute the vertical range of the graph if desired
if typeof @options.ymax is 'string'
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
@ymin = @yboundary('min', ymin)
@ymax = @yboundary('max', ymax)
if @ymin is @ymax
@ymin -= 1 if ymin
@ymax += 1
@ -188,6 +167,21 @@ class Morris.Grid extends Morris.EventEmitter
@dirty = true
@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: ->
w = @el.width()
h = @el.height()

View File

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