You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2562 lines
115 KiB

  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.txt |
  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.Bar = function (id, data)
  22. {
  23. // Get the canvas and context objects
  24. this.id = id;
  25. this.canvas = document.getElementById(id);
  26. this.context = this.canvas.getContext ? this.canvas.getContext("2d") : null;
  27. this.canvas.__object__ = this;
  28. this.type = 'bar';
  29. this.max = 0;
  30. this.stackedOrGrouped = false;
  31. this.isRGraph = true;
  32. this.uid = RGraph.CreateUID();
  33. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  34. this.colorsParsed = false;
  35. /**
  36. * Compatibility with older browsers
  37. */
  38. RGraph.OldBrowserCompat(this.context);
  39. // Various config type stuff
  40. this.properties = {
  41. 'chart.background.barcolor1': 'rgba(0,0,0,0)',
  42. 'chart.background.barcolor2': 'rgba(0,0,0,0)',
  43. 'chart.background.grid': true,
  44. 'chart.background.grid.color': '#ddd',
  45. 'chart.background.grid.width': 1,
  46. 'chart.background.grid.hsize': 20,
  47. 'chart.background.grid.vsize': 20,
  48. 'chart.background.grid.vlines': true,
  49. 'chart.background.grid.hlines': true,
  50. 'chart.background.grid.border': true,
  51. 'chart.background.grid.autofit':true,
  52. 'chart.background.grid.autofit.numhlines': 5,
  53. 'chart.background.grid.autofit.numvlines': 20,
  54. 'chart.background.image.stretch': true,
  55. 'chart.background.image.x': null,
  56. 'chart.background.image.y': null,
  57. 'chart.background.image.w': null,
  58. 'chart.background.image.h': null,
  59. 'chart.background.image.align': null,
  60. 'chart.ytickgap': 20,
  61. 'chart.smallyticks': 3,
  62. 'chart.largeyticks': 5,
  63. 'chart.numyticks': 10,
  64. 'chart.hmargin': 5,
  65. 'chart.hmargin.grouped': 1,
  66. 'chart.strokecolor': 'rgba(0,0,0,0)',
  67. 'chart.axis.color': 'black',
  68. 'chart.gutter.top': 25,
  69. 'chart.gutter.bottom': 25,
  70. 'chart.gutter.left': 25,
  71. 'chart.gutter.right': 25,
  72. 'chart.labels': null,
  73. 'chart.labels.colors': [], // Gradients aren't supported for these colors
  74. 'chart.labels.ingraph': null,
  75. 'chart.labels.above': false,
  76. 'chart.labels.above.decimals': 0,
  77. 'chart.labels.above.size': null,
  78. 'chart.labels.above.angle': null,
  79. 'chart.ylabels': true,
  80. 'chart.ylabels.colors': [], // Gradients aren't supported for these colors
  81. 'chart.ylabels.count': 5,
  82. 'chart.ylabels.inside': false,
  83. 'chart.xlabels.offset': 0,
  84. 'chart.xaxispos': 'bottom',
  85. 'chart.yaxispos': 'left',
  86. 'chart.text.color': 'black', // Gradients aren't supported for this color
  87. 'chart.text.size': 10,
  88. 'chart.text.angle': 0,
  89. 'chart.text.font': 'Arial',
  90. 'chart.ymin': 0,
  91. 'chart.ymax': null,
  92. 'chart.title': '',
  93. 'chart.title.font': null,
  94. 'chart.title.background': null, // Gradients aren't supported for this color
  95. 'chart.title.hpos': null,
  96. 'chart.title.vpos': null,
  97. 'chart.title.bold': true,
  98. 'chart.title.xaxis': '',
  99. 'chart.title.xaxis.bold': true,
  100. 'chart.title.xaxis.size': null,
  101. 'chart.title.xaxis.font': null,
  102. 'chart.title.yaxis': '',
  103. 'chart.title.yaxis.bold': true,
  104. 'chart.title.yaxis.size': null,
  105. 'chart.title.yaxis.font': null,
  106. 'chart.title.yaxis.color': null, // Gradients aren't supported for this color
  107. 'chart.title.xaxis.pos': null,
  108. 'chart.title.yaxis.pos': null,
  109. 'chart.colors': ['#01B4FF', '#0f0', '#00f', '#ff0', '#0ff', '#0f0'],
  110. 'chart.colors.sequential': false,
  111. 'chart.colors.reverse': false,
  112. 'chart.grouping': 'grouped',
  113. 'chart.variant': 'bar',
  114. 'chart.variant.sketch.verticals': true,
  115. 'chart.shadow': false,
  116. 'chart.shadow.color': '#999', // Gradients aren't supported for this color
  117. 'chart.shadow.offsetx': 3,
  118. 'chart.shadow.offsety': 3,
  119. 'chart.shadow.blur': 3,
  120. 'chart.tooltips': null,
  121. 'chart.tooltips.effect': 'fade',
  122. 'chart.tooltips.css.class': 'RGraph_tooltip',
  123. 'chart.tooltips.event': 'onclick',
  124. 'chart.tooltips.highlight': true,
  125. 'chart.highlight.stroke': 'rgba(0,0,0,0)',
  126. 'chart.highlight.fill': 'rgba(255,255,255,0.7)',
  127. 'chart.background.hbars': null,
  128. 'chart.key': null,
  129. 'chart.key.background': 'white',
  130. 'chart.key.position': 'graph',
  131. 'chart.key.shadow': false,
  132. 'chart.key.shadow.color': '#666',
  133. 'chart.key.shadow.blur': 3,
  134. 'chart.key.shadow.offsetx': 2,
  135. 'chart.key.shadow.offsety': 2,
  136. 'chart.key.position.gutter.boxed': true,
  137. 'chart.key.position.x': null,
  138. 'chart.key.position.y': null,
  139. 'chart.key.halign': 'right',
  140. 'chart.key.color.shape': 'square',
  141. 'chart.key.rounded': true,
  142. 'chart.key.text.size': 10,
  143. 'chart.key.linewidth': 1,
  144. 'chart.key.colors': null,
  145. 'chart.contextmenu': null,
  146. 'chart.units.pre': '',
  147. 'chart.units.post': '',
  148. 'chart.scale.decimals': 0,
  149. 'chart.scale.point': '.',
  150. 'chart.scale.thousand': ',',
  151. 'chart.crosshairs': false,
  152. 'chart.crosshairs.color': '#333',
  153. 'chart.crosshairs.hline': true,
  154. 'chart.crosshairs.vline': true,
  155. 'chart.linewidth': 1,
  156. 'chart.annotatable': false,
  157. 'chart.annotate.color': 'black',
  158. 'chart.zoom.factor': 1.5,
  159. 'chart.zoom.fade.in': true,
  160. 'chart.zoom.fade.out': true,
  161. 'chart.zoom.hdir': 'right',
  162. 'chart.zoom.vdir': 'down',
  163. 'chart.zoom.frames': 25,
  164. 'chart.zoom.delay': 16.666,
  165. 'chart.zoom.shadow': true,
  166. 'chart.zoom.background': true,
  167. 'chart.resizable': false,
  168. 'chart.resize.handle.adjust': [0,0],
  169. 'chart.resize.handle.background': null,
  170. 'chart.adjustable': false,
  171. 'chart.noaxes': false,
  172. 'chart.noxaxis': false,
  173. 'chart.noyaxis': false,
  174. 'chart.events.click': null,
  175. 'chart.events.mousemove': null,
  176. 'chart.numxticks': null
  177. }
  178. // Check for support
  179. if (!this.canvas) {
  180. alert('[BAR] No canvas support');
  181. return;
  182. }
  183. /**
  184. * Determine whether the chart will contain stacked or grouped bars
  185. */
  186. for (var i=0; i<data.length; ++i) {
  187. if (typeof(data[i]) == 'object') {
  188. this.stackedOrGrouped = true;
  189. }
  190. }
  191. /**
  192. * Create the dollar objects so that functions can be added to them
  193. */
  194. var linear_data = RGraph.array_linearize(data);
  195. for (var i=0; i<linear_data.length; ++i) {
  196. this['$' + i] = {};
  197. }
  198. // Store the data
  199. this.data = data;
  200. // Used to store the coords of the bars
  201. this.coords = [];
  202. /**
  203. * This linearises the data. Doing so can make it easier to pull
  204. * out the appropriate data from tooltips
  205. */
  206. this.data_arr = RGraph.array_linearize(this.data);
  207. /**
  208. * This adds the animation effects to the object so that you can do this: obj.Grow()
  209. */
  210. //RGraph.AddEffects(this);
  211. /**
  212. * Register the object
  213. */
  214. RGraph.Register(this);
  215. }
  216. /**
  217. * A setter
  218. *
  219. * @param name string The name of the property to set
  220. * @param value mixed The value of the property
  221. */
  222. RGraph.Bar.prototype.Set = function (name, value)
  223. {
  224. name = name.toLowerCase();
  225. if (name == 'chart.labels.abovebar') {
  226. name = 'chart.labels.above';
  227. }
  228. if (name == 'chart.strokestyle') {
  229. name = 'chart.strokecolor';
  230. }
  231. /**
  232. * Check for xaxispos
  233. */
  234. if (name == 'chart.xaxispos' ) {
  235. if (value != 'bottom' && value != 'center' && value != 'top') {
  236. alert('[BAR] (' + this.id + ') chart.xaxispos should be top, center or bottom. Tried to set it to: ' + value + ' Changing it to center');
  237. value = 'center';
  238. }
  239. if (value == 'top') {
  240. for (var i=0; i<this.data.length; ++i) {
  241. if (typeof(this.data[i]) == 'number' && this.data[i] > 0) {
  242. alert('[BAR] The data element with index ' + i + ' should be negative');
  243. }
  244. }
  245. }
  246. }
  247. /**
  248. * lineWidth doesn't appear to like a zero setting
  249. */
  250. if (name.toLowerCase() == 'chart.linewidth' && value == 0) {
  251. value = 0.0001;
  252. }
  253. this.properties[name] = value;
  254. }
  255. /**
  256. * A getter
  257. *
  258. * @param name string The name of the property to get
  259. */
  260. RGraph.Bar.prototype.Get = function (name)
  261. {
  262. return this.properties[name];
  263. }
  264. /**
  265. * The function you call to draw the bar chart
  266. */
  267. RGraph.Bar.prototype.Draw = function ()
  268. {
  269. // MUST be the first thing done!
  270. if (typeof(this.properties['chart.background.image']) == 'string') {
  271. RGraph.DrawBackgroundImage(this);
  272. }
  273. /**
  274. * Fire the onbeforedraw event
  275. */
  276. RGraph.FireCustomEvent(this, 'onbeforedraw');
  277. /**
  278. * Parse the colors. This allows for simple gradient syntax
  279. */
  280. if (!this.colorsParsed) {
  281. this.parseColors();
  282. // Don't want to do this again
  283. this.colorsParsed = true;
  284. }
  285. /**
  286. * This is new in May 2011 and facilitates indiviual gutter settings,
  287. * eg chart.gutter.left
  288. */
  289. this.gutterLeft = this.properties['chart.gutter.left'];
  290. this.gutterRight = this.properties['chart.gutter.right'];
  291. this.gutterTop = this.properties['chart.gutter.top'];
  292. this.gutterBottom = this.properties['chart.gutter.bottom'];
  293. /**
  294. * Convert any null values to 0. Won't make any difference to the bar (as opposed to the line chart)
  295. */
  296. for (var i=0; i<this.data.length; ++i) {
  297. if (this.data[i] == null) {
  298. this.data[i] = 0;
  299. }
  300. }
  301. // Cache this in a class variable as it's used rather a lot
  302. /**
  303. * Check for tooltips and alert the user that they're not supported with pyramid charts
  304. */
  305. if ( (this.properties['chart.variant'] == 'pyramid' || this.properties['chart.variant'] == 'dot')
  306. && typeof(this.properties['chart.tooltips']) == 'object'
  307. && this.properties['chart.tooltips']
  308. && this.properties['chart.tooltips'].length > 0) {
  309. alert('[BAR] (' + this.id + ') Sorry, tooltips are not supported with dot or pyramid charts');
  310. }
  311. /**
  312. * Stop the coords array from growing uncontrollably
  313. */
  314. this.coords = [];
  315. /**
  316. * Work out a few things. They need to be here because they depend on things you can change before you
  317. * call Draw() but after you instantiate the object
  318. */
  319. this.max = 0;
  320. this.grapharea = this.canvas.height - this.gutterTop - this.gutterBottom;
  321. this.halfgrapharea = this.grapharea / 2;
  322. this.halfTextHeight = this.properties['chart.text.size'] / 2;
  323. /**
  324. * This sets the label colors. Doing it here saves lots of if() conditions in the draw method
  325. */
  326. if (this.properties['chart.labels']) {
  327. if (this.properties['chart.labels.colors'].length < this.properties['chart.labels'].length) {
  328. while (this.properties['chart.labels.colors'].length < this.properties['chart.labels'].length) {
  329. this.properties['chart.labels.colors'].push(this.properties['chart.labels.colors'][this.properties['chart.labels.colors'].length - 1]);
  330. }
  331. } else {
  332. while (this.properties['chart.labels.colors'].length < this.properties['chart.labels'].length) {
  333. this.properties['chart.labels.colors'].push(this.properties['chart.text.color']);
  334. }
  335. }
  336. }
  337. if (this.properties['chart.ylabels.colors'].length < this.properties['chart.ylabels.count']) {
  338. while (this.properties['chart.ylabels.colors'].length < this.properties['chart.ylabels.count']) {
  339. this.properties['chart.ylabels.colors'].push(this.properties['chart.ylabels.colors'][this.properties['chart.ylabels.colors'].length - 1]);
  340. }
  341. } else {
  342. while (this.properties['chart.ylabels.colors'].length < this.properties['chart.ylabels'].count) {
  343. this.properties['chart.ylabels.colors'].push(this.properties['chart.text.color']);
  344. }
  345. }
  346. // Progressively Draw the chart
  347. RGraph.background.Draw(this);
  348. //If it's a sketch chart variant, draw the axes first
  349. if (this.properties['chart.variant'] == 'sketch') {
  350. this.DrawAxes();
  351. this.Drawbars();
  352. } else {
  353. this.Drawbars();
  354. this.DrawAxes();
  355. }
  356. this.DrawLabels();
  357. // Draw the key if necessary
  358. if (this.properties['chart.key'] && this.properties['chart.key'].length) {
  359. RGraph.DrawKey(this, this.properties['chart.key'], this.properties['chart.colors']);
  360. }
  361. /**
  362. * Setup the context menu if required
  363. */
  364. if (this.properties['chart.contextmenu']) {
  365. RGraph.ShowContext(this);
  366. }
  367. /**
  368. * Is a line is defined, draw it
  369. *
  370. var line = this.Get('chart.line');
  371. if (line) {
  372. line.__bar__ = this;
  373. // Check the X axis positions
  374. if (this.Get('chart.xaxispos') != line.Get('chart.xaxispos')) {
  375. alert("[BAR] Using different X axis positions when combining the Bar and Line is not advised");
  376. }
  377. line.Set('chart.gutter.left', this.Get('chart.gutter.left'));
  378. line.Set('chart.gutter.right', this.Get('chart.gutter.right'));
  379. line.Set('chart.gutter.top', this.Get('chart.gutter.top'));
  380. line.Set('chart.gutter.bottom', this.Get('chart.gutter.bottom'));
  381. line.Set('chart.hmargin', (this.canvas.width - this.gutterLeft - this.gutterRight) / (line.original_data[0].length * 2));
  382. // If a BAR custom yMax is set, use that
  383. if (this.Get('chart.ymax') && !line.Get('chart.ymax')) {
  384. line.Set('chart.ymax', this.Get('chart.ymax'));
  385. } else if (line.Get('chart.ymax')) {
  386. line.Set('chart.ymax', line.Get('chart.ymax'));
  387. }
  388. // The boolean is used to specify that the Line chart .Draw() method is being called by the Bar chart
  389. line.Draw(true);
  390. }
  391. */
  392. /**
  393. * Draw "in graph" labels
  394. */
  395. if (this.properties['chart.labels.ingraph']) {
  396. RGraph.DrawInGraphLabels(this);
  397. }
  398. /**
  399. * This function enables resizing
  400. */
  401. if (this.properties['chart.resizable']) {
  402. RGraph.AllowResizing(this);
  403. }
  404. /**
  405. * This installs the event listeners
  406. */
  407. RGraph.InstallEventListeners(this);
  408. /**
  409. * Fire the RGraph ondraw event
  410. */
  411. RGraph.FireCustomEvent(this, 'ondraw');
  412. }
  413. /**
  414. * Draws the charts axes
  415. */
  416. RGraph.Bar.prototype.DrawAxes = function ()
  417. {
  418. if (this.properties['chart.noaxes']) {
  419. return;
  420. }
  421. var xaxispos = this.properties['chart.xaxispos'];
  422. var yaxispos = this.properties['chart.yaxispos'];
  423. var isSketch = this.properties['chart.variant'] == 'sketch';
  424. this.context.beginPath();
  425. this.context.strokeStyle = this.properties['chart.axis.color'];
  426. this.context.lineWidth = 1;
  427. // Draw the Y axis
  428. if (this.properties['chart.noyaxis'] == false) {
  429. if (yaxispos == 'right') {
  430. this.context.moveTo(AA(this, this.canvas.width - this.gutterRight + (isSketch ? 3 : 0)), this.gutterTop - (isSketch ? 3 : 0));
  431. this.context.lineTo(AA(this, this.canvas.width - this.gutterRight) - (isSketch ? 2 : 0), this.canvas.height - this.gutterBottom + (isSketch ? 5 : 0));
  432. } else {
  433. this.context.moveTo(AA(this, this.gutterLeft - (isSketch ? 2 : 0)), this.gutterTop - (isSketch ? 5 : 0));
  434. this.context.lineTo(AA(this, this.gutterLeft - (isSketch ? 1 : 0)), this.canvas.height - this.gutterBottom + (isSketch ? 5 : 0));
  435. }
  436. }
  437. // Draw the X axis
  438. if (this.properties['chart.noxaxis'] == false) {
  439. if (xaxispos == 'center') {
  440. this.context.moveTo(this.gutterLeft - (isSketch ? 5 : 0), AA(this, ((this.canvas.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop + (isSketch ? 2 : 0)));
  441. this.context.lineTo(this.canvas.width - this.gutterRight + (isSketch ? 5 : 0), AA(this, ((this.canvas.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop - (isSketch ? 2 : 0)));
  442. } else if (xaxispos == 'top') {
  443. this.context.moveTo(this.gutterLeft - (isSketch ? 3 : 0), AA(this, this.gutterTop) - (isSketch ? 3 : 0));
  444. this.context.lineTo(this.canvas.width - this.gutterRight + (isSketch ? 5 : 0), AA(this, this.gutterTop + (isSketch ? 2 : 0)));
  445. } else {
  446. this.context.moveTo(this.gutterLeft - (isSketch ? 5 : 0), AA(this, this.canvas.height - this.gutterBottom) - (isSketch ? 2 : 0));
  447. this.context.lineTo(this.canvas.width - this.gutterRight + (isSketch ? 8 : 0), AA(this, this.canvas.height - this.gutterBottom + (isSketch ? 2 : 0)));
  448. }
  449. }
  450. var numYTicks = this.properties['chart.numyticks'];
  451. // Draw the Y tickmarks
  452. if (this.properties['chart.noyaxis'] == false && !isSketch) {
  453. var yTickGap = (this.canvas.height - this.gutterTop - this.gutterBottom) / numYTicks;
  454. var xpos = yaxispos == 'left' ? this.gutterLeft : this.canvas.width - this.gutterRight;
  455. if (this.properties['chart.numyticks'] > 0) {
  456. for (y=this.gutterTop;
  457. xaxispos == 'center' ? y <= (this.canvas.height - this.gutterBottom) : y < (this.canvas.height - this.gutterBottom + (xaxispos == 'top' ? 1 : 0));
  458. y += yTickGap) {
  459. if (xaxispos == 'center' && y == (this.gutterTop + (this.grapharea/ 2))) continue;
  460. // X axis at the top
  461. if (xaxispos == 'top' && y == this.gutterTop) continue;
  462. this.context.moveTo(xpos, AA(this, y));
  463. this.context.lineTo(xpos + (yaxispos == 'left' ? -3 : 3), AA(this, y));
  464. }
  465. }
  466. /**
  467. * If the X axis is not being shown, draw an extra tick
  468. */
  469. if (this.properties['chart.noxaxis']) {
  470. if (xaxispos == 'center') {
  471. this.context.moveTo(xpos + (yaxispos == 'left' ? -3 : 3), AA(this, this.canvas.height / 2));
  472. this.context.lineTo(xpos, AA(this, this.canvas.height / 2));
  473. } else if (xaxispos == 'top') {
  474. this.context.moveTo(xpos + (yaxispos == 'left' ? -3 : 3), AA(this, this.gutterTop));
  475. this.context.lineTo(xpos, AA(this, this.gutterTop));
  476. } else {
  477. this.context.moveTo(xpos + (yaxispos == 'left' ? -3 : 3), AA(this, this.canvas.height - this.gutterBottom));
  478. this.context.lineTo(xpos, AA(this, this.canvas.height - this.gutterBottom));
  479. }
  480. }
  481. }
  482. // Draw the X tickmarks
  483. if (this.properties['chart.noxaxis'] == false && !isSketch) {
  484. xTickGap = (this.canvas.width - this.gutterLeft - this.gutterRight) / this.data.length;
  485. if (typeof(this.properties['chart.numxticks']) == 'number') {
  486. xTickGap = (this.canvas.width - this.gutterLeft - this.gutterRight) / this.properties['chart.numxticks'];
  487. }
  488. if (xaxispos == 'bottom') {
  489. yStart = this.canvas.height - this.gutterBottom;
  490. yEnd = (this.canvas.height - this.gutterBottom) + 3;
  491. } else if (xaxispos == 'top') {
  492. yStart = this.gutterTop - 3;
  493. yEnd = this.gutterTop;
  494. } else if (xaxispos == 'center') {
  495. yStart = ((this.canvas.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop + 3;
  496. yEnd = ((this.canvas.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop - 3;
  497. }
  498. yStart = AA(this, yStart);
  499. yEnd = AA(this, yEnd);
  500. //////////////// X TICKS ////////////////
  501. var noEndXTick = this.properties['chart.noendxtick'];
  502. for (x=this.gutterLeft + (yaxispos == 'left' ? xTickGap : 0); x<this.canvas.width - this.gutterRight + (yaxispos == 'left' ? 5 : 0); x+=xTickGap) {
  503. if (yaxispos == 'left' && !noEndXTick && x > this.gutterLeft) {
  504. this.context.moveTo(AA(this, x), yStart);
  505. this.context.lineTo(AA(this, x), yEnd);
  506. } else if (yaxispos == 'left' && noEndXTick && x > this.gutterLeft && x < (this.canvas.width - this.gutterRight) ) {
  507. this.context.moveTo(AA(this, x), yStart);
  508. this.context.lineTo(AA(this, x), yEnd);
  509. } else if (yaxispos == 'right' && x < (this.canvas.width - this.gutterRight) && !noEndXTick) {
  510. this.context.moveTo(AA(this, x), yStart);
  511. this.context.lineTo(AA(this, x), yEnd);
  512. } else if (yaxispos == 'right' && x < (this.canvas.width - this.gutterRight) && x > (this.gutterLeft) && noEndXTick) {
  513. this.context.moveTo(AA(this, x), yStart);
  514. this.context.lineTo(AA(this, x), yEnd);
  515. }
  516. }
  517. if (this.properties['chart.noyaxis'] || this.properties['chart.numxticks'] == null) {
  518. if (typeof(this.properties['chart.numxticks']) == 'number' && this.properties['chart.numxticks'] > 0) {
  519. this.context.moveTo(AA(this, this.gutterLeft), yStart);
  520. this.context.lineTo(AA(this, this.gutterLeft), yEnd);
  521. }
  522. }
  523. //////////////// X TICKS ////////////////
  524. }
  525. /**
  526. * If the Y axis is not being shown, draw an extra tick
  527. */
  528. if (this.properties['chart.noyaxis'] && this.properties['chart.noxaxis'] == false && this.properties['chart.numxticks'] == null) {
  529. if (xaxispos == 'center') {
  530. this.context.moveTo(AA(this, this.gutterLeft), (this.canvas.height / 2) - 3);
  531. this.context.lineTo(AA(this, this.gutterLeft), (this.canvas.height / 2) + 3);
  532. } else {
  533. this.context.moveTo(AA(this, this.gutterLeft), this.canvas.height - this.gutterBottom);
  534. this.context.lineTo(AA(this, this.gutterLeft), this.canvas.height - this.gutterBottom + 3);
  535. }
  536. }
  537. this.context.stroke();
  538. }
  539. /**
  540. * Draws the bars
  541. */
  542. RGraph.Bar.prototype.Drawbars = function ()
  543. {
  544. this.context.lineWidth = this.properties['chart.linewidth'];
  545. this.context.strokeStyle = this.properties['chart.strokecolor'];
  546. this.context.fillStyle = this.properties['chart.colors'][0];
  547. var prevX = 0;
  548. var prevY = 0;
  549. var decimals = this.properties['chart.scale.decimals'];
  550. /**
  551. * Work out the max value
  552. */
  553. if (this.properties['chart.ymax']) {
  554. this.max = this.properties['chart.ymax'];
  555. this.min = this.properties['chart.ymin'];
  556. this.scale = [
  557. (((this.max - this.min) * (1/5)) + this.min).toFixed(decimals),
  558. (((this.max - this.min) * (2/5)) + this.min).toFixed(decimals),
  559. (((this.max - this.min) * (3/5)) + this.min).toFixed(decimals),
  560. (((this.max - this.min) * (4/5)) + this.min).toFixed(decimals),
  561. (((this.max - this.min) * (5/5) + this.min)).toFixed(decimals)
  562. ];
  563. } else {
  564. this.min = this.properties['chart.ymin'];
  565. for (i=0; i<this.data.length; ++i) {
  566. if (typeof(this.data[i]) == 'object') {
  567. var value = this.properties['chart.grouping'] == 'grouped' ? Number(RGraph.array_max(this.data[i], true)) : Number(RGraph.array_sum(this.data[i])) ;
  568. } else {
  569. var value = Number(this.data[i]);
  570. }
  571. this.max = Math.max(Math.abs(this.max), Math.abs(value));
  572. }
  573. this.scale = RGraph.getScale(this.max, this);
  574. this.max = this.scale[4];
  575. if (this.properties['chart.ymin']) {
  576. var decimals = this.properties['chart.scale.decimals'];
  577. this.scale[0] = ((Number(this.scale[4] - this.min) * 0.2) + this.min).toFixed(decimals);
  578. this.scale[1] = ((Number(this.scale[4] - this.min) * 0.4) + this.min).toFixed(decimals);
  579. this.scale[2] = ((Number(this.scale[4] - this.min) * 0.6) + this.min).toFixed(decimals);
  580. this.scale[3] = ((Number(this.scale[4] - this.min) * 0.8) + this.min).toFixed(decimals);
  581. this.scale[4] = ((Number(this.scale[4] - this.min) * 1.0) + this.min).toFixed(decimals);
  582. } else {
  583. if (this.properties['chart.scale.decimals']) {
  584. var decimals = this.properties['chart.scale.decimals'];
  585. this.scale[0] = Number(this.scale[0]).toFixed(decimals);
  586. this.scale[1] = Number(this.scale[1]).toFixed(decimals);
  587. this.scale[2] = Number(this.scale[2]).toFixed(decimals);
  588. this.scale[3] = Number(this.scale[3]).toFixed(decimals);
  589. this.scale[4] = Number(this.scale[4]).toFixed(decimals);
  590. }
  591. }
  592. }
  593. /**
  594. * if the chart is adjustable fix the scale so that it doesn't change.
  595. */
  596. if (this.properties['chart.adjustable'] && !this.properties['chart.ymax']) {
  597. this.Set('chart.ymax', this.scale[4]);
  598. }
  599. /**
  600. * Draw horizontal bars here
  601. */
  602. if (this.properties['chart.background.hbars'] && this.properties['chart.background.hbars'].length > 0) {
  603. RGraph.DrawBars(this);
  604. }
  605. var variant = this.properties['chart.variant'];
  606. /**
  607. * Draw the 3D axes is necessary
  608. */
  609. if (variant == '3d') {
  610. RGraph.Draw3DAxes(this);
  611. }
  612. /**
  613. * Get the variant once, and draw the bars, be they regular, stacked or grouped
  614. */
  615. // Get these variables outside of the loop
  616. var xaxispos = this.properties['chart.xaxispos'];
  617. var width = (this.canvas.width - this.gutterLeft - this.gutterRight ) / this.data.length;
  618. var orig_height = height;
  619. var hmargin = this.properties['chart.hmargin'];
  620. var shadow = this.properties['chart.shadow'];
  621. var shadowColor = this.properties['chart.shadow.color'];
  622. var shadowBlur = this.properties['chart.shadow.blur'];
  623. var shadowOffsetX = this.properties['chart.shadow.offsetx'];
  624. var shadowOffsetY = this.properties['chart.shadow.offsety'];
  625. var strokeStyle = this.properties['chart.strokecolor'];
  626. var colors = this.properties['chart.colors'];
  627. var sequentialColorIndex = 0;
  628. for (i=0; i<this.data.length; ++i) {
  629. // Work out the height
  630. //The width is up outside the loop
  631. var height = ((RGraph.array_sum(this.data[i]) < 0 ? RGraph.array_sum(this.data[i]) + this.min : RGraph.array_sum(this.data[i]) - this.min) / (this.max - this.min) ) * (this.canvas.height - this.gutterTop - this.gutterBottom);
  632. // Half the height if the Y axis is at the center
  633. if (xaxispos == 'center') {
  634. height /= 2;
  635. }
  636. var x = (i * width) + this.gutterLeft;
  637. var y = xaxispos == 'center' ? ((this.canvas.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop - height
  638. : this.canvas.height - height - this.gutterBottom;
  639. // xaxispos is top
  640. if (xaxispos == 'top') {
  641. y = this.gutterTop + Math.abs(height);
  642. }
  643. // Account for negative lengths - Some browsers (eg Chrome) don't like a negative value
  644. if (height < 0) {
  645. y += height;
  646. height = Math.abs(height);
  647. }
  648. /**
  649. * Turn on the shadow if need be
  650. */
  651. if (shadow) {
  652. this.context.shadowColor = shadowColor;
  653. this.context.shadowBlur = shadowBlur;
  654. this.context.shadowOffsetX = shadowOffsetX;
  655. this.context.shadowOffsetY = shadowOffsetY;
  656. }
  657. /**
  658. * Draw the bar
  659. */
  660. this.context.beginPath();
  661. if (typeof(this.data[i]) == 'number') {
  662. var barWidth = width - (2 * hmargin);
  663. /**
  664. * Check for a negative bar width
  665. */
  666. if (barWidth < 0) {
  667. alert('[RGRAPH] Warning: you have a negative bar width. This may be caused by the chart.hmargin being too high or the width of the canvas not being sufficient.');
  668. }
  669. // Set the fill color
  670. this.context.strokeStyle = strokeStyle;
  671. this.context.fillStyle = colors[0];
  672. /**
  673. * Sequential colors
  674. */
  675. if (this.properties['chart.colors.sequential']) {
  676. this.context.fillStyle = colors[i];
  677. }
  678. if (variant == 'sketch') {
  679. this.context.lineCap = 'round';
  680. var sketchOffset = 3;
  681. this.context.beginPath();
  682. this.context.strokeStyle = colors[0];
  683. /**
  684. * Sequential colors
  685. */
  686. if (this.properties['chart.colors.sequential']) {
  687. this.context.strokeStyle = colors[i];
  688. }
  689. // Left side
  690. this.context.moveTo(x + hmargin + 2, y + height - 2);
  691. this.context.lineTo(x + hmargin - 1, y - 4);
  692. // The top
  693. this.context.moveTo(x + hmargin - 3, y + -2 + (this.data[i] < 0 ? height : 0));
  694. this.context.bezierCurveTo(
  695. x + ((hmargin + width) * 0.33),
  696. y + 15 + (this.data[i] < 0 ? height - 10: 0),
  697. x + ((hmargin + width) * 0.66),
  698. y + 5 + (this.data[i] < 0 ? height - 10 : 0),x + hmargin + width + -1, y + 0 + (this.data[i] < 0 ? height : 0)
  699. );
  700. // The right side
  701. this.context.moveTo(x + hmargin + width - 5, y - 5);
  702. this.context.lineTo(x + hmargin + width - 3, y + height - 3);
  703. if (this.properties['chart.variant.sketch.verticals']) {
  704. for (var r=0.2; r<=0.8; r+=0.2) {
  705. this.context.moveTo(x + hmargin + width + (r > 0.4 ? -1 : 3) - (r * width),y - 1);
  706. this.context.lineTo(x + hmargin + width - (r > 0.4 ? 1 : -1) - (r * width), y + height + (r == 0.2 ? 1 : -2));
  707. }
  708. }
  709. this.context.stroke();
  710. // Regular bar
  711. } else if (variant == 'bar' || variant == '3d' || variant == 'glass' || variant == 'bevel') {
  712. if (RGraph.isOld() && shadow) {
  713. this.DrawIEShadow([x + hmargin, y, barWidth, height]);
  714. }
  715. if (variant == 'glass') {
  716. RGraph.filledCurvyRect(this.context, x + hmargin, y, barWidth, height, 3, this.data[i] > 0, this.data[i] > 0, this.data[i] < 0, this.data[i] < 0);
  717. RGraph.strokedCurvyRect(this.context, x + hmargin, y, barWidth, height, 3, this.data[i] > 0, this.data[i] > 0, this.data[i] < 0, this.data[i] < 0);
  718. } else {
  719. this.context.strokeRect(x + hmargin, y, barWidth, height);
  720. this.context.fillRect(x + hmargin, y, barWidth, height);
  721. }
  722. // 3D effect
  723. if (variant == '3d') {
  724. var prevStrokeStyle = this.context.strokeStyle;
  725. var prevFillStyle = this.context.fillStyle;
  726. // Draw the top
  727. this.context.beginPath();
  728. this.context.moveTo(x + hmargin, y);
  729. this.context.lineTo(x + hmargin + 10, y - 5);
  730. this.context.lineTo(x + hmargin + 10 + barWidth, y - 5);
  731. this.context.lineTo(x + hmargin + barWidth, y);
  732. this.context.closePath();
  733. this.context.stroke();
  734. this.context.fill();
  735. // Draw the right hand side
  736. this.context.beginPath();
  737. this.context.moveTo(x + hmargin + barWidth, y);
  738. this.context.lineTo(x + hmargin + barWidth + 10, y - 5);
  739. this.context.lineTo(x + hmargin + barWidth + 10, y + height - 5);
  740. this.context.lineTo(x + hmargin + barWidth, y + height);
  741. this.context.closePath();
  742. this.context.stroke();
  743. this.context.fill();
  744. // Draw the darker top section
  745. this.context.beginPath();
  746. this.context.fillStyle = 'rgba(255,255,255,0.3)';
  747. this.context.moveTo(x + hmargin, y);
  748. this.context.lineTo(x + hmargin + 10, y - 5);
  749. this.context.lineTo(x + hmargin + 10 + barWidth, y - 5);
  750. this.context.lineTo(x + hmargin + barWidth, y);
  751. this.context.lineTo(x + hmargin, y);
  752. this.context.closePath();
  753. this.context.stroke();
  754. this.context.fill();
  755. // Draw the darker right side section
  756. this.context.beginPath();
  757. this.context.fillStyle = 'rgba(0,0,0,0.4)';
  758. this.context.moveTo(x + hmargin + barWidth, y);
  759. this.context.lineTo(x + hmargin + barWidth + 10, y - 5);
  760. this.context.lineTo(x + hmargin + barWidth + 10, y - 5 + height);
  761. this.context.lineTo(x + hmargin + barWidth, y + height);
  762. this.context.lineTo(x + hmargin + barWidth, y);
  763. this.context.closePath();
  764. this.context.stroke();
  765. this.context.fill();
  766. this.context.strokeStyle = prevStrokeStyle;
  767. this.context.fillStyle = prevFillStyle;
  768. // Glass variant
  769. } else if (variant == 'glass') {
  770. var grad = this.context.createLinearGradient(
  771. x + hmargin,
  772. y,
  773. x + hmargin + (barWidth / 2),
  774. y
  775. );
  776. grad.addColorStop(0, 'rgba(255,255,255,0.9)');
  777. grad.addColorStop(1, 'rgba(255,255,255,0.5)');
  778. this.context.beginPath();
  779. this.context.fillStyle = grad;
  780. this.context.fillRect(x + hmargin + 2,y + (this.data[i] > 0 ? 2 : 0),(barWidth / 2) - 2,height - 2);
  781. this.context.fill();
  782. }
  783. // This bit draws the text labels that appear above the bars if requested
  784. if (this.properties['chart.labels.above']) {
  785. // Turn off any shadow
  786. if (shadow) {
  787. RGraph.NoShadow(this);
  788. }
  789. var yPos = y - 3;
  790. // Account for negative bars
  791. if (this.data[i] < 0) {
  792. yPos += height + 6 + (this.properties['chart.text.size'] - 4);
  793. }
  794. // Account for chart.xaxispos=top
  795. if (this.properties['chart.xaxispos'] == 'top') {
  796. yPos = this.gutterTop + height + 6 + (typeof(this.properties['chart.labels.above.size']) == 'number' ? this.properties['chart.labels.above.size'] : this.properties['chart.text.size'] - 4);
  797. }
  798. // Account for chart.variant=3d
  799. if (this.properties['chart.variant'] == '3d') {
  800. yPos -= 5;
  801. }
  802. this.context.fillStyle = this.properties['chart.text.color'];
  803. RGraph.Text(this.context,
  804. this.properties['chart.text.font'],
  805. typeof(this.properties['chart.labels.above.size']) == 'number' ? this.properties['chart.labels.above.size'] : this.properties['chart.text.size'] - 3,
  806. x + hmargin + (barWidth / 2),
  807. yPos,
  808. RGraph.number_format(this, Number(this.data[i]).toFixed(this.properties['chart.labels.above.decimals']),this.properties['chart.units.pre'],this.properties['chart.units.post']),
  809. this.properties['chart.labels.above.angle'] ? 'bottom' : null,
  810. this.properties['chart.labels.above.angle'] ? (this.properties['chart.labels.above.angle'] > 0 && (this.properties['chart.xaxispos'] != 'top') ? 'right' : 'left') : 'center',
  811. null,
  812. this.properties['chart.labels.above.angle']
  813. );
  814. }
  815. // Dot chart
  816. } else if (variant == 'dot') {
  817. this.context.beginPath();
  818. this.context.moveTo(x + (width / 2), y);
  819. this.context.lineTo(x + (width / 2), y + height);
  820. this.context.stroke();
  821. this.context.beginPath();
  822. this.context.fillStyle = this.properties['chart.colors'][i];
  823. this.context.arc(x + (width / 2), y + (this.data[i] > 0 ? 0 : height), 2, 0, 6.28, 0);
  824. // Set the colour for the dots
  825. this.context.fillStyle = this.properties['chart.colors'][0];
  826. /**
  827. * Sequential colors
  828. */
  829. if (this.properties['chart.colors.sequential']) {
  830. this.context.fillStyle = colors[i];
  831. }
  832. this.context.stroke();
  833. this.context.fill();
  834. // Pyramid chart
  835. } else if (variant == 'pyramid') {
  836. this.context.beginPath();
  837. var startY = (this.properties['chart.xaxispos'] == 'center' ? (this.canvas.height / 2) : (this.canvas.height - this.gutterBottom));
  838. this.context.moveTo(x + hmargin, startY);
  839. this.context.lineTo(
  840. x + hmargin + (barWidth / 2),
  841. y + (this.properties['chart.xaxispos'] == 'center' && (this.data[i] < 0) ? height : 0)
  842. );
  843. this.context.lineTo(x + hmargin + barWidth, startY);
  844. this.context.closePath();
  845. this.context.stroke();
  846. this.context.fill();
  847. // Arrow chart
  848. } else if (variant == 'arrow') {
  849. var startY = (this.properties['chart.xaxispos'] == 'center' ? (this.canvas.height / 2) : (this.canvas.height - this.gutterBottom));
  850. this.context.lineWidth = this.properties['chart.linewidth'] ? this.properties['chart.linewidth'] : 1;
  851. this.context.lineCap = 'round';
  852. this.context.beginPath();
  853. this.context.moveTo(AA(this, x + hmargin + (barWidth / 2)), startY);
  854. this.context.lineTo(AA(this, x + hmargin + (barWidth / 2)), y + (this.properties['chart.xaxispos'] == 'center' && (this.data[i] < 0) ? height : 0));
  855. this.context.arc(x + hmargin + (barWidth / 2),
  856. y + (this.properties['chart.xaxispos'] == 'center' && (this.data[i] < 0) ? height : 0),
  857. 5,
  858. this.data[i] > 0 ? 0.78 : 5.6,
  859. this.data[i] > 0 ? 0.79 : 5.48,
  860. this.data[i] < 0);
  861. this.context.moveTo(AA(this, x + hmargin + (barWidth / 2)), y + (this.properties['chart.xaxispos'] == 'center' && (this.data[i] < 0) ? height : 0));
  862. this.context.arc(x + hmargin + (barWidth / 2),
  863. y + (this.properties['chart.xaxispos'] == 'center' && (this.data[i] < 0) ? height : 0),
  864. 5,
  865. this.data[i] > 0 ? 2.355 : 4,
  866. this.data[i] > 0 ? 2.4 : 3.925,
  867. this.data[i] < 0);
  868. this.context.stroke();
  869. this.context.lineWidth = 1;
  870. // Unknown variant type
  871. } else {
  872. alert('[BAR] Warning! Unknown chart.variant: ' + variant);
  873. }
  874. this.coords.push([x + hmargin, y, width - (2 * hmargin), height]);
  875. /**
  876. * Stacked bar
  877. */
  878. } else if (typeof(this.data[i]) == 'object' && this.properties['chart.grouping'] == 'stacked') {
  879. if (this.min) {
  880. alert("[ERROR] Stacked Bar charts with a Y min are not supported");
  881. }
  882. var barWidth = width - (2 * hmargin);
  883. var redrawCoords = [];// Necessary to draw if the shadow is enabled
  884. var startY = 0;
  885. var dataset = this.data[i];
  886. /**
  887. * Check for a negative bar width
  888. */
  889. if (barWidth < 0) {
  890. alert('[RGRAPH] Warning: you have a negative bar width. This may be caused by the chart.hmargin being too high or the width of the canvas not being sufficient.');
  891. }
  892. for (j=0; j<dataset.length; ++j) {
  893. // Stacked bar chart and X axis pos in the middle - poitless since negative values are not permitted
  894. if (xaxispos == 'center') {
  895. alert("[BAR] It's pointless having the X axis position at the center on a stacked bar chart.");
  896. return;
  897. }
  898. // Negative values not permitted for the stacked chart
  899. if (this.data[i][j] < 0) {
  900. alert('[BAR] Negative values are not permitted with a stacked bar chart. Try a grouped one instead.');
  901. return;
  902. }
  903. /**
  904. * Set the fill and stroke colors
  905. */
  906. this.context.strokeStyle = strokeStyle
  907. this.context.fillStyle = colors[j];
  908. if (this.properties['chart.colors.reverse']) {
  909. this.context.fillStyle = colors[this.data[i].length - j - 1];
  910. }
  911. if (this.properties['chart.colors.sequential'] && colors[sequentialColorIndex]) {
  912. this.context.fillStyle = colors[sequentialColorIndex++];
  913. } else if (this.properties['chart.colors.sequential']) {
  914. this.context.fillStyle = colors[sequentialColorIndex - 1];
  915. }
  916. var height = (dataset[j] / this.max) * (this.canvas.height - this.gutterTop - this.gutterBottom );
  917. // If the X axis pos is in the center, we need to half the height
  918. if (xaxispos == 'center') {
  919. height /= 2;
  920. }
  921. var totalHeight = (RGraph.array_sum(dataset) / this.max) * (this.canvas.height - hmargin - this.gutterTop - this.gutterBottom);
  922. /**
  923. * Store the coords for tooltips
  924. */
  925. this.coords.push([x + hmargin, y, width - (2 * hmargin), height]);
  926. // MSIE shadow
  927. if (RGraph.isOld() && shadow) {
  928. this.DrawIEShadow([x + hmargin, y, width - (2 * hmargin), height + 1]);
  929. }
  930. this.context.strokeRect(x + hmargin, y, width - (2 * hmargin), height);
  931. this.context.fillRect(x + hmargin, y, width - (2 * hmargin), height);
  932. if (j == 0) {
  933. var startY = y;
  934. var startX = x;
  935. }
  936. /**
  937. * Store the redraw coords if the shadow is enabled
  938. */
  939. if (shadow) {
  940. redrawCoords.push([x + hmargin, y, width - (2 * hmargin), height, this.context.fillStyle]);
  941. }
  942. /**
  943. * Stacked 3D effect
  944. */
  945. if (variant == '3d') {
  946. var prevFillStyle = this.context.fillStyle;
  947. var prevStrokeStyle = this.context.strokeStyle;
  948. // Draw the top side
  949. if (j == 0) {
  950. this.context.beginPath();
  951. this.context.moveTo(startX + hmargin, y);
  952. this.context.lineTo(startX + 10 + hmargin, y - 5);
  953. this.context.lineTo(startX + 10 + barWidth + hmargin, y - 5);
  954. this.context.lineTo(startX + barWidth + hmargin, y);
  955. this.context.closePath();
  956. this.context.fill();
  957. this.context.stroke();
  958. }
  959. // Draw the side section
  960. this.context.beginPath();
  961. this.context.moveTo(startX + barWidth + hmargin, y);
  962. this.context.lineTo(startX + barWidth + hmargin + 10, y - 5);
  963. this.context.lineTo(startX + barWidth + hmargin + 10, y - 5 + height);
  964. this.context.lineTo(startX + barWidth + hmargin , y + height);
  965. this.context.closePath();
  966. this.context.fill();
  967. this.context.stroke();
  968. // Draw the darker top side
  969. if (j == 0) {
  970. this.context.fillStyle = 'rgba(255,255,255,0.3)';
  971. this.context.beginPath();
  972. this.context.moveTo(startX + hmargin, y);
  973. this.context.lineTo(startX + 10 + hmargin, y - 5);
  974. this.context.lineTo(startX + 10 + barWidth + hmargin, y - 5);
  975. this.context.lineTo(startX + barWidth + hmargin, y);
  976. this.context.closePath();
  977. this.context.fill();
  978. this.context.stroke();
  979. }
  980. // Draw the darker side section
  981. this.context.fillStyle = 'rgba(0,0,0,0.4)';
  982. this.context.beginPath();
  983. this.context.moveTo(startX + barWidth + hmargin, y);
  984. this.context.lineTo(startX + barWidth + hmargin + 10, y - 5);
  985. this.context.lineTo(startX + barWidth + hmargin + 10, y - 5 + height);
  986. this.context.lineTo(startX + barWidth + hmargin , y + height);
  987. this.context.closePath();
  988. this.context.fill();
  989. this.context.stroke();
  990. this.context.strokeStyle = prevStrokeStyle;
  991. this.context.fillStyle = prevFillStyle;
  992. }
  993. y += height;
  994. }
  995. // This bit draws the text labels that appear above the bars if requested
  996. if (this.Get('chart.labels.above')) {
  997. // Turn off any shadow
  998. RGraph.NoShadow(this);
  999. this.context.fillStyle = this.properties['chart.text.color'];
  1000. RGraph.Text(this.context,
  1001. this.properties['chart.text.font'],
  1002. typeof(this.properties['chart.labels.above.size']) == 'number' ? this.properties['chart.labels.above.size'] : this.properties['chart.text.size'] - 3,
  1003. startX + (barWidth / 2) + this.properties['chart.hmargin'],
  1004. startY - (this.properties['chart.shadow'] && this.properties['chart.shadow.offsety'] < 0 ? 7 : 4) - (this.properties['chart.variant'] == '3d' ? 5 : 0),
  1005. String(this.properties['chart.units.pre'] + RGraph.array_sum(this.data[i]).toFixed(this.properties['chart.labels.above.decimals']) + this.properties['chart.units.post']),
  1006. this.properties['chart.labels.above.angle'] ? 'bottom' : null,
  1007. this.properties['chart.labels.above.angle'] ? (this.properties['chart.labels.above.angle'] > 0 ? 'right' : 'left') : 'center',
  1008. null,
  1009. this.properties['chart.labels.above.angle']);
  1010. // Turn any shadow back on
  1011. if (shadow) {
  1012. this.context.shadowColor = shadowColor;
  1013. this.context.shadowBlur = shadowBlur;
  1014. this.context.shadowOffsetX = shadowOffsetX;
  1015. this.context.shadowOffsetY = shadowOffsetY;
  1016. }
  1017. }
  1018. /**
  1019. * Redraw the bars if the shadow is enabled due to hem being drawn from the bottom up, and the
  1020. * shadow spilling over to higher up bars
  1021. */
  1022. if (shadow) {
  1023. RGraph.NoShadow(this);
  1024. for (k=0; k<redrawCoords.length; ++k) {
  1025. this.context.strokeStyle = strokeStyle;
  1026. this.context.fillStyle = redrawCoords[k][4];
  1027. this.context.strokeRect(redrawCoords[k][0], redrawCoords[k][1], redrawCoords[k][2], redrawCoords[k][3]);
  1028. this.context.fillRect(redrawCoords[k][0], redrawCoords[k][1], redrawCoords[k][2], redrawCoords[k][3]);
  1029. this.context.stroke();
  1030. this.context.fill();
  1031. }
  1032. // Reset the redraw coords to be empty
  1033. redrawCoords = [];
  1034. }
  1035. /**
  1036. * Grouped bar
  1037. */
  1038. } else if (typeof(this.data[i]) == 'object' && this.properties['chart.grouping'] == 'grouped') {
  1039. var redrawCoords = [];
  1040. this.context.lineWidth = this.properties['chart.linewidth'];
  1041. for (j=0; j<this.data[i].length; ++j) {
  1042. // Set the fill and stroke colors
  1043. this.context.strokeStyle = strokeStyle;
  1044. this.context.fillStyle = colors[j];
  1045. /**
  1046. * Sequential colors
  1047. */
  1048. if (this.properties['chart.colors.sequential'] && colors[sequentialColorIndex]) {
  1049. this.context.fillStyle = colors[sequentialColorIndex++];
  1050. } else if (this.properties['chart.colors.sequential']) {
  1051. this.context.fillStyle = colors[sequentialColorIndex - 1];
  1052. }
  1053. var individualBarWidth = (width - (2 * hmargin)) / this.data[i].length;
  1054. var height = ((this.data[i][j] + (this.data[i][j] < 0 ? this.min : (-1 * this.min) )) / (this.max - this.min) ) * (this.canvas.height - this.gutterTop - this.gutterBottom );
  1055. /**
  1056. * Check for a negative bar width
  1057. */
  1058. if (individualBarWidth < 0) {
  1059. alert('[RGRAPH] Warning: you have a negative bar width. This may be caused by the chart.hmargin being too high or the width of the canvas not being sufficient.');
  1060. }
  1061. // If the X axis pos is in the center, we need to half the height
  1062. if (xaxispos == 'center') {
  1063. height /= 2;
  1064. }
  1065. var startX = x + hmargin + (j * individualBarWidth);
  1066. /**
  1067. * Determine the start positioning for the bar
  1068. */
  1069. if (xaxispos == 'top') {
  1070. var startY = this.gutterTop;
  1071. var height = Math.abs(height);
  1072. } else if (xaxispos == 'center') {
  1073. var startY = this.gutterTop + (this.grapharea / 2) - height;
  1074. } else {
  1075. var startY = this.canvas.height - this.gutterBottom - height;
  1076. var height = Math.abs(height);
  1077. }
  1078. /**
  1079. * Draw MSIE shadow
  1080. */
  1081. if (RGraph.isOld() && shadow) {
  1082. this.DrawIEShadow([startX, startY, individualBarWidth, height]);
  1083. }
  1084. var groupedMargin = this.properties['chart.hmargin.grouped'];
  1085. this.context.strokeRect(startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height);
  1086. this.context.fillRect(startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height);
  1087. y += height;
  1088. /**
  1089. * Grouped 3D effect
  1090. */
  1091. if (variant == '3d') {
  1092. var prevFillStyle = this.context.fillStyle;
  1093. var prevStrokeStyle = this.context.strokeStyle;
  1094. // Draw the top side
  1095. this.context.beginPath();
  1096. this.context.moveTo(startX, startY);
  1097. this.context.lineTo(startX + 10, startY - 5);
  1098. this.context.lineTo(startX + 10 + individualBarWidth, startY - 5);
  1099. this.context.lineTo(startX + individualBarWidth, startY);
  1100. this.context.closePath();
  1101. this.context.fill();
  1102. this.context.stroke();
  1103. // Draw the side section
  1104. this.context.beginPath();
  1105. this.context.moveTo(startX + individualBarWidth, startY);
  1106. this.context.lineTo(startX + individualBarWidth + 10, startY - 5);
  1107. this.context.lineTo(startX + individualBarWidth + 10, startY - 5 + height);
  1108. this.context.lineTo(startX + individualBarWidth , startY + height);
  1109. this.context.closePath();
  1110. this.context.fill();
  1111. this.context.stroke();
  1112. // Draw the darker top side
  1113. this.context.fillStyle = 'rgba(255,255,255,0.3)';
  1114. this.context.beginPath();
  1115. this.context.moveTo(startX, startY);
  1116. this.context.lineTo(startX + 10, startY - 5);
  1117. this.context.lineTo(startX + 10 + individualBarWidth, startY - 5);
  1118. this.context.lineTo(startX + individualBarWidth, startY);
  1119. this.context.closePath();
  1120. this.context.fill();
  1121. this.context.stroke();
  1122. // Draw the darker side section
  1123. this.context.fillStyle = 'rgba(0,0,0,0.4)';
  1124. this.context.beginPath();
  1125. this.context.moveTo(startX + individualBarWidth, startY);
  1126. this.context.lineTo(startX + individualBarWidth + 10, startY - 5);
  1127. this.context.lineTo(startX + individualBarWidth + 10, startY - 5 + height);
  1128. this.context.lineTo(startX + individualBarWidth , startY + height);
  1129. this.context.closePath();
  1130. this.context.fill();
  1131. this.context.stroke();
  1132. this.context.strokeStyle = prevStrokeStyle;
  1133. this.context.fillStyle = prevFillStyle;
  1134. }
  1135. if (height < 0) {
  1136. height = Math.abs(height);
  1137. startY = startY - height;
  1138. }
  1139. this.coords.push([startX, startY, individualBarWidth, height]);
  1140. // Facilitate shadows going to the left
  1141. if (this.properties['chart.shadow']) {
  1142. redrawCoords.push([startX + groupedMargin, startY, individualBarWidth - (2 * groupedMargin), height, this.context.fillStyle]);
  1143. }
  1144. // This bit draws the text labels that appear above the bars if requested
  1145. if (this.Get('chart.labels.above')) {
  1146. this.context.strokeStyle = 'rgba(0,0,0,0)';
  1147. // Turn off any shadow
  1148. if (shadow) {
  1149. RGraph.NoShadow(this);
  1150. }
  1151. var yPos = y - 3;
  1152. // Account for negative bars
  1153. if (this.data[i][j] < 0) {
  1154. yPos += height + 6 + (this.properties['chart.text.size'] - 4);
  1155. }
  1156. this.context.fillStyle = this.properties['chart.text.color'];
  1157. RGraph.Text(this.context,
  1158. this.properties['chart.text.font'],
  1159. typeof(this.properties['chart.labels.above.size']) == 'number' ? this.properties['chart.labels.above.size'] : this.properties['chart.text.size'] - 3,startX + (individualBarWidth / 2),
  1160. startY - 2 - (this.properties['chart.variant'] == '3d' ? 5 : 0),
  1161. RGraph.number_format(this, this.data[i][j].toFixed(this.properties['chart.labels.above.decimals'])),
  1162. null,
  1163. this.properties['chart.labels.above.angle'] ? (this.properties['chart.labels.above.angle'] > 0 ? 'right' : 'left') : 'center',
  1164. null,
  1165. this.properties['chart.labels.above.angle']);
  1166. // Turn any shadow back on
  1167. if (shadow) {
  1168. this.context.shadowColor = shadowColor;
  1169. this.context.shadowBlur = shadowBlur;
  1170. this.context.shadowOffsetX = shadowOffsetX;
  1171. this.context.shadowOffsetY = shadowOffsetY;
  1172. }
  1173. }
  1174. }
  1175. /**
  1176. * Redraw the bar if shadows are going to the left
  1177. */
  1178. if (redrawCoords.length) {
  1179. RGraph.NoShadow(this);
  1180. this.context.lineWidth = this.properties['chart.linewidth'];
  1181. this.context.beginPath();
  1182. for (var j=0; j<redrawCoords.length; ++j) {
  1183. this.context.fillStyle = redrawCoords[j][4];
  1184. this.context.strokeStyle = this.properties['chart.strokecolor'];
  1185. this.context.fillRect(redrawCoords[j][0], redrawCoords[j][1], redrawCoords[j][2], redrawCoords[j][3]);
  1186. this.context.strokeRect(redrawCoords[j][0], redrawCoords[j][1], redrawCoords[j][2], redrawCoords[j][3]);
  1187. }
  1188. this.context.fill();
  1189. this.context.stroke();
  1190. redrawCoords = [];
  1191. }
  1192. }
  1193. this.context.closePath();
  1194. }
  1195. /**
  1196. * Turn off any shadow
  1197. */
  1198. RGraph.NoShadow(this);
  1199. }
  1200. /**
  1201. * Draws the labels for the graph
  1202. */
  1203. RGraph.Bar.prototype.DrawLabels = function ()
  1204. {
  1205. var context = this.context;
  1206. var text_angle = this.properties['chart.text.angle'];
  1207. var text_size = this.properties['chart.text.size'];
  1208. var labels = this.properties['chart.labels'];
  1209. // Draw the Y axis labels:
  1210. if (this.properties['chart.ylabels']) {
  1211. this.Drawlabels_top();
  1212. this.Drawlabels_center();
  1213. this.Drawlabels_bottom();
  1214. }
  1215. /**
  1216. * The X axis labels
  1217. */
  1218. if (typeof(labels) == 'object' && labels) {
  1219. var yOffset = 5 + Number(this.properties['chart.xlabels.offset']);
  1220. if (this.properties['chart.xaxispos'] == 'top') {
  1221. yOffset += 18;
  1222. }
  1223. /**
  1224. * Text angle
  1225. */
  1226. var angle = 0;
  1227. var halign = 'center';
  1228. if (text_angle > 0) {
  1229. angle = -1 * text_angle;
  1230. halign = 'right';
  1231. yOffset -= 5;
  1232. if (this.properties['chart.xaxispos'] == 'top') {
  1233. halign = 'left';
  1234. yOffset += 5;
  1235. }
  1236. }
  1237. // Draw the X axis labels
  1238. context.fillStyle = this.properties['chart.text.color'];
  1239. // How wide is each bar
  1240. var barWidth = (this.canvas.width - this.gutterRight - this.gutterLeft) / labels.length;
  1241. // Reset the xTickGap
  1242. xTickGap = (this.canvas.width - this.gutterRight - this.gutterLeft) / labels.length
  1243. // Draw the X tickmarks
  1244. var i=0;
  1245. var font = this.properties['chart.text.font'];
  1246. for (x=this.gutterLeft + (xTickGap / 2); x<=this.canvas.width - this.gutterRight; x+=xTickGap) {
  1247. this.context.fillStyle = this.properties['chart.labels.colors'][i];
  1248. RGraph.Text(context, font,
  1249. text_size,
  1250. x + (this.properties['chart.text.angle'] == 90 ? 0 : 0),
  1251. this.properties['chart.xaxispos'] == 'top' ? this.gutterTop - yOffset + text_size - 1 : (this.canvas.height - this.gutterBottom) + yOffset,
  1252. String(labels[i++]),
  1253. (this.properties['chart.text.angle'] == 90 ? 'center' : 'top'),
  1254. halign,
  1255. null,
  1256. angle);
  1257. }
  1258. }
  1259. /**
  1260. * If chart.labels.above.specific is specified, draw them
  1261. */
  1262. if (this.properties['chart.labels.above.specific']) {
  1263. var labels = this.properties['chart.labels.above.specific'];
  1264. for (var i=0; i<this.coords.length; ++i) {
  1265. var coords = this.coords[i];
  1266. var angle = this.properties['chart.labels.above.angle'];
  1267. var value = this.data[i];
  1268. var valign = value >= 0 ? 'bottom' : 'top';
  1269. var halign = 'center';
  1270. // Horizontal alignment
  1271. if (typeof(this.properties['chart.labels.above.angle']) == 'number') {
  1272. if (this.properties['chart.labels.above.angle'] >= 0) {
  1273. if (this.data[i] >= 0) {
  1274. halign = 'right';
  1275. } else {
  1276. halign = 'left';
  1277. }
  1278. } else {
  1279. if (this.data[i] >= 0) {
  1280. halign = 'left';
  1281. } else {
  1282. halign = 'right';
  1283. }
  1284. }
  1285. }
  1286. RGraph.Text(context,
  1287. this.properties['chart.text.font'],
  1288. this.properties['chart.labels.above.size'] ? this.properties['chart.labels.above.size'] : this.properties['chart.text.size'],
  1289. coords[0] + (coords[2] / 2),
  1290. value >= 0 ? coords[1] - 5 : coords[1] + coords[3] + 3,
  1291. String(labels[i]),
  1292. valign,
  1293. halign,
  1294. null,
  1295. angle);
  1296. }
  1297. }
  1298. }
  1299. /**
  1300. * Draws the X axis at the top
  1301. */
  1302. RGraph.Bar.prototype.Drawlabels_top = function ()
  1303. {
  1304. this.context.beginPath();
  1305. this.context.fillStyle = this.properties['chart.text.color'];
  1306. this.context.strokeStyle = 'black';
  1307. if (this.properties['chart.xaxispos'] == 'top') {
  1308. var context = this.context;
  1309. var interval = (this.grapharea * (1/5) );
  1310. var text_size = this.properties['chart.text.size'];
  1311. var units_pre = this.properties['chart.units.pre'];
  1312. var units_post = this.properties['chart.units.post'];
  1313. var align = this.properties['chart.yaxispos'] == 'left' ? 'right' : 'left';
  1314. var font = this.properties['chart.text.font'];
  1315. var numYLabels = this.properties['chart.ylabels.count'];
  1316. if (this.properties['chart.ylabels.inside'] == true) {
  1317. var xpos = this.properties['chart.yaxispos'] == 'left' ? this.gutterLeft + 5 : this.canvas.width - this.gutterRight - 5;
  1318. var align = this.properties['chart.yaxispos'] == 'left' ? 'left' : 'right';
  1319. var boxed = true;
  1320. } else {
  1321. var xpos = this.properties['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : this.canvas.width - this.gutterRight + 5;
  1322. var boxed = false;
  1323. }
  1324. /**
  1325. * Draw specific Y labels here so that the local variables can be reused
  1326. */
  1327. if (typeof(this.properties['chart.ylabels.specific']) == 'object' && this.properties['chart.ylabels.specific']) {
  1328. var labels = RGraph.array_reverse(this.properties['chart.ylabels.specific']);
  1329. var grapharea = this.canvas.height - this.gutterTop - this.gutterBottom;
  1330. for (var i=0; i<labels.length; ++i) {
  1331. var y = this.gutterTop + (grapharea * (i / labels.length)) + (grapharea / labels.length);
  1332. // Color
  1333. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][i]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][i];}
  1334. RGraph.Text(context, font, text_size, xpos, y, labels[i], 'center', align, boxed);
  1335. }
  1336. return;
  1337. }
  1338. // One label
  1339. if (numYLabels == 1) {
  1340. // Color
  1341. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][4]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][4];}
  1342. RGraph.Text(context, font, text_size, xpos,this.canvas.height - this.gutterBottom,'-' + RGraph.number_format(this,Number(this.scale[4]).toFixed((this.properties['chart.scale.decimals'])),units_pre, units_post), 'center', align, boxed);
  1343. }
  1344. // 3 or 5 labels
  1345. if (numYLabels == 3 || numYLabels == 5) {
  1346. // Color
  1347. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][0]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][0];}
  1348. RGraph.Text(context, font, text_size, xpos, this.gutterTop + this.halfTextHeight + interval, '-' + RGraph.number_format(this, this.scale[0], units_pre, units_post), null, align, boxed);
  1349. // 5 labels
  1350. if (numYLabels == 5) {
  1351. // Color
  1352. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][1]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][1];}
  1353. RGraph.Text(context, font, text_size, xpos, (1*interval) + this.gutterTop + this.halfTextHeight + interval, '-' + RGraph.number_format(this, this.scale[1], units_pre, units_post), null, align, boxed);
  1354. // Color
  1355. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][3]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][3];}
  1356. RGraph.Text(context, font, text_size, xpos, (3*interval) + this.gutterTop + this.halfTextHeight + interval, '-' + RGraph.number_format(this, this.scale[3], units_pre, units_post), null, align, boxed);
  1357. }
  1358. // 3 labels
  1359. if (numYLabels == 3 || numYLabels == 5) {
  1360. // Color
  1361. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][2]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][2];}
  1362. RGraph.Text(context, font, text_size, xpos, (2*interval) + this.gutterTop + this.halfTextHeight + interval, '-' + RGraph.number_format(this, this.scale[2], units_pre, units_post), null, align, boxed);
  1363. // Color
  1364. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][4]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][4];}
  1365. RGraph.Text(context, font, text_size, xpos, (4*interval) + this.gutterTop + this.halfTextHeight + interval, '-' + RGraph.number_format(this, this.scale[4], units_pre, units_post), null, align, boxed);
  1366. }
  1367. }
  1368. // 10 Y labels
  1369. if (numYLabels == 10) {
  1370. interval = (this.grapharea / numYLabels );
  1371. for (var i=0; i<10; ++i) {
  1372. // Color
  1373. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][i]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][i];}
  1374. RGraph.Text(context, font, text_size, xpos,this.gutterTop + ((this.grapharea / 10) * (i + 1)),'-' + RGraph.number_format(this,((this.scale[4] / 10) * (i + 1)).toFixed((this.properties['chart.scale.decimals'])), units_pre, units_post), 'center', align, boxed);
  1375. }
  1376. }
  1377. /**
  1378. * Show the minimum value if its not zero
  1379. */
  1380. if (this.properties['chart.ymin'] != 0) {
  1381. // Color
  1382. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][0]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][0];}
  1383. RGraph.Text(context,
  1384. font,
  1385. text_size,
  1386. xpos,
  1387. this.gutterTop,
  1388. '-' + RGraph.number_format(this,(this.min.toFixed((this.properties['chart.scale.decimals']))), units_pre, units_post),
  1389. 'center',
  1390. align,
  1391. boxed);
  1392. }
  1393. }
  1394. this.context.fill();
  1395. this.context.stroke();
  1396. }
  1397. /**
  1398. * Draws the X axis in the middle
  1399. */
  1400. RGraph.Bar.prototype.Drawlabels_center = function ()
  1401. {
  1402. var font = this.properties['chart.text.font'];
  1403. var numYLabels = this.properties['chart.ylabels.count'];
  1404. this.context.fillStyle = this.properties['chart.text.color'];
  1405. if (this.properties['chart.xaxispos'] == 'center') {
  1406. /**
  1407. * Draw the top labels
  1408. */
  1409. var interval = (this.grapharea * (1/10) );
  1410. var text_size = this.properties['chart.text.size'];
  1411. var units_pre = this.properties['chart.units.pre'];
  1412. var units_post = this.properties['chart.units.post'];
  1413. var context = this.context;
  1414. var align = '';
  1415. var xpos = 0;
  1416. var boxed = false;
  1417. this.context.fillStyle = this.properties['chart.text.color'];
  1418. this.context.strokeStyle = 'black';
  1419. if (this.properties['chart.ylabels.inside'] == true) {
  1420. var xpos = this.properties['chart.yaxispos'] == 'left' ? this.gutterLeft + 5 : this.canvas.width - this.gutterRight - 5;
  1421. var align = this.properties['chart.yaxispos'] == 'left' ? 'left' : 'right';
  1422. var boxed = true;
  1423. } else {
  1424. var xpos = this.properties['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : this.canvas.width - this.gutterRight + 5;
  1425. var align = this.properties['chart.yaxispos'] == 'left' ? 'right' : 'left';
  1426. var boxed = false;
  1427. }
  1428. /**
  1429. * Draw specific Y labels here so that the local variables can be reused
  1430. */
  1431. if (typeof(this.properties['chart.ylabels.specific']) == 'object' && this.properties['chart.ylabels.specific']) {
  1432. var labels = this.properties['chart.ylabels.specific'];
  1433. var grapharea = this.canvas.height - this.gutterTop - this.gutterBottom;
  1434. // Draw the top halves labels
  1435. for (var i=0; i<labels.length; ++i) {
  1436. var y = this.gutterTop + ((grapharea / 2) / labels.length) * i;
  1437. // Color
  1438. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][i]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][i];}
  1439. RGraph.Text(context, font, text_size, xpos, y, String(labels[i]), 'center', align, boxed);
  1440. }
  1441. // Draw the bottom halves labels
  1442. for (var i=labels.length-1; i>=0; --i) {
  1443. var y = this.gutterTop + (grapharea * ( (i+1) / (labels.length * 2) )) + (grapharea / 2);
  1444. // Color
  1445. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][i + labels.length]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][i + labels.length];}
  1446. RGraph.Text(context, font, text_size, xpos, y, labels[labels.length - i - 1], 'center', align, boxed);
  1447. }
  1448. return;
  1449. }
  1450. if (numYLabels == 1 || numYLabels == 3 || numYLabels == 5) {
  1451. // Color
  1452. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][0]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][0];}
  1453. RGraph.Text(context, font, text_size, xpos, this.gutterTop + this.halfTextHeight, RGraph.number_format(this, this.scale[4], units_pre, units_post), null, align, boxed);
  1454. if (numYLabels == 5) {
  1455. // Color
  1456. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][1]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][1];}
  1457. RGraph.Text(context, font, text_size, xpos, (1*interval) + this.gutterTop + this.halfTextHeight, RGraph.number_format(this, this.scale[3], units_pre, units_post), null, align, boxed);
  1458. // Color
  1459. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][3]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][3];}
  1460. RGraph.Text(context, font, text_size, xpos, (3*interval) + this.gutterTop + this.halfTextHeight, RGraph.number_format(this, this.scale[1], units_pre, units_post), null, align, boxed);
  1461. }
  1462. if (numYLabels == 3 || numYLabels == 5) {
  1463. // Color
  1464. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][4]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][4];}
  1465. RGraph.Text(context, font, text_size, xpos, (4*interval) + this.gutterTop + this.halfTextHeight, RGraph.number_format(this, this.scale[0], units_pre, units_post), null, align, boxed);
  1466. // Color
  1467. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][2]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][2];}
  1468. RGraph.Text(context, font, text_size, xpos, (2*interval) + this.gutterTop + this.halfTextHeight, RGraph.number_format(this, this.scale[2], units_pre, units_post), null, align, boxed);
  1469. }
  1470. } else if (numYLabels == 10) {
  1471. // 10 Y labels
  1472. interval = (this.grapharea / 10) / 2;
  1473. for (var i=0; i<10; ++i) {
  1474. // Color
  1475. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][i]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][i];}
  1476. RGraph.Text(context, font, text_size, xpos,this.gutterTop + ((this.grapharea / (10 * 2)) * i),RGraph.number_format(this, ((this.scale[4] / 10) * (10 - i)).toFixed((this.properties['chart.scale.decimals'])), units_pre, units_post), 'center', align, boxed);
  1477. }
  1478. }
  1479. ///////////////////////////////////////////////////////////////////////////////////
  1480. /**
  1481. * Draw the bottom halves labels
  1482. */
  1483. var interval = (this.grapharea) / 10;
  1484. if (numYLabels == 1 || numYLabels == 3 || numYLabels == 5) {
  1485. if (numYLabels == 3 || numYLabels == 5) {
  1486. // Color
  1487. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][5]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][5];}
  1488. RGraph.Text(context, font, text_size, xpos, (this.grapharea + this.gutterTop + this.halfTextHeight) - (4 * interval), '-' + RGraph.number_format(this, this.scale[0], units_pre, units_post), null, align, boxed);
  1489. // Color
  1490. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][7]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][7];}
  1491. RGraph.Text(context, font, text_size, xpos, (this.grapharea + this.gutterTop + this.halfTextHeight) - (2 * interval), '-' + RGraph.number_format(this, this.scale[2], units_pre, units_post), null, align, boxed);
  1492. }
  1493. if (numYLabels == 5) {
  1494. // Color
  1495. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][6]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][6];}
  1496. RGraph.Text(context, font, text_size, xpos, (this.grapharea + this.gutterTop + this.halfTextHeight) - (3 * interval), '-' + RGraph.number_format(this, this.scale[1], units_pre, units_post), null, align, boxed);
  1497. // Color
  1498. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][8]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][8];}
  1499. RGraph.Text(context, font, text_size, xpos, (this.grapharea + this.gutterTop + this.halfTextHeight) - interval, '-' + RGraph.number_format(this, this.scale[3], units_pre, units_post), null, align, boxed);
  1500. }
  1501. // Color
  1502. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][9]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][9];}
  1503. RGraph.Text(context, font, text_size, xpos, this.grapharea + this.gutterTop + this.halfTextHeight, '-' + RGraph.number_format(this, this.scale[4], units_pre, units_post), null, align, boxed);
  1504. } else if (numYLabels == 10) {
  1505. // Arbitrary number of Y labels
  1506. interval = (this.grapharea / numYLabels) / 2;
  1507. for (var i=0; i<numYLabels; ++i) {
  1508. // Color
  1509. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][i + 10]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][i + 10];}
  1510. RGraph.Text(context, font, text_size, xpos,this.gutterTop + (this.grapharea / 2) + ((this.grapharea / (numYLabels * 2)) * i) + (this.grapharea / (numYLabels * 2)),RGraph.number_format(this, ((this.scale[4] / numYLabels) * (i+1)).toFixed((this.properties['chart.scale.decimals'])), '-' + units_pre, units_post),'center', align, boxed);
  1511. }
  1512. }
  1513. /**
  1514. * Show the minimum value if its not zero
  1515. */
  1516. if (this.properties['chart.ymin'] != 0) {
  1517. RGraph.Text(context,
  1518. font,
  1519. text_size,
  1520. xpos,
  1521. this.gutterTop + (this.grapharea / 2),
  1522. RGraph.number_format(this,(this.min.toFixed((this.properties['chart.scale.decimals']))), units_pre, units_post),
  1523. 'center',
  1524. align,
  1525. boxed);
  1526. }
  1527. }
  1528. }
  1529. /**
  1530. * Draws the X axdis at the bottom (the default)
  1531. */
  1532. RGraph.Bar.prototype.Drawlabels_bottom = function ()
  1533. {
  1534. this.context.beginPath();
  1535. this.context.fillStyle = this.properties['chart.text.color'];
  1536. this.context.strokeStyle = 'black';
  1537. if (this.properties['chart.xaxispos'] != 'center' && this.properties['chart.xaxispos'] != 'top') {
  1538. var interval = (this.grapharea * (1/5) );
  1539. var text_size = this.properties['chart.text.size'];
  1540. var units_pre = this.properties['chart.units.pre'];
  1541. var units_post = this.properties['chart.units.post'];
  1542. var context = this.context;
  1543. var align = this.properties['chart.yaxispos'] == 'left' ? 'right' : 'left';
  1544. var font = this.properties['chart.text.font'];
  1545. var numYLabels = this.properties['chart.ylabels.count'];
  1546. if (this.properties['chart.ylabels.inside'] == true) {
  1547. var xpos = this.properties['chart.yaxispos'] == 'left' ? this.gutterLeft + 5 : this.canvas.width - this.gutterRight - 5;
  1548. var align = this.properties['chart.yaxispos'] == 'left' ? 'left' : 'right';
  1549. var boxed = true;
  1550. } else {
  1551. var xpos = this.properties['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : this.canvas.width - this.gutterRight + 5;
  1552. var boxed = false;
  1553. }
  1554. /**
  1555. * Draw specific Y labels here so that the local variables can be reused
  1556. */
  1557. if (this.properties['chart.ylabels.specific'] && typeof(this.properties['chart.ylabels.specific']) == 'object') {
  1558. var labels = this.properties['chart.ylabels.specific'];
  1559. var grapharea = this.canvas.height - this.gutterTop - this.gutterBottom;
  1560. for (var i=0; i<labels.length; ++i) {
  1561. var y = this.gutterTop + (grapharea * (i / labels.length));
  1562. // Color
  1563. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][i]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][i];}
  1564. RGraph.Text(context, font, text_size, xpos, y, labels[i], 'center', align, boxed);
  1565. }
  1566. return;
  1567. }
  1568. // 1/3/5 label
  1569. if (numYLabels == 1 || numYLabels == 3 || numYLabels == 5) {
  1570. // Color
  1571. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][0]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][0];}
  1572. RGraph.Text(context, font, text_size, xpos, this.gutterTop + this.halfTextHeight, RGraph.number_format(this, this.scale[4], units_pre, units_post), null, align, boxed);
  1573. // 5 labels
  1574. if (numYLabels == 5) {
  1575. // Color
  1576. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][1]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][1];}
  1577. RGraph.Text(context, font, text_size, xpos, (1*interval) + this.gutterTop + this.halfTextHeight, RGraph.number_format(this, this.scale[3], units_pre, units_post), null, align, boxed);
  1578. // Color
  1579. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][3]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][3];}
  1580. RGraph.Text(context, font, text_size, xpos, (3*interval) + this.gutterTop + this.halfTextHeight, RGraph.number_format(this, this.scale[1], units_pre, units_post), null, align, boxed);
  1581. }
  1582. // 3 labels
  1583. if (numYLabels == 3 || numYLabels == 5) {
  1584. // Color
  1585. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][2]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][2];}
  1586. RGraph.Text(context, font, text_size, xpos, (2*interval) + this.gutterTop + this.halfTextHeight, RGraph.number_format(this, this.scale[2], units_pre, units_post), null, align, boxed);
  1587. // Color
  1588. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][4]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][4];}
  1589. RGraph.Text(context, font, text_size, xpos, (4*interval) + this.gutterTop + this.halfTextHeight, RGraph.number_format(this, this.scale[0], units_pre, units_post), null, align, boxed);
  1590. }
  1591. }
  1592. // 10 Y labels
  1593. if (numYLabels == 10) {
  1594. interval = (this.grapharea / numYLabels );
  1595. for (var i=0; i<10; ++i) {
  1596. // Color
  1597. if (typeof(this.properties['chart.ylabels.colors']) == 'object' && this.properties['chart.ylabels.colors'] && this.properties['chart.ylabels.colors'][i]) {this.context.fillStyle = this.properties['chart.ylabels.colors'][i];}
  1598. RGraph.Text(context, font, text_size, xpos, this.gutterTop + ((this.grapharea / numYLabels) * i), RGraph.number_format(this,((this.scale[4] / numYLabels) * (numYLabels - i)).toFixed((this.properties['chart.scale.decimals'])), units_pre, units_post), 'center', align, boxed);
  1599. }
  1600. }
  1601. /**
  1602. * Show the minimum value if its not zero
  1603. */
  1604. if (this.properties['chart.ymin'] != 0) {
  1605. RGraph.Text(context,
  1606. font,
  1607. text_size,
  1608. xpos,
  1609. this.canvas.height - this.gutterBottom,
  1610. RGraph.number_format(this,(this.min.toFixed((this.properties['chart.scale.decimals']))), units_pre, units_post),
  1611. 'center',
  1612. align,
  1613. boxed);
  1614. }
  1615. }
  1616. this.context.fill();
  1617. this.context.stroke();
  1618. }
  1619. /**
  1620. * This function is used by MSIE only to manually draw the shadow
  1621. *
  1622. * @param array coords The coords for the bar
  1623. */
  1624. RGraph.Bar.prototype.DrawIEShadow = function (coords)
  1625. {
  1626. var prevFillStyle = this.context.fillStyle;
  1627. var offsetx = this.properties['chart.shadow.offsetx'];
  1628. var offsety = this.properties['chart.shadow.offsety'];
  1629. this.context.lineWidth = this.properties['chart.linewidth'];
  1630. this.context.fillStyle = this.properties['chart.shadow.color'];
  1631. this.context.beginPath();
  1632. // Draw shadow here
  1633. this.context.fillRect(coords[0] + offsetx, coords[1] + offsety, coords[2], coords[3]);
  1634. this.context.fill();
  1635. // Change the fillstyle back to what it was
  1636. this.context.fillStyle = prevFillStyle;
  1637. }
  1638. /**
  1639. * Not used by the class during creating the graph, but is used by event handlers
  1640. * to get the coordinates (if any) of the selected bar
  1641. *
  1642. * @param object e The event object
  1643. * @param object OPTIONAL You can pass in the bar object instead of the
  1644. * function using "this"
  1645. */
  1646. RGraph.Bar.prototype.getShape =
  1647. RGraph.Bar.prototype.getBar = function (e)
  1648. {
  1649. // This facilitates you being able to pass in the bar object as a parameter instead of
  1650. // the function getting it from itself
  1651. var obj = arguments[1] ? arguments[1] : this;
  1652. var mouseXY = RGraph.getMouseXY(e);
  1653. var mouseX = mouseXY[0];
  1654. var mouseY = mouseXY[1];
  1655. for (var i=0; i<this.coords.length; i++) {
  1656. var left = obj.coords[i][0];
  1657. var top = obj.coords[i][1];
  1658. var width = obj.coords[i][2];
  1659. var height = obj.coords[i][3];
  1660. if (mouseX >= left && mouseX <= (left + width) && mouseY >= top && mouseY <= (top + height)) {
  1661. if (this.properties['chart.tooltips']) {
  1662. var tooltip = RGraph.parseTooltipText ? RGraph.parseTooltipText(this.properties['chart.tooltips'], i) : this.properties['chart.tooltips'][i];
  1663. }
  1664. // Work out the dataset
  1665. var dataset = 0;
  1666. var idx = i;
  1667. while (idx >= (typeof(this.data[dataset]) == 'object' ? this.data[dataset].length : 1)) {
  1668. if (typeof(this.data[dataset]) == 'number') {
  1669. idx -= 1;
  1670. } else {
  1671. idx -= this.data[dataset].length;
  1672. }
  1673. dataset++;
  1674. }
  1675. if (typeof(this.data[dataset]) == 'number') {
  1676. idx = null;
  1677. }
  1678. return {
  1679. 0: obj, 1: left, 2: top, 3: width, 4: height, 5: i,
  1680. 'object': obj, 'x': left, 'y': top, 'width': width, 'height': height, 'index': i, 'tooltip': tooltip, 'index_adjusted': idx, 'dataset': dataset
  1681. };
  1682. }
  1683. }
  1684. return null;
  1685. }
  1686. /**
  1687. * This retrives the bar based on the X coordinate only.
  1688. *
  1689. * @param object e The event object
  1690. * @param object OPTIONAL You can pass in the bar object instead of the
  1691. * function using "this"
  1692. */
  1693. RGraph.Bar.prototype.getShapeByX = function (e)
  1694. {
  1695. var canvas = e.target;
  1696. var mouseCoords = RGraph.getMouseXY(e);
  1697. // This facilitates you being able to pass in the bar object as a parameter instead of
  1698. // the function getting it from itself
  1699. var obj = arguments[1] ? arguments[1] : this;
  1700. /**
  1701. * Loop through the bars determining if the mouse is over a bar
  1702. */
  1703. for (var i=0; i<obj.coords.length; i++) {
  1704. var mouseX = mouseCoords[0];
  1705. var mouseY = mouseCoords[1];
  1706. var left = obj.coords[i][0];
  1707. var top = obj.coords[i][1];
  1708. var width = obj.coords[i][2];
  1709. var height = obj.coords[i][3];
  1710. if (mouseX >= left && mouseX <= (left + width)) {
  1711. if (this.properties['chart.tooltips']) {
  1712. var tooltip = RGraph.parseTooltipText ? RGraph.parseTooltipText(this.properties['chart.tooltips'], i) : this.properties['chart.tooltips'][i];
  1713. }
  1714. return {
  1715. 0: obj, 1: left, 2: top, 3: width, 4: height, 5: i,
  1716. 'object': obj, 'x': left, 'y': top, 'width': width, 'height': height, 'index': i, 'tooltip': tooltip
  1717. };
  1718. }
  1719. }
  1720. return null;
  1721. }
  1722. /**
  1723. * When you click on the chart, this method can return the Y value at that point. It works for any point on the
  1724. * chart (that is inside the gutters) - not just points within the Bars.
  1725. *
  1726. * EITHER:
  1727. *
  1728. * @param object arg The event object
  1729. *
  1730. * OR:
  1731. *
  1732. * @param object arg A two element array containing the X and Y coordinates
  1733. */
  1734. RGraph.Bar.prototype.getValue = function (arg)
  1735. {
  1736. if (arg.length == 2) {
  1737. var mouseX = arg[0];
  1738. var mouseY = arg[1];
  1739. } else {
  1740. var mouseCoords = RGraph.getMouseXY(arg);
  1741. var mouseX = mouseCoords[0];
  1742. var mouseY = mouseCoords[1];
  1743. }
  1744. if ( mouseY < this.properties['chart.gutter.top']
  1745. || mouseY > (this.canvas.height - this.properties['chart.gutter.bottom'])
  1746. || mouseX < this.properties['chart.gutter.left']
  1747. || mouseX > (this.canvas.width - this.properties['chart.gutter.right'])
  1748. ) {
  1749. return null;
  1750. }
  1751. if (this.properties['chart.xaxispos'] == 'center') {
  1752. var value = (((this.grapharea / 2) - (mouseY - this.properties['chart.gutter.top'])) / this.grapharea) * (this.max - this.min)
  1753. value *= 2;
  1754. if (value >= 0) {
  1755. value += this.min;
  1756. } else {
  1757. value -= this.min;
  1758. }
  1759. } else if (this.properties['chart.xaxispos'] == 'top') {
  1760. var value = ((this.grapharea - (mouseY - this.properties['chart.gutter.top'])) / this.grapharea) * (this.max - this.min)
  1761. value = this.max - value;
  1762. value = Math.abs(value) * -1;
  1763. } else {
  1764. var value = ((this.grapharea - (mouseY - this.properties['chart.gutter.top'])) / this.grapharea) * (this.max - this.min)
  1765. value += this.min;
  1766. }
  1767. return value;
  1768. }
  1769. /**
  1770. * This function can be used when the canvas is clicked on (or similar - depending on the event)
  1771. * to retrieve the relevant Y coordinate for a particular value.
  1772. *
  1773. * @param int value The value to get the Y coordinate for
  1774. */
  1775. RGraph.Bar.prototype.getYCoord = function (value)
  1776. {
  1777. if (value > this.max) {
  1778. return null;
  1779. }
  1780. var y;
  1781. var xaxispos = this.properties['chart.xaxispos'];
  1782. if (xaxispos == 'top') {
  1783. // Account for negative numbers
  1784. if (value < 0) {
  1785. value = Math.abs(value);
  1786. }
  1787. y = ((value - this.min) / (this.max - this.min)) * this.grapharea;
  1788. y = y + this.gutterTop
  1789. } else if (xaxispos == 'center') {
  1790. y = ((value - this.min) / (this.max - this.min)) * (this.grapharea / 2);
  1791. y = (this.grapharea / 2) - y;
  1792. y += this.gutterTop;
  1793. } else {
  1794. if (value < this.min) {
  1795. value = this.min;
  1796. }
  1797. y = ((value - this.min) / (this.max - this.min)) * this.grapharea;
  1798. y = this.canvas.height - this.gutterBottom - y;
  1799. }
  1800. return y;
  1801. }
  1802. /**
  1803. * Each object type has its own Highlight() function which highlights the appropriate shape
  1804. *
  1805. * @param object shape The shape to highlight
  1806. */
  1807. RGraph.Bar.prototype.Highlight = function (shape)
  1808. {
  1809. // Add the new highlight
  1810. RGraph.Highlight.Rect(this, shape);
  1811. }
  1812. /**
  1813. * The getObjectByXY() worker method
  1814. */
  1815. RGraph.Bar.prototype.getObjectByXY = function (e)
  1816. {
  1817. var mouseXY = RGraph.getMouseXY(e);
  1818. if (
  1819. mouseXY[0] >= this.properties['chart.gutter.left']
  1820. && mouseXY[0] <= (this.canvas.width - this.properties['chart.gutter.right'])
  1821. && mouseXY[1] >= this.properties['chart.gutter.top']
  1822. && mouseXY[1] <= (this.canvas.height - this.properties['chart.gutter.bottom'])
  1823. ) {
  1824. return this;
  1825. }
  1826. }
  1827. /**
  1828. * This method handles the adjusting calculation for when the mouse is moved
  1829. *
  1830. * @param object e The event object
  1831. */
  1832. RGraph.Bar.prototype.Adjusting_mousemove = function (e)
  1833. {
  1834. /**
  1835. * Handle adjusting for the Bar
  1836. */
  1837. if (RGraph.Registry.Get('chart.adjusting') && RGraph.Registry.Get('chart.adjusting').uid == this.uid) {
  1838. // Rounding the value to the given number of decimals make the chart step
  1839. var value = Number(this.getValue(e));//.toFixed(this.Get('chart.scale.decimals'));
  1840. var shape = this.getShapeByX(e);
  1841. if (shape) {
  1842. RGraph.Registry.Set('chart.adjusting.shape', shape);
  1843. this.data[shape['index']] = Number(value);
  1844. RGraph.RedrawCanvas(e.target);
  1845. RGraph.FireCustomEvent(this, 'onadjust');
  1846. }
  1847. }
  1848. }
  1849. /*********************************************************************************************************
  1850. * This is the combined bar and Line class which makes creating bar/line combo charts a little bit easier *
  1851. /*********************************************************************************************************/
  1852. RGraph.CombinedChart = function ()
  1853. {
  1854. /**
  1855. * Create a default empty array for the objects
  1856. */
  1857. this.objects = [];
  1858. for (var i=0; i<arguments.length; ++i) {
  1859. this.objects[i] = arguments[i];
  1860. /**
  1861. * Set the Line chart gutters to match the Bar chart gutters
  1862. */
  1863. this.objects[i].Set('chart.gutter.left', this.objects[0].Get('chart.gutter.left'));
  1864. this.objects[i].Set('chart.gutter.right', this.objects[0].Get('chart.gutter.right'));
  1865. this.objects[i].Set('chart.gutter.top', this.objects[0].Get('chart.gutter.top'));
  1866. this.objects[i].Set('chart.gutter.bottom', this.objects[0].Get('chart.gutter.bottom'));
  1867. if (this.objects[i].type == 'line') {
  1868. /**
  1869. * Set the line chart hmargin
  1870. */
  1871. this.objects[i].Set('chart.hmargin', ((this.objects[0].canvas.width - this.objects[0].Get('chart.gutter.right') - this.objects[0].Get('chart.gutter.left')) / this.objects[0].data.length) / 2 );
  1872. /**
  1873. * No labels, axes or grid on the Line chart
  1874. */
  1875. this.objects[i].Set('chart.noaxes', true);
  1876. this.objects[i].Set('chart.background.grid', false);
  1877. this.objects[i].Set('chart.ylabels', false);
  1878. }
  1879. /**
  1880. * Resizing
  1881. */
  1882. if (this.objects[i].Get('chart.resizable')) {
  1883. var resizable = true;
  1884. }
  1885. }
  1886. /**
  1887. * Resizing
  1888. */
  1889. if (resizable) {
  1890. /**
  1891. * This recalculates the Line chart hmargin when the chart is resized
  1892. */
  1893. function myOnresizebeforedraw (obj)
  1894. {
  1895. var gutterLeft = obj.Get('chart.gutter.left');
  1896. var gutterRight = obj.Get('chart.gutter.right');
  1897. obj.Set('chart.hmargin', (obj.canvas.width - gutterLeft - gutterRight) / (obj.original_data[0].length * 2));
  1898. }
  1899. RGraph.AddCustomEventListener(RGraph.ObjectRegistry.getFirstObjectByType(this.objects[0].canvas.id, 'line'), 'onresizebeforedraw', myOnresizebeforedraw);
  1900. }
  1901. }
  1902. RGraph.CombinedChart.prototype.Draw = function ()
  1903. {
  1904. for (var i=0; i<this.objects.length; ++i) {
  1905. if (typeof(arguments[i]) == 'function') {
  1906. arguments[i](this.objects[i]);
  1907. } else {
  1908. this.objects[i].Draw();
  1909. }
  1910. }
  1911. }
  1912. /**
  1913. * This function positions a tooltip when it is displayed
  1914. *
  1915. * @param obj object The chart object
  1916. * @param int x The X coordinate specified for the tooltip
  1917. * @param int y The Y coordinate specified for the tooltip
  1918. * @param objec tooltip The tooltips DIV element
  1919. */
  1920. RGraph.Bar.prototype.positionTooltip = function (obj, x, y, tooltip, idx)
  1921. {
  1922. var coordX = obj.coords[tooltip.__index__][0];
  1923. var coordY = obj.coords[tooltip.__index__][1];
  1924. var coordW = obj.coords[tooltip.__index__][2];
  1925. var coordH = obj.coords[tooltip.__index__][3];
  1926. var canvasXY = RGraph.getCanvasXY(obj.canvas);
  1927. var gutterLeft = obj.Get('chart.gutter.left');
  1928. var gutterTop = obj.Get('chart.gutter.top');
  1929. var width = tooltip.offsetWidth;
  1930. var height = tooltip.offsetHeight;
  1931. var value = obj.data_arr[tooltip.__index__];
  1932. // Set the top position
  1933. tooltip.style.left = 0;
  1934. tooltip.style.top = canvasXY[1] + coordY - height - 7 + 'px';
  1935. /**
  1936. * If the tooltip is for a negative value - position it underneath the bar
  1937. */
  1938. if (value < 0) {
  1939. tooltip.style.top = canvasXY[1] + coordY + coordH + 7 + 'px';
  1940. }
  1941. // By default any overflow is hidden
  1942. tooltip.style.overflow = '';
  1943. // Inverted arrow
  1944. // data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAFCAMAAACkeOZkAAAAK3RFWHRDcmVhdGlvbiBUaW1lAFNhdCA2IE9jdCAyMDEyIDEyOjQ5OjMyIC0wMDAw2S1RlgAAAAd0SU1FB9wKBgszM4Ed2k4AAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAEZ0FNQQAAsY8L/GEFAAAACVBMVEX/AAC9vb3//+92Pom0AAAAAXRSTlMAQObYZgAAAB1JREFUeNpjYAABRgY4YGRiRDCZYBwQE8qBMEEcAANCACqByy1sAAAAAElFTkSuQmCC
  1945. // The arrow
  1946. var img = new Image();
  1947. img.style.position = 'absolute';
  1948. img.id = '__rgraph_tooltip_pointer__';
  1949. if (value >= 0) {
  1950. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
  1951. img.style.top = (tooltip.offsetHeight - 2) + 'px';
  1952. } else {
  1953. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAFCAMAAACkeOZkAAAAK3RFWHRDcmVhdGlvbiBUaW1lAFNhdCA2IE9jdCAyMDEyIDEyOjQ5OjMyIC0wMDAw2S1RlgAAAAd0SU1FB9wKBgszM4Ed2k4AAAAJcEhZcwAACxIAAAsSAdLdfvwAAAAEZ0FNQQAAsY8L/GEFAAAACVBMVEX/AAC9vb3//+92Pom0AAAAAXRSTlMAQObYZgAAAB1JREFUeNpjYAABRgY4YGRiRDCZYBwQE8qBMEEcAANCACqByy1sAAAAAElFTkSuQmCC';
  1954. img.style.top = '-5px';
  1955. }
  1956. tooltip.appendChild(img);
  1957. // Reposition the tooltip if at the edges:
  1958. // LEFT edge
  1959. if ((canvasXY[0] + coordX + (coordW / 2) - (width / 2)) < 10) {
  1960. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + (coordW / 2) + 'px';
  1961. img.style.left = ((width * 0.1) - 8.5) + 'px';
  1962. // RIGHT edge
  1963. } else if ((canvasXY[0] + coordX + (width / 2)) > document.body.offsetWidth) {
  1964. tooltip.style.left = canvasXY[0] + coordX - (width * 0.9) + (coordW / 2) + 'px';
  1965. img.style.left = ((width * 0.9) - 8.5) + 'px';
  1966. // Default positioning - CENTERED
  1967. } else {
  1968. tooltip.style.left = (canvasXY[0] + coordX + (coordW / 2) - (width * 0.5)) + 'px';
  1969. img.style.left = ((width * 0.5) - 8.5) + 'px';
  1970. }
  1971. }
  1972. /**
  1973. * This allows for easy specification of gradients
  1974. */
  1975. RGraph.Bar.prototype.parseColors = function ()
  1976. {
  1977. // Set this as a local variable
  1978. var props = this.properties;
  1979. // chart.colors
  1980. var colors = props['chart.colors'];
  1981. if (colors) {
  1982. for (var i=0; i<colors.length; ++i) {
  1983. colors[i] = this.parseSingleColorForGradient(colors[i]);
  1984. }
  1985. }
  1986. // chart.key.colors
  1987. var colors = props['chart.key.colors'];
  1988. if (colors) {
  1989. for (var i=0; i<colors.length; ++i) {
  1990. colors[i] = this.parseSingleColorForGradient(colors[i]);
  1991. }
  1992. }
  1993. props['chart.crosshairs.color'] = this.parseSingleColorForGradient(props['chart.crosshairs.color']);
  1994. props['chart.highlight.stroke'] = this.parseSingleColorForGradient(props['chart.highlight.stroke']);
  1995. props['chart.highlight.fill'] = this.parseSingleColorForGradient(props['chart.highlight.fill']);
  1996. props['chart.text.color'] = this.parseSingleColorForGradient(props['chart.text.color']);
  1997. props['chart.background.barcolor1'] = this.parseSingleColorForGradient(props['chart.background.barcolor1']);
  1998. props['chart.background.barcolor2'] = this.parseSingleColorForGradient(props['chart.background.barcolor2']);
  1999. props['chart.background.grid.color'] = this.parseSingleColorForGradient(props['chart.background.grid.color']);
  2000. props['chart.strokecolor'] = this.parseSingleColorForGradient(props['chart.strokecolor']);
  2001. props['chart.axis.color'] = this.parseSingleColorForGradient(props['chart.axis.color']);
  2002. }
  2003. /**
  2004. * This parses a single color value
  2005. */
  2006. RGraph.Bar.prototype.parseSingleColorForGradient = function (color)
  2007. {
  2008. if (!color || typeof(color) != 'string') {
  2009. return color;
  2010. }
  2011. if (color.match(/^gradient\((.*)\)$/i)) {
  2012. var parts = RegExp.$1.split(':');
  2013. // Create the gradient
  2014. var grad = this.context.createLinearGradient(0,this.canvas.height - this.properties['chart.gutter.bottom'], 0, this.properties['chart.gutter.top']);
  2015. var diff = 1 / (parts.length - 1);
  2016. grad.addColorStop(0, RGraph.trim(parts[0]));
  2017. for (var j=1; j<parts.length; ++j) {
  2018. grad.addColorStop(j * diff, RGraph.trim(parts[j]));
  2019. }
  2020. }
  2021. return grad ? grad : color;
  2022. }