RGraph.drawing.circle.js 13 KB

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