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
|
/**
* 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 chart constructor. This function sets up the object. It takes the ID (the HTML attribute) of the canvas as the
* first argument and the data as the second. If you need to change this, you can.
*
* @param string id The canvas tag ID
* @param number min The minimum value
* @param number max The maximum value
* @param number value The value reported by the thermometer
*/
RGraph.Thermometer = function (id, min, max, value)
{
this.id = id;
this.canvas = document.getElementById(id);
this.context = this.canvas.getContext ? this.canvas.getContext("2d") : null;
this.canvas.__object__ = this;
this.type = 'thermometer';
this.isRGraph = true;
this.min = min;
this.max = max;
this.value = value;
this.coords = [];
this.graphArea = [];
this.currentValue = null;
RGraph.OldBrowserCompat(this.context);
this.properties = {
'chart.colors': ['red'],
'chart.gutter.left': 15,
'chart.gutter.right': 15,
'chart.gutter.top': 15,
'chart.gutter.bottom': 15,
'chart.ticksize': 5,
'chart.text.color': 'black',
'chart.text.font': 'Verdana',
'chart.text.size': 10,
'chart.units.pre': '',
'chart.units.post': '',
'chart.zoom.factor': 1.5,
'chart.zoom.fade.in': true,
'chart.zoom.fade.out': true,
'chart.zoom.hdir': 'right',
'chart.zoom.vdir': 'down',
'chart.zoom.frames': 25,
'chart.zoom.delay': 16.666,
'chart.zoom.shadow': true,
'chart.zoom.mode': 'canvas',
'chart.zoom.thumbnail.width': 75,
'chart.zoom.thumbnail.height': 75,
'chart.zoom.background': true,
'chart.title': '',
'chart.title.hpos': 0.5,
'chart.title.side': '',
'chart.title.side.bold': true,
'chart.title.side.font': null,
'chart.shadow': true,
'chart.shadow.offsetx': 0,
'chart.shadow.offsety': 0,
'chart.shadow.blur': 15,
'chart.shadow.color': 'gray',
'chart.resizable': false,
'chart.contextmenu': null,
'chart.adjustable': false,
'chart.value.label': true,
'chart.scale.visible': false,
'chart.scale.decimals': 0,
'chart.ylabels.count': 10,
'chart.annotatable': false,
'chart.annotate.color': 'black'
}
/**
* A simple check that the browser has canvas support
*/
if (!this.canvas) {
alert('[THERMOMETER] No canvas support');
return;
}
}
/**
* A setter.
*
* @param name string The name of the property to set
* @param value mixed The value of the property
*/
RGraph.Thermometer.prototype.Set = function (name, value)
{
this.properties[name.toLowerCase()] = value;
}
/**
* A getter.
*
* @param name string The name of the property to get
*/
RGraph.Thermometer.prototype.Get = function (name)
{
return this.properties[name];
}
/**
* Draws the thermometer
*/
RGraph.Thermometer.prototype.Draw = function ()
{
/**
* Fire the custom RGraph onbeforedraw event (which should be fired before the chart is drawn)
*/
RGraph.FireCustomEvent(this, 'onbeforedraw');
/**
* Clear all of this canvases event handlers (the ones installed by RGraph)
*/
RGraph.ClearEventListeners(this.id);
/**
* Set the current value
*/
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');
/**
* Draw the background
*/
this.DrawBackground();
/**
* Draw the bar that represents the value
*/
this.DrawBar();
/**
* Draw the tickmarks/hatchmarks
*/
this.DrawTickMarks();
/**
* Draw the label
*/
this.DrawLabels();
/**
* Draw the title
*/
if (this.Get('chart.title')) {
this.DrawTitle();
}
/**
* Draw the side title
*/
if (this.Get('chart.title.side')) {
this.DrawSideTitle();
}
/**
* This function enables resizing
*/
if (this.Get('chart.resizable')) {
RGraph.AllowResizing(this);
}
/**
* Setup the context menu if required
*/
if (this.Get('chart.contextmenu')) {
RGraph.ShowContext(this);
}
/**
* If the canvas is annotatable, do install the event handlers
*/
if (this.Get('chart.annotatable')) {
RGraph.Annotate(this);
}
/**
* Instead of using RGraph.common.adjusting.js, handle them here
*/
if (this.Get('chart.adjustable')) {
RGraph.AllowAdjusting(this);
}
/**
* Fire the custom RGraph ondraw event (which should be fired when you have drawn the chart)
*/
RGraph.FireCustomEvent(this, 'ondraw');
}
/**
* Draws the thermometer itself
*/
RGraph.Thermometer.prototype.DrawBackground = function ()
{
var canvas = this.canvas;
var context = this.context;
var bulbRadius = (this.canvas.width - this.gutterLeft - this.gutterRight) / 2;
// Cache the bulbRadius as an object variable
this.bulbRadius = bulbRadius;
// Draw the black background that becomes the border
context.beginPath();
context.fillStyle = 'black';
if (this.Get('chart.shadow')) {
RGraph.SetShadow(this, this.Get('chart.shadow.color'), this.Get('chart.shadow.offsetx'), this.Get('chart.shadow.offsety'), this.Get('chart.shadow.blur'));
}
context.fillRect(this.gutterLeft + 12,this.gutterTop + bulbRadius,RGraph.GetWidth(this) - this.gutterLeft - this.gutterRight - 24, RGraph.GetHeight(this) - this.gutterTop - this.gutterBottom - bulbRadius - bulbRadius);
context.arc(this.gutterLeft + bulbRadius, RGraph.GetHeight(this) - this.gutterBottom - bulbRadius, bulbRadius, 0, 6.28, 0);
context.arc(this.gutterLeft + bulbRadius,this.gutterTop + bulbRadius,(this.canvas.width - this.gutterLeft - this.gutterRight - 24)/ 2,0,6.28,0);
context.fill();
RGraph.NoShadow(this);
// Draw the white inner content background that creates the border
context.beginPath();
context.fillStyle = 'white';
context.fillRect(this.gutterLeft + 12 + 1,this.gutterTop + bulbRadius,RGraph.GetWidth(this) - this.gutterLeft - this.gutterRight - 24 - 2,RGraph.GetHeight(this) - this.gutterTop - this.gutterBottom - bulbRadius - bulbRadius);
context.arc(this.gutterLeft + bulbRadius, RGraph.GetHeight(this) - this.gutterBottom - bulbRadius, bulbRadius - 1, 0, 6.28, 0);
context.arc(this.gutterLeft + bulbRadius,this.gutterTop + bulbRadius,((RGraph.GetWidth(this) - this.gutterLeft - this.gutterRight - 24)/ 2) - 1,0,6.28,0);
context.fill();
// Draw the bottom content of the thermometer
context.beginPath();
context.fillStyle = this.Get('chart.colors')[0];
context.arc(this.gutterLeft + bulbRadius, RGraph.GetHeight(this) - this.gutterBottom - bulbRadius, bulbRadius - 1, 0, 6.28, 0);
context.fillRect(this.gutterLeft + 12 + 1, RGraph.GetHeight(this) - this.gutterBottom - bulbRadius - bulbRadius,RGraph.GetWidth(this) - this.gutterLeft - this.gutterRight - 24 - 2, bulbRadius);
context.fill();
// Save the X/Y/width/height
this.graphArea[0] = this.gutterLeft + 12 + 1;
this.graphArea[1] = this.gutterTop + bulbRadius;
this.graphArea[2] = this.canvas.width - this.gutterLeft - this.gutterRight - 24 - 2;
this.graphArea[3] = (this.canvas.height - this.gutterBottom - bulbRadius - bulbRadius) - (this.graphArea[1]);
}
/**
* This draws the bar that indicates the value of the thermometer
*/
RGraph.Thermometer.prototype.DrawBar = function ()
{
var barHeight = ((this.value - this.min) / (this.max - this.min)) * this.graphArea[3];
var context = this.context;
// Draw the actual bar that indicates the value
context.beginPath();
context.fillStyle = this.Get('chart.colors')[0];
context.fillRect(this.graphArea[0],this.graphArea[1] + this.graphArea[3] - barHeight,this.graphArea[2],barHeight);
context.fill();
this.coords = [this.graphArea[0],this.graphArea[1] + this.graphArea[3] - barHeight,this.graphArea[2],barHeight];
}
/**
* Draws the tickmarks of the thermometer
*/
RGraph.Thermometer.prototype.DrawTickMarks = function ()
{
var ticksize = this.Get('chart.ticksize');
// Left hand side tickmarks
for (var i=this.graphArea[1]; i<=(this.graphArea[1] + this.graphArea[3]); i += (this.graphArea[3] / 10)) {
this.context.beginPath();
this.context.moveTo(this.gutterLeft + 12, i);
this.context.lineTo(this.gutterLeft + 12 + ticksize, i);
this.context.stroke();
}
// Right hand side tickmarks
for (var i=this.graphArea[1]; i<=(this.graphArea[1] + this.graphArea[3]); i += (this.graphArea[3] / 10)) {
this.context.beginPath();
this.context.moveTo(RGraph.GetWidth(this) - (this.gutterRight + 12), i);
this.context.lineTo(RGraph.GetWidth(this) - (this.gutterRight + 12 + ticksize), i);
this.context.stroke();
}
}
/**
* Draws the labels of the thermometer. Now (4th August 2011) draws
* the scale too
*/
RGraph.Thermometer.prototype.DrawLabels = function ()
{
if (this.Get('chart.value.label')) {
this.context.beginPath();
this.context.fillStyle = this.Get('chart.text.color');
var text = this.Get('chart.scale.visible') ? String(this.value) : this.Get('chart.units.pre') + String(this.value) + this.Get('chart.units.post');
RGraph.Text(this.context,this.Get('chart.text.font'),this.Get('chart.text.size'),this.gutterLeft + this.bulbRadius,this.coords[1] + this.Get('chart.text.size'),text,'center','center',true,null,'white');
this.context.fill();
}
/**
* Draw the scale if requested
*/
if (this.Get('chart.scale.visible')) {
this.DrawScale();
}
}
/**
* Draws the title
*/
RGraph.Thermometer.prototype.DrawTitle = function ()
{
this.context.beginPath();
this.context.fillStyle = this.Get('chart.text.color');
RGraph.Text(this.context,this.Get('chart.text.font'),this.Get('chart.text.size') + 2,this.gutterLeft + ((RGraph.GetWidth(this) - this.gutterLeft - this.gutterRight) / 2),this.gutterTop,String(this.Get('chart.title')),'center','center',null,null,null,true);
this.context.fill();
}
/**
* Draws the title
*/
RGraph.Thermometer.prototype.DrawSideTitle = function ()
{
var font = this.Get('chart.title.side.font') ? this.Get('chart.title.side.font') : this.Get('chart.text.font');
var size = this.Get('chart.title.side.size') ? this.Get('chart.title.side.size') : this.Get('chart.text.size') + 2;
this.context.beginPath();
this.context.fillStyle = this.Get('chart.text.color');
RGraph.Text(this.context,
font,
size,
this.gutterLeft * this.Get('chart.title.hpos'),
this.canvas.height / 2,
String(this.Get('chart.title.side')),
'center',
'center',
null,
270,
null,
true);
this.context.fill();
}
/**
* Draw the scale if requested
*/
RGraph.Thermometer.prototype.DrawScale = function ()
{
var numLabels = this.Get('chart.ylabels.count') - 1; // The -1 is so that the number of labels tallies with what is displayed
var step = (this.max - this.min) / numLabels;
this.context.fillStyle = this.Get('chart.text.color');
this.context.beginPath();
for (var i=0; i<=numLabels; ++i) {
var text = this.Get('chart.units.pre') + (i == 0 ? String(this.min.toFixed(this.Get('chart.scale.decimals'))) : String((this.min + (i * step)).toFixed(this.Get('chart.scale.decimals')))) + this.Get('chart.units.post');
var x = this.canvas.width - this.gutterRight;
var y = this.canvas.height - this.gutterBottom - (2 * this.bulbRadius) - ((this.graphArea[3] / numLabels) * i);
RGraph.Text(this.context,
this.Get('chart.text.font'),
this.Get('chart.text.size'),
x - 6,
y,
text,
'center');
}
this.context.fill();
}
|