RGraph.rscatter.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. RGraph = window.RGraph || {isRGraph: true};
  12. /**
  13. * The chart constuctor
  14. *
  15. * @param object canvas
  16. * @param array data
  17. */
  18. RGraph.RScatter =
  19. RGraph.Rscatter = function (id)
  20. {
  21. var tmp = RGraph.getCanvasTag(id);
  22. // Get the canvas and context objects
  23. this.id = tmp[0];
  24. this.canvas = tmp[1];
  25. this.context = this.canvas.getContext ? this.canvas.getContext("2d", {alpha: (typeof id === 'object' && id.alpha === false) ? false : true}) : null;
  26. this.canvas.__object__ = this;
  27. this.type = 'rscatter';
  28. this.hasTooltips = false;
  29. this.isRGraph = true;
  30. this.uid = RGraph.CreateUID();
  31. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  32. this.colorsParsed = false;
  33. this.coordsText = [];
  34. this.original_colors = [];
  35. this.firstDraw = true; // After the first draw this will be false
  36. /**
  37. * Compatibility with older browsers
  38. */
  39. //RGraph.OldBrowserCompat(this.context);
  40. this.centerx = 0;
  41. this.centery = 0;
  42. this.radius = 0;
  43. this.max = 0;
  44. this.properties =
  45. {
  46. 'chart.radius': null,
  47. 'chart.colors': [], // This is used internally for the key
  48. 'chart.colors.default': 'black',
  49. 'chart.gutter.left': 25,
  50. 'chart.gutter.right': 25,
  51. 'chart.gutter.top': 25,
  52. 'chart.gutter.bottom': 25,
  53. 'chart.title': '',
  54. 'chart.title.background': null,
  55. 'chart.title.hpos': null,
  56. 'chart.title.vpos': null,
  57. 'chart.title.bold': true,
  58. 'chart.title.font': null,
  59. 'chart.title.x': null,
  60. 'chart.title.y': null,
  61. 'chart.title.halign': null,
  62. 'chart.title.valign': null,
  63. 'chart.labels': null,
  64. 'chart.labels.position': 'center',
  65. 'chart.labels.axes': 'nsew',
  66. 'chart.text.color': 'black',
  67. 'chart.text.font': 'Arial',
  68. 'chart.text.size': 10,
  69. 'chart.key': null,
  70. 'chart.key.background': 'white',
  71. 'chart.key.position': 'graph',
  72. 'chart.key.halign': 'right',
  73. 'chart.key.shadow': false,
  74. 'chart.key.shadow.color': '#666',
  75. 'chart.key.shadow.blur': 3,
  76. 'chart.key.shadow.offsetx': 2,
  77. 'chart.key.shadow.offsety': 2,
  78. 'chart.key.position.gutter.boxed':false,
  79. 'chart.key.position.x': null,
  80. 'chart.key.position.y': null,
  81. 'chart.key.color.shape': 'square',
  82. 'chart.key.rounded': true,
  83. 'chart.key.linewidth': 1,
  84. 'chart.key.colors': null,
  85. 'chart.key.interactive': false,
  86. 'chart.key.interactive.highlight.chart.fill':'rgba(255,0,0,0.9)',
  87. 'chart.key.interactive.highlight.label':'rgba(255,0,0,0.2)',
  88. 'chart.key.text.color': 'black',
  89. 'chart.contextmenu': null,
  90. 'chart.tooltips': null,
  91. 'chart.tooltips.event': 'onmousemove',
  92. 'chart.tooltips.effect': 'fade',
  93. 'chart.tooltips.css.class': 'RGraph_tooltip',
  94. 'chart.tooltips.highlight': true,
  95. 'chart.tooltips.hotspot': 3,
  96. 'chart.tooltips.coords.page': false,
  97. 'chart.annotatable': false,
  98. 'chart.annotate.color': 'black',
  99. 'chart.zoom.factor': 1.5,
  100. 'chart.zoom.fade.in': true,
  101. 'chart.zoom.fade.out': true,
  102. 'chart.zoom.hdir': 'right',
  103. 'chart.zoom.vdir': 'down',
  104. 'chart.zoom.frames': 25,
  105. 'chart.zoom.delay': 16.666,
  106. 'chart.zoom.shadow': true,
  107. 'chart.zoom.background': true,
  108. 'chart.zoom.action': 'zoom',
  109. 'chart.resizable': false,
  110. 'chart.resize.handle.background': null,
  111. 'chart.ymax': null,
  112. 'chart.ymin': 0,
  113. 'chart.tickmarks': 'cross',
  114. 'chart.ticksize': 3,
  115. 'chart.scale.decimals': null,
  116. 'chart.scale.point': '.',
  117. 'chart.scale.thousand': ',',
  118. 'chart.scale.round': false,
  119. 'chart.units.pre': '',
  120. 'chart.units.post': '',
  121. 'chart.events.mousemove': null,
  122. 'chart.events.click': null,
  123. 'chart.highlight.stroke': 'transparent',
  124. 'chart.highlight.fill': 'rgba(255,255,255,0.7)',
  125. 'chart.highlight.point.radius': 3,
  126. 'chart.labels.count': 5
  127. }
  128. this.data = [];
  129. // Handle multiple datasets being given as one argument
  130. if (arguments[1][0] && arguments[1][0][0] && typeof arguments[1][0][0] == 'object') {
  131. // Store the data set(s)
  132. for (var i=0; i<arguments[1].length; ++i) {
  133. this.data[i] = arguments[1][i];
  134. }
  135. // Handle multiple data sets being supplied as seperate arguments
  136. } else {
  137. // Store the data set(s)
  138. for (var i=1; i<arguments.length; ++i) {
  139. this.data[i - 1] = RGraph.array_clone(arguments[i]);
  140. }
  141. }
  142. /**
  143. * Create the $ objects so that functions can be added to them
  144. */
  145. for (var i=0,idx=0; i<this.data.length; ++i) {
  146. for (var j=0,len=this.data[i].length; j<len; j+=1,idx+=1) {
  147. this['$' + idx] = {}
  148. }
  149. }
  150. /**
  151. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  152. * done already
  153. */
  154. if (!this.canvas.__rgraph_aa_translated__) {
  155. this.context.translate(0.5,0.5);
  156. this.canvas.__rgraph_aa_translated__ = true;
  157. }
  158. // Short variable names
  159. var RG = RGraph;
  160. var ca = this.canvas;
  161. var co = ca.getContext('2d');
  162. var prop = this.properties;
  163. var jq = jQuery;
  164. var pa = RG.Path;
  165. var win = window;
  166. var doc = document;
  167. var ma = Math;
  168. /**
  169. * "Decorate" the object with the generic effects if the effects library has been included
  170. */
  171. if (RG.Effects && typeof RG.Effects.decorate === 'function') {
  172. RG.Effects.decorate(this);
  173. }
  174. /**
  175. * A simple setter
  176. *
  177. * @param string name The name of the property to set
  178. * @param string value The value of the property
  179. */
  180. this.set =
  181. this.Set = function (name, value)
  182. {
  183. /**
  184. * This should be done first - prepend the property name with "chart." if necessary
  185. */
  186. if (name.substr(0,6) != 'chart.') {
  187. name = 'chart.' + name;
  188. }
  189. prop[name.toLowerCase()] = value;
  190. return this;
  191. };
  192. /**
  193. * A simple getter
  194. *
  195. * @param string name The name of the property to get
  196. */
  197. this.get =
  198. this.Get = function (name)
  199. {
  200. /**
  201. * This should be done first - prepend the property name with "chart." if necessary
  202. */
  203. if (name.substr(0,6) != 'chart.') {
  204. name = 'chart.' + name;
  205. }
  206. return prop[name.toLowerCase()];
  207. };
  208. /**
  209. * This method draws the rose chart
  210. */
  211. this.draw =
  212. this.Draw = function ()
  213. {
  214. /**
  215. * Fire the onbeforedraw event
  216. */
  217. RG.FireCustomEvent(this, 'onbeforedraw');
  218. /**
  219. * This doesn't affect the chart, but is used for compatibility
  220. */
  221. this.gutterLeft = prop['chart.gutter.left'];
  222. this.gutterRight = prop['chart.gutter.right'];
  223. this.gutterTop = prop['chart.gutter.top'];
  224. this.gutterBottom = prop['chart.gutter.bottom'];
  225. // Calculate the radius
  226. this.radius = (Math.min(ca.width - this.gutterLeft - this.gutterRight, ca.height - this.gutterTop - this.gutterBottom) / 2);
  227. this.centerx = ((ca.width - this.gutterLeft - this.gutterRight) / 2) + this.gutterLeft;
  228. this.centery = ((ca.height - this.gutterTop - this.gutterBottom) / 2) + this.gutterTop;
  229. this.coords = [];
  230. this.coords2 = [];
  231. /**
  232. * Stop this growing uncontrollably
  233. */
  234. this.coordsText = [];
  235. /**
  236. * If there's a user specified radius/centerx/centery, use them
  237. */
  238. if (typeof(prop['chart.centerx']) == 'number') this.centerx = prop['chart.centerx'];
  239. if (typeof(prop['chart.centery']) == 'number') this.centery = prop['chart.centery'];
  240. if (typeof(prop['chart.radius']) == 'number') this.radius = prop['chart.radius'];
  241. /**
  242. * Parse the colors for gradients. Its down here so that the center X/Y can be used
  243. */
  244. if (!this.colorsParsed) {
  245. this.parseColors();
  246. // Don't want to do this again
  247. this.colorsParsed = true;
  248. }
  249. /**
  250. * Work out the scale
  251. */
  252. var max = prop['chart.ymax'];
  253. var min = prop['chart.ymin'];
  254. if (typeof(max) == 'number') {
  255. this.max = max;
  256. this.scale2 = RG.getScale2(this, {'max':max,
  257. 'min':min,
  258. 'strict':true,
  259. 'scale.decimals':Number(prop['chart.scale.decimals']),
  260. 'scale.point':prop['chart.scale.point'],
  261. 'scale.thousand':prop['chart.scale.thousand'],
  262. 'scale.round':prop['chart.scale.round'],
  263. 'units.pre':prop['chart.units.pre'],
  264. 'units.post':prop['chart.units.post'],
  265. 'ylabels.count':prop['chart.labels.count']
  266. });
  267. } else {
  268. for (var i=0; i<this.data.length; i+=1) {
  269. for (var j=0,len=this.data[i].length; j<len; j+=1) {
  270. this.max = Math.max(this.max, this.data[i][j][1]);
  271. }
  272. }
  273. this.min = prop['chart.ymin'];
  274. this.scale2 = RG.getScale2(this, {'max':this.max,
  275. 'min':min,
  276. 'scale.decimals':Number(prop['chart.scale.decimals']),
  277. 'scale.point':prop['chart.scale.point'],
  278. 'scale.thousand':prop['chart.scale.thousand'],
  279. 'scale.round':prop['chart.scale.round'],
  280. 'units.pre':prop['chart.units.pre'],
  281. 'units.post':prop['chart.units.post'],
  282. 'ylabels.count':prop['chart.labels.count']
  283. });
  284. this.max = this.scale2.max;
  285. }
  286. /**
  287. * Change the centerx marginally if the key is defined
  288. */
  289. if (prop['chart.key'] && prop['chart.key'].length > 0 && prop['chart.key'].length >= 3) {
  290. this.centerx = this.centerx - prop['chart.gutter.right'] + 5;
  291. }
  292. /**
  293. * Populate the colors array for the purposes of generating the key
  294. */
  295. if (typeof(prop['chart.key']) == 'object' && RG.is_array(prop['chart.key']) && prop['chart.key'][0]) {
  296. // Reset the colors array
  297. prop['chart.colors'] = [];
  298. for (var i=0; i<this.data.length; i+=1) {
  299. for (var j=0,len=this.data[i].length; j<len; j+=1) {
  300. if (typeof this.data[i][j][2] == 'string') {
  301. prop['chart.colors'].push(this.data[i][j][2]);
  302. }
  303. }
  304. }
  305. }
  306. /**
  307. * Populate the chart.tooltips array
  308. */
  309. this.Set('chart.tooltips', []);
  310. for (var i=0; i<this.data.length; i+=1) {
  311. for (var j=0,len=this.data[i].length; j<len; j+=1) {
  312. if (typeof this.data[i][j][3] == 'string') {
  313. prop['chart.tooltips'].push(this.data[i][j][3]);
  314. }
  315. }
  316. }
  317. // This resets the chart drawing state
  318. co.beginPath();
  319. this.DrawBackground();
  320. this.DrawRscatter();
  321. this.DrawLabels();
  322. /**
  323. * Setup the context menu if required
  324. */
  325. if (prop['chart.contextmenu']) {
  326. RG.ShowContext(this);
  327. }
  328. // Draw the title if any has been set
  329. if (prop['chart.title']) {
  330. RG.DrawTitle(this,
  331. prop['chart.title'],
  332. this.centery - this.radius - 10,
  333. this.centerx,
  334. prop['chart.title.size'] ? prop['chart.title.size'] : prop['chart.text.size'] + 2);
  335. }
  336. /**
  337. * This function enables resizing
  338. */
  339. if (prop['chart.resizable']) {
  340. RG.AllowResizing(this);
  341. }
  342. /**
  343. * This installs the event listeners
  344. */
  345. RG.InstallEventListeners(this);
  346. /**
  347. * Fire the onfirstdraw event
  348. */
  349. if (this.firstDraw) {
  350. RG.fireCustomEvent(this, 'onfirstdraw');
  351. this.firstDrawFunc();
  352. this.firstDraw = false;
  353. }
  354. /**
  355. * Fire the RGraph ondraw event
  356. */
  357. RG.FireCustomEvent(this, 'ondraw');
  358. return this;
  359. };
  360. /**
  361. * This method draws the rscatter charts background
  362. */
  363. this.drawBackground =
  364. this.DrawBackground = function ()
  365. {
  366. co.lineWidth = 1;
  367. // Draw the background grey circles
  368. co.strokeStyle = '#ccc'; // TODO Use a property here - eg chart.background.circles.color
  369. // Radius must be greater than 0 for Opera to work
  370. var r = this.radius / 10;
  371. for (var i=0,len=this.radius; i<=len; i+=r) {
  372. //co.moveTo(this.centerx + i, this.centery);
  373. // Radius must be greater than 0 for Opera to work
  374. co.arc(this.centerx, this.centery, i, 0, RG.TWOPI, 0);
  375. }
  376. co.stroke();
  377. // Draw the background lines that go from the center outwards
  378. co.beginPath();
  379. for (var i=15; i<360; i+=15) {
  380. // Radius must be greater than 0 for Opera to work
  381. co.arc(this.centerx, this.centery, this.radius, i / (180 / RG.PI), (i + 0.01) / (180 / RG.PI), 0);
  382. co.lineTo(this.centerx, this.centery);
  383. }
  384. co.stroke();
  385. co.beginPath();
  386. co.strokeStyle = 'black';
  387. // Draw the X axis
  388. co.moveTo(this.centerx - this.radius, Math.round(this.centery));
  389. co.lineTo(this.centerx + this.radius, Math.round(this.centery));
  390. // Draw the X ends
  391. co.moveTo(Math.round(this.centerx - this.radius), this.centery - 5);
  392. co.lineTo(Math.round(this.centerx - this.radius), this.centery + 5);
  393. co.moveTo(Math.round(this.centerx + this.radius), this.centery - 5);
  394. co.lineTo(Math.round(this.centerx + this.radius), this.centery + 5);
  395. // Draw the X check marks
  396. for (var i=(this.centerx - this.radius); i<(this.centerx + this.radius); i+=(this.radius / 10)) {
  397. co.moveTo(Math.round(i), this.centery - 3);
  398. co.lineTo(Math.round(i), this.centery + 3);
  399. }
  400. // Draw the Y check marks
  401. for (var i=(this.centery - this.radius); i<(this.centery + this.radius); i+=(this.radius / 10)) {
  402. co.moveTo(this.centerx - 3, Math.round(i));
  403. co.lineTo(this.centerx + 3, Math.round(i));
  404. }
  405. // Draw the Y axis
  406. co.moveTo(Math.round(this.centerx), this.centery - this.radius);
  407. co.lineTo(Math.round(this.centerx), this.centery + this.radius);
  408. // Draw the Y ends
  409. co.moveTo(this.centerx - 5, Math.round(this.centery - this.radius));
  410. co.lineTo(this.centerx + 5, Math.round(this.centery - this.radius));
  411. co.moveTo(this.centerx - 5, Math.round(this.centery + this.radius));
  412. co.lineTo(this.centerx + 5, Math.round(this.centery + this.radius));
  413. // Stroke it
  414. co.closePath();
  415. co.stroke();
  416. };
  417. /**
  418. * This method draws a set of data on the graph
  419. */
  420. this.drawRscatter =
  421. this.DrawRscatter = function ()
  422. {
  423. for (var dataset=0; dataset<this.data.length; dataset+=1) {
  424. var data = this.data[dataset];
  425. this.coords2[dataset] = [];
  426. for (var i=0; i<data.length; ++i) {
  427. var d1 = data[i][0];
  428. var d2 = data[i][1];
  429. var a = d1 / (180 / RG.PI); // RADIANS
  430. var r = ( (d2 - prop['chart.ymin']) / (this.scale2.max - this.scale2.min) ) * this.radius;
  431. var x = Math.sin(a) * r;
  432. var y = Math.cos(a) * r;
  433. var color = data[i][2] ? data[i][2] : prop['chart.colors.default'];
  434. var tooltip = data[i][3] ? data[i][3] : null;
  435. if (tooltip && String(tooltip).length) {
  436. this.hasTooltips = true;
  437. }
  438. /**
  439. * Account for the correct quadrant
  440. */
  441. x = x + this.centerx;
  442. y = this.centery - y;
  443. this.DrawTick(x, y, color);
  444. // Populate the coords array with the coordinates and the tooltip
  445. this.coords.push([x, y, color, tooltip]);
  446. this.coords2[dataset].push([x, y, color, tooltip]);
  447. }
  448. }
  449. };
  450. /**
  451. * Unsuprisingly, draws the labels
  452. */
  453. this.drawLabels =
  454. this.DrawLabels = function ()
  455. {
  456. co.lineWidth = 1;
  457. // Default the color to black
  458. co.fillStyle = 'black';
  459. co.strokeStyle = 'black';
  460. var key = prop['chart.key'];
  461. var r = this.radius;
  462. var color = prop['chart.text.color'];
  463. var font = prop['chart.text.font'];
  464. var size = prop['chart.text.size'];
  465. var axes = prop['chart.labels.axes'].toLowerCase();
  466. var units_pre = prop['chart.units.pre'];
  467. var units_post = prop['chart.units.post'];
  468. var decimals = prop['chart.scale.decimals'];
  469. var centerx = this.centerx;
  470. var centery = this.centery;
  471. co.fillStyle = prop['chart.text.color'];
  472. // Draw any labels
  473. if (typeof prop['chart.labels'] == 'object' && prop['chart.labels']) {
  474. this.DrawCircularLabels(co, prop['chart.labels'], font , size, r);
  475. }
  476. var color = 'rgba(255,255,255,0.8)';
  477. // Draw the axis labels
  478. for (var i=0,len=this.scale2.labels.length; i<len; ++i) {
  479. if (axes.indexOf('n') > -1) RG.Text2(this, {'tag': 'scale','font':font,'size':size,'x':centerx,'y':centery - (r * ((i+1) / len)),'text':this.scale2.labels[i],'valign':'center','halign':'center','bounding':true,'boundingFill':color});
  480. if (axes.indexOf('s') > -1) RG.Text2(this, {'tag': 'scale','font':font,'size':size,'x':centerx,'y':centery + (r * ((i+1) / len)),'text':this.scale2.labels[i],'valign':'center','halign':'center','bounding':true,'boundingFill':color});
  481. if (axes.indexOf('e') > -1) RG.Text2(this, {'tag': 'scale','font':font,'size':size,'x':centerx + (r * ((i+1) / len)),'y':centery,'text':this.scale2.labels[i],'valign':'center','halign':'center','bounding':true,'boundingFill':color});
  482. if (axes.indexOf('w') > -1) RG.Text2(this, {'tag': 'scale','font':font,'size':size,'x':centerx - (r * ((i+1) / len)),'y':centery,'text':this.scale2.labels[i],'valign':'center','halign':'center','bounding':true,'boundingFill':color});
  483. }
  484. // Draw the center minimum value (but only if there's at least one axes labels stipulated)
  485. if (prop['chart.labels.axes'].length > 0) {
  486. RG.Text2(this, {'font':font,
  487. 'size':size,
  488. 'x':centerx,
  489. 'y':centery,
  490. 'text':RG.number_format(this, Number(this.scale2.min).toFixed(this.scale2.decimals), this.scale2.units_pre, this.scale2.units_post),
  491. 'valign':'center',
  492. 'halign':'center',
  493. 'bounding':true,
  494. 'boundingFill':color,
  495. 'tag': 'scale'
  496. });
  497. }
  498. /**
  499. * Draw the key
  500. */
  501. if (key && key.length) {
  502. RG.DrawKey(this, key, prop['chart.colors']);
  503. }
  504. };
  505. /**
  506. * Draws the circular labels that go around the charts
  507. *
  508. * @param labels array The labels that go around the chart
  509. */
  510. this.drawCircularLabels =
  511. this.DrawCircularLabels = function (context, labels, font_face, font_size, r)
  512. {
  513. var position = prop['chart.labels.position'];
  514. var r = r + 10;
  515. for (var i=0; i<labels.length; ++i) {
  516. var a = (360 / labels.length) * (i + 1) - (360 / (labels.length * 2));
  517. var a = a - 90 + (prop['chart.labels.position'] == 'edge' ? ((360 / labels.length) / 2) : 0);
  518. var x = Math.cos(a / (180/RG.PI) ) * (r + 10);
  519. var y = Math.sin(a / (180/RG.PI)) * (r + 10);
  520. RG.Text2(this, {'font':font_face,
  521. 'size':font_size,
  522. 'x':this.centerx + x,
  523. 'y':this.centery + y,
  524. 'text':String(labels[i]),
  525. 'valign':'center',
  526. 'halign':'center',
  527. 'tag': 'labels'
  528. });
  529. }
  530. };
  531. /**
  532. * Draws a single tickmark
  533. */
  534. this.drawTick =
  535. this.DrawTick = function (x, y, color)
  536. {
  537. var tickmarks = prop['chart.tickmarks'];
  538. var ticksize = prop['chart.ticksize'];
  539. co.strokeStyle = color;
  540. co.fillStyle = color;
  541. // Cross
  542. if (tickmarks == 'cross') {
  543. co.beginPath();
  544. co.moveTo(x + ticksize, y + ticksize);
  545. co.lineTo(x - ticksize, y - ticksize);
  546. co.stroke();
  547. co.beginPath();
  548. co.moveTo(x - ticksize, y + ticksize);
  549. co.lineTo(x + ticksize, y - ticksize);
  550. co.stroke();
  551. // Circle
  552. } else if (tickmarks == 'circle') {
  553. co.beginPath();
  554. co.arc(x, y, ticksize, 0, 6.2830, false);
  555. co.fill();
  556. // Square
  557. } else if (tickmarks == 'square') {
  558. co.beginPath();
  559. co.fillRect(x - ticksize, y - ticksize, 2 * ticksize, 2 * ticksize);
  560. co.fill();
  561. // Diamond shape tickmarks
  562. } else if (tickmarks == 'diamond') {
  563. co.beginPath();
  564. co.moveTo(x, y - ticksize);
  565. co.lineTo(x + ticksize, y);
  566. co.lineTo(x, y + ticksize);
  567. co.lineTo(x - ticksize, y);
  568. co.closePath();
  569. co.fill();
  570. // Plus style tickmarks
  571. } else if (tickmarks == 'plus') {
  572. co.lineWidth = 1;
  573. co.beginPath();
  574. co.moveTo(x, y - ticksize);
  575. co.lineTo(x, y + ticksize);
  576. co.moveTo(x - ticksize, y);
  577. co.lineTo(x + ticksize, y);
  578. co.stroke();
  579. }
  580. };
  581. /**
  582. * This function makes it much easier to get the (if any) point that is currently being hovered over.
  583. *
  584. * @param object e The event object
  585. */
  586. this.getShape =
  587. this.getPoint = function (e)
  588. {
  589. var mouseXY = RG.getMouseXY(e);
  590. var mouseX = mouseXY[0];
  591. var mouseY = mouseXY[1];
  592. var overHotspot = false;
  593. var offset = prop['chart.tooltips.hotspot']; // This is how far the hotspot extends
  594. for (var i=0,len=this.coords.length; i<len; ++i) {
  595. var x = this.coords[i][0];
  596. var y = this.coords[i][1];
  597. var tooltip = this.coords[i][3];
  598. if (
  599. mouseX < (x + offset) &&
  600. mouseX > (x - offset) &&
  601. mouseY < (y + offset) &&
  602. mouseY > (y - offset)
  603. ) {
  604. var tooltip = RG.parseTooltipText(prop['chart.tooltips'], i);
  605. return {0:this,1:x,2:y,3:i,'object':this, 'x':x, 'y':y, 'index':i, 'tooltip': tooltip};
  606. }
  607. }
  608. };
  609. /**
  610. * This function facilitates the installation of tooltip event listeners if
  611. * tooltips are defined.
  612. */
  613. this.allowTooltips =
  614. this.AllowTooltips = function ()
  615. {
  616. // Preload any tooltip images that are used in the tooltips
  617. RG.PreLoadTooltipImages(this);
  618. /**
  619. * This installs the window mousedown event listener that lears any
  620. * highlight that may be visible.
  621. */
  622. RG.InstallWindowMousedownTooltipListener(this);
  623. /**
  624. * This installs the canvas mousemove event listener. This function
  625. * controls the pointer shape.
  626. */
  627. RG.InstallCanvasMousemoveTooltipListener(this);
  628. /**
  629. * This installs the canvas mouseup event listener. This is the
  630. * function that actually shows the appropriate tooltip (if any).
  631. */
  632. RG.InstallCanvasMouseupTooltipListener(this);
  633. };
  634. /**
  635. * Each object type has its own Highlight() function which highlights the appropriate shape
  636. *
  637. * @param object shape The shape to highlight
  638. */
  639. this.highlight =
  640. this.Highlight = function (shape)
  641. {
  642. // Add the new highlight
  643. RG.Highlight.Point(this, shape);
  644. };
  645. /**
  646. * The getObjectByXY() worker method. Don't call this call:
  647. *
  648. * RGraph.ObjectRegistry.getObjectByXY(e)
  649. *
  650. * @param object e The event object
  651. */
  652. this.getObjectByXY = function (e)
  653. {
  654. var mouseXY = RG.getMouseXY(e);
  655. var mouseX = mouseXY[0];
  656. var mouseY = mouseXY[1];
  657. var centerx = this.centerx;
  658. var centery = this.centery;
  659. var radius = this.radius;
  660. if (
  661. mouseX > (centerx - radius)
  662. && mouseX < (centerx + radius)
  663. && mouseY > (centery - radius)
  664. && mouseY < (centery + radius)
  665. ) {
  666. return this;
  667. }
  668. };
  669. /**
  670. * This function positions a tooltip when it is displayed
  671. *
  672. * @param obj object The chart object
  673. * @param int x The X coordinate specified for the tooltip
  674. * @param int y The Y coordinate specified for the tooltip
  675. * @param objec tooltip The tooltips DIV element
  676. */
  677. this.positionTooltip = function (obj, x, y, tooltip, idx)
  678. {
  679. var coordX = obj.coords[tooltip.__index__][0];
  680. var coordY = obj.coords[tooltip.__index__][1];
  681. var canvasXY = RG.getCanvasXY(obj.canvas);
  682. var gutterLeft = obj.gutterLeft;
  683. var gutterTop = obj.gutterTop;
  684. var width = tooltip.offsetWidth;
  685. // Set the top position
  686. tooltip.style.left = 0;
  687. tooltip.style.top = parseInt(tooltip.style.top) - 7 + 'px';
  688. // By default any overflow is hidden
  689. tooltip.style.overflow = '';
  690. // The arrow
  691. var img = new Image();
  692. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
  693. img.style.position = 'absolute';
  694. img.id = '__rgraph_tooltip_pointer__';
  695. img.style.top = (tooltip.offsetHeight - 2) + 'px';
  696. tooltip.appendChild(img);
  697. // Reposition the tooltip if at the edges:
  698. // LEFT edge
  699. if ((canvasXY[0] + coordX - (width / 2)) < 10) {
  700. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + 'px';
  701. img.style.left = ((width * 0.1) - 8.5) + 'px';
  702. // RIGHT edge
  703. } else if ((canvasXY[0] + coordX + (width / 2)) > doc.body.offsetWidth) {
  704. tooltip.style.left = canvasXY[0] + coordX - (width * 0.9) + 'px';
  705. img.style.left = ((width * 0.9) - 8.5) + 'px';
  706. // Default positioning - CENTERED
  707. } else {
  708. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.5)) + 'px';
  709. img.style.left = ((width * 0.5) - 8.5) + 'px';
  710. }
  711. };
  712. /**
  713. * This function returns the radius (ie the distance from the center) for a particular
  714. * value.
  715. *
  716. * @param number value The value you want the radius for
  717. */
  718. this.getRadius = function (value)
  719. {
  720. var max = this.max;
  721. if (value < 0 || value > max) {
  722. return null;
  723. }
  724. var r = (value / max) * this.radius;
  725. return r;
  726. };
  727. /**
  728. * This allows for easy specification of gradients
  729. */
  730. this.parseColors = function ()
  731. {
  732. // Save the original colors so that they can be restored when the canvas is reset
  733. if (this.original_colors.length === 0) {
  734. this.original_colors['data'] = RG.array_clone(this.data);
  735. this.original_colors['chart.highlight.stroke'] = RG.array_clone(prop['chart.highlight.stroke']);
  736. this.original_colors['chart.highlight.fill'] = RG.array_clone(prop['chart.highlight.fill']);
  737. this.original_colors['chart.colors.default'] = RG.array_clone(prop['chart.colors.default']);
  738. }
  739. // Go through the data
  740. for (var i=0; i<this.data.length; i+=1) {
  741. for (var j=0,len=this.data[i].length; j<len; j+=1) {
  742. this.data[i][j][2] = this.parseSingleColorForGradient(this.data[i][j][2]);
  743. }
  744. }
  745. prop['chart.highlight.stroke'] = this.parseSingleColorForGradient(prop['chart.highlight.stroke']);
  746. prop['chart.highlight.fill'] = this.parseSingleColorForGradient(prop['chart.highlight.fill']);
  747. prop['chart.colors.default'] = this.parseSingleColorForGradient(prop['chart.colors.default']);
  748. };
  749. /**
  750. * This parses a single color value
  751. */
  752. this.parseSingleColorForGradient = function (color)
  753. {
  754. if (!color || typeof color != 'string') {
  755. return color;
  756. }
  757. if (color.match(/^gradient\((.*)\)$/i)) {
  758. var parts = RegExp.$1.split(':');
  759. // Create the gradient
  760. var grad = co.createRadialGradient(this.centerx, this.centery, 0, this.centerx, this.centery, this.radius);
  761. var diff = 1 / (parts.length - 1);
  762. grad.addColorStop(0, RG.trim(parts[0]));
  763. for (var j=1; j<parts.length; ++j) {
  764. grad.addColorStop(j * diff, RG.trim(parts[j]));
  765. }
  766. }
  767. return grad ? grad : color;
  768. };
  769. /**
  770. * This function handles highlighting an entire data-series for the interactive
  771. * key
  772. *
  773. * @param int index The index of the data series to be highlighted
  774. */
  775. this.interactiveKeyHighlight = function (index)
  776. {
  777. if (this.coords2 && this.coords2[index] && this.coords2[index].length) {
  778. this.coords2[index].forEach(function (value, idx, arr)
  779. {
  780. co.beginPath();
  781. co.fillStyle = prop['chart.key.interactive.highlight.chart.fill'];
  782. co.arc(value[0], value[1], prop['chart.ticksize'] + 2, 0, RG.TWOPI, false);
  783. co.fill();
  784. });
  785. }
  786. };
  787. /**
  788. * Using a function to add events makes it easier to facilitate method chaining
  789. *
  790. * @param string type The type of even to add
  791. * @param function func
  792. */
  793. this.on = function (type, func)
  794. {
  795. if (type.substr(0,2) !== 'on') {
  796. type = 'on' + type;
  797. }
  798. this[type] = func;
  799. return this;
  800. };
  801. /**
  802. * This helps the Gantt reset colors when the reset function is called.
  803. * It handles going through the data and resetting the colors.
  804. */
  805. this.resetColorsToOriginalValues = function ()
  806. {
  807. /**
  808. * Copy the original colors over for single-event-per-line data
  809. */
  810. for (var i=0,len=this.original_colors['data'].length; i<len; ++i) {
  811. for (var j=0,len2=this.original_colors['data'][i].length; j<len2;++j) {
  812. this.data[i][j][2] = RG.array_clone(this.original_colors['data'][i][j][2]);
  813. }
  814. }
  815. };
  816. /**
  817. * This function runs once only
  818. * (put at the end of the file (before any effects))
  819. */
  820. this.firstDrawFunc = function ()
  821. {
  822. };
  823. /**
  824. * Register the object
  825. */
  826. RG.Register(this);
  827. };