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
|
/**
* o------------------------------------------------------------------------------o
* | This file is part of the RGraph package - you can learn more at: |
* | |
* | http://www.rgraph.net |
* | |
* | This package is licensed under the RGraph license. For all kinds of business |
* | purposes there is a small one-time licensing fee to pay and for non |
* | commercial purposes it is free to use. You can read the full license here: |
* | |
* | http://www.rgraph.net/LICENSE.txt |
* o------------------------------------------------------------------------------o
*/
if (typeof(RGraph) == 'undefined') RGraph = {};
/**
* The line chart constructor
*
* @param object canvas The cxanvas object
* @param array data The chart data
* @param array ... Other lines to plot
*/
RGraph.Gauge = function (id, min, max, value)
{
// Get the canvas and context objects
this.id = id;
this.canvas = document.getElementById(id);
this.context = this.canvas.getContext ? this.canvas.getContext("2d") : null;
this.canvas.__object__ = this;
this.type = 'gauge';
this.min = min;
this.max = max;
this.value = value;
this.isRGraph = true;
this.currentValue = null;
/**
* Range checking
*/
if (this.value > this.max) {
this.value = max;
}
if (this.value < this.min) {
this.value = min;
}
/**
* Compatibility with older browsers
*/
RGraph.OldBrowserCompat(this.context);
// Various config type stuff
this.properties = {
'chart.gutter.left': 5,
'chart.gutter.right': 5,
'chart.gutter.top': 5,
'chart.gutter.bottom': 5,
'chart.border.width': 10,
'chart.title': '',
'chart.title.font': 'Verdana',
'chart.title.size': 14,
'chart.title.color': '#333',
'chart.title.bold': false,
'chart.text.font': 'Verdana',
'chart.text.color': '#666',
'chart.text.size': 10,
'chart.scale.decimals': 0,
'chart.units.pre': '',
'chart.units.post': '',
'chart.red.start': 0.9 * this.max,
'chart.red.color': '#DC3912',
'chart.yellow.color': '#FF9900',
'chart.green.end': 0.7 * this.max,
'chart.green.color': 'rgba(0,0,0,0)',
'chart.needle.tail': false
}
}
/**
* An all encompassing accessor
*
* @param string name The name of the property
* @param mixed value The value of the property
*/
RGraph.Gauge.prototype.Set = function (name, value)
{
this.properties[name] = value;
}
/**
* An all encompassing accessor
*
* @param string name The name of the property
*/
RGraph.Gauge.prototype.Get = function (name)
{
return this.properties[name];
}
/**
* The function you call to draw the line chart
*
* @param bool An optional bool used internally to ditinguish whether the
* line chart is being called by the bar chart
*/
RGraph.Gauge.prototype.Draw = function ()
{
/**
* Fire the onbeforedraw event
*/
RGraph.FireCustomEvent(this, 'onbeforedraw');
/**
* Clear all of this canvases event handlers (the ones installed by RGraph)
*/
RGraph.ClearEventListeners(this.id);
/**
* Store the value (for animation primarily
*/
this.currentValue = this.value;
/**
* This is new in May 2011 and facilitates indiviual gutter settings,
* eg chart.gutter.left
*/
this.gutterLeft = this.Get('chart.gutter.left');
this.gutterRight = this.Get('chart.gutter.right');
this.gutterTop = this.Get('chart.gutter.top');
this.gutterBottom = this.Get('chart.gutter.bottom');
this.centerx = ((this.canvas.width - this.gutterLeft - this.gutterRight) / 2) + this.gutterLeft;
this.centery = ((this.canvas.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop;
this.radius = Math.min(
((this.canvas.width - this.gutterLeft - this.gutterRight) / 2),
((this.canvas.height - this.gutterTop - this.gutterBottom) / 2)
);
this.startAngle = (1.57 / 3) + 1.57;
this.endAngle = 6.28 + 1.57 - (1.57 / 3);
// This has to be in the constructor
this.centerpinRadius = (15/125) * this.radius;
/**
* Setup the context menu if required
*/
if (this.Get('chart.contextmenu')) {
RGraph.ShowContext(this);
}
// DRAW THE CHART HERE
this.DrawBackGround();
this.DrawColorBands();
this.DrawSmallTickmarks();
this.DrawBigTickmarks();
this.DrawLabels();
this.DrawTitle();
this.DrawNeedle();
this.DrawCenterpin();
/**
* If the canvas is annotatable, do install the event handlers
*/
if (this.Get('chart.annotatable')) {
RGraph.Annotate(this);
}
/**
* This bit shows the mini zoom window if requested
*/
if (this.Get('chart.zoom.mode') == 'thumbnail') {
RGraph.ShowZoomWindow(this);
}
/**
* This function enables the zoom in area mode
*/
if (this.Get('chart.zoom.mode') == 'area') {
RGraph.ZoomArea(this);
}
/**
* This function enables resizing
*/
if (this.Get('chart.resizable')) {
RGraph.AllowResizing(this);
}
/**
* Fire the RGraph ondraw event
*/
RGraph.FireCustomEvent(this, 'ondraw');
}
/**
* Draw the background
*/
RGraph.Gauge.prototype.DrawBackGround = function ()
{
var borderWidth = this.Get('chart.border.width');
this.context.beginPath();
this.context.fillStyle = 'white';
this.context.arc(this.centerx, this.centery, this.radius, 0, 6.28, 0);
this.context.fill();
/**
* Draw the gray circle
*/
this.context.beginPath();
this.context.fillStyle = '#ccc';;
this.context.arc(this.centerx, this.centery, this.radius, 0, 6.28, 0);
this.context.fill();
/**
* Draw the light gray inner border
*/
this.context.beginPath();
this.context.fillStyle = '#f1f1f1';
this.context.arc(this.centerx, this.centery, this.radius - borderWidth, 0, 6.28, 0);
this.context.fill();
// Draw the white circle inner border
this.context.beginPath();
this.context.fillStyle = 'white';
this.context.arc(this.centerx, this.centery, this.radius - borderWidth - 4, 0, 6.28, 0);
this.context.fill();
this.context.beginPath();
this.context.strokeStyle = 'black';
//this.context.moveTo(this.centerx, this.centery)
this.context.arc(this.centerx, this.centery, this.radius, 0, 6.28, 0);
this.context.stroke();
}
/**
* This function draws the smaller tickmarks
*/
RGraph.Gauge.prototype.DrawSmallTickmarks = function ()
{
var numTicks = 25;
for (var i=0; i<=numTicks; ++i) {
this.context.beginPath();
this.context.strokeStyle = 'black';
var a = (((this.endAngle - this.startAngle) / numTicks) * i) + this.startAngle;
this.context.arc(this.centerx, this.centery, this.radius - this.Get('chart.border.width') - 10, a, a + 0.00001, 0);
this.context.arc(this.centerx, this.centery, this.radius - this.Get('chart.border.width') - 10 - 5, a, a + 0.00001, 0);
this.context.stroke();
}
}
/**
* This function draws the large, bold tickmarks
*/
RGraph.Gauge.prototype.DrawBigTickmarks = function ()
{
var numTicks = 5;
this.context.lineWidth = 3;
this.context.lineCap = 'round';
for (var i=0; i<=numTicks; ++i) {
this.context.beginPath();
this.context.strokeStyle = 'black';
var a = (((this.endAngle - this.startAngle) / numTicks) * i) + this.startAngle;
this.context.arc(this.centerx, this.centery, this.radius - this.Get('chart.border.width') - 10, a, a + 0.00001, 0);
this.context.arc(this.centerx, this.centery, this.radius - this.Get('chart.border.width') - 10 - 10, a, a + 0.00001, 0);
this.context.stroke();
}
}
/**
* This function draws the centerpin
*/
RGraph.Gauge.prototype.DrawCenterpin = function ()
{
var offset = 6;
var grad = this.context.createRadialGradient(this.centerx + offset, this.centery - offset, 0, this.centerx + offset, this.centery - offset, 25);
grad.addColorStop(0, '#ddf');
grad.addColorStop(1, 'blue');
this.context.beginPath();
this.context.fillStyle = grad;
this.context.arc(this.centerx, this.centery, this.centerpinRadius, 0, 6.28, 0);
this.context.fill();
}
/**
* This function draws the labels
*/
RGraph.Gauge.prototype.DrawLabels = function ()
{
this.context.fillStyle = this.Get('chart.text.color');
this.context.beginPath();
// First label
RGraph.Text(this.context, this.Get('chart.text.font'), this.Get('chart.text.size'), this.centerx - Math.sin(0.52) * (this.radius - 25 - this.Get('chart.border.width')),this.centery + Math.cos(0.52) * (this.radius - 25 - this.Get('chart.border.width')), this.Get('chart.units.pre') + String(this.min.toFixed(this.Get('chart.scale.decimals'))) + this.Get('chart.units.post'),'bottom', 'left');
// Second label
RGraph.Text(this.context, this.Get('chart.text.font'), this.Get('chart.text.size'), this.centerx - this.radius + 25 + this.Get('chart.border.width'), this.centery,this.Get('chart.units.pre') + String((((this.max - this.min) * 0.2) + this.min).toFixed(this.Get('chart.scale.decimals')) + this.Get('chart.units.post')),'center', 'left');
// Third label
RGraph.Text(this.context, this.Get('chart.text.font'), this.Get('chart.text.size'), this.centerx - Math.sin(0.52) * (this.radius - 25 - this.Get('chart.border.width')),this.centery - Math.cos(0.52) * (this.radius - 25 - this.Get('chart.border.width')),this.Get('chart.units.pre') + String((((this.max - this.min) * 0.4) + this.min).toFixed(this.Get('chart.scale.decimals'))) + this.Get('chart.units.post'),'top', 'center');
// Fourth label
RGraph.Text(this.context, this.Get('chart.text.font'), this.Get('chart.text.size'), this.centerx + Math.sin(0.52) * (this.radius - 25 - this.Get('chart.border.width')),this.centery - Math.cos(0.52) * (this.radius - 25 - this.Get('chart.border.width')),this.Get('chart.units.pre') + String((((this.max - this.min) * 0.6) + this.min).toFixed(this.Get('chart.scale.decimals'))) + this.Get('chart.units.post'),'top', 'center');
// Fifth label
RGraph.Text(this.context, this.Get('chart.text.font'), this.Get('chart.text.size'), this.centerx + this.radius - 25 - this.Get('chart.border.width'), this.centery, this.Get('chart.units.pre') + String((((this.max - this.min) * 0.8) + this.min).toFixed(this.Get('chart.scale.decimals'))) + this.Get('chart.units.post'),'center', 'right');
// Sixth (last) label
RGraph.Text(this.context,this.Get('chart.text.font'), this.Get('chart.text.size'), this.centerx + Math.sin(0.52) * (this.radius - 25 - this.Get('chart.border.width')),this.centery + Math.cos(0.52) * (this.radius - 25 - this.Get('chart.border.width')),this.Get('chart.units.pre') + String(this.max.toFixed(this.Get('chart.scale.decimals'))) + this.Get('chart.units.post'),'bottom', 'right');
this.context.fill();
}
/**
* This function draws the title
*/
RGraph.Gauge.prototype.DrawTitle = function ()
{
var title = this.Get('chart.title');
if (title) {
this.context.fillStyle = this.Get('chart.title.color');
this.context.beginPath();
RGraph.Text(this.context,
this.Get('chart.title.font'),
this.Get('chart.title.size'),
this.centerx,
this.centery - 30,
String(this.Get('chart.title')),
'center',
'center',
null,
null,
null,
this.Get('chart.title.bold'));
this.context.fill();
}
}
/**
* This function draws the Needle
*/
RGraph.Gauge.prototype.DrawNeedle = function ()
{
this.context.lineWidth = 0.5;
this.context.strokeStyle = '#983724';
this.context.fillStyle = '#D5604D';
var angle = (this.endAngle - this.startAngle) * ((this.value - this.min) / (this.max - this.min));
angle += this.startAngle;
this.context.beginPath();
this.context.arc(this.centerx, this.centery, this.radius - 25 - this.Get('chart.border.width'), angle, angle + 0.00001, false);
this.context.arc(this.centerx, this.centery, this.centerpinRadius * 0.5, angle + 1.57, angle + 0.00001 + 1.57, false);
if (this.Get('chart.needle.tail')) {
this.context.arc(this.centerx, this.centery, this.radius * 0.2 , angle + 3.14, angle + 0.00001 + 3.14, false);
}
this.context.arc(this.centerx, this.centery, this.centerpinRadius * 0.5, angle - 1.57, angle - 0.00001 - 1.57, false);
this.context.stroke();
this.context.fill();
//this.context.strokeStyle = '#D5604D';
//this.context.beginPath();
// this.context.lineWidth = 5;
// this.context.arc(this.centerx, this.centery, 25, angle + 3.14, angle + 0.00001 + 3.14, false);
// this.context.lineTo(this.centerx, this.centery);
//this.context.stroke();
//this.context.fill();
/**
* Store the angle in an object variable
*/
this.angle = angle;
}
/**
* This draws the green background to the tickmarks
*/
RGraph.Gauge.prototype.DrawColorBands = function ()
{
/**
* Draw the GREEN region
*/
this.context.strokeStyle = this.Get('chart.green.color');
this.context.fillStyle = this.Get('chart.green.color');
var greenStart = this.startAngle;
var greenEnd = this.startAngle + (this.endAngle - this.startAngle) * ((this.Get('chart.green.end') - this.min) / (this.max - this.min))
this.context.beginPath();
this.context.arc(this.centerx, this.centery, this.radius - 10 - this.Get('chart.border.width'), greenStart, greenEnd, false);
this.context.arc(this.centerx, this.centery, this.radius - 20 - this.Get('chart.border.width'), greenEnd, greenStart, true);
this.context.fill();
/**
* Draw the YELLOW region
*/
this.context.strokeStyle = this.Get('chart.yellow.color');
this.context.fillStyle = this.Get('chart.yellow.color');
var yellowStart = greenEnd;
var yellowEnd = this.startAngle + (this.endAngle - this.startAngle) * ((this.Get('chart.red.start') - this.min) / (this.max - this.min))
this.context.beginPath();
this.context.arc(this.centerx, this.centery, this.radius - 10 - this.Get('chart.border.width'), yellowStart, yellowEnd, false);
this.context.arc(this.centerx, this.centery, this.radius - 20 - this.Get('chart.border.width'), yellowEnd, yellowStart, true);
this.context.fill();
/**
* Draw the RED region
*/
this.context.strokeStyle = this.Get('chart.red.color');
this.context.fillStyle = this.Get('chart.red.color');
var redStart = yellowEnd;
var redEnd = this.startAngle + (this.endAngle - this.startAngle) * ((this.max - this.min) / (this.max - this.min))
this.context.beginPath();
this.context.arc(this.centerx, this.centery, this.radius - 10 - this.Get('chart.border.width'), redStart, redEnd, false);
this.context.arc(this.centerx, this.centery, this.radius - 20 - this.Get('chart.border.width'), redEnd, redStart, true);
this.context.fill();
}
|