Skip to content

Commit

Permalink
Hide chart when setData is passed empty or null array. (Fixes #142)
Browse files Browse the repository at this point in the history
  • Loading branch information
oesmith committed Feb 6, 2013
1 parent a83c7e0 commit 811dc87
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/morris.bar.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class Morris.Bar extends Morris.Grid
# hit test - returns the index of the row beneath the given coordinate
#
hitTest: (x, y) ->
return null if @data.length == 0
x = Math.max(Math.min(x, @right), @left)
Math.min(@data.length - 1,
Math.floor((x - @left) / (@width / @data.length)))
Expand Down
6 changes: 6 additions & 0 deletions lib/morris.grid.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ class Morris.Grid extends Morris.EventEmitter
# Update the data series and redraw the chart.
#
setData: (data, redraw = true) ->
if !data? or data.length == 0
@data = []
@raphael.clear()
@hover.hide() if @hover?
return

ymax = if @cumulative then 0 else null
ymin = if @cumulative then 0 else null

Expand Down
1 change: 1 addition & 0 deletions lib/morris.line.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Morris.Line extends Morris.Grid
# hit test - returns the index of the row beneath the given coordinate
#
hitTest: (x, y) ->
return null if @data.length == 0
# TODO better search algo
for r, index in @data.slice(1)
break if x < (r._x + @data[index]._x) / 2
Expand Down
14 changes: 14 additions & 0 deletions morris.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion morris.min.js

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion spec/lib/grid/set_data_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,15 @@ describe 'Morris.Grid#setData', ->
line.ymax.should == 16
line.data.map((row) -> row.y).should.deep.equal [[13.5], [12], [16], [14]]


it 'should clear the chart when empty data is supplied', ->
line = Morris.Line
element: 'graph',
data: [{x: 2, y: '12'}, {x: 1, y: '13.5'}, {x: 4, y: '14'}, {x: 3, y: '16'}]
xkey: 'x'
ykeys: ['y']
labels: ['y']
line.data.length.should.equal 4
line.setData([])
line.data.length.should.equal 0
line.setData([{x: 2, y: '12'}, {x: 1, y: '13.5'}, {x: 4, y: '14'}, {x: 3, y: '16'}])
line.data.length.should.equal 4

0 comments on commit 811dc87

Please sign in to comment.