Improved support for empty charts.

This commit is contained in:
Olly Smith 2012-10-25 09:19:37 +01:00
parent 5ae2dfc478
commit 5776c44d60
4 changed files with 62 additions and 21 deletions

View File

@ -96,24 +96,27 @@ class Morris.Grid extends Morris.EventEmitter
if @options.ymax[0..3] is 'auto' if @options.ymax[0..3] is 'auto'
# use Array.concat to flatten arrays and find the max y value # use Array.concat to flatten arrays and find the max y value
if @options.ymax.length > 5 if @options.ymax.length > 5
@ymax = Math.max parseInt(@options.ymax[5..], 10), ymax @ymax = parseInt(@options.ymax[5..], 10)
@ymax = Math.max(ymax, @ymax) unless ymax is null
else else
@ymax = ymax @ymax = if ymax isnt null then ymax else 0
else else
@ymax = parseInt(@options.ymax, 10) @ymax = parseInt(@options.ymax, 10)
else else
@ymax = @options.ymax @ymax = @options.ymax
if typeof @options.ymin is 'string' and @options.ymin[0..3] is 'auto' if typeof @options.ymin is 'string'
if @options.ymin.length > 5 if @options.ymin[0..3] is 'auto'
@ymin = Math.min parseInt(@options.ymin[5..], 10), ymin if @options.ymin.length > 5
@ymin = parseInt(@options.ymin[5..], 10)
@ymin = Math.min(ymin, @ymin) unless ymin is null
else
@ymin = if ymin isnt null then ymin else 0
else else
@ymin = ymin @ymin = parseInt(@options.ymin, 10)
else if typeof @options.ymin is 'string'
@ymin = parseInt(@options.ymin, 10)
else else
@ymin = @options.ymin @ymin = @options.ymin
if @ymin is @ymax if @ymin is @ymax
if @ymin isnt 0 then @ymin -= 1 @ymin -= 1 if ymin
@ymax += 1 @ymax += 1
@yInterval = (@ymax - @ymin) / (@options.numLines - 1) @yInterval = (@ymax - @ymin) / (@options.numLines - 1)

View File

@ -391,9 +391,12 @@
if (typeof this.options.ymax === 'string') { if (typeof this.options.ymax === 'string') {
if (this.options.ymax.slice(0, 4) === 'auto') { if (this.options.ymax.slice(0, 4) === 'auto') {
if (this.options.ymax.length > 5) { if (this.options.ymax.length > 5) {
this.ymax = Math.max(parseInt(this.options.ymax.slice(5), 10), ymax); this.ymax = parseInt(this.options.ymax.slice(5), 10);
if (ymax !== null) {
this.ymax = Math.max(ymax, this.ymax);
}
} else { } else {
this.ymax = ymax; this.ymax = ymax !== null ? ymax : 0;
} }
} else { } else {
this.ymax = parseInt(this.options.ymax, 10); this.ymax = parseInt(this.options.ymax, 10);
@ -401,19 +404,24 @@
} else { } else {
this.ymax = this.options.ymax; this.ymax = this.options.ymax;
} }
if (typeof this.options.ymin === 'string' && this.options.ymin.slice(0, 4) === 'auto') { if (typeof this.options.ymin === 'string') {
if (this.options.ymin.length > 5) { if (this.options.ymin.slice(0, 4) === 'auto') {
this.ymin = Math.min(parseInt(this.options.ymin.slice(5), 10), ymin); if (this.options.ymin.length > 5) {
this.ymin = parseInt(this.options.ymin.slice(5), 10);
if (ymin !== null) {
this.ymin = Math.min(ymin, this.ymin);
}
} else {
this.ymin = ymin !== null ? ymin : 0;
}
} else { } else {
this.ymin = ymin; this.ymin = parseInt(this.options.ymin, 10);
} }
} else if (typeof this.options.ymin === 'string') {
this.ymin = parseInt(this.options.ymin, 10);
} else { } else {
this.ymin = this.options.ymin; this.ymin = this.options.ymin;
} }
if (this.ymin === this.ymax) { if (this.ymin === this.ymax) {
if (this.ymin !== 0) { if (ymin) {
this.ymin -= 1; this.ymin -= 1;
} }
this.ymax += 1; this.ymax += 1;

2
morris.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -38,6 +38,16 @@ describe 'Morris.Line data', ->
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'
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
line.ymax.should.equal 1
describe 'auto [n]', -> describe 'auto [n]', ->
@ -48,10 +58,20 @@ describe 'Morris.Line data', ->
xkey: 'x' xkey: 'x'
ykeys: ['y', 'z'] ykeys: ['y', 'z']
labels: ['y', 'z'] labels: ['y', 'z']
ymin: 'auto 12' ymin: 'auto 11'
ymax: 'auto 12' 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'
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', -> it 'should use a user-specified minimum and maximum value', ->
line = Morris.Line line = Morris.Line
@ -64,4 +84,14 @@ describe 'Morris.Line data', ->
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'
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
line.ymax.should.equal 20