RGraph.cornergauge.js 36 KB

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