d3.js 249 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373
  1. /*
  2. Copyright (c) 2013, Michael Bostock
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice, this
  7. list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * The name Michael Bostock may not be used to endorse or promote products
  12. derived from this software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT,
  17. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  19. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  20. OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  22. EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. (function(){if (!Date.now) Date.now = function() {
  25. return +new Date;
  26. };
  27. try {
  28. document.createElement("div").style.setProperty("opacity", 0, "");
  29. } catch (error) {
  30. var d3_style_prototype = CSSStyleDeclaration.prototype,
  31. d3_style_setProperty = d3_style_prototype.setProperty;
  32. d3_style_prototype.setProperty = function(name, value, priority) {
  33. d3_style_setProperty.call(this, name, value + "", priority);
  34. };
  35. }
  36. d3 = {version: "2.8.1"}; // semver
  37. function d3_class(ctor, properties) {
  38. try {
  39. for (var key in properties) {
  40. Object.defineProperty(ctor.prototype, key, {
  41. value: properties[key],
  42. enumerable: false
  43. });
  44. }
  45. } catch (e) {
  46. ctor.prototype = properties;
  47. }
  48. }
  49. var d3_array = d3_arraySlice; // conversion for NodeLists
  50. function d3_arrayCopy(pseudoarray) {
  51. var i = -1, n = pseudoarray.length, array = [];
  52. while (++i < n) array.push(pseudoarray[i]);
  53. return array;
  54. }
  55. function d3_arraySlice(pseudoarray) {
  56. return Array.prototype.slice.call(pseudoarray);
  57. }
  58. try {
  59. d3_array(document.documentElement.childNodes)[0].nodeType;
  60. } catch(e) {
  61. d3_array = d3_arrayCopy;
  62. }
  63. var d3_arraySubclass = [].__proto__?
  64. // Until ECMAScript supports array subclassing, prototype injection works well.
  65. function(array, prototype) {
  66. array.__proto__ = prototype;
  67. }:
  68. // And if your browser doesn't support __proto__, we'll use direct extension.
  69. function(array, prototype) {
  70. for (var property in prototype) array[property] = prototype[property];
  71. };
  72. d3.map = function(object) {
  73. var map = new d3_Map;
  74. for (var key in object) map.set(key, object[key]);
  75. return map;
  76. };
  77. function d3_Map() {}
  78. d3_class(d3_Map, {
  79. has: function(key) {
  80. return d3_map_prefix + key in this;
  81. },
  82. get: function(key) {
  83. return this[d3_map_prefix + key];
  84. },
  85. set: function(key, value) {
  86. return this[d3_map_prefix + key] = value;
  87. },
  88. remove: function(key) {
  89. key = d3_map_prefix + key;
  90. return key in this && delete this[key];
  91. },
  92. keys: function() {
  93. var keys = [];
  94. this.forEach(function(key) { keys.push(key); });
  95. return keys;
  96. },
  97. values: function() {
  98. var values = [];
  99. this.forEach(function(key, value) { values.push(value); });
  100. return values;
  101. },
  102. entries: function() {
  103. var entries = [];
  104. this.forEach(function(key, value) { entries.push({key: key, value: value}); });
  105. return entries;
  106. },
  107. forEach: function(f) {
  108. for (var key in this) {
  109. if (key.charCodeAt(0) === d3_map_prefixCode) {
  110. f.call(this, key.substring(1), this[key]);
  111. }
  112. }
  113. }
  114. });
  115. var d3_map_prefix = "\0", // prevent collision with built-ins
  116. d3_map_prefixCode = d3_map_prefix.charCodeAt(0);
  117. function d3_this() {
  118. return this;
  119. }
  120. d3.functor = function(v) {
  121. return typeof v === "function" ? v : function() { return v; };
  122. };
  123. // Copies a variable number of methods from source to target.
  124. d3.rebind = function(target, source) {
  125. var i = 1, n = arguments.length, method;
  126. while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]);
  127. return target;
  128. };
  129. // Method is assumed to be a standard D3 getter-setter:
  130. // If passed with no arguments, gets the value.
  131. // If passed with arguments, sets the value and returns the target.
  132. function d3_rebind(target, source, method) {
  133. return function() {
  134. var value = method.apply(source, arguments);
  135. return arguments.length ? target : value;
  136. };
  137. }
  138. d3.ascending = function(a, b) {
  139. return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
  140. };
  141. d3.descending = function(a, b) {
  142. return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN;
  143. };
  144. d3.mean = function(array, f) {
  145. var n = array.length,
  146. a,
  147. m = 0,
  148. i = -1,
  149. j = 0;
  150. if (arguments.length === 1) {
  151. while (++i < n) if (d3_number(a = array[i])) m += (a - m) / ++j;
  152. } else {
  153. while (++i < n) if (d3_number(a = f.call(array, array[i], i))) m += (a - m) / ++j;
  154. }
  155. return j ? m : undefined;
  156. };
  157. d3.median = function(array, f) {
  158. if (arguments.length > 1) array = array.map(f);
  159. array = array.filter(d3_number);
  160. return array.length ? d3.quantile(array.sort(d3.ascending), .5) : undefined;
  161. };
  162. d3.min = function(array, f) {
  163. var i = -1,
  164. n = array.length,
  165. a,
  166. b;
  167. if (arguments.length === 1) {
  168. while (++i < n && ((a = array[i]) == null || a != a)) a = undefined;
  169. while (++i < n) if ((b = array[i]) != null && a > b) a = b;
  170. } else {
  171. while (++i < n && ((a = f.call(array, array[i], i)) == null || a != a)) a = undefined;
  172. while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;
  173. }
  174. return a;
  175. };
  176. d3.max = function(array, f) {
  177. var i = -1,
  178. n = array.length,
  179. a,
  180. b;
  181. if (arguments.length === 1) {
  182. while (++i < n && ((a = array[i]) == null || a != a)) a = undefined;
  183. while (++i < n) if ((b = array[i]) != null && b > a) a = b;
  184. } else {
  185. while (++i < n && ((a = f.call(array, array[i], i)) == null || a != a)) a = undefined;
  186. while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b;
  187. }
  188. return a;
  189. };
  190. d3.extent = function(array, f) {
  191. var i = -1,
  192. n = array.length,
  193. a,
  194. b,
  195. c;
  196. if (arguments.length === 1) {
  197. while (++i < n && ((a = c = array[i]) == null || a != a)) a = c = undefined;
  198. while (++i < n) if ((b = array[i]) != null) {
  199. if (a > b) a = b;
  200. if (c < b) c = b;
  201. }
  202. } else {
  203. while (++i < n && ((a = c = f.call(array, array[i], i)) == null || a != a)) a = undefined;
  204. while (++i < n) if ((b = f.call(array, array[i], i)) != null) {
  205. if (a > b) a = b;
  206. if (c < b) c = b;
  207. }
  208. }
  209. return [a, c];
  210. };
  211. d3.random = {
  212. normal: function(mean, deviation) {
  213. if (arguments.length < 2) deviation = 1;
  214. if (arguments.length < 1) mean = 0;
  215. return function() {
  216. var x, y, r;
  217. do {
  218. x = Math.random() * 2 - 1;
  219. y = Math.random() * 2 - 1;
  220. r = x * x + y * y;
  221. } while (!r || r > 1);
  222. return mean + deviation * x * Math.sqrt(-2 * Math.log(r) / r);
  223. };
  224. }
  225. };
  226. function d3_number(x) {
  227. return x != null && !isNaN(x);
  228. }
  229. d3.sum = function(array, f) {
  230. var s = 0,
  231. n = array.length,
  232. a,
  233. i = -1;
  234. if (arguments.length === 1) {
  235. while (++i < n) if (!isNaN(a = +array[i])) s += a;
  236. } else {
  237. while (++i < n) if (!isNaN(a = +f.call(array, array[i], i))) s += a;
  238. }
  239. return s;
  240. };
  241. // R-7 per <http://en.wikipedia.org/wiki/Quantile>
  242. d3.quantile = function(values, p) {
  243. var H = (values.length - 1) * p + 1,
  244. h = Math.floor(H),
  245. v = values[h - 1],
  246. e = H - h;
  247. return e ? v + e * (values[h] - v) : v;
  248. };
  249. d3.transpose = function(matrix) {
  250. return d3.zip.apply(d3, matrix);
  251. };
  252. d3.zip = function() {
  253. if (!(n = arguments.length)) return [];
  254. for (var i = -1, m = d3.min(arguments, d3_zipLength), zips = new Array(m); ++i < m;) {
  255. for (var j = -1, n, zip = zips[i] = new Array(n); ++j < n;) {
  256. zip[j] = arguments[j][i];
  257. }
  258. }
  259. return zips;
  260. };
  261. function d3_zipLength(d) {
  262. return d.length;
  263. }
  264. d3.bisector = function(f) {
  265. return {
  266. left: function(a, x, lo, hi) {
  267. if (arguments.length < 3) lo = 0;
  268. if (arguments.length < 4) hi = a.length;
  269. while (lo < hi) {
  270. var mid = lo + hi >> 1;
  271. if (f.call(a, a[mid], mid) < x) lo = mid + 1;
  272. else hi = mid;
  273. }
  274. return lo;
  275. },
  276. right: function(a, x, lo, hi) {
  277. if (arguments.length < 3) lo = 0;
  278. if (arguments.length < 4) hi = a.length;
  279. while (lo < hi) {
  280. var mid = lo + hi >> 1;
  281. if (x < f.call(a, a[mid], mid)) hi = mid;
  282. else lo = mid + 1;
  283. }
  284. return lo;
  285. }
  286. };
  287. };
  288. var d3_bisector = d3.bisector(function(d) { return d; });
  289. d3.bisectLeft = d3_bisector.left;
  290. d3.bisect = d3.bisectRight = d3_bisector.right;
  291. d3.first = function(array, f) {
  292. var i = 0,
  293. n = array.length,
  294. a = array[0],
  295. b;
  296. if (arguments.length === 1) f = d3.ascending;
  297. while (++i < n) {
  298. if (f.call(array, a, b = array[i]) > 0) {
  299. a = b;
  300. }
  301. }
  302. return a;
  303. };
  304. d3.last = function(array, f) {
  305. var i = 0,
  306. n = array.length,
  307. a = array[0],
  308. b;
  309. if (arguments.length === 1) f = d3.ascending;
  310. while (++i < n) {
  311. if (f.call(array, a, b = array[i]) <= 0) {
  312. a = b;
  313. }
  314. }
  315. return a;
  316. };
  317. d3.nest = function() {
  318. var nest = {},
  319. keys = [],
  320. sortKeys = [],
  321. sortValues,
  322. rollup;
  323. function map(array, depth) {
  324. if (depth >= keys.length) return rollup
  325. ? rollup.call(nest, array) : (sortValues
  326. ? array.sort(sortValues)
  327. : array);
  328. var i = -1,
  329. n = array.length,
  330. key = keys[depth++],
  331. keyValue,
  332. object,
  333. valuesByKey = new d3_Map,
  334. values,
  335. o = {};
  336. while (++i < n) {
  337. if (values = valuesByKey.get(keyValue = key(object = array[i]))) {
  338. values.push(object);
  339. } else {
  340. valuesByKey.set(keyValue, [object]);
  341. }
  342. }
  343. valuesByKey.forEach(function(keyValue) {
  344. o[keyValue] = map(valuesByKey.get(keyValue), depth);
  345. });
  346. return o;
  347. }
  348. function entries(map, depth) {
  349. if (depth >= keys.length) return map;
  350. var a = [],
  351. sortKey = sortKeys[depth++],
  352. key;
  353. for (key in map) {
  354. a.push({key: key, values: entries(map[key], depth)});
  355. }
  356. if (sortKey) a.sort(function(a, b) {
  357. return sortKey(a.key, b.key);
  358. });
  359. return a;
  360. }
  361. nest.map = function(array) {
  362. return map(array, 0);
  363. };
  364. nest.entries = function(array) {
  365. return entries(map(array, 0), 0);
  366. };
  367. nest.key = function(d) {
  368. keys.push(d);
  369. return nest;
  370. };
  371. // Specifies the order for the most-recently specified key.
  372. // Note: only applies to entries. Map keys are unordered!
  373. nest.sortKeys = function(order) {
  374. sortKeys[keys.length - 1] = order;
  375. return nest;
  376. };
  377. // Specifies the order for leaf values.
  378. // Applies to both maps and entries array.
  379. nest.sortValues = function(order) {
  380. sortValues = order;
  381. return nest;
  382. };
  383. nest.rollup = function(f) {
  384. rollup = f;
  385. return nest;
  386. };
  387. return nest;
  388. };
  389. d3.keys = function(map) {
  390. var keys = [];
  391. for (var key in map) keys.push(key);
  392. return keys;
  393. };
  394. d3.values = function(map) {
  395. var values = [];
  396. for (var key in map) values.push(map[key]);
  397. return values;
  398. };
  399. d3.entries = function(map) {
  400. var entries = [];
  401. for (var key in map) entries.push({key: key, value: map[key]});
  402. return entries;
  403. };
  404. d3.permute = function(array, indexes) {
  405. var permutes = [],
  406. i = -1,
  407. n = indexes.length;
  408. while (++i < n) permutes[i] = array[indexes[i]];
  409. return permutes;
  410. };
  411. d3.merge = function(arrays) {
  412. return Array.prototype.concat.apply([], arrays);
  413. };
  414. d3.split = function(array, f) {
  415. var arrays = [],
  416. values = [],
  417. value,
  418. i = -1,
  419. n = array.length;
  420. if (arguments.length < 2) f = d3_splitter;
  421. while (++i < n) {
  422. if (f.call(values, value = array[i], i)) {
  423. values = [];
  424. } else {
  425. if (!values.length) arrays.push(values);
  426. values.push(value);
  427. }
  428. }
  429. return arrays;
  430. };
  431. function d3_splitter(d) {
  432. return d == null;
  433. }
  434. function d3_collapse(s) {
  435. return s.replace(/(^\s+)|(\s+$)/g, "").replace(/\s+/g, " ");
  436. }
  437. d3.range = function(start, stop, step) {
  438. if (arguments.length < 3) {
  439. step = 1;
  440. if (arguments.length < 2) {
  441. stop = start;
  442. start = 0;
  443. }
  444. }
  445. if ((stop - start) / step === Infinity) throw new Error("infinite range");
  446. var range = [],
  447. k = d3_range_integerScale(Math.abs(step)),
  448. i = -1,
  449. j;
  450. start *= k, stop *= k, step *= k;
  451. if (step < 0) while ((j = start + step * ++i) > stop) range.push(j / k);
  452. else while ((j = start + step * ++i) < stop) range.push(j / k);
  453. return range;
  454. };
  455. function d3_range_integerScale(x) {
  456. var k = 1;
  457. while (x * k % 1) k *= 10;
  458. return k;
  459. }
  460. d3.requote = function(s) {
  461. return s.replace(d3_requote_re, "\\$&");
  462. };
  463. var d3_requote_re = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;
  464. d3.round = function(x, n) {
  465. return n
  466. ? Math.round(x * (n = Math.pow(10, n))) / n
  467. : Math.round(x);
  468. };
  469. d3.xhr = function(url, mime, callback) {
  470. var req = new XMLHttpRequest;
  471. if (arguments.length < 3) callback = mime, mime = null;
  472. else if (mime && req.overrideMimeType) req.overrideMimeType(mime);
  473. req.open("GET", url, true);
  474. if (mime) req.setRequestHeader("Accept", mime);
  475. req.onreadystatechange = function() {
  476. if (req.readyState === 4) callback(req.status < 300 ? req : null);
  477. };
  478. req.send(null);
  479. };
  480. d3.text = function(url, mime, callback) {
  481. function ready(req) {
  482. callback(req && req.responseText);
  483. }
  484. if (arguments.length < 3) {
  485. callback = mime;
  486. mime = null;
  487. }
  488. d3.xhr(url, mime, ready);
  489. };
  490. d3.json = function(url, callback) {
  491. d3.text(url, "application/json", function(text) {
  492. callback(text ? JSON.parse(text) : null);
  493. });
  494. };
  495. d3.html = function(url, callback) {
  496. d3.text(url, "text/html", function(text) {
  497. if (text != null) { // Treat empty string as valid HTML.
  498. var range = document.createRange();
  499. range.selectNode(document.body);
  500. text = range.createContextualFragment(text);
  501. }
  502. callback(text);
  503. });
  504. };
  505. d3.xml = function(url, mime, callback) {
  506. function ready(req) {
  507. callback(req && req.responseXML);
  508. }
  509. if (arguments.length < 3) {
  510. callback = mime;
  511. mime = null;
  512. }
  513. d3.xhr(url, mime, ready);
  514. };
  515. var d3_nsPrefix = {
  516. svg: "http://www.w3.org/2000/svg",
  517. xhtml: "http://www.w3.org/1999/xhtml",
  518. xlink: "http://www.w3.org/1999/xlink",
  519. xml: "http://www.w3.org/XML/1998/namespace",
  520. xmlns: "http://www.w3.org/2000/xmlns/"
  521. };
  522. d3.ns = {
  523. prefix: d3_nsPrefix,
  524. qualify: function(name) {
  525. var i = name.indexOf(":"),
  526. prefix = name;
  527. if (i >= 0) {
  528. prefix = name.substring(0, i);
  529. name = name.substring(i + 1);
  530. }
  531. return d3_nsPrefix.hasOwnProperty(prefix)
  532. ? {space: d3_nsPrefix[prefix], local: name}
  533. : name;
  534. }
  535. };
  536. d3.dispatch = function() {
  537. var dispatch = new d3_dispatch,
  538. i = -1,
  539. n = arguments.length;
  540. while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch);
  541. return dispatch;
  542. };
  543. function d3_dispatch() {}
  544. d3_dispatch.prototype.on = function(type, listener) {
  545. var i = type.indexOf("."),
  546. name = "";
  547. // Extract optional namespace, e.g., "click.foo"
  548. if (i > 0) {
  549. name = type.substring(i + 1);
  550. type = type.substring(0, i);
  551. }
  552. return arguments.length < 2
  553. ? this[type].on(name)
  554. : this[type].on(name, listener);
  555. };
  556. function d3_dispatch_event(dispatch) {
  557. var listeners = [],
  558. listenerByName = new d3_Map;
  559. function event() {
  560. var z = listeners, // defensive reference
  561. i = -1,
  562. n = z.length,
  563. l;
  564. while (++i < n) if (l = z[i].on) l.apply(this, arguments);
  565. return dispatch;
  566. }
  567. event.on = function(name, listener) {
  568. var l = listenerByName.get(name),
  569. i;
  570. // return the current listener, if any
  571. if (arguments.length < 2) return l && l.on;
  572. // remove the old listener, if any (with copy-on-write)
  573. if (l) {
  574. l.on = null;
  575. listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1));
  576. listenerByName.remove(name);
  577. }
  578. // add the new listener, if any
  579. if (listener) listeners.push(listenerByName.set(name, {on: listener}));
  580. return dispatch;
  581. };
  582. return event;
  583. }
  584. // TODO align
  585. d3.format = function(specifier) {
  586. var match = d3_format_re.exec(specifier),
  587. fill = match[1] || " ",
  588. sign = match[3] || "",
  589. zfill = match[5],
  590. width = +match[6],
  591. comma = match[7],
  592. precision = match[8],
  593. type = match[9],
  594. scale = 1,
  595. suffix = "",
  596. integer = false;
  597. if (precision) precision = +precision.substring(1);
  598. if (zfill) {
  599. fill = "0"; // TODO align = "=";
  600. if (comma) width -= Math.floor((width - 1) / 4);
  601. }
  602. switch (type) {
  603. case "n": comma = true; type = "g"; break;
  604. case "%": scale = 100; suffix = "%"; type = "f"; break;
  605. case "p": scale = 100; suffix = "%"; type = "r"; break;
  606. case "d": integer = true; precision = 0; break;
  607. case "s": scale = -1; type = "r"; break;
  608. }
  609. // If no precision is specified for r, fallback to general notation.
  610. if (type == "r" && !precision) type = "g";
  611. type = d3_format_types.get(type) || d3_format_typeDefault;
  612. return function(value) {
  613. // Return the empty string for floats formatted as ints.
  614. if (integer && (value % 1)) return "";
  615. // Convert negative to positive, and record the sign prefix.
  616. var negative = (value < 0) && (value = -value) ? "\u2212" : sign;
  617. // Apply the scale, computing it from the value's exponent for si format.
  618. if (scale < 0) {
  619. var prefix = d3.formatPrefix(value, precision);
  620. value *= prefix.scale;
  621. suffix = prefix.symbol;
  622. } else {
  623. value *= scale;
  624. }
  625. // Convert to the desired precision.
  626. value = type(value, precision);
  627. // If the fill character is 0, the sign and group is applied after the fill.
  628. if (zfill) {
  629. var length = value.length + negative.length;
  630. if (length < width) value = new Array(width - length + 1).join(fill) + value;
  631. if (comma) value = d3_format_group(value);
  632. value = negative + value;
  633. }
  634. // Otherwise (e.g., space-filling), the sign and group is applied before.
  635. else {
  636. if (comma) value = d3_format_group(value);
  637. value = negative + value;
  638. var length = value.length;
  639. if (length < width) value = new Array(width - length + 1).join(fill) + value;
  640. }
  641. return value + suffix;
  642. };
  643. };
  644. // [[fill]align][sign][#][0][width][,][.precision][type]
  645. var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?(#)?(0)?([0-9]+)?(,)?(\.[0-9]+)?([a-zA-Z%])?/;
  646. var d3_format_types = d3.map({
  647. g: function(x, p) { return x.toPrecision(p); },
  648. e: function(x, p) { return x.toExponential(p); },
  649. f: function(x, p) { return x.toFixed(p); },
  650. r: function(x, p) { return d3.round(x, p = d3_format_precision(x, p)).toFixed(Math.max(0, Math.min(20, p))); }
  651. });
  652. function d3_format_precision(x, p) {
  653. return p - (x ? 1 + Math.floor(Math.log(x + Math.pow(10, 1 + Math.floor(Math.log(x) / Math.LN10) - p)) / Math.LN10) : 1);
  654. }
  655. function d3_format_typeDefault(x) {
  656. return x + "";
  657. }
  658. // Apply comma grouping for thousands.
  659. function d3_format_group(value) {
  660. var i = value.lastIndexOf("."),
  661. f = i >= 0 ? value.substring(i) : (i = value.length, ""),
  662. t = [];
  663. while (i > 0) t.push(value.substring(i -= 3, i + 3));
  664. return t.reverse().join(",") + f;
  665. }
  666. var d3_formatPrefixes = ["y","z","a","f","p","n","μ","m","","k","M","G","T","P","E","Z","Y"].map(d3_formatPrefix);
  667. d3.formatPrefix = function(value, precision) {
  668. var i = 0;
  669. if (value) {
  670. if (value < 0) value *= -1;
  671. if (precision) value = d3.round(value, d3_format_precision(value, precision));
  672. i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10);
  673. i = Math.max(-24, Math.min(24, Math.floor((i <= 0 ? i + 1 : i - 1) / 3) * 3));
  674. }
  675. return d3_formatPrefixes[8 + i / 3];
  676. };
  677. function d3_formatPrefix(d, i) {
  678. return {
  679. scale: Math.pow(10, (8 - i) * 3),
  680. symbol: d
  681. };
  682. }
  683. /*
  684. * TERMS OF USE - EASING EQUATIONS
  685. *
  686. * Open source under the BSD License.
  687. *
  688. * Copyright 2001 Robert Penner
  689. * All rights reserved.
  690. *
  691. * Redistribution and use in source and binary forms, with or without
  692. * modification, are permitted provided that the following conditions are met:
  693. *
  694. * - Redistributions of source code must retain the above copyright notice, this
  695. * list of conditions and the following disclaimer.
  696. *
  697. * - Redistributions in binary form must reproduce the above copyright notice,
  698. * this list of conditions and the following disclaimer in the documentation
  699. * and/or other materials provided with the distribution.
  700. *
  701. * - Neither the name of the author nor the names of contributors may be used to
  702. * endorse or promote products derived from this software without specific
  703. * prior written permission.
  704. *
  705. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  706. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  707. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  708. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  709. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  710. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  711. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  712. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  713. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  714. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  715. * POSSIBILITY OF SUCH DAMAGE.
  716. */
  717. var d3_ease_quad = d3_ease_poly(2),
  718. d3_ease_cubic = d3_ease_poly(3),
  719. d3_ease_default = function() { return d3_ease_identity; };
  720. var d3_ease = d3.map({
  721. linear: d3_ease_default,
  722. poly: d3_ease_poly,
  723. quad: function() { return d3_ease_quad; },
  724. cubic: function() { return d3_ease_cubic; },
  725. sin: function() { return d3_ease_sin; },
  726. exp: function() { return d3_ease_exp; },
  727. circle: function() { return d3_ease_circle; },
  728. elastic: d3_ease_elastic,
  729. back: d3_ease_back,
  730. bounce: function() { return d3_ease_bounce; }
  731. });
  732. var d3_ease_mode = d3.map({
  733. "in": d3_ease_identity,
  734. "out": d3_ease_reverse,
  735. "in-out": d3_ease_reflect,
  736. "out-in": function(f) { return d3_ease_reflect(d3_ease_reverse(f)); }
  737. });
  738. d3.ease = function(name) {
  739. var i = name.indexOf("-"),
  740. t = i >= 0 ? name.substring(0, i) : name,
  741. m = i >= 0 ? name.substring(i + 1) : "in";
  742. t = d3_ease.get(t) || d3_ease_default;
  743. m = d3_ease_mode.get(m) || d3_ease_identity;
  744. return d3_ease_clamp(m(t.apply(null, Array.prototype.slice.call(arguments, 1))));
  745. };
  746. function d3_ease_clamp(f) {
  747. return function(t) {
  748. return t <= 0 ? 0 : t >= 1 ? 1 : f(t);
  749. };
  750. }
  751. function d3_ease_reverse(f) {
  752. return function(t) {
  753. return 1 - f(1 - t);
  754. };
  755. }
  756. function d3_ease_reflect(f) {
  757. return function(t) {
  758. return .5 * (t < .5 ? f(2 * t) : (2 - f(2 - 2 * t)));
  759. };
  760. }
  761. function d3_ease_identity(t) {
  762. return t;
  763. }
  764. function d3_ease_poly(e) {
  765. return function(t) {
  766. return Math.pow(t, e);
  767. };
  768. }
  769. function d3_ease_sin(t) {
  770. return 1 - Math.cos(t * Math.PI / 2);
  771. }
  772. function d3_ease_exp(t) {
  773. return Math.pow(2, 10 * (t - 1));
  774. }
  775. function d3_ease_circle(t) {
  776. return 1 - Math.sqrt(1 - t * t);
  777. }
  778. function d3_ease_elastic(a, p) {
  779. var s;
  780. if (arguments.length < 2) p = 0.45;
  781. if (arguments.length < 1) { a = 1; s = p / 4; }
  782. else s = p / (2 * Math.PI) * Math.asin(1 / a);
  783. return function(t) {
  784. return 1 + a * Math.pow(2, 10 * -t) * Math.sin((t - s) * 2 * Math.PI / p);
  785. };
  786. }
  787. function d3_ease_back(s) {
  788. if (!s) s = 1.70158;
  789. return function(t) {
  790. return t * t * ((s + 1) * t - s);
  791. };
  792. }
  793. function d3_ease_bounce(t) {
  794. return t < 1 / 2.75 ? 7.5625 * t * t
  795. : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75
  796. : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375
  797. : 7.5625 * (t -= 2.625 / 2.75) * t + .984375;
  798. }
  799. d3.event = null;
  800. function d3_eventCancel() {
  801. d3.event.stopPropagation();
  802. d3.event.preventDefault();
  803. }
  804. function d3_eventSource() {
  805. var e = d3.event, s;
  806. while (s = e.sourceEvent) e = s;
  807. return e;
  808. }
  809. // Like d3.dispatch, but for custom events abstracting native UI events. These
  810. // events have a target component (such as a brush), a target element (such as
  811. // the svg:g element containing the brush) and the standard arguments `d` (the
  812. // target element's data) and `i` (the selection index of the target element).
  813. function d3_eventDispatch(target) {
  814. var dispatch = new d3_dispatch,
  815. i = 0,
  816. n = arguments.length;
  817. while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch);
  818. // Creates a dispatch context for the specified `thiz` (typically, the target
  819. // DOM element that received the source event) and `argumentz` (typically, the
  820. // data `d` and index `i` of the target element). The returned function can be
  821. // used to dispatch an event to any registered listeners; the function takes a
  822. // single argument as input, being the event to dispatch. The event must have
  823. // a "type" attribute which corresponds to a type registered in the
  824. // constructor. This context will automatically populate the "sourceEvent" and
  825. // "target" attributes of the event, as well as setting the `d3.event` global
  826. // for the duration of the notification.
  827. dispatch.of = function(thiz, argumentz) {
  828. return function(e1) {
  829. try {
  830. var e0 =
  831. e1.sourceEvent = d3.event;
  832. e1.target = target;
  833. d3.event = e1;
  834. dispatch[e1.type].apply(thiz, argumentz);
  835. } finally {
  836. d3.event = e0;
  837. }
  838. };
  839. };
  840. return dispatch;
  841. }
  842. d3.interpolate = function(a, b) {
  843. var i = d3.interpolators.length, f;
  844. while (--i >= 0 && !(f = d3.interpolators[i](a, b)));
  845. return f;
  846. };
  847. d3.interpolateNumber = function(a, b) {
  848. b -= a;
  849. return function(t) { return a + b * t; };
  850. };
  851. d3.interpolateRound = function(a, b) {
  852. b -= a;
  853. return function(t) { return Math.round(a + b * t); };
  854. };
  855. d3.interpolateString = function(a, b) {
  856. var m, // current match
  857. i, // current index
  858. j, // current index (for coallescing)
  859. s0 = 0, // start index of current string prefix
  860. s1 = 0, // end index of current string prefix
  861. s = [], // string constants and placeholders
  862. q = [], // number interpolators
  863. n, // q.length
  864. o;
  865. // Reset our regular expression!
  866. d3_interpolate_number.lastIndex = 0;
  867. // Find all numbers in b.
  868. for (i = 0; m = d3_interpolate_number.exec(b); ++i) {
  869. if (m.index) s.push(b.substring(s0, s1 = m.index));
  870. q.push({i: s.length, x: m[0]});
  871. s.push(null);
  872. s0 = d3_interpolate_number.lastIndex;
  873. }
  874. if (s0 < b.length) s.push(b.substring(s0));
  875. // Find all numbers in a.
  876. for (i = 0, n = q.length; (m = d3_interpolate_number.exec(a)) && i < n; ++i) {
  877. o = q[i];
  878. if (o.x == m[0]) { // The numbers match, so coallesce.
  879. if (o.i) {
  880. if (s[o.i + 1] == null) { // This match is followed by another number.
  881. s[o.i - 1] += o.x;
  882. s.splice(o.i, 1);
  883. for (j = i + 1; j < n; ++j) q[j].i--;
  884. } else { // This match is followed by a string, so coallesce twice.
  885. s[o.i - 1] += o.x + s[o.i + 1];
  886. s.splice(o.i, 2);
  887. for (j = i + 1; j < n; ++j) q[j].i -= 2;
  888. }
  889. } else {
  890. if (s[o.i + 1] == null) { // This match is followed by another number.
  891. s[o.i] = o.x;
  892. } else { // This match is followed by a string, so coallesce twice.
  893. s[o.i] = o.x + s[o.i + 1];
  894. s.splice(o.i + 1, 1);
  895. for (j = i + 1; j < n; ++j) q[j].i--;
  896. }
  897. }
  898. q.splice(i, 1);
  899. n--;
  900. i--;
  901. } else {
  902. o.x = d3.interpolateNumber(parseFloat(m[0]), parseFloat(o.x));
  903. }
  904. }
  905. // Remove any numbers in b not found in a.
  906. while (i < n) {
  907. o = q.pop();
  908. if (s[o.i + 1] == null) { // This match is followed by another number.
  909. s[o.i] = o.x;
  910. } else { // This match is followed by a string, so coallesce twice.
  911. s[o.i] = o.x + s[o.i + 1];
  912. s.splice(o.i + 1, 1);
  913. }
  914. n--;
  915. }
  916. // Special optimization for only a single match.
  917. if (s.length === 1) {
  918. return s[0] == null ? q[0].x : function() { return b; };
  919. }
  920. // Otherwise, interpolate each of the numbers and rejoin the string.
  921. return function(t) {
  922. for (i = 0; i < n; ++i) s[(o = q[i]).i] = o.x(t);
  923. return s.join("");
  924. };
  925. };
  926. d3.interpolateTransform = function(a, b) {
  927. var s = [], // string constants and placeholders
  928. q = [], // number interpolators
  929. n,
  930. A = d3.transform(a),
  931. B = d3.transform(b),
  932. ta = A.translate,
  933. tb = B.translate,
  934. ra = A.rotate,
  935. rb = B.rotate,
  936. wa = A.skew,
  937. wb = B.skew,
  938. ka = A.scale,
  939. kb = B.scale;
  940. if (ta[0] != tb[0] || ta[1] != tb[1]) {
  941. s.push("translate(", null, ",", null, ")");
  942. q.push({i: 1, x: d3.interpolateNumber(ta[0], tb[0])}, {i: 3, x: d3.interpolateNumber(ta[1], tb[1])});
  943. } else if (tb[0] || tb[1]) {
  944. s.push("translate(" + tb + ")");
  945. } else {
  946. s.push("");
  947. }
  948. if (ra != rb) {
  949. q.push({i: s.push(s.pop() + "rotate(", null, ")") - 2, x: d3.interpolateNumber(ra, rb)});
  950. } else if (rb) {
  951. s.push(s.pop() + "rotate(" + rb + ")");
  952. }
  953. if (wa != wb) {
  954. q.push({i: s.push(s.pop() + "skewX(", null, ")") - 2, x: d3.interpolateNumber(wa, wb)});
  955. } else if (wb) {
  956. s.push(s.pop() + "skewX(" + wb + ")");
  957. }
  958. if (ka[0] != kb[0] || ka[1] != kb[1]) {
  959. n = s.push(s.pop() + "scale(", null, ",", null, ")");
  960. q.push({i: n - 4, x: d3.interpolateNumber(ka[0], kb[0])}, {i: n - 2, x: d3.interpolateNumber(ka[1], kb[1])});
  961. } else if (kb[0] != 1 || kb[1] != 1) {
  962. s.push(s.pop() + "scale(" + kb + ")");
  963. }
  964. n = q.length;
  965. return function(t) {
  966. var i = -1, o;
  967. while (++i < n) s[(o = q[i]).i] = o.x(t);
  968. return s.join("");
  969. };
  970. };
  971. d3.interpolateRgb = function(a, b) {
  972. a = d3.rgb(a);
  973. b = d3.rgb(b);
  974. var ar = a.r,
  975. ag = a.g,
  976. ab = a.b,
  977. br = b.r - ar,
  978. bg = b.g - ag,
  979. bb = b.b - ab;
  980. return function(t) {
  981. return "#"
  982. + d3_rgb_hex(Math.round(ar + br * t))
  983. + d3_rgb_hex(Math.round(ag + bg * t))
  984. + d3_rgb_hex(Math.round(ab + bb * t));
  985. };
  986. };
  987. // interpolates HSL space, but outputs RGB string (for compatibility)
  988. d3.interpolateHsl = function(a, b) {
  989. a = d3.hsl(a);
  990. b = d3.hsl(b);
  991. var h0 = a.h,
  992. s0 = a.s,
  993. l0 = a.l,
  994. h1 = b.h - h0,
  995. s1 = b.s - s0,
  996. l1 = b.l - l0;
  997. return function(t) {
  998. return d3_hsl_rgb(h0 + h1 * t, s0 + s1 * t, l0 + l1 * t).toString();
  999. };
  1000. };
  1001. d3.interpolateArray = function(a, b) {
  1002. var x = [],
  1003. c = [],
  1004. na = a.length,
  1005. nb = b.length,
  1006. n0 = Math.min(a.length, b.length),
  1007. i;
  1008. for (i = 0; i < n0; ++i) x.push(d3.interpolate(a[i], b[i]));
  1009. for (; i < na; ++i) c[i] = a[i];
  1010. for (; i < nb; ++i) c[i] = b[i];
  1011. return function(t) {
  1012. for (i = 0; i < n0; ++i) c[i] = x[i](t);
  1013. return c;
  1014. };
  1015. };
  1016. d3.interpolateObject = function(a, b) {
  1017. var i = {},
  1018. c = {},
  1019. k;
  1020. for (k in a) {
  1021. if (k in b) {
  1022. i[k] = d3_interpolateByName(k)(a[k], b[k]);
  1023. } else {
  1024. c[k] = a[k];
  1025. }
  1026. }
  1027. for (k in b) {
  1028. if (!(k in a)) {
  1029. c[k] = b[k];
  1030. }
  1031. }
  1032. return function(t) {
  1033. for (k in i) c[k] = i[k](t);
  1034. return c;
  1035. };
  1036. }
  1037. var d3_interpolate_number = /[-+]?(?:\d*\.?\d+)(?:[eE][-+]?\d+)?/g;
  1038. function d3_interpolateByName(n) {
  1039. return n == "transform"
  1040. ? d3.interpolateTransform
  1041. : d3.interpolate;
  1042. }
  1043. d3.interpolators = [
  1044. d3.interpolateObject,
  1045. function(a, b) { return (b instanceof Array) && d3.interpolateArray(a, b); },
  1046. function(a, b) { return (typeof a === "string" || typeof b === "string") && d3.interpolateString(a + "", b + ""); },
  1047. function(a, b) { return (typeof b === "string" ? d3_rgb_names.has(b) || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a, b); },
  1048. function(a, b) { return !isNaN(a = +a) && !isNaN(b = +b) && d3.interpolateNumber(a, b); }
  1049. ];
  1050. function d3_uninterpolateNumber(a, b) {
  1051. b = b - (a = +a) ? 1 / (b - a) : 0;
  1052. return function(x) { return (x - a) * b; };
  1053. }
  1054. function d3_uninterpolateClamp(a, b) {
  1055. b = b - (a = +a) ? 1 / (b - a) : 0;
  1056. return function(x) { return Math.max(0, Math.min(1, (x - a) * b)); };
  1057. }
  1058. d3.rgb = function(r, g, b) {
  1059. return arguments.length === 1
  1060. ? (r instanceof d3_Rgb ? d3_rgb(r.r, r.g, r.b)
  1061. : d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb))
  1062. : d3_rgb(~~r, ~~g, ~~b);
  1063. };
  1064. function d3_rgb(r, g, b) {
  1065. return new d3_Rgb(r, g, b);
  1066. }
  1067. function d3_Rgb(r, g, b) {
  1068. this.r = r;
  1069. this.g = g;
  1070. this.b = b;
  1071. }
  1072. d3_Rgb.prototype.brighter = function(k) {
  1073. k = Math.pow(0.7, arguments.length ? k : 1);
  1074. var r = this.r,
  1075. g = this.g,
  1076. b = this.b,
  1077. i = 30;
  1078. if (!r && !g && !b) return d3_rgb(i, i, i);
  1079. if (r && r < i) r = i;
  1080. if (g && g < i) g = i;
  1081. if (b && b < i) b = i;
  1082. return d3_rgb(
  1083. Math.min(255, Math.floor(r / k)),
  1084. Math.min(255, Math.floor(g / k)),
  1085. Math.min(255, Math.floor(b / k)));
  1086. };
  1087. d3_Rgb.prototype.darker = function(k) {
  1088. k = Math.pow(0.7, arguments.length ? k : 1);
  1089. return d3_rgb(
  1090. Math.floor(k * this.r),
  1091. Math.floor(k * this.g),
  1092. Math.floor(k * this.b));
  1093. };
  1094. d3_Rgb.prototype.hsl = function() {
  1095. return d3_rgb_hsl(this.r, this.g, this.b);
  1096. };
  1097. d3_Rgb.prototype.toString = function() {
  1098. return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b);
  1099. };
  1100. function d3_rgb_hex(v) {
  1101. return v < 0x10
  1102. ? "0" + Math.max(0, v).toString(16)
  1103. : Math.min(255, v).toString(16);
  1104. }
  1105. function d3_rgb_parse(format, rgb, hsl) {
  1106. var r = 0, // red channel; int in [0, 255]
  1107. g = 0, // green channel; int in [0, 255]
  1108. b = 0, // blue channel; int in [0, 255]
  1109. m1, // CSS color specification match
  1110. m2, // CSS color specification type (e.g., rgb)
  1111. name;
  1112. /* Handle hsl, rgb. */
  1113. m1 = /([a-z]+)\((.*)\)/i.exec(format);
  1114. if (m1) {
  1115. m2 = m1[2].split(",");
  1116. switch (m1[1]) {
  1117. case "hsl": {
  1118. return hsl(
  1119. parseFloat(m2[0]), // degrees
  1120. parseFloat(m2[1]) / 100, // percentage
  1121. parseFloat(m2[2]) / 100 // percentage
  1122. );
  1123. }
  1124. case "rgb": {
  1125. return rgb(
  1126. d3_rgb_parseNumber(m2[0]),
  1127. d3_rgb_parseNumber(m2[1]),
  1128. d3_rgb_parseNumber(m2[2])
  1129. );
  1130. }
  1131. }
  1132. }
  1133. /* Named colors. */
  1134. if (name = d3_rgb_names.get(format)) return rgb(name.r, name.g, name.b);
  1135. /* Hexadecimal colors: #rgb and #rrggbb. */
  1136. if (format != null && format.charAt(0) === "#") {
  1137. if (format.length === 4) {
  1138. r = format.charAt(1); r += r;
  1139. g = format.charAt(2); g += g;
  1140. b = format.charAt(3); b += b;
  1141. } else if (format.length === 7) {
  1142. r = format.substring(1, 3);
  1143. g = format.substring(3, 5);
  1144. b = format.substring(5, 7);
  1145. }
  1146. r = parseInt(r, 16);
  1147. g = parseInt(g, 16);
  1148. b = parseInt(b, 16);
  1149. }
  1150. return rgb(r, g, b);
  1151. }
  1152. function d3_rgb_hsl(r, g, b) {
  1153. var min = Math.min(r /= 255, g /= 255, b /= 255),
  1154. max = Math.max(r, g, b),
  1155. d = max - min,
  1156. h,
  1157. s,
  1158. l = (max + min) / 2;
  1159. if (d) {
  1160. s = l < .5 ? d / (max + min) : d / (2 - max - min);
  1161. if (r == max) h = (g - b) / d + (g < b ? 6 : 0);
  1162. else if (g == max) h = (b - r) / d + 2;
  1163. else h = (r - g) / d + 4;
  1164. h *= 60;
  1165. } else {
  1166. s = h = 0;
  1167. }
  1168. return d3_hsl(h, s, l);
  1169. }
  1170. function d3_rgb_parseNumber(c) { // either integer or percentage
  1171. var f = parseFloat(c);
  1172. return c.charAt(c.length - 1) === "%" ? Math.round(f * 2.55) : f;
  1173. }
  1174. var d3_rgb_names = d3.map({
  1175. aliceblue: "#f0f8ff",
  1176. antiquewhite: "#faebd7",
  1177. aqua: "#00ffff",
  1178. aquamarine: "#7fffd4",
  1179. azure: "#f0ffff",
  1180. beige: "#f5f5dc",
  1181. bisque: "#ffe4c4",
  1182. black: "#000000",
  1183. blanchedalmond: "#ffebcd",
  1184. blue: "#0000ff",
  1185. blueviolet: "#8a2be2",
  1186. brown: "#a52a2a",
  1187. burlywood: "#deb887",
  1188. cadetblue: "#5f9ea0",
  1189. chartreuse: "#7fff00",
  1190. chocolate: "#d2691e",
  1191. coral: "#ff7f50",
  1192. cornflowerblue: "#6495ed",
  1193. cornsilk: "#fff8dc",
  1194. crimson: "#dc143c",
  1195. cyan: "#00ffff",
  1196. darkblue: "#00008b",
  1197. darkcyan: "#008b8b",
  1198. darkgoldenrod: "#b8860b",
  1199. darkgray: "#a9a9a9",
  1200. darkgreen: "#006400",
  1201. darkgrey: "#a9a9a9",
  1202. darkkhaki: "#bdb76b",
  1203. darkmagenta: "#8b008b",
  1204. darkolivegreen: "#556b2f",
  1205. darkorange: "#ff8c00",
  1206. darkorchid: "#9932cc",
  1207. darkred: "#8b0000",
  1208. darksalmon: "#e9967a",
  1209. darkseagreen: "#8fbc8f",
  1210. darkslateblue: "#483d8b",
  1211. darkslategray: "#2f4f4f",
  1212. darkslategrey: "#2f4f4f",
  1213. darkturquoise: "#00ced1",
  1214. darkviolet: "#9400d3",
  1215. deeppink: "#ff1493",
  1216. deepskyblue: "#00bfff",
  1217. dimgray: "#696969",
  1218. dimgrey: "#696969",
  1219. dodgerblue: "#1e90ff",
  1220. firebrick: "#b22222",
  1221. floralwhite: "#fffaf0",
  1222. forestgreen: "#228b22",
  1223. fuchsia: "#ff00ff",
  1224. gainsboro: "#dcdcdc",
  1225. ghostwhite: "#f8f8ff",
  1226. gold: "#ffd700",
  1227. goldenrod: "#daa520",
  1228. gray: "#808080",
  1229. green: "#008000",
  1230. greenyellow: "#adff2f",
  1231. grey: "#808080",
  1232. honeydew: "#f0fff0",
  1233. hotpink: "#ff69b4",
  1234. indianred: "#cd5c5c",
  1235. indigo: "#4b0082",
  1236. ivory: "#fffff0",
  1237. khaki: "#f0e68c",
  1238. lavender: "#e6e6fa",
  1239. lavenderblush: "#fff0f5",
  1240. lawngreen: "#7cfc00",
  1241. lemonchiffon: "#fffacd",
  1242. lightblue: "#add8e6",
  1243. lightcoral: "#f08080",
  1244. lightcyan: "#e0ffff",
  1245. lightgoldenrodyellow: "#fafad2",
  1246. lightgray: "#d3d3d3",
  1247. lightgreen: "#90ee90",
  1248. lightgrey: "#d3d3d3",
  1249. lightpink: "#ffb6c1",
  1250. lightsalmon: "#ffa07a",
  1251. lightseagreen: "#20b2aa",
  1252. lightskyblue: "#87cefa",
  1253. lightslategray: "#778899",
  1254. lightslategrey: "#778899",
  1255. lightsteelblue: "#b0c4de",
  1256. lightyellow: "#ffffe0",
  1257. lime: "#00ff00",
  1258. limegreen: "#32cd32",
  1259. linen: "#faf0e6",
  1260. magenta: "#ff00ff",
  1261. maroon: "#800000",
  1262. mediumaquamarine: "#66cdaa",
  1263. mediumblue: "#0000cd",
  1264. mediumorchid: "#ba55d3",
  1265. mediumpurple: "#9370db",
  1266. mediumseagreen: "#3cb371",
  1267. mediumslateblue: "#7b68ee",
  1268. mediumspringgreen: "#00fa9a",
  1269. mediumturquoise: "#48d1cc",
  1270. mediumvioletred: "#c71585",
  1271. midnightblue: "#191970",
  1272. mintcream: "#f5fffa",
  1273. mistyrose: "#ffe4e1",
  1274. moccasin: "#ffe4b5",
  1275. navajowhite: "#ffdead",
  1276. navy: "#000080",
  1277. oldlace: "#fdf5e6",
  1278. olive: "#808000",
  1279. olivedrab: "#6b8e23",
  1280. orange: "#ffa500",
  1281. orangered: "#ff4500",
  1282. orchid: "#da70d6",
  1283. palegoldenrod: "#eee8aa",
  1284. palegreen: "#98fb98",
  1285. paleturquoise: "#afeeee",
  1286. palevioletred: "#db7093",
  1287. papayawhip: "#ffefd5",
  1288. peachpuff: "#ffdab9",
  1289. peru: "#cd853f",
  1290. pink: "#ffc0cb",
  1291. plum: "#dda0dd",
  1292. powderblue: "#b0e0e6",
  1293. purple: "#800080",
  1294. red: "#ff0000",
  1295. rosybrown: "#bc8f8f",
  1296. royalblue: "#4169e1",
  1297. saddlebrown: "#8b4513",
  1298. salmon: "#fa8072",
  1299. sandybrown: "#f4a460",
  1300. seagreen: "#2e8b57",
  1301. seashell: "#fff5ee",
  1302. sienna: "#a0522d",
  1303. silver: "#c0c0c0",
  1304. skyblue: "#87ceeb",
  1305. slateblue: "#6a5acd",
  1306. slategray: "#708090",
  1307. slategrey: "#708090",
  1308. snow: "#fffafa",
  1309. springgreen: "#00ff7f",
  1310. steelblue: "#4682b4",
  1311. tan: "#d2b48c",
  1312. teal: "#008080",
  1313. thistle: "#d8bfd8",
  1314. tomato: "#ff6347",
  1315. turquoise: "#40e0d0",
  1316. violet: "#ee82ee",
  1317. wheat: "#f5deb3",
  1318. white: "#ffffff",
  1319. whitesmoke: "#f5f5f5",
  1320. yellow: "#ffff00",
  1321. yellowgreen: "#9acd32"
  1322. });
  1323. d3_rgb_names.forEach(function(key, value) {
  1324. d3_rgb_names.set(key, d3_rgb_parse(value, d3_rgb, d3_hsl_rgb));
  1325. });
  1326. d3.hsl = function(h, s, l) {
  1327. return arguments.length === 1
  1328. ? (h instanceof d3_Hsl ? d3_hsl(h.h, h.s, h.l)
  1329. : d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl))
  1330. : d3_hsl(+h, +s, +l);
  1331. };
  1332. function d3_hsl(h, s, l) {
  1333. return new d3_Hsl(h, s, l);
  1334. }
  1335. function d3_Hsl(h, s, l) {
  1336. this.h = h;
  1337. this.s = s;
  1338. this.l = l;
  1339. }
  1340. d3_Hsl.prototype.brighter = function(k) {
  1341. k = Math.pow(0.7, arguments.length ? k : 1);
  1342. return d3_hsl(this.h, this.s, this.l / k);
  1343. };
  1344. d3_Hsl.prototype.darker = function(k) {
  1345. k = Math.pow(0.7, arguments.length ? k : 1);
  1346. return d3_hsl(this.h, this.s, k * this.l);
  1347. };
  1348. d3_Hsl.prototype.rgb = function() {
  1349. return d3_hsl_rgb(this.h, this.s, this.l);
  1350. };
  1351. d3_Hsl.prototype.toString = function() {
  1352. return this.rgb().toString();
  1353. };
  1354. function d3_hsl_rgb(h, s, l) {
  1355. var m1,
  1356. m2;
  1357. /* Some simple corrections for h, s and l. */
  1358. h = h % 360; if (h < 0) h += 360;
  1359. s = s < 0 ? 0 : s > 1 ? 1 : s;
  1360. l = l < 0 ? 0 : l > 1 ? 1 : l;
  1361. /* From FvD 13.37, CSS Color Module Level 3 */
  1362. m2 = l <= .5 ? l * (1 + s) : l + s - l * s;
  1363. m1 = 2 * l - m2;
  1364. function v(h) {
  1365. if (h > 360) h -= 360;
  1366. else if (h < 0) h += 360;
  1367. if (h < 60) return m1 + (m2 - m1) * h / 60;
  1368. if (h < 180) return m2;
  1369. if (h < 240) return m1 + (m2 - m1) * (240 - h) / 60;
  1370. return m1;
  1371. }
  1372. function vv(h) {
  1373. return Math.round(v(h) * 255);
  1374. }
  1375. return d3_rgb(vv(h + 120), vv(h), vv(h - 120));
  1376. }
  1377. function d3_selection(groups) {
  1378. d3_arraySubclass(groups, d3_selectionPrototype);
  1379. return groups;
  1380. }
  1381. var d3_select = function(s, n) { return n.querySelector(s); },
  1382. d3_selectAll = function(s, n) { return n.querySelectorAll(s); },
  1383. d3_selectRoot = document.documentElement,
  1384. d3_selectMatcher = d3_selectRoot.matchesSelector || d3_selectRoot.webkitMatchesSelector || d3_selectRoot.mozMatchesSelector || d3_selectRoot.msMatchesSelector || d3_selectRoot.oMatchesSelector,
  1385. d3_selectMatches = function(n, s) { return d3_selectMatcher.call(n, s); };
  1386. // Prefer Sizzle, if available.
  1387. if (typeof Sizzle === "function") {
  1388. d3_select = function(s, n) { return Sizzle(s, n)[0]; };
  1389. d3_selectAll = function(s, n) { return Sizzle.uniqueSort(Sizzle(s, n)); };
  1390. d3_selectMatches = Sizzle.matchesSelector;
  1391. }
  1392. var d3_selectionPrototype = [];
  1393. d3.selection = function() {
  1394. return d3_selectionRoot;
  1395. };
  1396. d3.selection.prototype = d3_selectionPrototype;
  1397. d3_selectionPrototype.select = function(selector) {
  1398. var subgroups = [],
  1399. subgroup,
  1400. subnode,
  1401. group,
  1402. node;
  1403. if (typeof selector !== "function") selector = d3_selection_selector(selector);
  1404. for (var j = -1, m = this.length; ++j < m;) {
  1405. subgroups.push(subgroup = []);
  1406. subgroup.parentNode = (group = this[j]).parentNode;
  1407. for (var i = -1, n = group.length; ++i < n;) {
  1408. if (node = group[i]) {
  1409. subgroup.push(subnode = selector.call(node, node.__data__, i));
  1410. if (subnode && "__data__" in node) subnode.__data__ = node.__data__;
  1411. } else {
  1412. subgroup.push(null);
  1413. }
  1414. }
  1415. }
  1416. return d3_selection(subgroups);
  1417. };
  1418. function d3_selection_selector(selector) {
  1419. return function() {
  1420. return d3_select(selector, this);
  1421. };
  1422. }
  1423. d3_selectionPrototype.selectAll = function(selector) {
  1424. var subgroups = [],
  1425. subgroup,
  1426. node;
  1427. if (typeof selector !== "function") selector = d3_selection_selectorAll(selector);
  1428. for (var j = -1, m = this.length; ++j < m;) {
  1429. for (var group = this[j], i = -1, n = group.length; ++i < n;) {
  1430. if (node = group[i]) {
  1431. subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i)));
  1432. subgroup.parentNode = node;
  1433. }
  1434. }
  1435. }
  1436. return d3_selection(subgroups);
  1437. };
  1438. function d3_selection_selectorAll(selector) {
  1439. return function() {
  1440. return d3_selectAll(selector, this);
  1441. };
  1442. }
  1443. d3_selectionPrototype.attr = function(name, value) {
  1444. name = d3.ns.qualify(name);
  1445. // If no value is specified, return the first value.
  1446. if (arguments.length < 2) {
  1447. var node = this.node();
  1448. return name.local
  1449. ? node.getAttributeNS(name.space, name.local)
  1450. : node.getAttribute(name);
  1451. }
  1452. function attrNull() {
  1453. this.removeAttribute(name);
  1454. }
  1455. function attrNullNS() {
  1456. this.removeAttributeNS(name.space, name.local);
  1457. }
  1458. function attrConstant() {
  1459. this.setAttribute(name, value);
  1460. }
  1461. function attrConstantNS() {
  1462. this.setAttributeNS(name.space, name.local, value);
  1463. }
  1464. function attrFunction() {
  1465. var x = value.apply(this, arguments);
  1466. if (x == null) this.removeAttribute(name);
  1467. else this.setAttribute(name, x);
  1468. }
  1469. function attrFunctionNS() {
  1470. var x = value.apply(this, arguments);
  1471. if (x == null) this.removeAttributeNS(name.space, name.local);
  1472. else this.setAttributeNS(name.space, name.local, x);
  1473. }
  1474. return this.each(value == null
  1475. ? (name.local ? attrNullNS : attrNull) : (typeof value === "function"
  1476. ? (name.local ? attrFunctionNS : attrFunction)
  1477. : (name.local ? attrConstantNS : attrConstant)));
  1478. };
  1479. d3_selectionPrototype.classed = function(name, value) {
  1480. var names = name.split(d3_selection_classedWhitespace),
  1481. n = names.length,
  1482. i = -1;
  1483. if (arguments.length > 1) {
  1484. while (++i < n) d3_selection_classed.call(this, names[i], value);
  1485. return this;
  1486. } else {
  1487. while (++i < n) if (!d3_selection_classed.call(this, names[i])) return false;
  1488. return true;
  1489. }
  1490. };
  1491. var d3_selection_classedWhitespace = /\s+/g;
  1492. function d3_selection_classed(name, value) {
  1493. var re = new RegExp("(^|\\s+)" + d3.requote(name) + "(\\s+|$)", "g");
  1494. // If no value is specified, return the first value.
  1495. if (arguments.length < 2) {
  1496. var node = this.node();
  1497. if (c = node.classList) return c.contains(name);
  1498. var c = node.className;
  1499. re.lastIndex = 0;
  1500. return re.test(c.baseVal != null ? c.baseVal : c);
  1501. }
  1502. function classedAdd() {
  1503. if (c = this.classList) return c.add(name);
  1504. var c = this.className,
  1505. cb = c.baseVal != null,
  1506. cv = cb ? c.baseVal : c;
  1507. re.lastIndex = 0;
  1508. if (!re.test(cv)) {
  1509. cv = d3_collapse(cv + " " + name);
  1510. if (cb) c.baseVal = cv;
  1511. else this.className = cv;
  1512. }
  1513. }
  1514. function classedRemove() {
  1515. if (c = this.classList) return c.remove(name);
  1516. var c = this.className,
  1517. cb = c.baseVal != null,
  1518. cv = cb ? c.baseVal : c;
  1519. cv = d3_collapse(cv.replace(re, " "));
  1520. if (cb) c.baseVal = cv;
  1521. else this.className = cv;
  1522. }
  1523. function classedFunction() {
  1524. (value.apply(this, arguments)
  1525. ? classedAdd
  1526. : classedRemove).call(this);
  1527. }
  1528. return this.each(typeof value === "function"
  1529. ? classedFunction : value
  1530. ? classedAdd
  1531. : classedRemove);
  1532. }
  1533. d3_selectionPrototype.style = function(name, value, priority) {
  1534. if (arguments.length < 3) priority = "";
  1535. // If no value is specified, return the first value.
  1536. if (arguments.length < 2) return window
  1537. .getComputedStyle(this.node(), null)
  1538. .getPropertyValue(name);
  1539. function styleNull() {
  1540. this.style.removeProperty(name);
  1541. }
  1542. function styleConstant() {
  1543. this.style.setProperty(name, value, priority);
  1544. }
  1545. function styleFunction() {
  1546. var x = value.apply(this, arguments);
  1547. if (x == null) this.style.removeProperty(name);
  1548. else this.style.setProperty(name, x, priority);
  1549. }
  1550. return this.each(value == null
  1551. ? styleNull : (typeof value === "function"
  1552. ? styleFunction : styleConstant));
  1553. };
  1554. d3_selectionPrototype.property = function(name, value) {
  1555. // If no value is specified, return the first value.
  1556. if (arguments.length < 2) return this.node()[name];
  1557. function propertyNull() {
  1558. delete this[name];
  1559. }
  1560. function propertyConstant() {
  1561. this[name] = value;
  1562. }
  1563. function propertyFunction() {
  1564. var x = value.apply(this, arguments);
  1565. if (x == null) delete this[name];
  1566. else this[name] = x;
  1567. }
  1568. return this.each(value == null
  1569. ? propertyNull : (typeof value === "function"
  1570. ? propertyFunction : propertyConstant));
  1571. };
  1572. d3_selectionPrototype.text = function(value) {
  1573. return arguments.length < 1
  1574. ? this.node().textContent : this.each(typeof value === "function"
  1575. ? function() { var v = value.apply(this, arguments); this.textContent = v == null ? "" : v; } : value == null
  1576. ? function() { this.textContent = ""; }
  1577. : function() { this.textContent = value; });
  1578. };
  1579. d3_selectionPrototype.html = function(value) {
  1580. return arguments.length < 1
  1581. ? this.node().innerHTML : this.each(typeof value === "function"
  1582. ? function() { var v = value.apply(this, arguments); this.innerHTML = v == null ? "" : v; } : value == null
  1583. ? function() { this.innerHTML = ""; }
  1584. : function() { this.innerHTML = value; });
  1585. };
  1586. // TODO append(node)?
  1587. // TODO append(function)?
  1588. d3_selectionPrototype.append = function(name) {
  1589. name = d3.ns.qualify(name);
  1590. function append() {
  1591. return this.appendChild(document.createElementNS(this.namespaceURI, name));
  1592. }
  1593. function appendNS() {
  1594. return this.appendChild(document.createElementNS(name.space, name.local));
  1595. }
  1596. return this.select(name.local ? appendNS : append);
  1597. };
  1598. // TODO insert(node, function)?
  1599. // TODO insert(function, string)?
  1600. // TODO insert(function, function)?
  1601. d3_selectionPrototype.insert = function(name, before) {
  1602. name = d3.ns.qualify(name);
  1603. function insert() {
  1604. return this.insertBefore(
  1605. document.createElementNS(this.namespaceURI, name),
  1606. d3_select(before, this));
  1607. }
  1608. function insertNS() {
  1609. return this.insertBefore(
  1610. document.createElementNS(name.space, name.local),
  1611. d3_select(before, this));
  1612. }
  1613. return this.select(name.local ? insertNS : insert);
  1614. };
  1615. // TODO remove(selector)?
  1616. // TODO remove(node)?
  1617. // TODO remove(function)?
  1618. d3_selectionPrototype.remove = function() {
  1619. return this.each(function() {
  1620. var parent = this.parentNode;
  1621. if (parent) parent.removeChild(this);
  1622. });
  1623. };
  1624. d3_selectionPrototype.data = function(value, key) {
  1625. var i = -1,
  1626. n = this.length,
  1627. group,
  1628. node;
  1629. // If no value is specified, return the first value.
  1630. if (!arguments.length) {
  1631. value = new Array(n = (group = this[0]).length);
  1632. while (++i < n) {
  1633. if (node = group[i]) {
  1634. value[i] = node.__data__;
  1635. }
  1636. }
  1637. return value;
  1638. }
  1639. function bind(group, groupData) {
  1640. var i,
  1641. n = group.length,
  1642. m = groupData.length,
  1643. n0 = Math.min(n, m),
  1644. n1 = Math.max(n, m),
  1645. updateNodes = [],
  1646. enterNodes = [],
  1647. exitNodes = [],
  1648. node,
  1649. nodeData;
  1650. if (key) {
  1651. var nodeByKeyValue = new d3_Map,
  1652. keyValues = [],
  1653. keyValue,
  1654. j = groupData.length;
  1655. for (i = -1; ++i < n;) {
  1656. keyValue = key.call(node = group[i], node.__data__, i);
  1657. if (nodeByKeyValue.has(keyValue)) {
  1658. exitNodes[j++] = node; // duplicate key
  1659. } else {
  1660. nodeByKeyValue.set(keyValue, node);
  1661. }
  1662. keyValues.push(keyValue);
  1663. }
  1664. for (i = -1; ++i < m;) {
  1665. keyValue = key.call(groupData, nodeData = groupData[i], i)
  1666. if (nodeByKeyValue.has(keyValue)) {
  1667. updateNodes[i] = node = nodeByKeyValue.get(keyValue);
  1668. node.__data__ = nodeData;
  1669. enterNodes[i] = exitNodes[i] = null;
  1670. } else {
  1671. enterNodes[i] = d3_selection_dataNode(nodeData);
  1672. updateNodes[i] = exitNodes[i] = null;
  1673. }
  1674. nodeByKeyValue.remove(keyValue);
  1675. }
  1676. for (i = -1; ++i < n;) {
  1677. if (nodeByKeyValue.has(keyValues[i])) {
  1678. exitNodes[i] = group[i];
  1679. }
  1680. }
  1681. } else {
  1682. for (i = -1; ++i < n0;) {
  1683. node = group[i];
  1684. nodeData = groupData[i];
  1685. if (node) {
  1686. node.__data__ = nodeData;
  1687. updateNodes[i] = node;
  1688. enterNodes[i] = exitNodes[i] = null;
  1689. } else {
  1690. enterNodes[i] = d3_selection_dataNode(nodeData);
  1691. updateNodes[i] = exitNodes[i] = null;
  1692. }
  1693. }
  1694. for (; i < m; ++i) {
  1695. enterNodes[i] = d3_selection_dataNode(groupData[i]);
  1696. updateNodes[i] = exitNodes[i] = null;
  1697. }
  1698. for (; i < n1; ++i) {
  1699. exitNodes[i] = group[i];
  1700. enterNodes[i] = updateNodes[i] = null;
  1701. }
  1702. }
  1703. enterNodes.update
  1704. = updateNodes;
  1705. enterNodes.parentNode
  1706. = updateNodes.parentNode
  1707. = exitNodes.parentNode
  1708. = group.parentNode;
  1709. enter.push(enterNodes);
  1710. update.push(updateNodes);
  1711. exit.push(exitNodes);
  1712. }
  1713. var enter = d3_selection_enter([]),
  1714. update = d3_selection([]),
  1715. exit = d3_selection([]);
  1716. if (typeof value === "function") {
  1717. while (++i < n) {
  1718. bind(group = this[i], value.call(group, group.parentNode.__data__, i));
  1719. }
  1720. } else {
  1721. while (++i < n) {
  1722. bind(group = this[i], value);
  1723. }
  1724. }
  1725. update.enter = function() { return enter; };
  1726. update.exit = function() { return exit; };
  1727. return update;
  1728. };
  1729. function d3_selection_dataNode(data) {
  1730. return {__data__: data};
  1731. }
  1732. d3_selectionPrototype.datum =
  1733. d3_selectionPrototype.map = function(value) {
  1734. return arguments.length < 1
  1735. ? this.property("__data__")
  1736. : this.property("__data__", value);
  1737. };
  1738. d3_selectionPrototype.filter = function(filter) {
  1739. var subgroups = [],
  1740. subgroup,
  1741. group,
  1742. node;
  1743. if (typeof filter !== "function") filter = d3_selection_filter(filter);
  1744. for (var j = 0, m = this.length; j < m; j++) {
  1745. subgroups.push(subgroup = []);
  1746. subgroup.parentNode = (group = this[j]).parentNode;
  1747. for (var i = 0, n = group.length; i < n; i++) {
  1748. if ((node = group[i]) && filter.call(node, node.__data__, i)) {
  1749. subgroup.push(node);
  1750. }
  1751. }
  1752. }
  1753. return d3_selection(subgroups);
  1754. };
  1755. function d3_selection_filter(selector) {
  1756. return function() {
  1757. return d3_selectMatches(this, selector);
  1758. };
  1759. }
  1760. d3_selectionPrototype.order = function() {
  1761. for (var j = -1, m = this.length; ++j < m;) {
  1762. for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0;) {
  1763. if (node = group[i]) {
  1764. if (next && next !== node.nextSibling) next.parentNode.insertBefore(node, next);
  1765. next = node;
  1766. }
  1767. }
  1768. }
  1769. return this;
  1770. };
  1771. d3_selectionPrototype.sort = function(comparator) {
  1772. comparator = d3_selection_sortComparator.apply(this, arguments);
  1773. for (var j = -1, m = this.length; ++j < m;) this[j].sort(comparator);
  1774. return this.order();
  1775. };
  1776. function d3_selection_sortComparator(comparator) {
  1777. if (!arguments.length) comparator = d3.ascending;
  1778. return function(a, b) {
  1779. return comparator(a && a.__data__, b && b.__data__);
  1780. };
  1781. }
  1782. // type can be namespaced, e.g., "click.foo"
  1783. // listener can be null for removal
  1784. d3_selectionPrototype.on = function(type, listener, capture) {
  1785. if (arguments.length < 3) capture = false;
  1786. // parse the type specifier
  1787. var name = "__on" + type, i = type.indexOf(".");
  1788. if (i > 0) type = type.substring(0, i);
  1789. // if called with only one argument, return the current listener
  1790. if (arguments.length < 2) return (i = this.node()[name]) && i._;
  1791. // remove the old event listener, and add the new event listener
  1792. return this.each(function(d, i) {
  1793. var node = this,
  1794. o = node[name];
  1795. // remove the old listener, if any (using the previously-set capture)
  1796. if (o) {
  1797. node.removeEventListener(type, o, o.$);
  1798. delete node[name];
  1799. }
  1800. // add the new listener, if any (remembering the capture flag)
  1801. if (listener) {
  1802. node.addEventListener(type, node[name] = l, l.$ = capture);
  1803. l._ = listener; // stash the unwrapped listener for get
  1804. }
  1805. // wrapped event listener that preserves i
  1806. function l(e) {
  1807. var o = d3.event; // Events can be reentrant (e.g., focus).
  1808. d3.event = e;
  1809. try {
  1810. listener.call(node, node.__data__, i);
  1811. } finally {
  1812. d3.event = o;
  1813. }
  1814. }
  1815. });
  1816. };
  1817. d3_selectionPrototype.each = function(callback) {
  1818. for (var j = -1, m = this.length; ++j < m;) {
  1819. for (var group = this[j], i = -1, n = group.length; ++i < n;) {
  1820. var node = group[i];
  1821. if (node) callback.call(node, node.__data__, i, j);
  1822. }
  1823. }
  1824. return this;
  1825. };
  1826. //
  1827. // Note: assigning to the arguments array simultaneously changes the value of
  1828. // the corresponding argument!
  1829. //
  1830. // TODO The `this` argument probably shouldn't be the first argument to the
  1831. // callback, anyway, since it's redundant. However, that will require a major
  1832. // version bump due to backwards compatibility, so I'm not changing it right
  1833. // away.
  1834. //
  1835. d3_selectionPrototype.call = function(callback) {
  1836. callback.apply(this, (arguments[0] = this, arguments));
  1837. return this;
  1838. };
  1839. d3_selectionPrototype.empty = function() {
  1840. return !this.node();
  1841. };
  1842. d3_selectionPrototype.node = function(callback) {
  1843. for (var j = 0, m = this.length; j < m; j++) {
  1844. for (var group = this[j], i = 0, n = group.length; i < n; i++) {
  1845. var node = group[i];
  1846. if (node) return node;
  1847. }
  1848. }
  1849. return null;
  1850. };
  1851. d3_selectionPrototype.transition = function() {
  1852. var subgroups = [],
  1853. subgroup,
  1854. node;
  1855. for (var j = -1, m = this.length; ++j < m;) {
  1856. subgroups.push(subgroup = []);
  1857. for (var group = this[j], i = -1, n = group.length; ++i < n;) {
  1858. subgroup.push((node = group[i]) ? {node: node, delay: d3_transitionDelay, duration: d3_transitionDuration} : null);
  1859. }
  1860. }
  1861. return d3_transition(subgroups, d3_transitionId || ++d3_transitionNextId, Date.now());
  1862. };
  1863. var d3_selectionRoot = d3_selection([[document]]);
  1864. d3_selectionRoot[0].parentNode = d3_selectRoot;
  1865. // TODO fast singleton implementation!
  1866. // TODO select(function)
  1867. d3.select = function(selector) {
  1868. return typeof selector === "string"
  1869. ? d3_selectionRoot.select(selector)
  1870. : d3_selection([[selector]]); // assume node
  1871. };
  1872. // TODO selectAll(function)
  1873. d3.selectAll = function(selector) {
  1874. return typeof selector === "string"
  1875. ? d3_selectionRoot.selectAll(selector)
  1876. : d3_selection([d3_array(selector)]); // assume node[]
  1877. };
  1878. function d3_selection_enter(selection) {
  1879. d3_arraySubclass(selection, d3_selection_enterPrototype);
  1880. return selection;
  1881. }
  1882. var d3_selection_enterPrototype = [];
  1883. d3.selection.enter = d3_selection_enter;
  1884. d3.selection.enter.prototype = d3_selection_enterPrototype;
  1885. d3_selection_enterPrototype.append = d3_selectionPrototype.append;
  1886. d3_selection_enterPrototype.insert = d3_selectionPrototype.insert;
  1887. d3_selection_enterPrototype.empty = d3_selectionPrototype.empty;
  1888. d3_selection_enterPrototype.node = d3_selectionPrototype.node;
  1889. d3_selection_enterPrototype.select = function(selector) {
  1890. var subgroups = [],
  1891. subgroup,
  1892. subnode,
  1893. upgroup,
  1894. group,
  1895. node;
  1896. for (var j = -1, m = this.length; ++j < m;) {
  1897. upgroup = (group = this[j]).update;
  1898. subgroups.push(subgroup = []);
  1899. subgroup.parentNode = group.parentNode;
  1900. for (var i = -1, n = group.length; ++i < n;) {
  1901. if (node = group[i]) {
  1902. subgroup.push(upgroup[i] = subnode = selector.call(group.parentNode, node.__data__, i));
  1903. subnode.__data__ = node.__data__;
  1904. } else {
  1905. subgroup.push(null);
  1906. }
  1907. }
  1908. }
  1909. return d3_selection(subgroups);
  1910. };
  1911. function d3_transition(groups, id, time) {
  1912. d3_arraySubclass(groups, d3_transitionPrototype);
  1913. var tweens = new d3_Map,
  1914. event = d3.dispatch("start", "end"),
  1915. ease = d3_transitionEase;
  1916. groups.id = id;
  1917. groups.time = time;
  1918. groups.tween = function(name, tween) {
  1919. if (arguments.length < 2) return tweens.get(name);
  1920. if (tween == null) tweens.remove(name);
  1921. else tweens.set(name, tween);
  1922. return groups;
  1923. };
  1924. groups.ease = function(value) {
  1925. if (!arguments.length) return ease;
  1926. ease = typeof value === "function" ? value : d3.ease.apply(d3, arguments);
  1927. return groups;
  1928. };
  1929. groups.each = function(type, listener) {
  1930. if (arguments.length < 2) return d3_transition_each.call(groups, type);
  1931. event.on(type, listener);
  1932. return groups;
  1933. };
  1934. d3.timer(function(elapsed) {
  1935. groups.each(function(d, i, j) {
  1936. var tweened = [],
  1937. node = this,
  1938. delay = groups[j][i].delay,
  1939. duration = groups[j][i].duration,
  1940. lock = node.__transition__ || (node.__transition__ = {active: 0, count: 0});
  1941. ++lock.count;
  1942. delay <= elapsed ? start(elapsed) : d3.timer(start, delay, time);
  1943. function start(elapsed) {
  1944. if (lock.active > id) return stop();
  1945. lock.active = id;
  1946. tweens.forEach(function(key, value) {
  1947. if (tween = value.call(node, d, i)) {
  1948. tweened.push(tween);
  1949. }
  1950. });
  1951. event.start.call(node, d, i);
  1952. if (!tick(elapsed)) d3.timer(tick, 0, time);
  1953. return 1;
  1954. }
  1955. function tick(elapsed) {
  1956. if (lock.active !== id) return stop();
  1957. var t = (elapsed - delay) / duration,
  1958. e = ease(t),
  1959. n = tweened.length;
  1960. while (n > 0) {
  1961. tweened[--n].call(node, e);
  1962. }
  1963. if (t >= 1) {
  1964. stop();
  1965. d3_transitionId = id;
  1966. event.end.call(node, d, i);
  1967. d3_transitionId = 0;
  1968. return 1;
  1969. }
  1970. }
  1971. function stop() {
  1972. if (!--lock.count) delete node.__transition__;
  1973. return 1;
  1974. }
  1975. });
  1976. return 1;
  1977. }, 0, time);
  1978. return groups;
  1979. }
  1980. var d3_transitionRemove = {};
  1981. function d3_transitionNull(d, i, a) {
  1982. return a != "" && d3_transitionRemove;
  1983. }
  1984. function d3_transitionTween(name, b) {
  1985. var interpolate = d3_interpolateByName(name);
  1986. function transitionFunction(d, i, a) {
  1987. var v = b.call(this, d, i);
  1988. return v == null
  1989. ? a != "" && d3_transitionRemove
  1990. : a != v && interpolate(a, v);
  1991. }
  1992. function transitionString(d, i, a) {
  1993. return a != b && interpolate(a, b);
  1994. }
  1995. return typeof b === "function" ? transitionFunction
  1996. : b == null ? d3_transitionNull
  1997. : (b += "", transitionString);
  1998. }
  1999. var d3_transitionPrototype = [],
  2000. d3_transitionNextId = 0,
  2001. d3_transitionId = 0,
  2002. d3_transitionDefaultDelay = 0,
  2003. d3_transitionDefaultDuration = 250,
  2004. d3_transitionDefaultEase = d3.ease("cubic-in-out"),
  2005. d3_transitionDelay = d3_transitionDefaultDelay,
  2006. d3_transitionDuration = d3_transitionDefaultDuration,
  2007. d3_transitionEase = d3_transitionDefaultEase;
  2008. d3_transitionPrototype.call = d3_selectionPrototype.call;
  2009. d3.transition = function(selection) {
  2010. return arguments.length
  2011. ? (d3_transitionId ? selection.transition() : selection)
  2012. : d3_selectionRoot.transition();
  2013. };
  2014. d3.transition.prototype = d3_transitionPrototype;
  2015. d3_transitionPrototype.select = function(selector) {
  2016. var subgroups = [],
  2017. subgroup,
  2018. subnode,
  2019. node;
  2020. if (typeof selector !== "function") selector = d3_selection_selector(selector);
  2021. for (var j = -1, m = this.length; ++j < m;) {
  2022. subgroups.push(subgroup = []);
  2023. for (var group = this[j], i = -1, n = group.length; ++i < n;) {
  2024. if ((node = group[i]) && (subnode = selector.call(node.node, node.node.__data__, i))) {
  2025. if ("__data__" in node.node) subnode.__data__ = node.node.__data__;
  2026. subgroup.push({node: subnode, delay: node.delay, duration: node.duration});
  2027. } else {
  2028. subgroup.push(null);
  2029. }
  2030. }
  2031. }
  2032. return d3_transition(subgroups, this.id, this.time).ease(this.ease());
  2033. };
  2034. d3_transitionPrototype.selectAll = function(selector) {
  2035. var subgroups = [],
  2036. subgroup,
  2037. subnodes,
  2038. node;
  2039. if (typeof selector !== "function") selector = d3_selection_selectorAll(selector);
  2040. for (var j = -1, m = this.length; ++j < m;) {
  2041. for (var group = this[j], i = -1, n = group.length; ++i < n;) {
  2042. if (node = group[i]) {
  2043. subnodes = selector.call(node.node, node.node.__data__, i);
  2044. subgroups.push(subgroup = []);
  2045. for (var k = -1, o = subnodes.length; ++k < o;) {
  2046. subgroup.push({node: subnodes[k], delay: node.delay, duration: node.duration});
  2047. }
  2048. }
  2049. }
  2050. }
  2051. return d3_transition(subgroups, this.id, this.time).ease(this.ease());
  2052. };
  2053. d3_transitionPrototype.attr = function(name, value) {
  2054. return this.attrTween(name, d3_transitionTween(name, value));
  2055. };
  2056. d3_transitionPrototype.attrTween = function(nameNS, tween) {
  2057. var name = d3.ns.qualify(nameNS);
  2058. function attrTween(d, i) {
  2059. var f = tween.call(this, d, i, this.getAttribute(name));
  2060. return f === d3_transitionRemove
  2061. ? (this.removeAttribute(name), null)
  2062. : f && function(t) { this.setAttribute(name, f(t)); };
  2063. }
  2064. function attrTweenNS(d, i) {
  2065. var f = tween.call(this, d, i, this.getAttributeNS(name.space, name.local));
  2066. return f === d3_transitionRemove
  2067. ? (this.removeAttributeNS(name.space, name.local), null)
  2068. : f && function(t) { this.setAttributeNS(name.space, name.local, f(t)); };
  2069. }
  2070. return this.tween("attr." + nameNS, name.local ? attrTweenNS : attrTween);
  2071. };
  2072. d3_transitionPrototype.style = function(name, value, priority) {
  2073. if (arguments.length < 3) priority = "";
  2074. return this.styleTween(name, d3_transitionTween(name, value), priority);
  2075. };
  2076. d3_transitionPrototype.styleTween = function(name, tween, priority) {
  2077. if (arguments.length < 3) priority = "";
  2078. return this.tween("style." + name, function(d, i) {
  2079. var f = tween.call(this, d, i, window.getComputedStyle(this, null).getPropertyValue(name));
  2080. return f === d3_transitionRemove
  2081. ? (this.style.removeProperty(name), null)
  2082. : f && function(t) { this.style.setProperty(name, f(t), priority); };
  2083. });
  2084. };
  2085. d3_transitionPrototype.text = function(value) {
  2086. return this.tween("text", function(d, i) {
  2087. this.textContent = typeof value === "function"
  2088. ? value.call(this, d, i)
  2089. : value;
  2090. });
  2091. };
  2092. d3_transitionPrototype.remove = function() {
  2093. return this.each("end.transition", function() {
  2094. var p;
  2095. if (!this.__transition__ && (p = this.parentNode)) p.removeChild(this);
  2096. });
  2097. };
  2098. d3_transitionPrototype.delay = function(value) {
  2099. var groups = this;
  2100. return groups.each(typeof value === "function"
  2101. ? function(d, i, j) { groups[j][i].delay = value.apply(this, arguments) | 0; }
  2102. : (value = value | 0, function(d, i, j) { groups[j][i].delay = value; }));
  2103. };
  2104. d3_transitionPrototype.duration = function(value) {
  2105. var groups = this;
  2106. return groups.each(typeof value === "function"
  2107. ? function(d, i, j) { groups[j][i].duration = Math.max(1, value.apply(this, arguments) | 0); }
  2108. : (value = Math.max(1, value | 0), function(d, i, j) { groups[j][i].duration = value; }));
  2109. };
  2110. function d3_transition_each(callback) {
  2111. var id = d3_transitionId,
  2112. ease = d3_transitionEase,
  2113. delay = d3_transitionDelay,
  2114. duration = d3_transitionDuration;
  2115. d3_transitionId = this.id;
  2116. d3_transitionEase = this.ease();
  2117. for (var j = 0, m = this.length; j < m; j++) {
  2118. for (var group = this[j], i = 0, n = group.length; i < n; i++) {
  2119. var node = group[i];
  2120. if (node) {
  2121. d3_transitionDelay = this[j][i].delay;
  2122. d3_transitionDuration = this[j][i].duration;
  2123. callback.call(node = node.node, node.__data__, i, j);
  2124. }
  2125. }
  2126. }
  2127. d3_transitionId = id;
  2128. d3_transitionEase = ease;
  2129. d3_transitionDelay = delay;
  2130. d3_transitionDuration = duration;
  2131. return this;
  2132. }
  2133. d3_transitionPrototype.transition = function() {
  2134. return this.select(d3_this);
  2135. };
  2136. var d3_timer_queue = null,
  2137. d3_timer_interval, // is an interval (or frame) active?
  2138. d3_timer_timeout; // is a timeout active?
  2139. // The timer will continue to fire until callback returns true.
  2140. d3.timer = function(callback, delay, then) {
  2141. var found = false,
  2142. t0,
  2143. t1 = d3_timer_queue;
  2144. if (arguments.length < 3) {
  2145. if (arguments.length < 2) delay = 0;
  2146. else if (!isFinite(delay)) return;
  2147. then = Date.now();
  2148. }
  2149. // See if the callback's already in the queue.
  2150. while (t1) {
  2151. if (t1.callback === callback) {
  2152. t1.then = then;
  2153. t1.delay = delay;
  2154. found = true;
  2155. break;
  2156. }
  2157. t0 = t1;
  2158. t1 = t1.next;
  2159. }
  2160. // Otherwise, add the callback to the queue.
  2161. if (!found) d3_timer_queue = {
  2162. callback: callback,
  2163. then: then,
  2164. delay: delay,
  2165. next: d3_timer_queue
  2166. };
  2167. // Start animatin'!
  2168. if (!d3_timer_interval) {
  2169. d3_timer_timeout = clearTimeout(d3_timer_timeout);
  2170. d3_timer_interval = 1;
  2171. d3_timer_frame(d3_timer_step);
  2172. }
  2173. }
  2174. function d3_timer_step() {
  2175. var elapsed,
  2176. now = Date.now(),
  2177. t1 = d3_timer_queue;
  2178. while (t1) {
  2179. elapsed = now - t1.then;
  2180. if (elapsed >= t1.delay) t1.flush = t1.callback(elapsed);
  2181. t1 = t1.next;
  2182. }
  2183. var delay = d3_timer_flush() - now;
  2184. if (delay > 24) {
  2185. if (isFinite(delay)) {
  2186. clearTimeout(d3_timer_timeout);
  2187. d3_timer_timeout = setTimeout(d3_timer_step, delay);
  2188. }
  2189. d3_timer_interval = 0;
  2190. } else {
  2191. d3_timer_interval = 1;
  2192. d3_timer_frame(d3_timer_step);
  2193. }
  2194. }
  2195. d3.timer.flush = function() {
  2196. var elapsed,
  2197. now = Date.now(),
  2198. t1 = d3_timer_queue;
  2199. while (t1) {
  2200. elapsed = now - t1.then;
  2201. if (!t1.delay) t1.flush = t1.callback(elapsed);
  2202. t1 = t1.next;
  2203. }
  2204. d3_timer_flush();
  2205. };
  2206. // Flush after callbacks, to avoid concurrent queue modification.
  2207. function d3_timer_flush() {
  2208. var t0 = null,
  2209. t1 = d3_timer_queue,
  2210. then = Infinity;
  2211. while (t1) {
  2212. if (t1.flush) {
  2213. t1 = t0 ? t0.next = t1.next : d3_timer_queue = t1.next;
  2214. } else {
  2215. then = Math.min(then, t1.then + t1.delay);
  2216. t1 = (t0 = t1).next;
  2217. }
  2218. }
  2219. return then;
  2220. }
  2221. var d3_timer_frame = window.requestAnimationFrame
  2222. || window.webkitRequestAnimationFrame
  2223. || window.mozRequestAnimationFrame
  2224. || window.oRequestAnimationFrame
  2225. || window.msRequestAnimationFrame
  2226. || function(callback) { setTimeout(callback, 17); };
  2227. d3.transform = function(string) {
  2228. var g = document.createElementNS(d3.ns.prefix.svg, "g"),
  2229. identity = {a: 1, b: 0, c: 0, d: 1, e: 0, f: 0};
  2230. return (d3.transform = function(string) {
  2231. g.setAttribute("transform", string);
  2232. var t = g.transform.baseVal.consolidate();
  2233. return new d3_transform(t ? t.matrix : identity);
  2234. })(string);
  2235. };
  2236. // Compute x-scale and normalize the first row.
  2237. // Compute shear and make second row orthogonal to first.
  2238. // Compute y-scale and normalize the second row.
  2239. // Finally, compute the rotation.
  2240. function d3_transform(m) {
  2241. var r0 = [m.a, m.b],
  2242. r1 = [m.c, m.d],
  2243. kx = d3_transformNormalize(r0),
  2244. kz = d3_transformDot(r0, r1),
  2245. ky = d3_transformNormalize(d3_transformCombine(r1, r0, -kz)) || 0;
  2246. if (r0[0] * r1[1] < r1[0] * r0[1]) {
  2247. r0[0] *= -1;
  2248. r0[1] *= -1;
  2249. kx *= -1;
  2250. kz *= -1;
  2251. }
  2252. this.rotate = (kx ? Math.atan2(r0[1], r0[0]) : Math.atan2(-r1[0], r1[1])) * d3_transformDegrees;
  2253. this.translate = [m.e, m.f];
  2254. this.scale = [kx, ky];
  2255. this.skew = ky ? Math.atan2(kz, ky) * d3_transformDegrees : 0;
  2256. };
  2257. d3_transform.prototype.toString = function() {
  2258. return "translate(" + this.translate
  2259. + ")rotate(" + this.rotate
  2260. + ")skewX(" + this.skew
  2261. + ")scale(" + this.scale
  2262. + ")";
  2263. };
  2264. function d3_transformDot(a, b) {
  2265. return a[0] * b[0] + a[1] * b[1];
  2266. }
  2267. function d3_transformNormalize(a) {
  2268. var k = Math.sqrt(d3_transformDot(a, a));
  2269. if (k) {
  2270. a[0] /= k;
  2271. a[1] /= k;
  2272. }
  2273. return k;
  2274. }
  2275. function d3_transformCombine(a, b, k) {
  2276. a[0] += k * b[0];
  2277. a[1] += k * b[1];
  2278. return a;
  2279. }
  2280. var d3_transformDegrees = 180 / Math.PI;
  2281. d3.mouse = function(container) {
  2282. return d3_mousePoint(container, d3_eventSource());
  2283. };
  2284. // https://bugs.webkit.org/show_bug.cgi?id=44083
  2285. var d3_mouse_bug44083 = /WebKit/.test(navigator.userAgent) ? -1 : 0;
  2286. function d3_mousePoint(container, e) {
  2287. var svg = container.ownerSVGElement || container;
  2288. if (svg.createSVGPoint) {
  2289. var point = svg.createSVGPoint();
  2290. if ((d3_mouse_bug44083 < 0) && (window.scrollX || window.scrollY)) {
  2291. svg = d3.select(document.body)
  2292. .append("svg")
  2293. .style("position", "absolute")
  2294. .style("top", 0)
  2295. .style("left", 0);
  2296. var ctm = svg[0][0].getScreenCTM();
  2297. d3_mouse_bug44083 = !(ctm.f || ctm.e);
  2298. svg.remove();
  2299. }
  2300. if (d3_mouse_bug44083) {
  2301. point.x = e.pageX;
  2302. point.y = e.pageY;
  2303. } else {
  2304. point.x = e.clientX;
  2305. point.y = e.clientY;
  2306. }
  2307. point = point.matrixTransform(container.getScreenCTM().inverse());
  2308. return [point.x, point.y];
  2309. }
  2310. var rect = container.getBoundingClientRect();
  2311. return [e.clientX - rect.left - container.clientLeft, e.clientY - rect.top - container.clientTop];
  2312. };
  2313. d3.touches = function(container, touches) {
  2314. if (arguments.length < 2) touches = d3_eventSource().touches;
  2315. return touches ? d3_array(touches).map(function(touch) {
  2316. var point = d3_mousePoint(container, touch);
  2317. point.identifier = touch.identifier;
  2318. return point;
  2319. }) : [];
  2320. };
  2321. function d3_noop() {}
  2322. d3.scale = {};
  2323. function d3_scaleExtent(domain) {
  2324. var start = domain[0], stop = domain[domain.length - 1];
  2325. return start < stop ? [start, stop] : [stop, start];
  2326. }
  2327. function d3_scaleRange(scale) {
  2328. return scale.rangeExtent ? scale.rangeExtent() : d3_scaleExtent(scale.range());
  2329. }
  2330. function d3_scale_nice(domain, nice) {
  2331. var i0 = 0,
  2332. i1 = domain.length - 1,
  2333. x0 = domain[i0],
  2334. x1 = domain[i1],
  2335. dx;
  2336. if (x1 < x0) {
  2337. dx = i0; i0 = i1; i1 = dx;
  2338. dx = x0; x0 = x1; x1 = dx;
  2339. }
  2340. if (dx = x1 - x0) {
  2341. nice = nice(dx);
  2342. domain[i0] = nice.floor(x0);
  2343. domain[i1] = nice.ceil(x1);
  2344. }
  2345. return domain;
  2346. }
  2347. function d3_scale_niceDefault() {
  2348. return Math;
  2349. }
  2350. d3.scale.linear = function() {
  2351. return d3_scale_linear([0, 1], [0, 1], d3.interpolate, false);
  2352. };
  2353. function d3_scale_linear(domain, range, interpolate, clamp) {
  2354. var output,
  2355. input;
  2356. function rescale() {
  2357. var linear = Math.min(domain.length, range.length) > 2 ? d3_scale_polylinear : d3_scale_bilinear,
  2358. uninterpolate = clamp ? d3_uninterpolateClamp : d3_uninterpolateNumber;
  2359. output = linear(domain, range, uninterpolate, interpolate);
  2360. input = linear(range, domain, uninterpolate, d3.interpolate);
  2361. return scale;
  2362. }
  2363. function scale(x) {
  2364. return output(x);
  2365. }
  2366. // Note: requires range is coercible to number!
  2367. scale.invert = function(y) {
  2368. return input(y);
  2369. };
  2370. scale.domain = function(x) {
  2371. if (!arguments.length) return domain;
  2372. domain = x.map(Number);
  2373. return rescale();
  2374. };
  2375. scale.range = function(x) {
  2376. if (!arguments.length) return range;
  2377. range = x;
  2378. return rescale();
  2379. };
  2380. scale.rangeRound = function(x) {
  2381. return scale.range(x).interpolate(d3.interpolateRound);
  2382. };
  2383. scale.clamp = function(x) {
  2384. if (!arguments.length) return clamp;
  2385. clamp = x;
  2386. return rescale();
  2387. };
  2388. scale.interpolate = function(x) {
  2389. if (!arguments.length) return interpolate;
  2390. interpolate = x;
  2391. return rescale();
  2392. };
  2393. scale.ticks = function(m) {
  2394. return d3_scale_linearTicks(domain, m);
  2395. };
  2396. scale.tickFormat = function(m) {
  2397. return d3_scale_linearTickFormat(domain, m);
  2398. };
  2399. scale.nice = function() {
  2400. d3_scale_nice(domain, d3_scale_linearNice);
  2401. return rescale();
  2402. };
  2403. scale.copy = function() {
  2404. return d3_scale_linear(domain, range, interpolate, clamp);
  2405. };
  2406. return rescale();
  2407. }
  2408. function d3_scale_linearRebind(scale, linear) {
  2409. return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp");
  2410. }
  2411. function d3_scale_linearNice(dx) {
  2412. dx = Math.pow(10, Math.round(Math.log(dx) / Math.LN10) - 1);
  2413. return {
  2414. floor: function(x) { return Math.floor(x / dx) * dx; },
  2415. ceil: function(x) { return Math.ceil(x / dx) * dx; }
  2416. };
  2417. }
  2418. function d3_scale_linearTickRange(domain, m) {
  2419. var extent = d3_scaleExtent(domain),
  2420. span = extent[1] - extent[0],
  2421. step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)),
  2422. err = m / span * step;
  2423. // Filter ticks to get closer to the desired count.
  2424. if (err <= .15) step *= 10;
  2425. else if (err <= .35) step *= 5;
  2426. else if (err <= .75) step *= 2;
  2427. // Round start and stop values to step interval.
  2428. extent[0] = Math.ceil(extent[0] / step) * step;
  2429. extent[1] = Math.floor(extent[1] / step) * step + step * .5; // inclusive
  2430. extent[2] = step;
  2431. return extent;
  2432. }
  2433. function d3_scale_linearTicks(domain, m) {
  2434. return d3.range.apply(d3, d3_scale_linearTickRange(domain, m));
  2435. }
  2436. function d3_scale_linearTickFormat(domain, m) {
  2437. return d3.format(",." + Math.max(0, -Math.floor(Math.log(d3_scale_linearTickRange(domain, m)[2]) / Math.LN10 + .01)) + "f");
  2438. }
  2439. function d3_scale_bilinear(domain, range, uninterpolate, interpolate) {
  2440. var u = uninterpolate(domain[0], domain[1]),
  2441. i = interpolate(range[0], range[1]);
  2442. return function(x) {
  2443. return i(u(x));
  2444. };
  2445. }
  2446. function d3_scale_polylinear(domain, range, uninterpolate, interpolate) {
  2447. var u = [],
  2448. i = [],
  2449. j = 0,
  2450. k = Math.min(domain.length, range.length) - 1;
  2451. // Handle descending domains.
  2452. if (domain[k] < domain[0]) {
  2453. domain = domain.slice().reverse();
  2454. range = range.slice().reverse();
  2455. }
  2456. while (++j <= k) {
  2457. u.push(uninterpolate(domain[j - 1], domain[j]));
  2458. i.push(interpolate(range[j - 1], range[j]));
  2459. }
  2460. return function(x) {
  2461. var j = d3.bisect(domain, x, 1, k) - 1;
  2462. return i[j](u[j](x));
  2463. };
  2464. }
  2465. d3.scale.log = function() {
  2466. return d3_scale_log(d3.scale.linear(), d3_scale_logp);
  2467. };
  2468. function d3_scale_log(linear, log) {
  2469. var pow = log.pow;
  2470. function scale(x) {
  2471. return linear(log(x));
  2472. }
  2473. scale.invert = function(x) {
  2474. return pow(linear.invert(x));
  2475. };
  2476. scale.domain = function(x) {
  2477. if (!arguments.length) return linear.domain().map(pow);
  2478. log = x[0] < 0 ? d3_scale_logn : d3_scale_logp;
  2479. pow = log.pow;
  2480. linear.domain(x.map(log));
  2481. return scale;
  2482. };
  2483. scale.nice = function() {
  2484. linear.domain(d3_scale_nice(linear.domain(), d3_scale_niceDefault));
  2485. return scale;
  2486. };
  2487. scale.ticks = function() {
  2488. var extent = d3_scaleExtent(linear.domain()),
  2489. ticks = [];
  2490. if (extent.every(isFinite)) {
  2491. var i = Math.floor(extent[0]),
  2492. j = Math.ceil(extent[1]),
  2493. u = pow(extent[0]),
  2494. v = pow(extent[1]);
  2495. if (log === d3_scale_logn) {
  2496. ticks.push(pow(i));
  2497. for (; i++ < j;) for (var k = 9; k > 0; k--) ticks.push(pow(i) * k);
  2498. } else {
  2499. for (; i < j; i++) for (var k = 1; k < 10; k++) ticks.push(pow(i) * k);
  2500. ticks.push(pow(i));
  2501. }
  2502. for (i = 0; ticks[i] < u; i++) {} // strip small values
  2503. for (j = ticks.length; ticks[j - 1] > v; j--) {} // strip big values
  2504. ticks = ticks.slice(i, j);
  2505. }
  2506. return ticks;
  2507. };
  2508. scale.tickFormat = function(n, format) {
  2509. if (arguments.length < 2) format = d3_scale_logFormat;
  2510. if (arguments.length < 1) return format;
  2511. var k = n / scale.ticks().length,
  2512. f = log === d3_scale_logn ? (e = -1e-12, Math.floor) : (e = 1e-12, Math.ceil),
  2513. e;
  2514. return function(d) {
  2515. return d / pow(f(log(d) + e)) < k ? format(d) : "";
  2516. };
  2517. };
  2518. scale.copy = function() {
  2519. return d3_scale_log(linear.copy(), log);
  2520. };
  2521. return d3_scale_linearRebind(scale, linear);
  2522. }
  2523. var d3_scale_logFormat = d3.format(".0e");
  2524. function d3_scale_logp(x) {
  2525. return Math.log(x < 0 ? 0 : x) / Math.LN10;
  2526. }
  2527. function d3_scale_logn(x) {
  2528. return -Math.log(x > 0 ? 0 : -x) / Math.LN10;
  2529. }
  2530. d3_scale_logp.pow = function(x) {
  2531. return Math.pow(10, x);
  2532. };
  2533. d3_scale_logn.pow = function(x) {
  2534. return -Math.pow(10, -x);
  2535. };
  2536. d3.scale.pow = function() {
  2537. return d3_scale_pow(d3.scale.linear(), 1);
  2538. };
  2539. function d3_scale_pow(linear, exponent) {
  2540. var powp = d3_scale_powPow(exponent),
  2541. powb = d3_scale_powPow(1 / exponent);
  2542. function scale(x) {
  2543. return linear(powp(x));
  2544. }
  2545. scale.invert = function(x) {
  2546. return powb(linear.invert(x));
  2547. };
  2548. scale.domain = function(x) {
  2549. if (!arguments.length) return linear.domain().map(powb);
  2550. linear.domain(x.map(powp));
  2551. return scale;
  2552. };
  2553. scale.ticks = function(m) {
  2554. return d3_scale_linearTicks(scale.domain(), m);
  2555. };
  2556. scale.tickFormat = function(m) {
  2557. return d3_scale_linearTickFormat(scale.domain(), m);
  2558. };
  2559. scale.nice = function() {
  2560. return scale.domain(d3_scale_nice(scale.domain(), d3_scale_linearNice));
  2561. };
  2562. scale.exponent = function(x) {
  2563. if (!arguments.length) return exponent;
  2564. var domain = scale.domain();
  2565. powp = d3_scale_powPow(exponent = x);
  2566. powb = d3_scale_powPow(1 / exponent);
  2567. return scale.domain(domain);
  2568. };
  2569. scale.copy = function() {
  2570. return d3_scale_pow(linear.copy(), exponent);
  2571. };
  2572. return d3_scale_linearRebind(scale, linear);
  2573. }
  2574. function d3_scale_powPow(e) {
  2575. return function(x) {
  2576. return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e);
  2577. };
  2578. }
  2579. d3.scale.sqrt = function() {
  2580. return d3.scale.pow().exponent(.5);
  2581. };
  2582. d3.scale.ordinal = function() {
  2583. return d3_scale_ordinal([], {t: "range", x: []});
  2584. };
  2585. function d3_scale_ordinal(domain, ranger) {
  2586. var index,
  2587. range,
  2588. rangeBand;
  2589. function scale(x) {
  2590. return range[((index.get(x) || index.set(x, domain.push(x))) - 1) % range.length];
  2591. }
  2592. function steps(start, step) {
  2593. return d3.range(domain.length).map(function(i) { return start + step * i; });
  2594. }
  2595. scale.domain = function(x) {
  2596. if (!arguments.length) return domain;
  2597. domain = [];
  2598. index = new d3_Map;
  2599. var i = -1, n = x.length, xi;
  2600. while (++i < n) if (!index.has(xi = x[i])) index.set(xi, domain.push(xi));
  2601. return scale[ranger.t](ranger.x, ranger.p);
  2602. };
  2603. scale.range = function(x) {
  2604. if (!arguments.length) return range;
  2605. range = x;
  2606. rangeBand = 0;
  2607. ranger = {t: "range", x: x};
  2608. return scale;
  2609. };
  2610. scale.rangePoints = function(x, padding) {
  2611. if (arguments.length < 2) padding = 0;
  2612. var start = x[0],
  2613. stop = x[1],
  2614. step = (stop - start) / (domain.length - 1 + padding);
  2615. range = steps(domain.length < 2 ? (start + stop) / 2 : start + step * padding / 2, step);
  2616. rangeBand = 0;
  2617. ranger = {t: "rangePoints", x: x, p: padding};
  2618. return scale;
  2619. };
  2620. scale.rangeBands = function(x, padding) {
  2621. if (arguments.length < 2) padding = 0;
  2622. var reverse = x[1] < x[0],
  2623. start = x[reverse - 0],
  2624. stop = x[1 - reverse],
  2625. step = (stop - start) / (domain.length + padding);
  2626. range = steps(start + step * padding, step);
  2627. if (reverse) range.reverse();
  2628. rangeBand = step * (1 - padding);
  2629. ranger = {t: "rangeBands", x: x, p: padding};
  2630. return scale;
  2631. };
  2632. scale.rangeRoundBands = function(x, padding) {
  2633. if (arguments.length < 2) padding = 0;
  2634. var reverse = x[1] < x[0],
  2635. start = x[reverse - 0],
  2636. stop = x[1 - reverse],
  2637. step = Math.floor((stop - start) / (domain.length + padding)),
  2638. error = stop - start - (domain.length - padding) * step;
  2639. range = steps(start + Math.round(error / 2), step);
  2640. if (reverse) range.reverse();
  2641. rangeBand = Math.round(step * (1 - padding));
  2642. ranger = {t: "rangeRoundBands", x: x, p: padding};
  2643. return scale;
  2644. };
  2645. scale.rangeBand = function() {
  2646. return rangeBand;
  2647. };
  2648. scale.rangeExtent = function() {
  2649. return d3_scaleExtent(ranger.x);
  2650. };
  2651. scale.copy = function() {
  2652. return d3_scale_ordinal(domain, ranger);
  2653. };
  2654. return scale.domain(domain);
  2655. }
  2656. /*
  2657. * This product includes color specifications and designs developed by Cynthia
  2658. * Brewer (http://colorbrewer.org/). See lib/colorbrewer for more information.
  2659. */
  2660. d3.scale.category10 = function() {
  2661. return d3.scale.ordinal().range(d3_category10);
  2662. };
  2663. d3.scale.category20 = function() {
  2664. return d3.scale.ordinal().range(d3_category20);
  2665. };
  2666. d3.scale.category20b = function() {
  2667. return d3.scale.ordinal().range(d3_category20b);
  2668. };
  2669. d3.scale.category20c = function() {
  2670. return d3.scale.ordinal().range(d3_category20c);
  2671. };
  2672. var d3_category10 = [
  2673. "#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd",
  2674. "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"
  2675. ];
  2676. var d3_category20 = [
  2677. "#1f77b4", "#aec7e8",
  2678. "#ff7f0e", "#ffbb78",
  2679. "#2ca02c", "#98df8a",
  2680. "#d62728", "#ff9896",
  2681. "#9467bd", "#c5b0d5",
  2682. "#8c564b", "#c49c94",
  2683. "#e377c2", "#f7b6d2",
  2684. "#7f7f7f", "#c7c7c7",
  2685. "#bcbd22", "#dbdb8d",
  2686. "#17becf", "#9edae5"
  2687. ];
  2688. var d3_category20b = [
  2689. "#393b79", "#5254a3", "#6b6ecf", "#9c9ede",
  2690. "#637939", "#8ca252", "#b5cf6b", "#cedb9c",
  2691. "#8c6d31", "#bd9e39", "#e7ba52", "#e7cb94",
  2692. "#843c39", "#ad494a", "#d6616b", "#e7969c",
  2693. "#7b4173", "#a55194", "#ce6dbd", "#de9ed6"
  2694. ];
  2695. var d3_category20c = [
  2696. "#3182bd", "#6baed6", "#9ecae1", "#c6dbef",
  2697. "#e6550d", "#fd8d3c", "#fdae6b", "#fdd0a2",
  2698. "#31a354", "#74c476", "#a1d99b", "#c7e9c0",
  2699. "#756bb1", "#9e9ac8", "#bcbddc", "#dadaeb",
  2700. "#636363", "#969696", "#bdbdbd", "#d9d9d9"
  2701. ];
  2702. d3.scale.quantile = function() {
  2703. return d3_scale_quantile([], []);
  2704. };
  2705. function d3_scale_quantile(domain, range) {
  2706. var thresholds;
  2707. function rescale() {
  2708. var k = 0,
  2709. n = domain.length,
  2710. q = range.length;
  2711. thresholds = [];
  2712. while (++k < q) thresholds[k - 1] = d3.quantile(domain, k / q);
  2713. return scale;
  2714. }
  2715. function scale(x) {
  2716. if (isNaN(x = +x)) return NaN;
  2717. return range[d3.bisect(thresholds, x)];
  2718. }
  2719. scale.domain = function(x) {
  2720. if (!arguments.length) return domain;
  2721. domain = x.filter(function(d) { return !isNaN(d); }).sort(d3.ascending);
  2722. return rescale();
  2723. };
  2724. scale.range = function(x) {
  2725. if (!arguments.length) return range;
  2726. range = x;
  2727. return rescale();
  2728. };
  2729. scale.quantiles = function() {
  2730. return thresholds;
  2731. };
  2732. scale.copy = function() {
  2733. return d3_scale_quantile(domain, range); // copy on write!
  2734. };
  2735. return rescale();
  2736. }
  2737. d3.scale.quantize = function() {
  2738. return d3_scale_quantize(0, 1, [0, 1]);
  2739. };
  2740. function d3_scale_quantize(x0, x1, range) {
  2741. var kx, i;
  2742. function scale(x) {
  2743. return range[Math.max(0, Math.min(i, Math.floor(kx * (x - x0))))];
  2744. }
  2745. function rescale() {
  2746. kx = range.length / (x1 - x0);
  2747. i = range.length - 1;
  2748. return scale;
  2749. }
  2750. scale.domain = function(x) {
  2751. if (!arguments.length) return [x0, x1];
  2752. x0 = +x[0];
  2753. x1 = +x[x.length - 1];
  2754. return rescale();
  2755. };
  2756. scale.range = function(x) {
  2757. if (!arguments.length) return range;
  2758. range = x;
  2759. return rescale();
  2760. };
  2761. scale.copy = function() {
  2762. return d3_scale_quantize(x0, x1, range); // copy on write
  2763. };
  2764. return rescale();
  2765. }
  2766. d3.scale.identity = function() {
  2767. return d3_scale_identity([0, 1]);
  2768. };
  2769. function d3_scale_identity(domain) {
  2770. function identity(x) { return +x; }
  2771. identity.invert = identity;
  2772. identity.domain = identity.range = function(x) {
  2773. if (!arguments.length) return domain;
  2774. domain = x.map(identity);
  2775. return identity;
  2776. };
  2777. identity.ticks = function(m) {
  2778. return d3_scale_linearTicks(domain, m);
  2779. };
  2780. identity.tickFormat = function(m) {
  2781. return d3_scale_linearTickFormat(domain, m);
  2782. };
  2783. identity.copy = function() {
  2784. return d3_scale_identity(domain);
  2785. };
  2786. return identity;
  2787. }
  2788. d3.svg = {};
  2789. d3.svg.arc = function() {
  2790. var innerRadius = d3_svg_arcInnerRadius,
  2791. outerRadius = d3_svg_arcOuterRadius,
  2792. startAngle = d3_svg_arcStartAngle,
  2793. endAngle = d3_svg_arcEndAngle;
  2794. function arc() {
  2795. var r0 = innerRadius.apply(this, arguments),
  2796. r1 = outerRadius.apply(this, arguments),
  2797. a0 = startAngle.apply(this, arguments) + d3_svg_arcOffset,
  2798. a1 = endAngle.apply(this, arguments) + d3_svg_arcOffset,
  2799. da = (a1 < a0 && (da = a0, a0 = a1, a1 = da), a1 - a0),
  2800. df = da < Math.PI ? "0" : "1",
  2801. c0 = Math.cos(a0),
  2802. s0 = Math.sin(a0),
  2803. c1 = Math.cos(a1),
  2804. s1 = Math.sin(a1);
  2805. return da >= d3_svg_arcMax
  2806. ? (r0
  2807. ? "M0," + r1
  2808. + "A" + r1 + "," + r1 + " 0 1,1 0," + (-r1)
  2809. + "A" + r1 + "," + r1 + " 0 1,1 0," + r1
  2810. + "M0," + r0
  2811. + "A" + r0 + "," + r0 + " 0 1,0 0," + (-r0)
  2812. + "A" + r0 + "," + r0 + " 0 1,0 0," + r0
  2813. + "Z"
  2814. : "M0," + r1
  2815. + "A" + r1 + "," + r1 + " 0 1,1 0," + (-r1)
  2816. + "A" + r1 + "," + r1 + " 0 1,1 0," + r1
  2817. + "Z")
  2818. : (r0
  2819. ? "M" + r1 * c0 + "," + r1 * s0
  2820. + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1
  2821. + "L" + r0 * c1 + "," + r0 * s1
  2822. + "A" + r0 + "," + r0 + " 0 " + df + ",0 " + r0 * c0 + "," + r0 * s0
  2823. + "Z"
  2824. : "M" + r1 * c0 + "," + r1 * s0
  2825. + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1
  2826. + "L0,0"
  2827. + "Z");
  2828. }
  2829. arc.innerRadius = function(v) {
  2830. if (!arguments.length) return innerRadius;
  2831. innerRadius = d3.functor(v);
  2832. return arc;
  2833. };
  2834. arc.outerRadius = function(v) {
  2835. if (!arguments.length) return outerRadius;
  2836. outerRadius = d3.functor(v);
  2837. return arc;
  2838. };
  2839. arc.startAngle = function(v) {
  2840. if (!arguments.length) return startAngle;
  2841. startAngle = d3.functor(v);
  2842. return arc;
  2843. };
  2844. arc.endAngle = function(v) {
  2845. if (!arguments.length) return endAngle;
  2846. endAngle = d3.functor(v);
  2847. return arc;
  2848. };
  2849. arc.centroid = function() {
  2850. var r = (innerRadius.apply(this, arguments)
  2851. + outerRadius.apply(this, arguments)) / 2,
  2852. a = (startAngle.apply(this, arguments)
  2853. + endAngle.apply(this, arguments)) / 2 + d3_svg_arcOffset;
  2854. return [Math.cos(a) * r, Math.sin(a) * r];
  2855. };
  2856. return arc;
  2857. };
  2858. var d3_svg_arcOffset = -Math.PI / 2,
  2859. d3_svg_arcMax = 2 * Math.PI - 1e-6;
  2860. function d3_svg_arcInnerRadius(d) {
  2861. return d.innerRadius;
  2862. }
  2863. function d3_svg_arcOuterRadius(d) {
  2864. return d.outerRadius;
  2865. }
  2866. function d3_svg_arcStartAngle(d) {
  2867. return d.startAngle;
  2868. }
  2869. function d3_svg_arcEndAngle(d) {
  2870. return d.endAngle;
  2871. }
  2872. function d3_svg_line(projection) {
  2873. var x = d3_svg_lineX,
  2874. y = d3_svg_lineY,
  2875. interpolate = d3_svg_lineInterpolatorDefault,
  2876. interpolator = d3_svg_lineInterpolators.get(interpolate),
  2877. tension = .7;
  2878. function line(d) {
  2879. return d.length < 1 ? null : "M" + interpolator(projection(d3_svg_linePoints(this, d, x, y)), tension);
  2880. }
  2881. line.x = function(v) {
  2882. if (!arguments.length) return x;
  2883. x = v;
  2884. return line;
  2885. };
  2886. line.y = function(v) {
  2887. if (!arguments.length) return y;
  2888. y = v;
  2889. return line;
  2890. };
  2891. line.interpolate = function(v) {
  2892. if (!arguments.length) return interpolate;
  2893. if (!d3_svg_lineInterpolators.has(v += "")) v = d3_svg_lineInterpolatorDefault;
  2894. interpolator = d3_svg_lineInterpolators.get(interpolate = v);
  2895. return line;
  2896. };
  2897. line.tension = function(v) {
  2898. if (!arguments.length) return tension;
  2899. tension = v;
  2900. return line;
  2901. };
  2902. return line;
  2903. }
  2904. d3.svg.line = function() {
  2905. return d3_svg_line(Object);
  2906. };
  2907. // Converts the specified array of data into an array of points
  2908. // (x-y tuples), by evaluating the specified `x` and `y` functions on each
  2909. // data point. The `this` context of the evaluated functions is the specified
  2910. // "self" object; each function is passed the current datum and index.
  2911. function d3_svg_linePoints(self, d, x, y) {
  2912. var points = [],
  2913. i = -1,
  2914. n = d.length,
  2915. fx = typeof x === "function",
  2916. fy = typeof y === "function",
  2917. value;
  2918. if (fx && fy) {
  2919. while (++i < n) points.push([
  2920. x.call(self, value = d[i], i),
  2921. y.call(self, value, i)
  2922. ]);
  2923. } else if (fx) {
  2924. while (++i < n) points.push([x.call(self, d[i], i), y]);
  2925. } else if (fy) {
  2926. while (++i < n) points.push([x, y.call(self, d[i], i)]);
  2927. } else {
  2928. while (++i < n) points.push([x, y]);
  2929. }
  2930. return points;
  2931. }
  2932. // The default `x` property, which references d[0].
  2933. function d3_svg_lineX(d) {
  2934. return d[0];
  2935. }
  2936. // The default `y` property, which references d[1].
  2937. function d3_svg_lineY(d) {
  2938. return d[1];
  2939. }
  2940. var d3_svg_lineInterpolatorDefault = "linear";
  2941. // The various interpolators supported by the `line` class.
  2942. var d3_svg_lineInterpolators = d3.map({
  2943. "linear": d3_svg_lineLinear,
  2944. "step-before": d3_svg_lineStepBefore,
  2945. "step-after": d3_svg_lineStepAfter,
  2946. "basis": d3_svg_lineBasis,
  2947. "basis-open": d3_svg_lineBasisOpen,
  2948. "basis-closed": d3_svg_lineBasisClosed,
  2949. "bundle": d3_svg_lineBundle,
  2950. "cardinal": d3_svg_lineCardinal,
  2951. "cardinal-open": d3_svg_lineCardinalOpen,
  2952. "cardinal-closed": d3_svg_lineCardinalClosed,
  2953. "monotone": d3_svg_lineMonotone
  2954. });
  2955. // Linear interpolation; generates "L" commands.
  2956. function d3_svg_lineLinear(points) {
  2957. var i = 0,
  2958. n = points.length,
  2959. p = points[0],
  2960. path = [p[0], ",", p[1]];
  2961. while (++i < n) path.push("L", (p = points[i])[0], ",", p[1]);
  2962. return path.join("");
  2963. }
  2964. // Step interpolation; generates "H" and "V" commands.
  2965. function d3_svg_lineStepBefore(points) {
  2966. var i = 0,
  2967. n = points.length,
  2968. p = points[0],
  2969. path = [p[0], ",", p[1]];
  2970. while (++i < n) path.push("V", (p = points[i])[1], "H", p[0]);
  2971. return path.join("");
  2972. }
  2973. // Step interpolation; generates "H" and "V" commands.
  2974. function d3_svg_lineStepAfter(points) {
  2975. var i = 0,
  2976. n = points.length,
  2977. p = points[0],
  2978. path = [p[0], ",", p[1]];
  2979. while (++i < n) path.push("H", (p = points[i])[0], "V", p[1]);
  2980. return path.join("");
  2981. }
  2982. // Open cardinal spline interpolation; generates "C" commands.
  2983. function d3_svg_lineCardinalOpen(points, tension) {
  2984. return points.length < 4
  2985. ? d3_svg_lineLinear(points)
  2986. : points[1] + d3_svg_lineHermite(points.slice(1, points.length - 1),
  2987. d3_svg_lineCardinalTangents(points, tension));
  2988. }
  2989. // Closed cardinal spline interpolation; generates "C" commands.
  2990. function d3_svg_lineCardinalClosed(points, tension) {
  2991. return points.length < 3
  2992. ? d3_svg_lineLinear(points)
  2993. : points[0] + d3_svg_lineHermite((points.push(points[0]), points),
  2994. d3_svg_lineCardinalTangents([points[points.length - 2]]
  2995. .concat(points, [points[1]]), tension));
  2996. }
  2997. // Cardinal spline interpolation; generates "C" commands.
  2998. function d3_svg_lineCardinal(points, tension, closed) {
  2999. return points.length < 3
  3000. ? d3_svg_lineLinear(points)
  3001. : points[0] + d3_svg_lineHermite(points,
  3002. d3_svg_lineCardinalTangents(points, tension));
  3003. }
  3004. // Hermite spline construction; generates "C" commands.
  3005. function d3_svg_lineHermite(points, tangents) {
  3006. if (tangents.length < 1
  3007. || (points.length != tangents.length
  3008. && points.length != tangents.length + 2)) {
  3009. return d3_svg_lineLinear(points);
  3010. }
  3011. var quad = points.length != tangents.length,
  3012. path = "",
  3013. p0 = points[0],
  3014. p = points[1],
  3015. t0 = tangents[0],
  3016. t = t0,
  3017. pi = 1;
  3018. if (quad) {
  3019. path += "Q" + (p[0] - t0[0] * 2 / 3) + "," + (p[1] - t0[1] * 2 / 3)
  3020. + "," + p[0] + "," + p[1];
  3021. p0 = points[1];
  3022. pi = 2;
  3023. }
  3024. if (tangents.length > 1) {
  3025. t = tangents[1];
  3026. p = points[pi];
  3027. pi++;
  3028. path += "C" + (p0[0] + t0[0]) + "," + (p0[1] + t0[1])
  3029. + "," + (p[0] - t[0]) + "," + (p[1] - t[1])
  3030. + "," + p[0] + "," + p[1];
  3031. for (var i = 2; i < tangents.length; i++, pi++) {
  3032. p = points[pi];
  3033. t = tangents[i];
  3034. path += "S" + (p[0] - t[0]) + "," + (p[1] - t[1])
  3035. + "," + p[0] + "," + p[1];
  3036. }
  3037. }
  3038. if (quad) {
  3039. var lp = points[pi];
  3040. path += "Q" + (p[0] + t[0] * 2 / 3) + "," + (p[1] + t[1] * 2 / 3)
  3041. + "," + lp[0] + "," + lp[1];
  3042. }
  3043. return path;
  3044. }
  3045. // Generates tangents for a cardinal spline.
  3046. function d3_svg_lineCardinalTangents(points, tension) {
  3047. var tangents = [],
  3048. a = (1 - tension) / 2,
  3049. p0,
  3050. p1 = points[0],
  3051. p2 = points[1],
  3052. i = 1,
  3053. n = points.length;
  3054. while (++i < n) {
  3055. p0 = p1;
  3056. p1 = p2;
  3057. p2 = points[i];
  3058. tangents.push([a * (p2[0] - p0[0]), a * (p2[1] - p0[1])]);
  3059. }
  3060. return tangents;
  3061. }
  3062. // B-spline interpolation; generates "C" commands.
  3063. function d3_svg_lineBasis(points) {
  3064. if (points.length < 3) return d3_svg_lineLinear(points);
  3065. var i = 1,
  3066. n = points.length,
  3067. pi = points[0],
  3068. x0 = pi[0],
  3069. y0 = pi[1],
  3070. px = [x0, x0, x0, (pi = points[1])[0]],
  3071. py = [y0, y0, y0, pi[1]],
  3072. path = [x0, ",", y0];
  3073. d3_svg_lineBasisBezier(path, px, py);
  3074. while (++i < n) {
  3075. pi = points[i];
  3076. px.shift(); px.push(pi[0]);
  3077. py.shift(); py.push(pi[1]);
  3078. d3_svg_lineBasisBezier(path, px, py);
  3079. }
  3080. i = -1;
  3081. while (++i < 2) {
  3082. px.shift(); px.push(pi[0]);
  3083. py.shift(); py.push(pi[1]);
  3084. d3_svg_lineBasisBezier(path, px, py);
  3085. }
  3086. return path.join("");
  3087. }
  3088. // Open B-spline interpolation; generates "C" commands.
  3089. function d3_svg_lineBasisOpen(points) {
  3090. if (points.length < 4) return d3_svg_lineLinear(points);
  3091. var path = [],
  3092. i = -1,
  3093. n = points.length,
  3094. pi,
  3095. px = [0],
  3096. py = [0];
  3097. while (++i < 3) {
  3098. pi = points[i];
  3099. px.push(pi[0]);
  3100. py.push(pi[1]);
  3101. }
  3102. path.push(d3_svg_lineDot4(d3_svg_lineBasisBezier3, px)
  3103. + "," + d3_svg_lineDot4(d3_svg_lineBasisBezier3, py));
  3104. --i; while (++i < n) {
  3105. pi = points[i];
  3106. px.shift(); px.push(pi[0]);
  3107. py.shift(); py.push(pi[1]);
  3108. d3_svg_lineBasisBezier(path, px, py);
  3109. }
  3110. return path.join("");
  3111. }
  3112. // Closed B-spline interpolation; generates "C" commands.
  3113. function d3_svg_lineBasisClosed(points) {
  3114. var path,
  3115. i = -1,
  3116. n = points.length,
  3117. m = n + 4,
  3118. pi,
  3119. px = [],
  3120. py = [];
  3121. while (++i < 4) {
  3122. pi = points[i % n];
  3123. px.push(pi[0]);
  3124. py.push(pi[1]);
  3125. }
  3126. path = [
  3127. d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",",
  3128. d3_svg_lineDot4(d3_svg_lineBasisBezier3, py)
  3129. ];
  3130. --i; while (++i < m) {
  3131. pi = points[i % n];
  3132. px.shift(); px.push(pi[0]);
  3133. py.shift(); py.push(pi[1]);
  3134. d3_svg_lineBasisBezier(path, px, py);
  3135. }
  3136. return path.join("");
  3137. }
  3138. function d3_svg_lineBundle(points, tension) {
  3139. var n = points.length - 1,
  3140. x0 = points[0][0],
  3141. y0 = points[0][1],
  3142. dx = points[n][0] - x0,
  3143. dy = points[n][1] - y0,
  3144. i = -1,
  3145. p,
  3146. t;
  3147. while (++i <= n) {
  3148. p = points[i];
  3149. t = i / n;
  3150. p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx);
  3151. p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy);
  3152. }
  3153. return d3_svg_lineBasis(points);
  3154. }
  3155. // Returns the dot product of the given four-element vectors.
  3156. function d3_svg_lineDot4(a, b) {
  3157. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
  3158. }
  3159. // Matrix to transform basis (b-spline) control points to bezier
  3160. // control points. Derived from FvD 11.2.8.
  3161. var d3_svg_lineBasisBezier1 = [0, 2/3, 1/3, 0],
  3162. d3_svg_lineBasisBezier2 = [0, 1/3, 2/3, 0],
  3163. d3_svg_lineBasisBezier3 = [0, 1/6, 2/3, 1/6];
  3164. // Pushes a "C" Bézier curve onto the specified path array, given the
  3165. // two specified four-element arrays which define the control points.
  3166. function d3_svg_lineBasisBezier(path, x, y) {
  3167. path.push(
  3168. "C", d3_svg_lineDot4(d3_svg_lineBasisBezier1, x),
  3169. ",", d3_svg_lineDot4(d3_svg_lineBasisBezier1, y),
  3170. ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, x),
  3171. ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, y),
  3172. ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, x),
  3173. ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, y));
  3174. }
  3175. // Computes the slope from points p0 to p1.
  3176. function d3_svg_lineSlope(p0, p1) {
  3177. return (p1[1] - p0[1]) / (p1[0] - p0[0]);
  3178. }
  3179. // Compute three-point differences for the given points.
  3180. // http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Finite_difference
  3181. function d3_svg_lineFiniteDifferences(points) {
  3182. var i = 0,
  3183. j = points.length - 1,
  3184. m = [],
  3185. p0 = points[0],
  3186. p1 = points[1],
  3187. d = m[0] = d3_svg_lineSlope(p0, p1);
  3188. while (++i < j) {
  3189. m[i] = d + (d = d3_svg_lineSlope(p0 = p1, p1 = points[i + 1]));
  3190. }
  3191. m[i] = d;
  3192. return m;
  3193. }
  3194. // Interpolates the given points using Fritsch-Carlson Monotone cubic Hermite
  3195. // interpolation. Returns an array of tangent vectors. For details, see
  3196. // http://en.wikipedia.org/wiki/Monotone_cubic_interpolation
  3197. function d3_svg_lineMonotoneTangents(points) {
  3198. var tangents = [],
  3199. d,
  3200. a,
  3201. b,
  3202. s,
  3203. m = d3_svg_lineFiniteDifferences(points),
  3204. i = -1,
  3205. j = points.length - 1;
  3206. // The first two steps are done by computing finite-differences:
  3207. // 1. Compute the slopes of the secant lines between successive points.
  3208. // 2. Initialize the tangents at every point as the average of the secants.
  3209. // Then, for each segment…
  3210. while (++i < j) {
  3211. d = d3_svg_lineSlope(points[i], points[i + 1]);
  3212. // 3. If two successive yk = y{k + 1} are equal (i.e., d is zero), then set
  3213. // mk = m{k + 1} = 0 as the spline connecting these points must be flat to
  3214. // preserve monotonicity. Ignore step 4 and 5 for those k.
  3215. if (Math.abs(d) < 1e-6) {
  3216. m[i] = m[i + 1] = 0;
  3217. } else {
  3218. // 4. Let ak = mk / dk and bk = m{k + 1} / dk.
  3219. a = m[i] / d;
  3220. b = m[i + 1] / d;
  3221. // 5. Prevent overshoot and ensure monotonicity by restricting the
  3222. // magnitude of vector <ak, bk> to a circle of radius 3.
  3223. s = a * a + b * b;
  3224. if (s > 9) {
  3225. s = d * 3 / Math.sqrt(s);
  3226. m[i] = s * a;
  3227. m[i + 1] = s * b;
  3228. }
  3229. }
  3230. }
  3231. // Compute the normalized tangent vector from the slopes. Note that if x is
  3232. // not monotonic, it's possible that the slope will be infinite, so we protect
  3233. // against NaN by setting the coordinate to zero.
  3234. i = -1; while (++i <= j) {
  3235. s = (points[Math.min(j, i + 1)][0] - points[Math.max(0, i - 1)][0])
  3236. / (6 * (1 + m[i] * m[i]));
  3237. tangents.push([s || 0, m[i] * s || 0]);
  3238. }
  3239. return tangents;
  3240. }
  3241. function d3_svg_lineMonotone(points) {
  3242. return points.length < 3
  3243. ? d3_svg_lineLinear(points)
  3244. : points[0] +
  3245. d3_svg_lineHermite(points, d3_svg_lineMonotoneTangents(points));
  3246. }
  3247. d3.svg.line.radial = function() {
  3248. var line = d3_svg_line(d3_svg_lineRadial);
  3249. line.radius = line.x, delete line.x;
  3250. line.angle = line.y, delete line.y;
  3251. return line;
  3252. };
  3253. function d3_svg_lineRadial(points) {
  3254. var point,
  3255. i = -1,
  3256. n = points.length,
  3257. r,
  3258. a;
  3259. while (++i < n) {
  3260. point = points[i];
  3261. r = point[0];
  3262. a = point[1] + d3_svg_arcOffset;
  3263. point[0] = r * Math.cos(a);
  3264. point[1] = r * Math.sin(a);
  3265. }
  3266. return points;
  3267. }
  3268. function d3_svg_area(projection) {
  3269. var x0 = d3_svg_lineX,
  3270. x1 = d3_svg_lineX,
  3271. y0 = 0,
  3272. y1 = d3_svg_lineY,
  3273. interpolate,
  3274. i0,
  3275. i1,
  3276. tension = .7;
  3277. function area(d) {
  3278. if (d.length < 1) return null;
  3279. var points0 = d3_svg_linePoints(this, d, x0, y0),
  3280. points1 = d3_svg_linePoints(this, d, x0 === x1 ? d3_svg_areaX(points0) : x1, y0 === y1 ? d3_svg_areaY(points0) : y1);
  3281. return "M" + i0(projection(points1), tension)
  3282. + "L" + i1(projection(points0.reverse()), tension)
  3283. + "Z";
  3284. }
  3285. area.x = function(x) {
  3286. if (!arguments.length) return x1;
  3287. x0 = x1 = x;
  3288. return area;
  3289. };
  3290. area.x0 = function(x) {
  3291. if (!arguments.length) return x0;
  3292. x0 = x;
  3293. return area;
  3294. };
  3295. area.x1 = function(x) {
  3296. if (!arguments.length) return x1;
  3297. x1 = x;
  3298. return area;
  3299. };
  3300. area.y = function(y) {
  3301. if (!arguments.length) return y1;
  3302. y0 = y1 = y;
  3303. return area;
  3304. };
  3305. area.y0 = function(y) {
  3306. if (!arguments.length) return y0;
  3307. y0 = y;
  3308. return area;
  3309. };
  3310. area.y1 = function(y) {
  3311. if (!arguments.length) return y1;
  3312. y1 = y;
  3313. return area;
  3314. };
  3315. area.interpolate = function(x) {
  3316. if (!arguments.length) return interpolate;
  3317. if (!d3_svg_lineInterpolators.has(x += "")) x = d3_svg_lineInterpolatorDefault;
  3318. i0 = d3_svg_lineInterpolators.get(interpolate = x);
  3319. i1 = i0.reverse || i0;
  3320. return area;
  3321. };
  3322. area.tension = function(x) {
  3323. if (!arguments.length) return tension;
  3324. tension = x;
  3325. return area;
  3326. };
  3327. return area.interpolate("linear");
  3328. }
  3329. d3_svg_lineStepBefore.reverse = d3_svg_lineStepAfter;
  3330. d3_svg_lineStepAfter.reverse = d3_svg_lineStepBefore;
  3331. d3.svg.area = function() {
  3332. return d3_svg_area(Object);
  3333. };
  3334. function d3_svg_areaX(points) {
  3335. return function(d, i) {
  3336. return points[i][0];
  3337. };
  3338. }
  3339. function d3_svg_areaY(points) {
  3340. return function(d, i) {
  3341. return points[i][1];
  3342. };
  3343. }
  3344. d3.svg.area.radial = function() {
  3345. var area = d3_svg_area(d3_svg_lineRadial);
  3346. area.radius = area.x, delete area.x;
  3347. area.innerRadius = area.x0, delete area.x0;
  3348. area.outerRadius = area.x1, delete area.x1;
  3349. area.angle = area.y, delete area.y;
  3350. area.startAngle = area.y0, delete area.y0;
  3351. area.endAngle = area.y1, delete area.y1;
  3352. return area;
  3353. };
  3354. d3.svg.chord = function() {
  3355. var source = d3_svg_chordSource,
  3356. target = d3_svg_chordTarget,
  3357. radius = d3_svg_chordRadius,
  3358. startAngle = d3_svg_arcStartAngle,
  3359. endAngle = d3_svg_arcEndAngle;
  3360. // TODO Allow control point to be customized.
  3361. function chord(d, i) {
  3362. var s = subgroup(this, source, d, i),
  3363. t = subgroup(this, target, d, i);
  3364. return "M" + s.p0
  3365. + arc(s.r, s.p1, s.a1 - s.a0) + (equals(s, t)
  3366. ? curve(s.r, s.p1, s.r, s.p0)
  3367. : curve(s.r, s.p1, t.r, t.p0)
  3368. + arc(t.r, t.p1, t.a1 - t.a0)
  3369. + curve(t.r, t.p1, s.r, s.p0))
  3370. + "Z";
  3371. }
  3372. function subgroup(self, f, d, i) {
  3373. var subgroup = f.call(self, d, i),
  3374. r = radius.call(self, subgroup, i),
  3375. a0 = startAngle.call(self, subgroup, i) + d3_svg_arcOffset,
  3376. a1 = endAngle.call(self, subgroup, i) + d3_svg_arcOffset;
  3377. return {
  3378. r: r,
  3379. a0: a0,
  3380. a1: a1,
  3381. p0: [r * Math.cos(a0), r * Math.sin(a0)],
  3382. p1: [r * Math.cos(a1), r * Math.sin(a1)]
  3383. };
  3384. }
  3385. function equals(a, b) {
  3386. return a.a0 == b.a0 && a.a1 == b.a1;
  3387. }
  3388. function arc(r, p, a) {
  3389. return "A" + r + "," + r + " 0 " + +(a > Math.PI) + ",1 " + p;
  3390. }
  3391. function curve(r0, p0, r1, p1) {
  3392. return "Q 0,0 " + p1;
  3393. }
  3394. chord.radius = function(v) {
  3395. if (!arguments.length) return radius;
  3396. radius = d3.functor(v);
  3397. return chord;
  3398. };
  3399. chord.source = function(v) {
  3400. if (!arguments.length) return source;
  3401. source = d3.functor(v);
  3402. return chord;
  3403. };
  3404. chord.target = function(v) {
  3405. if (!arguments.length) return target;
  3406. target = d3.functor(v);
  3407. return chord;
  3408. };
  3409. chord.startAngle = function(v) {
  3410. if (!arguments.length) return startAngle;
  3411. startAngle = d3.functor(v);
  3412. return chord;
  3413. };
  3414. chord.endAngle = function(v) {
  3415. if (!arguments.length) return endAngle;
  3416. endAngle = d3.functor(v);
  3417. return chord;
  3418. };
  3419. return chord;
  3420. };
  3421. function d3_svg_chordSource(d) {
  3422. return d.source;
  3423. }
  3424. function d3_svg_chordTarget(d) {
  3425. return d.target;
  3426. }
  3427. function d3_svg_chordRadius(d) {
  3428. return d.radius;
  3429. }
  3430. function d3_svg_chordStartAngle(d) {
  3431. return d.startAngle;
  3432. }
  3433. function d3_svg_chordEndAngle(d) {
  3434. return d.endAngle;
  3435. }
  3436. d3.svg.diagonal = function() {
  3437. var source = d3_svg_chordSource,
  3438. target = d3_svg_chordTarget,
  3439. projection = d3_svg_diagonalProjection;
  3440. function diagonal(d, i) {
  3441. var p0 = source.call(this, d, i),
  3442. p3 = target.call(this, d, i),
  3443. m = (p0.y + p3.y) / 2,
  3444. p = [p0, {x: p0.x, y: m}, {x: p3.x, y: m}, p3];
  3445. p = p.map(projection);
  3446. return "M" + p[0] + "C" + p[1] + " " + p[2] + " " + p[3];
  3447. }
  3448. diagonal.source = function(x) {
  3449. if (!arguments.length) return source;
  3450. source = d3.functor(x);
  3451. return diagonal;
  3452. };
  3453. diagonal.target = function(x) {
  3454. if (!arguments.length) return target;
  3455. target = d3.functor(x);
  3456. return diagonal;
  3457. };
  3458. diagonal.projection = function(x) {
  3459. if (!arguments.length) return projection;
  3460. projection = x;
  3461. return diagonal;
  3462. };
  3463. return diagonal;
  3464. };
  3465. function d3_svg_diagonalProjection(d) {
  3466. return [d.x, d.y];
  3467. }
  3468. d3.svg.diagonal.radial = function() {
  3469. var diagonal = d3.svg.diagonal(),
  3470. projection = d3_svg_diagonalProjection,
  3471. projection_ = diagonal.projection;
  3472. diagonal.projection = function(x) {
  3473. return arguments.length
  3474. ? projection_(d3_svg_diagonalRadialProjection(projection = x))
  3475. : projection;
  3476. };
  3477. return diagonal;
  3478. };
  3479. function d3_svg_diagonalRadialProjection(projection) {
  3480. return function() {
  3481. var d = projection.apply(this, arguments),
  3482. r = d[0],
  3483. a = d[1] + d3_svg_arcOffset;
  3484. return [r * Math.cos(a), r * Math.sin(a)];
  3485. };
  3486. }
  3487. d3.svg.mouse = d3.mouse;
  3488. d3.svg.touches = d3.touches;
  3489. d3.svg.symbol = function() {
  3490. var type = d3_svg_symbolType,
  3491. size = d3_svg_symbolSize;
  3492. function symbol(d, i) {
  3493. return (d3_svg_symbols.get(type.call(this, d, i))
  3494. || d3_svg_symbolCircle)
  3495. (size.call(this, d, i));
  3496. }
  3497. symbol.type = function(x) {
  3498. if (!arguments.length) return type;
  3499. type = d3.functor(x);
  3500. return symbol;
  3501. };
  3502. // size of symbol in square pixels
  3503. symbol.size = function(x) {
  3504. if (!arguments.length) return size;
  3505. size = d3.functor(x);
  3506. return symbol;
  3507. };
  3508. return symbol;
  3509. };
  3510. function d3_svg_symbolSize() {
  3511. return 64;
  3512. }
  3513. function d3_svg_symbolType() {
  3514. return "circle";
  3515. }
  3516. function d3_svg_symbolCircle(size) {
  3517. var r = Math.sqrt(size / Math.PI);
  3518. return "M0," + r
  3519. + "A" + r + "," + r + " 0 1,1 0," + (-r)
  3520. + "A" + r + "," + r + " 0 1,1 0," + r
  3521. + "Z";
  3522. }
  3523. // TODO cross-diagonal?
  3524. var d3_svg_symbols = d3.map({
  3525. "circle": d3_svg_symbolCircle,
  3526. "cross": function(size) {
  3527. var r = Math.sqrt(size / 5) / 2;
  3528. return "M" + -3 * r + "," + -r
  3529. + "H" + -r
  3530. + "V" + -3 * r
  3531. + "H" + r
  3532. + "V" + -r
  3533. + "H" + 3 * r
  3534. + "V" + r
  3535. + "H" + r
  3536. + "V" + 3 * r
  3537. + "H" + -r
  3538. + "V" + r
  3539. + "H" + -3 * r
  3540. + "Z";
  3541. },
  3542. "diamond": function(size) {
  3543. var ry = Math.sqrt(size / (2 * d3_svg_symbolTan30)),
  3544. rx = ry * d3_svg_symbolTan30;
  3545. return "M0," + -ry
  3546. + "L" + rx + ",0"
  3547. + " 0," + ry
  3548. + " " + -rx + ",0"
  3549. + "Z";
  3550. },
  3551. "square": function(size) {
  3552. var r = Math.sqrt(size) / 2;
  3553. return "M" + -r + "," + -r
  3554. + "L" + r + "," + -r
  3555. + " " + r + "," + r
  3556. + " " + -r + "," + r
  3557. + "Z";
  3558. },
  3559. "triangle-down": function(size) {
  3560. var rx = Math.sqrt(size / d3_svg_symbolSqrt3),
  3561. ry = rx * d3_svg_symbolSqrt3 / 2;
  3562. return "M0," + ry
  3563. + "L" + rx +"," + -ry
  3564. + " " + -rx + "," + -ry
  3565. + "Z";
  3566. },
  3567. "triangle-up": function(size) {
  3568. var rx = Math.sqrt(size / d3_svg_symbolSqrt3),
  3569. ry = rx * d3_svg_symbolSqrt3 / 2;
  3570. return "M0," + -ry
  3571. + "L" + rx +"," + ry
  3572. + " " + -rx + "," + ry
  3573. + "Z";
  3574. }
  3575. });
  3576. d3.svg.symbolTypes = d3_svg_symbols.keys();
  3577. var d3_svg_symbolSqrt3 = Math.sqrt(3),
  3578. d3_svg_symbolTan30 = Math.tan(30 * Math.PI / 180);
  3579. d3.svg.axis = function() {
  3580. var scale = d3.scale.linear(),
  3581. orient = "bottom",
  3582. tickMajorSize = 6,
  3583. tickMinorSize = 6,
  3584. tickEndSize = 6,
  3585. tickPadding = 3,
  3586. tickArguments_ = [10],
  3587. tickValues = null,
  3588. tickFormat_,
  3589. tickSubdivide = 0;
  3590. function axis(g) {
  3591. g.each(function() {
  3592. var g = d3.select(this);
  3593. // Ticks, or domain values for ordinal scales.
  3594. var ticks = tickValues == null ? (scale.ticks ? scale.ticks.apply(scale, tickArguments_) : scale.domain()) : tickValues,
  3595. tickFormat = tickFormat_ == null ? (scale.tickFormat ? scale.tickFormat.apply(scale, tickArguments_) : String) : tickFormat_;
  3596. // Minor ticks.
  3597. var subticks = d3_svg_axisSubdivide(scale, ticks, tickSubdivide),
  3598. subtick = g.selectAll(".minor").data(subticks, String),
  3599. subtickEnter = subtick.enter().insert("line", "g").attr("class", "tick minor").style("opacity", 1e-6),
  3600. subtickExit = d3.transition(subtick.exit()).style("opacity", 1e-6).remove(),
  3601. subtickUpdate = d3.transition(subtick).style("opacity", 1);
  3602. // Major ticks.
  3603. var tick = g.selectAll("g").data(ticks, String),
  3604. tickEnter = tick.enter().insert("g", "path").style("opacity", 1e-6),
  3605. tickExit = d3.transition(tick.exit()).style("opacity", 1e-6).remove(),
  3606. tickUpdate = d3.transition(tick).style("opacity", 1),
  3607. tickTransform;
  3608. // Domain.
  3609. var range = d3_scaleRange(scale),
  3610. path = g.selectAll(".domain").data([0]),
  3611. pathEnter = path.enter().append("path").attr("class", "domain"),
  3612. pathUpdate = d3.transition(path);
  3613. // Stash a snapshot of the new scale, and retrieve the old snapshot.
  3614. var scale1 = scale.copy(),
  3615. scale0 = this.__chart__ || scale1;
  3616. this.__chart__ = scale1;
  3617. tickEnter.append("line").attr("class", "tick");
  3618. tickEnter.append("text");
  3619. tickUpdate.select("text").text(tickFormat);
  3620. switch (orient) {
  3621. case "bottom": {
  3622. tickTransform = d3_svg_axisX;
  3623. subtickEnter.attr("y2", tickMinorSize);
  3624. subtickUpdate.attr("x2", 0).attr("y2", tickMinorSize);
  3625. tickEnter.select("line").attr("y2", tickMajorSize);
  3626. tickEnter.select("text").attr("y", Math.max(tickMajorSize, 0) + tickPadding);
  3627. tickUpdate.select("line").attr("x2", 0).attr("y2", tickMajorSize);
  3628. tickUpdate.select("text").attr("x", 0).attr("y", Math.max(tickMajorSize, 0) + tickPadding).attr("dy", ".71em").attr("text-anchor", "middle");
  3629. pathUpdate.attr("d", "M" + range[0] + "," + tickEndSize + "V0H" + range[1] + "V" + tickEndSize);
  3630. break;
  3631. }
  3632. case "top": {
  3633. tickTransform = d3_svg_axisX;
  3634. subtickEnter.attr("y2", -tickMinorSize);
  3635. subtickUpdate.attr("x2", 0).attr("y2", -tickMinorSize);
  3636. tickEnter.select("line").attr("y2", -tickMajorSize);
  3637. tickEnter.select("text").attr("y", -(Math.max(tickMajorSize, 0) + tickPadding));
  3638. tickUpdate.select("line").attr("x2", 0).attr("y2", -tickMajorSize);
  3639. tickUpdate.select("text").attr("x", 0).attr("y", -(Math.max(tickMajorSize, 0) + tickPadding)).attr("dy", "0em").attr("text-anchor", "middle");
  3640. pathUpdate.attr("d", "M" + range[0] + "," + -tickEndSize + "V0H" + range[1] + "V" + -tickEndSize);
  3641. break;
  3642. }
  3643. case "left": {
  3644. tickTransform = d3_svg_axisY;
  3645. subtickEnter.attr("x2", -tickMinorSize);
  3646. subtickUpdate.attr("x2", -tickMinorSize).attr("y2", 0);
  3647. tickEnter.select("line").attr("x2", -tickMajorSize);
  3648. tickEnter.select("text").attr("x", -(Math.max(tickMajorSize, 0) + tickPadding));
  3649. tickUpdate.select("line").attr("x2", -tickMajorSize).attr("y2", 0);
  3650. tickUpdate.select("text").attr("x", -(Math.max(tickMajorSize, 0) + tickPadding)).attr("y", 0).attr("dy", ".32em").attr("text-anchor", "end");
  3651. pathUpdate.attr("d", "M" + -tickEndSize + "," + range[0] + "H0V" + range[1] + "H" + -tickEndSize);
  3652. break;
  3653. }
  3654. case "right": {
  3655. tickTransform = d3_svg_axisY;
  3656. subtickEnter.attr("x2", tickMinorSize);
  3657. subtickUpdate.attr("x2", tickMinorSize).attr("y2", 0);
  3658. tickEnter.select("line").attr("x2", tickMajorSize);
  3659. tickEnter.select("text").attr("x", Math.max(tickMajorSize, 0) + tickPadding);
  3660. tickUpdate.select("line").attr("x2", tickMajorSize).attr("y2", 0);
  3661. tickUpdate.select("text").attr("x", Math.max(tickMajorSize, 0) + tickPadding).attr("y", 0).attr("dy", ".32em").attr("text-anchor", "start");
  3662. pathUpdate.attr("d", "M" + tickEndSize + "," + range[0] + "H0V" + range[1] + "H" + tickEndSize);
  3663. break;
  3664. }
  3665. }
  3666. // For quantitative scales:
  3667. // - enter new ticks from the old scale
  3668. // - exit old ticks to the new scale
  3669. if (scale.ticks) {
  3670. tickEnter.call(tickTransform, scale0);
  3671. tickUpdate.call(tickTransform, scale1);
  3672. tickExit.call(tickTransform, scale1);
  3673. subtickEnter.call(tickTransform, scale0);
  3674. subtickUpdate.call(tickTransform, scale1);
  3675. subtickExit.call(tickTransform, scale1);
  3676. }
  3677. // For ordinal scales:
  3678. // - any entering ticks are undefined in the old scale
  3679. // - any exiting ticks are undefined in the new scale
  3680. // Therefore, we only need to transition updating ticks.
  3681. else {
  3682. var dx = scale1.rangeBand() / 2, x = function(d) { return scale1(d) + dx; };
  3683. tickEnter.call(tickTransform, x);
  3684. tickUpdate.call(tickTransform, x);
  3685. }
  3686. });
  3687. }
  3688. axis.scale = function(x) {
  3689. if (!arguments.length) return scale;
  3690. scale = x;
  3691. return axis;
  3692. };
  3693. axis.orient = function(x) {
  3694. if (!arguments.length) return orient;
  3695. orient = x;
  3696. return axis;
  3697. };
  3698. axis.ticks = function() {
  3699. if (!arguments.length) return tickArguments_;
  3700. tickArguments_ = arguments;
  3701. return axis;
  3702. };
  3703. axis.tickValues = function(x) {
  3704. if (!arguments.length) return tickValues;
  3705. tickValues = x;
  3706. return axis;
  3707. };
  3708. axis.tickFormat = function(x) {
  3709. if (!arguments.length) return tickFormat_;
  3710. tickFormat_ = x;
  3711. return axis;
  3712. };
  3713. axis.tickSize = function(x, y, z) {
  3714. if (!arguments.length) return tickMajorSize;
  3715. var n = arguments.length - 1;
  3716. tickMajorSize = +x;
  3717. tickMinorSize = n > 1 ? +y : tickMajorSize;
  3718. tickEndSize = n > 0 ? +arguments[n] : tickMajorSize;
  3719. return axis;
  3720. };
  3721. axis.tickPadding = function(x) {
  3722. if (!arguments.length) return tickPadding;
  3723. tickPadding = +x;
  3724. return axis;
  3725. };
  3726. axis.tickSubdivide = function(x) {
  3727. if (!arguments.length) return tickSubdivide;
  3728. tickSubdivide = +x;
  3729. return axis;
  3730. };
  3731. return axis;
  3732. };
  3733. function d3_svg_axisX(selection, x) {
  3734. selection.attr("transform", function(d) { return "translate(" + x(d) + ",0)"; });
  3735. }
  3736. function d3_svg_axisY(selection, y) {
  3737. selection.attr("transform", function(d) { return "translate(0," + y(d) + ")"; });
  3738. }
  3739. function d3_svg_axisSubdivide(scale, ticks, m) {
  3740. subticks = [];
  3741. if (m && ticks.length > 1) {
  3742. var extent = d3_scaleExtent(scale.domain()),
  3743. subticks,
  3744. i = -1,
  3745. n = ticks.length,
  3746. d = (ticks[1] - ticks[0]) / ++m,
  3747. j,
  3748. v;
  3749. while (++i < n) {
  3750. for (j = m; --j > 0;) {
  3751. if ((v = +ticks[i] - j * d) >= extent[0]) {
  3752. subticks.push(v);
  3753. }
  3754. }
  3755. }
  3756. for (--i, j = 0; ++j < m && (v = +ticks[i] + j * d) < extent[1];) {
  3757. subticks.push(v);
  3758. }
  3759. }
  3760. return subticks;
  3761. }
  3762. d3.svg.brush = function() {
  3763. var event = d3_eventDispatch(brush, "brushstart", "brush", "brushend"),
  3764. x = null, // x-scale, optional
  3765. y = null, // y-scale, optional
  3766. resizes = d3_svg_brushResizes[0],
  3767. extent = [[0, 0], [0, 0]], // [x0, y0], [x1, y1], in pixels (integers)
  3768. extentDomain; // the extent in data space, lazily created
  3769. function brush(g) {
  3770. g.each(function() {
  3771. var g = d3.select(this),
  3772. bg = g.selectAll(".background").data([0]),
  3773. fg = g.selectAll(".extent").data([0]),
  3774. tz = g.selectAll(".resize").data(resizes, String),
  3775. e;
  3776. // Prepare the brush container for events.
  3777. g
  3778. .style("pointer-events", "all")
  3779. .on("mousedown.brush", brushstart)
  3780. .on("touchstart.brush", brushstart);
  3781. // An invisible, mouseable area for starting a new brush.
  3782. bg.enter().append("rect")
  3783. .attr("class", "background")
  3784. .style("visibility", "hidden")
  3785. .style("cursor", "crosshair");
  3786. // The visible brush extent; style this as you like!
  3787. fg.enter().append("rect")
  3788. .attr("class", "extent")
  3789. .style("cursor", "move");
  3790. // More invisible rects for resizing the extent.
  3791. tz.enter().append("g")
  3792. .attr("class", function(d) { return "resize " + d; })
  3793. .style("cursor", function(d) { return d3_svg_brushCursor[d]; })
  3794. .append("rect")
  3795. .attr("x", function(d) { return /[ew]$/.test(d) ? -3 : null; })
  3796. .attr("y", function(d) { return /^[ns]/.test(d) ? -3 : null; })
  3797. .attr("width", 6)
  3798. .attr("height", 6)
  3799. .style("visibility", "hidden");
  3800. // Show or hide the resizers.
  3801. tz.style("display", brush.empty() ? "none" : null);
  3802. // Remove any superfluous resizers.
  3803. tz.exit().remove();
  3804. // Initialize the background to fill the defined range.
  3805. // If the range isn't defined, you can post-process.
  3806. if (x) {
  3807. e = d3_scaleRange(x);
  3808. bg.attr("x", e[0]).attr("width", e[1] - e[0]);
  3809. redrawX(g);
  3810. }
  3811. if (y) {
  3812. e = d3_scaleRange(y);
  3813. bg.attr("y", e[0]).attr("height", e[1] - e[0]);
  3814. redrawY(g);
  3815. }
  3816. redraw(g);
  3817. });
  3818. }
  3819. function redraw(g) {
  3820. g.selectAll(".resize").attr("transform", function(d) {
  3821. return "translate(" + extent[+/e$/.test(d)][0] + "," + extent[+/^s/.test(d)][1] + ")";
  3822. });
  3823. }
  3824. function redrawX(g) {
  3825. g.select(".extent").attr("x", extent[0][0]);
  3826. g.selectAll(".extent,.n>rect,.s>rect").attr("width", extent[1][0] - extent[0][0]);
  3827. }
  3828. function redrawY(g) {
  3829. g.select(".extent").attr("y", extent[0][1]);
  3830. g.selectAll(".extent,.e>rect,.w>rect").attr("height", extent[1][1] - extent[0][1]);
  3831. }
  3832. function brushstart() {
  3833. var target = this,
  3834. eventTarget = d3.select(d3.event.target),
  3835. event_ = event.of(target, arguments),
  3836. g = d3.select(target),
  3837. resizing = eventTarget.datum(),
  3838. resizingX = !/^(n|s)$/.test(resizing) && x,
  3839. resizingY = !/^(e|w)$/.test(resizing) && y,
  3840. dragging = eventTarget.classed("extent"),
  3841. center,
  3842. origin = mouse(),
  3843. offset;
  3844. var w = d3.select(window)
  3845. .on("mousemove.brush", brushmove)
  3846. .on("mouseup.brush", brushend)
  3847. .on("touchmove.brush", brushmove)
  3848. .on("touchend.brush", brushend)
  3849. .on("keydown.brush", keydown)
  3850. .on("keyup.brush", keyup);
  3851. // If the extent was clicked on, drag rather than brush;
  3852. // store the point between the mouse and extent origin instead.
  3853. if (dragging) {
  3854. origin[0] = extent[0][0] - origin[0];
  3855. origin[1] = extent[0][1] - origin[1];
  3856. }
  3857. // If a resizer was clicked on, record which side is to be resized.
  3858. // Also, set the origin to the opposite side.
  3859. else if (resizing) {
  3860. var ex = +/w$/.test(resizing),
  3861. ey = +/^n/.test(resizing);
  3862. offset = [extent[1 - ex][0] - origin[0], extent[1 - ey][1] - origin[1]];
  3863. origin[0] = extent[ex][0];
  3864. origin[1] = extent[ey][1];
  3865. }
  3866. // If the ALT key is down when starting a brush, the center is at the mouse.
  3867. else if (d3.event.altKey) center = origin.slice();
  3868. // Propagate the active cursor to the body for the drag duration.
  3869. g.style("pointer-events", "none").selectAll(".resize").style("display", null);
  3870. d3.select("body").style("cursor", eventTarget.style("cursor"));
  3871. // Notify listeners.
  3872. event_({type: "brushstart"});
  3873. brushmove();
  3874. d3_eventCancel();
  3875. function mouse() {
  3876. var touches = d3.event.changedTouches;
  3877. return touches ? d3.touches(target, touches)[0] : d3.mouse(target);
  3878. }
  3879. function keydown() {
  3880. if (d3.event.keyCode == 32) {
  3881. if (!dragging) {
  3882. center = null;
  3883. origin[0] -= extent[1][0];
  3884. origin[1] -= extent[1][1];
  3885. dragging = 2;
  3886. }
  3887. d3_eventCancel();
  3888. }
  3889. }
  3890. function keyup() {
  3891. if (d3.event.keyCode == 32 && dragging == 2) {
  3892. origin[0] += extent[1][0];
  3893. origin[1] += extent[1][1];
  3894. dragging = 0;
  3895. d3_eventCancel();
  3896. }
  3897. }
  3898. function brushmove() {
  3899. var point = mouse(),
  3900. moved = false;
  3901. // Preserve the offset for thick resizers.
  3902. if (offset) {
  3903. point[0] += offset[0];
  3904. point[1] += offset[1];
  3905. }
  3906. if (!dragging) {
  3907. // If needed, determine the center from the current extent.
  3908. if (d3.event.altKey) {
  3909. if (!center) center = [(extent[0][0] + extent[1][0]) / 2, (extent[0][1] + extent[1][1]) / 2];
  3910. // Update the origin, for when the ALT key is released.
  3911. origin[0] = extent[+(point[0] < center[0])][0];
  3912. origin[1] = extent[+(point[1] < center[1])][1];
  3913. }
  3914. // When the ALT key is released, we clear the center.
  3915. else center = null;
  3916. }
  3917. // Update the brush extent for each dimension.
  3918. if (resizingX && move1(point, x, 0)) {
  3919. redrawX(g);
  3920. moved = true;
  3921. }
  3922. if (resizingY && move1(point, y, 1)) {
  3923. redrawY(g);
  3924. moved = true;
  3925. }
  3926. // Final redraw and notify listeners.
  3927. if (moved) {
  3928. redraw(g);
  3929. event_({type: "brush", mode: dragging ? "move" : "resize"});
  3930. }
  3931. }
  3932. function move1(point, scale, i) {
  3933. var range = d3_scaleRange(scale),
  3934. r0 = range[0],
  3935. r1 = range[1],
  3936. position = origin[i],
  3937. size = extent[1][i] - extent[0][i],
  3938. min,
  3939. max;
  3940. // When dragging, reduce the range by the extent size and position.
  3941. if (dragging) {
  3942. r0 -= position;
  3943. r1 -= size + position;
  3944. }
  3945. // Clamp the point so that the extent fits within the range extent.
  3946. min = Math.max(r0, Math.min(r1, point[i]));
  3947. // Compute the new extent bounds.
  3948. if (dragging) {
  3949. max = (min += position) + size;
  3950. } else {
  3951. // If the ALT key is pressed, then preserve the center of the extent.
  3952. if (center) position = Math.max(r0, Math.min(r1, 2 * center[i] - min));
  3953. // Compute the min and max of the position and point.
  3954. if (position < min) {
  3955. max = min;
  3956. min = position;
  3957. } else {
  3958. max = position;
  3959. }
  3960. }
  3961. // Update the stored bounds.
  3962. if (extent[0][i] !== min || extent[1][i] !== max) {
  3963. extentDomain = null;
  3964. extent[0][i] = min;
  3965. extent[1][i] = max;
  3966. return true;
  3967. }
  3968. }
  3969. function brushend() {
  3970. brushmove();
  3971. // reset the cursor styles
  3972. g.style("pointer-events", "all").selectAll(".resize").style("display", brush.empty() ? "none" : null);
  3973. d3.select("body").style("cursor", null);
  3974. w .on("mousemove.brush", null)
  3975. .on("mouseup.brush", null)
  3976. .on("touchmove.brush", null)
  3977. .on("touchend.brush", null)
  3978. .on("keydown.brush", null)
  3979. .on("keyup.brush", null);
  3980. event_({type: "brushend"});
  3981. d3_eventCancel();
  3982. }
  3983. }
  3984. brush.x = function(z) {
  3985. if (!arguments.length) return x;
  3986. x = z;
  3987. resizes = d3_svg_brushResizes[!x << 1 | !y]; // fore!
  3988. return brush;
  3989. };
  3990. brush.y = function(z) {
  3991. if (!arguments.length) return y;
  3992. y = z;
  3993. resizes = d3_svg_brushResizes[!x << 1 | !y]; // fore!
  3994. return brush;
  3995. };
  3996. brush.extent = function(z) {
  3997. var x0, x1, y0, y1, t;
  3998. // Invert the pixel extent to data-space.
  3999. if (!arguments.length) {
  4000. z = extentDomain || extent;
  4001. if (x) {
  4002. x0 = z[0][0], x1 = z[1][0];
  4003. if (!extentDomain) {
  4004. x0 = extent[0][0], x1 = extent[1][0];
  4005. if (x.invert) x0 = x.invert(x0), x1 = x.invert(x1);
  4006. if (x1 < x0) t = x0, x0 = x1, x1 = t;
  4007. }
  4008. }
  4009. if (y) {
  4010. y0 = z[0][1], y1 = z[1][1];
  4011. if (!extentDomain) {
  4012. y0 = extent[0][1], y1 = extent[1][1];
  4013. if (y.invert) y0 = y.invert(y0), y1 = y.invert(y1);
  4014. if (y1 < y0) t = y0, y0 = y1, y1 = t;
  4015. }
  4016. }
  4017. return x && y ? [[x0, y0], [x1, y1]] : x ? [x0, x1] : y && [y0, y1];
  4018. }
  4019. // Scale the data-space extent to pixels.
  4020. extentDomain = [[0, 0], [0, 0]];
  4021. if (x) {
  4022. x0 = z[0], x1 = z[1];
  4023. if (y) x0 = x0[0], x1 = x1[0];
  4024. extentDomain[0][0] = x0, extentDomain[1][0] = x1;
  4025. if (x.invert) x0 = x(x0), x1 = x(x1);
  4026. if (x1 < x0) t = x0, x0 = x1, x1 = t;
  4027. extent[0][0] = x0 | 0, extent[1][0] = x1 | 0;
  4028. }
  4029. if (y) {
  4030. y0 = z[0], y1 = z[1];
  4031. if (x) y0 = y0[1], y1 = y1[1];
  4032. extentDomain[0][1] = y0, extentDomain[1][1] = y1;
  4033. if (y.invert) y0 = y(y0), y1 = y(y1);
  4034. if (y1 < y0) t = y0, y0 = y1, y1 = t;
  4035. extent[0][1] = y0 | 0, extent[1][1] = y1 | 0;
  4036. }
  4037. return brush;
  4038. };
  4039. brush.clear = function() {
  4040. extentDomain = null;
  4041. extent[0][0] =
  4042. extent[0][1] =
  4043. extent[1][0] =
  4044. extent[1][1] = 0;
  4045. return brush;
  4046. };
  4047. brush.empty = function() {
  4048. return (x && extent[0][0] === extent[1][0])
  4049. || (y && extent[0][1] === extent[1][1]);
  4050. };
  4051. return d3.rebind(brush, event, "on");
  4052. };
  4053. var d3_svg_brushCursor = {
  4054. n: "ns-resize",
  4055. e: "ew-resize",
  4056. s: "ns-resize",
  4057. w: "ew-resize",
  4058. nw: "nwse-resize",
  4059. ne: "nesw-resize",
  4060. se: "nwse-resize",
  4061. sw: "nesw-resize"
  4062. };
  4063. var d3_svg_brushResizes = [
  4064. ["n", "e", "s", "w", "nw", "ne", "se", "sw"],
  4065. ["e", "w"],
  4066. ["n", "s"],
  4067. []
  4068. ];
  4069. d3.behavior = {};
  4070. // TODO Track touch points by identifier.
  4071. d3.behavior.drag = function() {
  4072. var event = d3_eventDispatch(drag, "drag", "dragstart", "dragend"),
  4073. origin = null;
  4074. function drag() {
  4075. this.on("mousedown.drag", mousedown)
  4076. .on("touchstart.drag", mousedown);
  4077. }
  4078. function mousedown() {
  4079. var target = this,
  4080. event_ = event.of(target, arguments),
  4081. eventTarget = d3.event.target,
  4082. offset,
  4083. origin_ = point(),
  4084. moved = 0;
  4085. var w = d3.select(window)
  4086. .on("mousemove.drag", dragmove)
  4087. .on("touchmove.drag", dragmove)
  4088. .on("mouseup.drag", dragend, true)
  4089. .on("touchend.drag", dragend, true);
  4090. if (origin) {
  4091. offset = origin.apply(target, arguments);
  4092. offset = [offset.x - origin_[0], offset.y - origin_[1]];
  4093. } else {
  4094. offset = [0, 0];
  4095. }
  4096. event_({type: "dragstart"});
  4097. function point() {
  4098. var p = target.parentNode,
  4099. t = d3.event.changedTouches;
  4100. return t ? d3.touches(p, t)[0] : d3.mouse(p);
  4101. }
  4102. function dragmove() {
  4103. if (!target.parentNode) return dragend(); // target removed from DOM
  4104. var p = point(),
  4105. dx = p[0] - origin_[0],
  4106. dy = p[1] - origin_[1];
  4107. moved |= dx | dy;
  4108. origin_ = p;
  4109. d3_eventCancel();
  4110. event_({type: "drag", x: p[0] + offset[0], y: p[1] + offset[1], dx: dx, dy: dy});
  4111. }
  4112. function dragend() {
  4113. event_({type: "dragend"});
  4114. // if moved, prevent the mouseup (and possibly click) from propagating
  4115. if (moved) {
  4116. d3_eventCancel();
  4117. if (d3.event.target === eventTarget) w.on("click.drag", click, true);
  4118. }
  4119. w .on("mousemove.drag", null)
  4120. .on("touchmove.drag", null)
  4121. .on("mouseup.drag", null)
  4122. .on("touchend.drag", null);
  4123. }
  4124. // prevent the subsequent click from propagating (e.g., for anchors)
  4125. function click() {
  4126. d3_eventCancel();
  4127. w.on("click.drag", null);
  4128. }
  4129. }
  4130. drag.origin = function(x) {
  4131. if (!arguments.length) return origin;
  4132. origin = x;
  4133. return drag;
  4134. };
  4135. return d3.rebind(drag, event, "on");
  4136. };
  4137. d3.behavior.zoom = function() {
  4138. var translate = [0, 0],
  4139. translate0, // translate when we started zooming (to avoid drift)
  4140. scale = 1,
  4141. scale0, // scale when we started touching
  4142. scaleExtent = d3_behavior_zoomInfinity,
  4143. event = d3_eventDispatch(zoom, "zoom"),
  4144. x0,
  4145. x1,
  4146. y0,
  4147. y1,
  4148. touchtime; // time of last touchstart (to detect double-tap)
  4149. function zoom() {
  4150. this
  4151. .on("mousedown.zoom", mousedown)
  4152. .on("mousewheel.zoom", mousewheel)
  4153. .on("mousemove.zoom", mousemove)
  4154. .on("DOMMouseScroll.zoom", mousewheel)
  4155. .on("dblclick.zoom", dblclick)
  4156. .on("touchstart.zoom", touchstart)
  4157. .on("touchmove.zoom", touchmove)
  4158. .on("touchend.zoom", touchstart);
  4159. }
  4160. zoom.translate = function(x) {
  4161. if (!arguments.length) return translate;
  4162. translate = x.map(Number);
  4163. return zoom;
  4164. };
  4165. zoom.scale = function(x) {
  4166. if (!arguments.length) return scale;
  4167. scale = +x;
  4168. return zoom;
  4169. };
  4170. zoom.scaleExtent = function(x) {
  4171. if (!arguments.length) return scaleExtent;
  4172. scaleExtent = x == null ? d3_behavior_zoomInfinity : x.map(Number);
  4173. return zoom;
  4174. };
  4175. zoom.x = function(z) {
  4176. if (!arguments.length) return x1;
  4177. x1 = z;
  4178. x0 = z.copy();
  4179. return zoom;
  4180. };
  4181. zoom.y = function(z) {
  4182. if (!arguments.length) return y1;
  4183. y1 = z;
  4184. y0 = z.copy();
  4185. return zoom;
  4186. };
  4187. function location(p) {
  4188. return [(p[0] - translate[0]) / scale, (p[1] - translate[1]) / scale];
  4189. }
  4190. function point(l) {
  4191. return [l[0] * scale + translate[0], l[1] * scale + translate[1]];
  4192. }
  4193. function scaleTo(s) {
  4194. scale = Math.max(scaleExtent[0], Math.min(scaleExtent[1], s));
  4195. }
  4196. function translateTo(p, l) {
  4197. l = point(l);
  4198. translate[0] += p[0] - l[0];
  4199. translate[1] += p[1] - l[1];
  4200. }
  4201. function dispatch(event) {
  4202. if (x1) x1.domain(x0.range().map(function(x) { return (x - translate[0]) / scale; }).map(x0.invert));
  4203. if (y1) y1.domain(y0.range().map(function(y) { return (y - translate[1]) / scale; }).map(y0.invert));
  4204. d3.event.preventDefault();
  4205. event({type: "zoom", scale: scale, translate: translate});
  4206. }
  4207. function mousedown() {
  4208. var target = this,
  4209. event_ = event.of(target, arguments),
  4210. eventTarget = d3.event.target,
  4211. moved = 0,
  4212. w = d3.select(window).on("mousemove.zoom", mousemove).on("mouseup.zoom", mouseup),
  4213. l = location(d3.mouse(target));
  4214. window.focus();
  4215. d3_eventCancel();
  4216. function mousemove() {
  4217. moved = 1;
  4218. translateTo(d3.mouse(target), l);
  4219. dispatch(event_);
  4220. }
  4221. function mouseup() {
  4222. if (moved) d3_eventCancel();
  4223. w.on("mousemove.zoom", null).on("mouseup.zoom", null);
  4224. if (moved && d3.event.target === eventTarget) w.on("click.zoom", click);
  4225. }
  4226. function click() {
  4227. d3_eventCancel();
  4228. w.on("click.zoom", null);
  4229. }
  4230. }
  4231. function mousewheel() {
  4232. if (!translate0) translate0 = location(d3.mouse(this));
  4233. scaleTo(Math.pow(2, d3_behavior_zoomDelta() * .002) * scale);
  4234. translateTo(d3.mouse(this), translate0);
  4235. dispatch(event.of(this, arguments));
  4236. }
  4237. function mousemove() {
  4238. translate0 = null;
  4239. }
  4240. function dblclick() {
  4241. var p = d3.mouse(this), l = location(p);
  4242. scaleTo(d3.event.shiftKey ? scale / 2 : scale * 2);
  4243. translateTo(p, l);
  4244. dispatch(event.of(this, arguments));
  4245. }
  4246. function touchstart() {
  4247. var touches = d3.touches(this),
  4248. now = Date.now();
  4249. scale0 = scale;
  4250. translate0 = {};
  4251. touches.forEach(function(t) { translate0[t.identifier] = location(t); });
  4252. d3_eventCancel();
  4253. if ((touches.length === 1) && (now - touchtime < 500)) { // dbltap
  4254. var p = touches[0], l = location(touches[0]);
  4255. scaleTo(scale * 2);
  4256. translateTo(p, l);
  4257. dispatch(event.of(this, arguments));
  4258. }
  4259. touchtime = now;
  4260. }
  4261. function touchmove() {
  4262. var touches = d3.touches(this),
  4263. p0 = touches[0],
  4264. l0 = translate0[p0.identifier];
  4265. if (p1 = touches[1]) {
  4266. var p1, l1 = translate0[p1.identifier];
  4267. p0 = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2];
  4268. l0 = [(l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2];
  4269. scaleTo(d3.event.scale * scale0);
  4270. }
  4271. translateTo(p0, l0);
  4272. dispatch(event.of(this, arguments));
  4273. }
  4274. return d3.rebind(zoom, event, "on");
  4275. };
  4276. var d3_behavior_zoomDiv, // for interpreting mousewheel events
  4277. d3_behavior_zoomInfinity = [0, Infinity]; // default scale extent
  4278. function d3_behavior_zoomDelta() {
  4279. // mousewheel events are totally broken!
  4280. // https://bugs.webkit.org/show_bug.cgi?id=40441
  4281. // not only that, but Chrome and Safari differ in re. to acceleration!
  4282. if (!d3_behavior_zoomDiv) {
  4283. d3_behavior_zoomDiv = d3.select("body").append("div")
  4284. .style("visibility", "hidden")
  4285. .style("top", 0)
  4286. .style("height", 0)
  4287. .style("width", 0)
  4288. .style("overflow-y", "scroll")
  4289. .append("div")
  4290. .style("height", "2000px")
  4291. .node().parentNode;
  4292. }
  4293. var e = d3.event, delta;
  4294. try {
  4295. d3_behavior_zoomDiv.scrollTop = 1000;
  4296. d3_behavior_zoomDiv.dispatchEvent(e);
  4297. delta = 1000 - d3_behavior_zoomDiv.scrollTop;
  4298. } catch (error) {
  4299. delta = e.wheelDelta || (-e.detail * 5);
  4300. }
  4301. return delta;
  4302. }
  4303. d3.layout = {};
  4304. // Implements hierarchical edge bundling using Holten's algorithm. For each
  4305. // input link, a path is computed that travels through the tree, up the parent
  4306. // hierarchy to the least common ancestor, and then back down to the destination
  4307. // node. Each path is simply an array of nodes.
  4308. d3.layout.bundle = function() {
  4309. return function(links) {
  4310. var paths = [],
  4311. i = -1,
  4312. n = links.length;
  4313. while (++i < n) paths.push(d3_layout_bundlePath(links[i]));
  4314. return paths;
  4315. };
  4316. };
  4317. function d3_layout_bundlePath(link) {
  4318. var start = link.source,
  4319. end = link.target,
  4320. lca = d3_layout_bundleLeastCommonAncestor(start, end),
  4321. points = [start];
  4322. while (start !== lca) {
  4323. start = start.parent;
  4324. points.push(start);
  4325. }
  4326. var k = points.length;
  4327. while (end !== lca) {
  4328. points.splice(k, 0, end);
  4329. end = end.parent;
  4330. }
  4331. return points;
  4332. }
  4333. function d3_layout_bundleAncestors(node) {
  4334. var ancestors = [],
  4335. parent = node.parent;
  4336. while (parent != null) {
  4337. ancestors.push(node);
  4338. node = parent;
  4339. parent = parent.parent;
  4340. }
  4341. ancestors.push(node);
  4342. return ancestors;
  4343. }
  4344. function d3_layout_bundleLeastCommonAncestor(a, b) {
  4345. if (a === b) return a;
  4346. var aNodes = d3_layout_bundleAncestors(a),
  4347. bNodes = d3_layout_bundleAncestors(b),
  4348. aNode = aNodes.pop(),
  4349. bNode = bNodes.pop(),
  4350. sharedNode = null;
  4351. while (aNode === bNode) {
  4352. sharedNode = aNode;
  4353. aNode = aNodes.pop();
  4354. bNode = bNodes.pop();
  4355. }
  4356. return sharedNode;
  4357. }
  4358. d3.layout.chord = function() {
  4359. var chord = {},
  4360. chords,
  4361. groups,
  4362. matrix,
  4363. n,
  4364. padding = 0,
  4365. sortGroups,
  4366. sortSubgroups,
  4367. sortChords;
  4368. function relayout() {
  4369. var subgroups = {},
  4370. groupSums = [],
  4371. groupIndex = d3.range(n),
  4372. subgroupIndex = [],
  4373. k,
  4374. x,
  4375. x0,
  4376. i,
  4377. j;
  4378. chords = [];
  4379. groups = [];
  4380. // Compute the sum.
  4381. k = 0, i = -1; while (++i < n) {
  4382. x = 0, j = -1; while (++j < n) {
  4383. x += matrix[i][j];
  4384. }
  4385. groupSums.push(x);
  4386. subgroupIndex.push(d3.range(n));
  4387. k += x;
  4388. }
  4389. // Sort groups…
  4390. if (sortGroups) {
  4391. groupIndex.sort(function(a, b) {
  4392. return sortGroups(groupSums[a], groupSums[b]);
  4393. });
  4394. }
  4395. // Sort subgroups…
  4396. if (sortSubgroups) {
  4397. subgroupIndex.forEach(function(d, i) {
  4398. d.sort(function(a, b) {
  4399. return sortSubgroups(matrix[i][a], matrix[i][b]);
  4400. });
  4401. });
  4402. }
  4403. // Convert the sum to scaling factor for [0, 2pi].
  4404. // TODO Allow start and end angle to be specified.
  4405. // TODO Allow padding to be specified as percentage?
  4406. k = (2 * Math.PI - padding * n) / k;
  4407. // Compute the start and end angle for each group and subgroup.
  4408. // Note: Opera has a bug reordering object literal properties!
  4409. x = 0, i = -1; while (++i < n) {
  4410. x0 = x, j = -1; while (++j < n) {
  4411. var di = groupIndex[i],
  4412. dj = subgroupIndex[di][j],
  4413. v = matrix[di][dj],
  4414. a0 = x,
  4415. a1 = x += v * k;
  4416. subgroups[di + "-" + dj] = {
  4417. index: di,
  4418. subindex: dj,
  4419. startAngle: a0,
  4420. endAngle: a1,
  4421. value: v
  4422. };
  4423. }
  4424. groups.push({
  4425. index: di,
  4426. startAngle: x0,
  4427. endAngle: x,
  4428. value: (x - x0) / k
  4429. });
  4430. x += padding;
  4431. }
  4432. // Generate chords for each (non-empty) subgroup-subgroup link.
  4433. i = -1; while (++i < n) {
  4434. j = i - 1; while (++j < n) {
  4435. var source = subgroups[i + "-" + j],
  4436. target = subgroups[j + "-" + i];
  4437. if (source.value || target.value) {
  4438. chords.push(source.value < target.value
  4439. ? {source: target, target: source}
  4440. : {source: source, target: target});
  4441. }
  4442. }
  4443. }
  4444. if (sortChords) resort();
  4445. }
  4446. function resort() {
  4447. chords.sort(function(a, b) {
  4448. return sortChords(
  4449. (a.source.value + a.target.value) / 2,
  4450. (b.source.value + b.target.value) / 2);
  4451. });
  4452. }
  4453. chord.matrix = function(x) {
  4454. if (!arguments.length) return matrix;
  4455. n = (matrix = x) && matrix.length;
  4456. chords = groups = null;
  4457. return chord;
  4458. };
  4459. chord.padding = function(x) {
  4460. if (!arguments.length) return padding;
  4461. padding = x;
  4462. chords = groups = null;
  4463. return chord;
  4464. };
  4465. chord.sortGroups = function(x) {
  4466. if (!arguments.length) return sortGroups;
  4467. sortGroups = x;
  4468. chords = groups = null;
  4469. return chord;
  4470. };
  4471. chord.sortSubgroups = function(x) {
  4472. if (!arguments.length) return sortSubgroups;
  4473. sortSubgroups = x;
  4474. chords = null;
  4475. return chord;
  4476. };
  4477. chord.sortChords = function(x) {
  4478. if (!arguments.length) return sortChords;
  4479. sortChords = x;
  4480. if (chords) resort();
  4481. return chord;
  4482. };
  4483. chord.chords = function() {
  4484. if (!chords) relayout();
  4485. return chords;
  4486. };
  4487. chord.groups = function() {
  4488. if (!groups) relayout();
  4489. return groups;
  4490. };
  4491. return chord;
  4492. };
  4493. // A rudimentary force layout using Gauss-Seidel.
  4494. d3.layout.force = function() {
  4495. var force = {},
  4496. event = d3.dispatch("start", "tick", "end"),
  4497. size = [1, 1],
  4498. drag,
  4499. alpha,
  4500. friction = .9,
  4501. linkDistance = d3_layout_forceLinkDistance,
  4502. linkStrength = d3_layout_forceLinkStrength,
  4503. charge = -30,
  4504. gravity = .1,
  4505. theta = .8,
  4506. interval,
  4507. nodes = [],
  4508. links = [],
  4509. distances,
  4510. strengths,
  4511. charges;
  4512. function repulse(node) {
  4513. return function(quad, x1, y1, x2, y2) {
  4514. if (quad.point !== node) {
  4515. var dx = quad.cx - node.x,
  4516. dy = quad.cy - node.y,
  4517. dn = 1 / Math.sqrt(dx * dx + dy * dy);
  4518. /* Barnes-Hut criterion. */
  4519. if ((x2 - x1) * dn < theta) {
  4520. var k = quad.charge * dn * dn;
  4521. node.px -= dx * k;
  4522. node.py -= dy * k;
  4523. return true;
  4524. }
  4525. if (quad.point && isFinite(dn)) {
  4526. var k = quad.pointCharge * dn * dn;
  4527. node.px -= dx * k;
  4528. node.py -= dy * k;
  4529. }
  4530. }
  4531. return !quad.charge;
  4532. };
  4533. }
  4534. force.tick = function() {
  4535. // simulated annealing, basically
  4536. if ((alpha *= .99) < .005) {
  4537. event.end({type: "end", alpha: alpha = 0});
  4538. return true;
  4539. }
  4540. var n = nodes.length,
  4541. m = links.length,
  4542. q,
  4543. i, // current index
  4544. o, // current object
  4545. s, // current source
  4546. t, // current target
  4547. l, // current distance
  4548. k, // current force
  4549. x, // x-distance
  4550. y; // y-distance
  4551. // gauss-seidel relaxation for links
  4552. for (i = 0; i < m; ++i) {
  4553. o = links[i];
  4554. s = o.source;
  4555. t = o.target;
  4556. x = t.x - s.x;
  4557. y = t.y - s.y;
  4558. if (l = (x * x + y * y)) {
  4559. l = alpha * strengths[i] * ((l = Math.sqrt(l)) - distances[i]) / l;
  4560. x *= l;
  4561. y *= l;
  4562. t.x -= x * (k = s.weight / (t.weight + s.weight));
  4563. t.y -= y * k;
  4564. s.x += x * (k = 1 - k);
  4565. s.y += y * k;
  4566. }
  4567. }
  4568. // apply gravity forces
  4569. if (k = alpha * gravity) {
  4570. x = size[0] / 2;
  4571. y = size[1] / 2;
  4572. i = -1; if (k) while (++i < n) {
  4573. o = nodes[i];
  4574. o.x += (x - o.x) * k;
  4575. o.y += (y - o.y) * k;
  4576. }
  4577. }
  4578. // compute quadtree center of mass and apply charge forces
  4579. if (charge) {
  4580. d3_layout_forceAccumulate(q = d3.geom.quadtree(nodes), alpha, charges);
  4581. i = -1; while (++i < n) {
  4582. if (!(o = nodes[i]).fixed) {
  4583. q.visit(repulse(o));
  4584. }
  4585. }
  4586. }
  4587. // position verlet integration
  4588. i = -1; while (++i < n) {
  4589. o = nodes[i];
  4590. if (o.fixed) {
  4591. o.x = o.px;
  4592. o.y = o.py;
  4593. } else {
  4594. o.x -= (o.px - (o.px = o.x)) * friction;
  4595. o.y -= (o.py - (o.py = o.y)) * friction;
  4596. }
  4597. }
  4598. event.tick({type: "tick", alpha: alpha});
  4599. };
  4600. force.nodes = function(x) {
  4601. if (!arguments.length) return nodes;
  4602. nodes = x;
  4603. return force;
  4604. };
  4605. force.links = function(x) {
  4606. if (!arguments.length) return links;
  4607. links = x;
  4608. return force;
  4609. };
  4610. force.size = function(x) {
  4611. if (!arguments.length) return size;
  4612. size = x;
  4613. return force;
  4614. };
  4615. force.linkDistance = function(x) {
  4616. if (!arguments.length) return linkDistance;
  4617. linkDistance = d3.functor(x);
  4618. return force;
  4619. };
  4620. // For backwards-compatibility.
  4621. force.distance = force.linkDistance;
  4622. force.linkStrength = function(x) {
  4623. if (!arguments.length) return linkStrength;
  4624. linkStrength = d3.functor(x);
  4625. return force;
  4626. };
  4627. force.friction = function(x) {
  4628. if (!arguments.length) return friction;
  4629. friction = x;
  4630. return force;
  4631. };
  4632. force.charge = function(x) {
  4633. if (!arguments.length) return charge;
  4634. charge = typeof x === "function" ? x : +x;
  4635. return force;
  4636. };
  4637. force.gravity = function(x) {
  4638. if (!arguments.length) return gravity;
  4639. gravity = x;
  4640. return force;
  4641. };
  4642. force.theta = function(x) {
  4643. if (!arguments.length) return theta;
  4644. theta = x;
  4645. return force;
  4646. };
  4647. force.alpha = function(x) {
  4648. if (!arguments.length) return alpha;
  4649. if (alpha) { // if we're already running
  4650. if (x > 0) alpha = x; // we might keep it hot
  4651. else alpha = 0; // or, next tick will dispatch "end"
  4652. } else if (x > 0) { // otherwise, fire it up!
  4653. event.start({type: "start", alpha: alpha = x});
  4654. d3.timer(force.tick);
  4655. }
  4656. return force;
  4657. };
  4658. force.start = function() {
  4659. var i,
  4660. j,
  4661. n = nodes.length,
  4662. m = links.length,
  4663. w = size[0],
  4664. h = size[1],
  4665. neighbors,
  4666. o;
  4667. for (i = 0; i < n; ++i) {
  4668. (o = nodes[i]).index = i;
  4669. o.weight = 0;
  4670. }
  4671. distances = [];
  4672. strengths = [];
  4673. for (i = 0; i < m; ++i) {
  4674. o = links[i];
  4675. if (typeof o.source == "number") o.source = nodes[o.source];
  4676. if (typeof o.target == "number") o.target = nodes[o.target];
  4677. distances[i] = linkDistance.call(this, o, i);
  4678. strengths[i] = linkStrength.call(this, o, i);
  4679. ++o.source.weight;
  4680. ++o.target.weight;
  4681. }
  4682. for (i = 0; i < n; ++i) {
  4683. o = nodes[i];
  4684. if (isNaN(o.x)) o.x = position("x", w);
  4685. if (isNaN(o.y)) o.y = position("y", h);
  4686. if (isNaN(o.px)) o.px = o.x;
  4687. if (isNaN(o.py)) o.py = o.y;
  4688. }
  4689. charges = [];
  4690. if (typeof charge === "function") {
  4691. for (i = 0; i < n; ++i) {
  4692. charges[i] = +charge.call(this, nodes[i], i);
  4693. }
  4694. } else {
  4695. for (i = 0; i < n; ++i) {
  4696. charges[i] = charge;
  4697. }
  4698. }
  4699. // initialize node position based on first neighbor
  4700. function position(dimension, size) {
  4701. var neighbors = neighbor(i),
  4702. j = -1,
  4703. m = neighbors.length,
  4704. x;
  4705. while (++j < m) if (!isNaN(x = neighbors[j][dimension])) return x;
  4706. return Math.random() * size;
  4707. }
  4708. // initialize neighbors lazily
  4709. function neighbor() {
  4710. if (!neighbors) {
  4711. neighbors = [];
  4712. for (j = 0; j < n; ++j) {
  4713. neighbors[j] = [];
  4714. }
  4715. for (j = 0; j < m; ++j) {
  4716. var o = links[j];
  4717. neighbors[o.source.index].push(o.target);
  4718. neighbors[o.target.index].push(o.source);
  4719. }
  4720. }
  4721. return neighbors[i];
  4722. }
  4723. return force.resume();
  4724. };
  4725. force.resume = function() {
  4726. return force.alpha(.1);
  4727. };
  4728. force.stop = function() {
  4729. return force.alpha(0);
  4730. };
  4731. // use `node.call(force.drag)` to make nodes draggable
  4732. force.drag = function() {
  4733. if (!drag) drag = d3.behavior.drag()
  4734. .origin(Object)
  4735. .on("dragstart", dragstart)
  4736. .on("drag", d3_layout_forceDrag)
  4737. .on("dragend", d3_layout_forceDragEnd);
  4738. this.on("mouseover.force", d3_layout_forceDragOver)
  4739. .on("mouseout.force", d3_layout_forceDragOut)
  4740. .call(drag);
  4741. };
  4742. function dragstart(d) {
  4743. d3_layout_forceDragOver(d3_layout_forceDragNode = d);
  4744. d3_layout_forceDragForce = force;
  4745. }
  4746. return d3.rebind(force, event, "on");
  4747. };
  4748. var d3_layout_forceDragForce,
  4749. d3_layout_forceDragNode;
  4750. function d3_layout_forceDragOver(d) {
  4751. d.fixed |= 2;
  4752. }
  4753. function d3_layout_forceDragOut(d) {
  4754. if (d !== d3_layout_forceDragNode) d.fixed &= 1;
  4755. }
  4756. function d3_layout_forceDragEnd() {
  4757. d3_layout_forceDragNode.fixed &= 1;
  4758. d3_layout_forceDragForce = d3_layout_forceDragNode = null;
  4759. }
  4760. function d3_layout_forceDrag() {
  4761. d3_layout_forceDragNode.px = d3.event.x;
  4762. d3_layout_forceDragNode.py = d3.event.y;
  4763. d3_layout_forceDragForce.resume(); // restart annealing
  4764. }
  4765. function d3_layout_forceAccumulate(quad, alpha, charges) {
  4766. var cx = 0,
  4767. cy = 0;
  4768. quad.charge = 0;
  4769. if (!quad.leaf) {
  4770. var nodes = quad.nodes,
  4771. n = nodes.length,
  4772. i = -1,
  4773. c;
  4774. while (++i < n) {
  4775. c = nodes[i];
  4776. if (c == null) continue;
  4777. d3_layout_forceAccumulate(c, alpha, charges);
  4778. quad.charge += c.charge;
  4779. cx += c.charge * c.cx;
  4780. cy += c.charge * c.cy;
  4781. }
  4782. }
  4783. if (quad.point) {
  4784. // jitter internal nodes that are coincident
  4785. if (!quad.leaf) {
  4786. quad.point.x += Math.random() - .5;
  4787. quad.point.y += Math.random() - .5;
  4788. }
  4789. var k = alpha * charges[quad.point.index];
  4790. quad.charge += quad.pointCharge = k;
  4791. cx += k * quad.point.x;
  4792. cy += k * quad.point.y;
  4793. }
  4794. quad.cx = cx / quad.charge;
  4795. quad.cy = cy / quad.charge;
  4796. }
  4797. function d3_layout_forceLinkDistance(link) {
  4798. return 20;
  4799. }
  4800. function d3_layout_forceLinkStrength(link) {
  4801. return 1;
  4802. }
  4803. d3.layout.partition = function() {
  4804. var hierarchy = d3.layout.hierarchy(),
  4805. size = [1, 1]; // width, height
  4806. function position(node, x, dx, dy) {
  4807. var children = node.children;
  4808. node.x = x;
  4809. node.y = node.depth * dy;
  4810. node.dx = dx;
  4811. node.dy = dy;
  4812. if (children && (n = children.length)) {
  4813. var i = -1,
  4814. n,
  4815. c,
  4816. d;
  4817. dx = node.value ? dx / node.value : 0;
  4818. while (++i < n) {
  4819. position(c = children[i], x, d = c.value * dx, dy);
  4820. x += d;
  4821. }
  4822. }
  4823. }
  4824. function depth(node) {
  4825. var children = node.children,
  4826. d = 0;
  4827. if (children && (n = children.length)) {
  4828. var i = -1,
  4829. n;
  4830. while (++i < n) d = Math.max(d, depth(children[i]));
  4831. }
  4832. return 1 + d;
  4833. }
  4834. function partition(d, i) {
  4835. var nodes = hierarchy.call(this, d, i);
  4836. position(nodes[0], 0, size[0], size[1] / depth(nodes[0]));
  4837. return nodes;
  4838. }
  4839. partition.size = function(x) {
  4840. if (!arguments.length) return size;
  4841. size = x;
  4842. return partition;
  4843. };
  4844. return d3_layout_hierarchyRebind(partition, hierarchy);
  4845. };
  4846. d3.layout.pie = function() {
  4847. var value = Number,
  4848. sort = d3_layout_pieSortByValue,
  4849. startAngle = 0,
  4850. endAngle = 2 * Math.PI;
  4851. function pie(data, i) {
  4852. // Compute the numeric values for each data element.
  4853. var values = data.map(function(d, i) { return +value.call(pie, d, i); });
  4854. // Compute the start angle.
  4855. var a = +(typeof startAngle === "function"
  4856. ? startAngle.apply(this, arguments)
  4857. : startAngle);
  4858. // Compute the angular scale factor: from value to radians.
  4859. var k = ((typeof endAngle === "function"
  4860. ? endAngle.apply(this, arguments)
  4861. : endAngle) - startAngle)
  4862. / d3.sum(values);
  4863. // Optionally sort the data.
  4864. var index = d3.range(data.length);
  4865. if (sort != null) index.sort(sort === d3_layout_pieSortByValue
  4866. ? function(i, j) { return values[j] - values[i]; }
  4867. : function(i, j) { return sort(data[i], data[j]); });
  4868. // Compute the arcs!
  4869. // They are stored in the original data's order.
  4870. var arcs = [];
  4871. index.forEach(function(i) {
  4872. arcs[i] = {
  4873. data: data[i],
  4874. value: d = values[i],
  4875. startAngle: a,
  4876. endAngle: a += d * k
  4877. };
  4878. });
  4879. return arcs;
  4880. }
  4881. /**
  4882. * Specifies the value function *x*, which returns a nonnegative numeric value
  4883. * for each datum. The default value function is `Number`. The value function
  4884. * is passed two arguments: the current datum and the current index.
  4885. */
  4886. pie.value = function(x) {
  4887. if (!arguments.length) return value;
  4888. value = x;
  4889. return pie;
  4890. };
  4891. /**
  4892. * Specifies a sort comparison operator *x*. The comparator is passed two data
  4893. * elements from the data array, a and b; it returns a negative value if a is
  4894. * less than b, a positive value if a is greater than b, and zero if a equals
  4895. * b.
  4896. */
  4897. pie.sort = function(x) {
  4898. if (!arguments.length) return sort;
  4899. sort = x;
  4900. return pie;
  4901. };
  4902. /**
  4903. * Specifies the overall start angle of the pie chart. Defaults to 0. The
  4904. * start angle can be specified either as a constant or as a function; in the
  4905. * case of a function, it is evaluated once per array (as opposed to per
  4906. * element).
  4907. */
  4908. pie.startAngle = function(x) {
  4909. if (!arguments.length) return startAngle;
  4910. startAngle = x;
  4911. return pie;
  4912. };
  4913. /**
  4914. * Specifies the overall end angle of the pie chart. Defaults to 2π. The
  4915. * end angle can be specified either as a constant or as a function; in the
  4916. * case of a function, it is evaluated once per array (as opposed to per
  4917. * element).
  4918. */
  4919. pie.endAngle = function(x) {
  4920. if (!arguments.length) return endAngle;
  4921. endAngle = x;
  4922. return pie;
  4923. };
  4924. return pie;
  4925. };
  4926. var d3_layout_pieSortByValue = {};
  4927. // data is two-dimensional array of x,y; we populate y0
  4928. d3.layout.stack = function() {
  4929. var values = Object,
  4930. order = d3_layout_stackOrderDefault,
  4931. offset = d3_layout_stackOffsetZero,
  4932. out = d3_layout_stackOut,
  4933. x = d3_layout_stackX,
  4934. y = d3_layout_stackY;
  4935. function stack(data, index) {
  4936. // Convert series to canonical two-dimensional representation.
  4937. var series = data.map(function(d, i) {
  4938. return values.call(stack, d, i);
  4939. });
  4940. // Convert each series to canonical [[x,y]] representation.
  4941. var points = series.map(function(d, i) {
  4942. return d.map(function(v, i) {
  4943. return [x.call(stack, v, i), y.call(stack, v, i)];
  4944. });
  4945. });
  4946. // Compute the order of series, and permute them.
  4947. var orders = order.call(stack, points, index);
  4948. series = d3.permute(series, orders);
  4949. points = d3.permute(points, orders);
  4950. // Compute the baseline…
  4951. var offsets = offset.call(stack, points, index);
  4952. // And propagate it to other series.
  4953. var n = series.length,
  4954. m = series[0].length,
  4955. i,
  4956. j,
  4957. o;
  4958. for (j = 0; j < m; ++j) {
  4959. out.call(stack, series[0][j], o = offsets[j], points[0][j][1]);
  4960. for (i = 1; i < n; ++i) {
  4961. out.call(stack, series[i][j], o += points[i - 1][j][1], points[i][j][1]);
  4962. }
  4963. }
  4964. return data;
  4965. }
  4966. stack.values = function(x) {
  4967. if (!arguments.length) return values;
  4968. values = x;
  4969. return stack;
  4970. };
  4971. stack.order = function(x) {
  4972. if (!arguments.length) return order;
  4973. order = typeof x === "function" ? x : d3_layout_stackOrders.get(x) || d3_layout_stackOrderDefault;
  4974. return stack;
  4975. };
  4976. stack.offset = function(x) {
  4977. if (!arguments.length) return offset;
  4978. offset = typeof x === "function" ? x : d3_layout_stackOffsets.get(x) || d3_layout_stackOffsetZero;
  4979. return stack;
  4980. };
  4981. stack.x = function(z) {
  4982. if (!arguments.length) return x;
  4983. x = z;
  4984. return stack;
  4985. };
  4986. stack.y = function(z) {
  4987. if (!arguments.length) return y;
  4988. y = z;
  4989. return stack;
  4990. };
  4991. stack.out = function(z) {
  4992. if (!arguments.length) return out;
  4993. out = z;
  4994. return stack;
  4995. };
  4996. return stack;
  4997. }
  4998. function d3_layout_stackX(d) {
  4999. return d.x;
  5000. }
  5001. function d3_layout_stackY(d) {
  5002. return d.y;
  5003. }
  5004. function d3_layout_stackOut(d, y0, y) {
  5005. d.y0 = y0;
  5006. d.y = y;
  5007. }
  5008. var d3_layout_stackOrders = d3.map({
  5009. "inside-out": function(data) {
  5010. var n = data.length,
  5011. i,
  5012. j,
  5013. max = data.map(d3_layout_stackMaxIndex),
  5014. sums = data.map(d3_layout_stackReduceSum),
  5015. index = d3.range(n).sort(function(a, b) { return max[a] - max[b]; }),
  5016. top = 0,
  5017. bottom = 0,
  5018. tops = [],
  5019. bottoms = [];
  5020. for (i = 0; i < n; ++i) {
  5021. j = index[i];
  5022. if (top < bottom) {
  5023. top += sums[j];
  5024. tops.push(j);
  5025. } else {
  5026. bottom += sums[j];
  5027. bottoms.push(j);
  5028. }
  5029. }
  5030. return bottoms.reverse().concat(tops);
  5031. },
  5032. "reverse": function(data) {
  5033. return d3.range(data.length).reverse();
  5034. },
  5035. "default": d3_layout_stackOrderDefault
  5036. });
  5037. var d3_layout_stackOffsets = d3.map({
  5038. "silhouette": function(data) {
  5039. var n = data.length,
  5040. m = data[0].length,
  5041. sums = [],
  5042. max = 0,
  5043. i,
  5044. j,
  5045. o,
  5046. y0 = [];
  5047. for (j = 0; j < m; ++j) {
  5048. for (i = 0, o = 0; i < n; i++) o += data[i][j][1];
  5049. if (o > max) max = o;
  5050. sums.push(o);
  5051. }
  5052. for (j = 0; j < m; ++j) {
  5053. y0[j] = (max - sums[j]) / 2;
  5054. }
  5055. return y0;
  5056. },
  5057. "wiggle": function(data) {
  5058. var n = data.length,
  5059. x = data[0],
  5060. m = x.length,
  5061. max = 0,
  5062. i,
  5063. j,
  5064. k,
  5065. s1,
  5066. s2,
  5067. s3,
  5068. dx,
  5069. o,
  5070. o0,
  5071. y0 = [];
  5072. y0[0] = o = o0 = 0;
  5073. for (j = 1; j < m; ++j) {
  5074. for (i = 0, s1 = 0; i < n; ++i) s1 += data[i][j][1];
  5075. for (i = 0, s2 = 0, dx = x[j][0] - x[j - 1][0]; i < n; ++i) {
  5076. for (k = 0, s3 = (data[i][j][1] - data[i][j - 1][1]) / (2 * dx); k < i; ++k) {
  5077. s3 += (data[k][j][1] - data[k][j - 1][1]) / dx;
  5078. }
  5079. s2 += s3 * data[i][j][1];
  5080. }
  5081. y0[j] = o -= s1 ? s2 / s1 * dx : 0;
  5082. if (o < o0) o0 = o;
  5083. }
  5084. for (j = 0; j < m; ++j) y0[j] -= o0;
  5085. return y0;
  5086. },
  5087. "expand": function(data) {
  5088. var n = data.length,
  5089. m = data[0].length,
  5090. k = 1 / n,
  5091. i,
  5092. j,
  5093. o,
  5094. y0 = [];
  5095. for (j = 0; j < m; ++j) {
  5096. for (i = 0, o = 0; i < n; i++) o += data[i][j][1];
  5097. if (o) for (i = 0; i < n; i++) data[i][j][1] /= o;
  5098. else for (i = 0; i < n; i++) data[i][j][1] = k;
  5099. }
  5100. for (j = 0; j < m; ++j) y0[j] = 0;
  5101. return y0;
  5102. },
  5103. "zero": d3_layout_stackOffsetZero
  5104. });
  5105. function d3_layout_stackOrderDefault(data) {
  5106. return d3.range(data.length);
  5107. }
  5108. function d3_layout_stackOffsetZero(data) {
  5109. var j = -1,
  5110. m = data[0].length,
  5111. y0 = [];
  5112. while (++j < m) y0[j] = 0;
  5113. return y0;
  5114. }
  5115. function d3_layout_stackMaxIndex(array) {
  5116. var i = 1,
  5117. j = 0,
  5118. v = array[0][1],
  5119. k,
  5120. n = array.length;
  5121. for (; i < n; ++i) {
  5122. if ((k = array[i][1]) > v) {
  5123. j = i;
  5124. v = k;
  5125. }
  5126. }
  5127. return j;
  5128. }
  5129. function d3_layout_stackReduceSum(d) {
  5130. return d.reduce(d3_layout_stackSum, 0);
  5131. }
  5132. function d3_layout_stackSum(p, d) {
  5133. return p + d[1];
  5134. }
  5135. d3.layout.histogram = function() {
  5136. var frequency = true,
  5137. valuer = Number,
  5138. ranger = d3_layout_histogramRange,
  5139. binner = d3_layout_histogramBinSturges;
  5140. function histogram(data, i) {
  5141. var bins = [],
  5142. values = data.map(valuer, this),
  5143. range = ranger.call(this, values, i),
  5144. thresholds = binner.call(this, range, values, i),
  5145. bin,
  5146. i = -1,
  5147. n = values.length,
  5148. m = thresholds.length - 1,
  5149. k = frequency ? 1 : 1 / n,
  5150. x;
  5151. // Initialize the bins.
  5152. while (++i < m) {
  5153. bin = bins[i] = [];
  5154. bin.dx = thresholds[i + 1] - (bin.x = thresholds[i]);
  5155. bin.y = 0;
  5156. }
  5157. // Fill the bins, ignoring values outside the range.
  5158. i = -1; while(++i < n) {
  5159. x = values[i];
  5160. if ((x >= range[0]) && (x <= range[1])) {
  5161. bin = bins[d3.bisect(thresholds, x, 1, m) - 1];
  5162. bin.y += k;
  5163. bin.push(data[i]);
  5164. }
  5165. }
  5166. return bins;
  5167. }
  5168. // Specifies how to extract a value from the associated data. The default
  5169. // value function is `Number`, which is equivalent to the identity function.
  5170. histogram.value = function(x) {
  5171. if (!arguments.length) return valuer;
  5172. valuer = x;
  5173. return histogram;
  5174. };
  5175. // Specifies the range of the histogram. Values outside the specified range
  5176. // will be ignored. The argument `x` may be specified either as a two-element
  5177. // array representing the minimum and maximum value of the range, or as a
  5178. // function that returns the range given the array of values and the current
  5179. // index `i`. The default range is the extent (minimum and maximum) of the
  5180. // values.
  5181. histogram.range = function(x) {
  5182. if (!arguments.length) return ranger;
  5183. ranger = d3.functor(x);
  5184. return histogram;
  5185. };
  5186. // Specifies how to bin values in the histogram. The argument `x` may be
  5187. // specified as a number, in which case the range of values will be split
  5188. // uniformly into the given number of bins. Or, `x` may be an array of
  5189. // threshold values, defining the bins; the specified array must contain the
  5190. // rightmost (upper) value, thus specifying n + 1 values for n bins. Or, `x`
  5191. // may be a function which is evaluated, being passed the range, the array of
  5192. // values, and the current index `i`, returning an array of thresholds. The
  5193. // default bin function will divide the values into uniform bins using
  5194. // Sturges' formula.
  5195. histogram.bins = function(x) {
  5196. if (!arguments.length) return binner;
  5197. binner = typeof x === "number"
  5198. ? function(range) { return d3_layout_histogramBinFixed(range, x); }
  5199. : d3.functor(x);
  5200. return histogram;
  5201. };
  5202. // Specifies whether the histogram's `y` value is a count (frequency) or a
  5203. // probability (density). The default value is true.
  5204. histogram.frequency = function(x) {
  5205. if (!arguments.length) return frequency;
  5206. frequency = !!x;
  5207. return histogram;
  5208. };
  5209. return histogram;
  5210. };
  5211. function d3_layout_histogramBinSturges(range, values) {
  5212. return d3_layout_histogramBinFixed(range, Math.ceil(Math.log(values.length) / Math.LN2 + 1));
  5213. }
  5214. function d3_layout_histogramBinFixed(range, n) {
  5215. var x = -1,
  5216. b = +range[0],
  5217. m = (range[1] - b) / n,
  5218. f = [];
  5219. while (++x <= n) f[x] = m * x + b;
  5220. return f;
  5221. }
  5222. function d3_layout_histogramRange(values) {
  5223. return [d3.min(values), d3.max(values)];
  5224. }
  5225. d3.layout.hierarchy = function() {
  5226. var sort = d3_layout_hierarchySort,
  5227. children = d3_layout_hierarchyChildren,
  5228. value = d3_layout_hierarchyValue;
  5229. // Recursively compute the node depth and value.
  5230. // Also converts the data representation into a standard hierarchy structure.
  5231. function recurse(data, depth, nodes) {
  5232. var childs = children.call(hierarchy, data, depth),
  5233. node = d3_layout_hierarchyInline ? data : {data: data};
  5234. node.depth = depth;
  5235. nodes.push(node);
  5236. if (childs && (n = childs.length)) {
  5237. var i = -1,
  5238. n,
  5239. c = node.children = [],
  5240. v = 0,
  5241. j = depth + 1;
  5242. while (++i < n) {
  5243. d = recurse(childs[i], j, nodes);
  5244. d.parent = node;
  5245. c.push(d);
  5246. v += d.value;
  5247. }
  5248. if (sort) c.sort(sort);
  5249. if (value) node.value = v;
  5250. } else if (value) {
  5251. node.value = +value.call(hierarchy, data, depth) || 0;
  5252. }
  5253. return node;
  5254. }
  5255. // Recursively re-evaluates the node value.
  5256. function revalue(node, depth) {
  5257. var children = node.children,
  5258. v = 0;
  5259. if (children && (n = children.length)) {
  5260. var i = -1,
  5261. n,
  5262. j = depth + 1;
  5263. while (++i < n) v += revalue(children[i], j);
  5264. } else if (value) {
  5265. v = +value.call(hierarchy, d3_layout_hierarchyInline ? node : node.data, depth) || 0;
  5266. }
  5267. if (value) node.value = v;
  5268. return v;
  5269. }
  5270. function hierarchy(d) {
  5271. var nodes = [];
  5272. recurse(d, 0, nodes);
  5273. return nodes;
  5274. }
  5275. hierarchy.sort = function(x) {
  5276. if (!arguments.length) return sort;
  5277. sort = x;
  5278. return hierarchy;
  5279. };
  5280. hierarchy.children = function(x) {
  5281. if (!arguments.length) return children;
  5282. children = x;
  5283. return hierarchy;
  5284. };
  5285. hierarchy.value = function(x) {
  5286. if (!arguments.length) return value;
  5287. value = x;
  5288. return hierarchy;
  5289. };
  5290. // Re-evaluates the `value` property for the specified hierarchy.
  5291. hierarchy.revalue = function(root) {
  5292. revalue(root, 0);
  5293. return root;
  5294. };
  5295. return hierarchy;
  5296. };
  5297. // A method assignment helper for hierarchy subclasses.
  5298. function d3_layout_hierarchyRebind(object, hierarchy) {
  5299. d3.rebind(object, hierarchy, "sort", "children", "value");
  5300. // Add an alias for links, for convenience.
  5301. object.links = d3_layout_hierarchyLinks;
  5302. // If the new API is used, enabling inlining.
  5303. object.nodes = function(d) {
  5304. d3_layout_hierarchyInline = true;
  5305. return (object.nodes = object)(d);
  5306. };
  5307. return object;
  5308. }
  5309. function d3_layout_hierarchyChildren(d) {
  5310. return d.children;
  5311. }
  5312. function d3_layout_hierarchyValue(d) {
  5313. return d.value;
  5314. }
  5315. function d3_layout_hierarchySort(a, b) {
  5316. return b.value - a.value;
  5317. }
  5318. // Returns an array source+target objects for the specified nodes.
  5319. function d3_layout_hierarchyLinks(nodes) {
  5320. return d3.merge(nodes.map(function(parent) {
  5321. return (parent.children || []).map(function(child) {
  5322. return {source: parent, target: child};
  5323. });
  5324. }));
  5325. }
  5326. // For backwards-compatibility, don't enable inlining by default.
  5327. var d3_layout_hierarchyInline = false;
  5328. d3.layout.pack = function() {
  5329. var hierarchy = d3.layout.hierarchy().sort(d3_layout_packSort),
  5330. size = [1, 1];
  5331. function pack(d, i) {
  5332. var nodes = hierarchy.call(this, d, i),
  5333. root = nodes[0];
  5334. // Recursively compute the layout.
  5335. root.x = 0;
  5336. root.y = 0;
  5337. d3_layout_packTree(root);
  5338. // Scale the layout to fit the requested size.
  5339. var w = size[0],
  5340. h = size[1],
  5341. k = 1 / Math.max(2 * root.r / w, 2 * root.r / h);
  5342. d3_layout_packTransform(root, w / 2, h / 2, k);
  5343. return nodes;
  5344. }
  5345. pack.size = function(x) {
  5346. if (!arguments.length) return size;
  5347. size = x;
  5348. return pack;
  5349. };
  5350. return d3_layout_hierarchyRebind(pack, hierarchy);
  5351. };
  5352. function d3_layout_packSort(a, b) {
  5353. return a.value - b.value;
  5354. }
  5355. function d3_layout_packInsert(a, b) {
  5356. var c = a._pack_next;
  5357. a._pack_next = b;
  5358. b._pack_prev = a;
  5359. b._pack_next = c;
  5360. c._pack_prev = b;
  5361. }
  5362. function d3_layout_packSplice(a, b) {
  5363. a._pack_next = b;
  5364. b._pack_prev = a;
  5365. }
  5366. function d3_layout_packIntersects(a, b) {
  5367. var dx = b.x - a.x,
  5368. dy = b.y - a.y,
  5369. dr = a.r + b.r;
  5370. return dr * dr - dx * dx - dy * dy > .001; // within epsilon
  5371. }
  5372. function d3_layout_packCircle(nodes) {
  5373. var xMin = Infinity,
  5374. xMax = -Infinity,
  5375. yMin = Infinity,
  5376. yMax = -Infinity,
  5377. n = nodes.length,
  5378. a, b, c, j, k;
  5379. function bound(node) {
  5380. xMin = Math.min(node.x - node.r, xMin);
  5381. xMax = Math.max(node.x + node.r, xMax);
  5382. yMin = Math.min(node.y - node.r, yMin);
  5383. yMax = Math.max(node.y + node.r, yMax);
  5384. }
  5385. // Create node links.
  5386. nodes.forEach(d3_layout_packLink);
  5387. // Create first node.
  5388. a = nodes[0];
  5389. a.x = -a.r;
  5390. a.y = 0;
  5391. bound(a);
  5392. // Create second node.
  5393. if (n > 1) {
  5394. b = nodes[1];
  5395. b.x = b.r;
  5396. b.y = 0;
  5397. bound(b);
  5398. // Create third node and build chain.
  5399. if (n > 2) {
  5400. c = nodes[2];
  5401. d3_layout_packPlace(a, b, c);
  5402. bound(c);
  5403. d3_layout_packInsert(a, c);
  5404. a._pack_prev = c;
  5405. d3_layout_packInsert(c, b);
  5406. b = a._pack_next;
  5407. // Now iterate through the rest.
  5408. for (var i = 3; i < n; i++) {
  5409. d3_layout_packPlace(a, b, c = nodes[i]);
  5410. // Search for the closest intersection.
  5411. var isect = 0, s1 = 1, s2 = 1;
  5412. for (j = b._pack_next; j !== b; j = j._pack_next, s1++) {
  5413. if (d3_layout_packIntersects(j, c)) {
  5414. isect = 1;
  5415. break;
  5416. }
  5417. }
  5418. if (isect == 1) {
  5419. for (k = a._pack_prev; k !== j._pack_prev; k = k._pack_prev, s2++) {
  5420. if (d3_layout_packIntersects(k, c)) {
  5421. break;
  5422. }
  5423. }
  5424. }
  5425. // Update node chain.
  5426. if (isect) {
  5427. if (s1 < s2 || (s1 == s2 && b.r < a.r)) d3_layout_packSplice(a, b = j);
  5428. else d3_layout_packSplice(a = k, b);
  5429. i--;
  5430. } else {
  5431. d3_layout_packInsert(a, c);
  5432. b = c;
  5433. bound(c);
  5434. }
  5435. }
  5436. }
  5437. }
  5438. // Re-center the circles and return the encompassing radius.
  5439. var cx = (xMin + xMax) / 2,
  5440. cy = (yMin + yMax) / 2,
  5441. cr = 0;
  5442. for (var i = 0; i < n; i++) {
  5443. var node = nodes[i];
  5444. node.x -= cx;
  5445. node.y -= cy;
  5446. cr = Math.max(cr, node.r + Math.sqrt(node.x * node.x + node.y * node.y));
  5447. }
  5448. // Remove node links.
  5449. nodes.forEach(d3_layout_packUnlink);
  5450. return cr;
  5451. }
  5452. function d3_layout_packLink(node) {
  5453. node._pack_next = node._pack_prev = node;
  5454. }
  5455. function d3_layout_packUnlink(node) {
  5456. delete node._pack_next;
  5457. delete node._pack_prev;
  5458. }
  5459. function d3_layout_packTree(node) {
  5460. var children = node.children;
  5461. if (children && children.length) {
  5462. children.forEach(d3_layout_packTree);
  5463. node.r = d3_layout_packCircle(children);
  5464. } else {
  5465. node.r = Math.sqrt(node.value);
  5466. }
  5467. }
  5468. function d3_layout_packTransform(node, x, y, k) {
  5469. var children = node.children;
  5470. node.x = (x += k * node.x);
  5471. node.y = (y += k * node.y);
  5472. node.r *= k;
  5473. if (children) {
  5474. var i = -1, n = children.length;
  5475. while (++i < n) d3_layout_packTransform(children[i], x, y, k);
  5476. }
  5477. }
  5478. function d3_layout_packPlace(a, b, c) {
  5479. var db = a.r + c.r,
  5480. dx = b.x - a.x,
  5481. dy = b.y - a.y;
  5482. if (db && (dx || dy)) {
  5483. var da = b.r + c.r,
  5484. dc = Math.sqrt(dx * dx + dy * dy),
  5485. cos = Math.max(-1, Math.min(1, (db * db + dc * dc - da * da) / (2 * db * dc))),
  5486. theta = Math.acos(cos),
  5487. x = cos * (db /= dc),
  5488. y = Math.sin(theta) * db;
  5489. c.x = a.x + x * dx + y * dy;
  5490. c.y = a.y + x * dy - y * dx;
  5491. } else {
  5492. c.x = a.x + db;
  5493. c.y = a.y;
  5494. }
  5495. }
  5496. // Implements a hierarchical layout using the cluster (or dendrogram)
  5497. // algorithm.
  5498. d3.layout.cluster = function() {
  5499. var hierarchy = d3.layout.hierarchy().sort(null).value(null),
  5500. separation = d3_layout_treeSeparation,
  5501. size = [1, 1]; // width, height
  5502. function cluster(d, i) {
  5503. var nodes = hierarchy.call(this, d, i),
  5504. root = nodes[0],
  5505. previousNode,
  5506. x = 0,
  5507. kx,
  5508. ky;
  5509. // First walk, computing the initial x & y values.
  5510. d3_layout_treeVisitAfter(root, function(node) {
  5511. var children = node.children;
  5512. if (children && children.length) {
  5513. node.x = d3_layout_clusterX(children);
  5514. node.y = d3_layout_clusterY(children);
  5515. } else {
  5516. node.x = previousNode ? x += separation(node, previousNode) : 0;
  5517. node.y = 0;
  5518. previousNode = node;
  5519. }
  5520. });
  5521. // Compute the left-most, right-most, and depth-most nodes for extents.
  5522. var left = d3_layout_clusterLeft(root),
  5523. right = d3_layout_clusterRight(root),
  5524. x0 = left.x - separation(left, right) / 2,
  5525. x1 = right.x + separation(right, left) / 2;
  5526. // Second walk, normalizing x & y to the desired size.
  5527. d3_layout_treeVisitAfter(root, function(node) {
  5528. node.x = (node.x - x0) / (x1 - x0) * size[0];
  5529. node.y = (1 - (root.y ? node.y / root.y : 1)) * size[1];
  5530. });
  5531. return nodes;
  5532. }
  5533. cluster.separation = function(x) {
  5534. if (!arguments.length) return separation;
  5535. separation = x;
  5536. return cluster;
  5537. };
  5538. cluster.size = function(x) {
  5539. if (!arguments.length) return size;
  5540. size = x;
  5541. return cluster;
  5542. };
  5543. return d3_layout_hierarchyRebind(cluster, hierarchy);
  5544. };
  5545. function d3_layout_clusterY(children) {
  5546. return 1 + d3.max(children, function(child) {
  5547. return child.y;
  5548. });
  5549. }
  5550. function d3_layout_clusterX(children) {
  5551. return children.reduce(function(x, child) {
  5552. return x + child.x;
  5553. }, 0) / children.length;
  5554. }
  5555. function d3_layout_clusterLeft(node) {
  5556. var children = node.children;
  5557. return children && children.length ? d3_layout_clusterLeft(children[0]) : node;
  5558. }
  5559. function d3_layout_clusterRight(node) {
  5560. var children = node.children, n;
  5561. return children && (n = children.length) ? d3_layout_clusterRight(children[n - 1]) : node;
  5562. }
  5563. // Node-link tree diagram using the Reingold-Tilford "tidy" algorithm
  5564. d3.layout.tree = function() {
  5565. var hierarchy = d3.layout.hierarchy().sort(null).value(null),
  5566. separation = d3_layout_treeSeparation,
  5567. size = [1, 1]; // width, height
  5568. function tree(d, i) {
  5569. var nodes = hierarchy.call(this, d, i),
  5570. root = nodes[0];
  5571. function firstWalk(node, previousSibling) {
  5572. var children = node.children,
  5573. layout = node._tree;
  5574. if (children && (n = children.length)) {
  5575. var n,
  5576. firstChild = children[0],
  5577. previousChild,
  5578. ancestor = firstChild,
  5579. child,
  5580. i = -1;
  5581. while (++i < n) {
  5582. child = children[i];
  5583. firstWalk(child, previousChild);
  5584. ancestor = apportion(child, previousChild, ancestor);
  5585. previousChild = child;
  5586. }
  5587. d3_layout_treeShift(node);
  5588. var midpoint = .5 * (firstChild._tree.prelim + child._tree.prelim);
  5589. if (previousSibling) {
  5590. layout.prelim = previousSibling._tree.prelim + separation(node, previousSibling);
  5591. layout.mod = layout.prelim - midpoint;
  5592. } else {
  5593. layout.prelim = midpoint;
  5594. }
  5595. } else {
  5596. if (previousSibling) {
  5597. layout.prelim = previousSibling._tree.prelim + separation(node, previousSibling);
  5598. }
  5599. }
  5600. }
  5601. function secondWalk(node, x) {
  5602. node.x = node._tree.prelim + x;
  5603. var children = node.children;
  5604. if (children && (n = children.length)) {
  5605. var i = -1,
  5606. n;
  5607. x += node._tree.mod;
  5608. while (++i < n) {
  5609. secondWalk(children[i], x);
  5610. }
  5611. }
  5612. }
  5613. function apportion(node, previousSibling, ancestor) {
  5614. if (previousSibling) {
  5615. var vip = node,
  5616. vop = node,
  5617. vim = previousSibling,
  5618. vom = node.parent.children[0],
  5619. sip = vip._tree.mod,
  5620. sop = vop._tree.mod,
  5621. sim = vim._tree.mod,
  5622. som = vom._tree.mod,
  5623. shift;
  5624. while (vim = d3_layout_treeRight(vim), vip = d3_layout_treeLeft(vip), vim && vip) {
  5625. vom = d3_layout_treeLeft(vom);
  5626. vop = d3_layout_treeRight(vop);
  5627. vop._tree.ancestor = node;
  5628. shift = vim._tree.prelim + sim - vip._tree.prelim - sip + separation(vim, vip);
  5629. if (shift > 0) {
  5630. d3_layout_treeMove(d3_layout_treeAncestor(vim, node, ancestor), node, shift);
  5631. sip += shift;
  5632. sop += shift;
  5633. }
  5634. sim += vim._tree.mod;
  5635. sip += vip._tree.mod;
  5636. som += vom._tree.mod;
  5637. sop += vop._tree.mod;
  5638. }
  5639. if (vim && !d3_layout_treeRight(vop)) {
  5640. vop._tree.thread = vim;
  5641. vop._tree.mod += sim - sop;
  5642. }
  5643. if (vip && !d3_layout_treeLeft(vom)) {
  5644. vom._tree.thread = vip;
  5645. vom._tree.mod += sip - som;
  5646. ancestor = node;
  5647. }
  5648. }
  5649. return ancestor;
  5650. }
  5651. // Initialize temporary layout variables.
  5652. d3_layout_treeVisitAfter(root, function(node, previousSibling) {
  5653. node._tree = {
  5654. ancestor: node,
  5655. prelim: 0,
  5656. mod: 0,
  5657. change: 0,
  5658. shift: 0,
  5659. number: previousSibling ? previousSibling._tree.number + 1 : 0
  5660. };
  5661. });
  5662. // Compute the layout using Buchheim et al.'s algorithm.
  5663. firstWalk(root);
  5664. secondWalk(root, -root._tree.prelim);
  5665. // Compute the left-most, right-most, and depth-most nodes for extents.
  5666. var left = d3_layout_treeSearch(root, d3_layout_treeLeftmost),
  5667. right = d3_layout_treeSearch(root, d3_layout_treeRightmost),
  5668. deep = d3_layout_treeSearch(root, d3_layout_treeDeepest),
  5669. x0 = left.x - separation(left, right) / 2,
  5670. x1 = right.x + separation(right, left) / 2,
  5671. y1 = deep.depth || 1;
  5672. // Clear temporary layout variables; transform x and y.
  5673. d3_layout_treeVisitAfter(root, function(node) {
  5674. node.x = (node.x - x0) / (x1 - x0) * size[0];
  5675. node.y = node.depth / y1 * size[1];
  5676. delete node._tree;
  5677. });
  5678. return nodes;
  5679. }
  5680. tree.separation = function(x) {
  5681. if (!arguments.length) return separation;
  5682. separation = x;
  5683. return tree;
  5684. };
  5685. tree.size = function(x) {
  5686. if (!arguments.length) return size;
  5687. size = x;
  5688. return tree;
  5689. };
  5690. return d3_layout_hierarchyRebind(tree, hierarchy);
  5691. };
  5692. function d3_layout_treeSeparation(a, b) {
  5693. return a.parent == b.parent ? 1 : 2;
  5694. }
  5695. // function d3_layout_treeSeparationRadial(a, b) {
  5696. // return (a.parent == b.parent ? 1 : 2) / a.depth;
  5697. // }
  5698. function d3_layout_treeLeft(node) {
  5699. var children = node.children;
  5700. return children && children.length ? children[0] : node._tree.thread;
  5701. }
  5702. function d3_layout_treeRight(node) {
  5703. var children = node.children,
  5704. n;
  5705. return children && (n = children.length) ? children[n - 1] : node._tree.thread;
  5706. }
  5707. function d3_layout_treeSearch(node, compare) {
  5708. var children = node.children;
  5709. if (children && (n = children.length)) {
  5710. var child,
  5711. n,
  5712. i = -1;
  5713. while (++i < n) {
  5714. if (compare(child = d3_layout_treeSearch(children[i], compare), node) > 0) {
  5715. node = child;
  5716. }
  5717. }
  5718. }
  5719. return node;
  5720. }
  5721. function d3_layout_treeRightmost(a, b) {
  5722. return a.x - b.x;
  5723. }
  5724. function d3_layout_treeLeftmost(a, b) {
  5725. return b.x - a.x;
  5726. }
  5727. function d3_layout_treeDeepest(a, b) {
  5728. return a.depth - b.depth;
  5729. }
  5730. function d3_layout_treeVisitAfter(node, callback) {
  5731. function visit(node, previousSibling) {
  5732. var children = node.children;
  5733. if (children && (n = children.length)) {
  5734. var child,
  5735. previousChild = null,
  5736. i = -1,
  5737. n;
  5738. while (++i < n) {
  5739. child = children[i];
  5740. visit(child, previousChild);
  5741. previousChild = child;
  5742. }
  5743. }
  5744. callback(node, previousSibling);
  5745. }
  5746. visit(node, null);
  5747. }
  5748. function d3_layout_treeShift(node) {
  5749. var shift = 0,
  5750. change = 0,
  5751. children = node.children,
  5752. i = children.length,
  5753. child;
  5754. while (--i >= 0) {
  5755. child = children[i]._tree;
  5756. child.prelim += shift;
  5757. child.mod += shift;
  5758. shift += child.shift + (change += child.change);
  5759. }
  5760. }
  5761. function d3_layout_treeMove(ancestor, node, shift) {
  5762. ancestor = ancestor._tree;
  5763. node = node._tree;
  5764. var change = shift / (node.number - ancestor.number);
  5765. ancestor.change += change;
  5766. node.change -= change;
  5767. node.shift += shift;
  5768. node.prelim += shift;
  5769. node.mod += shift;
  5770. }
  5771. function d3_layout_treeAncestor(vim, node, ancestor) {
  5772. return vim._tree.ancestor.parent == node.parent
  5773. ? vim._tree.ancestor
  5774. : ancestor;
  5775. }
  5776. // Squarified Treemaps by Mark Bruls, Kees Huizing, and Jarke J. van Wijk
  5777. // Modified to support a target aspect ratio by Jeff Heer
  5778. d3.layout.treemap = function() {
  5779. var hierarchy = d3.layout.hierarchy(),
  5780. round = Math.round,
  5781. size = [1, 1], // width, height
  5782. padding = null,
  5783. pad = d3_layout_treemapPadNull,
  5784. sticky = false,
  5785. stickies,
  5786. ratio = 0.5 * (1 + Math.sqrt(5)); // golden ratio
  5787. // Compute the area for each child based on value & scale.
  5788. function scale(children, k) {
  5789. var i = -1,
  5790. n = children.length,
  5791. child,
  5792. area;
  5793. while (++i < n) {
  5794. area = (child = children[i]).value * (k < 0 ? 0 : k);
  5795. child.area = isNaN(area) || area <= 0 ? 0 : area;
  5796. }
  5797. }
  5798. // Recursively arranges the specified node's children into squarified rows.
  5799. function squarify(node) {
  5800. var children = node.children;
  5801. if (children && children.length) {
  5802. var rect = pad(node),
  5803. row = [],
  5804. remaining = children.slice(), // copy-on-write
  5805. child,
  5806. best = Infinity, // the best row score so far
  5807. score, // the current row score
  5808. u = Math.min(rect.dx, rect.dy), // initial orientation
  5809. n;
  5810. scale(remaining, rect.dx * rect.dy / node.value);
  5811. row.area = 0;
  5812. while ((n = remaining.length) > 0) {
  5813. row.push(child = remaining[n - 1]);
  5814. row.area += child.area;
  5815. if ((score = worst(row, u)) <= best) { // continue with this orientation
  5816. remaining.pop();
  5817. best = score;
  5818. } else { // abort, and try a different orientation
  5819. row.area -= row.pop().area;
  5820. position(row, u, rect, false);
  5821. u = Math.min(rect.dx, rect.dy);
  5822. row.length = row.area = 0;
  5823. best = Infinity;
  5824. }
  5825. }
  5826. if (row.length) {
  5827. position(row, u, rect, true);
  5828. row.length = row.area = 0;
  5829. }
  5830. children.forEach(squarify);
  5831. }
  5832. }
  5833. // Recursively resizes the specified node's children into existing rows.
  5834. // Preserves the existing layout!
  5835. function stickify(node) {
  5836. var children = node.children;
  5837. if (children && children.length) {
  5838. var rect = pad(node),
  5839. remaining = children.slice(), // copy-on-write
  5840. child,
  5841. row = [];
  5842. scale(remaining, rect.dx * rect.dy / node.value);
  5843. row.area = 0;
  5844. while (child = remaining.pop()) {
  5845. row.push(child);
  5846. row.area += child.area;
  5847. if (child.z != null) {
  5848. position(row, child.z ? rect.dx : rect.dy, rect, !remaining.length);
  5849. row.length = row.area = 0;
  5850. }
  5851. }
  5852. children.forEach(stickify);
  5853. }
  5854. }
  5855. // Computes the score for the specified row, as the worst aspect ratio.
  5856. function worst(row, u) {
  5857. var s = row.area,
  5858. r,
  5859. rmax = 0,
  5860. rmin = Infinity,
  5861. i = -1,
  5862. n = row.length;
  5863. while (++i < n) {
  5864. if (!(r = row[i].area)) continue;
  5865. if (r < rmin) rmin = r;
  5866. if (r > rmax) rmax = r;
  5867. }
  5868. s *= s;
  5869. u *= u;
  5870. return s
  5871. ? Math.max((u * rmax * ratio) / s, s / (u * rmin * ratio))
  5872. : Infinity;
  5873. }
  5874. // Positions the specified row of nodes. Modifies `rect`.
  5875. function position(row, u, rect, flush) {
  5876. var i = -1,
  5877. n = row.length,
  5878. x = rect.x,
  5879. y = rect.y,
  5880. v = u ? round(row.area / u) : 0,
  5881. o;
  5882. if (u == rect.dx) { // horizontal subdivision
  5883. if (flush || v > rect.dy) v = rect.dy; // over+underflow
  5884. while (++i < n) {
  5885. o = row[i];
  5886. o.x = x;
  5887. o.y = y;
  5888. o.dy = v;
  5889. x += o.dx = Math.min(rect.x + rect.dx - x, v ? round(o.area / v) : 0);
  5890. }
  5891. o.z = true;
  5892. o.dx += rect.x + rect.dx - x; // rounding error
  5893. rect.y += v;
  5894. rect.dy -= v;
  5895. } else { // vertical subdivision
  5896. if (flush || v > rect.dx) v = rect.dx; // over+underflow
  5897. while (++i < n) {
  5898. o = row[i];
  5899. o.x = x;
  5900. o.y = y;
  5901. o.dx = v;
  5902. y += o.dy = Math.min(rect.y + rect.dy - y, v ? round(o.area / v) : 0);
  5903. }
  5904. o.z = false;
  5905. o.dy += rect.y + rect.dy - y; // rounding error
  5906. rect.x += v;
  5907. rect.dx -= v;
  5908. }
  5909. }
  5910. function treemap(d) {
  5911. var nodes = stickies || hierarchy(d),
  5912. root = nodes[0];
  5913. root.x = 0;
  5914. root.y = 0;
  5915. root.dx = size[0];
  5916. root.dy = size[1];
  5917. if (stickies) hierarchy.revalue(root);
  5918. scale([root], root.dx * root.dy / root.value);
  5919. (stickies ? stickify : squarify)(root);
  5920. if (sticky) stickies = nodes;
  5921. return nodes;
  5922. }
  5923. treemap.size = function(x) {
  5924. if (!arguments.length) return size;
  5925. size = x;
  5926. return treemap;
  5927. };
  5928. treemap.padding = function(x) {
  5929. if (!arguments.length) return padding;
  5930. function padFunction(node) {
  5931. var p = x.call(treemap, node, node.depth);
  5932. return p == null
  5933. ? d3_layout_treemapPadNull(node)
  5934. : d3_layout_treemapPad(node, typeof p === "number" ? [p, p, p, p] : p);
  5935. }
  5936. function padConstant(node) {
  5937. return d3_layout_treemapPad(node, x);
  5938. }
  5939. var type;
  5940. pad = (padding = x) == null ? d3_layout_treemapPadNull
  5941. : (type = typeof x) === "function" ? padFunction
  5942. : type === "number" ? (x = [x, x, x, x], padConstant)
  5943. : padConstant;
  5944. return treemap;
  5945. };
  5946. treemap.round = function(x) {
  5947. if (!arguments.length) return round != Number;
  5948. round = x ? Math.round : Number;
  5949. return treemap;
  5950. };
  5951. treemap.sticky = function(x) {
  5952. if (!arguments.length) return sticky;
  5953. sticky = x;
  5954. stickies = null;
  5955. return treemap;
  5956. };
  5957. treemap.ratio = function(x) {
  5958. if (!arguments.length) return ratio;
  5959. ratio = x;
  5960. return treemap;
  5961. };
  5962. return d3_layout_hierarchyRebind(treemap, hierarchy);
  5963. };
  5964. function d3_layout_treemapPadNull(node) {
  5965. return {x: node.x, y: node.y, dx: node.dx, dy: node.dy};
  5966. }
  5967. function d3_layout_treemapPad(node, padding) {
  5968. var x = node.x + padding[3],
  5969. y = node.y + padding[0],
  5970. dx = node.dx - padding[1] - padding[3],
  5971. dy = node.dy - padding[0] - padding[2];
  5972. if (dx < 0) { x += dx / 2; dx = 0; }
  5973. if (dy < 0) { y += dy / 2; dy = 0; }
  5974. return {x: x, y: y, dx: dx, dy: dy};
  5975. }
  5976. d3.csv = function(url, callback) {
  5977. d3.text(url, "text/csv", function(text) {
  5978. callback(text && d3.csv.parse(text));
  5979. });
  5980. };
  5981. d3.csv.parse = function(text) {
  5982. var header;
  5983. return d3.csv.parseRows(text, function(row, i) {
  5984. if (i) {
  5985. var o = {}, j = -1, m = header.length;
  5986. while (++j < m) o[header[j]] = row[j];
  5987. return o;
  5988. } else {
  5989. header = row;
  5990. return null;
  5991. }
  5992. });
  5993. };
  5994. d3.csv.parseRows = function(text, f) {
  5995. var EOL = {}, // sentinel value for end-of-line
  5996. EOF = {}, // sentinel value for end-of-file
  5997. rows = [], // output rows
  5998. re = /\r\n|[,\r\n]/g, // field separator regex
  5999. n = 0, // the current line number
  6000. t, // the current token
  6001. eol; // is the current token followed by EOL?
  6002. re.lastIndex = 0; // work-around bug in FF 3.6
  6003. /** @private Returns the next token. */
  6004. function token() {
  6005. if (re.lastIndex >= text.length) return EOF; // special case: end of file
  6006. if (eol) { eol = false; return EOL; } // special case: end of line
  6007. // special case: quotes
  6008. var j = re.lastIndex;
  6009. if (text.charCodeAt(j) === 34) {
  6010. var i = j;
  6011. while (i++ < text.length) {
  6012. if (text.charCodeAt(i) === 34) {
  6013. if (text.charCodeAt(i + 1) !== 34) break;
  6014. i++;
  6015. }
  6016. }
  6017. re.lastIndex = i + 2;
  6018. var c = text.charCodeAt(i + 1);
  6019. if (c === 13) {
  6020. eol = true;
  6021. if (text.charCodeAt(i + 2) === 10) re.lastIndex++;
  6022. } else if (c === 10) {
  6023. eol = true;
  6024. }
  6025. return text.substring(j + 1, i).replace(/""/g, "\"");
  6026. }
  6027. // common case
  6028. var m = re.exec(text);
  6029. if (m) {
  6030. eol = m[0].charCodeAt(0) !== 44;
  6031. return text.substring(j, m.index);
  6032. }
  6033. re.lastIndex = text.length;
  6034. return text.substring(j);
  6035. }
  6036. while ((t = token()) !== EOF) {
  6037. var a = [];
  6038. while ((t !== EOL) && (t !== EOF)) {
  6039. a.push(t);
  6040. t = token();
  6041. }
  6042. if (f && !(a = f(a, n++))) continue;
  6043. rows.push(a);
  6044. }
  6045. return rows;
  6046. };
  6047. d3.csv.format = function(rows) {
  6048. return rows.map(d3_csv_formatRow).join("\n");
  6049. };
  6050. function d3_csv_formatRow(row) {
  6051. return row.map(d3_csv_formatValue).join(",");
  6052. }
  6053. function d3_csv_formatValue(text) {
  6054. return /[",\n]/.test(text)
  6055. ? "\"" + text.replace(/\"/g, "\"\"") + "\""
  6056. : text;
  6057. }
  6058. d3.geo = {};
  6059. var d3_geo_radians = Math.PI / 180;
  6060. // TODO clip input coordinates on opposite hemisphere
  6061. d3.geo.azimuthal = function() {
  6062. var mode = "orthographic", // or stereographic, gnomonic, equidistant or equalarea
  6063. origin,
  6064. scale = 200,
  6065. translate = [480, 250],
  6066. x0,
  6067. y0,
  6068. cy0,
  6069. sy0;
  6070. function azimuthal(coordinates) {
  6071. var x1 = coordinates[0] * d3_geo_radians - x0,
  6072. y1 = coordinates[1] * d3_geo_radians,
  6073. cx1 = Math.cos(x1),
  6074. sx1 = Math.sin(x1),
  6075. cy1 = Math.cos(y1),
  6076. sy1 = Math.sin(y1),
  6077. cc = mode !== "orthographic" ? sy0 * sy1 + cy0 * cy1 * cx1 : null,
  6078. c,
  6079. k = mode === "stereographic" ? 1 / (1 + cc)
  6080. : mode === "gnomonic" ? 1 / cc
  6081. : mode === "equidistant" ? (c = Math.acos(cc), c ? c / Math.sin(c) : 0)
  6082. : mode === "equalarea" ? Math.sqrt(2 / (1 + cc))
  6083. : 1,
  6084. x = k * cy1 * sx1,
  6085. y = k * (sy0 * cy1 * cx1 - cy0 * sy1);
  6086. return [
  6087. scale * x + translate[0],
  6088. scale * y + translate[1]
  6089. ];
  6090. }
  6091. azimuthal.invert = function(coordinates) {
  6092. var x = (coordinates[0] - translate[0]) / scale,
  6093. y = (coordinates[1] - translate[1]) / scale,
  6094. p = Math.sqrt(x * x + y * y),
  6095. c = mode === "stereographic" ? 2 * Math.atan(p)
  6096. : mode === "gnomonic" ? Math.atan(p)
  6097. : mode === "equidistant" ? p
  6098. : mode === "equalarea" ? 2 * Math.asin(.5 * p)
  6099. : Math.asin(p),
  6100. sc = Math.sin(c),
  6101. cc = Math.cos(c);
  6102. return [
  6103. (x0 + Math.atan2(x * sc, p * cy0 * cc + y * sy0 * sc)) / d3_geo_radians,
  6104. Math.asin(cc * sy0 - (p ? (y * sc * cy0) / p : 0)) / d3_geo_radians
  6105. ];
  6106. };
  6107. azimuthal.mode = function(x) {
  6108. if (!arguments.length) return mode;
  6109. mode = x + "";
  6110. return azimuthal;
  6111. };
  6112. azimuthal.origin = function(x) {
  6113. if (!arguments.length) return origin;
  6114. origin = x;
  6115. x0 = origin[0] * d3_geo_radians;
  6116. y0 = origin[1] * d3_geo_radians;
  6117. cy0 = Math.cos(y0);
  6118. sy0 = Math.sin(y0);
  6119. return azimuthal;
  6120. };
  6121. azimuthal.scale = function(x) {
  6122. if (!arguments.length) return scale;
  6123. scale = +x;
  6124. return azimuthal;
  6125. };
  6126. azimuthal.translate = function(x) {
  6127. if (!arguments.length) return translate;
  6128. translate = [+x[0], +x[1]];
  6129. return azimuthal;
  6130. };
  6131. return azimuthal.origin([0, 0]);
  6132. };
  6133. // Derived from Tom Carden's Albers implementation for Protovis.
  6134. // http://gist.github.com/476238
  6135. // http://mathworld.wolfram.com/AlbersEqual-AreaConicProjection.html
  6136. d3.geo.albers = function() {
  6137. var origin = [-98, 38],
  6138. parallels = [29.5, 45.5],
  6139. scale = 1000,
  6140. translate = [480, 250],
  6141. lng0, // d3_geo_radians * origin[0]
  6142. n,
  6143. C,
  6144. p0;
  6145. function albers(coordinates) {
  6146. var t = n * (d3_geo_radians * coordinates[0] - lng0),
  6147. p = Math.sqrt(C - 2 * n * Math.sin(d3_geo_radians * coordinates[1])) / n;
  6148. return [
  6149. scale * p * Math.sin(t) + translate[0],
  6150. scale * (p * Math.cos(t) - p0) + translate[1]
  6151. ];
  6152. }
  6153. albers.invert = function(coordinates) {
  6154. var x = (coordinates[0] - translate[0]) / scale,
  6155. y = (coordinates[1] - translate[1]) / scale,
  6156. p0y = p0 + y,
  6157. t = Math.atan2(x, p0y),
  6158. p = Math.sqrt(x * x + p0y * p0y);
  6159. return [
  6160. (lng0 + t / n) / d3_geo_radians,
  6161. Math.asin((C - p * p * n * n) / (2 * n)) / d3_geo_radians
  6162. ];
  6163. };
  6164. function reload() {
  6165. var phi1 = d3_geo_radians * parallels[0],
  6166. phi2 = d3_geo_radians * parallels[1],
  6167. lat0 = d3_geo_radians * origin[1],
  6168. s = Math.sin(phi1),
  6169. c = Math.cos(phi1);
  6170. lng0 = d3_geo_radians * origin[0];
  6171. n = .5 * (s + Math.sin(phi2));
  6172. C = c * c + 2 * n * s;
  6173. p0 = Math.sqrt(C - 2 * n * Math.sin(lat0)) / n;
  6174. return albers;
  6175. }
  6176. albers.origin = function(x) {
  6177. if (!arguments.length) return origin;
  6178. origin = [+x[0], +x[1]];
  6179. return reload();
  6180. };
  6181. albers.parallels = function(x) {
  6182. if (!arguments.length) return parallels;
  6183. parallels = [+x[0], +x[1]];
  6184. return reload();
  6185. };
  6186. albers.scale = function(x) {
  6187. if (!arguments.length) return scale;
  6188. scale = +x;
  6189. return albers;
  6190. };
  6191. albers.translate = function(x) {
  6192. if (!arguments.length) return translate;
  6193. translate = [+x[0], +x[1]];
  6194. return albers;
  6195. };
  6196. return reload();
  6197. };
  6198. // A composite projection for the United States, 960x500. The set of standard
  6199. // parallels for each region comes from USGS, which is published here:
  6200. // http://egsc.usgs.gov/isb/pubs/MapProjections/projections.html#albers
  6201. // TODO allow the composite projection to be rescaled?
  6202. d3.geo.albersUsa = function() {
  6203. var lower48 = d3.geo.albers();
  6204. var alaska = d3.geo.albers()
  6205. .origin([-160, 60])
  6206. .parallels([55, 65]);
  6207. var hawaii = d3.geo.albers()
  6208. .origin([-160, 20])
  6209. .parallels([8, 18]);
  6210. var puertoRico = d3.geo.albers()
  6211. .origin([-60, 10])
  6212. .parallels([8, 18]);
  6213. function albersUsa(coordinates) {
  6214. var lon = coordinates[0],
  6215. lat = coordinates[1];
  6216. return (lat > 50 ? alaska
  6217. : lon < -140 ? hawaii
  6218. : lat < 21 ? puertoRico
  6219. : lower48)(coordinates);
  6220. }
  6221. albersUsa.scale = function(x) {
  6222. if (!arguments.length) return lower48.scale();
  6223. lower48.scale(x);
  6224. alaska.scale(x * .6);
  6225. hawaii.scale(x);
  6226. puertoRico.scale(x * 1.5);
  6227. return albersUsa.translate(lower48.translate());
  6228. };
  6229. albersUsa.translate = function(x) {
  6230. if (!arguments.length) return lower48.translate();
  6231. var dz = lower48.scale() / 1000,
  6232. dx = x[0],
  6233. dy = x[1];
  6234. lower48.translate(x);
  6235. alaska.translate([dx - 400 * dz, dy + 170 * dz]);
  6236. hawaii.translate([dx - 190 * dz, dy + 200 * dz]);
  6237. puertoRico.translate([dx + 580 * dz, dy + 430 * dz]);
  6238. return albersUsa;
  6239. };
  6240. return albersUsa.scale(lower48.scale());
  6241. };
  6242. d3.geo.bonne = function() {
  6243. var scale = 200,
  6244. translate = [480, 250],
  6245. x0, // origin longitude in radians
  6246. y0, // origin latitude in radians
  6247. y1, // parallel latitude in radians
  6248. c1; // cot(y1)
  6249. function bonne(coordinates) {
  6250. var x = coordinates[0] * d3_geo_radians - x0,
  6251. y = coordinates[1] * d3_geo_radians - y0;
  6252. if (y1) {
  6253. var p = c1 + y1 - y, E = x * Math.cos(y) / p;
  6254. x = p * Math.sin(E);
  6255. y = p * Math.cos(E) - c1;
  6256. } else {
  6257. x *= Math.cos(y);
  6258. y *= -1;
  6259. }
  6260. return [
  6261. scale * x + translate[0],
  6262. scale * y + translate[1]
  6263. ];
  6264. }
  6265. bonne.invert = function(coordinates) {
  6266. var x = (coordinates[0] - translate[0]) / scale,
  6267. y = (coordinates[1] - translate[1]) / scale;
  6268. if (y1) {
  6269. var c = c1 + y, p = Math.sqrt(x * x + c * c);
  6270. y = c1 + y1 - p;
  6271. x = x0 + p * Math.atan2(x, c) / Math.cos(y);
  6272. } else {
  6273. y *= -1;
  6274. x /= Math.cos(y);
  6275. }
  6276. return [
  6277. x / d3_geo_radians,
  6278. y / d3_geo_radians
  6279. ];
  6280. };
  6281. // 90° for Werner, 0° for Sinusoidal
  6282. bonne.parallel = function(x) {
  6283. if (!arguments.length) return y1 / d3_geo_radians;
  6284. c1 = 1 / Math.tan(y1 = x * d3_geo_radians);
  6285. return bonne;
  6286. };
  6287. bonne.origin = function(x) {
  6288. if (!arguments.length) return [x0 / d3_geo_radians, y0 / d3_geo_radians];
  6289. x0 = x[0] * d3_geo_radians;
  6290. y0 = x[1] * d3_geo_radians;
  6291. return bonne;
  6292. };
  6293. bonne.scale = function(x) {
  6294. if (!arguments.length) return scale;
  6295. scale = +x;
  6296. return bonne;
  6297. };
  6298. bonne.translate = function(x) {
  6299. if (!arguments.length) return translate;
  6300. translate = [+x[0], +x[1]];
  6301. return bonne;
  6302. };
  6303. return bonne.origin([0, 0]).parallel(45);
  6304. };
  6305. d3.geo.equirectangular = function() {
  6306. var scale = 500,
  6307. translate = [480, 250];
  6308. function equirectangular(coordinates) {
  6309. var x = coordinates[0] / 360,
  6310. y = -coordinates[1] / 360;
  6311. return [
  6312. scale * x + translate[0],
  6313. scale * y + translate[1]
  6314. ];
  6315. }
  6316. equirectangular.invert = function(coordinates) {
  6317. var x = (coordinates[0] - translate[0]) / scale,
  6318. y = (coordinates[1] - translate[1]) / scale;
  6319. return [
  6320. 360 * x,
  6321. -360 * y
  6322. ];
  6323. };
  6324. equirectangular.scale = function(x) {
  6325. if (!arguments.length) return scale;
  6326. scale = +x;
  6327. return equirectangular;
  6328. };
  6329. equirectangular.translate = function(x) {
  6330. if (!arguments.length) return translate;
  6331. translate = [+x[0], +x[1]];
  6332. return equirectangular;
  6333. };
  6334. return equirectangular;
  6335. };
  6336. d3.geo.mercator = function() {
  6337. var scale = 500,
  6338. translate = [480, 250];
  6339. function mercator(coordinates) {
  6340. var x = coordinates[0] / 360,
  6341. y = -(Math.log(Math.tan(Math.PI / 4 + coordinates[1] * d3_geo_radians / 2)) / d3_geo_radians) / 360;
  6342. return [
  6343. scale * x + translate[0],
  6344. scale * Math.max(-.5, Math.min(.5, y)) + translate[1]
  6345. ];
  6346. }
  6347. mercator.invert = function(coordinates) {
  6348. var x = (coordinates[0] - translate[0]) / scale,
  6349. y = (coordinates[1] - translate[1]) / scale;
  6350. return [
  6351. 360 * x,
  6352. 2 * Math.atan(Math.exp(-360 * y * d3_geo_radians)) / d3_geo_radians - 90
  6353. ];
  6354. };
  6355. mercator.scale = function(x) {
  6356. if (!arguments.length) return scale;
  6357. scale = +x;
  6358. return mercator;
  6359. };
  6360. mercator.translate = function(x) {
  6361. if (!arguments.length) return translate;
  6362. translate = [+x[0], +x[1]];
  6363. return mercator;
  6364. };
  6365. return mercator;
  6366. };
  6367. function d3_geo_type(types, defaultValue) {
  6368. return function(object) {
  6369. return object && types.hasOwnProperty(object.type) ? types[object.type](object) : defaultValue;
  6370. };
  6371. }
  6372. /**
  6373. * Returns a function that, given a GeoJSON object (e.g., a feature), returns
  6374. * the corresponding SVG path. The function can be customized by overriding the
  6375. * projection. Point features are mapped to circles with a default radius of
  6376. * 4.5px; the radius can be specified either as a constant or a function that
  6377. * is evaluated per object.
  6378. */
  6379. d3.geo.path = function() {
  6380. var pointRadius = 4.5,
  6381. pointCircle = d3_path_circle(pointRadius),
  6382. projection = d3.geo.albersUsa();
  6383. function path(d, i) {
  6384. if (typeof pointRadius === "function") {
  6385. pointCircle = d3_path_circle(pointRadius.apply(this, arguments));
  6386. }
  6387. return pathType(d) || null;
  6388. }
  6389. function project(coordinates) {
  6390. return projection(coordinates).join(",");
  6391. }
  6392. var pathType = d3_geo_type({
  6393. FeatureCollection: function(o) {
  6394. var path = [],
  6395. features = o.features,
  6396. i = -1, // features.index
  6397. n = features.length;
  6398. while (++i < n) path.push(pathType(features[i].geometry));
  6399. return path.join("");
  6400. },
  6401. Feature: function(o) {
  6402. return pathType(o.geometry);
  6403. },
  6404. Point: function(o) {
  6405. return "M" + project(o.coordinates) + pointCircle;
  6406. },
  6407. MultiPoint: function(o) {
  6408. var path = [],
  6409. coordinates = o.coordinates,
  6410. i = -1, // coordinates.index
  6411. n = coordinates.length;
  6412. while (++i < n) path.push("M", project(coordinates[i]), pointCircle);
  6413. return path.join("");
  6414. },
  6415. LineString: function(o) {
  6416. var path = ["M"],
  6417. coordinates = o.coordinates,
  6418. i = -1, // coordinates.index
  6419. n = coordinates.length;
  6420. while (++i < n) path.push(project(coordinates[i]), "L");
  6421. path.pop();
  6422. return path.join("");
  6423. },
  6424. MultiLineString: function(o) {
  6425. var path = [],
  6426. coordinates = o.coordinates,
  6427. i = -1, // coordinates.index
  6428. n = coordinates.length,
  6429. subcoordinates, // coordinates[i]
  6430. j, // subcoordinates.index
  6431. m; // subcoordinates.length
  6432. while (++i < n) {
  6433. subcoordinates = coordinates[i];
  6434. j = -1;
  6435. m = subcoordinates.length;
  6436. path.push("M");
  6437. while (++j < m) path.push(project(subcoordinates[j]), "L");
  6438. path.pop();
  6439. }
  6440. return path.join("");
  6441. },
  6442. Polygon: function(o) {
  6443. var path = [],
  6444. coordinates = o.coordinates,
  6445. i = -1, // coordinates.index
  6446. n = coordinates.length,
  6447. subcoordinates, // coordinates[i]
  6448. j, // subcoordinates.index
  6449. m; // subcoordinates.length
  6450. while (++i < n) {
  6451. subcoordinates = coordinates[i];
  6452. j = -1;
  6453. if ((m = subcoordinates.length - 1) > 0) {
  6454. path.push("M");
  6455. while (++j < m) path.push(project(subcoordinates[j]), "L");
  6456. path[path.length - 1] = "Z";
  6457. }
  6458. }
  6459. return path.join("");
  6460. },
  6461. MultiPolygon: function(o) {
  6462. var path = [],
  6463. coordinates = o.coordinates,
  6464. i = -1, // coordinates index
  6465. n = coordinates.length,
  6466. subcoordinates, // coordinates[i]
  6467. j, // subcoordinates index
  6468. m, // subcoordinates.length
  6469. subsubcoordinates, // subcoordinates[j]
  6470. k, // subsubcoordinates index
  6471. p; // subsubcoordinates.length
  6472. while (++i < n) {
  6473. subcoordinates = coordinates[i];
  6474. j = -1;
  6475. m = subcoordinates.length;
  6476. while (++j < m) {
  6477. subsubcoordinates = subcoordinates[j];
  6478. k = -1;
  6479. if ((p = subsubcoordinates.length - 1) > 0) {
  6480. path.push("M");
  6481. while (++k < p) path.push(project(subsubcoordinates[k]), "L");
  6482. path[path.length - 1] = "Z";
  6483. }
  6484. }
  6485. }
  6486. return path.join("");
  6487. },
  6488. GeometryCollection: function(o) {
  6489. var path = [],
  6490. geometries = o.geometries,
  6491. i = -1, // geometries index
  6492. n = geometries.length;
  6493. while (++i < n) path.push(pathType(geometries[i]));
  6494. return path.join("");
  6495. }
  6496. });
  6497. var areaType = path.area = d3_geo_type({
  6498. FeatureCollection: function(o) {
  6499. var area = 0,
  6500. features = o.features,
  6501. i = -1, // features.index
  6502. n = features.length;
  6503. while (++i < n) area += areaType(features[i]);
  6504. return area;
  6505. },
  6506. Feature: function(o) {
  6507. return areaType(o.geometry);
  6508. },
  6509. Polygon: function(o) {
  6510. return polygonArea(o.coordinates);
  6511. },
  6512. MultiPolygon: function(o) {
  6513. var sum = 0,
  6514. coordinates = o.coordinates,
  6515. i = -1, // coordinates index
  6516. n = coordinates.length;
  6517. while (++i < n) sum += polygonArea(coordinates[i]);
  6518. return sum;
  6519. },
  6520. GeometryCollection: function(o) {
  6521. var sum = 0,
  6522. geometries = o.geometries,
  6523. i = -1, // geometries index
  6524. n = geometries.length;
  6525. while (++i < n) sum += areaType(geometries[i]);
  6526. return sum;
  6527. }
  6528. }, 0);
  6529. function polygonArea(coordinates) {
  6530. var sum = area(coordinates[0]), // exterior ring
  6531. i = 0, // coordinates.index
  6532. n = coordinates.length;
  6533. while (++i < n) sum -= area(coordinates[i]); // holes
  6534. return sum;
  6535. }
  6536. function polygonCentroid(coordinates) {
  6537. var polygon = d3.geom.polygon(coordinates[0].map(projection)), // exterior ring
  6538. area = polygon.area(),
  6539. centroid = polygon.centroid(area < 0 ? (area *= -1, 1) : -1),
  6540. x = centroid[0],
  6541. y = centroid[1],
  6542. z = area,
  6543. i = 0, // coordinates index
  6544. n = coordinates.length;
  6545. while (++i < n) {
  6546. polygon = d3.geom.polygon(coordinates[i].map(projection)); // holes
  6547. area = polygon.area();
  6548. centroid = polygon.centroid(area < 0 ? (area *= -1, 1) : -1);
  6549. x -= centroid[0];
  6550. y -= centroid[1];
  6551. z -= area;
  6552. }
  6553. return [x, y, 6 * z]; // weighted centroid
  6554. }
  6555. var centroidType = path.centroid = d3_geo_type({
  6556. // TODO FeatureCollection
  6557. // TODO Point
  6558. // TODO MultiPoint
  6559. // TODO LineString
  6560. // TODO MultiLineString
  6561. // TODO GeometryCollection
  6562. Feature: function(o) {
  6563. return centroidType(o.geometry);
  6564. },
  6565. Polygon: function(o) {
  6566. var centroid = polygonCentroid(o.coordinates);
  6567. return [centroid[0] / centroid[2], centroid[1] / centroid[2]];
  6568. },
  6569. MultiPolygon: function(o) {
  6570. var area = 0,
  6571. coordinates = o.coordinates,
  6572. centroid,
  6573. x = 0,
  6574. y = 0,
  6575. z = 0,
  6576. i = -1, // coordinates index
  6577. n = coordinates.length;
  6578. while (++i < n) {
  6579. centroid = polygonCentroid(coordinates[i]);
  6580. x += centroid[0];
  6581. y += centroid[1];
  6582. z += centroid[2];
  6583. }
  6584. return [x / z, y / z];
  6585. }
  6586. });
  6587. function area(coordinates) {
  6588. return Math.abs(d3.geom.polygon(coordinates.map(projection)).area());
  6589. }
  6590. path.projection = function(x) {
  6591. projection = x;
  6592. return path;
  6593. };
  6594. path.pointRadius = function(x) {
  6595. if (typeof x === "function") pointRadius = x;
  6596. else {
  6597. pointRadius = +x;
  6598. pointCircle = d3_path_circle(pointRadius);
  6599. }
  6600. return path;
  6601. };
  6602. return path;
  6603. };
  6604. function d3_path_circle(radius) {
  6605. return "m0," + radius
  6606. + "a" + radius + "," + radius + " 0 1,1 0," + (-2 * radius)
  6607. + "a" + radius + "," + radius + " 0 1,1 0," + (+2 * radius)
  6608. + "z";
  6609. }
  6610. /**
  6611. * Given a GeoJSON object, returns the corresponding bounding box. The bounding
  6612. * box is represented by a two-dimensional array: [[left, bottom], [right,
  6613. * top]], where left is the minimum longitude, bottom is the minimum latitude,
  6614. * right is maximum longitude, and top is the maximum latitude.
  6615. */
  6616. d3.geo.bounds = function(feature) {
  6617. var left = Infinity,
  6618. bottom = Infinity,
  6619. right = -Infinity,
  6620. top = -Infinity;
  6621. d3_geo_bounds(feature, function(x, y) {
  6622. if (x < left) left = x;
  6623. if (x > right) right = x;
  6624. if (y < bottom) bottom = y;
  6625. if (y > top) top = y;
  6626. });
  6627. return [[left, bottom], [right, top]];
  6628. };
  6629. function d3_geo_bounds(o, f) {
  6630. if (d3_geo_boundsTypes.hasOwnProperty(o.type)) d3_geo_boundsTypes[o.type](o, f);
  6631. }
  6632. var d3_geo_boundsTypes = {
  6633. Feature: d3_geo_boundsFeature,
  6634. FeatureCollection: d3_geo_boundsFeatureCollection,
  6635. GeometryCollection: d3_geo_boundsGeometryCollection,
  6636. LineString: d3_geo_boundsLineString,
  6637. MultiLineString: d3_geo_boundsMultiLineString,
  6638. MultiPoint: d3_geo_boundsLineString,
  6639. MultiPolygon: d3_geo_boundsMultiPolygon,
  6640. Point: d3_geo_boundsPoint,
  6641. Polygon: d3_geo_boundsPolygon
  6642. };
  6643. function d3_geo_boundsFeature(o, f) {
  6644. d3_geo_bounds(o.geometry, f);
  6645. }
  6646. function d3_geo_boundsFeatureCollection(o, f) {
  6647. for (var a = o.features, i = 0, n = a.length; i < n; i++) {
  6648. d3_geo_bounds(a[i].geometry, f);
  6649. }
  6650. }
  6651. function d3_geo_boundsGeometryCollection(o, f) {
  6652. for (var a = o.geometries, i = 0, n = a.length; i < n; i++) {
  6653. d3_geo_bounds(a[i], f);
  6654. }
  6655. }
  6656. function d3_geo_boundsLineString(o, f) {
  6657. for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
  6658. f.apply(null, a[i]);
  6659. }
  6660. }
  6661. function d3_geo_boundsMultiLineString(o, f) {
  6662. for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
  6663. for (var b = a[i], j = 0, m = b.length; j < m; j++) {
  6664. f.apply(null, b[j]);
  6665. }
  6666. }
  6667. }
  6668. function d3_geo_boundsMultiPolygon(o, f) {
  6669. for (var a = o.coordinates, i = 0, n = a.length; i < n; i++) {
  6670. for (var b = a[i][0], j = 0, m = b.length; j < m; j++) {
  6671. f.apply(null, b[j]);
  6672. }
  6673. }
  6674. }
  6675. function d3_geo_boundsPoint(o, f) {
  6676. f.apply(null, o.coordinates);
  6677. }
  6678. function d3_geo_boundsPolygon(o, f) {
  6679. for (var a = o.coordinates[0], i = 0, n = a.length; i < n; i++) {
  6680. f.apply(null, a[i]);
  6681. }
  6682. }
  6683. // TODO breakAtDateLine?
  6684. d3.geo.circle = function() {
  6685. var origin = [0, 0],
  6686. degrees = 90 - 1e-2,
  6687. radians = degrees * d3_geo_radians,
  6688. arc = d3.geo.greatArc().target(Object);
  6689. function circle() {
  6690. // TODO render a circle as a Polygon
  6691. }
  6692. function visible(point) {
  6693. return arc.distance(point) < radians;
  6694. }
  6695. circle.clip = function(d) {
  6696. arc.source(typeof origin === "function" ? origin.apply(this, arguments) : origin);
  6697. return clipType(d);
  6698. };
  6699. var clipType = d3_geo_type({
  6700. FeatureCollection: function(o) {
  6701. var features = o.features.map(clipType).filter(Object);
  6702. return features && (o = Object.create(o), o.features = features, o);
  6703. },
  6704. Feature: function(o) {
  6705. var geometry = clipType(o.geometry);
  6706. return geometry && (o = Object.create(o), o.geometry = geometry, o);
  6707. },
  6708. Point: function(o) {
  6709. return visible(o.coordinates) && o;
  6710. },
  6711. MultiPoint: function(o) {
  6712. var coordinates = o.coordinates.filter(visible);
  6713. return coordinates.length && {
  6714. type: o.type,
  6715. coordinates: coordinates
  6716. };
  6717. },
  6718. LineString: function(o) {
  6719. var coordinates = clip(o.coordinates);
  6720. return coordinates.length && (o = Object.create(o), o.coordinates = coordinates, o);
  6721. },
  6722. MultiLineString: function(o) {
  6723. var coordinates = o.coordinates.map(clip).filter(function(d) { return d.length; });
  6724. return coordinates.length && (o = Object.create(o), o.coordinates = coordinates, o);
  6725. },
  6726. Polygon: function(o) {
  6727. var coordinates = o.coordinates.map(clip);
  6728. return coordinates[0].length && (o = Object.create(o), o.coordinates = coordinates, o);
  6729. },
  6730. MultiPolygon: function(o) {
  6731. var coordinates = o.coordinates.map(function(d) { return d.map(clip); }).filter(function(d) { return d[0].length; });
  6732. return coordinates.length && (o = Object.create(o), o.coordinates = coordinates, o);
  6733. },
  6734. GeometryCollection: function(o) {
  6735. var geometries = o.geometries.map(clipType).filter(Object);
  6736. return geometries.length && (o = Object.create(o), o.geometries = geometries, o);
  6737. }
  6738. });
  6739. function clip(coordinates) {
  6740. var i = -1,
  6741. n = coordinates.length,
  6742. clipped = [],
  6743. p0,
  6744. p1,
  6745. p2,
  6746. d0,
  6747. d1;
  6748. while (++i < n) {
  6749. d1 = arc.distance(p2 = coordinates[i]);
  6750. if (d1 < radians) {
  6751. if (p1) clipped.push(d3_geo_greatArcInterpolate(p1, p2)((d0 - radians) / (d0 - d1)));
  6752. clipped.push(p2);
  6753. p0 = p1 = null;
  6754. } else {
  6755. p1 = p2;
  6756. if (!p0 && clipped.length) {
  6757. clipped.push(d3_geo_greatArcInterpolate(clipped[clipped.length - 1], p1)((radians - d0) / (d1 - d0)));
  6758. p0 = p1;
  6759. }
  6760. }
  6761. d0 = d1;
  6762. }
  6763. if (p1 && clipped.length) {
  6764. d1 = arc.distance(p2 = clipped[0]);
  6765. clipped.push(d3_geo_greatArcInterpolate(p1, p2)((d0 - radians) / (d0 - d1)));
  6766. }
  6767. return resample(clipped);
  6768. }
  6769. // Resample coordinates, creating great arcs between each.
  6770. function resample(coordinates) {
  6771. var i = 0,
  6772. n = coordinates.length,
  6773. j,
  6774. m,
  6775. resampled = n ? [coordinates[0]] : coordinates,
  6776. resamples,
  6777. origin = arc.source();
  6778. while (++i < n) {
  6779. resamples = arc.source(coordinates[i - 1])(coordinates[i]).coordinates;
  6780. for (j = 0, m = resamples.length; ++j < m;) resampled.push(resamples[j]);
  6781. }
  6782. arc.source(origin);
  6783. return resampled;
  6784. }
  6785. circle.origin = function(x) {
  6786. if (!arguments.length) return origin;
  6787. origin = x;
  6788. return circle;
  6789. };
  6790. circle.angle = function(x) {
  6791. if (!arguments.length) return degrees;
  6792. radians = (degrees = +x) * d3_geo_radians;
  6793. return circle;
  6794. };
  6795. // Precision is specified in degrees.
  6796. circle.precision = function(x) {
  6797. if (!arguments.length) return arc.precision();
  6798. arc.precision(x);
  6799. return circle;
  6800. };
  6801. return circle;
  6802. }
  6803. d3.geo.greatArc = function() {
  6804. var source = d3_geo_greatArcSource,
  6805. target = d3_geo_greatArcTarget,
  6806. precision = 6 * d3_geo_radians;
  6807. function greatArc() {
  6808. var a = typeof source === "function" ? source.apply(this, arguments) : source,
  6809. b = typeof target === "function" ? target.apply(this, arguments) : target,
  6810. i = d3_geo_greatArcInterpolate(a, b),
  6811. dt = precision / i.d,
  6812. t = 0,
  6813. coordinates = [a];
  6814. while ((t += dt) < 1) coordinates.push(i(t));
  6815. coordinates.push(b);
  6816. return {
  6817. type: "LineString",
  6818. coordinates: coordinates
  6819. };
  6820. }
  6821. // Length returned in radians; multiply by radius for distance.
  6822. greatArc.distance = function() {
  6823. var a = typeof source === "function" ? source.apply(this, arguments) : source,
  6824. b = typeof target === "function" ? target.apply(this, arguments) : target;
  6825. return d3_geo_greatArcInterpolate(a, b).d;
  6826. };
  6827. greatArc.source = function(x) {
  6828. if (!arguments.length) return source;
  6829. source = x;
  6830. return greatArc;
  6831. };
  6832. greatArc.target = function(x) {
  6833. if (!arguments.length) return target;
  6834. target = x;
  6835. return greatArc;
  6836. };
  6837. // Precision is specified in degrees.
  6838. greatArc.precision = function(x) {
  6839. if (!arguments.length) return precision / d3_geo_radians;
  6840. precision = x * d3_geo_radians;
  6841. return greatArc;
  6842. };
  6843. return greatArc;
  6844. };
  6845. function d3_geo_greatArcSource(d) {
  6846. return d.source;
  6847. }
  6848. function d3_geo_greatArcTarget(d) {
  6849. return d.target;
  6850. }
  6851. function d3_geo_greatArcInterpolate(a, b) {
  6852. var x0 = a[0] * d3_geo_radians, cx0 = Math.cos(x0), sx0 = Math.sin(x0),
  6853. y0 = a[1] * d3_geo_radians, cy0 = Math.cos(y0), sy0 = Math.sin(y0),
  6854. x1 = b[0] * d3_geo_radians, cx1 = Math.cos(x1), sx1 = Math.sin(x1),
  6855. y1 = b[1] * d3_geo_radians, cy1 = Math.cos(y1), sy1 = Math.sin(y1),
  6856. d = interpolate.d = Math.acos(Math.max(-1, Math.min(1, sy0 * sy1 + cy0 * cy1 * Math.cos(x1 - x0)))),
  6857. sd = Math.sin(d);
  6858. // From http://williams.best.vwh.net/avform.htm#Intermediate
  6859. function interpolate(t) {
  6860. var A = Math.sin(d - (t *= d)) / sd,
  6861. B = Math.sin(t) / sd,
  6862. x = A * cy0 * cx0 + B * cy1 * cx1,
  6863. y = A * cy0 * sx0 + B * cy1 * sx1,
  6864. z = A * sy0 + B * sy1;
  6865. return [
  6866. Math.atan2(y, x) / d3_geo_radians,
  6867. Math.atan2(z, Math.sqrt(x * x + y * y)) / d3_geo_radians
  6868. ];
  6869. }
  6870. return interpolate;
  6871. }
  6872. d3.geo.greatCircle = d3.geo.circle;
  6873. d3.geom = {};
  6874. /**
  6875. * Computes a contour for a given input grid function using the <a
  6876. * href="http://en.wikipedia.org/wiki/Marching_squares">marching
  6877. * squares</a> algorithm. Returns the contour polygon as an array of points.
  6878. *
  6879. * @param grid a two-input function(x, y) that returns true for values
  6880. * inside the contour and false for values outside the contour.
  6881. * @param start an optional starting point [x, y] on the grid.
  6882. * @returns polygon [[x1, y1], [x2, y2], …]
  6883. */
  6884. d3.geom.contour = function(grid, start) {
  6885. var s = start || d3_geom_contourStart(grid), // starting point
  6886. c = [], // contour polygon
  6887. x = s[0], // current x position
  6888. y = s[1], // current y position
  6889. dx = 0, // next x direction
  6890. dy = 0, // next y direction
  6891. pdx = NaN, // previous x direction
  6892. pdy = NaN, // previous y direction
  6893. i = 0;
  6894. do {
  6895. // determine marching squares index
  6896. i = 0;
  6897. if (grid(x-1, y-1)) i += 1;
  6898. if (grid(x, y-1)) i += 2;
  6899. if (grid(x-1, y )) i += 4;
  6900. if (grid(x, y )) i += 8;
  6901. // determine next direction
  6902. if (i === 6) {
  6903. dx = pdy === -1 ? -1 : 1;
  6904. dy = 0;
  6905. } else if (i === 9) {
  6906. dx = 0;
  6907. dy = pdx === 1 ? -1 : 1;
  6908. } else {
  6909. dx = d3_geom_contourDx[i];
  6910. dy = d3_geom_contourDy[i];
  6911. }
  6912. // update contour polygon
  6913. if (dx != pdx && dy != pdy) {
  6914. c.push([x, y]);
  6915. pdx = dx;
  6916. pdy = dy;
  6917. }
  6918. x += dx;
  6919. y += dy;
  6920. } while (s[0] != x || s[1] != y);
  6921. return c;
  6922. };
  6923. // lookup tables for marching directions
  6924. var d3_geom_contourDx = [1, 0, 1, 1,-1, 0,-1, 1,0, 0,0,0,-1, 0,-1,NaN],
  6925. d3_geom_contourDy = [0,-1, 0, 0, 0,-1, 0, 0,1,-1,1,1, 0,-1, 0,NaN];
  6926. function d3_geom_contourStart(grid) {
  6927. var x = 0,
  6928. y = 0;
  6929. // search for a starting point; begin at origin
  6930. // and proceed along outward-expanding diagonals
  6931. while (true) {
  6932. if (grid(x,y)) {
  6933. return [x,y];
  6934. }
  6935. if (x === 0) {
  6936. x = y + 1;
  6937. y = 0;
  6938. } else {
  6939. x = x - 1;
  6940. y = y + 1;
  6941. }
  6942. }
  6943. }
  6944. /**
  6945. * Computes the 2D convex hull of a set of points using Graham's scanning
  6946. * algorithm. The algorithm has been implemented as described in Cormen,
  6947. * Leiserson, and Rivest's Introduction to Algorithms. The running time of
  6948. * this algorithm is O(n log n), where n is the number of input points.
  6949. *
  6950. * @param vertices [[x1, y1], [x2, y2], …]
  6951. * @returns polygon [[x1, y1], [x2, y2], …]
  6952. */
  6953. d3.geom.hull = function(vertices) {
  6954. if (vertices.length < 3) return [];
  6955. var len = vertices.length,
  6956. plen = len - 1,
  6957. points = [],
  6958. stack = [],
  6959. i, j, h = 0, x1, y1, x2, y2, u, v, a, sp;
  6960. // find the starting ref point: leftmost point with the minimum y coord
  6961. for (i=1; i<len; ++i) {
  6962. if (vertices[i][1] < vertices[h][1]) {
  6963. h = i;
  6964. } else if (vertices[i][1] == vertices[h][1]) {
  6965. h = (vertices[i][0] < vertices[h][0] ? i : h);
  6966. }
  6967. }
  6968. // calculate polar angles from ref point and sort
  6969. for (i=0; i<len; ++i) {
  6970. if (i === h) continue;
  6971. y1 = vertices[i][1] - vertices[h][1];
  6972. x1 = vertices[i][0] - vertices[h][0];
  6973. points.push({angle: Math.atan2(y1, x1), index: i});
  6974. }
  6975. points.sort(function(a, b) { return a.angle - b.angle; });
  6976. // toss out duplicate angles
  6977. a = points[0].angle;
  6978. v = points[0].index;
  6979. u = 0;
  6980. for (i=1; i<plen; ++i) {
  6981. j = points[i].index;
  6982. if (a == points[i].angle) {
  6983. // keep angle for point most distant from the reference
  6984. x1 = vertices[v][0] - vertices[h][0];
  6985. y1 = vertices[v][1] - vertices[h][1];
  6986. x2 = vertices[j][0] - vertices[h][0];
  6987. y2 = vertices[j][1] - vertices[h][1];
  6988. if ((x1*x1 + y1*y1) >= (x2*x2 + y2*y2)) {
  6989. points[i].index = -1;
  6990. } else {
  6991. points[u].index = -1;
  6992. a = points[i].angle;
  6993. u = i;
  6994. v = j;
  6995. }
  6996. } else {
  6997. a = points[i].angle;
  6998. u = i;
  6999. v = j;
  7000. }
  7001. }
  7002. // initialize the stack
  7003. stack.push(h);
  7004. for (i=0, j=0; i<2; ++j) {
  7005. if (points[j].index !== -1) {
  7006. stack.push(points[j].index);
  7007. i++;
  7008. }
  7009. }
  7010. sp = stack.length;
  7011. // do graham's scan
  7012. for (; j<plen; ++j) {
  7013. if (points[j].index === -1) continue; // skip tossed out points
  7014. while (!d3_geom_hullCCW(stack[sp-2], stack[sp-1], points[j].index, vertices)) {
  7015. --sp;
  7016. }
  7017. stack[sp++] = points[j].index;
  7018. }
  7019. // construct the hull
  7020. var poly = [];
  7021. for (i=0; i<sp; ++i) {
  7022. poly.push(vertices[stack[i]]);
  7023. }
  7024. return poly;
  7025. }
  7026. // are three points in counter-clockwise order?
  7027. function d3_geom_hullCCW(i1, i2, i3, v) {
  7028. var t, a, b, c, d, e, f;
  7029. t = v[i1]; a = t[0]; b = t[1];
  7030. t = v[i2]; c = t[0]; d = t[1];
  7031. t = v[i3]; e = t[0]; f = t[1];
  7032. return ((f-b)*(c-a) - (d-b)*(e-a)) > 0;
  7033. }
  7034. // Note: requires coordinates to be counterclockwise and convex!
  7035. d3.geom.polygon = function(coordinates) {
  7036. coordinates.area = function() {
  7037. var i = 0,
  7038. n = coordinates.length,
  7039. a = coordinates[n - 1][0] * coordinates[0][1],
  7040. b = coordinates[n - 1][1] * coordinates[0][0];
  7041. while (++i < n) {
  7042. a += coordinates[i - 1][0] * coordinates[i][1];
  7043. b += coordinates[i - 1][1] * coordinates[i][0];
  7044. }
  7045. return (b - a) * .5;
  7046. };
  7047. coordinates.centroid = function(k) {
  7048. var i = -1,
  7049. n = coordinates.length,
  7050. x = 0,
  7051. y = 0,
  7052. a,
  7053. b = coordinates[n - 1],
  7054. c;
  7055. if (!arguments.length) k = -1 / (6 * coordinates.area());
  7056. while (++i < n) {
  7057. a = b;
  7058. b = coordinates[i];
  7059. c = a[0] * b[1] - b[0] * a[1];
  7060. x += (a[0] + b[0]) * c;
  7061. y += (a[1] + b[1]) * c;
  7062. }
  7063. return [x * k, y * k];
  7064. };
  7065. // The Sutherland-Hodgman clipping algorithm.
  7066. coordinates.clip = function(subject) {
  7067. var input,
  7068. i = -1,
  7069. n = coordinates.length,
  7070. j,
  7071. m,
  7072. a = coordinates[n - 1],
  7073. b,
  7074. c,
  7075. d;
  7076. while (++i < n) {
  7077. input = subject.slice();
  7078. subject.length = 0;
  7079. b = coordinates[i];
  7080. c = input[(m = input.length) - 1];
  7081. j = -1;
  7082. while (++j < m) {
  7083. d = input[j];
  7084. if (d3_geom_polygonInside(d, a, b)) {
  7085. if (!d3_geom_polygonInside(c, a, b)) {
  7086. subject.push(d3_geom_polygonIntersect(c, d, a, b));
  7087. }
  7088. subject.push(d);
  7089. } else if (d3_geom_polygonInside(c, a, b)) {
  7090. subject.push(d3_geom_polygonIntersect(c, d, a, b));
  7091. }
  7092. c = d;
  7093. }
  7094. a = b;
  7095. }
  7096. return subject;
  7097. };
  7098. return coordinates;
  7099. };
  7100. function d3_geom_polygonInside(p, a, b) {
  7101. return (b[0] - a[0]) * (p[1] - a[1]) < (b[1] - a[1]) * (p[0] - a[0]);
  7102. }
  7103. // Intersect two infinite lines cd and ab.
  7104. function d3_geom_polygonIntersect(c, d, a, b) {
  7105. var x1 = c[0], x2 = d[0], x3 = a[0], x4 = b[0],
  7106. y1 = c[1], y2 = d[1], y3 = a[1], y4 = b[1],
  7107. x13 = x1 - x3,
  7108. x21 = x2 - x1,
  7109. x43 = x4 - x3,
  7110. y13 = y1 - y3,
  7111. y21 = y2 - y1,
  7112. y43 = y4 - y3,
  7113. ua = (x43 * y13 - y43 * x13) / (y43 * x21 - x43 * y21);
  7114. return [x1 + ua * x21, y1 + ua * y21];
  7115. }
  7116. // Adapted from Nicolas Garcia Belmonte's JIT implementation:
  7117. // http://blog.thejit.org/2010/02/12/voronoi-tessellation/
  7118. // http://blog.thejit.org/assets/voronoijs/voronoi.js
  7119. // See lib/jit/LICENSE for details.
  7120. // Notes:
  7121. //
  7122. // This implementation does not clip the returned polygons, so if you want to
  7123. // clip them to a particular shape you will need to do that either in SVG or by
  7124. // post-processing with d3.geom.polygon's clip method.
  7125. //
  7126. // If any vertices are coincident or have NaN positions, the behavior of this
  7127. // method is undefined. Most likely invalid polygons will be returned. You
  7128. // should filter invalid points, and consolidate coincident points, before
  7129. // computing the tessellation.
  7130. /**
  7131. * @param vertices [[x1, y1], [x2, y2], …]
  7132. * @returns polygons [[[x1, y1], [x2, y2], …], …]
  7133. */
  7134. d3.geom.voronoi = function(vertices) {
  7135. var polygons = vertices.map(function() { return []; });
  7136. d3_voronoi_tessellate(vertices, function(e) {
  7137. var s1,
  7138. s2,
  7139. x1,
  7140. x2,
  7141. y1,
  7142. y2;
  7143. if (e.a === 1 && e.b >= 0) {
  7144. s1 = e.ep.r;
  7145. s2 = e.ep.l;
  7146. } else {
  7147. s1 = e.ep.l;
  7148. s2 = e.ep.r;
  7149. }
  7150. if (e.a === 1) {
  7151. y1 = s1 ? s1.y : -1e6;
  7152. x1 = e.c - e.b * y1;
  7153. y2 = s2 ? s2.y : 1e6;
  7154. x2 = e.c - e.b * y2;
  7155. } else {
  7156. x1 = s1 ? s1.x : -1e6;
  7157. y1 = e.c - e.a * x1;
  7158. x2 = s2 ? s2.x : 1e6;
  7159. y2 = e.c - e.a * x2;
  7160. }
  7161. var v1 = [x1, y1],
  7162. v2 = [x2, y2];
  7163. polygons[e.region.l.index].push(v1, v2);
  7164. polygons[e.region.r.index].push(v1, v2);
  7165. });
  7166. // Reconnect the polygon segments into counterclockwise loops.
  7167. return polygons.map(function(polygon, i) {
  7168. var cx = vertices[i][0],
  7169. cy = vertices[i][1];
  7170. polygon.forEach(function(v) {
  7171. v.angle = Math.atan2(v[0] - cx, v[1] - cy);
  7172. });
  7173. return polygon.sort(function(a, b) {
  7174. return a.angle - b.angle;
  7175. }).filter(function(d, i) {
  7176. return !i || (d.angle - polygon[i - 1].angle > 1e-10);
  7177. });
  7178. });
  7179. };
  7180. var d3_voronoi_opposite = {"l": "r", "r": "l"};
  7181. function d3_voronoi_tessellate(vertices, callback) {
  7182. var Sites = {
  7183. list: vertices
  7184. .map(function(v, i) {
  7185. return {
  7186. index: i,
  7187. x: v[0],
  7188. y: v[1]
  7189. };
  7190. })
  7191. .sort(function(a, b) {
  7192. return a.y < b.y ? -1
  7193. : a.y > b.y ? 1
  7194. : a.x < b.x ? -1
  7195. : a.x > b.x ? 1
  7196. : 0;
  7197. }),
  7198. bottomSite: null
  7199. };
  7200. var EdgeList = {
  7201. list: [],
  7202. leftEnd: null,
  7203. rightEnd: null,
  7204. init: function() {
  7205. EdgeList.leftEnd = EdgeList.createHalfEdge(null, "l");
  7206. EdgeList.rightEnd = EdgeList.createHalfEdge(null, "l");
  7207. EdgeList.leftEnd.r = EdgeList.rightEnd;
  7208. EdgeList.rightEnd.l = EdgeList.leftEnd;
  7209. EdgeList.list.unshift(EdgeList.leftEnd, EdgeList.rightEnd);
  7210. },
  7211. createHalfEdge: function(edge, side) {
  7212. return {
  7213. edge: edge,
  7214. side: side,
  7215. vertex: null,
  7216. "l": null,
  7217. "r": null
  7218. };
  7219. },
  7220. insert: function(lb, he) {
  7221. he.l = lb;
  7222. he.r = lb.r;
  7223. lb.r.l = he;
  7224. lb.r = he;
  7225. },
  7226. leftBound: function(p) {
  7227. var he = EdgeList.leftEnd;
  7228. do {
  7229. he = he.r;
  7230. } while (he != EdgeList.rightEnd && Geom.rightOf(he, p));
  7231. he = he.l;
  7232. return he;
  7233. },
  7234. del: function(he) {
  7235. he.l.r = he.r;
  7236. he.r.l = he.l;
  7237. he.edge = null;
  7238. },
  7239. right: function(he) {
  7240. return he.r;
  7241. },
  7242. left: function(he) {
  7243. return he.l;
  7244. },
  7245. leftRegion: function(he) {
  7246. return he.edge == null
  7247. ? Sites.bottomSite
  7248. : he.edge.region[he.side];
  7249. },
  7250. rightRegion: function(he) {
  7251. return he.edge == null
  7252. ? Sites.bottomSite
  7253. : he.edge.region[d3_voronoi_opposite[he.side]];
  7254. }
  7255. };
  7256. var Geom = {
  7257. bisect: function(s1, s2) {
  7258. var newEdge = {
  7259. region: {"l": s1, "r": s2},
  7260. ep: {"l": null, "r": null}
  7261. };
  7262. var dx = s2.x - s1.x,
  7263. dy = s2.y - s1.y,
  7264. adx = dx > 0 ? dx : -dx,
  7265. ady = dy > 0 ? dy : -dy;
  7266. newEdge.c = s1.x * dx + s1.y * dy
  7267. + (dx * dx + dy * dy) * .5;
  7268. if (adx > ady) {
  7269. newEdge.a = 1;
  7270. newEdge.b = dy / dx;
  7271. newEdge.c /= dx;
  7272. } else {
  7273. newEdge.b = 1;
  7274. newEdge.a = dx / dy;
  7275. newEdge.c /= dy;
  7276. }
  7277. return newEdge;
  7278. },
  7279. intersect: function(el1, el2) {
  7280. var e1 = el1.edge,
  7281. e2 = el2.edge;
  7282. if (!e1 || !e2 || (e1.region.r == e2.region.r)) {
  7283. return null;
  7284. }
  7285. var d = (e1.a * e2.b) - (e1.b * e2.a);
  7286. if (Math.abs(d) < 1e-10) {
  7287. return null;
  7288. }
  7289. var xint = (e1.c * e2.b - e2.c * e1.b) / d,
  7290. yint = (e2.c * e1.a - e1.c * e2.a) / d,
  7291. e1r = e1.region.r,
  7292. e2r = e2.region.r,
  7293. el,
  7294. e;
  7295. if ((e1r.y < e2r.y) ||
  7296. (e1r.y == e2r.y && e1r.x < e2r.x)) {
  7297. el = el1;
  7298. e = e1;
  7299. } else {
  7300. el = el2;
  7301. e = e2;
  7302. }
  7303. var rightOfSite = (xint >= e.region.r.x);
  7304. if ((rightOfSite && (el.side === "l")) ||
  7305. (!rightOfSite && (el.side === "r"))) {
  7306. return null;
  7307. }
  7308. return {
  7309. x: xint,
  7310. y: yint
  7311. };
  7312. },
  7313. rightOf: function(he, p) {
  7314. var e = he.edge,
  7315. topsite = e.region.r,
  7316. rightOfSite = (p.x > topsite.x);
  7317. if (rightOfSite && (he.side === "l")) {
  7318. return 1;
  7319. }
  7320. if (!rightOfSite && (he.side === "r")) {
  7321. return 0;
  7322. }
  7323. if (e.a === 1) {
  7324. var dyp = p.y - topsite.y,
  7325. dxp = p.x - topsite.x,
  7326. fast = 0,
  7327. above = 0;
  7328. if ((!rightOfSite && (e.b < 0)) ||
  7329. (rightOfSite && (e.b >= 0))) {
  7330. above = fast = (dyp >= e.b * dxp);
  7331. } else {
  7332. above = ((p.x + p.y * e.b) > e.c);
  7333. if (e.b < 0) {
  7334. above = !above;
  7335. }
  7336. if (!above) {
  7337. fast = 1;
  7338. }
  7339. }
  7340. if (!fast) {
  7341. var dxs = topsite.x - e.region.l.x;
  7342. above = (e.b * (dxp * dxp - dyp * dyp)) <
  7343. (dxs * dyp * (1 + 2 * dxp / dxs + e.b * e.b));
  7344. if (e.b < 0) {
  7345. above = !above;
  7346. }
  7347. }
  7348. } else /* e.b == 1 */ {
  7349. var yl = e.c - e.a * p.x,
  7350. t1 = p.y - yl,
  7351. t2 = p.x - topsite.x,
  7352. t3 = yl - topsite.y;
  7353. above = (t1 * t1) > (t2 * t2 + t3 * t3);
  7354. }
  7355. return he.side === "l" ? above : !above;
  7356. },
  7357. endPoint: function(edge, side, site) {
  7358. edge.ep[side] = site;
  7359. if (!edge.ep[d3_voronoi_opposite[side]]) return;
  7360. callback(edge);
  7361. },
  7362. distance: function(s, t) {
  7363. var dx = s.x - t.x,
  7364. dy = s.y - t.y;
  7365. return Math.sqrt(dx * dx + dy * dy);
  7366. }
  7367. };
  7368. var EventQueue = {
  7369. list: [],
  7370. insert: function(he, site, offset) {
  7371. he.vertex = site;
  7372. he.ystar = site.y + offset;
  7373. for (var i=0, list=EventQueue.list, l=list.length; i<l; i++) {
  7374. var next = list[i];
  7375. if (he.ystar > next.ystar ||
  7376. (he.ystar == next.ystar &&
  7377. site.x > next.vertex.x)) {
  7378. continue;
  7379. } else {
  7380. break;
  7381. }
  7382. }
  7383. list.splice(i, 0, he);
  7384. },
  7385. del: function(he) {
  7386. for (var i=0, ls=EventQueue.list, l=ls.length; i<l && (ls[i] != he); ++i) {}
  7387. ls.splice(i, 1);
  7388. },
  7389. empty: function() { return EventQueue.list.length === 0; },
  7390. nextEvent: function(he) {
  7391. for (var i=0, ls=EventQueue.list, l=ls.length; i<l; ++i) {
  7392. if (ls[i] == he) return ls[i+1];
  7393. }
  7394. return null;
  7395. },
  7396. min: function() {
  7397. var elem = EventQueue.list[0];
  7398. return {
  7399. x: elem.vertex.x,
  7400. y: elem.ystar
  7401. };
  7402. },
  7403. extractMin: function() {
  7404. return EventQueue.list.shift();
  7405. }
  7406. };
  7407. EdgeList.init();
  7408. Sites.bottomSite = Sites.list.shift();
  7409. var newSite = Sites.list.shift(), newIntStar;
  7410. var lbnd, rbnd, llbnd, rrbnd, bisector;
  7411. var bot, top, temp, p, v;
  7412. var e, pm;
  7413. while (true) {
  7414. if (!EventQueue.empty()) {
  7415. newIntStar = EventQueue.min();
  7416. }
  7417. if (newSite && (EventQueue.empty()
  7418. || newSite.y < newIntStar.y
  7419. || (newSite.y == newIntStar.y
  7420. && newSite.x < newIntStar.x))) { //new site is smallest
  7421. lbnd = EdgeList.leftBound(newSite);
  7422. rbnd = EdgeList.right(lbnd);
  7423. bot = EdgeList.rightRegion(lbnd);
  7424. e = Geom.bisect(bot, newSite);
  7425. bisector = EdgeList.createHalfEdge(e, "l");
  7426. EdgeList.insert(lbnd, bisector);
  7427. p = Geom.intersect(lbnd, bisector);
  7428. if (p) {
  7429. EventQueue.del(lbnd);
  7430. EventQueue.insert(lbnd, p, Geom.distance(p, newSite));
  7431. }
  7432. lbnd = bisector;
  7433. bisector = EdgeList.createHalfEdge(e, "r");
  7434. EdgeList.insert(lbnd, bisector);
  7435. p = Geom.intersect(bisector, rbnd);
  7436. if (p) {
  7437. EventQueue.insert(bisector, p, Geom.distance(p, newSite));
  7438. }
  7439. newSite = Sites.list.shift();
  7440. } else if (!EventQueue.empty()) { //intersection is smallest
  7441. lbnd = EventQueue.extractMin();
  7442. llbnd = EdgeList.left(lbnd);
  7443. rbnd = EdgeList.right(lbnd);
  7444. rrbnd = EdgeList.right(rbnd);
  7445. bot = EdgeList.leftRegion(lbnd);
  7446. top = EdgeList.rightRegion(rbnd);
  7447. v = lbnd.vertex;
  7448. Geom.endPoint(lbnd.edge, lbnd.side, v);
  7449. Geom.endPoint(rbnd.edge, rbnd.side, v);
  7450. EdgeList.del(lbnd);
  7451. EventQueue.del(rbnd);
  7452. EdgeList.del(rbnd);
  7453. pm = "l";
  7454. if (bot.y > top.y) {
  7455. temp = bot;
  7456. bot = top;
  7457. top = temp;
  7458. pm = "r";
  7459. }
  7460. e = Geom.bisect(bot, top);
  7461. bisector = EdgeList.createHalfEdge(e, pm);
  7462. EdgeList.insert(llbnd, bisector);
  7463. Geom.endPoint(e, d3_voronoi_opposite[pm], v);
  7464. p = Geom.intersect(llbnd, bisector);
  7465. if (p) {
  7466. EventQueue.del(llbnd);
  7467. EventQueue.insert(llbnd, p, Geom.distance(p, bot));
  7468. }
  7469. p = Geom.intersect(bisector, rrbnd);
  7470. if (p) {
  7471. EventQueue.insert(bisector, p, Geom.distance(p, bot));
  7472. }
  7473. } else {
  7474. break;
  7475. }
  7476. }//end while
  7477. for (lbnd = EdgeList.right(EdgeList.leftEnd);
  7478. lbnd != EdgeList.rightEnd;
  7479. lbnd = EdgeList.right(lbnd)) {
  7480. callback(lbnd.edge);
  7481. }
  7482. }
  7483. /**
  7484. * @param vertices [[x1, y1], [x2, y2], …]
  7485. * @returns triangles [[[x1, y1], [x2, y2], [x3, y3]], …]
  7486. */
  7487. d3.geom.delaunay = function(vertices) {
  7488. var edges = vertices.map(function() { return []; }),
  7489. triangles = [];
  7490. // Use the Voronoi tessellation to determine Delaunay edges.
  7491. d3_voronoi_tessellate(vertices, function(e) {
  7492. edges[e.region.l.index].push(vertices[e.region.r.index]);
  7493. });
  7494. // Reconnect the edges into counterclockwise triangles.
  7495. edges.forEach(function(edge, i) {
  7496. var v = vertices[i],
  7497. cx = v[0],
  7498. cy = v[1];
  7499. edge.forEach(function(v) {
  7500. v.angle = Math.atan2(v[0] - cx, v[1] - cy);
  7501. });
  7502. edge.sort(function(a, b) {
  7503. return a.angle - b.angle;
  7504. });
  7505. for (var j = 0, m = edge.length - 1; j < m; j++) {
  7506. triangles.push([v, edge[j], edge[j + 1]]);
  7507. }
  7508. });
  7509. return triangles;
  7510. };
  7511. // Constructs a new quadtree for the specified array of points. A quadtree is a
  7512. // two-dimensional recursive spatial subdivision. This implementation uses
  7513. // square partitions, dividing each square into four equally-sized squares. Each
  7514. // point exists in a unique node; if multiple points are in the same position,
  7515. // some points may be stored on internal nodes rather than leaf nodes. Quadtrees
  7516. // can be used to accelerate various spatial operations, such as the Barnes-Hut
  7517. // approximation for computing n-body forces, or collision detection.
  7518. d3.geom.quadtree = function(points, x1, y1, x2, y2) {
  7519. var p,
  7520. i = -1,
  7521. n = points.length;
  7522. // Type conversion for deprecated API.
  7523. if (n && isNaN(points[0].x)) points = points.map(d3_geom_quadtreePoint);
  7524. // Allow bounds to be specified explicitly.
  7525. if (arguments.length < 5) {
  7526. if (arguments.length === 3) {
  7527. y2 = x2 = y1;
  7528. y1 = x1;
  7529. } else {
  7530. x1 = y1 = Infinity;
  7531. x2 = y2 = -Infinity;
  7532. // Compute bounds.
  7533. while (++i < n) {
  7534. p = points[i];
  7535. if (p.x < x1) x1 = p.x;
  7536. if (p.y < y1) y1 = p.y;
  7537. if (p.x > x2) x2 = p.x;
  7538. if (p.y > y2) y2 = p.y;
  7539. }
  7540. // Squarify the bounds.
  7541. var dx = x2 - x1,
  7542. dy = y2 - y1;
  7543. if (dx > dy) y2 = y1 + dx;
  7544. else x2 = x1 + dy;
  7545. }
  7546. }
  7547. // Recursively inserts the specified point p at the node n or one of its
  7548. // descendants. The bounds are defined by [x1, x2] and [y1, y2].
  7549. function insert(n, p, x1, y1, x2, y2) {
  7550. if (isNaN(p.x) || isNaN(p.y)) return; // ignore invalid points
  7551. if (n.leaf) {
  7552. var v = n.point;
  7553. if (v) {
  7554. // If the point at this leaf node is at the same position as the new
  7555. // point we are adding, we leave the point associated with the
  7556. // internal node while adding the new point to a child node. This
  7557. // avoids infinite recursion.
  7558. if ((Math.abs(v.x - p.x) + Math.abs(v.y - p.y)) < .01) {
  7559. insertChild(n, p, x1, y1, x2, y2);
  7560. } else {
  7561. n.point = null;
  7562. insertChild(n, v, x1, y1, x2, y2);
  7563. insertChild(n, p, x1, y1, x2, y2);
  7564. }
  7565. } else {
  7566. n.point = p;
  7567. }
  7568. } else {
  7569. insertChild(n, p, x1, y1, x2, y2);
  7570. }
  7571. }
  7572. // Recursively inserts the specified point p into a descendant of node n. The
  7573. // bounds are defined by [x1, x2] and [y1, y2].
  7574. function insertChild(n, p, x1, y1, x2, y2) {
  7575. // Compute the split point, and the quadrant in which to insert p.
  7576. var sx = (x1 + x2) * .5,
  7577. sy = (y1 + y2) * .5,
  7578. right = p.x >= sx,
  7579. bottom = p.y >= sy,
  7580. i = (bottom << 1) + right;
  7581. // Recursively insert into the child node.
  7582. n.leaf = false;
  7583. n = n.nodes[i] || (n.nodes[i] = d3_geom_quadtreeNode());
  7584. // Update the bounds as we recurse.
  7585. if (right) x1 = sx; else x2 = sx;
  7586. if (bottom) y1 = sy; else y2 = sy;
  7587. insert(n, p, x1, y1, x2, y2);
  7588. }
  7589. // Create the root node.
  7590. var root = d3_geom_quadtreeNode();
  7591. root.add = function(p) {
  7592. insert(root, p, x1, y1, x2, y2);
  7593. };
  7594. root.visit = function(f) {
  7595. d3_geom_quadtreeVisit(f, root, x1, y1, x2, y2);
  7596. };
  7597. // Insert all points.
  7598. points.forEach(root.add);
  7599. return root;
  7600. };
  7601. function d3_geom_quadtreeNode() {
  7602. return {
  7603. leaf: true,
  7604. nodes: [],
  7605. point: null
  7606. };
  7607. }
  7608. function d3_geom_quadtreeVisit(f, node, x1, y1, x2, y2) {
  7609. if (!f(node, x1, y1, x2, y2)) {
  7610. var sx = (x1 + x2) * .5,
  7611. sy = (y1 + y2) * .5,
  7612. children = node.nodes;
  7613. if (children[0]) d3_geom_quadtreeVisit(f, children[0], x1, y1, sx, sy);
  7614. if (children[1]) d3_geom_quadtreeVisit(f, children[1], sx, y1, x2, sy);
  7615. if (children[2]) d3_geom_quadtreeVisit(f, children[2], x1, sy, sx, y2);
  7616. if (children[3]) d3_geom_quadtreeVisit(f, children[3], sx, sy, x2, y2);
  7617. }
  7618. }
  7619. function d3_geom_quadtreePoint(p) {
  7620. return {
  7621. x: p[0],
  7622. y: p[1]
  7623. };
  7624. }
  7625. d3.time = {};
  7626. var d3_time = Date;
  7627. function d3_time_utc() {
  7628. this._ = new Date(arguments.length > 1
  7629. ? Date.UTC.apply(this, arguments)
  7630. : arguments[0]);
  7631. }
  7632. d3_time_utc.prototype = {
  7633. getDate: function() { return this._.getUTCDate(); },
  7634. getDay: function() { return this._.getUTCDay(); },
  7635. getFullYear: function() { return this._.getUTCFullYear(); },
  7636. getHours: function() { return this._.getUTCHours(); },
  7637. getMilliseconds: function() { return this._.getUTCMilliseconds(); },
  7638. getMinutes: function() { return this._.getUTCMinutes(); },
  7639. getMonth: function() { return this._.getUTCMonth(); },
  7640. getSeconds: function() { return this._.getUTCSeconds(); },
  7641. getTime: function() { return this._.getTime(); },
  7642. getTimezoneOffset: function() { return 0; },
  7643. valueOf: function() { return this._.valueOf(); },
  7644. setDate: function() { d3_time_prototype.setUTCDate.apply(this._, arguments); },
  7645. setDay: function() { d3_time_prototype.setUTCDay.apply(this._, arguments); },
  7646. setFullYear: function() { d3_time_prototype.setUTCFullYear.apply(this._, arguments); },
  7647. setHours: function() { d3_time_prototype.setUTCHours.apply(this._, arguments); },
  7648. setMilliseconds: function() { d3_time_prototype.setUTCMilliseconds.apply(this._, arguments); },
  7649. setMinutes: function() { d3_time_prototype.setUTCMinutes.apply(this._, arguments); },
  7650. setMonth: function() { d3_time_prototype.setUTCMonth.apply(this._, arguments); },
  7651. setSeconds: function() { d3_time_prototype.setUTCSeconds.apply(this._, arguments); },
  7652. setTime: function() { d3_time_prototype.setTime.apply(this._, arguments); }
  7653. };
  7654. var d3_time_prototype = Date.prototype;
  7655. d3.time.format = function(template) {
  7656. var n = template.length;
  7657. function format(date) {
  7658. var string = [],
  7659. i = -1,
  7660. j = 0,
  7661. c,
  7662. f;
  7663. while (++i < n) {
  7664. if (template.charCodeAt(i) == 37) {
  7665. string.push(
  7666. template.substring(j, i),
  7667. (f = d3_time_formats[c = template.charAt(++i)])
  7668. ? f(date) : c);
  7669. j = i + 1;
  7670. }
  7671. }
  7672. string.push(template.substring(j, i));
  7673. return string.join("");
  7674. }
  7675. format.parse = function(string) {
  7676. var d = {y: 1900, m: 0, d: 1, H: 0, M: 0, S: 0, L: 0},
  7677. i = d3_time_parse(d, template, string, 0);
  7678. if (i != string.length) return null;
  7679. // The am-pm flag is 0 for AM, and 1 for PM.
  7680. if ("p" in d) d.H = d.H % 12 + d.p * 12;
  7681. var date = new d3_time();
  7682. date.setFullYear(d.y, d.m, d.d);
  7683. date.setHours(d.H, d.M, d.S, d.L);
  7684. return date;
  7685. };
  7686. format.toString = function() {
  7687. return template;
  7688. };
  7689. return format;
  7690. };
  7691. function d3_time_parse(date, template, string, j) {
  7692. var c,
  7693. p,
  7694. i = 0,
  7695. n = template.length,
  7696. m = string.length;
  7697. while (i < n) {
  7698. if (j >= m) return -1;
  7699. c = template.charCodeAt(i++);
  7700. if (c == 37) {
  7701. p = d3_time_parsers[template.charAt(i++)];
  7702. if (!p || ((j = p(date, string, j)) < 0)) return -1;
  7703. } else if (c != string.charCodeAt(j++)) {
  7704. return -1;
  7705. }
  7706. }
  7707. return j;
  7708. }
  7709. var d3_time_zfill2 = d3.format("02d"),
  7710. d3_time_zfill3 = d3.format("03d"),
  7711. d3_time_zfill4 = d3.format("04d"),
  7712. d3_time_sfill2 = d3.format("2d");
  7713. var d3_time_formats = {
  7714. a: function(d) { return d3_time_weekdays[d.getDay()].substring(0, 3); },
  7715. A: function(d) { return d3_time_weekdays[d.getDay()]; },
  7716. b: function(d) { return d3_time_months[d.getMonth()].substring(0, 3); },
  7717. B: function(d) { return d3_time_months[d.getMonth()]; },
  7718. c: d3.time.format("%a %b %e %H:%M:%S %Y"),
  7719. d: function(d) { return d3_time_zfill2(d.getDate()); },
  7720. e: function(d) { return d3_time_sfill2(d.getDate()); },
  7721. H: function(d) { return d3_time_zfill2(d.getHours()); },
  7722. I: function(d) { return d3_time_zfill2(d.getHours() % 12 || 12); },
  7723. j: function(d) { return d3_time_zfill3(1 + d3.time.dayOfYear(d)); },
  7724. L: function(d) { return d3_time_zfill3(d.getMilliseconds()); },
  7725. m: function(d) { return d3_time_zfill2(d.getMonth() + 1); },
  7726. M: function(d) { return d3_time_zfill2(d.getMinutes()); },
  7727. p: function(d) { return d.getHours() >= 12 ? "PM" : "AM"; },
  7728. S: function(d) { return d3_time_zfill2(d.getSeconds()); },
  7729. U: function(d) { return d3_time_zfill2(d3.time.sundayOfYear(d)); },
  7730. w: function(d) { return d.getDay(); },
  7731. W: function(d) { return d3_time_zfill2(d3.time.mondayOfYear(d)); },
  7732. x: d3.time.format("%m/%d/%y"),
  7733. X: d3.time.format("%H:%M:%S"),
  7734. y: function(d) { return d3_time_zfill2(d.getFullYear() % 100); },
  7735. Y: function(d) { return d3_time_zfill4(d.getFullYear() % 10000); },
  7736. Z: d3_time_zone,
  7737. "%": function(d) { return "%"; }
  7738. };
  7739. var d3_time_parsers = {
  7740. a: d3_time_parseWeekdayAbbrev,
  7741. A: d3_time_parseWeekday,
  7742. b: d3_time_parseMonthAbbrev,
  7743. B: d3_time_parseMonth,
  7744. c: d3_time_parseLocaleFull,
  7745. d: d3_time_parseDay,
  7746. e: d3_time_parseDay,
  7747. H: d3_time_parseHour24,
  7748. I: d3_time_parseHour24,
  7749. // j: function(d, s, i) { /*TODO day of year [001,366] */ return i; },
  7750. L: d3_time_parseMilliseconds,
  7751. m: d3_time_parseMonthNumber,
  7752. M: d3_time_parseMinutes,
  7753. p: d3_time_parseAmPm,
  7754. S: d3_time_parseSeconds,
  7755. // U: function(d, s, i) { /*TODO week number (sunday) [00,53] */ return i; },
  7756. // w: function(d, s, i) { /*TODO weekday [0,6] */ return i; },
  7757. // W: function(d, s, i) { /*TODO week number (monday) [00,53] */ return i; },
  7758. x: d3_time_parseLocaleDate,
  7759. X: d3_time_parseLocaleTime,
  7760. y: d3_time_parseYear,
  7761. Y: d3_time_parseFullYear
  7762. // ,
  7763. // Z: function(d, s, i) { /*TODO time zone */ return i; },
  7764. // "%": function(d, s, i) { /*TODO literal % */ return i; }
  7765. };
  7766. // Note: weekday is validated, but does not set the date.
  7767. function d3_time_parseWeekdayAbbrev(date, string, i) {
  7768. return d3_time_weekdayAbbrevRe.test(string.substring(i, i += 3)) ? i : -1;
  7769. }
  7770. // Note: weekday is validated, but does not set the date.
  7771. function d3_time_parseWeekday(date, string, i) {
  7772. d3_time_weekdayRe.lastIndex = 0;
  7773. var n = d3_time_weekdayRe.exec(string.substring(i, i + 10));
  7774. return n ? i += n[0].length : -1;
  7775. }
  7776. var d3_time_weekdayAbbrevRe = /^(?:sun|mon|tue|wed|thu|fri|sat)/i,
  7777. d3_time_weekdayRe = /^(?:Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday)/i;
  7778. d3_time_weekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  7779. function d3_time_parseMonthAbbrev(date, string, i) {
  7780. var n = d3_time_monthAbbrevLookup.get(string.substring(i, i += 3).toLowerCase());
  7781. return n == null ? -1 : (date.m = n, i);
  7782. }
  7783. var d3_time_monthAbbrevLookup = d3.map({
  7784. jan: 0,
  7785. feb: 1,
  7786. mar: 2,
  7787. apr: 3,
  7788. may: 4,
  7789. jun: 5,
  7790. jul: 6,
  7791. aug: 7,
  7792. sep: 8,
  7793. oct: 9,
  7794. nov: 10,
  7795. dec: 11
  7796. });
  7797. function d3_time_parseMonth(date, string, i) {
  7798. d3_time_monthRe.lastIndex = 0;
  7799. var n = d3_time_monthRe.exec(string.substring(i, i + 12));
  7800. return n ? (date.m = d3_time_monthLookup.get(n[0].toLowerCase()), i += n[0].length) : -1;
  7801. }
  7802. var d3_time_monthRe = /^(?:January|February|March|April|May|June|July|August|September|October|November|December)/ig;
  7803. var d3_time_monthLookup = d3.map({
  7804. january: 0,
  7805. february: 1,
  7806. march: 2,
  7807. april: 3,
  7808. may: 4,
  7809. june: 5,
  7810. july: 6,
  7811. august: 7,
  7812. september: 8,
  7813. october: 9,
  7814. november: 10,
  7815. december: 11
  7816. });
  7817. var d3_time_months = [
  7818. "January",
  7819. "February",
  7820. "March",
  7821. "April",
  7822. "May",
  7823. "June",
  7824. "July",
  7825. "August",
  7826. "September",
  7827. "October",
  7828. "November",
  7829. "December"
  7830. ];
  7831. function d3_time_parseLocaleFull(date, string, i) {
  7832. return d3_time_parse(date, d3_time_formats.c.toString(), string, i);
  7833. }
  7834. function d3_time_parseLocaleDate(date, string, i) {
  7835. return d3_time_parse(date, d3_time_formats.x.toString(), string, i);
  7836. }
  7837. function d3_time_parseLocaleTime(date, string, i) {
  7838. return d3_time_parse(date, d3_time_formats.X.toString(), string, i);
  7839. }
  7840. function d3_time_parseFullYear(date, string, i) {
  7841. d3_time_numberRe.lastIndex = 0;
  7842. var n = d3_time_numberRe.exec(string.substring(i, i + 4));
  7843. return n ? (date.y = +n[0], i += n[0].length) : -1;
  7844. }
  7845. function d3_time_parseYear(date, string, i) {
  7846. d3_time_numberRe.lastIndex = 0;
  7847. var n = d3_time_numberRe.exec(string.substring(i, i + 2));
  7848. return n ? (date.y = d3_time_century() + +n[0], i += n[0].length) : -1;
  7849. }
  7850. function d3_time_century() {
  7851. return ~~(new Date().getFullYear() / 1000) * 1000;
  7852. }
  7853. function d3_time_parseMonthNumber(date, string, i) {
  7854. d3_time_numberRe.lastIndex = 0;
  7855. var n = d3_time_numberRe.exec(string.substring(i, i + 2));
  7856. return n ? (date.m = n[0] - 1, i += n[0].length) : -1;
  7857. }
  7858. function d3_time_parseDay(date, string, i) {
  7859. d3_time_numberRe.lastIndex = 0;
  7860. var n = d3_time_numberRe.exec(string.substring(i, i + 2));
  7861. return n ? (date.d = +n[0], i += n[0].length) : -1;
  7862. }
  7863. // Note: we don't validate that the hour is in the range [0,23] or [1,12].
  7864. function d3_time_parseHour24(date, string, i) {
  7865. d3_time_numberRe.lastIndex = 0;
  7866. var n = d3_time_numberRe.exec(string.substring(i, i + 2));
  7867. return n ? (date.H = +n[0], i += n[0].length) : -1;
  7868. }
  7869. function d3_time_parseMinutes(date, string, i) {
  7870. d3_time_numberRe.lastIndex = 0;
  7871. var n = d3_time_numberRe.exec(string.substring(i, i + 2));
  7872. return n ? (date.M = +n[0], i += n[0].length) : -1;
  7873. }
  7874. function d3_time_parseSeconds(date, string, i) {
  7875. d3_time_numberRe.lastIndex = 0;
  7876. var n = d3_time_numberRe.exec(string.substring(i, i + 2));
  7877. return n ? (date.S = +n[0], i += n[0].length) : -1;
  7878. }
  7879. function d3_time_parseMilliseconds(date, string, i) {
  7880. d3_time_numberRe.lastIndex = 0;
  7881. var n = d3_time_numberRe.exec(string.substring(i, i + 3));
  7882. return n ? (date.L = +n[0], i += n[0].length) : -1;
  7883. }
  7884. // Note: we don't look at the next directive.
  7885. var d3_time_numberRe = /\s*\d+/;
  7886. function d3_time_parseAmPm(date, string, i) {
  7887. var n = d3_time_amPmLookup.get(string.substring(i, i += 2).toLowerCase());
  7888. return n == null ? -1 : (date.p = n, i);
  7889. }
  7890. var d3_time_amPmLookup = d3.map({
  7891. am: 0,
  7892. pm: 1
  7893. });
  7894. // TODO table of time zone offset names?
  7895. function d3_time_zone(d) {
  7896. var z = d.getTimezoneOffset(),
  7897. zs = z > 0 ? "-" : "+",
  7898. zh = ~~(Math.abs(z) / 60),
  7899. zm = Math.abs(z) % 60;
  7900. return zs + d3_time_zfill2(zh) + d3_time_zfill2(zm);
  7901. }
  7902. d3.time.format.utc = function(template) {
  7903. var local = d3.time.format(template);
  7904. function format(date) {
  7905. try {
  7906. d3_time = d3_time_utc;
  7907. var utc = new d3_time();
  7908. utc._ = date;
  7909. return local(utc);
  7910. } finally {
  7911. d3_time = Date;
  7912. }
  7913. }
  7914. format.parse = function(string) {
  7915. try {
  7916. d3_time = d3_time_utc;
  7917. var date = local.parse(string);
  7918. return date && date._;
  7919. } finally {
  7920. d3_time = Date;
  7921. }
  7922. };
  7923. format.toString = local.toString;
  7924. return format;
  7925. };
  7926. var d3_time_formatIso = d3.time.format.utc("%Y-%m-%dT%H:%M:%S.%LZ");
  7927. d3.time.format.iso = Date.prototype.toISOString ? d3_time_formatIsoNative : d3_time_formatIso;
  7928. function d3_time_formatIsoNative(date) {
  7929. return date.toISOString();
  7930. }
  7931. d3_time_formatIsoNative.parse = function(string) {
  7932. return new Date(string);
  7933. };
  7934. d3_time_formatIsoNative.toString = d3_time_formatIso.toString;
  7935. function d3_time_interval(local, step, number) {
  7936. function round(date) {
  7937. var d0 = local(date), d1 = offset(d0, 1);
  7938. return date - d0 < d1 - date ? d0 : d1;
  7939. }
  7940. function ceil(date) {
  7941. step(date = local(new d3_time(date - 1)), 1);
  7942. return date;
  7943. }
  7944. function offset(date, k) {
  7945. step(date = new d3_time(+date), k);
  7946. return date;
  7947. }
  7948. function range(t0, t1, dt) {
  7949. var time = ceil(t0), times = [];
  7950. if (dt > 1) {
  7951. while (time < t1) {
  7952. if (!(number(time) % dt)) times.push(new Date(+time));
  7953. step(time, 1);
  7954. }
  7955. } else {
  7956. while (time < t1) times.push(new Date(+time)), step(time, 1);
  7957. }
  7958. return times;
  7959. }
  7960. function range_utc(t0, t1, dt) {
  7961. try {
  7962. d3_time = d3_time_utc;
  7963. var utc = new d3_time_utc();
  7964. utc._ = t0;
  7965. return range(utc, t1, dt);
  7966. } finally {
  7967. d3_time = Date;
  7968. }
  7969. }
  7970. local.floor = local;
  7971. local.round = round;
  7972. local.ceil = ceil;
  7973. local.offset = offset;
  7974. local.range = range;
  7975. var utc = local.utc = d3_time_interval_utc(local);
  7976. utc.floor = utc;
  7977. utc.round = d3_time_interval_utc(round);
  7978. utc.ceil = d3_time_interval_utc(ceil);
  7979. utc.offset = d3_time_interval_utc(offset);
  7980. utc.range = range_utc;
  7981. return local;
  7982. }
  7983. function d3_time_interval_utc(method) {
  7984. return function(date, k) {
  7985. try {
  7986. d3_time = d3_time_utc;
  7987. var utc = new d3_time_utc();
  7988. utc._ = date;
  7989. return method(utc, k)._;
  7990. } finally {
  7991. d3_time = Date;
  7992. }
  7993. };
  7994. }
  7995. d3.time.second = d3_time_interval(function(date) {
  7996. return new d3_time(Math.floor(date / 1e3) * 1e3);
  7997. }, function(date, offset) {
  7998. date.setTime(date.getTime() + Math.floor(offset) * 1e3); // DST breaks setSeconds
  7999. }, function(date) {
  8000. return date.getSeconds();
  8001. });
  8002. d3.time.seconds = d3.time.second.range;
  8003. d3.time.seconds.utc = d3.time.second.utc.range;
  8004. d3.time.minute = d3_time_interval(function(date) {
  8005. return new d3_time(Math.floor(date / 6e4) * 6e4);
  8006. }, function(date, offset) {
  8007. date.setTime(date.getTime() + Math.floor(offset) * 6e4); // DST breaks setMinutes
  8008. }, function(date) {
  8009. return date.getMinutes();
  8010. });
  8011. d3.time.minutes = d3.time.minute.range;
  8012. d3.time.minutes.utc = d3.time.minute.utc.range;
  8013. d3.time.hour = d3_time_interval(function(date) {
  8014. var timezone = date.getTimezoneOffset() / 60;
  8015. return new d3_time((Math.floor(date / 36e5 - timezone) + timezone) * 36e5);
  8016. }, function(date, offset) {
  8017. date.setTime(date.getTime() + Math.floor(offset) * 36e5); // DST breaks setHours
  8018. }, function(date) {
  8019. return date.getHours();
  8020. });
  8021. d3.time.hours = d3.time.hour.range;
  8022. d3.time.hours.utc = d3.time.hour.utc.range;
  8023. d3.time.day = d3_time_interval(function(date) {
  8024. return new d3_time(date.getFullYear(), date.getMonth(), date.getDate());
  8025. }, function(date, offset) {
  8026. date.setDate(date.getDate() + offset);
  8027. }, function(date) {
  8028. return date.getDate() - 1;
  8029. });
  8030. d3.time.days = d3.time.day.range;
  8031. d3.time.days.utc = d3.time.day.utc.range;
  8032. d3.time.dayOfYear = function(date) {
  8033. var year = d3.time.year(date);
  8034. return Math.floor((date - year) / 864e5 - (date.getTimezoneOffset() - year.getTimezoneOffset()) / 1440);
  8035. };
  8036. d3_time_weekdays.forEach(function(day, i) {
  8037. day = day.toLowerCase();
  8038. i = 7 - i;
  8039. var interval = d3.time[day] = d3_time_interval(function(date) {
  8040. (date = d3.time.day(date)).setDate(date.getDate() - (date.getDay() + i) % 7);
  8041. return date;
  8042. }, function(date, offset) {
  8043. date.setDate(date.getDate() + Math.floor(offset) * 7);
  8044. }, function(date) {
  8045. var day = d3.time.year(date).getDay();
  8046. return Math.floor((d3.time.dayOfYear(date) + (day + i) % 7) / 7) - (day !== i);
  8047. });
  8048. d3.time[day + "s"] = interval.range;
  8049. d3.time[day + "s"].utc = interval.utc.range;
  8050. d3.time[day + "OfYear"] = function(date) {
  8051. var day = d3.time.year(date).getDay();
  8052. return Math.floor((d3.time.dayOfYear(date) + (day + i) % 7) / 7);
  8053. };
  8054. });
  8055. d3.time.week = d3.time.sunday;
  8056. d3.time.weeks = d3.time.sunday.range;
  8057. d3.time.weeks.utc = d3.time.sunday.utc.range;
  8058. d3.time.weekOfYear = d3.time.sundayOfYear;
  8059. d3.time.month = d3_time_interval(function(date) {
  8060. return new d3_time(date.getFullYear(), date.getMonth(), 1);
  8061. }, function(date, offset) {
  8062. date.setMonth(date.getMonth() + offset);
  8063. }, function(date) {
  8064. return date.getMonth();
  8065. });
  8066. d3.time.months = d3.time.month.range;
  8067. d3.time.months.utc = d3.time.month.utc.range;
  8068. d3.time.year = d3_time_interval(function(date) {
  8069. return new d3_time(date.getFullYear(), 0, 1);
  8070. }, function(date, offset) {
  8071. date.setFullYear(date.getFullYear() + offset);
  8072. }, function(date) {
  8073. return date.getFullYear();
  8074. });
  8075. d3.time.years = d3.time.year.range;
  8076. d3.time.years.utc = d3.time.year.utc.range;
  8077. function d3_time_scale(linear, methods, format) {
  8078. function scale(x) {
  8079. return linear(x);
  8080. }
  8081. scale.invert = function(x) {
  8082. return d3_time_scaleDate(linear.invert(x));
  8083. };
  8084. scale.domain = function(x) {
  8085. if (!arguments.length) return linear.domain().map(d3_time_scaleDate);
  8086. linear.domain(x);
  8087. return scale;
  8088. };
  8089. scale.nice = function(m) {
  8090. var extent = d3_time_scaleExtent(scale.domain());
  8091. return scale.domain([m.floor(extent[0]), m.ceil(extent[1])]);
  8092. };
  8093. scale.ticks = function(m, k) {
  8094. var extent = d3_time_scaleExtent(scale.domain());
  8095. if (typeof m !== "function") {
  8096. var span = extent[1] - extent[0],
  8097. target = span / m,
  8098. i = d3.bisect(d3_time_scaleSteps, target);
  8099. if (i == d3_time_scaleSteps.length) return methods.year(extent, m);
  8100. if (!i) return linear.ticks(m).map(d3_time_scaleDate);
  8101. if (Math.log(target / d3_time_scaleSteps[i - 1]) < Math.log(d3_time_scaleSteps[i] / target)) --i;
  8102. m = methods[i];
  8103. k = m[1];
  8104. m = m[0].range;
  8105. }
  8106. return m(extent[0], new Date(+extent[1] + 1), k); // inclusive upper bound
  8107. };
  8108. scale.tickFormat = function() {
  8109. return format;
  8110. };
  8111. scale.copy = function() {
  8112. return d3_time_scale(linear.copy(), methods, format);
  8113. };
  8114. // TOOD expose d3_scale_linear_rebind?
  8115. return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp");
  8116. }
  8117. // TODO expose d3_scaleExtent?
  8118. function d3_time_scaleExtent(domain) {
  8119. var start = domain[0], stop = domain[domain.length - 1];
  8120. return start < stop ? [start, stop] : [stop, start];
  8121. }
  8122. function d3_time_scaleDate(t) {
  8123. return new Date(t);
  8124. }
  8125. function d3_time_scaleFormat(formats) {
  8126. return function(date) {
  8127. var i = formats.length - 1, f = formats[i];
  8128. while (!f[1](date)) f = formats[--i];
  8129. return f[0](date);
  8130. };
  8131. }
  8132. function d3_time_scaleSetYear(y) {
  8133. var d = new Date(y, 0, 1);
  8134. d.setFullYear(y); // Y2K fail
  8135. return d;
  8136. }
  8137. function d3_time_scaleGetYear(d) {
  8138. var y = d.getFullYear(),
  8139. d0 = d3_time_scaleSetYear(y),
  8140. d1 = d3_time_scaleSetYear(y + 1);
  8141. return y + (d - d0) / (d1 - d0);
  8142. }
  8143. var d3_time_scaleSteps = [
  8144. 1e3, // 1-second
  8145. 5e3, // 5-second
  8146. 15e3, // 15-second
  8147. 3e4, // 30-second
  8148. 6e4, // 1-minute
  8149. 3e5, // 5-minute
  8150. 9e5, // 15-minute
  8151. 18e5, // 30-minute
  8152. 36e5, // 1-hour
  8153. 108e5, // 3-hour
  8154. 216e5, // 6-hour
  8155. 432e5, // 12-hour
  8156. 864e5, // 1-day
  8157. 1728e5, // 2-day
  8158. 6048e5, // 1-week
  8159. 2592e6, // 1-month
  8160. 7776e6, // 3-month
  8161. 31536e6 // 1-year
  8162. ];
  8163. var d3_time_scaleLocalMethods = [
  8164. [d3.time.second, 1],
  8165. [d3.time.second, 5],
  8166. [d3.time.second, 15],
  8167. [d3.time.second, 30],
  8168. [d3.time.minute, 1],
  8169. [d3.time.minute, 5],
  8170. [d3.time.minute, 15],
  8171. [d3.time.minute, 30],
  8172. [d3.time.hour, 1],
  8173. [d3.time.hour, 3],
  8174. [d3.time.hour, 6],
  8175. [d3.time.hour, 12],
  8176. [d3.time.day, 1],
  8177. [d3.time.day, 2],
  8178. [d3.time.week, 1],
  8179. [d3.time.month, 1],
  8180. [d3.time.month, 3],
  8181. [d3.time.year, 1]
  8182. ];
  8183. var d3_time_scaleLocalFormats = [
  8184. [d3.time.format("%Y"), function(d) { return true; }],
  8185. [d3.time.format("%B"), function(d) { return d.getMonth(); }],
  8186. [d3.time.format("%b %d"), function(d) { return d.getDate() != 1; }],
  8187. [d3.time.format("%a %d"), function(d) { return d.getDay() && d.getDate() != 1; }],
  8188. [d3.time.format("%I %p"), function(d) { return d.getHours(); }],
  8189. [d3.time.format("%I:%M"), function(d) { return d.getMinutes(); }],
  8190. [d3.time.format(":%S"), function(d) { return d.getSeconds(); }],
  8191. [d3.time.format(".%L"), function(d) { return d.getMilliseconds(); }]
  8192. ];
  8193. var d3_time_scaleLinear = d3.scale.linear(),
  8194. d3_time_scaleLocalFormat = d3_time_scaleFormat(d3_time_scaleLocalFormats);
  8195. d3_time_scaleLocalMethods.year = function(extent, m) {
  8196. return d3_time_scaleLinear.domain(extent.map(d3_time_scaleGetYear)).ticks(m).map(d3_time_scaleSetYear);
  8197. };
  8198. d3.time.scale = function() {
  8199. return d3_time_scale(d3.scale.linear(), d3_time_scaleLocalMethods, d3_time_scaleLocalFormat);
  8200. };
  8201. var d3_time_scaleUTCMethods = d3_time_scaleLocalMethods.map(function(m) {
  8202. return [m[0].utc, m[1]];
  8203. });
  8204. var d3_time_scaleUTCFormats = [
  8205. [d3.time.format.utc("%Y"), function(d) { return true; }],
  8206. [d3.time.format.utc("%B"), function(d) { return d.getUTCMonth(); }],
  8207. [d3.time.format.utc("%b %d"), function(d) { return d.getUTCDate() != 1; }],
  8208. [d3.time.format.utc("%a %d"), function(d) { return d.getUTCDay() && d.getUTCDate() != 1; }],
  8209. [d3.time.format.utc("%I %p"), function(d) { return d.getUTCHours(); }],
  8210. [d3.time.format.utc("%I:%M"), function(d) { return d.getUTCMinutes(); }],
  8211. [d3.time.format.utc(":%S"), function(d) { return d.getUTCSeconds(); }],
  8212. [d3.time.format.utc(".%L"), function(d) { return d.getUTCMilliseconds(); }]
  8213. ];
  8214. var d3_time_scaleUTCFormat = d3_time_scaleFormat(d3_time_scaleUTCFormats);
  8215. function d3_time_scaleUTCSetYear(y) {
  8216. var d = new Date(Date.UTC(y, 0, 1));
  8217. d.setUTCFullYear(y); // Y2K fail
  8218. return d;
  8219. }
  8220. function d3_time_scaleUTCGetYear(d) {
  8221. var y = d.getUTCFullYear(),
  8222. d0 = d3_time_scaleUTCSetYear(y),
  8223. d1 = d3_time_scaleUTCSetYear(y + 1);
  8224. return y + (d - d0) / (d1 - d0);
  8225. }
  8226. d3_time_scaleUTCMethods.year = function(extent, m) {
  8227. return d3_time_scaleLinear.domain(extent.map(d3_time_scaleUTCGetYear)).ticks(m).map(d3_time_scaleUTCSetYear);
  8228. };
  8229. d3.time.scale.utc = function() {
  8230. return d3_time_scale(d3.scale.linear(), d3_time_scaleUTCMethods, d3_time_scaleUTCFormat);
  8231. };
  8232. })();