RGraph.bipolar.js 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  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 bi-polar/age frequency constructor.
  14. *
  15. * @param string id The id of the canvas
  16. * @param array left The left set of data points
  17. * @param array right The right set of data points
  18. *
  19. * REMEMBER If ymin is implemented you need to update the .getValue() method
  20. */
  21. RGraph.Bipolar = function (id, left, right)
  22. {
  23. var tmp = RGraph.getCanvasTag(id);
  24. // Get the canvas and context objects
  25. this.id = tmp[0];
  26. this.canvas = tmp[1];
  27. this.context = this.canvas.getContext ? this.canvas.getContext("2d", {alpha: (typeof id === 'object' && id.alpha === false) ? false : true}) : null;
  28. this.canvas.__object__ = this;
  29. this.type = 'bipolar';
  30. this.coords = [];
  31. this.coordsLeft = [];
  32. this.coordsRight = [];
  33. this.max = 0;
  34. this.isRGraph = true;
  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. // The left and right data respectively
  45. this.left = left;
  46. this.right = right;
  47. this.data = [left, right];
  48. this.properties =
  49. {
  50. 'chart.margin': 2,
  51. 'chart.xtickinterval': null,
  52. 'chart.labels': [],
  53. 'chart.labels.above': false,
  54. 'chart.text.size': 10,
  55. 'chart.text.color': 'black', // (Simple) gradients are not supported
  56. 'chart.text.font': 'Arial',
  57. 'chart.title.left': '',
  58. 'chart.title.right': '',
  59. 'chart.gutter.center': 60,
  60. 'chart.gutter.left': 25,
  61. 'chart.gutter.right': 25,
  62. 'chart.gutter.top': 25,
  63. 'chart.gutter.bottom': 25,
  64. 'chart.title': '',
  65. 'chart.title.background': null,
  66. 'chart.title.hpos': null,
  67. 'chart.title.vpos': null,
  68. 'chart.title.bold': true,
  69. 'chart.title.font': null,
  70. 'chart.title.x': null,
  71. 'chart.title.y': null,
  72. 'chart.title.halign': null,
  73. 'chart.title.valign': null,
  74. 'chart.colors': ['#0f0'],
  75. 'chart.contextmenu': null,
  76. 'chart.tooltips': null,
  77. 'chart.tooltips.effect': 'fade',
  78. 'chart.tooltips.css.class': 'RGraph_tooltip',
  79. 'chart.tooltips.highlight': true,
  80. 'chart.tooltips.event': 'onclick',
  81. 'chart.highlight.stroke': 'rgba(0,0,0,0)',
  82. 'chart.highlight.fill': 'rgba(255,255,255,0.7)',
  83. 'chart.units.pre': '',
  84. 'chart.units.post': '',
  85. 'chart.shadow': false,
  86. 'chart.shadow.color': '#666',
  87. 'chart.shadow.offsetx': 3,
  88. 'chart.shadow.offsety': 3,
  89. 'chart.shadow.blur': 3,
  90. 'chart.annotatable': false,
  91. 'chart.annotate.color': 'black',
  92. 'chart.xmax': null,
  93. 'chart.xmin': 0,
  94. 'chart.scale.decimals': null,
  95. 'chart.scale.point': '.',
  96. 'chart.scale.thousand': ',',
  97. 'chart.axis.color': 'black',
  98. 'chart.zoom.factor': 1.5,
  99. 'chart.zoom.fade.in': true,
  100. 'chart.zoom.fade.out': true,
  101. 'chart.zoom.hdir': 'right',
  102. 'chart.zoom.vdir': 'down',
  103. 'chart.zoom.frames': 25,
  104. 'chart.zoom.delay': 16.666,
  105. 'chart.zoom.shadow': true,
  106. 'chart.zoom.background': true,
  107. 'chart.zoom.action': 'zoom',
  108. 'chart.resizable': false,
  109. 'chart.resize.handle.background': null,
  110. 'chart.strokestyle': 'rgba(0,0,0,0)',
  111. 'chart.events.mousemove': null,
  112. 'chart.events.click': null,
  113. 'chart.linewidth': 1,
  114. 'chart.noaxes': false,
  115. 'chart.xlabels': true,
  116. 'chart.numyticks': null,
  117. 'chart.numxticks': 5,
  118. 'chart.axis.linewidth': 1,
  119. 'chart.labels.count': 5
  120. }
  121. // Pad the arrays so they're the same size
  122. while (this.left.length < this.right.length) this.left.push(0);
  123. while (this.left.length > this.right.length) this.right.push(0);
  124. /**
  125. * Set the default for the number of Y tickmarks
  126. */
  127. this.properties['chart.numyticks'] = this.left.length;
  128. /**
  129. * Create the dollar objects so that functions can be added to them
  130. */
  131. var linear_data = RGraph.array_linearize(this.left, this.right);
  132. for (var i=0; i<linear_data.length; ++i) {
  133. this['$' + i] = {};
  134. }
  135. /**
  136. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  137. * done already
  138. */
  139. if (!this.canvas.__rgraph_aa_translated__) {
  140. this.context.translate(0.5,0.5);
  141. this.canvas.__rgraph_aa_translated__ = true;
  142. }
  143. // Short variable names
  144. var RG = RGraph;
  145. var ca = this.canvas;
  146. var co = ca.getContext('2d');
  147. var prop = this.properties;
  148. var jq = jQuery;
  149. var pa = RG.Path;
  150. var win = window;
  151. var doc = document;
  152. var ma = Math;
  153. /**
  154. * "Decorate" the object with the generic effects if the effects library has been included
  155. */
  156. if (RG.Effects && typeof RG.Effects.decorate === 'function') {
  157. RG.Effects.decorate(this);
  158. }
  159. /**
  160. * The setter
  161. *
  162. * @param name string The name of the parameter to set
  163. * @param value mixed The value of the paraneter
  164. */
  165. this.set =
  166. this.Set = function (name, value)
  167. {
  168. name = name.toLowerCase();
  169. /**
  170. * This should be done first - prepend the propertyy name with "chart." if necessary
  171. */
  172. if (name.substr(0,6) != 'chart.') {
  173. name = 'chart.' + name;
  174. }
  175. prop[name] = value;
  176. return this;
  177. };
  178. /**
  179. * The getter
  180. *
  181. * @param name string The name of the parameter to get
  182. */
  183. this.get =
  184. this.Get = function (name)
  185. {
  186. /**
  187. * This should be done first - prepend the property name with "chart." if necessary
  188. */
  189. if (name.substr(0,6) != 'chart.') {
  190. name = 'chart.' + name;
  191. }
  192. return this.properties[name.toLowerCase()];
  193. };
  194. /**
  195. * Draws the graph
  196. */
  197. this.draw =
  198. this.Draw = function ()
  199. {
  200. /**
  201. * Fire the onbeforedraw event
  202. */
  203. RG.FireCustomEvent(this, 'onbeforedraw');
  204. /**
  205. * Parse the colors. This allows for simple gradient syntax
  206. */
  207. if (!this.colorsParsed) {
  208. this.parseColors();
  209. // Don't want to do this again
  210. this.colorsParsed = true;
  211. }
  212. /**
  213. * This is new in May 2011 and facilitates indiviual gutter settings,
  214. * eg chart.gutter.left
  215. */
  216. this.gutterLeft = prop['chart.gutter.left'];
  217. this.gutterRight = prop['chart.gutter.right'];
  218. this.gutterTop = prop['chart.gutter.top'];
  219. this.gutterBottom = prop['chart.gutter.bottom'];
  220. // Reset the data to what was initially supplied
  221. this.left = this.data[0];
  222. this.right = this.data[1];
  223. // Sequential color index
  224. this.sequentialColorIndex = 0;
  225. /**
  226. * Reset the coords array
  227. */
  228. this.coords = [];
  229. /**
  230. * Stop this growing uncontrollably
  231. */
  232. this.coordsText = [];
  233. this.GetMax();
  234. this.DrawAxes();
  235. this.DrawTicks();
  236. this.DrawLeftBars();
  237. this.DrawRightBars();
  238. // Redraw the bars so that shadows on not on top
  239. this.RedrawBars();
  240. this.DrawAxes();
  241. this.DrawLabels();
  242. this.DrawTitles();
  243. /**
  244. * Setup the context menu if required
  245. */
  246. if (prop['chart.contextmenu']) {
  247. RG.ShowContext(this);
  248. }
  249. /**
  250. * This function enables resizing
  251. */
  252. if (prop['chart.resizable']) {
  253. RG.AllowResizing(this);
  254. }
  255. /**
  256. * This installs the event listeners
  257. */
  258. RG.InstallEventListeners(this);
  259. /**
  260. * Fire the onfirstdraw event
  261. */
  262. if (this.firstDraw) {
  263. RG.fireCustomEvent(this, 'onfirstdraw');
  264. this.firstDrawFunc();
  265. this.firstDraw = false;
  266. }
  267. /**
  268. * Fire the RGraph ondraw event
  269. */
  270. RG.FireCustomEvent(this, 'ondraw');
  271. return this;
  272. }
  273. /**
  274. * Draws the axes
  275. */
  276. this.drawAxes =
  277. this.DrawAxes = function ()
  278. {
  279. // Set the linewidth
  280. co.lineWidth = prop['chart.axis.linewidth'] + 0.001;
  281. // Draw the left set of axes
  282. co.beginPath();
  283. co.strokeStyle = prop['chart.axis.color'];
  284. this.axisWidth = (ca.width - prop['chart.gutter.center'] - this.gutterLeft - this.gutterRight) / 2;
  285. this.axisHeight = ca.height - this.gutterTop - this.gutterBottom;
  286. // This must be here so that the two above variables are calculated
  287. if (prop['chart.noaxes']) {
  288. return;
  289. }
  290. co.moveTo(this.gutterLeft, Math.round( ca.height - this.gutterBottom));
  291. co.lineTo(this.gutterLeft + this.axisWidth, Math.round( ca.height - this.gutterBottom));
  292. co.moveTo(Math.round( this.gutterLeft + this.axisWidth), ca.height - this.gutterBottom);
  293. co.lineTo(Math.round( this.gutterLeft + this.axisWidth), this.gutterTop);
  294. co.stroke();
  295. // Draw the right set of axes
  296. co.beginPath();
  297. var x = this.gutterLeft + this.axisWidth + prop['chart.gutter.center'];
  298. co.moveTo(Math.round( x), this.gutterTop);
  299. co.lineTo(Math.round( x), ca.height - this.gutterBottom);
  300. co.moveTo(Math.round( x), Math.round( ca.height - this.gutterBottom));
  301. co.lineTo(ca.width - this.gutterRight, Math.round( ca.height - this.gutterBottom));
  302. co.stroke();
  303. };
  304. /**
  305. * Draws the tick marks on the axes
  306. */
  307. this.drawTicks =
  308. this.DrawTicks = function ()
  309. {
  310. // Set the linewidth
  311. co.lineWidth = prop['chart.axis.linewidth'] + 0.001;
  312. var numDataPoints = this.left.length;
  313. var barHeight = ( (ca.height - this.gutterTop - this.gutterBottom)- (this.left.length * (prop['chart.margin'] * 2) )) / numDataPoints;
  314. // Store this for later
  315. this.barHeight = barHeight;
  316. // If no axes - no tickmarks
  317. if (prop['chart.noaxes']) {
  318. return;
  319. }
  320. // Draw the left Y tick marks
  321. if (prop['chart.numyticks'] > 0) {
  322. co.beginPath();
  323. for (var i=0; i<prop['chart.numyticks']; ++i) {
  324. var y = prop['chart.gutter.top'] + (((ca.height - this.gutterTop - this.gutterBottom) / prop['chart.numyticks']) * i);
  325. co.moveTo(this.gutterLeft + this.axisWidth , y);
  326. co.lineTo(this.gutterLeft + this.axisWidth + 3, y);
  327. }
  328. co.stroke();
  329. //Draw the right axis Y tick marks
  330. co.beginPath();
  331. for (var i=0; i<prop['chart.numyticks']; ++i) {
  332. var y = prop['chart.gutter.top'] + (((ca.height - this.gutterTop - this.gutterBottom) / prop['chart.numyticks']) * i);
  333. co.moveTo(this.gutterLeft + this.axisWidth + prop['chart.gutter.center'], y);
  334. co.lineTo(this.gutterLeft + this.axisWidth + prop['chart.gutter.center'] - 3, y);
  335. }
  336. co.stroke();
  337. }
  338. /**
  339. * X tickmarks
  340. */
  341. if (prop['chart.numxticks'] > 0) {
  342. var xInterval = this.axisWidth / prop['chart.numxticks'];
  343. // Is chart.xtickinterval specified ? If so, use that.
  344. if (typeof(prop['chart.xtickinterval']) == 'number') {
  345. xInterval = prop['chart.xtickinterval'];
  346. }
  347. // Draw the left sides X tick marks
  348. for (i=this.gutterLeft; i<(this.gutterLeft + this.axisWidth); i+=xInterval) {
  349. co.beginPath();
  350. co.moveTo(Math.round( i), ca.height - this.gutterBottom);
  351. co.lineTo(Math.round( i), (ca.height - this.gutterBottom) + 4);
  352. co.closePath();
  353. co.stroke();
  354. }
  355. // Draw the right sides X tick marks
  356. var stoppingPoint = ca.width - this.gutterRight;
  357. for (i=(this.gutterLeft + this.axisWidth + prop['chart.gutter.center'] + xInterval); i<=stoppingPoint; i+=xInterval) {
  358. co.beginPath();
  359. co.moveTo(Math.round(i), ca.height - this.gutterBottom);
  360. co.lineTo(Math.round(i), (ca.height - this.gutterBottom) + 4);
  361. co.closePath();
  362. co.stroke();
  363. }
  364. }
  365. };
  366. /**
  367. * Figures out the maximum value, or if defined, uses xmax
  368. */
  369. this.getMax =
  370. this.GetMax = function()
  371. {
  372. var dec = prop['chart.scale.decimals'];
  373. // chart.xmax defined
  374. if (prop['chart.xmax']) {
  375. var max = prop['chart.xmax'];
  376. var min = prop['chart.xmin'];
  377. this.scale2 = RG.getScale2(this, {
  378. 'max':max,
  379. 'min':min,
  380. 'strict': true,
  381. 'scale.thousand':prop['chart.scale.thousand'],
  382. 'scale.point':prop['chart.scale.point'],
  383. 'scale.decimals':prop['chart.scale.decimals'],
  384. 'ylabels.count':prop['chart.labels.count'],
  385. 'scale.round':prop['chart.scale.round'],
  386. 'units.pre': prop['chart.units.pre'],
  387. 'units.post': prop['chart.units.post']
  388. });
  389. this.max = this.scale2.max;
  390. this.min = this.scale2.min;
  391. /**
  392. * Generate the scale ourselves
  393. */
  394. } else {
  395. var max = Math.max(RG.array_max(this.left), RG.array_max(this.right));
  396. this.scale2 = RG.getScale2(this, {
  397. 'max':max,
  398. //'strict': true,
  399. 'min':prop['chart.xmin'],
  400. 'scale.thousand':prop['chart.scale.thousand'],
  401. 'scale.point':prop['chart.scale.point'],
  402. 'scale.decimals':prop['chart.scale.decimals'],
  403. 'ylabels.count':prop['chart.labels.count'],
  404. 'scale.round':prop['chart.scale.round'],
  405. 'units.pre': prop['chart.units.pre'],
  406. 'units.post': prop['chart.units.post']
  407. });
  408. this.max = this.scale2.max;
  409. this.min = this.scale2.min;
  410. }
  411. // Don't need to return it as it is stored in this.max
  412. };
  413. /**
  414. * Function to draw the left hand bars
  415. */
  416. this.drawLeftBars =
  417. this.DrawLeftBars = function ()
  418. {
  419. // Set the stroke colour
  420. co.strokeStyle = prop['chart.strokestyle'];
  421. // Set the linewidth
  422. co.lineWidth = prop['chart.linewidth'];
  423. for (i=0; i<this.left.length; ++i) {
  424. /**
  425. * Turn on a shadow if requested
  426. */
  427. if (prop['chart.shadow']) {
  428. co.shadowColor = prop['chart.shadow.color'];
  429. co.shadowBlur = prop['chart.shadow.blur'];
  430. co.shadowOffsetX = prop['chart.shadow.offsetx'];
  431. co.shadowOffsetY = prop['chart.shadow.offsety'];
  432. }
  433. co.beginPath();
  434. // If chart.colors.sequential is specified - handle that
  435. if (prop['chart.colors.sequential']) {
  436. co.fillStyle = prop['chart.colors'][this.sequentialColorIndex];
  437. this.sequentialColorIndex++;
  438. } else {
  439. co.fillStyle = prop['chart.colors'][0];
  440. }
  441. /**
  442. * Work out the coordinates
  443. */
  444. var width = (( (this.left[i] - this.min) / (this.max - this.min)) * this.axisWidth);
  445. var coords = [Math.round( this.gutterLeft + this.axisWidth - width),
  446. Math.round( this.gutterTop + (i * ( this.axisHeight / this.left.length)) + prop['chart.margin']),
  447. width,
  448. this.barHeight];
  449. // Draw the IE shadow if necessary
  450. if (RG.ISOLD && prop['chart.shadow']) {
  451. this.DrawIEShadow(coords);
  452. }
  453. if (this.left[i]) {
  454. co.strokeRect(coords[0], coords[1], coords[2], coords[3]);
  455. co.fillRect(coords[0], coords[1], coords[2], coords[3]);
  456. }
  457. co.stroke();
  458. co.fill();
  459. /**
  460. * Add the coordinates to the coords array
  461. */
  462. this.coords.push([coords[0],coords[1],coords[2],coords[3]]);
  463. this.coordsLeft.push([coords[0],coords[1],coords[2],coords[3]]);
  464. }
  465. /**
  466. * Turn off any shadow
  467. */
  468. RG.NoShadow(this);
  469. // Reset the linewidth
  470. co.lineWidth = 1;
  471. };
  472. /**
  473. * Function to draw the right hand bars
  474. */
  475. this.drawRightBars =
  476. this.DrawRightBars = function ()
  477. {
  478. // Set the stroke colour
  479. co.strokeStyle = prop['chart.strokestyle'];
  480. // Set the linewidth
  481. co.lineWidth = prop['chart.linewidth'];
  482. /**
  483. * Turn on a shadow if requested
  484. */
  485. if (prop['chart.shadow']) {
  486. co.shadowColor = prop['chart.shadow.color'];
  487. co.shadowBlur = prop['chart.shadow.blur'];
  488. co.shadowOffsetX = prop['chart.shadow.offsetx'];
  489. co.shadowOffsetY = prop['chart.shadow.offsety'];
  490. }
  491. for (var i=0; i<this.right.length; ++i) {
  492. co.beginPath();
  493. // If chart.colors.sequential is specified - handle that
  494. if (prop['chart.colors.sequential']) {
  495. co.fillStyle = prop['chart.colors'][this.sequentialColorIndex++];
  496. } else {
  497. co.fillStyle = prop['chart.colors'][0];
  498. }
  499. var width = (((this.right[i] - this.min) / (this.max - this.min)) * this.axisWidth);
  500. var coords = [
  501. Math.round( this.gutterLeft + this.axisWidth + prop['chart.gutter.center']),
  502. Math.round( prop['chart.margin'] + (i * (this.axisHeight / this.right.length)) + this.gutterTop),
  503. width,
  504. this.barHeight
  505. ];
  506. // Draw the IE shadow if necessary
  507. if (RG.ISOLD && prop['chart.shadow']) {
  508. this.DrawIEShadow(coords);
  509. }
  510. if (this.right[i]) {
  511. co.strokeRect(Math.round( coords[0]), Math.round( coords[1]), coords[2], coords[3]);
  512. co.fillRect(Math.round( coords[0]), Math.round( coords[1]), coords[2], coords[3]);
  513. }
  514. co.closePath();
  515. /**
  516. * Add the coordinates to the coords array
  517. */
  518. this.coords.push([coords[0],coords[1],coords[2],coords[3]]);
  519. this.coordsRight.push([coords[0],coords[1],coords[2],coords[3]]);
  520. }
  521. co.stroke();
  522. /**
  523. * Turn off any shadow
  524. */
  525. RG.NoShadow(this);
  526. // Reset the linewidth
  527. co.lineWidth = 1;
  528. };
  529. /**
  530. * Draws the titles
  531. */
  532. this.drawLabels =
  533. this.DrawLabels = function ()
  534. {
  535. co.fillStyle = prop['chart.text.color'];
  536. //var labelPoints = new Array();
  537. var font = prop['chart.text.font'];
  538. var size = prop['chart.text.size'];
  539. var labels = prop['chart.labels'];
  540. var barAreaHeight = ca.height - this.gutterTop - this.gutterBottom;
  541. for (var i=0,len=labels.length; i<len; i+=1) {
  542. RG.Text2(this, {'font':font,
  543. 'size':size,
  544. 'x':this.gutterLeft + this.axisWidth + (prop['chart.gutter.center'] / 2),
  545. 'y':this.gutterTop + ((barAreaHeight / labels.length) * (i)) + ((barAreaHeight / labels.length) / 2),
  546. 'text':String(labels[i] ? String(labels[i]) : ''),
  547. 'halign':'center',
  548. 'valign':'center',
  549. 'marker':false,
  550. 'tag': 'labels'
  551. });
  552. }
  553. /*
  554. * OLD STYLE LABELS
  555. *
  556. var max = Math.max(this.left.length, this.right.length);
  557. for (i=0; i<max; ++i) {
  558. var barAreaHeight = ca.height - this.gutterTop - this.gutterBottom;
  559. var barHeight = barAreaHeight / this.left.length;
  560. var yPos = (i * barAreaHeight) + this.gutterTop;
  561. labelPoints.push(this.gutterTop + (i * barHeight) + (barHeight / 2) + 5);
  562. }
  563. for (i=0; i<labelPoints.length; ++i) {
  564. RG.Text2(this, {'font':prop['chart.text.font'],
  565. 'size':prop['chart.text.size'],
  566. 'x':this.gutterLeft + this.axisWidth + (prop['chart.gutter.center'] / 2),
  567. 'y':labelPoints[i],
  568. 'text':String(prop['chart.labels'][i] ? prop['chart.labels'][i] : ''),
  569. 'halign':'center',
  570. 'tag': 'labels'
  571. });
  572. }
  573. */
  574. if (prop['chart.xlabels']) {
  575. var grapharea = (ca.width - prop['chart.gutter.center'] - this.gutterLeft - this.gutterRight) / 2;
  576. // Now draw the X labels for the left hand side
  577. for (var i=0; i<this.scale2.labels.length; ++i) {
  578. RG.Text2(this, {'font':font,
  579. 'size':size,
  580. 'x':this.gutterLeft + ((grapharea / this.scale2.labels.length) * i),
  581. 'y':ca.height - this.gutterBottom + 3,
  582. 'text':this.scale2.labels[this.scale2.labels.length - i - 1],
  583. 'valign':'top',
  584. 'halign':'center',
  585. 'tag': 'scale'
  586. });
  587. // Draw the scale for the right hand side
  588. RG.Text2(this, {'font':font,
  589. 'size':size,
  590. 'x':this.gutterLeft+ grapharea + prop['chart.gutter.center'] + ((grapharea / this.scale2.labels.length) * (i + 1)),
  591. 'y':ca.height - this.gutterBottom + 3,
  592. 'text':this.scale2.labels[i],
  593. 'valign':'top',
  594. 'halign':'center',
  595. 'tag': 'scale'
  596. });
  597. }
  598. }
  599. /**
  600. * Draw above labels
  601. */
  602. if (prop['chart.labels.above']) {
  603. // Draw the left sides above labels
  604. for (var i=0; i<this.coordsLeft.length; ++i) {
  605. if (typeof(this.left[i]) != 'number') {
  606. continue;
  607. }
  608. var coords = this.coordsLeft[i];
  609. RG.Text2(this, {'font':font,
  610. 'size':size,
  611. 'x':coords[0] - 5,
  612. 'y':coords[1] + (coords[3] / 2),
  613. 'text':RG.number_format(this, this.left[i], prop['chart.units.pre'], prop['chart.units.post']),
  614. 'valign':'center',
  615. 'halign':'right',
  616. 'tag':'labels.above'
  617. });
  618. }
  619. // Draw the right sides above labels
  620. for (i=0; i<this.coordsRight.length; ++i) {
  621. if (typeof(this.right[i]) != 'number') {
  622. continue;
  623. }
  624. var coords = this.coordsRight[i];
  625. RG.Text2(this, {'font':font,
  626. 'size':size,
  627. 'x':coords[0] + coords[2] + 5,
  628. 'y':coords[1] + (coords[3] / 2),
  629. 'text':RG.number_format(this, this.right[i], prop['chart.units.pre'], prop['chart.units.post']),
  630. 'valign':'center',
  631. 'halign':'left',
  632. 'tag': 'labels.above'
  633. });
  634. }
  635. }
  636. };
  637. /**
  638. * Draws the titles
  639. */
  640. this.drawTitles =
  641. this.DrawTitles = function ()
  642. {
  643. RG.Text2(this, {'font':prop['chart.text.font'],
  644. 'size':prop['chart.text.size'],
  645. 'x':this.gutterLeft + 5,
  646. 'y':this.gutterTop - 5,
  647. 'text':String(prop['chart.title.left']),
  648. 'halign':'left',
  649. 'valign':'bottom',
  650. 'tag': 'title.left'
  651. });
  652. RG.Text2(this, {'font':prop['chart.text.font'],
  653. 'size':prop['chart.text.size'],
  654. 'x': ca.width - this.gutterRight - 5,
  655. 'y':this.gutterTop - 5,
  656. 'text':String(prop['chart.title.right']),
  657. 'halign':'right',
  658. 'valign':'bottom',
  659. 'tag': 'title.right'
  660. });
  661. // Draw the main title for the whole chart
  662. RG.DrawTitle(this, prop['chart.title'], this.gutterTop, null, prop['chart.title.size'] ? prop['chart.title.size'] : null);
  663. };
  664. /**
  665. * This function is used by MSIE only to manually draw the shadow
  666. *
  667. * @param array coords The coords for the bar
  668. */
  669. this.drawIEShadow =
  670. this.DrawIEShadow = function (coords)
  671. {
  672. var prevFillStyle = co.fillStyle;
  673. var offsetx = prop['chart.shadow.offsetx'];
  674. var offsety = prop['chart.shadow.offsety'];
  675. co.lineWidth = prop['chart.linewidth'];
  676. co.fillStyle = prop['chart.shadow.color'];
  677. co.beginPath();
  678. // Draw shadow here
  679. co.fillRect(coords[0] + offsetx, coords[1] + offsety, coords[2],coords[3]);
  680. co.fill();
  681. // Change the fillstyle back to what it was
  682. co.fillStyle = prevFillStyle;
  683. }
  684. /**
  685. * Returns the appropriate focussed bar coordinates
  686. *
  687. * @param e object The event object
  688. */
  689. this.getShape =
  690. this.getBar = function (e)
  691. {
  692. var canvas = this.canvas;
  693. var context = this.context;
  694. var mouseCoords = RG.getMouseXY(e);
  695. /**
  696. * Loop through the bars determining if the mouse is over a bar
  697. */
  698. for (var i=0; i<this.coords.length; i++) {
  699. var mouseX = mouseCoords[0];
  700. var mouseY = mouseCoords[1];
  701. var left = this.coords[i][0];
  702. var top = this.coords[i][1];
  703. var width = this.coords[i][2];
  704. var height = this.coords[i][3];
  705. if (mouseX >= left && mouseX <= (left + width) && mouseY >= top && mouseY <= (top + height) ) {
  706. var tooltip = RG.parseTooltipText(prop['chart.tooltips'], i);
  707. return {
  708. 0: this,1: left,2: top,3: width,4: height,5: i,
  709. 'object': this, 'x': left, 'y': top, 'width': width, 'height': height, 'index': i, 'tooltip': tooltip
  710. };
  711. }
  712. }
  713. return null;
  714. };
  715. /**
  716. * Each object type has its own Highlight() function which highlights the appropriate shape
  717. *
  718. * @param object shape The shape to highlight
  719. */
  720. this.highlight =
  721. this.Highlight = function (shape)
  722. {
  723. // Add the new highlight
  724. RG.Highlight.Rect(this, shape);
  725. };
  726. /**
  727. * When you click on the canvas, this will return the relevant value (if any)
  728. *
  729. * REMEMBER This function will need updating if the Bipolar ever gets chart.ymin
  730. *
  731. * @param object e The event object
  732. */
  733. this.getValue = function (e)
  734. {
  735. var obj = e.target.__object__;
  736. var mouseXY = RG.getMouseXY(e);
  737. var mouseX = mouseXY[0];
  738. /**
  739. * Left hand side
  740. */
  741. if (mouseX > this.gutterLeft && mouseX < ( (ca.width / 2) - (prop['chart.gutter.center'] / 2) )) {
  742. var value = (mouseX - prop['chart.gutter.left']) / this.axisWidth;
  743. value = this.max - (value * this.max);
  744. }
  745. /**
  746. * Right hand side
  747. */
  748. if (mouseX < (ca.width - this.gutterRight) && mouseX > ( (ca.width / 2) + (prop['chart.gutter.center'] / 2) )) {
  749. var value = (mouseX - prop['chart.gutter.left'] - this.axisWidth - prop['chart.gutter.center']) / this.axisWidth;
  750. value = (value * this.max);
  751. }
  752. return value;
  753. };
  754. /**
  755. * The getObjectByXY() worker method. Don't call this call:
  756. *
  757. * RGraph.ObjectRegistry.getObjectByXY(e)
  758. *
  759. * @param object e The event object
  760. */
  761. this.getObjectByXY = function (e)
  762. {
  763. var mouseXY = RG.getMouseXY(e);
  764. if (
  765. mouseXY[0] > prop['chart.gutter.left']
  766. && mouseXY[0] < (ca.width - prop['chart.gutter.right'])
  767. && mouseXY[1] > prop['chart.gutter.top']
  768. && mouseXY[1] < (ca.height - prop['chart.gutter.bottom'])
  769. ) {
  770. return this;
  771. }
  772. };
  773. /**
  774. * This function positions a tooltip when it is displayed
  775. *
  776. * @param obj object The chart object
  777. * @param int x The X coordinate specified for the tooltip
  778. * @param int y The Y coordinate specified for the tooltip
  779. * @param objec tooltip The tooltips DIV element
  780. */
  781. this.positionTooltip = function (obj, x, y, tooltip, idx)
  782. {
  783. var coordX = obj.coords[tooltip.__index__][0];
  784. var coordY = obj.coords[tooltip.__index__][1];
  785. var coordW = obj.coords[tooltip.__index__][2];
  786. var coordH = obj.coords[tooltip.__index__][3];
  787. var canvasXY = RG.getCanvasXY(obj.canvas);
  788. var gutterLeft = obj.Get('chart.gutter.left');
  789. var gutterTop = obj.Get('chart.gutter.top');
  790. var width = tooltip.offsetWidth;
  791. var height = tooltip.offsetHeight;
  792. // Set the top position
  793. tooltip.style.left = 0;
  794. tooltip.style.top = canvasXY[1] + coordY - height - 7 + 'px';
  795. // By default any overflow is hidden
  796. tooltip.style.overflow = '';
  797. // The arrow
  798. var img = new Image();
  799. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
  800. img.style.position = 'absolute';
  801. img.id = '__rgraph_tooltip_pointer__';
  802. img.style.top = (tooltip.offsetHeight - 2) + 'px';
  803. tooltip.appendChild(img);
  804. // Reposition the tooltip if at the edges:
  805. // LEFT edge
  806. if ((canvasXY[0] + coordX + (coordW / 2)- (width / 2)) < 0) {
  807. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + (coordW / 2) + 'px';
  808. img.style.left = ((width * 0.1) - 8.5) + 'px';
  809. // RIGHT edge
  810. } else if ((canvasXY[0] + coordX + width) > doc.body.offsetWidth) {
  811. tooltip.style.left = canvasXY[0] + coordX - (width * 0.9) + (coordW / 2) + 'px';
  812. img.style.left = ((width * 0.9) - 8.5) + 'px';
  813. // Default positioning - CENTERED
  814. } else {
  815. tooltip.style.left = (canvasXY[0] + coordX + (coordW / 2) - (width * 0.5)) + 'px';
  816. img.style.left = ((width * 0.5) - 8.5) + 'px';
  817. }
  818. };
  819. /**
  820. * Redraw the bar so that the shadow is NOT on top
  821. */
  822. this.redrawBars =
  823. this.RedrawBars = function ()
  824. {
  825. var coords = this.coords;
  826. var len = coords.length;
  827. // Reset the sequentail color index
  828. this.sequentialColorIndex = 0;
  829. co.beginPath();
  830. // Turn off shadow
  831. RG.NoShadow(this);
  832. // Set the stroke color
  833. co.strokeStyle = prop['chart.strokestyle'];
  834. // Set the linewidth
  835. co.lineWidth = prop['chart.linewidth'];
  836. for (var i=0; i<len; ++i) {
  837. // No redrawing occurs if there is no value
  838. if (coords[i][2] > 0) {
  839. if (prop['chart.colors.sequential']) {
  840. co.fillStyle = prop['chart.colors'][this.sequentialColorIndex++];
  841. } else {
  842. co.fillStyle = prop['chart.colors'][0];
  843. }
  844. // Draw the bar itself
  845. co.strokeRect(coords[i][0], coords[i][1], coords[i][2], coords[i][3]);
  846. co.fillRect(coords[i][0], coords[i][1], coords[i][2], coords[i][3]);
  847. } else {
  848. // Even if there's no redrawing - the color index needs incrementing
  849. this.sequentialColorIndex++
  850. }
  851. }
  852. co.stroke();
  853. co.fill();
  854. };
  855. /**
  856. * Returns the X coords for a value. Returns two coords because there are... two scales.
  857. *
  858. * @param number value The value to get the coord for
  859. */
  860. this.getXCoord = function (value)
  861. {
  862. if (value > this.max || value < 0) {
  863. return null;
  864. }
  865. var ret = [];
  866. // The offset into the graph area
  867. var offset = ((value / this.max) * this.axisWidth);
  868. // Get the coords (one fo each side)
  869. ret[0] = (this.gutterLeft + this.axisWidth) - offset;
  870. ret[1] = (ca.width - this.gutterRight - this.axisWidth) + offset;
  871. return ret;
  872. };
  873. /**
  874. * This allows for easy specification of gradients
  875. */
  876. this.parseColors = function ()
  877. {
  878. // Save the original colors so that they can be restored when the canvas is reset
  879. if (this.original_colors.length === 0) {
  880. this.original_colors['chart.colors'] = RG.array_clone(prop['chart.colors']);
  881. this.original_colors['chart.highlight.stroke'] = RG.array_clone(prop['chart.highlight.fill']);
  882. this.original_colors['chart.highlight.fill'] = RG.array_clone(prop['chart.highlight.fill']);
  883. this.original_colors['chart.axis.color'] = RG.array_clone(prop['chart.axis.color']);
  884. this.original_colors['chart.strokestyle'] = RG.array_clone(prop['chart.strokestyle']);
  885. }
  886. var props = this.properties;
  887. var colors = props['chart.colors'];
  888. for (var i=0; i<colors.length; ++i) {
  889. colors[i] = this.parseSingleColorForGradient(colors[i]);
  890. }
  891. props['chart.highlight.stroke'] = this.parseSingleColorForGradient(props['chart.highlight.stroke']);
  892. props['chart.highlight.fill'] = this.parseSingleColorForGradient(props['chart.highlight.fill']);
  893. props['chart.axis.color'] = this.parseSingleColorForGradient(props['chart.axis.color']);
  894. props['chart.strokestyle'] = this.parseSingleColorForGradient(props['chart.strokestyle']);
  895. };
  896. /**
  897. * This parses a single color value
  898. */
  899. this.parseSingleColorForGradient = function (color)
  900. {
  901. if (!color || typeof(color) != 'string') {
  902. return color;
  903. }
  904. if (color.match(/^gradient\((.*)\)$/i)) {
  905. var parts = RegExp.$1.split(':');
  906. // Create the gradient
  907. var grad = co.createLinearGradient(prop['chart.gutter.left'],0,ca.width - prop['chart.gutter.right'],0);
  908. var diff = 1 / (parts.length - 1);
  909. grad.addColorStop(0, RG.trim(parts[0]));
  910. for (var j=1; j<parts.length; ++j) {
  911. grad.addColorStop(j * diff, RG.trim(parts[j]));
  912. }
  913. }
  914. return grad ? grad : color;
  915. };
  916. /**
  917. * Using a function to add events makes it easier to facilitate method chaining
  918. *
  919. * @param string type The type of even to add
  920. * @param function func
  921. */
  922. this.on = function (type, func)
  923. {
  924. if (type.substr(0,2) !== 'on') {
  925. type = 'on' + type;
  926. }
  927. this[type] = func;
  928. return this;
  929. };
  930. /**
  931. * This function runs once only
  932. * (put at the end of the file (before any effects))
  933. */
  934. this.firstDrawFunc = function ()
  935. {
  936. };
  937. /**
  938. * Objects are now always registered so that when RGraph.Redraw()
  939. * is called this chart will be redrawn.
  940. */
  941. RG.Register(this);
  942. };