diff --git a/lib/morris.grid.coffee b/lib/morris.grid.coffee index b7b46e4..8cc471b 100644 --- a/lib/morris.grid.coffee +++ b/lib/morris.grid.coffee @@ -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() diff --git a/spec/lib/grid/set_data_spec.coffee b/spec/lib/grid/set_data_spec.coffee index c7e0a1e..57150a7 100644 --- a/spec/lib/grid/set_data_spec.coffee +++ b/spec/lib/grid/set_data_spec.coffee @@ -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