oxjs/source/Ox.UI/js/List/Chart.js

280 lines
9.7 KiB
JavaScript
Raw Normal View History

2012-03-22 20:04:35 +00:00
'use strict';
2012-03-26 19:26:28 +00:00
/*@
2012-05-31 10:32:54 +00:00
Ox.Chart <f> Bar Chart
2012-03-26 19:26:28 +00:00
options <o> Options
color <[n]|[[n]]|[128, 128, 128]> Bar color
data <o> {k: v, ...} or {k: {k: v, ...}, ...}
formatKey <f|null> Format function for keys
keyAlign <s|'right'> Alignment of keys
keyWidth <n|128> Width of keys
2012-10-05 11:19:22 +00:00
limit <n|0> Number of items, or 0 for all
2012-03-26 19:26:28 +00:00
rows <n|1> undocumented
sort <o|{key: 'value', operator: '-'}> Sort
title <s|''> Chart title
width <n|512> Chart width
self <o> shared private variable
([options[, self]]) -> <o:Ox.Element> Chart object
2012-03-26 19:26:28 +00:00
@*/
2012-03-22 20:04:35 +00:00
Ox.Chart = function(options, self) {
self = self || {};
var that = Ox.Element({}, self)
.defaults({
color: [128, 128, 128],
data: {},
formatKey: null,
keyAlign: 'right',
keyWidth: 128,
2012-10-05 11:19:22 +00:00
limit: 0,
2012-03-22 20:04:35 +00:00
rows: 1,
sort: {key: 'value', operator: '-'},
2012-03-26 19:26:28 +00:00
sortKey: null,
2012-03-22 20:04:35 +00:00
title: '',
width: 512
})
.options(options || {})
2012-05-28 19:35:41 +00:00
.update({
width: function() {
self.$chart.empty();
renderChart();
}
})
2012-10-05 11:19:22 +00:00
.addClass('OxChart');
2012-03-22 20:04:35 +00:00
2012-03-26 19:26:28 +00:00
self.valueWidth = self.options.width - self.options.keyWidth;
self.keys = Object.keys(self.options.data);
if (Ox.isObject(self.options.data[self.keys[0]])) {
if (Ox.isUndefined(options.color)) {
self.options.color = [
[192, 64, 64], [ 64, 192, 64], [ 64, 64, 192],
[192, 192, 64], [ 64, 192, 192], [192, 64, 192],
[192, 128, 64], [ 64, 192, 128], [128, 64, 192],
[192, 64, 128], [128, 192, 64], [ 64, 128, 192]
];
}
self.subData = {};
}
2012-03-22 20:04:35 +00:00
self.sort = {};
2012-03-26 19:26:28 +00:00
self.totals = {};
2012-03-22 20:04:35 +00:00
Ox.forEach(self.options.data, function(value, key) {
2012-03-26 19:26:28 +00:00
self.totals[key] = self.subData ? Ox.sum(value) : value;
if (self.subData) {
Object.keys(value).forEach(function(subKey) {
self.subData[subKey] = (self.subData[subKey] || 0) + value[subKey];
});
}
2012-03-22 20:04:35 +00:00
self.sort[key] = key.replace(/(\d+)/g, function(number) {
return Ox.pad(parseInt(number, 10), 16);
2012-03-22 20:04:35 +00:00
});
});
2012-03-26 19:26:28 +00:00
self.max = Ox.max(self.totals);
self.sum = Ox.sum(self.totals);
if (self.subData) {
Ox.forEach(self.subData, function(subValue, subKey) {
self.sort[subKey] = subKey.replace(/(\d+)/g, function(number) {
return Ox.pad(parseInt(number, 10), 16);
2012-03-26 19:26:28 +00:00
});
});
self.subKeys = Object.keys(self.subData).sort(function(a, b) {
var aValue = self.subData[a],
bValue = self.subData[b];
2012-04-24 10:18:39 +00:00
return a === '' ? 1
: b === '' ? -1
2012-03-26 19:26:28 +00:00
: self.sort[a] < self.sort[b] ? -1
: self.sort[a] > self.sort[b] ? 1
: 0;
});
}
2012-03-22 20:04:35 +00:00
self.items = self.keys.map(function(key) {
var value = self.options.data[key],
sort = self.options.sort.operator == '+'
? (self.subData ? self.totals[key] : value)
2013-04-26 10:28:26 +00:00
: (self.subData ? self.max - self.totals[key] : self.max - value);
2012-10-27 18:51:14 +00:00
if (self.options.sort.key == 'value') {
sort = Ox.pad(value, 64) + sort;
2012-10-27 18:51:14 +00:00
}
return {key: key, value: value, sort: sort};
});
2012-10-05 11:19:22 +00:00
if (self.options.limit) {
2012-10-27 18:51:14 +00:00
self.items = Ox.sortBy(
self.items, {key: 'sort', operator: '+'}
2012-10-27 18:51:14 +00:00
).slice(0, self.options.limit);
self.max = Ox.max(self.items.map(function(item) {
return self.subData ? Ox.sum(item.value) : item.value;
}));
2012-10-05 11:19:22 +00:00
}
2012-10-27 18:51:14 +00:00
self.max = self.max || 1;
2012-03-26 19:26:28 +00:00
if (self.options.rows == 2) {
self.row = 0;
}
2012-03-22 20:04:35 +00:00
self.$title = Ox.Bar({size: 16})
.append(
$('<div>')
.css({margin: '1px 0 0 4px'})
.html(self.options.title)
)
.appendTo(that);
2012-03-26 19:26:28 +00:00
2012-03-22 20:04:35 +00:00
self.$chart = $('<div>')
.css({position: 'absolute', top: '16px'})
.append(renderChart())
.appendTo(that);
function getColumns() {
return [
{
align: self.options.keyAlign,
format: self.options.formatKey,
id: 'key',
width: self.options.keyWidth,
2012-05-26 15:48:19 +00:00
visible: true
2012-03-22 20:04:35 +00:00
},
{
format: renderValue,
id: 'value',
width: self.valueWidth,
visible: true
}
];
}
2012-03-26 19:26:28 +00:00
function getWidths(values) {
var max, maxKeys,
total = Ox.sum(values),
totalWidth = Math.ceil(total / self.max * self.valueWidth),
2012-03-26 19:26:28 +00:00
widths = {};
Ox.forEach(values, function(value, key) {
widths[key] = Math.round(value / total * totalWidth);
});
while (Ox.sum(widths) != totalWidth) {
max = Ox.max(widths);
2012-05-22 14:29:37 +00:00
maxKeys = Object.keys(widths).filter(function(key) {
return widths[key] == max;
2012-03-26 19:26:28 +00:00
});
widths[maxKeys[0]] += Ox.sum(widths) < totalWidth ? 1 : -1;
}
return widths;
}
2012-03-22 20:04:35 +00:00
function renderChart() {
2012-10-05 11:19:22 +00:00
that.css({
width: self.options.width + 'px',
height: 16 + self.items.length * 16 + 'px',
overflowY: 'hidden'
});
2012-06-27 07:41:10 +00:00
return Ox.TableList({
2012-03-22 20:04:35 +00:00
columns: getColumns(),
items: self.items,
max: 0,
min: 0,
pageLength: self.items.length,
sort: [{key: 'sort', operator: '+'}],
width: self.options.width,
unique: 'key'
2012-03-22 20:04:35 +00:00
})
.css({
left: 0,
top: 0,
width: self.options.width + 'px',
height: self.items.length * 16 + 'px'
});
}
function renderValue(value, data) {
2012-03-26 19:26:28 +00:00
var $bars = [],
$element,
colors = [], len, widths;
if (!self.subData) {
$element = $bars[0] = Ox.Element({
element: '<div>',
tooltip: Ox.formatNumber(value)
+ ' (' + Ox.formatPercent(value * self.options.rows, self.sum, 2) + ')'
})
.css({
width: Math.ceil(value / self.max * self.valueWidth) + 'px',
2012-03-26 19:26:28 +00:00
height: '14px',
borderRadius: '4px',
marginLeft: '-4px'
});
colors[0] = Ox.isFunction(self.options.color)
? self.options.color(data.key) : self.options.color;
} else {
$element = $('<div>')
.css({
2012-10-27 17:46:27 +00:00
width: Math.ceil(self.totals[data.key] / self.max * self.valueWidth) + 'px',
2012-03-26 19:26:28 +00:00
height: '14px',
marginLeft: '-4px'
});
len = Ox.len(value);
widths = getWidths(value);
self.subKeys.forEach(function(subKey, subKeyIndex) {
var i = $bars.length,
subValue = value[subKey];
if (subValue) {
$bars[i] = Ox.Element({
element: '<div>',
/*
tooltip: Ox.formatNumber(self.totals[data.key])
+ ' (' + Ox.formatPercent(self.totals[data.key] * self.options.rows, self.sum, 2) + ')'
+ '<br>' + subKey + ': ' + Ox.formatNumber(subValue)
+ ' (' + Ox.formatPercent(subValue, self.totals[data.key], 2) + ')'
*/
tooltip: Ox.formatNumber(self.totals[data.key])
+ ' (' + Ox.formatPercent(self.totals[data.key] * self.options.rows, self.sum, 2) + ')'
})
.css({
float: 'left',
width: widths[subKey] + 'px',
height: '14px',
borderTopLeftRadius: i == 0 ? '4px' : 0,
borderBottomLeftRadius: i == 0 ? '4px' : 0,
borderTopRightRadius: i == len - 1 ? '4px' : 0,
borderBottomRightRadius: i == len - 1 ? '4px' : 0
})
.appendTo($element);
colors[i] = subKey == '' ? [128, 128, 128]
: Ox.isArray(self.options.color)
? self.options.color[subKeyIndex % self.options.color.length]
: Ox.isObject(self.options.color)
? self.options.color[subKey]
: self.options.color(subKey);
}
2012-03-22 20:04:35 +00:00
});
2012-03-26 19:26:28 +00:00
}
$bars.forEach(function($bar, i) {
/*
if (self.options.rows == 2) {
colors[i] = colors[i].map(function(v) {
return v + (self.row % 2 == 0 ? 16 : -16);
});
}
*/
['moz', 'o', 'webkit'].forEach(function(browser) {
$bar.css({
backgroundImage: '-' + browser
+ '-linear-gradient(top, rgb(' + colors[i].map(function(v) {
return Ox.limit(v + 16, 0, 255);
}).join(', ') + '), rgb(' + colors[i].map(function(v) {
return Ox.limit(v - 16, 0, 255);
}) + '))'
});
2012-03-22 20:04:35 +00:00
});
});
2012-03-26 19:26:28 +00:00
if (self.options.rows == 2) {
self.row++;
}
2012-03-22 20:04:35 +00:00
return $element;
}
return that;
};