RGraph.hprogress.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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 progress bar constructor
  17. *
  18. * @param int id The ID of the canvas tag
  19. * @param int value The indicated value of the meter.
  20. * @param int max The end value (the upper most) of the meter
  21. */
  22. RGraph.HProgress = function (id, value, max)
  23. {
  24. this.id = id;
  25. this.max = max;
  26. this.value = value;
  27. this.canvas = document.getElementById(typeof id === 'object' ? id.id : id);
  28. this.context = this.canvas.getContext('2d');
  29. this.canvas.__object__ = this;
  30. this.type = 'hprogress';
  31. this.coords = [];
  32. this.isRGraph = true;
  33. this.currentValue = null;
  34. this.uid = RGraph.CreateUID();
  35. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  36. this.colorsParsed = false;
  37. this.coordsText = [];
  38. /**
  39. * Compatibility with older browsers
  40. */
  41. RGraph.OldBrowserCompat(this.context);
  42. this.properties = {
  43. 'chart.min': 0,
  44. 'chart.colors': ['Gradient(white:#0c0)','Gradient(white:red)','Gradient(white:green)','yellow','pink','cyan','black','white','gray'],
  45. 'chart.strokestyle.inner': '#999',
  46. 'chart.strokestyle.outer': '#999',
  47. 'chart.tickmarks': true,
  48. 'chart.tickmarks.color': '#999',
  49. 'chart.tickmarks.inner': false,
  50. 'chart.tickmarks.zerostart':true,
  51. 'chart.gutter.left': 25,
  52. 'chart.gutter.right': 25,
  53. 'chart.gutter.top': 25,
  54. 'chart.gutter.bottom': 25,
  55. 'chart.numticks': 10,
  56. 'chart.numticks.inner': 50,
  57. 'chart.background.color': '#eee',
  58. 'chart.shadow': false,
  59. 'chart.shadow.color': 'rgba(0,0,0,0.5)',
  60. 'chart.shadow.blur': 3,
  61. 'chart.shadow.offsetx': 3,
  62. 'chart.shadow.offsety': 3,
  63. 'chart.title': '',
  64. 'chart.title.background': null,
  65. 'chart.title.bold': true,
  66. 'chart.title.font': null,
  67. 'chart.title.x': null,
  68. 'chart.title.y': null,
  69. 'chart.title.halign': null,
  70. 'chart.title.valign': null,
  71. 'chart.text.size': 10,
  72. 'chart.text.color': 'black',
  73. 'chart.text.font': 'Arial',
  74. 'chart.contextmenu': null,
  75. 'chart.units.pre': '',
  76. 'chart.units.post': '',
  77. 'chart.tooltips': null,
  78. 'chart.tooltips.effect': 'fade',
  79. 'chart.tooltips.css.class': 'RGraph_tooltip',
  80. 'chart.tooltips.highlight': true,
  81. 'chart.tooltips.event': 'onclick',
  82. 'chart.highlight.stroke': 'rgba(0,0,0,0)',
  83. 'chart.highlight.fill': 'rgba(255,255,255,0.7)',
  84. 'chart.annotatable': false,
  85. 'chart.annotate.color': 'black',
  86. 'chart.zoom.factor': 1.5,
  87. 'chart.zoom.fade.in': true,
  88. 'chart.zoom.fade.out': true,
  89. 'chart.zoom.hdir': 'right',
  90. 'chart.zoom.vdir': 'down',
  91. 'chart.zoom.frames': 25,
  92. 'chart.zoom.delay': 16.666,
  93. 'chart.zoom.shadow': true,
  94. 'chart.zoom.background': true,
  95. 'chart.arrows': false,
  96. 'chart.margin': 0,
  97. 'chart.resizable': false,
  98. 'chart.resize.handle.adjust': [0,0],
  99. 'chart.resize.handle.background':null,
  100. 'chart.labels.specific': null,
  101. 'chart.labels.count': 10,
  102. 'chart.adjustable': false,
  103. 'chart.scale.decimals': 0,
  104. 'chart.scale.point': '.',
  105. 'chart.scale.thousand': ',',
  106. 'chart.key': null,
  107. 'chart.key.background': 'white',
  108. 'chart.key.position': 'gutter',
  109. 'chart.key.halign': 'right',
  110. 'chart.key.shadow': false,
  111. 'chart.key.shadow.color': '#666',
  112. 'chart.key.shadow.blur': 3,
  113. 'chart.key.shadow.offsetx': 2,
  114. 'chart.key.shadow.offsety': 2,
  115. 'chart.key.position.gutter.boxed': false,
  116. 'chart.key.position.x': null,
  117. 'chart.key.position.y': null,
  118. 'chart.key.color.shape': 'square',
  119. 'chart.key.rounded': true,
  120. 'chart.key.linewidth': 1,
  121. 'chart.key.colors': null,
  122. 'chart.key.color.shape': 'square',
  123. 'chart.key.interactive': false,
  124. 'chart.key.interactive.highlight.chart.stroke': 'black',
  125. 'chart.key.interactive.highlight.chart.fill': 'rgba(255,255,255,0.7)',
  126. 'chart.key.interactive.highlight.label': 'rgba(255,0,0,0.2)',
  127. 'chart.key.text.color': 'black',
  128. 'chart.labels.position': 'bottom',
  129. 'chart.events.mousemove': null,
  130. 'chart.events.click': null,
  131. 'chart.border.inner': true
  132. }
  133. /**
  134. * Allow for new style method of passing arguments to the constructor
  135. */
  136. if (arguments.length == 4) {
  137. this.min = arguments[1];
  138. this.max = arguments[2];
  139. this.value = arguments[3];
  140. this.properties['chart.min'] = arguments[1];
  141. } else if (arguments.length == 3) {
  142. this.min = 0;
  143. this.max = arguments[2];
  144. this.value = arguments[1];
  145. this.properties['chart.min'] = 0;
  146. }
  147. // Check for support
  148. if (!this.canvas) {
  149. alert('[HPROGRESS] No canvas support');
  150. return;
  151. }
  152. /**
  153. * Create the dollar objects so that functions can be added to them
  154. */
  155. var linear_data = RGraph.array_linearize(value);
  156. for (var i=0; i<linear_data.length; ++i) {
  157. this['$' + i] = {};
  158. }
  159. /**
  160. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  161. * done already
  162. */
  163. if (!this.canvas.__rgraph_aa_translated__) {
  164. this.context.translate(0.5,0.5);
  165. this.canvas.__rgraph_aa_translated__ = true;
  166. }
  167. ///////////////////////////////// SHORT PROPERTIES /////////////////////////////////
  168. var RG = RGraph;
  169. var ca = this.canvas;
  170. var co = ca.getContext('2d');
  171. var prop = this.properties;
  172. //////////////////////////////////// METHODS ///////////////////////////////////////
  173. /**
  174. * A generic setter
  175. *
  176. * @param string name The name of the property to set
  177. * @param string value The value of the poperty
  178. */
  179. this.Set = function (name, value)
  180. {
  181. name = name.toLowerCase();
  182. /**
  183. * This should be done first - prepend the propertyy name with "chart." if necessary
  184. */
  185. if (name.substr(0,6) != 'chart.') {
  186. name = 'chart.' + name;
  187. }
  188. /**
  189. * chart.strokestyle now sets both chart.strokestyle.inner and chart.strokestyle.outer
  190. */
  191. if (name == 'chart.strokestyle') {
  192. this.Set('chart.strokestyle.inner', value);
  193. this.Set('chart.strokestyle.outer', value);
  194. return;
  195. }
  196. prop[name] = value;
  197. return this;
  198. }
  199. /**
  200. * A generic getter
  201. *
  202. * @param string name The name of the property to get
  203. */
  204. this.Get = function (name)
  205. {
  206. /**
  207. * This should be done first - prepend the property name with "chart." if necessary
  208. */
  209. if (name.substr(0,6) != 'chart.') {
  210. name = 'chart.' + name;
  211. }
  212. return prop[name.toLowerCase()];
  213. }
  214. /**
  215. * Draws the progress bar
  216. */
  217. this.Draw = function ()
  218. {
  219. /**
  220. * Fire the onbeforedraw event
  221. */
  222. RG.FireCustomEvent(this, 'onbeforedraw');
  223. /**
  224. * Parse the colors. This allows for simple gradient syntax
  225. */
  226. if (!this.colorsParsed) {
  227. this.parseColors();
  228. // Don't want to do this again
  229. this.colorsParsed = true;
  230. }
  231. /**
  232. * Set the current value
  233. */
  234. this.currentValue = this.value;
  235. /**
  236. * This is new in May 2011 and facilitates individual gutter settings,
  237. * eg chart.gutter.left
  238. */
  239. this.gutterLeft = prop['chart.gutter.left'];
  240. this.gutterRight = prop['chart.gutter.right'];
  241. this.gutterTop = prop['chart.gutter.top'];
  242. this.gutterBottom = prop['chart.gutter.bottom'];
  243. // Figure out the width and height
  244. this.width = ca.width - this.gutterLeft - this.gutterRight;
  245. this.height = ca.height - this.gutterTop - this.gutterBottom;
  246. this.coords = [];
  247. this.Drawbar();
  248. this.DrawTickMarks();
  249. this.DrawLabels();
  250. this.DrawTitle();
  251. co.stroke();
  252. co.fill();
  253. /**
  254. * Draw the bevel effect if requested
  255. */
  256. if (prop['chart.bevel']) {
  257. this.DrawBevel();
  258. }
  259. /**
  260. * Setup the context menu if required
  261. */
  262. if (prop['chart.contextmenu']) {
  263. RG.ShowContext(this);
  264. }
  265. // Draw the key if necessary
  266. if (prop['chart.key'] && prop['chart.key'].length) {
  267. RG.DrawKey(this, prop['chart.key'], prop['chart.colors']);
  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 RGraph ondraw event
  281. */
  282. RG.FireCustomEvent(this, 'ondraw');
  283. return this;
  284. }
  285. /**
  286. * Draws the bar
  287. */
  288. this.Drawbar = function ()
  289. {
  290. /**
  291. * First get the scale
  292. */
  293. this.scale2 = RG.getScale2(this, {
  294. 'max':this.max,
  295. 'min':this.min,
  296. 'strict':true,
  297. 'scale.thousand':prop['chart.scale.thousand'],
  298. 'scale.point':prop['chart.scale.point'],
  299. 'scale.decimals':prop['chart.scale.decimals'],
  300. 'ylabels.count':prop['chart.labels.count'],
  301. 'scale.round':prop['chart.scale.round'],
  302. 'units.pre': prop['chart.units.pre'],
  303. 'units.post': prop['chart.units.post']
  304. });
  305. // Set a shadow if requested
  306. if (prop['chart.shadow']) {
  307. RG.SetShadow(this, prop['chart.shadow.color'], prop['chart.shadow.offsetx'], prop['chart.shadow.offsety'], prop['chart.shadow.blur']);
  308. }
  309. // Draw the shadow for MSIE
  310. if (ISOLD && prop['chart.shadow']) {
  311. co.fillStyle = prop['chart.shadow.color'];
  312. co.fillRect(this.gutterLeft + prop['chart.shadow.offsetx'], this.gutterTop + prop['chart.shadow.offsety'], this.width, this.height);
  313. }
  314. // Draw the outline
  315. co.fillStyle = prop['chart.background.color'];
  316. co.strokeStyle = prop['chart.strokestyle.outer'];
  317. co.strokeRect(this.gutterLeft, this.gutterTop, this.width, this.height);
  318. co.fillRect(this.gutterLeft, this.gutterTop, this.width, this.height);
  319. // Turn off any shadow
  320. RG.NoShadow(this);
  321. co.fillStyle = prop['chart.colors'][0];
  322. co.strokeStyle = prop['chart.strokestyle.outer'];
  323. var margin = prop['chart.margin'];
  324. // Draw the actual bar itself
  325. var barWidth = Math.min(this.width, ((RG.array_sum(this.value) - prop['chart.min']) / (this.max - prop['chart.min']) ) * this.width);
  326. if (prop['chart.tickmarks.inner']) {
  327. var spacing = (ca.width - this.gutterLeft - this.gutterRight) / prop['chart.numticks.inner'];
  328. co.lineWidth = 1;
  329. co.strokeStyle = prop['chart.strokestyle.outer'];
  330. co.beginPath();
  331. for (var x = this.gutterLeft; x<ca.width - this.gutterRight; x+=spacing) {
  332. co.moveTo(Math.round(x), this.gutterTop);
  333. co.lineTo(Math.round(x), this.gutterTop + 2);
  334. co.moveTo(Math.round(x), ca.height - this.gutterBottom);
  335. co.lineTo(Math.round(x), ca.height - this.gutterBottom - 2);
  336. }
  337. co.stroke();
  338. }
  339. /**
  340. * This bit draws the actual progress bar
  341. */
  342. if (typeof(this.value) == 'number') {
  343. co.beginPath();
  344. co.strokeStyle = prop['chart.strokestyle.inner'];
  345. co.fillStyle = prop['chart.colors'][0];
  346. if (prop['chart.border.inner']) {
  347. co.strokeRect(this.gutterLeft, this.gutterTop + margin, barWidth, this.height - margin - margin);
  348. }
  349. co.fillRect(this.gutterLeft, this.gutterTop + margin, barWidth, this.height - margin - margin);
  350. // Store the coords
  351. this.coords.push([this.gutterLeft,
  352. this.gutterTop + margin,
  353. barWidth,
  354. this.height - margin - margin]);
  355. } else if (typeof(this.value) == 'object') {
  356. co.beginPath();
  357. co.strokeStyle = prop['chart.strokestyle.inner'];
  358. var startPoint = this.gutterLeft;
  359. for (var i=0; i<this.value.length; ++i) {
  360. var segmentLength = (this.value[i] / RG.array_sum(this.value)) * barWidth;
  361. co.fillStyle = prop['chart.colors'][i];
  362. if (prop['chart.border.inner']) {
  363. co.strokeRect(startPoint, this.gutterTop + margin, segmentLength, this.height - margin - margin);
  364. }
  365. co.fillRect(startPoint, this.gutterTop + margin, segmentLength, this.height - margin - margin);
  366. // Store the coords
  367. this.coords.push([startPoint,
  368. this.gutterTop + margin,
  369. segmentLength,
  370. this.height - margin - margin]);
  371. startPoint += segmentLength;
  372. }
  373. }
  374. /**
  375. * Draw the arrows indicating the level if requested
  376. */
  377. if (prop['chart.arrows']) {
  378. var x = this.gutterLeft + barWidth;
  379. var y = this.gutterTop;
  380. co.lineWidth = 1;
  381. co.fillStyle = 'black';
  382. co.strokeStyle = 'black';
  383. co.beginPath();
  384. co.moveTo(x, y - 3);
  385. co.lineTo(x + 2, y - 7);
  386. co.lineTo(x - 2, y - 7);
  387. co.closePath();
  388. co.stroke();
  389. co.fill();
  390. co.beginPath();
  391. co.moveTo(x, y + this.height + 4);
  392. co.lineTo(x + 2, y + this.height + 9);
  393. co.lineTo(x - 2, y + this.height + 9);
  394. co.closePath();
  395. co.stroke();
  396. co.fill()
  397. }
  398. /**
  399. * Draw the "in-bar" label
  400. */
  401. if (prop['chart.label.inner']) {
  402. co.fillStyle = 'black';
  403. RG.Text2(this, {'font':prop['chart.text.font'],
  404. 'size':prop['chart.text.size'] + 2,
  405. 'x':this.gutterLeft + barWidth + 5,
  406. 'y':this.gutterTop + (this.height / 2),
  407. 'text': String(prop['chart.units.pre'] + this.value + prop['chart.units.post']),
  408. 'valign':'bottom',
  409. 'halign':'left',
  410. 'bounding':true,
  411. 'boundingFill':'white',
  412. 'tag': 'label.inner'
  413. });
  414. }
  415. }
  416. /**
  417. * The function that draws the tick marks. Apt name...
  418. */
  419. this.DrawTickMarks = function ()
  420. {
  421. co.strokeStyle = prop['chart.tickmarks.color'];
  422. if (prop['chart.tickmarks']) {
  423. co.beginPath();
  424. // This is used by the label function below
  425. this.tickInterval = this.width / prop['chart.numticks'];
  426. var start = prop['chart.tickmarks.zerostart'] ? 0 : this.tickInterval;
  427. if (prop['chart.labels.position'] == 'top') {
  428. for (var i=this.gutterLeft + start; i<=(this.width + this.gutterLeft + 0.1); i+=this.tickInterval) {
  429. co.moveTo(Math.round(i), this.gutterTop);
  430. co.lineTo(Math.round(i), this.gutterTop - 4);
  431. }
  432. } else {
  433. for (var i=this.gutterLeft + start; i<=(this.width + this.gutterLeft + 0.1); i+=this.tickInterval) {
  434. co.moveTo(Math.round(i), this.gutterTop + this.height);
  435. co.lineTo(Math.round(i), this.gutterTop + this.height + 4);
  436. }
  437. }
  438. co.stroke();
  439. }
  440. }
  441. /**
  442. * The function that draws the labels
  443. */
  444. this.DrawLabels = function ()
  445. {
  446. if (!RG.is_null(prop['chart.labels.specific'])) {
  447. return this.DrawSpecificLabels();
  448. }
  449. co.fillStyle = prop['chart.text.color'];
  450. var xPoints = [];
  451. var yPoints = [];
  452. var font = prop['chart.text.font'];
  453. var size = prop['chart.text.size'];
  454. for (i=0,len=this.scale2.labels.length; i<len; i++) {
  455. if (prop['chart.labels.position'] == 'top') {
  456. var x = this.width * (i/this.scale2.labels.length) + this.gutterLeft + (this.width / this.scale2.labels.length);
  457. var y = this.gutterTop - 6;
  458. var valign = 'bottom';
  459. } else {
  460. var x = this.width * (i/this.scale2.labels.length) + this.gutterLeft + (this.width / this.scale2.labels.length);
  461. var y = this.height + this.gutterTop + 4;
  462. var valign = 'top';
  463. }
  464. RG.Text2(this, {'font':font,
  465. 'size':size,
  466. 'x':x,
  467. 'y':y,
  468. 'text': this.scale2.labels[i],
  469. 'valign':valign,
  470. 'halign':'center',
  471. 'tag': 'scale'
  472. });
  473. }
  474. if (prop['chart.tickmarks.zerostart']) {
  475. if (prop['chart.labels.position'] == 'top') {
  476. RG.Text2(this, {'font':font,
  477. 'size':size,
  478. 'x':this.gutterLeft,
  479. 'y':this.gutterTop - 6,
  480. 'text': prop['chart.units.pre'] + Number(prop['chart.min']).toFixed(prop['chart.scale.decimals']) + prop['chart.units.post'],
  481. 'valign':'bottom',
  482. 'halign':'center',
  483. 'tag': 'scale'
  484. });
  485. } else {
  486. RG.Text2(this, {'font':font,
  487. 'size':size,
  488. 'x':this.gutterLeft,
  489. 'y':ca.height - this.gutterBottom + 5,
  490. 'text': prop['chart.units.pre'] + Number(prop['chart.min']).toFixed(prop['chart.scale.decimals']) + prop['chart.units.post'],
  491. 'valign':'top',
  492. 'halign':'center',
  493. 'tag': 'scale'
  494. });
  495. }
  496. }
  497. }
  498. /**
  499. * Returns the focused bar
  500. *
  501. * @param event e The event object
  502. */
  503. this.getShape =
  504. this.getBar = function (e)
  505. {
  506. var mouseCoords = RG.getMouseXY(e)
  507. for (var i=0; i<this.coords.length; i++) {
  508. var mouseCoords = RG.getMouseXY(e);
  509. var mouseX = mouseCoords[0];
  510. var mouseY = mouseCoords[1];
  511. var left = this.coords[i][0];
  512. var top = this.coords[i][1];
  513. var width = this.coords[i][2];
  514. var height = this.coords[i][3];
  515. var idx = i;
  516. if (mouseX >= left && mouseX <= (left + width) && mouseY >= top && mouseY <= (top + height) ) {
  517. var tooltip = RG.parseTooltipText(prop['chart.tooltips'], idx);
  518. return {
  519. 0: this, 1: left, 2: top, 3: width, 4: height, 5: idx,
  520. 'object':this, 'x':left, 'y':top, 'width': width, 'height': height, 'index': idx, 'tooltip': tooltip
  521. }
  522. }
  523. }
  524. }
  525. /**
  526. * This function returns the value that the mouse is positioned at, regardless of
  527. * the actual indicated value.
  528. *
  529. * @param object e The event object
  530. */
  531. this.getValue = function (e)
  532. {
  533. var mouseXY = RG.getMouseXY(e);
  534. var value = (mouseXY[0] - this.gutterLeft) / this.width;
  535. value *= this.max - prop['chart.min'];
  536. value += prop['chart.min'];
  537. if (mouseXY[0] < this.gutterLeft) {
  538. value = prop['chart.min'];
  539. }
  540. if (mouseXY[0] > (ca.width - this.gutterRight) ) {
  541. value = this.max
  542. }
  543. return value;
  544. }
  545. /**
  546. * Each object type has its own Highlight() function which highlights the appropriate shape
  547. *
  548. * @param object shape The shape to highlight
  549. */
  550. this.Highlight = function (shape)
  551. {
  552. // Add the new highlight
  553. RG.Highlight.Rect(this, shape);
  554. }
  555. /**
  556. * The getObjectByXY() worker method. Don't call this call:
  557. *
  558. * RGraph.ObjectRegistry.getObjectByXY(e)
  559. *
  560. * @param object e The event object
  561. */
  562. this.getObjectByXY = function (e)
  563. {
  564. var mouseXY = RG.getMouseXY(e);
  565. if (
  566. mouseXY[0] > this.gutterLeft
  567. && mouseXY[0] < (ca.width - this.gutterRight)
  568. && mouseXY[1] > this.gutterTop
  569. && mouseXY[1] < (ca.height - this.gutterBottom)
  570. ) {
  571. return this;
  572. }
  573. }
  574. /**
  575. * This method handles the adjusting calculation for when the mouse is moved
  576. *
  577. * @param object e The event object
  578. */
  579. this.Adjusting_mousemove = function (e)
  580. {
  581. /**
  582. * Handle adjusting for the HProgress
  583. */
  584. if (prop['chart.adjustable'] && RG.Registry.Get('chart.adjusting') && RG.Registry.Get('chart.adjusting').uid == this.uid) {
  585. var mouseXY = RG.getMouseXY(e);
  586. var value = this.getValue(e);
  587. if (typeof(value) == 'number') {
  588. // Fire the onadjust event
  589. RG.FireCustomEvent(this, 'onadjust');
  590. this.value = Number(value.toFixed(prop['chart.scale.decimals']));
  591. RG.Redraw();
  592. }
  593. }
  594. }
  595. /**
  596. * Draws chart.labels.specific
  597. */
  598. this.DrawSpecificLabels = function ()
  599. {
  600. var labels = prop['chart.labels.specific'];
  601. if (labels) {
  602. var font = prop['chart.text.font'];
  603. var size = prop['chart.text.size'];
  604. var valign = (prop['chart.labels.position'] == 'top' ? 'bottom' : 'top');
  605. var step = this.width / (labels.length - 1);
  606. co.beginPath();
  607. co.fillStyle = prop['chart.text.color'];
  608. for (var i=0; i<labels.length; ++i) {
  609. RG.Text2(this, {'font':font,
  610. 'size':size,
  611. 'x': this.gutterLeft + (step * i),
  612. 'y':prop['chart.labels.position'] == 'top' ? this.gutterTop - 7 : ca.height - this.gutterBottom + 7,
  613. 'text': labels[i],
  614. 'valign':valign,
  615. 'halign':'center',
  616. 'tag': 'labels.specific'
  617. });
  618. }
  619. co.fill();
  620. }
  621. }
  622. /**
  623. * This function positions a tooltip when it is displayed
  624. *
  625. * @param obj object The chart object
  626. * @param int x The X coordinate specified for the tooltip
  627. * @param int y The Y coordinate specified for the tooltip
  628. * @param objec tooltip The tooltips DIV element
  629. */
  630. this.positionTooltip = function (obj, x, y, tooltip, idx)
  631. {
  632. var coordX = obj.coords[tooltip.__index__][0];
  633. var coordY = obj.coords[tooltip.__index__][1];
  634. var coordW = obj.coords[tooltip.__index__][2];
  635. var coordH = obj.coords[tooltip.__index__][3];
  636. var canvasXY = RG.getCanvasXY(obj.canvas);
  637. var gutterLeft = this.gutterLeft;
  638. var gutterTop = this.gutterTop;
  639. var width = tooltip.offsetWidth;
  640. var height = tooltip.offsetHeight;
  641. // Set the top position
  642. tooltip.style.left = 0;
  643. tooltip.style.top = canvasXY[1] + coordY - height - 7 + 'px';
  644. // By default any overflow is hidden
  645. tooltip.style.overflow = '';
  646. // The arrow
  647. var img = new Image();
  648. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
  649. img.style.position = 'absolute';
  650. img.id = '__rgraph_tooltip_pointer__';
  651. img.style.top = (tooltip.offsetHeight - 2) + 'px';
  652. tooltip.appendChild(img);
  653. // Reposition the tooltip if at the edges:
  654. // LEFT edge
  655. if ((canvasXY[0] + coordX + (coordW / 2) - (width / 2)) < 10) {
  656. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + (coordW / 2) + 'px';
  657. img.style.left = ((width * 0.1) - 8.5) + 'px';
  658. // RIGHT edge
  659. } else if ((canvasXY[0] + (coordW / 2) + coordX + (width / 2)) > document.body.offsetWidth) {
  660. tooltip.style.left = canvasXY[0] + coordX - (width * 0.9) + (coordW / 2) + 'px';
  661. img.style.left = ((width * 0.9) - 8.5) + 'px';
  662. // Default positioning - CENTERED
  663. } else {
  664. tooltip.style.left = (canvasXY[0] + coordX + (coordW / 2) - (width * 0.5)) + 'px';
  665. img.style.left = ((width * 0.5) - 8.5) + 'px';
  666. }
  667. }
  668. /**
  669. * This function returns the appropriate X coordinate for the given value
  670. *
  671. * @param int value The value you want the coordinate for
  672. * @returm int The coordinate
  673. */
  674. this.getXCoord = function (value)
  675. {
  676. var min = prop['chart.min'];
  677. if (value < min || value > this.max) {
  678. return null;
  679. }
  680. var barWidth = ca.width - this.gutterLeft - this.gutterRight;
  681. var coord = ((value - min) / (this.max - min)) * barWidth;
  682. coord = this.gutterLeft + coord;
  683. return coord;
  684. }
  685. /**
  686. * This returns true/false as to whether the cursor is over the chart area.
  687. * The cursor does not necessarily have to be over the bar itself.
  688. */
  689. this.overChartArea = function (e)
  690. {
  691. var mouseXY = RGraph.getMouseXY(e);
  692. var mouseX = mouseXY[0];
  693. var mouseY = mouseXY[1];
  694. if ( mouseX >= this.gutterLeft
  695. && mouseX <= (ca.width - this.gutterRight)
  696. && mouseY >= this.gutterTop
  697. && mouseY <= (ca.height - this.gutterBottom)
  698. ) {
  699. return true;
  700. }
  701. return false;
  702. }
  703. /**
  704. *
  705. */
  706. this.parseColors = function ()
  707. {
  708. var colors = prop['chart.colors'];
  709. for (var i=0; i<colors.length; ++i) {
  710. colors[i] = this.parseSingleColorForGradient(colors[i]);
  711. }
  712. prop['chart.tickmarks.color'] = this.parseSingleColorForGradient(prop['chart.tickmarks.color']);
  713. prop['chart.strokestyle.inner'] = this.parseSingleColorForGradient(prop['chart.strokestyle.inner']);
  714. prop['chart.strokestyle.outer'] = this.parseSingleColorForGradient(prop['chart.strokestyle.outer']);
  715. prop['chart.highlight.fill'] = this.parseSingleColorForGradient(prop['chart.highlight.fill']);
  716. prop['chart.highlight.stroke'] = this.parseSingleColorForGradient(prop['chart.highlight.stroke']);
  717. prop['chart.background.color'] = this.parseSingleColorForGradient(prop['chart.background.color']);
  718. }
  719. /**
  720. * This parses a single color value
  721. */
  722. this.parseSingleColorForGradient = function (color)
  723. {
  724. if (!color || typeof(color) != 'string') {
  725. return color;
  726. }
  727. if (color.match(/^gradient\((.*)\)$/i)) {
  728. var parts = RegExp.$1.split(':');
  729. // Create the gradient
  730. var grad = co.createLinearGradient(prop['chart.gutter.left'],0,ca.width - prop['chart.gutter.right'],0);
  731. var diff = 1 / (parts.length - 1);
  732. grad.addColorStop(0, RG.trim(parts[0]));
  733. for (var j=1; j<parts.length; ++j) {
  734. grad.addColorStop(j * diff, RG.trim(parts[j]));
  735. }
  736. }
  737. return grad ? grad : color;
  738. }
  739. /**
  740. * Draws the bevel effect
  741. */
  742. this.DrawBevel = function ()
  743. {
  744. // In case of multiple segments - this adds up all the lengths
  745. for (var i=0,len=0; i<this.coords.length; ++i) len += this.coords[i][2];
  746. co.save();
  747. // Draw a path to clip to
  748. co.beginPath();
  749. co.rect(this.coords[0][0], this.coords[0][1], len, this.coords[0][3]);
  750. co.clip();
  751. // Now draw the rect with a shadow
  752. co.beginPath();
  753. co.shadowColor = 'black';
  754. co.shadowOffsetX = 0;
  755. co.shadowOffsetY = 0;
  756. co.shadowBlur = 15;
  757. co.lineWidth = 2;
  758. co.rect(this.coords[0][0] - 1, this.coords[0][1] - 1, len + 2, this.coords[0][3] + 2);
  759. co.stroke();
  760. co.restore();
  761. }
  762. /**
  763. * Draw the titles
  764. */
  765. this.DrawTitle = function ()
  766. {
  767. // Draw the title text
  768. if (prop['chart.title'].length) {
  769. var x = ((ca.width - this.gutterLeft - this.gutterRight) / 2) + this.gutterLeft;
  770. var text = prop['chart.title'];
  771. var size = prop['chart.title.size'] ? prop['chart.title.size'] : prop['chart.text.size'] + 2;
  772. var font = prop['chart.title.font'] ? prop['chart.title.font'] : prop['chart.text.font'];
  773. if (prop['chart.labels.position'] == 'top') {
  774. y = ca.height - this.gutterBottom +5;
  775. x = ((ca.width - this.gutterLeft - this.gutterRight) / 2) + this.gutterLeft;
  776. valign = 'top';
  777. } else {
  778. x = ((ca.width - this.gutterLeft - this.gutterRight) / 2) + this.gutterLeft;
  779. y = this.gutterTop - 5;
  780. valign = 'bottom';
  781. }
  782. RG.Text2(this, {'font':font,
  783. 'size':size,
  784. 'x': typeof(prop['chart.title.x']) == 'number' ? prop['chart.title.x'] : x,
  785. 'y': typeof(prop['chart.title.y']) == 'number' ? prop['chart.title.y'] : y,
  786. 'text': text,
  787. 'valign': prop['chart.title.valign'] ? prop['chart.title.valign'] : valign,
  788. 'halign': prop['chart.title.halign'] ? prop['chart.title.halign'] : 'center',
  789. 'bold':prop['chart.title.bold'],
  790. 'bounding': prop['chart.title.background'] ? true : false,
  791. 'boundingFill': prop['chart.title.background'],
  792. 'tag': 'title'
  793. });
  794. }
  795. }
  796. /**
  797. * This function handles highlighting an entire data-series for the interactive
  798. * key
  799. *
  800. * @param int index The index of the data series to be highlighted
  801. */
  802. this.interactiveKeyHighlight = function (index)
  803. {
  804. var coords = this.coords[index];
  805. co.beginPath();
  806. co.strokeStyle = prop['chart.key.interactive.highlight.chart.stroke'];
  807. co.lineWidth = 2;
  808. co.fillStyle = prop['chart.key.interactive.highlight.chart.fill'];
  809. co.rect(coords[0], coords[1], coords[2], coords[3]);
  810. co.fill();
  811. co.stroke();
  812. // Reset the linewidth
  813. co.lineWidth = 1;
  814. }
  815. /**
  816. * Register the object for redrawing
  817. */
  818. RG.Register(this);
  819. }