d3.js 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763
  1. // See d3.LICENSE.txt for license.
  2. (function(){if (!Date.now) Date.now = function() {
  3. return +new Date;
  4. };
  5. try {
  6. document.createElement("div").style.setProperty("opacity", 0, "");
  7. } catch (error) {
  8. var d3_style_prototype = CSSStyleDeclaration.prototype,
  9. d3_style_setProperty = d3_style_prototype.setProperty;
  10. d3_style_prototype.setProperty = function(name, value, priority) {
  11. d3_style_setProperty.call(this, name, value + "", priority);
  12. };
  13. }
  14. d3 = {version: "2.7.4"}; // semver
  15. var d3_array = d3_arraySlice; // conversion for NodeLists
  16. function d3_arrayCopy(pseudoarray) {
  17. var i = -1, n = pseudoarray.length, array = [];
  18. while (++i < n) array.push(pseudoarray[i]);
  19. return array;
  20. }
  21. function d3_arraySlice(pseudoarray) {
  22. return Array.prototype.slice.call(pseudoarray);
  23. }
  24. try {
  25. d3_array(document.documentElement.childNodes)[0].nodeType;
  26. } catch(e) {
  27. d3_array = d3_arrayCopy;
  28. }
  29. var d3_arraySubclass = [].__proto__?
  30. // Until ECMAScript supports array subclassing, prototype injection works well.
  31. function(array, prototype) {
  32. array.__proto__ = prototype;
  33. }:
  34. // And if your browser doesn't support __proto__, we'll use direct extension.
  35. function(array, prototype) {
  36. for (var property in prototype) array[property] = prototype[property];
  37. };
  38. function d3_this() {
  39. return this;
  40. }
  41. d3.functor = function(v) {
  42. return typeof v === "function" ? v : function() { return v; };
  43. };
  44. // Copies a variable number of methods from source to target.
  45. d3.rebind = function(target, source) {
  46. var i = 1, n = arguments.length, method;
  47. while (++i < n) target[method = arguments[i]] = d3_rebind(target, source, source[method]);
  48. return target;
  49. };
  50. // Method is assumed to be a standard D3 getter-setter:
  51. // If passed with no arguments, gets the value.
  52. // If passed with arguments, sets the value and returns the target.
  53. function d3_rebind(target, source, method) {
  54. return function() {
  55. var value = method.apply(source, arguments);
  56. return arguments.length ? target : value;
  57. };
  58. }
  59. d3.ascending = function(a, b) {
  60. return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
  61. };
  62. d3.descending = function(a, b) {
  63. return b < a ? -1 : b > a ? 1 : b >= a ? 0 : NaN;
  64. };
  65. d3.mean = function(array, f) {
  66. var n = array.length,
  67. a,
  68. m = 0,
  69. i = -1,
  70. j = 0;
  71. if (arguments.length === 1) {
  72. while (++i < n) if (d3_number(a = array[i])) m += (a - m) / ++j;
  73. } else {
  74. while (++i < n) if (d3_number(a = f.call(array, array[i], i))) m += (a - m) / ++j;
  75. }
  76. return j ? m : undefined;
  77. };
  78. d3.median = function(array, f) {
  79. if (arguments.length > 1) array = array.map(f);
  80. array = array.filter(d3_number);
  81. return array.length ? d3.quantile(array.sort(d3.ascending), .5) : undefined;
  82. };
  83. d3.min = function(array, f) {
  84. var i = -1,
  85. n = array.length,
  86. a,
  87. b;
  88. if (arguments.length === 1) {
  89. while (++i < n && ((a = array[i]) == null || a != a)) a = undefined;
  90. while (++i < n) if ((b = array[i]) != null && a > b) a = b;
  91. } else {
  92. while (++i < n && ((a = f.call(array, array[i], i)) == null || a != a)) a = undefined;
  93. while (++i < n) if ((b = f.call(array, array[i], i)) != null && a > b) a = b;
  94. }
  95. return a;
  96. };
  97. d3.max = function(array, f) {
  98. var i = -1,
  99. n = array.length,
  100. a,
  101. b;
  102. if (arguments.length === 1) {
  103. while (++i < n && ((a = array[i]) == null || a != a)) a = undefined;
  104. while (++i < n) if ((b = array[i]) != null && b > a) a = b;
  105. } else {
  106. while (++i < n && ((a = f.call(array, array[i], i)) == null || a != a)) a = undefined;
  107. while (++i < n) if ((b = f.call(array, array[i], i)) != null && b > a) a = b;
  108. }
  109. return a;
  110. };
  111. d3.extent = function(array, f) {
  112. var i = -1,
  113. n = array.length,
  114. a,
  115. b,
  116. c;
  117. if (arguments.length === 1) {
  118. while (++i < n && ((a = c = array[i]) == null || a != a)) a = c = undefined;
  119. while (++i < n) if ((b = array[i]) != null) {
  120. if (a > b) a = b;
  121. if (c < b) c = b;
  122. }
  123. } else {
  124. while (++i < n && ((a = c = f.call(array, array[i], i)) == null || a != a)) a = undefined;
  125. while (++i < n) if ((b = f.call(array, array[i], i)) != null) {
  126. if (a > b) a = b;
  127. if (c < b) c = b;
  128. }
  129. }
  130. return [a, c];
  131. };
  132. d3.random = {
  133. normal: function(mean, deviation) {
  134. if (arguments.length < 2) deviation = 1;
  135. if (arguments.length < 1) mean = 0;
  136. return function() {
  137. var x, y, r;
  138. do {
  139. x = Math.random() * 2 - 1;
  140. y = Math.random() * 2 - 1;
  141. r = x * x + y * y;
  142. } while (!r || r > 1);
  143. return mean + deviation * x * Math.sqrt(-2 * Math.log(r) / r);
  144. };
  145. }
  146. };
  147. function d3_number(x) {
  148. return x != null && !isNaN(x);
  149. }
  150. d3.sum = function(array, f) {
  151. var s = 0,
  152. n = array.length,
  153. a,
  154. i = -1;
  155. if (arguments.length === 1) {
  156. while (++i < n) if (!isNaN(a = +array[i])) s += a;
  157. } else {
  158. while (++i < n) if (!isNaN(a = +f.call(array, array[i], i))) s += a;
  159. }
  160. return s;
  161. };
  162. // R-7 per <http://en.wikipedia.org/wiki/Quantile>
  163. d3.quantile = function(values, p) {
  164. var H = (values.length - 1) * p + 1,
  165. h = Math.floor(H),
  166. v = values[h - 1],
  167. e = H - h;
  168. return e ? v + e * (values[h] - v) : v;
  169. };
  170. d3.transpose = function(matrix) {
  171. return d3.zip.apply(d3, matrix);
  172. };
  173. d3.zip = function() {
  174. if (!(n = arguments.length)) return [];
  175. for (var i = -1, m = d3.min(arguments, d3_zipLength), zips = new Array(m); ++i < m;) {
  176. for (var j = -1, n, zip = zips[i] = new Array(n); ++j < n;) {
  177. zip[j] = arguments[j][i];
  178. }
  179. }
  180. return zips;
  181. };
  182. function d3_zipLength(d) {
  183. return d.length;
  184. }
  185. // Locate the insertion point for x in a to maintain sorted order. The
  186. // arguments lo and hi may be used to specify a subset of the array which should
  187. // be considered; by default the entire array is used. If x is already present
  188. // in a, the insertion point will be before (to the left of) any existing
  189. // entries. The return value is suitable for use as the first argument to
  190. // `array.splice` assuming that a is already sorted.
  191. //
  192. // The returned insertion point i partitions the array a into two halves so that
  193. // all v < x for v in a[lo:i] for the left side and all v >= x for v in a[i:hi]
  194. // for the right side.
  195. d3.bisectLeft = function(a, x, lo, hi) {
  196. if (arguments.length < 3) lo = 0;
  197. if (arguments.length < 4) hi = a.length;
  198. while (lo < hi) {
  199. var mid = (lo + hi) >> 1;
  200. if (a[mid] < x) lo = mid + 1;
  201. else hi = mid;
  202. }
  203. return lo;
  204. };
  205. // Similar to bisectLeft, but returns an insertion point which comes after (to
  206. // the right of) any existing entries of x in a.
  207. //
  208. // The returned insertion point i partitions the array into two halves so that
  209. // all v <= x for v in a[lo:i] for the left side and all v > x for v in a[i:hi]
  210. // for the right side.
  211. d3.bisect =
  212. d3.bisectRight = function(a, x, lo, hi) {
  213. if (arguments.length < 3) lo = 0;
  214. if (arguments.length < 4) hi = a.length;
  215. while (lo < hi) {
  216. var mid = (lo + hi) >> 1;
  217. if (x < a[mid]) hi = mid;
  218. else lo = mid + 1;
  219. }
  220. return lo;
  221. };
  222. d3.first = function(array, f) {
  223. var i = 0,
  224. n = array.length,
  225. a = array[0],
  226. b;
  227. if (arguments.length === 1) f = d3.ascending;
  228. while (++i < n) {
  229. if (f.call(array, a, b = array[i]) > 0) {
  230. a = b;
  231. }
  232. }
  233. return a;
  234. };
  235. d3.last = function(array, f) {
  236. var i = 0,
  237. n = array.length,
  238. a = array[0],
  239. b;
  240. if (arguments.length === 1) f = d3.ascending;
  241. while (++i < n) {
  242. if (f.call(array, a, b = array[i]) <= 0) {
  243. a = b;
  244. }
  245. }
  246. return a;
  247. };
  248. d3.nest = function() {
  249. var nest = {},
  250. keys = [],
  251. sortKeys = [],
  252. sortValues,
  253. rollup;
  254. function map(array, depth) {
  255. if (depth >= keys.length) return rollup
  256. ? rollup.call(nest, array) : (sortValues
  257. ? array.sort(sortValues)
  258. : array);
  259. var i = -1,
  260. n = array.length,
  261. key = keys[depth++],
  262. keyValue,
  263. object,
  264. o = {};
  265. while (++i < n) {
  266. if ((keyValue = key(object = array[i])) in o) {
  267. o[keyValue].push(object);
  268. } else {
  269. o[keyValue] = [object];
  270. }
  271. }
  272. for (keyValue in o) {
  273. o[keyValue] = map(o[keyValue], depth);
  274. }
  275. return o;
  276. }
  277. function entries(map, depth) {
  278. if (depth >= keys.length) return map;
  279. var a = [],
  280. sortKey = sortKeys[depth++],
  281. key;
  282. for (key in map) {
  283. a.push({key: key, values: entries(map[key], depth)});
  284. }
  285. if (sortKey) a.sort(function(a, b) {
  286. return sortKey(a.key, b.key);
  287. });
  288. return a;
  289. }
  290. nest.map = function(array) {
  291. return map(array, 0);
  292. };
  293. nest.entries = function(array) {
  294. return entries(map(array, 0), 0);
  295. };
  296. nest.key = function(d) {
  297. keys.push(d);
  298. return nest;
  299. };
  300. // Specifies the order for the most-recently specified key.
  301. // Note: only applies to entries. Map keys are unordered!
  302. nest.sortKeys = function(order) {
  303. sortKeys[keys.length - 1] = order;
  304. return nest;
  305. };
  306. // Specifies the order for leaf values.
  307. // Applies to both maps and entries array.
  308. nest.sortValues = function(order) {
  309. sortValues = order;
  310. return nest;
  311. };
  312. nest.rollup = function(f) {
  313. rollup = f;
  314. return nest;
  315. };
  316. return nest;
  317. };
  318. d3.keys = function(map) {
  319. var keys = [];
  320. for (var key in map) keys.push(key);
  321. return keys;
  322. };
  323. d3.values = function(map) {
  324. var values = [];
  325. for (var key in map) values.push(map[key]);
  326. return values;
  327. };
  328. d3.entries = function(map) {
  329. var entries = [];
  330. for (var key in map) entries.push({key: key, value: map[key]});
  331. return entries;
  332. };
  333. d3.permute = function(array, indexes) {
  334. var permutes = [],
  335. i = -1,
  336. n = indexes.length;
  337. while (++i < n) permutes[i] = array[indexes[i]];
  338. return permutes;
  339. };
  340. d3.merge = function(arrays) {
  341. return Array.prototype.concat.apply([], arrays);
  342. };
  343. d3.split = function(array, f) {
  344. var arrays = [],
  345. values = [],
  346. value,
  347. i = -1,
  348. n = array.length;
  349. if (arguments.length < 2) f = d3_splitter;
  350. while (++i < n) {
  351. if (f.call(values, value = array[i], i)) {
  352. values = [];
  353. } else {
  354. if (!values.length) arrays.push(values);
  355. values.push(value);
  356. }
  357. }
  358. return arrays;
  359. };
  360. function d3_splitter(d) {
  361. return d == null;
  362. }
  363. function d3_collapse(s) {
  364. return s.replace(/(^\s+)|(\s+$)/g, "").replace(/\s+/g, " ");
  365. }
  366. /**
  367. * @param {number} start
  368. * @param {number=} stop
  369. * @param {number=} step
  370. */
  371. d3.range = function(start, stop, step) {
  372. if (arguments.length < 3) {
  373. step = 1;
  374. if (arguments.length < 2) {
  375. stop = start;
  376. start = 0;
  377. }
  378. }
  379. if ((stop - start) / step == Infinity) throw new Error("infinite range");
  380. var range = [],
  381. i = -1,
  382. j;
  383. if (step < 0) while ((j = start + step * ++i) > stop) range.push(j);
  384. else while ((j = start + step * ++i) < stop) range.push(j);
  385. return range;
  386. };
  387. d3.requote = function(s) {
  388. return s.replace(d3_requote_re, "\\$&");
  389. };
  390. var d3_requote_re = /[\\\^\$\*\+\?\|\[\]\(\)\.\{\}]/g;
  391. d3.round = function(x, n) {
  392. return n
  393. ? Math.round(x * (n = Math.pow(10, n))) / n
  394. : Math.round(x);
  395. };
  396. d3.xhr = function(url, mime, callback) {
  397. var req = new XMLHttpRequest;
  398. if (arguments.length < 3) callback = mime, mime = null;
  399. else if (mime && req.overrideMimeType) req.overrideMimeType(mime);
  400. req.open("GET", url, true);
  401. if (mime) req.setRequestHeader("Accept", mime);
  402. req.onreadystatechange = function() {
  403. if (req.readyState === 4) callback(req.status < 300 ? req : null);
  404. };
  405. req.send(null);
  406. };
  407. d3.text = function(url, mime, callback) {
  408. function ready(req) {
  409. callback(req && req.responseText);
  410. }
  411. if (arguments.length < 3) {
  412. callback = mime;
  413. mime = null;
  414. }
  415. d3.xhr(url, mime, ready);
  416. };
  417. d3.json = function(url, callback) {
  418. d3.text(url, "application/json", function(text) {
  419. callback(text ? JSON.parse(text) : null);
  420. });
  421. };
  422. d3.html = function(url, callback) {
  423. d3.text(url, "text/html", function(text) {
  424. if (text != null) { // Treat empty string as valid HTML.
  425. var range = document.createRange();
  426. range.selectNode(document.body);
  427. text = range.createContextualFragment(text);
  428. }
  429. callback(text);
  430. });
  431. };
  432. d3.xml = function(url, mime, callback) {
  433. function ready(req) {
  434. callback(req && req.responseXML);
  435. }
  436. if (arguments.length < 3) {
  437. callback = mime;
  438. mime = null;
  439. }
  440. d3.xhr(url, mime, ready);
  441. };
  442. var d3_nsPrefix = {
  443. svg: "http://www.w3.org/2000/svg",
  444. xhtml: "http://www.w3.org/1999/xhtml",
  445. xlink: "http://www.w3.org/1999/xlink",
  446. xml: "http://www.w3.org/XML/1998/namespace",
  447. xmlns: "http://www.w3.org/2000/xmlns/"
  448. };
  449. d3.ns = {
  450. prefix: d3_nsPrefix,
  451. qualify: function(name) {
  452. var i = name.indexOf(":");
  453. return i < 0 ? (name in d3_nsPrefix
  454. ? {space: d3_nsPrefix[name], local: name} : name)
  455. : {space: d3_nsPrefix[name.substring(0, i)], local: name.substring(i + 1)};
  456. }
  457. };
  458. d3.dispatch = function() {
  459. var dispatch = new d3_dispatch(),
  460. i = -1,
  461. n = arguments.length;
  462. while (++i < n) dispatch[arguments[i]] = d3_dispatch_event(dispatch);
  463. return dispatch;
  464. };
  465. function d3_dispatch() {}
  466. d3_dispatch.prototype.on = function(type, listener) {
  467. var i = type.indexOf("."),
  468. name = "";
  469. // Extract optional namespace, e.g., "click.foo"
  470. if (i > 0) {
  471. name = type.substring(i + 1);
  472. type = type.substring(0, i);
  473. }
  474. return arguments.length < 2
  475. ? this[type].on(name)
  476. : this[type].on(name, listener);
  477. };
  478. function d3_dispatch_event(dispatch) {
  479. var listeners = [],
  480. listenerByName = {};
  481. function event() {
  482. var z = listeners, // defensive reference
  483. i = -1,
  484. n = z.length,
  485. l;
  486. while (++i < n) if (l = z[i].on) l.apply(this, arguments);
  487. return dispatch;
  488. }
  489. event.on = function(name, listener) {
  490. var l, i;
  491. // return the current listener, if any
  492. if (arguments.length < 2) return (l = listenerByName[name]) && l.on;
  493. // remove the old listener, if any (with copy-on-write)
  494. if (l = listenerByName[name]) {
  495. l.on = null;
  496. listeners = listeners.slice(0, i = listeners.indexOf(l)).concat(listeners.slice(i + 1));
  497. delete listenerByName[name];
  498. }
  499. // add the new listener, if any
  500. if (listener) {
  501. listeners.push(listenerByName[name] = {on: listener});
  502. }
  503. return dispatch;
  504. };
  505. return event;
  506. };
  507. // TODO align
  508. d3.format = function(specifier) {
  509. var match = d3_format_re.exec(specifier),
  510. fill = match[1] || " ",
  511. sign = match[3] || "",
  512. zfill = match[5],
  513. width = +match[6],
  514. comma = match[7],
  515. precision = match[8],
  516. type = match[9],
  517. scale = 1,
  518. suffix = "",
  519. integer = false;
  520. if (precision) precision = +precision.substring(1);
  521. if (zfill) {
  522. fill = "0"; // TODO align = "=";
  523. if (comma) width -= Math.floor((width - 1) / 4);
  524. }
  525. switch (type) {
  526. case "n": comma = true; type = "g"; break;
  527. case "%": scale = 100; suffix = "%"; type = "f"; break;
  528. case "p": scale = 100; suffix = "%"; type = "r"; break;
  529. case "d": integer = true; precision = 0; break;
  530. case "s": scale = -1; type = "r"; break;
  531. }
  532. // If no precision is specified for r, fallback to general notation.
  533. if (type == "r" && !precision) type = "g";
  534. type = d3_format_types[type] || d3_format_typeDefault;
  535. return function(value) {
  536. // Return the empty string for floats formatted as ints.
  537. if (integer && (value % 1)) return "";
  538. // Convert negative to positive, and record the sign prefix.
  539. var negative = (value < 0) && (value = -value) ? "\u2212" : sign;
  540. // Apply the scale, computing it from the value's exponent for si format.
  541. if (scale < 0) {
  542. var prefix = d3.formatPrefix(value, precision);
  543. value *= prefix.scale;
  544. suffix = prefix.symbol;
  545. } else {
  546. value *= scale;
  547. }
  548. // Convert to the desired precision.
  549. value = type(value, precision);
  550. // If the fill character is 0, the sign and group is applied after the fill.
  551. if (zfill) {
  552. var length = value.length + negative.length;
  553. if (length < width) value = new Array(width - length + 1).join(fill) + value;
  554. if (comma) value = d3_format_group(value);
  555. value = negative + value;
  556. }
  557. // Otherwise (e.g., space-filling), the sign and group is applied before.
  558. else {
  559. if (comma) value = d3_format_group(value);
  560. value = negative + value;
  561. var length = value.length;
  562. if (length < width) value = new Array(width - length + 1).join(fill) + value;
  563. }
  564. return value + suffix;
  565. };
  566. };
  567. // [[fill]align][sign][#][0][width][,][.precision][type]
  568. var d3_format_re = /(?:([^{])?([<>=^]))?([+\- ])?(#)?(0)?([0-9]+)?(,)?(\.[0-9]+)?([a-zA-Z%])?/;
  569. var d3_format_types = {
  570. g: function(x, p) { return x.toPrecision(p); },
  571. e: function(x, p) { return x.toExponential(p); },
  572. f: function(x, p) { return x.toFixed(p); },
  573. r: function(x, p) { return d3.round(x, p = d3_format_precision(x, p)).toFixed(Math.max(0, Math.min(20, p))); }
  574. };
  575. function d3_format_precision(x, p) {
  576. return p - (x ? 1 + Math.floor(Math.log(x + Math.pow(10, 1 + Math.floor(Math.log(x) / Math.LN10) - p)) / Math.LN10) : 1);
  577. }
  578. function d3_format_typeDefault(x) {
  579. return x + "";
  580. }
  581. // Apply comma grouping for thousands.
  582. function d3_format_group(value) {
  583. var i = value.lastIndexOf("."),
  584. f = i >= 0 ? value.substring(i) : (i = value.length, ""),
  585. t = [];
  586. while (i > 0) t.push(value.substring(i -= 3, i + 3));
  587. return t.reverse().join(",") + f;
  588. }
  589. var d3_formatPrefixes = ["y","z","a","f","p","n","μ","m","","k","M","G","T","P","E","Z","Y"].map(d3_formatPrefix);
  590. d3.formatPrefix = function(value, precision) {
  591. var i = 0;
  592. if (value) {
  593. if (value < 0) value *= -1;
  594. if (precision) value = d3.round(value, d3_format_precision(value, precision));
  595. i = 1 + Math.floor(1e-12 + Math.log(value) / Math.LN10);
  596. i = Math.max(-24, Math.min(24, Math.floor((i <= 0 ? i + 1 : i - 1) / 3) * 3));
  597. }
  598. return d3_formatPrefixes[8 + i / 3];
  599. };
  600. function d3_formatPrefix(d, i) {
  601. return {
  602. scale: Math.pow(10, (8 - i) * 3),
  603. symbol: d
  604. };
  605. }
  606. /*
  607. * TERMS OF USE - EASING EQUATIONS
  608. *
  609. * Open source under the BSD License.
  610. *
  611. * Copyright 2001 Robert Penner
  612. * All rights reserved.
  613. *
  614. * Redistribution and use in source and binary forms, with or without
  615. * modification, are permitted provided that the following conditions are met:
  616. *
  617. * - Redistributions of source code must retain the above copyright notice, this
  618. * list of conditions and the following disclaimer.
  619. *
  620. * - Redistributions in binary form must reproduce the above copyright notice,
  621. * this list of conditions and the following disclaimer in the documentation
  622. * and/or other materials provided with the distribution.
  623. *
  624. * - Neither the name of the author nor the names of contributors may be used to
  625. * endorse or promote products derived from this software without specific
  626. * prior written permission.
  627. *
  628. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  629. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  630. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  631. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  632. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  633. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  634. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  635. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  636. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  637. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  638. * POSSIBILITY OF SUCH DAMAGE.
  639. */
  640. var d3_ease_quad = d3_ease_poly(2),
  641. d3_ease_cubic = d3_ease_poly(3);
  642. var d3_ease = {
  643. linear: function() { return d3_ease_linear; },
  644. poly: d3_ease_poly,
  645. quad: function() { return d3_ease_quad; },
  646. cubic: function() { return d3_ease_cubic; },
  647. sin: function() { return d3_ease_sin; },
  648. exp: function() { return d3_ease_exp; },
  649. circle: function() { return d3_ease_circle; },
  650. elastic: d3_ease_elastic,
  651. back: d3_ease_back,
  652. bounce: function() { return d3_ease_bounce; }
  653. };
  654. var d3_ease_mode = {
  655. "in": function(f) { return f; },
  656. "out": d3_ease_reverse,
  657. "in-out": d3_ease_reflect,
  658. "out-in": function(f) { return d3_ease_reflect(d3_ease_reverse(f)); }
  659. };
  660. d3.ease = function(name) {
  661. var i = name.indexOf("-"),
  662. t = i >= 0 ? name.substring(0, i) : name,
  663. m = i >= 0 ? name.substring(i + 1) : "in";
  664. return d3_ease_clamp(d3_ease_mode[m](d3_ease[t].apply(null, Array.prototype.slice.call(arguments, 1))));
  665. };
  666. function d3_ease_clamp(f) {
  667. return function(t) {
  668. return t <= 0 ? 0 : t >= 1 ? 1 : f(t);
  669. };
  670. }
  671. function d3_ease_reverse(f) {
  672. return function(t) {
  673. return 1 - f(1 - t);
  674. };
  675. }
  676. function d3_ease_reflect(f) {
  677. return function(t) {
  678. return .5 * (t < .5 ? f(2 * t) : (2 - f(2 - 2 * t)));
  679. };
  680. }
  681. function d3_ease_linear(t) {
  682. return t;
  683. }
  684. function d3_ease_poly(e) {
  685. return function(t) {
  686. return Math.pow(t, e);
  687. }
  688. }
  689. function d3_ease_sin(t) {
  690. return 1 - Math.cos(t * Math.PI / 2);
  691. }
  692. function d3_ease_exp(t) {
  693. return Math.pow(2, 10 * (t - 1));
  694. }
  695. function d3_ease_circle(t) {
  696. return 1 - Math.sqrt(1 - t * t);
  697. }
  698. function d3_ease_elastic(a, p) {
  699. var s;
  700. if (arguments.length < 2) p = 0.45;
  701. if (arguments.length < 1) { a = 1; s = p / 4; }
  702. else s = p / (2 * Math.PI) * Math.asin(1 / a);
  703. return function(t) {
  704. return 1 + a * Math.pow(2, 10 * -t) * Math.sin((t - s) * 2 * Math.PI / p);
  705. };
  706. }
  707. function d3_ease_back(s) {
  708. if (!s) s = 1.70158;
  709. return function(t) {
  710. return t * t * ((s + 1) * t - s);
  711. };
  712. }
  713. function d3_ease_bounce(t) {
  714. return t < 1 / 2.75 ? 7.5625 * t * t
  715. : t < 2 / 2.75 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75
  716. : t < 2.5 / 2.75 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375
  717. : 7.5625 * (t -= 2.625 / 2.75) * t + .984375;
  718. }
  719. d3.event = null;
  720. function d3_eventCancel() {
  721. d3.event.stopPropagation();
  722. d3.event.preventDefault();
  723. }
  724. d3.interpolate = function(a, b) {
  725. var i = d3.interpolators.length, f;
  726. while (--i >= 0 && !(f = d3.interpolators[i](a, b)));
  727. return f;
  728. };
  729. d3.interpolateNumber = function(a, b) {
  730. b -= a;
  731. return function(t) { return a + b * t; };
  732. };
  733. d3.interpolateRound = function(a, b) {
  734. b -= a;
  735. return function(t) { return Math.round(a + b * t); };
  736. };
  737. d3.interpolateString = function(a, b) {
  738. var m, // current match
  739. i, // current index
  740. j, // current index (for coallescing)
  741. s0 = 0, // start index of current string prefix
  742. s1 = 0, // end index of current string prefix
  743. s = [], // string constants and placeholders
  744. q = [], // number interpolators
  745. n, // q.length
  746. o;
  747. // Reset our regular expression!
  748. d3_interpolate_number.lastIndex = 0;
  749. // Find all numbers in b.
  750. for (i = 0; m = d3_interpolate_number.exec(b); ++i) {
  751. if (m.index) s.push(b.substring(s0, s1 = m.index));
  752. q.push({i: s.length, x: m[0]});
  753. s.push(null);
  754. s0 = d3_interpolate_number.lastIndex;
  755. }
  756. if (s0 < b.length) s.push(b.substring(s0));
  757. // Find all numbers in a.
  758. for (i = 0, n = q.length; (m = d3_interpolate_number.exec(a)) && i < n; ++i) {
  759. o = q[i];
  760. if (o.x == m[0]) { // The numbers match, so coallesce.
  761. if (o.i) {
  762. if (s[o.i + 1] == null) { // This match is followed by another number.
  763. s[o.i - 1] += o.x;
  764. s.splice(o.i, 1);
  765. for (j = i + 1; j < n; ++j) q[j].i--;
  766. } else { // This match is followed by a string, so coallesce twice.
  767. s[o.i - 1] += o.x + s[o.i + 1];
  768. s.splice(o.i, 2);
  769. for (j = i + 1; j < n; ++j) q[j].i -= 2;
  770. }
  771. } else {
  772. if (s[o.i + 1] == null) { // This match is followed by another number.
  773. s[o.i] = o.x;
  774. } else { // This match is followed by a string, so coallesce twice.
  775. s[o.i] = o.x + s[o.i + 1];
  776. s.splice(o.i + 1, 1);
  777. for (j = i + 1; j < n; ++j) q[j].i--;
  778. }
  779. }
  780. q.splice(i, 1);
  781. n--;
  782. i--;
  783. } else {
  784. o.x = d3.interpolateNumber(parseFloat(m[0]), parseFloat(o.x));
  785. }
  786. }
  787. // Remove any numbers in b not found in a.
  788. while (i < n) {
  789. o = q.pop();
  790. if (s[o.i + 1] == null) { // This match is followed by another number.
  791. s[o.i] = o.x;
  792. } else { // This match is followed by a string, so coallesce twice.
  793. s[o.i] = o.x + s[o.i + 1];
  794. s.splice(o.i + 1, 1);
  795. }
  796. n--;
  797. }
  798. // Special optimization for only a single match.
  799. if (s.length === 1) {
  800. return s[0] == null ? q[0].x : function() { return b; };
  801. }
  802. // Otherwise, interpolate each of the numbers and rejoin the string.
  803. return function(t) {
  804. for (i = 0; i < n; ++i) s[(o = q[i]).i] = o.x(t);
  805. return s.join("");
  806. };
  807. };
  808. d3.interpolateTransform = function(a, b) {
  809. var s = [], // string constants and placeholders
  810. q = [], // number interpolators
  811. n,
  812. A = d3.transform(a),
  813. B = d3.transform(b),
  814. ta = A.translate,
  815. tb = B.translate,
  816. ra = A.rotate,
  817. rb = B.rotate,
  818. wa = A.skew,
  819. wb = B.skew,
  820. ka = A.scale,
  821. kb = B.scale;
  822. if (ta[0] != tb[0] || ta[1] != tb[1]) {
  823. s.push("translate(", null, ",", null, ")");
  824. q.push({i: 1, x: d3.interpolateNumber(ta[0], tb[0])}, {i: 3, x: d3.interpolateNumber(ta[1], tb[1])});
  825. } else if (tb[0] || tb[1]) {
  826. s.push("translate(" + tb + ")");
  827. } else {
  828. s.push("");
  829. }
  830. if (ra != rb) {
  831. q.push({i: s.push(s.pop() + "rotate(", null, ")") - 2, x: d3.interpolateNumber(ra, rb)});
  832. } else if (rb) {
  833. s.push(s.pop() + "rotate(" + rb + ")");
  834. }
  835. if (wa != wb) {
  836. q.push({i: s.push(s.pop() + "skewX(", null, ")") - 2, x: d3.interpolateNumber(wa, wb)});
  837. } else if (wb) {
  838. s.push(s.pop() + "skewX(" + wb + ")");
  839. }
  840. if (ka[0] != kb[0] || ka[1] != kb[1]) {
  841. n = s.push(s.pop() + "scale(", null, ",", null, ")");
  842. q.push({i: n - 4, x: d3.interpolateNumber(ka[0], kb[0])}, {i: n - 2, x: d3.interpolateNumber(ka[1], kb[1])});
  843. } else if (kb[0] != 1 || kb[1] != 1) {
  844. s.push(s.pop() + "scale(" + kb + ")");
  845. }
  846. n = q.length;
  847. return function(t) {
  848. var i = -1, o;
  849. while (++i < n) s[(o = q[i]).i] = o.x(t);
  850. return s.join("");
  851. };
  852. };
  853. d3.interpolateRgb = function(a, b) {
  854. a = d3.rgb(a);
  855. b = d3.rgb(b);
  856. var ar = a.r,
  857. ag = a.g,
  858. ab = a.b,
  859. br = b.r - ar,
  860. bg = b.g - ag,
  861. bb = b.b - ab;
  862. return function(t) {
  863. return "#"
  864. + d3_rgb_hex(Math.round(ar + br * t))
  865. + d3_rgb_hex(Math.round(ag + bg * t))
  866. + d3_rgb_hex(Math.round(ab + bb * t));
  867. };
  868. };
  869. // interpolates HSL space, but outputs RGB string (for compatibility)
  870. d3.interpolateHsl = function(a, b) {
  871. a = d3.hsl(a);
  872. b = d3.hsl(b);
  873. var h0 = a.h,
  874. s0 = a.s,
  875. l0 = a.l,
  876. h1 = b.h - h0,
  877. s1 = b.s - s0,
  878. l1 = b.l - l0;
  879. return function(t) {
  880. return d3_hsl_rgb(h0 + h1 * t, s0 + s1 * t, l0 + l1 * t).toString();
  881. };
  882. };
  883. d3.interpolateArray = function(a, b) {
  884. var x = [],
  885. c = [],
  886. na = a.length,
  887. nb = b.length,
  888. n0 = Math.min(a.length, b.length),
  889. i;
  890. for (i = 0; i < n0; ++i) x.push(d3.interpolate(a[i], b[i]));
  891. for (; i < na; ++i) c[i] = a[i];
  892. for (; i < nb; ++i) c[i] = b[i];
  893. return function(t) {
  894. for (i = 0; i < n0; ++i) c[i] = x[i](t);
  895. return c;
  896. };
  897. };
  898. d3.interpolateObject = function(a, b) {
  899. var i = {},
  900. c = {},
  901. k;
  902. for (k in a) {
  903. if (k in b) {
  904. i[k] = d3_interpolateByName(k)(a[k], b[k]);
  905. } else {
  906. c[k] = a[k];
  907. }
  908. }
  909. for (k in b) {
  910. if (!(k in a)) {
  911. c[k] = b[k];
  912. }
  913. }
  914. return function(t) {
  915. for (k in i) c[k] = i[k](t);
  916. return c;
  917. };
  918. }
  919. var d3_interpolate_number = /[-+]?(?:\d*\.?\d+)(?:[eE][-+]?\d+)?/g;
  920. function d3_interpolateByName(n) {
  921. return n == "transform"
  922. ? d3.interpolateTransform
  923. : d3.interpolate;
  924. }
  925. d3.interpolators = [
  926. d3.interpolateObject,
  927. function(a, b) { return (b instanceof Array) && d3.interpolateArray(a, b); },
  928. function(a, b) { return (typeof a === "string" || typeof b === "string") && d3.interpolateString(a + "", b + ""); },
  929. function(a, b) { return (typeof b === "string" ? b in d3_rgb_names || /^(#|rgb\(|hsl\()/.test(b) : b instanceof d3_Rgb || b instanceof d3_Hsl) && d3.interpolateRgb(a, b); },
  930. function(a, b) { return !isNaN(a = +a) && !isNaN(b = +b) && d3.interpolateNumber(a, b); }
  931. ];
  932. function d3_uninterpolateNumber(a, b) {
  933. b = b - (a = +a) ? 1 / (b - a) : 0;
  934. return function(x) { return (x - a) * b; };
  935. }
  936. function d3_uninterpolateClamp(a, b) {
  937. b = b - (a = +a) ? 1 / (b - a) : 0;
  938. return function(x) { return Math.max(0, Math.min(1, (x - a) * b)); };
  939. }
  940. d3.rgb = function(r, g, b) {
  941. return arguments.length === 1
  942. ? (r instanceof d3_Rgb ? d3_rgb(r.r, r.g, r.b)
  943. : d3_rgb_parse("" + r, d3_rgb, d3_hsl_rgb))
  944. : d3_rgb(~~r, ~~g, ~~b);
  945. };
  946. function d3_rgb(r, g, b) {
  947. return new d3_Rgb(r, g, b);
  948. }
  949. function d3_Rgb(r, g, b) {
  950. this.r = r;
  951. this.g = g;
  952. this.b = b;
  953. }
  954. d3_Rgb.prototype.brighter = function(k) {
  955. k = Math.pow(0.7, arguments.length ? k : 1);
  956. var r = this.r,
  957. g = this.g,
  958. b = this.b,
  959. i = 30;
  960. if (!r && !g && !b) return d3_rgb(i, i, i);
  961. if (r && r < i) r = i;
  962. if (g && g < i) g = i;
  963. if (b && b < i) b = i;
  964. return d3_rgb(
  965. Math.min(255, Math.floor(r / k)),
  966. Math.min(255, Math.floor(g / k)),
  967. Math.min(255, Math.floor(b / k)));
  968. };
  969. d3_Rgb.prototype.darker = function(k) {
  970. k = Math.pow(0.7, arguments.length ? k : 1);
  971. return d3_rgb(
  972. Math.floor(k * this.r),
  973. Math.floor(k * this.g),
  974. Math.floor(k * this.b));
  975. };
  976. d3_Rgb.prototype.hsl = function() {
  977. return d3_rgb_hsl(this.r, this.g, this.b);
  978. };
  979. d3_Rgb.prototype.toString = function() {
  980. return "#" + d3_rgb_hex(this.r) + d3_rgb_hex(this.g) + d3_rgb_hex(this.b);
  981. };
  982. function d3_rgb_hex(v) {
  983. return v < 0x10
  984. ? "0" + Math.max(0, v).toString(16)
  985. : Math.min(255, v).toString(16);
  986. }
  987. function d3_rgb_parse(format, rgb, hsl) {
  988. var r = 0, // red channel; int in [0, 255]
  989. g = 0, // green channel; int in [0, 255]
  990. b = 0, // blue channel; int in [0, 255]
  991. m1, // CSS color specification match
  992. m2, // CSS color specification type (e.g., rgb)
  993. name;
  994. /* Handle hsl, rgb. */
  995. m1 = /([a-z]+)\((.*)\)/i.exec(format);
  996. if (m1) {
  997. m2 = m1[2].split(",");
  998. switch (m1[1]) {
  999. case "hsl": {
  1000. return hsl(
  1001. parseFloat(m2[0]), // degrees
  1002. parseFloat(m2[1]) / 100, // percentage
  1003. parseFloat(m2[2]) / 100 // percentage
  1004. );
  1005. }
  1006. case "rgb": {
  1007. return rgb(
  1008. d3_rgb_parseNumber(m2[0]),
  1009. d3_rgb_parseNumber(m2[1]),
  1010. d3_rgb_parseNumber(m2[2])
  1011. );
  1012. }
  1013. }
  1014. }
  1015. /* Named colors. */
  1016. if (name = d3_rgb_names[format]) return rgb(name.r, name.g, name.b);
  1017. /* Hexadecimal colors: #rgb and #rrggbb. */
  1018. if (format != null && format.charAt(0) === "#") {
  1019. if (format.length === 4) {
  1020. r = format.charAt(1); r += r;
  1021. g = format.charAt(2); g += g;
  1022. b = format.charAt(3); b += b;
  1023. } else if (format.length === 7) {
  1024. r = format.substring(1, 3);
  1025. g = format.substring(3, 5);
  1026. b = format.substring(5, 7);
  1027. }
  1028. r = parseInt(r, 16);
  1029. g = parseInt(g, 16);
  1030. b = parseInt(b, 16);
  1031. }
  1032. return rgb(r, g, b);
  1033. }
  1034. function d3_rgb_hsl(r, g, b) {
  1035. var min = Math.min(r /= 255, g /= 255, b /= 255),
  1036. max = Math.max(r, g, b),
  1037. d = max - min,
  1038. h,
  1039. s,
  1040. l = (max + min) / 2;
  1041. if (d) {
  1042. s = l < .5 ? d / (max + min) : d / (2 - max - min);
  1043. if (r == max) h = (g - b) / d + (g < b ? 6 : 0);
  1044. else if (g == max) h = (b - r) / d + 2;
  1045. else h = (r - g) / d + 4;
  1046. h *= 60;
  1047. } else {
  1048. s = h = 0;
  1049. }
  1050. return d3_hsl(h, s, l);
  1051. }
  1052. function d3_rgb_parseNumber(c) { // either integer or percentage
  1053. var f = parseFloat(c);
  1054. return c.charAt(c.length - 1) === "%" ? Math.round(f * 2.55) : f;
  1055. }
  1056. var d3_rgb_names = {
  1057. aliceblue: "#f0f8ff",
  1058. antiquewhite: "#faebd7",
  1059. aqua: "#00ffff",
  1060. aquamarine: "#7fffd4",
  1061. azure: "#f0ffff",
  1062. beige: "#f5f5dc",
  1063. bisque: "#ffe4c4",
  1064. black: "#000000",
  1065. blanchedalmond: "#ffebcd",
  1066. blue: "#0000ff",
  1067. blueviolet: "#8a2be2",
  1068. brown: "#a52a2a",
  1069. burlywood: "#deb887",
  1070. cadetblue: "#5f9ea0",
  1071. chartreuse: "#7fff00",
  1072. chocolate: "#d2691e",
  1073. coral: "#ff7f50",
  1074. cornflowerblue: "#6495ed",
  1075. cornsilk: "#fff8dc",
  1076. crimson: "#dc143c",
  1077. cyan: "#00ffff",
  1078. darkblue: "#00008b",
  1079. darkcyan: "#008b8b",
  1080. darkgoldenrod: "#b8860b",
  1081. darkgray: "#a9a9a9",
  1082. darkgreen: "#006400",
  1083. darkgrey: "#a9a9a9",
  1084. darkkhaki: "#bdb76b",
  1085. darkmagenta: "#8b008b",
  1086. darkolivegreen: "#556b2f",
  1087. darkorange: "#ff8c00",
  1088. darkorchid: "#9932cc",
  1089. darkred: "#8b0000",
  1090. darksalmon: "#e9967a",
  1091. darkseagreen: "#8fbc8f",
  1092. darkslateblue: "#483d8b",
  1093. darkslategray: "#2f4f4f",
  1094. darkslategrey: "#2f4f4f",
  1095. darkturquoise: "#00ced1",
  1096. darkviolet: "#9400d3",
  1097. deeppink: "#ff1493",
  1098. deepskyblue: "#00bfff",
  1099. dimgray: "#696969",
  1100. dimgrey: "#696969",
  1101. dodgerblue: "#1e90ff",
  1102. firebrick: "#b22222",
  1103. floralwhite: "#fffaf0",
  1104. forestgreen: "#228b22",
  1105. fuchsia: "#ff00ff",
  1106. gainsboro: "#dcdcdc",
  1107. ghostwhite: "#f8f8ff",
  1108. gold: "#ffd700",
  1109. goldenrod: "#daa520",
  1110. gray: "#808080",
  1111. green: "#008000",
  1112. greenyellow: "#adff2f",
  1113. grey: "#808080",
  1114. honeydew: "#f0fff0",
  1115. hotpink: "#ff69b4",
  1116. indianred: "#cd5c5c",
  1117. indigo: "#4b0082",
  1118. ivory: "#fffff0",
  1119. khaki: "#f0e68c",
  1120. lavender: "#e6e6fa",
  1121. lavenderblush: "#fff0f5",
  1122. lawngreen: "#7cfc00",
  1123. lemonchiffon: "#fffacd",
  1124. lightblue: "#add8e6",
  1125. lightcoral: "#f08080",
  1126. lightcyan: "#e0ffff",
  1127. lightgoldenrodyellow: "#fafad2",
  1128. lightgray: "#d3d3d3",
  1129. lightgreen: "#90ee90",
  1130. lightgrey: "#d3d3d3",
  1131. lightpink: "#ffb6c1",
  1132. lightsalmon: "#ffa07a",
  1133. lightseagreen: "#20b2aa",
  1134. lightskyblue: "#87cefa",
  1135. lightslategray: "#778899",
  1136. lightslategrey: "#778899",
  1137. lightsteelblue: "#b0c4de",
  1138. lightyellow: "#ffffe0",
  1139. lime: "#00ff00",
  1140. limegreen: "#32cd32",
  1141. linen: "#faf0e6",
  1142. magenta: "#ff00ff",
  1143. maroon: "#800000",
  1144. mediumaquamarine: "#66cdaa",
  1145. mediumblue: "#0000cd",
  1146. mediumorchid: "#ba55d3",
  1147. mediumpurple: "#9370db",
  1148. mediumseagreen: "#3cb371",
  1149. mediumslateblue: "#7b68ee",
  1150. mediumspringgreen: "#00fa9a",
  1151. mediumturquoise: "#48d1cc",
  1152. mediumvioletred: "#c71585",
  1153. midnightblue: "#191970",
  1154. mintcream: "#f5fffa",
  1155. mistyrose: "#ffe4e1",
  1156. moccasin: "#ffe4b5",
  1157. navajowhite: "#ffdead",
  1158. navy: "#000080",
  1159. oldlace: "#fdf5e6",
  1160. olive: "#808000",
  1161. olivedrab: "#6b8e23",
  1162. orange: "#ffa500",
  1163. orangered: "#ff4500",
  1164. orchid: "#da70d6",
  1165. palegoldenrod: "#eee8aa",
  1166. palegreen: "#98fb98",
  1167. paleturquoise: "#afeeee",
  1168. palevioletred: "#db7093",
  1169. papayawhip: "#ffefd5",
  1170. peachpuff: "#ffdab9",
  1171. peru: "#cd853f",
  1172. pink: "#ffc0cb",
  1173. plum: "#dda0dd",
  1174. powderblue: "#b0e0e6",
  1175. purple: "#800080",
  1176. red: "#ff0000",
  1177. rosybrown: "#bc8f8f",
  1178. royalblue: "#4169e1",
  1179. saddlebrown: "#8b4513",
  1180. salmon: "#fa8072",
  1181. sandybrown: "#f4a460",
  1182. seagreen: "#2e8b57",
  1183. seashell: "#fff5ee",
  1184. sienna: "#a0522d",
  1185. silver: "#c0c0c0",
  1186. skyblue: "#87ceeb",
  1187. slateblue: "#6a5acd",
  1188. slategray: "#708090",
  1189. slategrey: "#708090",
  1190. snow: "#fffafa",
  1191. springgreen: "#00ff7f",
  1192. steelblue: "#4682b4",
  1193. tan: "#d2b48c",
  1194. teal: "#008080",
  1195. thistle: "#d8bfd8",
  1196. tomato: "#ff6347",
  1197. turquoise: "#40e0d0",
  1198. violet: "#ee82ee",
  1199. wheat: "#f5deb3",
  1200. white: "#ffffff",
  1201. whitesmoke: "#f5f5f5",
  1202. yellow: "#ffff00",
  1203. yellowgreen: "#9acd32"
  1204. };
  1205. for (var d3_rgb_name in d3_rgb_names) {
  1206. d3_rgb_names[d3_rgb_name] = d3_rgb_parse(
  1207. d3_rgb_names[d3_rgb_name],
  1208. d3_rgb,
  1209. d3_hsl_rgb);
  1210. }
  1211. d3.hsl = function(h, s, l) {
  1212. return arguments.length === 1
  1213. ? (h instanceof d3_Hsl ? d3_hsl(h.h, h.s, h.l)
  1214. : d3_rgb_parse("" + h, d3_rgb_hsl, d3_hsl))
  1215. : d3_hsl(+h, +s, +l);
  1216. };
  1217. function d3_hsl(h, s, l) {
  1218. return new d3_Hsl(h, s, l);
  1219. }
  1220. function d3_Hsl(h, s, l) {
  1221. this.h = h;
  1222. this.s = s;
  1223. this.l = l;
  1224. }
  1225. d3_Hsl.prototype.brighter = function(k) {
  1226. k = Math.pow(0.7, arguments.length ? k : 1);
  1227. return d3_hsl(this.h, this.s, this.l / k);
  1228. };
  1229. d3_Hsl.prototype.darker = function(k) {
  1230. k = Math.pow(0.7, arguments.length ? k : 1);
  1231. return d3_hsl(this.h, this.s, k * this.l);
  1232. };
  1233. d3_Hsl.prototype.rgb = function() {
  1234. return d3_hsl_rgb(this.h, this.s, this.l);
  1235. };
  1236. d3_Hsl.prototype.toString = function() {
  1237. return this.rgb().toString();
  1238. };
  1239. function d3_hsl_rgb(h, s, l) {
  1240. var m1,
  1241. m2;
  1242. /* Some simple corrections for h, s and l. */
  1243. h = h % 360; if (h < 0) h += 360;
  1244. s = s < 0 ? 0 : s > 1 ? 1 : s;
  1245. l = l < 0 ? 0 : l > 1 ? 1 : l;
  1246. /* From FvD 13.37, CSS Color Module Level 3 */
  1247. m2 = l <= .5 ? l * (1 + s) : l + s - l * s;
  1248. m1 = 2 * l - m2;
  1249. function v(h) {
  1250. if (h > 360) h -= 360;
  1251. else if (h < 0) h += 360;
  1252. if (h < 60) return m1 + (m2 - m1) * h / 60;
  1253. if (h < 180) return m2;
  1254. if (h < 240) return m1 + (m2 - m1) * (240 - h) / 60;
  1255. return m1;
  1256. }
  1257. function vv(h) {
  1258. return Math.round(v(h) * 255);
  1259. }
  1260. return d3_rgb(vv(h + 120), vv(h), vv(h - 120));
  1261. }
  1262. function d3_selection(groups) {
  1263. d3_arraySubclass(groups, d3_selectionPrototype);
  1264. return groups;
  1265. }
  1266. var d3_select = function(s, n) { return n.querySelector(s); },
  1267. d3_selectAll = function(s, n) { return n.querySelectorAll(s); },
  1268. d3_selectRoot = document.documentElement,
  1269. d3_selectMatcher = d3_selectRoot.matchesSelector || d3_selectRoot.webkitMatchesSelector || d3_selectRoot.mozMatchesSelector || d3_selectRoot.msMatchesSelector || d3_selectRoot.oMatchesSelector,
  1270. d3_selectMatches = function(n, s) { return d3_selectMatcher.call(n, s); };
  1271. // Prefer Sizzle, if available.
  1272. if (typeof Sizzle === "function") {
  1273. d3_select = function(s, n) { return Sizzle(s, n)[0]; };
  1274. d3_selectAll = function(s, n) { return Sizzle.uniqueSort(Sizzle(s, n)); };
  1275. d3_selectMatches = Sizzle.matchesSelector;
  1276. }
  1277. var d3_selectionPrototype = [];
  1278. d3.selection = function() {
  1279. return d3_selectionRoot;
  1280. };
  1281. d3.selection.prototype = d3_selectionPrototype;
  1282. d3_selectionPrototype.select = function(selector) {
  1283. var subgroups = [],
  1284. subgroup,
  1285. subnode,
  1286. group,
  1287. node;
  1288. if (typeof selector !== "function") selector = d3_selection_selector(selector);
  1289. for (var j = -1, m = this.length; ++j < m;) {
  1290. subgroups.push(subgroup = []);
  1291. subgroup.parentNode = (group = this[j]).parentNode;
  1292. for (var i = -1, n = group.length; ++i < n;) {
  1293. if (node = group[i]) {
  1294. subgroup.push(subnode = selector.call(node, node.__data__, i));
  1295. if (subnode && "__data__" in node) subnode.__data__ = node.__data__;
  1296. } else {
  1297. subgroup.push(null);
  1298. }
  1299. }
  1300. }
  1301. return d3_selection(subgroups);
  1302. };
  1303. function d3_selection_selector(selector) {
  1304. return function() {
  1305. return d3_select(selector, this);
  1306. };
  1307. }
  1308. d3_selectionPrototype.selectAll = function(selector) {
  1309. var subgroups = [],
  1310. subgroup,
  1311. node;
  1312. if (typeof selector !== "function") selector = d3_selection_selectorAll(selector);
  1313. for (var j = -1, m = this.length; ++j < m;) {
  1314. for (var group = this[j], i = -1, n = group.length; ++i < n;) {
  1315. if (node = group[i]) {
  1316. subgroups.push(subgroup = d3_array(selector.call(node, node.__data__, i)));
  1317. subgroup.parentNode = node;
  1318. }
  1319. }
  1320. }
  1321. return d3_selection(subgroups);
  1322. };
  1323. function d3_selection_selectorAll(selector) {
  1324. return function() {
  1325. return d3_selectAll(selector, this);
  1326. };
  1327. }
  1328. d3_selectionPrototype.attr = function(name, value) {
  1329. name = d3.ns.qualify(name);
  1330. // If no value is specified, return the first value.
  1331. if (arguments.length < 2) {
  1332. var node = this.node();
  1333. return name.local
  1334. ? node.getAttributeNS(name.space, name.local)
  1335. : node.getAttribute(name);
  1336. }
  1337. function attrNull() {
  1338. this.removeAttribute(name);
  1339. }
  1340. function attrNullNS() {
  1341. this.removeAttributeNS(name.space, name.local);
  1342. }
  1343. function attrConstant() {
  1344. this.setAttribute(name, value);
  1345. }
  1346. function attrConstantNS() {
  1347. this.setAttributeNS(name.space, name.local, value);
  1348. }
  1349. function attrFunction() {
  1350. var x = value.apply(this, arguments);
  1351. if (x == null) this.removeAttribute(name);
  1352. else this.setAttribute(name, x);
  1353. }
  1354. function attrFunctionNS() {
  1355. var x = value.apply(this, arguments);
  1356. if (x == null) this.removeAttributeNS(name.space, name.local);
  1357. else this.setAttributeNS(name.space, name.local, x);
  1358. }
  1359. return this.each(value == null
  1360. ? (name.local ? attrNullNS : attrNull) : (typeof value === "function"
  1361. ? (name.local ? attrFunctionNS : attrFunction)
  1362. : (name.local ? attrConstantNS : attrConstant)));
  1363. };
  1364. d3_selectionPrototype.classed = function(name, value) {
  1365. var names = name.split(d3_selection_classedWhitespace),
  1366. n = names.length,
  1367. i = -1;
  1368. if (arguments.length > 1) {
  1369. while (++i < n) d3_selection_classed.call(this, names[i], value);
  1370. return this;
  1371. } else {
  1372. while (++i < n) if (!d3_selection_classed.call(this, names[i])) return false;
  1373. return true;
  1374. }
  1375. };
  1376. var d3_selection_classedWhitespace = /\s+/g;
  1377. function d3_selection_classed(name, value) {
  1378. var re = new RegExp("(^|\\s+)" + d3.requote(name) + "(\\s+|$)", "g");
  1379. // If no value is specified, return the first value.
  1380. if (arguments.length < 2) {
  1381. var node = this.node();
  1382. if (c = node.classList) return c.contains(name);
  1383. var c = node.className;
  1384. re.lastIndex = 0;
  1385. return re.test(c.baseVal != null ? c.baseVal : c);
  1386. }
  1387. function classedAdd() {
  1388. if (c = this.classList) return c.add(name);
  1389. var c = this.className,
  1390. cb = c.baseVal != null,
  1391. cv = cb ? c.baseVal : c;
  1392. re.lastIndex = 0;
  1393. if (!re.test(cv)) {
  1394. cv = d3_collapse(cv + " " + name);
  1395. if (cb) c.baseVal = cv;
  1396. else this.className = cv;
  1397. }
  1398. }
  1399. function classedRemove() {
  1400. if (c = this.classList) return c.remove(name);
  1401. var c = this.className,
  1402. cb = c.baseVal != null,
  1403. cv = cb ? c.baseVal : c;
  1404. cv = d3_collapse(cv.replace(re, " "));
  1405. if (cb) c.baseVal = cv;
  1406. else this.className = cv;
  1407. }
  1408. function classedFunction() {
  1409. (value.apply(this, arguments)
  1410. ? classedAdd
  1411. : classedRemove).call(this);
  1412. }
  1413. return this.each(typeof value === "function"
  1414. ? classedFunction : value
  1415. ? classedAdd
  1416. : classedRemove);
  1417. }
  1418. d3_selectionPrototype.style = function(name, value, priority) {
  1419. if (arguments.length < 3) priority = "";
  1420. // If no value is specified, return the first value.
  1421. if (arguments.length < 2) return window
  1422. .getComputedStyle(this.node(), null)
  1423. .getPropertyValue(name);
  1424. function styleNull() {
  1425. this.style.removeProperty(name);
  1426. }
  1427. function styleConstant() {
  1428. this.style.setProperty(name, value, priority);
  1429. }
  1430. function styleFunction() {
  1431. var x = value.apply(this, arguments);
  1432. if (x == null) this.style.removeProperty(name);
  1433. else this.style.setProperty(name, x, priority);
  1434. }
  1435. return this.each(value == null
  1436. ? styleNull : (typeof value === "function"
  1437. ? styleFunction : styleConstant));
  1438. };
  1439. d3_selectionPrototype.property = function(name, value) {
  1440. // If no value is specified, return the first value.
  1441. if (arguments.length < 2) return this.node()[name];
  1442. function propertyNull() {
  1443. delete this[name];
  1444. }
  1445. function propertyConstant() {
  1446. this[name] = value;
  1447. }
  1448. function propertyFunction() {
  1449. var x = value.apply(this, arguments);
  1450. if (x == null) delete this[name];
  1451. else this[name] = x;
  1452. }
  1453. return this.each(value == null
  1454. ? propertyNull : (typeof value === "function"
  1455. ? propertyFunction : propertyConstant));
  1456. };
  1457. d3_selectionPrototype.text = function(value) {
  1458. return arguments.length < 1
  1459. ? this.node().textContent : this.each(typeof value === "function"
  1460. ? function() { var v = value.apply(this, arguments); this.textContent = v == null ? "" : v; } : value == null
  1461. ? function() { this.textContent = ""; }
  1462. : function() { this.textContent = value; });
  1463. };
  1464. d3_selectionPrototype.html = function(value) {
  1465. return arguments.length < 1
  1466. ? this.node().innerHTML : this.each(typeof value === "function"
  1467. ? function() { var v = value.apply(this, arguments); this.innerHTML = v == null ? "" : v; } : value == null
  1468. ? function() { this.innerHTML = ""; }
  1469. : function() { this.innerHTML = value; });
  1470. };
  1471. // TODO append(node)?
  1472. // TODO append(function)?
  1473. d3_selectionPrototype.append = function(name) {
  1474. name = d3.ns.qualify(name);
  1475. function append() {
  1476. return this.appendChild(document.createElementNS(this.namespaceURI, name));
  1477. }
  1478. function appendNS() {
  1479. return this.appendChild(document.createElementNS(name.space, name.local));
  1480. }
  1481. return this.select(name.local ? appendNS : append);
  1482. };
  1483. // TODO insert(node, function)?
  1484. // TODO insert(function, string)?
  1485. // TODO insert(function, function)?
  1486. d3_selectionPrototype.insert = function(name, before) {
  1487. name = d3.ns.qualify(name);
  1488. function insert() {
  1489. return this.insertBefore(
  1490. document.createElementNS(this.namespaceURI, name),
  1491. d3_select(before, this));
  1492. }
  1493. function insertNS() {
  1494. return this.insertBefore(
  1495. document.createElementNS(name.space, name.local),
  1496. d3_select(before, this));
  1497. }
  1498. return this.select(name.local ? insertNS : insert);
  1499. };
  1500. // TODO remove(selector)?
  1501. // TODO remove(node)?
  1502. // TODO remove(function)?
  1503. d3_selectionPrototype.remove = function() {
  1504. return this.each(function() {
  1505. var parent = this.parentNode;
  1506. if (parent) parent.removeChild(this);
  1507. });
  1508. };
  1509. // TODO data(null) for clearing data?
  1510. d3_selectionPrototype.data = function(data, join) {
  1511. var enter = [],
  1512. update = [],
  1513. exit = [];
  1514. function bind(group, groupData) {
  1515. var i,
  1516. n = group.length,
  1517. m = groupData.length,
  1518. n0 = Math.min(n, m),
  1519. n1 = Math.max(n, m),
  1520. updateNodes = [],
  1521. enterNodes = [],
  1522. exitNodes = [],
  1523. node,
  1524. nodeData;
  1525. if (join) {
  1526. var nodeByKey = {},
  1527. keys = [],
  1528. key,
  1529. j = groupData.length;
  1530. for (i = -1; ++i < n;) {
  1531. key = join.call(node = group[i], node.__data__, i);
  1532. if (key in nodeByKey) {
  1533. exitNodes[j++] = node; // duplicate key
  1534. } else {
  1535. nodeByKey[key] = node;
  1536. }
  1537. keys.push(key);
  1538. }
  1539. for (i = -1; ++i < m;) {
  1540. node = nodeByKey[key = join.call(groupData, nodeData = groupData[i], i)];
  1541. if (node) {
  1542. node.__data__ = nodeData;
  1543. updateNodes[i] = node;
  1544. enterNodes[i] = exitNodes[i] = null;
  1545. } else {
  1546. enterNodes[i] = d3_selection_dataNode(nodeData);
  1547. updateNodes[i] = exitNodes[i] = null;
  1548. }
  1549. delete nodeByKey[key];
  1550. }
  1551. for (i = -1; ++i < n;) {
  1552. if (keys[i] in nodeByKey) {
  1553. exitNodes[i] = group[i];
  1554. }
  1555. }
  1556. } else {
  1557. for (i = -1; ++i < n0;) {
  1558. node = group[i];
  1559. nodeData = groupData[i];
  1560. if (node) {
  1561. node.__data__ = nodeData;
  1562. updateNodes[i] = node;
  1563. enterNodes[i] = exitNodes[i] = null;
  1564. } else {
  1565. enterNodes[i] = d3_selection_dataNode(nodeData);
  1566. updateNodes[i] = exitNodes[i] = null;
  1567. }
  1568. }
  1569. for (; i < m; ++i) {
  1570. enterNodes[i] = d3_selection_dataNode(groupData[i]);
  1571. updateNodes[i] = exitNodes[i] = null;
  1572. }
  1573. for (; i < n1; ++i) {
  1574. exitNodes[i] = group[i];
  1575. enterNodes[i] = updateNodes[i] = null;
  1576. }
  1577. }
  1578. enterNodes.update
  1579. = updateNodes;
  1580. enterNodes.parentNode
  1581. = updateNodes.parentNode
  1582. = exitNodes.parentNode
  1583. = group.parentNode;
  1584. enter.push(enterNodes);
  1585. update.push(updateNodes);
  1586. exit.push(exitNodes);
  1587. }
  1588. var i = -1,
  1589. n = this.length,
  1590. group;
  1591. if (typeof data === "function") {
  1592. while (++i < n) {
  1593. bind(group = this[i], data.call(group, group.parentNode.__data__, i));
  1594. }
  1595. } else {
  1596. while (++i < n) {
  1597. bind(group = this[i], data);
  1598. }
  1599. }
  1600. var selection = d3_selection(update);
  1601. selection.enter = function() { return d3_selection_enter(enter); };
  1602. selection.exit = function() { return d3_selection(exit); };
  1603. return selection;
  1604. };
  1605. function d3_selection_dataNode(data) {
  1606. return {__data__: data};
  1607. }
  1608. d3_selectionPrototype.filter = function(filter) {
  1609. var subgroups = [],
  1610. subgroup,
  1611. group,
  1612. node;
  1613. if (typeof filter !== "function") filter = d3_selection_filter(filter);
  1614. for (var j = 0, m = this.length; j < m; j++) {
  1615. subgroups.push(subgroup = []);
  1616. subgroup.parentNode = (group = this[j]).parentNode;
  1617. for (var i = 0, n = group.length; i < n; i++) {
  1618. if ((node = group[i]) && filter.call(node, node.__data__, i)) {
  1619. subgroup.push(node);
  1620. }
  1621. }
  1622. }
  1623. return d3_selection(subgroups);
  1624. };
  1625. function d3_selection_filter(selector) {
  1626. return function() {
  1627. return d3_selectMatches(this, selector);
  1628. };
  1629. }
  1630. d3_selectionPrototype.map = function(map) {
  1631. return this.each(function() {
  1632. this.__data__ = map.apply(this, arguments);
  1633. });
  1634. };
  1635. d3_selectionPrototype.order = function() {
  1636. for (var j = -1, m = this.length; ++j < m;) {
  1637. for (var group = this[j], i = group.length - 1, next = group[i], node; --i >= 0;) {
  1638. if (node = group[i]) {
  1639. if (next && next !== node.nextSibling) next.parentNode.insertBefore(node, next);
  1640. next = node;
  1641. }
  1642. }
  1643. }
  1644. return this;
  1645. };
  1646. d3_selectionPrototype.sort = function(comparator) {
  1647. comparator = d3_selection_sortComparator.apply(this, arguments);
  1648. for (var j = -1, m = this.length; ++j < m;) this[j].sort(comparator);
  1649. return this.order();
  1650. };
  1651. function d3_selection_sortComparator(comparator) {
  1652. if (!arguments.length) comparator = d3.ascending;
  1653. return function(a, b) {
  1654. return comparator(a && a.__data__, b && b.__data__);
  1655. };
  1656. }
  1657. // type can be namespaced, e.g., "click.foo"
  1658. // listener can be null for removal
  1659. d3_selectionPrototype.on = function(type, listener, capture) {
  1660. if (arguments.length < 3) capture = false;
  1661. // parse the type specifier
  1662. var name = "__on" + type, i = type.indexOf(".");
  1663. if (i > 0) type = type.substring(0, i);
  1664. // if called with only one argument, return the current listener
  1665. if (arguments.length < 2) return (i = this.node()[name]) && i._;
  1666. // remove the old event listener, and add the new event listener
  1667. return this.each(function(d, i) {
  1668. var node = this;
  1669. if (node[name]) node.removeEventListener(type, node[name], capture);
  1670. if (listener) node.addEventListener(type, node[name] = l, capture);
  1671. // wrapped event listener that preserves i
  1672. function l(e) {
  1673. var o = d3.event; // Events can be reentrant (e.g., focus).
  1674. d3.event = e;
  1675. try {
  1676. listener.call(node, node.__data__, i);
  1677. } finally {
  1678. d3.event = o;
  1679. }
  1680. }
  1681. // stash the unwrapped listener for retrieval
  1682. l._ = listener;
  1683. });
  1684. };
  1685. d3_selectionPrototype.each = function(callback) {
  1686. for (var j = -1, m = this.length; ++j < m;) {
  1687. for (var group = this[j], i = -1, n = group.length; ++i < n;) {
  1688. var node = group[i];
  1689. if (node) callback.call(node, node.__data__, i, j);
  1690. }
  1691. }
  1692. return this;
  1693. };
  1694. //
  1695. // Note: assigning to the arguments array simultaneously changes the value of
  1696. // the corresponding argument!
  1697. //
  1698. // TODO The `this` argument probably shouldn't be the first argument to the
  1699. // callback, anyway, since it's redundant. However, that will require a major
  1700. // version bump due to backwards compatibility, so I'm not changing it right
  1701. // away.
  1702. //
  1703. d3_selectionPrototype.call = function(callback) {
  1704. callback.apply(this, (arguments[0] = this, arguments));
  1705. return this;
  1706. };
  1707. d3_selectionPrototype.empty = function() {
  1708. return !this.node();
  1709. };
  1710. d3_selectionPrototype.node = function(callback) {
  1711. for (var j = 0, m = this.length; j < m; j++) {
  1712. for (var group = this[j], i = 0, n = group.length; i < n; i++) {
  1713. var node = group[i];
  1714. if (node) return node;
  1715. }
  1716. }
  1717. return null;
  1718. };
  1719. d3_selectionPrototype.transition = function() {
  1720. var subgroups = [],
  1721. subgroup,
  1722. node;
  1723. for (var j = -1, m = this.length; ++j < m;) {
  1724. subgroups.push(subgroup = []);
  1725. for (var group = this[j], i = -1, n = group.length; ++i < n;) {
  1726. subgroup.push((node = group[i]) ? {node: node, delay: 0, duration: 250} : null);
  1727. }
  1728. }
  1729. return d3_transition(subgroups, d3_transitionInheritId || ++d3_transitionId, Date.now());
  1730. };
  1731. var d3_selectionRoot = d3_selection([[document]]);
  1732. d3_selectionRoot[0].parentNode = d3_selectRoot;
  1733. // TODO fast singleton implementation!
  1734. // TODO select(function)
  1735. d3.select = function(selector) {
  1736. return typeof selector === "string"
  1737. ? d3_selectionRoot.select(selector)
  1738. : d3_selection([[selector]]); // assume node
  1739. };
  1740. // TODO selectAll(function)
  1741. d3.selectAll = function(selector) {
  1742. return typeof selector === "string"
  1743. ? d3_selectionRoot.selectAll(selector)
  1744. : d3_selection([d3_array(selector)]); // assume node[]
  1745. };
  1746. function d3_selection_enter(selection) {
  1747. d3_arraySubclass(selection, d3_selection_enterPrototype);
  1748. return selection;
  1749. }
  1750. var d3_selection_enterPrototype = [];
  1751. d3_selection_enterPrototype.append = d3_selectionPrototype.append;
  1752. d3_selection_enterPrototype.insert = d3_selectionPrototype.insert;
  1753. d3_selection_enterPrototype.empty = d3_selectionPrototype.empty;
  1754. d3_selection_enterPrototype.node = d3_selectionPrototype.node;
  1755. d3_selection_enterPrototype.select = function(selector) {
  1756. var subgroups = [],
  1757. subgroup,
  1758. subnode,
  1759. upgroup,
  1760. group,
  1761. node;
  1762. for (var j = -1, m = this.length; ++j < m;) {
  1763. upgroup = (group = this[j]).update;
  1764. subgroups.push(subgroup = []);
  1765. subgroup.parentNode = group.parentNode;
  1766. for (var i = -1, n = group.length; ++i < n;) {
  1767. if (node = group[i]) {
  1768. subgroup.push(upgroup[i] = subnode = selector.call(group.parentNode, node.__data__, i));
  1769. subnode.__data__ = node.__data__;
  1770. } else {
  1771. subgroup.push(null);
  1772. }
  1773. }
  1774. }
  1775. return d3_selection(subgroups);
  1776. };
  1777. function d3_transition(groups, id, time) {
  1778. d3_arraySubclass(groups, d3_transitionPrototype);
  1779. var tweens = {},
  1780. event = d3.dispatch("start", "end"),
  1781. ease = d3_transitionEase;
  1782. groups.id = id;
  1783. groups.time = time;
  1784. groups.tween = function(name, tween) {
  1785. if (arguments.length < 2) return tweens[name];
  1786. if (tween == null) delete tweens[name];
  1787. else tweens[name] = tween;
  1788. return groups;
  1789. };
  1790. groups.ease = function(value) {
  1791. if (!arguments.length) return ease;
  1792. ease = typeof value === "function" ? value : d3.ease.apply(d3, arguments);
  1793. return groups;
  1794. };
  1795. groups.each = function(type, listener) {
  1796. if (arguments.length < 2) return d3_transition_each.call(groups, type);
  1797. event.on(type, listener);
  1798. return groups;
  1799. };
  1800. d3.timer(function(elapsed) {
  1801. groups.each(function(d, i, j) {
  1802. var tweened = [],
  1803. node = this,
  1804. delay = groups[j][i].delay,
  1805. duration = groups[j][i].duration,
  1806. lock = node.__transition__ || (node.__transition__ = {active: 0, count: 0});
  1807. ++lock.count;
  1808. delay <= elapsed ? start(elapsed) : d3.timer(start, delay, time);
  1809. function start(elapsed) {
  1810. if (lock.active > id) return stop();
  1811. lock.active = id;
  1812. for (var tween in tweens) {
  1813. if (tween = tweens[tween].call(node, d, i)) {
  1814. tweened.push(tween);
  1815. }
  1816. }
  1817. event.start.call(node, d, i);
  1818. if (!tick(elapsed)) d3.timer(tick, 0, time);
  1819. return 1;
  1820. }
  1821. function tick(elapsed) {
  1822. if (lock.active !== id) return stop();
  1823. var t = (elapsed - delay) / duration,
  1824. e = ease(t),
  1825. n = tweened.length;
  1826. while (n > 0) {
  1827. tweened[--n].call(node, e);
  1828. }
  1829. if (t >= 1) {
  1830. stop();
  1831. d3_transitionInheritId = id;
  1832. event.end.call(node, d, i);
  1833. d3_transitionInheritId = 0;
  1834. return 1;
  1835. }
  1836. }
  1837. function stop() {
  1838. if (!--lock.count) delete node.__transition__;
  1839. return 1;
  1840. }
  1841. });
  1842. return 1;
  1843. }, 0, time);
  1844. return groups;
  1845. }
  1846. var d3_transitionRemove = {};
  1847. function d3_transitionNull(d, i, a) {
  1848. return a != "" && d3_transitionRemove;
  1849. }
  1850. function d3_transitionTween(name, b) {
  1851. var interpolate = d3_interpolateByName(name);
  1852. function transitionFunction(d, i, a) {
  1853. var v = b.call(this, d, i);
  1854. return v == null
  1855. ? a != "" && d3_transitionRemove
  1856. : a != v && interpolate(a, v);
  1857. }
  1858. function transitionString(d, i, a) {
  1859. return a != b && interpolate(a, b);
  1860. }
  1861. return typeof b === "function" ? transitionFunction
  1862. : b == null ? d3_transitionNull
  1863. : (b += "", transitionString);
  1864. }
  1865. var d3_transitionPrototype = [],
  1866. d3_transitionId = 0,
  1867. d3_transitionInheritId = 0,
  1868. d3_transitionEase = d3.ease("cubic-in-out");
  1869. d3_transitionPrototype.call = d3_selectionPrototype.call;
  1870. d3.transition = function() {
  1871. return d3_selectionRoot.transition();
  1872. };
  1873. d3.transition.prototype = d3_transitionPrototype;
  1874. d3_transitionPrototype.select = function(selector) {
  1875. var subgroups = [],
  1876. subgroup,
  1877. subnode,
  1878. node;
  1879. if (typeof selector !== "function") selector = d3_selection_selector(selector);
  1880. for (var j = -1, m = this.length; ++j < m;) {
  1881. subgroups.push(subgroup = []);
  1882. for (var group = this[j], i = -1, n = group.length; ++i < n;) {
  1883. if ((node = group[i]) && (subnode = selector.call(node.node, node.node.__data__, i))) {
  1884. if ("__data__" in node.node) subnode.__data__ = node.node.__data__;
  1885. subgroup.push({node: subnode, delay: node.delay, duration: node.duration});
  1886. } else {
  1887. subgroup.push(null);
  1888. }
  1889. }
  1890. }
  1891. return d3_transition(subgroups, this.id, this.time).ease(this.ease());
  1892. };
  1893. d3_transitionPrototype.selectAll = function(selector) {
  1894. var subgroups = [],
  1895. subgroup,
  1896. subnodes,
  1897. node;
  1898. if (typeof selector !== "function") selector = d3_selection_selectorAll(selector);
  1899. for (var j = -1, m = this.length; ++j < m;) {
  1900. for (var group = this[j], i = -1, n = group.length; ++i < n;) {
  1901. if (node = group[i]) {
  1902. subnodes = selector.call(node.node, node.node.__data__, i);
  1903. subgroups.push(subgroup = []);
  1904. for (var k = -1, o = subnodes.length; ++k < o;) {
  1905. subgroup.push({node: subnodes[k], delay: node.delay, duration: node.duration});
  1906. }
  1907. }
  1908. }
  1909. }
  1910. return d3_transition(subgroups, this.id, this.time).ease(this.ease());
  1911. };
  1912. d3_transitionPrototype.attr = function(name, value) {
  1913. return this.attrTween(name, d3_transitionTween(name, value));
  1914. };
  1915. d3_transitionPrototype.attrTween = function(nameNS, tween) {
  1916. var name = d3.ns.qualify(nameNS);
  1917. function attrTween(d, i) {
  1918. var f = tween.call(this, d, i, this.getAttribute(name));
  1919. return f === d3_transitionRemove
  1920. ? (this.removeAttribute(name), null)
  1921. : f && function(t) { this.setAttribute(name, f(t)); };
  1922. }
  1923. function attrTweenNS(d, i) {
  1924. var f = tween.call(this, d, i, this.getAttributeNS(name.space, name.local));
  1925. return f === d3_transitionRemove
  1926. ? (this.removeAttributeNS(name.space, name.local), null)
  1927. : f && function(t) { this.setAttributeNS(name.space, name.local, f(t)); };
  1928. }
  1929. return this.tween("attr." + nameNS, name.local ? attrTweenNS : attrTween);
  1930. };
  1931. d3_transitionPrototype.style = function(name, value, priority) {
  1932. if (arguments.length < 3) priority = "";
  1933. return this.styleTween(name, d3_transitionTween(name, value), priority);
  1934. };
  1935. d3_transitionPrototype.styleTween = function(name, tween, priority) {
  1936. if (arguments.length < 3) priority = "";
  1937. return this.tween("style." + name, function(d, i) {
  1938. var f = tween.call(this, d, i, window.getComputedStyle(this, null).getPropertyValue(name));
  1939. return f === d3_transitionRemove
  1940. ? (this.style.removeProperty(name), null)
  1941. : f && function(t) { this.style.setProperty(name, f(t), priority); };
  1942. });
  1943. };
  1944. d3_transitionPrototype.text = function(value) {
  1945. return this.tween("text", function(d, i) {
  1946. this.textContent = typeof value === "function"
  1947. ? value.call(this, d, i)
  1948. : value;
  1949. });
  1950. };
  1951. d3_transitionPrototype.remove = function() {
  1952. return this.each("end.transition", function() {
  1953. var p;
  1954. if (!this.__transition__ && (p = this.parentNode)) p.removeChild(this);
  1955. });
  1956. };
  1957. d3_transitionPrototype.delay = function(value) {
  1958. var groups = this;
  1959. return groups.each(typeof value === "function"
  1960. ? function(d, i, j) { groups[j][i].delay = +value.apply(this, arguments); }
  1961. : (value = +value, function(d, i, j) { groups[j][i].delay = value; }));
  1962. };
  1963. d3_transitionPrototype.duration = function(value) {
  1964. var groups = this;
  1965. return groups.each(typeof value === "function"
  1966. ? function(d, i, j) { groups[j][i].duration = +value.apply(this, arguments); }
  1967. : (value = +value, function(d, i, j) { groups[j][i].duration = value; }));
  1968. };
  1969. function d3_transition_each(callback) {
  1970. for (var j = 0, m = this.length; j < m; j++) {
  1971. for (var group = this[j], i = 0, n = group.length; i < n; i++) {
  1972. var node = group[i];
  1973. if (node) callback.call(node = node.node, node.__data__, i, j);
  1974. }
  1975. }
  1976. return this;
  1977. }
  1978. d3_transitionPrototype.transition = function() {
  1979. return this.select(d3_this);
  1980. };
  1981. var d3_timer_queue = null,
  1982. d3_timer_interval, // is an interval (or frame) active?
  1983. d3_timer_timeout; // is a timeout active?
  1984. // The timer will continue to fire until callback returns true.
  1985. d3.timer = function(callback, delay, then) {
  1986. var found = false,
  1987. t0,
  1988. t1 = d3_timer_queue;
  1989. if (arguments.length < 3) {
  1990. if (arguments.length < 2) delay = 0;
  1991. else if (!isFinite(delay)) return;
  1992. then = Date.now();
  1993. }
  1994. // See if the callback's already in the queue.
  1995. while (t1) {
  1996. if (t1.callback === callback) {
  1997. t1.then = then;
  1998. t1.delay = delay;
  1999. found = true;
  2000. break;
  2001. }
  2002. t0 = t1;
  2003. t1 = t1.next;
  2004. }
  2005. // Otherwise, add the callback to the queue.
  2006. if (!found) d3_timer_queue = {
  2007. callback: callback,
  2008. then: then,
  2009. delay: delay,
  2010. next: d3_timer_queue
  2011. };
  2012. // Start animatin'!
  2013. if (!d3_timer_interval) {
  2014. d3_timer_timeout = clearTimeout(d3_timer_timeout);
  2015. d3_timer_interval = 1;
  2016. d3_timer_frame(d3_timer_step);
  2017. }
  2018. }
  2019. function d3_timer_step() {
  2020. var elapsed,
  2021. now = Date.now(),
  2022. t1 = d3_timer_queue;
  2023. while (t1) {
  2024. elapsed = now - t1.then;
  2025. if (elapsed >= t1.delay) t1.flush = t1.callback(elapsed);
  2026. t1 = t1.next;
  2027. }
  2028. var delay = d3_timer_flush() - now;
  2029. if (delay > 24) {
  2030. if (isFinite(delay)) {
  2031. clearTimeout(d3_timer_timeout);
  2032. d3_timer_timeout = setTimeout(d3_timer_step, delay);
  2033. }
  2034. d3_timer_interval = 0;
  2035. } else {
  2036. d3_timer_interval = 1;
  2037. d3_timer_frame(d3_timer_step);
  2038. }
  2039. }
  2040. d3.timer.flush = function() {
  2041. var elapsed,
  2042. now = Date.now(),
  2043. t1 = d3_timer_queue;
  2044. while (t1) {
  2045. elapsed = now - t1.then;
  2046. if (!t1.delay) t1.flush = t1.callback(elapsed);
  2047. t1 = t1.next;
  2048. }
  2049. d3_timer_flush();
  2050. };
  2051. // Flush after callbacks, to avoid concurrent queue modification.
  2052. function d3_timer_flush() {
  2053. var t0 = null,
  2054. t1 = d3_timer_queue,
  2055. then = Infinity;
  2056. while (t1) {
  2057. if (t1.flush) {
  2058. t1 = t0 ? t0.next = t1.next : d3_timer_queue = t1.next;
  2059. } else {
  2060. then = Math.min(then, t1.then + t1.delay);
  2061. t1 = (t0 = t1).next;
  2062. }
  2063. }
  2064. return then;
  2065. }
  2066. var d3_timer_frame = window.requestAnimationFrame
  2067. || window.webkitRequestAnimationFrame
  2068. || window.mozRequestAnimationFrame
  2069. || window.oRequestAnimationFrame
  2070. || window.msRequestAnimationFrame
  2071. || function(callback) { setTimeout(callback, 17); };
  2072. d3.transform = function(string) {
  2073. var g = document.createElementNS(d3.ns.prefix.svg, "g"),
  2074. identity = {a: 1, b: 0, c: 0, d: 1, e: 0, f: 0};
  2075. return (d3.transform = function(string) {
  2076. g.setAttribute("transform", string);
  2077. var t = g.transform.baseVal.consolidate();
  2078. return new d3_transform(t ? t.matrix : identity);
  2079. })(string);
  2080. };
  2081. // Compute x-scale and normalize the first row.
  2082. // Compute shear and make second row orthogonal to first.
  2083. // Compute y-scale and normalize the second row.
  2084. // Finally, compute the rotation.
  2085. function d3_transform(m) {
  2086. var r0 = [m.a, m.b],
  2087. r1 = [m.c, m.d],
  2088. kx = d3_transformNormalize(r0),
  2089. kz = d3_transformDot(r0, r1),
  2090. ky = d3_transformNormalize(d3_transformCombine(r1, r0, -kz)) || 0;
  2091. if (r0[0] * r1[1] < r1[0] * r0[1]) {
  2092. r0[0] *= -1;
  2093. r0[1] *= -1;
  2094. kx *= -1;
  2095. kz *= -1;
  2096. }
  2097. this.rotate = (kx ? Math.atan2(r0[1], r0[0]) : Math.atan2(-r1[0], r1[1])) * d3_transformDegrees;
  2098. this.translate = [m.e, m.f];
  2099. this.scale = [kx, ky];
  2100. this.skew = ky ? Math.atan2(kz, ky) * d3_transformDegrees : 0;
  2101. };
  2102. d3_transform.prototype.toString = function() {
  2103. return "translate(" + this.translate
  2104. + ")rotate(" + this.rotate
  2105. + ")skewX(" + this.skew
  2106. + ")scale(" + this.scale
  2107. + ")";
  2108. };
  2109. function d3_transformDot(a, b) {
  2110. return a[0] * b[0] + a[1] * b[1];
  2111. }
  2112. function d3_transformNormalize(a) {
  2113. var k = Math.sqrt(d3_transformDot(a, a));
  2114. if (k) {
  2115. a[0] /= k;
  2116. a[1] /= k;
  2117. }
  2118. return k;
  2119. }
  2120. function d3_transformCombine(a, b, k) {
  2121. a[0] += k * b[0];
  2122. a[1] += k * b[1];
  2123. return a;
  2124. }
  2125. var d3_transformDegrees = 180 / Math.PI;
  2126. function d3_noop() {}
  2127. d3.scale = {};
  2128. function d3_scaleExtent(domain) {
  2129. var start = domain[0], stop = domain[domain.length - 1];
  2130. return start < stop ? [start, stop] : [stop, start];
  2131. }
  2132. function d3_scaleRange(scale) {
  2133. return scale.rangeExtent ? scale.rangeExtent() : d3_scaleExtent(scale.range());
  2134. }
  2135. function d3_scale_nice(domain, nice) {
  2136. var i0 = 0,
  2137. i1 = domain.length - 1,
  2138. x0 = domain[i0],
  2139. x1 = domain[i1],
  2140. dx;
  2141. if (x1 < x0) {
  2142. dx = i0; i0 = i1; i1 = dx;
  2143. dx = x0; x0 = x1; x1 = dx;
  2144. }
  2145. if (dx = x1 - x0) {
  2146. nice = nice(dx);
  2147. domain[i0] = nice.floor(x0);
  2148. domain[i1] = nice.ceil(x1);
  2149. }
  2150. return domain;
  2151. }
  2152. function d3_scale_niceDefault() {
  2153. return Math;
  2154. }
  2155. d3.scale.linear = function() {
  2156. return d3_scale_linear([0, 1], [0, 1], d3.interpolate, false);
  2157. };
  2158. function d3_scale_linear(domain, range, interpolate, clamp) {
  2159. var output,
  2160. input;
  2161. function rescale() {
  2162. var linear = domain.length == 2 ? d3_scale_bilinear : d3_scale_polylinear,
  2163. uninterpolate = clamp ? d3_uninterpolateClamp : d3_uninterpolateNumber;
  2164. output = linear(domain, range, uninterpolate, interpolate);
  2165. input = linear(range, domain, uninterpolate, d3.interpolate);
  2166. return scale;
  2167. }
  2168. function scale(x) {
  2169. return output(x);
  2170. }
  2171. // Note: requires range is coercible to number!
  2172. scale.invert = function(y) {
  2173. return input(y);
  2174. };
  2175. scale.domain = function(x) {
  2176. if (!arguments.length) return domain;
  2177. domain = x.map(Number);
  2178. return rescale();
  2179. };
  2180. scale.range = function(x) {
  2181. if (!arguments.length) return range;
  2182. range = x;
  2183. return rescale();
  2184. };
  2185. scale.rangeRound = function(x) {
  2186. return scale.range(x).interpolate(d3.interpolateRound);
  2187. };
  2188. scale.clamp = function(x) {
  2189. if (!arguments.length) return clamp;
  2190. clamp = x;
  2191. return rescale();
  2192. };
  2193. scale.interpolate = function(x) {
  2194. if (!arguments.length) return interpolate;
  2195. interpolate = x;
  2196. return rescale();
  2197. };
  2198. scale.ticks = function(m) {
  2199. return d3_scale_linearTicks(domain, m);
  2200. };
  2201. scale.tickFormat = function(m) {
  2202. return d3_scale_linearTickFormat(domain, m);
  2203. };
  2204. scale.nice = function() {
  2205. d3_scale_nice(domain, d3_scale_linearNice);
  2206. return rescale();
  2207. };
  2208. scale.copy = function() {
  2209. return d3_scale_linear(domain, range, interpolate, clamp);
  2210. };
  2211. return rescale();
  2212. };
  2213. function d3_scale_linearRebind(scale, linear) {
  2214. return d3.rebind(scale, linear, "range", "rangeRound", "interpolate", "clamp");
  2215. }
  2216. function d3_scale_linearNice(dx) {
  2217. dx = Math.pow(10, Math.round(Math.log(dx) / Math.LN10) - 1);
  2218. return {
  2219. floor: function(x) { return Math.floor(x / dx) * dx; },
  2220. ceil: function(x) { return Math.ceil(x / dx) * dx; }
  2221. };
  2222. }
  2223. function d3_scale_linearTickRange(domain, m) {
  2224. var extent = d3_scaleExtent(domain),
  2225. span = extent[1] - extent[0],
  2226. step = Math.pow(10, Math.floor(Math.log(span / m) / Math.LN10)),
  2227. err = m / span * step;
  2228. // Filter ticks to get closer to the desired count.
  2229. if (err <= .15) step *= 10;
  2230. else if (err <= .35) step *= 5;
  2231. else if (err <= .75) step *= 2;
  2232. // Round start and stop values to step interval.
  2233. extent[0] = Math.ceil(extent[0] / step) * step;
  2234. extent[1] = Math.floor(extent[1] / step) * step + step * .5; // inclusive
  2235. extent[2] = step;
  2236. return extent;
  2237. }
  2238. function d3_scale_linearTicks(domain, m) {
  2239. return d3.range.apply(d3, d3_scale_linearTickRange(domain, m));
  2240. }
  2241. function d3_scale_linearTickFormat(domain, m) {
  2242. return d3.format(",." + Math.max(0, -Math.floor(Math.log(d3_scale_linearTickRange(domain, m)[2]) / Math.LN10 + .01)) + "f");
  2243. }
  2244. function d3_scale_bilinear(domain, range, uninterpolate, interpolate) {
  2245. var u = uninterpolate(domain[0], domain[1]),
  2246. i = interpolate(range[0], range[1]);
  2247. return function(x) {
  2248. return i(u(x));
  2249. };
  2250. }
  2251. function d3_scale_polylinear(domain, range, uninterpolate, interpolate) {
  2252. var u = [],
  2253. i = [],
  2254. j = 0,
  2255. k = domain.length - 1;
  2256. // Handle descending domains.
  2257. if (domain[k] < domain[0]) {
  2258. domain = domain.slice().reverse();
  2259. range = range.slice().reverse();
  2260. }
  2261. while (++j <= k) {
  2262. u.push(uninterpolate(domain[j - 1], domain[j]));
  2263. i.push(interpolate(range[j - 1], range[j]));
  2264. }
  2265. return function(x) {
  2266. var j = d3.bisect(domain, x, 1, k) - 1;
  2267. return i[j](u[j](x));
  2268. };
  2269. }
  2270. d3.scale.log = function() {
  2271. return d3_scale_log(d3.scale.linear(), d3_scale_logp);
  2272. };
  2273. function d3_scale_log(linear, log) {
  2274. var pow = log.pow;
  2275. function scale(x) {
  2276. return linear(log(x));
  2277. }
  2278. scale.invert = function(x) {
  2279. return pow(linear.invert(x));
  2280. };
  2281. scale.domain = function(x) {
  2282. if (!arguments.length) return linear.domain().map(pow);
  2283. log = x[0] < 0 ? d3_scale_logn : d3_scale_logp;
  2284. pow = log.pow;
  2285. linear.domain(x.map(log));
  2286. return scale;
  2287. };
  2288. scale.nice = function() {
  2289. linear.domain(d3_scale_nice(linear.domain(), d3_scale_niceDefault));
  2290. return scale;
  2291. };
  2292. scale.ticks = function() {
  2293. var extent = d3_scaleExtent(linear.domain()),
  2294. ticks = [];
  2295. if (extent.every(isFinite)) {
  2296. var i = Math.floor(extent[0]),
  2297. j = Math.ceil(extent[1]),
  2298. u = pow(extent[0]),
  2299. v = pow(extent[1]);
  2300. if (log === d3_scale_logn) {
  2301. ticks.push(pow(i));
  2302. for (; i++ < j;) for (var k = 9; k > 0; k--) ticks.push(pow(i) * k);
  2303. } else {
  2304. for (; i < j; i++) for (var k = 1; k < 10; k++) ticks.push(pow(i) * k);
  2305. ticks.push(pow(i));
  2306. }
  2307. for (i = 0; ticks[i] < u; i++) {} // strip small values
  2308. for (j = ticks.length; ticks[j - 1] > v; j--) {} // strip big values
  2309. ticks = ticks.slice(i, j);
  2310. }
  2311. return ticks;
  2312. };
  2313. scale.tickFormat = function(n, format) {
  2314. if (arguments.length < 2) format = d3_scale_logFormat;
  2315. if (arguments.length < 1) return format;
  2316. var k = n / scale.ticks().length,
  2317. f = log === d3_scale_logn ? (e = -1e-12, Math.floor) : (e = 1e-12, Math.ceil),
  2318. e;
  2319. return function(d) {
  2320. return d / pow(f(log(d) + e)) < k ? format(d) : "";
  2321. };
  2322. };
  2323. scale.copy = function() {
  2324. return d3_scale_log(linear.copy(), log);
  2325. };
  2326. return d3_scale_linearRebind(scale, linear);
  2327. };
  2328. var d3_scale_logFormat = d3.format(".0e");
  2329. function d3_scale_logp(x) {
  2330. return Math.log(x < 0 ? 0 : x) / Math.LN10;
  2331. }
  2332. function d3_scale_logn(x) {
  2333. return -Math.log(x > 0 ? 0 : -x) / Math.LN10;
  2334. }
  2335. d3_scale_logp.pow = function(x) {
  2336. return Math.pow(10, x);
  2337. };
  2338. d3_scale_logn.pow = function(x) {
  2339. return -Math.pow(10, -x);
  2340. };
  2341. d3.scale.pow = function() {
  2342. return d3_scale_pow(d3.scale.linear(), 1);
  2343. };
  2344. function d3_scale_pow(linear, exponent) {
  2345. var powp = d3_scale_powPow(exponent),
  2346. powb = d3_scale_powPow(1 / exponent);
  2347. function scale(x) {
  2348. return linear(powp(x));
  2349. }
  2350. scale.invert = function(x) {
  2351. return powb(linear.invert(x));
  2352. };
  2353. scale.domain = function(x) {
  2354. if (!arguments.length) return linear.domain().map(powb);
  2355. linear.domain(x.map(powp));
  2356. return scale;
  2357. };
  2358. scale.ticks = function(m) {
  2359. return d3_scale_linearTicks(scale.domain(), m);
  2360. };
  2361. scale.tickFormat = function(m) {
  2362. return d3_scale_linearTickFormat(scale.domain(), m);
  2363. };
  2364. scale.nice = function() {
  2365. return scale.domain(d3_scale_nice(scale.domain(), d3_scale_linearNice));
  2366. };
  2367. scale.exponent = function(x) {
  2368. if (!arguments.length) return exponent;
  2369. var domain = scale.domain();
  2370. powp = d3_scale_powPow(exponent = x);
  2371. powb = d3_scale_powPow(1 / exponent);
  2372. return scale.domain(domain);
  2373. };
  2374. scale.copy = function() {
  2375. return d3_scale_pow(linear.copy(), exponent);
  2376. };
  2377. return d3_scale_linearRebind(scale, linear);
  2378. };
  2379. function d3_scale_powPow(e) {
  2380. return function(x) {
  2381. return x < 0 ? -Math.pow(-x, e) : Math.pow(x, e);
  2382. };
  2383. }
  2384. d3.scale.sqrt = function() {
  2385. return d3.scale.pow().exponent(.5);
  2386. };
  2387. d3.scale.ordinal = function() {
  2388. return d3_scale_ordinal([], {t: "range", x: []});
  2389. };
  2390. function d3_scale_ordinal(domain, ranger) {
  2391. var index,
  2392. range,
  2393. rangeBand;
  2394. function scale(x) {
  2395. return range[((index[x] || (index[x] = domain.push(x))) - 1) % range.length];
  2396. }
  2397. function steps(start, step) {
  2398. return d3.range(domain.length).map(function(i) { return start + step * i; });
  2399. }
  2400. scale.domain = function(x) {
  2401. if (!arguments.length) return domain;
  2402. domain = [];
  2403. index = {};
  2404. var i = -1, n = x.length, xi;
  2405. while (++i < n) if (!index[xi = x[i]]) index[xi] = domain.push(xi);
  2406. return scale[ranger.t](ranger.x, ranger.p);
  2407. };
  2408. scale.range = function(x) {
  2409. if (!arguments.length) return range;
  2410. range = x;
  2411. rangeBand = 0;
  2412. ranger = {t: "range", x: x};
  2413. return scale;
  2414. };
  2415. scale.rangePoints = function(x, padding) {
  2416. if (arguments.length < 2) padding = 0;
  2417. var start = x[0],
  2418. stop = x[1],
  2419. step = (stop - start) / (domain.length - 1 + padding);
  2420. range = steps(domain.length < 2 ? (start + stop) / 2 : start + step * padding / 2, step);
  2421. rangeBand = 0;
  2422. ranger = {t: "rangePoints", x: x, p: padding};
  2423. return scale;
  2424. };
  2425. scale.rangeBands = function(x, padding) {
  2426. if (arguments.length < 2) padding = 0;
  2427. var start = x[0],
  2428. stop = x[1],
  2429. step = (stop - start) / (domain.length + padding);
  2430. range = steps(start + step * padding, step);
  2431. rangeBand = step * (1 - padding);
  2432. ranger = {t: "rangeBands", x: x, p: padding};
  2433. return scale;
  2434. };
  2435. scale.rangeRoundBands = function(x, padding) {
  2436. if (arguments.length < 2) padding = 0;
  2437. var start = x[0],
  2438. stop = x[1],
  2439. step = Math.floor((stop - start) / (domain.length + padding));
  2440. range = steps(start + Math.round((stop - start - (domain.length - padding) * step) / 2), step);
  2441. rangeBand = Math.round(step * (1 - padding));
  2442. ranger = {t: "rangeRoundBands", x: x, p: padding};
  2443. return scale;
  2444. };
  2445. scale.rangeBand = function() {
  2446. return rangeBand;
  2447. };
  2448. scale.rangeExtent = function() {
  2449. return ranger.t === "range" ? d3_scaleExtent(ranger.x) : ranger.x;
  2450. };
  2451. scale.copy = function() {
  2452. return d3_scale_ordinal(domain, ranger);
  2453. };
  2454. return scale.domain(domain);
  2455. };
  2456. /*
  2457. * This product includes color specifications and designs developed by Cynthia
  2458. * Brewer (http://colorbrewer.org/). See lib/colorbrewer for more information.
  2459. */
  2460. d3.scale.category10 = function() {
  2461. return d3.scale.ordinal().range(d3_category10);
  2462. };
  2463. d3.scale.category20 = function() {
  2464. return d3.scale.ordinal().range(d3_category20);
  2465. };
  2466. d3.scale.category20b = function() {
  2467. return d3.scale.ordinal().range(d3_category20b);
  2468. };
  2469. d3.scale.category20c = function() {
  2470. return d3.scale.ordinal().range(d3_category20c);
  2471. };
  2472. var d3_category10 = [
  2473. "#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd",
  2474. "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"
  2475. ];
  2476. var d3_category20 = [
  2477. "#1f77b4", "#aec7e8",
  2478. "#ff7f0e", "#ffbb78",
  2479. "#2ca02c", "#98df8a",
  2480. "#d62728", "#ff9896",
  2481. "#9467bd", "#c5b0d5",
  2482. "#8c564b", "#c49c94",
  2483. "#e377c2", "#f7b6d2",
  2484. "#7f7f7f", "#c7c7c7",
  2485. "#bcbd22", "#dbdb8d",
  2486. "#17becf", "#9edae5"
  2487. ];
  2488. var d3_category20b = [
  2489. "#393b79", "#5254a3", "#6b6ecf", "#9c9ede",
  2490. "#637939", "#8ca252", "#b5cf6b", "#cedb9c",
  2491. "#8c6d31", "#bd9e39", "#e7ba52", "#e7cb94",
  2492. "#843c39", "#ad494a", "#d6616b", "#e7969c",
  2493. "#7b4173", "#a55194", "#ce6dbd", "#de9ed6"
  2494. ];
  2495. var d3_category20c = [
  2496. "#3182bd", "#6baed6", "#9ecae1", "#c6dbef",
  2497. "#e6550d", "#fd8d3c", "#fdae6b", "#fdd0a2",
  2498. "#31a354", "#74c476", "#a1d99b", "#c7e9c0",
  2499. "#756bb1", "#9e9ac8", "#bcbddc", "#dadaeb",
  2500. "#636363", "#969696", "#bdbdbd", "#d9d9d9"
  2501. ];
  2502. d3.scale.quantile = function() {
  2503. return d3_scale_quantile([], []);
  2504. };
  2505. function d3_scale_quantile(domain, range) {
  2506. var thresholds;
  2507. function rescale() {
  2508. var k = 0,
  2509. n = domain.length,
  2510. q = range.length;
  2511. thresholds = [];
  2512. while (++k < q) thresholds[k - 1] = d3.quantile(domain, k / q);
  2513. return scale;
  2514. }
  2515. function scale(x) {
  2516. if (isNaN(x = +x)) return NaN;
  2517. return range[d3.bisect(thresholds, x)];
  2518. }
  2519. scale.domain = function(x) {
  2520. if (!arguments.length) return domain;
  2521. domain = x.filter(function(d) { return !isNaN(d); }).sort(d3.ascending);
  2522. return rescale();
  2523. };
  2524. scale.range = function(x) {
  2525. if (!arguments.length) return range;
  2526. range = x;
  2527. return rescale();
  2528. };
  2529. scale.quantiles = function() {
  2530. return thresholds;
  2531. };
  2532. scale.copy = function() {
  2533. return d3_scale_quantile(domain, range); // copy on write!
  2534. };
  2535. return rescale();
  2536. };
  2537. d3.scale.quantize = function() {
  2538. return d3_scale_quantize(0, 1, [0, 1]);
  2539. };
  2540. function d3_scale_quantize(x0, x1, range) {
  2541. var kx, i;
  2542. function scale(x) {
  2543. return range[Math.max(0, Math.min(i, Math.floor(kx * (x - x0))))];
  2544. }
  2545. function rescale() {
  2546. kx = range.length / (x1 - x0);
  2547. i = range.length - 1;
  2548. return scale;
  2549. }
  2550. scale.domain = function(x) {
  2551. if (!arguments.length) return [x0, x1];
  2552. x0 = +x[0];
  2553. x1 = +x[x.length - 1];
  2554. return rescale();
  2555. };
  2556. scale.range = function(x) {
  2557. if (!arguments.length) return range;
  2558. range = x;
  2559. return rescale();
  2560. };
  2561. scale.copy = function() {
  2562. return d3_scale_quantize(x0, x1, range); // copy on write
  2563. };
  2564. return rescale();
  2565. };
  2566. d3.svg = {};
  2567. d3.svg.arc = function() {
  2568. var innerRadius = d3_svg_arcInnerRadius,
  2569. outerRadius = d3_svg_arcOuterRadius,
  2570. startAngle = d3_svg_arcStartAngle,
  2571. endAngle = d3_svg_arcEndAngle;
  2572. function arc() {
  2573. var r0 = innerRadius.apply(this, arguments),
  2574. r1 = outerRadius.apply(this, arguments),
  2575. a0 = startAngle.apply(this, arguments) + d3_svg_arcOffset,
  2576. a1 = endAngle.apply(this, arguments) + d3_svg_arcOffset,
  2577. da = (a1 < a0 && (da = a0, a0 = a1, a1 = da), a1 - a0),
  2578. df = da < Math.PI ? "0" : "1",
  2579. c0 = Math.cos(a0),
  2580. s0 = Math.sin(a0),
  2581. c1 = Math.cos(a1),
  2582. s1 = Math.sin(a1);
  2583. return da >= d3_svg_arcMax
  2584. ? (r0
  2585. ? "M0," + r1
  2586. + "A" + r1 + "," + r1 + " 0 1,1 0," + (-r1)
  2587. + "A" + r1 + "," + r1 + " 0 1,1 0," + r1
  2588. + "M0," + r0
  2589. + "A" + r0 + "," + r0 + " 0 1,0 0," + (-r0)
  2590. + "A" + r0 + "," + r0 + " 0 1,0 0," + r0
  2591. + "Z"
  2592. : "M0," + r1
  2593. + "A" + r1 + "," + r1 + " 0 1,1 0," + (-r1)
  2594. + "A" + r1 + "," + r1 + " 0 1,1 0," + r1
  2595. + "Z")
  2596. : (r0
  2597. ? "M" + r1 * c0 + "," + r1 * s0
  2598. + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1
  2599. + "L" + r0 * c1 + "," + r0 * s1
  2600. + "A" + r0 + "," + r0 + " 0 " + df + ",0 " + r0 * c0 + "," + r0 * s0
  2601. + "Z"
  2602. : "M" + r1 * c0 + "," + r1 * s0
  2603. + "A" + r1 + "," + r1 + " 0 " + df + ",1 " + r1 * c1 + "," + r1 * s1
  2604. + "L0,0"
  2605. + "Z");
  2606. }
  2607. arc.innerRadius = function(v) {
  2608. if (!arguments.length) return innerRadius;
  2609. innerRadius = d3.functor(v);
  2610. return arc;
  2611. };
  2612. arc.outerRadius = function(v) {
  2613. if (!arguments.length) return outerRadius;
  2614. outerRadius = d3.functor(v);
  2615. return arc;
  2616. };
  2617. arc.startAngle = function(v) {
  2618. if (!arguments.length) return startAngle;
  2619. startAngle = d3.functor(v);
  2620. return arc;
  2621. };
  2622. arc.endAngle = function(v) {
  2623. if (!arguments.length) return endAngle;
  2624. endAngle = d3.functor(v);
  2625. return arc;
  2626. };
  2627. arc.centroid = function() {
  2628. var r = (innerRadius.apply(this, arguments)
  2629. + outerRadius.apply(this, arguments)) / 2,
  2630. a = (startAngle.apply(this, arguments)
  2631. + endAngle.apply(this, arguments)) / 2 + d3_svg_arcOffset;
  2632. return [Math.cos(a) * r, Math.sin(a) * r];
  2633. };
  2634. return arc;
  2635. };
  2636. var d3_svg_arcOffset = -Math.PI / 2,
  2637. d3_svg_arcMax = 2 * Math.PI - 1e-6;
  2638. function d3_svg_arcInnerRadius(d) {
  2639. return d.innerRadius;
  2640. }
  2641. function d3_svg_arcOuterRadius(d) {
  2642. return d.outerRadius;
  2643. }
  2644. function d3_svg_arcStartAngle(d) {
  2645. return d.startAngle;
  2646. }
  2647. function d3_svg_arcEndAngle(d) {
  2648. return d.endAngle;
  2649. }
  2650. function d3_svg_line(projection) {
  2651. var x = d3_svg_lineX,
  2652. y = d3_svg_lineY,
  2653. interpolate = "linear",
  2654. interpolator = d3_svg_lineInterpolators[interpolate],
  2655. tension = .7;
  2656. function line(d) {
  2657. return d.length < 1 ? null : "M" + interpolator(projection(d3_svg_linePoints(this, d, x, y)), tension);
  2658. }
  2659. line.x = function(v) {
  2660. if (!arguments.length) return x;
  2661. x = v;
  2662. return line;
  2663. };
  2664. line.y = function(v) {
  2665. if (!arguments.length) return y;
  2666. y = v;
  2667. return line;
  2668. };
  2669. line.interpolate = function(v) {
  2670. if (!arguments.length) return interpolate;
  2671. interpolator = d3_svg_lineInterpolators[interpolate = v];
  2672. return line;
  2673. };
  2674. line.tension = function(v) {
  2675. if (!arguments.length) return tension;
  2676. tension = v;
  2677. return line;
  2678. };
  2679. return line;
  2680. }
  2681. d3.svg.line = function() {
  2682. return d3_svg_line(Object);
  2683. };
  2684. // Converts the specified array of data into an array of points
  2685. // (x-y tuples), by evaluating the specified `x` and `y` functions on each
  2686. // data point. The `this` context of the evaluated functions is the specified
  2687. // "self" object; each function is passed the current datum and index.
  2688. function d3_svg_linePoints(self, d, x, y) {
  2689. var points = [],
  2690. i = -1,
  2691. n = d.length,
  2692. fx = typeof x === "function",
  2693. fy = typeof y === "function",
  2694. value;
  2695. if (fx && fy) {
  2696. while (++i < n) points.push([
  2697. x.call(self, value = d[i], i),
  2698. y.call(self, value, i)
  2699. ]);
  2700. } else if (fx) {
  2701. while (++i < n) points.push([x.call(self, d[i], i), y]);
  2702. } else if (fy) {
  2703. while (++i < n) points.push([x, y.call(self, d[i], i)]);
  2704. } else {
  2705. while (++i < n) points.push([x, y]);
  2706. }
  2707. return points;
  2708. }
  2709. // The default `x` property, which references d[0].
  2710. function d3_svg_lineX(d) {
  2711. return d[0];
  2712. }
  2713. // The default `y` property, which references d[1].
  2714. function d3_svg_lineY(d) {
  2715. return d[1];
  2716. }
  2717. // The various interpolators supported by the `line` class.
  2718. var d3_svg_lineInterpolators = {
  2719. "linear": d3_svg_lineLinear,
  2720. "step-before": d3_svg_lineStepBefore,
  2721. "step-after": d3_svg_lineStepAfter,
  2722. "basis": d3_svg_lineBasis,
  2723. "basis-open": d3_svg_lineBasisOpen,
  2724. "basis-closed": d3_svg_lineBasisClosed,
  2725. "bundle": d3_svg_lineBundle,
  2726. "cardinal": d3_svg_lineCardinal,
  2727. "cardinal-open": d3_svg_lineCardinalOpen,
  2728. "cardinal-closed": d3_svg_lineCardinalClosed,
  2729. "monotone": d3_svg_lineMonotone
  2730. };
  2731. // Linear interpolation; generates "L" commands.
  2732. function d3_svg_lineLinear(points) {
  2733. var i = 0,
  2734. n = points.length,
  2735. p = points[0],
  2736. path = [p[0], ",", p[1]];
  2737. while (++i < n) path.push("L", (p = points[i])[0], ",", p[1]);
  2738. return path.join("");
  2739. }
  2740. // Step interpolation; generates "H" and "V" commands.
  2741. function d3_svg_lineStepBefore(points) {
  2742. var i = 0,
  2743. n = points.length,
  2744. p = points[0],
  2745. path = [p[0], ",", p[1]];
  2746. while (++i < n) path.push("V", (p = points[i])[1], "H", p[0]);
  2747. return path.join("");
  2748. }
  2749. // Step interpolation; generates "H" and "V" commands.
  2750. function d3_svg_lineStepAfter(points) {
  2751. var i = 0,
  2752. n = points.length,
  2753. p = points[0],
  2754. path = [p[0], ",", p[1]];
  2755. while (++i < n) path.push("H", (p = points[i])[0], "V", p[1]);
  2756. return path.join("");
  2757. }
  2758. // Open cardinal spline interpolation; generates "C" commands.
  2759. function d3_svg_lineCardinalOpen(points, tension) {
  2760. return points.length < 4
  2761. ? d3_svg_lineLinear(points)
  2762. : points[1] + d3_svg_lineHermite(points.slice(1, points.length - 1),
  2763. d3_svg_lineCardinalTangents(points, tension));
  2764. }
  2765. // Closed cardinal spline interpolation; generates "C" commands.
  2766. function d3_svg_lineCardinalClosed(points, tension) {
  2767. return points.length < 3
  2768. ? d3_svg_lineLinear(points)
  2769. : points[0] + d3_svg_lineHermite((points.push(points[0]), points),
  2770. d3_svg_lineCardinalTangents([points[points.length - 2]]
  2771. .concat(points, [points[1]]), tension));
  2772. }
  2773. // Cardinal spline interpolation; generates "C" commands.
  2774. function d3_svg_lineCardinal(points, tension, closed) {
  2775. return points.length < 3
  2776. ? d3_svg_lineLinear(points)
  2777. : points[0] + d3_svg_lineHermite(points,
  2778. d3_svg_lineCardinalTangents(points, tension));
  2779. }
  2780. // Hermite spline construction; generates "C" commands.
  2781. function d3_svg_lineHermite(points, tangents) {
  2782. if (tangents.length < 1
  2783. || (points.length != tangents.length
  2784. && points.length != tangents.length + 2)) {
  2785. return d3_svg_lineLinear(points);
  2786. }
  2787. var quad = points.length != tangents.length,
  2788. path = "",
  2789. p0 = points[0],
  2790. p = points[1],
  2791. t0 = tangents[0],
  2792. t = t0,
  2793. pi = 1;
  2794. if (quad) {
  2795. path += "Q" + (p[0] - t0[0] * 2 / 3) + "," + (p[1] - t0[1] * 2 / 3)
  2796. + "," + p[0] + "," + p[1];
  2797. p0 = points[1];
  2798. pi = 2;
  2799. }
  2800. if (tangents.length > 1) {
  2801. t = tangents[1];
  2802. p = points[pi];
  2803. pi++;
  2804. path += "C" + (p0[0] + t0[0]) + "," + (p0[1] + t0[1])
  2805. + "," + (p[0] - t[0]) + "," + (p[1] - t[1])
  2806. + "," + p[0] + "," + p[1];
  2807. for (var i = 2; i < tangents.length; i++, pi++) {
  2808. p = points[pi];
  2809. t = tangents[i];
  2810. path += "S" + (p[0] - t[0]) + "," + (p[1] - t[1])
  2811. + "," + p[0] + "," + p[1];
  2812. }
  2813. }
  2814. if (quad) {
  2815. var lp = points[pi];
  2816. path += "Q" + (p[0] + t[0] * 2 / 3) + "," + (p[1] + t[1] * 2 / 3)
  2817. + "," + lp[0] + "," + lp[1];
  2818. }
  2819. return path;
  2820. }
  2821. // Generates tangents for a cardinal spline.
  2822. function d3_svg_lineCardinalTangents(points, tension) {
  2823. var tangents = [],
  2824. a = (1 - tension) / 2,
  2825. p0,
  2826. p1 = points[0],
  2827. p2 = points[1],
  2828. i = 1,
  2829. n = points.length;
  2830. while (++i < n) {
  2831. p0 = p1;
  2832. p1 = p2;
  2833. p2 = points[i];
  2834. tangents.push([a * (p2[0] - p0[0]), a * (p2[1] - p0[1])]);
  2835. }
  2836. return tangents;
  2837. }
  2838. // B-spline interpolation; generates "C" commands.
  2839. function d3_svg_lineBasis(points) {
  2840. if (points.length < 3) return d3_svg_lineLinear(points);
  2841. var i = 1,
  2842. n = points.length,
  2843. pi = points[0],
  2844. x0 = pi[0],
  2845. y0 = pi[1],
  2846. px = [x0, x0, x0, (pi = points[1])[0]],
  2847. py = [y0, y0, y0, pi[1]],
  2848. path = [x0, ",", y0];
  2849. d3_svg_lineBasisBezier(path, px, py);
  2850. while (++i < n) {
  2851. pi = points[i];
  2852. px.shift(); px.push(pi[0]);
  2853. py.shift(); py.push(pi[1]);
  2854. d3_svg_lineBasisBezier(path, px, py);
  2855. }
  2856. i = -1;
  2857. while (++i < 2) {
  2858. px.shift(); px.push(pi[0]);
  2859. py.shift(); py.push(pi[1]);
  2860. d3_svg_lineBasisBezier(path, px, py);
  2861. }
  2862. return path.join("");
  2863. }
  2864. // Open B-spline interpolation; generates "C" commands.
  2865. function d3_svg_lineBasisOpen(points) {
  2866. if (points.length < 4) return d3_svg_lineLinear(points);
  2867. var path = [],
  2868. i = -1,
  2869. n = points.length,
  2870. pi,
  2871. px = [0],
  2872. py = [0];
  2873. while (++i < 3) {
  2874. pi = points[i];
  2875. px.push(pi[0]);
  2876. py.push(pi[1]);
  2877. }
  2878. path.push(d3_svg_lineDot4(d3_svg_lineBasisBezier3, px)
  2879. + "," + d3_svg_lineDot4(d3_svg_lineBasisBezier3, py));
  2880. --i; while (++i < n) {
  2881. pi = points[i];
  2882. px.shift(); px.push(pi[0]);
  2883. py.shift(); py.push(pi[1]);
  2884. d3_svg_lineBasisBezier(path, px, py);
  2885. }
  2886. return path.join("");
  2887. }
  2888. // Closed B-spline interpolation; generates "C" commands.
  2889. function d3_svg_lineBasisClosed(points) {
  2890. var path,
  2891. i = -1,
  2892. n = points.length,
  2893. m = n + 4,
  2894. pi,
  2895. px = [],
  2896. py = [];
  2897. while (++i < 4) {
  2898. pi = points[i % n];
  2899. px.push(pi[0]);
  2900. py.push(pi[1]);
  2901. }
  2902. path = [
  2903. d3_svg_lineDot4(d3_svg_lineBasisBezier3, px), ",",
  2904. d3_svg_lineDot4(d3_svg_lineBasisBezier3, py)
  2905. ];
  2906. --i; while (++i < m) {
  2907. pi = points[i % n];
  2908. px.shift(); px.push(pi[0]);
  2909. py.shift(); py.push(pi[1]);
  2910. d3_svg_lineBasisBezier(path, px, py);
  2911. }
  2912. return path.join("");
  2913. }
  2914. function d3_svg_lineBundle(points, tension) {
  2915. var n = points.length - 1,
  2916. x0 = points[0][0],
  2917. y0 = points[0][1],
  2918. dx = points[n][0] - x0,
  2919. dy = points[n][1] - y0,
  2920. i = -1,
  2921. p,
  2922. t;
  2923. while (++i <= n) {
  2924. p = points[i];
  2925. t = i / n;
  2926. p[0] = tension * p[0] + (1 - tension) * (x0 + t * dx);
  2927. p[1] = tension * p[1] + (1 - tension) * (y0 + t * dy);
  2928. }
  2929. return d3_svg_lineBasis(points);
  2930. }
  2931. // Returns the dot product of the given four-element vectors.
  2932. function d3_svg_lineDot4(a, b) {
  2933. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
  2934. }
  2935. // Matrix to transform basis (b-spline) control points to bezier
  2936. // control points. Derived from FvD 11.2.8.
  2937. var d3_svg_lineBasisBezier1 = [0, 2/3, 1/3, 0],
  2938. d3_svg_lineBasisBezier2 = [0, 1/3, 2/3, 0],
  2939. d3_svg_lineBasisBezier3 = [0, 1/6, 2/3, 1/6];
  2940. // Pushes a "C" Bézier curve onto the specified path array, given the
  2941. // two specified four-element arrays which define the control points.
  2942. function d3_svg_lineBasisBezier(path, x, y) {
  2943. path.push(
  2944. "C", d3_svg_lineDot4(d3_svg_lineBasisBezier1, x),
  2945. ",", d3_svg_lineDot4(d3_svg_lineBasisBezier1, y),
  2946. ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, x),
  2947. ",", d3_svg_lineDot4(d3_svg_lineBasisBezier2, y),
  2948. ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, x),
  2949. ",", d3_svg_lineDot4(d3_svg_lineBasisBezier3, y));
  2950. }
  2951. // Computes the slope from points p0 to p1.
  2952. function d3_svg_lineSlope(p0, p1) {
  2953. return (p1[1] - p0[1]) / (p1[0] - p0[0]);
  2954. }
  2955. // Compute three-point differences for the given points.
  2956. // http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Finite_difference
  2957. function d3_svg_lineFiniteDifferences(points) {
  2958. var i = 0,
  2959. j = points.length - 1,
  2960. m = [],
  2961. p0 = points[0],
  2962. p1 = points[1],
  2963. d = m[0] = d3_svg_lineSlope(p0, p1);
  2964. while (++i < j) {
  2965. m[i] = d + (d = d3_svg_lineSlope(p0 = p1, p1 = points[i + 1]));
  2966. }
  2967. m[i] = d;
  2968. return m;
  2969. }
  2970. // Interpolates the given points using Fritsch-Carlson Monotone cubic Hermite
  2971. // interpolation. Returns an array of tangent vectors. For details, see
  2972. // http://en.wikipedia.org/wiki/Monotone_cubic_interpolation
  2973. function d3_svg_lineMonotoneTangents(points) {
  2974. var tangents = [],
  2975. d,
  2976. a,
  2977. b,
  2978. s,
  2979. m = d3_svg_lineFiniteDifferences(points),
  2980. i = -1,
  2981. j = points.length - 1;
  2982. // The first two steps are done by computing finite-differences:
  2983. // 1. Compute the slopes of the secant lines between successive points.
  2984. // 2. Initialize the tangents at every point as the average of the secants.
  2985. // Then, for each segment…
  2986. while (++i < j) {
  2987. d = d3_svg_lineSlope(points[i], points[i + 1]);
  2988. // 3. If two successive yk = y{k + 1} are equal (i.e., d is zero), then set
  2989. // mk = m{k + 1} = 0 as the spline connecting these points must be flat to
  2990. // preserve monotonicity. Ignore step 4 and 5 for those k.
  2991. if (Math.abs(d) < 1e-6) {
  2992. m[i] = m[i + 1] = 0;
  2993. } else {
  2994. // 4. Let ak = mk / dk and bk = m{k + 1} / dk.
  2995. a = m[i] / d;
  2996. b = m[i + 1] / d;
  2997. // 5. Prevent overshoot and ensure monotonicity by restricting the
  2998. // magnitude of vector <ak, bk> to a circle of radius 3.
  2999. s = a * a + b * b;
  3000. if (s > 9) {
  3001. s = d * 3 / Math.sqrt(s);
  3002. m[i] = s * a;
  3003. m[i + 1] = s * b;
  3004. }
  3005. }
  3006. }
  3007. // Compute the normalized tangent vector from the slopes. Note that if x is
  3008. // not monotonic, it's possible that the slope will be infinite, so we protect
  3009. // against NaN by setting the coordinate to zero.
  3010. i = -1; while (++i <= j) {
  3011. s = (points[Math.min(j, i + 1)][0] - points[Math.max(0, i - 1)][0])
  3012. / (6 * (1 + m[i] * m[i]));
  3013. tangents.push([s || 0, m[i] * s || 0]);
  3014. }
  3015. return tangents;
  3016. }
  3017. function d3_svg_lineMonotone(points) {
  3018. return points.length < 3
  3019. ? d3_svg_lineLinear(points)
  3020. : points[0] +
  3021. d3_svg_lineHermite(points, d3_svg_lineMonotoneTangents(points));
  3022. }
  3023. d3.svg.line.radial = function() {
  3024. var line = d3_svg_line(d3_svg_lineRadial);
  3025. line.radius = line.x, delete line.x;
  3026. line.angle = line.y, delete line.y;
  3027. return line;
  3028. };
  3029. function d3_svg_lineRadial(points) {
  3030. var point,
  3031. i = -1,
  3032. n = points.length,
  3033. r,
  3034. a;
  3035. while (++i < n) {
  3036. point = points[i];
  3037. r = point[0];
  3038. a = point[1] + d3_svg_arcOffset;
  3039. point[0] = r * Math.cos(a);
  3040. point[1] = r * Math.sin(a);
  3041. }
  3042. return points;
  3043. }
  3044. function d3_svg_area(projection) {
  3045. var x0 = d3_svg_lineX,
  3046. x1 = d3_svg_lineX,
  3047. y0 = 0,
  3048. y1 = d3_svg_lineY,
  3049. interpolate,
  3050. i0,
  3051. i1,
  3052. tension = .7;
  3053. function area(d) {
  3054. if (d.length < 1) return null;
  3055. var points0 = d3_svg_linePoints(this, d, x0, y0),
  3056. points1 = d3_svg_linePoints(this, d, x0 === x1 ? d3_svg_areaX(points0) : x1, y0 === y1 ? d3_svg_areaY(points0) : y1);
  3057. return "M" + i0(projection(points1), tension)
  3058. + "L" + i1(projection(points0.reverse()), tension)
  3059. + "Z";
  3060. }
  3061. area.x = function(x) {
  3062. if (!arguments.length) return x1;
  3063. x0 = x1 = x;
  3064. return area;
  3065. };
  3066. area.x0 = function(x) {
  3067. if (!arguments.length) return x0;
  3068. x0 = x;
  3069. return area;
  3070. };
  3071. area.x1 = function(x) {
  3072. if (!arguments.length) return x1;
  3073. x1 = x;
  3074. return area;
  3075. };
  3076. area.y = function(y) {
  3077. if (!arguments.length) return y1;
  3078. y0 = y1 = y;
  3079. return area;
  3080. };
  3081. area.y0 = function(y) {
  3082. if (!arguments.length) return y0;
  3083. y0 = y;
  3084. return area;
  3085. };
  3086. area.y1 = function(y) {
  3087. if (!arguments.length) return y1;
  3088. y1 = y;
  3089. return area;
  3090. };
  3091. area.interpolate = function(x) {
  3092. if (!arguments.length) return interpolate;
  3093. i0 = d3_svg_lineInterpolators[interpolate = x];
  3094. i1 = i0.reverse || i0;
  3095. return area;
  3096. };
  3097. area.tension = function(x) {
  3098. if (!arguments.length) return tension;
  3099. tension = x;
  3100. return area;
  3101. };
  3102. return area.interpolate("linear");
  3103. }
  3104. d3_svg_lineStepBefore.reverse = d3_svg_lineStepAfter;
  3105. d3_svg_lineStepAfter.reverse = d3_svg_lineStepBefore;
  3106. d3.svg.area = function() {
  3107. return d3_svg_area(Object);
  3108. };
  3109. function d3_svg_areaX(points) {
  3110. return function(d, i) {
  3111. return points[i][0];
  3112. };
  3113. }
  3114. function d3_svg_areaY(points) {
  3115. return function(d, i) {
  3116. return points[i][1];
  3117. };
  3118. }
  3119. d3.svg.area.radial = function() {
  3120. var area = d3_svg_area(d3_svg_lineRadial);
  3121. area.radius = area.x, delete area.x;
  3122. area.innerRadius = area.x0, delete area.x0;
  3123. area.outerRadius = area.x1, delete area.x1;
  3124. area.angle = area.y, delete area.y;
  3125. area.startAngle = area.y0, delete area.y0;
  3126. area.endAngle = area.y1, delete area.y1;
  3127. return area;
  3128. };
  3129. d3.svg.chord = function() {
  3130. var source = d3_svg_chordSource,
  3131. target = d3_svg_chordTarget,
  3132. radius = d3_svg_chordRadius,
  3133. startAngle = d3_svg_arcStartAngle,
  3134. endAngle = d3_svg_arcEndAngle;
  3135. // TODO Allow control point to be customized.
  3136. function chord(d, i) {
  3137. var s = subgroup(this, source, d, i),
  3138. t = subgroup(this, target, d, i);
  3139. return "M" + s.p0
  3140. + arc(s.r, s.p1, s.a1 - s.a0) + (equals(s, t)
  3141. ? curve(s.r, s.p1, s.r, s.p0)
  3142. : curve(s.r, s.p1, t.r, t.p0)
  3143. + arc(t.r, t.p1, t.a1 - t.a0)
  3144. + curve(t.r, t.p1, s.r, s.p0))
  3145. + "Z";
  3146. }
  3147. function subgroup(self, f, d, i) {
  3148. var subgroup = f.call(self, d, i),
  3149. r = radius.call(self, subgroup, i),
  3150. a0 = startAngle.call(self, subgroup, i) + d3_svg_arcOffset,
  3151. a1 = endAngle.call(self, subgroup, i) + d3_svg_arcOffset;
  3152. return {
  3153. r: r,
  3154. a0: a0,
  3155. a1: a1,
  3156. p0: [r * Math.cos(a0), r * Math.sin(a0)],
  3157. p1: [r * Math.cos(a1), r * Math.sin(a1)]
  3158. };
  3159. }
  3160. function equals(a, b) {
  3161. return a.a0 == b.a0 && a.a1 == b.a1;
  3162. }
  3163. function arc(r, p, a) {
  3164. return "A" + r + "," + r + " 0 " + +(a > Math.PI) + ",1 " + p;
  3165. }
  3166. function curve(r0, p0, r1, p1) {
  3167. return "Q 0,0 " + p1;
  3168. }
  3169. chord.radius = function(v) {
  3170. if (!arguments.length) return radius;
  3171. radius = d3.functor(v);
  3172. return chord;
  3173. };
  3174. chord.source = function(v) {
  3175. if (!arguments.length) return source;
  3176. source = d3.functor(v);
  3177. return chord;
  3178. };
  3179. chord.target = function(v) {
  3180. if (!arguments.length) return target;
  3181. target = d3.functor(v);
  3182. return chord;
  3183. };
  3184. chord.startAngle = function(v) {
  3185. if (!arguments.length) return startAngle;
  3186. startAngle = d3.functor(v);
  3187. return chord;
  3188. };
  3189. chord.endAngle = function(v) {
  3190. if (!arguments.length) return endAngle;
  3191. endAngle = d3.functor(v);
  3192. return chord;
  3193. };
  3194. return chord;
  3195. };
  3196. function d3_svg_chordSource(d) {
  3197. return d.source;
  3198. }
  3199. function d3_svg_chordTarget(d) {
  3200. return d.target;
  3201. }
  3202. function d3_svg_chordRadius(d) {
  3203. return d.radius;
  3204. }
  3205. function d3_svg_chordStartAngle(d) {
  3206. return d.startAngle;
  3207. }
  3208. function d3_svg_chordEndAngle(d) {
  3209. return d.endAngle;
  3210. }
  3211. d3.svg.diagonal = function() {
  3212. var source = d3_svg_chordSource,
  3213. target = d3_svg_chordTarget,
  3214. projection = d3_svg_diagonalProjection;
  3215. function diagonal(d, i) {
  3216. var p0 = source.call(this, d, i),
  3217. p3 = target.call(this, d, i),
  3218. m = (p0.y + p3.y) / 2,
  3219. p = [p0, {x: p0.x, y: m}, {x: p3.x, y: m}, p3];
  3220. p = p.map(projection);
  3221. return "M" + p[0] + "C" + p[1] + " " + p[2] + " " + p[3];
  3222. }
  3223. diagonal.source = function(x) {
  3224. if (!arguments.length) return source;
  3225. source = d3.functor(x);
  3226. return diagonal;
  3227. };
  3228. diagonal.target = function(x) {
  3229. if (!arguments.length) return target;
  3230. target = d3.functor(x);
  3231. return diagonal;
  3232. };
  3233. diagonal.projection = function(x) {
  3234. if (!arguments.length) return projection;
  3235. projection = x;
  3236. return diagonal;
  3237. };
  3238. return diagonal;
  3239. };
  3240. function d3_svg_diagonalProjection(d) {
  3241. return [d.x, d.y];
  3242. }
  3243. d3.svg.diagonal.radial = function() {
  3244. var diagonal = d3.svg.diagonal(),
  3245. projection = d3_svg_diagonalProjection,
  3246. projection_ = diagonal.projection;
  3247. diagonal.projection = function(x) {
  3248. return arguments.length
  3249. ? projection_(d3_svg_diagonalRadialProjection(projection = x))
  3250. : projection;
  3251. };
  3252. return diagonal;
  3253. };
  3254. function d3_svg_diagonalRadialProjection(projection) {
  3255. return function() {
  3256. var d = projection.apply(this, arguments),
  3257. r = d[0],
  3258. a = d[1] + d3_svg_arcOffset;
  3259. return [r * Math.cos(a), r * Math.sin(a)];
  3260. };
  3261. }
  3262. d3.svg.mouse = function(container) {
  3263. return d3_svg_mousePoint(container, d3.event);
  3264. };
  3265. // https://bugs.webkit.org/show_bug.cgi?id=44083
  3266. var d3_mouse_bug44083 = /WebKit/.test(navigator.userAgent) ? -1 : 0;
  3267. function d3_svg_mousePoint(container, e) {
  3268. var point = (container.ownerSVGElement || container).createSVGPoint();
  3269. if ((d3_mouse_bug44083 < 0) && (window.scrollX || window.scrollY)) {
  3270. var svg = d3.select(document.body)
  3271. .append("svg")
  3272. .style("position", "absolute")
  3273. .style("top", 0)
  3274. .style("left", 0);
  3275. var ctm = svg[0][0].getScreenCTM();
  3276. d3_mouse_bug44083 = !(ctm.f || ctm.e);
  3277. svg.remove();
  3278. }
  3279. if (d3_mouse_bug44083) {
  3280. point.x = e.pageX;
  3281. point.y = e.pageY;
  3282. } else {
  3283. point.x = e.clientX;
  3284. point.y = e.clientY;
  3285. }
  3286. point = point.matrixTransform(container.getScreenCTM().inverse());
  3287. return [point.x, point.y];
  3288. };
  3289. d3.svg.touches = function(container, touches) {
  3290. if (arguments.length < 2) touches = d3.event.touches;
  3291. return touches ? d3_array(touches).map(function(touch) {
  3292. var point = d3_svg_mousePoint(container, touch);
  3293. point.identifier = touch.identifier;
  3294. return point;
  3295. }) : [];
  3296. };
  3297. d3.svg.symbol = function() {
  3298. var type = d3_svg_symbolType,
  3299. size = d3_svg_symbolSize;
  3300. function symbol(d, i) {
  3301. return (d3_svg_symbols[type.call(this, d, i)]
  3302. || d3_svg_symbols.circle)
  3303. (size.call(this, d, i));
  3304. }
  3305. symbol.type = function(x) {
  3306. if (!arguments.length) return type;
  3307. type = d3.functor(x);
  3308. return symbol;
  3309. };
  3310. // size of symbol in square pixels
  3311. symbol.size = function(x) {
  3312. if (!arguments.length) return size;
  3313. size = d3.functor(x);
  3314. return symbol;
  3315. };
  3316. return symbol;
  3317. };
  3318. function d3_svg_symbolSize() {
  3319. return 64;
  3320. }
  3321. function d3_svg_symbolType() {
  3322. return "circle";
  3323. }
  3324. // TODO cross-diagonal?
  3325. var d3_svg_symbols = {
  3326. "circle": function(size) {
  3327. var r = Math.sqrt(size / Math.PI);
  3328. return "M0," + r
  3329. + "A" + r + "," + r + " 0 1,1 0," + (-r)
  3330. + "A" + r + "," + r + " 0 1,1 0," + r
  3331. + "Z";
  3332. },
  3333. "cross": function(size) {
  3334. var r = Math.sqrt(size / 5) / 2;
  3335. return "M" + -3 * r + "," + -r
  3336. + "H" + -r
  3337. + "V" + -3 * r
  3338. + "H" + r
  3339. + "V" + -r
  3340. + "H" + 3 * r
  3341. + "V" + r
  3342. + "H" + r
  3343. + "V" + 3 * r
  3344. + "H" + -r
  3345. + "V" + r
  3346. + "H" + -3 * r
  3347. + "Z";
  3348. },
  3349. "diamond": function(size) {
  3350. var ry = Math.sqrt(size / (2 * d3_svg_symbolTan30)),
  3351. rx = ry * d3_svg_symbolTan30;
  3352. return "M0," + -ry
  3353. + "L" + rx + ",0"
  3354. + " 0," + ry
  3355. + " " + -rx + ",0"
  3356. + "Z";
  3357. },
  3358. "square": function(size) {
  3359. var r = Math.sqrt(size) / 2;
  3360. return "M" + -r + "," + -r
  3361. + "L" + r + "," + -r
  3362. + " " + r + "," + r
  3363. + " " + -r + "," + r
  3364. + "Z";
  3365. },
  3366. "triangle-down": function(size) {
  3367. var rx = Math.sqrt(size / d3_svg_symbolSqrt3),
  3368. ry = rx * d3_svg_symbolSqrt3 / 2;
  3369. return "M0," + ry
  3370. + "L" + rx +"," + -ry
  3371. + " " + -rx + "," + -ry
  3372. + "Z";
  3373. },
  3374. "triangle-up": function(size) {
  3375. var rx = Math.sqrt(size / d3_svg_symbolSqrt3),
  3376. ry = rx * d3_svg_symbolSqrt3 / 2;
  3377. return "M0," + -ry
  3378. + "L" + rx +"," + ry
  3379. + " " + -rx + "," + ry
  3380. + "Z";
  3381. }
  3382. };
  3383. d3.svg.symbolTypes = d3.keys(d3_svg_symbols);
  3384. var d3_svg_symbolSqrt3 = Math.sqrt(3),
  3385. d3_svg_symbolTan30 = Math.tan(30 * Math.PI / 180);
  3386. d3.svg.axis = function() {
  3387. var scale = d3.scale.linear(),
  3388. orient = "bottom",
  3389. tickMajorSize = 6,
  3390. tickMinorSize = 6,
  3391. tickEndSize = 6,
  3392. tickPadding = 3,
  3393. tickArguments_ = [10],
  3394. tickFormat_,
  3395. tickSubdivide = 0;
  3396. function axis(selection) {
  3397. selection.each(function(d, i, j) {
  3398. var g = d3.select(this);
  3399. // If selection is a transition, create subtransitions.
  3400. var transition = selection.delay ? function(o) {
  3401. var id = d3_transitionInheritId;
  3402. try {
  3403. d3_transitionInheritId = selection.id;
  3404. return o.transition()
  3405. .delay(selection[j][i].delay)
  3406. .duration(selection[j][i].duration)
  3407. .ease(selection.ease());
  3408. } finally {
  3409. d3_transitionInheritId = id;
  3410. }
  3411. } : Object;
  3412. // Ticks, or domain values for ordinal scales.
  3413. var ticks = scale.ticks ? scale.ticks.apply(scale, tickArguments_) : scale.domain(),
  3414. tickFormat = tickFormat_ == null ? (scale.tickFormat ? scale.tickFormat.apply(scale, tickArguments_) : String) : tickFormat_;
  3415. // Minor ticks.
  3416. var subticks = d3_svg_axisSubdivide(scale, ticks, tickSubdivide),
  3417. subtick = g.selectAll(".minor").data(subticks, String),
  3418. subtickEnter = subtick.enter().insert("line", "g").attr("class", "tick minor").style("opacity", 1e-6),
  3419. subtickExit = transition(subtick.exit()).style("opacity", 1e-6).remove(),
  3420. subtickUpdate = transition(subtick).style("opacity", 1);
  3421. // Major ticks.
  3422. var tick = g.selectAll("g").data(ticks, String),
  3423. tickEnter = tick.enter().insert("g", "path").style("opacity", 1e-6),
  3424. tickExit = transition(tick.exit()).style("opacity", 1e-6).remove(),
  3425. tickUpdate = transition(tick).style("opacity", 1),
  3426. tickTransform;
  3427. // Domain.
  3428. var range = d3_scaleRange(scale),
  3429. path = g.selectAll(".domain").data([0]),
  3430. pathEnter = path.enter().append("path").attr("class", "domain"),
  3431. pathUpdate = transition(path);
  3432. // Stash a snapshot of the new scale, and retrieve the old snapshot.
  3433. var scale1 = scale.copy(),
  3434. scale0 = this.__chart__ || scale1;
  3435. this.__chart__ = scale1;
  3436. tickEnter.append("line").attr("class", "tick");
  3437. tickEnter.append("text");
  3438. tickUpdate.select("text").text(tickFormat);
  3439. switch (orient) {
  3440. case "bottom": {
  3441. tickTransform = d3_svg_axisX;
  3442. subtickEnter.attr("y2", tickMinorSize);
  3443. subtickUpdate.attr("x2", 0).attr("y2", tickMinorSize);
  3444. tickEnter.select("line").attr("y2", tickMajorSize);
  3445. tickEnter.select("text").attr("y", Math.max(tickMajorSize, 0) + tickPadding);
  3446. tickUpdate.select("line").attr("x2", 0).attr("y2", tickMajorSize);
  3447. tickUpdate.select("text").attr("x", 0).attr("y", Math.max(tickMajorSize, 0) + tickPadding).attr("dy", ".71em").attr("text-anchor", "middle");
  3448. pathUpdate.attr("d", "M" + range[0] + "," + tickEndSize + "V0H" + range[1] + "V" + tickEndSize);
  3449. break;
  3450. }
  3451. case "top": {
  3452. tickTransform = d3_svg_axisX;
  3453. subtickEnter.attr("y2", -tickMinorSize);
  3454. subtickUpdate.attr("x2", 0).attr("y2", -tickMinorSize);
  3455. tickEnter.select("line").attr("y2", -tickMajorSize);
  3456. tickEnter.select("text").attr("y", -(Math.max(tickMajorSize, 0) + tickPadding));
  3457. tickUpdate.select("line").attr("x2", 0).attr("y2", -tickMajorSize);
  3458. tickUpdate.select("text").attr("x", 0).attr("y", -(Math.max(tickMajorSize, 0) + tickPadding)).attr("dy", "0em").attr("text-anchor", "middle");
  3459. pathUpdate.attr("d", "M" + range[0] + "," + -tickEndSize + "V0H" + range[1] + "V" + -tickEndSize);
  3460. break;
  3461. }
  3462. case "left": {
  3463. tickTransform = d3_svg_axisY;
  3464. subtickEnter.attr("x2", -tickMinorSize);
  3465. subtickUpdate.attr("x2", -tickMinorSize).attr("y2", 0);
  3466. tickEnter.select("line").attr("x2", -tickMajorSize);
  3467. tickEnter.select("text").attr("x", -(Math.max(tickMajorSize, 0) + tickPadding));
  3468. tickUpdate.select("line").attr("x2", -tickMajorSize).attr("y2", 0);
  3469. tickUpdate.select("text").attr("x", -(Math.max(tickMajorSize, 0) + tickPadding)).attr("y", 0).attr("dy", ".32em").attr("text-anchor", "end");
  3470. pathUpdate.attr("d", "M" + -tickEndSize + "," + range[0] + "H0V" + range[1] + "H" + -tickEndSize);
  3471. break;
  3472. }
  3473. case "right": {
  3474. tickTransform = d3_svg_axisY;
  3475. subtickEnter.attr("x2", tickMinorSize);
  3476. subtickUpdate.attr("x2", tickMinorSize).attr("y2", 0);
  3477. tickEnter.select("line").attr("x2", tickMajorSize);
  3478. tickEnter.select("text").attr("x", Math.max(tickMajorSize, 0) + tickPadding);
  3479. tickUpdate.select("line").attr("x2", tickMajorSize).attr("y2", 0);
  3480. tickUpdate.select("text").attr("x", Math.max(tickMajorSize, 0) + tickPadding).attr("y", 0).attr("dy", ".32em").attr("text-anchor", "start");
  3481. pathUpdate.attr("d", "M" + tickEndSize + "," + range[0] + "H0V" + range[1] + "H" + tickEndSize);
  3482. break;
  3483. }
  3484. }
  3485. // For quantitative scales:
  3486. // - enter new ticks from the old scale
  3487. // - exit old ticks to the new scale
  3488. if (scale.ticks) {
  3489. tickEnter.call(tickTransform, scale0);
  3490. tickUpdate.call(tickTransform, scale1);
  3491. tickExit.call(tickTransform, scale1);
  3492. subtickEnter.call(tickTransform, scale0);
  3493. subtickUpdate.call(tickTransform, scale1);
  3494. subtickExit.call(tickTransform, scale1);
  3495. }
  3496. // For ordinal scales:
  3497. // - any entering ticks are undefined in the old scale
  3498. // - any exiting ticks are undefined in the new scale
  3499. // Therefore, we only need to transition updating ticks.
  3500. else {
  3501. var dx = scale1.rangeBand() / 2, x = function(d) { return scale1(d) + dx; };
  3502. tickEnter.call(tickTransform, x);
  3503. tickUpdate.call(tickTransform, x);
  3504. }
  3505. });
  3506. }
  3507. axis.scale = function(x) {
  3508. if (!arguments.length) return scale;
  3509. scale = x;
  3510. return axis;
  3511. };
  3512. axis.orient = function(x) {
  3513. if (!arguments.length) return orient;
  3514. orient = x;
  3515. return axis;
  3516. };
  3517. axis.ticks = function() {
  3518. if (!arguments.length) return tickArguments_;
  3519. tickArguments_ = arguments;
  3520. return axis;
  3521. };
  3522. axis.tickFormat = function(x) {
  3523. if (!arguments.length) return tickFormat_;
  3524. tickFormat_ = x;
  3525. return axis;
  3526. };
  3527. axis.tickSize = function(x, y, z) {
  3528. if (!arguments.length) return tickMajorSize;
  3529. var n = arguments.length - 1;
  3530. tickMajorSize = +x;
  3531. tickMinorSize = n > 1 ? +y : tickMajorSize;
  3532. tickEndSize = n > 0 ? +arguments[n] : tickMajorSize;
  3533. return axis;
  3534. };
  3535. axis.tickPadding = function(x) {
  3536. if (!arguments.length) return tickPadding;
  3537. tickPadding = +x;
  3538. return axis;
  3539. };
  3540. axis.tickSubdivide = function(x) {
  3541. if (!arguments.length) return tickSubdivide;
  3542. tickSubdivide = +x;
  3543. return axis;
  3544. };
  3545. return axis;
  3546. };
  3547. function d3_svg_axisX(selection, x) {
  3548. selection.attr("transform", function(d) { return "translate(" + x(d) + ",0)"; });
  3549. }
  3550. function d3_svg_axisY(selection, y) {
  3551. selection.attr("transform", function(d) { return "translate(0," + y(d) + ")"; });
  3552. }
  3553. function d3_svg_axisSubdivide(scale, ticks, m) {
  3554. subticks = [];
  3555. if (m && ticks.length > 1) {
  3556. var extent = d3_scaleExtent(scale.domain()),
  3557. subticks,
  3558. i = -1,
  3559. n = ticks.length,
  3560. d = (ticks[1] - ticks[0]) / ++m,
  3561. j,
  3562. v;
  3563. while (++i < n) {
  3564. for (j = m; --j > 0;) {
  3565. if ((v = +ticks[i] - j * d) >= extent[0]) {
  3566. subticks.push(v);
  3567. }
  3568. }
  3569. }
  3570. for (--i, j = 0; ++j < m && (v = +ticks[i] + j * d) < extent[1];) {
  3571. subticks.push(v);
  3572. }
  3573. }
  3574. return subticks;
  3575. }
  3576. d3.svg.brush = function() {
  3577. var event = d3.dispatch("brushstart", "brush", "brushend"),
  3578. x, // x-scale, optional
  3579. y, // y-scale, optional
  3580. extent = [[0, 0], [0, 0]]; // [x0, y0], [x1, y1]
  3581. function brush(g) {
  3582. var resizes = x && y ? ["n", "e", "s", "w", "nw", "ne", "se", "sw"]
  3583. : x ? ["e", "w"]
  3584. : y ? ["n", "s"]
  3585. : [];
  3586. g.each(function() {
  3587. var g = d3.select(this).on("mousedown.brush", down),
  3588. bg = g.selectAll(".background").data([0]),
  3589. fg = g.selectAll(".extent").data([0]),
  3590. tz = g.selectAll(".resize").data(resizes, String),
  3591. e;
  3592. // An invisible, mouseable area for starting a new brush.
  3593. bg.enter().append("rect")
  3594. .attr("class", "background")
  3595. .style("visibility", "hidden")
  3596. .style("pointer-events", "all")
  3597. .style("cursor", "crosshair");
  3598. // The visible brush extent; style this as you like!
  3599. fg.enter().append("rect")
  3600. .attr("class", "extent")
  3601. .style("cursor", "move");
  3602. // More invisible rects for resizing the extent.
  3603. tz.enter().append("rect")
  3604. .attr("class", function(d) { return "resize " + d; })
  3605. .attr("width", 6)
  3606. .attr("height", 6)
  3607. .style("visibility", "hidden")
  3608. .style("cursor", function(d) { return d3_svg_brushCursor[d]; });
  3609. // Update the resizers.
  3610. tz.style("pointer-events", brush.empty() ? "none" : "all");
  3611. // Remove any superfluous resizers.
  3612. tz.exit().remove();
  3613. // Initialize the background to fill the defined range.
  3614. // If the range isn't defined, you can post-process.
  3615. if (x) {
  3616. e = d3_scaleRange(x);
  3617. bg.attr("x", e[0]).attr("width", e[1] - e[0]);
  3618. d3_svg_brushRedrawX(g, extent);
  3619. }
  3620. if (y) {
  3621. e = d3_scaleRange(y);
  3622. bg.attr("y", e[0]).attr("height", e[1] - e[0]);
  3623. d3_svg_brushRedrawY(g, extent);
  3624. }
  3625. });
  3626. }
  3627. function down() {
  3628. var target = d3.select(d3.event.target);
  3629. // Store some global state for the duration of the brush gesture.
  3630. d3_svg_brush = brush;
  3631. d3_svg_brushTarget = this;
  3632. d3_svg_brushExtent = extent;
  3633. d3_svg_brushOffset = d3.svg.mouse(d3_svg_brushTarget);
  3634. // If the extent was clicked on, drag rather than brush;
  3635. // store the offset between the mouse and extent origin instead.
  3636. if (d3_svg_brushDrag = target.classed("extent")) {
  3637. d3_svg_brushOffset[0] = extent[0][0] - d3_svg_brushOffset[0];
  3638. d3_svg_brushOffset[1] = extent[0][1] - d3_svg_brushOffset[1];
  3639. }
  3640. // If a resizer was clicked on, record which side is to be resized.
  3641. // Also, set the offset to the opposite side.
  3642. else if (target.classed("resize")) {
  3643. d3_svg_brushResize = d3.event.target.__data__;
  3644. d3_svg_brushOffset[0] = extent[+/w$/.test(d3_svg_brushResize)][0];
  3645. d3_svg_brushOffset[1] = extent[+/^n/.test(d3_svg_brushResize)][1];
  3646. }
  3647. // If the ALT key is down when starting a brush, the center is at the mouse.
  3648. else if (d3.event.altKey) {
  3649. d3_svg_brushCenter = d3_svg_brushOffset.slice();
  3650. }
  3651. // Restrict which dimensions are resized.
  3652. d3_svg_brushX = !/^(n|s)$/.test(d3_svg_brushResize) && x;
  3653. d3_svg_brushY = !/^(e|w)$/.test(d3_svg_brushResize) && y;
  3654. // Notify listeners.
  3655. d3_svg_brushDispatch = dispatcher(this, arguments);
  3656. d3_svg_brushDispatch("brushstart");
  3657. d3_svg_brushMove();
  3658. d3_eventCancel();
  3659. }
  3660. function dispatcher(that, argumentz) {
  3661. return function(type) {
  3662. var e = d3.event;
  3663. try {
  3664. d3.event = {type: type, target: brush};
  3665. event[type].apply(that, argumentz);
  3666. } finally {
  3667. d3.event = e;
  3668. }
  3669. };
  3670. }
  3671. brush.x = function(z) {
  3672. if (!arguments.length) return x;
  3673. x = z;
  3674. return brush;
  3675. };
  3676. brush.y = function(z) {
  3677. if (!arguments.length) return y;
  3678. y = z;
  3679. return brush;
  3680. };
  3681. brush.extent = function(z) {
  3682. var x0, x1, y0, y1, t;
  3683. // Invert the pixel extent to data-space.
  3684. if (!arguments.length) {
  3685. if (x) {
  3686. x0 = extent[0][0], x1 = extent[1][0];
  3687. if (x.invert) x0 = x.invert(x0), x1 = x.invert(x1);
  3688. if (x1 < x0) t = x0, x0 = x1, x1 = t;
  3689. }
  3690. if (y) {
  3691. y0 = extent[0][1], y1 = extent[1][1];
  3692. if (y.invert) y0 = y.invert(y0), y1 = y.invert(y1);
  3693. if (y1 < y0) t = y0, y0 = y1, y1 = t;
  3694. }
  3695. return x && y ? [[x0, y0], [x1, y1]] : x ? [x0, x1] : y && [y0, y1];
  3696. }
  3697. // Scale the data-space extent to pixels.
  3698. if (x) {
  3699. x0 = z[0], x1 = z[1];
  3700. if (y) x0 = x0[0], x1 = x1[0];
  3701. if (x.invert) x0 = x(x0), x1 = x(x1);
  3702. if (x1 < x0) t = x0, x0 = x1, x1 = t;
  3703. extent[0][0] = x0, extent[1][0] = x1;
  3704. }
  3705. if (y) {
  3706. y0 = z[0], y1 = z[1];
  3707. if (x) y0 = y0[1], y1 = y1[1];
  3708. if (y.invert) y0 = y(y0), y1 = y(y1);
  3709. if (y1 < y0) t = y0, y0 = y1, y1 = t;
  3710. extent[0][1] = y0, extent[1][1] = y1;
  3711. }
  3712. return brush;
  3713. };
  3714. brush.clear = function() {
  3715. extent[0][0] =
  3716. extent[0][1] =
  3717. extent[1][0] =
  3718. extent[1][1] = 0;
  3719. return brush;
  3720. };
  3721. brush.empty = function() {
  3722. return (x && extent[0][0] === extent[1][0])
  3723. || (y && extent[0][1] === extent[1][1]);
  3724. };
  3725. d3.select(window)
  3726. .on("mousemove.brush", d3_svg_brushMove)
  3727. .on("mouseup.brush", d3_svg_brushUp)
  3728. .on("keydown.brush", d3_svg_brushKeydown)
  3729. .on("keyup.brush", d3_svg_brushKeyup);
  3730. return d3.rebind(brush, event, "on");
  3731. };
  3732. var d3_svg_brush,
  3733. d3_svg_brushDispatch,
  3734. d3_svg_brushTarget,
  3735. d3_svg_brushX,
  3736. d3_svg_brushY,
  3737. d3_svg_brushExtent,
  3738. d3_svg_brushDrag,
  3739. d3_svg_brushResize,
  3740. d3_svg_brushCenter,
  3741. d3_svg_brushOffset;
  3742. function d3_svg_brushRedrawX(g, extent) {
  3743. g.select(".extent").attr("x", extent[0][0]);
  3744. g.selectAll(".n,.s,.w,.nw,.sw").attr("x", extent[0][0] - 2);
  3745. g.selectAll(".e,.ne,.se").attr("x", extent[1][0] - 3);
  3746. g.selectAll(".extent,.n,.s").attr("width", extent[1][0] - extent[0][0]);
  3747. }
  3748. function d3_svg_brushRedrawY(g, extent) {
  3749. g.select(".extent").attr("y", extent[0][1]);
  3750. g.selectAll(".n,.e,.w,.nw,.ne").attr("y", extent[0][1] - 3);
  3751. g.selectAll(".s,.se,.sw").attr("y", extent[1][1] - 4);
  3752. g.selectAll(".extent,.e,.w").attr("height", extent[1][1] - extent[0][1]);
  3753. }
  3754. function d3_svg_brushKeydown() {
  3755. if (d3.event.keyCode == 32 && d3_svg_brushTarget && !d3_svg_brushDrag) {
  3756. d3_svg_brushCenter = null;
  3757. d3_svg_brushOffset[0] -= d3_svg_brushExtent[1][0];
  3758. d3_svg_brushOffset[1] -= d3_svg_brushExtent[1][1];
  3759. d3_svg_brushDrag = 2;
  3760. d3_eventCancel();
  3761. }
  3762. }
  3763. function d3_svg_brushKeyup() {
  3764. if (d3.event.keyCode == 32 && d3_svg_brushDrag == 2) {
  3765. d3_svg_brushOffset[0] += d3_svg_brushExtent[1][0];
  3766. d3_svg_brushOffset[1] += d3_svg_brushExtent[1][1];
  3767. d3_svg_brushDrag = 0;
  3768. d3_eventCancel();
  3769. }
  3770. }
  3771. function d3_svg_brushMove() {
  3772. if (d3_svg_brushOffset) {
  3773. var mouse = d3.svg.mouse(d3_svg_brushTarget),
  3774. g = d3.select(d3_svg_brushTarget);
  3775. if (!d3_svg_brushDrag) {
  3776. // If needed, determine the center from the current extent.
  3777. if (d3.event.altKey) {
  3778. if (!d3_svg_brushCenter) {
  3779. d3_svg_brushCenter = [
  3780. (d3_svg_brushExtent[0][0] + d3_svg_brushExtent[1][0]) / 2,
  3781. (d3_svg_brushExtent[0][1] + d3_svg_brushExtent[1][1]) / 2
  3782. ];
  3783. }
  3784. // Update the offset, for when the ALT key is released.
  3785. d3_svg_brushOffset[0] = d3_svg_brushExtent[+(mouse[0] < d3_svg_brushCenter[0])][0];
  3786. d3_svg_brushOffset[1] = d3_svg_brushExtent[+(mouse[1] < d3_svg_brushCenter[1])][1];
  3787. }
  3788. // When the ALT key is released, we clear the center.
  3789. else d3_svg_brushCenter = null;
  3790. }
  3791. // Update the brush extent for each dimension.
  3792. if (d3_svg_brushX) {
  3793. d3_svg_brushMove1(mouse, d3_svg_brushX, 0);
  3794. d3_svg_brushRedrawX(g, d3_svg_brushExtent);
  3795. }
  3796. if (d3_svg_brushY) {
  3797. d3_svg_brushMove1(mouse, d3_svg_brushY, 1);
  3798. d3_svg_brushRedrawY(g, d3_svg_brushExtent);
  3799. }
  3800. // Notify listeners.
  3801. d3_svg_brushDispatch("brush");
  3802. }
  3803. }
  3804. function d3_svg_brushMove1(mouse, scale, i) {
  3805. var range = d3_scaleRange(scale),
  3806. r0 = range[0],
  3807. r1 = range[1],
  3808. offset = d3_svg_brushOffset[i],
  3809. size = d3_svg_brushExtent[1][i] - d3_svg_brushExtent[0][i],
  3810. min,
  3811. max;
  3812. // When dragging, reduce the range by the extent size and offset.
  3813. if (d3_svg_brushDrag) {
  3814. r0 -= offset;
  3815. r1 -= size + offset;
  3816. }
  3817. // Clamp the mouse so that the extent fits within the range extent.
  3818. min = Math.max(r0, Math.min(r1, mouse[i]));
  3819. // Compute the new extent bounds.
  3820. if (d3_svg_brushDrag) {
  3821. max = (min += offset) + size;
  3822. } else {
  3823. // If the ALT key is pressed, then preserve the center of the extent.
  3824. if (d3_svg_brushCenter) offset = Math.max(r0, Math.min(r1, 2 * d3_svg_brushCenter[i] - min));
  3825. // Compute the min and max of the offset and mouse.
  3826. if (offset < min) {
  3827. max = min;
  3828. min = offset;
  3829. } else {
  3830. max = offset;
  3831. }
  3832. }
  3833. // Update the stored bounds.
  3834. d3_svg_brushExtent[0][i] = min;
  3835. d3_svg_brushExtent[1][i] = max;
  3836. }
  3837. function d3_svg_brushUp() {
  3838. if (d3_svg_brushOffset) {
  3839. d3_svg_brushMove();
  3840. d3.select(d3_svg_brushTarget).selectAll(".resize").style("pointer-events", d3_svg_brush.empty() ? "none" : "all");
  3841. d3_svg_brushDispatch("brushend");
  3842. d3_svg_brush =
  3843. d3_svg_brushDispatch =
  3844. d3_svg_brushTarget =
  3845. d3_svg_brushX =
  3846. d3_svg_brushY =
  3847. d3_svg_brushExtent =
  3848. d3_svg_brushDrag =
  3849. d3_svg_brushResize =
  3850. d3_svg_brushCenter =
  3851. d3_svg_brushOffset = null;
  3852. d3_eventCancel();
  3853. }
  3854. }
  3855. var d3_svg_brushCursor = {
  3856. n: "ns-resize",
  3857. e: "ew-resize",
  3858. s: "ns-resize",
  3859. w: "ew-resize",
  3860. nw: "nwse-resize",
  3861. ne: "nesw-resize",
  3862. se: "nwse-resize",
  3863. sw: "nesw-resize"
  3864. };
  3865. d3.behavior = {};
  3866. // TODO Track touch points by identifier.
  3867. d3.behavior.drag = function() {
  3868. var event = d3.dispatch("drag", "dragstart", "dragend"),
  3869. origin = null;
  3870. function drag() {
  3871. this
  3872. .on("mousedown.drag", mousedown)
  3873. .on("touchstart.drag", mousedown);
  3874. d3.select(window)
  3875. .on("mousemove.drag", d3_behavior_dragMove)
  3876. .on("touchmove.drag", d3_behavior_dragMove)
  3877. .on("mouseup.drag", d3_behavior_dragUp, true)
  3878. .on("touchend.drag", d3_behavior_dragUp, true)
  3879. .on("click.drag", d3_behavior_dragClick, true);
  3880. }
  3881. // snapshot the local context for subsequent dispatch
  3882. function start() {
  3883. d3_behavior_dragEvent = event;
  3884. d3_behavior_dragEventTarget = d3.event.target;
  3885. d3_behavior_dragTarget = this;
  3886. d3_behavior_dragArguments = arguments;
  3887. d3_behavior_dragOrigin = d3_behavior_dragPoint();
  3888. if (origin) {
  3889. d3_behavior_dragOffset = origin.apply(d3_behavior_dragTarget, d3_behavior_dragArguments);
  3890. d3_behavior_dragOffset = [d3_behavior_dragOffset.x - d3_behavior_dragOrigin[0], d3_behavior_dragOffset.y - d3_behavior_dragOrigin[1]];
  3891. } else {
  3892. d3_behavior_dragOffset = [0, 0];
  3893. }
  3894. d3_behavior_dragMoved = 0;
  3895. }
  3896. function mousedown() {
  3897. start.apply(this, arguments);
  3898. d3_behavior_dragDispatch("dragstart");
  3899. }
  3900. drag.origin = function(x) {
  3901. if (!arguments.length) return origin;
  3902. origin = x;
  3903. return drag;
  3904. };
  3905. return d3.rebind(drag, event, "on");
  3906. };
  3907. var d3_behavior_dragEvent,
  3908. d3_behavior_dragEventTarget,
  3909. d3_behavior_dragTarget,
  3910. d3_behavior_dragArguments,
  3911. d3_behavior_dragOffset,
  3912. d3_behavior_dragOrigin,
  3913. d3_behavior_dragMoved;
  3914. function d3_behavior_dragDispatch(type) {
  3915. var p = d3_behavior_dragPoint(),
  3916. o = d3.event,
  3917. e = d3.event = {type: type};
  3918. if (p) {
  3919. e.x = p[0] + d3_behavior_dragOffset[0];
  3920. e.y = p[1] + d3_behavior_dragOffset[1];
  3921. e.dx = p[0] - d3_behavior_dragOrigin[0];
  3922. e.dy = p[1] - d3_behavior_dragOrigin[1];
  3923. d3_behavior_dragMoved |= e.dx | e.dy;
  3924. d3_behavior_dragOrigin = p;
  3925. }
  3926. try {
  3927. d3_behavior_dragEvent[type].apply(d3_behavior_dragTarget, d3_behavior_dragArguments);
  3928. } finally {
  3929. d3.event = o;
  3930. }
  3931. o.stopPropagation();
  3932. o.preventDefault();
  3933. }
  3934. function d3_behavior_dragPoint() {
  3935. var p = d3_behavior_dragTarget.parentNode,
  3936. t = d3.event.changedTouches;
  3937. return p && (t
  3938. ? d3.svg.touches(p, t)[0]
  3939. : d3.svg.mouse(p));
  3940. }
  3941. function d3_behavior_dragMove() {
  3942. if (!d3_behavior_dragTarget) return;
  3943. var parent = d3_behavior_dragTarget.parentNode;
  3944. // O NOES! The drag element was removed from the DOM.
  3945. if (!parent) return d3_behavior_dragUp();
  3946. d3_behavior_dragDispatch("drag");
  3947. d3_eventCancel();
  3948. }
  3949. function d3_behavior_dragUp() {
  3950. if (!d3_behavior_dragTarget) return;
  3951. d3_behavior_dragDispatch("dragend");
  3952. // If the node was moved, prevent the mouseup from propagating.
  3953. // Also prevent the subsequent click from propagating (e.g., for anchors).
  3954. if (d3_behavior_dragMoved) {
  3955. d3_eventCancel();
  3956. d3_behavior_dragMoved = d3.event.target === d3_behavior_dragEventTarget;
  3957. }
  3958. d3_behavior_dragEvent =
  3959. d3_behavior_dragEventTarget =
  3960. d3_behavior_dragTarget =
  3961. d3_behavior_dragArguments =
  3962. d3_behavior_dragOffset =
  3963. d3_behavior_dragOrigin = null;
  3964. }
  3965. function d3_behavior_dragClick() {
  3966. if (d3_behavior_dragMoved) {
  3967. d3_eventCancel();
  3968. d3_behavior_dragMoved = 0;
  3969. }
  3970. }
  3971. // TODO unbind zoom behavior?
  3972. d3.behavior.zoom = function() {
  3973. var xyz = [0, 0, 0],
  3974. event = d3.dispatch("zoom"),
  3975. extent = d3_behavior_zoomInfiniteExtent;
  3976. function zoom() {
  3977. this
  3978. .on("mousedown.zoom", mousedown)
  3979. .on("mousewheel.zoom", mousewheel)
  3980. .on("DOMMouseScroll.zoom", mousewheel)
  3981. .on("dblclick.zoom", dblclick)
  3982. .on("touchstart.zoom", touchstart);
  3983. d3.select(window)
  3984. .on("mousemove.zoom", d3_behavior_zoomMousemove)
  3985. .on("mouseup.zoom", d3_behavior_zoomMouseup)
  3986. .on("touchmove.zoom", d3_behavior_zoomTouchmove)
  3987. .on("touchend.zoom", d3_behavior_zoomTouchup)
  3988. .on("click.zoom", d3_behavior_zoomClick, true);
  3989. }
  3990. // snapshot the local context for subsequent dispatch
  3991. function start() {
  3992. d3_behavior_zoomXyz = xyz;
  3993. d3_behavior_zoomExtent = extent;
  3994. d3_behavior_zoomDispatch = event.zoom;
  3995. d3_behavior_zoomEventTarget = d3.event.target;
  3996. d3_behavior_zoomTarget = this;
  3997. d3_behavior_zoomArguments = arguments;
  3998. }
  3999. function mousedown() {
  4000. start.apply(this, arguments);
  4001. d3_behavior_zoomPanning = d3_behavior_zoomLocation(d3.svg.mouse(d3_behavior_zoomTarget));
  4002. d3_behavior_zoomMoved = 0;
  4003. d3.event.preventDefault();
  4004. window.focus();
  4005. }
  4006. // store starting mouse location
  4007. function mousewheel() {
  4008. start.apply(this, arguments);
  4009. if (!d3_behavior_zoomZooming) d3_behavior_zoomZooming = d3_behavior_zoomLocation(d3.svg.mouse(d3_behavior_zoomTarget));
  4010. d3_behavior_zoomTo(d3_behavior_zoomDelta() + xyz[2], d3.svg.mouse(d3_behavior_zoomTarget), d3_behavior_zoomZooming);
  4011. }
  4012. function dblclick() {
  4013. start.apply(this, arguments);
  4014. var mouse = d3.svg.mouse(d3_behavior_zoomTarget);
  4015. d3_behavior_zoomTo(d3.event.shiftKey ? Math.ceil(xyz[2] - 1) : Math.floor(xyz[2] + 1), mouse, d3_behavior_zoomLocation(mouse));
  4016. }
  4017. // doubletap detection
  4018. function touchstart() {
  4019. start.apply(this, arguments);
  4020. var touches = d3_behavior_zoomTouchup(),
  4021. touch,
  4022. now = Date.now();
  4023. if ((touches.length === 1) && (now - d3_behavior_zoomLast < 300)) {
  4024. d3_behavior_zoomTo(1 + Math.floor(xyz[2]), touch = touches[0], d3_behavior_zoomLocations[touch.identifier]);
  4025. }
  4026. d3_behavior_zoomLast = now;
  4027. }
  4028. zoom.extent = function(x) {
  4029. if (!arguments.length) return extent;
  4030. extent = x == null ? d3_behavior_zoomInfiniteExtent : x;
  4031. return zoom;
  4032. };
  4033. return d3.rebind(zoom, event, "on");
  4034. };
  4035. var d3_behavior_zoomDiv,
  4036. d3_behavior_zoomPanning,
  4037. d3_behavior_zoomZooming,
  4038. d3_behavior_zoomLocations = {}, // identifier -> location
  4039. d3_behavior_zoomLast = 0,
  4040. d3_behavior_zoomXyz,
  4041. d3_behavior_zoomExtent,
  4042. d3_behavior_zoomDispatch,
  4043. d3_behavior_zoomEventTarget,
  4044. d3_behavior_zoomTarget,
  4045. d3_behavior_zoomArguments,
  4046. d3_behavior_zoomMoved;
  4047. function d3_behavior_zoomLocation(point) {
  4048. return [
  4049. point[0] - d3_behavior_zoomXyz[0],
  4050. point[1] - d3_behavior_zoomXyz[1],
  4051. d3_behavior_zoomXyz[2]
  4052. ];
  4053. }
  4054. // detect the pixels that would be scrolled by this wheel event
  4055. function d3_behavior_zoomDelta() {
  4056. // mousewheel events are totally broken!
  4057. // https://bugs.webkit.org/show_bug.cgi?id=40441
  4058. // not only that, but Chrome and Safari differ in re. to acceleration!
  4059. if (!d3_behavior_zoomDiv) {
  4060. d3_behavior_zoomDiv = d3.select("body").append("div")
  4061. .style("visibility", "hidden")
  4062. .style("top", 0)
  4063. .style("height", 0)
  4064. .style("width", 0)
  4065. .style("overflow-y", "scroll")
  4066. .append("div")
  4067. .style("height", "2000px")
  4068. .node().parentNode;
  4069. }
  4070. var e = d3.event, delta;
  4071. try {
  4072. d3_behavior_zoomDiv.scrollTop = 1000;
  4073. d3_behavior_zoomDiv.dispatchEvent(e);
  4074. delta = 1000 - d3_behavior_zoomDiv.scrollTop;
  4075. } catch (error) {
  4076. delta = e.wheelDelta || (-e.detail * 5);
  4077. }
  4078. return delta * .005;
  4079. }
  4080. // Note: Since we don't rotate, it's possible for the touches to become
  4081. // slightly detached from their original positions. Thus, we recompute the
  4082. // touch points on touchend as well as touchstart!
  4083. function d3_behavior_zoomTouchup() {
  4084. var touches = d3.svg.touches(d3_behavior_zoomTarget),
  4085. i = -1,
  4086. n = touches.length,
  4087. touch;
  4088. while (++i < n) d3_behavior_zoomLocations[(touch = touches[i]).identifier] = d3_behavior_zoomLocation(touch);
  4089. return touches;
  4090. }
  4091. function d3_behavior_zoomTouchmove() {
  4092. var touches = d3.svg.touches(d3_behavior_zoomTarget);
  4093. switch (touches.length) {
  4094. // single-touch pan
  4095. case 1: {
  4096. var touch = touches[0];
  4097. d3_behavior_zoomTo(d3_behavior_zoomXyz[2], touch, d3_behavior_zoomLocations[touch.identifier]);
  4098. break;
  4099. }
  4100. // double-touch pan + zoom
  4101. case 2: {
  4102. var p0 = touches[0],
  4103. p1 = touches[1],
  4104. p2 = [(p0[0] + p1[0]) / 2, (p0[1] + p1[1]) / 2],
  4105. l0 = d3_behavior_zoomLocations[p0.identifier],
  4106. l1 = d3_behavior_zoomLocations[p1.identifier],
  4107. l2 = [(l0[0] + l1[0]) / 2, (l0[1] + l1[1]) / 2, l0[2]];
  4108. d3_behavior_zoomTo(Math.log(d3.event.scale) / Math.LN2 + l0[2], p2, l2);
  4109. break;
  4110. }
  4111. }
  4112. }
  4113. function d3_behavior_zoomMousemove() {
  4114. d3_behavior_zoomZooming = null;
  4115. if (d3_behavior_zoomPanning) {
  4116. d3_behavior_zoomMoved = 1;
  4117. d3_behavior_zoomTo(d3_behavior_zoomXyz[2], d3.svg.mouse(d3_behavior_zoomTarget), d3_behavior_zoomPanning);
  4118. }
  4119. }
  4120. function d3_behavior_zoomMouseup() {
  4121. if (d3_behavior_zoomPanning) {
  4122. if (d3_behavior_zoomMoved) {
  4123. d3_eventCancel();
  4124. d3_behavior_zoomMoved = d3_behavior_zoomEventTarget === d3.event.target;
  4125. }
  4126. d3_behavior_zoomXyz =
  4127. d3_behavior_zoomExtent =
  4128. d3_behavior_zoomDispatch =
  4129. d3_behavior_zoomEventTarget =
  4130. d3_behavior_zoomTarget =
  4131. d3_behavior_zoomArguments =
  4132. d3_behavior_zoomPanning = null;
  4133. }
  4134. }
  4135. function d3_behavior_zoomClick() {
  4136. if (d3_behavior_zoomMoved) {
  4137. d3_eventCancel();
  4138. d3_behavior_zoomMoved = 0;
  4139. }
  4140. }
  4141. function d3_behavior_zoomTo(z, x0, x1) {
  4142. z = d3_behavior_zoomExtentClamp(z, 2);
  4143. var j = Math.pow(2, d3_behavior_zoomXyz[2]),
  4144. k = Math.pow(2, z),
  4145. K = Math.pow(2, (d3_behavior_zoomXyz[2] = z) - x1[2]),
  4146. x_ = d3_behavior_zoomXyz[0],
  4147. y_ = d3_behavior_zoomXyz[1],
  4148. x = d3_behavior_zoomXyz[0] = d3_behavior_zoomExtentClamp((x0[0] - x1[0] * K), 0, k),
  4149. y = d3_behavior_zoomXyz[1] = d3_behavior_zoomExtentClamp((x0[1] - x1[1] * K), 1, k),
  4150. o = d3.event; // Events can be reentrant (e.g., focus).
  4151. d3.event = {
  4152. scale: k,
  4153. translate: [x, y],
  4154. transform: function(sx, sy) {
  4155. if (sx) transform(sx, x_, x);
  4156. if (sy) transform(sy, y_, y);
  4157. }
  4158. };
  4159. function transform(scale, a, b) {
  4160. scale.domain(scale.range().map(function(v) { return scale.invert(((v - b) * j) / k + a); }));
  4161. }
  4162. try {
  4163. d3_behavior_zoomDispatch.apply(d3_behavior_zoomTarget, d3_behavior_zoomArguments);
  4164. } finally {
  4165. d3.event = o;
  4166. }
  4167. o.preventDefault();
  4168. }
  4169. var d3_behavior_zoomInfiniteExtent = [
  4170. [-Infinity, Infinity],
  4171. [-Infinity, Infinity],
  4172. [-Infinity, Infinity]
  4173. ];
  4174. function d3_behavior_zoomExtentClamp(x, i, k) {
  4175. var range = d3_behavior_zoomExtent[i],
  4176. r0 = range[0],
  4177. r1 = range[1];
  4178. return arguments.length === 3
  4179. ? Math.max(r1 * (r1 === Infinity ? -Infinity : 1 / k - 1),
  4180. Math.min(r0 === -Infinity ? Infinity : r0, x / k)) * k
  4181. : Math.max(r0, Math.min(r1, x));
  4182. }
  4183. })();