-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgauge.js
488 lines (418 loc) · 16.8 KB
/
gauge.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
var prevGaugePoint = 0;
var d3;
var config = {
size : 200,
clipWidth : 200,
clipHeight : 110,
ringInset : 20,
ringWidth : 20,
arcColor : "#AAAAAA",
pointerWidth : 5,
pointerTailLength : 1,
pointerHeadLengthPercent : 0.82,
minValue : 0,
maxValue : 100,
minAngle : -90,
maxAngle : 90,
transitionMs : 750,
secondArcStart : 30,
secondArcEnd : 90,
secondArcColor : "#DDFFDD",
thirdArcStart : 50,
thirdArcEnd : 70,
thirdArcColor : "#00FF00",
NeedleColor : "dimgrey",
majorTicks : 10,
labelFormat : "%",
labelInset : 10,
arcColorFn : d3.interpolateHsl(d3.rgb('#e8e2ca'), d3.rgb('#3e6c0a'))
};
var gauge = function(container, configuration) {
var that = {};
var range;
var r;
var pointerHeadLength;
var svg;
var arcGrey, arcLightGreen, arcGreen;
var pointerTail, pointerCircle;
var scale;
var ticks;
var tickData;
var pointer;
d3.layout.pie();
function deg2rad(deg) {
return deg * Math.PI / 180;
}
function configure(configuration) {
for (var prop in configuration ) {
config[prop] = configuration[prop];
}
range = config.maxAngle - config.minAngle;
r = config.size / 2;
pointerHeadLength = Math.round(r * config.pointerHeadLengthPercent);
// a linear scale that maps domain values to a percent from 0..1
scale = d3.scale.linear()
.range([0,1])
.domain([config.minValue, config.maxValue]);
ticks = scale.ticks(config.majorTicks);
tickData = d3.range(config.majorTicks).map(function() {return 1/config.majorTicks;});
arcGrey = d3.svg.arc()
.innerRadius(r * 0.8)
.outerRadius(r * 0.85)
.startAngle(function(d, i) {
var ratio = d * i;
return deg2rad(config.minAngle + (ratio * range));
})
.endAngle(function(d, i) {
var ratio = d * (i+1);
return deg2rad(config.minAngle + (ratio * range));
});
arcLightGreen = d3.svg.arc()
.innerRadius(r * 0.75)
.outerRadius(r * 0.9)
.startAngle(deg2rad(config.minAngle + 180 / 100 * config.secondArcStart))
.endAngle(deg2rad(config.minAngle + 180 / 100 * config.secondArcEnd));
arcGreen = d3.svg.arc()
.innerRadius(r * 0.75)
.outerRadius(r * 0.9)
.startAngle(deg2rad(config.minAngle + 180 / 100 * config.thirdArcStart))
.endAngle(deg2rad(config.minAngle + 180 / 100 * config.thirdArcEnd));
}
that.configure = configure;
function centerTranslation() {
return 'translate('+r +','+ r +')';
}
function isRendered() {
return (svg !== undefined);
}
that.isRendered = isRendered;
function render(newValue) {
svg = d3.select(container)
.append('svg:svg')
.attr('class', 'gauge')
.attr('width', config.clipWidth)
.attr('height', config.clipHeight);
var centerTx = centerTranslation();
// Add Arcs
var arcsGrey = svg.append('g')
.attr('class', 'arcGrey')
.attr('transform', centerTx);
arcsGrey.selectAll('path')
.data(tickData)
.enter().append('path')
.attr('fill', config.arcColor)
.attr('d', arcGrey);
var arcsLightGreen = svg.append('g')
.attr('class', 'arcLightGreen')
.attr('transform', centerTx);
arcsLightGreen.append('path')
.attr('fill', config.secondArcColor)
.attr('d', arcLightGreen);
var arcsGreen = svg.append('g')
.attr('class', 'arcGreen')
.attr('transform', centerTx)
.attr('y', 40 + "px");
arcsGreen.append('path')
.attr('fill', config.thirdArcColor)
.attr('d', arcGreen);
// Add Ticks
var label_grey = svg.append('g')
.attr('class', 'label_grey')
.attr('transform', centerTx);
label_grey.selectAll('text')
.data([config.minValue, config.maxValue])
.enter().append('text')
.attr('x', function(d){
var radius = 180 - 180 / 100 * d;
radius = 3.14 / 180 * radius;
return Math.cos(radius) * (pointerHeadLength + 40);
})
.attr('y', function(d){
var radius = 180 - 180 / 100 * d;
radius = 3.14 / 180 * radius;
return - Math.sin(radius) * (pointerHeadLength + 2);
})
.text(function(d){
return d + config.labelFormat;
});
var label_lightgreen = svg.append('g')
.attr('class', 'label_lightgreen')
.attr('transform', centerTx);
label_lightgreen.selectAll('text')
.data([config.secondArcStart, config.secondArcEnd])
.enter().append('text')
.attr('class', function(d, i){
if (i == 0) return "start2";
else return "end2";
})
.attr('x', function(d){
var radius = 180 - 180 / 100 * d;
radius = 3.14 / 180 * radius;
return Math.cos(radius) * (pointerHeadLength + 40);
})
.attr('y', function(d){
var radius = 180 - 180 / 100 * d;
radius = 3.14 / 180 * radius;
return - Math.sin(radius) * (pointerHeadLength + 2);
})
.text(function(d){
return d + config.labelFormat;
});
var label_green = svg.append('g')
.attr('class', 'label_green')
.attr('transform', centerTx);
label_green.selectAll('text')
.data([config.thirdArcStart, config.thirdArcEnd])
.enter().append('text')
.attr('class', function(d, i){
if (i == 0) return "start3";
else return "end3";
})
.attr('x', function(d){
var radius = 180 - 180 / 100 * d;
radius = 3.14 / 180 * radius;
return Math.cos(radius) * (pointerHeadLength + 40);
})
.attr('y', function(d){
var radius = 180 - 180 / 100 * d;
radius = 3.14 / 180 * radius;
return - Math.sin(radius) * (pointerHeadLength + 2);
})
.text(function(d){
return d + config.labelFormat;
});
svg.append('g')
.attr('class', 'total')
.attr('transform', centerTx)
.append('text')
.attr('class', 'totalpoint')
.attr('x', 20)
.attr('y', 70)
.text(50);
d3.selectAll('.total')
.append('text')
.attr('class', 'totalpercent')
.attr('x', 20)
.attr('y', 70)
.text(config.labelFormat);
var lineData = [ [config.pointerWidth / 2, 0],
[0, -pointerHeadLength]];
var pointerLine = d3.svg.line();
var pg = svg.append('g').data([lineData])
.attr('class', 'pointerNeedle')
.style('stroke-width', 2)
.attr('transform', centerTx);
pointer = pg.append('path')
.attr('d', pointerLine/*function(d) { return pointerLine(d) +'Z';}*/ )
.style('fill', config.NeedleColor)
.style('stroke', config.NeedleColor)
.attr('transform', 'rotate(' + config.minAngle + ')');
var lineData1 = [
[config.pointerWidth / 2 - 5, 30],
[config.pointerWidth / 2 - 5, -pointerHeadLength/10],
[config.pointerWidth / 2 + 4, -pointerHeadLength/10],
[config.pointerWidth / 2 + 4, 30],
];
var pointerLine1 = d3.svg.line();
var pg1 = svg.append('g').data([lineData1])
.attr('class', 'pointerTail')
.style('stroke-width', 1)
.style('fill', config.NeedleColor)
.style('stroke', config.NeedleColor)
.attr('transform', centerTx);
pointerTail = pg1.append('path')
.attr('d', pointerLine1)
.attr('transform', 'rotate(' +config.minAngle +')');
pointerCircle = pg1.append("circle")
.attr("class", "circle")
.attr("r", 16)
.attr("cx", config.pointerWidth / 2)
.attr("cy", 0)
.style("fill", "white")
.style("stroke-width", 2)
.attr('transform', 'rotate(' +config.minAngle +')');
updatearcGrey(newValue === undefined ? 0 : newValue);
}
that.render = render;
function updatearcGrey(newValue, newConfiguration) {
if ( newConfiguration !== undefined) {
configure(newConfiguration);
}
var ratio = scale(newValue);
var newAngle = config.minAngle + (ratio * range);
pointer.transition()
.duration(config.transitionMs)
.ease('elastic')
.attr('transform', 'rotate(' +newAngle +')');
pointerTail.transition()
.duration(config.transitionMs)
.ease('elastic')
.attr('transform', 'rotate(' +newAngle +')');
pointerCircle.transition()
.duration(config.transitionMs)
.ease('elastic')
.attr('transform', 'rotate(' +newAngle +')');
d3.selectAll('.totalpoint')
.text(Math.floor(newValue));
}
that.updatearcGrey = updatearcGrey;
function updatearcLightGreen(startAngle, endAngle, percent){
// Draw Light Green Arcs By start & end Angle
d3.selectAll('.start2')
.attr('x', function(d){
var radius = 180 - 180 / 100 * startAngle;
radius = 3.14 / 180 * radius;
return Math.cos(radius) * (pointerHeadLength + 40);
})
.attr('y', function(d){
var radius = 180 - 180 / 100 * startAngle;
radius = 3.14 / 180 * radius;
return - Math.sin(radius) * (pointerHeadLength + 2);
})
.text(Math.floor(startAngle) + config.labelFormat);
d3.selectAll('.end2')
.attr('x', function(d){
var radius = 180 - 180 / 100 * endAngle;
radius = 3.14 / 180 * radius;
return Math.cos(radius) * (pointerHeadLength + 40);
})
.attr('y', function(d){
var radius = 180 - 180 / 100 * endAngle;
radius = 3.14 / 180 * radius;
return - Math.sin(radius) * (pointerHeadLength + 2);
})
.text(Math.floor(endAngle) + config.labelFormat);
var value = 50;
var count = 0;
function func() {
var step1 = config.secondArcStart + (startAngle - config.secondArcStart) / value * (count + 1);
var step2 = config.secondArcEnd + (endAngle - config.secondArcEnd) / value * (count + 1);
arcLightGreen = d3.svg.arc()
.innerRadius(r * 0.75)
.outerRadius(r * 0.9)
.startAngle(deg2rad(config.minAngle + 180 / 100 * step1))
.endAngle(deg2rad(config.minAngle + 180 / 100 * step2));
d3.selectAll('.arcLightGreen > path')
.transition().duration(500 / value)
.attr('d', arcLightGreen);
count ++;
if (count < value) setTimeout(func, 500 / value);
else{
config.secondArcStart = startAngle;
config.secondArcEnd = endAngle;
updatePointColor(percent);
}
}
func();
}
that.updatearcLightGreen = updatearcLightGreen;
function updatearcGreen(startAngle, endAngle, percent){
// Draw Green Arcs By start & end Angle
d3.selectAll('.start3')
.attr('x', function(d){
var radius = 180 - 180 / 100 * startAngle;
radius = 3.14 / 180 * radius;
return Math.cos(radius) * (pointerHeadLength + 40);
})
.attr('y', function(d){
var radius = 180 - 180 / 100 * startAngle;
radius = 3.14 / 180 * radius;
return - Math.sin(radius) * (pointerHeadLength + 20);
})
.text(Math.floor(startAngle) + config.labelFormat);
d3.selectAll('.end3')
.attr('x', function(d){
var radius = 180 - 180 / 100 * endAngle;
radius = 3.14 / 180 * radius;
return Math.cos(radius) * (pointerHeadLength + 40);
})
.attr('y', function(d){
var radius = 180 - 180 / 100 * endAngle;
radius = 3.14 / 180 * radius;
return - Math.sin(radius) * (pointerHeadLength + 20);
})
.text(Math.floor(endAngle) + config.labelFormat);
var value = 50;
var count = 0;
function func() {
var step1 = config.thirdArcStart + (startAngle - config.thirdArcStart) / value * (count + 1);
var step2 = config.thirdArcEnd + (endAngle - config.thirdArcEnd) / value * (count + 1);
arcGreen = d3.svg.arc()
.innerRadius(r * 0.75)
.outerRadius(r * 0.9)
.startAngle(deg2rad(config.minAngle + 180 / 100 * step1))
.endAngle(deg2rad(config.minAngle + 180 / 100 * step2));
d3.selectAll('.arcGreen > path')
.transition().duration(500 / value)
.attr('d', arcGreen);
count ++;
if (count < value) setTimeout(func, 500 / value);
else{
config.thirdArcStart = startAngle;
config.thirdArcEnd = endAngle;
updatePointColor(percent);
}
}
func();
}
that.updatearcGreen = updatearcGreen;
function updatePointColor(percent){
if ((percent >= config.thirdArcStart) && (percent <= config.thirdArcEnd)){
pointerCircle.style("fill", config.thirdArcColor);
}else if ((percent >= config.secondArcStart) && (percent < config.thirdArcStart)){
pointerCircle.style("fill", config.secondArcColor);
}else if ((percent > config.thirdArcEnd) && (percent <= config.secondArcEnd)){
pointerCircle.style("fill", config.secondArcColor);
}else{
pointerCircle.style("fill", config.arcColor);
}
}
that.updatePointColor = updatePointColor;
configure(configuration);
return that;
};
var powerGauge = gauge('.gauge', {
size: 300,
clipWidth: 300,
clipHeight: 300,
ringWidth: 60,
maxValue: 100,
transitionMs: 1500,
});
powerGauge.render();
function updateGauge() {
// just pump in random data here...
var randomValue = Math.random() * 100;
// randomValue = 0;
randomValue = Math.floor(randomValue);
powerGauge.updatearcGrey(randomValue);
var startAngle2 = 10 + Math.random() * 20;
var endAngle2 = 70 + Math.random() * 20;
powerGauge.updatearcLightGreen(startAngle2, endAngle2, randomValue);
var startAngle3 = 35 + Math.random() * 10;
var endAngle3 = 50 + Math.random() * 15;
powerGauge.updatearcGreen(startAngle3, endAngle3, randomValue);
if (prevGaugePoint > randomValue){
document.getElementById("gaugesymbol").classList.remove('glyphicon-triangle-bottom');
document.getElementById("gaugesymbol").classList.remove('glyphicon-triangle-top');
document.getElementById("gaugesymbol").classList.add('glyphicon-triangle-bottom');
document.getElementById("gaugecompareid").classList.remove('red');
document.getElementById("gaugecompareid").classList.remove('green');
document.getElementById("gaugecompareid").classList.add('red');
document.getElementById("gaugecomparetext").innerText = (prevGaugePoint - randomValue) + "%";
}else if(prevGaugePoint < randomValue){
document.getElementById("gaugesymbol").classList.remove('glyphicon-triangle-bottom');
document.getElementById("gaugesymbol").classList.remove('glyphicon-triangle-top');
document.getElementById("gaugesymbol").classList.add('glyphicon-triangle-top');
document.getElementById("gaugecompareid").classList.remove('red');
document.getElementById("gaugecompareid").classList.remove('green');
document.getElementById("gaugecompareid").classList.add('green');
document.getElementById("gaugecomparetext").innerText = (randomValue - prevGaugePoint) + "%";
}
prevGaugePoint = randomValue;
}
// every few seconds update reading values
updateGauge();
// setInterval(function() {
// updateGauge();
// }, 3 * 1000);