RGraph.fuel.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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 Fuel widget constructor
  14. *
  15. * @param object canvas The canvas object
  16. * @param int min The minimum value
  17. * @param int max The maximum value
  18. * @param int value The indicated value
  19. */
  20. RGraph.Fuel = 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 = 'fuel';
  29. this.isRGraph = true;
  30. this.min = min;
  31. this.max = max;
  32. this.value = RGraph.stringsToNumbers(value);
  33. this.angles = {};
  34. this.currentValue = null;
  35. this.uid = RGraph.CreateUID();
  36. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  37. this.coordsText = [];
  38. this.original_colors = [];
  39. this.firstDraw = true; // After the first draw this will be false
  40. /**
  41. * Compatibility with older browsers
  42. */
  43. //RGraph.OldBrowserCompat(this.context);
  44. // Check for support
  45. if (!this.canvas) {
  46. alert('[FUEL] No canvas support');
  47. return;
  48. }
  49. /**
  50. * The funnel charts properties
  51. */
  52. this.properties =
  53. {
  54. 'chart.colors': ['Gradient(white:red)'],
  55. 'chart.needle.color': 'red',
  56. 'chart.gutter.left': 5,
  57. 'chart.gutter.right': 5,
  58. 'chart.gutter.top': 5,
  59. 'chart.gutter.bottom': 5,
  60. 'chart.text.size': 10,
  61. 'chart.text.color': 'black', // Does not support gradients
  62. 'chart.text.font': 'Arial',
  63. 'chart.contextmenu': null,
  64. 'chart.annotatable': false,
  65. 'chart.annotate.color': 'black',
  66. 'chart.zoom.factor': 1.5,
  67. 'chart.zoom.fade.in': true,
  68. 'chart.zoom.fade.out': true,
  69. 'chart.zoom.factor': 1.5,
  70. 'chart.zoom.fade.in': true,
  71. 'chart.zoom.fade.out': true,
  72. 'chart.zoom.hdir': 'right',
  73. 'chart.zoom.vdir': 'down',
  74. 'chart.zoom.frames': 25,
  75. 'chart.zoom.delay': 16.666,
  76. 'chart.zoom.shadow': true,
  77. 'chart.zoom.background': true,
  78. 'chart.zoom.action': 'zoom',
  79. 'chart.adjustable': false,
  80. 'chart.resizable': false,
  81. 'chart.resize.handle.background': null,
  82. 'chart.icon': 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAfCAYAAAD0ma06AAAEGElEQVRIS7VXSyhtYRT+jnfe5FEMjAwUBiQGHikzRWIkkgy8YyDK+xnJK5JCeZSUGKBMiAyYkMxMJAMpSfJ+2/d8695/33NunSPnHqt2Z5+91/9/' + '/' + '/et9a/1b8Pn56dmMBhg/IWDgwNoNzc38PHxkXtN0+Tiexp9eH18fIDj1Bj63N/fw8vLS/wsmcHoqKmXT09PuL29RVFREU5OTvTJ6UIAgioQ+vLe09MTb29v8PX1RWBgICYnJ+XXIqDRWXN0dJT3nIDsWlpadP+lpSWZlD4KmL/8/' + '/7+Ls/S09N1/7y8PISHh+sK/QssDJWcHEyGCnB1dRUDAwPIzMzUx5GpAnZ1dcXy8jK2trbM5j06OsLc3JzISx8q4OzsLOOsAq6treHg4AAeHh4WJbq7u0Nzc7P+PiYmBnt7ezg9PcXExAQCAgLg5OSEx8dHuLu7Wwfc3t7G/v6+yEcjO8rIROGKaWdnZ+jr6zMDjI6OxvT0tDzr6uqS2KtksspwZ2cHjY2NuqSUhnHmilUCraysmElaWloKJpQCjI2NRX5+Pl5eXr6WlCv08/MTEMVOZDH+Zzw4CdlfX1/rDHt7ezE1NQXGkcYEKi4ulkVKYlpLGouBs/JiaGgIZL25uSlecXFxohAz/ccAz8/P4e/vj7q6Ojw8PMje5DNRy94MQ0JCUFtbK2wqKipE+sHBQbi4uPwMQ86ak5ODxMREVFdXIywsDCUlJRJDXnZlmJqaip6eHuTm5kqikGlycjIyMjL+ZrY9JSUgMzQiIgINDQ2ypaqqqkCZWXHsnjQEHB8fR0pKigAxabq7uyWOlJNxtLukTJDs7GxUVlZKDNl5oqKi8Pr6+jOAIyMjiI+Pl5JGQG4F1Qy+LN7f3fiUdGZmBsHBwRgbG8Pw8LD01ba2NmlX0rTtnTQLCwvSjEdHR3FxcSExLCwsRGRkpBR9vePzeMDyw3bT1NT0XXLiT4a7u7s4Pj4GGzd7K8GCgoKEsRR8I4Cm6hwHXV5eiv62GAE5npMTmFuBTCkzmzT7qs5Q9TlW/o6ODlvwhCHPM5SVPZIxYzNeXFxEa2srvL29YTC2GI3aMm3Zeq6urv4LMC0tDRsbG1K8k5KS9DgS0IwhKVFjSsJA22r9/f0oKCgQdvPz83JEmZ2dlcpD9maSshow0KZnlO8Csx9yK3BLKCMJPpf2xGMigdi9WXooaWdn53dxdP+amhrZh4eHh1hfX5cTW319vZyBnp+ffzNkBWBmhYaGysB/j322oCckJCArK0uGMlsJ5ubmBoPxRiMzFlomjr2MGdne3i5ANILRJEtJt6ysTG8h9gDl4am8vFwSUWron1O9LulXIOqk9pWftfdSS40yyj5Uh101wPRryuR7R1ZMX/U1pfy5IF40xcgUnGAc9wsGYxsFhy87kwAAAABJRU5ErkJggg==',
  83. 'chart.icon.redraw': true,
  84. 'chart.background.image.stretch': false,
  85. 'chart.background.image.x': null,
  86. 'chart.background.image.y': null,
  87. 'chart.labels.full': 'F',
  88. 'chart.labels.empty': 'E',
  89. 'chart.labels.count': 5,
  90. 'chart.centerx': null,
  91. 'chart.centery': null,
  92. 'chart.radius': null,
  93. 'chart.scale.visible': false,
  94. 'chart.scale.decimals': 0,
  95. 'chart.units.pre': '',
  96. 'chart.units.post': ''
  97. }
  98. /**
  99. * Bounds checking - if the value is outside the scale
  100. */
  101. if (this.value > this.max) this.value = this.max;
  102. if (this.value < this.min) this.value = this.min;
  103. /*
  104. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  105. * done already
  106. */
  107. if (!this.canvas.__rgraph_aa_translated__) {
  108. this.context.translate(0.5,0.5);
  109. this.canvas.__rgraph_aa_translated__ = true;
  110. }
  111. // Short variable names
  112. var RG = RGraph;
  113. var ca = this.canvas;
  114. var co = ca.getContext('2d');
  115. var prop = this.properties;
  116. var jq = jQuery;
  117. var pa = RG.Path;
  118. var win = window;
  119. var doc = document;
  120. var ma = Math;
  121. /**
  122. * "Decorate" the object with the generic effects if the effects library has been included
  123. */
  124. if (RG.Effects && typeof RG.Effects.decorate === 'function') {
  125. RG.Effects.decorate(this);
  126. }
  127. /**
  128. * A setter
  129. *
  130. * @param name string The name of the property to set
  131. * @param value mixed The value of the property
  132. */
  133. this.set =
  134. this.Set = function (name, value)
  135. {
  136. name = name.toLowerCase();
  137. /**
  138. * This should be done first - prepend the propertyy name with "chart." if necessary
  139. */
  140. if (name.substr(0,6) != 'chart.') {
  141. name = 'chart.' + name;
  142. }
  143. prop[name] = value;
  144. return this;
  145. };
  146. /**
  147. * A getter
  148. *
  149. * @param name string The name of the property to get
  150. */
  151. this.get =
  152. this.Get = function (name)
  153. {
  154. /**
  155. * This should be done first - prepend the property name with "chart." if necessary
  156. */
  157. if (name.substr(0,6) != 'chart.') {
  158. name = 'chart.' + name;
  159. }
  160. return prop[name.toLowerCase()];
  161. };
  162. /**
  163. * The function you call to draw the bar chart
  164. */
  165. this.draw =
  166. this.Draw = function ()
  167. {
  168. /**
  169. * Fire the onbeforedraw event
  170. */
  171. RG.FireCustomEvent(this, 'onbeforedraw');
  172. /**
  173. * Set the current value
  174. */
  175. this.currentValue = this.value;
  176. /**
  177. * This is new in May 2011 and facilitates indiviual gutter settings,
  178. * eg chart.gutter.left
  179. */
  180. this.gutterLeft = prop['chart.gutter.left'];
  181. this.gutterRight = prop['chart.gutter.right'];
  182. this.gutterTop = prop['chart.gutter.top'];
  183. this.gutterBottom = prop['chart.gutter.bottom'];
  184. /**
  185. * Get the center X and Y of the chart. This is the center of the needle bulb
  186. */
  187. this.centerx = ((ca.width - this.gutterLeft - this.gutterRight) / 2) + this.gutterLeft;
  188. this.centery = ca.height - 20 - this.gutterBottom
  189. /**
  190. * Work out the radius of the chart
  191. */
  192. this.radius = ca.height - this.gutterTop - this.gutterBottom - 20;
  193. /**
  194. * Stop this growing uncntrollably
  195. */
  196. this.coordsText = [];
  197. /**
  198. * You can now specify chart.centerx, chart.centery and chart.radius
  199. */
  200. if (typeof(prop['chart.centerx']) == 'number') this.centerx = prop['chart.centerx'];
  201. if (typeof(prop['chart.centery']) == 'number') this.centery = prop['chart.centery'];
  202. if (typeof(prop['chart.radius']) == 'number') this.radius = prop['chart.radius'];
  203. /**
  204. * Parse the colors. This allows for simple gradient syntax
  205. */
  206. if (!this.colorsParsed) {
  207. this.parseColors();
  208. // Don't want to do this again
  209. this.colorsParsed = true;
  210. }
  211. /**
  212. * The start and end angles of the chart
  213. */
  214. this.angles.start = (RG.PI + RG.HALFPI) - 0.5;
  215. this.angles.end = (RG.PI + RG.HALFPI) + 0.5;
  216. this.angles.needle = this.getAngle(this.value);
  217. /**
  218. * Draw the labels on the chart
  219. */
  220. this.DrawLabels();
  221. /**
  222. * Draw the fuel guage
  223. */
  224. this.DrawChart();
  225. /**
  226. * Setup the context menu if required
  227. */
  228. if (prop['chart.contextmenu']) {
  229. RG.ShowContext(this);
  230. }
  231. /**
  232. * This function enables resizing
  233. */
  234. if (prop['chart.resizable']) {
  235. RG.AllowResizing(this);
  236. }
  237. /**
  238. * This installs the event listeners
  239. */
  240. RG.InstallEventListeners(this);
  241. /**
  242. * Fire the onfirstdraw event
  243. */
  244. if (this.firstDraw) {
  245. RG.fireCustomEvent(this, 'onfirstdraw');
  246. this.firstDrawFunc();
  247. this.firstDraw = false;
  248. }
  249. /**
  250. * Fire the RGraph ondraw event
  251. */
  252. RG.FireCustomEvent(this, 'ondraw');
  253. return this;
  254. };
  255. /**
  256. * This function actually draws the chart
  257. */
  258. this.drawChart =
  259. this.DrawChart = function ()
  260. {
  261. /**
  262. * Draw the "Scale"
  263. */
  264. this.DrawScale();
  265. /**
  266. * Place the icon on the canvas
  267. */
  268. if (!RG.ISOLD) {
  269. this.DrawIcon();
  270. }
  271. /**
  272. * Draw the needle
  273. */
  274. this.DrawNeedle();
  275. };
  276. /**
  277. * Draws the labels
  278. */
  279. this.drawLabels =
  280. this.DrawLabels = function ()
  281. {
  282. if (!prop['chart.scale.visible']) {
  283. var radius = (this.radius - 20);
  284. co.fillStyle = prop['chart.text.color'];
  285. // Draw the left label
  286. var y = this.centery - Math.sin(this.angles.start - RG.PI) * (this.radius - 25);
  287. var x = this.centerx - Math.cos(this.angles.start - RG.PI) * (this.radius - 25);
  288. RG.Text2(this, {'font':prop['chart.text.font'],
  289. 'size':prop['chart.text.size'],
  290. 'x':x,
  291. 'y':y,
  292. 'text':prop['chart.labels.empty'],
  293. 'halign': 'center',
  294. 'valign':'center',
  295. 'tag': 'labels'
  296. });
  297. // Draw the right label
  298. var y = this.centery - Math.sin(this.angles.start - RG.PI) * (this.radius - 25);
  299. var x = this.centerx + Math.cos(this.angles.start - RG.PI) * (this.radius - 25);
  300. RG.Text2(this, {'font':prop['chart.text.font'],
  301. 'size':prop['chart.text.size'],
  302. 'x':x,
  303. 'y':y,
  304. 'text':prop['chart.labels.full'],
  305. 'halign': 'center',
  306. 'valign':'center',
  307. 'tag': 'labels'
  308. });
  309. }
  310. };
  311. /**
  312. * Draws the needle
  313. */
  314. this.drawNeedle =
  315. this.DrawNeedle = function ()
  316. {
  317. // Draw the actual needle
  318. co.beginPath();
  319. co.lineWidth = 5;
  320. co.lineCap = 'round';
  321. co.strokeStyle = prop['chart.needle.color'];
  322. /**
  323. * The angle for the needle
  324. */
  325. var angle = this.angles.needle;
  326. co.arc(this.centerx, this.centery, this.radius - 30, angle, angle + 0.0001, false);
  327. co.lineTo(this.centerx, this.centery);
  328. co.stroke();
  329. co.lineWidth = 1;
  330. // Create the gradient for the bulb
  331. var cx = this.centerx + 10;
  332. var cy = this.centery - 10
  333. var grad = co.createRadialGradient(cx, cy, 35, cx, cy, 0);
  334. grad.addColorStop(0, 'black');
  335. grad.addColorStop(1, '#eee');
  336. if (navigator.userAgent.indexOf('Firefox/6.0') > 0) {
  337. grad = co.createLinearGradient(cx + 10, cy - 10, cx - 10, cy + 10);
  338. grad.addColorStop(1, '#666');
  339. grad.addColorStop(0.5, '#ccc');
  340. }
  341. // Draw the bulb
  342. co.beginPath();
  343. co.fillStyle = grad;
  344. co.moveTo(this.centerx, this.centery);
  345. co.arc(this.centerx, this.centery, 20, 0, RG.TWOPI, 0);
  346. co.fill();
  347. };
  348. /**
  349. * Draws the "scale"
  350. */
  351. this.drawScale =
  352. this.DrawScale = function ()
  353. {
  354. var a, x, y;
  355. //First draw the fill background
  356. co.beginPath();
  357. co.strokeStyle = 'black';
  358. co.fillStyle = 'white';
  359. co.arc(this.centerx, this.centery, this.radius, this.angles.start, this.angles.end, false);
  360. co.arc(this.centerx, this.centery, this.radius - 10, this.angles.end, this.angles.start, true);
  361. co.closePath();
  362. co.stroke();
  363. co.fill();
  364. //First draw the fill itself
  365. var start = this.angles.start;
  366. var end = this.angles.needle;
  367. co.beginPath();
  368. co.fillStyle = prop['chart.colors'][0];
  369. co.arc(this.centerx, this.centery, this.radius, start, end, false);
  370. co.arc(this.centerx, this.centery, this.radius - 10, end, start, true);
  371. co.closePath();
  372. //co.stroke();
  373. co.fill();
  374. // This draws the tickmarks
  375. for (a = this.angles.start; a<=this.angles.end+0.01; a+=((this.angles.end - this.angles.start) / 5)) {
  376. co.beginPath();
  377. co.arc(this.centerx, this.centery, this.radius - 10, a, a + 0.0001, false);
  378. co.arc(this.centerx, this.centery, this.radius - 15, a + 0.0001, a, true);
  379. co.stroke();
  380. }
  381. /**
  382. * If chart.scale.visible is specified draw the textual scale
  383. */
  384. if (prop['chart.scale.visible']) {
  385. co.fillStyle = prop['chart.text.color'];
  386. // The labels
  387. var numLabels = prop['chart.labels.count'];
  388. var decimals = prop['chart.scale.decimals'];
  389. var font = prop['chart.text.font'];
  390. var size = prop['chart.text.size'];
  391. var units_post = prop['chart.units.post'];
  392. var units_pre = prop['chart.units.pre'];
  393. for (var i=0; i<=numLabels; ++i) {
  394. a = ((this.angles.end - this.angles.start) * (i/numLabels)) + this.angles.start;
  395. y = this.centery - Math.sin(a - RG.PI) * (this.radius - 25);
  396. x = this.centerx - Math.cos(a - RG.PI) * (this.radius - 25);
  397. RG.Text2(this, {'font':font,
  398. 'size':size,
  399. 'x':x,
  400. 'y':y,
  401. 'text': RG.number_format(this, (this.min + ((this.max - this.min) * (i/numLabels))).toFixed(decimals),units_pre,units_post),
  402. 'halign': 'center',
  403. 'valign':'center',
  404. 'tag': 'scale'
  405. });
  406. }
  407. }
  408. };
  409. /**
  410. * A placeholder function that is here to prevent errors
  411. */
  412. this.getShape = function (e) {};
  413. /**
  414. * This function returns the pertinent value based on a click
  415. *
  416. * @param object e An event object
  417. * @return number The relevant value at the point of click
  418. */
  419. this.getValue = function (e)
  420. {
  421. var mouseXY = RG.getMouseXY(e);
  422. var angle = RG.getAngleByXY(this.centerx, this.centery, mouseXY[0], mouseXY[1]);
  423. /**
  424. * Boundary checking
  425. */
  426. if (angle >= this.angles.end) {
  427. return this.max;
  428. } else if (angle <= this.angles.start) {
  429. return this.min;
  430. }
  431. var value = (angle - this.angles.start) / (this.angles.end - this.angles.start);
  432. value = value * (this.max - this.min);
  433. value = value + this.min;
  434. return value;
  435. };
  436. /**
  437. * The getObjectByXY() worker method. Don't call this call:
  438. *
  439. * RG.ObjectRegistry.getObjectByXY(e)
  440. *
  441. * @param object e The event object
  442. */
  443. this.getObjectByXY = function (e)
  444. {
  445. var mouseXY = RG.getMouseXY(e);
  446. var angle = RG.getAngleByXY(this.centerx, this.centery, mouseXY[0], mouseXY[1]);
  447. var accuracy = 15;
  448. var leftMin = this.centerx - this.radius;
  449. var rightMax = this.centerx + this.radius;
  450. var topMin = this.centery - this.radius;
  451. var bottomMax = this.centery + this.radius;
  452. if (
  453. mouseXY[0] > leftMin
  454. && mouseXY[0] < rightMax
  455. && mouseXY[1] > topMin
  456. && mouseXY[1] < bottomMax
  457. ) {
  458. return this;
  459. }
  460. };
  461. /**
  462. * Draws the icon
  463. */
  464. this.drawIcon =
  465. this.DrawIcon = function ()
  466. {
  467. if (!RG.ISOLD) {
  468. if (!this.__icon__ || !this.__icon__.__loaded__) {
  469. var img = new Image();
  470. img.src = prop['chart.icon'];
  471. img.__object__ = this;
  472. this.__icon__ = img;
  473. img.onload = function (e)
  474. {
  475. img.__loaded__ = true;
  476. var obj = img.__object__;
  477. //var co = obj.context;
  478. //var prop = obj.properties;
  479. co.drawImage(img,obj.centerx - (img.width / 2), obj.centery - obj.radius + 35);
  480. obj.DrawNeedle();
  481. if (prop['chart.icon.redraw']) {
  482. obj.Set('chart.icon.redraw', false);
  483. RG.Clear(obj.canvas);
  484. RG.RedrawCanvas(ca);
  485. }
  486. }
  487. } else {
  488. var img = this.__icon__;
  489. co.drawImage(img,this.centerx - (img.width / 2), this.centery - this.radius + 35);
  490. }
  491. }
  492. this.DrawNeedle();
  493. };
  494. /**
  495. * This method handles the adjusting calculation for when the mouse is moved
  496. *
  497. * @param object e The event object
  498. */
  499. this.adjusting_mousemove =
  500. this.Adjusting_mousemove = function (e)
  501. {
  502. /**
  503. * Handle adjusting for the Fuel gauge
  504. */
  505. if (prop['chart.adjustable'] && RG.Registry.Get('chart.adjusting') && RG.Registry.Get('chart.adjusting').uid == this.uid) {
  506. this.value = this.getValue(e);
  507. //RG.Clear(ca);
  508. RG.redrawCanvas(ca);
  509. RG.fireCustomEvent(this, 'onadjust');
  510. }
  511. };
  512. /**
  513. * This method gives you the relevant angle (in radians) that a particular value is
  514. *
  515. * @param number value The relevant angle
  516. */
  517. this.getAngle = function (value)
  518. {
  519. // Range checking
  520. if (value < this.min || value > this.max) {
  521. return null;
  522. }
  523. var angle = (((value - this.min) / (this.max - this.min)) * (this.angles.end - this.angles.start)) + this.angles.start;
  524. return angle;
  525. };
  526. /**
  527. * This allows for easy specification of gradients
  528. */
  529. this.parseColors = function ()
  530. {
  531. // Save the original colors so that they can be restored when the canvas is reset
  532. if (this.original_colors.length === 0) {
  533. this.original_colors['chart.colors'] = RG.array_clone(prop['chart.colors']);
  534. this.original_colors['chart.needle.color'] = RG.array_clone(prop['chart.needle.color']);
  535. }
  536. var props = this.properties;
  537. var colors = props['chart.colors'];
  538. for (var i=0; i<colors.length; ++i) {
  539. colors[i] = this.parseSingleColorForLinearGradient(colors[i]);
  540. }
  541. props['chart.needle.color'] = this.parseSingleColorForRadialGradient(props['chart.needle.color']);
  542. };
  543. /**
  544. * This parses a single color value
  545. */
  546. this.parseSingleColorForLinearGradient = function (color)
  547. {
  548. if (!color || typeof(color) != 'string') {
  549. return color;
  550. }
  551. if (color.match(/^gradient\((.*)\)$/i)) {
  552. var parts = RegExp.$1.split(':');
  553. // Create the gradient
  554. var grad = co.createLinearGradient(prop['chart.gutter.left'],0,ca.width - prop['chart.gutter.right'],0);
  555. var diff = 1 / (parts.length - 1);
  556. grad.addColorStop(0, RG.trim(parts[0]));
  557. for (var j=1; j<parts.length; ++j) {
  558. grad.addColorStop(j * diff, RG.trim(parts[j]));
  559. }
  560. }
  561. return grad ? grad : color;
  562. };
  563. /**
  564. * This parses a single color value
  565. */
  566. this.parseSingleColorForRadialGradient = function (color)
  567. {
  568. if (!color || typeof color != 'string') {
  569. return color;
  570. }
  571. if (color.match(/^gradient\((.*)\)$/i)) {
  572. var parts = RegExp.$1.split(':');
  573. // Create the gradient
  574. var grad = co.createRadialGradient(this.centerx, this.centery, 0, this.centerx, this.centery, this.radius);
  575. var diff = 1 / (parts.length - 1);
  576. grad.addColorStop(0, RG.trim(parts[0]));
  577. for (var j=1; j<parts.length; ++j) {
  578. grad.addColorStop(j * diff, RG.trim(parts[j]));
  579. }
  580. }
  581. return grad ? grad : color;
  582. };
  583. /**
  584. * Using a function to add events makes it easier to facilitate method chaining
  585. *
  586. * @param string type The type of even to add
  587. * @param function func
  588. */
  589. this.on = function (type, func)
  590. {
  591. if (type.substr(0,2) !== 'on') {
  592. type = 'on' + type;
  593. }
  594. this[type] = func;
  595. return this;
  596. };
  597. /**
  598. * This function runs once only
  599. * (put at the end of the file (before any effects))
  600. */
  601. this.firstDrawFunc = function ()
  602. {
  603. };
  604. /**
  605. * Grow
  606. *
  607. * The Fuel chart Grow effect gradually increases the values of the Fuel chart
  608. *
  609. * @param object obj The graph object
  610. */
  611. this.grow = function ()
  612. {
  613. var callback = arguments[1] || function () {};
  614. var opt = arguments[0] || {};
  615. var numFrames = opt.frames || 30;
  616. var frame = 0;
  617. var obj = this;
  618. var origValue = Number(this.currentValue);
  619. if (this.currentValue == null) {
  620. this.currentValue = this.min;
  621. origValue = this.min;
  622. }
  623. var newValue = this.value;
  624. var diff = newValue - origValue;
  625. var step = (diff / numFrames);
  626. var frame = 0;
  627. function iterator ()
  628. {
  629. frame++;
  630. obj.value = ((frame / numFrames) * diff) + origValue
  631. if (obj.value > obj.max) obj.value = obj.max;
  632. if (obj.value < obj.min) obj.value = obj.min;
  633. RGraph.clear(obj.canvas);
  634. RGraph.redrawCanvas(obj.canvas);
  635. if (frame < numFrames) {
  636. RGraph.Effects.updateCanvas(iterator);
  637. // The callback variable is always function
  638. } else {
  639. callback(obj);
  640. }
  641. }
  642. iterator();
  643. return this;
  644. };
  645. /**
  646. * Now need to register all chart types. MUST be after the setters/getters are defined
  647. *
  648. * *** MUST BE LAST IN THE CONSTRUCTOR ***
  649. */
  650. RG.Register(this);
  651. };