RGraph.waterfall.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /**
  2. * o------------------------------------------------------------------------------o
  3. * | This file is part of the RGraph package - you can learn more at: |
  4. * | |
  5. * | http://www.rgraph.net |
  6. * | |
  7. * | This package is licensed under the RGraph license. For all kinds of business |
  8. * | purposes there is a small one-time licensing fee to pay and for non |
  9. * | commercial purposes it is free to use. You can read the full license here: |
  10. * | |
  11. * | http://www.rgraph.net/license |
  12. * o------------------------------------------------------------------------------o
  13. */
  14. if (typeof(RGraph) == 'undefined') RGraph = {};
  15. /**
  16. * The bar chart constructor
  17. *
  18. * @param object canvas The canvas object
  19. * @param array data The chart data
  20. */
  21. RGraph.Waterfall = function (id, data)
  22. {
  23. // Get the canvas and context objects
  24. this.id = id;
  25. this.canvas = document.getElementById(typeof id === 'object' ? id.id : id);
  26. this.context = this.canvas.getContext ? this.canvas.getContext("2d") : null;
  27. this.canvas.__object__ = this;
  28. this.type = 'waterfall';
  29. this.max = 0;
  30. this.isRGraph = true;
  31. this.coords = [];
  32. this.uid = RGraph.CreateUID();
  33. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  34. this.colorsParsed = false;
  35. this.coordsText = [];
  36. /**
  37. * Compatibility with older browsers
  38. */
  39. RGraph.OldBrowserCompat(this.context);
  40. // Various config
  41. this.properties = {
  42. 'chart.background.barcolor1': 'rgba(0,0,0,0)',
  43. 'chart.background.barcolor2': 'rgba(0,0,0,0)',
  44. 'chart.background.grid': true,
  45. 'chart.background.grid.color': '#ddd',
  46. 'chart.background.grid.width': 1,
  47. 'chart.background.grid.hsize': 20,
  48. 'chart.background.grid.vsize': 20,
  49. 'chart.background.grid.vlines': true,
  50. 'chart.background.grid.hlines': true,
  51. 'chart.background.grid.border': true,
  52. 'chart.background.grid.autofit':true,
  53. 'chart.background.grid.autofit.numhlines': 5,
  54. 'chart.background.grid.autofit.numvlines': 20,
  55. 'chart.background.grid.autofit.align': false,
  56. 'chart.background.image': null,
  57. 'chart.background.hbars': null,
  58. 'chart.linewidth': 1,
  59. 'chart.axis.linewidth': 1,
  60. 'chart.xaxispos': 'bottom',
  61. 'chart.numxticks': null,
  62. 'chart.numyticks': 10,
  63. 'chart.hmargin': 5,
  64. 'chart.strokestyle': '#666',
  65. 'chart.axis.color': 'black',
  66. 'chart.gutter.left': 25,
  67. 'chart.gutter.right': 25,
  68. 'chart.gutter.top': 25,
  69. 'chart.gutter.bottom': 25,
  70. 'chart.labels': [],
  71. 'chart.ylabels': true,
  72. 'chart.text.color': 'black',
  73. 'chart.text.size': 10,
  74. 'chart.text.angle': 0,
  75. 'chart.text.font': 'Arial',
  76. 'chart.ymax': null,
  77. 'chart.title': '',
  78. 'chart.title.color': 'black',
  79. 'chart.title.background': null,
  80. 'chart.title.hpos': null,
  81. 'chart.title.vpos': null,
  82. 'chart.title.bold': true,
  83. 'chart.title.font': null,
  84. 'chart.title.xaxis': '',
  85. 'chart.title.yaxis': '',
  86. 'chart.title.yaxis.bold': true,
  87. 'chart.title.yaxis.size': null,
  88. 'chart.title.yaxis.font': null,
  89. 'chart.title.yaxis.color': null,
  90. 'chart.title.xaxis.pos': null,
  91. 'chart.title.yaxis.pos': null,
  92. 'chart.title.yaxis.align': 'left',
  93. 'chart.title.xaxis.bold': true,
  94. 'chart.title.xaxis.size': null,
  95. 'chart.title.xaxis.font': null,
  96. 'chart.title.yaxis.x': null,
  97. 'chart.title.yaxis.y': null,
  98. 'chart.title.xaxis.x': null,
  99. 'chart.title.xaxis.y': null,
  100. 'chart.title.x': null,
  101. 'chart.title.y': null,
  102. 'chart.title.halign': null,
  103. 'chart.title.valign': null,
  104. 'chart.colors': ['Gradient(white:green)', 'Gradient(white:red)', 'Gradient(white:blue)'],
  105. 'chart.shadow': false,
  106. 'chart.shadow.color': '#666',
  107. 'chart.shadow.offsetx': 3,
  108. 'chart.shadow.offsety': 3,
  109. 'chart.shadow.blur': 3,
  110. 'chart.tooltips': null,
  111. 'chart.tooltips.effect': 'fade',
  112. 'chart.tooltips.css.class': 'RGraph_tooltip',
  113. 'chart.tooltips.event': 'onclick',
  114. 'chart.tooltips.highlight': true,
  115. 'chart.tooltips.override': null,
  116. 'chart.highlight.stroke': 'rgba(0,0,0,0)',
  117. 'chart.highlight.fill': 'rgba(255,255,255,0.7)',
  118. 'chart.contextmenu': null,
  119. 'chart.units.pre': '',
  120. 'chart.units.post': '',
  121. 'chart.scale.decimals': 0,
  122. 'chart.scale.point': '.',
  123. 'chart.scale.thousand': ',',
  124. //'chart.scale.formatter': null,
  125. 'chart.crosshairs': false,
  126. 'chart.crosshairs.color': '#333',
  127. 'chart.crosshairs.hline': true,
  128. 'chart.crosshairs.vline': true,
  129. 'chart.annotatable': false,
  130. 'chart.annotate.color': 'black',
  131. 'chart.zoom.factor': 1.5,
  132. 'chart.zoom.fade.in': true,
  133. 'chart.zoom.fade.out': true,
  134. 'chart.zoom.hdir': 'right',
  135. 'chart.zoom.vdir': 'down',
  136. 'chart.zoom.frames': 25,
  137. 'chart.zoom.delay': 16.666,
  138. 'chart.zoom.shadow': true,
  139. 'chart.zoom.background': true,
  140. 'chart.resizable': false,
  141. 'chart.resize.handle.background': null,
  142. 'chart.noaxes': false,
  143. 'chart.noxaxis': false,
  144. 'chart.noyaxis': false,
  145. 'chart.axis.color': 'black',
  146. 'chart.total': true,
  147. 'chart.multiplier.x': 1,
  148. 'chart.multiplier.w': 1,
  149. 'chart.events.click': null,
  150. 'chart.events.mousemove': null
  151. }
  152. // Check for support
  153. if (!this.canvas) {
  154. alert('[WATERFALL] No canvas support');
  155. return;
  156. }
  157. // Store the data
  158. this.data = data;
  159. /**
  160. * Create the $ objects
  161. */
  162. for (var i=0; i<=this.data.length; ++i) {
  163. this['$' + i] = {}
  164. }
  165. /**
  166. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  167. * done already
  168. */
  169. if (!this.canvas.__rgraph_aa_translated__) {
  170. this.context.translate(0.5,0.5);
  171. this.canvas.__rgraph_aa_translated__ = true;
  172. }
  173. ///////////////////////////////// SHORT PROPERTIES /////////////////////////////////
  174. var RG = RGraph;
  175. var ca = this.canvas;
  176. var co = ca.getContext('2d');
  177. var prop = this.properties;
  178. //////////////////////////////////// METHODS ///////////////////////////////////////
  179. /**
  180. * A setter
  181. *
  182. * @param name string The name of the property to set
  183. * @param value mixed The value of the property
  184. */
  185. this.Set = function (name, value)
  186. {
  187. /**
  188. * This should be done first - prepend the propertyy name with "chart." if necessary
  189. */
  190. if (name.substr(0,6) != 'chart.') {
  191. name = 'chart.' + name;
  192. }
  193. if (name == 'chart.total' && prop['chart.numxticks'] == null) {
  194. prop['chart.numxticks'] = this.data.length;
  195. }
  196. prop[name.toLowerCase()] = value;
  197. return this;
  198. }
  199. /**
  200. * A getter
  201. *
  202. * @param name string 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. * The function you call to draw the bar chart
  216. */
  217. this.Draw = function ()
  218. {
  219. /**
  220. * Fire the onbeforedraw event
  221. */
  222. RGraph.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. * Draw the background image
  233. */
  234. RGraph.DrawBackgroundImage(this);
  235. /**
  236. * This is new in May 2011 and facilitates indiviual 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. /**
  244. * Stop the coords array from growing uncontrollably
  245. */
  246. this.coords = [];
  247. /**
  248. * This gets used a lot
  249. */
  250. this.centery = ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop;
  251. /**
  252. * Work out a few things. They need to be here because they depend on things you can change after you instantiate the object
  253. */
  254. this.max = 0;
  255. this.grapharea = ca.height - this.gutterTop - this.gutterBottom;
  256. this.graphwidth = ca.width - this.gutterLeft - this.gutterRight;
  257. this.halfTextHeight = prop['chart.text.size'] / 2;
  258. /**
  259. * Work out the maximum value
  260. */
  261. this.max = this.getMax(this.data);
  262. var decimals = prop['chart.scale.decimals'];
  263. this.scale2 = RGraph.getScale2(this, {'max':typeof(prop['chart.ymax']) == 'number' ? prop['chart.ymax'] : this.max,
  264. 'min':prop['chart.xmin'],
  265. 'strict': typeof(prop['chart.ymax']) == 'number' ? true : false,
  266. 'scale.decimals':Number(decimals),
  267. 'scale.point':prop['chart.scale.point'],
  268. 'scale.thousand':prop['chart.scale.thousand'],
  269. 'scale.round':prop['chart.scale.round'],
  270. 'units.pre':prop['chart.units.pre'],
  271. 'units.post':prop['chart.units.post'],
  272. 'ylabels.count':prop['chart.labels.count']
  273. });
  274. this.max = this.scale2.max;
  275. // Draw the background hbars
  276. RGraph.DrawBars(this)
  277. // Progressively Draw the chart
  278. RG.background.Draw(this);
  279. this.DrawAxes();
  280. this.Drawbars();
  281. this.DrawLabels();
  282. /**
  283. * If the X axis is at the bottom - draw the it again so that it appears "on top" of the bars
  284. */
  285. if (prop['chart.xaxispos'] == 'bottom' && prop['chart.noaxes'] == false && prop['chart.noxaxis'] == false) {
  286. co.strokeStyle = prop['chart.axis.color'];
  287. co.strokeRect(prop['chart.gutter.left'], ca.height - prop['chart.gutter.bottom'], ca.width - this.gutterLeft - this.gutterRight, 0);
  288. }
  289. /**
  290. * Setup the context menu if required
  291. */
  292. if (prop['chart.contextmenu']) {
  293. RG.ShowContext(this);
  294. }
  295. /**
  296. * This function enables resizing
  297. */
  298. if (prop['chart.resizable']) {
  299. RG.AllowResizing(this);
  300. }
  301. /**
  302. * This installs the event listeners
  303. */
  304. RG.InstallEventListeners(this);
  305. /**
  306. * Fire the RGraph ondraw event
  307. */
  308. RG.FireCustomEvent(this, 'ondraw');
  309. return this;
  310. }
  311. /**
  312. * Draws the charts axes
  313. */
  314. this.DrawAxes = function ()
  315. {
  316. if (prop['chart.noaxes']) {
  317. return;
  318. }
  319. co.beginPath();
  320. co.strokeStyle = prop['chart.axis.color'];
  321. co.lineWidth = prop['chart.axis.linewidth'] + 0.001;
  322. // Draw the Y axis
  323. if (prop['chart.noyaxis'] == false) {
  324. co.moveTo(Math.round(this.gutterLeft), this.gutterTop);
  325. co.lineTo(Math.round(this.gutterLeft), ca.height - this.gutterBottom);
  326. }
  327. // Draw the X axis
  328. if (prop['chart.noxaxis'] == false) {
  329. // Center X axis
  330. if (prop['chart.xaxispos'] == 'center') {
  331. co.moveTo(this.gutterLeft, Math.round( ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop));
  332. co.lineTo(ca.width - this.gutterRight, Math.round( ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop));
  333. } else {
  334. co.moveTo(this.gutterLeft, Math.round(ca.height - this.gutterBottom));
  335. co.lineTo(ca.width - this.gutterRight, Math.round(ca.height - this.gutterBottom));
  336. }
  337. }
  338. var numYTicks = prop['chart.numyticks'];
  339. // Draw the Y tickmarks
  340. if (prop['chart.noyaxis'] == false && prop['chart.numyticks'] > 0) {
  341. var yTickGap = (ca.height - this.gutterTop - this.gutterBottom) / numYTicks;
  342. for (y=this.gutterTop; y < (ca.height - this.gutterBottom); y += yTickGap) {
  343. if (prop['chart.xaxispos'] == 'bottom' || (y != ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop)) {
  344. co.moveTo(this.gutterLeft, Math.round( y));
  345. co.lineTo(this.gutterLeft - 3, Math.round( y));
  346. }
  347. }
  348. /**
  349. * If the X axis is not being shown, draw an extra tick
  350. */
  351. if (prop['chart.noxaxis'] || prop['chart.xaxispos'] == 'center') {
  352. co.moveTo(this.gutterLeft - 3, Math.round(ca.height - this.gutterBottom));
  353. co.lineTo(this.gutterLeft, Math.round(ca.height - this.gutterBottom));
  354. }
  355. }
  356. // Draw the X tickmarks
  357. if (prop['chart.numxticks'] == null) {
  358. prop['chart.numxticks'] = this.data.length + (prop['chart.total'] ? 1 : 0)
  359. }
  360. if (prop['chart.noxaxis'] == false && prop['chart.numxticks'] > 0) {
  361. xTickGap = (ca.width - this.gutterLeft - this.gutterRight ) / prop['chart.numxticks'];
  362. if (prop['chart.xaxispos'] == 'center') {
  363. yStart = ((ca.height - this.gutterBottom - this.gutterTop) / 2) + this.gutterTop - 3;
  364. yEnd = ((ca.height - this.gutterBottom - this.gutterTop) / 2) + this.gutterTop + 3;
  365. } else {
  366. yStart = ca.height - this.gutterBottom;
  367. yEnd = (ca.height - this.gutterBottom) + 3;
  368. }
  369. for (x=this.gutterLeft + xTickGap; x<=ca.width - this.gutterRight + 1; x+=xTickGap) {
  370. co.moveTo(Math.round( x), yStart);
  371. co.lineTo(Math.round( x), yEnd);
  372. }
  373. if (prop['chart.noyaxis']) {
  374. co.moveTo(Math.round( this.gutterLeft), yStart);
  375. co.lineTo(Math.round( this.gutterLeft), yEnd);
  376. }
  377. }
  378. /**
  379. * If the Y axis is not being shown, draw an extra tick
  380. */
  381. if (prop['chart.noyaxis'] && prop['chart.noxaxis'] == false) {
  382. co.moveTo(Math.round( this.gutterLeft), ca.height - this.gutterBottom);
  383. co.lineTo(Math.round( this.gutterLeft), ca.height - this.gutterBottom + 3);
  384. }
  385. co.stroke();
  386. }
  387. /**
  388. * Draws the labels for the graph
  389. */
  390. this.DrawLabels = function ()
  391. {
  392. var context = co;
  393. var numYLabels = 5; // Make this configurable
  394. var interval = this.grapharea / numYLabels;
  395. var font = prop['chart.text.font'];
  396. var size = prop['chart.text.size'];
  397. var color = prop['chart.text.color'];
  398. var units_pre = prop['chart.units.pre'];
  399. var units_post = prop['chart.units.post'];
  400. co.beginPath();
  401. co.fillStyle = color;
  402. /**
  403. * First, draw the Y labels
  404. */
  405. if (prop['chart.ylabels']) {
  406. if (prop['chart.xaxispos'] == 'center') {
  407. var halfInterval = interval / 2;
  408. var halfWay = ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop;
  409. for (var i=0,len=this.scale2.labels.length; i<len; ++i) {
  410. RG.Text2(this, {
  411. 'font':font,
  412. 'size':size,
  413. 'x':this.gutterLeft - 5,
  414. 'y': this.gutterTop + (((this.grapharea/2) / len) * i),
  415. 'text':this.scale2.labels[len - i - 1],
  416. 'valign':'center',
  417. 'halign':'right',
  418. 'tag': 'scale'
  419. });
  420. RG.Text2(this, {
  421. 'font':font,
  422. 'size':size,
  423. 'x':this.gutterLeft - 5,
  424. 'y': halfWay + (((this.grapharea/2) / len) * (i + 1)),
  425. 'text':this.scale2.labels[i],
  426. 'valign':'center',
  427. 'halign':'right',
  428. 'tag': 'scale'
  429. });
  430. }
  431. } else {
  432. for (var i=0,len=this.scale2.labels.length; i<len; ++i) {
  433. RG.Text2(this, {
  434. 'font':font,
  435. 'size':size,
  436. 'x':this.gutterLeft - 5,
  437. 'y': ca.height - this.gutterBottom - ((this.grapharea / len) * (i + 1)),
  438. 'text':this.scale2.labels[i],
  439. 'valign':'center',
  440. 'halign':'right',
  441. 'tag': 'scale'
  442. });
  443. }
  444. }
  445. }
  446. /**
  447. * Now, draw the X labels
  448. */
  449. if (prop['chart.labels'].length > 0) {
  450. // Recalculate the interval for the X labels
  451. interval = (ca.width - this.gutterLeft - this.gutterRight) / prop['chart.labels'].length;
  452. var halign = 'center';
  453. var angle = prop['chart.text.angle'];
  454. if (angle) {
  455. halign = 'right';
  456. angle *= -1;
  457. }
  458. var labels = prop['chart.labels'];
  459. for (var i=0,len=labels.length; i<len; i+=1) {
  460. RG.Text2(this, {'font':font,
  461. 'size':size,
  462. 'x':this.gutterLeft + (i * interval) + (interval / 2),
  463. 'y':ca.height - this.gutterBottom + 5 + this.halfTextHeight,
  464. 'text':labels[i],
  465. 'valign':'center',
  466. 'halign':halign,
  467. 'angle':angle,
  468. 'tag': 'labels'
  469. });
  470. }
  471. }
  472. co.stroke();
  473. co.fill();
  474. }
  475. /**
  476. * Draws the bars on to the chart
  477. */
  478. this.Drawbars = function ()
  479. {
  480. var context = co;
  481. var canvas = ca;
  482. var hmargin = prop['chart.hmargin'];
  483. var runningTotal = 0;
  484. co.lineWidth = prop['chart.linewidth'] + 0.001;
  485. for (var i=0,len=this.data.length; i<len; ++i) {
  486. co.beginPath();
  487. co.strokeStyle = prop['chart.strokestyle'];
  488. var x = Math.round( this.gutterLeft + hmargin + (((this.graphwidth / (this.data.length + (prop['chart.total'] ? 1 : 0))) * i) * prop['chart.multiplier.x']));
  489. var y = Math.round( this.gutterTop + this.grapharea - (i == 0 ? ((this.data[0] / this.max) * this.grapharea) : (this.data[i] > 0 ? ((runningTotal + this.data[i]) / this.max) * this.grapharea : (runningTotal / this.max) * this.grapharea)));
  490. var w = ((ca.width - this.gutterLeft - this.gutterRight) / (this.data.length + (prop['chart.total'] ? 1 : 0 )) ) - (2 * prop['chart.hmargin']);
  491. w = w * prop['chart.multiplier.w'];
  492. var h = (Math.abs(this.data[i]) / this.max) * this.grapharea;
  493. if (prop['chart.xaxispos'] == 'center') {
  494. h /= 2;
  495. y = (i == 0 ? ((this.data[0] / this.max) * this.grapharea) : (this.data[i] > 0 ? ((runningTotal + this.data[i]) / this.max) * this.grapharea : (runningTotal / this.max) * this.grapharea));
  496. y = this.gutterTop + (this.grapharea/2) - (y / 2);
  497. }
  498. // Color
  499. co.fillStyle = this.data[i] >= 0 ? prop['chart.colors'][0] : prop['chart.colors'][1];
  500. if (prop['chart.shadow']) {
  501. RG.SetShadow(this, prop['chart.shadow.color'], prop['chart.shadow.offsetx'], prop['chart.shadow.offsety'], prop['chart.shadow.blur']);
  502. } else {
  503. RG.NoShadow(this);
  504. }
  505. co.rect(x, y, w, h);
  506. this.coords.push([x, y, w, h]);
  507. runningTotal += this.data[i];
  508. co.stroke();
  509. co.fill();
  510. }
  511. // Store the total
  512. this.total = runningTotal;
  513. if (prop['chart.total']) {
  514. // This is the height of the final bar
  515. h = (runningTotal / this.max) * (this.grapharea / (prop['chart.xaxispos'] == 'center' ? 2 : 1));
  516. /**
  517. * Set the Y (ie the start point) value
  518. */
  519. if (prop['chart.xaxispos'] == 'center') {
  520. y = runningTotal > 0 ? this.centery - h : this.centery - h;
  521. } else {
  522. y = ca.height - this.gutterBottom - h;
  523. }
  524. // This is the X position of the final bar
  525. x = x + (prop['chart.hmargin'] * 2) + w;
  526. // Final color
  527. co.fillStyle = prop['chart.colors'][2];
  528. co.beginPath();
  529. co.strokeRect(x, y, w, h);
  530. co.fillRect(x, y, w, h);
  531. co.stroke();
  532. co.fill();
  533. this.coords.push([x, y - (runningTotal > 0 ? 0 : Math.abs(h)), w, Math.abs(h)]);
  534. }
  535. RG.NoShadow(this);
  536. /**
  537. * This draws the connecting lines
  538. */
  539. co.lineWidth = 1;
  540. for (var i=1,len=this.coords.length; i<len; i+=1) {
  541. co.strokeStyle = 'gray';
  542. co.beginPath();
  543. if (this.data[i - 1] > 0) {
  544. co.moveTo(this.coords[i - 1][0] + this.coords[i - 1][2], Math.round( this.coords[i - 1][1]));
  545. co.lineTo(this.coords[i - 1][0] + this.coords[i - 1][2] + (2 * hmargin), Math.round( this.coords[i - 1][1]));
  546. } else {
  547. co.moveTo(this.coords[i - 1][0] + this.coords[i - 1][2], Math.round( this.coords[i - 1][1] + this.coords[i - 1][3]));
  548. co.lineTo(this.coords[i - 1][0] + this.coords[i - 1][2] + (2 * hmargin), Math.round( this.coords[i - 1][1] + this.coords[i - 1][3]));
  549. }
  550. co.stroke();
  551. }
  552. }
  553. /**
  554. * Not used by the class during creating the graph, but is used by event handlers
  555. * to get the coordinates (if any) of the selected bar
  556. *
  557. * @param object e The event object
  558. */
  559. this.getShape =
  560. this.getBar = function (e)
  561. {
  562. /**
  563. * Loop through the bars determining if the mouse is over a bar
  564. */
  565. for (var i=0,len=this.coords.length; i<len; i++) {
  566. var mouseCoords = RG.getMouseXY(e);
  567. var mouseX = mouseCoords[0];
  568. var mouseY = mouseCoords[1];
  569. var left = this.coords[i][0];
  570. var top = this.coords[i][1];
  571. var width = this.coords[i][2];
  572. var height = this.coords[i][3];
  573. if ( mouseX >= left
  574. && mouseX <= (left + width)
  575. && mouseY >= top
  576. && mouseY <= top + height) {
  577. var tooltip = RG.parseTooltipText(prop['chart.tooltips'], i);
  578. return {0: this, 'object': this,
  579. 1: left, 'x': left,
  580. 2: top, 'y': top,
  581. 3: width, 'width': width,
  582. 4: height, 'height': height,
  583. 5: i, 'index': i,
  584. 'tooltip': tooltip};
  585. }
  586. }
  587. return null;
  588. }
  589. /**
  590. * The Waterfall is slightly different to Bar/Line charts so has this function to get the max value
  591. */
  592. this.getMax = function (data)
  593. {
  594. var runningTotal = 0;
  595. var max = 0;
  596. for (var i=0,len=data.length; i<len; i+=1) {
  597. runningTotal += data[i];
  598. max = Math.max(max, Math.abs(runningTotal));
  599. }
  600. return max;
  601. }
  602. /**
  603. * This function facilitates the installation of tooltip event listeners if
  604. * tooltips are defined.
  605. */
  606. this.AllowTooltips = function ()
  607. {
  608. // Preload any tooltip images that are used in the tooltips
  609. RG.PreLoadTooltipImages(this);
  610. /**
  611. * This installs the window mousedown event listener that lears any
  612. * highlight that may be visible.
  613. */
  614. RG.InstallWindowMousedownTooltipListener(this);
  615. /**
  616. * This installs the canvas mousemove event listener. This function
  617. * controls the pointer shape.
  618. */
  619. RG.InstallCanvasMousemoveTooltipListener(this);
  620. /**
  621. * This installs the canvas mouseup event listener. This is the
  622. * function that actually shows the appropriate tooltip (if any).
  623. */
  624. RG.InstallCanvasMouseupTooltipListener(this);
  625. }
  626. /**
  627. * Each object type has its own Highlight() function which highlights the appropriate shape
  628. *
  629. * @param object shape The shape to highlight
  630. */
  631. this.Highlight = function (shape)
  632. {
  633. // Add the new highlight
  634. RG.Highlight.Rect(this, shape);
  635. }
  636. /**
  637. * The getObjectByXY() worker method. Don't call this call:
  638. *
  639. * RGraph.ObjectRegistry.getObjectByXY(e)
  640. *
  641. * @param object e The event object
  642. */
  643. this.getObjectByXY = function (e)
  644. {
  645. var mouseXY = RG.getMouseXY(e);
  646. if (
  647. mouseXY[0] > this.gutterLeft
  648. && mouseXY[0] < (ca.width - this.gutterRight)
  649. && mouseXY[1] > this.gutterTop
  650. && mouseXY[1] < (ca.height - this.gutterBottom)
  651. ) {
  652. return this;
  653. }
  654. }
  655. /**
  656. * This function positions a tooltip when it is displayed
  657. *
  658. * @param obj object The chart object
  659. * @param int x The X coordinate specified for the tooltip
  660. * @param int y The Y coordinate specified for the tooltip
  661. * @param objec tooltip The tooltips DIV element
  662. */
  663. this.positionTooltip = function (obj, x, y, tooltip, idx)
  664. {
  665. var coordX = obj.coords[tooltip.__index__][0];
  666. var coordY = obj.coords[tooltip.__index__][1];
  667. var coordW = obj.coords[tooltip.__index__][2];
  668. var coordH = obj.coords[tooltip.__index__][3];
  669. var canvasXY = RG.getCanvasXY(obj.canvas);
  670. var gutterLeft = obj.gutterLeft;
  671. var gutterTop = obj.gutterTop;
  672. var width = tooltip.offsetWidth;
  673. var height = tooltip.offsetHeight;
  674. var value = obj.data[idx];
  675. /**
  676. * Change the value to be the total if necessary
  677. */
  678. if (tooltip.__index__ == obj.data.length) {
  679. value = obj.total;
  680. }
  681. // Set the top position
  682. tooltip.style.left = 0;
  683. tooltip.style.top = canvasXY[1] + coordY - height - 7 + 'px';
  684. /**
  685. * If the tooltip is for a negative value - position it underneath the bar
  686. */
  687. if (value < 0) {
  688. tooltip.style.top = canvasXY[1] + coordY + coordH + 7 + 'px';
  689. }
  690. // By default any overflow is hidden
  691. tooltip.style.overflow = '';
  692. // The arrow
  693. var img = new Image();
  694. img.id = '__rgraph_tooltip_pointer__';
  695. img.style.position = 'absolute';
  696. if (value >= 0) {
  697. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
  698. img.style.top = (tooltip.offsetHeight - 2) + 'px';
  699. } else {
  700. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAFCAMAAACkeOZkAAAAK3RFWHRDcmVhdGlvbiBUaW1lAFNhdCA2IE9jdCAyMDEyIDEyOjQ5OjMyIC0wMDAw2S1RlgAAAAd0SU1FB9wKBgszM4Ed2k4AAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAEZ0FNQQAAsY8L/GEFAAAACVBMVEX/AAC9vb3//+92Pom0AAAAAXRSTlMAQObYZgAAAB1JREFUeNpjYAABRgY4YGRiRDCZYBwQE8qBMEEcAANCACqByy1sAAAAAElFTkSuQmCC';
  701. img.style.top = '-5px';
  702. }
  703. tooltip.appendChild(img);
  704. // Reposition the tooltip if at the edges:
  705. // LEFT edge
  706. if ((canvasXY[0] + coordX - (width / 2)) < 10) {
  707. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + (coordW / 2) + 'px';
  708. img.style.left = ((width * 0.1) - 8.5) + 'px';
  709. // RIGHT edge
  710. } else if ((canvasXY[0] + coordX + (width / 2)) > document.body.offsetWidth) {
  711. tooltip.style.left = canvasXY[0] + coordX - (width * 0.9) + (coordW / 2) + 'px';
  712. img.style.left = ((width * 0.9) - 8.5) + 'px';
  713. // Default positioning - CENTERED
  714. } else {
  715. tooltip.style.left = (canvasXY[0] + coordX + (coordW / 2) - (width * 0.5)) + 'px';
  716. img.style.left = ((width * 0.5) - 8.5) + 'px';
  717. }
  718. }
  719. /**
  720. * This method returns the appropriate Y coord for the given value
  721. *
  722. * @param number value The value
  723. */
  724. this.getYCoord = function (value)
  725. {
  726. if (value > this.max) {
  727. return null;
  728. }
  729. if (prop['chart.xaxispos'] == 'center') {
  730. if (value < (-1 * this.max)) {
  731. return null;
  732. }
  733. var coord = (value / this.max) * (this.grapharea / 2);
  734. return this.gutterTop + (this.grapharea / 2) - coord;
  735. } else {
  736. if (value < 0) {
  737. return null;
  738. }
  739. var coord = (value / this.max) * this.grapharea;
  740. coord = coord + this.gutterTop;
  741. return ca.height - coord;
  742. }
  743. }
  744. /**
  745. * This allows for easy specification of gradients
  746. */
  747. this.parseColors = function ()
  748. {
  749. // chart.colors
  750. var colors = prop['chart.colors'];
  751. if (colors) {
  752. for (var i=0,len=colors.length; i<len; ++i) {
  753. colors[i] = this.parseSingleColorForGradient(colors[i]);
  754. }
  755. }
  756. // chart.key.colors
  757. var colors = prop['chart.key.colors'];
  758. if (colors) {
  759. for (var i=0,len=colors.length; i<len; ++i) {
  760. colors[i] = this.parseSingleColorForGradient(colors[i]);
  761. }
  762. }
  763. prop['chart.crosshairs.color'] = this.parseSingleColorForGradient(prop['chart.crosshairs.color']);
  764. prop['chart.highlight.stroke'] = this.parseSingleColorForGradient(prop['chart.highlight.stroke']);
  765. prop['chart.highlight.fill'] = this.parseSingleColorForGradient(prop['chart.highlight.fill']);
  766. prop['chart.background.barcolor1'] = this.parseSingleColorForGradient(prop['chart.background.barcolor1']);
  767. prop['chart.background.barcolor2'] = this.parseSingleColorForGradient(prop['chart.background.barcolor2']);
  768. prop['chart.background.grid.color'] = this.parseSingleColorForGradient(prop['chart.background.grid.color']);
  769. prop['chart.strokestyle'] = this.parseSingleColorForGradient(prop['chart.strokestyle']);
  770. prop['chart.axis.color'] = this.parseSingleColorForGradient(prop['chart.axis.color']);
  771. }
  772. /**
  773. * This parses a single color value
  774. *
  775. * @param string color The color to parse for gradients
  776. */
  777. this.parseSingleColorForGradient = function (color)
  778. {
  779. if (!color || typeof(color) != 'string') {
  780. return color;
  781. }
  782. if (color.match(/^gradient\((.*)\)$/i)) {
  783. var parts = RegExp.$1.split(':');
  784. // Create the gradient
  785. var grad = co.createLinearGradient(0,ca.height - prop['chart.gutter.bottom'], 0, prop['chart.gutter.top']);
  786. var diff = 1 / (parts.length - 1);
  787. grad.addColorStop(0, RG.trim(parts[0]));
  788. for (var j=1,len=parts.length; j<len; ++j) {
  789. grad.addColorStop(j * diff, RG.trim(parts[j]));
  790. }
  791. }
  792. return grad ? grad : color;
  793. }
  794. /**
  795. * Now, because canvases can support multiple charts, canvases must always be registered
  796. */
  797. RG.Register(this);
  798. }