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