RGraph.odo.js 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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 odometer constructor. Pass it the ID of the canvas tag, the start value of the odo,
  14. * the end value, and the value that the pointer should point to.
  15. *
  16. * @param string id The ID of the canvas tag
  17. * @param int start The start value of the Odo
  18. * @param int end The end value of the odo
  19. * @param int value The indicated value (what the needle points to)
  20. */
  21. RGraph.Odometer = function (id, start, end, value)
  22. {
  23. var tmp = RGraph.getCanvasTag(id);
  24. // Get the canvas and context objects
  25. this.id = tmp[0];
  26. this.canvas = tmp[1];
  27. this.context = this.canvas.getContext ? this.canvas.getContext("2d", {alpha: (typeof id === 'object' && id.alpha === false) ? false : true}) : null;
  28. this.canvas.__object__ = this;
  29. this.type = 'odo';
  30. this.isRGraph = true;
  31. this.start = start;
  32. this.min = start;
  33. this.end = end;
  34. this.max = end;
  35. this.value = RGraph.stringsToNumbers(value);
  36. this.currentValue = null;
  37. this.uid = RGraph.CreateUID();
  38. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  39. this.colorsParsed = false;
  40. this.coordsText = [];
  41. this.original_colors = [];
  42. this.firstDraw = true; // After the first draw this will be false
  43. /**
  44. * Compatibility with older browsers
  45. */
  46. //RGraph.OldBrowserCompat(this.context);
  47. this.properties =
  48. {
  49. 'chart.background.border': 'black',
  50. 'chart.background.color': '#eee',
  51. 'chart.background.lines.color': '#ddd',
  52. 'chart.centerx': null,
  53. 'chart.centery': null,
  54. 'chart.radius': null,
  55. 'chart.value.text': false,
  56. 'chart.value.text.decimals': 0,
  57. 'chart.needle.color': 'black',
  58. 'chart.needle.width': 2,
  59. 'chart.needle.head': true,
  60. 'chart.needle.tail': true,
  61. 'chart.needle.type': 'pointer',
  62. 'chart.needle.extra': [],
  63. 'chart.needle.triangle.border': '#aaa',
  64. 'chart.text.size': 10,
  65. 'chart.text.color': 'black',
  66. 'chart.text.font': 'Arial',
  67. 'chart.green.max': end * 0.75,
  68. 'chart.red.min': end * 0.9,
  69. 'chart.green.color': 'Gradient(white:#0c0)',
  70. 'chart.yellow.color': 'Gradient(white:#ff0)',
  71. 'chart.red.color': 'Gradient(white:#f00)',
  72. 'chart.label.area': 35,
  73. 'chart.gutter.left': 25,
  74. 'chart.gutter.right': 25,
  75. 'chart.gutter.top': 25,
  76. 'chart.gutter.bottom': 25,
  77. 'chart.title': '',
  78. 'chart.title.background': null,
  79. 'chart.title.hpos': null,
  80. 'chart.title.vpos': null,
  81. 'chart.title.font': null,
  82. 'chart.title.bold': true,
  83. 'chart.title.x': null,
  84. 'chart.title.y': null,
  85. 'chart.title.halign': null,
  86. 'chart.title.valign': null,
  87. 'chart.contextmenu': null,
  88. 'chart.linewidth': 1,
  89. 'chart.shadow.inner': false,
  90. 'chart.shadow.inner.color': 'black',
  91. 'chart.shadow.inner.offsetx': 3,
  92. 'chart.shadow.inner.offsety': 3,
  93. 'chart.shadow.inner.blur': 6,
  94. 'chart.shadow.outer': false,
  95. 'chart.shadow.outer.color': 'black',
  96. 'chart.shadow.outer.offsetx': 3,
  97. 'chart.shadow.outer.offsety': 3,
  98. 'chart.shadow.outer.blur': 6,
  99. 'chart.annotatable': false,
  100. 'chart.annotate.color': 'black',
  101. 'chart.scale.decimals': 0,
  102. 'chart.scale.point': '.',
  103. 'chart.scale.thousand': ',',
  104. 'chart.zoom.factor': 1.5,
  105. 'chart.zoom.fade.in': true,
  106. 'chart.zoom.fade.out': true,
  107. 'chart.zoom.hdir': 'right',
  108. 'chart.zoom.vdir': 'down',
  109. 'chart.zoom.frames': 25,
  110. 'chart.zoom.delay': 16.666,
  111. 'chart.zoom.shadow': true,
  112. 'chart.zoom.background': true,
  113. 'chart.zoom.action': 'zoom',
  114. 'chart.resizable': false,
  115. 'chart.resize.handle.adjust': [0,0],
  116. 'chart.resize.handle.background': null,
  117. 'chart.units.pre': '',
  118. 'chart.units.post': '',
  119. 'chart.border': false,
  120. 'chart.border.color1': '#BEBCB0',
  121. 'chart.border.color2': '#F0EFEA',
  122. 'chart.border.color3': '#BEBCB0',
  123. 'chart.tickmarks': true,
  124. 'chart.tickmarks.highlighted': false,
  125. 'chart.tickmarks.big.color': '#999',
  126. 'chart.zerostart': false,
  127. 'chart.labels': null,
  128. 'chart.units.pre': '',
  129. 'chart.units.post': '',
  130. 'chart.value.units.pre': '',
  131. 'chart.value.units.post': '',
  132. 'chart.key': null,
  133. 'chart.key.background': 'white',
  134. 'chart.key.position': 'graph',
  135. 'chart.key.shadow': false,
  136. 'chart.key.shadow.color': '#666',
  137. 'chart.key.shadow.blur': 3,
  138. 'chart.key.shadow.offsetx': 2,
  139. 'chart.key.shadow.offsety': 2,
  140. 'chart.key.position.gutter.boxed':false,
  141. 'chart.key.position.x': null,
  142. 'chart.key.position.y': null,
  143. 'chart.key.halign': 'right',
  144. 'chart.key.color.shape': 'square',
  145. 'chart.key.rounded': true,
  146. 'chart.key.text.size': 10,
  147. 'chart.key.colors': null,
  148. 'chart.key.text.color': 'black',
  149. 'chart.adjustable': false
  150. }
  151. /*
  152. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  153. * done already
  154. */
  155. if (!this.canvas.__rgraph_aa_translated__) {
  156. this.context.translate(0.5,0.5);
  157. this.canvas.__rgraph_aa_translated__ = true;
  158. }
  159. // Short variable names
  160. var RG = RGraph;
  161. var ca = this.canvas;
  162. var co = ca.getContext('2d');
  163. var prop = this.properties;
  164. var jq = jQuery;
  165. var pa = RG.Path;
  166. var win = window;
  167. var doc = document;
  168. var ma = Math;
  169. /**
  170. * "Decorate" the object with the generic effects if the effects library has been included
  171. */
  172. if (RG.Effects && typeof RG.Effects.decorate === 'function') {
  173. RG.Effects.decorate(this);
  174. }
  175. /**
  176. * A peudo setter
  177. *
  178. * @param name string The name of the property to set
  179. * @param value mixed The value of the property
  180. */
  181. this.set =
  182. this.Set = function (name, value)
  183. {
  184. name = name.toLowerCase();
  185. /**
  186. * This should be done first - prepend the property name with "chart." if necessary
  187. */
  188. if (name.substr(0,6) != 'chart.') {
  189. name = 'chart.' + name;
  190. }
  191. if (name == 'chart.needle.style') {
  192. alert('[RGRAPH] The RGraph property chart.needle.style has changed to chart.needle.color');
  193. }
  194. if (name == 'chart.needle.thickness') {
  195. name = 'chart.needle.width';
  196. }
  197. if (name == 'chart.value') {
  198. this.value = value;
  199. return;
  200. }
  201. prop[name] = value;
  202. return this;
  203. };
  204. /**
  205. * A getter
  206. *
  207. * @param name string The name of the property to get
  208. */
  209. this.get =
  210. this.Get = function (name)
  211. {
  212. /**
  213. * This should be done first - prepend the property name with "chart." if necessary
  214. */
  215. if (name.substr(0,6) != 'chart.') {
  216. name = 'chart.' + name;
  217. }
  218. if (name == 'chart.value') {
  219. return this.value;
  220. }
  221. return prop[name.toLowerCase()];
  222. };
  223. /**
  224. * Draws the odometer
  225. */
  226. this.draw =
  227. this.Draw = function ()
  228. {
  229. /**
  230. * Fire the onbeforedraw event
  231. */
  232. RG.FireCustomEvent(this, 'onbeforedraw');
  233. /**
  234. * Set the current value
  235. */
  236. this.currentValue = this.value;
  237. /**
  238. * No longer allow values outside the range of the Odo
  239. */
  240. if (this.value > this.end) {
  241. this.value = this.end;
  242. }
  243. if (this.value < this.start) {
  244. this.value = this.start;
  245. }
  246. /**
  247. * This is new in May 2011 and facilitates indiviual gutter settings,
  248. * eg chart.gutter.left
  249. */
  250. this.gutterLeft = prop['chart.gutter.left'];
  251. this.gutterRight = prop['chart.gutter.right'];
  252. this.gutterTop = prop['chart.gutter.top'];
  253. this.gutterBottom = prop['chart.gutter.bottom'];
  254. // Work out a few things
  255. this.radius = Math.min(
  256. (ca.width - this.gutterLeft - this.gutterRight) / 2,
  257. (ca.height - this.gutterTop - this.gutterBottom) / 2
  258. )
  259. - (prop['chart.border'] ? 25 : 0);
  260. this.diameter = 2 * this.radius;
  261. this.centerx = ((ca.width - this.gutterLeft- this.gutterRight) / 2) + this.gutterLeft;
  262. this.centery = ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop;
  263. this.range = this.end - this.start;
  264. this.coordsText = [];
  265. /**
  266. * Move the centerx if the key is defined
  267. */
  268. if (prop['chart.key'] && prop['chart.key'].length > 0 && ca.width > ca.height) this.centerx = 5 + this.radius;
  269. if (typeof(prop['chart.centerx']) == 'number') this.centerx = prop['chart.centerx'];
  270. if (typeof(prop['chart.centery']) == 'number') this.centery = prop['chart.centery'];
  271. /**
  272. * Allow custom setting of the radius
  273. */
  274. if (typeof(prop['chart.radius']) == 'number') {
  275. this.radius = prop['chart.radius'];
  276. if (prop['chart.border']) {
  277. this.radius -= 25;
  278. }
  279. }
  280. /**
  281. * Parse the colors for gradients. Its down here so that the center X/Y can be used
  282. */
  283. if (!this.colorsParsed) {
  284. this.parseColors();
  285. // Don't want to do this again
  286. this.colorsParsed = true;
  287. }
  288. co.lineWidth = prop['chart.linewidth'];
  289. // Draw the background
  290. this.DrawBackground();
  291. // And lastly, draw the labels
  292. this.DrawLabels();
  293. // Draw the needle
  294. this.DrawNeedle(this.value, prop['chart.needle.color']);
  295. /**
  296. * Draw any extra needles
  297. */
  298. if (prop['chart.needle.extra'].length > 0) {
  299. for (var i=0; i<prop['chart.needle.extra'].length; ++i) {
  300. var needle = prop['chart.needle.extra'][i];
  301. this.DrawNeedle(needle[0], needle[1], needle[2]);
  302. }
  303. }
  304. /**
  305. * Draw the key if requested
  306. */
  307. if (prop['chart.key'] && prop['chart.key'].length > 0) {
  308. // Build a colors array out of the needle colors
  309. var colors = [prop['chart.needle.color']];
  310. if (prop['chart.needle.extra'].length > 0) {
  311. for (var i=0; i<prop['chart.needle.extra'].length; ++i) {
  312. var needle = prop['chart.needle.extra'][i];
  313. colors.push(needle[1]);
  314. }
  315. }
  316. RG.DrawKey(this, prop['chart.key'], colors);
  317. }
  318. /**
  319. * Setup the context menu if required
  320. */
  321. if (prop['chart.contextmenu']) {
  322. RG.ShowContext(this);
  323. }
  324. /**
  325. * This function enables resizing
  326. */
  327. if (prop['chart.resizable']) {
  328. RG.AllowResizing(this);
  329. }
  330. /**
  331. * This installs the event listeners
  332. */
  333. RG.InstallEventListeners(this);
  334. /**
  335. * Fire the onfirstdraw event
  336. */
  337. if (this.firstDraw) {
  338. RG.fireCustomEvent(this, 'onfirstdraw');
  339. this.firstDrawFunc();
  340. this.firstDraw = false;
  341. }
  342. /**
  343. * Fire the RGraph ondraw event
  344. */
  345. RG.FireCustomEvent(this, 'ondraw');
  346. return this;
  347. };
  348. /**
  349. * Draws the background
  350. */
  351. this.drawBackground =
  352. this.DrawBackground = function ()
  353. {
  354. co.beginPath();
  355. /**
  356. * Turn on the shadow if need be
  357. */
  358. if (prop['chart.shadow.outer']) {
  359. RG.setShadow(this, prop['chart.shadow.outer.color'], prop['chart.shadow.outer.offsetx'], prop['chart.shadow.outer.offsety'], prop['chart.shadow.outer.blur']);
  360. }
  361. var backgroundColor = prop['chart.background.color'];
  362. // Draw the grey border
  363. co.fillStyle = backgroundColor;
  364. co.arc(this.centerx, this.centery, this.radius, 0.0001, RG.TWOPI, false);
  365. co.fill();
  366. /**
  367. * Turn off the shadow
  368. */
  369. RG.noShadow(this);
  370. // Draw a circle
  371. co.strokeStyle = '#666';
  372. co.arc(this.centerx, this.centery, this.radius, 0, RG.TWOPI, false);
  373. // Now draw a big white circle to make the lines appear as tick marks
  374. // This is solely for Chrome
  375. co.fillStyle = backgroundColor;
  376. co.arc(this.centerx, this.centery, this.radius, 0, RG.TWOPI, false);
  377. co.fill();
  378. /**
  379. * Draw more tickmarks
  380. */
  381. if (prop['chart.tickmarks']) {
  382. co.beginPath();
  383. co.strokeStyle = '#bbb';
  384. for (var i=0; i<=360; i+=3) {
  385. co.arc(this.centerx, this.centery, this.radius, 0, i / 57.3, false);
  386. co.lineTo(this.centerx, this.centery);
  387. }
  388. co.stroke();
  389. }
  390. co.beginPath();
  391. co.lineWidth = 1;
  392. co.strokeStyle = 'black';
  393. // Now draw a big white circle to make the lines appear as tick marks
  394. co.fillStyle = backgroundColor;
  395. co.strokeStyle = backgroundColor;
  396. co.arc(this.centerx, this.centery, this.radius - 5, 0, RG.TWOPI, false);
  397. co.fill();
  398. co.stroke();
  399. // Gray lines at 18 degree intervals
  400. co.beginPath();
  401. co.strokeStyle = prop['chart.background.lines.color'];
  402. for (var i=0; i<360; i+=18) {
  403. co.arc(this.centerx, this.centery, this.radius, 0, RG.degrees2Radians(i), false);
  404. co.lineTo(this.centerx, this.centery);
  405. }
  406. co.stroke();
  407. // Redraw the outer circle
  408. co.beginPath();
  409. co.strokeStyle = prop['chart.background.border'];
  410. co.arc(this.centerx, this.centery, this.radius, 0, RG.TWOPI, false);
  411. co.stroke();
  412. /**
  413. * Now draw the center bits shadow if need be
  414. */
  415. if (prop['chart.shadow.inner']) {
  416. co.beginPath();
  417. RG.SetShadow(this, prop['chart.shadow.inner.color'], prop['chart.shadow.inner.offsetx'], prop['chart.shadow.inner.offsety'], prop['chart.shadow.inner.blur']);
  418. co.arc(this.centerx, this.centery, this.radius - prop['chart.label.area'], 0, RG.TWOPI, 0);
  419. co.fill();
  420. co.stroke();
  421. /**
  422. * Turn off the shadow
  423. */
  424. RG.NoShadow(this);
  425. }
  426. /*******************************************************
  427. * Draw the green area
  428. *******************************************************/
  429. var greengrad = prop['chart.green.color'];
  430. // Draw the "tick highlight"
  431. if (prop['chart.tickmarks.highlighted']) {
  432. co.beginPath();
  433. co.lineWidth = 5;
  434. co.strokeStyle = greengrad;
  435. co.arc(this.centerx, this.centery, this.radius - 2.5,
  436. -1 * RG.HALFPI,
  437. (((prop['chart.green.max'] - this.start)/ (this.end - this.start)) * RG.TWOPI) - RG.HALFPI,
  438. 0);
  439. co.stroke();
  440. co.lineWidth = 1;
  441. }
  442. co.beginPath();
  443. co.fillStyle = greengrad;
  444. co.arc(
  445. this.centerx,
  446. this.centery,
  447. this.radius - prop['chart.label.area'],
  448. 0 - RG.HALFPI,
  449. (((prop['chart.green.max'] - this.start)/ (this.end - this.start)) * RG.TWOPI) - RG.HALFPI,
  450. false
  451. );
  452. co.lineTo(this.centerx, this.centery);
  453. co.closePath();
  454. co.fill();
  455. /*******************************************************
  456. * Draw the yellow area
  457. *******************************************************/
  458. var yellowgrad = prop['chart.yellow.color'];
  459. // Draw the "tick highlight"
  460. if (prop['chart.tickmarks.highlighted']) {
  461. co.beginPath();
  462. co.lineWidth = 5;
  463. co.strokeStyle = yellowgrad;
  464. co.arc(this.centerx, this.centery, this.radius - 2.5, (
  465. ((prop['chart.green.max'] - this.start) / (this.end - this.start)) * RG.TWOPI) - RG.HALFPI,
  466. (((prop['chart.red.min'] - this.start) / (this.end - this.start)) * RG.TWOPI) - RG.HALFPI,
  467. 0);
  468. co.stroke();
  469. co.lineWidth = 1;
  470. }
  471. co.beginPath();
  472. co.fillStyle = yellowgrad;
  473. co.arc(
  474. this.centerx,
  475. this.centery,
  476. this.radius - prop['chart.label.area'],
  477. ( ((prop['chart.green.max'] - this.start) / (this.end - this.start)) * RG.TWOPI) - RG.HALFPI,
  478. ( ((prop['chart.red.min'] - this.start) / (this.end - this.start)) * RG.TWOPI) - RG.HALFPI,
  479. false
  480. );
  481. co.lineTo(this.centerx, this.centery);
  482. co.closePath();
  483. co.fill();
  484. /*******************************************************
  485. * Draw the red area
  486. *******************************************************/
  487. var redgrad = prop['chart.red.color'];
  488. // Draw the "tick highlight"
  489. if (prop['chart.tickmarks.highlighted']) {
  490. co.beginPath();
  491. co.lineWidth = 5;
  492. co.strokeStyle = redgrad;
  493. co.arc(this.centerx, this.centery, this.radius - 2.5,(((prop['chart.red.min'] - this.start) / (this.end - this.start)) * RG.TWOPI) - RG.HALFPI,RG.TWOPI - RG.HALFPI,0);
  494. co.stroke();
  495. co.lineWidth = 1;
  496. }
  497. co.beginPath();
  498. co.fillStyle = redgrad;
  499. co.strokeStyle = redgrad;
  500. co.arc(
  501. this.centerx,
  502. this.centery,
  503. this.radius - prop['chart.label.area'],
  504. (((prop['chart.red.min'] - this.start) / (this.end - this.start)) * RG.TWOPI) - RG.HALFPI,
  505. RG.TWOPI - RG.HALFPI,
  506. false
  507. );
  508. co.lineTo(this.centerx, this.centery);
  509. co.closePath();
  510. co.fill();
  511. /**
  512. * Draw the thick border
  513. */
  514. if (prop['chart.border']) {
  515. var grad = co.createRadialGradient(this.centerx, this.centery, this.radius, this.centerx, this.centery, this.radius + 20);
  516. grad.addColorStop(0, prop['chart.border.color1']);
  517. grad.addColorStop(0.5, prop['chart.border.color2']);
  518. grad.addColorStop(1, prop['chart.border.color3']);
  519. co.beginPath();
  520. co.fillStyle = grad;
  521. co.strokeStyle = 'rgba(0,0,0,0)'
  522. co.lineWidth = 0.001;
  523. co.arc(this.centerx, this.centery, this.radius + 20, 0, RG.TWOPI, 0);
  524. co.arc(this.centerx, this.centery, this.radius - 2, RG.TWOPI, 0, 1);
  525. co.fill();
  526. }
  527. // Put the linewidth back to what it was
  528. co.lineWidth = prop['chart.linewidth'];
  529. /**
  530. * Draw the title if specified
  531. */
  532. if (prop['chart.title']) {
  533. RG.DrawTitle(this,
  534. prop['chart.title'],
  535. this.centery - this.radius,
  536. null,
  537. prop['chart.title.size'] ? prop['chart.title.size'] : prop['chart.text.size'] + 2);
  538. }
  539. // Draw the big tick marks
  540. if (!prop['chart.tickmarks.highlighted']) {
  541. for (var i=18; i<=360; i+=36) {
  542. co.beginPath();
  543. co.strokeStyle = prop['chart.tickmarks.big.color'];
  544. co.lineWidth = 2;
  545. co.arc(this.centerx, this.centery, this.radius - 1, RG.degrees2Radians(i), RG.degrees2Radians(i+0.01), false);
  546. co.arc(this.centerx, this.centery, this.radius - 7, RG.degrees2Radians(i), RG.degrees2Radians(i+0.01), false);
  547. co.stroke();
  548. }
  549. }
  550. };
  551. /**
  552. * Draws the needle of the odometer
  553. *
  554. * @param number value The value to represent
  555. * @param string color The color of the needle
  556. * @param number The OPTIONAL length of the needle
  557. */
  558. this.drawNeedle =
  559. this.DrawNeedle = function (value, color)
  560. {
  561. // The optional length of the needle
  562. var length = arguments[2] ? arguments[2] : this.radius - prop['chart.label.area'];
  563. // ===== First draw a grey background circle =====
  564. co.fillStyle = '#999';
  565. co.beginPath();
  566. co.moveTo(this.centerx, this.centery);
  567. co.arc(this.centerx, this.centery, 10, 0, RG.TWOPI, false);
  568. co.fill();
  569. co.closePath();
  570. co.fill();
  571. // ===============================================
  572. co.fillStyle = color
  573. co.strokeStyle = '#666';
  574. // Draw the centre bit
  575. co.beginPath();
  576. co.moveTo(this.centerx, this.centery);
  577. co.arc(this.centerx, this.centery, 8, 0, RG.TWOPI, false);
  578. co.fill();
  579. co.closePath();
  580. co.stroke();
  581. co.fill();
  582. if (prop['chart.needle.type'] == 'pointer') {
  583. co.strokeStyle = color;
  584. co.lineWidth = prop['chart.needle.width'];
  585. co.lineCap = 'round';
  586. co.lineJoin = 'round';
  587. // Draw the needle
  588. co.beginPath();
  589. // The trailing bit on the opposite side of the dial
  590. co.beginPath();
  591. co.moveTo(this.centerx, this.centery);
  592. if (prop['chart.needle.tail']) {
  593. co.arc(this.centerx,
  594. this.centery,
  595. 20,
  596. (((value / this.range) * 360) + 90) / (180 / RG.PI),
  597. (((value / this.range) * 360) + 90 + 0.01) / (180 / RG.PI), // The 0.01 avoids a bug in ExCanvas and Chrome 6
  598. false
  599. );
  600. }
  601. // Draw the long bit on the opposite side
  602. co.arc(this.centerx,
  603. this.centery,
  604. length - 10,
  605. (((value / this.range) * 360) - 90) / (180 / RG.PI),
  606. (((value / this.range) * 360) - 90 + 0.1 ) / (180 / RG.PI), // The 0.1 avoids a bug in ExCanvas and Chrome 6
  607. false
  608. );
  609. co.closePath();
  610. //co.stroke();
  611. //co.fill();
  612. } else if (prop['chart.needle.type'] == 'triangle') {
  613. co.lineWidth = 0.01;
  614. co.lineEnd = 'square';
  615. co.lineJoin = 'miter';
  616. /**
  617. * This draws the version of the pointer that becomes the border
  618. */
  619. co.beginPath();
  620. co.fillStyle = prop['chart.needle.triangle.border'];
  621. co.arc(this.centerx, this.centery, 11, (((value / this.range) * 360)) / 57.3, ((((value / this.range) * 360)) + 0.01) / 57.3, 0);
  622. co.arc(this.centerx, this.centery, 11, (((value / this.range) * 360) + 180) / 57.3, ((((value / this.range) * 360) + 180) + 0.01)/ 57.3, 0);
  623. co.arc(this.centerx, this.centery, length - 5, (((value / this.range) * 360) - 90) / 57.3, ((((value / this.range) * 360) - 90) / 57.3) + 0.01, 0);
  624. co.closePath();
  625. co.fill();
  626. co.beginPath();
  627. co.arc(this.centerx, this.centery, 15, 0, RG.TWOPI, 0);
  628. co.closePath();
  629. co.fill();
  630. // This draws the pointer
  631. co.beginPath();
  632. co.strokeStyle = 'black';
  633. co.fillStyle = color;
  634. co.arc(this.centerx, this.centery, 7, (((value / this.range) * 360)) / 57.3, ((((value / this.range) * 360)) + 0.01) / 57.3, 0);
  635. co.arc(this.centerx, this.centery, 7, (((value / this.range) * 360) + 180) / 57.3, ((((value / this.range) * 360) + 180) + 0.01)/ 57.3, 0);
  636. co.arc(this.centerx, this.centery, length - 13, (((value / this.range) * 360) - 90) / 57.3, ((((value / this.range) * 360) - 90) / 57.3) + 0.01, 0);
  637. co.closePath();
  638. co.stroke();
  639. co.fill();
  640. /**
  641. * This is here to accomodate the MSIE/ExCanvas combo
  642. */
  643. co.beginPath();
  644. co.arc(this.centerx, this.centery, 7, 0, RG.TWOPI, 0);
  645. co.closePath();
  646. co.fill();
  647. }
  648. co.stroke();
  649. co.fill();
  650. // Draw the mini center circle
  651. co.beginPath();
  652. co.fillStyle = color;
  653. co.arc(this.centerx, this.centery, prop['chart.needle.type'] == 'pointer' ? 7 : 12, 0.01, RG.TWOPI, false);
  654. co.fill();
  655. // This draws the arrow at the end of the line
  656. if (prop['chart.needle.head'] && prop['chart.needle.type'] == 'pointer') {
  657. co.lineWidth = 1;
  658. co.fillStyle = color;
  659. // round, bevel, miter
  660. co.lineJoin = 'miter';
  661. co.lineCap = 'butt';
  662. co.beginPath();
  663. co.arc(this.centerx, this.centery, length - 5, (((value / this.range) * 360) - 90) / 57.3, (((value / this.range) * 360) - 90 + 0.1) / 57.3, false);
  664. co.arc(this.centerx,
  665. this.centery,
  666. length - 20,
  667. RG.degrees2Radians( ((value / this.range) * 360) - (length < 60 ? 80 : 85) ),
  668. RG.degrees2Radians( ((value / this.range) * 360) - (length < 60 ? 100 : 95) ),
  669. 1);
  670. co.closePath();
  671. co.fill();
  672. //co.stroke();
  673. }
  674. /**
  675. * Draw a white circle at the centre
  676. */
  677. co.beginPath();
  678. co.fillStyle = 'gray';
  679. co.moveTo(this.centerx, this.centery);
  680. co.arc(this.centerx,this.centery,2,0,6.2795,false);
  681. co.closePath();
  682. co.fill();
  683. };
  684. /**
  685. * Draws the labels for the Odo
  686. */
  687. this.drawLabels =
  688. this.DrawLabels = function ()
  689. {
  690. var size = prop['chart.text.size'];
  691. var font = prop['chart.text.font'];
  692. var centerx = this.centerx;
  693. var centery = this.centery;
  694. var r = this.radius - (prop['chart.label.area'] / 2);
  695. var start = this.start;
  696. var end = this.end;
  697. var decimals = prop['chart.scale.decimals'];
  698. var labels = prop['chart.labels'];
  699. var units_pre = prop['chart.units.pre'];
  700. var units_post = prop['chart.units.post'];
  701. co.beginPath();
  702. co.fillStyle = prop['chart.text.color'];
  703. /**
  704. * If labels are specified, use those
  705. */
  706. if (labels) {
  707. for (var i=0; i<labels.length; ++i) {
  708. RG.Text2(this, {'font':font,
  709. 'size':size,
  710. 'x':centerx + (Math.cos(((i / labels.length) * RG.TWOPI) - RG.HALFPI) * (this.radius - (prop['chart.label.area'] / 2) ) ), // Sin A = Opp / Hyp
  711. 'y':centery + (Math.sin(((i / labels.length) * RG.TWOPI) - RG.HALFPI) * (this.radius - (prop['chart.label.area'] / 2) ) ), // Cos A = Adj / Hyp
  712. 'text': String(labels[i]),
  713. 'valign':'center',
  714. 'halign':'center',
  715. 'tag': 'labels'
  716. });
  717. }
  718. /**
  719. * If not, use the maximum value
  720. */
  721. } else {
  722. RG.Text2(this, {'font':font,'size':size,'x':centerx + (0.588 * r ),'y':centery - (0.809 * r ),'text':RG.number_format(this, (((end - start) * (1/10)) + start).toFixed(decimals), units_pre, units_post),'halign':'center','valign':'center','angle':36,'tag': 'scale'});
  723. RG.Text2(this, {'font':font,'size':size,'x':centerx + (0.951 * r ),'y':centery - (0.309 * r),'text':RG.number_format(this, (((end - start) * (2/10)) + start).toFixed(decimals), units_pre, units_post),'halign':'center','valign':'center','angle':72,'tag': 'scale'});
  724. RG.Text2(this, {'font':font,'size':size,'x':centerx + (0.949 * r),'y':centery + (0.31 * r),'text':RG.number_format(this, (((end - start) * (3/10)) + start).toFixed(decimals), units_pre, units_post),'halign':'center','valign':'center','angle':108,'tag': 'scale'});
  725. RG.Text2(this, {'font':font,'size':size,'x':centerx + (0.588 * r ),'y':centery + (0.809 * r ),'text':RG.number_format(this, (((end - start) * (4/10)) + start).toFixed(decimals), units_pre, units_post),'halign':'center','valign':'center','angle':144,'tag': 'scale'});
  726. RG.Text2(this, {'font':font,'size':size,'x':centerx,'y':centery + r,'text':RG.number_format(this, (((end - start) * (5/10)) + start).toFixed(decimals),units_pre, units_post),'halign':'center','valign':'center','angle':180,'tag': 'scale'});
  727. RG.Text2(this, {'font':font,'size':size,'x':centerx - (0.588 * r ),'y':centery + (0.809 * r ),'text':RG.number_format(this, (((end - start) * (6/10)) + start).toFixed(decimals), units_pre, units_post),'halign':'center','valign':'center','angle':216,'tag': 'scale'});
  728. RG.Text2(this, {'font':font,'size':size,'x':centerx - (0.949 * r),'y':centery + (0.300 * r),'text':RG.number_format(this, (((end - start) * (7/10)) + start).toFixed(decimals), units_pre, units_post),'halign':'center','valign':'center','angle':252,'tag': 'scale'});
  729. RG.Text2(this, {'font':font,'size':size,'x':centerx - (0.951 * r),'y':centery - (0.309 * r),'text':RG.number_format(this, (((end - start) * (8/10)) + start).toFixed(decimals), units_pre, units_post),'halign':'center','valign':'center','angle':288,'tag': 'scale'});
  730. RG.Text2(this, {'font':font,'size':size,'x':centerx - (0.588 * r ),'y':centery - (0.809 * r ),'text':RG.number_format(this, (((end - start) * (9/10)) + start).toFixed(decimals), units_pre, units_post),'halign':'center','valign':'center','angle':324,'tag': 'scale'});
  731. RG.Text2(this, {'font':font,'size':size,'x':centerx,'y':centery - r,'text': prop['chart.zerostart'] ? RG.number_format(this, this.start.toFixed(decimals), units_pre, units_post) : RG.number_format(this, (((end - start) * (10/10)) + start).toFixed(decimals), units_pre, units_post),'halign':'center','valign':'center','tag': 'scale'});
  732. }
  733. co.fill();
  734. /**
  735. * Draw the text label below the center point
  736. */
  737. if (prop['chart.value.text']) {
  738. co.strokeStyle = 'black';
  739. RG.Text2(this, {'font':font,
  740. 'size':size+2,
  741. 'x':centerx,
  742. 'y':centery + size + 15,
  743. 'text':String(prop['chart.value.units.pre'] + this.value.toFixed(prop['chart.value.text.decimals']) + prop['chart.value.units.post']),
  744. 'halign':'center',
  745. 'valign':'center',
  746. 'bounding':true,
  747. 'boundingFill':'white',
  748. 'tag': 'value.text'
  749. });
  750. }
  751. };
  752. /**
  753. * A placeholder function
  754. *
  755. * @param object The event object
  756. */
  757. this.getShape = function (e) {};
  758. /**
  759. * This function returns the pertinent value at the point of click
  760. *
  761. * @param object The event object
  762. */
  763. this.getValue = function (e)
  764. {
  765. var mouseXY = RG.getMouseXY(e)
  766. var angle = RG.getAngleByXY(this.centerx, this.centery, mouseXY[0], mouseXY[1]);
  767. angle += RG.HALFPI;
  768. if (mouseXY[0] >= this.centerx && mouseXY[1] <= this.centery) {
  769. angle -= RG.TWOPI;
  770. }
  771. var value = ((angle / RG.TWOPI) * (this.max - this.min)) + this.min;
  772. return value;
  773. };
  774. /**
  775. * The getObjectByXY() worker method. Don't call this call:
  776. *
  777. * RGraph.ObjectRegistry.getObjectByXY(e)
  778. *
  779. * @param object e The event object
  780. */
  781. this.getObjectByXY = function (e)
  782. {
  783. var mouseXY = RG.getMouseXY(e);
  784. var radius = RG.getHypLength(this.centerx, this.centery, mouseXY[0], mouseXY[1]);
  785. if (
  786. mouseXY[0] > (this.centerx - this.radius)
  787. && mouseXY[0] < (this.centerx + this.radius)
  788. && mouseXY[1] > (this.centery - this.radius)
  789. && mouseXY[1] < (this.centery + this.radius)
  790. && radius <= this.radius
  791. ) {
  792. return this;
  793. }
  794. };
  795. /**
  796. * This method handles the adjusting calculation for when the mouse is moved
  797. *
  798. * @param object e The event object
  799. */
  800. this.adjusting_mousemove =
  801. this.Adjusting_mousemove = function (e)
  802. {
  803. /**
  804. * Handle adjusting for the Bar
  805. */
  806. if (prop['chart.adjustable'] && RG.Registry.Get('chart.adjusting') && RG.Registry.Get('chart.adjusting').uid == this.uid) {
  807. this.value = this.getValue(e);
  808. RG.clear(ca);
  809. RG.redrawCanvas(ca);
  810. RG.fireCustomEvent(this, 'onadjust');
  811. }
  812. };
  813. /**
  814. * This method returns the appropriate angle for a value
  815. *
  816. * @param number value The value
  817. */
  818. this.getAngle = function (value)
  819. {
  820. // Higher than max or lower than min
  821. if (value > this.max || value < this.min) {
  822. return null;
  823. }
  824. var angle = (((value - this.min) / (this.max - this.min)) * RG.TWOPI);
  825. angle -= RG.HALFPI;
  826. return angle;
  827. };
  828. /**
  829. * This allows for easy specification of gradients
  830. */
  831. this.parseColors = function ()
  832. {
  833. // Save the original colors so that they can be restored when the canvas is reset
  834. if (this.original_colors.length === 0) {
  835. this.original_colors['chart.green.color'] = RG.array_clone(prop['chart.green.color']);
  836. this.original_colors['chart.yellow.color'] = RG.array_clone(prop['chart.yellow.color']);
  837. this.original_colors['chart.red.color'] = RG.array_clone(prop['chart.red.color']);
  838. }
  839. // Parse the basic colors
  840. prop['chart.green.color'] = this.parseSingleColorForGradient(prop['chart.green.color']);
  841. prop['chart.yellow.color'] = this.parseSingleColorForGradient(prop['chart.yellow.color']);
  842. prop['chart.red.color'] = this.parseSingleColorForGradient(prop['chart.red.color']);
  843. };
  844. /**
  845. * This parses a single color value
  846. */
  847. this.parseSingleColorForGradient = function (color)
  848. {
  849. if (!color || typeof(color) != 'string') {
  850. return color;
  851. }
  852. if (color.match(/^gradient\((.*)\)$/i)) {
  853. var parts = RegExp.$1.split(':');
  854. // Create the gradient
  855. var grad = co.createRadialGradient(this.centerx, this.centery, 0, this.centerx, this.centery, this.radius);
  856. var diff = 1 / (parts.length - 1);
  857. grad.addColorStop(0, RG.trim(parts[0]));
  858. for (var j=1; j<parts.length; ++j) {
  859. grad.addColorStop(j * diff, RG.trim(parts[j]));
  860. }
  861. }
  862. return grad ? grad : color;
  863. };
  864. /**
  865. * Using a function to add events makes it easier to facilitate method chaining
  866. *
  867. * @param string type The type of even to add
  868. * @param function func
  869. */
  870. this.on = function (type, func)
  871. {
  872. if (type.substr(0,2) !== 'on') {
  873. type = 'on' + type;
  874. }
  875. this[type] = func;
  876. return this;
  877. };
  878. /**
  879. * This function runs once only
  880. * (put at the end of the file (before any effects))
  881. */
  882. this.firstDrawFunc = function ()
  883. {
  884. };
  885. /**
  886. * Odo Grow
  887. *
  888. * This effect gradually increases the represented value
  889. *
  890. * @param An object of effect properties - eg: {frames: 30}
  891. * @param function An optional callback function
  892. */
  893. this.grow = function ()
  894. {
  895. var obj = this;
  896. var opt = arguments[0] || {};
  897. var frames = opt.frames || 30;
  898. var frame = 0;
  899. var current = obj.currentValue || 0;
  900. var origValue = Number(obj.currentValue);
  901. var newValue = obj.value;
  902. var diff = newValue - origValue;
  903. var step = (diff / frames);
  904. var callback = arguments[1] || function () {};
  905. function iterator ()
  906. {
  907. obj.value = origValue + (frame * step);
  908. RG.clear(obj.canvas);
  909. RG.redrawCanvas(obj.canvas);
  910. if (frame++ < frames) {
  911. RG.Effects.updateCanvas(iterator);
  912. } else {
  913. callback(obj);
  914. }
  915. }
  916. iterator();
  917. return this;
  918. };
  919. /**
  920. * Register the object
  921. */
  922. RG.register(this);
  923. };