RGraph.common.core.js 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936
  1. /**
  2. * o------------------------------------------------------------------------------o
  3. * | This file is part of the RGraph package - you can learn more at: |
  4. * | |
  5. * | http://www.rgraph.net |
  6. * | |
  7. * | This package is licensed under the RGraph license. For all kinds of business |
  8. * | purposes there is a small one-time licensing fee to pay and for non |
  9. * | commercial purposes it is free to use. You can read the full license here: |
  10. * | |
  11. * | http://www.rgraph.net/license |
  12. * o------------------------------------------------------------------------------o
  13. */
  14. /**
  15. * Initialise the various objects
  16. */
  17. if (typeof(RGraph) == 'undefined') RGraph = {isRGraph:true,type:'common'};
  18. RGraph.Highlight = {};
  19. RGraph.Registry = {};
  20. RGraph.Registry.store = [];
  21. RGraph.Registry.store['chart.event.handlers'] = [];
  22. RGraph.Registry.store['__rgraph_event_listeners__'] = []; // Used in the new system for tooltips
  23. RGraph.background = {};
  24. RGraph.objects = [];
  25. RGraph.Resizing = {};
  26. RGraph.events = [];
  27. RGraph.cursor = [];
  28. RGraph.ObjectRegistry = {};
  29. RGraph.ObjectRegistry.objects = {};
  30. RGraph.ObjectRegistry.objects.byUID = [];
  31. RGraph.ObjectRegistry.objects.byCanvasID = [];
  32. /**
  33. * Some "constants"
  34. */
  35. PI = Math.PI;
  36. HALFPI = PI / 2;
  37. TWOPI = PI * 2;
  38. ISFF = navigator.userAgent.indexOf('Firefox') != -1;
  39. ISOPERA = navigator.userAgent.indexOf('Opera') != -1;
  40. ISCHROME = navigator.userAgent.indexOf('Chrome') != -1;
  41. ISSAFARI = navigator.userAgent.indexOf('Safari') != -1 && !ISCHROME;
  42. ISWEBKIT = navigator.userAgent.indexOf('WebKit') != -1;
  43. //ISIE is defined below
  44. //ISIE6 is defined below
  45. //ISIE7 is defined below
  46. //ISIE8 is defined below
  47. //ISIE9 is defined below
  48. //ISIE9 is defined below
  49. //ISIE9UP is defined below
  50. //ISIE10 is defined below
  51. //ISIE10UP is defined below
  52. //ISOLD is defined below
  53. /**
  54. * Returns five values which are used as a nice scale
  55. *
  56. * @param max int The maximum value of the graph
  57. * @param obj object The graph object
  58. * @return array An appropriate scale
  59. */
  60. RGraph.getScale = function (max, obj)
  61. {
  62. /**
  63. * Special case for 0
  64. */
  65. if (max == 0) {
  66. return ['0.2', '0.4', '0.6', '0.8', '1.0'];
  67. }
  68. var original_max = max;
  69. /**
  70. * Manually do decimals
  71. */
  72. if (max <= 1) {
  73. if (max > 0.5) {
  74. return [0.2,0.4,0.6,0.8, Number(1).toFixed(1)];
  75. } else if (max >= 0.1) {
  76. return obj.Get('chart.scale.round') ? [0.2,0.4,0.6,0.8,1] : [0.1,0.2,0.3,0.4,0.5];
  77. } else {
  78. var tmp = max;
  79. var exp = 0;
  80. while (tmp < 1.01) {
  81. exp += 1;
  82. tmp *= 10;
  83. }
  84. var ret = ['2e-' + exp, '4e-' + exp, '6e-' + exp, '8e-' + exp, '10e-' + exp];
  85. if (max <= ('5e-' + exp)) {
  86. ret = ['1e-' + exp, '2e-' + exp, '3e-' + exp, '4e-' + exp, '5e-' + exp];
  87. }
  88. return ret;
  89. }
  90. }
  91. // Take off any decimals
  92. if (String(max).indexOf('.') > 0) {
  93. max = String(max).replace(/\.\d+$/, '');
  94. }
  95. var interval = Math.pow(10, Number(String(Number(max)).length - 1));
  96. var topValue = interval;
  97. while (topValue < max) {
  98. topValue += (interval / 2);
  99. }
  100. // Handles cases where the max is (for example) 50.5
  101. if (Number(original_max) > Number(topValue)) {
  102. topValue += (interval / 2);
  103. }
  104. // Custom if the max is greater than 5 and less than 10
  105. if (max < 10) {
  106. topValue = (Number(original_max) <= 5 ? 5 : 10);
  107. }
  108. /**
  109. * Added 02/11/2010 to create "nicer" scales
  110. */
  111. if (obj && typeof(obj.Get('chart.scale.round')) == 'boolean' && obj.Get('chart.scale.round')) {
  112. topValue = 10 * interval;
  113. }
  114. return [topValue * 0.2, topValue * 0.4, topValue * 0.6, topValue * 0.8, topValue];
  115. }
  116. /**
  117. * Returns an appropriate scale. The return value is actualy anm object consiosting of:
  118. * scale.max
  119. * scale.min
  120. * scale.scale
  121. *
  122. * @param obj object The graph object
  123. * @param prop object An object consisting of configuration properties
  124. * @return object An object containg scale information
  125. */
  126. RGraph.getScale2 = function (obj, opt)
  127. {
  128. var RG = RGraph;
  129. var ca = obj.canvas;
  130. var co = obj.context;
  131. var prop = obj.properties;
  132. var numlabels = typeof(opt['ylabels.count']) == 'number' ? opt['ylabels.count'] : 5;
  133. var units_pre = typeof(opt['units.pre']) == 'string' ? opt['units.pre'] : '';
  134. var units_post = typeof(opt['units.post']) == 'string' ? opt['units.post'] : '';
  135. var max = Number(opt['max']);
  136. var min = typeof(opt['min']) == 'number' ? opt['min'] : 0;
  137. var strict = opt['strict'];
  138. var decimals = Number(opt['scale.decimals']); // Sometimes the default is null
  139. var point = opt['scale.point']; // Default is a string in all chart libraries so no need to cast it
  140. var thousand = opt['scale.thousand']; // Default is a string in all chart libraries so no need to cast it
  141. var original_max = max;
  142. var round = opt['scale.round'];
  143. var scale = {'max':1,'labels':[]};
  144. /**
  145. * Special case for 0
  146. *
  147. * ** Must be first **
  148. */
  149. if (!max) {
  150. var max = 1;
  151. var scale = {max:1,min:0,labels:[]};
  152. for (var i=0; i<numlabels; ++i) {
  153. var label = ((((max - min) / numlabels) + min) * (i + 1)).toFixed(decimals);
  154. scale.labels.push(units_pre + label + units_post);
  155. }
  156. /**
  157. * Manually do decimals
  158. */
  159. } else if (max <= 1 && !strict) {
  160. if (max > 0.5) {
  161. max = 1;
  162. min = min;
  163. scale.min = min;
  164. for (var i=0; i<numlabels; ++i) {
  165. var label = ((((max - min) / numlabels) * (i + 1)) + min).toFixed(decimals);
  166. scale.labels.push(units_pre + label + units_post);
  167. }
  168. } else if (max >= 0.1) {
  169. max = 0.5;
  170. min = min;
  171. scale = {'max': 0.5, 'min':min,'labels':[]}
  172. for (var i=0; i<numlabels; ++i) {
  173. var label = ((((max - min) / numlabels) + min) * (i + 1)).toFixed(decimals);
  174. scale.labels.push(units_pre + label + units_post);
  175. }
  176. } else {
  177. scale = {'min':min,'labels':[]}
  178. var max_str = String(max);
  179. if (max_str.indexOf('e') > 0) {
  180. var numdecimals = Math.abs(max_str.substring(max_str.indexOf('e') + 1));
  181. } else {
  182. var numdecimals = String(max).length - 2;
  183. }
  184. var max = 1 / Math.pow(10,numdecimals - 1);
  185. for (var i=0; i<numlabels; ++i) {
  186. var label = ((((max - min) / numlabels) + min) * (i + 1));
  187. label = label.toExponential();
  188. label = label.split(/e/);
  189. label[0] = Math.round(label[0]);
  190. label = label.join('e');
  191. scale.labels.push(label);
  192. }
  193. //This makes the top scale value of the format 10e-2 instead of 1e-1
  194. tmp = scale.labels[scale.labels.length - 1].split(/e/);
  195. tmp[0] += 0;
  196. tmp[1] = Number(tmp[1]) - 1;
  197. tmp = tmp[0] + 'e' + tmp[1];
  198. scale.labels[scale.labels.length - 1] = tmp;
  199. // Add the units
  200. for (var i=0; i<scale.labels.length ; ++i) {
  201. scale.labels[i] = units_pre + scale.labels[i] + units_post;
  202. }
  203. scale.max = Number(max);
  204. }
  205. } else if (!strict) {
  206. /**
  207. * Now comes the scale handling for integer values
  208. */
  209. // This accomodates decimals by rounding the max up to the next integer
  210. max = Math.ceil(max);
  211. var interval = Math.pow(10, Math.max(1, Number(String(Number(max) - Number(min)).length - 1)) );
  212. var topValue = interval;
  213. while (topValue < max) {
  214. topValue += (interval / 2);
  215. }
  216. // Handles cases where the max is (for example) 50.5
  217. if (Number(original_max) > Number(topValue)) {
  218. topValue += (interval / 2);
  219. }
  220. // Custom if the max is greater than 5 and less than 10
  221. if (max <= 10) {
  222. topValue = (Number(original_max) <= 5 ? 5 : 10);
  223. }
  224. // Added 02/11/2010 to create "nicer" scales
  225. if (obj && typeof(round) == 'boolean' && round) {
  226. topValue = 10 * interval;
  227. }
  228. scale.max = topValue;
  229. // Now generate the scale. Temporarily set the objects chart.scale.decimal and chart.scale.point to those
  230. //that we've been given as the number_format functuion looks at those instead of using argumrnts.
  231. var tmp_point = prop['chart.scale.point'];
  232. var tmp_thousand = prop['chart.scale.thousand'];
  233. obj.Set('chart.scale.thousand', thousand);
  234. obj.Set('chart.scale.point', point);
  235. for (var i=0; i<numlabels; ++i) {
  236. scale.labels.push( RG.number_format(obj, ((((i+1) / numlabels) * (topValue - min)) + min).toFixed(decimals), units_pre, units_post) );
  237. }
  238. obj.Set('chart.scale.thousand', tmp_thousand);
  239. obj.Set('chart.scale.point', tmp_point);
  240. } else if (typeof(max) == 'number' && strict) {
  241. /**
  242. * ymax is set and also strict
  243. */
  244. for (var i=0; i<numlabels; ++i) {
  245. scale.labels.push( RG.number_format(obj, ((((i+1) / numlabels) * (max - min)) + min).toFixed(decimals), units_pre, units_post) );
  246. }
  247. // ???
  248. scale.max = max;
  249. }
  250. scale.units_pre = units_pre;
  251. scale.units_post = units_post;
  252. scale.point = point;
  253. scale.decimals = decimals;
  254. scale.thousand = thousand;
  255. scale.numlabels = numlabels;
  256. scale.round = Boolean(round);
  257. scale.min = min;
  258. return scale;
  259. }
  260. /**
  261. * Returns the maximum numeric value which is in an array
  262. *
  263. * @param array arr The array (can also be a number, in which case it's returned as-is)
  264. * @param int Whether to ignore signs (ie negative/positive)
  265. * @return int The maximum value in the array
  266. */
  267. RGraph.array_max = function (arr)
  268. {
  269. var max = null;
  270. var MathLocal = Math;
  271. if (typeof(arr) == 'number') {
  272. return arr;
  273. }
  274. if (RGraph.is_null(arr)) {
  275. return 0;
  276. }
  277. for (var i=0,len=arr.length; i<len; ++i) {
  278. if (typeof(arr[i]) == 'number') {
  279. var val = arguments[1] ? MathLocal.abs(arr[i]) : arr[i];
  280. if (typeof max == 'number') {
  281. max = MathLocal.max(max, val);
  282. } else {
  283. max = val;
  284. }
  285. }
  286. }
  287. return max;
  288. }
  289. /**
  290. * Returns the maximum value which is in an array
  291. *
  292. * @param array arr The array
  293. * @param int len The length to pad the array to
  294. * @param mixed The value to use to pad the array (optional)
  295. */
  296. RGraph.array_pad = function (arr, len)
  297. {
  298. if (arr.length < len) {
  299. var val = arguments[2] ? arguments[2] : null;
  300. for (var i=arr.length; i<len; i+=1) {
  301. arr[i] = val;
  302. }
  303. }
  304. return arr;
  305. }
  306. /**
  307. * An array sum function
  308. *
  309. * @param array arr The array to calculate the total of
  310. * @return int The summed total of the arrays elements
  311. */
  312. RGraph.array_sum = function (arr)
  313. {
  314. // Allow integers
  315. if (typeof(arr) == 'number') {
  316. return arr;
  317. }
  318. // Account for null
  319. if (RGraph.is_null(arr)) {
  320. return 0;
  321. }
  322. var i, sum;
  323. var len = arr.length;
  324. for(i=0,sum=0;i<len;sum+=arr[i++]);
  325. return sum;
  326. }
  327. /**
  328. * Takes any number of arguments and adds them to one big linear array
  329. * which is then returned
  330. *
  331. * @param ... mixed The data to linearise. You can strings, booleans, numbers or arrays
  332. */
  333. RGraph.array_linearize = function ()
  334. {
  335. var arr = [];
  336. var args = arguments;
  337. var RG = RGraph;
  338. for (var i=0,len=args.length; i<len; ++i) {
  339. if (typeof(args[i]) == 'object' && args[i]) {
  340. for (var j=0; j<args[i].length; ++j) {
  341. var sub = RG.array_linearize(args[i][j]);
  342. for (var k=0; k<sub.length; ++k) {
  343. arr.push(sub[k]);
  344. }
  345. }
  346. } else {
  347. arr.push(args[i]);
  348. }
  349. }
  350. return arr;
  351. }
  352. /**
  353. * This is a useful function which is basically a shortcut for drawing left, right, top and bottom alligned text.
  354. *
  355. * @param object context The context
  356. * @param string font The font
  357. * @param int size The size of the text
  358. * @param int x The X coordinate
  359. * @param int y The Y coordinate
  360. * @param string text The text to draw
  361. * @parm string The vertical alignment. Can be null. "center" gives center aligned text, "top" gives top aligned text.
  362. * Anything else produces bottom aligned text. Default is bottom.
  363. * @param string The horizontal alignment. Can be null. "center" gives center aligned text, "right" gives right aligned text.
  364. * Anything else produces left aligned text. Default is left.
  365. * @param bool Whether to show a bounding box around the text. Defaults not to
  366. * @param int The angle that the text should be rotate at (IN DEGREES)
  367. * @param string Background color for the text
  368. * @param bool Whether the text is bold or not
  369. */
  370. RGraph.Text = function (context, font, size, x, y, text)
  371. {
  372. // "Cache" the args as a local variable
  373. var args = arguments;
  374. // Handle undefined - change it to an empty string
  375. if ((typeof(text) != 'string' && typeof(text) != 'number') || text == 'undefined') {
  376. return;
  377. }
  378. /**
  379. * This accommodates multi-line text
  380. */
  381. if (typeof(text) == 'string' && text.match(/\r\n/)) {
  382. var dimensions = RGraph.MeasureText('M', args[11], font, size);
  383. /**
  384. * Measure the text (width and height)
  385. */
  386. var arr = text.split('\r\n');
  387. /**
  388. * Adjust the Y position
  389. */
  390. // This adjusts the initial y position
  391. if (args[6] && args[6] == 'center') y = (y - (dimensions[1] * ((arr.length - 1) / 2)));
  392. for (var i=1; i<arr.length; ++i) {
  393. RGraph.Text(context,
  394. font,
  395. size,
  396. args[9] == -90 ? (x + (size * 1.5)) : x,
  397. y + (dimensions[1] * i),
  398. arr[i],
  399. args[6] ? args[6] : null,
  400. args[7],
  401. args[8],
  402. args[9],
  403. args[10],
  404. args[11],
  405. args[12]);
  406. }
  407. // Update text to just be the first line
  408. text = arr[0];
  409. }
  410. // Accommodate MSIE
  411. if (document.all && ISOLD) {
  412. y += 2;
  413. }
  414. context.font = (args[11] ? 'Bold ': '') + size + 'pt ' + font;
  415. var i;
  416. var origX = x;
  417. var origY = y;
  418. var originalFillStyle = context.fillStyle;
  419. var originalLineWidth = context.lineWidth;
  420. // Need these now the angle can be specified, ie defaults for the former two args
  421. if (typeof(args[6]) == 'undefined') args[6] = 'bottom'; // Vertical alignment. Default to bottom/baseline
  422. if (typeof(args[7]) == 'undefined') args[7] = 'left'; // Horizontal alignment. Default to left
  423. if (typeof(args[8]) == 'undefined') args[8] = null; // Show a bounding box. Useful for positioning during development. Defaults to false
  424. if (typeof(args[9]) == 'undefined') args[9] = 0; // Angle (IN DEGREES) that the text should be drawn at. 0 is middle right, and it goes clockwise
  425. // The alignment is recorded here for purposes of Opera compatibility
  426. if (navigator.userAgent.indexOf('Opera') != -1) {
  427. context.canvas.__rgraph_valign__ = args[6];
  428. context.canvas.__rgraph_halign__ = args[7];
  429. }
  430. // First, translate to x/y coords
  431. context.save();
  432. context.canvas.__rgraph_originalx__ = x;
  433. context.canvas.__rgraph_originaly__ = y;
  434. context.translate(x, y);
  435. x = 0;
  436. y = 0;
  437. // Rotate the canvas if need be
  438. if (args[9]) {
  439. context.rotate(args[9] / (180 / PI));
  440. }
  441. // Vertical alignment - defaults to bottom
  442. if (args[6]) {
  443. var vAlign = args[6];
  444. if (vAlign == 'center') {
  445. context.textBaseline = 'middle';
  446. } else if (vAlign == 'top') {
  447. context.textBaseline = 'top';
  448. }
  449. }
  450. // Hoeizontal alignment - defaults to left
  451. if (args[7]) {
  452. var hAlign = args[7];
  453. var width = context.measureText(text).width;
  454. if (hAlign) {
  455. if (hAlign == 'center') {
  456. context.textAlign = 'center';
  457. } else if (hAlign == 'right') {
  458. context.textAlign = 'right';
  459. }
  460. }
  461. }
  462. context.fillStyle = originalFillStyle;
  463. /**
  464. * Draw a bounding box if requested
  465. */
  466. context.save();
  467. context.fillText(text,0,0);
  468. context.lineWidth = 1;
  469. var width = context.measureText(text).width;
  470. var width_offset = (hAlign == 'center' ? (width / 2) : (hAlign == 'right' ? width : 0));
  471. var height = size * 1.5; // !!!
  472. var height_offset = (vAlign == 'center' ? (height / 2) : (vAlign == 'top' ? height : 0));
  473. var ieOffset = ISOLD ? 2 : 0;
  474. if (args[8]) {
  475. context.strokeRect(-3 - width_offset,
  476. 0 - 3 - height - ieOffset + height_offset,
  477. width + 6,
  478. height + 6);
  479. /**
  480. * If requested, draw a background for the text
  481. */
  482. if (args[10]) {
  483. context.fillStyle = args[10];
  484. context.fillRect(-3 - width_offset,
  485. 0 - 3 - height - ieOffset + height_offset,
  486. width + 6,
  487. height + 6);
  488. }
  489. context.fillStyle = originalFillStyle;
  490. /**
  491. * Do the actual drawing of the text
  492. */
  493. context.fillText(text,0,0);
  494. }
  495. context.restore();
  496. // Reset the lineWidth
  497. context.lineWidth = originalLineWidth;
  498. context.restore();
  499. }
  500. /**
  501. * Clears the canvas by setting the width. You can specify a colour if you wish.
  502. *
  503. * @param object canvas The canvas to clear
  504. */
  505. RGraph.Clear = function (ca)
  506. {
  507. var RG = RGraph;
  508. var co = ca.getContext('2d');
  509. var color = arguments[1];
  510. if (!ca) {
  511. return;
  512. }
  513. RG.FireCustomEvent(ca.__object__, 'onbeforeclear');
  514. if (ISIE8 && !color) {
  515. color = 'white';
  516. }
  517. /**
  518. * Can now clear the canvas back to fully transparent
  519. */
  520. if (!color || (color && color == 'rgba(0,0,0,0)' || color == 'transparent')) {
  521. co.clearRect(0,0,ca.width, ca.height);
  522. // Reset the globalCompositeOperation
  523. co.globalCompositeOperation = 'source-over';
  524. } else {
  525. co.fillStyle = color;
  526. co.beginPath();
  527. if (ISIE8) {
  528. co.fillRect(0,0,ca.width,ca.height);
  529. } else {
  530. co.fillRect(-10,-10,ca.width + 20,ca.height + 20);
  531. }
  532. co.fill();
  533. }
  534. //if (RG.ClearAnnotations) {
  535. //RG.ClearAnnotations(ca.id);
  536. //}
  537. /**
  538. * This removes any background image that may be present
  539. */
  540. if (RG.Registry.Get('chart.background.image.' + ca.id)) {
  541. var img = RG.Registry.Get('chart.background.image.' + ca.id);
  542. img.style.position = 'absolute';
  543. img.style.left = '-10000px';
  544. img.style.top = '-10000px';
  545. }
  546. /**
  547. * This hides the tooltip that is showing IF it has the same canvas ID as
  548. * that which is being cleared
  549. */
  550. if (RG.Registry.Get('chart.tooltip')) {
  551. RG.HideTooltip(ca);
  552. //RG.Redraw();
  553. }
  554. /**
  555. * Set the cursor to default
  556. */
  557. ca.style.cursor = 'default';
  558. RG.FireCustomEvent(ca.__object__, 'onclear');
  559. }
  560. /**
  561. * Draws the title of the graph
  562. *
  563. * @param object canvas The canvas object
  564. * @param string text The title to write
  565. * @param integer gutter The size of the gutter
  566. * @param integer The center X point (optional - if not given it will be generated from the canvas width)
  567. * @param integer Size of the text. If not given it will be 14
  568. */
  569. RGraph.DrawTitle = function (obj, text, gutterTop)
  570. {
  571. var RG = RGraph;
  572. var ca = canvas = obj.canvas;
  573. var co = context = obj.context;
  574. var prop = obj.properties;
  575. var gutterLeft = prop['chart.gutter.left'];
  576. var gutterRight = prop['chart.gutter.right'];
  577. var gutterTop = gutterTop;
  578. var gutterBottom = prop['chart.gutter.bottom'];
  579. var size = arguments[4] ? arguments[4] : 12;
  580. var bold = prop['chart.title.bold'];
  581. var centerx = (arguments[3] ? arguments[3] : ((ca.width - gutterLeft - gutterRight) / 2) + gutterLeft);
  582. var keypos = prop['chart.key.position'];
  583. var vpos = prop['chart.title.vpos'];
  584. var hpos = prop['chart.title.hpos'];
  585. var bgcolor = prop['chart.title.background'];
  586. var x = prop['chart.title.x'];
  587. var y = prop['chart.title.y'];
  588. var halign = 'center';
  589. var valign = 'center';
  590. // Account for 3D effect by faking the key position
  591. if (obj.type == 'bar' && prop['chart.variant'] == '3d') {
  592. keypos = 'gutter';
  593. }
  594. co.beginPath();
  595. co.fillStyle = prop['chart.text.color'] ? prop['chart.text.color'] : 'black';
  596. /**
  597. * Vertically center the text if the key is not present
  598. */
  599. if (keypos && keypos != 'gutter') {
  600. var valign = 'center';
  601. } else if (!keypos) {
  602. var valign = 'center';
  603. } else {
  604. var valign = 'bottom';
  605. }
  606. // if chart.title.vpos is a number, use that
  607. if (typeof(prop['chart.title.vpos']) == 'number') {
  608. vpos = prop['chart.title.vpos'] * gutterTop;
  609. if (prop['chart.xaxispos'] == 'top') {
  610. vpos = prop['chart.title.vpos'] * gutterBottom + gutterTop + (ca.height - gutterTop - gutterBottom);
  611. }
  612. } else {
  613. vpos = gutterTop - size - 5;
  614. if (prop['chart.xaxispos'] == 'top') {
  615. vpos = ca.height - gutterBottom + size + 5;
  616. }
  617. }
  618. // if chart.title.hpos is a number, use that. It's multiplied with the (entire) canvas width
  619. if (typeof(hpos) == 'number') {
  620. centerx = hpos * ca.width;
  621. }
  622. /**
  623. * Now the chart.title.x and chart.title.y settings override (is set) the above
  624. */
  625. if (typeof(x) == 'number') centerx = x;
  626. if (typeof(y) == 'number') vpos = y;
  627. /**
  628. * Horizontal alignment can now (Jan 2013) be specified
  629. */
  630. if (typeof(prop['chart.title.halign']) == 'string') {
  631. halign = prop['chart.title.halign'];
  632. }
  633. /**
  634. * Vertical alignment can now (Jan 2013) be specified
  635. */
  636. if (typeof(prop['chart.title.valign']) == 'string') {
  637. valign = prop['chart.title.valign'];
  638. }
  639. // Set the colour
  640. if (typeof(prop['chart.title.color'] != null)) {
  641. var oldColor = co.fillStyle
  642. var newColor = prop['chart.title.color']
  643. co.fillStyle = newColor ? newColor : 'black';
  644. }
  645. /**
  646. * Default font is Arial
  647. */
  648. var font = prop['chart.text.font'];
  649. /**
  650. * Override the default font with chart.title.font
  651. */
  652. if (typeof(prop['chart.title.font']) == 'string') {
  653. font = prop['chart.title.font'];
  654. }
  655. /**
  656. * Draw the title
  657. */
  658. RG.Text2(obj,{'font':font,
  659. 'size':size,
  660. 'x':centerx,
  661. 'y':vpos,
  662. 'text':text,
  663. 'valign':valign,
  664. 'halign':halign,
  665. 'bounding':bgcolor != null,
  666. 'bounding.fill':bgcolor,
  667. 'bold':bold,
  668. 'tag':'title'
  669. });
  670. // Reset the fill colour
  671. co.fillStyle = oldColor;
  672. }
  673. /**
  674. * This function returns the mouse position in relation to the canvas
  675. *
  676. * @param object e The event object.
  677. *
  678. RGraph.getMouseXY = function (e)
  679. {
  680. var el = (ISOLD ? event.srcElement : e.target);
  681. var x;
  682. var y;
  683. // ???
  684. var paddingLeft = el.style.paddingLeft ? parseInt(el.style.paddingLeft) : 0;
  685. var paddingTop = el.style.paddingTop ? parseInt(el.style.paddingTop) : 0;
  686. var borderLeft = el.style.borderLeftWidth ? parseInt(el.style.borderLeftWidth) : 0;
  687. var borderTop = el.style.borderTopWidth ? parseInt(el.style.borderTopWidth) : 0;
  688. if (ISIE8) e = event;
  689. // Browser with offsetX and offsetY
  690. if (typeof(e.offsetX) == 'number' && typeof(e.offsetY) == 'number') {
  691. x = e.offsetX;
  692. y = e.offsetY;
  693. // FF and other
  694. } else {
  695. x = 0;
  696. y = 0;
  697. while (el != document.body && el) {
  698. x += el.offsetLeft;
  699. y += el.offsetTop;
  700. el = el.offsetParent;
  701. }
  702. x = e.pageX - x;
  703. y = e.pageY - y;
  704. }
  705. return [x, y];
  706. }*/
  707. RGraph.getMouseXY = function(e)
  708. {
  709. var el = e.target;
  710. var ca = el;
  711. var caStyle = ca.style;
  712. var offsetX = 0;
  713. var offsetY = 0;
  714. var x;
  715. var y;
  716. var ISFIXED = (ca.style.position == 'fixed');
  717. var borderLeft = parseInt(caStyle.borderLeftWidth) || 0;
  718. var borderTop = parseInt(caStyle.borderTopWidth) || 0;
  719. var paddingLeft = parseInt(caStyle.paddingLeft) || 0
  720. var paddingTop = parseInt(caStyle.paddingTop) || 0
  721. var additionalX = borderLeft + paddingLeft;
  722. var additionalY = borderTop + paddingTop;
  723. if (typeof(e.offsetX) == 'number' && typeof(e.offsetY) == 'number') {
  724. if (ISFIXED) {
  725. if (ISOPERA) {
  726. x = e.offsetX;
  727. y = e.offsetY;
  728. } else if (ISWEBKIT) {
  729. x = e.offsetX - paddingLeft - borderLeft;
  730. y = e.offsetY - paddingTop - borderTop;
  731. } else if (ISIE) {
  732. x = e.offsetX - paddingLeft;
  733. y = e.offsetY - paddingTop;
  734. } else {
  735. x = e.offsetX;
  736. y = e.offsetY;
  737. }
  738. } else {
  739. if (!ISIE && !ISOPERA) {
  740. x = e.offsetX - borderLeft - paddingLeft;
  741. y = e.offsetY - borderTop - paddingTop;
  742. } else if (ISIE) {
  743. x = e.offsetX - paddingLeft;
  744. y = e.offsetY - paddingTop;
  745. } else {
  746. x = e.offsetX;
  747. y = e.offsetY;
  748. }
  749. }
  750. } else {
  751. if (typeof(el.offsetParent) != 'undefined') {
  752. do {
  753. offsetX += el.offsetLeft;
  754. offsetY += el.offsetTop;
  755. } while ((el = el.offsetParent));
  756. }
  757. x = e.pageX - offsetX - additionalX;
  758. y = e.pageY - offsetY - additionalY;
  759. x -= (2 * (parseInt(document.body.style.borderLeftWidth) || 0));
  760. y -= (2 * (parseInt(document.body.style.borderTopWidth) || 0));
  761. //x += (parseInt(caStyle.borderLeftWidth) || 0);
  762. //y += (parseInt(caStyle.borderTopWidth) || 0);
  763. }
  764. // We return a javascript array with x and y defined
  765. return [x, y];
  766. }
  767. /**
  768. * This function returns a two element array of the canvas x/y position in
  769. * relation to the page
  770. *
  771. * @param object canvas
  772. */
  773. RGraph.getCanvasXY = function (canvas)
  774. {
  775. var x = 0;
  776. var y = 0;
  777. var el = canvas; // !!!
  778. do {
  779. x += el.offsetLeft;
  780. y += el.offsetTop;
  781. // ACCOUNT FOR TABLES IN wEBkIT
  782. if (el.tagName.toLowerCase() == 'table' && (ISCHROME || ISSAFARI)) {
  783. x += parseInt(el.border) || 0;
  784. y += parseInt(el.border) || 0;
  785. }
  786. el = el.offsetParent;
  787. } while (el && el.tagName.toLowerCase() != 'body');
  788. var paddingLeft = canvas.style.paddingLeft ? parseInt(canvas.style.paddingLeft) : 0;
  789. var paddingTop = canvas.style.paddingTop ? parseInt(canvas.style.paddingTop) : 0;
  790. var borderLeft = canvas.style.borderLeftWidth ? parseInt(canvas.style.borderLeftWidth) : 0;
  791. var borderTop = canvas.style.borderTopWidth ? parseInt(canvas.style.borderTopWidth) : 0;
  792. if (navigator.userAgent.indexOf('Firefox') > 0) {
  793. x += parseInt(document.body.style.borderLeftWidth) || 0;
  794. y += parseInt(document.body.style.borderTopWidth) || 0;
  795. }
  796. return [x + paddingLeft + borderLeft, y + paddingTop + borderTop];
  797. }
  798. /**
  799. * This function determines whther a canvas is fixed (CSS positioning) or not. If not it returns
  800. * false. If it is then the element that is fixed is returned (it may be a parent of the canvas).
  801. *
  802. * @return Either false or the fixed positioned element
  803. */
  804. RGraph.isFixed = function (canvas)
  805. {
  806. var obj = canvas;
  807. var i = 0;
  808. while (obj && obj.tagName.toLowerCase() != 'body' && i < 99) {
  809. if (obj.style.position == 'fixed') {
  810. return obj;
  811. }
  812. obj = obj.offsetParent;
  813. }
  814. return false;
  815. }
  816. /**
  817. * Registers a graph object (used when the canvas is redrawn)
  818. *
  819. * @param object obj The object to be registered
  820. */
  821. RGraph.Register = function (obj)
  822. {
  823. // Checking this property ensures the object is only registered once
  824. if (!obj.Get('chart.noregister')) {
  825. // As of 21st/1/2012 the object registry is now used
  826. RGraph.ObjectRegistry.Add(obj);
  827. obj.Set('chart.noregister', true);
  828. }
  829. }
  830. /**
  831. * Causes all registered objects to be redrawn
  832. *
  833. * @param string An optional color to use to clear the canvas
  834. */
  835. RGraph.Redraw = function ()
  836. {
  837. var objectRegistry = RGraph.ObjectRegistry.objects.byCanvasID;
  838. // Get all of the canvas tags on the page
  839. var tags = document.getElementsByTagName('canvas');
  840. for (var i=0,len=tags.length; i<len; ++i) {
  841. if (tags[i].__object__ && tags[i].__object__.isRGraph) {
  842. // Only clear the canvas if it's not Trace'ing - this applies to the Line/Scatter Trace effects
  843. if (!tags[i].noclear) {
  844. RGraph.Clear(tags[i], arguments[0] ? arguments[0] : null);
  845. }
  846. }
  847. }
  848. // Go through the object registry and redraw *all* of the canvas'es that have been registered
  849. for (var i=0,len=objectRegistry.length; i<len; ++i) {
  850. if (objectRegistry[i]) {
  851. var id = objectRegistry[i][0];
  852. objectRegistry[i][1].Draw();
  853. }
  854. }
  855. }
  856. /**
  857. * Causes all registered objects ON THE GIVEN CANVAS to be redrawn
  858. *
  859. * @param canvas object The canvas object to redraw
  860. * @param bool Optional boolean which defaults to true and determines whether to clear the canvas
  861. */
  862. RGraph.RedrawCanvas = function (canvas)
  863. {
  864. var objects = RGraph.ObjectRegistry.getObjectsByCanvasID(canvas.id);
  865. /**
  866. * First clear the canvas
  867. */
  868. if (!arguments[1] || (typeof(arguments[1]) == 'boolean' && !arguments[1] == false) ) {
  869. // TODO This function should really support passing a color as the second optional argument - which is then used in the below
  870. // call
  871. RGraph.Clear(canvas);
  872. }
  873. /**
  874. * Now redraw all the charts associated with that canvas
  875. */
  876. for (var i=0,len=objects.length; i<len; ++i) {
  877. if (objects[i]) {
  878. if (objects[i] && objects[i].isRGraph) { // Is it an RGraph object ??
  879. objects[i].Draw();
  880. }
  881. }
  882. }
  883. }
  884. /**
  885. * This function draws the background for the bar chart, line chart and scatter chart.
  886. *
  887. * @param object obj The graph object
  888. */
  889. RGraph.background.Draw = function (obj)
  890. {
  891. var RG = RGraph;
  892. var ca = canvas = obj.canvas;
  893. var co = context = obj.context;
  894. var prop = obj.properties;
  895. var height = 0;
  896. var gutterLeft = obj.gutterLeft;
  897. var gutterRight = obj.gutterRight;
  898. var gutterTop = obj.gutterTop;
  899. var gutterBottom = obj.gutterBottom;
  900. var variant = prop['chart.variant'];
  901. co.fillStyle = prop['chart.text.color'];
  902. // If it's a bar and 3D variant, translate
  903. if (variant == '3d') {
  904. co.save();
  905. co.translate(10, -5);
  906. }
  907. // X axis title
  908. if (typeof(prop['chart.title.xaxis']) == 'string' && prop['chart.title.xaxis'].length) {
  909. var size = prop['chart.text.size'] + 2;
  910. var font = prop['chart.text.font'];
  911. var bold = prop['chart.title.xaxis.bold'];
  912. if (typeof(prop['chart.title.xaxis.size']) == 'number') {
  913. size = prop['chart.title.xaxis.size'];
  914. }
  915. if (typeof(prop['chart.title.xaxis.font']) == 'string') {
  916. font = prop['chart.title.xaxis.font'];
  917. }
  918. var hpos = ((ca.width - gutterLeft - gutterRight) / 2) + gutterLeft;
  919. var vpos = ca.height - gutterBottom + 25;
  920. if (typeof(prop['chart.title.xaxis.pos']) == 'number') {
  921. vpos = ca.height - (gutterBottom * prop['chart.title.xaxis.pos']);
  922. }
  923. // Specifically specified X/Y positions
  924. if (typeof prop['chart.title.xaxis.x'] == 'number') {
  925. hpos = prop['chart.title.xaxis.x'];
  926. }
  927. if (typeof prop['chart.title.xaxis.y'] == 'number') {
  928. vpos = prop['chart.title.xaxis.y'];
  929. }
  930. RG.Text2(obj, {'font':font,
  931. 'size':size,
  932. 'x':hpos,
  933. 'y':vpos,
  934. 'text':prop['chart.title.xaxis'],
  935. 'halign':'center',
  936. 'valign':'center',
  937. 'bold':bold,
  938. 'tag': 'title xaxis'
  939. });
  940. }
  941. // Y axis title
  942. if (typeof(prop['chart.title.yaxis']) == 'string' && prop['chart.title.yaxis'].length) {
  943. var size = prop['chart.text.size'] + 2;
  944. var font = prop['chart.text.font'];
  945. var angle = 270;
  946. var bold = prop['chart.title.yaxis.bold'];
  947. var color = prop['chart.title.yaxis.color'];
  948. if (typeof(prop['chart.title.yaxis.pos']) == 'number') {
  949. var yaxis_title_pos = prop['chart.title.yaxis.pos'] * gutterLeft;
  950. } else {
  951. var yaxis_title_pos = ((gutterLeft - 25) / gutterLeft) * gutterLeft;
  952. }
  953. if (typeof(prop['chart.title.yaxis.size']) == 'number') {
  954. size = prop['chart.title.yaxis.size'];
  955. }
  956. if (typeof(prop['chart.title.yaxis.font']) == 'string') {
  957. font = prop['chart.title.yaxis.font'];
  958. }
  959. if (prop['chart.title.yaxis.align'] == 'right' || prop['chart.title.yaxis.position'] == 'right') {
  960. angle = 90;
  961. yaxis_title_pos = prop['chart.title.yaxis.pos'] ? (ca.width - gutterRight) + (prop['chart.title.yaxis.pos'] * gutterRight) :
  962. ca.width - gutterRight + prop['chart.text.size'] + 5;
  963. } else {
  964. yaxis_title_pos = yaxis_title_pos;
  965. }
  966. var y = ((ca.height - gutterTop - gutterBottom) / 2) + gutterTop;
  967. // Specifically specified X/Y positions
  968. if (typeof prop['chart.title.yaxis.x'] == 'number') {
  969. yaxis_title_pos = prop['chart.title.yaxis.x'];
  970. }
  971. if (typeof prop['chart.title.yaxis.y'] == 'number') {
  972. y = prop['chart.title.yaxis.y'];
  973. }
  974. co.fillStyle = color;
  975. RG.Text2(obj, {'font':font,
  976. 'size':size,
  977. 'x':yaxis_title_pos,
  978. 'y':y,
  979. 'valign':'center',
  980. 'halign':'center',
  981. 'angle':angle,
  982. 'bold':bold,
  983. 'text':prop['chart.title.yaxis'],
  984. 'tag':'title yaxis'
  985. });
  986. }
  987. /**
  988. * If the background color is spec ified - draw that. It's a rectangle that fills the
  989. * entire are within the gutters
  990. */
  991. var bgcolor = prop['chart.background.color'];
  992. if (bgcolor) {
  993. co.fillStyle = bgcolor;
  994. co.fillRect(gutterLeft, gutterTop, ca.width - gutterLeft - gutterRight, ca.height - gutterTop - gutterBottom);
  995. }
  996. /**
  997. * Draw horizontal background bars
  998. */
  999. co.beginPath(); // Necessary?
  1000. co.fillStyle = prop['chart.background.barcolor1'];
  1001. co.strokeStyle = co.fillStyle;
  1002. height = (ca.height - gutterBottom);
  1003. for (var i=gutterTop; i<height ; i+=80) {
  1004. co.fillRect(gutterLeft, i, ca.width - gutterLeft - gutterRight, Math.min(40, ca.height - gutterBottom - i) );
  1005. }
  1006. co.fillStyle = prop['chart.background.barcolor2'];
  1007. co.strokeStyle = co.fillStyle;
  1008. height = (ca.height - gutterBottom);
  1009. for (var i= (40 + gutterTop); i<height; i+=80) {
  1010. co.fillRect(gutterLeft, i, ca.width - gutterLeft - gutterRight, i + 40 > (ca.height - gutterBottom) ? ca.height - (gutterBottom + i) : 40);
  1011. }
  1012. //context.stroke();
  1013. co.beginPath();
  1014. // Draw the background grid
  1015. if (prop['chart.background.grid']) {
  1016. // If autofit is specified, use the .numhlines and .numvlines along with the width to work
  1017. // out the hsize and vsize
  1018. if (prop['chart.background.grid.autofit']) {
  1019. /**
  1020. * Align the grid to the tickmarks
  1021. */
  1022. if (prop['chart.background.grid.autofit.align']) {
  1023. // Align the horizontal lines
  1024. obj.Set('chart.background.grid.autofit.numhlines', prop['chart.ylabels.count']);
  1025. // Align the vertical lines for the line
  1026. if (obj.type == 'line') {
  1027. if (prop['chart.labels'] && prop['chart.labels'].length) {
  1028. obj.Set('chart.background.grid.autofit.numvlines', prop['chart.labels'].length - 1);
  1029. } else {
  1030. obj.Set('chart.background.grid.autofit.numvlines', obj.data[0].length - 1);
  1031. }
  1032. // Align the vertical lines for the bar
  1033. } else if (obj.type == 'bar' && prop['chart.labels'] && prop['chart.labels'].length) {
  1034. obj.Set('chart.background.grid.autofit.numvlines', prop['chart.labels'].length);
  1035. }
  1036. }
  1037. var vsize = ((ca.width - gutterLeft - gutterRight)) / prop['chart.background.grid.autofit.numvlines'];
  1038. var hsize = (ca.height - gutterTop - gutterBottom) / prop['chart.background.grid.autofit.numhlines'];
  1039. obj.Set('chart.background.grid.vsize', vsize);
  1040. obj.Set('chart.background.grid.hsize', hsize);
  1041. }
  1042. co.beginPath();
  1043. co.lineWidth = prop['chart.background.grid.width'] ? prop['chart.background.grid.width'] : 1;
  1044. co.strokeStyle = prop['chart.background.grid.color'];
  1045. // Dashed background grid
  1046. if (prop['chart.background.grid.dashed'] && typeof co.setLineDash == 'function') {
  1047. co.setLineDash([3,2]);
  1048. }
  1049. // Dotted background grid
  1050. if (prop['chart.background.grid.dotted'] && typeof co.setLineDash == 'function') {
  1051. co.setLineDash([1,2]);
  1052. }
  1053. // Draw the horizontal lines
  1054. if (prop['chart.background.grid.hlines']) {
  1055. height = (ca.height - gutterBottom)
  1056. for (y=gutterTop; y<height; y+=prop['chart.background.grid.hsize']) {
  1057. context.moveTo(gutterLeft, Math.round(y));
  1058. context.lineTo(ca.width - gutterRight, Math.round(y));
  1059. }
  1060. }
  1061. if (prop['chart.background.grid.vlines']) {
  1062. // Draw the vertical lines
  1063. var width = (ca.width - gutterRight)
  1064. for (x=gutterLeft; x<=width; x+=prop['chart.background.grid.vsize']) {
  1065. co.moveTo(Math.round(x), gutterTop);
  1066. co.lineTo(Math.round(x), ca.height - gutterBottom);
  1067. }
  1068. }
  1069. if (prop['chart.background.grid.border']) {
  1070. // Make sure a rectangle, the same colour as the grid goes around the graph
  1071. co.strokeStyle = prop['chart.background.grid.color'];
  1072. co.strokeRect(Math.round(gutterLeft), Math.round(gutterTop), ca.width - gutterLeft - gutterRight, ca.height - gutterTop - gutterBottom);
  1073. }
  1074. }
  1075. context.stroke();
  1076. // Reset the line dash
  1077. if (typeof co.setLineDash == 'function') {
  1078. co.setLineDash([1,0]);
  1079. }
  1080. // If it's a bar and 3D variant, translate
  1081. if (variant == '3d') {
  1082. co.restore();
  1083. }
  1084. // Draw the title if one is set
  1085. if ( typeof(prop['chart.title']) == 'string') {
  1086. if (obj.type == 'gantt') {
  1087. gutterTop -= 10;
  1088. }
  1089. RG.DrawTitle(obj,
  1090. prop['chart.title'],
  1091. gutterTop,
  1092. null,
  1093. prop['chart.title.size'] ? prop['chart.title.size'] : prop['chart.text.size'] + 2);
  1094. }
  1095. co.stroke();
  1096. }
  1097. /**
  1098. * Makes a clone of an object
  1099. *
  1100. * @param obj val The object to clone
  1101. */
  1102. RGraph.array_clone = function (obj)
  1103. {
  1104. var RG = RGraph;
  1105. if(obj == null || typeof(obj) != 'object') {
  1106. return obj;
  1107. }
  1108. var temp = [];
  1109. for (var i=0,len=obj.length;i<len; ++i) {
  1110. if (typeof(obj[i]) == 'number') {
  1111. temp[i] = (function (arg) {return Number(arg);})(obj[i]);
  1112. } else if (typeof(obj[i]) == 'string') {
  1113. temp[i] = (function (arg) {return String(arg);})(obj[i]);
  1114. } else if (typeof(obj[i]) == 'function') {
  1115. temp[i] = obj[i];
  1116. } else {
  1117. temp[i] = RG.array_clone(obj[i]);
  1118. }
  1119. }
  1120. return temp;
  1121. }
  1122. /**
  1123. * Formats a number with thousand seperators so it's easier to read
  1124. *
  1125. * @param integer obj The chart object
  1126. * @param integer num The number to format
  1127. * @param string The (optional) string to prepend to the string
  1128. * @param string The (optional) string to append to the string
  1129. * @return string The formatted number
  1130. */
  1131. RGraph.number_format = function (obj, num)
  1132. {
  1133. var RG = RGraph;
  1134. var ca = obj.canvas;
  1135. var co = obj.context;
  1136. var prop = obj.properties;
  1137. var i;
  1138. var prepend = arguments[2] ? String(arguments[2]) : '';
  1139. var append = arguments[3] ? String(arguments[3]) : '';
  1140. var output = '';
  1141. var decimal = '';
  1142. var decimal_seperator = typeof(prop['chart.scale.point']) == 'string' ? prop['chart.scale.point'] : '.';
  1143. var thousand_seperator = typeof(prop['chart.scale.thousand']) == 'string' ? prop['chart.scale.thousand'] : ',';
  1144. RegExp.$1 = '';
  1145. var i,j;
  1146. if (typeof(prop['chart.scale.formatter']) == 'function') {
  1147. return prop['chart.scale.formatter'](obj, num);
  1148. }
  1149. // Ignore the preformatted version of "1e-2"
  1150. if (String(num).indexOf('e') > 0) {
  1151. return String(prepend + String(num) + append);
  1152. }
  1153. // We need then number as a string
  1154. num = String(num);
  1155. // Take off the decimal part - we re-append it later
  1156. if (num.indexOf('.') > 0) {
  1157. var tmp = num;
  1158. num = num.replace(/\.(.*)/, ''); // The front part of the number
  1159. decimal = tmp.replace(/(.*)\.(.*)/, '$2'); // The decimal part of the number
  1160. }
  1161. // Thousand seperator
  1162. //var seperator = arguments[1] ? String(arguments[1]) : ',';
  1163. var seperator = thousand_seperator;
  1164. /**
  1165. * Work backwards adding the thousand seperators
  1166. */
  1167. var foundPoint;
  1168. for (i=(num.length - 1),j=0; i>=0; j++,i--) {
  1169. var character = num.charAt(i);
  1170. if ( j % 3 == 0 && j != 0) {
  1171. output += seperator;
  1172. }
  1173. /**
  1174. * Build the output
  1175. */
  1176. output += character;
  1177. }
  1178. /**
  1179. * Now need to reverse the string
  1180. */
  1181. var rev = output;
  1182. output = '';
  1183. for (i=(rev.length - 1); i>=0; i--) {
  1184. output += rev.charAt(i);
  1185. }
  1186. // Tidy up
  1187. //output = output.replace(/^-,/, '-');
  1188. if (output.indexOf('-' + prop['chart.scale.thousand']) == 0) {
  1189. output = '-' + output.substr(('-' + prop['chart.scale.thousand']).length);
  1190. }
  1191. // Reappend the decimal
  1192. if (decimal.length) {
  1193. output = output + decimal_seperator + decimal;
  1194. decimal = '';
  1195. RegExp.$1 = '';
  1196. }
  1197. // Minor bugette
  1198. if (output.charAt(0) == '-') {
  1199. output = output.replace(/-/, '');
  1200. prepend = '-' + prepend;
  1201. }
  1202. return prepend + output + append;
  1203. }
  1204. /**
  1205. * Draws horizontal coloured bars on something like the bar, line or scatter
  1206. */
  1207. RGraph.DrawBars = function (obj)
  1208. {
  1209. var prop = obj.properties;
  1210. var co = obj.context;
  1211. var ca = obj.canvas;
  1212. var RG = RGraph;
  1213. var hbars = prop['chart.background.hbars'];
  1214. if (hbars === null) {
  1215. return;
  1216. }
  1217. /**
  1218. * Draws a horizontal bar
  1219. */
  1220. co.beginPath();
  1221. for (i=0,len=hbars.length; i<len; ++i) {
  1222. var start = hbars[i][0];
  1223. var length = hbars[i][1];
  1224. var color = hbars[i][2];
  1225. // Perform some bounds checking
  1226. if(RG.is_null(start))start = obj.scale2.max
  1227. if (start > obj.scale2.max) start = obj.scale2.max;
  1228. if (RG.is_null(length)) length = obj.scale2.max - start;
  1229. if (start + length > obj.scale2.max) length = obj.scale2.max - start;
  1230. if (start + length < (-1 * obj.scale2.max) ) length = (-1 * obj.scale2.max) - start;
  1231. if (prop['chart.xaxispos'] == 'center' && start == obj.scale2.max && length < (obj.scale2.max * -2)) {
  1232. length = obj.scale2.max * -2;
  1233. }
  1234. /**
  1235. * Draw the bar
  1236. */
  1237. var x = prop['chart.gutter.left'];
  1238. var y = obj.getYCoord(start);
  1239. var w = ca.width - prop['chart.gutter.left'] - prop['chart.gutter.right'];
  1240. var h = obj.getYCoord(start + length) - y;
  1241. // Accommodate Opera :-/
  1242. if (ISOPERA != -1 && prop['chart.xaxispos'] == 'center' && h < 0) {
  1243. h *= -1;
  1244. y = y - h;
  1245. }
  1246. /**
  1247. * Account for X axis at the top
  1248. */
  1249. if (prop['chart.xaxispos'] == 'top') {
  1250. y = ca.height - y;
  1251. h *= -1;
  1252. }
  1253. co.fillStyle = color;
  1254. co.fillRect(x, y, w, h);
  1255. }
  1256. /*
  1257. // If the X axis is at the bottom, and a negative max is given, warn the user
  1258. if (obj.Get('chart.xaxispos') == 'bottom' && (hbars[i][0] < 0 || (hbars[i][1] + hbars[i][1] < 0)) ) {
  1259. alert('[' + obj.type.toUpperCase() + ' (ID: ' + obj.id + ') BACKGROUND HBARS] You have a negative value in one of your background hbars values, whilst the X axis is in the center');
  1260. }
  1261. var ystart = (obj.grapharea - (((hbars[i][0] - obj.scale2.min) / (obj.scale2.max - obj.scale2.min)) * obj.grapharea));
  1262. //var height = (Math.min(hbars[i][1], obj.max - hbars[i][0]) / (obj.scale2.max - obj.scale2.min)) * obj.grapharea;
  1263. var height = obj.getYCoord(hbars[i][0]) - obj.getYCoord(hbars[i][1]);
  1264. // Account for the X axis being in the center
  1265. if (obj.Get('chart.xaxispos') == 'center') {
  1266. ystart /= 2;
  1267. //height /= 2;
  1268. }
  1269. ystart += obj.Get('chart.gutter.top')
  1270. var x = obj.Get('chart.gutter.left');
  1271. var y = ystart - height;
  1272. var w = obj.canvas.width - obj.Get('chart.gutter.left') - obj.Get('chart.gutter.right');
  1273. var h = height;
  1274. // Accommodate Opera :-/
  1275. if (navigator.userAgent.indexOf('Opera') != -1 && obj.Get('chart.xaxispos') == 'center' && h < 0) {
  1276. h *= -1;
  1277. y = y - h;
  1278. }
  1279. /**
  1280. * Account for X axis at the top
  1281. */
  1282. //if (obj.Get('chart.xaxispos') == 'top') {
  1283. // y = obj.canvas.height - y;
  1284. // h *= -1;
  1285. //}
  1286. //obj.context.fillStyle = hbars[i][2];
  1287. //obj.context.fillRect(x, y, w, h);
  1288. //}
  1289. }
  1290. /**
  1291. * Draws in-graph labels.
  1292. *
  1293. * @param object obj The graph object
  1294. */
  1295. RGraph.DrawInGraphLabels = function (obj)
  1296. {
  1297. var RG = RGraph;
  1298. var ca = obj.canvas;
  1299. var co = obj.context;
  1300. var prop = obj.properties;
  1301. var labels = prop['chart.labels.ingraph'];
  1302. var labels_processed = [];
  1303. // Defaults
  1304. var fgcolor = 'black';
  1305. var bgcolor = 'white';
  1306. var direction = 1;
  1307. if (!labels) {
  1308. return;
  1309. }
  1310. /**
  1311. * Preprocess the labels array. Numbers are expanded
  1312. */
  1313. for (var i=0,len=labels.length; i<len; i+=1) {
  1314. if (typeof(labels[i]) == 'number') {
  1315. for (var j=0; j<labels[i]; ++j) {
  1316. labels_processed.push(null);
  1317. }
  1318. } else if (typeof(labels[i]) == 'string' || typeof(labels[i]) == 'object') {
  1319. labels_processed.push(labels[i]);
  1320. } else {
  1321. labels_processed.push('');
  1322. }
  1323. }
  1324. /**
  1325. * Turn off any shadow
  1326. */
  1327. RG.NoShadow(obj);
  1328. if (labels_processed && labels_processed.length > 0) {
  1329. for (var i=0,len=labels_processed.length; i<len; ++i) {
  1330. if (labels_processed[i]) {
  1331. var coords = obj.coords[i];
  1332. if (coords && coords.length > 0) {
  1333. var x = (obj.type == 'bar' ? coords[0] + (coords[2] / 2) : coords[0]);
  1334. var y = (obj.type == 'bar' ? coords[1] + (coords[3] / 2) : coords[1]);
  1335. var length = typeof(labels_processed[i][4]) == 'number' ? labels_processed[i][4] : 25;
  1336. co.beginPath();
  1337. co.fillStyle = 'black';
  1338. co.strokeStyle = 'black';
  1339. if (obj.type == 'bar') {
  1340. /**
  1341. * X axis at the top
  1342. */
  1343. if (obj.Get('chart.xaxispos') == 'top') {
  1344. length *= -1;
  1345. }
  1346. if (prop['chart.variant'] == 'dot') {
  1347. co.moveTo(Math.round(x), obj.coords[i][1] - 5);
  1348. co.lineTo(Math.round(x), obj.coords[i][1] - 5 - length);
  1349. var text_x = Math.round(x);
  1350. var text_y = obj.coords[i][1] - 5 - length;
  1351. } else if (prop['chart.variant'] == 'arrow') {
  1352. co.moveTo(Math.round(x), obj.coords[i][1] - 5);
  1353. co.lineTo(Math.round(x), obj.coords[i][1] - 5 - length);
  1354. var text_x = Math.round(x);
  1355. var text_y = obj.coords[i][1] - 5 - length;
  1356. } else {
  1357. co.arc(Math.round(x), y, 2.5, 0, 6.28, 0);
  1358. co.moveTo(Math.round(x), y);
  1359. co.lineTo(Math.round(x), y - length);
  1360. var text_x = Math.round(x);
  1361. var text_y = y - length;
  1362. }
  1363. co.stroke();
  1364. co.fill();
  1365. } else if (obj.type == 'line') {
  1366. if (
  1367. typeof(labels_processed[i]) == 'object' &&
  1368. typeof(labels_processed[i][3]) == 'number' &&
  1369. labels_processed[i][3] == -1
  1370. ) {
  1371. co.moveTo(Math.round(x), y + 5);
  1372. co.lineTo(Math.round(x), y + 5 + length);
  1373. co.stroke();
  1374. co.beginPath();
  1375. // This draws the arrow
  1376. co.moveTo(Math.round(x), y + 5);
  1377. co.lineTo(Math.round(x) - 3, y + 10);
  1378. co.lineTo(Math.round(x) + 3, y + 10);
  1379. co.closePath();
  1380. var text_x = x;
  1381. var text_y = y + 5 + length;
  1382. } else {
  1383. var text_x = x;
  1384. var text_y = y - 5 - length;
  1385. co.moveTo(Math.round(x), y - 5);
  1386. co.lineTo(Math.round(x), y - 5 - length);
  1387. co.stroke();
  1388. co.beginPath();
  1389. // This draws the arrow
  1390. co.moveTo(Math.round(x), y - 5);
  1391. co.lineTo(Math.round(x) - 3, y - 10);
  1392. co.lineTo(Math.round(x) + 3, y - 10);
  1393. co.closePath();
  1394. }
  1395. co.fill();
  1396. }
  1397. // Taken out on the 10th Nov 2010 - unnecessary
  1398. //var width = context.measureText(labels[i]).width;
  1399. co.beginPath();
  1400. // Fore ground color
  1401. co.fillStyle = (typeof(labels_processed[i]) == 'object' && typeof(labels_processed[i][1]) == 'string') ? labels_processed[i][1] : 'black';
  1402. RG.Text2(obj,{'font':prop['chart.text.font'],
  1403. 'size':prop['chart.text.size'],
  1404. 'x':text_x,
  1405. 'y':text_y,
  1406. 'text': (typeof(labels_processed[i]) == 'object' && typeof(labels_processed[i][0]) == 'string') ? labels_processed[i][0] : labels_processed[i],
  1407. 'valign': 'bottom',
  1408. 'halign':'center',
  1409. 'bounding':true,
  1410. 'bounding.fill': (typeof(labels_processed[i]) == 'object' && typeof(labels_processed[i][2]) == 'string') ? labels_processed[i][2] : 'white',
  1411. 'tag':'labels ingraph'
  1412. });
  1413. co.fill();
  1414. }
  1415. }
  1416. }
  1417. }
  1418. }
  1419. /**
  1420. * This function "fills in" key missing properties that various implementations lack
  1421. *
  1422. * @param object e The event object
  1423. */
  1424. RGraph.FixEventObject = function (e)
  1425. {
  1426. if (ISOLD) {
  1427. var e = event;
  1428. e.pageX = (event.clientX + document.body.scrollLeft);
  1429. e.pageY = (event.clientY + document.body.scrollTop);
  1430. e.target = event.srcElement;
  1431. if (!document.body.scrollTop && document.documentElement.scrollTop) {
  1432. e.pageX += parseInt(document.documentElement.scrollLeft);
  1433. e.pageY += parseInt(document.documentElement.scrollTop);
  1434. }
  1435. }
  1436. // Any browser that doesn't implement stopPropagation() (MSIE)
  1437. if (!e.stopPropagation) {
  1438. e.stopPropagation = function () {window.event.cancelBubble = true;}
  1439. }
  1440. return e;
  1441. }
  1442. /**
  1443. * Thisz function hides the crosshairs coordinates
  1444. */
  1445. RGraph.HideCrosshairCoords = function ()
  1446. {
  1447. var RG = RGraph;
  1448. var div = RG.Registry.Get('chart.coordinates.coords.div');
  1449. if ( div
  1450. && div.style.opacity == 1
  1451. && div.__object__.Get('chart.crosshairs.coords.fadeout')
  1452. ) {
  1453. setTimeout(function() {RG.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.9;}, 50);
  1454. setTimeout(function() {RG.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.8;}, 100);
  1455. setTimeout(function() {RG.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.7;}, 150);
  1456. setTimeout(function() {RG.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.6;}, 200);
  1457. setTimeout(function() {RG.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.5;}, 250);
  1458. setTimeout(function() {RG.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.4;}, 300);
  1459. setTimeout(function() {RG.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.3;}, 350);
  1460. setTimeout(function() {RG.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.2;}, 400);
  1461. setTimeout(function() {RG.Registry.Get('chart.coordinates.coords.div').style.opacity = 0.1;}, 450);
  1462. setTimeout(function() {RG.Registry.Get('chart.coordinates.coords.div').style.opacity = 0;}, 500);
  1463. setTimeout(function() {RG.Registry.Get('chart.coordinates.coords.div').style.display = 'none';}, 550);
  1464. }
  1465. }
  1466. /**
  1467. * Draws the3D axes/background
  1468. */
  1469. RGraph.Draw3DAxes = function (obj)
  1470. {
  1471. var prop = obj.properties;
  1472. var co = obj.context;
  1473. var ca = obj.canvas;
  1474. var gutterLeft = prop['chart.gutter.left'];
  1475. var gutterRight = prop['chart.gutter.right'];
  1476. var gutterTop = prop['chart.gutter.top'];
  1477. var gutterBottom = prop['chart.gutter.bottom'];
  1478. co.strokeStyle = '#aaa';
  1479. co.fillStyle = '#ddd';
  1480. // Draw the vertical left side
  1481. co.beginPath();
  1482. co.moveTo(gutterLeft, gutterTop);
  1483. co.lineTo(gutterLeft + 10, gutterTop - 5);
  1484. co.lineTo(gutterLeft + 10, ca.height - gutterBottom - 5);
  1485. co.lineTo(gutterLeft, ca.height - gutterBottom);
  1486. co.closePath();
  1487. co.stroke();
  1488. co.fill();
  1489. // Draw the bottom floor
  1490. co.beginPath();
  1491. co.moveTo(gutterLeft, ca.height - gutterBottom);
  1492. co.lineTo(gutterLeft + 10, ca.height - gutterBottom - 5);
  1493. co.lineTo(ca.width - gutterRight + 10, ca.height - gutterBottom - 5);
  1494. co.lineTo(ca.width - gutterRight, ca.height - gutterBottom);
  1495. co.closePath();
  1496. co.stroke();
  1497. co.fill();
  1498. }
  1499. /**
  1500. * This function attempts to "fill in" missing functions from the canvas
  1501. * context object. Only two at the moment - measureText() nd fillText().
  1502. *
  1503. * @param object context The canvas 2D context
  1504. */
  1505. RGraph.OldBrowserCompat = function (co)
  1506. {
  1507. if (!co) {
  1508. return;
  1509. }
  1510. if (!co.measureText) {
  1511. // This emulates the measureText() function
  1512. co.measureText = function (text)
  1513. {
  1514. var textObj = document.createElement('DIV');
  1515. textObj.innerHTML = text;
  1516. textObj.style.position = 'absolute';
  1517. textObj.style.top = '-100px';
  1518. textObj.style.left = 0;
  1519. document.body.appendChild(textObj);
  1520. var width = {width: textObj.offsetWidth};
  1521. textObj.style.display = 'none';
  1522. return width;
  1523. }
  1524. }
  1525. if (!co.fillText) {
  1526. // This emulates the fillText() method
  1527. co.fillText = function (text, targetX, targetY)
  1528. {
  1529. return false;
  1530. }
  1531. }
  1532. // If IE8, add addEventListener()
  1533. if (!co.canvas.addEventListener) {
  1534. window.addEventListener = function (ev, func, bubble)
  1535. {
  1536. return this.attachEvent('on' + ev, func);
  1537. }
  1538. co.canvas.addEventListener = function (ev, func, bubble)
  1539. {
  1540. return this.attachEvent('on' + ev, func);
  1541. }
  1542. }
  1543. }
  1544. /**
  1545. * Draws a rectangle with curvy corners
  1546. *
  1547. * @param co object The context
  1548. * @param x number The X coordinate (top left of the square)
  1549. * @param y number The Y coordinate (top left of the square)
  1550. * @param w number The width of the rectangle
  1551. * @param h number The height of the rectangle
  1552. * @param number The radius of the curved corners
  1553. * @param boolean Whether the top left corner is curvy
  1554. * @param boolean Whether the top right corner is curvy
  1555. * @param boolean Whether the bottom right corner is curvy
  1556. * @param boolean Whether the bottom left corner is curvy
  1557. */
  1558. RGraph.strokedCurvyRect = function (co, x, y, w, h)
  1559. {
  1560. // The corner radius
  1561. var r = arguments[5] ? arguments[5] : 3;
  1562. // The corners
  1563. var corner_tl = (arguments[6] || arguments[6] == null) ? true : false;
  1564. var corner_tr = (arguments[7] || arguments[7] == null) ? true : false;
  1565. var corner_br = (arguments[8] || arguments[8] == null) ? true : false;
  1566. var corner_bl = (arguments[9] || arguments[9] == null) ? true : false;
  1567. co.beginPath();
  1568. // Top left side
  1569. co.moveTo(x + (corner_tl ? r : 0), y);
  1570. co.lineTo(x + w - (corner_tr ? r : 0), y);
  1571. // Top right corner
  1572. if (corner_tr) {
  1573. co.arc(x + w - r, y + r, r, PI + HALFPI, TWOPI, false);
  1574. }
  1575. // Top right side
  1576. co.lineTo(x + w, y + h - (corner_br ? r : 0) );
  1577. // Bottom right corner
  1578. if (corner_br) {
  1579. co.arc(x + w - r, y - r + h, r, TWOPI, HALFPI, false);
  1580. }
  1581. // Bottom right side
  1582. co.lineTo(x + (corner_bl ? r : 0), y + h);
  1583. // Bottom left corner
  1584. if (corner_bl) {
  1585. co.arc(x + r, y - r + h, r, HALFPI, PI, false);
  1586. }
  1587. // Bottom left side
  1588. co.lineTo(x, y + (corner_tl ? r : 0) );
  1589. // Top left corner
  1590. if (corner_tl) {
  1591. co.arc(x + r, y + r, r, PI, PI + HALFPI, false);
  1592. }
  1593. co.stroke();
  1594. }
  1595. /**
  1596. * Draws a filled rectangle with curvy corners
  1597. *
  1598. * @param context object The context
  1599. * @param x number The X coordinate (top left of the square)
  1600. * @param y number The Y coordinate (top left of the square)
  1601. * @param w number The width of the rectangle
  1602. * @param h number The height of the rectangle
  1603. * @param number The radius of the curved corners
  1604. * @param boolean Whether the top left corner is curvy
  1605. * @param boolean Whether the top right corner is curvy
  1606. * @param boolean Whether the bottom right corner is curvy
  1607. * @param boolean Whether the bottom left corner is curvy
  1608. */
  1609. RGraph.filledCurvyRect = function (co, x, y, w, h)
  1610. {
  1611. // The corner radius
  1612. var r = arguments[5] ? arguments[5] : 3;
  1613. // The corners
  1614. var corner_tl = (arguments[6] || arguments[6] == null) ? true : false;
  1615. var corner_tr = (arguments[7] || arguments[7] == null) ? true : false;
  1616. var corner_br = (arguments[8] || arguments[8] == null) ? true : false;
  1617. var corner_bl = (arguments[9] || arguments[9] == null) ? true : false;
  1618. co.beginPath();
  1619. // First draw the corners
  1620. // Top left corner
  1621. if (corner_tl) {
  1622. co.moveTo(x + r, y + r);
  1623. co.arc(x + r, y + r, r, PI, PI + HALFPI, false);
  1624. } else {
  1625. co.fillRect(x, y, r, r);
  1626. }
  1627. // Top right corner
  1628. if (corner_tr) {
  1629. co.moveTo(x + w - r, y + r);
  1630. co.arc(x + w - r, y + r, r, PI + HALFPI, 0, false);
  1631. } else {
  1632. co.moveTo(x + w - r, y);
  1633. co.fillRect(x + w - r, y, r, r);
  1634. }
  1635. // Bottom right corner
  1636. if (corner_br) {
  1637. co.moveTo(x + w - r, y + h - r);
  1638. co.arc(x + w - r, y - r + h, r, 0, HALFPI, false);
  1639. } else {
  1640. co.moveTo(x + w - r, y + h - r);
  1641. co.fillRect(x + w - r, y + h - r, r, r);
  1642. }
  1643. // Bottom left corner
  1644. if (corner_bl) {
  1645. co.moveTo(x + r, y + h - r);
  1646. co.arc(x + r, y - r + h, r, HALFPI, PI, false);
  1647. } else {
  1648. co.moveTo(x, y + h - r);
  1649. co.fillRect(x, y + h - r, r, r);
  1650. }
  1651. // Now fill it in
  1652. co.fillRect(x + r, y, w - r - r, h);
  1653. co.fillRect(x, y + r, r + 1, h - r - r);
  1654. co.fillRect(x + w - r - 1, y + r, r + 1, h - r - r);
  1655. co.fill();
  1656. }
  1657. /**
  1658. * Hides the zoomed canvas
  1659. */
  1660. RGraph.HideZoomedCanvas = function ()
  1661. {
  1662. var interval = 15;
  1663. var frames = 10;
  1664. if (typeof(__zoomedimage__) == 'object') {
  1665. var obj = __zoomedimage__.obj;
  1666. var prop = obj.properties;
  1667. } else {
  1668. return;
  1669. }
  1670. if (prop['chart.zoom.fade.out']) {
  1671. for (var i=frames,j=1; i>=0; --i, ++j) {
  1672. if (typeof(__zoomedimage__) == 'object') {
  1673. setTimeout("__zoomedimage__.style.opacity = " + String(i / 10), j * interval);
  1674. }
  1675. }
  1676. if (typeof(__zoomedbackground__) == 'object') {
  1677. setTimeout("__zoomedbackground__.style.opacity = " + String(i / frames), j * interval);
  1678. }
  1679. }
  1680. if (typeof(__zoomedimage__) == 'object') {
  1681. setTimeout("__zoomedimage__.style.display = 'none'", prop['chart.zoom.fade.out'] ? (frames * interval) + 10 : 0);
  1682. }
  1683. if (typeof(__zoomedbackground__) == 'object') {
  1684. setTimeout("__zoomedbackground__.style.display = 'none'", prop['chart.zoom.fade.out'] ? (frames * interval) + 10 : 0);
  1685. }
  1686. }
  1687. /**
  1688. * Adds an event handler
  1689. *
  1690. * @param object obj The graph object
  1691. * @param string event The name of the event, eg ontooltip
  1692. * @param object func The callback function
  1693. */
  1694. RGraph.AddCustomEventListener = function (obj, name, func)
  1695. {
  1696. var RG = RGraph;
  1697. if (typeof(RG.events[obj.uid]) == 'undefined') {
  1698. RG.events[obj.uid] = [];
  1699. }
  1700. RG.events[obj.uid].push([obj, name, func]);
  1701. return RG.events[obj.uid].length - 1;
  1702. }
  1703. /**
  1704. * Used to fire one of the RGraph custom events
  1705. *
  1706. * @param object obj The graph object that fires the event
  1707. * @param string event The name of the event to fire
  1708. */
  1709. RGraph.FireCustomEvent = function (obj, name)
  1710. {
  1711. var RG = RGraph;
  1712. if (obj && obj.isRGraph) {
  1713. // New style of adding custom events
  1714. if (obj[name]) {
  1715. (obj[name])(obj);
  1716. }
  1717. var uid = obj.uid;
  1718. if ( typeof(uid) == 'string'
  1719. && typeof(RG.events) == 'object'
  1720. && typeof(RG.events[uid]) == 'object'
  1721. && RG.events[uid].length > 0) {
  1722. for(var j=0; j<RG.events[uid].length; ++j) {
  1723. if (RG.events[uid][j] && RG.events[uid][j][1] == name) {
  1724. RG.events[uid][j][2](obj);
  1725. }
  1726. }
  1727. }
  1728. }
  1729. }
  1730. /**
  1731. * If you prefer, you can use the SetConfig() method to set the configuration information
  1732. * for your chart. You may find that setting the configuration this way eases reuse.
  1733. *
  1734. * @param object obj The graph object
  1735. * @param object config The graph configuration information
  1736. */
  1737. RGraph.SetConfig = function (obj, config)
  1738. {
  1739. for (i in config) {
  1740. if (typeof(i) == 'string') {
  1741. obj.Set(i, config[i]);
  1742. }
  1743. }
  1744. return obj;
  1745. }
  1746. /**
  1747. * Clears all the custom event listeners that have been registered
  1748. *
  1749. * @param string Limits the clearing to this object ID
  1750. */
  1751. RGraph.RemoveAllCustomEventListeners = function ()
  1752. {
  1753. var RG = RGraph;
  1754. var id = arguments[0];
  1755. if (id && RG.events[id]) {
  1756. RG.events[id] = [];
  1757. } else {
  1758. RG.events = [];
  1759. }
  1760. }
  1761. /**
  1762. * Clears a particular custom event listener
  1763. *
  1764. * @param object obj The graph object
  1765. * @param number i This is the index that is return by .AddCustomEventListener()
  1766. */
  1767. RGraph.RemoveCustomEventListener = function (obj, i)
  1768. {
  1769. var RG = RGraph;
  1770. if ( typeof(RG.events) == 'object'
  1771. && typeof(RG.events[obj.id]) == 'object'
  1772. && typeof(RG.events[obj.id][i]) == 'object') {
  1773. RG.events[obj.id][i] = null;
  1774. }
  1775. }
  1776. /**
  1777. * This draws the background
  1778. *
  1779. * @param object obj The graph object
  1780. */
  1781. RGraph.DrawBackgroundImage = function (obj)
  1782. {
  1783. var prop = obj.properties;
  1784. var ca = obj.canvas;
  1785. var co = obj.context;
  1786. var RG = RGraph;
  1787. if (typeof(prop['chart.background.image']) == 'string') {
  1788. if (typeof(ca.__rgraph_background_image__) == 'undefined') {
  1789. var img = new Image();
  1790. img.__object__ = obj;
  1791. img.__canvas__ = ca;
  1792. img.__context__ = co;
  1793. img.src = obj.Get('chart.background.image');
  1794. ca.__rgraph_background_image__ = img;
  1795. } else {
  1796. img = ca.__rgraph_background_image__;
  1797. }
  1798. // When the image has loaded - redraw the canvas
  1799. img.onload = function ()
  1800. {
  1801. obj.__rgraph_background_image_loaded__ = true;
  1802. RG.Clear(ca);
  1803. RG.RedrawCanvas(ca);
  1804. }
  1805. var gutterLeft = obj.gutterLeft;
  1806. var gutterRight = obj.gutterRight;
  1807. var gutterTop = obj.gutterTop;
  1808. var gutterBottom = obj.gutterBottom;
  1809. var stretch = prop['chart.background.image.stretch'];
  1810. var align = prop['chart.background.image.align'];
  1811. // Handle chart.background.image.align
  1812. if (typeof(align) == 'string') {
  1813. if (align.indexOf('right') != -1) {
  1814. var x = ca.width - img.width - gutterRight;
  1815. } else {
  1816. var x = gutterLeft;
  1817. }
  1818. if (align.indexOf('bottom') != -1) {
  1819. var y = ca.height - img.height - gutterBottom;
  1820. } else {
  1821. var y = gutterTop;
  1822. }
  1823. } else {
  1824. var x = gutterLeft;
  1825. var y = gutterTop;
  1826. }
  1827. // X/Y coords take precedence over the align
  1828. var x = typeof(prop['chart.background.image.x']) == 'number' ? prop['chart.background.image.x'] : x;
  1829. var y = typeof(prop['chart.background.image.y']) == 'number' ? prop['chart.background.image.y'] : y;
  1830. var w = stretch ? ca.width - gutterLeft - gutterRight : img.width;
  1831. var h = stretch ? ca.height - gutterTop - gutterBottom : img.height;
  1832. /**
  1833. * You can now specify the width and height of the image
  1834. */
  1835. if (typeof(prop['chart.background.image.w']) == 'number') w = prop['chart.background.image.w'];
  1836. if (typeof(prop['chart.background.image.h']) == 'number') h = prop['chart.background.image.h'];
  1837. co.drawImage(img,x,y,w, h);
  1838. }
  1839. }
  1840. /**
  1841. * This function determines wshether an object has tooltips or not
  1842. *
  1843. * @param object obj The chart object
  1844. */
  1845. RGraph.hasTooltips = function (obj)
  1846. {
  1847. var prop = obj.properties;
  1848. if (typeof(prop['chart.tooltips']) == 'object' && prop['chart.tooltips']) {
  1849. for (var i=0,len=prop['chart.tooltips'].length; i<len; ++i) {
  1850. if (!RGraph.is_null(obj.Get('chart.tooltips')[i])) {
  1851. return true;
  1852. }
  1853. }
  1854. } else if (typeof(prop['chart.tooltips']) == 'function') {
  1855. return true;
  1856. }
  1857. return false;
  1858. }
  1859. /**
  1860. * This function creates a (G)UID which can be used to identify objects.
  1861. *
  1862. * @return string (g)uid The (G)UID
  1863. */
  1864. RGraph.CreateUID = function ()
  1865. {
  1866. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c)
  1867. {
  1868. var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
  1869. return v.toString(16);
  1870. });
  1871. }
  1872. /**
  1873. * This is the new object registry, used to facilitate multiple objects per canvas.
  1874. *
  1875. * @param object obj The object to register
  1876. */
  1877. RGraph.ObjectRegistry.Add = function (obj)
  1878. {
  1879. var uid = obj.uid;
  1880. var id = obj.canvas.id;
  1881. var RG = RGraph;
  1882. /**
  1883. * Index the objects by UID
  1884. */
  1885. RG.ObjectRegistry.objects.byUID.push([uid, obj]);
  1886. /**
  1887. * Index the objects by the canvas that they're drawn on
  1888. */
  1889. RG.ObjectRegistry.objects.byCanvasID.push([id, obj]);
  1890. }
  1891. /**
  1892. * Remove an object from the object registry
  1893. *
  1894. * @param object obj The object to remove.
  1895. */
  1896. RGraph.ObjectRegistry.Remove = function (obj)
  1897. {
  1898. var id = obj.id;
  1899. var uid = obj.uid;
  1900. var RG = RGraph;
  1901. for (var i=0; i<RG.ObjectRegistry.objects.byUID.length; ++i) {
  1902. if (RG.ObjectRegistry.objects.byUID[i] && RG.ObjectRegistry.objects.byUID[i][1].uid == uid) {
  1903. RG.ObjectRegistry.objects.byUID[i] = null;
  1904. }
  1905. }
  1906. for (var i=0; i<RG.ObjectRegistry.objects.byCanvasID.length; ++i) {
  1907. if ( RG.ObjectRegistry.objects.byCanvasID[i]
  1908. && RG.ObjectRegistry.objects.byCanvasID[i][1]
  1909. && RG.ObjectRegistry.objects.byCanvasID[i][1].uid == uid) {
  1910. RG.ObjectRegistry.objects.byCanvasID[i] = null;
  1911. }
  1912. }
  1913. }
  1914. /**
  1915. * Removes all objects from the ObjectRegistry. If either the ID of a canvas is supplied,
  1916. * or the canvas itself, then only objects pertaining to that canvas are cleared.
  1917. *
  1918. * @param mixed Either a canvas object (as returned by document.getElementById()
  1919. * or the ID of a canvas (ie a string)
  1920. */
  1921. RGraph.ObjectRegistry.Clear = function ()
  1922. {
  1923. var RG = RGraph;
  1924. // If an ID is supplied restrict the learing to that
  1925. if (arguments[0]) {
  1926. var id = (typeof(arguments[0]) == 'object' ? arguments[0].id : arguments[0]);
  1927. var objects = RG.ObjectRegistry.getObjectsByCanvasID(id);
  1928. for (var i=0; i<objects.length; ++i) {
  1929. RG.ObjectRegistry.Remove(objects[i]);
  1930. }
  1931. } else {
  1932. RG.ObjectRegistry.objects = {};
  1933. RG.ObjectRegistry.objects.byUID = [];
  1934. RG.ObjectRegistry.objects.byCanvasID = [];
  1935. }
  1936. }
  1937. /**
  1938. * Lists all objects in the ObjectRegistry
  1939. *
  1940. * @param boolean ret Whether to return the list or alert() it
  1941. */
  1942. RGraph.ObjectRegistry.List = function ()
  1943. {
  1944. var list = [];
  1945. var RG = RGraph;
  1946. for (var i=0,len=RG.ObjectRegistry.objects.byUID.length; i<len; ++i) {
  1947. if (RG.ObjectRegistry.objects.byUID[i]) {
  1948. list.push(RG.ObjectRegistry.objects.byUID[i][1].type);
  1949. }
  1950. }
  1951. if (arguments[0]) {
  1952. return list;
  1953. } else {
  1954. p(list);
  1955. }
  1956. }
  1957. /**
  1958. * Clears the ObjectRegistry of objects that are of a certain given type
  1959. *
  1960. * @param type string The type to clear
  1961. */
  1962. RGraph.ObjectRegistry.ClearByType = function (type)
  1963. {
  1964. var RG = RGraph;
  1965. var objects = RG.ObjectRegistry.objects.byUID;
  1966. for (var i=0; i<objects.length; ++i) {
  1967. if (objects[i]) {
  1968. var uid = objects[i][0];
  1969. var obj = objects[i][1];
  1970. if (obj && obj.type == type) {
  1971. RG.ObjectRegistry.Remove(obj);
  1972. }
  1973. }
  1974. }
  1975. }
  1976. /**
  1977. * This function provides an easy way to go through all of the objects that are held in the
  1978. * Registry
  1979. *
  1980. * @param func function This function is run for every object. Its passed the object as an argument
  1981. * @param string type Optionally, you can pass a type of object to look for
  1982. */
  1983. RGraph.ObjectRegistry.Iterate = function (func)
  1984. {
  1985. var objects = RGraph.ObjectRegistry.objects.byUID;
  1986. for (var i=0; i<objects.length; ++i) {
  1987. if (typeof arguments[1] == 'string') {
  1988. var types = arguments[1].split(/,/);
  1989. for (var j=0; j<types.length; ++j) {
  1990. if (types[j] == objects[i][1].type) {
  1991. func(objects[i][1]);
  1992. }
  1993. }
  1994. } else {
  1995. func(objects[i][1]);
  1996. }
  1997. }
  1998. }
  1999. /**
  2000. * Retrieves all objects for a given canvas id
  2001. *
  2002. * @patarm id string The canvas ID to get objects for.
  2003. */
  2004. RGraph.ObjectRegistry.getObjectsByCanvasID = function (id)
  2005. {
  2006. var store = RGraph.ObjectRegistry.objects.byCanvasID;
  2007. var ret = [];
  2008. // Loop through all of the objects and return the appropriate ones
  2009. for (var i=0; i<store.length; ++i) {
  2010. if (store[i] && store[i][0] == id ) {
  2011. ret.push(store[i][1]);
  2012. }
  2013. }
  2014. return ret;
  2015. }
  2016. /**
  2017. * Retrieves the relevant object based on the X/Y position.
  2018. *
  2019. * @param object e The event object
  2020. * @return object The applicable (if any) object
  2021. */
  2022. RGraph.ObjectRegistry.getFirstObjectByXY =
  2023. RGraph.ObjectRegistry.getObjectByXY = function (e)
  2024. {
  2025. var canvas = e.target;
  2026. var ret = null;
  2027. var objects = RGraph.ObjectRegistry.getObjectsByCanvasID(canvas.id);
  2028. for (var i=(objects.length - 1); i>=0; --i) {
  2029. var obj = objects[i].getObjectByXY(e);
  2030. if (obj) {
  2031. return obj;
  2032. }
  2033. }
  2034. }
  2035. /**
  2036. * Retrieves the relevant objects based on the X/Y position.
  2037. * NOTE This function returns an array of objects
  2038. *
  2039. * @param object e The event object
  2040. * @return An array of pertinent objects. Note the there may be only one object
  2041. */
  2042. RGraph.ObjectRegistry.getObjectsByXY = function (e)
  2043. {
  2044. var canvas = e.target;
  2045. var ret = [];
  2046. var objects = RGraph.ObjectRegistry.getObjectsByCanvasID(canvas.id);
  2047. // Retrieve objects "front to back"
  2048. for (var i=(objects.length - 1); i>=0; --i) {
  2049. var obj = objects[i].getObjectByXY(e);
  2050. if (obj) {
  2051. ret.push(obj);
  2052. }
  2053. }
  2054. return ret;
  2055. }
  2056. /**
  2057. * Retrieves the object with the corresponding UID
  2058. *
  2059. * @param string uid The UID to get the relevant object for
  2060. */
  2061. RGraph.ObjectRegistry.getObjectByUID = function (uid)
  2062. {
  2063. var objects = RGraph.ObjectRegistry.objects.byUID;
  2064. for (var i=0; i<objects.length; ++i) {
  2065. if (objects[i] && objects[i][1].uid == uid) {
  2066. return objects[i][1];
  2067. }
  2068. }
  2069. }
  2070. /**
  2071. * Brings a chart to the front of the ObjectRegistry by
  2072. * removing it and then readding it at the end and then
  2073. * redrawing the canvas
  2074. *
  2075. * @param object obj The object to bring to the front
  2076. * @param boolean redraw Whether to redraw the canvas after the
  2077. * object has been moved
  2078. */
  2079. RGraph.ObjectRegistry.bringToFront = function (obj)
  2080. {
  2081. var redraw = typeof arguments[1] == 'undefined' ? true : arguments[1];
  2082. RGraph.ObjectRegistry.Remove(obj);
  2083. RGraph.ObjectRegistry.Add(obj);
  2084. if (redraw) {
  2085. RGraph.RedrawCanvas(obj.canvas);
  2086. }
  2087. }
  2088. /**
  2089. * Retrieves the objects that are the given type
  2090. *
  2091. * @param mixed canvas The canvas to check. It can either be the canvas object itself or just the ID
  2092. * @param string type The type to look for
  2093. * @return array An array of one or more objects
  2094. */
  2095. RGraph.ObjectRegistry.getObjectsByType = function (type)
  2096. {
  2097. var objects = RGraph.ObjectRegistry.objects.byUID;
  2098. var ret = [];
  2099. for (var i=0; i<objects.length; ++i) {
  2100. if (objects[i] && objects[i][1] && objects[i][1].type && objects[i][1].type && objects[i][1].type == type) {
  2101. ret.push(objects[i][1]);
  2102. }
  2103. }
  2104. return ret;
  2105. }
  2106. /**
  2107. * Retrieves the FIRST object that matches the given type
  2108. *
  2109. * @param string type The type of object to look for
  2110. * @return object The FIRST object that matches the given type
  2111. */
  2112. RGraph.ObjectRegistry.getFirstObjectByType = function (type)
  2113. {
  2114. var objects = RGraph.ObjectRegistry.objects.byUID;
  2115. for (var i=0; i<objects.length; ++i) {
  2116. if (objects[i] && objects[i][1] && objects[i][1].type == type) {
  2117. return objects[i][1];
  2118. }
  2119. }
  2120. return null;
  2121. }
  2122. /**
  2123. * This takes centerx, centery, x and y coordinates and returns the
  2124. * appropriate angle relative to the canvas angle system. Remember
  2125. * that the canvas angle system starts at the EAST axis
  2126. *
  2127. * @param number cx The centerx coordinate
  2128. * @param number cy The centery coordinate
  2129. * @param number x The X coordinate (eg the mouseX if coming from a click)
  2130. * @param number y The Y coordinate (eg the mouseY if coming from a click)
  2131. * @return number The relevant angle (measured in in RADIANS)
  2132. */
  2133. RGraph.getAngleByXY = function (cx, cy, x, y)
  2134. {
  2135. var angle = Math.atan((y - cy) / (x - cx));
  2136. angle = Math.abs(angle)
  2137. if (x >= cx && y >= cy) {
  2138. angle += TWOPI;
  2139. } else if (x >= cx && y < cy) {
  2140. angle = (HALFPI - angle) + (PI + HALFPI);
  2141. } else if (x < cx && y < cy) {
  2142. angle += PI;
  2143. } else {
  2144. angle = PI - angle;
  2145. }
  2146. /**
  2147. * Upper and lower limit checking
  2148. */
  2149. if (angle > TWOPI) {
  2150. angle -= TWOPI;
  2151. }
  2152. return angle;
  2153. }
  2154. /**
  2155. * This function returns the distance between two points. In effect the
  2156. * radius of an imaginary circle that is centered on x1 and y1. The name
  2157. * of this function is derived from the word "Hypoteneuse", which in
  2158. * trigonmetry is the longest side of a triangle
  2159. *
  2160. * @param number x1 The original X coordinate
  2161. * @param number y1 The original Y coordinate
  2162. * @param number x2 The target X coordinate
  2163. * @param number y2 The target Y coordinate
  2164. */
  2165. RGraph.getHypLength = function (x1, y1, x2, y2)
  2166. {
  2167. var ret = Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
  2168. return ret;
  2169. }
  2170. /**
  2171. * This function gets the end point (X/Y coordinates) of a given radius.
  2172. * You pass it the center X/Y and the radius and this function will return
  2173. * the endpoint X/Y coordinates.
  2174. *
  2175. * @param number cx The center X coord
  2176. * @param number cy The center Y coord
  2177. * @param number r The lrngth of the radius
  2178. */
  2179. RGraph.getRadiusEndPoint = function (cx, cy, angle, radius)
  2180. {
  2181. var x = cx + (Math.cos(angle) * radius);
  2182. var y = cy + (Math.sin(angle) * radius);
  2183. return [x, y];
  2184. }
  2185. /**
  2186. * This installs all of the event listeners
  2187. *
  2188. * @param object obj The chart object
  2189. */
  2190. RGraph.InstallEventListeners = function (obj)
  2191. {
  2192. var RG = RGraph;
  2193. var prop = obj.properties;
  2194. /**
  2195. * Don't attempt to install event listeners for older versions of MSIE
  2196. */
  2197. if (ISOLD) {
  2198. return;
  2199. }
  2200. /**
  2201. * If this function exists, then the dynamic file has been included.
  2202. */
  2203. if (RG.InstallCanvasClickListener) {
  2204. RG.InstallWindowMousedownListener(obj);
  2205. RG.InstallWindowMouseupListener(obj);
  2206. RG.InstallCanvasMousemoveListener(obj);
  2207. RG.InstallCanvasMouseupListener(obj);
  2208. RG.InstallCanvasMousedownListener(obj);
  2209. RG.InstallCanvasClickListener(obj);
  2210. } else if ( RG.hasTooltips(obj)
  2211. || prop['chart.adjustable']
  2212. || prop['chart.annotatable']
  2213. || prop['chart.contextmenu']
  2214. || prop['chart.resizable']
  2215. || prop['chart.key.interactive']
  2216. || prop['chart.events.click']
  2217. || prop['chart.events.mousemove']
  2218. || typeof obj.onclick == 'function'
  2219. || typeof obj.onmousemove == 'function'
  2220. ) {
  2221. alert('[RGRAPH] You appear to have used dynamic features but not included the file: RGraph.common.dynamic.js');
  2222. }
  2223. }
  2224. /**
  2225. * Loosly mimicks the PHP function print_r();
  2226. */
  2227. RGraph.pr = function (obj)
  2228. {
  2229. var indent = (arguments[2] ? arguments[2] : ' ');
  2230. var str = '';
  2231. var counter = typeof arguments[3] == 'number' ? arguments[3] : 0;
  2232. if (counter >= 5) {
  2233. return '';
  2234. }
  2235. switch (typeof obj) {
  2236. case 'string': str += obj + ' (' + (typeof obj) + ', ' + obj.length + ')'; break;
  2237. case 'number': str += obj + ' (' + (typeof obj) + ')'; break;
  2238. case 'boolean': str += obj + ' (' + (typeof obj) + ')'; break;
  2239. case 'function': str += 'function () {}'; break;
  2240. case 'undefined': str += 'undefined'; break;
  2241. case 'null': str += 'null'; break;
  2242. case 'object':
  2243. // In case of null
  2244. if (RGraph.is_null(obj)) {
  2245. str += indent + 'null\n';
  2246. } else {
  2247. str += indent + 'Object {' + '\n'
  2248. for (j in obj) {
  2249. str += indent + ' ' + j + ' => ' + RGraph.pr(obj[j], true, indent + ' ', counter + 1) + '\n';
  2250. }
  2251. str += indent + '}';
  2252. }
  2253. break;
  2254. default:
  2255. str += 'Unknown type: ' + typeof obj + '';
  2256. break;
  2257. }
  2258. /**
  2259. * Finished, now either return if we're in a recursed call, or alert()
  2260. * if we're not.
  2261. */
  2262. if (!arguments[1]) {
  2263. alert(str);
  2264. }
  2265. return str;
  2266. }
  2267. /**
  2268. * Produces a dashed line
  2269. *
  2270. * @param object co The 2D context
  2271. * @param number x1 The start X coordinate
  2272. * @param number y1 The start Y coordinate
  2273. * @param number x2 The end X coordinate
  2274. * @param number y2 The end Y coordinate
  2275. */
  2276. RGraph.DashedLine = function(co, x1, y1, x2, y2)
  2277. {
  2278. /**
  2279. * This is the size of the dashes
  2280. */
  2281. var size = 5;
  2282. /**
  2283. * The optional fifth argument can be the size of the dashes
  2284. */
  2285. if (typeof(arguments[5]) == 'number') {
  2286. size = arguments[5];
  2287. }
  2288. var dx = x2 - x1;
  2289. var dy = y2 - y1;
  2290. var num = Math.floor(Math.sqrt((dx * dx) + (dy * dy)) / size);
  2291. var xLen = dx / num;
  2292. var yLen = dy / num;
  2293. var count = 0;
  2294. do {
  2295. (count % 2 == 0 && count > 0) ? co.lineTo(x1, y1) : co.moveTo(x1, y1);
  2296. x1 += xLen;
  2297. y1 += yLen;
  2298. } while(count++ <= num);
  2299. }
  2300. /**
  2301. * Makes an AJAX call. It calls the given callback (a function) when ready
  2302. *
  2303. * @param string url The URL to retrieve
  2304. * @param function callback A function that is called when the response is ready, there's an example below
  2305. * called "myCallback".
  2306. */
  2307. RGraph.AJAX = function (url, callback)
  2308. {
  2309. // Mozilla, Safari, ...
  2310. if (window.XMLHttpRequest) {
  2311. var httpRequest = new XMLHttpRequest();
  2312. // MSIE
  2313. } else if (window.ActiveXObject) {
  2314. var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  2315. }
  2316. httpRequest.onreadystatechange = function ()
  2317. {
  2318. if (this.readyState == 4 && this.status == 200) {
  2319. this.__user_callback__ = callback;
  2320. this.__user_callback__(this.responseText);
  2321. }
  2322. }
  2323. httpRequest.open('GET', url, true);
  2324. httpRequest.send();
  2325. }
  2326. /**
  2327. * Makes an AJAX POST request. It calls the given callback (a function) when ready
  2328. *
  2329. * @param string url The URL to retrieve
  2330. * @param object data The POST data
  2331. * @param function callback A function that is called when the response is ready, there's an example below
  2332. * called "myCallback".
  2333. */
  2334. RGraph.AJAX.POST = function (url, data, callback)
  2335. {
  2336. // Used when building the POST string
  2337. var crumbs = [];
  2338. // Mozilla, Safari, ...
  2339. if (window.XMLHttpRequest) {
  2340. var httpRequest = new XMLHttpRequest();
  2341. // MSIE
  2342. } else if (window.ActiveXObject) {
  2343. var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
  2344. }
  2345. httpRequest.onreadystatechange = function ()
  2346. {
  2347. if (this.readyState == 4 && this.status == 200) {
  2348. this.__user_callback__ = callback;
  2349. this.__user_callback__(this.responseText);
  2350. }
  2351. }
  2352. httpRequest.open('POST', url, true);
  2353. httpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  2354. for (i in data) {
  2355. if (typeof i == 'string') {
  2356. crumbs.push(i + '=' + encodeURIComponent(data[i]));
  2357. }
  2358. }
  2359. httpRequest.send(crumbs.join('&'));
  2360. }
  2361. /**
  2362. * Uses the above function but calls the call back passing a number as its argument
  2363. *
  2364. * @param url string The URL to fetch
  2365. * @param callback function Your callback function (which is passed the number as an argument)
  2366. */
  2367. RGraph.AJAX.getNumber = function (url, callback)
  2368. {
  2369. RGraph.AJAX(url, function ()
  2370. {
  2371. var num = parseFloat(this.responseText);
  2372. callback(num);
  2373. });
  2374. }
  2375. /**
  2376. * Uses the above function but calls the call back passing a string as its argument
  2377. *
  2378. * @param url string The URL to fetch
  2379. * @param callback function Your callback function (which is passed the string as an argument)
  2380. */
  2381. RGraph.AJAX.getString = function (url, callback)
  2382. {
  2383. RGraph.AJAX(url, function ()
  2384. {
  2385. var str = String(this.responseText);
  2386. callback(str);
  2387. });
  2388. }
  2389. /**
  2390. * Uses the above function but calls the call back passing JSON (ie a JavaScript object ) as its argument
  2391. *
  2392. * @param url string The URL to fetch
  2393. * @param callback function Your callback function (which is passed the JSON object as an argument)
  2394. */
  2395. RGraph.AJAX.getJSON = function (url, callback)
  2396. {
  2397. RGraph.AJAX(url, function ()
  2398. {
  2399. var json = eval('(' + this.responseText + ')');
  2400. callback(json);
  2401. });
  2402. }
  2403. /**
  2404. * Uses the above RGraph.AJAX function but calls the call back passing an array as its argument.
  2405. * Useful if you're retrieving CSV data
  2406. *
  2407. * @param url string The URL to fetch
  2408. * @param callback function Your callback function (which is passed the CSV/array as an argument)
  2409. */
  2410. RGraph.AJAX.getCSV = function (url, callback)
  2411. {
  2412. var seperator = arguments[2] ? arguments[2] : ',';
  2413. RGraph.AJAX(url, function ()
  2414. {
  2415. var regexp = new RegExp(seperator);
  2416. var arr = this.responseText.split(regexp);
  2417. // Convert the strings to numbers
  2418. for (var i=0,len=arr.length;i<len;++i) {
  2419. arr[i] = parseFloat(arr[i]);
  2420. }
  2421. callback(arr);
  2422. });
  2423. }
  2424. /**
  2425. * Rotates the canvas
  2426. *
  2427. * @param object canvas The canvas to rotate
  2428. * @param int x The X coordinate about which to rotate the canvas
  2429. * @param int y The Y coordinate about which to rotate the canvas
  2430. * @param int angle The angle(in RADIANS) to rotate the canvas by
  2431. */
  2432. RGraph.RotateCanvas = function (ca, x, y, angle)
  2433. {
  2434. var co = ca.getContext('2d');
  2435. co.translate(x, y);
  2436. co.rotate(angle);
  2437. co.translate(0 - x, 0 - y);
  2438. }
  2439. /**
  2440. * Measures text by creating a DIV in the document and adding the relevant text to it.
  2441. * Then checking the .offsetWidth and .offsetHeight.
  2442. *
  2443. * @param string text The text to measure
  2444. * @param bool bold Whether the text is bold or not
  2445. * @param string font The font to use
  2446. * @param size number The size of the text (in pts)
  2447. * @return array A two element array of the width and height of the text
  2448. */
  2449. RGraph.MeasureText = function (text, bold, font, size)
  2450. {
  2451. // Add the sizes to the cache as adding DOM elements is costly and causes slow downs
  2452. if (typeof(__rgraph_measuretext_cache__) == 'undefined') {
  2453. __rgraph_measuretext_cache__ = [];
  2454. }
  2455. var str = text + ':' + bold + ':' + font + ':' + size;
  2456. if (typeof(__rgraph_measuretext_cache__) == 'object' && __rgraph_measuretext_cache__[str]) {
  2457. return __rgraph_measuretext_cache__[str];
  2458. }
  2459. if (!__rgraph_measuretext_cache__['text-div']) {
  2460. var div = document.createElement('DIV');
  2461. div.style.position = 'absolute';
  2462. div.style.top = '-100px';
  2463. div.style.left = '-100px';
  2464. document.body.appendChild(div);
  2465. // Now store the newly created DIV
  2466. __rgraph_measuretext_cache__['text-div'] = div;
  2467. } else if (__rgraph_measuretext_cache__['text-div']) {
  2468. var div = __rgraph_measuretext_cache__['text-div'];
  2469. }
  2470. div.innerHTML = text.replace(/\r\n/g, '<br />');
  2471. div.style.fontFamily = font;
  2472. div.style.fontWeight = bold ? 'bold' : 'normal';
  2473. div.style.fontSize = size + 'pt';
  2474. var size = [div.offsetWidth, div.offsetHeight];
  2475. //document.body.removeChild(div);
  2476. __rgraph_measuretext_cache__[str] = size;
  2477. return size;
  2478. }
  2479. /* New text function. Accepts two arguments:
  2480. * o obj - The chart object
  2481. * o opt - An object/hash/map of properties. This can consist of:
  2482. * x The X coordinate (REQUIRED)
  2483. * y The Y coordinate (REQUIRED)
  2484. * text The text to show (REQUIRED)
  2485. * font The font to use
  2486. * size The size of the text (in pt)
  2487. * bold Whether the text shouldd be bold or not
  2488. * marker Whether to show a marker that indicates the X/Y coordinates
  2489. * valign The vertical alignment
  2490. * halign The horizontal alignment
  2491. * bounding Whether to draw a bounding box for the text
  2492. * boundingStroke The strokeStyle of the bounding box
  2493. * boundingFill The fillStyle of the bounding box
  2494. */
  2495. RGraph.Text2 = function (obj, opt)
  2496. {
  2497. /**
  2498. * An RGraph object can be given, or a string or the 2D rendering context
  2499. * The coords are placed on the obj.coordsText variable ONLY if it's an RGraph object. The function
  2500. * still returns the cooords though in all cases.
  2501. */
  2502. if (obj && obj.isRGraph) {
  2503. var co = obj.context;
  2504. var ca = obj.canvas;
  2505. } else if (typeof obj == 'string') {
  2506. var ca = document.getElementById(obj);
  2507. var co = ca.getContext('2d');
  2508. } else if (typeof obj.getContext == 'function') {
  2509. var ca = obj;
  2510. var co = ca.getContext('2d');
  2511. } else if (obj.toString().indexOf('CanvasRenderingContext2D') != -1) {
  2512. var co = obj;
  2513. var ca = obj.context;
  2514. }
  2515. var x = opt.x;
  2516. var y = opt.y;
  2517. var originalX = x;
  2518. var originalY = y;
  2519. var text = opt.text;
  2520. var text_multiline = text.split(/\r?\n/g);
  2521. var numlines = text_multiline.length;
  2522. var font = opt.font ? opt.font : 'Arial';
  2523. var size = opt.size ? opt.size : 10;
  2524. var size_pixels = size * 1.5;
  2525. var bold = opt.bold;
  2526. var halign = opt.halign ? opt.halign : 'left';
  2527. var valign = opt.valign ? opt.valign : 'bottom';
  2528. var tag = typeof opt.tag == 'string' && opt.tag.length > 0 ? opt.tag : '';
  2529. var marker = opt.marker;
  2530. var angle = opt.angle || 0;
  2531. /**
  2532. * Changed the name of boundingFill/boundingStroke - this allows you to still use those names
  2533. */
  2534. if (typeof opt.boundingFill == 'string') opt['bounding.fill'] = opt.boundingFill;
  2535. if (typeof opt.boundingStroke == 'string') opt['bounding.stroke'] = opt.boundingStroke;
  2536. var bounding = opt.bounding;
  2537. var bounding_stroke = opt['bounding.stroke'] ? opt['bounding.stroke'] : 'black';
  2538. var bounding_fill = opt['bounding.fill'] ? opt['bounding.fill'] : 'rgba(255,255,255,0.7)';
  2539. var bounding_shadow = opt['bounding.shadow'];
  2540. var bounding_shadow_color = opt['bounding.shadow.color'] || '#ccc';
  2541. var bounding_shadow_blur = opt['bounding.shadow.blur'] || 3;
  2542. var bounding_shadow_offsetx = opt['bounding.shadow.offsetx'] || 3;
  2543. var bounding_shadow_offsety = opt['bounding.shadow.offsety'] || 3;
  2544. var bounding_linewidth = opt['bounding.linewidth'] || 1;
  2545. /**
  2546. * Initialize the return value to an empty object
  2547. */
  2548. var ret = {};
  2549. /**
  2550. * The text arg must be a string or a number
  2551. */
  2552. if (typeof text == 'number') {
  2553. text = String(text);
  2554. }
  2555. if (typeof text != 'string') {
  2556. alert('[RGRAPH TEXT] The text given must a string or a number');
  2557. return;
  2558. }
  2559. /**
  2560. * This facilitates vertical text
  2561. */
  2562. if (angle != 0) {
  2563. co.save();
  2564. co.translate(x, y);
  2565. co.rotate((Math.PI / 180) * angle)
  2566. x = 0;
  2567. y = 0;
  2568. }
  2569. /**
  2570. * Set the font
  2571. */
  2572. co.font = (opt.bold ? 'bold ' : '') + size + 'pt ' + font;
  2573. /**
  2574. * Measure the width/height. This must be done AFTER the font has been set
  2575. */
  2576. var width=0;
  2577. for (var i=0; i<numlines; ++i) {
  2578. width = Math.max(width, co.measureText(text_multiline[i]).width);
  2579. }
  2580. var height = size_pixels * numlines;
  2581. /**
  2582. * Accommodate old MSIE 7/8
  2583. */
  2584. //if (document.all && ISOLD) {
  2585. //y += 2;
  2586. //}
  2587. /**
  2588. * If marker is specified draw a marker at the X/Y coordinates
  2589. */
  2590. if (opt.marker) {
  2591. var marker_size = 10;
  2592. var strokestyle = co.strokeStyle;
  2593. co.beginPath();
  2594. co.strokeStyle = 'red';
  2595. co.moveTo(x, y - marker_size);
  2596. co.lineTo(x, y + marker_size);
  2597. co.moveTo(x - marker_size, y);
  2598. co.lineTo(x + marker_size, y);
  2599. co.stroke();
  2600. co.strokeStyle = strokestyle;
  2601. }
  2602. /**
  2603. * Set the horizontal alignment
  2604. */
  2605. if (halign == 'center') {
  2606. co.textAlign = 'center';
  2607. var boundingX = x - 2 - (width / 2);
  2608. } else if (halign == 'right') {
  2609. co.textAlign = 'right';
  2610. var boundingX = x - 2 - width;
  2611. } else {
  2612. co.textAlign = 'left';
  2613. var boundingX = x - 2;
  2614. }
  2615. /**
  2616. * Set the vertical alignment
  2617. */
  2618. if (valign == 'center') {
  2619. co.textBaseline = 'middle';
  2620. // Move the text slightly
  2621. y -= 1;
  2622. y -= ((numlines - 1) / 2) * size_pixels;
  2623. var boundingY = y - (size_pixels / 2) - 2;
  2624. } else if (valign == 'top') {
  2625. co.textBaseline = 'top';
  2626. var boundingY = y - 2;
  2627. } else {
  2628. co.textBaseline = 'bottom';
  2629. // Move the Y coord if multiline text
  2630. if (numlines > 1) {
  2631. y -= ((numlines - 1) * size_pixels);
  2632. }
  2633. var boundingY = y - size_pixels - 2;
  2634. }
  2635. var boundingW = width + 4;
  2636. var boundingH = height + 4;
  2637. /**
  2638. * Draw a bounding box if required
  2639. */
  2640. if (bounding) {
  2641. var pre_bounding_linewidth = co.lineWidth;
  2642. var pre_bounding_strokestyle = co.strokeStyle;
  2643. var pre_bounding_fillstyle = co.fillStyle;
  2644. var pre_bounding_shadowcolor = co.shadowColor;
  2645. var pre_bounding_shadowblur = co.shadowBlur;
  2646. var pre_bounding_shadowoffsetx = co.shadowOffsetX;
  2647. var pre_bounding_shadowoffsety = co.shadowOffsetY;
  2648. co.lineWidth = bounding_linewidth;
  2649. co.strokeStyle = bounding_stroke;
  2650. co.fillStyle = bounding_fill;
  2651. if (bounding_shadow) {
  2652. co.shadowColor = bounding_shadow_color;
  2653. co.shadowBlur = bounding_shadow_blur;
  2654. co.shadowOffsetX = bounding_shadow_offsetx;
  2655. co.shadowOffsetY = bounding_shadow_offsety;
  2656. }
  2657. //obj.context.strokeRect(boundingX, boundingY, width + 6, (size_pixels * numlines) + 4);
  2658. //obj.context.fillRect(boundingX, boundingY, width + 6, (size_pixels * numlines) + 4);
  2659. co.strokeRect(boundingX, boundingY, boundingW, boundingH);
  2660. co.fillRect(boundingX, boundingY, boundingW, boundingH);
  2661. // Reset the linewidth,colors and shadow to it's original setting
  2662. co.lineWidth = pre_bounding_linewidth;
  2663. co.strokeStyle = pre_bounding_strokestyle;
  2664. co.fillStyle = pre_bounding_fillstyle;
  2665. co.shadowColor = pre_bounding_shadowcolor
  2666. co.shadowBlur = pre_bounding_shadowblur
  2667. co.shadowOffsetX = pre_bounding_shadowoffsetx
  2668. co.shadowOffsetY = pre_bounding_shadowoffsety
  2669. }
  2670. /**
  2671. * Draw the text
  2672. */
  2673. if (numlines > 1) {
  2674. for (var i=0; i<numlines; ++i) {
  2675. co.fillText(text_multiline[i], x, y + (size_pixels * i));
  2676. }
  2677. } else {
  2678. co.fillText(text, x, y);
  2679. }
  2680. /**
  2681. * If the text is at 90 degrees restore() the canvas - getting rid of the rotation
  2682. * and the translate that we did
  2683. */
  2684. if (angle != 0) {
  2685. if (angle == 90) {
  2686. if (halign == 'left') {
  2687. if (valign == 'bottom') {boundingX = originalX - 2; boundingY = originalY - 2; boundingW = height + 4; boundingH = width + 4;}
  2688. if (valign == 'center') {boundingX = originalX - (height / 2) - 2; boundingY = originalY - 2; boundingW = height + 4; boundingH = width + 4;}
  2689. if (valign == 'top') {boundingX = originalX - height - 2; boundingY = originalY - 2; boundingW = height + 4; boundingH = width + 4;}
  2690. } else if (halign == 'center') {
  2691. if (valign == 'bottom') {boundingX = originalX - 2; boundingY = originalY - (width / 2) - 2; boundingW = height + 4; boundingH = width + 4;}
  2692. if (valign == 'center') {boundingX = originalX - (height / 2) - 2; boundingY = originalY - (width / 2) - 2; boundingW = height + 4; boundingH = width + 4;}
  2693. if (valign == 'top') {boundingX = originalX - height - 2; boundingY = originalY - (width / 2) - 2; boundingW = height + 4; boundingH = width + 4;}
  2694. } else if (halign == 'right') {
  2695. if (valign == 'bottom') {boundingX = originalX - 2; boundingY = originalY - width - 2; boundingW = height + 4; boundingH = width + 4;}
  2696. if (valign == 'center') {boundingX = originalX - (height / 2) - 2; boundingY = originalY - width - 2; boundingW = height + 4; boundingH = width + 4;}
  2697. if (valign == 'top') {boundingX = originalX - height - 2; boundingY = originalY - width - 2; boundingW = height + 4; boundingH = width + 4;}
  2698. }
  2699. } else if (angle == 180) {
  2700. if (halign == 'left') {
  2701. if (valign == 'bottom') {boundingX = originalX - width - 2; boundingY = originalY - 2; boundingW = width + 4; boundingH = height + 4;}
  2702. if (valign == 'center') {boundingX = originalX - width - 2; boundingY = originalY - (height / 2) - 2; boundingW = width + 4; boundingH = height + 4;}
  2703. if (valign == 'top') {boundingX = originalX - width - 2; boundingY = originalY - height - 2; boundingW = width + 4; boundingH = height + 4;}
  2704. } else if (halign == 'center') {
  2705. if (valign == 'bottom') {boundingX = originalX - (width / 2) - 2; boundingY = originalY - 2; boundingW = width + 4; boundingH = height + 4;}
  2706. if (valign == 'center') {boundingX = originalX - (width / 2) - 2; boundingY = originalY - (height / 2) - 2; boundingW = width + 4; boundingH = height + 4;}
  2707. if (valign == 'top') {boundingX = originalX - (width / 2) - 2; boundingY = originalY - height - 2; boundingW = width + 4; boundingH = height + 4;}
  2708. } else if (halign == 'right') {
  2709. if (valign == 'bottom') {boundingX = originalX - 2; boundingY = originalY - 2; boundingW = width + 4; boundingH = height + 4;}
  2710. if (valign == 'center') {boundingX = originalX - 2; boundingY = originalY - (height / 2) - 2; boundingW = width + 4; boundingH = height + 4;}
  2711. if (valign == 'top') {boundingX = originalX - 2; boundingY = originalY - height - 2; boundingW = width + 4; boundingH = height + 4;}
  2712. }
  2713. } else if (angle == 270) {
  2714. if (halign == 'left') {
  2715. if (valign == 'bottom') {boundingX = originalX - height - 2; boundingY = originalY - width - 2; boundingW = height + 4; boundingH = width + 4;}
  2716. if (valign == 'center') {boundingX = originalX - (height / 2) - 4; boundingY = originalY - width - 2; boundingW = height + 4; boundingH = width + 4;}
  2717. if (valign == 'top') {boundingX = originalX - 2; boundingY = originalY - width - 2; boundingW = height + 4; boundingH = width + 4;}
  2718. } else if (halign == 'center') {
  2719. if (valign == 'bottom') {boundingX = originalX - height - 2; boundingY = originalY - (width/2) - 2; boundingW = height + 4; boundingH = width + 4;}
  2720. if (valign == 'center') {boundingX = originalX - (height/2) - 4; boundingY = originalY - (width/2) - 2; boundingW = height + 4; boundingH = width + 4;}
  2721. if (valign == 'top') {boundingX = originalX - 2; boundingY = originalY - (width/2) - 2; boundingW = height + 4; boundingH = width + 4;}
  2722. } else if (halign == 'right') {
  2723. if (valign == 'bottom') {boundingX = originalX - height - 2; boundingY = originalY - 2; boundingW = height + 4; boundingH = width + 4;}
  2724. if (valign == 'center') {boundingX = originalX - (height/2) - 2; boundingY = originalY - 2; boundingW = height + 4; boundingH = width + 4;}
  2725. if (valign == 'top') {boundingX = originalX - 2; boundingY = originalY - 2; boundingW = height + 4; boundingH = width + 4;}
  2726. }
  2727. }
  2728. co.restore();
  2729. }
  2730. /**
  2731. * Reset the text alignment so that text rendered
  2732. */
  2733. co.textBaseline = 'alphabetic';
  2734. co.textAlign = 'left';
  2735. /**
  2736. * Fill the ret variable with details of the text
  2737. */
  2738. ret.x = boundingX;
  2739. ret.y = boundingY;
  2740. ret.width = boundingW;
  2741. ret.height = boundingH
  2742. ret.object = obj;
  2743. ret.text = text;
  2744. ret.tag = tag;
  2745. /**
  2746. * Save and then return the details of the text (but oly
  2747. * if it's an RGraph object that was given)
  2748. */
  2749. if (obj && obj.isRGraph && obj.coordsText) {
  2750. obj.coordsText.push(ret);
  2751. }
  2752. return ret;
  2753. }
  2754. /**
  2755. * Takes a sequential index abd returns the group/index variation of it. Eg if you have a
  2756. * sequential index from a grouped bar chart this function can be used to convert that into
  2757. * an appropriate group/index combination
  2758. *
  2759. * @param nindex number The sequential index
  2760. * @param data array The original data (which is grouped)
  2761. * @return The group/index information
  2762. */
  2763. RGraph.sequentialIndexToGrouped = function (index, data)
  2764. {
  2765. var group = 0;
  2766. var grouped_index = 0;
  2767. while (--index >= 0) {
  2768. if (RGraph.is_null(data[group])) {
  2769. group++;
  2770. grouped_index = 0;
  2771. continue;
  2772. }
  2773. // Allow for numbers as well as arrays in the dataset
  2774. if (typeof data[group] == 'number') {
  2775. group++
  2776. grouped_index = 0;
  2777. continue;
  2778. }
  2779. grouped_index++;
  2780. if (grouped_index >= data[group].length) {
  2781. group++;
  2782. grouped_index = 0;
  2783. }
  2784. }
  2785. return [group, grouped_index];
  2786. }
  2787. /**
  2788. * Similar to the jQuery each() function - this lets you iterate easily over an array. The 'this' variable is set]
  2789. * to the array in the callback function.
  2790. *
  2791. * @param array arr The array
  2792. * @param function func The function to call
  2793. * @param object Optionally you can specify the object that the "this" variable is set to
  2794. */
  2795. RGraph.each = function (arr, func)
  2796. {
  2797. for(var i=0, len=arr.length; i<len; i+=1) {
  2798. if (typeof arguments[2] !== 'undefined') {
  2799. var ret = func.call(arguments[2], i, arr[i]);
  2800. } else {
  2801. var ret = func.call(arr, i, arr[i]);
  2802. }
  2803. if (ret === false) {
  2804. return;
  2805. }
  2806. }
  2807. }
  2808. /**
  2809. * Checks whether strings or numbers are empty or not. It also
  2810. * handles null or variables set to undefined. If a variable really
  2811. * is undefined - ie it hasn't been declared at all - you need to use
  2812. * "typeof variable" and check the return value - which will be undefined.
  2813. *
  2814. * @param mixed value The variable to check
  2815. */
  2816. function empty (value)
  2817. {
  2818. if (!value || value.length <= 0) {
  2819. return true;
  2820. }
  2821. return false;
  2822. }
  2823. /**
  2824. * This function highlights a rectangle
  2825. *
  2826. * @param object obj The chart object
  2827. * @param number shape The coordinates of the rect to highlight
  2828. */
  2829. RGraph.Highlight.Rect = function (obj, shape)
  2830. {
  2831. var ca = obj.canvas;
  2832. var co = obj.context;
  2833. var prop = obj.properties;
  2834. if (prop['chart.tooltips.highlight']) {
  2835. // Safari seems to need this
  2836. co.lineWidth = 1;
  2837. /**
  2838. * Draw a rectangle on the canvas to highlight the appropriate area
  2839. */
  2840. co.beginPath();
  2841. co.strokeStyle = prop['chart.highlight.stroke'];
  2842. co.fillStyle = prop['chart.highlight.fill'];
  2843. co.strokeRect(shape['x'],shape['y'],shape['width'],shape['height']);
  2844. co.fillRect(shape['x'],shape['y'],shape['width'],shape['height']);
  2845. co.stroke;
  2846. co.fill();
  2847. }
  2848. }
  2849. /**
  2850. * This function highlights a point
  2851. *
  2852. * @param object obj The chart object
  2853. * @param number shape The coordinates of the rect to highlight
  2854. */
  2855. RGraph.Highlight.Point = function (obj, shape)
  2856. {
  2857. var prop = obj.properties;
  2858. var ca = obj.canvas;
  2859. var co = obj.context;
  2860. if (prop['chart.tooltips.highlight']) {
  2861. /**
  2862. * Draw a rectangle on the canvas to highlight the appropriate area
  2863. */
  2864. co.beginPath();
  2865. co.strokeStyle = prop['chart.highlight.stroke'];
  2866. co.fillStyle = prop['chart.highlight.fill'];
  2867. var radius = prop['chart.highlight.point.radius'] || 2;
  2868. co.arc(shape['x'],shape['y'],radius, 0, TWOPI, 0);
  2869. co.stroke();
  2870. co.fill();
  2871. }
  2872. }
  2873. RGraph.HTML = {
  2874. /**
  2875. * Creates an HTML tag
  2876. *
  2877. * @param string type
  2878. * @param obj parent
  2879. * @param obj
  2880. * @param obj
  2881. */
  2882. create: function (type, parent)
  2883. {
  2884. var obj = document.createElement(type);
  2885. // Add the attributes
  2886. if (arguments[2]) {
  2887. this.attr(obj, arguments[2]);
  2888. }
  2889. // Add the styles
  2890. if (arguments[3]) {
  2891. this.css(obj, arguments[3]);
  2892. }
  2893. /**
  2894. * Add the tag to the document
  2895. */
  2896. parent.appendChild(obj);
  2897. return obj;
  2898. },
  2899. /**
  2900. * Sets attributes on a HTML object
  2901. *
  2902. * @param object obj
  2903. * @param object attr
  2904. */
  2905. attr: function (obj, attr)
  2906. {
  2907. for (i in attr) {
  2908. if (typeof i == 'string') {
  2909. obj[i] = attr[i];
  2910. }
  2911. }
  2912. },
  2913. /**
  2914. * Sets CSS on a HTML object
  2915. *
  2916. * @param object obj
  2917. * @param object css
  2918. */
  2919. css: function (obj, styles)
  2920. {
  2921. var style = obj.style;
  2922. for (i in styles) {
  2923. if (typeof i == 'string') {
  2924. style[i] = styles[i];
  2925. }
  2926. }
  2927. }
  2928. }
  2929. // Some other functions. Because they're rarely changed - they're hand minified
  2930. RGraph.LinearGradient=function(obj,x1,y1,x2,y2,color1,color2){var gradient=obj.context.createLinearGradient(x1,y1,x2,y2);var numColors=arguments.length-5;for (var i=5;i<arguments.length;++i){var color=arguments[i];var stop=(i-5)/(numColors-1);gradient.addColorStop(stop,color);}return gradient;}
  2931. RGraph.RadialGradient=function(obj,x1,y1,r1,x2,y2,r2,color1,color2){var gradient=obj.context.createRadialGradient(x1,y1,r1,x2,y2,r2);var numColors=arguments.length-7;for(var i=7;i<arguments.length; ++i){var color=arguments[i];var stop=(i-7)/(numColors-1);gradient.addColorStop(stop,color);}return gradient;}
  2932. RGraph.array_shift=function(arr){var ret=[];for(var i=1;i<arr.length;++i){ret.push(arr[i]);}return ret;}
  2933. RGraph.AddEventListener=function(id,e,func){var type=arguments[3]?arguments[3]:'unknown';RGraph.Registry.Get('chart.event.handlers').push([id,e,func,type]);}
  2934. RGraph.ClearEventListeners=function(id){if(id&&id=='window'){window.removeEventListener('mousedown',window.__rgraph_mousedown_event_listener_installed__,false);window.removeEventListener('mouseup',window.__rgraph_mouseup_event_listener_installed__,false);}else{var canvas = document.getElementById(id);canvas.removeEventListener('mouseup',canvas.__rgraph_mouseup_event_listener_installed__,false);canvas.removeEventListener('mousemove',canvas.__rgraph_mousemove_event_listener_installed__,false);canvas.removeEventListener('mousedown',canvas.__rgraph_mousedown_event_listener_installed__,false);canvas.removeEventListener('click',canvas.__rgraph_click_event_listener_installed__,false);}}
  2935. RGraph.HidePalette=function(){var div=RGraph.Registry.Get('palette');if(typeof(div)=='object'&&div){div.style.visibility='hidden';div.style.display='none';RGraph.Registry.Set('palette',null);}}
  2936. RGraph.random=function(min,max){var dp=arguments[2]?arguments[2]:0;var r=Math.random();return Number((((max - min) * r) + min).toFixed(dp));}
  2937. RGraph.random.array=function(num,min,max){var arr = [];for(var i=0;i<num;i++)arr.push(RGraph.random(min,max));return arr;}
  2938. RGraph.NoShadow=function(obj){obj.context.shadowColor='rgba(0,0,0,0)';obj.context.shadowBlur=0;obj.context.shadowOffsetX=0;obj.context.shadowOffsetY=0;}
  2939. RGraph.SetShadow=function(obj,color,offsetx,offsety,blur){obj.context.shadowColor=color;obj.context.shadowOffsetX=offsetx;obj.context.shadowOffsetY=offsety;obj.context.shadowBlur=blur;}
  2940. RGraph.array_reverse=function(arr){var newarr=[];for(var i=arr.length-1;i>=0;i--){newarr.push(arr[i]);}return newarr;}
  2941. RGraph.Registry.Set=function(name,value){RGraph.Registry.store[name]=value;return value;}
  2942. RGraph.Registry.Get=function(name){return RGraph.Registry.store[name];}
  2943. RGraph.degrees2Radians=function(degrees){return degrees*(PI/180);}
  2944. RGraph.log=(function(n,base){var log=Math.log;return function(n,base){return log(n)/(base?log(base):1);};})();
  2945. RGraph.is_array=function(obj){return obj!=null&&obj.constructor.toString().indexOf('Array')!=-1;}
  2946. RGraph.trim=function(str){return RGraph.ltrim(RGraph.rtrim(str));}
  2947. RGraph.ltrim=function(str){return str.replace(/^(\s|\0)+/, '');}
  2948. RGraph.rtrim=function(str){return str.replace(/(\s|\0)+$/, '');}
  2949. RGraph.GetHeight=function(obj){return obj.canvas.height;}
  2950. RGraph.GetWidth=function(obj){return obj.canvas.width;}
  2951. RGraph.is_null=function(arg){if(arg==null||(typeof(arg))=='object'&&!arg){return true;}return false;}
  2952. RGraph.Timer=function(label){if(typeof(RGraph.TIMER_LAST_CHECKPOINT)=='undefined'){RGraph.TIMER_LAST_CHECKPOINT=Date.now();}var now=Date.now();console.log(label+': '+(now-RGraph.TIMER_LAST_CHECKPOINT).toString());RGraph.TIMER_LAST_CHECKPOINT=now;}
  2953. RGraph.Async=function(func){return setTimeout(func,arguments[1]?arguments[1]:1);}
  2954. RGraph.isIE=function(){return navigator.userAgent.indexOf('MSIE')>0;};ISIE=RGraph.isIE();
  2955. RGraph.isIE6=function(){return navigator.userAgent.indexOf('MSIE 6')>0;};ISIE6=RGraph.isIE6();
  2956. RGraph.isIE7=function(){return navigator.userAgent.indexOf('MSIE 7')>0;};ISIE7=RGraph.isIE7();
  2957. RGraph.isIE8=function(){return navigator.userAgent.indexOf('MSIE 8')>0;};ISIE8=RGraph.isIE8();
  2958. RGraph.isIE9=function(){return navigator.userAgent.indexOf('MSIE 9')>0;};ISIE9=RGraph.isIE9();
  2959. RGraph.isIE10=function(){return navigator.userAgent.indexOf('MSIE 10')>0;};ISIE10=RGraph.isIE10();
  2960. RGraph.isIE9up=function(){navigator.userAgent.match(/MSIE (\d+)/);return Number(RegExp.$1)>=9;};ISIE9UP=RGraph.isIE9up();
  2961. RGraph.isIE10up=function(){navigator.userAgent.match(/MSIE (\d+)/);return Number(RegExp.$1)>=10;};ISIE10UP=RGraph.isIE10up();
  2962. RGraph.isOld=function(){return ISIE6||ISIE7||ISIE8;};ISOLD=RGraph.isOld();
  2963. RGraph.Reset=function(canvas){canvas.width=canvas.width;RGraph.ObjectRegistry.Clear(canvas);canvas.__rgraph_aa_translated__=false;}
  2964. function pd(variable){RGraph.pr(variable);}
  2965. function p(variable){RGraph.pr(arguments[0],arguments[1],arguments[3]);}
  2966. function a(variable){alert(variable);}
  2967. function cl(variable){return console.log(variable);}