RGraph.scatter.js 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508
  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.txt |
  12. * o------------------------------------------------------------------------------o
  13. */
  14. if (typeof(RGraph) == 'undefined') RGraph = {};
  15. /**
  16. * The scatter graph constructor
  17. *
  18. * @param object canvas The cxanvas object
  19. * @param array data The chart data
  20. */
  21. RGraph.Scatter = function (id, data)
  22. {
  23. // Get the canvas and context objects
  24. this.id = id;
  25. this.canvas = document.getElementById(typeof id === 'object' ? id.id : id);
  26. this.canvas.__object__ = this;
  27. this.context = this.canvas.getContext ? this.canvas.getContext("2d") : null;
  28. this.max = 0;
  29. this.coords = [];
  30. this.data = [];
  31. this.type = 'scatter';
  32. this.isRGraph = true;
  33. this.uid = RGraph.CreateUID();
  34. this.canvas.uid = this.canvas.uid ? this.canvas.uid : RGraph.CreateUID();
  35. this.colorsParsed = false;
  36. this.coordsText = [];
  37. /**
  38. * Compatibility with older browsers
  39. */
  40. RGraph.OldBrowserCompat(this.context);
  41. // Various config properties
  42. this.properties = {
  43. 'chart.background.barcolor1': 'rgba(0,0,0,0)',
  44. 'chart.background.barcolor2': 'rgba(0,0,0,0)',
  45. 'chart.background.grid': true,
  46. 'chart.background.grid.width': 1,
  47. 'chart.background.grid.color': '#ddd',
  48. 'chart.background.grid.hsize': 20,
  49. 'chart.background.grid.vsize': 20,
  50. 'chart.background.hbars': null,
  51. 'chart.background.vbars': null,
  52. 'chart.background.grid.vlines': true,
  53. 'chart.background.grid.hlines': true,
  54. 'chart.background.grid.border': true,
  55. 'chart.background.grid.autofit':true,
  56. 'chart.background.grid.autofit.numhlines': 5,
  57. 'chart.background.grid.autofit.numvlines': 20,
  58. 'chart.background.image': null,
  59. 'chart.background.image.stretch': true,
  60. 'chart.background.image.x': null,
  61. 'chart.background.image.y': null,
  62. 'chart.background.image.w': null,
  63. 'chart.background.image.h': null,
  64. 'chart.background.image.align': null,
  65. 'chart.text.size': 10,
  66. 'chart.text.angle': 0,
  67. 'chart.text.color': 'black',
  68. 'chart.text.font': 'Arial',
  69. 'chart.tooltips': [], // Default must be an empty array
  70. 'chart.tooltips.effect': 'fade',
  71. 'chart.tooltips.event': 'onmousemove',
  72. 'chart.tooltips.hotspot': 3,
  73. 'chart.tooltips.css.class': 'RGraph_tooltip',
  74. 'chart.tooltips.highlight': true,
  75. 'chart.tooltips.coords.page': false,
  76. 'chart.units.pre': '',
  77. 'chart.units.post': '',
  78. 'chart.numyticks': 10,
  79. 'chart.tickmarks': 'cross',
  80. 'chart.ticksize': 5,
  81. 'chart.numxticks': true,
  82. 'chart.xaxis': true,
  83. 'chart.gutter.left': 25,
  84. 'chart.gutter.right': 25,
  85. 'chart.gutter.top': 25,
  86. 'chart.gutter.bottom': 25,
  87. 'chart.xmin': 0,
  88. 'chart.xmax': 0,
  89. 'chart.ymax': null,
  90. 'chart.ymin': 0,
  91. 'chart.scale.decimals': null,
  92. 'chart.scale.point': '.',
  93. 'chart.scale.thousand': ',',
  94. 'chart.title': '',
  95. 'chart.title.background': null,
  96. 'chart.title.hpos': null,
  97. 'chart.title.vpos': null,
  98. 'chart.title.bold': true,
  99. 'chart.title.font': null,
  100. 'chart.title.xaxis': '',
  101. 'chart.title.xaxis.bold': true,
  102. 'chart.title.xaxis.size': null,
  103. 'chart.title.xaxis.font': null,
  104. 'chart.title.yaxis': '',
  105. 'chart.title.yaxis.bold': true,
  106. 'chart.title.yaxis.size': null,
  107. 'chart.title.yaxis.font': null,
  108. 'chart.title.yaxis.color': null,
  109. 'chart.title.xaxis.pos': null,
  110. 'chart.title.yaxis.pos': null,
  111. 'chart.title.yaxis.x': null,
  112. 'chart.title.yaxis.y': null,
  113. 'chart.title.xaxis.x': null,
  114. 'chart.title.xaxis.y': null,
  115. 'chart.title.x': null,
  116. 'chart.title.y': null,
  117. 'chart.title.halign': null,
  118. 'chart.title.valign': null,
  119. 'chart.labels': [],
  120. 'chart.labels.ingraph': null,
  121. 'chart.labels.above': false,
  122. 'chart.labels.above.size': 8,
  123. 'chart.labels.above.decimals': 0,
  124. 'chart.ylabels': true,
  125. 'chart.ylabels.count': 5,
  126. 'chart.ylabels.invert': false,
  127. 'chart.ylabels.specific': null,
  128. 'chart.ylabels.inside': false,
  129. 'chart.contextmenu': null,
  130. 'chart.defaultcolor': 'black',
  131. 'chart.xaxispos': 'bottom',
  132. 'chart.yaxispos': 'left',
  133. 'chart.crosshairs': false,
  134. 'chart.crosshairs.color': '#333',
  135. 'chart.crosshairs.linewidth': 1,
  136. 'chart.crosshairs.coords': false,
  137. 'chart.crosshairs.coords.fixed':true,
  138. 'chart.crosshairs.coords.fadeout':false,
  139. 'chart.crosshairs.coords.labels.x': 'X',
  140. 'chart.crosshairs.coords.labels.y': 'Y',
  141. 'chart.crosshairs.hline': true,
  142. 'chart.crosshairs.vline': true,
  143. 'chart.annotatable': false,
  144. 'chart.annotate.color': 'black',
  145. 'chart.line': false,
  146. 'chart.line.linewidth': 1,
  147. 'chart.line.colors': ['green', 'red'],
  148. 'chart.line.shadow.color': 'rgba(0,0,0,0)',
  149. 'chart.line.shadow.blur': 2,
  150. 'chart.line.shadow.offsetx': 3,
  151. 'chart.line.shadow.offsety': 3,
  152. 'chart.line.stepped': false,
  153. 'chart.line.visible': true,
  154. 'chart.noaxes': false,
  155. 'chart.noyaxis': false,
  156. 'chart.key': null,
  157. 'chart.key.background': 'white',
  158. 'chart.key.position': 'graph',
  159. 'chart.key.halign': 'right',
  160. 'chart.key.shadow': false,
  161. 'chart.key.shadow.color': '#666',
  162. 'chart.key.shadow.blur': 3,
  163. 'chart.key.shadow.offsetx': 2,
  164. 'chart.key.shadow.offsety': 2,
  165. 'chart.key.position.gutter.boxed': false,
  166. 'chart.key.position.x': null,
  167. 'chart.key.position.y': null,
  168. 'chart.key.interactive': false,
  169. 'chart.key.interactive.highlight.chart.fill': 'rgba(255,0,0,0.9)',
  170. 'chart.key.interactive.highlight.label': 'rgba(255,0,0,0.2)',
  171. 'chart.key.color.shape': 'square',
  172. 'chart.key.rounded': true,
  173. 'chart.key.linewidth': 1,
  174. 'chart.key.colors': null,
  175. 'chart.key.text.color': 'black',
  176. 'chart.axis.color': 'black',
  177. 'chart.zoom.factor': 1.5,
  178. 'chart.zoom.fade.in': true,
  179. 'chart.zoom.fade.out': true,
  180. 'chart.zoom.hdir': 'right',
  181. 'chart.zoom.vdir': 'down',
  182. 'chart.zoom.frames': 25,
  183. 'chart.zoom.delay': 16.666,
  184. 'chart.zoom.shadow': true,
  185. 'chart.zoom.background': true,
  186. 'chart.zoom.action': 'zoom',
  187. 'chart.boxplot.width': 1,
  188. 'chart.boxplot.capped': true,
  189. 'chart.resizable': false,
  190. 'chart.resize.handle.background': null,
  191. 'chart.xmin': 0,
  192. 'chart.labels.specific.align': 'left',
  193. 'chart.xscale': false,
  194. 'chart.xscale.units.pre': '',
  195. 'chart.xscale.units.post': '',
  196. 'chart.xscale.numlabels': 10,
  197. 'chart.xscale.formatter': null,
  198. 'chart.xscale.decimals': 0,
  199. 'chart.xscale.thousand': ',',
  200. 'chart.xscale.point': '.',
  201. 'chart.noendxtick': false,
  202. 'chart.noendytick': true,
  203. 'chart.events.mousemove': null,
  204. 'chart.events.click': null,
  205. 'chart.highlight.stroke': 'rgba(0,0,0,0)',
  206. 'chart.highlight.fill': 'rgba(255,255,255,0.7)'
  207. }
  208. // Handle multiple datasets being given as one argument
  209. if (arguments[1][0] && arguments[1][0][0] && typeof(arguments[1][0][0]) == 'object') {
  210. // Store the data set(s)
  211. for (var i=0; i<arguments[1].length; ++i) {
  212. this.data[i] = arguments[1][i];
  213. }
  214. // Handle multiple data sets being supplied as seperate arguments
  215. } else {
  216. // Store the data set(s)
  217. for (var i=1; i<arguments.length; ++i) {
  218. this.data[i - 1] = arguments[i];
  219. }
  220. }
  221. /**
  222. * This allows the data points to be given as dates as well as numbers. Formats supported by Date.parse() are accepted.
  223. */
  224. for (var i=0; i<this.data.length; ++i) {
  225. for (var j=0; j<this.data[i].length; ++j) {
  226. if (typeof(this.data[i][j][0]) == 'string') {
  227. this.data[i][j][0] = Date.parse(this.data[i][j][0]);
  228. }
  229. }
  230. }
  231. /**
  232. * Now make the data_arr array - all the data as one big array
  233. */
  234. this.data_arr = [];
  235. for (var i=0; i<this.data.length; ++i) {
  236. for (var j=0; j<this.data[i].length; ++j) {
  237. this.data_arr.push(this.data[i][j]);
  238. }
  239. }
  240. // Create the $ objects so that they can be used
  241. for (var i=0; i<this.data_arr.length; ++i) {
  242. this['$' + i] = {}
  243. }
  244. // Check for support
  245. if (!this.canvas) {
  246. alert('[SCATTER] No canvas support');
  247. return;
  248. }
  249. /**
  250. * Translate half a pixel for antialiasing purposes - but only if it hasn't beeen
  251. * done already
  252. */
  253. if (!this.canvas.__rgraph_aa_translated__) {
  254. this.context.translate(0.5,0.5);
  255. this.canvas.__rgraph_aa_translated__ = true;
  256. }
  257. ///////////////////////////////// SHORT PROPERTIES /////////////////////////////////
  258. var RG = RGraph;
  259. var ca = this.canvas;
  260. var co = ca.getContext('2d');
  261. var prop = this.properties;
  262. //////////////////////////////////// METHODS ///////////////////////////////////////
  263. /**
  264. * A simple setter
  265. *
  266. * @param string name The name of the property to set
  267. * @param string value The value of the property
  268. */
  269. this.Set = function (name, value)
  270. {
  271. /**
  272. * This should be done first - prepend the propertyy name with "chart." if necessary
  273. */
  274. if (name.substr(0,6) != 'chart.') {
  275. name = 'chart.' + name;
  276. }
  277. /**
  278. * BC for chart.xticks
  279. */
  280. if (name == 'chart.xticks') {
  281. name == 'chart.numxticks';
  282. }
  283. /**
  284. * This is here because the key expects a name of "chart.colors"
  285. */
  286. if (name == 'chart.line.colors') {
  287. prop['chart.colors'] = value;
  288. }
  289. /**
  290. * Allow compatibility with older property names
  291. */
  292. if (name == 'chart.tooltip.hotspot') {
  293. name = 'chart.tooltips.hotspot';
  294. }
  295. /**
  296. * chart.yaxispos should be left or right
  297. */
  298. if (name == 'chart.yaxispos' && value != 'left' && value != 'right') {
  299. alert("[SCATTER] chart.yaxispos should be left or right. You've set it to: '" + value + "' Changing it to left");
  300. value = 'left';
  301. }
  302. /**
  303. * Check for xaxispos
  304. */
  305. if (name == 'chart.xaxispos' ) {
  306. if (value != 'bottom' && value != 'center') {
  307. alert('[SCATTER] (' + this.id + ') chart.xaxispos should be center or bottom. Tried to set it to: ' + value + ' Changing it to center');
  308. value = 'center';
  309. }
  310. }
  311. prop[name.toLowerCase()] = value;
  312. return this;
  313. }
  314. /**
  315. * A simple getter
  316. *
  317. * @param string name The name of the property to set
  318. */
  319. this.Get = function (name)
  320. {
  321. /**
  322. * This should be done first - prepend the property name with "chart." if necessary
  323. */
  324. if (name.substr(0,6) != 'chart.') {
  325. name = 'chart.' + name;
  326. }
  327. return prop[name];
  328. }
  329. /**
  330. * The function you call to draw the line chart
  331. */
  332. this.Draw = function ()
  333. {
  334. // MUST be the first thing done!
  335. if (typeof(prop['chart.background.image']) == 'string') {
  336. RG.DrawBackgroundImage(this);
  337. }
  338. /**
  339. * Fire the onbeforedraw event
  340. */
  341. RG.FireCustomEvent(this, 'onbeforedraw');
  342. /**
  343. * Parse the colors. This allows for simple gradient syntax
  344. */
  345. if (!this.colorsParsed) {
  346. this.parseColors();
  347. // Don't want to do this again
  348. this.colorsParsed = true;
  349. }
  350. /**
  351. * This is new in May 2011 and facilitates indiviual gutter settings,
  352. * eg chart.gutter.left
  353. */
  354. this.gutterLeft = prop['chart.gutter.left'];
  355. this.gutterRight = prop['chart.gutter.right'];
  356. this.gutterTop = prop['chart.gutter.top'];
  357. this.gutterBottom = prop['chart.gutter.bottom'];
  358. // Go through all the data points and see if a tooltip has been given
  359. this.hasTooltips = false;
  360. var overHotspot = false;
  361. // Reset the coords array
  362. this.coords = [];
  363. /**
  364. * This facilitates the xmax, xmin and X values being dates
  365. */
  366. if (typeof(prop['chart.xmin']) == 'string') prop['chart.xmin'] = Date.parse(prop['chart.xmin']);
  367. if (typeof(prop['chart.xmax']) == 'string') prop['chart.xmax'] = Date.parse(prop['chart.xmax']);
  368. /**
  369. * Look for tooltips and populate chart.tooltips
  370. *
  371. * NB 26/01/2011 Updated so that chart.tooltips is ALWAYS populated
  372. */
  373. if (!ISOLD) {
  374. this.Set('chart.tooltips', []);
  375. for (var i=0; i<this.data.length; ++i) {
  376. for (var j =0;j<this.data[i].length; ++j) {
  377. if (this.data[i][j] && this.data[i][j][3]) {
  378. prop['chart.tooltips'].push(this.data[i][j][3]);
  379. this.hasTooltips = true;
  380. } else {
  381. prop['chart.tooltips'].push(null);
  382. }
  383. }
  384. }
  385. }
  386. // Reset the maximum value
  387. this.max = 0;
  388. // Work out the maximum Y value
  389. //if (prop['chart.ymax'] && prop['chart.ymax'] > 0) {
  390. if (typeof(prop['chart.ymax']) == 'number') {
  391. this.max = prop['chart.ymax'];
  392. this.min = prop['chart.ymin'] ? prop['chart.ymin'] : 0;
  393. this.scale2 = RG.getScale2(this, {
  394. 'max':this.max,
  395. 'min':this.min,
  396. 'strict':true,
  397. 'scale.thousand':prop['chart.scale.thousand'],
  398. 'scale.point':prop['chart.scale.point'],
  399. 'scale.decimals':prop['chart.scale.decimals'],
  400. 'ylabels.count':prop['chart.ylabels.count'],
  401. 'scale.round':prop['chart.scale.round'],
  402. 'units.pre': prop['chart.units.pre'],
  403. 'units.post': prop['chart.units.post']
  404. });
  405. this.max = this.scale2.max;
  406. this.min = this.scale2.min;
  407. var decimals = prop['chart.scale.decimals'];
  408. } else {
  409. var i = 0;
  410. var j = 0;
  411. for (i=0; i<this.data.length; ++i) {
  412. for (j=0; j<this.data[i].length; ++j) {
  413. if (this.data[i][j][1] != null) {
  414. this.max = Math.max(this.max, typeof(this.data[i][j][1]) == 'object' ? RG.array_max(this.data[i][j][1]) : Math.abs(this.data[i][j][1]));
  415. }
  416. }
  417. }
  418. this.min = prop['chart.ymin'] ? prop['chart.ymin'] : 0;
  419. this.scale2 = RG.getScale2(this, {
  420. 'max':this.max,
  421. 'min':this.min,
  422. 'scale.thousand':prop['chart.scale.thousand'],
  423. 'scale.point':prop['chart.scale.point'],
  424. 'scale.decimals':prop['chart.scale.decimals'],
  425. 'ylabels.count':prop['chart.ylabels.count'],
  426. 'scale.round':prop['chart.scale.round'],
  427. 'units.pre': prop['chart.units.pre'],
  428. 'units.post': prop['chart.units.post']
  429. });
  430. this.max = this.scale2.max;
  431. this.min = this.scale2.min;
  432. }
  433. this.grapharea = ca.height - this.gutterTop - this.gutterBottom;
  434. // Progressively Draw the chart
  435. RG.background.Draw(this);
  436. /**
  437. * Draw any horizontal bars that have been specified
  438. */
  439. if (prop['chart.background.hbars'] && prop['chart.background.hbars'].length) {
  440. RG.DrawBars(this);
  441. }
  442. /**
  443. * Draw any vertical bars that have been specified
  444. */
  445. if (prop['chart.background.vbars'] && prop['chart.background.vbars'].length) {
  446. this.DrawVBars();
  447. }
  448. if (!prop['chart.noaxes']) {
  449. this.DrawAxes();
  450. }
  451. this.DrawLabels();
  452. i = 0;
  453. for(i=0; i<this.data.length; ++i) {
  454. this.DrawMarks(i);
  455. // Set the shadow
  456. co.shadowColor = prop['chart.line.shadow.color'];
  457. co.shadowOffsetX = prop['chart.line.shadow.offsetx'];
  458. co.shadowOffsetY = prop['chart.line.shadow.offsety'];
  459. co.shadowBlur = prop['chart.line.shadow.blur'];
  460. this.DrawLine(i);
  461. // Turn the shadow off
  462. RG.NoShadow(this);
  463. }
  464. if (prop['chart.line']) {
  465. for (var i=0;i<this.data.length; ++i) {
  466. this.DrawMarks(i); // Call this again so the tickmarks appear over the line
  467. }
  468. }
  469. /**
  470. * Setup the context menu if required
  471. */
  472. if (prop['chart.contextmenu']) {
  473. RG.ShowContext(this);
  474. }
  475. /**
  476. * Draw the key if necessary
  477. */
  478. if (prop['chart.key'] && prop['chart.key'].length) {
  479. RG.DrawKey(this, prop['chart.key'], prop['chart.line.colors']);
  480. }
  481. /**
  482. * Draw " above" labels if enabled
  483. */
  484. if (prop['chart.labels.above']) {
  485. this.DrawAboveLabels();
  486. }
  487. /**
  488. * Draw the "in graph" labels, using the member function, NOT the shared function in RGraph.common.core.js
  489. */
  490. this.DrawInGraphLabels(this);
  491. /**
  492. * This function enables resizing
  493. */
  494. if (prop['chart.resizable']) {
  495. RG.AllowResizing(this);
  496. }
  497. /**
  498. * This installs the event listeners
  499. */
  500. RG.InstallEventListeners(this);
  501. /**
  502. * Fire the RGraph ondraw event
  503. */
  504. RG.FireCustomEvent(this, 'ondraw');
  505. return this;
  506. }
  507. /**
  508. * Draws the axes of the scatter graph
  509. */
  510. this.DrawAxes = function ()
  511. {
  512. var graphHeight = ca.height - this.gutterTop - this.gutterBottom;
  513. co.beginPath();
  514. co.strokeStyle = prop['chart.axis.color'];
  515. co.lineWidth = (prop['chart.axis.linewidth'] || 1) + 0.001;
  516. // Draw the Y axis
  517. if (prop['chart.noyaxis'] == false) {
  518. if (prop['chart.yaxispos'] == 'left') {
  519. co.moveTo(this.gutterLeft, this.gutterTop);
  520. co.lineTo(this.gutterLeft, ca.height - this.gutterBottom);
  521. } else {
  522. co.moveTo(ca.width - this.gutterRight, this.gutterTop);
  523. co.lineTo(ca.width - this.gutterRight, ca.height - this.gutterBottom);
  524. }
  525. }
  526. // Draw the X axis
  527. if (prop['chart.xaxis']) {
  528. if (prop['chart.xaxispos'] == 'center') {
  529. co.moveTo(this.gutterLeft, Math.round(this.gutterTop + ((ca.height - this.gutterTop - this.gutterBottom) / 2)));
  530. co.lineTo(ca.width - this.gutterRight, Math.round(this.gutterTop + ((ca.height - this.gutterTop - this.gutterBottom) / 2)));
  531. } else {
  532. co.moveTo(this.gutterLeft, ca.height - this.gutterBottom);
  533. co.lineTo(ca.width - this.gutterRight, ca.height - this.gutterBottom);
  534. }
  535. }
  536. // Draw the Y tickmarks
  537. if (prop['chart.noyaxis'] == false) {
  538. var numyticks = prop['chart.numyticks'];
  539. //for (y=this.gutterTop; y < ca.height - this.gutterBottom + (prop['chart.xaxispos'] == 'center' ? 1 : 0) ; y+=(graphHeight / numyticks)) {
  540. for (i=0; i<numyticks; ++i) {
  541. var y = ((ca.height - this.gutterTop - this.gutterBottom) / numyticks) * i;
  542. y = y + this.gutterTop;
  543. if (prop['chart.xaxispos'] == 'center' && i == (numyticks / 2)) {
  544. continue;
  545. }
  546. if (prop['chart.yaxispos'] == 'left') {
  547. co.moveTo(this.gutterLeft, Math.round(y));
  548. co.lineTo(this.gutterLeft - 3, Math.round(y));
  549. } else {
  550. co.moveTo(ca.width - this.gutterRight +3, Math.round(y));
  551. co.lineTo(ca.width - this.gutterRight, Math.round(y));
  552. }
  553. }
  554. /**
  555. * Draw the end Y tickmark if the X axis is in the centre
  556. */
  557. if (prop['chart.numyticks'] > 0) {
  558. if (prop['chart.xaxispos'] == 'center' && prop['chart.yaxispos'] == 'left') {
  559. co.moveTo(this.gutterLeft, Math.round(ca.height - this.gutterBottom));
  560. co.lineTo(this.gutterLeft - 3, Math.round(ca.height - this.gutterBottom));
  561. } else if (prop['chart.xaxispos'] == 'center') {
  562. co.moveTo(ca.width - this.gutterRight + 3, Math.round(ca.height - this.gutterBottom));
  563. co.lineTo(ca.width - this.gutterRight, Math.round(ca.height - this.gutterBottom));
  564. }
  565. }
  566. /**
  567. * Draw an extra tick if the X axis isn't being shown
  568. */
  569. if (prop['chart.xaxis'] == false && prop['chart.yaxispos'] == 'left') {
  570. co.moveTo(this.gutterLeft, Math.round(ca.height - this.gutterBottom));
  571. co.lineTo(this.gutterLeft - 3, Math.round(ca.height - this.gutterBottom));
  572. } else if (prop['chart.xaxis'] == false && prop['chart.yaxispos'] == 'right') {
  573. co.moveTo(ca.width - this.gutterRight, Math.round(ca.height - this.gutterBottom));
  574. co.lineTo(ca.width - this.gutterRight + 3, Math.round(ca.height - this.gutterBottom));
  575. }
  576. }
  577. /**
  578. * Draw the X tickmarks
  579. */
  580. if (prop['chart.numxticks'] > 0 && prop['chart.xaxis']) {
  581. var x = 0;
  582. var y = (prop['chart.xaxispos'] == 'center') ? this.gutterTop + (this.grapharea / 2) : (ca.height - this.gutterBottom);
  583. this.xTickGap = (prop['chart.labels'] && prop['chart.labels'].length) ? ((ca.width - this.gutterLeft - this.gutterRight ) / prop['chart.labels'].length) : (ca.width - this.gutterLeft - this.gutterRight) / 10;
  584. /**
  585. * This allows the number of X tickmarks to be specified
  586. */
  587. if (typeof(prop['chart.numxticks']) == 'number') {
  588. this.xTickGap = (ca.width - this.gutterLeft - this.gutterRight) / prop['chart.numxticks'];
  589. }
  590. for (x=(this.gutterLeft + (prop['chart.yaxispos'] == 'left' && prop['chart.noyaxis'] == false ? this.xTickGap : 0) );
  591. x <= (ca.width - this.gutterRight - (prop['chart.yaxispos'] == 'left' || prop['chart.noyaxis'] == true ? -1 : 1));
  592. x += this.xTickGap) {
  593. if (prop['chart.yaxispos'] == 'left' && prop['chart.noendxtick'] == true && x == (ca.width - this.gutterRight) ) {
  594. continue;
  595. } else if (prop['chart.yaxispos'] == 'right' && prop['chart.noendxtick'] == true && x == this.gutterLeft) {
  596. continue;
  597. }
  598. co.moveTo(Math.round(x), y - (prop['chart.xaxispos'] == 'center' ? 3 : 0));
  599. co.lineTo(Math.round(x), y + 3);
  600. }
  601. }
  602. co.stroke();
  603. /**
  604. * Reset the linewidth back to one
  605. */
  606. co.lineWidth = 1;
  607. }
  608. /**
  609. * Draws the labels on the scatter graph
  610. */
  611. this.DrawLabels = function ()
  612. {
  613. co.fillStyle = prop['chart.text.color'];
  614. var font = prop['chart.text.font'];
  615. var xMin = prop['chart.xmin'];
  616. var xMax = prop['chart.xmax'];
  617. var yMax = this.scale2.max;
  618. var yMin = prop['chart.ymin'] ? prop['chart.ymin'] : 0;
  619. var text_size = prop['chart.text.size'];
  620. var units_pre = prop['chart.units.pre'];
  621. var units_post = prop['chart.units.post'];
  622. var numYLabels = prop['chart.ylabels.count'];
  623. var invert = prop['chart.ylabels.invert'];
  624. var inside = prop['chart.ylabels.inside'];
  625. var context = co;
  626. var canvas = ca;
  627. var boxed = false;
  628. this.halfTextHeight = text_size / 2;
  629. this.halfGraphHeight = (ca.height - this.gutterTop - this.gutterBottom) / 2;
  630. /**
  631. * Draw the Y yaxis labels, be it at the top or center
  632. */
  633. if (prop['chart.ylabels']) {
  634. var xPos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : ca.width - this.gutterRight + 5;
  635. var align = prop['chart.yaxispos'] == 'right' ? 'left' : 'right';
  636. /**
  637. * Now change the two things above if chart.ylabels.inside is specified
  638. */
  639. if (inside) {
  640. if (prop['chart.yaxispos'] == 'left') {
  641. xPos = prop['chart.gutter.left'] + 5;
  642. align = 'left';
  643. boxed = true;
  644. } else {
  645. xPos = ca.width - prop['chart.gutter.right'] - 5;
  646. align = 'right';
  647. boxed = true;
  648. }
  649. }
  650. if (prop['chart.xaxispos'] == 'center') {
  651. /**
  652. * Specific Y labels
  653. */
  654. if (typeof(prop['chart.ylabels.specific']) == 'object' && prop['chart.ylabels.specific'] != null && prop['chart.ylabels.specific'].length) {
  655. var labels = prop['chart.ylabels.specific'];
  656. if (prop['chart.ymin'] > 0) {
  657. labels = [];
  658. for (var i=0; i<(prop['chart.ylabels.specific'].length - 1); ++i) {
  659. labels.push(prop['chart.ylabels.specific'][i]);
  660. }
  661. }
  662. for (var i=0; i<labels.length; ++i) {
  663. var y = this.gutterTop + (i * (this.grapharea / (labels.length * 2) ) );
  664. RG.Text2(this, {'font':font,
  665. 'size':text_size,
  666. 'x':xPos,
  667. 'y':y,
  668. 'text':labels[i],
  669. 'valign':'center',
  670. 'halign':align,
  671. 'bounding':boxed,
  672. 'tag': 'labels.specific'
  673. });
  674. }
  675. var reversed_labels = RG.array_reverse(labels);
  676. for (var i=0; i<reversed_labels.length; ++i) {
  677. var y = this.gutterTop + (this.grapharea / 2) + ((i+1) * (this.grapharea / (labels.length * 2) ) );
  678. RG.Text2(this, {'font':font,
  679. 'size':text_size,
  680. 'x':xPos,
  681. 'y':y,
  682. 'text':reversed_labels[i],
  683. 'valign':'center',
  684. 'halign':align,
  685. 'bounding':boxed,
  686. 'tag': 'labels.specific'
  687. });
  688. }
  689. /**
  690. * Draw the center label if chart.ymin is specified
  691. */
  692. if (prop['chart.ymin'] != 0) {
  693. RG.Text2(this, {'font':font,
  694. 'size':text_size,
  695. 'x':xPos,
  696. 'y':(this.grapharea / 2) + this.gutterTop,
  697. 'text':prop['chart.ylabels.specific'][prop['chart.ylabels.specific'].length - 1],
  698. 'valign':'center',
  699. 'halign':align,
  700. 'bounding':boxed,
  701. 'tag': 'labels.specific'
  702. });
  703. }
  704. }
  705. if (!prop['chart.ylabels.specific'] && typeof(numYLabels) == 'number') {
  706. /**
  707. * Draw the top half
  708. */
  709. for (var i=0; i<this.scale2.labels.length; ++i) {
  710. //var value = ((this.max - this.min)/ numYLabels) * (i+1);
  711. //value = (invert ? this.max - value : value);
  712. //if (!invert) value += this.min;
  713. //value = value.toFixed(prop['chart.scale.decimals']);
  714. if (!invert) {
  715. RG.Text2(this, {'font':font,
  716. 'size': text_size,
  717. 'x': xPos,
  718. 'y': this.gutterTop + this.halfGraphHeight - (((i + 1)/numYLabels) * this.halfGraphHeight),
  719. 'valign': 'center',
  720. 'halign':align,
  721. 'bounding': boxed,
  722. 'boundingFill': 'white',
  723. 'text': this.scale2.labels[i],
  724. 'tag': 'scale'
  725. });
  726. } else {
  727. RG.Text2(this, {'font':font,
  728. 'size': text_size,
  729. 'x': xPos,
  730. 'y': this.gutterTop + this.halfGraphHeight - ((i/numYLabels) * this.halfGraphHeight),
  731. 'valign': 'center',
  732. 'halign':align,
  733. 'bounding': boxed,
  734. 'boundingFill': 'white',
  735. 'text': this.scale2.labels[this.scale2.labels.length - (i + 1)],
  736. 'tag': 'scale'
  737. });
  738. }
  739. }
  740. /**
  741. * Draw the bottom half
  742. */
  743. for (var i=0; i<this.scale2.labels.length; ++i) {
  744. //var value = (((this.max - this.min)/ numYLabels) * i) + this.min;
  745. // value = (invert ? value : this.max - (value - this.min)).toFixed(prop['chart.scale.decimals']);
  746. if (!invert) {
  747. RG.Text2(this, {'font':font,
  748. 'size': text_size,
  749. 'x': xPos,
  750. 'y': this.gutterTop + this.halfGraphHeight + this.halfGraphHeight - ((i/numYLabels) * this.halfGraphHeight),
  751. 'valign': 'center',
  752. 'halign':align,
  753. 'bounding': boxed,
  754. 'boundingFill': 'white',
  755. 'text': '-' + this.scale2.labels[this.scale2.labels.length - (i+1)],
  756. 'tag': 'scale'
  757. });
  758. } else {
  759. // This ensures that the center label isn't drawn twice
  760. if (i == (this.scale2.labels.length - 1)&& invert) {
  761. continue;
  762. }
  763. RG.Text2(this, {'font':font,
  764. 'size': text_size,
  765. 'x': xPos,
  766. 'y': this.gutterTop + this.halfGraphHeight + this.halfGraphHeight - (((i + 1)/numYLabels) * this.halfGraphHeight),
  767. 'valign': 'center',
  768. 'halign':align,
  769. 'bounding': boxed,
  770. 'boundingFill': 'white',
  771. 'text': '-' + this.scale2.labels[i],
  772. 'tag': 'scale'
  773. });
  774. }
  775. }
  776. // If ymin is specified draw that
  777. if (!invert && yMin > 0) {
  778. RG.Text2(this, {'font':font,
  779. 'size': text_size,
  780. 'x': xPos,
  781. 'y': this.gutterTop + this.halfGraphHeight,
  782. 'valign': 'center',
  783. 'halign':align,
  784. 'bounding': boxed,
  785. 'boundingFill': 'white',
  786. 'text': RG.number_format(this, yMin.toFixed(prop['chart.scale.decimals']), units_pre, units_post),
  787. 'tag': 'scale'
  788. });
  789. }
  790. if (invert) {
  791. RG.Text2(this, {'font':font,
  792. 'size': text_size,
  793. 'x': xPos,
  794. 'y': this.gutterTop,
  795. 'valign': 'center',
  796. 'halign':align,
  797. 'bounding': boxed,
  798. 'boundingFill': 'white',
  799. 'text': RG.number_format(this, yMin.toFixed(prop['chart.scale.decimals']), units_pre, units_post),
  800. 'tag': 'scale'
  801. });
  802. RG.Text2(this, {'font':font,
  803. 'size': text_size,
  804. 'x': xPos,
  805. 'y': this.gutterTop + (this.halfGraphHeight * 2),
  806. 'valign': 'center',
  807. 'halign':align,
  808. 'bounding': boxed,
  809. 'boundingFill': 'white',
  810. 'text': RG.number_format(this, yMin.toFixed(prop['chart.scale.decimals']), units_pre, units_post),
  811. 'tag': 'scale'
  812. });
  813. }
  814. }
  815. // X axis at the bottom
  816. } else {
  817. var xPos = prop['chart.yaxispos'] == 'left' ? this.gutterLeft - 5 : ca.width - this.gutterRight + 5;
  818. var align = prop['chart.yaxispos'] == 'right' ? 'left' : 'right';
  819. if (inside) {
  820. if (prop['chart.yaxispos'] == 'left') {
  821. xPos = prop['chart.gutter.left'] + 5;
  822. align = 'left';
  823. boxed = true;
  824. } else {
  825. xPos = ca.width - obj.gutterRight - 5;
  826. align = 'right';
  827. boxed = true;
  828. }
  829. }
  830. /**
  831. * Specific Y labels
  832. */
  833. if (typeof(prop['chart.ylabels.specific']) == 'object' && prop['chart.ylabels.specific']) {
  834. var labels = prop['chart.ylabels.specific'];
  835. // Lose the last label
  836. if (prop['chart.ymin'] > 9999) {
  837. labels = [];
  838. for (var i=0; i<(prop['chart.ylabels.specific'].length - 1); ++i) {
  839. labels.push(prop['chart.ylabels.specific'][i]);
  840. }
  841. }
  842. for (var i=0; i<labels.length; ++i) {
  843. var y = this.gutterTop + (i * (this.grapharea / (labels.length - 1)) );
  844. RG.Text2(this, {'font':font,
  845. 'size':text_size,
  846. 'x':xPos,
  847. 'y':y,
  848. 'text':labels[i],
  849. 'halign':align,
  850. 'valign':'center',
  851. 'bounding':boxed,
  852. 'tag': 'scale'
  853. });
  854. }
  855. /**
  856. * X axis at the bottom
  857. */
  858. } else {
  859. if (typeof(numYLabels) == 'number') {
  860. if (invert) {
  861. for (var i=0; i<numYLabels; ++i) {
  862. //var value = ((this.max - this.min)/ numYLabels) * i;
  863. // value = value.toFixed(prop['chart.scale.decimals']);
  864. var interval = (ca.height - this.gutterTop - this.gutterBottom) / numYLabels;
  865. RG.Text2(this, {'font':font,
  866. 'size': text_size,
  867. 'x': xPos,
  868. 'y': this.gutterTop + ((i+1) * interval),
  869. 'valign': 'center',
  870. 'halign':align,
  871. 'bounding': boxed,
  872. 'boundingFill': 'white',
  873. 'text': this.scale2.labels[i],
  874. 'tag': 'scale'
  875. });
  876. }
  877. // No X axis being shown and there's no ymin. If ymin IS set its added further down
  878. if (!prop['chart.xaxis'] && !prop['chart.ymin']) {
  879. RG.Text2(this, {'font':font,
  880. 'size': text_size,
  881. 'x': xPos,
  882. 'y': this.gutterTop,
  883. 'valign': 'center',
  884. 'halign':align,
  885. 'bounding': boxed,
  886. 'boundingFill': 'white',
  887. 'text': RG.number_format(this, (this.min).toFixed(prop['chart.scale.decimals']), units_pre, units_post),
  888. 'tag': 'scale'
  889. });
  890. }
  891. } else {
  892. for (var i=0; i<this.scale2.labels.length; ++i) {
  893. //var value = ((this.max - this.min)/ numYLabels) * (i+1);
  894. // value = (invert ? this.max - value : value);
  895. // if (!invert) value += this.min;
  896. // value = value.toFixed(prop['chart.scale.decimals']);
  897. RG.Text2(this, {'font':font,
  898. 'size': text_size,
  899. 'x': xPos,
  900. 'y': this.gutterTop + this.grapharea - (((i + 1)/this.scale2.labels.length) * this.grapharea),
  901. 'valign': 'center',
  902. 'halign':align,
  903. 'bounding': boxed,
  904. 'boundingFill': 'white',
  905. 'text': this.scale2.labels[i],
  906. 'tag': 'scale'
  907. });
  908. }
  909. if (!prop['chart.xaxis'] && prop['chart.ymin'] == 0) {
  910. RG.Text2(this, {'font':font,
  911. 'size': text_size,
  912. 'x': xPos,
  913. 'y': ca.height - this.gutterBottom,
  914. 'valign': 'center',
  915. 'halign':align,
  916. 'bounding': boxed,
  917. 'boundingFill': 'white',
  918. 'text': RG.number_format(this, (0).toFixed(prop['chart.scale.decimals']), units_pre, units_post),
  919. 'tag': 'scale'
  920. });
  921. }
  922. }
  923. }
  924. if (prop['chart.ymin'] && !invert) {
  925. RG.Text2(this, {'font':font,
  926. 'size': text_size,
  927. 'x': xPos,
  928. 'y': ca.height - this.gutterBottom,
  929. 'valign': 'center',
  930. 'halign':align,
  931. 'bounding': boxed,
  932. 'boundingFill': 'white',
  933. 'text': RG.number_format(this, prop['chart.ymin'].toFixed(prop['chart.scale.decimals']), units_pre, units_post),
  934. 'tag': 'scale'
  935. });
  936. } else if (invert) {
  937. RG.Text2(this, {'font':font,
  938. 'size': text_size,
  939. 'x': xPos,
  940. 'y': this.gutterTop,
  941. 'valign': 'center',
  942. 'halign':align,
  943. 'bounding': boxed,
  944. 'boundingFill': 'white',
  945. 'text': RG.number_format(this, prop['chart.ymin'].toFixed(prop['chart.scale.decimals']), units_pre, units_post),
  946. 'tag': 'scale'
  947. });
  948. }
  949. }
  950. }
  951. }
  952. /**
  953. * Draw an X scale
  954. */
  955. if (prop['chart.xscale']) {
  956. var numXLabels = prop['chart.xscale.numlabels'];
  957. var y = ca.height - this.gutterBottom + 5 + (text_size / 2);
  958. var units_pre_x = prop['chart.xscale.units.pre'];
  959. var units_post_x = prop['chart.xscale.units.post'];
  960. var decimals = prop['chart.xscale.decimals'];
  961. var point = prop['chart.xscale.point'];
  962. var thousand = prop['chart.xscale.thousand'];
  963. if (!prop['chart.xmax']) {
  964. var xmax = 0;
  965. var xmin = prop['chart.xmin'];
  966. for (var ds=0; ds<this.data.length; ++ds) {
  967. for (var point=0; point<this.data[ds].length; ++point) {
  968. xmax = Math.max(xmax, this.data[ds][point][0]);
  969. }
  970. }
  971. } else {
  972. xmax = prop['chart.xmax'];
  973. xmin = prop['chart.xmin']
  974. }
  975. this.xscale2 = RG.getScale2(this, {'max':xmax,
  976. 'min': xmin,
  977. 'scale.decimals': decimals,
  978. 'scale.point': point,
  979. 'scale.thousand': thousand,
  980. 'units.pre': units_pre_x,
  981. 'units.post': units_post_x,
  982. 'ylabels.count': numXLabels,
  983. 'strict': true
  984. });
  985. this.Set('chart.xmax', this.xscale2.max);
  986. var interval = (ca.width - this.gutterLeft - this.gutterRight) / this.xscale2.labels.length;
  987. for (var i=0; i<this.xscale2.labels.length; ++i) {
  988. var num = ( (prop['chart.xmax'] - prop['chart.xmin']) * ((i+1) / numXLabels)) + (xmin || 0);
  989. var x = this.gutterLeft + ((i+1) * interval);
  990. if (typeof(prop['chart.xscale.formatter']) == 'function') {
  991. var text = String(prop['chart.xscale.formatter'](this, num));
  992. } else {
  993. var text = this.xscale2.labels[i]
  994. }
  995. RG.Text2(this, {'font':font,
  996. 'size': text_size,
  997. 'x': x,
  998. 'y': y,
  999. 'valign': 'center',
  1000. 'halign':'center',
  1001. 'text':text,
  1002. 'tag': 'xscale'
  1003. });
  1004. }
  1005. // If the Y axis is on the right hand side - draw the left most X label
  1006. if (prop['chart.yaxispos'] == 'right') {
  1007. RG.Text2(this, {'font':font,
  1008. 'size': text_size,
  1009. 'x': this.gutterLeft,
  1010. 'y': y,
  1011. 'valign': 'center',
  1012. 'halign':'center',
  1013. 'text':String(prop['chart.xmin']),
  1014. 'tag': 'xscale'
  1015. });
  1016. }
  1017. /**
  1018. * Draw X labels
  1019. */
  1020. } else {
  1021. // Put the text on the X axis
  1022. var graphArea = ca.width - this.gutterLeft - this.gutterRight;
  1023. var xInterval = graphArea / prop['chart.labels'].length;
  1024. var xPos = this.gutterLeft;
  1025. var yPos = (ca.height - this.gutterBottom) + 3;
  1026. var labels = prop['chart.labels'];
  1027. /**
  1028. * Text angle
  1029. */
  1030. var angle = 0;
  1031. var valign = 'top';
  1032. var halign = 'center';
  1033. if (prop['chart.text.angle'] > 0) {
  1034. angle = -1 * prop['chart.text.angle'];
  1035. valign = 'center';
  1036. halign = 'right';
  1037. yPos += 10;
  1038. }
  1039. for (i=0; i<labels.length; ++i) {
  1040. if (typeof(labels[i]) == 'object') {
  1041. if (prop['chart.labels.specific.align'] == 'center') {
  1042. var rightEdge = 0;
  1043. if (labels[i+1] && labels[i+1][1]) {
  1044. rightEdge = labels[i+1][1];
  1045. } else {
  1046. rightEdge = prop['chart.xmax'];
  1047. }
  1048. var offset = (this.getXCoord(rightEdge) - this.getXCoord(labels[i][1])) / 2;
  1049. } else {
  1050. var offset = 5;
  1051. }
  1052. RG.Text2(this, {'font':font,
  1053. 'size': prop['chart.text.size'],
  1054. 'x': this.getXCoord(labels[i][1]) + offset,
  1055. 'y': yPos,
  1056. 'valign': valign,
  1057. 'halign':angle != 0 ? 'right' : (prop['chart.labels.specific.align'] == 'center' ? 'center' : 'left'),
  1058. 'text':String(labels[i][0]),
  1059. 'angle':angle,
  1060. 'marker':false,
  1061. 'tag': 'labels.specific'
  1062. });
  1063. /**
  1064. * Draw the gray indicator line
  1065. */
  1066. co.beginPath();
  1067. co.strokeStyle = '#bbb';
  1068. co.moveTo(Math.round(this.gutterLeft + (graphArea * ((labels[i][1] - xMin)/ (prop['chart.xmax'] - xMin)))), ca.height - this.gutterBottom);
  1069. co.lineTo(Math.round(this.gutterLeft + (graphArea * ((labels[i][1] - xMin)/ (prop['chart.xmax'] - xMin)))), ca.height - this.gutterBottom + 20);
  1070. co.stroke();
  1071. } else {
  1072. RG.Text2(this, {'font':font,
  1073. 'size': prop['chart.text.size'],
  1074. 'x': xPos + (xInterval / 2),
  1075. 'y': yPos,
  1076. 'valign': valign,
  1077. 'halign':halign,
  1078. 'text':String(labels[i]),
  1079. 'angle':angle,
  1080. 'tag': 'labels'
  1081. });
  1082. }
  1083. // Do this for the next time around
  1084. xPos += xInterval;
  1085. }
  1086. /**
  1087. * Draw the final indicator line
  1088. */
  1089. if (typeof(labels[0]) == 'object') {
  1090. co.beginPath();
  1091. co.strokeStyle = '#bbb';
  1092. co.moveTo(this.gutterLeft + graphArea, ca.height - this.gutterBottom);
  1093. co.lineTo(this.gutterLeft + graphArea, ca.height - this.gutterBottom + 20);
  1094. co.stroke();
  1095. }
  1096. }
  1097. }
  1098. /**
  1099. * Draws the actual scatter graph marks
  1100. *
  1101. * @param i integer The dataset index
  1102. */
  1103. this.DrawMarks = function (i)
  1104. {
  1105. /**
  1106. * Reset the coords array
  1107. */
  1108. this.coords[i] = [];
  1109. /**
  1110. * Plot the values
  1111. */
  1112. var xmax = prop['chart.xmax'];
  1113. var default_color = prop['chart.defaultcolor'];
  1114. for (var j=0; j<this.data[i].length; ++j) {
  1115. /**
  1116. * This is here because tooltips are optional
  1117. */
  1118. var data_point = this.data[i];
  1119. var xCoord = data_point[j][0];
  1120. var yCoord = data_point[j][1];
  1121. var color = data_point[j][2] ? data_point[j][2] : default_color;
  1122. var tooltip = (data_point[j] && data_point[j][3]) ? data_point[j][3] : null;
  1123. this.DrawMark(
  1124. i,
  1125. xCoord,
  1126. yCoord,
  1127. xmax,
  1128. this.scale2.max,
  1129. color,
  1130. tooltip,
  1131. this.coords[i],
  1132. data_point,
  1133. j
  1134. );
  1135. }
  1136. }
  1137. /**
  1138. * Draws a single scatter mark
  1139. */
  1140. this.DrawMark = function (data_set_index, x, y, xMax, yMax, color, tooltip, coords, data, data_index)
  1141. {
  1142. var tickmarks = prop['chart.tickmarks'];
  1143. var tickSize = prop['chart.ticksize'];
  1144. var xMin = prop['chart.xmin'];
  1145. var x = ((x - xMin) / (xMax - xMin)) * (ca.width - this.gutterLeft - this.gutterRight);
  1146. var originalX = x;
  1147. var originalY = y;
  1148. /**
  1149. * This allows chart.tickmarks to be an array
  1150. */
  1151. if (tickmarks && typeof(tickmarks) == 'object') {
  1152. tickmarks = tickmarks[data_set_index];
  1153. }
  1154. /**
  1155. * This allows chart.ticksize to be an array
  1156. */
  1157. if (typeof(tickSize) == 'object') {
  1158. var tickSize = tickSize[data_set_index];
  1159. var halfTickSize = tickSize / 2;
  1160. } else {
  1161. var halfTickSize = tickSize / 2;
  1162. }
  1163. /**
  1164. * This bit is for boxplots only
  1165. */
  1166. if ( y
  1167. && typeof(y) == 'object'
  1168. && typeof(y[0]) == 'number'
  1169. && typeof(y[1]) == 'number'
  1170. && typeof(y[2]) == 'number'
  1171. && typeof(y[3]) == 'number'
  1172. && typeof(y[4]) == 'number'
  1173. ) {
  1174. //var yMin = prop['chart.ymin'] ? prop['chart.ymin'] : 0;
  1175. this.Set('chart.boxplot', true);
  1176. //this.graphheight = ca.height - this.gutterTop - this.gutterBottom;
  1177. //if (prop['chart.xaxispos'] == 'center') {
  1178. // this.graphheight /= 2;
  1179. //}
  1180. var y0 = this.getYCoord(y[0]);//(this.graphheight) - ((y[4] - yMin) / (yMax - yMin)) * (this.graphheight);
  1181. var y1 = this.getYCoord(y[1]);//(this.graphheight) - ((y[3] - yMin) / (yMax - yMin)) * (this.graphheight);
  1182. var y2 = this.getYCoord(y[2]);//(this.graphheight) - ((y[2] - yMin) / (yMax - yMin)) * (this.graphheight);
  1183. var y3 = this.getYCoord(y[3]);//(this.graphheight) - ((y[1] - yMin) / (yMax - yMin)) * (this.graphheight);
  1184. var y4 = this.getYCoord(y[4]);//(this.graphheight) - ((y[0] - yMin) / (yMax - yMin)) * (this.graphheight);
  1185. var col1 = y[5];
  1186. var col2 = y[6];
  1187. var boxWidth = typeof(y[7]) == 'number' ? y[7] : prop['chart.boxplot.width'];
  1188. //var y = this.graphheight - y2;
  1189. } else {
  1190. /**
  1191. * The new way of getting the Y coord. This function (should) handle everything
  1192. */
  1193. var yCoord = this.getYCoord(y);
  1194. }
  1195. //if (prop['chart.xaxispos'] == 'center'] {
  1196. // y /= 2;
  1197. // y += this.halfGraphHeight;
  1198. //
  1199. // if (prop['chart.ylabels.invert']) {
  1200. // p(y)
  1201. // }
  1202. //}
  1203. /**
  1204. * Account for the X axis being at the centre
  1205. */
  1206. // This is so that points are on the graph, and not the gutter
  1207. x += this.gutterLeft;
  1208. //y = ca.height - this.gutterBottom - y;
  1209. co.beginPath();
  1210. // Color
  1211. co.strokeStyle = color;
  1212. /**
  1213. * Boxplots
  1214. */
  1215. if (prop['chart.boxplot']) {
  1216. // boxWidth is now a scale value, so convert it to a pixel vlue
  1217. boxWidth = (boxWidth / prop['chart.xmax']) * (ca.width -this.gutterLeft - this.gutterRight);
  1218. var halfBoxWidth = boxWidth / 2;
  1219. if (prop['chart.line.visible']) {
  1220. co.beginPath();
  1221. co.strokeRect(x - halfBoxWidth, y1, boxWidth, y3 - y1);
  1222. // Draw the upper coloured box if a value is specified
  1223. if (col1) {
  1224. co.fillStyle = col1;
  1225. co.fillRect(x - halfBoxWidth, y1, boxWidth, y2 - y1);
  1226. }
  1227. // Draw the lower coloured box if a value is specified
  1228. if (col2) {
  1229. co.fillStyle = col2;
  1230. co.fillRect(x - halfBoxWidth, y2, boxWidth, y3 - y2);
  1231. }
  1232. co.stroke();
  1233. // Now draw the whiskers
  1234. co.beginPath();
  1235. if (prop['chart.boxplot.capped']) {
  1236. co.moveTo(x - halfBoxWidth, Math.round(y0));
  1237. co.lineTo(x + halfBoxWidth, Math.round(y0));
  1238. }
  1239. co.moveTo(Math.round(x), y0);
  1240. co.lineTo(Math.round(x), y1);
  1241. if (prop['chart.boxplot.capped']) {
  1242. co.moveTo(x - halfBoxWidth, Math.round(y4));
  1243. co.lineTo(x + halfBoxWidth, Math.round(y4));
  1244. }
  1245. co.moveTo(Math.round(x), y4);
  1246. co.lineTo(Math.round(x), y3);
  1247. co.stroke();
  1248. }
  1249. }
  1250. /**
  1251. * Draw the tickmark, but not for boxplots
  1252. */
  1253. if (prop['chart.line.visible'] && typeof(y) == 'number' && !y0 && !y1 && !y2 && !y3 && !y4) {
  1254. if (tickmarks == 'circle') {
  1255. co.arc(x, yCoord, halfTickSize, 0, 6.28, 0);
  1256. co.fillStyle = color;
  1257. co.fill();
  1258. } else if (tickmarks == 'plus') {
  1259. co.moveTo(x, yCoord - halfTickSize);
  1260. co.lineTo(x, yCoord + halfTickSize);
  1261. co.moveTo(x - halfTickSize, yCoord);
  1262. co.lineTo(x + halfTickSize, yCoord);
  1263. co.stroke();
  1264. } else if (tickmarks == 'square') {
  1265. co.strokeStyle = color;
  1266. co.fillStyle = color;
  1267. co.fillRect(
  1268. x - halfTickSize,
  1269. yCoord - halfTickSize,
  1270. tickSize,
  1271. tickSize
  1272. );
  1273. //co.fill();
  1274. } else if (tickmarks == 'cross') {
  1275. co.moveTo(x - halfTickSize, yCoord - halfTickSize);
  1276. co.lineTo(x + halfTickSize, yCoord + halfTickSize);
  1277. co.moveTo(x + halfTickSize, yCoord - halfTickSize);
  1278. co.lineTo(x - halfTickSize, yCoord + halfTickSize);
  1279. co.stroke();
  1280. /**
  1281. * Diamond shape tickmarks
  1282. */
  1283. } else if (tickmarks == 'diamond') {
  1284. co.fillStyle = co.strokeStyle;
  1285. co.moveTo(x, yCoord - halfTickSize);
  1286. co.lineTo(x + halfTickSize, yCoord);
  1287. co.lineTo(x, yCoord + halfTickSize);
  1288. co.lineTo(x - halfTickSize, yCoord);
  1289. co.lineTo(x, yCoord - halfTickSize);
  1290. co.fill();
  1291. co.stroke();
  1292. /**
  1293. * Custom tickmark style
  1294. */
  1295. } else if (typeof(tickmarks) == 'function') {
  1296. var graphWidth = ca.width - this.gutterLeft - this.gutterRight
  1297. var graphheight = ca.height - this.gutterTop - this.gutterBottom;
  1298. var xVal = ((x - this.gutterLeft) / graphWidth) * xMax;
  1299. var yVal = ((graphheight - (yCoord - this.gutterTop)) / graphheight) * yMax;
  1300. tickmarks(this, data, x, yCoord, xVal, yVal, xMax, yMax, color, data_set_index, data_index)
  1301. /**
  1302. * No tickmarks
  1303. */
  1304. } else if (tickmarks == null) {
  1305. /**
  1306. * Unknown tickmark type
  1307. */
  1308. } else {
  1309. alert('[SCATTER] (' + this.id + ') Unknown tickmark style: ' + tickmarks );
  1310. }
  1311. }
  1312. /**
  1313. * Add the tickmark to the coords array
  1314. */
  1315. if ( prop['chart.boxplot']
  1316. && typeof(y0) == 'number'
  1317. && typeof(y1) == 'number'
  1318. && typeof(y2) == 'number'
  1319. && typeof(y3) == 'number'
  1320. && typeof(y4) == 'number') {
  1321. x = [x - halfBoxWidth, x + halfBoxWidth];
  1322. yCoord = [y0, y1, y2, y3, y4];
  1323. }
  1324. coords.push([x, yCoord, tooltip]);
  1325. }
  1326. /**
  1327. * Draws an optional line connecting the tick marks.
  1328. *
  1329. * @param i The index of the dataset to use
  1330. */
  1331. this.DrawLine = function (i)
  1332. {
  1333. if (typeof(prop['chart.line.visible']) == 'boolean' && prop['chart.line.visible'] == false) {
  1334. return;
  1335. }
  1336. if (prop['chart.line'] && this.coords[i].length >= 2) {
  1337. co.lineCap = 'round';
  1338. co.lineJoin = 'round';
  1339. co.lineWidth = this.GetLineWidth(i);// i is the index of the set of coordinates
  1340. co.strokeStyle = prop['chart.line.colors'][i];
  1341. co.beginPath();
  1342. var len = this.coords[i].length;
  1343. var prevY = null;
  1344. var currY = null;
  1345. for (var j=0; j<this.coords[i].length; ++j) {
  1346. var xPos = this.coords[i][j][0];
  1347. var yPos = this.coords[i][j][1];
  1348. if (j > 0) prevY = this.coords[i][j - 1][1];
  1349. currY = yPos;
  1350. if (j == 0 || RG.is_null(prevY) || RG.is_null(currY)) {
  1351. co.moveTo(xPos, yPos);
  1352. } else {
  1353. // Stepped?
  1354. var stepped = prop['chart.line.stepped'];
  1355. if ( (typeof(stepped) == 'boolean' && stepped)
  1356. || (typeof(stepped) == 'object' && stepped[i])
  1357. ) {
  1358. co.lineTo(this.coords[i][j][0], this.coords[i][j - 1][1]);
  1359. }
  1360. co.lineTo(xPos, yPos);
  1361. }
  1362. }
  1363. co.stroke();
  1364. }
  1365. /**
  1366. * Set the linewidth back to 1
  1367. */
  1368. co.lineWidth = 1;
  1369. }
  1370. /**
  1371. * Returns the linewidth
  1372. *
  1373. * @param number i The index of the "line" (/set of coordinates)
  1374. */
  1375. this.GetLineWidth = function (i)
  1376. {
  1377. var linewidth = prop['chart.line.linewidth'];
  1378. if (typeof(linewidth) == 'number') {
  1379. return linewidth;
  1380. } else if (typeof(linewidth) == 'object') {
  1381. if (linewidth[i]) {
  1382. return linewidth[i];
  1383. } else {
  1384. return linewidth[0];
  1385. }
  1386. alert('[SCATTER] Error! chart.linewidth should be a single number or an array of one or more numbers');
  1387. }
  1388. }
  1389. /**
  1390. * Draws vertical bars. Line chart doesn't use a horizontal scale, hence this function
  1391. * is not common
  1392. */
  1393. this.DrawVBars = function ()
  1394. {
  1395. var vbars = prop['chart.background.vbars'];
  1396. var graphWidth = ca.width - this.gutterLeft - this.gutterRight;
  1397. if (vbars) {
  1398. var xmax = prop['chart.xmax'];
  1399. var xmin = prop['chart.xmin'];
  1400. RGraph.each (vbars, function (key, value)
  1401. {
  1402. /**
  1403. * Accomodate date/time values
  1404. */
  1405. if (typeof value[0] == 'string') value[0] = Date.parse(value[0]);
  1406. if (typeof value[1] == 'string') value[1] = Date.parse(value[1]) - value[0];
  1407. var x = (( (value[0] - xmin) / (xmax - xmin) ) * graphWidth) + this.gutterLeft;
  1408. var width = (value[1] / (xmax - xmin) ) * graphWidth;
  1409. co.fillStyle = value[2];
  1410. co.fillRect(x, this.gutterTop, width, (ca.height - this.gutterTop - this.gutterBottom));
  1411. }, this);
  1412. }
  1413. }
  1414. /**
  1415. * Draws in-graph labels.
  1416. *
  1417. * @param object obj The graph object
  1418. */
  1419. this.DrawInGraphLabels = function (obj)
  1420. {
  1421. var labels = obj.Get('chart.labels.ingraph');
  1422. var labels_processed = [];
  1423. // Defaults
  1424. var fgcolor = 'black';
  1425. var bgcolor = 'white';
  1426. var direction = 1;
  1427. if (!labels) {
  1428. return;
  1429. }
  1430. /**
  1431. * Preprocess the labels array. Numbers are expanded
  1432. */
  1433. for (var i=0; i<labels.length; ++i) {
  1434. if (typeof(labels[i]) == 'number') {
  1435. for (var j=0; j<labels[i]; ++j) {
  1436. labels_processed.push(null);
  1437. }
  1438. } else if (typeof(labels[i]) == 'string' || typeof(labels[i]) == 'object') {
  1439. labels_processed.push(labels[i]);
  1440. } else {
  1441. labels_processed.push('');
  1442. }
  1443. }
  1444. /**
  1445. * Turn off any shadow
  1446. */
  1447. RG.NoShadow(obj);
  1448. if (labels_processed && labels_processed.length > 0) {
  1449. var i=0;
  1450. for (var set=0; set<obj.coords.length; ++set) {
  1451. for (var point = 0; point<obj.coords[set].length; ++point) {
  1452. if (labels_processed[i]) {
  1453. var x = obj.coords[set][point][0];
  1454. var y = obj.coords[set][point][1];
  1455. var length = typeof(labels_processed[i][4]) == 'number' ? labels_processed[i][4] : 25;
  1456. var text_x = x;
  1457. var text_y = y - 5 - length;
  1458. co.moveTo(x, y - 5);
  1459. co.lineTo(x, y - 5 - length);
  1460. co.stroke();
  1461. co.beginPath();
  1462. // This draws the arrow
  1463. co.moveTo(x, y - 5);
  1464. co.lineTo(x - 3, y - 10);
  1465. co.lineTo(x + 3, y - 10);
  1466. co.closePath();
  1467. co.beginPath();
  1468. // Fore ground color
  1469. co.fillStyle = (typeof(labels_processed[i]) == 'object' && typeof(labels_processed[i][1]) == 'string') ? labels_processed[i][1] : 'black';
  1470. RG.Text2(this, {
  1471. 'font':obj.Get('chart.text.font'),
  1472. 'size':obj.Get('chart.text.size'),
  1473. 'x':text_x,
  1474. 'y':text_y,
  1475. 'text':(typeof(labels_processed[i]) == 'object' && typeof(labels_processed[i][0]) == 'string') ? labels_processed[i][0] : labels_processed[i],
  1476. 'valign':'bottom',
  1477. 'halign':'center',
  1478. 'bounding':true,
  1479. 'boundingFill':(typeof(labels_processed[i]) == 'object' && typeof(labels_processed[i][2]) == 'string') ? labels_processed[i][2] : 'white',
  1480. 'tag': 'labels.ingraph'
  1481. });
  1482. co.fill();
  1483. }
  1484. i++;
  1485. }
  1486. }
  1487. }
  1488. }
  1489. /**
  1490. * This function makes it much easier to get the (if any) point that is currently being hovered over.
  1491. *
  1492. * @param object e The event object
  1493. */
  1494. this.getShape =
  1495. this.getPoint = function (e)
  1496. {
  1497. var mouseXY = RG.getMouseXY(e);
  1498. var mouseX = mouseXY[0];
  1499. var mouseY = mouseXY[1];
  1500. var overHotspot = false;
  1501. var offset = prop['chart.tooltips.hotspot']; // This is how far the hotspot extends
  1502. for (var set=0; set<this.coords.length; ++set) {
  1503. for (var i=0; i<this.coords[set].length; ++i) {
  1504. var x = this.coords[set][i][0];
  1505. var y = this.coords[set][i][1];
  1506. var tooltip = this.data[set][i][3];
  1507. if (typeof(y) == 'number') {
  1508. if (mouseX <= (x + offset) &&
  1509. mouseX >= (x - offset) &&
  1510. mouseY <= (y + offset) &&
  1511. mouseY >= (y - offset)) {
  1512. var tooltip = RG.parseTooltipText(this.data[set][i][3], 0);
  1513. var index_adjusted = i;
  1514. for (var ds=(set-1); ds >=0; --ds) {
  1515. index_adjusted += this.data[ds].length;
  1516. }
  1517. return {
  1518. 0: this, 1: x, 2: y, 3: set, 4: i, 5: this.data[set][i][3],
  1519. 'object': this, 'x': x, 'y': y, 'dataset': set, 'index': i, 'tooltip': tooltip, 'index_adjusted': index_adjusted
  1520. };
  1521. }
  1522. } else if (RG.is_null(y)) {
  1523. // Nothing to see here
  1524. } else {
  1525. var mark = this.data[set][i];
  1526. /**
  1527. * Determine the width
  1528. */
  1529. var width = prop['chart.boxplot.width'];
  1530. if (typeof(mark[1][7]) == 'number') {
  1531. width = mark[1][7];
  1532. }
  1533. if ( typeof(x) == 'object'
  1534. && mouseX > x[0]
  1535. && mouseX < x[1]
  1536. && mouseY < y[1]
  1537. && mouseY > y[3]
  1538. ) {
  1539. var tooltip = RG.parseTooltipText(this.data[set][i][3], 0);
  1540. return {
  1541. 0: this, 1: x[0], 2: x[1] - x[0], 3: y[1], 4: y[3] - y[1], 5: set, 6: i, 7: this.data[set][i][3],
  1542. 'object': this, 'x': x[0], 'y': y[1], 'width': x[1] - x[0], 'height': y[3] - y[1], 'dataset': set, 'index': i, 'tooltip': tooltip
  1543. };
  1544. }
  1545. }
  1546. }
  1547. }
  1548. }
  1549. /**
  1550. * Draws the above line labels
  1551. */
  1552. this.DrawAboveLabels = function ()
  1553. {
  1554. var size = prop['chart.labels.above.size'];
  1555. var font = prop['chart.text.font'];
  1556. var units_pre = prop['chart.units.pre'];
  1557. var units_post = prop['chart.units.post'];
  1558. for (var set=0; set<this.coords.length; ++set) {
  1559. for (var point=0; point<this.coords[set].length; ++point) {
  1560. var x_val = this.data[set][point][0];
  1561. var y_val = this.data[set][point][1];
  1562. if (!RG.is_null(y_val)) {
  1563. // Use the top most value from a box plot
  1564. if (RG.is_array(y_val)) {
  1565. var max = 0;
  1566. for (var i=0; i<y_val; ++i) {
  1567. max = Math.max(max, y_val[i]);
  1568. }
  1569. y_val = max;
  1570. }
  1571. var x_pos = this.coords[set][point][0];
  1572. var y_pos = this.coords[set][point][1];
  1573. RG.Text2(this, {
  1574. 'font':font,
  1575. 'size':size,
  1576. 'x':x_pos,
  1577. 'y':y_pos - 5 - size,
  1578. 'text':x_val.toFixed(prop['chart.labels.above.decimals']) + ', ' + y_val.toFixed(prop['chart.labels.above.decimals']),
  1579. 'valign':'center',
  1580. 'halign':'center',
  1581. 'bounding':true,
  1582. 'boundingFill':'rgba(255, 255, 255, 0.7)',
  1583. 'tag': 'labels.above'
  1584. });
  1585. }
  1586. }
  1587. }
  1588. }
  1589. /**
  1590. * When you click on the chart, this method can return the Y value at that point. It works for any point on the
  1591. * chart (that is inside the gutters) - not just points within the Bars.
  1592. *
  1593. * @param object e The event object
  1594. */
  1595. this.getYValue =
  1596. this.getValue = function (arg)
  1597. {
  1598. if (arg.length == 2) {
  1599. var mouseX = arg[0];
  1600. var mouseY = arg[1];
  1601. } else {
  1602. var mouseCoords = RG.getMouseXY(arg);
  1603. var mouseX = mouseCoords[0];
  1604. var mouseY = mouseCoords[1];
  1605. }
  1606. var obj = this;
  1607. if ( mouseY < this.gutterTop
  1608. || mouseY > (ca.height - this.gutterBottom)
  1609. || mouseX < this.gutterLeft
  1610. || mouseX > (ca.width - this.gutterRight)
  1611. ) {
  1612. return null;
  1613. }
  1614. if (prop['chart.xaxispos'] == 'center') {
  1615. var value = (((this.grapharea / 2) - (mouseY - this.gutterTop)) / this.grapharea) * (this.max - this.min)
  1616. value *= 2;
  1617. if (value >= 0) {
  1618. value += this.min
  1619. } else {
  1620. value -= this.min
  1621. }
  1622. } else {
  1623. var value = ((this.grapharea - (mouseY - this.gutterTop)) / this.grapharea) * (this.max - this.min)
  1624. value += this.min;
  1625. }
  1626. return value;
  1627. }
  1628. /**
  1629. * When you click on the chart, this method can return the X value at that point.
  1630. *
  1631. * @param mixed arg This can either be an event object or the X coordinate
  1632. * @param number If specifying the X coord as the first arg then this should be the Y coord
  1633. */
  1634. this.getXValue = function (arg)
  1635. {
  1636. if (arg.length == 2) {
  1637. var mouseX = arg[0];
  1638. var mouseY = arg[1];
  1639. } else {
  1640. var mouseXY = RG.getMouseXY(arg);
  1641. var mouseX = mouseXY[0];
  1642. var mouseY = mouseXY[1];
  1643. }
  1644. var obj = this;
  1645. if ( mouseY < this.gutterTop
  1646. || mouseY > (ca.height - this.gutterBottom)
  1647. || mouseX < this.gutterLeft
  1648. || mouseX > (ca.width - this.gutterRight)
  1649. ) {
  1650. return null;
  1651. }
  1652. var width = (ca.width - this.gutterLeft - this.gutterRight);
  1653. var value = ((mouseX - this.gutterLeft) / width) * (prop['chart.xmax'] - prop['chart.xmin'])
  1654. value += prop['chart.xmin'];
  1655. return value;
  1656. }
  1657. /**
  1658. * Each object type has its own Highlight() function which highlights the appropriate shape
  1659. *
  1660. * @param object shape The shape to highlight
  1661. */
  1662. this.Highlight = function (shape)
  1663. {
  1664. // Boxplot highlight
  1665. if (shape['height']) {
  1666. RG.Highlight.Rect(this, shape);
  1667. // Point highlight
  1668. } else {
  1669. RG.Highlight.Point(this, shape);
  1670. }
  1671. }
  1672. /**
  1673. * The getObjectByXY() worker method. Don't call this call:
  1674. *
  1675. * RG.ObjectRegistry.getObjectByXY(e)
  1676. *
  1677. * @param object e The event object
  1678. */
  1679. this.getObjectByXY = function (e)
  1680. {
  1681. var mouseXY = RG.getMouseXY(e);
  1682. if (
  1683. mouseXY[0] > (this.gutterLeft - 3)
  1684. && mouseXY[0] < (ca.width - this.gutterRight + 3)
  1685. && mouseXY[1] > (this.gutterTop - 3)
  1686. && mouseXY[1] < ((ca.height - this.gutterBottom) + 3)
  1687. ) {
  1688. return this;
  1689. }
  1690. }
  1691. /**
  1692. * This function can be used when the canvas is clicked on (or similar - depending on the event)
  1693. * to retrieve the relevant X coordinate for a particular value.
  1694. *
  1695. * @param int value The value to get the X coordinate for
  1696. */
  1697. this.getXCoord = function (value)
  1698. {
  1699. if (typeof(value) != 'number') {
  1700. return null;
  1701. }
  1702. var xmin = prop['chart.xmin'];
  1703. var xmax = prop['chart.xmax'];
  1704. var x;
  1705. if (value < xmin) return null;
  1706. if (value > xmax) return null;
  1707. var gutterRight = this.gutterRight;
  1708. var gutterLeft = this.gutterLeft;
  1709. if (prop['chart.yaxispos'] == 'right') {
  1710. x = ((value - xmin) / (xmax - xmin)) * (ca.width - gutterLeft - gutterRight);
  1711. x = (ca.width - gutterRight - x);
  1712. } else {
  1713. x = ((value - xmin) / (xmax - xmin)) * (ca.width - gutterLeft - gutterRight);
  1714. x = x + gutterLeft;
  1715. }
  1716. return x;
  1717. }
  1718. /**
  1719. * This function positions a tooltip when it is displayed
  1720. *
  1721. * @param obj object The chart object
  1722. * @param int x The X coordinate specified for the tooltip
  1723. * @param int y The Y coordinate specified for the tooltip
  1724. * @param objec tooltip The tooltips DIV element
  1725. */
  1726. this.positionTooltip = function (obj, x, y, tooltip, idx)
  1727. {
  1728. var shape = RG.Registry.Get('chart.tooltip.shape');
  1729. var dataset = shape['dataset'];
  1730. var index = shape['index'];
  1731. var coordX = obj.coords[dataset][index][0]
  1732. var coordY = obj.coords[dataset][index][1]
  1733. var canvasXY = RG.getCanvasXY(obj.canvas);
  1734. var gutterLeft = obj.gutterLeft;
  1735. var gutterTop = obj.gutterTop;
  1736. var width = tooltip.offsetWidth;
  1737. var height = tooltip.offsetHeight;
  1738. tooltip.style.left = 0;
  1739. tooltip.style.top = 0;
  1740. // Is the coord a boxplot
  1741. var isBoxplot = typeof(coordY) == 'object' ? true : false;
  1742. // Show any overflow (ie the arrow)
  1743. tooltip.style.overflow = '';
  1744. // Create the arrow
  1745. var img = new Image();
  1746. img.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAFCAYAAACjKgd3AAAARUlEQVQYV2NkQAN79+797+RkhC4M5+/bd47B2dmZEVkBCgcmgcsgbAaA9GA1BCSBbhAuA/AagmwQPgMIGgIzCD0M0AMMAEFVIAa6UQgcAAAAAElFTkSuQmCC';
  1747. img.style.position = 'absolute';
  1748. img.id = '__rgraph_tooltip_pointer__';
  1749. img.style.top = (tooltip.offsetHeight - 2) + 'px';
  1750. tooltip.appendChild(img);
  1751. // Reposition the tooltip if at the edges:
  1752. // LEFT edge //////////////////////////////////////////////////////////////////
  1753. if ((canvasXY[0] + (coordX[0] || coordX) - (width / 2)) < 10) {
  1754. if (isBoxplot) {
  1755. tooltip.style.left = canvasXY[0] + coordX[0] + ((coordX[1] - coordX[0]) / 2) - (width * 0.1) + 'px';
  1756. tooltip.style.top = canvasXY[1] + coordY[2] - height - 5 + 'px';
  1757. img.style.left = ((width * 0.1) - 8.5) + 'px';
  1758. } else {
  1759. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.1)) + 'px';
  1760. tooltip.style.top = canvasXY[1] + coordY - height - 9 + 'px';
  1761. img.style.left = ((width * 0.1) - 8.5) + 'px';
  1762. }
  1763. // RIGHT edge //////////////////////////////////////////////////////////////////
  1764. } else if ((canvasXY[0] + (coordX[0] || coordX) + (width / 2)) > document.body.offsetWidth) {
  1765. if (isBoxplot) {
  1766. tooltip.style.left = canvasXY[0] + coordX[0] + ((coordX[1] - coordX[0]) / 2) - (width * 0.9) + 'px';
  1767. tooltip.style.top = canvasXY[1] + coordY[2] - height - 5 + 'px';
  1768. img.style.left = ((width * 0.9) - 8.5) + 'px';
  1769. } else {
  1770. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.9)) + 'px';
  1771. tooltip.style.top = canvasXY[1] + coordY - height - 9 + 'px';
  1772. img.style.left = ((width * 0.9) - 8.5) + 'px';
  1773. }
  1774. // Default positioning - CENTERED //////////////////////////////////////////////////////////////////
  1775. } else {
  1776. if (isBoxplot) {
  1777. tooltip.style.left = canvasXY[0] + coordX[0] + ((coordX[1] - coordX[0]) / 2) - (width / 2) + 'px';
  1778. tooltip.style.top = canvasXY[1] + coordY[2] - height - 5 + 'px';
  1779. img.style.left = ((width * 0.5) - 8.5) + 'px';
  1780. } else {
  1781. tooltip.style.left = (canvasXY[0] + coordX - (width * 0.5)) + 'px';
  1782. tooltip.style.top = canvasXY[1] + coordY - height - 9 + 'px';
  1783. img.style.left = ((width * 0.5) - 8.5) + 'px';
  1784. }
  1785. }
  1786. }
  1787. /**
  1788. * Returns the applicable Y COORDINATE when given a Y value
  1789. *
  1790. * @param int value The value to use
  1791. * @return int The appropriate Y coordinate
  1792. */
  1793. this.getYCoord =
  1794. this.getYCoordFromValue = function (value)
  1795. {
  1796. if (typeof(value) != 'number') {
  1797. return null;
  1798. }
  1799. var invert = prop['chart.ylabels.invert'];
  1800. var xaxispos = prop['chart.xaxispos'];
  1801. var graphHeight = ca.height - this.gutterTop - this.gutterBottom;
  1802. var halfGraphHeight = graphHeight / 2;
  1803. var ymax = this.max;
  1804. var ymin = prop['chart.ymin'];
  1805. var coord = 0;
  1806. if (value > ymax || (prop['chart.xaxispos'] == 'bottom' && value < ymin) || (prop['chart.xaxispos'] == 'center' && ((value > 0 && value < ymin) || (value < 0 && value > (-1 * ymin))))) {
  1807. return null;
  1808. }
  1809. /**
  1810. * This calculates scale values if the X axis is in the center
  1811. */
  1812. if (xaxispos == 'center') {
  1813. coord = ((Math.abs(value) - ymin) / (ymax - ymin)) * halfGraphHeight;
  1814. if (invert) {
  1815. coord = halfGraphHeight - coord;
  1816. }
  1817. if (value < 0) {
  1818. coord += this.gutterTop;
  1819. coord += halfGraphHeight;
  1820. } else {
  1821. coord = halfGraphHeight - coord;
  1822. coord += this.gutterTop;
  1823. }
  1824. /**
  1825. * And this calculates scale values when the X axis is at the bottom
  1826. */
  1827. } else {
  1828. coord = ((value - ymin) / (ymax - ymin)) * graphHeight;
  1829. if (invert) {
  1830. coord = graphHeight - coord;
  1831. }
  1832. // Invert the coordinate because the Y scale starts at the top
  1833. coord = graphHeight - coord;
  1834. // And add on the top gutter
  1835. coord = this.gutterTop + coord;
  1836. }
  1837. return coord;
  1838. }
  1839. /**
  1840. * A helper class that helps facilitatesbubble charts
  1841. */
  1842. RG.Scatter.Bubble = function (scatter, min, max, width, data)
  1843. {
  1844. this.scatter = scatter;
  1845. this.min = min;
  1846. this.max = max;
  1847. this.width = width;
  1848. this.data = data;
  1849. /**
  1850. * A setter for the Bubble chart class - it just acts as a "passthru" to the Scatter object
  1851. */
  1852. this.Set = function (name, value)
  1853. {
  1854. this.scatter.Set(name, value);
  1855. return this;
  1856. }
  1857. /**
  1858. * A getter for the Bubble chart class - it just acts as a "passthru" to the Scatter object
  1859. */
  1860. this.Get = function (name)
  1861. {
  1862. this.scatter.Get(name);
  1863. }
  1864. /**
  1865. * Tha Bubble chart draw function
  1866. */
  1867. this.Draw = function ()
  1868. {
  1869. var bubble_min = this.min;
  1870. var bubble_max = this.max;
  1871. var bubble_data = this.data;
  1872. var bubble_max_width = this.width;
  1873. // This custom ondraw event listener draws the bubbles
  1874. this.scatter.ondraw = function (obj)
  1875. {
  1876. // Loop through all the points (first dataset)
  1877. for (var i=0; i<obj.coords[0].length; ++i) {
  1878. bubble_data[i] = Math.max(bubble_data[i], bubble_min);
  1879. bubble_data[i] = Math.min(bubble_data[i], bubble_max);
  1880. var r = ((bubble_data[i] - bubble_min) / (bubble_max - bubble_min) ) * bubble_max_width;
  1881. co.beginPath();
  1882. co.fillStyle = RG.RadialGradient(obj,
  1883. obj.coords[0][i][0] + (r / 2.5),
  1884. obj.coords[0][i][1] - (r / 2.5),
  1885. 0,
  1886. obj.coords[0][i][0] + (r / 2.5),
  1887. obj.coords[0][i][1] - (r / 2.5),
  1888. 50,
  1889. 'white',
  1890. obj.data[0][i][2] ? obj.data[0][i][2] : obj.properties['chart.defaultcolor']
  1891. );
  1892. co.arc(obj.coords[0][i][0], obj.coords[0][i][1], r, 0, TWOPI, false);
  1893. co.fill();
  1894. }
  1895. }
  1896. return this.scatter.Draw();
  1897. }
  1898. }
  1899. /**
  1900. * This allows for easy specification of gradients
  1901. */
  1902. this.parseColors = function ()
  1903. {
  1904. // Colors
  1905. var data = this.data;
  1906. if (data) {
  1907. for (var dataset=0; dataset<data.length; ++dataset) {
  1908. for (var i=0; i<this.data[dataset].length; ++i) {
  1909. // Boxplots
  1910. if (typeof(this.data[dataset][i][1]) == 'object' && this.data[dataset][i][1]) {
  1911. if (typeof(this.data[dataset][i][1][5]) == 'string') this.data[dataset][i][1][5] = this.parseSingleColorForGradient(this.data[dataset][i][1][5]);
  1912. if (typeof(this.data[dataset][i][1][6]) == 'string') this.data[dataset][i][1][6] = this.parseSingleColorForGradient(this.data[dataset][i][1][6]);
  1913. }
  1914. this.data[dataset][i][2] = this.parseSingleColorForGradient(this.data[dataset][i][2]);
  1915. }
  1916. }
  1917. }
  1918. // Parse HBars
  1919. var hbars = prop['chart.background.hbars'];
  1920. if (hbars) {
  1921. for (i=0; i<hbars.length; ++i) {
  1922. hbars[i][2] = this.parseSingleColorForGradient(hbars[i][2]);
  1923. }
  1924. }
  1925. // Parse HBars
  1926. var vbars = prop['chart.background.vbars'];
  1927. if (vbars) {
  1928. for (i=0; i<vbars.length; ++i) {
  1929. vbars[i][2] = this.parseSingleColorForGradient(vbars[i][2]);
  1930. }
  1931. }
  1932. // Parse line colors
  1933. var colors = prop['chart.line.colors'];
  1934. if (colors) {
  1935. for (i=0; i<colors.length; ++i) {
  1936. colors[i] = this.parseSingleColorForGradient(colors[i]);
  1937. }
  1938. }
  1939. prop['chart.defaultcolor'] = this.parseSingleColorForGradient(prop['chart.defaultcolor']);
  1940. prop['chart.crosshairs.color'] = this.parseSingleColorForGradient(prop['chart.crosshairs.color']);
  1941. prop['chart.highlight.stroke'] = this.parseSingleColorForGradient(prop['chart.highlight.stroke']);
  1942. prop['chart.highlight.fill'] = this.parseSingleColorForGradient(prop['chart.highlight.fill']);
  1943. prop['chart.background.barcolor1'] = this.parseSingleColorForGradient(prop['chart.background.barcolor1']);
  1944. prop['chart.background.barcolor2'] = this.parseSingleColorForGradient(prop['chart.background.barcolor2']);
  1945. prop['chart.background.grid.color'] = this.parseSingleColorForGradient(prop['chart.background.grid.color']);
  1946. prop['chart.axis.color'] = this.parseSingleColorForGradient(prop['chart.axis.color']);
  1947. }
  1948. /**
  1949. * This parses a single color value for a gradient
  1950. */
  1951. this.parseSingleColorForGradient = function (color)
  1952. {
  1953. if (!color || typeof(color) != 'string') {
  1954. return color;
  1955. }
  1956. if (color.match(/^gradient\((.*)\)$/i)) {
  1957. var parts = RegExp.$1.split(':');
  1958. // Create the gradient
  1959. var grad = co.createLinearGradient(0,ca.height - prop['chart.gutter.bottom'], 0, prop['chart.gutter.top']);
  1960. var diff = 1 / (parts.length - 1);
  1961. grad.addColorStop(0, RG.trim(parts[0]));
  1962. for (var j=1; j<parts.length; ++j) {
  1963. grad.addColorStop(j * diff, RG.trim(parts[j]));
  1964. }
  1965. }
  1966. return grad ? grad : color;
  1967. }
  1968. /**
  1969. * This function handles highlighting an entire data-series for the interactive
  1970. * key
  1971. *
  1972. * @param int index The index of the data series to be highlighted
  1973. */
  1974. this.interactiveKeyHighlight = function (index)
  1975. {
  1976. if (this.coords && this.coords[index] && this.coords[index].length) {
  1977. this.coords[index].forEach(function (value, idx, arr)
  1978. {
  1979. co.beginPath();
  1980. co.fillStyle = prop['chart.key.interactive.highlight.chart.fill'];
  1981. co.arc(value[0], value[1], prop['chart.ticksize'] + 3, 0, TWOPI, false);
  1982. co.fill();
  1983. });
  1984. }
  1985. }
  1986. /**
  1987. * Register the object
  1988. */
  1989. RG.Register(this);
  1990. }