RGraph.bipolar.js 41 KB

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