nv.d3.js 739 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125
  1. /* nvd3 version 1.8.6-dev (https://github.com/novus/nvd3) 2018-02-24 */
  2. (function(){
  3. // set up main nv object
  4. var nv = {};
  5. // the major global objects under the nv namespace
  6. nv.dev = false; //set false when in production
  7. nv.tooltip = nv.tooltip || {}; // For the tooltip system
  8. nv.utils = nv.utils || {}; // Utility subsystem
  9. nv.models = nv.models || {}; //stores all the possible models/components
  10. nv.charts = {}; //stores all the ready to use charts
  11. nv.logs = {}; //stores some statistics and potential error messages
  12. nv.dom = {}; //DOM manipulation functions
  13. // Node/CommonJS - require D3
  14. if (typeof(module) !== 'undefined' && typeof(exports) !== 'undefined' && typeof(d3) == 'undefined') {
  15. d3 = require('d3');
  16. }
  17. nv.dispatch = d3.dispatch('render_start', 'render_end');
  18. // Function bind polyfill
  19. // Needed ONLY for phantomJS as it's missing until version 2.0 which is unreleased as of this comment
  20. // https://github.com/ariya/phantomjs/issues/10522
  21. // http://kangax.github.io/compat-table/es5/#Function.prototype.bind
  22. // phantomJS is used for running the test suite
  23. if (!Function.prototype.bind) {
  24. Function.prototype.bind = function (oThis) {
  25. if (typeof this !== "function") {
  26. // closest thing possible to the ECMAScript 5 internal IsCallable function
  27. throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
  28. }
  29. var aArgs = Array.prototype.slice.call(arguments, 1),
  30. fToBind = this,
  31. fNOP = function () {},
  32. fBound = function () {
  33. return fToBind.apply(this instanceof fNOP && oThis
  34. ? this
  35. : oThis,
  36. aArgs.concat(Array.prototype.slice.call(arguments)));
  37. };
  38. fNOP.prototype = this.prototype;
  39. fBound.prototype = new fNOP();
  40. return fBound;
  41. };
  42. }
  43. // Development render timers - disabled if dev = false
  44. if (nv.dev) {
  45. nv.dispatch.on('render_start', function(e) {
  46. nv.logs.startTime = +new Date();
  47. });
  48. nv.dispatch.on('render_end', function(e) {
  49. nv.logs.endTime = +new Date();
  50. nv.logs.totalTime = nv.logs.endTime - nv.logs.startTime;
  51. nv.log('total', nv.logs.totalTime); // used for development, to keep track of graph generation times
  52. });
  53. }
  54. // Logs all arguments, and returns the last so you can test things in place
  55. // Note: in IE8 console.log is an object not a function, and if modernizr is used
  56. // then calling Function.prototype.bind with with anything other than a function
  57. // causes a TypeError to be thrown.
  58. nv.log = function() {
  59. if (nv.dev && window.console && console.log && console.log.apply)
  60. console.log.apply(console, arguments);
  61. else if (nv.dev && window.console && typeof console.log == "function" && Function.prototype.bind) {
  62. var log = Function.prototype.bind.call(console.log, console);
  63. log.apply(console, arguments);
  64. }
  65. return arguments[arguments.length - 1];
  66. };
  67. // print console warning, should be used by deprecated functions
  68. nv.deprecated = function(name, info) {
  69. if (console && console.warn) {
  70. console.warn('nvd3 warning: `' + name + '` has been deprecated. ', info || '');
  71. }
  72. };
  73. // The nv.render function is used to queue up chart rendering
  74. // in non-blocking async functions.
  75. // When all queued charts are done rendering, nv.dispatch.render_end is invoked.
  76. nv.render = function render(step) {
  77. // number of graphs to generate in each timeout loop
  78. step = step || 1;
  79. nv.render.active = true;
  80. nv.dispatch.render_start();
  81. var renderLoop = function() {
  82. var chart, graph;
  83. for (var i = 0; i < step && (graph = nv.render.queue[i]); i++) {
  84. chart = graph.generate();
  85. if (typeof graph.callback == typeof(Function)) graph.callback(chart);
  86. }
  87. nv.render.queue.splice(0, i);
  88. if (nv.render.queue.length) {
  89. setTimeout(renderLoop);
  90. }
  91. else {
  92. nv.dispatch.render_end();
  93. nv.render.active = false;
  94. }
  95. };
  96. setTimeout(renderLoop);
  97. };
  98. nv.render.active = false;
  99. nv.render.queue = [];
  100. /*
  101. Adds a chart to the async rendering queue. This method can take arguments in two forms:
  102. nv.addGraph({
  103. generate: <Function>
  104. callback: <Function>
  105. })
  106. or
  107. nv.addGraph(<generate Function>, <callback Function>)
  108. The generate function should contain code that creates the NVD3 model, sets options
  109. on it, adds data to an SVG element, and invokes the chart model. The generate function
  110. should return the chart model. See examples/lineChart.html for a usage example.
  111. The callback function is optional, and it is called when the generate function completes.
  112. */
  113. nv.addGraph = function(obj) {
  114. if (typeof arguments[0] === typeof(Function)) {
  115. obj = {generate: arguments[0], callback: arguments[1]};
  116. }
  117. nv.render.queue.push(obj);
  118. if (!nv.render.active) {
  119. nv.render();
  120. }
  121. };
  122. // Node/CommonJS exports
  123. if (typeof(module) !== 'undefined' && typeof(exports) !== 'undefined') {
  124. module.exports = nv;
  125. }
  126. if (typeof(window) !== 'undefined') {
  127. window.nv = nv;
  128. }
  129. /* Facade for queueing DOM write operations
  130. * with Fastdom (https://github.com/wilsonpage/fastdom)
  131. * if available.
  132. * This could easily be extended to support alternate
  133. * implementations in the future.
  134. */
  135. nv.dom.write = function(callback) {
  136. if (window.fastdom !== undefined) {
  137. return fastdom.mutate(callback);
  138. }
  139. return callback();
  140. };
  141. /* Facade for queueing DOM read operations
  142. * with Fastdom (https://github.com/wilsonpage/fastdom)
  143. * if available.
  144. * This could easily be extended to support alternate
  145. * implementations in the future.
  146. */
  147. nv.dom.read = function(callback) {
  148. if (window.fastdom !== undefined) {
  149. return fastdom.measure(callback);
  150. }
  151. return callback();
  152. };
  153. /* Utility class to handle creation of an interactive layer.
  154. This places a rectangle on top of the chart. When you mouse move over it, it sends a dispatch
  155. containing the X-coordinate. It can also render a vertical line where the mouse is located.
  156. dispatch.elementMousemove is the important event to latch onto. It is fired whenever the mouse moves over
  157. the rectangle. The dispatch is given one object which contains the mouseX/Y location.
  158. It also has 'pointXValue', which is the conversion of mouseX to the x-axis scale.
  159. */
  160. nv.interactiveGuideline = function() {
  161. "use strict";
  162. var margin = { left: 0, top: 0 } //Pass the chart's top and left magins. Used to calculate the mouseX/Y.
  163. , width = null
  164. , height = null
  165. , xScale = d3.scale.linear()
  166. , dispatch = d3.dispatch('elementMousemove', 'elementMouseout', 'elementClick', 'elementDblclick', 'elementMouseDown', 'elementMouseUp')
  167. , showGuideLine = true
  168. , svgContainer = null // Must pass the chart's svg, we'll use its mousemove event.
  169. , tooltip = nv.models.tooltip()
  170. , isMSIE = window.ActiveXObject// Checkt if IE by looking for activeX. (excludes IE11)
  171. ;
  172. tooltip
  173. .duration(0)
  174. .hideDelay(0)
  175. .hidden(false);
  176. function layer(selection) {
  177. selection.each(function(data) {
  178. var container = d3.select(this);
  179. var availableWidth = (width || 960), availableHeight = (height || 400);
  180. var wrap = container.selectAll("g.nv-wrap.nv-interactiveLineLayer")
  181. .data([data]);
  182. var wrapEnter = wrap.enter()
  183. .append("g").attr("class", " nv-wrap nv-interactiveLineLayer");
  184. wrapEnter.append("g").attr("class","nv-interactiveGuideLine");
  185. if (!svgContainer) {
  186. return;
  187. }
  188. function mouseHandler() {
  189. var mouseX = d3.event.clientX - this.getBoundingClientRect().left;
  190. var mouseY = d3.event.clientY - this.getBoundingClientRect().top;
  191. var subtractMargin = true;
  192. var mouseOutAnyReason = false;
  193. if (isMSIE) {
  194. /*
  195. D3.js (or maybe SVG.getScreenCTM) has a nasty bug in Internet Explorer 10.
  196. d3.mouse() returns incorrect X,Y mouse coordinates when mouse moving
  197. over a rect in IE 10.
  198. However, d3.event.offsetX/Y also returns the mouse coordinates
  199. relative to the triggering <rect>. So we use offsetX/Y on IE.
  200. */
  201. mouseX = d3.event.offsetX;
  202. mouseY = d3.event.offsetY;
  203. /*
  204. On IE, if you attach a mouse event listener to the <svg> container,
  205. it will actually trigger it for all the child elements (like <path>, <circle>, etc).
  206. When this happens on IE, the offsetX/Y is set to where ever the child element
  207. is located.
  208. As a result, we do NOT need to subtract margins to figure out the mouse X/Y
  209. position under this scenario. Removing the line below *will* cause
  210. the interactive layer to not work right on IE.
  211. */
  212. if(d3.event.target.tagName !== "svg") {
  213. subtractMargin = false;
  214. }
  215. if (d3.event.target.className.baseVal.match("nv-legend")) {
  216. mouseOutAnyReason = true;
  217. }
  218. }
  219. if(subtractMargin) {
  220. mouseX -= margin.left;
  221. mouseY -= margin.top;
  222. }
  223. /* If mouseX/Y is outside of the chart's bounds,
  224. trigger a mouseOut event.
  225. */
  226. if (d3.event.type === 'mouseout'
  227. || mouseX < 0 || mouseY < 0
  228. || mouseX > availableWidth || mouseY > availableHeight
  229. || (d3.event.relatedTarget && d3.event.relatedTarget.ownerSVGElement === undefined)
  230. || mouseOutAnyReason
  231. ) {
  232. if (isMSIE) {
  233. if (d3.event.relatedTarget
  234. && d3.event.relatedTarget.ownerSVGElement === undefined
  235. && (d3.event.relatedTarget.className === undefined
  236. || d3.event.relatedTarget.className.match(tooltip.nvPointerEventsClass))) {
  237. return;
  238. }
  239. }
  240. dispatch.elementMouseout({
  241. mouseX: mouseX,
  242. mouseY: mouseY
  243. });
  244. layer.renderGuideLine(null); //hide the guideline
  245. tooltip.hidden(true);
  246. return;
  247. } else {
  248. tooltip.hidden(false);
  249. }
  250. var scaleIsOrdinal = typeof xScale.rangeBands === 'function';
  251. var pointXValue = undefined;
  252. // Ordinal scale has no invert method
  253. if (scaleIsOrdinal) {
  254. var elementIndex = d3.bisect(xScale.range(), mouseX) - 1;
  255. // Check if mouseX is in the range band
  256. if (xScale.range()[elementIndex] + xScale.rangeBand() >= mouseX) {
  257. pointXValue = xScale.domain()[d3.bisect(xScale.range(), mouseX) - 1];
  258. }
  259. else {
  260. dispatch.elementMouseout({
  261. mouseX: mouseX,
  262. mouseY: mouseY
  263. });
  264. layer.renderGuideLine(null); //hide the guideline
  265. tooltip.hidden(true);
  266. return;
  267. }
  268. }
  269. else {
  270. pointXValue = xScale.invert(mouseX);
  271. }
  272. dispatch.elementMousemove({
  273. mouseX: mouseX,
  274. mouseY: mouseY,
  275. pointXValue: pointXValue
  276. });
  277. //If user double clicks the layer, fire a elementDblclick
  278. if (d3.event.type === "dblclick") {
  279. dispatch.elementDblclick({
  280. mouseX: mouseX,
  281. mouseY: mouseY,
  282. pointXValue: pointXValue
  283. });
  284. }
  285. // if user single clicks the layer, fire elementClick
  286. if (d3.event.type === 'click') {
  287. dispatch.elementClick({
  288. mouseX: mouseX,
  289. mouseY: mouseY,
  290. pointXValue: pointXValue
  291. });
  292. }
  293. // if user presses mouse down the layer, fire elementMouseDown
  294. if (d3.event.type === 'mousedown') {
  295. dispatch.elementMouseDown({
  296. mouseX: mouseX,
  297. mouseY: mouseY,
  298. pointXValue: pointXValue
  299. });
  300. }
  301. // if user presses mouse down the layer, fire elementMouseUp
  302. if (d3.event.type === 'mouseup') {
  303. dispatch.elementMouseUp({
  304. mouseX: mouseX,
  305. mouseY: mouseY,
  306. pointXValue: pointXValue
  307. });
  308. }
  309. }
  310. svgContainer
  311. .on("touchmove",mouseHandler)
  312. .on("mousemove",mouseHandler, true)
  313. .on("mouseout" ,mouseHandler,true)
  314. .on("mousedown" ,mouseHandler,true)
  315. .on("mouseup" ,mouseHandler,true)
  316. .on("dblclick" ,mouseHandler)
  317. .on("click", mouseHandler)
  318. ;
  319. layer.guideLine = null;
  320. //Draws a vertical guideline at the given X postion.
  321. layer.renderGuideLine = function(x) {
  322. if (!showGuideLine) return;
  323. if (layer.guideLine && layer.guideLine.attr("x1") === x) return;
  324. nv.dom.write(function() {
  325. var line = wrap.select(".nv-interactiveGuideLine")
  326. .selectAll("line")
  327. .data((x != null) ? [nv.utils.NaNtoZero(x)] : [], String);
  328. line.enter()
  329. .append("line")
  330. .attr("class", "nv-guideline")
  331. .attr("x1", function(d) { return d;})
  332. .attr("x2", function(d) { return d;})
  333. .attr("y1", availableHeight)
  334. .attr("y2",0);
  335. line.exit().remove();
  336. });
  337. }
  338. });
  339. }
  340. layer.dispatch = dispatch;
  341. layer.tooltip = tooltip;
  342. layer.margin = function(_) {
  343. if (!arguments.length) return margin;
  344. margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
  345. margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
  346. return layer;
  347. };
  348. layer.width = function(_) {
  349. if (!arguments.length) return width;
  350. width = _;
  351. return layer;
  352. };
  353. layer.height = function(_) {
  354. if (!arguments.length) return height;
  355. height = _;
  356. return layer;
  357. };
  358. layer.xScale = function(_) {
  359. if (!arguments.length) return xScale;
  360. xScale = _;
  361. return layer;
  362. };
  363. layer.showGuideLine = function(_) {
  364. if (!arguments.length) return showGuideLine;
  365. showGuideLine = _;
  366. return layer;
  367. };
  368. layer.svgContainer = function(_) {
  369. if (!arguments.length) return svgContainer;
  370. svgContainer = _;
  371. return layer;
  372. };
  373. return layer;
  374. };
  375. /* Utility class that uses d3.bisect to find the index in a given array, where a search value can be inserted.
  376. This is different from normal bisectLeft; this function finds the nearest index to insert the search value.
  377. For instance, lets say your array is [1,2,3,5,10,30], and you search for 28.
  378. Normal d3.bisectLeft will return 4, because 28 is inserted after the number 10. But interactiveBisect will return 5
  379. because 28 is closer to 30 than 10.
  380. Unit tests can be found in: interactiveBisectTest.html
  381. Has the following known issues:
  382. * Will not work if the data points move backwards (ie, 10,9,8,7, etc) or if the data points are in random order.
  383. * Won't work if there are duplicate x coordinate values.
  384. */
  385. nv.interactiveBisect = function (values, searchVal, xAccessor) {
  386. "use strict";
  387. if (! (values instanceof Array)) {
  388. return null;
  389. }
  390. var _xAccessor;
  391. if (typeof xAccessor !== 'function') {
  392. _xAccessor = function(d) {
  393. return d.x;
  394. }
  395. } else {
  396. _xAccessor = xAccessor;
  397. }
  398. var _cmp = function(d, v) {
  399. // Accessors are no longer passed the index of the element along with
  400. // the element itself when invoked by d3.bisector.
  401. //
  402. // Starting at D3 v3.4.4, d3.bisector() started inspecting the
  403. // function passed to determine if it should consider it an accessor
  404. // or a comparator. This meant that accessors that take two arguments
  405. // (expecting an index as the second parameter) are treated as
  406. // comparators where the second argument is the search value against
  407. // which the first argument is compared.
  408. return _xAccessor(d) - v;
  409. };
  410. var bisect = d3.bisector(_cmp).left;
  411. var index = d3.max([0, bisect(values,searchVal) - 1]);
  412. var currentValue = _xAccessor(values[index]);
  413. if (typeof currentValue === 'undefined') {
  414. currentValue = index;
  415. }
  416. if (currentValue === searchVal) {
  417. return index; //found exact match
  418. }
  419. var nextIndex = d3.min([index+1, values.length - 1]);
  420. var nextValue = _xAccessor(values[nextIndex]);
  421. if (typeof nextValue === 'undefined') {
  422. nextValue = nextIndex;
  423. }
  424. if (Math.abs(nextValue - searchVal) >= Math.abs(currentValue - searchVal)) {
  425. return index;
  426. } else {
  427. return nextIndex
  428. }
  429. };
  430. /*
  431. Returns the index in the array "values" that is closest to searchVal.
  432. Only returns an index if searchVal is within some "threshold".
  433. Otherwise, returns null.
  434. */
  435. nv.nearestValueIndex = function (values, searchVal, threshold) {
  436. "use strict";
  437. var yDistMax = Infinity, indexToHighlight = null;
  438. values.forEach(function(d,i) {
  439. var delta = Math.abs(searchVal - d);
  440. if ( d != null && delta <= yDistMax && delta < threshold) {
  441. yDistMax = delta;
  442. indexToHighlight = i;
  443. }
  444. });
  445. return indexToHighlight;
  446. };
  447. /* Model which can be instantiated to handle tooltip rendering.
  448. Example usage:
  449. var tip = nv.models.tooltip().gravity('w').distance(23)
  450. .data(myDataObject);
  451. tip(); //just invoke the returned function to render tooltip.
  452. */
  453. nv.models.tooltip = function() {
  454. "use strict";
  455. /*
  456. Tooltip data. If data is given in the proper format, a consistent tooltip is generated.
  457. Example Format of data:
  458. {
  459. key: "Date",
  460. value: "August 2009",
  461. series: [
  462. {key: "Series 1", value: "Value 1", color: "#000"},
  463. {key: "Series 2", value: "Value 2", color: "#00f"}
  464. ]
  465. }
  466. */
  467. var id = "nvtooltip-" + Math.floor(Math.random() * 100000) // Generates a unique id when you create a new tooltip() object.
  468. , data = null
  469. , gravity = 'w' // Can be 'n','s','e','w'. Determines how tooltip is positioned.
  470. , distance = 25 // Distance to offset tooltip from the mouse location.
  471. , snapDistance = 0 // Tolerance allowed before tooltip is moved from its current position (creates 'snapping' effect)
  472. , classes = null // Attaches additional CSS classes to the tooltip DIV that is created.
  473. , hidden = true // Start off hidden, toggle with hide/show functions below.
  474. , hideDelay = 200 // Delay (in ms) before the tooltip hides after calling hide().
  475. , tooltip = null // d3 select of the tooltip div.
  476. , lastPosition = { left: null, top: null } // Last position the tooltip was in.
  477. , enabled = true // True -> tooltips are rendered. False -> don't render tooltips.
  478. , duration = 100 // Tooltip movement duration, in ms.
  479. , headerEnabled = true // If is to show the tooltip header.
  480. , nvPointerEventsClass = "nv-pointer-events-none" // CSS class to specify whether element should not have mouse events.
  481. ;
  482. // Format function for the tooltip values column.
  483. // d is value,
  484. // i is series index
  485. // p is point containing the value
  486. var valueFormatter = function(d, i, p) {
  487. return d;
  488. };
  489. // Format function for the tooltip header value.
  490. var headerFormatter = function(d) {
  491. return d;
  492. };
  493. var keyFormatter = function(d, i) {
  494. return d;
  495. };
  496. // By default, the tooltip model renders a beautiful table inside a DIV, returned as HTML
  497. // You can override this function if a custom tooltip is desired. For instance, you could directly manipulate
  498. // the DOM by accessing elem and returning false.
  499. var contentGenerator = function(d, elem) {
  500. if (d === null) {
  501. return '';
  502. }
  503. var table = d3.select(document.createElement("table"));
  504. if (headerEnabled) {
  505. var theadEnter = table.selectAll("thead")
  506. .data([d])
  507. .enter().append("thead");
  508. theadEnter.append("tr")
  509. .append("td")
  510. .attr("colspan", 3)
  511. .append("strong")
  512. .classed("x-value", true)
  513. .html(headerFormatter(d.value));
  514. }
  515. var tbodyEnter = table.selectAll("tbody")
  516. .data([d])
  517. .enter().append("tbody");
  518. var trowEnter = tbodyEnter.selectAll("tr")
  519. .data(function(p) { return p.series})
  520. .enter()
  521. .append("tr")
  522. .classed("highlight", function(p) { return p.highlight});
  523. trowEnter.append("td")
  524. .classed("legend-color-guide",true)
  525. .append("div")
  526. .style("background-color", function(p) { return p.color});
  527. trowEnter.append("td")
  528. .classed("key",true)
  529. .classed("total",function(p) { return !!p.total})
  530. .html(function(p, i) { return keyFormatter(p.key, i)});
  531. trowEnter.append("td")
  532. .classed("value",true)
  533. .html(function(p, i) { return valueFormatter(p.value, i, p) });
  534. trowEnter.filter(function (p,i) { return p.percent !== undefined }).append("td")
  535. .classed("percent", true)
  536. .html(function(p, i) { return "(" + d3.format('%')(p.percent) + ")" });
  537. trowEnter.selectAll("td").each(function(p) {
  538. if (p.highlight) {
  539. var opacityScale = d3.scale.linear().domain([0,1]).range(["#fff",p.color]);
  540. var opacity = 0.6;
  541. d3.select(this)
  542. .style("border-bottom-color", opacityScale(opacity))
  543. .style("border-top-color", opacityScale(opacity))
  544. ;
  545. }
  546. });
  547. var html = table.node().outerHTML;
  548. if (d.footer !== undefined)
  549. html += "<div class='footer'>" + d.footer + "</div>";
  550. return html;
  551. };
  552. /*
  553. Function that returns the position (relative to the viewport/document.body)
  554. the tooltip should be placed in.
  555. Should return: {
  556. left: <leftPos>,
  557. top: <topPos>
  558. }
  559. */
  560. var position = function() {
  561. var pos = {
  562. left: d3.event !== null ? d3.event.clientX : 0,
  563. top: d3.event !== null ? d3.event.clientY : 0
  564. };
  565. if(getComputedStyle(document.body).transform != 'none') {
  566. // Take the offset into account, as now the tooltip is relative
  567. // to document.body.
  568. var client = document.body.getBoundingClientRect();
  569. pos.left -= client.left;
  570. pos.top -= client.top;
  571. }
  572. return pos;
  573. };
  574. var dataSeriesExists = function(d) {
  575. if (d && d.series) {
  576. if (nv.utils.isArray(d.series)) {
  577. return true;
  578. }
  579. // if object, it's okay just convert to array of the object
  580. if (nv.utils.isObject(d.series)) {
  581. d.series = [d.series];
  582. return true;
  583. }
  584. }
  585. return false;
  586. };
  587. // Calculates the gravity offset of the tooltip. Parameter is position of tooltip
  588. // relative to the viewport.
  589. var calcGravityOffset = function(pos) {
  590. var height = tooltip.node().offsetHeight,
  591. width = tooltip.node().offsetWidth,
  592. clientWidth = document.documentElement.clientWidth, // Don't want scrollbars.
  593. clientHeight = document.documentElement.clientHeight, // Don't want scrollbars.
  594. left, top, tmp;
  595. // calculate position based on gravity
  596. switch (gravity) {
  597. case 'e':
  598. left = - width - distance;
  599. top = - (height / 2);
  600. if(pos.left + left < 0) left = distance;
  601. if((tmp = pos.top + top) < 0) top -= tmp;
  602. if((tmp = pos.top + top + height) > clientHeight) top -= tmp - clientHeight;
  603. break;
  604. case 'w':
  605. left = distance;
  606. top = - (height / 2);
  607. if (pos.left + left + width > clientWidth) left = - width - distance;
  608. if ((tmp = pos.top + top) < 0) top -= tmp;
  609. if ((tmp = pos.top + top + height) > clientHeight) top -= tmp - clientHeight;
  610. break;
  611. case 'n':
  612. left = - (width / 2) - 5; // - 5 is an approximation of the mouse's height.
  613. top = distance;
  614. if (pos.top + top + height > clientHeight) top = - height - distance;
  615. if ((tmp = pos.left + left) < 0) left -= tmp;
  616. if ((tmp = pos.left + left + width) > clientWidth) left -= tmp - clientWidth;
  617. break;
  618. case 's':
  619. left = - (width / 2);
  620. top = - height - distance;
  621. if (pos.top + top < 0) top = distance;
  622. if ((tmp = pos.left + left) < 0) left -= tmp;
  623. if ((tmp = pos.left + left + width) > clientWidth) left -= tmp - clientWidth;
  624. break;
  625. case 'center':
  626. left = - (width / 2);
  627. top = - (height / 2);
  628. break;
  629. default:
  630. left = 0;
  631. top = 0;
  632. break;
  633. }
  634. return { 'left': left, 'top': top };
  635. };
  636. /*
  637. Positions the tooltip in the correct place, as given by the position() function.
  638. */
  639. var positionTooltip = function() {
  640. nv.dom.read(function() {
  641. var pos = position(),
  642. gravityOffset = calcGravityOffset(pos),
  643. left = pos.left + gravityOffset.left,
  644. top = pos.top + gravityOffset.top;
  645. // delay hiding a bit to avoid flickering
  646. if (hidden) {
  647. tooltip
  648. .interrupt()
  649. .transition()
  650. .delay(hideDelay)
  651. .duration(0)
  652. .style('opacity', 0);
  653. } else {
  654. // using tooltip.style('transform') returns values un-usable for tween
  655. var old_translate = 'translate(' + lastPosition.left + 'px, ' + lastPosition.top + 'px)';
  656. var new_translate = 'translate(' + Math.round(left) + 'px, ' + Math.round(top) + 'px)';
  657. var translateInterpolator = d3.interpolateString(old_translate, new_translate);
  658. var is_hidden = tooltip.style('opacity') < 0.1;
  659. tooltip
  660. .interrupt() // cancel running transitions
  661. .transition()
  662. .duration(is_hidden ? 0 : duration)
  663. // using tween since some versions of d3 can't auto-tween a translate on a div
  664. .styleTween('transform', function (d) {
  665. return translateInterpolator;
  666. }, 'important')
  667. // Safari has its own `-webkit-transform` and does not support `transform`
  668. .styleTween('-webkit-transform', function (d) {
  669. return translateInterpolator;
  670. })
  671. .style('-ms-transform', new_translate)
  672. .style('opacity', 1);
  673. }
  674. lastPosition.left = left;
  675. lastPosition.top = top;
  676. });
  677. };
  678. // Creates new tooltip container, or uses existing one on DOM.
  679. function initTooltip() {
  680. if (!tooltip || !tooltip.node()) {
  681. // Create new tooltip div if it doesn't exist on DOM.
  682. var data = [1];
  683. tooltip = d3.select(document.body).selectAll('#'+id).data(data);
  684. tooltip.enter().append('div')
  685. .attr("class", "nvtooltip " + (classes ? classes : "xy-tooltip"))
  686. .attr("id", id)
  687. .style("top", 0).style("left", 0)
  688. .style('opacity', 0)
  689. .style('position', 'absolute')
  690. .selectAll("div, table, td, tr").classed(nvPointerEventsClass, true)
  691. .classed(nvPointerEventsClass, true);
  692. tooltip.exit().remove()
  693. }
  694. }
  695. // Draw the tooltip onto the DOM.
  696. function nvtooltip() {
  697. if (!enabled) return;
  698. if (!dataSeriesExists(data)) return;
  699. nv.dom.write(function () {
  700. initTooltip();
  701. // Generate data and set it into tooltip.
  702. // Bonus - If you override contentGenerator and return false, you can use something like
  703. // Angular, React or Knockout to bind the data for your tooltip directly to the DOM.
  704. var newContent = contentGenerator(data, tooltip.node());
  705. if (newContent) {
  706. tooltip.node().innerHTML = newContent;
  707. }
  708. positionTooltip();
  709. });
  710. return nvtooltip;
  711. }
  712. nvtooltip.nvPointerEventsClass = nvPointerEventsClass;
  713. nvtooltip.options = nv.utils.optionsFunc.bind(nvtooltip);
  714. nvtooltip._options = Object.create({}, {
  715. // simple read/write options
  716. duration: {get: function(){return duration;}, set: function(_){duration=_;}},
  717. gravity: {get: function(){return gravity;}, set: function(_){gravity=_;}},
  718. distance: {get: function(){return distance;}, set: function(_){distance=_;}},
  719. snapDistance: {get: function(){return snapDistance;}, set: function(_){snapDistance=_;}},
  720. classes: {get: function(){return classes;}, set: function(_){classes=_;}},
  721. enabled: {get: function(){return enabled;}, set: function(_){enabled=_;}},
  722. hideDelay: {get: function(){return hideDelay;}, set: function(_){hideDelay=_;}},
  723. contentGenerator: {get: function(){return contentGenerator;}, set: function(_){contentGenerator=_;}},
  724. valueFormatter: {get: function(){return valueFormatter;}, set: function(_){valueFormatter=_;}},
  725. headerFormatter: {get: function(){return headerFormatter;}, set: function(_){headerFormatter=_;}},
  726. keyFormatter: {get: function(){return keyFormatter;}, set: function(_){keyFormatter=_;}},
  727. headerEnabled: {get: function(){return headerEnabled;}, set: function(_){headerEnabled=_;}},
  728. position: {get: function(){return position;}, set: function(_){position=_;}},
  729. // Deprecated options
  730. chartContainer: {get: function(){return document.body;}, set: function(_){
  731. // deprecated after 1.8.3
  732. nv.deprecated('chartContainer', 'feature removed after 1.8.3');
  733. }},
  734. fixedTop: {get: function(){return null;}, set: function(_){
  735. // deprecated after 1.8.1
  736. nv.deprecated('fixedTop', 'feature removed after 1.8.1');
  737. }},
  738. offset: {get: function(){return {left: 0, top: 0};}, set: function(_){
  739. // deprecated after 1.8.1
  740. nv.deprecated('offset', 'use chart.tooltip.distance() instead');
  741. }},
  742. // options with extra logic
  743. hidden: {get: function(){return hidden;}, set: function(_){
  744. if (hidden != _) {
  745. hidden = !!_;
  746. nvtooltip();
  747. }
  748. }},
  749. data: {get: function(){return data;}, set: function(_){
  750. // if showing a single data point, adjust data format with that
  751. if (_.point) {
  752. _.value = _.point.x;
  753. _.series = _.series || {};
  754. _.series.value = _.point.y;
  755. _.series.color = _.point.color || _.series.color;
  756. }
  757. data = _;
  758. }},
  759. // read only properties
  760. node: {get: function(){return tooltip.node();}, set: function(_){}},
  761. id: {get: function(){return id;}, set: function(_){}}
  762. });
  763. nv.utils.initOptions(nvtooltip);
  764. return nvtooltip;
  765. };
  766. /*
  767. Gets the browser window size
  768. Returns object with height and width properties
  769. */
  770. nv.utils.windowSize = function() {
  771. // Sane defaults
  772. var size = {width: 640, height: 480};
  773. // Most recent browsers use
  774. if (window.innerWidth && window.innerHeight) {
  775. size.width = window.innerWidth;
  776. size.height = window.innerHeight;
  777. return (size);
  778. }
  779. // IE can use depending on mode it is in
  780. if (document.compatMode=='CSS1Compat' &&
  781. document.documentElement &&
  782. document.documentElement.offsetWidth ) {
  783. size.width = document.documentElement.offsetWidth;
  784. size.height = document.documentElement.offsetHeight;
  785. return (size);
  786. }
  787. // Earlier IE uses Doc.body
  788. if (document.body && document.body.offsetWidth) {
  789. size.width = document.body.offsetWidth;
  790. size.height = document.body.offsetHeight;
  791. return (size);
  792. }
  793. return (size);
  794. };
  795. /* handle dumb browser quirks... isinstance breaks if you use frames
  796. typeof returns 'object' for null, NaN is a number, etc.
  797. */
  798. nv.utils.isArray = Array.isArray;
  799. nv.utils.isObject = function(a) {
  800. return a !== null && typeof a === 'object';
  801. };
  802. nv.utils.isFunction = function(a) {
  803. return typeof a === 'function';
  804. };
  805. nv.utils.isDate = function(a) {
  806. return toString.call(a) === '[object Date]';
  807. };
  808. nv.utils.isNumber = function(a) {
  809. return !isNaN(a) && typeof a === 'number';
  810. };
  811. /*
  812. Binds callback function to run when window is resized
  813. */
  814. nv.utils.windowResize = function(handler) {
  815. if (window.addEventListener) {
  816. window.addEventListener('resize', handler);
  817. } else {
  818. nv.log("ERROR: Failed to bind to window.resize with: ", handler);
  819. }
  820. // return object with clear function to remove the single added callback.
  821. return {
  822. callback: handler,
  823. clear: function() {
  824. window.removeEventListener('resize', handler);
  825. }
  826. }
  827. };
  828. /*
  829. Backwards compatible way to implement more d3-like coloring of graphs.
  830. Can take in nothing, an array, or a function/scale
  831. To use a normal scale, get the range and pass that because we must be able
  832. to take two arguments and use the index to keep backward compatibility
  833. */
  834. nv.utils.getColor = function(color) {
  835. //if you pass in nothing, get default colors back
  836. if (color === undefined) {
  837. return nv.utils.defaultColor();
  838. //if passed an array, turn it into a color scale
  839. } else if(nv.utils.isArray(color)) {
  840. var color_scale = d3.scale.ordinal().range(color);
  841. return function(d, i) {
  842. var key = i === undefined ? d : i;
  843. return d.color || color_scale(key);
  844. };
  845. //if passed a function or scale, return it, or whatever it may be
  846. //external libs, such as angularjs-nvd3-directives use this
  847. } else {
  848. //can't really help it if someone passes rubbish as color
  849. return color;
  850. }
  851. };
  852. /*
  853. Default color chooser uses a color scale of 20 colors from D3
  854. https://github.com/mbostock/d3/wiki/Ordinal-Scales#categorical-colors
  855. */
  856. nv.utils.defaultColor = function() {
  857. // get range of the scale so we'll turn it into our own function.
  858. return nv.utils.getColor(d3.scale.category20().range());
  859. };
  860. /*
  861. Returns a color function that takes the result of 'getKey' for each series and
  862. looks for a corresponding color from the dictionary
  863. */
  864. nv.utils.customTheme = function(dictionary, getKey, defaultColors) {
  865. // use default series.key if getKey is undefined
  866. getKey = getKey || function(series) { return series.key };
  867. defaultColors = defaultColors || d3.scale.category20().range();
  868. // start at end of default color list and walk back to index 0
  869. var defIndex = defaultColors.length;
  870. return function(series, index) {
  871. var key = getKey(series);
  872. if (nv.utils.isFunction(dictionary[key])) {
  873. return dictionary[key]();
  874. } else if (dictionary[key] !== undefined) {
  875. return dictionary[key];
  876. } else {
  877. // no match in dictionary, use a default color
  878. if (!defIndex) {
  879. // used all the default colors, start over
  880. defIndex = defaultColors.length;
  881. }
  882. defIndex = defIndex - 1;
  883. return defaultColors[defIndex];
  884. }
  885. };
  886. };
  887. /*
  888. From the PJAX example on d3js.org, while this is not really directly needed
  889. it's a very cool method for doing pjax, I may expand upon it a little bit,
  890. open to suggestions on anything that may be useful
  891. */
  892. nv.utils.pjax = function(links, content) {
  893. var load = function(href) {
  894. d3.html(href, function(fragment) {
  895. var target = d3.select(content).node();
  896. target.parentNode.replaceChild(
  897. d3.select(fragment).select(content).node(),
  898. target);
  899. nv.utils.pjax(links, content);
  900. });
  901. };
  902. d3.selectAll(links).on("click", function() {
  903. history.pushState(this.href, this.textContent, this.href);
  904. load(this.href);
  905. d3.event.preventDefault();
  906. });
  907. d3.select(window).on("popstate", function() {
  908. if (d3.event.state) {
  909. load(d3.event.state);
  910. }
  911. });
  912. };
  913. /*
  914. For when we want to approximate the width in pixels for an SVG:text element.
  915. Most common instance is when the element is in a display:none; container.
  916. Forumla is : text.length * font-size * constant_factor
  917. */
  918. nv.utils.calcApproxTextWidth = function (svgTextElem) {
  919. if (nv.utils.isFunction(svgTextElem.style) && nv.utils.isFunction(svgTextElem.text)) {
  920. var fontSize = parseInt(svgTextElem.style("font-size").replace("px",""), 10);
  921. var textLength = svgTextElem.text().length;
  922. return nv.utils.NaNtoZero(textLength * fontSize * 0.5);
  923. }
  924. return 0;
  925. };
  926. /*
  927. Numbers that are undefined, null or NaN, convert them to zeros.
  928. */
  929. nv.utils.NaNtoZero = function(n) {
  930. if (!nv.utils.isNumber(n)
  931. || isNaN(n)
  932. || n === null
  933. || n === Infinity
  934. || n === -Infinity) {
  935. return 0;
  936. }
  937. return n;
  938. };
  939. /*
  940. Add a way to watch for d3 transition ends to d3
  941. */
  942. d3.selection.prototype.watchTransition = function(renderWatch){
  943. var args = [this].concat([].slice.call(arguments, 1));
  944. return renderWatch.transition.apply(renderWatch, args);
  945. };
  946. /*
  947. Helper object to watch when d3 has rendered something
  948. */
  949. nv.utils.renderWatch = function(dispatch, duration) {
  950. if (!(this instanceof nv.utils.renderWatch)) {
  951. return new nv.utils.renderWatch(dispatch, duration);
  952. }
  953. var _duration = duration !== undefined ? duration : 250;
  954. var renderStack = [];
  955. var self = this;
  956. this.models = function(models) {
  957. models = [].slice.call(arguments, 0);
  958. models.forEach(function(model){
  959. model.__rendered = false;
  960. (function(m){
  961. m.dispatch.on('renderEnd', function(arg){
  962. m.__rendered = true;
  963. self.renderEnd('model');
  964. });
  965. })(model);
  966. if (renderStack.indexOf(model) < 0) {
  967. renderStack.push(model);
  968. }
  969. });
  970. return this;
  971. };
  972. this.reset = function(duration) {
  973. if (duration !== undefined) {
  974. _duration = duration;
  975. }
  976. renderStack = [];
  977. };
  978. this.transition = function(selection, args, duration) {
  979. args = arguments.length > 1 ? [].slice.call(arguments, 1) : [];
  980. if (args.length > 1) {
  981. duration = args.pop();
  982. } else {
  983. duration = _duration !== undefined ? _duration : 250;
  984. }
  985. selection.__rendered = false;
  986. if (renderStack.indexOf(selection) < 0) {
  987. renderStack.push(selection);
  988. }
  989. if (duration === 0) {
  990. selection.__rendered = true;
  991. selection.delay = function() { return this; };
  992. selection.duration = function() { return this; };
  993. return selection;
  994. } else {
  995. if (selection.length === 0) {
  996. selection.__rendered = true;
  997. } else if (selection.every( function(d){ return !d.length; } )) {
  998. selection.__rendered = true;
  999. } else {
  1000. selection.__rendered = false;
  1001. }
  1002. var n = 0;
  1003. return selection
  1004. .transition()
  1005. .duration(duration)
  1006. .each(function(){ ++n; })
  1007. .each('end', function(d, i) {
  1008. if (--n === 0) {
  1009. selection.__rendered = true;
  1010. self.renderEnd.apply(this, args);
  1011. }
  1012. });
  1013. }
  1014. };
  1015. this.renderEnd = function() {
  1016. if (renderStack.every( function(d){ return d.__rendered; } )) {
  1017. renderStack.forEach( function(d){ d.__rendered = false; });
  1018. dispatch.renderEnd.apply(this, arguments);
  1019. }
  1020. }
  1021. };
  1022. /*
  1023. Takes multiple objects and combines them into the first one (dst)
  1024. example: nv.utils.deepExtend({a: 1}, {a: 2, b: 3}, {c: 4});
  1025. gives: {a: 2, b: 3, c: 4}
  1026. */
  1027. nv.utils.deepExtend = function(dst){
  1028. var sources = arguments.length > 1 ? [].slice.call(arguments, 1) : [];
  1029. sources.forEach(function(source) {
  1030. for (var key in source) {
  1031. var isArray = nv.utils.isArray(dst[key]);
  1032. var isObject = nv.utils.isObject(dst[key]);
  1033. var srcObj = nv.utils.isObject(source[key]);
  1034. if (isObject && !isArray && srcObj) {
  1035. nv.utils.deepExtend(dst[key], source[key]);
  1036. } else {
  1037. dst[key] = source[key];
  1038. }
  1039. }
  1040. });
  1041. };
  1042. /*
  1043. state utility object, used to track d3 states in the models
  1044. */
  1045. nv.utils.state = function(){
  1046. if (!(this instanceof nv.utils.state)) {
  1047. return new nv.utils.state();
  1048. }
  1049. var state = {};
  1050. var _self = this;
  1051. var _setState = function(){};
  1052. var _getState = function(){ return {}; };
  1053. var init = null;
  1054. var changed = null;
  1055. this.dispatch = d3.dispatch('change', 'set');
  1056. this.dispatch.on('set', function(state){
  1057. _setState(state, true);
  1058. });
  1059. this.getter = function(fn){
  1060. _getState = fn;
  1061. return this;
  1062. };
  1063. this.setter = function(fn, callback) {
  1064. if (!callback) {
  1065. callback = function(){};
  1066. }
  1067. _setState = function(state, update){
  1068. fn(state);
  1069. if (update) {
  1070. callback();
  1071. }
  1072. };
  1073. return this;
  1074. };
  1075. this.init = function(state){
  1076. init = init || {};
  1077. nv.utils.deepExtend(init, state);
  1078. };
  1079. var _set = function(){
  1080. var settings = _getState();
  1081. if (JSON.stringify(settings) === JSON.stringify(state)) {
  1082. return false;
  1083. }
  1084. for (var key in settings) {
  1085. if (state[key] === undefined) {
  1086. state[key] = {};
  1087. }
  1088. state[key] = settings[key];
  1089. changed = true;
  1090. }
  1091. return true;
  1092. };
  1093. this.update = function(){
  1094. if (init) {
  1095. _setState(init, false);
  1096. init = null;
  1097. }
  1098. if (_set.call(this)) {
  1099. this.dispatch.change(state);
  1100. }
  1101. };
  1102. };
  1103. /*
  1104. Snippet of code you can insert into each nv.models.* to give you the ability to
  1105. do things like:
  1106. chart.options({
  1107. showXAxis: true,
  1108. tooltips: true
  1109. });
  1110. To enable in the chart:
  1111. chart.options = nv.utils.optionsFunc.bind(chart);
  1112. */
  1113. nv.utils.optionsFunc = function(args) {
  1114. if (args) {
  1115. d3.map(args).forEach((function(key,value) {
  1116. if (nv.utils.isFunction(this[key])) {
  1117. this[key](value);
  1118. }
  1119. }).bind(this));
  1120. }
  1121. return this;
  1122. };
  1123. /*
  1124. numTicks: requested number of ticks
  1125. data: the chart data
  1126. returns the number of ticks to actually use on X axis, based on chart data
  1127. to avoid duplicate ticks with the same value
  1128. */
  1129. nv.utils.calcTicksX = function(numTicks, data) {
  1130. // find max number of values from all data streams
  1131. var numValues = 1;
  1132. var i = 0;
  1133. for (i; i < data.length; i += 1) {
  1134. var stream_len = data[i] && data[i].values ? data[i].values.length : 0;
  1135. numValues = stream_len > numValues ? stream_len : numValues;
  1136. }
  1137. nv.log("Requested number of ticks: ", numTicks);
  1138. nv.log("Calculated max values to be: ", numValues);
  1139. // make sure we don't have more ticks than values to avoid duplicates
  1140. numTicks = numTicks > numValues ? numTicks = numValues - 1 : numTicks;
  1141. // make sure we have at least one tick
  1142. numTicks = numTicks < 1 ? 1 : numTicks;
  1143. // make sure it's an integer
  1144. numTicks = Math.floor(numTicks);
  1145. nv.log("Calculating tick count as: ", numTicks);
  1146. return numTicks;
  1147. };
  1148. /*
  1149. returns number of ticks to actually use on Y axis, based on chart data
  1150. */
  1151. nv.utils.calcTicksY = function(numTicks, data) {
  1152. // currently uses the same logic but we can adjust here if needed later
  1153. return nv.utils.calcTicksX(numTicks, data);
  1154. };
  1155. /*
  1156. Add a particular option from an options object onto chart
  1157. Options exposed on a chart are a getter/setter function that returns chart
  1158. on set to mimic typical d3 option chaining, e.g. svg.option1('a').option2('b');
  1159. option objects should be generated via Object.create() to provide
  1160. the option of manipulating data via get/set functions.
  1161. */
  1162. nv.utils.initOption = function(chart, name) {
  1163. // if it's a call option, just call it directly, otherwise do get/set
  1164. if (chart._calls && chart._calls[name]) {
  1165. chart[name] = chart._calls[name];
  1166. } else {
  1167. chart[name] = function (_) {
  1168. if (!arguments.length) return chart._options[name];
  1169. chart._overrides[name] = true;
  1170. chart._options[name] = _;
  1171. return chart;
  1172. };
  1173. // calling the option as _option will ignore if set by option already
  1174. // so nvd3 can set options internally but the stop if set manually
  1175. chart['_' + name] = function(_) {
  1176. if (!arguments.length) return chart._options[name];
  1177. if (!chart._overrides[name]) {
  1178. chart._options[name] = _;
  1179. }
  1180. return chart;
  1181. }
  1182. }
  1183. };
  1184. /*
  1185. Add all options in an options object to the chart
  1186. */
  1187. nv.utils.initOptions = function(chart) {
  1188. chart._overrides = chart._overrides || {};
  1189. var ops = Object.getOwnPropertyNames(chart._options || {});
  1190. var calls = Object.getOwnPropertyNames(chart._calls || {});
  1191. ops = ops.concat(calls);
  1192. for (var i in ops) {
  1193. nv.utils.initOption(chart, ops[i]);
  1194. }
  1195. };
  1196. /*
  1197. Inherit options from a D3 object
  1198. d3.rebind makes calling the function on target actually call it on source
  1199. Also use _d3options so we can track what we inherit for documentation and chained inheritance
  1200. */
  1201. nv.utils.inheritOptionsD3 = function(target, d3_source, oplist) {
  1202. target._d3options = oplist.concat(target._d3options || []);
  1203. // Find unique d3 options (string) and update d3options
  1204. target._d3options = (target._d3options || []).filter(function(item, i, ar){ return ar.indexOf(item) === i; });
  1205. oplist.unshift(d3_source);
  1206. oplist.unshift(target);
  1207. d3.rebind.apply(this, oplist);
  1208. };
  1209. /*
  1210. Remove duplicates from an array
  1211. */
  1212. nv.utils.arrayUnique = function(a) {
  1213. return a.sort().filter(function(item, pos) {
  1214. return !pos || item != a[pos - 1];
  1215. });
  1216. };
  1217. /*
  1218. Keeps a list of custom symbols to draw from in addition to d3.svg.symbol
  1219. Necessary since d3 doesn't let you extend its list -_-
  1220. Add new symbols by doing nv.utils.symbols.set('name', function(size){...});
  1221. */
  1222. nv.utils.symbolMap = d3.map();
  1223. /*
  1224. Replaces d3.svg.symbol so that we can look both there and our own map
  1225. */
  1226. nv.utils.symbol = function() {
  1227. var type,
  1228. size = 64;
  1229. function symbol(d,i) {
  1230. var t = type.call(this,d,i);
  1231. var s = size.call(this,d,i);
  1232. if (d3.svg.symbolTypes.indexOf(t) !== -1) {
  1233. return d3.svg.symbol().type(t).size(s)();
  1234. } else {
  1235. return nv.utils.symbolMap.get(t)(s);
  1236. }
  1237. }
  1238. symbol.type = function(_) {
  1239. if (!arguments.length) return type;
  1240. type = d3.functor(_);
  1241. return symbol;
  1242. };
  1243. symbol.size = function(_) {
  1244. if (!arguments.length) return size;
  1245. size = d3.functor(_);
  1246. return symbol;
  1247. };
  1248. return symbol;
  1249. };
  1250. /*
  1251. Inherit option getter/setter functions from source to target
  1252. d3.rebind makes calling the function on target actually call it on source
  1253. Also track via _inherited and _d3options so we can track what we inherit
  1254. for documentation generation purposes and chained inheritance
  1255. */
  1256. nv.utils.inheritOptions = function(target, source) {
  1257. // inherit all the things
  1258. var ops = Object.getOwnPropertyNames(source._options || {});
  1259. var calls = Object.getOwnPropertyNames(source._calls || {});
  1260. var inherited = source._inherited || [];
  1261. var d3ops = source._d3options || [];
  1262. var args = ops.concat(calls).concat(inherited).concat(d3ops);
  1263. args.unshift(source);
  1264. args.unshift(target);
  1265. d3.rebind.apply(this, args);
  1266. // pass along the lists to keep track of them, don't allow duplicates
  1267. target._inherited = nv.utils.arrayUnique(ops.concat(calls).concat(inherited).concat(ops).concat(target._inherited || []));
  1268. target._d3options = nv.utils.arrayUnique(d3ops.concat(target._d3options || []));
  1269. };
  1270. /*
  1271. Runs common initialize code on the svg before the chart builds
  1272. */
  1273. nv.utils.initSVG = function(svg) {
  1274. svg.classed({'nvd3-svg':true});
  1275. };
  1276. /*
  1277. Sanitize and provide default for the container height.
  1278. */
  1279. nv.utils.sanitizeHeight = function(height, container) {
  1280. return (height || parseInt(container.style('height'), 10) || 400);
  1281. };
  1282. /*
  1283. Sanitize and provide default for the container width.
  1284. */
  1285. nv.utils.sanitizeWidth = function(width, container) {
  1286. return (width || parseInt(container.style('width'), 10) || 960);
  1287. };
  1288. /*
  1289. Calculate the available height for a chart.
  1290. */
  1291. nv.utils.availableHeight = function(height, container, margin) {
  1292. return Math.max(0,nv.utils.sanitizeHeight(height, container) - margin.top - margin.bottom);
  1293. };
  1294. /*
  1295. Calculate the available width for a chart.
  1296. */
  1297. nv.utils.availableWidth = function(width, container, margin) {
  1298. return Math.max(0,nv.utils.sanitizeWidth(width, container) - margin.left - margin.right);
  1299. };
  1300. /*
  1301. Clear any rendered chart components and display a chart's 'noData' message
  1302. */
  1303. nv.utils.noData = function(chart, container) {
  1304. var opt = chart.options(),
  1305. margin = opt.margin(),
  1306. noData = opt.noData(),
  1307. data = (noData == null) ? ["No Data Available."] : [noData],
  1308. height = nv.utils.availableHeight(null, container, margin),
  1309. width = nv.utils.availableWidth(null, container, margin),
  1310. x = margin.left + width/2,
  1311. y = margin.top + height/2;
  1312. //Remove any previously created chart components
  1313. container.selectAll('g').remove();
  1314. var noDataText = container.selectAll('.nv-noData').data(data);
  1315. noDataText.enter().append('text')
  1316. .attr('class', 'nvd3 nv-noData')
  1317. .attr('dy', '-.7em')
  1318. .style('text-anchor', 'middle');
  1319. noDataText
  1320. .attr('x', x)
  1321. .attr('y', y)
  1322. .text(function(t){ return t; });
  1323. };
  1324. /*
  1325. Wrap long labels.
  1326. */
  1327. nv.utils.wrapTicks = function (text, width) {
  1328. text.each(function() {
  1329. var text = d3.select(this),
  1330. words = text.text().split(/\s+/).reverse(),
  1331. word,
  1332. line = [],
  1333. lineNumber = 0,
  1334. lineHeight = 1.1,
  1335. y = text.attr("y"),
  1336. dy = parseFloat(text.attr("dy")),
  1337. tspan = text.text(null).append("tspan").attr("x", 0).attr("y", y).attr("dy", dy + "em");
  1338. while (word = words.pop()) {
  1339. line.push(word);
  1340. tspan.text(line.join(" "));
  1341. if (tspan.node().getComputedTextLength() > width) {
  1342. line.pop();
  1343. tspan.text(line.join(" "));
  1344. line = [word];
  1345. tspan = text.append("tspan").attr("x", 0).attr("y", y).attr("dy", ++lineNumber * lineHeight + dy + "em").text(word);
  1346. }
  1347. }
  1348. });
  1349. };
  1350. /*
  1351. Check equality of 2 array
  1352. */
  1353. nv.utils.arrayEquals = function (array1, array2) {
  1354. if (array1 === array2)
  1355. return true;
  1356. if (!array1 || !array2)
  1357. return false;
  1358. // compare lengths - can save a lot of time
  1359. if (array1.length != array2.length)
  1360. return false;
  1361. for (var i = 0,
  1362. l = array1.length; i < l; i++) {
  1363. // Check if we have nested arrays
  1364. if (array1[i] instanceof Array && array2[i] instanceof Array) {
  1365. // recurse into the nested arrays
  1366. if (!nv.arrayEquals(array1[i], array2[i]))
  1367. return false;
  1368. } else if (array1[i] != array2[i]) {
  1369. // Warning - two different object instances will never be equal: {x:20} != {x:20}
  1370. return false;
  1371. }
  1372. }
  1373. return true;
  1374. };
  1375. /*
  1376. Check if a point within an arc
  1377. */
  1378. nv.utils.pointIsInArc = function(pt, ptData, d3Arc) {
  1379. // Center of the arc is assumed to be 0,0
  1380. // (pt.x, pt.y) are assumed to be relative to the center
  1381. var r1 = d3Arc.innerRadius()(ptData), // Note: Using the innerRadius
  1382. r2 = d3Arc.outerRadius()(ptData),
  1383. theta1 = d3Arc.startAngle()(ptData),
  1384. theta2 = d3Arc.endAngle()(ptData);
  1385. var dist = pt.x * pt.x + pt.y * pt.y,
  1386. angle = Math.atan2(pt.x, -pt.y); // Note: different coordinate system.
  1387. angle = (angle < 0) ? (angle + Math.PI * 2) : angle;
  1388. return (r1 * r1 <= dist) && (dist <= r2 * r2) &&
  1389. (theta1 <= angle) && (angle <= theta2);
  1390. };
  1391. nv.models.axis = function() {
  1392. "use strict";
  1393. //============================================================
  1394. // Public Variables with Default Settings
  1395. //------------------------------------------------------------
  1396. var axis = d3.svg.axis();
  1397. var scale = d3.scale.linear();
  1398. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  1399. , width = 75 //only used for tickLabel currently
  1400. , height = 60 //only used for tickLabel currently
  1401. , axisLabelText = null
  1402. , showMaxMin = true //TODO: showMaxMin should be disabled on all ordinal scaled axes
  1403. , rotateLabels = 0
  1404. , rotateYLabel = true
  1405. , staggerLabels = false
  1406. , isOrdinal = false
  1407. , ticks = null
  1408. , axisLabelDistance = 0
  1409. , fontSize = undefined
  1410. , duration = 250
  1411. , dispatch = d3.dispatch('renderEnd')
  1412. , tickFormatMaxMin
  1413. ;
  1414. axis
  1415. .scale(scale)
  1416. .orient('bottom')
  1417. .tickFormat(function(d) { return d })
  1418. ;
  1419. //============================================================
  1420. // Private Variables
  1421. //------------------------------------------------------------
  1422. var scale0;
  1423. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  1424. function chart(selection) {
  1425. renderWatch.reset();
  1426. selection.each(function(data) {
  1427. var container = d3.select(this);
  1428. nv.utils.initSVG(container);
  1429. // Setup containers and skeleton of chart
  1430. var wrap = container.selectAll('g.nv-wrap.nv-axis').data([data]);
  1431. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-axis');
  1432. var gEnter = wrapEnter.append('g');
  1433. var g = wrap.select('g');
  1434. if (ticks !== null)
  1435. axis.ticks(ticks);
  1436. else if (axis.orient() == 'top' || axis.orient() == 'bottom')
  1437. axis.ticks(Math.abs(scale.range()[1] - scale.range()[0]) / 100);
  1438. //TODO: consider calculating width/height based on whether or not label is added, for reference in charts using this component
  1439. g.watchTransition(renderWatch, 'axis').call(axis);
  1440. scale0 = scale0 || axis.scale();
  1441. var fmt = axis.tickFormat();
  1442. if (fmt == null) {
  1443. fmt = scale0.tickFormat();
  1444. }
  1445. var axisLabel = g.selectAll('text.nv-axislabel')
  1446. .data([axisLabelText || null]);
  1447. axisLabel.exit().remove();
  1448. //only skip when fontSize is undefined so it can be cleared with a null or blank string
  1449. if (fontSize !== undefined) {
  1450. g.selectAll('g').select("text").style('font-size', fontSize);
  1451. }
  1452. var xLabelMargin;
  1453. var axisMaxMin;
  1454. var w;
  1455. switch (axis.orient()) {
  1456. case 'top':
  1457. axisLabel.enter().append('text').attr('class', 'nv-axislabel');
  1458. w = 0;
  1459. if (scale.range().length === 1) {
  1460. w = isOrdinal ? scale.range()[0] * 2 + scale.rangeBand() : 0;
  1461. } else if (scale.range().length === 2) {
  1462. w = isOrdinal ? scale.range()[0] + scale.range()[1] + scale.rangeBand() : scale.range()[1];
  1463. } else if ( scale.range().length > 2){
  1464. w = scale.range()[scale.range().length-1]+(scale.range()[1]-scale.range()[0]);
  1465. };
  1466. axisLabel
  1467. .attr('text-anchor', 'middle')
  1468. .attr('y', 0)
  1469. .attr('x', w/2);
  1470. if (showMaxMin) {
  1471. axisMaxMin = wrap.selectAll('g.nv-axisMaxMin')
  1472. .data(scale.domain());
  1473. axisMaxMin.enter().append('g').attr('class',function(d,i){
  1474. return ['nv-axisMaxMin','nv-axisMaxMin-x',(i == 0 ? 'nv-axisMin-x':'nv-axisMax-x')].join(' ')
  1475. }).append('text');
  1476. axisMaxMin.exit().remove();
  1477. axisMaxMin
  1478. .attr('transform', function(d,i) {
  1479. return 'translate(' + nv.utils.NaNtoZero(scale(d)) + ',0)'
  1480. })
  1481. .select('text')
  1482. .attr('dy', '-0.5em')
  1483. .attr('y', -axis.tickPadding())
  1484. .attr('text-anchor', 'middle')
  1485. .text(function(d,i) {
  1486. var formatter = tickFormatMaxMin || fmt;
  1487. var v = formatter(d);
  1488. return ('' + v).match('NaN') ? '' : v;
  1489. });
  1490. axisMaxMin.watchTransition(renderWatch, 'min-max top')
  1491. .attr('transform', function(d,i) {
  1492. return 'translate(' + nv.utils.NaNtoZero(scale.range()[i]) + ',0)'
  1493. });
  1494. }
  1495. break;
  1496. case 'bottom':
  1497. xLabelMargin = axisLabelDistance + 36;
  1498. var maxTextWidth = 30;
  1499. var textHeight = 0;
  1500. var xTicks = g.selectAll('g').select("text");
  1501. var rotateLabelsRule = '';
  1502. if (rotateLabels%360) {
  1503. //Reset transform on ticks so textHeight can be calculated correctly
  1504. xTicks.attr('transform', '');
  1505. //Calculate the longest xTick width
  1506. xTicks.each(function(d,i){
  1507. var box = this.getBoundingClientRect();
  1508. var width = box.width;
  1509. textHeight = box.height;
  1510. if(width > maxTextWidth) maxTextWidth = width;
  1511. });
  1512. rotateLabelsRule = 'rotate(' + rotateLabels + ' 0,' + (textHeight/2 + axis.tickPadding()) + ')';
  1513. //Convert to radians before calculating sin. Add 30 to margin for healthy padding.
  1514. var sin = Math.abs(Math.sin(rotateLabels*Math.PI/180));
  1515. xLabelMargin = (sin ? sin*maxTextWidth : maxTextWidth)+30;
  1516. //Rotate all xTicks
  1517. xTicks
  1518. .attr('transform', rotateLabelsRule)
  1519. .style('text-anchor', rotateLabels%360 > 0 ? 'start' : 'end');
  1520. } else {
  1521. if (staggerLabels) {
  1522. xTicks
  1523. .attr('transform', function(d,i) {
  1524. return 'translate(0,' + (i % 2 == 0 ? '0' : '12') + ')'
  1525. });
  1526. } else {
  1527. xTicks.attr('transform', "translate(0,0)");
  1528. }
  1529. }
  1530. axisLabel.enter().append('text').attr('class', 'nv-axislabel');
  1531. w = 0;
  1532. if (scale.range().length === 1) {
  1533. w = isOrdinal ? scale.range()[0] * 2 + scale.rangeBand() : 0;
  1534. } else if (scale.range().length === 2) {
  1535. w = isOrdinal ? scale.range()[0] + scale.range()[1] + scale.rangeBand() : scale.range()[1];
  1536. } else if ( scale.range().length > 2){
  1537. w = scale.range()[scale.range().length-1]+(scale.range()[1]-scale.range()[0]);
  1538. };
  1539. axisLabel
  1540. .attr('text-anchor', 'middle')
  1541. .attr('y', xLabelMargin)
  1542. .attr('x', w/2);
  1543. if (showMaxMin) {
  1544. //if (showMaxMin && !isOrdinal) {
  1545. axisMaxMin = wrap.selectAll('g.nv-axisMaxMin')
  1546. //.data(scale.domain())
  1547. .data([scale.domain()[0], scale.domain()[scale.domain().length - 1]]);
  1548. axisMaxMin.enter().append('g').attr('class',function(d,i){
  1549. return ['nv-axisMaxMin','nv-axisMaxMin-x',(i == 0 ? 'nv-axisMin-x':'nv-axisMax-x')].join(' ')
  1550. }).append('text');
  1551. axisMaxMin.exit().remove();
  1552. axisMaxMin
  1553. .attr('transform', function(d,i) {
  1554. return 'translate(' + nv.utils.NaNtoZero((scale(d) + (isOrdinal ? scale.rangeBand() / 2 : 0))) + ',0)'
  1555. })
  1556. .select('text')
  1557. .attr('dy', '.71em')
  1558. .attr('y', axis.tickPadding())
  1559. .attr('transform', rotateLabelsRule)
  1560. .style('text-anchor', rotateLabels ? (rotateLabels%360 > 0 ? 'start' : 'end') : 'middle')
  1561. .text(function(d,i) {
  1562. var formatter = tickFormatMaxMin || fmt;
  1563. var v = formatter(d);
  1564. return ('' + v).match('NaN') ? '' : v;
  1565. });
  1566. axisMaxMin.watchTransition(renderWatch, 'min-max bottom')
  1567. .attr('transform', function(d,i) {
  1568. return 'translate(' + nv.utils.NaNtoZero((scale(d) + (isOrdinal ? scale.rangeBand() / 2 : 0))) + ',0)'
  1569. });
  1570. }
  1571. break;
  1572. case 'right':
  1573. axisLabel.enter().append('text').attr('class', 'nv-axislabel');
  1574. axisLabel
  1575. .style('text-anchor', rotateYLabel ? 'middle' : 'begin')
  1576. .attr('transform', rotateYLabel ? 'rotate(90)' : '')
  1577. .attr('y', rotateYLabel ? (-Math.max(margin.right, width) + 12 - (axisLabelDistance || 0)) : -10) //TODO: consider calculating this based on largest tick width... OR at least expose this on chart
  1578. .attr('x', rotateYLabel ? (d3.max(scale.range()) / 2) : axis.tickPadding());
  1579. if (showMaxMin) {
  1580. axisMaxMin = wrap.selectAll('g.nv-axisMaxMin')
  1581. .data(scale.domain());
  1582. axisMaxMin.enter().append('g').attr('class',function(d,i){
  1583. return ['nv-axisMaxMin','nv-axisMaxMin-y',(i == 0 ? 'nv-axisMin-y':'nv-axisMax-y')].join(' ')
  1584. }).append('text')
  1585. .style('opacity', 0);
  1586. axisMaxMin.exit().remove();
  1587. axisMaxMin
  1588. .attr('transform', function(d,i) {
  1589. return 'translate(0,' + nv.utils.NaNtoZero(scale(d)) + ')'
  1590. })
  1591. .select('text')
  1592. .attr('dy', '.32em')
  1593. .attr('y', 0)
  1594. .attr('x', axis.tickPadding())
  1595. .style('text-anchor', 'start')
  1596. .text(function(d, i) {
  1597. var formatter = tickFormatMaxMin || fmt;
  1598. var v = formatter(d);
  1599. return ('' + v).match('NaN') ? '' : v;
  1600. });
  1601. axisMaxMin.watchTransition(renderWatch, 'min-max right')
  1602. .attr('transform', function(d,i) {
  1603. return 'translate(0,' + nv.utils.NaNtoZero(scale.range()[i]) + ')'
  1604. })
  1605. .select('text')
  1606. .style('opacity', 1);
  1607. }
  1608. break;
  1609. case 'left':
  1610. /*
  1611. //For dynamically placing the label. Can be used with dynamically-sized chart axis margins
  1612. var yTicks = g.selectAll('g').select("text");
  1613. yTicks.each(function(d,i){
  1614. var labelPadding = this.getBoundingClientRect().width + axis.tickPadding() + 16;
  1615. if(labelPadding > width) width = labelPadding;
  1616. });
  1617. */
  1618. axisLabel.enter().append('text').attr('class', 'nv-axislabel');
  1619. axisLabel
  1620. .style('text-anchor', rotateYLabel ? 'middle' : 'end')
  1621. .attr('transform', rotateYLabel ? 'rotate(-90)' : '')
  1622. .attr('y', rotateYLabel ? (-Math.max(margin.left, width) + 25 - (axisLabelDistance || 0)) : -10)
  1623. .attr('x', rotateYLabel ? (-d3.max(scale.range()) / 2) : -axis.tickPadding());
  1624. if (showMaxMin) {
  1625. axisMaxMin = wrap.selectAll('g.nv-axisMaxMin')
  1626. .data(scale.domain());
  1627. axisMaxMin.enter().append('g').attr('class',function(d,i){
  1628. return ['nv-axisMaxMin','nv-axisMaxMin-y',(i == 0 ? 'nv-axisMin-y':'nv-axisMax-y')].join(' ')
  1629. }).append('text')
  1630. .style('opacity', 0);
  1631. axisMaxMin.exit().remove();
  1632. axisMaxMin
  1633. .attr('transform', function(d,i) {
  1634. return 'translate(0,' + nv.utils.NaNtoZero(scale0(d)) + ')'
  1635. })
  1636. .select('text')
  1637. .attr('dy', '.32em')
  1638. .attr('y', 0)
  1639. .attr('x', -axis.tickPadding())
  1640. .attr('text-anchor', 'end')
  1641. .text(function(d,i) {
  1642. var formatter = tickFormatMaxMin || fmt;
  1643. var v = formatter(d);
  1644. return ('' + v).match('NaN') ? '' : v;
  1645. });
  1646. axisMaxMin.watchTransition(renderWatch, 'min-max right')
  1647. .attr('transform', function(d,i) {
  1648. return 'translate(0,' + nv.utils.NaNtoZero(scale.range()[i]) + ')'
  1649. })
  1650. .select('text')
  1651. .style('opacity', 1);
  1652. }
  1653. break;
  1654. }
  1655. axisLabel.text(function(d) { return d });
  1656. if (showMaxMin && (axis.orient() === 'left' || axis.orient() === 'right')) {
  1657. //check if max and min overlap other values, if so, hide the values that overlap
  1658. g.selectAll('g') // the g's wrapping each tick
  1659. .each(function(d,i) {
  1660. d3.select(this).select('text').attr('opacity', 1);
  1661. if (scale(d) < scale.range()[1] + 10 || scale(d) > scale.range()[0] - 10) { // 10 is assuming text height is 16... if d is 0, leave it!
  1662. if (d > 1e-10 || d < -1e-10) // accounts for minor floating point errors... though could be problematic if the scale is EXTREMELY SMALL
  1663. d3.select(this).attr('opacity', 0);
  1664. d3.select(this).select('text').attr('opacity', 0); // Don't remove the ZERO line!!
  1665. }
  1666. });
  1667. //if Max and Min = 0 only show min, Issue #281
  1668. if (scale.domain()[0] == scale.domain()[1] && scale.domain()[0] == 0) {
  1669. wrap.selectAll('g.nv-axisMaxMin').style('opacity', function (d, i) {
  1670. return !i ? 1 : 0
  1671. });
  1672. }
  1673. }
  1674. if (showMaxMin && (axis.orient() === 'top' || axis.orient() === 'bottom')) {
  1675. var maxMinRange = [];
  1676. wrap.selectAll('g.nv-axisMaxMin')
  1677. .each(function(d,i) {
  1678. try {
  1679. if (i) // i== 1, max position
  1680. maxMinRange.push(scale(d) - this.getBoundingClientRect().width - 4); //assuming the max and min labels are as wide as the next tick (with an extra 4 pixels just in case)
  1681. else // i==0, min position
  1682. maxMinRange.push(scale(d) + this.getBoundingClientRect().width + 4)
  1683. }catch (err) {
  1684. if (i) // i== 1, max position
  1685. maxMinRange.push(scale(d) - 4); //assuming the max and min labels are as wide as the next tick (with an extra 4 pixels just in case)
  1686. else // i==0, min position
  1687. maxMinRange.push(scale(d) + 4);
  1688. }
  1689. });
  1690. // the g's wrapping each tick
  1691. g.selectAll('g').each(function(d, i) {
  1692. if (scale(d) < maxMinRange[0] || scale(d) > maxMinRange[1]) {
  1693. if (d > 1e-10 || d < -1e-10) // accounts for minor floating point errors... though could be problematic if the scale is EXTREMELY SMALL
  1694. d3.select(this).remove();
  1695. else
  1696. d3.select(this).select('text').remove(); // Don't remove the ZERO line!!
  1697. }
  1698. });
  1699. }
  1700. //Highlight zero tick line
  1701. g.selectAll('.tick')
  1702. .filter(function (d) {
  1703. /*
  1704. The filter needs to return only ticks at or near zero.
  1705. Numbers like 0.00001 need to count as zero as well,
  1706. and the arithmetic trick below solves that.
  1707. */
  1708. return !parseFloat(Math.round(d * 100000) / 1000000) && (d !== undefined)
  1709. })
  1710. .classed('zero', true);
  1711. //store old scales for use in transitions on update
  1712. scale0 = scale.copy();
  1713. });
  1714. renderWatch.renderEnd('axis immediate');
  1715. return chart;
  1716. }
  1717. //============================================================
  1718. // Expose Public Variables
  1719. //------------------------------------------------------------
  1720. // expose chart's sub-components
  1721. chart.axis = axis;
  1722. chart.dispatch = dispatch;
  1723. chart.options = nv.utils.optionsFunc.bind(chart);
  1724. chart._options = Object.create({}, {
  1725. // simple options, just get/set the necessary values
  1726. axisLabelDistance: {get: function(){return axisLabelDistance;}, set: function(_){axisLabelDistance=_;}},
  1727. staggerLabels: {get: function(){return staggerLabels;}, set: function(_){staggerLabels=_;}},
  1728. rotateLabels: {get: function(){return rotateLabels;}, set: function(_){rotateLabels=_;}},
  1729. rotateYLabel: {get: function(){return rotateYLabel;}, set: function(_){rotateYLabel=_;}},
  1730. showMaxMin: {get: function(){return showMaxMin;}, set: function(_){showMaxMin=_;}},
  1731. axisLabel: {get: function(){return axisLabelText;}, set: function(_){axisLabelText=_;}},
  1732. height: {get: function(){return height;}, set: function(_){height=_;}},
  1733. ticks: {get: function(){return ticks;}, set: function(_){ticks=_;}},
  1734. width: {get: function(){return width;}, set: function(_){width=_;}},
  1735. fontSize: {get: function(){return fontSize;}, set: function(_){fontSize=_;}},
  1736. tickFormatMaxMin: {get: function(){return tickFormatMaxMin;}, set: function(_){tickFormatMaxMin=_;}},
  1737. // options that require extra logic in the setter
  1738. margin: {get: function(){return margin;}, set: function(_){
  1739. margin.top = _.top !== undefined ? _.top : margin.top;
  1740. margin.right = _.right !== undefined ? _.right : margin.right;
  1741. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  1742. margin.left = _.left !== undefined ? _.left : margin.left;
  1743. }},
  1744. duration: {get: function(){return duration;}, set: function(_){
  1745. duration=_;
  1746. renderWatch.reset(duration);
  1747. }},
  1748. scale: {get: function(){return scale;}, set: function(_){
  1749. scale = _;
  1750. axis.scale(scale);
  1751. isOrdinal = typeof scale.rangeBands === 'function';
  1752. nv.utils.inheritOptionsD3(chart, scale, ['domain', 'range', 'rangeBand', 'rangeBands']);
  1753. }}
  1754. });
  1755. nv.utils.initOptions(chart);
  1756. nv.utils.inheritOptionsD3(chart, axis, ['orient', 'tickValues', 'tickSubdivide', 'tickSize', 'tickPadding', 'tickFormat']);
  1757. nv.utils.inheritOptionsD3(chart, scale, ['domain', 'range', 'rangeBand', 'rangeBands']);
  1758. return chart;
  1759. };
  1760. nv.models.boxPlot = function() {
  1761. "use strict";
  1762. //============================================================
  1763. // Public Variables with Default Settings
  1764. //------------------------------------------------------------
  1765. var margin = {top: 0, right: 0, bottom: 0, left: 0},
  1766. width = 960,
  1767. height = 500,
  1768. id = Math.floor(Math.random() * 10000), // Create semi-unique ID in case user doesn't select one
  1769. xScale = d3.scale.ordinal(),
  1770. yScale = d3.scale.linear(),
  1771. getX = function(d) { return d.label }, // Default data model selectors.
  1772. getQ1 = function(d) { return d.values.Q1 },
  1773. getQ2 = function(d) { return d.values.Q2 },
  1774. getQ3 = function(d) { return d.values.Q3 },
  1775. getWl = function(d) { return d.values.whisker_low },
  1776. getWh = function(d) { return d.values.whisker_high },
  1777. getColor = function(d) { return d.color },
  1778. getOlItems = function(d) { return d.values.outliers },
  1779. getOlValue = function(d, i, j) { return d },
  1780. getOlLabel = function(d, i, j) { return d },
  1781. getOlColor = function(d, i, j) { return undefined },
  1782. color = nv.utils.defaultColor(),
  1783. container = null,
  1784. xDomain, xRange,
  1785. yDomain, yRange,
  1786. dispatch = d3.dispatch('elementMouseover', 'elementMouseout', 'elementMousemove', 'renderEnd'),
  1787. duration = 250,
  1788. maxBoxWidth = null;
  1789. //============================================================
  1790. // Private Variables
  1791. //------------------------------------------------------------
  1792. var xScale0, yScale0;
  1793. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  1794. function chart(selection) {
  1795. renderWatch.reset();
  1796. selection.each(function(data) {
  1797. var availableWidth = width - margin.left - margin.right,
  1798. availableHeight = height - margin.top - margin.bottom;
  1799. container = d3.select(this);
  1800. nv.utils.initSVG(container);
  1801. // Setup Scales
  1802. xScale.domain(xDomain || data.map(function(d,i) { return getX(d,i); }))
  1803. .rangeBands(xRange || [0, availableWidth], 0.1);
  1804. // if we know yDomain, no need to calculate
  1805. var yData = []
  1806. if (!yDomain) {
  1807. // (y-range is based on quartiles, whiskers and outliers)
  1808. var values = [], yMin, yMax;
  1809. data.forEach(function (d, i) {
  1810. var q1 = getQ1(d), q3 = getQ3(d), wl = getWl(d), wh = getWh(d);
  1811. var olItems = getOlItems(d);
  1812. if (olItems) {
  1813. olItems.forEach(function (e, i) {
  1814. values.push(getOlValue(e, i, undefined));
  1815. });
  1816. }
  1817. if (wl) { values.push(wl) }
  1818. if (q1) { values.push(q1) }
  1819. if (q3) { values.push(q3) }
  1820. if (wh) { values.push(wh) }
  1821. });
  1822. yMin = d3.min(values);
  1823. yMax = d3.max(values);
  1824. yData = [ yMin, yMax ] ;
  1825. }
  1826. yScale.domain(yDomain || yData);
  1827. yScale.range(yRange || [availableHeight, 0]);
  1828. //store old scales if they exist
  1829. xScale0 = xScale0 || xScale;
  1830. yScale0 = yScale0 || yScale.copy().range([yScale(0),yScale(0)]);
  1831. // Setup containers and skeleton of chart
  1832. var wrap = container.selectAll('g.nv-wrap').data([data]);
  1833. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap');
  1834. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  1835. var boxplots = wrap.selectAll('.nv-boxplot').data(function(d) { return d });
  1836. var boxEnter = boxplots.enter().append('g').style('stroke-opacity', 1e-6).style('fill-opacity', 1e-6);
  1837. boxplots
  1838. .attr('class', 'nv-boxplot')
  1839. .attr('transform', function(d,i,j) { return 'translate(' + (xScale(getX(d,i)) + xScale.rangeBand() * 0.05) + ', 0)'; })
  1840. .classed('hover', function(d) { return d.hover });
  1841. boxplots
  1842. .watchTransition(renderWatch, 'nv-boxplot: boxplots')
  1843. .style('stroke-opacity', 1)
  1844. .style('fill-opacity', 0.75)
  1845. .delay(function(d,i) { return i * duration / data.length })
  1846. .attr('transform', function(d,i) {
  1847. return 'translate(' + (xScale(getX(d,i)) + xScale.rangeBand() * 0.05) + ', 0)';
  1848. });
  1849. boxplots.exit().remove();
  1850. // ----- add the SVG elements for each boxPlot -----
  1851. // conditionally append whisker lines
  1852. boxEnter.each(function(d,i) {
  1853. var box = d3.select(this);
  1854. [getWl, getWh].forEach(function (f) {
  1855. if (f(d) !== undefined && f(d) !== null) {
  1856. var key = (f === getWl) ? 'low' : 'high';
  1857. box.append('line')
  1858. .style('stroke', getColor(d) || color(d,i))
  1859. .attr('class', 'nv-boxplot-whisker nv-boxplot-' + key);
  1860. box.append('line')
  1861. .style('stroke', getColor(d) || color(d,i))
  1862. .attr('class', 'nv-boxplot-tick nv-boxplot-' + key);
  1863. }
  1864. });
  1865. });
  1866. var box_width = function() { return (maxBoxWidth === null ? xScale.rangeBand() * 0.9 : Math.min(75, xScale.rangeBand() * 0.9)); };
  1867. var box_left = function() { return xScale.rangeBand() * 0.45 - box_width()/2; };
  1868. var box_right = function() { return xScale.rangeBand() * 0.45 + box_width()/2; };
  1869. // update whisker lines and ticks
  1870. [getWl, getWh].forEach(function (f) {
  1871. var key = (f === getWl) ? 'low' : 'high';
  1872. var endpoint = (f === getWl) ? getQ1 : getQ3;
  1873. boxplots.select('line.nv-boxplot-whisker.nv-boxplot-' + key)
  1874. .watchTransition(renderWatch, 'nv-boxplot: boxplots')
  1875. .attr('x1', xScale.rangeBand() * 0.45 )
  1876. .attr('y1', function(d,i) { return yScale(f(d)); })
  1877. .attr('x2', xScale.rangeBand() * 0.45 )
  1878. .attr('y2', function(d,i) { return yScale(endpoint(d)); });
  1879. boxplots.select('line.nv-boxplot-tick.nv-boxplot-' + key)
  1880. .watchTransition(renderWatch, 'nv-boxplot: boxplots')
  1881. .attr('x1', box_left )
  1882. .attr('y1', function(d,i) { return yScale(f(d)); })
  1883. .attr('x2', box_right )
  1884. .attr('y2', function(d,i) { return yScale(f(d)); });
  1885. });
  1886. [getWl, getWh].forEach(function (f) {
  1887. var key = (f === getWl) ? 'low' : 'high';
  1888. boxEnter.selectAll('.nv-boxplot-' + key)
  1889. .on('mouseover', function(d,i,j) {
  1890. d3.select(this).classed('hover', true);
  1891. dispatch.elementMouseover({
  1892. series: { key: f(d), color: getColor(d) || color(d,j) },
  1893. e: d3.event
  1894. });
  1895. })
  1896. .on('mouseout', function(d,i,j) {
  1897. d3.select(this).classed('hover', false);
  1898. dispatch.elementMouseout({
  1899. series: { key: f(d), color: getColor(d) || color(d,j) },
  1900. e: d3.event
  1901. });
  1902. })
  1903. .on('mousemove', function(d,i) {
  1904. dispatch.elementMousemove({e: d3.event});
  1905. });
  1906. });
  1907. // boxes
  1908. boxEnter.append('rect')
  1909. .attr('class', 'nv-boxplot-box')
  1910. // tooltip events
  1911. .on('mouseover', function(d,i) {
  1912. d3.select(this).classed('hover', true);
  1913. dispatch.elementMouseover({
  1914. key: getX(d),
  1915. value: getX(d),
  1916. series: [
  1917. { key: 'Q3', value: getQ3(d), color: getColor(d) || color(d,i) },
  1918. { key: 'Q2', value: getQ2(d), color: getColor(d) || color(d,i) },
  1919. { key: 'Q1', value: getQ1(d), color: getColor(d) || color(d,i) }
  1920. ],
  1921. data: d,
  1922. index: i,
  1923. e: d3.event
  1924. });
  1925. })
  1926. .on('mouseout', function(d,i) {
  1927. d3.select(this).classed('hover', false);
  1928. dispatch.elementMouseout({
  1929. key: getX(d),
  1930. value: getX(d),
  1931. series: [
  1932. { key: 'Q3', value: getQ3(d), color: getColor(d) || color(d,i) },
  1933. { key: 'Q2', value: getQ2(d), color: getColor(d) || color(d,i) },
  1934. { key: 'Q1', value: getQ1(d), color: getColor(d) || color(d,i) }
  1935. ],
  1936. data: d,
  1937. index: i,
  1938. e: d3.event
  1939. });
  1940. })
  1941. .on('mousemove', function(d,i) {
  1942. dispatch.elementMousemove({e: d3.event});
  1943. });
  1944. // box transitions
  1945. boxplots.select('rect.nv-boxplot-box')
  1946. .watchTransition(renderWatch, 'nv-boxplot: boxes')
  1947. .attr('y', function(d,i) { return yScale(getQ3(d)); })
  1948. .attr('width', box_width)
  1949. .attr('x', box_left )
  1950. .attr('height', function(d,i) { return Math.abs(yScale(getQ3(d)) - yScale(getQ1(d))) || 1 })
  1951. .style('fill', function(d,i) { return getColor(d) || color(d,i) })
  1952. .style('stroke', function(d,i) { return getColor(d) || color(d,i) });
  1953. // median line
  1954. boxEnter.append('line').attr('class', 'nv-boxplot-median');
  1955. boxplots.select('line.nv-boxplot-median')
  1956. .watchTransition(renderWatch, 'nv-boxplot: boxplots line')
  1957. .attr('x1', box_left)
  1958. .attr('y1', function(d,i) { return yScale(getQ2(d)); })
  1959. .attr('x2', box_right)
  1960. .attr('y2', function(d,i) { return yScale(getQ2(d)); });
  1961. // outliers
  1962. var outliers = boxplots.selectAll('.nv-boxplot-outlier').data(function(d) {
  1963. return getOlItems(d) || [];
  1964. });
  1965. outliers.enter().append('circle')
  1966. .style('fill', function(d,i,j) { return getOlColor(d,i,j) || color(d,j) })
  1967. .style('stroke', function(d,i,j) { return getOlColor(d,i,j) || color(d,j) })
  1968. .style('z-index', 9000)
  1969. .on('mouseover', function(d,i,j) {
  1970. d3.select(this).classed('hover', true);
  1971. dispatch.elementMouseover({
  1972. series: { key: getOlLabel(d,i,j), color: getOlColor(d,i,j) || color(d,j) },
  1973. e: d3.event
  1974. });
  1975. })
  1976. .on('mouseout', function(d,i,j) {
  1977. d3.select(this).classed('hover', false);
  1978. dispatch.elementMouseout({
  1979. series: { key: getOlLabel(d,i,j), color: getOlColor(d,i,j) || color(d,j) },
  1980. e: d3.event
  1981. });
  1982. })
  1983. .on('mousemove', function(d,i) {
  1984. dispatch.elementMousemove({e: d3.event});
  1985. });
  1986. outliers.attr('class', 'nv-boxplot-outlier');
  1987. outliers
  1988. .watchTransition(renderWatch, 'nv-boxplot: nv-boxplot-outlier')
  1989. .attr('cx', xScale.rangeBand() * 0.45)
  1990. .attr('cy', function(d,i,j) { return yScale(getOlValue(d,i,j)); })
  1991. .attr('r', '3');
  1992. outliers.exit().remove();
  1993. //store old scales for use in transitions on update
  1994. xScale0 = xScale.copy();
  1995. yScale0 = yScale.copy();
  1996. });
  1997. renderWatch.renderEnd('nv-boxplot immediate');
  1998. return chart;
  1999. }
  2000. //============================================================
  2001. // Expose Public Variables
  2002. //------------------------------------------------------------
  2003. chart.dispatch = dispatch;
  2004. chart.options = nv.utils.optionsFunc.bind(chart);
  2005. chart._options = Object.create({}, {
  2006. // simple options, just get/set the necessary values
  2007. width: {get: function(){return width;}, set: function(_){width=_;}},
  2008. height: {get: function(){return height;}, set: function(_){height=_;}},
  2009. maxBoxWidth: {get: function(){return maxBoxWidth;}, set: function(_){maxBoxWidth=_;}},
  2010. x: {get: function(){return getX;}, set: function(_){getX=_;}},
  2011. q1: {get: function(){return getQ1;}, set: function(_){getQ1=_;}},
  2012. q2: {get: function(){return getQ2;}, set: function(_){getQ2=_;}},
  2013. q3: {get: function(){return getQ3;}, set: function(_){getQ3=_;}},
  2014. wl: {get: function(){return getWl;}, set: function(_){getWl=_;}},
  2015. wh: {get: function(){return getWh;}, set: function(_){getWh=_;}},
  2016. itemColor: {get: function(){return getColor;}, set: function(_){getColor=_;}},
  2017. outliers: {get: function(){return getOlItems;}, set: function(_){getOlItems=_;}},
  2018. outlierValue: {get: function(){return getOlValue;}, set: function(_){getOlValue=_;}},
  2019. outlierLabel: {get: function(){return getOlLabel;}, set: function(_){getOlLabel=_;}},
  2020. outlierColor: {get: function(){return getOlColor;}, set: function(_){getOlColor=_;}},
  2021. xScale: {get: function(){return xScale;}, set: function(_){xScale=_;}},
  2022. yScale: {get: function(){return yScale;}, set: function(_){yScale=_;}},
  2023. xDomain: {get: function(){return xDomain;}, set: function(_){xDomain=_;}},
  2024. yDomain: {get: function(){return yDomain;}, set: function(_){yDomain=_;}},
  2025. xRange: {get: function(){return xRange;}, set: function(_){xRange=_;}},
  2026. yRange: {get: function(){return yRange;}, set: function(_){yRange=_;}},
  2027. id: {get: function(){return id;}, set: function(_){id=_;}},
  2028. // rectClass: {get: function(){return rectClass;}, set: function(_){rectClass=_;}},
  2029. y: {
  2030. get: function() {
  2031. console.warn('BoxPlot \'y\' chart option is deprecated. Please use model overrides instead.');
  2032. return {};
  2033. },
  2034. set: function(_) {
  2035. console.warn('BoxPlot \'y\' chart option is deprecated. Please use model overrides instead.');
  2036. }
  2037. },
  2038. // options that require extra logic in the setter
  2039. margin: {get: function(){return margin;}, set: function(_){
  2040. margin.top = _.top !== undefined ? _.top : margin.top;
  2041. margin.right = _.right !== undefined ? _.right : margin.right;
  2042. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  2043. margin.left = _.left !== undefined ? _.left : margin.left;
  2044. }},
  2045. color: {get: function(){return color;}, set: function(_){
  2046. color = nv.utils.getColor(_);
  2047. }},
  2048. duration: {get: function(){return duration;}, set: function(_){
  2049. duration = _;
  2050. renderWatch.reset(duration);
  2051. }}
  2052. });
  2053. nv.utils.initOptions(chart);
  2054. return chart;
  2055. };
  2056. nv.models.boxPlotChart = function() {
  2057. "use strict";
  2058. //============================================================
  2059. // Public Variables with Default Settings
  2060. //------------------------------------------------------------
  2061. var boxplot = nv.models.boxPlot(),
  2062. xAxis = nv.models.axis(),
  2063. yAxis = nv.models.axis();
  2064. var margin = {top: 15, right: 10, bottom: 50, left: 60},
  2065. width = null,
  2066. height = null,
  2067. color = nv.utils.getColor(),
  2068. showXAxis = true,
  2069. showYAxis = true,
  2070. rightAlignYAxis = false,
  2071. staggerLabels = false,
  2072. tooltip = nv.models.tooltip(),
  2073. x, y,
  2074. noData = 'No Data Available.',
  2075. dispatch = d3.dispatch('beforeUpdate', 'renderEnd'),
  2076. duration = 250;
  2077. xAxis
  2078. .orient('bottom')
  2079. .showMaxMin(false)
  2080. .tickFormat(function(d) { return d })
  2081. ;
  2082. yAxis
  2083. .orient((rightAlignYAxis) ? 'right' : 'left')
  2084. .tickFormat(d3.format(',.1f'))
  2085. ;
  2086. tooltip.duration(0);
  2087. //============================================================
  2088. // Private Variables
  2089. //------------------------------------------------------------
  2090. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  2091. function chart(selection) {
  2092. renderWatch.reset();
  2093. renderWatch.models(boxplot);
  2094. if (showXAxis) renderWatch.models(xAxis);
  2095. if (showYAxis) renderWatch.models(yAxis);
  2096. selection.each(function(data) {
  2097. var container = d3.select(this), that = this;
  2098. nv.utils.initSVG(container);
  2099. var availableWidth = (width || parseInt(container.style('width')) || 960) - margin.left - margin.right;
  2100. var availableHeight = (height || parseInt(container.style('height')) || 400) - margin.top - margin.bottom;
  2101. chart.update = function() {
  2102. dispatch.beforeUpdate();
  2103. container.transition().duration(duration).call(chart);
  2104. };
  2105. chart.container = this;
  2106. // TODO still need to find a way to validate quartile data presence using boxPlot callbacks.
  2107. // Display No Data message if there's nothing to show. (quartiles required at minimum).
  2108. if (!data || !data.length) {
  2109. var noDataText = container.selectAll('.nv-noData').data([noData]);
  2110. noDataText.enter().append('text')
  2111. .attr('class', 'nvd3 nv-noData')
  2112. .attr('dy', '-.7em')
  2113. .style('text-anchor', 'middle');
  2114. noDataText
  2115. .attr('x', margin.left + availableWidth / 2)
  2116. .attr('y', margin.top + availableHeight / 2)
  2117. .text(function(d) { return d });
  2118. return chart;
  2119. } else {
  2120. container.selectAll('.nv-noData').remove();
  2121. }
  2122. // Setup Scales
  2123. x = boxplot.xScale();
  2124. y = boxplot.yScale().clamp(true);
  2125. // Setup containers and skeleton of chart
  2126. var wrap = container.selectAll('g.nv-wrap.nv-boxPlotWithAxes').data([data]);
  2127. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-boxPlotWithAxes').append('g');
  2128. var defsEnter = gEnter.append('defs');
  2129. var g = wrap.select('g');
  2130. gEnter.append('g').attr('class', 'nv-x nv-axis');
  2131. gEnter.append('g').attr('class', 'nv-y nv-axis')
  2132. .append('g').attr('class', 'nv-zeroLine')
  2133. .append('line');
  2134. gEnter.append('g').attr('class', 'nv-barsWrap');
  2135. g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  2136. if (rightAlignYAxis) {
  2137. g.select('.nv-y.nv-axis')
  2138. .attr('transform', 'translate(' + availableWidth + ',0)');
  2139. }
  2140. // Main Chart Component(s)
  2141. boxplot.width(availableWidth).height(availableHeight);
  2142. var barsWrap = g.select('.nv-barsWrap')
  2143. .datum(data.filter(function(d) { return !d.disabled }))
  2144. barsWrap.transition().call(boxplot);
  2145. defsEnter.append('clipPath')
  2146. .attr('id', 'nv-x-label-clip-' + boxplot.id())
  2147. .append('rect');
  2148. g.select('#nv-x-label-clip-' + boxplot.id() + ' rect')
  2149. .attr('width', x.rangeBand() * (staggerLabels ? 2 : 1))
  2150. .attr('height', 16)
  2151. .attr('x', -x.rangeBand() / (staggerLabels ? 1 : 2 ));
  2152. // Setup Axes
  2153. if (showXAxis) {
  2154. xAxis
  2155. .scale(x)
  2156. .ticks( nv.utils.calcTicksX(availableWidth/100, data) )
  2157. .tickSize(-availableHeight, 0);
  2158. g.select('.nv-x.nv-axis').attr('transform', 'translate(0,' + y.range()[0] + ')');
  2159. g.select('.nv-x.nv-axis').call(xAxis);
  2160. var xTicks = g.select('.nv-x.nv-axis').selectAll('g');
  2161. if (staggerLabels) {
  2162. xTicks
  2163. .selectAll('text')
  2164. .attr('transform', function(d,i,j) { return 'translate(0,' + (j % 2 === 0 ? '5' : '17') + ')' })
  2165. }
  2166. }
  2167. if (showYAxis) {
  2168. yAxis
  2169. .scale(y)
  2170. .ticks( Math.floor(availableHeight/36) ) // can't use nv.utils.calcTicksY with Object data
  2171. .tickSize( -availableWidth, 0);
  2172. g.select('.nv-y.nv-axis').call(yAxis);
  2173. }
  2174. // Zero line
  2175. g.select('.nv-zeroLine line')
  2176. .attr('x1',0)
  2177. .attr('x2',availableWidth)
  2178. .attr('y1', y(0))
  2179. .attr('y2', y(0))
  2180. ;
  2181. //============================================================
  2182. // Event Handling/Dispatching (in chart's scope)
  2183. //------------------------------------------------------------
  2184. });
  2185. renderWatch.renderEnd('nv-boxplot chart immediate');
  2186. return chart;
  2187. }
  2188. //============================================================
  2189. // Event Handling/Dispatching (out of chart's scope)
  2190. //------------------------------------------------------------
  2191. boxplot.dispatch.on('elementMouseover.tooltip', function(evt) {
  2192. tooltip.data(evt).hidden(false);
  2193. });
  2194. boxplot.dispatch.on('elementMouseout.tooltip', function(evt) {
  2195. tooltip.data(evt).hidden(true);
  2196. });
  2197. boxplot.dispatch.on('elementMousemove.tooltip', function(evt) {
  2198. tooltip();
  2199. });
  2200. //============================================================
  2201. // Expose Public Variables
  2202. //------------------------------------------------------------
  2203. chart.dispatch = dispatch;
  2204. chart.boxplot = boxplot;
  2205. chart.xAxis = xAxis;
  2206. chart.yAxis = yAxis;
  2207. chart.tooltip = tooltip;
  2208. chart.options = nv.utils.optionsFunc.bind(chart);
  2209. chart._options = Object.create({}, {
  2210. // simple options, just get/set the necessary values
  2211. width: {get: function(){return width;}, set: function(_){width=_;}},
  2212. height: {get: function(){return height;}, set: function(_){height=_;}},
  2213. staggerLabels: {get: function(){return staggerLabels;}, set: function(_){staggerLabels=_;}},
  2214. showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}},
  2215. showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}},
  2216. tooltipContent: {get: function(){return tooltip;}, set: function(_){tooltip=_;}},
  2217. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  2218. // options that require extra logic in the setter
  2219. margin: {get: function(){return margin;}, set: function(_){
  2220. margin.top = _.top !== undefined ? _.top : margin.top;
  2221. margin.right = _.right !== undefined ? _.right : margin.right;
  2222. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  2223. margin.left = _.left !== undefined ? _.left : margin.left;
  2224. }},
  2225. duration: {get: function(){return duration;}, set: function(_){
  2226. duration = _;
  2227. renderWatch.reset(duration);
  2228. boxplot.duration(duration);
  2229. xAxis.duration(duration);
  2230. yAxis.duration(duration);
  2231. }},
  2232. color: {get: function(){return color;}, set: function(_){
  2233. color = nv.utils.getColor(_);
  2234. boxplot.color(color);
  2235. }},
  2236. rightAlignYAxis: {get: function(){return rightAlignYAxis;}, set: function(_){
  2237. rightAlignYAxis = _;
  2238. yAxis.orient( (_) ? 'right' : 'left');
  2239. }}
  2240. });
  2241. nv.utils.inheritOptions(chart, boxplot);
  2242. nv.utils.initOptions(chart);
  2243. return chart;
  2244. }
  2245. // Chart design based on the recommendations of Stephen Few. Implementation
  2246. // based on the work of Clint Ivy, Jamie Love, and Jason Davies.
  2247. // http://projects.instantcognition.com/protovis/bulletchart/
  2248. nv.models.bullet = function() {
  2249. "use strict";
  2250. //============================================================
  2251. // Public Variables with Default Settings
  2252. //------------------------------------------------------------
  2253. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  2254. , orient = 'left' // TODO top & bottom
  2255. , reverse = false
  2256. , ranges = function(d) { return d.ranges }
  2257. , markers = function(d) { return d.markers ? d.markers : [] }
  2258. , markerLines = function(d) { return d.markerLines ? d.markerLines : [0] }
  2259. , measures = function(d) { return d.measures }
  2260. , rangeLabels = function(d) { return d.rangeLabels ? d.rangeLabels : [] }
  2261. , markerLabels = function(d) { return d.markerLabels ? d.markerLabels : [] }
  2262. , markerLineLabels = function(d) { return d.markerLineLabels ? d.markerLineLabels : [] }
  2263. , measureLabels = function(d) { return d.measureLabels ? d.measureLabels : [] }
  2264. , forceX = [0] // List of numbers to Force into the X scale (ie. 0, or a max / min, etc.)
  2265. , width = 380
  2266. , height = 30
  2267. , container = null
  2268. , tickFormat = null
  2269. , color = nv.utils.getColor(['#1f77b4'])
  2270. , dispatch = d3.dispatch('elementMouseover', 'elementMouseout', 'elementMousemove')
  2271. , defaultRangeLabels = ["Maximum", "Mean", "Minimum"]
  2272. , legacyRangeClassNames = ["Max", "Avg", "Min"]
  2273. , duration = 1000
  2274. ;
  2275. function sortLabels(labels, values){
  2276. var lz = labels.slice();
  2277. labels.sort(function(a, b){
  2278. var iA = lz.indexOf(a);
  2279. var iB = lz.indexOf(b);
  2280. return d3.descending(values[iA], values[iB]);
  2281. });
  2282. };
  2283. function chart(selection) {
  2284. selection.each(function(d, i) {
  2285. var availableWidth = width - margin.left - margin.right,
  2286. availableHeight = height - margin.top - margin.bottom;
  2287. container = d3.select(this);
  2288. nv.utils.initSVG(container);
  2289. var rangez = ranges.call(this, d, i).slice(),
  2290. markerz = markers.call(this, d, i).slice(),
  2291. markerLinez = markerLines.call(this, d, i).slice(),
  2292. measurez = measures.call(this, d, i).slice(),
  2293. rangeLabelz = rangeLabels.call(this, d, i).slice(),
  2294. markerLabelz = markerLabels.call(this, d, i).slice(),
  2295. markerLineLabelz = markerLineLabels.call(this, d, i).slice(),
  2296. measureLabelz = measureLabels.call(this, d, i).slice();
  2297. // Sort labels according to their sorted values
  2298. sortLabels(rangeLabelz, rangez);
  2299. sortLabels(markerLabelz, markerz);
  2300. sortLabels(markerLineLabelz, markerLinez);
  2301. sortLabels(measureLabelz, measurez);
  2302. // sort values descending
  2303. rangez.sort(d3.descending);
  2304. markerz.sort(d3.descending);
  2305. markerLinez.sort(d3.descending);
  2306. measurez.sort(d3.descending);
  2307. // Setup Scales
  2308. // Compute the new x-scale.
  2309. var x1 = d3.scale.linear()
  2310. .domain( d3.extent(d3.merge([forceX, rangez])) )
  2311. .range(reverse ? [availableWidth, 0] : [0, availableWidth]);
  2312. // Retrieve the old x-scale, if this is an update.
  2313. var x0 = this.__chart__ || d3.scale.linear()
  2314. .domain([0, Infinity])
  2315. .range(x1.range());
  2316. // Stash the new scale.
  2317. this.__chart__ = x1;
  2318. var rangeMin = d3.min(rangez), //rangez[2]
  2319. rangeMax = d3.max(rangez), //rangez[0]
  2320. rangeAvg = rangez[1];
  2321. // Setup containers and skeleton of chart
  2322. var wrap = container.selectAll('g.nv-wrap.nv-bullet').data([d]);
  2323. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-bullet');
  2324. var gEnter = wrapEnter.append('g');
  2325. var g = wrap.select('g');
  2326. for(var i=0,il=rangez.length; i<il; i++){
  2327. var rangeClassNames = 'nv-range nv-range'+i;
  2328. if(i <= 2){
  2329. rangeClassNames = rangeClassNames + ' nv-range'+legacyRangeClassNames[i];
  2330. }
  2331. gEnter.append('rect').attr('class', rangeClassNames);
  2332. }
  2333. gEnter.append('rect').attr('class', 'nv-measure');
  2334. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  2335. var w0 = function(d) { return Math.abs(x0(d) - x0(0)) }, // TODO: could optimize by precalculating x0(0) and x1(0)
  2336. w1 = function(d) { return Math.abs(x1(d) - x1(0)) };
  2337. var xp0 = function(d) { return d < 0 ? x0(d) : x0(0) },
  2338. xp1 = function(d) { return d < 0 ? x1(d) : x1(0) };
  2339. for(var i=0,il=rangez.length; i<il; i++){
  2340. var range = rangez[i];
  2341. g.select('rect.nv-range'+i)
  2342. .datum(range)
  2343. .attr('height', availableHeight)
  2344. .transition()
  2345. .duration(duration)
  2346. .attr('width', w1(range))
  2347. .attr('x', xp1(range))
  2348. }
  2349. g.select('rect.nv-measure')
  2350. .style('fill', color)
  2351. .attr('height', availableHeight / 3)
  2352. .attr('y', availableHeight / 3)
  2353. .on('mouseover', function() {
  2354. dispatch.elementMouseover({
  2355. value: measurez[0],
  2356. label: measureLabelz[0] || 'Current',
  2357. color: d3.select(this).style("fill")
  2358. })
  2359. })
  2360. .on('mousemove', function() {
  2361. dispatch.elementMousemove({
  2362. value: measurez[0],
  2363. label: measureLabelz[0] || 'Current',
  2364. color: d3.select(this).style("fill")
  2365. })
  2366. })
  2367. .on('mouseout', function() {
  2368. dispatch.elementMouseout({
  2369. value: measurez[0],
  2370. label: measureLabelz[0] || 'Current',
  2371. color: d3.select(this).style("fill")
  2372. })
  2373. })
  2374. .transition()
  2375. .duration(duration)
  2376. .attr('width', measurez < 0 ?
  2377. x1(0) - x1(measurez[0])
  2378. : x1(measurez[0]) - x1(0))
  2379. .attr('x', xp1(measurez));
  2380. var h3 = availableHeight / 6;
  2381. var markerData = markerz.map( function(marker, index) {
  2382. return {value: marker, label: markerLabelz[index]}
  2383. });
  2384. gEnter
  2385. .selectAll("path.nv-markerTriangle")
  2386. .data(markerData)
  2387. .enter()
  2388. .append('path')
  2389. .attr('class', 'nv-markerTriangle')
  2390. .attr('d', 'M0,' + h3 + 'L' + h3 + ',' + (-h3) + ' ' + (-h3) + ',' + (-h3) + 'Z')
  2391. .on('mouseover', function(d) {
  2392. dispatch.elementMouseover({
  2393. value: d.value,
  2394. label: d.label || 'Previous',
  2395. color: d3.select(this).style("fill"),
  2396. pos: [x1(d.value), availableHeight/2]
  2397. })
  2398. })
  2399. .on('mousemove', function(d) {
  2400. dispatch.elementMousemove({
  2401. value: d.value,
  2402. label: d.label || 'Previous',
  2403. color: d3.select(this).style("fill")
  2404. })
  2405. })
  2406. .on('mouseout', function(d, i) {
  2407. dispatch.elementMouseout({
  2408. value: d.value,
  2409. label: d.label || 'Previous',
  2410. color: d3.select(this).style("fill")
  2411. })
  2412. });
  2413. g.selectAll("path.nv-markerTriangle")
  2414. .data(markerData)
  2415. .transition()
  2416. .duration(duration)
  2417. .attr('transform', function(d) { return 'translate(' + x1(d.value) + ',' + (availableHeight / 2) + ')' });
  2418. var markerLinesData = markerLinez.map( function(marker, index) {
  2419. return {value: marker, label: markerLineLabelz[index]}
  2420. });
  2421. gEnter
  2422. .selectAll("line.nv-markerLine")
  2423. .data(markerLinesData)
  2424. .enter()
  2425. .append('line')
  2426. .attr('cursor', '')
  2427. .attr('class', 'nv-markerLine')
  2428. .attr('x1', function(d) { return x1(d.value) })
  2429. .attr('y1', '2')
  2430. .attr('x2', function(d) { return x1(d.value) })
  2431. .attr('y2', availableHeight - 2)
  2432. .on('mouseover', function(d) {
  2433. dispatch.elementMouseover({
  2434. value: d.value,
  2435. label: d.label || 'Previous',
  2436. color: d3.select(this).style("fill"),
  2437. pos: [x1(d.value), availableHeight/2]
  2438. })
  2439. })
  2440. .on('mousemove', function(d) {
  2441. dispatch.elementMousemove({
  2442. value: d.value,
  2443. label: d.label || 'Previous',
  2444. color: d3.select(this).style("fill")
  2445. })
  2446. })
  2447. .on('mouseout', function(d, i) {
  2448. dispatch.elementMouseout({
  2449. value: d.value,
  2450. label: d.label || 'Previous',
  2451. color: d3.select(this).style("fill")
  2452. })
  2453. });
  2454. g.selectAll("line.nv-markerLine")
  2455. .data(markerLinesData)
  2456. .transition()
  2457. .duration(duration)
  2458. .attr('x1', function(d) { return x1(d.value) })
  2459. .attr('x2', function(d) { return x1(d.value) });
  2460. wrap.selectAll('.nv-range')
  2461. .on('mouseover', function(d,i) {
  2462. var label = rangeLabelz[i] || defaultRangeLabels[i];
  2463. dispatch.elementMouseover({
  2464. value: d,
  2465. label: label,
  2466. color: d3.select(this).style("fill")
  2467. })
  2468. })
  2469. .on('mousemove', function() {
  2470. dispatch.elementMousemove({
  2471. value: measurez[0],
  2472. label: measureLabelz[0] || 'Previous',
  2473. color: d3.select(this).style("fill")
  2474. })
  2475. })
  2476. .on('mouseout', function(d,i) {
  2477. var label = rangeLabelz[i] || defaultRangeLabels[i];
  2478. dispatch.elementMouseout({
  2479. value: d,
  2480. label: label,
  2481. color: d3.select(this).style("fill")
  2482. })
  2483. });
  2484. });
  2485. return chart;
  2486. }
  2487. //============================================================
  2488. // Expose Public Variables
  2489. //------------------------------------------------------------
  2490. chart.dispatch = dispatch;
  2491. chart.options = nv.utils.optionsFunc.bind(chart);
  2492. chart._options = Object.create({}, {
  2493. // simple options, just get/set the necessary values
  2494. ranges: {get: function(){return ranges;}, set: function(_){ranges=_;}}, // ranges (bad, satisfactory, good)
  2495. markers: {get: function(){return markers;}, set: function(_){markers=_;}}, // markers (previous, goal)
  2496. measures: {get: function(){return measures;}, set: function(_){measures=_;}}, // measures (actual, forecast)
  2497. forceX: {get: function(){return forceX;}, set: function(_){forceX=_;}},
  2498. width: {get: function(){return width;}, set: function(_){width=_;}},
  2499. height: {get: function(){return height;}, set: function(_){height=_;}},
  2500. tickFormat: {get: function(){return tickFormat;}, set: function(_){tickFormat=_;}},
  2501. duration: {get: function(){return duration;}, set: function(_){duration=_;}},
  2502. // options that require extra logic in the setter
  2503. margin: {get: function(){return margin;}, set: function(_){
  2504. margin.top = _.top !== undefined ? _.top : margin.top;
  2505. margin.right = _.right !== undefined ? _.right : margin.right;
  2506. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  2507. margin.left = _.left !== undefined ? _.left : margin.left;
  2508. }},
  2509. orient: {get: function(){return orient;}, set: function(_){ // left, right, top, bottom
  2510. orient = _;
  2511. reverse = orient == 'right' || orient == 'bottom';
  2512. }},
  2513. color: {get: function(){return color;}, set: function(_){
  2514. color = nv.utils.getColor(_);
  2515. }}
  2516. });
  2517. nv.utils.initOptions(chart);
  2518. return chart;
  2519. };
  2520. // Chart design based on the recommendations of Stephen Few. Implementation
  2521. // based on the work of Clint Ivy, Jamie Love, and Jason Davies.
  2522. // http://projects.instantcognition.com/protovis/bulletchart/
  2523. nv.models.bulletChart = function() {
  2524. "use strict";
  2525. //============================================================
  2526. // Public Variables with Default Settings
  2527. //------------------------------------------------------------
  2528. var bullet = nv.models.bullet();
  2529. var tooltip = nv.models.tooltip();
  2530. var orient = 'left' // TODO top & bottom
  2531. , reverse = false
  2532. , margin = {top: 5, right: 40, bottom: 20, left: 120}
  2533. , ranges = function(d) { return d.ranges }
  2534. , markers = function(d) { return d.markers ? d.markers : [] }
  2535. , measures = function(d) { return d.measures }
  2536. , width = null
  2537. , height = 55
  2538. , tickFormat = null
  2539. , ticks = null
  2540. , noData = null
  2541. , dispatch = d3.dispatch()
  2542. ;
  2543. tooltip
  2544. .duration(0)
  2545. .headerEnabled(false);
  2546. function chart(selection) {
  2547. selection.each(function(d, i) {
  2548. var container = d3.select(this);
  2549. nv.utils.initSVG(container);
  2550. var availableWidth = nv.utils.availableWidth(width, container, margin),
  2551. availableHeight = height - margin.top - margin.bottom,
  2552. that = this;
  2553. chart.update = function() { chart(selection) };
  2554. chart.container = this;
  2555. // Display No Data message if there's nothing to show.
  2556. if (!d || !ranges.call(this, d, i)) {
  2557. nv.utils.noData(chart, container)
  2558. return chart;
  2559. } else {
  2560. container.selectAll('.nv-noData').remove();
  2561. }
  2562. var rangez = ranges.call(this, d, i).slice().sort(d3.descending),
  2563. markerz = markers.call(this, d, i).slice().sort(d3.descending),
  2564. measurez = measures.call(this, d, i).slice().sort(d3.descending);
  2565. // Setup containers and skeleton of chart
  2566. var wrap = container.selectAll('g.nv-wrap.nv-bulletChart').data([d]);
  2567. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-bulletChart');
  2568. var gEnter = wrapEnter.append('g');
  2569. var g = wrap.select('g');
  2570. gEnter.append('g').attr('class', 'nv-bulletWrap');
  2571. gEnter.append('g').attr('class', 'nv-titles');
  2572. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  2573. // Compute the new x-scale.
  2574. var x1 = d3.scale.linear()
  2575. .domain([0, Math.max(rangez[0], (markerz[0] || 0), measurez[0])]) // TODO: need to allow forceX and forceY, and xDomain, yDomain
  2576. .range(reverse ? [availableWidth, 0] : [0, availableWidth]);
  2577. // Retrieve the old x-scale, if this is an update.
  2578. var x0 = this.__chart__ || d3.scale.linear()
  2579. .domain([0, Infinity])
  2580. .range(x1.range());
  2581. // Stash the new scale.
  2582. this.__chart__ = x1;
  2583. var w0 = function(d) { return Math.abs(x0(d) - x0(0)) }, // TODO: could optimize by precalculating x0(0) and x1(0)
  2584. w1 = function(d) { return Math.abs(x1(d) - x1(0)) };
  2585. var title = gEnter.select('.nv-titles').append('g')
  2586. .attr('text-anchor', 'end')
  2587. .attr('transform', 'translate(-6,' + (height - margin.top - margin.bottom) / 2 + ')');
  2588. title.append('text')
  2589. .attr('class', 'nv-title')
  2590. .text(function(d) { return d.title; });
  2591. title.append('text')
  2592. .attr('class', 'nv-subtitle')
  2593. .attr('dy', '1em')
  2594. .text(function(d) { return d.subtitle; });
  2595. bullet
  2596. .width(availableWidth)
  2597. .height(availableHeight);
  2598. var bulletWrap = g.select('.nv-bulletWrap');
  2599. d3.transition(bulletWrap).call(bullet);
  2600. // Compute the tick format.
  2601. var format = tickFormat || x1.tickFormat( availableWidth / 100 );
  2602. // Update the tick groups.
  2603. var tick = g.selectAll('g.nv-tick')
  2604. .data(x1.ticks( ticks ? ticks : (availableWidth / 50) ), function(d) {
  2605. return this.textContent || format(d);
  2606. });
  2607. // Initialize the ticks with the old scale, x0.
  2608. var tickEnter = tick.enter().append('g')
  2609. .attr('class', 'nv-tick')
  2610. .attr('transform', function(d) { return 'translate(' + x0(d) + ',0)' })
  2611. .style('opacity', 1e-6);
  2612. tickEnter.append('line')
  2613. .attr('y1', availableHeight)
  2614. .attr('y2', availableHeight * 7 / 6);
  2615. tickEnter.append('text')
  2616. .attr('text-anchor', 'middle')
  2617. .attr('dy', '1em')
  2618. .attr('y', availableHeight * 7 / 6)
  2619. .text(format);
  2620. // Transition the updating ticks to the new scale, x1.
  2621. var tickUpdate = d3.transition(tick)
  2622. .transition()
  2623. .duration(bullet.duration())
  2624. .attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
  2625. .style('opacity', 1);
  2626. tickUpdate.select('line')
  2627. .attr('y1', availableHeight)
  2628. .attr('y2', availableHeight * 7 / 6);
  2629. tickUpdate.select('text')
  2630. .attr('y', availableHeight * 7 / 6);
  2631. // Transition the exiting ticks to the new scale, x1.
  2632. d3.transition(tick.exit())
  2633. .transition()
  2634. .duration(bullet.duration())
  2635. .attr('transform', function(d) { return 'translate(' + x1(d) + ',0)' })
  2636. .style('opacity', 1e-6)
  2637. .remove();
  2638. });
  2639. d3.timer.flush();
  2640. return chart;
  2641. }
  2642. //============================================================
  2643. // Event Handling/Dispatching (out of chart's scope)
  2644. //------------------------------------------------------------
  2645. bullet.dispatch.on('elementMouseover.tooltip', function(evt) {
  2646. evt['series'] = {
  2647. key: evt.label,
  2648. value: evt.value,
  2649. color: evt.color
  2650. };
  2651. tooltip.data(evt).hidden(false);
  2652. });
  2653. bullet.dispatch.on('elementMouseout.tooltip', function(evt) {
  2654. tooltip.hidden(true);
  2655. });
  2656. bullet.dispatch.on('elementMousemove.tooltip', function(evt) {
  2657. tooltip();
  2658. });
  2659. //============================================================
  2660. // Expose Public Variables
  2661. //------------------------------------------------------------
  2662. chart.bullet = bullet;
  2663. chart.dispatch = dispatch;
  2664. chart.tooltip = tooltip;
  2665. chart.options = nv.utils.optionsFunc.bind(chart);
  2666. chart._options = Object.create({}, {
  2667. // simple options, just get/set the necessary values
  2668. ranges: {get: function(){return ranges;}, set: function(_){ranges=_;}}, // ranges (bad, satisfactory, good)
  2669. markers: {get: function(){return markers;}, set: function(_){markers=_;}}, // markers (previous, goal)
  2670. measures: {get: function(){return measures;}, set: function(_){measures=_;}}, // measures (actual, forecast)
  2671. width: {get: function(){return width;}, set: function(_){width=_;}},
  2672. height: {get: function(){return height;}, set: function(_){height=_;}},
  2673. tickFormat: {get: function(){return tickFormat;}, set: function(_){tickFormat=_;}},
  2674. ticks: {get: function(){return ticks;}, set: function(_){ticks=_;}},
  2675. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  2676. // options that require extra logic in the setter
  2677. margin: {get: function(){return margin;}, set: function(_){
  2678. margin.top = _.top !== undefined ? _.top : margin.top;
  2679. margin.right = _.right !== undefined ? _.right : margin.right;
  2680. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  2681. margin.left = _.left !== undefined ? _.left : margin.left;
  2682. }},
  2683. orient: {get: function(){return orient;}, set: function(_){ // left, right, top, bottom
  2684. orient = _;
  2685. reverse = orient == 'right' || orient == 'bottom';
  2686. }}
  2687. });
  2688. nv.utils.inheritOptions(chart, bullet);
  2689. nv.utils.initOptions(chart);
  2690. return chart;
  2691. };
  2692. nv.models.candlestickBar = function() {
  2693. "use strict";
  2694. //============================================================
  2695. // Public Variables with Default Settings
  2696. //------------------------------------------------------------
  2697. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  2698. , width = null
  2699. , height = null
  2700. , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one
  2701. , container
  2702. , x = d3.scale.linear()
  2703. , y = d3.scale.linear()
  2704. , getX = function(d) { return d.x }
  2705. , getY = function(d) { return d.y }
  2706. , getOpen = function(d) { return d.open }
  2707. , getClose = function(d) { return d.close }
  2708. , getHigh = function(d) { return d.high }
  2709. , getLow = function(d) { return d.low }
  2710. , forceX = []
  2711. , forceY = []
  2712. , padData = false // If true, adds half a data points width to front and back, for lining up a line chart with a bar chart
  2713. , clipEdge = true
  2714. , color = nv.utils.defaultColor()
  2715. , interactive = false
  2716. , xDomain
  2717. , yDomain
  2718. , xRange
  2719. , yRange
  2720. , dispatch = d3.dispatch('stateChange', 'changeState', 'renderEnd', 'chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout', 'elementMousemove')
  2721. ;
  2722. //============================================================
  2723. // Private Variables
  2724. //------------------------------------------------------------
  2725. function chart(selection) {
  2726. selection.each(function(data) {
  2727. container = d3.select(this);
  2728. var availableWidth = nv.utils.availableWidth(width, container, margin),
  2729. availableHeight = nv.utils.availableHeight(height, container, margin);
  2730. nv.utils.initSVG(container);
  2731. // Width of the candlestick bars.
  2732. var barWidth = (availableWidth / data[0].values.length) * .45;
  2733. // Setup Scales
  2734. x.domain(xDomain || d3.extent(data[0].values.map(getX).concat(forceX) ));
  2735. if (padData)
  2736. x.range(xRange || [availableWidth * .5 / data[0].values.length, availableWidth * (data[0].values.length - .5) / data[0].values.length ]);
  2737. else
  2738. x.range(xRange || [5 + barWidth / 2, availableWidth - barWidth / 2 - 5]);
  2739. y.domain(yDomain || [
  2740. d3.min(data[0].values.map(getLow).concat(forceY)),
  2741. d3.max(data[0].values.map(getHigh).concat(forceY))
  2742. ]
  2743. ).range(yRange || [availableHeight, 0]);
  2744. // If scale's domain don't have a range, slightly adjust to make one... so a chart can show a single data point
  2745. if (x.domain()[0] === x.domain()[1])
  2746. x.domain()[0] ?
  2747. x.domain([x.domain()[0] - x.domain()[0] * 0.01, x.domain()[1] + x.domain()[1] * 0.01])
  2748. : x.domain([-1,1]);
  2749. if (y.domain()[0] === y.domain()[1])
  2750. y.domain()[0] ?
  2751. y.domain([y.domain()[0] + y.domain()[0] * 0.01, y.domain()[1] - y.domain()[1] * 0.01])
  2752. : y.domain([-1,1]);
  2753. // Setup containers and skeleton of chart
  2754. var wrap = d3.select(this).selectAll('g.nv-wrap.nv-candlestickBar').data([data[0].values]);
  2755. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-candlestickBar');
  2756. var defsEnter = wrapEnter.append('defs');
  2757. var gEnter = wrapEnter.append('g');
  2758. var g = wrap.select('g');
  2759. gEnter.append('g').attr('class', 'nv-ticks');
  2760. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  2761. container
  2762. .on('click', function(d,i) {
  2763. dispatch.chartClick({
  2764. data: d,
  2765. index: i,
  2766. pos: d3.event,
  2767. id: id
  2768. });
  2769. });
  2770. defsEnter.append('clipPath')
  2771. .attr('id', 'nv-chart-clip-path-' + id)
  2772. .append('rect');
  2773. wrap.select('#nv-chart-clip-path-' + id + ' rect')
  2774. .attr('width', availableWidth)
  2775. .attr('height', availableHeight);
  2776. g .attr('clip-path', clipEdge ? 'url(#nv-chart-clip-path-' + id + ')' : '');
  2777. var ticks = wrap.select('.nv-ticks').selectAll('.nv-tick')
  2778. .data(function(d) { return d });
  2779. ticks.exit().remove();
  2780. var tickGroups = ticks.enter().append('g');
  2781. // The colors are currently controlled by CSS.
  2782. ticks
  2783. .attr('class', function(d, i, j) { return (getOpen(d, i) > getClose(d, i) ? 'nv-tick negative' : 'nv-tick positive') + ' nv-tick-' + j + '-' + i});
  2784. var lines = tickGroups.append('line')
  2785. .attr('class', 'nv-candlestick-lines')
  2786. .attr('transform', function(d, i) { return 'translate(' + x(getX(d, i)) + ',0)'; })
  2787. .attr('x1', 0)
  2788. .attr('y1', function(d, i) { return y(getHigh(d, i)); })
  2789. .attr('x2', 0)
  2790. .attr('y2', function(d, i) { return y(getLow(d, i)); });
  2791. var rects = tickGroups.append('rect')
  2792. .attr('class', 'nv-candlestick-rects nv-bars')
  2793. .attr('transform', function(d, i) {
  2794. return 'translate(' + (x(getX(d, i)) - barWidth/2) + ','
  2795. + (y(getY(d, i)) - (getOpen(d, i) > getClose(d, i) ? (y(getClose(d, i)) - y(getOpen(d, i))) : 0))
  2796. + ')';
  2797. })
  2798. .attr('x', 0)
  2799. .attr('y', 0)
  2800. .attr('width', barWidth)
  2801. .attr('height', function(d, i) {
  2802. var open = getOpen(d, i);
  2803. var close = getClose(d, i);
  2804. return open > close ? y(close) - y(open) : y(open) - y(close);
  2805. });
  2806. ticks.select('.nv-candlestick-lines').transition()
  2807. .attr('transform', function(d, i) { return 'translate(' + x(getX(d, i)) + ',0)'; })
  2808. .attr('x1', 0)
  2809. .attr('y1', function(d, i) { return y(getHigh(d, i)); })
  2810. .attr('x2', 0)
  2811. .attr('y2', function(d, i) { return y(getLow(d, i)); });
  2812. ticks.select('.nv-candlestick-rects').transition()
  2813. .attr('transform', function(d, i) {
  2814. return 'translate(' + (x(getX(d, i)) - barWidth/2) + ','
  2815. + (y(getY(d, i)) - (getOpen(d, i) > getClose(d, i) ? (y(getClose(d, i)) - y(getOpen(d, i))) : 0))
  2816. + ')';
  2817. })
  2818. .attr('x', 0)
  2819. .attr('y', 0)
  2820. .attr('width', barWidth)
  2821. .attr('height', function(d, i) {
  2822. var open = getOpen(d, i);
  2823. var close = getClose(d, i);
  2824. return open > close ? y(close) - y(open) : y(open) - y(close);
  2825. });
  2826. });
  2827. return chart;
  2828. }
  2829. //Create methods to allow outside functions to highlight a specific bar.
  2830. chart.highlightPoint = function(pointIndex, isHoverOver) {
  2831. chart.clearHighlights();
  2832. container.select(".nv-candlestickBar .nv-tick-0-" + pointIndex)
  2833. .classed("hover", isHoverOver)
  2834. ;
  2835. };
  2836. chart.clearHighlights = function() {
  2837. container.select(".nv-candlestickBar .nv-tick.hover")
  2838. .classed("hover", false)
  2839. ;
  2840. };
  2841. //============================================================
  2842. // Expose Public Variables
  2843. //------------------------------------------------------------
  2844. chart.dispatch = dispatch;
  2845. chart.options = nv.utils.optionsFunc.bind(chart);
  2846. chart._options = Object.create({}, {
  2847. // simple options, just get/set the necessary values
  2848. width: {get: function(){return width;}, set: function(_){width=_;}},
  2849. height: {get: function(){return height;}, set: function(_){height=_;}},
  2850. xScale: {get: function(){return x;}, set: function(_){x=_;}},
  2851. yScale: {get: function(){return y;}, set: function(_){y=_;}},
  2852. xDomain: {get: function(){return xDomain;}, set: function(_){xDomain=_;}},
  2853. yDomain: {get: function(){return yDomain;}, set: function(_){yDomain=_;}},
  2854. xRange: {get: function(){return xRange;}, set: function(_){xRange=_;}},
  2855. yRange: {get: function(){return yRange;}, set: function(_){yRange=_;}},
  2856. forceX: {get: function(){return forceX;}, set: function(_){forceX=_;}},
  2857. forceY: {get: function(){return forceY;}, set: function(_){forceY=_;}},
  2858. padData: {get: function(){return padData;}, set: function(_){padData=_;}},
  2859. clipEdge: {get: function(){return clipEdge;}, set: function(_){clipEdge=_;}},
  2860. id: {get: function(){return id;}, set: function(_){id=_;}},
  2861. interactive: {get: function(){return interactive;}, set: function(_){interactive=_;}},
  2862. x: {get: function(){return getX;}, set: function(_){getX=_;}},
  2863. y: {get: function(){return getY;}, set: function(_){getY=_;}},
  2864. open: {get: function(){return getOpen();}, set: function(_){getOpen=_;}},
  2865. close: {get: function(){return getClose();}, set: function(_){getClose=_;}},
  2866. high: {get: function(){return getHigh;}, set: function(_){getHigh=_;}},
  2867. low: {get: function(){return getLow;}, set: function(_){getLow=_;}},
  2868. // options that require extra logic in the setter
  2869. margin: {get: function(){return margin;}, set: function(_){
  2870. margin.top = _.top != undefined ? _.top : margin.top;
  2871. margin.right = _.right != undefined ? _.right : margin.right;
  2872. margin.bottom = _.bottom != undefined ? _.bottom : margin.bottom;
  2873. margin.left = _.left != undefined ? _.left : margin.left;
  2874. }},
  2875. color: {get: function(){return color;}, set: function(_){
  2876. color = nv.utils.getColor(_);
  2877. }}
  2878. });
  2879. nv.utils.initOptions(chart);
  2880. return chart;
  2881. };
  2882. nv.models.cumulativeLineChart = function() {
  2883. "use strict";
  2884. //============================================================
  2885. // Public Variables with Default Settings
  2886. //------------------------------------------------------------
  2887. var lines = nv.models.line()
  2888. , xAxis = nv.models.axis()
  2889. , yAxis = nv.models.axis()
  2890. , legend = nv.models.legend()
  2891. , controls = nv.models.legend()
  2892. , interactiveLayer = nv.interactiveGuideline()
  2893. , tooltip = nv.models.tooltip()
  2894. ;
  2895. var margin = {top: 30, right: 30, bottom: 50, left: 60}
  2896. , marginTop = null
  2897. , color = nv.utils.defaultColor()
  2898. , width = null
  2899. , height = null
  2900. , showLegend = true
  2901. , showXAxis = true
  2902. , showYAxis = true
  2903. , rightAlignYAxis = false
  2904. , showControls = true
  2905. , useInteractiveGuideline = false
  2906. , rescaleY = true
  2907. , x //can be accessed via chart.xScale()
  2908. , y //can be accessed via chart.yScale()
  2909. , id = lines.id()
  2910. , state = nv.utils.state()
  2911. , defaultState = null
  2912. , noData = null
  2913. , average = function(d) { return d.average }
  2914. , dispatch = d3.dispatch('stateChange', 'changeState', 'renderEnd')
  2915. , transitionDuration = 250
  2916. , duration = 250
  2917. , noErrorCheck = false //if set to TRUE, will bypass an error check in the indexify function.
  2918. ;
  2919. state.index = 0;
  2920. state.rescaleY = rescaleY;
  2921. xAxis.orient('bottom').tickPadding(7);
  2922. yAxis.orient((rightAlignYAxis) ? 'right' : 'left');
  2923. tooltip.valueFormatter(function(d, i) {
  2924. return yAxis.tickFormat()(d, i);
  2925. }).headerFormatter(function(d, i) {
  2926. return xAxis.tickFormat()(d, i);
  2927. });
  2928. controls.updateState(false);
  2929. //============================================================
  2930. // Private Variables
  2931. //------------------------------------------------------------
  2932. var dx = d3.scale.linear()
  2933. , index = {i: 0, x: 0}
  2934. , renderWatch = nv.utils.renderWatch(dispatch, duration)
  2935. , currentYDomain
  2936. ;
  2937. var stateGetter = function(data) {
  2938. return function(){
  2939. return {
  2940. active: data.map(function(d) { return !d.disabled }),
  2941. index: index.i,
  2942. rescaleY: rescaleY
  2943. };
  2944. }
  2945. };
  2946. var stateSetter = function(data) {
  2947. return function(state) {
  2948. if (state.index !== undefined)
  2949. index.i = state.index;
  2950. if (state.rescaleY !== undefined)
  2951. rescaleY = state.rescaleY;
  2952. if (state.active !== undefined)
  2953. data.forEach(function(series,i) {
  2954. series.disabled = !state.active[i];
  2955. });
  2956. }
  2957. };
  2958. function chart(selection) {
  2959. renderWatch.reset();
  2960. renderWatch.models(lines);
  2961. if (showXAxis) renderWatch.models(xAxis);
  2962. if (showYAxis) renderWatch.models(yAxis);
  2963. selection.each(function(data) {
  2964. var container = d3.select(this);
  2965. nv.utils.initSVG(container);
  2966. container.classed('nv-chart-' + id, true);
  2967. var that = this;
  2968. var availableWidth = nv.utils.availableWidth(width, container, margin),
  2969. availableHeight = nv.utils.availableHeight(height, container, margin);
  2970. chart.update = function() {
  2971. if (duration === 0)
  2972. container.call(chart);
  2973. else
  2974. container.transition().duration(duration).call(chart)
  2975. };
  2976. chart.container = this;
  2977. state
  2978. .setter(stateSetter(data), chart.update)
  2979. .getter(stateGetter(data))
  2980. .update();
  2981. // DEPRECATED set state.disableddisabled
  2982. state.disabled = data.map(function(d) { return !!d.disabled });
  2983. if (!defaultState) {
  2984. var key;
  2985. defaultState = {};
  2986. for (key in state) {
  2987. if (state[key] instanceof Array)
  2988. defaultState[key] = state[key].slice(0);
  2989. else
  2990. defaultState[key] = state[key];
  2991. }
  2992. }
  2993. var indexDrag = d3.behavior.drag()
  2994. .on('dragstart', dragStart)
  2995. .on('drag', dragMove)
  2996. .on('dragend', dragEnd);
  2997. function dragStart(d,i) {
  2998. d3.select(chart.container)
  2999. .style('cursor', 'ew-resize');
  3000. }
  3001. function dragMove(d,i) {
  3002. index.x = d3.event.x;
  3003. index.i = Math.round(dx.invert(index.x));
  3004. updateZero();
  3005. }
  3006. function dragEnd(d,i) {
  3007. d3.select(chart.container)
  3008. .style('cursor', 'auto');
  3009. // update state and send stateChange with new index
  3010. state.index = index.i;
  3011. dispatch.stateChange(state);
  3012. }
  3013. // Display No Data message if there's nothing to show.
  3014. if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
  3015. nv.utils.noData(chart, container)
  3016. return chart;
  3017. } else {
  3018. container.selectAll('.nv-noData').remove();
  3019. }
  3020. // Setup Scales
  3021. x = lines.xScale();
  3022. y = lines.yScale();
  3023. dx.domain([0, data[0].values.length - 1]) //Assumes all series have same length
  3024. .range([0, availableWidth])
  3025. .clamp(true);
  3026. var data = indexify(index.i, data);
  3027. // initialize the starting yDomain for the not-rescale case after indexify (to have calculated point.display)
  3028. if (typeof(currentYDomain) === "undefined") {
  3029. currentYDomain = getCurrentYDomain(data);
  3030. }
  3031. if (!rescaleY) {
  3032. lines.yDomain(currentYDomain);
  3033. lines.clipEdge(true);
  3034. } else {
  3035. lines.yDomain(null);
  3036. }
  3037. // Setup containers and skeleton of chart
  3038. var interactivePointerEvents = (useInteractiveGuideline) ? "none" : "all";
  3039. var wrap = container.selectAll('g.nv-wrap.nv-cumulativeLine').data([data]);
  3040. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-cumulativeLine').append('g');
  3041. var g = wrap.select('g');
  3042. gEnter.append('g').attr('class', 'nv-interactive');
  3043. gEnter.append('g').attr('class', 'nv-x nv-axis').style("pointer-events","none");
  3044. gEnter.append('g').attr('class', 'nv-y nv-axis');
  3045. gEnter.append('g').attr('class', 'nv-background');
  3046. gEnter.append('g').attr('class', 'nv-linesWrap').style("pointer-events",interactivePointerEvents);
  3047. gEnter.append('g').attr('class', 'nv-avgLinesWrap').style("pointer-events","none");
  3048. gEnter.append('g').attr('class', 'nv-legendWrap');
  3049. gEnter.append('g').attr('class', 'nv-controlsWrap');
  3050. // Legend
  3051. if (!showLegend) {
  3052. g.select('.nv-legendWrap').selectAll('*').remove();
  3053. } else {
  3054. legend.width(availableWidth);
  3055. g.select('.nv-legendWrap')
  3056. .datum(data)
  3057. .call(legend);
  3058. if (!marginTop && legend.height() !== margin.top) {
  3059. margin.top = legend.height();
  3060. availableHeight = nv.utils.availableHeight(height, container, margin);
  3061. }
  3062. g.select('.nv-legendWrap')
  3063. .attr('transform', 'translate(0,' + (-margin.top) +')')
  3064. }
  3065. // Controls
  3066. if (!showControls) {
  3067. g.select('.nv-controlsWrap').selectAll('*').remove();
  3068. } else {
  3069. var controlsData = [
  3070. { key: 'Re-scale y-axis', disabled: !rescaleY }
  3071. ];
  3072. controls
  3073. .width(140)
  3074. .color(['#444', '#444', '#444'])
  3075. .rightAlign(false)
  3076. .margin({top: 5, right: 0, bottom: 5, left: 20})
  3077. ;
  3078. g.select('.nv-controlsWrap')
  3079. .datum(controlsData)
  3080. .attr('transform', 'translate(0,' + (-margin.top) +')')
  3081. .call(controls);
  3082. }
  3083. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  3084. if (rightAlignYAxis) {
  3085. g.select(".nv-y.nv-axis")
  3086. .attr("transform", "translate(" + availableWidth + ",0)");
  3087. }
  3088. // Show error if index point value is 0 (division by zero avoided)
  3089. var tempDisabled = data.filter(function(d) { return d.tempDisabled });
  3090. wrap.select('.tempDisabled').remove(); //clean-up and prevent duplicates
  3091. if (tempDisabled.length) {
  3092. wrap.append('text').attr('class', 'tempDisabled')
  3093. .attr('x', availableWidth / 2)
  3094. .attr('y', '-.71em')
  3095. .style('text-anchor', 'end')
  3096. .text(tempDisabled.map(function(d) { return d.key }).join(', ') + ' values cannot be calculated for this time period.');
  3097. }
  3098. //Set up interactive layer
  3099. if (useInteractiveGuideline) {
  3100. interactiveLayer
  3101. .width(availableWidth)
  3102. .height(availableHeight)
  3103. .margin({left:margin.left,top:margin.top})
  3104. .svgContainer(container)
  3105. .xScale(x);
  3106. wrap.select(".nv-interactive").call(interactiveLayer);
  3107. }
  3108. gEnter.select('.nv-background')
  3109. .append('rect');
  3110. g.select('.nv-background rect')
  3111. .attr('width', availableWidth)
  3112. .attr('height', availableHeight);
  3113. lines
  3114. //.x(function(d) { return d.x })
  3115. .y(function(d) { return d.display.y })
  3116. .width(availableWidth)
  3117. .height(availableHeight)
  3118. .color(data.map(function(d,i) {
  3119. return d.color || color(d, i);
  3120. }).filter(function(d,i) { return !data[i].disabled && !data[i].tempDisabled; }));
  3121. var linesWrap = g.select('.nv-linesWrap')
  3122. .datum(data.filter(function(d) { return !d.disabled && !d.tempDisabled }));
  3123. linesWrap.call(lines);
  3124. //Store a series index number in the data array.
  3125. data.forEach(function(d,i) {
  3126. d.seriesIndex = i;
  3127. });
  3128. var avgLineData = data.filter(function(d) {
  3129. return !d.disabled && !!average(d);
  3130. });
  3131. var avgLines = g.select(".nv-avgLinesWrap").selectAll("line")
  3132. .data(avgLineData, function(d) { return d.key; });
  3133. var getAvgLineY = function(d) {
  3134. //If average lines go off the svg element, clamp them to the svg bounds.
  3135. var yVal = y(average(d));
  3136. if (yVal < 0) return 0;
  3137. if (yVal > availableHeight) return availableHeight;
  3138. return yVal;
  3139. };
  3140. avgLines.enter()
  3141. .append('line')
  3142. .style('stroke-width',2)
  3143. .style('stroke-dasharray','10,10')
  3144. .style('stroke',function (d,i) {
  3145. return lines.color()(d,d.seriesIndex);
  3146. })
  3147. .attr('x1',0)
  3148. .attr('x2',availableWidth)
  3149. .attr('y1', getAvgLineY)
  3150. .attr('y2', getAvgLineY);
  3151. avgLines
  3152. .style('stroke-opacity',function(d){
  3153. //If average lines go offscreen, make them transparent
  3154. var yVal = y(average(d));
  3155. if (yVal < 0 || yVal > availableHeight) return 0;
  3156. return 1;
  3157. })
  3158. .attr('x1',0)
  3159. .attr('x2',availableWidth)
  3160. .attr('y1', getAvgLineY)
  3161. .attr('y2', getAvgLineY);
  3162. avgLines.exit().remove();
  3163. //Create index line
  3164. var indexLine = linesWrap.selectAll('.nv-indexLine')
  3165. .data([index]);
  3166. indexLine.enter().append('rect').attr('class', 'nv-indexLine')
  3167. .attr('width', 3)
  3168. .attr('x', -2)
  3169. .attr('fill', 'red')
  3170. .attr('fill-opacity', .5)
  3171. .style("pointer-events","all")
  3172. .call(indexDrag);
  3173. indexLine
  3174. .attr('transform', function(d) { return 'translate(' + dx(d.i) + ',0)' })
  3175. .attr('height', availableHeight);
  3176. // Setup Axes
  3177. if (showXAxis) {
  3178. xAxis
  3179. .scale(x)
  3180. ._ticks( nv.utils.calcTicksX(availableWidth/70, data) )
  3181. .tickSize(-availableHeight, 0);
  3182. g.select('.nv-x.nv-axis')
  3183. .attr('transform', 'translate(0,' + y.range()[0] + ')');
  3184. g.select('.nv-x.nv-axis')
  3185. .call(xAxis);
  3186. }
  3187. if (showYAxis) {
  3188. yAxis
  3189. .scale(y)
  3190. ._ticks( nv.utils.calcTicksY(availableHeight/36, data) )
  3191. .tickSize( -availableWidth, 0);
  3192. g.select('.nv-y.nv-axis')
  3193. .call(yAxis);
  3194. }
  3195. //============================================================
  3196. // Event Handling/Dispatching (in chart's scope)
  3197. //------------------------------------------------------------
  3198. function updateZero() {
  3199. indexLine
  3200. .data([index]);
  3201. //When dragging the index line, turn off line transitions.
  3202. // Then turn them back on when done dragging.
  3203. var oldDuration = chart.duration();
  3204. chart.duration(0);
  3205. chart.update();
  3206. chart.duration(oldDuration);
  3207. }
  3208. g.select('.nv-background rect')
  3209. .on('click', function() {
  3210. index.x = d3.mouse(this)[0];
  3211. index.i = Math.round(dx.invert(index.x));
  3212. // update state and send stateChange with new index
  3213. state.index = index.i;
  3214. dispatch.stateChange(state);
  3215. updateZero();
  3216. });
  3217. lines.dispatch.on('elementClick', function(e) {
  3218. index.i = e.pointIndex;
  3219. index.x = dx(index.i);
  3220. // update state and send stateChange with new index
  3221. state.index = index.i;
  3222. dispatch.stateChange(state);
  3223. updateZero();
  3224. });
  3225. controls.dispatch.on('legendClick', function(d,i) {
  3226. d.disabled = !d.disabled;
  3227. rescaleY = !d.disabled;
  3228. state.rescaleY = rescaleY;
  3229. if (!rescaleY) {
  3230. currentYDomain = getCurrentYDomain(data); // rescale is turned off, so set the currentYDomain
  3231. }
  3232. dispatch.stateChange(state);
  3233. chart.update();
  3234. });
  3235. legend.dispatch.on('stateChange', function(newState) {
  3236. for (var key in newState)
  3237. state[key] = newState[key];
  3238. dispatch.stateChange(state);
  3239. chart.update();
  3240. });
  3241. interactiveLayer.dispatch.on('elementMousemove', function(e) {
  3242. lines.clearHighlights();
  3243. var singlePoint, pointIndex, pointXLocation, allData = [];
  3244. data
  3245. .filter(function(series, i) {
  3246. series.seriesIndex = i;
  3247. return !(series.disabled || series.tempDisabled);
  3248. })
  3249. .forEach(function(series,i) {
  3250. pointIndex = nv.interactiveBisect(series.values, e.pointXValue, chart.x());
  3251. lines.highlightPoint(i, pointIndex, true);
  3252. var point = series.values[pointIndex];
  3253. if (typeof point === 'undefined') return;
  3254. if (typeof singlePoint === 'undefined') singlePoint = point;
  3255. if (typeof pointXLocation === 'undefined') pointXLocation = chart.xScale()(chart.x()(point,pointIndex));
  3256. allData.push({
  3257. key: series.key,
  3258. value: chart.y()(point, pointIndex),
  3259. color: color(series,series.seriesIndex)
  3260. });
  3261. });
  3262. //Highlight the tooltip entry based on which point the mouse is closest to.
  3263. if (allData.length > 2) {
  3264. var yValue = chart.yScale().invert(e.mouseY);
  3265. var domainExtent = Math.abs(chart.yScale().domain()[0] - chart.yScale().domain()[1]);
  3266. var threshold = 0.03 * domainExtent;
  3267. var indexToHighlight = nv.nearestValueIndex(allData.map(function(d){return d.value}),yValue,threshold);
  3268. if (indexToHighlight !== null)
  3269. allData[indexToHighlight].highlight = true;
  3270. }
  3271. var xValue = xAxis.tickFormat()(chart.x()(singlePoint,pointIndex), pointIndex);
  3272. interactiveLayer.tooltip
  3273. .valueFormatter(function(d,i) {
  3274. return yAxis.tickFormat()(d);
  3275. })
  3276. .data(
  3277. {
  3278. value: xValue,
  3279. series: allData
  3280. }
  3281. )();
  3282. interactiveLayer.renderGuideLine(pointXLocation);
  3283. });
  3284. interactiveLayer.dispatch.on("elementMouseout",function(e) {
  3285. lines.clearHighlights();
  3286. });
  3287. // Update chart from a state object passed to event handler
  3288. dispatch.on('changeState', function(e) {
  3289. if (typeof e.disabled !== 'undefined') {
  3290. data.forEach(function(series,i) {
  3291. series.disabled = e.disabled[i];
  3292. });
  3293. state.disabled = e.disabled;
  3294. }
  3295. if (typeof e.index !== 'undefined') {
  3296. index.i = e.index;
  3297. index.x = dx(index.i);
  3298. state.index = e.index;
  3299. indexLine
  3300. .data([index]);
  3301. }
  3302. if (typeof e.rescaleY !== 'undefined') {
  3303. rescaleY = e.rescaleY;
  3304. }
  3305. chart.update();
  3306. });
  3307. });
  3308. renderWatch.renderEnd('cumulativeLineChart immediate');
  3309. return chart;
  3310. }
  3311. //============================================================
  3312. // Event Handling/Dispatching (out of chart's scope)
  3313. //------------------------------------------------------------
  3314. lines.dispatch.on('elementMouseover.tooltip', function(evt) {
  3315. var point = {
  3316. x: chart.x()(evt.point),
  3317. y: chart.y()(evt.point),
  3318. color: evt.point.color
  3319. };
  3320. evt.point = point;
  3321. tooltip.data(evt).hidden(false);
  3322. });
  3323. lines.dispatch.on('elementMouseout.tooltip', function(evt) {
  3324. tooltip.hidden(true)
  3325. });
  3326. //============================================================
  3327. // Functions
  3328. //------------------------------------------------------------
  3329. var indexifyYGetter = null;
  3330. /* Normalize the data according to an index point. */
  3331. function indexify(idx, data) {
  3332. if (!indexifyYGetter) indexifyYGetter = lines.y();
  3333. return data.map(function(line, i) {
  3334. if (!line.values) {
  3335. return line;
  3336. }
  3337. var indexValue = line.values[idx];
  3338. if (indexValue == null) {
  3339. return line;
  3340. }
  3341. var v = indexifyYGetter(indexValue, idx);
  3342. // avoid divide by zero
  3343. if (Math.abs(v) < 0.00001 && !noErrorCheck) {
  3344. line.tempDisabled = true;
  3345. return line;
  3346. }
  3347. line.tempDisabled = false;
  3348. line.values = line.values.map(function(point, pointIndex) {
  3349. point.display = {'y': (indexifyYGetter(point, pointIndex) - v) / v };
  3350. return point;
  3351. });
  3352. return line;
  3353. })
  3354. }
  3355. function getCurrentYDomain(data) {
  3356. var seriesDomains = data
  3357. .filter(function(series) { return !(series.disabled || series.tempDisabled)})
  3358. .map(function(series,i) {
  3359. return d3.extent(series.values, function (d) { return d.display.y });
  3360. });
  3361. return [
  3362. d3.min(seriesDomains, function(d) { return d[0] }),
  3363. d3.max(seriesDomains, function(d) { return d[1] })
  3364. ];
  3365. }
  3366. //============================================================
  3367. // Expose Public Variables
  3368. //------------------------------------------------------------
  3369. // expose chart's sub-components
  3370. chart.dispatch = dispatch;
  3371. chart.lines = lines;
  3372. chart.legend = legend;
  3373. chart.controls = controls;
  3374. chart.xAxis = xAxis;
  3375. chart.yAxis = yAxis;
  3376. chart.interactiveLayer = interactiveLayer;
  3377. chart.state = state;
  3378. chart.tooltip = tooltip;
  3379. chart.options = nv.utils.optionsFunc.bind(chart);
  3380. chart._options = Object.create({}, {
  3381. // simple options, just get/set the necessary values
  3382. width: {get: function(){return width;}, set: function(_){width=_;}},
  3383. height: {get: function(){return height;}, set: function(_){height=_;}},
  3384. showControls: {get: function(){return showControls;}, set: function(_){showControls=_;}},
  3385. showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
  3386. average: {get: function(){return average;}, set: function(_){average=_;}},
  3387. defaultState: {get: function(){return defaultState;}, set: function(_){defaultState=_;}},
  3388. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  3389. showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}},
  3390. showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}},
  3391. noErrorCheck: {get: function(){return noErrorCheck;}, set: function(_){noErrorCheck=_;}},
  3392. // options that require extra logic in the setter
  3393. rescaleY: {get: function(){return rescaleY;}, set: function(_){
  3394. rescaleY = _;
  3395. chart.state.rescaleY = _; // also update state
  3396. }},
  3397. margin: {get: function(){return margin;}, set: function(_){
  3398. if (_.top !== undefined) {
  3399. margin.top = _.top;
  3400. marginTop = _.top;
  3401. }
  3402. margin.right = _.right !== undefined ? _.right : margin.right;
  3403. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  3404. margin.left = _.left !== undefined ? _.left : margin.left;
  3405. }},
  3406. color: {get: function(){return color;}, set: function(_){
  3407. color = nv.utils.getColor(_);
  3408. legend.color(color);
  3409. }},
  3410. useInteractiveGuideline: {get: function(){return useInteractiveGuideline;}, set: function(_){
  3411. useInteractiveGuideline = _;
  3412. if (_ === true) {
  3413. chart.interactive(false);
  3414. chart.useVoronoi(false);
  3415. }
  3416. }},
  3417. rightAlignYAxis: {get: function(){return rightAlignYAxis;}, set: function(_){
  3418. rightAlignYAxis = _;
  3419. yAxis.orient( (_) ? 'right' : 'left');
  3420. }},
  3421. duration: {get: function(){return duration;}, set: function(_){
  3422. duration = _;
  3423. lines.duration(duration);
  3424. xAxis.duration(duration);
  3425. yAxis.duration(duration);
  3426. renderWatch.reset(duration);
  3427. }}
  3428. });
  3429. nv.utils.inheritOptions(chart, lines);
  3430. nv.utils.initOptions(chart);
  3431. return chart;
  3432. };
  3433. //TODO: consider deprecating by adding necessary features to multiBar model
  3434. nv.models.discreteBar = function() {
  3435. "use strict";
  3436. //============================================================
  3437. // Public Variables with Default Settings
  3438. //------------------------------------------------------------
  3439. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  3440. , width = 960
  3441. , height = 500
  3442. , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one
  3443. , container
  3444. , x = d3.scale.ordinal()
  3445. , y = d3.scale.linear()
  3446. , getX = function(d) { return d.x }
  3447. , getY = function(d) { return d.y }
  3448. , forceY = [0] // 0 is forced by default.. this makes sense for the majority of bar graphs... user can always do chart.forceY([]) to remove
  3449. , color = nv.utils.defaultColor()
  3450. , showValues = false
  3451. , valueFormat = d3.format(',.2f')
  3452. , xDomain
  3453. , yDomain
  3454. , xRange
  3455. , yRange
  3456. , dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout', 'elementMousemove', 'renderEnd')
  3457. , rectClass = 'discreteBar'
  3458. , duration = 250
  3459. ;
  3460. //============================================================
  3461. // Private Variables
  3462. //------------------------------------------------------------
  3463. var x0, y0;
  3464. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  3465. function chart(selection) {
  3466. renderWatch.reset();
  3467. selection.each(function(data) {
  3468. var availableWidth = width - margin.left - margin.right,
  3469. availableHeight = height - margin.top - margin.bottom;
  3470. container = d3.select(this);
  3471. nv.utils.initSVG(container);
  3472. //add series index to each data point for reference
  3473. data.forEach(function(series, i) {
  3474. series.values.forEach(function(point) {
  3475. point.series = i;
  3476. });
  3477. });
  3478. // Setup Scales
  3479. // remap and flatten the data for use in calculating the scales' domains
  3480. var seriesData = (xDomain && yDomain) ? [] : // if we know xDomain and yDomain, no need to calculate
  3481. data.map(function(d) {
  3482. return d.values.map(function(d,i) {
  3483. return { x: getX(d,i), y: getY(d,i), y0: d.y0 }
  3484. })
  3485. });
  3486. x .domain(xDomain || d3.merge(seriesData).map(function(d) { return d.x }))
  3487. .rangeBands(xRange || [0, availableWidth], .1);
  3488. y .domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) { return d.y }).concat(forceY)));
  3489. // If showValues, pad the Y axis range to account for label height
  3490. if (showValues) y.range(yRange || [availableHeight - (y.domain()[0] < 0 ? 12 : 0), y.domain()[1] > 0 ? 12 : 0]);
  3491. else y.range(yRange || [availableHeight, 0]);
  3492. //store old scales if they exist
  3493. x0 = x0 || x;
  3494. y0 = y0 || y.copy().range([y(0),y(0)]);
  3495. // Setup containers and skeleton of chart
  3496. var wrap = container.selectAll('g.nv-wrap.nv-discretebar').data([data]);
  3497. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-discretebar');
  3498. var gEnter = wrapEnter.append('g');
  3499. var g = wrap.select('g');
  3500. gEnter.append('g').attr('class', 'nv-groups');
  3501. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  3502. //TODO: by definition, the discrete bar should not have multiple groups, will modify/remove later
  3503. var groups = wrap.select('.nv-groups').selectAll('.nv-group')
  3504. .data(function(d) { return d }, function(d) { return d.key });
  3505. groups.enter().append('g')
  3506. .style('stroke-opacity', 1e-6)
  3507. .style('fill-opacity', 1e-6);
  3508. groups.exit()
  3509. .watchTransition(renderWatch, 'discreteBar: exit groups')
  3510. .style('stroke-opacity', 1e-6)
  3511. .style('fill-opacity', 1e-6)
  3512. .remove();
  3513. groups
  3514. .attr('class', function(d,i) { return 'nv-group nv-series-' + i })
  3515. .classed('hover', function(d) { return d.hover });
  3516. groups
  3517. .watchTransition(renderWatch, 'discreteBar: groups')
  3518. .style('stroke-opacity', 1)
  3519. .style('fill-opacity', .75);
  3520. var bars = groups.selectAll('g.nv-bar')
  3521. .data(function(d) { return d.values });
  3522. bars.exit().remove();
  3523. var barsEnter = bars.enter().append('g')
  3524. .attr('transform', function(d,i,j) {
  3525. return 'translate(' + (x(getX(d,i)) + x.rangeBand() * .05 ) + ', ' + y(0) + ')'
  3526. })
  3527. .on('mouseover', function(d,i) { //TODO: figure out why j works above, but not here
  3528. d3.select(this).classed('hover', true);
  3529. dispatch.elementMouseover({
  3530. data: d,
  3531. index: i,
  3532. color: d3.select(this).style("fill")
  3533. });
  3534. })
  3535. .on('mouseout', function(d,i) {
  3536. d3.select(this).classed('hover', false);
  3537. dispatch.elementMouseout({
  3538. data: d,
  3539. index: i,
  3540. color: d3.select(this).style("fill")
  3541. });
  3542. })
  3543. .on('mousemove', function(d,i) {
  3544. dispatch.elementMousemove({
  3545. data: d,
  3546. index: i,
  3547. color: d3.select(this).style("fill")
  3548. });
  3549. })
  3550. .on('click', function(d,i) {
  3551. var element = this;
  3552. dispatch.elementClick({
  3553. data: d,
  3554. index: i,
  3555. color: d3.select(this).style("fill"),
  3556. event: d3.event,
  3557. element: element
  3558. });
  3559. d3.event.stopPropagation();
  3560. })
  3561. .on('dblclick', function(d,i) {
  3562. dispatch.elementDblClick({
  3563. data: d,
  3564. index: i,
  3565. color: d3.select(this).style("fill")
  3566. });
  3567. d3.event.stopPropagation();
  3568. });
  3569. barsEnter.append('rect')
  3570. .attr('height', 0)
  3571. .attr('width', x.rangeBand() * .9 / data.length )
  3572. if (showValues) {
  3573. barsEnter.append('text')
  3574. .attr('text-anchor', 'middle')
  3575. ;
  3576. bars.select('text')
  3577. .text(function(d,i) { return valueFormat(getY(d,i)) })
  3578. .watchTransition(renderWatch, 'discreteBar: bars text')
  3579. .attr('x', x.rangeBand() * .9 / 2)
  3580. .attr('y', function(d,i) { return getY(d,i) < 0 ? y(getY(d,i)) - y(0) + 12 : -4 })
  3581. ;
  3582. } else {
  3583. bars.selectAll('text').remove();
  3584. }
  3585. bars
  3586. .attr('class', function(d,i) { return getY(d,i) < 0 ? 'nv-bar negative' : 'nv-bar positive' })
  3587. .style('fill', function(d,i) { return d.color || color(d,i) })
  3588. .style('stroke', function(d,i) { return d.color || color(d,i) })
  3589. .select('rect')
  3590. .attr('class', rectClass)
  3591. .watchTransition(renderWatch, 'discreteBar: bars rect')
  3592. .attr('width', x.rangeBand() * .9 / data.length);
  3593. bars.watchTransition(renderWatch, 'discreteBar: bars')
  3594. //.delay(function(d,i) { return i * 1200 / data[0].values.length })
  3595. .attr('transform', function(d,i) {
  3596. var left = x(getX(d,i)) + x.rangeBand() * .05,
  3597. top = getY(d,i) < 0 ?
  3598. y(0) :
  3599. y(0) - y(getY(d,i)) < 1 ?
  3600. y(0) - 1 : //make 1 px positive bars show up above y=0
  3601. y(getY(d,i));
  3602. return 'translate(' + left + ', ' + top + ')'
  3603. })
  3604. .select('rect')
  3605. .attr('height', function(d,i) {
  3606. return Math.max(Math.abs(y(getY(d,i)) - y(0)), 1)
  3607. });
  3608. //store old scales for use in transitions on update
  3609. x0 = x.copy();
  3610. y0 = y.copy();
  3611. });
  3612. renderWatch.renderEnd('discreteBar immediate');
  3613. return chart;
  3614. }
  3615. //============================================================
  3616. // Expose Public Variables
  3617. //------------------------------------------------------------
  3618. chart.dispatch = dispatch;
  3619. chart.options = nv.utils.optionsFunc.bind(chart);
  3620. chart._options = Object.create({}, {
  3621. // simple options, just get/set the necessary values
  3622. width: {get: function(){return width;}, set: function(_){width=_;}},
  3623. height: {get: function(){return height;}, set: function(_){height=_;}},
  3624. forceY: {get: function(){return forceY;}, set: function(_){forceY=_;}},
  3625. showValues: {get: function(){return showValues;}, set: function(_){showValues=_;}},
  3626. x: {get: function(){return getX;}, set: function(_){getX=_;}},
  3627. y: {get: function(){return getY;}, set: function(_){getY=_;}},
  3628. xScale: {get: function(){return x;}, set: function(_){x=_;}},
  3629. yScale: {get: function(){return y;}, set: function(_){y=_;}},
  3630. xDomain: {get: function(){return xDomain;}, set: function(_){xDomain=_;}},
  3631. yDomain: {get: function(){return yDomain;}, set: function(_){yDomain=_;}},
  3632. xRange: {get: function(){return xRange;}, set: function(_){xRange=_;}},
  3633. yRange: {get: function(){return yRange;}, set: function(_){yRange=_;}},
  3634. valueFormat: {get: function(){return valueFormat;}, set: function(_){valueFormat=_;}},
  3635. id: {get: function(){return id;}, set: function(_){id=_;}},
  3636. rectClass: {get: function(){return rectClass;}, set: function(_){rectClass=_;}},
  3637. // options that require extra logic in the setter
  3638. margin: {get: function(){return margin;}, set: function(_){
  3639. margin.top = _.top !== undefined ? _.top : margin.top;
  3640. margin.right = _.right !== undefined ? _.right : margin.right;
  3641. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  3642. margin.left = _.left !== undefined ? _.left : margin.left;
  3643. }},
  3644. color: {get: function(){return color;}, set: function(_){
  3645. color = nv.utils.getColor(_);
  3646. }},
  3647. duration: {get: function(){return duration;}, set: function(_){
  3648. duration = _;
  3649. renderWatch.reset(duration);
  3650. }}
  3651. });
  3652. nv.utils.initOptions(chart);
  3653. return chart;
  3654. };
  3655. nv.models.discreteBarChart = function() {
  3656. "use strict";
  3657. //============================================================
  3658. // Public Variables with Default Settings
  3659. //------------------------------------------------------------
  3660. var discretebar = nv.models.discreteBar()
  3661. , xAxis = nv.models.axis()
  3662. , yAxis = nv.models.axis()
  3663. , legend = nv.models.legend()
  3664. , tooltip = nv.models.tooltip()
  3665. ;
  3666. var margin = {top: 15, right: 10, bottom: 50, left: 60}
  3667. , marginTop = null
  3668. , width = null
  3669. , height = null
  3670. , color = nv.utils.getColor()
  3671. , showLegend = false
  3672. , showXAxis = true
  3673. , showYAxis = true
  3674. , rightAlignYAxis = false
  3675. , staggerLabels = false
  3676. , wrapLabels = false
  3677. , rotateLabels = 0
  3678. , x
  3679. , y
  3680. , noData = null
  3681. , dispatch = d3.dispatch('beforeUpdate','renderEnd')
  3682. , duration = 250
  3683. ;
  3684. xAxis
  3685. .orient('bottom')
  3686. .showMaxMin(false)
  3687. .tickFormat(function(d) { return d })
  3688. ;
  3689. yAxis
  3690. .orient((rightAlignYAxis) ? 'right' : 'left')
  3691. .tickFormat(d3.format(',.1f'))
  3692. ;
  3693. tooltip
  3694. .duration(0)
  3695. .headerEnabled(false)
  3696. .valueFormatter(function(d, i) {
  3697. return yAxis.tickFormat()(d, i);
  3698. })
  3699. .keyFormatter(function(d, i) {
  3700. return xAxis.tickFormat()(d, i);
  3701. });
  3702. //============================================================
  3703. // Private Variables
  3704. //------------------------------------------------------------
  3705. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  3706. function chart(selection) {
  3707. renderWatch.reset();
  3708. renderWatch.models(discretebar);
  3709. if (showXAxis) renderWatch.models(xAxis);
  3710. if (showYAxis) renderWatch.models(yAxis);
  3711. selection.each(function(data) {
  3712. var container = d3.select(this),
  3713. that = this;
  3714. nv.utils.initSVG(container);
  3715. var availableWidth = nv.utils.availableWidth(width, container, margin),
  3716. availableHeight = nv.utils.availableHeight(height, container, margin);
  3717. chart.update = function() {
  3718. dispatch.beforeUpdate();
  3719. container.transition().duration(duration).call(chart);
  3720. };
  3721. chart.container = this;
  3722. // Display No Data message if there's nothing to show.
  3723. if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
  3724. nv.utils.noData(chart, container);
  3725. return chart;
  3726. } else {
  3727. container.selectAll('.nv-noData').remove();
  3728. }
  3729. // Setup Scales
  3730. x = discretebar.xScale();
  3731. y = discretebar.yScale().clamp(true);
  3732. // Setup containers and skeleton of chart
  3733. var wrap = container.selectAll('g.nv-wrap.nv-discreteBarWithAxes').data([data]);
  3734. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-discreteBarWithAxes').append('g');
  3735. var defsEnter = gEnter.append('defs');
  3736. var g = wrap.select('g');
  3737. gEnter.append('g').attr('class', 'nv-x nv-axis');
  3738. gEnter.append('g').attr('class', 'nv-y nv-axis')
  3739. .append('g').attr('class', 'nv-zeroLine')
  3740. .append('line');
  3741. gEnter.append('g').attr('class', 'nv-barsWrap');
  3742. gEnter.append('g').attr('class', 'nv-legendWrap');
  3743. g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  3744. // Legend
  3745. if (!showLegend) {
  3746. g.select('.nv-legendWrap').selectAll('*').remove();
  3747. } else {
  3748. legend.width(availableWidth);
  3749. g.select('.nv-legendWrap')
  3750. .datum(data)
  3751. .call(legend);
  3752. if (!marginTop && legend.height() !== margin.top) {
  3753. margin.top = legend.height();
  3754. availableHeight = nv.utils.availableHeight(height, container, margin);
  3755. }
  3756. wrap.select('.nv-legendWrap')
  3757. .attr('transform', 'translate(0,' + (-margin.top) +')')
  3758. }
  3759. if (rightAlignYAxis) {
  3760. g.select(".nv-y.nv-axis")
  3761. .attr("transform", "translate(" + availableWidth + ",0)");
  3762. }
  3763. // Main Chart Component(s)
  3764. discretebar
  3765. .width(availableWidth)
  3766. .height(availableHeight);
  3767. var barsWrap = g.select('.nv-barsWrap')
  3768. .datum(data.filter(function(d) { return !d.disabled }));
  3769. barsWrap.transition().call(discretebar);
  3770. defsEnter.append('clipPath')
  3771. .attr('id', 'nv-x-label-clip-' + discretebar.id())
  3772. .append('rect');
  3773. g.select('#nv-x-label-clip-' + discretebar.id() + ' rect')
  3774. .attr('width', x.rangeBand() * (staggerLabels ? 2 : 1))
  3775. .attr('height', 16)
  3776. .attr('x', -x.rangeBand() / (staggerLabels ? 1 : 2 ));
  3777. // Setup Axes
  3778. if (showXAxis) {
  3779. xAxis
  3780. .scale(x)
  3781. ._ticks( nv.utils.calcTicksX(availableWidth/100, data) )
  3782. .tickSize(-availableHeight, 0);
  3783. g.select('.nv-x.nv-axis')
  3784. .attr('transform', 'translate(0,' + (y.range()[0] + ((discretebar.showValues() && y.domain()[0] < 0) ? 16 : 0)) + ')');
  3785. g.select('.nv-x.nv-axis').call(xAxis);
  3786. var xTicks = g.select('.nv-x.nv-axis').selectAll('g');
  3787. if (staggerLabels) {
  3788. xTicks
  3789. .selectAll('text')
  3790. .attr('transform', function(d,i,j) { return 'translate(0,' + (j % 2 == 0 ? '5' : '17') + ')' })
  3791. }
  3792. if (rotateLabels) {
  3793. xTicks
  3794. .selectAll('.tick text')
  3795. .attr('transform', 'rotate(' + rotateLabels + ' 0,0)')
  3796. .style('text-anchor', rotateLabels > 0 ? 'start' : 'end');
  3797. }
  3798. if (wrapLabels) {
  3799. g.selectAll('.tick text')
  3800. .call(nv.utils.wrapTicks, chart.xAxis.rangeBand())
  3801. }
  3802. }
  3803. if (showYAxis) {
  3804. yAxis
  3805. .scale(y)
  3806. ._ticks( nv.utils.calcTicksY(availableHeight/36, data) )
  3807. .tickSize( -availableWidth, 0);
  3808. g.select('.nv-y.nv-axis').call(yAxis);
  3809. }
  3810. // Zero line
  3811. g.select(".nv-zeroLine line")
  3812. .attr("x1",0)
  3813. .attr("x2",(rightAlignYAxis) ? -availableWidth : availableWidth)
  3814. .attr("y1", y(0))
  3815. .attr("y2", y(0))
  3816. ;
  3817. });
  3818. renderWatch.renderEnd('discreteBar chart immediate');
  3819. return chart;
  3820. }
  3821. //============================================================
  3822. // Event Handling/Dispatching (out of chart's scope)
  3823. //------------------------------------------------------------
  3824. discretebar.dispatch.on('elementMouseover.tooltip', function(evt) {
  3825. evt['series'] = {
  3826. key: chart.x()(evt.data),
  3827. value: chart.y()(evt.data),
  3828. color: evt.color
  3829. };
  3830. tooltip.data(evt).hidden(false);
  3831. });
  3832. discretebar.dispatch.on('elementMouseout.tooltip', function(evt) {
  3833. tooltip.hidden(true);
  3834. });
  3835. discretebar.dispatch.on('elementMousemove.tooltip', function(evt) {
  3836. tooltip();
  3837. });
  3838. //============================================================
  3839. // Expose Public Variables
  3840. //------------------------------------------------------------
  3841. chart.dispatch = dispatch;
  3842. chart.discretebar = discretebar;
  3843. chart.legend = legend;
  3844. chart.xAxis = xAxis;
  3845. chart.yAxis = yAxis;
  3846. chart.tooltip = tooltip;
  3847. chart.options = nv.utils.optionsFunc.bind(chart);
  3848. chart._options = Object.create({}, {
  3849. // simple options, just get/set the necessary values
  3850. width: {get: function(){return width;}, set: function(_){width=_;}},
  3851. height: {get: function(){return height;}, set: function(_){height=_;}},
  3852. showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
  3853. staggerLabels: {get: function(){return staggerLabels;}, set: function(_){staggerLabels=_;}},
  3854. rotateLabels: {get: function(){return rotateLabels;}, set: function(_){rotateLabels=_;}},
  3855. wrapLabels: {get: function(){return wrapLabels;}, set: function(_){wrapLabels=!!_;}},
  3856. showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}},
  3857. showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}},
  3858. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  3859. // options that require extra logic in the setter
  3860. margin: {get: function(){return margin;}, set: function(_){
  3861. if (_.top !== undefined) {
  3862. margin.top = _.top;
  3863. marginTop = _.top;
  3864. }
  3865. margin.right = _.right !== undefined ? _.right : margin.right;
  3866. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  3867. margin.left = _.left !== undefined ? _.left : margin.left;
  3868. }},
  3869. duration: {get: function(){return duration;}, set: function(_){
  3870. duration = _;
  3871. renderWatch.reset(duration);
  3872. discretebar.duration(duration);
  3873. xAxis.duration(duration);
  3874. yAxis.duration(duration);
  3875. }},
  3876. color: {get: function(){return color;}, set: function(_){
  3877. color = nv.utils.getColor(_);
  3878. discretebar.color(color);
  3879. legend.color(color);
  3880. }},
  3881. rightAlignYAxis: {get: function(){return rightAlignYAxis;}, set: function(_){
  3882. rightAlignYAxis = _;
  3883. yAxis.orient( (_) ? 'right' : 'left');
  3884. }}
  3885. });
  3886. nv.utils.inheritOptions(chart, discretebar);
  3887. nv.utils.initOptions(chart);
  3888. return chart;
  3889. }
  3890. nv.models.distribution = function() {
  3891. "use strict";
  3892. //============================================================
  3893. // Public Variables with Default Settings
  3894. //------------------------------------------------------------
  3895. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  3896. , width = 400 //technically width or height depending on x or y....
  3897. , size = 8
  3898. , axis = 'x' // 'x' or 'y'... horizontal or vertical
  3899. , getData = function(d) { return d[axis] } // defaults d.x or d.y
  3900. , color = nv.utils.defaultColor()
  3901. , scale = d3.scale.linear()
  3902. , domain
  3903. , duration = 250
  3904. , dispatch = d3.dispatch('renderEnd')
  3905. ;
  3906. //============================================================
  3907. //============================================================
  3908. // Private Variables
  3909. //------------------------------------------------------------
  3910. var scale0;
  3911. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  3912. //============================================================
  3913. function chart(selection) {
  3914. renderWatch.reset();
  3915. selection.each(function(data) {
  3916. var availableLength = width - (axis === 'x' ? margin.left + margin.right : margin.top + margin.bottom),
  3917. naxis = axis == 'x' ? 'y' : 'x',
  3918. container = d3.select(this);
  3919. nv.utils.initSVG(container);
  3920. //------------------------------------------------------------
  3921. // Setup Scales
  3922. scale0 = scale0 || scale;
  3923. //------------------------------------------------------------
  3924. //------------------------------------------------------------
  3925. // Setup containers and skeleton of chart
  3926. var wrap = container.selectAll('g.nv-distribution').data([data]);
  3927. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-distribution');
  3928. var gEnter = wrapEnter.append('g');
  3929. var g = wrap.select('g');
  3930. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
  3931. //------------------------------------------------------------
  3932. var distWrap = g.selectAll('g.nv-dist')
  3933. .data(function(d) { return d }, function(d) { return d.key });
  3934. distWrap.enter().append('g');
  3935. distWrap
  3936. .attr('class', function(d,i) { return 'nv-dist nv-series-' + i })
  3937. .style('stroke', function(d,i) { return color(d, i) });
  3938. var dist = distWrap.selectAll('line.nv-dist' + axis)
  3939. .data(function(d) { return d.values })
  3940. dist.enter().append('line')
  3941. .attr(axis + '1', function(d,i) { return scale0(getData(d,i)) })
  3942. .attr(axis + '2', function(d,i) { return scale0(getData(d,i)) })
  3943. renderWatch.transition(distWrap.exit().selectAll('line.nv-dist' + axis), 'dist exit')
  3944. // .transition()
  3945. .attr(axis + '1', function(d,i) { return scale(getData(d,i)) })
  3946. .attr(axis + '2', function(d,i) { return scale(getData(d,i)) })
  3947. .style('stroke-opacity', 0)
  3948. .remove();
  3949. dist
  3950. .attr('class', function(d,i) { return 'nv-dist' + axis + ' nv-dist' + axis + '-' + i })
  3951. .attr(naxis + '1', 0)
  3952. .attr(naxis + '2', size);
  3953. renderWatch.transition(dist, 'dist')
  3954. // .transition()
  3955. .attr(axis + '1', function(d,i) { return scale(getData(d,i)) })
  3956. .attr(axis + '2', function(d,i) { return scale(getData(d,i)) })
  3957. scale0 = scale.copy();
  3958. });
  3959. renderWatch.renderEnd('distribution immediate');
  3960. return chart;
  3961. }
  3962. //============================================================
  3963. // Expose Public Variables
  3964. //------------------------------------------------------------
  3965. chart.options = nv.utils.optionsFunc.bind(chart);
  3966. chart.dispatch = dispatch;
  3967. chart.margin = function(_) {
  3968. if (!arguments.length) return margin;
  3969. margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
  3970. margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
  3971. margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
  3972. margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
  3973. return chart;
  3974. };
  3975. chart.width = function(_) {
  3976. if (!arguments.length) return width;
  3977. width = _;
  3978. return chart;
  3979. };
  3980. chart.axis = function(_) {
  3981. if (!arguments.length) return axis;
  3982. axis = _;
  3983. return chart;
  3984. };
  3985. chart.size = function(_) {
  3986. if (!arguments.length) return size;
  3987. size = _;
  3988. return chart;
  3989. };
  3990. chart.getData = function(_) {
  3991. if (!arguments.length) return getData;
  3992. getData = d3.functor(_);
  3993. return chart;
  3994. };
  3995. chart.scale = function(_) {
  3996. if (!arguments.length) return scale;
  3997. scale = _;
  3998. return chart;
  3999. };
  4000. chart.color = function(_) {
  4001. if (!arguments.length) return color;
  4002. color = nv.utils.getColor(_);
  4003. return chart;
  4004. };
  4005. chart.duration = function(_) {
  4006. if (!arguments.length) return duration;
  4007. duration = _;
  4008. renderWatch.reset(duration);
  4009. return chart;
  4010. };
  4011. //============================================================
  4012. return chart;
  4013. }
  4014. nv.models.distroPlot = function() {
  4015. "use strict";
  4016. // IMPROVEMENTS:
  4017. // - cleanup tooltip to look like candlestick example (don't need color square for everything)
  4018. // - extend y scale range to min/max data better visually
  4019. // - tips of violins need to be cut off if very long
  4020. // - transition from box to violin not great since box only has a few points, and violin has many - need to generate box with as many points as violin
  4021. // - when providing colorGroup, should color boxes by either parent or child group category (e.g. isolator)
  4022. //============================================================
  4023. // Public Variables with Default Settings
  4024. //------------------------------------------------------------
  4025. var margin = {top: 0, right: 0, bottom: 0, left: 0},
  4026. width = 960,
  4027. height = 500,
  4028. id = Math.floor(Math.random() * 10000), // Create semi-unique ID in case user doesn't select one
  4029. xScale = d3.scale.ordinal(),
  4030. yScale = d3.scale.linear(),
  4031. getX = function(d) { return d.label }, // Default data model selectors.
  4032. getY = function(d) { return d.value },
  4033. getColor = function(d) { return d.color },
  4034. getQ1 = function(d) { return d.values.q1 },
  4035. getQ2 = function(d) { return d.values.q2 },
  4036. getQ3 = function(d) { return d.values.q3 },
  4037. getNl = function(d) { return (centralTendency == 'mean' ? getMean(d) : getQ2(d)) - d.values.notch },
  4038. getNu = function(d) { return (centralTendency == 'mean' ? getMean(d) : getQ2(d)) + d.values.notch },
  4039. getMean = function(d) { return d.values.mean },
  4040. getWl = function(d) { return d.values.wl[whiskerDef] },
  4041. getWh = function(d) { return d.values.wu[whiskerDef] },
  4042. getMin = function(d) { return d.values.min },
  4043. getMax = function(d) { return d.values.max },
  4044. getDev = function(d) { return d.values.dev },
  4045. getValsObj = function(d) { return d.values.observations; },
  4046. getValsArr = function(d) { return d.values.observations.map(function(e) { return e.y }); },
  4047. plotType, // type of background: 'box', 'violin', 'none'/false - default: 'box' - 'none' will activate random scatter automatically
  4048. observationType = false, // type of observations to show: 'random', 'swarm', 'line', 'centered' - default: false (don't show any observations, even if an outlier)
  4049. whiskerDef = 'iqr', // type of whisker to render: 'iqr', 'minmax', 'stddev' - default: iqr
  4050. hideWhiskers = false,
  4051. notchBox = false, // bool whether to notch box
  4052. colorGroup = false, // if specified, each x-category will be split into groups, each colored
  4053. centralTendency = false,
  4054. showOnlyOutliers = true, // show only outliers in box plot
  4055. jitter = 0.7, // faction of that jitter should take up in 'random' observationType, must be in range [0,1]; see jitterX(), default 0.7
  4056. squash = true, // whether to remove the x-axis positions for empty data groups, default is true
  4057. bandwidth = 'scott', // bandwidth for kde calculation, can be float or str, if str, must be one of scott or silverman
  4058. clampViolin = true, // whether to clamp the "tails" of the violin; prevents long 0-density area
  4059. resolution = 50,
  4060. pointSize = 3,
  4061. color = nv.utils.defaultColor(),
  4062. container = null,
  4063. xDomain, xRange,
  4064. yDomain, yRange,
  4065. dispatch = d3.dispatch('elementMouseover', 'elementMouseout', 'elementMousemove', 'renderEnd'),
  4066. duration = 250,
  4067. maxBoxWidth = null;
  4068. //============================================================
  4069. // Helper Functions
  4070. //------------------------------------------------------------
  4071. /* Returns the smaller of std(X, ddof=1) or normalized IQR(X) over axis 0.
  4072. *
  4073. * @param (list) x - input x formatted as a single list of values
  4074. *
  4075. * @return float
  4076. *
  4077. * Source: https://github.com/statsmodels/statsmodels/blob/master/statsmodels/nonparametric/bandwidths.py#L9
  4078. */
  4079. function select_sigma(x) {
  4080. var sorted = x.sort(d3.ascending); // sort our dat
  4081. var normalize = 1.349;
  4082. var IQR = (d3.quantile(sorted, 0.75) - d3.quantile(sorted, 0.25))/normalize; // normalized IQR
  4083. return d3.min([d3.deviation(sorted), IQR]);
  4084. }
  4085. /*
  4086. Scott's Rule of Thumb
  4087. Parameters
  4088. ----------
  4089. x : array-like
  4090. Array for which to get the bandwidth
  4091. type : string
  4092. The type of estimate to use, must be one of scott or silverman
  4093. Returns
  4094. -------
  4095. bw : float
  4096. The estimate of the bandwidth
  4097. Notes
  4098. -----
  4099. Returns 1.059 * A * n ** (-1/5.) where ::
  4100. A = min(std(x, ddof=1), IQR/1.349)
  4101. IQR = np.subtract.reduce(np.percentile(x, [75,25]))
  4102. References
  4103. ----------
  4104. Scott, D.W. (1992) Multivariate Density Estimation: Theory, Practice, and
  4105. Visualization.
  4106. */
  4107. function calcBandwidth(x, type) {
  4108. if (typeof type === 'undefined') type = 'scott';
  4109. // TODO: consider using https://github.com/jasondavies/science.js
  4110. var A = select_sigma(x);
  4111. var n = x.length;
  4112. return type==='scott' ? Math.pow(1.059 * A * n, -0.2) : Math.pow(.9 * A * n, -0.2);
  4113. }
  4114. /*
  4115. * Prep data for use with distroPlot by grouping data
  4116. * by .x() option set by user and then calculating
  4117. * count, sum, mean, q1, q2 (median), q3, lower whisker (wl)
  4118. * upper whisker (wu), iqr, min, max, and standard dev.
  4119. *
  4120. * NOTE: preparing this data can be resource intensive, and
  4121. * is therefore only run once on plot load. It can
  4122. * manually be run by calling recalcData(). This should
  4123. * be re-run any time the axis accessors are changed or
  4124. * when bandwidth/resolution are updated.
  4125. *
  4126. * NOTE: this will also setup the individual vertical scales
  4127. * for the violins.
  4128. *
  4129. * @param (list) dat - input data formatted as list of objects,
  4130. * with an object key that must exist when accessed by getX()
  4131. *
  4132. * @return prepared data in the form for box plotType:
  4133. * [{
  4134. * key : YY,
  4135. * values: {
  4136. * count: XX,
  4137. * sum: XX,
  4138. * mean: XX,
  4139. * q1: XX,
  4140. * q2: XX,
  4141. * q3: XX,
  4142. * wl: XX,
  4143. * wu: XX,
  4144. * iqr: XX,
  4145. * min: XX,
  4146. * max: XX,
  4147. * dev: XX,
  4148. * observations: [{y:XX,..},..],
  4149. * key: XX,
  4150. * kdeDat: XX,
  4151. * notch: XX,
  4152. * }
  4153. * },
  4154. * ...
  4155. * ]
  4156. * for violin plotType:
  4157. * [{
  4158. * key : YY,
  4159. * values: {
  4160. * original: [{y:XX,..},..]
  4161. * }
  4162. * },
  4163. * ...
  4164. * ]
  4165. * where YY are those keys in dat that define the
  4166. * x-axis and which are defined by .x()
  4167. */
  4168. function prepData(dat) {
  4169. // helper function to calcuate the various boxplot stats
  4170. function calcStats(g, xGroup) {
  4171. // sort data by Y so we can calc quartiles
  4172. var v = g.map(function(d) {
  4173. if (colorGroup) allColorGroups.add(colorGroup(d)); // list of all colorGroups; used to set x-axis
  4174. return getY(d);
  4175. }).sort(d3.ascending);
  4176. var q1 = d3.quantile(v, 0.25);
  4177. var q3 = d3.quantile(v, 0.75);
  4178. var iqr = q3 - q1;
  4179. var upper = q3 + 1.5 * iqr;
  4180. var lower = q1 - 1.5 * iqr;
  4181. /* whisker definitions:
  4182. * - iqr: also known as Tukey boxplot, the lowest datum still within 1.5 IQR of the lower quartile, and the highest datum still within 1.5 IQR of the upper quartile
  4183. * - minmax: the minimum and maximum of all of the data
  4184. * - sttdev: one standard deviation above and below the mean of the data
  4185. * Note that the central tendency type (median or mean) does not impact the whisker location
  4186. */
  4187. var wl = {iqr: d3.max([d3.min(v), d3.min(v.filter(function(d) {return d > lower}))]), minmax: d3.min(v), stddev: d3.mean(v) - d3.deviation(v)};
  4188. var wu = {iqr: d3.min([d3.max(v), d3.max(v.filter(function(d) {return d < upper}))]), minmax: d3.max(v), stddev: d3.mean(v) + d3.deviation(v)};
  4189. var median = d3.median(v);
  4190. var mean = d3.mean(v);
  4191. var observations = [];
  4192. // d3-beeswarm library must be externally loaded if being used
  4193. // https://github.com/Kcnarf/d3-beeswarm
  4194. if (typeof d3.beeswarm !== 'undefined') {
  4195. observations = d3.beeswarm()
  4196. .data(g.map(function(e) { return getY(e); }))
  4197. .radius(pointSize+1)
  4198. .orientation('vertical')
  4199. .side('symmetric')
  4200. .distributeOn(function(e) { return yScale(e); })
  4201. .arrange()
  4202. // add group info for tooltip
  4203. observations.map(function(e,i) {
  4204. e.key = xGroup;
  4205. e.object_constancy = g[i].object_constancy;
  4206. e.isOutlier = (e.datum < wl.iqr || e.datum > wu.iqr) // add isOulier meta for proper class assignment
  4207. e.isOutlierStdDev = (e.datum < wl.stddev || e.datum > wu.stddev) // add isOulier meta for proper class assignment
  4208. e.randX = Math.random() * jitter * (Math.floor(Math.random()*2) == 1 ? 1 : -1) // calculate random x-position only once for each point
  4209. })
  4210. } else {
  4211. v.forEach(function(e,i) {
  4212. observations.push({
  4213. object_constancy: e.object_constancy,
  4214. datum: e,
  4215. key: xGroup,
  4216. isOutlier: (e < wl.iqr || e > wu.iqr), // add isOulier meta for proper class assignment
  4217. isOutlierStdDev: (e < wl.stddev || e > wu.stddev), // add isOulier meta for proper class assignment
  4218. randX: Math.random() * jitter * (Math.floor(Math.random()*2) == 1 ? 1 : -1)
  4219. })
  4220. })
  4221. }
  4222. // calculate bandwidth if no number is provided
  4223. if(isNaN(parseFloat(bandwidth))) { // if not is float
  4224. var bandwidthCalc;
  4225. if (['scott','silverman'].indexOf(bandwidth) != -1) {
  4226. bandwidthCalc = calcBandwidth(v, bandwidth);
  4227. } else {
  4228. bandwidthCalc = calcBandwidth(v); // calculate with default 'scott'
  4229. }
  4230. }
  4231. var kde = kernelDensityEstimator(eKernel(bandwidthCalc), yScale.ticks(resolution));
  4232. var kdeDat = clampViolin ? clampViolinKDE(kde(v), d3.extent(v)) : kde(v);
  4233. // make a new vertical scale for each group
  4234. var tmpScale = d3.scale.linear()
  4235. .domain([0, d3.max(kdeDat, function (e) { return e.y;})])
  4236. .clamp(true);
  4237. yVScale.push(tmpScale);
  4238. var reformat = {
  4239. count: v.length,
  4240. num_outlier: observations.filter(function (e) { return e.isOutlier; }).length,
  4241. sum: d3.sum(v),
  4242. mean: mean,
  4243. q1: q1,
  4244. q2: median,
  4245. q3: q3,
  4246. wl: wl,
  4247. wu: wu,
  4248. iqr: iqr,
  4249. min: d3.min(v),
  4250. max: d3.max(v),
  4251. dev: d3.deviation(v),
  4252. observations: observations,
  4253. key: xGroup,
  4254. kde: kdeDat,
  4255. notch: 1.57 * iqr / Math.sqrt(v.length), // notch distance from mean/median
  4256. };
  4257. if (colorGroup) {reformatDatFlat.push({key: xGroup, values: reformat});}
  4258. return reformat;
  4259. }
  4260. // assign a unique identifier for each point for object constancy
  4261. // this makes updating data possible
  4262. dat.forEach(function(d,i) { d.object_constancy = i + '_' + getY(d) + '_' + getX(d); })
  4263. // TODO not DRY
  4264. // couldn't find a conditional way of doing the key() grouping
  4265. var formatted;
  4266. if (!colorGroup) {
  4267. formatted = d3.nest()
  4268. .key(function(d) { return getX(d); })
  4269. .rollup(function(v,i) {
  4270. return calcStats(v);
  4271. })
  4272. .entries(dat);
  4273. } else {
  4274. allColorGroups = d3.set() // reset
  4275. var tmp = d3.nest()
  4276. .key(function(d) { return getX(d); })
  4277. .key(function(d) { return colorGroup(d); })
  4278. .rollup(function(v) {
  4279. return calcStats(v, getX(v[0]));
  4280. })
  4281. .entries(dat);
  4282. // generate a final list of all x & colorGroup combinations
  4283. // this is used to properly set the x-axis domain
  4284. allColorGroups = allColorGroups.values(); // convert from d3.set to list
  4285. var xGroups = tmp.map(function(d) { return d.key; });
  4286. var allGroups = [];
  4287. for (var i = 0; i < xGroups.length; i++) {
  4288. for (var j = 0; j < allColorGroups.length; j++) {
  4289. allGroups.push(xGroups[i] + '_' + allColorGroups[j]);
  4290. }
  4291. }
  4292. allColorGroups = allGroups;
  4293. // flatten the inner most level so that
  4294. // the plot retains the same DOM structure
  4295. // to allow for smooth updating between
  4296. // all groups.
  4297. formatted = [];
  4298. tmp.forEach(function(d) {
  4299. d.values.forEach(function(e) { e.key = d.key +'_'+e.key }) // generate a combo key so that each boxplot has a distinct x-position
  4300. formatted.push.apply(formatted, d.values)
  4301. });
  4302. }
  4303. return formatted;
  4304. }
  4305. // https://bl.ocks.org/mbostock/4341954
  4306. function kernelDensityEstimator(kernel, X) {
  4307. return function (sample) {
  4308. return X.map(function(x) {
  4309. var y = d3.mean(sample, function (v) {return kernel(x - v);});
  4310. return {x:x, y:y};
  4311. });
  4312. };
  4313. }
  4314. /*
  4315. * Limit whether the density extends past the extreme datapoints
  4316. * of the violin.
  4317. *
  4318. * @param (list) kde - x & y kde cooridinates
  4319. * @param (list) extent - min/max y-values used for clamping violing
  4320. */
  4321. function clampViolinKDE(kde, extent) {
  4322. // this handles the case when all the x-values are equal
  4323. // which means no kde could be properly calculated
  4324. // just return the kde data so we can continue plotting successfully
  4325. if (extent[0] === extent[1]) return kde;
  4326. var clamped = kde.reduce(function(res, d) {
  4327. if (d.x >= extent[0] && d.x <= extent[1]) res.push(d);
  4328. return res;
  4329. },[]);
  4330. // add the extreme data points back in
  4331. if (extent[0] < clamped[0].x) clamped.unshift({x:extent[0], y:clamped[0].y})
  4332. if (extent[1] > clamped[clamped.length-1].x) clamped.push({x:extent[1], y:clamped[clamped.length-1].y})
  4333. return clamped;
  4334. }
  4335. // https://bl.ocks.org/mbostock/4341954
  4336. function eKernel(scale) {
  4337. return function (u) {
  4338. return Math.abs(u /= scale) <= 1 ? .75 * (1 - u * u) / scale : 0;
  4339. };
  4340. }
  4341. /**
  4342. * Makes the svg polygon string for a boxplot in either a notched
  4343. * or square version
  4344. *
  4345. * NOTE: this actually only draws the left half of the box, since
  4346. * the shape is symmetric (and since this is how violins are drawn)
  4347. * we can simply generate half the box and mirror it.
  4348. *
  4349. * @param boxLeft {float} - left position of box
  4350. * @param notchLeft {float} - left position of notch
  4351. * @param dat {obj} - box plot data that was run through prepDat, must contain
  4352. * data for Q1, median, Q2, notch upper and notch lower
  4353. * @returns {string} A string in the proper format for a svg polygon
  4354. */
  4355. function makeNotchBox(boxLeft, notchLeft, boxCenter, dat) {
  4356. var boxPoints;
  4357. var y = centralTendency == 'mean' ? getMean(dat) : getQ2(dat); // if centralTendency is not specified, we still want to notch boxes on 'median'
  4358. if (notchBox) {
  4359. boxPoints = [
  4360. {x:boxCenter, y:yScale(getQ1(dat))},
  4361. {x:boxLeft, y:yScale(getQ1(dat))},
  4362. {x:boxLeft, y:yScale(getNl(dat))},
  4363. {x:notchLeft, y:yScale(y)},
  4364. {x:boxLeft, y:yScale(getNu(dat))},
  4365. {x:boxLeft, y:yScale(getQ3(dat))},
  4366. {x:boxCenter, y:yScale(getQ3(dat))},
  4367. ];
  4368. } else {
  4369. boxPoints = [
  4370. {x:boxCenter, y:yScale(getQ1(dat))},
  4371. {x:boxLeft, y:yScale(getQ1(dat))},
  4372. {x:boxLeft, y:yScale(y)}, // repeated point so that transition between notched/regular more smooth
  4373. {x:boxLeft, y:yScale(y)},
  4374. {x:boxLeft, y:yScale(y)}, // repeated point so that transition between notched/regular more smooth
  4375. {x:boxLeft, y:yScale(getQ3(dat))},
  4376. {x:boxCenter, y:yScale(getQ3(dat))},
  4377. ];
  4378. }
  4379. return boxPoints;
  4380. }
  4381. /**
  4382. * Given an x-axis group, return the available color groups within it
  4383. * provided that colorGroups is set, if not, x-axis group is returned
  4384. */
  4385. function getAvailableColorGroups(x) {
  4386. if (!colorGroup) return x;
  4387. var tmp = reformatDat.find(function(d) { return d.key == x });
  4388. return tmp.values.map(function(d) { return d.key }).sort(d3.ascending);
  4389. }
  4390. // return true if point is an outlier
  4391. function isOutlier(d) {
  4392. return (whiskerDef == 'iqr' && d.isOutlier) || (whiskerDef == 'stddev' && d.isOutlierStdDev)
  4393. }
  4394. //============================================================
  4395. // Private Variables
  4396. //------------------------------------------------------------
  4397. var allColorGroups = d3.set()
  4398. var yVScale = [], reformatDat, reformatDatFlat = [];
  4399. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  4400. var availableWidth, availableHeight;
  4401. function chart(selection) {
  4402. renderWatch.reset();
  4403. selection.each(function(data) {
  4404. availableWidth = width - margin.left - margin.right,
  4405. availableHeight = height - margin.top - margin.bottom;
  4406. container = d3.select(this);
  4407. nv.utils.initSVG(container);
  4408. // Setup y-scale so that beeswarm layout can use it in prepData()
  4409. yScale.domain(yDomain || d3.extent(data.map(function(d) { return getY(d)}))).nice()
  4410. .range(yRange || [availableHeight, 0]);
  4411. if (typeof reformatDat === 'undefined') reformatDat = prepData(data); // this prevents us from recalculating data all the time
  4412. // Setup x-scale
  4413. xScale.rangeBands(xRange || [0, availableWidth], 0.1)
  4414. .domain(xDomain || (colorGroup && !squash) ? allColorGroups : reformatDat.map(function(d) { return d.key }))
  4415. // Setup containers and skeleton of chart
  4416. var wrap = container.selectAll('g.nv-wrap').data([reformatDat]);
  4417. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap');
  4418. wrap.watchTransition(renderWatch, 'nv-wrap: wrap')
  4419. .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  4420. var areaEnter,
  4421. distroplots = wrap.selectAll('.nv-distroplot-x-group')
  4422. .data(function(d) { return d; });
  4423. // rebind new data
  4424. // we don't rebuild individual x-axis groups so that we can update transition them
  4425. // however the data associated with each x-axis group needs to be updated
  4426. // so we manually update it here
  4427. distroplots.each(function(d,i) {
  4428. d3.select(this).selectAll('line.nv-distroplot-middle').datum(d);
  4429. })
  4430. areaEnter = distroplots.enter()
  4431. .append('g')
  4432. .attr('class', 'nv-distroplot-x-group')
  4433. .style('stroke-opacity', 1e-6).style('fill-opacity', 1e-6)
  4434. .style('fill', function(d,i) { return getColor(d) || color(d,i) })
  4435. .style('stroke', function(d,i) { return getColor(d) || color(d,i) })
  4436. distroplots.exit().remove();
  4437. var rangeBand = function() { return xScale.rangeBand() };
  4438. var areaWidth = function() { return d3.min([maxBoxWidth,rangeBand() * 0.9]); };
  4439. var areaCenter = function() { return areaWidth()/2; };
  4440. var areaLeft = function() { return areaCenter() - areaWidth()/2; };
  4441. var areaRight = function() { return areaCenter() + areaWidth()/2; };
  4442. var tickLeft = function() { return areaCenter() - areaWidth()/5; };
  4443. var tickRight = function() { return areaCenter() + areaWidth()/5; };
  4444. areaEnter.attr('transform', function(d) {
  4445. return 'translate(' + (xScale(d.key) + (rangeBand() - areaWidth()) * 0.5) + ', 0)';
  4446. });
  4447. distroplots
  4448. .watchTransition(renderWatch, 'nv-distroplot-x-group: distroplots')
  4449. .style('stroke-opacity', 1)
  4450. .style('fill-opacity', 0.5)
  4451. .attr('transform', function(d) {
  4452. return 'translate(' + (xScale(d.key) + (rangeBand() - areaWidth()) * 0.5) + ', 0)';
  4453. });
  4454. // set range for violin scale
  4455. yVScale.map(function(d) { d.range([areaWidth()/2, 0]) });
  4456. // ----- add the SVG elements for each plot type -----
  4457. // scatter plot type
  4458. if (!plotType) {
  4459. showOnlyOutliers = false; // force all observations to be seen
  4460. if (!observationType) observationType = 'random'
  4461. }
  4462. // conditionally append whisker lines
  4463. areaEnter.each(function(d,i) {
  4464. var box = d3.select(this);
  4465. [getWl, getWh].forEach(function (f) {
  4466. var key = (f === getWl) ? 'low' : 'high';
  4467. box.append('line')
  4468. .style('opacity', function() { return !hideWhiskers ? '0' : '1' })
  4469. .attr('class', 'nv-distroplot-whisker nv-distroplot-' + key)
  4470. box.append('line')
  4471. .style('opacity', function() { return hideWhiskers ? '0' : '1' })
  4472. .attr('class', 'nv-distroplot-tick nv-distroplot-' + key)
  4473. });
  4474. });
  4475. // update whisker lines and ticks
  4476. [getWl, getWh].forEach(function (f) {
  4477. var key = (f === getWl) ? 'low' : 'high';
  4478. var endpoint = (f === getWl) ? getQ1 : getQ3;
  4479. distroplots.select('line.nv-distroplot-whisker.nv-distroplot-' + key)
  4480. .watchTransition(renderWatch, 'nv-distroplot-x-group: distroplots')
  4481. .attr('x1', areaCenter())
  4482. .attr('y1', function(d) { return plotType!='violin' ? yScale(f(d)) : yScale(getQ2(d)); })
  4483. .attr('x2', areaCenter())
  4484. .attr('y2', function(d) { return plotType=='box' ? yScale(endpoint(d)) : yScale(getQ2(d)); })
  4485. .style('opacity', function() { return hideWhiskers ? '0' : '1' })
  4486. distroplots.select('line.nv-distroplot-tick.nv-distroplot-' + key)
  4487. .watchTransition(renderWatch, 'nv-distroplot-x-group: distroplots')
  4488. .attr('x1', function(d) { return plotType!='violin' ? tickLeft() : areaCenter()} )
  4489. .attr('y1', function(d,i) { return plotType!='violin' ? yScale(f(d)) : yScale(getQ2(d)); })
  4490. .attr('x2', function(d) { return plotType!='violin' ? tickRight() : areaCenter()} )
  4491. .attr('y2', function(d,i) { return plotType!='violin' ? yScale(f(d)) : yScale(getQ2(d)); })
  4492. .style('opacity', function() { return hideWhiskers ? '0' : '1' })
  4493. });
  4494. [getWl, getWh].forEach(function (f) {
  4495. var key = (f === getWl) ? 'low' : 'high';
  4496. areaEnter.selectAll('.nv-distroplot-' + key)
  4497. .on('mouseover', function(d,i,j) {
  4498. d3.select(this.parentNode).selectAll('line.nv-distroplot-'+key).classed('hover',true);
  4499. dispatch.elementMouseover({
  4500. value: key == 'low' ? 'Lower whisker' : 'Upper whisker',
  4501. series: { key: f(d).toFixed(2), color: getColor(d) || color(d,j) },
  4502. e: d3.event
  4503. });
  4504. })
  4505. .on('mouseout', function(d,i,j) {
  4506. d3.select(this.parentNode).selectAll('line.nv-distroplot-'+key).classed('hover',false);
  4507. dispatch.elementMouseout({
  4508. value: key == 'low' ? 'Lower whisker' : 'Upper whisker',
  4509. series: { key: f(d).toFixed(2), color: getColor(d) || color(d,j) },
  4510. e: d3.event
  4511. });
  4512. })
  4513. .on('mousemove', function(d,i) {
  4514. dispatch.elementMousemove({e: d3.event});
  4515. });
  4516. });
  4517. // setup boxes as 4 parts: left-area, left-line, right-area, right-line,
  4518. // this way we can transition to a violin
  4519. areaEnter.each(function(d,i) {
  4520. var violin = d3.select(this);
  4521. ['left','right'].forEach(function(side) {
  4522. ['line','area'].forEach(function(d) {
  4523. violin.append('path')
  4524. .attr('class', 'nv-distribution-' + d + ' nv-distribution-' + side)
  4525. .attr("transform", "rotate(90,0,0) translate(0," + (side == 'left' ? -areaWidth() : 0) + ")" + (side == 'left' ? '' : ' scale(1,-1)')); // rotate violin
  4526. })
  4527. })
  4528. areaEnter.selectAll('.nv-distribution-line')
  4529. .style('fill','none')
  4530. areaEnter.selectAll('.nv-distribution-area')
  4531. .style('stroke','none')
  4532. .style('opacity',0.7)
  4533. });
  4534. // transitions
  4535. distroplots.each(function(d,i) {
  4536. var violin = d3.select(this);
  4537. var objData = plotType == 'box' ? makeNotchBox(areaLeft(), tickLeft(), areaCenter(), d) : d.values.kde;
  4538. violin.selectAll('path')
  4539. .datum(objData)
  4540. var tmpScale = yVScale[i];
  4541. var interp = plotType=='box' ? 'linear' : 'basis';
  4542. if (plotType == 'box' || plotType == 'violin') {
  4543. ['left','right'].forEach(function(side) {
  4544. // line
  4545. distroplots.selectAll('.nv-distribution-line.nv-distribution-' + side)
  4546. //.watchTransition(renderWatch, 'nv-distribution-line: distroplots') // disable transition for now because it's jaring
  4547. .attr("d", d3.svg.line()
  4548. .x(function(e) { return plotType=='box' ? e.y : yScale(e.x); })
  4549. .y(function(e) { return plotType=='box' ? e.x : tmpScale(e.y) })
  4550. .interpolate(interp)
  4551. )
  4552. .attr("transform", "rotate(90,0,0) translate(0," + (side == 'left' ? -areaWidth() : 0) + ")" + (side == 'left' ? '' : ' scale(1,-1)')) // rotate violin
  4553. .style('opacity', !plotType ? '0' : '1');
  4554. // area
  4555. distroplots.selectAll('.nv-distribution-area.nv-distribution-' + side)
  4556. //.watchTransition(renderWatch, 'nv-distribution-line: distroplots') // disable transition for now because it's jaring
  4557. .attr("d", d3.svg.area()
  4558. .x(function(e) { return plotType=='box' ? e.y : yScale(e.x); })
  4559. .y(function(e) { return plotType=='box' ? e.x : tmpScale(e.y) })
  4560. .y0(areaWidth()/2)
  4561. .interpolate(interp)
  4562. )
  4563. .attr("transform", "rotate(90,0,0) translate(0," + (side == 'left' ? -areaWidth() : 0) + ")" + (side == 'left' ? '' : ' scale(1,-1)')) // rotate violin
  4564. .style('opacity', !plotType ? '0' : '1');
  4565. })
  4566. } else { // scatter type, hide areas
  4567. distroplots.selectAll('.nv-distribution-area')
  4568. .watchTransition(renderWatch, 'nv-distribution-area: distroplots')
  4569. .style('opacity', !plotType ? '0' : '1');
  4570. distroplots.selectAll('.nv-distribution-line')
  4571. .watchTransition(renderWatch, 'nv-distribution-line: distroplots')
  4572. .style('opacity', !plotType ? '0' : '1');
  4573. }
  4574. })
  4575. // tooltip events
  4576. distroplots.selectAll('path')
  4577. .on('mouseover', function(d,i,j) {
  4578. d = d3.select(this.parentNode).datum(); // grab data from parent g
  4579. d3.select(this).classed('hover', true);
  4580. dispatch.elementMouseover({
  4581. key: d.key,
  4582. value: 'Group ' + d.key + ' stats',
  4583. series: [
  4584. { key: 'max', value: getMax(d).toFixed(2), color: getColor(d) || color(d,j) },
  4585. { key: 'Q3', value: getQ3(d).toFixed(2), color: getColor(d) || color(d,j) },
  4586. { key: 'Q2', value: getQ2(d).toFixed(2), color: getColor(d) || color(d,j) },
  4587. { key: 'Q1', value: getQ1(d).toFixed(2), color: getColor(d) || color(d,j) },
  4588. { key: 'min', value: getMin(d).toFixed(2), color: getColor(d) || color(d,j) },
  4589. { key: 'mean', value: getMean(d).toFixed(2), color: getColor(d) || color(d,j) },
  4590. { key: 'std. dev.', value: getDev(d).toFixed(2), color: getColor(d) || color(d,j) },
  4591. { key: 'count', value: d.values.count, color: getColor(d) || color(d,j) },
  4592. { key: 'num. outliers', value: d.values.num_outlier, color: getColor(d) || color(d,j) },
  4593. ],
  4594. data: d,
  4595. index: i,
  4596. e: d3.event
  4597. });
  4598. })
  4599. .on('mouseout', function(d,i,j) {
  4600. d3.select(this).classed('hover', false);
  4601. d = d3.select(this.parentNode).datum(); // grab data from parent g
  4602. dispatch.elementMouseout({
  4603. key: d.key,
  4604. value: 'Group ' + d.key + ' stats',
  4605. series: [
  4606. { key: 'max', value: getMax(d).toFixed(2), color: getColor(d) || color(d,j) },
  4607. { key: 'Q3', value: getQ3(d).toFixed(2), color: getColor(d) || color(d,j) },
  4608. { key: 'Q2', value: getQ2(d).toFixed(2), color: getColor(d) || color(d,j) },
  4609. { key: 'Q1', value: getQ1(d).toFixed(2), color: getColor(d) || color(d,j) },
  4610. { key: 'min', value: getMin(d).toFixed(2), color: getColor(d) || color(d,j) },
  4611. { key: 'mean', value: getMean(d).toFixed(2), color: getColor(d) || color(d,j) },
  4612. { key: 'std. dev.', value: getDev(d).toFixed(2), color: getColor(d) || color(d,j) },
  4613. { key: 'count', value: d.values.count, color: getColor(d) || color(d,j) },
  4614. { key: 'num. outliers', value: d.values.num_outlier, color: getColor(d) || color(d,j) },
  4615. ],
  4616. data: d,
  4617. index: i,
  4618. e: d3.event
  4619. });
  4620. })
  4621. .on('mousemove', function(d,i) {
  4622. dispatch.elementMousemove({e: d3.event});
  4623. });
  4624. // median/mean line
  4625. areaEnter.append('line')
  4626. .attr('class', function(d) { return 'nv-distroplot-middle'})
  4627. distroplots.selectAll('line.nv-distroplot-middle')
  4628. .watchTransition(renderWatch, 'nv-distroplot-x-group: distroplots line')
  4629. .attr('x1', notchBox ? tickLeft : plotType != 'violin' ? areaLeft : tickLeft())
  4630. .attr('y1', function(d,i,j) { return centralTendency == 'mean' ? yScale(getMean(d)) : yScale(getQ2(d)); })
  4631. .attr('x2', notchBox ? tickRight : plotType != 'violin' ? areaRight : tickRight())
  4632. .attr('y2', function(d,i) { return centralTendency == 'mean' ? yScale(getMean(d)) : yScale(getQ2(d)); })
  4633. .style('opacity', centralTendency ? '1' : '0');
  4634. // tooltip
  4635. distroplots.selectAll('.nv-distroplot-middle')
  4636. .on('mouseover', function(d,i,j) {
  4637. if (d3.select(this).style('opacity') == 0) return; // don't show tooltip for hidden lines
  4638. var fillColor = d3.select(this.parentNode).style('fill'); // color set by parent g fill
  4639. d3.select(this).classed('hover', true);
  4640. dispatch.elementMouseover({
  4641. value: centralTendency == 'mean' ? 'Mean' : 'Median',
  4642. series: { key: centralTendency == 'mean' ? getMean(d).toFixed(2) : getQ2(d).toFixed(2), color: fillColor },
  4643. e: d3.event
  4644. });
  4645. })
  4646. .on('mouseout', function(d,i,j) {
  4647. if (d3.select(this).style('opacity') == 0) return; // don't show tooltip for hidden lines
  4648. d3.select(this).classed('hover', false);
  4649. var fillColor = d3.select(this.parentNode).style('fill'); // color set by parent g fill
  4650. dispatch.elementMouseout({
  4651. value: centralTendency == 'mean' ? 'Mean' : 'Median',
  4652. series: { key: centralTendency == 'mean' ? getMean(d).toFixed(2) : getQ2(d).toFixed(2), color: fillColor },
  4653. e: d3.event
  4654. });
  4655. })
  4656. .on('mousemove', function(d,i) {
  4657. dispatch.elementMousemove({e: d3.event});
  4658. });
  4659. // setup observations
  4660. // create DOMs even if not requested (and hide them), so that
  4661. // we can do transitions on them
  4662. var obsWrap = distroplots.selectAll('g.nv-distroplot-observation')
  4663. .data(function(d) { return getValsObj(d) }, function(d) { return d.object_constancy; });
  4664. var obsGroup = obsWrap.enter()
  4665. .append('g')
  4666. .attr('class', 'nv-distroplot-observation')
  4667. obsGroup.append('circle')
  4668. .style({'opacity': 0})
  4669. obsGroup.append('line')
  4670. .style('stroke-width', 1)
  4671. .style({'stroke': d3.rgb(85, 85, 85), 'opacity': 0})
  4672. obsWrap.exit().remove();
  4673. obsWrap.attr('class', function(d) { return 'nv-distroplot-observation ' + (isOutlier(d) && plotType == 'box' ? 'nv-distroplot-outlier' : 'nv-distroplot-non-outlier')})
  4674. // transition observations
  4675. if (observationType == 'line') {
  4676. distroplots.selectAll('g.nv-distroplot-observation line')
  4677. .watchTransition(renderWatch, 'nv-distrolot-x-group: nv-distoplot-observation')
  4678. .attr("x1", tickLeft() + areaWidth()/4)
  4679. .attr("x2", tickRight() - areaWidth()/4)
  4680. .attr('y1', function(d) { return yScale(d.datum)})
  4681. .attr('y2', function(d) { return yScale(d.datum)});
  4682. } else {
  4683. distroplots.selectAll('g.nv-distroplot-observation circle')
  4684. .watchTransition(renderWatch, 'nv-distroplot: nv-distroplot-observation')
  4685. .attr('cy', function(d) { return yScale(d.datum); })
  4686. .attr('r', pointSize);
  4687. // NOTE: this update can be slow when re-sizing window when many point visible
  4688. // TODO: filter selection down to only visible points, no need to update x-position
  4689. // of the hidden points
  4690. distroplots.selectAll('g.nv-distroplot-observation circle')
  4691. .watchTransition(renderWatch, 'nv-distroplot: nv-distroplot-observation')
  4692. .attr('cx', function(d) { return observationType == 'swarm' ? d.x + areaWidth()/2 : observationType == 'random' ? areaWidth()/2 + d.randX * areaWidth()/2 : areaWidth()/2; })
  4693. }
  4694. // set opacity on outliers/non-outliers
  4695. // any circle/line entering has opacity 0
  4696. if (observationType !== false) { // observationType is False when hidding all circle/lines
  4697. if (!showOnlyOutliers) { // show all line/circle
  4698. distroplots.selectAll(observationType== 'line' ? 'line':'circle')
  4699. .watchTransition(renderWatch, 'nv-distroplot: nv-distroplot-observation')
  4700. .style('opacity',1)
  4701. } else { // show only outliers
  4702. distroplots.selectAll('.nv-distroplot-outlier '+ (observationType== 'line' ? 'line':'circle'))
  4703. .watchTransition(renderWatch, 'nv-distroplot: nv-distroplot-observation')
  4704. .style('opacity',1)
  4705. distroplots.selectAll('.nv-distroplot-non-outlier '+ (observationType== 'line' ? 'line':'circle'))
  4706. .watchTransition(renderWatch, 'nv-distroplot: nv-distroplot-observation')
  4707. .style('opacity',0)
  4708. }
  4709. }
  4710. // hide all other observations
  4711. distroplots.selectAll('.nv-distroplot-observation' + (observationType=='line'?' circle':' line'))
  4712. .watchTransition(renderWatch, 'nv-distroplot: nv-distoplot-observation')
  4713. .style('opacity',0)
  4714. // tooltip events for observations
  4715. distroplots.selectAll('.nv-distroplot-observation')
  4716. .on('mouseover', function(d,i,j) {
  4717. var pt = d3.select(this);
  4718. if (showOnlyOutliers && plotType == 'box' && !isOutlier(d)) return; // don't show tooltip for hidden observation
  4719. var fillColor = d3.select(this.parentNode).style('fill'); // color set by parent g fill
  4720. pt.classed('hover', true);
  4721. dispatch.elementMouseover({
  4722. value: (plotType == 'box' && isOutlier(d)) ? 'Outlier' : 'Observation',
  4723. series: { key: d.datum.toFixed(2), color: fillColor },
  4724. e: d3.event
  4725. });
  4726. })
  4727. .on('mouseout', function(d,i,j) {
  4728. var pt = d3.select(this);
  4729. var fillColor = d3.select(this.parentNode).style('fill'); // color set by parent g fill
  4730. pt.classed('hover', false);
  4731. dispatch.elementMouseout({
  4732. value: (plotType == 'box' && isOutlier(d)) ? 'Outlier' : 'Observation',
  4733. series: { key: d.datum.toFixed(2), color: fillColor },
  4734. e: d3.event
  4735. });
  4736. })
  4737. .on('mousemove', function(d,i) {
  4738. dispatch.elementMousemove({e: d3.event});
  4739. });
  4740. });
  4741. renderWatch.renderEnd('nv-distroplot-x-group immediate');
  4742. return chart;
  4743. }
  4744. //============================================================
  4745. // Expose Public Variables
  4746. //------------------------------------------------------------
  4747. chart.dispatch = dispatch;
  4748. chart.options = nv.utils.optionsFunc.bind(chart);
  4749. chart._options = Object.create({}, {
  4750. // simple options, just get/set the necessary values
  4751. width: {get: function(){return width;}, set: function(_){width=_;}},
  4752. height: {get: function(){return height;}, set: function(_){height=_;}},
  4753. maxBoxWidth: {get: function(){return maxBoxWidth;}, set: function(_){maxBoxWidth=_;}},
  4754. x: {get: function(){return getX;}, set: function(_){getX=_;}},
  4755. y: {get: function(){return getY;}, set: function(_){getY=_;}},
  4756. plotType: {get: function(){return plotType;}, set: function(_){plotType=_;}}, // plotType of background: 'box', 'violin' - default: 'box'
  4757. observationType: {get: function(){return observationType;}, set: function(_){observationType=_;}}, // type of observations to show: 'random', 'swarm', 'line', 'point' - default: false (don't show observations)
  4758. whiskerDef: {get: function(){return whiskerDef;}, set: function(_){whiskerDef=_;}}, // type of whisker to render: 'iqr', 'minmax', 'stddev' - default: iqr
  4759. notchBox: {get: function(){return notchBox;}, set: function(_){notchBox=_;}}, // bool whether to notch box
  4760. hideWhiskers: {get: function(){return hideWhiskers;}, set: function(_){hideWhiskers=_;}},
  4761. colorGroup: {get: function(){return colorGroup;}, set: function(_){colorGroup=_;}}, // data key to use to set color group of each x-category - default: don't group
  4762. centralTendency: {get: function(){return centralTendency;}, set: function(_){centralTendency=_;}}, // add a mean or median line to the data - default: don't show, must be one of 'mean' or 'median'
  4763. bandwidth: {get: function(){return bandwidth;}, set: function(_){bandwidth=_;}}, // bandwidth for kde calculation, can be float or str, if str, must be one of scott or silverman
  4764. clampViolin: {get: function(){return clampViolin;}, set: function(_){clampViolin=_;}},
  4765. resolution: {get: function(){return resolution;}, set: function(_){resolution=_;}}, // resolution for kde calculation, default 50
  4766. xScale: {get: function(){return xScale;}, set: function(_){xScale=_;}},
  4767. yScale: {get: function(){return yScale;}, set: function(_){yScale=_;}},
  4768. showOnlyOutliers: {get: function(){return showOnlyOutliers;}, set: function(_){showOnlyOutliers=_;}}, // show only outliers in box plot, default true
  4769. jitter: {get: function(){return jitter;}, set: function(_){jitter=_;}}, // faction of that jitter should take up in 'random' observationType, must be in range [0,1]; see jitterX(), default 0.7
  4770. squash: {get: function(){return squash;}, set: function(_){squash=_;}}, // whether to squash sparse distribution of color groups towards middle of x-axis position
  4771. pointSize: {get: function(){return pointSize;}, set: function(_){pointSize=_;}},
  4772. xDomain: {get: function(){return xDomain;}, set: function(_){xDomain=_;}},
  4773. yDomain: {get: function(){return yDomain;}, set: function(_){yDomain=_;}},
  4774. xRange: {get: function(){return xRange;}, set: function(_){xRange=_;}},
  4775. yRange: {get: function(){return yRange;}, set: function(_){yRange=_;}},
  4776. recalcData: {get: function() { reformatDat = prepData(container.datum()); } },
  4777. itemColor: {get: function(){return getColor;}, set: function(_){getColor=_;}},
  4778. id: {get: function(){return id;}, set: function(_){id=_;}},
  4779. // options that require extra logic in the setter
  4780. margin: {get: function(){return margin;}, set: function(_){
  4781. margin.top = _.top !== undefined ? _.top : margin.top;
  4782. margin.right = _.right !== undefined ? _.right : margin.right;
  4783. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  4784. margin.left = _.left !== undefined ? _.left : margin.left;
  4785. }},
  4786. color: {get: function(){return color;}, set: function(_){
  4787. color = nv.utils.getColor(_);
  4788. }},
  4789. duration: {get: function(){return duration;}, set: function(_){
  4790. duration = _;
  4791. renderWatch.reset(duration);
  4792. }}
  4793. });
  4794. nv.utils.initOptions(chart);
  4795. return chart;
  4796. };
  4797. nv.models.distroPlotChart = function() {
  4798. "use strict";
  4799. //============================================================
  4800. // Public Variables with Default Settings
  4801. //------------------------------------------------------------
  4802. var distroplot = nv.models.distroPlot(),
  4803. xAxis = nv.models.axis(),
  4804. yAxis = nv.models.axis()
  4805. var margin = {top: 25, right: 10, bottom: 40, left: 60},
  4806. width = null,
  4807. height = null,
  4808. color = nv.utils.getColor(),
  4809. showXAxis = true,
  4810. showYAxis = true,
  4811. rightAlignYAxis = false,
  4812. staggerLabels = false,
  4813. xLabel = false,
  4814. yLabel = false,
  4815. tooltip = nv.models.tooltip(),
  4816. x, y,
  4817. noData = 'No Data Available.',
  4818. dispatch = d3.dispatch('stateChange', 'beforeUpdate', 'renderEnd'),
  4819. duration = 500;
  4820. xAxis
  4821. .orient('bottom')
  4822. .showMaxMin(false)
  4823. .tickFormat(function(d) { return d })
  4824. ;
  4825. yAxis
  4826. .orient((rightAlignYAxis) ? 'right' : 'left')
  4827. .tickFormat(d3.format(',.1f'))
  4828. ;
  4829. tooltip.duration(0);
  4830. //============================================================
  4831. // Private Variables
  4832. //------------------------------------------------------------
  4833. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  4834. var colorGroup0, marginTop0 = margin.top, x0, y0, resolution0, bandwidth0, clampViolin0;
  4835. var dataCache;
  4836. // return true if data has changed somehow after
  4837. // an .update() was called
  4838. // works by comparing current data set to the
  4839. // one previously cached
  4840. // TODO - since we keep another version of the dataset
  4841. // around for comparison, it doubles the memory usage :(
  4842. function dataHasChanged(d) {
  4843. if (arraysEqual(d, dataCache)) {
  4844. return false;
  4845. } else {
  4846. dataCache = JSON.parse(JSON.stringify(d)) // deep copy
  4847. return true;
  4848. }
  4849. }
  4850. // return true if array of objects equivalent
  4851. function arraysEqual(arr1, arr2) {
  4852. if(arr1.length !== arr2.length) return false;
  4853. for(var i = arr1.length; i--;) {
  4854. if ('object_constancy' in arr1[i]) delete arr1[i].object_constancy
  4855. if ('object_constancy' in arr2[i]) delete arr2[i].object_constancy
  4856. if(!objectEquals(arr1[i], arr2[i])) {
  4857. return false;
  4858. }
  4859. }
  4860. return true;
  4861. }
  4862. // return true if objects are equivalent
  4863. function objectEquals(a, b) {
  4864. // Create arrays of property names
  4865. var aProps = Object.getOwnPropertyNames(a);
  4866. var bProps = Object.getOwnPropertyNames(b);
  4867. // If number of properties is different,
  4868. // objects are not equivalent
  4869. if (aProps.length != bProps.length) {
  4870. return false;
  4871. }
  4872. for (var i = 0; i < aProps.length; i++) {
  4873. var propName = aProps[i];
  4874. // If values of same property are not equal,
  4875. // objects are not equivalent
  4876. if (a[propName] !== b[propName]) {
  4877. return false;
  4878. }
  4879. }
  4880. return true;
  4881. }
  4882. function chart(selection) {
  4883. renderWatch.reset();
  4884. renderWatch.models(distroplot);
  4885. if (showXAxis) renderWatch.models(xAxis);
  4886. if (showYAxis) renderWatch.models(yAxis);
  4887. selection.each(function(data) {
  4888. var container = d3.select(this), that = this;
  4889. nv.utils.initSVG(container);
  4890. var availableWidth = (width || parseInt(container.style('width')) || 960) - margin.left - margin.right;
  4891. var availableHeight = (height || parseInt(container.style('height')) || 400) - margin.top - margin.bottom;
  4892. if (typeof dataCache === 'undefined') {
  4893. dataCache = JSON.parse(JSON.stringify(data)) // deep copy
  4894. }
  4895. chart.update = function() {
  4896. dispatch.beforeUpdate();
  4897. var opts = distroplot.options()
  4898. if (colorGroup0 !== opts.colorGroup() || // recalc data when any of the axis accessors are changed
  4899. x0 !== opts.x() ||
  4900. y0 !== opts.y() ||
  4901. bandwidth0 !== opts.bandwidth() ||
  4902. resolution0 !== opts.resolution() ||
  4903. clampViolin0 !== opts.clampViolin() ||
  4904. dataHasChanged(data)
  4905. ) {
  4906. distroplot.recalcData();
  4907. }
  4908. container.transition().duration(duration).call(chart);
  4909. };
  4910. chart.container = this;
  4911. if (typeof d3.beeswarm !== 'function' && chart.options().observationType() == 'swarm') {
  4912. var xPos = margin.left + availableWidth/2;
  4913. noData = 'Please include the library https://github.com/Kcnarf/d3-beeswarm to use "swarm".'
  4914. nv.utils.noData(chart, container);
  4915. return chart;
  4916. } else if (!data || !data.length) {
  4917. nv.utils.noData(chart, container);
  4918. return chart;
  4919. } else {
  4920. container.selectAll('.nv-noData').remove();
  4921. }
  4922. // Setup Scales
  4923. x = distroplot.xScale();
  4924. y = distroplot.yScale().clamp(true);
  4925. // Setup containers and skeleton of chart
  4926. var wrap = container.selectAll('g.nv-wrap.nv-distroPlot').data([data]);
  4927. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-distroPlot').append('g');
  4928. var defsEnter = gEnter.append('defs');
  4929. var g = wrap.select('g');
  4930. gEnter.append('g').attr('class', 'nv-x nv-axis');
  4931. gEnter.append('g').attr('class', 'nv-y nv-axis')
  4932. .append('g').attr('class', 'nv-zeroLine')
  4933. .append('line');
  4934. gEnter.append('g').attr('class', 'nv-distroWrap');
  4935. gEnter.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  4936. g.watchTransition(renderWatch, 'nv-wrap: wrap')
  4937. .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  4938. if (rightAlignYAxis) {
  4939. g.select('.nv-y.nv-axis')
  4940. .attr('transform', 'translate(' + availableWidth + ',0)');
  4941. }
  4942. // Main Chart Component(s)
  4943. distroplot.width(availableWidth).height(availableHeight);
  4944. var distroWrap = g.select('.nv-distroWrap')
  4945. .datum(data)
  4946. distroWrap.transition().call(distroplot);
  4947. defsEnter.append('clipPath')
  4948. .attr('id', 'nv-x-label-clip-' + distroplot.id())
  4949. .append('rect');
  4950. g.select('#nv-x-label-clip-' + distroplot.id() + ' rect')
  4951. .attr('width', x.rangeBand() * (staggerLabels ? 2 : 1))
  4952. .attr('height', 16)
  4953. .attr('x', -x.rangeBand() / (staggerLabels ? 1 : 2 ));
  4954. // Setup Axes
  4955. if (showXAxis) {
  4956. xAxis
  4957. .scale(x)
  4958. .ticks( nv.utils.calcTicksX(availableWidth/100, data) )
  4959. .tickSize(-availableHeight, 0);
  4960. g.select('.nv-x.nv-axis').attr('transform', 'translate(0,' + y.range()[0] + ')')
  4961. g.select('.nv-x.nv-axis').call(xAxis);
  4962. //g.select('.nv-x.nv-axis').select('.nv-axislabel')
  4963. // .style('font-size', d3.min([availableWidth * 0.05,20]) + 'px')
  4964. var xTicks = g.select('.nv-x.nv-axis').selectAll('g');
  4965. if (staggerLabels) {
  4966. xTicks
  4967. .selectAll('text')
  4968. .attr('transform', function(d,i,j) { return 'translate(0,' + (j % 2 === 0 ? '5' : '17') + ')' })
  4969. }
  4970. }
  4971. if (showYAxis) {
  4972. yAxis
  4973. .scale(y)
  4974. .ticks( Math.floor(availableHeight/36) ) // can't use nv.utils.calcTicksY with Object data
  4975. .tickSize( -availableWidth, 0);
  4976. g.select('.nv-y.nv-axis').call(yAxis);
  4977. //g.select('.nv-y.nv-axis').select('.nv-axislabel')
  4978. // .style('font-size', d3.min([availableHeight * 0.05,20]) + 'px')
  4979. }
  4980. // Zero line on chart bottom
  4981. g.select('.nv-zeroLine line')
  4982. .attr('x1',0)
  4983. .attr('x2',availableWidth)
  4984. .attr('y1', y(0))
  4985. .attr('y2', y(0))
  4986. ;
  4987. // store original values so that we can
  4988. // call 'recalcData()' if needed
  4989. colorGroup0 = distroplot.options().colorGroup();
  4990. x0 = distroplot.options().x();
  4991. y0 = distroplot.options().y();
  4992. bandwidth0 = distroplot.options().bandwidth();
  4993. resolution0 = distroplot.options().resolution();
  4994. clampViolin0 = distroplot.options().clampViolin();
  4995. //============================================================
  4996. // Event Handling/Dispatching (in chart's scope)
  4997. //------------------------------------------------------------
  4998. });
  4999. renderWatch.renderEnd('nv-distroplot chart immediate');
  5000. return chart;
  5001. }
  5002. //============================================================
  5003. // Event Handling/Dispatching (out of chart's scope)
  5004. //------------------------------------------------------------
  5005. distroplot.dispatch.on('elementMouseover.tooltip', function(evt) {
  5006. tooltip.data(evt).hidden(false);
  5007. });
  5008. distroplot.dispatch.on('elementMouseout.tooltip', function(evt) {
  5009. tooltip.data(evt).hidden(true);
  5010. });
  5011. distroplot.dispatch.on('elementMousemove.tooltip', function(evt) {
  5012. tooltip();
  5013. });
  5014. //============================================================
  5015. // Expose Public Variables
  5016. //------------------------------------------------------------
  5017. chart.dispatch = dispatch;
  5018. chart.distroplot = distroplot;
  5019. chart.xAxis = xAxis;
  5020. chart.yAxis = yAxis;
  5021. chart.tooltip = tooltip;
  5022. chart.options = nv.utils.optionsFunc.bind(chart);
  5023. chart._options = Object.create({}, {
  5024. // simple options, just get/set the necessary values
  5025. width: {get: function(){return width;}, set: function(_){width=_;}},
  5026. height: {get: function(){return height;}, set: function(_){height=_;}},
  5027. staggerLabels: {get: function(){return staggerLabels;}, set: function(_){staggerLabels=_;}},
  5028. showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}},
  5029. showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}},
  5030. tooltipContent: {get: function(){return tooltip;}, set: function(_){tooltip=_;}},
  5031. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  5032. defaultState: {get: function(){return defaultState;}, set: function(_){defaultState=_;}},
  5033. // options that require extra logic in the setter
  5034. margin: {get: function(){return margin;}, set: function(_){
  5035. margin.top = _.top !== undefined ? _.top : margin.top;
  5036. margin.right = _.right !== undefined ? _.right : margin.right;
  5037. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  5038. margin.left = _.left !== undefined ? _.left : margin.left;
  5039. }},
  5040. duration: {get: function(){return duration;}, set: function(_){
  5041. duration = _;
  5042. renderWatch.reset(duration);
  5043. distroplot.duration(duration);
  5044. xAxis.duration(duration);
  5045. yAxis.duration(duration);
  5046. }},
  5047. color: {get: function(){return color;}, set: function(_){
  5048. color = nv.utils.getColor(_);
  5049. distroplot.color(color);
  5050. }},
  5051. rightAlignYAxis: {get: function(){return rightAlignYAxis;}, set: function(_){
  5052. rightAlignYAxis = _;
  5053. yAxis.orient( (_) ? 'right' : 'left');
  5054. }},
  5055. xLabel: {get: function(){return xLabel;}, set: function(_){
  5056. xLabel=_;
  5057. xAxis.axisLabel(xLabel);
  5058. }},
  5059. yLabel: {get: function(){return yLabel;}, set: function(_){
  5060. yLabel=_;
  5061. yAxis.axisLabel(yLabel);
  5062. }},
  5063. });
  5064. nv.utils.inheritOptions(chart, distroplot);
  5065. nv.utils.initOptions(chart);
  5066. return chart;
  5067. }
  5068. nv.models.focus = function(content) {
  5069. "use strict";
  5070. //============================================================
  5071. // Public Variables with Default Settings
  5072. //------------------------------------------------------------
  5073. var content = content || nv.models.line()
  5074. , xAxis = nv.models.axis()
  5075. , yAxis = nv.models.axis()
  5076. , brush = d3.svg.brush()
  5077. ;
  5078. var margin = {top: 10, right: 0, bottom: 30, left: 0}
  5079. , color = nv.utils.defaultColor()
  5080. , width = null
  5081. , height = 70
  5082. , showXAxis = true
  5083. , showYAxis = false
  5084. , rightAlignYAxis = false
  5085. , ticks = null
  5086. , x
  5087. , y
  5088. , brushExtent = null
  5089. , duration = 250
  5090. , dispatch = d3.dispatch('brush', 'onBrush', 'renderEnd')
  5091. , syncBrushing = true
  5092. ;
  5093. content.interactive(false);
  5094. content.pointActive(function(d) { return false; });
  5095. //============================================================
  5096. // Private Variables
  5097. //------------------------------------------------------------
  5098. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  5099. function chart(selection) {
  5100. renderWatch.reset();
  5101. renderWatch.models(content);
  5102. if (showXAxis) renderWatch.models(xAxis);
  5103. if (showYAxis) renderWatch.models(yAxis);
  5104. selection.each(function(data) {
  5105. var container = d3.select(this);
  5106. nv.utils.initSVG(container);
  5107. var availableWidth = nv.utils.availableWidth(width, container, margin),
  5108. availableHeight = height - margin.top - margin.bottom;
  5109. chart.update = function() {
  5110. if( duration === 0 ) {
  5111. container.call( chart );
  5112. } else {
  5113. container.transition().duration(duration).call(chart);
  5114. }
  5115. };
  5116. chart.container = this;
  5117. // Setup Scales
  5118. x = content.xScale();
  5119. y = content.yScale();
  5120. // Setup containers and skeleton of chart
  5121. var wrap = container.selectAll('g.nv-focus').data([data]);
  5122. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-focus').append('g');
  5123. var g = wrap.select('g');
  5124. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  5125. gEnter.append('g').attr('class', 'nv-background').append('rect');
  5126. gEnter.append('g').attr('class', 'nv-x nv-axis');
  5127. gEnter.append('g').attr('class', 'nv-y nv-axis');
  5128. gEnter.append('g').attr('class', 'nv-contentWrap');
  5129. gEnter.append('g').attr('class', 'nv-brushBackground');
  5130. gEnter.append('g').attr('class', 'nv-x nv-brush');
  5131. if (rightAlignYAxis) {
  5132. g.select(".nv-y.nv-axis")
  5133. .attr("transform", "translate(" + availableWidth + ",0)");
  5134. }
  5135. g.select('.nv-background rect')
  5136. .attr('width', availableWidth)
  5137. .attr('height', availableHeight);
  5138. content
  5139. .width(availableWidth)
  5140. .height(availableHeight)
  5141. .color(data.map(function(d,i) {
  5142. return d.color || color(d, i);
  5143. }).filter(function(d,i) { return !data[i].disabled; }));
  5144. var contentWrap = g.select('.nv-contentWrap')
  5145. .datum(data.filter(function(d) { return !d.disabled; }));
  5146. d3.transition(contentWrap).call(content);
  5147. // Setup Brush
  5148. brush
  5149. .x(x)
  5150. .on('brush', function() {
  5151. onBrush(syncBrushing);
  5152. });
  5153. brush.on('brushend', function () {
  5154. if (!syncBrushing) {
  5155. dispatch.onBrush(brush.empty() ? x.domain() : brush.extent());
  5156. }
  5157. });
  5158. if (brushExtent) brush.extent(brushExtent);
  5159. var brushBG = g.select('.nv-brushBackground').selectAll('g')
  5160. .data([brushExtent || brush.extent()]);
  5161. var brushBGenter = brushBG.enter()
  5162. .append('g');
  5163. brushBGenter.append('rect')
  5164. .attr('class', 'left')
  5165. .attr('x', 0)
  5166. .attr('y', 0)
  5167. .attr('height', availableHeight);
  5168. brushBGenter.append('rect')
  5169. .attr('class', 'right')
  5170. .attr('x', 0)
  5171. .attr('y', 0)
  5172. .attr('height', availableHeight);
  5173. var gBrush = g.select('.nv-x.nv-brush')
  5174. .call(brush);
  5175. gBrush.selectAll('rect')
  5176. .attr('height', availableHeight);
  5177. gBrush.selectAll('.resize').append('path').attr('d', resizePath);
  5178. onBrush(true);
  5179. g.select('.nv-background rect')
  5180. .attr('width', availableWidth)
  5181. .attr('height', availableHeight);
  5182. if (showXAxis) {
  5183. xAxis.scale(x)
  5184. ._ticks( nv.utils.calcTicksX(availableWidth/100, data) )
  5185. .tickSize(-availableHeight, 0);
  5186. g.select('.nv-x.nv-axis')
  5187. .attr('transform', 'translate(0,' + y.range()[0] + ')');
  5188. d3.transition(g.select('.nv-x.nv-axis'))
  5189. .call(xAxis);
  5190. }
  5191. if (showYAxis) {
  5192. yAxis
  5193. .scale(y)
  5194. ._ticks( nv.utils.calcTicksY(availableHeight/36, data) )
  5195. .tickSize( -availableWidth, 0);
  5196. d3.transition(g.select('.nv-y.nv-axis'))
  5197. .call(yAxis);
  5198. }
  5199. g.select('.nv-x.nv-axis')
  5200. .attr('transform', 'translate(0,' + y.range()[0] + ')');
  5201. //============================================================
  5202. // Event Handling/Dispatching (in chart's scope)
  5203. //------------------------------------------------------------
  5204. //============================================================
  5205. // Functions
  5206. //------------------------------------------------------------
  5207. // Taken from crossfilter (http://square.github.com/crossfilter/)
  5208. function resizePath(d) {
  5209. var e = +(d == 'e'),
  5210. x = e ? 1 : -1,
  5211. y = availableHeight / 3;
  5212. return 'M' + (0.5 * x) + ',' + y
  5213. + 'A6,6 0 0 ' + e + ' ' + (6.5 * x) + ',' + (y + 6)
  5214. + 'V' + (2 * y - 6)
  5215. + 'A6,6 0 0 ' + e + ' ' + (0.5 * x) + ',' + (2 * y)
  5216. + 'Z'
  5217. + 'M' + (2.5 * x) + ',' + (y + 8)
  5218. + 'V' + (2 * y - 8)
  5219. + 'M' + (4.5 * x) + ',' + (y + 8)
  5220. + 'V' + (2 * y - 8);
  5221. }
  5222. function updateBrushBG() {
  5223. if (!brush.empty()) brush.extent(brushExtent);
  5224. brushBG
  5225. .data([brush.empty() ? x.domain() : brushExtent])
  5226. .each(function(d,i) {
  5227. var leftWidth = x(d[0]) - x.range()[0],
  5228. rightWidth = availableWidth - x(d[1]);
  5229. d3.select(this).select('.left')
  5230. .attr('width', leftWidth < 0 ? 0 : leftWidth);
  5231. d3.select(this).select('.right')
  5232. .attr('x', x(d[1]))
  5233. .attr('width', rightWidth < 0 ? 0 : rightWidth);
  5234. });
  5235. }
  5236. function onBrush(shouldDispatch) {
  5237. brushExtent = brush.empty() ? null : brush.extent();
  5238. var extent = brush.empty() ? x.domain() : brush.extent();
  5239. dispatch.brush({extent: extent, brush: brush});
  5240. updateBrushBG();
  5241. if (shouldDispatch) {
  5242. dispatch.onBrush(extent);
  5243. }
  5244. }
  5245. });
  5246. renderWatch.renderEnd('focus immediate');
  5247. return chart;
  5248. }
  5249. //============================================================
  5250. // Event Handling/Dispatching (out of chart's scope)
  5251. //------------------------------------------------------------
  5252. //============================================================
  5253. // Expose Public Variables
  5254. //------------------------------------------------------------
  5255. // expose chart's sub-components
  5256. chart.dispatch = dispatch;
  5257. chart.content = content;
  5258. chart.brush = brush;
  5259. chart.xAxis = xAxis;
  5260. chart.yAxis = yAxis;
  5261. chart.options = nv.utils.optionsFunc.bind(chart);
  5262. chart._options = Object.create({}, {
  5263. // simple options, just get/set the necessary values
  5264. width: {get: function(){return width;}, set: function(_){width=_;}},
  5265. height: {get: function(){return height;}, set: function(_){height=_;}},
  5266. showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}},
  5267. showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}},
  5268. brushExtent: {get: function(){return brushExtent;}, set: function(_){brushExtent=_;}},
  5269. syncBrushing: {get: function(){return syncBrushing;}, set: function(_){syncBrushing=_;}},
  5270. // options that require extra logic in the setter
  5271. margin: {get: function(){return margin;}, set: function(_){
  5272. margin.top = _.top !== undefined ? _.top : margin.top;
  5273. margin.right = _.right !== undefined ? _.right : margin.right;
  5274. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  5275. margin.left = _.left !== undefined ? _.left : margin.left;
  5276. }},
  5277. duration: {get: function(){return duration;}, set: function(_){
  5278. duration = _;
  5279. renderWatch.reset(duration);
  5280. content.duration(duration);
  5281. xAxis.duration(duration);
  5282. yAxis.duration(duration);
  5283. }},
  5284. color: {get: function(){return color;}, set: function(_){
  5285. color = nv.utils.getColor(_);
  5286. content.color(color);
  5287. }},
  5288. interpolate: {get: function(){return content.interpolate();}, set: function(_){
  5289. content.interpolate(_);
  5290. }},
  5291. xTickFormat: {get: function(){return xAxis.tickFormat();}, set: function(_){
  5292. xAxis.tickFormat(_);
  5293. }},
  5294. yTickFormat: {get: function(){return yAxis.tickFormat();}, set: function(_){
  5295. yAxis.tickFormat(_);
  5296. }},
  5297. x: {get: function(){return content.x();}, set: function(_){
  5298. content.x(_);
  5299. }},
  5300. y: {get: function(){return content.y();}, set: function(_){
  5301. content.y(_);
  5302. }},
  5303. rightAlignYAxis: {get: function(){return rightAlignYAxis;}, set: function(_){
  5304. rightAlignYAxis = _;
  5305. yAxis.orient( rightAlignYAxis ? 'right' : 'left');
  5306. }}
  5307. });
  5308. nv.utils.inheritOptions(chart, content);
  5309. nv.utils.initOptions(chart);
  5310. return chart;
  5311. };
  5312. nv.models.forceDirectedGraph = function() {
  5313. "use strict";
  5314. //============================================================
  5315. // Public Variables with Default Settings
  5316. //------------------------------------------------------------
  5317. var margin = {top: 2, right: 0, bottom: 2, left: 0}
  5318. , width = 400
  5319. , height = 32
  5320. , container = null
  5321. , dispatch = d3.dispatch('renderEnd')
  5322. , color = nv.utils.getColor(['#000'])
  5323. , tooltip = nv.models.tooltip()
  5324. , noData = null
  5325. // Force directed graph specific parameters [default values]
  5326. , linkStrength = 0.1
  5327. , friction = 0.9
  5328. , linkDist = 30
  5329. , charge = -120
  5330. , gravity = 0.1
  5331. , theta = 0.8
  5332. , alpha = 0.1
  5333. , radius = 5
  5334. // These functions allow to add extra attributes to ndes and links
  5335. ,nodeExtras = function(nodes) { /* Do nothing */ }
  5336. ,linkExtras = function(links) { /* Do nothing */ }
  5337. , getX=d3.functor(0.0)
  5338. , getY=d3.functor(0.0)
  5339. ;
  5340. //============================================================
  5341. // Private Variables
  5342. //------------------------------------------------------------
  5343. var renderWatch = nv.utils.renderWatch(dispatch);
  5344. function chart(selection) {
  5345. renderWatch.reset();
  5346. selection.each(function(data) {
  5347. container = d3.select(this);
  5348. nv.utils.initSVG(container);
  5349. var availableWidth = nv.utils.availableWidth(width, container, margin),
  5350. availableHeight = nv.utils.availableHeight(height, container, margin);
  5351. container
  5352. .attr("width", availableWidth)
  5353. .attr("height", availableHeight);
  5354. // Display No Data message if there's nothing to show.
  5355. if (!data || !data.links || !data.nodes) {
  5356. nv.utils.noData(chart, container)
  5357. return chart;
  5358. } else {
  5359. container.selectAll('.nv-noData').remove();
  5360. }
  5361. container.selectAll('*').remove();
  5362. // Collect names of all fields in the nodes
  5363. var nodeFieldSet = new Set();
  5364. data.nodes.forEach(function(node) {
  5365. var keys = Object.keys(node);
  5366. keys.forEach(function(key) {
  5367. nodeFieldSet.add(key);
  5368. });
  5369. });
  5370. var force = d3.layout.force()
  5371. .nodes(data.nodes)
  5372. .links(data.links)
  5373. .size([availableWidth, availableHeight])
  5374. .linkStrength(linkStrength)
  5375. .friction(friction)
  5376. .linkDistance(linkDist)
  5377. .charge(charge)
  5378. .gravity(gravity)
  5379. .theta(theta)
  5380. .alpha(alpha)
  5381. .start();
  5382. var link = container.selectAll(".link")
  5383. .data(data.links)
  5384. .enter().append("line")
  5385. .attr("class", "nv-force-link")
  5386. .style("stroke-width", function(d) { return Math.sqrt(d.value); });
  5387. var node = container.selectAll(".node")
  5388. .data(data.nodes)
  5389. .enter()
  5390. .append("g")
  5391. .attr("class", "nv-force-node")
  5392. .call(force.drag);
  5393. node
  5394. .append("circle")
  5395. .attr("r", radius)
  5396. .style("fill", function(d) { return color(d) } )
  5397. .on("mouseover", function(evt) {
  5398. container.select('.nv-series-' + evt.seriesIndex + ' .nv-distx-' + evt.pointIndex)
  5399. .attr('y1', evt.py);
  5400. container.select('.nv-series-' + evt.seriesIndex + ' .nv-disty-' + evt.pointIndex)
  5401. .attr('x2', evt.px);
  5402. // Add 'series' object to
  5403. var nodeColor = color(evt);
  5404. evt.series = [];
  5405. nodeFieldSet.forEach(function(field) {
  5406. evt.series.push({
  5407. color: nodeColor,
  5408. key: field,
  5409. value: evt[field]
  5410. });
  5411. });
  5412. tooltip.data(evt).hidden(false);
  5413. })
  5414. .on("mouseout", function(d) {
  5415. tooltip.hidden(true);
  5416. });
  5417. tooltip.headerFormatter(function(d) {return "Node";});
  5418. // Apply extra attributes to nodes and links (if any)
  5419. linkExtras(link);
  5420. nodeExtras(node);
  5421. force.on("tick", function() {
  5422. link.attr("x1", function(d) { return d.source.x; })
  5423. .attr("y1", function(d) { return d.source.y; })
  5424. .attr("x2", function(d) { return d.target.x; })
  5425. .attr("y2", function(d) { return d.target.y; });
  5426. node.attr("transform", function(d) {
  5427. return "translate(" + d.x + ", " + d.y + ")";
  5428. });
  5429. });
  5430. });
  5431. return chart;
  5432. }
  5433. //============================================================
  5434. // Expose Public Variables
  5435. //------------------------------------------------------------
  5436. chart.options = nv.utils.optionsFunc.bind(chart);
  5437. chart._options = Object.create({}, {
  5438. // simple options, just get/set the necessary values
  5439. width: {get: function(){return width;}, set: function(_){width=_;}},
  5440. height: {get: function(){return height;}, set: function(_){height=_;}},
  5441. // Force directed graph specific parameters
  5442. linkStrength:{get: function(){return linkStrength;}, set: function(_){linkStrength=_;}},
  5443. friction: {get: function(){return friction;}, set: function(_){friction=_;}},
  5444. linkDist: {get: function(){return linkDist;}, set: function(_){linkDist=_;}},
  5445. charge: {get: function(){return charge;}, set: function(_){charge=_;}},
  5446. gravity: {get: function(){return gravity;}, set: function(_){gravity=_;}},
  5447. theta: {get: function(){return theta;}, set: function(_){theta=_;}},
  5448. alpha: {get: function(){return alpha;}, set: function(_){alpha=_;}},
  5449. radius: {get: function(){return radius;}, set: function(_){radius=_;}},
  5450. //functor options
  5451. x: {get: function(){return getX;}, set: function(_){getX=d3.functor(_);}},
  5452. y: {get: function(){return getY;}, set: function(_){getY=d3.functor(_);}},
  5453. // options that require extra logic in the setter
  5454. margin: {get: function(){return margin;}, set: function(_){
  5455. margin.top = _.top !== undefined ? _.top : margin.top;
  5456. margin.right = _.right !== undefined ? _.right : margin.right;
  5457. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  5458. margin.left = _.left !== undefined ? _.left : margin.left;
  5459. }},
  5460. color: {get: function(){return color;}, set: function(_){
  5461. color = nv.utils.getColor(_);
  5462. }},
  5463. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  5464. nodeExtras: {get: function(){return nodeExtras;}, set: function(_){
  5465. nodeExtras = _;
  5466. }},
  5467. linkExtras: {get: function(){return linkExtras;}, set: function(_){
  5468. linkExtras = _;
  5469. }}
  5470. });
  5471. chart.dispatch = dispatch;
  5472. chart.tooltip = tooltip;
  5473. nv.utils.initOptions(chart);
  5474. return chart;
  5475. };
  5476. nv.models.furiousLegend = function() {
  5477. "use strict";
  5478. //============================================================
  5479. // Public Variables with Default Settings
  5480. //------------------------------------------------------------
  5481. var margin = {top: 5, right: 0, bottom: 5, left: 0}
  5482. , width = 400
  5483. , height = 20
  5484. , getKey = function(d) { return d.key }
  5485. , keyFormatter = function (d) { return d }
  5486. , color = nv.utils.getColor()
  5487. , maxKeyLength = 20 //default value for key lengths
  5488. , align = true
  5489. , padding = 28 //define how much space between legend items. - recommend 32 for furious version
  5490. , rightAlign = true
  5491. , updateState = true //If true, legend will update data.disabled and trigger a 'stateChange' dispatch.
  5492. , radioButtonMode = false //If true, clicking legend items will cause it to behave like a radio button. (only one can be selected at a time)
  5493. , expanded = false
  5494. , dispatch = d3.dispatch('legendClick', 'legendDblclick', 'legendMouseover', 'legendMouseout', 'stateChange')
  5495. , vers = 'classic' //Options are "classic" and "furious"
  5496. ;
  5497. function chart(selection) {
  5498. selection.each(function(data) {
  5499. var availableWidth = width - margin.left - margin.right,
  5500. container = d3.select(this);
  5501. nv.utils.initSVG(container);
  5502. // Setup containers and skeleton of chart
  5503. var wrap = container.selectAll('g.nv-legend').data([data]);
  5504. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-legend').append('g');
  5505. var g = wrap.select('g');
  5506. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  5507. var series = g.selectAll('.nv-series')
  5508. .data(function(d) {
  5509. if(vers != 'furious') return d;
  5510. return d.filter(function(n) {
  5511. return expanded ? true : !n.disengaged;
  5512. });
  5513. });
  5514. var seriesEnter = series.enter().append('g').attr('class', 'nv-series')
  5515. var seriesShape;
  5516. if(vers == 'classic') {
  5517. seriesEnter.append('circle')
  5518. .style('stroke-width', 2)
  5519. .attr('class','nv-legend-symbol')
  5520. .attr('r', 5);
  5521. seriesShape = series.select('circle');
  5522. } else if (vers == 'furious') {
  5523. seriesEnter.append('rect')
  5524. .style('stroke-width', 2)
  5525. .attr('class','nv-legend-symbol')
  5526. .attr('rx', 3)
  5527. .attr('ry', 3);
  5528. seriesShape = series.select('rect');
  5529. seriesEnter.append('g')
  5530. .attr('class', 'nv-check-box')
  5531. .property('innerHTML','<path d="M0.5,5 L22.5,5 L22.5,26.5 L0.5,26.5 L0.5,5 Z" class="nv-box"></path><path d="M5.5,12.8618467 L11.9185089,19.2803556 L31,0.198864511" class="nv-check"></path>')
  5532. .attr('transform', 'translate(-10,-8)scale(0.5)');
  5533. var seriesCheckbox = series.select('.nv-check-box');
  5534. seriesCheckbox.each(function(d,i) {
  5535. d3.select(this).selectAll('path')
  5536. .attr('stroke', setTextColor(d,i));
  5537. });
  5538. }
  5539. seriesEnter.append('text')
  5540. .attr('text-anchor', 'start')
  5541. .attr('class','nv-legend-text')
  5542. .attr('dy', '.32em')
  5543. .attr('dx', '8');
  5544. var seriesText = series.select('text.nv-legend-text');
  5545. series
  5546. .on('mouseover', function(d,i) {
  5547. dispatch.legendMouseover(d,i); //TODO: Make consistent with other event objects
  5548. })
  5549. .on('mouseout', function(d,i) {
  5550. dispatch.legendMouseout(d,i);
  5551. })
  5552. .on('click', function(d,i) {
  5553. dispatch.legendClick(d,i);
  5554. // make sure we re-get data in case it was modified
  5555. var data = series.data();
  5556. if (updateState) {
  5557. if(vers =='classic') {
  5558. if (radioButtonMode) {
  5559. //Radio button mode: set every series to disabled,
  5560. // and enable the clicked series.
  5561. data.forEach(function(series) { series.disabled = true});
  5562. d.disabled = false;
  5563. }
  5564. else {
  5565. d.disabled = !d.disabled;
  5566. if (data.every(function(series) { return series.disabled})) {
  5567. //the default behavior of NVD3 legends is, if every single series
  5568. // is disabled, turn all series' back on.
  5569. data.forEach(function(series) { series.disabled = false});
  5570. }
  5571. }
  5572. } else if(vers == 'furious') {
  5573. if(expanded) {
  5574. d.disengaged = !d.disengaged;
  5575. d.userDisabled = d.userDisabled == undefined ? !!d.disabled : d.userDisabled;
  5576. d.disabled = d.disengaged || d.userDisabled;
  5577. } else if (!expanded) {
  5578. d.disabled = !d.disabled;
  5579. d.userDisabled = d.disabled;
  5580. var engaged = data.filter(function(d) { return !d.disengaged; });
  5581. if (engaged.every(function(series) { return series.userDisabled })) {
  5582. //the default behavior of NVD3 legends is, if every single series
  5583. // is disabled, turn all series' back on.
  5584. data.forEach(function(series) {
  5585. series.disabled = series.userDisabled = false;
  5586. });
  5587. }
  5588. }
  5589. }
  5590. dispatch.stateChange({
  5591. disabled: data.map(function(d) { return !!d.disabled }),
  5592. disengaged: data.map(function(d) { return !!d.disengaged })
  5593. });
  5594. }
  5595. })
  5596. .on('dblclick', function(d,i) {
  5597. if(vers == 'furious' && expanded) return;
  5598. dispatch.legendDblclick(d,i);
  5599. if (updateState) {
  5600. // make sure we re-get data in case it was modified
  5601. var data = series.data();
  5602. //the default behavior of NVD3 legends, when double clicking one,
  5603. // is to set all other series' to false, and make the double clicked series enabled.
  5604. data.forEach(function(series) {
  5605. series.disabled = true;
  5606. if(vers == 'furious') series.userDisabled = series.disabled;
  5607. });
  5608. d.disabled = false;
  5609. if(vers == 'furious') d.userDisabled = d.disabled;
  5610. dispatch.stateChange({
  5611. disabled: data.map(function(d) { return !!d.disabled })
  5612. });
  5613. }
  5614. });
  5615. series.classed('nv-disabled', function(d) { return d.userDisabled });
  5616. series.exit().remove();
  5617. seriesText
  5618. .attr('fill', setTextColor)
  5619. .text(function (d) { return keyFormatter(getKey(d)) });
  5620. //TODO: implement fixed-width and max-width options (max-width is especially useful with the align option)
  5621. // NEW ALIGNING CODE, TODO: clean up
  5622. var versPadding;
  5623. switch(vers) {
  5624. case 'furious' :
  5625. versPadding = 23;
  5626. break;
  5627. case 'classic' :
  5628. versPadding = 20;
  5629. }
  5630. if (align) {
  5631. var seriesWidths = [];
  5632. series.each(function(d,i) {
  5633. var legendText;
  5634. if (keyFormatter(getKey(d)) && keyFormatter(getKey(d)).length > maxKeyLength) {
  5635. var trimmedKey = keyFormatter(getKey(d)).substring(0, maxKeyLength);
  5636. legendText = d3.select(this).select('text').text(trimmedKey + "...");
  5637. d3.select(this).append("svg:title").text(keyFormatter(getKey(d)));
  5638. } else {
  5639. legendText = d3.select(this).select('text');
  5640. }
  5641. var nodeTextLength;
  5642. try {
  5643. nodeTextLength = legendText.node().getComputedTextLength();
  5644. // If the legendText is display:none'd (nodeTextLength == 0), simulate an error so we approximate, instead
  5645. if(nodeTextLength <= 0) throw Error();
  5646. }
  5647. catch(e) {
  5648. nodeTextLength = nv.utils.calcApproxTextWidth(legendText);
  5649. }
  5650. seriesWidths.push(nodeTextLength + padding);
  5651. });
  5652. var seriesPerRow = 0;
  5653. var legendWidth = 0;
  5654. var columnWidths = [];
  5655. while ( legendWidth < availableWidth && seriesPerRow < seriesWidths.length) {
  5656. columnWidths[seriesPerRow] = seriesWidths[seriesPerRow];
  5657. legendWidth += seriesWidths[seriesPerRow++];
  5658. }
  5659. if (seriesPerRow === 0) seriesPerRow = 1; //minimum of one series per row
  5660. while ( legendWidth > availableWidth && seriesPerRow > 1 ) {
  5661. columnWidths = [];
  5662. seriesPerRow--;
  5663. for (var k = 0; k < seriesWidths.length; k++) {
  5664. if (seriesWidths[k] > (columnWidths[k % seriesPerRow] || 0) )
  5665. columnWidths[k % seriesPerRow] = seriesWidths[k];
  5666. }
  5667. legendWidth = columnWidths.reduce(function(prev, cur, index, array) {
  5668. return prev + cur;
  5669. });
  5670. }
  5671. var xPositions = [];
  5672. for (var i = 0, curX = 0; i < seriesPerRow; i++) {
  5673. xPositions[i] = curX;
  5674. curX += columnWidths[i];
  5675. }
  5676. series
  5677. .attr('transform', function(d, i) {
  5678. return 'translate(' + xPositions[i % seriesPerRow] + ',' + (5 + Math.floor(i / seriesPerRow) * versPadding) + ')';
  5679. });
  5680. //position legend as far right as possible within the total width
  5681. if (rightAlign) {
  5682. g.attr('transform', 'translate(' + (width - margin.right - legendWidth) + ',' + margin.top + ')');
  5683. }
  5684. else {
  5685. g.attr('transform', 'translate(0' + ',' + margin.top + ')');
  5686. }
  5687. height = margin.top + margin.bottom + (Math.ceil(seriesWidths.length / seriesPerRow) * versPadding);
  5688. } else {
  5689. var ypos = 5,
  5690. newxpos = 5,
  5691. maxwidth = 0,
  5692. xpos;
  5693. series
  5694. .attr('transform', function(d, i) {
  5695. var length = d3.select(this).select('text').node().getComputedTextLength() + padding;
  5696. xpos = newxpos;
  5697. if (width < margin.left + margin.right + xpos + length) {
  5698. newxpos = xpos = 5;
  5699. ypos += versPadding;
  5700. }
  5701. newxpos += length;
  5702. if (newxpos > maxwidth) maxwidth = newxpos;
  5703. return 'translate(' + xpos + ',' + ypos + ')';
  5704. });
  5705. //position legend as far right as possible within the total width
  5706. g.attr('transform', 'translate(' + (width - margin.right - maxwidth) + ',' + margin.top + ')');
  5707. height = margin.top + margin.bottom + ypos + 15;
  5708. }
  5709. if(vers == 'furious') {
  5710. // Size rectangles after text is placed
  5711. seriesShape
  5712. .attr('width', function(d,i) {
  5713. return seriesText[0][i].getComputedTextLength() + 27;
  5714. })
  5715. .attr('height', 18)
  5716. .attr('y', -9)
  5717. .attr('x', -15)
  5718. }
  5719. seriesShape
  5720. .style('fill', setBGColor)
  5721. .style('stroke', function(d,i) { return d.color || color(d, i) });
  5722. });
  5723. function setTextColor(d,i) {
  5724. if(vers != 'furious') return '#000';
  5725. if(expanded) {
  5726. return d.disengaged ? color(d,i) : '#fff';
  5727. } else if (!expanded) {
  5728. return !!d.disabled ? color(d,i) : '#fff';
  5729. }
  5730. }
  5731. function setBGColor(d,i) {
  5732. if(expanded && vers == 'furious') {
  5733. return d.disengaged ? '#fff' : color(d,i);
  5734. } else {
  5735. return !!d.disabled ? '#fff' : color(d,i);
  5736. }
  5737. }
  5738. return chart;
  5739. }
  5740. //============================================================
  5741. // Expose Public Variables
  5742. //------------------------------------------------------------
  5743. chart.dispatch = dispatch;
  5744. chart.options = nv.utils.optionsFunc.bind(chart);
  5745. chart._options = Object.create({}, {
  5746. // simple options, just get/set the necessary values
  5747. width: {get: function(){return width;}, set: function(_){width=_;}},
  5748. height: {get: function(){return height;}, set: function(_){height=_;}},
  5749. key: {get: function(){return getKey;}, set: function(_){getKey=_;}},
  5750. keyFormatter: {get: function(){return keyFormatter;}, set: function(_){keyFormatter=_;}},
  5751. align: {get: function(){return align;}, set: function(_){align=_;}},
  5752. rightAlign: {get: function(){return rightAlign;}, set: function(_){rightAlign=_;}},
  5753. maxKeyLength: {get: function(){return maxKeyLength;}, set: function(_){maxKeyLength=_;}},
  5754. padding: {get: function(){return padding;}, set: function(_){padding=_;}},
  5755. updateState: {get: function(){return updateState;}, set: function(_){updateState=_;}},
  5756. radioButtonMode:{get: function(){return radioButtonMode;}, set: function(_){radioButtonMode=_;}},
  5757. expanded: {get: function(){return expanded;}, set: function(_){expanded=_;}},
  5758. vers: {get: function(){return vers;}, set: function(_){vers=_;}},
  5759. // options that require extra logic in the setter
  5760. margin: {get: function(){return margin;}, set: function(_){
  5761. margin.top = _.top !== undefined ? _.top : margin.top;
  5762. margin.right = _.right !== undefined ? _.right : margin.right;
  5763. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  5764. margin.left = _.left !== undefined ? _.left : margin.left;
  5765. }},
  5766. color: {get: function(){return color;}, set: function(_){
  5767. color = nv.utils.getColor(_);
  5768. }}
  5769. });
  5770. nv.utils.initOptions(chart);
  5771. return chart;
  5772. };
  5773. /*
  5774. Improvements:
  5775. - consistenly apply no-hover classes to rect isntead of to containing g, see example CSS style for .no-hover rect, rect.no-hover
  5776. - row/column order (user specified) or 'ascending' / 'descending'
  5777. - I haven't tested for transitions between changing datasets
  5778. */
  5779. nv.models.heatMap = function() {
  5780. "use strict";
  5781. //============================================================
  5782. // Public Variables with Default Settings
  5783. //------------------------------------------------------------
  5784. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  5785. , width = 960
  5786. , height = 500
  5787. , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one
  5788. , container
  5789. , xScale = d3.scale.ordinal()
  5790. , yScale = d3.scale.ordinal()
  5791. , colorScale = false
  5792. , getX = function(d) { return d.x }
  5793. , getY = function(d) { return d.y }
  5794. , getCellValue = function(d) { return d.value }
  5795. , showCellValues = true
  5796. , cellValueFormat = function(d) { return typeof d === 'number' ? d.toFixed(0) : d }
  5797. , cellAspectRatio = false // width / height of cell
  5798. , cellRadius = 2
  5799. , cellBorderWidth = 4 // pixels between cells
  5800. , normalize = false
  5801. , highContrastText = true
  5802. , xDomain
  5803. , yDomain
  5804. , xMetaColorScale = nv.utils.defaultColor()
  5805. , yMetaColorScale = nv.utils.defaultColor()
  5806. , missingDataColor = '#bcbcbc'
  5807. , missingDataLabel = ''
  5808. , metaOffset = 5 // spacing between meta rects and cells
  5809. , xRange
  5810. , yRange
  5811. , xMeta
  5812. , yMeta
  5813. , colorRange
  5814. , colorDomain
  5815. , dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout', 'elementMousemove', 'renderEnd')
  5816. , duration = 250
  5817. , xMetaHeight = function(d) { return cellHeight / 3 }
  5818. , yMetaWidth = function(d) { return cellWidth / 3 }
  5819. , showGrid = false
  5820. ;
  5821. //============================================================
  5822. // Aux helper function for heatmap
  5823. //------------------------------------------------------------
  5824. // choose high contrast text color based on background
  5825. // shameful steal: https://github.com/alexandersimoes/d3plus/blob/master/src/color/text.coffee
  5826. function cellTextColor(bgColor) {
  5827. if (highContrastText) {
  5828. var rgbColor = d3.rgb(bgColor);
  5829. var r = rgbColor.r;
  5830. var g = rgbColor.g;
  5831. var b = rgbColor.b;
  5832. var yiq = (r * 299 + g * 587 + b * 114) / 1000;
  5833. return yiq >= 128 ? "#404040" : "#EDEDED"; // dark text else light text
  5834. } else {
  5835. return 'black';
  5836. }
  5837. }
  5838. /* go through heatmap data and generate array of values
  5839. * for each row/column or for entire dataset; for use in
  5840. * calculating means/medians of data for normalizing
  5841. * @param {str} axis - 'row', 'col' or null
  5842. *
  5843. * @returns {row/column index: [array of values for row/col]}
  5844. * note that if axis is not specified, the return will be
  5845. * {0: [all values in heatmap]}
  5846. */
  5847. function getHeatmapValues(data, axis) {
  5848. var vals = {};
  5849. data.forEach(function(cell, i) {
  5850. if (axis == 'row') {
  5851. if (!(getIY(cell) in vals)) vals[getIY(cell)] = [];
  5852. vals[getIY(cell)].push(getCellValue(cell));
  5853. } else if (axis == 'col') {
  5854. if (!(getIX(cell) in vals)) vals[getIX(cell)] = [];
  5855. vals[getIX(cell)].push(getCellValue(cell));
  5856. } else if (axis == null) { // if calculating stat over entire dataset
  5857. if (!(0 in vals)) vals[0] = [];
  5858. vals[0].push(getCellValue(cell));
  5859. }
  5860. })
  5861. return vals;
  5862. }
  5863. // calculate the median absolute deviation of the given array of data
  5864. // https://en.wikipedia.org/wiki/Median_absolute_deviation
  5865. // MAD = median(abs(Xi - median(X)))
  5866. function mad(dat) {
  5867. var med = d3.median(dat);
  5868. var vals = dat.map(function(d) { return Math.abs(d - med); })
  5869. return d3.median(vals);
  5870. }
  5871. // set cell color based on cell value
  5872. // depending on whether it should be normalized or not
  5873. function cellColor(d) {
  5874. var colorVal = normalize ? getNorm(d) : getCellValue(d);
  5875. return (cellsAreNumeric() && !isNaN(colorVal) || typeof colorVal !== 'undefined') ? colorScale(colorVal) : missingDataColor;
  5876. }
  5877. // return the domain of the color data
  5878. // if ordinal data is given for the cells, this will
  5879. // return all possible cells values; otherwise it
  5880. // returns the extent of the cell values
  5881. // will take into account normalization if specified
  5882. function getColorDomain() {
  5883. if (cellsAreNumeric()) { // if cell values are numeric
  5884. return normalize ? d3.extent(prepedData, function(d) { return getNorm(d); }) : d3.extent(uniqueColor);
  5885. } else if (!cellsAreNumeric()) { // if cell values are ordinal
  5886. return uniqueColor;
  5887. }
  5888. }
  5889. // return true if cells are numeric
  5890. // as opposed to categorical
  5891. function cellsAreNumeric() {
  5892. return typeof uniqueColor[0] === 'number';
  5893. }
  5894. /*
  5895. * Normalize input data
  5896. *
  5897. * normalize must be one of centerX, robustCenterX, centerScaleX, robustCenterScaleX, centerAll,
  5898. * robustCenterAll, centerScaleAll, robustCenterScaleAll where X is either 'Row' or 'Column'
  5899. *
  5900. * - centerX: subtract row/column mean from cell
  5901. * - centerAll: subtract mean of whole data set from cell
  5902. * - centerScaleX: scale so that row/column has mean 0 and variance 1 (Z-score)
  5903. * - centerScaleAll: scale by overall normalization factor so that the whole data set has mean 0 and variance 1 (Z-score)
  5904. * - robustCenterX: subtract row/column median from cell
  5905. * - robustCenterScaleX: subtract row/column median from cell and then scale row/column by median absolute deviation
  5906. * - robustCenterAll: subtract median of whole data set from cell
  5907. * - robustCenterScaleAll: subtract overall median from cell and scale by overall median absolute deviation
  5908. */
  5909. function normalizeData(dat) {
  5910. var normTypes = ['centerRow',
  5911. 'robustCenterRow',
  5912. 'centerScaleRow',
  5913. 'robustCenterScaleRow',
  5914. 'centerColumn',
  5915. 'robustCenterColumn',
  5916. 'centerScaleColumn',
  5917. 'robustCenterScaleColumn',
  5918. 'centerAll',
  5919. 'robustCenterAll',
  5920. 'centerScaleAll',
  5921. 'robustCenterScaleAll'];
  5922. if(normTypes.indexOf(normalize) != -1) {
  5923. var xVals = Object.keys(uniqueX), yVals = Object.keys(uniqueY);
  5924. // setup normalization options
  5925. var scale = normalize.includes('Scale') ? true: false,
  5926. agg = normalize.includes('robust') ? 'median': 'mean',
  5927. axis = normalize.includes('Row') ? 'row' : normalize.includes('Column') ? 'col' : null,
  5928. vals = getHeatmapValues(dat, axis);
  5929. // calculate mean or median
  5930. // calculate standard dev or median absolute deviation
  5931. var stat = {};
  5932. var dev = {};
  5933. for (var key in vals) {
  5934. stat[key] = agg == 'mean' ? d3.mean(vals[key]) : d3.median(vals[key]);
  5935. if (scale) dev[key] = agg == 'mean' ? d3.deviation(vals[key]) : mad(vals[key]);
  5936. }
  5937. // do the normalizing
  5938. dat.forEach(function(cell, i) {
  5939. if (cellsAreNumeric()) {
  5940. if (axis == 'row') {
  5941. var key = getIY(cell);
  5942. } else if (axis == 'col') {
  5943. var key = getIX(cell);
  5944. } else if (axis == null) { // if calculating stat over entire dataset
  5945. var key = 0;
  5946. }
  5947. var normVal = getCellValue(cell) - stat[key];
  5948. if (scale) {
  5949. cell._cellPos.norm = normVal / dev[key];
  5950. } else {
  5951. cell._cellPos.norm = normVal;
  5952. }
  5953. } else {
  5954. cell._cellPos.norm = getCellValue(cell); // if trying to normalize ordinal cells, just set norm to cell value
  5955. }
  5956. })
  5957. } else {
  5958. normalize = false; // proper normalize option was not provided, disable it so heatmap still shows colors
  5959. }
  5960. return dat;
  5961. }
  5962. /*
  5963. * Process incoming data for use with heatmap including:
  5964. * - adding a unique key indexer to each data point (idx)
  5965. * - getting a unique list of all x & y values
  5966. * - generating a position index (x & y) for each data point
  5967. * - sorting that data for correct traversal when generating rect
  5968. * - generating placeholders for missing data
  5969. *
  5970. * In order to allow for the flexibility of the user providing either
  5971. * categorical or quantitative data, we're going to position the cells
  5972. * through indices that we increment based on previously seen data
  5973. * this way we can use ordinal() axes even if the data is quantitative.
  5974. *
  5975. * When we generate the SVG elements, we assumes traversal occures from
  5976. * top to bottom and from left to right.
  5977. *
  5978. * @param data {list} - input data organize as a list of objects
  5979. *
  5980. * @return - copy of input data with additional '_cellPos' key
  5981. * formatted as {idx: XXX, ix, XXX, iy: XXX}
  5982. * where idx is a global identifier; ix is an identifier
  5983. * within each column, and iy is an identifier within
  5984. * each row.
  5985. */
  5986. function prepData(data) {
  5987. // reinitialize
  5988. uniqueX = {}, // {cell x value: ix index}
  5989. uniqueY = {}, // {cell y value: iy index}
  5990. uniqueColor = [], // [cell color value]
  5991. uniqueXMeta = [], // [cell x metadata value]
  5992. uniqueYMeta = [], // [cell y metadata value]
  5993. uniqueCells = []; // [cell x,y values stored as array]
  5994. var warnings = [];
  5995. var sortedCells = {}; // {cell x values: {cell y value: cell data, ... }, ... }
  5996. var ix = 0, iy = 0; // use these indices to position cell in x & y direction
  5997. var combo, idx=0;
  5998. data.forEach(function(cell) {
  5999. var valX = getX(cell),
  6000. valY = getY(cell),
  6001. valColor = getCellValue(cell);
  6002. // assemble list of unique values for each dimension
  6003. if (!(valX in uniqueX)) {
  6004. uniqueX[valX] = ix;
  6005. ix++;
  6006. sortedCells[valX] = {}
  6007. if (typeof xMeta === 'function') uniqueXMeta.push(xMeta(cell));
  6008. }
  6009. if (!(valY in uniqueY)) {
  6010. uniqueY[valY] = iy;
  6011. iy++;
  6012. sortedCells[valX][valY] = {}
  6013. if (typeof yMeta === 'function') uniqueYMeta.push(yMeta(cell));
  6014. }
  6015. if (uniqueColor.indexOf(valColor) == -1) uniqueColor.push(valColor)
  6016. // for each data point, we generate an object of data
  6017. // needed to properly position each cell
  6018. cell._cellPos = {
  6019. idx: idx,
  6020. ix: uniqueX[valX],
  6021. iy: uniqueY[valY],
  6022. }
  6023. idx++;
  6024. // keep track of row & column combinations we've already seen
  6025. // this prevents the same cells from being generated when
  6026. // the user hasn't provided proper data (one value for each
  6027. // row & column).
  6028. // if properly formatted data is not provided, only the first
  6029. // row & column value is used (the rest are ignored)
  6030. combo = [valX, valY];
  6031. if (!isArrayInArray(uniqueCells, combo)) {
  6032. uniqueCells.push(combo)
  6033. sortedCells[valX][valY] = cell;
  6034. } else if (warnings.indexOf(valX + valY) == -1) {
  6035. warnings.push(valX + valY);
  6036. console.warn("The row/column position " + valX + "/" + valY + " has multiple values; ensure each cell has only a single value.");
  6037. }
  6038. });
  6039. uniqueColor = uniqueColor.sort()
  6040. // check in sortedCells that each x has all the y's
  6041. // if not, generate an empty placeholder
  6042. // this will also sort all cells from left to right
  6043. // and top to bottom
  6044. var reformatData = [];
  6045. Object.keys(uniqueY).forEach(function(j) {
  6046. Object.keys(uniqueX).forEach(function(i) {
  6047. var cellVal = sortedCells[i][j];
  6048. if (cellVal) {
  6049. reformatData.push(cellVal);
  6050. } else {
  6051. var cellPos = {
  6052. idx: idx,
  6053. ix: uniqueX[i],
  6054. iy: uniqueY[j],
  6055. }
  6056. idx++;
  6057. reformatData.push({_cellPos: cellPos}); // empty cell placeholder
  6058. }
  6059. })
  6060. })
  6061. // normalize data is needed
  6062. return normalize ? normalizeData(reformatData) : reformatData;
  6063. }
  6064. // https://stackoverflow.com/a/41661388/1153897
  6065. function isArrayInArray(arr, item){
  6066. var item_as_string = JSON.stringify(item);
  6067. var contains = arr.some(function(ele){
  6068. return JSON.stringify(ele) === item_as_string;
  6069. });
  6070. return contains;
  6071. }
  6072. function removeAllHoverClasses() {
  6073. // remove all hover classes
  6074. d3.selectAll('.cell-hover').classed('cell-hover', false);
  6075. d3.selectAll('.no-hover').classed('no-hover', false);
  6076. d3.selectAll('.row-hover').classed('row-hover', false);
  6077. d3.selectAll('.column-hover').classed('column-hover', false);
  6078. }
  6079. // return the formatted cell value if it is
  6080. // a number, otherwise return missingDataLabel
  6081. var cellValueLabel = function(d) {
  6082. var val = !normalize ? cellValueFormat(getCellValue(d)) : cellValueFormat(getNorm(d));
  6083. return (cellsAreNumeric() && !isNaN(val) || typeof val !== 'undefined') ? val : missingDataLabel;
  6084. }
  6085. // https://stackoverflow.com/a/16794116/1153897
  6086. // note this returns the obj keys
  6087. function sortObjByVals(obj) {
  6088. return Object.keys(obj).sort(function(a,b){return obj[a]-obj[b]})
  6089. }
  6090. // https://stackoverflow.com/a/28191966/1153897
  6091. function getKeyByValue(object, value) {
  6092. //return Object.keys(object).find(key => object[key] === value);
  6093. return Object.keys(object).filter(function(key) {return object[key] === value})[0];
  6094. }
  6095. //============================================================
  6096. // Private Variables
  6097. //------------------------------------------------------------
  6098. var prepedData, cellHeight, cellWidth;
  6099. var uniqueX = {}, uniqueY = {}, uniqueColor = [];
  6100. var uniqueXMeta = [], uniqueYMeta = [], uniqueCells = []
  6101. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  6102. var RdYlBu = ["#a50026","#d73027","#f46d43","#fdae61","#fee090","#ffffbf","#e0f3f8","#abd9e9","#74add1","#4575b4","#313695"];
  6103. var getCellPos = function(d) { return d._cellPos; };
  6104. var getIX = function(d) { return getCellPos(d).ix; } // get the given cell's x index position
  6105. var getIY = function(d) { return getCellPos(d).iy; } // get the given cell's y index position
  6106. var getNorm = function(d) { return getCellPos(d).norm; }
  6107. var getIdx = function(d) { return getCellPos(d).idx; }
  6108. function chart(selection) {
  6109. renderWatch.reset();
  6110. selection.each(function(data) {
  6111. prepedData = prepData(data);
  6112. var availableWidth = width - margin.left - margin.right,
  6113. availableHeight = height - margin.top - margin.bottom;
  6114. // available width/height set the cell dimenions unless
  6115. // the aspect ratio is defined - in that case the cell
  6116. // height is adjusted and availableHeight updated
  6117. cellWidth = availableWidth / Object.keys(uniqueX).length;
  6118. cellHeight = cellAspectRatio ? cellWidth / cellAspectRatio : availableHeight / Object.keys(uniqueY).length;
  6119. if (cellAspectRatio) availableHeight = cellHeight * Object.keys(uniqueY).length - margin.top - margin.bottom;
  6120. container = d3.select(this);
  6121. nv.utils.initSVG(container);
  6122. // Setup Scales
  6123. xScale.domain(xDomain || sortObjByVals(uniqueX))
  6124. .rangeBands(xRange || [0, availableWidth-cellBorderWidth/2]);
  6125. yScale.domain(yDomain || sortObjByVals(uniqueY))
  6126. .rangeBands(yRange || [0, availableHeight-cellBorderWidth/2]);
  6127. colorScale = cellsAreNumeric() ? d3.scale.quantize() : d3.scale.ordinal();
  6128. colorScale.domain(colorDomain || getColorDomain())
  6129. .range(colorRange || RdYlBu);
  6130. // Setup containers and skeleton of chart
  6131. var wrap = container.selectAll('g.nv-heatMapWrap').data([prepedData]);
  6132. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-heatMapWrap');
  6133. wrapEnter
  6134. .append('g')
  6135. .attr('class','cellWrap')
  6136. wrap.watchTransition(renderWatch, 'nv-wrap: heatMapWrap')
  6137. .attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  6138. var gridWrap = wrapEnter
  6139. .append('g')
  6140. .attr('class','cellGrid')
  6141. .style('opacity',1e-6)
  6142. var gridLinesV = wrap.select('.cellGrid').selectAll('.gridLines.verticalGrid')
  6143. .data(Object.values(uniqueX).concat([Object.values(uniqueX).length]))
  6144. gridLinesV.enter()
  6145. .append('line')
  6146. .attr('class','gridLines verticalGrid')
  6147. gridLinesV.exit()
  6148. .remove()
  6149. var gridLinesH = wrap.select('.cellGrid').selectAll('.gridLines.horizontalGrid')
  6150. .data(Object.values(uniqueY).concat([Object.values(uniqueY).length]))
  6151. gridLinesH.enter()
  6152. .append('line')
  6153. .attr('class','gridLines horizontalGrid')
  6154. gridLinesH.exit()
  6155. .remove()
  6156. var cellWrap = wrap.select('.cellWrap')
  6157. .selectAll(".nv-cell")
  6158. .data(function(d) { return d; }, function(e) { return getIdx(e); })
  6159. var xMetaWrap = wrapEnter
  6160. .append('g')
  6161. .attr('class','xMetaWrap')
  6162. .attr("transform", function() { return "translate(0," + (-xMetaHeight()-cellBorderWidth-metaOffset) + ")" })
  6163. var xMetas = wrap.select('.xMetaWrap').selectAll('.x-meta')
  6164. .data(uniqueXMeta)
  6165. var xMetaEnter = xMetas
  6166. .enter()
  6167. .append('rect')
  6168. .attr('class','x-meta meta')
  6169. .attr("width", cellWidth-cellBorderWidth)
  6170. .attr("height", xMetaHeight())
  6171. .attr("transform", "translate(0,0)")
  6172. .attr("fill", function(d) { return xMetaColorScale(d); })
  6173. var yMetaWrap = wrapEnter
  6174. .append('g')
  6175. .attr('class','yMetaWrap')
  6176. .attr("transform", function(d,i) { return "translate(" + (-yMetaWidth()-cellBorderWidth-metaOffset) + ",0)" })
  6177. var yMetas = wrap.select('.yMetaWrap').selectAll('.y-meta')
  6178. .data(uniqueYMeta)
  6179. var yMetaEnter = yMetas
  6180. .enter()
  6181. .append('rect')
  6182. .attr('class','y-meta meta')
  6183. .attr("width", yMetaWidth())
  6184. .attr("height", cellHeight-cellBorderWidth)
  6185. .attr("transform", function(d,i) { return "translate(0,0)" })
  6186. .attr("fill", function(d,i) { return yMetaColorScale(d); })
  6187. xMetas.exit().remove()
  6188. yMetas.exit().remove()
  6189. // CELLS
  6190. var cellsEnter = cellWrap
  6191. .enter()
  6192. .append('g')
  6193. .style('opacity', 1e-6)
  6194. .attr("transform", function(d) { return "translate(0," + getIY(d) * cellHeight + ")" }) // enter all g's here for a sweep-right transition
  6195. .attr('data-row', function(d) { return getIY(d) })
  6196. .attr('data-column', function(d) { return getIX(d) });
  6197. cellsEnter
  6198. .append("rect")
  6199. cellsEnter
  6200. .append('text')
  6201. .attr('text-anchor', 'middle')
  6202. .attr("dy", 4)
  6203. .attr("class","cell-text")
  6204. // transition cell (rect) size
  6205. cellWrap.selectAll('rect')
  6206. .watchTransition(renderWatch, 'heatMap: rect')
  6207. .attr("width", cellWidth-cellBorderWidth)
  6208. .attr("height", cellHeight-cellBorderWidth)
  6209. .attr('rx', cellRadius)
  6210. .attr('ry', cellRadius)
  6211. .style('stroke', function(d) { return cellColor(d) })
  6212. // transition cell (g) position, opacity and fill
  6213. cellWrap
  6214. .attr("class",function(d) { return isNaN(getCellValue(d)) ? 'nv-cell cell-missing' : 'nv-cell'})
  6215. .watchTransition(renderWatch, 'heatMap: cells')
  6216. .style({
  6217. 'opacity': 1,
  6218. 'fill': function(d) { return cellColor(d) },
  6219. })
  6220. .attr("transform", function(d) { return "translate(" + getIX(d) * cellWidth + "," + getIY(d) * cellHeight + ")" })
  6221. .attr("class",function(d) { return isNaN(getCellValue(d)) ? 'nv-cell cell-missing' : 'nv-cell'})
  6222. cellWrap.exit().remove();
  6223. // transition text position and fill
  6224. cellWrap.selectAll('text')
  6225. .watchTransition(renderWatch, 'heatMap: cells text')
  6226. .text(function(d) { return cellValueLabel(d); })
  6227. .attr("x", function(d) { return (cellWidth-cellBorderWidth) / 2; })
  6228. .attr("y", function(d) { return (cellHeight-cellBorderWidth) / 2; })
  6229. .style("fill", function(d) { return cellTextColor(cellColor(d)) })
  6230. .style('opacity', function() { return showCellValues ? 1 : 0 })
  6231. // transition grid
  6232. wrap.selectAll('.verticalGrid')
  6233. .watchTransition(renderWatch, 'heatMap: gridLines')
  6234. .attr('y1',0)
  6235. .attr('y2',availableHeight-cellBorderWidth)
  6236. .attr('x1',function(d) { return d*cellWidth-cellBorderWidth/2; })
  6237. .attr('x2',function(d) { return d*cellWidth-cellBorderWidth/2; })
  6238. var numHLines = Object.keys(uniqueY).length;
  6239. wrap.selectAll('.horizontalGrid')
  6240. .watchTransition(renderWatch, 'heatMap: gridLines')
  6241. .attr('x1',function(d) { return (d == 0 || d == numHLines) ? -cellBorderWidth : 0 })
  6242. .attr('x2',function(d) { return (d == 0 || d == numHLines) ? availableWidth : availableWidth-cellBorderWidth})
  6243. .attr('y1',function(d) { return d*cellHeight-cellBorderWidth/2; })
  6244. .attr('y2',function(d) { return d*cellHeight-cellBorderWidth/2; })
  6245. wrap.select('.cellGrid')
  6246. .watchTransition(renderWatch, 'heatMap: gridLines')
  6247. .style({
  6248. 'stroke-width': cellBorderWidth,
  6249. 'opacity': function() { return showGrid ? 1 : 1e-6 },
  6250. })
  6251. var xMetaRect = wrap.selectAll('.x-meta')
  6252. var yMetaRect = wrap.selectAll('.y-meta')
  6253. var allMetaRect = wrap.selectAll('.meta')
  6254. // transition meta rect size
  6255. xMetas
  6256. .watchTransition(renderWatch, 'heatMap: xMetaRect')
  6257. .attr("width", cellWidth-cellBorderWidth)
  6258. .attr("height", xMetaHeight())
  6259. .attr("transform", function(d,i) { return "translate(" + (i * cellWidth) + ",0)" })
  6260. yMetas
  6261. .watchTransition(renderWatch, 'heatMap: yMetaRect')
  6262. .attr("width", yMetaWidth())
  6263. .attr("height", cellHeight-cellBorderWidth)
  6264. .attr("transform", function(d,i) { return "translate(0," + (i * cellHeight) + ")" })
  6265. // transition position of meta wrap g & opacity
  6266. wrap.select('.xMetaWrap')
  6267. .watchTransition(renderWatch, 'heatMap: xMetaWrap')
  6268. .attr("transform", function(d,i) { return "translate(0," + (-xMetaHeight()-cellBorderWidth-metaOffset) + ")" })
  6269. .style("opacity", function() { return xMeta !== false ? 1 : 0 })
  6270. wrap.select('.yMetaWrap')
  6271. .watchTransition(renderWatch, 'heatMap: yMetaWrap')
  6272. .attr("transform", function(d,i) { return "translate(" + (-yMetaWidth()-cellBorderWidth-metaOffset) + ",0)" })
  6273. .style("opacity", function() { return yMeta !== false ? 1 : 0 })
  6274. // TOOLTIPS
  6275. cellWrap
  6276. .on('mouseover', function(d,i) {
  6277. var idx = getIdx(d);
  6278. var ix = getIX(d);
  6279. var iy = getIY(d);
  6280. // set the proper classes for all cells
  6281. // hover row gets class .row-hover
  6282. // hover column gets class .column-hover
  6283. // hover cell gets class .cell-hover
  6284. // all remaining cells get class .no-hover
  6285. d3.selectAll('.nv-cell').each(function(e) {
  6286. if (idx == getIdx(e)) {
  6287. d3.select(this).classed('cell-hover', true);
  6288. d3.select(this).classed('no-hover', false);
  6289. } else {
  6290. d3.select(this).classed('no-hover', true);
  6291. d3.select(this).classed('cell-hover', false);
  6292. }
  6293. if (ix == getIX(e)) {
  6294. d3.select(this).classed('no-hover', false);
  6295. d3.select(this).classed('column-hover', true);
  6296. }
  6297. if (iy == getIY(e)) {
  6298. d3.select(this).classed('no-hover', false);
  6299. d3.select(this).classed('row-hover', true);
  6300. }
  6301. })
  6302. // set hover classes for column metadata
  6303. d3.selectAll('.x-meta').each(function(e, j) {
  6304. if (j == ix) {
  6305. d3.select(this).classed('cell-hover', true);
  6306. d3.select(this).classed('no-hover', false);
  6307. } else {
  6308. d3.select(this).classed('no-hover', true);
  6309. d3.select(this).classed('cell-hover', false);
  6310. }
  6311. });
  6312. // set hover class for row metadata
  6313. d3.selectAll('.y-meta').each(function(e, j) {
  6314. if (j == iy) {
  6315. d3.select(this).classed('cell-hover', true);
  6316. d3.select(this).classed('no-hover', false);
  6317. } else {
  6318. d3.select(this).classed('no-hover', true);
  6319. d3.select(this).classed('cell-hover', false);
  6320. }
  6321. });
  6322. dispatch.elementMouseover({
  6323. value: getKeyByValue(uniqueX, ix) + ' & ' + getKeyByValue(uniqueY, iy),
  6324. series: {
  6325. value: cellValueLabel(d),
  6326. color: d3.select(this).select('rect').style("fill")
  6327. },
  6328. e: d3.event,
  6329. });
  6330. })
  6331. .on('mouseout', function(d,i) {
  6332. // allow tooltip to remain even when mouse is over the
  6333. // space between the cell;
  6334. // this prevents cells from "flashing" when transitioning
  6335. // between cells
  6336. var bBox = d3.select(this).select('rect').node().getBBox();
  6337. var coordinates = d3.mouse(d3.select('.nv-heatMap').node());
  6338. var x = coordinates[0];
  6339. var y = coordinates[1];
  6340. // we only trigger mouseout when mouse moves outside of
  6341. // .nv-heatMap
  6342. if (x + cellBorderWidth >= availableWidth || y + cellBorderWidth >= availableHeight || x < 0 || y < 0) {
  6343. // remove all hover classes
  6344. removeAllHoverClasses();
  6345. dispatch.elementMouseout({e: d3.event});
  6346. }
  6347. })
  6348. .on('mousemove', function(d,i) {
  6349. dispatch.elementMousemove({e: d3.event});
  6350. })
  6351. allMetaRect
  6352. .on('mouseover', function(d,i) {
  6353. // true if hovering over a row metadata rect
  6354. var isColMeta = d3.select(this).attr('class').indexOf('x-meta') != -1 ? true : false;
  6355. // apply proper .row-hover & .column-hover
  6356. // classes to cells
  6357. d3.selectAll('.nv-cell').each(function(e) {
  6358. if (isColMeta && i == getIX(e)) {
  6359. d3.select(this).classed('column-hover', true);
  6360. d3.select(this).classed('no-hover', false);
  6361. } else if (!isColMeta && i-uniqueXMeta.length == getIY(e)) {
  6362. // since allMetaRect selects all the meta rects, the index for the y's will
  6363. // be offset by the number of x rects. TODO - write seperate tooltip sections
  6364. // for x meta rect & y meta rect
  6365. d3.select(this).classed('row-hover', true);
  6366. d3.select(this).classed('no-hover', false);
  6367. } else {
  6368. d3.select(this).classed('no-hover', true);
  6369. d3.select(this).classed('column-hover', false);
  6370. d3.select(this).classed('row-hover', false);
  6371. }
  6372. d3.select(this).classed('cell-hover', false);
  6373. })
  6374. // apply proper .row-hover & .column-hover
  6375. // classes to meta rects
  6376. d3.selectAll('.meta').classed('no-hover', true);
  6377. d3.select(this).classed('cell-hover', true);
  6378. d3.select(this).classed('no-hover', false);
  6379. dispatch.elementMouseover({
  6380. value: isColMeta ? 'Column meta' : 'Row meta',
  6381. series: { value: d, color: d3.select(this).style('fill'), }
  6382. });
  6383. })
  6384. .on('mouseout', function(d,i) {
  6385. // true if hovering over a row metadata rect
  6386. var isColMeta = d3.select(this).attr('class').indexOf('x-meta') != -1 ? true : false;
  6387. // allow tooltip to remain even when mouse is over the
  6388. // space between the cell;
  6389. // this prevents cells from "flashing" when transitioning
  6390. // between cells
  6391. var bBox = d3.select(this).node().getBBox();
  6392. var coordinates = d3.mouse(d3.select(isColMeta ? '.xMetaWrap' : '.yMetaWrap').node());
  6393. var x = coordinates[0];
  6394. var y = coordinates[1];
  6395. if ( y < 0 || x < 0 ||
  6396. (isColMeta && x + cellBorderWidth >= availableWidth) ||
  6397. (!isColMeta && y + cellBorderWidth >= availableHeight)
  6398. ) {
  6399. // remove all hover classes
  6400. removeAllHoverClasses();
  6401. dispatch.elementMouseout({e: d3.event});
  6402. }
  6403. })
  6404. .on('mousemove', function(d,i) {
  6405. dispatch.elementMousemove({e: d3.event});
  6406. })
  6407. });
  6408. renderWatch.renderEnd('heatMap immediate');
  6409. return chart;
  6410. }
  6411. //============================================================
  6412. // Expose Public Variables
  6413. //------------------------------------------------------------
  6414. chart.dispatch = dispatch;
  6415. chart.options = nv.utils.optionsFunc.bind(chart);
  6416. chart._options = Object.create({}, {
  6417. // simple options, just get/set the necessary values
  6418. width: {get: function(){return width;}, set: function(_){width=_;}},
  6419. height: {get: function(){return height;}, set: function(_){height=_;}},
  6420. showCellValues: {get: function(){return showCellValues;}, set: function(_){showCellValues=_;}},
  6421. x: {get: function(){return getX;}, set: function(_){getX=_;}}, // data attribute for horizontal axis
  6422. y: {get: function(){return getY;}, set: function(_){getY=_;}}, // data attribute for vertical axis
  6423. cellValue: {get: function(){return getCellValue;}, set: function(_){getCellValue=_;}}, // data attribute that sets cell value and color
  6424. missingDataColor: {get: function(){return missingDataColor;}, set: function(_){missingDataColor=_;}},
  6425. missingDataLabel: {get: function(){return missingDataLabel;}, set: function(_){missingDataLabel=_;}},
  6426. xScale: {get: function(){return xScale;}, set: function(_){xScale=_;}},
  6427. yScale: {get: function(){return yScale;}, set: function(_){yScale=_;}},
  6428. colorScale: {get: function(){return colorScale;}, set: function(_){colorScale=_;}}, // scale to map cell values to colors
  6429. xDomain: {get: function(){return xDomain;}, set: function(_){xDomain=_;}},
  6430. yDomain: {get: function(){return yDomain;}, set: function(_){yDomain=_;}},
  6431. xRange: {get: function(){return xRange;}, set: function(_){xRange=_;}},
  6432. yRange: {get: function(){return yRange;}, set: function(_){yRange=_;}},
  6433. colorRange: {get: function(){return colorRange;}, set: function(_){colorRange=_;}},
  6434. colorDomain: {get: function(){return colorDomain;}, set: function(_){colorDomain=_;}},
  6435. xMeta: {get: function(){return xMeta;}, set: function(_){xMeta=_;}},
  6436. yMeta: {get: function(){return yMeta;}, set: function(_){yMeta=_;}},
  6437. xMetaColorScale: {get: function(){return color;}, set: function(_){color = nv.utils.getColor(_);}},
  6438. yMetaColorScale: {get: function(){return color;}, set: function(_){color = nv.utils.getColor(_);}},
  6439. cellAspectRatio: {get: function(){return cellAspectRatio;}, set: function(_){cellAspectRatio=_;}}, // cell width / height
  6440. cellRadius: {get: function(){return cellRadius;}, set: function(_){cellRadius=_;}}, // cell width / height
  6441. cellHeight: {get: function(){return cellHeight;}}, // TODO - should not be exposed since we don't want user setting this
  6442. cellWidth: {get: function(){return cellWidth;}}, // TODO - should not be exposed since we don't want user setting this
  6443. normalize: {get: function(){return normalize;}, set: function(_){normalize=_;}},
  6444. cellBorderWidth: {get: function(){return cellBorderWidth;}, set: function(_){cellBorderWidth=_;}},
  6445. highContrastText: {get: function(){return highContrastText;}, set: function(_){highContrastText=_;}},
  6446. cellValueFormat: {get: function(){return cellValueFormat;}, set: function(_){cellValueFormat=_;}},
  6447. id: {get: function(){return id;}, set: function(_){id=_;}},
  6448. metaOffset: {get: function(){return metaOffset;}, set: function(_){metaOffset=_;}},
  6449. xMetaHeight: {get: function(){return xMetaHeight;}, set: function(_){xMetaHeight=_;}},
  6450. yMetaWidth: {get: function(){return yMetaWidth;}, set: function(_){yMetaWidth=_;}},
  6451. showGrid: {get: function(){return showGrid;}, set: function(_){showGrid=_;}},
  6452. // options that require extra logic in the setter
  6453. margin: {get: function(){return margin;}, set: function(_){
  6454. margin.top = _.top !== undefined ? _.top : margin.top;
  6455. margin.right = _.right !== undefined ? _.right : margin.right;
  6456. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  6457. margin.left = _.left !== undefined ? _.left : margin.left;
  6458. }},
  6459. duration: {get: function(){return duration;}, set: function(_){
  6460. duration = _;
  6461. renderWatch.reset(duration);
  6462. }}
  6463. });
  6464. nv.utils.initOptions(chart);
  6465. return chart;
  6466. };
  6467. /* Heatmap Chart Type
  6468. A heatmap is a graphical representation of data where the individual values
  6469. contained in a matrix are represented as colors within cells. Furthermore,
  6470. metadata can be associated with each of the matrix rows or columns. By grouping
  6471. these rows/columns together by a given metadata value, data trends can be spotted.
  6472. Format for input data should be:
  6473. var data = [
  6474. {day: 'mo', hour: '1a', value: 16, timeperiod: 'early morning', weekperiod: 'week', category: 1},
  6475. {day: 'mo', hour: '2a', value: 20, timeperiod: 'early morning', weekperiod: 'week', category: 2},
  6476. {day: 'mo', hour: '3a', value: 0, timeperiod: 'early morning', weekperiod: 'week', category: 1},
  6477. ...
  6478. ]
  6479. where the keys 'day' and 'hour' specify the row/column of the heatmap, 'value' specifies the cell
  6480. value and the keys 'timeperiod', 'weekperiod' and 'week' are extra metadata that can be associated
  6481. with rows/columns.
  6482. Options for chart:
  6483. */
  6484. nv.models.heatMapChart = function() {
  6485. "use strict";
  6486. //============================================================
  6487. // Public Variables with Default Settings
  6488. //------------------------------------------------------------
  6489. var heatMap = nv.models.heatMap()
  6490. , legend = nv.models.legend()
  6491. , legendRowMeta = nv.models.legend()
  6492. , legendColumnMeta = nv.models.legend()
  6493. , tooltip = nv.models.tooltip()
  6494. , xAxis = nv.models.axis()
  6495. , yAxis = nv.models.axis()
  6496. ;
  6497. var margin = {top: 20, right: 10, bottom: 50, left: 60}
  6498. , marginTop = null
  6499. , width = null
  6500. , height = null
  6501. , color = nv.utils.getColor()
  6502. , showLegend = true
  6503. , staggerLabels = false
  6504. , showXAxis = true
  6505. , showYAxis = true
  6506. , alignYAxis = 'left'
  6507. , alignXAxis = 'top'
  6508. , rotateLabels = 0
  6509. , title = false
  6510. , x
  6511. , y
  6512. , noData = null
  6513. , dispatch = d3.dispatch('beforeUpdate','renderEnd')
  6514. , duration = 250
  6515. ;
  6516. xAxis
  6517. .orient(alignXAxis)
  6518. .showMaxMin(false)
  6519. .tickFormat(function(d) { return d })
  6520. ;
  6521. yAxis
  6522. .orient(alignYAxis)
  6523. .showMaxMin(false)
  6524. .tickFormat(function(d) { return d })
  6525. ;
  6526. tooltip
  6527. .duration(0)
  6528. .headerEnabled(true)
  6529. .keyFormatter(function(d, i) {
  6530. return xAxis.tickFormat()(d, i);
  6531. })
  6532. //============================================================
  6533. // Private Variables
  6534. //------------------------------------------------------------
  6535. // https://bl.ocks.org/mbostock/4573883
  6536. // get max/min range for all the quantized cell values
  6537. // returns an array where each element is [start,stop]
  6538. // of color bin
  6539. function quantizeLegendValues() {
  6540. var e = heatMap.colorScale(), legendVals;
  6541. if (typeof e.domain()[0] === 'string') { // if color scale is ordinal
  6542. legendVals = e.domain();
  6543. } else { // if color scale is numeric
  6544. legendVals = e.range().map(function(color) {
  6545. var d = e.invertExtent(color);
  6546. if (d[0] === null) d[0] = e.domain()[0];
  6547. if (d[1] === null) d[1] = e.domain()[1];
  6548. return d;
  6549. })
  6550. }
  6551. return legendVals
  6552. }
  6553. // return true if row metadata specified by user
  6554. function hasRowMeta() {
  6555. return typeof heatMap.yMeta() === 'function'
  6556. }
  6557. // return true if col metadata specified by user
  6558. function hasColumnMeta() {
  6559. return typeof heatMap.xMeta() === 'function'
  6560. }
  6561. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  6562. function chart(selection) {
  6563. renderWatch.reset();
  6564. renderWatch.models(heatMap);
  6565. renderWatch.models(xAxis);
  6566. renderWatch.models(yAxis);
  6567. selection.each(function(data) {
  6568. var container = d3.select(this),
  6569. that = this;
  6570. nv.utils.initSVG(container);
  6571. var availableWidth = nv.utils.availableWidth(width, container, margin),
  6572. availableHeight = nv.utils.availableHeight(height, container, margin);
  6573. chart.update = function() {
  6574. dispatch.beforeUpdate();
  6575. container.transition().duration(duration).call(chart);
  6576. };
  6577. chart.container = this;
  6578. // Display No Data message if there's nothing to show.
  6579. if (!data || !data.length) {
  6580. nv.utils.noData(chart, container);
  6581. return chart;
  6582. } else {
  6583. container.selectAll('.nv-noData').remove();
  6584. }
  6585. // Setup Scales
  6586. x = heatMap.xScale();
  6587. y = heatMap.yScale();
  6588. // Setup containers and skeleton of chart
  6589. var wrap = container.selectAll('g.nv-wrap').data([data]);
  6590. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap').append('g');
  6591. var g = wrap.select('g');
  6592. gEnter.append('g').attr('class', 'nv-heatMap');
  6593. gEnter.append('g').attr('class', 'nv-legendWrap');
  6594. gEnter.append('g').attr('class', 'nv-x nv-axis');
  6595. gEnter.append('g').attr('class', 'nv-y nv-axis')
  6596. g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  6597. heatMap
  6598. .width(availableWidth)
  6599. .height(availableHeight);
  6600. var heatMapWrap = g.select('.nv-heatMap')
  6601. .datum(data.filter(function(d) { return !d.disabled }));
  6602. heatMapWrap.transition().call(heatMap);
  6603. if (heatMap.cellAspectRatio()) {
  6604. availableHeight = heatMap.cellHeight() * y.domain().length;
  6605. heatMap.height(availableHeight);
  6606. }
  6607. // Setup Axes
  6608. xAxis
  6609. .scale(x)
  6610. ._ticks( nv.utils.calcTicksX(availableWidth/100, data) )
  6611. .tickSize(-availableHeight, 0);
  6612. var axisX = g.select('.nv-x.nv-axis')
  6613. axisX.call(xAxis)
  6614. .watchTransition(renderWatch, 'heatMap: axisX')
  6615. .selectAll('.tick')
  6616. .style('opacity', function() { return showXAxis ? 1 : 0 } )
  6617. var xTicks = axisX.selectAll('g');
  6618. xTicks
  6619. .selectAll('.tick text')
  6620. .attr('transform', function(d,i,j) {
  6621. var rot = rotateLabels != 0 ? rotateLabels : '0';
  6622. var stagger = staggerLabels ? j % 2 == 0 ? '5' : '17' : '0';
  6623. return 'translate(0, ' + stagger + ') rotate(' + rot + ' 0,0)';
  6624. })
  6625. .style('text-anchor', rotateLabels > 0 ? 'start' : rotateLabels < 0 ? 'end' : 'middle');
  6626. // position text in center of meta rects
  6627. var yPos = -5;
  6628. if (hasColumnMeta()) {
  6629. axisX.selectAll('text').style('text-anchor', 'middle')
  6630. yPos = -heatMap.xMetaHeight()()/2 - heatMap.metaOffset() + 3;
  6631. }
  6632. // adjust position of axis based on presence of metadata group
  6633. if (alignXAxis == 'bottom') {
  6634. axisX
  6635. .watchTransition(renderWatch, 'heatMap: axisX')
  6636. .attr("transform", "translate(0," + (availableHeight - yPos) + ")");
  6637. if (heatMap.xMeta() !== false) { // if showing x metadata
  6638. var pos = availableHeight+heatMap.metaOffset()+heatMap.cellBorderWidth()
  6639. g.select('.xMetaWrap')
  6640. .watchTransition(renderWatch, 'heatMap: xMetaWrap')
  6641. .attr("transform", function(d,i) { return "translate(0," + pos + ")" })
  6642. }
  6643. } else {
  6644. axisX
  6645. .watchTransition(renderWatch, 'heatMap: axisX')
  6646. .attr("transform", "translate(0," + yPos + ")");
  6647. }
  6648. yAxis
  6649. .scale(y)
  6650. ._ticks( nv.utils.calcTicksY(availableHeight/36, data) )
  6651. .tickSize( -availableWidth, 0);
  6652. var axisY = g.select('.nv-y.nv-axis')
  6653. axisY.call(yAxis)
  6654. .watchTransition(renderWatch, 'heatMap: axisY')
  6655. .selectAll('.tick')
  6656. .style('opacity', function() { return showYAxis ? 1 : 0 } )
  6657. // position text in center of meta rects
  6658. var xPos = -5;
  6659. if (hasRowMeta()) {
  6660. axisY.selectAll('text').style('text-anchor', 'middle')
  6661. xPos = -heatMap.yMetaWidth()()/2 - heatMap.metaOffset();
  6662. }
  6663. // adjust position of axis based on presence of metadata group
  6664. if (alignYAxis == 'right') {
  6665. axisY.attr("transform", "translate(" + (availableWidth - xPos) + ",0)");
  6666. if (heatMap.yMeta() !== false) { // if showing y meatdata
  6667. var pos = availableWidth+heatMap.metaOffset()+heatMap.cellBorderWidth()
  6668. g.select('.yMetaWrap')
  6669. .watchTransition(renderWatch, 'heatMap: yMetaWrap')
  6670. .attr("transform", function(d,i) { return "translate(" + pos + ",0)" })
  6671. }
  6672. } else {
  6673. axisY.attr("transform", "translate(" + xPos + ",0)");
  6674. }
  6675. // Legend
  6676. var legendWrap = g.select('.nv-legendWrap')
  6677. legend
  6678. .width(availableWidth)
  6679. .color(heatMap.colorScale().range())
  6680. var legendVal = quantizeLegendValues().map(function(d) {
  6681. if (Array.isArray(d)) { // if cell values are numeric
  6682. return {key: d[0].toFixed(1) + " - " + d[1].toFixed(1)};
  6683. } else { // if cell values are ordinal
  6684. return {key: d};
  6685. }
  6686. })
  6687. legendWrap
  6688. .datum(legendVal)
  6689. .call(legend)
  6690. .attr('transform', 'translate(0,' + (alignXAxis == 'top' ? availableHeight : -30) + ')'); // TODO: more intelligent offset (-30) when top aligning legend
  6691. legendWrap
  6692. .watchTransition(renderWatch, 'heatMap: nv-legendWrap')
  6693. .style('opacity', function() { return showLegend ? 1 : 0 } )
  6694. });
  6695. // axis don't have a flag for disabling the zero line, so we do it manually
  6696. d3.selectAll('.nv-axis').selectAll('line')
  6697. .style('stroke-opacity', 0)
  6698. d3.select('.nv-y').select('path.domain').remove()
  6699. renderWatch.renderEnd('heatMap chart immediate');
  6700. return chart;
  6701. }
  6702. //============================================================
  6703. // Event Handling/Dispatching (out of chart's scope)
  6704. //------------------------------------------------------------
  6705. heatMap.dispatch.on('elementMouseover.tooltip', function(evt) {
  6706. tooltip.data(evt).hidden(false);
  6707. });
  6708. heatMap.dispatch.on('elementMouseout.tooltip', function(evt) {
  6709. tooltip.hidden(true);
  6710. });
  6711. heatMap.dispatch.on('elementMousemove.tooltip', function(evt) {
  6712. tooltip();
  6713. });
  6714. //============================================================
  6715. // Expose Public Variables
  6716. //------------------------------------------------------------
  6717. chart.dispatch = dispatch;
  6718. chart.heatMap = heatMap;
  6719. chart.legend = legend;
  6720. chart.xAxis = xAxis;
  6721. chart.yAxis = yAxis;
  6722. chart.tooltip = tooltip;
  6723. chart.options = nv.utils.optionsFunc.bind(chart);
  6724. chart._options = Object.create({}, {
  6725. // simple options, just get/set the necessary values
  6726. width: {get: function(){return width;}, set: function(_){width=_;}},
  6727. height: {get: function(){return height;}, set: function(_){height=_;}},
  6728. showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
  6729. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  6730. showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}},
  6731. showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}},
  6732. staggerLabels: {get: function(){return staggerLabels;}, set: function(_){staggerLabels=_;}},
  6733. rotateLabels: {get: function(){return rotateLabels;}, set: function(_){rotateLabels=_;}},
  6734. // options that require extra logic in the setter
  6735. margin: {get: function(){return margin;}, set: function(_){
  6736. if (_.top !== undefined) {
  6737. margin.top = _.top;
  6738. marginTop = _.top;
  6739. }
  6740. margin.right = _.right !== undefined ? _.right : margin.right;
  6741. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  6742. margin.left = _.left !== undefined ? _.left : margin.left;
  6743. }},
  6744. duration: {get: function(){return duration;}, set: function(_){
  6745. duration = _;
  6746. renderWatch.reset(duration);
  6747. heatMap.duration(duration);
  6748. xAxis.duration(duration);
  6749. yAxis.duration(duration);
  6750. }},
  6751. alignYAxis: {get: function(){return alignYAxis;}, set: function(_){
  6752. alignYAxis = _;
  6753. yAxis.orient(_);
  6754. }},
  6755. alignXAxis: {get: function(){return alignXAxis;}, set: function(_){
  6756. alignXAxis = _;
  6757. xAxis.orient(_);
  6758. }},
  6759. });
  6760. nv.utils.inheritOptions(chart, heatMap);
  6761. nv.utils.initOptions(chart);
  6762. return chart;
  6763. }
  6764. //TODO: consider deprecating and using multibar with single series for this
  6765. nv.models.historicalBar = function() {
  6766. "use strict";
  6767. //============================================================
  6768. // Public Variables with Default Settings
  6769. //------------------------------------------------------------
  6770. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  6771. , width = null
  6772. , height = null
  6773. , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one
  6774. , container = null
  6775. , x = d3.scale.linear()
  6776. , y = d3.scale.linear()
  6777. , getX = function(d) { return d.x }
  6778. , getY = function(d) { return d.y }
  6779. , forceX = []
  6780. , forceY = [0]
  6781. , padData = false
  6782. , clipEdge = true
  6783. , color = nv.utils.defaultColor()
  6784. , xDomain
  6785. , yDomain
  6786. , xRange
  6787. , yRange
  6788. , dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout', 'elementMousemove', 'renderEnd')
  6789. , interactive = true
  6790. ;
  6791. var renderWatch = nv.utils.renderWatch(dispatch, 0);
  6792. function chart(selection) {
  6793. selection.each(function(data) {
  6794. renderWatch.reset();
  6795. container = d3.select(this);
  6796. var availableWidth = nv.utils.availableWidth(width, container, margin),
  6797. availableHeight = nv.utils.availableHeight(height, container, margin);
  6798. nv.utils.initSVG(container);
  6799. // Setup Scales
  6800. x.domain(xDomain || d3.extent(data[0].values.map(getX).concat(forceX) ));
  6801. if (padData)
  6802. x.range(xRange || [availableWidth * .5 / data[0].values.length, availableWidth * (data[0].values.length - .5) / data[0].values.length ]);
  6803. else
  6804. x.range(xRange || [0, availableWidth]);
  6805. y.domain(yDomain || d3.extent(data[0].values.map(getY).concat(forceY) ))
  6806. .range(yRange || [availableHeight, 0]);
  6807. // If scale's domain don't have a range, slightly adjust to make one... so a chart can show a single data point
  6808. if (x.domain()[0] === x.domain()[1])
  6809. x.domain()[0] ?
  6810. x.domain([x.domain()[0] - x.domain()[0] * 0.01, x.domain()[1] + x.domain()[1] * 0.01])
  6811. : x.domain([-1,1]);
  6812. if (y.domain()[0] === y.domain()[1])
  6813. y.domain()[0] ?
  6814. y.domain([y.domain()[0] + y.domain()[0] * 0.01, y.domain()[1] - y.domain()[1] * 0.01])
  6815. : y.domain([-1,1]);
  6816. // Setup containers and skeleton of chart
  6817. var wrap = container.selectAll('g.nv-wrap.nv-historicalBar-' + id).data([data[0].values]);
  6818. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-historicalBar-' + id);
  6819. var defsEnter = wrapEnter.append('defs');
  6820. var gEnter = wrapEnter.append('g');
  6821. var g = wrap.select('g');
  6822. gEnter.append('g').attr('class', 'nv-bars');
  6823. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  6824. container
  6825. .on('click', function(d,i) {
  6826. dispatch.chartClick({
  6827. data: d,
  6828. index: i,
  6829. pos: d3.event,
  6830. id: id
  6831. });
  6832. });
  6833. defsEnter.append('clipPath')
  6834. .attr('id', 'nv-chart-clip-path-' + id)
  6835. .append('rect');
  6836. wrap.select('#nv-chart-clip-path-' + id + ' rect')
  6837. .attr('width', availableWidth)
  6838. .attr('height', availableHeight);
  6839. g.attr('clip-path', clipEdge ? 'url(#nv-chart-clip-path-' + id + ')' : '');
  6840. var bars = wrap.select('.nv-bars').selectAll('.nv-bar')
  6841. .data(function(d) { return d }, function(d,i) {return getX(d,i)});
  6842. bars.exit().remove();
  6843. bars.enter().append('rect')
  6844. .attr('x', 0 )
  6845. .attr('y', function(d,i) { return nv.utils.NaNtoZero(y(Math.max(0, getY(d,i)))) })
  6846. .attr('height', function(d,i) { return nv.utils.NaNtoZero(Math.abs(y(getY(d,i)) - y(0))) })
  6847. .attr('transform', function(d,i) { return 'translate(' + (x(getX(d,i)) - availableWidth / data[0].values.length * .45) + ',0)'; })
  6848. .on('mouseover', function(d,i) {
  6849. if (!interactive) return;
  6850. d3.select(this).classed('hover', true);
  6851. dispatch.elementMouseover({
  6852. data: d,
  6853. index: i,
  6854. color: d3.select(this).style("fill")
  6855. });
  6856. })
  6857. .on('mouseout', function(d,i) {
  6858. if (!interactive) return;
  6859. d3.select(this).classed('hover', false);
  6860. dispatch.elementMouseout({
  6861. data: d,
  6862. index: i,
  6863. color: d3.select(this).style("fill")
  6864. });
  6865. })
  6866. .on('mousemove', function(d,i) {
  6867. if (!interactive) return;
  6868. dispatch.elementMousemove({
  6869. data: d,
  6870. index: i,
  6871. color: d3.select(this).style("fill")
  6872. });
  6873. })
  6874. .on('click', function(d,i) {
  6875. if (!interactive) return;
  6876. var element = this;
  6877. dispatch.elementClick({
  6878. data: d,
  6879. index: i,
  6880. color: d3.select(this).style("fill"),
  6881. event: d3.event,
  6882. element: element
  6883. });
  6884. d3.event.stopPropagation();
  6885. })
  6886. .on('dblclick', function(d,i) {
  6887. if (!interactive) return;
  6888. dispatch.elementDblClick({
  6889. data: d,
  6890. index: i,
  6891. color: d3.select(this).style("fill")
  6892. });
  6893. d3.event.stopPropagation();
  6894. });
  6895. bars
  6896. .attr('fill', function(d,i) { return color(d, i); })
  6897. .attr('class', function(d,i,j) { return (getY(d,i) < 0 ? 'nv-bar negative' : 'nv-bar positive') + ' nv-bar-' + j + '-' + i })
  6898. .watchTransition(renderWatch, 'bars')
  6899. .attr('transform', function(d,i) { return 'translate(' + (x(getX(d,i)) - availableWidth / data[0].values.length * .45) + ',0)'; })
  6900. //TODO: better width calculations that don't assume always uniform data spacing;w
  6901. .attr('width', (availableWidth / data[0].values.length) * .9 );
  6902. bars.watchTransition(renderWatch, 'bars')
  6903. .attr('y', function(d,i) {
  6904. var rval = getY(d,i) < 0 ?
  6905. y(0) :
  6906. y(0) - y(getY(d,i)) < 1 ?
  6907. y(0) - 1 :
  6908. y(getY(d,i));
  6909. return nv.utils.NaNtoZero(rval);
  6910. })
  6911. .attr('height', function(d,i) { return nv.utils.NaNtoZero(Math.max(Math.abs(y(getY(d,i)) - y(0)),1)) });
  6912. });
  6913. renderWatch.renderEnd('historicalBar immediate');
  6914. return chart;
  6915. }
  6916. //Create methods to allow outside functions to highlight a specific bar.
  6917. chart.highlightPoint = function(pointIndex, isHoverOver) {
  6918. container
  6919. .select(".nv-bars .nv-bar-0-" + pointIndex)
  6920. .classed("hover", isHoverOver)
  6921. ;
  6922. };
  6923. chart.clearHighlights = function() {
  6924. container
  6925. .select(".nv-bars .nv-bar.hover")
  6926. .classed("hover", false)
  6927. ;
  6928. };
  6929. //============================================================
  6930. // Expose Public Variables
  6931. //------------------------------------------------------------
  6932. chart.dispatch = dispatch;
  6933. chart.options = nv.utils.optionsFunc.bind(chart);
  6934. chart._options = Object.create({}, {
  6935. // simple options, just get/set the necessary values
  6936. width: {get: function(){return width;}, set: function(_){width=_;}},
  6937. height: {get: function(){return height;}, set: function(_){height=_;}},
  6938. forceX: {get: function(){return forceX;}, set: function(_){forceX=_;}},
  6939. forceY: {get: function(){return forceY;}, set: function(_){forceY=_;}},
  6940. padData: {get: function(){return padData;}, set: function(_){padData=_;}},
  6941. x: {get: function(){return getX;}, set: function(_){getX=_;}},
  6942. y: {get: function(){return getY;}, set: function(_){getY=_;}},
  6943. xScale: {get: function(){return x;}, set: function(_){x=_;}},
  6944. yScale: {get: function(){return y;}, set: function(_){y=_;}},
  6945. xDomain: {get: function(){return xDomain;}, set: function(_){xDomain=_;}},
  6946. yDomain: {get: function(){return yDomain;}, set: function(_){yDomain=_;}},
  6947. xRange: {get: function(){return xRange;}, set: function(_){xRange=_;}},
  6948. yRange: {get: function(){return yRange;}, set: function(_){yRange=_;}},
  6949. clipEdge: {get: function(){return clipEdge;}, set: function(_){clipEdge=_;}},
  6950. id: {get: function(){return id;}, set: function(_){id=_;}},
  6951. interactive: {get: function(){return interactive;}, set: function(_){interactive=_;}},
  6952. // options that require extra logic in the setter
  6953. margin: {get: function(){return margin;}, set: function(_){
  6954. margin.top = _.top !== undefined ? _.top : margin.top;
  6955. margin.right = _.right !== undefined ? _.right : margin.right;
  6956. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  6957. margin.left = _.left !== undefined ? _.left : margin.left;
  6958. }},
  6959. color: {get: function(){return color;}, set: function(_){
  6960. color = nv.utils.getColor(_);
  6961. }}
  6962. });
  6963. nv.utils.initOptions(chart);
  6964. return chart;
  6965. };
  6966. nv.models.historicalBarChart = function(bar_model) {
  6967. "use strict";
  6968. //============================================================
  6969. // Public Variables with Default Settings
  6970. //------------------------------------------------------------
  6971. var bars = bar_model || nv.models.historicalBar()
  6972. , xAxis = nv.models.axis()
  6973. , yAxis = nv.models.axis()
  6974. , legend = nv.models.legend()
  6975. , interactiveLayer = nv.interactiveGuideline()
  6976. , tooltip = nv.models.tooltip()
  6977. ;
  6978. var margin = {top: 30, right: 90, bottom: 50, left: 90}
  6979. , marginTop = null
  6980. , color = nv.utils.defaultColor()
  6981. , width = null
  6982. , height = null
  6983. , showLegend = false
  6984. , showXAxis = true
  6985. , showYAxis = true
  6986. , rightAlignYAxis = false
  6987. , useInteractiveGuideline = false
  6988. , x
  6989. , y
  6990. , state = {}
  6991. , defaultState = null
  6992. , noData = null
  6993. , dispatch = d3.dispatch('tooltipHide', 'stateChange', 'changeState', 'renderEnd')
  6994. , transitionDuration = 250
  6995. ;
  6996. xAxis.orient('bottom').tickPadding(7);
  6997. yAxis.orient( (rightAlignYAxis) ? 'right' : 'left');
  6998. tooltip
  6999. .duration(0)
  7000. .headerEnabled(false)
  7001. .valueFormatter(function(d, i) {
  7002. return yAxis.tickFormat()(d, i);
  7003. })
  7004. .headerFormatter(function(d, i) {
  7005. return xAxis.tickFormat()(d, i);
  7006. });
  7007. //============================================================
  7008. // Private Variables
  7009. //------------------------------------------------------------
  7010. var renderWatch = nv.utils.renderWatch(dispatch, 0);
  7011. function chart(selection) {
  7012. selection.each(function(data) {
  7013. renderWatch.reset();
  7014. renderWatch.models(bars);
  7015. if (showXAxis) renderWatch.models(xAxis);
  7016. if (showYAxis) renderWatch.models(yAxis);
  7017. var container = d3.select(this),
  7018. that = this;
  7019. nv.utils.initSVG(container);
  7020. var availableWidth = nv.utils.availableWidth(width, container, margin),
  7021. availableHeight = nv.utils.availableHeight(height, container, margin);
  7022. chart.update = function() { container.transition().duration(transitionDuration).call(chart) };
  7023. chart.container = this;
  7024. //set state.disabled
  7025. state.disabled = data.map(function(d) { return !!d.disabled });
  7026. if (!defaultState) {
  7027. var key;
  7028. defaultState = {};
  7029. for (key in state) {
  7030. if (state[key] instanceof Array)
  7031. defaultState[key] = state[key].slice(0);
  7032. else
  7033. defaultState[key] = state[key];
  7034. }
  7035. }
  7036. // Display noData message if there's nothing to show.
  7037. if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
  7038. nv.utils.noData(chart, container)
  7039. return chart;
  7040. } else {
  7041. container.selectAll('.nv-noData').remove();
  7042. }
  7043. // Setup Scales
  7044. x = bars.xScale();
  7045. y = bars.yScale();
  7046. // Setup containers and skeleton of chart
  7047. var wrap = container.selectAll('g.nv-wrap.nv-historicalBarChart').data([data]);
  7048. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-historicalBarChart').append('g');
  7049. var g = wrap.select('g');
  7050. gEnter.append('g').attr('class', 'nv-x nv-axis');
  7051. gEnter.append('g').attr('class', 'nv-y nv-axis');
  7052. gEnter.append('g').attr('class', 'nv-barsWrap');
  7053. gEnter.append('g').attr('class', 'nv-legendWrap');
  7054. gEnter.append('g').attr('class', 'nv-interactive');
  7055. // Legend
  7056. if (!showLegend) {
  7057. g.select('.nv-legendWrap').selectAll('*').remove();
  7058. } else {
  7059. legend.width(availableWidth);
  7060. g.select('.nv-legendWrap')
  7061. .datum(data)
  7062. .call(legend);
  7063. if (!marginTop && legend.height() !== margin.top) {
  7064. margin.top = legend.height();
  7065. availableHeight = nv.utils.availableHeight(height, container, margin);
  7066. }
  7067. wrap.select('.nv-legendWrap')
  7068. .attr('transform', 'translate(0,' + (-margin.top) +')')
  7069. }
  7070. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  7071. if (rightAlignYAxis) {
  7072. g.select(".nv-y.nv-axis")
  7073. .attr("transform", "translate(" + availableWidth + ",0)");
  7074. }
  7075. //Set up interactive layer
  7076. if (useInteractiveGuideline) {
  7077. interactiveLayer
  7078. .width(availableWidth)
  7079. .height(availableHeight)
  7080. .margin({left:margin.left, top:margin.top})
  7081. .svgContainer(container)
  7082. .xScale(x);
  7083. wrap.select(".nv-interactive").call(interactiveLayer);
  7084. }
  7085. bars
  7086. .width(availableWidth)
  7087. .height(availableHeight)
  7088. .color(data.map(function(d,i) {
  7089. return d.color || color(d, i);
  7090. }).filter(function(d,i) { return !data[i].disabled }));
  7091. var barsWrap = g.select('.nv-barsWrap')
  7092. .datum(data.filter(function(d) { return !d.disabled }));
  7093. barsWrap.transition().call(bars);
  7094. // Setup Axes
  7095. if (showXAxis) {
  7096. xAxis
  7097. .scale(x)
  7098. ._ticks( nv.utils.calcTicksX(availableWidth/100, data) )
  7099. .tickSize(-availableHeight, 0);
  7100. g.select('.nv-x.nv-axis')
  7101. .attr('transform', 'translate(0,' + y.range()[0] + ')');
  7102. g.select('.nv-x.nv-axis')
  7103. .transition()
  7104. .call(xAxis);
  7105. }
  7106. if (showYAxis) {
  7107. yAxis
  7108. .scale(y)
  7109. ._ticks( nv.utils.calcTicksY(availableHeight/36, data) )
  7110. .tickSize( -availableWidth, 0);
  7111. g.select('.nv-y.nv-axis')
  7112. .transition()
  7113. .call(yAxis);
  7114. }
  7115. //============================================================
  7116. // Event Handling/Dispatching (in chart's scope)
  7117. //------------------------------------------------------------
  7118. interactiveLayer.dispatch.on('elementMousemove', function(e) {
  7119. bars.clearHighlights();
  7120. var singlePoint, pointIndex, pointXLocation, allData = [];
  7121. data
  7122. .filter(function(series, i) {
  7123. series.seriesIndex = i;
  7124. return !series.disabled;
  7125. })
  7126. .forEach(function(series,i) {
  7127. pointIndex = nv.interactiveBisect(series.values, e.pointXValue, chart.x());
  7128. bars.highlightPoint(pointIndex,true);
  7129. var point = series.values[pointIndex];
  7130. if (point === undefined) return;
  7131. if (singlePoint === undefined) singlePoint = point;
  7132. if (pointXLocation === undefined) pointXLocation = chart.xScale()(chart.x()(point,pointIndex));
  7133. allData.push({
  7134. key: series.key,
  7135. value: chart.y()(point, pointIndex),
  7136. color: color(series,series.seriesIndex),
  7137. data: series.values[pointIndex]
  7138. });
  7139. });
  7140. var xValue = xAxis.tickFormat()(chart.x()(singlePoint,pointIndex));
  7141. interactiveLayer.tooltip
  7142. .valueFormatter(function(d,i) {
  7143. return yAxis.tickFormat()(d);
  7144. })
  7145. .data({
  7146. value: xValue,
  7147. index: pointIndex,
  7148. series: allData
  7149. })();
  7150. interactiveLayer.renderGuideLine(pointXLocation);
  7151. });
  7152. interactiveLayer.dispatch.on("elementMouseout",function(e) {
  7153. dispatch.tooltipHide();
  7154. bars.clearHighlights();
  7155. });
  7156. legend.dispatch.on('legendClick', function(d,i) {
  7157. d.disabled = !d.disabled;
  7158. if (!data.filter(function(d) { return !d.disabled }).length) {
  7159. data.map(function(d) {
  7160. d.disabled = false;
  7161. wrap.selectAll('.nv-series').classed('disabled', false);
  7162. return d;
  7163. });
  7164. }
  7165. state.disabled = data.map(function(d) { return !!d.disabled });
  7166. dispatch.stateChange(state);
  7167. selection.transition().call(chart);
  7168. });
  7169. legend.dispatch.on('legendDblclick', function(d) {
  7170. //Double clicking should always enable current series, and disabled all others.
  7171. data.forEach(function(d) {
  7172. d.disabled = true;
  7173. });
  7174. d.disabled = false;
  7175. state.disabled = data.map(function(d) { return !!d.disabled });
  7176. dispatch.stateChange(state);
  7177. chart.update();
  7178. });
  7179. dispatch.on('changeState', function(e) {
  7180. if (typeof e.disabled !== 'undefined') {
  7181. data.forEach(function(series,i) {
  7182. series.disabled = e.disabled[i];
  7183. });
  7184. state.disabled = e.disabled;
  7185. }
  7186. chart.update();
  7187. });
  7188. });
  7189. renderWatch.renderEnd('historicalBarChart immediate');
  7190. return chart;
  7191. }
  7192. //============================================================
  7193. // Event Handling/Dispatching (out of chart's scope)
  7194. //------------------------------------------------------------
  7195. bars.dispatch.on('elementMouseover.tooltip', function(evt) {
  7196. evt['series'] = {
  7197. key: chart.x()(evt.data),
  7198. value: chart.y()(evt.data),
  7199. color: evt.color
  7200. };
  7201. tooltip.data(evt).hidden(false);
  7202. });
  7203. bars.dispatch.on('elementMouseout.tooltip', function(evt) {
  7204. tooltip.hidden(true);
  7205. });
  7206. bars.dispatch.on('elementMousemove.tooltip', function(evt) {
  7207. tooltip();
  7208. });
  7209. //============================================================
  7210. // Expose Public Variables
  7211. //------------------------------------------------------------
  7212. // expose chart's sub-components
  7213. chart.dispatch = dispatch;
  7214. chart.bars = bars;
  7215. chart.legend = legend;
  7216. chart.xAxis = xAxis;
  7217. chart.yAxis = yAxis;
  7218. chart.interactiveLayer = interactiveLayer;
  7219. chart.tooltip = tooltip;
  7220. chart.options = nv.utils.optionsFunc.bind(chart);
  7221. chart._options = Object.create({}, {
  7222. // simple options, just get/set the necessary values
  7223. width: {get: function(){return width;}, set: function(_){width=_;}},
  7224. height: {get: function(){return height;}, set: function(_){height=_;}},
  7225. showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
  7226. showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}},
  7227. showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}},
  7228. defaultState: {get: function(){return defaultState;}, set: function(_){defaultState=_;}},
  7229. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  7230. // options that require extra logic in the setter
  7231. margin: {get: function(){return margin;}, set: function(_){
  7232. if (_.top !== undefined) {
  7233. margin.top = _.top;
  7234. marginTop = _.top;
  7235. }
  7236. margin.right = _.right !== undefined ? _.right : margin.right;
  7237. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  7238. margin.left = _.left !== undefined ? _.left : margin.left;
  7239. }},
  7240. color: {get: function(){return color;}, set: function(_){
  7241. color = nv.utils.getColor(_);
  7242. legend.color(color);
  7243. bars.color(color);
  7244. }},
  7245. duration: {get: function(){return transitionDuration;}, set: function(_){
  7246. transitionDuration=_;
  7247. renderWatch.reset(transitionDuration);
  7248. yAxis.duration(transitionDuration);
  7249. xAxis.duration(transitionDuration);
  7250. }},
  7251. rightAlignYAxis: {get: function(){return rightAlignYAxis;}, set: function(_){
  7252. rightAlignYAxis = _;
  7253. yAxis.orient( (_) ? 'right' : 'left');
  7254. }},
  7255. useInteractiveGuideline: {get: function(){return useInteractiveGuideline;}, set: function(_){
  7256. useInteractiveGuideline = _;
  7257. if (_ === true) {
  7258. chart.interactive(false);
  7259. }
  7260. }}
  7261. });
  7262. nv.utils.inheritOptions(chart, bars);
  7263. nv.utils.initOptions(chart);
  7264. return chart;
  7265. };
  7266. // ohlcChart is just a historical chart with ohlc bars and some tweaks
  7267. nv.models.ohlcBarChart = function() {
  7268. var chart = nv.models.historicalBarChart(nv.models.ohlcBar());
  7269. // special default tooltip since we show multiple values per x
  7270. chart.useInteractiveGuideline(true);
  7271. chart.interactiveLayer.tooltip.contentGenerator(function(data) {
  7272. // we assume only one series exists for this chart
  7273. var d = data.series[0].data;
  7274. // match line colors as defined in nv.d3.css
  7275. var color = d.open < d.close ? "2ca02c" : "d62728";
  7276. return '' +
  7277. '<h3 style="color: #' + color + '">' + data.value + '</h3>' +
  7278. '<table>' +
  7279. '<tr><td>open:</td><td>' + chart.yAxis.tickFormat()(d.open) + '</td></tr>' +
  7280. '<tr><td>close:</td><td>' + chart.yAxis.tickFormat()(d.close) + '</td></tr>' +
  7281. '<tr><td>high</td><td>' + chart.yAxis.tickFormat()(d.high) + '</td></tr>' +
  7282. '<tr><td>low:</td><td>' + chart.yAxis.tickFormat()(d.low) + '</td></tr>' +
  7283. '</table>';
  7284. });
  7285. return chart;
  7286. };
  7287. // candlestickChart is just a historical chart with candlestick bars and some tweaks
  7288. nv.models.candlestickBarChart = function() {
  7289. var chart = nv.models.historicalBarChart(nv.models.candlestickBar());
  7290. // special default tooltip since we show multiple values per x
  7291. chart.useInteractiveGuideline(true);
  7292. chart.interactiveLayer.tooltip.contentGenerator(function(data) {
  7293. // we assume only one series exists for this chart
  7294. var d = data.series[0].data;
  7295. // match line colors as defined in nv.d3.css
  7296. var color = d.open < d.close ? "2ca02c" : "d62728";
  7297. return '' +
  7298. '<h3 style="color: #' + color + '">' + data.value + '</h3>' +
  7299. '<table>' +
  7300. '<tr><td>open:</td><td>' + chart.yAxis.tickFormat()(d.open) + '</td></tr>' +
  7301. '<tr><td>close:</td><td>' + chart.yAxis.tickFormat()(d.close) + '</td></tr>' +
  7302. '<tr><td>high</td><td>' + chart.yAxis.tickFormat()(d.high) + '</td></tr>' +
  7303. '<tr><td>low:</td><td>' + chart.yAxis.tickFormat()(d.low) + '</td></tr>' +
  7304. '</table>';
  7305. });
  7306. return chart;
  7307. };
  7308. nv.models.legend = function() {
  7309. "use strict";
  7310. //============================================================
  7311. // Public Variables with Default Settings
  7312. //------------------------------------------------------------
  7313. var margin = {top: 5, right: 0, bottom: 5, left: 0}
  7314. , width = 400
  7315. , height = 20
  7316. , getKey = function(d) { return d.key }
  7317. , keyFormatter = function (d) { return d }
  7318. , color = nv.utils.getColor()
  7319. , maxKeyLength = 20 //default value for key lengths
  7320. , align = true
  7321. , padding = 32 //define how much space between legend items. - recommend 32 for furious version
  7322. , rightAlign = true
  7323. , updateState = true //If true, legend will update data.disabled and trigger a 'stateChange' dispatch.
  7324. , enableDoubleClick = true //If true, legend will enable double click handling
  7325. , radioButtonMode = false //If true, clicking legend items will cause it to behave like a radio button. (only one can be selected at a time)
  7326. , expanded = false
  7327. , dispatch = d3.dispatch('legendClick', 'legendDblclick', 'legendMouseover', 'legendMouseout', 'stateChange')
  7328. , vers = 'classic' //Options are "classic" and "furious"
  7329. ;
  7330. function chart(selection) {
  7331. selection.each(function(data) {
  7332. var availableWidth = width - margin.left - margin.right,
  7333. container = d3.select(this);
  7334. nv.utils.initSVG(container);
  7335. // Setup containers and skeleton of chart
  7336. var wrap = container.selectAll('g.nv-legend').data([data]);
  7337. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-legend').append('g');
  7338. var g = wrap.select('g');
  7339. if (rightAlign)
  7340. wrap.attr('transform', 'translate(' + (- margin.right) + ',' + margin.top + ')');
  7341. else
  7342. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  7343. var series = g.selectAll('.nv-series')
  7344. .data(function(d) {
  7345. if(vers != 'furious') return d;
  7346. return d.filter(function(n) {
  7347. return expanded ? true : !n.disengaged;
  7348. });
  7349. });
  7350. var seriesEnter = series.enter().append('g').attr('class', 'nv-series');
  7351. var seriesShape;
  7352. var versPadding;
  7353. switch(vers) {
  7354. case 'furious' :
  7355. versPadding = 23;
  7356. break;
  7357. case 'classic' :
  7358. versPadding = 20;
  7359. }
  7360. if(vers == 'classic') {
  7361. seriesEnter.append('circle')
  7362. .style('stroke-width', 2)
  7363. .attr('class','nv-legend-symbol')
  7364. .attr('r', 5);
  7365. seriesShape = series.select('.nv-legend-symbol');
  7366. } else if (vers == 'furious') {
  7367. seriesEnter.append('rect')
  7368. .style('stroke-width', 2)
  7369. .attr('class','nv-legend-symbol')
  7370. .attr('rx', 3)
  7371. .attr('ry', 3);
  7372. seriesShape = series.select('.nv-legend-symbol');
  7373. seriesEnter.append('g')
  7374. .attr('class', 'nv-check-box')
  7375. .property('innerHTML','<path d="M0.5,5 L22.5,5 L22.5,26.5 L0.5,26.5 L0.5,5 Z" class="nv-box"></path><path d="M5.5,12.8618467 L11.9185089,19.2803556 L31,0.198864511" class="nv-check"></path>')
  7376. .attr('transform', 'translate(-10,-8)scale(0.5)');
  7377. var seriesCheckbox = series.select('.nv-check-box');
  7378. seriesCheckbox.each(function(d,i) {
  7379. d3.select(this).selectAll('path')
  7380. .attr('stroke', setTextColor(d,i));
  7381. });
  7382. }
  7383. seriesEnter.append('text')
  7384. .attr('text-anchor', 'start')
  7385. .attr('class','nv-legend-text')
  7386. .attr('dy', '.32em')
  7387. .attr('dx', '8');
  7388. var seriesText = series.select('text.nv-legend-text');
  7389. series
  7390. .on('mouseover', function(d,i) {
  7391. dispatch.legendMouseover(d,i); //TODO: Make consistent with other event objects
  7392. })
  7393. .on('mouseout', function(d,i) {
  7394. dispatch.legendMouseout(d,i);
  7395. })
  7396. .on('click', function(d,i) {
  7397. dispatch.legendClick(d,i);
  7398. // make sure we re-get data in case it was modified
  7399. var data = series.data();
  7400. if (updateState) {
  7401. if(vers =='classic') {
  7402. if (radioButtonMode) {
  7403. //Radio button mode: set every series to disabled,
  7404. // and enable the clicked series.
  7405. data.forEach(function(series) { series.disabled = true});
  7406. d.disabled = false;
  7407. }
  7408. else {
  7409. d.disabled = !d.disabled;
  7410. if (data.every(function(series) { return series.disabled})) {
  7411. //the default behavior of NVD3 legends is, if every single series
  7412. // is disabled, turn all series' back on.
  7413. data.forEach(function(series) { series.disabled = false});
  7414. }
  7415. }
  7416. } else if(vers == 'furious') {
  7417. if(expanded) {
  7418. d.disengaged = !d.disengaged;
  7419. d.userDisabled = d.userDisabled == undefined ? !!d.disabled : d.userDisabled;
  7420. d.disabled = d.disengaged || d.userDisabled;
  7421. } else if (!expanded) {
  7422. d.disabled = !d.disabled;
  7423. d.userDisabled = d.disabled;
  7424. var engaged = data.filter(function(d) { return !d.disengaged; });
  7425. if (engaged.every(function(series) { return series.userDisabled })) {
  7426. //the default behavior of NVD3 legends is, if every single series
  7427. // is disabled, turn all series' back on.
  7428. data.forEach(function(series) {
  7429. series.disabled = series.userDisabled = false;
  7430. });
  7431. }
  7432. }
  7433. }
  7434. dispatch.stateChange({
  7435. disabled: data.map(function(d) { return !!d.disabled }),
  7436. disengaged: data.map(function(d) { return !!d.disengaged })
  7437. });
  7438. }
  7439. })
  7440. .on('dblclick', function(d,i) {
  7441. if (enableDoubleClick) {
  7442. if (vers == 'furious' && expanded) return;
  7443. dispatch.legendDblclick(d, i);
  7444. if (updateState) {
  7445. // make sure we re-get data in case it was modified
  7446. var data = series.data();
  7447. //the default behavior of NVD3 legends, when double clicking one,
  7448. // is to set all other series' to false, and make the double clicked series enabled.
  7449. data.forEach(function (series) {
  7450. series.disabled = true;
  7451. if (vers == 'furious') series.userDisabled = series.disabled;
  7452. });
  7453. d.disabled = false;
  7454. if (vers == 'furious') d.userDisabled = d.disabled;
  7455. dispatch.stateChange({
  7456. disabled: data.map(function (d) {
  7457. return !!d.disabled
  7458. })
  7459. });
  7460. }
  7461. }
  7462. });
  7463. series.classed('nv-disabled', function(d) { return d.userDisabled });
  7464. series.exit().remove();
  7465. seriesText
  7466. .attr('fill', setTextColor)
  7467. .text(function (d) { return keyFormatter(getKey(d)) });
  7468. //TODO: implement fixed-width and max-width options (max-width is especially useful with the align option)
  7469. // NEW ALIGNING CODE, TODO: clean up
  7470. var legendWidth = 0;
  7471. if (align) {
  7472. var seriesWidths = [];
  7473. series.each(function(d,i) {
  7474. var legendText;
  7475. if (keyFormatter(getKey(d)) && keyFormatter(getKey(d)).length > maxKeyLength) {
  7476. var trimmedKey = keyFormatter(getKey(d)).substring(0, maxKeyLength);
  7477. legendText = d3.select(this).select('text').text(trimmedKey + "...");
  7478. d3.select(this).append("svg:title").text(keyFormatter(getKey(d)));
  7479. } else {
  7480. legendText = d3.select(this).select('text');
  7481. }
  7482. var nodeTextLength;
  7483. try {
  7484. nodeTextLength = legendText.node().getComputedTextLength();
  7485. // If the legendText is display:none'd (nodeTextLength == 0), simulate an error so we approximate, instead
  7486. if(nodeTextLength <= 0) throw Error();
  7487. }
  7488. catch(e) {
  7489. nodeTextLength = nv.utils.calcApproxTextWidth(legendText);
  7490. }
  7491. seriesWidths.push(nodeTextLength + padding);
  7492. });
  7493. var seriesPerRow = 0;
  7494. var columnWidths = [];
  7495. legendWidth = 0;
  7496. while ( legendWidth < availableWidth && seriesPerRow < seriesWidths.length) {
  7497. columnWidths[seriesPerRow] = seriesWidths[seriesPerRow];
  7498. legendWidth += seriesWidths[seriesPerRow++];
  7499. }
  7500. if (seriesPerRow === 0) seriesPerRow = 1; //minimum of one series per row
  7501. while ( legendWidth > availableWidth && seriesPerRow > 1 ) {
  7502. columnWidths = [];
  7503. seriesPerRow--;
  7504. for (var k = 0; k < seriesWidths.length; k++) {
  7505. if (seriesWidths[k] > (columnWidths[k % seriesPerRow] || 0) )
  7506. columnWidths[k % seriesPerRow] = seriesWidths[k];
  7507. }
  7508. legendWidth = columnWidths.reduce(function(prev, cur, index, array) {
  7509. return prev + cur;
  7510. });
  7511. }
  7512. var xPositions = [];
  7513. for (var i = 0, curX = 0; i < seriesPerRow; i++) {
  7514. xPositions[i] = curX;
  7515. curX += columnWidths[i];
  7516. }
  7517. series
  7518. .attr('transform', function(d, i) {
  7519. return 'translate(' + xPositions[i % seriesPerRow] + ',' + (5 + Math.floor(i / seriesPerRow) * versPadding) + ')';
  7520. });
  7521. //position legend as far right as possible within the total width
  7522. if (rightAlign) {
  7523. g.attr('transform', 'translate(' + (width - margin.right - legendWidth) + ',' + margin.top + ')');
  7524. }
  7525. else {
  7526. g.attr('transform', 'translate(0' + ',' + margin.top + ')');
  7527. }
  7528. height = margin.top + margin.bottom + (Math.ceil(seriesWidths.length / seriesPerRow) * versPadding);
  7529. } else {
  7530. var ypos = 5,
  7531. newxpos = 5,
  7532. maxwidth = 0,
  7533. xpos;
  7534. series
  7535. .attr('transform', function(d, i) {
  7536. var length = d3.select(this).select('text').node().getComputedTextLength() + padding;
  7537. xpos = newxpos;
  7538. if (width < margin.left + margin.right + xpos + length) {
  7539. newxpos = xpos = 5;
  7540. ypos += versPadding;
  7541. }
  7542. newxpos += length;
  7543. if (newxpos > maxwidth) maxwidth = newxpos;
  7544. if(legendWidth < xpos + maxwidth) {
  7545. legendWidth = xpos + maxwidth;
  7546. }
  7547. return 'translate(' + xpos + ',' + ypos + ')';
  7548. });
  7549. //position legend as far right as possible within the total width
  7550. g.attr('transform', 'translate(' + (width - margin.right - maxwidth) + ',' + margin.top + ')');
  7551. height = margin.top + margin.bottom + ypos + 15;
  7552. }
  7553. if(vers == 'furious') {
  7554. // Size rectangles after text is placed
  7555. seriesShape
  7556. .attr('width', function(d,i) {
  7557. return seriesText[0][i].getComputedTextLength() + 27;
  7558. })
  7559. .attr('height', 18)
  7560. .attr('y', -9)
  7561. .attr('x', -15);
  7562. // The background for the expanded legend (UI)
  7563. gEnter.insert('rect',':first-child')
  7564. .attr('class', 'nv-legend-bg')
  7565. .attr('fill', '#eee')
  7566. // .attr('stroke', '#444')
  7567. .attr('opacity',0);
  7568. var seriesBG = g.select('.nv-legend-bg');
  7569. seriesBG
  7570. .transition().duration(300)
  7571. .attr('x', -versPadding )
  7572. .attr('width', legendWidth + versPadding - 12)
  7573. .attr('height', height + 10)
  7574. .attr('y', -margin.top - 10)
  7575. .attr('opacity', expanded ? 1 : 0);
  7576. }
  7577. seriesShape
  7578. .style('fill', setBGColor)
  7579. .style('fill-opacity', setBGOpacity)
  7580. .style('stroke', setBGColor);
  7581. });
  7582. function setTextColor(d,i) {
  7583. if(vers != 'furious') return '#000';
  7584. if(expanded) {
  7585. return d.disengaged ? '#000' : '#fff';
  7586. } else if (!expanded) {
  7587. if(!d.color) d.color = color(d,i);
  7588. return !!d.disabled ? d.color : '#fff';
  7589. }
  7590. }
  7591. function setBGColor(d,i) {
  7592. if(expanded && vers == 'furious') {
  7593. return d.disengaged ? '#eee' : d.color || color(d,i);
  7594. } else {
  7595. return d.color || color(d,i);
  7596. }
  7597. }
  7598. function setBGOpacity(d,i) {
  7599. if(expanded && vers == 'furious') {
  7600. return 1;
  7601. } else {
  7602. return !!d.disabled ? 0 : 1;
  7603. }
  7604. }
  7605. return chart;
  7606. }
  7607. //============================================================
  7608. // Expose Public Variables
  7609. //------------------------------------------------------------
  7610. chart.dispatch = dispatch;
  7611. chart.options = nv.utils.optionsFunc.bind(chart);
  7612. chart._options = Object.create({}, {
  7613. // simple options, just get/set the necessary values
  7614. width: {get: function(){return width;}, set: function(_){width=_;}},
  7615. height: {get: function(){return height;}, set: function(_){height=_;}},
  7616. key: {get: function(){return getKey;}, set: function(_){getKey=_;}},
  7617. keyFormatter: {get: function(){return keyFormatter;}, set: function(_){keyFormatter=_;}},
  7618. align: {get: function(){return align;}, set: function(_){align=_;}},
  7619. maxKeyLength: {get: function(){return maxKeyLength;}, set: function(_){maxKeyLength=_;}},
  7620. rightAlign: {get: function(){return rightAlign;}, set: function(_){rightAlign=_;}},
  7621. padding: {get: function(){return padding;}, set: function(_){padding=_;}},
  7622. updateState: {get: function(){return updateState;}, set: function(_){updateState=_;}},
  7623. enableDoubleClick: {get: function(){return enableDoubleClick;}, set: function(_){enableDoubleClick=_;}},
  7624. radioButtonMode:{get: function(){return radioButtonMode;}, set: function(_){radioButtonMode=_;}},
  7625. expanded: {get: function(){return expanded;}, set: function(_){expanded=_;}},
  7626. vers: {get: function(){return vers;}, set: function(_){vers=_;}},
  7627. // options that require extra logic in the setter
  7628. margin: {get: function(){return margin;}, set: function(_){
  7629. margin.top = _.top !== undefined ? _.top : margin.top;
  7630. margin.right = _.right !== undefined ? _.right : margin.right;
  7631. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  7632. margin.left = _.left !== undefined ? _.left : margin.left;
  7633. }},
  7634. color: {get: function(){return color;}, set: function(_){
  7635. color = nv.utils.getColor(_);
  7636. }}
  7637. });
  7638. nv.utils.initOptions(chart);
  7639. return chart;
  7640. };
  7641. nv.models.line = function() {
  7642. "use strict";
  7643. //============================================================
  7644. // Public Variables with Default Settings
  7645. //------------------------------------------------------------
  7646. var scatter = nv.models.scatter()
  7647. ;
  7648. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  7649. , width = 960
  7650. , height = 500
  7651. , container = null
  7652. , strokeWidth = 1.5
  7653. , color = nv.utils.defaultColor() // a function that returns a color
  7654. , getX = function(d) { return d.x } // accessor to get the x value from a data point
  7655. , getY = function(d) { return d.y } // accessor to get the y value from a data point
  7656. , defined = function(d,i) { return !isNaN(getY(d,i)) && getY(d,i) !== null } // allows a line to be not continuous when it is not defined
  7657. , isArea = function(d) { return d.area } // decides if a line is an area or just a line
  7658. , clipEdge = false // if true, masks lines within x and y scale
  7659. , x //can be accessed via chart.xScale()
  7660. , y //can be accessed via chart.yScale()
  7661. , interpolate = "linear" // controls the line interpolation
  7662. , duration = 250
  7663. , dispatch = d3.dispatch('elementClick', 'elementMouseover', 'elementMouseout', 'renderEnd')
  7664. ;
  7665. scatter
  7666. .pointSize(16) // default size
  7667. .pointDomain([16,256]) //set to speed up calculation, needs to be unset if there is a custom size accessor
  7668. ;
  7669. //============================================================
  7670. //============================================================
  7671. // Private Variables
  7672. //------------------------------------------------------------
  7673. var x0, y0 //used to store previous scales
  7674. , renderWatch = nv.utils.renderWatch(dispatch, duration)
  7675. ;
  7676. //============================================================
  7677. function chart(selection) {
  7678. renderWatch.reset();
  7679. renderWatch.models(scatter);
  7680. selection.each(function(data) {
  7681. container = d3.select(this);
  7682. var availableWidth = nv.utils.availableWidth(width, container, margin),
  7683. availableHeight = nv.utils.availableHeight(height, container, margin);
  7684. nv.utils.initSVG(container);
  7685. // Setup Scales
  7686. x = scatter.xScale();
  7687. y = scatter.yScale();
  7688. x0 = x0 || x;
  7689. y0 = y0 || y;
  7690. // Setup containers and skeleton of chart
  7691. var wrap = container.selectAll('g.nv-wrap.nv-line').data([data]);
  7692. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-line');
  7693. var defsEnter = wrapEnter.append('defs');
  7694. var gEnter = wrapEnter.append('g');
  7695. var g = wrap.select('g');
  7696. gEnter.append('g').attr('class', 'nv-groups');
  7697. gEnter.append('g').attr('class', 'nv-scatterWrap');
  7698. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  7699. scatter
  7700. .width(availableWidth)
  7701. .height(availableHeight);
  7702. var scatterWrap = wrap.select('.nv-scatterWrap');
  7703. scatterWrap.call(scatter);
  7704. defsEnter.append('clipPath')
  7705. .attr('id', 'nv-edge-clip-' + scatter.id())
  7706. .append('rect');
  7707. wrap.select('#nv-edge-clip-' + scatter.id() + ' rect')
  7708. .attr('width', availableWidth)
  7709. .attr('height', (availableHeight > 0) ? availableHeight : 0);
  7710. g .attr('clip-path', clipEdge ? 'url(#nv-edge-clip-' + scatter.id() + ')' : '');
  7711. scatterWrap
  7712. .attr('clip-path', clipEdge ? 'url(#nv-edge-clip-' + scatter.id() + ')' : '');
  7713. var groups = wrap.select('.nv-groups').selectAll('.nv-group')
  7714. .data(function(d) { return d }, function(d) { return d.key });
  7715. groups.enter().append('g')
  7716. .style('stroke-opacity', 1e-6)
  7717. .style('stroke-width', function(d) { return d.strokeWidth || strokeWidth })
  7718. .style('fill-opacity', 1e-6);
  7719. groups.exit().remove();
  7720. groups
  7721. .attr('class', function(d,i) {
  7722. return (d.classed || '') + ' nv-group nv-series-' + i;
  7723. })
  7724. .classed('hover', function(d) { return d.hover })
  7725. .style('fill', function(d,i){ return color(d, i) })
  7726. .style('stroke', function(d,i){ return color(d, i)});
  7727. groups.watchTransition(renderWatch, 'line: groups')
  7728. .style('stroke-opacity', 1)
  7729. .style('fill-opacity', function(d) { return d.fillOpacity || .5});
  7730. var areaPaths = groups.selectAll('path.nv-area')
  7731. .data(function(d) { return isArea(d) ? [d] : [] }); // this is done differently than lines because I need to check if series is an area
  7732. areaPaths.enter().append('path')
  7733. .attr('class', 'nv-area')
  7734. .attr('d', function(d) {
  7735. return d3.svg.area()
  7736. .interpolate(interpolate)
  7737. .defined(defined)
  7738. .x(function(d,i) { return nv.utils.NaNtoZero(x0(getX(d,i))) })
  7739. .y0(function(d,i) { return nv.utils.NaNtoZero(y0(getY(d,i))) })
  7740. .y1(function(d,i) { return y0( y.domain()[0] <= 0 ? y.domain()[1] >= 0 ? 0 : y.domain()[1] : y.domain()[0] ) })
  7741. //.y1(function(d,i) { return y0(0) }) //assuming 0 is within y domain.. may need to tweak this
  7742. .apply(this, [d.values])
  7743. });
  7744. groups.exit().selectAll('path.nv-area')
  7745. .remove();
  7746. areaPaths.watchTransition(renderWatch, 'line: areaPaths')
  7747. .attr('d', function(d) {
  7748. return d3.svg.area()
  7749. .interpolate(interpolate)
  7750. .defined(defined)
  7751. .x(function(d,i) { return nv.utils.NaNtoZero(x(getX(d,i))) })
  7752. .y0(function(d,i) { return nv.utils.NaNtoZero(y(getY(d,i))) })
  7753. .y1(function(d,i) { return y( y.domain()[0] <= 0 ? y.domain()[1] >= 0 ? 0 : y.domain()[1] : y.domain()[0] ) })
  7754. //.y1(function(d,i) { return y0(0) }) //assuming 0 is within y domain.. may need to tweak this
  7755. .apply(this, [d.values])
  7756. });
  7757. var linePaths = groups.selectAll('path.nv-line')
  7758. .data(function(d) { return [d.values] });
  7759. linePaths.enter().append('path')
  7760. .attr('class', 'nv-line')
  7761. .attr('d',
  7762. d3.svg.line()
  7763. .interpolate(interpolate)
  7764. .defined(defined)
  7765. .x(function(d,i) { return nv.utils.NaNtoZero(x0(getX(d,i))) })
  7766. .y(function(d,i) { return nv.utils.NaNtoZero(y0(getY(d,i))) })
  7767. );
  7768. linePaths.watchTransition(renderWatch, 'line: linePaths')
  7769. .attr('d',
  7770. d3.svg.line()
  7771. .interpolate(interpolate)
  7772. .defined(defined)
  7773. .x(function(d,i) { return nv.utils.NaNtoZero(x(getX(d,i))) })
  7774. .y(function(d,i) { return nv.utils.NaNtoZero(y(getY(d,i))) })
  7775. );
  7776. //store old scales for use in transitions on update
  7777. x0 = x.copy();
  7778. y0 = y.copy();
  7779. });
  7780. renderWatch.renderEnd('line immediate');
  7781. return chart;
  7782. }
  7783. //============================================================
  7784. // Expose Public Variables
  7785. //------------------------------------------------------------
  7786. chart.dispatch = dispatch;
  7787. chart.scatter = scatter;
  7788. // Pass through events
  7789. scatter.dispatch.on('elementClick', function(){ dispatch.elementClick.apply(this, arguments); });
  7790. scatter.dispatch.on('elementMouseover', function(){ dispatch.elementMouseover.apply(this, arguments); });
  7791. scatter.dispatch.on('elementMouseout', function(){ dispatch.elementMouseout.apply(this, arguments); });
  7792. chart.options = nv.utils.optionsFunc.bind(chart);
  7793. chart._options = Object.create({}, {
  7794. // simple options, just get/set the necessary values
  7795. width: {get: function(){return width;}, set: function(_){width=_;}},
  7796. height: {get: function(){return height;}, set: function(_){height=_;}},
  7797. defined: {get: function(){return defined;}, set: function(_){defined=_;}},
  7798. interpolate: {get: function(){return interpolate;}, set: function(_){interpolate=_;}},
  7799. clipEdge: {get: function(){return clipEdge;}, set: function(_){clipEdge=_;}},
  7800. // options that require extra logic in the setter
  7801. margin: {get: function(){return margin;}, set: function(_){
  7802. margin.top = _.top !== undefined ? _.top : margin.top;
  7803. margin.right = _.right !== undefined ? _.right : margin.right;
  7804. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  7805. margin.left = _.left !== undefined ? _.left : margin.left;
  7806. }},
  7807. duration: {get: function(){return duration;}, set: function(_){
  7808. duration = _;
  7809. renderWatch.reset(duration);
  7810. scatter.duration(duration);
  7811. }},
  7812. isArea: {get: function(){return isArea;}, set: function(_){
  7813. isArea = d3.functor(_);
  7814. }},
  7815. x: {get: function(){return getX;}, set: function(_){
  7816. getX = _;
  7817. scatter.x(_);
  7818. }},
  7819. y: {get: function(){return getY;}, set: function(_){
  7820. getY = _;
  7821. scatter.y(_);
  7822. }},
  7823. color: {get: function(){return color;}, set: function(_){
  7824. color = nv.utils.getColor(_);
  7825. scatter.color(color);
  7826. }}
  7827. });
  7828. nv.utils.inheritOptions(chart, scatter);
  7829. nv.utils.initOptions(chart);
  7830. return chart;
  7831. };
  7832. nv.models.lineChart = function() {
  7833. "use strict";
  7834. //============================================================
  7835. // Public Variables with Default Settings
  7836. //------------------------------------------------------------
  7837. var lines = nv.models.line()
  7838. , xAxis = nv.models.axis()
  7839. , yAxis = nv.models.axis()
  7840. , legend = nv.models.legend()
  7841. , interactiveLayer = nv.interactiveGuideline()
  7842. , tooltip = nv.models.tooltip()
  7843. , focus = nv.models.focus(nv.models.line())
  7844. ;
  7845. var margin = {top: 30, right: 20, bottom: 50, left: 60}
  7846. , marginTop = null
  7847. , color = nv.utils.defaultColor()
  7848. , width = null
  7849. , height = null
  7850. , showLegend = true
  7851. , legendPosition = 'top'
  7852. , showXAxis = true
  7853. , showYAxis = true
  7854. , rightAlignYAxis = false
  7855. , useInteractiveGuideline = false
  7856. , x
  7857. , y
  7858. , focusEnable = false
  7859. , state = nv.utils.state()
  7860. , defaultState = null
  7861. , noData = null
  7862. , dispatch = d3.dispatch('stateChange', 'changeState', 'renderEnd')
  7863. , duration = 250
  7864. ;
  7865. // set options on sub-objects for this chart
  7866. xAxis.orient('bottom').tickPadding(7);
  7867. yAxis.orient(rightAlignYAxis ? 'right' : 'left');
  7868. lines.clipEdge(true).duration(0);
  7869. tooltip.valueFormatter(function(d, i) {
  7870. return yAxis.tickFormat()(d, i);
  7871. }).headerFormatter(function(d, i) {
  7872. return xAxis.tickFormat()(d, i);
  7873. });
  7874. interactiveLayer.tooltip.valueFormatter(function(d, i) {
  7875. return yAxis.tickFormat()(d, i);
  7876. }).headerFormatter(function(d, i) {
  7877. return xAxis.tickFormat()(d, i);
  7878. });
  7879. //============================================================
  7880. // Private Variables
  7881. //------------------------------------------------------------
  7882. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  7883. var stateGetter = function(data) {
  7884. return function(){
  7885. return {
  7886. active: data.map(function(d) { return !d.disabled; })
  7887. };
  7888. };
  7889. };
  7890. var stateSetter = function(data) {
  7891. return function(state) {
  7892. if (state.active !== undefined)
  7893. data.forEach(function(series,i) {
  7894. series.disabled = !state.active[i];
  7895. });
  7896. };
  7897. };
  7898. function chart(selection) {
  7899. renderWatch.reset();
  7900. renderWatch.models(lines);
  7901. if (showXAxis) renderWatch.models(xAxis);
  7902. if (showYAxis) renderWatch.models(yAxis);
  7903. selection.each(function(data) {
  7904. var container = d3.select(this);
  7905. nv.utils.initSVG(container);
  7906. var availableWidth = nv.utils.availableWidth(width, container, margin),
  7907. availableHeight = nv.utils.availableHeight(height, container, margin) - (focusEnable ? focus.height() : 0);
  7908. chart.update = function() {
  7909. if( duration === 0 ) {
  7910. container.call( chart );
  7911. } else {
  7912. container.transition().duration(duration).call(chart);
  7913. }
  7914. };
  7915. chart.container = this;
  7916. state
  7917. .setter(stateSetter(data), chart.update)
  7918. .getter(stateGetter(data))
  7919. .update();
  7920. // DEPRECATED set state.disabled
  7921. state.disabled = data.map(function(d) { return !!d.disabled; });
  7922. if (!defaultState) {
  7923. var key;
  7924. defaultState = {};
  7925. for (key in state) {
  7926. if (state[key] instanceof Array)
  7927. defaultState[key] = state[key].slice(0);
  7928. else
  7929. defaultState[key] = state[key];
  7930. }
  7931. }
  7932. // Display noData message if there's nothing to show.
  7933. if (!data || !data.length || !data.filter(function(d) { return d.values.length; }).length) {
  7934. nv.utils.noData(chart, container);
  7935. return chart;
  7936. } else {
  7937. container.selectAll('.nv-noData').remove();
  7938. }
  7939. /* Update `main' graph on brush update. */
  7940. focus.dispatch.on("onBrush", function(extent) {
  7941. onBrush(extent);
  7942. });
  7943. // Setup Scales
  7944. x = lines.xScale();
  7945. y = lines.yScale();
  7946. // Setup containers and skeleton of chart
  7947. var wrap = container.selectAll('g.nv-wrap.nv-lineChart').data([data]);
  7948. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-lineChart').append('g');
  7949. var g = wrap.select('g');
  7950. gEnter.append('g').attr('class', 'nv-legendWrap');
  7951. var focusEnter = gEnter.append('g').attr('class', 'nv-focus');
  7952. focusEnter.append('g').attr('class', 'nv-background').append('rect');
  7953. focusEnter.append('g').attr('class', 'nv-x nv-axis');
  7954. focusEnter.append('g').attr('class', 'nv-y nv-axis');
  7955. focusEnter.append('g').attr('class', 'nv-linesWrap');
  7956. focusEnter.append('g').attr('class', 'nv-interactive');
  7957. var contextEnter = gEnter.append('g').attr('class', 'nv-focusWrap');
  7958. // Legend
  7959. if (!showLegend) {
  7960. g.select('.nv-legendWrap').selectAll('*').remove();
  7961. } else {
  7962. legend.width(availableWidth);
  7963. g.select('.nv-legendWrap')
  7964. .datum(data)
  7965. .call(legend);
  7966. if (legendPosition === 'bottom') {
  7967. margin.bottom = xAxis.height() + legend.height();
  7968. availableHeight = nv.utils.availableHeight(height, container, margin);
  7969. g.select('.nv-legendWrap')
  7970. .attr('transform', 'translate(0,' + (availableHeight + xAxis.height()) +')');
  7971. } else if (legendPosition === 'top') {
  7972. if (!marginTop && legend.height() !== margin.top) {
  7973. margin.top = legend.height();
  7974. availableHeight = nv.utils.availableHeight(height, container, margin) - (focusEnable ? focus.height() : 0);
  7975. }
  7976. wrap.select('.nv-legendWrap')
  7977. .attr('transform', 'translate(0,' + (-margin.top) +')');
  7978. }
  7979. }
  7980. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  7981. if (rightAlignYAxis) {
  7982. g.select(".nv-y.nv-axis")
  7983. .attr("transform", "translate(" + availableWidth + ",0)");
  7984. }
  7985. //Set up interactive layer
  7986. if (useInteractiveGuideline) {
  7987. interactiveLayer
  7988. .width(availableWidth)
  7989. .height(availableHeight)
  7990. .margin({left:margin.left, top:margin.top})
  7991. .svgContainer(container)
  7992. .xScale(x);
  7993. wrap.select(".nv-interactive").call(interactiveLayer);
  7994. }
  7995. g.select('.nv-focus .nv-background rect')
  7996. .attr('width', availableWidth)
  7997. .attr('height', availableHeight);
  7998. lines
  7999. .width(availableWidth)
  8000. .height(availableHeight)
  8001. .color(data.map(function(d,i) {
  8002. return d.color || color(d, i);
  8003. }).filter(function(d,i) { return !data[i].disabled; }));
  8004. var linesWrap = g.select('.nv-linesWrap')
  8005. .datum(data.filter(function(d) { return !d.disabled; }));
  8006. // Setup Main (Focus) Axes
  8007. if (showXAxis) {
  8008. xAxis
  8009. .scale(x)
  8010. ._ticks(nv.utils.calcTicksX(availableWidth/100, data) )
  8011. .tickSize(-availableHeight, 0);
  8012. }
  8013. if (showYAxis) {
  8014. yAxis
  8015. .scale(y)
  8016. ._ticks( nv.utils.calcTicksY(availableHeight/36, data) )
  8017. .tickSize( -availableWidth, 0);
  8018. }
  8019. //============================================================
  8020. // Update Axes
  8021. //============================================================
  8022. function updateXAxis() {
  8023. if(showXAxis) {
  8024. g.select('.nv-focus .nv-x.nv-axis')
  8025. .transition()
  8026. .duration(duration)
  8027. .call(xAxis)
  8028. ;
  8029. }
  8030. }
  8031. function updateYAxis() {
  8032. if(showYAxis) {
  8033. g.select('.nv-focus .nv-y.nv-axis')
  8034. .transition()
  8035. .duration(duration)
  8036. .call(yAxis)
  8037. ;
  8038. }
  8039. }
  8040. g.select('.nv-focus .nv-x.nv-axis')
  8041. .attr('transform', 'translate(0,' + availableHeight + ')');
  8042. //============================================================
  8043. // Update Focus
  8044. //============================================================
  8045. if (!focusEnable && focus.brush.extent() === null) {
  8046. linesWrap.transition().call(lines);
  8047. updateXAxis();
  8048. updateYAxis();
  8049. } else {
  8050. focus.width(availableWidth);
  8051. g.select('.nv-focusWrap')
  8052. .style('display', focusEnable ? 'initial' : 'none')
  8053. .attr('transform', 'translate(0,' + ( availableHeight + margin.bottom + focus.margin().top) + ')')
  8054. .call(focus);
  8055. var extent = focus.brush.empty() ? focus.xDomain() : focus.brush.extent();
  8056. if (extent !== null) {
  8057. onBrush(extent);
  8058. }
  8059. }
  8060. //============================================================
  8061. // Event Handling/Dispatching (in chart's scope)
  8062. //------------------------------------------------------------
  8063. legend.dispatch.on('stateChange', function(newState) {
  8064. for (var key in newState)
  8065. state[key] = newState[key];
  8066. dispatch.stateChange(state);
  8067. chart.update();
  8068. });
  8069. interactiveLayer.dispatch.on('elementMousemove', function(e) {
  8070. lines.clearHighlights();
  8071. var singlePoint, pointIndex, pointXLocation, allData = [];
  8072. data
  8073. .filter(function(series, i) {
  8074. series.seriesIndex = i;
  8075. return !series.disabled && !series.disableTooltip;
  8076. })
  8077. .forEach(function(series,i) {
  8078. var extent = focus.brush.extent() !== null ? (focus.brush.empty() ? focus.xScale().domain() : focus.brush.extent()) : x.domain();
  8079. var currentValues = series.values.filter(function(d,i) {
  8080. // Checks if the x point is between the extents, handling case where extent[0] is greater than extent[1]
  8081. // (e.g. x domain is manually set to reverse the x-axis)
  8082. if(extent[0] <= extent[1]) {
  8083. return lines.x()(d,i) >= extent[0] && lines.x()(d,i) <= extent[1];
  8084. } else {
  8085. return lines.x()(d,i) >= extent[1] && lines.x()(d,i) <= extent[0];
  8086. }
  8087. });
  8088. if (currentValues.length > 0) {
  8089. pointIndex = nv.interactiveBisect(currentValues, e.pointXValue, lines.x());
  8090. var point = currentValues[pointIndex];
  8091. var pointYValue = chart.y()(point, pointIndex);
  8092. if (pointYValue !== null) {
  8093. lines.highlightPoint(i, series.values.indexOf(point), true);
  8094. }
  8095. if (point === undefined) return;
  8096. if (singlePoint === undefined) singlePoint = point;
  8097. if (pointXLocation === undefined) pointXLocation = chart.xScale()(chart.x()(point,pointIndex));
  8098. allData.push({
  8099. key: series.key,
  8100. value: pointYValue,
  8101. color: color(series,series.seriesIndex),
  8102. data: point
  8103. });
  8104. }
  8105. });
  8106. //Highlight the tooltip entry based on which point the mouse is closest to.
  8107. if (allData.length > 2) {
  8108. var yValue = chart.yScale().invert(e.mouseY);
  8109. var domainExtent = Math.abs(chart.yScale().domain()[0] - chart.yScale().domain()[1]);
  8110. var threshold = 0.03 * domainExtent;
  8111. var indexToHighlight = nv.nearestValueIndex(allData.map(function(d){return d.value;}),yValue,threshold);
  8112. if (indexToHighlight !== null)
  8113. allData[indexToHighlight].highlight = true;
  8114. }
  8115. var defaultValueFormatter = function(d,i) {
  8116. return d == null ? "N/A" : yAxis.tickFormat()(d);
  8117. };
  8118. if (typeof pointIndex !== 'undefined') {
  8119. interactiveLayer.tooltip
  8120. .valueFormatter(interactiveLayer.tooltip.valueFormatter() || defaultValueFormatter)
  8121. .data({
  8122. value: chart.x()( singlePoint,pointIndex ),
  8123. index: pointIndex,
  8124. series: allData
  8125. })();
  8126. interactiveLayer.renderGuideLine(pointXLocation);
  8127. }
  8128. });
  8129. interactiveLayer.dispatch.on('elementClick', function(e) {
  8130. var pointXLocation, allData = [];
  8131. data.filter(function(series, i) {
  8132. series.seriesIndex = i;
  8133. return !series.disabled;
  8134. }).forEach(function(series) {
  8135. var pointIndex = nv.interactiveBisect(series.values, e.pointXValue, chart.x());
  8136. var point = series.values[pointIndex];
  8137. if (typeof point === 'undefined') return;
  8138. if (typeof pointXLocation === 'undefined') pointXLocation = chart.xScale()(chart.x()(point,pointIndex));
  8139. var yPos = chart.yScale()(chart.y()(point,pointIndex));
  8140. allData.push({
  8141. point: point,
  8142. pointIndex: pointIndex,
  8143. pos: [pointXLocation, yPos],
  8144. seriesIndex: series.seriesIndex,
  8145. series: series
  8146. });
  8147. });
  8148. lines.dispatch.elementClick(allData);
  8149. });
  8150. interactiveLayer.dispatch.on("elementMouseout",function(e) {
  8151. lines.clearHighlights();
  8152. });
  8153. dispatch.on('changeState', function(e) {
  8154. if (typeof e.disabled !== 'undefined' && data.length === e.disabled.length) {
  8155. data.forEach(function(series,i) {
  8156. series.disabled = e.disabled[i];
  8157. });
  8158. state.disabled = e.disabled;
  8159. }
  8160. chart.update();
  8161. });
  8162. //============================================================
  8163. // Functions
  8164. //------------------------------------------------------------
  8165. // Taken from crossfilter (http://square.github.com/crossfilter/)
  8166. function resizePath(d) {
  8167. var e = +(d == 'e'),
  8168. x = e ? 1 : -1,
  8169. y = availableHeight / 3;
  8170. return 'M' + (0.5 * x) + ',' + y
  8171. + 'A6,6 0 0 ' + e + ' ' + (6.5 * x) + ',' + (y + 6)
  8172. + 'V' + (2 * y - 6)
  8173. + 'A6,6 0 0 ' + e + ' ' + (0.5 * x) + ',' + (2 * y)
  8174. + 'Z'
  8175. + 'M' + (2.5 * x) + ',' + (y + 8)
  8176. + 'V' + (2 * y - 8)
  8177. + 'M' + (4.5 * x) + ',' + (y + 8)
  8178. + 'V' + (2 * y - 8);
  8179. }
  8180. function onBrush(extent) {
  8181. // Update Main (Focus)
  8182. var focusLinesWrap = g.select('.nv-focus .nv-linesWrap')
  8183. .datum(
  8184. data.filter(function(d) { return !d.disabled; })
  8185. .map(function(d,i) {
  8186. return {
  8187. key: d.key,
  8188. area: d.area,
  8189. classed: d.classed,
  8190. values: d.values.filter(function(d,i) {
  8191. return lines.x()(d,i) >= extent[0] && lines.x()(d,i) <= extent[1];
  8192. }),
  8193. disableTooltip: d.disableTooltip
  8194. };
  8195. })
  8196. );
  8197. focusLinesWrap.transition().duration(duration).call(lines);
  8198. // Update Main (Focus) Axes
  8199. updateXAxis();
  8200. updateYAxis();
  8201. }
  8202. });
  8203. renderWatch.renderEnd('lineChart immediate');
  8204. return chart;
  8205. }
  8206. //============================================================
  8207. // Event Handling/Dispatching (out of chart's scope)
  8208. //------------------------------------------------------------
  8209. lines.dispatch.on('elementMouseover.tooltip', function(evt) {
  8210. if(!evt.series.disableTooltip){
  8211. tooltip.data(evt).hidden(false);
  8212. }
  8213. });
  8214. lines.dispatch.on('elementMouseout.tooltip', function(evt) {
  8215. tooltip.hidden(true);
  8216. });
  8217. //============================================================
  8218. // Expose Public Variables
  8219. //------------------------------------------------------------
  8220. // expose chart's sub-components
  8221. chart.dispatch = dispatch;
  8222. chart.lines = lines;
  8223. chart.legend = legend;
  8224. chart.focus = focus;
  8225. chart.xAxis = xAxis;
  8226. chart.x2Axis = focus.xAxis
  8227. chart.yAxis = yAxis;
  8228. chart.y2Axis = focus.yAxis
  8229. chart.interactiveLayer = interactiveLayer;
  8230. chart.tooltip = tooltip;
  8231. chart.state = state;
  8232. chart.dispatch = dispatch;
  8233. chart.options = nv.utils.optionsFunc.bind(chart);
  8234. chart._options = Object.create({}, {
  8235. // simple options, just get/set the necessary values
  8236. width: {get: function(){return width;}, set: function(_){width=_;}},
  8237. height: {get: function(){return height;}, set: function(_){height=_;}},
  8238. showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
  8239. legendPosition: {get: function(){return legendPosition;}, set: function(_){legendPosition=_;}},
  8240. showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}},
  8241. showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}},
  8242. defaultState: {get: function(){return defaultState;}, set: function(_){defaultState=_;}},
  8243. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  8244. // Focus options, mostly passed onto focus model.
  8245. focusEnable: {get: function(){return focusEnable;}, set: function(_){focusEnable=_;}},
  8246. focusHeight: {get: function(){return focus.height();}, set: function(_){focus.height(_);}},
  8247. focusShowAxisX: {get: function(){return focus.showXAxis();}, set: function(_){focus.showXAxis(_);}},
  8248. focusShowAxisY: {get: function(){return focus.showYAxis();}, set: function(_){focus.showYAxis(_);}},
  8249. brushExtent: {get: function(){return focus.brushExtent();}, set: function(_){focus.brushExtent(_);}},
  8250. // options that require extra logic in the setter
  8251. focusMargin: {get: function(){return focus.margin}, set: function(_){
  8252. if (_.top !== undefined) {
  8253. margin.top = _.top;
  8254. marginTop = _.top;
  8255. }
  8256. focus.margin.right = _.right !== undefined ? _.right : focus.margin.right;
  8257. focus.margin.bottom = _.bottom !== undefined ? _.bottom : focus.margin.bottom;
  8258. focus.margin.left = _.left !== undefined ? _.left : focus.margin.left;
  8259. }},
  8260. margin: {get: function(){return margin;}, set: function(_){
  8261. margin.top = _.top !== undefined ? _.top : margin.top;
  8262. margin.right = _.right !== undefined ? _.right : margin.right;
  8263. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  8264. margin.left = _.left !== undefined ? _.left : margin.left;
  8265. }},
  8266. duration: {get: function(){return duration;}, set: function(_){
  8267. duration = _;
  8268. renderWatch.reset(duration);
  8269. lines.duration(duration);
  8270. focus.duration(duration);
  8271. xAxis.duration(duration);
  8272. yAxis.duration(duration);
  8273. }},
  8274. color: {get: function(){return color;}, set: function(_){
  8275. color = nv.utils.getColor(_);
  8276. legend.color(color);
  8277. lines.color(color);
  8278. focus.color(color);
  8279. }},
  8280. interpolate: {get: function(){return lines.interpolate();}, set: function(_){
  8281. lines.interpolate(_);
  8282. focus.interpolate(_);
  8283. }},
  8284. xTickFormat: {get: function(){return xAxis.tickFormat();}, set: function(_){
  8285. xAxis.tickFormat(_);
  8286. focus.xTickFormat(_);
  8287. }},
  8288. yTickFormat: {get: function(){return yAxis.tickFormat();}, set: function(_){
  8289. yAxis.tickFormat(_);
  8290. focus.yTickFormat(_);
  8291. }},
  8292. x: {get: function(){return lines.x();}, set: function(_){
  8293. lines.x(_);
  8294. focus.x(_);
  8295. }},
  8296. y: {get: function(){return lines.y();}, set: function(_){
  8297. lines.y(_);
  8298. focus.y(_);
  8299. }},
  8300. rightAlignYAxis: {get: function(){return rightAlignYAxis;}, set: function(_){
  8301. rightAlignYAxis = _;
  8302. yAxis.orient( rightAlignYAxis ? 'right' : 'left');
  8303. }},
  8304. useInteractiveGuideline: {get: function(){return useInteractiveGuideline;}, set: function(_){
  8305. useInteractiveGuideline = _;
  8306. if (useInteractiveGuideline) {
  8307. lines.interactive(false);
  8308. lines.useVoronoi(false);
  8309. }
  8310. }}
  8311. });
  8312. nv.utils.inheritOptions(chart, lines);
  8313. nv.utils.initOptions(chart);
  8314. return chart;
  8315. };
  8316. nv.models.lineWithFocusChart = function() {
  8317. return nv.models.lineChart()
  8318. .margin({ bottom: 30 })
  8319. .focusEnable( true );
  8320. };
  8321. nv.models.linePlusBarChart = function() {
  8322. "use strict";
  8323. //============================================================
  8324. // Public Variables with Default Settings
  8325. //------------------------------------------------------------
  8326. var lines = nv.models.line()
  8327. , lines2 = nv.models.line()
  8328. , bars = nv.models.historicalBar()
  8329. , bars2 = nv.models.historicalBar()
  8330. , xAxis = nv.models.axis()
  8331. , x2Axis = nv.models.axis()
  8332. , y1Axis = nv.models.axis()
  8333. , y2Axis = nv.models.axis()
  8334. , y3Axis = nv.models.axis()
  8335. , y4Axis = nv.models.axis()
  8336. , legend = nv.models.legend()
  8337. , brush = d3.svg.brush()
  8338. , tooltip = nv.models.tooltip()
  8339. ;
  8340. var margin = {top: 30, right: 30, bottom: 30, left: 60}
  8341. , marginTop = null
  8342. , margin2 = {top: 0, right: 30, bottom: 20, left: 60}
  8343. , width = null
  8344. , height = null
  8345. , getX = function(d) { return d.x }
  8346. , getY = function(d) { return d.y }
  8347. , color = nv.utils.defaultColor()
  8348. , showLegend = true
  8349. , focusEnable = true
  8350. , focusShowAxisY = false
  8351. , focusShowAxisX = true
  8352. , focusHeight = 50
  8353. , extent
  8354. , brushExtent = null
  8355. , x
  8356. , x2
  8357. , y1
  8358. , y2
  8359. , y3
  8360. , y4
  8361. , noData = null
  8362. , dispatch = d3.dispatch('brush', 'stateChange', 'changeState')
  8363. , transitionDuration = 0
  8364. , state = nv.utils.state()
  8365. , defaultState = null
  8366. , legendLeftAxisHint = ' (left axis)'
  8367. , legendRightAxisHint = ' (right axis)'
  8368. , switchYAxisOrder = false
  8369. ;
  8370. lines.clipEdge(true);
  8371. lines2.interactive(false);
  8372. // We don't want any points emitted for the focus chart's scatter graph.
  8373. lines2.pointActive(function(d) { return false });
  8374. xAxis.orient('bottom').tickPadding(5);
  8375. y1Axis.orient('left');
  8376. y2Axis.orient('right');
  8377. x2Axis.orient('bottom').tickPadding(5);
  8378. y3Axis.orient('left');
  8379. y4Axis.orient('right');
  8380. tooltip.headerEnabled(true).headerFormatter(function(d, i) {
  8381. return xAxis.tickFormat()(d, i);
  8382. });
  8383. //============================================================
  8384. // Private Variables
  8385. //------------------------------------------------------------
  8386. var getBarsAxis = function() {
  8387. return switchYAxisOrder
  8388. ? { main: y2Axis, focus: y4Axis }
  8389. : { main: y1Axis, focus: y3Axis }
  8390. }
  8391. var getLinesAxis = function() {
  8392. return switchYAxisOrder
  8393. ? { main: y1Axis, focus: y3Axis }
  8394. : { main: y2Axis, focus: y4Axis }
  8395. }
  8396. var stateGetter = function(data) {
  8397. return function(){
  8398. return {
  8399. active: data.map(function(d) { return !d.disabled })
  8400. };
  8401. }
  8402. };
  8403. var stateSetter = function(data) {
  8404. return function(state) {
  8405. if (state.active !== undefined)
  8406. data.forEach(function(series,i) {
  8407. series.disabled = !state.active[i];
  8408. });
  8409. }
  8410. };
  8411. var allDisabled = function(data) {
  8412. return data.every(function(series) {
  8413. return series.disabled;
  8414. });
  8415. }
  8416. function chart(selection) {
  8417. selection.each(function(data) {
  8418. var container = d3.select(this),
  8419. that = this;
  8420. nv.utils.initSVG(container);
  8421. var availableWidth = nv.utils.availableWidth(width, container, margin),
  8422. availableHeight1 = nv.utils.availableHeight(height, container, margin)
  8423. - (focusEnable ? focusHeight : 0),
  8424. availableHeight2 = focusHeight - margin2.top - margin2.bottom;
  8425. chart.update = function() { container.transition().duration(transitionDuration).call(chart); };
  8426. chart.container = this;
  8427. state
  8428. .setter(stateSetter(data), chart.update)
  8429. .getter(stateGetter(data))
  8430. .update();
  8431. // DEPRECATED set state.disableddisabled
  8432. state.disabled = data.map(function(d) { return !!d.disabled });
  8433. if (!defaultState) {
  8434. var key;
  8435. defaultState = {};
  8436. for (key in state) {
  8437. if (state[key] instanceof Array)
  8438. defaultState[key] = state[key].slice(0);
  8439. else
  8440. defaultState[key] = state[key];
  8441. }
  8442. }
  8443. // Display No Data message if there's nothing to show.
  8444. if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
  8445. nv.utils.noData(chart, container)
  8446. return chart;
  8447. } else {
  8448. container.selectAll('.nv-noData').remove();
  8449. }
  8450. // Setup Scales
  8451. var dataBars = data.filter(function(d) { return !d.disabled && d.bar });
  8452. var dataLines = data.filter(function(d) { return !d.bar }); // removed the !d.disabled clause here to fix Issue #240
  8453. if (dataBars.length && !switchYAxisOrder) {
  8454. x = bars.xScale();
  8455. } else {
  8456. x = lines.xScale();
  8457. }
  8458. x2 = x2Axis.scale();
  8459. // select the scales and series based on the position of the yAxis
  8460. y1 = switchYAxisOrder ? lines.yScale() : bars.yScale();
  8461. y2 = switchYAxisOrder ? bars.yScale() : lines.yScale();
  8462. y3 = switchYAxisOrder ? lines2.yScale() : bars2.yScale();
  8463. y4 = switchYAxisOrder ? bars2.yScale() : lines2.yScale();
  8464. var series1 = data
  8465. .filter(function(d) { return !d.disabled && (switchYAxisOrder ? !d.bar : d.bar) })
  8466. .map(function(d) {
  8467. return d.values.map(function(d,i) {
  8468. return { x: getX(d,i), y: getY(d,i) }
  8469. })
  8470. });
  8471. var series2 = data
  8472. .filter(function(d) { return !d.disabled && (switchYAxisOrder ? d.bar : !d.bar) })
  8473. .map(function(d) {
  8474. return d.values.map(function(d,i) {
  8475. return { x: getX(d,i), y: getY(d,i) }
  8476. })
  8477. });
  8478. x.range([0, availableWidth]);
  8479. x2 .domain(d3.extent(d3.merge(series1.concat(series2)), function(d) { return d.x } ))
  8480. .range([0, availableWidth]);
  8481. // Setup containers and skeleton of chart
  8482. var wrap = container.selectAll('g.nv-wrap.nv-linePlusBar').data([data]);
  8483. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-linePlusBar').append('g');
  8484. var g = wrap.select('g');
  8485. gEnter.append('g').attr('class', 'nv-legendWrap');
  8486. // this is the main chart
  8487. var focusEnter = gEnter.append('g').attr('class', 'nv-focus');
  8488. focusEnter.append('g').attr('class', 'nv-x nv-axis');
  8489. focusEnter.append('g').attr('class', 'nv-y1 nv-axis');
  8490. focusEnter.append('g').attr('class', 'nv-y2 nv-axis');
  8491. focusEnter.append('g').attr('class', 'nv-barsWrap');
  8492. focusEnter.append('g').attr('class', 'nv-linesWrap');
  8493. // context chart is where you can focus in
  8494. var contextEnter = gEnter.append('g').attr('class', 'nv-context');
  8495. contextEnter.append('g').attr('class', 'nv-x nv-axis');
  8496. contextEnter.append('g').attr('class', 'nv-y1 nv-axis');
  8497. contextEnter.append('g').attr('class', 'nv-y2 nv-axis');
  8498. contextEnter.append('g').attr('class', 'nv-barsWrap');
  8499. contextEnter.append('g').attr('class', 'nv-linesWrap');
  8500. contextEnter.append('g').attr('class', 'nv-brushBackground');
  8501. contextEnter.append('g').attr('class', 'nv-x nv-brush');
  8502. //============================================================
  8503. // Legend
  8504. //------------------------------------------------------------
  8505. if (!showLegend) {
  8506. g.select('.nv-legendWrap').selectAll('*').remove();
  8507. } else {
  8508. var legendWidth = legend.align() ? availableWidth / 2 : availableWidth;
  8509. var legendXPosition = legend.align() ? legendWidth : 0;
  8510. legend.width(legendWidth);
  8511. g.select('.nv-legendWrap')
  8512. .datum(data.map(function(series) {
  8513. series.originalKey = series.originalKey === undefined ? series.key : series.originalKey;
  8514. if(switchYAxisOrder) {
  8515. series.key = series.originalKey + (series.bar ? legendRightAxisHint : legendLeftAxisHint);
  8516. } else {
  8517. series.key = series.originalKey + (series.bar ? legendLeftAxisHint : legendRightAxisHint);
  8518. }
  8519. return series;
  8520. }))
  8521. .call(legend);
  8522. if (!marginTop && legend.height() !== margin.top) {
  8523. margin.top = legend.height();
  8524. // FIXME: shouldn't this be "- (focusEnabled ? focusHeight : 0)"?
  8525. availableHeight1 = nv.utils.availableHeight(height, container, margin) - focusHeight;
  8526. }
  8527. g.select('.nv-legendWrap')
  8528. .attr('transform', 'translate(' + legendXPosition + ',' + (-margin.top) +')');
  8529. }
  8530. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  8531. //============================================================
  8532. // Context chart (focus chart) components
  8533. //------------------------------------------------------------
  8534. // hide or show the focus context chart
  8535. g.select('.nv-context').style('display', focusEnable ? 'initial' : 'none');
  8536. bars2
  8537. .width(availableWidth)
  8538. .height(availableHeight2)
  8539. .color(data.map(function (d, i) {
  8540. return d.color || color(d, i);
  8541. }).filter(function (d, i) {
  8542. return !data[i].disabled && data[i].bar
  8543. }));
  8544. lines2
  8545. .width(availableWidth)
  8546. .height(availableHeight2)
  8547. .color(data.map(function (d, i) {
  8548. return d.color || color(d, i);
  8549. }).filter(function (d, i) {
  8550. return !data[i].disabled && !data[i].bar
  8551. }));
  8552. var bars2Wrap = g.select('.nv-context .nv-barsWrap')
  8553. .datum(dataBars.length ? dataBars : [
  8554. {values: []}
  8555. ]);
  8556. var lines2Wrap = g.select('.nv-context .nv-linesWrap')
  8557. .datum(allDisabled(dataLines) ?
  8558. [{values: []}] :
  8559. dataLines.filter(function(dataLine) {
  8560. return !dataLine.disabled;
  8561. }));
  8562. g.select('.nv-context')
  8563. .attr('transform', 'translate(0,' + ( availableHeight1 + margin.bottom + margin2.top) + ')');
  8564. bars2Wrap.transition().call(bars2);
  8565. lines2Wrap.transition().call(lines2);
  8566. // context (focus chart) axis controls
  8567. if (focusShowAxisX) {
  8568. x2Axis
  8569. ._ticks( nv.utils.calcTicksX(availableWidth / 100, data))
  8570. .tickSize(-availableHeight2, 0);
  8571. g.select('.nv-context .nv-x.nv-axis')
  8572. .attr('transform', 'translate(0,' + y3.range()[0] + ')');
  8573. g.select('.nv-context .nv-x.nv-axis').transition()
  8574. .call(x2Axis);
  8575. }
  8576. if (focusShowAxisY) {
  8577. y3Axis
  8578. .scale(y3)
  8579. ._ticks( availableHeight2 / 36 )
  8580. .tickSize( -availableWidth, 0);
  8581. y4Axis
  8582. .scale(y4)
  8583. ._ticks( availableHeight2 / 36 )
  8584. .tickSize(dataBars.length ? 0 : -availableWidth, 0); // Show the y2 rules only if y1 has none
  8585. g.select('.nv-context .nv-y3.nv-axis')
  8586. .style('opacity', dataBars.length ? 1 : 0)
  8587. .attr('transform', 'translate(0,' + x2.range()[0] + ')');
  8588. g.select('.nv-context .nv-y2.nv-axis')
  8589. .style('opacity', dataLines.length ? 1 : 0)
  8590. .attr('transform', 'translate(' + x2.range()[1] + ',0)');
  8591. g.select('.nv-context .nv-y1.nv-axis').transition()
  8592. .call(y3Axis);
  8593. g.select('.nv-context .nv-y2.nv-axis').transition()
  8594. .call(y4Axis);
  8595. }
  8596. // Setup Brush
  8597. brush.x(x2).on('brush', onBrush);
  8598. if (brushExtent) brush.extent(brushExtent);
  8599. var brushBG = g.select('.nv-brushBackground').selectAll('g')
  8600. .data([brushExtent || brush.extent()]);
  8601. var brushBGenter = brushBG.enter()
  8602. .append('g');
  8603. brushBGenter.append('rect')
  8604. .attr('class', 'left')
  8605. .attr('x', 0)
  8606. .attr('y', 0)
  8607. .attr('height', availableHeight2);
  8608. brushBGenter.append('rect')
  8609. .attr('class', 'right')
  8610. .attr('x', 0)
  8611. .attr('y', 0)
  8612. .attr('height', availableHeight2);
  8613. var gBrush = g.select('.nv-x.nv-brush')
  8614. .call(brush);
  8615. gBrush.selectAll('rect')
  8616. //.attr('y', -5)
  8617. .attr('height', availableHeight2);
  8618. gBrush.selectAll('.resize').append('path').attr('d', resizePath);
  8619. //============================================================
  8620. // Event Handling/Dispatching (in chart's scope)
  8621. //------------------------------------------------------------
  8622. legend.dispatch.on('stateChange', function(newState) {
  8623. for (var key in newState)
  8624. state[key] = newState[key];
  8625. dispatch.stateChange(state);
  8626. chart.update();
  8627. });
  8628. // Update chart from a state object passed to event handler
  8629. dispatch.on('changeState', function(e) {
  8630. if (typeof e.disabled !== 'undefined') {
  8631. data.forEach(function(series,i) {
  8632. series.disabled = e.disabled[i];
  8633. });
  8634. state.disabled = e.disabled;
  8635. }
  8636. chart.update();
  8637. });
  8638. //============================================================
  8639. // Functions
  8640. //------------------------------------------------------------
  8641. // Taken from crossfilter (http://square.github.com/crossfilter/)
  8642. function resizePath(d) {
  8643. var e = +(d == 'e'),
  8644. x = e ? 1 : -1,
  8645. y = availableHeight2 / 3;
  8646. return 'M' + (.5 * x) + ',' + y
  8647. + 'A6,6 0 0 ' + e + ' ' + (6.5 * x) + ',' + (y + 6)
  8648. + 'V' + (2 * y - 6)
  8649. + 'A6,6 0 0 ' + e + ' ' + (.5 * x) + ',' + (2 * y)
  8650. + 'Z'
  8651. + 'M' + (2.5 * x) + ',' + (y + 8)
  8652. + 'V' + (2 * y - 8)
  8653. + 'M' + (4.5 * x) + ',' + (y + 8)
  8654. + 'V' + (2 * y - 8);
  8655. }
  8656. function updateBrushBG() {
  8657. if (!brush.empty()) brush.extent(brushExtent);
  8658. brushBG
  8659. .data([brush.empty() ? x2.domain() : brushExtent])
  8660. .each(function(d,i) {
  8661. var leftWidth = x2(d[0]) - x2.range()[0],
  8662. rightWidth = x2.range()[1] - x2(d[1]);
  8663. d3.select(this).select('.left')
  8664. .attr('width', leftWidth < 0 ? 0 : leftWidth);
  8665. d3.select(this).select('.right')
  8666. .attr('x', x2(d[1]))
  8667. .attr('width', rightWidth < 0 ? 0 : rightWidth);
  8668. });
  8669. }
  8670. function onBrush() {
  8671. brushExtent = brush.empty() ? null : brush.extent();
  8672. extent = brush.empty() ? x2.domain() : brush.extent();
  8673. dispatch.brush({extent: extent, brush: brush});
  8674. updateBrushBG();
  8675. // Prepare Main (Focus) Bars and Lines
  8676. bars
  8677. .width(availableWidth)
  8678. .height(availableHeight1)
  8679. .color(data.map(function(d,i) {
  8680. return d.color || color(d, i);
  8681. }).filter(function(d,i) { return !data[i].disabled && data[i].bar }));
  8682. lines
  8683. .width(availableWidth)
  8684. .height(availableHeight1)
  8685. .color(data.map(function(d,i) {
  8686. return d.color || color(d, i);
  8687. }).filter(function(d,i) { return !data[i].disabled && !data[i].bar }));
  8688. var focusBarsWrap = g.select('.nv-focus .nv-barsWrap')
  8689. .datum(!dataBars.length ? [{values:[]}] :
  8690. dataBars
  8691. .map(function(d,i) {
  8692. return {
  8693. key: d.key,
  8694. values: d.values.filter(function(d,i) {
  8695. return bars.x()(d,i) >= extent[0] && bars.x()(d,i) <= extent[1];
  8696. })
  8697. }
  8698. })
  8699. );
  8700. var focusLinesWrap = g.select('.nv-focus .nv-linesWrap')
  8701. .datum(allDisabled(dataLines) ? [{values:[]}] :
  8702. dataLines
  8703. .filter(function(dataLine) { return !dataLine.disabled; })
  8704. .map(function(d,i) {
  8705. return {
  8706. area: d.area,
  8707. fillOpacity: d.fillOpacity,
  8708. strokeWidth: d.strokeWidth,
  8709. key: d.key,
  8710. values: d.values.filter(function(d,i) {
  8711. return lines.x()(d,i) >= extent[0] && lines.x()(d,i) <= extent[1];
  8712. })
  8713. }
  8714. })
  8715. );
  8716. // Update Main (Focus) X Axis
  8717. if (dataBars.length && !switchYAxisOrder) {
  8718. x = bars.xScale();
  8719. } else {
  8720. x = lines.xScale();
  8721. }
  8722. xAxis
  8723. .scale(x)
  8724. ._ticks( nv.utils.calcTicksX(availableWidth/100, data) )
  8725. .tickSize(-availableHeight1, 0);
  8726. xAxis.domain([Math.ceil(extent[0]), Math.floor(extent[1])]);
  8727. g.select('.nv-x.nv-axis').transition().duration(transitionDuration)
  8728. .call(xAxis);
  8729. // Update Main (Focus) Bars and Lines
  8730. focusBarsWrap.transition().duration(transitionDuration).call(bars);
  8731. focusLinesWrap.transition().duration(transitionDuration).call(lines);
  8732. // Setup and Update Main (Focus) Y Axes
  8733. g.select('.nv-focus .nv-x.nv-axis')
  8734. .attr('transform', 'translate(0,' + y1.range()[0] + ')');
  8735. y1Axis
  8736. .scale(y1)
  8737. ._ticks( nv.utils.calcTicksY(availableHeight1/36, data) )
  8738. .tickSize(-availableWidth, 0);
  8739. y2Axis
  8740. .scale(y2)
  8741. ._ticks( nv.utils.calcTicksY(availableHeight1/36, data) );
  8742. // Show the y2 rules only if y1 has none
  8743. if(!switchYAxisOrder) {
  8744. y2Axis.tickSize(dataBars.length ? 0 : -availableWidth, 0);
  8745. } else {
  8746. y2Axis.tickSize(dataLines.length ? 0 : -availableWidth, 0);
  8747. }
  8748. // Calculate opacity of the axis
  8749. var barsOpacity = dataBars.length ? 1 : 0;
  8750. var linesOpacity = dataLines.length && !allDisabled(dataLines) ? 1 : 0;
  8751. var y1Opacity = switchYAxisOrder ? linesOpacity : barsOpacity;
  8752. var y2Opacity = switchYAxisOrder ? barsOpacity : linesOpacity;
  8753. g.select('.nv-focus .nv-y1.nv-axis')
  8754. .style('opacity', y1Opacity);
  8755. g.select('.nv-focus .nv-y2.nv-axis')
  8756. .style('opacity', y2Opacity)
  8757. .attr('transform', 'translate(' + x.range()[1] + ',0)');
  8758. g.select('.nv-focus .nv-y1.nv-axis').transition().duration(transitionDuration)
  8759. .call(y1Axis);
  8760. g.select('.nv-focus .nv-y2.nv-axis').transition().duration(transitionDuration)
  8761. .call(y2Axis);
  8762. }
  8763. onBrush();
  8764. });
  8765. return chart;
  8766. }
  8767. //============================================================
  8768. // Event Handling/Dispatching (out of chart's scope)
  8769. //------------------------------------------------------------
  8770. lines.dispatch.on('elementMouseover.tooltip', function(evt) {
  8771. tooltip
  8772. .duration(100)
  8773. .valueFormatter(function(d, i) {
  8774. return getLinesAxis().main.tickFormat()(d, i);
  8775. })
  8776. .data(evt)
  8777. .hidden(false);
  8778. });
  8779. lines.dispatch.on('elementMouseout.tooltip', function(evt) {
  8780. tooltip.hidden(true)
  8781. });
  8782. bars.dispatch.on('elementMouseover.tooltip', function(evt) {
  8783. evt.value = chart.x()(evt.data);
  8784. evt['series'] = {
  8785. value: chart.y()(evt.data),
  8786. color: evt.color
  8787. };
  8788. tooltip
  8789. .duration(0)
  8790. .valueFormatter(function(d, i) {
  8791. return getBarsAxis().main.tickFormat()(d, i);
  8792. })
  8793. .data(evt)
  8794. .hidden(false);
  8795. });
  8796. bars.dispatch.on('elementMouseout.tooltip', function(evt) {
  8797. tooltip.hidden(true);
  8798. });
  8799. bars.dispatch.on('elementMousemove.tooltip', function(evt) {
  8800. tooltip();
  8801. });
  8802. //============================================================
  8803. //============================================================
  8804. // Expose Public Variables
  8805. //------------------------------------------------------------
  8806. // expose chart's sub-components
  8807. chart.dispatch = dispatch;
  8808. chart.legend = legend;
  8809. chart.lines = lines;
  8810. chart.lines2 = lines2;
  8811. chart.bars = bars;
  8812. chart.bars2 = bars2;
  8813. chart.xAxis = xAxis;
  8814. chart.x2Axis = x2Axis;
  8815. chart.y1Axis = y1Axis;
  8816. chart.y2Axis = y2Axis;
  8817. chart.y3Axis = y3Axis;
  8818. chart.y4Axis = y4Axis;
  8819. chart.tooltip = tooltip;
  8820. chart.options = nv.utils.optionsFunc.bind(chart);
  8821. chart._options = Object.create({}, {
  8822. // simple options, just get/set the necessary values
  8823. width: {get: function(){return width;}, set: function(_){width=_;}},
  8824. height: {get: function(){return height;}, set: function(_){height=_;}},
  8825. showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
  8826. brushExtent: {get: function(){return brushExtent;}, set: function(_){brushExtent=_;}},
  8827. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  8828. focusEnable: {get: function(){return focusEnable;}, set: function(_){focusEnable=_;}},
  8829. focusHeight: {get: function(){return focusHeight;}, set: function(_){focusHeight=_;}},
  8830. focusShowAxisX: {get: function(){return focusShowAxisX;}, set: function(_){focusShowAxisX=_;}},
  8831. focusShowAxisY: {get: function(){return focusShowAxisY;}, set: function(_){focusShowAxisY=_;}},
  8832. legendLeftAxisHint: {get: function(){return legendLeftAxisHint;}, set: function(_){legendLeftAxisHint=_;}},
  8833. legendRightAxisHint: {get: function(){return legendRightAxisHint;}, set: function(_){legendRightAxisHint=_;}},
  8834. // options that require extra logic in the setter
  8835. margin: {get: function(){return margin;}, set: function(_){
  8836. if (_.top !== undefined) {
  8837. margin.top = _.top;
  8838. marginTop = _.top;
  8839. }
  8840. margin.right = _.right !== undefined ? _.right : margin.right;
  8841. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  8842. margin.left = _.left !== undefined ? _.left : margin.left;
  8843. }},
  8844. focusMargin: {get: function(){return margin2;}, set: function(_){
  8845. margin2.top = _.top !== undefined ? _.top : margin2.top;
  8846. margin2.right = _.right !== undefined ? _.right : margin2.right;
  8847. margin2.bottom = _.bottom !== undefined ? _.bottom : margin2.bottom;
  8848. margin2.left = _.left !== undefined ? _.left : margin2.left;
  8849. }},
  8850. duration: {get: function(){return transitionDuration;}, set: function(_){
  8851. transitionDuration = _;
  8852. }},
  8853. color: {get: function(){return color;}, set: function(_){
  8854. color = nv.utils.getColor(_);
  8855. legend.color(color);
  8856. }},
  8857. x: {get: function(){return getX;}, set: function(_){
  8858. getX = _;
  8859. lines.x(_);
  8860. lines2.x(_);
  8861. bars.x(_);
  8862. bars2.x(_);
  8863. }},
  8864. y: {get: function(){return getY;}, set: function(_){
  8865. getY = _;
  8866. lines.y(_);
  8867. lines2.y(_);
  8868. bars.y(_);
  8869. bars2.y(_);
  8870. }},
  8871. switchYAxisOrder: {get: function(){return switchYAxisOrder;}, set: function(_){
  8872. // Switch the tick format for the yAxis
  8873. if(switchYAxisOrder !== _) {
  8874. var y1 = y1Axis;
  8875. y1Axis = y2Axis;
  8876. y2Axis = y1;
  8877. var y3 = y3Axis;
  8878. y3Axis = y4Axis;
  8879. y4Axis = y3;
  8880. }
  8881. switchYAxisOrder=_;
  8882. y1Axis.orient('left');
  8883. y2Axis.orient('right');
  8884. y3Axis.orient('left');
  8885. y4Axis.orient('right');
  8886. }}
  8887. });
  8888. nv.utils.inheritOptions(chart, lines);
  8889. nv.utils.initOptions(chart);
  8890. return chart;
  8891. };
  8892. nv.models.multiBar = function() {
  8893. "use strict";
  8894. //============================================================
  8895. // Public Variables with Default Settings
  8896. //------------------------------------------------------------
  8897. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  8898. , width = 960
  8899. , height = 500
  8900. , x = d3.scale.ordinal()
  8901. , y = d3.scale.linear()
  8902. , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one
  8903. , container = null
  8904. , getX = function(d) { return d.x }
  8905. , getY = function(d) { return d.y }
  8906. , forceY = [0] // 0 is forced by default.. this makes sense for the majority of bar graphs... user can always do chart.forceY([]) to remove
  8907. , clipEdge = true
  8908. , stacked = false
  8909. , stackOffset = 'zero' // options include 'silhouette', 'wiggle', 'expand', 'zero', or a custom function
  8910. , color = nv.utils.defaultColor()
  8911. , hideable = false
  8912. , barColor = null // adding the ability to set the color for each rather than the whole group
  8913. , disabled // used in conjunction with barColor to communicate from multiBarHorizontalChart what series are disabled
  8914. , duration = 500
  8915. , xDomain
  8916. , yDomain
  8917. , xRange
  8918. , yRange
  8919. , groupSpacing = 0.1
  8920. , fillOpacity = 0.75
  8921. , dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout', 'elementMousemove', 'renderEnd')
  8922. ;
  8923. //============================================================
  8924. // Private Variables
  8925. //------------------------------------------------------------
  8926. var x0, y0 //used to store previous scales
  8927. , renderWatch = nv.utils.renderWatch(dispatch, duration)
  8928. ;
  8929. var last_datalength = 0;
  8930. function chart(selection) {
  8931. renderWatch.reset();
  8932. selection.each(function(data) {
  8933. var availableWidth = width - margin.left - margin.right,
  8934. availableHeight = height - margin.top - margin.bottom;
  8935. container = d3.select(this);
  8936. nv.utils.initSVG(container);
  8937. var nonStackableCount = 0;
  8938. // This function defines the requirements for render complete
  8939. var endFn = function(d, i) {
  8940. if (d.series === data.length - 1 && i === data[0].values.length - 1)
  8941. return true;
  8942. return false;
  8943. };
  8944. if(hideable && data.length) hideable = [{
  8945. values: data[0].values.map(function(d) {
  8946. return {
  8947. x: d.x,
  8948. y: 0,
  8949. series: d.series,
  8950. size: 0.01
  8951. };}
  8952. )}];
  8953. if (stacked) {
  8954. var parsed = d3.layout.stack()
  8955. .offset(stackOffset)
  8956. .values(function(d){ return d.values })
  8957. .y(getY)
  8958. (!data.length && hideable ? hideable : data);
  8959. parsed.forEach(function(series, i){
  8960. // if series is non-stackable, use un-parsed data
  8961. if (series.nonStackable) {
  8962. data[i].nonStackableSeries = nonStackableCount++;
  8963. parsed[i] = data[i];
  8964. } else {
  8965. // don't stack this seires on top of the nonStackable seriees
  8966. if (i > 0 && parsed[i - 1].nonStackable){
  8967. parsed[i].values.map(function(d,j){
  8968. d.y0 -= parsed[i - 1].values[j].y;
  8969. d.y1 = d.y0 + d.y;
  8970. });
  8971. }
  8972. }
  8973. });
  8974. data = parsed;
  8975. }
  8976. //add series index and key to each data point for reference
  8977. data.forEach(function(series, i) {
  8978. series.values.forEach(function(point) {
  8979. point.series = i;
  8980. point.key = series.key;
  8981. });
  8982. });
  8983. // HACK for negative value stacking
  8984. if (stacked && data.length > 0) {
  8985. data[0].values.map(function(d,i) {
  8986. var posBase = 0, negBase = 0;
  8987. data.map(function(d, idx) {
  8988. if (!data[idx].nonStackable) {
  8989. var f = d.values[i]
  8990. f.size = Math.abs(f.y);
  8991. if (f.y<0) {
  8992. f.y1 = negBase;
  8993. negBase = negBase - f.size;
  8994. } else
  8995. {
  8996. f.y1 = f.size + posBase;
  8997. posBase = posBase + f.size;
  8998. }
  8999. }
  9000. });
  9001. });
  9002. }
  9003. // Setup Scales
  9004. // remap and flatten the data for use in calculating the scales' domains
  9005. var seriesData = (xDomain && yDomain) ? [] : // if we know xDomain and yDomain, no need to calculate
  9006. data.map(function(d, idx) {
  9007. return d.values.map(function(d,i) {
  9008. return { x: getX(d,i), y: getY(d,i), y0: d.y0, y1: d.y1, idx:idx }
  9009. })
  9010. });
  9011. x.domain(xDomain || d3.merge(seriesData).map(function(d) { return d.x }))
  9012. .rangeBands(xRange || [0, availableWidth], groupSpacing);
  9013. y.domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) {
  9014. var domain = d.y;
  9015. // increase the domain range if this series is stackable
  9016. if (stacked && !data[d.idx].nonStackable) {
  9017. if (d.y > 0){
  9018. domain = d.y1
  9019. } else {
  9020. domain = d.y1 + d.y
  9021. }
  9022. }
  9023. return domain;
  9024. }).concat(forceY)))
  9025. .range(yRange || [availableHeight, 0]);
  9026. // If scale's domain don't have a range, slightly adjust to make one... so a chart can show a single data point
  9027. if (x.domain()[0] === x.domain()[1])
  9028. x.domain()[0] ?
  9029. x.domain([x.domain()[0] - x.domain()[0] * 0.01, x.domain()[1] + x.domain()[1] * 0.01])
  9030. : x.domain([-1,1]);
  9031. if (y.domain()[0] === y.domain()[1])
  9032. y.domain()[0] ?
  9033. y.domain([y.domain()[0] + y.domain()[0] * 0.01, y.domain()[1] - y.domain()[1] * 0.01])
  9034. : y.domain([-1,1]);
  9035. x0 = x0 || x;
  9036. y0 = y0 || y;
  9037. // Setup containers and skeleton of chart
  9038. var wrap = container.selectAll('g.nv-wrap.nv-multibar').data([data]);
  9039. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-multibar');
  9040. var defsEnter = wrapEnter.append('defs');
  9041. var gEnter = wrapEnter.append('g');
  9042. var g = wrap.select('g');
  9043. gEnter.append('g').attr('class', 'nv-groups');
  9044. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  9045. defsEnter.append('clipPath')
  9046. .attr('id', 'nv-edge-clip-' + id)
  9047. .append('rect');
  9048. wrap.select('#nv-edge-clip-' + id + ' rect')
  9049. .attr('width', availableWidth)
  9050. .attr('height', availableHeight);
  9051. g.attr('clip-path', clipEdge ? 'url(#nv-edge-clip-' + id + ')' : '');
  9052. var groups = wrap.select('.nv-groups').selectAll('.nv-group')
  9053. .data(function(d) { return d }, function(d,i) { return i });
  9054. groups.enter().append('g')
  9055. .style('stroke-opacity', 1e-6)
  9056. .style('fill-opacity', 1e-6);
  9057. var exitTransition = renderWatch
  9058. .transition(groups.exit().selectAll('rect.nv-bar'), 'multibarExit', Math.min(100, duration))
  9059. .attr('y', function(d, i, j) {
  9060. var yVal = y0(0) || 0;
  9061. if (stacked) {
  9062. if (data[d.series] && !data[d.series].nonStackable) {
  9063. yVal = y0(d.y0);
  9064. }
  9065. }
  9066. return yVal;
  9067. })
  9068. .attr('height', 0)
  9069. .remove();
  9070. if (exitTransition.delay)
  9071. exitTransition.delay(function(d,i) {
  9072. var delay = i * (duration / (last_datalength + 1)) - i;
  9073. return delay;
  9074. });
  9075. groups
  9076. .attr('class', function(d,i) { return 'nv-group nv-series-' + i })
  9077. .classed('hover', function(d) { return d.hover })
  9078. .style('fill', function(d,i){ return color(d, i) })
  9079. .style('stroke', function(d,i){ return color(d, i) });
  9080. groups
  9081. .style('stroke-opacity', 1)
  9082. .style('fill-opacity', fillOpacity);
  9083. var bars = groups.selectAll('rect.nv-bar')
  9084. .data(function(d) { return (hideable && !data.length) ? hideable.values : d.values });
  9085. bars.exit().remove();
  9086. var barsEnter = bars.enter().append('rect')
  9087. .attr('class', function(d,i) { return getY(d,i) < 0 ? 'nv-bar negative' : 'nv-bar positive'})
  9088. .attr('x', function(d,i,j) {
  9089. return stacked && !data[j].nonStackable ? 0 : (j * x.rangeBand() / data.length )
  9090. })
  9091. .attr('y', function(d,i,j) { return y0(stacked && !data[j].nonStackable ? d.y0 : 0) || 0 })
  9092. .attr('height', 0)
  9093. .attr('width', function(d,i,j) { return x.rangeBand() / (stacked && !data[j].nonStackable ? 1 : data.length) })
  9094. .attr('transform', function(d,i) { return 'translate(' + x(getX(d,i)) + ',0)'; })
  9095. ;
  9096. bars
  9097. .style('fill', function(d,i,j){ return color(d, j, i); })
  9098. .style('stroke', function(d,i,j){ return color(d, j, i); })
  9099. .on('mouseover', function(d,i,j) {
  9100. d3.select(this).classed('hover', true);
  9101. dispatch.elementMouseover({
  9102. data: d,
  9103. index: i,
  9104. series: data[j],
  9105. color: d3.select(this).style("fill")
  9106. });
  9107. })
  9108. .on('mouseout', function(d,i,j) {
  9109. d3.select(this).classed('hover', false);
  9110. dispatch.elementMouseout({
  9111. data: d,
  9112. index: i,
  9113. series: data[j],
  9114. color: d3.select(this).style("fill")
  9115. });
  9116. })
  9117. .on('mousemove', function(d,i,j) {
  9118. dispatch.elementMousemove({
  9119. data: d,
  9120. index: i,
  9121. series: data[j],
  9122. color: d3.select(this).style("fill")
  9123. });
  9124. })
  9125. .on('click', function(d,i,j) {
  9126. var element = this;
  9127. dispatch.elementClick({
  9128. data: d,
  9129. index: i,
  9130. series: data[j],
  9131. color: d3.select(this).style("fill"),
  9132. event: d3.event,
  9133. element: element
  9134. });
  9135. d3.event.stopPropagation();
  9136. })
  9137. .on('dblclick', function(d,i,j) {
  9138. dispatch.elementDblClick({
  9139. data: d,
  9140. index: i,
  9141. series: data[j],
  9142. color: d3.select(this).style("fill")
  9143. });
  9144. d3.event.stopPropagation();
  9145. });
  9146. bars
  9147. .attr('class', function(d,i) { return getY(d,i) < 0 ? 'nv-bar negative' : 'nv-bar positive'})
  9148. .attr('transform', function(d,i) { return 'translate(' + x(getX(d,i)) + ',0)'; })
  9149. if (barColor) {
  9150. if (!disabled) disabled = data.map(function() { return true });
  9151. bars
  9152. .style('fill', function(d,i,j) { return d3.rgb(barColor(d,i)).darker( disabled.map(function(d,i) { return i }).filter(function(d,i){ return !disabled[i] })[j] ).toString(); })
  9153. .style('stroke', function(d,i,j) { return d3.rgb(barColor(d,i)).darker( disabled.map(function(d,i) { return i }).filter(function(d,i){ return !disabled[i] })[j] ).toString(); });
  9154. }
  9155. var barSelection =
  9156. bars.watchTransition(renderWatch, 'multibar', Math.min(250, duration))
  9157. .delay(function(d,i) {
  9158. return i * duration / data[0].values.length;
  9159. });
  9160. if (stacked){
  9161. barSelection
  9162. .attr('y', function(d,i,j) {
  9163. var yVal = 0;
  9164. // if stackable, stack it on top of the previous series
  9165. if (!data[j].nonStackable) {
  9166. yVal = y(d.y1);
  9167. } else {
  9168. if (getY(d,i) < 0){
  9169. yVal = y(0);
  9170. } else {
  9171. if (y(0) - y(getY(d,i)) < -1){
  9172. yVal = y(0) - 1;
  9173. } else {
  9174. yVal = y(getY(d, i)) || 0;
  9175. }
  9176. }
  9177. }
  9178. return yVal;
  9179. })
  9180. .attr('height', function(d,i,j) {
  9181. if (!data[j].nonStackable) {
  9182. return Math.max(Math.abs(y(d.y+d.y0) - y(d.y0)), 0);
  9183. } else {
  9184. return Math.max(Math.abs(y(getY(d,i)) - y(0)), 0) || 0;
  9185. }
  9186. })
  9187. .attr('x', function(d,i,j) {
  9188. var width = 0;
  9189. if (data[j].nonStackable) {
  9190. width = d.series * x.rangeBand() / data.length;
  9191. if (data.length !== nonStackableCount){
  9192. width = data[j].nonStackableSeries * x.rangeBand()/(nonStackableCount*2);
  9193. }
  9194. }
  9195. return width;
  9196. })
  9197. .attr('width', function(d,i,j){
  9198. if (!data[j].nonStackable) {
  9199. return x.rangeBand();
  9200. } else {
  9201. // if all series are nonStacable, take the full width
  9202. var width = (x.rangeBand() / nonStackableCount);
  9203. // otherwise, nonStackable graph will be only taking the half-width
  9204. // of the x rangeBand
  9205. if (data.length !== nonStackableCount) {
  9206. width = x.rangeBand()/(nonStackableCount*2);
  9207. }
  9208. return width;
  9209. }
  9210. });
  9211. }
  9212. else {
  9213. barSelection
  9214. .attr('x', function(d,i) {
  9215. return d.series * x.rangeBand() / data.length;
  9216. })
  9217. .attr('width', x.rangeBand() / data.length)
  9218. .attr('y', function(d,i) {
  9219. return getY(d,i) < 0 ?
  9220. y(0) :
  9221. y(0) - y(getY(d,i)) < 1 ?
  9222. y(0) - 1 :
  9223. y(getY(d,i)) || 0;
  9224. })
  9225. .attr('height', function(d,i) {
  9226. return Math.max(Math.abs(y(getY(d,i)) - y(0)),1) || 0;
  9227. });
  9228. }
  9229. //store old scales for use in transitions on update
  9230. x0 = x.copy();
  9231. y0 = y.copy();
  9232. // keep track of the last data value length for transition calculations
  9233. if (data[0] && data[0].values) {
  9234. last_datalength = data[0].values.length;
  9235. }
  9236. });
  9237. renderWatch.renderEnd('multibar immediate');
  9238. return chart;
  9239. }
  9240. //============================================================
  9241. // Expose Public Variables
  9242. //------------------------------------------------------------
  9243. chart.dispatch = dispatch;
  9244. chart.options = nv.utils.optionsFunc.bind(chart);
  9245. chart._options = Object.create({}, {
  9246. // simple options, just get/set the necessary values
  9247. width: {get: function(){return width;}, set: function(_){width=_;}},
  9248. height: {get: function(){return height;}, set: function(_){height=_;}},
  9249. x: {get: function(){return getX;}, set: function(_){getX=_;}},
  9250. y: {get: function(){return getY;}, set: function(_){getY=_;}},
  9251. xScale: {get: function(){return x;}, set: function(_){x=_;}},
  9252. yScale: {get: function(){return y;}, set: function(_){y=_;}},
  9253. xDomain: {get: function(){return xDomain;}, set: function(_){xDomain=_;}},
  9254. yDomain: {get: function(){return yDomain;}, set: function(_){yDomain=_;}},
  9255. xRange: {get: function(){return xRange;}, set: function(_){xRange=_;}},
  9256. yRange: {get: function(){return yRange;}, set: function(_){yRange=_;}},
  9257. forceY: {get: function(){return forceY;}, set: function(_){forceY=_;}},
  9258. stacked: {get: function(){return stacked;}, set: function(_){stacked=_;}},
  9259. stackOffset: {get: function(){return stackOffset;}, set: function(_){stackOffset=_;}},
  9260. clipEdge: {get: function(){return clipEdge;}, set: function(_){clipEdge=_;}},
  9261. disabled: {get: function(){return disabled;}, set: function(_){disabled=_;}},
  9262. id: {get: function(){return id;}, set: function(_){id=_;}},
  9263. hideable: {get: function(){return hideable;}, set: function(_){hideable=_;}},
  9264. groupSpacing:{get: function(){return groupSpacing;}, set: function(_){groupSpacing=_;}},
  9265. fillOpacity: {get: function(){return fillOpacity;}, set: function(_){fillOpacity=_;}},
  9266. // options that require extra logic in the setter
  9267. margin: {get: function(){return margin;}, set: function(_){
  9268. margin.top = _.top !== undefined ? _.top : margin.top;
  9269. margin.right = _.right !== undefined ? _.right : margin.right;
  9270. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  9271. margin.left = _.left !== undefined ? _.left : margin.left;
  9272. }},
  9273. duration: {get: function(){return duration;}, set: function(_){
  9274. duration = _;
  9275. renderWatch.reset(duration);
  9276. }},
  9277. color: {get: function(){return color;}, set: function(_){
  9278. color = nv.utils.getColor(_);
  9279. }},
  9280. barColor: {get: function(){return barColor;}, set: function(_){
  9281. barColor = _ ? nv.utils.getColor(_) : null;
  9282. }}
  9283. });
  9284. nv.utils.initOptions(chart);
  9285. return chart;
  9286. };
  9287. nv.models.multiBarChart = function() {
  9288. "use strict";
  9289. //============================================================
  9290. // Public Variables with Default Settings
  9291. //------------------------------------------------------------
  9292. var multibar = nv.models.multiBar()
  9293. , xAxis = nv.models.axis()
  9294. , yAxis = nv.models.axis()
  9295. , interactiveLayer = nv.interactiveGuideline()
  9296. , legend = nv.models.legend()
  9297. , controls = nv.models.legend()
  9298. , tooltip = nv.models.tooltip()
  9299. ;
  9300. var margin = {top: 30, right: 20, bottom: 50, left: 60}
  9301. , marginTop = null
  9302. , width = null
  9303. , height = null
  9304. , color = nv.utils.defaultColor()
  9305. , showControls = true
  9306. , controlLabels = {}
  9307. , showLegend = true
  9308. , legendPosition = null
  9309. , showXAxis = true
  9310. , showYAxis = true
  9311. , rightAlignYAxis = false
  9312. , reduceXTicks = true // if false a tick will show for every data point
  9313. , staggerLabels = false
  9314. , wrapLabels = false
  9315. , rotateLabels = 0
  9316. , x //can be accessed via chart.xScale()
  9317. , y //can be accessed via chart.yScale()
  9318. , state = nv.utils.state()
  9319. , defaultState = null
  9320. , noData = null
  9321. , dispatch = d3.dispatch('stateChange', 'changeState', 'renderEnd')
  9322. , controlWidth = function() { return showControls ? 180 : 0 }
  9323. , duration = 250
  9324. , useInteractiveGuideline = false
  9325. ;
  9326. state.stacked = false // DEPRECATED Maintained for backward compatibility
  9327. multibar.stacked(false);
  9328. xAxis
  9329. .orient('bottom')
  9330. .tickPadding(7)
  9331. .showMaxMin(false)
  9332. .tickFormat(function(d) { return d })
  9333. ;
  9334. yAxis
  9335. .orient((rightAlignYAxis) ? 'right' : 'left')
  9336. .tickFormat(d3.format(',.1f'))
  9337. ;
  9338. tooltip
  9339. .duration(0)
  9340. .valueFormatter(function(d, i) {
  9341. return yAxis.tickFormat()(d, i);
  9342. })
  9343. .headerFormatter(function(d, i) {
  9344. return xAxis.tickFormat()(d, i);
  9345. });
  9346. interactiveLayer.tooltip
  9347. .valueFormatter(function(d, i) {
  9348. return d == null ? "N/A" : yAxis.tickFormat()(d, i);
  9349. })
  9350. .headerFormatter(function(d, i) {
  9351. return xAxis.tickFormat()(d, i);
  9352. });
  9353. interactiveLayer.tooltip
  9354. .valueFormatter(function (d, i) {
  9355. return d == null ? "N/A" : yAxis.tickFormat()(d, i);
  9356. })
  9357. .headerFormatter(function (d, i) {
  9358. return xAxis.tickFormat()(d, i);
  9359. });
  9360. interactiveLayer.tooltip
  9361. .duration(0)
  9362. .valueFormatter(function(d, i) {
  9363. return yAxis.tickFormat()(d, i);
  9364. })
  9365. .headerFormatter(function(d, i) {
  9366. return xAxis.tickFormat()(d, i);
  9367. });
  9368. controls.updateState(false);
  9369. //============================================================
  9370. // Private Variables
  9371. //------------------------------------------------------------
  9372. var renderWatch = nv.utils.renderWatch(dispatch);
  9373. var stacked = false;
  9374. var stateGetter = function(data) {
  9375. return function(){
  9376. return {
  9377. active: data.map(function(d) { return !d.disabled }),
  9378. stacked: stacked
  9379. };
  9380. }
  9381. };
  9382. var stateSetter = function(data) {
  9383. return function(state) {
  9384. if (state.stacked !== undefined)
  9385. stacked = state.stacked;
  9386. if (state.active !== undefined)
  9387. data.forEach(function(series,i) {
  9388. series.disabled = !state.active[i];
  9389. });
  9390. }
  9391. };
  9392. function chart(selection) {
  9393. renderWatch.reset();
  9394. renderWatch.models(multibar);
  9395. if (showXAxis) renderWatch.models(xAxis);
  9396. if (showYAxis) renderWatch.models(yAxis);
  9397. selection.each(function(data) {
  9398. var container = d3.select(this),
  9399. that = this;
  9400. nv.utils.initSVG(container);
  9401. var availableWidth = nv.utils.availableWidth(width, container, margin),
  9402. availableHeight = nv.utils.availableHeight(height, container, margin);
  9403. chart.update = function() {
  9404. if (duration === 0)
  9405. container.call(chart);
  9406. else
  9407. container.transition()
  9408. .duration(duration)
  9409. .call(chart);
  9410. };
  9411. chart.container = this;
  9412. state
  9413. .setter(stateSetter(data), chart.update)
  9414. .getter(stateGetter(data))
  9415. .update();
  9416. // DEPRECATED set state.disableddisabled
  9417. state.disabled = data.map(function(d) { return !!d.disabled });
  9418. if (!defaultState) {
  9419. var key;
  9420. defaultState = {};
  9421. for (key in state) {
  9422. if (state[key] instanceof Array)
  9423. defaultState[key] = state[key].slice(0);
  9424. else
  9425. defaultState[key] = state[key];
  9426. }
  9427. }
  9428. // Display noData message if there's nothing to show.
  9429. if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
  9430. nv.utils.noData(chart, container)
  9431. return chart;
  9432. } else {
  9433. container.selectAll('.nv-noData').remove();
  9434. }
  9435. // Setup Scales
  9436. x = multibar.xScale();
  9437. y = multibar.yScale();
  9438. // Setup containers and skeleton of chart
  9439. var wrap = container.selectAll('g.nv-wrap.nv-multiBarWithLegend').data([data]);
  9440. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-multiBarWithLegend').append('g');
  9441. var g = wrap.select('g');
  9442. gEnter.append('g').attr('class', 'nv-x nv-axis');
  9443. gEnter.append('g').attr('class', 'nv-y nv-axis');
  9444. gEnter.append('g').attr('class', 'nv-barsWrap');
  9445. gEnter.append('g').attr('class', 'nv-legendWrap');
  9446. gEnter.append('g').attr('class', 'nv-controlsWrap');
  9447. gEnter.append('g').attr('class', 'nv-interactive');
  9448. // Legend
  9449. if (!showLegend) {
  9450. g.select('.nv-legendWrap').selectAll('*').remove();
  9451. } else {
  9452. if (legendPosition === 'bottom') {
  9453. legend.width(availableWidth - margin.right);
  9454. g.select('.nv-legendWrap')
  9455. .datum(data)
  9456. .call(legend);
  9457. margin.bottom = xAxis.height() + legend.height();
  9458. availableHeight = nv.utils.availableHeight(height, container, margin);
  9459. g.select('.nv-legendWrap')
  9460. .attr('transform', 'translate(0,' + (availableHeight + xAxis.height()) +')');
  9461. } else {
  9462. legend.width(availableWidth - controlWidth());
  9463. g.select('.nv-legendWrap')
  9464. .datum(data)
  9465. .call(legend);
  9466. if (!marginTop && legend.height() !== margin.top) {
  9467. margin.top = legend.height();
  9468. availableHeight = nv.utils.availableHeight(height, container, margin);
  9469. }
  9470. g.select('.nv-legendWrap')
  9471. .attr('transform', 'translate(' + controlWidth() + ',' + (-margin.top) +')');
  9472. }
  9473. }
  9474. // Controls
  9475. if (!showControls) {
  9476. g.select('.nv-controlsWrap').selectAll('*').remove();
  9477. } else {
  9478. var controlsData = [
  9479. { key: controlLabels.grouped || 'Grouped', disabled: multibar.stacked() },
  9480. { key: controlLabels.stacked || 'Stacked', disabled: !multibar.stacked() }
  9481. ];
  9482. controls.width(controlWidth()).color(['#444', '#444', '#444']);
  9483. g.select('.nv-controlsWrap')
  9484. .datum(controlsData)
  9485. .attr('transform', 'translate(0,' + (-margin.top) +')')
  9486. .call(controls);
  9487. }
  9488. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  9489. if (rightAlignYAxis) {
  9490. g.select(".nv-y.nv-axis")
  9491. .attr("transform", "translate(" + availableWidth + ",0)");
  9492. }
  9493. // Main Chart Component(s)
  9494. multibar
  9495. .disabled(data.map(function(series) { return series.disabled }))
  9496. .width(availableWidth)
  9497. .height(availableHeight)
  9498. .color(data.map(function(d,i) {
  9499. return d.color || color(d, i);
  9500. }).filter(function(d,i) { return !data[i].disabled }));
  9501. var barsWrap = g.select('.nv-barsWrap')
  9502. .datum(data.filter(function(d) { return !d.disabled }));
  9503. barsWrap.call(multibar);
  9504. // Setup Axes
  9505. if (showXAxis) {
  9506. xAxis
  9507. .scale(x)
  9508. ._ticks( nv.utils.calcTicksX(availableWidth/100, data) )
  9509. .tickSize(-availableHeight, 0);
  9510. g.select('.nv-x.nv-axis')
  9511. .attr('transform', 'translate(0,' + y.range()[0] + ')');
  9512. g.select('.nv-x.nv-axis')
  9513. .call(xAxis);
  9514. var xTicks = g.select('.nv-x.nv-axis > g').selectAll('g');
  9515. xTicks
  9516. .selectAll('line, text')
  9517. .style('opacity', 1)
  9518. if (staggerLabels) {
  9519. var getTranslate = function(x,y) {
  9520. return "translate(" + x + "," + y + ")";
  9521. };
  9522. var staggerUp = 5, staggerDown = 17; //pixels to stagger by
  9523. // Issue #140
  9524. xTicks
  9525. .selectAll("text")
  9526. .attr('transform', function(d,i,j) {
  9527. return getTranslate(0, (j % 2 == 0 ? staggerUp : staggerDown));
  9528. });
  9529. var totalInBetweenTicks = d3.selectAll(".nv-x.nv-axis .nv-wrap g g text")[0].length;
  9530. g.selectAll(".nv-x.nv-axis .nv-axisMaxMin text")
  9531. .attr("transform", function(d,i) {
  9532. return getTranslate(0, (i === 0 || totalInBetweenTicks % 2 !== 0) ? staggerDown : staggerUp);
  9533. });
  9534. }
  9535. if (wrapLabels) {
  9536. g.selectAll('.tick text')
  9537. .call(nv.utils.wrapTicks, chart.xAxis.rangeBand())
  9538. }
  9539. if (reduceXTicks)
  9540. xTicks
  9541. .filter(function(d,i) {
  9542. return i % Math.ceil(data[0].values.length / (availableWidth / 100)) !== 0;
  9543. })
  9544. .selectAll('text, line')
  9545. .style('opacity', 0);
  9546. if(rotateLabels)
  9547. xTicks
  9548. .selectAll('.tick text')
  9549. .attr('transform', 'rotate(' + rotateLabels + ' 0,0)')
  9550. .style('text-anchor', rotateLabels > 0 ? 'start' : 'end');
  9551. g.select('.nv-x.nv-axis').selectAll('g.nv-axisMaxMin text')
  9552. .style('opacity', 1);
  9553. }
  9554. if (showYAxis) {
  9555. yAxis
  9556. .scale(y)
  9557. ._ticks( nv.utils.calcTicksY(availableHeight/36, data) )
  9558. .tickSize( -availableWidth, 0);
  9559. g.select('.nv-y.nv-axis')
  9560. .call(yAxis);
  9561. }
  9562. //Set up interactive layer
  9563. if (useInteractiveGuideline) {
  9564. interactiveLayer
  9565. .width(availableWidth)
  9566. .height(availableHeight)
  9567. .margin({left:margin.left, top:margin.top})
  9568. .svgContainer(container)
  9569. .xScale(x);
  9570. wrap.select(".nv-interactive").call(interactiveLayer);
  9571. }
  9572. //============================================================
  9573. // Event Handling/Dispatching (in chart's scope)
  9574. //------------------------------------------------------------
  9575. legend.dispatch.on('stateChange', function(newState) {
  9576. for (var key in newState)
  9577. state[key] = newState[key];
  9578. dispatch.stateChange(state);
  9579. chart.update();
  9580. });
  9581. controls.dispatch.on('legendClick', function(d,i) {
  9582. if (!d.disabled) return;
  9583. controlsData = controlsData.map(function(s) {
  9584. s.disabled = true;
  9585. return s;
  9586. });
  9587. d.disabled = false;
  9588. switch (d.key) {
  9589. case 'Grouped':
  9590. case controlLabels.grouped:
  9591. multibar.stacked(false);
  9592. break;
  9593. case 'Stacked':
  9594. case controlLabels.stacked:
  9595. multibar.stacked(true);
  9596. break;
  9597. }
  9598. state.stacked = multibar.stacked();
  9599. dispatch.stateChange(state);
  9600. chart.update();
  9601. });
  9602. // Update chart from a state object passed to event handler
  9603. dispatch.on('changeState', function(e) {
  9604. if (typeof e.disabled !== 'undefined') {
  9605. data.forEach(function(series,i) {
  9606. series.disabled = e.disabled[i];
  9607. });
  9608. state.disabled = e.disabled;
  9609. }
  9610. if (typeof e.stacked !== 'undefined') {
  9611. multibar.stacked(e.stacked);
  9612. state.stacked = e.stacked;
  9613. stacked = e.stacked;
  9614. }
  9615. chart.update();
  9616. });
  9617. if (useInteractiveGuideline) {
  9618. interactiveLayer.dispatch.on('elementMousemove', function(e) {
  9619. if (e.pointXValue == undefined) return;
  9620. var singlePoint, pointIndex, pointXLocation, xValue, allData = [];
  9621. data
  9622. .filter(function(series, i) {
  9623. series.seriesIndex = i;
  9624. return !series.disabled;
  9625. })
  9626. .forEach(function(series,i) {
  9627. pointIndex = x.domain().indexOf(e.pointXValue)
  9628. var point = series.values[pointIndex];
  9629. if (point === undefined) return;
  9630. xValue = point.x;
  9631. if (singlePoint === undefined) singlePoint = point;
  9632. if (pointXLocation === undefined) pointXLocation = e.mouseX
  9633. allData.push({
  9634. key: series.key,
  9635. value: chart.y()(point, pointIndex),
  9636. color: color(series,series.seriesIndex),
  9637. data: series.values[pointIndex]
  9638. });
  9639. });
  9640. interactiveLayer.tooltip
  9641. .data({
  9642. value: xValue,
  9643. index: pointIndex,
  9644. series: allData
  9645. })();
  9646. interactiveLayer.renderGuideLine(pointXLocation);
  9647. });
  9648. interactiveLayer.dispatch.on("elementMouseout",function(e) {
  9649. interactiveLayer.tooltip.hidden(true);
  9650. });
  9651. }
  9652. else {
  9653. multibar.dispatch.on('elementMouseover.tooltip', function(evt) {
  9654. evt.value = chart.x()(evt.data);
  9655. evt['series'] = {
  9656. key: evt.data.key,
  9657. value: chart.y()(evt.data),
  9658. color: evt.color
  9659. };
  9660. tooltip.data(evt).hidden(false);
  9661. });
  9662. multibar.dispatch.on('elementMouseout.tooltip', function(evt) {
  9663. tooltip.hidden(true);
  9664. });
  9665. multibar.dispatch.on('elementMousemove.tooltip', function(evt) {
  9666. tooltip();
  9667. });
  9668. }
  9669. });
  9670. renderWatch.renderEnd('multibarchart immediate');
  9671. return chart;
  9672. }
  9673. //============================================================
  9674. // Expose Public Variables
  9675. //------------------------------------------------------------
  9676. // expose chart's sub-components
  9677. chart.dispatch = dispatch;
  9678. chart.multibar = multibar;
  9679. chart.legend = legend;
  9680. chart.controls = controls;
  9681. chart.xAxis = xAxis;
  9682. chart.yAxis = yAxis;
  9683. chart.state = state;
  9684. chart.tooltip = tooltip;
  9685. chart.interactiveLayer = interactiveLayer;
  9686. chart.options = nv.utils.optionsFunc.bind(chart);
  9687. chart._options = Object.create({}, {
  9688. // simple options, just get/set the necessary values
  9689. width: {get: function(){return width;}, set: function(_){width=_;}},
  9690. height: {get: function(){return height;}, set: function(_){height=_;}},
  9691. showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
  9692. legendPosition: {get: function(){return legendPosition;}, set: function(_){legendPosition=_;}},
  9693. showControls: {get: function(){return showControls;}, set: function(_){showControls=_;}},
  9694. controlLabels: {get: function(){return controlLabels;}, set: function(_){controlLabels=_;}},
  9695. showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}},
  9696. showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}},
  9697. defaultState: {get: function(){return defaultState;}, set: function(_){defaultState=_;}},
  9698. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  9699. reduceXTicks: {get: function(){return reduceXTicks;}, set: function(_){reduceXTicks=_;}},
  9700. rotateLabels: {get: function(){return rotateLabels;}, set: function(_){rotateLabels=_;}},
  9701. staggerLabels: {get: function(){return staggerLabels;}, set: function(_){staggerLabels=_;}},
  9702. wrapLabels: {get: function(){return wrapLabels;}, set: function(_){wrapLabels=!!_;}},
  9703. // options that require extra logic in the setter
  9704. margin: {get: function(){return margin;}, set: function(_){
  9705. if (_.top !== undefined) {
  9706. margin.top = _.top;
  9707. marginTop = _.top;
  9708. }
  9709. margin.right = _.right !== undefined ? _.right : margin.right;
  9710. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  9711. margin.left = _.left !== undefined ? _.left : margin.left;
  9712. }},
  9713. duration: {get: function(){return duration;}, set: function(_){
  9714. duration = _;
  9715. multibar.duration(duration);
  9716. xAxis.duration(duration);
  9717. yAxis.duration(duration);
  9718. renderWatch.reset(duration);
  9719. }},
  9720. color: {get: function(){return color;}, set: function(_){
  9721. color = nv.utils.getColor(_);
  9722. legend.color(color);
  9723. }},
  9724. rightAlignYAxis: {get: function(){return rightAlignYAxis;}, set: function(_){
  9725. rightAlignYAxis = _;
  9726. yAxis.orient( rightAlignYAxis ? 'right' : 'left');
  9727. }},
  9728. useInteractiveGuideline: {get: function(){return useInteractiveGuideline;}, set: function(_){
  9729. useInteractiveGuideline = _;
  9730. }},
  9731. barColor: {get: function(){return multibar.barColor;}, set: function(_){
  9732. multibar.barColor(_);
  9733. legend.color(function(d,i) {return d3.rgb('#ccc').darker(i * 1.5).toString();})
  9734. }}
  9735. });
  9736. nv.utils.inheritOptions(chart, multibar);
  9737. nv.utils.initOptions(chart);
  9738. return chart;
  9739. };
  9740. nv.models.multiBarHorizontal = function() {
  9741. "use strict";
  9742. //============================================================
  9743. // Public Variables with Default Settings
  9744. //------------------------------------------------------------
  9745. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  9746. , width = 960
  9747. , height = 500
  9748. , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one
  9749. , container = null
  9750. , x = d3.scale.ordinal()
  9751. , y = d3.scale.linear()
  9752. , getX = function(d) { return d.x }
  9753. , getY = function(d) { return d.y }
  9754. , getYerr = function(d) { return d.yErr }
  9755. , forceY = [0] // 0 is forced by default.. this makes sense for the majority of bar graphs... user can always do chart.forceY([]) to remove
  9756. , color = nv.utils.defaultColor()
  9757. , barColor = null // adding the ability to set the color for each rather than the whole group
  9758. , disabled // used in conjunction with barColor to communicate from multiBarHorizontalChart what series are disabled
  9759. , stacked = false
  9760. , showValues = false
  9761. , showBarLabels = false
  9762. , valuePadding = 60
  9763. , groupSpacing = 0.1
  9764. , fillOpacity = 0.75
  9765. , valueFormat = d3.format(',.2f')
  9766. , delay = 1200
  9767. , xDomain
  9768. , yDomain
  9769. , xRange
  9770. , yRange
  9771. , duration = 250
  9772. , dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout', 'elementMousemove', 'renderEnd')
  9773. ;
  9774. //============================================================
  9775. // Private Variables
  9776. //------------------------------------------------------------
  9777. var x0, y0; //used to store previous scales
  9778. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  9779. function chart(selection) {
  9780. renderWatch.reset();
  9781. selection.each(function(data) {
  9782. var availableWidth = width - margin.left - margin.right,
  9783. availableHeight = height - margin.top - margin.bottom;
  9784. container = d3.select(this);
  9785. nv.utils.initSVG(container);
  9786. if (stacked)
  9787. data = d3.layout.stack()
  9788. .offset('zero')
  9789. .values(function(d){ return d.values })
  9790. .y(getY)
  9791. (data);
  9792. //add series index and key to each data point for reference
  9793. data.forEach(function(series, i) {
  9794. series.values.forEach(function(point) {
  9795. point.series = i;
  9796. point.key = series.key;
  9797. });
  9798. });
  9799. // HACK for negative value stacking
  9800. if (stacked)
  9801. data[0].values.map(function(d,i) {
  9802. var posBase = 0, negBase = 0;
  9803. data.map(function(d) {
  9804. var f = d.values[i]
  9805. f.size = Math.abs(f.y);
  9806. if (f.y<0) {
  9807. f.y1 = negBase - f.size;
  9808. negBase = negBase - f.size;
  9809. } else
  9810. {
  9811. f.y1 = posBase;
  9812. posBase = posBase + f.size;
  9813. }
  9814. });
  9815. });
  9816. // Setup Scales
  9817. // remap and flatten the data for use in calculating the scales' domains
  9818. var seriesData = (xDomain && yDomain) ? [] : // if we know xDomain and yDomain, no need to calculate
  9819. data.map(function(d) {
  9820. return d.values.map(function(d,i) {
  9821. return { x: getX(d,i), y: getY(d,i), y0: d.y0, y1: d.y1 }
  9822. })
  9823. });
  9824. x.domain(xDomain || d3.merge(seriesData).map(function(d) { return d.x }))
  9825. .rangeBands(xRange || [0, availableHeight], groupSpacing);
  9826. y.domain(yDomain || d3.extent(d3.merge(seriesData).map(function(d) { return stacked ? (d.y > 0 ? d.y1 + d.y : d.y1 ) : d.y }).concat(forceY)))
  9827. if (showValues && !stacked)
  9828. y.range(yRange || [(y.domain()[0] < 0 ? valuePadding : 0), availableWidth - (y.domain()[1] > 0 ? valuePadding : 0) ]);
  9829. else
  9830. y.range(yRange || [0, availableWidth]);
  9831. x0 = x0 || x;
  9832. y0 = y0 || d3.scale.linear().domain(y.domain()).range([y(0),y(0)]);
  9833. // Setup containers and skeleton of chart
  9834. var wrap = d3.select(this).selectAll('g.nv-wrap.nv-multibarHorizontal').data([data]);
  9835. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-multibarHorizontal');
  9836. var defsEnter = wrapEnter.append('defs');
  9837. var gEnter = wrapEnter.append('g');
  9838. var g = wrap.select('g');
  9839. gEnter.append('g').attr('class', 'nv-groups');
  9840. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  9841. var groups = wrap.select('.nv-groups').selectAll('.nv-group')
  9842. .data(function(d) { return d }, function(d,i) { return i });
  9843. groups.enter().append('g')
  9844. .style('stroke-opacity', 1e-6)
  9845. .style('fill-opacity', 1e-6);
  9846. groups.exit().watchTransition(renderWatch, 'multibarhorizontal: exit groups')
  9847. .style('stroke-opacity', 1e-6)
  9848. .style('fill-opacity', 1e-6)
  9849. .remove();
  9850. groups
  9851. .attr('class', function(d,i) { return 'nv-group nv-series-' + i })
  9852. .classed('hover', function(d) { return d.hover })
  9853. .style('fill', function(d,i){ return color(d, i) })
  9854. .style('stroke', function(d,i){ return color(d, i) });
  9855. groups.watchTransition(renderWatch, 'multibarhorizontal: groups')
  9856. .style('stroke-opacity', 1)
  9857. .style('fill-opacity', fillOpacity);
  9858. var bars = groups.selectAll('g.nv-bar')
  9859. .data(function(d) { return d.values });
  9860. bars.exit().remove();
  9861. var barsEnter = bars.enter().append('g')
  9862. .attr('transform', function(d,i,j) {
  9863. return 'translate(' + y0(stacked ? d.y0 : 0) + ',' + (stacked ? 0 : (j * x.rangeBand() / data.length ) + x(getX(d,i))) + ')'
  9864. });
  9865. barsEnter.append('rect')
  9866. .attr('width', 0)
  9867. .attr('height', x.rangeBand() / (stacked ? 1 : data.length) )
  9868. bars
  9869. .on('mouseover', function(d,i) { //TODO: figure out why j works above, but not here
  9870. d3.select(this).classed('hover', true);
  9871. dispatch.elementMouseover({
  9872. data: d,
  9873. index: i,
  9874. color: d3.select(this).style("fill")
  9875. });
  9876. })
  9877. .on('mouseout', function(d,i) {
  9878. d3.select(this).classed('hover', false);
  9879. dispatch.elementMouseout({
  9880. data: d,
  9881. index: i,
  9882. color: d3.select(this).style("fill")
  9883. });
  9884. })
  9885. .on('mouseout', function(d,i) {
  9886. dispatch.elementMouseout({
  9887. data: d,
  9888. index: i,
  9889. color: d3.select(this).style("fill")
  9890. });
  9891. })
  9892. .on('mousemove', function(d,i) {
  9893. dispatch.elementMousemove({
  9894. data: d,
  9895. index: i,
  9896. color: d3.select(this).style("fill")
  9897. });
  9898. })
  9899. .on('click', function(d,i) {
  9900. var element = this;
  9901. dispatch.elementClick({
  9902. data: d,
  9903. index: i,
  9904. color: d3.select(this).style("fill"),
  9905. event: d3.event,
  9906. element: element
  9907. });
  9908. d3.event.stopPropagation();
  9909. })
  9910. .on('dblclick', function(d,i) {
  9911. dispatch.elementDblClick({
  9912. data: d,
  9913. index: i,
  9914. color: d3.select(this).style("fill")
  9915. });
  9916. d3.event.stopPropagation();
  9917. });
  9918. if (getYerr(data[0],0)) {
  9919. barsEnter.append('polyline');
  9920. bars.select('polyline')
  9921. .attr('fill', 'none')
  9922. .attr('points', function(d,i) {
  9923. var xerr = getYerr(d,i)
  9924. , mid = 0.8 * x.rangeBand() / ((stacked ? 1 : data.length) * 2);
  9925. xerr = xerr.length ? xerr : [-Math.abs(xerr), Math.abs(xerr)];
  9926. xerr = xerr.map(function(e) { return y(e + ((getY(d,i) < 0) ? 0 : getY(d,i))) - y(0); });
  9927. var a = [[xerr[0],-mid], [xerr[0],mid], [xerr[0],0], [xerr[1],0], [xerr[1],-mid], [xerr[1],mid]];
  9928. return a.map(function (path) { return path.join(',') }).join(' ');
  9929. })
  9930. .attr('transform', function(d,i) {
  9931. var mid = x.rangeBand() / ((stacked ? 1 : data.length) * 2);
  9932. return 'translate(0, ' + mid + ')';
  9933. });
  9934. }
  9935. barsEnter.append('text');
  9936. if (showValues && !stacked) {
  9937. bars.select('text')
  9938. .attr('text-anchor', function(d,i) { return getY(d,i) < 0 ? 'end' : 'start' })
  9939. .attr('y', x.rangeBand() / (data.length * 2))
  9940. .attr('dy', '.32em')
  9941. .text(function(d,i) {
  9942. var t = valueFormat(getY(d,i))
  9943. , yerr = getYerr(d,i);
  9944. if (yerr === undefined)
  9945. return t;
  9946. if (!yerr.length)
  9947. return t + '±' + valueFormat(Math.abs(yerr));
  9948. return t + '+' + valueFormat(Math.abs(yerr[1])) + '-' + valueFormat(Math.abs(yerr[0]));
  9949. });
  9950. bars.watchTransition(renderWatch, 'multibarhorizontal: bars')
  9951. .select('text')
  9952. .attr('x', function(d,i) { return getY(d,i) < 0 ? -4 : y(getY(d,i)) - y(0) + 4 })
  9953. } else {
  9954. bars.selectAll('text').text('');
  9955. }
  9956. if (showBarLabels && !stacked) {
  9957. barsEnter.append('text').classed('nv-bar-label',true);
  9958. bars.select('text.nv-bar-label')
  9959. .attr('text-anchor', function(d,i) { return getY(d,i) < 0 ? 'start' : 'end' })
  9960. .attr('y', x.rangeBand() / (data.length * 2))
  9961. .attr('dy', '.32em')
  9962. .text(function(d,i) { return getX(d,i) });
  9963. bars.watchTransition(renderWatch, 'multibarhorizontal: bars')
  9964. .select('text.nv-bar-label')
  9965. .attr('x', function(d,i) { return getY(d,i) < 0 ? y(0) - y(getY(d,i)) + 4 : -4 });
  9966. }
  9967. else {
  9968. bars.selectAll('text.nv-bar-label').text('');
  9969. }
  9970. bars
  9971. .attr('class', function(d,i) { return getY(d,i) < 0 ? 'nv-bar negative' : 'nv-bar positive'})
  9972. if (barColor) {
  9973. if (!disabled) disabled = data.map(function() { return true });
  9974. bars
  9975. .style('fill', function(d,i,j) { return d3.rgb(barColor(d,i)).darker( disabled.map(function(d,i) { return i }).filter(function(d,i){ return !disabled[i] })[j] ).toString(); })
  9976. .style('stroke', function(d,i,j) { return d3.rgb(barColor(d,i)).darker( disabled.map(function(d,i) { return i }).filter(function(d,i){ return !disabled[i] })[j] ).toString(); });
  9977. }
  9978. if (stacked)
  9979. bars.watchTransition(renderWatch, 'multibarhorizontal: bars')
  9980. .attr('transform', function(d,i) {
  9981. return 'translate(' + y(d.y1) + ',' + x(getX(d,i)) + ')'
  9982. })
  9983. .select('rect')
  9984. .attr('width', function(d,i) {
  9985. return Math.abs(y(getY(d,i) + d.y0) - y(d.y0)) || 0
  9986. })
  9987. .attr('height', x.rangeBand() );
  9988. else
  9989. bars.watchTransition(renderWatch, 'multibarhorizontal: bars')
  9990. .attr('transform', function(d,i) {
  9991. //TODO: stacked must be all positive or all negative, not both?
  9992. return 'translate(' +
  9993. (getY(d,i) < 0 ? y(getY(d,i)) : y(0))
  9994. + ',' +
  9995. (d.series * x.rangeBand() / data.length
  9996. +
  9997. x(getX(d,i)) )
  9998. + ')'
  9999. })
  10000. .select('rect')
  10001. .attr('height', x.rangeBand() / data.length )
  10002. .attr('width', function(d,i) {
  10003. return Math.max(Math.abs(y(getY(d,i)) - y(0)),1) || 0
  10004. });
  10005. //store old scales for use in transitions on update
  10006. x0 = x.copy();
  10007. y0 = y.copy();
  10008. });
  10009. renderWatch.renderEnd('multibarHorizontal immediate');
  10010. return chart;
  10011. }
  10012. //============================================================
  10013. // Expose Public Variables
  10014. //------------------------------------------------------------
  10015. chart.dispatch = dispatch;
  10016. chart.options = nv.utils.optionsFunc.bind(chart);
  10017. chart._options = Object.create({}, {
  10018. // simple options, just get/set the necessary values
  10019. width: {get: function(){return width;}, set: function(_){width=_;}},
  10020. height: {get: function(){return height;}, set: function(_){height=_;}},
  10021. x: {get: function(){return getX;}, set: function(_){getX=_;}},
  10022. y: {get: function(){return getY;}, set: function(_){getY=_;}},
  10023. yErr: {get: function(){return getYerr;}, set: function(_){getYerr=_;}},
  10024. xScale: {get: function(){return x;}, set: function(_){x=_;}},
  10025. yScale: {get: function(){return y;}, set: function(_){y=_;}},
  10026. xDomain: {get: function(){return xDomain;}, set: function(_){xDomain=_;}},
  10027. yDomain: {get: function(){return yDomain;}, set: function(_){yDomain=_;}},
  10028. xRange: {get: function(){return xRange;}, set: function(_){xRange=_;}},
  10029. yRange: {get: function(){return yRange;}, set: function(_){yRange=_;}},
  10030. forceY: {get: function(){return forceY;}, set: function(_){forceY=_;}},
  10031. stacked: {get: function(){return stacked;}, set: function(_){stacked=_;}},
  10032. showValues: {get: function(){return showValues;}, set: function(_){showValues=_;}},
  10033. // this shows the group name, seems pointless?
  10034. //showBarLabels: {get: function(){return showBarLabels;}, set: function(_){showBarLabels=_;}},
  10035. disabled: {get: function(){return disabled;}, set: function(_){disabled=_;}},
  10036. id: {get: function(){return id;}, set: function(_){id=_;}},
  10037. valueFormat: {get: function(){return valueFormat;}, set: function(_){valueFormat=_;}},
  10038. valuePadding: {get: function(){return valuePadding;}, set: function(_){valuePadding=_;}},
  10039. groupSpacing: {get: function(){return groupSpacing;}, set: function(_){groupSpacing=_;}},
  10040. fillOpacity: {get: function(){return fillOpacity;}, set: function(_){fillOpacity=_;}},
  10041. // options that require extra logic in the setter
  10042. margin: {get: function(){return margin;}, set: function(_){
  10043. margin.top = _.top !== undefined ? _.top : margin.top;
  10044. margin.right = _.right !== undefined ? _.right : margin.right;
  10045. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  10046. margin.left = _.left !== undefined ? _.left : margin.left;
  10047. }},
  10048. duration: {get: function(){return duration;}, set: function(_){
  10049. duration = _;
  10050. renderWatch.reset(duration);
  10051. }},
  10052. color: {get: function(){return color;}, set: function(_){
  10053. color = nv.utils.getColor(_);
  10054. }},
  10055. barColor: {get: function(){return barColor;}, set: function(_){
  10056. barColor = _ ? nv.utils.getColor(_) : null;
  10057. }}
  10058. });
  10059. nv.utils.initOptions(chart);
  10060. return chart;
  10061. };
  10062. nv.models.multiBarHorizontalChart = function() {
  10063. "use strict";
  10064. //============================================================
  10065. // Public Variables with Default Settings
  10066. //------------------------------------------------------------
  10067. var multibar = nv.models.multiBarHorizontal()
  10068. , xAxis = nv.models.axis()
  10069. , yAxis = nv.models.axis()
  10070. , legend = nv.models.legend().height(30)
  10071. , controls = nv.models.legend().height(30)
  10072. , tooltip = nv.models.tooltip()
  10073. ;
  10074. var margin = {top: 30, right: 20, bottom: 50, left: 60}
  10075. , marginTop = null
  10076. , width = null
  10077. , height = null
  10078. , color = nv.utils.defaultColor()
  10079. , showControls = true
  10080. , controlsPosition = 'top'
  10081. , controlLabels = {}
  10082. , showLegend = true
  10083. , legendPosition = 'top'
  10084. , showXAxis = true
  10085. , showYAxis = true
  10086. , stacked = false
  10087. , x //can be accessed via chart.xScale()
  10088. , y //can be accessed via chart.yScale()
  10089. , state = nv.utils.state()
  10090. , defaultState = null
  10091. , noData = null
  10092. , dispatch = d3.dispatch('stateChange', 'changeState','renderEnd')
  10093. , controlWidth = function() { return showControls ? 180 : 0 }
  10094. , duration = 250
  10095. ;
  10096. state.stacked = false; // DEPRECATED Maintained for backward compatibility
  10097. multibar.stacked(stacked);
  10098. xAxis
  10099. .orient('left')
  10100. .tickPadding(5)
  10101. .showMaxMin(false)
  10102. .tickFormat(function(d) { return d })
  10103. ;
  10104. yAxis
  10105. .orient('bottom')
  10106. .tickFormat(d3.format(',.1f'))
  10107. ;
  10108. tooltip
  10109. .duration(0)
  10110. .valueFormatter(function(d, i) {
  10111. return yAxis.tickFormat()(d, i);
  10112. })
  10113. .headerFormatter(function(d, i) {
  10114. return xAxis.tickFormat()(d, i);
  10115. });
  10116. controls.updateState(false);
  10117. //============================================================
  10118. // Private Variables
  10119. //------------------------------------------------------------
  10120. var stateGetter = function(data) {
  10121. return function(){
  10122. return {
  10123. active: data.map(function(d) { return !d.disabled }),
  10124. stacked: stacked
  10125. };
  10126. }
  10127. };
  10128. var stateSetter = function(data) {
  10129. return function(state) {
  10130. if (state.stacked !== undefined)
  10131. stacked = state.stacked;
  10132. if (state.active !== undefined)
  10133. data.forEach(function(series,i) {
  10134. series.disabled = !state.active[i];
  10135. });
  10136. }
  10137. };
  10138. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  10139. function chart(selection) {
  10140. renderWatch.reset();
  10141. renderWatch.models(multibar);
  10142. if (showXAxis) renderWatch.models(xAxis);
  10143. if (showYAxis) renderWatch.models(yAxis);
  10144. selection.each(function(data) {
  10145. var container = d3.select(this),
  10146. that = this;
  10147. nv.utils.initSVG(container);
  10148. var availableWidth = nv.utils.availableWidth(width, container, margin),
  10149. availableHeight = nv.utils.availableHeight(height, container, margin);
  10150. chart.update = function() { container.transition().duration(duration).call(chart) };
  10151. chart.container = this;
  10152. stacked = multibar.stacked();
  10153. state
  10154. .setter(stateSetter(data), chart.update)
  10155. .getter(stateGetter(data))
  10156. .update();
  10157. // DEPRECATED set state.disableddisabled
  10158. state.disabled = data.map(function(d) { return !!d.disabled });
  10159. if (!defaultState) {
  10160. var key;
  10161. defaultState = {};
  10162. for (key in state) {
  10163. if (state[key] instanceof Array)
  10164. defaultState[key] = state[key].slice(0);
  10165. else
  10166. defaultState[key] = state[key];
  10167. }
  10168. }
  10169. // Display No Data message if there's nothing to show.
  10170. if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
  10171. nv.utils.noData(chart, container)
  10172. return chart;
  10173. } else {
  10174. container.selectAll('.nv-noData').remove();
  10175. }
  10176. // Setup Scales
  10177. x = multibar.xScale();
  10178. y = multibar.yScale().clamp(true);
  10179. // Setup containers and skeleton of chart
  10180. var wrap = container.selectAll('g.nv-wrap.nv-multiBarHorizontalChart').data([data]);
  10181. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-multiBarHorizontalChart').append('g');
  10182. var g = wrap.select('g');
  10183. gEnter.append('g').attr('class', 'nv-x nv-axis');
  10184. gEnter.append('g').attr('class', 'nv-y nv-axis')
  10185. .append('g').attr('class', 'nv-zeroLine')
  10186. .append('line');
  10187. gEnter.append('g').attr('class', 'nv-barsWrap');
  10188. gEnter.append('g').attr('class', 'nv-legendWrap');
  10189. gEnter.append('g').attr('class', 'nv-controlsWrap');
  10190. // Legend
  10191. if (!showLegend) {
  10192. g.select('.nv-legendWrap').selectAll('*').remove();
  10193. } else {
  10194. legend.width(availableWidth - controlWidth());
  10195. g.select('.nv-legendWrap')
  10196. .datum(data)
  10197. .call(legend);
  10198. if (legendPosition === 'bottom') {
  10199. margin.bottom = xAxis.height() + legend.height();
  10200. availableHeight = nv.utils.availableHeight(height, container, margin);
  10201. g.select('.nv-legendWrap')
  10202. .attr('transform', 'translate(' + controlWidth() + ',' + (availableHeight + xAxis.height()) +')');
  10203. } else if (legendPosition === 'top') {
  10204. if (!marginTop && legend.height() !== margin.top) {
  10205. margin.top = legend.height();
  10206. availableHeight = nv.utils.availableHeight(height, container, margin);
  10207. }
  10208. g.select('.nv-legendWrap')
  10209. .attr('transform', 'translate(' + controlWidth() + ',' + (-margin.top) +')');
  10210. }
  10211. }
  10212. // Controls
  10213. if (!showControls) {
  10214. g.select('.nv-controlsWrap').selectAll('*').remove();
  10215. } else {
  10216. var controlsData = [
  10217. { key: controlLabels.grouped || 'Grouped', disabled: multibar.stacked() },
  10218. { key: controlLabels.stacked || 'Stacked', disabled: !multibar.stacked() }
  10219. ];
  10220. controls.width(controlWidth()).color(['#444', '#444', '#444']);
  10221. if (controlsPosition === 'bottom') {
  10222. margin.bottom = xAxis.height() + legend.height();
  10223. availableHeight = nv.utils.availableHeight(height, container, margin);
  10224. g.select('.nv-controlsWrap')
  10225. .datum(controlsData)
  10226. .attr('transform', 'translate(0,' + (availableHeight + xAxis.height()) +')')
  10227. .call(controls);
  10228. } else if (controlsPosition === 'top') {
  10229. g.select('.nv-controlsWrap')
  10230. .datum(controlsData)
  10231. .attr('transform', 'translate(0,' + (-margin.top) +')')
  10232. .call(controls);
  10233. }
  10234. }
  10235. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  10236. // Main Chart Component(s)
  10237. multibar
  10238. .disabled(data.map(function(series) { return series.disabled }))
  10239. .width(availableWidth)
  10240. .height(availableHeight)
  10241. .color(data.map(function(d,i) {
  10242. return d.color || color(d, i);
  10243. }).filter(function(d,i) { return !data[i].disabled }));
  10244. var barsWrap = g.select('.nv-barsWrap')
  10245. .datum(data.filter(function(d) { return !d.disabled }));
  10246. barsWrap.transition().call(multibar);
  10247. // Setup Axes
  10248. if (showXAxis) {
  10249. xAxis
  10250. .scale(x)
  10251. ._ticks( nv.utils.calcTicksY(availableHeight/24, data) )
  10252. .tickSize(-availableWidth, 0);
  10253. g.select('.nv-x.nv-axis').call(xAxis);
  10254. var xTicks = g.select('.nv-x.nv-axis').selectAll('g');
  10255. xTicks
  10256. .selectAll('line, text');
  10257. }
  10258. if (showYAxis) {
  10259. yAxis
  10260. .scale(y)
  10261. ._ticks( nv.utils.calcTicksX(availableWidth/100, data) )
  10262. .tickSize( -availableHeight, 0);
  10263. g.select('.nv-y.nv-axis')
  10264. .attr('transform', 'translate(0,' + availableHeight + ')');
  10265. g.select('.nv-y.nv-axis').call(yAxis);
  10266. }
  10267. // Zero line
  10268. g.select(".nv-zeroLine line")
  10269. .attr("x1", y(0))
  10270. .attr("x2", y(0))
  10271. .attr("y1", 0)
  10272. .attr("y2", -availableHeight)
  10273. ;
  10274. //============================================================
  10275. // Event Handling/Dispatching (in chart's scope)
  10276. //------------------------------------------------------------
  10277. legend.dispatch.on('stateChange', function(newState) {
  10278. for (var key in newState)
  10279. state[key] = newState[key];
  10280. dispatch.stateChange(state);
  10281. chart.update();
  10282. });
  10283. controls.dispatch.on('legendClick', function(d,i) {
  10284. if (!d.disabled) return;
  10285. controlsData = controlsData.map(function(s) {
  10286. s.disabled = true;
  10287. return s;
  10288. });
  10289. d.disabled = false;
  10290. switch (d.key) {
  10291. case 'Grouped':
  10292. case controlLabels.grouped:
  10293. multibar.stacked(false);
  10294. break;
  10295. case 'Stacked':
  10296. case controlLabels.stacked:
  10297. multibar.stacked(true);
  10298. break;
  10299. }
  10300. state.stacked = multibar.stacked();
  10301. dispatch.stateChange(state);
  10302. stacked = multibar.stacked();
  10303. chart.update();
  10304. });
  10305. // Update chart from a state object passed to event handler
  10306. dispatch.on('changeState', function(e) {
  10307. if (typeof e.disabled !== 'undefined') {
  10308. data.forEach(function(series,i) {
  10309. series.disabled = e.disabled[i];
  10310. });
  10311. state.disabled = e.disabled;
  10312. }
  10313. if (typeof e.stacked !== 'undefined') {
  10314. multibar.stacked(e.stacked);
  10315. state.stacked = e.stacked;
  10316. stacked = e.stacked;
  10317. }
  10318. chart.update();
  10319. });
  10320. });
  10321. renderWatch.renderEnd('multibar horizontal chart immediate');
  10322. return chart;
  10323. }
  10324. //============================================================
  10325. // Event Handling/Dispatching (out of chart's scope)
  10326. //------------------------------------------------------------
  10327. multibar.dispatch.on('elementMouseover.tooltip', function(evt) {
  10328. evt.value = chart.x()(evt.data);
  10329. evt['series'] = {
  10330. key: evt.data.key,
  10331. value: chart.y()(evt.data),
  10332. color: evt.color
  10333. };
  10334. tooltip.data(evt).hidden(false);
  10335. });
  10336. multibar.dispatch.on('elementMouseout.tooltip', function(evt) {
  10337. tooltip.hidden(true);
  10338. });
  10339. multibar.dispatch.on('elementMousemove.tooltip', function(evt) {
  10340. tooltip();
  10341. });
  10342. //============================================================
  10343. // Expose Public Variables
  10344. //------------------------------------------------------------
  10345. // expose chart's sub-components
  10346. chart.dispatch = dispatch;
  10347. chart.multibar = multibar;
  10348. chart.legend = legend;
  10349. chart.controls = controls;
  10350. chart.xAxis = xAxis;
  10351. chart.yAxis = yAxis;
  10352. chart.state = state;
  10353. chart.tooltip = tooltip;
  10354. chart.options = nv.utils.optionsFunc.bind(chart);
  10355. chart._options = Object.create({}, {
  10356. // simple options, just get/set the necessary values
  10357. width: {get: function(){return width;}, set: function(_){width=_;}},
  10358. height: {get: function(){return height;}, set: function(_){height=_;}},
  10359. showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
  10360. legendPosition: {get: function(){return legendPosition;}, set: function(_){legendPosition=_;}},
  10361. controlsPosition: {get: function(){return controlsPosition;}, set: function(_){controlsPosition=_;}},
  10362. showControls: {get: function(){return showControls;}, set: function(_){showControls=_;}},
  10363. controlLabels: {get: function(){return controlLabels;}, set: function(_){controlLabels=_;}},
  10364. showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}},
  10365. showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}},
  10366. defaultState: {get: function(){return defaultState;}, set: function(_){defaultState=_;}},
  10367. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  10368. // options that require extra logic in the setter
  10369. margin: {get: function(){return margin;}, set: function(_){
  10370. if (_.top !== undefined) {
  10371. margin.top = _.top;
  10372. marginTop = _.top;
  10373. }
  10374. margin.right = _.right !== undefined ? _.right : margin.right;
  10375. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  10376. margin.left = _.left !== undefined ? _.left : margin.left;
  10377. }},
  10378. duration: {get: function(){return duration;}, set: function(_){
  10379. duration = _;
  10380. renderWatch.reset(duration);
  10381. multibar.duration(duration);
  10382. xAxis.duration(duration);
  10383. yAxis.duration(duration);
  10384. }},
  10385. color: {get: function(){return color;}, set: function(_){
  10386. color = nv.utils.getColor(_);
  10387. legend.color(color);
  10388. }},
  10389. barColor: {get: function(){return multibar.barColor;}, set: function(_){
  10390. multibar.barColor(_);
  10391. legend.color(function(d,i) {return d3.rgb('#ccc').darker(i * 1.5).toString();})
  10392. }}
  10393. });
  10394. nv.utils.inheritOptions(chart, multibar);
  10395. nv.utils.initOptions(chart);
  10396. return chart;
  10397. };
  10398. nv.models.multiChart = function() {
  10399. "use strict";
  10400. //============================================================
  10401. // Public Variables with Default Settings
  10402. //------------------------------------------------------------
  10403. var margin = {top: 30, right: 20, bottom: 50, left: 60},
  10404. marginTop = null,
  10405. color = nv.utils.defaultColor(),
  10406. width = null,
  10407. height = null,
  10408. showLegend = true,
  10409. noData = null,
  10410. yDomain1,
  10411. yDomain2,
  10412. getX = function(d) { return d.x },
  10413. getY = function(d) { return d.y},
  10414. interpolate = 'linear',
  10415. useVoronoi = true,
  10416. interactiveLayer = nv.interactiveGuideline(),
  10417. useInteractiveGuideline = false,
  10418. legendRightAxisHint = ' (right axis)',
  10419. duration = 250
  10420. ;
  10421. //============================================================
  10422. // Private Variables
  10423. //------------------------------------------------------------
  10424. var x = d3.scale.linear(),
  10425. yScale1 = d3.scale.linear(),
  10426. yScale2 = d3.scale.linear(),
  10427. lines1 = nv.models.line().yScale(yScale1).duration(duration),
  10428. lines2 = nv.models.line().yScale(yScale2).duration(duration),
  10429. scatters1 = nv.models.scatter().yScale(yScale1).duration(duration),
  10430. scatters2 = nv.models.scatter().yScale(yScale2).duration(duration),
  10431. bars1 = nv.models.multiBar().stacked(false).yScale(yScale1).duration(duration),
  10432. bars2 = nv.models.multiBar().stacked(false).yScale(yScale2).duration(duration),
  10433. stack1 = nv.models.stackedArea().yScale(yScale1).duration(duration),
  10434. stack2 = nv.models.stackedArea().yScale(yScale2).duration(duration),
  10435. xAxis = nv.models.axis().scale(x).orient('bottom').tickPadding(5).duration(duration),
  10436. yAxis1 = nv.models.axis().scale(yScale1).orient('left').duration(duration),
  10437. yAxis2 = nv.models.axis().scale(yScale2).orient('right').duration(duration),
  10438. legend = nv.models.legend().height(30),
  10439. tooltip = nv.models.tooltip(),
  10440. dispatch = d3.dispatch();
  10441. var charts = [lines1, lines2, scatters1, scatters2, bars1, bars2, stack1, stack2];
  10442. function chart(selection) {
  10443. selection.each(function(data) {
  10444. var container = d3.select(this),
  10445. that = this;
  10446. nv.utils.initSVG(container);
  10447. chart.update = function() { container.transition().call(chart); };
  10448. chart.container = this;
  10449. var availableWidth = nv.utils.availableWidth(width, container, margin),
  10450. availableHeight = nv.utils.availableHeight(height, container, margin);
  10451. var dataLines1 = data.filter(function(d) {return d.type == 'line' && d.yAxis == 1});
  10452. var dataLines2 = data.filter(function(d) {return d.type == 'line' && d.yAxis == 2});
  10453. var dataScatters1 = data.filter(function(d) {return d.type == 'scatter' && d.yAxis == 1});
  10454. var dataScatters2 = data.filter(function(d) {return d.type == 'scatter' && d.yAxis == 2});
  10455. var dataBars1 = data.filter(function(d) {return d.type == 'bar' && d.yAxis == 1});
  10456. var dataBars2 = data.filter(function(d) {return d.type == 'bar' && d.yAxis == 2});
  10457. var dataStack1 = data.filter(function(d) {return d.type == 'area' && d.yAxis == 1});
  10458. var dataStack2 = data.filter(function(d) {return d.type == 'area' && d.yAxis == 2});
  10459. // Display noData message if there's nothing to show.
  10460. if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
  10461. nv.utils.noData(chart, container);
  10462. return chart;
  10463. } else {
  10464. container.selectAll('.nv-noData').remove();
  10465. }
  10466. var series1 = data.filter(function(d) {return !d.disabled && d.yAxis == 1})
  10467. .map(function(d) {
  10468. return d.values.map(function(d,i) {
  10469. return { x: getX(d), y: getY(d) }
  10470. })
  10471. });
  10472. var series2 = data.filter(function(d) {return !d.disabled && d.yAxis == 2})
  10473. .map(function(d) {
  10474. return d.values.map(function(d,i) {
  10475. return { x: getX(d), y: getY(d) }
  10476. })
  10477. });
  10478. x .domain(d3.extent(d3.merge(series1.concat(series2)), function(d) { return d.x }))
  10479. .range([0, availableWidth]);
  10480. var wrap = container.selectAll('g.wrap.multiChart').data([data]);
  10481. var gEnter = wrap.enter().append('g').attr('class', 'wrap nvd3 multiChart').append('g');
  10482. gEnter.append('g').attr('class', 'nv-x nv-axis');
  10483. gEnter.append('g').attr('class', 'nv-y1 nv-axis');
  10484. gEnter.append('g').attr('class', 'nv-y2 nv-axis');
  10485. gEnter.append('g').attr('class', 'stack1Wrap');
  10486. gEnter.append('g').attr('class', 'stack2Wrap');
  10487. gEnter.append('g').attr('class', 'bars1Wrap');
  10488. gEnter.append('g').attr('class', 'bars2Wrap');
  10489. gEnter.append('g').attr('class', 'scatters1Wrap');
  10490. gEnter.append('g').attr('class', 'scatters2Wrap');
  10491. gEnter.append('g').attr('class', 'lines1Wrap');
  10492. gEnter.append('g').attr('class', 'lines2Wrap');
  10493. gEnter.append('g').attr('class', 'legendWrap');
  10494. gEnter.append('g').attr('class', 'nv-interactive');
  10495. var g = wrap.select('g');
  10496. var color_array = data.map(function(d,i) {
  10497. return data[i].color || color(d, i);
  10498. });
  10499. // Legend
  10500. if (!showLegend) {
  10501. g.select('.legendWrap').selectAll('*').remove();
  10502. } else {
  10503. var legendWidth = legend.align() ? availableWidth / 2 : availableWidth;
  10504. var legendXPosition = legend.align() ? legendWidth : 0;
  10505. legend.width(legendWidth);
  10506. legend.color(color_array);
  10507. g.select('.legendWrap')
  10508. .datum(data.map(function(series) {
  10509. series.originalKey = series.originalKey === undefined ? series.key : series.originalKey;
  10510. series.key = series.originalKey + (series.yAxis == 1 ? '' : legendRightAxisHint);
  10511. return series;
  10512. }))
  10513. .call(legend);
  10514. if (!marginTop && legend.height() !== margin.top) {
  10515. margin.top = legend.height();
  10516. availableHeight = nv.utils.availableHeight(height, container, margin);
  10517. }
  10518. g.select('.legendWrap')
  10519. .attr('transform', 'translate(' + legendXPosition + ',' + (-margin.top) +')');
  10520. }
  10521. lines1
  10522. .width(availableWidth)
  10523. .height(availableHeight)
  10524. .interpolate(interpolate)
  10525. .color(color_array.filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 1 && data[i].type == 'line'}));
  10526. lines2
  10527. .width(availableWidth)
  10528. .height(availableHeight)
  10529. .interpolate(interpolate)
  10530. .color(color_array.filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 2 && data[i].type == 'line'}));
  10531. scatters1
  10532. .width(availableWidth)
  10533. .height(availableHeight)
  10534. .color(color_array.filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 1 && data[i].type == 'scatter'}));
  10535. scatters2
  10536. .width(availableWidth)
  10537. .height(availableHeight)
  10538. .color(color_array.filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 2 && data[i].type == 'scatter'}));
  10539. bars1
  10540. .width(availableWidth)
  10541. .height(availableHeight)
  10542. .color(color_array.filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 1 && data[i].type == 'bar'}));
  10543. bars2
  10544. .width(availableWidth)
  10545. .height(availableHeight)
  10546. .color(color_array.filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 2 && data[i].type == 'bar'}));
  10547. stack1
  10548. .width(availableWidth)
  10549. .height(availableHeight)
  10550. .interpolate(interpolate)
  10551. .color(color_array.filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 1 && data[i].type == 'area'}));
  10552. stack2
  10553. .width(availableWidth)
  10554. .height(availableHeight)
  10555. .interpolate(interpolate)
  10556. .color(color_array.filter(function(d,i) { return !data[i].disabled && data[i].yAxis == 2 && data[i].type == 'area'}));
  10557. g.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  10558. var lines1Wrap = g.select('.lines1Wrap')
  10559. .datum(dataLines1.filter(function(d){return !d.disabled}));
  10560. var scatters1Wrap = g.select('.scatters1Wrap')
  10561. .datum(dataScatters1.filter(function(d){return !d.disabled}));
  10562. var bars1Wrap = g.select('.bars1Wrap')
  10563. .datum(dataBars1.filter(function(d){return !d.disabled}));
  10564. var stack1Wrap = g.select('.stack1Wrap')
  10565. .datum(dataStack1.filter(function(d){return !d.disabled}));
  10566. var lines2Wrap = g.select('.lines2Wrap')
  10567. .datum(dataLines2.filter(function(d){return !d.disabled}));
  10568. var scatters2Wrap = g.select('.scatters2Wrap')
  10569. .datum(dataScatters2.filter(function(d){return !d.disabled}));
  10570. var bars2Wrap = g.select('.bars2Wrap')
  10571. .datum(dataBars2.filter(function(d){return !d.disabled}));
  10572. var stack2Wrap = g.select('.stack2Wrap')
  10573. .datum(dataStack2.filter(function(d){return !d.disabled}));
  10574. var extraValue1BarStacked = [];
  10575. if (bars1.stacked() && dataBars1.length) {
  10576. var extraValue1BarStacked = dataBars1.filter(function(d){return !d.disabled}).map(function(a){return a.values});
  10577. if (extraValue1BarStacked.length > 0)
  10578. extraValue1BarStacked = extraValue1BarStacked.reduce(function(a,b){
  10579. return a.map(function(aVal,i){return {x: aVal.x, y: aVal.y + b[i].y}})
  10580. });
  10581. }
  10582. if (dataBars1.length) {
  10583. extraValue1BarStacked.push({x:0, y:0});
  10584. }
  10585. var extraValue2BarStacked = [];
  10586. if (bars2.stacked() && dataBars2.length) {
  10587. var extraValue2BarStacked = dataBars2.filter(function(d){return !d.disabled}).map(function(a){return a.values});
  10588. if (extraValue2BarStacked.length > 0)
  10589. extraValue2BarStacked = extraValue2BarStacked.reduce(function(a,b){
  10590. return a.map(function(aVal,i){return {x: aVal.x, y: aVal.y + b[i].y}})
  10591. });
  10592. }
  10593. if (dataBars2.length) {
  10594. extraValue2BarStacked.push({x:0, y:0});
  10595. }
  10596. yScale1 .domain(yDomain1 || d3.extent(d3.merge(series1).concat(extraValue1BarStacked), function(d) { return d.y } ))
  10597. .range([0, availableHeight]);
  10598. yScale2 .domain(yDomain2 || d3.extent(d3.merge(series2).concat(extraValue2BarStacked), function(d) { return d.y } ))
  10599. .range([0, availableHeight]);
  10600. lines1.yDomain(yScale1.domain());
  10601. scatters1.yDomain(yScale1.domain());
  10602. bars1.yDomain(yScale1.domain());
  10603. stack1.yDomain(yScale1.domain());
  10604. lines2.yDomain(yScale2.domain());
  10605. scatters2.yDomain(yScale2.domain());
  10606. bars2.yDomain(yScale2.domain());
  10607. stack2.yDomain(yScale2.domain());
  10608. if(dataStack1.length){d3.transition(stack1Wrap).call(stack1);}
  10609. if(dataStack2.length){d3.transition(stack2Wrap).call(stack2);}
  10610. if(dataBars1.length){d3.transition(bars1Wrap).call(bars1);}
  10611. if(dataBars2.length){d3.transition(bars2Wrap).call(bars2);}
  10612. if(dataLines1.length){d3.transition(lines1Wrap).call(lines1);}
  10613. if(dataLines2.length){d3.transition(lines2Wrap).call(lines2);}
  10614. if(dataScatters1.length){d3.transition(scatters1Wrap).call(scatters1);}
  10615. if(dataScatters2.length){d3.transition(scatters2Wrap).call(scatters2);}
  10616. xAxis
  10617. ._ticks( nv.utils.calcTicksX(availableWidth/100, data) )
  10618. .tickSize(-availableHeight, 0);
  10619. g.select('.nv-x.nv-axis')
  10620. .attr('transform', 'translate(0,' + availableHeight + ')');
  10621. d3.transition(g.select('.nv-x.nv-axis'))
  10622. .call(xAxis);
  10623. yAxis1
  10624. ._ticks( nv.utils.calcTicksY(availableHeight/36, data) )
  10625. .tickSize( -availableWidth, 0);
  10626. d3.transition(g.select('.nv-y1.nv-axis'))
  10627. .call(yAxis1);
  10628. yAxis2
  10629. ._ticks( nv.utils.calcTicksY(availableHeight/36, data) )
  10630. .tickSize( -availableWidth, 0);
  10631. d3.transition(g.select('.nv-y2.nv-axis'))
  10632. .call(yAxis2);
  10633. g.select('.nv-y1.nv-axis')
  10634. .classed('nv-disabled', series1.length ? false : true)
  10635. .attr('transform', 'translate(' + x.range()[0] + ',0)');
  10636. g.select('.nv-y2.nv-axis')
  10637. .classed('nv-disabled', series2.length ? false : true)
  10638. .attr('transform', 'translate(' + x.range()[1] + ',0)');
  10639. legend.dispatch.on('stateChange', function(newState) {
  10640. chart.update();
  10641. });
  10642. if(useInteractiveGuideline){
  10643. interactiveLayer
  10644. .width(availableWidth)
  10645. .height(availableHeight)
  10646. .margin({left:margin.left, top:margin.top})
  10647. .svgContainer(container)
  10648. .xScale(x);
  10649. wrap.select(".nv-interactive").call(interactiveLayer);
  10650. }
  10651. //============================================================
  10652. // Event Handling/Dispatching
  10653. //------------------------------------------------------------
  10654. function mouseover_line(evt) {
  10655. var yaxis = evt.series.yAxis === 2 ? yAxis2 : yAxis1;
  10656. evt.value = evt.point.x;
  10657. evt.series = {
  10658. value: evt.point.y,
  10659. color: evt.point.color,
  10660. key: evt.series.key
  10661. };
  10662. tooltip
  10663. .duration(0)
  10664. .headerFormatter(function(d, i) {
  10665. return xAxis.tickFormat()(d, i);
  10666. })
  10667. .valueFormatter(function(d, i) {
  10668. return yaxis.tickFormat()(d, i);
  10669. })
  10670. .data(evt)
  10671. .hidden(false);
  10672. }
  10673. function mouseover_scatter(evt) {
  10674. var yaxis = evt.series.yAxis === 2 ? yAxis2 : yAxis1;
  10675. evt.value = evt.point.x;
  10676. evt.series = {
  10677. value: evt.point.y,
  10678. color: evt.point.color,
  10679. key: evt.series.key
  10680. };
  10681. tooltip
  10682. .duration(100)
  10683. .headerFormatter(function(d, i) {
  10684. return xAxis.tickFormat()(d, i);
  10685. })
  10686. .valueFormatter(function(d, i) {
  10687. return yaxis.tickFormat()(d, i);
  10688. })
  10689. .data(evt)
  10690. .hidden(false);
  10691. }
  10692. function mouseover_stack(evt) {
  10693. var yaxis = evt.series.yAxis === 2 ? yAxis2 : yAxis1;
  10694. evt.point['x'] = stack1.x()(evt.point);
  10695. evt.point['y'] = stack1.y()(evt.point);
  10696. tooltip
  10697. .duration(0)
  10698. .headerFormatter(function(d, i) {
  10699. return xAxis.tickFormat()(d, i);
  10700. })
  10701. .valueFormatter(function(d, i) {
  10702. return yaxis.tickFormat()(d, i);
  10703. })
  10704. .data(evt)
  10705. .hidden(false);
  10706. }
  10707. function mouseover_bar(evt) {
  10708. var yaxis = evt.series.yAxis === 2 ? yAxis2 : yAxis1;
  10709. evt.value = bars1.x()(evt.data);
  10710. evt['series'] = {
  10711. value: bars1.y()(evt.data),
  10712. color: evt.color,
  10713. key: evt.data.key
  10714. };
  10715. tooltip
  10716. .duration(0)
  10717. .headerFormatter(function(d, i) {
  10718. return xAxis.tickFormat()(d, i);
  10719. })
  10720. .valueFormatter(function(d, i) {
  10721. return yaxis.tickFormat()(d, i);
  10722. })
  10723. .data(evt)
  10724. .hidden(false);
  10725. }
  10726. function clearHighlights() {
  10727. for(var i=0, il=charts.length; i < il; i++){
  10728. var chart = charts[i];
  10729. try {
  10730. chart.clearHighlights();
  10731. } catch(e){}
  10732. }
  10733. }
  10734. function highlightPoint(serieIndex, pointIndex, b){
  10735. for(var i=0, il=charts.length; i < il; i++){
  10736. var chart = charts[i];
  10737. try {
  10738. chart.highlightPoint(serieIndex, pointIndex, b);
  10739. } catch(e){}
  10740. }
  10741. }
  10742. if(useInteractiveGuideline){
  10743. interactiveLayer.dispatch.on('elementMousemove', function(e) {
  10744. clearHighlights();
  10745. var singlePoint, pointIndex, pointXLocation, allData = [];
  10746. data
  10747. .filter(function(series, i) {
  10748. series.seriesIndex = i;
  10749. return !series.disabled;
  10750. })
  10751. .forEach(function(series,i) {
  10752. var extent = x.domain();
  10753. var currentValues = series.values.filter(function(d,i) {
  10754. return chart.x()(d,i) >= extent[0] && chart.x()(d,i) <= extent[1];
  10755. });
  10756. pointIndex = nv.interactiveBisect(currentValues, e.pointXValue, chart.x());
  10757. var point = currentValues[pointIndex];
  10758. var pointYValue = chart.y()(point, pointIndex);
  10759. if (pointYValue !== null) {
  10760. highlightPoint(i, pointIndex, true);
  10761. }
  10762. if (point === undefined) return;
  10763. if (singlePoint === undefined) singlePoint = point;
  10764. if (pointXLocation === undefined) pointXLocation = x(chart.x()(point,pointIndex));
  10765. allData.push({
  10766. key: series.key,
  10767. value: pointYValue,
  10768. color: color(series,series.seriesIndex),
  10769. data: point,
  10770. yAxis: series.yAxis == 2 ? yAxis2 : yAxis1
  10771. });
  10772. });
  10773. var defaultValueFormatter = function(d,i) {
  10774. var yAxis = allData[i].yAxis;
  10775. return d == null ? "N/A" : yAxis.tickFormat()(d);
  10776. };
  10777. interactiveLayer.tooltip
  10778. .headerFormatter(function(d, i) {
  10779. return xAxis.tickFormat()(d, i);
  10780. })
  10781. .valueFormatter(interactiveLayer.tooltip.valueFormatter() || defaultValueFormatter)
  10782. .data({
  10783. value: chart.x()( singlePoint,pointIndex ),
  10784. index: pointIndex,
  10785. series: allData
  10786. })();
  10787. interactiveLayer.renderGuideLine(pointXLocation);
  10788. });
  10789. interactiveLayer.dispatch.on("elementMouseout",function(e) {
  10790. clearHighlights();
  10791. });
  10792. } else {
  10793. lines1.dispatch.on('elementMouseover.tooltip', mouseover_line);
  10794. lines2.dispatch.on('elementMouseover.tooltip', mouseover_line);
  10795. lines1.dispatch.on('elementMouseout.tooltip', function(evt) {
  10796. tooltip.hidden(true)
  10797. });
  10798. lines2.dispatch.on('elementMouseout.tooltip', function(evt) {
  10799. tooltip.hidden(true)
  10800. });
  10801. scatters1.dispatch.on('elementMouseover.tooltip', mouseover_scatter);
  10802. scatters2.dispatch.on('elementMouseover.tooltip', mouseover_scatter);
  10803. scatters1.dispatch.on('elementMouseout.tooltip', function(evt) {
  10804. tooltip.hidden(true)
  10805. });
  10806. scatters2.dispatch.on('elementMouseout.tooltip', function(evt) {
  10807. tooltip.hidden(true)
  10808. });
  10809. stack1.dispatch.on('elementMouseover.tooltip', mouseover_stack);
  10810. stack2.dispatch.on('elementMouseover.tooltip', mouseover_stack);
  10811. stack1.dispatch.on('elementMouseout.tooltip', function(evt) {
  10812. tooltip.hidden(true)
  10813. });
  10814. stack2.dispatch.on('elementMouseout.tooltip', function(evt) {
  10815. tooltip.hidden(true)
  10816. });
  10817. bars1.dispatch.on('elementMouseover.tooltip', mouseover_bar);
  10818. bars2.dispatch.on('elementMouseover.tooltip', mouseover_bar);
  10819. bars1.dispatch.on('elementMouseout.tooltip', function(evt) {
  10820. tooltip.hidden(true);
  10821. });
  10822. bars2.dispatch.on('elementMouseout.tooltip', function(evt) {
  10823. tooltip.hidden(true);
  10824. });
  10825. bars1.dispatch.on('elementMousemove.tooltip', function(evt) {
  10826. tooltip();
  10827. });
  10828. bars2.dispatch.on('elementMousemove.tooltip', function(evt) {
  10829. tooltip();
  10830. });
  10831. }
  10832. });
  10833. return chart;
  10834. }
  10835. //============================================================
  10836. // Global getters and setters
  10837. //------------------------------------------------------------
  10838. chart.dispatch = dispatch;
  10839. chart.legend = legend;
  10840. chart.lines1 = lines1;
  10841. chart.lines2 = lines2;
  10842. chart.scatters1 = scatters1;
  10843. chart.scatters2 = scatters2;
  10844. chart.bars1 = bars1;
  10845. chart.bars2 = bars2;
  10846. chart.stack1 = stack1;
  10847. chart.stack2 = stack2;
  10848. chart.xAxis = xAxis;
  10849. chart.yAxis1 = yAxis1;
  10850. chart.yAxis2 = yAxis2;
  10851. chart.tooltip = tooltip;
  10852. chart.interactiveLayer = interactiveLayer;
  10853. chart.options = nv.utils.optionsFunc.bind(chart);
  10854. chart._options = Object.create({}, {
  10855. // simple options, just get/set the necessary values
  10856. width: {get: function(){return width;}, set: function(_){width=_;}},
  10857. height: {get: function(){return height;}, set: function(_){height=_;}},
  10858. showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
  10859. xScale: {get: function(){return x;}, set: function(_){ x = _; xAxis.scale(x); }},
  10860. yDomain1: {get: function(){return yDomain1;}, set: function(_){yDomain1=_;}},
  10861. yDomain2: {get: function(){return yDomain2;}, set: function(_){yDomain2=_;}},
  10862. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  10863. interpolate: {get: function(){return interpolate;}, set: function(_){interpolate=_;}},
  10864. legendRightAxisHint: {get: function(){return legendRightAxisHint;}, set: function(_){legendRightAxisHint=_;}},
  10865. // options that require extra logic in the setter
  10866. margin: {get: function(){return margin;}, set: function(_){
  10867. if (_.top !== undefined) {
  10868. margin.top = _.top;
  10869. marginTop = _.top;
  10870. }
  10871. margin.right = _.right !== undefined ? _.right : margin.right;
  10872. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  10873. margin.left = _.left !== undefined ? _.left : margin.left;
  10874. }},
  10875. color: {get: function(){return color;}, set: function(_){
  10876. color = nv.utils.getColor(_);
  10877. }},
  10878. x: {get: function(){return getX;}, set: function(_){
  10879. getX = _;
  10880. lines1.x(_);
  10881. lines2.x(_);
  10882. scatters1.x(_);
  10883. scatters2.x(_);
  10884. bars1.x(_);
  10885. bars2.x(_);
  10886. stack1.x(_);
  10887. stack2.x(_);
  10888. }},
  10889. y: {get: function(){return getY;}, set: function(_){
  10890. getY = _;
  10891. lines1.y(_);
  10892. lines2.y(_);
  10893. scatters1.y(_);
  10894. scatters2.y(_);
  10895. stack1.y(_);
  10896. stack2.y(_);
  10897. bars1.y(_);
  10898. bars2.y(_);
  10899. }},
  10900. useVoronoi: {get: function(){return useVoronoi;}, set: function(_){
  10901. useVoronoi=_;
  10902. lines1.useVoronoi(_);
  10903. lines2.useVoronoi(_);
  10904. stack1.useVoronoi(_);
  10905. stack2.useVoronoi(_);
  10906. }},
  10907. useInteractiveGuideline: {get: function(){return useInteractiveGuideline;}, set: function(_){
  10908. useInteractiveGuideline = _;
  10909. if (useInteractiveGuideline) {
  10910. lines1.interactive(false);
  10911. lines1.useVoronoi(false);
  10912. lines2.interactive(false);
  10913. lines2.useVoronoi(false);
  10914. stack1.interactive(false);
  10915. stack1.useVoronoi(false);
  10916. stack2.interactive(false);
  10917. stack2.useVoronoi(false);
  10918. scatters1.interactive(false);
  10919. scatters2.interactive(false);
  10920. }
  10921. }},
  10922. duration: {get: function(){return duration;}, set: function(_) {
  10923. duration = _;
  10924. [lines1, lines2, stack1, stack2, scatters1, scatters2, xAxis, yAxis1, yAxis2].forEach(function(model){
  10925. model.duration(duration);
  10926. });
  10927. }}
  10928. });
  10929. nv.utils.initOptions(chart);
  10930. return chart;
  10931. };
  10932. nv.models.ohlcBar = function() {
  10933. "use strict";
  10934. //============================================================
  10935. // Public Variables with Default Settings
  10936. //------------------------------------------------------------
  10937. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  10938. , width = null
  10939. , height = null
  10940. , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one
  10941. , container = null
  10942. , x = d3.scale.linear()
  10943. , y = d3.scale.linear()
  10944. , getX = function(d) { return d.x }
  10945. , getY = function(d) { return d.y }
  10946. , getOpen = function(d) { return d.open }
  10947. , getClose = function(d) { return d.close }
  10948. , getHigh = function(d) { return d.high }
  10949. , getLow = function(d) { return d.low }
  10950. , forceX = []
  10951. , forceY = []
  10952. , padData = false // If true, adds half a data points width to front and back, for lining up a line chart with a bar chart
  10953. , clipEdge = true
  10954. , color = nv.utils.defaultColor()
  10955. , interactive = false
  10956. , xDomain
  10957. , yDomain
  10958. , xRange
  10959. , yRange
  10960. , dispatch = d3.dispatch('stateChange', 'changeState', 'renderEnd', 'chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout', 'elementMousemove')
  10961. ;
  10962. //============================================================
  10963. // Private Variables
  10964. //------------------------------------------------------------
  10965. function chart(selection) {
  10966. selection.each(function(data) {
  10967. container = d3.select(this);
  10968. var availableWidth = nv.utils.availableWidth(width, container, margin),
  10969. availableHeight = nv.utils.availableHeight(height, container, margin);
  10970. nv.utils.initSVG(container);
  10971. // ohlc bar width.
  10972. var w = (availableWidth / data[0].values.length) * .9;
  10973. // Setup Scales
  10974. x.domain(xDomain || d3.extent(data[0].values.map(getX).concat(forceX) ));
  10975. if (padData)
  10976. x.range(xRange || [availableWidth * .5 / data[0].values.length, availableWidth * (data[0].values.length - .5) / data[0].values.length ]);
  10977. else
  10978. x.range(xRange || [5 + w/2, availableWidth - w/2 - 5]);
  10979. y.domain(yDomain || [
  10980. d3.min(data[0].values.map(getLow).concat(forceY)),
  10981. d3.max(data[0].values.map(getHigh).concat(forceY))
  10982. ]
  10983. ).range(yRange || [availableHeight, 0]);
  10984. // If scale's domain don't have a range, slightly adjust to make one... so a chart can show a single data point
  10985. if (x.domain()[0] === x.domain()[1])
  10986. x.domain()[0] ?
  10987. x.domain([x.domain()[0] - x.domain()[0] * 0.01, x.domain()[1] + x.domain()[1] * 0.01])
  10988. : x.domain([-1,1]);
  10989. if (y.domain()[0] === y.domain()[1])
  10990. y.domain()[0] ?
  10991. y.domain([y.domain()[0] + y.domain()[0] * 0.01, y.domain()[1] - y.domain()[1] * 0.01])
  10992. : y.domain([-1,1]);
  10993. // Setup containers and skeleton of chart
  10994. var wrap = d3.select(this).selectAll('g.nv-wrap.nv-ohlcBar').data([data[0].values]);
  10995. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-ohlcBar');
  10996. var defsEnter = wrapEnter.append('defs');
  10997. var gEnter = wrapEnter.append('g');
  10998. var g = wrap.select('g');
  10999. gEnter.append('g').attr('class', 'nv-ticks');
  11000. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  11001. container
  11002. .on('click', function(d,i) {
  11003. dispatch.chartClick({
  11004. data: d,
  11005. index: i,
  11006. pos: d3.event,
  11007. id: id
  11008. });
  11009. });
  11010. defsEnter.append('clipPath')
  11011. .attr('id', 'nv-chart-clip-path-' + id)
  11012. .append('rect');
  11013. wrap.select('#nv-chart-clip-path-' + id + ' rect')
  11014. .attr('width', availableWidth)
  11015. .attr('height', availableHeight);
  11016. g .attr('clip-path', clipEdge ? 'url(#nv-chart-clip-path-' + id + ')' : '');
  11017. var ticks = wrap.select('.nv-ticks').selectAll('.nv-tick')
  11018. .data(function(d) { return d });
  11019. ticks.exit().remove();
  11020. ticks.enter().append('path')
  11021. .attr('class', function(d,i,j) { return (getOpen(d,i) > getClose(d,i) ? 'nv-tick negative' : 'nv-tick positive') + ' nv-tick-' + j + '-' + i })
  11022. .attr('d', function(d,i) {
  11023. return 'm0,0l0,'
  11024. + (y(getOpen(d,i))
  11025. - y(getHigh(d,i)))
  11026. + 'l'
  11027. + (-w/2)
  11028. + ',0l'
  11029. + (w/2)
  11030. + ',0l0,'
  11031. + (y(getLow(d,i)) - y(getOpen(d,i)))
  11032. + 'l0,'
  11033. + (y(getClose(d,i))
  11034. - y(getLow(d,i)))
  11035. + 'l'
  11036. + (w/2)
  11037. + ',0l'
  11038. + (-w/2)
  11039. + ',0z';
  11040. })
  11041. .attr('transform', function(d,i) { return 'translate(' + x(getX(d,i)) + ',' + y(getHigh(d,i)) + ')'; })
  11042. .attr('fill', function(d,i) { return color[0]; })
  11043. .attr('stroke', function(d,i) { return color[0]; })
  11044. .attr('x', 0 )
  11045. .attr('y', function(d,i) { return y(Math.max(0, getY(d,i))) })
  11046. .attr('height', function(d,i) { return Math.abs(y(getY(d,i)) - y(0)) });
  11047. // the bar colors are controlled by CSS currently
  11048. ticks.attr('class', function(d,i,j) {
  11049. return (getOpen(d,i) > getClose(d,i) ? 'nv-tick negative' : 'nv-tick positive') + ' nv-tick-' + j + '-' + i;
  11050. });
  11051. d3.transition(ticks)
  11052. .attr('transform', function(d,i) { return 'translate(' + x(getX(d,i)) + ',' + y(getHigh(d,i)) + ')'; })
  11053. .attr('d', function(d,i) {
  11054. var w = (availableWidth / data[0].values.length) * .9;
  11055. return 'm0,0l0,'
  11056. + (y(getOpen(d,i))
  11057. - y(getHigh(d,i)))
  11058. + 'l'
  11059. + (-w/2)
  11060. + ',0l'
  11061. + (w/2)
  11062. + ',0l0,'
  11063. + (y(getLow(d,i))
  11064. - y(getOpen(d,i)))
  11065. + 'l0,'
  11066. + (y(getClose(d,i))
  11067. - y(getLow(d,i)))
  11068. + 'l'
  11069. + (w/2)
  11070. + ',0l'
  11071. + (-w/2)
  11072. + ',0z';
  11073. });
  11074. });
  11075. return chart;
  11076. }
  11077. //Create methods to allow outside functions to highlight a specific bar.
  11078. chart.highlightPoint = function(pointIndex, isHoverOver) {
  11079. chart.clearHighlights();
  11080. container.select(".nv-ohlcBar .nv-tick-0-" + pointIndex)
  11081. .classed("hover", isHoverOver)
  11082. ;
  11083. };
  11084. chart.clearHighlights = function() {
  11085. container.select(".nv-ohlcBar .nv-tick.hover")
  11086. .classed("hover", false)
  11087. ;
  11088. };
  11089. //============================================================
  11090. // Expose Public Variables
  11091. //------------------------------------------------------------
  11092. chart.dispatch = dispatch;
  11093. chart.options = nv.utils.optionsFunc.bind(chart);
  11094. chart._options = Object.create({}, {
  11095. // simple options, just get/set the necessary values
  11096. width: {get: function(){return width;}, set: function(_){width=_;}},
  11097. height: {get: function(){return height;}, set: function(_){height=_;}},
  11098. xScale: {get: function(){return x;}, set: function(_){x=_;}},
  11099. yScale: {get: function(){return y;}, set: function(_){y=_;}},
  11100. xDomain: {get: function(){return xDomain;}, set: function(_){xDomain=_;}},
  11101. yDomain: {get: function(){return yDomain;}, set: function(_){yDomain=_;}},
  11102. xRange: {get: function(){return xRange;}, set: function(_){xRange=_;}},
  11103. yRange: {get: function(){return yRange;}, set: function(_){yRange=_;}},
  11104. forceX: {get: function(){return forceX;}, set: function(_){forceX=_;}},
  11105. forceY: {get: function(){return forceY;}, set: function(_){forceY=_;}},
  11106. padData: {get: function(){return padData;}, set: function(_){padData=_;}},
  11107. clipEdge: {get: function(){return clipEdge;}, set: function(_){clipEdge=_;}},
  11108. id: {get: function(){return id;}, set: function(_){id=_;}},
  11109. interactive: {get: function(){return interactive;}, set: function(_){interactive=_;}},
  11110. x: {get: function(){return getX;}, set: function(_){getX=_;}},
  11111. y: {get: function(){return getY;}, set: function(_){getY=_;}},
  11112. open: {get: function(){return getOpen();}, set: function(_){getOpen=_;}},
  11113. close: {get: function(){return getClose();}, set: function(_){getClose=_;}},
  11114. high: {get: function(){return getHigh;}, set: function(_){getHigh=_;}},
  11115. low: {get: function(){return getLow;}, set: function(_){getLow=_;}},
  11116. // options that require extra logic in the setter
  11117. margin: {get: function(){return margin;}, set: function(_){
  11118. margin.top = _.top != undefined ? _.top : margin.top;
  11119. margin.right = _.right != undefined ? _.right : margin.right;
  11120. margin.bottom = _.bottom != undefined ? _.bottom : margin.bottom;
  11121. margin.left = _.left != undefined ? _.left : margin.left;
  11122. }},
  11123. color: {get: function(){return color;}, set: function(_){
  11124. color = nv.utils.getColor(_);
  11125. }}
  11126. });
  11127. nv.utils.initOptions(chart);
  11128. return chart;
  11129. };
  11130. // Code adapted from Jason Davies' "Parallel Coordinates"
  11131. // http://bl.ocks.org/jasondavies/1341281
  11132. nv.models.parallelCoordinates = function() {
  11133. "use strict";
  11134. //============================================================
  11135. // Public Variables with Default Settings
  11136. //------------------------------------------------------------
  11137. var margin = {top: 30, right: 0, bottom: 10, left: 0}
  11138. , width = null
  11139. , height = null
  11140. , availableWidth = null
  11141. , availableHeight = null
  11142. , x = d3.scale.ordinal()
  11143. , y = {}
  11144. , undefinedValuesLabel = "undefined values"
  11145. , dimensionData = []
  11146. , enabledDimensions = []
  11147. , dimensionNames = []
  11148. , displayBrush = true
  11149. , color = nv.utils.defaultColor()
  11150. , filters = []
  11151. , active = []
  11152. , dragging = []
  11153. , axisWithUndefinedValues = []
  11154. , lineTension = 1
  11155. , foreground
  11156. , background
  11157. , dimensions
  11158. , line = d3.svg.line()
  11159. , axis = d3.svg.axis()
  11160. , dispatch = d3.dispatch('brushstart', 'brush', 'brushEnd', 'dimensionsOrder', "stateChange", 'elementClick', 'elementMouseover', 'elementMouseout', 'elementMousemove', 'renderEnd', 'activeChanged')
  11161. ;
  11162. //============================================================
  11163. // Private Variables
  11164. //------------------------------------------------------------
  11165. var renderWatch = nv.utils.renderWatch(dispatch);
  11166. function chart(selection) {
  11167. renderWatch.reset();
  11168. selection.each(function(data) {
  11169. var container = d3.select(this);
  11170. availableWidth = nv.utils.availableWidth(width, container, margin);
  11171. availableHeight = nv.utils.availableHeight(height, container, margin);
  11172. nv.utils.initSVG(container);
  11173. //Convert old data to new format (name, values)
  11174. if (data[0].values === undefined) {
  11175. var newData = [];
  11176. data.forEach(function (d) {
  11177. var val = {};
  11178. var key = Object.keys(d);
  11179. key.forEach(function (k) { if (k !== "name") val[k] = d[k] });
  11180. newData.push({ key: d.name, values: val });
  11181. });
  11182. data = newData;
  11183. }
  11184. var dataValues = data.map(function (d) {return d.values});
  11185. if (active.length === 0) {
  11186. active = data;
  11187. }; //set all active before first brush call
  11188. dimensionNames = dimensionData.sort(function (a, b) { return a.currentPosition - b.currentPosition; }).map(function (d) { return d.key });
  11189. enabledDimensions = dimensionData.filter(function (d) { return !d.disabled; });
  11190. // Setup Scales
  11191. x.rangePoints([0, availableWidth], 1).domain(enabledDimensions.map(function (d) { return d.key; }));
  11192. //Set as true if all values on an axis are missing.
  11193. // Extract the list of dimensions and create a scale for each.
  11194. var oldDomainMaxValue = {};
  11195. var displayMissingValuesline = false;
  11196. var currentTicks = [];
  11197. dimensionNames.forEach(function(d) {
  11198. var extent = d3.extent(dataValues, function (p) { return +p[d]; });
  11199. var min = extent[0];
  11200. var max = extent[1];
  11201. var onlyUndefinedValues = false;
  11202. //If there is no values to display on an axis, set the extent to 0
  11203. if (isNaN(min) || isNaN(max)) {
  11204. onlyUndefinedValues = true;
  11205. min = 0;
  11206. max = 0;
  11207. }
  11208. //Scale axis if there is only one value
  11209. if (min === max) {
  11210. min = min - 1;
  11211. max = max + 1;
  11212. }
  11213. var f = filters.filter(function (k) { return k.dimension == d; });
  11214. if (f.length !== 0) {
  11215. //If there is only NaN values, keep the existing domain.
  11216. if (onlyUndefinedValues) {
  11217. min = y[d].domain()[0];
  11218. max = y[d].domain()[1];
  11219. }
  11220. //If the brush extent is > max (< min), keep the extent value.
  11221. else if (!f[0].hasOnlyNaN && displayBrush) {
  11222. min = min > f[0].extent[0] ? f[0].extent[0] : min;
  11223. max = max < f[0].extent[1] ? f[0].extent[1] : max;
  11224. }
  11225. //If there is NaN values brushed be sure the brush extent is on the domain.
  11226. else if (f[0].hasNaN) {
  11227. max = max < f[0].extent[1] ? f[0].extent[1] : max;
  11228. oldDomainMaxValue[d] = y[d].domain()[1];
  11229. displayMissingValuesline = true;
  11230. }
  11231. }
  11232. //Use 90% of (availableHeight - 12) for the axis range, 12 reprensenting the space necessary to display "undefined values" text.
  11233. //The remaining 10% are used to display the missingValue line.
  11234. y[d] = d3.scale.linear()
  11235. .domain([min, max])
  11236. .range([(availableHeight - 12) * 0.9, 0]);
  11237. axisWithUndefinedValues = [];
  11238. y[d].brush = d3.svg.brush().y(y[d]).on('brushstart', brushstart).on('brush', brush).on('brushend', brushend);
  11239. });
  11240. // Setup containers and skeleton of chart
  11241. var wrap = container.selectAll('g.nv-wrap.nv-parallelCoordinates').data([data]);
  11242. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-parallelCoordinates');
  11243. var gEnter = wrapEnter.append('g');
  11244. var g = wrap.select('g');
  11245. gEnter.append('g').attr('class', 'nv-parallelCoordinates background');
  11246. gEnter.append('g').attr('class', 'nv-parallelCoordinates foreground');
  11247. gEnter.append('g').attr('class', 'nv-parallelCoordinates missingValuesline');
  11248. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  11249. line.interpolate('cardinal').tension(lineTension);
  11250. axis.orient('left');
  11251. var axisDrag = d3.behavior.drag()
  11252. .on('dragstart', dragStart)
  11253. .on('drag', dragMove)
  11254. .on('dragend', dragEnd);
  11255. //Add missing value line at the bottom of the chart
  11256. var missingValuesline, missingValueslineText;
  11257. var step = x.range()[1] - x.range()[0];
  11258. step = isNaN(step) ? x.range()[0] : step;
  11259. if (!isNaN(step)) {
  11260. var lineData = [0 + step / 2, availableHeight - 12, availableWidth - step / 2, availableHeight - 12];
  11261. missingValuesline = wrap.select('.missingValuesline').selectAll('line').data([lineData]);
  11262. missingValuesline.enter().append('line');
  11263. missingValuesline.exit().remove();
  11264. missingValuesline.attr("x1", function(d) { return d[0]; })
  11265. .attr("y1", function(d) { return d[1]; })
  11266. .attr("x2", function(d) { return d[2]; })
  11267. .attr("y2", function(d) { return d[3]; });
  11268. //Add the text "undefined values" under the missing value line
  11269. missingValueslineText = wrap.select('.missingValuesline').selectAll('text').data([undefinedValuesLabel]);
  11270. missingValueslineText.append('text').data([undefinedValuesLabel]);
  11271. missingValueslineText.enter().append('text');
  11272. missingValueslineText.exit().remove();
  11273. missingValueslineText.attr("y", availableHeight)
  11274. //To have the text right align with the missingValues line, substract 92 representing the text size.
  11275. .attr("x", availableWidth - 92 - step / 2)
  11276. .text(function(d) { return d; });
  11277. }
  11278. // Add grey background lines for context.
  11279. background = wrap.select('.background').selectAll('path').data(data);
  11280. background.enter().append('path');
  11281. background.exit().remove();
  11282. background.attr('d', path);
  11283. // Add blue foreground lines for focus.
  11284. foreground = wrap.select('.foreground').selectAll('path').data(data);
  11285. foreground.enter().append('path')
  11286. foreground.exit().remove();
  11287. foreground.attr('d', path)
  11288. .style("stroke-width", function (d, i) {
  11289. if (isNaN(d.strokeWidth)) { d.strokeWidth = 1;} return d.strokeWidth;})
  11290. .attr('stroke', function (d, i) { return d.color || color(d, i); });
  11291. foreground.on("mouseover", function (d, i) {
  11292. d3.select(this).classed('hover', true).style("stroke-width", d.strokeWidth + 2 + "px").style("stroke-opacity", 1);
  11293. dispatch.elementMouseover({
  11294. label: d.name,
  11295. color: d.color || color(d, i),
  11296. values: d.values,
  11297. dimensions: enabledDimensions
  11298. });
  11299. });
  11300. foreground.on("mouseout", function (d, i) {
  11301. d3.select(this).classed('hover', false).style("stroke-width", d.strokeWidth + "px").style("stroke-opacity", 0.7);
  11302. dispatch.elementMouseout({
  11303. label: d.name,
  11304. index: i
  11305. });
  11306. });
  11307. foreground.on('mousemove', function (d, i) {
  11308. dispatch.elementMousemove();
  11309. });
  11310. foreground.on('click', function (d) {
  11311. dispatch.elementClick({
  11312. id: d.id
  11313. });
  11314. });
  11315. // Add a group element for each dimension.
  11316. dimensions = g.selectAll('.dimension').data(enabledDimensions);
  11317. var dimensionsEnter = dimensions.enter().append('g').attr('class', 'nv-parallelCoordinates dimension');
  11318. dimensions.attr('transform', function(d) { return 'translate(' + x(d.key) + ',0)'; });
  11319. dimensionsEnter.append('g').attr('class', 'nv-axis');
  11320. // Add an axis and title.
  11321. dimensionsEnter.append('text')
  11322. .attr('class', 'nv-label')
  11323. .style("cursor", "move")
  11324. .attr('dy', '-1em')
  11325. .attr('text-anchor', 'middle')
  11326. .on("mouseover", function(d, i) {
  11327. dispatch.elementMouseover({
  11328. label: d.tooltip || d.key,
  11329. color: d.color
  11330. });
  11331. })
  11332. .on("mouseout", function(d, i) {
  11333. dispatch.elementMouseout({
  11334. label: d.tooltip
  11335. });
  11336. })
  11337. .on('mousemove', function (d, i) {
  11338. dispatch.elementMousemove();
  11339. })
  11340. .call(axisDrag);
  11341. dimensionsEnter.append('g').attr('class', 'nv-brushBackground');
  11342. dimensions.exit().remove();
  11343. dimensions.select('.nv-label').text(function (d) { return d.key });
  11344. // Add and store a brush for each axis.
  11345. restoreBrush(displayBrush);
  11346. var actives = dimensionNames.filter(function (p) { return !y[p].brush.empty(); }),
  11347. extents = actives.map(function (p) { return y[p].brush.extent(); });
  11348. var formerActive = active.slice(0);
  11349. //Restore active values
  11350. active = [];
  11351. foreground.style("display", function (d) {
  11352. var isActive = actives.every(function (p, i) {
  11353. if ((isNaN(d.values[p]) || isNaN(parseFloat(d.values[p]))) && extents[i][0] == y[p].brush.y().domain()[0]) {
  11354. return true;
  11355. }
  11356. return (extents[i][0] <= d.values[p] && d.values[p] <= extents[i][1]) && !isNaN(parseFloat(d.values[p]));
  11357. });
  11358. if (isActive)
  11359. active.push(d);
  11360. return !isActive ? "none" : null;
  11361. });
  11362. if (filters.length > 0 || !nv.utils.arrayEquals(active, formerActive)) {
  11363. dispatch.activeChanged(active);
  11364. }
  11365. // Returns the path for a given data point.
  11366. function path(d) {
  11367. return line(enabledDimensions.map(function (p) {
  11368. //If value if missing, put the value on the missing value line
  11369. if (isNaN(d.values[p.key]) || isNaN(parseFloat(d.values[p.key])) || displayMissingValuesline) {
  11370. var domain = y[p.key].domain();
  11371. var range = y[p.key].range();
  11372. var min = domain[0] - (domain[1] - domain[0]) / 9;
  11373. //If it's not already the case, allow brush to select undefined values
  11374. if (axisWithUndefinedValues.indexOf(p.key) < 0) {
  11375. var newscale = d3.scale.linear().domain([min, domain[1]]).range([availableHeight - 12, range[1]]);
  11376. y[p.key].brush.y(newscale);
  11377. axisWithUndefinedValues.push(p.key);
  11378. }
  11379. if (isNaN(d.values[p.key]) || isNaN(parseFloat(d.values[p.key]))) {
  11380. return [x(p.key), y[p.key](min)];
  11381. }
  11382. }
  11383. //If parallelCoordinate contain missing values show the missing values line otherwise, hide it.
  11384. if (missingValuesline !== undefined) {
  11385. if (axisWithUndefinedValues.length > 0 || displayMissingValuesline) {
  11386. missingValuesline.style("display", "inline");
  11387. missingValueslineText.style("display", "inline");
  11388. } else {
  11389. missingValuesline.style("display", "none");
  11390. missingValueslineText.style("display", "none");
  11391. }
  11392. }
  11393. return [x(p.key), y[p.key](d.values[p.key])];
  11394. }));
  11395. }
  11396. function restoreBrush(visible) {
  11397. filters.forEach(function (f) {
  11398. //If filter brushed NaN values, keep the brush on the bottom of the axis.
  11399. var brushDomain = y[f.dimension].brush.y().domain();
  11400. if (f.hasOnlyNaN) {
  11401. f.extent[1] = (y[f.dimension].domain()[1] - brushDomain[0]) * (f.extent[1] - f.extent[0]) / (oldDomainMaxValue[f.dimension] - f.extent[0]) + brushDomain[0];
  11402. }
  11403. if (f.hasNaN) {
  11404. f.extent[0] = brushDomain[0];
  11405. }
  11406. if (visible)
  11407. y[f.dimension].brush.extent(f.extent);
  11408. });
  11409. dimensions.select('.nv-brushBackground')
  11410. .each(function (d) {
  11411. d3.select(this).call(y[d.key].brush);
  11412. })
  11413. .selectAll('rect')
  11414. .attr('x', -8)
  11415. .attr('width', 16);
  11416. updateTicks();
  11417. }
  11418. // Handles a brush event, toggling the display of foreground lines.
  11419. function brushstart() {
  11420. //If brush aren't visible, show it before brushing again.
  11421. if (displayBrush === false) {
  11422. displayBrush = true;
  11423. restoreBrush(true);
  11424. }
  11425. }
  11426. // Handles a brush event, toggling the display of foreground lines.
  11427. function brush() {
  11428. actives = dimensionNames.filter(function (p) { return !y[p].brush.empty(); });
  11429. extents = actives.map(function(p) { return y[p].brush.extent(); });
  11430. filters = []; //erase current filters
  11431. actives.forEach(function(d,i) {
  11432. filters[i] = {
  11433. dimension: d,
  11434. extent: extents[i],
  11435. hasNaN: false,
  11436. hasOnlyNaN: false
  11437. }
  11438. });
  11439. active = []; //erase current active list
  11440. foreground.style('display', function(d) {
  11441. var isActive = actives.every(function(p, i) {
  11442. if ((isNaN(d.values[p]) || isNaN(parseFloat(d.values[p]))) && extents[i][0] == y[p].brush.y().domain()[0]) return true;
  11443. return (extents[i][0] <= d.values[p] && d.values[p] <= extents[i][1]) && !isNaN(parseFloat(d.values[p]));
  11444. });
  11445. if (isActive) active.push(d);
  11446. return isActive ? null : 'none';
  11447. });
  11448. updateTicks();
  11449. dispatch.brush({
  11450. filters: filters,
  11451. active: active
  11452. });
  11453. }
  11454. function brushend() {
  11455. var hasActiveBrush = actives.length > 0 ? true : false;
  11456. filters.forEach(function (f) {
  11457. if (f.extent[0] === y[f.dimension].brush.y().domain()[0] && axisWithUndefinedValues.indexOf(f.dimension) >= 0)
  11458. f.hasNaN = true;
  11459. if (f.extent[1] < y[f.dimension].domain()[0])
  11460. f.hasOnlyNaN = true;
  11461. });
  11462. dispatch.brushEnd(active, hasActiveBrush);
  11463. }
  11464. function updateTicks() {
  11465. dimensions.select('.nv-axis')
  11466. .each(function (d, i) {
  11467. var f = filters.filter(function (k) { return k.dimension == d.key; });
  11468. currentTicks[d.key] = y[d.key].domain();
  11469. //If brush are available, display brush extent
  11470. if (f.length != 0 && displayBrush)
  11471. {
  11472. currentTicks[d.key] = [];
  11473. if (f[0].extent[1] > y[d.key].domain()[0])
  11474. currentTicks[d.key] = [f[0].extent[1]];
  11475. if (f[0].extent[0] >= y[d.key].domain()[0])
  11476. currentTicks[d.key].push(f[0].extent[0]);
  11477. }
  11478. d3.select(this).call(axis.scale(y[d.key]).tickFormat(d.format).tickValues(currentTicks[d.key]));
  11479. });
  11480. }
  11481. function dragStart(d) {
  11482. dragging[d.key] = this.parentNode.__origin__ = x(d.key);
  11483. background.attr("visibility", "hidden");
  11484. }
  11485. function dragMove(d) {
  11486. dragging[d.key] = Math.min(availableWidth, Math.max(0, this.parentNode.__origin__ += d3.event.x));
  11487. foreground.attr("d", path);
  11488. enabledDimensions.sort(function (a, b) { return dimensionPosition(a.key) - dimensionPosition(b.key); });
  11489. enabledDimensions.forEach(function (d, i) { return d.currentPosition = i; });
  11490. x.domain(enabledDimensions.map(function (d) { return d.key; }));
  11491. dimensions.attr("transform", function(d) { return "translate(" + dimensionPosition(d.key) + ")"; });
  11492. }
  11493. function dragEnd(d, i) {
  11494. delete this.parentNode.__origin__;
  11495. delete dragging[d.key];
  11496. d3.select(this.parentNode).attr("transform", "translate(" + x(d.key) + ")");
  11497. foreground
  11498. .attr("d", path);
  11499. background
  11500. .attr("d", path)
  11501. .attr("visibility", null);
  11502. dispatch.dimensionsOrder(enabledDimensions);
  11503. }
  11504. function dimensionPosition(d) {
  11505. var v = dragging[d];
  11506. return v == null ? x(d) : v;
  11507. }
  11508. });
  11509. return chart;
  11510. }
  11511. //============================================================
  11512. // Expose Public Variables
  11513. //------------------------------------------------------------
  11514. chart.dispatch = dispatch;
  11515. chart.options = nv.utils.optionsFunc.bind(chart);
  11516. chart._options = Object.create({}, {
  11517. // simple options, just get/set the necessary values
  11518. width: {get: function(){return width;}, set: function(_){width= _;}},
  11519. height: {get: function(){return height;}, set: function(_){height= _;}},
  11520. dimensionData: { get: function () { return dimensionData; }, set: function (_) { dimensionData = _; } },
  11521. displayBrush: { get: function () { return displayBrush; }, set: function (_) { displayBrush = _; } },
  11522. filters: { get: function () { return filters; }, set: function (_) { filters = _; } },
  11523. active: { get: function () { return active; }, set: function (_) { active = _; } },
  11524. lineTension: {get: function(){return lineTension;}, set: function(_){lineTension = _;}},
  11525. undefinedValuesLabel : {get: function(){return undefinedValuesLabel;}, set: function(_){undefinedValuesLabel=_;}},
  11526. // deprecated options
  11527. dimensions: {get: function () { return dimensionData.map(function (d){return d.key}); }, set: function (_) {
  11528. // deprecated after 1.8.1
  11529. nv.deprecated('dimensions', 'use dimensionData instead');
  11530. if (dimensionData.length === 0) {
  11531. _.forEach(function (k) { dimensionData.push({ key: k }) })
  11532. } else {
  11533. _.forEach(function (k, i) { dimensionData[i].key= k })
  11534. }
  11535. }},
  11536. dimensionNames: {get: function () { return dimensionData.map(function (d){return d.key}); }, set: function (_) {
  11537. // deprecated after 1.8.1
  11538. nv.deprecated('dimensionNames', 'use dimensionData instead');
  11539. dimensionNames = [];
  11540. if (dimensionData.length === 0) {
  11541. _.forEach(function (k) { dimensionData.push({ key: k }) })
  11542. } else {
  11543. _.forEach(function (k, i) { dimensionData[i].key = k })
  11544. }
  11545. }},
  11546. dimensionFormats: {get: function () { return dimensionData.map(function (d) { return d.format }); }, set: function (_) {
  11547. // deprecated after 1.8.1
  11548. nv.deprecated('dimensionFormats', 'use dimensionData instead');
  11549. if (dimensionData.length === 0) {
  11550. _.forEach(function (f) { dimensionData.push({ format: f }) })
  11551. } else {
  11552. _.forEach(function (f, i) { dimensionData[i].format = f })
  11553. }
  11554. }},
  11555. // options that require extra logic in the setter
  11556. margin: {get: function(){return margin;}, set: function(_){
  11557. margin.top = _.top !== undefined ? _.top : margin.top;
  11558. margin.right = _.right !== undefined ? _.right : margin.right;
  11559. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  11560. margin.left = _.left !== undefined ? _.left : margin.left;
  11561. }},
  11562. color: {get: function(){return color;}, set: function(_){
  11563. color = nv.utils.getColor(_);
  11564. }}
  11565. });
  11566. nv.utils.initOptions(chart);
  11567. return chart;
  11568. };
  11569. nv.models.parallelCoordinatesChart = function () {
  11570. "use strict";
  11571. //============================================================
  11572. // Public Variables with Default Settings
  11573. //------------------------------------------------------------
  11574. var parallelCoordinates = nv.models.parallelCoordinates()
  11575. var legend = nv.models.legend()
  11576. var tooltip = nv.models.tooltip();
  11577. var dimensionTooltip = nv.models.tooltip();
  11578. var margin = { top: 0, right: 0, bottom: 0, left: 0 }
  11579. , marginTop = null
  11580. , width = null
  11581. , height = null
  11582. , showLegend = true
  11583. , color = nv.utils.defaultColor()
  11584. , state = nv.utils.state()
  11585. , dimensionData = []
  11586. , displayBrush = true
  11587. , defaultState = null
  11588. , noData = null
  11589. , nanValue = "undefined"
  11590. , dispatch = d3.dispatch('dimensionsOrder', 'brushEnd', 'stateChange', 'changeState', 'renderEnd')
  11591. , controlWidth = function () { return showControls ? 180 : 0 }
  11592. ;
  11593. //============================================================
  11594. //============================================================
  11595. // Private Variables
  11596. //------------------------------------------------------------
  11597. var renderWatch = nv.utils.renderWatch(dispatch);
  11598. var stateGetter = function(data) {
  11599. return function() {
  11600. return {
  11601. active: data.map(function(d) { return !d.disabled })
  11602. };
  11603. }
  11604. };
  11605. var stateSetter = function(data) {
  11606. return function(state) {
  11607. if(state.active !== undefined) {
  11608. data.forEach(function(series, i) {
  11609. series.disabled = !state.active[i];
  11610. });
  11611. }
  11612. }
  11613. };
  11614. tooltip.contentGenerator(function(data) {
  11615. var str = '<table><thead><tr><td class="legend-color-guide"><div style="background-color:' + data.color + '"></div></td><td><strong>' + data.key + '</strong></td></tr></thead>';
  11616. if(data.series.length !== 0)
  11617. {
  11618. str = str + '<tbody><tr><td height ="10px"></td></tr>';
  11619. data.series.forEach(function(d){
  11620. str = str + '<tr><td class="legend-color-guide"><div style="background-color:' + d.color + '"></div></td><td class="key">' + d.key + '</td><td class="value">' + d.value + '</td></tr>';
  11621. });
  11622. str = str + '</tbody>';
  11623. }
  11624. str = str + '</table>';
  11625. return str;
  11626. });
  11627. //============================================================
  11628. // Chart function
  11629. //------------------------------------------------------------
  11630. function chart(selection) {
  11631. renderWatch.reset();
  11632. renderWatch.models(parallelCoordinates);
  11633. selection.each(function(data) {
  11634. var container = d3.select(this);
  11635. nv.utils.initSVG(container);
  11636. var that = this;
  11637. var availableWidth = nv.utils.availableWidth(width, container, margin),
  11638. availableHeight = nv.utils.availableHeight(height, container, margin);
  11639. chart.update = function() { container.call(chart); };
  11640. chart.container = this;
  11641. state.setter(stateSetter(dimensionData), chart.update)
  11642. .getter(stateGetter(dimensionData))
  11643. .update();
  11644. //set state.disabled
  11645. state.disabled = dimensionData.map(function (d) { return !!d.disabled });
  11646. //Keep dimensions position in memory
  11647. dimensionData = dimensionData.map(function (d) {d.disabled = !!d.disabled; return d});
  11648. dimensionData.forEach(function (d, i) {
  11649. d.originalPosition = isNaN(d.originalPosition) ? i : d.originalPosition;
  11650. d.currentPosition = isNaN(d.currentPosition) ? i : d.currentPosition;
  11651. });
  11652. if (!defaultState) {
  11653. var key;
  11654. defaultState = {};
  11655. for(key in state) {
  11656. if(state[key] instanceof Array)
  11657. defaultState[key] = state[key].slice(0);
  11658. else
  11659. defaultState[key] = state[key];
  11660. }
  11661. }
  11662. // Display No Data message if there's nothing to show.
  11663. if(!data || !data.length) {
  11664. nv.utils.noData(chart, container);
  11665. return chart;
  11666. } else {
  11667. container.selectAll('.nv-noData').remove();
  11668. }
  11669. //------------------------------------------------------------
  11670. // Setup containers and skeleton of chart
  11671. var wrap = container.selectAll('g.nv-wrap.nv-parallelCoordinatesChart').data([data]);
  11672. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-parallelCoordinatesChart').append('g');
  11673. var g = wrap.select('g');
  11674. gEnter.append('g').attr('class', 'nv-parallelCoordinatesWrap');
  11675. gEnter.append('g').attr('class', 'nv-legendWrap');
  11676. g.select("rect")
  11677. .attr("width", availableWidth)
  11678. .attr("height", (availableHeight > 0) ? availableHeight : 0);
  11679. // Legend
  11680. if (!showLegend) {
  11681. g.select('.nv-legendWrap').selectAll('*').remove();
  11682. } else {
  11683. legend.width(availableWidth)
  11684. .color(function (d) { return "rgb(188,190,192)"; });
  11685. g.select('.nv-legendWrap')
  11686. .datum(dimensionData.sort(function (a, b) { return a.originalPosition - b.originalPosition; }))
  11687. .call(legend);
  11688. if (!marginTop && legend.height() !== margin.top) {
  11689. margin.top = legend.height();
  11690. availableHeight = nv.utils.availableHeight(height, container, margin);
  11691. }
  11692. wrap.select('.nv-legendWrap')
  11693. .attr('transform', 'translate( 0 ,' + (-margin.top) + ')');
  11694. }
  11695. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  11696. // Main Chart Component(s)
  11697. parallelCoordinates
  11698. .width(availableWidth)
  11699. .height(availableHeight)
  11700. .dimensionData(dimensionData)
  11701. .displayBrush(displayBrush);
  11702. var parallelCoordinatesWrap = g.select('.nv-parallelCoordinatesWrap ')
  11703. .datum(data);
  11704. parallelCoordinatesWrap.transition().call(parallelCoordinates);
  11705. //============================================================
  11706. // Event Handling/Dispatching (in chart's scope)
  11707. //------------------------------------------------------------
  11708. //Display reset brush button
  11709. parallelCoordinates.dispatch.on('brushEnd', function (active, hasActiveBrush) {
  11710. if (hasActiveBrush) {
  11711. displayBrush = true;
  11712. dispatch.brushEnd(active);
  11713. } else {
  11714. displayBrush = false;
  11715. }
  11716. });
  11717. legend.dispatch.on('stateChange', function(newState) {
  11718. for(var key in newState) {
  11719. state[key] = newState[key];
  11720. }
  11721. dispatch.stateChange(state);
  11722. chart.update();
  11723. });
  11724. //Update dimensions order and display reset sorting button
  11725. parallelCoordinates.dispatch.on('dimensionsOrder', function (e) {
  11726. dimensionData.sort(function (a, b) { return a.currentPosition - b.currentPosition; });
  11727. var isSorted = false;
  11728. dimensionData.forEach(function (d, i) {
  11729. d.currentPosition = i;
  11730. if (d.currentPosition !== d.originalPosition)
  11731. isSorted = true;
  11732. });
  11733. dispatch.dimensionsOrder(dimensionData, isSorted);
  11734. });
  11735. // Update chart from a state object passed to event handler
  11736. dispatch.on('changeState', function (e) {
  11737. if (typeof e.disabled !== 'undefined') {
  11738. dimensionData.forEach(function (series, i) {
  11739. series.disabled = e.disabled[i];
  11740. });
  11741. state.disabled = e.disabled;
  11742. }
  11743. chart.update();
  11744. });
  11745. });
  11746. renderWatch.renderEnd('parraleleCoordinateChart immediate');
  11747. return chart;
  11748. }
  11749. //============================================================
  11750. // Event Handling/Dispatching (out of chart's scope)
  11751. //------------------------------------------------------------
  11752. parallelCoordinates.dispatch.on('elementMouseover.tooltip', function (evt) {
  11753. var tp = {
  11754. key: evt.label,
  11755. color: evt.color,
  11756. series: []
  11757. }
  11758. if(evt.values){
  11759. Object.keys(evt.values).forEach(function (d) {
  11760. var dim = evt.dimensions.filter(function (dd) {return dd.key === d;})[0];
  11761. if(dim){
  11762. var v;
  11763. if (isNaN(evt.values[d]) || isNaN(parseFloat(evt.values[d]))) {
  11764. v = nanValue;
  11765. } else {
  11766. v = dim.format(evt.values[d]);
  11767. }
  11768. tp.series.push({ idx: dim.currentPosition, key: d, value: v, color: dim.color });
  11769. }
  11770. });
  11771. tp.series.sort(function(a,b) {return a.idx - b.idx});
  11772. }
  11773. tooltip.data(tp).hidden(false);
  11774. });
  11775. parallelCoordinates.dispatch.on('elementMouseout.tooltip', function(evt) {
  11776. tooltip.hidden(true)
  11777. });
  11778. parallelCoordinates.dispatch.on('elementMousemove.tooltip', function () {
  11779. tooltip();
  11780. });
  11781. //============================================================
  11782. // Expose Public Variables
  11783. //------------------------------------------------------------
  11784. // expose chart's sub-components
  11785. chart.dispatch = dispatch;
  11786. chart.parallelCoordinates = parallelCoordinates;
  11787. chart.legend = legend;
  11788. chart.tooltip = tooltip;
  11789. chart.options = nv.utils.optionsFunc.bind(chart);
  11790. chart._options = Object.create({}, {
  11791. // simple options, just get/set the necessary values
  11792. width: { get: function () { return width; }, set: function (_) { width = _; } },
  11793. height: { get: function () { return height; }, set: function (_) { height = _; } },
  11794. showLegend: { get: function () { return showLegend; }, set: function (_) { showLegend = _; } },
  11795. defaultState: { get: function () { return defaultState; }, set: function (_) { defaultState = _; } },
  11796. dimensionData: { get: function () { return dimensionData; }, set: function (_) { dimensionData = _; } },
  11797. displayBrush: { get: function () { return displayBrush; }, set: function (_) { displayBrush = _; } },
  11798. noData: { get: function () { return noData; }, set: function (_) { noData = _; } },
  11799. nanValue: { get: function () { return nanValue; }, set: function (_) { nanValue = _; } },
  11800. // options that require extra logic in the setter
  11801. margin: {
  11802. get: function () { return margin; },
  11803. set: function (_) {
  11804. if (_.top !== undefined) {
  11805. margin.top = _.top;
  11806. marginTop = _.top;
  11807. }
  11808. margin.right = _.right !== undefined ? _.right : margin.right;
  11809. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  11810. margin.left = _.left !== undefined ? _.left : margin.left;
  11811. }
  11812. },
  11813. color: {get: function(){return color;}, set: function(_){
  11814. color = nv.utils.getColor(_);
  11815. legend.color(color);
  11816. parallelCoordinates.color(color);
  11817. }}
  11818. });
  11819. nv.utils.inheritOptions(chart, parallelCoordinates);
  11820. nv.utils.initOptions(chart);
  11821. return chart;
  11822. };
  11823. nv.models.pie = function() {
  11824. "use strict";
  11825. //============================================================
  11826. // Public Variables with Default Settings
  11827. //------------------------------------------------------------
  11828. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  11829. , width = 500
  11830. , height = 500
  11831. , getX = function(d) { return d.x }
  11832. , getY = function(d) { return d.y }
  11833. , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one
  11834. , container = null
  11835. , color = nv.utils.defaultColor()
  11836. , valueFormat = d3.format(',.2f')
  11837. , showLabels = true
  11838. , labelsOutside = false
  11839. , labelType = "key"
  11840. , labelThreshold = .02 //if slice percentage is under this, don't show label
  11841. , hideOverlapLabels = false //Hide labels that don't fit in slice
  11842. , donut = false
  11843. , title = false
  11844. , growOnHover = true
  11845. , titleOffset = 0
  11846. , labelSunbeamLayout = false
  11847. , startAngle = false
  11848. , padAngle = false
  11849. , endAngle = false
  11850. , cornerRadius = 0
  11851. , donutRatio = 0.5
  11852. , duration = 250
  11853. , arcsRadius = []
  11854. , dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout', 'elementMousemove', 'renderEnd')
  11855. ;
  11856. var arcs = [];
  11857. var arcsOver = [];
  11858. //============================================================
  11859. // chart function
  11860. //------------------------------------------------------------
  11861. var renderWatch = nv.utils.renderWatch(dispatch);
  11862. function chart(selection) {
  11863. renderWatch.reset();
  11864. selection.each(function(data) {
  11865. var availableWidth = width - margin.left - margin.right
  11866. , availableHeight = height - margin.top - margin.bottom
  11867. , radius = Math.min(availableWidth, availableHeight) / 2
  11868. , arcsRadiusOuter = []
  11869. , arcsRadiusInner = []
  11870. ;
  11871. container = d3.select(this)
  11872. if (arcsRadius.length === 0) {
  11873. var outer = radius - radius / 10;
  11874. var inner = donutRatio * radius;
  11875. for (var i = 0; i < data[0].length; i++) {
  11876. arcsRadiusOuter.push(outer);
  11877. arcsRadiusInner.push(inner);
  11878. }
  11879. } else {
  11880. if(growOnHover){
  11881. arcsRadiusOuter = arcsRadius.map(function (d) { return (d.outer - d.outer / 10) * radius; });
  11882. arcsRadiusInner = arcsRadius.map(function (d) { return (d.inner - d.inner / 10) * radius; });
  11883. donutRatio = d3.min(arcsRadius.map(function (d) { return (d.inner - d.inner / 10); }));
  11884. } else {
  11885. arcsRadiusOuter = arcsRadius.map(function (d) { return d.outer * radius; });
  11886. arcsRadiusInner = arcsRadius.map(function (d) { return d.inner * radius; });
  11887. donutRatio = d3.min(arcsRadius.map(function (d) { return d.inner; }));
  11888. }
  11889. }
  11890. nv.utils.initSVG(container);
  11891. // Setup containers and skeleton of chart
  11892. var wrap = container.selectAll('.nv-wrap.nv-pie').data(data);
  11893. var wrapEnter = wrap.enter().append('g').attr('class','nvd3 nv-wrap nv-pie nv-chart-' + id);
  11894. var gEnter = wrapEnter.append('g');
  11895. var g = wrap.select('g');
  11896. var g_pie = gEnter.append('g').attr('class', 'nv-pie');
  11897. gEnter.append('g').attr('class', 'nv-pieLabels');
  11898. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  11899. g.select('.nv-pie').attr('transform', 'translate(' + availableWidth / 2 + ',' + availableHeight / 2 + ')');
  11900. g.select('.nv-pieLabels').attr('transform', 'translate(' + availableWidth / 2 + ',' + availableHeight / 2 + ')');
  11901. //
  11902. container.on('click', function(d,i) {
  11903. dispatch.chartClick({
  11904. data: d,
  11905. index: i,
  11906. pos: d3.event,
  11907. id: id
  11908. });
  11909. });
  11910. arcs = [];
  11911. arcsOver = [];
  11912. for (var i = 0; i < data[0].length; i++) {
  11913. var arc = d3.svg.arc().outerRadius(arcsRadiusOuter[i]);
  11914. var arcOver = d3.svg.arc().outerRadius(arcsRadiusOuter[i] + 5);
  11915. if (startAngle !== false) {
  11916. arc.startAngle(startAngle);
  11917. arcOver.startAngle(startAngle);
  11918. }
  11919. if (endAngle !== false) {
  11920. arc.endAngle(endAngle);
  11921. arcOver.endAngle(endAngle);
  11922. }
  11923. if (donut) {
  11924. arc.innerRadius(arcsRadiusInner[i]);
  11925. arcOver.innerRadius(arcsRadiusInner[i]);
  11926. }
  11927. if (arc.cornerRadius && cornerRadius) {
  11928. arc.cornerRadius(cornerRadius);
  11929. arcOver.cornerRadius(cornerRadius);
  11930. }
  11931. arcs.push(arc);
  11932. arcsOver.push(arcOver);
  11933. }
  11934. // Setup the Pie chart and choose the data element
  11935. var pie = d3.layout.pie()
  11936. .sort(null)
  11937. .value(function(d) { return d.disabled ? 0 : getY(d) });
  11938. // padAngle added in d3 3.5
  11939. if (pie.padAngle && padAngle) {
  11940. pie.padAngle(padAngle);
  11941. }
  11942. // if title is specified and donut, put it in the middle
  11943. if (donut && title) {
  11944. g_pie.append("text").attr('class', 'nv-pie-title');
  11945. wrap.select('.nv-pie-title')
  11946. .style("text-anchor", "middle")
  11947. .text(function (d) {
  11948. return title;
  11949. })
  11950. .style("font-size", (Math.min(availableWidth, availableHeight)) * donutRatio * 2 / (title.length + 2) + "px")
  11951. .attr("dy", "0.35em") // trick to vertically center text
  11952. .attr('transform', function(d, i) {
  11953. return 'translate(0, '+ titleOffset + ')';
  11954. });
  11955. }
  11956. var slices = wrap.select('.nv-pie').selectAll('.nv-slice').data(pie);
  11957. var pieLabels = wrap.select('.nv-pieLabels').selectAll('.nv-label').data(pie);
  11958. slices.exit().remove();
  11959. pieLabels.exit().remove();
  11960. var ae = slices.enter().append('g');
  11961. ae.attr('class', 'nv-slice');
  11962. ae.on('mouseover', function(d, i) {
  11963. d3.select(this).classed('hover', true);
  11964. if (growOnHover) {
  11965. d3.select(this).select("path").transition()
  11966. .duration(70)
  11967. .attr("d", arcsOver[i]);
  11968. }
  11969. dispatch.elementMouseover({
  11970. data: d.data,
  11971. index: i,
  11972. color: d3.select(this).style("fill"),
  11973. percent: (d.endAngle - d.startAngle) / (2 * Math.PI)
  11974. });
  11975. });
  11976. ae.on('mouseout', function(d, i) {
  11977. d3.select(this).classed('hover', false);
  11978. if (growOnHover) {
  11979. d3.select(this).select("path").transition()
  11980. .duration(50)
  11981. .attr("d", arcs[i]);
  11982. }
  11983. dispatch.elementMouseout({data: d.data, index: i});
  11984. });
  11985. ae.on('mousemove', function(d, i) {
  11986. dispatch.elementMousemove({data: d.data, index: i});
  11987. });
  11988. ae.on('click', function(d, i) {
  11989. var element = this;
  11990. dispatch.elementClick({
  11991. data: d.data,
  11992. index: i,
  11993. color: d3.select(this).style("fill"),
  11994. event: d3.event,
  11995. element: element
  11996. });
  11997. });
  11998. ae.on('dblclick', function(d, i) {
  11999. dispatch.elementDblClick({
  12000. data: d.data,
  12001. index: i,
  12002. color: d3.select(this).style("fill")
  12003. });
  12004. });
  12005. slices.attr('fill', function(d,i) { return color(d.data, i); });
  12006. slices.attr('stroke', function(d,i) { return color(d.data, i); });
  12007. var paths = ae.append('path').each(function(d) {
  12008. this._current = d;
  12009. });
  12010. slices.select('path')
  12011. .transition()
  12012. .duration(duration)
  12013. .attr('d', function (d, i) { return arcs[i](d); })
  12014. .attrTween('d', arcTween);
  12015. if (showLabels) {
  12016. // This does the normal label
  12017. var labelsArc = [];
  12018. for (var i = 0; i < data[0].length; i++) {
  12019. labelsArc.push(arcs[i]);
  12020. if (labelsOutside) {
  12021. if (donut) {
  12022. labelsArc[i] = d3.svg.arc().outerRadius(arcs[i].outerRadius());
  12023. if (startAngle !== false) labelsArc[i].startAngle(startAngle);
  12024. if (endAngle !== false) labelsArc[i].endAngle(endAngle);
  12025. }
  12026. } else if (!donut) {
  12027. labelsArc[i].innerRadius(0);
  12028. }
  12029. }
  12030. pieLabels.enter().append("g").classed("nv-label",true).each(function(d,i) {
  12031. var group = d3.select(this);
  12032. group.attr('transform', function (d, i) {
  12033. if (labelSunbeamLayout) {
  12034. d.outerRadius = arcsRadiusOuter[i] + 10; // Set Outer Coordinate
  12035. d.innerRadius = arcsRadiusOuter[i] + 15; // Set Inner Coordinate
  12036. var rotateAngle = (d.startAngle + d.endAngle) / 2 * (180 / Math.PI);
  12037. if ((d.startAngle + d.endAngle) / 2 < Math.PI) {
  12038. rotateAngle -= 90;
  12039. } else {
  12040. rotateAngle += 90;
  12041. }
  12042. return 'translate(' + labelsArc[i].centroid(d) + ') rotate(' + rotateAngle + ')';
  12043. } else {
  12044. d.outerRadius = radius + 10; // Set Outer Coordinate
  12045. d.innerRadius = radius + 15; // Set Inner Coordinate
  12046. return 'translate(' + labelsArc[i].centroid(d) + ')'
  12047. }
  12048. });
  12049. group.append('rect')
  12050. .style('stroke', '#fff')
  12051. .style('fill', '#fff')
  12052. .attr("rx", 3)
  12053. .attr("ry", 3);
  12054. group.append('text')
  12055. .style('text-anchor', labelSunbeamLayout ? ((d.startAngle + d.endAngle) / 2 < Math.PI ? 'start' : 'end') : 'middle') //center the text on it's origin or begin/end if orthogonal aligned
  12056. .style('fill', '#000')
  12057. });
  12058. var labelLocationHash = {};
  12059. var avgHeight = 14;
  12060. var avgWidth = 140;
  12061. var createHashKey = function(coordinates) {
  12062. return Math.floor(coordinates[0]/avgWidth) * avgWidth + ',' + Math.floor(coordinates[1]/avgHeight) * avgHeight;
  12063. };
  12064. var getSlicePercentage = function(d) {
  12065. return (d.endAngle - d.startAngle) / (2 * Math.PI);
  12066. };
  12067. pieLabels.watchTransition(renderWatch, 'pie labels').attr('transform', function (d, i) {
  12068. if (labelSunbeamLayout) {
  12069. d.outerRadius = arcsRadiusOuter[i] + 10; // Set Outer Coordinate
  12070. d.innerRadius = arcsRadiusOuter[i] + 15; // Set Inner Coordinate
  12071. var rotateAngle = (d.startAngle + d.endAngle) / 2 * (180 / Math.PI);
  12072. if ((d.startAngle + d.endAngle) / 2 < Math.PI) {
  12073. rotateAngle -= 90;
  12074. } else {
  12075. rotateAngle += 90;
  12076. }
  12077. return 'translate(' + labelsArc[i].centroid(d) + ') rotate(' + rotateAngle + ')';
  12078. } else {
  12079. d.outerRadius = radius + 10; // Set Outer Coordinate
  12080. d.innerRadius = radius + 15; // Set Inner Coordinate
  12081. /*
  12082. Overlapping pie labels are not good. What this attempts to do is, prevent overlapping.
  12083. Each label location is hashed, and if a hash collision occurs, we assume an overlap.
  12084. Adjust the label's y-position to remove the overlap.
  12085. */
  12086. var center = labelsArc[i].centroid(d);
  12087. var percent = getSlicePercentage(d);
  12088. if (d.value && percent >= labelThreshold) {
  12089. var hashKey = createHashKey(center);
  12090. if (labelLocationHash[hashKey]) {
  12091. center[1] -= avgHeight;
  12092. }
  12093. labelLocationHash[createHashKey(center)] = true;
  12094. }
  12095. return 'translate(' + center + ')'
  12096. }
  12097. });
  12098. pieLabels.select(".nv-label text")
  12099. .style('text-anchor', function(d,i) {
  12100. //center the text on it's origin or begin/end if orthogonal aligned
  12101. return labelSunbeamLayout ? ((d.startAngle + d.endAngle) / 2 < Math.PI ? 'start' : 'end') : 'middle';
  12102. })
  12103. .text(function(d, i) {
  12104. var percent = getSlicePercentage(d);
  12105. var label = '';
  12106. if (!d.value || percent < labelThreshold) return '';
  12107. if(typeof labelType === 'function') {
  12108. label = labelType(d, i, {
  12109. 'key': getX(d.data),
  12110. 'value': getY(d.data),
  12111. 'percent': valueFormat(percent)
  12112. });
  12113. } else {
  12114. switch (labelType) {
  12115. case 'key':
  12116. label = getX(d.data);
  12117. break;
  12118. case 'value':
  12119. label = valueFormat(getY(d.data));
  12120. break;
  12121. case 'percent':
  12122. label = d3.format('%')(percent);
  12123. break;
  12124. }
  12125. }
  12126. return label;
  12127. })
  12128. ;
  12129. if (hideOverlapLabels) {
  12130. pieLabels
  12131. .each(function (d, i) {
  12132. if (!this.getBBox) return;
  12133. var bb = this.getBBox(),
  12134. center = labelsArc[i].centroid(d);
  12135. var topLeft = {
  12136. x : center[0] + bb.x,
  12137. y : center[1] + bb.y
  12138. };
  12139. var topRight = {
  12140. x : topLeft.x + bb.width,
  12141. y : topLeft.y
  12142. };
  12143. var bottomLeft = {
  12144. x : topLeft.x,
  12145. y : topLeft.y + bb.height
  12146. };
  12147. var bottomRight = {
  12148. x : topLeft.x + bb.width,
  12149. y : topLeft.y + bb.height
  12150. };
  12151. d.visible = nv.utils.pointIsInArc(topLeft, d, arc) &&
  12152. nv.utils.pointIsInArc(topRight, d, arc) &&
  12153. nv.utils.pointIsInArc(bottomLeft, d, arc) &&
  12154. nv.utils.pointIsInArc(bottomRight, d, arc);
  12155. })
  12156. .style('display', function (d) {
  12157. return d.visible ? null : 'none';
  12158. })
  12159. ;
  12160. }
  12161. }
  12162. // Computes the angle of an arc, converting from radians to degrees.
  12163. function angle(d) {
  12164. var a = (d.startAngle + d.endAngle) * 90 / Math.PI - 90;
  12165. return a > 90 ? a - 180 : a;
  12166. }
  12167. function arcTween(a, idx) {
  12168. a.endAngle = isNaN(a.endAngle) ? 0 : a.endAngle;
  12169. a.startAngle = isNaN(a.startAngle) ? 0 : a.startAngle;
  12170. if (!donut) a.innerRadius = 0;
  12171. var i = d3.interpolate(this._current, a);
  12172. this._current = i(0);
  12173. return function (t) {
  12174. return arcs[idx](i(t));
  12175. };
  12176. }
  12177. });
  12178. renderWatch.renderEnd('pie immediate');
  12179. return chart;
  12180. }
  12181. //============================================================
  12182. // Expose Public Variables
  12183. //------------------------------------------------------------
  12184. chart.dispatch = dispatch;
  12185. chart.options = nv.utils.optionsFunc.bind(chart);
  12186. chart._options = Object.create({}, {
  12187. // simple options, just get/set the necessary values
  12188. arcsRadius: { get: function () { return arcsRadius; }, set: function (_) { arcsRadius = _; } },
  12189. width: {get: function(){return width;}, set: function(_){width=_;}},
  12190. height: {get: function(){return height;}, set: function(_){height=_;}},
  12191. showLabels: {get: function(){return showLabels;}, set: function(_){showLabels=_;}},
  12192. title: {get: function(){return title;}, set: function(_){title=_;}},
  12193. titleOffset: {get: function(){return titleOffset;}, set: function(_){titleOffset=_;}},
  12194. labelThreshold: {get: function(){return labelThreshold;}, set: function(_){labelThreshold=_;}},
  12195. hideOverlapLabels: {get: function(){return hideOverlapLabels;}, set: function(_){hideOverlapLabels=_;}},
  12196. valueFormat: {get: function(){return valueFormat;}, set: function(_){valueFormat=_;}},
  12197. x: {get: function(){return getX;}, set: function(_){getX=_;}},
  12198. id: {get: function(){return id;}, set: function(_){id=_;}},
  12199. endAngle: {get: function(){return endAngle;}, set: function(_){endAngle=_;}},
  12200. startAngle: {get: function(){return startAngle;}, set: function(_){startAngle=_;}},
  12201. padAngle: {get: function(){return padAngle;}, set: function(_){padAngle=_;}},
  12202. cornerRadius: {get: function(){return cornerRadius;}, set: function(_){cornerRadius=_;}},
  12203. donutRatio: {get: function(){return donutRatio;}, set: function(_){donutRatio=_;}},
  12204. labelsOutside: {get: function(){return labelsOutside;}, set: function(_){labelsOutside=_;}},
  12205. labelSunbeamLayout: {get: function(){return labelSunbeamLayout;}, set: function(_){labelSunbeamLayout=_;}},
  12206. donut: {get: function(){return donut;}, set: function(_){donut=_;}},
  12207. growOnHover: {get: function(){return growOnHover;}, set: function(_){growOnHover=_;}},
  12208. // depreciated after 1.7.1
  12209. pieLabelsOutside: {get: function(){return labelsOutside;}, set: function(_){
  12210. labelsOutside=_;
  12211. nv.deprecated('pieLabelsOutside', 'use labelsOutside instead');
  12212. }},
  12213. // depreciated after 1.7.1
  12214. donutLabelsOutside: {get: function(){return labelsOutside;}, set: function(_){
  12215. labelsOutside=_;
  12216. nv.deprecated('donutLabelsOutside', 'use labelsOutside instead');
  12217. }},
  12218. // deprecated after 1.7.1
  12219. labelFormat: {get: function(){ return valueFormat;}, set: function(_) {
  12220. valueFormat=_;
  12221. nv.deprecated('labelFormat','use valueFormat instead');
  12222. }},
  12223. // options that require extra logic in the setter
  12224. margin: {get: function(){return margin;}, set: function(_){
  12225. margin.top = typeof _.top != 'undefined' ? _.top : margin.top;
  12226. margin.right = typeof _.right != 'undefined' ? _.right : margin.right;
  12227. margin.bottom = typeof _.bottom != 'undefined' ? _.bottom : margin.bottom;
  12228. margin.left = typeof _.left != 'undefined' ? _.left : margin.left;
  12229. }},
  12230. duration: {get: function(){return duration;}, set: function(_){
  12231. duration = _;
  12232. renderWatch.reset(duration);
  12233. }},
  12234. y: {get: function(){return getY;}, set: function(_){
  12235. getY=d3.functor(_);
  12236. }},
  12237. color: {get: function(){return color;}, set: function(_){
  12238. color=nv.utils.getColor(_);
  12239. }},
  12240. labelType: {get: function(){return labelType;}, set: function(_){
  12241. labelType= _ || 'key';
  12242. }}
  12243. });
  12244. nv.utils.initOptions(chart);
  12245. return chart;
  12246. };
  12247. nv.models.pieChart = function() {
  12248. "use strict";
  12249. //============================================================
  12250. // Public Variables with Default Settings
  12251. //------------------------------------------------------------
  12252. var pie = nv.models.pie();
  12253. var legend = nv.models.legend();
  12254. var tooltip = nv.models.tooltip();
  12255. var margin = {top: 30, right: 20, bottom: 20, left: 20}
  12256. , marginTop = null
  12257. , width = null
  12258. , height = null
  12259. , showTooltipPercent = false
  12260. , showLegend = true
  12261. , legendPosition = "top"
  12262. , color = nv.utils.defaultColor()
  12263. , state = nv.utils.state()
  12264. , defaultState = null
  12265. , noData = null
  12266. , duration = 250
  12267. , dispatch = d3.dispatch('stateChange', 'changeState','renderEnd')
  12268. ;
  12269. tooltip
  12270. .duration(0)
  12271. .headerEnabled(false)
  12272. .valueFormatter(function(d, i) {
  12273. return pie.valueFormat()(d, i);
  12274. });
  12275. //============================================================
  12276. // Private Variables
  12277. //------------------------------------------------------------
  12278. var renderWatch = nv.utils.renderWatch(dispatch);
  12279. var stateGetter = function(data) {
  12280. return function(){
  12281. return {
  12282. active: data.map(function(d) { return !d.disabled })
  12283. };
  12284. }
  12285. };
  12286. var stateSetter = function(data) {
  12287. return function(state) {
  12288. if (state.active !== undefined) {
  12289. data.forEach(function (series, i) {
  12290. series.disabled = !state.active[i];
  12291. });
  12292. }
  12293. }
  12294. };
  12295. //============================================================
  12296. // Chart function
  12297. //------------------------------------------------------------
  12298. function chart(selection) {
  12299. renderWatch.reset();
  12300. renderWatch.models(pie);
  12301. selection.each(function(data) {
  12302. var container = d3.select(this);
  12303. nv.utils.initSVG(container);
  12304. var that = this;
  12305. var availableWidth = nv.utils.availableWidth(width, container, margin),
  12306. availableHeight = nv.utils.availableHeight(height, container, margin);
  12307. chart.update = function() { container.transition().call(chart); };
  12308. chart.container = this;
  12309. state.setter(stateSetter(data), chart.update)
  12310. .getter(stateGetter(data))
  12311. .update();
  12312. //set state.disabled
  12313. state.disabled = data.map(function(d) { return !!d.disabled });
  12314. if (!defaultState) {
  12315. var key;
  12316. defaultState = {};
  12317. for (key in state) {
  12318. if (state[key] instanceof Array)
  12319. defaultState[key] = state[key].slice(0);
  12320. else
  12321. defaultState[key] = state[key];
  12322. }
  12323. }
  12324. // Display No Data message if there's nothing to show.
  12325. if (!data || !data.length) {
  12326. nv.utils.noData(chart, container);
  12327. return chart;
  12328. } else {
  12329. container.selectAll('.nv-noData').remove();
  12330. }
  12331. // Setup containers and skeleton of chart
  12332. var wrap = container.selectAll('g.nv-wrap.nv-pieChart').data([data]);
  12333. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-pieChart').append('g');
  12334. var g = wrap.select('g');
  12335. gEnter.append('g').attr('class', 'nv-pieWrap');
  12336. gEnter.append('g').attr('class', 'nv-legendWrap');
  12337. // Legend
  12338. if (!showLegend) {
  12339. g.select('.nv-legendWrap').selectAll('*').remove();
  12340. } else {
  12341. if (legendPosition === "top") {
  12342. legend.width( availableWidth ).key(pie.x());
  12343. wrap.select('.nv-legendWrap')
  12344. .datum(data)
  12345. .call(legend);
  12346. if (!marginTop && legend.height() !== margin.top) {
  12347. margin.top = legend.height();
  12348. availableHeight = nv.utils.availableHeight(height, container, margin);
  12349. }
  12350. wrap.select('.nv-legendWrap')
  12351. .attr('transform', 'translate(0,' + (-margin.top) +')');
  12352. } else if (legendPosition === "right") {
  12353. var legendWidth = nv.models.legend().width();
  12354. if (availableWidth / 2 < legendWidth) {
  12355. legendWidth = (availableWidth / 2)
  12356. }
  12357. legend.height(availableHeight).key(pie.x());
  12358. legend.width(legendWidth);
  12359. availableWidth -= legend.width();
  12360. wrap.select('.nv-legendWrap')
  12361. .datum(data)
  12362. .call(legend)
  12363. .attr('transform', 'translate(' + (availableWidth) +',0)');
  12364. } else if (legendPosition === "bottom") {
  12365. legend.width( availableWidth ).key(pie.x());
  12366. wrap.select('.nv-legendWrap')
  12367. .datum(data)
  12368. .call(legend);
  12369. margin.bottom = legend.height();
  12370. availableHeight = nv.utils.availableHeight(height, container, margin);
  12371. wrap.select('.nv-legendWrap')
  12372. .attr('transform', 'translate(0,' + availableHeight +')');
  12373. }
  12374. }
  12375. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  12376. // Main Chart Component(s)
  12377. pie.width(availableWidth).height(availableHeight);
  12378. var pieWrap = g.select('.nv-pieWrap').datum([data]);
  12379. d3.transition(pieWrap).call(pie);
  12380. //============================================================
  12381. // Event Handling/Dispatching (in chart's scope)
  12382. //------------------------------------------------------------
  12383. legend.dispatch.on('stateChange', function(newState) {
  12384. for (var key in newState) {
  12385. state[key] = newState[key];
  12386. }
  12387. dispatch.stateChange(state);
  12388. chart.update();
  12389. });
  12390. // Update chart from a state object passed to event handler
  12391. dispatch.on('changeState', function(e) {
  12392. if (typeof e.disabled !== 'undefined') {
  12393. data.forEach(function(series,i) {
  12394. series.disabled = e.disabled[i];
  12395. });
  12396. state.disabled = e.disabled;
  12397. }
  12398. chart.update();
  12399. });
  12400. });
  12401. renderWatch.renderEnd('pieChart immediate');
  12402. return chart;
  12403. }
  12404. //============================================================
  12405. // Event Handling/Dispatching (out of chart's scope)
  12406. //------------------------------------------------------------
  12407. pie.dispatch.on('elementMouseover.tooltip', function(evt) {
  12408. evt['series'] = {
  12409. key: chart.x()(evt.data),
  12410. value: chart.y()(evt.data),
  12411. color: evt.color,
  12412. percent: evt.percent
  12413. };
  12414. if (!showTooltipPercent) {
  12415. delete evt.percent;
  12416. delete evt.series.percent;
  12417. }
  12418. tooltip.data(evt).hidden(false);
  12419. });
  12420. pie.dispatch.on('elementMouseout.tooltip', function(evt) {
  12421. tooltip.hidden(true);
  12422. });
  12423. pie.dispatch.on('elementMousemove.tooltip', function(evt) {
  12424. tooltip();
  12425. });
  12426. //============================================================
  12427. // Expose Public Variables
  12428. //------------------------------------------------------------
  12429. // expose chart's sub-components
  12430. chart.legend = legend;
  12431. chart.dispatch = dispatch;
  12432. chart.pie = pie;
  12433. chart.tooltip = tooltip;
  12434. chart.options = nv.utils.optionsFunc.bind(chart);
  12435. // use Object get/set functionality to map between vars and chart functions
  12436. chart._options = Object.create({}, {
  12437. // simple options, just get/set the necessary values
  12438. width: {get: function(){return width;}, set: function(_){width=_;}},
  12439. height: {get: function(){return height;}, set: function(_){height=_;}},
  12440. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  12441. showTooltipPercent: {get: function(){return showTooltipPercent;}, set: function(_){showTooltipPercent=_;}},
  12442. showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
  12443. legendPosition: {get: function(){return legendPosition;}, set: function(_){legendPosition=_;}},
  12444. defaultState: {get: function(){return defaultState;}, set: function(_){defaultState=_;}},
  12445. // options that require extra logic in the setter
  12446. color: {get: function(){return color;}, set: function(_){
  12447. color = _;
  12448. legend.color(color);
  12449. pie.color(color);
  12450. }},
  12451. duration: {get: function(){return duration;}, set: function(_){
  12452. duration = _;
  12453. renderWatch.reset(duration);
  12454. pie.duration(duration);
  12455. }},
  12456. margin: {get: function(){return margin;}, set: function(_){
  12457. if (_.top !== undefined) {
  12458. margin.top = _.top;
  12459. marginTop = _.top;
  12460. }
  12461. margin.right = _.right !== undefined ? _.right : margin.right;
  12462. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  12463. margin.left = _.left !== undefined ? _.left : margin.left;
  12464. }}
  12465. });
  12466. nv.utils.inheritOptions(chart, pie);
  12467. nv.utils.initOptions(chart);
  12468. return chart;
  12469. };
  12470. nv.models.sankey = function() {
  12471. 'use strict';
  12472. // Sources:
  12473. // - https://bost.ocks.org/mike/sankey/
  12474. // - https://github.com/soxofaan/d3-plugin-captain-sankey
  12475. //============================================================
  12476. // Public Variables with Default Settings
  12477. //------------------------------------------------------------
  12478. var sankey = {},
  12479. nodeWidth = 24,
  12480. nodePadding = 8,
  12481. size = [1, 1],
  12482. nodes = [],
  12483. links = [],
  12484. sinksRight = true;
  12485. var layout = function(iterations) {
  12486. computeNodeLinks();
  12487. computeNodeValues();
  12488. computeNodeBreadths();
  12489. computeNodeDepths(iterations);
  12490. };
  12491. var relayout = function() {
  12492. computeLinkDepths();
  12493. };
  12494. // SVG path data generator, to be used as 'd' attribute on 'path' element selection.
  12495. var link = function() {
  12496. var curvature = .5;
  12497. function link(d) {
  12498. var x0 = d.source.x + d.source.dx,
  12499. x1 = d.target.x,
  12500. xi = d3.interpolateNumber(x0, x1),
  12501. x2 = xi(curvature),
  12502. x3 = xi(1 - curvature),
  12503. y0 = d.source.y + d.sy + d.dy / 2,
  12504. y1 = d.target.y + d.ty + d.dy / 2;
  12505. var linkPath = 'M' + x0 + ',' + y0
  12506. + 'C' + x2 + ',' + y0
  12507. + ' ' + x3 + ',' + y1
  12508. + ' ' + x1 + ',' + y1;
  12509. return linkPath;
  12510. }
  12511. link.curvature = function(_) {
  12512. if (!arguments.length) return curvature;
  12513. curvature = +_;
  12514. return link;
  12515. };
  12516. return link;
  12517. };
  12518. // Y-position of the middle of a node.
  12519. var center = function(node) {
  12520. return node.y + node.dy / 2;
  12521. };
  12522. //============================================================
  12523. // Private Variables
  12524. //------------------------------------------------------------
  12525. // Populate the sourceLinks and targetLinks for each node.
  12526. // Also, if the source and target are not objects, assume they are indices.
  12527. function computeNodeLinks() {
  12528. nodes.forEach(function(node) {
  12529. // Links that have this node as source.
  12530. node.sourceLinks = [];
  12531. // Links that have this node as target.
  12532. node.targetLinks = [];
  12533. });
  12534. links.forEach(function(link) {
  12535. var source = link.source,
  12536. target = link.target;
  12537. if (typeof source === 'number') source = link.source = nodes[link.source];
  12538. if (typeof target === 'number') target = link.target = nodes[link.target];
  12539. source.sourceLinks.push(link);
  12540. target.targetLinks.push(link);
  12541. });
  12542. }
  12543. // Compute the value (size) of each node by summing the associated links.
  12544. function computeNodeValues() {
  12545. nodes.forEach(function(node) {
  12546. node.value = Math.max(
  12547. d3.sum(node.sourceLinks, value),
  12548. d3.sum(node.targetLinks, value)
  12549. );
  12550. });
  12551. }
  12552. // Iteratively assign the breadth (x-position) for each node.
  12553. // Nodes are assigned the maximum breadth of incoming neighbors plus one;
  12554. // nodes with no incoming links are assigned breadth zero, while
  12555. // nodes with no outgoing links are assigned the maximum breadth.
  12556. function computeNodeBreadths() {
  12557. //
  12558. var remainingNodes = nodes,
  12559. nextNodes,
  12560. x = 0;
  12561. // Work from left to right.
  12562. // Keep updating the breath (x-position) of nodes that are target of recently updated nodes.
  12563. //
  12564. while (remainingNodes.length && x < nodes.length) {
  12565. nextNodes = [];
  12566. remainingNodes.forEach(function(node) {
  12567. node.x = x;
  12568. node.dx = nodeWidth;
  12569. node.sourceLinks.forEach(function(link) {
  12570. if (nextNodes.indexOf(link.target) < 0) {
  12571. nextNodes.push(link.target);
  12572. }
  12573. });
  12574. });
  12575. remainingNodes = nextNodes;
  12576. ++x;
  12577. //
  12578. }
  12579. // Optionally move pure sinks always to the right.
  12580. if (sinksRight) {
  12581. moveSinksRight(x);
  12582. }
  12583. scaleNodeBreadths((size[0] - nodeWidth) / (x - 1));
  12584. }
  12585. function moveSourcesRight() {
  12586. nodes.forEach(function(node) {
  12587. if (!node.targetLinks.length) {
  12588. node.x = d3.min(node.sourceLinks, function(d) { return d.target.x; }) - 1;
  12589. }
  12590. });
  12591. }
  12592. function moveSinksRight(x) {
  12593. nodes.forEach(function(node) {
  12594. if (!node.sourceLinks.length) {
  12595. node.x = x - 1;
  12596. }
  12597. });
  12598. }
  12599. function scaleNodeBreadths(kx) {
  12600. nodes.forEach(function(node) {
  12601. node.x *= kx;
  12602. });
  12603. }
  12604. // Compute the depth (y-position) for each node.
  12605. function computeNodeDepths(iterations) {
  12606. // Group nodes by breath.
  12607. var nodesByBreadth = d3.nest()
  12608. .key(function(d) { return d.x; })
  12609. .sortKeys(d3.ascending)
  12610. .entries(nodes)
  12611. .map(function(d) { return d.values; });
  12612. //
  12613. initializeNodeDepth();
  12614. resolveCollisions();
  12615. computeLinkDepths();
  12616. for (var alpha = 1; iterations > 0; --iterations) {
  12617. relaxRightToLeft(alpha *= .99);
  12618. resolveCollisions();
  12619. computeLinkDepths();
  12620. relaxLeftToRight(alpha);
  12621. resolveCollisions();
  12622. computeLinkDepths();
  12623. }
  12624. function initializeNodeDepth() {
  12625. // Calculate vertical scaling factor.
  12626. var ky = d3.min(nodesByBreadth, function(nodes) {
  12627. return (size[1] - (nodes.length - 1) * nodePadding) / d3.sum(nodes, value);
  12628. });
  12629. nodesByBreadth.forEach(function(nodes) {
  12630. nodes.forEach(function(node, i) {
  12631. node.y = i;
  12632. node.dy = node.value * ky;
  12633. });
  12634. });
  12635. links.forEach(function(link) {
  12636. link.dy = link.value * ky;
  12637. });
  12638. }
  12639. function relaxLeftToRight(alpha) {
  12640. nodesByBreadth.forEach(function(nodes, breadth) {
  12641. nodes.forEach(function(node) {
  12642. if (node.targetLinks.length) {
  12643. // Value-weighted average of the y-position of source node centers linked to this node.
  12644. var y = d3.sum(node.targetLinks, weightedSource) / d3.sum(node.targetLinks, value);
  12645. node.y += (y - center(node)) * alpha;
  12646. }
  12647. });
  12648. });
  12649. function weightedSource(link) {
  12650. return (link.source.y + link.sy + link.dy / 2) * link.value;
  12651. }
  12652. }
  12653. function relaxRightToLeft(alpha) {
  12654. nodesByBreadth.slice().reverse().forEach(function(nodes) {
  12655. nodes.forEach(function(node) {
  12656. if (node.sourceLinks.length) {
  12657. // Value-weighted average of the y-positions of target nodes linked to this node.
  12658. var y = d3.sum(node.sourceLinks, weightedTarget) / d3.sum(node.sourceLinks, value);
  12659. node.y += (y - center(node)) * alpha;
  12660. }
  12661. });
  12662. });
  12663. function weightedTarget(link) {
  12664. return (link.target.y + link.ty + link.dy / 2) * link.value;
  12665. }
  12666. }
  12667. function resolveCollisions() {
  12668. nodesByBreadth.forEach(function(nodes) {
  12669. var node,
  12670. dy,
  12671. y0 = 0,
  12672. n = nodes.length,
  12673. i;
  12674. // Push any overlapping nodes down.
  12675. nodes.sort(ascendingDepth);
  12676. for (i = 0; i < n; ++i) {
  12677. node = nodes[i];
  12678. dy = y0 - node.y;
  12679. if (dy > 0) node.y += dy;
  12680. y0 = node.y + node.dy + nodePadding;
  12681. }
  12682. // If the bottommost node goes outside the bounds, push it back up.
  12683. dy = y0 - nodePadding - size[1];
  12684. if (dy > 0) {
  12685. y0 = node.y -= dy;
  12686. // Push any overlapping nodes back up.
  12687. for (i = n - 2; i >= 0; --i) {
  12688. node = nodes[i];
  12689. dy = node.y + node.dy + nodePadding - y0;
  12690. if (dy > 0) node.y -= dy;
  12691. y0 = node.y;
  12692. }
  12693. }
  12694. });
  12695. }
  12696. function ascendingDepth(a, b) {
  12697. return a.y - b.y;
  12698. }
  12699. }
  12700. // Compute y-offset of the source endpoint (sy) and target endpoints (ty) of links,
  12701. // relative to the source/target node's y-position.
  12702. function computeLinkDepths() {
  12703. nodes.forEach(function(node) {
  12704. node.sourceLinks.sort(ascendingTargetDepth);
  12705. node.targetLinks.sort(ascendingSourceDepth);
  12706. });
  12707. nodes.forEach(function(node) {
  12708. var sy = 0, ty = 0;
  12709. node.sourceLinks.forEach(function(link) {
  12710. link.sy = sy;
  12711. sy += link.dy;
  12712. });
  12713. node.targetLinks.forEach(function(link) {
  12714. link.ty = ty;
  12715. ty += link.dy;
  12716. });
  12717. });
  12718. function ascendingSourceDepth(a, b) {
  12719. return a.source.y - b.source.y;
  12720. }
  12721. function ascendingTargetDepth(a, b) {
  12722. return a.target.y - b.target.y;
  12723. }
  12724. }
  12725. // Value property accessor.
  12726. function value(x) {
  12727. return x.value;
  12728. }
  12729. sankey.options = nv.utils.optionsFunc.bind(sankey);
  12730. sankey._options = Object.create({}, {
  12731. nodeWidth: {get: function(){return nodeWidth;}, set: function(_){nodeWidth=+_;}},
  12732. nodePadding: {get: function(){return nodePadding;}, set: function(_){nodePadding=_;}},
  12733. nodes: {get: function(){return nodes;}, set: function(_){nodes=_;}},
  12734. links: {get: function(){return links ;}, set: function(_){links=_;}},
  12735. size: {get: function(){return size;}, set: function(_){size=_;}},
  12736. sinksRight: {get: function(){return sinksRight;}, set: function(_){sinksRight=_;}},
  12737. layout: {get: function(){layout(32);}, set: function(_){layout(_);}},
  12738. relayout: {get: function(){relayout();}, set: function(_){}},
  12739. center: {get: function(){return center();}, set: function(_){
  12740. if(typeof _ === 'function'){
  12741. center=_;
  12742. }
  12743. }},
  12744. link: {get: function(){return link();}, set: function(_){
  12745. if(typeof _ === 'function'){
  12746. link=_;
  12747. }
  12748. return link();
  12749. }}
  12750. });
  12751. nv.utils.initOptions(sankey);
  12752. return sankey;
  12753. };
  12754. nv.models.sankeyChart = function() {
  12755. "use strict";
  12756. // Sources:
  12757. // - https://bost.ocks.org/mike/sankey/
  12758. // - https://github.com/soxofaan/d3-plugin-captain-sankey
  12759. //============================================================
  12760. // Public Variables with Default Settings
  12761. //------------------------------------------------------------
  12762. var margin = {top: 5, right: 0, bottom: 5, left: 0}
  12763. , sankey = nv.models.sankey()
  12764. , width = 600
  12765. , height = 400
  12766. , nodeWidth = 36
  12767. , nodePadding = 40
  12768. , units = 'units'
  12769. , center = undefined
  12770. ;
  12771. //============================================================
  12772. // Private Variables
  12773. //------------------------------------------------------------
  12774. var formatNumber = d3.format(',.0f'); // zero decimal places
  12775. var format = function(d) {
  12776. return formatNumber(d) + ' ' + units;
  12777. };
  12778. var color = d3.scale.category20();
  12779. var linkTitle = function(d){
  12780. return d.source.name + ' → ' + d.target.name + '\n' + format(d.value);
  12781. };
  12782. var nodeFillColor = function(d){
  12783. return d.color = color(d.name.replace(/ .*/, ''));
  12784. };
  12785. var nodeStrokeColor = function(d){
  12786. return d3.rgb(d.color).darker(2);
  12787. };
  12788. var nodeTitle = function(d){
  12789. return d.name + '\n' + format(d.value);
  12790. };
  12791. var showError = function(element, message) {
  12792. element.append('text')
  12793. .attr('x', 0)
  12794. .attr('y', 0)
  12795. .attr('class', 'nvd3-sankey-chart-error')
  12796. .attr('text-anchor', 'middle')
  12797. .text(message);
  12798. };
  12799. function chart(selection) {
  12800. selection.each(function(data) {
  12801. var testData = {
  12802. nodes:
  12803. [
  12804. {'node': 1, 'name': 'Test 1'},
  12805. {'node': 2, 'name': 'Test 2'},
  12806. {'node': 3, 'name': 'Test 3'},
  12807. {'node': 4, 'name': 'Test 4'},
  12808. {'node': 5, 'name': 'Test 5'},
  12809. {'node': 6, 'name': 'Test 6'}
  12810. ],
  12811. links:
  12812. [
  12813. {'source': 0, 'target': 1, 'value': 2295},
  12814. {'source': 0, 'target': 5, 'value': 1199},
  12815. {'source': 1, 'target': 2, 'value': 1119},
  12816. {'source': 1, 'target': 5, 'value': 1176},
  12817. {'source': 2, 'target': 3, 'value': 487},
  12818. {'source': 2, 'target': 5, 'value': 632},
  12819. {'source': 3, 'target': 4, 'value': 301},
  12820. {'source': 3, 'target': 5, 'value': 186}
  12821. ]
  12822. };
  12823. // Error handling
  12824. var isDataValid = false;
  12825. var dataAvailable = false;
  12826. // check if data is valid
  12827. if(
  12828. (typeof data['nodes'] === 'object' && data['nodes'].length) >= 0 &&
  12829. (typeof data['links'] === 'object' && data['links'].length) >= 0
  12830. ){
  12831. isDataValid = true;
  12832. }
  12833. // check if data is available
  12834. if(
  12835. data['nodes'] && data['nodes'].length > 0 &&
  12836. data['links'] && data['links'].length > 0
  12837. ) {
  12838. dataAvailable = true;
  12839. }
  12840. // show error
  12841. if(!isDataValid) {
  12842. console.error('NVD3 Sankey chart error:', 'invalid data format for', data);
  12843. console.info('Valid data format is: ', testData, JSON.stringify(testData));
  12844. showError(selection, 'Error loading chart, data is invalid');
  12845. return false;
  12846. }
  12847. // TODO use nv.utils.noData
  12848. if(!dataAvailable) {
  12849. showError(selection, 'No data available');
  12850. return false;
  12851. }
  12852. // No errors, continue
  12853. // append the svg canvas to the page
  12854. var svg = selection.append('svg')
  12855. .attr('width', width)
  12856. .attr('height', height)
  12857. .append('g')
  12858. .attr('class', 'nvd3 nv-wrap nv-sankeyChart');
  12859. // Set the sankey diagram properties
  12860. sankey
  12861. .nodeWidth(nodeWidth)
  12862. .nodePadding(nodePadding)
  12863. .size([width, height]);
  12864. var path = sankey.link();
  12865. sankey
  12866. .nodes(data.nodes)
  12867. .links(data.links)
  12868. .layout(32)
  12869. .center(center);
  12870. // add in the links
  12871. var link = svg.append('g').selectAll('.link')
  12872. .data(data.links)
  12873. .enter().append('path')
  12874. .attr('class', 'link')
  12875. .attr('d', path)
  12876. .style('stroke-width', function(d) { return Math.max(1, d.dy); })
  12877. .sort(function(a,b) { return b.dy - a.dy; });
  12878. // add the link titles
  12879. link.append('title')
  12880. .text(linkTitle);
  12881. // add in the nodes
  12882. var node = svg.append('g').selectAll('.node')
  12883. .data(data.nodes)
  12884. .enter().append('g')
  12885. .attr('class', 'node')
  12886. .attr('transform', function(d) { return 'translate(' + d.x + ',' + d.y + ')'; })
  12887. .call(
  12888. d3.behavior
  12889. .drag()
  12890. .origin(function(d) { return d; })
  12891. .on('dragstart', function() {
  12892. this.parentNode.appendChild(this);
  12893. })
  12894. .on('drag', dragmove)
  12895. );
  12896. // add the rectangles for the nodes
  12897. node.append('rect')
  12898. .attr('height', function(d) { return d.dy; })
  12899. .attr('width', sankey.nodeWidth())
  12900. .style('fill', nodeFillColor)
  12901. .style('stroke', nodeStrokeColor)
  12902. .append('title')
  12903. .text(nodeTitle);
  12904. // add in the title for the nodes
  12905. node.append('text')
  12906. .attr('x', -6)
  12907. .attr('y', function(d) { return d.dy / 2; })
  12908. .attr('dy', '.35em')
  12909. .attr('text-anchor', 'end')
  12910. .attr('transform', null)
  12911. .text(function(d) { return d.name; })
  12912. .filter(function(d) { return d.x < width / 2; })
  12913. .attr('x', 6 + sankey.nodeWidth())
  12914. .attr('text-anchor', 'start');
  12915. // the function for moving the nodes
  12916. function dragmove(d) {
  12917. d3.select(this).attr('transform',
  12918. 'translate(' + d.x + ',' + (
  12919. d.y = Math.max(0, Math.min(height - d.dy, d3.event.y))
  12920. ) + ')');
  12921. sankey.relayout();
  12922. link.attr('d', path);
  12923. }
  12924. });
  12925. return chart;
  12926. }
  12927. //============================================================
  12928. // Expose Public Variables
  12929. //------------------------------------------------------------
  12930. chart.options = nv.utils.optionsFunc.bind(chart);
  12931. chart._options = Object.create({}, {
  12932. // simple options, just get/set the necessary values
  12933. units: {get: function(){return units;}, set: function(_){units=_;}},
  12934. width: {get: function(){return width;}, set: function(_){width=_;}},
  12935. height: {get: function(){return height;}, set: function(_){height=_;}},
  12936. format: {get: function(){return format;}, set: function(_){format=_;}},
  12937. linkTitle: {get: function(){return linkTitle;}, set: function(_){linkTitle=_;}},
  12938. nodeWidth: {get: function(){return nodeWidth;}, set: function(_){nodeWidth=_;}},
  12939. nodePadding: {get: function(){return nodePadding;}, set: function(_){nodePadding=_;}},
  12940. center: {get: function(){return center}, set: function(_){center=_}},
  12941. // options that require extra logic in the setter
  12942. margin: {get: function(){return margin;}, set: function(_){
  12943. margin.top = _.top !== undefined ? _.top : margin.top;
  12944. margin.right = _.right !== undefined ? _.right : margin.right;
  12945. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  12946. margin.left = _.left !== undefined ? _.left : margin.left;
  12947. }},
  12948. nodeStyle: {get: function(){return {};}, set: function(_){
  12949. nodeFillColor = _.fillColor !== undefined ? _.fillColor : nodeFillColor;
  12950. nodeStrokeColor = _.strokeColor !== undefined ? _.strokeColor : nodeStrokeColor;
  12951. nodeTitle = _.title !== undefined ? _.title : nodeTitle;
  12952. }}
  12953. });
  12954. nv.utils.initOptions(chart);
  12955. return chart;
  12956. };
  12957. nv.models.scatter = function() {
  12958. "use strict";
  12959. //============================================================
  12960. // Public Variables with Default Settings
  12961. //------------------------------------------------------------
  12962. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  12963. , width = null
  12964. , height = null
  12965. , color = nv.utils.defaultColor() // chooses color
  12966. , pointBorderColor = null
  12967. , id = Math.floor(Math.random() * 100000) //Create semi-unique ID incase user doesn't select one
  12968. , container = null
  12969. , x = d3.scale.linear()
  12970. , y = d3.scale.linear()
  12971. , z = d3.scale.linear() //linear because d3.svg.shape.size is treated as area
  12972. , getX = function(d) { return d.x } // accessor to get the x value
  12973. , getY = function(d) { return d.y } // accessor to get the y value
  12974. , getSize = function(d) { return d.size || 1} // accessor to get the point size
  12975. , getShape = function(d) { return d.shape || 'circle' } // accessor to get point shape
  12976. , forceX = [] // List of numbers to Force into the X scale (ie. 0, or a max / min, etc.)
  12977. , forceY = [] // List of numbers to Force into the Y scale
  12978. , forceSize = [] // List of numbers to Force into the Size scale
  12979. , interactive = true // If true, plots a voronoi overlay for advanced point intersection
  12980. , pointActive = function(d) { return !d.notActive } // any points that return false will be filtered out
  12981. , padData = false // If true, adds half a data points width to front and back, for lining up a line chart with a bar chart
  12982. , padDataOuter = .1 //outerPadding to imitate ordinal scale outer padding
  12983. , clipEdge = false // if true, masks points within x and y scale
  12984. , clipVoronoi = true // if true, masks each point with a circle... can turn off to slightly increase performance
  12985. , showVoronoi = false // display the voronoi areas
  12986. , clipRadius = function() { return 25 } // function to get the radius for voronoi point clips
  12987. , xDomain = null // Override x domain (skips the calculation from data)
  12988. , yDomain = null // Override y domain
  12989. , xRange = null // Override x range
  12990. , yRange = null // Override y range
  12991. , sizeDomain = null // Override point size domain
  12992. , sizeRange = null
  12993. , singlePoint = false
  12994. , dispatch = d3.dispatch('elementClick', 'elementDblClick', 'elementMouseover', 'elementMouseout', 'renderEnd')
  12995. , useVoronoi = true
  12996. , duration = 250
  12997. , interactiveUpdateDelay = 300
  12998. , showLabels = false
  12999. ;
  13000. //============================================================
  13001. // Private Variables
  13002. //------------------------------------------------------------
  13003. var x0, y0, z0 // used to store previous scales
  13004. , xDom, yDom // used to store previous domains
  13005. , width0
  13006. , height0
  13007. , timeoutID
  13008. , needsUpdate = false // Flag for when the points are visually updating, but the interactive layer is behind, to disable tooltips
  13009. , renderWatch = nv.utils.renderWatch(dispatch, duration)
  13010. , _sizeRange_def = [16, 256]
  13011. , _cache = {}
  13012. ;
  13013. //============================================================
  13014. // Diff and Cache Utilities
  13015. //------------------------------------------------------------
  13016. // getDiffs is used to filter unchanged points from the update
  13017. // selection. It implicitly updates it's cache when called and
  13018. // therefor the diff is based upon the previous invocation NOT
  13019. // the previous update.
  13020. //
  13021. // getDiffs takes a point as its first argument followed by n
  13022. // key getter pairs (d, [key, get... key, get]) this approach
  13023. // was chosen for efficiency. (The filter will call it a LOT).
  13024. //
  13025. // It is important to call delCache on point exit to prevent a
  13026. // memory leak. It is also needed to prevent invalid caches if
  13027. // a new point uses the same series and point id key.
  13028. //
  13029. // Argument Performance Concerns:
  13030. // - Object property lists for key getter pairs would be very
  13031. // expensive (points * objects for the GC every update).
  13032. // - ES6 function names for implicit keys would be nice but
  13033. // they are not guaranteed to be unique.
  13034. // - function.toString to obtain implicit keys is possible
  13035. // but long object keys are not free (internal hash).
  13036. // - Explicit key without objects are the most efficient.
  13037. function getCache(d) {
  13038. var key, val;
  13039. key = d[0].series + ':' + d[1];
  13040. val = _cache[key] = _cache[key] || {};
  13041. return val;
  13042. }
  13043. function delCache(d) {
  13044. var key, val;
  13045. key = d[0].series + ':' + d[1];
  13046. delete _cache[key];
  13047. }
  13048. function getDiffs(d) {
  13049. var i, key, val,
  13050. cache = getCache(d),
  13051. diffs = false;
  13052. for (i = 1; i < arguments.length; i += 2) {
  13053. key = arguments[i];
  13054. val = arguments[i + 1](d[0], d[1]);
  13055. if (cache[key] !== val || !cache.hasOwnProperty(key)) {
  13056. cache[key] = val;
  13057. diffs = true;
  13058. }
  13059. }
  13060. return diffs;
  13061. }
  13062. function chart(selection) {
  13063. renderWatch.reset();
  13064. selection.each(function(data) {
  13065. container = d3.select(this);
  13066. var availableWidth = nv.utils.availableWidth(width, container, margin),
  13067. availableHeight = nv.utils.availableHeight(height, container, margin);
  13068. nv.utils.initSVG(container);
  13069. //add series index to each data point for reference
  13070. data.forEach(function(series, i) {
  13071. series.values.forEach(function(point) {
  13072. point.series = i;
  13073. });
  13074. });
  13075. // Setup Scales
  13076. var logScale = (typeof(chart.yScale().base) === "function"); // Only log scale has a method "base()"
  13077. // remap and flatten the data for use in calculating the scales' domains
  13078. var seriesData = (xDomain && yDomain && sizeDomain) ? [] : // if we know xDomain and yDomain and sizeDomain, no need to calculate.... if Size is constant remember to set sizeDomain to speed up performance
  13079. d3.merge(
  13080. data.map(function(d) {
  13081. return d.values.map(function(d,i) {
  13082. return { x: getX(d,i), y: getY(d,i), size: getSize(d,i) }
  13083. })
  13084. })
  13085. );
  13086. x .domain(xDomain || d3.extent(seriesData.map(function(d) { return d.x; }).concat(forceX)))
  13087. if (padData && data[0])
  13088. x.range(xRange || [(availableWidth * padDataOuter + availableWidth) / (2 *data[0].values.length), availableWidth - availableWidth * (1 + padDataOuter) / (2 * data[0].values.length) ]);
  13089. //x.range([availableWidth * .5 / data[0].values.length, availableWidth * (data[0].values.length - .5) / data[0].values.length ]);
  13090. else
  13091. x.range(xRange || [0, availableWidth]);
  13092. if (logScale) {
  13093. var min = d3.min(seriesData.map(function(d) { if (d.y !== 0) return d.y; }));
  13094. y.clamp(true)
  13095. .domain(yDomain || d3.extent(seriesData.map(function(d) {
  13096. if (d.y !== 0) return d.y;
  13097. else return min * 0.1;
  13098. }).concat(forceY)))
  13099. .range(yRange || [availableHeight, 0]);
  13100. } else {
  13101. y.domain(yDomain || d3.extent(seriesData.map(function (d) { return d.y;}).concat(forceY)))
  13102. .range(yRange || [availableHeight, 0]);
  13103. }
  13104. z .domain(sizeDomain || d3.extent(seriesData.map(function(d) { return d.size }).concat(forceSize)))
  13105. .range(sizeRange || _sizeRange_def);
  13106. // If scale's domain don't have a range, slightly adjust to make one... so a chart can show a single data point
  13107. singlePoint = x.domain()[0] === x.domain()[1] || y.domain()[0] === y.domain()[1];
  13108. if (x.domain()[0] === x.domain()[1])
  13109. x.domain()[0] ?
  13110. x.domain([x.domain()[0] - x.domain()[0] * 0.01, x.domain()[1] + x.domain()[1] * 0.01])
  13111. : x.domain([-1,1]);
  13112. if (y.domain()[0] === y.domain()[1])
  13113. y.domain()[0] ?
  13114. y.domain([y.domain()[0] - y.domain()[0] * 0.01, y.domain()[1] + y.domain()[1] * 0.01])
  13115. : y.domain([-1,1]);
  13116. if ( isNaN(x.domain()[0])) {
  13117. x.domain([-1,1]);
  13118. }
  13119. if ( isNaN(y.domain()[0])) {
  13120. y.domain([-1,1]);
  13121. }
  13122. x0 = x0 || x;
  13123. y0 = y0 || y;
  13124. z0 = z0 || z;
  13125. var scaleDiff = x(1) !== x0(1) || y(1) !== y0(1) || z(1) !== z0(1);
  13126. width0 = width0 || width;
  13127. height0 = height0 || height;
  13128. var sizeDiff = width0 !== width || height0 !== height;
  13129. // Domain Diffs
  13130. xDom = xDom || [];
  13131. var domainDiff = xDom[0] !== x.domain()[0] || xDom[1] !== x.domain()[1];
  13132. xDom = x.domain();
  13133. yDom = yDom || [];
  13134. domainDiff = domainDiff || yDom[0] !== y.domain()[0] || yDom[1] !== y.domain()[1];
  13135. yDom = y.domain();
  13136. // Setup containers and skeleton of chart
  13137. var wrap = container.selectAll('g.nv-wrap.nv-scatter').data([data]);
  13138. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-scatter nv-chart-' + id);
  13139. var defsEnter = wrapEnter.append('defs');
  13140. var gEnter = wrapEnter.append('g');
  13141. var g = wrap.select('g');
  13142. wrap.classed('nv-single-point', singlePoint);
  13143. gEnter.append('g').attr('class', 'nv-groups');
  13144. gEnter.append('g').attr('class', 'nv-point-paths');
  13145. wrapEnter.append('g').attr('class', 'nv-point-clips');
  13146. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  13147. defsEnter.append('clipPath')
  13148. .attr('id', 'nv-edge-clip-' + id)
  13149. .append('rect')
  13150. .attr('transform', 'translate( -10, -10)');
  13151. wrap.select('#nv-edge-clip-' + id + ' rect')
  13152. .attr('width', availableWidth + 20)
  13153. .attr('height', (availableHeight > 0) ? availableHeight + 20 : 0);
  13154. g.attr('clip-path', clipEdge ? 'url(#nv-edge-clip-' + id + ')' : '');
  13155. function updateInteractiveLayer() {
  13156. // Always clear needs-update flag regardless of whether or not
  13157. // we will actually do anything (avoids needless invocations).
  13158. needsUpdate = false;
  13159. if (!interactive) return false;
  13160. container.selectAll(".nv-point.hover").classed("hover", false);
  13161. // nuke all voronoi paths
  13162. wrap.select('.nv-point-paths').selectAll('path').remove();
  13163. // inject series and point index for reference into voronoi
  13164. if (useVoronoi === true) {
  13165. var vertices = d3.merge(data.map(function(group, groupIndex) {
  13166. return group.values
  13167. .map(function(point, pointIndex) {
  13168. // *Adding noise to make duplicates very unlikely
  13169. // *Injecting series and point index for reference
  13170. // *Adding a 'jitter' to the points, because there's an issue in d3.geom.voronoi.
  13171. var pX = getX(point,pointIndex);
  13172. var pY = getY(point,pointIndex);
  13173. return [nv.utils.NaNtoZero(x(pX)) + Math.random() * 1e-4,
  13174. nv.utils.NaNtoZero(y(pY)) + Math.random() * 1e-4,
  13175. groupIndex,
  13176. pointIndex, point];
  13177. })
  13178. .filter(function(pointArray, pointIndex) {
  13179. return pointActive(pointArray[4], pointIndex); // Issue #237.. move filter to after map, so pointIndex is correct!
  13180. })
  13181. })
  13182. );
  13183. if (vertices.length == 0) return false; // No active points, we're done
  13184. if (vertices.length < 3) {
  13185. // Issue #283 - Adding 2 dummy points to the voronoi b/c voronoi requires min 3 points to work
  13186. vertices.push([x.range()[0] - 20, y.range()[0] - 20, null, null]);
  13187. vertices.push([x.range()[1] + 20, y.range()[1] + 20, null, null]);
  13188. vertices.push([x.range()[0] - 20, y.range()[0] + 20, null, null]);
  13189. vertices.push([x.range()[1] + 20, y.range()[1] - 20, null, null]);
  13190. }
  13191. // keep voronoi sections from going more than 10 outside of graph
  13192. // to avoid overlap with other things like legend etc
  13193. var bounds = d3.geom.polygon([
  13194. [-10,-10],
  13195. [-10,height + 10],
  13196. [width + 10,height + 10],
  13197. [width + 10,-10]
  13198. ]);
  13199. // delete duplicates from vertices - essential assumption for d3.geom.voronoi
  13200. var epsilon = 1e-4; // Uses 1e-4 to determine equivalence.
  13201. vertices = vertices.sort(function(a,b){return ((a[0] - b[0]) || (a[1] - b[1]))});
  13202. for (var i = 0; i < vertices.length - 1; ) {
  13203. if ((Math.abs(vertices[i][0] - vertices[i+1][0]) < epsilon) &&
  13204. (Math.abs(vertices[i][1] - vertices[i+1][1]) < epsilon)) {
  13205. vertices.splice(i+1, 1);
  13206. } else {
  13207. i++;
  13208. }
  13209. }
  13210. var voronoi = d3.geom.voronoi(vertices).map(function(d, i) {
  13211. if (d.length === 0) {
  13212. return null;
  13213. }
  13214. return {
  13215. 'data': bounds.clip(d),
  13216. 'series': vertices[i][2],
  13217. 'point': vertices[i][3]
  13218. }
  13219. });
  13220. var pointPaths = wrap.select('.nv-point-paths').selectAll('path').data(voronoi);
  13221. var vPointPaths = pointPaths
  13222. .enter().append("svg:path")
  13223. .attr("d", function(d) {
  13224. if (!d || !d.data || d.data.length === 0)
  13225. return 'M 0 0';
  13226. else
  13227. return "M" + d.data.join(",") + "Z";
  13228. })
  13229. .attr("id", function(d,i) {
  13230. return "nv-path-"+i; })
  13231. .attr("clip-path", function(d,i) { return "url(#nv-clip-"+id+"-"+i+")"; })
  13232. ;
  13233. // good for debugging point hover issues
  13234. if (showVoronoi) {
  13235. vPointPaths.style("fill", d3.rgb(230, 230, 230))
  13236. .style('fill-opacity', 0.4)
  13237. .style('stroke-opacity', 1)
  13238. .style("stroke", d3.rgb(200,200,200));
  13239. }
  13240. if (clipVoronoi) {
  13241. // voronoi sections are already set to clip,
  13242. // just create the circles with the IDs they expect
  13243. wrap.select('.nv-point-clips').selectAll('*').remove(); // must do * since it has sub-dom
  13244. var pointClips = wrap.select('.nv-point-clips').selectAll('clipPath').data(vertices);
  13245. var vPointClips = pointClips
  13246. .enter().append("svg:clipPath")
  13247. .attr("id", function(d, i) { return "nv-clip-"+id+"-"+i;})
  13248. .append("svg:circle")
  13249. .attr('cx', function(d) { return d[0]; })
  13250. .attr('cy', function(d) { return d[1]; })
  13251. .attr('r', clipRadius);
  13252. }
  13253. var mouseEventCallback = function(el, d, mDispatch) {
  13254. if (needsUpdate) return 0;
  13255. var series = data[d.series];
  13256. if (series === undefined) return;
  13257. var point = series.values[d.point];
  13258. point['color'] = color(series, d.series);
  13259. // standardize attributes for tooltip.
  13260. point['x'] = getX(point);
  13261. point['y'] = getY(point);
  13262. // can't just get box of event node since it's actually a voronoi polygon
  13263. var box = container.node().getBoundingClientRect();
  13264. var scrollTop = window.pageYOffset || document.documentElement.scrollTop;
  13265. var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft;
  13266. var pos = {
  13267. left: x(getX(point, d.point)) + box.left + scrollLeft + margin.left + 10,
  13268. top: y(getY(point, d.point)) + box.top + scrollTop + margin.top + 10
  13269. };
  13270. mDispatch({
  13271. point: point,
  13272. series: series,
  13273. pos: pos,
  13274. relativePos: [x(getX(point, d.point)) + margin.left, y(getY(point, d.point)) + margin.top],
  13275. seriesIndex: d.series,
  13276. pointIndex: d.point,
  13277. event: d3.event,
  13278. element: el
  13279. });
  13280. };
  13281. pointPaths
  13282. .on('click', function(d) {
  13283. mouseEventCallback(this, d, dispatch.elementClick);
  13284. })
  13285. .on('dblclick', function(d) {
  13286. mouseEventCallback(this, d, dispatch.elementDblClick);
  13287. })
  13288. .on('mouseover', function(d) {
  13289. mouseEventCallback(this, d, dispatch.elementMouseover);
  13290. })
  13291. .on('mouseout', function(d, i) {
  13292. mouseEventCallback(this, d, dispatch.elementMouseout);
  13293. });
  13294. } else {
  13295. // add event handlers to points instead voronoi paths
  13296. wrap.select('.nv-groups').selectAll('.nv-group')
  13297. .selectAll('.nv-point')
  13298. //.data(dataWithPoints)
  13299. //.style('pointer-events', 'auto') // recativate events, disabled by css
  13300. .on('click', function(d,i) {
  13301. //nv.log('test', d, i);
  13302. if (needsUpdate || !data[d[0].series]) return 0; //check if this is a dummy point
  13303. var series = data[d[0].series],
  13304. point = series.values[i];
  13305. var element = this;
  13306. dispatch.elementClick({
  13307. point: point,
  13308. series: series,
  13309. pos: [x(getX(point, i)) + margin.left, y(getY(point, i)) + margin.top], //TODO: make this pos base on the page
  13310. relativePos: [x(getX(point, i)) + margin.left, y(getY(point, i)) + margin.top],
  13311. seriesIndex: d[0].series,
  13312. pointIndex: i,
  13313. event: d3.event,
  13314. element: element
  13315. });
  13316. })
  13317. .on('dblclick', function(d,i) {
  13318. if (needsUpdate || !data[d[0].series]) return 0; //check if this is a dummy point
  13319. var series = data[d[0].series],
  13320. point = series.values[i];
  13321. dispatch.elementDblClick({
  13322. point: point,
  13323. series: series,
  13324. pos: [x(getX(point, i)) + margin.left, y(getY(point, i)) + margin.top],//TODO: make this pos base on the page
  13325. relativePos: [x(getX(point, i)) + margin.left, y(getY(point, i)) + margin.top],
  13326. seriesIndex: d[0].series,
  13327. pointIndex: i
  13328. });
  13329. })
  13330. .on('mouseover', function(d,i) {
  13331. if (needsUpdate || !data[d[0].series]) return 0; //check if this is a dummy point
  13332. var series = data[d[0].series],
  13333. point = series.values[i];
  13334. dispatch.elementMouseover({
  13335. point: point,
  13336. series: series,
  13337. pos: [x(getX(point, i)) + margin.left, y(getY(point, i)) + margin.top],//TODO: make this pos base on the page
  13338. relativePos: [x(getX(point, i)) + margin.left, y(getY(point, i)) + margin.top],
  13339. seriesIndex: d[0].series,
  13340. pointIndex: i,
  13341. color: color(d[0], i)
  13342. });
  13343. })
  13344. .on('mouseout', function(d,i) {
  13345. if (needsUpdate || !data[d[0].series]) return 0; //check if this is a dummy point
  13346. var series = data[d[0].series],
  13347. point = series.values[i];
  13348. dispatch.elementMouseout({
  13349. point: point,
  13350. series: series,
  13351. pos: [x(getX(point, i)) + margin.left, y(getY(point, i)) + margin.top],//TODO: make this pos base on the page
  13352. relativePos: [x(getX(point, i)) + margin.left, y(getY(point, i)) + margin.top],
  13353. seriesIndex: d[0].series,
  13354. pointIndex: i,
  13355. color: color(d[0], i)
  13356. });
  13357. });
  13358. }
  13359. }
  13360. needsUpdate = true;
  13361. var groups = wrap.select('.nv-groups').selectAll('.nv-group')
  13362. .data(function(d) { return d }, function(d) { return d.key });
  13363. groups.enter().append('g')
  13364. .style('stroke-opacity', 1e-6)
  13365. .style('fill-opacity', 1e-6);
  13366. groups.exit()
  13367. .remove();
  13368. groups
  13369. .attr('class', function(d,i) {
  13370. return (d.classed || '') + ' nv-group nv-series-' + i;
  13371. })
  13372. .classed('nv-noninteractive', !interactive)
  13373. .classed('hover', function(d) { return d.hover });
  13374. groups.watchTransition(renderWatch, 'scatter: groups')
  13375. .style('fill', function(d,i) { return color(d, i) })
  13376. .style('stroke', function(d,i) { return d.pointBorderColor || pointBorderColor || color(d, i) })
  13377. .style('stroke-opacity', 1)
  13378. .style('fill-opacity', .5);
  13379. // create the points, maintaining their IDs from the original data set
  13380. var points = groups.selectAll('path.nv-point')
  13381. .data(function(d) {
  13382. return d.values.map(
  13383. function (point, pointIndex) {
  13384. return [point, pointIndex]
  13385. }).filter(
  13386. function(pointArray, pointIndex) {
  13387. return pointActive(pointArray[0], pointIndex)
  13388. })
  13389. });
  13390. points.enter().append('path')
  13391. .attr('class', function (d) {
  13392. return 'nv-point nv-point-' + d[1];
  13393. })
  13394. .style('fill', function (d) { return d.color })
  13395. .style('stroke', function (d) { return d.color })
  13396. .attr('transform', function(d) {
  13397. return 'translate(' + nv.utils.NaNtoZero(x0(getX(d[0],d[1]))) + ',' + nv.utils.NaNtoZero(y0(getY(d[0],d[1]))) + ')'
  13398. })
  13399. .attr('d',
  13400. nv.utils.symbol()
  13401. .type(function(d) { return getShape(d[0]); })
  13402. .size(function(d) { return z(getSize(d[0],d[1])) })
  13403. );
  13404. points.exit().each(delCache).remove();
  13405. groups.exit().selectAll('path.nv-point')
  13406. .watchTransition(renderWatch, 'scatter exit')
  13407. .attr('transform', function(d) {
  13408. return 'translate(' + nv.utils.NaNtoZero(x(getX(d[0],d[1]))) + ',' + nv.utils.NaNtoZero(y(getY(d[0],d[1]))) + ')'
  13409. })
  13410. .remove();
  13411. //============================================================
  13412. // Point Update Optimisation Notes
  13413. //------------------------------------------------------------
  13414. // The following update selections are filtered with getDiffs
  13415. // (defined at the top of this file) this brings a performance
  13416. // benefit for charts with large data sets that accumulate a
  13417. // subset of changes or additions over time.
  13418. //
  13419. // Uneccesary and expensive DOM calls are avoided by culling
  13420. // unchanged points from the selection in exchange for the
  13421. // cheaper overhead of caching and diffing each point first.
  13422. //
  13423. // Due to the way D3 and NVD3 work, other global changes need
  13424. // to be considered in addition to local point properties.
  13425. // This is a potential source of bugs (if any of the global
  13426. // changes that possibly affect points are missed).
  13427. // Update Point Positions [x, y]
  13428. points.filter(function (d) {
  13429. // getDiffs must always be called to update cache
  13430. return getDiffs(d, 'x', getX, 'y', getY) ||
  13431. scaleDiff || sizeDiff || domainDiff;
  13432. })
  13433. .watchTransition(renderWatch, 'scatter points')
  13434. .attr('transform', function (d) {
  13435. return 'translate(' +
  13436. nv.utils.NaNtoZero(x(getX(d[0], d[1]))) + ',' +
  13437. nv.utils.NaNtoZero(y(getY(d[0], d[1]))) + ')'
  13438. });
  13439. // Update Point Appearance [shape, size]
  13440. points.filter(function (d) {
  13441. // getDiffs must always be called to update cache
  13442. return getDiffs(d, 'shape', getShape, 'size', getSize) ||
  13443. scaleDiff || sizeDiff || domainDiff;
  13444. })
  13445. .watchTransition(renderWatch, 'scatter points')
  13446. .attr('d', nv.utils.symbol()
  13447. .type(function (d) { return getShape(d[0]) })
  13448. .size(function (d) { return z(getSize(d[0], d[1])) })
  13449. );
  13450. // add label a label to scatter chart
  13451. if(showLabels)
  13452. {
  13453. var titles = groups.selectAll('.nv-label')
  13454. .data(function(d) {
  13455. return d.values.map(
  13456. function (point, pointIndex) {
  13457. return [point, pointIndex]
  13458. }).filter(
  13459. function(pointArray, pointIndex) {
  13460. return pointActive(pointArray[0], pointIndex)
  13461. })
  13462. });
  13463. titles.enter().append('text')
  13464. .style('fill', function (d,i) {
  13465. return d.color })
  13466. .style('stroke-opacity', 0)
  13467. .style('fill-opacity', 1)
  13468. .attr('transform', function(d) {
  13469. var dx = nv.utils.NaNtoZero(x0(getX(d[0],d[1]))) + Math.sqrt(z(getSize(d[0],d[1]))/Math.PI) + 2;
  13470. return 'translate(' + dx + ',' + nv.utils.NaNtoZero(y0(getY(d[0],d[1]))) + ')';
  13471. })
  13472. .text(function(d,i){
  13473. return d[0].label;});
  13474. titles.exit().remove();
  13475. groups.exit().selectAll('path.nv-label')
  13476. .watchTransition(renderWatch, 'scatter exit')
  13477. .attr('transform', function(d) {
  13478. var dx = nv.utils.NaNtoZero(x(getX(d[0],d[1])))+ Math.sqrt(z(getSize(d[0],d[1]))/Math.PI)+2;
  13479. return 'translate(' + dx + ',' + nv.utils.NaNtoZero(y(getY(d[0],d[1]))) + ')';
  13480. })
  13481. .remove();
  13482. titles.each(function(d) {
  13483. d3.select(this)
  13484. .classed('nv-label', true)
  13485. .classed('nv-label-' + d[1], false)
  13486. .classed('hover',false);
  13487. });
  13488. titles.watchTransition(renderWatch, 'scatter labels')
  13489. .text(function(d,i){
  13490. return d[0].label;})
  13491. .attr('transform', function(d) {
  13492. var dx = nv.utils.NaNtoZero(x(getX(d[0],d[1])))+ Math.sqrt(z(getSize(d[0],d[1]))/Math.PI)+2;
  13493. return 'translate(' + dx + ',' + nv.utils.NaNtoZero(y(getY(d[0],d[1]))) + ')'
  13494. });
  13495. }
  13496. // Delay updating the invisible interactive layer for smoother animation
  13497. if( interactiveUpdateDelay )
  13498. {
  13499. clearTimeout(timeoutID); // stop repeat calls to updateInteractiveLayer
  13500. timeoutID = setTimeout(updateInteractiveLayer, interactiveUpdateDelay );
  13501. }
  13502. else
  13503. {
  13504. updateInteractiveLayer();
  13505. }
  13506. //store old scales for use in transitions on update
  13507. x0 = x.copy();
  13508. y0 = y.copy();
  13509. z0 = z.copy();
  13510. width0 = width;
  13511. height0 = height;
  13512. });
  13513. renderWatch.renderEnd('scatter immediate');
  13514. return chart;
  13515. }
  13516. //============================================================
  13517. // Expose Public Variables
  13518. //------------------------------------------------------------
  13519. chart.dispatch = dispatch;
  13520. chart.options = nv.utils.optionsFunc.bind(chart);
  13521. // utility function calls provided by this chart
  13522. chart._calls = new function() {
  13523. this.clearHighlights = function () {
  13524. nv.dom.write(function() {
  13525. container.selectAll(".nv-point.hover").classed("hover", false);
  13526. });
  13527. return null;
  13528. };
  13529. this.highlightPoint = function (seriesIndex, pointIndex, isHoverOver) {
  13530. nv.dom.write(function() {
  13531. container.select('.nv-groups')
  13532. .selectAll(".nv-series-" + seriesIndex)
  13533. .selectAll(".nv-point-" + pointIndex)
  13534. .classed("hover", isHoverOver);
  13535. });
  13536. };
  13537. };
  13538. // trigger calls from events too
  13539. dispatch.on('elementMouseover.point', function(d) {
  13540. if (interactive) chart._calls.highlightPoint(d.seriesIndex,d.pointIndex,true);
  13541. });
  13542. dispatch.on('elementMouseout.point', function(d) {
  13543. if (interactive) chart._calls.highlightPoint(d.seriesIndex,d.pointIndex,false);
  13544. });
  13545. chart._options = Object.create({}, {
  13546. // simple options, just get/set the necessary values
  13547. width: {get: function(){return width;}, set: function(_){width=_;}},
  13548. height: {get: function(){return height;}, set: function(_){height=_;}},
  13549. xScale: {get: function(){return x;}, set: function(_){x=_;}},
  13550. yScale: {get: function(){return y;}, set: function(_){y=_;}},
  13551. pointScale: {get: function(){return z;}, set: function(_){z=_;}},
  13552. xDomain: {get: function(){return xDomain;}, set: function(_){xDomain=_;}},
  13553. yDomain: {get: function(){return yDomain;}, set: function(_){yDomain=_;}},
  13554. pointDomain: {get: function(){return sizeDomain;}, set: function(_){sizeDomain=_;}},
  13555. xRange: {get: function(){return xRange;}, set: function(_){xRange=_;}},
  13556. yRange: {get: function(){return yRange;}, set: function(_){yRange=_;}},
  13557. pointRange: {get: function(){return sizeRange;}, set: function(_){sizeRange=_;}},
  13558. forceX: {get: function(){return forceX;}, set: function(_){forceX=_;}},
  13559. forceY: {get: function(){return forceY;}, set: function(_){forceY=_;}},
  13560. forcePoint: {get: function(){return forceSize;}, set: function(_){forceSize=_;}},
  13561. interactive: {get: function(){return interactive;}, set: function(_){interactive=_;}},
  13562. pointActive: {get: function(){return pointActive;}, set: function(_){pointActive=_;}},
  13563. padDataOuter: {get: function(){return padDataOuter;}, set: function(_){padDataOuter=_;}},
  13564. padData: {get: function(){return padData;}, set: function(_){padData=_;}},
  13565. clipEdge: {get: function(){return clipEdge;}, set: function(_){clipEdge=_;}},
  13566. clipVoronoi: {get: function(){return clipVoronoi;}, set: function(_){clipVoronoi=_;}},
  13567. clipRadius: {get: function(){return clipRadius;}, set: function(_){clipRadius=_;}},
  13568. showVoronoi: {get: function(){return showVoronoi;}, set: function(_){showVoronoi=_;}},
  13569. id: {get: function(){return id;}, set: function(_){id=_;}},
  13570. interactiveUpdateDelay: {get:function(){return interactiveUpdateDelay;}, set: function(_){interactiveUpdateDelay=_;}},
  13571. showLabels: {get: function(){return showLabels;}, set: function(_){ showLabels = _;}},
  13572. pointBorderColor: {get: function(){return pointBorderColor;}, set: function(_){pointBorderColor=_;}},
  13573. // simple functor options
  13574. x: {get: function(){return getX;}, set: function(_){getX = d3.functor(_);}},
  13575. y: {get: function(){return getY;}, set: function(_){getY = d3.functor(_);}},
  13576. pointSize: {get: function(){return getSize;}, set: function(_){getSize = d3.functor(_);}},
  13577. pointShape: {get: function(){return getShape;}, set: function(_){getShape = d3.functor(_);}},
  13578. // options that require extra logic in the setter
  13579. margin: {get: function(){return margin;}, set: function(_){
  13580. margin.top = _.top !== undefined ? _.top : margin.top;
  13581. margin.right = _.right !== undefined ? _.right : margin.right;
  13582. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  13583. margin.left = _.left !== undefined ? _.left : margin.left;
  13584. }},
  13585. duration: {get: function(){return duration;}, set: function(_){
  13586. duration = _;
  13587. renderWatch.reset(duration);
  13588. }},
  13589. color: {get: function(){return color;}, set: function(_){
  13590. color = nv.utils.getColor(_);
  13591. }},
  13592. useVoronoi: {get: function(){return useVoronoi;}, set: function(_){
  13593. useVoronoi = _;
  13594. if (useVoronoi === false) {
  13595. clipVoronoi = false;
  13596. }
  13597. }}
  13598. });
  13599. nv.utils.initOptions(chart);
  13600. return chart;
  13601. };
  13602. nv.models.scatterChart = function() {
  13603. "use strict";
  13604. //============================================================
  13605. // Public Variables with Default Settings
  13606. //------------------------------------------------------------
  13607. var scatter = nv.models.scatter()
  13608. , xAxis = nv.models.axis()
  13609. , yAxis = nv.models.axis()
  13610. , legend = nv.models.legend()
  13611. , distX = nv.models.distribution()
  13612. , distY = nv.models.distribution()
  13613. , tooltip = nv.models.tooltip()
  13614. ;
  13615. var margin = {top: 30, right: 20, bottom: 50, left: 75}
  13616. , marginTop = null
  13617. , width = null
  13618. , height = null
  13619. , container = null
  13620. , color = nv.utils.defaultColor()
  13621. , x = scatter.xScale()
  13622. , y = scatter.yScale()
  13623. , showDistX = false
  13624. , showDistY = false
  13625. , showLegend = true
  13626. , showXAxis = true
  13627. , showYAxis = true
  13628. , rightAlignYAxis = false
  13629. , state = nv.utils.state()
  13630. , defaultState = null
  13631. , dispatch = d3.dispatch('stateChange', 'changeState', 'renderEnd')
  13632. , noData = null
  13633. , duration = 250
  13634. , showLabels = false
  13635. ;
  13636. scatter.xScale(x).yScale(y);
  13637. xAxis.orient('bottom').tickPadding(10);
  13638. yAxis
  13639. .orient((rightAlignYAxis) ? 'right' : 'left')
  13640. .tickPadding(10)
  13641. ;
  13642. distX.axis('x');
  13643. distY.axis('y');
  13644. tooltip
  13645. .headerFormatter(function(d, i) {
  13646. return xAxis.tickFormat()(d, i);
  13647. })
  13648. .valueFormatter(function(d, i) {
  13649. return yAxis.tickFormat()(d, i);
  13650. });
  13651. //============================================================
  13652. // Private Variables
  13653. //------------------------------------------------------------
  13654. var x0, y0
  13655. , renderWatch = nv.utils.renderWatch(dispatch, duration);
  13656. var stateGetter = function(data) {
  13657. return function(){
  13658. return {
  13659. active: data.map(function(d) { return !d.disabled })
  13660. };
  13661. }
  13662. };
  13663. var stateSetter = function(data) {
  13664. return function(state) {
  13665. if (state.active !== undefined)
  13666. data.forEach(function(series,i) {
  13667. series.disabled = !state.active[i];
  13668. });
  13669. }
  13670. };
  13671. function chart(selection) {
  13672. renderWatch.reset();
  13673. renderWatch.models(scatter);
  13674. if (showXAxis) renderWatch.models(xAxis);
  13675. if (showYAxis) renderWatch.models(yAxis);
  13676. if (showDistX) renderWatch.models(distX);
  13677. if (showDistY) renderWatch.models(distY);
  13678. selection.each(function(data) {
  13679. var that = this;
  13680. container = d3.select(this);
  13681. nv.utils.initSVG(container);
  13682. var availableWidth = nv.utils.availableWidth(width, container, margin),
  13683. availableHeight = nv.utils.availableHeight(height, container, margin);
  13684. chart.update = function() {
  13685. if (duration === 0)
  13686. container.call(chart);
  13687. else
  13688. container.transition().duration(duration).call(chart);
  13689. };
  13690. chart.container = this;
  13691. state
  13692. .setter(stateSetter(data), chart.update)
  13693. .getter(stateGetter(data))
  13694. .update();
  13695. // DEPRECATED set state.disableddisabled
  13696. state.disabled = data.map(function(d) { return !!d.disabled });
  13697. if (!defaultState) {
  13698. var key;
  13699. defaultState = {};
  13700. for (key in state) {
  13701. if (state[key] instanceof Array)
  13702. defaultState[key] = state[key].slice(0);
  13703. else
  13704. defaultState[key] = state[key];
  13705. }
  13706. }
  13707. // Display noData message if there's nothing to show.
  13708. if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
  13709. nv.utils.noData(chart, container);
  13710. renderWatch.renderEnd('scatter immediate');
  13711. return chart;
  13712. } else {
  13713. container.selectAll('.nv-noData').remove();
  13714. }
  13715. // Setup Scales
  13716. x = scatter.xScale();
  13717. y = scatter.yScale();
  13718. // Setup containers and skeleton of chart
  13719. var wrap = container.selectAll('g.nv-wrap.nv-scatterChart').data([data]);
  13720. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-scatterChart nv-chart-' + scatter.id());
  13721. var gEnter = wrapEnter.append('g');
  13722. var g = wrap.select('g');
  13723. // background for pointer events
  13724. gEnter.append('rect').attr('class', 'nvd3 nv-background').style("pointer-events","none");
  13725. gEnter.append('g').attr('class', 'nv-x nv-axis');
  13726. gEnter.append('g').attr('class', 'nv-y nv-axis');
  13727. gEnter.append('g').attr('class', 'nv-scatterWrap');
  13728. gEnter.append('g').attr('class', 'nv-regressionLinesWrap');
  13729. gEnter.append('g').attr('class', 'nv-distWrap');
  13730. gEnter.append('g').attr('class', 'nv-legendWrap');
  13731. if (rightAlignYAxis) {
  13732. g.select(".nv-y.nv-axis")
  13733. .attr("transform", "translate(" + availableWidth + ",0)");
  13734. }
  13735. // Legend
  13736. if (!showLegend) {
  13737. g.select('.nv-legendWrap').selectAll('*').remove();
  13738. } else {
  13739. var legendWidth = availableWidth;
  13740. legend.width(legendWidth);
  13741. wrap.select('.nv-legendWrap')
  13742. .datum(data)
  13743. .call(legend);
  13744. if (!marginTop && legend.height() !== margin.top) {
  13745. margin.top = legend.height();
  13746. availableHeight = nv.utils.availableHeight(height, container, margin);
  13747. }
  13748. wrap.select('.nv-legendWrap')
  13749. .attr('transform', 'translate(0' + ',' + (-margin.top) +')');
  13750. }
  13751. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  13752. // Main Chart Component(s)
  13753. scatter
  13754. .width(availableWidth)
  13755. .height(availableHeight)
  13756. .color(data.map(function(d,i) {
  13757. d.color = d.color || color(d, i);
  13758. return d.color;
  13759. }).filter(function(d,i) { return !data[i].disabled }))
  13760. .showLabels(showLabels);
  13761. wrap.select('.nv-scatterWrap')
  13762. .datum(data.filter(function(d) { return !d.disabled }))
  13763. .call(scatter);
  13764. wrap.select('.nv-regressionLinesWrap')
  13765. .attr('clip-path', 'url(#nv-edge-clip-' + scatter.id() + ')');
  13766. var regWrap = wrap.select('.nv-regressionLinesWrap').selectAll('.nv-regLines')
  13767. .data(function (d) {
  13768. return d;
  13769. });
  13770. regWrap.enter().append('g').attr('class', 'nv-regLines');
  13771. var regLine = regWrap.selectAll('.nv-regLine')
  13772. .data(function (d) {
  13773. return [d]
  13774. });
  13775. regLine.enter()
  13776. .append('line').attr('class', 'nv-regLine')
  13777. .style('stroke-opacity', 0);
  13778. // don't add lines unless we have slope and intercept to use
  13779. regLine.filter(function(d) {
  13780. return d.intercept && d.slope;
  13781. })
  13782. .watchTransition(renderWatch, 'scatterPlusLineChart: regline')
  13783. .attr('x1', x.range()[0])
  13784. .attr('x2', x.range()[1])
  13785. .attr('y1', function (d, i) {
  13786. return y(x.domain()[0] * d.slope + d.intercept)
  13787. })
  13788. .attr('y2', function (d, i) {
  13789. return y(x.domain()[1] * d.slope + d.intercept)
  13790. })
  13791. .style('stroke', function (d, i, j) {
  13792. return color(d, j)
  13793. })
  13794. .style('stroke-opacity', function (d, i) {
  13795. return (d.disabled || typeof d.slope === 'undefined' || typeof d.intercept === 'undefined') ? 0 : 1
  13796. });
  13797. // Setup Axes
  13798. if (showXAxis) {
  13799. xAxis
  13800. .scale(x)
  13801. ._ticks( nv.utils.calcTicksX(availableWidth/100, data) )
  13802. .tickSize( -availableHeight , 0);
  13803. g.select('.nv-x.nv-axis')
  13804. .attr('transform', 'translate(0,' + y.range()[0] + ')')
  13805. .call(xAxis);
  13806. }
  13807. if (showYAxis) {
  13808. yAxis
  13809. .scale(y)
  13810. ._ticks( nv.utils.calcTicksY(availableHeight/36, data) )
  13811. .tickSize( -availableWidth, 0);
  13812. g.select('.nv-y.nv-axis')
  13813. .call(yAxis);
  13814. }
  13815. // Setup Distribution
  13816. distX
  13817. .getData(scatter.x())
  13818. .scale(x)
  13819. .width(availableWidth)
  13820. .color(data.map(function(d,i) {
  13821. return d.color || color(d, i);
  13822. }).filter(function(d,i) { return !data[i].disabled }));
  13823. gEnter.select('.nv-distWrap').append('g')
  13824. .attr('class', 'nv-distributionX');
  13825. g.select('.nv-distributionX')
  13826. .attr('transform', 'translate(0,' + y.range()[0] + ')')
  13827. .datum(data.filter(function(d) { return !d.disabled }))
  13828. .call(distX)
  13829. .style('opacity', function() { return showDistX ? '1' : '1e-6'; })
  13830. .watchTransition(renderWatch, 'scatterPlusLineChart')
  13831. .style('opacity', function() { return showDistX ? '1' : '1e-6'; })
  13832. distY
  13833. .getData(scatter.y())
  13834. .scale(y)
  13835. .width(availableHeight)
  13836. .color(data.map(function(d,i) {
  13837. return d.color || color(d, i);
  13838. }).filter(function(d,i) { return !data[i].disabled }));
  13839. gEnter.select('.nv-distWrap').append('g')
  13840. .attr('class', 'nv-distributionY');
  13841. g.select('.nv-distributionY')
  13842. .attr('transform', 'translate(' + (rightAlignYAxis ? availableWidth : -distY.size() ) + ',0)')
  13843. .datum(data.filter(function(d) { return !d.disabled }))
  13844. .call(distY)
  13845. .style('opacity', function() { return showDistY ? '1' : '1e-6'; })
  13846. .watchTransition(renderWatch, 'scatterPlusLineChart')
  13847. .style('opacity', function() { return showDistY ? '1' : '1e-6'; })
  13848. //============================================================
  13849. // Event Handling/Dispatching (in chart's scope)
  13850. //------------------------------------------------------------
  13851. legend.dispatch.on('stateChange', function(newState) {
  13852. for (var key in newState)
  13853. state[key] = newState[key];
  13854. dispatch.stateChange(state);
  13855. chart.update();
  13856. });
  13857. // Update chart from a state object passed to event handler
  13858. dispatch.on('changeState', function(e) {
  13859. if (typeof e.disabled !== 'undefined') {
  13860. data.forEach(function(series,i) {
  13861. series.disabled = e.disabled[i];
  13862. });
  13863. state.disabled = e.disabled;
  13864. }
  13865. chart.update();
  13866. });
  13867. // mouseover needs availableHeight so we just keep scatter mouse events inside the chart block
  13868. scatter.dispatch.on('elementMouseout.tooltip', function(evt) {
  13869. tooltip.hidden(true);
  13870. container.select('.nv-chart-' + scatter.id() + ' .nv-series-' + evt.seriesIndex + ' .nv-distx-' + evt.pointIndex)
  13871. .attr('y1', 0);
  13872. container.select('.nv-chart-' + scatter.id() + ' .nv-series-' + evt.seriesIndex + ' .nv-disty-' + evt.pointIndex)
  13873. .attr('x2', distY.size());
  13874. });
  13875. scatter.dispatch.on('elementMouseover.tooltip', function(evt) {
  13876. container.select('.nv-series-' + evt.seriesIndex + ' .nv-distx-' + evt.pointIndex)
  13877. .attr('y1', evt.relativePos[1] - availableHeight);
  13878. container.select('.nv-series-' + evt.seriesIndex + ' .nv-disty-' + evt.pointIndex)
  13879. .attr('x2', evt.relativePos[0] + distX.size());
  13880. tooltip.data(evt).hidden(false);
  13881. });
  13882. //store old scales for use in transitions on update
  13883. x0 = x.copy();
  13884. y0 = y.copy();
  13885. });
  13886. renderWatch.renderEnd('scatter with line immediate');
  13887. return chart;
  13888. }
  13889. //============================================================
  13890. // Expose Public Variables
  13891. //------------------------------------------------------------
  13892. // expose chart's sub-components
  13893. chart.dispatch = dispatch;
  13894. chart.scatter = scatter;
  13895. chart.legend = legend;
  13896. chart.xAxis = xAxis;
  13897. chart.yAxis = yAxis;
  13898. chart.distX = distX;
  13899. chart.distY = distY;
  13900. chart.tooltip = tooltip;
  13901. chart.options = nv.utils.optionsFunc.bind(chart);
  13902. chart._options = Object.create({}, {
  13903. // simple options, just get/set the necessary values
  13904. width: {get: function(){return width;}, set: function(_){width=_;}},
  13905. height: {get: function(){return height;}, set: function(_){height=_;}},
  13906. container: {get: function(){return container;}, set: function(_){container=_;}},
  13907. showDistX: {get: function(){return showDistX;}, set: function(_){showDistX=_;}},
  13908. showDistY: {get: function(){return showDistY;}, set: function(_){showDistY=_;}},
  13909. showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
  13910. showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}},
  13911. showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}},
  13912. defaultState: {get: function(){return defaultState;}, set: function(_){defaultState=_;}},
  13913. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  13914. duration: {get: function(){return duration;}, set: function(_){duration=_;}},
  13915. showLabels: {get: function(){return showLabels;}, set: function(_){showLabels=_;}},
  13916. // options that require extra logic in the setter
  13917. margin: {get: function(){return margin;}, set: function(_){
  13918. if (_.top !== undefined) {
  13919. margin.top = _.top;
  13920. marginTop = _.top;
  13921. }
  13922. margin.right = _.right !== undefined ? _.right : margin.right;
  13923. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  13924. margin.left = _.left !== undefined ? _.left : margin.left;
  13925. }},
  13926. rightAlignYAxis: {get: function(){return rightAlignYAxis;}, set: function(_){
  13927. rightAlignYAxis = _;
  13928. yAxis.orient( (_) ? 'right' : 'left');
  13929. }},
  13930. color: {get: function(){return color;}, set: function(_){
  13931. color = nv.utils.getColor(_);
  13932. legend.color(color);
  13933. distX.color(color);
  13934. distY.color(color);
  13935. }}
  13936. });
  13937. nv.utils.inheritOptions(chart, scatter);
  13938. nv.utils.initOptions(chart);
  13939. return chart;
  13940. };
  13941. nv.models.sparkline = function() {
  13942. "use strict";
  13943. //============================================================
  13944. // Public Variables with Default Settings
  13945. //------------------------------------------------------------
  13946. var margin = {top: 2, right: 0, bottom: 2, left: 0}
  13947. , width = 400
  13948. , height = 32
  13949. , container = null
  13950. , animate = true
  13951. , x = d3.scale.linear()
  13952. , y = d3.scale.linear()
  13953. , getX = function(d) { return d.x }
  13954. , getY = function(d) { return d.y }
  13955. , color = nv.utils.getColor(['#000'])
  13956. , xDomain
  13957. , yDomain
  13958. , xRange
  13959. , yRange
  13960. , showMinMaxPoints = true
  13961. , showCurrentPoint = true
  13962. , dispatch = d3.dispatch('renderEnd')
  13963. ;
  13964. //============================================================
  13965. // Private Variables
  13966. //------------------------------------------------------------
  13967. var renderWatch = nv.utils.renderWatch(dispatch);
  13968. function chart(selection) {
  13969. renderWatch.reset();
  13970. selection.each(function(data) {
  13971. var availableWidth = width - margin.left - margin.right,
  13972. availableHeight = height - margin.top - margin.bottom;
  13973. container = d3.select(this);
  13974. nv.utils.initSVG(container);
  13975. // Setup Scales
  13976. x .domain(xDomain || d3.extent(data, getX ))
  13977. .range(xRange || [0, availableWidth]);
  13978. y .domain(yDomain || d3.extent(data, getY ))
  13979. .range(yRange || [availableHeight, 0]);
  13980. // Setup containers and skeleton of chart
  13981. var wrap = container.selectAll('g.nv-wrap.nv-sparkline').data([data]);
  13982. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-sparkline');
  13983. var gEnter = wrapEnter.append('g');
  13984. var g = wrap.select('g');
  13985. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
  13986. var paths = wrap.selectAll('path')
  13987. .data(function(d) { return [d] });
  13988. paths.enter().append('path');
  13989. paths.exit().remove();
  13990. paths
  13991. .style('stroke', function(d,i) { return d.color || color(d, i) })
  13992. .attr('d', d3.svg.line()
  13993. .x(function(d,i) { return x(getX(d,i)) })
  13994. .y(function(d,i) { return y(getY(d,i)) })
  13995. );
  13996. // TODO: Add CURRENT data point (Need Min, Mac, Current / Most recent)
  13997. var points = wrap.selectAll('circle.nv-point')
  13998. .data(function(data) {
  13999. var yValues = data.map(function(d, i) { return getY(d,i); });
  14000. function pointIndex(index) {
  14001. if (index != -1) {
  14002. var result = data[index];
  14003. result.pointIndex = index;
  14004. return result;
  14005. } else {
  14006. return null;
  14007. }
  14008. }
  14009. var maxPoint = pointIndex(yValues.lastIndexOf(y.domain()[1])),
  14010. minPoint = pointIndex(yValues.indexOf(y.domain()[0])),
  14011. currentPoint = pointIndex(yValues.length - 1);
  14012. return [(showMinMaxPoints ? minPoint : null), (showMinMaxPoints ? maxPoint : null), (showCurrentPoint ? currentPoint : null)].filter(function (d) {return d != null;});
  14013. });
  14014. points.enter().append('circle');
  14015. points.exit().remove();
  14016. points
  14017. .attr('cx', function(d,i) { return x(getX(d,d.pointIndex)) })
  14018. .attr('cy', function(d,i) { return y(getY(d,d.pointIndex)) })
  14019. .attr('r', 2)
  14020. .attr('class', function(d,i) {
  14021. return getX(d, d.pointIndex) == x.domain()[1] ? 'nv-point nv-currentValue' :
  14022. getY(d, d.pointIndex) == y.domain()[0] ? 'nv-point nv-minValue' : 'nv-point nv-maxValue'
  14023. });
  14024. });
  14025. renderWatch.renderEnd('sparkline immediate');
  14026. return chart;
  14027. }
  14028. //============================================================
  14029. // Expose Public Variables
  14030. //------------------------------------------------------------
  14031. chart.options = nv.utils.optionsFunc.bind(chart);
  14032. chart._options = Object.create({}, {
  14033. // simple options, just get/set the necessary values
  14034. width: {get: function(){return width;}, set: function(_){width=_;}},
  14035. height: {get: function(){return height;}, set: function(_){height=_;}},
  14036. xDomain: {get: function(){return xDomain;}, set: function(_){xDomain=_;}},
  14037. yDomain: {get: function(){return yDomain;}, set: function(_){yDomain=_;}},
  14038. xRange: {get: function(){return xRange;}, set: function(_){xRange=_;}},
  14039. yRange: {get: function(){return yRange;}, set: function(_){yRange=_;}},
  14040. xScale: {get: function(){return x;}, set: function(_){x=_;}},
  14041. yScale: {get: function(){return y;}, set: function(_){y=_;}},
  14042. animate: {get: function(){return animate;}, set: function(_){animate=_;}},
  14043. showMinMaxPoints: {get: function(){return showMinMaxPoints;}, set: function(_){showMinMaxPoints=_;}},
  14044. showCurrentPoint: {get: function(){return showCurrentPoint;}, set: function(_){showCurrentPoint=_;}},
  14045. //functor options
  14046. x: {get: function(){return getX;}, set: function(_){getX=d3.functor(_);}},
  14047. y: {get: function(){return getY;}, set: function(_){getY=d3.functor(_);}},
  14048. // options that require extra logic in the setter
  14049. margin: {get: function(){return margin;}, set: function(_){
  14050. margin.top = _.top !== undefined ? _.top : margin.top;
  14051. margin.right = _.right !== undefined ? _.right : margin.right;
  14052. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  14053. margin.left = _.left !== undefined ? _.left : margin.left;
  14054. }},
  14055. color: {get: function(){return color;}, set: function(_){
  14056. color = nv.utils.getColor(_);
  14057. }}
  14058. });
  14059. chart.dispatch = dispatch;
  14060. nv.utils.initOptions(chart);
  14061. return chart;
  14062. };
  14063. nv.models.sparklinePlus = function() {
  14064. "use strict";
  14065. //============================================================
  14066. // Public Variables with Default Settings
  14067. //------------------------------------------------------------
  14068. var sparkline = nv.models.sparkline();
  14069. var margin = {top: 15, right: 100, bottom: 10, left: 50}
  14070. , width = null
  14071. , height = null
  14072. , x
  14073. , y
  14074. , index = []
  14075. , paused = false
  14076. , xTickFormat = d3.format(',r')
  14077. , yTickFormat = d3.format(',.2f')
  14078. , showLastValue = true
  14079. , alignValue = true
  14080. , rightAlignValue = false
  14081. , noData = null
  14082. , dispatch = d3.dispatch('renderEnd')
  14083. ;
  14084. //============================================================
  14085. // Private Variables
  14086. //------------------------------------------------------------
  14087. var renderWatch = nv.utils.renderWatch(dispatch);
  14088. function chart(selection) {
  14089. renderWatch.reset();
  14090. renderWatch.models(sparkline);
  14091. selection.each(function(data) {
  14092. var container = d3.select(this);
  14093. nv.utils.initSVG(container);
  14094. var availableWidth = nv.utils.availableWidth(width, container, margin),
  14095. availableHeight = nv.utils.availableHeight(height, container, margin);
  14096. chart.update = function() { container.call(chart); };
  14097. chart.container = this;
  14098. // Display No Data message if there's nothing to show.
  14099. if (!data || !data.length) {
  14100. nv.utils.noData(chart, container)
  14101. return chart;
  14102. } else {
  14103. container.selectAll('.nv-noData').remove();
  14104. }
  14105. var currentValue = sparkline.y()(data[data.length-1], data.length-1);
  14106. // Setup Scales
  14107. x = sparkline.xScale();
  14108. y = sparkline.yScale();
  14109. // Setup containers and skeleton of chart
  14110. var wrap = container.selectAll('g.nv-wrap.nv-sparklineplus').data([data]);
  14111. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-sparklineplus');
  14112. var gEnter = wrapEnter.append('g');
  14113. var g = wrap.select('g');
  14114. gEnter.append('g').attr('class', 'nv-sparklineWrap');
  14115. gEnter.append('g').attr('class', 'nv-valueWrap');
  14116. gEnter.append('g').attr('class', 'nv-hoverArea');
  14117. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  14118. // Main Chart Component(s)
  14119. var sparklineWrap = g.select('.nv-sparklineWrap');
  14120. sparkline.width(availableWidth).height(availableHeight);
  14121. sparklineWrap.call(sparkline);
  14122. if (showLastValue) {
  14123. var valueWrap = g.select('.nv-valueWrap');
  14124. var value = valueWrap.selectAll('.nv-currentValue')
  14125. .data([currentValue]);
  14126. value.enter().append('text').attr('class', 'nv-currentValue')
  14127. .attr('dx', rightAlignValue ? -8 : 8)
  14128. .attr('dy', '.40em')
  14129. .style('text-anchor', rightAlignValue ? 'end' : 'start');
  14130. value
  14131. .attr('x', availableWidth + (rightAlignValue ? margin.right : 0))
  14132. .attr('y', alignValue ? function (d) {
  14133. return y(d)
  14134. } : 0)
  14135. .style('fill', sparkline.color()(data[data.length - 1], data.length - 1))
  14136. .text(yTickFormat(currentValue));
  14137. }
  14138. gEnter.select('.nv-hoverArea').append('rect')
  14139. .on('mousemove', sparklineHover)
  14140. .on('click', function() { paused = !paused })
  14141. .on('mouseout', function() { index = []; updateValueLine(); });
  14142. g.select('.nv-hoverArea rect')
  14143. .attr('transform', function(d) { return 'translate(' + -margin.left + ',' + -margin.top + ')' })
  14144. .attr('width', availableWidth + margin.left + margin.right)
  14145. .attr('height', availableHeight + margin.top);
  14146. //index is currently global (within the chart), may or may not keep it that way
  14147. function updateValueLine() {
  14148. if (paused) return;
  14149. var hoverValue = g.selectAll('.nv-hoverValue').data(index);
  14150. var hoverEnter = hoverValue.enter()
  14151. .append('g').attr('class', 'nv-hoverValue')
  14152. .style('stroke-opacity', 0)
  14153. .style('fill-opacity', 0);
  14154. hoverValue.exit()
  14155. .transition().duration(250)
  14156. .style('stroke-opacity', 0)
  14157. .style('fill-opacity', 0)
  14158. .remove();
  14159. hoverValue
  14160. .attr('transform', function(d) { return 'translate(' + x(sparkline.x()(data[d],d)) + ',0)' })
  14161. .transition().duration(250)
  14162. .style('stroke-opacity', 1)
  14163. .style('fill-opacity', 1);
  14164. if (!index.length) return;
  14165. hoverEnter.append('line')
  14166. .attr('x1', 0)
  14167. .attr('y1', -margin.top)
  14168. .attr('x2', 0)
  14169. .attr('y2', availableHeight);
  14170. hoverEnter.append('text').attr('class', 'nv-xValue')
  14171. .attr('x', -6)
  14172. .attr('y', -margin.top)
  14173. .attr('text-anchor', 'end')
  14174. .attr('dy', '.40em');
  14175. g.select('.nv-hoverValue .nv-xValue')
  14176. .text(xTickFormat(sparkline.x()(data[index[0]], index[0])));
  14177. hoverEnter.append('text').attr('class', 'nv-yValue')
  14178. .attr('x', 6)
  14179. .attr('y', -margin.top)
  14180. .attr('text-anchor', 'start')
  14181. .attr('dy', '.40em');
  14182. g.select('.nv-hoverValue .nv-yValue')
  14183. .text(yTickFormat(sparkline.y()(data[index[0]], index[0])));
  14184. }
  14185. function sparklineHover() {
  14186. if (paused) return;
  14187. var pos = d3.mouse(this)[0] - margin.left;
  14188. function getClosestIndex(data, x) {
  14189. var distance = Math.abs(sparkline.x()(data[0], 0) - x);
  14190. var closestIndex = 0;
  14191. for (var i = 0; i < data.length; i++){
  14192. if (Math.abs(sparkline.x()(data[i], i) - x) < distance) {
  14193. distance = Math.abs(sparkline.x()(data[i], i) - x);
  14194. closestIndex = i;
  14195. }
  14196. }
  14197. return closestIndex;
  14198. }
  14199. index = [getClosestIndex(data, Math.round(x.invert(pos)))];
  14200. updateValueLine();
  14201. }
  14202. });
  14203. renderWatch.renderEnd('sparklinePlus immediate');
  14204. return chart;
  14205. }
  14206. //============================================================
  14207. // Expose Public Variables
  14208. //------------------------------------------------------------
  14209. // expose chart's sub-components
  14210. chart.dispatch = dispatch;
  14211. chart.sparkline = sparkline;
  14212. chart.options = nv.utils.optionsFunc.bind(chart);
  14213. chart._options = Object.create({}, {
  14214. // simple options, just get/set the necessary values
  14215. width: {get: function(){return width;}, set: function(_){width=_;}},
  14216. height: {get: function(){return height;}, set: function(_){height=_;}},
  14217. xTickFormat: {get: function(){return xTickFormat;}, set: function(_){xTickFormat=_;}},
  14218. yTickFormat: {get: function(){return yTickFormat;}, set: function(_){yTickFormat=_;}},
  14219. showLastValue: {get: function(){return showLastValue;}, set: function(_){showLastValue=_;}},
  14220. alignValue: {get: function(){return alignValue;}, set: function(_){alignValue=_;}},
  14221. rightAlignValue: {get: function(){return rightAlignValue;}, set: function(_){rightAlignValue=_;}},
  14222. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  14223. // options that require extra logic in the setter
  14224. margin: {get: function(){return margin;}, set: function(_){
  14225. margin.top = _.top !== undefined ? _.top : margin.top;
  14226. margin.right = _.right !== undefined ? _.right : margin.right;
  14227. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  14228. margin.left = _.left !== undefined ? _.left : margin.left;
  14229. }}
  14230. });
  14231. nv.utils.inheritOptions(chart, sparkline);
  14232. nv.utils.initOptions(chart);
  14233. return chart;
  14234. };
  14235. nv.models.stackedArea = function() {
  14236. "use strict";
  14237. //============================================================
  14238. // Public Variables with Default Settings
  14239. //------------------------------------------------------------
  14240. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  14241. , width = 960
  14242. , height = 500
  14243. , color = nv.utils.defaultColor() // a function that computes the color
  14244. , id = Math.floor(Math.random() * 100000) //Create semi-unique ID incase user doesn't selet one
  14245. , container = null
  14246. , getX = function(d) { return d.x } // accessor to get the x value from a data point
  14247. , getY = function(d) { return d.y } // accessor to get the y value from a data point
  14248. , defined = function(d,i) { return !isNaN(getY(d,i)) && getY(d,i) !== null } // allows a line to be not continuous when it is not defined
  14249. , style = 'stack'
  14250. , offset = 'zero'
  14251. , order = 'default'
  14252. , interpolate = 'linear' // controls the line interpolation
  14253. , clipEdge = false // if true, masks lines within x and y scale
  14254. , x //can be accessed via chart.xScale()
  14255. , y //can be accessed via chart.yScale()
  14256. , scatter = nv.models.scatter()
  14257. , duration = 250
  14258. , dispatch = d3.dispatch('areaClick', 'areaMouseover', 'areaMouseout','renderEnd', 'elementClick', 'elementMouseover', 'elementMouseout')
  14259. ;
  14260. scatter
  14261. .pointSize(2.2) // default size
  14262. .pointDomain([2.2, 2.2]) // all the same size by default
  14263. ;
  14264. /************************************
  14265. * offset:
  14266. * 'wiggle' (stream)
  14267. * 'zero' (stacked)
  14268. * 'expand' (normalize to 100%)
  14269. * 'silhouette' (simple centered)
  14270. *
  14271. * order:
  14272. * 'inside-out' (stream)
  14273. * 'default' (input order)
  14274. ************************************/
  14275. var renderWatch = nv.utils.renderWatch(dispatch, duration);
  14276. function chart(selection) {
  14277. renderWatch.reset();
  14278. renderWatch.models(scatter);
  14279. selection.each(function(data) {
  14280. var availableWidth = width - margin.left - margin.right,
  14281. availableHeight = height - margin.top - margin.bottom;
  14282. container = d3.select(this);
  14283. nv.utils.initSVG(container);
  14284. // Setup Scales
  14285. x = scatter.xScale();
  14286. y = scatter.yScale();
  14287. var dataRaw = data;
  14288. // Injecting point index into each point because d3.layout.stack().out does not give index
  14289. data.forEach(function(aseries, i) {
  14290. aseries.seriesIndex = i;
  14291. aseries.values = aseries.values.map(function(d, j) {
  14292. d.index = j;
  14293. d.seriesIndex = i;
  14294. return d;
  14295. });
  14296. });
  14297. var dataFiltered = data.filter(function(series) {
  14298. return !series.disabled;
  14299. });
  14300. data = d3.layout.stack()
  14301. .order(order)
  14302. .offset(offset)
  14303. .values(function(d) { return d.values }) //TODO: make values customizeable in EVERY model in this fashion
  14304. .x(getX)
  14305. .y(getY)
  14306. .out(function(d, y0, y) {
  14307. d.display = {
  14308. y: y,
  14309. y0: y0
  14310. };
  14311. })
  14312. (dataFiltered);
  14313. // Setup containers and skeleton of chart
  14314. var wrap = container.selectAll('g.nv-wrap.nv-stackedarea').data([data]);
  14315. var wrapEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-stackedarea');
  14316. var defsEnter = wrapEnter.append('defs');
  14317. var gEnter = wrapEnter.append('g');
  14318. var g = wrap.select('g');
  14319. gEnter.append('g').attr('class', 'nv-areaWrap');
  14320. gEnter.append('g').attr('class', 'nv-scatterWrap');
  14321. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  14322. // If the user has not specified forceY, make sure 0 is included in the domain
  14323. // Otherwise, use user-specified values for forceY
  14324. if (scatter.forceY().length == 0) {
  14325. scatter.forceY().push(0);
  14326. }
  14327. scatter
  14328. .width(availableWidth)
  14329. .height(availableHeight)
  14330. .x(getX)
  14331. .y(function(d) {
  14332. if (d.display !== undefined) { return d.display.y + d.display.y0; }
  14333. })
  14334. .color(data.map(function(d,i) {
  14335. d.color = d.color || color(d, d.seriesIndex);
  14336. return d.color;
  14337. }));
  14338. var scatterWrap = g.select('.nv-scatterWrap')
  14339. .datum(data);
  14340. scatterWrap.call(scatter);
  14341. defsEnter.append('clipPath')
  14342. .attr('id', 'nv-edge-clip-' + id)
  14343. .append('rect');
  14344. wrap.select('#nv-edge-clip-' + id + ' rect')
  14345. .attr('width', availableWidth)
  14346. .attr('height', availableHeight);
  14347. g.attr('clip-path', clipEdge ? 'url(#nv-edge-clip-' + id + ')' : '');
  14348. var area = d3.svg.area()
  14349. .defined(defined)
  14350. .x(function(d,i) { return x(getX(d,i)) })
  14351. .y0(function(d) {
  14352. return y(d.display.y0)
  14353. })
  14354. .y1(function(d) {
  14355. return y(d.display.y + d.display.y0)
  14356. })
  14357. .interpolate(interpolate);
  14358. var zeroArea = d3.svg.area()
  14359. .defined(defined)
  14360. .x(function(d,i) { return x(getX(d,i)) })
  14361. .y0(function(d) { return y(d.display.y0) })
  14362. .y1(function(d) { return y(d.display.y0) });
  14363. var path = g.select('.nv-areaWrap').selectAll('path.nv-area')
  14364. .data(function(d) { return d });
  14365. path.enter().append('path').attr('class', function(d,i) { return 'nv-area nv-area-' + i })
  14366. .attr('d', function(d,i){
  14367. return zeroArea(d.values, d.seriesIndex);
  14368. })
  14369. .on('mouseover', function(d,i) {
  14370. d3.select(this).classed('hover', true);
  14371. dispatch.areaMouseover({
  14372. point: d,
  14373. series: d.key,
  14374. pos: [d3.event.pageX, d3.event.pageY],
  14375. seriesIndex: d.seriesIndex
  14376. });
  14377. })
  14378. .on('mouseout', function(d,i) {
  14379. d3.select(this).classed('hover', false);
  14380. dispatch.areaMouseout({
  14381. point: d,
  14382. series: d.key,
  14383. pos: [d3.event.pageX, d3.event.pageY],
  14384. seriesIndex: d.seriesIndex
  14385. });
  14386. })
  14387. .on('click', function(d,i) {
  14388. d3.select(this).classed('hover', false);
  14389. dispatch.areaClick({
  14390. point: d,
  14391. series: d.key,
  14392. pos: [d3.event.pageX, d3.event.pageY],
  14393. seriesIndex: d.seriesIndex
  14394. });
  14395. });
  14396. path.exit().remove();
  14397. path.style('fill', function(d,i){
  14398. return d.color || color(d, d.seriesIndex)
  14399. })
  14400. .style('stroke', function(d,i){ return d.color || color(d, d.seriesIndex) });
  14401. path.watchTransition(renderWatch,'stackedArea path')
  14402. .attr('d', function(d,i) {
  14403. return area(d.values,i)
  14404. });
  14405. //============================================================
  14406. // Event Handling/Dispatching (in chart's scope)
  14407. //------------------------------------------------------------
  14408. scatter.dispatch.on('elementMouseover.area', function(e) {
  14409. g.select('.nv-chart-' + id + ' .nv-area-' + e.seriesIndex).classed('hover', true);
  14410. });
  14411. scatter.dispatch.on('elementMouseout.area', function(e) {
  14412. g.select('.nv-chart-' + id + ' .nv-area-' + e.seriesIndex).classed('hover', false);
  14413. });
  14414. //Special offset functions
  14415. chart.d3_stackedOffset_stackPercent = function(stackData) {
  14416. var n = stackData.length, //How many series
  14417. m = stackData[0].length, //how many points per series
  14418. i,
  14419. j,
  14420. o,
  14421. y0 = [];
  14422. for (j = 0; j < m; ++j) { //Looping through all points
  14423. for (i = 0, o = 0; i < dataRaw.length; i++) { //looping through all series
  14424. o += getY(dataRaw[i].values[j]); //total y value of all series at a certian point in time.
  14425. }
  14426. if (o) for (i = 0; i < n; i++) { //(total y value of all series at point in time i) != 0
  14427. stackData[i][j][1] /= o;
  14428. } else { //(total y value of all series at point in time i) == 0
  14429. for (i = 0; i < n; i++) {
  14430. stackData[i][j][1] = 0;
  14431. }
  14432. }
  14433. }
  14434. for (j = 0; j < m; ++j) y0[j] = 0;
  14435. return y0;
  14436. };
  14437. });
  14438. renderWatch.renderEnd('stackedArea immediate');
  14439. return chart;
  14440. }
  14441. //============================================================
  14442. // Global getters and setters
  14443. //------------------------------------------------------------
  14444. chart.dispatch = dispatch;
  14445. chart.scatter = scatter;
  14446. scatter.dispatch.on('elementClick', function(){ dispatch.elementClick.apply(this, arguments); });
  14447. scatter.dispatch.on('elementMouseover', function(){ dispatch.elementMouseover.apply(this, arguments); });
  14448. scatter.dispatch.on('elementMouseout', function(){ dispatch.elementMouseout.apply(this, arguments); });
  14449. chart.interpolate = function(_) {
  14450. if (!arguments.length) return interpolate;
  14451. interpolate = _;
  14452. return chart;
  14453. };
  14454. chart.duration = function(_) {
  14455. if (!arguments.length) return duration;
  14456. duration = _;
  14457. renderWatch.reset(duration);
  14458. scatter.duration(duration);
  14459. return chart;
  14460. };
  14461. chart.dispatch = dispatch;
  14462. chart.scatter = scatter;
  14463. chart.options = nv.utils.optionsFunc.bind(chart);
  14464. chart._options = Object.create({}, {
  14465. // simple options, just get/set the necessary values
  14466. width: {get: function(){return width;}, set: function(_){width=_;}},
  14467. height: {get: function(){return height;}, set: function(_){height=_;}},
  14468. defined: {get: function(){return defined;}, set: function(_){defined=_;}},
  14469. clipEdge: {get: function(){return clipEdge;}, set: function(_){clipEdge=_;}},
  14470. offset: {get: function(){return offset;}, set: function(_){offset=_;}},
  14471. order: {get: function(){return order;}, set: function(_){order=_;}},
  14472. interpolate: {get: function(){return interpolate;}, set: function(_){interpolate=_;}},
  14473. // simple functor options
  14474. x: {get: function(){return getX;}, set: function(_){getX = d3.functor(_);}},
  14475. y: {get: function(){return getY;}, set: function(_){getY = d3.functor(_);}},
  14476. // options that require extra logic in the setter
  14477. margin: {get: function(){return margin;}, set: function(_){
  14478. margin.top = _.top !== undefined ? _.top : margin.top;
  14479. margin.right = _.right !== undefined ? _.right : margin.right;
  14480. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  14481. margin.left = _.left !== undefined ? _.left : margin.left;
  14482. }},
  14483. color: {get: function(){return color;}, set: function(_){
  14484. color = nv.utils.getColor(_);
  14485. }},
  14486. style: {get: function(){return style;}, set: function(_){
  14487. style = _;
  14488. switch (style) {
  14489. case 'stack':
  14490. chart.offset('zero');
  14491. chart.order('default');
  14492. break;
  14493. case 'stream':
  14494. chart.offset('wiggle');
  14495. chart.order('inside-out');
  14496. break;
  14497. case 'stream-center':
  14498. chart.offset('silhouette');
  14499. chart.order('inside-out');
  14500. break;
  14501. case 'expand':
  14502. chart.offset('expand');
  14503. chart.order('default');
  14504. break;
  14505. case 'stack_percent':
  14506. chart.offset(chart.d3_stackedOffset_stackPercent);
  14507. chart.order('default');
  14508. break;
  14509. }
  14510. }},
  14511. duration: {get: function(){return duration;}, set: function(_){
  14512. duration = _;
  14513. renderWatch.reset(duration);
  14514. scatter.duration(duration);
  14515. }}
  14516. });
  14517. nv.utils.inheritOptions(chart, scatter);
  14518. nv.utils.initOptions(chart);
  14519. return chart;
  14520. };
  14521. nv.models.stackedAreaChart = function() {
  14522. "use strict";
  14523. //============================================================
  14524. // Public Variables with Default Settings
  14525. //------------------------------------------------------------
  14526. var stacked = nv.models.stackedArea()
  14527. , xAxis = nv.models.axis()
  14528. , yAxis = nv.models.axis()
  14529. , legend = nv.models.legend()
  14530. , controls = nv.models.legend()
  14531. , interactiveLayer = nv.interactiveGuideline()
  14532. , tooltip = nv.models.tooltip()
  14533. , focus = nv.models.focus(nv.models.stackedArea())
  14534. ;
  14535. var margin = {top: 10, right: 25, bottom: 50, left: 60}
  14536. , marginTop = null
  14537. , width = null
  14538. , height = null
  14539. , color = nv.utils.defaultColor()
  14540. , showControls = true
  14541. , showLegend = true
  14542. , legendPosition = 'top'
  14543. , showXAxis = true
  14544. , showYAxis = true
  14545. , rightAlignYAxis = false
  14546. , focusEnable = false
  14547. , useInteractiveGuideline = false
  14548. , showTotalInTooltip = true
  14549. , totalLabel = 'TOTAL'
  14550. , x //can be accessed via chart.xScale()
  14551. , y //can be accessed via chart.yScale()
  14552. , state = nv.utils.state()
  14553. , defaultState = null
  14554. , noData = null
  14555. , dispatch = d3.dispatch('stateChange', 'changeState','renderEnd')
  14556. , controlWidth = 250
  14557. , controlOptions = ['Stacked','Stream','Expanded']
  14558. , controlLabels = {}
  14559. , duration = 250
  14560. ;
  14561. state.style = stacked.style();
  14562. xAxis.orient('bottom').tickPadding(7);
  14563. yAxis.orient((rightAlignYAxis) ? 'right' : 'left');
  14564. tooltip
  14565. .headerFormatter(function(d, i) {
  14566. return xAxis.tickFormat()(d, i);
  14567. })
  14568. .valueFormatter(function(d, i) {
  14569. return yAxis.tickFormat()(d, i);
  14570. });
  14571. interactiveLayer.tooltip
  14572. .headerFormatter(function(d, i) {
  14573. return xAxis.tickFormat()(d, i);
  14574. })
  14575. .valueFormatter(function(d, i) {
  14576. return d == null ? "N/A" : yAxis.tickFormat()(d, i);
  14577. });
  14578. var oldYTickFormat = null,
  14579. oldValueFormatter = null;
  14580. controls.updateState(false);
  14581. //============================================================
  14582. // Private Variables
  14583. //------------------------------------------------------------
  14584. var renderWatch = nv.utils.renderWatch(dispatch);
  14585. var style = stacked.style();
  14586. var stateGetter = function(data) {
  14587. return function(){
  14588. return {
  14589. active: data.map(function(d) { return !d.disabled }),
  14590. style: stacked.style()
  14591. };
  14592. }
  14593. };
  14594. var stateSetter = function(data) {
  14595. return function(state) {
  14596. if (state.style !== undefined)
  14597. style = state.style;
  14598. if (state.active !== undefined)
  14599. data.forEach(function(series,i) {
  14600. series.disabled = !state.active[i];
  14601. });
  14602. }
  14603. };
  14604. var percentFormatter = d3.format('%');
  14605. function chart(selection) {
  14606. renderWatch.reset();
  14607. renderWatch.models(stacked);
  14608. if (showXAxis) renderWatch.models(xAxis);
  14609. if (showYAxis) renderWatch.models(yAxis);
  14610. selection.each(function(data) {
  14611. var container = d3.select(this),
  14612. that = this;
  14613. nv.utils.initSVG(container);
  14614. var availableWidth = nv.utils.availableWidth(width, container, margin),
  14615. availableHeight = nv.utils.availableHeight(height, container, margin) - (focusEnable ? focus.height() : 0);
  14616. chart.update = function() { container.transition().duration(duration).call(chart); };
  14617. chart.container = this;
  14618. state
  14619. .setter(stateSetter(data), chart.update)
  14620. .getter(stateGetter(data))
  14621. .update();
  14622. // DEPRECATED set state.disabled
  14623. state.disabled = data.map(function(d) { return !!d.disabled });
  14624. if (!defaultState) {
  14625. var key;
  14626. defaultState = {};
  14627. for (key in state) {
  14628. if (state[key] instanceof Array)
  14629. defaultState[key] = state[key].slice(0);
  14630. else
  14631. defaultState[key] = state[key];
  14632. }
  14633. }
  14634. // Display No Data message if there's nothing to show.
  14635. if (!data || !data.length || !data.filter(function(d) { return d.values.length }).length) {
  14636. nv.utils.noData(chart, container)
  14637. return chart;
  14638. } else {
  14639. container.selectAll('.nv-noData').remove();
  14640. }
  14641. // Setup Scales
  14642. x = stacked.xScale();
  14643. y = stacked.yScale();
  14644. // Setup containers and skeleton of chart
  14645. var wrap = container.selectAll('g.nv-wrap.nv-stackedAreaChart').data([data]);
  14646. var gEnter = wrap.enter().append('g').attr('class', 'nvd3 nv-wrap nv-stackedAreaChart').append('g');
  14647. var g = wrap.select('g');
  14648. gEnter.append('g').attr('class', 'nv-legendWrap');
  14649. gEnter.append('g').attr('class', 'nv-controlsWrap');
  14650. var focusEnter = gEnter.append('g').attr('class', 'nv-focus');
  14651. focusEnter.append('g').attr('class', 'nv-background').append('rect');
  14652. focusEnter.append('g').attr('class', 'nv-x nv-axis');
  14653. focusEnter.append('g').attr('class', 'nv-y nv-axis');
  14654. focusEnter.append('g').attr('class', 'nv-stackedWrap');
  14655. focusEnter.append('g').attr('class', 'nv-interactive');
  14656. // g.select("rect").attr("width",availableWidth).attr("height",availableHeight);
  14657. var contextEnter = gEnter.append('g').attr('class', 'nv-focusWrap');
  14658. // Legend
  14659. if (!showLegend) {
  14660. g.select('.nv-legendWrap').selectAll('*').remove();
  14661. } else {
  14662. var legendWidth = (showControls && legendPosition === 'top') ? availableWidth - controlWidth : availableWidth;
  14663. legend.width(legendWidth);
  14664. g.select('.nv-legendWrap').datum(data).call(legend);
  14665. if (legendPosition === 'bottom') {
  14666. var xAxisHeight = xAxis.height();
  14667. margin.bottom = Math.max(legend.height() + xAxisHeight, margin.bottom);
  14668. availableHeight = nv.utils.availableHeight(height, container, margin) - (focusEnable ? focus.height() : 0);
  14669. var legendTop = availableHeight + xAxisHeight;
  14670. g.select('.nv-legendWrap')
  14671. .attr('transform', 'translate(0,' + legendTop +')');
  14672. } else if (legendPosition === 'top') {
  14673. if (!marginTop && margin.top != legend.height()) {
  14674. margin.top = legend.height();
  14675. availableHeight = nv.utils.availableHeight(height, container, margin) - (focusEnable ? focus.height() : 0);
  14676. }
  14677. g.select('.nv-legendWrap')
  14678. .attr('transform', 'translate(' + (availableWidth-legendWidth) + ',' + (-margin.top) +')');
  14679. }
  14680. }
  14681. // Controls
  14682. if (!showControls) {
  14683. g.select('.nv-controlsWrap').selectAll('*').remove();
  14684. } else {
  14685. var controlsData = [
  14686. {
  14687. key: controlLabels.stacked || 'Stacked',
  14688. metaKey: 'Stacked',
  14689. disabled: stacked.style() != 'stack',
  14690. style: 'stack'
  14691. },
  14692. {
  14693. key: controlLabels.stream || 'Stream',
  14694. metaKey: 'Stream',
  14695. disabled: stacked.style() != 'stream',
  14696. style: 'stream'
  14697. },
  14698. {
  14699. key: controlLabels.stream_center || 'Stream Center',
  14700. metaKey: 'Stream_Center',
  14701. disabled: stacked.style() != 'stream_center',
  14702. style: 'stream-center'
  14703. },
  14704. {
  14705. key: controlLabels.expanded || 'Expanded',
  14706. metaKey: 'Expanded',
  14707. disabled: stacked.style() != 'expand',
  14708. style: 'expand'
  14709. },
  14710. {
  14711. key: controlLabels.stack_percent || 'Stack %',
  14712. metaKey: 'Stack_Percent',
  14713. disabled: stacked.style() != 'stack_percent',
  14714. style: 'stack_percent'
  14715. }
  14716. ];
  14717. controlWidth = (controlOptions.length/3) * 260;
  14718. controlsData = controlsData.filter(function(d) {
  14719. return controlOptions.indexOf(d.metaKey) !== -1;
  14720. });
  14721. controls
  14722. .width( controlWidth )
  14723. .color(['#444', '#444', '#444']);
  14724. g.select('.nv-controlsWrap')
  14725. .datum(controlsData)
  14726. .call(controls);
  14727. var requiredTop = Math.max(controls.height(), showLegend && (legendPosition === 'top') ? legend.height() : 0);
  14728. if ( margin.top != requiredTop ) {
  14729. margin.top = requiredTop;
  14730. availableHeight = nv.utils.availableHeight(height, container, margin) - (focusEnable ? focus.height() : 0);
  14731. }
  14732. g.select('.nv-controlsWrap')
  14733. .attr('transform', 'translate(0,' + (-margin.top) +')');
  14734. }
  14735. wrap.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
  14736. if (rightAlignYAxis) {
  14737. g.select(".nv-y.nv-axis")
  14738. .attr("transform", "translate(" + availableWidth + ",0)");
  14739. }
  14740. //Set up interactive layer
  14741. if (useInteractiveGuideline) {
  14742. interactiveLayer
  14743. .width(availableWidth)
  14744. .height(availableHeight)
  14745. .margin({left: margin.left, top: margin.top})
  14746. .svgContainer(container)
  14747. .xScale(x);
  14748. wrap.select(".nv-interactive").call(interactiveLayer);
  14749. }
  14750. g.select('.nv-focus .nv-background rect')
  14751. .attr('width', availableWidth)
  14752. .attr('height', availableHeight);
  14753. stacked
  14754. .width(availableWidth)
  14755. .height(availableHeight)
  14756. .color(data.map(function(d,i) {
  14757. return d.color || color(d, i);
  14758. }).filter(function(d,i) { return !data[i].disabled; }));
  14759. var stackedWrap = g.select('.nv-focus .nv-stackedWrap')
  14760. .datum(data.filter(function(d) { return !d.disabled; }));
  14761. // Setup Axes
  14762. if (showXAxis) {
  14763. xAxis.scale(x)
  14764. ._ticks( nv.utils.calcTicksX(availableWidth/100, data) )
  14765. .tickSize( -availableHeight, 0);
  14766. }
  14767. if (showYAxis) {
  14768. var ticks;
  14769. if (stacked.offset() === 'wiggle') {
  14770. ticks = 0;
  14771. }
  14772. else {
  14773. ticks = nv.utils.calcTicksY(availableHeight/36, data);
  14774. }
  14775. yAxis.scale(y)
  14776. ._ticks(ticks)
  14777. .tickSize(-availableWidth, 0);
  14778. }
  14779. //============================================================
  14780. // Update Axes
  14781. //============================================================
  14782. function updateXAxis() {
  14783. if(showXAxis) {
  14784. g.select('.nv-focus .nv-x.nv-axis')
  14785. .attr('transform', 'translate(0,' + availableHeight + ')')
  14786. .transition()
  14787. .duration(duration)
  14788. .call(xAxis)
  14789. ;
  14790. }
  14791. }
  14792. function updateYAxis() {
  14793. if(showYAxis) {
  14794. if (stacked.style() === 'expand' || stacked.style() === 'stack_percent') {
  14795. var currentFormat = yAxis.tickFormat();
  14796. if ( !oldYTickFormat || currentFormat !== percentFormatter )
  14797. oldYTickFormat = currentFormat;
  14798. //Forces the yAxis to use percentage in 'expand' mode.
  14799. yAxis.tickFormat(percentFormatter);
  14800. }
  14801. else {
  14802. if (oldYTickFormat) {
  14803. yAxis.tickFormat(oldYTickFormat);
  14804. oldYTickFormat = null;
  14805. }
  14806. }
  14807. g.select('.nv-focus .nv-y.nv-axis')
  14808. .transition().duration(0)
  14809. .call(yAxis);
  14810. }
  14811. }
  14812. //============================================================
  14813. // Update Focus
  14814. //============================================================
  14815. if(!focusEnable) {
  14816. stackedWrap.transition().call(stacked);
  14817. updateXAxis();
  14818. updateYAxis();
  14819. } else {
  14820. focus.width(availableWidth);
  14821. g.select('.nv-focusWrap')
  14822. .attr('transform', 'translate(0,' + ( availableHeight + margin.bottom + focus.margin().top) + ')')
  14823. .datum(data.filter(function(d) { return !d.disabled; }))
  14824. .call(focus);
  14825. var extent = focus.brush.empty() ? focus.xDomain() : focus.brush.extent();
  14826. if(extent !== null){
  14827. onBrush(extent);
  14828. }
  14829. }
  14830. //============================================================
  14831. // Event Handling/Dispatching (in chart's scope)
  14832. //------------------------------------------------------------
  14833. stacked.dispatch.on('areaClick.toggle', function(e) {
  14834. if (data.filter(function(d) { return !d.disabled }).length === 1)
  14835. data.forEach(function(d) {
  14836. d.disabled = false;
  14837. });
  14838. else
  14839. data.forEach(function(d,i) {
  14840. d.disabled = (i != e.seriesIndex);
  14841. });
  14842. state.disabled = data.map(function(d) { return !!d.disabled });
  14843. dispatch.stateChange(state);
  14844. chart.update();
  14845. });
  14846. legend.dispatch.on('stateChange', function(newState) {
  14847. for (var key in newState)
  14848. state[key] = newState[key];
  14849. dispatch.stateChange(state);
  14850. chart.update();
  14851. });
  14852. controls.dispatch.on('legendClick', function(d,i) {
  14853. if (!d.disabled) return;
  14854. controlsData = controlsData.map(function(s) {
  14855. s.disabled = true;
  14856. return s;
  14857. });
  14858. d.disabled = false;
  14859. stacked.style(d.style);
  14860. state.style = stacked.style();
  14861. dispatch.stateChange(state);
  14862. chart.update();
  14863. });
  14864. interactiveLayer.dispatch.on('elementMousemove', function(e) {
  14865. stacked.clearHighlights();
  14866. var singlePoint, pointIndex, pointXLocation, allData = [], valueSum = 0, allNullValues = true, atleastOnePoint = false;
  14867. data
  14868. .filter(function(series, i) {
  14869. series.seriesIndex = i;
  14870. return !series.disabled;
  14871. })
  14872. .forEach(function(series,i) {
  14873. pointIndex = nv.interactiveBisect(series.values, e.pointXValue, chart.x());
  14874. var point = series.values[pointIndex];
  14875. var pointYValue = chart.y()(point, pointIndex);
  14876. if (pointYValue != null && pointYValue > 0) {
  14877. stacked.highlightPoint(i, pointIndex, true);
  14878. atleastOnePoint = true;
  14879. }
  14880. // Draw at least one point if all values are zero.
  14881. if (i === (data.length - 1) && !atleastOnePoint) {
  14882. stacked.highlightPoint(i, pointIndex, true);
  14883. }
  14884. if (typeof point === 'undefined') return;
  14885. if (typeof singlePoint === 'undefined') singlePoint = point;
  14886. if (typeof pointXLocation === 'undefined') pointXLocation = chart.xScale()(chart.x()(point,pointIndex));
  14887. //If we are in 'expand' mode, use the stacked percent value instead of raw value.
  14888. var tooltipValue = (stacked.style() == 'expand') ? point.display.y : chart.y()(point,pointIndex);
  14889. allData.push({
  14890. key: series.key,
  14891. value: tooltipValue,
  14892. color: color(series,series.seriesIndex),
  14893. point: point
  14894. });
  14895. if (showTotalInTooltip && stacked.style() != 'expand' && tooltipValue != null) {
  14896. valueSum += tooltipValue;
  14897. allNullValues = false;
  14898. };
  14899. });
  14900. allData.reverse();
  14901. //Highlight the tooltip entry based on which stack the mouse is closest to.
  14902. if (allData.length > 2) {
  14903. var yValue = chart.yScale().invert(e.mouseY);
  14904. var yDistMax = Infinity, indexToHighlight = null;
  14905. allData.forEach(function(series,i) {
  14906. //To handle situation where the stacked area chart is negative, we need to use absolute values
  14907. //when checking if the mouse Y value is within the stack area.
  14908. yValue = Math.abs(yValue);
  14909. var stackedY0 = Math.abs(series.point.display.y0);
  14910. var stackedY = Math.abs(series.point.display.y);
  14911. if ( yValue >= stackedY0 && yValue <= (stackedY + stackedY0))
  14912. {
  14913. indexToHighlight = i;
  14914. return;
  14915. }
  14916. });
  14917. if (indexToHighlight != null)
  14918. allData[indexToHighlight].highlight = true;
  14919. }
  14920. //If we are not in 'expand' mode, add a 'Total' row to the tooltip.
  14921. if (showTotalInTooltip && stacked.style() != 'expand' && allData.length >= 2 && !allNullValues) {
  14922. allData.push({
  14923. key: totalLabel,
  14924. value: valueSum,
  14925. total: true
  14926. });
  14927. }
  14928. var xValue = chart.x()(singlePoint,pointIndex);
  14929. var valueFormatter = interactiveLayer.tooltip.valueFormatter();
  14930. // Keeps track of the tooltip valueFormatter if the chart changes to expanded view
  14931. if (stacked.style() === 'expand' || stacked.style() === 'stack_percent') {
  14932. if ( !oldValueFormatter ) {
  14933. oldValueFormatter = valueFormatter;
  14934. }
  14935. //Forces the tooltip to use percentage in 'expand' mode.
  14936. valueFormatter = d3.format(".1%");
  14937. }
  14938. else {
  14939. if (oldValueFormatter) {
  14940. valueFormatter = oldValueFormatter;
  14941. oldValueFormatter = null;
  14942. }
  14943. }
  14944. interactiveLayer.tooltip
  14945. .valueFormatter(valueFormatter)
  14946. .data(
  14947. {
  14948. value: xValue,
  14949. series: allData
  14950. }
  14951. )();
  14952. interactiveLayer.renderGuideLine(pointXLocation);
  14953. });
  14954. interactiveLayer.dispatch.on("elementMouseout",function(e) {
  14955. stacked.clearHighlights();
  14956. });
  14957. /* Update `main' graph on brush update. */
  14958. focus.dispatch.on("onBrush", function(extent) {
  14959. onBrush(extent);
  14960. });
  14961. // Update chart from a state object passed to event handler
  14962. dispatch.on('changeState', function(e) {
  14963. if (typeof e.disabled !== 'undefined' && data.length === e.disabled.length) {
  14964. data.forEach(function(series,i) {
  14965. series.disabled = e.disabled[i];
  14966. });
  14967. state.disabled = e.disabled;
  14968. }
  14969. if (typeof e.style !== 'undefined') {
  14970. stacked.style(e.style);
  14971. style = e.style;
  14972. }
  14973. chart.update();
  14974. });
  14975. //============================================================
  14976. // Functions
  14977. //------------------------------------------------------------
  14978. function onBrush(extent) {
  14979. // Update Main (Focus)
  14980. var stackedWrap = g.select('.nv-focus .nv-stackedWrap')
  14981. .datum(
  14982. data.filter(function(d) { return !d.disabled; })
  14983. .map(function(d,i) {
  14984. return {
  14985. key: d.key,
  14986. area: d.area,
  14987. classed: d.classed,
  14988. values: d.values.filter(function(d,i) {
  14989. return stacked.x()(d,i) >= extent[0] && stacked.x()(d,i) <= extent[1];
  14990. }),
  14991. disableTooltip: d.disableTooltip
  14992. };
  14993. })
  14994. );
  14995. stackedWrap.transition().duration(duration).call(stacked);
  14996. // Update Main (Focus) Axes
  14997. updateXAxis();
  14998. updateYAxis();
  14999. }
  15000. });
  15001. renderWatch.renderEnd('stacked Area chart immediate');
  15002. return chart;
  15003. }
  15004. //============================================================
  15005. // Event Handling/Dispatching (out of chart's scope)
  15006. //------------------------------------------------------------
  15007. stacked.dispatch.on('elementMouseover.tooltip', function(evt) {
  15008. evt.point['x'] = stacked.x()(evt.point);
  15009. evt.point['y'] = stacked.y()(evt.point);
  15010. tooltip.data(evt).hidden(false);
  15011. });
  15012. stacked.dispatch.on('elementMouseout.tooltip', function(evt) {
  15013. tooltip.hidden(true)
  15014. });
  15015. //============================================================
  15016. // Expose Public Variables
  15017. //------------------------------------------------------------
  15018. // expose chart's sub-components
  15019. chart.dispatch = dispatch;
  15020. chart.stacked = stacked;
  15021. chart.legend = legend;
  15022. chart.controls = controls;
  15023. chart.xAxis = xAxis;
  15024. chart.x2Axis = focus.xAxis;
  15025. chart.yAxis = yAxis;
  15026. chart.y2Axis = focus.yAxis;
  15027. chart.interactiveLayer = interactiveLayer;
  15028. chart.tooltip = tooltip;
  15029. chart.focus = focus;
  15030. chart.dispatch = dispatch;
  15031. chart.options = nv.utils.optionsFunc.bind(chart);
  15032. chart._options = Object.create({}, {
  15033. // simple options, just get/set the necessary values
  15034. width: {get: function(){return width;}, set: function(_){width=_;}},
  15035. height: {get: function(){return height;}, set: function(_){height=_;}},
  15036. showLegend: {get: function(){return showLegend;}, set: function(_){showLegend=_;}},
  15037. legendPosition: {get: function(){return legendPosition;}, set: function(_){legendPosition=_;}},
  15038. showXAxis: {get: function(){return showXAxis;}, set: function(_){showXAxis=_;}},
  15039. showYAxis: {get: function(){return showYAxis;}, set: function(_){showYAxis=_;}},
  15040. defaultState: {get: function(){return defaultState;}, set: function(_){defaultState=_;}},
  15041. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  15042. showControls: {get: function(){return showControls;}, set: function(_){showControls=_;}},
  15043. controlLabels: {get: function(){return controlLabels;}, set: function(_){controlLabels=_;}},
  15044. controlOptions: {get: function(){return controlOptions;}, set: function(_){controlOptions=_;}},
  15045. showTotalInTooltip: {get: function(){return showTotalInTooltip;}, set: function(_){showTotalInTooltip=_;}},
  15046. totalLabel: {get: function(){return totalLabel;}, set: function(_){totalLabel=_;}},
  15047. focusEnable: {get: function(){return focusEnable;}, set: function(_){focusEnable=_;}},
  15048. focusHeight: {get: function(){return focus.height();}, set: function(_){focus.height(_);}},
  15049. brushExtent: {get: function(){return focus.brushExtent();}, set: function(_){focus.brushExtent(_);}},
  15050. // options that require extra logic in the setter
  15051. margin: {get: function(){return margin;}, set: function(_){
  15052. if (_.top !== undefined) {
  15053. margin.top = _.top;
  15054. marginTop = _.top;
  15055. }
  15056. margin.right = _.right !== undefined ? _.right : margin.right;
  15057. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  15058. margin.left = _.left !== undefined ? _.left : margin.left;
  15059. }},
  15060. focusMargin: {get: function(){return focus.margin}, set: function(_){
  15061. focus.margin.top = _.top !== undefined ? _.top : focus.margin.top;
  15062. focus.margin.right = _.right !== undefined ? _.right : focus.margin.right;
  15063. focus.margin.bottom = _.bottom !== undefined ? _.bottom : focus.margin.bottom;
  15064. focus.margin.left = _.left !== undefined ? _.left : focus.margin.left;
  15065. }},
  15066. duration: {get: function(){return duration;}, set: function(_){
  15067. duration = _;
  15068. renderWatch.reset(duration);
  15069. stacked.duration(duration);
  15070. xAxis.duration(duration);
  15071. yAxis.duration(duration);
  15072. }},
  15073. color: {get: function(){return color;}, set: function(_){
  15074. color = nv.utils.getColor(_);
  15075. legend.color(color);
  15076. stacked.color(color);
  15077. focus.color(color);
  15078. }},
  15079. x: {get: function(){return stacked.x();}, set: function(_){
  15080. stacked.x(_);
  15081. focus.x(_);
  15082. }},
  15083. y: {get: function(){return stacked.y();}, set: function(_){
  15084. stacked.y(_);
  15085. focus.y(_);
  15086. }},
  15087. rightAlignYAxis: {get: function(){return rightAlignYAxis;}, set: function(_){
  15088. rightAlignYAxis = _;
  15089. yAxis.orient( rightAlignYAxis ? 'right' : 'left');
  15090. }},
  15091. useInteractiveGuideline: {get: function(){return useInteractiveGuideline;}, set: function(_){
  15092. useInteractiveGuideline = !!_;
  15093. chart.interactive(!_);
  15094. chart.useVoronoi(!_);
  15095. stacked.scatter.interactive(!_);
  15096. }}
  15097. });
  15098. nv.utils.inheritOptions(chart, stacked);
  15099. nv.utils.initOptions(chart);
  15100. return chart;
  15101. };
  15102. nv.models.stackedAreaWithFocusChart = function() {
  15103. return nv.models.stackedAreaChart()
  15104. .margin({ bottom: 30 })
  15105. .focusEnable( true );
  15106. };
  15107. // based on http://bl.ocks.org/kerryrodden/477c1bfb081b783f80ad
  15108. nv.models.sunburst = function() {
  15109. "use strict";
  15110. //============================================================
  15111. // Public Variables with Default Settings
  15112. //------------------------------------------------------------
  15113. var margin = {top: 0, right: 0, bottom: 0, left: 0}
  15114. , width = 600
  15115. , height = 600
  15116. , mode = "count"
  15117. , modes = {count: function(d) { return 1; }, value: function(d) { return d.value || d.size }, size: function(d) { return d.value || d.size }}
  15118. , id = Math.floor(Math.random() * 10000) //Create semi-unique ID in case user doesn't select one
  15119. , container = null
  15120. , color = nv.utils.defaultColor()
  15121. , showLabels = false
  15122. , labelFormat = function(d){if(mode === 'count'){return d.name + ' #' + d.value}else{return d.name + ' ' + (d.value || d.size)}}
  15123. , labelThreshold = 0.02
  15124. , sort = function(d1, d2){return d1.name > d2.name;}
  15125. , key = function(d,i){
  15126. if (d.parent !== undefined) {
  15127. return d.name + '-' + d.parent.name + '-' + i;
  15128. } else {
  15129. return d.name;
  15130. }
  15131. }
  15132. , groupColorByParent = true
  15133. , duration = 500
  15134. , dispatch = d3.dispatch('chartClick', 'elementClick', 'elementDblClick', 'elementMousemove', 'elementMouseover', 'elementMouseout', 'renderEnd');
  15135. //============================================================
  15136. // aux functions and setup
  15137. //------------------------------------------------------------
  15138. var x = d3.scale.linear().range([0, 2 * Math.PI]);
  15139. var y = d3.scale.sqrt();
  15140. var partition = d3.layout.partition().sort(sort);
  15141. var node, availableWidth, availableHeight, radius;
  15142. var prevPositions = {};
  15143. var arc = d3.svg.arc()
  15144. .startAngle(function(d) {return Math.max(0, Math.min(2 * Math.PI, x(d.x))) })
  15145. .endAngle(function(d) {return Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx))) })
  15146. .innerRadius(function(d) {return Math.max(0, y(d.y)) })
  15147. .outerRadius(function(d) {return Math.max(0, y(d.y + d.dy)) });
  15148. function rotationToAvoidUpsideDown(d) {
  15149. var centerAngle = computeCenterAngle(d);
  15150. if(centerAngle > 90){
  15151. return 180;
  15152. }
  15153. else {
  15154. return 0;
  15155. }
  15156. }
  15157. function computeCenterAngle(d) {
  15158. var startAngle = Math.max(0, Math.min(2 * Math.PI, x(d.x)));
  15159. var endAngle = Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx)));
  15160. var centerAngle = (((startAngle + endAngle) / 2) * (180 / Math.PI)) - 90;
  15161. return centerAngle;
  15162. }
  15163. function computeNodePercentage(d) {
  15164. var startAngle = Math.max(0, Math.min(2 * Math.PI, x(d.x)));
  15165. var endAngle = Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx)));
  15166. return (endAngle - startAngle) / (2 * Math.PI);
  15167. }
  15168. function labelThresholdMatched(d) {
  15169. var startAngle = Math.max(0, Math.min(2 * Math.PI, x(d.x)));
  15170. var endAngle = Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx)));
  15171. var size = endAngle - startAngle;
  15172. return size > labelThreshold;
  15173. }
  15174. // When zooming: interpolate the scales.
  15175. function arcTweenZoom(e,i) {
  15176. var xd = d3.interpolate(x.domain(), [node.x, node.x + node.dx]),
  15177. yd = d3.interpolate(y.domain(), [node.y, 1]),
  15178. yr = d3.interpolate(y.range(), [node.y ? 20 : 0, radius]);
  15179. if (i === 0) {
  15180. return function() {return arc(e);}
  15181. }
  15182. else {
  15183. return function (t) {
  15184. x.domain(xd(t));
  15185. y.domain(yd(t)).range(yr(t));
  15186. return arc(e);
  15187. }
  15188. };
  15189. }
  15190. function arcTweenUpdate(d) {
  15191. var ipo = d3.interpolate({x: d.x0, dx: d.dx0, y: d.y0, dy: d.dy0}, d);
  15192. return function (t) {
  15193. var b = ipo(t);
  15194. d.x0 = b.x;
  15195. d.dx0 = b.dx;
  15196. d.y0 = b.y;
  15197. d.dy0 = b.dy;
  15198. return arc(b);
  15199. };
  15200. }
  15201. function updatePrevPosition(node) {
  15202. var k = key(node);
  15203. if(! prevPositions[k]) prevPositions[k] = {};
  15204. var pP = prevPositions[k];
  15205. pP.dx = node.dx;
  15206. pP.x = node.x;
  15207. pP.dy = node.dy;
  15208. pP.y = node.y;
  15209. }
  15210. function storeRetrievePrevPositions(nodes) {
  15211. nodes.forEach(function(n){
  15212. var k = key(n);
  15213. var pP = prevPositions[k];
  15214. //console.log(k,n,pP);
  15215. if( pP ){
  15216. n.dx0 = pP.dx;
  15217. n.x0 = pP.x;
  15218. n.dy0 = pP.dy;
  15219. n.y0 = pP.y;
  15220. }
  15221. else {
  15222. n.dx0 = n.dx;
  15223. n.x0 = n.x;
  15224. n.dy0 = n.dy;
  15225. n.y0 = n.y;
  15226. }
  15227. updatePrevPosition(n);
  15228. });
  15229. }
  15230. function zoomClick(d) {
  15231. var labels = container.selectAll('text')
  15232. var path = container.selectAll('path')
  15233. // fade out all text elements
  15234. labels.transition().attr("opacity",0);
  15235. // to allow reference to the new center node
  15236. node = d;
  15237. path.transition()
  15238. .duration(duration)
  15239. .attrTween("d", arcTweenZoom)
  15240. .each('end', function(e) {
  15241. // partially taken from here: http://bl.ocks.org/metmajer/5480307
  15242. // check if the animated element's data e lies within the visible angle span given in d
  15243. if(e.x >= d.x && e.x < (d.x + d.dx) ){
  15244. if(e.depth >= d.depth){
  15245. // get a selection of the associated text element
  15246. var parentNode = d3.select(this.parentNode);
  15247. var arcText = parentNode.select('text');
  15248. // fade in the text element and recalculate positions
  15249. arcText.transition().duration(duration)
  15250. .text( function(e){return labelFormat(e) })
  15251. .attr("opacity", function(d){
  15252. if(labelThresholdMatched(d)) {
  15253. return 1;
  15254. }
  15255. else {
  15256. return 0;
  15257. }
  15258. })
  15259. .attr("transform", function() {
  15260. var width = this.getBBox().width;
  15261. if(e.depth === 0)
  15262. return "translate(" + (width / 2 * - 1) + ",0)";
  15263. else if(e.depth === d.depth){
  15264. return "translate(" + (y(e.y) + 5) + ",0)";
  15265. }
  15266. else {
  15267. var centerAngle = computeCenterAngle(e);
  15268. var rotation = rotationToAvoidUpsideDown(e);
  15269. if (rotation === 0) {
  15270. return 'rotate('+ centerAngle +')translate(' + (y(e.y) + 5) + ',0)';
  15271. }
  15272. else {
  15273. return 'rotate('+ centerAngle +')translate(' + (y(e.y) + width + 5) + ',0)rotate(' + rotation + ')';
  15274. }
  15275. }
  15276. });
  15277. }
  15278. }
  15279. })
  15280. }
  15281. //============================================================
  15282. // chart function
  15283. //------------------------------------------------------------
  15284. var renderWatch = nv.utils.renderWatch(dispatch);
  15285. function chart(selection) {
  15286. renderWatch.reset();
  15287. selection.each(function(data) {
  15288. container = d3.select(this);
  15289. availableWidth = nv.utils.availableWidth(width, container, margin);
  15290. availableHeight = nv.utils.availableHeight(height, container, margin);
  15291. radius = Math.min(availableWidth, availableHeight) / 2;
  15292. y.range([0, radius]);
  15293. // Setup containers and skeleton of chart
  15294. var wrap = container.select('g.nvd3.nv-wrap.nv-sunburst');
  15295. if( !wrap[0][0] ) {
  15296. wrap = container.append('g')
  15297. .attr('class', 'nvd3 nv-wrap nv-sunburst nv-chart-' + id)
  15298. .attr('transform', 'translate(' + ((availableWidth / 2) + margin.left + margin.right) + ',' + ((availableHeight / 2) + margin.top + margin.bottom) + ')');
  15299. } else {
  15300. wrap.attr('transform', 'translate(' + ((availableWidth / 2) + margin.left + margin.right) + ',' + ((availableHeight / 2) + margin.top + margin.bottom) + ')');
  15301. }
  15302. container.on('click', function (d, i) {
  15303. dispatch.chartClick({
  15304. data: d,
  15305. index: i,
  15306. pos: d3.event,
  15307. id: id
  15308. });
  15309. });
  15310. partition.value(modes[mode] || modes["count"]);
  15311. //reverse the drawing order so that the labels of inner
  15312. //arcs are drawn on top of the outer arcs.
  15313. var nodes = partition.nodes(data[0]).reverse()
  15314. storeRetrievePrevPositions(nodes);
  15315. var cG = wrap.selectAll('.arc-container').data(nodes, key)
  15316. //handle new datapoints
  15317. var cGE = cG.enter()
  15318. .append("g")
  15319. .attr("class",'arc-container')
  15320. cGE.append("path")
  15321. .attr("d", arc)
  15322. .style("fill", function (d) {
  15323. if (d.color) {
  15324. return d.color;
  15325. }
  15326. else if (groupColorByParent) {
  15327. return color((d.children ? d : d.parent).name);
  15328. }
  15329. else {
  15330. return color(d.name);
  15331. }
  15332. })
  15333. .style("stroke", "#FFF")
  15334. .on("click", function(d,i){
  15335. zoomClick(d);
  15336. dispatch.elementClick({
  15337. data: d,
  15338. index: i
  15339. })
  15340. })
  15341. .on('mouseover', function(d,i){
  15342. d3.select(this).classed('hover', true).style('opacity', 0.8);
  15343. dispatch.elementMouseover({
  15344. data: d,
  15345. color: d3.select(this).style("fill"),
  15346. percent: computeNodePercentage(d)
  15347. });
  15348. })
  15349. .on('mouseout', function(d,i){
  15350. d3.select(this).classed('hover', false).style('opacity', 1);
  15351. dispatch.elementMouseout({
  15352. data: d
  15353. });
  15354. })
  15355. .on('mousemove', function(d,i){
  15356. dispatch.elementMousemove({
  15357. data: d
  15358. });
  15359. });
  15360. ///Iterating via each and selecting based on the this
  15361. ///makes it work ... a cG.selectAll('path') doesn't.
  15362. ///Without iteration the data (in the element) didn't update.
  15363. cG.each(function(d){
  15364. d3.select(this).select('path')
  15365. .transition()
  15366. .duration(duration)
  15367. .attrTween('d', arcTweenUpdate);
  15368. });
  15369. if(showLabels){
  15370. //remove labels first and add them back
  15371. cG.selectAll('text').remove();
  15372. //this way labels are on top of newly added arcs
  15373. cG.append('text')
  15374. .text( function(e){ return labelFormat(e)})
  15375. .transition()
  15376. .duration(duration)
  15377. .attr("opacity", function(d){
  15378. if(labelThresholdMatched(d)) {
  15379. return 1;
  15380. }
  15381. else {
  15382. return 0;
  15383. }
  15384. })
  15385. .attr("transform", function(d) {
  15386. var width = this.getBBox().width;
  15387. if(d.depth === 0){
  15388. return "rotate(0)translate(" + (width / 2 * -1) + ",0)";
  15389. }
  15390. else {
  15391. var centerAngle = computeCenterAngle(d);
  15392. var rotation = rotationToAvoidUpsideDown(d);
  15393. if (rotation === 0) {
  15394. return 'rotate('+ centerAngle +')translate(' + (y(d.y) + 5) + ',0)';
  15395. }
  15396. else {
  15397. return 'rotate('+ centerAngle +')translate(' + (y(d.y) + width + 5) + ',0)rotate(' + rotation + ')';
  15398. }
  15399. }
  15400. });
  15401. }
  15402. //zoom out to the center when the data is updated.
  15403. zoomClick(nodes[nodes.length - 1])
  15404. //remove unmatched elements ...
  15405. cG.exit()
  15406. .transition()
  15407. .duration(duration)
  15408. .attr('opacity',0)
  15409. .each('end',function(d){
  15410. var k = key(d);
  15411. prevPositions[k] = undefined;
  15412. })
  15413. .remove();
  15414. });
  15415. renderWatch.renderEnd('sunburst immediate');
  15416. return chart;
  15417. }
  15418. //============================================================
  15419. // Expose Public Variables
  15420. //------------------------------------------------------------
  15421. chart.dispatch = dispatch;
  15422. chart.options = nv.utils.optionsFunc.bind(chart);
  15423. chart._options = Object.create({}, {
  15424. // simple options, just get/set the necessary values
  15425. width: {get: function(){return width;}, set: function(_){width=_;}},
  15426. height: {get: function(){return height;}, set: function(_){height=_;}},
  15427. mode: {get: function(){return mode;}, set: function(_){mode=_;}},
  15428. id: {get: function(){return id;}, set: function(_){id=_;}},
  15429. duration: {get: function(){return duration;}, set: function(_){duration=_;}},
  15430. groupColorByParent: {get: function(){return groupColorByParent;}, set: function(_){groupColorByParent=!!_;}},
  15431. showLabels: {get: function(){return showLabels;}, set: function(_){showLabels=!!_}},
  15432. labelFormat: {get: function(){return labelFormat;}, set: function(_){labelFormat=_}},
  15433. labelThreshold: {get: function(){return labelThreshold;}, set: function(_){labelThreshold=_}},
  15434. sort: {get: function(){return sort;}, set: function(_){sort=_}},
  15435. key: {get: function(){return key;}, set: function(_){key=_}},
  15436. // options that require extra logic in the setter
  15437. margin: {get: function(){return margin;}, set: function(_){
  15438. margin.top = _.top != undefined ? _.top : margin.top;
  15439. margin.right = _.right != undefined ? _.right : margin.right;
  15440. margin.bottom = _.bottom != undefined ? _.bottom : margin.bottom;
  15441. margin.left = _.left != undefined ? _.left : margin.left;
  15442. }},
  15443. color: {get: function(){return color;}, set: function(_){
  15444. color=nv.utils.getColor(_);
  15445. }}
  15446. });
  15447. nv.utils.initOptions(chart);
  15448. return chart;
  15449. };
  15450. nv.models.sunburstChart = function() {
  15451. "use strict";
  15452. //============================================================
  15453. // Public Variables with Default Settings
  15454. //------------------------------------------------------------
  15455. var sunburst = nv.models.sunburst();
  15456. var tooltip = nv.models.tooltip();
  15457. var margin = {top: 30, right: 20, bottom: 20, left: 20}
  15458. , width = null
  15459. , height = null
  15460. , color = nv.utils.defaultColor()
  15461. , showTooltipPercent = false
  15462. , id = Math.round(Math.random() * 100000)
  15463. , defaultState = null
  15464. , noData = null
  15465. , duration = 250
  15466. , dispatch = d3.dispatch('stateChange', 'changeState','renderEnd');
  15467. //============================================================
  15468. // Private Variables
  15469. //------------------------------------------------------------
  15470. var renderWatch = nv.utils.renderWatch(dispatch);
  15471. tooltip
  15472. .duration(0)
  15473. .headerEnabled(false)
  15474. .valueFormatter(function(d){return d;});
  15475. //============================================================
  15476. // Chart function
  15477. //------------------------------------------------------------
  15478. function chart(selection) {
  15479. renderWatch.reset();
  15480. renderWatch.models(sunburst);
  15481. selection.each(function(data) {
  15482. var container = d3.select(this);
  15483. nv.utils.initSVG(container);
  15484. var availableWidth = nv.utils.availableWidth(width, container, margin);
  15485. var availableHeight = nv.utils.availableHeight(height, container, margin);
  15486. chart.update = function() {
  15487. if (duration === 0) {
  15488. container.call(chart);
  15489. } else {
  15490. container.transition().duration(duration).call(chart);
  15491. }
  15492. };
  15493. chart.container = container;
  15494. // Display No Data message if there's nothing to show.
  15495. if (!data || !data.length) {
  15496. nv.utils.noData(chart, container);
  15497. return chart;
  15498. } else {
  15499. container.selectAll('.nv-noData').remove();
  15500. }
  15501. sunburst.width(availableWidth).height(availableHeight).margin(margin);
  15502. container.call(sunburst);
  15503. });
  15504. renderWatch.renderEnd('sunburstChart immediate');
  15505. return chart;
  15506. }
  15507. //============================================================
  15508. // Event Handling/Dispatching (out of chart's scope)
  15509. //------------------------------------------------------------
  15510. sunburst.dispatch.on('elementMouseover.tooltip', function(evt) {
  15511. evt.series = {
  15512. key: evt.data.name,
  15513. value: (evt.data.value || evt.data.size),
  15514. color: evt.color,
  15515. percent: evt.percent
  15516. };
  15517. if (!showTooltipPercent) {
  15518. delete evt.percent;
  15519. delete evt.series.percent;
  15520. }
  15521. tooltip.data(evt).hidden(false);
  15522. });
  15523. sunburst.dispatch.on('elementMouseout.tooltip', function(evt) {
  15524. tooltip.hidden(true);
  15525. });
  15526. sunburst.dispatch.on('elementMousemove.tooltip', function(evt) {
  15527. tooltip();
  15528. });
  15529. //============================================================
  15530. // Expose Public Variables
  15531. //------------------------------------------------------------
  15532. // expose chart's sub-components
  15533. chart.dispatch = dispatch;
  15534. chart.sunburst = sunburst;
  15535. chart.tooltip = tooltip;
  15536. chart.options = nv.utils.optionsFunc.bind(chart);
  15537. // use Object get/set functionality to map between vars and chart functions
  15538. chart._options = Object.create({}, {
  15539. // simple options, just get/set the necessary values
  15540. noData: {get: function(){return noData;}, set: function(_){noData=_;}},
  15541. defaultState: {get: function(){return defaultState;}, set: function(_){defaultState=_;}},
  15542. showTooltipPercent: {get: function(){return showTooltipPercent;}, set: function(_){showTooltipPercent=_;}},
  15543. // options that require extra logic in the setter
  15544. color: {get: function(){return color;}, set: function(_){
  15545. color = _;
  15546. sunburst.color(color);
  15547. }},
  15548. duration: {get: function(){return duration;}, set: function(_){
  15549. duration = _;
  15550. renderWatch.reset(duration);
  15551. sunburst.duration(duration);
  15552. }},
  15553. margin: {get: function(){return margin;}, set: function(_){
  15554. margin.top = _.top !== undefined ? _.top : margin.top;
  15555. margin.right = _.right !== undefined ? _.right : margin.right;
  15556. margin.bottom = _.bottom !== undefined ? _.bottom : margin.bottom;
  15557. margin.left = _.left !== undefined ? _.left : margin.left;
  15558. sunburst.margin(margin);
  15559. }}
  15560. });
  15561. nv.utils.inheritOptions(chart, sunburst);
  15562. nv.utils.initOptions(chart);
  15563. return chart;
  15564. };
  15565. nv.version = "1.8.6-dev";
  15566. })();