Ox.Chart: improve sorting

This commit is contained in:
rlx 2012-10-27 18:51:14 +00:00
parent 41ca92a3d1
commit dfde724a38

View file

@ -58,8 +58,6 @@ Ox.Chart = function(options, self) {
self.subData = {};
}
// fixme: a lot of the sorting is unneeded, since it happens in the list
self.sort = {};
self.totals = {};
Ox.forEach(self.options.data, function(value, key) {
@ -87,8 +85,6 @@ Ox.Chart = function(options, self) {
bValue = self.subData[b];
return a === '' ? 1
: b === '' ? -1
//: aValue < bValue ? 1
//: aValue > bValue ? -1
: self.sort[a] < self.sort[b] ? -1
: self.sort[a] > self.sort[b] ? 1
: 0;
@ -96,23 +92,22 @@ Ox.Chart = function(options, self) {
}
self.items = self.keys.map(function(key) {
return {key: key, value: self.options.data[key]};
})
.sort(function(a, b) {
var key = self.options.sort.key,
aValue = key == 'key' ? self.sort[a.key]
: self.subData ? self.totals[a.key] : a.value,
bValue = key == 'key' ? self.sort[b.key]
: self.subData ? self.totals[b.key] : b.value;
return aValue < bValue ? (self.options.sort.operator == '+' ? -1 : 1)
: aValue > bValue ? (self.options.sort.operator == '+' ? 1 : -1)
: key == 'value' && self.sort[a.key] < self.sort[b.key] ? -1
: key == 'value' && self.sort[a.key] > self.sort[b.key] ? 1
: 0;
var sort = self.sort[key],
value = self.options.data[key];
if (self.options.sort.key == 'value') {
sort = Ox.pad(self.subData ? self.totals[key] : value, 64) + sort;
}
return {key: key, value: value, sort: sort};
});
if (self.options.limit) {
self.items = self.items.slice(0, self.options.limit);
self.items = Ox.sortBy(
self.items, {key: 'sort', operator: self.options.sort.operator}
).slice(0, self.options.limit);
self.max = Ox.max(self.items.map(function(item) {
return self.subData ? Ox.sum(item.value) : item.value;
}));
}
self.max = self.max || 1;
if (self.options.rows == 2) {
self.row = 0;
@ -179,7 +174,7 @@ Ox.Chart = function(options, self) {
max: 0,
min: 0,
pageLength: self.items.length,
sort: [self.options.sort],
sort: [{key: 'sort', operator: self.options.sort.operator}],
width: self.options.width,
unique: 'key'
})