RGraph.drawing.circle.js 15 KB

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