Hide chart when setData is passed empty or null array. (Fixes #142)

This commit is contained in:
Olly Smith 2013-02-06 07:35:54 +00:00
parent a83c7e07f8
commit 811dc87d8b
6 changed files with 35 additions and 2 deletions

View File

@ -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)))

View File

@ -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

View File

@ -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

View File

@ -145,6 +145,14 @@
if (redraw == null) {
redraw = true;
}
if (!(data != null) || data.length === 0) {
this.data = [];
this.raphael.clear();
if (this.hover != null) {
this.hover.hide();
}
return;
}
ymax = this.cumulative ? 0 : null;
ymin = this.cumulative ? 0 : null;
if (this.options.goals.length > 0) {
@ -636,6 +644,9 @@
Line.prototype.hitTest = function(x, y) {
var index, r, _i, _len, _ref;
if (this.data.length === 0) {
return null;
}
_ref = this.data.slice(1);
for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) {
r = _ref[index];
@ -1291,6 +1302,9 @@
};
Bar.prototype.hitTest = function(x, y) {
if (this.data.length === 0) {
return null;
}
x = Math.max(Math.min(x, this.right), this.left);
return Math.min(this.data.length - 1, Math.floor((x - this.left) / (this.width / this.data.length)));
};

2
morris.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -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