RGraph.common.core.js 130 KB

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