-
Notifications
You must be signed in to change notification settings - Fork 2
/
hBarChart.js
117 lines (116 loc) · 4.11 KB
/
hBarChart.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
/*
* hBarChart
* version: 1.0
* Copyright (c) 2017 Md.Harun-Ur-Rashid
* Git: https://github.com/haruncpi/hBarChart.github.io
* licensed under the MIT
* Website: www.learn24bd.com
* Country: Bangladesh
*/
(function($) {
"use strict";
$.fn.hBarChart = function(customConfig) {
var config = $.extend({
bgColor: 'green',
textColor: '#fff',
show: 'label',
sorting: true,
maxStyle: {
bg: 'orange',
text: 'white'
}
}, customConfig);
var chartObj = $(this);
var data = [];
var max = null;
var bgColor = config.bgColor;
var lightenDarkenColor = function(col, amt) {
var usePound = false;
if (col[0] == "#") {
col = col.slice(1);
usePound = true;
}
var num = parseInt(col, 16);
var r = (num >> 16) + amt;
if (r > 255) r = 255;
else if (r < 0) r = 0;
var b = ((num >> 8) & 0x00FF) + amt;
if (b > 255) b = 255;
else if (b < 0) b = 0;
var g = (num & 0x0000FF) + amt;
if (g > 255) g = 255;
else if (g < 0) g = 0;
return (usePound ? "#" : "") + (g | (b << 8) | (r << 16)).toString(16);
}
//end sorting operation
if (config.sorting) {
var sort_li = function(a, b) {
return ($(b).data('data')) > ($(a).data('data')) ? 1 : -1;
}
chartObj.find("li").sort(sort_li) // sort elements
.appendTo(chartObj); // append again to the list
}
//end sorting operation
//global style
chartObj.find("li").css({
listStyleType: 'none',
padding: '5px',
boxSizing: 'border-box',
marginTop: '3px',
width: '100%',
background: config.bgColor,
color: config.textColor,
whiteSpace: 'nowrap',
borderRadius: '4px',
fontSize: '13px',
fontFamily: 'Tahoma, Geneva, sans-serif'
})
//global style
//find max
chartObj.find("li").each(function() {
var val = $(this).data('data');
data.push(val);
});
max = Math.max.apply(Math, data);
// find max
chartObj.find("li").each(function() {
var lbl = $(this).text();
var val = $(this).data('data');
var percentage = (100 / max) * val;
bgColor = lightenDarkenColor(bgColor, 10);
// bar animation
$(this).css({
width: 0,
background: bgColor
});
$(this).animate({
width: percentage + '%'
}, 1000)
// bar animation
//show label
switch (config.show) {
case 'label':
$(this).text(lbl);
break;
case 'data':
$(this).text(val);
break;
case 'both':
$(this).text(lbl).append(' (' + val + ')');
break;
default:
break;
}
// end show label
// max style
if (val == max) {
$(this).css({
background: config.maxStyle.bg,
color: config.maxStyle.text
})
}
// max style
});
}
//end plugin function
}(jQuery));