RGraph.drawing.marker1.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 label
  24. * @param number y The Y position of the label
  25. * @param number text The text used - should be a single character (unless you've significantly increased
  26. * the size of the marker.
  27. */
  28. RGraph.Drawing.Marker1 = function (id, x, y, radius, text)
  29. {
  30. var tmp = RGraph.getCanvasTag(id);
  31. // Get the canvas and context objects
  32. this.id = tmp[0];
  33. this.canvas = tmp[1];
  34. this.context = this.canvas.getContext ? this.canvas.getContext("2d", {alpha: (typeof id === 'object' && id.alpha === false) ? false : true}) : null;
  35. this.colorsParsed = false;
  36. this.canvas.__object__ = this;
  37. this.original_colors = [];
  38. this.firstDraw = true; // After the first draw this will be false
  39. /**
  40. * Store the properties
  41. */
  42. this.centerx = x;
  43. this.centery = y;
  44. this.radius = radius;
  45. this.text = text;
  46. /**
  47. * This defines the type of this shape
  48. */
  49. this.type = 'drawing.marker1';
  50. /**
  51. * This facilitates easy object identification, and should always be true
  52. */
  53. this.isRGraph = true;
  54. /**
  55. * This adds a uid to the object that you can use for identification purposes
  56. */
  57. this.uid = RGraph.CreateUID();
  58. /**
  59. * This adds a UID to the canvas for identification purposes
  60. */
  61. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  62. /**
  63. * This does a few things, for example adding the .fillText() method to the canvas 2D context when
  64. * it doesn't exist. This facilitates the graphs to be still shown in older browser (though without
  65. * text obviously). You'll find the function in RGraph.common.core.js
  66. */
  67. //RGraph.OldBrowserCompat(this.context);
  68. /**
  69. * Some example background properties
  70. */
  71. this.properties =
  72. {
  73. 'chart.strokestyle': 'black',
  74. 'chart.fillstyle': 'white',
  75. 'chart.linewidth': 2,
  76. 'chart.text.color': 'black',
  77. 'chart.text.size': 12,
  78. 'chart.text.font': 'Arial',
  79. 'chart.events.click': null,
  80. 'chart.events.mousemove': null,
  81. 'chart.shadow': true,
  82. 'chart.shadow.color': '#aaa',
  83. 'chart.shadow.offsetx': 0,
  84. 'chart.shadow.offsety': 0,
  85. 'chart.shadow.blur': 15,
  86. 'chart.highlight.stroke': 'rgba(0,0,0,0)',
  87. 'chart.highlight.fill': 'rgba(255,0,0,0.7)',
  88. 'chart.tooltips': null,
  89. 'chart.tooltips.highlight': true,
  90. 'chart.tooltips.event': 'onclick',
  91. 'chart.align': 'center'
  92. }
  93. /**
  94. * A simple check that the browser has canvas support
  95. */
  96. if (!this.canvas) {
  97. alert('[DRAWING.MARKER1] No canvas support');
  98. return;
  99. }
  100. /**
  101. * Create the dollar object so that functions can be added to them
  102. */
  103. this.$0 = {};
  104. /**
  105. * Arrays that store the coordinates
  106. */
  107. this.coords = [];
  108. this.coordsText = [];
  109. /**
  110. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  111. * done already
  112. */
  113. if (!this.canvas.__rgraph_aa_translated__) {
  114. this.context.translate(0.5,0.5);
  115. this.canvas.__rgraph_aa_translated__ = true;
  116. }
  117. // Short variable names
  118. var RG = RGraph;
  119. var ca = this.canvas;
  120. var co = ca.getContext('2d');
  121. var prop = this.properties;
  122. var jq = jQuery;
  123. var pa = RG.Path;
  124. var win = window;
  125. var doc = document;
  126. var ma = Math;
  127. /**
  128. * "Decorate" the object with the generic effects if the effects library has been included
  129. */
  130. if (RG.Effects && typeof RG.Effects.decorate === 'function') {
  131. RG.Effects.decorate(this);
  132. }
  133. /**
  134. * A setter method for setting graph properties. It can be used like this: obj.Set('chart.strokestyle', '#666');
  135. *
  136. * @param name string The name of the property to set
  137. * @param value mixed The value of the property
  138. */
  139. this.set =
  140. this.Set = function (name, value)
  141. {
  142. name = name.toLowerCase();
  143. /**
  144. * This should be done first - prepend the propertyy name with "chart." if necessary
  145. */
  146. if (name.substr(0,6) != 'chart.') {
  147. name = 'chart.' + name;
  148. }
  149. prop[name] = value;
  150. return this;
  151. };
  152. /**
  153. * A getter method for retrieving graph properties. It can be used like this: obj.Get('chart.strokestyle');
  154. *
  155. * @param name string The name of the property to get
  156. */
  157. this.get =
  158. this.Get = function (name)
  159. {
  160. /**
  161. * This should be done first - prepend the property name with "chart." if necessary
  162. */
  163. if (name.substr(0,6) != 'chart.') {
  164. name = 'chart.' + name;
  165. }
  166. return prop[name.toLowerCase()];
  167. };
  168. /**
  169. * Draws the circle
  170. */
  171. this.draw =
  172. this.Draw = function ()
  173. {
  174. /**
  175. * Fire the onbeforedraw event
  176. */
  177. RG.FireCustomEvent(this, 'onbeforedraw');
  178. var r = this.radius;
  179. if (prop['chart.align'] == 'left') {
  180. this.markerCenterx = this.centerx - r - r - 3;
  181. this.markerCentery = this.centery - r - r - 3;
  182. } else if (prop['chart.align'] == 'right') {
  183. this.markerCenterx = this.centerx + r + r + 3;
  184. this.markerCentery = this.centery - r - r - 3;
  185. } else {
  186. this.markerCenterx = this.centerx;
  187. this.markerCentery = this.centery - r - r - 3;
  188. }
  189. /**
  190. * Parse the colors. This allows for simple gradient syntax
  191. */
  192. if (!this.colorsParsed) {
  193. this.parseColors();
  194. // Don't want to do this again
  195. this.colorsParsed = true;
  196. }
  197. /**
  198. * Stop this growing uncontrollably
  199. */
  200. this.coordsText = [];
  201. /**
  202. * DRAW THE MARKER HERE
  203. */
  204. pa(co, ['b','lw',prop['chart.linewidth']]);
  205. if (prop['chart.shadow']) {
  206. RG.SetShadow(this, prop['chart.shadow.color'], prop['chart.shadow.offsetx'], prop['chart.shadow.offsety'], prop['chart.shadow.blur']);
  207. }
  208. this.DrawMarker();
  209. pa(co, ['c','s',prop['chart.strokestyle'],'f',prop['chart.fillstyle']]);
  210. // Turn the shadow off
  211. RG.NoShadow(this);
  212. // Now draw the text on the marker
  213. co.fillStyle = prop['chart.text.color'];
  214. // Draw the text on the marker
  215. RG.Text2(this, {'font':prop['chart.text.font'],
  216. 'size':prop['chart.text.size'],
  217. 'x':this.coords[0][0] - 1,
  218. 'y':this.coords[0][1] - 1,
  219. 'text':this.text,
  220. 'valign':'center',
  221. 'halign':'center',
  222. 'tag': 'labels'
  223. });
  224. /**
  225. * This installs the event listeners
  226. */
  227. RG.InstallEventListeners(this);
  228. /**
  229. * Fire the onfirstdraw event
  230. */
  231. if (this.firstDraw) {
  232. RG.fireCustomEvent(this, 'onfirstdraw');
  233. this.firstDrawFunc();
  234. this.firstDraw = false;
  235. }
  236. /**
  237. * Fire the ondraw event
  238. */
  239. RG.FireCustomEvent(this, 'ondraw');
  240. return this;
  241. };
  242. /**
  243. * The getObjectByXY() worker method
  244. */
  245. this.getObjectByXY = function (e)
  246. {
  247. if (this.getShape(e)) {
  248. return this;
  249. }
  250. };
  251. /**
  252. * Not used by the class during creating the shape, but is used by event handlers
  253. * to get the coordinates (if any) of the selected bar
  254. *
  255. * @param object e The event object
  256. * @param object OPTIONAL You can pass in the bar object instead of the
  257. * function using "this"
  258. */
  259. this.getShape = function (e)
  260. {
  261. var mouseXY = RG.getMouseXY(e);
  262. var mouseX = mouseXY[0];
  263. var mouseY = mouseXY[1];
  264. /**
  265. * Path the marker but DON'T STROKE OR FILL it
  266. */
  267. co.beginPath();
  268. this.DrawMarker();
  269. if (co.isPointInPath(mouseXY[0], mouseXY[1])) {
  270. return {
  271. 0: this, 1: this.coords[0][0], 2: this.coords[0][1], 3: this.coords[0][2], 4: 0,
  272. 'object': this, 'x': this.coords[0][0], 'y': this.coords[0][1], 'radius': this.coords[0][2], 'index': 0, 'tooltip': prop['chart.tooltips'] ? prop['chart.tooltips'][0] : null
  273. };
  274. }
  275. return null;
  276. };
  277. /**
  278. * This function positions a tooltip when it is displayed
  279. *
  280. * @param obj object The chart object
  281. * @param int x The X coordinate specified for the tooltip
  282. * @param int y The Y coordinate specified for the tooltip
  283. * @param object tooltip The tooltips DIV element
  284. * @param number idx The index of the tooltip
  285. */
  286. this.positionTooltip = function (obj, x, y, tooltip, idx)
  287. {
  288. var canvasXY = RG.getCanvasXY(obj.canvas);
  289. var width = tooltip.offsetWidth;
  290. var height = tooltip.offsetHeight;
  291. // Set the top position
  292. tooltip.style.left = 0;
  293. tooltip.style.top = canvasXY[1] + this.coords[0][1] - (height / 2) - this.radius + 'px';
  294. // By default any overflow is hidden
  295. tooltip.style.overflow = '';
  296. // The arrow
  297. var img = new Image();
  298. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
  299. img.style.position = 'absolute';
  300. img.id = '__rgraph_tooltip_pointer__';
  301. img.style.top = (tooltip.offsetHeight - 2) + 'px';
  302. tooltip.appendChild(img);
  303. // Reposition the tooltip if at the edges:
  304. // LEFT edge
  305. if ((canvasXY[0] + obj.coords[0][0] + (obj.coords[0][2] / 2) - (width / 2)) < 10) {
  306. tooltip.style.left = canvasXY[0] + this.markerCenterx - (width * 0.1) + 'px';
  307. img.style.left = ((width * 0.1) - 8.5) + 'px';
  308. // RIGHT edge
  309. } else if ((canvasXY[0] + this.coords[0][0] + (this.coords[0][2] / 2) + (width / 2)) > doc.body.offsetWidth) {
  310. tooltip.style.left = canvasXY[0] + this.markerCenterx - (width * 0.9) + 'px';
  311. img.style.left = ((width * 0.9) - 8.5) + 'px';
  312. // Default positioning - CENTERED
  313. } else {
  314. tooltip.style.left = (canvasXY[0] + this.markerCenterx - (width * 0.5)) + 'px';
  315. img.style.left = ((width * 0.5) - 8.5) + 'px';
  316. }
  317. };
  318. /**
  319. * Each object type has its own Highlight() function which highlights the appropriate shape
  320. *
  321. * @param object shape The shape to highlight
  322. */
  323. this.highlight =
  324. this.Highlight = function (shape)
  325. {
  326. if (prop['chart.tooltips.highlight']) {
  327. pa(this, ['b','fu',function (obj){obj.DrawMarker();},'c','s',prop['chart.highlight.stroke'],'f',prop['chart.highlight.fill']]);
  328. }
  329. };
  330. /**
  331. * This function is used to encapsulate the actual drawing of the marker. It
  332. * intentional does not start a path or set colors.
  333. */
  334. this.drawMarker =
  335. this.DrawMarker = function ()
  336. {
  337. var r = this.radius;
  338. if (prop['chart.align'] == 'left') {
  339. var x = this.markerCenterx;
  340. var y = this.markerCentery;
  341. pa(co, ['a',x,y,r,RG.HALFPI,RG.TWOPI,false]);
  342. // special case for MSIE 7/8
  343. if (RG.ISOLD) {
  344. pa(co,['m',x + r + r,y+r+r,'qc',x + r,y + r,x + r + 1,y,'m',x + r + r,y+r+r]);
  345. } else {
  346. pa(co, ['qc',x + r,y + r,x + r + r,y + r + r]);
  347. }
  348. pa(co, ['qc',x + r,y + r,x,y + r + (RG.ISOLD ? 1 : 0)]);
  349. } else if (prop['chart.align'] == 'right') {
  350. var x = this.markerCenterx;
  351. var y = this.markerCentery;
  352. pa(co, ['a',x,y,r,RG.HALFPI,RG.PI,true]);
  353. // special case for MSIE 7/8
  354. if (RG.ISOLD) {
  355. pa(co, ['m',x - r - r,y+r+r,'qc',x - r,y + r,x - r - 1,y,'m',x - r - r,y+r+r]);
  356. } else {
  357. pa(co, ['qc',x - r,y + r,x - r - r,y + r + r]);
  358. }
  359. pa(co, ['qc',x - r, y + r, x, y + r + (RG.ISOLD ? 1 : 0)]);
  360. // Default is center
  361. } else {
  362. var x = this.markerCenterx;
  363. var y = this.markerCentery;
  364. pa(co, ['a',x, y, r, RG.HALFPI / 2, RG.PI - (RG.HALFPI / 2), true]);
  365. //co.arc(x, y, r, HALFPI / 2, PI - (HALFPI / 2), true);
  366. // special case for MSIE 7/8
  367. if (RG.ISOLD) {
  368. pa(co, ['m',x,y+r+r-2,'qc',x,y + r + (r / 4),x - (Math.cos(RG.HALFPI / 2) * r),y + (Math.sin(RG.HALFPI / 2) * r),'m',x, y+r+r-2]);
  369. } else {
  370. pa(co, ['qc',x,y + r + (r / 4),x,y + r + r - 2]);
  371. }
  372. pa(co, ['qc',x,y + r + (r / 4),x + (Math.cos(RG.HALFPI / 2) * r),y + (Math.sin(RG.HALFPI / 2) * r)]);
  373. }
  374. this.coords[0] = [x, y, r];
  375. };
  376. /**
  377. * This allows for easy specification of gradients
  378. */
  379. this.parseColors = function ()
  380. {
  381. // Save the original colors so that they can be restored when the canvas is reset
  382. if (this.original_colors.length === 0) {
  383. this.original_colors['chart.fillstyle'] = RG.array_clone(prop['chart.fillstyle']);
  384. this.original_colors['chart.strokestyle'] = RG.array_clone(prop['chart.strokestyle']);
  385. this.original_colors['chart.highlight.fill'] = RG.array_clone(prop['chart.highlight.fill']);
  386. this.original_colors['chart.highlight.stroke'] = RG.array_clone(prop['chart.highlight.stroke']);
  387. this.original_colors['chart.text.color'] = RG.array_clone(prop['chart.text.color']);
  388. }
  389. /**
  390. * Parse various properties for colors
  391. */
  392. prop['chart.fillstyle'] = this.parseSingleColorForGradient(prop['chart.fillstyle']);
  393. prop['chart.strokestyle'] = this.parseSingleColorForGradient(prop['chart.strokestyle']);
  394. prop['chart.highlight.stroke'] = this.parseSingleColorForGradient(prop['chart.highlight.stroke']);
  395. prop['chart.highlight.fill'] = this.parseSingleColorForGradient(prop['chart.highlight.fill']);
  396. prop['chart.text.color'] = this.parseSingleColorForGradient(prop['chart.text.color']);
  397. };
  398. /**
  399. * This parses a single color value
  400. */
  401. this.parseSingleColorForGradient = function (color)
  402. {
  403. if (!color || typeof(color) != 'string') {
  404. return color;
  405. }
  406. if (color.match(/^gradient\((.*)\)$/i)) {
  407. var parts = RegExp.$1.split(':');
  408. // Create the gradient
  409. var grad = co.createRadialGradient(this.markerCenterx, this.markerCentery, 0, this.markerCenterx, this.markerCentery, this.radius);
  410. var diff = 1 / (parts.length - 1);
  411. grad.addColorStop(0, RG.trim(parts[0]));
  412. for (var j=1; j<parts.length; ++j) {
  413. grad.addColorStop(j * diff, RG.trim(parts[j]));
  414. }
  415. }
  416. return grad ? grad : color;
  417. };
  418. /**
  419. * Using a function to add events makes it easier to facilitate method chaining
  420. *
  421. * @param string type The type of even to add
  422. * @param function func
  423. */
  424. this.on = function (type, func)
  425. {
  426. if (type.substr(0,2) !== 'on') {
  427. type = 'on' + type;
  428. }
  429. this[type] = func;
  430. return this;
  431. };
  432. /**
  433. * This function runs once only
  434. * (put at the end of the file (before any effects))
  435. */
  436. this.firstDrawFunc = function ()
  437. {
  438. };
  439. /**
  440. * Objects are now always registered so that the chart is redrawn if need be.
  441. */
  442. RG.Register(this);
  443. };