jit.js 479 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155
  1. /*
  2. Copyright (c) 2011 Sencha Inc. - Author: Nicolas Garcia Belmonte (http://philogb.github.com/)
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. (function () {
  20. /*
  21. File: Core.js
  22. /*
  23. Object: $jit
  24. Defines the namespace for all library Classes and Objects.
  25. This variable is the *only* global variable defined in the Toolkit.
  26. There are also other interesting properties attached to this variable described below.
  27. */
  28. window.$jit = function(w) {
  29. w = w || window;
  30. for(var k in $jit) {
  31. if($jit[k].$extend) {
  32. w[k] = $jit[k];
  33. }
  34. }
  35. };
  36. $jit.version = '2.0.1';
  37. /*
  38. Object: $jit.id
  39. Works just like *document.getElementById*
  40. Example:
  41. (start code js)
  42. var element = $jit.id('elementId');
  43. (end code)
  44. */
  45. /*
  46. Object: $jit.util
  47. Contains utility functions.
  48. Some of the utility functions and the Class system were based in the MooTools Framework
  49. <http://mootools.net>. Copyright (c) 2006-2010 Valerio Proietti, <http://mad4milk.net/>.
  50. MIT license <http://mootools.net/license.txt>.
  51. These methods are generally also implemented in DOM manipulation frameworks like JQuery, MooTools and Prototype.
  52. I'd suggest you to use the functions from those libraries instead of using these, since their functions
  53. are widely used and tested in many different platforms/browsers. Use these functions only if you have to.
  54. */
  55. var $ = function(d) {
  56. return document.getElementById(d);
  57. };
  58. $.empty = function() {
  59. };
  60. /*
  61. Method: extend
  62. Augment an object by appending another object's properties.
  63. Parameters:
  64. original - (object) The object to be extended.
  65. extended - (object) An object which properties are going to be appended to the original object.
  66. Example:
  67. (start code js)
  68. $jit.util.extend({ 'a': 1, 'b': 2 }, { 'b': 3, 'c': 4 }); //{ 'a':1, 'b': 3, 'c': 4 }
  69. (end code)
  70. */
  71. $.extend = function(original, extended) {
  72. for ( var key in (extended || {}))
  73. original[key] = extended[key];
  74. return original;
  75. };
  76. $.lambda = function(value) {
  77. return (typeof value == 'function') ? value : function() {
  78. return value;
  79. };
  80. };
  81. $.time = Date.now || function() {
  82. return +new Date;
  83. };
  84. /*
  85. Method: splat
  86. Returns an array wrapping *obj* if *obj* is not an array. Returns *obj* otherwise.
  87. Parameters:
  88. obj - (mixed) The object to be wrapped in an array.
  89. Example:
  90. (start code js)
  91. $jit.util.splat(3); //[3]
  92. $jit.util.splat([3]); //[3]
  93. (end code)
  94. */
  95. $.splat = function(obj) {
  96. var type = $.type(obj);
  97. return type ? ((type != 'array') ? [ obj ] : obj) : [];
  98. };
  99. $.type = function(elem) {
  100. var type = $.type.s.call(elem).match(/^\[object\s(.*)\]$/)[1].toLowerCase();
  101. if(type != 'object') return type;
  102. if(elem && elem.$$family) return elem.$$family;
  103. return (elem && elem.nodeName && elem.nodeType == 1)? 'element' : type;
  104. };
  105. $.type.s = Object.prototype.toString;
  106. /*
  107. Method: each
  108. Iterates through an iterable applying *f*.
  109. Parameters:
  110. iterable - (array) The original array.
  111. fn - (function) The function to apply to the array elements.
  112. Example:
  113. (start code js)
  114. $jit.util.each([3, 4, 5], function(n) { alert('number ' + n); });
  115. (end code)
  116. */
  117. $.each = function(iterable, fn) {
  118. var type = $.type(iterable);
  119. if (type == 'object') {
  120. for ( var key in iterable)
  121. fn(iterable[key], key);
  122. } else {
  123. for ( var i = 0, l = iterable.length; i < l; i++)
  124. fn(iterable[i], i);
  125. }
  126. };
  127. $.indexOf = function(array, item) {
  128. if(Array.indexOf) return array.indexOf(item);
  129. for(var i=0,l=array.length; i<l; i++) {
  130. if(array[i] === item) return i;
  131. }
  132. return -1;
  133. };
  134. /*
  135. Method: map
  136. Maps or collects an array by applying *f*.
  137. Parameters:
  138. array - (array) The original array.
  139. f - (function) The function to apply to the array elements.
  140. Example:
  141. (start code js)
  142. $jit.util.map([3, 4, 5], function(n) { return n*n; }); //[9, 16, 25]
  143. (end code)
  144. */
  145. $.map = function(array, f) {
  146. var ans = [];
  147. $.each(array, function(elem, i) {
  148. ans.push(f(elem, i));
  149. });
  150. return ans;
  151. };
  152. /*
  153. Method: reduce
  154. Iteratively applies the binary function *f* storing the result in an accumulator.
  155. Parameters:
  156. array - (array) The original array.
  157. f - (function) The function to apply to the array elements.
  158. opt - (optional|mixed) The starting value for the acumulator.
  159. Example:
  160. (start code js)
  161. $jit.util.reduce([3, 4, 5], function(x, y) { return x + y; }, 0); //12
  162. (end code)
  163. */
  164. $.reduce = function(array, f, opt) {
  165. var l = array.length;
  166. if(l==0) return opt;
  167. var acum = arguments.length == 3? opt : array[--l];
  168. while(l--) {
  169. acum = f(acum, array[l]);
  170. }
  171. return acum;
  172. };
  173. /*
  174. Method: merge
  175. Merges n-objects and their sub-objects creating a new, fresh object.
  176. Parameters:
  177. An arbitrary number of objects.
  178. Example:
  179. (start code js)
  180. $jit.util.merge({ 'a': 1, 'b': 2 }, { 'b': 3, 'c': 4 }); //{ 'a':1, 'b': 3, 'c': 4 }
  181. (end code)
  182. */
  183. $.merge = function() {
  184. var mix = {};
  185. for ( var i = 0, l = arguments.length; i < l; i++) {
  186. var object = arguments[i];
  187. if ($.type(object) != 'object')
  188. continue;
  189. for ( var key in object) {
  190. var op = object[key], mp = mix[key];
  191. mix[key] = (mp && $.type(op) == 'object' && $.type(mp) == 'object') ? $
  192. .merge(mp, op) : $.unlink(op);
  193. }
  194. }
  195. return mix;
  196. };
  197. $.unlink = function(object) {
  198. var unlinked;
  199. switch ($.type(object)) {
  200. case 'object':
  201. unlinked = {};
  202. for ( var p in object)
  203. unlinked[p] = $.unlink(object[p]);
  204. break;
  205. case 'array':
  206. unlinked = [];
  207. for ( var i = 0, l = object.length; i < l; i++)
  208. unlinked[i] = $.unlink(object[i]);
  209. break;
  210. default:
  211. return object;
  212. }
  213. return unlinked;
  214. };
  215. $.zip = function() {
  216. if(arguments.length === 0) return [];
  217. for(var j=0, ans=[], l=arguments.length, ml=arguments[0].length; j<ml; j++) {
  218. for(var i=0, row=[]; i<l; i++) {
  219. row.push(arguments[i][j]);
  220. }
  221. ans.push(row);
  222. }
  223. return ans;
  224. };
  225. /*
  226. Method: rgbToHex
  227. Converts an RGB array into a Hex string.
  228. Parameters:
  229. srcArray - (array) An array with R, G and B values
  230. Example:
  231. (start code js)
  232. $jit.util.rgbToHex([255, 255, 255]); //'#ffffff'
  233. (end code)
  234. */
  235. $.rgbToHex = function(srcArray, array) {
  236. if (srcArray.length < 3)
  237. return null;
  238. if (srcArray.length == 4 && srcArray[3] == 0 && !array)
  239. return 'transparent';
  240. var hex = [];
  241. for ( var i = 0; i < 3; i++) {
  242. var bit = (srcArray[i] - 0).toString(16);
  243. hex.push(bit.length == 1 ? '0' + bit : bit);
  244. }
  245. return array ? hex : '#' + hex.join('');
  246. };
  247. /*
  248. Method: hexToRgb
  249. Converts an Hex color string into an RGB array.
  250. Parameters:
  251. hex - (string) A color hex string.
  252. Example:
  253. (start code js)
  254. $jit.util.hexToRgb('#fff'); //[255, 255, 255]
  255. (end code)
  256. */
  257. $.hexToRgb = function(hex) {
  258. if (hex.length != 7) {
  259. hex = hex.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
  260. hex.shift();
  261. if (hex.length != 3)
  262. return null;
  263. var rgb = [];
  264. for ( var i = 0; i < 3; i++) {
  265. var value = hex[i];
  266. if (value.length == 1)
  267. value += value;
  268. rgb.push(parseInt(value, 16));
  269. }
  270. return rgb;
  271. } else {
  272. hex = parseInt(hex.slice(1), 16);
  273. return [ hex >> 16, hex >> 8 & 0xff, hex & 0xff ];
  274. }
  275. };
  276. $.destroy = function(elem) {
  277. $.clean(elem);
  278. if (elem.parentNode)
  279. elem.parentNode.removeChild(elem);
  280. if (elem.clearAttributes)
  281. elem.clearAttributes();
  282. };
  283. $.clean = function(elem) {
  284. for (var ch = elem.childNodes, i = 0, l = ch.length; i < l; i++) {
  285. $.destroy(ch[i]);
  286. }
  287. };
  288. /*
  289. Method: addEvent
  290. Cross-browser add event listener.
  291. Parameters:
  292. obj - (obj) The Element to attach the listener to.
  293. type - (string) The listener type. For example 'click', or 'mousemove'.
  294. fn - (function) The callback function to be used when the event is fired.
  295. Example:
  296. (start code js)
  297. $jit.util.addEvent(elem, 'click', function(){ alert('hello'); });
  298. (end code)
  299. */
  300. $.addEvent = function(obj, type, fn) {
  301. if (obj.addEventListener)
  302. obj.addEventListener(type, fn, false);
  303. else
  304. obj.attachEvent('on' + type, fn);
  305. };
  306. $.addEvents = function(obj, typeObj) {
  307. for(var type in typeObj) {
  308. $.addEvent(obj, type, typeObj[type]);
  309. }
  310. };
  311. $.hasClass = function(obj, klass) {
  312. return (' ' + obj.className + ' ').indexOf(' ' + klass + ' ') > -1;
  313. };
  314. $.addClass = function(obj, klass) {
  315. if (!$.hasClass(obj, klass))
  316. obj.className = (obj.className + " " + klass);
  317. };
  318. $.removeClass = function(obj, klass) {
  319. obj.className = obj.className.replace(new RegExp(
  320. '(^|\\s)' + klass + '(?:\\s|$)'), '$1');
  321. };
  322. $.getPos = function(elem) {
  323. var offset = getOffsets(elem);
  324. var scroll = getScrolls(elem);
  325. return {
  326. x: offset.x - scroll.x,
  327. y: offset.y - scroll.y
  328. };
  329. function getOffsets(elem) {
  330. var position = {
  331. x: 0,
  332. y: 0
  333. };
  334. while (elem && !isBody(elem)) {
  335. position.x += elem.offsetLeft;
  336. position.y += elem.offsetTop;
  337. elem = elem.offsetParent;
  338. }
  339. return position;
  340. }
  341. function getScrolls(elem) {
  342. var position = {
  343. x: 0,
  344. y: 0
  345. };
  346. while (elem && !isBody(elem)) {
  347. position.x += elem.scrollLeft;
  348. position.y += elem.scrollTop;
  349. elem = elem.parentNode;
  350. }
  351. return position;
  352. }
  353. function isBody(element) {
  354. return (/^(?:body|html)$/i).test(element.tagName);
  355. }
  356. };
  357. $.event = {
  358. get: function(e, win) {
  359. win = win || window;
  360. return e || win.event;
  361. },
  362. getWheel: function(e) {
  363. return e.wheelDelta? e.wheelDelta / 120 : -(e.detail || 0) / 3;
  364. },
  365. isRightClick: function(e) {
  366. return (e.which == 3 || e.button == 2);
  367. },
  368. getPos: function(e, win) {
  369. // get mouse position
  370. win = win || window;
  371. e = e || win.event;
  372. var doc = win.document;
  373. doc = doc.documentElement || doc.body;
  374. //make touch event handling better
  375. if(e.touches && e.touches.length) {
  376. e = e.touches[0];
  377. }
  378. var page = {
  379. x: e.pageX || (e.clientX + doc.scrollLeft),
  380. y: e.pageY || (e.clientY + doc.scrollTop)
  381. };
  382. return page;
  383. },
  384. stop: function(e) {
  385. if (e.stopPropagation) e.stopPropagation();
  386. e.cancelBubble = true;
  387. if (e.preventDefault) e.preventDefault();
  388. else e.returnValue = false;
  389. }
  390. };
  391. $jit.util = $jit.id = $;
  392. var Class = function(properties) {
  393. properties = properties || {};
  394. var klass = function() {
  395. for ( var key in this) {
  396. if (typeof this[key] != 'function')
  397. this[key] = $.unlink(this[key]);
  398. }
  399. this.constructor = klass;
  400. if (Class.prototyping)
  401. return this;
  402. var instance = this.initialize ? this.initialize.apply(this, arguments)
  403. : this;
  404. //typize
  405. this.$$family = 'class';
  406. return instance;
  407. };
  408. for ( var mutator in Class.Mutators) {
  409. if (!properties[mutator])
  410. continue;
  411. properties = Class.Mutators[mutator](properties, properties[mutator]);
  412. delete properties[mutator];
  413. }
  414. $.extend(klass, this);
  415. klass.constructor = Class;
  416. klass.prototype = properties;
  417. return klass;
  418. };
  419. Class.Mutators = {
  420. Implements: function(self, klasses) {
  421. $.each($.splat(klasses), function(klass) {
  422. Class.prototyping = klass;
  423. var instance = (typeof klass == 'function') ? new klass : klass;
  424. for ( var prop in instance) {
  425. if (!(prop in self)) {
  426. self[prop] = instance[prop];
  427. }
  428. }
  429. delete Class.prototyping;
  430. });
  431. return self;
  432. }
  433. };
  434. $.extend(Class, {
  435. inherit: function(object, properties) {
  436. for ( var key in properties) {
  437. var override = properties[key];
  438. var previous = object[key];
  439. var type = $.type(override);
  440. if (previous && type == 'function') {
  441. if (override != previous) {
  442. Class.override(object, key, override);
  443. }
  444. } else if (type == 'object') {
  445. object[key] = $.merge(previous, override);
  446. } else {
  447. object[key] = override;
  448. }
  449. }
  450. return object;
  451. },
  452. override: function(object, name, method) {
  453. var parent = Class.prototyping;
  454. if (parent && object[name] != parent[name])
  455. parent = null;
  456. var override = function() {
  457. var previous = this.parent;
  458. this.parent = parent ? parent[name] : object[name];
  459. var value = method.apply(this, arguments);
  460. this.parent = previous;
  461. return value;
  462. };
  463. object[name] = override;
  464. }
  465. });
  466. Class.prototype.implement = function() {
  467. var proto = this.prototype;
  468. $.each(Array.prototype.slice.call(arguments || []), function(properties) {
  469. Class.inherit(proto, properties);
  470. });
  471. return this;
  472. };
  473. $jit.Class = Class;
  474. /*
  475. Object: $jit.json
  476. Provides JSON utility functions.
  477. Most of these functions are JSON-tree traversal and manipulation functions.
  478. */
  479. $jit.json = {
  480. /*
  481. Method: prune
  482. Clears all tree nodes having depth greater than maxLevel.
  483. Parameters:
  484. tree - (object) A JSON tree object. For more information please see <Loader.loadJSON>.
  485. maxLevel - (number) An integer specifying the maximum level allowed for this tree. All nodes having depth greater than max level will be deleted.
  486. */
  487. prune: function(tree, maxLevel) {
  488. this.each(tree, function(elem, i) {
  489. if (i == maxLevel && elem.children) {
  490. delete elem.children;
  491. elem.children = [];
  492. }
  493. });
  494. },
  495. /*
  496. Method: getParent
  497. Returns the parent node of the node having _id_ as id.
  498. Parameters:
  499. tree - (object) A JSON tree object. See also <Loader.loadJSON>.
  500. id - (string) The _id_ of the child node whose parent will be returned.
  501. Returns:
  502. A tree JSON node if any, or false otherwise.
  503. */
  504. getParent: function(tree, id) {
  505. if (tree.id == id)
  506. return false;
  507. var ch = tree.children;
  508. if (ch && ch.length > 0) {
  509. for ( var i = 0; i < ch.length; i++) {
  510. if (ch[i].id == id)
  511. return tree;
  512. else {
  513. var ans = this.getParent(ch[i], id);
  514. if (ans)
  515. return ans;
  516. }
  517. }
  518. }
  519. return false;
  520. },
  521. /*
  522. Method: getSubtree
  523. Returns the subtree that matches the given id.
  524. Parameters:
  525. tree - (object) A JSON tree object. See also <Loader.loadJSON>.
  526. id - (string) A node *unique* identifier.
  527. Returns:
  528. A subtree having a root node matching the given id. Returns null if no subtree matching the id is found.
  529. */
  530. getSubtree: function(tree, id) {
  531. if (tree.id == id)
  532. return tree;
  533. for ( var i = 0, ch = tree.children; ch && i < ch.length; i++) {
  534. var t = this.getSubtree(ch[i], id);
  535. if (t != null)
  536. return t;
  537. }
  538. return null;
  539. },
  540. /*
  541. Method: eachLevel
  542. Iterates on tree nodes with relative depth less or equal than a specified level.
  543. Parameters:
  544. tree - (object) A JSON tree or subtree. See also <Loader.loadJSON>.
  545. initLevel - (number) An integer specifying the initial relative level. Usually zero.
  546. toLevel - (number) An integer specifying a top level. This method will iterate only through nodes with depth less than or equal this number.
  547. action - (function) A function that receives a node and an integer specifying the actual level of the node.
  548. Example:
  549. (start code js)
  550. $jit.json.eachLevel(tree, 0, 3, function(node, depth) {
  551. alert(node.name + ' ' + depth);
  552. });
  553. (end code)
  554. */
  555. eachLevel: function(tree, initLevel, toLevel, action) {
  556. if (initLevel <= toLevel) {
  557. action(tree, initLevel);
  558. if(!tree.children) return;
  559. for ( var i = 0, ch = tree.children; i < ch.length; i++) {
  560. this.eachLevel(ch[i], initLevel + 1, toLevel, action);
  561. }
  562. }
  563. },
  564. /*
  565. Method: each
  566. A JSON tree iterator.
  567. Parameters:
  568. tree - (object) A JSON tree or subtree. See also <Loader.loadJSON>.
  569. action - (function) A function that receives a node.
  570. Example:
  571. (start code js)
  572. $jit.json.each(tree, function(node) {
  573. alert(node.name);
  574. });
  575. (end code)
  576. */
  577. each: function(tree, action) {
  578. this.eachLevel(tree, 0, Number.MAX_VALUE, action);
  579. }
  580. };
  581. /*
  582. An object containing multiple type of transformations.
  583. */
  584. $jit.Trans = {
  585. $extend: true,
  586. linear: function(p){
  587. return p;
  588. }
  589. };
  590. var Trans = $jit.Trans;
  591. (function(){
  592. var makeTrans = function(transition, params){
  593. params = $.splat(params);
  594. return $.extend(transition, {
  595. easeIn: function(pos){
  596. return transition(pos, params);
  597. },
  598. easeOut: function(pos){
  599. return 1 - transition(1 - pos, params);
  600. },
  601. easeInOut: function(pos){
  602. return (pos <= 0.5)? transition(2 * pos, params) / 2 : (2 - transition(
  603. 2 * (1 - pos), params)) / 2;
  604. }
  605. });
  606. };
  607. var transitions = {
  608. Pow: function(p, x){
  609. return Math.pow(p, x[0] || 6);
  610. },
  611. Expo: function(p){
  612. return Math.pow(2, 8 * (p - 1));
  613. },
  614. Circ: function(p){
  615. return 1 - Math.sin(Math.acos(p));
  616. },
  617. Sine: function(p){
  618. return 1 - Math.sin((1 - p) * Math.PI / 2);
  619. },
  620. Back: function(p, x){
  621. x = x[0] || 1.618;
  622. return Math.pow(p, 2) * ((x + 1) * p - x);
  623. },
  624. Bounce: function(p){
  625. var value;
  626. for ( var a = 0, b = 1; 1; a += b, b /= 2) {
  627. if (p >= (7 - 4 * a) / 11) {
  628. value = b * b - Math.pow((11 - 6 * a - 11 * p) / 4, 2);
  629. break;
  630. }
  631. }
  632. return value;
  633. },
  634. Elastic: function(p, x){
  635. return Math.pow(2, 10 * --p)
  636. * Math.cos(20 * p * Math.PI * (x[0] || 1) / 3);
  637. }
  638. };
  639. $.each(transitions, function(val, key){
  640. Trans[key] = makeTrans(val);
  641. });
  642. $.each( [
  643. 'Quad', 'Cubic', 'Quart', 'Quint'
  644. ], function(elem, i){
  645. Trans[elem] = makeTrans(function(p){
  646. return Math.pow(p, [
  647. i + 2
  648. ]);
  649. });
  650. });
  651. })();
  652. /*
  653. A Class that can perform animations for generic objects.
  654. If you are looking for animation transitions please take a look at the <Trans> object.
  655. Used by:
  656. <Graph.Plot>
  657. Based on:
  658. The Animation class is based in the MooTools Framework <http://mootools.net>. Copyright (c) 2006-2009 Valerio Proietti, <http://mad4milk.net/>. MIT license <http://mootools.net/license.txt>.
  659. */
  660. var Animation = new Class( {
  661. initialize: function(options){
  662. this.setOptions(options);
  663. },
  664. setOptions: function(options){
  665. var opt = {
  666. duration: 2500,
  667. fps: 40,
  668. transition: Trans.Quart.easeInOut,
  669. compute: $.empty,
  670. complete: $.empty,
  671. link: 'ignore'
  672. };
  673. this.opt = $.merge(opt, options || {});
  674. return this;
  675. },
  676. step: function(){
  677. var time = $.time(), opt = this.opt;
  678. if (time < this.time + opt.duration) {
  679. var delta = opt.transition((time - this.time) / opt.duration);
  680. opt.compute(delta);
  681. } else {
  682. this.timer = clearInterval(this.timer);
  683. opt.compute(1);
  684. opt.complete();
  685. }
  686. },
  687. start: function(){
  688. if (!this.check())
  689. return this;
  690. this.time = 0;
  691. this.startTimer();
  692. return this;
  693. },
  694. startTimer: function(){
  695. var that = this, fps = this.opt.fps;
  696. if (this.timer)
  697. return false;
  698. this.time = $.time() - this.time;
  699. this.timer = setInterval((function(){
  700. that.step();
  701. }), Math.round(1000 / fps));
  702. return true;
  703. },
  704. pause: function(){
  705. this.stopTimer();
  706. return this;
  707. },
  708. resume: function(){
  709. this.startTimer();
  710. return this;
  711. },
  712. stopTimer: function(){
  713. if (!this.timer)
  714. return false;
  715. this.time = $.time() - this.time;
  716. this.timer = clearInterval(this.timer);
  717. return true;
  718. },
  719. check: function(){
  720. if (!this.timer)
  721. return true;
  722. if (this.opt.link == 'cancel') {
  723. this.stopTimer();
  724. return true;
  725. }
  726. return false;
  727. }
  728. });
  729. var Options = function() {
  730. var args = arguments;
  731. for(var i=0, l=args.length, ans={}; i<l; i++) {
  732. var opt = Options[args[i]];
  733. if(opt.$extend) {
  734. $.extend(ans, opt);
  735. } else {
  736. ans[args[i]] = opt;
  737. }
  738. }
  739. return ans;
  740. };
  741. /*
  742. * File: Options.AreaChart.js
  743. *
  744. */
  745. /*
  746. Object: Options.AreaChart
  747. <AreaChart> options.
  748. Other options included in the AreaChart are <Options.Canvas>, <Options.Label>, <Options.Margin>, <Options.Tips> and <Options.Events>.
  749. Syntax:
  750. (start code js)
  751. Options.AreaChart = {
  752. animate: true,
  753. labelOffset: 3,
  754. type: 'stacked',
  755. selectOnHover: true,
  756. showAggregates: true,
  757. showLabels: true,
  758. filterOnClick: false,
  759. restoreOnRightClick: false
  760. };
  761. (end code)
  762. Example:
  763. (start code js)
  764. var areaChart = new $jit.AreaChart({
  765. animate: true,
  766. type: 'stacked:gradient',
  767. selectOnHover: true,
  768. filterOnClick: true,
  769. restoreOnRightClick: true
  770. });
  771. (end code)
  772. Parameters:
  773. animate - (boolean) Default's *true*. Whether to add animated transitions when filtering/restoring stacks.
  774. labelOffset - (number) Default's *3*. Adds margin between the label and the default place where it should be drawn.
  775. type - (string) Default's *'stacked'*. Stack style. Posible values are 'stacked', 'stacked:gradient' to add gradients.
  776. selectOnHover - (boolean) Default's *true*. If true, it will add a mark to the hovered stack.
  777. showAggregates - (boolean, function) Default's *true*. Display the values of the stacks. Can also be a function that returns *true* or *false* to display or filter some values. That same function can also return a string with the formatted value.
  778. showLabels - (boolean, function) Default's *true*. Display the name of the slots. Can also be a function that returns *true* or *false* to display or not each label.
  779. filterOnClick - (boolean) Default's *true*. Select the clicked stack by hiding all other stacks.
  780. restoreOnRightClick - (boolean) Default's *true*. Show all stacks by right clicking.
  781. */
  782. Options.AreaChart = {
  783. $extend: true,
  784. animate: true,
  785. labelOffset: 3, // label offset
  786. type: 'stacked', // gradient
  787. Tips: {
  788. enable: false,
  789. onShow: $.empty,
  790. onHide: $.empty
  791. },
  792. Events: {
  793. enable: false,
  794. onClick: $.empty
  795. },
  796. selectOnHover: true,
  797. showAggregates: true,
  798. showLabels: true,
  799. filterOnClick: false,
  800. restoreOnRightClick: false
  801. };
  802. /*
  803. * File: Options.Margin.js
  804. *
  805. */
  806. /*
  807. Object: Options.Margin
  808. Canvas drawing margins.
  809. Syntax:
  810. (start code js)
  811. Options.Margin = {
  812. top: 0,
  813. left: 0,
  814. right: 0,
  815. bottom: 0
  816. };
  817. (end code)
  818. Example:
  819. (start code js)
  820. var viz = new $jit.Viz({
  821. Margin: {
  822. right: 10,
  823. bottom: 20
  824. }
  825. });
  826. (end code)
  827. Parameters:
  828. top - (number) Default's *0*. Top margin.
  829. left - (number) Default's *0*. Left margin.
  830. right - (number) Default's *0*. Right margin.
  831. bottom - (number) Default's *0*. Bottom margin.
  832. */
  833. Options.Margin = {
  834. $extend: false,
  835. top: 0,
  836. left: 0,
  837. right: 0,
  838. bottom: 0
  839. };
  840. /*
  841. * File: Options.Canvas.js
  842. *
  843. */
  844. /*
  845. Object: Options.Canvas
  846. These are Canvas general options, like where to append it in the DOM, its dimensions, background,
  847. and other more advanced options.
  848. Syntax:
  849. (start code js)
  850. Options.Canvas = {
  851. injectInto: 'id',
  852. type: '2D', //'3D'
  853. width: false,
  854. height: false,
  855. useCanvas: false,
  856. withLabels: true,
  857. background: false
  858. };
  859. (end code)
  860. Example:
  861. (start code js)
  862. var viz = new $jit.Viz({
  863. injectInto: 'someContainerId',
  864. width: 500,
  865. height: 700
  866. });
  867. (end code)
  868. Parameters:
  869. injectInto - *required* (string|element) The id of the DOM container for the visualization. It can also be an Element provided that it has an id.
  870. type - (string) Context type. Default's 2D but can be 3D for webGL enabled browsers.
  871. width - (number) Default's to the *container's offsetWidth*. The width of the canvas.
  872. height - (number) Default's to the *container's offsetHeight*. The height of the canvas.
  873. useCanvas - (boolean|object) Default's *false*. You can pass another <Canvas> instance to be used by the visualization.
  874. withLabels - (boolean) Default's *true*. Whether to use a label container for the visualization.
  875. background - (boolean|object) Default's *false*. An object containing information about the rendering of a background canvas.
  876. */
  877. Options.Canvas = {
  878. $extend: true,
  879. injectInto: 'id',
  880. type: '2D',
  881. width: false,
  882. height: false,
  883. useCanvas: false,
  884. withLabels: true,
  885. background: false,
  886. Scene: {
  887. Lighting: {
  888. enable: false,
  889. ambient: [1, 1, 1],
  890. directional: {
  891. direction: { x: -100, y: -100, z: -100 },
  892. color: [0.5, 0.3, 0.1]
  893. }
  894. }
  895. }
  896. };
  897. /*
  898. * File: Options.Tree.js
  899. *
  900. */
  901. /*
  902. Object: Options.Tree
  903. Options related to (strict) Tree layout algorithms. These options are used by the <ST> visualization.
  904. Syntax:
  905. (start code js)
  906. Options.Tree = {
  907. orientation: "left",
  908. subtreeOffset: 8,
  909. siblingOffset: 5,
  910. indent:10,
  911. multitree: false,
  912. align:"center"
  913. };
  914. (end code)
  915. Example:
  916. (start code js)
  917. var st = new $jit.ST({
  918. orientation: 'left',
  919. subtreeOffset: 1,
  920. siblingOFfset: 5,
  921. multitree: true
  922. });
  923. (end code)
  924. Parameters:
  925. subtreeOffset - (number) Default's 8. Separation offset between subtrees.
  926. siblingOffset - (number) Default's 5. Separation offset between siblings.
  927. orientation - (string) Default's 'left'. Tree orientation layout. Possible values are 'left', 'top', 'right', 'bottom'.
  928. align - (string) Default's *center*. Whether the tree alignment is 'left', 'center' or 'right'.
  929. indent - (number) Default's 10. Used when *align* is left or right and shows an indentation between parent and children.
  930. multitree - (boolean) Default's *false*. Used with the node $orn data property for creating multitrees.
  931. */
  932. Options.Tree = {
  933. $extend: true,
  934. orientation: "left",
  935. subtreeOffset: 8,
  936. siblingOffset: 5,
  937. indent:10,
  938. multitree: false,
  939. align:"center"
  940. };
  941. /*
  942. * File: Options.Node.js
  943. *
  944. */
  945. /*
  946. Object: Options.Node
  947. Provides Node rendering options for Tree and Graph based visualizations.
  948. Syntax:
  949. (start code js)
  950. Options.Node = {
  951. overridable: false,
  952. type: 'circle',
  953. color: '#ccb',
  954. alpha: 1,
  955. dim: 3,
  956. height: 20,
  957. width: 90,
  958. autoHeight: false,
  959. autoWidth: false,
  960. lineWidth: 1,
  961. transform: true,
  962. align: "center",
  963. angularWidth:1,
  964. span:1,
  965. CanvasStyles: {}
  966. };
  967. (end code)
  968. Example:
  969. (start code js)
  970. var viz = new $jit.Viz({
  971. Node: {
  972. overridable: true,
  973. width: 30,
  974. autoHeight: true,
  975. type: 'rectangle'
  976. }
  977. });
  978. (end code)
  979. Parameters:
  980. overridable - (boolean) Default's *false*. Determine whether or not general node properties can be overridden by a particular <Graph.Node>.
  981. type - (string) Default's *circle*. Node's shape. Node built-in types include 'circle', 'rectangle', 'square', 'ellipse', 'triangle', 'star'. The default Node type might vary in each visualization. You can also implement (non built-in) custom Node types into your visualizations.
  982. color - (string) Default's *#ccb*. Node color.
  983. alpha - (number) Default's *1*. The Node's alpha value. *1* is for full opacity.
  984. dim - (number) Default's *3*. An extra parameter used by 'circle', 'square', 'triangle' and 'star' node types. Depending on each shape, this parameter can set the radius of a circle, half the length of the side of a square, half the base and half the height of a triangle or the length of a side of a star (concave decagon).
  985. height - (number) Default's *20*. Used by 'rectangle' and 'ellipse' node types. The height of the node shape.
  986. width - (number) Default's *90*. Used by 'rectangle' and 'ellipse' node types. The width of the node shape.
  987. autoHeight - (boolean) Default's *false*. Whether to set an auto height for the node depending on the content of the Node's label.
  988. autoWidth - (boolean) Default's *false*. Whether to set an auto width for the node depending on the content of the Node's label.
  989. lineWidth - (number) Default's *1*. Used only by some Node shapes. The line width of the strokes of a node.
  990. transform - (boolean) Default's *true*. Only used by the <Hypertree> visualization. Whether to scale the nodes according to the moebius transformation.
  991. align - (string) Default's *center*. Possible values are 'center', 'left' or 'right'. Used only by the <ST> visualization, these parameters are used for aligning nodes when some of they dimensions vary.
  992. angularWidth - (number) Default's *1*. Used in radial layouts (like <RGraph> or <Sunburst> visualizations). The amount of relative 'space' set for a node.
  993. span - (number) Default's *1*. Used in radial layouts (like <RGraph> or <Sunburst> visualizations). The angle span amount set for a node.
  994. CanvasStyles - (object) Default's an empty object (i.e. {}). Attach any other canvas specific property that you'd set to the canvas context before plotting a Node.
  995. */
  996. Options.Node = {
  997. $extend: false,
  998. overridable: false,
  999. type: 'circle',
  1000. color: '#fff000', // node Color
  1001. alpha: 1,
  1002. dim: 3,
  1003. height: 20,
  1004. width: 90,
  1005. autoHeight: false,
  1006. autoWidth: false,
  1007. lineWidth: 1,
  1008. transform: true,
  1009. align: "center",
  1010. angularWidth:1,
  1011. span:1,
  1012. //Raw canvas styles to be
  1013. //applied to the context instance
  1014. //before plotting a node
  1015. CanvasStyles: {}
  1016. };
  1017. /*
  1018. * File: Options.Edge.js
  1019. *
  1020. */
  1021. /*
  1022. Object: Options.Edge
  1023. Provides Edge rendering options for Tree and Graph based visualizations.
  1024. Syntax:
  1025. (start code js)
  1026. Options.Edge = {
  1027. overridable: false,
  1028. type: 'line',
  1029. color: '#ccb',
  1030. lineWidth: 1,
  1031. dim:15,
  1032. alpha: 1,
  1033. CanvasStyles: {}
  1034. };
  1035. (end code)
  1036. Example:
  1037. (start code js)
  1038. var viz = new $jit.Viz({
  1039. Edge: {
  1040. overridable: true,
  1041. type: 'line',
  1042. color: '#fff',
  1043. CanvasStyles: {
  1044. shadowColor: '#ccc',
  1045. shadowBlur: 10
  1046. }
  1047. }
  1048. });
  1049. (end code)
  1050. Parameters:
  1051. overridable - (boolean) Default's *false*. Determine whether or not general edges properties can be overridden by a particular <Graph.Adjacence>.
  1052. type - (string) Default's 'line'. Edge styles include 'line', 'hyperline', 'arrow'. The default Edge type might vary in each visualization. You can also implement custom Edge types.
  1053. color - (string) Default's '#ccb'. Edge color.
  1054. lineWidth - (number) Default's *1*. Line/Edge width.
  1055. alpha - (number) Default's *1*. The Edge's alpha value. *1* is for full opacity.
  1056. dim - (number) Default's *15*. An extra parameter used by other complex shapes such as quadratic, bezier or arrow, to determine the shape's diameter.
  1057. epsilon - (number) Default's *7*. Only used when using *enableForEdges* in <Options.Events>. This dimension is used to create an area for the line where the contains method for the edge returns *true*.
  1058. CanvasStyles - (object) Default's an empty object (i.e. {}). Attach any other canvas specific property that you'd set to the canvas context before plotting an Edge.
  1059. See also:
  1060. If you want to know more about how to customize Node/Edge data per element, in the JSON or programmatically, take a look at this article.
  1061. */
  1062. Options.Edge = {
  1063. $extend: false,
  1064. overridable: false,
  1065. type: 'line',
  1066. color: '#ccb',
  1067. lineWidth: 1,
  1068. dim:15,
  1069. alpha: 1,
  1070. epsilon: 7,
  1071. //Raw canvas styles to be
  1072. //applied to the context instance
  1073. //before plotting an edge
  1074. CanvasStyles: {}
  1075. };
  1076. /*
  1077. * File: Options.Fx.js
  1078. *
  1079. */
  1080. /*
  1081. Object: Options.Fx
  1082. Provides animation options like duration of the animations, frames per second and animation transitions.
  1083. Syntax:
  1084. (start code js)
  1085. Options.Fx = {
  1086. fps:40,
  1087. duration: 2500,
  1088. transition: $jit.Trans.Quart.easeInOut,
  1089. clearCanvas: true
  1090. };
  1091. (end code)
  1092. Example:
  1093. (start code js)
  1094. var viz = new $jit.Viz({
  1095. duration: 1000,
  1096. fps: 35,
  1097. transition: $jit.Trans.linear
  1098. });
  1099. (end code)
  1100. Parameters:
  1101. clearCanvas - (boolean) Default's *true*. Whether to clear the frame/canvas when the viz is plotted or animated.
  1102. duration - (number) Default's *2500*. Duration of the animation in milliseconds.
  1103. fps - (number) Default's *40*. Frames per second.
  1104. transition - (object) Default's *$jit.Trans.Quart.easeInOut*. The transition used for the animations. See below for a more detailed explanation.
  1105. Object: $jit.Trans
  1106. This object is used for specifying different animation transitions in all visualizations.
  1107. There are many different type of animation transitions.
  1108. linear:
  1109. Displays a linear transition
  1110. >Trans.linear
  1111. (see Linear.png)
  1112. Quad:
  1113. Displays a Quadratic transition.
  1114. >Trans.Quad.easeIn
  1115. >Trans.Quad.easeOut
  1116. >Trans.Quad.easeInOut
  1117. (see Quad.png)
  1118. Cubic:
  1119. Displays a Cubic transition.
  1120. >Trans.Cubic.easeIn
  1121. >Trans.Cubic.easeOut
  1122. >Trans.Cubic.easeInOut
  1123. (see Cubic.png)
  1124. Quart:
  1125. Displays a Quartetic transition.
  1126. >Trans.Quart.easeIn
  1127. >Trans.Quart.easeOut
  1128. >Trans.Quart.easeInOut
  1129. (see Quart.png)
  1130. Quint:
  1131. Displays a Quintic transition.
  1132. >Trans.Quint.easeIn
  1133. >Trans.Quint.easeOut
  1134. >Trans.Quint.easeInOut
  1135. (see Quint.png)
  1136. Expo:
  1137. Displays an Exponential transition.
  1138. >Trans.Expo.easeIn
  1139. >Trans.Expo.easeOut
  1140. >Trans.Expo.easeInOut
  1141. (see Expo.png)
  1142. Circ:
  1143. Displays a Circular transition.
  1144. >Trans.Circ.easeIn
  1145. >Trans.Circ.easeOut
  1146. >Trans.Circ.easeInOut
  1147. (see Circ.png)
  1148. Sine:
  1149. Displays a Sineousidal transition.
  1150. >Trans.Sine.easeIn
  1151. >Trans.Sine.easeOut
  1152. >Trans.Sine.easeInOut
  1153. (see Sine.png)
  1154. Back:
  1155. >Trans.Back.easeIn
  1156. >Trans.Back.easeOut
  1157. >Trans.Back.easeInOut
  1158. (see Back.png)
  1159. Bounce:
  1160. Bouncy transition.
  1161. >Trans.Bounce.easeIn
  1162. >Trans.Bounce.easeOut
  1163. >Trans.Bounce.easeInOut
  1164. (see Bounce.png)
  1165. Elastic:
  1166. Elastic curve.
  1167. >Trans.Elastic.easeIn
  1168. >Trans.Elastic.easeOut
  1169. >Trans.Elastic.easeInOut
  1170. (see Elastic.png)
  1171. Based on:
  1172. Easing and Transition animation methods are based in the MooTools Framework <http://mootools.net>. Copyright (c) 2006-2010 Valerio Proietti, <http://mad4milk.net/>. MIT license <http://mootools.net/license.txt>.
  1173. */
  1174. Options.Fx = {
  1175. $extend: true,
  1176. fps:40,
  1177. duration: 2500,
  1178. transition: $jit.Trans.Quart.easeInOut,
  1179. clearCanvas: true
  1180. };
  1181. /*
  1182. * File: Options.Label.js
  1183. *
  1184. */
  1185. /*
  1186. Object: Options.Label
  1187. Provides styling for Labels such as font size, family, etc. Also sets Node labels as HTML, SVG or Native canvas elements.
  1188. Syntax:
  1189. (start code js)
  1190. Options.Label = {
  1191. overridable: false,
  1192. type: 'HTML', //'SVG', 'Native'
  1193. style: ' ',
  1194. size: 10,
  1195. family: 'sans-serif',
  1196. textAlign: 'center',
  1197. textBaseline: 'alphabetic',
  1198. color: '#fff'
  1199. };
  1200. (end code)
  1201. Example:
  1202. (start code js)
  1203. var viz = new $jit.Viz({
  1204. Label: {
  1205. type: 'Native',
  1206. size: 11,
  1207. color: '#ccc'
  1208. }
  1209. });
  1210. (end code)
  1211. Parameters:
  1212. overridable - (boolean) Default's *false*. Determine whether or not general label properties can be overridden by a particular <Graph.Node>.
  1213. type - (string) Default's *HTML*. The type for the labels. Can be 'HTML', 'SVG' or 'Native' canvas labels.
  1214. style - (string) Default's *empty string*. Can be 'italic' or 'bold'. This parameter is only taken into account when using 'Native' canvas labels. For DOM based labels the className *node* is added to the DOM element for styling via CSS. You can also use <Options.Controller> methods to style individual labels.
  1215. size - (number) Default's *10*. The font's size. This parameter is only taken into account when using 'Native' canvas labels. For DOM based labels the className *node* is added to the DOM element for styling via CSS. You can also use <Options.Controller> methods to style individual labels.
  1216. family - (string) Default's *sans-serif*. The font's family. This parameter is only taken into account when using 'Native' canvas labels. For DOM based labels the className *node* is added to the DOM element for styling via CSS. You can also use <Options.Controller> methods to style individual labels.
  1217. color - (string) Default's *#fff*. The font's color. This parameter is only taken into account when using 'Native' canvas labels. For DOM based labels the className *node* is added to the DOM element for styling via CSS. You can also use <Options.Controller> methods to style individual labels.
  1218. */
  1219. Options.Label = {
  1220. $extend: false,
  1221. overridable: false,
  1222. type: 'HTML', //'SVG', 'Native'
  1223. style: 'bold',
  1224. size: 12,
  1225. family: ' Courier, Courier New, monospace',
  1226. textAlign: 'center',
  1227. textBaseline: 'alphabetic',
  1228. color: 'white'
  1229. };
  1230. /*
  1231. * File: Options.Tips.js
  1232. *
  1233. */
  1234. /*
  1235. Object: Options.Tips
  1236. Tips options
  1237. Syntax:
  1238. (start code js)
  1239. Options.Tips = {
  1240. enable: false,
  1241. type: 'auto',
  1242. offsetX: 20,
  1243. offsetY: 20,
  1244. onShow: $.empty,
  1245. onHide: $.empty
  1246. };
  1247. (end code)
  1248. Example:
  1249. (start code js)
  1250. var viz = new $jit.Viz({
  1251. Tips: {
  1252. enable: true,
  1253. type: 'Native',
  1254. offsetX: 10,
  1255. offsetY: 10,
  1256. onShow: function(tip, node) {
  1257. tip.innerHTML = node.name;
  1258. }
  1259. }
  1260. });
  1261. (end code)
  1262. Parameters:
  1263. enable - (boolean) Default's *false*. If *true*, a tooltip will be shown when a node is hovered. The tooltip is a div DOM element having "tip" as CSS class.
  1264. type - (string) Default's *auto*. Defines where to attach the MouseEnter/Leave tooltip events. Possible values are 'Native' to attach them to the canvas or 'HTML' to attach them to DOM label elements (if defined). 'auto' sets this property to the value of <Options.Label>'s *type* property.
  1265. offsetX - (number) Default's *20*. An offset added to the current tooltip x-position (which is the same as the current mouse position). Default's 20.
  1266. offsetY - (number) Default's *20*. An offset added to the current tooltip y-position (which is the same as the current mouse position). Default's 20.
  1267. onShow(tip, node) - This callack is used right before displaying a tooltip. The first formal parameter is the tip itself (which is a DivElement). The second parameter may be a <Graph.Node> for graph based visualizations or an object with label, value properties for charts.
  1268. onHide() - This callack is used when hiding a tooltip.
  1269. */
  1270. Options.Tips = {
  1271. $extend: false,
  1272. enable: false,
  1273. type: 'auto',
  1274. offsetX: 20,
  1275. offsetY: 20,
  1276. force: false,
  1277. onShow: $.empty,
  1278. onHide: $.empty
  1279. };
  1280. /*
  1281. * File: Options.NodeStyles.js
  1282. *
  1283. */
  1284. /*
  1285. Object: Options.NodeStyles
  1286. Apply different styles when a node is hovered or selected.
  1287. Syntax:
  1288. (start code js)
  1289. Options.NodeStyles = {
  1290. enable: false,
  1291. type: 'auto',
  1292. stylesHover: false,
  1293. stylesClick: false
  1294. };
  1295. (end code)
  1296. Example:
  1297. (start code js)
  1298. var viz = new $jit.Viz({
  1299. NodeStyles: {
  1300. enable: true,
  1301. type: 'Native',
  1302. stylesHover: {
  1303. dim: 30,
  1304. color: '#fcc'
  1305. },
  1306. duration: 600
  1307. }
  1308. });
  1309. (end code)
  1310. Parameters:
  1311. enable - (boolean) Default's *false*. Whether to enable this option.
  1312. type - (string) Default's *auto*. Use this to attach the hover/click events in the nodes or the nodes labels (if they have been defined as DOM elements: 'HTML' or 'SVG', see <Options.Label> for more details). The default 'auto' value will set NodeStyles to the same type defined for <Options.Label>.
  1313. stylesHover - (boolean|object) Default's *false*. An object with node styles just like the ones defined for <Options.Node> or *false* otherwise.
  1314. stylesClick - (boolean|object) Default's *false*. An object with node styles just like the ones defined for <Options.Node> or *false* otherwise.
  1315. */
  1316. Options.NodeStyles = {
  1317. $extend: false,
  1318. enable: false,
  1319. type: 'auto',
  1320. stylesHover: false,
  1321. stylesClick: false
  1322. };
  1323. /*
  1324. * File: Options.Events.js
  1325. *
  1326. */
  1327. /*
  1328. Object: Options.Events
  1329. Configuration for adding mouse/touch event handlers to Nodes.
  1330. Syntax:
  1331. (start code js)
  1332. Options.Events = {
  1333. enable: false,
  1334. enableForEdges: false,
  1335. type: 'auto',
  1336. onClick: $.empty,
  1337. onRightClick: $.empty,
  1338. onMouseMove: $.empty,
  1339. onMouseEnter: $.empty,
  1340. onMouseLeave: $.empty,
  1341. onDragStart: $.empty,
  1342. onDragMove: $.empty,
  1343. onDragCancel: $.empty,
  1344. onDragEnd: $.empty,
  1345. onTouchStart: $.empty,
  1346. onTouchMove: $.empty,
  1347. onTouchEnd: $.empty,
  1348. onTouchCancel: $.empty,
  1349. onMouseWheel: $.empty
  1350. };
  1351. (end code)
  1352. Example:
  1353. (start code js)
  1354. var viz = new $jit.Viz({
  1355. Events: {
  1356. enable: true,
  1357. onClick: function(node, eventInfo, e) {
  1358. viz.doSomething();
  1359. },
  1360. onMouseEnter: function(node, eventInfo, e) {
  1361. viz.canvas.getElement().style.cursor = 'pointer';
  1362. },
  1363. onMouseLeave: function(node, eventInfo, e) {
  1364. viz.canvas.getElement().style.cursor = '';
  1365. }
  1366. }
  1367. });
  1368. (end code)
  1369. Parameters:
  1370. enable - (boolean) Default's *false*. Whether to enable the Event system.
  1371. enableForEdges - (boolean) Default's *false*. Whether to track events also in arcs. If *true* the same callbacks -described below- are used for nodes *and* edges. A simple duck type check for edges is to check for *node.nodeFrom*.
  1372. type - (string) Default's 'auto'. Whether to attach the events onto the HTML labels (via event delegation) or to use the custom 'Native' canvas Event System of the library. 'auto' is set when you let the <Options.Label> *type* parameter decide this.
  1373. onClick(node, eventInfo, e) - Triggered when a user performs a click in the canvas. *node* is the <Graph.Node> clicked or false if no node has been clicked. *e* is the grabbed event (should return the native event in a cross-browser manner). *eventInfo* is an object containing useful methods like *getPos* to get the mouse position relative to the canvas.
  1374. onRightClick(node, eventInfo, e) - Triggered when a user performs a right click in the canvas. *node* is the <Graph.Node> right clicked or false if no node has been clicked. *e* is the grabbed event (should return the native event in a cross-browser manner). *eventInfo* is an object containing useful methods like *getPos* to get the mouse position relative to the canvas.
  1375. onMouseMove(node, eventInfo, e) - Triggered when the user moves the mouse. *node* is the <Graph.Node> under the cursor as it's moving over the canvas or false if no node has been clicked. *e* is the grabbed event (should return the native event in a cross-browser manner). *eventInfo* is an object containing useful methods like *getPos* to get the mouse position relative to the canvas.
  1376. onMouseEnter(node, eventInfo, e) - Triggered when a user moves the mouse over a node. *node* is the <Graph.Node> that the mouse just entered. *e* is the grabbed event (should return the native event in a cross-browser manner). *eventInfo* is an object containing useful methods like *getPos* to get the mouse position relative to the canvas.
  1377. onMouseLeave(node, eventInfo, e) - Triggered when the user mouse-outs a node. *node* is the <Graph.Node> 'mouse-outed'. *e* is the grabbed event (should return the native event in a cross-browser manner). *eventInfo* is an object containing useful methods like *getPos* to get the mouse position relative to the canvas.
  1378. onDragStart(node, eventInfo, e) - Triggered when the user mouse-downs over a node. *node* is the <Graph.Node> being pressed. *e* is the grabbed event (should return the native event in a cross-browser manner). *eventInfo* is an object containing useful methods like *getPos* to get the mouse position relative to the canvas.
  1379. onDragMove(node, eventInfo, e) - Triggered when a user, after pressing the mouse button over a node, moves the mouse around. *node* is the <Graph.Node> being dragged. *e* is the grabbed event (should return the native event in a cross-browser manner). *eventInfo* is an object containing useful methods like *getPos* to get the mouse position relative to the canvas.
  1380. onDragEnd(node, eventInfo, e) - Triggered when a user finished dragging a node. *node* is the <Graph.Node> being dragged. *e* is the grabbed event (should return the native event in a cross-browser manner). *eventInfo* is an object containing useful methods like *getPos* to get the mouse position relative to the canvas.
  1381. onDragCancel(node, eventInfo, e) - Triggered when the user releases the mouse button over a <Graph.Node> that wasn't dragged (i.e. the user didn't perform any mouse movement after pressing the mouse button). *node* is the <Graph.Node> being dragged. *e* is the grabbed event (should return the native event in a cross-browser manner). *eventInfo* is an object containing useful methods like *getPos* to get the mouse position relative to the canvas.
  1382. onTouchStart(node, eventInfo, e) - Behaves just like onDragStart.
  1383. onTouchMove(node, eventInfo, e) - Behaves just like onDragMove.
  1384. onTouchEnd(node, eventInfo, e) - Behaves just like onDragEnd.
  1385. onTouchCancel(node, eventInfo, e) - Behaves just like onDragCancel.
  1386. onMouseWheel(delta, e) - Triggered when the user uses the mouse scroll over the canvas. *delta* is 1 or -1 depending on the sense of the mouse scroll.
  1387. */
  1388. Options.Events = {
  1389. $extend: false,
  1390. enable: false,
  1391. enableForEdges: false,
  1392. type: 'auto',
  1393. onClick: $.empty,
  1394. onRightClick: $.empty,
  1395. onMouseMove: $.empty,
  1396. onMouseEnter: $.empty,
  1397. onMouseLeave: $.empty,
  1398. onDragStart: $.empty,
  1399. onDragMove: $.empty,
  1400. onDragCancel: $.empty,
  1401. onDragEnd: $.empty,
  1402. onTouchStart: $.empty,
  1403. onTouchMove: $.empty,
  1404. onTouchEnd: $.empty,
  1405. onMouseWheel: $.empty
  1406. };
  1407. /*
  1408. * File: Options.Navigation.js
  1409. *
  1410. */
  1411. /*
  1412. Object: Options.Navigation
  1413. Panning and zooming options for Graph/Tree based visualizations. These options are implemented
  1414. by all visualizations except charts (<AreaChart>, <BarChart> and <PieChart>).
  1415. Syntax:
  1416. (start code js)
  1417. Options.Navigation = {
  1418. enable: false,
  1419. type: 'auto',
  1420. panning: false, //true, 'avoid nodes'
  1421. zooming: false
  1422. };
  1423. (end code)
  1424. Example:
  1425. (start code js)
  1426. var viz = new $jit.Viz({
  1427. Navigation: {
  1428. enable: true,
  1429. panning: 'avoid nodes',
  1430. zooming: 20
  1431. }
  1432. });
  1433. (end code)
  1434. Parameters:
  1435. enable - (boolean) Default's *false*. Whether to enable Navigation capabilities.
  1436. type - (string) Default's 'auto'. Whether to attach the navigation events onto the HTML labels (via event delegation) or to use the custom 'Native' canvas Event System of the library. When 'auto' set when you let the <Options.Label> *type* parameter decide this.
  1437. panning - (boolean|string) Default's *false*. Set this property to *true* if you want to add Drag and Drop panning support to the visualization. You can also set this parameter to 'avoid nodes' to enable DnD panning but disable it if the DnD is taking place over a node. This is useful when some other events like Drag & Drop for nodes are added to <Graph.Nodes>.
  1438. zooming - (boolean|number) Default's *false*. Set this property to a numeric value to turn mouse-scroll zooming on. The number will be proportional to the mouse-scroll sensitivity.
  1439. */
  1440. Options.Navigation = {
  1441. $extend: false,
  1442. enable: false,
  1443. type: 'auto',
  1444. panning: false, //true | 'avoid nodes'
  1445. zooming: false
  1446. };
  1447. /*
  1448. * File: Options.Controller.js
  1449. *
  1450. */
  1451. /*
  1452. Object: Options.Controller
  1453. Provides controller methods. Controller methods are callback functions that get called at different stages
  1454. of the animation, computing or plotting of the visualization.
  1455. Implemented by:
  1456. All visualizations except charts (<AreaChart>, <BarChart> and <PieChart>).
  1457. Syntax:
  1458. (start code js)
  1459. Options.Controller = {
  1460. onBeforeCompute: $.empty,
  1461. onAfterCompute: $.empty,
  1462. onCreateLabel: $.empty,
  1463. onPlaceLabel: $.empty,
  1464. onComplete: $.empty,
  1465. onBeforePlotLine:$.empty,
  1466. onAfterPlotLine: $.empty,
  1467. onBeforePlotNode:$.empty,
  1468. onAfterPlotNode: $.empty,
  1469. request: false
  1470. };
  1471. (end code)
  1472. Example:
  1473. (start code js)
  1474. var viz = new $jit.Viz({
  1475. onBeforePlotNode: function(node) {
  1476. if(node.selected) {
  1477. node.setData('color', '#ffc');
  1478. } else {
  1479. node.removeData('color');
  1480. }
  1481. },
  1482. onBeforePlotLine: function(adj) {
  1483. if(adj.nodeFrom.selected && adj.nodeTo.selected) {
  1484. adj.setData('color', '#ffc');
  1485. } else {
  1486. adj.removeData('color');
  1487. }
  1488. },
  1489. onAfterCompute: function() {
  1490. alert("computed!");
  1491. }
  1492. });
  1493. (end code)
  1494. Parameters:
  1495. onBeforeCompute(node) - This method is called right before performing all computations and animations. The selected <Graph.Node> is passed as parameter.
  1496. onAfterCompute() - This method is triggered after all animations or computations ended.
  1497. onCreateLabel(domElement, node) - This method receives a new label DIV element as first parameter, and the corresponding <Graph.Node> as second parameter. This method will only be called once for each label. This method is useful when adding events or styles to the labels used by the JIT.
  1498. onPlaceLabel(domElement, node) - This method receives a label DIV element as first parameter and the corresponding <Graph.Node> as second parameter. This method is called each time a label has been placed in the visualization, for example at each step of an animation, and thus it allows you to update the labels properties, such as size or position. Note that onPlaceLabel will be triggered after updating the labels positions. That means that, for example, the left and top css properties are already updated to match the nodes positions. Width and height properties are not set however.
  1499. onBeforePlotNode(node) - This method is triggered right before plotting each <Graph.Node>. This method is useful for changing a node style right before plotting it.
  1500. onAfterPlotNode(node) - This method is triggered right after plotting each <Graph.Node>.
  1501. onBeforePlotLine(adj) - This method is triggered right before plotting a <Graph.Adjacence>. This method is useful for adding some styles to a particular edge before being plotted.
  1502. onAfterPlotLine(adj) - This method is triggered right after plotting a <Graph.Adjacence>.
  1503. *Used in <ST>, <TM.Base> and <Icicle> visualizations*
  1504. request(nodeId, level, onComplete) - This method is used for buffering information into the visualization. When clicking on an empty node, the visualization will make a request for this node's subtrees, specifying a given level for this subtree (defined by _levelsToShow_). Once the request is completed, the onComplete callback should be called with the given result. This is useful to provide on-demand information into the visualizations withought having to load the entire information from start. The parameters used by this method are _nodeId_, which is the id of the root of the subtree to request, _level_ which is the depth of the subtree to be requested (0 would mean just the root node). _onComplete_ is an object having the callback method _onComplete.onComplete(json)_ that should be called once the json has been retrieved.
  1505. */
  1506. Options.Controller = {
  1507. $extend: true,
  1508. onBeforeCompute: $.empty,
  1509. onAfterCompute: $.empty,
  1510. onCreateLabel: $.empty,
  1511. onPlaceLabel: $.empty,
  1512. onComplete: $.empty,
  1513. onBeforePlotLine:$.empty,
  1514. onAfterPlotLine: $.empty,
  1515. onBeforePlotNode:$.empty,
  1516. onAfterPlotNode: $.empty,
  1517. request: false
  1518. };
  1519. /*
  1520. * File: Extras.js
  1521. *
  1522. * Provides Extras such as Tips and Style Effects.
  1523. *
  1524. * Description:
  1525. *
  1526. * Provides the <Tips> and <NodeStyles> classes and functions.
  1527. *
  1528. */
  1529. /*
  1530. * Manager for mouse events (clicking and mouse moving).
  1531. *
  1532. * This class is used for registering objects implementing onClick
  1533. * and onMousemove methods. These methods are called when clicking or
  1534. * moving the mouse around the Canvas.
  1535. * For now, <Tips> and <NodeStyles> are classes implementing these methods.
  1536. *
  1537. */
  1538. var ExtrasInitializer = {
  1539. initialize: function(className, viz) {
  1540. this.viz = viz;
  1541. this.canvas = viz.canvas;
  1542. this.config = viz.config[className];
  1543. this.nodeTypes = viz.fx.nodeTypes;
  1544. var type = this.config.type;
  1545. this.dom = type == 'auto'? (viz.config.Label.type != 'Native') : (type != 'Native');
  1546. this.labelContainer = this.dom && viz.labels.getLabelContainer();
  1547. this.isEnabled() && this.initializePost();
  1548. },
  1549. initializePost: $.empty,
  1550. setAsProperty: $.lambda(false),
  1551. isEnabled: function() {
  1552. return this.config.enable;
  1553. },
  1554. isLabel: function(e, win, group) {
  1555. e = $.event.get(e, win);
  1556. var labelContainer = this.labelContainer,
  1557. target = e.target || e.srcElement,
  1558. related = e.relatedTarget;
  1559. if(group) {
  1560. return related && related == this.viz.canvas.getCtx().canvas
  1561. && !!target && this.isDescendantOf(target, labelContainer);
  1562. } else {
  1563. return this.isDescendantOf(target, labelContainer);
  1564. }
  1565. },
  1566. isDescendantOf: function(elem, par) {
  1567. while(elem && elem.parentNode) {
  1568. if(elem.parentNode == par)
  1569. return elem;
  1570. elem = elem.parentNode;
  1571. }
  1572. return false;
  1573. }
  1574. };
  1575. var EventsInterface = {
  1576. onMouseUp: $.empty,
  1577. onMouseDown: $.empty,
  1578. onMouseMove: $.empty,
  1579. onMouseOver: $.empty,
  1580. onMouseOut: $.empty,
  1581. onMouseWheel: $.empty,
  1582. onTouchStart: $.empty,
  1583. onTouchMove: $.empty,
  1584. onTouchEnd: $.empty,
  1585. onTouchCancel: $.empty
  1586. };
  1587. var MouseEventsManager = new Class({
  1588. initialize: function(viz) {
  1589. this.viz = viz;
  1590. this.canvas = viz.canvas;
  1591. this.node = false;
  1592. this.edge = false;
  1593. this.registeredObjects = [];
  1594. this.attachEvents();
  1595. },
  1596. attachEvents: function() {
  1597. var htmlCanvas = this.canvas.getElement(),
  1598. that = this;
  1599. htmlCanvas.oncontextmenu = $.lambda(false);
  1600. $.addEvents(htmlCanvas, {
  1601. 'mouseup': function(e, win) {
  1602. var event = $.event.get(e, win);
  1603. that.handleEvent('MouseUp', e, win,
  1604. that.makeEventObject(e, win),
  1605. $.event.isRightClick(event));
  1606. },
  1607. 'mousedown': function(e, win) {
  1608. var event = $.event.get(e, win);
  1609. that.handleEvent('MouseDown', e, win, that.makeEventObject(e, win),
  1610. $.event.isRightClick(event));
  1611. },
  1612. 'mousemove': function(e, win) {
  1613. that.handleEvent('MouseMove', e, win, that.makeEventObject(e, win));
  1614. },
  1615. 'mouseover': function(e, win) {
  1616. that.handleEvent('MouseOver', e, win, that.makeEventObject(e, win));
  1617. },
  1618. 'mouseout': function(e, win) {
  1619. that.handleEvent('MouseOut', e, win, that.makeEventObject(e, win));
  1620. },
  1621. 'touchstart': function(e, win) {
  1622. that.handleEvent('TouchStart', e, win, that.makeEventObject(e, win));
  1623. },
  1624. 'touchmove': function(e, win) {
  1625. that.handleEvent('TouchMove', e, win, that.makeEventObject(e, win));
  1626. },
  1627. 'touchend': function(e, win) {
  1628. that.handleEvent('TouchEnd', e, win, that.makeEventObject(e, win));
  1629. }
  1630. });
  1631. //attach mousewheel event
  1632. var handleMouseWheel = function(e, win) {
  1633. var event = $.event.get(e, win);
  1634. var wheel = $.event.getWheel(event);
  1635. that.handleEvent('MouseWheel', e, win, wheel);
  1636. };
  1637. //this is a horrible check for non-gecko browsers!
  1638. if(!document.getBoxObjectFor && window.mozInnerScreenX == null) {
  1639. $.addEvent(htmlCanvas, 'mousewheel', handleMouseWheel);
  1640. } else {
  1641. htmlCanvas.addEventListener('DOMMouseScroll', handleMouseWheel, false);
  1642. }
  1643. },
  1644. register: function(obj) {
  1645. this.registeredObjects.push(obj);
  1646. },
  1647. handleEvent: function() {
  1648. var args = Array.prototype.slice.call(arguments),
  1649. type = args.shift();
  1650. for(var i=0, regs=this.registeredObjects, l=regs.length; i<l; i++) {
  1651. regs[i]['on' + type].apply(regs[i], args);
  1652. }
  1653. },
  1654. makeEventObject: function(e, win) {
  1655. var that = this,
  1656. graph = this.viz.graph,
  1657. fx = this.viz.fx,
  1658. ntypes = fx.nodeTypes,
  1659. etypes = fx.edgeTypes;
  1660. return {
  1661. pos: false,
  1662. node: false,
  1663. edge: false,
  1664. contains: false,
  1665. getNodeCalled: false,
  1666. getEdgeCalled: false,
  1667. getPos: function() {
  1668. //check why this can't be cache anymore when using edge detection
  1669. //if(this.pos) return this.pos;
  1670. var canvas = that.viz.canvas,
  1671. s = canvas.getSize(),
  1672. p = canvas.getPos(),
  1673. ox = canvas.translateOffsetX,
  1674. oy = canvas.translateOffsetY,
  1675. sx = canvas.scaleOffsetX,
  1676. sy = canvas.scaleOffsetY,
  1677. pos = $.event.getPos(e, win);
  1678. this.pos = {
  1679. x: (pos.x - p.x - s.width/2 - ox) * 1/sx,
  1680. y: (pos.y - p.y - s.height/2 - oy) * 1/sy
  1681. };
  1682. return this.pos;
  1683. },
  1684. getNode: function() {
  1685. if(this.getNodeCalled) return this.node;
  1686. this.getNodeCalled = true;
  1687. for(var id in graph.nodes) {
  1688. var n = graph.nodes[id],
  1689. geom = n && ntypes[n.getData('type')],
  1690. contains = geom && geom.contains && geom.contains.call(fx, n, this.getPos());
  1691. if(contains) {
  1692. this.contains = contains;
  1693. return that.node = this.node = n;
  1694. }
  1695. }
  1696. return that.node = this.node = false;
  1697. },
  1698. getEdge: function() {
  1699. if(this.getEdgeCalled) return this.edge;
  1700. this.getEdgeCalled = true;
  1701. var hashset = {};
  1702. for(var id in graph.edges) {
  1703. var edgeFrom = graph.edges[id];
  1704. hashset[id] = true;
  1705. for(var edgeId in edgeFrom) {
  1706. if(edgeId in hashset) continue;
  1707. var e = edgeFrom[edgeId],
  1708. geom = e && etypes[e.getData('type')],
  1709. contains = geom && geom.contains && geom.contains.call(fx, e, this.getPos());
  1710. if(contains) {
  1711. this.contains = contains;
  1712. return that.edge = this.edge = e;
  1713. }
  1714. }
  1715. }
  1716. return that.edge = this.edge = false;
  1717. },
  1718. getContains: function() {
  1719. if(this.getNodeCalled) return this.contains;
  1720. this.getNode();
  1721. return this.contains;
  1722. }
  1723. };
  1724. }
  1725. });
  1726. /*
  1727. * Provides the initialization function for <NodeStyles> and <Tips> implemented
  1728. * by all main visualizations.
  1729. *
  1730. */
  1731. var Extras = {
  1732. initializeExtras: function() {
  1733. var mem = new MouseEventsManager(this), that = this;
  1734. $.each(['NodeStyles', 'Tips', 'Navigation', 'Events'], function(k) {
  1735. var obj = new Extras.Classes[k](k, that);
  1736. if(obj.isEnabled()) {
  1737. mem.register(obj);
  1738. }
  1739. if(obj.setAsProperty()) {
  1740. that[k.toLowerCase()] = obj;
  1741. }
  1742. });
  1743. }
  1744. };
  1745. Extras.Classes = {};
  1746. /*
  1747. Class: Events
  1748. This class defines an Event API to be accessed by the user.
  1749. The methods implemented are the ones defined in the <Options.Events> object.
  1750. */
  1751. Extras.Classes.Events = new Class({
  1752. Implements: [ExtrasInitializer, EventsInterface],
  1753. initializePost: function() {
  1754. this.fx = this.viz.fx;
  1755. this.ntypes = this.viz.fx.nodeTypes;
  1756. this.etypes = this.viz.fx.edgeTypes;
  1757. this.hovered = false;
  1758. this.pressed = false;
  1759. this.touched = false;
  1760. this.touchMoved = false;
  1761. this.moved = false;
  1762. },
  1763. setAsProperty: $.lambda(true),
  1764. onMouseUp: function(e, win, event, isRightClick) {
  1765. var evt = $.event.get(e, win);
  1766. if(!this.moved) {
  1767. if(isRightClick) {
  1768. this.config.onRightClick(this.hovered, event, evt);
  1769. } else {
  1770. this.config.onClick(this.pressed, event, evt);
  1771. }
  1772. }
  1773. if(this.pressed) {
  1774. if(this.moved) {
  1775. this.config.onDragEnd(this.pressed, event, evt);
  1776. } else {
  1777. this.config.onDragCancel(this.pressed, event, evt);
  1778. }
  1779. this.pressed = this.moved = false;
  1780. }
  1781. },
  1782. onMouseOut: function(e, win, event) {
  1783. //mouseout a label
  1784. var evt = $.event.get(e, win), label;
  1785. if(this.dom && (label = this.isLabel(e, win, true))) {
  1786. this.config.onMouseLeave(this.viz.graph.getNode(label.id),
  1787. event, evt);
  1788. this.hovered = false;
  1789. return;
  1790. }
  1791. //mouseout canvas
  1792. var rt = evt.relatedTarget,
  1793. canvasWidget = this.canvas.getElement();
  1794. while(rt && rt.parentNode) {
  1795. if(canvasWidget == rt.parentNode) return;
  1796. rt = rt.parentNode;
  1797. }
  1798. if(this.hovered) {
  1799. this.config.onMouseLeave(this.hovered,
  1800. event, evt);
  1801. this.hovered = false;
  1802. }
  1803. },
  1804. onMouseOver: function(e, win, event) {
  1805. //mouseover a label
  1806. var evt = $.event.get(e, win), label;
  1807. if(this.dom && (label = this.isLabel(e, win, true))) {
  1808. this.hovered = this.viz.graph.getNode(label.id);
  1809. this.config.onMouseEnter(this.hovered,
  1810. event, evt);
  1811. }
  1812. },
  1813. onMouseMove: function(e, win, event) {
  1814. var label, evt = $.event.get(e, win);
  1815. if(this.pressed) {
  1816. this.moved = true;
  1817. this.config.onDragMove(this.pressed, event, evt);
  1818. return;
  1819. }
  1820. if(this.dom) {
  1821. this.config.onMouseMove(this.hovered,
  1822. event, evt);
  1823. } else {
  1824. if(this.hovered) {
  1825. var hn = this.hovered;
  1826. var geom = hn.nodeFrom? this.etypes[hn.getData('type')] : this.ntypes[hn.getData('type')];
  1827. var contains = geom && geom.contains
  1828. && geom.contains.call(this.fx, hn, event.getPos());
  1829. if(contains) {
  1830. this.config.onMouseMove(hn, event, evt);
  1831. return;
  1832. } else {
  1833. this.config.onMouseLeave(hn, event, evt);
  1834. this.hovered = false;
  1835. }
  1836. }
  1837. if(this.hovered = (event.getNode() || (this.config.enableForEdges && event.getEdge()))) {
  1838. this.config.onMouseEnter(this.hovered, event, evt);
  1839. } else {
  1840. this.config.onMouseMove(false, event, evt);
  1841. }
  1842. }
  1843. },
  1844. onMouseWheel: function(e, win, delta) {
  1845. this.config.onMouseWheel(delta, $.event.get(e, win));
  1846. },
  1847. onMouseDown: function(e, win, event) {
  1848. var evt = $.event.get(e, win), label;
  1849. if(this.dom) {
  1850. if(label = this.isLabel(e, win)) {
  1851. this.pressed = this.viz.graph.getNode(label.id);
  1852. }
  1853. } else {
  1854. this.pressed = event.getNode() || (this.config.enableForEdges && event.getEdge());
  1855. }
  1856. this.pressed && this.config.onDragStart(this.pressed, event, evt);
  1857. },
  1858. onTouchStart: function(e, win, event) {
  1859. var evt = $.event.get(e, win), label;
  1860. if(this.dom && (label = this.isLabel(e, win))) {
  1861. this.touched = this.viz.graph.getNode(label.id);
  1862. } else {
  1863. this.touched = event.getNode() || (this.config.enableForEdges && event.getEdge());
  1864. }
  1865. this.touched && this.config.onTouchStart(this.touched, event, evt);
  1866. },
  1867. onTouchMove: function(e, win, event) {
  1868. var evt = $.event.get(e, win);
  1869. if(this.touched) {
  1870. this.touchMoved = true;
  1871. this.config.onTouchMove(this.touched, event, evt);
  1872. }
  1873. },
  1874. onTouchEnd: function(e, win, event) {
  1875. var evt = $.event.get(e, win);
  1876. if(this.touched) {
  1877. if(this.touchMoved) {
  1878. this.config.onTouchEnd(this.touched, event, evt);
  1879. } else {
  1880. this.config.onTouchCancel(this.touched, event, evt);
  1881. }
  1882. this.touched = this.touchMoved = false;
  1883. }
  1884. }
  1885. });
  1886. /*
  1887. Class: Tips
  1888. A class containing tip related functions. This class is used internally.
  1889. Used by:
  1890. <ST>, <Sunburst>, <Hypertree>, <RGraph>, <TM>, <ForceDirected>, <Icicle>
  1891. See also:
  1892. <Options.Tips>
  1893. */
  1894. Extras.Classes.Tips = new Class({
  1895. Implements: [ExtrasInitializer, EventsInterface],
  1896. initializePost: function() {
  1897. //add DOM tooltip
  1898. if(document.body) {
  1899. var tip = $('_tooltip') || document.createElement('div');
  1900. tip.id = '_tooltip';
  1901. tip.className = 'tip';
  1902. $.extend(tip.style, {
  1903. position: 'absolute',
  1904. display: 'none',
  1905. zIndex: 13000
  1906. });
  1907. document.body.appendChild(tip);
  1908. this.tip = tip;
  1909. this.node = false;
  1910. }
  1911. },
  1912. setAsProperty: $.lambda(true),
  1913. onMouseOut: function(e, win) {
  1914. //mouseout a label
  1915. var evt = $.event.get(e, win);
  1916. if(this.dom && this.isLabel(e, win, true)) {
  1917. this.hide(true);
  1918. return;
  1919. }
  1920. //mouseout canvas
  1921. var rt = e.relatedTarget,
  1922. canvasWidget = this.canvas.getElement();
  1923. while(rt && rt.parentNode) {
  1924. if(canvasWidget == rt.parentNode) return;
  1925. rt = rt.parentNode;
  1926. }
  1927. this.hide(false);
  1928. },
  1929. onMouseOver: function(e, win) {
  1930. //mouseover a label
  1931. var label;
  1932. if(this.dom && (label = this.isLabel(e, win, false))) {
  1933. this.node = this.viz.graph.getNode(label.id);
  1934. this.config.onShow(this.tip, this.node, label);
  1935. }
  1936. },
  1937. onMouseMove: function(e, win, opt) {
  1938. if(this.dom && this.isLabel(e, win)) {
  1939. this.setTooltipPosition($.event.getPos(e, win));
  1940. }
  1941. if(!this.dom) {
  1942. var node = opt.getNode();
  1943. if(!node) {
  1944. this.hide(true);
  1945. return;
  1946. }
  1947. if(this.config.force || !this.node || this.node.id != node.id) {
  1948. this.node = node;
  1949. this.config.onShow(this.tip, node, opt.getContains());
  1950. }
  1951. this.setTooltipPosition($.event.getPos(e, win));
  1952. }
  1953. },
  1954. setTooltipPosition: function(pos) {
  1955. var tip = this.tip,
  1956. style = tip.style,
  1957. cont = this.config;
  1958. style.display = '';
  1959. //get window dimensions
  1960. var win = {
  1961. 'height': document.body.clientHeight,
  1962. 'width': document.body.clientWidth
  1963. };
  1964. //get tooltip dimensions
  1965. var obj = {
  1966. 'width': tip.offsetWidth,
  1967. 'height': tip.offsetHeight
  1968. };
  1969. //set tooltip position
  1970. var x = cont.offsetX, y = cont.offsetY;
  1971. style.top = ((pos.y + y + obj.height > win.height)?
  1972. (pos.y - obj.height - y) : pos.y + y) + 'px';
  1973. style.left = ((pos.x + obj.width + x > win.width)?
  1974. (pos.x - obj.width - x) : pos.x + x) + 'px';
  1975. },
  1976. hide: function(triggerCallback) {
  1977. this.tip.style.display = 'none';
  1978. triggerCallback && this.config.onHide();
  1979. }
  1980. });
  1981. /*
  1982. Class: NodeStyles
  1983. Change node styles when clicking or hovering a node. This class is used internally.
  1984. Used by:
  1985. <ST>, <Sunburst>, <Hypertree>, <RGraph>, <TM>, <ForceDirected>, <Icicle>
  1986. See also:
  1987. <Options.NodeStyles>
  1988. */
  1989. Extras.Classes.NodeStyles = new Class({
  1990. Implements: [ExtrasInitializer, EventsInterface],
  1991. initializePost: function() {
  1992. this.fx = this.viz.fx;
  1993. this.types = this.viz.fx.nodeTypes;
  1994. this.nStyles = this.config;
  1995. this.nodeStylesOnHover = this.nStyles.stylesHover;
  1996. this.nodeStylesOnClick = this.nStyles.stylesClick;
  1997. this.hoveredNode = false;
  1998. this.fx.nodeFxAnimation = new Animation();
  1999. this.down = false;
  2000. this.move = false;
  2001. },
  2002. onMouseOut: function(e, win) {
  2003. this.down = this.move = false;
  2004. if(!this.hoveredNode) return;
  2005. //mouseout a label
  2006. if(this.dom && this.isLabel(e, win, true)) {
  2007. this.toggleStylesOnHover(this.hoveredNode, false);
  2008. }
  2009. //mouseout canvas
  2010. var rt = e.relatedTarget,
  2011. canvasWidget = this.canvas.getElement();
  2012. while(rt && rt.parentNode) {
  2013. if(canvasWidget == rt.parentNode) return;
  2014. rt = rt.parentNode;
  2015. }
  2016. this.toggleStylesOnHover(this.hoveredNode, false);
  2017. this.hoveredNode = false;
  2018. },
  2019. onMouseOver: function(e, win) {
  2020. //mouseover a label
  2021. var label;
  2022. if(this.dom && (label = this.isLabel(e, win, true))) {
  2023. var node = this.viz.graph.getNode(label.id);
  2024. if(node.selected) return;
  2025. this.hoveredNode = node;
  2026. this.toggleStylesOnHover(this.hoveredNode, true);
  2027. }
  2028. },
  2029. onMouseDown: function(e, win, event, isRightClick) {
  2030. if(isRightClick) return;
  2031. var label;
  2032. if(this.dom && (label = this.isLabel(e, win))) {
  2033. this.down = this.viz.graph.getNode(label.id);
  2034. } else if(!this.dom) {
  2035. this.down = event.getNode();
  2036. }
  2037. this.move = false;
  2038. },
  2039. onMouseUp: function(e, win, event, isRightClick) {
  2040. if(isRightClick) return;
  2041. if(!this.move) {
  2042. this.onClick(event.getNode());
  2043. }
  2044. this.down = this.move = false;
  2045. },
  2046. getRestoredStyles: function(node, type) {
  2047. var restoredStyles = {},
  2048. nStyles = this['nodeStylesOn' + type];
  2049. for(var prop in nStyles) {
  2050. restoredStyles[prop] = node.styles['$' + prop];
  2051. }
  2052. return restoredStyles;
  2053. },
  2054. toggleStylesOnHover: function(node, set) {
  2055. if(this.nodeStylesOnHover) {
  2056. this.toggleStylesOn('Hover', node, set);
  2057. }
  2058. },
  2059. toggleStylesOnClick: function(node, set) {
  2060. if(this.nodeStylesOnClick) {
  2061. this.toggleStylesOn('Click', node, set);
  2062. }
  2063. },
  2064. toggleStylesOn: function(type, node, set) {
  2065. var viz = this.viz;
  2066. var nStyles = this.nStyles;
  2067. if(set) {
  2068. var that = this;
  2069. if(!node.styles) {
  2070. node.styles = $.merge(node.data, {});
  2071. }
  2072. for(var s in this['nodeStylesOn' + type]) {
  2073. var $s = '$' + s;
  2074. if(!($s in node.styles)) {
  2075. node.styles[$s] = node.getData(s);
  2076. }
  2077. }
  2078. viz.fx.nodeFx($.extend({
  2079. 'elements': {
  2080. 'id': node.id,
  2081. 'properties': that['nodeStylesOn' + type]
  2082. },
  2083. transition: Trans.Quart.easeOut,
  2084. duration:300,
  2085. fps:40
  2086. }, this.config));
  2087. } else {
  2088. var restoredStyles = this.getRestoredStyles(node, type);
  2089. viz.fx.nodeFx($.extend({
  2090. 'elements': {
  2091. 'id': node.id,
  2092. 'properties': restoredStyles
  2093. },
  2094. transition: Trans.Quart.easeOut,
  2095. duration:300,
  2096. fps:40
  2097. }, this.config));
  2098. }
  2099. },
  2100. onClick: function(node) {
  2101. if(!node) return;
  2102. var nStyles = this.nodeStylesOnClick;
  2103. if(!nStyles) return;
  2104. //if the node is selected then unselect it
  2105. if(node.selected) {
  2106. this.toggleStylesOnClick(node, false);
  2107. delete node.selected;
  2108. } else {
  2109. //unselect all selected nodes...
  2110. this.viz.graph.eachNode(function(n) {
  2111. if(n.selected) {
  2112. for(var s in nStyles) {
  2113. n.setData(s, n.styles['$' + s], 'end');
  2114. }
  2115. delete n.selected;
  2116. }
  2117. });
  2118. //select clicked node
  2119. this.toggleStylesOnClick(node, true);
  2120. node.selected = true;
  2121. delete node.hovered;
  2122. this.hoveredNode = false;
  2123. }
  2124. },
  2125. onMouseMove: function(e, win, event) {
  2126. //if mouse button is down and moving set move=true
  2127. if(this.down) this.move = true;
  2128. //already handled by mouseover/out
  2129. if(this.dom && this.isLabel(e, win)) return;
  2130. var nStyles = this.nodeStylesOnHover;
  2131. if(!nStyles) return;
  2132. if(!this.dom) {
  2133. if(this.hoveredNode) {
  2134. var geom = this.types[this.hoveredNode.getData('type')];
  2135. var contains = geom && geom.contains && geom.contains.call(this.fx,
  2136. this.hoveredNode, event.getPos());
  2137. if(contains) return;
  2138. }
  2139. var node = event.getNode();
  2140. //if no node is being hovered then just exit
  2141. if(!this.hoveredNode && !node) return;
  2142. //if the node is hovered then exit
  2143. if(node.hovered) return;
  2144. //select hovered node
  2145. if(node && !node.selected) {
  2146. //check if an animation is running and exit it
  2147. this.fx.nodeFxAnimation.stopTimer();
  2148. //unselect all hovered nodes...
  2149. this.viz.graph.eachNode(function(n) {
  2150. if(n.hovered && !n.selected) {
  2151. for(var s in nStyles) {
  2152. n.setData(s, n.styles['$' + s], 'end');
  2153. }
  2154. delete n.hovered;
  2155. }
  2156. });
  2157. //select hovered node
  2158. node.hovered = true;
  2159. this.hoveredNode = node;
  2160. this.toggleStylesOnHover(node, true);
  2161. } else if(this.hoveredNode && !this.hoveredNode.selected) {
  2162. //check if an animation is running and exit it
  2163. this.fx.nodeFxAnimation.stopTimer();
  2164. //unselect hovered node
  2165. this.toggleStylesOnHover(this.hoveredNode, false);
  2166. delete this.hoveredNode.hovered;
  2167. this.hoveredNode = false;
  2168. }
  2169. }
  2170. }
  2171. });
  2172. Extras.Classes.Navigation = new Class({
  2173. Implements: [ExtrasInitializer, EventsInterface],
  2174. initializePost: function() {
  2175. this.pos = false;
  2176. this.pressed = false;
  2177. },
  2178. onMouseWheel: function(e, win, scroll) {
  2179. if(!this.config.zooming) return;
  2180. $.event.stop($.event.get(e, win));
  2181. var val = this.config.zooming / 1000,
  2182. ans = 1 + scroll * val;
  2183. this.canvas.scale(ans, ans);
  2184. },
  2185. onMouseDown: function(e, win, eventInfo) {
  2186. if(!this.config.panning) return;
  2187. if(this.config.panning == 'avoid nodes' && (this.dom? this.isLabel(e, win) : eventInfo.getNode())) return;
  2188. this.pressed = true;
  2189. this.pos = eventInfo.getPos();
  2190. var canvas = this.canvas,
  2191. ox = canvas.translateOffsetX,
  2192. oy = canvas.translateOffsetY,
  2193. sx = canvas.scaleOffsetX,
  2194. sy = canvas.scaleOffsetY;
  2195. this.pos.x *= sx;
  2196. this.pos.x += ox;
  2197. this.pos.y *= sy;
  2198. this.pos.y += oy;
  2199. },
  2200. onMouseMove: function(e, win, eventInfo) {
  2201. if(!this.config.panning) return;
  2202. if(!this.pressed) return;
  2203. if(this.config.panning == 'avoid nodes' && (this.dom? this.isLabel(e, win) : eventInfo.getNode())) return;
  2204. var thispos = this.pos,
  2205. currentPos = eventInfo.getPos(),
  2206. canvas = this.canvas,
  2207. ox = canvas.translateOffsetX,
  2208. oy = canvas.translateOffsetY,
  2209. sx = canvas.scaleOffsetX,
  2210. sy = canvas.scaleOffsetY;
  2211. currentPos.x *= sx;
  2212. currentPos.y *= sy;
  2213. currentPos.x += ox;
  2214. currentPos.y += oy;
  2215. var x = currentPos.x - thispos.x,
  2216. y = currentPos.y - thispos.y;
  2217. this.pos = currentPos;
  2218. this.canvas.translate(x * 1/sx, y * 1/sy);
  2219. },
  2220. onMouseUp: function(e, win, eventInfo, isRightClick) {
  2221. if(!this.config.panning) return;
  2222. this.pressed = false;
  2223. }
  2224. });
  2225. /*
  2226. * File: Canvas.js
  2227. *
  2228. */
  2229. /*
  2230. Class: Canvas
  2231. A canvas widget used by all visualizations. The canvas object can be accessed by doing *viz.canvas*. If you want to
  2232. know more about <Canvas> options take a look at <Options.Canvas>.
  2233. A canvas widget is a set of DOM elements that wrap the native canvas DOM Element providing a consistent API and behavior
  2234. across all browsers. It can also include Elements to add DOM (SVG or HTML) label support to all visualizations.
  2235. Example:
  2236. Suppose we have this HTML
  2237. (start code xml)
  2238. <div id="infovis"></div>
  2239. (end code)
  2240. Now we create a new Visualization
  2241. (start code js)
  2242. var viz = new $jit.Viz({
  2243. //Where to inject the canvas. Any div container will do.
  2244. 'injectInto':'infovis',
  2245. //width and height for canvas.
  2246. //Default's to the container offsetWidth and Height.
  2247. 'width': 900,
  2248. 'height':500
  2249. });
  2250. (end code)
  2251. The generated HTML will look like this
  2252. (start code xml)
  2253. <div id="infovis">
  2254. <div id="infovis-canvaswidget" style="position:relative;">
  2255. <canvas id="infovis-canvas" width=900 height=500
  2256. style="position:absolute; top:0; left:0; width:900px; height:500px;" />
  2257. <div id="infovis-label"
  2258. style="overflow:visible; position:absolute; top:0; left:0; width:900px; height:0px">
  2259. </div>
  2260. </div>
  2261. </div>
  2262. (end code)
  2263. As you can see, the generated HTML consists of a canvas DOM Element of id *infovis-canvas* and a div label container
  2264. of id *infovis-label*, wrapped in a main div container of id *infovis-canvaswidget*.
  2265. */
  2266. var Canvas;
  2267. (function() {
  2268. //check for native canvas support
  2269. var canvasType = typeof HTMLCanvasElement,
  2270. supportsCanvas = (canvasType == 'object' || canvasType == 'function');
  2271. //create element function
  2272. function $E(tag, props) {
  2273. var elem = document.createElement(tag);
  2274. for(var p in props) {
  2275. if(typeof props[p] == "object") {
  2276. $.extend(elem[p], props[p]);
  2277. } else {
  2278. elem[p] = props[p];
  2279. }
  2280. }
  2281. if (tag == "canvas" && !supportsCanvas && G_vmlCanvasManager) {
  2282. elem = G_vmlCanvasManager.initElement(document.body.appendChild(elem));
  2283. }
  2284. return elem;
  2285. }
  2286. //canvas widget which we will call just Canvas
  2287. $jit.Canvas = Canvas = new Class({
  2288. canvases: [],
  2289. pos: false,
  2290. element: false,
  2291. labelContainer: false,
  2292. translateOffsetX: 0,
  2293. translateOffsetY: 0,
  2294. scaleOffsetX: 1,
  2295. scaleOffsetY: 1,
  2296. initialize: function(viz, opt) {
  2297. this.viz = viz;
  2298. this.opt = this.config = opt;
  2299. var id = $.type(opt.injectInto) == 'string'?
  2300. opt.injectInto:opt.injectInto.id,
  2301. type = opt.type,
  2302. idLabel = id + "-label",
  2303. wrapper = $(id),
  2304. width = opt.width || wrapper.offsetWidth,
  2305. height = opt.height || wrapper.offsetHeight;
  2306. this.id = id;
  2307. //canvas options
  2308. var canvasOptions = {
  2309. injectInto: id,
  2310. width: width,
  2311. height: height
  2312. };
  2313. //create main wrapper
  2314. this.element = $E('div', {
  2315. 'id': id + '-canvaswidget',
  2316. 'style': {
  2317. 'position': 'relative',
  2318. 'width': width + 'px',
  2319. 'height': height + 'px'
  2320. }
  2321. });
  2322. //create label container
  2323. this.labelContainer = this.createLabelContainer(opt.Label.type,
  2324. idLabel, canvasOptions);
  2325. //create primary canvas
  2326. this.canvases.push(new Canvas.Base[type]({
  2327. config: $.extend({idSuffix: '-canvas'}, canvasOptions),
  2328. plot: function(base) {
  2329. viz.fx.plot();
  2330. },
  2331. resize: function() {
  2332. viz.refresh();
  2333. }
  2334. }));
  2335. //create secondary canvas
  2336. var back = opt.background;
  2337. if(back) {
  2338. var backCanvas = new Canvas.Background[back.type](viz, $.extend(back, canvasOptions));
  2339. this.canvases.push(new Canvas.Base[type](backCanvas));
  2340. }
  2341. //insert canvases
  2342. var len = this.canvases.length;
  2343. while(len--) {
  2344. this.element.appendChild(this.canvases[len].canvas);
  2345. if(len > 0) {
  2346. this.canvases[len].plot();
  2347. }
  2348. }
  2349. this.element.appendChild(this.labelContainer);
  2350. wrapper.appendChild(this.element);
  2351. //Update canvas position when the page is scrolled.
  2352. var timer = null, that = this;
  2353. $.addEvent(window, 'scroll', function() {
  2354. clearTimeout(timer);
  2355. timer = setTimeout(function() {
  2356. that.getPos(true); //update canvas position
  2357. }, 500);
  2358. });
  2359. },
  2360. /*
  2361. Method: getCtx
  2362. Returns the main canvas context object
  2363. Example:
  2364. (start code js)
  2365. var ctx = canvas.getCtx();
  2366. //Now I can use the native canvas context
  2367. //and for example change some canvas styles
  2368. ctx.globalAlpha = 1;
  2369. (end code)
  2370. */
  2371. getCtx: function(i) {
  2372. return this.canvases[i || 0].getCtx();
  2373. },
  2374. /*
  2375. Method: getConfig
  2376. Returns the current Configuration for this Canvas Widget.
  2377. Example:
  2378. (start code js)
  2379. var config = canvas.getConfig();
  2380. (end code)
  2381. */
  2382. getConfig: function() {
  2383. return this.opt;
  2384. },
  2385. /*
  2386. Method: getElement
  2387. Returns the main Canvas DOM wrapper
  2388. Example:
  2389. (start code js)
  2390. var wrapper = canvas.getElement();
  2391. //Returns <div id="infovis-canvaswidget" ... >...</div> as element
  2392. (end code)
  2393. */
  2394. getElement: function() {
  2395. return this.element;
  2396. },
  2397. /*
  2398. Method: getSize
  2399. Returns canvas dimensions.
  2400. Returns:
  2401. An object with *width* and *height* properties.
  2402. Example:
  2403. (start code js)
  2404. canvas.getSize(); //returns { width: 900, height: 500 }
  2405. (end code)
  2406. */
  2407. getSize: function(i) {
  2408. return this.canvases[i || 0].getSize();
  2409. },
  2410. /*
  2411. Method: resize
  2412. Resizes the canvas.
  2413. Parameters:
  2414. width - New canvas width.
  2415. height - New canvas height.
  2416. Example:
  2417. (start code js)
  2418. canvas.resize(width, height);
  2419. (end code)
  2420. */
  2421. resize: function(width, height) {
  2422. this.getPos(true);
  2423. this.translateOffsetX = this.translateOffsetY = 0;
  2424. this.scaleOffsetX = this.scaleOffsetY = 1;
  2425. for(var i=0, l=this.canvases.length; i<l; i++) {
  2426. this.canvases[i].resize(width, height);
  2427. }
  2428. var style = this.element.style;
  2429. style.width = width + 'px';
  2430. style.height = height + 'px';
  2431. if(this.labelContainer)
  2432. this.labelContainer.style.width = width + 'px';
  2433. },
  2434. /*
  2435. Method: translate
  2436. Applies a translation to the canvas.
  2437. Parameters:
  2438. x - (number) x offset.
  2439. y - (number) y offset.
  2440. disablePlot - (boolean) Default's *false*. Set this to *true* if you don't want to refresh the visualization.
  2441. Example:
  2442. (start code js)
  2443. canvas.translate(30, 30);
  2444. (end code)
  2445. */
  2446. translate: function(x, y, disablePlot) {
  2447. this.translateOffsetX += x*this.scaleOffsetX;
  2448. this.translateOffsetY += y*this.scaleOffsetY;
  2449. for(var i=0, l=this.canvases.length; i<l; i++) {
  2450. this.canvases[i].translate(x, y, disablePlot);
  2451. }
  2452. },
  2453. /*
  2454. Method: scale
  2455. Scales the canvas.
  2456. Parameters:
  2457. x - (number) scale value.
  2458. y - (number) scale value.
  2459. disablePlot - (boolean) Default's *false*. Set this to *true* if you don't want to refresh the visualization.
  2460. Example:
  2461. (start code js)
  2462. canvas.scale(0.5, 0.5);
  2463. (end code)
  2464. */
  2465. scale: function(x, y, disablePlot) {
  2466. var px = this.scaleOffsetX * x,
  2467. py = this.scaleOffsetY * y;
  2468. var dx = this.translateOffsetX * (x -1) / px,
  2469. dy = this.translateOffsetY * (y -1) / py;
  2470. this.scaleOffsetX = px;
  2471. this.scaleOffsetY = py;
  2472. for(var i=0, l=this.canvases.length; i<l; i++) {
  2473. this.canvases[i].scale(x, y, true);
  2474. }
  2475. this.translate(dx, dy, false);
  2476. },
  2477. /*
  2478. Method: getPos
  2479. Returns the canvas position as an *x, y* object.
  2480. Parameters:
  2481. force - (boolean) Default's *false*. Set this to *true* if you want to recalculate the position without using any cache information.
  2482. Returns:
  2483. An object with *x* and *y* properties.
  2484. Example:
  2485. (start code js)
  2486. canvas.getPos(true); //returns { x: 900, y: 500 }
  2487. (end code)
  2488. */
  2489. getPos: function(force){
  2490. if(force || !this.pos) {
  2491. return this.pos = $.getPos(this.getElement());
  2492. }
  2493. return this.pos;
  2494. },
  2495. /*
  2496. Method: clear
  2497. Clears the canvas.
  2498. */
  2499. clear: function(i){
  2500. this.canvases[i||0].clear();
  2501. },
  2502. path: function(type, action){
  2503. var ctx = this.canvases[0].getCtx();
  2504. ctx.beginPath();
  2505. action(ctx);
  2506. ctx[type]();
  2507. ctx.closePath();
  2508. },
  2509. createLabelContainer: function(type, idLabel, dim) {
  2510. var NS = 'http://www.w3.org/2000/svg';
  2511. if(type == 'HTML' || type == 'Native') {
  2512. return $E('div', {
  2513. 'id': idLabel,
  2514. 'style': {
  2515. 'overflow': 'visible',
  2516. 'position': 'absolute',
  2517. 'top': 0,
  2518. 'left': 0,
  2519. 'width': dim.width + 'px',
  2520. 'height': 0
  2521. }
  2522. });
  2523. } else if(type == 'SVG') {
  2524. var svgContainer = document.createElementNS(NS, 'svg:svg');
  2525. svgContainer.setAttribute("width", dim.width);
  2526. svgContainer.setAttribute('height', dim.height);
  2527. var style = svgContainer.style;
  2528. style.position = 'absolute';
  2529. style.left = style.top = '0px';
  2530. var labelContainer = document.createElementNS(NS, 'svg:g');
  2531. labelContainer.setAttribute('width', dim.width);
  2532. labelContainer.setAttribute('height', dim.height);
  2533. labelContainer.setAttribute('x', 0);
  2534. labelContainer.setAttribute('y', 0);
  2535. labelContainer.setAttribute('id', idLabel);
  2536. svgContainer.appendChild(labelContainer);
  2537. return svgContainer;
  2538. }
  2539. }
  2540. });
  2541. //base canvas wrapper
  2542. Canvas.Base = {};
  2543. Canvas.Base['2D'] = new Class({
  2544. translateOffsetX: 0,
  2545. translateOffsetY: 0,
  2546. scaleOffsetX: 1,
  2547. scaleOffsetY: 1,
  2548. initialize: function(viz) {
  2549. this.viz = viz;
  2550. this.opt = viz.config;
  2551. this.size = false;
  2552. this.createCanvas();
  2553. this.translateToCenter();
  2554. },
  2555. createCanvas: function() {
  2556. var opt = this.opt,
  2557. width = opt.width,
  2558. height = opt.height;
  2559. this.canvas = $E('canvas', {
  2560. 'id': opt.injectInto + opt.idSuffix,
  2561. 'width': width,
  2562. 'height': height,
  2563. 'style': {
  2564. 'position': 'absolute',
  2565. 'top': 0,
  2566. 'left': 0,
  2567. 'width': width + 'px',
  2568. 'height': height + 'px'
  2569. }
  2570. });
  2571. },
  2572. getCtx: function() {
  2573. if(!this.ctx)
  2574. return this.ctx = this.canvas.getContext('2d');
  2575. return this.ctx;
  2576. },
  2577. getSize: function() {
  2578. if(this.size) return this.size;
  2579. var canvas = this.canvas;
  2580. return this.size = {
  2581. width: canvas.width,
  2582. height: canvas.height
  2583. };
  2584. },
  2585. translateToCenter: function(ps) {
  2586. var size = this.getSize(),
  2587. width = ps? (size.width - ps.width - this.translateOffsetX*2) : size.width;
  2588. height = ps? (size.height - ps.height - this.translateOffsetY*2) : size.height;
  2589. var ctx = this.getCtx();
  2590. ps && ctx.scale(1/this.scaleOffsetX, 1/this.scaleOffsetY);
  2591. ctx.translate(width/2, height/2);
  2592. },
  2593. resize: function(width, height) {
  2594. var size = this.getSize(),
  2595. canvas = this.canvas,
  2596. styles = canvas.style;
  2597. this.size = false;
  2598. canvas.width = width;
  2599. canvas.height = height;
  2600. styles.width = width + "px";
  2601. styles.height = height + "px";
  2602. //small ExCanvas fix
  2603. if(!supportsCanvas) {
  2604. this.translateToCenter(size);
  2605. } else {
  2606. this.translateToCenter();
  2607. }
  2608. this.translateOffsetX =
  2609. this.translateOffsetY = 0;
  2610. this.scaleOffsetX =
  2611. this.scaleOffsetY = 1;
  2612. this.clear();
  2613. this.viz.resize(width, height, this);
  2614. },
  2615. translate: function(x, y, disablePlot) {
  2616. var sx = this.scaleOffsetX,
  2617. sy = this.scaleOffsetY;
  2618. this.translateOffsetX += x*sx;
  2619. this.translateOffsetY += y*sy;
  2620. this.getCtx().translate(x, y);
  2621. !disablePlot && this.plot();
  2622. },
  2623. scale: function(x, y, disablePlot) {
  2624. this.scaleOffsetX *= x;
  2625. this.scaleOffsetY *= y;
  2626. this.getCtx().scale(x, y);
  2627. !disablePlot && this.plot();
  2628. },
  2629. clear: function(){
  2630. var size = this.getSize(),
  2631. ox = this.translateOffsetX,
  2632. oy = this.translateOffsetY,
  2633. sx = this.scaleOffsetX,
  2634. sy = this.scaleOffsetY;
  2635. this.getCtx().clearRect((-size.width / 2 - ox) * 1/sx,
  2636. (-size.height / 2 - oy) * 1/sy,
  2637. size.width * 1/sx, size.height * 1/sy);
  2638. },
  2639. plot: function() {
  2640. this.clear();
  2641. this.viz.plot(this);
  2642. }
  2643. });
  2644. //background canvases
  2645. //document this!
  2646. Canvas.Background = {};
  2647. Canvas.Background.Circles = new Class({
  2648. initialize: function(viz, options) {
  2649. this.viz = viz;
  2650. this.config = $.merge({
  2651. idSuffix: '-bkcanvas',
  2652. levelDistance: 100,
  2653. numberOfCircles: 6,
  2654. CanvasStyles: {},
  2655. offset: 0
  2656. }, options);
  2657. },
  2658. resize: function(width, height, base) {
  2659. this.plot(base);
  2660. },
  2661. plot: function(base) {
  2662. var canvas = base.canvas,
  2663. ctx = base.getCtx(),
  2664. conf = this.config,
  2665. styles = conf.CanvasStyles;
  2666. //set canvas styles
  2667. for(var s in styles) ctx[s] = styles[s];
  2668. var n = conf.numberOfCircles,
  2669. rho = conf.levelDistance;
  2670. for(var i=1; i<=n; i++) {
  2671. ctx.beginPath();
  2672. ctx.arc(0, 0, rho * i, 0, 2 * Math.PI, false);
  2673. ctx.stroke();
  2674. ctx.closePath();
  2675. }
  2676. //print labels too!
  2677. }
  2678. });
  2679. })();
  2680. /*
  2681. * File: Polar.js
  2682. *
  2683. * Defines the <Polar> class.
  2684. *
  2685. * Description:
  2686. *
  2687. * The <Polar> class, just like the <Complex> class, is used by the <Hypertree>, <ST> and <RGraph> as a 2D point representation.
  2688. *
  2689. * See also:
  2690. *
  2691. * <http://en.wikipedia.org/wiki/Polar_coordinates>
  2692. *
  2693. */
  2694. /*
  2695. Class: Polar
  2696. A multi purpose polar representation.
  2697. Description:
  2698. The <Polar> class, just like the <Complex> class, is used by the <Hypertree>, <ST> and <RGraph> as a 2D point representation.
  2699. See also:
  2700. <http://en.wikipedia.org/wiki/Polar_coordinates>
  2701. Parameters:
  2702. theta - An angle.
  2703. rho - The norm.
  2704. */
  2705. var Polar = function(theta, rho) {
  2706. this.theta = theta || 0;
  2707. this.rho = rho || 0;
  2708. };
  2709. $jit.Polar = Polar;
  2710. Polar.prototype = {
  2711. /*
  2712. Method: getc
  2713. Returns a complex number.
  2714. Parameters:
  2715. simple - _optional_ If *true*, this method will return only an object holding x and y properties and not a <Complex> instance. Default's *false*.
  2716. Returns:
  2717. A complex number.
  2718. */
  2719. getc: function(simple) {
  2720. return this.toComplex(simple);
  2721. },
  2722. /*
  2723. Method: getp
  2724. Returns a <Polar> representation.
  2725. Returns:
  2726. A variable in polar coordinates.
  2727. */
  2728. getp: function() {
  2729. return this;
  2730. },
  2731. /*
  2732. Method: set
  2733. Sets a number.
  2734. Parameters:
  2735. v - A <Complex> or <Polar> instance.
  2736. */
  2737. set: function(v) {
  2738. v = v.getp();
  2739. this.theta = v.theta; this.rho = v.rho;
  2740. },
  2741. /*
  2742. Method: setc
  2743. Sets a <Complex> number.
  2744. Parameters:
  2745. x - A <Complex> number real part.
  2746. y - A <Complex> number imaginary part.
  2747. */
  2748. setc: function(x, y) {
  2749. this.rho = Math.sqrt(x * x + y * y);
  2750. this.theta = Math.atan2(y, x);
  2751. if(this.theta < 0) this.theta += Math.PI * 2;
  2752. },
  2753. /*
  2754. Method: setp
  2755. Sets a polar number.
  2756. Parameters:
  2757. theta - A <Polar> number angle property.
  2758. rho - A <Polar> number rho property.
  2759. */
  2760. setp: function(theta, rho) {
  2761. this.theta = theta;
  2762. this.rho = rho;
  2763. },
  2764. /*
  2765. Method: clone
  2766. Returns a copy of the current object.
  2767. Returns:
  2768. A copy of the real object.
  2769. */
  2770. clone: function() {
  2771. return new Polar(this.theta, this.rho);
  2772. },
  2773. /*
  2774. Method: toComplex
  2775. Translates from polar to cartesian coordinates and returns a new <Complex> instance.
  2776. Parameters:
  2777. simple - _optional_ If *true* this method will only return an object with x and y properties (and not the whole <Complex> instance). Default's *false*.
  2778. Returns:
  2779. A new <Complex> instance.
  2780. */
  2781. toComplex: function(simple) {
  2782. var x = Math.cos(this.theta) * this.rho;
  2783. var y = Math.sin(this.theta) * this.rho;
  2784. if(simple) return { 'x': x, 'y': y};
  2785. return new Complex(x, y);
  2786. },
  2787. /*
  2788. Method: add
  2789. Adds two <Polar> instances.
  2790. Parameters:
  2791. polar - A <Polar> number.
  2792. Returns:
  2793. A new Polar instance.
  2794. */
  2795. add: function(polar) {
  2796. return new Polar(this.theta + polar.theta, this.rho + polar.rho);
  2797. },
  2798. /*
  2799. Method: scale
  2800. Scales a polar norm.
  2801. Parameters:
  2802. number - A scale factor.
  2803. Returns:
  2804. A new Polar instance.
  2805. */
  2806. scale: function(number) {
  2807. return new Polar(this.theta, this.rho * number);
  2808. },
  2809. /*
  2810. Method: equals
  2811. Comparison method.
  2812. Returns *true* if the theta and rho properties are equal.
  2813. Parameters:
  2814. c - A <Polar> number.
  2815. Returns:
  2816. *true* if the theta and rho parameters for these objects are equal. *false* otherwise.
  2817. */
  2818. equals: function(c) {
  2819. return this.theta == c.theta && this.rho == c.rho;
  2820. },
  2821. /*
  2822. Method: $add
  2823. Adds two <Polar> instances affecting the current object.
  2824. Paramters:
  2825. polar - A <Polar> instance.
  2826. Returns:
  2827. The changed object.
  2828. */
  2829. $add: function(polar) {
  2830. this.theta = this.theta + polar.theta; this.rho += polar.rho;
  2831. return this;
  2832. },
  2833. /*
  2834. Method: $madd
  2835. Adds two <Polar> instances affecting the current object. The resulting theta angle is modulo 2pi.
  2836. Parameters:
  2837. polar - A <Polar> instance.
  2838. Returns:
  2839. The changed object.
  2840. */
  2841. $madd: function(polar) {
  2842. this.theta = (this.theta + polar.theta) % (Math.PI * 2); this.rho += polar.rho;
  2843. return this;
  2844. },
  2845. /*
  2846. Method: $scale
  2847. Scales a polar instance affecting the object.
  2848. Parameters:
  2849. number - A scaling factor.
  2850. Returns:
  2851. The changed object.
  2852. */
  2853. $scale: function(number) {
  2854. this.rho *= number;
  2855. return this;
  2856. },
  2857. /*
  2858. Method: isZero
  2859. Returns *true* if the number is zero.
  2860. */
  2861. isZero: function () {
  2862. var almostZero = 0.0001, abs = Math.abs;
  2863. return abs(this.theta) < almostZero && abs(this.rho) < almostZero;
  2864. },
  2865. /*
  2866. Method: interpolate
  2867. Calculates a polar interpolation between two points at a given delta moment.
  2868. Parameters:
  2869. elem - A <Polar> instance.
  2870. delta - A delta factor ranging [0, 1].
  2871. Returns:
  2872. A new <Polar> instance representing an interpolation between _this_ and _elem_
  2873. */
  2874. interpolate: function(elem, delta) {
  2875. var pi = Math.PI, pi2 = pi * 2;
  2876. var ch = function(t) {
  2877. var a = (t < 0)? (t % pi2) + pi2 : t % pi2;
  2878. return a;
  2879. };
  2880. var tt = this.theta, et = elem.theta;
  2881. var sum, diff = Math.abs(tt - et);
  2882. if(diff == pi) {
  2883. if(tt > et) {
  2884. sum = ch((et + ((tt - pi2) - et) * delta)) ;
  2885. } else {
  2886. sum = ch((et - pi2 + (tt - (et)) * delta));
  2887. }
  2888. } else if(diff >= pi) {
  2889. if(tt > et) {
  2890. sum = ch((et + ((tt - pi2) - et) * delta)) ;
  2891. } else {
  2892. sum = ch((et - pi2 + (tt - (et - pi2)) * delta));
  2893. }
  2894. } else {
  2895. sum = ch((et + (tt - et) * delta)) ;
  2896. }
  2897. var r = (this.rho - elem.rho) * delta + elem.rho;
  2898. return {
  2899. 'theta': sum,
  2900. 'rho': r
  2901. };
  2902. }
  2903. };
  2904. var $P = function(a, b) { return new Polar(a, b); };
  2905. Polar.KER = $P(0, 0);
  2906. /*
  2907. * File: Complex.js
  2908. *
  2909. * Defines the <Complex> class.
  2910. *
  2911. * Description:
  2912. *
  2913. * The <Complex> class, just like the <Polar> class, is used by the <Hypertree>, <ST> and <RGraph> as a 2D point representation.
  2914. *
  2915. * See also:
  2916. *
  2917. * <http://en.wikipedia.org/wiki/Complex_number>
  2918. *
  2919. */
  2920. /*
  2921. Class: Complex
  2922. A multi-purpose Complex Class with common methods.
  2923. Description:
  2924. The <Complex> class, just like the <Polar> class, is used by the <Hypertree>, <ST> and <RGraph> as a 2D point representation.
  2925. See also:
  2926. <http://en.wikipedia.org/wiki/Complex_number>
  2927. Parameters:
  2928. x - _optional_ A Complex number real part.
  2929. y - _optional_ A Complex number imaginary part.
  2930. */
  2931. var Complex = function(x, y) {
  2932. this.x = x || 0;
  2933. this.y = y || 0;
  2934. };
  2935. $jit.Complex = Complex;
  2936. Complex.prototype = {
  2937. /*
  2938. Method: getc
  2939. Returns a complex number.
  2940. Returns:
  2941. A complex number.
  2942. */
  2943. getc: function() {
  2944. return this;
  2945. },
  2946. /*
  2947. Method: getp
  2948. Returns a <Polar> representation of this number.
  2949. Parameters:
  2950. simple - _optional_ If *true*, this method will return only an object holding theta and rho properties and not a <Polar> instance. Default's *false*.
  2951. Returns:
  2952. A variable in <Polar> coordinates.
  2953. */
  2954. getp: function(simple) {
  2955. return this.toPolar(simple);
  2956. },
  2957. /*
  2958. Method: set
  2959. Sets a number.
  2960. Parameters:
  2961. c - A <Complex> or <Polar> instance.
  2962. */
  2963. set: function(c) {
  2964. c = c.getc(true);
  2965. this.x = c.x;
  2966. this.y = c.y;
  2967. },
  2968. /*
  2969. Method: setc
  2970. Sets a complex number.
  2971. Parameters:
  2972. x - A <Complex> number Real part.
  2973. y - A <Complex> number Imaginary part.
  2974. */
  2975. setc: function(x, y) {
  2976. this.x = x;
  2977. this.y = y;
  2978. },
  2979. /*
  2980. Method: setp
  2981. Sets a polar number.
  2982. Parameters:
  2983. theta - A <Polar> number theta property.
  2984. rho - A <Polar> number rho property.
  2985. */
  2986. setp: function(theta, rho) {
  2987. this.x = Math.cos(theta) * rho;
  2988. this.y = Math.sin(theta) * rho;
  2989. },
  2990. /*
  2991. Method: clone
  2992. Returns a copy of the current object.
  2993. Returns:
  2994. A copy of the real object.
  2995. */
  2996. clone: function() {
  2997. return new Complex(this.x, this.y);
  2998. },
  2999. /*
  3000. Method: toPolar
  3001. Transforms cartesian to polar coordinates.
  3002. Parameters:
  3003. simple - _optional_ If *true* this method will only return an object with theta and rho properties (and not the whole <Polar> instance). Default's *false*.
  3004. Returns:
  3005. A new <Polar> instance.
  3006. */
  3007. toPolar: function(simple) {
  3008. var rho = this.norm();
  3009. var atan = Math.atan2(this.y, this.x);
  3010. if(atan < 0) atan += Math.PI * 2;
  3011. if(simple) return { 'theta': atan, 'rho': rho };
  3012. return new Polar(atan, rho);
  3013. },
  3014. /*
  3015. Method: norm
  3016. Calculates a <Complex> number norm.
  3017. Returns:
  3018. A real number representing the complex norm.
  3019. */
  3020. norm: function () {
  3021. return Math.sqrt(this.squaredNorm());
  3022. },
  3023. /*
  3024. Method: squaredNorm
  3025. Calculates a <Complex> number squared norm.
  3026. Returns:
  3027. A real number representing the complex squared norm.
  3028. */
  3029. squaredNorm: function () {
  3030. return this.x*this.x + this.y*this.y;
  3031. },
  3032. /*
  3033. Method: add
  3034. Returns the result of adding two complex numbers.
  3035. Does not alter the original object.
  3036. Parameters:
  3037. pos - A <Complex> instance.
  3038. Returns:
  3039. The result of adding two complex numbers.
  3040. */
  3041. add: function(pos) {
  3042. return new Complex(this.x + pos.x, this.y + pos.y);
  3043. },
  3044. /*
  3045. Method: prod
  3046. Returns the result of multiplying two <Complex> numbers.
  3047. Does not alter the original object.
  3048. Parameters:
  3049. pos - A <Complex> instance.
  3050. Returns:
  3051. The result of multiplying two complex numbers.
  3052. */
  3053. prod: function(pos) {
  3054. return new Complex(this.x*pos.x - this.y*pos.y, this.y*pos.x + this.x*pos.y);
  3055. },
  3056. /*
  3057. Method: conjugate
  3058. Returns the conjugate of this <Complex> number.
  3059. Does not alter the original object.
  3060. Returns:
  3061. The conjugate of this <Complex> number.
  3062. */
  3063. conjugate: function() {
  3064. return new Complex(this.x, -this.y);
  3065. },
  3066. /*
  3067. Method: scale
  3068. Returns the result of scaling a <Complex> instance.
  3069. Does not alter the original object.
  3070. Parameters:
  3071. factor - A scale factor.
  3072. Returns:
  3073. The result of scaling this complex to a factor.
  3074. */
  3075. scale: function(factor) {
  3076. return new Complex(this.x * factor, this.y * factor);
  3077. },
  3078. /*
  3079. Method: equals
  3080. Comparison method.
  3081. Returns *true* if both real and imaginary parts are equal.
  3082. Parameters:
  3083. c - A <Complex> instance.
  3084. Returns:
  3085. A boolean instance indicating if both <Complex> numbers are equal.
  3086. */
  3087. equals: function(c) {
  3088. return this.x == c.x && this.y == c.y;
  3089. },
  3090. /*
  3091. Method: $add
  3092. Returns the result of adding two <Complex> numbers.
  3093. Alters the original object.
  3094. Parameters:
  3095. pos - A <Complex> instance.
  3096. Returns:
  3097. The result of adding two complex numbers.
  3098. */
  3099. $add: function(pos) {
  3100. this.x += pos.x; this.y += pos.y;
  3101. return this;
  3102. },
  3103. /*
  3104. Method: $prod
  3105. Returns the result of multiplying two <Complex> numbers.
  3106. Alters the original object.
  3107. Parameters:
  3108. pos - A <Complex> instance.
  3109. Returns:
  3110. The result of multiplying two complex numbers.
  3111. */
  3112. $prod:function(pos) {
  3113. var x = this.x, y = this.y;
  3114. this.x = x*pos.x - y*pos.y;
  3115. this.y = y*pos.x + x*pos.y;
  3116. return this;
  3117. },
  3118. /*
  3119. Method: $conjugate
  3120. Returns the conjugate for this <Complex>.
  3121. Alters the original object.
  3122. Returns:
  3123. The conjugate for this complex.
  3124. */
  3125. $conjugate: function() {
  3126. this.y = -this.y;
  3127. return this;
  3128. },
  3129. /*
  3130. Method: $scale
  3131. Returns the result of scaling a <Complex> instance.
  3132. Alters the original object.
  3133. Parameters:
  3134. factor - A scale factor.
  3135. Returns:
  3136. The result of scaling this complex to a factor.
  3137. */
  3138. $scale: function(factor) {
  3139. this.x *= factor; this.y *= factor;
  3140. return this;
  3141. },
  3142. /*
  3143. Method: $div
  3144. Returns the division of two <Complex> numbers.
  3145. Alters the original object.
  3146. Parameters:
  3147. pos - A <Complex> number.
  3148. Returns:
  3149. The result of scaling this complex to a factor.
  3150. */
  3151. $div: function(pos) {
  3152. var x = this.x, y = this.y;
  3153. var sq = pos.squaredNorm();
  3154. this.x = x * pos.x + y * pos.y; this.y = y * pos.x - x * pos.y;
  3155. return this.$scale(1 / sq);
  3156. },
  3157. /*
  3158. Method: isZero
  3159. Returns *true* if the number is zero.
  3160. */
  3161. isZero: function () {
  3162. var almostZero = 0.0001, abs = Math.abs;
  3163. return abs(this.x) < almostZero && abs(this.y) < almostZero;
  3164. }
  3165. };
  3166. var $C = function(a, b) { return new Complex(a, b); };
  3167. Complex.KER = $C(0, 0);
  3168. /*
  3169. * File: Graph.js
  3170. *
  3171. */
  3172. /*
  3173. Class: Graph
  3174. A Graph Class that provides useful manipulation functions. You can find more manipulation methods in the <Graph.Util> object.
  3175. An instance of this class can be accessed by using the *graph* parameter of any tree or graph visualization.
  3176. Example:
  3177. (start code js)
  3178. //create new visualization
  3179. var viz = new $jit.Viz(options);
  3180. //load JSON data
  3181. viz.loadJSON(json);
  3182. //access model
  3183. viz.graph; //<Graph> instance
  3184. (end code)
  3185. Implements:
  3186. The following <Graph.Util> methods are implemented in <Graph>
  3187. - <Graph.Util.getNode>
  3188. - <Graph.Util.eachNode>
  3189. - <Graph.Util.computeLevels>
  3190. - <Graph.Util.eachBFS>
  3191. - <Graph.Util.clean>
  3192. - <Graph.Util.getClosestNodeToPos>
  3193. - <Graph.Util.getClosestNodeToOrigin>
  3194. */
  3195. $jit.Graph = new Class({
  3196. initialize: function(opt, Node, Edge, Label) {
  3197. var innerOptions = {
  3198. 'klass': Complex,
  3199. 'Node': {}
  3200. };
  3201. this.Node = Node;
  3202. this.Edge = Edge;
  3203. this.Label = Label;
  3204. this.opt = $.merge(innerOptions, opt || {});
  3205. this.nodes = {};
  3206. this.edges = {};
  3207. //add nodeList methods
  3208. var that = this;
  3209. this.nodeList = {};
  3210. for(var p in Accessors) {
  3211. that.nodeList[p] = (function(p) {
  3212. return function() {
  3213. var args = Array.prototype.slice.call(arguments);
  3214. that.eachNode(function(n) {
  3215. n[p].apply(n, args);
  3216. });
  3217. };
  3218. })(p);
  3219. }
  3220. },
  3221. /*
  3222. Method: getNode
  3223. Returns a <Graph.Node> by *id*.
  3224. Parameters:
  3225. id - (string) A <Graph.Node> id.
  3226. Example:
  3227. (start code js)
  3228. var node = graph.getNode('nodeId');
  3229. (end code)
  3230. */
  3231. getNode: function(id) {
  3232. if(this.hasNode(id)) return this.nodes[id];
  3233. return false;
  3234. },
  3235. /*
  3236. Method: get
  3237. An alias for <Graph.Util.getNode>. Returns a node by *id*.
  3238. Parameters:
  3239. id - (string) A <Graph.Node> id.
  3240. Example:
  3241. (start code js)
  3242. var node = graph.get('nodeId');
  3243. (end code)
  3244. */
  3245. get: function(id) {
  3246. return this.getNode(id);
  3247. },
  3248. /*
  3249. Method: getByName
  3250. Returns a <Graph.Node> by *name*.
  3251. Parameters:
  3252. name - (string) A <Graph.Node> name.
  3253. Example:
  3254. (start code js)
  3255. var node = graph.getByName('someName');
  3256. (end code)
  3257. */
  3258. getByName: function(name) {
  3259. for(var id in this.nodes) {
  3260. var n = this.nodes[id];
  3261. if(n.name == name) return n;
  3262. }
  3263. return false;
  3264. },
  3265. /*
  3266. Method: getAdjacence
  3267. Returns a <Graph.Adjacence> object connecting nodes with ids *id* and *id2*.
  3268. Parameters:
  3269. id - (string) A <Graph.Node> id.
  3270. id2 - (string) A <Graph.Node> id.
  3271. */
  3272. getAdjacence: function (id, id2) {
  3273. if(id in this.edges) {
  3274. return this.edges[id][id2];
  3275. }
  3276. return false;
  3277. },
  3278. /*
  3279. Method: addNode
  3280. Adds a node.
  3281. Parameters:
  3282. obj - An object with the properties described below
  3283. id - (string) A node id
  3284. name - (string) A node's name
  3285. data - (object) A node's data hash
  3286. See also:
  3287. <Graph.Node>
  3288. */
  3289. addNode: function(obj) {
  3290. if(!this.nodes[obj.id]) {
  3291. var edges = this.edges[obj.id] = {};
  3292. this.nodes[obj.id] = new Graph.Node($.extend({
  3293. 'id': obj.id,
  3294. 'name': obj.name,
  3295. 'data': $.merge(obj.data || {}, {}),
  3296. 'adjacencies': edges
  3297. }, this.opt.Node),
  3298. this.opt.klass,
  3299. this.Node,
  3300. this.Edge,
  3301. this.Label);
  3302. }
  3303. return this.nodes[obj.id];
  3304. },
  3305. /*
  3306. Method: addAdjacence
  3307. Connects nodes specified by *obj* and *obj2*. If not found, nodes are created.
  3308. Parameters:
  3309. obj - (object) A <Graph.Node> object.
  3310. obj2 - (object) Another <Graph.Node> object.
  3311. data - (object) A data object. Used to store some extra information in the <Graph.Adjacence> object created.
  3312. See also:
  3313. <Graph.Node>, <Graph.Adjacence>
  3314. */
  3315. addAdjacence: function (obj, obj2, data) {
  3316. if(!this.hasNode(obj.id)) { this.addNode(obj); }
  3317. if(!this.hasNode(obj2.id)) { this.addNode(obj2); }
  3318. obj = this.nodes[obj.id]; obj2 = this.nodes[obj2.id];
  3319. if(!obj.adjacentTo(obj2)) {
  3320. var adjsObj = this.edges[obj.id] = this.edges[obj.id] || {};
  3321. var adjsObj2 = this.edges[obj2.id] = this.edges[obj2.id] || {};
  3322. adjsObj[obj2.id] = adjsObj2[obj.id] = new Graph.Adjacence(obj, obj2, data, this.Edge, this.Label);
  3323. return adjsObj[obj2.id];
  3324. }
  3325. return this.edges[obj.id][obj2.id];
  3326. },
  3327. /*
  3328. Method: removeNode
  3329. Removes a <Graph.Node> matching the specified *id*.
  3330. Parameters:
  3331. id - (string) A node's id.
  3332. */
  3333. removeNode: function(id) {
  3334. if(this.hasNode(id)) {
  3335. delete this.nodes[id];
  3336. var adjs = this.edges[id];
  3337. for(var to in adjs) {
  3338. delete this.edges[to][id];
  3339. }
  3340. delete this.edges[id];
  3341. }
  3342. },
  3343. /*
  3344. Method: removeAdjacence
  3345. Removes a <Graph.Adjacence> matching *id1* and *id2*.
  3346. Parameters:
  3347. id1 - (string) A <Graph.Node> id.
  3348. id2 - (string) A <Graph.Node> id.
  3349. */
  3350. removeAdjacence: function(id1, id2) {
  3351. delete this.edges[id1][id2];
  3352. delete this.edges[id2][id1];
  3353. },
  3354. /*
  3355. Method: hasNode
  3356. Returns a boolean indicating if the node belongs to the <Graph> or not.
  3357. Parameters:
  3358. id - (string) Node id.
  3359. */
  3360. hasNode: function(id) {
  3361. return id in this.nodes;
  3362. },
  3363. /*
  3364. Method: empty
  3365. Empties the Graph
  3366. */
  3367. empty: function() { this.nodes = {}; this.edges = {};}
  3368. });
  3369. var Graph = $jit.Graph;
  3370. /*
  3371. Object: Accessors
  3372. Defines a set of methods for data, canvas and label styles manipulation implemented by <Graph.Node> and <Graph.Adjacence> instances.
  3373. */
  3374. var Accessors;
  3375. (function () {
  3376. var getDataInternal = function(prefix, prop, type, force, prefixConfig) {
  3377. var data;
  3378. type = type || 'current';
  3379. prefix = "$" + (prefix ? prefix + "-" : "");
  3380. if(type == 'current') {
  3381. data = this.data;
  3382. } else if(type == 'start') {
  3383. data = this.startData;
  3384. } else if(type == 'end') {
  3385. data = this.endData;
  3386. }
  3387. var dollar = prefix + prop;
  3388. if(force) {
  3389. return data[dollar];
  3390. }
  3391. if(!this.Config.overridable)
  3392. return prefixConfig[prop] || 0;
  3393. return (dollar in data) ?
  3394. data[dollar] : ((dollar in this.data) ? this.data[dollar] : (prefixConfig[prop] || 0));
  3395. }
  3396. var setDataInternal = function(prefix, prop, value, type) {
  3397. type = type || 'current';
  3398. prefix = '$' + (prefix ? prefix + '-' : '');
  3399. var data;
  3400. if(type == 'current') {
  3401. data = this.data;
  3402. } else if(type == 'start') {
  3403. data = this.startData;
  3404. } else if(type == 'end') {
  3405. data = this.endData;
  3406. }
  3407. data[prefix + prop] = value;
  3408. }
  3409. var removeDataInternal = function(prefix, properties) {
  3410. prefix = '$' + (prefix ? prefix + '-' : '');
  3411. var that = this;
  3412. $.each(properties, function(prop) {
  3413. var pref = prefix + prop;
  3414. delete that.data[pref];
  3415. delete that.endData[pref];
  3416. delete that.startData[pref];
  3417. });
  3418. }
  3419. Accessors = {
  3420. /*
  3421. Method: getData
  3422. Returns the specified data value property.
  3423. This is useful for querying special/reserved <Graph.Node> data properties
  3424. (i.e dollar prefixed properties).
  3425. Parameters:
  3426. prop - (string) The name of the property. The dollar sign is not needed. For
  3427. example *getData(width)* will return *data.$width*.
  3428. type - (string) The type of the data property queried. Default's "current". You can access *start* and *end*
  3429. data properties also. These properties are used when making animations.
  3430. force - (boolean) Whether to obtain the true value of the property (equivalent to
  3431. *data.$prop*) or to check for *node.overridable = true* first.
  3432. Returns:
  3433. The value of the dollar prefixed property or the global Node/Edge property
  3434. value if *overridable=false*
  3435. Example:
  3436. (start code js)
  3437. node.getData('width'); //will return node.data.$width if Node.overridable=true;
  3438. (end code)
  3439. */
  3440. getData: function(prop, type, force) {
  3441. return getDataInternal.call(this, "", prop, type, force, this.Config);
  3442. },
  3443. /*
  3444. Method: setData
  3445. Sets the current data property with some specific value.
  3446. This method is only useful for reserved (dollar prefixed) properties.
  3447. Parameters:
  3448. prop - (string) The name of the property. The dollar sign is not necessary. For
  3449. example *setData(width)* will set *data.$width*.
  3450. value - (mixed) The value to store.
  3451. type - (string) The type of the data property to store. Default's "current" but
  3452. can also be "start" or "end".
  3453. Example:
  3454. (start code js)
  3455. node.setData('width', 30);
  3456. (end code)
  3457. If we were to make an animation of a node/edge width then we could do
  3458. (start code js)
  3459. var node = viz.getNode('nodeId');
  3460. //set start and end values
  3461. node.setData('width', 10, 'start');
  3462. node.setData('width', 30, 'end');
  3463. //will animate nodes width property
  3464. viz.fx.animate({
  3465. modes: ['node-property:width'],
  3466. duration: 1000
  3467. });
  3468. (end code)
  3469. */
  3470. setData: function(prop, value, type) {
  3471. setDataInternal.call(this, "", prop, value, type);
  3472. },
  3473. /*
  3474. Method: setDataset
  3475. Convenience method to set multiple data values at once.
  3476. Parameters:
  3477. types - (array|string) A set of 'current', 'end' or 'start' values.
  3478. obj - (object) A hash containing the names and values of the properties to be altered.
  3479. Example:
  3480. (start code js)
  3481. node.setDataset(['current', 'end'], {
  3482. 'width': [100, 5],
  3483. 'color': ['#fff', '#ccc']
  3484. });
  3485. //...or also
  3486. node.setDataset('end', {
  3487. 'width': 5,
  3488. 'color': '#ccc'
  3489. });
  3490. (end code)
  3491. See also:
  3492. <Accessors.setData>
  3493. */
  3494. setDataset: function(types, obj) {
  3495. types = $.splat(types);
  3496. for(var attr in obj) {
  3497. for(var i=0, val = $.splat(obj[attr]), l=types.length; i<l; i++) {
  3498. this.setData(attr, val[i], types[i]);
  3499. }
  3500. }
  3501. },
  3502. /*
  3503. Method: removeData
  3504. Remove data properties.
  3505. Parameters:
  3506. One or more property names as arguments. The dollar sign is not needed.
  3507. Example:
  3508. (start code js)
  3509. node.removeData('width'); //now the default width value is returned
  3510. (end code)
  3511. */
  3512. removeData: function() {
  3513. removeDataInternal.call(this, "", Array.prototype.slice.call(arguments));
  3514. },
  3515. /*
  3516. Method: getCanvasStyle
  3517. Returns the specified canvas style data value property. This is useful for
  3518. querying special/reserved <Graph.Node> canvas style data properties (i.e.
  3519. dollar prefixed properties that match with $canvas-<name of canvas style>).
  3520. Parameters:
  3521. prop - (string) The name of the property. The dollar sign is not needed. For
  3522. example *getCanvasStyle(shadowBlur)* will return *data[$canvas-shadowBlur]*.
  3523. type - (string) The type of the data property queried. Default's *current*. You can access *start* and *end*
  3524. data properties also.
  3525. Example:
  3526. (start code js)
  3527. node.getCanvasStyle('shadowBlur');
  3528. (end code)
  3529. See also:
  3530. <Accessors.getData>
  3531. */
  3532. getCanvasStyle: function(prop, type, force) {
  3533. return getDataInternal.call(
  3534. this, 'canvas', prop, type, force, this.Config.CanvasStyles);
  3535. },
  3536. /*
  3537. Method: setCanvasStyle
  3538. Sets the canvas style data property with some specific value.
  3539. This method is only useful for reserved (dollar prefixed) properties.
  3540. Parameters:
  3541. prop - (string) Name of the property. Can be any canvas property like 'shadowBlur', 'shadowColor', 'strokeStyle', etc.
  3542. value - (mixed) The value to set to the property.
  3543. type - (string) Default's *current*. Whether to set *start*, *current* or *end* type properties.
  3544. Example:
  3545. (start code js)
  3546. node.setCanvasStyle('shadowBlur', 30);
  3547. (end code)
  3548. If we were to make an animation of a node/edge shadowBlur canvas style then we could do
  3549. (start code js)
  3550. var node = viz.getNode('nodeId');
  3551. //set start and end values
  3552. node.setCanvasStyle('shadowBlur', 10, 'start');
  3553. node.setCanvasStyle('shadowBlur', 30, 'end');
  3554. //will animate nodes canvas style property for nodes
  3555. viz.fx.animate({
  3556. modes: ['node-style:shadowBlur'],
  3557. duration: 1000
  3558. });
  3559. (end code)
  3560. See also:
  3561. <Accessors.setData>.
  3562. */
  3563. setCanvasStyle: function(prop, value, type) {
  3564. setDataInternal.call(this, 'canvas', prop, value, type);
  3565. },
  3566. /*
  3567. Method: setCanvasStyles
  3568. Convenience method to set multiple styles at once.
  3569. Parameters:
  3570. types - (array|string) A set of 'current', 'end' or 'start' values.
  3571. obj - (object) A hash containing the names and values of the properties to be altered.
  3572. See also:
  3573. <Accessors.setDataset>.
  3574. */
  3575. setCanvasStyles: function(types, obj) {
  3576. types = $.splat(types);
  3577. for(var attr in obj) {
  3578. for(var i=0, val = $.splat(obj[attr]), l=types.length; i<l; i++) {
  3579. this.setCanvasStyle(attr, val[i], types[i]);
  3580. }
  3581. }
  3582. },
  3583. /*
  3584. Method: removeCanvasStyle
  3585. Remove canvas style properties from data.
  3586. Parameters:
  3587. A variable number of canvas style strings.
  3588. See also:
  3589. <Accessors.removeData>.
  3590. */
  3591. removeCanvasStyle: function() {
  3592. removeDataInternal.call(this, 'canvas', Array.prototype.slice.call(arguments));
  3593. },
  3594. /*
  3595. Method: getLabelData
  3596. Returns the specified label data value property. This is useful for
  3597. querying special/reserved <Graph.Node> label options (i.e.
  3598. dollar prefixed properties that match with $label-<name of label style>).
  3599. Parameters:
  3600. prop - (string) The name of the property. The dollar sign prefix is not needed. For
  3601. example *getLabelData(size)* will return *data[$label-size]*.
  3602. type - (string) The type of the data property queried. Default's *current*. You can access *start* and *end*
  3603. data properties also.
  3604. See also:
  3605. <Accessors.getData>.
  3606. */
  3607. getLabelData: function(prop, type, force) {
  3608. return getDataInternal.call(
  3609. this, 'label', prop, type, force, this.Label);
  3610. },
  3611. /*
  3612. Method: setLabelData
  3613. Sets the current label data with some specific value.
  3614. This method is only useful for reserved (dollar prefixed) properties.
  3615. Parameters:
  3616. prop - (string) Name of the property. Can be any canvas property like 'shadowBlur', 'shadowColor', 'strokeStyle', etc.
  3617. value - (mixed) The value to set to the property.
  3618. type - (string) Default's *current*. Whether to set *start*, *current* or *end* type properties.
  3619. Example:
  3620. (start code js)
  3621. node.setLabelData('size', 30);
  3622. (end code)
  3623. If we were to make an animation of a node label size then we could do
  3624. (start code js)
  3625. var node = viz.getNode('nodeId');
  3626. //set start and end values
  3627. node.setLabelData('size', 10, 'start');
  3628. node.setLabelData('size', 30, 'end');
  3629. //will animate nodes label size
  3630. viz.fx.animate({
  3631. modes: ['label-property:size'],
  3632. duration: 1000
  3633. });
  3634. (end code)
  3635. See also:
  3636. <Accessors.setData>.
  3637. */
  3638. setLabelData: function(prop, value, type) {
  3639. setDataInternal.call(this, 'label', prop, value, type);
  3640. },
  3641. /*
  3642. Method: setLabelDataset
  3643. Convenience function to set multiple label data at once.
  3644. Parameters:
  3645. types - (array|string) A set of 'current', 'end' or 'start' values.
  3646. obj - (object) A hash containing the names and values of the properties to be altered.
  3647. See also:
  3648. <Accessors.setDataset>.
  3649. */
  3650. setLabelDataset: function(types, obj) {
  3651. types = $.splat(types);
  3652. for(var attr in obj) {
  3653. for(var i=0, val = $.splat(obj[attr]), l=types.length; i<l; i++) {
  3654. this.setLabelData(attr, val[i], types[i]);
  3655. }
  3656. }
  3657. },
  3658. /*
  3659. Method: removeLabelData
  3660. Remove label properties from data.
  3661. Parameters:
  3662. A variable number of label property strings.
  3663. See also:
  3664. <Accessors.removeData>.
  3665. */
  3666. removeLabelData: function() {
  3667. removeDataInternal.call(this, 'label', Array.prototype.slice.call(arguments));
  3668. }
  3669. };
  3670. })();
  3671. /*
  3672. Class: Graph.Node
  3673. A <Graph> node.
  3674. Implements:
  3675. <Accessors> methods.
  3676. The following <Graph.Util> methods are implemented by <Graph.Node>
  3677. - <Graph.Util.eachAdjacency>
  3678. - <Graph.Util.eachLevel>
  3679. - <Graph.Util.eachSubgraph>
  3680. - <Graph.Util.eachSubnode>
  3681. - <Graph.Util.anySubnode>
  3682. - <Graph.Util.getSubnodes>
  3683. - <Graph.Util.getParents>
  3684. - <Graph.Util.isDescendantOf>
  3685. */
  3686. Graph.Node = new Class({
  3687. initialize: function(opt, klass, Node, Edge, Label) {
  3688. var innerOptions = {
  3689. 'id': '',
  3690. 'name': '',
  3691. 'data': {},
  3692. 'startData': {},
  3693. 'endData': {},
  3694. 'adjacencies': {},
  3695. 'selected': false,
  3696. 'drawn': false,
  3697. 'exist': false,
  3698. 'angleSpan': {
  3699. 'begin': 0,
  3700. 'end' : 0
  3701. },
  3702. 'pos': new klass,
  3703. 'startPos': new klass,
  3704. 'endPos': new klass
  3705. };
  3706. $.extend(this, $.extend(innerOptions, opt));
  3707. this.Config = this.Node = Node;
  3708. this.Edge = Edge;
  3709. this.Label = Label;
  3710. },
  3711. /*
  3712. Method: adjacentTo
  3713. Indicates if the node is adjacent to the node specified by id
  3714. Parameters:
  3715. id - (string) A node id.
  3716. Example:
  3717. (start code js)
  3718. node.adjacentTo('nodeId') == true;
  3719. (end code)
  3720. */
  3721. adjacentTo: function(node) {
  3722. return node.id in this.adjacencies;
  3723. },
  3724. /*
  3725. Method: getAdjacency
  3726. Returns a <Graph.Adjacence> object connecting the current <Graph.Node> and the node having *id* as id.
  3727. Parameters:
  3728. id - (string) A node id.
  3729. */
  3730. getAdjacency: function(id) {
  3731. return this.adjacencies[id];
  3732. },
  3733. /*
  3734. Method: getPos
  3735. Returns the position of the node.
  3736. Parameters:
  3737. type - (string) Default's *current*. Possible values are "start", "end" or "current".
  3738. Returns:
  3739. A <Complex> or <Polar> instance.
  3740. Example:
  3741. (start code js)
  3742. var pos = node.getPos('end');
  3743. (end code)
  3744. */
  3745. getPos: function(type) {
  3746. type = type || "current";
  3747. if(type == "current") {
  3748. return this.pos;
  3749. } else if(type == "end") {
  3750. return this.endPos;
  3751. } else if(type == "start") {
  3752. return this.startPos;
  3753. }
  3754. },
  3755. /*
  3756. Method: setPos
  3757. Sets the node's position.
  3758. Parameters:
  3759. value - (object) A <Complex> or <Polar> instance.
  3760. type - (string) Default's *current*. Possible values are "start", "end" or "current".
  3761. Example:
  3762. (start code js)
  3763. node.setPos(new $jit.Complex(0, 0), 'end');
  3764. (end code)
  3765. */
  3766. setPos: function(value, type) {
  3767. type = type || "current";
  3768. var pos;
  3769. if(type == "current") {
  3770. pos = this.pos;
  3771. } else if(type == "end") {
  3772. pos = this.endPos;
  3773. } else if(type == "start") {
  3774. pos = this.startPos;
  3775. }
  3776. pos.set(value);
  3777. }
  3778. });
  3779. Graph.Node.implement(Accessors);
  3780. /*
  3781. Class: Graph.Adjacence
  3782. A <Graph> adjacence (or edge) connecting two <Graph.Nodes>.
  3783. Implements:
  3784. <Accessors> methods.
  3785. See also:
  3786. <Graph>, <Graph.Node>
  3787. Properties:
  3788. nodeFrom - A <Graph.Node> connected by this edge.
  3789. nodeTo - Another <Graph.Node> connected by this edge.
  3790. data - Node data property containing a hash (i.e {}) with custom options.
  3791. */
  3792. Graph.Adjacence = new Class({
  3793. initialize: function(nodeFrom, nodeTo, data, Edge, Label) {
  3794. this.nodeFrom = nodeFrom;
  3795. this.nodeTo = nodeTo;
  3796. this.data = data || {};
  3797. this.startData = {};
  3798. this.endData = {};
  3799. this.Config = this.Edge = Edge;
  3800. this.Label = Label;
  3801. }
  3802. });
  3803. Graph.Adjacence.implement(Accessors);
  3804. /*
  3805. Object: Graph.Util
  3806. <Graph> traversal and processing utility object.
  3807. Note:
  3808. For your convenience some of these methods have also been appended to <Graph> and <Graph.Node> classes.
  3809. */
  3810. Graph.Util = {
  3811. /*
  3812. filter
  3813. For internal use only. Provides a filtering function based on flags.
  3814. */
  3815. filter: function(param) {
  3816. if(!param || !($.type(param) == 'string')) return function() { return true; };
  3817. var props = param.split(" ");
  3818. return function(elem) {
  3819. for(var i=0; i<props.length; i++) {
  3820. if(elem[props[i]]) {
  3821. return false;
  3822. }
  3823. }
  3824. return true;
  3825. };
  3826. },
  3827. /*
  3828. Method: getNode
  3829. Returns a <Graph.Node> by *id*.
  3830. Also implemented by:
  3831. <Graph>
  3832. Parameters:
  3833. graph - (object) A <Graph> instance.
  3834. id - (string) A <Graph.Node> id.
  3835. Example:
  3836. (start code js)
  3837. $jit.Graph.Util.getNode(graph, 'nodeid');
  3838. //or...
  3839. graph.getNode('nodeid');
  3840. (end code)
  3841. */
  3842. getNode: function(graph, id) {
  3843. return graph.nodes[id];
  3844. },
  3845. /*
  3846. Method: eachNode
  3847. Iterates over <Graph> nodes performing an *action*.
  3848. Also implemented by:
  3849. <Graph>.
  3850. Parameters:
  3851. graph - (object) A <Graph> instance.
  3852. action - (function) A callback function having a <Graph.Node> as first formal parameter.
  3853. Example:
  3854. (start code js)
  3855. $jit.Graph.Util.eachNode(graph, function(node) {
  3856. alert(node.name);
  3857. });
  3858. //or...
  3859. graph.eachNode(function(node) {
  3860. alert(node.name);
  3861. });
  3862. (end code)
  3863. */
  3864. eachNode: function(graph, action, flags) {
  3865. var filter = this.filter(flags);
  3866. for(var i in graph.nodes) {
  3867. if(filter(graph.nodes[i])) action(graph.nodes[i]);
  3868. }
  3869. },
  3870. /*
  3871. Method: each
  3872. Iterates over <Graph> nodes performing an *action*. It's an alias for <Graph.Util.eachNode>.
  3873. Also implemented by:
  3874. <Graph>.
  3875. Parameters:
  3876. graph - (object) A <Graph> instance.
  3877. action - (function) A callback function having a <Graph.Node> as first formal parameter.
  3878. Example:
  3879. (start code js)
  3880. $jit.Graph.Util.each(graph, function(node) {
  3881. alert(node.name);
  3882. });
  3883. //or...
  3884. graph.each(function(node) {
  3885. alert(node.name);
  3886. });
  3887. (end code)
  3888. */
  3889. each: function(graph, action, flags) {
  3890. this.eachNode(graph, action, flags);
  3891. },
  3892. /*
  3893. Method: eachAdjacency
  3894. Iterates over <Graph.Node> adjacencies applying the *action* function.
  3895. Also implemented by:
  3896. <Graph.Node>.
  3897. Parameters:
  3898. node - (object) A <Graph.Node>.
  3899. action - (function) A callback function having <Graph.Adjacence> as first formal parameter.
  3900. Example:
  3901. (start code js)
  3902. $jit.Graph.Util.eachAdjacency(node, function(adj) {
  3903. alert(adj.nodeTo.name);
  3904. });
  3905. //or...
  3906. node.eachAdjacency(function(adj) {
  3907. alert(adj.nodeTo.name);
  3908. });
  3909. (end code)
  3910. */
  3911. eachAdjacency: function(node, action, flags) {
  3912. var adj = node.adjacencies, filter = this.filter(flags);
  3913. for(var id in adj) {
  3914. var a = adj[id];
  3915. if(filter(a)) {
  3916. if(a.nodeFrom != node) {
  3917. var tmp = a.nodeFrom;
  3918. a.nodeFrom = a.nodeTo;
  3919. a.nodeTo = tmp;
  3920. }
  3921. action(a, id);
  3922. }
  3923. }
  3924. },
  3925. /*
  3926. Method: computeLevels
  3927. Performs a BFS traversal setting the correct depth for each node.
  3928. Also implemented by:
  3929. <Graph>.
  3930. Note:
  3931. The depth of each node can then be accessed by
  3932. >node._depth
  3933. Parameters:
  3934. graph - (object) A <Graph>.
  3935. id - (string) A starting node id for the BFS traversal.
  3936. startDepth - (optional|number) A minimum depth value. Default's 0.
  3937. */
  3938. computeLevels: function(graph, id, startDepth, flags) {
  3939. startDepth = startDepth || 0;
  3940. var filter = this.filter(flags);
  3941. this.eachNode(graph, function(elem) {
  3942. elem._flag = false;
  3943. elem._depth = -1;
  3944. }, flags);
  3945. var root = graph.getNode(id);
  3946. root._depth = startDepth;
  3947. var queue = [root];
  3948. while(queue.length != 0) {
  3949. var node = queue.pop();
  3950. node._flag = true;
  3951. this.eachAdjacency(node, function(adj) {
  3952. var n = adj.nodeTo;
  3953. if(n._flag == false && filter(n)) {
  3954. if(n._depth < 0) n._depth = node._depth + 1 + startDepth;
  3955. queue.unshift(n);
  3956. }
  3957. }, flags);
  3958. }
  3959. },
  3960. /*
  3961. Method: eachBFS
  3962. Performs a BFS traversal applying *action* to each <Graph.Node>.
  3963. Also implemented by:
  3964. <Graph>.
  3965. Parameters:
  3966. graph - (object) A <Graph>.
  3967. id - (string) A starting node id for the BFS traversal.
  3968. action - (function) A callback function having a <Graph.Node> as first formal parameter.
  3969. Example:
  3970. (start code js)
  3971. $jit.Graph.Util.eachBFS(graph, 'mynodeid', function(node) {
  3972. alert(node.name);
  3973. });
  3974. //or...
  3975. graph.eachBFS('mynodeid', function(node) {
  3976. alert(node.name);
  3977. });
  3978. (end code)
  3979. */
  3980. eachBFS: function(graph, id, action, flags) {
  3981. var filter = this.filter(flags);
  3982. this.clean(graph);
  3983. var queue = [graph.getNode(id)];
  3984. while(queue.length != 0) {
  3985. var node = queue.pop();
  3986. node._flag = true;
  3987. action(node, node._depth);
  3988. this.eachAdjacency(node, function(adj) {
  3989. var n = adj.nodeTo;
  3990. if(n._flag == false && filter(n)) {
  3991. n._flag = true;
  3992. queue.unshift(n);
  3993. }
  3994. }, flags);
  3995. }
  3996. },
  3997. /*
  3998. Method: eachLevel
  3999. Iterates over a node's subgraph applying *action* to the nodes of relative depth between *levelBegin* and *levelEnd*.
  4000. Also implemented by:
  4001. <Graph.Node>.
  4002. Parameters:
  4003. node - (object) A <Graph.Node>.
  4004. levelBegin - (number) A relative level value.
  4005. levelEnd - (number) A relative level value.
  4006. action - (function) A callback function having a <Graph.Node> as first formal parameter.
  4007. */
  4008. eachLevel: function(node, levelBegin, levelEnd, action, flags) {
  4009. var d = node._depth, filter = this.filter(flags), that = this;
  4010. levelEnd = levelEnd === false? Number.MAX_VALUE -d : levelEnd;
  4011. (function loopLevel(node, levelBegin, levelEnd) {
  4012. var d = node._depth;
  4013. if(d >= levelBegin && d <= levelEnd && filter(node)) action(node, d);
  4014. if(d < levelEnd) {
  4015. that.eachAdjacency(node, function(adj) {
  4016. var n = adj.nodeTo;
  4017. if(n._depth > d) loopLevel(n, levelBegin, levelEnd);
  4018. });
  4019. }
  4020. })(node, levelBegin + d, levelEnd + d);
  4021. },
  4022. /*
  4023. Method: eachSubgraph
  4024. Iterates over a node's children recursively.
  4025. Also implemented by:
  4026. <Graph.Node>.
  4027. Parameters:
  4028. node - (object) A <Graph.Node>.
  4029. action - (function) A callback function having a <Graph.Node> as first formal parameter.
  4030. Example:
  4031. (start code js)
  4032. $jit.Graph.Util.eachSubgraph(node, function(node) {
  4033. alert(node.name);
  4034. });
  4035. //or...
  4036. node.eachSubgraph(function(node) {
  4037. alert(node.name);
  4038. });
  4039. (end code)
  4040. */
  4041. eachSubgraph: function(node, action, flags) {
  4042. this.eachLevel(node, 0, false, action, flags);
  4043. },
  4044. /*
  4045. Method: eachSubnode
  4046. Iterates over a node's children (without deeper recursion).
  4047. Also implemented by:
  4048. <Graph.Node>.
  4049. Parameters:
  4050. node - (object) A <Graph.Node>.
  4051. action - (function) A callback function having a <Graph.Node> as first formal parameter.
  4052. Example:
  4053. (start code js)
  4054. $jit.Graph.Util.eachSubnode(node, function(node) {
  4055. alert(node.name);
  4056. });
  4057. //or...
  4058. node.eachSubnode(function(node) {
  4059. alert(node.name);
  4060. });
  4061. (end code)
  4062. */
  4063. eachSubnode: function(node, action, flags) {
  4064. this.eachLevel(node, 1, 1, action, flags);
  4065. },
  4066. /*
  4067. Method: anySubnode
  4068. Returns *true* if any subnode matches the given condition.
  4069. Also implemented by:
  4070. <Graph.Node>.
  4071. Parameters:
  4072. node - (object) A <Graph.Node>.
  4073. cond - (function) A callback function returning a Boolean instance. This function has as first formal parameter a <Graph.Node>.
  4074. Example:
  4075. (start code js)
  4076. $jit.Graph.Util.anySubnode(node, function(node) { return node.name == "mynodename"; });
  4077. //or...
  4078. node.anySubnode(function(node) { return node.name == 'mynodename'; });
  4079. (end code)
  4080. */
  4081. anySubnode: function(node, cond, flags) {
  4082. var flag = false;
  4083. cond = cond || $.lambda(true);
  4084. var c = $.type(cond) == 'string'? function(n) { return n[cond]; } : cond;
  4085. this.eachSubnode(node, function(elem) {
  4086. if(c(elem)) flag = true;
  4087. }, flags);
  4088. return flag;
  4089. },
  4090. /*
  4091. Method: getSubnodes
  4092. Collects all subnodes for a specified node.
  4093. The *level* parameter filters nodes having relative depth of *level* from the root node.
  4094. Also implemented by:
  4095. <Graph.Node>.
  4096. Parameters:
  4097. node - (object) A <Graph.Node>.
  4098. level - (optional|number) Default's *0*. A starting relative depth for collecting nodes.
  4099. Returns:
  4100. An array of nodes.
  4101. */
  4102. getSubnodes: function(node, level, flags) {
  4103. var ans = [], that = this;
  4104. level = level || 0;
  4105. var levelStart, levelEnd;
  4106. if($.type(level) == 'array') {
  4107. levelStart = level[0];
  4108. levelEnd = level[1];
  4109. } else {
  4110. levelStart = level;
  4111. levelEnd = Number.MAX_VALUE - node._depth;
  4112. }
  4113. this.eachLevel(node, levelStart, levelEnd, function(n) {
  4114. ans.push(n);
  4115. }, flags);
  4116. return ans;
  4117. },
  4118. /*
  4119. Method: getParents
  4120. Returns an Array of <Graph.Nodes> which are parents of the given node.
  4121. Also implemented by:
  4122. <Graph.Node>.
  4123. Parameters:
  4124. node - (object) A <Graph.Node>.
  4125. Returns:
  4126. An Array of <Graph.Nodes>.
  4127. Example:
  4128. (start code js)
  4129. var pars = $jit.Graph.Util.getParents(node);
  4130. //or...
  4131. var pars = node.getParents();
  4132. if(pars.length > 0) {
  4133. //do stuff with parents
  4134. }
  4135. (end code)
  4136. */
  4137. getParents: function(node) {
  4138. var ans = [];
  4139. this.eachAdjacency(node, function(adj) {
  4140. var n = adj.nodeTo;
  4141. if(n._depth < node._depth) ans.push(n);
  4142. });
  4143. return ans;
  4144. },
  4145. /*
  4146. Method: isDescendantOf
  4147. Returns a boolean indicating if some node is descendant of the node with the given id.
  4148. Also implemented by:
  4149. <Graph.Node>.
  4150. Parameters:
  4151. node - (object) A <Graph.Node>.
  4152. id - (string) A <Graph.Node> id.
  4153. Example:
  4154. (start code js)
  4155. $jit.Graph.Util.isDescendantOf(node, "nodeid"); //true|false
  4156. //or...
  4157. node.isDescendantOf('nodeid');//true|false
  4158. (end code)
  4159. */
  4160. isDescendantOf: function(node, id) {
  4161. if(node.id == id) return true;
  4162. var pars = this.getParents(node), ans = false;
  4163. for ( var i = 0; !ans && i < pars.length; i++) {
  4164. ans = ans || this.isDescendantOf(pars[i], id);
  4165. }
  4166. return ans;
  4167. },
  4168. /*
  4169. Method: clean
  4170. Cleans flags from nodes.
  4171. Also implemented by:
  4172. <Graph>.
  4173. Parameters:
  4174. graph - A <Graph> instance.
  4175. */
  4176. clean: function(graph) { this.eachNode(graph, function(elem) { elem._flag = false; }); },
  4177. /*
  4178. Method: getClosestNodeToOrigin
  4179. Returns the closest node to the center of canvas.
  4180. Also implemented by:
  4181. <Graph>.
  4182. Parameters:
  4183. graph - (object) A <Graph> instance.
  4184. prop - (optional|string) Default's 'current'. A <Graph.Node> position property. Possible properties are 'start', 'current' or 'end'.
  4185. */
  4186. getClosestNodeToOrigin: function(graph, prop, flags) {
  4187. return this.getClosestNodeToPos(graph, Polar.KER, prop, flags);
  4188. },
  4189. /*
  4190. Method: getClosestNodeToPos
  4191. Returns the closest node to the given position.
  4192. Also implemented by:
  4193. <Graph>.
  4194. Parameters:
  4195. graph - (object) A <Graph> instance.
  4196. pos - (object) A <Complex> or <Polar> instance.
  4197. prop - (optional|string) Default's *current*. A <Graph.Node> position property. Possible properties are 'start', 'current' or 'end'.
  4198. */
  4199. getClosestNodeToPos: function(graph, pos, prop, flags) {
  4200. var node = null;
  4201. prop = prop || 'current';
  4202. pos = pos && pos.getc(true) || Complex.KER;
  4203. var distance = function(a, b) {
  4204. var d1 = a.x - b.x, d2 = a.y - b.y;
  4205. return d1 * d1 + d2 * d2;
  4206. };
  4207. this.eachNode(graph, function(elem) {
  4208. node = (node == null || distance(elem.getPos(prop).getc(true), pos) < distance(
  4209. node.getPos(prop).getc(true), pos)) ? elem : node;
  4210. }, flags);
  4211. return node;
  4212. }
  4213. };
  4214. //Append graph methods to <Graph>
  4215. $.each(['get', 'getNode', 'each', 'eachNode', 'computeLevels', 'eachBFS', 'clean', 'getClosestNodeToPos', 'getClosestNodeToOrigin'], function(m) {
  4216. Graph.prototype[m] = function() {
  4217. return Graph.Util[m].apply(Graph.Util, [this].concat(Array.prototype.slice.call(arguments)));
  4218. };
  4219. });
  4220. //Append node methods to <Graph.Node>
  4221. $.each(['eachAdjacency', 'eachLevel', 'eachSubgraph', 'eachSubnode', 'anySubnode', 'getSubnodes', 'getParents', 'isDescendantOf'], function(m) {
  4222. Graph.Node.prototype[m] = function() {
  4223. return Graph.Util[m].apply(Graph.Util, [this].concat(Array.prototype.slice.call(arguments)));
  4224. };
  4225. });
  4226. /*
  4227. * File: Graph.Op.js
  4228. *
  4229. */
  4230. /*
  4231. Object: Graph.Op
  4232. Perform <Graph> operations like adding/removing <Graph.Nodes> or <Graph.Adjacences>,
  4233. morphing a <Graph> into another <Graph>, contracting or expanding subtrees, etc.
  4234. */
  4235. Graph.Op = {
  4236. options: {
  4237. type: 'nothing',
  4238. duration: 2000,
  4239. hideLabels: true,
  4240. fps:30
  4241. },
  4242. initialize: function(viz) {
  4243. this.viz = viz;
  4244. },
  4245. /*
  4246. Method: removeNode
  4247. Removes one or more <Graph.Nodes> from the visualization.
  4248. It can also perform several animations like fading sequentially, fading concurrently, iterating or replotting.
  4249. Parameters:
  4250. node - (string|array) The node's id. Can also be an array having many ids.
  4251. opt - (object) Animation options. It's an object with optional properties described below
  4252. type - (string) Default's *nothing*. Type of the animation. Can be "nothing", "replot", "fade:seq", "fade:con" or "iter".
  4253. duration - Described in <Options.Fx>.
  4254. fps - Described in <Options.Fx>.
  4255. transition - Described in <Options.Fx>.
  4256. hideLabels - (boolean) Default's *true*. Hide labels during the animation.
  4257. Example:
  4258. (start code js)
  4259. var viz = new $jit.Viz(options);
  4260. viz.op.removeNode('nodeId', {
  4261. type: 'fade:seq',
  4262. duration: 1000,
  4263. hideLabels: false,
  4264. transition: $jit.Trans.Quart.easeOut
  4265. });
  4266. //or also
  4267. viz.op.removeNode(['someId', 'otherId'], {
  4268. type: 'fade:con',
  4269. duration: 1500
  4270. });
  4271. (end code)
  4272. */
  4273. removeNode: function(node, opt) {
  4274. var viz = this.viz;
  4275. var options = $.merge(this.options, viz.controller, opt);
  4276. var n = $.splat(node);
  4277. var i, that, nodeObj;
  4278. switch(options.type) {
  4279. case 'nothing':
  4280. for(i=0; i<n.length; i++) viz.graph.removeNode(n[i]);
  4281. break;
  4282. case 'replot':
  4283. this.removeNode(n, { type: 'nothing' });
  4284. viz.labels.clearLabels();
  4285. viz.refresh(true);
  4286. break;
  4287. case 'fade:seq': case 'fade':
  4288. that = this;
  4289. //set alpha to 0 for nodes to remove.
  4290. for(i=0; i<n.length; i++) {
  4291. nodeObj = viz.graph.getNode(n[i]);
  4292. nodeObj.setData('alpha', 0, 'end');
  4293. }
  4294. viz.fx.animate($.merge(options, {
  4295. modes: ['node-property:alpha'],
  4296. onComplete: function() {
  4297. that.removeNode(n, { type: 'nothing' });
  4298. viz.labels.clearLabels();
  4299. viz.reposition();
  4300. viz.fx.animate($.merge(options, {
  4301. modes: ['linear']
  4302. }));
  4303. }
  4304. }));
  4305. break;
  4306. case 'fade:con':
  4307. that = this;
  4308. //set alpha to 0 for nodes to remove. Tag them for being ignored on computing positions.
  4309. for(i=0; i<n.length; i++) {
  4310. nodeObj = viz.graph.getNode(n[i]);
  4311. nodeObj.setData('alpha', 0, 'end');
  4312. nodeObj.ignore = true;
  4313. }
  4314. viz.reposition();
  4315. viz.fx.animate($.merge(options, {
  4316. modes: ['node-property:alpha', 'linear'],
  4317. onComplete: function() {
  4318. that.removeNode(n, { type: 'nothing' });
  4319. options.onComplete && options.onComplete();
  4320. }
  4321. }));
  4322. break;
  4323. case 'iter':
  4324. that = this;
  4325. viz.fx.sequence({
  4326. condition: function() { return n.length != 0; },
  4327. step: function() { that.removeNode(n.shift(), { type: 'nothing' }); viz.labels.clearLabels(); },
  4328. onComplete: function() { options.onComplete && options.onComplete(); },
  4329. duration: Math.ceil(options.duration / n.length)
  4330. });
  4331. break;
  4332. default: this.doError();
  4333. }
  4334. },
  4335. /*
  4336. Method: removeEdge
  4337. Removes one or more <Graph.Adjacences> from the visualization.
  4338. It can also perform several animations like fading sequentially, fading concurrently, iterating or replotting.
  4339. Parameters:
  4340. vertex - (array) An array having two strings which are the ids of the nodes connected by this edge (i.e ['id1', 'id2']). Can also be a two dimensional array holding many edges (i.e [['id1', 'id2'], ['id3', 'id4'], ...]).
  4341. opt - (object) Animation options. It's an object with optional properties described below
  4342. type - (string) Default's *nothing*. Type of the animation. Can be "nothing", "replot", "fade:seq", "fade:con" or "iter".
  4343. duration - Described in <Options.Fx>.
  4344. fps - Described in <Options.Fx>.
  4345. transition - Described in <Options.Fx>.
  4346. hideLabels - (boolean) Default's *true*. Hide labels during the animation.
  4347. Example:
  4348. (start code js)
  4349. var viz = new $jit.Viz(options);
  4350. viz.op.removeEdge(['nodeId', 'otherId'], {
  4351. type: 'fade:seq',
  4352. duration: 1000,
  4353. hideLabels: false,
  4354. transition: $jit.Trans.Quart.easeOut
  4355. });
  4356. //or also
  4357. viz.op.removeEdge([['someId', 'otherId'], ['id3', 'id4']], {
  4358. type: 'fade:con',
  4359. duration: 1500
  4360. });
  4361. (end code)
  4362. */
  4363. removeEdge: function(vertex, opt) {
  4364. var viz = this.viz;
  4365. var options = $.merge(this.options, viz.controller, opt);
  4366. var v = ($.type(vertex[0]) == 'string')? [vertex] : vertex;
  4367. var i, that, adj;
  4368. switch(options.type) {
  4369. case 'nothing':
  4370. for(i=0; i<v.length; i++) viz.graph.removeAdjacence(v[i][0], v[i][1]);
  4371. break;
  4372. case 'replot':
  4373. this.removeEdge(v, { type: 'nothing' });
  4374. viz.refresh(true);
  4375. break;
  4376. case 'fade:seq': case 'fade':
  4377. that = this;
  4378. //set alpha to 0 for edges to remove.
  4379. for(i=0; i<v.length; i++) {
  4380. adj = viz.graph.getAdjacence(v[i][0], v[i][1]);
  4381. if(adj) {
  4382. adj.setData('alpha', 0,'end');
  4383. }
  4384. }
  4385. viz.fx.animate($.merge(options, {
  4386. modes: ['edge-property:alpha'],
  4387. onComplete: function() {
  4388. that.removeEdge(v, { type: 'nothing' });
  4389. viz.reposition();
  4390. viz.fx.animate($.merge(options, {
  4391. modes: ['linear']
  4392. }));
  4393. }
  4394. }));
  4395. break;
  4396. case 'fade:con':
  4397. that = this;
  4398. //set alpha to 0 for nodes to remove. Tag them for being ignored when computing positions.
  4399. for(i=0; i<v.length; i++) {
  4400. adj = viz.graph.getAdjacence(v[i][0], v[i][1]);
  4401. if(adj) {
  4402. adj.setData('alpha',0 ,'end');
  4403. adj.ignore = true;
  4404. }
  4405. }
  4406. viz.reposition();
  4407. viz.fx.animate($.merge(options, {
  4408. modes: ['edge-property:alpha', 'linear'],
  4409. onComplete: function() {
  4410. that.removeEdge(v, { type: 'nothing' });
  4411. options.onComplete && options.onComplete();
  4412. }
  4413. }));
  4414. break;
  4415. case 'iter':
  4416. that = this;
  4417. viz.fx.sequence({
  4418. condition: function() { return v.length != 0; },
  4419. step: function() { that.removeEdge(v.shift(), { type: 'nothing' }); viz.labels.clearLabels(); },
  4420. onComplete: function() { options.onComplete(); },
  4421. duration: Math.ceil(options.duration / v.length)
  4422. });
  4423. break;
  4424. default: this.doError();
  4425. }
  4426. },
  4427. /*
  4428. Method: sum
  4429. Adds a new graph to the visualization.
  4430. The JSON graph (or tree) must at least have a common node with the current graph plotted by the visualization.
  4431. The resulting graph can be defined as follows <http://mathworld.wolfram.com/GraphSum.html>
  4432. Parameters:
  4433. json - (object) A json tree or graph structure. See also <Loader.loadJSON>.
  4434. opt - (object) Animation options. It's an object with optional properties described below
  4435. type - (string) Default's *nothing*. Type of the animation. Can be "nothing", "replot", "fade:seq", "fade:con".
  4436. duration - Described in <Options.Fx>.
  4437. fps - Described in <Options.Fx>.
  4438. transition - Described in <Options.Fx>.
  4439. hideLabels - (boolean) Default's *true*. Hide labels during the animation.
  4440. Example:
  4441. (start code js)
  4442. //...json contains a tree or graph structure...
  4443. var viz = new $jit.Viz(options);
  4444. viz.op.sum(json, {
  4445. type: 'fade:seq',
  4446. duration: 1000,
  4447. hideLabels: false,
  4448. transition: $jit.Trans.Quart.easeOut
  4449. });
  4450. //or also
  4451. viz.op.sum(json, {
  4452. type: 'fade:con',
  4453. duration: 1500
  4454. });
  4455. (end code)
  4456. */
  4457. sum: function(json, opt) {
  4458. var viz = this.viz;
  4459. var options = $.merge(this.options, viz.controller, opt), root = viz.root;
  4460. var graph;
  4461. viz.root = opt.id || viz.root;
  4462. switch(options.type) {
  4463. case 'nothing':
  4464. graph = viz.construct(json);
  4465. graph.eachNode(function(elem) {
  4466. elem.eachAdjacency(function(adj) {
  4467. viz.graph.addAdjacence(adj.nodeFrom, adj.nodeTo, adj.data);
  4468. });
  4469. });
  4470. break;
  4471. case 'replot':
  4472. viz.refresh(true);
  4473. this.sum(json, { type: 'nothing' });
  4474. viz.refresh(true);
  4475. break;
  4476. case 'fade:seq': case 'fade': case 'fade:con':
  4477. that = this;
  4478. graph = viz.construct(json);
  4479. //set alpha to 0 for nodes to add.
  4480. var fadeEdges = this.preprocessSum(graph);
  4481. var modes = !fadeEdges? ['node-property:alpha'] : ['node-property:alpha', 'edge-property:alpha'];
  4482. viz.reposition();
  4483. if(options.type != 'fade:con') {
  4484. viz.fx.animate($.merge(options, {
  4485. modes: ['linear'],
  4486. onComplete: function() {
  4487. viz.fx.animate($.merge(options, {
  4488. modes: modes,
  4489. onComplete: function() {
  4490. options.onComplete();
  4491. }
  4492. }));
  4493. }
  4494. }));
  4495. } else {
  4496. viz.graph.eachNode(function(elem) {
  4497. if (elem.id != root && elem.pos.isZero()) {
  4498. elem.pos.set(elem.endPos);
  4499. elem.startPos.set(elem.endPos);
  4500. }
  4501. });
  4502. viz.fx.animate($.merge(options, {
  4503. modes: ['linear'].concat(modes)
  4504. }));
  4505. }
  4506. break;
  4507. default: this.doError();
  4508. }
  4509. },
  4510. /*
  4511. Method: morph
  4512. This method will transform the current visualized graph into the new JSON representation passed in the method.
  4513. The JSON object must at least have the root node in common with the current visualized graph.
  4514. Parameters:
  4515. json - (object) A json tree or graph structure. See also <Loader.loadJSON>.
  4516. opt - (object) Animation options. It's an object with optional properties described below
  4517. type - (string) Default's *nothing*. Type of the animation. Can be "nothing", "replot", "fade:con".
  4518. duration - Described in <Options.Fx>.
  4519. fps - Described in <Options.Fx>.
  4520. transition - Described in <Options.Fx>.
  4521. hideLabels - (boolean) Default's *true*. Hide labels during the animation.
  4522. id - (string) The shared <Graph.Node> id between both graphs.
  4523. extraModes - (optional|object) When morphing with an animation, dollar prefixed data parameters are added to
  4524. *endData* and not *data* itself. This way you can animate dollar prefixed parameters during your morphing operation.
  4525. For animating these extra-parameters you have to specify an object that has animation groups as keys and animation
  4526. properties as values, just like specified in <Graph.Plot.animate>.
  4527. Example:
  4528. (start code js)
  4529. //...json contains a tree or graph structure...
  4530. var viz = new $jit.Viz(options);
  4531. viz.op.morph(json, {
  4532. type: 'fade',
  4533. duration: 1000,
  4534. hideLabels: false,
  4535. transition: $jit.Trans.Quart.easeOut
  4536. });
  4537. //or also
  4538. viz.op.morph(json, {
  4539. type: 'fade',
  4540. duration: 1500
  4541. });
  4542. //if the json data contains dollar prefixed params
  4543. //like $width or $height these too can be animated
  4544. viz.op.morph(json, {
  4545. type: 'fade',
  4546. duration: 1500
  4547. }, {
  4548. 'node-property': ['width', 'height']
  4549. });
  4550. (end code)
  4551. */
  4552. morph: function(json, opt, extraModes) {
  4553. extraModes = extraModes || {};
  4554. var viz = this.viz;
  4555. var options = $.merge(this.options, viz.controller, opt), root = viz.root;
  4556. var graph;
  4557. //TODO(nico) this hack makes morphing work with the Hypertree.
  4558. //Need to check if it has been solved and this can be removed.
  4559. viz.root = opt.id || viz.root;
  4560. switch(options.type) {
  4561. case 'nothing':
  4562. graph = viz.construct(json);
  4563. graph.eachNode(function(elem) {
  4564. var nodeExists = viz.graph.hasNode(elem.id);
  4565. elem.eachAdjacency(function(adj) {
  4566. var adjExists = !!viz.graph.getAdjacence(adj.nodeFrom.id, adj.nodeTo.id);
  4567. viz.graph.addAdjacence(adj.nodeFrom, adj.nodeTo, adj.data);
  4568. //Update data properties if the node existed
  4569. if(adjExists) {
  4570. var addedAdj = viz.graph.getAdjacence(adj.nodeFrom.id, adj.nodeTo.id);
  4571. for(var prop in (adj.data || {})) {
  4572. addedAdj.data[prop] = adj.data[prop];
  4573. }
  4574. }
  4575. });
  4576. //Update data properties if the node existed
  4577. if(nodeExists) {
  4578. var addedNode = viz.graph.getNode(elem.id);
  4579. for(var prop in (elem.data || {})) {
  4580. addedNode.data[prop] = elem.data[prop];
  4581. }
  4582. }
  4583. });
  4584. viz.graph.eachNode(function(elem) {
  4585. elem.eachAdjacency(function(adj) {
  4586. if(!graph.getAdjacence(adj.nodeFrom.id, adj.nodeTo.id)) {
  4587. viz.graph.removeAdjacence(adj.nodeFrom.id, adj.nodeTo.id);
  4588. }
  4589. });
  4590. if(!graph.hasNode(elem.id)) viz.graph.removeNode(elem.id);
  4591. });
  4592. break;
  4593. case 'replot':
  4594. viz.labels.clearLabels(true);
  4595. this.morph(json, { type: 'nothing' });
  4596. viz.refresh(true);
  4597. viz.refresh(true);
  4598. break;
  4599. case 'fade:seq': case 'fade': case 'fade:con':
  4600. that = this;
  4601. graph = viz.construct(json);
  4602. //preprocessing for nodes to delete.
  4603. //get node property modes to interpolate
  4604. var nodeModes = ('node-property' in extraModes)
  4605. && $.map($.splat(extraModes['node-property']),
  4606. function(n) { return '$' + n; });
  4607. viz.graph.eachNode(function(elem) {
  4608. var graphNode = graph.getNode(elem.id);
  4609. if(!graphNode) {
  4610. elem.setData('alpha', 1);
  4611. elem.setData('alpha', 1, 'start');
  4612. elem.setData('alpha', 0, 'end');
  4613. elem.ignore = true;
  4614. } else {
  4615. //Update node data information
  4616. var graphNodeData = graphNode.data;
  4617. for(var prop in graphNodeData) {
  4618. if(nodeModes && ($.indexOf(nodeModes, prop) > -1)) {
  4619. elem.endData[prop] = graphNodeData[prop];
  4620. } else {
  4621. elem.data[prop] = graphNodeData[prop];
  4622. }
  4623. }
  4624. }
  4625. });
  4626. viz.graph.eachNode(function(elem) {
  4627. if(elem.ignore) return;
  4628. elem.eachAdjacency(function(adj) {
  4629. if(adj.nodeFrom.ignore || adj.nodeTo.ignore) return;
  4630. var nodeFrom = graph.getNode(adj.nodeFrom.id);
  4631. var nodeTo = graph.getNode(adj.nodeTo.id);
  4632. if(!nodeFrom.adjacentTo(nodeTo)) {
  4633. var adj = viz.graph.getAdjacence(nodeFrom.id, nodeTo.id);
  4634. fadeEdges = true;
  4635. adj.setData('alpha', 1);
  4636. adj.setData('alpha', 1, 'start');
  4637. adj.setData('alpha', 0, 'end');
  4638. }
  4639. });
  4640. });
  4641. //preprocessing for adding nodes.
  4642. var fadeEdges = this.preprocessSum(graph);
  4643. var modes = !fadeEdges? ['node-property:alpha'] :
  4644. ['node-property:alpha',
  4645. 'edge-property:alpha'];
  4646. //Append extra node-property animations (if any)
  4647. modes[0] = modes[0] + (('node-property' in extraModes)?
  4648. (':' + $.splat(extraModes['node-property']).join(':')) : '');
  4649. //Append extra edge-property animations (if any)
  4650. modes[1] = (modes[1] || 'edge-property:alpha') + (('edge-property' in extraModes)?
  4651. (':' + $.splat(extraModes['edge-property']).join(':')) : '');
  4652. //Add label-property animations (if any)
  4653. if('label-property' in extraModes) {
  4654. modes.push('label-property:' + $.splat(extraModes['label-property']).join(':'))
  4655. }
  4656. //only use reposition if its implemented.
  4657. if (viz.reposition) {
  4658. viz.reposition();
  4659. } else {
  4660. viz.compute('end');
  4661. }
  4662. viz.graph.eachNode(function(elem) {
  4663. if (elem.id != root && elem.pos.getp().equals(Polar.KER)) {
  4664. elem.pos.set(elem.endPos); elem.startPos.set(elem.endPos);
  4665. }
  4666. });
  4667. viz.fx.animate($.merge(options, {
  4668. modes: [extraModes.position || 'polar'].concat(modes),
  4669. onComplete: function() {
  4670. viz.graph.eachNode(function(elem) {
  4671. if(elem.ignore) viz.graph.removeNode(elem.id);
  4672. });
  4673. viz.graph.eachNode(function(elem) {
  4674. elem.eachAdjacency(function(adj) {
  4675. if(adj.ignore) viz.graph.removeAdjacence(adj.nodeFrom.id, adj.nodeTo.id);
  4676. });
  4677. });
  4678. options.onComplete();
  4679. }
  4680. }));
  4681. break;
  4682. default:;
  4683. }
  4684. },
  4685. /*
  4686. Method: contract
  4687. Collapses the subtree of the given node. The node will have a _collapsed=true_ property.
  4688. Parameters:
  4689. node - (object) A <Graph.Node>.
  4690. opt - (object) An object containing options described below
  4691. type - (string) Whether to 'replot' or 'animate' the contraction.
  4692. There are also a number of Animation options. For more information see <Options.Fx>.
  4693. Example:
  4694. (start code js)
  4695. var viz = new $jit.Viz(options);
  4696. viz.op.contract(node, {
  4697. type: 'animate',
  4698. duration: 1000,
  4699. hideLabels: true,
  4700. transition: $jit.Trans.Quart.easeOut
  4701. });
  4702. (end code)
  4703. */
  4704. contract: function(node, opt) {
  4705. var viz = this.viz;
  4706. if(node.collapsed || !node.anySubnode($.lambda(true))) return;
  4707. opt = $.merge(this.options, viz.config, opt || {}, {
  4708. 'modes': ['node-property:alpha:span', 'linear']
  4709. });
  4710. node.collapsed = true;
  4711. (function subn(n) {
  4712. n.eachSubnode(function(ch) {
  4713. ch.ignore = true;
  4714. ch.setData('alpha', 0, opt.type == 'animate'? 'end' : 'current');
  4715. subn(ch);
  4716. });
  4717. })(node);
  4718. if(opt.type == 'animate') {
  4719. viz.compute('end');
  4720. if(viz.rotated) {
  4721. viz.rotate(viz.rotated, 'none', {
  4722. 'property':'end'
  4723. });
  4724. }
  4725. (function subn(n) {
  4726. n.eachSubnode(function(ch) {
  4727. ch.setPos(node.getPos('end'), 'end');
  4728. subn(ch);
  4729. });
  4730. })(node);
  4731. viz.fx.animate(opt);
  4732. } else if(opt.type == 'replot'){
  4733. viz.refresh();
  4734. }
  4735. },
  4736. /*
  4737. Method: expand
  4738. Expands the previously contracted subtree. The given node must have the _collapsed=true_ property.
  4739. Parameters:
  4740. node - (object) A <Graph.Node>.
  4741. opt - (object) An object containing options described below
  4742. type - (string) Whether to 'replot' or 'animate'.
  4743. There are also a number of Animation options. For more information see <Options.Fx>.
  4744. Example:
  4745. (start code js)
  4746. var viz = new $jit.Viz(options);
  4747. viz.op.expand(node, {
  4748. type: 'animate',
  4749. duration: 1000,
  4750. hideLabels: true,
  4751. transition: $jit.Trans.Quart.easeOut
  4752. });
  4753. (end code)
  4754. */
  4755. expand: function(node, opt) {
  4756. if(!('collapsed' in node)) return;
  4757. var viz = this.viz;
  4758. opt = $.merge(this.options, viz.config, opt || {}, {
  4759. 'modes': ['node-property:alpha:span', 'linear']
  4760. });
  4761. delete node.collapsed;
  4762. (function subn(n) {
  4763. n.eachSubnode(function(ch) {
  4764. delete ch.ignore;
  4765. ch.setData('alpha', 1, opt.type == 'animate'? 'end' : 'current');
  4766. subn(ch);
  4767. });
  4768. })(node);
  4769. if(opt.type == 'animate') {
  4770. viz.compute('end');
  4771. if(viz.rotated) {
  4772. viz.rotate(viz.rotated, 'none', {
  4773. 'property':'end'
  4774. });
  4775. }
  4776. viz.fx.animate(opt);
  4777. } else if(opt.type == 'replot'){
  4778. viz.refresh();
  4779. }
  4780. },
  4781. preprocessSum: function(graph) {
  4782. var viz = this.viz;
  4783. graph.eachNode(function(elem) {
  4784. if(!viz.graph.hasNode(elem.id)) {
  4785. viz.graph.addNode(elem);
  4786. var n = viz.graph.getNode(elem.id);
  4787. n.setData('alpha', 0);
  4788. n.setData('alpha', 0, 'start');
  4789. n.setData('alpha', 1, 'end');
  4790. }
  4791. });
  4792. var fadeEdges = false;
  4793. graph.eachNode(function(elem) {
  4794. elem.eachAdjacency(function(adj) {
  4795. var nodeFrom = viz.graph.getNode(adj.nodeFrom.id);
  4796. var nodeTo = viz.graph.getNode(adj.nodeTo.id);
  4797. if(!nodeFrom.adjacentTo(nodeTo)) {
  4798. var adj = viz.graph.addAdjacence(nodeFrom, nodeTo, adj.data);
  4799. if(nodeFrom.startAlpha == nodeFrom.endAlpha
  4800. && nodeTo.startAlpha == nodeTo.endAlpha) {
  4801. fadeEdges = true;
  4802. adj.setData('alpha', 0);
  4803. adj.setData('alpha', 0, 'start');
  4804. adj.setData('alpha', 1, 'end');
  4805. }
  4806. }
  4807. });
  4808. });
  4809. return fadeEdges;
  4810. }
  4811. };
  4812. /*
  4813. File: Helpers.js
  4814. Helpers are objects that contain rendering primitives (like rectangles, ellipses, etc), for plotting nodes and edges.
  4815. Helpers also contain implementations of the *contains* method, a method returning a boolean indicating whether the mouse
  4816. position is over the rendered shape.
  4817. Helpers are very useful when implementing new NodeTypes, since you can access them through *this.nodeHelper* and
  4818. *this.edgeHelper* <Graph.Plot> properties, providing you with simple primitives and mouse-position check functions.
  4819. Example:
  4820. (start code js)
  4821. //implement a new node type
  4822. $jit.Viz.Plot.NodeTypes.implement({
  4823. 'customNodeType': {
  4824. 'render': function(node, canvas) {
  4825. this.nodeHelper.circle.render ...
  4826. },
  4827. 'contains': function(node, pos) {
  4828. this.nodeHelper.circle.contains ...
  4829. }
  4830. }
  4831. });
  4832. //implement an edge type
  4833. $jit.Viz.Plot.EdgeTypes.implement({
  4834. 'customNodeType': {
  4835. 'render': function(node, canvas) {
  4836. this.edgeHelper.circle.render ...
  4837. },
  4838. //optional
  4839. 'contains': function(node, pos) {
  4840. this.edgeHelper.circle.contains ...
  4841. }
  4842. }
  4843. });
  4844. (end code)
  4845. */
  4846. /*
  4847. Object: NodeHelper
  4848. Contains rendering and other type of primitives for simple shapes.
  4849. */
  4850. var NodeHelper = {
  4851. 'none': {
  4852. 'render': $.empty,
  4853. 'contains': $.lambda(false)
  4854. },
  4855. /*
  4856. Object: NodeHelper.circle
  4857. */
  4858. 'circle': {
  4859. /*
  4860. Method: render
  4861. Renders a circle into the canvas.
  4862. Parameters:
  4863. type - (string) Possible options are 'fill' or 'stroke'.
  4864. pos - (object) An *x*, *y* object with the position of the center of the circle.
  4865. radius - (number) The radius of the circle to be rendered.
  4866. canvas - (object) A <Canvas> instance.
  4867. Example:
  4868. (start code js)
  4869. NodeHelper.circle.render('fill', { x: 10, y: 30 }, 30, viz.canvas);
  4870. (end code)
  4871. */
  4872. 'render': function(type, pos, radius, canvas){
  4873. var ctx = canvas.getCtx();
  4874. ctx.beginPath();
  4875. ctx.arc(pos.x, pos.y, radius, 0, Math.PI * 2, true);
  4876. ctx.closePath();
  4877. ctx[type]();
  4878. },
  4879. /*
  4880. Method: contains
  4881. Returns *true* if *pos* is contained in the area of the shape. Returns *false* otherwise.
  4882. Parameters:
  4883. npos - (object) An *x*, *y* object with the <Graph.Node> position.
  4884. pos - (object) An *x*, *y* object with the position to check.
  4885. radius - (number) The radius of the rendered circle.
  4886. Example:
  4887. (start code js)
  4888. NodeHelper.circle.contains({ x: 10, y: 30 }, { x: 15, y: 35 }, 30); //true
  4889. (end code)
  4890. */
  4891. 'contains': function(npos, pos, radius){
  4892. var diffx = npos.x - pos.x,
  4893. diffy = npos.y - pos.y,
  4894. diff = diffx * diffx + diffy * diffy;
  4895. return diff <= radius * radius;
  4896. }
  4897. },
  4898. /*
  4899. Object: NodeHelper.ellipse
  4900. */
  4901. 'ellipse': {
  4902. /*
  4903. Method: render
  4904. Renders an ellipse into the canvas.
  4905. Parameters:
  4906. type - (string) Possible options are 'fill' or 'stroke'.
  4907. pos - (object) An *x*, *y* object with the position of the center of the ellipse.
  4908. width - (number) The width of the ellipse.
  4909. height - (number) The height of the ellipse.
  4910. canvas - (object) A <Canvas> instance.
  4911. Example:
  4912. (start code js)
  4913. NodeHelper.ellipse.render('fill', { x: 10, y: 30 }, 30, 40, viz.canvas);
  4914. (end code)
  4915. */
  4916. 'render': function(type, pos, width, height, canvas){
  4917. var ctx = canvas.getCtx(),
  4918. scalex = 1,
  4919. scaley = 1,
  4920. scaleposx = 1,
  4921. scaleposy = 1,
  4922. radius = 0;
  4923. if (width > height) {
  4924. radius = width / 2;
  4925. scaley = height / width;
  4926. scaleposy = width / height;
  4927. } else {
  4928. radius = height / 2;
  4929. scalex = width / height;
  4930. scaleposx = height / width;
  4931. }
  4932. ctx.save();
  4933. ctx.scale(scalex, scaley);
  4934. ctx.beginPath();
  4935. ctx.arc(pos.x * scaleposx, pos.y * scaleposy, radius, 0, Math.PI * 2, true);
  4936. ctx.closePath();
  4937. ctx[type]();
  4938. ctx.restore();
  4939. },
  4940. /*
  4941. Method: contains
  4942. Returns *true* if *pos* is contained in the area of the shape. Returns *false* otherwise.
  4943. Parameters:
  4944. npos - (object) An *x*, *y* object with the <Graph.Node> position.
  4945. pos - (object) An *x*, *y* object with the position to check.
  4946. width - (number) The width of the rendered ellipse.
  4947. height - (number) The height of the rendered ellipse.
  4948. Example:
  4949. (start code js)
  4950. NodeHelper.ellipse.contains({ x: 10, y: 30 }, { x: 15, y: 35 }, 30, 40);
  4951. (end code)
  4952. */
  4953. 'contains': function(npos, pos, width, height){
  4954. var radius = 0,
  4955. scalex = 1,
  4956. scaley = 1,
  4957. diffx = 0,
  4958. diffy = 0,
  4959. diff = 0;
  4960. if (width > height) {
  4961. radius = width / 2;
  4962. scaley = height / width;
  4963. } else {
  4964. radius = height / 2;
  4965. scalex = width / height;
  4966. }
  4967. diffx = (npos.x - pos.x) * (1 / scalex);
  4968. diffy = (npos.y - pos.y) * (1 / scaley);
  4969. diff = diffx * diffx + diffy * diffy;
  4970. return diff <= radius * radius;
  4971. }
  4972. },
  4973. /*
  4974. Object: NodeHelper.square
  4975. */
  4976. 'square': {
  4977. /*
  4978. Method: render
  4979. Renders a square into the canvas.
  4980. Parameters:
  4981. type - (string) Possible options are 'fill' or 'stroke'.
  4982. pos - (object) An *x*, *y* object with the position of the center of the square.
  4983. dim - (number) The radius (or half-diameter) of the square.
  4984. canvas - (object) A <Canvas> instance.
  4985. Example:
  4986. (start code js)
  4987. NodeHelper.square.render('stroke', { x: 10, y: 30 }, 40, viz.canvas);
  4988. (end code)
  4989. */
  4990. 'render': function(type, pos, dim, canvas){
  4991. canvas.getCtx()[type + "Rect"](pos.x - dim, pos.y - dim, 2*dim, 2*dim);
  4992. },
  4993. /*
  4994. Method: contains
  4995. Returns *true* if *pos* is contained in the area of the shape. Returns *false* otherwise.
  4996. Parameters:
  4997. npos - (object) An *x*, *y* object with the <Graph.Node> position.
  4998. pos - (object) An *x*, *y* object with the position to check.
  4999. dim - (number) The radius (or half-diameter) of the square.
  5000. Example:
  5001. (start code js)
  5002. NodeHelper.square.contains({ x: 10, y: 30 }, { x: 15, y: 35 }, 30);
  5003. (end code)
  5004. */
  5005. 'contains': function(npos, pos, dim){
  5006. return Math.abs(pos.x - npos.x) <= dim && Math.abs(pos.y - npos.y) <= dim;
  5007. }
  5008. },
  5009. /*
  5010. Object: NodeHelper.rectangle
  5011. */
  5012. 'rectangle': {
  5013. /*
  5014. Method: render
  5015. Renders a rectangle into the canvas.
  5016. Parameters:
  5017. type - (string) Possible options are 'fill' or 'stroke'.
  5018. pos - (object) An *x*, *y* object with the position of the center of the rectangle.
  5019. width - (number) The width of the rectangle.
  5020. height - (number) The height of the rectangle.
  5021. canvas - (object) A <Canvas> instance.
  5022. Example:
  5023. (start code js)
  5024. NodeHelper.rectangle.render('fill', { x: 10, y: 30 }, 30, 40, viz.canvas);
  5025. (end code)
  5026. */
  5027. 'render': function(type, pos, width, height, canvas){
  5028. canvas.getCtx()[type + "Rect"](pos.x - width / 2, pos.y - height / 2,
  5029. width, height);
  5030. },
  5031. /*
  5032. Method: contains
  5033. Returns *true* if *pos* is contained in the area of the shape. Returns *false* otherwise.
  5034. Parameters:
  5035. npos - (object) An *x*, *y* object with the <Graph.Node> position.
  5036. pos - (object) An *x*, *y* object with the position to check.
  5037. width - (number) The width of the rendered rectangle.
  5038. height - (number) The height of the rendered rectangle.
  5039. Example:
  5040. (start code js)
  5041. NodeHelper.rectangle.contains({ x: 10, y: 30 }, { x: 15, y: 35 }, 30, 40);
  5042. (end code)
  5043. */
  5044. 'contains': function(npos, pos, width, height){
  5045. return Math.abs(pos.x - npos.x) <= width / 2
  5046. && Math.abs(pos.y - npos.y) <= height / 2;
  5047. }
  5048. },
  5049. /*
  5050. Object: NodeHelper.triangle
  5051. */
  5052. 'triangle': {
  5053. /*
  5054. Method: render
  5055. Renders a triangle into the canvas.
  5056. Parameters:
  5057. type - (string) Possible options are 'fill' or 'stroke'.
  5058. pos - (object) An *x*, *y* object with the position of the center of the triangle.
  5059. dim - (number) Half the base and half the height of the triangle.
  5060. canvas - (object) A <Canvas> instance.
  5061. Example:
  5062. (start code js)
  5063. NodeHelper.triangle.render('stroke', { x: 10, y: 30 }, 40, viz.canvas);
  5064. (end code)
  5065. */
  5066. 'render': function(type, pos, dim, canvas){
  5067. var ctx = canvas.getCtx(),
  5068. c1x = pos.x,
  5069. c1y = pos.y - dim,
  5070. c2x = c1x - dim,
  5071. c2y = pos.y + dim,
  5072. c3x = c1x + dim,
  5073. c3y = c2y;
  5074. ctx.beginPath();
  5075. ctx.moveTo(c1x, c1y);
  5076. ctx.lineTo(c2x, c2y);
  5077. ctx.lineTo(c3x, c3y);
  5078. ctx.closePath();
  5079. ctx[type]();
  5080. },
  5081. /*
  5082. Method: contains
  5083. Returns *true* if *pos* is contained in the area of the shape. Returns *false* otherwise.
  5084. Parameters:
  5085. npos - (object) An *x*, *y* object with the <Graph.Node> position.
  5086. pos - (object) An *x*, *y* object with the position to check.
  5087. dim - (number) Half the base and half the height of the triangle.
  5088. Example:
  5089. (start code js)
  5090. NodeHelper.triangle.contains({ x: 10, y: 30 }, { x: 15, y: 35 }, 30);
  5091. (end code)
  5092. */
  5093. 'contains': function(npos, pos, dim) {
  5094. return NodeHelper.circle.contains(npos, pos, dim);
  5095. }
  5096. },
  5097. /*
  5098. Object: NodeHelper.star
  5099. */
  5100. 'star': {
  5101. /*
  5102. Method: render
  5103. Renders a star (concave decagon) into the canvas.
  5104. Parameters:
  5105. type - (string) Possible options are 'fill' or 'stroke'.
  5106. pos - (object) An *x*, *y* object with the position of the center of the star.
  5107. dim - (number) The length of a side of a concave decagon.
  5108. canvas - (object) A <Canvas> instance.
  5109. Example:
  5110. (start code js)
  5111. NodeHelper.star.render('stroke', { x: 10, y: 30 }, 40, viz.canvas);
  5112. (end code)
  5113. */
  5114. 'render': function(type, pos, dim, canvas){
  5115. var ctx = canvas.getCtx(),
  5116. pi5 = Math.PI / 5;
  5117. ctx.save();
  5118. ctx.translate(pos.x, pos.y);
  5119. ctx.beginPath();
  5120. ctx.moveTo(dim, 0);
  5121. for (var i = 0; i < 9; i++) {
  5122. ctx.rotate(pi5);
  5123. if (i % 2 == 0) {
  5124. ctx.lineTo((dim / 0.525731) * 0.200811, 0);
  5125. } else {
  5126. ctx.lineTo(dim, 0);
  5127. }
  5128. }
  5129. ctx.closePath();
  5130. ctx[type]();
  5131. ctx.restore();
  5132. },
  5133. /*
  5134. Method: contains
  5135. Returns *true* if *pos* is contained in the area of the shape. Returns *false* otherwise.
  5136. Parameters:
  5137. npos - (object) An *x*, *y* object with the <Graph.Node> position.
  5138. pos - (object) An *x*, *y* object with the position to check.
  5139. dim - (number) The length of a side of a concave decagon.
  5140. Example:
  5141. (start code js)
  5142. NodeHelper.star.contains({ x: 10, y: 30 }, { x: 15, y: 35 }, 30);
  5143. (end code)
  5144. */
  5145. 'contains': function(npos, pos, dim) {
  5146. return NodeHelper.circle.contains(npos, pos, dim);
  5147. }
  5148. }
  5149. };
  5150. /*
  5151. Object: EdgeHelper
  5152. Contains rendering primitives for simple edge shapes.
  5153. */
  5154. var EdgeHelper = {
  5155. /*
  5156. Object: EdgeHelper.line
  5157. */
  5158. 'line': {
  5159. /*
  5160. Method: render
  5161. Renders a line into the canvas.
  5162. Parameters:
  5163. from - (object) An *x*, *y* object with the starting position of the line.
  5164. to - (object) An *x*, *y* object with the ending position of the line.
  5165. canvas - (object) A <Canvas> instance.
  5166. Example:
  5167. (start code js)
  5168. EdgeHelper.line.render({ x: 10, y: 30 }, { x: 10, y: 50 }, viz.canvas);
  5169. (end code)
  5170. */
  5171. 'render': function(from, to, canvas){
  5172. var ctx = canvas.getCtx();
  5173. ctx.beginPath();
  5174. ctx.moveTo(from.x, from.y);
  5175. ctx.lineTo(to.x, to.y);
  5176. ctx.stroke();
  5177. },
  5178. /*
  5179. Method: contains
  5180. Returns *true* if *pos* is contained in the area of the shape. Returns *false* otherwise.
  5181. Parameters:
  5182. posFrom - (object) An *x*, *y* object with a <Graph.Node> position.
  5183. posTo - (object) An *x*, *y* object with a <Graph.Node> position.
  5184. pos - (object) An *x*, *y* object with the position to check.
  5185. epsilon - (number) The dimension of the shape.
  5186. Example:
  5187. (start code js)
  5188. EdgeHelper.line.contains({ x: 10, y: 30 }, { x: 15, y: 35 }, { x: 15, y: 35 }, 30);
  5189. (end code)
  5190. */
  5191. 'contains': function(posFrom, posTo, pos, epsilon) {
  5192. var min = Math.min,
  5193. max = Math.max,
  5194. minPosX = min(posFrom.x, posTo.x),
  5195. maxPosX = max(posFrom.x, posTo.x),
  5196. minPosY = min(posFrom.y, posTo.y),
  5197. maxPosY = max(posFrom.y, posTo.y);
  5198. if(pos.x >= minPosX && pos.x <= maxPosX
  5199. && pos.y >= minPosY && pos.y <= maxPosY) {
  5200. if(Math.abs(posTo.x - posFrom.x) <= epsilon) {
  5201. return true;
  5202. }
  5203. var dist = (posTo.y - posFrom.y) / (posTo.x - posFrom.x) * (pos.x - posFrom.x) + posFrom.y;
  5204. return Math.abs(dist - pos.y) <= epsilon;
  5205. }
  5206. return false;
  5207. }
  5208. },
  5209. /*
  5210. Object: EdgeHelper.arrow
  5211. */
  5212. 'arrow': {
  5213. /*
  5214. Method: render
  5215. Renders an arrow into the canvas.
  5216. Parameters:
  5217. from - (object) An *x*, *y* object with the starting position of the arrow.
  5218. to - (object) An *x*, *y* object with the ending position of the arrow.
  5219. dim - (number) The dimension of the arrow.
  5220. swap - (boolean) Whether to set the arrow pointing to the starting position or the ending position.
  5221. canvas - (object) A <Canvas> instance.
  5222. Example:
  5223. (start code js)
  5224. EdgeHelper.arrow.render({ x: 10, y: 30 }, { x: 10, y: 50 }, 13, false, viz.canvas);
  5225. (end code)
  5226. */
  5227. 'render': function(from, to, dim, swap, canvas){
  5228. var ctx = canvas.getCtx();
  5229. // invert edge direction
  5230. if (swap) {
  5231. var tmp = from;
  5232. from = to;
  5233. to = tmp;
  5234. }
  5235. var vect = new Complex(to.x - from.x, to.y - from.y);
  5236. vect.$scale(dim / vect.norm());
  5237. var intermediatePoint = new Complex(to.x - vect.x, to.y - vect.y),
  5238. normal = new Complex(-vect.y / 2, vect.x / 2),
  5239. v1 = intermediatePoint.add(normal),
  5240. v2 = intermediatePoint.$add(normal.$scale(-1));
  5241. ctx.beginPath();
  5242. ctx.moveTo(from.x, from.y);
  5243. ctx.lineTo(to.x, to.y);
  5244. ctx.stroke();
  5245. ctx.beginPath();
  5246. ctx.moveTo(v1.x, v1.y);
  5247. ctx.lineTo(v2.x, v2.y);
  5248. ctx.lineTo(to.x, to.y);
  5249. ctx.closePath();
  5250. ctx.fill();
  5251. },
  5252. /*
  5253. Method: contains
  5254. Returns *true* if *pos* is contained in the area of the shape. Returns *false* otherwise.
  5255. Parameters:
  5256. posFrom - (object) An *x*, *y* object with a <Graph.Node> position.
  5257. posTo - (object) An *x*, *y* object with a <Graph.Node> position.
  5258. pos - (object) An *x*, *y* object with the position to check.
  5259. epsilon - (number) The dimension of the shape.
  5260. Example:
  5261. (start code js)
  5262. EdgeHelper.arrow.contains({ x: 10, y: 30 }, { x: 15, y: 35 }, { x: 15, y: 35 }, 30);
  5263. (end code)
  5264. */
  5265. 'contains': function(posFrom, posTo, pos, epsilon) {
  5266. return EdgeHelper.line.contains(posFrom, posTo, pos, epsilon);
  5267. }
  5268. },
  5269. /*
  5270. Object: EdgeHelper.hyperline
  5271. */
  5272. 'hyperline': {
  5273. /*
  5274. Method: render
  5275. Renders a hyperline into the canvas. A hyperline are the lines drawn for the <Hypertree> visualization.
  5276. Parameters:
  5277. from - (object) An *x*, *y* object with the starting position of the hyperline. *x* and *y* must belong to [0, 1).
  5278. to - (object) An *x*, *y* object with the ending position of the hyperline. *x* and *y* must belong to [0, 1).
  5279. r - (number) The scaling factor.
  5280. canvas - (object) A <Canvas> instance.
  5281. Example:
  5282. (start code js)
  5283. EdgeHelper.hyperline.render({ x: 10, y: 30 }, { x: 10, y: 50 }, 100, viz.canvas);
  5284. (end code)
  5285. */
  5286. 'render': function(from, to, r, canvas){
  5287. var ctx = canvas.getCtx();
  5288. var centerOfCircle = computeArcThroughTwoPoints(from, to);
  5289. if (centerOfCircle.a > 1000 || centerOfCircle.b > 1000
  5290. || centerOfCircle.ratio < 0) {
  5291. ctx.beginPath();
  5292. ctx.moveTo(from.x * r, from.y * r);
  5293. ctx.lineTo(to.x * r, to.y * r);
  5294. ctx.stroke();
  5295. } else {
  5296. var angleBegin = Math.atan2(to.y - centerOfCircle.y, to.x
  5297. - centerOfCircle.x);
  5298. var angleEnd = Math.atan2(from.y - centerOfCircle.y, from.x
  5299. - centerOfCircle.x);
  5300. var sense = sense(angleBegin, angleEnd);
  5301. ctx.beginPath();
  5302. ctx.arc(centerOfCircle.x * r, centerOfCircle.y * r, centerOfCircle.ratio
  5303. * r, angleBegin, angleEnd, sense);
  5304. ctx.stroke();
  5305. }
  5306. /*
  5307. Calculates the arc parameters through two points.
  5308. More information in <http://en.wikipedia.org/wiki/Poincar%C3%A9_disc_model#Analytic_geometry_constructions_in_the_hyperbolic_plane>
  5309. Parameters:
  5310. p1 - A <Complex> instance.
  5311. p2 - A <Complex> instance.
  5312. scale - The Disk's diameter.
  5313. Returns:
  5314. An object containing some arc properties.
  5315. */
  5316. function computeArcThroughTwoPoints(p1, p2){
  5317. var aDen = (p1.x * p2.y - p1.y * p2.x), bDen = aDen;
  5318. var sq1 = p1.squaredNorm(), sq2 = p2.squaredNorm();
  5319. // Fall back to a straight line
  5320. if (aDen == 0)
  5321. return {
  5322. x: 0,
  5323. y: 0,
  5324. ratio: -1
  5325. };
  5326. var a = (p1.y * sq2 - p2.y * sq1 + p1.y - p2.y) / aDen;
  5327. var b = (p2.x * sq1 - p1.x * sq2 + p2.x - p1.x) / bDen;
  5328. var x = -a / 2;
  5329. var y = -b / 2;
  5330. var squaredRatio = (a * a + b * b) / 4 - 1;
  5331. // Fall back to a straight line
  5332. if (squaredRatio < 0)
  5333. return {
  5334. x: 0,
  5335. y: 0,
  5336. ratio: -1
  5337. };
  5338. var ratio = Math.sqrt(squaredRatio);
  5339. var out = {
  5340. x: x,
  5341. y: y,
  5342. ratio: ratio > 1000? -1 : ratio,
  5343. a: a,
  5344. b: b
  5345. };
  5346. return out;
  5347. }
  5348. /*
  5349. Sets angle direction to clockwise (true) or counterclockwise (false).
  5350. Parameters:
  5351. angleBegin - Starting angle for drawing the arc.
  5352. angleEnd - The HyperLine will be drawn from angleBegin to angleEnd.
  5353. Returns:
  5354. A Boolean instance describing the sense for drawing the HyperLine.
  5355. */
  5356. function sense(angleBegin, angleEnd){
  5357. return (angleBegin < angleEnd)? ((angleBegin + Math.PI > angleEnd)? false
  5358. : true) : ((angleEnd + Math.PI > angleBegin)? true : false);
  5359. }
  5360. },
  5361. /*
  5362. Method: contains
  5363. Not Implemented
  5364. Returns *true* if *pos* is contained in the area of the shape. Returns *false* otherwise.
  5365. Parameters:
  5366. posFrom - (object) An *x*, *y* object with a <Graph.Node> position.
  5367. posTo - (object) An *x*, *y* object with a <Graph.Node> position.
  5368. pos - (object) An *x*, *y* object with the position to check.
  5369. epsilon - (number) The dimension of the shape.
  5370. Example:
  5371. (start code js)
  5372. EdgeHelper.hyperline.contains({ x: 10, y: 30 }, { x: 15, y: 35 }, { x: 15, y: 35 }, 30);
  5373. (end code)
  5374. */
  5375. 'contains': $.lambda(false)
  5376. }
  5377. };
  5378. /*
  5379. * File: Graph.Plot.js
  5380. */
  5381. /*
  5382. Object: Graph.Plot
  5383. <Graph> rendering and animation methods.
  5384. Properties:
  5385. nodeHelper - <NodeHelper> object.
  5386. edgeHelper - <EdgeHelper> object.
  5387. */
  5388. Graph.Plot = {
  5389. //Default initializer
  5390. initialize: function(viz, klass){
  5391. this.viz = viz;
  5392. this.config = viz.config;
  5393. this.node = viz.config.Node;
  5394. this.edge = viz.config.Edge;
  5395. this.animation = new Animation;
  5396. this.nodeTypes = new klass.Plot.NodeTypes;
  5397. this.edgeTypes = new klass.Plot.EdgeTypes;
  5398. this.labels = viz.labels;
  5399. },
  5400. //Add helpers
  5401. nodeHelper: NodeHelper,
  5402. edgeHelper: EdgeHelper,
  5403. Interpolator: {
  5404. //node/edge property parsers
  5405. 'map': {
  5406. 'border': 'color',
  5407. 'color': 'color',
  5408. 'width': 'number',
  5409. 'height': 'number',
  5410. 'dim': 'number',
  5411. 'alpha': 'number',
  5412. 'lineWidth': 'number',
  5413. 'angularWidth':'number',
  5414. 'span':'number',
  5415. 'valueArray':'array-number',
  5416. 'dimArray':'array-number'
  5417. //'colorArray':'array-color'
  5418. },
  5419. //canvas specific parsers
  5420. 'canvas': {
  5421. 'globalAlpha': 'number',
  5422. 'fillStyle': 'color',
  5423. 'strokeStyle': 'color',
  5424. 'lineWidth': 'number',
  5425. 'shadowBlur': 'number',
  5426. 'shadowColor': 'color',
  5427. 'shadowOffsetX': 'number',
  5428. 'shadowOffsetY': 'number',
  5429. 'miterLimit': 'number'
  5430. },
  5431. //label parsers
  5432. 'label': {
  5433. 'size': 'number',
  5434. 'color': 'color'
  5435. },
  5436. //Number interpolator
  5437. 'compute': function(from, to, delta) {
  5438. return from + (to - from) * delta;
  5439. },
  5440. //Position interpolators
  5441. 'moebius': function(elem, props, delta, vector) {
  5442. var v = vector.scale(-delta);
  5443. if(v.norm() < 1) {
  5444. var x = v.x, y = v.y;
  5445. var ans = elem.startPos
  5446. .getc().moebiusTransformation(v);
  5447. elem.pos.setc(ans.x, ans.y);
  5448. v.x = x; v.y = y;
  5449. }
  5450. },
  5451. 'linear': function(elem, props, delta) {
  5452. var from = elem.startPos.getc(true);
  5453. var to = elem.endPos.getc(true);
  5454. elem.pos.setc(this.compute(from.x, to.x, delta),
  5455. this.compute(from.y, to.y, delta));
  5456. },
  5457. 'polar': function(elem, props, delta) {
  5458. var from = elem.startPos.getp(true);
  5459. var to = elem.endPos.getp();
  5460. var ans = to.interpolate(from, delta);
  5461. elem.pos.setp(ans.theta, ans.rho);
  5462. },
  5463. //Graph's Node/Edge interpolators
  5464. 'number': function(elem, prop, delta, getter, setter) {
  5465. var from = elem[getter](prop, 'start');
  5466. var to = elem[getter](prop, 'end');
  5467. elem[setter](prop, this.compute(from, to, delta));
  5468. },
  5469. 'color': function(elem, prop, delta, getter, setter) {
  5470. var from = $.hexToRgb(elem[getter](prop, 'start'));
  5471. var to = $.hexToRgb(elem[getter](prop, 'end'));
  5472. var comp = this.compute;
  5473. var val = $.rgbToHex([parseInt(comp(from[0], to[0], delta)),
  5474. parseInt(comp(from[1], to[1], delta)),
  5475. parseInt(comp(from[2], to[2], delta))]);
  5476. elem[setter](prop, val);
  5477. },
  5478. 'array-number': function(elem, prop, delta, getter, setter) {
  5479. var from = elem[getter](prop, 'start'),
  5480. to = elem[getter](prop, 'end'),
  5481. cur = [];
  5482. for(var i=0, l=from.length; i<l; i++) {
  5483. var fromi = from[i], toi = to[i];
  5484. if(fromi.length) {
  5485. for(var j=0, len=fromi.length, curi=[]; j<len; j++) {
  5486. curi.push(this.compute(fromi[j], toi[j], delta));
  5487. }
  5488. cur.push(curi);
  5489. } else {
  5490. cur.push(this.compute(fromi, toi, delta));
  5491. }
  5492. }
  5493. elem[setter](prop, cur);
  5494. },
  5495. 'node': function(elem, props, delta, map, getter, setter) {
  5496. map = this[map];
  5497. if(props) {
  5498. var len = props.length;
  5499. for(var i=0; i<len; i++) {
  5500. var pi = props[i];
  5501. this[map[pi]](elem, pi, delta, getter, setter);
  5502. }
  5503. } else {
  5504. for(var pi in map) {
  5505. this[map[pi]](elem, pi, delta, getter, setter);
  5506. }
  5507. }
  5508. },
  5509. 'edge': function(elem, props, delta, mapKey, getter, setter) {
  5510. var adjs = elem.adjacencies;
  5511. for(var id in adjs) this['node'](adjs[id], props, delta, mapKey, getter, setter);
  5512. },
  5513. 'node-property': function(elem, props, delta) {
  5514. this['node'](elem, props, delta, 'map', 'getData', 'setData');
  5515. },
  5516. 'edge-property': function(elem, props, delta) {
  5517. this['edge'](elem, props, delta, 'map', 'getData', 'setData');
  5518. },
  5519. 'label-property': function(elem, props, delta) {
  5520. this['node'](elem, props, delta, 'label', 'getLabelData', 'setLabelData');
  5521. },
  5522. 'node-style': function(elem, props, delta) {
  5523. this['node'](elem, props, delta, 'canvas', 'getCanvasStyle', 'setCanvasStyle');
  5524. },
  5525. 'edge-style': function(elem, props, delta) {
  5526. this['edge'](elem, props, delta, 'canvas', 'getCanvasStyle', 'setCanvasStyle');
  5527. }
  5528. },
  5529. /*
  5530. sequence
  5531. Iteratively performs an action while refreshing the state of the visualization.
  5532. Parameters:
  5533. options - (object) An object containing some sequence options described below
  5534. condition - (function) A function returning a boolean instance in order to stop iterations.
  5535. step - (function) A function to execute on each step of the iteration.
  5536. onComplete - (function) A function to execute when the sequence finishes.
  5537. duration - (number) Duration (in milliseconds) of each step.
  5538. Example:
  5539. (start code js)
  5540. var rg = new $jit.RGraph(options);
  5541. var i = 0;
  5542. rg.fx.sequence({
  5543. condition: function() {
  5544. return i == 10;
  5545. },
  5546. step: function() {
  5547. alert(i++);
  5548. },
  5549. onComplete: function() {
  5550. alert('done!');
  5551. }
  5552. });
  5553. (end code)
  5554. */
  5555. sequence: function(options) {
  5556. var that = this;
  5557. options = $.merge({
  5558. condition: $.lambda(false),
  5559. step: $.empty,
  5560. onComplete: $.empty,
  5561. duration: 200
  5562. }, options || {});
  5563. var interval = setInterval(function() {
  5564. if(options.condition()) {
  5565. options.step();
  5566. } else {
  5567. clearInterval(interval);
  5568. options.onComplete();
  5569. }
  5570. that.viz.refresh(true);
  5571. }, options.duration);
  5572. },
  5573. /*
  5574. prepare
  5575. Prepare graph position and other attribute values before performing an Animation.
  5576. This method is used internally by the Toolkit.
  5577. See also:
  5578. <Animation>, <Graph.Plot.animate>
  5579. */
  5580. prepare: function(modes) {
  5581. var graph = this.viz.graph,
  5582. accessors = {
  5583. 'node-property': {
  5584. 'getter': 'getData',
  5585. 'setter': 'setData'
  5586. },
  5587. 'edge-property': {
  5588. 'getter': 'getData',
  5589. 'setter': 'setData'
  5590. },
  5591. 'node-style': {
  5592. 'getter': 'getCanvasStyle',
  5593. 'setter': 'setCanvasStyle'
  5594. },
  5595. 'edge-style': {
  5596. 'getter': 'getCanvasStyle',
  5597. 'setter': 'setCanvasStyle'
  5598. }
  5599. };
  5600. //parse modes
  5601. var m = {};
  5602. if($.type(modes) == 'array') {
  5603. for(var i=0, len=modes.length; i < len; i++) {
  5604. var elems = modes[i].split(':');
  5605. m[elems.shift()] = elems;
  5606. }
  5607. } else {
  5608. for(var p in modes) {
  5609. if(p == 'position') {
  5610. m[modes.position] = [];
  5611. } else {
  5612. m[p] = $.splat(modes[p]);
  5613. }
  5614. }
  5615. }
  5616. graph.eachNode(function(node) {
  5617. node.startPos.set(node.pos);
  5618. $.each(['node-property', 'node-style'], function(p) {
  5619. if(p in m) {
  5620. var prop = m[p];
  5621. for(var i=0, l=prop.length; i < l; i++) {
  5622. node[accessors[p].setter](prop[i], node[accessors[p].getter](prop[i]), 'start');
  5623. }
  5624. }
  5625. });
  5626. $.each(['edge-property', 'edge-style'], function(p) {
  5627. if(p in m) {
  5628. var prop = m[p];
  5629. node.eachAdjacency(function(adj) {
  5630. for(var i=0, l=prop.length; i < l; i++) {
  5631. adj[accessors[p].setter](prop[i], adj[accessors[p].getter](prop[i]), 'start');
  5632. }
  5633. });
  5634. }
  5635. });
  5636. });
  5637. return m;
  5638. },
  5639. /*
  5640. Method: animate
  5641. Animates a <Graph> by interpolating some <Graph.Node>, <Graph.Adjacence> or <Graph.Label> properties.
  5642. Parameters:
  5643. opt - (object) Animation options. The object properties are described below
  5644. duration - (optional) Described in <Options.Fx>.
  5645. fps - (optional) Described in <Options.Fx>.
  5646. hideLabels - (optional|boolean) Whether to hide labels during the animation.
  5647. modes - (required|object) An object with animation modes (described below).
  5648. Animation modes:
  5649. Animation modes are strings representing different node/edge and graph properties that you'd like to animate.
  5650. They are represented by an object that has as keys main categories of properties to animate and as values a list
  5651. of these specific properties. The properties are described below
  5652. position - Describes the way nodes' positions must be interpolated. Possible values are 'linear', 'polar' or 'moebius'.
  5653. node-property - Describes which Node properties will be interpolated. These properties can be any of the ones defined in <Options.Node>.
  5654. edge-property - Describes which Edge properties will be interpolated. These properties can be any the ones defined in <Options.Edge>.
  5655. label-property - Describes which Label properties will be interpolated. These properties can be any of the ones defined in <Options.Label> like color or size.
  5656. node-style - Describes which Node Canvas Styles will be interpolated. These are specific canvas properties like fillStyle, strokeStyle, lineWidth, shadowBlur, shadowColor, shadowOffsetX, shadowOffsetY, etc.
  5657. edge-style - Describes which Edge Canvas Styles will be interpolated. These are specific canvas properties like fillStyle, strokeStyle, lineWidth, shadowBlur, shadowColor, shadowOffsetX, shadowOffsetY, etc.
  5658. Example:
  5659. (start code js)
  5660. var viz = new $jit.Viz(options);
  5661. //...tweak some Data, CanvasStyles or LabelData properties...
  5662. viz.fx.animate({
  5663. modes: {
  5664. 'position': 'linear',
  5665. 'node-property': ['width', 'height'],
  5666. 'node-style': 'shadowColor',
  5667. 'label-property': 'size'
  5668. },
  5669. hideLabels: false
  5670. });
  5671. //...can also be written like this...
  5672. viz.fx.animate({
  5673. modes: ['linear',
  5674. 'node-property:width:height',
  5675. 'node-style:shadowColor',
  5676. 'label-property:size'],
  5677. hideLabels: false
  5678. });
  5679. (end code)
  5680. */
  5681. animate: function(opt, versor) {
  5682. opt = $.merge(this.viz.config, opt || {});
  5683. var that = this,
  5684. viz = this.viz,
  5685. graph = viz.graph,
  5686. interp = this.Interpolator,
  5687. animation = opt.type === 'nodefx'? this.nodeFxAnimation : this.animation;
  5688. //prepare graph values
  5689. var m = this.prepare(opt.modes);
  5690. //animate
  5691. if(opt.hideLabels) this.labels.hideLabels(true);
  5692. animation.setOptions($.extend(opt, {
  5693. $animating: false,
  5694. compute: function(delta) {
  5695. graph.eachNode(function(node) {
  5696. for(var p in m) {
  5697. interp[p](node, m[p], delta, versor);
  5698. }
  5699. });
  5700. that.plot(opt, this.$animating, delta);
  5701. this.$animating = true;
  5702. },
  5703. complete: function() {
  5704. if(opt.hideLabels) that.labels.hideLabels(false);
  5705. that.plot(opt);
  5706. opt.onComplete();
  5707. //This shouldn't be here!
  5708. //opt.onAfterCompute();
  5709. }
  5710. })).start();
  5711. },
  5712. /*
  5713. nodeFx
  5714. Apply animation to node properties like color, width, height, dim, etc.
  5715. Parameters:
  5716. options - Animation options. This object properties is described below
  5717. elements - The Elements to be transformed. This is an object that has a properties
  5718. (start code js)
  5719. 'elements': {
  5720. //can also be an array of ids
  5721. 'id': 'id-of-node-to-transform',
  5722. //properties to be modified. All properties are optional.
  5723. 'properties': {
  5724. 'color': '#ccc', //some color
  5725. 'width': 10, //some width
  5726. 'height': 10, //some height
  5727. 'dim': 20, //some dim
  5728. 'lineWidth': 10 //some line width
  5729. }
  5730. }
  5731. (end code)
  5732. - _reposition_ Whether to recalculate positions and add a motion animation.
  5733. This might be used when changing _width_ or _height_ properties in a <Layouts.Tree> like layout. Default's *false*.
  5734. - _onComplete_ A method that is called when the animation completes.
  5735. ...and all other <Graph.Plot.animate> options like _duration_, _fps_, _transition_, etc.
  5736. Example:
  5737. (start code js)
  5738. var rg = new RGraph(canvas, config); //can be also Hypertree or ST
  5739. rg.fx.nodeFx({
  5740. 'elements': {
  5741. 'id':'mynodeid',
  5742. 'properties': {
  5743. 'color':'#ccf'
  5744. },
  5745. 'transition': Trans.Quart.easeOut
  5746. }
  5747. });
  5748. (end code)
  5749. */
  5750. nodeFx: function(opt) {
  5751. var viz = this.viz,
  5752. graph = viz.graph,
  5753. animation = this.nodeFxAnimation,
  5754. options = $.merge(this.viz.config, {
  5755. 'elements': {
  5756. 'id': false,
  5757. 'properties': {}
  5758. },
  5759. 'reposition': false
  5760. });
  5761. opt = $.merge(options, opt || {}, {
  5762. onBeforeCompute: $.empty,
  5763. onAfterCompute: $.empty
  5764. });
  5765. //check if an animation is running
  5766. animation.stopTimer();
  5767. var props = opt.elements.properties;
  5768. //set end values for nodes
  5769. if(!opt.elements.id) {
  5770. graph.eachNode(function(n) {
  5771. for(var prop in props) {
  5772. n.setData(prop, props[prop], 'end');
  5773. }
  5774. });
  5775. } else {
  5776. var ids = $.splat(opt.elements.id);
  5777. $.each(ids, function(id) {
  5778. var n = graph.getNode(id);
  5779. if(n) {
  5780. for(var prop in props) {
  5781. n.setData(prop, props[prop], 'end');
  5782. }
  5783. }
  5784. });
  5785. }
  5786. //get keys
  5787. var propnames = [];
  5788. for(var prop in props) propnames.push(prop);
  5789. //add node properties modes
  5790. var modes = ['node-property:' + propnames.join(':')];
  5791. //set new node positions
  5792. if(opt.reposition) {
  5793. modes.push('linear');
  5794. viz.compute('end');
  5795. }
  5796. //animate
  5797. this.animate($.merge(opt, {
  5798. modes: modes,
  5799. type: 'nodefx'
  5800. }));
  5801. },
  5802. /*
  5803. Method: plot
  5804. Plots a <Graph>.
  5805. Parameters:
  5806. opt - (optional) Plotting options. Most of them are described in <Options.Fx>.
  5807. Example:
  5808. (start code js)
  5809. var viz = new $jit.Viz(options);
  5810. viz.fx.plot();
  5811. (end code)
  5812. */
  5813. plot: function(opt, animating) {
  5814. var viz = this.viz,
  5815. aGraph = viz.graph,
  5816. canvas = viz.canvas,
  5817. id = viz.root,
  5818. that = this,
  5819. ctx = canvas.getCtx(),
  5820. min = Math.min,
  5821. opt = opt || this.viz.controller;
  5822. opt.clearCanvas && canvas.clear();
  5823. var root = aGraph.getNode(id);
  5824. if(!root) return;
  5825. var T = !!root.visited;
  5826. aGraph.eachNode(function(node) {
  5827. var nodeAlpha = node.getData('alpha');
  5828. node.eachAdjacency(function(adj) {
  5829. var nodeTo = adj.nodeTo;
  5830. if(!!nodeTo.visited === T && node.drawn && nodeTo.drawn) {
  5831. !animating && opt.onBeforePlotLine(adj);
  5832. that.plotLine(adj, canvas, animating);
  5833. !animating && opt.onAfterPlotLine(adj);
  5834. }
  5835. });
  5836. if(node.drawn) {
  5837. !animating && opt.onBeforePlotNode(node);
  5838. that.plotNode(node, canvas, animating);
  5839. !animating && opt.onAfterPlotNode(node);
  5840. }
  5841. if(!that.labelsHidden && opt.withLabels) {
  5842. if(node.drawn && nodeAlpha >= 0.95) {
  5843. that.labels.plotLabel(canvas, node, opt);
  5844. } else {
  5845. that.labels.hideLabel(node, false);
  5846. }
  5847. }
  5848. node.visited = !T;
  5849. });
  5850. },
  5851. /*
  5852. Plots a Subtree.
  5853. */
  5854. plotTree: function(node, opt, animating) {
  5855. var that = this,
  5856. viz = this.viz,
  5857. canvas = viz.canvas,
  5858. config = this.config,
  5859. ctx = canvas.getCtx();
  5860. var nodeAlpha = node.getData('alpha');
  5861. node.eachSubnode(function(elem) {
  5862. if(opt.plotSubtree(node, elem) && elem.exist && elem.drawn) {
  5863. var adj = node.getAdjacency(elem.id);
  5864. !animating && opt.onBeforePlotLine(adj);
  5865. that.plotLine(adj, canvas, animating);
  5866. !animating && opt.onAfterPlotLine(adj);
  5867. that.plotTree(elem, opt, animating);
  5868. }
  5869. });
  5870. if(node.drawn) {
  5871. !animating && opt.onBeforePlotNode(node);
  5872. this.plotNode(node, canvas, animating);
  5873. !animating && opt.onAfterPlotNode(node);
  5874. if(!opt.hideLabels && opt.withLabels && nodeAlpha >= 0.95)
  5875. this.labels.plotLabel(canvas, node, opt);
  5876. else
  5877. this.labels.hideLabel(node, false);
  5878. } else {
  5879. this.labels.hideLabel(node, true);
  5880. }
  5881. },
  5882. /*
  5883. Method: plotNode
  5884. Plots a <Graph.Node>.
  5885. Parameters:
  5886. node - (object) A <Graph.Node>.
  5887. canvas - (object) A <Canvas> element.
  5888. */
  5889. plotNode: function(node, canvas, animating) {
  5890. var f = node.getData('type'),
  5891. ctxObj = this.node.CanvasStyles;
  5892. if(f != 'none') {
  5893. var width = node.getData('lineWidth'),
  5894. color = node.getData('color'),
  5895. alpha = node.getData('alpha'),
  5896. ctx = canvas.getCtx();
  5897. ctx.save();
  5898. ctx.lineWidth = width;
  5899. ctx.fillStyle = ctx.strokeStyle = color;
  5900. ctx.globalAlpha = alpha;
  5901. for(var s in ctxObj) {
  5902. ctx[s] = node.getCanvasStyle(s);
  5903. }
  5904. this.nodeTypes[f].render.call(this, node, canvas, animating);
  5905. ctx.restore();
  5906. }
  5907. },
  5908. /*
  5909. Method: plotLine
  5910. Plots a <Graph.Adjacence>.
  5911. Parameters:
  5912. adj - (object) A <Graph.Adjacence>.
  5913. canvas - (object) A <Canvas> instance.
  5914. */
  5915. plotLine: function(adj, canvas, animating) {
  5916. var f = adj.getData('type'),
  5917. ctxObj = this.edge.CanvasStyles;
  5918. if(f != 'none') {
  5919. var width = adj.getData('lineWidth'),
  5920. color = adj.getData('color'),
  5921. ctx = canvas.getCtx(),
  5922. nodeFrom = adj.nodeFrom,
  5923. nodeTo = adj.nodeTo;
  5924. ctx.save();
  5925. ctx.lineWidth = width;
  5926. ctx.fillStyle = ctx.strokeStyle = color;
  5927. ctx.globalAlpha = Math.min(nodeFrom.getData('alpha'),
  5928. nodeTo.getData('alpha'),
  5929. adj.getData('alpha'));
  5930. for(var s in ctxObj) {
  5931. ctx[s] = adj.getCanvasStyle(s);
  5932. }
  5933. this.edgeTypes[f].render.call(this, adj, canvas, animating);
  5934. ctx.restore();
  5935. }
  5936. }
  5937. };
  5938. /*
  5939. Object: Graph.Plot3D
  5940. <Graph> 3D rendering and animation methods.
  5941. Properties:
  5942. nodeHelper - <NodeHelper> object.
  5943. edgeHelper - <EdgeHelper> object.
  5944. */
  5945. Graph.Plot3D = $.merge(Graph.Plot, {
  5946. Interpolator: {
  5947. 'linear': function(elem, props, delta) {
  5948. var from = elem.startPos.getc(true);
  5949. var to = elem.endPos.getc(true);
  5950. elem.pos.setc(this.compute(from.x, to.x, delta),
  5951. this.compute(from.y, to.y, delta),
  5952. this.compute(from.z, to.z, delta));
  5953. }
  5954. },
  5955. plotNode: function(node, canvas) {
  5956. if(node.getData('type') == 'none') return;
  5957. this.plotElement(node, canvas, {
  5958. getAlpha: function() {
  5959. return node.getData('alpha');
  5960. }
  5961. });
  5962. },
  5963. plotLine: function(adj, canvas) {
  5964. if(adj.getData('type') == 'none') return;
  5965. this.plotElement(adj, canvas, {
  5966. getAlpha: function() {
  5967. return Math.min(adj.nodeFrom.getData('alpha'),
  5968. adj.nodeTo.getData('alpha'),
  5969. adj.getData('alpha'));
  5970. }
  5971. });
  5972. },
  5973. plotElement: function(elem, canvas, opt) {
  5974. var gl = canvas.getCtx(),
  5975. viewMatrix = new Matrix4,
  5976. lighting = canvas.config.Scene.Lighting,
  5977. wcanvas = canvas.canvases[0],
  5978. program = wcanvas.program,
  5979. camera = wcanvas.camera;
  5980. if(!elem.geometry) {
  5981. elem.geometry = new O3D[elem.getData('type')];
  5982. }
  5983. elem.geometry.update(elem);
  5984. if(!elem.webGLVertexBuffer) {
  5985. var vertices = [],
  5986. faces = [],
  5987. normals = [],
  5988. vertexIndex = 0,
  5989. geom = elem.geometry;
  5990. for(var i=0, vs=geom.vertices, fs=geom.faces, fsl=fs.length; i<fsl; i++) {
  5991. var face = fs[i],
  5992. v1 = vs[face.a],
  5993. v2 = vs[face.b],
  5994. v3 = vs[face.c],
  5995. v4 = face.d? vs[face.d] : false,
  5996. n = face.normal;
  5997. vertices.push(v1.x, v1.y, v1.z);
  5998. vertices.push(v2.x, v2.y, v2.z);
  5999. vertices.push(v3.x, v3.y, v3.z);
  6000. if(v4) vertices.push(v4.x, v4.y, v4.z);
  6001. normals.push(n.x, n.y, n.z);
  6002. normals.push(n.x, n.y, n.z);
  6003. normals.push(n.x, n.y, n.z);
  6004. if(v4) normals.push(n.x, n.y, n.z);
  6005. faces.push(vertexIndex, vertexIndex +1, vertexIndex +2);
  6006. if(v4) {
  6007. faces.push(vertexIndex, vertexIndex +2, vertexIndex +3);
  6008. vertexIndex += 4;
  6009. } else {
  6010. vertexIndex += 3;
  6011. }
  6012. }
  6013. //create and store vertex data
  6014. elem.webGLVertexBuffer = gl.createBuffer();
  6015. gl.bindBuffer(gl.ARRAY_BUFFER, elem.webGLVertexBuffer);
  6016. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
  6017. //create and store faces index data
  6018. elem.webGLFaceBuffer = gl.createBuffer();
  6019. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elem.webGLFaceBuffer);
  6020. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(faces), gl.STATIC_DRAW);
  6021. elem.webGLFaceCount = faces.length;
  6022. //calculate vertex normals and store them
  6023. elem.webGLNormalBuffer = gl.createBuffer();
  6024. gl.bindBuffer(gl.ARRAY_BUFFER, elem.webGLNormalBuffer);
  6025. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
  6026. }
  6027. viewMatrix.multiply(camera.matrix, elem.geometry.matrix);
  6028. //send matrix data
  6029. gl.uniformMatrix4fv(program.viewMatrix, false, viewMatrix.flatten());
  6030. gl.uniformMatrix4fv(program.projectionMatrix, false, camera.projectionMatrix.flatten());
  6031. //send normal matrix for lighting
  6032. var normalMatrix = Matrix4.makeInvert(viewMatrix);
  6033. normalMatrix.$transpose();
  6034. gl.uniformMatrix4fv(program.normalMatrix, false, normalMatrix.flatten());
  6035. //send color data
  6036. var color = $.hexToRgb(elem.getData('color'));
  6037. color.push(opt.getAlpha());
  6038. gl.uniform4f(program.color, color[0] / 255, color[1] / 255, color[2] / 255, color[3]);
  6039. //send lighting data
  6040. gl.uniform1i(program.enableLighting, lighting.enable);
  6041. if(lighting.enable) {
  6042. //set ambient light color
  6043. if(lighting.ambient) {
  6044. var acolor = lighting.ambient;
  6045. gl.uniform3f(program.ambientColor, acolor[0], acolor[1], acolor[2]);
  6046. }
  6047. //set directional light
  6048. if(lighting.directional) {
  6049. var dir = lighting.directional,
  6050. color = dir.color,
  6051. pos = dir.direction,
  6052. vd = new Vector3(pos.x, pos.y, pos.z).normalize().$scale(-1);
  6053. gl.uniform3f(program.lightingDirection, vd.x, vd.y, vd.z);
  6054. gl.uniform3f(program.directionalColor, color[0], color[1], color[2]);
  6055. }
  6056. }
  6057. //send vertices data
  6058. gl.bindBuffer(gl.ARRAY_BUFFER, elem.webGLVertexBuffer);
  6059. gl.vertexAttribPointer(program.position, 3, gl.FLOAT, false, 0, 0);
  6060. //send normals data
  6061. gl.bindBuffer(gl.ARRAY_BUFFER, elem.webGLNormalBuffer);
  6062. gl.vertexAttribPointer(program.normal, 3, gl.FLOAT, false, 0, 0);
  6063. //draw!
  6064. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elem.webGLFaceBuffer );
  6065. gl.drawElements(gl.TRIANGLES, elem.webGLFaceCount, gl.UNSIGNED_SHORT, 0);
  6066. }
  6067. });
  6068. /*
  6069. * File: Graph.Label.js
  6070. *
  6071. */
  6072. /*
  6073. Object: Graph.Label
  6074. An interface for plotting/hiding/showing labels.
  6075. Description:
  6076. This is a generic interface for plotting/hiding/showing labels.
  6077. The <Graph.Label> interface is implemented in multiple ways to provide
  6078. different label types.
  6079. For example, the Graph.Label interface is implemented as <Graph.Label.HTML> to provide
  6080. HTML label elements. Also we provide the <Graph.Label.SVG> interface for SVG type labels.
  6081. The <Graph.Label.Native> interface implements these methods with the native Canvas text rendering functions.
  6082. All subclasses (<Graph.Label.HTML>, <Graph.Label.SVG> and <Graph.Label.Native>) implement the method plotLabel.
  6083. */
  6084. Graph.Label = {};
  6085. /*
  6086. Class: Graph.Label.Native
  6087. Implements labels natively, using the Canvas text API.
  6088. */
  6089. Graph.Label.Native = new Class({
  6090. initialize: function(viz) {
  6091. this.viz = viz;
  6092. },
  6093. /*
  6094. Method: plotLabel
  6095. Plots a label for a given node.
  6096. Parameters:
  6097. canvas - (object) A <Canvas> instance.
  6098. node - (object) A <Graph.Node>.
  6099. controller - (object) A configuration object.
  6100. Example:
  6101. (start code js)
  6102. var viz = new $jit.Viz(options);
  6103. var node = viz.graph.getNode('nodeId');
  6104. viz.labels.plotLabel(viz.canvas, node, viz.config);
  6105. (end code)
  6106. */
  6107. plotLabel: function(canvas, node, controller) {
  6108. var ctx = canvas.getCtx();
  6109. var pos = node.pos.getc(true);
  6110. ctx.font = node.getLabelData('style') + ' ' + node.getLabelData('size') + 'px ' + node.getLabelData('family');
  6111. ctx.textAlign = node.getLabelData('textAlign');
  6112. ctx.fillStyle = ctx.strokeStyle = node.getLabelData('color');
  6113. ctx.textBaseline = node.getLabelData('textBaseline');
  6114. this.renderLabel(canvas, node, controller);
  6115. },
  6116. /*
  6117. renderLabel
  6118. Does the actual rendering of the label in the canvas. The default
  6119. implementation renders the label close to the position of the node, this
  6120. method should be overriden to position the labels differently.
  6121. Parameters:
  6122. canvas - A <Canvas> instance.
  6123. node - A <Graph.Node>.
  6124. controller - A configuration object. See also <Hypertree>, <RGraph>, <ST>.
  6125. */
  6126. renderLabel: function(canvas, node, controller) {
  6127. var ctx = canvas.getCtx();
  6128. var pos = node.pos.getc(true);
  6129. ctx.fillText(node.name, pos.x, pos.y + node.getData("height") / 2);
  6130. },
  6131. hideLabel: $.empty,
  6132. hideLabels: $.empty
  6133. });
  6134. /*
  6135. Class: Graph.Label.DOM
  6136. Abstract Class implementing some DOM label methods.
  6137. Implemented by:
  6138. <Graph.Label.HTML> and <Graph.Label.SVG>.
  6139. */
  6140. Graph.Label.DOM = new Class({
  6141. //A flag value indicating if node labels are being displayed or not.
  6142. labelsHidden: false,
  6143. //Label container
  6144. labelContainer: false,
  6145. //Label elements hash.
  6146. labels: {},
  6147. /*
  6148. Method: getLabelContainer
  6149. Lazy fetcher for the label container.
  6150. Returns:
  6151. The label container DOM element.
  6152. Example:
  6153. (start code js)
  6154. var viz = new $jit.Viz(options);
  6155. var labelContainer = viz.labels.getLabelContainer();
  6156. alert(labelContainer.innerHTML);
  6157. (end code)
  6158. */
  6159. getLabelContainer: function() {
  6160. return this.labelContainer ?
  6161. this.labelContainer :
  6162. this.labelContainer = document.getElementById(this.viz.config.labelContainer);
  6163. },
  6164. /*
  6165. Method: getLabel
  6166. Lazy fetcher for the label element.
  6167. Parameters:
  6168. id - (string) The label id (which is also a <Graph.Node> id).
  6169. Returns:
  6170. The label element.
  6171. Example:
  6172. (start code js)
  6173. var viz = new $jit.Viz(options);
  6174. var label = viz.labels.getLabel('someid');
  6175. alert(label.innerHTML);
  6176. (end code)
  6177. */
  6178. getLabel: function(id) {
  6179. return (id in this.labels && this.labels[id] != null) ?
  6180. this.labels[id] :
  6181. this.labels[id] = document.getElementById(id);
  6182. },
  6183. /*
  6184. Method: hideLabels
  6185. Hides all labels (by hiding the label container).
  6186. Parameters:
  6187. hide - (boolean) A boolean value indicating if the label container must be hidden or not.
  6188. Example:
  6189. (start code js)
  6190. var viz = new $jit.Viz(options);
  6191. rg.labels.hideLabels(true);
  6192. (end code)
  6193. */
  6194. hideLabels: function (hide) {
  6195. var container = this.getLabelContainer();
  6196. if(hide)
  6197. container.style.display = 'none';
  6198. else
  6199. container.style.display = '';
  6200. this.labelsHidden = hide;
  6201. },
  6202. /*
  6203. Method: clearLabels
  6204. Clears the label container.
  6205. Useful when using a new visualization with the same canvas element/widget.
  6206. Parameters:
  6207. force - (boolean) Forces deletion of all labels.
  6208. Example:
  6209. (start code js)
  6210. var viz = new $jit.Viz(options);
  6211. viz.labels.clearLabels();
  6212. (end code)
  6213. */
  6214. clearLabels: function(force) {
  6215. for(var id in this.labels) {
  6216. if (force || !this.viz.graph.hasNode(id)) {
  6217. this.disposeLabel(id);
  6218. delete this.labels[id];
  6219. }
  6220. }
  6221. },
  6222. /*
  6223. Method: disposeLabel
  6224. Removes a label.
  6225. Parameters:
  6226. id - (string) A label id (which generally is also a <Graph.Node> id).
  6227. Example:
  6228. (start code js)
  6229. var viz = new $jit.Viz(options);
  6230. viz.labels.disposeLabel('labelid');
  6231. (end code)
  6232. */
  6233. disposeLabel: function(id) {
  6234. var elem = this.getLabel(id);
  6235. if(elem && elem.parentNode) {
  6236. elem.parentNode.removeChild(elem);
  6237. }
  6238. },
  6239. /*
  6240. Method: hideLabel
  6241. Hides the corresponding <Graph.Node> label.
  6242. Parameters:
  6243. node - (object) A <Graph.Node>. Can also be an array of <Graph.Nodes>.
  6244. show - (boolean) If *true*, nodes will be shown. Otherwise nodes will be hidden.
  6245. Example:
  6246. (start code js)
  6247. var rg = new $jit.Viz(options);
  6248. viz.labels.hideLabel(viz.graph.getNode('someid'), false);
  6249. (end code)
  6250. */
  6251. hideLabel: function(node, show) {
  6252. node = $.splat(node);
  6253. var st = show ? "" : "none", lab, that = this;
  6254. $.each(node, function(n) {
  6255. var lab = that.getLabel(n.id);
  6256. if (lab) {
  6257. lab.style.display = st;
  6258. }
  6259. });
  6260. },
  6261. /*
  6262. fitsInCanvas
  6263. Returns _true_ or _false_ if the label for the node is contained in the canvas dom element or not.
  6264. Parameters:
  6265. pos - A <Complex> instance (I'm doing duck typing here so any object with _x_ and _y_ parameters will do).
  6266. canvas - A <Canvas> instance.
  6267. Returns:
  6268. A boolean value specifying if the label is contained in the <Canvas> DOM element or not.
  6269. */
  6270. fitsInCanvas: function(pos, canvas) {
  6271. var size = canvas.getSize();
  6272. if(pos.x >= size.width || pos.x < 0
  6273. || pos.y >= size.height || pos.y < 0) return false;
  6274. return true;
  6275. }
  6276. });
  6277. /*
  6278. Class: Graph.Label.HTML
  6279. Implements HTML labels.
  6280. Extends:
  6281. All <Graph.Label.DOM> methods.
  6282. */
  6283. Graph.Label.HTML = new Class({
  6284. Implements: Graph.Label.DOM,
  6285. /*
  6286. Method: plotLabel
  6287. Plots a label for a given node.
  6288. Parameters:
  6289. canvas - (object) A <Canvas> instance.
  6290. node - (object) A <Graph.Node>.
  6291. controller - (object) A configuration object.
  6292. Example:
  6293. (start code js)
  6294. var viz = new $jit.Viz(options);
  6295. var node = viz.graph.getNode('nodeId');
  6296. viz.labels.plotLabel(viz.canvas, node, viz.config);
  6297. (end code)
  6298. */
  6299. plotLabel: function(canvas, node, controller) {
  6300. var id = node.id, tag = this.getLabel(id);
  6301. if(!tag && !(tag = document.getElementById(id))) {
  6302. tag = document.createElement('div');
  6303. var container = this.getLabelContainer();
  6304. tag.id = id;
  6305. tag.className = 'node';
  6306. tag.style.position = 'absolute';
  6307. controller.onCreateLabel(tag, node);
  6308. container.appendChild(tag);
  6309. this.labels[node.id] = tag;
  6310. }
  6311. this.placeLabel(tag, node, controller);
  6312. }
  6313. });
  6314. /*
  6315. Class: Graph.Label.SVG
  6316. Implements SVG labels.
  6317. Extends:
  6318. All <Graph.Label.DOM> methods.
  6319. */
  6320. Graph.Label.SVG = new Class({
  6321. Implements: Graph.Label.DOM,
  6322. /*
  6323. Method: plotLabel
  6324. Plots a label for a given node.
  6325. Parameters:
  6326. canvas - (object) A <Canvas> instance.
  6327. node - (object) A <Graph.Node>.
  6328. controller - (object) A configuration object.
  6329. Example:
  6330. (start code js)
  6331. var viz = new $jit.Viz(options);
  6332. var node = viz.graph.getNode('nodeId');
  6333. viz.labels.plotLabel(viz.canvas, node, viz.config);
  6334. (end code)
  6335. */
  6336. plotLabel: function(canvas, node, controller) {
  6337. var id = node.id, tag = this.getLabel(id);
  6338. if(!tag && !(tag = document.getElementById(id))) {
  6339. var ns = 'http://www.w3.org/2000/svg';
  6340. tag = document.createElementNS(ns, 'svg:text');
  6341. var tspan = document.createElementNS(ns, 'svg:tspan');
  6342. tag.appendChild(tspan);
  6343. var container = this.getLabelContainer();
  6344. tag.setAttribute('id', id);
  6345. tag.setAttribute('class', 'node');
  6346. container.appendChild(tag);
  6347. controller.onCreateLabel(tag, node);
  6348. this.labels[node.id] = tag;
  6349. }
  6350. this.placeLabel(tag, node, controller);
  6351. }
  6352. });
  6353. Graph.Geom = new Class({
  6354. initialize: function(viz) {
  6355. this.viz = viz;
  6356. this.config = viz.config;
  6357. this.node = viz.config.Node;
  6358. this.edge = viz.config.Edge;
  6359. },
  6360. /*
  6361. Applies a translation to the tree.
  6362. Parameters:
  6363. pos - A <Complex> number specifying translation vector.
  6364. prop - A <Graph.Node> position property ('pos', 'start' or 'end').
  6365. Example:
  6366. (start code js)
  6367. st.geom.translate(new Complex(300, 100), 'end');
  6368. (end code)
  6369. */
  6370. translate: function(pos, prop) {
  6371. prop = $.splat(prop);
  6372. this.viz.graph.eachNode(function(elem) {
  6373. $.each(prop, function(p) { elem.getPos(p).$add(pos); });
  6374. });
  6375. },
  6376. /*
  6377. Hides levels of the tree until it properly fits in canvas.
  6378. */
  6379. setRightLevelToShow: function(node, canvas, callback) {
  6380. var level = this.getRightLevelToShow(node, canvas),
  6381. fx = this.viz.labels,
  6382. opt = $.merge({
  6383. execShow:true,
  6384. execHide:true,
  6385. onHide: $.empty,
  6386. onShow: $.empty
  6387. }, callback || {});
  6388. node.eachLevel(0, this.config.levelsToShow, function(n) {
  6389. var d = n._depth - node._depth;
  6390. if(d > level) {
  6391. opt.onHide(n);
  6392. if(opt.execHide) {
  6393. n.drawn = false;
  6394. n.exist = false;
  6395. fx.hideLabel(n, false);
  6396. }
  6397. } else {
  6398. opt.onShow(n);
  6399. if(opt.execShow) {
  6400. n.exist = true;
  6401. }
  6402. }
  6403. });
  6404. node.drawn= true;
  6405. },
  6406. /*
  6407. Returns the right level to show for the current tree in order to fit in canvas.
  6408. */
  6409. getRightLevelToShow: function(node, canvas) {
  6410. var config = this.config;
  6411. var level = config.levelsToShow;
  6412. var constrained = config.constrained;
  6413. if(!constrained) return level;
  6414. while(!this.treeFitsInCanvas(node, canvas, level) && level > 1) { level-- ; }
  6415. return level;
  6416. }
  6417. });
  6418. /*
  6419. * File: Loader.js
  6420. *
  6421. */
  6422. /*
  6423. Object: Loader
  6424. Provides methods for loading and serving JSON data.
  6425. */
  6426. var Loader = {
  6427. construct: function(json) {
  6428. var isGraph = ($.type(json) == 'array');
  6429. var ans = new Graph(this.graphOptions, this.config.Node, this.config.Edge, this.config.Label);
  6430. if(!isGraph)
  6431. //make tree
  6432. (function (ans, json) {
  6433. ans.addNode(json);
  6434. if(json.children) {
  6435. for(var i=0, ch = json.children; i<ch.length; i++) {
  6436. ans.addAdjacence(json, ch[i]);
  6437. arguments.callee(ans, ch[i]);
  6438. }
  6439. }
  6440. })(ans, json);
  6441. else
  6442. //make graph
  6443. (function (ans, json) {
  6444. var getNode = function(id) {
  6445. for(var i=0, l=json.length; i<l; i++) {
  6446. if(json[i].id == id) {
  6447. return json[i];
  6448. }
  6449. }
  6450. // The node was not defined in the JSON
  6451. // Let's create it
  6452. var newNode = {
  6453. "id" : id,
  6454. "name" : id
  6455. };
  6456. return ans.addNode(newNode);
  6457. };
  6458. for(var i=0, l=json.length; i<l; i++) {
  6459. ans.addNode(json[i]);
  6460. var adj = json[i].adjacencies;
  6461. if (adj) {
  6462. for(var j=0, lj=adj.length; j<lj; j++) {
  6463. var node = adj[j], data = {};
  6464. if(typeof adj[j] != 'string') {
  6465. data = $.merge(node.data, {});
  6466. node = node.nodeTo;
  6467. }
  6468. ans.addAdjacence(json[i], getNode(node), data);
  6469. }
  6470. }
  6471. }
  6472. })(ans, json);
  6473. return ans;
  6474. },
  6475. /*
  6476. Method: loadJSON
  6477. Loads a JSON structure to the visualization. The JSON structure can be a JSON *tree* or *graph* structure.
  6478. A JSON tree or graph structure consists of nodes, each having as properties
  6479. id - (string) A unique identifier for the node
  6480. name - (string) A node's name
  6481. data - (object) The data optional property contains a hash (i.e {})
  6482. where you can store all the information you want about this node.
  6483. For JSON *Tree* structures, there's an extra optional property *children* of type Array which contains the node's children.
  6484. Example:
  6485. (start code js)
  6486. var json = {
  6487. "id": "aUniqueIdentifier",
  6488. "name": "usually a nodes name",
  6489. "data": {
  6490. "some key": "some value",
  6491. "some other key": "some other value"
  6492. },
  6493. "children": [ *other nodes or empty* ]
  6494. };
  6495. (end code)
  6496. JSON *Graph* structures consist of an array of nodes, each specifying the nodes to which the current node is connected.
  6497. For JSON *Graph* structures, the *children* property is replaced by the *adjacencies* property.
  6498. There are two types of *Graph* structures, *simple* and *extended* graph structures.
  6499. For *simple* Graph structures, the adjacencies property contains an array of strings, each specifying the
  6500. id of the node connected to the main node.
  6501. Example:
  6502. (start code js)
  6503. var json = [
  6504. {
  6505. "id": "aUniqueIdentifier",
  6506. "name": "usually a nodes name",
  6507. "data": {
  6508. "some key": "some value",
  6509. "some other key": "some other value"
  6510. },
  6511. "adjacencies": ["anotherUniqueIdentifier", "yetAnotherUniqueIdentifier", 'etc']
  6512. },
  6513. 'other nodes go here...'
  6514. ];
  6515. (end code)
  6516. For *extended Graph structures*, the adjacencies property contains an array of Adjacency objects that have as properties
  6517. nodeTo - (string) The other node connected by this adjacency.
  6518. data - (object) A data property, where we can store custom key/value information.
  6519. Example:
  6520. (start code js)
  6521. var json = [
  6522. {
  6523. "id": "aUniqueIdentifier",
  6524. "name": "usually a nodes name",
  6525. "data": {
  6526. "some key": "some value",
  6527. "some other key": "some other value"
  6528. },
  6529. "adjacencies": [
  6530. {
  6531. nodeTo:"aNodeId",
  6532. data: {} //put whatever you want here
  6533. },
  6534. 'other adjacencies go here...'
  6535. },
  6536. 'other nodes go here...'
  6537. ];
  6538. (end code)
  6539. About the data property:
  6540. As described before, you can store custom data in the *data* property of JSON *nodes* and *adjacencies*.
  6541. You can use almost any string as key for the data object. Some keys though are reserved by the toolkit, and
  6542. have special meanings. This is the case for keys starting with a dollar sign, for example, *$width*.
  6543. For JSON *node* objects, adding dollar prefixed properties that match the names of the options defined in
  6544. <Options.Node> will override the general value for that option with that particular value. For this to work
  6545. however, you do have to set *overridable = true* in <Options.Node>.
  6546. The same thing is true for JSON adjacencies. Dollar prefixed data properties will alter values set in <Options.Edge>
  6547. if <Options.Edge> has *overridable = true*.
  6548. When loading JSON data into TreeMaps, the *data* property must contain a value for the *$area* key,
  6549. since this is the value which will be taken into account when creating the layout.
  6550. The same thing goes for the *$color* parameter.
  6551. In JSON Nodes you can use also *$label-* prefixed properties to refer to <Options.Label> properties. For example,
  6552. *$label-size* will refer to <Options.Label> size property. Also, in JSON nodes and adjacencies you can set
  6553. canvas specific properties individually by using the *$canvas-* prefix. For example, *$canvas-shadowBlur* will refer
  6554. to the *shadowBlur* property.
  6555. These properties can also be accessed after loading the JSON data from <Graph.Nodes> and <Graph.Adjacences>
  6556. by using <Accessors>. For more information take a look at the <Graph> and <Accessors> documentation.
  6557. Finally, these properties can also be used to create advanced animations like with <Options.NodeStyles>. For more
  6558. information about creating animations please take a look at the <Graph.Plot> and <Graph.Plot.animate> documentation.
  6559. loadJSON Parameters:
  6560. json - A JSON Tree or Graph structure.
  6561. i - For Graph structures only. Sets the indexed node as root for the visualization.
  6562. */
  6563. loadJSON: function(json, i) {
  6564. this.json = json;
  6565. //if they're canvas labels erase them.
  6566. if(this.labels && this.labels.clearLabels) {
  6567. this.labels.clearLabels(true);
  6568. }
  6569. this.graph = this.construct(json);
  6570. if($.type(json) != 'array'){
  6571. this.root = json.id;
  6572. } else {
  6573. this.root = json[i? i : 0].id;
  6574. }
  6575. },
  6576. /*
  6577. Method: toJSON
  6578. Returns a JSON tree/graph structure from the visualization's <Graph>.
  6579. See <Loader.loadJSON> for the graph formats available.
  6580. See also:
  6581. <Loader.loadJSON>
  6582. Parameters:
  6583. type - (string) Default's "tree". The type of the JSON structure to be returned.
  6584. Possible options are "tree" or "graph".
  6585. */
  6586. toJSON: function(type) {
  6587. type = type || "tree";
  6588. if(type == 'tree') {
  6589. var ans = {};
  6590. var rootNode = this.graph.getNode(this.root);
  6591. var ans = (function recTree(node) {
  6592. var ans = {};
  6593. ans.id = node.id;
  6594. ans.name = node.name;
  6595. ans.data = node.data;
  6596. var ch =[];
  6597. node.eachSubnode(function(n) {
  6598. ch.push(recTree(n));
  6599. });
  6600. ans.children = ch;
  6601. return ans;
  6602. })(rootNode);
  6603. return ans;
  6604. } else {
  6605. var ans = [];
  6606. var T = !!this.graph.getNode(this.root).visited;
  6607. this.graph.eachNode(function(node) {
  6608. var ansNode = {};
  6609. ansNode.id = node.id;
  6610. ansNode.name = node.name;
  6611. ansNode.data = node.data;
  6612. var adjs = [];
  6613. node.eachAdjacency(function(adj) {
  6614. var nodeTo = adj.nodeTo;
  6615. if(!!nodeTo.visited === T) {
  6616. var ansAdj = {};
  6617. ansAdj.nodeTo = nodeTo.id;
  6618. ansAdj.data = adj.data;
  6619. adjs.push(ansAdj);
  6620. }
  6621. });
  6622. ansNode.adjacencies = adjs;
  6623. ans.push(ansNode);
  6624. node.visited = !T;
  6625. });
  6626. return ans;
  6627. }
  6628. }
  6629. };
  6630. /*
  6631. * File: Layouts.js
  6632. *
  6633. * Implements base Tree and Graph layouts.
  6634. *
  6635. * Description:
  6636. *
  6637. * Implements base Tree and Graph layouts like Radial, Tree, etc.
  6638. *
  6639. */
  6640. /*
  6641. * Object: Layouts
  6642. *
  6643. * Parent object for common layouts.
  6644. *
  6645. */
  6646. var Layouts = $jit.Layouts = {};
  6647. //Some util shared layout functions are defined here.
  6648. var NodeDim = {
  6649. label: null,
  6650. compute: function(graph, prop, opt) {
  6651. this.initializeLabel(opt);
  6652. var label = this.label, style = label.style;
  6653. graph.eachNode(function(n) {
  6654. var autoWidth = n.getData('autoWidth'),
  6655. autoHeight = n.getData('autoHeight');
  6656. if(autoWidth || autoHeight) {
  6657. //delete dimensions since these are
  6658. //going to be overridden now.
  6659. delete n.data.$width;
  6660. delete n.data.$height;
  6661. delete n.data.$dim;
  6662. var width = n.getData('width'),
  6663. height = n.getData('height');
  6664. //reset label dimensions
  6665. style.width = autoWidth? 'auto' : width + 'px';
  6666. style.height = autoHeight? 'auto' : height + 'px';
  6667. //TODO(nico) should let the user choose what to insert here.
  6668. label.innerHTML = n.name;
  6669. var offsetWidth = label.offsetWidth,
  6670. offsetHeight = label.offsetHeight;
  6671. var type = n.getData('type');
  6672. if($.indexOf(['circle', 'square', 'triangle', 'star'], type) === -1) {
  6673. n.setData('width', offsetWidth);
  6674. n.setData('height', offsetHeight);
  6675. } else {
  6676. var dim = offsetWidth > offsetHeight? offsetWidth : offsetHeight;
  6677. n.setData('width', dim);
  6678. n.setData('height', dim);
  6679. n.setData('dim', dim);
  6680. }
  6681. }
  6682. });
  6683. },
  6684. initializeLabel: function(opt) {
  6685. if(!this.label) {
  6686. this.label = document.createElement('div');
  6687. document.body.appendChild(this.label);
  6688. }
  6689. this.setLabelStyles(opt);
  6690. },
  6691. setLabelStyles: function(opt) {
  6692. $.extend(this.label.style, {
  6693. 'visibility': 'hidden',
  6694. 'position': 'absolute',
  6695. 'width': 'auto',
  6696. 'height': 'auto'
  6697. });
  6698. this.label.className = 'jit-autoadjust-label';
  6699. }
  6700. };
  6701. /*
  6702. * Class: Layouts.Tree
  6703. *
  6704. * Implements a Tree Layout.
  6705. *
  6706. * Implemented By:
  6707. *
  6708. * <ST>
  6709. *
  6710. * Inspired by:
  6711. *
  6712. * Drawing Trees (Andrew J. Kennedy) <http://research.microsoft.com/en-us/um/people/akenn/fun/drawingtrees.pdf>
  6713. *
  6714. */
  6715. Layouts.Tree = (function() {
  6716. //Layout functions
  6717. var slice = Array.prototype.slice;
  6718. /*
  6719. Calculates the max width and height nodes for a tree level
  6720. */
  6721. function getBoundaries(graph, config, level, orn, prop) {
  6722. var dim = config.Node;
  6723. var multitree = config.multitree;
  6724. if (dim.overridable) {
  6725. var w = -1, h = -1;
  6726. graph.eachNode(function(n) {
  6727. if (n._depth == level
  6728. && (!multitree || ('$orn' in n.data) && n.data.$orn == orn)) {
  6729. var dw = n.getData('width', prop);
  6730. var dh = n.getData('height', prop);
  6731. w = (w < dw) ? dw : w;
  6732. h = (h < dh) ? dh : h;
  6733. }
  6734. });
  6735. return {
  6736. 'width' : w < 0 ? dim.width : w,
  6737. 'height' : h < 0 ? dim.height : h
  6738. };
  6739. } else {
  6740. return dim;
  6741. }
  6742. }
  6743. function movetree(node, prop, val, orn) {
  6744. var p = (orn == "left" || orn == "right") ? "y" : "x";
  6745. node.getPos(prop)[p] += val;
  6746. }
  6747. function moveextent(extent, val) {
  6748. var ans = [];
  6749. $.each(extent, function(elem) {
  6750. elem = slice.call(elem);
  6751. elem[0] += val;
  6752. elem[1] += val;
  6753. ans.push(elem);
  6754. });
  6755. return ans;
  6756. }
  6757. function merge(ps, qs) {
  6758. if (ps.length == 0)
  6759. return qs;
  6760. if (qs.length == 0)
  6761. return ps;
  6762. var p = ps.shift(), q = qs.shift();
  6763. return [ [ p[0], q[1] ] ].concat(merge(ps, qs));
  6764. }
  6765. function mergelist(ls, def) {
  6766. def = def || [];
  6767. if (ls.length == 0)
  6768. return def;
  6769. var ps = ls.pop();
  6770. return mergelist(ls, merge(ps, def));
  6771. }
  6772. function fit(ext1, ext2, subtreeOffset, siblingOffset, i) {
  6773. if (ext1.length <= i || ext2.length <= i)
  6774. return 0;
  6775. var p = ext1[i][1], q = ext2[i][0];
  6776. return Math.max(fit(ext1, ext2, subtreeOffset, siblingOffset, ++i)
  6777. + subtreeOffset, p - q + siblingOffset);
  6778. }
  6779. function fitlistl(es, subtreeOffset, siblingOffset) {
  6780. function $fitlistl(acc, es, i) {
  6781. if (es.length <= i)
  6782. return [];
  6783. var e = es[i], ans = fit(acc, e, subtreeOffset, siblingOffset, 0);
  6784. return [ ans ].concat($fitlistl(merge(acc, moveextent(e, ans)), es, ++i));
  6785. }
  6786. ;
  6787. return $fitlistl( [], es, 0);
  6788. }
  6789. function fitlistr(es, subtreeOffset, siblingOffset) {
  6790. function $fitlistr(acc, es, i) {
  6791. if (es.length <= i)
  6792. return [];
  6793. var e = es[i], ans = -fit(e, acc, subtreeOffset, siblingOffset, 0);
  6794. return [ ans ].concat($fitlistr(merge(moveextent(e, ans), acc), es, ++i));
  6795. }
  6796. ;
  6797. es = slice.call(es);
  6798. var ans = $fitlistr( [], es.reverse(), 0);
  6799. return ans.reverse();
  6800. }
  6801. function fitlist(es, subtreeOffset, siblingOffset, align) {
  6802. var esl = fitlistl(es, subtreeOffset, siblingOffset), esr = fitlistr(es,
  6803. subtreeOffset, siblingOffset);
  6804. if (align == "left")
  6805. esr = esl;
  6806. else if (align == "right")
  6807. esl = esr;
  6808. for ( var i = 0, ans = []; i < esl.length; i++) {
  6809. ans[i] = (esl[i] + esr[i]) / 2;
  6810. }
  6811. return ans;
  6812. }
  6813. function design(graph, node, prop, config, orn) {
  6814. var multitree = config.multitree;
  6815. var auxp = [ 'x', 'y' ], auxs = [ 'width', 'height' ];
  6816. var ind = +(orn == "left" || orn == "right");
  6817. var p = auxp[ind], notp = auxp[1 - ind];
  6818. var cnode = config.Node;
  6819. var s = auxs[ind], nots = auxs[1 - ind];
  6820. var siblingOffset = config.siblingOffset;
  6821. var subtreeOffset = config.subtreeOffset;
  6822. var align = config.align;
  6823. function $design(node, maxsize, acum) {
  6824. var sval = node.getData(s, prop);
  6825. var notsval = maxsize
  6826. || (node.getData(nots, prop));
  6827. var trees = [], extents = [], chmaxsize = false;
  6828. var chacum = notsval + config.levelDistance;
  6829. node.eachSubnode(function(n) {
  6830. if (n.exist
  6831. && (!multitree || ('$orn' in n.data) && n.data.$orn == orn)) {
  6832. if (!chmaxsize)
  6833. chmaxsize = getBoundaries(graph, config, n._depth, orn, prop);
  6834. var s = $design(n, chmaxsize[nots], acum + chacum);
  6835. trees.push(s.tree);
  6836. extents.push(s.extent);
  6837. }
  6838. });
  6839. var positions = fitlist(extents, subtreeOffset, siblingOffset, align);
  6840. for ( var i = 0, ptrees = [], pextents = []; i < trees.length; i++) {
  6841. movetree(trees[i], prop, positions[i], orn);
  6842. pextents.push(moveextent(extents[i], positions[i]));
  6843. }
  6844. var resultextent = [ [ -sval / 2, sval / 2 ] ]
  6845. .concat(mergelist(pextents));
  6846. node.getPos(prop)[p] = 0;
  6847. if (orn == "top" || orn == "left") {
  6848. node.getPos(prop)[notp] = acum;
  6849. } else {
  6850. node.getPos(prop)[notp] = -acum;
  6851. }
  6852. return {
  6853. tree : node,
  6854. extent : resultextent
  6855. };
  6856. }
  6857. $design(node, false, 0);
  6858. }
  6859. return new Class({
  6860. /*
  6861. Method: compute
  6862. Computes nodes' positions.
  6863. */
  6864. compute : function(property, computeLevels) {
  6865. var prop = property || 'start';
  6866. var node = this.graph.getNode(this.root);
  6867. $.extend(node, {
  6868. 'drawn' : true,
  6869. 'exist' : true,
  6870. 'selected' : true
  6871. });
  6872. NodeDim.compute(this.graph, prop, this.config);
  6873. if (!!computeLevels || !("_depth" in node)) {
  6874. this.graph.computeLevels(this.root, 0, "ignore");
  6875. }
  6876. this.computePositions(node, prop);
  6877. },
  6878. computePositions : function(node, prop) {
  6879. var config = this.config;
  6880. var multitree = config.multitree;
  6881. var align = config.align;
  6882. var indent = align !== 'center' && config.indent;
  6883. var orn = config.orientation;
  6884. var orns = multitree ? [ 'top', 'right', 'bottom', 'left' ] : [ orn ];
  6885. var that = this;
  6886. $.each(orns, function(orn) {
  6887. //calculate layout
  6888. design(that.graph, node, prop, that.config, orn, prop);
  6889. var i = [ 'x', 'y' ][+(orn == "left" || orn == "right")];
  6890. //absolutize
  6891. (function red(node) {
  6892. node.eachSubnode(function(n) {
  6893. if (n.exist
  6894. && (!multitree || ('$orn' in n.data) && n.data.$orn == orn)) {
  6895. n.getPos(prop)[i] += node.getPos(prop)[i];
  6896. if (indent) {
  6897. n.getPos(prop)[i] += align == 'left' ? indent : -indent;
  6898. }
  6899. red(n);
  6900. }
  6901. });
  6902. })(node);
  6903. });
  6904. }
  6905. });
  6906. })();
  6907. /*
  6908. * File: Spacetree.js
  6909. */
  6910. /*
  6911. Class: ST
  6912. A Tree layout with advanced contraction and expansion animations.
  6913. Inspired by:
  6914. SpaceTree: Supporting Exploration in Large Node Link Tree, Design Evolution and Empirical Evaluation (Catherine Plaisant, Jesse Grosjean, Benjamin B. Bederson)
  6915. <http://hcil.cs.umd.edu/trs/2002-05/2002-05.pdf>
  6916. Drawing Trees (Andrew J. Kennedy) <http://research.microsoft.com/en-us/um/people/akenn/fun/drawingtrees.pdf>
  6917. Note:
  6918. This visualization was built and engineered from scratch, taking only the papers as inspiration, and only shares some features with the visualization described in those papers.
  6919. Implements:
  6920. All <Loader> methods
  6921. Constructor Options:
  6922. Inherits options from
  6923. - <Options.Canvas>
  6924. - <Options.Controller>
  6925. - <Options.Tree>
  6926. - <Options.Node>
  6927. - <Options.Edge>
  6928. - <Options.Label>
  6929. - <Options.Events>
  6930. - <Options.Tips>
  6931. - <Options.NodeStyles>
  6932. - <Options.Navigation>
  6933. Additionally, there are other parameters and some default values changed
  6934. constrained - (boolean) Default's *true*. Whether to show the entire tree when loaded or just the number of levels specified by _levelsToShow_.
  6935. levelsToShow - (number) Default's *2*. The number of levels to show for a subtree. This number is relative to the selected node.
  6936. levelDistance - (number) Default's *30*. The distance between two consecutive levels of the tree.
  6937. Node.type - Described in <Options.Node>. Default's set to *rectangle*.
  6938. offsetX - (number) Default's *0*. The x-offset distance from the selected node to the center of the canvas.
  6939. offsetY - (number) Default's *0*. The y-offset distance from the selected node to the center of the canvas.
  6940. duration - Described in <Options.Fx>. It's default value has been changed to *700*.
  6941. Instance Properties:
  6942. canvas - Access a <Canvas> instance.
  6943. graph - Access a <Graph> instance.
  6944. op - Access a <ST.Op> instance.
  6945. fx - Access a <ST.Plot> instance.
  6946. labels - Access a <ST.Label> interface implementation.
  6947. */
  6948. $jit.ST= (function() {
  6949. // Define some private methods first...
  6950. // Nodes in path
  6951. var nodesInPath = [];
  6952. // Nodes to contract
  6953. function getNodesToHide(node) {
  6954. node = node || this.clickedNode;
  6955. if(!this.config.constrained) {
  6956. return [];
  6957. }
  6958. var Geom = this.geom;
  6959. var graph = this.graph;
  6960. var canvas = this.canvas;
  6961. var level = node._depth, nodeArray = [];
  6962. graph.eachNode(function(n) {
  6963. if(n.exist && !n.selected) {
  6964. if(n.isDescendantOf(node.id)) {
  6965. if(n._depth <= level) nodeArray.push(n);
  6966. } else {
  6967. nodeArray.push(n);
  6968. }
  6969. }
  6970. });
  6971. var leafLevel = Geom.getRightLevelToShow(node, canvas);
  6972. node.eachLevel(leafLevel, leafLevel, function(n) {
  6973. if(n.exist && !n.selected) nodeArray.push(n);
  6974. });
  6975. for (var i = 0; i < nodesInPath.length; i++) {
  6976. var n = this.graph.getNode(nodesInPath[i]);
  6977. if(!n.isDescendantOf(node.id)) {
  6978. nodeArray.push(n);
  6979. }
  6980. }
  6981. return nodeArray;
  6982. };
  6983. // Nodes to expand
  6984. function getNodesToShow(node) {
  6985. var nodeArray = [], config = this.config;
  6986. node = node || this.clickedNode;
  6987. this.clickedNode.eachLevel(0, config.levelsToShow, function(n) {
  6988. if(config.multitree && !('$orn' in n.data)
  6989. && n.anySubnode(function(ch){ return ch.exist && !ch.drawn; })) {
  6990. nodeArray.push(n);
  6991. } else if(n.drawn && !n.anySubnode("drawn")) {
  6992. nodeArray.push(n);
  6993. }
  6994. });
  6995. return nodeArray;
  6996. };
  6997. // Now define the actual class.
  6998. return new Class({
  6999. Implements: [Loader, Extras, Layouts.Tree],
  7000. initialize: function(controller) {
  7001. var $ST = $jit.ST;
  7002. var config= {
  7003. levelsToShow: 2,
  7004. levelDistance: 30,
  7005. constrained: true,
  7006. Node: {
  7007. type: 'rectangle'
  7008. },
  7009. duration: 700,
  7010. offsetX: 0,
  7011. offsetY: 0
  7012. };
  7013. this.controller = this.config = $.merge(
  7014. Options("Canvas", "Fx", "Tree", "Node", "Edge", "Controller",
  7015. "Tips", "NodeStyles", "Events", "Navigation", "Label"), config, controller);
  7016. var canvasConfig = this.config;
  7017. if(canvasConfig.useCanvas) {
  7018. this.canvas = canvasConfig.useCanvas;
  7019. this.config.labelContainer = this.canvas.id + '-label';
  7020. } else {
  7021. if(canvasConfig.background) {
  7022. canvasConfig.background = $.merge({
  7023. type: 'Circles'
  7024. }, canvasConfig.background);
  7025. }
  7026. this.canvas = new Canvas(this, canvasConfig);
  7027. this.config.labelContainer = (typeof canvasConfig.injectInto == 'string'? canvasConfig.injectInto : canvasConfig.injectInto.id) + '-label';
  7028. }
  7029. this.graphOptions = {
  7030. 'klass': Complex
  7031. };
  7032. this.graph = new Graph(this.graphOptions, this.config.Node, this.config.Edge);
  7033. this.labels = new $ST.Label[canvasConfig.Label.type](this);
  7034. this.fx = new $ST.Plot(this, $ST);
  7035. this.op = new $ST.Op(this);
  7036. this.group = new $ST.Group(this);
  7037. this.geom = new $ST.Geom(this);
  7038. this.clickedNode= null;
  7039. // initialize extras
  7040. this.initializeExtras();
  7041. },
  7042. /*
  7043. Method: plot
  7044. Plots the <ST>. This is a shortcut to *fx.plot*.
  7045. */
  7046. plot: function() { this.fx.plot(this.controller); },
  7047. /*
  7048. Method: switchPosition
  7049. Switches the tree orientation.
  7050. Parameters:
  7051. pos - (string) The new tree orientation. Possible values are "top", "left", "right" and "bottom".
  7052. method - (string) Set this to "animate" if you want to animate the tree when switching its position. You can also set this parameter to "replot" to just replot the subtree.
  7053. onComplete - (optional|object) This callback is called once the "switching" animation is complete.
  7054. Example:
  7055. (start code js)
  7056. st.switchPosition("right", "animate", {
  7057. onComplete: function() {
  7058. alert('completed!');
  7059. }
  7060. });
  7061. (end code)
  7062. */
  7063. switchPosition: function(pos, method, onComplete) {
  7064. var Geom = this.geom, Plot = this.fx, that = this;
  7065. if(!Plot.busy) {
  7066. Plot.busy = true;
  7067. this.contract({
  7068. onComplete: function() {
  7069. Geom.switchOrientation(pos);
  7070. that.compute('end', false);
  7071. Plot.busy = false;
  7072. if(method == 'animate') {
  7073. that.onClick(that.clickedNode.id, onComplete);
  7074. } else if(method == 'replot') {
  7075. that.select(that.clickedNode.id, onComplete);
  7076. }
  7077. }
  7078. }, pos);
  7079. }
  7080. },
  7081. /*
  7082. Method: switchAlignment
  7083. Switches the tree alignment.
  7084. Parameters:
  7085. align - (string) The new tree alignment. Possible values are "left", "center" and "right".
  7086. method - (string) Set this to "animate" if you want to animate the tree after aligning its position. You can also set this parameter to "replot" to just replot the subtree.
  7087. onComplete - (optional|object) This callback is called once the "switching" animation is complete.
  7088. Example:
  7089. (start code js)
  7090. st.switchAlignment("right", "animate", {
  7091. onComplete: function() {
  7092. alert('completed!');
  7093. }
  7094. });
  7095. (end code)
  7096. */
  7097. switchAlignment: function(align, method, onComplete) {
  7098. this.config.align = align;
  7099. if(method == 'animate') {
  7100. this.select(this.clickedNode.id, onComplete);
  7101. } else if(method == 'replot') {
  7102. this.onClick(this.clickedNode.id, onComplete);
  7103. }
  7104. },
  7105. /*
  7106. Method: addNodeInPath
  7107. Adds a node to the current path as selected node. The selected node will be visible (as in non-collapsed) at all times.
  7108. Parameters:
  7109. id - (string) A <Graph.Node> id.
  7110. Example:
  7111. (start code js)
  7112. st.addNodeInPath("nodeId");
  7113. (end code)
  7114. */
  7115. addNodeInPath: function(id) {
  7116. nodesInPath.push(id);
  7117. this.select((this.clickedNode && this.clickedNode.id) || this.root);
  7118. },
  7119. /*
  7120. Method: clearNodesInPath
  7121. Removes all nodes tagged as selected by the <ST.addNodeInPath> method.
  7122. See also:
  7123. <ST.addNodeInPath>
  7124. Example:
  7125. (start code js)
  7126. st.clearNodesInPath();
  7127. (end code)
  7128. */
  7129. clearNodesInPath: function(id) {
  7130. nodesInPath.length = 0;
  7131. this.select((this.clickedNode && this.clickedNode.id) || this.root);
  7132. },
  7133. /*
  7134. Method: refresh
  7135. Computes positions and plots the tree.
  7136. */
  7137. refresh: function() {
  7138. this.reposition();
  7139. this.select((this.clickedNode && this.clickedNode.id) || this.root);
  7140. },
  7141. reposition: function() {
  7142. this.graph.computeLevels(this.root, 0, "ignore");
  7143. this.geom.setRightLevelToShow(this.clickedNode, this.canvas);
  7144. this.graph.eachNode(function(n) {
  7145. if(n.exist) n.drawn = true;
  7146. });
  7147. this.compute('end');
  7148. },
  7149. requestNodes: function(node, onComplete) {
  7150. var handler = $.merge(this.controller, onComplete),
  7151. lev = this.config.levelsToShow;
  7152. if(handler.request) {
  7153. var leaves = [], d = node._depth;
  7154. node.eachLevel(0, lev, function(n) {
  7155. if(n.drawn &&
  7156. !n.anySubnode()) {
  7157. leaves.push(n);
  7158. n._level = lev - (n._depth - d);
  7159. }
  7160. });
  7161. this.group.requestNodes(leaves, handler);
  7162. }
  7163. else
  7164. handler.onComplete();
  7165. },
  7166. contract: function(onComplete, switched) {
  7167. var orn = this.config.orientation;
  7168. var Geom = this.geom, Group = this.group;
  7169. if(switched) Geom.switchOrientation(switched);
  7170. var nodes = getNodesToHide.call(this);
  7171. if(switched) Geom.switchOrientation(orn);
  7172. Group.contract(nodes, $.merge(this.controller, onComplete));
  7173. },
  7174. move: function(node, onComplete) {
  7175. this.compute('end', false);
  7176. var move = onComplete.Move, offset = {
  7177. 'x': move.offsetX,
  7178. 'y': move.offsetY
  7179. };
  7180. if(move.enable) {
  7181. this.geom.translate(node.endPos.add(offset).$scale(-1), "end");
  7182. }
  7183. this.fx.animate($.merge(this.controller, { modes: ['linear'] }, onComplete));
  7184. },
  7185. expand: function (node, onComplete) {
  7186. var nodeArray = getNodesToShow.call(this, node);
  7187. this.group.expand(nodeArray, $.merge(this.controller, onComplete));
  7188. },
  7189. selectPath: function(node) {
  7190. var that = this;
  7191. this.graph.eachNode(function(n) { n.selected = false; });
  7192. function path(node) {
  7193. if(node == null || node.selected) return;
  7194. node.selected = true;
  7195. $.each(that.group.getSiblings([node])[node.id],
  7196. function(n) {
  7197. n.exist = true;
  7198. n.drawn = true;
  7199. });
  7200. var parents = node.getParents();
  7201. parents = (parents.length > 0)? parents[0] : null;
  7202. path(parents);
  7203. };
  7204. for(var i=0, ns = [node.id].concat(nodesInPath); i < ns.length; i++) {
  7205. path(this.graph.getNode(ns[i]));
  7206. }
  7207. },
  7208. /*
  7209. Method: setRoot
  7210. Switches the current root node. Changes the topology of the Tree.
  7211. Parameters:
  7212. id - (string) The id of the node to be set as root.
  7213. method - (string) Set this to "animate" if you want to animate the tree after adding the subtree. You can also set this parameter to "replot" to just replot the subtree.
  7214. onComplete - (optional|object) An action to perform after the animation (if any).
  7215. Example:
  7216. (start code js)
  7217. st.setRoot('nodeId', 'animate', {
  7218. onComplete: function() {
  7219. alert('complete!');
  7220. }
  7221. });
  7222. (end code)
  7223. */
  7224. setRoot: function(id, method, onComplete) {
  7225. if(this.busy) return;
  7226. this.busy = true;
  7227. var that = this, canvas = this.canvas;
  7228. var rootNode = this.graph.getNode(this.root);
  7229. var clickedNode = this.graph.getNode(id);
  7230. function $setRoot() {
  7231. if(this.config.multitree && clickedNode.data.$orn) {
  7232. var orn = clickedNode.data.$orn;
  7233. var opp = {
  7234. 'left': 'right',
  7235. 'right': 'left',
  7236. 'top': 'bottom',
  7237. 'bottom': 'top'
  7238. }[orn];
  7239. rootNode.data.$orn = opp;
  7240. (function tag(rootNode) {
  7241. rootNode.eachSubnode(function(n) {
  7242. if(n.id != id) {
  7243. n.data.$orn = opp;
  7244. tag(n);
  7245. }
  7246. });
  7247. })(rootNode);
  7248. delete clickedNode.data.$orn;
  7249. }
  7250. this.root = id;
  7251. this.clickedNode = clickedNode;
  7252. this.graph.computeLevels(this.root, 0, "ignore");
  7253. this.geom.setRightLevelToShow(clickedNode, canvas, {
  7254. execHide: false,
  7255. onShow: function(node) {
  7256. if(!node.drawn) {
  7257. node.drawn = true;
  7258. node.setData('alpha', 1, 'end');
  7259. node.setData('alpha', 0);
  7260. node.pos.setc(clickedNode.pos.x, clickedNode.pos.y);
  7261. }
  7262. }
  7263. });
  7264. this.compute('end');
  7265. this.busy = true;
  7266. this.fx.animate({
  7267. modes: ['linear', 'node-property:alpha'],
  7268. onComplete: function() {
  7269. that.busy = false;
  7270. that.onClick(id, {
  7271. onComplete: function() {
  7272. onComplete && onComplete.onComplete();
  7273. }
  7274. });
  7275. }
  7276. });
  7277. }
  7278. // delete previous orientations (if any)
  7279. delete rootNode.data.$orns;
  7280. if(method == 'animate') {
  7281. $setRoot.call(this);
  7282. that.selectPath(clickedNode);
  7283. } else if(method == 'replot') {
  7284. $setRoot.call(this);
  7285. this.select(this.root);
  7286. }
  7287. },
  7288. /*
  7289. Method: addSubtree
  7290. Adds a subtree.
  7291. Parameters:
  7292. subtree - (object) A JSON Tree object. See also <Loader.loadJSON>.
  7293. method - (string) Set this to "animate" if you want to animate the tree after adding the subtree. You can also set this parameter to "replot" to just replot the subtree.
  7294. onComplete - (optional|object) An action to perform after the animation (if any).
  7295. Example:
  7296. (start code js)
  7297. st.addSubtree(json, 'animate', {
  7298. onComplete: function() {
  7299. alert('complete!');
  7300. }
  7301. });
  7302. (end code)
  7303. */
  7304. addSubtree: function(subtree, method, onComplete) {
  7305. if(method == 'replot') {
  7306. this.op.sum(subtree, $.extend({ type: 'replot' }, onComplete || {}));
  7307. } else if (method == 'animate') {
  7308. this.op.sum(subtree, $.extend({ type: 'fade:seq' }, onComplete || {}));
  7309. }
  7310. },
  7311. /*
  7312. Method: removeSubtree
  7313. Removes a subtree.
  7314. Parameters:
  7315. id - (string) The _id_ of the subtree to be removed.
  7316. removeRoot - (boolean) Default's *false*. Remove the root of the subtree or only its subnodes.
  7317. method - (string) Set this to "animate" if you want to animate the tree after removing the subtree. You can also set this parameter to "replot" to just replot the subtree.
  7318. onComplete - (optional|object) An action to perform after the animation (if any).
  7319. Example:
  7320. (start code js)
  7321. st.removeSubtree('idOfSubtreeToBeRemoved', false, 'animate', {
  7322. onComplete: function() {
  7323. alert('complete!');
  7324. }
  7325. });
  7326. (end code)
  7327. */
  7328. removeSubtree: function(id, removeRoot, method, onComplete) {
  7329. var node = this.graph.getNode(id), subids = [];
  7330. node.eachLevel(+!removeRoot, false, function(n) {
  7331. subids.push(n.id);
  7332. });
  7333. if(method == 'replot') {
  7334. this.op.removeNode(subids, $.extend({ type: 'replot' }, onComplete || {}));
  7335. } else if (method == 'animate') {
  7336. this.op.removeNode(subids, $.extend({ type: 'fade:seq'}, onComplete || {}));
  7337. }
  7338. },
  7339. /*
  7340. Method: select
  7341. Selects a node in the <ST> without performing an animation. Useful when selecting
  7342. nodes which are currently hidden or deep inside the tree.
  7343. Parameters:
  7344. id - (string) The id of the node to select.
  7345. onComplete - (optional|object) an onComplete callback.
  7346. Example:
  7347. (start code js)
  7348. st.select('mynodeid', {
  7349. onComplete: function() {
  7350. alert('complete!');
  7351. }
  7352. });
  7353. (end code)
  7354. */
  7355. select: function(id, onComplete) {
  7356. var group = this.group, geom = this.geom;
  7357. var node= this.graph.getNode(id), canvas = this.canvas;
  7358. var root = this.graph.getNode(this.root);
  7359. var complete = $.merge(this.controller, onComplete);
  7360. var that = this;
  7361. complete.onBeforeCompute(node);
  7362. this.selectPath(node);
  7363. this.clickedNode= node;
  7364. this.requestNodes(node, {
  7365. onComplete: function(){
  7366. group.hide(group.prepare(getNodesToHide.call(that)), complete);
  7367. geom.setRightLevelToShow(node, canvas);
  7368. that.compute("current");
  7369. that.graph.eachNode(function(n) {
  7370. var pos = n.pos.getc(true);
  7371. n.startPos.setc(pos.x, pos.y);
  7372. n.endPos.setc(pos.x, pos.y);
  7373. n.visited = false;
  7374. });
  7375. var offset = { x: complete.offsetX, y: complete.offsetY };
  7376. that.geom.translate(node.endPos.add(offset).$scale(-1), ["start", "current", "end"]);
  7377. group.show(getNodesToShow.call(that));
  7378. that.plot();
  7379. complete.onAfterCompute(that.clickedNode);
  7380. complete.onComplete();
  7381. }
  7382. });
  7383. },
  7384. /*
  7385. Method: onClick
  7386. Animates the <ST> to center the node specified by *id*.
  7387. Parameters:
  7388. id - (string) A node id.
  7389. options - (optional|object) A group of options and callbacks described below.
  7390. onComplete - (object) An object callback called when the animation finishes.
  7391. Move - (object) An object that has as properties _offsetX_ or _offsetY_ for adding some offset position to the centered node.
  7392. Example:
  7393. (start code js)
  7394. st.onClick('mynodeid', {
  7395. Move: {
  7396. enable: true,
  7397. offsetX: 30,
  7398. offsetY: 5
  7399. },
  7400. onComplete: function() {
  7401. alert('yay!');
  7402. }
  7403. });
  7404. (end code)
  7405. */
  7406. onClick: function (id, options) {
  7407. var canvas = this.canvas, that = this, Geom = this.geom, config = this.config;
  7408. var innerController = {
  7409. Move: {
  7410. enable: true,
  7411. offsetX: config.offsetX || 0,
  7412. offsetY: config.offsetY || 0
  7413. },
  7414. setRightLevelToShowConfig: false,
  7415. onBeforeRequest: $.empty,
  7416. onBeforeContract: $.empty,
  7417. onBeforeMove: $.empty,
  7418. onBeforeExpand: $.empty
  7419. };
  7420. var complete = $.merge(this.controller, innerController, options);
  7421. if(!this.busy) {
  7422. this.busy = true;
  7423. var node = this.graph.getNode(id);
  7424. this.selectPath(node, this.clickedNode);
  7425. this.clickedNode = node;
  7426. complete.onBeforeCompute(node);
  7427. complete.onBeforeRequest(node);
  7428. this.requestNodes(node, {
  7429. onComplete: function() {
  7430. complete.onBeforeContract(node);
  7431. that.contract({
  7432. onComplete: function() {
  7433. Geom.setRightLevelToShow(node, canvas, complete.setRightLevelToShowConfig);
  7434. complete.onBeforeMove(node);
  7435. that.move(node, {
  7436. Move: complete.Move,
  7437. onComplete: function() {
  7438. complete.onBeforeExpand(node);
  7439. that.expand(node, {
  7440. onComplete: function() {
  7441. that.busy = false;
  7442. complete.onAfterCompute(id);
  7443. complete.onComplete();
  7444. }
  7445. }); // expand
  7446. }
  7447. }); // move
  7448. }
  7449. });// contract
  7450. }
  7451. });// request
  7452. }
  7453. }
  7454. });
  7455. })();
  7456. $jit.ST.$extend = true;
  7457. /*
  7458. Class: ST.Op
  7459. Custom extension of <Graph.Op>.
  7460. Extends:
  7461. All <Graph.Op> methods
  7462. See also:
  7463. <Graph.Op>
  7464. */
  7465. $jit.ST.Op = new Class({
  7466. Implements: Graph.Op
  7467. });
  7468. /*
  7469. Performs operations on group of nodes.
  7470. */
  7471. $jit.ST.Group = new Class({
  7472. initialize: function(viz) {
  7473. this.viz = viz;
  7474. this.canvas = viz.canvas;
  7475. this.config = viz.config;
  7476. this.animation = new Animation;
  7477. this.nodes = null;
  7478. },
  7479. /*
  7480. Calls the request method on the controller to request a subtree for each node.
  7481. */
  7482. requestNodes: function(nodes, controller) {
  7483. var counter = 0, len = nodes.length, nodeSelected = {};
  7484. var complete = function() { controller.onComplete(); };
  7485. var viz = this.viz;
  7486. if(len == 0) complete();
  7487. for(var i=0; i<len; i++) {
  7488. nodeSelected[nodes[i].id] = nodes[i];
  7489. controller.request(nodes[i].id, nodes[i]._level, {
  7490. onComplete: function(nodeId, data) {
  7491. if(data && data.children) {
  7492. data.id = nodeId;
  7493. viz.op.sum(data, { type: 'nothing' });
  7494. }
  7495. if(++counter == len) {
  7496. viz.graph.computeLevels(viz.root, 0);
  7497. complete();
  7498. }
  7499. }
  7500. });
  7501. }
  7502. },
  7503. /*
  7504. Collapses group of nodes.
  7505. */
  7506. contract: function(nodes, controller) {
  7507. var viz = this.viz;
  7508. var that = this;
  7509. nodes = this.prepare(nodes);
  7510. this.animation.setOptions($.merge(controller, {
  7511. $animating: false,
  7512. compute: function(delta) {
  7513. if(delta == 1) delta = 0.99;
  7514. that.plotStep(1 - delta, controller, this.$animating);
  7515. this.$animating = 'contract';
  7516. },
  7517. complete: function() {
  7518. that.hide(nodes, controller);
  7519. }
  7520. })).start();
  7521. },
  7522. hide: function(nodes, controller) {
  7523. var viz = this.viz;
  7524. for(var i=0; i<nodes.length; i++) {
  7525. // TODO nodes are requested on demand, but not
  7526. // deleted when hidden. Would that be a good feature?
  7527. // Currently that feature is buggy, so I'll turn it off
  7528. // Actually this feature is buggy because trimming should take
  7529. // place onAfterCompute and not right after collapsing nodes.
  7530. if (true || !controller || !controller.request) {
  7531. nodes[i].eachLevel(1, false, function(elem){
  7532. if (elem.exist) {
  7533. $.extend(elem, {
  7534. 'drawn': false,
  7535. 'exist': false
  7536. });
  7537. }
  7538. });
  7539. } else {
  7540. var ids = [];
  7541. nodes[i].eachLevel(1, false, function(n) {
  7542. ids.push(n.id);
  7543. });
  7544. viz.op.removeNode(ids, { 'type': 'nothing' });
  7545. viz.labels.clearLabels();
  7546. }
  7547. }
  7548. controller.onComplete();
  7549. },
  7550. /*
  7551. Expands group of nodes.
  7552. */
  7553. expand: function(nodes, controller) {
  7554. var that = this;
  7555. this.show(nodes);
  7556. this.animation.setOptions($.merge(controller, {
  7557. $animating: false,
  7558. compute: function(delta) {
  7559. that.plotStep(delta, controller, this.$animating);
  7560. this.$animating = 'expand';
  7561. },
  7562. complete: function() {
  7563. that.plotStep(undefined, controller, false);
  7564. controller.onComplete();
  7565. }
  7566. })).start();
  7567. },
  7568. show: function(nodes) {
  7569. var config = this.config;
  7570. this.prepare(nodes);
  7571. $.each(nodes, function(n) {
  7572. // check for root nodes if multitree
  7573. if(config.multitree && !('$orn' in n.data)) {
  7574. delete n.data.$orns;
  7575. var orns = ' ';
  7576. n.eachSubnode(function(ch) {
  7577. if(('$orn' in ch.data)
  7578. && orns.indexOf(ch.data.$orn) < 0
  7579. && ch.exist && !ch.drawn) {
  7580. orns += ch.data.$orn + ' ';
  7581. }
  7582. });
  7583. n.data.$orns = orns;
  7584. }
  7585. n.eachLevel(0, config.levelsToShow, function(n) {
  7586. if(n.exist) n.drawn = true;
  7587. });
  7588. });
  7589. },
  7590. prepare: function(nodes) {
  7591. this.nodes = this.getNodesWithChildren(nodes);
  7592. return this.nodes;
  7593. },
  7594. /*
  7595. Filters an array of nodes leaving only nodes with children.
  7596. */
  7597. getNodesWithChildren: function(nodes) {
  7598. var ans = [], config = this.config, root = this.viz.root;
  7599. nodes.sort(function(a, b) { return (a._depth <= b._depth) - (a._depth >= b._depth); });
  7600. for(var i=0; i<nodes.length; i++) {
  7601. if(nodes[i].anySubnode("exist")) {
  7602. for (var j = i+1, desc = false; !desc && j < nodes.length; j++) {
  7603. if(!config.multitree || '$orn' in nodes[j].data) {
  7604. desc = desc || nodes[i].isDescendantOf(nodes[j].id);
  7605. }
  7606. }
  7607. if(!desc) ans.push(nodes[i]);
  7608. }
  7609. }
  7610. return ans;
  7611. },
  7612. plotStep: function(delta, controller, animating) {
  7613. var viz = this.viz,
  7614. config = this.config,
  7615. canvas = viz.canvas,
  7616. ctx = canvas.getCtx(),
  7617. nodes = this.nodes;
  7618. var i, node;
  7619. // hide nodes that are meant to be collapsed/expanded
  7620. var nds = {};
  7621. for(i=0; i<nodes.length; i++) {
  7622. node = nodes[i];
  7623. nds[node.id] = [];
  7624. var root = config.multitree && !('$orn' in node.data);
  7625. var orns = root && node.data.$orns;
  7626. node.eachSubgraph(function(n) {
  7627. // Cleanup
  7628. // special check for root node subnodes when
  7629. // multitree is checked.
  7630. if(root && orns && orns.indexOf(n.data.$orn) > 0
  7631. && n.drawn) {
  7632. n.drawn = false;
  7633. nds[node.id].push(n);
  7634. } else if((!root || !orns) && n.drawn) {
  7635. n.drawn = false;
  7636. nds[node.id].push(n);
  7637. }
  7638. });
  7639. node.drawn = true;
  7640. }
  7641. // plot the whole (non-scaled) tree
  7642. if(nodes.length > 0) viz.fx.plot();
  7643. // show nodes that were previously hidden
  7644. for(i in nds) {
  7645. $.each(nds[i], function(n) { n.drawn = true; });
  7646. }
  7647. // plot each scaled subtree
  7648. for(i=0; i<nodes.length; i++) {
  7649. node = nodes[i];
  7650. ctx.save();
  7651. viz.fx.plotSubtree(node, controller, delta, animating);
  7652. ctx.restore();
  7653. }
  7654. },
  7655. getSiblings: function(nodes) {
  7656. var siblings = {};
  7657. $.each(nodes, function(n) {
  7658. var par = n.getParents();
  7659. if (par.length == 0) {
  7660. siblings[n.id] = [n];
  7661. } else {
  7662. var ans = [];
  7663. par[0].eachSubnode(function(sn) {
  7664. ans.push(sn);
  7665. });
  7666. siblings[n.id] = ans;
  7667. }
  7668. });
  7669. return siblings;
  7670. }
  7671. });
  7672. /*
  7673. ST.Geom
  7674. Performs low level geometrical computations.
  7675. Access:
  7676. This instance can be accessed with the _geom_ parameter of the st instance created.
  7677. Example:
  7678. (start code js)
  7679. var st = new ST(canvas, config);
  7680. st.geom.translate //or can also call any other <ST.Geom> method
  7681. (end code)
  7682. */
  7683. $jit.ST.Geom = new Class({
  7684. Implements: Graph.Geom,
  7685. /*
  7686. Changes the tree current orientation to the one specified.
  7687. You should usually use <ST.switchPosition> instead.
  7688. */
  7689. switchOrientation: function(orn) {
  7690. this.config.orientation = orn;
  7691. },
  7692. /*
  7693. Makes a value dispatch according to the current layout
  7694. Works like a CSS property, either _top-right-bottom-left_ or _top|bottom - left|right_.
  7695. */
  7696. dispatch: function() {
  7697. // TODO(nico) should store Array.prototype.slice.call somewhere.
  7698. var args = Array.prototype.slice.call(arguments);
  7699. var s = args.shift(), len = args.length;
  7700. var val = function(a) { return typeof a == 'function'? a() : a; };
  7701. if(len == 2) {
  7702. return (s == "top" || s == "bottom")? val(args[0]) : val(args[1]);
  7703. } else if(len == 4) {
  7704. switch(s) {
  7705. case "top": return val(args[0]);
  7706. case "right": return val(args[1]);
  7707. case "bottom": return val(args[2]);
  7708. case "left": return val(args[3]);
  7709. }
  7710. }
  7711. return undefined;
  7712. },
  7713. /*
  7714. Returns label height or with, depending on the tree current orientation.
  7715. */
  7716. getSize: function(n, invert) {
  7717. var data = n.data, config = this.config;
  7718. var siblingOffset = config.siblingOffset;
  7719. var s = (config.multitree
  7720. && ('$orn' in data)
  7721. && data.$orn) || config.orientation;
  7722. var w = n.getData('width') + siblingOffset;
  7723. var h = n.getData('height') + siblingOffset;
  7724. if(!invert)
  7725. return this.dispatch(s, h, w);
  7726. else
  7727. return this.dispatch(s, w, h);
  7728. },
  7729. /*
  7730. Calculates a subtree base size. This is an utility function used by _getBaseSize_
  7731. */
  7732. getTreeBaseSize: function(node, level, leaf) {
  7733. var size = this.getSize(node, true), baseHeight = 0, that = this;
  7734. if(leaf(level, node)) return size;
  7735. if(level === 0) return 0;
  7736. node.eachSubnode(function(elem) {
  7737. baseHeight += that.getTreeBaseSize(elem, level -1, leaf);
  7738. });
  7739. return (size > baseHeight? size : baseHeight) + this.config.subtreeOffset;
  7740. },
  7741. /*
  7742. getEdge
  7743. Returns a Complex instance with the begin or end position of the edge to be plotted.
  7744. Parameters:
  7745. node - A <Graph.Node> that is connected to this edge.
  7746. type - Returns the begin or end edge position. Possible values are 'begin' or 'end'.
  7747. Returns:
  7748. A <Complex> number specifying the begin or end position.
  7749. */
  7750. getEdge: function(node, type, s) {
  7751. var $C = function(a, b) {
  7752. return function(){
  7753. return node.pos.add(new Complex(a, b));
  7754. };
  7755. };
  7756. var dim = this.node;
  7757. var w = node.getData('width');
  7758. var h = node.getData('height');
  7759. if(type == 'begin') {
  7760. if(dim.align == "center") {
  7761. return this.dispatch(s, $C(0, h/2), $C(-w/2, 0),
  7762. $C(0, -h/2),$C(w/2, 0));
  7763. } else if(dim.align == "left") {
  7764. return this.dispatch(s, $C(0, h), $C(0, 0),
  7765. $C(0, 0), $C(w, 0));
  7766. } else if(dim.align == "right") {
  7767. return this.dispatch(s, $C(0, 0), $C(-w, 0),
  7768. $C(0, -h),$C(0, 0));
  7769. } else throw "align: not implemented";
  7770. } else if(type == 'end') {
  7771. if(dim.align == "center") {
  7772. return this.dispatch(s, $C(0, -h/2), $C(w/2, 0),
  7773. $C(0, h/2), $C(-w/2, 0));
  7774. } else if(dim.align == "left") {
  7775. return this.dispatch(s, $C(0, 0), $C(w, 0),
  7776. $C(0, h), $C(0, 0));
  7777. } else if(dim.align == "right") {
  7778. return this.dispatch(s, $C(0, -h),$C(0, 0),
  7779. $C(0, 0), $C(-w, 0));
  7780. } else throw "align: not implemented";
  7781. }
  7782. },
  7783. /*
  7784. Adjusts the tree position due to canvas scaling or translation.
  7785. */
  7786. getScaledTreePosition: function(node, scale) {
  7787. var dim = this.node;
  7788. var w = node.getData('width');
  7789. var h = node.getData('height');
  7790. var s = (this.config.multitree
  7791. && ('$orn' in node.data)
  7792. && node.data.$orn) || this.config.orientation;
  7793. var $C = function(a, b) {
  7794. return function(){
  7795. return node.pos.add(new Complex(a, b)).$scale(1 - scale);
  7796. };
  7797. };
  7798. if(dim.align == "left") {
  7799. return this.dispatch(s, $C(0, h), $C(0, 0),
  7800. $C(0, 0), $C(w, 0));
  7801. } else if(dim.align == "center") {
  7802. return this.dispatch(s, $C(0, h / 2), $C(-w / 2, 0),
  7803. $C(0, -h / 2),$C(w / 2, 0));
  7804. } else if(dim.align == "right") {
  7805. return this.dispatch(s, $C(0, 0), $C(-w, 0),
  7806. $C(0, -h),$C(0, 0));
  7807. } else throw "align: not implemented";
  7808. },
  7809. /*
  7810. treeFitsInCanvas
  7811. Returns a Boolean if the current subtree fits in canvas.
  7812. Parameters:
  7813. node - A <Graph.Node> which is the current root of the subtree.
  7814. canvas - The <Canvas> object.
  7815. level - The depth of the subtree to be considered.
  7816. */
  7817. treeFitsInCanvas: function(node, canvas, level) {
  7818. var csize = canvas.getSize();
  7819. var s = (this.config.multitree
  7820. && ('$orn' in node.data)
  7821. && node.data.$orn) || this.config.orientation;
  7822. var size = this.dispatch(s, csize.width, csize.height);
  7823. var baseSize = this.getTreeBaseSize(node, level, function(level, node) {
  7824. return level === 0 || !node.anySubnode();
  7825. });
  7826. return (baseSize < size);
  7827. }
  7828. });
  7829. /*
  7830. Class: ST.Plot
  7831. Custom extension of <Graph.Plot>.
  7832. Extends:
  7833. All <Graph.Plot> methods
  7834. See also:
  7835. <Graph.Plot>
  7836. */
  7837. $jit.ST.Plot = new Class({
  7838. Implements: Graph.Plot,
  7839. /*
  7840. Plots a subtree from the spacetree.
  7841. */
  7842. plotSubtree: function(node, opt, scale, animating) {
  7843. var viz = this.viz, canvas = viz.canvas, config = viz.config;
  7844. scale = Math.min(Math.max(0.001, scale), 1);
  7845. if(scale >= 0) {
  7846. node.drawn = false;
  7847. var ctx = canvas.getCtx();
  7848. var diff = viz.geom.getScaledTreePosition(node, scale);
  7849. ctx.translate(diff.x, diff.y);
  7850. ctx.scale(scale, scale);
  7851. }
  7852. this.plotTree(node, $.merge(opt, {
  7853. 'withLabels': true,
  7854. 'hideLabels': !!scale,
  7855. 'plotSubtree': function(n, ch) {
  7856. var root = config.multitree && !('$orn' in node.data);
  7857. var orns = root && node.getData('orns');
  7858. return !root || orns.indexOf(node.getData('orn')) > -1;
  7859. }
  7860. }), animating);
  7861. if(scale >= 0) node.drawn = true;
  7862. },
  7863. /*
  7864. Method: getAlignedPos
  7865. Returns a *x, y* object with the position of the top/left corner of a <ST> node.
  7866. Parameters:
  7867. pos - (object) A <Graph.Node> position.
  7868. width - (number) The width of the node.
  7869. height - (number) The height of the node.
  7870. */
  7871. getAlignedPos: function(pos, width, height) {
  7872. var nconfig = this.node;
  7873. var square, orn;
  7874. if(nconfig.align == "center") {
  7875. square = {
  7876. x: pos.x - width / 2,
  7877. y: pos.y - height / 2
  7878. };
  7879. } else if (nconfig.align == "left") {
  7880. orn = this.config.orientation;
  7881. if(orn == "bottom" || orn == "top") {
  7882. square = {
  7883. x: pos.x - width / 2,
  7884. y: pos.y
  7885. };
  7886. } else {
  7887. square = {
  7888. x: pos.x,
  7889. y: pos.y - height / 2
  7890. };
  7891. }
  7892. } else if(nconfig.align == "right") {
  7893. orn = this.config.orientation;
  7894. if(orn == "bottom" || orn == "top") {
  7895. square = {
  7896. x: pos.x - width / 2,
  7897. y: pos.y - height
  7898. };
  7899. } else {
  7900. square = {
  7901. x: pos.x - width,
  7902. y: pos.y - height / 2
  7903. };
  7904. }
  7905. } else throw "align: not implemented";
  7906. return square;
  7907. },
  7908. getOrientation: function(adj) {
  7909. var config = this.config;
  7910. var orn = config.orientation;
  7911. if(config.multitree) {
  7912. var nodeFrom = adj.nodeFrom;
  7913. var nodeTo = adj.nodeTo;
  7914. orn = (('$orn' in nodeFrom.data)
  7915. && nodeFrom.data.$orn)
  7916. || (('$orn' in nodeTo.data)
  7917. && nodeTo.data.$orn);
  7918. }
  7919. return orn;
  7920. }
  7921. });
  7922. /*
  7923. Class: ST.Label
  7924. Custom extension of <Graph.Label>.
  7925. Contains custom <Graph.Label.SVG>, <Graph.Label.HTML> and <Graph.Label.Native> extensions.
  7926. Extends:
  7927. All <Graph.Label> methods and subclasses.
  7928. See also:
  7929. <Graph.Label>, <Graph.Label.Native>, <Graph.Label.HTML>, <Graph.Label.SVG>.
  7930. */
  7931. $jit.ST.Label = {};
  7932. /*
  7933. ST.Label.Native
  7934. Custom extension of <Graph.Label.Native>.
  7935. Extends:
  7936. All <Graph.Label.Native> methods
  7937. See also:
  7938. <Graph.Label.Native>
  7939. */
  7940. $jit.ST.Label.Native = new Class({
  7941. Implements: Graph.Label.Native,
  7942. renderLabel: function(canvas, node, controller) {
  7943. var ctx = canvas.getCtx(),
  7944. coord = node.pos.getc(true),
  7945. width = node.getData('width'),
  7946. height = node.getData('height'),
  7947. pos = this.viz.fx.getAlignedPos(coord, width, height);
  7948. ctx.fillText(node.name, pos.x + width / 2, pos.y + height / 2);
  7949. }
  7950. });
  7951. $jit.ST.Label.DOM = new Class({
  7952. Implements: Graph.Label.DOM,
  7953. /*
  7954. placeLabel
  7955. Overrides abstract method placeLabel in <Graph.Plot>.
  7956. Parameters:
  7957. tag - A DOM label element.
  7958. node - A <Graph.Node>.
  7959. controller - A configuration/controller object passed to the visualization.
  7960. */
  7961. placeLabel: function(tag, node, controller) {
  7962. var pos = node.pos.getc(true),
  7963. config = this.viz.config,
  7964. dim = config.Node,
  7965. canvas = this.viz.canvas,
  7966. w = node.getData('width'),
  7967. h = node.getData('height'),
  7968. radius = canvas.getSize(),
  7969. labelPos, orn;
  7970. var ox = canvas.translateOffsetX,
  7971. oy = canvas.translateOffsetY,
  7972. sx = canvas.scaleOffsetX,
  7973. sy = canvas.scaleOffsetY,
  7974. posx = pos.x * sx + ox,
  7975. posy = pos.y * sy + oy;
  7976. if(dim.align == "center") {
  7977. labelPos= {
  7978. x: Math.round(posx - w / 2 + radius.width/2),
  7979. y: Math.round(posy - h / 2 + radius.height/2)
  7980. };
  7981. } else if (dim.align == "left") {
  7982. orn = config.orientation;
  7983. if(orn == "bottom" || orn == "top") {
  7984. labelPos= {
  7985. x: Math.round(posx - w / 2 + radius.width/2),
  7986. y: Math.round(posy + radius.height/2)
  7987. };
  7988. } else {
  7989. labelPos= {
  7990. x: Math.round(posx + radius.width/2),
  7991. y: Math.round(posy - h / 2 + radius.height/2)
  7992. };
  7993. }
  7994. } else if(dim.align == "right") {
  7995. orn = config.orientation;
  7996. if(orn == "bottom" || orn == "top") {
  7997. labelPos= {
  7998. x: Math.round(posx - w / 2 + radius.width/2),
  7999. y: Math.round(posy - h + radius.height/2)
  8000. };
  8001. } else {
  8002. labelPos= {
  8003. x: Math.round(posx - w + radius.width/2),
  8004. y: Math.round(posy - h / 2 + radius.height/2)
  8005. };
  8006. }
  8007. } else throw "align: not implemented";
  8008. var style = tag.style;
  8009. style.left = labelPos.x + 'px';
  8010. style.top = labelPos.y + 'px';
  8011. style.display = this.fitsInCanvas(labelPos, canvas)? '' : 'none';
  8012. controller.onPlaceLabel(tag, node);
  8013. }
  8014. });
  8015. /*
  8016. ST.Label.SVG
  8017. Custom extension of <Graph.Label.SVG>.
  8018. Extends:
  8019. All <Graph.Label.SVG> methods
  8020. See also:
  8021. <Graph.Label.SVG>
  8022. */
  8023. $jit.ST.Label.SVG = new Class({
  8024. Implements: [$jit.ST.Label.DOM, Graph.Label.SVG],
  8025. initialize: function(viz) {
  8026. this.viz = viz;
  8027. }
  8028. });
  8029. /*
  8030. ST.Label.HTML
  8031. Custom extension of <Graph.Label.HTML>.
  8032. Extends:
  8033. All <Graph.Label.HTML> methods.
  8034. See also:
  8035. <Graph.Label.HTML>
  8036. */
  8037. $jit.ST.Label.HTML = new Class({
  8038. Implements: [$jit.ST.Label.DOM, Graph.Label.HTML],
  8039. initialize: function(viz) {
  8040. this.viz = viz;
  8041. }
  8042. });
  8043. /*
  8044. Class: ST.Plot.NodeTypes
  8045. This class contains a list of <Graph.Node> built-in types.
  8046. Node types implemented are 'none', 'circle', 'rectangle', 'ellipse' and 'square'.
  8047. You can add your custom node types, customizing your visualization to the extreme.
  8048. Example:
  8049. (start code js)
  8050. ST.Plot.NodeTypes.implement({
  8051. 'mySpecialType': {
  8052. 'render': function(node, canvas) {
  8053. //print your custom node to canvas
  8054. },
  8055. //optional
  8056. 'contains': function(node, pos) {
  8057. //return true if pos is inside the node or false otherwise
  8058. }
  8059. }
  8060. });
  8061. (end code)
  8062. */
  8063. $jit.ST.Plot.NodeTypes = new Class({
  8064. 'none': {
  8065. 'render': $.empty,
  8066. 'contains': $.lambda(false)
  8067. },
  8068. 'circle': {
  8069. 'render': function(node, canvas) {
  8070. var dim = node.getData('dim'),
  8071. pos = this.getAlignedPos(node.pos.getc(true), dim, dim),
  8072. dim2 = dim/2;
  8073. this.nodeHelper.circle.render('fill', {x:pos.x+dim2, y:pos.y+dim2}, dim2, canvas);
  8074. },
  8075. 'contains': function(node, pos) {
  8076. var dim = node.getData('dim'),
  8077. npos = this.getAlignedPos(node.pos.getc(true), dim, dim),
  8078. dim2 = dim/2;
  8079. this.nodeHelper.circle.contains({x:npos.x+dim2, y:npos.y+dim2}, pos, dim2);
  8080. }
  8081. },
  8082. 'square': {
  8083. 'render': function(node, canvas) {
  8084. var dim = node.getData('dim'),
  8085. dim2 = dim/2,
  8086. pos = this.getAlignedPos(node.pos.getc(true), dim, dim);
  8087. this.nodeHelper.square.render('fill', {x:pos.x+dim2, y:pos.y+dim2}, dim2, canvas);
  8088. },
  8089. 'contains': function(node, pos) {
  8090. var dim = node.getData('dim'),
  8091. npos = this.getAlignedPos(node.pos.getc(true), dim, dim),
  8092. dim2 = dim/2;
  8093. this.nodeHelper.square.contains({x:npos.x+dim2, y:npos.y+dim2}, pos, dim2);
  8094. }
  8095. },
  8096. 'ellipse': {
  8097. 'render': function(node, canvas) {
  8098. var width = node.getData('width'),
  8099. height = node.getData('height'),
  8100. pos = this.getAlignedPos(node.pos.getc(true), width, height);
  8101. this.nodeHelper.ellipse.render('fill', {x:pos.x+width/2, y:pos.y+height/2}, width, height, canvas);
  8102. },
  8103. 'contains': function(node, pos) {
  8104. var width = node.getData('width'),
  8105. height = node.getData('height'),
  8106. npos = this.getAlignedPos(node.pos.getc(true), width, height);
  8107. this.nodeHelper.ellipse.contains({x:npos.x+width/2, y:npos.y+height/2}, pos, width, height);
  8108. }
  8109. },
  8110. 'rectangle': {
  8111. 'render': function(node, canvas) {
  8112. var width = node.getData('width'),
  8113. height = node.getData('height'),
  8114. pos = this.getAlignedPos(node.pos.getc(true), width, height);
  8115. this.nodeHelper.rectangle.render('fill', {x:pos.x+width/2, y:pos.y+height/2}, width, height, canvas);
  8116. },
  8117. 'contains': function(node, pos) {
  8118. var width = node.getData('width'),
  8119. height = node.getData('height'),
  8120. npos = this.getAlignedPos(node.pos.getc(true), width, height);
  8121. this.nodeHelper.rectangle.contains({x:npos.x+width/2, y:npos.y+height/2}, pos, width, height);
  8122. }
  8123. }
  8124. });
  8125. /*
  8126. Class: ST.Plot.EdgeTypes
  8127. This class contains a list of <Graph.Adjacence> built-in types.
  8128. Edge types implemented are 'none', 'line', 'arrow', 'quadratic:begin', 'quadratic:end', 'bezier'.
  8129. You can add your custom edge types, customizing your visualization to the extreme.
  8130. Example:
  8131. (start code js)
  8132. ST.Plot.EdgeTypes.implement({
  8133. 'mySpecialType': {
  8134. 'render': function(adj, canvas) {
  8135. //print your custom edge to canvas
  8136. },
  8137. //optional
  8138. 'contains': function(adj, pos) {
  8139. //return true if pos is inside the arc or false otherwise
  8140. }
  8141. }
  8142. });
  8143. (end code)
  8144. */
  8145. $jit.ST.Plot.EdgeTypes = new Class({
  8146. 'none': $.empty,
  8147. 'line': {
  8148. 'render': function(adj, canvas) {
  8149. var orn = this.getOrientation(adj),
  8150. nodeFrom = adj.nodeFrom,
  8151. nodeTo = adj.nodeTo,
  8152. rel = nodeFrom._depth < nodeTo._depth,
  8153. from = this.viz.geom.getEdge(rel? nodeFrom:nodeTo, 'begin', orn),
  8154. to = this.viz.geom.getEdge(rel? nodeTo:nodeFrom, 'end', orn);
  8155. this.edgeHelper.line.render(from, to, canvas);
  8156. },
  8157. 'contains': function(adj, pos) {
  8158. var orn = this.getOrientation(adj),
  8159. nodeFrom = adj.nodeFrom,
  8160. nodeTo = adj.nodeTo,
  8161. rel = nodeFrom._depth < nodeTo._depth,
  8162. from = this.viz.geom.getEdge(rel? nodeFrom:nodeTo, 'begin', orn),
  8163. to = this.viz.geom.getEdge(rel? nodeTo:nodeFrom, 'end', orn);
  8164. return this.edgeHelper.line.contains(from, to, pos, this.edge.epsilon);
  8165. }
  8166. },
  8167. 'arrow': {
  8168. 'render': function(adj, canvas) {
  8169. var orn = this.getOrientation(adj),
  8170. node = adj.nodeFrom,
  8171. child = adj.nodeTo,
  8172. dim = adj.getData('dim'),
  8173. from = this.viz.geom.getEdge(node, 'begin', orn),
  8174. to = this.viz.geom.getEdge(child, 'end', orn),
  8175. direction = adj.data.$direction,
  8176. inv = (direction && direction.length>1 && direction[0] != node.id);
  8177. this.edgeHelper.arrow.render(from, to, dim, inv, canvas);
  8178. },
  8179. 'contains': function(adj, pos) {
  8180. var orn = this.getOrientation(adj),
  8181. nodeFrom = adj.nodeFrom,
  8182. nodeTo = adj.nodeTo,
  8183. rel = nodeFrom._depth < nodeTo._depth,
  8184. from = this.viz.geom.getEdge(rel? nodeFrom:nodeTo, 'begin', orn),
  8185. to = this.viz.geom.getEdge(rel? nodeTo:nodeFrom, 'end', orn);
  8186. return this.edgeHelper.arrow.contains(from, to, pos, this.edge.epsilon);
  8187. }
  8188. },
  8189. 'quadratic:begin': {
  8190. 'render': function(adj, canvas) {
  8191. var orn = this.getOrientation(adj);
  8192. var nodeFrom = adj.nodeFrom,
  8193. nodeTo = adj.nodeTo,
  8194. rel = nodeFrom._depth < nodeTo._depth,
  8195. begin = this.viz.geom.getEdge(rel? nodeFrom:nodeTo, 'begin', orn),
  8196. end = this.viz.geom.getEdge(rel? nodeTo:nodeFrom, 'end', orn),
  8197. dim = adj.getData('dim'),
  8198. ctx = canvas.getCtx();
  8199. ctx.beginPath();
  8200. ctx.moveTo(begin.x, begin.y);
  8201. switch(orn) {
  8202. case "left":
  8203. ctx.quadraticCurveTo(begin.x + dim, begin.y, end.x, end.y);
  8204. break;
  8205. case "right":
  8206. ctx.quadraticCurveTo(begin.x - dim, begin.y, end.x, end.y);
  8207. break;
  8208. case "top":
  8209. ctx.quadraticCurveTo(begin.x, begin.y + dim, end.x, end.y);
  8210. break;
  8211. case "bottom":
  8212. ctx.quadraticCurveTo(begin.x, begin.y - dim, end.x, end.y);
  8213. break;
  8214. }
  8215. ctx.stroke();
  8216. }
  8217. },
  8218. 'quadratic:end': {
  8219. 'render': function(adj, canvas) {
  8220. var orn = this.getOrientation(adj);
  8221. var nodeFrom = adj.nodeFrom,
  8222. nodeTo = adj.nodeTo,
  8223. rel = nodeFrom._depth < nodeTo._depth,
  8224. begin = this.viz.geom.getEdge(rel? nodeFrom:nodeTo, 'begin', orn),
  8225. end = this.viz.geom.getEdge(rel? nodeTo:nodeFrom, 'end', orn),
  8226. dim = adj.getData('dim'),
  8227. ctx = canvas.getCtx();
  8228. ctx.beginPath();
  8229. ctx.moveTo(begin.x, begin.y);
  8230. switch(orn) {
  8231. case "left":
  8232. ctx.quadraticCurveTo(end.x - dim, end.y, end.x, end.y);
  8233. break;
  8234. case "right":
  8235. ctx.quadraticCurveTo(end.x + dim, end.y, end.x, end.y);
  8236. break;
  8237. case "top":
  8238. ctx.quadraticCurveTo(end.x, end.y - dim, end.x, end.y);
  8239. break;
  8240. case "bottom":
  8241. ctx.quadraticCurveTo(end.x, end.y + dim, end.x, end.y);
  8242. break;
  8243. }
  8244. ctx.stroke();
  8245. }
  8246. },
  8247. 'bezier': {
  8248. 'render': function(adj, canvas) {
  8249. var orn = this.getOrientation(adj),
  8250. nodeFrom = adj.nodeFrom,
  8251. nodeTo = adj.nodeTo,
  8252. rel = nodeFrom._depth < nodeTo._depth,
  8253. begin = this.viz.geom.getEdge(rel? nodeFrom:nodeTo, 'begin', orn),
  8254. end = this.viz.geom.getEdge(rel? nodeTo:nodeFrom, 'end', orn),
  8255. dim = adj.getData('dim'),
  8256. ctx = canvas.getCtx();
  8257. ctx.beginPath();
  8258. ctx.moveTo(begin.x, begin.y);
  8259. switch(orn) {
  8260. case "left":
  8261. ctx.bezierCurveTo(begin.x + dim, begin.y, end.x - dim, end.y, end.x, end.y);
  8262. break;
  8263. case "right":
  8264. ctx.bezierCurveTo(begin.x - dim, begin.y, end.x + dim, end.y, end.x, end.y);
  8265. break;
  8266. case "top":
  8267. ctx.bezierCurveTo(begin.x, begin.y + dim, end.x, end.y - dim, end.x, end.y);
  8268. break;
  8269. case "bottom":
  8270. ctx.bezierCurveTo(begin.x, begin.y - dim, end.x, end.y + dim, end.x, end.y);
  8271. break;
  8272. }
  8273. ctx.stroke();
  8274. }
  8275. }
  8276. });
  8277. /*
  8278. * File: AreaChart.js
  8279. *
  8280. */
  8281. $jit.ST.Plot.NodeTypes.implement({
  8282. 'areachart-stacked' : {
  8283. 'render' : function(node, canvas) {
  8284. var pos = node.pos.getc(true),
  8285. width = node.getData('width'),
  8286. height = node.getData('height'),
  8287. algnPos = this.getAlignedPos(pos, width, height),
  8288. x = algnPos.x, y = algnPos.y,
  8289. stringArray = node.getData('stringArray'),
  8290. dimArray = node.getData('dimArray'),
  8291. valArray = node.getData('valueArray'),
  8292. valLeft = $.reduce(valArray, function(x, y) { return x + y[0]; }, 0),
  8293. valRight = $.reduce(valArray, function(x, y) { return x + y[1]; }, 0),
  8294. colorArray = node.getData('colorArray'),
  8295. colorLength = colorArray.length,
  8296. config = node.getData('config'),
  8297. gradient = node.getData('gradient'),
  8298. showLabels = config.showLabels,
  8299. aggregates = config.showAggregates,
  8300. label = config.Label,
  8301. prev = node.getData('prev');
  8302. var ctx = canvas.getCtx(), border = node.getData('border');
  8303. if (colorArray && dimArray && stringArray) {
  8304. for (var i=0, l=dimArray.length, acumLeft=0, acumRight=0, valAcum=0; i<l; i++) {
  8305. ctx.fillStyle = ctx.strokeStyle = colorArray[i % colorLength];
  8306. ctx.save();
  8307. if(gradient && (dimArray[i][0] > 0 || dimArray[i][1] > 0)) {
  8308. var h1 = acumLeft + dimArray[i][0],
  8309. h2 = acumRight + dimArray[i][1],
  8310. alpha = Math.atan((h2 - h1) / width),
  8311. delta = 55;
  8312. var linear = ctx.createLinearGradient(x + width/2,
  8313. y - (h1 + h2)/2,
  8314. x + width/2 + delta * Math.sin(alpha),
  8315. y - (h1 + h2)/2 + delta * Math.cos(alpha));
  8316. var color = $.rgbToHex($.map($.hexToRgb(colorArray[i % colorLength].slice(1)),
  8317. function(v) { return (v * 0.85) >> 0; }));
  8318. linear.addColorStop(0, colorArray[i % colorLength]);
  8319. linear.addColorStop(1, color);
  8320. ctx.fillStyle = linear;
  8321. }
  8322. ctx.beginPath();
  8323. ctx.moveTo(x, y - acumLeft);
  8324. ctx.lineTo(x + width, y - acumRight);
  8325. ctx.lineTo(x + width, y - acumRight - dimArray[i][1]);
  8326. ctx.lineTo(x, y - acumLeft - dimArray[i][0]);
  8327. ctx.lineTo(x, y - acumLeft);
  8328. ctx.fill();
  8329. ctx.restore();
  8330. if(border) {
  8331. var strong = border.name == stringArray[i];
  8332. var perc = strong? 0.7 : 0.8;
  8333. var color = $.rgbToHex($.map($.hexToRgb(colorArray[i % colorLength].slice(1)),
  8334. function(v) { return (v * perc) >> 0; }));
  8335. ctx.strokeStyle = color;
  8336. ctx.lineWidth = strong? 4 : 1;
  8337. ctx.save();
  8338. ctx.beginPath();
  8339. if(border.index === 0) {
  8340. ctx.moveTo(x, y - acumLeft);
  8341. ctx.lineTo(x, y - acumLeft - dimArray[i][0]);
  8342. } else {
  8343. ctx.moveTo(x + width, y - acumRight);
  8344. ctx.lineTo(x + width, y - acumRight - dimArray[i][1]);
  8345. }
  8346. ctx.stroke();
  8347. ctx.restore();
  8348. }
  8349. acumLeft += (dimArray[i][0] || 0);
  8350. acumRight += (dimArray[i][1] || 0);
  8351. if(dimArray[i][0] > 0)
  8352. valAcum += (valArray[i][0] || 0);
  8353. }
  8354. if(prev && label.type == 'Native') {
  8355. ctx.save();
  8356. ctx.beginPath();
  8357. ctx.fillStyle = ctx.strokeStyle = label.color;
  8358. ctx.font = label.style + ' ' + label.size + 'px ' + label.family;
  8359. ctx.textAlign = 'center';
  8360. ctx.textBaseline = 'middle';
  8361. var aggValue = aggregates(node.name, valLeft, valRight, node, valAcum);
  8362. if(aggValue !== false) {
  8363. ctx.fillText(aggValue !== true? aggValue : valAcum, x, y - acumLeft - config.labelOffset - label.size/2, width);
  8364. }
  8365. if(showLabels(node.name, valLeft, valRight, node)) {
  8366. ctx.fillText(node.name, x, y + label.size/2 + config.labelOffset);
  8367. }
  8368. ctx.restore();
  8369. }
  8370. }
  8371. },
  8372. 'contains': function(node, mpos) {
  8373. var pos = node.pos.getc(true),
  8374. width = node.getData('width'),
  8375. height = node.getData('height'),
  8376. algnPos = this.getAlignedPos(pos, width, height),
  8377. x = algnPos.x, y = algnPos.y,
  8378. dimArray = node.getData('dimArray'),
  8379. rx = mpos.x - x;
  8380. //bounding box check
  8381. if(mpos.x < x || mpos.x > x + width
  8382. || mpos.y > y || mpos.y < y - height) {
  8383. return false;
  8384. }
  8385. //deep check
  8386. for(var i=0, l=dimArray.length, lAcum=y, rAcum=y; i<l; i++) {
  8387. var dimi = dimArray[i];
  8388. lAcum -= dimi[0];
  8389. rAcum -= dimi[1];
  8390. var intersec = lAcum + (rAcum - lAcum) * rx / width;
  8391. if(mpos.y >= intersec) {
  8392. var index = +(rx > width/2);
  8393. return {
  8394. 'name': node.getData('stringArray')[i],
  8395. 'color': node.getData('colorArray')[i],
  8396. 'value': node.getData('valueArray')[i][index],
  8397. 'index': index
  8398. };
  8399. }
  8400. }
  8401. return false;
  8402. }
  8403. }
  8404. });
  8405. /*
  8406. Class: AreaChart
  8407. A visualization that displays stacked area charts.
  8408. Constructor Options:
  8409. See <Options.AreaChart>.
  8410. */
  8411. $jit.AreaChart = new Class({
  8412. st: null,
  8413. colors: ["#416D9C", "#70A35E", "#EBB056", "#C74243", "#83548B", "#909291", "#557EAA"],
  8414. selected: {},
  8415. busy: false,
  8416. initialize: function(opt) {
  8417. this.controller = this.config =
  8418. $.merge(Options("Canvas", "Margin", "Label", "AreaChart"), {
  8419. Label: { type: 'Native' }
  8420. }, opt);
  8421. //set functions for showLabels and showAggregates
  8422. var showLabels = this.config.showLabels,
  8423. typeLabels = $.type(showLabels),
  8424. showAggregates = this.config.showAggregates,
  8425. typeAggregates = $.type(showAggregates);
  8426. this.config.showLabels = typeLabels == 'function'? showLabels : $.lambda(showLabels);
  8427. this.config.showAggregates = typeAggregates == 'function'? showAggregates : $.lambda(showAggregates);
  8428. this.initializeViz();
  8429. },
  8430. initializeViz: function() {
  8431. var config = this.config,
  8432. that = this,
  8433. nodeType = config.type.split(":")[0],
  8434. nodeLabels = {};
  8435. var delegate = new $jit.ST({
  8436. injectInto: config.injectInto,
  8437. width: config.width,
  8438. height: config.height,
  8439. orientation: "bottom",
  8440. levelDistance: 0,
  8441. siblingOffset: 0,
  8442. subtreeOffset: 0,
  8443. withLabels: config.Label.type != 'Native',
  8444. useCanvas: config.useCanvas,
  8445. Label: {
  8446. type: config.Label.type
  8447. },
  8448. Node: {
  8449. overridable: true,
  8450. type: 'areachart-' + nodeType,
  8451. align: 'left',
  8452. width: 1,
  8453. height: 1
  8454. },
  8455. Edge: {
  8456. type: 'none'
  8457. },
  8458. Tips: {
  8459. enable: config.Tips.enable,
  8460. type: 'Native',
  8461. force: true,
  8462. onShow: function(tip, node, contains) {
  8463. var elem = contains;
  8464. config.Tips.onShow(tip, elem, node);
  8465. }
  8466. },
  8467. Events: {
  8468. enable: true,
  8469. type: 'Native',
  8470. onClick: function(node, eventInfo, evt) {
  8471. if(!config.filterOnClick && !config.Events.enable) return;
  8472. var elem = eventInfo.getContains();
  8473. if(elem) config.filterOnClick && that.filter(elem.name);
  8474. config.Events.enable && config.Events.onClick(elem, eventInfo, evt);
  8475. },
  8476. onRightClick: function(node, eventInfo, evt) {
  8477. if(!config.restoreOnRightClick) return;
  8478. that.restore();
  8479. },
  8480. onMouseMove: function(node, eventInfo, evt) {
  8481. if(!config.selectOnHover) return;
  8482. if(node) {
  8483. var elem = eventInfo.getContains();
  8484. that.select(node.id, elem.name, elem.index);
  8485. } else {
  8486. that.select(false, false, false);
  8487. }
  8488. }
  8489. },
  8490. onCreateLabel: function(domElement, node) {
  8491. var labelConf = config.Label,
  8492. valueArray = node.getData('valueArray'),
  8493. acumLeft = $.reduce(valueArray, function(x, y) { return x + y[0]; }, 0),
  8494. acumRight = $.reduce(valueArray, function(x, y) { return x + y[1]; }, 0);
  8495. if(node.getData('prev')) {
  8496. var nlbs = {
  8497. wrapper: document.createElement('div'),
  8498. aggregate: document.createElement('div'),
  8499. label: document.createElement('div')
  8500. };
  8501. var wrapper = nlbs.wrapper,
  8502. label = nlbs.label,
  8503. aggregate = nlbs.aggregate,
  8504. wrapperStyle = wrapper.style,
  8505. labelStyle = label.style,
  8506. aggregateStyle = aggregate.style;
  8507. //store node labels
  8508. nodeLabels[node.id] = nlbs;
  8509. //append labels
  8510. wrapper.appendChild(label);
  8511. wrapper.appendChild(aggregate);
  8512. if(!config.showLabels(node.name, acumLeft, acumRight, node)) {
  8513. label.style.display = 'none';
  8514. }
  8515. if(!config.showAggregates(node.name, acumLeft, acumRight, node)) {
  8516. aggregate.style.display = 'none';
  8517. }
  8518. wrapperStyle.position = 'relative';
  8519. wrapperStyle.overflow = 'visible';
  8520. wrapperStyle.fontSize = labelConf.size + 'px';
  8521. wrapperStyle.fontFamily = labelConf.family;
  8522. wrapperStyle.color = labelConf.color;
  8523. wrapperStyle.textAlign = 'center';
  8524. aggregateStyle.position = labelStyle.position = 'absolute';
  8525. domElement.style.width = node.getData('width') + 'px';
  8526. domElement.style.height = node.getData('height') + 'px';
  8527. label.innerHTML = node.name;
  8528. domElement.appendChild(wrapper);
  8529. }
  8530. },
  8531. onPlaceLabel: function(domElement, node) {
  8532. if(!node.getData('prev')) return;
  8533. var labels = nodeLabels[node.id],
  8534. wrapperStyle = labels.wrapper.style,
  8535. labelStyle = labels.label.style,
  8536. aggregateStyle = labels.aggregate.style,
  8537. width = node.getData('width'),
  8538. height = node.getData('height'),
  8539. dimArray = node.getData('dimArray'),
  8540. valArray = node.getData('valueArray'),
  8541. acumLeft = $.reduce(valArray, function(x, y) { return x + y[0]; }, 0),
  8542. acumRight = $.reduce(valArray, function(x, y) { return x + y[1]; }, 0),
  8543. font = parseInt(wrapperStyle.fontSize, 10),
  8544. domStyle = domElement.style;
  8545. if(dimArray && valArray) {
  8546. if(config.showLabels(node.name, acumLeft, acumRight, node)) {
  8547. labelStyle.display = '';
  8548. } else {
  8549. labelStyle.display = 'none';
  8550. }
  8551. var aggValue = config.showAggregates(node.name, acumLeft, acumRight, node);
  8552. if(aggValue !== false) {
  8553. aggregateStyle.display = '';
  8554. } else {
  8555. aggregateStyle.display = 'none';
  8556. }
  8557. wrapperStyle.width = aggregateStyle.width = labelStyle.width = domElement.style.width = width + 'px';
  8558. aggregateStyle.left = labelStyle.left = -width/2 + 'px';
  8559. for(var i=0, l=valArray.length, acum=0, leftAcum=0; i<l; i++) {
  8560. if(dimArray[i][0] > 0) {
  8561. acum+= valArray[i][0];
  8562. leftAcum+= dimArray[i][0];
  8563. }
  8564. }
  8565. aggregateStyle.top = (-font - config.labelOffset) + 'px';
  8566. labelStyle.top = (config.labelOffset + leftAcum) + 'px';
  8567. domElement.style.top = parseInt(domElement.style.top, 10) - leftAcum + 'px';
  8568. domElement.style.height = wrapperStyle.height = leftAcum + 'px';
  8569. labels.aggregate.innerHTML = aggValue !== true? aggValue : acum;
  8570. }
  8571. }
  8572. });
  8573. var size = delegate.canvas.getSize(),
  8574. margin = config.Margin;
  8575. delegate.config.offsetY = -size.height/2 + margin.bottom
  8576. + (config.showLabels && (config.labelOffset + config.Label.size));
  8577. delegate.config.offsetX = (margin.right - margin.left)/2;
  8578. this.delegate = delegate;
  8579. this.canvas = this.delegate.canvas;
  8580. },
  8581. /*
  8582. Method: loadJSON
  8583. Loads JSON data into the visualization.
  8584. Parameters:
  8585. json - The JSON data format. This format is described in <http://blog.thejit.org/2010/04/24/new-javascript-infovis-toolkit-visualizations/#json-data-format>.
  8586. Example:
  8587. (start code js)
  8588. var areaChart = new $jit.AreaChart(options);
  8589. areaChart.loadJSON(json);
  8590. (end code)
  8591. */
  8592. loadJSON: function(json) {
  8593. var prefix = $.time(),
  8594. ch = [],
  8595. delegate = this.delegate,
  8596. name = $.splat(json.label),
  8597. color = $.splat(json.color || this.colors),
  8598. config = this.config,
  8599. gradient = !!config.type.split(":")[1],
  8600. animate = config.animate;
  8601. for(var i=0, values=json.values, l=values.length; i<l-1; i++) {
  8602. var val = values[i], prev = values[i-1], next = values[i+1];
  8603. var valLeft = $.splat(values[i].values), valRight = $.splat(values[i+1].values);
  8604. var valArray = $.zip(valLeft, valRight);
  8605. var acumLeft = 0, acumRight = 0;
  8606. ch.push({
  8607. 'id': prefix + val.label,
  8608. 'name': val.label,
  8609. 'data': {
  8610. 'value': valArray,
  8611. '$valueArray': valArray,
  8612. '$colorArray': color,
  8613. '$stringArray': name,
  8614. '$next': next.label,
  8615. '$prev': prev? prev.label:false,
  8616. '$config': config,
  8617. '$gradient': gradient
  8618. },
  8619. 'children': []
  8620. });
  8621. }
  8622. var root = {
  8623. 'id': prefix + '$root',
  8624. 'name': '',
  8625. 'data': {
  8626. '$type': 'none',
  8627. '$width': 1,
  8628. '$height': 1
  8629. },
  8630. 'children': ch
  8631. };
  8632. delegate.loadJSON(root);
  8633. this.normalizeDims();
  8634. delegate.compute();
  8635. delegate.select(delegate.root);
  8636. if(animate) {
  8637. delegate.fx.animate({
  8638. modes: ['node-property:height:dimArray'],
  8639. duration:1500
  8640. });
  8641. }
  8642. },
  8643. /*
  8644. Method: updateJSON
  8645. Use this method when updating values for the current JSON data. If the items specified by the JSON data already exist in the graph then their values will be updated.
  8646. Parameters:
  8647. json - (object) JSON data to be updated. The JSON format corresponds to the one described in <AreaChart.loadJSON>.
  8648. onComplete - (object) A callback object to be called when the animation transition when updating the data end.
  8649. Example:
  8650. (start code js)
  8651. areaChart.updateJSON(json, {
  8652. onComplete: function() {
  8653. alert('update complete!');
  8654. }
  8655. });
  8656. (end code)
  8657. */
  8658. updateJSON: function(json, onComplete) {
  8659. if(this.busy) return;
  8660. this.busy = true;
  8661. var delegate = this.delegate,
  8662. graph = delegate.graph,
  8663. labels = json.label && $.splat(json.label),
  8664. values = json.values,
  8665. animate = this.config.animate,
  8666. that = this,
  8667. hashValues = {};
  8668. //convert the whole thing into a hash
  8669. for (var i = 0, l = values.length; i < l; i++) {
  8670. hashValues[values[i].label] = values[i];
  8671. }
  8672. graph.eachNode(function(n) {
  8673. var v = hashValues[n.name],
  8674. stringArray = n.getData('stringArray'),
  8675. valArray = n.getData('valueArray'),
  8676. next = n.getData('next');
  8677. if (v) {
  8678. v.values = $.splat(v.values);
  8679. $.each(valArray, function(a, i) {
  8680. a[0] = v.values[i];
  8681. if(labels) stringArray[i] = labels[i];
  8682. });
  8683. n.setData('valueArray', valArray);
  8684. }
  8685. if(next) {
  8686. v = hashValues[next];
  8687. if(v) {
  8688. $.each(valArray, function(a, i) {
  8689. a[1] = v.values[i];
  8690. });
  8691. }
  8692. }
  8693. });
  8694. this.normalizeDims();
  8695. delegate.compute();
  8696. delegate.select(delegate.root);
  8697. if(animate) {
  8698. delegate.fx.animate({
  8699. modes: ['node-property:height:dimArray'],
  8700. duration:1500,
  8701. onComplete: function() {
  8702. that.busy = false;
  8703. onComplete && onComplete.onComplete();
  8704. }
  8705. });
  8706. }
  8707. },
  8708. /*
  8709. Method: filter
  8710. Filter selected stacks, collapsing all other stacks. You can filter multiple stacks at the same time.
  8711. Parameters:
  8712. filters - (array) An array of strings with the name of the stacks to be filtered.
  8713. callback - (object) An object with an *onComplete* callback method.
  8714. Example:
  8715. (start code js)
  8716. areaChart.filter(['label A', 'label C'], {
  8717. onComplete: function() {
  8718. console.log('done!');
  8719. }
  8720. });
  8721. (end code)
  8722. See also:
  8723. <AreaChart.restore>.
  8724. */
  8725. filter: function(filters, callback) {
  8726. if(this.busy) return;
  8727. this.busy = true;
  8728. if(this.config.Tips.enable) this.delegate.tips.hide();
  8729. this.select(false, false, false);
  8730. var args = $.splat(filters);
  8731. var rt = this.delegate.graph.getNode(this.delegate.root);
  8732. var that = this;
  8733. this.normalizeDims();
  8734. rt.eachAdjacency(function(adj) {
  8735. var n = adj.nodeTo,
  8736. dimArray = n.getData('dimArray', 'end'),
  8737. stringArray = n.getData('stringArray');
  8738. n.setData('dimArray', $.map(dimArray, function(d, i) {
  8739. return ($.indexOf(args, stringArray[i]) > -1)? d:[0, 0];
  8740. }), 'end');
  8741. });
  8742. this.delegate.fx.animate({
  8743. modes: ['node-property:dimArray'],
  8744. duration:1500,
  8745. onComplete: function() {
  8746. that.busy = false;
  8747. callback && callback.onComplete();
  8748. }
  8749. });
  8750. },
  8751. /*
  8752. Method: restore
  8753. Sets all stacks that could have been filtered visible.
  8754. Example:
  8755. (start code js)
  8756. areaChart.restore();
  8757. (end code)
  8758. See also:
  8759. <AreaChart.filter>.
  8760. */
  8761. restore: function(callback) {
  8762. if(this.busy) return;
  8763. this.busy = true;
  8764. if(this.config.Tips.enable) this.delegate.tips.hide();
  8765. this.select(false, false, false);
  8766. this.normalizeDims();
  8767. var that = this;
  8768. this.delegate.fx.animate({
  8769. modes: ['node-property:height:dimArray'],
  8770. duration:1500,
  8771. onComplete: function() {
  8772. that.busy = false;
  8773. callback && callback.onComplete();
  8774. }
  8775. });
  8776. },
  8777. //adds the little brown bar when hovering the node
  8778. select: function(id, name, index) {
  8779. if(!this.config.selectOnHover) return;
  8780. var s = this.selected;
  8781. if(s.id != id || s.name != name
  8782. || s.index != index) {
  8783. s.id = id;
  8784. s.name = name;
  8785. s.index = index;
  8786. this.delegate.graph.eachNode(function(n) {
  8787. n.setData('border', false);
  8788. });
  8789. if(id) {
  8790. var n = this.delegate.graph.getNode(id);
  8791. n.setData('border', s);
  8792. var link = index === 0? 'prev':'next';
  8793. link = n.getData(link);
  8794. if(link) {
  8795. n = this.delegate.graph.getByName(link);
  8796. if(n) {
  8797. n.setData('border', {
  8798. name: name,
  8799. index: 1-index
  8800. });
  8801. }
  8802. }
  8803. }
  8804. this.delegate.plot();
  8805. }
  8806. },
  8807. /*
  8808. Method: getLegend
  8809. Returns an object containing as keys the legend names and as values hex strings with color values.
  8810. Example:
  8811. (start code js)
  8812. var legend = areaChart.getLegend();
  8813. (end code)
  8814. */
  8815. getLegend: function() {
  8816. var legend = {};
  8817. var n;
  8818. this.delegate.graph.getNode(this.delegate.root).eachAdjacency(function(adj) {
  8819. n = adj.nodeTo;
  8820. });
  8821. var colors = n.getData('colorArray'),
  8822. len = colors.length;
  8823. $.each(n.getData('stringArray'), function(s, i) {
  8824. legend[s] = colors[i % len];
  8825. });
  8826. return legend;
  8827. },
  8828. /*
  8829. Method: getMaxValue
  8830. Returns the maximum accumulated value for the stacks. This method is used for normalizing the graph heights according to the canvas height.
  8831. Example:
  8832. (start code js)
  8833. var ans = areaChart.getMaxValue();
  8834. (end code)
  8835. In some cases it could be useful to override this method to normalize heights for a group of AreaCharts, like when doing small multiples.
  8836. Example:
  8837. (start code js)
  8838. //will return 100 for all AreaChart instances,
  8839. //displaying all of them with the same scale
  8840. $jit.AreaChart.implement({
  8841. 'getMaxValue': function() {
  8842. return 100;
  8843. }
  8844. });
  8845. (end code)
  8846. */
  8847. getMaxValue: function() {
  8848. var maxValue = 0;
  8849. this.delegate.graph.eachNode(function(n) {
  8850. var valArray = n.getData('valueArray'),
  8851. acumLeft = 0, acumRight = 0;
  8852. $.each(valArray, function(v) {
  8853. acumLeft += +v[0];
  8854. acumRight += +v[1];
  8855. });
  8856. var acum = acumRight>acumLeft? acumRight:acumLeft;
  8857. maxValue = maxValue>acum? maxValue:acum;
  8858. });
  8859. return maxValue;
  8860. },
  8861. normalizeDims: function() {
  8862. //number of elements
  8863. var root = this.delegate.graph.getNode(this.delegate.root), l=0;
  8864. root.eachAdjacency(function() {
  8865. l++;
  8866. });
  8867. var maxValue = this.getMaxValue() || 1,
  8868. size = this.delegate.canvas.getSize(),
  8869. config = this.config,
  8870. margin = config.Margin,
  8871. labelOffset = config.labelOffset + config.Label.size,
  8872. fixedDim = (size.width - (margin.left + margin.right)) / l,
  8873. animate = config.animate,
  8874. height = size.height - (margin.top + margin.bottom) - (config.showAggregates && labelOffset)
  8875. - (config.showLabels && labelOffset);
  8876. this.delegate.graph.eachNode(function(n) {
  8877. var acumLeft = 0, acumRight = 0, animateValue = [];
  8878. $.each(n.getData('valueArray'), function(v) {
  8879. acumLeft += +v[0];
  8880. acumRight += +v[1];
  8881. animateValue.push([0, 0]);
  8882. });
  8883. var acum = acumRight>acumLeft? acumRight:acumLeft;
  8884. n.setData('width', fixedDim);
  8885. if(animate) {
  8886. n.setData('height', acum * height / maxValue, 'end');
  8887. n.setData('dimArray', $.map(n.getData('valueArray'), function(n) {
  8888. return [n[0] * height / maxValue, n[1] * height / maxValue];
  8889. }), 'end');
  8890. var dimArray = n.getData('dimArray');
  8891. if(!dimArray) {
  8892. n.setData('dimArray', animateValue);
  8893. }
  8894. } else {
  8895. n.setData('height', acum * height / maxValue);
  8896. n.setData('dimArray', $.map(n.getData('valueArray'), function(n) {
  8897. return [n[0] * height / maxValue, n[1] * height / maxValue];
  8898. }));
  8899. }
  8900. });
  8901. }
  8902. });
  8903. /*
  8904. * File: Options.BarChart.js
  8905. *
  8906. */
  8907. /*
  8908. Object: Options.BarChart
  8909. <BarChart> options.
  8910. Other options included in the BarChart are <Options.Canvas>, <Options.Label>, <Options.Margin>, <Options.Tips> and <Options.Events>.
  8911. Syntax:
  8912. (start code js)
  8913. Options.BarChart = {
  8914. animate: true,
  8915. labelOffset: 3,
  8916. barsOffset: 0,
  8917. type: 'stacked',
  8918. hoveredColor: '#9fd4ff',
  8919. orientation: 'horizontal',
  8920. showAggregates: true,
  8921. showLabels: true
  8922. };
  8923. (end code)
  8924. Example:
  8925. (start code js)
  8926. var barChart = new $jit.BarChart({
  8927. animate: true,
  8928. barsOffset: 10,
  8929. type: 'stacked:gradient'
  8930. });
  8931. (end code)
  8932. Parameters:
  8933. animate - (boolean) Default's *true*. Whether to add animated transitions when filtering/restoring stacks.
  8934. offset - (number) Default's *25*. Adds margin between the visualization and the canvas.
  8935. labelOffset - (number) Default's *3*. Adds margin between the label and the default place where it should be drawn.
  8936. barsOffset - (number) Default's *0*. Separation between bars.
  8937. type - (string) Default's *'stacked'*. Stack or grouped styles. Posible values are 'stacked', 'grouped', 'stacked:gradient', 'grouped:gradient' to add gradients.
  8938. hoveredColor - (boolean|string) Default's *'#9fd4ff'*. Sets the selected color for a hovered bar stack.
  8939. orientation - (string) Default's 'horizontal'. Sets the direction of the bars. Possible options are 'vertical' or 'horizontal'.
  8940. showAggregates - (boolean, function) Default's *true*. Display the sum the values of each bar. Can also be a function that returns *true* or *false* to display the value of the bar or not. That same function can also return a string with the formatted data to be added.
  8941. showLabels - (boolean, function) Default's *true*. Display the name of the slots. Can also be a function that returns *true* or *false* for each bar to decide whether to show the label or not.
  8942. */
  8943. Options.BarChart = {
  8944. $extend: true,
  8945. animate: true,
  8946. type: 'stacked', //stacked, grouped, : gradient
  8947. labelOffset: 3, //label offset
  8948. barsOffset: 0, //distance between bars
  8949. hoveredColor: '#9fd4ff',
  8950. orientation: 'horizontal',
  8951. showAggregates: true,
  8952. showLabels: true,
  8953. Tips: {
  8954. enable: false,
  8955. onShow: $.empty,
  8956. onHide: $.empty
  8957. },
  8958. Events: {
  8959. enable: false,
  8960. onClick: $.empty
  8961. }
  8962. };
  8963. /*
  8964. * File: BarChart.js
  8965. *
  8966. */
  8967. $jit.ST.Plot.NodeTypes.implement({
  8968. 'barchart-stacked' : {
  8969. 'render' : function(node, canvas) {
  8970. var pos = node.pos.getc(true),
  8971. width = node.getData('width'),
  8972. height = node.getData('height'),
  8973. algnPos = this.getAlignedPos(pos, width, height),
  8974. x = algnPos.x, y = algnPos.y,
  8975. dimArray = node.getData('dimArray'),
  8976. valueArray = node.getData('valueArray'),
  8977. colorArray = node.getData('colorArray'),
  8978. colorLength = colorArray.length,
  8979. stringArray = node.getData('stringArray');
  8980. var ctx = canvas.getCtx(),
  8981. opt = {},
  8982. border = node.getData('border'),
  8983. gradient = node.getData('gradient'),
  8984. config = node.getData('config'),
  8985. horz = config.orientation == 'horizontal',
  8986. aggregates = config.showAggregates,
  8987. showLabels = config.showLabels,
  8988. label = config.Label;
  8989. if (colorArray && dimArray && stringArray) {
  8990. for (var i=0, l=dimArray.length, acum=0, valAcum=0; i<l; i++) {
  8991. ctx.fillStyle = ctx.strokeStyle = colorArray[i % colorLength];
  8992. if(gradient) {
  8993. var linear;
  8994. if(horz) {
  8995. linear = ctx.createLinearGradient(x + acum + dimArray[i]/2, y,
  8996. x + acum + dimArray[i]/2, y + height);
  8997. } else {
  8998. linear = ctx.createLinearGradient(x, y - acum - dimArray[i]/2,
  8999. x + width, y - acum- dimArray[i]/2);
  9000. }
  9001. var color = $.rgbToHex($.map($.hexToRgb(colorArray[i % colorLength].slice(1)),
  9002. function(v) { return (v * 0.5) >> 0; }));
  9003. linear.addColorStop(0, color);
  9004. linear.addColorStop(0.5, colorArray[i % colorLength]);
  9005. linear.addColorStop(1, color);
  9006. ctx.fillStyle = linear;
  9007. }
  9008. if(horz) {
  9009. ctx.fillRect(x + acum, y, dimArray[i], height);
  9010. } else {
  9011. ctx.fillRect(x, y - acum - dimArray[i], width, dimArray[i]);
  9012. }
  9013. if(border && border.name == stringArray[i]) {
  9014. opt.acum = acum;
  9015. opt.dimValue = dimArray[i];
  9016. }
  9017. acum += (dimArray[i] || 0);
  9018. valAcum += (valueArray[i] || 0);
  9019. }
  9020. if(border) {
  9021. ctx.save();
  9022. ctx.lineWidth = 2;
  9023. ctx.strokeStyle = border.color;
  9024. if(horz) {
  9025. ctx.strokeRect(x + opt.acum + 1, y + 1, opt.dimValue -2, height - 2);
  9026. } else {
  9027. ctx.strokeRect(x + 1, y - opt.acum - opt.dimValue + 1, width -2, opt.dimValue -2);
  9028. }
  9029. ctx.restore();
  9030. }
  9031. if(label.type == 'Native') {
  9032. ctx.save();
  9033. ctx.fillStyle = ctx.strokeStyle = label.color;
  9034. ctx.font = label.style + ' ' + label.size + 'px ' + label.family;
  9035. ctx.textBaseline = 'middle';
  9036. var aggValue = aggregates(node.name, valAcum, node);
  9037. if(aggValue !== false) {
  9038. aggValue = aggValue !== true? aggValue : valAcum;
  9039. if(horz) {
  9040. ctx.textAlign = 'right';
  9041. ctx.fillText(aggValue, x + acum - config.labelOffset, y + height/2);
  9042. } else {
  9043. ctx.textAlign = 'center';
  9044. ctx.fillText(aggValue, x + width/2, y - height - label.size/2 - config.labelOffset);
  9045. }
  9046. }
  9047. if(showLabels(node.name, valAcum, node)) {
  9048. if(horz) {
  9049. ctx.textAlign = 'center';
  9050. ctx.translate(x - config.labelOffset - label.size/2, y + height/2);
  9051. ctx.rotate(Math.PI / 2);
  9052. ctx.fillText(node.name, 0, 0);
  9053. } else {
  9054. ctx.textAlign = 'center';
  9055. ctx.fillText(node.name, x + width/2, y + label.size/2 + config.labelOffset);
  9056. }
  9057. }
  9058. ctx.restore();
  9059. }
  9060. }
  9061. },
  9062. 'contains': function(node, mpos) {
  9063. var pos = node.pos.getc(true),
  9064. width = node.getData('width'),
  9065. height = node.getData('height'),
  9066. algnPos = this.getAlignedPos(pos, width, height),
  9067. x = algnPos.x, y = algnPos.y,
  9068. dimArray = node.getData('dimArray'),
  9069. config = node.getData('config'),
  9070. rx = mpos.x - x,
  9071. horz = config.orientation == 'horizontal';
  9072. //bounding box check
  9073. if(horz) {
  9074. if(mpos.x < x || mpos.x > x + width
  9075. || mpos.y > y + height || mpos.y < y) {
  9076. return false;
  9077. }
  9078. } else {
  9079. if(mpos.x < x || mpos.x > x + width
  9080. || mpos.y > y || mpos.y < y - height) {
  9081. return false;
  9082. }
  9083. }
  9084. //deep check
  9085. for(var i=0, l=dimArray.length, acum=(horz? x:y); i<l; i++) {
  9086. var dimi = dimArray[i];
  9087. if(horz) {
  9088. acum += dimi;
  9089. var intersec = acum;
  9090. if(mpos.x <= intersec) {
  9091. return {
  9092. 'name': node.getData('stringArray')[i],
  9093. 'color': node.getData('colorArray')[i],
  9094. 'value': node.getData('valueArray')[i],
  9095. 'label': node.name
  9096. };
  9097. }
  9098. } else {
  9099. acum -= dimi;
  9100. var intersec = acum;
  9101. if(mpos.y >= intersec) {
  9102. return {
  9103. 'name': node.getData('stringArray')[i],
  9104. 'color': node.getData('colorArray')[i],
  9105. 'value': node.getData('valueArray')[i],
  9106. 'label': node.name
  9107. };
  9108. }
  9109. }
  9110. }
  9111. return false;
  9112. }
  9113. },
  9114. 'barchart-grouped' : {
  9115. 'render' : function(node, canvas) {
  9116. var pos = node.pos.getc(true),
  9117. width = node.getData('width'),
  9118. height = node.getData('height'),
  9119. algnPos = this.getAlignedPos(pos, width, height),
  9120. x = algnPos.x, y = algnPos.y,
  9121. dimArray = node.getData('dimArray'),
  9122. valueArray = node.getData('valueArray'),
  9123. valueLength = valueArray.length,
  9124. colorArray = node.getData('colorArray'),
  9125. colorLength = colorArray.length,
  9126. stringArray = node.getData('stringArray');
  9127. var ctx = canvas.getCtx(),
  9128. opt = {},
  9129. border = node.getData('border'),
  9130. gradient = node.getData('gradient'),
  9131. config = node.getData('config'),
  9132. horz = config.orientation == 'horizontal',
  9133. aggregates = config.showAggregates,
  9134. showLabels = config.showLabels,
  9135. label = config.Label,
  9136. fixedDim = (horz? height : width) / valueLength;
  9137. if (colorArray && dimArray && stringArray) {
  9138. for (var i=0, l=valueLength, acum=0, valAcum=0; i<l; i++) {
  9139. ctx.fillStyle = ctx.strokeStyle = colorArray[i % colorLength];
  9140. if(gradient) {
  9141. var linear;
  9142. if(horz) {
  9143. linear = ctx.createLinearGradient(x + dimArray[i]/2, y + fixedDim * i,
  9144. x + dimArray[i]/2, y + fixedDim * (i + 1));
  9145. } else {
  9146. linear = ctx.createLinearGradient(x + fixedDim * i, y - dimArray[i]/2,
  9147. x + fixedDim * (i + 1), y - dimArray[i]/2);
  9148. }
  9149. var color = $.rgbToHex($.map($.hexToRgb(colorArray[i % colorLength].slice(1)),
  9150. function(v) { return (v * 0.5) >> 0; }));
  9151. linear.addColorStop(0, color);
  9152. linear.addColorStop(0.5, colorArray[i % colorLength]);
  9153. linear.addColorStop(1, color);
  9154. ctx.fillStyle = linear;
  9155. }
  9156. if(horz) {
  9157. ctx.fillRect(x, y + fixedDim * i, dimArray[i], fixedDim);
  9158. } else {
  9159. ctx.fillRect(x + fixedDim * i, y - dimArray[i], fixedDim, dimArray[i]);
  9160. }
  9161. if(border && border.name == stringArray[i]) {
  9162. opt.acum = fixedDim * i;
  9163. opt.dimValue = dimArray[i];
  9164. }
  9165. acum += (dimArray[i] || 0);
  9166. valAcum += (valueArray[i] || 0);
  9167. }
  9168. if(border) {
  9169. ctx.save();
  9170. ctx.lineWidth = 2;
  9171. ctx.strokeStyle = border.color;
  9172. if(horz) {
  9173. ctx.strokeRect(x + 1, y + opt.acum + 1, opt.dimValue -2, fixedDim - 2);
  9174. } else {
  9175. ctx.strokeRect(x + opt.acum + 1, y - opt.dimValue + 1, fixedDim -2, opt.dimValue -2);
  9176. }
  9177. ctx.restore();
  9178. }
  9179. if(label.type == 'Native') {
  9180. ctx.save();
  9181. ctx.fillStyle = ctx.strokeStyle = label.color;
  9182. ctx.font = label.style + ' ' + label.size + 'px ' + label.family;
  9183. ctx.textBaseline = 'middle';
  9184. var aggValue = aggregates(node.name, valAcum, node);
  9185. if(aggValue !== false) {
  9186. aggValue = aggValue !== true? aggValue : valAcum;
  9187. if(horz) {
  9188. ctx.textAlign = 'right';
  9189. ctx.fillText(aggValue, x + Math.max.apply(null, dimArray) - config.labelOffset, y + height/2);
  9190. } else {
  9191. ctx.textAlign = 'center';
  9192. ctx.fillText(aggValue, x + width/2, y - Math.max.apply(null, dimArray) - label.size/2 - config.labelOffset);
  9193. }
  9194. }
  9195. if(showLabels(node.name, valAcum, node)) {
  9196. if(horz) {
  9197. ctx.textAlign = 'center';
  9198. ctx.translate(x - config.labelOffset - label.size/2, y + height/2);
  9199. ctx.rotate(Math.PI / 2);
  9200. ctx.fillText(node.name, 0, 0);
  9201. } else {
  9202. ctx.textAlign = 'center';
  9203. ctx.fillText(node.name, x + width/2, y + label.size/2 + config.labelOffset);
  9204. }
  9205. }
  9206. ctx.restore();
  9207. }
  9208. }
  9209. },
  9210. 'contains': function(node, mpos) {
  9211. var pos = node.pos.getc(true),
  9212. width = node.getData('width'),
  9213. height = node.getData('height'),
  9214. algnPos = this.getAlignedPos(pos, width, height),
  9215. x = algnPos.x, y = algnPos.y,
  9216. dimArray = node.getData('dimArray'),
  9217. len = dimArray.length,
  9218. config = node.getData('config'),
  9219. rx = mpos.x - x,
  9220. horz = config.orientation == 'horizontal',
  9221. fixedDim = (horz? height : width) / len;
  9222. //bounding box check
  9223. if(horz) {
  9224. if(mpos.x < x || mpos.x > x + width
  9225. || mpos.y > y + height || mpos.y < y) {
  9226. return false;
  9227. }
  9228. } else {
  9229. if(mpos.x < x || mpos.x > x + width
  9230. || mpos.y > y || mpos.y < y - height) {
  9231. return false;
  9232. }
  9233. }
  9234. //deep check
  9235. for(var i=0, l=dimArray.length; i<l; i++) {
  9236. var dimi = dimArray[i];
  9237. if(horz) {
  9238. var limit = y + fixedDim * i;
  9239. if(mpos.x <= x+ dimi && mpos.y >= limit && mpos.y <= limit + fixedDim) {
  9240. return {
  9241. 'name': node.getData('stringArray')[i],
  9242. 'color': node.getData('colorArray')[i],
  9243. 'value': node.getData('valueArray')[i],
  9244. 'label': node.name
  9245. };
  9246. }
  9247. } else {
  9248. var limit = x + fixedDim * i;
  9249. if(mpos.x >= limit && mpos.x <= limit + fixedDim && mpos.y >= y - dimi) {
  9250. return {
  9251. 'name': node.getData('stringArray')[i],
  9252. 'color': node.getData('colorArray')[i],
  9253. 'value': node.getData('valueArray')[i],
  9254. 'label': node.name
  9255. };
  9256. }
  9257. }
  9258. }
  9259. return false;
  9260. }
  9261. }
  9262. });
  9263. /*
  9264. Class: BarChart
  9265. A visualization that displays stacked bar charts.
  9266. Constructor Options:
  9267. See <Options.BarChart>.
  9268. */
  9269. $jit.BarChart = new Class({
  9270. st: null,
  9271. colors: ["#416D9C", "#70A35E", "#EBB056", "#C74243", "#83548B", "#909291", "#557EAA"],
  9272. selected: {},
  9273. busy: false,
  9274. initialize: function(opt) {
  9275. this.controller = this.config =
  9276. $.merge(Options("Canvas", "Margin", "Label", "BarChart"), {
  9277. Label: { type: 'Native' }
  9278. }, opt);
  9279. //set functions for showLabels and showAggregates
  9280. var showLabels = this.config.showLabels,
  9281. typeLabels = $.type(showLabels),
  9282. showAggregates = this.config.showAggregates,
  9283. typeAggregates = $.type(showAggregates);
  9284. this.config.showLabels = typeLabels == 'function'? showLabels : $.lambda(showLabels);
  9285. this.config.showAggregates = typeAggregates == 'function'? showAggregates : $.lambda(showAggregates);
  9286. this.initializeViz();
  9287. },
  9288. initializeViz: function() {
  9289. var config = this.config, that = this;
  9290. var nodeType = config.type.split(":")[0],
  9291. horz = config.orientation == 'horizontal',
  9292. nodeLabels = {};
  9293. var delegate = new $jit.ST({
  9294. injectInto: config.injectInto,
  9295. width: config.width,
  9296. height: config.height,
  9297. orientation: horz? 'left' : 'bottom',
  9298. levelDistance: 0,
  9299. siblingOffset: config.barsOffset,
  9300. subtreeOffset: 0,
  9301. withLabels: config.Label.type != 'Native',
  9302. useCanvas: config.useCanvas,
  9303. Label: {
  9304. type: config.Label.type
  9305. },
  9306. Node: {
  9307. overridable: true,
  9308. type: 'barchart-' + nodeType,
  9309. align: 'left',
  9310. width: 1,
  9311. height: 1
  9312. },
  9313. Edge: {
  9314. type: 'none'
  9315. },
  9316. Tips: {
  9317. enable: config.Tips.enable,
  9318. type: 'Native',
  9319. force: true,
  9320. onShow: function(tip, node, contains) {
  9321. var elem = contains;
  9322. config.Tips.onShow(tip, elem, node);
  9323. }
  9324. },
  9325. Events: {
  9326. enable: true,
  9327. type: 'Native',
  9328. onClick: function(node, eventInfo, evt) {
  9329. if(!config.Events.enable) return;
  9330. var elem = eventInfo.getContains();
  9331. config.Events.onClick(elem, eventInfo, evt);
  9332. },
  9333. onMouseMove: function(node, eventInfo, evt) {
  9334. if(!config.hoveredColor) return;
  9335. if(node) {
  9336. var elem = eventInfo.getContains();
  9337. that.select(node.id, elem.name, elem.index);
  9338. } else {
  9339. that.select(false, false, false);
  9340. }
  9341. }
  9342. },
  9343. onCreateLabel: function(domElement, node) {
  9344. var labelConf = config.Label,
  9345. valueArray = node.getData('valueArray'),
  9346. acum = $.reduce(valueArray, function(x, y) { return x + y; }, 0);
  9347. var nlbs = {
  9348. wrapper: document.createElement('div'),
  9349. aggregate: document.createElement('div'),
  9350. label: document.createElement('div')
  9351. };
  9352. var wrapper = nlbs.wrapper,
  9353. label = nlbs.label,
  9354. aggregate = nlbs.aggregate,
  9355. wrapperStyle = wrapper.style,
  9356. labelStyle = label.style,
  9357. aggregateStyle = aggregate.style;
  9358. //store node labels
  9359. nodeLabels[node.id] = nlbs;
  9360. //append labels
  9361. wrapper.appendChild(label);
  9362. wrapper.appendChild(aggregate);
  9363. if(!config.showLabels(node.name, acum, node)) {
  9364. labelStyle.display = 'none';
  9365. }
  9366. if(!config.showAggregates(node.name, acum, node)) {
  9367. aggregateStyle.display = 'none';
  9368. }
  9369. wrapperStyle.position = 'relative';
  9370. wrapperStyle.overflow = 'visible';
  9371. wrapperStyle.fontSize = labelConf.size + 'px';
  9372. wrapperStyle.fontFamily = labelConf.family;
  9373. wrapperStyle.color = labelConf.color;
  9374. wrapperStyle.textAlign = 'center';
  9375. aggregateStyle.position = labelStyle.position = 'absolute';
  9376. domElement.style.width = node.getData('width') + 'px';
  9377. domElement.style.height = node.getData('height') + 'px';
  9378. aggregateStyle.left = labelStyle.left = '0px';
  9379. label.innerHTML = node.name;
  9380. domElement.appendChild(wrapper);
  9381. },
  9382. onPlaceLabel: function(domElement, node) {
  9383. if(!nodeLabels[node.id]) return;
  9384. var labels = nodeLabels[node.id],
  9385. wrapperStyle = labels.wrapper.style,
  9386. labelStyle = labels.label.style,
  9387. aggregateStyle = labels.aggregate.style,
  9388. grouped = config.type.split(':')[0] == 'grouped',
  9389. horz = config.orientation == 'horizontal',
  9390. dimArray = node.getData('dimArray'),
  9391. valArray = node.getData('valueArray'),
  9392. width = (grouped && horz)? Math.max.apply(null, dimArray) : node.getData('width'),
  9393. height = (grouped && !horz)? Math.max.apply(null, dimArray) : node.getData('height'),
  9394. font = parseInt(wrapperStyle.fontSize, 10),
  9395. domStyle = domElement.style;
  9396. if(dimArray && valArray) {
  9397. wrapperStyle.width = aggregateStyle.width = labelStyle.width = domElement.style.width = width + 'px';
  9398. for(var i=0, l=valArray.length, acum=0; i<l; i++) {
  9399. if(dimArray[i] > 0) {
  9400. acum+= valArray[i];
  9401. }
  9402. }
  9403. if(config.showLabels(node.name, acum, node)) {
  9404. labelStyle.display = '';
  9405. } else {
  9406. labelStyle.display = 'none';
  9407. }
  9408. var aggValue = config.showAggregates(node.name, acum, node);
  9409. if(aggValue !== false) {
  9410. aggregateStyle.display = '';
  9411. } else {
  9412. aggregateStyle.display = 'none';
  9413. }
  9414. if(config.orientation == 'horizontal') {
  9415. aggregateStyle.textAlign = 'right';
  9416. labelStyle.textAlign = 'left';
  9417. labelStyle.textIndex = aggregateStyle.textIndent = config.labelOffset + 'px';
  9418. aggregateStyle.top = labelStyle.top = (height-font)/2 + 'px';
  9419. domElement.style.height = wrapperStyle.height = height + 'px';
  9420. } else {
  9421. aggregateStyle.top = (-font - config.labelOffset) + 'px';
  9422. labelStyle.top = (config.labelOffset + height) + 'px';
  9423. domElement.style.top = parseInt(domElement.style.top, 10) - height + 'px';
  9424. domElement.style.height = wrapperStyle.height = height + 'px';
  9425. }
  9426. labels.aggregate.innerHTML = aggValue !== true? aggValue : acum;
  9427. }
  9428. }
  9429. });
  9430. var size = delegate.canvas.getSize(),
  9431. margin = config.Margin;
  9432. if(horz) {
  9433. delegate.config.offsetX = size.width/2 - margin.left
  9434. - (config.showLabels && (config.labelOffset + config.Label.size));
  9435. delegate.config.offsetY = (margin.bottom - margin.top)/2;
  9436. } else {
  9437. delegate.config.offsetY = -size.height/2 + margin.bottom
  9438. + (config.showLabels && (config.labelOffset + config.Label.size));
  9439. delegate.config.offsetX = (margin.right - margin.left)/2;
  9440. }
  9441. this.delegate = delegate;
  9442. this.canvas = this.delegate.canvas;
  9443. },
  9444. /*
  9445. Method: loadJSON
  9446. Loads JSON data into the visualization.
  9447. Parameters:
  9448. json - The JSON data format. This format is described in <http://blog.thejit.org/2010/04/24/new-javascript-infovis-toolkit-visualizations/#json-data-format>.
  9449. Example:
  9450. (start code js)
  9451. var barChart = new $jit.BarChart(options);
  9452. barChart.loadJSON(json);
  9453. (end code)
  9454. */
  9455. loadJSON: function(json) {
  9456. if(this.busy) return;
  9457. this.busy = true;
  9458. var prefix = $.time(),
  9459. ch = [],
  9460. delegate = this.delegate,
  9461. name = $.splat(json.label),
  9462. color = $.splat(json.color || this.colors),
  9463. config = this.config,
  9464. gradient = !!config.type.split(":")[1],
  9465. animate = config.animate,
  9466. horz = config.orientation == 'horizontal',
  9467. that = this;
  9468. for(var i=0, values=json.values, l=values.length; i<l; i++) {
  9469. var val = values[i]
  9470. var valArray = $.splat(values[i].values);
  9471. var acum = 0;
  9472. ch.push({
  9473. 'id': prefix + val.label,
  9474. 'name': val.label,
  9475. 'data': {
  9476. 'value': valArray,
  9477. '$valueArray': valArray,
  9478. '$colorArray': color,
  9479. '$stringArray': name,
  9480. '$gradient': gradient,
  9481. '$config': config
  9482. },
  9483. 'children': []
  9484. });
  9485. }
  9486. var root = {
  9487. 'id': prefix + '$root',
  9488. 'name': '',
  9489. 'data': {
  9490. '$type': 'none',
  9491. '$width': 1,
  9492. '$height': 1
  9493. },
  9494. 'children': ch
  9495. };
  9496. delegate.loadJSON(root);
  9497. this.normalizeDims();
  9498. delegate.compute();
  9499. delegate.select(delegate.root);
  9500. if(animate) {
  9501. if(horz) {
  9502. delegate.fx.animate({
  9503. modes: ['node-property:width:dimArray'],
  9504. duration:1500,
  9505. onComplete: function() {
  9506. that.busy = false;
  9507. }
  9508. });
  9509. } else {
  9510. delegate.fx.animate({
  9511. modes: ['node-property:height:dimArray'],
  9512. duration:1500,
  9513. onComplete: function() {
  9514. that.busy = false;
  9515. }
  9516. });
  9517. }
  9518. } else {
  9519. this.busy = false;
  9520. }
  9521. },
  9522. /*
  9523. Method: updateJSON
  9524. Use this method when updating values for the current JSON data. If the items specified by the JSON data already exist in the graph then their values will be updated.
  9525. Parameters:
  9526. json - (object) JSON data to be updated. The JSON format corresponds to the one described in <BarChart.loadJSON>.
  9527. onComplete - (object) A callback object to be called when the animation transition when updating the data end.
  9528. Example:
  9529. (start code js)
  9530. barChart.updateJSON(json, {
  9531. onComplete: function() {
  9532. alert('update complete!');
  9533. }
  9534. });
  9535. (end code)
  9536. */
  9537. updateJSON: function(json, onComplete) {
  9538. if(this.busy) return;
  9539. this.busy = true;
  9540. this.select(false, false, false);
  9541. var delegate = this.delegate;
  9542. var graph = delegate.graph;
  9543. var values = json.values;
  9544. var animate = this.config.animate;
  9545. var that = this;
  9546. var horz = this.config.orientation == 'horizontal';
  9547. $.each(values, function(v) {
  9548. var n = graph.getByName(v.label);
  9549. if(n) {
  9550. n.setData('valueArray', $.splat(v.values));
  9551. if(json.label) {
  9552. n.setData('stringArray', $.splat(json.label));
  9553. }
  9554. }
  9555. });
  9556. this.normalizeDims();
  9557. delegate.compute();
  9558. delegate.select(delegate.root);
  9559. if(animate) {
  9560. if(horz) {
  9561. delegate.fx.animate({
  9562. modes: ['node-property:width:dimArray'],
  9563. duration:1500,
  9564. onComplete: function() {
  9565. that.busy = false;
  9566. onComplete && onComplete.onComplete();
  9567. }
  9568. });
  9569. } else {
  9570. delegate.fx.animate({
  9571. modes: ['node-property:height:dimArray'],
  9572. duration:1500,
  9573. onComplete: function() {
  9574. that.busy = false;
  9575. onComplete && onComplete.onComplete();
  9576. }
  9577. });
  9578. }
  9579. }
  9580. },
  9581. //adds the little brown bar when hovering the node
  9582. select: function(id, name) {
  9583. if(!this.config.hoveredColor) return;
  9584. var s = this.selected;
  9585. if(s.id != id || s.name != name) {
  9586. s.id = id;
  9587. s.name = name;
  9588. s.color = this.config.hoveredColor;
  9589. this.delegate.graph.eachNode(function(n) {
  9590. if(id == n.id) {
  9591. n.setData('border', s);
  9592. } else {
  9593. n.setData('border', false);
  9594. }
  9595. });
  9596. this.delegate.plot();
  9597. }
  9598. },
  9599. /*
  9600. Method: getLegend
  9601. Returns an object containing as keys the legend names and as values hex strings with color values.
  9602. Example:
  9603. (start code js)
  9604. var legend = barChart.getLegend();
  9605. (end code)
  9606. */
  9607. getLegend: function() {
  9608. var legend = {};
  9609. var n;
  9610. this.delegate.graph.getNode(this.delegate.root).eachAdjacency(function(adj) {
  9611. n = adj.nodeTo;
  9612. });
  9613. var colors = n.getData('colorArray'),
  9614. len = colors.length;
  9615. $.each(n.getData('stringArray'), function(s, i) {
  9616. legend[s] = colors[i % len];
  9617. });
  9618. return legend;
  9619. },
  9620. /*
  9621. Method: getMaxValue
  9622. Returns the maximum accumulated value for the stacks. This method is used for normalizing the graph heights according to the canvas height.
  9623. Example:
  9624. (start code js)
  9625. var ans = barChart.getMaxValue();
  9626. (end code)
  9627. In some cases it could be useful to override this method to normalize heights for a group of BarCharts, like when doing small multiples.
  9628. Example:
  9629. (start code js)
  9630. //will return 100 for all BarChart instances,
  9631. //displaying all of them with the same scale
  9632. $jit.BarChart.implement({
  9633. 'getMaxValue': function() {
  9634. return 100;
  9635. }
  9636. });
  9637. (end code)
  9638. */
  9639. getMaxValue: function() {
  9640. var maxValue = 0, stacked = this.config.type.split(':')[0] == 'stacked';
  9641. this.delegate.graph.eachNode(function(n) {
  9642. var valArray = n.getData('valueArray'),
  9643. acum = 0;
  9644. if(!valArray) return;
  9645. if(stacked) {
  9646. $.each(valArray, function(v) {
  9647. acum += +v;
  9648. });
  9649. } else {
  9650. acum = Math.max.apply(null, valArray);
  9651. }
  9652. maxValue = maxValue>acum? maxValue:acum;
  9653. });
  9654. return maxValue;
  9655. },
  9656. setBarType: function(type) {
  9657. this.config.type = type;
  9658. this.delegate.config.Node.type = 'barchart-' + type.split(':')[0];
  9659. },
  9660. normalizeDims: function() {
  9661. //number of elements
  9662. var root = this.delegate.graph.getNode(this.delegate.root), l=0;
  9663. root.eachAdjacency(function() {
  9664. l++;
  9665. });
  9666. var maxValue = this.getMaxValue() || 1,
  9667. size = this.delegate.canvas.getSize(),
  9668. config = this.config,
  9669. margin = config.Margin,
  9670. marginWidth = margin.left + margin.right,
  9671. marginHeight = margin.top + margin.bottom,
  9672. horz = config.orientation == 'horizontal',
  9673. fixedDim = (size[horz? 'height':'width'] - (horz? marginHeight:marginWidth) - (l -1) * config.barsOffset) / l,
  9674. animate = config.animate,
  9675. height = size[horz? 'width':'height'] - (horz? marginWidth:marginHeight)
  9676. - (!horz && config.showAggregates && (config.Label.size + config.labelOffset))
  9677. - (config.showLabels && (config.Label.size + config.labelOffset)),
  9678. dim1 = horz? 'height':'width',
  9679. dim2 = horz? 'width':'height';
  9680. this.delegate.graph.eachNode(function(n) {
  9681. var acum = 0, animateValue = [];
  9682. $.each(n.getData('valueArray'), function(v) {
  9683. acum += +v;
  9684. animateValue.push(0);
  9685. });
  9686. n.setData(dim1, fixedDim);
  9687. if(animate) {
  9688. n.setData(dim2, acum * height / maxValue, 'end');
  9689. n.setData('dimArray', $.map(n.getData('valueArray'), function(n) {
  9690. return n * height / maxValue;
  9691. }), 'end');
  9692. var dimArray = n.getData('dimArray');
  9693. if(!dimArray) {
  9694. n.setData('dimArray', animateValue);
  9695. }
  9696. } else {
  9697. n.setData(dim2, acum * height / maxValue);
  9698. n.setData('dimArray', $.map(n.getData('valueArray'), function(n) {
  9699. return n * height / maxValue;
  9700. }));
  9701. }
  9702. });
  9703. }
  9704. });
  9705. /*
  9706. * File: Options.PieChart.js
  9707. *
  9708. */
  9709. /*
  9710. Object: Options.PieChart
  9711. <PieChart> options.
  9712. Other options included in the PieChart are <Options.Canvas>, <Options.Label>, <Options.Tips> and <Options.Events>.
  9713. Syntax:
  9714. (start code js)
  9715. Options.PieChart = {
  9716. animate: true,
  9717. offset: 25,
  9718. sliceOffset:0,
  9719. labelOffset: 3,
  9720. type: 'stacked',
  9721. hoveredColor: '#9fd4ff',
  9722. showLabels: true,
  9723. resizeLabels: false,
  9724. updateHeights: false
  9725. };
  9726. (end code)
  9727. Example:
  9728. (start code js)
  9729. var pie = new $jit.PieChart({
  9730. animate: true,
  9731. sliceOffset: 5,
  9732. type: 'stacked:gradient'
  9733. });
  9734. (end code)
  9735. Parameters:
  9736. animate - (boolean) Default's *true*. Whether to add animated transitions when plotting/updating the visualization.
  9737. offset - (number) Default's *25*. Adds margin between the visualization and the canvas.
  9738. sliceOffset - (number) Default's *0*. Separation between the center of the canvas and each pie slice.
  9739. labelOffset - (number) Default's *3*. Adds margin between the label and the default place where it should be drawn.
  9740. type - (string) Default's *'stacked'*. Stack style. Posible values are 'stacked', 'stacked:gradient' to add gradients.
  9741. hoveredColor - (boolean|string) Default's *'#9fd4ff'*. Sets the selected color for a hovered pie stack.
  9742. showLabels - (boolean) Default's *true*. Display the name of the slots.
  9743. resizeLabels - (boolean|number) Default's *false*. Resize the pie labels according to their stacked values. Set a number for *resizeLabels* to set a font size minimum.
  9744. updateHeights - (boolean) Default's *false*. Only for mono-valued (most common) pie charts. Resize the height of the pie slices according to their current values.
  9745. */
  9746. Options.PieChart = {
  9747. $extend: true,
  9748. animate: true,
  9749. offset: 25, // page offset
  9750. sliceOffset:0,
  9751. labelOffset: 3, // label offset
  9752. type: 'stacked', // gradient
  9753. hoveredColor: '#9fd4ff',
  9754. Events: {
  9755. enable: false,
  9756. onClick: $.empty
  9757. },
  9758. Tips: {
  9759. enable: false,
  9760. onShow: $.empty,
  9761. onHide: $.empty
  9762. },
  9763. showLabels: true,
  9764. resizeLabels: false,
  9765. //only valid for mono-valued datasets
  9766. updateHeights: false
  9767. };
  9768. /*
  9769. * Class: Layouts.Radial
  9770. *
  9771. * Implements a Radial Layout.
  9772. *
  9773. * Implemented By:
  9774. *
  9775. * <RGraph>, <Hypertree>
  9776. *
  9777. */
  9778. Layouts.Radial = new Class({
  9779. /*
  9780. * Method: compute
  9781. *
  9782. * Computes nodes' positions.
  9783. *
  9784. * Parameters:
  9785. *
  9786. * property - _optional_ A <Graph.Node> position property to store the new
  9787. * positions. Possible values are 'pos', 'end' or 'start'.
  9788. *
  9789. */
  9790. compute : function(property) {
  9791. var prop = $.splat(property || [ 'current', 'start', 'end' ]);
  9792. NodeDim.compute(this.graph, prop, this.config);
  9793. this.graph.computeLevels(this.root, 0, "ignore");
  9794. var lengthFunc = this.createLevelDistanceFunc();
  9795. this.computeAngularWidths(prop);
  9796. this.computePositions(prop, lengthFunc);
  9797. },
  9798. /*
  9799. * computePositions
  9800. *
  9801. * Performs the main algorithm for computing node positions.
  9802. */
  9803. computePositions : function(property, getLength) {
  9804. var propArray = property;
  9805. var graph = this.graph;
  9806. var root = graph.getNode(this.root);
  9807. var parent = this.parent;
  9808. var config = this.config;
  9809. for ( var i=0, l=propArray.length; i < l; i++) {
  9810. var pi = propArray[i];
  9811. root.setPos($P(0, 0), pi);
  9812. root.setData('span', Math.PI * 2, pi);
  9813. }
  9814. root.angleSpan = {
  9815. begin : 0,
  9816. end : 2 * Math.PI
  9817. };
  9818. graph.eachBFS(this.root, function(elem) {
  9819. var angleSpan = elem.angleSpan.end - elem.angleSpan.begin;
  9820. var angleInit = elem.angleSpan.begin;
  9821. var len = getLength(elem);
  9822. //Calculate the sum of all angular widths
  9823. var totalAngularWidths = 0, subnodes = [], maxDim = {};
  9824. elem.eachSubnode(function(sib) {
  9825. totalAngularWidths += sib._treeAngularWidth;
  9826. //get max dim
  9827. for ( var i=0, l=propArray.length; i < l; i++) {
  9828. var pi = propArray[i], dim = sib.getData('dim', pi);
  9829. maxDim[pi] = (pi in maxDim)? (dim > maxDim[pi]? dim : maxDim[pi]) : dim;
  9830. }
  9831. subnodes.push(sib);
  9832. }, "ignore");
  9833. //Maintain children order
  9834. //Second constraint for <http://bailando.sims.berkeley.edu/papers/infovis01.htm>
  9835. if (parent && parent.id == elem.id && subnodes.length > 0
  9836. && subnodes[0].dist) {
  9837. subnodes.sort(function(a, b) {
  9838. return (a.dist >= b.dist) - (a.dist <= b.dist);
  9839. });
  9840. }
  9841. //Calculate nodes positions.
  9842. for (var k = 0, ls=subnodes.length; k < ls; k++) {
  9843. var child = subnodes[k];
  9844. if (!child._flag) {
  9845. var angleProportion = child._treeAngularWidth / totalAngularWidths * angleSpan;
  9846. var theta = angleInit + angleProportion / 2;
  9847. for ( var i=0, l=propArray.length; i < l; i++) {
  9848. var pi = propArray[i];
  9849. child.setPos($P(theta, len), pi);
  9850. child.setData('span', angleProportion, pi);
  9851. child.setData('dim-quotient', child.getData('dim', pi) / maxDim[pi], pi);
  9852. }
  9853. child.angleSpan = {
  9854. begin : angleInit,
  9855. end : angleInit + angleProportion
  9856. };
  9857. angleInit += angleProportion;
  9858. }
  9859. }
  9860. }, "ignore");
  9861. },
  9862. /*
  9863. * Method: setAngularWidthForNodes
  9864. *
  9865. * Sets nodes angular widths.
  9866. */
  9867. setAngularWidthForNodes : function(prop) {
  9868. this.graph.eachBFS(this.root, function(elem, i) {
  9869. var diamValue = elem.getData('angularWidth', prop[0]) || 5;
  9870. elem._angularWidth = diamValue / i;
  9871. }, "ignore");
  9872. },
  9873. /*
  9874. * Method: setSubtreesAngularWidth
  9875. *
  9876. * Sets subtrees angular widths.
  9877. */
  9878. setSubtreesAngularWidth : function() {
  9879. var that = this;
  9880. this.graph.eachNode(function(elem) {
  9881. that.setSubtreeAngularWidth(elem);
  9882. }, "ignore");
  9883. },
  9884. /*
  9885. * Method: setSubtreeAngularWidth
  9886. *
  9887. * Sets the angular width for a subtree.
  9888. */
  9889. setSubtreeAngularWidth : function(elem) {
  9890. var that = this, nodeAW = elem._angularWidth, sumAW = 0;
  9891. elem.eachSubnode(function(child) {
  9892. that.setSubtreeAngularWidth(child);
  9893. sumAW += child._treeAngularWidth;
  9894. }, "ignore");
  9895. elem._treeAngularWidth = Math.max(nodeAW, sumAW);
  9896. },
  9897. /*
  9898. * Method: computeAngularWidths
  9899. *
  9900. * Computes nodes and subtrees angular widths.
  9901. */
  9902. computeAngularWidths : function(prop) {
  9903. this.setAngularWidthForNodes(prop);
  9904. this.setSubtreesAngularWidth();
  9905. }
  9906. });
  9907. /*
  9908. * File: Sunburst.js
  9909. */
  9910. /*
  9911. Class: Sunburst
  9912. A radial space filling tree visualization.
  9913. Inspired by:
  9914. Sunburst <http://www.cc.gatech.edu/gvu/ii/sunburst/>.
  9915. Note:
  9916. This visualization was built and engineered from scratch, taking only the paper as inspiration, and only shares some features with the visualization described in the paper.
  9917. Implements:
  9918. All <Loader> methods
  9919. Constructor Options:
  9920. Inherits options from
  9921. - <Options.Canvas>
  9922. - <Options.Controller>
  9923. - <Options.Node>
  9924. - <Options.Edge>
  9925. - <Options.Label>
  9926. - <Options.Events>
  9927. - <Options.Tips>
  9928. - <Options.NodeStyles>
  9929. - <Options.Navigation>
  9930. Additionally, there are other parameters and some default values changed
  9931. interpolation - (string) Default's *linear*. Describes the way nodes are interpolated. Possible values are 'linear' and 'polar'.
  9932. levelDistance - (number) Default's *100*. The distance between levels of the tree.
  9933. Node.type - Described in <Options.Node>. Default's to *multipie*.
  9934. Node.height - Described in <Options.Node>. Default's *0*.
  9935. Edge.type - Described in <Options.Edge>. Default's *none*.
  9936. Label.textAlign - Described in <Options.Label>. Default's *start*.
  9937. Label.textBaseline - Described in <Options.Label>. Default's *middle*.
  9938. Instance Properties:
  9939. canvas - Access a <Canvas> instance.
  9940. graph - Access a <Graph> instance.
  9941. op - Access a <Sunburst.Op> instance.
  9942. fx - Access a <Sunburst.Plot> instance.
  9943. labels - Access a <Sunburst.Label> interface implementation.
  9944. */
  9945. $jit.Sunburst = new Class({
  9946. Implements: [ Loader, Extras, Layouts.Radial ],
  9947. initialize: function(controller) {
  9948. var $Sunburst = $jit.Sunburst;
  9949. var config = {
  9950. interpolation: 'linear',
  9951. levelDistance: 100,
  9952. Node: {
  9953. 'type': 'multipie',
  9954. 'height':0
  9955. },
  9956. Edge: {
  9957. 'type': 'none'
  9958. },
  9959. Label: {
  9960. textAlign: 'start',
  9961. textBaseline: 'middle'
  9962. }
  9963. };
  9964. this.controller = this.config = $.merge(Options("Canvas", "Node", "Edge",
  9965. "Fx", "Tips", "NodeStyles", "Events", "Navigation", "Controller", "Label"), config, controller);
  9966. var canvasConfig = this.config;
  9967. if(canvasConfig.useCanvas) {
  9968. this.canvas = canvasConfig.useCanvas;
  9969. this.config.labelContainer = this.canvas.id + '-label';
  9970. } else {
  9971. if(canvasConfig.background) {
  9972. canvasConfig.background = $.merge({
  9973. type: 'Circles'
  9974. }, canvasConfig.background);
  9975. }
  9976. this.canvas = new Canvas(this, canvasConfig);
  9977. this.config.labelContainer = (typeof canvasConfig.injectInto == 'string'? canvasConfig.injectInto : canvasConfig.injectInto.id) + '-label';
  9978. }
  9979. this.graphOptions = {
  9980. 'klass': Polar,
  9981. 'Node': {
  9982. 'selected': false,
  9983. 'exist': true,
  9984. 'drawn': true
  9985. }
  9986. };
  9987. this.graph = new Graph(this.graphOptions, this.config.Node,
  9988. this.config.Edge);
  9989. this.labels = new $Sunburst.Label[canvasConfig.Label.type](this);
  9990. this.fx = new $Sunburst.Plot(this, $Sunburst);
  9991. this.op = new $Sunburst.Op(this);
  9992. this.json = null;
  9993. this.root = null;
  9994. this.rotated = null;
  9995. this.busy = false;
  9996. // initialize extras
  9997. this.initializeExtras();
  9998. },
  9999. /*
  10000. createLevelDistanceFunc
  10001. Returns the levelDistance function used for calculating a node distance
  10002. to its origin. This function returns a function that is computed
  10003. per level and not per node, such that all nodes with the same depth will have the
  10004. same distance to the origin. The resulting function gets the
  10005. parent node as parameter and returns a float.
  10006. */
  10007. createLevelDistanceFunc: function() {
  10008. var ld = this.config.levelDistance;
  10009. return function(elem) {
  10010. return (elem._depth + 1) * ld;
  10011. };
  10012. },
  10013. /*
  10014. Method: refresh
  10015. Computes positions and plots the tree.
  10016. */
  10017. refresh: function() {
  10018. this.compute();
  10019. this.plot();
  10020. },
  10021. /*
  10022. reposition
  10023. An alias for computing new positions to _endPos_
  10024. See also:
  10025. <Sunburst.compute>
  10026. */
  10027. reposition: function() {
  10028. this.compute('end');
  10029. },
  10030. /*
  10031. Method: rotate
  10032. Rotates the graph so that the selected node is horizontal on the right.
  10033. Parameters:
  10034. node - (object) A <Graph.Node>.
  10035. method - (string) Whether to perform an animation or just replot the graph. Possible values are "replot" or "animate".
  10036. opt - (object) Configuration options merged with this visualization configuration options.
  10037. See also:
  10038. <Sunburst.rotateAngle>
  10039. */
  10040. rotate: function(node, method, opt) {
  10041. var theta = node.getPos(opt.property || 'current').getp(true).theta;
  10042. this.rotated = node;
  10043. this.rotateAngle(-theta, method, opt);
  10044. },
  10045. /*
  10046. Method: rotateAngle
  10047. Rotates the graph of an angle theta.
  10048. Parameters:
  10049. node - (object) A <Graph.Node>.
  10050. method - (string) Whether to perform an animation or just replot the graph. Possible values are "replot" or "animate".
  10051. opt - (object) Configuration options merged with this visualization configuration options.
  10052. See also:
  10053. <Sunburst.rotate>
  10054. */
  10055. rotateAngle: function(theta, method, opt) {
  10056. var that = this;
  10057. var options = $.merge(this.config, opt || {}, {
  10058. modes: [ 'polar' ]
  10059. });
  10060. var prop = opt.property || (method === "animate" ? 'end' : 'current');
  10061. if(method === 'animate') {
  10062. this.fx.animation.pause();
  10063. }
  10064. this.graph.eachNode(function(n) {
  10065. var p = n.getPos(prop);
  10066. p.theta += theta;
  10067. if (p.theta < 0) {
  10068. p.theta += Math.PI * 2;
  10069. }
  10070. });
  10071. if (method == 'animate') {
  10072. this.fx.animate(options);
  10073. } else if (method == 'replot') {
  10074. this.fx.plot();
  10075. this.busy = false;
  10076. }
  10077. },
  10078. /*
  10079. Method: plot
  10080. Plots the Sunburst. This is a shortcut to *fx.plot*.
  10081. */
  10082. plot: function() {
  10083. this.fx.plot();
  10084. }
  10085. });
  10086. $jit.Sunburst.$extend = true;
  10087. (function(Sunburst) {
  10088. /*
  10089. Class: Sunburst.Op
  10090. Custom extension of <Graph.Op>.
  10091. Extends:
  10092. All <Graph.Op> methods
  10093. See also:
  10094. <Graph.Op>
  10095. */
  10096. Sunburst.Op = new Class( {
  10097. Implements: Graph.Op
  10098. });
  10099. /*
  10100. Class: Sunburst.Plot
  10101. Custom extension of <Graph.Plot>.
  10102. Extends:
  10103. All <Graph.Plot> methods
  10104. See also:
  10105. <Graph.Plot>
  10106. */
  10107. Sunburst.Plot = new Class( {
  10108. Implements: Graph.Plot
  10109. });
  10110. /*
  10111. Class: Sunburst.Label
  10112. Custom extension of <Graph.Label>.
  10113. Contains custom <Graph.Label.SVG>, <Graph.Label.HTML> and <Graph.Label.Native> extensions.
  10114. Extends:
  10115. All <Graph.Label> methods and subclasses.
  10116. See also:
  10117. <Graph.Label>, <Graph.Label.Native>, <Graph.Label.HTML>, <Graph.Label.SVG>.
  10118. */
  10119. Sunburst.Label = {};
  10120. /*
  10121. Sunburst.Label.Native
  10122. Custom extension of <Graph.Label.Native>.
  10123. Extends:
  10124. All <Graph.Label.Native> methods
  10125. See also:
  10126. <Graph.Label.Native>
  10127. */
  10128. Sunburst.Label.Native = new Class( {
  10129. Implements: Graph.Label.Native,
  10130. initialize: function(viz) {
  10131. this.viz = viz;
  10132. this.label = viz.config.Label;
  10133. this.config = viz.config;
  10134. },
  10135. renderLabel: function(canvas, node, controller) {
  10136. var span = node.getData('span');
  10137. if(span < Math.PI /2 && Math.tan(span) *
  10138. this.config.levelDistance * node._depth < 10) {
  10139. return;
  10140. }
  10141. var ctx = canvas.getCtx();
  10142. var measure = ctx.measureText(node.name);
  10143. if (node.id == this.viz.root) {
  10144. var x = -measure.width / 2, y = 0, thetap = 0;
  10145. var ld = 0;
  10146. } else {
  10147. var indent = 5;
  10148. var ld = controller.levelDistance - indent;
  10149. var clone = node.pos.clone();
  10150. clone.rho += indent;
  10151. var p = clone.getp(true);
  10152. var ct = clone.getc(true);
  10153. var x = ct.x, y = ct.y;
  10154. // get angle in degrees
  10155. var pi = Math.PI;
  10156. var cond = (p.theta > pi / 2 && p.theta < 3 * pi / 2);
  10157. var thetap = cond ? p.theta + pi : p.theta;
  10158. if (cond) {
  10159. x -= Math.abs(Math.cos(p.theta) * measure.width);
  10160. y += Math.sin(p.theta) * measure.width;
  10161. } else if (node.id == this.viz.root) {
  10162. x -= measure.width / 2;
  10163. }
  10164. }
  10165. ctx.save();
  10166. ctx.translate(x, y);
  10167. ctx.rotate(thetap);
  10168. ctx.fillText(node.name, 0, 0);
  10169. ctx.restore();
  10170. }
  10171. });
  10172. /*
  10173. Sunburst.Label.SVG
  10174. Custom extension of <Graph.Label.SVG>.
  10175. Extends:
  10176. All <Graph.Label.SVG> methods
  10177. See also:
  10178. <Graph.Label.SVG>
  10179. */
  10180. Sunburst.Label.SVG = new Class( {
  10181. Implements: Graph.Label.SVG,
  10182. initialize: function(viz) {
  10183. this.viz = viz;
  10184. },
  10185. /*
  10186. placeLabel
  10187. Overrides abstract method placeLabel in <Graph.Plot>.
  10188. Parameters:
  10189. tag - A DOM label element.
  10190. node - A <Graph.Node>.
  10191. controller - A configuration/controller object passed to the visualization.
  10192. */
  10193. placeLabel: function(tag, node, controller) {
  10194. var pos = node.pos.getc(true), viz = this.viz, canvas = this.viz.canvas;
  10195. var radius = canvas.getSize();
  10196. var labelPos = {
  10197. x: Math.round(pos.x + radius.width / 2),
  10198. y: Math.round(pos.y + radius.height / 2)
  10199. };
  10200. tag.setAttribute('x', labelPos.x);
  10201. tag.setAttribute('y', labelPos.y);
  10202. var bb = tag.getBBox();
  10203. if (bb) {
  10204. // center the label
  10205. var x = tag.getAttribute('x');
  10206. var y = tag.getAttribute('y');
  10207. // get polar coordinates
  10208. var p = node.pos.getp(true);
  10209. // get angle in degrees
  10210. var pi = Math.PI;
  10211. var cond = (p.theta > pi / 2 && p.theta < 3 * pi / 2);
  10212. if (cond) {
  10213. tag.setAttribute('x', x - bb.width);
  10214. tag.setAttribute('y', y - bb.height);
  10215. } else if (node.id == viz.root) {
  10216. tag.setAttribute('x', x - bb.width / 2);
  10217. }
  10218. var thetap = cond ? p.theta + pi : p.theta;
  10219. if(node._depth)
  10220. tag.setAttribute('transform', 'rotate(' + thetap * 360 / (2 * pi) + ' ' + x
  10221. + ' ' + y + ')');
  10222. }
  10223. controller.onPlaceLabel(tag, node);
  10224. }
  10225. });
  10226. /*
  10227. Sunburst.Label.HTML
  10228. Custom extension of <Graph.Label.HTML>.
  10229. Extends:
  10230. All <Graph.Label.HTML> methods.
  10231. See also:
  10232. <Graph.Label.HTML>
  10233. */
  10234. Sunburst.Label.HTML = new Class( {
  10235. Implements: Graph.Label.HTML,
  10236. initialize: function(viz) {
  10237. this.viz = viz;
  10238. },
  10239. /*
  10240. placeLabel
  10241. Overrides abstract method placeLabel in <Graph.Plot>.
  10242. Parameters:
  10243. tag - A DOM label element.
  10244. node - A <Graph.Node>.
  10245. controller - A configuration/controller object passed to the visualization.
  10246. */
  10247. placeLabel: function(tag, node, controller) {
  10248. var pos = node.pos.clone(),
  10249. canvas = this.viz.canvas,
  10250. height = node.getData('height'),
  10251. ldist = ((height || node._depth == 0)? height : this.viz.config.levelDistance) /2,
  10252. radius = canvas.getSize();
  10253. pos.rho += ldist;
  10254. pos = pos.getc(true);
  10255. var labelPos = {
  10256. x: Math.round(pos.x + radius.width / 2),
  10257. y: Math.round(pos.y + radius.height / 2)
  10258. };
  10259. var style = tag.style;
  10260. style.left = labelPos.x + 'px';
  10261. style.top = labelPos.y + 'px';
  10262. style.display = this.fitsInCanvas(labelPos, canvas) ? '' : 'none';
  10263. controller.onPlaceLabel(tag, node);
  10264. }
  10265. });
  10266. /*
  10267. Class: Sunburst.Plot.NodeTypes
  10268. This class contains a list of <Graph.Node> built-in types.
  10269. Node types implemented are 'none', 'pie', 'multipie', 'gradient-pie' and 'gradient-multipie'.
  10270. You can add your custom node types, customizing your visualization to the extreme.
  10271. Example:
  10272. (start code js)
  10273. Sunburst.Plot.NodeTypes.implement({
  10274. 'mySpecialType': {
  10275. 'render': function(node, canvas) {
  10276. //print your custom node to canvas
  10277. },
  10278. //optional
  10279. 'contains': function(node, pos) {
  10280. //return true if pos is inside the node or false otherwise
  10281. }
  10282. }
  10283. });
  10284. (end code)
  10285. */
  10286. Sunburst.Plot.NodeTypes = new Class( {
  10287. 'none': {
  10288. 'render': $.empty,
  10289. 'contains': $.lambda(false),
  10290. 'anglecontains': function(node, pos) {
  10291. var span = node.getData('span') / 2, theta = node.pos.theta;
  10292. var begin = theta - span, end = theta + span;
  10293. if (begin < 0)
  10294. begin += Math.PI * 2;
  10295. var atan = Math.atan2(pos.y, pos.x);
  10296. if (atan < 0)
  10297. atan += Math.PI * 2;
  10298. if (begin > end) {
  10299. return (atan > begin && atan <= Math.PI * 2) || atan < end;
  10300. } else {
  10301. return atan > begin && atan < end;
  10302. }
  10303. }
  10304. },
  10305. 'pie': {
  10306. 'render': function(node, canvas) {
  10307. var span = node.getData('span') / 2, theta = node.pos.theta;
  10308. var begin = theta - span, end = theta + span;
  10309. var polarNode = node.pos.getp(true);
  10310. var polar = new Polar(polarNode.rho, begin);
  10311. var p1coord = polar.getc(true);
  10312. polar.theta = end;
  10313. var p2coord = polar.getc(true);
  10314. var ctx = canvas.getCtx();
  10315. ctx.beginPath();
  10316. ctx.moveTo(0, 0);
  10317. ctx.lineTo(p1coord.x, p1coord.y);
  10318. ctx.moveTo(0, 0);
  10319. ctx.lineTo(p2coord.x, p2coord.y);
  10320. ctx.moveTo(0, 0);
  10321. ctx.arc(0, 0, polarNode.rho * node.getData('dim-quotient'), begin, end,
  10322. false);
  10323. ctx.fill();
  10324. },
  10325. 'contains': function(node, pos) {
  10326. if (this.nodeTypes['none'].anglecontains.call(this, node, pos)) {
  10327. var rho = Math.sqrt(pos.x * pos.x + pos.y * pos.y);
  10328. var ld = this.config.levelDistance, d = node._depth;
  10329. return (rho <= ld * d);
  10330. }
  10331. return false;
  10332. }
  10333. },
  10334. 'multipie': {
  10335. 'render': function(node, canvas) {
  10336. var height = node.getData('height');
  10337. var ldist = height? height : this.config.levelDistance;
  10338. var span = node.getData('span') / 2, theta = node.pos.theta;
  10339. var begin = theta - span, end = theta + span;
  10340. var polarNode = node.pos.getp(true);
  10341. var polar = new Polar(polarNode.rho, begin);
  10342. var p1coord = polar.getc(true);
  10343. polar.theta = end;
  10344. var p2coord = polar.getc(true);
  10345. polar.rho += ldist;
  10346. var p3coord = polar.getc(true);
  10347. polar.theta = begin;
  10348. var p4coord = polar.getc(true);
  10349. var ctx = canvas.getCtx();
  10350. ctx.moveTo(0, 0);
  10351. ctx.beginPath();
  10352. ctx.arc(0, 0, polarNode.rho, begin, end, false);
  10353. ctx.arc(0, 0, polarNode.rho + ldist, end, begin, true);
  10354. ctx.moveTo(p1coord.x, p1coord.y);
  10355. ctx.lineTo(p4coord.x, p4coord.y);
  10356. ctx.moveTo(p2coord.x, p2coord.y);
  10357. ctx.lineTo(p3coord.x, p3coord.y);
  10358. ctx.fill();
  10359. if (node.collapsed) {
  10360. ctx.save();
  10361. ctx.lineWidth = 2;
  10362. ctx.moveTo(0, 0);
  10363. ctx.beginPath();
  10364. ctx.arc(0, 0, polarNode.rho + ldist + 5, end - 0.01, begin + 0.01,
  10365. true);
  10366. ctx.stroke();
  10367. ctx.restore();
  10368. }
  10369. },
  10370. 'contains': function(node, pos) {
  10371. if (this.nodeTypes['none'].anglecontains.call(this, node, pos)) {
  10372. var rho = Math.sqrt(pos.x * pos.x + pos.y * pos.y);
  10373. var height = node.getData('height');
  10374. var ldist = height? height : this.config.levelDistance;
  10375. var ld = this.config.levelDistance, d = node._depth;
  10376. return (rho >= ld * d) && (rho <= (ld * d + ldist));
  10377. }
  10378. return false;
  10379. }
  10380. },
  10381. 'gradient-multipie': {
  10382. 'render': function(node, canvas) {
  10383. var ctx = canvas.getCtx();
  10384. var height = node.getData('height');
  10385. var ldist = height? height : this.config.levelDistance;
  10386. var radialGradient = ctx.createRadialGradient(0, 0, node.getPos().rho,
  10387. 0, 0, node.getPos().rho + ldist);
  10388. var colorArray = $.hexToRgb(node.getData('color')), ans = [];
  10389. $.each(colorArray, function(i) {
  10390. ans.push(parseInt(i * 0.5, 10));
  10391. });
  10392. var endColor = $.rgbToHex(ans);
  10393. radialGradient.addColorStop(0, endColor);
  10394. radialGradient.addColorStop(1, node.getData('color'));
  10395. ctx.fillStyle = radialGradient;
  10396. this.nodeTypes['multipie'].render.call(this, node, canvas);
  10397. },
  10398. 'contains': function(node, pos) {
  10399. return this.nodeTypes['multipie'].contains.call(this, node, pos);
  10400. }
  10401. },
  10402. 'gradient-pie': {
  10403. 'render': function(node, canvas) {
  10404. var ctx = canvas.getCtx();
  10405. var radialGradient = ctx.createRadialGradient(0, 0, 0, 0, 0, node
  10406. .getPos().rho);
  10407. var colorArray = $.hexToRgb(node.getData('color')), ans = [];
  10408. $.each(colorArray, function(i) {
  10409. ans.push(parseInt(i * 0.5, 10));
  10410. });
  10411. var endColor = $.rgbToHex(ans);
  10412. radialGradient.addColorStop(1, endColor);
  10413. radialGradient.addColorStop(0, node.getData('color'));
  10414. ctx.fillStyle = radialGradient;
  10415. this.nodeTypes['pie'].render.call(this, node, canvas);
  10416. },
  10417. 'contains': function(node, pos) {
  10418. return this.nodeTypes['pie'].contains.call(this, node, pos);
  10419. }
  10420. }
  10421. });
  10422. /*
  10423. Class: Sunburst.Plot.EdgeTypes
  10424. This class contains a list of <Graph.Adjacence> built-in types.
  10425. Edge types implemented are 'none', 'line' and 'arrow'.
  10426. You can add your custom edge types, customizing your visualization to the extreme.
  10427. Example:
  10428. (start code js)
  10429. Sunburst.Plot.EdgeTypes.implement({
  10430. 'mySpecialType': {
  10431. 'render': function(adj, canvas) {
  10432. //print your custom edge to canvas
  10433. },
  10434. //optional
  10435. 'contains': function(adj, pos) {
  10436. //return true if pos is inside the arc or false otherwise
  10437. }
  10438. }
  10439. });
  10440. (end code)
  10441. */
  10442. Sunburst.Plot.EdgeTypes = new Class({
  10443. 'none': $.empty,
  10444. 'line': {
  10445. 'render': function(adj, canvas) {
  10446. var from = adj.nodeFrom.pos.getc(true),
  10447. to = adj.nodeTo.pos.getc(true);
  10448. this.edgeHelper.line.render(from, to, canvas);
  10449. },
  10450. 'contains': function(adj, pos) {
  10451. var from = adj.nodeFrom.pos.getc(true),
  10452. to = adj.nodeTo.pos.getc(true);
  10453. return this.edgeHelper.line.contains(from, to, pos, this.edge.epsilon);
  10454. }
  10455. },
  10456. 'arrow': {
  10457. 'render': function(adj, canvas) {
  10458. var from = adj.nodeFrom.pos.getc(true),
  10459. to = adj.nodeTo.pos.getc(true),
  10460. dim = adj.getData('dim'),
  10461. direction = adj.data.$direction,
  10462. inv = (direction && direction.length>1 && direction[0] != adj.nodeFrom.id);
  10463. this.edgeHelper.arrow.render(from, to, dim, inv, canvas);
  10464. },
  10465. 'contains': function(adj, pos) {
  10466. var from = adj.nodeFrom.pos.getc(true),
  10467. to = adj.nodeTo.pos.getc(true);
  10468. return this.edgeHelper.arrow.contains(from, to, pos, this.edge.epsilon);
  10469. }
  10470. },
  10471. 'hyperline': {
  10472. 'render': function(adj, canvas) {
  10473. var from = adj.nodeFrom.pos.getc(),
  10474. to = adj.nodeTo.pos.getc(),
  10475. dim = Math.max(from.norm(), to.norm());
  10476. this.edgeHelper.hyperline.render(from.$scale(1/dim), to.$scale(1/dim), dim, canvas);
  10477. },
  10478. 'contains': $.lambda(false) //Implement this!
  10479. }
  10480. });
  10481. })($jit.Sunburst);
  10482. /*
  10483. * File: PieChart.js
  10484. *
  10485. */
  10486. $jit.Sunburst.Plot.NodeTypes.implement({
  10487. 'piechart-stacked' : {
  10488. 'render' : function(node, canvas) {
  10489. var pos = node.pos.getp(true),
  10490. dimArray = node.getData('dimArray'),
  10491. valueArray = node.getData('valueArray'),
  10492. colorArray = node.getData('colorArray'),
  10493. colorLength = colorArray.length,
  10494. stringArray = node.getData('stringArray'),
  10495. span = node.getData('span') / 2,
  10496. theta = node.pos.theta,
  10497. begin = theta - span,
  10498. end = theta + span,
  10499. polar = new Polar;
  10500. var ctx = canvas.getCtx(),
  10501. opt = {},
  10502. gradient = node.getData('gradient'),
  10503. border = node.getData('border'),
  10504. config = node.getData('config'),
  10505. showLabels = config.showLabels,
  10506. resizeLabels = config.resizeLabels,
  10507. label = config.Label;
  10508. var xpos = config.sliceOffset * Math.cos((begin + end) /2);
  10509. var ypos = config.sliceOffset * Math.sin((begin + end) /2);
  10510. if (colorArray && dimArray && stringArray) {
  10511. for (var i=0, l=dimArray.length, acum=0, valAcum=0; i<l; i++) {
  10512. var dimi = dimArray[i], colori = colorArray[i % colorLength];
  10513. if(dimi <= 0) continue;
  10514. ctx.fillStyle = ctx.strokeStyle = colori;
  10515. if(gradient && dimi) {
  10516. var radialGradient = ctx.createRadialGradient(xpos, ypos, acum + config.sliceOffset,
  10517. xpos, ypos, acum + dimi + config.sliceOffset);
  10518. var colorRgb = $.hexToRgb(colori),
  10519. ans = $.map(colorRgb, function(i) { return (i * 0.8) >> 0; }),
  10520. endColor = $.rgbToHex(ans);
  10521. radialGradient.addColorStop(0, colori);
  10522. radialGradient.addColorStop(0.5, colori);
  10523. radialGradient.addColorStop(1, endColor);
  10524. ctx.fillStyle = radialGradient;
  10525. }
  10526. polar.rho = acum + config.sliceOffset;
  10527. polar.theta = begin;
  10528. var p1coord = polar.getc(true);
  10529. polar.theta = end;
  10530. var p2coord = polar.getc(true);
  10531. polar.rho += dimi;
  10532. var p3coord = polar.getc(true);
  10533. polar.theta = begin;
  10534. var p4coord = polar.getc(true);
  10535. ctx.beginPath();
  10536. //fixing FF arc method + fill
  10537. ctx.arc(xpos, ypos, acum + .01, begin, end, false);
  10538. ctx.arc(xpos, ypos, acum + dimi + .01, end, begin, true);
  10539. ctx.fill();
  10540. if(border && border.name == stringArray[i]) {
  10541. opt.acum = acum;
  10542. opt.dimValue = dimArray[i];
  10543. opt.begin = begin;
  10544. opt.end = end;
  10545. }
  10546. acum += (dimi || 0);
  10547. valAcum += (valueArray[i] || 0);
  10548. }
  10549. if(border) {
  10550. ctx.save();
  10551. ctx.globalCompositeOperation = "source-over";
  10552. ctx.lineWidth = 2;
  10553. ctx.strokeStyle = border.color;
  10554. var s = begin < end? 1 : -1;
  10555. ctx.beginPath();
  10556. //fixing FF arc method + fill
  10557. ctx.arc(xpos, ypos, opt.acum + .01 + 1, opt.begin, opt.end, false);
  10558. ctx.arc(xpos, ypos, opt.acum + opt.dimValue + .01 - 1, opt.end, opt.begin, true);
  10559. ctx.closePath();
  10560. ctx.stroke();
  10561. ctx.restore();
  10562. }
  10563. if(showLabels && label.type == 'Native') {
  10564. ctx.save();
  10565. ctx.fillStyle = ctx.strokeStyle = label.color;
  10566. var scale = resizeLabels? node.getData('normalizedDim') : 1,
  10567. fontSize = (label.size * scale) >> 0;
  10568. fontSize = fontSize < +resizeLabels? +resizeLabels : fontSize;
  10569. ctx.font = label.style + ' ' + fontSize + 'px ' + label.family;
  10570. ctx.textBaseline = 'middle';
  10571. ctx.textAlign = 'center';
  10572. polar.rho = acum + config.labelOffset + config.sliceOffset;
  10573. polar.theta = node.pos.theta;
  10574. var cart = polar.getc(true);
  10575. ctx.fillText(node.name, cart.x, cart.y);
  10576. ctx.restore();
  10577. }
  10578. }
  10579. },
  10580. 'contains': function(node, pos) {
  10581. if (this.nodeTypes['none'].anglecontains.call(this, node, pos)) {
  10582. var rho = Math.sqrt(pos.x * pos.x + pos.y * pos.y);
  10583. var ld = this.config.levelDistance, d = node._depth;
  10584. var config = node.getData('config');
  10585. if(rho <=ld * d + config.sliceOffset) {
  10586. var dimArray = node.getData('dimArray');
  10587. for(var i=0,l=dimArray.length,acum=config.sliceOffset; i<l; i++) {
  10588. var dimi = dimArray[i];
  10589. if(rho >= acum && rho <= acum + dimi) {
  10590. return {
  10591. name: node.getData('stringArray')[i],
  10592. color: node.getData('colorArray')[i],
  10593. value: node.getData('valueArray')[i],
  10594. label: node.name
  10595. };
  10596. }
  10597. acum += dimi;
  10598. }
  10599. }
  10600. return false;
  10601. }
  10602. return false;
  10603. }
  10604. }
  10605. });
  10606. /*
  10607. Class: PieChart
  10608. A visualization that displays stacked bar charts.
  10609. Constructor Options:
  10610. See <Options.PieChart>.
  10611. */
  10612. $jit.PieChart = new Class({
  10613. sb: null,
  10614. colors: ["#416D9C", "#70A35E", "#EBB056", "#C74243", "#83548B", "#909291", "#557EAA"],
  10615. selected: {},
  10616. busy: false,
  10617. initialize: function(opt) {
  10618. this.controller = this.config =
  10619. $.merge(Options("Canvas", "PieChart", "Label"), {
  10620. Label: { type: 'Native' }
  10621. }, opt);
  10622. this.initializeViz();
  10623. },
  10624. initializeViz: function() {
  10625. var config = this.config, that = this;
  10626. var nodeType = config.type.split(":")[0];
  10627. var delegate = new $jit.Sunburst({
  10628. injectInto: config.injectInto,
  10629. width: config.width,
  10630. height: config.height,
  10631. useCanvas: config.useCanvas,
  10632. withLabels: config.Label.type != 'Native',
  10633. Label: {
  10634. type: config.Label.type
  10635. },
  10636. Node: {
  10637. overridable: true,
  10638. type: 'piechart-' + nodeType,
  10639. width: 1,
  10640. height: 1
  10641. },
  10642. Edge: {
  10643. type: 'none'
  10644. },
  10645. Tips: {
  10646. enable: config.Tips.enable,
  10647. type: 'Native',
  10648. force: true,
  10649. onShow: function(tip, node, contains) {
  10650. var elem = contains;
  10651. config.Tips.onShow(tip, elem, node);
  10652. }
  10653. },
  10654. Events: {
  10655. enable: true,
  10656. type: 'Native',
  10657. onClick: function(node, eventInfo, evt) {
  10658. if(!config.Events.enable) return;
  10659. var elem = eventInfo.getContains();
  10660. config.Events.onClick(elem, eventInfo, evt);
  10661. },
  10662. onMouseMove: function(node, eventInfo, evt) {
  10663. if(!config.hoveredColor) return;
  10664. if(node) {
  10665. var elem = eventInfo.getContains();
  10666. that.select(node.id, elem.name, elem.index);
  10667. } else {
  10668. that.select(false, false, false);
  10669. }
  10670. }
  10671. },
  10672. onCreateLabel: function(domElement, node) {
  10673. var labelConf = config.Label;
  10674. if(config.showLabels) {
  10675. var style = domElement.style;
  10676. style.fontSize = labelConf.size + 'px';
  10677. style.fontFamily = labelConf.family;
  10678. style.color = labelConf.color;
  10679. style.textAlign = 'center';
  10680. domElement.innerHTML = node.name;
  10681. }
  10682. },
  10683. onPlaceLabel: function(domElement, node) {
  10684. if(!config.showLabels) return;
  10685. var pos = node.pos.getp(true),
  10686. dimArray = node.getData('dimArray'),
  10687. span = node.getData('span') / 2,
  10688. theta = node.pos.theta,
  10689. begin = theta - span,
  10690. end = theta + span,
  10691. polar = new Polar;
  10692. var showLabels = config.showLabels,
  10693. resizeLabels = config.resizeLabels,
  10694. label = config.Label;
  10695. if (dimArray) {
  10696. for (var i=0, l=dimArray.length, acum=0; i<l; i++) {
  10697. acum += dimArray[i];
  10698. }
  10699. var scale = resizeLabels? node.getData('normalizedDim') : 1,
  10700. fontSize = (label.size * scale) >> 0;
  10701. fontSize = fontSize < +resizeLabels? +resizeLabels : fontSize;
  10702. domElement.style.fontSize = fontSize + 'px';
  10703. polar.rho = acum + config.labelOffset + config.sliceOffset;
  10704. polar.theta = (begin + end) / 2;
  10705. var pos = polar.getc(true);
  10706. var radius = that.canvas.getSize();
  10707. var labelPos = {
  10708. x: Math.round(pos.x + radius.width / 2),
  10709. y: Math.round(pos.y + radius.height / 2)
  10710. };
  10711. domElement.style.left = labelPos.x + 'px';
  10712. domElement.style.top = labelPos.y + 'px';
  10713. }
  10714. }
  10715. });
  10716. var size = delegate.canvas.getSize(),
  10717. min = Math.min;
  10718. delegate.config.levelDistance = min(size.width, size.height)/2
  10719. - config.offset - config.sliceOffset;
  10720. this.delegate = delegate;
  10721. this.canvas = this.delegate.canvas;
  10722. this.canvas.getCtx().globalCompositeOperation = 'lighter';
  10723. },
  10724. /*
  10725. Method: loadJSON
  10726. Loads JSON data into the visualization.
  10727. Parameters:
  10728. json - The JSON data format. This format is described in <http://blog.thejit.org/2010/04/24/new-javascript-infovis-toolkit-visualizations/#json-data-format>.
  10729. Example:
  10730. (start code js)
  10731. var pieChart = new $jit.PieChart(options);
  10732. pieChart.loadJSON(json);
  10733. (end code)
  10734. */
  10735. loadJSON: function(json) {
  10736. var prefix = $.time(),
  10737. ch = [],
  10738. delegate = this.delegate,
  10739. name = $.splat(json.label),
  10740. nameLength = name.length,
  10741. color = $.splat(json.color || this.colors),
  10742. colorLength = color.length,
  10743. config = this.config,
  10744. gradient = !!config.type.split(":")[1],
  10745. animate = config.animate,
  10746. mono = nameLength == 1;
  10747. for(var i=0, values=json.values, l=values.length; i<l; i++) {
  10748. var val = values[i];
  10749. var valArray = $.splat(val.values);
  10750. ch.push({
  10751. 'id': prefix + val.label,
  10752. 'name': val.label,
  10753. 'data': {
  10754. 'value': valArray,
  10755. '$valueArray': valArray,
  10756. '$colorArray': mono? $.splat(color[i % colorLength]) : color,
  10757. '$stringArray': name,
  10758. '$gradient': gradient,
  10759. '$config': config,
  10760. '$angularWidth': $.reduce(valArray, function(x,y){return x+y;})
  10761. },
  10762. 'children': []
  10763. });
  10764. }
  10765. var root = {
  10766. 'id': prefix + '$root',
  10767. 'name': '',
  10768. 'data': {
  10769. '$type': 'none',
  10770. '$width': 1,
  10771. '$height': 1
  10772. },
  10773. 'children': ch
  10774. };
  10775. delegate.loadJSON(root);
  10776. this.normalizeDims();
  10777. delegate.refresh();
  10778. if(animate) {
  10779. delegate.fx.animate({
  10780. modes: ['node-property:dimArray'],
  10781. duration:1500
  10782. });
  10783. }
  10784. },
  10785. /*
  10786. Method: updateJSON
  10787. Use this method when updating values for the current JSON data. If the items specified by the JSON data already exist in the graph then their values will be updated.
  10788. Parameters:
  10789. json - (object) JSON data to be updated. The JSON format corresponds to the one described in <PieChart.loadJSON>.
  10790. onComplete - (object) A callback object to be called when the animation transition when updating the data end.
  10791. Example:
  10792. (start code js)
  10793. pieChart.updateJSON(json, {
  10794. onComplete: function() {
  10795. alert('update complete!');
  10796. }
  10797. });
  10798. (end code)
  10799. */
  10800. updateJSON: function(json, onComplete) {
  10801. if(this.busy) return;
  10802. this.busy = true;
  10803. var delegate = this.delegate;
  10804. var graph = delegate.graph;
  10805. var values = json.values;
  10806. var animate = this.config.animate;
  10807. var that = this;
  10808. $.each(values, function(v) {
  10809. var n = graph.getByName(v.label),
  10810. vals = $.splat(v.values);
  10811. if(n) {
  10812. n.setData('valueArray', vals);
  10813. n.setData('angularWidth', $.reduce(vals, function(x,y){return x+y;}));
  10814. if(json.label) {
  10815. n.setData('stringArray', $.splat(json.label));
  10816. }
  10817. }
  10818. });
  10819. this.normalizeDims();
  10820. if(animate) {
  10821. delegate.compute('end');
  10822. delegate.fx.animate({
  10823. modes: ['node-property:dimArray:span', 'linear'],
  10824. duration:1500,
  10825. onComplete: function() {
  10826. that.busy = false;
  10827. onComplete && onComplete.onComplete();
  10828. }
  10829. });
  10830. } else {
  10831. delegate.refresh();
  10832. }
  10833. },
  10834. //adds the little brown bar when hovering the node
  10835. select: function(id, name) {
  10836. if(!this.config.hoveredColor) return;
  10837. var s = this.selected;
  10838. if(s.id != id || s.name != name) {
  10839. s.id = id;
  10840. s.name = name;
  10841. s.color = this.config.hoveredColor;
  10842. this.delegate.graph.eachNode(function(n) {
  10843. if(id == n.id) {
  10844. n.setData('border', s);
  10845. } else {
  10846. n.setData('border', false);
  10847. }
  10848. });
  10849. this.delegate.plot();
  10850. }
  10851. },
  10852. /*
  10853. Method: getLegend
  10854. Returns an object containing as keys the legend names and as values hex strings with color values.
  10855. Example:
  10856. (start code js)
  10857. var legend = pieChart.getLegend();
  10858. (end code)
  10859. */
  10860. getLegend: function() {
  10861. var legend = {};
  10862. var n;
  10863. this.delegate.graph.getNode(this.delegate.root).eachAdjacency(function(adj) {
  10864. n = adj.nodeTo;
  10865. });
  10866. var colors = n.getData('colorArray'),
  10867. len = colors.length;
  10868. $.each(n.getData('stringArray'), function(s, i) {
  10869. legend[s] = colors[i % len];
  10870. });
  10871. return legend;
  10872. },
  10873. /*
  10874. Method: getMaxValue
  10875. Returns the maximum accumulated value for the stacks. This method is used for normalizing the graph heights according to the canvas height.
  10876. Example:
  10877. (start code js)
  10878. var ans = pieChart.getMaxValue();
  10879. (end code)
  10880. In some cases it could be useful to override this method to normalize heights for a group of PieCharts, like when doing small multiples.
  10881. Example:
  10882. (start code js)
  10883. //will return 100 for all PieChart instances,
  10884. //displaying all of them with the same scale
  10885. $jit.PieChart.implement({
  10886. 'getMaxValue': function() {
  10887. return 100;
  10888. }
  10889. });
  10890. (end code)
  10891. */
  10892. getMaxValue: function() {
  10893. var maxValue = 0;
  10894. this.delegate.graph.eachNode(function(n) {
  10895. var valArray = n.getData('valueArray'),
  10896. acum = 0;
  10897. $.each(valArray, function(v) {
  10898. acum += +v;
  10899. });
  10900. maxValue = maxValue>acum? maxValue:acum;
  10901. });
  10902. return maxValue;
  10903. },
  10904. normalizeDims: function() {
  10905. //number of elements
  10906. var root = this.delegate.graph.getNode(this.delegate.root), l=0;
  10907. root.eachAdjacency(function() {
  10908. l++;
  10909. });
  10910. var maxValue = this.getMaxValue() || 1,
  10911. config = this.config,
  10912. animate = config.animate,
  10913. rho = this.delegate.config.levelDistance;
  10914. this.delegate.graph.eachNode(function(n) {
  10915. var acum = 0, animateValue = [];
  10916. $.each(n.getData('valueArray'), function(v) {
  10917. acum += +v;
  10918. animateValue.push(1);
  10919. });
  10920. var stat = (animateValue.length == 1) && !config.updateHeights;
  10921. if(animate) {
  10922. n.setData('dimArray', $.map(n.getData('valueArray'), function(n) {
  10923. return stat? rho: (n * rho / maxValue);
  10924. }), 'end');
  10925. var dimArray = n.getData('dimArray');
  10926. if(!dimArray) {
  10927. n.setData('dimArray', animateValue);
  10928. }
  10929. } else {
  10930. n.setData('dimArray', $.map(n.getData('valueArray'), function(n) {
  10931. return stat? rho : (n * rho / maxValue);
  10932. }));
  10933. }
  10934. n.setData('normalizedDim', acum / maxValue);
  10935. });
  10936. }
  10937. });
  10938. /*
  10939. * Class: Layouts.TM
  10940. *
  10941. * Implements TreeMaps layouts (SliceAndDice, Squarified, Strip).
  10942. *
  10943. * Implemented By:
  10944. *
  10945. * <TM>
  10946. *
  10947. */
  10948. Layouts.TM = {};
  10949. Layouts.TM.SliceAndDice = new Class({
  10950. compute: function(prop) {
  10951. var root = this.graph.getNode(this.clickedNode && this.clickedNode.id || this.root);
  10952. this.controller.onBeforeCompute(root);
  10953. var size = this.canvas.getSize(),
  10954. config = this.config,
  10955. width = size.width,
  10956. height = size.height;
  10957. this.graph.computeLevels(this.root, 0, "ignore");
  10958. //set root position and dimensions
  10959. root.getPos(prop).setc(-width/2, -height/2);
  10960. root.setData('width', width, prop);
  10961. root.setData('height', height + config.titleHeight, prop);
  10962. this.computePositions(root, root, this.layout.orientation, prop);
  10963. this.controller.onAfterCompute(root);
  10964. },
  10965. computePositions: function(par, ch, orn, prop) {
  10966. //compute children areas
  10967. var totalArea = 0;
  10968. par.eachSubnode(function(n) {
  10969. totalArea += n.getData('area', prop);
  10970. });
  10971. var config = this.config,
  10972. offst = config.offset,
  10973. width = par.getData('width', prop),
  10974. height = Math.max(par.getData('height', prop) - config.titleHeight, 0),
  10975. fact = par == ch? 1 : (ch.getData('area', prop) / totalArea);
  10976. var otherSize, size, dim, pos, pos2, posth, pos2th;
  10977. var horizontal = (orn == "h");
  10978. if(horizontal) {
  10979. orn = 'v';
  10980. otherSize = height;
  10981. size = width * fact;
  10982. dim = 'height';
  10983. pos = 'y';
  10984. pos2 = 'x';
  10985. posth = config.titleHeight;
  10986. pos2th = 0;
  10987. } else {
  10988. orn = 'h';
  10989. otherSize = height * fact;
  10990. size = width;
  10991. dim = 'width';
  10992. pos = 'x';
  10993. pos2 = 'y';
  10994. posth = 0;
  10995. pos2th = config.titleHeight;
  10996. }
  10997. var cpos = ch.getPos(prop);
  10998. ch.setData('width', size, prop);
  10999. ch.setData('height', otherSize, prop);
  11000. var offsetSize = 0, tm = this;
  11001. ch.eachSubnode(function(n) {
  11002. var p = n.getPos(prop);
  11003. p[pos] = offsetSize + cpos[pos] + posth;
  11004. p[pos2] = cpos[pos2] + pos2th;
  11005. tm.computePositions(ch, n, orn, prop);
  11006. offsetSize += n.getData(dim, prop);
  11007. });
  11008. }
  11009. });
  11010. Layouts.TM.Area = {
  11011. /*
  11012. Method: compute
  11013. Called by loadJSON to calculate recursively all node positions and lay out the tree.
  11014. Parameters:
  11015. json - A JSON tree. See also <Loader.loadJSON>.
  11016. coord - A coordinates object specifying width, height, left and top style properties.
  11017. */
  11018. compute: function(prop) {
  11019. prop = prop || "current";
  11020. var root = this.graph.getNode(this.clickedNode && this.clickedNode.id || this.root);
  11021. this.controller.onBeforeCompute(root);
  11022. var config = this.config,
  11023. size = this.canvas.getSize(),
  11024. width = size.width,
  11025. height = size.height,
  11026. offst = config.offset,
  11027. offwdth = width - offst,
  11028. offhght = height - offst;
  11029. this.graph.computeLevels(this.root, 0, "ignore");
  11030. //set root position and dimensions
  11031. root.getPos(prop).setc(-width/2, -height/2);
  11032. root.setData('width', width, prop);
  11033. root.setData('height', height, prop);
  11034. //create a coordinates object
  11035. var coord = {
  11036. 'top': -height/2 + config.titleHeight,
  11037. 'left': -width/2,
  11038. 'width': offwdth,
  11039. 'height': offhght - config.titleHeight
  11040. };
  11041. this.computePositions(root, coord, prop);
  11042. this.controller.onAfterCompute(root);
  11043. },
  11044. /*
  11045. Method: computeDim
  11046. Computes dimensions and positions of a group of nodes
  11047. according to a custom layout row condition.
  11048. Parameters:
  11049. tail - An array of nodes.
  11050. initElem - An array of nodes (containing the initial node to be laid).
  11051. w - A fixed dimension where nodes will be layed out.
  11052. coord - A coordinates object specifying width, height, left and top style properties.
  11053. comp - A custom comparison function
  11054. */
  11055. computeDim: function(tail, initElem, w, coord, comp, prop) {
  11056. if(tail.length + initElem.length == 1) {
  11057. var l = (tail.length == 1)? tail : initElem;
  11058. this.layoutLast(l, w, coord, prop);
  11059. return;
  11060. }
  11061. if(tail.length >= 2 && initElem.length == 0) {
  11062. initElem = [tail.shift()];
  11063. }
  11064. if(tail.length == 0) {
  11065. if(initElem.length > 0) this.layoutRow(initElem, w, coord, prop);
  11066. return;
  11067. }
  11068. var c = tail[0];
  11069. if(comp(initElem, w) >= comp([c].concat(initElem), w)) {
  11070. this.computeDim(tail.slice(1), initElem.concat([c]), w, coord, comp, prop);
  11071. } else {
  11072. var newCoords = this.layoutRow(initElem, w, coord, prop);
  11073. this.computeDim(tail, [], newCoords.dim, newCoords, comp, prop);
  11074. }
  11075. },
  11076. /*
  11077. Method: worstAspectRatio
  11078. Calculates the worst aspect ratio of a group of rectangles.
  11079. See also:
  11080. <http://en.wikipedia.org/wiki/Aspect_ratio>
  11081. Parameters:
  11082. ch - An array of nodes.
  11083. w - The fixed dimension where rectangles are being laid out.
  11084. Returns:
  11085. The worst aspect ratio.
  11086. */
  11087. worstAspectRatio: function(ch, w) {
  11088. if(!ch || ch.length == 0) return Number.MAX_VALUE;
  11089. var areaSum = 0, maxArea = 0, minArea = Number.MAX_VALUE;
  11090. for(var i=0, l=ch.length; i<l; i++) {
  11091. var area = ch[i]._area;
  11092. areaSum += area;
  11093. minArea = minArea < area? minArea : area;
  11094. maxArea = maxArea > area? maxArea : area;
  11095. }
  11096. var sqw = w * w, sqAreaSum = areaSum * areaSum;
  11097. return Math.max(sqw * maxArea / sqAreaSum,
  11098. sqAreaSum / (sqw * minArea));
  11099. },
  11100. /*
  11101. Method: avgAspectRatio
  11102. Calculates the average aspect ratio of a group of rectangles.
  11103. See also:
  11104. <http://en.wikipedia.org/wiki/Aspect_ratio>
  11105. Parameters:
  11106. ch - An array of nodes.
  11107. w - The fixed dimension where rectangles are being laid out.
  11108. Returns:
  11109. The average aspect ratio.
  11110. */
  11111. avgAspectRatio: function(ch, w) {
  11112. if(!ch || ch.length == 0) return Number.MAX_VALUE;
  11113. var arSum = 0;
  11114. for(var i=0, l=ch.length; i<l; i++) {
  11115. var area = ch[i]._area;
  11116. var h = area / w;
  11117. arSum += w > h? w / h : h / w;
  11118. }
  11119. return arSum / l;
  11120. },
  11121. /*
  11122. layoutLast
  11123. Performs the layout of the last computed sibling.
  11124. Parameters:
  11125. ch - An array of nodes.
  11126. w - A fixed dimension where nodes will be layed out.
  11127. coord - A coordinates object specifying width, height, left and top style properties.
  11128. */
  11129. layoutLast: function(ch, w, coord, prop) {
  11130. var child = ch[0];
  11131. child.getPos(prop).setc(coord.left, coord.top);
  11132. child.setData('width', coord.width, prop);
  11133. child.setData('height', coord.height, prop);
  11134. }
  11135. };
  11136. Layouts.TM.Squarified = new Class({
  11137. Implements: Layouts.TM.Area,
  11138. computePositions: function(node, coord, prop) {
  11139. var config = this.config,
  11140. max = Math.max;
  11141. if (coord.width >= coord.height)
  11142. this.layout.orientation = 'h';
  11143. else
  11144. this.layout.orientation = 'v';
  11145. var ch = node.getSubnodes([1, 1], "ignore");
  11146. if(ch.length > 0) {
  11147. this.processChildrenLayout(node, ch, coord, prop);
  11148. for(var i=0, l=ch.length; i<l; i++) {
  11149. var chi = ch[i],
  11150. offst = config.offset,
  11151. height = max(chi.getData('height', prop) - offst - config.titleHeight, 0),
  11152. width = max(chi.getData('width', prop) - offst, 0),
  11153. chipos = chi.getPos(prop);
  11154. coord = {
  11155. 'width': width,
  11156. 'height': height,
  11157. 'top': chipos.y + config.titleHeight,
  11158. 'left': chipos.x
  11159. };
  11160. this.computePositions(chi, coord, prop);
  11161. }
  11162. }
  11163. },
  11164. /*
  11165. Method: processChildrenLayout
  11166. Computes children real areas and other useful parameters for performing the Squarified algorithm.
  11167. Parameters:
  11168. par - The parent node of the json subtree.
  11169. ch - An Array of nodes
  11170. coord - A coordinates object specifying width, height, left and top style properties.
  11171. */
  11172. processChildrenLayout: function(par, ch, coord, prop) {
  11173. //compute children real areas
  11174. var parentArea = coord.width * coord.height;
  11175. var i, l=ch.length, totalChArea=0, chArea = [];
  11176. for(i=0; i<l; i++) {
  11177. chArea[i] = parseFloat(ch[i].getData('area', prop));
  11178. totalChArea += chArea[i];
  11179. }
  11180. for(i=0; i<l; i++) {
  11181. ch[i]._area = parentArea * chArea[i] / totalChArea;
  11182. }
  11183. var minimumSideValue = this.layout.horizontal()? coord.height : coord.width;
  11184. ch.sort(function(a, b) {
  11185. var diff = b._area - a._area;
  11186. return diff? diff : (b.id == a.id? 0 : (b.id < a.id? 1 : -1));
  11187. });
  11188. var initElem = [ch[0]];
  11189. var tail = ch.slice(1);
  11190. this.squarify(tail, initElem, minimumSideValue, coord, prop);
  11191. },
  11192. /*
  11193. Method: squarify
  11194. Performs an heuristic method to calculate div elements sizes in order to have a good aspect ratio.
  11195. Parameters:
  11196. tail - An array of nodes.
  11197. initElem - An array of nodes, containing the initial node to be laid out.
  11198. w - A fixed dimension where nodes will be laid out.
  11199. coord - A coordinates object specifying width, height, left and top style properties.
  11200. */
  11201. squarify: function(tail, initElem, w, coord, prop) {
  11202. this.computeDim(tail, initElem, w, coord, this.worstAspectRatio, prop);
  11203. },
  11204. /*
  11205. Method: layoutRow
  11206. Performs the layout of an array of nodes.
  11207. Parameters:
  11208. ch - An array of nodes.
  11209. w - A fixed dimension where nodes will be laid out.
  11210. coord - A coordinates object specifying width, height, left and top style properties.
  11211. */
  11212. layoutRow: function(ch, w, coord, prop) {
  11213. if(this.layout.horizontal()) {
  11214. return this.layoutV(ch, w, coord, prop);
  11215. } else {
  11216. return this.layoutH(ch, w, coord, prop);
  11217. }
  11218. },
  11219. layoutV: function(ch, w, coord, prop) {
  11220. var totalArea = 0, rnd = function(x) { return x; };
  11221. $.each(ch, function(elem) { totalArea += elem._area; });
  11222. var width = rnd(totalArea / w), top = 0;
  11223. for(var i=0, l=ch.length; i<l; i++) {
  11224. var h = rnd(ch[i]._area / width);
  11225. var chi = ch[i];
  11226. chi.getPos(prop).setc(coord.left, coord.top + top);
  11227. chi.setData('width', width, prop);
  11228. chi.setData('height', h, prop);
  11229. top += h;
  11230. }
  11231. var ans = {
  11232. 'height': coord.height,
  11233. 'width': coord.width - width,
  11234. 'top': coord.top,
  11235. 'left': coord.left + width
  11236. };
  11237. //take minimum side value.
  11238. ans.dim = Math.min(ans.width, ans.height);
  11239. if(ans.dim != ans.height) this.layout.change();
  11240. return ans;
  11241. },
  11242. layoutH: function(ch, w, coord, prop) {
  11243. var totalArea = 0;
  11244. $.each(ch, function(elem) { totalArea += elem._area; });
  11245. var height = totalArea / w,
  11246. top = coord.top,
  11247. left = 0;
  11248. for(var i=0, l=ch.length; i<l; i++) {
  11249. var chi = ch[i];
  11250. var w = chi._area / height;
  11251. chi.getPos(prop).setc(coord.left + left, top);
  11252. chi.setData('width', w, prop);
  11253. chi.setData('height', height, prop);
  11254. left += w;
  11255. }
  11256. var ans = {
  11257. 'height': coord.height - height,
  11258. 'width': coord.width,
  11259. 'top': coord.top + height,
  11260. 'left': coord.left
  11261. };
  11262. ans.dim = Math.min(ans.width, ans.height);
  11263. if(ans.dim != ans.width) this.layout.change();
  11264. return ans;
  11265. }
  11266. });
  11267. Layouts.TM.Strip = new Class({
  11268. Implements: Layouts.TM.Area,
  11269. /*
  11270. Method: compute
  11271. Called by loadJSON to calculate recursively all node positions and lay out the tree.
  11272. Parameters:
  11273. json - A JSON subtree. See also <Loader.loadJSON>.
  11274. coord - A coordinates object specifying width, height, left and top style properties.
  11275. */
  11276. computePositions: function(node, coord, prop) {
  11277. var ch = node.getSubnodes([1, 1], "ignore"),
  11278. config = this.config,
  11279. max = Math.max;
  11280. if(ch.length > 0) {
  11281. this.processChildrenLayout(node, ch, coord, prop);
  11282. for(var i=0, l=ch.length; i<l; i++) {
  11283. var chi = ch[i];
  11284. var offst = config.offset,
  11285. height = max(chi.getData('height', prop) - offst - config.titleHeight, 0),
  11286. width = max(chi.getData('width', prop) - offst, 0);
  11287. var chipos = chi.getPos(prop);
  11288. coord = {
  11289. 'width': width,
  11290. 'height': height,
  11291. 'top': chipos.y + config.titleHeight,
  11292. 'left': chipos.x
  11293. };
  11294. this.computePositions(chi, coord, prop);
  11295. }
  11296. }
  11297. },
  11298. /*
  11299. Method: processChildrenLayout
  11300. Computes children real areas and other useful parameters for performing the Strip algorithm.
  11301. Parameters:
  11302. par - The parent node of the json subtree.
  11303. ch - An Array of nodes
  11304. coord - A coordinates object specifying width, height, left and top style properties.
  11305. */
  11306. processChildrenLayout: function(par, ch, coord, prop) {
  11307. //compute children real areas
  11308. var parentArea = coord.width * coord.height;
  11309. var i, l=ch.length, totalChArea=0, chArea = [];
  11310. for(i=0; i<l; i++) {
  11311. chArea[i] = +ch[i].getData('area', prop);
  11312. totalChArea += chArea[i];
  11313. }
  11314. for(i=0; i<l; i++) {
  11315. ch[i]._area = parentArea * chArea[i] / totalChArea;
  11316. }
  11317. var side = this.layout.horizontal()? coord.width : coord.height;
  11318. var initElem = [ch[0]];
  11319. var tail = ch.slice(1);
  11320. this.stripify(tail, initElem, side, coord, prop);
  11321. },
  11322. /*
  11323. Method: stripify
  11324. Performs an heuristic method to calculate div elements sizes in order to have
  11325. a good compromise between aspect ratio and order.
  11326. Parameters:
  11327. tail - An array of nodes.
  11328. initElem - An array of nodes.
  11329. w - A fixed dimension where nodes will be layed out.
  11330. coord - A coordinates object specifying width, height, left and top style properties.
  11331. */
  11332. stripify: function(tail, initElem, w, coord, prop) {
  11333. this.computeDim(tail, initElem, w, coord, this.avgAspectRatio, prop);
  11334. },
  11335. /*
  11336. Method: layoutRow
  11337. Performs the layout of an array of nodes.
  11338. Parameters:
  11339. ch - An array of nodes.
  11340. w - A fixed dimension where nodes will be laid out.
  11341. coord - A coordinates object specifying width, height, left and top style properties.
  11342. */
  11343. layoutRow: function(ch, w, coord, prop) {
  11344. if(this.layout.horizontal()) {
  11345. return this.layoutH(ch, w, coord, prop);
  11346. } else {
  11347. return this.layoutV(ch, w, coord, prop);
  11348. }
  11349. },
  11350. layoutV: function(ch, w, coord, prop) {
  11351. var totalArea = 0;
  11352. $.each(ch, function(elem) { totalArea += elem._area; });
  11353. var width = totalArea / w, top = 0;
  11354. for(var i=0, l=ch.length; i<l; i++) {
  11355. var chi = ch[i];
  11356. var h = chi._area / width;
  11357. chi.getPos(prop).setc(coord.left,
  11358. coord.top + (w - h - top));
  11359. chi.setData('width', width, prop);
  11360. chi.setData('height', h, prop);
  11361. top += h;
  11362. }
  11363. return {
  11364. 'height': coord.height,
  11365. 'width': coord.width - width,
  11366. 'top': coord.top,
  11367. 'left': coord.left + width,
  11368. 'dim': w
  11369. };
  11370. },
  11371. layoutH: function(ch, w, coord, prop) {
  11372. var totalArea = 0;
  11373. $.each(ch, function(elem) { totalArea += elem._area; });
  11374. var height = totalArea / w,
  11375. top = coord.height - height,
  11376. left = 0;
  11377. for(var i=0, l=ch.length; i<l; i++) {
  11378. var chi = ch[i];
  11379. var s = chi._area / height;
  11380. chi.getPos(prop).setc(coord.left + left, coord.top + top);
  11381. chi.setData('width', s, prop);
  11382. chi.setData('height', height, prop);
  11383. left += s;
  11384. }
  11385. return {
  11386. 'height': coord.height - height,
  11387. 'width': coord.width,
  11388. 'top': coord.top,
  11389. 'left': coord.left,
  11390. 'dim': w
  11391. };
  11392. }
  11393. });
  11394. /*
  11395. * Class: Layouts.Icicle
  11396. *
  11397. * Implements the icicle tree layout.
  11398. *
  11399. * Implemented By:
  11400. *
  11401. * <Icicle>
  11402. *
  11403. */
  11404. Layouts.Icicle = new Class({
  11405. /*
  11406. * Method: compute
  11407. *
  11408. * Called by loadJSON to calculate all node positions.
  11409. *
  11410. * Parameters:
  11411. *
  11412. * posType - The nodes' position to compute. Either "start", "end" or
  11413. * "current". Defaults to "current".
  11414. */
  11415. compute: function(posType) {
  11416. posType = posType || "current";
  11417. var root = this.graph.getNode(this.root),
  11418. config = this.config,
  11419. size = this.canvas.getSize(),
  11420. width = size.width,
  11421. height = size.height,
  11422. offset = config.offset,
  11423. levelsToShow = config.constrained ? config.levelsToShow : Number.MAX_VALUE;
  11424. this.controller.onBeforeCompute(root);
  11425. Graph.Util.computeLevels(this.graph, root.id, 0, "ignore");
  11426. var treeDepth = 0;
  11427. Graph.Util.eachLevel(root, 0, false, function (n, d) { if(d > treeDepth) treeDepth = d; });
  11428. var startNode = this.graph.getNode(this.clickedNode && this.clickedNode.id || root.id);
  11429. var maxDepth = Math.min(treeDepth, levelsToShow-1);
  11430. var initialDepth = startNode._depth;
  11431. if(this.layout.horizontal()) {
  11432. this.computeSubtree(startNode, -width/2, -height/2, width/(maxDepth+1), height, initialDepth, maxDepth, posType);
  11433. } else {
  11434. this.computeSubtree(startNode, -width/2, -height/2, width, height/(maxDepth+1), initialDepth, maxDepth, posType);
  11435. }
  11436. },
  11437. computeSubtree: function (root, x, y, width, height, initialDepth, maxDepth, posType) {
  11438. root.getPos(posType).setc(x, y);
  11439. root.setData('width', width, posType);
  11440. root.setData('height', height, posType);
  11441. var nodeLength, prevNodeLength = 0, totalDim = 0;
  11442. var children = Graph.Util.getSubnodes(root, [1, 1], 'ignore'); // next level from this node
  11443. if(!children.length)
  11444. return;
  11445. $.each(children, function(e) { totalDim += e.getData('dim'); });
  11446. for(var i=0, l=children.length; i < l; i++) {
  11447. if(this.layout.horizontal()) {
  11448. nodeLength = height * children[i].getData('dim') / totalDim;
  11449. this.computeSubtree(children[i], x+width, y, width, nodeLength, initialDepth, maxDepth, posType);
  11450. y += nodeLength;
  11451. } else {
  11452. nodeLength = width * children[i].getData('dim') / totalDim;
  11453. this.computeSubtree(children[i], x, y+height, nodeLength, height, initialDepth, maxDepth, posType);
  11454. x += nodeLength;
  11455. }
  11456. }
  11457. }
  11458. });
  11459. /*
  11460. * File: Icicle.js
  11461. *
  11462. */
  11463. /*
  11464. Class: Icicle
  11465. Icicle space filling visualization.
  11466. Implements:
  11467. All <Loader> methods
  11468. Constructor Options:
  11469. Inherits options from
  11470. - <Options.Canvas>
  11471. - <Options.Controller>
  11472. - <Options.Node>
  11473. - <Options.Edge>
  11474. - <Options.Label>
  11475. - <Options.Events>
  11476. - <Options.Tips>
  11477. - <Options.NodeStyles>
  11478. - <Options.Navigation>
  11479. Additionally, there are other parameters and some default values changed
  11480. orientation - (string) Default's *h*. Whether to set horizontal or vertical layouts. Possible values are 'h' and 'v'.
  11481. offset - (number) Default's *2*. Boxes offset.
  11482. constrained - (boolean) Default's *false*. Whether to show the entire tree when loaded or just the number of levels specified by _levelsToShow_.
  11483. levelsToShow - (number) Default's *3*. The number of levels to show for a subtree. This number is relative to the selected node.
  11484. animate - (boolean) Default's *false*. Whether to animate transitions.
  11485. Node.type - Described in <Options.Node>. Default's *rectangle*.
  11486. Label.type - Described in <Options.Label>. Default's *Native*.
  11487. duration - Described in <Options.Fx>. Default's *700*.
  11488. fps - Described in <Options.Fx>. Default's *45*.
  11489. Instance Properties:
  11490. canvas - Access a <Canvas> instance.
  11491. graph - Access a <Graph> instance.
  11492. op - Access a <Icicle.Op> instance.
  11493. fx - Access a <Icicle.Plot> instance.
  11494. labels - Access a <Icicle.Label> interface implementation.
  11495. */
  11496. $jit.Icicle = new Class({
  11497. Implements: [ Loader, Extras, Layouts.Icicle ],
  11498. layout: {
  11499. orientation: "h",
  11500. vertical: function(){
  11501. return this.orientation == "v";
  11502. },
  11503. horizontal: function(){
  11504. return this.orientation == "h";
  11505. },
  11506. change: function(){
  11507. this.orientation = this.vertical()? "h" : "v";
  11508. }
  11509. },
  11510. initialize: function(controller) {
  11511. var config = {
  11512. animate: false,
  11513. orientation: "h",
  11514. offset: 2,
  11515. levelsToShow: Number.MAX_VALUE,
  11516. constrained: false,
  11517. Node: {
  11518. type: 'rectangle',
  11519. overridable: true
  11520. },
  11521. Edge: {
  11522. type: 'none'
  11523. },
  11524. Label: {
  11525. type: 'Native'
  11526. },
  11527. duration: 700,
  11528. fps: 45
  11529. };
  11530. var opts = Options("Canvas", "Node", "Edge", "Fx", "Tips", "NodeStyles",
  11531. "Events", "Navigation", "Controller", "Label");
  11532. this.controller = this.config = $.merge(opts, config, controller);
  11533. this.layout.orientation = this.config.orientation;
  11534. var canvasConfig = this.config;
  11535. if (canvasConfig.useCanvas) {
  11536. this.canvas = canvasConfig.useCanvas;
  11537. this.config.labelContainer = this.canvas.id + '-label';
  11538. } else {
  11539. this.canvas = new Canvas(this, canvasConfig);
  11540. this.config.labelContainer = (typeof canvasConfig.injectInto == 'string'? canvasConfig.injectInto : canvasConfig.injectInto.id) + '-label';
  11541. }
  11542. this.graphOptions = {
  11543. 'klass': Complex,
  11544. 'Node': {
  11545. 'selected': false,
  11546. 'exist': true,
  11547. 'drawn': true
  11548. }
  11549. };
  11550. this.graph = new Graph(
  11551. this.graphOptions, this.config.Node, this.config.Edge, this.config.Label);
  11552. this.labels = new $jit.Icicle.Label[this.config.Label.type](this);
  11553. this.fx = new $jit.Icicle.Plot(this, $jit.Icicle);
  11554. this.op = new $jit.Icicle.Op(this);
  11555. this.group = new $jit.Icicle.Group(this);
  11556. this.clickedNode = null;
  11557. this.initializeExtras();
  11558. },
  11559. /*
  11560. Method: refresh
  11561. Computes positions and plots the tree.
  11562. */
  11563. refresh: function(){
  11564. var labelType = this.config.Label.type;
  11565. if(labelType != 'Native') {
  11566. var that = this;
  11567. this.graph.eachNode(function(n) { that.labels.hideLabel(n, false); });
  11568. }
  11569. this.compute();
  11570. this.plot();
  11571. },
  11572. /*
  11573. Method: plot
  11574. Plots the Icicle visualization. This is a shortcut to *fx.plot*.
  11575. */
  11576. plot: function(){
  11577. this.fx.plot(this.config);
  11578. },
  11579. /*
  11580. Method: enter
  11581. Sets the node as root.
  11582. Parameters:
  11583. node - (object) A <Graph.Node>.
  11584. */
  11585. enter: function (node) {
  11586. if (this.busy)
  11587. return;
  11588. this.busy = true;
  11589. var that = this,
  11590. config = this.config;
  11591. var callback = {
  11592. onComplete: function() {
  11593. //compute positions of newly inserted nodes
  11594. if(config.request)
  11595. that.compute();
  11596. if(config.animate) {
  11597. that.graph.nodeList.setDataset(['current', 'end'], {
  11598. 'alpha': [1, 0] //fade nodes
  11599. });
  11600. Graph.Util.eachSubgraph(node, function(n) {
  11601. n.setData('alpha', 1, 'end');
  11602. }, "ignore");
  11603. that.fx.animate({
  11604. duration: 500,
  11605. modes:['node-property:alpha'],
  11606. onComplete: function() {
  11607. that.clickedNode = node;
  11608. that.compute('end');
  11609. that.fx.animate({
  11610. modes:['linear', 'node-property:width:height'],
  11611. duration: 1000,
  11612. onComplete: function() {
  11613. that.busy = false;
  11614. that.clickedNode = node;
  11615. }
  11616. });
  11617. }
  11618. });
  11619. } else {
  11620. that.clickedNode = node;
  11621. that.busy = false;
  11622. that.refresh();
  11623. }
  11624. }
  11625. };
  11626. if(config.request) {
  11627. this.requestNodes(clickedNode, callback);
  11628. } else {
  11629. callback.onComplete();
  11630. }
  11631. },
  11632. /*
  11633. Method: out
  11634. Sets the parent node of the current selected node as root.
  11635. */
  11636. out: function(){
  11637. if(this.busy)
  11638. return;
  11639. var that = this,
  11640. GUtil = Graph.Util,
  11641. config = this.config,
  11642. graph = this.graph,
  11643. parents = GUtil.getParents(graph.getNode(this.clickedNode && this.clickedNode.id || this.root)),
  11644. parent = parents[0],
  11645. clickedNode = parent,
  11646. previousClickedNode = this.clickedNode;
  11647. this.busy = true;
  11648. this.events.hoveredNode = false;
  11649. if(!parent) {
  11650. this.busy = false;
  11651. return;
  11652. }
  11653. //final plot callback
  11654. callback = {
  11655. onComplete: function() {
  11656. that.clickedNode = parent;
  11657. if(config.request) {
  11658. that.requestNodes(parent, {
  11659. onComplete: function() {
  11660. that.compute();
  11661. that.plot();
  11662. that.busy = false;
  11663. }
  11664. });
  11665. } else {
  11666. that.compute();
  11667. that.plot();
  11668. that.busy = false;
  11669. }
  11670. }
  11671. };
  11672. //animate node positions
  11673. if(config.animate) {
  11674. this.clickedNode = clickedNode;
  11675. this.compute('end');
  11676. //animate the visible subtree only
  11677. this.clickedNode = previousClickedNode;
  11678. this.fx.animate({
  11679. modes:['linear', 'node-property:width:height'],
  11680. duration: 1000,
  11681. onComplete: function() {
  11682. //animate the parent subtree
  11683. that.clickedNode = clickedNode;
  11684. //change nodes alpha
  11685. graph.nodeList.setDataset(['current', 'end'], {
  11686. 'alpha': [0, 1]
  11687. });
  11688. GUtil.eachSubgraph(previousClickedNode, function(node) {
  11689. node.setData('alpha', 1);
  11690. }, "ignore");
  11691. that.fx.animate({
  11692. duration: 500,
  11693. modes:['node-property:alpha'],
  11694. onComplete: function() {
  11695. callback.onComplete();
  11696. }
  11697. });
  11698. }
  11699. });
  11700. } else {
  11701. callback.onComplete();
  11702. }
  11703. },
  11704. requestNodes: function(node, onComplete){
  11705. var handler = $.merge(this.controller, onComplete),
  11706. levelsToShow = this.config.constrained ? this.config.levelsToShow : Number.MAX_VALUE;
  11707. if (handler.request) {
  11708. var leaves = [], d = node._depth;
  11709. Graph.Util.eachLevel(node, 0, levelsToShow, function(n){
  11710. if (n.drawn && !Graph.Util.anySubnode(n)) {
  11711. leaves.push(n);
  11712. n._level = n._depth - d;
  11713. if (this.config.constrained)
  11714. n._level = levelsToShow - n._level;
  11715. }
  11716. });
  11717. this.group.requestNodes(leaves, handler);
  11718. } else {
  11719. handler.onComplete();
  11720. }
  11721. }
  11722. });
  11723. /*
  11724. Class: Icicle.Op
  11725. Custom extension of <Graph.Op>.
  11726. Extends:
  11727. All <Graph.Op> methods
  11728. See also:
  11729. <Graph.Op>
  11730. */
  11731. $jit.Icicle.Op = new Class({
  11732. Implements: Graph.Op
  11733. });
  11734. /*
  11735. * Performs operations on group of nodes.
  11736. */
  11737. $jit.Icicle.Group = new Class({
  11738. initialize: function(viz){
  11739. this.viz = viz;
  11740. this.canvas = viz.canvas;
  11741. this.config = viz.config;
  11742. },
  11743. /*
  11744. * Calls the request method on the controller to request a subtree for each node.
  11745. */
  11746. requestNodes: function(nodes, controller){
  11747. var counter = 0, len = nodes.length, nodeSelected = {};
  11748. var complete = function(){
  11749. controller.onComplete();
  11750. };
  11751. var viz = this.viz;
  11752. if (len == 0)
  11753. complete();
  11754. for(var i = 0; i < len; i++) {
  11755. nodeSelected[nodes[i].id] = nodes[i];
  11756. controller.request(nodes[i].id, nodes[i]._level, {
  11757. onComplete: function(nodeId, data){
  11758. if (data && data.children) {
  11759. data.id = nodeId;
  11760. viz.op.sum(data, {
  11761. type: 'nothing'
  11762. });
  11763. }
  11764. if (++counter == len) {
  11765. Graph.Util.computeLevels(viz.graph, viz.root, 0);
  11766. complete();
  11767. }
  11768. }
  11769. });
  11770. }
  11771. }
  11772. });
  11773. /*
  11774. Class: Icicle.Plot
  11775. Custom extension of <Graph.Plot>.
  11776. Extends:
  11777. All <Graph.Plot> methods
  11778. See also:
  11779. <Graph.Plot>
  11780. */
  11781. $jit.Icicle.Plot = new Class({
  11782. Implements: Graph.Plot,
  11783. plot: function(opt, animating){
  11784. opt = opt || this.viz.controller;
  11785. var viz = this.viz,
  11786. graph = viz.graph,
  11787. root = graph.getNode(viz.clickedNode && viz.clickedNode.id || viz.root),
  11788. initialDepth = root._depth;
  11789. viz.canvas.clear();
  11790. this.plotTree(root, $.merge(opt, {
  11791. 'withLabels': true,
  11792. 'hideLabels': false,
  11793. 'plotSubtree': function(root, node) {
  11794. return !viz.config.constrained ||
  11795. (node._depth - initialDepth < viz.config.levelsToShow);
  11796. }
  11797. }), animating);
  11798. }
  11799. });
  11800. /*
  11801. Class: Icicle.Label
  11802. Custom extension of <Graph.Label>.
  11803. Contains custom <Graph.Label.SVG>, <Graph.Label.HTML> and <Graph.Label.Native> extensions.
  11804. Extends:
  11805. All <Graph.Label> methods and subclasses.
  11806. See also:
  11807. <Graph.Label>, <Graph.Label.Native>, <Graph.Label.HTML>, <Graph.Label.SVG>.
  11808. */
  11809. $jit.Icicle.Label = {};
  11810. /*
  11811. Icicle.Label.Native
  11812. Custom extension of <Graph.Label.Native>.
  11813. Extends:
  11814. All <Graph.Label.Native> methods
  11815. See also:
  11816. <Graph.Label.Native>
  11817. */
  11818. $jit.Icicle.Label.Native = new Class({
  11819. Implements: Graph.Label.Native,
  11820. renderLabel: function(canvas, node, controller) {
  11821. var ctx = canvas.getCtx(),
  11822. width = node.getData('width'),
  11823. height = node.getData('height'),
  11824. size = node.getLabelData('size'),
  11825. m = ctx.measureText(node.name);
  11826. // Guess as much as possible if the label will fit in the node
  11827. if(height < (size * 1.5) || width < m.width)
  11828. return;
  11829. var pos = node.pos.getc(true);
  11830. ctx.fillText(node.name,
  11831. pos.x + width / 2,
  11832. pos.y + height / 2);
  11833. }
  11834. });
  11835. /*
  11836. Icicle.Label.SVG
  11837. Custom extension of <Graph.Label.SVG>.
  11838. Extends:
  11839. All <Graph.Label.SVG> methods
  11840. See also:
  11841. <Graph.Label.SVG>
  11842. */
  11843. $jit.Icicle.Label.SVG = new Class( {
  11844. Implements: Graph.Label.SVG,
  11845. initialize: function(viz){
  11846. this.viz = viz;
  11847. },
  11848. /*
  11849. placeLabel
  11850. Overrides abstract method placeLabel in <Graph.Plot>.
  11851. Parameters:
  11852. tag - A DOM label element.
  11853. node - A <Graph.Node>.
  11854. controller - A configuration/controller object passed to the visualization.
  11855. */
  11856. placeLabel: function(tag, node, controller){
  11857. var pos = node.pos.getc(true), canvas = this.viz.canvas;
  11858. var radius = canvas.getSize();
  11859. var labelPos = {
  11860. x: Math.round(pos.x + radius.width / 2),
  11861. y: Math.round(pos.y + radius.height / 2)
  11862. };
  11863. tag.setAttribute('x', labelPos.x);
  11864. tag.setAttribute('y', labelPos.y);
  11865. controller.onPlaceLabel(tag, node);
  11866. }
  11867. });
  11868. /*
  11869. Icicle.Label.HTML
  11870. Custom extension of <Graph.Label.HTML>.
  11871. Extends:
  11872. All <Graph.Label.HTML> methods.
  11873. See also:
  11874. <Graph.Label.HTML>
  11875. */
  11876. $jit.Icicle.Label.HTML = new Class( {
  11877. Implements: Graph.Label.HTML,
  11878. initialize: function(viz){
  11879. this.viz = viz;
  11880. },
  11881. /*
  11882. placeLabel
  11883. Overrides abstract method placeLabel in <Graph.Plot>.
  11884. Parameters:
  11885. tag - A DOM label element.
  11886. node - A <Graph.Node>.
  11887. controller - A configuration/controller object passed to the visualization.
  11888. */
  11889. placeLabel: function(tag, node, controller){
  11890. var pos = node.pos.getc(true), canvas = this.viz.canvas;
  11891. var radius = canvas.getSize();
  11892. var labelPos = {
  11893. x: Math.round(pos.x + radius.width / 2),
  11894. y: Math.round(pos.y + radius.height / 2)
  11895. };
  11896. var style = tag.style;
  11897. style.left = labelPos.x + 'px';
  11898. style.top = labelPos.y + 'px';
  11899. style.display = '';
  11900. controller.onPlaceLabel(tag, node);
  11901. }
  11902. });
  11903. /*
  11904. Class: Icicle.Plot.NodeTypes
  11905. This class contains a list of <Graph.Node> built-in types.
  11906. Node types implemented are 'none', 'rectangle'.
  11907. You can add your custom node types, customizing your visualization to the extreme.
  11908. Example:
  11909. (start code js)
  11910. Icicle.Plot.NodeTypes.implement({
  11911. 'mySpecialType': {
  11912. 'render': function(node, canvas) {
  11913. //print your custom node to canvas
  11914. },
  11915. //optional
  11916. 'contains': function(node, pos) {
  11917. //return true if pos is inside the node or false otherwise
  11918. }
  11919. }
  11920. });
  11921. (end code)
  11922. */
  11923. $jit.Icicle.Plot.NodeTypes = new Class( {
  11924. 'none': {
  11925. 'render': $.empty
  11926. },
  11927. 'rectangle': {
  11928. 'render': function(node, canvas, animating) {
  11929. var config = this.viz.config;
  11930. var offset = config.offset;
  11931. var width = node.getData('width');
  11932. var height = node.getData('height');
  11933. var border = node.getData('border');
  11934. var pos = node.pos.getc(true);
  11935. var posx = pos.x + offset / 2, posy = pos.y + offset / 2;
  11936. var ctx = canvas.getCtx();
  11937. if(width - offset < 2 || height - offset < 2) return;
  11938. if(config.cushion) {
  11939. var color = node.getData('color');
  11940. var lg = ctx.createRadialGradient(posx + (width - offset)/2,
  11941. posy + (height - offset)/2, 1,
  11942. posx + (width-offset)/2, posy + (height-offset)/2,
  11943. width < height? height : width);
  11944. var colorGrad = $.rgbToHex($.map($.hexToRgb(color),
  11945. function(r) { return r * 0.3 >> 0; }));
  11946. lg.addColorStop(0, color);
  11947. lg.addColorStop(1, colorGrad);
  11948. ctx.fillStyle = lg;
  11949. }
  11950. if (border) {
  11951. ctx.strokeStyle = border;
  11952. ctx.lineWidth = 3;
  11953. }
  11954. ctx.fillRect(posx, posy, Math.max(0, width - offset), Math.max(0, height - offset));
  11955. border && ctx.strokeRect(pos.x, pos.y, width, height);
  11956. },
  11957. 'contains': function(node, pos) {
  11958. if(this.viz.clickedNode && !$jit.Graph.Util.isDescendantOf(node, this.viz.clickedNode.id)) return false;
  11959. var npos = node.pos.getc(true),
  11960. width = node.getData('width'),
  11961. height = node.getData('height');
  11962. return this.nodeHelper.rectangle.contains({x: npos.x + width/2, y: npos.y + height/2}, pos, width, height);
  11963. }
  11964. }
  11965. });
  11966. $jit.Icicle.Plot.EdgeTypes = new Class( {
  11967. 'none': $.empty
  11968. });
  11969. /*
  11970. * File: Layouts.ForceDirected.js
  11971. *
  11972. */
  11973. /*
  11974. * Class: Layouts.ForceDirected
  11975. *
  11976. * Implements a Force Directed Layout.
  11977. *
  11978. * Implemented By:
  11979. *
  11980. * <ForceDirected>
  11981. *
  11982. * Credits:
  11983. *
  11984. * Marcus Cobden <http://marcuscobden.co.uk>
  11985. *
  11986. */
  11987. Layouts.ForceDirected = new Class({
  11988. getOptions: function(random) {
  11989. var s = this.canvas.getSize();
  11990. var w = s.width, h = s.height;
  11991. //count nodes
  11992. var count = 0;
  11993. this.graph.eachNode(function(n) {
  11994. count++;
  11995. });
  11996. var k2 = w * h / count, k = Math.sqrt(k2);
  11997. var l = this.config.levelDistance;
  11998. return {
  11999. width: w,
  12000. height: h,
  12001. tstart: w * 0.1,
  12002. nodef: function(x) { return k2 / (x || 1); },
  12003. edgef: function(x) { return /* x * x / k; */ k * (x - l); }
  12004. };
  12005. },
  12006. compute: function(property, incremental) {
  12007. var prop = $.splat(property || ['current', 'start', 'end']);
  12008. var opt = this.getOptions();
  12009. NodeDim.compute(this.graph, prop, this.config);
  12010. this.graph.computeLevels(this.root, 0, "ignore");
  12011. this.graph.eachNode(function(n) {
  12012. $.each(prop, function(p) {
  12013. var pos = n.getPos(p);
  12014. if(pos.equals(Complex.KER)) {
  12015. pos.x = opt.width/5 * (Math.random() - 0.5);
  12016. pos.y = opt.height/5 * (Math.random() - 0.5);
  12017. }
  12018. //initialize disp vector
  12019. n.disp = {};
  12020. $.each(prop, function(p) {
  12021. n.disp[p] = $C(0, 0);
  12022. });
  12023. });
  12024. });
  12025. this.computePositions(prop, opt, incremental);
  12026. },
  12027. computePositions: function(property, opt, incremental) {
  12028. var times = this.config.iterations, i = 0, that = this;
  12029. if(incremental) {
  12030. (function iter() {
  12031. for(var total=incremental.iter, j=0; j<total; j++) {
  12032. opt.t = opt.tstart;
  12033. if(times) opt.t *= (1 - i++/(times -1));
  12034. that.computePositionStep(property, opt);
  12035. if(times && i >= times) {
  12036. incremental.onComplete();
  12037. return;
  12038. }
  12039. }
  12040. incremental.onStep(Math.round(i / (times -1) * 100));
  12041. setTimeout(iter, 1);
  12042. })();
  12043. } else {
  12044. for(; i < times; i++) {
  12045. opt.t = opt.tstart * (1 - i/(times -1));
  12046. this.computePositionStep(property, opt);
  12047. }
  12048. }
  12049. },
  12050. computePositionStep: function(property, opt) {
  12051. var graph = this.graph;
  12052. var min = Math.min, max = Math.max;
  12053. var dpos = $C(0, 0);
  12054. //calculate repulsive forces
  12055. graph.eachNode(function(v) {
  12056. //initialize disp
  12057. $.each(property, function(p) {
  12058. v.disp[p].x = 0; v.disp[p].y = 0;
  12059. });
  12060. graph.eachNode(function(u) {
  12061. if(u.id != v.id) {
  12062. $.each(property, function(p) {
  12063. var vp = v.getPos(p), up = u.getPos(p);
  12064. dpos.x = vp.x - up.x;
  12065. dpos.y = vp.y - up.y;
  12066. var norm = dpos.norm() || 1;
  12067. v.disp[p].$add(dpos
  12068. .$scale(opt.nodef(norm) / norm));
  12069. });
  12070. }
  12071. });
  12072. });
  12073. //calculate attractive forces
  12074. var T = !!graph.getNode(this.root).visited;
  12075. graph.eachNode(function(node) {
  12076. node.eachAdjacency(function(adj) {
  12077. var nodeTo = adj.nodeTo;
  12078. if(!!nodeTo.visited === T) {
  12079. $.each(property, function(p) {
  12080. var vp = node.getPos(p), up = nodeTo.getPos(p);
  12081. dpos.x = vp.x - up.x;
  12082. dpos.y = vp.y - up.y;
  12083. var norm = dpos.norm() || 1;
  12084. node.disp[p].$add(dpos.$scale(-opt.edgef(norm) / norm));
  12085. nodeTo.disp[p].$add(dpos.$scale(-1));
  12086. });
  12087. }
  12088. });
  12089. node.visited = !T;
  12090. });
  12091. //arrange positions to fit the canvas
  12092. var t = opt.t, w2 = opt.width / 2, h2 = opt.height / 2;
  12093. graph.eachNode(function(u) {
  12094. $.each(property, function(p) {
  12095. var disp = u.disp[p];
  12096. var norm = disp.norm() || 1;
  12097. var p = u.getPos(p);
  12098. p.$add($C(disp.x * min(Math.abs(disp.x), t) / norm,
  12099. disp.y * min(Math.abs(disp.y), t) / norm));
  12100. p.x = min(w2, max(-w2, p.x));
  12101. p.y = min(h2, max(-h2, p.y));
  12102. });
  12103. });
  12104. }
  12105. });
  12106. /*
  12107. * File: ForceDirected.js
  12108. */
  12109. /*
  12110. Class: ForceDirected
  12111. A visualization that lays graphs using a Force-Directed layout algorithm.
  12112. Inspired by:
  12113. Force-Directed Drawing Algorithms (Stephen G. Kobourov) <http://www.cs.brown.edu/~rt/gdhandbook/chapters/force-directed.pdf>
  12114. Implements:
  12115. All <Loader> methods
  12116. Constructor Options:
  12117. Inherits options from
  12118. - <Options.Canvas>
  12119. - <Options.Controller>
  12120. - <Options.Node>
  12121. - <Options.Edge>
  12122. - <Options.Label>
  12123. - <Options.Events>
  12124. - <Options.Tips>
  12125. - <Options.NodeStyles>
  12126. - <Options.Navigation>
  12127. Additionally, there are two parameters
  12128. levelDistance - (number) Default's *50*. The natural length desired for the edges.
  12129. iterations - (number) Default's *50*. The number of iterations for the spring layout simulation. Depending on the browser's speed you could set this to a more 'interesting' number, like *200*.
  12130. Instance Properties:
  12131. canvas - Access a <Canvas> instance.
  12132. graph - Access a <Graph> instance.
  12133. op - Access a <ForceDirected.Op> instance.
  12134. fx - Access a <ForceDirected.Plot> instance.
  12135. labels - Access a <ForceDirected.Label> interface implementation.
  12136. */
  12137. $jit.ForceDirected = new Class( {
  12138. Implements: [ Loader, Extras, Layouts.ForceDirected ],
  12139. initialize: function(controller) {
  12140. var $ForceDirected = $jit.ForceDirected;
  12141. var config = {
  12142. iterations: 50,
  12143. levelDistance: 50
  12144. };
  12145. this.controller = this.config = $.merge(Options("Canvas", "Node", "Edge",
  12146. "Fx", "Tips", "NodeStyles", "Events", "Navigation", "Controller", "Label"), config, controller);
  12147. var canvasConfig = this.config;
  12148. if(canvasConfig.useCanvas) {
  12149. this.canvas = canvasConfig.useCanvas;
  12150. this.config.labelContainer = this.canvas.id + '-label';
  12151. } else {
  12152. if(canvasConfig.background) {
  12153. canvasConfig.background = $.merge({
  12154. type: 'Circles'
  12155. }, canvasConfig.background);
  12156. }
  12157. this.canvas = new Canvas(this, canvasConfig);
  12158. this.config.labelContainer = (typeof canvasConfig.injectInto == 'string'? canvasConfig.injectInto : canvasConfig.injectInto.id) + '-label';
  12159. }
  12160. this.graphOptions = {
  12161. 'klass': Complex,
  12162. 'Node': {
  12163. 'selected': false,
  12164. 'exist': true,
  12165. 'drawn': true
  12166. }
  12167. };
  12168. this.graph = new Graph(this.graphOptions, this.config.Node,
  12169. this.config.Edge);
  12170. this.labels = new $ForceDirected.Label[canvasConfig.Label.type](this);
  12171. this.fx = new $ForceDirected.Plot(this, $ForceDirected);
  12172. this.op = new $ForceDirected.Op(this);
  12173. this.json = null;
  12174. this.busy = false;
  12175. // initialize extras
  12176. this.initializeExtras();
  12177. },
  12178. /*
  12179. Method: refresh
  12180. Computes positions and plots the tree.
  12181. */
  12182. refresh: function() {
  12183. this.compute();
  12184. this.plot();
  12185. },
  12186. reposition: function() {
  12187. this.compute('end');
  12188. },
  12189. /*
  12190. Method: computeIncremental
  12191. Performs the Force Directed algorithm incrementally.
  12192. Description:
  12193. ForceDirected algorithms can perform many computations and lead to JavaScript taking too much time to complete.
  12194. This method splits the algorithm into smaller parts allowing the user to track the evolution of the algorithm and
  12195. avoiding browser messages such as "This script is taking too long to complete".
  12196. Parameters:
  12197. opt - (object) The object properties are described below
  12198. iter - (number) Default's *20*. Split the algorithm into pieces of _iter_ iterations. For example, if the _iterations_ configuration property
  12199. of your <ForceDirected> class is 100, then you could set _iter_ to 20 to split the main algorithm into 5 smaller pieces.
  12200. property - (string) Default's *end*. Whether to update starting, current or ending node positions. Possible values are 'end', 'start', 'current'.
  12201. You can also set an array of these properties. If you'd like to keep the current node positions but to perform these
  12202. computations for final animation positions then you can just choose 'end'.
  12203. onStep - (function) A callback function called when each "small part" of the algorithm completed. This function gets as first formal
  12204. parameter a percentage value.
  12205. onComplete - A callback function called when the algorithm completed.
  12206. Example:
  12207. In this example I calculate the end positions and then animate the graph to those positions
  12208. (start code js)
  12209. var fd = new $jit.ForceDirected(...);
  12210. fd.computeIncremental({
  12211. iter: 20,
  12212. property: 'end',
  12213. onStep: function(perc) {
  12214. Log.write("loading " + perc + "%");
  12215. },
  12216. onComplete: function() {
  12217. Log.write("done");
  12218. fd.animate();
  12219. }
  12220. });
  12221. (end code)
  12222. In this example I calculate all positions and (re)plot the graph
  12223. (start code js)
  12224. var fd = new ForceDirected(...);
  12225. fd.computeIncremental({
  12226. iter: 20,
  12227. property: ['end', 'start', 'current'],
  12228. onStep: function(perc) {
  12229. Log.write("loading " + perc + "%");
  12230. },
  12231. onComplete: function() {
  12232. Log.write("done");
  12233. fd.plot();
  12234. }
  12235. });
  12236. (end code)
  12237. */
  12238. computeIncremental: function(opt) {
  12239. opt = $.merge( {
  12240. iter: 20,
  12241. property: 'end',
  12242. onStep: $.empty,
  12243. onComplete: $.empty
  12244. }, opt || {});
  12245. this.config.onBeforeCompute(this.graph.getNode(this.root));
  12246. this.compute(opt.property, opt);
  12247. },
  12248. /*
  12249. Method: plot
  12250. Plots the ForceDirected graph. This is a shortcut to *fx.plot*.
  12251. */
  12252. plot: function() {
  12253. this.fx.plot();
  12254. },
  12255. /*
  12256. Method: animate
  12257. Animates the graph from the current positions to the 'end' node positions.
  12258. */
  12259. animate: function(opt) {
  12260. this.fx.animate($.merge( {
  12261. modes: [ 'linear' ]
  12262. }, opt || {}));
  12263. }
  12264. });
  12265. $jit.ForceDirected.$extend = true;
  12266. (function(ForceDirected) {
  12267. /*
  12268. Class: ForceDirected.Op
  12269. Custom extension of <Graph.Op>.
  12270. Extends:
  12271. All <Graph.Op> methods
  12272. See also:
  12273. <Graph.Op>
  12274. */
  12275. ForceDirected.Op = new Class( {
  12276. Implements: Graph.Op
  12277. });
  12278. /*
  12279. Class: ForceDirected.Plot
  12280. Custom extension of <Graph.Plot>.
  12281. Extends:
  12282. All <Graph.Plot> methods
  12283. See also:
  12284. <Graph.Plot>
  12285. */
  12286. ForceDirected.Plot = new Class( {
  12287. Implements: Graph.Plot
  12288. });
  12289. /*
  12290. Class: ForceDirected.Label
  12291. Custom extension of <Graph.Label>.
  12292. Contains custom <Graph.Label.SVG>, <Graph.Label.HTML> and <Graph.Label.Native> extensions.
  12293. Extends:
  12294. All <Graph.Label> methods and subclasses.
  12295. See also:
  12296. <Graph.Label>, <Graph.Label.Native>, <Graph.Label.HTML>, <Graph.Label.SVG>.
  12297. */
  12298. ForceDirected.Label = {};
  12299. /*
  12300. ForceDirected.Label.Native
  12301. Custom extension of <Graph.Label.Native>.
  12302. Extends:
  12303. All <Graph.Label.Native> methods
  12304. See also:
  12305. <Graph.Label.Native>
  12306. */
  12307. ForceDirected.Label.Native = new Class( {
  12308. Implements: Graph.Label.Native
  12309. });
  12310. /*
  12311. ForceDirected.Label.SVG
  12312. Custom extension of <Graph.Label.SVG>.
  12313. Extends:
  12314. All <Graph.Label.SVG> methods
  12315. See also:
  12316. <Graph.Label.SVG>
  12317. */
  12318. ForceDirected.Label.SVG = new Class( {
  12319. Implements: Graph.Label.SVG,
  12320. initialize: function(viz) {
  12321. this.viz = viz;
  12322. },
  12323. /*
  12324. placeLabel
  12325. Overrides abstract method placeLabel in <Graph.Label>.
  12326. Parameters:
  12327. tag - A DOM label element.
  12328. node - A <Graph.Node>.
  12329. controller - A configuration/controller object passed to the visualization.
  12330. */
  12331. placeLabel: function(tag, node, controller) {
  12332. var pos = node.pos.getc(true),
  12333. canvas = this.viz.canvas,
  12334. ox = canvas.translateOffsetX,
  12335. oy = canvas.translateOffsetY,
  12336. sx = canvas.scaleOffsetX,
  12337. sy = canvas.scaleOffsetY,
  12338. radius = canvas.getSize();
  12339. var labelPos = {
  12340. x: Math.round(pos.x * sx + ox + radius.width / 2),
  12341. y: Math.round(pos.y * sy + oy + radius.height / 2)
  12342. };
  12343. tag.setAttribute('x', labelPos.x);
  12344. tag.setAttribute('y', labelPos.y);
  12345. controller.onPlaceLabel(tag, node);
  12346. }
  12347. });
  12348. /*
  12349. ForceDirected.Label.HTML
  12350. Custom extension of <Graph.Label.HTML>.
  12351. Extends:
  12352. All <Graph.Label.HTML> methods.
  12353. See also:
  12354. <Graph.Label.HTML>
  12355. */
  12356. ForceDirected.Label.HTML = new Class( {
  12357. Implements: Graph.Label.HTML,
  12358. initialize: function(viz) {
  12359. this.viz = viz;
  12360. },
  12361. /*
  12362. placeLabel
  12363. Overrides abstract method placeLabel in <Graph.Plot>.
  12364. Parameters:
  12365. tag - A DOM label element.
  12366. node - A <Graph.Node>.
  12367. controller - A configuration/controller object passed to the visualization.
  12368. */
  12369. placeLabel: function(tag, node, controller) {
  12370. var pos = node.pos.getc(true),
  12371. canvas = this.viz.canvas,
  12372. ox = canvas.translateOffsetX,
  12373. oy = canvas.translateOffsetY,
  12374. sx = canvas.scaleOffsetX,
  12375. sy = canvas.scaleOffsetY,
  12376. radius = canvas.getSize();
  12377. var labelPos = {
  12378. x: Math.round(pos.x * sx + ox + radius.width / 2),
  12379. y: Math.round(pos.y * sy + oy + radius.height / 2)
  12380. };
  12381. var style = tag.style;
  12382. style.left = labelPos.x + 'px';
  12383. style.top = labelPos.y + 'px';
  12384. style.display = this.fitsInCanvas(labelPos, canvas) ? '' : 'none';
  12385. controller.onPlaceLabel(tag, node);
  12386. }
  12387. });
  12388. /*
  12389. Class: ForceDirected.Plot.NodeTypes
  12390. This class contains a list of <Graph.Node> built-in types.
  12391. Node types implemented are 'none', 'circle', 'triangle', 'rectangle', 'star', 'ellipse' and 'square'.
  12392. You can add your custom node types, customizing your visualization to the extreme.
  12393. Example:
  12394. (start code js)
  12395. ForceDirected.Plot.NodeTypes.implement({
  12396. 'mySpecialType': {
  12397. 'render': function(node, canvas) {
  12398. //print your custom node to canvas
  12399. },
  12400. //optional
  12401. 'contains': function(node, pos) {
  12402. //return true if pos is inside the node or false otherwise
  12403. }
  12404. }
  12405. });
  12406. (end code)
  12407. */
  12408. ForceDirected.Plot.NodeTypes = new Class({
  12409. 'none': {
  12410. 'render': $.empty,
  12411. 'contains': $.lambda(false)
  12412. },
  12413. 'circle': {
  12414. 'render': function(node, canvas){
  12415. var pos = node.pos.getc(true),
  12416. dim = node.getData('dim');
  12417. this.nodeHelper.circle.render('fill', pos, dim, canvas);
  12418. },
  12419. 'contains': function(node, pos){
  12420. var npos = node.pos.getc(true),
  12421. dim = node.getData('dim');
  12422. return this.nodeHelper.circle.contains(npos, pos, dim);
  12423. }
  12424. },
  12425. 'ellipse': {
  12426. 'render': function(node, canvas){
  12427. var pos = node.pos.getc(true),
  12428. width = node.getData('width'),
  12429. height = node.getData('height');
  12430. this.nodeHelper.ellipse.render('fill', pos, width, height, canvas);
  12431. },
  12432. 'contains': function(node, pos){
  12433. var npos = node.pos.getc(true),
  12434. width = node.getData('width'),
  12435. height = node.getData('height');
  12436. return this.nodeHelper.ellipse.contains(npos, pos, width, height);
  12437. }
  12438. },
  12439. 'square': {
  12440. 'render': function(node, canvas){
  12441. var pos = node.pos.getc(true),
  12442. dim = node.getData('dim');
  12443. this.nodeHelper.square.render('fill', pos, dim, canvas);
  12444. },
  12445. 'contains': function(node, pos){
  12446. var npos = node.pos.getc(true),
  12447. dim = node.getData('dim');
  12448. return this.nodeHelper.square.contains(npos, pos, dim);
  12449. }
  12450. },
  12451. 'rectangle': {
  12452. 'render': function(node, canvas){
  12453. var pos = node.pos.getc(true),
  12454. width = node.getData('width'),
  12455. height = node.getData('height');
  12456. this.nodeHelper.rectangle.render('fill', pos, width, height, canvas);
  12457. },
  12458. 'contains': function(node, pos){
  12459. var npos = node.pos.getc(true),
  12460. width = node.getData('width'),
  12461. height = node.getData('height');
  12462. return this.nodeHelper.rectangle.contains(npos, pos, width, height);
  12463. }
  12464. },
  12465. 'triangle': {
  12466. 'render': function(node, canvas){
  12467. var pos = node.pos.getc(true),
  12468. dim = node.getData('dim');
  12469. this.nodeHelper.triangle.render('fill', pos, dim, canvas);
  12470. },
  12471. 'contains': function(node, pos) {
  12472. var npos = node.pos.getc(true),
  12473. dim = node.getData('dim');
  12474. return this.nodeHelper.triangle.contains(npos, pos, dim);
  12475. }
  12476. },
  12477. 'star': {
  12478. 'render': function(node, canvas){
  12479. var pos = node.pos.getc(true),
  12480. dim = node.getData('dim');
  12481. this.nodeHelper.star.render('fill', pos, dim, canvas);
  12482. },
  12483. 'contains': function(node, pos) {
  12484. var npos = node.pos.getc(true),
  12485. dim = node.getData('dim');
  12486. return this.nodeHelper.star.contains(npos, pos, dim);
  12487. }
  12488. }
  12489. });
  12490. /*
  12491. Class: ForceDirected.Plot.EdgeTypes
  12492. This class contains a list of <Graph.Adjacence> built-in types.
  12493. Edge types implemented are 'none', 'line' and 'arrow'.
  12494. You can add your custom edge types, customizing your visualization to the extreme.
  12495. Example:
  12496. (start code js)
  12497. ForceDirected.Plot.EdgeTypes.implement({
  12498. 'mySpecialType': {
  12499. 'render': function(adj, canvas) {
  12500. //print your custom edge to canvas
  12501. },
  12502. //optional
  12503. 'contains': function(adj, pos) {
  12504. //return true if pos is inside the arc or false otherwise
  12505. }
  12506. }
  12507. });
  12508. (end code)
  12509. */
  12510. ForceDirected.Plot.EdgeTypes = new Class({
  12511. 'none': $.empty,
  12512. 'line': {
  12513. 'render': function(adj, canvas) {
  12514. var from = adj.nodeFrom.pos.getc(true),
  12515. to = adj.nodeTo.pos.getc(true);
  12516. this.edgeHelper.line.render(from, to, canvas);
  12517. },
  12518. 'contains': function(adj, pos) {
  12519. var from = adj.nodeFrom.pos.getc(true),
  12520. to = adj.nodeTo.pos.getc(true);
  12521. return this.edgeHelper.line.contains(from, to, pos, this.edge.epsilon);
  12522. }
  12523. },
  12524. 'arrow': {
  12525. 'render': function(adj, canvas) {
  12526. var from = adj.nodeFrom.pos.getc(true),
  12527. to = adj.nodeTo.pos.getc(true),
  12528. dim = adj.getData('dim'),
  12529. direction = adj.data.$direction,
  12530. inv = (direction && direction.length>1 && direction[0] != adj.nodeFrom.id);
  12531. this.edgeHelper.arrow.render(from, to, dim, inv, canvas);
  12532. },
  12533. 'contains': function(adj, pos) {
  12534. var from = adj.nodeFrom.pos.getc(true),
  12535. to = adj.nodeTo.pos.getc(true);
  12536. return this.edgeHelper.arrow.contains(from, to, pos, this.edge.epsilon);
  12537. }
  12538. }
  12539. });
  12540. })($jit.ForceDirected);
  12541. /*
  12542. * File: Treemap.js
  12543. *
  12544. */
  12545. $jit.TM = {};
  12546. var TM = $jit.TM;
  12547. $jit.TM.$extend = true;
  12548. /*
  12549. Class: TM.Base
  12550. Abstract class providing base functionality for <TM.Squarified>, <TM.Strip> and <TM.SliceAndDice> visualizations.
  12551. Implements:
  12552. All <Loader> methods
  12553. Constructor Options:
  12554. Inherits options from
  12555. - <Options.Canvas>
  12556. - <Options.Controller>
  12557. - <Options.Node>
  12558. - <Options.Edge>
  12559. - <Options.Label>
  12560. - <Options.Events>
  12561. - <Options.Tips>
  12562. - <Options.NodeStyles>
  12563. - <Options.Navigation>
  12564. Additionally, there are other parameters and some default values changed
  12565. orientation - (string) Default's *h*. Whether to set horizontal or vertical layouts. Possible values are 'h' and 'v'.
  12566. titleHeight - (number) Default's *13*. The height of the title rectangle for inner (non-leaf) nodes.
  12567. offset - (number) Default's *2*. Boxes offset.
  12568. constrained - (boolean) Default's *false*. Whether to show the entire tree when loaded or just the number of levels specified by _levelsToShow_.
  12569. levelsToShow - (number) Default's *3*. The number of levels to show for a subtree. This number is relative to the selected node.
  12570. animate - (boolean) Default's *false*. Whether to animate transitions.
  12571. Node.type - Described in <Options.Node>. Default's *rectangle*.
  12572. duration - Described in <Options.Fx>. Default's *700*.
  12573. fps - Described in <Options.Fx>. Default's *45*.
  12574. Instance Properties:
  12575. canvas - Access a <Canvas> instance.
  12576. graph - Access a <Graph> instance.
  12577. op - Access a <TM.Op> instance.
  12578. fx - Access a <TM.Plot> instance.
  12579. labels - Access a <TM.Label> interface implementation.
  12580. Inspired by:
  12581. Squarified Treemaps (Mark Bruls, Kees Huizing, and Jarke J. van Wijk) <http://www.win.tue.nl/~vanwijk/stm.pdf>
  12582. Tree visualization with tree-maps: 2-d space-filling approach (Ben Shneiderman) <http://hcil.cs.umd.edu/trs/91-03/91-03.html>
  12583. Note:
  12584. This visualization was built and engineered from scratch, taking only the paper as inspiration, and only shares some features with the visualization described in the paper.
  12585. */
  12586. TM.Base = {
  12587. layout: {
  12588. orientation: "h",
  12589. vertical: function(){
  12590. return this.orientation == "v";
  12591. },
  12592. horizontal: function(){
  12593. return this.orientation == "h";
  12594. },
  12595. change: function(){
  12596. this.orientation = this.vertical()? "h" : "v";
  12597. }
  12598. },
  12599. initialize: function(controller){
  12600. var config = {
  12601. orientation: "h",
  12602. titleHeight: 13,
  12603. offset: 2,
  12604. levelsToShow: 0,
  12605. constrained: false,
  12606. animate: false,
  12607. Node: {
  12608. type: 'rectangle',
  12609. overridable: true,
  12610. //we all know why this is not zero,
  12611. //right, Firefox?
  12612. width: 3,
  12613. height: 3,
  12614. color: '#444'
  12615. },
  12616. Label: {
  12617. textAlign: 'center',
  12618. textBaseline: 'top'
  12619. },
  12620. Edge: {
  12621. type: 'none'
  12622. },
  12623. duration: 700,
  12624. fps: 45
  12625. };
  12626. this.controller = this.config = $.merge(Options("Canvas", "Node", "Edge",
  12627. "Fx", "Controller", "Tips", "NodeStyles", "Events", "Navigation", "Label"), config, controller);
  12628. this.layout.orientation = this.config.orientation;
  12629. var canvasConfig = this.config;
  12630. if (canvasConfig.useCanvas) {
  12631. this.canvas = canvasConfig.useCanvas;
  12632. this.config.labelContainer = this.canvas.id + '-label';
  12633. } else {
  12634. if(canvasConfig.background) {
  12635. canvasConfig.background = $.merge({
  12636. type: 'Circles'
  12637. }, canvasConfig.background);
  12638. }
  12639. this.canvas = new Canvas(this, canvasConfig);
  12640. this.config.labelContainer = (typeof canvasConfig.injectInto == 'string'? canvasConfig.injectInto : canvasConfig.injectInto.id) + '-label';
  12641. }
  12642. this.graphOptions = {
  12643. 'klass': Complex,
  12644. 'Node': {
  12645. 'selected': false,
  12646. 'exist': true,
  12647. 'drawn': true
  12648. }
  12649. };
  12650. this.graph = new Graph(this.graphOptions, this.config.Node,
  12651. this.config.Edge);
  12652. this.labels = new TM.Label[canvasConfig.Label.type](this);
  12653. this.fx = new TM.Plot(this);
  12654. this.op = new TM.Op(this);
  12655. this.group = new TM.Group(this);
  12656. this.geom = new TM.Geom(this);
  12657. this.clickedNode = null;
  12658. this.busy = false;
  12659. // initialize extras
  12660. this.initializeExtras();
  12661. },
  12662. /*
  12663. Method: refresh
  12664. Computes positions and plots the tree.
  12665. */
  12666. refresh: function(){
  12667. if(this.busy) return;
  12668. this.busy = true;
  12669. var that = this;
  12670. if(this.config.animate) {
  12671. this.compute('end');
  12672. this.config.levelsToShow > 0 && this.geom.setRightLevelToShow(this.graph.getNode(this.clickedNode
  12673. && this.clickedNode.id || this.root));
  12674. this.fx.animate($.merge(this.config, {
  12675. modes: ['linear', 'node-property:width:height'],
  12676. onComplete: function() {
  12677. that.busy = false;
  12678. }
  12679. }));
  12680. } else {
  12681. var labelType = this.config.Label.type;
  12682. if(labelType != 'Native') {
  12683. var that = this;
  12684. this.graph.eachNode(function(n) { that.labels.hideLabel(n, false); });
  12685. }
  12686. this.busy = false;
  12687. this.compute();
  12688. this.config.levelsToShow > 0 && this.geom.setRightLevelToShow(this.graph.getNode(this.clickedNode
  12689. && this.clickedNode.id || this.root));
  12690. this.plot();
  12691. }
  12692. },
  12693. /*
  12694. Method: plot
  12695. Plots the TreeMap. This is a shortcut to *fx.plot*.
  12696. */
  12697. plot: function(){
  12698. this.fx.plot();
  12699. },
  12700. /*
  12701. Method: leaf
  12702. Returns whether the node is a leaf.
  12703. Parameters:
  12704. n - (object) A <Graph.Node>.
  12705. */
  12706. leaf: function(n){
  12707. return n.getSubnodes([
  12708. 1, 1
  12709. ], "ignore").length == 0;
  12710. },
  12711. /*
  12712. Method: enter
  12713. Sets the node as root.
  12714. Parameters:
  12715. n - (object) A <Graph.Node>.
  12716. */
  12717. enter: function(n){
  12718. if(this.busy) return;
  12719. this.busy = true;
  12720. var that = this,
  12721. config = this.config,
  12722. graph = this.graph,
  12723. clickedNode = n,
  12724. previousClickedNode = this.clickedNode;
  12725. var callback = {
  12726. onComplete: function() {
  12727. //ensure that nodes are shown for that level
  12728. if(config.levelsToShow > 0) {
  12729. that.geom.setRightLevelToShow(n);
  12730. }
  12731. //compute positions of newly inserted nodes
  12732. if(config.levelsToShow > 0 || config.request) that.compute();
  12733. if(config.animate) {
  12734. //fade nodes
  12735. graph.nodeList.setData('alpha', 0, 'end');
  12736. n.eachSubgraph(function(n) {
  12737. n.setData('alpha', 1, 'end');
  12738. }, "ignore");
  12739. that.fx.animate({
  12740. duration: 500,
  12741. modes:['node-property:alpha'],
  12742. onComplete: function() {
  12743. //compute end positions
  12744. that.clickedNode = clickedNode;
  12745. that.compute('end');
  12746. //animate positions
  12747. //TODO(nico) commenting this line didn't seem to throw errors...
  12748. that.clickedNode = previousClickedNode;
  12749. that.fx.animate({
  12750. modes:['linear', 'node-property:width:height'],
  12751. duration: 1000,
  12752. onComplete: function() {
  12753. that.busy = false;
  12754. //TODO(nico) check comment above
  12755. that.clickedNode = clickedNode;
  12756. }
  12757. });
  12758. }
  12759. });
  12760. } else {
  12761. that.busy = false;
  12762. that.clickedNode = n;
  12763. that.refresh();
  12764. }
  12765. }
  12766. };
  12767. if(config.request) {
  12768. this.requestNodes(clickedNode, callback);
  12769. } else {
  12770. callback.onComplete();
  12771. }
  12772. },
  12773. /*
  12774. Method: out
  12775. Sets the parent node of the current selected node as root.
  12776. */
  12777. out: function(){
  12778. if(this.busy) return;
  12779. this.busy = true;
  12780. this.events.hoveredNode = false;
  12781. var that = this,
  12782. config = this.config,
  12783. graph = this.graph,
  12784. parents = graph.getNode(this.clickedNode
  12785. && this.clickedNode.id || this.root).getParents(),
  12786. parent = parents[0],
  12787. clickedNode = parent,
  12788. previousClickedNode = this.clickedNode;
  12789. //if no parents return
  12790. if(!parent) {
  12791. this.busy = false;
  12792. return;
  12793. }
  12794. //final plot callback
  12795. callback = {
  12796. onComplete: function() {
  12797. that.clickedNode = parent;
  12798. if(config.request) {
  12799. that.requestNodes(parent, {
  12800. onComplete: function() {
  12801. that.compute();
  12802. that.plot();
  12803. that.busy = false;
  12804. }
  12805. });
  12806. } else {
  12807. that.compute();
  12808. that.plot();
  12809. that.busy = false;
  12810. }
  12811. }
  12812. };
  12813. //prune tree
  12814. if (config.levelsToShow > 0)
  12815. this.geom.setRightLevelToShow(parent);
  12816. //animate node positions
  12817. if(config.animate) {
  12818. this.clickedNode = clickedNode;
  12819. this.compute('end');
  12820. //animate the visible subtree only
  12821. this.clickedNode = previousClickedNode;
  12822. this.fx.animate({
  12823. modes:['linear', 'node-property:width:height'],
  12824. duration: 1000,
  12825. onComplete: function() {
  12826. //animate the parent subtree
  12827. that.clickedNode = clickedNode;
  12828. //change nodes alpha
  12829. graph.eachNode(function(n) {
  12830. n.setDataset(['current', 'end'], {
  12831. 'alpha': [0, 1]
  12832. });
  12833. }, "ignore");
  12834. previousClickedNode.eachSubgraph(function(node) {
  12835. node.setData('alpha', 1);
  12836. }, "ignore");
  12837. that.fx.animate({
  12838. duration: 500,
  12839. modes:['node-property:alpha'],
  12840. onComplete: function() {
  12841. callback.onComplete();
  12842. }
  12843. });
  12844. }
  12845. });
  12846. } else {
  12847. callback.onComplete();
  12848. }
  12849. },
  12850. requestNodes: function(node, onComplete){
  12851. var handler = $.merge(this.controller, onComplete),
  12852. lev = this.config.levelsToShow;
  12853. if (handler.request) {
  12854. var leaves = [], d = node._depth;
  12855. node.eachLevel(0, lev, function(n){
  12856. var nodeLevel = lev - (n._depth - d);
  12857. if (n.drawn && !n.anySubnode() && nodeLevel > 0) {
  12858. leaves.push(n);
  12859. n._level = nodeLevel;
  12860. }
  12861. });
  12862. this.group.requestNodes(leaves, handler);
  12863. } else {
  12864. handler.onComplete();
  12865. }
  12866. },
  12867. reposition: function() {
  12868. this.compute('end');
  12869. }
  12870. };
  12871. /*
  12872. Class: TM.Op
  12873. Custom extension of <Graph.Op>.
  12874. Extends:
  12875. All <Graph.Op> methods
  12876. See also:
  12877. <Graph.Op>
  12878. */
  12879. TM.Op = new Class({
  12880. Implements: Graph.Op,
  12881. initialize: function(viz){
  12882. this.viz = viz;
  12883. }
  12884. });
  12885. //extend level methods of Graph.Geom
  12886. TM.Geom = new Class({
  12887. Implements: Graph.Geom,
  12888. getRightLevelToShow: function() {
  12889. return this.viz.config.levelsToShow;
  12890. },
  12891. setRightLevelToShow: function(node) {
  12892. var level = this.getRightLevelToShow(),
  12893. fx = this.viz.labels;
  12894. node.eachLevel(0, level+1, function(n) {
  12895. var d = n._depth - node._depth;
  12896. if(d > level) {
  12897. n.drawn = false;
  12898. n.exist = false;
  12899. n.ignore = true;
  12900. fx.hideLabel(n, false);
  12901. } else {
  12902. n.drawn = true;
  12903. n.exist = true;
  12904. delete n.ignore;
  12905. }
  12906. });
  12907. node.drawn = true;
  12908. delete node.ignore;
  12909. }
  12910. });
  12911. /*
  12912. Performs operations on group of nodes.
  12913. */
  12914. TM.Group = new Class( {
  12915. initialize: function(viz){
  12916. this.viz = viz;
  12917. this.canvas = viz.canvas;
  12918. this.config = viz.config;
  12919. },
  12920. /*
  12921. Calls the request method on the controller to request a subtree for each node.
  12922. */
  12923. requestNodes: function(nodes, controller){
  12924. var counter = 0, len = nodes.length, nodeSelected = {};
  12925. var complete = function(){
  12926. controller.onComplete();
  12927. };
  12928. var viz = this.viz;
  12929. if (len == 0)
  12930. complete();
  12931. for ( var i = 0; i < len; i++) {
  12932. nodeSelected[nodes[i].id] = nodes[i];
  12933. controller.request(nodes[i].id, nodes[i]._level, {
  12934. onComplete: function(nodeId, data){
  12935. if (data && data.children) {
  12936. data.id = nodeId;
  12937. viz.op.sum(data, {
  12938. type: 'nothing'
  12939. });
  12940. }
  12941. if (++counter == len) {
  12942. viz.graph.computeLevels(viz.root, 0);
  12943. complete();
  12944. }
  12945. }
  12946. });
  12947. }
  12948. }
  12949. });
  12950. /*
  12951. Class: TM.Plot
  12952. Custom extension of <Graph.Plot>.
  12953. Extends:
  12954. All <Graph.Plot> methods
  12955. See also:
  12956. <Graph.Plot>
  12957. */
  12958. TM.Plot = new Class({
  12959. Implements: Graph.Plot,
  12960. initialize: function(viz){
  12961. this.viz = viz;
  12962. this.config = viz.config;
  12963. this.node = this.config.Node;
  12964. this.edge = this.config.Edge;
  12965. this.animation = new Animation;
  12966. this.nodeTypes = new TM.Plot.NodeTypes;
  12967. this.edgeTypes = new TM.Plot.EdgeTypes;
  12968. this.labels = viz.labels;
  12969. },
  12970. plot: function(opt, animating){
  12971. var viz = this.viz,
  12972. graph = viz.graph;
  12973. viz.canvas.clear();
  12974. this.plotTree(graph.getNode(viz.clickedNode && viz.clickedNode.id || viz.root), $.merge(viz.config, opt || {}, {
  12975. 'withLabels': true,
  12976. 'hideLabels': false,
  12977. 'plotSubtree': function(n, ch){
  12978. return n.anySubnode("exist");
  12979. }
  12980. }), animating);
  12981. }
  12982. });
  12983. /*
  12984. Class: TM.Label
  12985. Custom extension of <Graph.Label>.
  12986. Contains custom <Graph.Label.SVG>, <Graph.Label.HTML> and <Graph.Label.Native> extensions.
  12987. Extends:
  12988. All <Graph.Label> methods and subclasses.
  12989. See also:
  12990. <Graph.Label>, <Graph.Label.Native>, <Graph.Label.HTML>, <Graph.Label.SVG>.
  12991. */
  12992. TM.Label = {};
  12993. /*
  12994. TM.Label.Native
  12995. Custom extension of <Graph.Label.Native>.
  12996. Extends:
  12997. All <Graph.Label.Native> methods
  12998. See also:
  12999. <Graph.Label.Native>
  13000. */
  13001. TM.Label.Native = new Class({
  13002. Implements: Graph.Label.Native,
  13003. initialize: function(viz) {
  13004. this.config = viz.config;
  13005. this.leaf = viz.leaf;
  13006. },
  13007. renderLabel: function(canvas, node, controller){
  13008. if(!this.leaf(node) && !this.config.titleHeight) return;
  13009. var pos = node.pos.getc(true),
  13010. ctx = canvas.getCtx(),
  13011. width = node.getData('width'),
  13012. height = node.getData('height'),
  13013. x = pos.x + width/2,
  13014. y = pos.y;
  13015. ctx.fillText(node.name, x, y, width);
  13016. }
  13017. });
  13018. /*
  13019. TM.Label.SVG
  13020. Custom extension of <Graph.Label.SVG>.
  13021. Extends:
  13022. All <Graph.Label.SVG> methods
  13023. See also:
  13024. <Graph.Label.SVG>
  13025. */
  13026. TM.Label.SVG = new Class( {
  13027. Implements: Graph.Label.SVG,
  13028. initialize: function(viz){
  13029. this.viz = viz;
  13030. this.leaf = viz.leaf;
  13031. this.config = viz.config;
  13032. },
  13033. /*
  13034. placeLabel
  13035. Overrides abstract method placeLabel in <Graph.Plot>.
  13036. Parameters:
  13037. tag - A DOM label element.
  13038. node - A <Graph.Node>.
  13039. controller - A configuration/controller object passed to the visualization.
  13040. */
  13041. placeLabel: function(tag, node, controller){
  13042. var pos = node.pos.getc(true),
  13043. canvas = this.viz.canvas,
  13044. ox = canvas.translateOffsetX,
  13045. oy = canvas.translateOffsetY,
  13046. sx = canvas.scaleOffsetX,
  13047. sy = canvas.scaleOffsetY,
  13048. radius = canvas.getSize();
  13049. var labelPos = {
  13050. x: Math.round(pos.x * sx + ox + radius.width / 2),
  13051. y: Math.round(pos.y * sy + oy + radius.height / 2)
  13052. };
  13053. tag.setAttribute('x', labelPos.x);
  13054. tag.setAttribute('y', labelPos.y);
  13055. if(!this.leaf(node) && !this.config.titleHeight) {
  13056. tag.style.display = 'none';
  13057. }
  13058. controller.onPlaceLabel(tag, node);
  13059. }
  13060. });
  13061. /*
  13062. TM.Label.HTML
  13063. Custom extension of <Graph.Label.HTML>.
  13064. Extends:
  13065. All <Graph.Label.HTML> methods.
  13066. See also:
  13067. <Graph.Label.HTML>
  13068. */
  13069. TM.Label.HTML = new Class( {
  13070. Implements: Graph.Label.HTML,
  13071. initialize: function(viz){
  13072. this.viz = viz;
  13073. this.leaf = viz.leaf;
  13074. this.config = viz.config;
  13075. },
  13076. /*
  13077. placeLabel
  13078. Overrides abstract method placeLabel in <Graph.Plot>.
  13079. Parameters:
  13080. tag - A DOM label element.
  13081. node - A <Graph.Node>.
  13082. controller - A configuration/controller object passed to the visualization.
  13083. */
  13084. placeLabel: function(tag, node, controller){
  13085. var pos = node.pos.getc(true),
  13086. canvas = this.viz.canvas,
  13087. ox = canvas.translateOffsetX,
  13088. oy = canvas.translateOffsetY,
  13089. sx = canvas.scaleOffsetX,
  13090. sy = canvas.scaleOffsetY,
  13091. radius = canvas.getSize();
  13092. var labelPos = {
  13093. x: Math.round(pos.x * sx + ox + radius.width / 2),
  13094. y: Math.round(pos.y * sy + oy + radius.height / 2)
  13095. };
  13096. var style = tag.style;
  13097. style.left = labelPos.x + 'px';
  13098. style.top = labelPos.y + 'px';
  13099. style.width = node.getData('width') * sx + 'px';
  13100. style.height = node.getData('height') * sy + 'px';
  13101. style.zIndex = node._depth * 100;
  13102. style.display = '';
  13103. if(!this.leaf(node) && !this.config.titleHeight) {
  13104. tag.style.display = 'none';
  13105. }
  13106. controller.onPlaceLabel(tag, node);
  13107. }
  13108. });
  13109. /*
  13110. Class: TM.Plot.NodeTypes
  13111. This class contains a list of <Graph.Node> built-in types.
  13112. Node types implemented are 'none', 'rectangle'.
  13113. You can add your custom node types, customizing your visualization to the extreme.
  13114. Example:
  13115. (start code js)
  13116. TM.Plot.NodeTypes.implement({
  13117. 'mySpecialType': {
  13118. 'render': function(node, canvas) {
  13119. //print your custom node to canvas
  13120. },
  13121. //optional
  13122. 'contains': function(node, pos) {
  13123. //return true if pos is inside the node or false otherwise
  13124. }
  13125. }
  13126. });
  13127. (end code)
  13128. */
  13129. TM.Plot.NodeTypes = new Class( {
  13130. 'none': {
  13131. 'render': $.empty
  13132. },
  13133. 'rectangle': {
  13134. 'render': function(node, canvas, animating){
  13135. var leaf = this.viz.leaf(node),
  13136. config = this.config,
  13137. offst = config.offset,
  13138. titleHeight = config.titleHeight,
  13139. pos = node.pos.getc(true),
  13140. width = node.getData('width'),
  13141. height = node.getData('height'),
  13142. border = node.getData('border'),
  13143. ctx = canvas.getCtx(),
  13144. posx = pos.x + offst / 2,
  13145. posy = pos.y + offst / 2;
  13146. if(width <= offst || height <= offst) return;
  13147. if (leaf) {
  13148. if(config.cushion) {
  13149. var lg = ctx.createRadialGradient(posx + (width-offst)/2, posy + (height-offst)/2, 1,
  13150. posx + (width-offst)/2, posy + (height-offst)/2, width < height? height : width);
  13151. var color = node.getData('color');
  13152. var colorGrad = $.rgbToHex($.map($.hexToRgb(color),
  13153. function(r) { return r * 0.2 >> 0; }));
  13154. lg.addColorStop(0, color);
  13155. lg.addColorStop(1, colorGrad);
  13156. ctx.fillStyle = lg;
  13157. }
  13158. ctx.fillRect(posx, posy, width - offst, height - offst);
  13159. if(border) {
  13160. ctx.save();
  13161. ctx.strokeStyle = border;
  13162. ctx.strokeRect(posx, posy, width - offst, height - offst);
  13163. ctx.restore();
  13164. }
  13165. } else if(titleHeight > 0){
  13166. ctx.fillRect(pos.x + offst / 2, pos.y + offst / 2, width - offst,
  13167. titleHeight - offst);
  13168. if(border) {
  13169. ctx.save();
  13170. ctx.strokeStyle = border;
  13171. ctx.strokeRect(pos.x + offst / 2, pos.y + offst / 2, width - offst,
  13172. height - offst);
  13173. ctx.restore();
  13174. }
  13175. }
  13176. },
  13177. 'contains': function(node, pos) {
  13178. if(this.viz.clickedNode && !node.isDescendantOf(this.viz.clickedNode.id) || node.ignore) return false;
  13179. var npos = node.pos.getc(true),
  13180. width = node.getData('width'),
  13181. leaf = this.viz.leaf(node),
  13182. height = leaf? node.getData('height') : this.config.titleHeight;
  13183. return this.nodeHelper.rectangle.contains({x: npos.x + width/2, y: npos.y + height/2}, pos, width, height);
  13184. }
  13185. }
  13186. });
  13187. TM.Plot.EdgeTypes = new Class( {
  13188. 'none': $.empty
  13189. });
  13190. /*
  13191. Class: TM.SliceAndDice
  13192. A slice and dice TreeMap visualization.
  13193. Implements:
  13194. All <TM.Base> methods and properties.
  13195. */
  13196. TM.SliceAndDice = new Class( {
  13197. Implements: [
  13198. Loader, Extras, TM.Base, Layouts.TM.SliceAndDice
  13199. ]
  13200. });
  13201. /*
  13202. Class: TM.Squarified
  13203. A squarified TreeMap visualization.
  13204. Implements:
  13205. All <TM.Base> methods and properties.
  13206. */
  13207. TM.Squarified = new Class( {
  13208. Implements: [
  13209. Loader, Extras, TM.Base, Layouts.TM.Squarified
  13210. ]
  13211. });
  13212. /*
  13213. Class: TM.Strip
  13214. A strip TreeMap visualization.
  13215. Implements:
  13216. All <TM.Base> methods and properties.
  13217. */
  13218. TM.Strip = new Class( {
  13219. Implements: [
  13220. Loader, Extras, TM.Base, Layouts.TM.Strip
  13221. ]
  13222. });
  13223. /*
  13224. * File: RGraph.js
  13225. *
  13226. */
  13227. /*
  13228. Class: RGraph
  13229. A radial graph visualization with advanced animations.
  13230. Inspired by:
  13231. Animated Exploration of Dynamic Graphs with Radial Layout (Ka-Ping Yee, Danyel Fisher, Rachna Dhamija, Marti Hearst) <http://bailando.sims.berkeley.edu/papers/infovis01.htm>
  13232. Note:
  13233. This visualization was built and engineered from scratch, taking only the paper as inspiration, and only shares some features with the visualization described in the paper.
  13234. Implements:
  13235. All <Loader> methods
  13236. Constructor Options:
  13237. Inherits options from
  13238. - <Options.Canvas>
  13239. - <Options.Controller>
  13240. - <Options.Node>
  13241. - <Options.Edge>
  13242. - <Options.Label>
  13243. - <Options.Events>
  13244. - <Options.Tips>
  13245. - <Options.NodeStyles>
  13246. - <Options.Navigation>
  13247. Additionally, there are other parameters and some default values changed
  13248. interpolation - (string) Default's *linear*. Describes the way nodes are interpolated. Possible values are 'linear' and 'polar'.
  13249. levelDistance - (number) Default's *100*. The distance between levels of the tree.
  13250. Instance Properties:
  13251. canvas - Access a <Canvas> instance.
  13252. graph - Access a <Graph> instance.
  13253. op - Access a <RGraph.Op> instance.
  13254. fx - Access a <RGraph.Plot> instance.
  13255. labels - Access a <RGraph.Label> interface implementation.
  13256. */
  13257. $jit.RGraph = new Class( {
  13258. Implements: [
  13259. Loader, Extras, Layouts.Radial
  13260. ],
  13261. initialize: function(controller){
  13262. var $RGraph = $jit.RGraph;
  13263. var config = {
  13264. interpolation: 'linear',
  13265. levelDistance: 100
  13266. };
  13267. this.controller = this.config = $.merge(Options("Canvas", "Node", "Edge",
  13268. "Fx", "Controller", "Tips", "NodeStyles", "Events", "Navigation", "Label"), config, controller);
  13269. var canvasConfig = this.config;
  13270. if(canvasConfig.useCanvas) {
  13271. this.canvas = canvasConfig.useCanvas;
  13272. this.config.labelContainer = this.canvas.id + '-label';
  13273. } else {
  13274. if(canvasConfig.background) {
  13275. canvasConfig.background = $.merge({
  13276. type: 'Circles'
  13277. }, canvasConfig.background);
  13278. }
  13279. this.canvas = new Canvas(this, canvasConfig);
  13280. this.config.labelContainer = (typeof canvasConfig.injectInto == 'string'? canvasConfig.injectInto : canvasConfig.injectInto.id) + '-label';
  13281. }
  13282. this.graphOptions = {
  13283. 'klass': Polar,
  13284. 'Node': {
  13285. 'selected': false,
  13286. 'exist': true,
  13287. 'drawn': true
  13288. }
  13289. };
  13290. this.graph = new Graph(this.graphOptions, this.config.Node,
  13291. this.config.Edge);
  13292. this.labels = new $RGraph.Label[canvasConfig.Label.type](this);
  13293. this.fx = new $RGraph.Plot(this, $RGraph);
  13294. this.op = new $RGraph.Op(this);
  13295. this.json = null;
  13296. this.root = null;
  13297. this.busy = false;
  13298. this.parent = false;
  13299. // initialize extras
  13300. this.initializeExtras();
  13301. },
  13302. /*
  13303. createLevelDistanceFunc
  13304. Returns the levelDistance function used for calculating a node distance
  13305. to its origin. This function returns a function that is computed
  13306. per level and not per node, such that all nodes with the same depth will have the
  13307. same distance to the origin. The resulting function gets the
  13308. parent node as parameter and returns a float.
  13309. */
  13310. createLevelDistanceFunc: function(){
  13311. var ld = this.config.levelDistance;
  13312. return function(elem){
  13313. return (elem._depth + 1) * ld;
  13314. };
  13315. },
  13316. /*
  13317. Method: refresh
  13318. Computes positions and plots the tree.
  13319. */
  13320. refresh: function(){
  13321. this.compute();
  13322. this.plot();
  13323. },
  13324. reposition: function(){
  13325. this.compute('end');
  13326. },
  13327. /*
  13328. Method: plot
  13329. Plots the RGraph. This is a shortcut to *fx.plot*.
  13330. */
  13331. plot: function(){
  13332. this.fx.plot();
  13333. },
  13334. /*
  13335. getNodeAndParentAngle
  13336. Returns the _parent_ of the given node, also calculating its angle span.
  13337. */
  13338. getNodeAndParentAngle: function(id){
  13339. var theta = false;
  13340. var n = this.graph.getNode(id);
  13341. var ps = n.getParents();
  13342. var p = (ps.length > 0)? ps[0] : false;
  13343. if (p) {
  13344. var posParent = p.pos.getc(), posChild = n.pos.getc();
  13345. var newPos = posParent.add(posChild.scale(-1));
  13346. theta = Math.atan2(newPos.y, newPos.x);
  13347. if (theta < 0)
  13348. theta += 2 * Math.PI;
  13349. }
  13350. return {
  13351. parent: p,
  13352. theta: theta
  13353. };
  13354. },
  13355. /*
  13356. tagChildren
  13357. Enumerates the children in order to maintain child ordering (second constraint of the paper).
  13358. */
  13359. tagChildren: function(par, id){
  13360. if (par.angleSpan) {
  13361. var adjs = [];
  13362. par.eachAdjacency(function(elem){
  13363. adjs.push(elem.nodeTo);
  13364. }, "ignore");
  13365. var len = adjs.length;
  13366. for ( var i = 0; i < len && id != adjs[i].id; i++)
  13367. ;
  13368. for ( var j = (i + 1) % len, k = 0; id != adjs[j].id; j = (j + 1) % len) {
  13369. adjs[j].dist = k++;
  13370. }
  13371. }
  13372. },
  13373. /*
  13374. Method: onClick
  13375. Animates the <RGraph> to center the node specified by *id*.
  13376. Parameters:
  13377. id - A <Graph.Node> id.
  13378. opt - (optional|object) An object containing some extra properties described below
  13379. hideLabels - (boolean) Default's *true*. Hide labels when performing the animation.
  13380. Example:
  13381. (start code js)
  13382. rgraph.onClick('someid');
  13383. //or also...
  13384. rgraph.onClick('someid', {
  13385. hideLabels: false
  13386. });
  13387. (end code)
  13388. */
  13389. onClick: function(id, opt){
  13390. if (this.root != id && !this.busy) {
  13391. this.busy = true;
  13392. this.root = id;
  13393. var that = this;
  13394. this.controller.onBeforeCompute(this.graph.getNode(id));
  13395. var obj = this.getNodeAndParentAngle(id);
  13396. // second constraint
  13397. this.tagChildren(obj.parent, id);
  13398. this.parent = obj.parent;
  13399. this.compute('end');
  13400. // first constraint
  13401. var thetaDiff = obj.theta - obj.parent.endPos.theta;
  13402. this.graph.eachNode(function(elem){
  13403. elem.endPos.set(elem.endPos.getp().add($P(thetaDiff, 0)));
  13404. });
  13405. var mode = this.config.interpolation;
  13406. opt = $.merge( {
  13407. onComplete: $.empty
  13408. }, opt || {});
  13409. this.fx.animate($.merge( {
  13410. hideLabels: true,
  13411. modes: [
  13412. mode
  13413. ]
  13414. }, opt, {
  13415. onComplete: function(){
  13416. that.busy = false;
  13417. opt.onComplete();
  13418. }
  13419. }));
  13420. }
  13421. }
  13422. });
  13423. $jit.RGraph.$extend = true;
  13424. (function(RGraph){
  13425. /*
  13426. Class: RGraph.Op
  13427. Custom extension of <Graph.Op>.
  13428. Extends:
  13429. All <Graph.Op> methods
  13430. See also:
  13431. <Graph.Op>
  13432. */
  13433. RGraph.Op = new Class( {
  13434. Implements: Graph.Op
  13435. });
  13436. /*
  13437. Class: RGraph.Plot
  13438. Custom extension of <Graph.Plot>.
  13439. Extends:
  13440. All <Graph.Plot> methods
  13441. See also:
  13442. <Graph.Plot>
  13443. */
  13444. RGraph.Plot = new Class( {
  13445. Implements: Graph.Plot
  13446. });
  13447. /*
  13448. Object: RGraph.Label
  13449. Custom extension of <Graph.Label>.
  13450. Contains custom <Graph.Label.SVG>, <Graph.Label.HTML> and <Graph.Label.Native> extensions.
  13451. Extends:
  13452. All <Graph.Label> methods and subclasses.
  13453. See also:
  13454. <Graph.Label>, <Graph.Label.Native>, <Graph.Label.HTML>, <Graph.Label.SVG>.
  13455. */
  13456. RGraph.Label = {};
  13457. /*
  13458. RGraph.Label.Native
  13459. Custom extension of <Graph.Label.Native>.
  13460. Extends:
  13461. All <Graph.Label.Native> methods
  13462. See also:
  13463. <Graph.Label.Native>
  13464. */
  13465. RGraph.Label.Native = new Class( {
  13466. Implements: Graph.Label.Native
  13467. });
  13468. /*
  13469. RGraph.Label.SVG
  13470. Custom extension of <Graph.Label.SVG>.
  13471. Extends:
  13472. All <Graph.Label.SVG> methods
  13473. See also:
  13474. <Graph.Label.SVG>
  13475. */
  13476. RGraph.Label.SVG = new Class( {
  13477. Implements: Graph.Label.SVG,
  13478. initialize: function(viz){
  13479. this.viz = viz;
  13480. },
  13481. /*
  13482. placeLabel
  13483. Overrides abstract method placeLabel in <Graph.Plot>.
  13484. Parameters:
  13485. tag - A DOM label element.
  13486. node - A <Graph.Node>.
  13487. controller - A configuration/controller object passed to the visualization.
  13488. */
  13489. placeLabel: function(tag, node, controller){
  13490. var pos = node.pos.getc(true),
  13491. canvas = this.viz.canvas,
  13492. ox = canvas.translateOffsetX,
  13493. oy = canvas.translateOffsetY,
  13494. sx = canvas.scaleOffsetX,
  13495. sy = canvas.scaleOffsetY,
  13496. radius = canvas.getSize();
  13497. var labelPos = {
  13498. x: Math.round(pos.x * sx + ox + radius.width / 2),
  13499. y: Math.round(pos.y * sy + oy + radius.height / 2)
  13500. };
  13501. tag.setAttribute('x', labelPos.x);
  13502. tag.setAttribute('y', labelPos.y);
  13503. controller.onPlaceLabel(tag, node);
  13504. }
  13505. });
  13506. /*
  13507. RGraph.Label.HTML
  13508. Custom extension of <Graph.Label.HTML>.
  13509. Extends:
  13510. All <Graph.Label.HTML> methods.
  13511. See also:
  13512. <Graph.Label.HTML>
  13513. */
  13514. RGraph.Label.HTML = new Class( {
  13515. Implements: Graph.Label.HTML,
  13516. initialize: function(viz){
  13517. this.viz = viz;
  13518. },
  13519. /*
  13520. placeLabel
  13521. Overrides abstract method placeLabel in <Graph.Plot>.
  13522. Parameters:
  13523. tag - A DOM label element.
  13524. node - A <Graph.Node>.
  13525. controller - A configuration/controller object passed to the visualization.
  13526. */
  13527. placeLabel: function(tag, node, controller){
  13528. var pos = node.pos.getc(true),
  13529. canvas = this.viz.canvas,
  13530. ox = canvas.translateOffsetX,
  13531. oy = canvas.translateOffsetY,
  13532. sx = canvas.scaleOffsetX,
  13533. sy = canvas.scaleOffsetY,
  13534. radius = canvas.getSize();
  13535. var labelPos = {
  13536. x: Math.round(pos.x * sx + ox + radius.width / 2),
  13537. y: Math.round(pos.y * sy + oy + radius.height / 2)
  13538. };
  13539. var style = tag.style;
  13540. style.left = labelPos.x + 'px';
  13541. style.top = labelPos.y + 'px';
  13542. style.display = this.fitsInCanvas(labelPos, canvas)? '' : 'none';
  13543. controller.onPlaceLabel(tag, node);
  13544. }
  13545. });
  13546. /*
  13547. Class: RGraph.Plot.NodeTypes
  13548. This class contains a list of <Graph.Node> built-in types.
  13549. Node types implemented are 'none', 'circle', 'triangle', 'rectangle', 'star', 'ellipse' and 'square'.
  13550. You can add your custom node types, customizing your visualization to the extreme.
  13551. Example:
  13552. (start code js)
  13553. RGraph.Plot.NodeTypes.implement({
  13554. 'mySpecialType': {
  13555. 'render': function(node, canvas) {
  13556. //print your custom node to canvas
  13557. },
  13558. //optional
  13559. 'contains': function(node, pos) {
  13560. //return true if pos is inside the node or false otherwise
  13561. }
  13562. }
  13563. });
  13564. (end code)
  13565. */
  13566. RGraph.Plot.NodeTypes = new Class({
  13567. 'none': {
  13568. 'render': $.empty,
  13569. 'contains': $.lambda(false)
  13570. },
  13571. 'circle': {
  13572. 'render': function(node, canvas){
  13573. var pos = node.pos.getc(true),
  13574. dim = node.getData('dim');
  13575. this.nodeHelper.circle.render('fill', pos, dim, canvas);
  13576. },
  13577. 'contains': function(node, pos){
  13578. var npos = node.pos.getc(true),
  13579. dim = node.getData('dim');
  13580. return this.nodeHelper.circle.contains(npos, pos, dim);
  13581. }
  13582. },
  13583. 'ellipse': {
  13584. 'render': function(node, canvas){
  13585. var pos = node.pos.getc(true),
  13586. width = node.getData('width'),
  13587. height = node.getData('height');
  13588. this.nodeHelper.ellipse.render('fill', pos, width, height, canvas);
  13589. },
  13590. 'contains': function(node, pos){
  13591. var npos = node.pos.getc(true),
  13592. width = node.getData('width'),
  13593. height = node.getData('height');
  13594. return this.nodeHelper.ellipse.contains(npos, pos, width, height);
  13595. }
  13596. },
  13597. 'square': {
  13598. 'render': function(node, canvas){
  13599. var pos = node.pos.getc(true),
  13600. dim = node.getData('dim');
  13601. this.nodeHelper.square.render('fill', pos, dim, canvas);
  13602. },
  13603. 'contains': function(node, pos){
  13604. var npos = node.pos.getc(true),
  13605. dim = node.getData('dim');
  13606. return this.nodeHelper.square.contains(npos, pos, dim);
  13607. }
  13608. },
  13609. 'rectangle': {
  13610. 'render': function(node, canvas){
  13611. var pos = node.pos.getc(true),
  13612. width = node.getData('width'),
  13613. height = node.getData('height');
  13614. this.nodeHelper.rectangle.render('fill', pos, width, height, canvas);
  13615. },
  13616. 'contains': function(node, pos){
  13617. var npos = node.pos.getc(true),
  13618. width = node.getData('width'),
  13619. height = node.getData('height');
  13620. return this.nodeHelper.rectangle.contains(npos, pos, width, height);
  13621. }
  13622. },
  13623. 'triangle': {
  13624. 'render': function(node, canvas){
  13625. var pos = node.pos.getc(true),
  13626. dim = node.getData('dim');
  13627. this.nodeHelper.triangle.render('fill', pos, dim, canvas);
  13628. },
  13629. 'contains': function(node, pos) {
  13630. var npos = node.pos.getc(true),
  13631. dim = node.getData('dim');
  13632. return this.nodeHelper.triangle.contains(npos, pos, dim);
  13633. }
  13634. },
  13635. 'star': {
  13636. 'render': function(node, canvas){
  13637. var pos = node.pos.getc(true),
  13638. dim = node.getData('dim');
  13639. this.nodeHelper.star.render('fill', pos, dim, canvas);
  13640. },
  13641. 'contains': function(node, pos) {
  13642. var npos = node.pos.getc(true),
  13643. dim = node.getData('dim');
  13644. return this.nodeHelper.star.contains(npos, pos, dim);
  13645. }
  13646. }
  13647. });
  13648. /*
  13649. Class: RGraph.Plot.EdgeTypes
  13650. This class contains a list of <Graph.Adjacence> built-in types.
  13651. Edge types implemented are 'none', 'line' and 'arrow'.
  13652. You can add your custom edge types, customizing your visualization to the extreme.
  13653. Example:
  13654. (start code js)
  13655. RGraph.Plot.EdgeTypes.implement({
  13656. 'mySpecialType': {
  13657. 'render': function(adj, canvas) {
  13658. //print your custom edge to canvas
  13659. },
  13660. //optional
  13661. 'contains': function(adj, pos) {
  13662. //return true if pos is inside the arc or false otherwise
  13663. }
  13664. }
  13665. });
  13666. (end code)
  13667. */
  13668. RGraph.Plot.EdgeTypes = new Class({
  13669. 'none': $.empty,
  13670. 'line': {
  13671. 'render': function(adj, canvas) {
  13672. var from = adj.nodeFrom.pos.getc(true),
  13673. to = adj.nodeTo.pos.getc(true);
  13674. this.edgeHelper.line.render(from, to, canvas);
  13675. },
  13676. 'contains': function(adj, pos) {
  13677. var from = adj.nodeFrom.pos.getc(true),
  13678. to = adj.nodeTo.pos.getc(true);
  13679. return this.edgeHelper.line.contains(from, to, pos, this.edge.epsilon);
  13680. }
  13681. },
  13682. 'arrow': {
  13683. 'render': function(adj, canvas) {
  13684. var from = adj.nodeFrom.pos.getc(true),
  13685. to = adj.nodeTo.pos.getc(true),
  13686. dim = adj.getData('dim'),
  13687. direction = adj.data.$direction,
  13688. inv = (direction && direction.length>1 && direction[0] != adj.nodeFrom.id);
  13689. this.edgeHelper.arrow.render(from, to, dim, inv, canvas);
  13690. },
  13691. 'contains': function(adj, pos) {
  13692. var from = adj.nodeFrom.pos.getc(true),
  13693. to = adj.nodeTo.pos.getc(true);
  13694. return this.edgeHelper.arrow.contains(from, to, pos, this.edge.epsilon);
  13695. }
  13696. }
  13697. });
  13698. })($jit.RGraph);
  13699. /*
  13700. * File: Hypertree.js
  13701. *
  13702. */
  13703. /*
  13704. Complex
  13705. A multi-purpose Complex Class with common methods. Extended for the Hypertree.
  13706. */
  13707. /*
  13708. moebiusTransformation
  13709. Calculates a moebius transformation for this point / complex.
  13710. For more information go to:
  13711. http://en.wikipedia.org/wiki/Moebius_transformation.
  13712. Parameters:
  13713. c - An initialized Complex instance representing a translation Vector.
  13714. */
  13715. Complex.prototype.moebiusTransformation = function(c) {
  13716. var num = this.add(c);
  13717. var den = c.$conjugate().$prod(this);
  13718. den.x++;
  13719. return num.$div(den);
  13720. };
  13721. /*
  13722. moebiusTransformation
  13723. Calculates a moebius transformation for the hyperbolic tree.
  13724. <http://en.wikipedia.org/wiki/Moebius_transformation>
  13725. Parameters:
  13726. graph - A <Graph> instance.
  13727. pos - A <Complex>.
  13728. prop - A property array.
  13729. theta - Rotation angle.
  13730. startPos - _optional_ start position.
  13731. */
  13732. Graph.Util.moebiusTransformation = function(graph, pos, prop, startPos, flags) {
  13733. this.eachNode(graph, function(elem) {
  13734. for ( var i = 0; i < prop.length; i++) {
  13735. var p = pos[i].scale(-1), property = startPos ? startPos : prop[i];
  13736. elem.getPos(prop[i]).set(elem.getPos(property).getc().moebiusTransformation(p));
  13737. }
  13738. }, flags);
  13739. };
  13740. /*
  13741. Class: Hypertree
  13742. A Hyperbolic Tree/Graph visualization.
  13743. Inspired by:
  13744. A Focus+Context Technique Based on Hyperbolic Geometry for Visualizing Large Hierarchies (John Lamping, Ramana Rao, and Peter Pirolli).
  13745. <http://www.cs.tau.ac.il/~asharf/shrek/Projects/HypBrowser/startree-chi95.pdf>
  13746. Note:
  13747. This visualization was built and engineered from scratch, taking only the paper as inspiration, and only shares some features with the Hypertree described in the paper.
  13748. Implements:
  13749. All <Loader> methods
  13750. Constructor Options:
  13751. Inherits options from
  13752. - <Options.Canvas>
  13753. - <Options.Controller>
  13754. - <Options.Node>
  13755. - <Options.Edge>
  13756. - <Options.Label>
  13757. - <Options.Events>
  13758. - <Options.Tips>
  13759. - <Options.NodeStyles>
  13760. - <Options.Navigation>
  13761. Additionally, there are other parameters and some default values changed
  13762. radius - (string|number) Default's *auto*. The radius of the disc to plot the <Hypertree> in. 'auto' will take the smaller value from the width and height canvas dimensions. You can also set this to a custom value, for example *250*.
  13763. offset - (number) Default's *0*. A number in the range [0, 1) that will be substracted to each node position to make a more compact <Hypertree>. This will avoid placing nodes too far from each other when a there's a selected node.
  13764. fps - Described in <Options.Fx>. It's default value has been changed to *35*.
  13765. duration - Described in <Options.Fx>. It's default value has been changed to *1500*.
  13766. Edge.type - Described in <Options.Edge>. It's default value has been changed to *hyperline*.
  13767. Instance Properties:
  13768. canvas - Access a <Canvas> instance.
  13769. graph - Access a <Graph> instance.
  13770. op - Access a <Hypertree.Op> instance.
  13771. fx - Access a <Hypertree.Plot> instance.
  13772. labels - Access a <Hypertree.Label> interface implementation.
  13773. */
  13774. $jit.Hypertree = new Class( {
  13775. Implements: [ Loader, Extras, Layouts.Radial ],
  13776. initialize: function(controller) {
  13777. var $Hypertree = $jit.Hypertree;
  13778. var config = {
  13779. radius: "auto",
  13780. offset: 0,
  13781. Edge: {
  13782. type: 'hyperline'
  13783. },
  13784. duration: 1500,
  13785. fps: 35
  13786. };
  13787. this.controller = this.config = $.merge(Options("Canvas", "Node", "Edge",
  13788. "Fx", "Tips", "NodeStyles", "Events", "Navigation", "Controller", "Label"), config, controller);
  13789. var canvasConfig = this.config;
  13790. if(canvasConfig.useCanvas) {
  13791. this.canvas = canvasConfig.useCanvas;
  13792. this.config.labelContainer = this.canvas.id + '-label';
  13793. } else {
  13794. if(canvasConfig.background) {
  13795. canvasConfig.background = $.merge({
  13796. type: 'Circles'
  13797. }, canvasConfig.background);
  13798. }
  13799. this.canvas = new Canvas(this, canvasConfig);
  13800. this.config.labelContainer = (typeof canvasConfig.injectInto == 'string'? canvasConfig.injectInto : canvasConfig.injectInto.id) + '-label';
  13801. }
  13802. this.graphOptions = {
  13803. 'klass': Polar,
  13804. 'Node': {
  13805. 'selected': false,
  13806. 'exist': true,
  13807. 'drawn': true
  13808. }
  13809. };
  13810. this.graph = new Graph(this.graphOptions, this.config.Node,
  13811. this.config.Edge);
  13812. this.labels = new $Hypertree.Label[canvasConfig.Label.type](this);
  13813. this.fx = new $Hypertree.Plot(this, $Hypertree);
  13814. this.op = new $Hypertree.Op(this);
  13815. this.json = null;
  13816. this.root = null;
  13817. this.busy = false;
  13818. // initialize extras
  13819. this.initializeExtras();
  13820. },
  13821. /*
  13822. createLevelDistanceFunc
  13823. Returns the levelDistance function used for calculating a node distance
  13824. to its origin. This function returns a function that is computed
  13825. per level and not per node, such that all nodes with the same depth will have the
  13826. same distance to the origin. The resulting function gets the
  13827. parent node as parameter and returns a float.
  13828. */
  13829. createLevelDistanceFunc: function() {
  13830. // get max viz. length.
  13831. var r = this.getRadius();
  13832. // get max depth.
  13833. var depth = 0, max = Math.max, config = this.config;
  13834. this.graph.eachNode(function(node) {
  13835. depth = max(node._depth, depth);
  13836. }, "ignore");
  13837. depth++;
  13838. // node distance generator
  13839. var genDistFunc = function(a) {
  13840. return function(node) {
  13841. node.scale = r;
  13842. var d = node._depth + 1;
  13843. var acum = 0, pow = Math.pow;
  13844. while (d) {
  13845. acum += pow(a, d--);
  13846. }
  13847. return acum - config.offset;
  13848. };
  13849. };
  13850. // estimate better edge length.
  13851. for ( var i = 0.51; i <= 1; i += 0.01) {
  13852. var valSeries = (1 - Math.pow(i, depth)) / (1 - i);
  13853. if (valSeries >= 2) { return genDistFunc(i - 0.01); }
  13854. }
  13855. return genDistFunc(0.75);
  13856. },
  13857. /*
  13858. Method: getRadius
  13859. Returns the current radius of the visualization. If *config.radius* is *auto* then it
  13860. calculates the radius by taking the smaller size of the <Canvas> widget.
  13861. See also:
  13862. <Canvas.getSize>
  13863. */
  13864. getRadius: function() {
  13865. var rad = this.config.radius;
  13866. if (rad !== "auto") { return rad; }
  13867. var s = this.canvas.getSize();
  13868. return Math.min(s.width, s.height) / 2;
  13869. },
  13870. /*
  13871. Method: refresh
  13872. Computes positions and plots the tree.
  13873. Parameters:
  13874. reposition - (optional|boolean) Set this to *true* to force all positions (current, start, end) to match.
  13875. */
  13876. refresh: function(reposition) {
  13877. if (reposition) {
  13878. this.reposition();
  13879. this.graph.eachNode(function(node) {
  13880. node.startPos.rho = node.pos.rho = node.endPos.rho;
  13881. node.startPos.theta = node.pos.theta = node.endPos.theta;
  13882. });
  13883. } else {
  13884. this.compute();
  13885. }
  13886. this.plot();
  13887. },
  13888. /*
  13889. reposition
  13890. Computes nodes' positions and restores the tree to its previous position.
  13891. For calculating nodes' positions the root must be placed on its origin. This method does this
  13892. and then attemps to restore the hypertree to its previous position.
  13893. */
  13894. reposition: function() {
  13895. this.compute('end');
  13896. var vector = this.graph.getNode(this.root).pos.getc().scale(-1);
  13897. Graph.Util.moebiusTransformation(this.graph, [ vector ], [ 'end' ],
  13898. 'end', "ignore");
  13899. this.graph.eachNode(function(node) {
  13900. if (node.ignore) {
  13901. node.endPos.rho = node.pos.rho;
  13902. node.endPos.theta = node.pos.theta;
  13903. }
  13904. });
  13905. },
  13906. /*
  13907. Method: plot
  13908. Plots the <Hypertree>. This is a shortcut to *fx.plot*.
  13909. */
  13910. plot: function() {
  13911. this.fx.plot();
  13912. },
  13913. /*
  13914. Method: onClick
  13915. Animates the <Hypertree> to center the node specified by *id*.
  13916. Parameters:
  13917. id - A <Graph.Node> id.
  13918. opt - (optional|object) An object containing some extra properties described below
  13919. hideLabels - (boolean) Default's *true*. Hide labels when performing the animation.
  13920. Example:
  13921. (start code js)
  13922. ht.onClick('someid');
  13923. //or also...
  13924. ht.onClick('someid', {
  13925. hideLabels: false
  13926. });
  13927. (end code)
  13928. */
  13929. onClick: function(id, opt) {
  13930. var pos = this.graph.getNode(id).pos.getc(true);
  13931. this.move(pos, opt);
  13932. },
  13933. /*
  13934. Method: move
  13935. Translates the tree to the given position.
  13936. Parameters:
  13937. pos - (object) A *x, y* coordinate object where x, y in [0, 1), to move the tree to.
  13938. opt - This object has been defined in <Hypertree.onClick>
  13939. Example:
  13940. (start code js)
  13941. ht.move({ x: 0, y: 0.7 }, {
  13942. hideLabels: false
  13943. });
  13944. (end code)
  13945. */
  13946. move: function(pos, opt) {
  13947. var versor = $C(pos.x, pos.y);
  13948. if (this.busy === false && versor.norm() < 1) {
  13949. this.busy = true;
  13950. var root = this.graph.getClosestNodeToPos(versor), that = this;
  13951. this.graph.computeLevels(root.id, 0);
  13952. this.controller.onBeforeCompute(root);
  13953. opt = $.merge( {
  13954. onComplete: $.empty
  13955. }, opt || {});
  13956. this.fx.animate($.merge( {
  13957. modes: [ 'moebius' ],
  13958. hideLabels: true
  13959. }, opt, {
  13960. onComplete: function() {
  13961. that.busy = false;
  13962. opt.onComplete();
  13963. }
  13964. }), versor);
  13965. }
  13966. }
  13967. });
  13968. $jit.Hypertree.$extend = true;
  13969. (function(Hypertree) {
  13970. /*
  13971. Class: Hypertree.Op
  13972. Custom extension of <Graph.Op>.
  13973. Extends:
  13974. All <Graph.Op> methods
  13975. See also:
  13976. <Graph.Op>
  13977. */
  13978. Hypertree.Op = new Class( {
  13979. Implements: Graph.Op
  13980. });
  13981. /*
  13982. Class: Hypertree.Plot
  13983. Custom extension of <Graph.Plot>.
  13984. Extends:
  13985. All <Graph.Plot> methods
  13986. See also:
  13987. <Graph.Plot>
  13988. */
  13989. Hypertree.Plot = new Class( {
  13990. Implements: Graph.Plot
  13991. });
  13992. /*
  13993. Object: Hypertree.Label
  13994. Custom extension of <Graph.Label>.
  13995. Contains custom <Graph.Label.SVG>, <Graph.Label.HTML> and <Graph.Label.Native> extensions.
  13996. Extends:
  13997. All <Graph.Label> methods and subclasses.
  13998. See also:
  13999. <Graph.Label>, <Graph.Label.Native>, <Graph.Label.HTML>, <Graph.Label.SVG>.
  14000. */
  14001. Hypertree.Label = {};
  14002. /*
  14003. Hypertree.Label.Native
  14004. Custom extension of <Graph.Label.Native>.
  14005. Extends:
  14006. All <Graph.Label.Native> methods
  14007. See also:
  14008. <Graph.Label.Native>
  14009. */
  14010. Hypertree.Label.Native = new Class( {
  14011. Implements: Graph.Label.Native,
  14012. initialize: function(viz) {
  14013. this.viz = viz;
  14014. },
  14015. renderLabel: function(canvas, node, controller) {
  14016. var ctx = canvas.getCtx();
  14017. var coord = node.pos.getc(true);
  14018. var s = this.viz.getRadius();
  14019. ctx.fillText(node.name, coord.x * s, coord.y * s);
  14020. }
  14021. });
  14022. /*
  14023. Hypertree.Label.SVG
  14024. Custom extension of <Graph.Label.SVG>.
  14025. Extends:
  14026. All <Graph.Label.SVG> methods
  14027. See also:
  14028. <Graph.Label.SVG>
  14029. */
  14030. Hypertree.Label.SVG = new Class( {
  14031. Implements: Graph.Label.SVG,
  14032. initialize: function(viz) {
  14033. this.viz = viz;
  14034. },
  14035. /*
  14036. placeLabel
  14037. Overrides abstract method placeLabel in <Graph.Plot>.
  14038. Parameters:
  14039. tag - A DOM label element.
  14040. node - A <Graph.Node>.
  14041. controller - A configuration/controller object passed to the visualization.
  14042. */
  14043. placeLabel: function(tag, node, controller) {
  14044. var pos = node.pos.getc(true),
  14045. canvas = this.viz.canvas,
  14046. ox = canvas.translateOffsetX,
  14047. oy = canvas.translateOffsetY,
  14048. sx = canvas.scaleOffsetX,
  14049. sy = canvas.scaleOffsetY,
  14050. radius = canvas.getSize(),
  14051. r = this.viz.getRadius();
  14052. var labelPos = {
  14053. x: Math.round((pos.x * sx) * r + ox + radius.width / 2),
  14054. y: Math.round((pos.y * sy) * r + oy + radius.height / 2)
  14055. };
  14056. tag.setAttribute('x', labelPos.x);
  14057. tag.setAttribute('y', labelPos.y);
  14058. controller.onPlaceLabel(tag, node);
  14059. }
  14060. });
  14061. /*
  14062. Hypertree.Label.HTML
  14063. Custom extension of <Graph.Label.HTML>.
  14064. Extends:
  14065. All <Graph.Label.HTML> methods.
  14066. See also:
  14067. <Graph.Label.HTML>
  14068. */
  14069. Hypertree.Label.HTML = new Class( {
  14070. Implements: Graph.Label.HTML,
  14071. initialize: function(viz) {
  14072. this.viz = viz;
  14073. },
  14074. /*
  14075. placeLabel
  14076. Overrides abstract method placeLabel in <Graph.Plot>.
  14077. Parameters:
  14078. tag - A DOM label element.
  14079. node - A <Graph.Node>.
  14080. controller - A configuration/controller object passed to the visualization.
  14081. */
  14082. placeLabel: function(tag, node, controller) {
  14083. var pos = node.pos.getc(true),
  14084. canvas = this.viz.canvas,
  14085. ox = canvas.translateOffsetX,
  14086. oy = canvas.translateOffsetY,
  14087. sx = canvas.scaleOffsetX,
  14088. sy = canvas.scaleOffsetY,
  14089. radius = canvas.getSize(),
  14090. r = this.viz.getRadius();
  14091. var labelPos = {
  14092. x: Math.round((pos.x * sx) * r + ox + radius.width / 2),
  14093. y: Math.round((pos.y * sy) * r + oy + radius.height / 2)
  14094. };
  14095. var style = tag.style;
  14096. style.left = labelPos.x + 'px';
  14097. style.top = labelPos.y + 'px';
  14098. style.display = this.fitsInCanvas(labelPos, canvas) ? '' : 'none';
  14099. controller.onPlaceLabel(tag, node);
  14100. }
  14101. });
  14102. /*
  14103. Class: Hypertree.Plot.NodeTypes
  14104. This class contains a list of <Graph.Node> built-in types.
  14105. Node types implemented are 'none', 'circle', 'triangle', 'rectangle', 'star', 'ellipse' and 'square'.
  14106. You can add your custom node types, customizing your visualization to the extreme.
  14107. Example:
  14108. (start code js)
  14109. Hypertree.Plot.NodeTypes.implement({
  14110. 'mySpecialType': {
  14111. 'render': function(node, canvas) {
  14112. //print your custom node to canvas
  14113. },
  14114. //optional
  14115. 'contains': function(node, pos) {
  14116. //return true if pos is inside the node or false otherwise
  14117. }
  14118. }
  14119. });
  14120. (end code)
  14121. */
  14122. Hypertree.Plot.NodeTypes = new Class({
  14123. 'none': {
  14124. 'render': $.empty,
  14125. 'contains': $.lambda(false)
  14126. },
  14127. 'circle': {
  14128. 'render': function(node, canvas) {
  14129. var nconfig = this.node,
  14130. dim = node.getData('dim'),
  14131. p = node.pos.getc();
  14132. dim = nconfig.transform? dim * (1 - p.squaredNorm()) : dim;
  14133. p.$scale(node.scale);
  14134. if (dim > 0.2) {
  14135. this.nodeHelper.circle.render('fill', p, dim, canvas);
  14136. }
  14137. },
  14138. 'contains': function(node, pos) {
  14139. var dim = node.getData('dim'),
  14140. npos = node.pos.getc().$scale(node.scale);
  14141. return this.nodeHelper.circle.contains(npos, pos, dim);
  14142. }
  14143. },
  14144. 'ellipse': {
  14145. 'render': function(node, canvas) {
  14146. var pos = node.pos.getc().$scale(node.scale),
  14147. width = node.getData('width'),
  14148. height = node.getData('height');
  14149. this.nodeHelper.ellipse.render('fill', pos, width, height, canvas);
  14150. },
  14151. 'contains': function(node, pos) {
  14152. var width = node.getData('width'),
  14153. height = node.getData('height'),
  14154. npos = node.pos.getc().$scale(node.scale);
  14155. return this.nodeHelper.circle.contains(npos, pos, width, height);
  14156. }
  14157. },
  14158. 'square': {
  14159. 'render': function(node, canvas) {
  14160. var nconfig = this.node,
  14161. dim = node.getData('dim'),
  14162. p = node.pos.getc();
  14163. dim = nconfig.transform? dim * (1 - p.squaredNorm()) : dim;
  14164. p.$scale(node.scale);
  14165. if (dim > 0.2) {
  14166. this.nodeHelper.square.render('fill', p, dim, canvas);
  14167. }
  14168. },
  14169. 'contains': function(node, pos) {
  14170. var dim = node.getData('dim'),
  14171. npos = node.pos.getc().$scale(node.scale);
  14172. return this.nodeHelper.square.contains(npos, pos, dim);
  14173. }
  14174. },
  14175. 'rectangle': {
  14176. 'render': function(node, canvas) {
  14177. var nconfig = this.node,
  14178. width = node.getData('width'),
  14179. height = node.getData('height'),
  14180. pos = node.pos.getc();
  14181. width = nconfig.transform? width * (1 - pos.squaredNorm()) : width;
  14182. height = nconfig.transform? height * (1 - pos.squaredNorm()) : height;
  14183. pos.$scale(node.scale);
  14184. if (width > 0.2 && height > 0.2) {
  14185. this.nodeHelper.rectangle.render('fill', pos, width, height, canvas);
  14186. }
  14187. },
  14188. 'contains': function(node, pos) {
  14189. var width = node.getData('width'),
  14190. height = node.getData('height'),
  14191. npos = node.pos.getc().$scale(node.scale);
  14192. return this.nodeHelper.rectangle.contains(npos, pos, width, height);
  14193. }
  14194. },
  14195. 'triangle': {
  14196. 'render': function(node, canvas) {
  14197. var nconfig = this.node,
  14198. dim = node.getData('dim'),
  14199. p = node.pos.getc();
  14200. dim = nconfig.transform? dim * (1 - p.squaredNorm()) : dim;
  14201. p.$scale(node.scale);
  14202. if (dim > 0.2) {
  14203. this.nodeHelper.triangle.render('fill', p, dim, canvas);
  14204. }
  14205. },
  14206. 'contains': function(node, pos) {
  14207. var dim = node.getData('dim'),
  14208. npos = node.pos.getc().$scale(node.scale);
  14209. return this.nodeHelper.triangle.contains(npos, pos, dim);
  14210. }
  14211. },
  14212. 'star': {
  14213. 'render': function(node, canvas) {
  14214. var nconfig = this.node,
  14215. dim = node.getData('dim'),
  14216. p = node.pos.getc();
  14217. dim = nconfig.transform? dim * (1 - p.squaredNorm()) : dim;
  14218. p.$scale(node.scale);
  14219. if (dim > 0.2) {
  14220. this.nodeHelper.star.render('fill', p, dim, canvas);
  14221. }
  14222. },
  14223. 'contains': function(node, pos) {
  14224. var dim = node.getData('dim'),
  14225. npos = node.pos.getc().$scale(node.scale);
  14226. return this.nodeHelper.star.contains(npos, pos, dim);
  14227. }
  14228. }
  14229. });
  14230. /*
  14231. Class: Hypertree.Plot.EdgeTypes
  14232. This class contains a list of <Graph.Adjacence> built-in types.
  14233. Edge types implemented are 'none', 'line', 'arrow' and 'hyperline'.
  14234. You can add your custom edge types, customizing your visualization to the extreme.
  14235. Example:
  14236. (start code js)
  14237. Hypertree.Plot.EdgeTypes.implement({
  14238. 'mySpecialType': {
  14239. 'render': function(adj, canvas) {
  14240. //print your custom edge to canvas
  14241. },
  14242. //optional
  14243. 'contains': function(adj, pos) {
  14244. //return true if pos is inside the arc or false otherwise
  14245. }
  14246. }
  14247. });
  14248. (end code)
  14249. */
  14250. Hypertree.Plot.EdgeTypes = new Class({
  14251. 'none': $.empty,
  14252. 'line': {
  14253. 'render': function(adj, canvas) {
  14254. var from = adj.nodeFrom.pos.getc(true),
  14255. to = adj.nodeTo.pos.getc(true),
  14256. r = adj.nodeFrom.scale;
  14257. this.edgeHelper.line.render({x:from.x*r, y:from.y*r}, {x:to.x*r, y:to.y*r}, canvas);
  14258. },
  14259. 'contains': function(adj, pos) {
  14260. var from = adj.nodeFrom.pos.getc(true),
  14261. to = adj.nodeTo.pos.getc(true),
  14262. r = adj.nodeFrom.scale;
  14263. this.edgeHelper.line.contains({x:from.x*r, y:from.y*r}, {x:to.x*r, y:to.y*r}, pos, this.edge.epsilon);
  14264. }
  14265. },
  14266. 'arrow': {
  14267. 'render': function(adj, canvas) {
  14268. var from = adj.nodeFrom.pos.getc(true),
  14269. to = adj.nodeTo.pos.getc(true),
  14270. r = adj.nodeFrom.scale,
  14271. dim = adj.getData('dim'),
  14272. direction = adj.data.$direction,
  14273. inv = (direction && direction.length>1 && direction[0] != adj.nodeFrom.id);
  14274. this.edgeHelper.arrow.render({x:from.x*r, y:from.y*r}, {x:to.x*r, y:to.y*r}, dim, inv, canvas);
  14275. },
  14276. 'contains': function(adj, pos) {
  14277. var from = adj.nodeFrom.pos.getc(true),
  14278. to = adj.nodeTo.pos.getc(true),
  14279. r = adj.nodeFrom.scale;
  14280. this.edgeHelper.arrow.contains({x:from.x*r, y:from.y*r}, {x:to.x*r, y:to.y*r}, pos, this.edge.epsilon);
  14281. }
  14282. },
  14283. 'hyperline': {
  14284. 'render': function(adj, canvas) {
  14285. var from = adj.nodeFrom.pos.getc(),
  14286. to = adj.nodeTo.pos.getc(),
  14287. dim = this.viz.getRadius();
  14288. this.edgeHelper.hyperline.render(from, to, dim, canvas);
  14289. },
  14290. 'contains': $.lambda(false)
  14291. }
  14292. });
  14293. })($jit.Hypertree);
  14294. })();