RGraph.drawing.rect.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. /**
  15. * Having this here means that the RGraph libraries can be included in any order, instead of you having
  16. * to include the common core library first.
  17. */
  18. if (typeof(RGraph) == 'undefined') RGraph = {};
  19. if (typeof(RGraph.Drawing) == 'undefined') RGraph.Drawing = {};
  20. /**
  21. * The constructor. This function sets up the object. It takes the ID (the HTML attribute) of the canvas as the
  22. * first argument and the data as the second. If you need to change this, you can.
  23. *
  24. * @param string id The canvas tag ID
  25. * @param number x The X position of the rectangle
  26. * @param number y The Y position of the rectangle
  27. * @param number w The width of the rectangle
  28. * @param number h The height of the rectangle
  29. */
  30. RGraph.Drawing.Rect = function (id, x, y, w, h)
  31. {
  32. this.id = id;
  33. this.canvas = document.getElementById(typeof id === 'object' ? id.id : id);
  34. this.context = this.canvas.getContext ? this.canvas.getContext("2d") : null;
  35. this.colorsParsed = false;
  36. this.canvas.__object__ = this;
  37. /**
  38. * This defines the type of this shape
  39. */
  40. this.type = 'drawing.rect';
  41. /**
  42. * This facilitates easy object identification, and should always be true
  43. */
  44. this.isRGraph = true;
  45. /**
  46. * This adds a uid to the object that you can use for identification purposes
  47. */
  48. this.uid = RGraph.CreateUID();
  49. /**
  50. * This adds a UID to the canvas for identification purposes
  51. */
  52. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  53. /**
  54. * This does a few things, for example adding the .fillText() method to the canvas 2D context when
  55. * it doesn't exist. This facilitates the graphs to be still shown in older browser (though without
  56. * text obviously). You'll find the function in RGraph.common.core.js
  57. */
  58. RGraph.OldBrowserCompat(this.context);
  59. /**
  60. * Some example background properties
  61. */
  62. this.properties =
  63. {
  64. 'chart.strokestyle': 'rgba(0,0,0,0)',
  65. 'chart.fillstyle': 'red',
  66. 'chart.events.click': null,
  67. 'chart.events.mousemove': null,
  68. 'chart.shadow': false,
  69. 'chart.shadow.color': 'gray',
  70. 'chart.shadow.offsetx': 3,
  71. 'chart.shadow.offsety': 3,
  72. 'chart.shadow.blur': 5,
  73. 'chart.highlight.stroke': 'black',
  74. 'chart.highlight.fill': 'rgba(255,255,255,0.7)',
  75. 'chart.tooltips': null,
  76. 'chart.tooltips.effect': 'fade',
  77. 'chart.tooltips.css.class':'RGraph_tooltip',
  78. 'chart.tooltips.event': 'onclick',
  79. 'chart.tooltips.highlight':true,
  80. 'chart.tooltips.coords.page': false,
  81. 'chart.tooltips.valign': 'top'
  82. }
  83. /**
  84. * A simple check that the browser has canvas support
  85. */
  86. if (!this.canvas) {
  87. alert('[DRAWING.RECT] No canvas support');
  88. return;
  89. }
  90. /**
  91. * This can be used to store the coordinates of shapes on the graph
  92. */
  93. this.coords = [[Math.round(x), Math.round(y), w, h]];
  94. /**
  95. * Create the dollar object so that functions can be added to them
  96. */
  97. this.$0 = {};
  98. /**
  99. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  100. * done already
  101. */
  102. if (!this.canvas.__rgraph_aa_translated__) {
  103. this.context.translate(0.5,0.5);
  104. this.canvas.__rgraph_aa_translated__ = true;
  105. }
  106. ///////////////////////////////// SHORT PROPERTIES /////////////////////////////////
  107. var RG = RGraph;
  108. var ca = this.canvas;
  109. var co = ca.getContext('2d');
  110. var prop = this.properties;
  111. //////////////////////////////////// METHODS ///////////////////////////////////////
  112. /**
  113. * A setter method for setting graph properties. It can be used like this: obj.Set('chart.strokestyle', '#666');
  114. *
  115. * @param name string The name of the property to set
  116. * @param value mixed The value of the property
  117. */
  118. this.Set = function (name, value)
  119. {
  120. name = name.toLowerCase();
  121. /**
  122. * This should be done first - prepend the propertyy name with "chart." if necessary
  123. */
  124. if (name.substr(0,6) != 'chart.') {
  125. name = 'chart.' + name;
  126. }
  127. prop[name] = value;
  128. return this;
  129. }
  130. /**
  131. * A getter method for retrieving graph properties. It can be used like this: obj.Get('chart.strokestyle');
  132. *
  133. * @param name string The name of the property to get
  134. */
  135. this.Get = function (name)
  136. {
  137. /**
  138. * This should be done first - prepend the property name with "chart." if necessary
  139. */
  140. if (name.substr(0,6) != 'chart.') {
  141. name = 'chart.' + name;
  142. }
  143. return prop[name.toLowerCase()];
  144. }
  145. /**
  146. * Draws the rectangle
  147. */
  148. this.Draw = function ()
  149. {
  150. /**
  151. * Fire the onbeforedraw event
  152. */
  153. RG.FireCustomEvent(this, 'onbeforedraw');
  154. /**
  155. * Parse the colors. This allows for simple gradient syntax
  156. */
  157. if (!this.colorsParsed) {
  158. this.parseColors();
  159. // Don't want to do this again
  160. this.colorsParsed = true;
  161. }
  162. co.beginPath();
  163. co.strokeStyle = prop['chart.strokestyle'];
  164. co.fillStyle = prop['chart.fillstyle'];
  165. if (prop['chart.shadow']) {
  166. RG.SetShadow(this, prop['chart.shadow.color'], prop['chart.shadow.offsetx'], prop['chart.shadow.offsety'], prop['chart.shadow.blur']);
  167. }
  168. co.rect(this.coords[0][0], this.coords[0][1], this.coords[0][2], this.coords[0][3]);
  169. co.stroke();
  170. co.fill();
  171. // Turn the shadow off
  172. RG.NoShadow(this);
  173. /**
  174. * This installs the event listeners
  175. */
  176. RG.InstallEventListeners(this);
  177. /**
  178. * Fire the ondraw event
  179. */
  180. RG.FireCustomEvent(this, 'ondraw');
  181. return this;
  182. }
  183. /**
  184. * The getObjectByXY() worker method
  185. */
  186. this.getObjectByXY = function (e)
  187. {
  188. if (this.getShape(e)) {
  189. return this;
  190. }
  191. }
  192. /**
  193. * Not used by the class during creating the graph, but is used by event handlers
  194. * to get the coordinates (if any) of the selected bar
  195. *
  196. * @param object e The event object
  197. * @param object OPTIONAL You can pass in the bar object instead of the
  198. * function using "this"
  199. */
  200. this.getShape = function (e)
  201. {
  202. var mouseXY = RGraph.getMouseXY(e);
  203. var mouseX = mouseXY[0];
  204. var mouseY = mouseXY[1];
  205. for (var i=0,len=this.coords.length; i<len; i++) {
  206. var coords = this.coords[i];
  207. var left = coords[0];
  208. var top = coords[1];
  209. var width = coords[2];
  210. var height = coords[3];
  211. if (mouseX >= left && mouseX <= (left + width) && mouseY >= top && mouseY <= (top + height)) {
  212. return {
  213. 0: this, 1: left, 2: top, 3: width, 4: height, 5: 0,
  214. 'object': this, 'x': left, 'y': top, 'width': width, 'height': height, 'index': 0, 'tooltip': prop['chart.tooltips'] ? prop['chart.tooltips'][0] : null
  215. };
  216. }
  217. }
  218. return null;
  219. }
  220. /**
  221. * This function positions a tooltip when it is displayed
  222. *
  223. * @param obj object The chart object
  224. * @param int x The X coordinate specified for the tooltip
  225. * @param int y The Y coordinate specified for the tooltip
  226. * @param objec tooltip The tooltips DIV element
  227. */
  228. this.positionTooltip = function (obj, x, y, tooltip, idx)
  229. {
  230. var coordX = obj.coords[0][0];
  231. var coordY = obj.coords[0][1];
  232. var coordW = obj.coords[0][2];
  233. var coordH = obj.coords[0][3];
  234. var canvasXY = RG.getCanvasXY(obj.canvas);
  235. var width = tooltip.offsetWidth;
  236. var height = tooltip.offsetHeight;
  237. // Set the top position
  238. tooltip.style.left = 0;
  239. if (prop['chart.tooltips.valign'] == 'center') {
  240. tooltip.style.top = canvasXY[1] + coordY + (coordH / 2) -height + 'px';
  241. } else {
  242. tooltip.style.top = canvasXY[1] + coordY - height - 7 + 'px';
  243. }
  244. // By default any overflow is hidden
  245. tooltip.style.overflow = '';
  246. // The arrow
  247. var img = new Image();
  248. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
  249. img.style.position = 'absolute';
  250. img.id = '__rgraph_tooltip_pointer__';
  251. img.style.top = (tooltip.offsetHeight - 2) + 'px';
  252. tooltip.appendChild(img);
  253. // Reposition the tooltip if at the edges:
  254. // LEFT edge
  255. if ((canvasXY[0] + coordX + (coordW / 2) - (width / 2)) < 10) {
  256. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + (coordW / 2) + 'px';
  257. img.style.left = ((width * 0.1) - 8.5) + 'px';
  258. // RIGHT edge
  259. } else if ((canvasXY[0] + coordX + (width / 2)) > document.body.offsetWidth) {
  260. tooltip.style.left = canvasXY[0] + coordX - (width * 0.9) + (coordW / 2) + 'px';
  261. img.style.left = ((width * 0.9) - 8.5) + 'px';
  262. // Default positioning - CENTERED
  263. } else {
  264. tooltip.style.left = (canvasXY[0] + coordX + (coordW / 2) - (width * 0.5)) + 'px';
  265. img.style.left = ((width * 0.5) - 8.5) + 'px';
  266. }
  267. }
  268. /**
  269. * Each object type has its own Highlight() function which highlights the appropriate shape
  270. *
  271. * @param object shape The shape to highlight
  272. */
  273. this.Highlight = function (shape)
  274. {
  275. // Add the new highlight
  276. RG.Highlight.Rect(this, shape);
  277. }
  278. /**
  279. * This allows for easy specification of gradients
  280. */
  281. this.parseColors = function ()
  282. {
  283. /**
  284. * Parse various properties for colors
  285. */
  286. prop['chart.fillstyle'] = this.parseSingleColorForGradient(prop['chart.fillstyle']);
  287. prop['chart.strokestyle'] = this.parseSingleColorForGradient(prop['chart.strokestyle']);
  288. prop['chart.highlight.stroke'] = this.parseSingleColorForGradient(prop['chart.highlight.stroke']);
  289. prop['chart.highlight.fill'] = this.parseSingleColorForGradient(prop['chart.highlight.fill']);
  290. }
  291. /**
  292. * This parses a single color value
  293. */
  294. this.parseSingleColorForGradient = function (color)
  295. {
  296. if (!color) {
  297. return color;
  298. }
  299. if (color.match(/^gradient\((.*)\)$/i)) {
  300. var parts = RegExp.$1.split(':');
  301. // Create the gradient
  302. var grad = co.createLinearGradient(0,0,ca.width,0);
  303. var diff = 1 / (parts.length - 1);
  304. grad.addColorStop(0, RG.trim(parts[0]));
  305. for (var j=1,len=parts.length; j<len; ++j) {
  306. grad.addColorStop(j * diff, RG.trim(parts[j]));
  307. }
  308. }
  309. return grad ? grad : color;
  310. }
  311. /**
  312. * Objects are now always registered so that the chart is redrawn if need be.
  313. */
  314. RG.Register(this);
  315. }