RGraph.meter.js 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  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 bar chart constructor
  14. *
  15. * @param string canvas The canvas ID
  16. * @param min integer The minimum value
  17. * @param max integer The maximum value
  18. * @param value integer The indicated value
  19. */
  20. RGraph.Meter = function (id, min, max, value)
  21. {
  22. var tmp = RGraph.getCanvasTag(id);
  23. // Get the canvas and context objects
  24. this.id = tmp[0];
  25. this.canvas = tmp[1];
  26. this.context = this.canvas.getContext ? this.canvas.getContext("2d", {alpha: (typeof id === 'object' && id.alpha === false) ? false : true}) : null;
  27. this.canvas.__object__ = this;
  28. this.type = 'meter';
  29. this.min = min;
  30. this.max = max;
  31. this.value = RGraph.stringsToNumbers(value);
  32. this.centerx = null;
  33. this.centery = null;
  34. this.radius = null;
  35. this.isRGraph = true;
  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. // Various config type stuff
  48. this.properties =
  49. {
  50. 'chart.background.color': 'white',
  51. 'chart.gutter.left': 15,
  52. 'chart.gutter.right': 15,
  53. 'chart.gutter.top': 15,
  54. 'chart.gutter.bottom': 20,
  55. 'chart.linewidth': 1,
  56. 'chart.linewidth.segments': 0,
  57. 'chart.strokestyle': null,
  58. 'chart.border': true,
  59. 'chart.border.color': 'black',
  60. 'chart.text.font': 'Arial',
  61. 'chart.text.size': 10,
  62. 'chart.text.color': 'black',
  63. 'chart.text.valign': 'center',
  64. 'chart.value.text.decimals': 0,
  65. 'chart.value.text.units.pre': '',
  66. 'chart.value.text.units.post': '',
  67. 'chart.title': '',
  68. 'chart.title.background': null,
  69. 'chart.title.hpos': null,
  70. 'chart.title.vpos': null,
  71. 'chart.title.color': 'black',
  72. 'chart.title.bold': true,
  73. 'chart.title.font': null,
  74. 'chart.title.x': null,
  75. 'chart.title.y': null,
  76. 'chart.title.halign': null,
  77. 'chart.title.valign': null,
  78. 'chart.green.start': ((this.max - this.min) * 0.35) + this.min,
  79. 'chart.green.end': this.max,
  80. 'chart.green.color': '#207A20',
  81. 'chart.yellow.start': ((this.max - this.min) * 0.1) + this.min,
  82. 'chart.yellow.end': ((this.max - this.min) * 0.35) + this.min,
  83. 'chart.yellow.color': '#D0AC41',
  84. 'chart.red.start': this.min,
  85. 'chart.red.end': ((this.max - this.min) * 0.1) + this.min,
  86. 'chart.red.color': '#9E1E1E',
  87. 'chart.colors.ranges': null,
  88. 'chart.units.pre': '',
  89. 'chart.units.post': '',
  90. 'chart.contextmenu': null,
  91. 'chart.zoom.factor': 1.5,
  92. 'chart.zoom.fade.in': true,
  93. 'chart.zoom.fade.out': true,
  94. 'chart.zoom.hdir': 'right',
  95. 'chart.zoom.vdir': 'down',
  96. 'chart.zoom.frames': 25,
  97. 'chart.zoom.delay': 16.666,
  98. 'chart.zoom.shadow': true,
  99. 'chart.zoom.background': true,
  100. 'chart.zoom.action': 'zoom',
  101. 'chart.annotatable': false,
  102. 'chart.annotate.color': 'black',
  103. 'chart.shadow': false,
  104. 'chart.shadow.color': 'rgba(0,0,0,0.5)',
  105. 'chart.shadow.blur': 3,
  106. 'chart.shadow.offsetx': 3,
  107. 'chart.shadow.offsety': 3,
  108. 'chart.resizable': false,
  109. 'chart.resize.handle.adjust': [0,0],
  110. 'chart.resize.handle.background': null,
  111. 'chart.tickmarks.small.num': 100,
  112. 'chart.tickmarks.big.num': 10,
  113. 'chart.tickmarks.small.color': '#bbb',
  114. 'chart.tickmarks.big.color': 'black',
  115. 'chart.scale.decimals': 0,
  116. 'chart.scale.point': '.',
  117. 'chart.scale.thousand': ',',
  118. 'chart.radius': null,
  119. 'chart.centerx': null,
  120. 'chart.centery': null,
  121. 'chart.labels': true,
  122. 'chart.segment.radius.start': 0,
  123. 'chart.needle.radius': null,
  124. 'chart.needle.tail': false,
  125. 'chart.needle.head': true,
  126. 'chart.needle.color': 'black',
  127. 'chart.adjustable': false,
  128. 'chart.angles.start': RGraph.PI,
  129. 'chart.angles.end': RGraph.TWOPI,
  130. 'chart.centerpin.stroke': 'black',
  131. 'chart.centerpin.fill': 'white'
  132. }
  133. // Check for support
  134. if (!this.canvas) {
  135. alert('[METER] No canvas support');
  136. return;
  137. }
  138. /*
  139. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  140. * done already
  141. */
  142. if (!this.canvas.__rgraph_aa_translated__) {
  143. this.context.translate(0.5,0.5);
  144. this.canvas.__rgraph_aa_translated__ = true;
  145. }
  146. // Short variable names
  147. var RG = RGraph;
  148. var ca = this.canvas;
  149. var co = ca.getContext('2d');
  150. var prop = this.properties;
  151. var jq = jQuery;
  152. var pa = RG.Path;
  153. var win = window;
  154. var doc = document;
  155. var ma = Math;
  156. /**
  157. * "Decorate" the object with the generic effects if the effects library has been included
  158. */
  159. if (RG.Effects && typeof RG.Effects.decorate === 'function') {
  160. RG.Effects.decorate(this);
  161. }
  162. /**
  163. * A setter
  164. *
  165. * @param name string The name of the property to set
  166. * @param value mixed The value of the property
  167. */
  168. this.set =
  169. this.Set = function (name, value)
  170. {
  171. name = name.toLowerCase();
  172. /**
  173. * This should be done first - prepend the propertyy name with "chart." if necessary
  174. */
  175. if (name.substr(0,6) != 'chart.') {
  176. name = 'chart.' + name;
  177. }
  178. if (name == 'chart.value') {
  179. this.value = value;
  180. return;
  181. }
  182. prop[name] = value;
  183. return this;
  184. };
  185. /**
  186. * A getter
  187. *
  188. * @param name string The name of the property to get
  189. */
  190. this.get =
  191. this.Get = function (name)
  192. {
  193. /**
  194. * This should be done first - prepend the property name with "chart." if necessary
  195. */
  196. if (name.substr(0,6) != 'chart.') {
  197. name = 'chart.' + name;
  198. }
  199. if (name == 'chart.value') {
  200. return this.value;
  201. }
  202. return prop[name];
  203. };
  204. /**
  205. * The function you call to draw the bar chart
  206. */
  207. this.draw =
  208. this.Draw = function ()
  209. {
  210. /**
  211. * Fire the onbeforedraw event
  212. */
  213. RG.FireCustomEvent(this, 'onbeforedraw');
  214. /**
  215. * Constrain the value to be within the min and max
  216. */
  217. if (this.value > this.max) this.value = this.max;
  218. if (this.value < this.min) this.value = this.min;
  219. /**
  220. * Set the current value
  221. */
  222. this.currentValue = this.value;
  223. /**
  224. * This is new in May 2011 and facilitates indiviual gutter settings,
  225. * eg chart.gutter.left
  226. */
  227. this.gutterLeft = prop['chart.gutter.left'];
  228. this.gutterRight = prop['chart.gutter.right'];
  229. this.gutterTop = prop['chart.gutter.top'];
  230. this.gutterBottom = prop['chart.gutter.bottom'];
  231. this.centerx = ((ca.width - this.gutterLeft - this.gutterRight) / 2) + this.gutterLeft;
  232. this.centery = ca.height - this.gutterBottom;
  233. this.radius = Math.min(
  234. (ca.width - this.gutterLeft - this.gutterRight) / 2,
  235. (ca.height - this.gutterTop - this.gutterBottom)
  236. );
  237. /**
  238. * Stop this growing uncontrollably
  239. */
  240. this.coordsText = [];
  241. /**
  242. * Custom centerx, centery and radius
  243. */
  244. if (typeof(prop['chart.centerx']) == 'number') this.centerx = prop['chart.centerx'];
  245. if (typeof(prop['chart.centery']) == 'number') this.centery = prop['chart.centery'];
  246. if (typeof(prop['chart.radius']) == 'number') this.radius = prop['chart.radius'];
  247. /**
  248. * Parse the colors for gradients. Its down here so that the center X/Y can be used
  249. */
  250. if (!this.colorsParsed) {
  251. this.parseColors();
  252. // Don't want to do this again
  253. this.colorsParsed = true;
  254. }
  255. this.DrawBackground();
  256. this.DrawNeedle();
  257. this.DrawLabels();
  258. this.DrawReadout();
  259. /**
  260. * Draw the title
  261. */
  262. RG.DrawTitle(this, prop['chart.title'], this.gutterTop, null, prop['chart.title.size'] ? prop['chart.title.size'] : prop['chart.text.size'] + 2);
  263. /**
  264. * Setup the context menu if required
  265. */
  266. if (prop['chart.contextmenu']) {
  267. RG.ShowContext(this);
  268. }
  269. /**
  270. * This function enables resizing
  271. */
  272. if (prop['chart.resizable']) {
  273. RG.AllowResizing(this);
  274. }
  275. /**
  276. * This installs the event listeners
  277. */
  278. RG.InstallEventListeners(this);
  279. /**
  280. * Fire the onfirstdraw event
  281. */
  282. if (this.firstDraw) {
  283. RG.fireCustomEvent(this, 'onfirstdraw');
  284. this.firstDrawFunc();
  285. this.firstDraw = false;
  286. }
  287. /**
  288. * Fire the RGraph ondraw event
  289. */
  290. RG.FireCustomEvent(this, 'ondraw');
  291. return this;
  292. };
  293. /**
  294. * Draws the background of the chart
  295. */
  296. this.drawBackground =
  297. this.DrawBackground = function ()
  298. {
  299. /**
  300. * Draw the white background
  301. */
  302. co.beginPath();
  303. co.fillStyle = prop['chart.background.color'];
  304. if (prop['chart.shadow']) {
  305. RG.SetShadow(this, prop['chart.shadow.color'],prop['chart.shadow.offsetx'],prop['chart.shadow.offsety'], prop['chart.shadow.blur']);
  306. }
  307. co.moveTo(this.centerx,this.centery);
  308. co.arc(this.centerx,
  309. this.centery,
  310. this.radius,
  311. prop['chart.angles.start'],
  312. prop['chart.angles.end'],
  313. false);
  314. co.fill();
  315. RG.NoShadow(this);
  316. // Draw the shadow
  317. if (prop['chart.shadow']) {
  318. co.beginPath();
  319. var r = (this.radius * 0.06) > 40 ? 40 : (this.radius * 0.06);
  320. co.arc(this.centerx, this.centery, r, 0, RG.TWOPI, 0);
  321. co.fill();
  322. RG.NoShadow(this);
  323. }
  324. // First, draw the grey tickmarks
  325. if (prop['chart.tickmarks.small.num']) {
  326. for (var i=0; i<(prop['chart.angles.end'] - prop['chart.angles.start']); i+=(RG.PI / prop['chart.tickmarks.small.num'])) {
  327. co.beginPath();
  328. co.strokeStyle = prop['chart.tickmarks.small.color'];
  329. co.arc(this.centerx, this.centery, this.radius, prop['chart.angles.start'] + i, prop['chart.angles.start'] + i + 0.00001, 0);
  330. co.arc(this.centerx, this.centery, this.radius - 5, prop['chart.angles.start'] + i, prop['chart.angles.start'] + i + 0.00001, 0);
  331. co.stroke();
  332. }
  333. // Draw the semi-circle that makes the tickmarks
  334. co.beginPath();
  335. co.fillStyle = prop['chart.background.color'];
  336. co.arc(this.centerx, this.centery, this.radius - 4, prop['chart.angles.start'], prop['chart.angles.end'], false);
  337. co.closePath();
  338. co.fill();
  339. }
  340. // Second, draw the darker tickmarks. First run draws them in white to get rid of the existing tickmark,
  341. // then the second run draws them in the requested color
  342. if (prop['chart.tickmarks.big.num']) {
  343. var colors = ['white','white',prop['chart.tickmarks.big.color']];
  344. for (var j=0; j<colors.length; ++j) {
  345. for (var i=0; i<(prop['chart.angles.end'] - prop['chart.angles.start']); i+=((prop['chart.angles.end'] - prop['chart.angles.start']) / prop['chart.tickmarks.big.num'])) {
  346. co.beginPath();
  347. co.strokeStyle = colors[j];
  348. co.arc(this.centerx, this.centery, this.radius, prop['chart.angles.start'] + i, prop['chart.angles.start'] + i + 0.001, 0);
  349. co.arc(this.centerx, this.centery, this.radius - 5, prop['chart.angles.start'] + i, prop['chart.angles.start'] + i + 0.0001, 0);
  350. co.stroke();
  351. }
  352. }
  353. }
  354. // Draw the white circle that makes the tickmarks
  355. co.beginPath();
  356. co.fillStyle = prop['chart.background.color'];
  357. co.moveTo(this.centerx, this.centery);
  358. co.arc(this.centerx, this.centery, this.radius - 7, prop['chart.angles.start'], prop['chart.angles.end'], false);
  359. co.closePath();
  360. co.fill();
  361. /**
  362. * Color ranges - either green/yellow/red or an arbitrary number of ranges
  363. */
  364. var ranges = prop['chart.colors.ranges'];
  365. if (RG.is_array(prop['chart.colors.ranges'])) {
  366. var ranges = prop['chart.colors.ranges'];
  367. for (var i=0; i<ranges.length; ++i) {
  368. co.strokeStyle = prop['chart.strokestyle'] ? prop['chart.strokestyle'] : ranges[i][2];
  369. co.fillStyle = ranges[i][2];
  370. co.lineWidth = prop['chart.linewidth.segments'];
  371. co.beginPath();
  372. co.arc(this.centerx,
  373. this.centery,
  374. this.radius * 0.85,
  375. (((ranges[i][0] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  376. (((ranges[i][1] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  377. false);
  378. if (prop['chart.segment.radius.start'] > 0) {
  379. co.arc(this.centerx,
  380. this.centery,
  381. prop['chart.segment.radius.start'],
  382. (((ranges[i][1] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  383. (((ranges[i][0] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  384. true);
  385. } else {
  386. co.lineTo(this.centerx, this.centery);
  387. }
  388. co.closePath();
  389. co.stroke();
  390. co.fill();
  391. }
  392. // Stops the last line from being changed to a big linewidth.
  393. co.beginPath();
  394. } else {
  395. co.lineWidth = prop['chart.linewidth'];
  396. // Draw the green area
  397. co.strokeStyle = prop['chart.strokestyle'] ? prop['chart.strokestyle'] : prop['chart.green.color'];
  398. co.fillStyle = prop['chart.green.color'];
  399. co.lineWidth = prop['chart.linewidth.segments'];
  400. co.beginPath();
  401. co.arc(this.centerx,
  402. this.centery,
  403. this.radius * 0.85,
  404. (((prop['chart.green.start'] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - this.properties['chart.angles.start'])) + prop['chart.angles.start'],
  405. (((prop['chart.green.end'] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  406. false);
  407. if (prop['chart.segment.radius.start'] > 0) {
  408. co.arc(this.centerx,
  409. this.centery,
  410. prop['chart.segment.radius.start'],
  411. (((prop['chart.green.end'] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  412. (((prop['chart.green.start'] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  413. true);
  414. } else {
  415. co.lineTo(this.centerx, this.centery);
  416. }
  417. co.closePath();
  418. co.stroke();
  419. co.fill();
  420. // Draw the yellow area
  421. co.strokeStyle = prop['chart.strokestyle'] ? prop['chart.strokestyle'] : prop['chart.yellow.color'];
  422. co.fillStyle = prop['chart.yellow.color'];
  423. co.lineWidth = prop['chart.linewidth.segments'];
  424. co.beginPath();
  425. co.arc(this.centerx,
  426. this.centery,
  427. this.radius * 0.85,
  428. (((prop['chart.yellow.start'] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  429. (((prop['chart.yellow.end'] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  430. false);
  431. if (prop['chart.segment.radius.start'] > 0) {
  432. co.arc(this.centerx,
  433. this.centery,
  434. prop['chart.segment.radius.start'],
  435. (((prop['chart.yellow.end'] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  436. (((prop['chart.yellow.start'] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  437. true);
  438. } else {
  439. co.lineTo(this.centerx, this.centery);
  440. }
  441. co.closePath();
  442. co.stroke();
  443. co.fill();
  444. // Draw the red area
  445. co.strokeStyle = prop['chart.strokestyle'] ? prop['chart.strokestyle'] : prop['chart.red.color'];
  446. co.fillStyle = prop['chart.red.color'];
  447. co.lineWidth = prop['chart.linewidth.segments'];
  448. co.beginPath();
  449. co.arc(this.centerx,
  450. this.centery,this.radius * 0.85,
  451. (((prop['chart.red.start'] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  452. (((prop['chart.red.end'] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  453. false);
  454. if (prop['chart.segment.radius.start'] > 0) {
  455. co.arc(this.centerx,
  456. this.centery,
  457. prop['chart.segment.radius.start'],
  458. (((prop['chart.red.end'] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  459. (((prop['chart.red.start'] - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'],
  460. true);
  461. } else {
  462. co.lineTo(this.centerx, this.centery);
  463. }
  464. co.closePath();
  465. co.stroke();
  466. co.fill();
  467. // Revert the linewidth
  468. co.lineWidth = 1;
  469. }
  470. // Draw the outline
  471. if (prop['chart.border']) {
  472. co.strokeStyle = prop['chart.border.color'];
  473. co.lineWidth = prop['chart.linewidth'];
  474. co.beginPath();
  475. co.moveTo(this.centerx, this.centery);
  476. co.arc(this.centerx,
  477. this.centery,
  478. this.radius,
  479. prop['chart.angles.start'],
  480. prop['chart.angles.end'],
  481. false);
  482. co.closePath();
  483. }
  484. co.stroke();
  485. // Reset the linewidth back to 1
  486. co.lineWidth = 1;
  487. };
  488. /**
  489. * Draws the pointer
  490. */
  491. this.drawNeedle =
  492. this.DrawNeedle = function ()
  493. {
  494. // Allow customising the needle radius
  495. var needleRadius = typeof(prop['chart.needle.radius']) == 'number' ? prop['chart.needle.radius'] : this.radius * 0.7;
  496. // First draw the circle at the bottom
  497. co.fillStyle = 'black';
  498. co.lineWidth = this.radius >= 200 ? 7 : 3;
  499. co.lineCap = 'round';
  500. // Now, draw the arm of the needle
  501. co.beginPath();
  502. co.strokeStyle = prop['chart.needle.color'];
  503. if (typeof(prop['chart.needle.linewidth']) == 'number') co.lineWidth = prop['chart.needle.linewidth'];
  504. var a = (((this.value - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'];
  505. co.arc(this.centerx, this.centery, needleRadius, a, a + 0.001, false);
  506. co.lineTo(this.centerx, this.centery);
  507. co.stroke();
  508. // Draw the triangular needle head
  509. if (prop['chart.needle.head']) {
  510. co.fillStyle = prop['chart.needle.color'];
  511. co.beginPath();
  512. co.lineWidth = 1;
  513. //co.moveTo(this.centerx, this.centery);
  514. co.arc(this.centerx, this.centery, needleRadius + 15, a, a + 0.001, 0);
  515. co.arc(this.centerx, this.centery, needleRadius - 15, a + 0.087, a + 0.087999, 0);
  516. co.arc(this.centerx, this.centery, needleRadius - 15, a - 0.087, a - 0.087999, 1);
  517. co.fill();
  518. }
  519. // Draw the tail if requested
  520. if (prop['chart.needle.tail']) {
  521. co.beginPath();
  522. co.strokeStyle = prop['chart.needle.color'];
  523. if (typeof(prop['chart.needle.linewidth']) == 'number') co.lineWidth = prop['chart.needle.linewidth'];
  524. var a = ((this.value - this.min) / (this.max - this.min) * (this.properties['chart.angles.end'] - this.properties['chart.angles.start'])) + this.properties['chart.angles.start'] + RG.PI;
  525. co.arc(this.centerx, this.centery, 25, a, a + 0.001, false);
  526. co.lineTo(this.centerx, this.centery);
  527. co.stroke();
  528. }
  529. // Draw the center circle (the stroke)
  530. var r = (this.radius * 0.06) > 40 ? 40 : (this.radius * 0.06);
  531. co.beginPath();
  532. co.fillStyle = prop['chart.centerpin.stroke'];
  533. co.arc(this.centerx, this.centery, r, 0 + 0.001, RG.TWOPI, 0);
  534. co.fill();
  535. // Draw the centre bit of the circle (the fill)
  536. co.fillStyle = prop['chart.centerpin.fill'];
  537. co.beginPath();
  538. co.arc(this.centerx, this.centery, r - 2, 0 + 0.001, RG.TWOPI, 0);
  539. co.fill();
  540. };
  541. /**
  542. * Draws the labels
  543. */
  544. this.drawLabels =
  545. this.DrawLabels = function ()
  546. {
  547. if (!prop['chart.labels']) {
  548. return;
  549. }
  550. var radius = this.radius;
  551. var text_size = prop['chart.text.size'];
  552. var text_font = prop['chart.text.font'];
  553. var units_post = prop['chart.units.post'];
  554. var units_pre = prop['chart.units.pre'];
  555. var centerx = this.centerx;
  556. var centery = this.centery;
  557. var min = this.min;
  558. var max = this.max;
  559. var decimals = prop['chart.scale.decimals'];
  560. co.fillStyle = prop['chart.text.color'];
  561. co.lineWidth = 1;
  562. co.beginPath();
  563. for (var i=0; i<=10; ++i) {
  564. var angle = ((prop['chart.angles.end'] - prop['chart.angles.start']) * (i / 10)) + prop['chart.angles.start'];
  565. var coords = RG.getRadiusEndPoint(centerx, centery, angle + (((i==0||i==10) && prop['chart.border']) ? (i==0 ? 0.05 : -0.05) : 0), (this.radius * 0.925) - (prop['chart.text.valign'] === 'bottom' ? 15 : 0));
  566. var angleStart = prop['chart.angles.start'];
  567. var angleEnd = prop['chart.angles.end'];
  568. var angleRange = angleEnd - angleStart;
  569. var angleStart_degrees = angleStart * (180 / RG.PI);
  570. var angleEnd_degrees = angleEnd * (180 / RG.PI);
  571. var angleRange_degrees = angleRange * (180 / RG.PI);
  572. // Vertical alignment
  573. valign = prop['chart.text.valign'];
  574. // Horizontal alignment
  575. if (prop['chart.border']) {
  576. if (i == 0) {
  577. halign = 'left';
  578. } else if (i == 10) {
  579. halign = 'right';
  580. } else {
  581. halign = 'center'
  582. }
  583. } else {
  584. halign = 'center';
  585. }
  586. RG.Text2(this, {'font':text_font,
  587. 'size':text_size,
  588. 'x':coords[0],
  589. 'y':coords[1],
  590. 'text':RG.number_format(this, (((this.max - this.min) * (i / 10)) + this.min).toFixed(decimals),units_pre,units_post),
  591. 'halign':halign,
  592. 'valign':valign,
  593. 'angle':((angleRange_degrees * 0.1 * i) + angleStart_degrees) - 270,
  594. 'bounding':false,
  595. 'boundingFill':(i == 0 || i == 10) ? 'white': null,
  596. 'tag': 'scale'
  597. });
  598. }
  599. };
  600. /**
  601. * This function draws the text readout if specified
  602. */
  603. this.drawReadout =
  604. this.DrawReadout = function ()
  605. {
  606. if (prop['chart.value.text']) {
  607. co.beginPath();
  608. RG.Text2(this, {'font':prop['chart.text.font'],
  609. 'size':prop['chart.text.size'],
  610. 'x':this.centerx,
  611. 'y':this.centery - prop['chart.text.size'] - 15,
  612. 'text':prop['chart.value.text.units.pre'] + (this.value).toFixed(prop['chart.value.text.decimals']) + prop['chart.value.text.units.post'],
  613. 'halign':'center',
  614. 'valign':'bottom',
  615. 'bounding':true,
  616. 'boundingFill':'white',
  617. 'tag': 'value.text'
  618. });
  619. co.stroke();
  620. co.fill();
  621. }
  622. };
  623. /**
  624. * A placeholder function
  625. *
  626. * @param object The event object
  627. */
  628. this.getShape = function (e) {};
  629. /**
  630. * This function returns the pertinent value for a particular click (or other mouse event)
  631. *
  632. * @param obj e The event object
  633. */
  634. this.getValue = function (e)
  635. {
  636. var mouseXY = RG.getMouseXY(e);
  637. var angle = RG.getAngleByXY(this.centerx, this.centery, mouseXY[0], mouseXY[1]);
  638. // Work out the radius
  639. var radius = RG.getHypLength(this.centerx, this.centery, mouseXY[0], mouseXY[1]);
  640. if (radius > this.radius) {
  641. return null;
  642. }
  643. if (angle < RG.HALFPI) {
  644. angle += RG.TWOPI;
  645. }
  646. var value = (((angle - prop['chart.angles.start']) / (prop['chart.angles.end'] - prop['chart.angles.start'])) * (this.max - this.min)) + this.min;
  647. value = Math.max(value, this.min);
  648. value = Math.min(value, this.max);
  649. return value;
  650. };
  651. /**
  652. * The getObjectByXY() worker method. Don't call this call:
  653. *
  654. * RGraph.ObjectRegistry.getObjectByXY(e)
  655. *
  656. * @param object e The event object
  657. */
  658. this.getObjectByXY = function (e)
  659. {
  660. var mouseXY = RGraph.getMouseXY(e);
  661. // Work out the radius
  662. var radius = RG.getHypLength(this.centerx, this.centery, mouseXY[0], mouseXY[1]);
  663. if (
  664. mouseXY[0] > (this.centerx - this.radius)
  665. && mouseXY[0] < (this.centerx + this.radius)
  666. && mouseXY[1] > (this.centery - this.radius)
  667. && mouseXY[1] < (this.centery + this.radius)
  668. && radius <= this.radius
  669. ) {
  670. return this;
  671. }
  672. };
  673. /**
  674. * This method handles the adjusting calculation for when the mouse is moved
  675. *
  676. * @param object e The event object
  677. */
  678. this.adjusting_mousemove =
  679. this.Adjusting_mousemove = function (e)
  680. {
  681. /**
  682. * Handle adjusting for the Bar
  683. */
  684. if (prop['chart.adjustable'] && RG.Registry.Get('chart.adjusting') && RG.Registry.Get('chart.adjusting').uid == this.uid) {
  685. this.value = this.getValue(e);
  686. RG.clear(this.canvas);
  687. RG.redrawCanvas(this.canvas);
  688. RG.fireCustomEvent(this, 'onadjust');
  689. }
  690. };
  691. /**
  692. * This method returns the appropriate angle for a value
  693. *
  694. * @param number value The value
  695. */
  696. this.getAngle = function (value)
  697. {
  698. // Higher than max
  699. if (value > this.max || value < this.min) {
  700. return null;
  701. }
  702. var angle = (((value - this.min) / (this.max - this.min)) * (prop['chart.angles.end'] - prop['chart.angles.start'])) + prop['chart.angles.start'];
  703. return angle;
  704. };
  705. /**
  706. * This allows for easy specification of gradients
  707. */
  708. this.parseColors = function ()
  709. {
  710. // Save the original colors so that they can be restored when the canvas is reset
  711. if (this.original_colors.length === 0) {
  712. this.original_colors['chart.green.color'] = RG.array_clone(prop['chart.green.color']);
  713. this.original_colors['chart.yellow.color'] = RG.array_clone(prop['chart.yellow.color']);
  714. this.original_colors['chart.red.color'] = RG.array_clone(prop['chart.red.color']);
  715. this.original_colors['chart.colors.ranges'] = RG.array_clone(prop['chart.colors.ranges']);
  716. }
  717. // Parse the basic colors
  718. prop['chart.green.color'] = this.parseSingleColorForGradient(prop['chart.green.color']);
  719. prop['chart.yellow.color'] = this.parseSingleColorForGradient(prop['chart.yellow.color']);
  720. prop['chart.red.color'] = this.parseSingleColorForGradient(prop['chart.red.color']);
  721. // Parse chart.colors.ranges
  722. var ranges = prop['chart.colors.ranges'];
  723. if (ranges && ranges.length) {
  724. for (var i=0; i<ranges.length; ++i) {
  725. ranges[i][2] = this.parseSingleColorForGradient(ranges[i][2]);
  726. }
  727. }
  728. };
  729. /**
  730. * This parses a single color value
  731. */
  732. this.parseSingleColorForGradient = function (color)
  733. {
  734. if (!color || typeof(color) != 'string') {
  735. return color;
  736. }
  737. if (color.match(/^gradient\((.*)\)$/i)) {
  738. var parts = RegExp.$1.split(':');
  739. // Create the gradient
  740. var grad = co.createRadialGradient(this.centerx, this.centery, prop['chart.segment.radius.start'], this.centerx, this.centery, this.radius * 0.85);
  741. var diff = 1 / (parts.length - 1);
  742. for (var j=0; j<parts.length; ++j) {
  743. grad.addColorStop(j * diff, RG.trim(parts[j]));
  744. }
  745. }
  746. return grad ? grad : color;
  747. };
  748. /**
  749. * Using a function to add events makes it easier to facilitate method chaining
  750. *
  751. * @param string type The type of even to add
  752. * @param function func
  753. */
  754. this.on = function (type, func)
  755. {
  756. if (type.substr(0,2) !== 'on') {
  757. type = 'on' + type;
  758. }
  759. this[type] = func;
  760. return this;
  761. };
  762. /**
  763. * This function runs once only
  764. * (put at the end of the file (before any effects))
  765. */
  766. this.firstDrawFunc = function ()
  767. {
  768. };
  769. /**
  770. * Meter Grow
  771. *
  772. * This effect gradually increases the represented value
  773. *
  774. * @param An object of options - eg: {frames: 60}
  775. * @param function An optional callback function
  776. */
  777. this.grow = function ()
  778. {
  779. var obj = this;
  780. obj.currentValue = obj.currentValue || obj.min;
  781. var opt = arguments[0] || {};
  782. var frames = opt.frames || 30;
  783. var frame = 0;
  784. var diff = obj.value - obj.currentValue;
  785. var step = diff / frames;
  786. var callback = arguments[1] || function () {};
  787. var initial = obj.currentValue;
  788. function iterator ()
  789. {
  790. obj.value = initial + (frame++ * step);
  791. RG.clear(obj.canvas);
  792. RG.redrawCanvas(obj.canvas);
  793. if (frame <= frames) {
  794. RG.Effects.updateCanvas(iterator);
  795. } else {
  796. callback(obj);
  797. }
  798. }
  799. iterator();
  800. return this;
  801. };
  802. /**
  803. * Register the object
  804. */
  805. RG.register(this);
  806. };