-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTableBarChart.js
196 lines (180 loc) · 5.97 KB
/
TableBarChart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Table Bar Chart jQuery plugin 1.0
*
* Copyright (c) 2014, AIWSolutions
* License: GPL2
* Project Website: http://wiki.aiwsolutions.net/Snld9
**/
jQuery.fn.tableBarChart = function(targetDiv, caption, reverseGroup) {
var source = $(this);
var target = $(targetDiv);
var maxValue = 0;
var minValue = Number.MAX_VALUE;
var yAxisMax = 0;
var yAxisMin = 0;
var groupTitles = [];
var legends = [];
var tableData = [];
function getHeaderNames(isColumn) {
var parentNode = isColumn ? 'thead' : 'tbody';
var names = [];
source.find(parentNode + ' th').each(function(index, element) {
// skip first cell
if (index > 0 || isColumn === undefined || !isColumn) {
names.push($(element).text());
}
});
return names;
}
function getDataByRow() {
var dataMatrix = [];
source.find('tbody tr').each(function(i, trElement) {
var rowValue = [];
$(trElement).find('td').each(function(j, tdElement) {
var intValue = parseInt($(tdElement).text());
if (intValue > maxValue) {
maxValue = intValue;
} else if (intValue < minValue) {
minValue = intValue;
}
rowValue.push(intValue);
});
dataMatrix.push(rowValue);
});
return dataMatrix;
}
function getDataByColumn() {
var dataMatrix = [];
var numCols = source.find('tbody tr:eq(0) td').size();
for (var i = 0; i < numCols; i++) {
var colValue = [];
source.find('tbody tr').each(function() {
var intValue = parseInt($($(this).find('td:eq(' + i + ')')[0]).text());
if (intValue > maxValue) {
maxValue = intValue;
} else if (intValue < minValue) {
minValue = intValue;
}
colValue.push(intValue);
});
dataMatrix.push(colValue);
}
return dataMatrix;
}
function getCaption() {
if (caption === undefined || caption.length === 0) {
caption = '';
source.find('caption').each(function() {
caption = $(this).text();
});
}
return caption;
}
function getAxisHTML(sourceArray, cssClass) {
var axis = $('<ul class="' + cssClass + '"></ul>');
$(sourceArray).each(function() {
axis.append('<li><span>' + this + '</span></li>');
});
return axis;
}
function getYAxisArray(stepCount) {
var maxDigitCount = String(maxValue).length;
var minDigitCount = String(minValue).length;
var base10 = Math.pow(10, maxDigitCount - 1);
yAxisMax = base10 * (Math.floor(maxValue / base10) + 1);
if (maxDigitCount === minDigitCount) {
yAxisMin = base10 * (Math.floor(minValue / base10));
}
var result = [];
var step = (yAxisMax - yAxisMin) / stepCount;
for (var i = stepCount; i >= 0; i--) {
var stepValue = yAxisMin + step * i;
if (stepValue % 1 !== 0) {
stepValue = parseFloat(Math.round(stepValue * 100) / 100).toFixed(2);
}
result.push(stepValue);
}
return result;
}
function animateBar(index) {
target.find('.bar.item-' + index).each(function() {
var bar = $(this);
bar.css('height', 0);
var value = bar.attr('value');
bar.animate({
'height': value
}, 800);
});
}
function getLegendHTML() {
var legendContainer = $('<ul class="legend"></ul>');
$(legends).each(function(index) {
var legendItem = $('<li><span class="icon item-' + index + '"></span>' +
this + '</li>');
legendItem.mouseenter(function() {
animateBar(index);
});
legendContainer.append(legendItem);
});
return legendContainer;
}
function getBarChartHTML() {
var barsContainer = $('<div class="bars"></div>');
$(tableData).each(function(i, columnGroup) {
var barGroup = $('<div class="bar-group"></div>');
$(columnGroup).each(function(j, cell) {
var bar = $('<div class="bar item-' + j + '" value="' +
Math.floor((cell - yAxisMin) / (yAxisMax - yAxisMin) * 100) + '%"><span>' +
cell + '</span></div>');
// CSS :hover won't work on IE
bar.hover(function() {
bar.find('span').css('display', 'block');
}, function() {
bar.find('span').css('display', 'none');
});
barGroup.append(bar);
});
barsContainer.append(barGroup);
});
return barsContainer;
}
function layout() {
var defaultMargin = 10;
var yAxisWidth = 50;
target.find('.y-axis').css('width', '100%');
target.find('.y-axis span').css('width', yAxisWidth).css('margin', '-' + defaultMargin + 'px 0 0 -' + (yAxisWidth + defaultMargin) + 'px');
var graphWidth = target.width() - (yAxisWidth + 2 * defaultMargin);
var graphHeight = target.height() - target.find('.caption').height() - target.find('.legend').height() - 3 * defaultMargin;
target.find('.graph').css('width', graphWidth).css('height', graphHeight);
var stepHeight = Math.floor((graphHeight - target.find('.x-axis').height() - 2 * defaultMargin) / (groupTitles.length + 1));
target.find('.y-axis li').css('height', stepHeight).css('width', '100%');
var barGroupHeight = (target.find('.y-axis li').height() + 1) * (groupTitles.length + 1);
target.find('.bars').css('height', barGroupHeight).css('width', '100%');
var barGroupWidth = graphWidth / groupTitles.length - 2 * defaultMargin;
target.find('.bar-group').css('width', barGroupWidth).css('margin', '0 ' + defaultMargin);
target.find('.x-axis li').css('width', barGroupWidth);
var barWidth = barGroupWidth / legends.length - 2;
target.find('.bar').css('width', barWidth);
for (var i = 0; i < legends.length; i++) {
target.find('.bar.item-' + i).css('left', i * (barWidth + 2));
animateBar(i);
}
}
function render() {
target.append('<div class="caption">' + getCaption() + '</div>');
var graphContainer = $('<div class="graph"></div>');
graphContainer.append(getAxisHTML(groupTitles, 'x-axis'));
graphContainer.append(getAxisHTML(getYAxisArray(groupTitles.length + 1), 'y-axis'));
graphContainer.append(getBarChartHTML());
target.append(graphContainer);
target.append(getLegendHTML());
layout();
}
function initialize() {
groupTitles = getHeaderNames(reverseGroup ? false : true);
legends = getHeaderNames(reverseGroup ? true : false);
tableData = reverseGroup ? getDataByRow() : getDataByColumn();
render();
}
initialize();
}