RGraph.waterfall.js 42 KB

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