diff --git a/src/js/Rickshaw.Graph.Renderer.js b/src/js/Rickshaw.Graph.Renderer.js index fc34edaf..abc59f45 100644 --- a/src/js/Rickshaw.Graph.Renderer.js +++ b/src/js/Rickshaw.Graph.Renderer.js @@ -28,19 +28,14 @@ Rickshaw.Graph.Renderer = Rickshaw.Class.create( { }, domain: function(data) { - + // Requires that at least one series contains some data var stackedData = data || this.graph.stackedData || this.graph.stackData(); - var firstPoint = stackedData[0][0]; - - if (firstPoint === undefined) { - return { x: [null, null], y: [null, null] }; - } - var xMin = firstPoint.x; - var xMax = firstPoint.x; + var xMin = +Infinity; + var xMax = -Infinity; - var yMin = firstPoint.y + firstPoint.y0; - var yMax = firstPoint.y + firstPoint.y0; + var yMin = +Infinity; + var yMax = -Infinity; stackedData.forEach( function(series) { diff --git a/src/js/Rickshaw.Graph.js b/src/js/Rickshaw.Graph.js index 11e3a9ef..8693290a 100644 --- a/src/js/Rickshaw.Graph.js +++ b/src/js/Rickshaw.Graph.js @@ -72,13 +72,15 @@ Rickshaw.Graph = function(args) { if (!Array.isArray(s.data)) { throw "series data is not an array: " + JSON.stringify(s.data); } - - var x = s.data[0].x; - var y = s.data[0].y; - - if (typeof x != 'number' || ( typeof y != 'number' && y !== null ) ) { - throw "x and y properties of points should be numbers instead of " + - (typeof x) + " and " + (typeof y); + + if (s.data.length > 0) { + var x = s.data[0].x; + var y = s.data[0].y; + + if (typeof x != 'number' || ( typeof y != 'number' && y !== null ) ) { + throw "x and y properties of points should be numbers instead of " + + (typeof x) + " and " + (typeof y); + } } if (s.data.length >= 3) { diff --git a/tests/Rickshaw.Graph.Renderer.js b/tests/Rickshaw.Graph.Renderer.js index df09562f..a64fee88 100644 --- a/tests/Rickshaw.Graph.Renderer.js +++ b/tests/Rickshaw.Graph.Renderer.js @@ -1,5 +1,23 @@ var Rickshaw = require("../rickshaw"); +exports.setUp = function(callback) { + + Rickshaw = require('../rickshaw'); + + global.document = d3.select('html')[0][0].parentNode; + global.window = document.defaultView; + + new Rickshaw.Compat.ClassList(); + + callback(); +}; + +exports.tearDown = function(callback) { + + delete require.cache.d3; + callback(); +}; + exports.domain = function(test) { // document comes from jsdom @@ -87,7 +105,6 @@ exports.domain = function(test) { test.done(); }; - exports.respectStrokeFactory = function(test) { var el = document.createElement("div"); @@ -151,3 +168,36 @@ exports.respectStrokeFactory = function(test) { test.done(); }; + + +exports['should allow arbitrary empty series when finding the domain of stacked data'] = function(test) { + + var el = document.createElement("div"); + + // should not throw + var graph = new Rickshaw.Graph({ + element: el, + stroke: true, + width: 10, + height: 10, + renderer: 'line', + series: [ + { + data: [] + }, + { + data: [ + { x: 0, y: 40 }, + { x: 1, y: 49 }, + { x: 2, y: 38 }, + { x: 3, y: 30 }, + { x: 4, y: 32 } + ] + } + ] + }); + test.deepEqual(graph.renderer.domain(), { x: [0, 4], y: [0, 49.49]}); + + test.done(); +}; + diff --git a/tests/Rickshaw.Graph.js b/tests/Rickshaw.Graph.js index dd9547f1..97ef4120 100644 --- a/tests/Rickshaw.Graph.js +++ b/tests/Rickshaw.Graph.js @@ -56,7 +56,7 @@ exports.validate = function(test) { var el = document.createElement("div"); - test.throws( function() { + test.throws( function() { var graph = new Rickshaw.Graph({ element: el, @@ -79,6 +79,36 @@ exports.validate = function(test) { }; +exports['should validate empty data when rendering multiple series'] = function(test) { + var el = document.createElement("div"); + + try { + var graph = new Rickshaw.Graph({ + element: el, + width: 960, + height: 500, + renderer: 'line', + series: [ + {data: [], name:'first: empty'},{ + data : [ + { x: 0, y: 40 }, + { x: 1, y: 49 }, + { x: 2, y: 38 }, + { x: 3, y: 30 }, + { x: 4, y: 32 } ], + name: '5 datas' + }, + {data: [], name:'last: empty'}] + }); + } catch (error) { + test.fail(error); + } + //test.deepEquals(graph.domain(), [NaN, NaN], should have proper ); + + + test.done(); +}; + exports.scales = function(test) { var el = document.createElement("div");