RGraph.drawing.poly.js 14 KB

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