Permalink
Browse files

Fixes #39: using method to sort datas

  • Loading branch information...
1 parent 64d184b commit 2474f29375272877bce456dacdb3c486e3f7f74f @alecharp alecharp committed Apr 14, 2013
Showing with 72 additions and 11 deletions.
  1. +22 −9 lib/chart.js
  2. +50 −2 test/chart.test.js
View
@@ -44,7 +44,12 @@ var emptyData = [[]],
timing: 750,
// Line interpolation
- interpolation: 'monotone'
+ interpolation: 'monotone',
+
+ // Data sorting
+ sortingMethod: function (a, b) {
+ return (!a.x && !b.x) ? 0 : (a.x < b.x) ? -1 : 1;
+ }
};
// What/how should the warning/error be presented?
@@ -248,13 +253,7 @@ _.defaults(xChart.prototype, {
np.y = o.dataFormatY(p.y);
}
return np;
- })
- .sort(function (a, b) {
- if (!a.x && !b.x) {
- return 0;
- }
- return (a.x < b.x) ? -1 : 1;
- });
+ }).sort(o.sortingMethod);
return _.extend(_.clone(set), { data: d });
}
@@ -647,5 +646,19 @@ _.defaults(xChart.prototype, {
throw 'Required method "' + method + '" not found on vis type "' +
self._type + '".';
}
- }
+ }/*,
+
+ _updateSorting:function(method) {
+ self = this;
+ if(typeof(method) == 'function') { self.sortingMethod = method }
+ else if (method == 'auto') {
+ self.sortingMethod = function (a, b) {
+ if (!a.x && !b.x) {
+ return 0;
+ }
+ return (a.x < b.x) ? -1 : 1;
+ }
+ }
+ self.setData(_mainData);
+ }*/
});
View
@@ -65,7 +65,8 @@
xMin: null,
xMax: null,
yMin: null,
- yMax: null
+ yMax: null,
+ sortingMethod: c._options.sortingMethod
});
});
@@ -223,7 +224,7 @@
],
type: 'line',
className: 'foo_bar'
- }],
+ }]
},
c = new xChart('bar', data, container);
@@ -532,4 +533,51 @@
expect(count).to.be(1);
});
});
+
+ describe('sort data', function () {
+ var data = {
+ main: [{
+ label: 'Foobar',
+ data: [
+ { x: 2, y: 3 },
+ { x: 3, y: 2 },
+ { x: 1, y: 4 },
+ { x: 0, y: 1 },
+ ],
+ className: 'foo_bar'
+ }],
+ xScale: 'ordinal',
+ yScale: 'linear'
+ };
+
+ it('automatic sorting', function () {
+ var chart = new xChart('bar', data, container);
+ expect(chart._mainData[0].data[0]).to.be.eql({x : 0, y : 1});
+ expect(chart._mainData[0].data[1]).to.be.eql({x : 1, y : 4});
+ expect(chart._mainData[0].data[2]).to.be.eql({x : 2, y : 3});
+ expect(chart._mainData[0].data[3]).to.be.eql({x : 3, y : 2});
+ });
+
+ it('no sorting', function () {
+ var chart = new xChart('bar', data, container, {
+ sortingMethod: function (a, b) {}
+ });
+ expect(chart._mainData[0].data[0]).to.be.eql({x : 2, y : 3});
+ expect(chart._mainData[0].data[1]).to.be.eql({x : 3, y : 2});
+ expect(chart._mainData[0].data[2]).to.be.eql({x : 1, y : 4});
+ expect(chart._mainData[0].data[3]).to.be.eql({x : 0, y : 1});
+ });
+
+ it('custom sorting method (reverse)', function () {
+ var chart = new xChart('bar', data, container, {
+ sortingMethod: function (a, b) {
+ return !a.x && b.x ? 0 : a.x < b.x ? 1 : -1;
+ }
+ });
+ expect(chart._mainData[0].data[0]).to.be.eql({x : 3, y : 2});
+ expect(chart._mainData[0].data[1]).to.be.eql({x : 2, y : 3});
+ expect(chart._mainData[0].data[2]).to.be.eql({x : 1, y : 4});
+ expect(chart._mainData[0].data[3]).to.be.eql({x : 0, y : 1});
+ });
+ });
}());

0 comments on commit 2474f29

Please sign in to comment.