-
Notifications
You must be signed in to change notification settings - Fork 0
/
kuma-gauge.jquery.js
342 lines (295 loc) · 13 KB
/
kuma-gauge.jquery.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Kuma Gauge - v0.2
*
* Made by Sam Bellen for Kunstmaan
* - http:// www.kunstmaan.be
* - http:// www.sambellen.be
*
* Under MIT License
*/
;(function ( $, window, document, undefined ) {
var config = {
radius : 170,
paddingX : 10,
paddingY : 20,
gaugeWidth : 30,
fill : '0-#00bfff:0-#25cd6b:50-#e23131:80',
gaugeBackground : '#f4f4f4',
background : '#fff',
showNeedle : false,
animationSpeed : 600,
width : 0,
height : 0,
centerX : 0,
centerY : 0,
min : 0,
max : 40,
value : 80,
valueLabel : {
display : true,
fontFamily : 'Arial',
fontColor : '#000',
fontSize : '48',
fontWeight : 'bold'
},
title : {
display : true,
value : '',
fontFamily : 'Arial',
fontColor : '#000',
fontSize : '20',
fontWeight : 'normal'
},
label : {
display : true,
left : '-5',
right : '10',
fontFamily : 'Arial',
fontColor : '#000',
fontSize : '14',
fontWeight : 'bold'
}
};
// Create an arc with raphael.js
Raphael.fn.arc = function(startX, startY, endX, endY, radius1, radius2, angle) {
var arcSVG = [radius1, radius2, angle, 0, 1, endX, endY].join(' ');
return this.path('M' + startX + ' ' + startY + ' a ' + arcSVG);
};
// Calculate a circular arc with raphael.js
Raphael.fn.circularArc = function(centerX, centerY, radius, startAngle, endAngle) {
var startX = centerX + radius * Math.cos(startAngle * Math.PI / 180);
var startY = centerY + radius * Math.sin(startAngle * Math.PI / 180);
var endX = centerX + radius * Math.cos(endAngle * Math.PI / 180);
var endY = centerY + radius * Math.sin(endAngle * Math.PI / 180);
return this.arc(startX, startY, endX-startX, endY-startY, radius, radius, 0);
};
// The kuma Gauge constructor
function kumaGauge(element, options , method) {
// This
var _this = this;
// The element
_this.element = element;
_this.$element = $(element);
// The config
_this.config = $.extend( {}, config, options );
_this._config = config;
_this.method = method;
// The actual gauge
_this.gauge = {};
// Initialise
_this.init();
}
// Extend the kumaGauge object
kumaGauge.prototype = {
init: function () {
// this
_this = this;
if (!_this.method) {
_this.draw();
}
},
_setup : function() {
// This
_this = this;
// Calculate some values needed do draw the gauge
_this.config.width = (_this.config.radius * 2) + (_this.config.paddingX * 2);
_this.config.height = _this.config.radius + (_this.config.paddingY * 2);
_this.config.centerX = _this.config.paddingX + _this.config.radius;
_this.config.centerY = _this.config.paddingY + _this.config.radius;
// The div wich acts as the canvas needs an id, so we give it a unique one if it doesn't have one
if (typeof $(this).attr('id') === 'undefined' || $(this).attr('id') === '') {
_this.config.id = 'gauge-' + $('*[id^="gauge-"]').length;
_this.$element.attr('id', _this.config.id);
}
},
_calculateRotation : function(min, max, val) {
var _range, _rotation;
_range = max - min;
if (val < max && val > min) {
_rotation = 180 * ((val - min) / _range);
} else if (val <= min){
_rotation = 0;
} else {
_rotation = 180;
}
return _rotation;
},
draw : function() {
//this
var _this = this;
// Setup all the needed config variables
_this._setup();
// Make the base drawing Canvas
_this.gauge = new Raphael(_this.config.id, _this.config.width, _this.config.height);
// Draw the gauge
_this.gauge.gauge = _this.gauge.circularArc(_this.config.centerX, _this.config.centerY,
_this.config.radius, 180, 0);
_this.gauge.gauge.attr({
'fill' : _this.config.fill,
'stroke' : 'none'
});
_this.gauge.gauge.node.setAttribute('class', 'gauge');
// Draw the gauge background
_this.gauge.gaugeBackground = _this.gauge.circularArc(_this.config.centerX, _this.config.centerY,
_this.config.radius, 180, 0);
_this.gauge.gaugeBackground.attr({
'fill' : _this.config.gaugeBackground,
'stroke' : 'none'
});
_this.gauge.gaugeBackground.node.setAttribute('class', 'gauge__background');
// Draw the white center arc
_this.gauge.centerArc = _this.gauge.circularArc(_this.config.centerX, _this.config.centerY,
_this.config.radius - _this.config.gaugeWidth, 180, 0);
_this.gauge.centerArc.attr({
'fill' : _this.config.background,
'stroke' : 'none'
});
_this.gauge.centerArc.node.setAttribute('class', 'gauge__center');
// Draw the needle
if (_this.config.showNeedle) {
_this.gauge.needle = _this.gauge.rect(_this.config.centerX, _this.config.paddingY, 1, 40);
_this.gauge.needle.attr({
'fill' : '#000',
'stroke' : 'none'
});
_this.gauge.needle.node.setAttribute('class', 'gauge__needle');
}
// Draw the bottom mask to hide the rotated background arc
_this.gauge.bottomMask = _this.gauge.rect(0, _this.config.centerY, _this.config.width, 40);
_this.gauge.bottomMask.attr({
'fill' : _this.config.background,
'stroke' : 'none'
});
// Draw the text container for the value
if (_this.config.valueLabel.display) {
if (_this.config.showNeedle) {
_this.gauge.valueLabel = _this.gauge.text(_this.config.centerX, _this.config.centerY - 10,
Math.round((_this.config.max - _this.config.min) / 2));
} else {
_this.gauge.valueLabel = _this.gauge.text(_this.config.centerX, _this.config.centerY - 10,
_this.config.value + _this.config.valueEndText);
}
_this.gauge.valueLabel.attr({
'fill' : _this.config.valueLabel.fontColor,
'font-size' : _this.config.valueLabel.fontSize,
'font-family' : _this.config.valueLabel.fontColor,
'font-weight' : _this.config.valueLabel.fontWeight
});
_this.gauge.valueLabel.node.setAttribute('class', 'gauge__value');
}
// Draw the title
if (_this.config.title.display) {
_this.gauge.title = _this.gauge.text(_this.config.centerX, _this.config.paddingY - 5,
_this.config.title.value);
_this.gauge.title.attr({
'fill' : _this.config.title.fontColor,
'fill-opacity' : 0,
'font-size' : _this.config.title.fontSize,
'font-family' : _this.config.title.fontFamily,
'font-weight' : _this.config.title.fontWeight
});
_this.gauge.title.node.setAttribute('class', 'gauge__title');
}
if (_this.config.label.display) {
// Draw the left label
_this.gauge.leftLabel = _this.gauge.text((_this.config.gaugeWidth / 2) + _this.config.paddingX,
_this.config.centerY + 10, _this.config.label.left);
_this.gauge.leftLabel.attr({
'fill' : _this.config.title.fontColor,
'fill-opacity' : 0,
'font-size' : _this.config.label.fontSize,
'font-family' : _this.config.label.fontFamily,
'font-weight' : _this.config.label.fontWeight
});
_this.gauge.leftLabel.node.setAttribute('class', 'gauge__label--left');
// Draw the right label
_this.gauge.rightLabel = _this.gauge.text((_this.config.width - (_this.config.gaugeWidth / 2)) - _this.config.paddingX,
_this.config.centerY + 10, _this.config.label.right);
_this.gauge.rightLabel.attr({
'fill' : _this.config.title.fontColor,
'fill-opacity' : 0,
'font-size' : _this.config.label.fontSize,
'font-family' : _this.config.label.fontFamily,
'font-weight' : _this.config.label.fontWeight
});
_this.gauge.rightLabel.node.setAttribute('class', 'gauge__label--right');
}
// There is a bug with raphael.js and webkit which renders text element at double the Y value.
// Resetting the Y values after a timeout fixes this.
// See https://github.com/DmitryBaranovskiy/raphael/issues/491
setTimeout(function() {
if (_this.config.valueLabel.display) {
_this.gauge.valueLabel.attr('y', _this.config.centerY - 10);
}
if (_this.config.title.display) {
_this.gauge.title.attr({
'y' : _this.config.paddingY - (_this.gauge.title.getBBox().height / 2),
'fill-opacity' : 1
});
}
if (_this.config.label.display) {
_this.gauge.leftLabel.attr({
'y' : _this.config.centerY + (_this.gauge.leftLabel.getBBox().height / 2),
'fill-opacity' : 1,
});
_this.gauge.rightLabel.attr({
'y' : _this.config.centerY + (_this.gauge.rightLabel.getBBox().height / 2),
'fill-opacity' : 1,
});
}
}, 1000);
// Animate the gauge to the right value position
_this.gauge.gaugeBackground.animate({transform:'r' +
_this._calculateRotation(_this.config.min, _this.config.max, _this.config.value) + ',' +
_this.config.centerX + ',' + _this.config.centerY}, _this.config.animationSpeed, '<>');
},
update: function (data) {
//this
var _this = this;
var updateGauge = function(min, max, value) {
_this.config.min = min;
_this.config.max = max;
_this.config.value = value;
// Update the rotation of the gauge
_this.gauge.gaugeBackground.animate({transform:'r' +
_this._calculateRotation(min, max, value) + ',' +
_this.config.centerX + ',' + _this.config.centerY}, _this.config.animationSpeed, '<>');
// Update the value label
if (_this.config.valueLabel.display) {
if (_this.config.showNeedle) {
_this.gauge.valueLabel.attr('text', value);
} else {
_this.gauge.valueLabel.attr('text', (max - min) / 2);
}
}
};
if (typeof data.min !== 'undefined' && typeof data.max !== 'undefined' && typeof data.value !== 'undefined') {
updateGauge(data.min, data.max, data.value);
} else if (typeof data.value !== 'undefined') {
updateGauge(_this.config.min, _this.config.max, data.value);
}
}
};
$.fn.kumaGauge = function ( method, options ) {
var _method = method,
_arguments = arguments,
_this = this;
if (typeof _method !== 'string') {
if (_arguments.length === 1 ) {
options = _method;
method = false;
return this.each(function() {
if ( !$.data( this, 'kumaGauge' ) ) {
$.data( this, 'kumaGauge', new kumaGauge( this, options, method ) );
}
});
}
} else {
return this.each(function() {
if (typeof $.data(this, 'kumaGauge')[method] === 'function') {
$.data(this, 'kumaGauge')[method](options);
}
});
}
};
})( jQuery, window, document );