flot.bundle.js 279 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132
  1. /* Javascript plotting library for jQuery, version 0.8.3.
  2. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  3. Licensed under the MIT license.
  4. */
  5. // first an inline dependency, jquery.colorhelpers.js, we inline it here
  6. // for convenience
  7. /* Plugin for jQuery for working with colors.
  8. *
  9. * Version 1.1.
  10. *
  11. * Inspiration from jQuery color animation plugin by John Resig.
  12. *
  13. * Released under the MIT license by Ole Laursen, October 2009.
  14. *
  15. * Examples:
  16. *
  17. * $.color.parse("#fff").scale('rgb', 0.25).add('a', -0.5).toString()
  18. * var c = $.color.extract($("#mydiv"), 'background-color');
  19. * console.log(c.r, c.g, c.b, c.a);
  20. * $.color.make(100, 50, 25, 0.4).toString() // returns "rgba(100,50,25,0.4)"
  21. *
  22. * Note that .scale() and .add() return the same modified object
  23. * instead of making a new one.
  24. *
  25. * V. 1.1: Fix error handling so e.g. parsing an empty string does
  26. * produce a color rather than just crashing.
  27. */
  28. (function($){$.color={};$.color.make=function(r,g,b,a){var o={};o.r=r||0;o.g=g||0;o.b=b||0;o.a=a!=null?a:1;o.add=function(c,d){for(var i=0;i<c.length;++i)o[c.charAt(i)]+=d;return o.normalize()};o.scale=function(c,f){for(var i=0;i<c.length;++i)o[c.charAt(i)]*=f;return o.normalize()};o.toString=function(){if(o.a>=1){return"rgb("+[o.r,o.g,o.b].join(",")+")"}else{return"rgba("+[o.r,o.g,o.b,o.a].join(",")+")"}};o.normalize=function(){function clamp(min,value,max){return value<min?min:value>max?max:value}o.r=clamp(0,parseInt(o.r),255);o.g=clamp(0,parseInt(o.g),255);o.b=clamp(0,parseInt(o.b),255);o.a=clamp(0,o.a,1);return o};o.clone=function(){return $.color.make(o.r,o.b,o.g,o.a)};return o.normalize()};$.color.extract=function(elem,css){var c;do{c=elem.css(css).toLowerCase();if(c!=""&&c!="transparent")break;elem=elem.parent()}while(elem.length&&!$.nodeName(elem.get(0),"body"));if(c=="rgba(0, 0, 0, 0)")c="transparent";return $.color.parse(c)};$.color.parse=function(str){var res,m=$.color.make;if(res=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(str))return m(parseInt(res[1],10),parseInt(res[2],10),parseInt(res[3],10));if(res=/rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str))return m(parseInt(res[1],10),parseInt(res[2],10),parseInt(res[3],10),parseFloat(res[4]));if(res=/rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(str))return m(parseFloat(res[1])*2.55,parseFloat(res[2])*2.55,parseFloat(res[3])*2.55);if(res=/rgba\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str))return m(parseFloat(res[1])*2.55,parseFloat(res[2])*2.55,parseFloat(res[3])*2.55,parseFloat(res[4]));if(res=/#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(str))return m(parseInt(res[1],16),parseInt(res[2],16),parseInt(res[3],16));if(res=/#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(str))return m(parseInt(res[1]+res[1],16),parseInt(res[2]+res[2],16),parseInt(res[3]+res[3],16));var name=$.trim(str).toLowerCase();if(name=="transparent")return m(255,255,255,0);else{res=lookupColors[name]||[0,0,0];return m(res[0],res[1],res[2])}};var lookupColors={aqua:[0,255,255],azure:[240,255,255],beige:[245,245,220],black:[0,0,0],blue:[0,0,255],brown:[165,42,42],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgrey:[169,169,169],darkgreen:[0,100,0],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkviolet:[148,0,211],fuchsia:[255,0,255],gold:[255,215,0],green:[0,128,0],indigo:[75,0,130],khaki:[240,230,140],lightblue:[173,216,230],lightcyan:[224,255,255],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightyellow:[255,255,224],lime:[0,255,0],magenta:[255,0,255],maroon:[128,0,0],navy:[0,0,128],olive:[128,128,0],orange:[255,165,0],pink:[255,192,203],purple:[128,0,128],violet:[128,0,128],red:[255,0,0],silver:[192,192,192],white:[255,255,255],yellow:[255,255,0]}})(jQuery);
  29. // the actual Flot code
  30. (function($) {
  31. // Cache the prototype hasOwnProperty for faster access
  32. var hasOwnProperty = Object.prototype.hasOwnProperty;
  33. // A shim to provide 'detach' to jQuery versions prior to 1.4. Using a DOM
  34. // operation produces the same effect as detach, i.e. removing the element
  35. // without touching its jQuery data.
  36. // Do not merge this into Flot 0.9, since it requires jQuery 1.4.4+.
  37. if (!$.fn.detach) {
  38. $.fn.detach = function() {
  39. return this.each(function() {
  40. if (this.parentNode) {
  41. this.parentNode.removeChild( this );
  42. }
  43. });
  44. };
  45. }
  46. ///////////////////////////////////////////////////////////////////////////
  47. // The Canvas object is a wrapper around an HTML5 <canvas> tag.
  48. //
  49. // @constructor
  50. // @param {string} cls List of classes to apply to the canvas.
  51. // @param {element} container Element onto which to append the canvas.
  52. //
  53. // Requiring a container is a little iffy, but unfortunately canvas
  54. // operations don't work unless the canvas is attached to the DOM.
  55. function Canvas(cls, container) {
  56. var element = container.children("." + cls)[0];
  57. if (element == null) {
  58. element = document.createElement("canvas");
  59. element.className = cls;
  60. $(element).css({ direction: "ltr", position: "absolute", left: 0, top: 0 })
  61. .appendTo(container);
  62. // If HTML5 Canvas isn't available, fall back to [Ex|Flash]canvas
  63. if (!element.getContext) {
  64. if (window.G_vmlCanvasManager) {
  65. element = window.G_vmlCanvasManager.initElement(element);
  66. } else {
  67. throw new Error("Canvas is not available. If you're using IE with a fall-back such as Excanvas, then there's either a mistake in your conditional include, or the page has no DOCTYPE and is rendering in Quirks Mode.");
  68. }
  69. }
  70. }
  71. this.element = element;
  72. var context = this.context = element.getContext("2d");
  73. // Determine the screen's ratio of physical to device-independent
  74. // pixels. This is the ratio between the canvas width that the browser
  75. // advertises and the number of pixels actually present in that space.
  76. // The iPhone 4, for example, has a device-independent width of 320px,
  77. // but its screen is actually 640px wide. It therefore has a pixel
  78. // ratio of 2, while most normal devices have a ratio of 1.
  79. var devicePixelRatio = window.devicePixelRatio || 1,
  80. backingStoreRatio =
  81. context.webkitBackingStorePixelRatio ||
  82. context.mozBackingStorePixelRatio ||
  83. context.msBackingStorePixelRatio ||
  84. context.oBackingStorePixelRatio ||
  85. context.backingStorePixelRatio || 1;
  86. this.pixelRatio = devicePixelRatio / backingStoreRatio;
  87. // Size the canvas to match the internal dimensions of its container
  88. this.resize(container.width(), container.height());
  89. // Collection of HTML div layers for text overlaid onto the canvas
  90. this.textContainer = null;
  91. this.text = {};
  92. // Cache of text fragments and metrics, so we can avoid expensively
  93. // re-calculating them when the plot is re-rendered in a loop.
  94. this._textCache = {};
  95. }
  96. // Resizes the canvas to the given dimensions.
  97. //
  98. // @param {number} width New width of the canvas, in pixels.
  99. // @param {number} width New height of the canvas, in pixels.
  100. Canvas.prototype.resize = function(width, height) {
  101. if (width <= 0 || height <= 0) {
  102. throw new Error("Invalid dimensions for plot, width = " + width + ", height = " + height);
  103. }
  104. var element = this.element,
  105. context = this.context,
  106. pixelRatio = this.pixelRatio;
  107. // Resize the canvas, increasing its density based on the display's
  108. // pixel ratio; basically giving it more pixels without increasing the
  109. // size of its element, to take advantage of the fact that retina
  110. // displays have that many more pixels in the same advertised space.
  111. // Resizing should reset the state (excanvas seems to be buggy though)
  112. if (this.width != width) {
  113. element.width = width * pixelRatio;
  114. element.style.width = width + "px";
  115. this.width = width;
  116. }
  117. if (this.height != height) {
  118. element.height = height * pixelRatio;
  119. element.style.height = height + "px";
  120. this.height = height;
  121. }
  122. // Save the context, so we can reset in case we get replotted. The
  123. // restore ensure that we're really back at the initial state, and
  124. // should be safe even if we haven't saved the initial state yet.
  125. context.restore();
  126. context.save();
  127. // Scale the coordinate space to match the display density; so even though we
  128. // may have twice as many pixels, we still want lines and other drawing to
  129. // appear at the same size; the extra pixels will just make them crisper.
  130. context.scale(pixelRatio, pixelRatio);
  131. };
  132. // Clears the entire canvas area, not including any overlaid HTML text
  133. Canvas.prototype.clear = function() {
  134. this.context.clearRect(0, 0, this.width, this.height);
  135. };
  136. // Finishes rendering the canvas, including managing the text overlay.
  137. Canvas.prototype.render = function() {
  138. var cache = this._textCache;
  139. // For each text layer, add elements marked as active that haven't
  140. // already been rendered, and remove those that are no longer active.
  141. for (var layerKey in cache) {
  142. if (hasOwnProperty.call(cache, layerKey)) {
  143. var layer = this.getTextLayer(layerKey),
  144. layerCache = cache[layerKey];
  145. layer.hide();
  146. for (var styleKey in layerCache) {
  147. if (hasOwnProperty.call(layerCache, styleKey)) {
  148. var styleCache = layerCache[styleKey];
  149. for (var key in styleCache) {
  150. if (hasOwnProperty.call(styleCache, key)) {
  151. var positions = styleCache[key].positions;
  152. for (var i = 0, position; position = positions[i]; i++) {
  153. if (position.active) {
  154. if (!position.rendered) {
  155. layer.append(position.element);
  156. position.rendered = true;
  157. }
  158. } else {
  159. positions.splice(i--, 1);
  160. if (position.rendered) {
  161. position.element.detach();
  162. }
  163. }
  164. }
  165. if (positions.length == 0) {
  166. delete styleCache[key];
  167. }
  168. }
  169. }
  170. }
  171. }
  172. layer.show();
  173. }
  174. }
  175. };
  176. // Creates (if necessary) and returns the text overlay container.
  177. //
  178. // @param {string} classes String of space-separated CSS classes used to
  179. // uniquely identify the text layer.
  180. // @return {object} The jQuery-wrapped text-layer div.
  181. Canvas.prototype.getTextLayer = function(classes) {
  182. var layer = this.text[classes];
  183. // Create the text layer if it doesn't exist
  184. if (layer == null) {
  185. // Create the text layer container, if it doesn't exist
  186. if (this.textContainer == null) {
  187. this.textContainer = $("<div class='flot-text'></div>")
  188. .css({
  189. position: "absolute",
  190. top: 0,
  191. left: 0,
  192. bottom: 0,
  193. right: 0,
  194. 'font-size': "smaller",
  195. color: "#545454"
  196. })
  197. .insertAfter(this.element);
  198. }
  199. layer = this.text[classes] = $("<div></div>")
  200. .addClass(classes)
  201. .css({
  202. position: "absolute",
  203. top: 0,
  204. left: 0,
  205. bottom: 0,
  206. right: 0
  207. })
  208. .appendTo(this.textContainer);
  209. }
  210. return layer;
  211. };
  212. // Creates (if necessary) and returns a text info object.
  213. //
  214. // The object looks like this:
  215. //
  216. // {
  217. // width: Width of the text's wrapper div.
  218. // height: Height of the text's wrapper div.
  219. // element: The jQuery-wrapped HTML div containing the text.
  220. // positions: Array of positions at which this text is drawn.
  221. // }
  222. //
  223. // The positions array contains objects that look like this:
  224. //
  225. // {
  226. // active: Flag indicating whether the text should be visible.
  227. // rendered: Flag indicating whether the text is currently visible.
  228. // element: The jQuery-wrapped HTML div containing the text.
  229. // x: X coordinate at which to draw the text.
  230. // y: Y coordinate at which to draw the text.
  231. // }
  232. //
  233. // Each position after the first receives a clone of the original element.
  234. //
  235. // The idea is that that the width, height, and general 'identity' of the
  236. // text is constant no matter where it is placed; the placements are a
  237. // secondary property.
  238. //
  239. // Canvas maintains a cache of recently-used text info objects; getTextInfo
  240. // either returns the cached element or creates a new entry.
  241. //
  242. // @param {string} layer A string of space-separated CSS classes uniquely
  243. // identifying the layer containing this text.
  244. // @param {string} text Text string to retrieve info for.
  245. // @param {(string|object)=} font Either a string of space-separated CSS
  246. // classes or a font-spec object, defining the text's font and style.
  247. // @param {number=} angle Angle at which to rotate the text, in degrees.
  248. // Angle is currently unused, it will be implemented in the future.
  249. // @param {number=} width Maximum width of the text before it wraps.
  250. // @return {object} a text info object.
  251. Canvas.prototype.getTextInfo = function(layer, text, font, angle, width) {
  252. var textStyle, layerCache, styleCache, info;
  253. // Cast the value to a string, in case we were given a number or such
  254. text = "" + text;
  255. // If the font is a font-spec object, generate a CSS font definition
  256. if (typeof font === "object") {
  257. textStyle = font.style + " " + font.variant + " " + font.weight + " " + font.size + "px/" + font.lineHeight + "px " + font.family;
  258. } else {
  259. textStyle = font;
  260. }
  261. // Retrieve (or create) the cache for the text's layer and styles
  262. layerCache = this._textCache[layer];
  263. if (layerCache == null) {
  264. layerCache = this._textCache[layer] = {};
  265. }
  266. styleCache = layerCache[textStyle];
  267. if (styleCache == null) {
  268. styleCache = layerCache[textStyle] = {};
  269. }
  270. info = styleCache[text];
  271. // If we can't find a matching element in our cache, create a new one
  272. if (info == null) {
  273. var element = $("<div></div>").html(text)
  274. .css({
  275. position: "absolute",
  276. 'max-width': width,
  277. top: -9999
  278. })
  279. .appendTo(this.getTextLayer(layer));
  280. if (typeof font === "object") {
  281. element.css({
  282. font: textStyle,
  283. color: font.color
  284. });
  285. } else if (typeof font === "string") {
  286. element.addClass(font);
  287. }
  288. info = styleCache[text] = {
  289. width: element.outerWidth(true),
  290. height: element.outerHeight(true),
  291. element: element,
  292. positions: []
  293. };
  294. element.detach();
  295. }
  296. return info;
  297. };
  298. // Adds a text string to the canvas text overlay.
  299. //
  300. // The text isn't drawn immediately; it is marked as rendering, which will
  301. // result in its addition to the canvas on the next render pass.
  302. //
  303. // @param {string} layer A string of space-separated CSS classes uniquely
  304. // identifying the layer containing this text.
  305. // @param {number} x X coordinate at which to draw the text.
  306. // @param {number} y Y coordinate at which to draw the text.
  307. // @param {string} text Text string to draw.
  308. // @param {(string|object)=} font Either a string of space-separated CSS
  309. // classes or a font-spec object, defining the text's font and style.
  310. // @param {number=} angle Angle at which to rotate the text, in degrees.
  311. // Angle is currently unused, it will be implemented in the future.
  312. // @param {number=} width Maximum width of the text before it wraps.
  313. // @param {string=} halign Horizontal alignment of the text; either "left",
  314. // "center" or "right".
  315. // @param {string=} valign Vertical alignment of the text; either "top",
  316. // "middle" or "bottom".
  317. Canvas.prototype.addText = function(layer, x, y, text, font, angle, width, halign, valign) {
  318. var info = this.getTextInfo(layer, text, font, angle, width),
  319. positions = info.positions;
  320. // Tweak the div's position to match the text's alignment
  321. if (halign == "center") {
  322. x -= info.width / 2;
  323. } else if (halign == "right") {
  324. x -= info.width;
  325. }
  326. if (valign == "middle") {
  327. y -= info.height / 2;
  328. } else if (valign == "bottom") {
  329. y -= info.height;
  330. }
  331. // Determine whether this text already exists at this position.
  332. // If so, mark it for inclusion in the next render pass.
  333. for (var i = 0, position; position = positions[i]; i++) {
  334. if (position.x == x && position.y == y) {
  335. position.active = true;
  336. return;
  337. }
  338. }
  339. // If the text doesn't exist at this position, create a new entry
  340. // For the very first position we'll re-use the original element,
  341. // while for subsequent ones we'll clone it.
  342. position = {
  343. active: true,
  344. rendered: false,
  345. element: positions.length ? info.element.clone() : info.element,
  346. x: x,
  347. y: y
  348. };
  349. positions.push(position);
  350. // Move the element to its final position within the container
  351. position.element.css({
  352. top: Math.round(y),
  353. left: Math.round(x),
  354. 'text-align': halign // In case the text wraps
  355. });
  356. };
  357. // Removes one or more text strings from the canvas text overlay.
  358. //
  359. // If no parameters are given, all text within the layer is removed.
  360. //
  361. // Note that the text is not immediately removed; it is simply marked as
  362. // inactive, which will result in its removal on the next render pass.
  363. // This avoids the performance penalty for 'clear and redraw' behavior,
  364. // where we potentially get rid of all text on a layer, but will likely
  365. // add back most or all of it later, as when redrawing axes, for example.
  366. //
  367. // @param {string} layer A string of space-separated CSS classes uniquely
  368. // identifying the layer containing this text.
  369. // @param {number=} x X coordinate of the text.
  370. // @param {number=} y Y coordinate of the text.
  371. // @param {string=} text Text string to remove.
  372. // @param {(string|object)=} font Either a string of space-separated CSS
  373. // classes or a font-spec object, defining the text's font and style.
  374. // @param {number=} angle Angle at which the text is rotated, in degrees.
  375. // Angle is currently unused, it will be implemented in the future.
  376. Canvas.prototype.removeText = function(layer, x, y, text, font, angle) {
  377. if (text == null) {
  378. var layerCache = this._textCache[layer];
  379. if (layerCache != null) {
  380. for (var styleKey in layerCache) {
  381. if (hasOwnProperty.call(layerCache, styleKey)) {
  382. var styleCache = layerCache[styleKey];
  383. for (var key in styleCache) {
  384. if (hasOwnProperty.call(styleCache, key)) {
  385. var positions = styleCache[key].positions;
  386. for (var i = 0, position; position = positions[i]; i++) {
  387. position.active = false;
  388. }
  389. }
  390. }
  391. }
  392. }
  393. }
  394. } else {
  395. var positions = this.getTextInfo(layer, text, font, angle).positions;
  396. for (var i = 0, position; position = positions[i]; i++) {
  397. if (position.x == x && position.y == y) {
  398. position.active = false;
  399. }
  400. }
  401. }
  402. };
  403. ///////////////////////////////////////////////////////////////////////////
  404. // The top-level container for the entire plot.
  405. function Plot(placeholder, data_, options_, plugins) {
  406. // data is on the form:
  407. // [ series1, series2 ... ]
  408. // where series is either just the data as [ [x1, y1], [x2, y2], ... ]
  409. // or { data: [ [x1, y1], [x2, y2], ... ], label: "some label", ... }
  410. var series = [],
  411. options = {
  412. // the color theme used for graphs
  413. colors: ["#edc240", "#afd8f8", "#cb4b4b", "#4da74d", "#9440ed"],
  414. legend: {
  415. show: true,
  416. noColumns: 1, // number of colums in legend table
  417. labelFormatter: null, // fn: string -> string
  418. labelBoxBorderColor: "#ccc", // border color for the little label boxes
  419. container: null, // container (as jQuery object) to put legend in, null means default on top of graph
  420. position: "ne", // position of default legend container within plot
  421. margin: 5, // distance from grid edge to default legend container within plot
  422. backgroundColor: null, // null means auto-detect
  423. backgroundOpacity: 0.85, // set to 0 to avoid background
  424. sorted: null // default to no legend sorting
  425. },
  426. xaxis: {
  427. show: null, // null = auto-detect, true = always, false = never
  428. position: "bottom", // or "top"
  429. mode: null, // null or "time"
  430. font: null, // null (derived from CSS in placeholder) or object like { size: 11, lineHeight: 13, style: "italic", weight: "bold", family: "sans-serif", variant: "small-caps" }
  431. color: null, // base color, labels, ticks
  432. tickColor: null, // possibly different color of ticks, e.g. "rgba(0,0,0,0.15)"
  433. transform: null, // null or f: number -> number to transform axis
  434. inverseTransform: null, // if transform is set, this should be the inverse function
  435. min: null, // min. value to show, null means set automatically
  436. max: null, // max. value to show, null means set automatically
  437. autoscaleMargin: null, // margin in % to add if auto-setting min/max
  438. ticks: null, // either [1, 3] or [[1, "a"], 3] or (fn: axis info -> ticks) or app. number of ticks for auto-ticks
  439. tickFormatter: null, // fn: number -> string
  440. labelWidth: null, // size of tick labels in pixels
  441. labelHeight: null,
  442. reserveSpace: null, // whether to reserve space even if axis isn't shown
  443. tickLength: null, // size in pixels of ticks, or "full" for whole line
  444. alignTicksWithAxis: null, // axis number or null for no sync
  445. tickDecimals: null, // no. of decimals, null means auto
  446. tickSize: null, // number or [number, "unit"]
  447. minTickSize: null // number or [number, "unit"]
  448. },
  449. yaxis: {
  450. autoscaleMargin: 0.02,
  451. position: "left" // or "right"
  452. },
  453. xaxes: [],
  454. yaxes: [],
  455. series: {
  456. points: {
  457. show: false,
  458. radius: 3,
  459. lineWidth: 2, // in pixels
  460. fill: true,
  461. fillColor: "#ffffff",
  462. symbol: "circle" // or callback
  463. },
  464. lines: {
  465. // we don't put in show: false so we can see
  466. // whether lines were actively disabled
  467. lineWidth: 2, // in pixels
  468. fill: false,
  469. fillColor: null,
  470. steps: false
  471. // Omit 'zero', so we can later default its value to
  472. // match that of the 'fill' option.
  473. },
  474. bars: {
  475. show: false,
  476. lineWidth: 2, // in pixels
  477. barWidth: 1, // in units of the x axis
  478. fill: true,
  479. fillColor: null,
  480. align: "left", // "left", "right", or "center"
  481. horizontal: false,
  482. zero: true
  483. },
  484. shadowSize: 3,
  485. highlightColor: null
  486. },
  487. grid: {
  488. show: true,
  489. aboveData: false,
  490. color: "#545454", // primary color used for outline and labels
  491. backgroundColor: null, // null for transparent, else color
  492. borderColor: null, // set if different from the grid color
  493. tickColor: null, // color for the ticks, e.g. "rgba(0,0,0,0.15)"
  494. margin: 0, // distance from the canvas edge to the grid
  495. labelMargin: 5, // in pixels
  496. axisMargin: 8, // in pixels
  497. borderWidth: 2, // in pixels
  498. minBorderMargin: null, // in pixels, null means taken from points radius
  499. markings: null, // array of ranges or fn: axes -> array of ranges
  500. markingsColor: "#f4f4f4",
  501. markingsLineWidth: 2,
  502. // interactive stuff
  503. clickable: false,
  504. hoverable: false,
  505. autoHighlight: true, // highlight in case mouse is near
  506. mouseActiveRadius: 10 // how far the mouse can be away to activate an item
  507. },
  508. interaction: {
  509. redrawOverlayInterval: 1000/60 // time between updates, -1 means in same flow
  510. },
  511. hooks: {}
  512. },
  513. surface = null, // the canvas for the plot itself
  514. overlay = null, // canvas for interactive stuff on top of plot
  515. eventHolder = null, // jQuery object that events should be bound to
  516. ctx = null, octx = null,
  517. xaxes = [], yaxes = [],
  518. plotOffset = { left: 0, right: 0, top: 0, bottom: 0},
  519. plotWidth = 0, plotHeight = 0,
  520. hooks = {
  521. processOptions: [],
  522. processRawData: [],
  523. processDatapoints: [],
  524. processOffset: [],
  525. drawBackground: [],
  526. drawSeries: [],
  527. draw: [],
  528. bindEvents: [],
  529. drawOverlay: [],
  530. shutdown: []
  531. },
  532. plot = this;
  533. // public functions
  534. plot.setData = setData;
  535. plot.setupGrid = setupGrid;
  536. plot.draw = draw;
  537. plot.getPlaceholder = function() { return placeholder; };
  538. plot.getCanvas = function() { return surface.element; };
  539. plot.getPlotOffset = function() { return plotOffset; };
  540. plot.width = function () { return plotWidth; };
  541. plot.height = function () { return plotHeight; };
  542. plot.offset = function () {
  543. var o = eventHolder.offset();
  544. o.left += plotOffset.left;
  545. o.top += plotOffset.top;
  546. return o;
  547. };
  548. plot.getData = function () { return series; };
  549. plot.getAxes = function () {
  550. var res = {}, i;
  551. $.each(xaxes.concat(yaxes), function (_, axis) {
  552. if (axis)
  553. res[axis.direction + (axis.n != 1 ? axis.n : "") + "axis"] = axis;
  554. });
  555. return res;
  556. };
  557. plot.getXAxes = function () { return xaxes; };
  558. plot.getYAxes = function () { return yaxes; };
  559. plot.c2p = canvasToAxisCoords;
  560. plot.p2c = axisToCanvasCoords;
  561. plot.getOptions = function () { return options; };
  562. plot.highlight = highlight;
  563. plot.unhighlight = unhighlight;
  564. plot.triggerRedrawOverlay = triggerRedrawOverlay;
  565. plot.pointOffset = function(point) {
  566. return {
  567. left: parseInt(xaxes[axisNumber(point, "x") - 1].p2c(+point.x) + plotOffset.left, 10),
  568. top: parseInt(yaxes[axisNumber(point, "y") - 1].p2c(+point.y) + plotOffset.top, 10)
  569. };
  570. };
  571. plot.shutdown = shutdown;
  572. plot.destroy = function () {
  573. shutdown();
  574. placeholder.removeData("plot").empty();
  575. series = [];
  576. options = null;
  577. surface = null;
  578. overlay = null;
  579. eventHolder = null;
  580. ctx = null;
  581. octx = null;
  582. xaxes = [];
  583. yaxes = [];
  584. hooks = null;
  585. highlights = [];
  586. plot = null;
  587. };
  588. plot.resize = function () {
  589. var width = placeholder.width(),
  590. height = placeholder.height();
  591. surface.resize(width, height);
  592. overlay.resize(width, height);
  593. };
  594. // public attributes
  595. plot.hooks = hooks;
  596. // initialize
  597. initPlugins(plot);
  598. parseOptions(options_);
  599. setupCanvases();
  600. setData(data_);
  601. setupGrid();
  602. draw();
  603. bindEvents();
  604. function executeHooks(hook, args) {
  605. args = [plot].concat(args);
  606. for (var i = 0; i < hook.length; ++i)
  607. hook[i].apply(this, args);
  608. }
  609. function initPlugins() {
  610. // References to key classes, allowing plugins to modify them
  611. var classes = {
  612. Canvas: Canvas
  613. };
  614. for (var i = 0; i < plugins.length; ++i) {
  615. var p = plugins[i];
  616. p.init(plot, classes);
  617. if (p.options)
  618. $.extend(true, options, p.options);
  619. }
  620. }
  621. function parseOptions(opts) {
  622. $.extend(true, options, opts);
  623. // $.extend merges arrays, rather than replacing them. When less
  624. // colors are provided than the size of the default palette, we
  625. // end up with those colors plus the remaining defaults, which is
  626. // not expected behavior; avoid it by replacing them here.
  627. if (opts && opts.colors) {
  628. options.colors = opts.colors;
  629. }
  630. if (options.xaxis.color == null)
  631. options.xaxis.color = $.color.parse(options.grid.color).scale('a', 0.22).toString();
  632. if (options.yaxis.color == null)
  633. options.yaxis.color = $.color.parse(options.grid.color).scale('a', 0.22).toString();
  634. if (options.xaxis.tickColor == null) // grid.tickColor for back-compatibility
  635. options.xaxis.tickColor = options.grid.tickColor || options.xaxis.color;
  636. if (options.yaxis.tickColor == null) // grid.tickColor for back-compatibility
  637. options.yaxis.tickColor = options.grid.tickColor || options.yaxis.color;
  638. if (options.grid.borderColor == null)
  639. options.grid.borderColor = options.grid.color;
  640. if (options.grid.tickColor == null)
  641. options.grid.tickColor = $.color.parse(options.grid.color).scale('a', 0.22).toString();
  642. // Fill in defaults for axis options, including any unspecified
  643. // font-spec fields, if a font-spec was provided.
  644. // If no x/y axis options were provided, create one of each anyway,
  645. // since the rest of the code assumes that they exist.
  646. var i, axisOptions, axisCount,
  647. fontSize = placeholder.css("font-size"),
  648. fontSizeDefault = fontSize ? +fontSize.replace("px", "") : 13,
  649. fontDefaults = {
  650. style: placeholder.css("font-style"),
  651. size: Math.round(0.8 * fontSizeDefault),
  652. variant: placeholder.css("font-variant"),
  653. weight: placeholder.css("font-weight"),
  654. family: placeholder.css("font-family")
  655. };
  656. axisCount = options.xaxes.length || 1;
  657. for (i = 0; i < axisCount; ++i) {
  658. axisOptions = options.xaxes[i];
  659. if (axisOptions && !axisOptions.tickColor) {
  660. axisOptions.tickColor = axisOptions.color;
  661. }
  662. axisOptions = $.extend(true, {}, options.xaxis, axisOptions);
  663. options.xaxes[i] = axisOptions;
  664. if (axisOptions.font) {
  665. axisOptions.font = $.extend({}, fontDefaults, axisOptions.font);
  666. if (!axisOptions.font.color) {
  667. axisOptions.font.color = axisOptions.color;
  668. }
  669. if (!axisOptions.font.lineHeight) {
  670. axisOptions.font.lineHeight = Math.round(axisOptions.font.size * 1.15);
  671. }
  672. }
  673. }
  674. axisCount = options.yaxes.length || 1;
  675. for (i = 0; i < axisCount; ++i) {
  676. axisOptions = options.yaxes[i];
  677. if (axisOptions && !axisOptions.tickColor) {
  678. axisOptions.tickColor = axisOptions.color;
  679. }
  680. axisOptions = $.extend(true, {}, options.yaxis, axisOptions);
  681. options.yaxes[i] = axisOptions;
  682. if (axisOptions.font) {
  683. axisOptions.font = $.extend({}, fontDefaults, axisOptions.font);
  684. if (!axisOptions.font.color) {
  685. axisOptions.font.color = axisOptions.color;
  686. }
  687. if (!axisOptions.font.lineHeight) {
  688. axisOptions.font.lineHeight = Math.round(axisOptions.font.size * 1.15);
  689. }
  690. }
  691. }
  692. // backwards compatibility, to be removed in future
  693. if (options.xaxis.noTicks && options.xaxis.ticks == null)
  694. options.xaxis.ticks = options.xaxis.noTicks;
  695. if (options.yaxis.noTicks && options.yaxis.ticks == null)
  696. options.yaxis.ticks = options.yaxis.noTicks;
  697. if (options.x2axis) {
  698. options.xaxes[1] = $.extend(true, {}, options.xaxis, options.x2axis);
  699. options.xaxes[1].position = "top";
  700. // Override the inherit to allow the axis to auto-scale
  701. if (options.x2axis.min == null) {
  702. options.xaxes[1].min = null;
  703. }
  704. if (options.x2axis.max == null) {
  705. options.xaxes[1].max = null;
  706. }
  707. }
  708. if (options.y2axis) {
  709. options.yaxes[1] = $.extend(true, {}, options.yaxis, options.y2axis);
  710. options.yaxes[1].position = "right";
  711. // Override the inherit to allow the axis to auto-scale
  712. if (options.y2axis.min == null) {
  713. options.yaxes[1].min = null;
  714. }
  715. if (options.y2axis.max == null) {
  716. options.yaxes[1].max = null;
  717. }
  718. }
  719. if (options.grid.coloredAreas)
  720. options.grid.markings = options.grid.coloredAreas;
  721. if (options.grid.coloredAreasColor)
  722. options.grid.markingsColor = options.grid.coloredAreasColor;
  723. if (options.lines)
  724. $.extend(true, options.series.lines, options.lines);
  725. if (options.points)
  726. $.extend(true, options.series.points, options.points);
  727. if (options.bars)
  728. $.extend(true, options.series.bars, options.bars);
  729. if (options.shadowSize != null)
  730. options.series.shadowSize = options.shadowSize;
  731. if (options.highlightColor != null)
  732. options.series.highlightColor = options.highlightColor;
  733. // save options on axes for future reference
  734. for (i = 0; i < options.xaxes.length; ++i)
  735. getOrCreateAxis(xaxes, i + 1).options = options.xaxes[i];
  736. for (i = 0; i < options.yaxes.length; ++i)
  737. getOrCreateAxis(yaxes, i + 1).options = options.yaxes[i];
  738. // add hooks from options
  739. for (var n in hooks)
  740. if (options.hooks[n] && options.hooks[n].length)
  741. hooks[n] = hooks[n].concat(options.hooks[n]);
  742. executeHooks(hooks.processOptions, [options]);
  743. }
  744. function setData(d) {
  745. series = parseData(d);
  746. fillInSeriesOptions();
  747. processData();
  748. }
  749. function parseData(d) {
  750. var res = [];
  751. for (var i = 0; i < d.length; ++i) {
  752. var s = $.extend(true, {}, options.series);
  753. if (d[i].data != null) {
  754. s.data = d[i].data; // move the data instead of deep-copy
  755. delete d[i].data;
  756. $.extend(true, s, d[i]);
  757. d[i].data = s.data;
  758. }
  759. else
  760. s.data = d[i];
  761. res.push(s);
  762. }
  763. return res;
  764. }
  765. function axisNumber(obj, coord) {
  766. var a = obj[coord + "axis"];
  767. if (typeof a == "object") // if we got a real axis, extract number
  768. a = a.n;
  769. if (typeof a != "number")
  770. a = 1; // default to first axis
  771. return a;
  772. }
  773. function allAxes() {
  774. // return flat array without annoying null entries
  775. return $.grep(xaxes.concat(yaxes), function (a) { return a; });
  776. }
  777. function canvasToAxisCoords(pos) {
  778. // return an object with x/y corresponding to all used axes
  779. var res = {}, i, axis;
  780. for (i = 0; i < xaxes.length; ++i) {
  781. axis = xaxes[i];
  782. if (axis && axis.used)
  783. res["x" + axis.n] = axis.c2p(pos.left);
  784. }
  785. for (i = 0; i < yaxes.length; ++i) {
  786. axis = yaxes[i];
  787. if (axis && axis.used)
  788. res["y" + axis.n] = axis.c2p(pos.top);
  789. }
  790. if (res.x1 !== undefined)
  791. res.x = res.x1;
  792. if (res.y1 !== undefined)
  793. res.y = res.y1;
  794. return res;
  795. }
  796. function axisToCanvasCoords(pos) {
  797. // get canvas coords from the first pair of x/y found in pos
  798. var res = {}, i, axis, key;
  799. for (i = 0; i < xaxes.length; ++i) {
  800. axis = xaxes[i];
  801. if (axis && axis.used) {
  802. key = "x" + axis.n;
  803. if (pos[key] == null && axis.n == 1)
  804. key = "x";
  805. if (pos[key] != null) {
  806. res.left = axis.p2c(pos[key]);
  807. break;
  808. }
  809. }
  810. }
  811. for (i = 0; i < yaxes.length; ++i) {
  812. axis = yaxes[i];
  813. if (axis && axis.used) {
  814. key = "y" + axis.n;
  815. if (pos[key] == null && axis.n == 1)
  816. key = "y";
  817. if (pos[key] != null) {
  818. res.top = axis.p2c(pos[key]);
  819. break;
  820. }
  821. }
  822. }
  823. return res;
  824. }
  825. function getOrCreateAxis(axes, number) {
  826. if (!axes[number - 1])
  827. axes[number - 1] = {
  828. n: number, // save the number for future reference
  829. direction: axes == xaxes ? "x" : "y",
  830. options: $.extend(true, {}, axes == xaxes ? options.xaxis : options.yaxis)
  831. };
  832. return axes[number - 1];
  833. }
  834. function fillInSeriesOptions() {
  835. var neededColors = series.length, maxIndex = -1, i;
  836. // Subtract the number of series that already have fixed colors or
  837. // color indexes from the number that we still need to generate.
  838. for (i = 0; i < series.length; ++i) {
  839. var sc = series[i].color;
  840. if (sc != null) {
  841. neededColors--;
  842. if (typeof sc == "number" && sc > maxIndex) {
  843. maxIndex = sc;
  844. }
  845. }
  846. }
  847. // If any of the series have fixed color indexes, then we need to
  848. // generate at least as many colors as the highest index.
  849. if (neededColors <= maxIndex) {
  850. neededColors = maxIndex + 1;
  851. }
  852. // Generate all the colors, using first the option colors and then
  853. // variations on those colors once they're exhausted.
  854. var c, colors = [], colorPool = options.colors,
  855. colorPoolSize = colorPool.length, variation = 0;
  856. for (i = 0; i < neededColors; i++) {
  857. c = $.color.parse(colorPool[i % colorPoolSize] || "#666");
  858. // Each time we exhaust the colors in the pool we adjust
  859. // a scaling factor used to produce more variations on
  860. // those colors. The factor alternates negative/positive
  861. // to produce lighter/darker colors.
  862. // Reset the variation after every few cycles, or else
  863. // it will end up producing only white or black colors.
  864. if (i % colorPoolSize == 0 && i) {
  865. if (variation >= 0) {
  866. if (variation < 0.5) {
  867. variation = -variation - 0.2;
  868. } else variation = 0;
  869. } else variation = -variation;
  870. }
  871. colors[i] = c.scale('rgb', 1 + variation);
  872. }
  873. // Finalize the series options, filling in their colors
  874. var colori = 0, s;
  875. for (i = 0; i < series.length; ++i) {
  876. s = series[i];
  877. // assign colors
  878. if (s.color == null) {
  879. s.color = colors[colori].toString();
  880. ++colori;
  881. }
  882. else if (typeof s.color == "number")
  883. s.color = colors[s.color].toString();
  884. // turn on lines automatically in case nothing is set
  885. if (s.lines.show == null) {
  886. var v, show = true;
  887. for (v in s)
  888. if (s[v] && s[v].show) {
  889. show = false;
  890. break;
  891. }
  892. if (show)
  893. s.lines.show = true;
  894. }
  895. // If nothing was provided for lines.zero, default it to match
  896. // lines.fill, since areas by default should extend to zero.
  897. if (s.lines.zero == null) {
  898. s.lines.zero = !!s.lines.fill;
  899. }
  900. // setup axes
  901. s.xaxis = getOrCreateAxis(xaxes, axisNumber(s, "x"));
  902. s.yaxis = getOrCreateAxis(yaxes, axisNumber(s, "y"));
  903. }
  904. }
  905. function processData() {
  906. var topSentry = Number.POSITIVE_INFINITY,
  907. bottomSentry = Number.NEGATIVE_INFINITY,
  908. fakeInfinity = Number.MAX_VALUE,
  909. i, j, k, m, length,
  910. s, points, ps, x, y, axis, val, f, p,
  911. data, format;
  912. function updateAxis(axis, min, max) {
  913. if (min < axis.datamin && min != -fakeInfinity)
  914. axis.datamin = min;
  915. if (max > axis.datamax && max != fakeInfinity)
  916. axis.datamax = max;
  917. }
  918. $.each(allAxes(), function (_, axis) {
  919. // init axis
  920. axis.datamin = topSentry;
  921. axis.datamax = bottomSentry;
  922. axis.used = false;
  923. });
  924. for (i = 0; i < series.length; ++i) {
  925. s = series[i];
  926. s.datapoints = { points: [] };
  927. executeHooks(hooks.processRawData, [ s, s.data, s.datapoints ]);
  928. }
  929. // first pass: clean and copy data
  930. for (i = 0; i < series.length; ++i) {
  931. s = series[i];
  932. data = s.data;
  933. format = s.datapoints.format;
  934. if (!format) {
  935. format = [];
  936. // find out how to copy
  937. format.push({ x: true, number: true, required: true });
  938. format.push({ y: true, number: true, required: true });
  939. if (s.bars.show || (s.lines.show && s.lines.fill)) {
  940. var autoscale = !!((s.bars.show && s.bars.zero) || (s.lines.show && s.lines.zero));
  941. format.push({ y: true, number: true, required: false, defaultValue: 0, autoscale: autoscale });
  942. if (s.bars.horizontal) {
  943. delete format[format.length - 1].y;
  944. format[format.length - 1].x = true;
  945. }
  946. }
  947. s.datapoints.format = format;
  948. }
  949. if (s.datapoints.pointsize != null)
  950. continue; // already filled in
  951. s.datapoints.pointsize = format.length;
  952. ps = s.datapoints.pointsize;
  953. points = s.datapoints.points;
  954. var insertSteps = s.lines.show && s.lines.steps;
  955. s.xaxis.used = s.yaxis.used = true;
  956. for (j = k = 0; j < data.length; ++j, k += ps) {
  957. p = data[j];
  958. var nullify = p == null;
  959. if (!nullify) {
  960. for (m = 0; m < ps; ++m) {
  961. val = p[m];
  962. f = format[m];
  963. if (f) {
  964. if (f.number && val != null) {
  965. val = +val; // convert to number
  966. if (isNaN(val))
  967. val = null;
  968. else if (val == Infinity)
  969. val = fakeInfinity;
  970. else if (val == -Infinity)
  971. val = -fakeInfinity;
  972. }
  973. if (val == null) {
  974. if (f.required)
  975. nullify = true;
  976. if (f.defaultValue != null)
  977. val = f.defaultValue;
  978. }
  979. }
  980. points[k + m] = val;
  981. }
  982. }
  983. if (nullify) {
  984. for (m = 0; m < ps; ++m) {
  985. val = points[k + m];
  986. if (val != null) {
  987. f = format[m];
  988. // extract min/max info
  989. if (f.autoscale !== false) {
  990. if (f.x) {
  991. updateAxis(s.xaxis, val, val);
  992. }
  993. if (f.y) {
  994. updateAxis(s.yaxis, val, val);
  995. }
  996. }
  997. }
  998. points[k + m] = null;
  999. }
  1000. }
  1001. else {
  1002. // a little bit of line specific stuff that
  1003. // perhaps shouldn't be here, but lacking
  1004. // better means...
  1005. if (insertSteps && k > 0
  1006. && points[k - ps] != null
  1007. && points[k - ps] != points[k]
  1008. && points[k - ps + 1] != points[k + 1]) {
  1009. // copy the point to make room for a middle point
  1010. for (m = 0; m < ps; ++m)
  1011. points[k + ps + m] = points[k + m];
  1012. // middle point has same y
  1013. points[k + 1] = points[k - ps + 1];
  1014. // we've added a point, better reflect that
  1015. k += ps;
  1016. }
  1017. }
  1018. }
  1019. }
  1020. // give the hooks a chance to run
  1021. for (i = 0; i < series.length; ++i) {
  1022. s = series[i];
  1023. executeHooks(hooks.processDatapoints, [ s, s.datapoints]);
  1024. }
  1025. // second pass: find datamax/datamin for auto-scaling
  1026. for (i = 0; i < series.length; ++i) {
  1027. s = series[i];
  1028. points = s.datapoints.points;
  1029. ps = s.datapoints.pointsize;
  1030. format = s.datapoints.format;
  1031. var xmin = topSentry, ymin = topSentry,
  1032. xmax = bottomSentry, ymax = bottomSentry;
  1033. for (j = 0; j < points.length; j += ps) {
  1034. if (points[j] == null)
  1035. continue;
  1036. for (m = 0; m < ps; ++m) {
  1037. val = points[j + m];
  1038. f = format[m];
  1039. if (!f || f.autoscale === false || val == fakeInfinity || val == -fakeInfinity)
  1040. continue;
  1041. if (f.x) {
  1042. if (val < xmin)
  1043. xmin = val;
  1044. if (val > xmax)
  1045. xmax = val;
  1046. }
  1047. if (f.y) {
  1048. if (val < ymin)
  1049. ymin = val;
  1050. if (val > ymax)
  1051. ymax = val;
  1052. }
  1053. }
  1054. }
  1055. if (s.bars.show) {
  1056. // make sure we got room for the bar on the dancing floor
  1057. var delta;
  1058. switch (s.bars.align) {
  1059. case "left":
  1060. delta = 0;
  1061. break;
  1062. case "right":
  1063. delta = -s.bars.barWidth;
  1064. break;
  1065. default:
  1066. delta = -s.bars.barWidth / 2;
  1067. }
  1068. if (s.bars.horizontal) {
  1069. ymin += delta;
  1070. ymax += delta + s.bars.barWidth;
  1071. }
  1072. else {
  1073. xmin += delta;
  1074. xmax += delta + s.bars.barWidth;
  1075. }
  1076. }
  1077. updateAxis(s.xaxis, xmin, xmax);
  1078. updateAxis(s.yaxis, ymin, ymax);
  1079. }
  1080. $.each(allAxes(), function (_, axis) {
  1081. if (axis.datamin == topSentry)
  1082. axis.datamin = null;
  1083. if (axis.datamax == bottomSentry)
  1084. axis.datamax = null;
  1085. });
  1086. }
  1087. function setupCanvases() {
  1088. // Make sure the placeholder is clear of everything except canvases
  1089. // from a previous plot in this container that we'll try to re-use.
  1090. placeholder.css("padding", 0) // padding messes up the positioning
  1091. .children().filter(function(){
  1092. return !$(this).hasClass("flot-overlay") && !$(this).hasClass('flot-base');
  1093. }).remove();
  1094. if (placeholder.css("position") == 'static')
  1095. placeholder.css("position", "relative"); // for positioning labels and overlay
  1096. surface = new Canvas("flot-base", placeholder);
  1097. overlay = new Canvas("flot-overlay", placeholder); // overlay canvas for interactive features
  1098. ctx = surface.context;
  1099. octx = overlay.context;
  1100. // define which element we're listening for events on
  1101. eventHolder = $(overlay.element).unbind();
  1102. // If we're re-using a plot object, shut down the old one
  1103. var existing = placeholder.data("plot");
  1104. if (existing) {
  1105. existing.shutdown();
  1106. overlay.clear();
  1107. }
  1108. // save in case we get replotted
  1109. placeholder.data("plot", plot);
  1110. }
  1111. function bindEvents() {
  1112. // bind events
  1113. if (options.grid.hoverable) {
  1114. eventHolder.mousemove(onMouseMove);
  1115. // Use bind, rather than .mouseleave, because we officially
  1116. // still support jQuery 1.2.6, which doesn't define a shortcut
  1117. // for mouseenter or mouseleave. This was a bug/oversight that
  1118. // was fixed somewhere around 1.3.x. We can return to using
  1119. // .mouseleave when we drop support for 1.2.6.
  1120. eventHolder.bind("mouseleave", onMouseLeave);
  1121. }
  1122. if (options.grid.clickable)
  1123. eventHolder.click(onClick);
  1124. executeHooks(hooks.bindEvents, [eventHolder]);
  1125. }
  1126. function shutdown() {
  1127. if (redrawTimeout)
  1128. clearTimeout(redrawTimeout);
  1129. eventHolder.unbind("mousemove", onMouseMove);
  1130. eventHolder.unbind("mouseleave", onMouseLeave);
  1131. eventHolder.unbind("click", onClick);
  1132. executeHooks(hooks.shutdown, [eventHolder]);
  1133. }
  1134. function setTransformationHelpers(axis) {
  1135. // set helper functions on the axis, assumes plot area
  1136. // has been computed already
  1137. function identity(x) { return x; }
  1138. var s, m, t = axis.options.transform || identity,
  1139. it = axis.options.inverseTransform;
  1140. // precompute how much the axis is scaling a point
  1141. // in canvas space
  1142. if (axis.direction == "x") {
  1143. s = axis.scale = plotWidth / Math.abs(t(axis.max) - t(axis.min));
  1144. m = Math.min(t(axis.max), t(axis.min));
  1145. }
  1146. else {
  1147. s = axis.scale = plotHeight / Math.abs(t(axis.max) - t(axis.min));
  1148. s = -s;
  1149. m = Math.max(t(axis.max), t(axis.min));
  1150. }
  1151. // data point to canvas coordinate
  1152. if (t == identity) // slight optimization
  1153. axis.p2c = function (p) { return (p - m) * s; };
  1154. else
  1155. axis.p2c = function (p) { return (t(p) - m) * s; };
  1156. // canvas coordinate to data point
  1157. if (!it)
  1158. axis.c2p = function (c) { return m + c / s; };
  1159. else
  1160. axis.c2p = function (c) { return it(m + c / s); };
  1161. }
  1162. function measureTickLabels(axis) {
  1163. var opts = axis.options,
  1164. ticks = axis.ticks || [],
  1165. labelWidth = opts.labelWidth || 0,
  1166. labelHeight = opts.labelHeight || 0,
  1167. maxWidth = labelWidth || (axis.direction == "x" ? Math.floor(surface.width / (ticks.length || 1)) : null),
  1168. legacyStyles = axis.direction + "Axis " + axis.direction + axis.n + "Axis",
  1169. layer = "flot-" + axis.direction + "-axis flot-" + axis.direction + axis.n + "-axis " + legacyStyles,
  1170. font = opts.font || "flot-tick-label tickLabel";
  1171. for (var i = 0; i < ticks.length; ++i) {
  1172. var t = ticks[i];
  1173. if (!t.label)
  1174. continue;
  1175. var info = surface.getTextInfo(layer, t.label, font, null, maxWidth);
  1176. labelWidth = Math.max(labelWidth, info.width);
  1177. labelHeight = Math.max(labelHeight, info.height);
  1178. }
  1179. axis.labelWidth = opts.labelWidth || labelWidth;
  1180. axis.labelHeight = opts.labelHeight || labelHeight;
  1181. }
  1182. function allocateAxisBoxFirstPhase(axis) {
  1183. // find the bounding box of the axis by looking at label
  1184. // widths/heights and ticks, make room by diminishing the
  1185. // plotOffset; this first phase only looks at one
  1186. // dimension per axis, the other dimension depends on the
  1187. // other axes so will have to wait
  1188. var lw = axis.labelWidth,
  1189. lh = axis.labelHeight,
  1190. pos = axis.options.position,
  1191. isXAxis = axis.direction === "x",
  1192. tickLength = axis.options.tickLength,
  1193. axisMargin = options.grid.axisMargin,
  1194. padding = options.grid.labelMargin,
  1195. innermost = true,
  1196. outermost = true,
  1197. first = true,
  1198. found = false;
  1199. // Determine the axis's position in its direction and on its side
  1200. $.each(isXAxis ? xaxes : yaxes, function(i, a) {
  1201. if (a && (a.show || a.reserveSpace)) {
  1202. if (a === axis) {
  1203. found = true;
  1204. } else if (a.options.position === pos) {
  1205. if (found) {
  1206. outermost = false;
  1207. } else {
  1208. innermost = false;
  1209. }
  1210. }
  1211. if (!found) {
  1212. first = false;
  1213. }
  1214. }
  1215. });
  1216. // The outermost axis on each side has no margin
  1217. if (outermost) {
  1218. axisMargin = 0;
  1219. }
  1220. // The ticks for the first axis in each direction stretch across
  1221. if (tickLength == null) {
  1222. tickLength = first ? "full" : 5;
  1223. }
  1224. if (!isNaN(+tickLength))
  1225. padding += +tickLength;
  1226. if (isXAxis) {
  1227. lh += padding;
  1228. if (pos == "bottom") {
  1229. plotOffset.bottom += lh + axisMargin;
  1230. axis.box = { top: surface.height - plotOffset.bottom, height: lh };
  1231. }
  1232. else {
  1233. axis.box = { top: plotOffset.top + axisMargin, height: lh };
  1234. plotOffset.top += lh + axisMargin;
  1235. }
  1236. }
  1237. else {
  1238. lw += padding;
  1239. if (pos == "left") {
  1240. axis.box = { left: plotOffset.left + axisMargin, width: lw };
  1241. plotOffset.left += lw + axisMargin;
  1242. }
  1243. else {
  1244. plotOffset.right += lw + axisMargin;
  1245. axis.box = { left: surface.width - plotOffset.right, width: lw };
  1246. }
  1247. }
  1248. // save for future reference
  1249. axis.position = pos;
  1250. axis.tickLength = tickLength;
  1251. axis.box.padding = padding;
  1252. axis.innermost = innermost;
  1253. }
  1254. function allocateAxisBoxSecondPhase(axis) {
  1255. // now that all axis boxes have been placed in one
  1256. // dimension, we can set the remaining dimension coordinates
  1257. if (axis.direction == "x") {
  1258. axis.box.left = plotOffset.left - axis.labelWidth / 2;
  1259. axis.box.width = surface.width - plotOffset.left - plotOffset.right + axis.labelWidth;
  1260. }
  1261. else {
  1262. axis.box.top = plotOffset.top - axis.labelHeight / 2;
  1263. axis.box.height = surface.height - plotOffset.bottom - plotOffset.top + axis.labelHeight;
  1264. }
  1265. }
  1266. function adjustLayoutForThingsStickingOut() {
  1267. // possibly adjust plot offset to ensure everything stays
  1268. // inside the canvas and isn't clipped off
  1269. var minMargin = options.grid.minBorderMargin,
  1270. axis, i;
  1271. // check stuff from the plot (FIXME: this should just read
  1272. // a value from the series, otherwise it's impossible to
  1273. // customize)
  1274. if (minMargin == null) {
  1275. minMargin = 0;
  1276. for (i = 0; i < series.length; ++i)
  1277. minMargin = Math.max(minMargin, 2 * (series[i].points.radius + series[i].points.lineWidth/2));
  1278. }
  1279. var margins = {
  1280. left: minMargin,
  1281. right: minMargin,
  1282. top: minMargin,
  1283. bottom: minMargin
  1284. };
  1285. // check axis labels, note we don't check the actual
  1286. // labels but instead use the overall width/height to not
  1287. // jump as much around with replots
  1288. $.each(allAxes(), function (_, axis) {
  1289. if (axis.reserveSpace && axis.ticks && axis.ticks.length) {
  1290. if (axis.direction === "x") {
  1291. margins.left = Math.max(margins.left, axis.labelWidth / 2);
  1292. margins.right = Math.max(margins.right, axis.labelWidth / 2);
  1293. } else {
  1294. margins.bottom = Math.max(margins.bottom, axis.labelHeight / 2);
  1295. margins.top = Math.max(margins.top, axis.labelHeight / 2);
  1296. }
  1297. }
  1298. });
  1299. plotOffset.left = Math.ceil(Math.max(margins.left, plotOffset.left));
  1300. plotOffset.right = Math.ceil(Math.max(margins.right, plotOffset.right));
  1301. plotOffset.top = Math.ceil(Math.max(margins.top, plotOffset.top));
  1302. plotOffset.bottom = Math.ceil(Math.max(margins.bottom, plotOffset.bottom));
  1303. }
  1304. function setupGrid() {
  1305. var i, axes = allAxes(), showGrid = options.grid.show;
  1306. // Initialize the plot's offset from the edge of the canvas
  1307. for (var a in plotOffset) {
  1308. var margin = options.grid.margin || 0;
  1309. plotOffset[a] = typeof margin == "number" ? margin : margin[a] || 0;
  1310. }
  1311. executeHooks(hooks.processOffset, [plotOffset]);
  1312. // If the grid is visible, add its border width to the offset
  1313. for (var a in plotOffset) {
  1314. if(typeof(options.grid.borderWidth) == "object") {
  1315. plotOffset[a] += showGrid ? options.grid.borderWidth[a] : 0;
  1316. }
  1317. else {
  1318. plotOffset[a] += showGrid ? options.grid.borderWidth : 0;
  1319. }
  1320. }
  1321. $.each(axes, function (_, axis) {
  1322. var axisOpts = axis.options;
  1323. axis.show = axisOpts.show == null ? axis.used : axisOpts.show;
  1324. axis.reserveSpace = axisOpts.reserveSpace == null ? axis.show : axisOpts.reserveSpace;
  1325. setRange(axis);
  1326. });
  1327. if (showGrid) {
  1328. var allocatedAxes = $.grep(axes, function (axis) {
  1329. return axis.show || axis.reserveSpace;
  1330. });
  1331. $.each(allocatedAxes, function (_, axis) {
  1332. // make the ticks
  1333. setupTickGeneration(axis);
  1334. setTicks(axis);
  1335. snapRangeToTicks(axis, axis.ticks);
  1336. // find labelWidth/Height for axis
  1337. measureTickLabels(axis);
  1338. });
  1339. // with all dimensions calculated, we can compute the
  1340. // axis bounding boxes, start from the outside
  1341. // (reverse order)
  1342. for (i = allocatedAxes.length - 1; i >= 0; --i)
  1343. allocateAxisBoxFirstPhase(allocatedAxes[i]);
  1344. // make sure we've got enough space for things that
  1345. // might stick out
  1346. adjustLayoutForThingsStickingOut();
  1347. $.each(allocatedAxes, function (_, axis) {
  1348. allocateAxisBoxSecondPhase(axis);
  1349. });
  1350. }
  1351. plotWidth = surface.width - plotOffset.left - plotOffset.right;
  1352. plotHeight = surface.height - plotOffset.bottom - plotOffset.top;
  1353. // now we got the proper plot dimensions, we can compute the scaling
  1354. $.each(axes, function (_, axis) {
  1355. setTransformationHelpers(axis);
  1356. });
  1357. if (showGrid) {
  1358. drawAxisLabels();
  1359. }
  1360. insertLegend();
  1361. }
  1362. function setRange(axis) {
  1363. var opts = axis.options,
  1364. min = +(opts.min != null ? opts.min : axis.datamin),
  1365. max = +(opts.max != null ? opts.max : axis.datamax),
  1366. delta = max - min;
  1367. if (delta == 0.0) {
  1368. // degenerate case
  1369. var widen = max == 0 ? 1 : 0.01;
  1370. if (opts.min == null)
  1371. min -= widen;
  1372. // always widen max if we couldn't widen min to ensure we
  1373. // don't fall into min == max which doesn't work
  1374. if (opts.max == null || opts.min != null)
  1375. max += widen;
  1376. }
  1377. else {
  1378. // consider autoscaling
  1379. var margin = opts.autoscaleMargin;
  1380. if (margin != null) {
  1381. if (opts.min == null) {
  1382. min -= delta * margin;
  1383. // make sure we don't go below zero if all values
  1384. // are positive
  1385. if (min < 0 && axis.datamin != null && axis.datamin >= 0)
  1386. min = 0;
  1387. }
  1388. if (opts.max == null) {
  1389. max += delta * margin;
  1390. if (max > 0 && axis.datamax != null && axis.datamax <= 0)
  1391. max = 0;
  1392. }
  1393. }
  1394. }
  1395. axis.min = min;
  1396. axis.max = max;
  1397. }
  1398. function setupTickGeneration(axis) {
  1399. var opts = axis.options;
  1400. // estimate number of ticks
  1401. var noTicks;
  1402. if (typeof opts.ticks == "number" && opts.ticks > 0)
  1403. noTicks = opts.ticks;
  1404. else
  1405. // heuristic based on the model a*sqrt(x) fitted to
  1406. // some data points that seemed reasonable
  1407. noTicks = 0.3 * Math.sqrt(axis.direction == "x" ? surface.width : surface.height);
  1408. var delta = (axis.max - axis.min) / noTicks,
  1409. dec = -Math.floor(Math.log(delta) / Math.LN10),
  1410. maxDec = opts.tickDecimals;
  1411. if (maxDec != null && dec > maxDec) {
  1412. dec = maxDec;
  1413. }
  1414. var magn = Math.pow(10, -dec),
  1415. norm = delta / magn, // norm is between 1.0 and 10.0
  1416. size;
  1417. if (norm < 1.5) {
  1418. size = 1;
  1419. } else if (norm < 3) {
  1420. size = 2;
  1421. // special case for 2.5, requires an extra decimal
  1422. if (norm > 2.25 && (maxDec == null || dec + 1 <= maxDec)) {
  1423. size = 2.5;
  1424. ++dec;
  1425. }
  1426. } else if (norm < 7.5) {
  1427. size = 5;
  1428. } else {
  1429. size = 10;
  1430. }
  1431. size *= magn;
  1432. if (opts.minTickSize != null && size < opts.minTickSize) {
  1433. size = opts.minTickSize;
  1434. }
  1435. axis.delta = delta;
  1436. axis.tickDecimals = Math.max(0, maxDec != null ? maxDec : dec);
  1437. axis.tickSize = opts.tickSize || size;
  1438. // Time mode was moved to a plug-in in 0.8, and since so many people use it
  1439. // we'll add an especially friendly reminder to make sure they included it.
  1440. if (opts.mode == "time" && !axis.tickGenerator) {
  1441. throw new Error("Time mode requires the flot.time plugin.");
  1442. }
  1443. // Flot supports base-10 axes; any other mode else is handled by a plug-in,
  1444. // like flot.time.js.
  1445. if (!axis.tickGenerator) {
  1446. axis.tickGenerator = function (axis) {
  1447. var ticks = [],
  1448. start = floorInBase(axis.min, axis.tickSize),
  1449. i = 0,
  1450. v = Number.NaN,
  1451. prev;
  1452. do {
  1453. prev = v;
  1454. v = start + i * axis.tickSize;
  1455. ticks.push(v);
  1456. ++i;
  1457. } while (v < axis.max && v != prev);
  1458. return ticks;
  1459. };
  1460. axis.tickFormatter = function (value, axis) {
  1461. var factor = axis.tickDecimals ? Math.pow(10, axis.tickDecimals) : 1;
  1462. var formatted = "" + Math.round(value * factor) / factor;
  1463. // If tickDecimals was specified, ensure that we have exactly that
  1464. // much precision; otherwise default to the value's own precision.
  1465. if (axis.tickDecimals != null) {
  1466. var decimal = formatted.indexOf(".");
  1467. var precision = decimal == -1 ? 0 : formatted.length - decimal - 1;
  1468. if (precision < axis.tickDecimals) {
  1469. return (precision ? formatted : formatted + ".") + ("" + factor).substr(1, axis.tickDecimals - precision);
  1470. }
  1471. }
  1472. return formatted;
  1473. };
  1474. }
  1475. if ($.isFunction(opts.tickFormatter))
  1476. axis.tickFormatter = function (v, axis) { return "" + opts.tickFormatter(v, axis); };
  1477. if (opts.alignTicksWithAxis != null) {
  1478. var otherAxis = (axis.direction == "x" ? xaxes : yaxes)[opts.alignTicksWithAxis - 1];
  1479. if (otherAxis && otherAxis.used && otherAxis != axis) {
  1480. // consider snapping min/max to outermost nice ticks
  1481. var niceTicks = axis.tickGenerator(axis);
  1482. if (niceTicks.length > 0) {
  1483. if (opts.min == null)
  1484. axis.min = Math.min(axis.min, niceTicks[0]);
  1485. if (opts.max == null && niceTicks.length > 1)
  1486. axis.max = Math.max(axis.max, niceTicks[niceTicks.length - 1]);
  1487. }
  1488. axis.tickGenerator = function (axis) {
  1489. // copy ticks, scaled to this axis
  1490. var ticks = [], v, i;
  1491. for (i = 0; i < otherAxis.ticks.length; ++i) {
  1492. v = (otherAxis.ticks[i].v - otherAxis.min) / (otherAxis.max - otherAxis.min);
  1493. v = axis.min + v * (axis.max - axis.min);
  1494. ticks.push(v);
  1495. }
  1496. return ticks;
  1497. };
  1498. // we might need an extra decimal since forced
  1499. // ticks don't necessarily fit naturally
  1500. if (!axis.mode && opts.tickDecimals == null) {
  1501. var extraDec = Math.max(0, -Math.floor(Math.log(axis.delta) / Math.LN10) + 1),
  1502. ts = axis.tickGenerator(axis);
  1503. // only proceed if the tick interval rounded
  1504. // with an extra decimal doesn't give us a
  1505. // zero at end
  1506. if (!(ts.length > 1 && /\..*0$/.test((ts[1] - ts[0]).toFixed(extraDec))))
  1507. axis.tickDecimals = extraDec;
  1508. }
  1509. }
  1510. }
  1511. }
  1512. function setTicks(axis) {
  1513. var oticks = axis.options.ticks, ticks = [];
  1514. if (oticks == null || (typeof oticks == "number" && oticks > 0))
  1515. ticks = axis.tickGenerator(axis);
  1516. else if (oticks) {
  1517. if ($.isFunction(oticks))
  1518. // generate the ticks
  1519. ticks = oticks(axis);
  1520. else
  1521. ticks = oticks;
  1522. }
  1523. // clean up/labelify the supplied ticks, copy them over
  1524. var i, v;
  1525. axis.ticks = [];
  1526. for (i = 0; i < ticks.length; ++i) {
  1527. var label = null;
  1528. var t = ticks[i];
  1529. if (typeof t == "object") {
  1530. v = +t[0];
  1531. if (t.length > 1)
  1532. label = t[1];
  1533. }
  1534. else
  1535. v = +t;
  1536. if (label == null)
  1537. label = axis.tickFormatter(v, axis);
  1538. if (!isNaN(v))
  1539. axis.ticks.push({ v: v, label: label });
  1540. }
  1541. }
  1542. function snapRangeToTicks(axis, ticks) {
  1543. if (axis.options.autoscaleMargin && ticks.length > 0) {
  1544. // snap to ticks
  1545. if (axis.options.min == null)
  1546. axis.min = Math.min(axis.min, ticks[0].v);
  1547. if (axis.options.max == null && ticks.length > 1)
  1548. axis.max = Math.max(axis.max, ticks[ticks.length - 1].v);
  1549. }
  1550. }
  1551. function draw() {
  1552. surface.clear();
  1553. executeHooks(hooks.drawBackground, [ctx]);
  1554. var grid = options.grid;
  1555. // draw background, if any
  1556. if (grid.show && grid.backgroundColor)
  1557. drawBackground();
  1558. if (grid.show && !grid.aboveData) {
  1559. drawGrid();
  1560. }
  1561. for (var i = 0; i < series.length; ++i) {
  1562. executeHooks(hooks.drawSeries, [ctx, series[i]]);
  1563. drawSeries(series[i]);
  1564. }
  1565. executeHooks(hooks.draw, [ctx]);
  1566. if (grid.show && grid.aboveData) {
  1567. drawGrid();
  1568. }
  1569. surface.render();
  1570. // A draw implies that either the axes or data have changed, so we
  1571. // should probably update the overlay highlights as well.
  1572. triggerRedrawOverlay();
  1573. }
  1574. function extractRange(ranges, coord) {
  1575. var axis, from, to, key, axes = allAxes();
  1576. for (var i = 0; i < axes.length; ++i) {
  1577. axis = axes[i];
  1578. if (axis.direction == coord) {
  1579. key = coord + axis.n + "axis";
  1580. if (!ranges[key] && axis.n == 1)
  1581. key = coord + "axis"; // support x1axis as xaxis
  1582. if (ranges[key]) {
  1583. from = ranges[key].from;
  1584. to = ranges[key].to;
  1585. break;
  1586. }
  1587. }
  1588. }
  1589. // backwards-compat stuff - to be removed in future
  1590. if (!ranges[key]) {
  1591. axis = coord == "x" ? xaxes[0] : yaxes[0];
  1592. from = ranges[coord + "1"];
  1593. to = ranges[coord + "2"];
  1594. }
  1595. // auto-reverse as an added bonus
  1596. if (from != null && to != null && from > to) {
  1597. var tmp = from;
  1598. from = to;
  1599. to = tmp;
  1600. }
  1601. return { from: from, to: to, axis: axis };
  1602. }
  1603. function drawBackground() {
  1604. ctx.save();
  1605. ctx.translate(plotOffset.left, plotOffset.top);
  1606. ctx.fillStyle = getColorOrGradient(options.grid.backgroundColor, plotHeight, 0, "rgba(255, 255, 255, 0)");
  1607. ctx.fillRect(0, 0, plotWidth, plotHeight);
  1608. ctx.restore();
  1609. }
  1610. function drawGrid() {
  1611. var i, axes, bw, bc;
  1612. ctx.save();
  1613. ctx.translate(plotOffset.left, plotOffset.top);
  1614. // draw markings
  1615. var markings = options.grid.markings;
  1616. if (markings) {
  1617. if ($.isFunction(markings)) {
  1618. axes = plot.getAxes();
  1619. // xmin etc. is backwards compatibility, to be
  1620. // removed in the future
  1621. axes.xmin = axes.xaxis.min;
  1622. axes.xmax = axes.xaxis.max;
  1623. axes.ymin = axes.yaxis.min;
  1624. axes.ymax = axes.yaxis.max;
  1625. markings = markings(axes);
  1626. }
  1627. for (i = 0; i < markings.length; ++i) {
  1628. var m = markings[i],
  1629. xrange = extractRange(m, "x"),
  1630. yrange = extractRange(m, "y");
  1631. // fill in missing
  1632. if (xrange.from == null)
  1633. xrange.from = xrange.axis.min;
  1634. if (xrange.to == null)
  1635. xrange.to = xrange.axis.max;
  1636. if (yrange.from == null)
  1637. yrange.from = yrange.axis.min;
  1638. if (yrange.to == null)
  1639. yrange.to = yrange.axis.max;
  1640. // clip
  1641. if (xrange.to < xrange.axis.min || xrange.from > xrange.axis.max ||
  1642. yrange.to < yrange.axis.min || yrange.from > yrange.axis.max)
  1643. continue;
  1644. xrange.from = Math.max(xrange.from, xrange.axis.min);
  1645. xrange.to = Math.min(xrange.to, xrange.axis.max);
  1646. yrange.from = Math.max(yrange.from, yrange.axis.min);
  1647. yrange.to = Math.min(yrange.to, yrange.axis.max);
  1648. var xequal = xrange.from === xrange.to,
  1649. yequal = yrange.from === yrange.to;
  1650. if (xequal && yequal) {
  1651. continue;
  1652. }
  1653. // then draw
  1654. xrange.from = Math.floor(xrange.axis.p2c(xrange.from));
  1655. xrange.to = Math.floor(xrange.axis.p2c(xrange.to));
  1656. yrange.from = Math.floor(yrange.axis.p2c(yrange.from));
  1657. yrange.to = Math.floor(yrange.axis.p2c(yrange.to));
  1658. if (xequal || yequal) {
  1659. var lineWidth = m.lineWidth || options.grid.markingsLineWidth,
  1660. subPixel = lineWidth % 2 ? 0.5 : 0;
  1661. ctx.beginPath();
  1662. ctx.strokeStyle = m.color || options.grid.markingsColor;
  1663. ctx.lineWidth = lineWidth;
  1664. if (xequal) {
  1665. ctx.moveTo(xrange.to + subPixel, yrange.from);
  1666. ctx.lineTo(xrange.to + subPixel, yrange.to);
  1667. } else {
  1668. ctx.moveTo(xrange.from, yrange.to + subPixel);
  1669. ctx.lineTo(xrange.to, yrange.to + subPixel);
  1670. }
  1671. ctx.stroke();
  1672. } else {
  1673. ctx.fillStyle = m.color || options.grid.markingsColor;
  1674. ctx.fillRect(xrange.from, yrange.to,
  1675. xrange.to - xrange.from,
  1676. yrange.from - yrange.to);
  1677. }
  1678. }
  1679. }
  1680. // draw the ticks
  1681. axes = allAxes();
  1682. bw = options.grid.borderWidth;
  1683. for (var j = 0; j < axes.length; ++j) {
  1684. var axis = axes[j], box = axis.box,
  1685. t = axis.tickLength, x, y, xoff, yoff;
  1686. if (!axis.show || axis.ticks.length == 0)
  1687. continue;
  1688. ctx.lineWidth = 1;
  1689. // find the edges
  1690. if (axis.direction == "x") {
  1691. x = 0;
  1692. if (t == "full")
  1693. y = (axis.position == "top" ? 0 : plotHeight);
  1694. else
  1695. y = box.top - plotOffset.top + (axis.position == "top" ? box.height : 0);
  1696. }
  1697. else {
  1698. y = 0;
  1699. if (t == "full")
  1700. x = (axis.position == "left" ? 0 : plotWidth);
  1701. else
  1702. x = box.left - plotOffset.left + (axis.position == "left" ? box.width : 0);
  1703. }
  1704. // draw tick bar
  1705. if (!axis.innermost) {
  1706. ctx.strokeStyle = axis.options.color;
  1707. ctx.beginPath();
  1708. xoff = yoff = 0;
  1709. if (axis.direction == "x")
  1710. xoff = plotWidth + 1;
  1711. else
  1712. yoff = plotHeight + 1;
  1713. if (ctx.lineWidth == 1) {
  1714. if (axis.direction == "x") {
  1715. y = Math.floor(y) + 0.5;
  1716. } else {
  1717. x = Math.floor(x) + 0.5;
  1718. }
  1719. }
  1720. ctx.moveTo(x, y);
  1721. ctx.lineTo(x + xoff, y + yoff);
  1722. ctx.stroke();
  1723. }
  1724. // draw ticks
  1725. ctx.strokeStyle = axis.options.tickColor;
  1726. ctx.beginPath();
  1727. for (i = 0; i < axis.ticks.length; ++i) {
  1728. var v = axis.ticks[i].v;
  1729. xoff = yoff = 0;
  1730. if (isNaN(v) || v < axis.min || v > axis.max
  1731. // skip those lying on the axes if we got a border
  1732. || (t == "full"
  1733. && ((typeof bw == "object" && bw[axis.position] > 0) || bw > 0)
  1734. && (v == axis.min || v == axis.max)))
  1735. continue;
  1736. if (axis.direction == "x") {
  1737. x = axis.p2c(v);
  1738. yoff = t == "full" ? -plotHeight : t;
  1739. if (axis.position == "top")
  1740. yoff = -yoff;
  1741. }
  1742. else {
  1743. y = axis.p2c(v);
  1744. xoff = t == "full" ? -plotWidth : t;
  1745. if (axis.position == "left")
  1746. xoff = -xoff;
  1747. }
  1748. if (ctx.lineWidth == 1) {
  1749. if (axis.direction == "x")
  1750. x = Math.floor(x) + 0.5;
  1751. else
  1752. y = Math.floor(y) + 0.5;
  1753. }
  1754. ctx.moveTo(x, y);
  1755. ctx.lineTo(x + xoff, y + yoff);
  1756. }
  1757. ctx.stroke();
  1758. }
  1759. // draw border
  1760. if (bw) {
  1761. // If either borderWidth or borderColor is an object, then draw the border
  1762. // line by line instead of as one rectangle
  1763. bc = options.grid.borderColor;
  1764. if(typeof bw == "object" || typeof bc == "object") {
  1765. if (typeof bw !== "object") {
  1766. bw = {top: bw, right: bw, bottom: bw, left: bw};
  1767. }
  1768. if (typeof bc !== "object") {
  1769. bc = {top: bc, right: bc, bottom: bc, left: bc};
  1770. }
  1771. if (bw.top > 0) {
  1772. ctx.strokeStyle = bc.top;
  1773. ctx.lineWidth = bw.top;
  1774. ctx.beginPath();
  1775. ctx.moveTo(0 - bw.left, 0 - bw.top/2);
  1776. ctx.lineTo(plotWidth, 0 - bw.top/2);
  1777. ctx.stroke();
  1778. }
  1779. if (bw.right > 0) {
  1780. ctx.strokeStyle = bc.right;
  1781. ctx.lineWidth = bw.right;
  1782. ctx.beginPath();
  1783. ctx.moveTo(plotWidth + bw.right / 2, 0 - bw.top);
  1784. ctx.lineTo(plotWidth + bw.right / 2, plotHeight);
  1785. ctx.stroke();
  1786. }
  1787. if (bw.bottom > 0) {
  1788. ctx.strokeStyle = bc.bottom;
  1789. ctx.lineWidth = bw.bottom;
  1790. ctx.beginPath();
  1791. ctx.moveTo(plotWidth + bw.right, plotHeight + bw.bottom / 2);
  1792. ctx.lineTo(0, plotHeight + bw.bottom / 2);
  1793. ctx.stroke();
  1794. }
  1795. if (bw.left > 0) {
  1796. ctx.strokeStyle = bc.left;
  1797. ctx.lineWidth = bw.left;
  1798. ctx.beginPath();
  1799. ctx.moveTo(0 - bw.left/2, plotHeight + bw.bottom);
  1800. ctx.lineTo(0- bw.left/2, 0);
  1801. ctx.stroke();
  1802. }
  1803. }
  1804. else {
  1805. ctx.lineWidth = bw;
  1806. ctx.strokeStyle = options.grid.borderColor;
  1807. ctx.strokeRect(-bw/2, -bw/2, plotWidth + bw, plotHeight + bw);
  1808. }
  1809. }
  1810. ctx.restore();
  1811. }
  1812. function drawAxisLabels() {
  1813. $.each(allAxes(), function (_, axis) {
  1814. var box = axis.box,
  1815. legacyStyles = axis.direction + "Axis " + axis.direction + axis.n + "Axis",
  1816. layer = "flot-" + axis.direction + "-axis flot-" + axis.direction + axis.n + "-axis " + legacyStyles,
  1817. font = axis.options.font || "flot-tick-label tickLabel",
  1818. tick, x, y, halign, valign;
  1819. // Remove text before checking for axis.show and ticks.length;
  1820. // otherwise plugins, like flot-tickrotor, that draw their own
  1821. // tick labels will end up with both theirs and the defaults.
  1822. surface.removeText(layer);
  1823. if (!axis.show || axis.ticks.length == 0)
  1824. return;
  1825. for (var i = 0; i < axis.ticks.length; ++i) {
  1826. tick = axis.ticks[i];
  1827. if (!tick.label || tick.v < axis.min || tick.v > axis.max)
  1828. continue;
  1829. if (axis.direction == "x") {
  1830. halign = "center";
  1831. x = plotOffset.left + axis.p2c(tick.v);
  1832. if (axis.position == "bottom") {
  1833. y = box.top + box.padding;
  1834. } else {
  1835. y = box.top + box.height - box.padding;
  1836. valign = "bottom";
  1837. }
  1838. } else {
  1839. valign = "middle";
  1840. y = plotOffset.top + axis.p2c(tick.v);
  1841. if (axis.position == "left") {
  1842. x = box.left + box.width - box.padding;
  1843. halign = "right";
  1844. } else {
  1845. x = box.left + box.padding;
  1846. }
  1847. }
  1848. surface.addText(layer, x, y, tick.label, font, null, null, halign, valign);
  1849. }
  1850. });
  1851. }
  1852. function drawSeries(series) {
  1853. if (series.lines.show)
  1854. drawSeriesLines(series);
  1855. if (series.bars.show)
  1856. drawSeriesBars(series);
  1857. if (series.points.show)
  1858. drawSeriesPoints(series);
  1859. }
  1860. function drawSeriesLines(series) {
  1861. function plotLine(datapoints, xoffset, yoffset, axisx, axisy) {
  1862. var points = datapoints.points,
  1863. ps = datapoints.pointsize,
  1864. prevx = null, prevy = null;
  1865. ctx.beginPath();
  1866. for (var i = ps; i < points.length; i += ps) {
  1867. var x1 = points[i - ps], y1 = points[i - ps + 1],
  1868. x2 = points[i], y2 = points[i + 1];
  1869. if (x1 == null || x2 == null)
  1870. continue;
  1871. // clip with ymin
  1872. if (y1 <= y2 && y1 < axisy.min) {
  1873. if (y2 < axisy.min)
  1874. continue; // line segment is outside
  1875. // compute new intersection point
  1876. x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
  1877. y1 = axisy.min;
  1878. }
  1879. else if (y2 <= y1 && y2 < axisy.min) {
  1880. if (y1 < axisy.min)
  1881. continue;
  1882. x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
  1883. y2 = axisy.min;
  1884. }
  1885. // clip with ymax
  1886. if (y1 >= y2 && y1 > axisy.max) {
  1887. if (y2 > axisy.max)
  1888. continue;
  1889. x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
  1890. y1 = axisy.max;
  1891. }
  1892. else if (y2 >= y1 && y2 > axisy.max) {
  1893. if (y1 > axisy.max)
  1894. continue;
  1895. x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
  1896. y2 = axisy.max;
  1897. }
  1898. // clip with xmin
  1899. if (x1 <= x2 && x1 < axisx.min) {
  1900. if (x2 < axisx.min)
  1901. continue;
  1902. y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
  1903. x1 = axisx.min;
  1904. }
  1905. else if (x2 <= x1 && x2 < axisx.min) {
  1906. if (x1 < axisx.min)
  1907. continue;
  1908. y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
  1909. x2 = axisx.min;
  1910. }
  1911. // clip with xmax
  1912. if (x1 >= x2 && x1 > axisx.max) {
  1913. if (x2 > axisx.max)
  1914. continue;
  1915. y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
  1916. x1 = axisx.max;
  1917. }
  1918. else if (x2 >= x1 && x2 > axisx.max) {
  1919. if (x1 > axisx.max)
  1920. continue;
  1921. y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
  1922. x2 = axisx.max;
  1923. }
  1924. if (x1 != prevx || y1 != prevy)
  1925. ctx.moveTo(axisx.p2c(x1) + xoffset, axisy.p2c(y1) + yoffset);
  1926. prevx = x2;
  1927. prevy = y2;
  1928. ctx.lineTo(axisx.p2c(x2) + xoffset, axisy.p2c(y2) + yoffset);
  1929. }
  1930. ctx.stroke();
  1931. }
  1932. function plotLineArea(datapoints, axisx, axisy) {
  1933. var points = datapoints.points,
  1934. ps = datapoints.pointsize,
  1935. bottom = Math.min(Math.max(0, axisy.min), axisy.max),
  1936. i = 0, top, areaOpen = false,
  1937. ypos = 1, segmentStart = 0, segmentEnd = 0;
  1938. // we process each segment in two turns, first forward
  1939. // direction to sketch out top, then once we hit the
  1940. // end we go backwards to sketch the bottom
  1941. while (true) {
  1942. if (ps > 0 && i > points.length + ps)
  1943. break;
  1944. i += ps; // ps is negative if going backwards
  1945. var x1 = points[i - ps],
  1946. y1 = points[i - ps + ypos],
  1947. x2 = points[i], y2 = points[i + ypos];
  1948. if (areaOpen) {
  1949. if (ps > 0 && x1 != null && x2 == null) {
  1950. // at turning point
  1951. segmentEnd = i;
  1952. ps = -ps;
  1953. ypos = 2;
  1954. continue;
  1955. }
  1956. if (ps < 0 && i == segmentStart + ps) {
  1957. // done with the reverse sweep
  1958. ctx.fill();
  1959. areaOpen = false;
  1960. ps = -ps;
  1961. ypos = 1;
  1962. i = segmentStart = segmentEnd + ps;
  1963. continue;
  1964. }
  1965. }
  1966. if (x1 == null || x2 == null)
  1967. continue;
  1968. // clip x values
  1969. // clip with xmin
  1970. if (x1 <= x2 && x1 < axisx.min) {
  1971. if (x2 < axisx.min)
  1972. continue;
  1973. y1 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
  1974. x1 = axisx.min;
  1975. }
  1976. else if (x2 <= x1 && x2 < axisx.min) {
  1977. if (x1 < axisx.min)
  1978. continue;
  1979. y2 = (axisx.min - x1) / (x2 - x1) * (y2 - y1) + y1;
  1980. x2 = axisx.min;
  1981. }
  1982. // clip with xmax
  1983. if (x1 >= x2 && x1 > axisx.max) {
  1984. if (x2 > axisx.max)
  1985. continue;
  1986. y1 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
  1987. x1 = axisx.max;
  1988. }
  1989. else if (x2 >= x1 && x2 > axisx.max) {
  1990. if (x1 > axisx.max)
  1991. continue;
  1992. y2 = (axisx.max - x1) / (x2 - x1) * (y2 - y1) + y1;
  1993. x2 = axisx.max;
  1994. }
  1995. if (!areaOpen) {
  1996. // open area
  1997. ctx.beginPath();
  1998. ctx.moveTo(axisx.p2c(x1), axisy.p2c(bottom));
  1999. areaOpen = true;
  2000. }
  2001. // now first check the case where both is outside
  2002. if (y1 >= axisy.max && y2 >= axisy.max) {
  2003. ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.max));
  2004. ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.max));
  2005. continue;
  2006. }
  2007. else if (y1 <= axisy.min && y2 <= axisy.min) {
  2008. ctx.lineTo(axisx.p2c(x1), axisy.p2c(axisy.min));
  2009. ctx.lineTo(axisx.p2c(x2), axisy.p2c(axisy.min));
  2010. continue;
  2011. }
  2012. // else it's a bit more complicated, there might
  2013. // be a flat maxed out rectangle first, then a
  2014. // triangular cutout or reverse; to find these
  2015. // keep track of the current x values
  2016. var x1old = x1, x2old = x2;
  2017. // clip the y values, without shortcutting, we
  2018. // go through all cases in turn
  2019. // clip with ymin
  2020. if (y1 <= y2 && y1 < axisy.min && y2 >= axisy.min) {
  2021. x1 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
  2022. y1 = axisy.min;
  2023. }
  2024. else if (y2 <= y1 && y2 < axisy.min && y1 >= axisy.min) {
  2025. x2 = (axisy.min - y1) / (y2 - y1) * (x2 - x1) + x1;
  2026. y2 = axisy.min;
  2027. }
  2028. // clip with ymax
  2029. if (y1 >= y2 && y1 > axisy.max && y2 <= axisy.max) {
  2030. x1 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
  2031. y1 = axisy.max;
  2032. }
  2033. else if (y2 >= y1 && y2 > axisy.max && y1 <= axisy.max) {
  2034. x2 = (axisy.max - y1) / (y2 - y1) * (x2 - x1) + x1;
  2035. y2 = axisy.max;
  2036. }
  2037. // if the x value was changed we got a rectangle
  2038. // to fill
  2039. if (x1 != x1old) {
  2040. ctx.lineTo(axisx.p2c(x1old), axisy.p2c(y1));
  2041. // it goes to (x1, y1), but we fill that below
  2042. }
  2043. // fill triangular section, this sometimes result
  2044. // in redundant points if (x1, y1) hasn't changed
  2045. // from previous line to, but we just ignore that
  2046. ctx.lineTo(axisx.p2c(x1), axisy.p2c(y1));
  2047. ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2));
  2048. // fill the other rectangle if it's there
  2049. if (x2 != x2old) {
  2050. ctx.lineTo(axisx.p2c(x2), axisy.p2c(y2));
  2051. ctx.lineTo(axisx.p2c(x2old), axisy.p2c(y2));
  2052. }
  2053. }
  2054. }
  2055. ctx.save();
  2056. ctx.translate(plotOffset.left, plotOffset.top);
  2057. ctx.lineJoin = "round";
  2058. var lw = series.lines.lineWidth,
  2059. sw = series.shadowSize;
  2060. // FIXME: consider another form of shadow when filling is turned on
  2061. if (lw > 0 && sw > 0) {
  2062. // draw shadow as a thick and thin line with transparency
  2063. ctx.lineWidth = sw;
  2064. ctx.strokeStyle = "rgba(0,0,0,0.1)";
  2065. // position shadow at angle from the mid of line
  2066. var angle = Math.PI/18;
  2067. plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/2), Math.cos(angle) * (lw/2 + sw/2), series.xaxis, series.yaxis);
  2068. ctx.lineWidth = sw/2;
  2069. plotLine(series.datapoints, Math.sin(angle) * (lw/2 + sw/4), Math.cos(angle) * (lw/2 + sw/4), series.xaxis, series.yaxis);
  2070. }
  2071. ctx.lineWidth = lw;
  2072. ctx.strokeStyle = series.color;
  2073. var fillStyle = getFillStyle(series.lines, series.color, 0, plotHeight);
  2074. if (fillStyle) {
  2075. ctx.fillStyle = fillStyle;
  2076. plotLineArea(series.datapoints, series.xaxis, series.yaxis);
  2077. }
  2078. if (lw > 0)
  2079. plotLine(series.datapoints, 0, 0, series.xaxis, series.yaxis);
  2080. ctx.restore();
  2081. }
  2082. function drawSeriesPoints(series) {
  2083. function plotPoints(datapoints, radius, fillStyle, offset, shadow, axisx, axisy, symbol) {
  2084. var points = datapoints.points, ps = datapoints.pointsize;
  2085. for (var i = 0; i < points.length; i += ps) {
  2086. var x = points[i], y = points[i + 1];
  2087. if (x == null || x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
  2088. continue;
  2089. ctx.beginPath();
  2090. x = axisx.p2c(x);
  2091. y = axisy.p2c(y) + offset;
  2092. if (symbol == "circle")
  2093. ctx.arc(x, y, radius, 0, shadow ? Math.PI : Math.PI * 2, false);
  2094. else
  2095. symbol(ctx, x, y, radius, shadow);
  2096. ctx.closePath();
  2097. if (fillStyle) {
  2098. ctx.fillStyle = fillStyle;
  2099. ctx.fill();
  2100. }
  2101. ctx.stroke();
  2102. }
  2103. }
  2104. ctx.save();
  2105. ctx.translate(plotOffset.left, plotOffset.top);
  2106. var lw = series.points.lineWidth,
  2107. sw = series.shadowSize,
  2108. radius = series.points.radius,
  2109. symbol = series.points.symbol;
  2110. // If the user sets the line width to 0, we change it to a very
  2111. // small value. A line width of 0 seems to force the default of 1.
  2112. // Doing the conditional here allows the shadow setting to still be
  2113. // optional even with a lineWidth of 0.
  2114. if( lw == 0 )
  2115. lw = 0.0001;
  2116. if (lw > 0 && sw > 0) {
  2117. // draw shadow in two steps
  2118. var w = sw / 2;
  2119. ctx.lineWidth = w;
  2120. ctx.strokeStyle = "rgba(0,0,0,0.1)";
  2121. plotPoints(series.datapoints, radius, null, w + w/2, true,
  2122. series.xaxis, series.yaxis, symbol);
  2123. ctx.strokeStyle = "rgba(0,0,0,0.2)";
  2124. plotPoints(series.datapoints, radius, null, w/2, true,
  2125. series.xaxis, series.yaxis, symbol);
  2126. }
  2127. ctx.lineWidth = lw;
  2128. ctx.strokeStyle = series.color;
  2129. plotPoints(series.datapoints, radius,
  2130. getFillStyle(series.points, series.color), 0, false,
  2131. series.xaxis, series.yaxis, symbol);
  2132. ctx.restore();
  2133. }
  2134. function drawBar(x, y, b, barLeft, barRight, fillStyleCallback, axisx, axisy, c, horizontal, lineWidth) {
  2135. var left, right, bottom, top,
  2136. drawLeft, drawRight, drawTop, drawBottom,
  2137. tmp;
  2138. // in horizontal mode, we start the bar from the left
  2139. // instead of from the bottom so it appears to be
  2140. // horizontal rather than vertical
  2141. if (horizontal) {
  2142. drawBottom = drawRight = drawTop = true;
  2143. drawLeft = false;
  2144. left = b;
  2145. right = x;
  2146. top = y + barLeft;
  2147. bottom = y + barRight;
  2148. // account for negative bars
  2149. if (right < left) {
  2150. tmp = right;
  2151. right = left;
  2152. left = tmp;
  2153. drawLeft = true;
  2154. drawRight = false;
  2155. }
  2156. }
  2157. else {
  2158. drawLeft = drawRight = drawTop = true;
  2159. drawBottom = false;
  2160. left = x + barLeft;
  2161. right = x + barRight;
  2162. bottom = b;
  2163. top = y;
  2164. // account for negative bars
  2165. if (top < bottom) {
  2166. tmp = top;
  2167. top = bottom;
  2168. bottom = tmp;
  2169. drawBottom = true;
  2170. drawTop = false;
  2171. }
  2172. }
  2173. // clip
  2174. if (right < axisx.min || left > axisx.max ||
  2175. top < axisy.min || bottom > axisy.max)
  2176. return;
  2177. if (left < axisx.min) {
  2178. left = axisx.min;
  2179. drawLeft = false;
  2180. }
  2181. if (right > axisx.max) {
  2182. right = axisx.max;
  2183. drawRight = false;
  2184. }
  2185. if (bottom < axisy.min) {
  2186. bottom = axisy.min;
  2187. drawBottom = false;
  2188. }
  2189. if (top > axisy.max) {
  2190. top = axisy.max;
  2191. drawTop = false;
  2192. }
  2193. left = axisx.p2c(left);
  2194. bottom = axisy.p2c(bottom);
  2195. right = axisx.p2c(right);
  2196. top = axisy.p2c(top);
  2197. // fill the bar
  2198. if (fillStyleCallback) {
  2199. c.fillStyle = fillStyleCallback(bottom, top);
  2200. c.fillRect(left, top, right - left, bottom - top)
  2201. }
  2202. // draw outline
  2203. if (lineWidth > 0 && (drawLeft || drawRight || drawTop || drawBottom)) {
  2204. c.beginPath();
  2205. // FIXME: inline moveTo is buggy with excanvas
  2206. c.moveTo(left, bottom);
  2207. if (drawLeft)
  2208. c.lineTo(left, top);
  2209. else
  2210. c.moveTo(left, top);
  2211. if (drawTop)
  2212. c.lineTo(right, top);
  2213. else
  2214. c.moveTo(right, top);
  2215. if (drawRight)
  2216. c.lineTo(right, bottom);
  2217. else
  2218. c.moveTo(right, bottom);
  2219. if (drawBottom)
  2220. c.lineTo(left, bottom);
  2221. else
  2222. c.moveTo(left, bottom);
  2223. c.stroke();
  2224. }
  2225. }
  2226. function drawSeriesBars(series) {
  2227. function plotBars(datapoints, barLeft, barRight, fillStyleCallback, axisx, axisy) {
  2228. var points = datapoints.points, ps = datapoints.pointsize;
  2229. for (var i = 0; i < points.length; i += ps) {
  2230. if (points[i] == null)
  2231. continue;
  2232. drawBar(points[i], points[i + 1], points[i + 2], barLeft, barRight, fillStyleCallback, axisx, axisy, ctx, series.bars.horizontal, series.bars.lineWidth);
  2233. }
  2234. }
  2235. ctx.save();
  2236. ctx.translate(plotOffset.left, plotOffset.top);
  2237. // FIXME: figure out a way to add shadows (for instance along the right edge)
  2238. ctx.lineWidth = series.bars.lineWidth;
  2239. ctx.strokeStyle = series.color;
  2240. var barLeft;
  2241. switch (series.bars.align) {
  2242. case "left":
  2243. barLeft = 0;
  2244. break;
  2245. case "right":
  2246. barLeft = -series.bars.barWidth;
  2247. break;
  2248. default:
  2249. barLeft = -series.bars.barWidth / 2;
  2250. }
  2251. var fillStyleCallback = series.bars.fill ? function (bottom, top) { return getFillStyle(series.bars, series.color, bottom, top); } : null;
  2252. plotBars(series.datapoints, barLeft, barLeft + series.bars.barWidth, fillStyleCallback, series.xaxis, series.yaxis);
  2253. ctx.restore();
  2254. }
  2255. function getFillStyle(filloptions, seriesColor, bottom, top) {
  2256. var fill = filloptions.fill;
  2257. if (!fill)
  2258. return null;
  2259. if (filloptions.fillColor)
  2260. return getColorOrGradient(filloptions.fillColor, bottom, top, seriesColor);
  2261. var c = $.color.parse(seriesColor);
  2262. c.a = typeof fill == "number" ? fill : 0.4;
  2263. c.normalize();
  2264. return c.toString();
  2265. }
  2266. function insertLegend() {
  2267. if (options.legend.container != null) {
  2268. $(options.legend.container).html("");
  2269. } else {
  2270. placeholder.find(".legend").remove();
  2271. }
  2272. if (!options.legend.show) {
  2273. return;
  2274. }
  2275. var fragments = [], entries = [], rowStarted = false,
  2276. lf = options.legend.labelFormatter, s, label;
  2277. // Build a list of legend entries, with each having a label and a color
  2278. for (var i = 0; i < series.length; ++i) {
  2279. s = series[i];
  2280. if (s.label) {
  2281. label = lf ? lf(s.label, s) : s.label;
  2282. if (label) {
  2283. entries.push({
  2284. label: label,
  2285. color: s.color
  2286. });
  2287. }
  2288. }
  2289. }
  2290. // Sort the legend using either the default or a custom comparator
  2291. if (options.legend.sorted) {
  2292. if ($.isFunction(options.legend.sorted)) {
  2293. entries.sort(options.legend.sorted);
  2294. } else if (options.legend.sorted == "reverse") {
  2295. entries.reverse();
  2296. } else {
  2297. var ascending = options.legend.sorted != "descending";
  2298. entries.sort(function(a, b) {
  2299. return a.label == b.label ? 0 : (
  2300. (a.label < b.label) != ascending ? 1 : -1 // Logical XOR
  2301. );
  2302. });
  2303. }
  2304. }
  2305. // Generate markup for the list of entries, in their final order
  2306. for (var i = 0; i < entries.length; ++i) {
  2307. var entry = entries[i];
  2308. if (i % options.legend.noColumns == 0) {
  2309. if (rowStarted)
  2310. fragments.push('</tr>');
  2311. fragments.push('<tr>');
  2312. rowStarted = true;
  2313. }
  2314. fragments.push(
  2315. '<td class="legendColorBox"><div style="border:1px solid ' + options.legend.labelBoxBorderColor + ';padding:1px"><div style="width:4px;height:0;border:5px solid ' + entry.color + ';overflow:hidden"></div></div></td>' +
  2316. '<td class="legendLabel">' + entry.label + '</td>'
  2317. );
  2318. }
  2319. if (rowStarted)
  2320. fragments.push('</tr>');
  2321. if (fragments.length == 0)
  2322. return;
  2323. var table = '<table style="font-size:smaller;color:' + options.grid.color + '">' + fragments.join("") + '</table>';
  2324. if (options.legend.container != null)
  2325. $(options.legend.container).html(table);
  2326. else {
  2327. var pos = "",
  2328. p = options.legend.position,
  2329. m = options.legend.margin;
  2330. if (m[0] == null)
  2331. m = [m, m];
  2332. if (p.charAt(0) == "n")
  2333. pos += 'top:' + (m[1] + plotOffset.top) + 'px;';
  2334. else if (p.charAt(0) == "s")
  2335. pos += 'bottom:' + (m[1] + plotOffset.bottom) + 'px;';
  2336. if (p.charAt(1) == "e")
  2337. pos += 'right:' + (m[0] + plotOffset.right) + 'px;';
  2338. else if (p.charAt(1) == "w")
  2339. pos += 'left:' + (m[0] + plotOffset.left) + 'px;';
  2340. var legend = $('<div class="legend">' + table.replace('style="', 'style="position:absolute;' + pos +';') + '</div>').appendTo(placeholder);
  2341. if (options.legend.backgroundOpacity != 0.0) {
  2342. // put in the transparent background
  2343. // separately to avoid blended labels and
  2344. // label boxes
  2345. var c = options.legend.backgroundColor;
  2346. if (c == null) {
  2347. c = options.grid.backgroundColor;
  2348. if (c && typeof c == "string")
  2349. c = $.color.parse(c);
  2350. else
  2351. c = $.color.extract(legend, 'background-color');
  2352. c.a = 1;
  2353. c = c.toString();
  2354. }
  2355. var div = legend.children();
  2356. $('<div style="position:absolute;width:' + div.width() + 'px;height:' + div.height() + 'px;' + pos +'background-color:' + c + ';"> </div>').prependTo(legend).css('opacity', options.legend.backgroundOpacity);
  2357. }
  2358. }
  2359. }
  2360. // interactive features
  2361. var highlights = [],
  2362. redrawTimeout = null;
  2363. // returns the data item the mouse is over, or null if none is found
  2364. function findNearbyItem(mouseX, mouseY, seriesFilter) {
  2365. var maxDistance = options.grid.mouseActiveRadius,
  2366. smallestDistance = maxDistance * maxDistance + 1,
  2367. item = null, foundPoint = false, i, j, ps;
  2368. for (i = series.length - 1; i >= 0; --i) {
  2369. if (!seriesFilter(series[i]))
  2370. continue;
  2371. var s = series[i],
  2372. axisx = s.xaxis,
  2373. axisy = s.yaxis,
  2374. points = s.datapoints.points,
  2375. mx = axisx.c2p(mouseX), // precompute some stuff to make the loop faster
  2376. my = axisy.c2p(mouseY),
  2377. maxx = maxDistance / axisx.scale,
  2378. maxy = maxDistance / axisy.scale;
  2379. ps = s.datapoints.pointsize;
  2380. // with inverse transforms, we can't use the maxx/maxy
  2381. // optimization, sadly
  2382. if (axisx.options.inverseTransform)
  2383. maxx = Number.MAX_VALUE;
  2384. if (axisy.options.inverseTransform)
  2385. maxy = Number.MAX_VALUE;
  2386. if (s.lines.show || s.points.show) {
  2387. for (j = 0; j < points.length; j += ps) {
  2388. var x = points[j], y = points[j + 1];
  2389. if (x == null)
  2390. continue;
  2391. // For points and lines, the cursor must be within a
  2392. // certain distance to the data point
  2393. if (x - mx > maxx || x - mx < -maxx ||
  2394. y - my > maxy || y - my < -maxy)
  2395. continue;
  2396. // We have to calculate distances in pixels, not in
  2397. // data units, because the scales of the axes may be different
  2398. var dx = Math.abs(axisx.p2c(x) - mouseX),
  2399. dy = Math.abs(axisy.p2c(y) - mouseY),
  2400. dist = dx * dx + dy * dy; // we save the sqrt
  2401. // use <= to ensure last point takes precedence
  2402. // (last generally means on top of)
  2403. if (dist < smallestDistance) {
  2404. smallestDistance = dist;
  2405. item = [i, j / ps];
  2406. }
  2407. }
  2408. }
  2409. if (s.bars.show && !item) { // no other point can be nearby
  2410. var barLeft, barRight;
  2411. switch (s.bars.align) {
  2412. case "left":
  2413. barLeft = 0;
  2414. break;
  2415. case "right":
  2416. barLeft = -s.bars.barWidth;
  2417. break;
  2418. default:
  2419. barLeft = -s.bars.barWidth / 2;
  2420. }
  2421. barRight = barLeft + s.bars.barWidth;
  2422. for (j = 0; j < points.length; j += ps) {
  2423. var x = points[j], y = points[j + 1], b = points[j + 2];
  2424. if (x == null)
  2425. continue;
  2426. // for a bar graph, the cursor must be inside the bar
  2427. if (series[i].bars.horizontal ?
  2428. (mx <= Math.max(b, x) && mx >= Math.min(b, x) &&
  2429. my >= y + barLeft && my <= y + barRight) :
  2430. (mx >= x + barLeft && mx <= x + barRight &&
  2431. my >= Math.min(b, y) && my <= Math.max(b, y)))
  2432. item = [i, j / ps];
  2433. }
  2434. }
  2435. }
  2436. if (item) {
  2437. i = item[0];
  2438. j = item[1];
  2439. ps = series[i].datapoints.pointsize;
  2440. return { datapoint: series[i].datapoints.points.slice(j * ps, (j + 1) * ps),
  2441. dataIndex: j,
  2442. series: series[i],
  2443. seriesIndex: i };
  2444. }
  2445. return null;
  2446. }
  2447. function onMouseMove(e) {
  2448. if (options.grid.hoverable)
  2449. triggerClickHoverEvent("plothover", e,
  2450. function (s) { return s["hoverable"] != false; });
  2451. }
  2452. function onMouseLeave(e) {
  2453. if (options.grid.hoverable)
  2454. triggerClickHoverEvent("plothover", e,
  2455. function (s) { return false; });
  2456. }
  2457. function onClick(e) {
  2458. triggerClickHoverEvent("plotclick", e,
  2459. function (s) { return s["clickable"] != false; });
  2460. }
  2461. // trigger click or hover event (they send the same parameters
  2462. // so we share their code)
  2463. function triggerClickHoverEvent(eventname, event, seriesFilter) {
  2464. var offset = eventHolder.offset(),
  2465. canvasX = event.pageX - offset.left - plotOffset.left,
  2466. canvasY = event.pageY - offset.top - plotOffset.top,
  2467. pos = canvasToAxisCoords({ left: canvasX, top: canvasY });
  2468. pos.pageX = event.pageX;
  2469. pos.pageY = event.pageY;
  2470. var item = findNearbyItem(canvasX, canvasY, seriesFilter);
  2471. if (item) {
  2472. // fill in mouse pos for any listeners out there
  2473. item.pageX = parseInt(item.series.xaxis.p2c(item.datapoint[0]) + offset.left + plotOffset.left, 10);
  2474. item.pageY = parseInt(item.series.yaxis.p2c(item.datapoint[1]) + offset.top + plotOffset.top, 10);
  2475. }
  2476. if (options.grid.autoHighlight) {
  2477. // clear auto-highlights
  2478. for (var i = 0; i < highlights.length; ++i) {
  2479. var h = highlights[i];
  2480. if (h.auto == eventname &&
  2481. !(item && h.series == item.series &&
  2482. h.point[0] == item.datapoint[0] &&
  2483. h.point[1] == item.datapoint[1]))
  2484. unhighlight(h.series, h.point);
  2485. }
  2486. if (item)
  2487. highlight(item.series, item.datapoint, eventname);
  2488. }
  2489. placeholder.trigger(eventname, [ pos, item ]);
  2490. }
  2491. function triggerRedrawOverlay() {
  2492. var t = options.interaction.redrawOverlayInterval;
  2493. if (t == -1) { // skip event queue
  2494. drawOverlay();
  2495. return;
  2496. }
  2497. if (!redrawTimeout)
  2498. redrawTimeout = setTimeout(drawOverlay, t);
  2499. }
  2500. function drawOverlay() {
  2501. redrawTimeout = null;
  2502. // draw highlights
  2503. octx.save();
  2504. overlay.clear();
  2505. octx.translate(plotOffset.left, plotOffset.top);
  2506. var i, hi;
  2507. for (i = 0; i < highlights.length; ++i) {
  2508. hi = highlights[i];
  2509. if (hi.series.bars.show)
  2510. drawBarHighlight(hi.series, hi.point);
  2511. else
  2512. drawPointHighlight(hi.series, hi.point);
  2513. }
  2514. octx.restore();
  2515. executeHooks(hooks.drawOverlay, [octx]);
  2516. }
  2517. function highlight(s, point, auto) {
  2518. if (typeof s == "number")
  2519. s = series[s];
  2520. if (typeof point == "number") {
  2521. var ps = s.datapoints.pointsize;
  2522. point = s.datapoints.points.slice(ps * point, ps * (point + 1));
  2523. }
  2524. var i = indexOfHighlight(s, point);
  2525. if (i == -1) {
  2526. highlights.push({ series: s, point: point, auto: auto });
  2527. triggerRedrawOverlay();
  2528. }
  2529. else if (!auto)
  2530. highlights[i].auto = false;
  2531. }
  2532. function unhighlight(s, point) {
  2533. if (s == null && point == null) {
  2534. highlights = [];
  2535. triggerRedrawOverlay();
  2536. return;
  2537. }
  2538. if (typeof s == "number")
  2539. s = series[s];
  2540. if (typeof point == "number") {
  2541. var ps = s.datapoints.pointsize;
  2542. point = s.datapoints.points.slice(ps * point, ps * (point + 1));
  2543. }
  2544. var i = indexOfHighlight(s, point);
  2545. if (i != -1) {
  2546. highlights.splice(i, 1);
  2547. triggerRedrawOverlay();
  2548. }
  2549. }
  2550. function indexOfHighlight(s, p) {
  2551. for (var i = 0; i < highlights.length; ++i) {
  2552. var h = highlights[i];
  2553. if (h.series == s && h.point[0] == p[0]
  2554. && h.point[1] == p[1])
  2555. return i;
  2556. }
  2557. return -1;
  2558. }
  2559. function drawPointHighlight(series, point) {
  2560. var x = point[0], y = point[1],
  2561. axisx = series.xaxis, axisy = series.yaxis,
  2562. highlightColor = (typeof series.highlightColor === "string") ? series.highlightColor : $.color.parse(series.color).scale('a', 0.5).toString();
  2563. if (x < axisx.min || x > axisx.max || y < axisy.min || y > axisy.max)
  2564. return;
  2565. var pointRadius = series.points.radius + series.points.lineWidth / 2;
  2566. octx.lineWidth = pointRadius;
  2567. octx.strokeStyle = highlightColor;
  2568. var radius = 1.5 * pointRadius;
  2569. x = axisx.p2c(x);
  2570. y = axisy.p2c(y);
  2571. octx.beginPath();
  2572. if (series.points.symbol == "circle")
  2573. octx.arc(x, y, radius, 0, 2 * Math.PI, false);
  2574. else
  2575. series.points.symbol(octx, x, y, radius, false);
  2576. octx.closePath();
  2577. octx.stroke();
  2578. }
  2579. function drawBarHighlight(series, point) {
  2580. var highlightColor = (typeof series.highlightColor === "string") ? series.highlightColor : $.color.parse(series.color).scale('a', 0.5).toString(),
  2581. fillStyle = highlightColor,
  2582. barLeft;
  2583. switch (series.bars.align) {
  2584. case "left":
  2585. barLeft = 0;
  2586. break;
  2587. case "right":
  2588. barLeft = -series.bars.barWidth;
  2589. break;
  2590. default:
  2591. barLeft = -series.bars.barWidth / 2;
  2592. }
  2593. octx.lineWidth = series.bars.lineWidth;
  2594. octx.strokeStyle = highlightColor;
  2595. drawBar(point[0], point[1], point[2] || 0, barLeft, barLeft + series.bars.barWidth,
  2596. function () { return fillStyle; }, series.xaxis, series.yaxis, octx, series.bars.horizontal, series.bars.lineWidth);
  2597. }
  2598. function getColorOrGradient(spec, bottom, top, defaultColor) {
  2599. if (typeof spec == "string")
  2600. return spec;
  2601. else {
  2602. // assume this is a gradient spec; IE currently only
  2603. // supports a simple vertical gradient properly, so that's
  2604. // what we support too
  2605. var gradient = ctx.createLinearGradient(0, top, 0, bottom);
  2606. for (var i = 0, l = spec.colors.length; i < l; ++i) {
  2607. var c = spec.colors[i];
  2608. if (typeof c != "string") {
  2609. var co = $.color.parse(defaultColor);
  2610. if (c.brightness != null)
  2611. co = co.scale('rgb', c.brightness);
  2612. if (c.opacity != null)
  2613. co.a *= c.opacity;
  2614. c = co.toString();
  2615. }
  2616. gradient.addColorStop(i / (l - 1), c);
  2617. }
  2618. return gradient;
  2619. }
  2620. }
  2621. }
  2622. // Add the plot function to the top level of the jQuery object
  2623. $.plot = function(placeholder, data, options) {
  2624. //var t0 = new Date();
  2625. var plot = new Plot($(placeholder), data, options, $.plot.plugins);
  2626. //(window.console ? console.log : alert)("time used (msecs): " + ((new Date()).getTime() - t0.getTime()));
  2627. return plot;
  2628. };
  2629. $.plot.version = "0.8.3";
  2630. $.plot.plugins = [];
  2631. // Also add the plot function as a chainable property
  2632. $.fn.plot = function(data, options) {
  2633. return this.each(function() {
  2634. $.plot(this, data, options);
  2635. });
  2636. };
  2637. // round to nearby lower multiple of base
  2638. function floorInBase(n, base) {
  2639. return base * Math.floor(n / base);
  2640. }
  2641. })(jQuery);
  2642. /* Plugin for jQuery for working with colors.
  2643. *
  2644. * Version 1.1.
  2645. *
  2646. * Inspiration from jQuery color animation plugin by John Resig.
  2647. *
  2648. * Released under the MIT license by Ole Laursen, October 2009.
  2649. *
  2650. * Examples:
  2651. *
  2652. * $.color.parse("#fff").scale('rgb', 0.25).add('a', -0.5).toString()
  2653. * var c = $.color.extract($("#mydiv"), 'background-color');
  2654. * console.log(c.r, c.g, c.b, c.a);
  2655. * $.color.make(100, 50, 25, 0.4).toString() // returns "rgba(100,50,25,0.4)"
  2656. *
  2657. * Note that .scale() and .add() return the same modified object
  2658. * instead of making a new one.
  2659. *
  2660. * V. 1.1: Fix error handling so e.g. parsing an empty string does
  2661. * produce a color rather than just crashing.
  2662. */
  2663. (function($) {
  2664. $.color = {};
  2665. // construct color object with some convenient chainable helpers
  2666. $.color.make = function (r, g, b, a) {
  2667. var o = {};
  2668. o.r = r || 0;
  2669. o.g = g || 0;
  2670. o.b = b || 0;
  2671. o.a = a != null ? a : 1;
  2672. o.add = function (c, d) {
  2673. for (var i = 0; i < c.length; ++i)
  2674. o[c.charAt(i)] += d;
  2675. return o.normalize();
  2676. };
  2677. o.scale = function (c, f) {
  2678. for (var i = 0; i < c.length; ++i)
  2679. o[c.charAt(i)] *= f;
  2680. return o.normalize();
  2681. };
  2682. o.toString = function () {
  2683. if (o.a >= 1.0) {
  2684. return "rgb("+[o.r, o.g, o.b].join(",")+")";
  2685. } else {
  2686. return "rgba("+[o.r, o.g, o.b, o.a].join(",")+")";
  2687. }
  2688. };
  2689. o.normalize = function () {
  2690. function clamp(min, value, max) {
  2691. return value < min ? min: (value > max ? max: value);
  2692. }
  2693. o.r = clamp(0, parseInt(o.r), 255);
  2694. o.g = clamp(0, parseInt(o.g), 255);
  2695. o.b = clamp(0, parseInt(o.b), 255);
  2696. o.a = clamp(0, o.a, 1);
  2697. return o;
  2698. };
  2699. o.clone = function () {
  2700. return $.color.make(o.r, o.b, o.g, o.a);
  2701. };
  2702. return o.normalize();
  2703. }
  2704. // extract CSS color property from element, going up in the DOM
  2705. // if it's "transparent"
  2706. $.color.extract = function (elem, css) {
  2707. var c;
  2708. do {
  2709. c = elem.css(css).toLowerCase();
  2710. // keep going until we find an element that has color, or
  2711. // we hit the body or root (have no parent)
  2712. if (c != '' && c != 'transparent')
  2713. break;
  2714. elem = elem.parent();
  2715. } while (elem.length && !$.nodeName(elem.get(0), "body"));
  2716. // catch Safari's way of signalling transparent
  2717. if (c == "rgba(0, 0, 0, 0)")
  2718. c = "transparent";
  2719. return $.color.parse(c);
  2720. }
  2721. // parse CSS color string (like "rgb(10, 32, 43)" or "#fff"),
  2722. // returns color object, if parsing failed, you get black (0, 0,
  2723. // 0) out
  2724. $.color.parse = function (str) {
  2725. var res, m = $.color.make;
  2726. // Look for rgb(num,num,num)
  2727. if (res = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(str))
  2728. return m(parseInt(res[1], 10), parseInt(res[2], 10), parseInt(res[3], 10));
  2729. // Look for rgba(num,num,num,num)
  2730. if (res = /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str))
  2731. return m(parseInt(res[1], 10), parseInt(res[2], 10), parseInt(res[3], 10), parseFloat(res[4]));
  2732. // Look for rgb(num%,num%,num%)
  2733. if (res = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(str))
  2734. return m(parseFloat(res[1])*2.55, parseFloat(res[2])*2.55, parseFloat(res[3])*2.55);
  2735. // Look for rgba(num%,num%,num%,num)
  2736. if (res = /rgba\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str))
  2737. return m(parseFloat(res[1])*2.55, parseFloat(res[2])*2.55, parseFloat(res[3])*2.55, parseFloat(res[4]));
  2738. // Look for #a0b1c2
  2739. if (res = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(str))
  2740. return m(parseInt(res[1], 16), parseInt(res[2], 16), parseInt(res[3], 16));
  2741. // Look for #fff
  2742. if (res = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(str))
  2743. return m(parseInt(res[1]+res[1], 16), parseInt(res[2]+res[2], 16), parseInt(res[3]+res[3], 16));
  2744. // Otherwise, we're most likely dealing with a named color
  2745. var name = $.trim(str).toLowerCase();
  2746. if (name == "transparent")
  2747. return m(255, 255, 255, 0);
  2748. else {
  2749. // default to black
  2750. res = lookupColors[name] || [0, 0, 0];
  2751. return m(res[0], res[1], res[2]);
  2752. }
  2753. }
  2754. var lookupColors = {
  2755. aqua:[0,255,255],
  2756. azure:[240,255,255],
  2757. beige:[245,245,220],
  2758. black:[0,0,0],
  2759. blue:[0,0,255],
  2760. brown:[165,42,42],
  2761. cyan:[0,255,255],
  2762. darkblue:[0,0,139],
  2763. darkcyan:[0,139,139],
  2764. darkgrey:[169,169,169],
  2765. darkgreen:[0,100,0],
  2766. darkkhaki:[189,183,107],
  2767. darkmagenta:[139,0,139],
  2768. darkolivegreen:[85,107,47],
  2769. darkorange:[255,140,0],
  2770. darkorchid:[153,50,204],
  2771. darkred:[139,0,0],
  2772. darksalmon:[233,150,122],
  2773. darkviolet:[148,0,211],
  2774. fuchsia:[255,0,255],
  2775. gold:[255,215,0],
  2776. green:[0,128,0],
  2777. indigo:[75,0,130],
  2778. khaki:[240,230,140],
  2779. lightblue:[173,216,230],
  2780. lightcyan:[224,255,255],
  2781. lightgreen:[144,238,144],
  2782. lightgrey:[211,211,211],
  2783. lightpink:[255,182,193],
  2784. lightyellow:[255,255,224],
  2785. lime:[0,255,0],
  2786. magenta:[255,0,255],
  2787. maroon:[128,0,0],
  2788. navy:[0,0,128],
  2789. olive:[128,128,0],
  2790. orange:[255,165,0],
  2791. pink:[255,192,203],
  2792. purple:[128,0,128],
  2793. violet:[128,0,128],
  2794. red:[255,0,0],
  2795. silver:[192,192,192],
  2796. white:[255,255,255],
  2797. yellow:[255,255,0]
  2798. };
  2799. })(jQuery);
  2800. /* Flot plugin for drawing all elements of a plot on the canvas.
  2801. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  2802. Licensed under the MIT license.
  2803. Flot normally produces certain elements, like axis labels and the legend, using
  2804. HTML elements. This permits greater interactivity and customization, and often
  2805. looks better, due to cross-browser canvas text inconsistencies and limitations.
  2806. It can also be desirable to render the plot entirely in canvas, particularly
  2807. if the goal is to save it as an image, or if Flot is being used in a context
  2808. where the HTML DOM does not exist, as is the case within Node.js. This plugin
  2809. switches out Flot's standard drawing operations for canvas-only replacements.
  2810. Currently the plugin supports only axis labels, but it will eventually allow
  2811. every element of the plot to be rendered directly to canvas.
  2812. The plugin supports these options:
  2813. {
  2814. canvas: boolean
  2815. }
  2816. The "canvas" option controls whether full canvas drawing is enabled, making it
  2817. possible to toggle on and off. This is useful when a plot uses HTML text in the
  2818. browser, but needs to redraw with canvas text when exporting as an image.
  2819. */
  2820. (function($) {
  2821. var options = {
  2822. canvas: true
  2823. };
  2824. var render, getTextInfo, addText;
  2825. // Cache the prototype hasOwnProperty for faster access
  2826. var hasOwnProperty = Object.prototype.hasOwnProperty;
  2827. function init(plot, classes) {
  2828. var Canvas = classes.Canvas;
  2829. // We only want to replace the functions once; the second time around
  2830. // we would just get our new function back. This whole replacing of
  2831. // prototype functions is a disaster, and needs to be changed ASAP.
  2832. if (render == null) {
  2833. getTextInfo = Canvas.prototype.getTextInfo,
  2834. addText = Canvas.prototype.addText,
  2835. render = Canvas.prototype.render;
  2836. }
  2837. // Finishes rendering the canvas, including overlaid text
  2838. Canvas.prototype.render = function() {
  2839. if (!plot.getOptions().canvas) {
  2840. return render.call(this);
  2841. }
  2842. var context = this.context,
  2843. cache = this._textCache;
  2844. // For each text layer, render elements marked as active
  2845. context.save();
  2846. context.textBaseline = "middle";
  2847. for (var layerKey in cache) {
  2848. if (hasOwnProperty.call(cache, layerKey)) {
  2849. var layerCache = cache[layerKey];
  2850. for (var styleKey in layerCache) {
  2851. if (hasOwnProperty.call(layerCache, styleKey)) {
  2852. var styleCache = layerCache[styleKey],
  2853. updateStyles = true;
  2854. for (var key in styleCache) {
  2855. if (hasOwnProperty.call(styleCache, key)) {
  2856. var info = styleCache[key],
  2857. positions = info.positions,
  2858. lines = info.lines;
  2859. // Since every element at this level of the cache have the
  2860. // same font and fill styles, we can just change them once
  2861. // using the values from the first element.
  2862. if (updateStyles) {
  2863. context.fillStyle = info.font.color;
  2864. context.font = info.font.definition;
  2865. updateStyles = false;
  2866. }
  2867. for (var i = 0, position; position = positions[i]; i++) {
  2868. if (position.active) {
  2869. for (var j = 0, line; line = position.lines[j]; j++) {
  2870. context.fillText(lines[j].text, line[0], line[1]);
  2871. }
  2872. } else {
  2873. positions.splice(i--, 1);
  2874. }
  2875. }
  2876. if (positions.length == 0) {
  2877. delete styleCache[key];
  2878. }
  2879. }
  2880. }
  2881. }
  2882. }
  2883. }
  2884. }
  2885. context.restore();
  2886. };
  2887. // Creates (if necessary) and returns a text info object.
  2888. //
  2889. // When the canvas option is set, the object looks like this:
  2890. //
  2891. // {
  2892. // width: Width of the text's bounding box.
  2893. // height: Height of the text's bounding box.
  2894. // positions: Array of positions at which this text is drawn.
  2895. // lines: [{
  2896. // height: Height of this line.
  2897. // widths: Width of this line.
  2898. // text: Text on this line.
  2899. // }],
  2900. // font: {
  2901. // definition: Canvas font property string.
  2902. // color: Color of the text.
  2903. // },
  2904. // }
  2905. //
  2906. // The positions array contains objects that look like this:
  2907. //
  2908. // {
  2909. // active: Flag indicating whether the text should be visible.
  2910. // lines: Array of [x, y] coordinates at which to draw the line.
  2911. // x: X coordinate at which to draw the text.
  2912. // y: Y coordinate at which to draw the text.
  2913. // }
  2914. Canvas.prototype.getTextInfo = function(layer, text, font, angle, width) {
  2915. if (!plot.getOptions().canvas) {
  2916. return getTextInfo.call(this, layer, text, font, angle, width);
  2917. }
  2918. var textStyle, layerCache, styleCache, info;
  2919. // Cast the value to a string, in case we were given a number
  2920. text = "" + text;
  2921. // If the font is a font-spec object, generate a CSS definition
  2922. if (typeof font === "object") {
  2923. textStyle = font.style + " " + font.variant + " " + font.weight + " " + font.size + "px " + font.family;
  2924. } else {
  2925. textStyle = font;
  2926. }
  2927. // Retrieve (or create) the cache for the text's layer and styles
  2928. layerCache = this._textCache[layer];
  2929. if (layerCache == null) {
  2930. layerCache = this._textCache[layer] = {};
  2931. }
  2932. styleCache = layerCache[textStyle];
  2933. if (styleCache == null) {
  2934. styleCache = layerCache[textStyle] = {};
  2935. }
  2936. info = styleCache[text];
  2937. if (info == null) {
  2938. var context = this.context;
  2939. // If the font was provided as CSS, create a div with those
  2940. // classes and examine it to generate a canvas font spec.
  2941. if (typeof font !== "object") {
  2942. var element = $("<div>&nbsp;</div>")
  2943. .css("position", "absolute")
  2944. .addClass(typeof font === "string" ? font : null)
  2945. .appendTo(this.getTextLayer(layer));
  2946. font = {
  2947. lineHeight: element.height(),
  2948. style: element.css("font-style"),
  2949. variant: element.css("font-variant"),
  2950. weight: element.css("font-weight"),
  2951. family: element.css("font-family"),
  2952. color: element.css("color")
  2953. };
  2954. // Setting line-height to 1, without units, sets it equal
  2955. // to the font-size, even if the font-size is abstract,
  2956. // like 'smaller'. This enables us to read the real size
  2957. // via the element's height, working around browsers that
  2958. // return the literal 'smaller' value.
  2959. font.size = element.css("line-height", 1).height();
  2960. element.remove();
  2961. }
  2962. textStyle = font.style + " " + font.variant + " " + font.weight + " " + font.size + "px " + font.family;
  2963. // Create a new info object, initializing the dimensions to
  2964. // zero so we can count them up line-by-line.
  2965. info = styleCache[text] = {
  2966. width: 0,
  2967. height: 0,
  2968. positions: [],
  2969. lines: [],
  2970. font: {
  2971. definition: textStyle,
  2972. color: font.color
  2973. }
  2974. };
  2975. context.save();
  2976. context.font = textStyle;
  2977. // Canvas can't handle multi-line strings; break on various
  2978. // newlines, including HTML brs, to build a list of lines.
  2979. // Note that we could split directly on regexps, but IE < 9 is
  2980. // broken; revisit when we drop IE 7/8 support.
  2981. var lines = (text + "").replace(/<br ?\/?>|\r\n|\r/g, "\n").split("\n");
  2982. for (var i = 0; i < lines.length; ++i) {
  2983. var lineText = lines[i],
  2984. measured = context.measureText(lineText);
  2985. info.width = Math.max(measured.width, info.width);
  2986. info.height += font.lineHeight;
  2987. info.lines.push({
  2988. text: lineText,
  2989. width: measured.width,
  2990. height: font.lineHeight
  2991. });
  2992. }
  2993. context.restore();
  2994. }
  2995. return info;
  2996. };
  2997. // Adds a text string to the canvas text overlay.
  2998. Canvas.prototype.addText = function(layer, x, y, text, font, angle, width, halign, valign) {
  2999. if (!plot.getOptions().canvas) {
  3000. return addText.call(this, layer, x, y, text, font, angle, width, halign, valign);
  3001. }
  3002. var info = this.getTextInfo(layer, text, font, angle, width),
  3003. positions = info.positions,
  3004. lines = info.lines;
  3005. // Text is drawn with baseline 'middle', which we need to account
  3006. // for by adding half a line's height to the y position.
  3007. y += info.height / lines.length / 2;
  3008. // Tweak the initial y-position to match vertical alignment
  3009. if (valign == "middle") {
  3010. y = Math.round(y - info.height / 2);
  3011. } else if (valign == "bottom") {
  3012. y = Math.round(y - info.height);
  3013. } else {
  3014. y = Math.round(y);
  3015. }
  3016. // FIXME: LEGACY BROWSER FIX
  3017. // AFFECTS: Opera < 12.00
  3018. // Offset the y coordinate, since Opera is off pretty
  3019. // consistently compared to the other browsers.
  3020. if (!!(window.opera && window.opera.version().split(".")[0] < 12)) {
  3021. y -= 2;
  3022. }
  3023. // Determine whether this text already exists at this position.
  3024. // If so, mark it for inclusion in the next render pass.
  3025. for (var i = 0, position; position = positions[i]; i++) {
  3026. if (position.x == x && position.y == y) {
  3027. position.active = true;
  3028. return;
  3029. }
  3030. }
  3031. // If the text doesn't exist at this position, create a new entry
  3032. position = {
  3033. active: true,
  3034. lines: [],
  3035. x: x,
  3036. y: y
  3037. };
  3038. positions.push(position);
  3039. // Fill in the x & y positions of each line, adjusting them
  3040. // individually for horizontal alignment.
  3041. for (var i = 0, line; line = lines[i]; i++) {
  3042. if (halign == "center") {
  3043. position.lines.push([Math.round(x - line.width / 2), y]);
  3044. } else if (halign == "right") {
  3045. position.lines.push([Math.round(x - line.width), y]);
  3046. } else {
  3047. position.lines.push([Math.round(x), y]);
  3048. }
  3049. y += line.height;
  3050. }
  3051. };
  3052. }
  3053. $.plot.plugins.push({
  3054. init: init,
  3055. options: options,
  3056. name: "canvas",
  3057. version: "1.0"
  3058. });
  3059. })(jQuery);
  3060. /* Flot plugin for plotting textual data or categories.
  3061. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  3062. Licensed under the MIT license.
  3063. Consider a dataset like [["February", 34], ["March", 20], ...]. This plugin
  3064. allows you to plot such a dataset directly.
  3065. To enable it, you must specify mode: "categories" on the axis with the textual
  3066. labels, e.g.
  3067. $.plot("#placeholder", data, { xaxis: { mode: "categories" } });
  3068. By default, the labels are ordered as they are met in the data series. If you
  3069. need a different ordering, you can specify "categories" on the axis options
  3070. and list the categories there:
  3071. xaxis: {
  3072. mode: "categories",
  3073. categories: ["February", "March", "April"]
  3074. }
  3075. If you need to customize the distances between the categories, you can specify
  3076. "categories" as an object mapping labels to values
  3077. xaxis: {
  3078. mode: "categories",
  3079. categories: { "February": 1, "March": 3, "April": 4 }
  3080. }
  3081. If you don't specify all categories, the remaining categories will be numbered
  3082. from the max value plus 1 (with a spacing of 1 between each).
  3083. Internally, the plugin works by transforming the input data through an auto-
  3084. generated mapping where the first category becomes 0, the second 1, etc.
  3085. Hence, a point like ["February", 34] becomes [0, 34] internally in Flot (this
  3086. is visible in hover and click events that return numbers rather than the
  3087. category labels). The plugin also overrides the tick generator to spit out the
  3088. categories as ticks instead of the values.
  3089. If you need to map a value back to its label, the mapping is always accessible
  3090. as "categories" on the axis object, e.g. plot.getAxes().xaxis.categories.
  3091. */
  3092. (function ($) {
  3093. var options = {
  3094. xaxis: {
  3095. categories: null
  3096. },
  3097. yaxis: {
  3098. categories: null
  3099. }
  3100. };
  3101. function processRawData(plot, series, data, datapoints) {
  3102. // if categories are enabled, we need to disable
  3103. // auto-transformation to numbers so the strings are intact
  3104. // for later processing
  3105. var xCategories = series.xaxis.options.mode == "categories",
  3106. yCategories = series.yaxis.options.mode == "categories";
  3107. if (!(xCategories || yCategories))
  3108. return;
  3109. var format = datapoints.format;
  3110. if (!format) {
  3111. // FIXME: auto-detection should really not be defined here
  3112. var s = series;
  3113. format = [];
  3114. format.push({ x: true, number: true, required: true });
  3115. format.push({ y: true, number: true, required: true });
  3116. if (s.bars.show || (s.lines.show && s.lines.fill)) {
  3117. var autoscale = !!((s.bars.show && s.bars.zero) || (s.lines.show && s.lines.zero));
  3118. format.push({ y: true, number: true, required: false, defaultValue: 0, autoscale: autoscale });
  3119. if (s.bars.horizontal) {
  3120. delete format[format.length - 1].y;
  3121. format[format.length - 1].x = true;
  3122. }
  3123. }
  3124. datapoints.format = format;
  3125. }
  3126. for (var m = 0; m < format.length; ++m) {
  3127. if (format[m].x && xCategories)
  3128. format[m].number = false;
  3129. if (format[m].y && yCategories)
  3130. format[m].number = false;
  3131. }
  3132. }
  3133. function getNextIndex(categories) {
  3134. var index = -1;
  3135. for (var v in categories)
  3136. if (categories[v] > index)
  3137. index = categories[v];
  3138. return index + 1;
  3139. }
  3140. function categoriesTickGenerator(axis) {
  3141. var res = [];
  3142. for (var label in axis.categories) {
  3143. var v = axis.categories[label];
  3144. if (v >= axis.min && v <= axis.max)
  3145. res.push([v, label]);
  3146. }
  3147. res.sort(function (a, b) { return a[0] - b[0]; });
  3148. return res;
  3149. }
  3150. function setupCategoriesForAxis(series, axis, datapoints) {
  3151. if (series[axis].options.mode != "categories")
  3152. return;
  3153. if (!series[axis].categories) {
  3154. // parse options
  3155. var c = {}, o = series[axis].options.categories || {};
  3156. if ($.isArray(o)) {
  3157. for (var i = 0; i < o.length; ++i)
  3158. c[o[i]] = i;
  3159. }
  3160. else {
  3161. for (var v in o)
  3162. c[v] = o[v];
  3163. }
  3164. series[axis].categories = c;
  3165. }
  3166. // fix ticks
  3167. if (!series[axis].options.ticks)
  3168. series[axis].options.ticks = categoriesTickGenerator;
  3169. transformPointsOnAxis(datapoints, axis, series[axis].categories);
  3170. }
  3171. function transformPointsOnAxis(datapoints, axis, categories) {
  3172. // go through the points, transforming them
  3173. var points = datapoints.points,
  3174. ps = datapoints.pointsize,
  3175. format = datapoints.format,
  3176. formatColumn = axis.charAt(0),
  3177. index = getNextIndex(categories);
  3178. for (var i = 0; i < points.length; i += ps) {
  3179. if (points[i] == null)
  3180. continue;
  3181. for (var m = 0; m < ps; ++m) {
  3182. var val = points[i + m];
  3183. if (val == null || !format[m][formatColumn])
  3184. continue;
  3185. if (!(val in categories)) {
  3186. categories[val] = index;
  3187. ++index;
  3188. }
  3189. points[i + m] = categories[val];
  3190. }
  3191. }
  3192. }
  3193. function processDatapoints(plot, series, datapoints) {
  3194. setupCategoriesForAxis(series, "xaxis", datapoints);
  3195. setupCategoriesForAxis(series, "yaxis", datapoints);
  3196. }
  3197. function init(plot) {
  3198. plot.hooks.processRawData.push(processRawData);
  3199. plot.hooks.processDatapoints.push(processDatapoints);
  3200. }
  3201. $.plot.plugins.push({
  3202. init: init,
  3203. options: options,
  3204. name: 'categories',
  3205. version: '1.0'
  3206. });
  3207. })(jQuery);
  3208. /* Flot plugin for showing crosshairs when the mouse hovers over the plot.
  3209. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  3210. Licensed under the MIT license.
  3211. The plugin supports these options:
  3212. crosshair: {
  3213. mode: null or "x" or "y" or "xy"
  3214. color: color
  3215. lineWidth: number
  3216. }
  3217. Set the mode to one of "x", "y" or "xy". The "x" mode enables a vertical
  3218. crosshair that lets you trace the values on the x axis, "y" enables a
  3219. horizontal crosshair and "xy" enables them both. "color" is the color of the
  3220. crosshair (default is "rgba(170, 0, 0, 0.80)"), "lineWidth" is the width of
  3221. the drawn lines (default is 1).
  3222. The plugin also adds four public methods:
  3223. - setCrosshair( pos )
  3224. Set the position of the crosshair. Note that this is cleared if the user
  3225. moves the mouse. "pos" is in coordinates of the plot and should be on the
  3226. form { x: xpos, y: ypos } (you can use x2/x3/... if you're using multiple
  3227. axes), which is coincidentally the same format as what you get from a
  3228. "plothover" event. If "pos" is null, the crosshair is cleared.
  3229. - clearCrosshair()
  3230. Clear the crosshair.
  3231. - lockCrosshair(pos)
  3232. Cause the crosshair to lock to the current location, no longer updating if
  3233. the user moves the mouse. Optionally supply a position (passed on to
  3234. setCrosshair()) to move it to.
  3235. Example usage:
  3236. var myFlot = $.plot( $("#graph"), ..., { crosshair: { mode: "x" } } };
  3237. $("#graph").bind( "plothover", function ( evt, position, item ) {
  3238. if ( item ) {
  3239. // Lock the crosshair to the data point being hovered
  3240. myFlot.lockCrosshair({
  3241. x: item.datapoint[ 0 ],
  3242. y: item.datapoint[ 1 ]
  3243. });
  3244. } else {
  3245. // Return normal crosshair operation
  3246. myFlot.unlockCrosshair();
  3247. }
  3248. });
  3249. - unlockCrosshair()
  3250. Free the crosshair to move again after locking it.
  3251. */
  3252. (function ($) {
  3253. var options = {
  3254. crosshair: {
  3255. mode: null, // one of null, "x", "y" or "xy",
  3256. color: "rgba(170, 0, 0, 0.80)",
  3257. lineWidth: 1
  3258. }
  3259. };
  3260. function init(plot) {
  3261. // position of crosshair in pixels
  3262. var crosshair = { x: -1, y: -1, locked: false };
  3263. plot.setCrosshair = function setCrosshair(pos) {
  3264. if (!pos)
  3265. crosshair.x = -1;
  3266. else {
  3267. var o = plot.p2c(pos);
  3268. crosshair.x = Math.max(0, Math.min(o.left, plot.width()));
  3269. crosshair.y = Math.max(0, Math.min(o.top, plot.height()));
  3270. }
  3271. plot.triggerRedrawOverlay();
  3272. };
  3273. plot.clearCrosshair = plot.setCrosshair; // passes null for pos
  3274. plot.lockCrosshair = function lockCrosshair(pos) {
  3275. if (pos)
  3276. plot.setCrosshair(pos);
  3277. crosshair.locked = true;
  3278. };
  3279. plot.unlockCrosshair = function unlockCrosshair() {
  3280. crosshair.locked = false;
  3281. };
  3282. function onMouseOut(e) {
  3283. if (crosshair.locked)
  3284. return;
  3285. if (crosshair.x != -1) {
  3286. crosshair.x = -1;
  3287. plot.triggerRedrawOverlay();
  3288. }
  3289. }
  3290. function onMouseMove(e) {
  3291. if (crosshair.locked)
  3292. return;
  3293. if (plot.getSelection && plot.getSelection()) {
  3294. crosshair.x = -1; // hide the crosshair while selecting
  3295. return;
  3296. }
  3297. var offset = plot.offset();
  3298. crosshair.x = Math.max(0, Math.min(e.pageX - offset.left, plot.width()));
  3299. crosshair.y = Math.max(0, Math.min(e.pageY - offset.top, plot.height()));
  3300. plot.triggerRedrawOverlay();
  3301. }
  3302. plot.hooks.bindEvents.push(function (plot, eventHolder) {
  3303. if (!plot.getOptions().crosshair.mode)
  3304. return;
  3305. eventHolder.mouseout(onMouseOut);
  3306. eventHolder.mousemove(onMouseMove);
  3307. });
  3308. plot.hooks.drawOverlay.push(function (plot, ctx) {
  3309. var c = plot.getOptions().crosshair;
  3310. if (!c.mode)
  3311. return;
  3312. var plotOffset = plot.getPlotOffset();
  3313. ctx.save();
  3314. ctx.translate(plotOffset.left, plotOffset.top);
  3315. if (crosshair.x != -1) {
  3316. var adj = plot.getOptions().crosshair.lineWidth % 2 ? 0.5 : 0;
  3317. ctx.strokeStyle = c.color;
  3318. ctx.lineWidth = c.lineWidth;
  3319. ctx.lineJoin = "round";
  3320. ctx.beginPath();
  3321. if (c.mode.indexOf("x") != -1) {
  3322. var drawX = Math.floor(crosshair.x) + adj;
  3323. ctx.moveTo(drawX, 0);
  3324. ctx.lineTo(drawX, plot.height());
  3325. }
  3326. if (c.mode.indexOf("y") != -1) {
  3327. var drawY = Math.floor(crosshair.y) + adj;
  3328. ctx.moveTo(0, drawY);
  3329. ctx.lineTo(plot.width(), drawY);
  3330. }
  3331. ctx.stroke();
  3332. }
  3333. ctx.restore();
  3334. });
  3335. plot.hooks.shutdown.push(function (plot, eventHolder) {
  3336. eventHolder.unbind("mouseout", onMouseOut);
  3337. eventHolder.unbind("mousemove", onMouseMove);
  3338. });
  3339. }
  3340. $.plot.plugins.push({
  3341. init: init,
  3342. options: options,
  3343. name: 'crosshair',
  3344. version: '1.0'
  3345. });
  3346. })(jQuery);
  3347. /* Flot plugin for plotting error bars.
  3348. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  3349. Licensed under the MIT license.
  3350. Error bars are used to show standard deviation and other statistical
  3351. properties in a plot.
  3352. * Created by Rui Pereira - rui (dot) pereira (at) gmail (dot) com
  3353. This plugin allows you to plot error-bars over points. Set "errorbars" inside
  3354. the points series to the axis name over which there will be error values in
  3355. your data array (*even* if you do not intend to plot them later, by setting
  3356. "show: null" on xerr/yerr).
  3357. The plugin supports these options:
  3358. series: {
  3359. points: {
  3360. errorbars: "x" or "y" or "xy",
  3361. xerr: {
  3362. show: null/false or true,
  3363. asymmetric: null/false or true,
  3364. upperCap: null or "-" or function,
  3365. lowerCap: null or "-" or function,
  3366. color: null or color,
  3367. radius: null or number
  3368. },
  3369. yerr: { same options as xerr }
  3370. }
  3371. }
  3372. Each data point array is expected to be of the type:
  3373. "x" [ x, y, xerr ]
  3374. "y" [ x, y, yerr ]
  3375. "xy" [ x, y, xerr, yerr ]
  3376. Where xerr becomes xerr_lower,xerr_upper for the asymmetric error case, and
  3377. equivalently for yerr. Eg., a datapoint for the "xy" case with symmetric
  3378. error-bars on X and asymmetric on Y would be:
  3379. [ x, y, xerr, yerr_lower, yerr_upper ]
  3380. By default no end caps are drawn. Setting upperCap and/or lowerCap to "-" will
  3381. draw a small cap perpendicular to the error bar. They can also be set to a
  3382. user-defined drawing function, with (ctx, x, y, radius) as parameters, as eg.
  3383. function drawSemiCircle( ctx, x, y, radius ) {
  3384. ctx.beginPath();
  3385. ctx.arc( x, y, radius, 0, Math.PI, false );
  3386. ctx.moveTo( x - radius, y );
  3387. ctx.lineTo( x + radius, y );
  3388. ctx.stroke();
  3389. }
  3390. Color and radius both default to the same ones of the points series if not
  3391. set. The independent radius parameter on xerr/yerr is useful for the case when
  3392. we may want to add error-bars to a line, without showing the interconnecting
  3393. points (with radius: 0), and still showing end caps on the error-bars.
  3394. shadowSize and lineWidth are derived as well from the points series.
  3395. */
  3396. (function ($) {
  3397. var options = {
  3398. series: {
  3399. points: {
  3400. errorbars: null, //should be 'x', 'y' or 'xy'
  3401. xerr: { err: 'x', show: null, asymmetric: null, upperCap: null, lowerCap: null, color: null, radius: null},
  3402. yerr: { err: 'y', show: null, asymmetric: null, upperCap: null, lowerCap: null, color: null, radius: null}
  3403. }
  3404. }
  3405. };
  3406. function processRawData(plot, series, data, datapoints){
  3407. if (!series.points.errorbars)
  3408. return;
  3409. // x,y values
  3410. var format = [
  3411. { x: true, number: true, required: true },
  3412. { y: true, number: true, required: true }
  3413. ];
  3414. var errors = series.points.errorbars;
  3415. // error bars - first X then Y
  3416. if (errors == 'x' || errors == 'xy') {
  3417. // lower / upper error
  3418. if (series.points.xerr.asymmetric) {
  3419. format.push({ x: true, number: true, required: true });
  3420. format.push({ x: true, number: true, required: true });
  3421. } else
  3422. format.push({ x: true, number: true, required: true });
  3423. }
  3424. if (errors == 'y' || errors == 'xy') {
  3425. // lower / upper error
  3426. if (series.points.yerr.asymmetric) {
  3427. format.push({ y: true, number: true, required: true });
  3428. format.push({ y: true, number: true, required: true });
  3429. } else
  3430. format.push({ y: true, number: true, required: true });
  3431. }
  3432. datapoints.format = format;
  3433. }
  3434. function parseErrors(series, i){
  3435. var points = series.datapoints.points;
  3436. // read errors from points array
  3437. var exl = null,
  3438. exu = null,
  3439. eyl = null,
  3440. eyu = null;
  3441. var xerr = series.points.xerr,
  3442. yerr = series.points.yerr;
  3443. var eb = series.points.errorbars;
  3444. // error bars - first X
  3445. if (eb == 'x' || eb == 'xy') {
  3446. if (xerr.asymmetric) {
  3447. exl = points[i + 2];
  3448. exu = points[i + 3];
  3449. if (eb == 'xy')
  3450. if (yerr.asymmetric){
  3451. eyl = points[i + 4];
  3452. eyu = points[i + 5];
  3453. } else eyl = points[i + 4];
  3454. } else {
  3455. exl = points[i + 2];
  3456. if (eb == 'xy')
  3457. if (yerr.asymmetric) {
  3458. eyl = points[i + 3];
  3459. eyu = points[i + 4];
  3460. } else eyl = points[i + 3];
  3461. }
  3462. // only Y
  3463. } else if (eb == 'y')
  3464. if (yerr.asymmetric) {
  3465. eyl = points[i + 2];
  3466. eyu = points[i + 3];
  3467. } else eyl = points[i + 2];
  3468. // symmetric errors?
  3469. if (exu == null) exu = exl;
  3470. if (eyu == null) eyu = eyl;
  3471. var errRanges = [exl, exu, eyl, eyu];
  3472. // nullify if not showing
  3473. if (!xerr.show){
  3474. errRanges[0] = null;
  3475. errRanges[1] = null;
  3476. }
  3477. if (!yerr.show){
  3478. errRanges[2] = null;
  3479. errRanges[3] = null;
  3480. }
  3481. return errRanges;
  3482. }
  3483. function drawSeriesErrors(plot, ctx, s){
  3484. var points = s.datapoints.points,
  3485. ps = s.datapoints.pointsize,
  3486. ax = [s.xaxis, s.yaxis],
  3487. radius = s.points.radius,
  3488. err = [s.points.xerr, s.points.yerr];
  3489. //sanity check, in case some inverted axis hack is applied to flot
  3490. var invertX = false;
  3491. if (ax[0].p2c(ax[0].max) < ax[0].p2c(ax[0].min)) {
  3492. invertX = true;
  3493. var tmp = err[0].lowerCap;
  3494. err[0].lowerCap = err[0].upperCap;
  3495. err[0].upperCap = tmp;
  3496. }
  3497. var invertY = false;
  3498. if (ax[1].p2c(ax[1].min) < ax[1].p2c(ax[1].max)) {
  3499. invertY = true;
  3500. var tmp = err[1].lowerCap;
  3501. err[1].lowerCap = err[1].upperCap;
  3502. err[1].upperCap = tmp;
  3503. }
  3504. for (var i = 0; i < s.datapoints.points.length; i += ps) {
  3505. //parse
  3506. var errRanges = parseErrors(s, i);
  3507. //cycle xerr & yerr
  3508. for (var e = 0; e < err.length; e++){
  3509. var minmax = [ax[e].min, ax[e].max];
  3510. //draw this error?
  3511. if (errRanges[e * err.length]){
  3512. //data coordinates
  3513. var x = points[i],
  3514. y = points[i + 1];
  3515. //errorbar ranges
  3516. var upper = [x, y][e] + errRanges[e * err.length + 1],
  3517. lower = [x, y][e] - errRanges[e * err.length];
  3518. //points outside of the canvas
  3519. if (err[e].err == 'x')
  3520. if (y > ax[1].max || y < ax[1].min || upper < ax[0].min || lower > ax[0].max)
  3521. continue;
  3522. if (err[e].err == 'y')
  3523. if (x > ax[0].max || x < ax[0].min || upper < ax[1].min || lower > ax[1].max)
  3524. continue;
  3525. // prevent errorbars getting out of the canvas
  3526. var drawUpper = true,
  3527. drawLower = true;
  3528. if (upper > minmax[1]) {
  3529. drawUpper = false;
  3530. upper = minmax[1];
  3531. }
  3532. if (lower < minmax[0]) {
  3533. drawLower = false;
  3534. lower = minmax[0];
  3535. }
  3536. //sanity check, in case some inverted axis hack is applied to flot
  3537. if ((err[e].err == 'x' && invertX) || (err[e].err == 'y' && invertY)) {
  3538. //swap coordinates
  3539. var tmp = lower;
  3540. lower = upper;
  3541. upper = tmp;
  3542. tmp = drawLower;
  3543. drawLower = drawUpper;
  3544. drawUpper = tmp;
  3545. tmp = minmax[0];
  3546. minmax[0] = minmax[1];
  3547. minmax[1] = tmp;
  3548. }
  3549. // convert to pixels
  3550. x = ax[0].p2c(x),
  3551. y = ax[1].p2c(y),
  3552. upper = ax[e].p2c(upper);
  3553. lower = ax[e].p2c(lower);
  3554. minmax[0] = ax[e].p2c(minmax[0]);
  3555. minmax[1] = ax[e].p2c(minmax[1]);
  3556. //same style as points by default
  3557. var lw = err[e].lineWidth ? err[e].lineWidth : s.points.lineWidth,
  3558. sw = s.points.shadowSize != null ? s.points.shadowSize : s.shadowSize;
  3559. //shadow as for points
  3560. if (lw > 0 && sw > 0) {
  3561. var w = sw / 2;
  3562. ctx.lineWidth = w;
  3563. ctx.strokeStyle = "rgba(0,0,0,0.1)";
  3564. drawError(ctx, err[e], x, y, upper, lower, drawUpper, drawLower, radius, w + w/2, minmax);
  3565. ctx.strokeStyle = "rgba(0,0,0,0.2)";
  3566. drawError(ctx, err[e], x, y, upper, lower, drawUpper, drawLower, radius, w/2, minmax);
  3567. }
  3568. ctx.strokeStyle = err[e].color? err[e].color: s.color;
  3569. ctx.lineWidth = lw;
  3570. //draw it
  3571. drawError(ctx, err[e], x, y, upper, lower, drawUpper, drawLower, radius, 0, minmax);
  3572. }
  3573. }
  3574. }
  3575. }
  3576. function drawError(ctx,err,x,y,upper,lower,drawUpper,drawLower,radius,offset,minmax){
  3577. //shadow offset
  3578. y += offset;
  3579. upper += offset;
  3580. lower += offset;
  3581. // error bar - avoid plotting over circles
  3582. if (err.err == 'x'){
  3583. if (upper > x + radius) drawPath(ctx, [[upper,y],[Math.max(x + radius,minmax[0]),y]]);
  3584. else drawUpper = false;
  3585. if (lower < x - radius) drawPath(ctx, [[Math.min(x - radius,minmax[1]),y],[lower,y]] );
  3586. else drawLower = false;
  3587. }
  3588. else {
  3589. if (upper < y - radius) drawPath(ctx, [[x,upper],[x,Math.min(y - radius,minmax[0])]] );
  3590. else drawUpper = false;
  3591. if (lower > y + radius) drawPath(ctx, [[x,Math.max(y + radius,minmax[1])],[x,lower]] );
  3592. else drawLower = false;
  3593. }
  3594. //internal radius value in errorbar, allows to plot radius 0 points and still keep proper sized caps
  3595. //this is a way to get errorbars on lines without visible connecting dots
  3596. radius = err.radius != null? err.radius: radius;
  3597. // upper cap
  3598. if (drawUpper) {
  3599. if (err.upperCap == '-'){
  3600. if (err.err=='x') drawPath(ctx, [[upper,y - radius],[upper,y + radius]] );
  3601. else drawPath(ctx, [[x - radius,upper],[x + radius,upper]] );
  3602. } else if ($.isFunction(err.upperCap)){
  3603. if (err.err=='x') err.upperCap(ctx, upper, y, radius);
  3604. else err.upperCap(ctx, x, upper, radius);
  3605. }
  3606. }
  3607. // lower cap
  3608. if (drawLower) {
  3609. if (err.lowerCap == '-'){
  3610. if (err.err=='x') drawPath(ctx, [[lower,y - radius],[lower,y + radius]] );
  3611. else drawPath(ctx, [[x - radius,lower],[x + radius,lower]] );
  3612. } else if ($.isFunction(err.lowerCap)){
  3613. if (err.err=='x') err.lowerCap(ctx, lower, y, radius);
  3614. else err.lowerCap(ctx, x, lower, radius);
  3615. }
  3616. }
  3617. }
  3618. function drawPath(ctx, pts){
  3619. ctx.beginPath();
  3620. ctx.moveTo(pts[0][0], pts[0][1]);
  3621. for (var p=1; p < pts.length; p++)
  3622. ctx.lineTo(pts[p][0], pts[p][1]);
  3623. ctx.stroke();
  3624. }
  3625. function draw(plot, ctx){
  3626. var plotOffset = plot.getPlotOffset();
  3627. ctx.save();
  3628. ctx.translate(plotOffset.left, plotOffset.top);
  3629. $.each(plot.getData(), function (i, s) {
  3630. if (s.points.errorbars && (s.points.xerr.show || s.points.yerr.show))
  3631. drawSeriesErrors(plot, ctx, s);
  3632. });
  3633. ctx.restore();
  3634. }
  3635. function init(plot) {
  3636. plot.hooks.processRawData.push(processRawData);
  3637. plot.hooks.draw.push(draw);
  3638. }
  3639. $.plot.plugins.push({
  3640. init: init,
  3641. options: options,
  3642. name: 'errorbars',
  3643. version: '1.0'
  3644. });
  3645. })(jQuery);
  3646. /* Flot plugin for computing bottoms for filled line and bar charts.
  3647. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  3648. Licensed under the MIT license.
  3649. The case: you've got two series that you want to fill the area between. In Flot
  3650. terms, you need to use one as the fill bottom of the other. You can specify the
  3651. bottom of each data point as the third coordinate manually, or you can use this
  3652. plugin to compute it for you.
  3653. In order to name the other series, you need to give it an id, like this:
  3654. var dataset = [
  3655. { data: [ ... ], id: "foo" } , // use default bottom
  3656. { data: [ ... ], fillBetween: "foo" }, // use first dataset as bottom
  3657. ];
  3658. $.plot($("#placeholder"), dataset, { lines: { show: true, fill: true }});
  3659. As a convenience, if the id given is a number that doesn't appear as an id in
  3660. the series, it is interpreted as the index in the array instead (so fillBetween:
  3661. 0 can also mean the first series).
  3662. Internally, the plugin modifies the datapoints in each series. For line series,
  3663. extra data points might be inserted through interpolation. Note that at points
  3664. where the bottom line is not defined (due to a null point or start/end of line),
  3665. the current line will show a gap too. The algorithm comes from the
  3666. jquery.flot.stack.js plugin, possibly some code could be shared.
  3667. */
  3668. (function ( $ ) {
  3669. var options = {
  3670. series: {
  3671. fillBetween: null // or number
  3672. }
  3673. };
  3674. function init( plot ) {
  3675. function findBottomSeries( s, allseries ) {
  3676. var i;
  3677. for ( i = 0; i < allseries.length; ++i ) {
  3678. if ( allseries[ i ].id === s.fillBetween ) {
  3679. return allseries[ i ];
  3680. }
  3681. }
  3682. if ( typeof s.fillBetween === "number" ) {
  3683. if ( s.fillBetween < 0 || s.fillBetween >= allseries.length ) {
  3684. return null;
  3685. }
  3686. return allseries[ s.fillBetween ];
  3687. }
  3688. return null;
  3689. }
  3690. function computeFillBottoms( plot, s, datapoints ) {
  3691. if ( s.fillBetween == null ) {
  3692. return;
  3693. }
  3694. var other = findBottomSeries( s, plot.getData() );
  3695. if ( !other ) {
  3696. return;
  3697. }
  3698. var ps = datapoints.pointsize,
  3699. points = datapoints.points,
  3700. otherps = other.datapoints.pointsize,
  3701. otherpoints = other.datapoints.points,
  3702. newpoints = [],
  3703. px, py, intery, qx, qy, bottom,
  3704. withlines = s.lines.show,
  3705. withbottom = ps > 2 && datapoints.format[2].y,
  3706. withsteps = withlines && s.lines.steps,
  3707. fromgap = true,
  3708. i = 0,
  3709. j = 0,
  3710. l, m;
  3711. while ( true ) {
  3712. if ( i >= points.length ) {
  3713. break;
  3714. }
  3715. l = newpoints.length;
  3716. if ( points[ i ] == null ) {
  3717. // copy gaps
  3718. for ( m = 0; m < ps; ++m ) {
  3719. newpoints.push( points[ i + m ] );
  3720. }
  3721. i += ps;
  3722. } else if ( j >= otherpoints.length ) {
  3723. // for lines, we can't use the rest of the points
  3724. if ( !withlines ) {
  3725. for ( m = 0; m < ps; ++m ) {
  3726. newpoints.push( points[ i + m ] );
  3727. }
  3728. }
  3729. i += ps;
  3730. } else if ( otherpoints[ j ] == null ) {
  3731. // oops, got a gap
  3732. for ( m = 0; m < ps; ++m ) {
  3733. newpoints.push( null );
  3734. }
  3735. fromgap = true;
  3736. j += otherps;
  3737. } else {
  3738. // cases where we actually got two points
  3739. px = points[ i ];
  3740. py = points[ i + 1 ];
  3741. qx = otherpoints[ j ];
  3742. qy = otherpoints[ j + 1 ];
  3743. bottom = 0;
  3744. if ( px === qx ) {
  3745. for ( m = 0; m < ps; ++m ) {
  3746. newpoints.push( points[ i + m ] );
  3747. }
  3748. //newpoints[ l + 1 ] += qy;
  3749. bottom = qy;
  3750. i += ps;
  3751. j += otherps;
  3752. } else if ( px > qx ) {
  3753. // we got past point below, might need to
  3754. // insert interpolated extra point
  3755. if ( withlines && i > 0 && points[ i - ps ] != null ) {
  3756. intery = py + ( points[ i - ps + 1 ] - py ) * ( qx - px ) / ( points[ i - ps ] - px );
  3757. newpoints.push( qx );
  3758. newpoints.push( intery );
  3759. for ( m = 2; m < ps; ++m ) {
  3760. newpoints.push( points[ i + m ] );
  3761. }
  3762. bottom = qy;
  3763. }
  3764. j += otherps;
  3765. } else { // px < qx
  3766. // if we come from a gap, we just skip this point
  3767. if ( fromgap && withlines ) {
  3768. i += ps;
  3769. continue;
  3770. }
  3771. for ( m = 0; m < ps; ++m ) {
  3772. newpoints.push( points[ i + m ] );
  3773. }
  3774. // we might be able to interpolate a point below,
  3775. // this can give us a better y
  3776. if ( withlines && j > 0 && otherpoints[ j - otherps ] != null ) {
  3777. bottom = qy + ( otherpoints[ j - otherps + 1 ] - qy ) * ( px - qx ) / ( otherpoints[ j - otherps ] - qx );
  3778. }
  3779. //newpoints[l + 1] += bottom;
  3780. i += ps;
  3781. }
  3782. fromgap = false;
  3783. if ( l !== newpoints.length && withbottom ) {
  3784. newpoints[ l + 2 ] = bottom;
  3785. }
  3786. }
  3787. // maintain the line steps invariant
  3788. if ( withsteps && l !== newpoints.length && l > 0 &&
  3789. newpoints[ l ] !== null &&
  3790. newpoints[ l ] !== newpoints[ l - ps ] &&
  3791. newpoints[ l + 1 ] !== newpoints[ l - ps + 1 ] ) {
  3792. for (m = 0; m < ps; ++m) {
  3793. newpoints[ l + ps + m ] = newpoints[ l + m ];
  3794. }
  3795. newpoints[ l + 1 ] = newpoints[ l - ps + 1 ];
  3796. }
  3797. }
  3798. datapoints.points = newpoints;
  3799. }
  3800. plot.hooks.processDatapoints.push( computeFillBottoms );
  3801. }
  3802. $.plot.plugins.push({
  3803. init: init,
  3804. options: options,
  3805. name: "fillbetween",
  3806. version: "1.0"
  3807. });
  3808. })(jQuery);
  3809. /* Flot plugin for plotting images.
  3810. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  3811. Licensed under the MIT license.
  3812. The data syntax is [ [ image, x1, y1, x2, y2 ], ... ] where (x1, y1) and
  3813. (x2, y2) are where you intend the two opposite corners of the image to end up
  3814. in the plot. Image must be a fully loaded Javascript image (you can make one
  3815. with new Image()). If the image is not complete, it's skipped when plotting.
  3816. There are two helpers included for retrieving images. The easiest work the way
  3817. that you put in URLs instead of images in the data, like this:
  3818. [ "myimage.png", 0, 0, 10, 10 ]
  3819. Then call $.plot.image.loadData( data, options, callback ) where data and
  3820. options are the same as you pass in to $.plot. This loads the images, replaces
  3821. the URLs in the data with the corresponding images and calls "callback" when
  3822. all images are loaded (or failed loading). In the callback, you can then call
  3823. $.plot with the data set. See the included example.
  3824. A more low-level helper, $.plot.image.load(urls, callback) is also included.
  3825. Given a list of URLs, it calls callback with an object mapping from URL to
  3826. Image object when all images are loaded or have failed loading.
  3827. The plugin supports these options:
  3828. series: {
  3829. images: {
  3830. show: boolean
  3831. anchor: "corner" or "center"
  3832. alpha: [ 0, 1 ]
  3833. }
  3834. }
  3835. They can be specified for a specific series:
  3836. $.plot( $("#placeholder"), [{
  3837. data: [ ... ],
  3838. images: { ... }
  3839. ])
  3840. Note that because the data format is different from usual data points, you
  3841. can't use images with anything else in a specific data series.
  3842. Setting "anchor" to "center" causes the pixels in the image to be anchored at
  3843. the corner pixel centers inside of at the pixel corners, effectively letting
  3844. half a pixel stick out to each side in the plot.
  3845. A possible future direction could be support for tiling for large images (like
  3846. Google Maps).
  3847. */
  3848. (function ($) {
  3849. var options = {
  3850. series: {
  3851. images: {
  3852. show: false,
  3853. alpha: 1,
  3854. anchor: "corner" // or "center"
  3855. }
  3856. }
  3857. };
  3858. $.plot.image = {};
  3859. $.plot.image.loadDataImages = function (series, options, callback) {
  3860. var urls = [], points = [];
  3861. var defaultShow = options.series.images.show;
  3862. $.each(series, function (i, s) {
  3863. if (!(defaultShow || s.images.show))
  3864. return;
  3865. if (s.data)
  3866. s = s.data;
  3867. $.each(s, function (i, p) {
  3868. if (typeof p[0] == "string") {
  3869. urls.push(p[0]);
  3870. points.push(p);
  3871. }
  3872. });
  3873. });
  3874. $.plot.image.load(urls, function (loadedImages) {
  3875. $.each(points, function (i, p) {
  3876. var url = p[0];
  3877. if (loadedImages[url])
  3878. p[0] = loadedImages[url];
  3879. });
  3880. callback();
  3881. });
  3882. }
  3883. $.plot.image.load = function (urls, callback) {
  3884. var missing = urls.length, loaded = {};
  3885. if (missing == 0)
  3886. callback({});
  3887. $.each(urls, function (i, url) {
  3888. var handler = function () {
  3889. --missing;
  3890. loaded[url] = this;
  3891. if (missing == 0)
  3892. callback(loaded);
  3893. };
  3894. $('<img />').load(handler).error(handler).attr('src', url);
  3895. });
  3896. };
  3897. function drawSeries(plot, ctx, series) {
  3898. var plotOffset = plot.getPlotOffset();
  3899. if (!series.images || !series.images.show)
  3900. return;
  3901. var points = series.datapoints.points,
  3902. ps = series.datapoints.pointsize;
  3903. for (var i = 0; i < points.length; i += ps) {
  3904. var img = points[i],
  3905. x1 = points[i + 1], y1 = points[i + 2],
  3906. x2 = points[i + 3], y2 = points[i + 4],
  3907. xaxis = series.xaxis, yaxis = series.yaxis,
  3908. tmp;
  3909. // actually we should check img.complete, but it
  3910. // appears to be a somewhat unreliable indicator in
  3911. // IE6 (false even after load event)
  3912. if (!img || img.width <= 0 || img.height <= 0)
  3913. continue;
  3914. if (x1 > x2) {
  3915. tmp = x2;
  3916. x2 = x1;
  3917. x1 = tmp;
  3918. }
  3919. if (y1 > y2) {
  3920. tmp = y2;
  3921. y2 = y1;
  3922. y1 = tmp;
  3923. }
  3924. // if the anchor is at the center of the pixel, expand the
  3925. // image by 1/2 pixel in each direction
  3926. if (series.images.anchor == "center") {
  3927. tmp = 0.5 * (x2-x1) / (img.width - 1);
  3928. x1 -= tmp;
  3929. x2 += tmp;
  3930. tmp = 0.5 * (y2-y1) / (img.height - 1);
  3931. y1 -= tmp;
  3932. y2 += tmp;
  3933. }
  3934. // clip
  3935. if (x1 == x2 || y1 == y2 ||
  3936. x1 >= xaxis.max || x2 <= xaxis.min ||
  3937. y1 >= yaxis.max || y2 <= yaxis.min)
  3938. continue;
  3939. var sx1 = 0, sy1 = 0, sx2 = img.width, sy2 = img.height;
  3940. if (x1 < xaxis.min) {
  3941. sx1 += (sx2 - sx1) * (xaxis.min - x1) / (x2 - x1);
  3942. x1 = xaxis.min;
  3943. }
  3944. if (x2 > xaxis.max) {
  3945. sx2 += (sx2 - sx1) * (xaxis.max - x2) / (x2 - x1);
  3946. x2 = xaxis.max;
  3947. }
  3948. if (y1 < yaxis.min) {
  3949. sy2 += (sy1 - sy2) * (yaxis.min - y1) / (y2 - y1);
  3950. y1 = yaxis.min;
  3951. }
  3952. if (y2 > yaxis.max) {
  3953. sy1 += (sy1 - sy2) * (yaxis.max - y2) / (y2 - y1);
  3954. y2 = yaxis.max;
  3955. }
  3956. x1 = xaxis.p2c(x1);
  3957. x2 = xaxis.p2c(x2);
  3958. y1 = yaxis.p2c(y1);
  3959. y2 = yaxis.p2c(y2);
  3960. // the transformation may have swapped us
  3961. if (x1 > x2) {
  3962. tmp = x2;
  3963. x2 = x1;
  3964. x1 = tmp;
  3965. }
  3966. if (y1 > y2) {
  3967. tmp = y2;
  3968. y2 = y1;
  3969. y1 = tmp;
  3970. }
  3971. tmp = ctx.globalAlpha;
  3972. ctx.globalAlpha *= series.images.alpha;
  3973. ctx.drawImage(img,
  3974. sx1, sy1, sx2 - sx1, sy2 - sy1,
  3975. x1 + plotOffset.left, y1 + plotOffset.top,
  3976. x2 - x1, y2 - y1);
  3977. ctx.globalAlpha = tmp;
  3978. }
  3979. }
  3980. function processRawData(plot, series, data, datapoints) {
  3981. if (!series.images.show)
  3982. return;
  3983. // format is Image, x1, y1, x2, y2 (opposite corners)
  3984. datapoints.format = [
  3985. { required: true },
  3986. { x: true, number: true, required: true },
  3987. { y: true, number: true, required: true },
  3988. { x: true, number: true, required: true },
  3989. { y: true, number: true, required: true }
  3990. ];
  3991. }
  3992. function init(plot) {
  3993. plot.hooks.processRawData.push(processRawData);
  3994. plot.hooks.drawSeries.push(drawSeries);
  3995. }
  3996. $.plot.plugins.push({
  3997. init: init,
  3998. options: options,
  3999. name: 'image',
  4000. version: '1.1'
  4001. });
  4002. })(jQuery);
  4003. /* Flot plugin for adding the ability to pan and zoom the plot.
  4004. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  4005. Licensed under the MIT license.
  4006. The default behaviour is double click and scrollwheel up/down to zoom in, drag
  4007. to pan. The plugin defines plot.zoom({ center }), plot.zoomOut() and
  4008. plot.pan( offset ) so you easily can add custom controls. It also fires
  4009. "plotpan" and "plotzoom" events, useful for synchronizing plots.
  4010. The plugin supports these options:
  4011. zoom: {
  4012. interactive: false
  4013. trigger: "dblclick" // or "click" for single click
  4014. amount: 1.5 // 2 = 200% (zoom in), 0.5 = 50% (zoom out)
  4015. }
  4016. pan: {
  4017. interactive: false
  4018. cursor: "move" // CSS mouse cursor value used when dragging, e.g. "pointer"
  4019. frameRate: 20
  4020. }
  4021. xaxis, yaxis, x2axis, y2axis: {
  4022. zoomRange: null // or [ number, number ] (min range, max range) or false
  4023. panRange: null // or [ number, number ] (min, max) or false
  4024. }
  4025. "interactive" enables the built-in drag/click behaviour. If you enable
  4026. interactive for pan, then you'll have a basic plot that supports moving
  4027. around; the same for zoom.
  4028. "amount" specifies the default amount to zoom in (so 1.5 = 150%) relative to
  4029. the current viewport.
  4030. "cursor" is a standard CSS mouse cursor string used for visual feedback to the
  4031. user when dragging.
  4032. "frameRate" specifies the maximum number of times per second the plot will
  4033. update itself while the user is panning around on it (set to null to disable
  4034. intermediate pans, the plot will then not update until the mouse button is
  4035. released).
  4036. "zoomRange" is the interval in which zooming can happen, e.g. with zoomRange:
  4037. [1, 100] the zoom will never scale the axis so that the difference between min
  4038. and max is smaller than 1 or larger than 100. You can set either end to null
  4039. to ignore, e.g. [1, null]. If you set zoomRange to false, zooming on that axis
  4040. will be disabled.
  4041. "panRange" confines the panning to stay within a range, e.g. with panRange:
  4042. [-10, 20] panning stops at -10 in one end and at 20 in the other. Either can
  4043. be null, e.g. [-10, null]. If you set panRange to false, panning on that axis
  4044. will be disabled.
  4045. Example API usage:
  4046. plot = $.plot(...);
  4047. // zoom default amount in on the pixel ( 10, 20 )
  4048. plot.zoom({ center: { left: 10, top: 20 } });
  4049. // zoom out again
  4050. plot.zoomOut({ center: { left: 10, top: 20 } });
  4051. // zoom 200% in on the pixel (10, 20)
  4052. plot.zoom({ amount: 2, center: { left: 10, top: 20 } });
  4053. // pan 100 pixels to the left and 20 down
  4054. plot.pan({ left: -100, top: 20 })
  4055. Here, "center" specifies where the center of the zooming should happen. Note
  4056. that this is defined in pixel space, not the space of the data points (you can
  4057. use the p2c helpers on the axes in Flot to help you convert between these).
  4058. "amount" is the amount to zoom the viewport relative to the current range, so
  4059. 1 is 100% (i.e. no change), 1.5 is 150% (zoom in), 0.7 is 70% (zoom out). You
  4060. can set the default in the options.
  4061. */
  4062. // First two dependencies, jquery.event.drag.js and
  4063. // jquery.mousewheel.js, we put them inline here to save people the
  4064. // effort of downloading them.
  4065. /*
  4066. jquery.event.drag.js ~ v1.5 ~ Copyright (c) 2008, Three Dub Media (http://threedubmedia.com)
  4067. Licensed under the MIT License ~ http://threedubmedia.googlecode.com/files/MIT-LICENSE.txt
  4068. */
  4069. (function(a){function e(h){var k,j=this,l=h.data||{};if(l.elem)j=h.dragTarget=l.elem,h.dragProxy=d.proxy||j,h.cursorOffsetX=l.pageX-l.left,h.cursorOffsetY=l.pageY-l.top,h.offsetX=h.pageX-h.cursorOffsetX,h.offsetY=h.pageY-h.cursorOffsetY;else if(d.dragging||l.which>0&&h.which!=l.which||a(h.target).is(l.not))return;switch(h.type){case"mousedown":return a.extend(l,a(j).offset(),{elem:j,target:h.target,pageX:h.pageX,pageY:h.pageY}),b.add(document,"mousemove mouseup",e,l),i(j,!1),d.dragging=null,!1;case!d.dragging&&"mousemove":if(g(h.pageX-l.pageX)+g(h.pageY-l.pageY)<l.distance)break;h.target=l.target,k=f(h,"dragstart",j),k!==!1&&(d.dragging=j,d.proxy=h.dragProxy=a(k||j)[0]);case"mousemove":if(d.dragging){if(k=f(h,"drag",j),c.drop&&(c.drop.allowed=k!==!1,c.drop.handler(h)),k!==!1)break;h.type="mouseup"}case"mouseup":b.remove(document,"mousemove mouseup",e),d.dragging&&(c.drop&&c.drop.handler(h),f(h,"dragend",j)),i(j,!0),d.dragging=d.proxy=l.elem=!1}return!0}function f(b,c,d){b.type=c;var e=a.event.dispatch.call(d,b);return e===!1?!1:e||b.result}function g(a){return Math.pow(a,2)}function h(){return d.dragging===!1}function i(a,b){a&&(a.unselectable=b?"off":"on",a.onselectstart=function(){return b},a.style&&(a.style.MozUserSelect=b?"":"none"))}a.fn.drag=function(a,b,c){return b&&this.bind("dragstart",a),c&&this.bind("dragend",c),a?this.bind("drag",b?b:a):this.trigger("drag")};var b=a.event,c=b.special,d=c.drag={not:":input",distance:0,which:1,dragging:!1,setup:function(c){c=a.extend({distance:d.distance,which:d.which,not:d.not},c||{}),c.distance=g(c.distance),b.add(this,"mousedown",e,c),this.attachEvent&&this.attachEvent("ondragstart",h)},teardown:function(){b.remove(this,"mousedown",e),this===d.dragging&&(d.dragging=d.proxy=!1),i(this,!0),this.detachEvent&&this.detachEvent("ondragstart",h)}};c.dragstart=c.dragend={setup:function(){},teardown:function(){}}})(jQuery);
  4070. /* jquery.mousewheel.min.js
  4071. * Copyright (c) 2011 Brandon Aaron (http://brandonaaron.net)
  4072. * Licensed under the MIT License (LICENSE.txt).
  4073. * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
  4074. * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
  4075. * Thanks to: Seamus Leahy for adding deltaX and deltaY
  4076. *
  4077. * Version: 3.0.6
  4078. *
  4079. * Requires: 1.2.2+
  4080. */
  4081. (function(d){function e(a){var b=a||window.event,c=[].slice.call(arguments,1),f=0,e=0,g=0,a=d.event.fix(b);a.type="mousewheel";b.wheelDelta&&(f=b.wheelDelta/120);b.detail&&(f=-b.detail/3);g=f;void 0!==b.axis&&b.axis===b.HORIZONTAL_AXIS&&(g=0,e=-1*f);void 0!==b.wheelDeltaY&&(g=b.wheelDeltaY/120);void 0!==b.wheelDeltaX&&(e=-1*b.wheelDeltaX/120);c.unshift(a,f,e,g);return(d.event.dispatch||d.event.handle).apply(this,c)}var c=["DOMMouseScroll","mousewheel"];if(d.event.fixHooks)for(var h=c.length;h;)d.event.fixHooks[c[--h]]=d.event.mouseHooks;d.event.special.mousewheel={setup:function(){if(this.addEventListener)for(var a=c.length;a;)this.addEventListener(c[--a],e,!1);else this.onmousewheel=e},teardown:function(){if(this.removeEventListener)for(var a=c.length;a;)this.removeEventListener(c[--a],e,!1);else this.onmousewheel=null}};d.fn.extend({mousewheel:function(a){return a?this.bind("mousewheel",a):this.trigger("mousewheel")},unmousewheel:function(a){return this.unbind("mousewheel",a)}})})(jQuery);
  4082. (function ($) {
  4083. var options = {
  4084. xaxis: {
  4085. zoomRange: null, // or [number, number] (min range, max range)
  4086. panRange: null // or [number, number] (min, max)
  4087. },
  4088. zoom: {
  4089. interactive: false,
  4090. trigger: "dblclick", // or "click" for single click
  4091. amount: 1.5 // how much to zoom relative to current position, 2 = 200% (zoom in), 0.5 = 50% (zoom out)
  4092. },
  4093. pan: {
  4094. interactive: false,
  4095. cursor: "move",
  4096. frameRate: 20
  4097. }
  4098. };
  4099. function init(plot) {
  4100. function onZoomClick(e, zoomOut) {
  4101. var c = plot.offset();
  4102. c.left = e.pageX - c.left;
  4103. c.top = e.pageY - c.top;
  4104. if (zoomOut)
  4105. plot.zoomOut({ center: c });
  4106. else
  4107. plot.zoom({ center: c });
  4108. }
  4109. function onMouseWheel(e, delta) {
  4110. e.preventDefault();
  4111. onZoomClick(e, delta < 0);
  4112. return false;
  4113. }
  4114. var prevCursor = 'default', prevPageX = 0, prevPageY = 0,
  4115. panTimeout = null;
  4116. function onDragStart(e) {
  4117. if (e.which != 1) // only accept left-click
  4118. return false;
  4119. var c = plot.getPlaceholder().css('cursor');
  4120. if (c)
  4121. prevCursor = c;
  4122. plot.getPlaceholder().css('cursor', plot.getOptions().pan.cursor);
  4123. prevPageX = e.pageX;
  4124. prevPageY = e.pageY;
  4125. }
  4126. function onDrag(e) {
  4127. var frameRate = plot.getOptions().pan.frameRate;
  4128. if (panTimeout || !frameRate)
  4129. return;
  4130. panTimeout = setTimeout(function () {
  4131. plot.pan({ left: prevPageX - e.pageX,
  4132. top: prevPageY - e.pageY });
  4133. prevPageX = e.pageX;
  4134. prevPageY = e.pageY;
  4135. panTimeout = null;
  4136. }, 1 / frameRate * 1000);
  4137. }
  4138. function onDragEnd(e) {
  4139. if (panTimeout) {
  4140. clearTimeout(panTimeout);
  4141. panTimeout = null;
  4142. }
  4143. plot.getPlaceholder().css('cursor', prevCursor);
  4144. plot.pan({ left: prevPageX - e.pageX,
  4145. top: prevPageY - e.pageY });
  4146. }
  4147. function bindEvents(plot, eventHolder) {
  4148. var o = plot.getOptions();
  4149. if (o.zoom.interactive) {
  4150. eventHolder[o.zoom.trigger](onZoomClick);
  4151. eventHolder.mousewheel(onMouseWheel);
  4152. }
  4153. if (o.pan.interactive) {
  4154. eventHolder.bind("dragstart", { distance: 10 }, onDragStart);
  4155. eventHolder.bind("drag", onDrag);
  4156. eventHolder.bind("dragend", onDragEnd);
  4157. }
  4158. }
  4159. plot.zoomOut = function (args) {
  4160. if (!args)
  4161. args = {};
  4162. if (!args.amount)
  4163. args.amount = plot.getOptions().zoom.amount;
  4164. args.amount = 1 / args.amount;
  4165. plot.zoom(args);
  4166. };
  4167. plot.zoom = function (args) {
  4168. if (!args)
  4169. args = {};
  4170. var c = args.center,
  4171. amount = args.amount || plot.getOptions().zoom.amount,
  4172. w = plot.width(), h = plot.height();
  4173. if (!c)
  4174. c = { left: w / 2, top: h / 2 };
  4175. var xf = c.left / w,
  4176. yf = c.top / h,
  4177. minmax = {
  4178. x: {
  4179. min: c.left - xf * w / amount,
  4180. max: c.left + (1 - xf) * w / amount
  4181. },
  4182. y: {
  4183. min: c.top - yf * h / amount,
  4184. max: c.top + (1 - yf) * h / amount
  4185. }
  4186. };
  4187. $.each(plot.getAxes(), function(_, axis) {
  4188. var opts = axis.options,
  4189. min = minmax[axis.direction].min,
  4190. max = minmax[axis.direction].max,
  4191. zr = opts.zoomRange,
  4192. pr = opts.panRange;
  4193. if (zr === false) // no zooming on this axis
  4194. return;
  4195. min = axis.c2p(min);
  4196. max = axis.c2p(max);
  4197. if (min > max) {
  4198. // make sure min < max
  4199. var tmp = min;
  4200. min = max;
  4201. max = tmp;
  4202. }
  4203. //Check that we are in panRange
  4204. if (pr) {
  4205. if (pr[0] != null && min < pr[0]) {
  4206. min = pr[0];
  4207. }
  4208. if (pr[1] != null && max > pr[1]) {
  4209. max = pr[1];
  4210. }
  4211. }
  4212. var range = max - min;
  4213. if (zr &&
  4214. ((zr[0] != null && range < zr[0] && amount >1) ||
  4215. (zr[1] != null && range > zr[1] && amount <1)))
  4216. return;
  4217. opts.min = min;
  4218. opts.max = max;
  4219. });
  4220. plot.setupGrid();
  4221. plot.draw();
  4222. if (!args.preventEvent)
  4223. plot.getPlaceholder().trigger("plotzoom", [ plot, args ]);
  4224. };
  4225. plot.pan = function (args) {
  4226. var delta = {
  4227. x: +args.left,
  4228. y: +args.top
  4229. };
  4230. if (isNaN(delta.x))
  4231. delta.x = 0;
  4232. if (isNaN(delta.y))
  4233. delta.y = 0;
  4234. $.each(plot.getAxes(), function (_, axis) {
  4235. var opts = axis.options,
  4236. min, max, d = delta[axis.direction];
  4237. min = axis.c2p(axis.p2c(axis.min) + d),
  4238. max = axis.c2p(axis.p2c(axis.max) + d);
  4239. var pr = opts.panRange;
  4240. if (pr === false) // no panning on this axis
  4241. return;
  4242. if (pr) {
  4243. // check whether we hit the wall
  4244. if (pr[0] != null && pr[0] > min) {
  4245. d = pr[0] - min;
  4246. min += d;
  4247. max += d;
  4248. }
  4249. if (pr[1] != null && pr[1] < max) {
  4250. d = pr[1] - max;
  4251. min += d;
  4252. max += d;
  4253. }
  4254. }
  4255. opts.min = min;
  4256. opts.max = max;
  4257. });
  4258. plot.setupGrid();
  4259. plot.draw();
  4260. if (!args.preventEvent)
  4261. plot.getPlaceholder().trigger("plotpan", [ plot, args ]);
  4262. };
  4263. function shutdown(plot, eventHolder) {
  4264. eventHolder.unbind(plot.getOptions().zoom.trigger, onZoomClick);
  4265. eventHolder.unbind("mousewheel", onMouseWheel);
  4266. eventHolder.unbind("dragstart", onDragStart);
  4267. eventHolder.unbind("drag", onDrag);
  4268. eventHolder.unbind("dragend", onDragEnd);
  4269. if (panTimeout)
  4270. clearTimeout(panTimeout);
  4271. }
  4272. plot.hooks.bindEvents.push(bindEvents);
  4273. plot.hooks.shutdown.push(shutdown);
  4274. }
  4275. $.plot.plugins.push({
  4276. init: init,
  4277. options: options,
  4278. name: 'navigate',
  4279. version: '1.3'
  4280. });
  4281. })(jQuery);
  4282. /* Flot plugin for rendering pie charts.
  4283. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  4284. Licensed under the MIT license.
  4285. The plugin assumes that each series has a single data value, and that each
  4286. value is a positive integer or zero. Negative numbers don't make sense for a
  4287. pie chart, and have unpredictable results. The values do NOT need to be
  4288. passed in as percentages; the plugin will calculate the total and per-slice
  4289. percentages internally.
  4290. * Created by Brian Medendorp
  4291. * Updated with contributions from btburnett3, Anthony Aragues and Xavi Ivars
  4292. The plugin supports these options:
  4293. series: {
  4294. pie: {
  4295. show: true/false
  4296. radius: 0-1 for percentage of fullsize, or a specified pixel length, or 'auto'
  4297. innerRadius: 0-1 for percentage of fullsize or a specified pixel length, for creating a donut effect
  4298. startAngle: 0-2 factor of PI used for starting angle (in radians) i.e 3/2 starts at the top, 0 and 2 have the same result
  4299. tilt: 0-1 for percentage to tilt the pie, where 1 is no tilt, and 0 is completely flat (nothing will show)
  4300. offset: {
  4301. top: integer value to move the pie up or down
  4302. left: integer value to move the pie left or right, or 'auto'
  4303. },
  4304. stroke: {
  4305. color: any hexidecimal color value (other formats may or may not work, so best to stick with something like '#FFF')
  4306. width: integer pixel width of the stroke
  4307. },
  4308. label: {
  4309. show: true/false, or 'auto'
  4310. formatter: a user-defined function that modifies the text/style of the label text
  4311. radius: 0-1 for percentage of fullsize, or a specified pixel length
  4312. background: {
  4313. color: any hexidecimal color value (other formats may or may not work, so best to stick with something like '#000')
  4314. opacity: 0-1
  4315. },
  4316. threshold: 0-1 for the percentage value at which to hide labels (if they're too small)
  4317. },
  4318. combine: {
  4319. threshold: 0-1 for the percentage value at which to combine slices (if they're too small)
  4320. color: any hexidecimal color value (other formats may or may not work, so best to stick with something like '#CCC'), if null, the plugin will automatically use the color of the first slice to be combined
  4321. label: any text value of what the combined slice should be labeled
  4322. }
  4323. highlight: {
  4324. opacity: 0-1
  4325. }
  4326. }
  4327. }
  4328. More detail and specific examples can be found in the included HTML file.
  4329. */
  4330. (function($) {
  4331. // Maximum redraw attempts when fitting labels within the plot
  4332. var REDRAW_ATTEMPTS = 10;
  4333. // Factor by which to shrink the pie when fitting labels within the plot
  4334. var REDRAW_SHRINK = 0.95;
  4335. function init(plot) {
  4336. var canvas = null,
  4337. target = null,
  4338. options = null,
  4339. maxRadius = null,
  4340. centerLeft = null,
  4341. centerTop = null,
  4342. processed = false,
  4343. ctx = null;
  4344. // interactive variables
  4345. var highlights = [];
  4346. // add hook to determine if pie plugin in enabled, and then perform necessary operations
  4347. plot.hooks.processOptions.push(function(plot, options) {
  4348. if (options.series.pie.show) {
  4349. options.grid.show = false;
  4350. // set labels.show
  4351. if (options.series.pie.label.show == "auto") {
  4352. if (options.legend.show) {
  4353. options.series.pie.label.show = false;
  4354. } else {
  4355. options.series.pie.label.show = true;
  4356. }
  4357. }
  4358. // set radius
  4359. if (options.series.pie.radius == "auto") {
  4360. if (options.series.pie.label.show) {
  4361. options.series.pie.radius = 3/4;
  4362. } else {
  4363. options.series.pie.radius = 1;
  4364. }
  4365. }
  4366. // ensure sane tilt
  4367. if (options.series.pie.tilt > 1) {
  4368. options.series.pie.tilt = 1;
  4369. } else if (options.series.pie.tilt < 0) {
  4370. options.series.pie.tilt = 0;
  4371. }
  4372. }
  4373. });
  4374. plot.hooks.bindEvents.push(function(plot, eventHolder) {
  4375. var options = plot.getOptions();
  4376. if (options.series.pie.show) {
  4377. if (options.grid.hoverable) {
  4378. eventHolder.unbind("mousemove").mousemove(onMouseMove);
  4379. }
  4380. if (options.grid.clickable) {
  4381. eventHolder.unbind("click").click(onClick);
  4382. }
  4383. }
  4384. });
  4385. plot.hooks.processDatapoints.push(function(plot, series, data, datapoints) {
  4386. var options = plot.getOptions();
  4387. if (options.series.pie.show) {
  4388. processDatapoints(plot, series, data, datapoints);
  4389. }
  4390. });
  4391. plot.hooks.drawOverlay.push(function(plot, octx) {
  4392. var options = plot.getOptions();
  4393. if (options.series.pie.show) {
  4394. drawOverlay(plot, octx);
  4395. }
  4396. });
  4397. plot.hooks.draw.push(function(plot, newCtx) {
  4398. var options = plot.getOptions();
  4399. if (options.series.pie.show) {
  4400. draw(plot, newCtx);
  4401. }
  4402. });
  4403. function processDatapoints(plot, series, datapoints) {
  4404. if (!processed) {
  4405. processed = true;
  4406. canvas = plot.getCanvas();
  4407. target = $(canvas).parent();
  4408. options = plot.getOptions();
  4409. plot.setData(combine(plot.getData()));
  4410. }
  4411. }
  4412. function combine(data) {
  4413. var total = 0,
  4414. combined = 0,
  4415. numCombined = 0,
  4416. color = options.series.pie.combine.color,
  4417. newdata = [];
  4418. // Fix up the raw data from Flot, ensuring the data is numeric
  4419. for (var i = 0; i < data.length; ++i) {
  4420. var value = data[i].data;
  4421. // If the data is an array, we'll assume that it's a standard
  4422. // Flot x-y pair, and are concerned only with the second value.
  4423. // Note how we use the original array, rather than creating a
  4424. // new one; this is more efficient and preserves any extra data
  4425. // that the user may have stored in higher indexes.
  4426. if ($.isArray(value) && value.length == 1) {
  4427. value = value[0];
  4428. }
  4429. if ($.isArray(value)) {
  4430. // Equivalent to $.isNumeric() but compatible with jQuery < 1.7
  4431. if (!isNaN(parseFloat(value[1])) && isFinite(value[1])) {
  4432. value[1] = +value[1];
  4433. } else {
  4434. value[1] = 0;
  4435. }
  4436. } else if (!isNaN(parseFloat(value)) && isFinite(value)) {
  4437. value = [1, +value];
  4438. } else {
  4439. value = [1, 0];
  4440. }
  4441. data[i].data = [value];
  4442. }
  4443. // Sum up all the slices, so we can calculate percentages for each
  4444. for (var i = 0; i < data.length; ++i) {
  4445. total += data[i].data[0][1];
  4446. }
  4447. // Count the number of slices with percentages below the combine
  4448. // threshold; if it turns out to be just one, we won't combine.
  4449. for (var i = 0; i < data.length; ++i) {
  4450. var value = data[i].data[0][1];
  4451. if (value / total <= options.series.pie.combine.threshold) {
  4452. combined += value;
  4453. numCombined++;
  4454. if (!color) {
  4455. color = data[i].color;
  4456. }
  4457. }
  4458. }
  4459. for (var i = 0; i < data.length; ++i) {
  4460. var value = data[i].data[0][1];
  4461. if (numCombined < 2 || value / total > options.series.pie.combine.threshold) {
  4462. newdata.push(
  4463. $.extend(data[i], { /* extend to allow keeping all other original data values
  4464. and using them e.g. in labelFormatter. */
  4465. data: [[1, value]],
  4466. color: data[i].color,
  4467. label: data[i].label,
  4468. angle: value * Math.PI * 2 / total,
  4469. percent: value / (total / 100)
  4470. })
  4471. );
  4472. }
  4473. }
  4474. if (numCombined > 1) {
  4475. newdata.push({
  4476. data: [[1, combined]],
  4477. color: color,
  4478. label: options.series.pie.combine.label,
  4479. angle: combined * Math.PI * 2 / total,
  4480. percent: combined / (total / 100)
  4481. });
  4482. }
  4483. return newdata;
  4484. }
  4485. function draw(plot, newCtx) {
  4486. if (!target) {
  4487. return; // if no series were passed
  4488. }
  4489. var canvasWidth = plot.getPlaceholder().width(),
  4490. canvasHeight = plot.getPlaceholder().height(),
  4491. legendWidth = target.children().filter(".legend").children().width() || 0;
  4492. ctx = newCtx;
  4493. // WARNING: HACK! REWRITE THIS CODE AS SOON AS POSSIBLE!
  4494. // When combining smaller slices into an 'other' slice, we need to
  4495. // add a new series. Since Flot gives plugins no way to modify the
  4496. // list of series, the pie plugin uses a hack where the first call
  4497. // to processDatapoints results in a call to setData with the new
  4498. // list of series, then subsequent processDatapoints do nothing.
  4499. // The plugin-global 'processed' flag is used to control this hack;
  4500. // it starts out false, and is set to true after the first call to
  4501. // processDatapoints.
  4502. // Unfortunately this turns future setData calls into no-ops; they
  4503. // call processDatapoints, the flag is true, and nothing happens.
  4504. // To fix this we'll set the flag back to false here in draw, when
  4505. // all series have been processed, so the next sequence of calls to
  4506. // processDatapoints once again starts out with a slice-combine.
  4507. // This is really a hack; in 0.9 we need to give plugins a proper
  4508. // way to modify series before any processing begins.
  4509. processed = false;
  4510. // calculate maximum radius and center point
  4511. maxRadius = Math.min(canvasWidth, canvasHeight / options.series.pie.tilt) / 2;
  4512. centerTop = canvasHeight / 2 + options.series.pie.offset.top;
  4513. centerLeft = canvasWidth / 2;
  4514. if (options.series.pie.offset.left == "auto") {
  4515. if (options.legend.position.match("w")) {
  4516. centerLeft += legendWidth / 2;
  4517. } else {
  4518. centerLeft -= legendWidth / 2;
  4519. }
  4520. if (centerLeft < maxRadius) {
  4521. centerLeft = maxRadius;
  4522. } else if (centerLeft > canvasWidth - maxRadius) {
  4523. centerLeft = canvasWidth - maxRadius;
  4524. }
  4525. } else {
  4526. centerLeft += options.series.pie.offset.left;
  4527. }
  4528. var slices = plot.getData(),
  4529. attempts = 0;
  4530. // Keep shrinking the pie's radius until drawPie returns true,
  4531. // indicating that all the labels fit, or we try too many times.
  4532. do {
  4533. if (attempts > 0) {
  4534. maxRadius *= REDRAW_SHRINK;
  4535. }
  4536. attempts += 1;
  4537. clear();
  4538. if (options.series.pie.tilt <= 0.8) {
  4539. drawShadow();
  4540. }
  4541. } while (!drawPie() && attempts < REDRAW_ATTEMPTS)
  4542. if (attempts >= REDRAW_ATTEMPTS) {
  4543. clear();
  4544. target.prepend("<div class='error'>Could not draw pie with labels contained inside canvas</div>");
  4545. }
  4546. if (plot.setSeries && plot.insertLegend) {
  4547. plot.setSeries(slices);
  4548. plot.insertLegend();
  4549. }
  4550. // we're actually done at this point, just defining internal functions at this point
  4551. function clear() {
  4552. ctx.clearRect(0, 0, canvasWidth, canvasHeight);
  4553. target.children().filter(".pieLabel, .pieLabelBackground").remove();
  4554. }
  4555. function drawShadow() {
  4556. var shadowLeft = options.series.pie.shadow.left;
  4557. var shadowTop = options.series.pie.shadow.top;
  4558. var edge = 10;
  4559. var alpha = options.series.pie.shadow.alpha;
  4560. var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
  4561. if (radius >= canvasWidth / 2 - shadowLeft || radius * options.series.pie.tilt >= canvasHeight / 2 - shadowTop || radius <= edge) {
  4562. return; // shadow would be outside canvas, so don't draw it
  4563. }
  4564. ctx.save();
  4565. ctx.translate(shadowLeft,shadowTop);
  4566. ctx.globalAlpha = alpha;
  4567. ctx.fillStyle = "#000";
  4568. // center and rotate to starting position
  4569. ctx.translate(centerLeft,centerTop);
  4570. ctx.scale(1, options.series.pie.tilt);
  4571. //radius -= edge;
  4572. for (var i = 1; i <= edge; i++) {
  4573. ctx.beginPath();
  4574. ctx.arc(0, 0, radius, 0, Math.PI * 2, false);
  4575. ctx.fill();
  4576. radius -= i;
  4577. }
  4578. ctx.restore();
  4579. }
  4580. function drawPie() {
  4581. var startAngle = Math.PI * options.series.pie.startAngle;
  4582. var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
  4583. // center and rotate to starting position
  4584. ctx.save();
  4585. ctx.translate(centerLeft,centerTop);
  4586. ctx.scale(1, options.series.pie.tilt);
  4587. //ctx.rotate(startAngle); // start at top; -- This doesn't work properly in Opera
  4588. // draw slices
  4589. ctx.save();
  4590. var currentAngle = startAngle;
  4591. for (var i = 0; i < slices.length; ++i) {
  4592. slices[i].startAngle = currentAngle;
  4593. drawSlice(slices[i].angle, slices[i].color, true);
  4594. }
  4595. ctx.restore();
  4596. // draw slice outlines
  4597. if (options.series.pie.stroke.width > 0) {
  4598. ctx.save();
  4599. ctx.lineWidth = options.series.pie.stroke.width;
  4600. currentAngle = startAngle;
  4601. for (var i = 0; i < slices.length; ++i) {
  4602. drawSlice(slices[i].angle, options.series.pie.stroke.color, false);
  4603. }
  4604. ctx.restore();
  4605. }
  4606. // draw donut hole
  4607. drawDonutHole(ctx);
  4608. ctx.restore();
  4609. // Draw the labels, returning true if they fit within the plot
  4610. if (options.series.pie.label.show) {
  4611. return drawLabels();
  4612. } else return true;
  4613. function drawSlice(angle, color, fill) {
  4614. if (angle <= 0 || isNaN(angle)) {
  4615. return;
  4616. }
  4617. if (fill) {
  4618. ctx.fillStyle = color;
  4619. } else {
  4620. ctx.strokeStyle = color;
  4621. ctx.lineJoin = "round";
  4622. }
  4623. ctx.beginPath();
  4624. if (Math.abs(angle - Math.PI * 2) > 0.000000001) {
  4625. ctx.moveTo(0, 0); // Center of the pie
  4626. }
  4627. //ctx.arc(0, 0, radius, 0, angle, false); // This doesn't work properly in Opera
  4628. ctx.arc(0, 0, radius,currentAngle, currentAngle + angle / 2, false);
  4629. ctx.arc(0, 0, radius,currentAngle + angle / 2, currentAngle + angle, false);
  4630. ctx.closePath();
  4631. //ctx.rotate(angle); // This doesn't work properly in Opera
  4632. currentAngle += angle;
  4633. if (fill) {
  4634. ctx.fill();
  4635. } else {
  4636. ctx.stroke();
  4637. }
  4638. }
  4639. function drawLabels() {
  4640. var currentAngle = startAngle;
  4641. var radius = options.series.pie.label.radius > 1 ? options.series.pie.label.radius : maxRadius * options.series.pie.label.radius;
  4642. for (var i = 0; i < slices.length; ++i) {
  4643. if (slices[i].percent >= options.series.pie.label.threshold * 100) {
  4644. if (!drawLabel(slices[i], currentAngle, i)) {
  4645. return false;
  4646. }
  4647. }
  4648. currentAngle += slices[i].angle;
  4649. }
  4650. return true;
  4651. function drawLabel(slice, startAngle, index) {
  4652. if (slice.data[0][1] == 0) {
  4653. return true;
  4654. }
  4655. // format label text
  4656. var lf = options.legend.labelFormatter, text, plf = options.series.pie.label.formatter;
  4657. if (lf) {
  4658. text = lf(slice.label, slice);
  4659. } else {
  4660. text = slice.label;
  4661. }
  4662. if (plf) {
  4663. text = plf(text, slice);
  4664. }
  4665. var halfAngle = ((startAngle + slice.angle) + startAngle) / 2;
  4666. var x = centerLeft + Math.round(Math.cos(halfAngle) * radius);
  4667. var y = centerTop + Math.round(Math.sin(halfAngle) * radius) * options.series.pie.tilt;
  4668. var html = "<span class='pieLabel' id='pieLabel" + index + "' style='position:absolute;top:" + y + "px;left:" + x + "px;'>" + text + "</span>";
  4669. target.append(html);
  4670. var label = target.children("#pieLabel" + index);
  4671. var labelTop = (y - label.height() / 2);
  4672. var labelLeft = (x - label.width() / 2);
  4673. label.css("top", labelTop);
  4674. label.css("left", labelLeft);
  4675. // check to make sure that the label is not outside the canvas
  4676. if (0 - labelTop > 0 || 0 - labelLeft > 0 || canvasHeight - (labelTop + label.height()) < 0 || canvasWidth - (labelLeft + label.width()) < 0) {
  4677. return false;
  4678. }
  4679. if (options.series.pie.label.background.opacity != 0) {
  4680. // put in the transparent background separately to avoid blended labels and label boxes
  4681. var c = options.series.pie.label.background.color;
  4682. if (c == null) {
  4683. c = slice.color;
  4684. }
  4685. var pos = "top:" + labelTop + "px;left:" + labelLeft + "px;";
  4686. $("<div class='pieLabelBackground' style='position:absolute;width:" + label.width() + "px;height:" + label.height() + "px;" + pos + "background-color:" + c + ";'></div>")
  4687. .css("opacity", options.series.pie.label.background.opacity)
  4688. .insertBefore(label);
  4689. }
  4690. return true;
  4691. } // end individual label function
  4692. } // end drawLabels function
  4693. } // end drawPie function
  4694. } // end draw function
  4695. // Placed here because it needs to be accessed from multiple locations
  4696. function drawDonutHole(layer) {
  4697. if (options.series.pie.innerRadius > 0) {
  4698. // subtract the center
  4699. layer.save();
  4700. var innerRadius = options.series.pie.innerRadius > 1 ? options.series.pie.innerRadius : maxRadius * options.series.pie.innerRadius;
  4701. layer.globalCompositeOperation = "destination-out"; // this does not work with excanvas, but it will fall back to using the stroke color
  4702. layer.beginPath();
  4703. layer.fillStyle = options.series.pie.stroke.color;
  4704. layer.arc(0, 0, innerRadius, 0, Math.PI * 2, false);
  4705. layer.fill();
  4706. layer.closePath();
  4707. layer.restore();
  4708. // add inner stroke
  4709. layer.save();
  4710. layer.beginPath();
  4711. layer.strokeStyle = options.series.pie.stroke.color;
  4712. layer.arc(0, 0, innerRadius, 0, Math.PI * 2, false);
  4713. layer.stroke();
  4714. layer.closePath();
  4715. layer.restore();
  4716. // TODO: add extra shadow inside hole (with a mask) if the pie is tilted.
  4717. }
  4718. }
  4719. //-- Additional Interactive related functions --
  4720. function isPointInPoly(poly, pt) {
  4721. for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
  4722. ((poly[i][1] <= pt[1] && pt[1] < poly[j][1]) || (poly[j][1] <= pt[1] && pt[1]< poly[i][1]))
  4723. && (pt[0] < (poly[j][0] - poly[i][0]) * (pt[1] - poly[i][1]) / (poly[j][1] - poly[i][1]) + poly[i][0])
  4724. && (c = !c);
  4725. return c;
  4726. }
  4727. function findNearbySlice(mouseX, mouseY) {
  4728. var slices = plot.getData(),
  4729. options = plot.getOptions(),
  4730. radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius,
  4731. x, y;
  4732. for (var i = 0; i < slices.length; ++i) {
  4733. var s = slices[i];
  4734. if (s.pie.show) {
  4735. ctx.save();
  4736. ctx.beginPath();
  4737. ctx.moveTo(0, 0); // Center of the pie
  4738. //ctx.scale(1, options.series.pie.tilt); // this actually seems to break everything when here.
  4739. ctx.arc(0, 0, radius, s.startAngle, s.startAngle + s.angle / 2, false);
  4740. ctx.arc(0, 0, radius, s.startAngle + s.angle / 2, s.startAngle + s.angle, false);
  4741. ctx.closePath();
  4742. x = mouseX - centerLeft;
  4743. y = mouseY - centerTop;
  4744. if (ctx.isPointInPath) {
  4745. if (ctx.isPointInPath(mouseX - centerLeft, mouseY - centerTop)) {
  4746. ctx.restore();
  4747. return {
  4748. datapoint: [s.percent, s.data],
  4749. dataIndex: 0,
  4750. series: s,
  4751. seriesIndex: i
  4752. };
  4753. }
  4754. } else {
  4755. // excanvas for IE doesn;t support isPointInPath, this is a workaround.
  4756. var p1X = radius * Math.cos(s.startAngle),
  4757. p1Y = radius * Math.sin(s.startAngle),
  4758. p2X = radius * Math.cos(s.startAngle + s.angle / 4),
  4759. p2Y = radius * Math.sin(s.startAngle + s.angle / 4),
  4760. p3X = radius * Math.cos(s.startAngle + s.angle / 2),
  4761. p3Y = radius * Math.sin(s.startAngle + s.angle / 2),
  4762. p4X = radius * Math.cos(s.startAngle + s.angle / 1.5),
  4763. p4Y = radius * Math.sin(s.startAngle + s.angle / 1.5),
  4764. p5X = radius * Math.cos(s.startAngle + s.angle),
  4765. p5Y = radius * Math.sin(s.startAngle + s.angle),
  4766. arrPoly = [[0, 0], [p1X, p1Y], [p2X, p2Y], [p3X, p3Y], [p4X, p4Y], [p5X, p5Y]],
  4767. arrPoint = [x, y];
  4768. // TODO: perhaps do some mathmatical trickery here with the Y-coordinate to compensate for pie tilt?
  4769. if (isPointInPoly(arrPoly, arrPoint)) {
  4770. ctx.restore();
  4771. return {
  4772. datapoint: [s.percent, s.data],
  4773. dataIndex: 0,
  4774. series: s,
  4775. seriesIndex: i
  4776. };
  4777. }
  4778. }
  4779. ctx.restore();
  4780. }
  4781. }
  4782. return null;
  4783. }
  4784. function onMouseMove(e) {
  4785. triggerClickHoverEvent("plothover", e);
  4786. }
  4787. function onClick(e) {
  4788. triggerClickHoverEvent("plotclick", e);
  4789. }
  4790. // trigger click or hover event (they send the same parameters so we share their code)
  4791. function triggerClickHoverEvent(eventname, e) {
  4792. var offset = plot.offset();
  4793. var canvasX = parseInt(e.pageX - offset.left);
  4794. var canvasY = parseInt(e.pageY - offset.top);
  4795. var item = findNearbySlice(canvasX, canvasY);
  4796. if (options.grid.autoHighlight) {
  4797. // clear auto-highlights
  4798. for (var i = 0; i < highlights.length; ++i) {
  4799. var h = highlights[i];
  4800. if (h.auto == eventname && !(item && h.series == item.series)) {
  4801. unhighlight(h.series);
  4802. }
  4803. }
  4804. }
  4805. // highlight the slice
  4806. if (item) {
  4807. highlight(item.series, eventname);
  4808. }
  4809. // trigger any hover bind events
  4810. var pos = { pageX: e.pageX, pageY: e.pageY };
  4811. target.trigger(eventname, [pos, item]);
  4812. }
  4813. function highlight(s, auto) {
  4814. //if (typeof s == "number") {
  4815. // s = series[s];
  4816. //}
  4817. var i = indexOfHighlight(s);
  4818. if (i == -1) {
  4819. highlights.push({ series: s, auto: auto });
  4820. plot.triggerRedrawOverlay();
  4821. } else if (!auto) {
  4822. highlights[i].auto = false;
  4823. }
  4824. }
  4825. function unhighlight(s) {
  4826. if (s == null) {
  4827. highlights = [];
  4828. plot.triggerRedrawOverlay();
  4829. }
  4830. //if (typeof s == "number") {
  4831. // s = series[s];
  4832. //}
  4833. var i = indexOfHighlight(s);
  4834. if (i != -1) {
  4835. highlights.splice(i, 1);
  4836. plot.triggerRedrawOverlay();
  4837. }
  4838. }
  4839. function indexOfHighlight(s) {
  4840. for (var i = 0; i < highlights.length; ++i) {
  4841. var h = highlights[i];
  4842. if (h.series == s)
  4843. return i;
  4844. }
  4845. return -1;
  4846. }
  4847. function drawOverlay(plot, octx) {
  4848. var options = plot.getOptions();
  4849. var radius = options.series.pie.radius > 1 ? options.series.pie.radius : maxRadius * options.series.pie.radius;
  4850. octx.save();
  4851. octx.translate(centerLeft, centerTop);
  4852. octx.scale(1, options.series.pie.tilt);
  4853. for (var i = 0; i < highlights.length; ++i) {
  4854. drawHighlight(highlights[i].series);
  4855. }
  4856. drawDonutHole(octx);
  4857. octx.restore();
  4858. function drawHighlight(series) {
  4859. if (series.angle <= 0 || isNaN(series.angle)) {
  4860. return;
  4861. }
  4862. //octx.fillStyle = parseColor(options.series.pie.highlight.color).scale(null, null, null, options.series.pie.highlight.opacity).toString();
  4863. octx.fillStyle = "rgba(255, 255, 255, " + options.series.pie.highlight.opacity + ")"; // this is temporary until we have access to parseColor
  4864. octx.beginPath();
  4865. if (Math.abs(series.angle - Math.PI * 2) > 0.000000001) {
  4866. octx.moveTo(0, 0); // Center of the pie
  4867. }
  4868. octx.arc(0, 0, radius, series.startAngle, series.startAngle + series.angle / 2, false);
  4869. octx.arc(0, 0, radius, series.startAngle + series.angle / 2, series.startAngle + series.angle, false);
  4870. octx.closePath();
  4871. octx.fill();
  4872. }
  4873. }
  4874. } // end init (plugin body)
  4875. // define pie specific options and their default values
  4876. var options = {
  4877. series: {
  4878. pie: {
  4879. show: false,
  4880. radius: "auto", // actual radius of the visible pie (based on full calculated radius if <=1, or hard pixel value)
  4881. innerRadius: 0, /* for donut */
  4882. startAngle: 3/2,
  4883. tilt: 1,
  4884. shadow: {
  4885. left: 5, // shadow left offset
  4886. top: 15, // shadow top offset
  4887. alpha: 0.02 // shadow alpha
  4888. },
  4889. offset: {
  4890. top: 0,
  4891. left: "auto"
  4892. },
  4893. stroke: {
  4894. color: "#fff",
  4895. width: 1
  4896. },
  4897. label: {
  4898. show: "auto",
  4899. formatter: function(label, slice) {
  4900. return "<div style='font-size:x-small;text-align:center;padding:2px;color:" + slice.color + ";'>" + label + "<br/>" + Math.round(slice.percent) + "%</div>";
  4901. }, // formatter function
  4902. radius: 1, // radius at which to place the labels (based on full calculated radius if <=1, or hard pixel value)
  4903. background: {
  4904. color: null,
  4905. opacity: 0
  4906. },
  4907. threshold: 0 // percentage at which to hide the label (i.e. the slice is too narrow)
  4908. },
  4909. combine: {
  4910. threshold: -1, // percentage at which to combine little slices into one larger slice
  4911. color: null, // color to give the new slice (auto-generated if null)
  4912. label: "Other" // label to give the new slice
  4913. },
  4914. highlight: {
  4915. //color: "#fff", // will add this functionality once parseColor is available
  4916. opacity: 0.5
  4917. }
  4918. }
  4919. }
  4920. };
  4921. $.plot.plugins.push({
  4922. init: init,
  4923. options: options,
  4924. name: "pie",
  4925. version: "1.1"
  4926. });
  4927. })(jQuery);
  4928. /* Flot plugin for automatically redrawing plots as the placeholder resizes.
  4929. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  4930. Licensed under the MIT license.
  4931. It works by listening for changes on the placeholder div (through the jQuery
  4932. resize event plugin) - if the size changes, it will redraw the plot.
  4933. There are no options. If you need to disable the plugin for some plots, you
  4934. can just fix the size of their placeholders.
  4935. */
  4936. /* Inline dependency:
  4937. * jQuery resize event - v1.1 - 3/14/2010
  4938. * http://benalman.com/projects/jquery-resize-plugin/
  4939. *
  4940. * Copyright (c) 2010 "Cowboy" Ben Alman
  4941. * Dual licensed under the MIT and GPL licenses.
  4942. * http://benalman.com/about/license/
  4943. */
  4944. (function($,e,t){"$:nomunge";var i=[],n=$.resize=$.extend($.resize,{}),a,r=false,s="setTimeout",u="resize",m=u+"-special-event",o="pendingDelay",l="activeDelay",f="throttleWindow";n[o]=200;n[l]=20;n[f]=true;$.event.special[u]={setup:function(){if(!n[f]&&this[s]){return false}var e=$(this);i.push(this);e.data(m,{w:e.width(),h:e.height()});if(i.length===1){a=t;h()}},teardown:function(){if(!n[f]&&this[s]){return false}var e=$(this);for(var t=i.length-1;t>=0;t--){if(i[t]==this){i.splice(t,1);break}}e.removeData(m);if(!i.length){if(r){cancelAnimationFrame(a)}else{clearTimeout(a)}a=null}},add:function(e){if(!n[f]&&this[s]){return false}var i;function a(e,n,a){var r=$(this),s=r.data(m)||{};s.w=n!==t?n:r.width();s.h=a!==t?a:r.height();i.apply(this,arguments)}if($.isFunction(e)){i=e;return a}else{i=e.handler;e.handler=a}}};function h(t){if(r===true){r=t||1}for(var s=i.length-1;s>=0;s--){var l=$(i[s]);if(l[0]==e||l.is(":visible")){var f=l.width(),c=l.height(),d=l.data(m);if(d&&(f!==d.w||c!==d.h)){l.trigger(u,[d.w=f,d.h=c]);r=t||true}}else{d=l.data(m);d.w=0;d.h=0}}if(a!==null){if(r&&(t==null||t-r<1e3)){a=e.requestAnimationFrame(h)}else{a=setTimeout(h,n[o]);r=false}}}if(!e.requestAnimationFrame){e.requestAnimationFrame=function(){return e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(t,i){return e.setTimeout(function(){t((new Date).getTime())},n[l])}}()}if(!e.cancelAnimationFrame){e.cancelAnimationFrame=function(){return e.webkitCancelRequestAnimationFrame||e.mozCancelRequestAnimationFrame||e.oCancelRequestAnimationFrame||e.msCancelRequestAnimationFrame||clearTimeout}()}})(jQuery,this);
  4945. (function ($) {
  4946. var options = { }; // no options
  4947. function init(plot) {
  4948. function onResize() {
  4949. var placeholder = plot.getPlaceholder();
  4950. // somebody might have hidden us and we can't plot
  4951. // when we don't have the dimensions
  4952. if (placeholder.width() == 0 || placeholder.height() == 0)
  4953. return;
  4954. plot.resize();
  4955. plot.setupGrid();
  4956. plot.draw();
  4957. }
  4958. function bindEvents(plot, eventHolder) {
  4959. plot.getPlaceholder().resize(onResize);
  4960. }
  4961. function shutdown(plot, eventHolder) {
  4962. plot.getPlaceholder().unbind("resize", onResize);
  4963. }
  4964. plot.hooks.bindEvents.push(bindEvents);
  4965. plot.hooks.shutdown.push(shutdown);
  4966. }
  4967. $.plot.plugins.push({
  4968. init: init,
  4969. options: options,
  4970. name: 'resize',
  4971. version: '1.0'
  4972. });
  4973. })(jQuery);
  4974. /* Flot plugin for selecting regions of a plot.
  4975. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  4976. Licensed under the MIT license.
  4977. The plugin supports these options:
  4978. selection: {
  4979. mode: null or "x" or "y" or "xy",
  4980. color: color,
  4981. shape: "round" or "miter" or "bevel",
  4982. minSize: number of pixels
  4983. }
  4984. Selection support is enabled by setting the mode to one of "x", "y" or "xy".
  4985. In "x" mode, the user will only be able to specify the x range, similarly for
  4986. "y" mode. For "xy", the selection becomes a rectangle where both ranges can be
  4987. specified. "color" is color of the selection (if you need to change the color
  4988. later on, you can get to it with plot.getOptions().selection.color). "shape"
  4989. is the shape of the corners of the selection.
  4990. "minSize" is the minimum size a selection can be in pixels. This value can
  4991. be customized to determine the smallest size a selection can be and still
  4992. have the selection rectangle be displayed. When customizing this value, the
  4993. fact that it refers to pixels, not axis units must be taken into account.
  4994. Thus, for example, if there is a bar graph in time mode with BarWidth set to 1
  4995. minute, setting "minSize" to 1 will not make the minimum selection size 1
  4996. minute, but rather 1 pixel. Note also that setting "minSize" to 0 will prevent
  4997. "plotunselected" events from being fired when the user clicks the mouse without
  4998. dragging.
  4999. When selection support is enabled, a "plotselected" event will be emitted on
  5000. the DOM element you passed into the plot function. The event handler gets a
  5001. parameter with the ranges selected on the axes, like this:
  5002. placeholder.bind( "plotselected", function( event, ranges ) {
  5003. alert("You selected " + ranges.xaxis.from + " to " + ranges.xaxis.to)
  5004. // similar for yaxis - with multiple axes, the extra ones are in
  5005. // x2axis, x3axis, ...
  5006. });
  5007. The "plotselected" event is only fired when the user has finished making the
  5008. selection. A "plotselecting" event is fired during the process with the same
  5009. parameters as the "plotselected" event, in case you want to know what's
  5010. happening while it's happening,
  5011. A "plotunselected" event with no arguments is emitted when the user clicks the
  5012. mouse to remove the selection. As stated above, setting "minSize" to 0 will
  5013. destroy this behavior.
  5014. The plugin allso adds the following methods to the plot object:
  5015. - setSelection( ranges, preventEvent )
  5016. Set the selection rectangle. The passed in ranges is on the same form as
  5017. returned in the "plotselected" event. If the selection mode is "x", you
  5018. should put in either an xaxis range, if the mode is "y" you need to put in
  5019. an yaxis range and both xaxis and yaxis if the selection mode is "xy", like
  5020. this:
  5021. setSelection({ xaxis: { from: 0, to: 10 }, yaxis: { from: 40, to: 60 } });
  5022. setSelection will trigger the "plotselected" event when called. If you don't
  5023. want that to happen, e.g. if you're inside a "plotselected" handler, pass
  5024. true as the second parameter. If you are using multiple axes, you can
  5025. specify the ranges on any of those, e.g. as x2axis/x3axis/... instead of
  5026. xaxis, the plugin picks the first one it sees.
  5027. - clearSelection( preventEvent )
  5028. Clear the selection rectangle. Pass in true to avoid getting a
  5029. "plotunselected" event.
  5030. - getSelection()
  5031. Returns the current selection in the same format as the "plotselected"
  5032. event. If there's currently no selection, the function returns null.
  5033. */
  5034. (function ($) {
  5035. function init(plot) {
  5036. var selection = {
  5037. first: { x: -1, y: -1}, second: { x: -1, y: -1},
  5038. show: false,
  5039. active: false
  5040. };
  5041. // FIXME: The drag handling implemented here should be
  5042. // abstracted out, there's some similar code from a library in
  5043. // the navigation plugin, this should be massaged a bit to fit
  5044. // the Flot cases here better and reused. Doing this would
  5045. // make this plugin much slimmer.
  5046. var savedhandlers = {};
  5047. var mouseUpHandler = null;
  5048. function onMouseMove(e) {
  5049. if (selection.active) {
  5050. updateSelection(e);
  5051. plot.getPlaceholder().trigger("plotselecting", [ getSelection() ]);
  5052. }
  5053. }
  5054. function onMouseDown(e) {
  5055. if (e.which != 1) // only accept left-click
  5056. return;
  5057. // cancel out any text selections
  5058. document.body.focus();
  5059. // prevent text selection and drag in old-school browsers
  5060. if (document.onselectstart !== undefined && savedhandlers.onselectstart == null) {
  5061. savedhandlers.onselectstart = document.onselectstart;
  5062. document.onselectstart = function () { return false; };
  5063. }
  5064. if (document.ondrag !== undefined && savedhandlers.ondrag == null) {
  5065. savedhandlers.ondrag = document.ondrag;
  5066. document.ondrag = function () { return false; };
  5067. }
  5068. setSelectionPos(selection.first, e);
  5069. selection.active = true;
  5070. // this is a bit silly, but we have to use a closure to be
  5071. // able to whack the same handler again
  5072. mouseUpHandler = function (e) { onMouseUp(e); };
  5073. $(document).one("mouseup", mouseUpHandler);
  5074. }
  5075. function onMouseUp(e) {
  5076. mouseUpHandler = null;
  5077. // revert drag stuff for old-school browsers
  5078. if (document.onselectstart !== undefined)
  5079. document.onselectstart = savedhandlers.onselectstart;
  5080. if (document.ondrag !== undefined)
  5081. document.ondrag = savedhandlers.ondrag;
  5082. // no more dragging
  5083. selection.active = false;
  5084. updateSelection(e);
  5085. if (selectionIsSane())
  5086. triggerSelectedEvent();
  5087. else {
  5088. // this counts as a clear
  5089. plot.getPlaceholder().trigger("plotunselected", [ ]);
  5090. plot.getPlaceholder().trigger("plotselecting", [ null ]);
  5091. }
  5092. return false;
  5093. }
  5094. function getSelection() {
  5095. if (!selectionIsSane())
  5096. return null;
  5097. if (!selection.show) return null;
  5098. var r = {}, c1 = selection.first, c2 = selection.second;
  5099. $.each(plot.getAxes(), function (name, axis) {
  5100. if (axis.used) {
  5101. var p1 = axis.c2p(c1[axis.direction]), p2 = axis.c2p(c2[axis.direction]);
  5102. r[name] = { from: Math.min(p1, p2), to: Math.max(p1, p2) };
  5103. }
  5104. });
  5105. return r;
  5106. }
  5107. function triggerSelectedEvent() {
  5108. var r = getSelection();
  5109. plot.getPlaceholder().trigger("plotselected", [ r ]);
  5110. // backwards-compat stuff, to be removed in future
  5111. if (r.xaxis && r.yaxis)
  5112. plot.getPlaceholder().trigger("selected", [ { x1: r.xaxis.from, y1: r.yaxis.from, x2: r.xaxis.to, y2: r.yaxis.to } ]);
  5113. }
  5114. function clamp(min, value, max) {
  5115. return value < min ? min: (value > max ? max: value);
  5116. }
  5117. function setSelectionPos(pos, e) {
  5118. var o = plot.getOptions();
  5119. var offset = plot.getPlaceholder().offset();
  5120. var plotOffset = plot.getPlotOffset();
  5121. pos.x = clamp(0, e.pageX - offset.left - plotOffset.left, plot.width());
  5122. pos.y = clamp(0, e.pageY - offset.top - plotOffset.top, plot.height());
  5123. if (o.selection.mode == "y")
  5124. pos.x = pos == selection.first ? 0 : plot.width();
  5125. if (o.selection.mode == "x")
  5126. pos.y = pos == selection.first ? 0 : plot.height();
  5127. }
  5128. function updateSelection(pos) {
  5129. if (pos.pageX == null)
  5130. return;
  5131. setSelectionPos(selection.second, pos);
  5132. if (selectionIsSane()) {
  5133. selection.show = true;
  5134. plot.triggerRedrawOverlay();
  5135. }
  5136. else
  5137. clearSelection(true);
  5138. }
  5139. function clearSelection(preventEvent) {
  5140. if (selection.show) {
  5141. selection.show = false;
  5142. plot.triggerRedrawOverlay();
  5143. if (!preventEvent)
  5144. plot.getPlaceholder().trigger("plotunselected", [ ]);
  5145. }
  5146. }
  5147. // function taken from markings support in Flot
  5148. function extractRange(ranges, coord) {
  5149. var axis, from, to, key, axes = plot.getAxes();
  5150. for (var k in axes) {
  5151. axis = axes[k];
  5152. if (axis.direction == coord) {
  5153. key = coord + axis.n + "axis";
  5154. if (!ranges[key] && axis.n == 1)
  5155. key = coord + "axis"; // support x1axis as xaxis
  5156. if (ranges[key]) {
  5157. from = ranges[key].from;
  5158. to = ranges[key].to;
  5159. break;
  5160. }
  5161. }
  5162. }
  5163. // backwards-compat stuff - to be removed in future
  5164. if (!ranges[key]) {
  5165. axis = coord == "x" ? plot.getXAxes()[0] : plot.getYAxes()[0];
  5166. from = ranges[coord + "1"];
  5167. to = ranges[coord + "2"];
  5168. }
  5169. // auto-reverse as an added bonus
  5170. if (from != null && to != null && from > to) {
  5171. var tmp = from;
  5172. from = to;
  5173. to = tmp;
  5174. }
  5175. return { from: from, to: to, axis: axis };
  5176. }
  5177. function setSelection(ranges, preventEvent) {
  5178. var axis, range, o = plot.getOptions();
  5179. if (o.selection.mode == "y") {
  5180. selection.first.x = 0;
  5181. selection.second.x = plot.width();
  5182. }
  5183. else {
  5184. range = extractRange(ranges, "x");
  5185. selection.first.x = range.axis.p2c(range.from);
  5186. selection.second.x = range.axis.p2c(range.to);
  5187. }
  5188. if (o.selection.mode == "x") {
  5189. selection.first.y = 0;
  5190. selection.second.y = plot.height();
  5191. }
  5192. else {
  5193. range = extractRange(ranges, "y");
  5194. selection.first.y = range.axis.p2c(range.from);
  5195. selection.second.y = range.axis.p2c(range.to);
  5196. }
  5197. selection.show = true;
  5198. plot.triggerRedrawOverlay();
  5199. if (!preventEvent && selectionIsSane())
  5200. triggerSelectedEvent();
  5201. }
  5202. function selectionIsSane() {
  5203. var minSize = plot.getOptions().selection.minSize;
  5204. return Math.abs(selection.second.x - selection.first.x) >= minSize &&
  5205. Math.abs(selection.second.y - selection.first.y) >= minSize;
  5206. }
  5207. plot.clearSelection = clearSelection;
  5208. plot.setSelection = setSelection;
  5209. plot.getSelection = getSelection;
  5210. plot.hooks.bindEvents.push(function(plot, eventHolder) {
  5211. var o = plot.getOptions();
  5212. if (o.selection.mode != null) {
  5213. eventHolder.mousemove(onMouseMove);
  5214. eventHolder.mousedown(onMouseDown);
  5215. }
  5216. });
  5217. plot.hooks.drawOverlay.push(function (plot, ctx) {
  5218. // draw selection
  5219. if (selection.show && selectionIsSane()) {
  5220. var plotOffset = plot.getPlotOffset();
  5221. var o = plot.getOptions();
  5222. ctx.save();
  5223. ctx.translate(plotOffset.left, plotOffset.top);
  5224. var c = $.color.parse(o.selection.color);
  5225. ctx.strokeStyle = c.scale('a', 0.8).toString();
  5226. ctx.lineWidth = 1;
  5227. ctx.lineJoin = o.selection.shape;
  5228. ctx.fillStyle = c.scale('a', 0.4).toString();
  5229. var x = Math.min(selection.first.x, selection.second.x) + 0.5,
  5230. y = Math.min(selection.first.y, selection.second.y) + 0.5,
  5231. w = Math.abs(selection.second.x - selection.first.x) - 1,
  5232. h = Math.abs(selection.second.y - selection.first.y) - 1;
  5233. ctx.fillRect(x, y, w, h);
  5234. ctx.strokeRect(x, y, w, h);
  5235. ctx.restore();
  5236. }
  5237. });
  5238. plot.hooks.shutdown.push(function (plot, eventHolder) {
  5239. eventHolder.unbind("mousemove", onMouseMove);
  5240. eventHolder.unbind("mousedown", onMouseDown);
  5241. if (mouseUpHandler)
  5242. $(document).unbind("mouseup", mouseUpHandler);
  5243. });
  5244. }
  5245. $.plot.plugins.push({
  5246. init: init,
  5247. options: {
  5248. selection: {
  5249. mode: null, // one of null, "x", "y" or "xy"
  5250. color: "#e8cfac",
  5251. shape: "round", // one of "round", "miter", or "bevel"
  5252. minSize: 5 // minimum number of pixels
  5253. }
  5254. },
  5255. name: 'selection',
  5256. version: '1.1'
  5257. });
  5258. })(jQuery);
  5259. /* Flot plugin for stacking data sets rather than overlyaing them.
  5260. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  5261. Licensed under the MIT license.
  5262. The plugin assumes the data is sorted on x (or y if stacking horizontally).
  5263. For line charts, it is assumed that if a line has an undefined gap (from a
  5264. null point), then the line above it should have the same gap - insert zeros
  5265. instead of "null" if you want another behaviour. This also holds for the start
  5266. and end of the chart. Note that stacking a mix of positive and negative values
  5267. in most instances doesn't make sense (so it looks weird).
  5268. Two or more series are stacked when their "stack" attribute is set to the same
  5269. key (which can be any number or string or just "true"). To specify the default
  5270. stack, you can set the stack option like this:
  5271. series: {
  5272. stack: null/false, true, or a key (number/string)
  5273. }
  5274. You can also specify it for a single series, like this:
  5275. $.plot( $("#placeholder"), [{
  5276. data: [ ... ],
  5277. stack: true
  5278. }])
  5279. The stacking order is determined by the order of the data series in the array
  5280. (later series end up on top of the previous).
  5281. Internally, the plugin modifies the datapoints in each series, adding an
  5282. offset to the y value. For line series, extra data points are inserted through
  5283. interpolation. If there's a second y value, it's also adjusted (e.g for bar
  5284. charts or filled areas).
  5285. */
  5286. (function ($) {
  5287. var options = {
  5288. series: { stack: null } // or number/string
  5289. };
  5290. function init(plot) {
  5291. function findMatchingSeries(s, allseries) {
  5292. var res = null;
  5293. for (var i = 0; i < allseries.length; ++i) {
  5294. if (s == allseries[i])
  5295. break;
  5296. if (allseries[i].stack == s.stack)
  5297. res = allseries[i];
  5298. }
  5299. return res;
  5300. }
  5301. function stackData(plot, s, datapoints) {
  5302. if (s.stack == null || s.stack === false)
  5303. return;
  5304. var other = findMatchingSeries(s, plot.getData());
  5305. if (!other)
  5306. return;
  5307. var ps = datapoints.pointsize,
  5308. points = datapoints.points,
  5309. otherps = other.datapoints.pointsize,
  5310. otherpoints = other.datapoints.points,
  5311. newpoints = [],
  5312. px, py, intery, qx, qy, bottom,
  5313. withlines = s.lines.show,
  5314. horizontal = s.bars.horizontal,
  5315. withbottom = ps > 2 && (horizontal ? datapoints.format[2].x : datapoints.format[2].y),
  5316. withsteps = withlines && s.lines.steps,
  5317. fromgap = true,
  5318. keyOffset = horizontal ? 1 : 0,
  5319. accumulateOffset = horizontal ? 0 : 1,
  5320. i = 0, j = 0, l, m;
  5321. while (true) {
  5322. if (i >= points.length)
  5323. break;
  5324. l = newpoints.length;
  5325. if (points[i] == null) {
  5326. // copy gaps
  5327. for (m = 0; m < ps; ++m)
  5328. newpoints.push(points[i + m]);
  5329. i += ps;
  5330. }
  5331. else if (j >= otherpoints.length) {
  5332. // for lines, we can't use the rest of the points
  5333. if (!withlines) {
  5334. for (m = 0; m < ps; ++m)
  5335. newpoints.push(points[i + m]);
  5336. }
  5337. i += ps;
  5338. }
  5339. else if (otherpoints[j] == null) {
  5340. // oops, got a gap
  5341. for (m = 0; m < ps; ++m)
  5342. newpoints.push(null);
  5343. fromgap = true;
  5344. j += otherps;
  5345. }
  5346. else {
  5347. // cases where we actually got two points
  5348. px = points[i + keyOffset];
  5349. py = points[i + accumulateOffset];
  5350. qx = otherpoints[j + keyOffset];
  5351. qy = otherpoints[j + accumulateOffset];
  5352. bottom = 0;
  5353. if (px == qx) {
  5354. for (m = 0; m < ps; ++m)
  5355. newpoints.push(points[i + m]);
  5356. newpoints[l + accumulateOffset] += qy;
  5357. bottom = qy;
  5358. i += ps;
  5359. j += otherps;
  5360. }
  5361. else if (px > qx) {
  5362. // we got past point below, might need to
  5363. // insert interpolated extra point
  5364. if (withlines && i > 0 && points[i - ps] != null) {
  5365. intery = py + (points[i - ps + accumulateOffset] - py) * (qx - px) / (points[i - ps + keyOffset] - px);
  5366. newpoints.push(qx);
  5367. newpoints.push(intery + qy);
  5368. for (m = 2; m < ps; ++m)
  5369. newpoints.push(points[i + m]);
  5370. bottom = qy;
  5371. }
  5372. j += otherps;
  5373. }
  5374. else { // px < qx
  5375. if (fromgap && withlines) {
  5376. // if we come from a gap, we just skip this point
  5377. i += ps;
  5378. continue;
  5379. }
  5380. for (m = 0; m < ps; ++m)
  5381. newpoints.push(points[i + m]);
  5382. // we might be able to interpolate a point below,
  5383. // this can give us a better y
  5384. if (withlines && j > 0 && otherpoints[j - otherps] != null)
  5385. bottom = qy + (otherpoints[j - otherps + accumulateOffset] - qy) * (px - qx) / (otherpoints[j - otherps + keyOffset] - qx);
  5386. newpoints[l + accumulateOffset] += bottom;
  5387. i += ps;
  5388. }
  5389. fromgap = false;
  5390. if (l != newpoints.length && withbottom)
  5391. newpoints[l + 2] += bottom;
  5392. }
  5393. // maintain the line steps invariant
  5394. if (withsteps && l != newpoints.length && l > 0
  5395. && newpoints[l] != null
  5396. && newpoints[l] != newpoints[l - ps]
  5397. && newpoints[l + 1] != newpoints[l - ps + 1]) {
  5398. for (m = 0; m < ps; ++m)
  5399. newpoints[l + ps + m] = newpoints[l + m];
  5400. newpoints[l + 1] = newpoints[l - ps + 1];
  5401. }
  5402. }
  5403. datapoints.points = newpoints;
  5404. }
  5405. plot.hooks.processDatapoints.push(stackData);
  5406. }
  5407. $.plot.plugins.push({
  5408. init: init,
  5409. options: options,
  5410. name: 'stack',
  5411. version: '1.2'
  5412. });
  5413. })(jQuery);
  5414. /**
  5415. * Flot plugin that provides spline interpolation for line graphs
  5416. * author: Alex Bardas < alex.bardas@gmail.com >
  5417. * modified by: Avi Kohn https://github.com/AMKohn
  5418. * based on the spline interpolation described at:
  5419. * http://scaledinnovation.com/analytics/splines/aboutSplines.html
  5420. *
  5421. * Example usage: (add in plot options series object)
  5422. * for linespline:
  5423. * series: {
  5424. * ...
  5425. * lines: {
  5426. * show: false
  5427. * },
  5428. * splines: {
  5429. * show: true,
  5430. * tension: x, (float between 0 and 1, defaults to 0.5),
  5431. * lineWidth: y (number, defaults to 2),
  5432. * fill: z (float between 0 .. 1 or false, as in flot documentation)
  5433. * },
  5434. * ...
  5435. * }
  5436. * areaspline:
  5437. * series: {
  5438. * ...
  5439. * lines: {
  5440. * show: true,
  5441. * lineWidth: 0, (line drawing will not execute)
  5442. * fill: x, (float between 0 .. 1, as in flot documentation)
  5443. * ...
  5444. * },
  5445. * splines: {
  5446. * show: true,
  5447. * tension: 0.5 (float between 0 and 1)
  5448. * },
  5449. * ...
  5450. * }
  5451. *
  5452. */
  5453. (function($) {
  5454. 'use strict'
  5455. /**
  5456. * @param {Number} x0, y0, x1, y1: coordinates of the end (knot) points of the segment
  5457. * @param {Number} x2, y2: the next knot (not connected, but needed to calculate p2)
  5458. * @param {Number} tension: control how far the control points spread
  5459. * @return {Array}: p1 -> control point, from x1 back toward x0
  5460. * p2 -> the next control point, returned to become the next segment's p1
  5461. *
  5462. * @api private
  5463. */
  5464. function getControlPoints(x0, y0, x1, y1, x2, y2, tension) {
  5465. var pow = Math.pow,
  5466. sqrt = Math.sqrt,
  5467. d01, d12, fa, fb, p1x, p1y, p2x, p2y;
  5468. // Scaling factors: distances from this knot to the previous and following knots.
  5469. d01 = sqrt(pow(x1 - x0, 2) + pow(y1 - y0, 2));
  5470. d12 = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
  5471. fa = tension * d01 / (d01 + d12);
  5472. fb = tension - fa;
  5473. p1x = x1 + fa * (x0 - x2);
  5474. p1y = y1 + fa * (y0 - y2);
  5475. p2x = x1 - fb * (x0 - x2);
  5476. p2y = y1 - fb * (y0 - y2);
  5477. return [p1x, p1y, p2x, p2y];
  5478. }
  5479. var line = [];
  5480. function drawLine(points, ctx, height, fill, seriesColor) {
  5481. var c = $.color.parse(seriesColor);
  5482. c.a = typeof fill == "number" ? fill : .3;
  5483. c.normalize();
  5484. c = c.toString();
  5485. ctx.beginPath();
  5486. ctx.moveTo(points[0][0], points[0][1]);
  5487. var plength = points.length;
  5488. for (var i = 0; i < plength; i++) {
  5489. ctx[points[i][3]].apply(ctx, points[i][2]);
  5490. }
  5491. ctx.stroke();
  5492. ctx.lineWidth = 0;
  5493. ctx.lineTo(points[plength - 1][0], height);
  5494. ctx.lineTo(points[0][0], height);
  5495. ctx.closePath();
  5496. if (fill !== false) {
  5497. ctx.fillStyle = c;
  5498. ctx.fill();
  5499. }
  5500. }
  5501. /**
  5502. * @param {Object} ctx: canvas context
  5503. * @param {String} type: accepted strings: 'bezier' or 'quadratic' (defaults to quadratic)
  5504. * @param {Array} points: 2 points for which to draw the interpolation
  5505. * @param {Array} cpoints: control points for those segment points
  5506. *
  5507. * @api private
  5508. */
  5509. function queue(ctx, type, points, cpoints) {
  5510. if (type === void 0 || (type !== 'bezier' && type !== 'quadratic')) {
  5511. type = 'quadratic';
  5512. }
  5513. type = type + 'CurveTo';
  5514. if (line.length == 0) line.push([points[0], points[1], cpoints.concat(points.slice(2)), type]);
  5515. else if (type == "quadraticCurveTo" && points.length == 2) {
  5516. cpoints = cpoints.slice(0, 2).concat(points);
  5517. line.push([points[0], points[1], cpoints, type]);
  5518. }
  5519. else line.push([points[2], points[3], cpoints.concat(points.slice(2)), type]);
  5520. }
  5521. /**
  5522. * @param {Object} plot
  5523. * @param {Object} ctx: canvas context
  5524. * @param {Object} series
  5525. *
  5526. * @api private
  5527. */
  5528. function drawSpline(plot, ctx, series) {
  5529. // Not interested if spline is not requested
  5530. if (series.splines.show !== true) {
  5531. return;
  5532. }
  5533. var cp = [],
  5534. // array of control points
  5535. tension = series.splines.tension || 0.5,
  5536. idx, x, y, points = series.datapoints.points,
  5537. ps = series.datapoints.pointsize,
  5538. plotOffset = plot.getPlotOffset(),
  5539. len = points.length,
  5540. pts = [];
  5541. line = [];
  5542. // Cannot display a linespline/areaspline if there are less than 3 points
  5543. if (len / ps < 4) {
  5544. $.extend(series.lines, series.splines);
  5545. return;
  5546. }
  5547. for (idx = 0; idx < len; idx += ps) {
  5548. x = points[idx];
  5549. y = points[idx + 1];
  5550. if (x == null || x < series.xaxis.min || x > series.xaxis.max || y < series.yaxis.min || y > series.yaxis.max) {
  5551. continue;
  5552. }
  5553. pts.push(series.xaxis.p2c(x) + plotOffset.left, series.yaxis.p2c(y) + plotOffset.top);
  5554. }
  5555. len = pts.length;
  5556. // Draw an open curve, not connected at the ends
  5557. for (idx = 0; idx < len - 2; idx += 2) {
  5558. cp = cp.concat(getControlPoints.apply(this, pts.slice(idx, idx + 6).concat([tension])));
  5559. }
  5560. ctx.save();
  5561. ctx.strokeStyle = series.color;
  5562. ctx.lineWidth = series.splines.lineWidth;
  5563. queue(ctx, 'quadratic', pts.slice(0, 4), cp.slice(0, 2));
  5564. for (idx = 2; idx < len - 3; idx += 2) {
  5565. queue(ctx, 'bezier', pts.slice(idx, idx + 4), cp.slice(2 * idx - 2, 2 * idx + 2));
  5566. }
  5567. queue(ctx, 'quadratic', pts.slice(len - 2, len), [cp[2 * len - 10], cp[2 * len - 9], pts[len - 4], pts[len - 3]]);
  5568. drawLine(line, ctx, plot.height() + 10, series.splines.fill, series.color);
  5569. ctx.restore();
  5570. }
  5571. $.plot.plugins.push({
  5572. init: function(plot) {
  5573. plot.hooks.drawSeries.push(drawSpline);
  5574. },
  5575. options: {
  5576. series: {
  5577. splines: {
  5578. show: false,
  5579. lineWidth: 2,
  5580. tension: 0.5,
  5581. fill: false
  5582. }
  5583. }
  5584. },
  5585. name: 'spline',
  5586. version: '0.8.2'
  5587. });
  5588. })(jQuery);
  5589. /* Flot plugin that adds some extra symbols for plotting points.
  5590. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  5591. Licensed under the MIT license.
  5592. The symbols are accessed as strings through the standard symbol options:
  5593. series: {
  5594. points: {
  5595. symbol: "square" // or "diamond", "triangle", "cross"
  5596. }
  5597. }
  5598. */
  5599. (function ($) {
  5600. function processRawData(plot, series, datapoints) {
  5601. // we normalize the area of each symbol so it is approximately the
  5602. // same as a circle of the given radius
  5603. var handlers = {
  5604. square: function (ctx, x, y, radius, shadow) {
  5605. // pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2
  5606. var size = radius * Math.sqrt(Math.PI) / 2;
  5607. ctx.rect(x - size, y - size, size + size, size + size);
  5608. },
  5609. diamond: function (ctx, x, y, radius, shadow) {
  5610. // pi * r^2 = 2s^2 => s = r * sqrt(pi/2)
  5611. var size = radius * Math.sqrt(Math.PI / 2);
  5612. ctx.moveTo(x - size, y);
  5613. ctx.lineTo(x, y - size);
  5614. ctx.lineTo(x + size, y);
  5615. ctx.lineTo(x, y + size);
  5616. ctx.lineTo(x - size, y);
  5617. },
  5618. triangle: function (ctx, x, y, radius, shadow) {
  5619. // pi * r^2 = 1/2 * s^2 * sin (pi / 3) => s = r * sqrt(2 * pi / sin(pi / 3))
  5620. var size = radius * Math.sqrt(2 * Math.PI / Math.sin(Math.PI / 3));
  5621. var height = size * Math.sin(Math.PI / 3);
  5622. ctx.moveTo(x - size/2, y + height/2);
  5623. ctx.lineTo(x + size/2, y + height/2);
  5624. if (!shadow) {
  5625. ctx.lineTo(x, y - height/2);
  5626. ctx.lineTo(x - size/2, y + height/2);
  5627. }
  5628. },
  5629. cross: function (ctx, x, y, radius, shadow) {
  5630. // pi * r^2 = (2s)^2 => s = r * sqrt(pi)/2
  5631. var size = radius * Math.sqrt(Math.PI) / 2;
  5632. ctx.moveTo(x - size, y - size);
  5633. ctx.lineTo(x + size, y + size);
  5634. ctx.moveTo(x - size, y + size);
  5635. ctx.lineTo(x + size, y - size);
  5636. }
  5637. };
  5638. var s = series.points.symbol;
  5639. if (handlers[s])
  5640. series.points.symbol = handlers[s];
  5641. }
  5642. function init(plot) {
  5643. plot.hooks.processDatapoints.push(processRawData);
  5644. }
  5645. $.plot.plugins.push({
  5646. init: init,
  5647. name: 'symbols',
  5648. version: '1.0'
  5649. });
  5650. })(jQuery);
  5651. /* Flot plugin for thresholding data.
  5652. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  5653. Licensed under the MIT license.
  5654. The plugin supports these options:
  5655. series: {
  5656. threshold: {
  5657. below: number
  5658. color: colorspec
  5659. }
  5660. }
  5661. It can also be applied to a single series, like this:
  5662. $.plot( $("#placeholder"), [{
  5663. data: [ ... ],
  5664. threshold: { ... }
  5665. }])
  5666. An array can be passed for multiple thresholding, like this:
  5667. threshold: [{
  5668. below: number1
  5669. color: color1
  5670. },{
  5671. below: number2
  5672. color: color2
  5673. }]
  5674. These multiple threshold objects can be passed in any order since they are
  5675. sorted by the processing function.
  5676. The data points below "below" are drawn with the specified color. This makes
  5677. it easy to mark points below 0, e.g. for budget data.
  5678. Internally, the plugin works by splitting the data into two series, above and
  5679. below the threshold. The extra series below the threshold will have its label
  5680. cleared and the special "originSeries" attribute set to the original series.
  5681. You may need to check for this in hover events.
  5682. */
  5683. (function ($) {
  5684. var options = {
  5685. series: { threshold: null } // or { below: number, color: color spec}
  5686. };
  5687. function init(plot) {
  5688. function thresholdData(plot, s, datapoints, below, color) {
  5689. var ps = datapoints.pointsize, i, x, y, p, prevp,
  5690. thresholded = $.extend({}, s); // note: shallow copy
  5691. thresholded.datapoints = { points: [], pointsize: ps, format: datapoints.format };
  5692. thresholded.label = null;
  5693. thresholded.color = color;
  5694. thresholded.threshold = null;
  5695. thresholded.originSeries = s;
  5696. thresholded.data = [];
  5697. var origpoints = datapoints.points,
  5698. addCrossingPoints = s.lines.show;
  5699. var threspoints = [];
  5700. var newpoints = [];
  5701. var m;
  5702. for (i = 0; i < origpoints.length; i += ps) {
  5703. x = origpoints[i];
  5704. y = origpoints[i + 1];
  5705. prevp = p;
  5706. if (y < below)
  5707. p = threspoints;
  5708. else
  5709. p = newpoints;
  5710. if (addCrossingPoints && prevp != p && x != null
  5711. && i > 0 && origpoints[i - ps] != null) {
  5712. var interx = x + (below - y) * (x - origpoints[i - ps]) / (y - origpoints[i - ps + 1]);
  5713. prevp.push(interx);
  5714. prevp.push(below);
  5715. for (m = 2; m < ps; ++m)
  5716. prevp.push(origpoints[i + m]);
  5717. p.push(null); // start new segment
  5718. p.push(null);
  5719. for (m = 2; m < ps; ++m)
  5720. p.push(origpoints[i + m]);
  5721. p.push(interx);
  5722. p.push(below);
  5723. for (m = 2; m < ps; ++m)
  5724. p.push(origpoints[i + m]);
  5725. }
  5726. p.push(x);
  5727. p.push(y);
  5728. for (m = 2; m < ps; ++m)
  5729. p.push(origpoints[i + m]);
  5730. }
  5731. datapoints.points = newpoints;
  5732. thresholded.datapoints.points = threspoints;
  5733. if (thresholded.datapoints.points.length > 0) {
  5734. var origIndex = $.inArray(s, plot.getData());
  5735. // Insert newly-generated series right after original one (to prevent it from becoming top-most)
  5736. plot.getData().splice(origIndex + 1, 0, thresholded);
  5737. }
  5738. // FIXME: there are probably some edge cases left in bars
  5739. }
  5740. function processThresholds(plot, s, datapoints) {
  5741. if (!s.threshold)
  5742. return;
  5743. if (s.threshold instanceof Array) {
  5744. s.threshold.sort(function(a, b) {
  5745. return a.below - b.below;
  5746. });
  5747. $(s.threshold).each(function(i, th) {
  5748. thresholdData(plot, s, datapoints, th.below, th.color);
  5749. });
  5750. }
  5751. else {
  5752. thresholdData(plot, s, datapoints, s.threshold.below, s.threshold.color);
  5753. }
  5754. }
  5755. plot.hooks.processDatapoints.push(processThresholds);
  5756. }
  5757. $.plot.plugins.push({
  5758. init: init,
  5759. options: options,
  5760. name: 'threshold',
  5761. version: '1.2'
  5762. });
  5763. })(jQuery);
  5764. /*
  5765. * jquery.flot.tooltip
  5766. *
  5767. * description: easy-to-use tooltips for Flot charts
  5768. * version: 0.9.0
  5769. * authors: Krzysztof Urbas @krzysu [myviews.pl],Evan Steinkerchner @Roundaround
  5770. * website: https://github.com/krzysu/flot.tooltip
  5771. *
  5772. * build on 2016-07-26
  5773. * released under MIT License, 2012
  5774. */
  5775. (function ($) {
  5776. // plugin options, default values
  5777. var defaultOptions = {
  5778. tooltip: {
  5779. show: false,
  5780. cssClass: "flotTip",
  5781. content: "%s | X: %x | Y: %y",
  5782. // allowed templates are:
  5783. // %s -> series label,
  5784. // %c -> series color,
  5785. // %lx -> x axis label (requires flot-axislabels plugin https://github.com/markrcote/flot-axislabels),
  5786. // %ly -> y axis label (requires flot-axislabels plugin https://github.com/markrcote/flot-axislabels),
  5787. // %x -> X value,
  5788. // %y -> Y value,
  5789. // %x.2 -> precision of X value,
  5790. // %p -> percent
  5791. // %n -> value (not percent) of pie chart
  5792. xDateFormat: null,
  5793. yDateFormat: null,
  5794. monthNames: null,
  5795. dayNames: null,
  5796. shifts: {
  5797. x: 10,
  5798. y: 20
  5799. },
  5800. defaultTheme: true,
  5801. snap: true,
  5802. lines: false,
  5803. clickTips: false,
  5804. // callbacks
  5805. onHover: function (flotItem, $tooltipEl) {},
  5806. $compat: false
  5807. }
  5808. };
  5809. // dummy default options object for legacy code (<0.8.5) - is deleted later
  5810. defaultOptions.tooltipOpts = defaultOptions.tooltip;
  5811. // object
  5812. var FlotTooltip = function (plot) {
  5813. // variables
  5814. this.tipPosition = {x: 0, y: 0};
  5815. this.init(plot);
  5816. };
  5817. // main plugin function
  5818. FlotTooltip.prototype.init = function (plot) {
  5819. var that = this;
  5820. // detect other flot plugins
  5821. var plotPluginsLength = $.plot.plugins.length;
  5822. this.plotPlugins = [];
  5823. if (plotPluginsLength) {
  5824. for (var p = 0; p < plotPluginsLength; p++) {
  5825. this.plotPlugins.push($.plot.plugins[p].name);
  5826. }
  5827. }
  5828. plot.hooks.bindEvents.push(function (plot, eventHolder) {
  5829. // get plot options
  5830. that.plotOptions = plot.getOptions();
  5831. // for legacy (<0.8.5) implementations
  5832. if (typeof(that.plotOptions.tooltip) === 'boolean') {
  5833. that.plotOptions.tooltipOpts.show = that.plotOptions.tooltip;
  5834. that.plotOptions.tooltip = that.plotOptions.tooltipOpts;
  5835. delete that.plotOptions.tooltipOpts;
  5836. }
  5837. // if not enabled return
  5838. if (that.plotOptions.tooltip.show === false || typeof that.plotOptions.tooltip.show === 'undefined') return;
  5839. // shortcut to access tooltip options
  5840. that.tooltipOptions = that.plotOptions.tooltip;
  5841. if (that.tooltipOptions.$compat) {
  5842. that.wfunc = 'width';
  5843. that.hfunc = 'height';
  5844. } else {
  5845. that.wfunc = 'innerWidth';
  5846. that.hfunc = 'innerHeight';
  5847. }
  5848. // create tooltip DOM element
  5849. var $tip = that.getDomElement();
  5850. // bind event
  5851. $( plot.getPlaceholder() ).bind("plothover", plothover);
  5852. if (that.tooltipOptions.clickTips) {
  5853. $( plot.getPlaceholder() ).bind("plotclick", plotclick);
  5854. }
  5855. that.clickmode = false;
  5856. $(eventHolder).bind('mousemove', mouseMove);
  5857. });
  5858. plot.hooks.shutdown.push(function (plot, eventHolder){
  5859. $(plot.getPlaceholder()).unbind("plothover", plothover);
  5860. $(plot.getPlaceholder()).unbind("plotclick", plotclick);
  5861. plot.removeTooltip();
  5862. $(eventHolder).unbind("mousemove", mouseMove);
  5863. });
  5864. function mouseMove(e){
  5865. var pos = {};
  5866. pos.x = e.pageX;
  5867. pos.y = e.pageY;
  5868. plot.setTooltipPosition(pos);
  5869. }
  5870. /**
  5871. * open the tooltip (if not already open) and freeze it on the current position till the next click
  5872. */
  5873. function plotclick(event, pos, item) {
  5874. if (! that.clickmode) {
  5875. // it is the click activating the clicktip
  5876. plothover(event, pos, item);
  5877. if (that.getDomElement().is(":visible")) {
  5878. $(plot.getPlaceholder()).unbind("plothover", plothover);
  5879. that.clickmode = true;
  5880. }
  5881. } else {
  5882. // it is the click deactivating the clicktip
  5883. $( plot.getPlaceholder() ).bind("plothover", plothover);
  5884. plot.hideTooltip();
  5885. that.clickmode = false;
  5886. }
  5887. }
  5888. function plothover(event, pos, item) {
  5889. // Simple distance formula.
  5890. var lineDistance = function (p1x, p1y, p2x, p2y) {
  5891. return Math.sqrt((p2x - p1x) * (p2x - p1x) + (p2y - p1y) * (p2y - p1y));
  5892. };
  5893. // Here is some voodoo magic for determining the distance to a line form a given point {x, y}.
  5894. var dotLineLength = function (x, y, x0, y0, x1, y1, o) {
  5895. if (o && !(o =
  5896. function (x, y, x0, y0, x1, y1) {
  5897. if (typeof x0 !== 'undefined') return { x: x0, y: y };
  5898. else if (typeof y0 !== 'undefined') return { x: x, y: y0 };
  5899. var left,
  5900. tg = -1 / ((y1 - y0) / (x1 - x0));
  5901. return {
  5902. x: left = (x1 * (x * tg - y + y0) + x0 * (x * -tg + y - y1)) / (tg * (x1 - x0) + y0 - y1),
  5903. y: tg * left - tg * x + y
  5904. };
  5905. } (x, y, x0, y0, x1, y1),
  5906. o.x >= Math.min(x0, x1) && o.x <= Math.max(x0, x1) && o.y >= Math.min(y0, y1) && o.y <= Math.max(y0, y1))
  5907. ) {
  5908. var l1 = lineDistance(x, y, x0, y0), l2 = lineDistance(x, y, x1, y1);
  5909. return l1 > l2 ? l2 : l1;
  5910. } else {
  5911. var a = y0 - y1, b = x1 - x0, c = x0 * y1 - y0 * x1;
  5912. return Math.abs(a * x + b * y + c) / Math.sqrt(a * a + b * b);
  5913. }
  5914. };
  5915. if (item) {
  5916. plot.showTooltip(item, that.tooltipOptions.snap ? item : pos);
  5917. } else if (that.plotOptions.series.lines.show && that.tooltipOptions.lines === true) {
  5918. var maxDistance = that.plotOptions.grid.mouseActiveRadius;
  5919. var closestTrace = {
  5920. distance: maxDistance + 1
  5921. };
  5922. var ttPos = pos;
  5923. $.each(plot.getData(), function (i, series) {
  5924. var xBeforeIndex = 0,
  5925. xAfterIndex = -1;
  5926. // Our search here assumes our data is sorted via the x-axis.
  5927. // TODO: Improve efficiency somehow - search smaller sets of data.
  5928. for (var j = 1; j < series.data.length; j++) {
  5929. if (series.data[j - 1][0] <= pos.x && series.data[j][0] >= pos.x) {
  5930. xBeforeIndex = j - 1;
  5931. xAfterIndex = j;
  5932. }
  5933. }
  5934. if (xAfterIndex === -1) {
  5935. plot.hideTooltip();
  5936. return;
  5937. }
  5938. var pointPrev = { x: series.data[xBeforeIndex][0], y: series.data[xBeforeIndex][1] },
  5939. pointNext = { x: series.data[xAfterIndex][0], y: series.data[xAfterIndex][1] };
  5940. var distToLine = dotLineLength(series.xaxis.p2c(pos.x), series.yaxis.p2c(pos.y), series.xaxis.p2c(pointPrev.x),
  5941. series.yaxis.p2c(pointPrev.y), series.xaxis.p2c(pointNext.x), series.yaxis.p2c(pointNext.y), false);
  5942. if (distToLine < closestTrace.distance) {
  5943. var closestIndex = lineDistance(pointPrev.x, pointPrev.y, pos.x, pos.y) <
  5944. lineDistance(pos.x, pos.y, pointNext.x, pointNext.y) ? xBeforeIndex : xAfterIndex;
  5945. var pointSize = series.datapoints.pointsize;
  5946. // Calculate the point on the line vertically closest to our cursor.
  5947. var pointOnLine = [
  5948. pos.x,
  5949. pointPrev.y + ((pointNext.y - pointPrev.y) * ((pos.x - pointPrev.x) / (pointNext.x - pointPrev.x)))
  5950. ];
  5951. var item = {
  5952. datapoint: pointOnLine,
  5953. dataIndex: closestIndex,
  5954. series: series,
  5955. seriesIndex: i
  5956. };
  5957. closestTrace = {
  5958. distance: distToLine,
  5959. item: item
  5960. };
  5961. if (that.tooltipOptions.snap) {
  5962. ttPos = {
  5963. pageX: series.xaxis.p2c(pointOnLine[0]),
  5964. pageY: series.yaxis.p2c(pointOnLine[1])
  5965. };
  5966. }
  5967. }
  5968. });
  5969. if (closestTrace.distance < maxDistance + 1)
  5970. plot.showTooltip(closestTrace.item, ttPos);
  5971. else
  5972. plot.hideTooltip();
  5973. } else {
  5974. plot.hideTooltip();
  5975. }
  5976. }
  5977. // Quick little function for setting the tooltip position.
  5978. plot.setTooltipPosition = function (pos) {
  5979. var $tip = that.getDomElement();
  5980. var totalTipWidth = $tip.outerWidth() + that.tooltipOptions.shifts.x;
  5981. var totalTipHeight = $tip.outerHeight() + that.tooltipOptions.shifts.y;
  5982. if ((pos.x - $(window).scrollLeft()) > ($(window)[that.wfunc]() - totalTipWidth)) {
  5983. pos.x -= totalTipWidth;
  5984. pos.x = Math.max(pos.x, 0);
  5985. }
  5986. if ((pos.y - $(window).scrollTop()) > ($(window)[that.hfunc]() - totalTipHeight)) {
  5987. pos.y -= totalTipHeight;
  5988. }
  5989. /*
  5990. The section applies the new positioning ONLY if pos.x and pos.y
  5991. are numbers. If they are undefined or not a number, use the last
  5992. known numerical position. This hack fixes a bug that kept pie
  5993. charts from keeping their tooltip positioning.
  5994. */
  5995. if (isNaN(pos.x)) {
  5996. that.tipPosition.x = that.tipPosition.xPrev;
  5997. }
  5998. else {
  5999. that.tipPosition.x = pos.x;
  6000. that.tipPosition.xPrev = pos.x;
  6001. }
  6002. if (isNaN(pos.y)) {
  6003. that.tipPosition.y = that.tipPosition.yPrev;
  6004. }
  6005. else {
  6006. that.tipPosition.y = pos.y;
  6007. that.tipPosition.yPrev = pos.y;
  6008. }
  6009. };
  6010. // Quick little function for showing the tooltip.
  6011. plot.showTooltip = function (target, position, targetPosition) {
  6012. var $tip = that.getDomElement();
  6013. // convert tooltip content template to real tipText
  6014. var tipText = that.stringFormat(that.tooltipOptions.content, target);
  6015. if (tipText === '')
  6016. return;
  6017. $tip.html(tipText);
  6018. plot.setTooltipPosition({ x: that.tipPosition.x, y: that.tipPosition.y });
  6019. $tip.css({
  6020. left: that.tipPosition.x + that.tooltipOptions.shifts.x,
  6021. top: that.tipPosition.y + that.tooltipOptions.shifts.y
  6022. }).show();
  6023. // run callback
  6024. if (typeof that.tooltipOptions.onHover === 'function') {
  6025. that.tooltipOptions.onHover(target, $tip);
  6026. }
  6027. };
  6028. // Quick little function for hiding the tooltip.
  6029. plot.hideTooltip = function () {
  6030. that.getDomElement().hide().html('');
  6031. };
  6032. plot.removeTooltip = function() {
  6033. that.getDomElement().remove();
  6034. };
  6035. };
  6036. /**
  6037. * get or create tooltip DOM element
  6038. * @return jQuery object
  6039. */
  6040. FlotTooltip.prototype.getDomElement = function () {
  6041. var $tip = $('<div>');
  6042. if (this.tooltipOptions && this.tooltipOptions.cssClass) {
  6043. $tip = $('.' + this.tooltipOptions.cssClass);
  6044. if( $tip.length === 0 ){
  6045. $tip = $('<div />').addClass(this.tooltipOptions.cssClass);
  6046. $tip.appendTo('body').hide().css({position: 'absolute'});
  6047. if(this.tooltipOptions.defaultTheme) {
  6048. $tip.css({
  6049. 'background': '#fff',
  6050. 'z-index': '1040',
  6051. 'padding': '0.4em 0.6em',
  6052. 'border-radius': '0.5em',
  6053. 'font-size': '0.8em',
  6054. 'border': '1px solid #111',
  6055. 'display': 'none',
  6056. 'white-space': 'nowrap'
  6057. });
  6058. }
  6059. }
  6060. }
  6061. return $tip;
  6062. };
  6063. /**
  6064. * core function, create tooltip content
  6065. * @param {string} content - template with tooltip content
  6066. * @param {object} item - Flot item
  6067. * @return {string} real tooltip content for current item
  6068. */
  6069. FlotTooltip.prototype.stringFormat = function (content, item) {
  6070. var percentPattern = /%p\.{0,1}(\d{0,})/;
  6071. var seriesPattern = /%s/;
  6072. var colorPattern = /%c/;
  6073. var xLabelPattern = /%lx/; // requires flot-axislabels plugin https://github.com/markrcote/flot-axislabels, will be ignored if plugin isn't loaded
  6074. var yLabelPattern = /%ly/; // requires flot-axislabels plugin https://github.com/markrcote/flot-axislabels, will be ignored if plugin isn't loaded
  6075. var xPattern = /%x\.{0,1}(\d{0,})/;
  6076. var yPattern = /%y\.{0,1}(\d{0,})/;
  6077. var xPatternWithoutPrecision = "%x";
  6078. var yPatternWithoutPrecision = "%y";
  6079. var customTextPattern = "%ct";
  6080. var nPiePattern = "%n";
  6081. var x, y, customText, p, n;
  6082. // for threshold plugin we need to read data from different place
  6083. if (typeof item.series.threshold !== "undefined") {
  6084. x = item.datapoint[0];
  6085. y = item.datapoint[1];
  6086. customText = item.datapoint[2];
  6087. }
  6088. // for CurvedLines plugin we need to read data from different place
  6089. else if (typeof item.series.curvedLines !== "undefined") {
  6090. x = item.datapoint[0];
  6091. y = item.datapoint[1];
  6092. }
  6093. else if (typeof item.series.lines !== "undefined" && item.series.lines.steps) {
  6094. x = item.series.datapoints.points[item.dataIndex * 2];
  6095. y = item.series.datapoints.points[item.dataIndex * 2 + 1];
  6096. // TODO: where to find custom text in this variant?
  6097. customText = "";
  6098. } else {
  6099. x = item.series.data[item.dataIndex][0];
  6100. y = item.series.data[item.dataIndex][1];
  6101. customText = item.series.data[item.dataIndex][2];
  6102. }
  6103. // I think this is only in case of threshold plugin
  6104. if (item.series.label === null && item.series.originSeries) {
  6105. item.series.label = item.series.originSeries.label;
  6106. }
  6107. // if it is a function callback get the content string
  6108. if (typeof(content) === 'function') {
  6109. content = content(item.series.label, x, y, item);
  6110. }
  6111. // the case where the passed content is equal to false
  6112. if (typeof(content) === 'boolean' && !content) {
  6113. return '';
  6114. }
  6115. /* replacement of %ct and other multi-character templates must
  6116. precede the replacement of single-character templates
  6117. to avoid conflict between '%c' and '%ct' and similar substrings
  6118. */
  6119. if (customText) {
  6120. content = content.replace(customTextPattern, customText);
  6121. }
  6122. // percent match for pie charts and stacked percent
  6123. if (typeof (item.series.percent) !== 'undefined') {
  6124. p = item.series.percent;
  6125. } else if (typeof (item.series.percents) !== 'undefined') {
  6126. p = item.series.percents[item.dataIndex];
  6127. }
  6128. if (typeof p === 'number') {
  6129. content = this.adjustValPrecision(percentPattern, content, p);
  6130. }
  6131. // replace %n with number of items represented by slice in pie charts
  6132. if (item.series.hasOwnProperty('pie')) {
  6133. if (typeof item.series.data[0][1] !== 'undefined') {
  6134. n = item.series.data[0][1];
  6135. }
  6136. }
  6137. if (typeof n === 'number') {
  6138. content = content.replace(nPiePattern, n);
  6139. }
  6140. // series match
  6141. if (typeof(item.series.label) !== 'undefined') {
  6142. content = content.replace(seriesPattern, item.series.label);
  6143. } else {
  6144. //remove %s if label is undefined
  6145. content = content.replace(seriesPattern, "");
  6146. }
  6147. // color match
  6148. if (typeof(item.series.color) !== 'undefined') {
  6149. content = content.replace(colorPattern, item.series.color);
  6150. } else {
  6151. //remove %s if color is undefined
  6152. content = content.replace(colorPattern, "");
  6153. }
  6154. // x axis label match
  6155. if (this.hasAxisLabel('xaxis', item)) {
  6156. content = content.replace(xLabelPattern, item.series.xaxis.options.axisLabel);
  6157. } else {
  6158. //remove %lx if axis label is undefined or axislabels plugin not present
  6159. content = content.replace(xLabelPattern, "");
  6160. }
  6161. // y axis label match
  6162. if (this.hasAxisLabel('yaxis', item)) {
  6163. content = content.replace(yLabelPattern, item.series.yaxis.options.axisLabel);
  6164. } else {
  6165. //remove %ly if axis label is undefined or axislabels plugin not present
  6166. content = content.replace(yLabelPattern, "");
  6167. }
  6168. // time mode axes with custom dateFormat
  6169. if (this.isTimeMode('xaxis', item) && this.isXDateFormat(item)) {
  6170. content = content.replace(xPattern, this.timestampToDate(x, this.tooltipOptions.xDateFormat, item.series.xaxis.options));
  6171. }
  6172. if (this.isTimeMode('yaxis', item) && this.isYDateFormat(item)) {
  6173. content = content.replace(yPattern, this.timestampToDate(y, this.tooltipOptions.yDateFormat, item.series.yaxis.options));
  6174. }
  6175. // set precision if defined
  6176. if (typeof x === 'number') {
  6177. content = this.adjustValPrecision(xPattern, content, x);
  6178. }
  6179. if (typeof y === 'number') {
  6180. content = this.adjustValPrecision(yPattern, content, y);
  6181. }
  6182. // change x from number to given label, if given
  6183. if (typeof item.series.xaxis.ticks !== 'undefined') {
  6184. var ticks;
  6185. if (this.hasRotatedXAxisTicks(item)) {
  6186. // xaxis.ticks will be an empty array if tickRotor is being used, but the values are available in rotatedTicks
  6187. ticks = 'rotatedTicks';
  6188. } else {
  6189. ticks = 'ticks';
  6190. }
  6191. // see https://github.com/krzysu/flot.tooltip/issues/65
  6192. var tickIndex = item.dataIndex + item.seriesIndex;
  6193. for (var xIndex in item.series.xaxis[ticks]) {
  6194. if (item.series.xaxis[ticks].hasOwnProperty(tickIndex) && !this.isTimeMode('xaxis', item)) {
  6195. var valueX = (this.isCategoriesMode('xaxis', item)) ? item.series.xaxis[ticks][tickIndex].label : item.series.xaxis[ticks][tickIndex].v;
  6196. if (valueX === x) {
  6197. content = content.replace(xPattern, item.series.xaxis[ticks][tickIndex].label.replace(/\$/g, '$$$$'));
  6198. }
  6199. }
  6200. }
  6201. }
  6202. // change y from number to given label, if given
  6203. if (typeof item.series.yaxis.ticks !== 'undefined') {
  6204. for (var yIndex in item.series.yaxis.ticks) {
  6205. if (item.series.yaxis.ticks.hasOwnProperty(yIndex)) {
  6206. var valueY = (this.isCategoriesMode('yaxis', item)) ? item.series.yaxis.ticks[yIndex].label : item.series.yaxis.ticks[yIndex].v;
  6207. if (valueY === y) {
  6208. content = content.replace(yPattern, item.series.yaxis.ticks[yIndex].label.replace(/\$/g, '$$$$'));
  6209. }
  6210. }
  6211. }
  6212. }
  6213. // if no value customization, use tickFormatter by default
  6214. if (typeof item.series.xaxis.tickFormatter !== 'undefined') {
  6215. //escape dollar
  6216. content = content.replace(xPatternWithoutPrecision, item.series.xaxis.tickFormatter(x, item.series.xaxis).replace(/\$/g, '$$'));
  6217. }
  6218. if (typeof item.series.yaxis.tickFormatter !== 'undefined') {
  6219. //escape dollar
  6220. content = content.replace(yPatternWithoutPrecision, item.series.yaxis.tickFormatter(y, item.series.yaxis).replace(/\$/g, '$$'));
  6221. }
  6222. return content;
  6223. };
  6224. // helpers just for readability
  6225. FlotTooltip.prototype.isTimeMode = function (axisName, item) {
  6226. return (typeof item.series[axisName].options.mode !== 'undefined' && item.series[axisName].options.mode === 'time');
  6227. };
  6228. FlotTooltip.prototype.isXDateFormat = function (item) {
  6229. return (typeof this.tooltipOptions.xDateFormat !== 'undefined' && this.tooltipOptions.xDateFormat !== null);
  6230. };
  6231. FlotTooltip.prototype.isYDateFormat = function (item) {
  6232. return (typeof this.tooltipOptions.yDateFormat !== 'undefined' && this.tooltipOptions.yDateFormat !== null);
  6233. };
  6234. FlotTooltip.prototype.isCategoriesMode = function (axisName, item) {
  6235. return (typeof item.series[axisName].options.mode !== 'undefined' && item.series[axisName].options.mode === 'categories');
  6236. };
  6237. //
  6238. FlotTooltip.prototype.timestampToDate = function (tmst, dateFormat, options) {
  6239. var theDate = $.plot.dateGenerator(tmst, options);
  6240. return $.plot.formatDate(theDate, dateFormat, this.tooltipOptions.monthNames, this.tooltipOptions.dayNames);
  6241. };
  6242. //
  6243. FlotTooltip.prototype.adjustValPrecision = function (pattern, content, value) {
  6244. var precision;
  6245. var matchResult = content.match(pattern);
  6246. if( matchResult !== null ) {
  6247. if(RegExp.$1 !== '') {
  6248. precision = RegExp.$1;
  6249. value = value.toFixed(precision);
  6250. // only replace content if precision exists, in other case use thickformater
  6251. content = content.replace(pattern, value);
  6252. }
  6253. }
  6254. return content;
  6255. };
  6256. // other plugins detection below
  6257. // check if flot-axislabels plugin (https://github.com/markrcote/flot-axislabels) is used and that an axis label is given
  6258. FlotTooltip.prototype.hasAxisLabel = function (axisName, item) {
  6259. return ($.inArray('axisLabels', this.plotPlugins) !== -1 && typeof item.series[axisName].options.axisLabel !== 'undefined' && item.series[axisName].options.axisLabel.length > 0);
  6260. };
  6261. // check whether flot-tickRotor, a plugin which allows rotation of X-axis ticks, is being used
  6262. FlotTooltip.prototype.hasRotatedXAxisTicks = function (item) {
  6263. return ($.inArray('tickRotor',this.plotPlugins) !== -1 && typeof item.series.xaxis.rotatedTicks !== 'undefined');
  6264. };
  6265. //
  6266. var init = function (plot) {
  6267. new FlotTooltip(plot);
  6268. };
  6269. // define Flot plugin
  6270. $.plot.plugins.push({
  6271. init: init,
  6272. options: defaultOptions,
  6273. name: 'tooltip',
  6274. version: '0.8.5'
  6275. });
  6276. })(jQuery);
  6277. /* Pretty handling of time axes.
  6278. Copyright (c) 2007-2014 IOLA and Ole Laursen.
  6279. Licensed under the MIT license.
  6280. Set axis.mode to "time" to enable. See the section "Time series data" in
  6281. API.txt for details.
  6282. */
  6283. (function($) {
  6284. var options = {
  6285. xaxis: {
  6286. timezone: null, // "browser" for local to the client or timezone for timezone-js
  6287. timeformat: null, // format string to use
  6288. twelveHourClock: false, // 12 or 24 time in time mode
  6289. monthNames: null // list of names of months
  6290. }
  6291. };
  6292. // round to nearby lower multiple of base
  6293. function floorInBase(n, base) {
  6294. return base * Math.floor(n / base);
  6295. }
  6296. // Returns a string with the date d formatted according to fmt.
  6297. // A subset of the Open Group's strftime format is supported.
  6298. function formatDate(d, fmt, monthNames, dayNames) {
  6299. if (typeof d.strftime == "function") {
  6300. return d.strftime(fmt);
  6301. }
  6302. var leftPad = function(n, pad) {
  6303. n = "" + n;
  6304. pad = "" + (pad == null ? "0" : pad);
  6305. return n.length == 1 ? pad + n : n;
  6306. };
  6307. var r = [];
  6308. var escape = false;
  6309. var hours = d.getHours();
  6310. var isAM = hours < 12;
  6311. if (monthNames == null) {
  6312. monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  6313. }
  6314. if (dayNames == null) {
  6315. dayNames = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  6316. }
  6317. var hours12;
  6318. if (hours > 12) {
  6319. hours12 = hours - 12;
  6320. } else if (hours == 0) {
  6321. hours12 = 12;
  6322. } else {
  6323. hours12 = hours;
  6324. }
  6325. for (var i = 0; i < fmt.length; ++i) {
  6326. var c = fmt.charAt(i);
  6327. if (escape) {
  6328. switch (c) {
  6329. case 'a': c = "" + dayNames[d.getDay()]; break;
  6330. case 'b': c = "" + monthNames[d.getMonth()]; break;
  6331. case 'd': c = leftPad(d.getDate()); break;
  6332. case 'e': c = leftPad(d.getDate(), " "); break;
  6333. case 'h': // For back-compat with 0.7; remove in 1.0
  6334. case 'H': c = leftPad(hours); break;
  6335. case 'I': c = leftPad(hours12); break;
  6336. case 'l': c = leftPad(hours12, " "); break;
  6337. case 'm': c = leftPad(d.getMonth() + 1); break;
  6338. case 'M': c = leftPad(d.getMinutes()); break;
  6339. // quarters not in Open Group's strftime specification
  6340. case 'q':
  6341. c = "" + (Math.floor(d.getMonth() / 3) + 1); break;
  6342. case 'S': c = leftPad(d.getSeconds()); break;
  6343. case 'y': c = leftPad(d.getFullYear() % 100); break;
  6344. case 'Y': c = "" + d.getFullYear(); break;
  6345. case 'p': c = (isAM) ? ("" + "am") : ("" + "pm"); break;
  6346. case 'P': c = (isAM) ? ("" + "AM") : ("" + "PM"); break;
  6347. case 'w': c = "" + d.getDay(); break;
  6348. }
  6349. r.push(c);
  6350. escape = false;
  6351. } else {
  6352. if (c == "%") {
  6353. escape = true;
  6354. } else {
  6355. r.push(c);
  6356. }
  6357. }
  6358. }
  6359. return r.join("");
  6360. }
  6361. // To have a consistent view of time-based data independent of which time
  6362. // zone the client happens to be in we need a date-like object independent
  6363. // of time zones. This is done through a wrapper that only calls the UTC
  6364. // versions of the accessor methods.
  6365. function makeUtcWrapper(d) {
  6366. function addProxyMethod(sourceObj, sourceMethod, targetObj, targetMethod) {
  6367. sourceObj[sourceMethod] = function() {
  6368. return targetObj[targetMethod].apply(targetObj, arguments);
  6369. };
  6370. };
  6371. var utc = {
  6372. date: d
  6373. };
  6374. // support strftime, if found
  6375. if (d.strftime != undefined) {
  6376. addProxyMethod(utc, "strftime", d, "strftime");
  6377. }
  6378. addProxyMethod(utc, "getTime", d, "getTime");
  6379. addProxyMethod(utc, "setTime", d, "setTime");
  6380. var props = ["Date", "Day", "FullYear", "Hours", "Milliseconds", "Minutes", "Month", "Seconds"];
  6381. for (var p = 0; p < props.length; p++) {
  6382. addProxyMethod(utc, "get" + props[p], d, "getUTC" + props[p]);
  6383. addProxyMethod(utc, "set" + props[p], d, "setUTC" + props[p]);
  6384. }
  6385. return utc;
  6386. };
  6387. // select time zone strategy. This returns a date-like object tied to the
  6388. // desired timezone
  6389. function dateGenerator(ts, opts) {
  6390. if (opts.timezone == "browser") {
  6391. return new Date(ts);
  6392. } else if (!opts.timezone || opts.timezone == "utc") {
  6393. return makeUtcWrapper(new Date(ts));
  6394. } else if (typeof timezoneJS != "undefined" && typeof timezoneJS.Date != "undefined") {
  6395. var d = new timezoneJS.Date();
  6396. // timezone-js is fickle, so be sure to set the time zone before
  6397. // setting the time.
  6398. d.setTimezone(opts.timezone);
  6399. d.setTime(ts);
  6400. return d;
  6401. } else {
  6402. return makeUtcWrapper(new Date(ts));
  6403. }
  6404. }
  6405. // map of app. size of time units in milliseconds
  6406. var timeUnitSize = {
  6407. "second": 1000,
  6408. "minute": 60 * 1000,
  6409. "hour": 60 * 60 * 1000,
  6410. "day": 24 * 60 * 60 * 1000,
  6411. "month": 30 * 24 * 60 * 60 * 1000,
  6412. "quarter": 3 * 30 * 24 * 60 * 60 * 1000,
  6413. "year": 365.2425 * 24 * 60 * 60 * 1000
  6414. };
  6415. // the allowed tick sizes, after 1 year we use
  6416. // an integer algorithm
  6417. var baseSpec = [
  6418. [1, "second"], [2, "second"], [5, "second"], [10, "second"],
  6419. [30, "second"],
  6420. [1, "minute"], [2, "minute"], [5, "minute"], [10, "minute"],
  6421. [30, "minute"],
  6422. [1, "hour"], [2, "hour"], [4, "hour"],
  6423. [8, "hour"], [12, "hour"],
  6424. [1, "day"], [2, "day"], [3, "day"],
  6425. [0.25, "month"], [0.5, "month"], [1, "month"],
  6426. [2, "month"]
  6427. ];
  6428. // we don't know which variant(s) we'll need yet, but generating both is
  6429. // cheap
  6430. var specMonths = baseSpec.concat([[3, "month"], [6, "month"],
  6431. [1, "year"]]);
  6432. var specQuarters = baseSpec.concat([[1, "quarter"], [2, "quarter"],
  6433. [1, "year"]]);
  6434. function init(plot) {
  6435. plot.hooks.processOptions.push(function (plot, options) {
  6436. $.each(plot.getAxes(), function(axisName, axis) {
  6437. var opts = axis.options;
  6438. if (opts.mode == "time") {
  6439. axis.tickGenerator = function(axis) {
  6440. var ticks = [];
  6441. var d = dateGenerator(axis.min, opts);
  6442. var minSize = 0;
  6443. // make quarter use a possibility if quarters are
  6444. // mentioned in either of these options
  6445. var spec = (opts.tickSize && opts.tickSize[1] ===
  6446. "quarter") ||
  6447. (opts.minTickSize && opts.minTickSize[1] ===
  6448. "quarter") ? specQuarters : specMonths;
  6449. if (opts.minTickSize != null) {
  6450. if (typeof opts.tickSize == "number") {
  6451. minSize = opts.tickSize;
  6452. } else {
  6453. minSize = opts.minTickSize[0] * timeUnitSize[opts.minTickSize[1]];
  6454. }
  6455. }
  6456. for (var i = 0; i < spec.length - 1; ++i) {
  6457. if (axis.delta < (spec[i][0] * timeUnitSize[spec[i][1]]
  6458. + spec[i + 1][0] * timeUnitSize[spec[i + 1][1]]) / 2
  6459. && spec[i][0] * timeUnitSize[spec[i][1]] >= minSize) {
  6460. break;
  6461. }
  6462. }
  6463. var size = spec[i][0];
  6464. var unit = spec[i][1];
  6465. // special-case the possibility of several years
  6466. if (unit == "year") {
  6467. // if given a minTickSize in years, just use it,
  6468. // ensuring that it's an integer
  6469. if (opts.minTickSize != null && opts.minTickSize[1] == "year") {
  6470. size = Math.floor(opts.minTickSize[0]);
  6471. } else {
  6472. var magn = Math.pow(10, Math.floor(Math.log(axis.delta / timeUnitSize.year) / Math.LN10));
  6473. var norm = (axis.delta / timeUnitSize.year) / magn;
  6474. if (norm < 1.5) {
  6475. size = 1;
  6476. } else if (norm < 3) {
  6477. size = 2;
  6478. } else if (norm < 7.5) {
  6479. size = 5;
  6480. } else {
  6481. size = 10;
  6482. }
  6483. size *= magn;
  6484. }
  6485. // minimum size for years is 1
  6486. if (size < 1) {
  6487. size = 1;
  6488. }
  6489. }
  6490. axis.tickSize = opts.tickSize || [size, unit];
  6491. var tickSize = axis.tickSize[0];
  6492. unit = axis.tickSize[1];
  6493. var step = tickSize * timeUnitSize[unit];
  6494. if (unit == "second") {
  6495. d.setSeconds(floorInBase(d.getSeconds(), tickSize));
  6496. } else if (unit == "minute") {
  6497. d.setMinutes(floorInBase(d.getMinutes(), tickSize));
  6498. } else if (unit == "hour") {
  6499. d.setHours(floorInBase(d.getHours(), tickSize));
  6500. } else if (unit == "month") {
  6501. d.setMonth(floorInBase(d.getMonth(), tickSize));
  6502. } else if (unit == "quarter") {
  6503. d.setMonth(3 * floorInBase(d.getMonth() / 3,
  6504. tickSize));
  6505. } else if (unit == "year") {
  6506. d.setFullYear(floorInBase(d.getFullYear(), tickSize));
  6507. }
  6508. // reset smaller components
  6509. d.setMilliseconds(0);
  6510. if (step >= timeUnitSize.minute) {
  6511. d.setSeconds(0);
  6512. }
  6513. if (step >= timeUnitSize.hour) {
  6514. d.setMinutes(0);
  6515. }
  6516. if (step >= timeUnitSize.day) {
  6517. d.setHours(0);
  6518. }
  6519. if (step >= timeUnitSize.day * 4) {
  6520. d.setDate(1);
  6521. }
  6522. if (step >= timeUnitSize.month * 2) {
  6523. d.setMonth(floorInBase(d.getMonth(), 3));
  6524. }
  6525. if (step >= timeUnitSize.quarter * 2) {
  6526. d.setMonth(floorInBase(d.getMonth(), 6));
  6527. }
  6528. if (step >= timeUnitSize.year) {
  6529. d.setMonth(0);
  6530. }
  6531. var carry = 0;
  6532. var v = Number.NaN;
  6533. var prev;
  6534. do {
  6535. prev = v;
  6536. v = d.getTime();
  6537. ticks.push(v);
  6538. if (unit == "month" || unit == "quarter") {
  6539. if (tickSize < 1) {
  6540. // a bit complicated - we'll divide the
  6541. // month/quarter up but we need to take
  6542. // care of fractions so we don't end up in
  6543. // the middle of a day
  6544. d.setDate(1);
  6545. var start = d.getTime();
  6546. d.setMonth(d.getMonth() +
  6547. (unit == "quarter" ? 3 : 1));
  6548. var end = d.getTime();
  6549. d.setTime(v + carry * timeUnitSize.hour + (end - start) * tickSize);
  6550. carry = d.getHours();
  6551. d.setHours(0);
  6552. } else {
  6553. d.setMonth(d.getMonth() +
  6554. tickSize * (unit == "quarter" ? 3 : 1));
  6555. }
  6556. } else if (unit == "year") {
  6557. d.setFullYear(d.getFullYear() + tickSize);
  6558. } else {
  6559. d.setTime(v + step);
  6560. }
  6561. } while (v < axis.max && v != prev);
  6562. return ticks;
  6563. };
  6564. axis.tickFormatter = function (v, axis) {
  6565. var d = dateGenerator(v, axis.options);
  6566. // first check global format
  6567. if (opts.timeformat != null) {
  6568. return formatDate(d, opts.timeformat, opts.monthNames, opts.dayNames);
  6569. }
  6570. // possibly use quarters if quarters are mentioned in
  6571. // any of these places
  6572. var useQuarters = (axis.options.tickSize &&
  6573. axis.options.tickSize[1] == "quarter") ||
  6574. (axis.options.minTickSize &&
  6575. axis.options.minTickSize[1] == "quarter");
  6576. var t = axis.tickSize[0] * timeUnitSize[axis.tickSize[1]];
  6577. var span = axis.max - axis.min;
  6578. var suffix = (opts.twelveHourClock) ? " %p" : "";
  6579. var hourCode = (opts.twelveHourClock) ? "%I" : "%H";
  6580. var fmt;
  6581. if (t < timeUnitSize.minute) {
  6582. fmt = hourCode + ":%M:%S" + suffix;
  6583. } else if (t < timeUnitSize.day) {
  6584. if (span < 2 * timeUnitSize.day) {
  6585. fmt = hourCode + ":%M" + suffix;
  6586. } else {
  6587. fmt = "%b %d " + hourCode + ":%M" + suffix;
  6588. }
  6589. } else if (t < timeUnitSize.month) {
  6590. fmt = "%b %d";
  6591. } else if ((useQuarters && t < timeUnitSize.quarter) ||
  6592. (!useQuarters && t < timeUnitSize.year)) {
  6593. if (span < timeUnitSize.year) {
  6594. fmt = "%b";
  6595. } else {
  6596. fmt = "%b %Y";
  6597. }
  6598. } else if (useQuarters && t < timeUnitSize.year) {
  6599. if (span < timeUnitSize.year) {
  6600. fmt = "Q%q";
  6601. } else {
  6602. fmt = "Q%q %Y";
  6603. }
  6604. } else {
  6605. fmt = "%Y";
  6606. }
  6607. var rt = formatDate(d, fmt, opts.monthNames, opts.dayNames);
  6608. return rt;
  6609. };
  6610. }
  6611. });
  6612. });
  6613. }
  6614. $.plot.plugins.push({
  6615. init: init,
  6616. options: options,
  6617. name: 'time',
  6618. version: '1.0'
  6619. });
  6620. // Time-axis support used to be in Flot core, which exposed the
  6621. // formatDate function on the plot object. Various plugins depend
  6622. // on the function, so we need to re-expose it here.
  6623. $.plot.formatDate = formatDate;
  6624. $.plot.dateGenerator = dateGenerator;
  6625. })(jQuery);