RGraph.thermometer.js 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. // version: 2014-06-26
  2. /**
  3. * o--------------------------------------------------------------------------------o
  4. * | This file is part of the RGraph package. RGraph is Free Software, licensed |
  5. * | under the MIT license - so it's free to use for all purposes. If you want to |
  6. * | donate to help keep the project going then you can do so here: |
  7. * | |
  8. * | http://www.rgraph.net/donate |
  9. * o--------------------------------------------------------------------------------o
  10. */
  11. RGraph = window.RGraph || {isRGraph: true};
  12. /**
  13. * The chart constructor. This function sets up the object. It takes the ID (the HTML attribute) of the canvas as the
  14. * first argument and the data as the second. If you need to change this, you can.
  15. *
  16. * NB: If tooltips are ever implemented they must go below the use event listeners!!
  17. *
  18. * @param string id The canvas tag ID
  19. * @param number min The minimum value
  20. * @param number max The maximum value
  21. * @param number value The value reported by the thermometer
  22. */
  23. RGraph.Thermometer = function (id, min, max, value)
  24. {
  25. var tmp = RGraph.getCanvasTag(id);
  26. // Get the canvas and context objects
  27. this.id = tmp[0];
  28. this.canvas = tmp[1];
  29. this.context = this.canvas.getContext ? this.canvas.getContext("2d", {alpha: (typeof id === 'object' && id.alpha === false) ? false : true}) : null;
  30. this.canvas.__object__ = this;
  31. this.uid = RGraph.CreateUID();
  32. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  33. this.colorsParsed = false;
  34. this.type = 'thermometer';
  35. this.isRGraph = true;
  36. this.min = min;
  37. this.max = max;
  38. this.value = RGraph.stringsToNumbers(value);
  39. this.coords = [];
  40. this.graphArea = [];
  41. this.currentValue = null;
  42. this.bulbRadius = 0;
  43. this.bulbTopRadius = 0;
  44. this.bulbTopCenterX = 0
  45. this.bulbTopCenterY = 0;
  46. this.coordsText = [];
  47. this.original_colors = [];
  48. this.firstDraw = true; // After the first draw this will be false
  49. //RGraph.OldBrowserCompat(this.context);
  50. this.properties =
  51. {
  52. 'chart.colors': ['Gradient(#c00:red:#f66:#fcc)'],
  53. 'chart.gutter.left': 15,
  54. 'chart.gutter.right': 15,
  55. 'chart.gutter.top': 15,
  56. 'chart.gutter.bottom': 15,
  57. 'chart.ticksize': 5,
  58. 'chart.text.color': 'black',
  59. 'chart.text.font': 'Arial',
  60. 'chart.text.size': 10,
  61. 'chart.units.pre': '',
  62. 'chart.units.post': '',
  63. 'chart.zoom.factor': 1.5,
  64. 'chart.zoom.fade.in': true,
  65. 'chart.zoom.fade.out': true,
  66. 'chart.zoom.hdir': 'right',
  67. 'chart.zoom.vdir': 'down',
  68. 'chart.zoom.frames': 25,
  69. 'chart.zoom.delay': 16.666,
  70. 'chart.zoom.shadow': true,
  71. 'chart.zoom.background': true,
  72. 'chart.title': '',
  73. 'chart.title.side': '',
  74. 'chart.title.side.bold': true,
  75. 'chart.title.side.font': null,
  76. 'chart.shadow': true,
  77. 'chart.shadow.offsetx': 0,
  78. 'chart.shadow.offsety': 0,
  79. 'chart.shadow.blur': 15,
  80. 'chart.shadow.color': 'gray',
  81. 'chart.resizable': false,
  82. 'chart.contextmenu': null,
  83. 'chart.adjustable': false,
  84. 'chart.value.label': true,
  85. 'chart.value.label.decimals': null,
  86. 'chart.value.label.thousand': ',',
  87. 'chart.value.label.point': '.',
  88. 'chart.labels.count': 5,
  89. 'chart.scale.visible': false,
  90. 'chart.scale.decimals': 0,
  91. 'chart.annotatable': false,
  92. 'chart.annotate.color': 'black',
  93. 'chart.scale.decimals': 0,
  94. 'chart.scale.point': '.',
  95. 'chart.scale.thousand': ',',
  96. 'chart.tooltips': null,
  97. 'chart.tooltips.highlight': true,
  98. 'chart.tooltips.effect': 'fade',
  99. 'chart.tooltips.event': 'onclick',
  100. 'chart.highlight.stroke': 'rgba(0,0,0,0)',
  101. 'chart.highlight.fill': 'rgba(255,255,255,0.7)'
  102. }
  103. /**
  104. * A simple check that the browser has canvas support
  105. */
  106. if (!this.canvas) {
  107. alert('[THERMOMETER] No canvas support');
  108. return;
  109. }
  110. /**
  111. * The thermometer can only have one data point so only this.$0 needs to be created
  112. */
  113. this.$0 = {}
  114. /**
  115. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  116. * done already
  117. */
  118. if (!this.canvas.__rgraph_aa_translated__) {
  119. this.context.translate(0.5,0.5);
  120. this.canvas.__rgraph_aa_translated__ = true;
  121. }
  122. // Short variable names
  123. var RG = RGraph;
  124. var ca = this.canvas;
  125. var co = ca.getContext('2d');
  126. var prop = this.properties;
  127. var jq = jQuery;
  128. var pa = RG.Path;
  129. var win = window;
  130. var doc = document;
  131. var ma = Math;
  132. /**
  133. * "Decorate" the object with the generic effects if the effects library has been included
  134. */
  135. if (RG.Effects && typeof RG.Effects.decorate === 'function') {
  136. RG.Effects.decorate(this);
  137. }
  138. /**
  139. * A setter.
  140. *
  141. * @param name string The name of the property to set
  142. * @param value mixed The value of the property
  143. */
  144. this.set =
  145. this.Set = function (name, value)
  146. {
  147. /**
  148. * This should be done first - prepend the property name with "chart." if necessary
  149. */
  150. if (name.substr(0,6) != 'chart.') {
  151. name = 'chart.' + name;
  152. }
  153. /**
  154. * Change of name
  155. */
  156. if (name == 'chart.ylabels.count') {
  157. name = 'chart.labels.count';
  158. }
  159. prop[name.toLowerCase()] = value;
  160. return this;
  161. };
  162. /**
  163. * A getter.
  164. *
  165. * @param name string The name of the property to get
  166. */
  167. this.get =
  168. this.Get = function (name)
  169. {
  170. /**
  171. * This should be done first - prepend the property name with "chart." if necessary
  172. */
  173. if (name.substr(0,6) != 'chart.') {
  174. name = 'chart.' + name;
  175. }
  176. return prop[name];
  177. };
  178. /**
  179. * Draws the thermometer
  180. */
  181. this.draw =
  182. this.Draw = function ()
  183. {
  184. /**
  185. * Fire the custom RGraph onbeforedraw event (which should be fired before the chart is drawn)
  186. */
  187. RG.FireCustomEvent(this, 'onbeforedraw');
  188. /**
  189. * Parse the colors. This allows for simple gradient syntax
  190. */
  191. if (!this.colorsParsed) {
  192. this.parseColors();
  193. // Don't want to do this again
  194. this.colorsParsed = true;
  195. }
  196. /**
  197. * Set the current value
  198. */
  199. this.currentValue = this.value;
  200. /**
  201. * Stop this growing uncontrollably
  202. */
  203. this.coordsText = [];
  204. /**
  205. * This is new in May 2011 and facilitates indiviual gutter settings,
  206. * eg chart.gutter.left
  207. */
  208. this.gutterLeft = prop['chart.gutter.left'];
  209. this.gutterRight = prop['chart.gutter.right'];
  210. this.gutterTop = prop['chart.gutter.top'];
  211. this.gutterBottom = prop['chart.gutter.bottom'];
  212. /**
  213. * Get the scale
  214. */
  215. this.scale2 = RG.getScale2(this, {
  216. 'max':this.max,
  217. 'min':this.min,
  218. 'strict':true,
  219. 'scale.thousand':prop['chart.scale.thousand'],
  220. 'scale.point':prop['chart.scale.point'],
  221. 'scale.decimals':prop['chart.scale.decimals'],
  222. 'ylabels.count':prop['chart.labels.count'],
  223. 'scale.round':prop['chart.scale.round'],
  224. 'units.pre': prop['chart.units.pre'],
  225. 'units.post': prop['chart.units.post']
  226. });
  227. /**
  228. * Draw the background
  229. */
  230. this.DrawBackground();
  231. /**
  232. * Draw the bar that represents the value
  233. */
  234. this.DrawBar();
  235. /**
  236. * Draw the tickmarks/hatchmarks
  237. */
  238. this.DrawTickMarks();
  239. /**
  240. * Draw the label
  241. */
  242. this.DrawLabels();
  243. /**
  244. * Draw the title
  245. */
  246. if (prop['chart.title']) {
  247. this.DrawTitle();
  248. }
  249. /**
  250. * Draw the side title
  251. */
  252. if (prop['chart.title.side']) {
  253. this.DrawSideTitle();
  254. }
  255. /**
  256. * This function enables resizing
  257. */
  258. if (prop['chart.resizable']) {
  259. RG.AllowResizing(this);
  260. }
  261. /**
  262. * Setup the context menu if required
  263. */
  264. if (prop['chart.contextmenu']) {
  265. RG.ShowContext(this);
  266. }
  267. /**
  268. * This installs the event listeners
  269. */
  270. RG.InstallEventListeners(this);
  271. /**
  272. * Fire the onfirstdraw event
  273. */
  274. if (this.firstDraw) {
  275. RG.fireCustomEvent(this, 'onfirstdraw');
  276. this.firstDrawFunc();
  277. this.firstDraw = false;
  278. }
  279. /**
  280. * Fire the custom RGraph ondraw event (which should be fired when you have drawn the chart)
  281. */
  282. RG.FireCustomEvent(this, 'ondraw');
  283. return this;
  284. };
  285. /**
  286. * Draws the thermometer itself
  287. */
  288. this.drawBackground =
  289. this.DrawBackground = function ()
  290. {
  291. var bulbRadius = (ca.width - this.gutterLeft - this.gutterRight) / 2;
  292. // This is the radius/x/y of the top "semi-circle"
  293. this.bulbTopRadius = (ca.width - this.gutterLeft - this.gutterRight - 24)/ 2
  294. this.bulbTopCenterX = this.gutterLeft + bulbRadius;
  295. this.bulbTopCenterY = this.gutterTop + bulbRadius;
  296. //This is the radius/x/y of the bottom bulb
  297. this.bulbBottomRadius = bulbRadius;
  298. this.bulbBottomCenterX = this.gutterLeft + bulbRadius;
  299. this.bulbBottomCenterY = ca.height - this.gutterBottom - bulbRadius;
  300. // Save the bulbRadius as an object variable
  301. this.bulbRadius = bulbRadius;
  302. // Draw the black background that becomes the border
  303. co.beginPath();
  304. co.fillStyle = 'black';
  305. if (prop['chart.shadow']) {
  306. RG.SetShadow(this, prop['chart.shadow.color'], prop['chart.shadow.offsetx'], prop['chart.shadow.offsety'], prop['chart.shadow.blur']);
  307. }
  308. co.fillRect(this.gutterLeft + 12,this.gutterTop + bulbRadius,ca.width - this.gutterLeft - this.gutterRight - 24, ca.height - this.gutterTop - this.gutterBottom - bulbRadius - bulbRadius);
  309. // Bottom bulb
  310. co.arc(this.bulbBottomCenterX, this.bulbBottomCenterY, bulbRadius, 0, RG.TWOPI, 0);
  311. // Top bulb (which is actually a semi-circle)
  312. co.arc(this.bulbTopCenterX,this.bulbTopCenterY,this.bulbTopRadius,0,RG.TWOPI,0);
  313. co.fill();
  314. // Save the radius of the top semi circle
  315. RG.NoShadow(this);
  316. // Draw the white inner content background that creates the border
  317. co.beginPath();
  318. co.fillStyle = 'white';
  319. co.fillRect(this.gutterLeft + 12 + 1,this.gutterTop + bulbRadius,ca.width - this.gutterLeft - this.gutterRight - 24 - 2,ca.height - this.gutterTop - this.gutterBottom - bulbRadius - bulbRadius);
  320. co.arc(this.gutterLeft + bulbRadius, ca.height - this.gutterBottom - bulbRadius, bulbRadius - 1, 0, RG.TWOPI, 0);
  321. co.arc(this.gutterLeft + bulbRadius,this.gutterTop + bulbRadius,((ca.width - this.gutterLeft - this.gutterRight - 24)/ 2) - 1,0,RG.TWOPI,0);
  322. co.fill();
  323. // Draw the bottom content of the thermometer
  324. co.beginPath();
  325. co.fillStyle = prop['chart.colors'][0];
  326. co.arc(this.gutterLeft + bulbRadius, ca.height - this.gutterBottom - bulbRadius, bulbRadius - 1, 0, RG.TWOPI, 0);
  327. co.rect(this.gutterLeft + 12 + 1, ca.height - this.gutterBottom - bulbRadius - bulbRadius,ca.width - this.gutterLeft - this.gutterRight - 24 - 2, bulbRadius);
  328. co.fill();
  329. // Save the X/Y/width/height
  330. this.graphArea[0] = this.gutterLeft + 12 + 1;
  331. this.graphArea[1] = this.gutterTop + bulbRadius;
  332. this.graphArea[2] = ca.width - this.gutterLeft - this.gutterRight - 24 - 2;
  333. this.graphArea[3] = (ca.height - this.gutterBottom - bulbRadius - bulbRadius) - (this.graphArea[1]);
  334. };
  335. /**
  336. * This draws the bar that indicates the value of the thermometer
  337. */
  338. this.drawBar =
  339. this.DrawBar = function ()
  340. {
  341. var barHeight = ((this.value - this.min) / (this.max - this.min)) * this.graphArea[3];
  342. // Draw the actual bar that indicates the value
  343. co.beginPath();
  344. co.fillStyle = prop['chart.colors'][0];
  345. // This solves an issue with ExCanvas showing a whiite cutout in the chart
  346. if (RGraph.ISOLD) {
  347. co.arc(this.bulbBottomCenterX, this.bulbBottomCenterY, this.bulbBottomRadius - 1, 0, RG.TWOPI, false)
  348. }
  349. co.rect(this.graphArea[0],
  350. this.graphArea[1] + this.graphArea[3] - barHeight,
  351. this.graphArea[2],
  352. barHeight + 2);
  353. co.fill();
  354. this.coords[0] = [this.graphArea[0],this.graphArea[1] + this.graphArea[3] - barHeight,this.graphArea[2],barHeight];
  355. };
  356. /**
  357. * Draws the tickmarks of the thermometer
  358. */
  359. this.drawTickMarks =
  360. this.DrawTickMarks = function ()
  361. {
  362. co.strokeStyle = 'black'
  363. var ticksize = prop['chart.ticksize'];
  364. // Left hand side tickmarks
  365. co.beginPath();
  366. for (var i=this.graphArea[1]; i<=(this.graphArea[1] + this.graphArea[3]); i += (this.graphArea[3] / 10)) {
  367. co.moveTo(this.gutterLeft + 12, Math.round(i));
  368. co.lineTo(this.gutterLeft + 12 + ticksize, Math.round(i));
  369. }
  370. co.stroke();
  371. // Right hand side tickmarks
  372. co.beginPath();
  373. for (var i=this.graphArea[1]; i<=(this.graphArea[1] + this.graphArea[3]); i += (this.graphArea[3] / 10)) {
  374. co.moveTo(ca.width - (this.gutterRight + 12), Math.round(i));
  375. co.lineTo(ca.width - (this.gutterRight + 12 + ticksize), Math.round(i));
  376. }
  377. co.stroke();
  378. };
  379. /**
  380. * Draws the labels of the thermometer. Now (4th August 2011) draws
  381. * the scale too
  382. */
  383. this.drawLabels =
  384. this.DrawLabels = function ()
  385. {
  386. /**
  387. * This draws draws the label that sits at the top of the chart
  388. */
  389. if (prop['chart.value.label']) {
  390. co.fillStyle = prop['chart.text.color'];
  391. // Weird...
  392. var text = prop['chart.scale.visible'] ? RG.number_format(this,
  393. this.value.toFixed(typeof prop['chart.value.label.decimals'] == 'number' ? prop['chart.value.label.decimals'] : prop['chart.scale.decimals'])
  394. )
  395. :
  396. RG.number_format(this,
  397. this.value.toFixed(typeof prop['chart.value.label.decimals'] == 'number' ? prop['chart.value.label.decimals'] : prop['chart.scale.decimals']),
  398. prop['chart.units.pre'],
  399. prop['chart.units.post']
  400. );
  401. RG.Text2(this, {'font': prop['chart.text.font'],
  402. 'size': prop['chart.text.size'],
  403. 'x':this.gutterLeft + this.bulbRadius,
  404. 'y': this.coords[0][1] + 7,
  405. 'text': text,
  406. 'valign':'top',
  407. 'halign':'center',
  408. 'bounding':true,
  409. 'boundingFill':'white',
  410. 'tag': 'value.label'
  411. });
  412. }
  413. /**
  414. * Draw the scale if requested
  415. */
  416. if (prop['chart.scale.visible']) {
  417. this.DrawScale();
  418. }
  419. };
  420. /**
  421. * Draws the title
  422. */
  423. this.drawTitle =
  424. this.DrawTitle = function ()
  425. {
  426. co.fillStyle = prop['chart.text.color'];
  427. RG.Text2(this, {'font': prop['chart.text.font'],
  428. 'size': prop['chart.text.size'] + 2,
  429. 'x':this.gutterLeft + ((ca.width - this.gutterLeft - this.gutterRight) / 2),
  430. 'y': this.gutterTop,
  431. 'text': String(prop['chart.title']),
  432. 'valign':'center',
  433. 'halign':'center',
  434. 'bold':true,
  435. 'tag': 'title'
  436. });
  437. };
  438. /**
  439. * Draws the title
  440. */
  441. this.drawSideTitle =
  442. this.DrawSideTitle = function ()
  443. {
  444. var font = prop['chart.title.side.font'] ? prop['chart.title.side.font'] : prop['chart.text.font'];
  445. var size = prop['chart.title.side.size'] ? prop['chart.title.side.size'] : prop['chart.text.size'] + 2;
  446. co.fillStyle = prop['chart.text.color'];
  447. RG.Text2(this, {'font': font,
  448. 'size': size + 2,
  449. 'x':this.gutterLeft - 3,
  450. 'y': ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop,
  451. 'text': String(prop['chart.title.side']),
  452. 'valign':'center',
  453. 'halign':'center',
  454. 'angle':270,
  455. 'bold':prop['chart.title.side.bold'],
  456. 'tag': 'title.side'
  457. });
  458. };
  459. /**
  460. * Draw the scale if requested
  461. */
  462. this.drawScale =
  463. this.DrawScale = function ()
  464. {
  465. var numLabels = prop['chart.labels.count']; // The -1 is so that the number of labels tallies with what is displayed
  466. var step = (this.max - this.min) / numLabels;
  467. co.fillStyle = prop['chart.text.color'];
  468. var font = prop['chart.text.font'];
  469. var size = prop['chart.text.size'];
  470. var units_pre = prop['chart.units.pre'];
  471. var units_post = prop['chart.units.post'];
  472. var decimals = prop['chart.scale.decimals'];
  473. for (var i=1; i<=numLabels; ++i) {
  474. var x = ca.width - this.gutterRight;
  475. var y = ca.height - this.gutterBottom - (2 * this.bulbRadius) - ((this.graphArea[3] / numLabels) * i);
  476. var text = RG.number_format(this, String((this.min + (i * step)).toFixed(decimals)), units_pre, units_post);
  477. RG.Text2(this, {'font':font,
  478. 'size':size,
  479. 'x':x-6,
  480. 'y':y,
  481. 'text':text,
  482. 'valign':'center',
  483. 'tag': 'scale'
  484. });
  485. }
  486. // Draw zero
  487. RG.Text2(this, {'font':font,
  488. 'size':size,
  489. 'x':x-6,
  490. 'y':ca.height - this.gutterBottom - (2 * this.bulbRadius),
  491. 'text':RG.number_format(this, (this.min).toFixed(decimals),units_pre,units_post),
  492. 'valign':'center',
  493. 'tag': 'scale'
  494. });
  495. };
  496. /**
  497. * Returns the focused/clicked bar
  498. *
  499. * @param event e The event object
  500. */
  501. this.getShape =
  502. this.getBar = function (e)
  503. {
  504. for (var i=0; i<this.coords.length; i++) {
  505. var mouseCoords = RGraph.getMouseXY(e);
  506. var mouseX = mouseCoords[0];
  507. var mouseY = mouseCoords[1];
  508. var left = this.coords[i][0];
  509. var top = this.coords[i][1];
  510. var width = this.coords[i][2];
  511. var height = this.coords[i][3];
  512. if ( (mouseX >= left && mouseX <= (left + width) && mouseY >= top && mouseY <= (top + height + this.bulbBottomRadius)) // The bulbBottomRadius is added as the rect and the bulb don't fully cover the red bit
  513. || RG.getHypLength(this.bulbBottomCenterX, this.bulbBottomCenterY, mouseX, mouseY) <= this.bulbBottomRadius) {
  514. var tooltip = RG.parseTooltipText ? RG.parseTooltipText(prop['chart.tooltips'], i) : '';
  515. return {0: this, 'object': this,
  516. 1: left, 'x': left,
  517. 2: top, 'y': top,
  518. 3: width, 'width': width,
  519. 4: height, 'height': height,
  520. 5: i, 'index': i,
  521. 'tooltip': tooltip
  522. };
  523. }
  524. }
  525. return null;
  526. };
  527. /**
  528. * This function returns the value that the mouse is positioned t, regardless of
  529. * the actual indicated value.
  530. *
  531. * @param object e The event object (or it can also be an two element array containing the X/Y coords)
  532. */
  533. this.getValue = function (arg)
  534. {
  535. if (arg.length == 2) {
  536. var mouseX = arg[0];
  537. var mouseY = arg[1];
  538. } else {
  539. var mouseCoords = RGraph.getMouseXY(arg);
  540. var mouseX = mouseCoords[0];
  541. var mouseY = mouseCoords[1];
  542. }
  543. var value = this.graphArea[3] - (mouseY - this.graphArea[1]);
  544. value = (value / this.graphArea[3]) * (this.max - this.min);
  545. value = value + this.min;
  546. value = Math.max(value, this.min);
  547. value = Math.min(value, this.max);
  548. return value;
  549. };
  550. /**
  551. * Each object type has its own Highlight() function which highlights the appropriate shape
  552. *
  553. * @param object shape The shape to highlight
  554. */
  555. this.highlight =
  556. this.Highlight = function (shape)
  557. {
  558. if (prop['chart.tooltips.highlight']) {
  559. // Add the new highlight
  560. //RGraph.Highlight.Rect(this, shape);
  561. co.beginPath();
  562. co.strokeStyle = prop['chart.highlight.stroke'];
  563. co.fillStyle = prop['chart.highlight.fill'];
  564. co.rect(shape['x'],shape['y'],shape['width'],shape['height'] + this.bulbBottomRadius);
  565. co.arc(this.bulbBottomCenterX, this.bulbBottomCenterY, this.bulbBottomRadius - 1, 0, RG.TWOPI, false);
  566. co.stroke;
  567. co.fill();
  568. }
  569. };
  570. /**
  571. * The getObjectByXY() worker method. Don't call this - call:
  572. *
  573. * RGraph.ObjectRegistry.getObjectByXY(e)
  574. *
  575. * @param object e The event object
  576. */
  577. this.getObjectByXY = function (e)
  578. {
  579. var mouseXY = RGraph.getMouseXY(e);
  580. if (
  581. mouseXY[0] > this.gutterLeft
  582. && mouseXY[0] < (ca.width - this.gutterRight)
  583. && mouseXY[1] >= this.gutterTop
  584. && mouseXY[1] <= (ca.height - this.gutterBottom)
  585. ) {
  586. return this;
  587. }
  588. };
  589. /**
  590. * This method handles the adjusting calculation for when the mouse is moved
  591. *
  592. * @param object e The event object
  593. */
  594. this.adjusting_mousemove =
  595. this.Adjusting_mousemove = function (e)
  596. {
  597. /**
  598. * Handle adjusting for the Thermometer
  599. */
  600. if (prop['chart.adjustable'] && RG.Registry.Get('chart.adjusting') && RG.Registry.Get('chart.adjusting').uid == this.uid) {
  601. var mouseXY = RGraph.getMouseXY(e);
  602. var value = this.getValue(e);
  603. if (typeof(value) == 'number') {
  604. // Fire the onadjust event
  605. RG.FireCustomEvent(this, 'onadjust');
  606. this.value = Number(value.toFixed(prop['chart.scale.decimals']));
  607. RG.redrawCanvas(ca);
  608. }
  609. }
  610. };
  611. /**
  612. * This function positions a tooltip when it is displayed
  613. *
  614. * @param obj object The chart object
  615. * @param int x The X coordinate specified for the tooltip
  616. * @param int y The Y coordinate specified for the tooltip
  617. * @param objec tooltip The tooltips DIV element
  618. */
  619. this.positionTooltip = function (obj, x, y, tooltip, idx)
  620. {
  621. var coordX = obj.coords[tooltip.__index__][0];
  622. var coordY = obj.coords[tooltip.__index__][1];
  623. var coordW = obj.coords[tooltip.__index__][2];
  624. var coordH = obj.coords[tooltip.__index__][3];
  625. var canvasXY = RGraph.getCanvasXY(ca);
  626. var gutterLeft = obj.gutterLeft;
  627. var gutterTop = obj.gutterTop;
  628. var width = tooltip.offsetWidth;
  629. var height = tooltip.offsetHeight;
  630. // Set the top position
  631. tooltip.style.left = 0;
  632. tooltip.style.top = canvasXY[1] + coordY - height - 7 + 'px';
  633. // By default any overflow is hidden
  634. tooltip.style.overflow = '';
  635. // The arrow
  636. var img = new Image();
  637. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
  638. img.style.position = 'absolute';
  639. img.id = '__rgraph_tooltip_pointer__';
  640. img.style.top = (tooltip.offsetHeight - 2) + 'px';
  641. tooltip.appendChild(img);
  642. // Reposition the tooltip if at the edges:
  643. // LEFT edge
  644. if ((canvasXY[0] + coordX - (width / 2)) < 10) {
  645. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + (coordW / 2) + 'px';
  646. img.style.left = ((width * 0.1) - 8.5) + 'px';
  647. // RIGHT edge
  648. } else if ((canvasXY[0] + coordX + (width / 2)) > doc.body.offsetWidth) {
  649. tooltip.style.left = canvasXY[0] + coordX - (width * 0.9) + (coordW / 2) + 'px';
  650. img.style.left = ((width * 0.9) - 8.5) + 'px';
  651. // Default positioning - CENTERED
  652. } else {
  653. tooltip.style.left = (canvasXY[0] + coordX + (coordW / 2) - (width * 0.5)) + 'px';
  654. img.style.left = ((width * 0.5) - 8.5) + 'px';
  655. }
  656. };
  657. /**
  658. * Returns the appropriate Y coord for a value
  659. *
  660. * @param number value The value to return the coord for
  661. */
  662. this.getYCoord = function (value)
  663. {
  664. if (value > this.max || value < this.min) {
  665. return null;
  666. }
  667. var y = (this.graphArea[1] + this.graphArea[3]) - (((value - this.min) / (this.max - this.min)) * this.graphArea[3]);
  668. return y;
  669. };
  670. /**
  671. * This returns true/false as to whether the cursor is over the chart area.
  672. * The cursor does not necessarily have to be over the bar itself.
  673. */
  674. this.overChartArea = function (e)
  675. {
  676. var mouseXY = RG.getMouseXY(e);
  677. var mouseX = mouseXY[0];
  678. var mouseY = mouseXY[1];
  679. // Is the mouse in the "graphArea"?
  680. // ( with a little extra height added)
  681. if ( mouseX >= this.graphArea[0]
  682. && mouseX <= (this.graphArea[0] + this.graphArea[2])
  683. && mouseY >= this.graphArea[1]
  684. && mouseY <= (this.graphArea[1] + this.graphArea[3] + this.bulbRadius)
  685. ) {
  686. return true;
  687. }
  688. // Is the mouse over the bottom bulb?
  689. if (RG.getHypLength(this.bulbBottomCenterX, this.bulbBottomCenterY, mouseX, mouseY) <= this.bulbRadius) {
  690. return true;
  691. }
  692. // Is the mouse over the semi-circle at the top?
  693. if (RG.getHypLength(this.bulbTopCenterX, this.bulbTopCenterY, mouseX, mouseY) <= this.bulbTopRadius) {
  694. return true;
  695. }
  696. return false;
  697. };
  698. /**
  699. * This allows for easy specification of gradients
  700. */
  701. this.parseColors = function ()
  702. {
  703. // Save the original colors so that they can be restored when the canvas is reset
  704. if (this.original_colors.length === 0) {
  705. this.original_colors['chart.colors'] = RG.array_clone(prop['chart.colors']);
  706. }
  707. var colors = prop['chart.colors'];
  708. for (var i=0; i<colors.length; ++i) {
  709. colors[i] = this.parseSingleColorForGradient(colors[i]);
  710. }
  711. };
  712. /**
  713. * This parses a single color value
  714. */
  715. this.parseSingleColorForGradient = function (color)
  716. {
  717. if (!color) {
  718. return color;
  719. }
  720. if (typeof color === 'string' && color.match(/^gradient\((.*)\)$/i)) {
  721. var parts = RegExp.$1.split(':');
  722. // Create the gradient
  723. var grad = co.createLinearGradient(prop['chart.gutter.left'], 0, ca.width - prop['chart.gutter.right'],0);
  724. var diff = 1 / (parts.length - 1);
  725. grad.addColorStop(0, RG.trim(parts[0]));
  726. for (var j=1; j<parts.length; ++j) {
  727. grad.addColorStop(j * diff, RG.trim(parts[j]));
  728. }
  729. }
  730. return grad ? grad : color;
  731. };
  732. /**
  733. * Using a function to add events makes it easier to facilitate method chaining
  734. *
  735. * @param string type The type of even to add
  736. * @param function func
  737. */
  738. this.on = function (type, func)
  739. {
  740. if (type.substr(0,2) !== 'on') {
  741. type = 'on' + type;
  742. }
  743. this[type] = func;
  744. return this;
  745. };
  746. /**
  747. * This function runs once only
  748. * (put at the end of the file (before any effects))
  749. */
  750. this.firstDrawFunc = function ()
  751. {
  752. };
  753. /**
  754. * Gauge Grow
  755. *
  756. * This effect gradually increases the represented value
  757. *
  758. * @param object obj The chart object
  759. * @param Not used - pass null
  760. * @param function An optional callback function
  761. */
  762. this.grow = function ()
  763. {
  764. var obj = this;
  765. var callback = arguments[1] || function () {};
  766. var opt = arguments[0] || {};
  767. var frames = opt.frames ? opt.frames : 30;
  768. var origValue = Number(obj.currentValue);
  769. var newValue = obj.value;
  770. newValue = ma.min(newValue, this.max);
  771. newValue = ma.max(newValue, this.min);
  772. var diff = newValue - origValue;
  773. var step = (diff / frames);
  774. var frame = 0;
  775. function iterate ()
  776. {
  777. // Set the new value
  778. obj.value = (step * frame) + origValue;
  779. RGraph.clear(obj.canvas);
  780. RGraph.redrawCanvas(obj.canvas);
  781. if (frame < frames) {
  782. frame++;
  783. RGraph.Effects.updateCanvas(iterate);
  784. } else {
  785. callback(obj);
  786. }
  787. }
  788. iterate();
  789. return this;
  790. };
  791. /**
  792. * Now, because canvases can support multiple charts, canvases must always be registered
  793. */
  794. RG.Register(this);
  795. };