RGraph.thermometer.js 32 KB

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