RGraph.common.deprecated.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // version: 2014-06-26
  2. /**
  3. * o--------------------------------------------------------------------------------o
  4. * | This file is part of the RGraph package. RGraph is Free Software, licensed |
  5. * | under the MIT license - so it's free to use for all purposes. If you want to |
  6. * | donate to help keep the project going then you can do so here: |
  7. * | |
  8. * | http://www.rgraph.net/donate |
  9. * o--------------------------------------------------------------------------------o
  10. */
  11. RGraph = window.RGraph || {isRGraph: true};
  12. // Module pattern
  13. (function (win, doc, undefined)
  14. {
  15. var RG = RGraph,
  16. ua = navigator.userAgent,
  17. ma = Math;
  18. /**
  19. * This is a useful function which is basically a shortcut for drawing left, right, top and bottom alligned text.
  20. *
  21. * @param object context The context
  22. * @param string font The font
  23. * @param int size The size of the text
  24. * @param int x The X coordinate
  25. * @param int y The Y coordinate
  26. * @param string text The text to draw
  27. * @parm string The vertical alignment. Can be null. "center" gives center aligned text, "top" gives top aligned text.
  28. * Anything else produces bottom aligned text. Default is bottom.
  29. * @param string The horizontal alignment. Can be null. "center" gives center aligned text, "right" gives right aligned text.
  30. * Anything else produces left aligned text. Default is left.
  31. * @param bool Whether to show a bounding box around the text. Defaults not to
  32. * @param int The angle that the text should be rotate at (IN DEGREES)
  33. * @param string Background color for the text
  34. * @param bool Whether the text is bold or not
  35. */
  36. RG.text =
  37. RG.Text = function (context, font, size, x, y, text)
  38. {
  39. // "Cache" the args as a local variable
  40. var args = arguments;
  41. // Handle undefined - change it to an empty string
  42. if ((typeof(text) != 'string' && typeof(text) != 'number') || text == 'undefined') {
  43. return;
  44. }
  45. /**
  46. * This accommodates multi-line text
  47. */
  48. if (typeof(text) == 'string' && text.match(/\r\n/)) {
  49. var dimensions = RGraph.MeasureText('M', args[11], font, size);
  50. /**
  51. * Measure the text (width and height)
  52. */
  53. var arr = text.split('\r\n');
  54. /**
  55. * Adjust the Y position
  56. */
  57. // This adjusts the initial y position
  58. if (args[6] && args[6] == 'center') y = (y - (dimensions[1] * ((arr.length - 1) / 2)));
  59. for (var i=1; i<arr.length; ++i) {
  60. RGraph.Text(context,
  61. font,
  62. size,
  63. args[9] == -90 ? (x + (size * 1.5)) : x,
  64. y + (dimensions[1] * i),
  65. arr[i],
  66. args[6] ? args[6] : null,
  67. args[7],
  68. args[8],
  69. args[9],
  70. args[10],
  71. args[11],
  72. args[12]);
  73. }
  74. // Update text to just be the first line
  75. text = arr[0];
  76. }
  77. // Accommodate MSIE
  78. if (document.all && RGraph.ISOLD) {
  79. y += 2;
  80. }
  81. context.font = (args[11] ? 'Bold ': '') + size + 'pt ' + font;
  82. var i;
  83. var origX = x;
  84. var origY = y;
  85. var originalFillStyle = context.fillStyle;
  86. var originalLineWidth = context.lineWidth;
  87. // Need these now the angle can be specified, ie defaults for the former two args
  88. if (typeof(args[6]) == 'undefined') args[6] = 'bottom'; // Vertical alignment. Default to bottom/baseline
  89. if (typeof(args[7]) == 'undefined') args[7] = 'left'; // Horizontal alignment. Default to left
  90. if (typeof(args[8]) == 'undefined') args[8] = null; // Show a bounding box. Useful for positioning during development. Defaults to false
  91. if (typeof(args[9]) == 'undefined') args[9] = 0; // Angle (IN DEGREES) that the text should be drawn at. 0 is middle right, and it goes clockwise
  92. // The alignment is recorded here for purposes of Opera compatibility
  93. if (navigator.userAgent.indexOf('Opera') != -1) {
  94. context.canvas.__rgraph_valign__ = args[6];
  95. context.canvas.__rgraph_halign__ = args[7];
  96. }
  97. // First, translate to x/y coords
  98. context.save();
  99. context.canvas.__rgraph_originalx__ = x;
  100. context.canvas.__rgraph_originaly__ = y;
  101. context.translate(x, y);
  102. x = 0;
  103. y = 0;
  104. // Rotate the canvas if need be
  105. if (args[9]) {
  106. context.rotate(args[9] / (180 / RGraph.PI));
  107. }
  108. // Vertical alignment - defaults to bottom
  109. if (args[6]) {
  110. var vAlign = args[6];
  111. if (vAlign == 'center') {
  112. context.textBaseline = 'middle';
  113. } else if (vAlign == 'top') {
  114. context.textBaseline = 'top';
  115. }
  116. }
  117. // Hoeizontal alignment - defaults to left
  118. if (args[7]) {
  119. var hAlign = args[7];
  120. var width = context.measureText(text).width;
  121. if (hAlign) {
  122. if (hAlign == 'center') {
  123. context.textAlign = 'center';
  124. } else if (hAlign == 'right') {
  125. context.textAlign = 'right';
  126. }
  127. }
  128. }
  129. context.fillStyle = originalFillStyle;
  130. /**
  131. * Draw a bounding box if requested
  132. */
  133. context.save();
  134. context.fillText(text,0,0);
  135. context.lineWidth = 1;
  136. var width = context.measureText(text).width;
  137. var width_offset = (hAlign == 'center' ? (width / 2) : (hAlign == 'right' ? width : 0));
  138. var height = size * 1.5; // !!!
  139. var height_offset = (vAlign == 'center' ? (height / 2) : (vAlign == 'top' ? height : 0));
  140. var ieOffset = RGraph.ISOLD ? 2 : 0;
  141. if (args[8]) {
  142. context.strokeRect(-3 - width_offset,
  143. 0 - 3 - height - ieOffset + height_offset,
  144. width + 6,
  145. height + 6);
  146. /**
  147. * If requested, draw a background for the text
  148. */
  149. if (args[10]) {
  150. context.fillStyle = args[10];
  151. context.fillRect(-3 - width_offset,
  152. 0 - 3 - height - ieOffset + height_offset,
  153. width + 6,
  154. height + 6);
  155. }
  156. context.fillStyle = originalFillStyle;
  157. /**
  158. * Do the actual drawing of the text
  159. */
  160. context.fillText(text,0,0);
  161. }
  162. context.restore();
  163. // Reset the lineWidth
  164. context.lineWidth = originalLineWidth;
  165. context.restore();
  166. };
  167. /**
  168. * This function returns the mouse position in relation to the canvas
  169. *
  170. * @param object e The event object.
  171. */
  172. RG.getMouseXY = function (e)
  173. {
  174. var el = (RGraph.ISOLD ? event.srcElement : e.target);
  175. var x;
  176. var y;
  177. // ???
  178. var paddingLeft = el.style.paddingLeft ? parseInt(el.style.paddingLeft) : 0;
  179. var paddingTop = el.style.paddingTop ? parseInt(el.style.paddingTop) : 0;
  180. var borderLeft = el.style.borderLeftWidth ? parseInt(el.style.borderLeftWidth) : 0;
  181. var borderTop = el.style.borderTopWidth ? parseInt(el.style.borderTopWidth) : 0;
  182. if (RGraph.ISIE8) e = event;
  183. // Browser with offsetX and offsetY
  184. if (typeof(e.offsetX) == 'number' && typeof(e.offsetY) == 'number') {
  185. x = e.offsetX;
  186. y = e.offsetY;
  187. // FF and other
  188. } else {
  189. x = 0;
  190. y = 0;
  191. while (el != document.body && el) {
  192. x += el.offsetLeft;
  193. y += el.offsetTop;
  194. el = el.offsetParent;
  195. }
  196. x = e.pageX - x;
  197. y = e.pageY - y;
  198. }
  199. return [x, y];
  200. };
  201. /**
  202. * This function attempts to "fill in" missing functions from the canvas
  203. * context object. Only two at the moment - measureText() nd fillText().
  204. *
  205. * @param object context The canvas 2D context
  206. */
  207. RG.oldBrowserCompat =
  208. RG.OldBrowserCompat = function (co)
  209. {
  210. if (!co) {
  211. return;
  212. }
  213. if (!co.measureText) {
  214. // This emulates the measureText() function
  215. co.measureText = function (text)
  216. {
  217. var textObj = document.createElement('DIV');
  218. textObj.innerHTML = text;
  219. textObj.style.position = 'absolute';
  220. textObj.style.top = '-100px';
  221. textObj.style.left = 0;
  222. document.body.appendChild(textObj);
  223. var width = {width: textObj.offsetWidth};
  224. textObj.style.display = 'none';
  225. return width;
  226. }
  227. }
  228. if (!co.fillText) {
  229. // This emulates the fillText() method
  230. co.fillText = function (text, targetX, targetY)
  231. {
  232. return false;
  233. }
  234. }
  235. // If IE8, add addEventListener()
  236. if (!co.canvas.addEventListener) {
  237. window.addEventListener = function (ev, func, bubble)
  238. {
  239. return this.attachEvent('on' + ev, func);
  240. }
  241. co.canvas.addEventListener = function (ev, func, bubble)
  242. {
  243. return this.attachEvent('on' + ev, func);
  244. }
  245. }
  246. };
  247. /**
  248. * Similar to the jQuery each() function - this lets you iterate easily over an array. The 'this' variable is set]
  249. * to the array in the callback function.
  250. *
  251. * @param array arr The array
  252. * @param function func The function to call
  253. * @param object Optionally you can specify the object that the "this" variable is set to
  254. */
  255. RG.each = function (arr, func)
  256. {
  257. for(var i=0, len=arr.length; i<len; i+=1) {
  258. if (typeof arguments[2] !== 'undefined') {
  259. var ret = func.call(arguments[2], i, arr[i]);
  260. } else {
  261. var ret = func.call(arr, i, arr[i]);
  262. }
  263. if (ret === false) {
  264. return;
  265. }
  266. }
  267. };
  268. /**
  269. * An old function the was used before all 4 gutters were added
  270. *
  271. * DEPRECATED
  272. *
  273. * @param object obj The chart object
  274. */
  275. RG.getHeight =
  276. RG.GetHeight = function (obj)
  277. {
  278. return obj.canvas.height;
  279. };
  280. /**
  281. * An old function the was used before all 4 gutters were added
  282. *
  283. * DEPRECATED
  284. *
  285. * @param object obj The chart object
  286. */
  287. RG.getWidth =
  288. RG.GetWidth = function (obj)
  289. {
  290. return obj.canvas.width;
  291. };
  292. /**
  293. * A timer function for measuring... time!
  294. *
  295. * @param string label A string to associate with this 'checkpoint'
  296. */
  297. RG.timer =
  298. RG.Timer = function (label)
  299. {
  300. if(typeof RG.TIMER_LAST_CHECKPOINT == 'undefined') {
  301. RG.TIMER_LAST_CHECKPOINT = Date.now();
  302. }
  303. var now = Date.now();
  304. console.log(label+': ' + (now - RG.TIMER_LAST_CHECKPOINT).toString());
  305. RG.TIMER_LAST_CHECKPOINT = now;
  306. };
  307. /**
  308. * If you prefer, you can use the SetConfig() method to set the configuration information
  309. * for your chart. You may find that setting the configuration this way eases reuse.
  310. *
  311. * @param object obj The graph object
  312. * @param object config The graph configuration information
  313. */
  314. RG.setConfig =
  315. RG.SetConfig = function (obj, config)
  316. {
  317. for (i in config) {
  318. if (typeof i === 'string') {
  319. obj.Set(i, config[i]);
  320. }
  321. }
  322. return obj;
  323. };
  324. // End module pattern
  325. })(window, document);
  326. /**
  327. * Checks whether strings or numbers are empty or not. It also
  328. * handles null or variables set to undefined. If a variable really
  329. * is undefined - ie it hasn't been declared at all - you need to use
  330. * "typeof variable" and check the return value - which will be undefined.
  331. *
  332. * @param mixed value The variable to check
  333. */
  334. window.$empty = function (value)
  335. {
  336. if (!value || value.length <= 0) {
  337. return true;
  338. }
  339. return false;
  340. };