c3.esm.js 378 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027
  1. /* @license C3.js v0.7.18 | (c) C3 Team and other contributors | http://c3js.org/ */
  2. import * as d3 from 'd3';
  3. function ChartInternal(api) {
  4. var $$ = this;
  5. // Note: This part will be replaced by rollup-plugin-modify
  6. // When bundling esm output. Beware of changing this line.
  7. // TODO: Maybe we should check that the modification by rollup-plugin-modify
  8. // is valid during unit tests.
  9. $$.d3 = window.d3
  10. ? window.d3
  11. : typeof require !== 'undefined'
  12. ? require('d3')
  13. : undefined;
  14. $$.api = api;
  15. $$.config = $$.getDefaultConfig();
  16. $$.data = {};
  17. $$.cache = {};
  18. $$.axes = {};
  19. }
  20. /**
  21. * The Chart class
  22. *
  23. * The methods of this class is the public APIs of the chart object.
  24. */
  25. function Chart(config) {
  26. this.internal = new ChartInternal(this);
  27. this.internal.loadConfig(config);
  28. this.internal.beforeInit(config);
  29. this.internal.init();
  30. this.internal.afterInit(config)
  31. // bind "this" to nested API
  32. ;(function bindThis(fn, target, argThis) {
  33. Object.keys(fn).forEach(function(key) {
  34. target[key] = fn[key].bind(argThis);
  35. if (Object.keys(fn[key]).length > 0) {
  36. bindThis(fn[key], target[key], argThis);
  37. }
  38. });
  39. })(Chart.prototype, this, this);
  40. }
  41. var asHalfPixel = function(n) {
  42. return Math.ceil(n) + 0.5
  43. };
  44. var ceil10 = function(v) {
  45. return Math.ceil(v / 10) * 10
  46. };
  47. var diffDomain = function(d) {
  48. return d[1] - d[0]
  49. };
  50. var getOption = function(options, key, defaultValue) {
  51. return isDefined(options[key]) ? options[key] : defaultValue
  52. };
  53. var getPathBox = function(path) {
  54. var box = getBBox(path),
  55. items = [path.pathSegList.getItem(0), path.pathSegList.getItem(1)],
  56. minX = items[0].x,
  57. minY = Math.min(items[0].y, items[1].y);
  58. return { x: minX, y: minY, width: box.width, height: box.height }
  59. };
  60. var getBBox = function(element) {
  61. try {
  62. return element.getBBox()
  63. } catch (ignore) {
  64. // Firefox will throw an exception if getBBox() is called whereas the
  65. // element is rendered with display:none
  66. // See https://github.com/c3js/c3/issues/2692
  67. // The previous code was using `getBoundingClientRect` which was returning
  68. // everything at 0 in this case so let's reproduce this behavior here.
  69. return { x: 0, y: 0, width: 0, height: 0 }
  70. }
  71. };
  72. var hasValue = function(dict, value) {
  73. var found = false;
  74. Object.keys(dict).forEach(function(key) {
  75. if (dict[key] === value) {
  76. found = true;
  77. }
  78. });
  79. return found
  80. };
  81. var isArray = function(o) {
  82. return Array.isArray(o)
  83. };
  84. var isDefined = function(v) {
  85. return typeof v !== 'undefined'
  86. };
  87. var isEmpty = function(o) {
  88. return (
  89. typeof o === 'undefined' ||
  90. o === null ||
  91. (isString(o) && o.length === 0) ||
  92. (typeof o === 'object' && Object.keys(o).length === 0)
  93. )
  94. };
  95. var isFunction = function(o) {
  96. return typeof o === 'function'
  97. };
  98. var isNumber = function(o) {
  99. return typeof o === 'number'
  100. };
  101. var isString = function(o) {
  102. return typeof o === 'string'
  103. };
  104. var isUndefined = function(v) {
  105. return typeof v === 'undefined'
  106. };
  107. var isValue = function(v) {
  108. return v || v === 0
  109. };
  110. var notEmpty = function(o) {
  111. return !isEmpty(o)
  112. };
  113. var sanitise = function(str) {
  114. return typeof str === 'string'
  115. ? str.replace(/</g, '&lt;').replace(/>/g, '&gt;')
  116. : str
  117. };
  118. var flattenArray = function(arr) {
  119. return Array.isArray(arr) ? [].concat(...arr) : []
  120. };
  121. /**
  122. * Returns whether the point is within the given box.
  123. *
  124. * @param {Array} point An [x,y] coordinate
  125. * @param {Object} box An object with {x, y, width, height} keys
  126. * @param {Number} sensitivity An offset to ease check on very small boxes
  127. */
  128. var isWithinBox = function(point, box, sensitivity = 0) {
  129. const xStart = box.x - sensitivity;
  130. const xEnd = box.x + box.width + sensitivity;
  131. const yStart = box.y + box.height + sensitivity;
  132. const yEnd = box.y - sensitivity;
  133. return (
  134. xStart < point[0] && point[0] < xEnd && yEnd < point[1] && point[1] < yStart
  135. )
  136. };
  137. /**
  138. * Returns Internet Explorer version number (or false if no Internet Explorer used).
  139. *
  140. * @param string agent Optional parameter to specify user agent
  141. */
  142. var getIEVersion = function(agent) {
  143. // https://stackoverflow.com/questions/19999388/check-if-user-is-using-ie
  144. if (typeof agent === 'undefined') {
  145. agent = window.navigator.userAgent;
  146. }
  147. let pos = agent.indexOf('MSIE '); // up to IE10
  148. if (pos > 0) {
  149. return parseInt(agent.substring(pos + 5, agent.indexOf('.', pos)), 10)
  150. }
  151. pos = agent.indexOf('Trident/'); // IE11
  152. if (pos > 0) {
  153. pos = agent.indexOf('rv:');
  154. return parseInt(agent.substring(pos + 3, agent.indexOf('.', pos)), 10)
  155. }
  156. return false
  157. };
  158. /**
  159. * Returns whether the used browser is Internet Explorer.
  160. *
  161. * @param {Number} version Optional parameter to specify IE version
  162. */
  163. var isIE = function(version) {
  164. const ver = getIEVersion();
  165. if (typeof version === 'undefined') {
  166. return !!ver
  167. }
  168. return version === ver
  169. };
  170. function AxisInternal(component, params) {
  171. var internal = this;
  172. internal.component = component;
  173. internal.params = params || {};
  174. internal.d3 = component.d3;
  175. internal.scale = internal.d3.scaleLinear();
  176. internal.range;
  177. internal.orient = 'bottom';
  178. internal.innerTickSize = 6;
  179. internal.outerTickSize = this.params.withOuterTick ? 6 : 0;
  180. internal.tickPadding = 3;
  181. internal.tickValues = null;
  182. internal.tickFormat;
  183. internal.tickArguments;
  184. internal.tickOffset = 0;
  185. internal.tickCulling = true;
  186. internal.tickCentered;
  187. internal.tickTextCharSize;
  188. internal.tickTextRotate = internal.params.tickTextRotate;
  189. internal.tickLength;
  190. internal.axis = internal.generateAxis();
  191. }
  192. AxisInternal.prototype.axisX = function(selection, x, tickOffset) {
  193. selection.attr('transform', function(d) {
  194. return 'translate(' + Math.ceil(x(d) + tickOffset) + ', 0)'
  195. });
  196. };
  197. AxisInternal.prototype.axisY = function(selection, y) {
  198. selection.attr('transform', function(d) {
  199. return 'translate(0,' + Math.ceil(y(d)) + ')'
  200. });
  201. };
  202. AxisInternal.prototype.scaleExtent = function(domain) {
  203. var start = domain[0],
  204. stop = domain[domain.length - 1];
  205. return start < stop ? [start, stop] : [stop, start]
  206. };
  207. AxisInternal.prototype.generateTicks = function(scale) {
  208. var internal = this;
  209. var i,
  210. domain,
  211. ticks = [];
  212. if (scale.ticks) {
  213. return scale.ticks.apply(scale, internal.tickArguments)
  214. }
  215. domain = scale.domain();
  216. for (i = Math.ceil(domain[0]); i < domain[1]; i++) {
  217. ticks.push(i);
  218. }
  219. if (ticks.length > 0 && ticks[0] > 0) {
  220. ticks.unshift(ticks[0] - (ticks[1] - ticks[0]));
  221. }
  222. return ticks
  223. };
  224. AxisInternal.prototype.copyScale = function() {
  225. var internal = this;
  226. var newScale = internal.scale.copy(),
  227. domain;
  228. if (internal.params.isCategory) {
  229. domain = internal.scale.domain();
  230. newScale.domain([domain[0], domain[1] - 1]);
  231. }
  232. return newScale
  233. };
  234. AxisInternal.prototype.textFormatted = function(v) {
  235. var internal = this,
  236. formatted = internal.tickFormat ? internal.tickFormat(v) : v;
  237. return typeof formatted !== 'undefined' ? formatted : ''
  238. };
  239. AxisInternal.prototype.updateRange = function() {
  240. var internal = this;
  241. internal.range = internal.scale.rangeExtent
  242. ? internal.scale.rangeExtent()
  243. : internal.scaleExtent(internal.scale.range());
  244. return internal.range
  245. };
  246. AxisInternal.prototype.updateTickTextCharSize = function(tick) {
  247. var internal = this;
  248. if (internal.tickTextCharSize) {
  249. return internal.tickTextCharSize
  250. }
  251. var size = {
  252. h: 11.5,
  253. w: 5.5
  254. };
  255. tick
  256. .select('text')
  257. .text(function(d) {
  258. return internal.textFormatted(d)
  259. })
  260. .each(function(d) {
  261. var box = getBBox(this),
  262. text = internal.textFormatted(d),
  263. h = box.height,
  264. w = text ? box.width / text.length : undefined;
  265. if (h && w) {
  266. size.h = h;
  267. size.w = w;
  268. }
  269. })
  270. .text('');
  271. internal.tickTextCharSize = size;
  272. return size
  273. };
  274. AxisInternal.prototype.isVertical = function() {
  275. return this.orient === 'left' || this.orient === 'right'
  276. };
  277. AxisInternal.prototype.tspanData = function(d, i, scale) {
  278. var internal = this;
  279. var splitted = internal.params.tickMultiline
  280. ? internal.splitTickText(d, scale)
  281. : [].concat(internal.textFormatted(d));
  282. if (internal.params.tickMultiline && internal.params.tickMultilineMax > 0) {
  283. splitted = internal.ellipsify(splitted, internal.params.tickMultilineMax);
  284. }
  285. return splitted.map(function(s) {
  286. return { index: i, splitted: s, length: splitted.length }
  287. })
  288. };
  289. AxisInternal.prototype.splitTickText = function(d, scale) {
  290. var internal = this,
  291. tickText = internal.textFormatted(d),
  292. maxWidth = internal.params.tickWidth,
  293. subtext,
  294. spaceIndex,
  295. textWidth,
  296. splitted = [];
  297. if (Object.prototype.toString.call(tickText) === '[object Array]') {
  298. return tickText
  299. }
  300. if (!maxWidth || maxWidth <= 0) {
  301. maxWidth = internal.isVertical()
  302. ? 95
  303. : internal.params.isCategory
  304. ? Math.ceil(scale(1) - scale(0)) - 12
  305. : 110;
  306. }
  307. function split(splitted, text) {
  308. spaceIndex = undefined;
  309. for (var i = 1; i < text.length; i++) {
  310. if (text.charAt(i) === ' ') {
  311. spaceIndex = i;
  312. }
  313. subtext = text.substr(0, i + 1);
  314. textWidth = internal.tickTextCharSize.w * subtext.length;
  315. // if text width gets over tick width, split by space index or crrent index
  316. if (maxWidth < textWidth) {
  317. return split(
  318. splitted.concat(text.substr(0, spaceIndex ? spaceIndex : i)),
  319. text.slice(spaceIndex ? spaceIndex + 1 : i)
  320. )
  321. }
  322. }
  323. return splitted.concat(text)
  324. }
  325. return split(splitted, tickText + '')
  326. };
  327. AxisInternal.prototype.ellipsify = function(splitted, max) {
  328. if (splitted.length <= max) {
  329. return splitted
  330. }
  331. var ellipsified = splitted.slice(0, max);
  332. var remaining = 3;
  333. for (var i = max - 1; i >= 0; i--) {
  334. var available = ellipsified[i].length;
  335. ellipsified[i] = ellipsified[i]
  336. .substr(0, available - remaining)
  337. .padEnd(available, '.');
  338. remaining -= available;
  339. if (remaining <= 0) {
  340. break
  341. }
  342. }
  343. return ellipsified
  344. };
  345. AxisInternal.prototype.updateTickLength = function() {
  346. var internal = this;
  347. internal.tickLength =
  348. Math.max(internal.innerTickSize, 0) + internal.tickPadding;
  349. };
  350. AxisInternal.prototype.lineY2 = function(d) {
  351. var internal = this,
  352. tickPosition =
  353. internal.scale(d) + (internal.tickCentered ? 0 : internal.tickOffset);
  354. return internal.range[0] < tickPosition && tickPosition < internal.range[1]
  355. ? internal.innerTickSize
  356. : 0
  357. };
  358. AxisInternal.prototype.textY = function() {
  359. var internal = this,
  360. rotate = internal.tickTextRotate;
  361. return rotate
  362. ? 11.5 - 2.5 * (rotate / 15) * (rotate > 0 ? 1 : -1)
  363. : internal.tickLength
  364. };
  365. AxisInternal.prototype.textTransform = function() {
  366. var internal = this,
  367. rotate = internal.tickTextRotate;
  368. return rotate ? 'rotate(' + rotate + ')' : ''
  369. };
  370. AxisInternal.prototype.textTextAnchor = function() {
  371. var internal = this,
  372. rotate = internal.tickTextRotate;
  373. return rotate ? (rotate > 0 ? 'start' : 'end') : 'middle'
  374. };
  375. AxisInternal.prototype.tspanDx = function() {
  376. var internal = this,
  377. rotate = internal.tickTextRotate;
  378. return rotate ? 8 * Math.sin(Math.PI * (rotate / 180)) : 0
  379. };
  380. AxisInternal.prototype.tspanDy = function(d, i) {
  381. var internal = this,
  382. dy = internal.tickTextCharSize.h;
  383. if (i === 0) {
  384. if (internal.isVertical()) {
  385. dy = -((d.length - 1) * (internal.tickTextCharSize.h / 2) - 3);
  386. } else {
  387. dy = '.71em';
  388. }
  389. }
  390. return dy
  391. };
  392. AxisInternal.prototype.generateAxis = function() {
  393. var internal = this,
  394. d3 = internal.d3,
  395. params = internal.params;
  396. function axis(g, transition) {
  397. var self;
  398. g.each(function() {
  399. var g = (axis.g = d3.select(this));
  400. var scale0 = this.__chart__ || internal.scale,
  401. scale1 = (this.__chart__ = internal.copyScale());
  402. var ticksValues = internal.tickValues
  403. ? internal.tickValues
  404. : internal.generateTicks(scale1),
  405. ticks = g.selectAll('.tick').data(ticksValues, scale1),
  406. tickEnter = ticks
  407. .enter()
  408. .insert('g', '.domain')
  409. .attr('class', 'tick')
  410. .style('opacity', 1e-6),
  411. // MEMO: No exit transition. The reason is this transition affects max tick width calculation because old tick will be included in the ticks.
  412. tickExit = ticks.exit().remove(),
  413. tickUpdate = ticks.merge(tickEnter),
  414. tickTransform,
  415. tickX,
  416. tickY;
  417. if (params.isCategory) {
  418. internal.tickOffset = Math.ceil((scale1(1) - scale1(0)) / 2);
  419. tickX = internal.tickCentered ? 0 : internal.tickOffset;
  420. tickY = internal.tickCentered ? internal.tickOffset : 0;
  421. } else {
  422. internal.tickOffset = tickX = 0;
  423. }
  424. internal.updateRange();
  425. internal.updateTickLength();
  426. internal.updateTickTextCharSize(g.select('.tick'));
  427. var lineUpdate = tickUpdate
  428. .select('line')
  429. .merge(tickEnter.append('line')),
  430. textUpdate = tickUpdate.select('text').merge(tickEnter.append('text'));
  431. var tspans = tickUpdate
  432. .selectAll('text')
  433. .selectAll('tspan')
  434. .data(function(d, i) {
  435. return internal.tspanData(d, i, scale1)
  436. }),
  437. tspanEnter = tspans.enter().append('tspan'),
  438. tspanUpdate = tspanEnter.merge(tspans).text(function(d) {
  439. return d.splitted
  440. });
  441. tspans.exit().remove();
  442. var path = g.selectAll('.domain').data([0]),
  443. pathUpdate = path
  444. .enter()
  445. .append('path')
  446. .merge(path)
  447. .attr('class', 'domain');
  448. // TODO: each attr should be one function and change its behavior by internal.orient, probably
  449. switch (internal.orient) {
  450. case 'bottom': {
  451. tickTransform = internal.axisX;
  452. lineUpdate
  453. .attr('x1', tickX)
  454. .attr('x2', tickX)
  455. .attr('y2', function(d, i) {
  456. return internal.lineY2(d, i)
  457. });
  458. textUpdate
  459. .attr('x', 0)
  460. .attr('y', function(d, i) {
  461. return internal.textY(d, i)
  462. })
  463. .attr('transform', function(d, i) {
  464. return internal.textTransform(d, i)
  465. })
  466. .style('text-anchor', function(d, i) {
  467. return internal.textTextAnchor(d, i)
  468. });
  469. tspanUpdate
  470. .attr('x', 0)
  471. .attr('dy', function(d, i) {
  472. return internal.tspanDy(d, i)
  473. })
  474. .attr('dx', function(d, i) {
  475. return internal.tspanDx(d, i)
  476. });
  477. pathUpdate.attr(
  478. 'd',
  479. 'M' +
  480. internal.range[0] +
  481. ',' +
  482. internal.outerTickSize +
  483. 'V0H' +
  484. internal.range[1] +
  485. 'V' +
  486. internal.outerTickSize
  487. );
  488. break
  489. }
  490. case 'top': {
  491. // TODO: rotated tick text
  492. tickTransform = internal.axisX;
  493. lineUpdate
  494. .attr('x1', tickX)
  495. .attr('x2', tickX)
  496. .attr('y2', function(d, i) {
  497. return -1 * internal.lineY2(d, i)
  498. });
  499. textUpdate
  500. .attr('x', 0)
  501. .attr('y', function(d, i) {
  502. return (
  503. -1 * internal.textY(d, i) -
  504. (params.isCategory ? 2 : internal.tickLength - 2)
  505. )
  506. })
  507. .attr('transform', function(d, i) {
  508. return internal.textTransform(d, i)
  509. })
  510. .style('text-anchor', function(d, i) {
  511. return internal.textTextAnchor(d, i)
  512. });
  513. tspanUpdate
  514. .attr('x', 0)
  515. .attr('dy', function(d, i) {
  516. return internal.tspanDy(d, i)
  517. })
  518. .attr('dx', function(d, i) {
  519. return internal.tspanDx(d, i)
  520. });
  521. pathUpdate.attr(
  522. 'd',
  523. 'M' +
  524. internal.range[0] +
  525. ',' +
  526. -internal.outerTickSize +
  527. 'V0H' +
  528. internal.range[1] +
  529. 'V' +
  530. -internal.outerTickSize
  531. );
  532. break
  533. }
  534. case 'left': {
  535. tickTransform = internal.axisY;
  536. lineUpdate
  537. .attr('x2', -internal.innerTickSize)
  538. .attr('y1', tickY)
  539. .attr('y2', tickY);
  540. textUpdate
  541. .attr('x', -internal.tickLength)
  542. .attr('y', internal.tickOffset)
  543. .style('text-anchor', 'end');
  544. tspanUpdate
  545. .attr('x', -internal.tickLength)
  546. .attr('dy', function(d, i) {
  547. return internal.tspanDy(d, i)
  548. });
  549. pathUpdate.attr(
  550. 'd',
  551. 'M' +
  552. -internal.outerTickSize +
  553. ',' +
  554. internal.range[0] +
  555. 'H0V' +
  556. internal.range[1] +
  557. 'H' +
  558. -internal.outerTickSize
  559. );
  560. break
  561. }
  562. case 'right': {
  563. tickTransform = internal.axisY;
  564. lineUpdate
  565. .attr('x2', internal.innerTickSize)
  566. .attr('y1', tickY)
  567. .attr('y2', tickY);
  568. textUpdate
  569. .attr('x', internal.tickLength)
  570. .attr('y', internal.tickOffset)
  571. .style('text-anchor', 'start');
  572. tspanUpdate.attr('x', internal.tickLength).attr('dy', function(d, i) {
  573. return internal.tspanDy(d, i)
  574. });
  575. pathUpdate.attr(
  576. 'd',
  577. 'M' +
  578. internal.outerTickSize +
  579. ',' +
  580. internal.range[0] +
  581. 'H0V' +
  582. internal.range[1] +
  583. 'H' +
  584. internal.outerTickSize
  585. );
  586. break
  587. }
  588. }
  589. if (scale1.rangeBand) {
  590. var x = scale1,
  591. dx = x.rangeBand() / 2;
  592. scale0 = scale1 = function(d) {
  593. return x(d) + dx
  594. };
  595. } else if (scale0.rangeBand) {
  596. scale0 = scale1;
  597. } else {
  598. tickExit.call(tickTransform, scale1, internal.tickOffset);
  599. }
  600. tickEnter.call(tickTransform, scale0, internal.tickOffset);
  601. self = (transition ? tickUpdate.transition(transition) : tickUpdate)
  602. .style('opacity', 1)
  603. .call(tickTransform, scale1, internal.tickOffset);
  604. });
  605. return self
  606. }
  607. axis.scale = function(x) {
  608. if (!arguments.length) {
  609. return internal.scale
  610. }
  611. internal.scale = x;
  612. return axis
  613. };
  614. axis.orient = function(x) {
  615. if (!arguments.length) {
  616. return internal.orient
  617. }
  618. internal.orient =
  619. x in { top: 1, right: 1, bottom: 1, left: 1 } ? x + '' : 'bottom';
  620. return axis
  621. };
  622. axis.tickFormat = function(format) {
  623. if (!arguments.length) {
  624. return internal.tickFormat
  625. }
  626. internal.tickFormat = format;
  627. return axis
  628. };
  629. axis.tickCentered = function(isCentered) {
  630. if (!arguments.length) {
  631. return internal.tickCentered
  632. }
  633. internal.tickCentered = isCentered;
  634. return axis
  635. };
  636. axis.tickOffset = function() {
  637. return internal.tickOffset
  638. };
  639. axis.tickInterval = function() {
  640. var interval, length;
  641. if (params.isCategory) {
  642. interval = internal.tickOffset * 2;
  643. } else {
  644. length =
  645. axis.g
  646. .select('path.domain')
  647. .node()
  648. .getTotalLength() -
  649. internal.outerTickSize * 2;
  650. interval = length / axis.g.selectAll('line').size();
  651. }
  652. return interval === Infinity ? 0 : interval
  653. };
  654. axis.ticks = function() {
  655. if (!arguments.length) {
  656. return internal.tickArguments
  657. }
  658. internal.tickArguments = arguments;
  659. return axis
  660. };
  661. axis.tickCulling = function(culling) {
  662. if (!arguments.length) {
  663. return internal.tickCulling
  664. }
  665. internal.tickCulling = culling;
  666. return axis
  667. };
  668. axis.tickValues = function(x) {
  669. if (typeof x === 'function') {
  670. internal.tickValues = function() {
  671. return x(internal.scale.domain())
  672. };
  673. } else {
  674. if (!arguments.length) {
  675. return internal.tickValues
  676. }
  677. internal.tickValues = x;
  678. }
  679. return axis
  680. };
  681. return axis
  682. };
  683. var CLASS = {
  684. target: 'c3-target',
  685. chart: 'c3-chart',
  686. chartLine: 'c3-chart-line',
  687. chartLines: 'c3-chart-lines',
  688. chartBar: 'c3-chart-bar',
  689. chartBars: 'c3-chart-bars',
  690. chartText: 'c3-chart-text',
  691. chartTexts: 'c3-chart-texts',
  692. chartArc: 'c3-chart-arc',
  693. chartArcs: 'c3-chart-arcs',
  694. chartArcsTitle: 'c3-chart-arcs-title',
  695. chartArcsBackground: 'c3-chart-arcs-background',
  696. chartArcsGaugeUnit: 'c3-chart-arcs-gauge-unit',
  697. chartArcsGaugeMax: 'c3-chart-arcs-gauge-max',
  698. chartArcsGaugeMin: 'c3-chart-arcs-gauge-min',
  699. selectedCircle: 'c3-selected-circle',
  700. selectedCircles: 'c3-selected-circles',
  701. eventRect: 'c3-event-rect',
  702. eventRects: 'c3-event-rects',
  703. eventRectsSingle: 'c3-event-rects-single',
  704. eventRectsMultiple: 'c3-event-rects-multiple',
  705. zoomRect: 'c3-zoom-rect',
  706. brush: 'c3-brush',
  707. dragZoom: 'c3-drag-zoom',
  708. focused: 'c3-focused',
  709. defocused: 'c3-defocused',
  710. region: 'c3-region',
  711. regions: 'c3-regions',
  712. title: 'c3-title',
  713. tooltipContainer: 'c3-tooltip-container',
  714. tooltip: 'c3-tooltip',
  715. tooltipName: 'c3-tooltip-name',
  716. shape: 'c3-shape',
  717. shapes: 'c3-shapes',
  718. line: 'c3-line',
  719. lines: 'c3-lines',
  720. bar: 'c3-bar',
  721. bars: 'c3-bars',
  722. circle: 'c3-circle',
  723. circles: 'c3-circles',
  724. arc: 'c3-arc',
  725. arcLabelLine: 'c3-arc-label-line',
  726. arcs: 'c3-arcs',
  727. area: 'c3-area',
  728. areas: 'c3-areas',
  729. empty: 'c3-empty',
  730. text: 'c3-text',
  731. texts: 'c3-texts',
  732. gaugeValue: 'c3-gauge-value',
  733. grid: 'c3-grid',
  734. gridLines: 'c3-grid-lines',
  735. xgrid: 'c3-xgrid',
  736. xgrids: 'c3-xgrids',
  737. xgridLine: 'c3-xgrid-line',
  738. xgridLines: 'c3-xgrid-lines',
  739. xgridFocus: 'c3-xgrid-focus',
  740. ygrid: 'c3-ygrid',
  741. ygrids: 'c3-ygrids',
  742. ygridLine: 'c3-ygrid-line',
  743. ygridLines: 'c3-ygrid-lines',
  744. colorScale: 'c3-colorscale',
  745. stanfordElements: 'c3-stanford-elements',
  746. stanfordLine: 'c3-stanford-line',
  747. stanfordLines: 'c3-stanford-lines',
  748. stanfordRegion: 'c3-stanford-region',
  749. stanfordRegions: 'c3-stanford-regions',
  750. stanfordText: 'c3-stanford-text',
  751. stanfordTexts: 'c3-stanford-texts',
  752. axis: 'c3-axis',
  753. axisX: 'c3-axis-x',
  754. axisXLabel: 'c3-axis-x-label',
  755. axisY: 'c3-axis-y',
  756. axisYLabel: 'c3-axis-y-label',
  757. axisY2: 'c3-axis-y2',
  758. axisY2Label: 'c3-axis-y2-label',
  759. legendBackground: 'c3-legend-background',
  760. legendItem: 'c3-legend-item',
  761. legendItemEvent: 'c3-legend-item-event',
  762. legendItemTile: 'c3-legend-item-tile',
  763. legendItemHidden: 'c3-legend-item-hidden',
  764. legendItemFocused: 'c3-legend-item-focused',
  765. dragarea: 'c3-dragarea',
  766. EXPANDED: '_expanded_',
  767. SELECTED: '_selected_',
  768. INCLUDED: '_included_'
  769. };
  770. class Axis {
  771. constructor(owner) {
  772. this.owner = owner;
  773. this.d3 = owner.d3;
  774. this.internal = AxisInternal;
  775. }
  776. }
  777. Axis.prototype.init = function init() {
  778. var $$ = this.owner,
  779. config = $$.config,
  780. main = $$.main;
  781. $$.axes.x = main
  782. .append('g')
  783. .attr('class', CLASS.axis + ' ' + CLASS.axisX)
  784. .attr('clip-path', config.axis_x_inner ? '' : $$.clipPathForXAxis)
  785. .attr('transform', $$.getTranslate('x'))
  786. .style('visibility', config.axis_x_show ? 'visible' : 'hidden');
  787. $$.axes.x
  788. .append('text')
  789. .attr('class', CLASS.axisXLabel)
  790. .attr('transform', config.axis_rotated ? 'rotate(-90)' : '')
  791. .style('text-anchor', this.textAnchorForXAxisLabel.bind(this));
  792. $$.axes.y = main
  793. .append('g')
  794. .attr('class', CLASS.axis + ' ' + CLASS.axisY)
  795. .attr('clip-path', config.axis_y_inner ? '' : $$.clipPathForYAxis)
  796. .attr('transform', $$.getTranslate('y'))
  797. .style('visibility', config.axis_y_show ? 'visible' : 'hidden');
  798. $$.axes.y
  799. .append('text')
  800. .attr('class', CLASS.axisYLabel)
  801. .attr('transform', config.axis_rotated ? '' : 'rotate(-90)')
  802. .style('text-anchor', this.textAnchorForYAxisLabel.bind(this));
  803. $$.axes.y2 = main
  804. .append('g')
  805. .attr('class', CLASS.axis + ' ' + CLASS.axisY2)
  806. // clip-path?
  807. .attr('transform', $$.getTranslate('y2'))
  808. .style('visibility', config.axis_y2_show ? 'visible' : 'hidden');
  809. $$.axes.y2
  810. .append('text')
  811. .attr('class', CLASS.axisY2Label)
  812. .attr('transform', config.axis_rotated ? '' : 'rotate(-90)')
  813. .style('text-anchor', this.textAnchorForY2AxisLabel.bind(this));
  814. };
  815. Axis.prototype.getXAxis = function getXAxis(
  816. scale,
  817. orient,
  818. tickFormat,
  819. tickValues,
  820. withOuterTick,
  821. withoutTransition,
  822. withoutRotateTickText
  823. ) {
  824. var $$ = this.owner,
  825. config = $$.config,
  826. axisParams = {
  827. isCategory: $$.isCategorized(),
  828. withOuterTick: withOuterTick,
  829. tickMultiline: config.axis_x_tick_multiline,
  830. tickMultilineMax: config.axis_x_tick_multiline
  831. ? Number(config.axis_x_tick_multilineMax)
  832. : 0,
  833. tickWidth: config.axis_x_tick_width,
  834. tickTextRotate: withoutRotateTickText ? 0 : config.axis_x_tick_rotate,
  835. withoutTransition: withoutTransition
  836. },
  837. axis = new this.internal(this, axisParams).axis.scale(scale).orient(orient);
  838. if ($$.isTimeSeries() && tickValues && typeof tickValues !== 'function') {
  839. tickValues = tickValues.map(function(v) {
  840. return $$.parseDate(v)
  841. });
  842. }
  843. // Set tick
  844. axis.tickFormat(tickFormat).tickValues(tickValues);
  845. if ($$.isCategorized()) {
  846. axis.tickCentered(config.axis_x_tick_centered);
  847. if (isEmpty(config.axis_x_tick_culling)) {
  848. config.axis_x_tick_culling = false;
  849. }
  850. }
  851. return axis
  852. };
  853. Axis.prototype.updateXAxisTickValues = function updateXAxisTickValues(
  854. targets,
  855. axis
  856. ) {
  857. var $$ = this.owner,
  858. config = $$.config,
  859. tickValues;
  860. if (config.axis_x_tick_fit || config.axis_x_tick_count) {
  861. tickValues = this.generateTickValues(
  862. $$.mapTargetsToUniqueXs(targets),
  863. config.axis_x_tick_count,
  864. $$.isTimeSeries()
  865. );
  866. }
  867. if (axis) {
  868. axis.tickValues(tickValues);
  869. } else {
  870. $$.xAxis.tickValues(tickValues);
  871. $$.subXAxis.tickValues(tickValues);
  872. }
  873. return tickValues
  874. };
  875. Axis.prototype.getYAxis = function getYAxis(
  876. axisId,
  877. scale,
  878. orient,
  879. tickValues,
  880. withOuterTick,
  881. withoutTransition,
  882. withoutRotateTickText
  883. ) {
  884. const $$ = this.owner;
  885. const config = $$.config;
  886. let tickFormat = config[`axis_${axisId}_tick_format`];
  887. if (!tickFormat && $$.isAxisNormalized(axisId)) {
  888. tickFormat = x => `${x}%`;
  889. }
  890. const axis = new this.internal(this, {
  891. withOuterTick: withOuterTick,
  892. withoutTransition: withoutTransition,
  893. tickTextRotate: withoutRotateTickText ? 0 : config.axis_y_tick_rotate
  894. }).axis
  895. .scale(scale)
  896. .orient(orient);
  897. if (tickFormat) {
  898. axis.tickFormat(tickFormat);
  899. }
  900. if ($$.isTimeSeriesY()) {
  901. axis.ticks(config.axis_y_tick_time_type, config.axis_y_tick_time_interval);
  902. } else {
  903. axis.tickValues(tickValues);
  904. }
  905. return axis
  906. };
  907. Axis.prototype.getId = function getId(id) {
  908. var config = this.owner.config;
  909. return id in config.data_axes ? config.data_axes[id] : 'y'
  910. };
  911. Axis.prototype.getXAxisTickFormat = function getXAxisTickFormat() {
  912. // #2251 previously set any negative values to a whole number,
  913. // however both should be truncated according to the users format specification
  914. var $$ = this.owner,
  915. config = $$.config;
  916. let format = $$.isTimeSeries()
  917. ? $$.defaultAxisTimeFormat
  918. : $$.isCategorized()
  919. ? $$.categoryName
  920. : function(v) {
  921. return v
  922. };
  923. if (config.axis_x_tick_format) {
  924. if (isFunction(config.axis_x_tick_format)) {
  925. format = config.axis_x_tick_format;
  926. } else if ($$.isTimeSeries()) {
  927. format = function(date) {
  928. return date ? $$.axisTimeFormat(config.axis_x_tick_format)(date) : ''
  929. };
  930. }
  931. }
  932. return isFunction(format)
  933. ? function(v) {
  934. return format.call($$, v)
  935. }
  936. : format
  937. };
  938. Axis.prototype.getTickValues = function getTickValues(tickValues, axis) {
  939. return tickValues ? tickValues : axis ? axis.tickValues() : undefined
  940. };
  941. Axis.prototype.getXAxisTickValues = function getXAxisTickValues() {
  942. return this.getTickValues(
  943. this.owner.config.axis_x_tick_values,
  944. this.owner.xAxis
  945. )
  946. };
  947. Axis.prototype.getYAxisTickValues = function getYAxisTickValues() {
  948. return this.getTickValues(
  949. this.owner.config.axis_y_tick_values,
  950. this.owner.yAxis
  951. )
  952. };
  953. Axis.prototype.getY2AxisTickValues = function getY2AxisTickValues() {
  954. return this.getTickValues(
  955. this.owner.config.axis_y2_tick_values,
  956. this.owner.y2Axis
  957. )
  958. };
  959. Axis.prototype.getLabelOptionByAxisId = function getLabelOptionByAxisId(
  960. axisId
  961. ) {
  962. var $$ = this.owner,
  963. config = $$.config,
  964. option;
  965. if (axisId === 'y') {
  966. option = config.axis_y_label;
  967. } else if (axisId === 'y2') {
  968. option = config.axis_y2_label;
  969. } else if (axisId === 'x') {
  970. option = config.axis_x_label;
  971. }
  972. return option
  973. };
  974. Axis.prototype.getLabelText = function getLabelText(axisId) {
  975. var option = this.getLabelOptionByAxisId(axisId);
  976. return isString(option) ? option : option ? option.text : null
  977. };
  978. Axis.prototype.setLabelText = function setLabelText(axisId, text) {
  979. var $$ = this.owner,
  980. config = $$.config,
  981. option = this.getLabelOptionByAxisId(axisId);
  982. if (isString(option)) {
  983. if (axisId === 'y') {
  984. config.axis_y_label = text;
  985. } else if (axisId === 'y2') {
  986. config.axis_y2_label = text;
  987. } else if (axisId === 'x') {
  988. config.axis_x_label = text;
  989. }
  990. } else if (option) {
  991. option.text = text;
  992. }
  993. };
  994. Axis.prototype.getLabelPosition = function getLabelPosition(
  995. axisId,
  996. defaultPosition
  997. ) {
  998. var option = this.getLabelOptionByAxisId(axisId),
  999. position =
  1000. option && typeof option === 'object' && option.position
  1001. ? option.position
  1002. : defaultPosition;
  1003. return {
  1004. isInner: position.indexOf('inner') >= 0,
  1005. isOuter: position.indexOf('outer') >= 0,
  1006. isLeft: position.indexOf('left') >= 0,
  1007. isCenter: position.indexOf('center') >= 0,
  1008. isRight: position.indexOf('right') >= 0,
  1009. isTop: position.indexOf('top') >= 0,
  1010. isMiddle: position.indexOf('middle') >= 0,
  1011. isBottom: position.indexOf('bottom') >= 0
  1012. }
  1013. };
  1014. Axis.prototype.getXAxisLabelPosition = function getXAxisLabelPosition() {
  1015. return this.getLabelPosition(
  1016. 'x',
  1017. this.owner.config.axis_rotated ? 'inner-top' : 'inner-right'
  1018. )
  1019. };
  1020. Axis.prototype.getYAxisLabelPosition = function getYAxisLabelPosition() {
  1021. return this.getLabelPosition(
  1022. 'y',
  1023. this.owner.config.axis_rotated ? 'inner-right' : 'inner-top'
  1024. )
  1025. };
  1026. Axis.prototype.getY2AxisLabelPosition = function getY2AxisLabelPosition() {
  1027. return this.getLabelPosition(
  1028. 'y2',
  1029. this.owner.config.axis_rotated ? 'inner-right' : 'inner-top'
  1030. )
  1031. };
  1032. Axis.prototype.getLabelPositionById = function getLabelPositionById(id) {
  1033. return id === 'y2'
  1034. ? this.getY2AxisLabelPosition()
  1035. : id === 'y'
  1036. ? this.getYAxisLabelPosition()
  1037. : this.getXAxisLabelPosition()
  1038. };
  1039. Axis.prototype.textForXAxisLabel = function textForXAxisLabel() {
  1040. return this.getLabelText('x')
  1041. };
  1042. Axis.prototype.textForYAxisLabel = function textForYAxisLabel() {
  1043. return this.getLabelText('y')
  1044. };
  1045. Axis.prototype.textForY2AxisLabel = function textForY2AxisLabel() {
  1046. return this.getLabelText('y2')
  1047. };
  1048. Axis.prototype.xForAxisLabel = function xForAxisLabel(forHorizontal, position) {
  1049. var $$ = this.owner;
  1050. if (forHorizontal) {
  1051. return position.isLeft ? 0 : position.isCenter ? $$.width / 2 : $$.width
  1052. } else {
  1053. return position.isBottom
  1054. ? -$$.height
  1055. : position.isMiddle
  1056. ? -$$.height / 2
  1057. : 0
  1058. }
  1059. };
  1060. Axis.prototype.dxForAxisLabel = function dxForAxisLabel(
  1061. forHorizontal,
  1062. position
  1063. ) {
  1064. if (forHorizontal) {
  1065. return position.isLeft ? '0.5em' : position.isRight ? '-0.5em' : '0'
  1066. } else {
  1067. return position.isTop ? '-0.5em' : position.isBottom ? '0.5em' : '0'
  1068. }
  1069. };
  1070. Axis.prototype.textAnchorForAxisLabel = function textAnchorForAxisLabel(
  1071. forHorizontal,
  1072. position
  1073. ) {
  1074. if (forHorizontal) {
  1075. return position.isLeft ? 'start' : position.isCenter ? 'middle' : 'end'
  1076. } else {
  1077. return position.isBottom ? 'start' : position.isMiddle ? 'middle' : 'end'
  1078. }
  1079. };
  1080. Axis.prototype.xForXAxisLabel = function xForXAxisLabel() {
  1081. return this.xForAxisLabel(
  1082. !this.owner.config.axis_rotated,
  1083. this.getXAxisLabelPosition()
  1084. )
  1085. };
  1086. Axis.prototype.xForYAxisLabel = function xForYAxisLabel() {
  1087. return this.xForAxisLabel(
  1088. this.owner.config.axis_rotated,
  1089. this.getYAxisLabelPosition()
  1090. )
  1091. };
  1092. Axis.prototype.xForY2AxisLabel = function xForY2AxisLabel() {
  1093. return this.xForAxisLabel(
  1094. this.owner.config.axis_rotated,
  1095. this.getY2AxisLabelPosition()
  1096. )
  1097. };
  1098. Axis.prototype.dxForXAxisLabel = function dxForXAxisLabel() {
  1099. return this.dxForAxisLabel(
  1100. !this.owner.config.axis_rotated,
  1101. this.getXAxisLabelPosition()
  1102. )
  1103. };
  1104. Axis.prototype.dxForYAxisLabel = function dxForYAxisLabel() {
  1105. return this.dxForAxisLabel(
  1106. this.owner.config.axis_rotated,
  1107. this.getYAxisLabelPosition()
  1108. )
  1109. };
  1110. Axis.prototype.dxForY2AxisLabel = function dxForY2AxisLabel() {
  1111. return this.dxForAxisLabel(
  1112. this.owner.config.axis_rotated,
  1113. this.getY2AxisLabelPosition()
  1114. )
  1115. };
  1116. Axis.prototype.dyForXAxisLabel = function dyForXAxisLabel() {
  1117. var $$ = this.owner,
  1118. config = $$.config,
  1119. position = this.getXAxisLabelPosition();
  1120. if (config.axis_rotated) {
  1121. return position.isInner
  1122. ? '1.2em'
  1123. : -25 - ($$.config.axis_x_inner ? 0 : this.getMaxTickWidth('x'))
  1124. } else {
  1125. return position.isInner ? '-0.5em' : $$.getHorizontalAxisHeight('x') - 10
  1126. }
  1127. };
  1128. Axis.prototype.dyForYAxisLabel = function dyForYAxisLabel() {
  1129. var $$ = this.owner,
  1130. position = this.getYAxisLabelPosition();
  1131. if ($$.config.axis_rotated) {
  1132. return position.isInner ? '-0.5em' : '3em'
  1133. } else {
  1134. return position.isInner
  1135. ? '1.2em'
  1136. : -10 - ($$.config.axis_y_inner ? 0 : this.getMaxTickWidth('y') + 10)
  1137. }
  1138. };
  1139. Axis.prototype.dyForY2AxisLabel = function dyForY2AxisLabel() {
  1140. var $$ = this.owner,
  1141. position = this.getY2AxisLabelPosition();
  1142. if ($$.config.axis_rotated) {
  1143. return position.isInner ? '1.2em' : '-2.2em'
  1144. } else {
  1145. return position.isInner
  1146. ? '-0.5em'
  1147. : 15 + ($$.config.axis_y2_inner ? 0 : this.getMaxTickWidth('y2') + 15)
  1148. }
  1149. };
  1150. Axis.prototype.textAnchorForXAxisLabel = function textAnchorForXAxisLabel() {
  1151. var $$ = this.owner;
  1152. return this.textAnchorForAxisLabel(
  1153. !$$.config.axis_rotated,
  1154. this.getXAxisLabelPosition()
  1155. )
  1156. };
  1157. Axis.prototype.textAnchorForYAxisLabel = function textAnchorForYAxisLabel() {
  1158. var $$ = this.owner;
  1159. return this.textAnchorForAxisLabel(
  1160. $$.config.axis_rotated,
  1161. this.getYAxisLabelPosition()
  1162. )
  1163. };
  1164. Axis.prototype.textAnchorForY2AxisLabel = function textAnchorForY2AxisLabel() {
  1165. var $$ = this.owner;
  1166. return this.textAnchorForAxisLabel(
  1167. $$.config.axis_rotated,
  1168. this.getY2AxisLabelPosition()
  1169. )
  1170. };
  1171. Axis.prototype.getMaxTickWidth = function getMaxTickWidth(
  1172. id,
  1173. withoutRecompute
  1174. ) {
  1175. var $$ = this.owner,
  1176. maxWidth = 0,
  1177. targetsToShow,
  1178. scale,
  1179. axis,
  1180. dummy,
  1181. svg;
  1182. if (withoutRecompute && $$.currentMaxTickWidths[id]) {
  1183. return $$.currentMaxTickWidths[id]
  1184. }
  1185. if ($$.svg) {
  1186. targetsToShow = $$.filterTargetsToShow($$.data.targets);
  1187. if (id === 'y') {
  1188. scale = $$.y.copy().domain($$.getYDomain(targetsToShow, 'y'));
  1189. axis = this.getYAxis(
  1190. id,
  1191. scale,
  1192. $$.yOrient,
  1193. $$.yAxisTickValues,
  1194. false,
  1195. true,
  1196. true
  1197. );
  1198. } else if (id === 'y2') {
  1199. scale = $$.y2.copy().domain($$.getYDomain(targetsToShow, 'y2'));
  1200. axis = this.getYAxis(
  1201. id,
  1202. scale,
  1203. $$.y2Orient,
  1204. $$.y2AxisTickValues,
  1205. false,
  1206. true,
  1207. true
  1208. );
  1209. } else {
  1210. scale = $$.x.copy().domain($$.getXDomain(targetsToShow));
  1211. axis = this.getXAxis(
  1212. scale,
  1213. $$.xOrient,
  1214. $$.xAxisTickFormat,
  1215. $$.xAxisTickValues,
  1216. false,
  1217. true,
  1218. true
  1219. );
  1220. this.updateXAxisTickValues(targetsToShow, axis);
  1221. }
  1222. dummy = $$.d3
  1223. .select('body')
  1224. .append('div')
  1225. .classed('c3', true)
  1226. ;(svg = dummy
  1227. .append('svg')
  1228. .style('visibility', 'hidden')
  1229. .style('position', 'fixed')
  1230. .style('top', 0)
  1231. .style('left', 0)),
  1232. svg
  1233. .append('g')
  1234. .call(axis)
  1235. .each(function() {
  1236. $$.d3
  1237. .select(this)
  1238. .selectAll('text')
  1239. .each(function() {
  1240. var box = getBBox(this);
  1241. if (maxWidth < box.width) {
  1242. maxWidth = box.width;
  1243. }
  1244. });
  1245. dummy.remove();
  1246. });
  1247. }
  1248. $$.currentMaxTickWidths[id] =
  1249. maxWidth <= 0 ? $$.currentMaxTickWidths[id] : maxWidth;
  1250. return $$.currentMaxTickWidths[id]
  1251. };
  1252. Axis.prototype.updateLabels = function updateLabels(withTransition) {
  1253. var $$ = this.owner;
  1254. var axisXLabel = $$.main.select('.' + CLASS.axisX + ' .' + CLASS.axisXLabel),
  1255. axisYLabel = $$.main.select('.' + CLASS.axisY + ' .' + CLASS.axisYLabel),
  1256. axisY2Label = $$.main.select('.' + CLASS.axisY2 + ' .' + CLASS.axisY2Label)
  1257. ;(withTransition ? axisXLabel.transition() : axisXLabel)
  1258. .attr('x', this.xForXAxisLabel.bind(this))
  1259. .attr('dx', this.dxForXAxisLabel.bind(this))
  1260. .attr('dy', this.dyForXAxisLabel.bind(this))
  1261. .text(this.textForXAxisLabel.bind(this))
  1262. ;(withTransition ? axisYLabel.transition() : axisYLabel)
  1263. .attr('x', this.xForYAxisLabel.bind(this))
  1264. .attr('dx', this.dxForYAxisLabel.bind(this))
  1265. .attr('dy', this.dyForYAxisLabel.bind(this))
  1266. .text(this.textForYAxisLabel.bind(this))
  1267. ;(withTransition ? axisY2Label.transition() : axisY2Label)
  1268. .attr('x', this.xForY2AxisLabel.bind(this))
  1269. .attr('dx', this.dxForY2AxisLabel.bind(this))
  1270. .attr('dy', this.dyForY2AxisLabel.bind(this))
  1271. .text(this.textForY2AxisLabel.bind(this));
  1272. };
  1273. Axis.prototype.getPadding = function getPadding(
  1274. padding,
  1275. key,
  1276. defaultValue,
  1277. domainLength
  1278. ) {
  1279. var p = typeof padding === 'number' ? padding : padding[key];
  1280. if (!isValue(p)) {
  1281. return defaultValue
  1282. }
  1283. if (padding.unit === 'ratio') {
  1284. return padding[key] * domainLength
  1285. }
  1286. // assume padding is pixels if unit is not specified
  1287. return this.convertPixelsToAxisPadding(p, domainLength)
  1288. };
  1289. Axis.prototype.convertPixelsToAxisPadding = function convertPixelsToAxisPadding(
  1290. pixels,
  1291. domainLength
  1292. ) {
  1293. var $$ = this.owner,
  1294. length = $$.config.axis_rotated ? $$.width : $$.height;
  1295. return domainLength * (pixels / length)
  1296. };
  1297. Axis.prototype.generateTickValues = function generateTickValues(
  1298. values,
  1299. tickCount,
  1300. forTimeSeries
  1301. ) {
  1302. var tickValues = values,
  1303. targetCount,
  1304. start,
  1305. end,
  1306. count,
  1307. interval,
  1308. i,
  1309. tickValue;
  1310. if (tickCount) {
  1311. targetCount = isFunction(tickCount) ? tickCount() : tickCount;
  1312. // compute ticks according to tickCount
  1313. if (targetCount === 1) {
  1314. tickValues = [values[0]];
  1315. } else if (targetCount === 2) {
  1316. tickValues = [values[0], values[values.length - 1]];
  1317. } else if (targetCount > 2) {
  1318. count = targetCount - 2;
  1319. start = values[0];
  1320. end = values[values.length - 1];
  1321. interval = (end - start) / (count + 1);
  1322. // re-construct unique values
  1323. tickValues = [start];
  1324. for (i = 0; i < count; i++) {
  1325. tickValue = +start + interval * (i + 1);
  1326. tickValues.push(forTimeSeries ? new Date(tickValue) : tickValue);
  1327. }
  1328. tickValues.push(end);
  1329. }
  1330. }
  1331. if (!forTimeSeries) {
  1332. tickValues = tickValues.sort(function(a, b) {
  1333. return a - b
  1334. });
  1335. }
  1336. return tickValues
  1337. };
  1338. Axis.prototype.generateTransitions = function generateTransitions(duration) {
  1339. var $$ = this.owner,
  1340. axes = $$.axes;
  1341. return {
  1342. axisX: duration ? axes.x.transition().duration(duration) : axes.x,
  1343. axisY: duration ? axes.y.transition().duration(duration) : axes.y,
  1344. axisY2: duration ? axes.y2.transition().duration(duration) : axes.y2,
  1345. axisSubX: duration ? axes.subx.transition().duration(duration) : axes.subx
  1346. }
  1347. };
  1348. Axis.prototype.redraw = function redraw(duration, isHidden) {
  1349. var $$ = this.owner,
  1350. transition = duration ? $$.d3.transition().duration(duration) : null;
  1351. $$.axes.x.style('opacity', isHidden ? 0 : 1).call($$.xAxis, transition);
  1352. $$.axes.y.style('opacity', isHidden ? 0 : 1).call($$.yAxis, transition);
  1353. $$.axes.y2.style('opacity', isHidden ? 0 : 1).call($$.y2Axis, transition);
  1354. $$.axes.subx.style('opacity', isHidden ? 0 : 1).call($$.subXAxis, transition);
  1355. };
  1356. var c3 = {
  1357. version: '0.7.18',
  1358. chart: {
  1359. fn: Chart.prototype,
  1360. internal: {
  1361. fn: ChartInternal.prototype,
  1362. axis: {
  1363. fn: Axis.prototype,
  1364. internal: {
  1365. fn: AxisInternal.prototype
  1366. }
  1367. }
  1368. }
  1369. },
  1370. generate: function(config) {
  1371. return new Chart(config)
  1372. }
  1373. };
  1374. ChartInternal.prototype.beforeInit = function() {
  1375. // can do something
  1376. };
  1377. ChartInternal.prototype.afterInit = function() {
  1378. // can do something
  1379. };
  1380. ChartInternal.prototype.init = function() {
  1381. var $$ = this,
  1382. config = $$.config;
  1383. $$.initParams();
  1384. if (config.data_url) {
  1385. $$.convertUrlToData(
  1386. config.data_url,
  1387. config.data_mimeType,
  1388. config.data_headers,
  1389. config.data_keys,
  1390. $$.initWithData
  1391. );
  1392. } else if (config.data_json) {
  1393. $$.initWithData($$.convertJsonToData(config.data_json, config.data_keys));
  1394. } else if (config.data_rows) {
  1395. $$.initWithData($$.convertRowsToData(config.data_rows));
  1396. } else if (config.data_columns) {
  1397. $$.initWithData($$.convertColumnsToData(config.data_columns));
  1398. } else {
  1399. throw Error('url or json or rows or columns is required.')
  1400. }
  1401. };
  1402. ChartInternal.prototype.initParams = function() {
  1403. var $$ = this,
  1404. d3 = $$.d3,
  1405. config = $$.config;
  1406. // MEMO: clipId needs to be unique because it conflicts when multiple charts exist
  1407. $$.clipId = 'c3-' + new Date().valueOf() + '-clip';
  1408. $$.clipIdForXAxis = $$.clipId + '-xaxis';
  1409. $$.clipIdForYAxis = $$.clipId + '-yaxis';
  1410. $$.clipIdForGrid = $$.clipId + '-grid';
  1411. $$.clipIdForSubchart = $$.clipId + '-subchart';
  1412. $$.clipPath = $$.getClipPath($$.clipId);
  1413. $$.clipPathForXAxis = $$.getClipPath($$.clipIdForXAxis);
  1414. $$.clipPathForYAxis = $$.getClipPath($$.clipIdForYAxis);
  1415. $$.clipPathForGrid = $$.getClipPath($$.clipIdForGrid);
  1416. $$.clipPathForSubchart = $$.getClipPath($$.clipIdForSubchart);
  1417. $$.dragStart = null;
  1418. $$.dragging = false;
  1419. $$.flowing = false;
  1420. $$.cancelClick = false;
  1421. $$.mouseover = undefined;
  1422. $$.transiting = false;
  1423. $$.color = $$.generateColor();
  1424. $$.levelColor = $$.generateLevelColor();
  1425. $$.dataTimeParse = (config.data_xLocaltime ? d3.timeParse : d3.utcParse)(
  1426. $$.config.data_xFormat
  1427. );
  1428. $$.axisTimeFormat = config.axis_x_localtime ? d3.timeFormat : d3.utcFormat;
  1429. $$.defaultAxisTimeFormat = function(date) {
  1430. if (date.getMilliseconds()) {
  1431. return d3.timeFormat('.%L')(date)
  1432. }
  1433. if (date.getSeconds()) {
  1434. return d3.timeFormat(':%S')(date)
  1435. }
  1436. if (date.getMinutes()) {
  1437. return d3.timeFormat('%I:%M')(date)
  1438. }
  1439. if (date.getHours()) {
  1440. return d3.timeFormat('%I %p')(date)
  1441. }
  1442. if (date.getDay() && date.getDate() !== 1) {
  1443. return d3.timeFormat('%-m/%-d')(date)
  1444. }
  1445. if (date.getDate() !== 1) {
  1446. return d3.timeFormat('%-m/%-d')(date)
  1447. }
  1448. if (date.getMonth()) {
  1449. return d3.timeFormat('%-m/%-d')(date)
  1450. }
  1451. return d3.timeFormat('%Y/%-m/%-d')(date)
  1452. };
  1453. $$.hiddenTargetIds = [];
  1454. $$.hiddenLegendIds = [];
  1455. $$.focusedTargetIds = [];
  1456. $$.defocusedTargetIds = [];
  1457. $$.xOrient = config.axis_rotated
  1458. ? config.axis_x_inner
  1459. ? 'right'
  1460. : 'left'
  1461. : config.axis_x_inner
  1462. ? 'top'
  1463. : 'bottom';
  1464. $$.yOrient = config.axis_rotated
  1465. ? config.axis_y_inner
  1466. ? 'top'
  1467. : 'bottom'
  1468. : config.axis_y_inner
  1469. ? 'right'
  1470. : 'left';
  1471. $$.y2Orient = config.axis_rotated
  1472. ? config.axis_y2_inner
  1473. ? 'bottom'
  1474. : 'top'
  1475. : config.axis_y2_inner
  1476. ? 'left'
  1477. : 'right';
  1478. $$.subXOrient = config.axis_rotated ? 'left' : 'bottom';
  1479. $$.isLegendRight = config.legend_position === 'right';
  1480. $$.isLegendInset = config.legend_position === 'inset';
  1481. $$.isLegendTop =
  1482. config.legend_inset_anchor === 'top-left' ||
  1483. config.legend_inset_anchor === 'top-right';
  1484. $$.isLegendLeft =
  1485. config.legend_inset_anchor === 'top-left' ||
  1486. config.legend_inset_anchor === 'bottom-left';
  1487. $$.legendStep = 0;
  1488. $$.legendItemWidth = 0;
  1489. $$.legendItemHeight = 0;
  1490. $$.currentMaxTickWidths = {
  1491. x: 0,
  1492. y: 0,
  1493. y2: 0
  1494. };
  1495. $$.rotated_padding_left = 30;
  1496. $$.rotated_padding_right = config.axis_rotated && !config.axis_x_show ? 0 : 30;
  1497. $$.rotated_padding_top = 5;
  1498. $$.withoutFadeIn = {};
  1499. $$.intervalForObserveInserted = undefined;
  1500. $$.axes.subx = d3.selectAll([]); // needs when excluding subchart.js
  1501. };
  1502. ChartInternal.prototype.initChartElements = function() {
  1503. if (this.initBar) {
  1504. this.initBar();
  1505. }
  1506. if (this.initLine) {
  1507. this.initLine();
  1508. }
  1509. if (this.initArc) {
  1510. this.initArc();
  1511. }
  1512. if (this.initGauge) {
  1513. this.initGauge();
  1514. }
  1515. if (this.initText) {
  1516. this.initText();
  1517. }
  1518. };
  1519. ChartInternal.prototype.initWithData = function(data) {
  1520. var $$ = this,
  1521. d3 = $$.d3,
  1522. config = $$.config;
  1523. var defs,
  1524. main,
  1525. binding = true;
  1526. $$.axis = new Axis($$);
  1527. if (!config.bindto) {
  1528. $$.selectChart = d3.selectAll([]);
  1529. } else if (typeof config.bindto.node === 'function') {
  1530. $$.selectChart = config.bindto;
  1531. } else {
  1532. $$.selectChart = d3.select(config.bindto);
  1533. }
  1534. if ($$.selectChart.empty()) {
  1535. $$.selectChart = d3
  1536. .select(document.createElement('div'))
  1537. .style('opacity', 0);
  1538. $$.observeInserted($$.selectChart);
  1539. binding = false;
  1540. }
  1541. $$.selectChart.html('').classed('c3', true);
  1542. // Init data as targets
  1543. $$.data.xs = {};
  1544. $$.data.targets = $$.convertDataToTargets(data);
  1545. if (config.data_filter) {
  1546. $$.data.targets = $$.data.targets.filter(config.data_filter);
  1547. }
  1548. // Set targets to hide if needed
  1549. if (config.data_hide) {
  1550. $$.addHiddenTargetIds(
  1551. config.data_hide === true
  1552. ? $$.mapToIds($$.data.targets)
  1553. : config.data_hide
  1554. );
  1555. }
  1556. if (config.legend_hide) {
  1557. $$.addHiddenLegendIds(
  1558. config.legend_hide === true
  1559. ? $$.mapToIds($$.data.targets)
  1560. : config.legend_hide
  1561. );
  1562. }
  1563. if ($$.isStanfordGraphType()) {
  1564. $$.initStanfordData();
  1565. }
  1566. // Init sizes and scales
  1567. $$.updateSizes();
  1568. $$.updateScales();
  1569. // Set domains for each scale
  1570. $$.x.domain(d3.extent($$.getXDomain($$.data.targets)));
  1571. $$.y.domain($$.getYDomain($$.data.targets, 'y'));
  1572. $$.y2.domain($$.getYDomain($$.data.targets, 'y2'));
  1573. $$.subX.domain($$.x.domain());
  1574. $$.subY.domain($$.y.domain());
  1575. $$.subY2.domain($$.y2.domain());
  1576. // Save original x domain for zoom update
  1577. $$.orgXDomain = $$.x.domain();
  1578. /*-- Basic Elements --*/
  1579. // Define svgs
  1580. $$.svg = $$.selectChart
  1581. .append('svg')
  1582. .style('overflow', 'hidden')
  1583. .on('mouseenter', function() {
  1584. return config.onmouseover.call($$)
  1585. })
  1586. .on('mouseleave', function() {
  1587. return config.onmouseout.call($$)
  1588. });
  1589. if ($$.config.svg_classname) {
  1590. $$.svg.attr('class', $$.config.svg_classname);
  1591. }
  1592. // Define defs
  1593. defs = $$.svg.append('defs');
  1594. $$.clipChart = $$.appendClip(defs, $$.clipId);
  1595. $$.clipXAxis = $$.appendClip(defs, $$.clipIdForXAxis);
  1596. $$.clipYAxis = $$.appendClip(defs, $$.clipIdForYAxis);
  1597. $$.clipGrid = $$.appendClip(defs, $$.clipIdForGrid);
  1598. $$.clipSubchart = $$.appendClip(defs, $$.clipIdForSubchart);
  1599. $$.updateSvgSize();
  1600. // Define regions
  1601. main = $$.main = $$.svg.append('g').attr('transform', $$.getTranslate('main'));
  1602. if ($$.initPie) {
  1603. $$.initPie();
  1604. }
  1605. if ($$.initDragZoom) {
  1606. $$.initDragZoom();
  1607. }
  1608. if (config.subchart_show && $$.initSubchart) {
  1609. $$.initSubchart();
  1610. }
  1611. if ($$.initTooltip) {
  1612. $$.initTooltip();
  1613. }
  1614. if ($$.initLegend) {
  1615. $$.initLegend();
  1616. }
  1617. if ($$.initTitle) {
  1618. $$.initTitle();
  1619. }
  1620. if ($$.initZoom) {
  1621. $$.initZoom();
  1622. }
  1623. if ($$.isStanfordGraphType()) {
  1624. $$.drawColorScale();
  1625. }
  1626. // Update selection based on size and scale
  1627. // TODO: currently this must be called after initLegend because of update of sizes, but it should be done in initSubchart.
  1628. if (config.subchart_show && $$.initSubchartBrush) {
  1629. $$.initSubchartBrush();
  1630. }
  1631. /*-- Main Region --*/
  1632. // text when empty
  1633. main
  1634. .append('text')
  1635. .attr('class', CLASS.text + ' ' + CLASS.empty)
  1636. .attr('text-anchor', 'middle') // horizontal centering of text at x position in all browsers.
  1637. .attr('dominant-baseline', 'middle'); // vertical centering of text at y position in all browsers, except IE.
  1638. // Regions
  1639. $$.initRegion();
  1640. // Grids
  1641. $$.initGrid();
  1642. // Define g for chart area
  1643. main
  1644. .append('g')
  1645. .attr('clip-path', $$.clipPath)
  1646. .attr('class', CLASS.chart);
  1647. // Grid lines
  1648. if (config.grid_lines_front) {
  1649. $$.initGridLines();
  1650. }
  1651. $$.initStanfordElements();
  1652. // Cover whole with rects for events
  1653. $$.initEventRect();
  1654. // Define g for chart
  1655. $$.initChartElements();
  1656. // Add Axis
  1657. $$.axis.init();
  1658. // Set targets
  1659. $$.updateTargets($$.data.targets);
  1660. // Set default extent if defined
  1661. if (config.axis_x_selection) {
  1662. $$.brush.selectionAsValue($$.getDefaultSelection());
  1663. }
  1664. // Draw with targets
  1665. if (binding) {
  1666. $$.updateDimension();
  1667. $$.config.oninit.call($$);
  1668. $$.redraw({
  1669. withTransition: false,
  1670. withTransform: true,
  1671. withUpdateXDomain: true,
  1672. withUpdateOrgXDomain: true,
  1673. withTransitionForAxis: false
  1674. });
  1675. }
  1676. // Bind to resize event
  1677. $$.bindResize();
  1678. // Bind to window focus event
  1679. $$.bindWindowFocus();
  1680. // export element of the chart
  1681. $$.api.element = $$.selectChart.node();
  1682. };
  1683. ChartInternal.prototype.smoothLines = function(el, type) {
  1684. var $$ = this;
  1685. if (type === 'grid') {
  1686. el.each(function() {
  1687. var g = $$.d3.select(this),
  1688. x1 = g.attr('x1'),
  1689. x2 = g.attr('x2'),
  1690. y1 = g.attr('y1'),
  1691. y2 = g.attr('y2');
  1692. g.attr({
  1693. x1: Math.ceil(x1),
  1694. x2: Math.ceil(x2),
  1695. y1: Math.ceil(y1),
  1696. y2: Math.ceil(y2)
  1697. });
  1698. });
  1699. }
  1700. };
  1701. ChartInternal.prototype.updateSizes = function() {
  1702. var $$ = this,
  1703. config = $$.config;
  1704. var legendHeight = $$.legend ? $$.getLegendHeight() : 0,
  1705. legendWidth = $$.legend ? $$.getLegendWidth() : 0,
  1706. legendHeightForBottom =
  1707. $$.isLegendRight || $$.isLegendInset ? 0 : legendHeight,
  1708. hasArc = $$.hasArcType(),
  1709. xAxisHeight =
  1710. config.axis_rotated || hasArc ? 0 : $$.getHorizontalAxisHeight('x'),
  1711. subchartHeight =
  1712. config.subchart_show && !hasArc
  1713. ? config.subchart_size_height + xAxisHeight
  1714. : 0;
  1715. $$.currentWidth = $$.getCurrentWidth();
  1716. $$.currentHeight = $$.getCurrentHeight();
  1717. // for main
  1718. $$.margin = config.axis_rotated
  1719. ? {
  1720. top: $$.getHorizontalAxisHeight('y2') + $$.getCurrentPaddingTop(),
  1721. right: hasArc ? 0 : $$.getCurrentPaddingRight(),
  1722. bottom:
  1723. $$.getHorizontalAxisHeight('y') +
  1724. legendHeightForBottom +
  1725. $$.getCurrentPaddingBottom(),
  1726. left: subchartHeight + (hasArc ? 0 : $$.getCurrentPaddingLeft())
  1727. }
  1728. : {
  1729. top: 4 + $$.getCurrentPaddingTop(), // for top tick text
  1730. right: hasArc ? 0 : $$.getCurrentPaddingRight(),
  1731. bottom:
  1732. xAxisHeight +
  1733. subchartHeight +
  1734. legendHeightForBottom +
  1735. $$.getCurrentPaddingBottom(),
  1736. left: hasArc ? 0 : $$.getCurrentPaddingLeft()
  1737. };
  1738. // for subchart
  1739. $$.margin2 = config.axis_rotated
  1740. ? {
  1741. top: $$.margin.top,
  1742. right: NaN,
  1743. bottom: 20 + legendHeightForBottom,
  1744. left: $$.rotated_padding_left
  1745. }
  1746. : {
  1747. top: $$.currentHeight - subchartHeight - legendHeightForBottom,
  1748. right: NaN,
  1749. bottom: xAxisHeight + legendHeightForBottom,
  1750. left: $$.margin.left
  1751. };
  1752. // for legend
  1753. $$.margin3 = {
  1754. top: 0,
  1755. right: NaN,
  1756. bottom: 0,
  1757. left: 0
  1758. };
  1759. if ($$.updateSizeForLegend) {
  1760. $$.updateSizeForLegend(legendHeight, legendWidth);
  1761. }
  1762. $$.width = $$.currentWidth - $$.margin.left - $$.margin.right;
  1763. $$.height = $$.currentHeight - $$.margin.top - $$.margin.bottom;
  1764. if ($$.width < 0) {
  1765. $$.width = 0;
  1766. }
  1767. if ($$.height < 0) {
  1768. $$.height = 0;
  1769. }
  1770. $$.width2 = config.axis_rotated
  1771. ? $$.margin.left - $$.rotated_padding_left - $$.rotated_padding_right
  1772. : $$.width;
  1773. $$.height2 = config.axis_rotated
  1774. ? $$.height
  1775. : $$.currentHeight - $$.margin2.top - $$.margin2.bottom;
  1776. if ($$.width2 < 0) {
  1777. $$.width2 = 0;
  1778. }
  1779. if ($$.height2 < 0) {
  1780. $$.height2 = 0;
  1781. }
  1782. // for arc
  1783. $$.arcWidth = $$.width - ($$.isLegendRight ? legendWidth + 10 : 0);
  1784. $$.arcHeight = $$.height - ($$.isLegendRight ? 0 : 10);
  1785. if ($$.hasType('gauge') && !config.gauge_fullCircle) {
  1786. $$.arcHeight += $$.height - $$.getGaugeLabelHeight();
  1787. }
  1788. if ($$.updateRadius) {
  1789. $$.updateRadius();
  1790. }
  1791. if ($$.isLegendRight && hasArc) {
  1792. $$.margin3.left = $$.arcWidth / 2 + $$.radiusExpanded * 1.1;
  1793. }
  1794. };
  1795. ChartInternal.prototype.updateTargets = function(targets) {
  1796. var $$ = this,
  1797. config = $$.config;
  1798. /*-- Main --*/
  1799. //-- Text --//
  1800. $$.updateTargetsForText(targets);
  1801. //-- Bar --//
  1802. $$.updateTargetsForBar(targets);
  1803. //-- Line --//
  1804. $$.updateTargetsForLine(targets);
  1805. //-- Arc --//
  1806. if ($$.hasArcType() && $$.updateTargetsForArc) {
  1807. $$.updateTargetsForArc(targets);
  1808. }
  1809. /*-- Sub --*/
  1810. if (config.subchart_show && $$.updateTargetsForSubchart) {
  1811. $$.updateTargetsForSubchart(targets);
  1812. }
  1813. // Fade-in each chart
  1814. $$.showTargets();
  1815. };
  1816. ChartInternal.prototype.showTargets = function() {
  1817. var $$ = this;
  1818. $$.svg
  1819. .selectAll('.' + CLASS.target)
  1820. .filter(function(d) {
  1821. return $$.isTargetToShow(d.id)
  1822. })
  1823. .transition()
  1824. .duration($$.config.transition_duration)
  1825. .style('opacity', 1);
  1826. };
  1827. ChartInternal.prototype.redraw = function(options, transitions) {
  1828. var $$ = this,
  1829. main = $$.main,
  1830. d3 = $$.d3,
  1831. config = $$.config;
  1832. var areaIndices = $$.getShapeIndices($$.isAreaType),
  1833. barIndices = $$.getShapeIndices($$.isBarType),
  1834. lineIndices = $$.getShapeIndices($$.isLineType);
  1835. var withY,
  1836. withSubchart,
  1837. withTransition,
  1838. withTransitionForExit,
  1839. withTransitionForAxis,
  1840. withTransform,
  1841. withUpdateXDomain,
  1842. withUpdateOrgXDomain,
  1843. withTrimXDomain,
  1844. withLegend,
  1845. withEventRect,
  1846. withDimension,
  1847. withUpdateXAxis;
  1848. var hideAxis = $$.hasArcType();
  1849. var drawArea, drawBar, drawLine, xForText, yForText;
  1850. var duration, durationForExit, durationForAxis;
  1851. var transitionsToWait, waitForDraw, flow, transition;
  1852. var targetsToShow = $$.filterTargetsToShow($$.data.targets),
  1853. tickValues,
  1854. i,
  1855. intervalForCulling,
  1856. xDomainForZoom;
  1857. var xv = $$.xv.bind($$),
  1858. cx,
  1859. cy;
  1860. options = options || {};
  1861. withY = getOption(options, 'withY', true);
  1862. withSubchart = getOption(options, 'withSubchart', true);
  1863. withTransition = getOption(options, 'withTransition', true);
  1864. withTransform = getOption(options, 'withTransform', false);
  1865. withUpdateXDomain = getOption(options, 'withUpdateXDomain', false);
  1866. withUpdateOrgXDomain = getOption(options, 'withUpdateOrgXDomain', false);
  1867. withTrimXDomain = getOption(options, 'withTrimXDomain', true);
  1868. withUpdateXAxis = getOption(options, 'withUpdateXAxis', withUpdateXDomain);
  1869. withLegend = getOption(options, 'withLegend', false);
  1870. withEventRect = getOption(options, 'withEventRect', true);
  1871. withDimension = getOption(options, 'withDimension', true);
  1872. withTransitionForExit = getOption(
  1873. options,
  1874. 'withTransitionForExit',
  1875. withTransition
  1876. );
  1877. withTransitionForAxis = getOption(
  1878. options,
  1879. 'withTransitionForAxis',
  1880. withTransition
  1881. );
  1882. duration = withTransition ? config.transition_duration : 0;
  1883. durationForExit = withTransitionForExit ? duration : 0;
  1884. durationForAxis = withTransitionForAxis ? duration : 0;
  1885. transitions = transitions || $$.axis.generateTransitions(durationForAxis);
  1886. // update legend and transform each g
  1887. if (withLegend && config.legend_show) {
  1888. $$.updateLegend($$.mapToIds($$.data.targets), options, transitions);
  1889. } else if (withDimension) {
  1890. // need to update dimension (e.g. axis.y.tick.values) because y tick values should change
  1891. // no need to update axis in it because they will be updated in redraw()
  1892. $$.updateDimension(true);
  1893. }
  1894. // MEMO: needed for grids calculation
  1895. if ($$.isCategorized() && targetsToShow.length === 0) {
  1896. $$.x.domain([0, $$.axes.x.selectAll('.tick').size()]);
  1897. }
  1898. if (targetsToShow.length) {
  1899. $$.updateXDomain(
  1900. targetsToShow,
  1901. withUpdateXDomain,
  1902. withUpdateOrgXDomain,
  1903. withTrimXDomain
  1904. );
  1905. if (!config.axis_x_tick_values) {
  1906. tickValues = $$.axis.updateXAxisTickValues(targetsToShow);
  1907. }
  1908. } else {
  1909. $$.xAxis.tickValues([]);
  1910. $$.subXAxis.tickValues([]);
  1911. }
  1912. if (config.zoom_rescale && !options.flow) {
  1913. xDomainForZoom = $$.x.orgDomain();
  1914. }
  1915. $$.y.domain($$.getYDomain(targetsToShow, 'y', xDomainForZoom));
  1916. $$.y2.domain($$.getYDomain(targetsToShow, 'y2', xDomainForZoom));
  1917. if (!config.axis_y_tick_values && config.axis_y_tick_count) {
  1918. $$.yAxis.tickValues(
  1919. $$.axis.generateTickValues($$.y.domain(), config.axis_y_tick_count)
  1920. );
  1921. }
  1922. if (!config.axis_y2_tick_values && config.axis_y2_tick_count) {
  1923. $$.y2Axis.tickValues(
  1924. $$.axis.generateTickValues($$.y2.domain(), config.axis_y2_tick_count)
  1925. );
  1926. }
  1927. // axes
  1928. $$.axis.redraw(durationForAxis, hideAxis);
  1929. // Update axis label
  1930. $$.axis.updateLabels(withTransition);
  1931. // show/hide if manual culling needed
  1932. if ((withUpdateXDomain || withUpdateXAxis) && targetsToShow.length) {
  1933. if (config.axis_x_tick_culling && tickValues) {
  1934. for (i = 1; i < tickValues.length; i++) {
  1935. if (tickValues.length / i < config.axis_x_tick_culling_max) {
  1936. intervalForCulling = i;
  1937. break
  1938. }
  1939. }
  1940. $$.svg.selectAll('.' + CLASS.axisX + ' .tick text').each(function(e) {
  1941. var index = tickValues.indexOf(e);
  1942. if (index >= 0) {
  1943. d3.select(this).style(
  1944. 'display',
  1945. index % intervalForCulling ? 'none' : 'block'
  1946. );
  1947. }
  1948. });
  1949. } else {
  1950. $$.svg
  1951. .selectAll('.' + CLASS.axisX + ' .tick text')
  1952. .style('display', 'block');
  1953. }
  1954. }
  1955. // setup drawer - MEMO: these must be called after axis updated
  1956. drawArea = $$.generateDrawArea
  1957. ? $$.generateDrawArea(areaIndices, false)
  1958. : undefined;
  1959. drawBar = $$.generateDrawBar ? $$.generateDrawBar(barIndices) : undefined;
  1960. drawLine = $$.generateDrawLine
  1961. ? $$.generateDrawLine(lineIndices, false)
  1962. : undefined;
  1963. xForText = $$.generateXYForText(areaIndices, barIndices, lineIndices, true);
  1964. yForText = $$.generateXYForText(areaIndices, barIndices, lineIndices, false);
  1965. // update circleY based on updated parameters
  1966. $$.updateCircleY();
  1967. // generate circle x/y functions depending on updated params
  1968. cx = ($$.config.axis_rotated ? $$.circleY : $$.circleX).bind($$);
  1969. cy = ($$.config.axis_rotated ? $$.circleX : $$.circleY).bind($$);
  1970. // Update sub domain
  1971. if (withY) {
  1972. $$.subY.domain($$.getYDomain(targetsToShow, 'y'));
  1973. $$.subY2.domain($$.getYDomain(targetsToShow, 'y2'));
  1974. }
  1975. // xgrid focus
  1976. $$.updateXgridFocus();
  1977. // Data empty label positioning and text.
  1978. main
  1979. .select('text.' + CLASS.text + '.' + CLASS.empty)
  1980. .attr('x', $$.width / 2)
  1981. .attr('y', $$.height / 2)
  1982. .text(config.data_empty_label_text)
  1983. .transition()
  1984. .style('opacity', targetsToShow.length ? 0 : 1);
  1985. // event rect
  1986. if (withEventRect) {
  1987. $$.redrawEventRect();
  1988. }
  1989. // grid
  1990. $$.updateGrid(duration);
  1991. $$.updateStanfordElements(duration);
  1992. // rect for regions
  1993. $$.updateRegion(duration);
  1994. // bars
  1995. $$.updateBar(durationForExit);
  1996. // lines, areas and circles
  1997. $$.updateLine(durationForExit);
  1998. $$.updateArea(durationForExit);
  1999. $$.updateCircle(cx, cy);
  2000. // text
  2001. if ($$.hasDataLabel()) {
  2002. $$.updateText(xForText, yForText, durationForExit);
  2003. }
  2004. // title
  2005. if ($$.redrawTitle) {
  2006. $$.redrawTitle();
  2007. }
  2008. // arc
  2009. if ($$.redrawArc) {
  2010. $$.redrawArc(duration, durationForExit, withTransform);
  2011. }
  2012. // subchart
  2013. if (config.subchart_show && $$.redrawSubchart) {
  2014. $$.redrawSubchart(
  2015. withSubchart,
  2016. transitions,
  2017. duration,
  2018. durationForExit,
  2019. areaIndices,
  2020. barIndices,
  2021. lineIndices
  2022. );
  2023. }
  2024. if ($$.isStanfordGraphType()) {
  2025. $$.drawColorScale();
  2026. }
  2027. // circles for select
  2028. main
  2029. .selectAll('.' + CLASS.selectedCircles)
  2030. .filter($$.isBarType.bind($$))
  2031. .selectAll('circle')
  2032. .remove();
  2033. if (options.flow) {
  2034. flow = $$.generateFlow({
  2035. targets: targetsToShow,
  2036. flow: options.flow,
  2037. duration: options.flow.duration,
  2038. drawBar: drawBar,
  2039. drawLine: drawLine,
  2040. drawArea: drawArea,
  2041. cx: cx,
  2042. cy: cy,
  2043. xv: xv,
  2044. xForText: xForText,
  2045. yForText: yForText
  2046. });
  2047. }
  2048. if (duration && $$.isTabVisible()) {
  2049. // Only use transition if tab visible. See #938.
  2050. // transition should be derived from one transition
  2051. transition = d3.transition().duration(duration);
  2052. transitionsToWait = []
  2053. ;[
  2054. $$.redrawBar(drawBar, true, transition),
  2055. $$.redrawLine(drawLine, true, transition),
  2056. $$.redrawArea(drawArea, true, transition),
  2057. $$.redrawCircle(cx, cy, true, transition),
  2058. $$.redrawText(xForText, yForText, options.flow, true, transition),
  2059. $$.redrawRegion(true, transition),
  2060. $$.redrawGrid(true, transition)
  2061. ].forEach(function(transitions) {
  2062. transitions.forEach(function(transition) {
  2063. transitionsToWait.push(transition);
  2064. });
  2065. });
  2066. // Wait for end of transitions to call flow and onrendered callback
  2067. waitForDraw = $$.generateWait();
  2068. transitionsToWait.forEach(function(t) {
  2069. waitForDraw.add(t);
  2070. });
  2071. waitForDraw(function() {
  2072. if (flow) {
  2073. flow();
  2074. }
  2075. if (config.onrendered) {
  2076. config.onrendered.call($$);
  2077. }
  2078. });
  2079. } else {
  2080. $$.redrawBar(drawBar);
  2081. $$.redrawLine(drawLine);
  2082. $$.redrawArea(drawArea);
  2083. $$.redrawCircle(cx, cy);
  2084. $$.redrawText(xForText, yForText, options.flow);
  2085. $$.redrawRegion();
  2086. $$.redrawGrid();
  2087. if (flow) {
  2088. flow();
  2089. }
  2090. if (config.onrendered) {
  2091. config.onrendered.call($$);
  2092. }
  2093. }
  2094. // update fadein condition
  2095. $$.mapToIds($$.data.targets).forEach(function(id) {
  2096. $$.withoutFadeIn[id] = true;
  2097. });
  2098. };
  2099. ChartInternal.prototype.updateAndRedraw = function(options) {
  2100. var $$ = this,
  2101. config = $$.config,
  2102. transitions;
  2103. options = options || {};
  2104. // same with redraw
  2105. options.withTransition = getOption(options, 'withTransition', true);
  2106. options.withTransform = getOption(options, 'withTransform', false);
  2107. options.withLegend = getOption(options, 'withLegend', false);
  2108. // NOT same with redraw
  2109. options.withUpdateXDomain = getOption(options, 'withUpdateXDomain', true);
  2110. options.withUpdateOrgXDomain = getOption(
  2111. options,
  2112. 'withUpdateOrgXDomain',
  2113. true
  2114. );
  2115. options.withTransitionForExit = false;
  2116. options.withTransitionForTransform = getOption(
  2117. options,
  2118. 'withTransitionForTransform',
  2119. options.withTransition
  2120. );
  2121. // MEMO: this needs to be called before updateLegend and it means this ALWAYS needs to be called)
  2122. $$.updateSizes();
  2123. // MEMO: called in updateLegend in redraw if withLegend
  2124. if (!(options.withLegend && config.legend_show)) {
  2125. transitions = $$.axis.generateTransitions(
  2126. options.withTransitionForAxis ? config.transition_duration : 0
  2127. );
  2128. // Update scales
  2129. $$.updateScales();
  2130. $$.updateSvgSize();
  2131. // Update g positions
  2132. $$.transformAll(options.withTransitionForTransform, transitions);
  2133. }
  2134. // Draw with new sizes & scales
  2135. $$.redraw(options, transitions);
  2136. };
  2137. ChartInternal.prototype.redrawWithoutRescale = function() {
  2138. this.redraw({
  2139. withY: false,
  2140. withSubchart: false,
  2141. withEventRect: false,
  2142. withTransitionForAxis: false
  2143. });
  2144. };
  2145. ChartInternal.prototype.isTimeSeries = function() {
  2146. return this.config.axis_x_type === 'timeseries'
  2147. };
  2148. ChartInternal.prototype.isCategorized = function() {
  2149. return this.config.axis_x_type.indexOf('categor') >= 0
  2150. };
  2151. ChartInternal.prototype.isCustomX = function() {
  2152. var $$ = this,
  2153. config = $$.config;
  2154. return !$$.isTimeSeries() && (config.data_x || notEmpty(config.data_xs))
  2155. };
  2156. ChartInternal.prototype.isTimeSeriesY = function() {
  2157. return this.config.axis_y_type === 'timeseries'
  2158. };
  2159. ChartInternal.prototype.getTranslate = function(target) {
  2160. var $$ = this,
  2161. config = $$.config,
  2162. x,
  2163. y;
  2164. if (target === 'main') {
  2165. x = asHalfPixel($$.margin.left);
  2166. y = asHalfPixel($$.margin.top);
  2167. } else if (target === 'context') {
  2168. x = asHalfPixel($$.margin2.left);
  2169. y = asHalfPixel($$.margin2.top);
  2170. } else if (target === 'legend') {
  2171. x = $$.margin3.left;
  2172. y = $$.margin3.top;
  2173. } else if (target === 'x') {
  2174. x = 0;
  2175. y = config.axis_rotated ? 0 : $$.height;
  2176. } else if (target === 'y') {
  2177. x = 0;
  2178. y = config.axis_rotated ? $$.height : 0;
  2179. } else if (target === 'y2') {
  2180. x = config.axis_rotated ? 0 : $$.width;
  2181. y = config.axis_rotated ? 1 : 0;
  2182. } else if (target === 'subx') {
  2183. x = 0;
  2184. y = config.axis_rotated ? 0 : $$.height2;
  2185. } else if (target === 'arc') {
  2186. x = $$.arcWidth / 2;
  2187. y = $$.arcHeight / 2 - ($$.hasType('gauge') ? 6 : 0); // to prevent wrong display of min and max label
  2188. }
  2189. return 'translate(' + x + ',' + y + ')'
  2190. };
  2191. ChartInternal.prototype.initialOpacity = function(d) {
  2192. return d.value !== null && this.withoutFadeIn[d.id] ? 1 : 0
  2193. };
  2194. ChartInternal.prototype.initialOpacityForCircle = function(d) {
  2195. return d.value !== null && this.withoutFadeIn[d.id]
  2196. ? this.opacityForCircle(d)
  2197. : 0
  2198. };
  2199. ChartInternal.prototype.opacityForCircle = function(d) {
  2200. var isPointShouldBeShown = isFunction(this.config.point_show)
  2201. ? this.config.point_show(d)
  2202. : this.config.point_show;
  2203. var opacity = isPointShouldBeShown || this.isStanfordType(d) ? 1 : 0;
  2204. return isValue(d.value) ? (this.isScatterType(d) ? 0.5 : opacity) : 0
  2205. };
  2206. ChartInternal.prototype.opacityForText = function() {
  2207. return this.hasDataLabel() ? 1 : 0
  2208. };
  2209. ChartInternal.prototype.xx = function(d) {
  2210. return d ? this.x(d.x) : null
  2211. };
  2212. ChartInternal.prototype.xvCustom = function(d, xyValue) {
  2213. var $$ = this,
  2214. value = xyValue ? d[xyValue] : d.value;
  2215. if ($$.isTimeSeries()) {
  2216. value = $$.parseDate(d.value);
  2217. } else if ($$.isCategorized() && typeof d.value === 'string') {
  2218. value = $$.config.axis_x_categories.indexOf(d.value);
  2219. }
  2220. return Math.ceil($$.x(value))
  2221. };
  2222. ChartInternal.prototype.yvCustom = function(d, xyValue) {
  2223. var $$ = this,
  2224. yScale = d.axis && d.axis === 'y2' ? $$.y2 : $$.y,
  2225. value = xyValue ? d[xyValue] : d.value;
  2226. return Math.ceil(yScale(value))
  2227. };
  2228. ChartInternal.prototype.xv = function(d) {
  2229. var $$ = this,
  2230. value = d.value;
  2231. if ($$.isTimeSeries()) {
  2232. value = $$.parseDate(d.value);
  2233. } else if ($$.isCategorized() && typeof d.value === 'string') {
  2234. value = $$.config.axis_x_categories.indexOf(d.value);
  2235. }
  2236. return Math.ceil($$.x(value))
  2237. };
  2238. ChartInternal.prototype.yv = function(d) {
  2239. var $$ = this,
  2240. yScale = d.axis && d.axis === 'y2' ? $$.y2 : $$.y;
  2241. return Math.ceil(yScale(d.value))
  2242. };
  2243. ChartInternal.prototype.subxx = function(d) {
  2244. return d ? this.subX(d.x) : null
  2245. };
  2246. ChartInternal.prototype.transformMain = function(withTransition, transitions) {
  2247. var $$ = this,
  2248. xAxis,
  2249. yAxis,
  2250. y2Axis;
  2251. if (transitions && transitions.axisX) {
  2252. xAxis = transitions.axisX;
  2253. } else {
  2254. xAxis = $$.main.select('.' + CLASS.axisX);
  2255. if (withTransition) {
  2256. xAxis = xAxis.transition();
  2257. }
  2258. }
  2259. if (transitions && transitions.axisY) {
  2260. yAxis = transitions.axisY;
  2261. } else {
  2262. yAxis = $$.main.select('.' + CLASS.axisY);
  2263. if (withTransition) {
  2264. yAxis = yAxis.transition();
  2265. }
  2266. }
  2267. if (transitions && transitions.axisY2) {
  2268. y2Axis = transitions.axisY2;
  2269. } else {
  2270. y2Axis = $$.main.select('.' + CLASS.axisY2);
  2271. if (withTransition) {
  2272. y2Axis = y2Axis.transition();
  2273. }
  2274. }
  2275. (withTransition ? $$.main.transition() : $$.main).attr(
  2276. 'transform',
  2277. $$.getTranslate('main')
  2278. );
  2279. xAxis.attr('transform', $$.getTranslate('x'));
  2280. yAxis.attr('transform', $$.getTranslate('y'));
  2281. y2Axis.attr('transform', $$.getTranslate('y2'));
  2282. $$.main
  2283. .select('.' + CLASS.chartArcs)
  2284. .attr('transform', $$.getTranslate('arc'));
  2285. };
  2286. ChartInternal.prototype.transformAll = function(withTransition, transitions) {
  2287. var $$ = this;
  2288. $$.transformMain(withTransition, transitions);
  2289. if ($$.config.subchart_show) {
  2290. $$.transformContext(withTransition, transitions);
  2291. }
  2292. if ($$.legend) {
  2293. $$.transformLegend(withTransition);
  2294. }
  2295. };
  2296. ChartInternal.prototype.updateSvgSize = function() {
  2297. var $$ = this,
  2298. brush = $$.svg.select(`.${CLASS.brush} .overlay`);
  2299. $$.svg.attr('width', $$.currentWidth).attr('height', $$.currentHeight);
  2300. $$.svg
  2301. .selectAll(['#' + $$.clipId, '#' + $$.clipIdForGrid])
  2302. .select('rect')
  2303. .attr('width', $$.width)
  2304. .attr('height', $$.height);
  2305. $$.svg
  2306. .select('#' + $$.clipIdForXAxis)
  2307. .select('rect')
  2308. .attr('x', $$.getXAxisClipX.bind($$))
  2309. .attr('y', $$.getXAxisClipY.bind($$))
  2310. .attr('width', $$.getXAxisClipWidth.bind($$))
  2311. .attr('height', $$.getXAxisClipHeight.bind($$));
  2312. $$.svg
  2313. .select('#' + $$.clipIdForYAxis)
  2314. .select('rect')
  2315. .attr('x', $$.getYAxisClipX.bind($$))
  2316. .attr('y', $$.getYAxisClipY.bind($$))
  2317. .attr('width', $$.getYAxisClipWidth.bind($$))
  2318. .attr('height', $$.getYAxisClipHeight.bind($$));
  2319. $$.svg
  2320. .select('#' + $$.clipIdForSubchart)
  2321. .select('rect')
  2322. .attr('width', $$.width)
  2323. .attr('height', (brush.size() && brush.attr('height')) || 0);
  2324. // MEMO: parent div's height will be bigger than svg when <!DOCTYPE html>
  2325. $$.selectChart.style('max-height', $$.currentHeight + 'px');
  2326. };
  2327. ChartInternal.prototype.updateDimension = function(withoutAxis) {
  2328. var $$ = this;
  2329. if (!withoutAxis) {
  2330. if ($$.config.axis_rotated) {
  2331. $$.axes.x.call($$.xAxis);
  2332. $$.axes.subx.call($$.subXAxis);
  2333. } else {
  2334. $$.axes.y.call($$.yAxis);
  2335. $$.axes.y2.call($$.y2Axis);
  2336. }
  2337. }
  2338. $$.updateSizes();
  2339. $$.updateScales();
  2340. $$.updateSvgSize();
  2341. $$.transformAll(false);
  2342. };
  2343. ChartInternal.prototype.observeInserted = function(selection) {
  2344. var $$ = this,
  2345. observer;
  2346. if (typeof MutationObserver === 'undefined') {
  2347. window.console.error('MutationObserver not defined.');
  2348. return
  2349. }
  2350. observer = new MutationObserver(function(mutations) {
  2351. mutations.forEach(function(mutation) {
  2352. if (mutation.type === 'childList' && mutation.previousSibling) {
  2353. observer.disconnect();
  2354. // need to wait for completion of load because size calculation requires the actual sizes determined after that completion
  2355. $$.intervalForObserveInserted = window.setInterval(function() {
  2356. // parentNode will NOT be null when completed
  2357. if (selection.node().parentNode) {
  2358. window.clearInterval($$.intervalForObserveInserted);
  2359. $$.updateDimension();
  2360. if ($$.brush) {
  2361. $$.brush.update();
  2362. }
  2363. $$.config.oninit.call($$);
  2364. $$.redraw({
  2365. withTransform: true,
  2366. withUpdateXDomain: true,
  2367. withUpdateOrgXDomain: true,
  2368. withTransition: false,
  2369. withTransitionForTransform: false,
  2370. withLegend: true
  2371. });
  2372. selection.transition().style('opacity', 1);
  2373. }
  2374. }, 10);
  2375. }
  2376. });
  2377. });
  2378. observer.observe(selection.node(), {
  2379. attributes: true,
  2380. childList: true,
  2381. characterData: true
  2382. });
  2383. };
  2384. /**
  2385. * Binds handlers to the window resize event.
  2386. */
  2387. ChartInternal.prototype.bindResize = function() {
  2388. var $$ = this,
  2389. config = $$.config;
  2390. $$.resizeFunction = $$.generateResize(); // need to call .remove
  2391. $$.resizeFunction.add(function() {
  2392. config.onresize.call($$);
  2393. });
  2394. if (config.resize_auto) {
  2395. $$.resizeFunction.add(function() {
  2396. if ($$.resizeTimeout !== undefined) {
  2397. window.clearTimeout($$.resizeTimeout);
  2398. }
  2399. $$.resizeTimeout = window.setTimeout(function() {
  2400. delete $$.resizeTimeout;
  2401. $$.updateAndRedraw({
  2402. withUpdateXDomain: false,
  2403. withUpdateOrgXDomain: false,
  2404. withTransition: false,
  2405. withTransitionForTransform: false,
  2406. withLegend: true
  2407. });
  2408. if ($$.brush) {
  2409. $$.brush.update();
  2410. }
  2411. }, 100);
  2412. });
  2413. }
  2414. $$.resizeFunction.add(function() {
  2415. config.onresized.call($$);
  2416. });
  2417. $$.resizeIfElementDisplayed = function() {
  2418. // if element not displayed skip it
  2419. if ($$.api == null || !$$.api.element.offsetParent) {
  2420. return
  2421. }
  2422. $$.resizeFunction();
  2423. };
  2424. if (window.attachEvent) {
  2425. window.attachEvent('onresize', $$.resizeIfElementDisplayed);
  2426. } else if (window.addEventListener) {
  2427. window.addEventListener('resize', $$.resizeIfElementDisplayed, false);
  2428. } else {
  2429. // fallback to this, if this is a very old browser
  2430. var wrapper = window.onresize;
  2431. if (!wrapper) {
  2432. // create a wrapper that will call all charts
  2433. wrapper = $$.generateResize();
  2434. } else if (!wrapper.add || !wrapper.remove) {
  2435. // there is already a handler registered, make sure we call it too
  2436. wrapper = $$.generateResize();
  2437. wrapper.add(window.onresize);
  2438. }
  2439. // add this graph to the wrapper, we will be removed if the user calls destroy
  2440. wrapper.add($$.resizeFunction);
  2441. window.onresize = function() {
  2442. // if element not displayed skip it
  2443. if (!$$.api.element.offsetParent) {
  2444. return
  2445. }
  2446. wrapper();
  2447. };
  2448. }
  2449. };
  2450. /**
  2451. * Binds handlers to the window focus event.
  2452. */
  2453. ChartInternal.prototype.bindWindowFocus = function() {
  2454. if (this.windowFocusHandler) {
  2455. // The handler is already set
  2456. return
  2457. }
  2458. this.windowFocusHandler = () => {
  2459. this.redraw();
  2460. };
  2461. window.addEventListener('focus', this.windowFocusHandler);
  2462. };
  2463. /**
  2464. * Unbinds from the window focus event.
  2465. */
  2466. ChartInternal.prototype.unbindWindowFocus = function() {
  2467. window.removeEventListener('focus', this.windowFocusHandler);
  2468. delete this.windowFocusHandler;
  2469. };
  2470. ChartInternal.prototype.generateResize = function() {
  2471. var resizeFunctions = [];
  2472. function callResizeFunctions() {
  2473. resizeFunctions.forEach(function(f) {
  2474. f();
  2475. });
  2476. }
  2477. callResizeFunctions.add = function(f) {
  2478. resizeFunctions.push(f);
  2479. };
  2480. callResizeFunctions.remove = function(f) {
  2481. for (var i = 0; i < resizeFunctions.length; i++) {
  2482. if (resizeFunctions[i] === f) {
  2483. resizeFunctions.splice(i, 1);
  2484. break
  2485. }
  2486. }
  2487. };
  2488. return callResizeFunctions
  2489. };
  2490. ChartInternal.prototype.endall = function(transition, callback) {
  2491. var n = 0;
  2492. transition
  2493. .each(function() {
  2494. ++n;
  2495. })
  2496. .on('end', function() {
  2497. if (!--n) {
  2498. callback.apply(this, arguments);
  2499. }
  2500. });
  2501. };
  2502. ChartInternal.prototype.generateWait = function() {
  2503. var $$ = this;
  2504. var transitionsToWait = [],
  2505. f = function(callback) {
  2506. var timer = setInterval(function() {
  2507. if (!$$.isTabVisible()) {
  2508. return
  2509. }
  2510. var done = 0;
  2511. transitionsToWait.forEach(function(t) {
  2512. if (t.empty()) {
  2513. done += 1;
  2514. return
  2515. }
  2516. try {
  2517. t.transition();
  2518. } catch (e) {
  2519. done += 1;
  2520. }
  2521. });
  2522. if (done === transitionsToWait.length) {
  2523. clearInterval(timer);
  2524. if (callback) {
  2525. callback();
  2526. }
  2527. }
  2528. }, 50);
  2529. };
  2530. f.add = function(transition) {
  2531. transitionsToWait.push(transition);
  2532. };
  2533. return f
  2534. };
  2535. ChartInternal.prototype.parseDate = function(date) {
  2536. var $$ = this,
  2537. parsedDate;
  2538. if (date instanceof Date) {
  2539. parsedDate = date;
  2540. } else if (typeof date === 'string') {
  2541. parsedDate = $$.dataTimeParse(date);
  2542. } else if (typeof date === 'object') {
  2543. parsedDate = new Date(+date);
  2544. } else if (typeof date === 'number' && !isNaN(date)) {
  2545. parsedDate = new Date(+date);
  2546. }
  2547. if (!parsedDate || isNaN(+parsedDate)) {
  2548. window.console.error("Failed to parse x '" + date + "' to Date object");
  2549. }
  2550. return parsedDate
  2551. };
  2552. ChartInternal.prototype.isTabVisible = function() {
  2553. return !document.hidden
  2554. };
  2555. ChartInternal.prototype.getPathBox = getPathBox;
  2556. ChartInternal.prototype.CLASS = CLASS;
  2557. /* jshint ignore:start */
  2558. (function() {
  2559. if (!('SVGPathSeg' in window)) {
  2560. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGPathSeg
  2561. window.SVGPathSeg = function(type, typeAsLetter, owningPathSegList) {
  2562. this.pathSegType = type;
  2563. this.pathSegTypeAsLetter = typeAsLetter;
  2564. this._owningPathSegList = owningPathSegList;
  2565. };
  2566. window.SVGPathSeg.prototype.classname = 'SVGPathSeg';
  2567. window.SVGPathSeg.PATHSEG_UNKNOWN = 0;
  2568. window.SVGPathSeg.PATHSEG_CLOSEPATH = 1;
  2569. window.SVGPathSeg.PATHSEG_MOVETO_ABS = 2;
  2570. window.SVGPathSeg.PATHSEG_MOVETO_REL = 3;
  2571. window.SVGPathSeg.PATHSEG_LINETO_ABS = 4;
  2572. window.SVGPathSeg.PATHSEG_LINETO_REL = 5;
  2573. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS = 6;
  2574. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL = 7;
  2575. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS = 8;
  2576. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL = 9;
  2577. window.SVGPathSeg.PATHSEG_ARC_ABS = 10;
  2578. window.SVGPathSeg.PATHSEG_ARC_REL = 11;
  2579. window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS = 12;
  2580. window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL = 13;
  2581. window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS = 14;
  2582. window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL = 15;
  2583. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS = 16;
  2584. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL = 17;
  2585. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS = 18;
  2586. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL = 19;
  2587. // Notify owning PathSegList on any changes so they can be synchronized back to the path element.
  2588. window.SVGPathSeg.prototype._segmentChanged = function() {
  2589. if (this._owningPathSegList) this._owningPathSegList.segmentChanged(this);
  2590. };
  2591. window.SVGPathSegClosePath = function(owningPathSegList) {
  2592. window.SVGPathSeg.call(
  2593. this,
  2594. window.SVGPathSeg.PATHSEG_CLOSEPATH,
  2595. 'z',
  2596. owningPathSegList
  2597. );
  2598. };
  2599. window.SVGPathSegClosePath.prototype = Object.create(
  2600. window.SVGPathSeg.prototype
  2601. );
  2602. window.SVGPathSegClosePath.prototype.toString = function() {
  2603. return '[object SVGPathSegClosePath]'
  2604. };
  2605. window.SVGPathSegClosePath.prototype._asPathString = function() {
  2606. return this.pathSegTypeAsLetter
  2607. };
  2608. window.SVGPathSegClosePath.prototype.clone = function() {
  2609. return new window.SVGPathSegClosePath(undefined)
  2610. };
  2611. window.SVGPathSegMovetoAbs = function(owningPathSegList, x, y) {
  2612. window.SVGPathSeg.call(
  2613. this,
  2614. window.SVGPathSeg.PATHSEG_MOVETO_ABS,
  2615. 'M',
  2616. owningPathSegList
  2617. );
  2618. this._x = x;
  2619. this._y = y;
  2620. };
  2621. window.SVGPathSegMovetoAbs.prototype = Object.create(
  2622. window.SVGPathSeg.prototype
  2623. );
  2624. window.SVGPathSegMovetoAbs.prototype.toString = function() {
  2625. return '[object SVGPathSegMovetoAbs]'
  2626. };
  2627. window.SVGPathSegMovetoAbs.prototype._asPathString = function() {
  2628. return this.pathSegTypeAsLetter + ' ' + this._x + ' ' + this._y
  2629. };
  2630. window.SVGPathSegMovetoAbs.prototype.clone = function() {
  2631. return new window.SVGPathSegMovetoAbs(undefined, this._x, this._y)
  2632. };
  2633. Object.defineProperty(window.SVGPathSegMovetoAbs.prototype, 'x', {
  2634. get: function() {
  2635. return this._x
  2636. },
  2637. set: function(x) {
  2638. this._x = x;
  2639. this._segmentChanged();
  2640. },
  2641. enumerable: true
  2642. });
  2643. Object.defineProperty(window.SVGPathSegMovetoAbs.prototype, 'y', {
  2644. get: function() {
  2645. return this._y
  2646. },
  2647. set: function(y) {
  2648. this._y = y;
  2649. this._segmentChanged();
  2650. },
  2651. enumerable: true
  2652. });
  2653. window.SVGPathSegMovetoRel = function(owningPathSegList, x, y) {
  2654. window.SVGPathSeg.call(
  2655. this,
  2656. window.SVGPathSeg.PATHSEG_MOVETO_REL,
  2657. 'm',
  2658. owningPathSegList
  2659. );
  2660. this._x = x;
  2661. this._y = y;
  2662. };
  2663. window.SVGPathSegMovetoRel.prototype = Object.create(
  2664. window.SVGPathSeg.prototype
  2665. );
  2666. window.SVGPathSegMovetoRel.prototype.toString = function() {
  2667. return '[object SVGPathSegMovetoRel]'
  2668. };
  2669. window.SVGPathSegMovetoRel.prototype._asPathString = function() {
  2670. return this.pathSegTypeAsLetter + ' ' + this._x + ' ' + this._y
  2671. };
  2672. window.SVGPathSegMovetoRel.prototype.clone = function() {
  2673. return new window.SVGPathSegMovetoRel(undefined, this._x, this._y)
  2674. };
  2675. Object.defineProperty(window.SVGPathSegMovetoRel.prototype, 'x', {
  2676. get: function() {
  2677. return this._x
  2678. },
  2679. set: function(x) {
  2680. this._x = x;
  2681. this._segmentChanged();
  2682. },
  2683. enumerable: true
  2684. });
  2685. Object.defineProperty(window.SVGPathSegMovetoRel.prototype, 'y', {
  2686. get: function() {
  2687. return this._y
  2688. },
  2689. set: function(y) {
  2690. this._y = y;
  2691. this._segmentChanged();
  2692. },
  2693. enumerable: true
  2694. });
  2695. window.SVGPathSegLinetoAbs = function(owningPathSegList, x, y) {
  2696. window.SVGPathSeg.call(
  2697. this,
  2698. window.SVGPathSeg.PATHSEG_LINETO_ABS,
  2699. 'L',
  2700. owningPathSegList
  2701. );
  2702. this._x = x;
  2703. this._y = y;
  2704. };
  2705. window.SVGPathSegLinetoAbs.prototype = Object.create(
  2706. window.SVGPathSeg.prototype
  2707. );
  2708. window.SVGPathSegLinetoAbs.prototype.toString = function() {
  2709. return '[object SVGPathSegLinetoAbs]'
  2710. };
  2711. window.SVGPathSegLinetoAbs.prototype._asPathString = function() {
  2712. return this.pathSegTypeAsLetter + ' ' + this._x + ' ' + this._y
  2713. };
  2714. window.SVGPathSegLinetoAbs.prototype.clone = function() {
  2715. return new window.SVGPathSegLinetoAbs(undefined, this._x, this._y)
  2716. };
  2717. Object.defineProperty(window.SVGPathSegLinetoAbs.prototype, 'x', {
  2718. get: function() {
  2719. return this._x
  2720. },
  2721. set: function(x) {
  2722. this._x = x;
  2723. this._segmentChanged();
  2724. },
  2725. enumerable: true
  2726. });
  2727. Object.defineProperty(window.SVGPathSegLinetoAbs.prototype, 'y', {
  2728. get: function() {
  2729. return this._y
  2730. },
  2731. set: function(y) {
  2732. this._y = y;
  2733. this._segmentChanged();
  2734. },
  2735. enumerable: true
  2736. });
  2737. window.SVGPathSegLinetoRel = function(owningPathSegList, x, y) {
  2738. window.SVGPathSeg.call(
  2739. this,
  2740. window.SVGPathSeg.PATHSEG_LINETO_REL,
  2741. 'l',
  2742. owningPathSegList
  2743. );
  2744. this._x = x;
  2745. this._y = y;
  2746. };
  2747. window.SVGPathSegLinetoRel.prototype = Object.create(
  2748. window.SVGPathSeg.prototype
  2749. );
  2750. window.SVGPathSegLinetoRel.prototype.toString = function() {
  2751. return '[object SVGPathSegLinetoRel]'
  2752. };
  2753. window.SVGPathSegLinetoRel.prototype._asPathString = function() {
  2754. return this.pathSegTypeAsLetter + ' ' + this._x + ' ' + this._y
  2755. };
  2756. window.SVGPathSegLinetoRel.prototype.clone = function() {
  2757. return new window.SVGPathSegLinetoRel(undefined, this._x, this._y)
  2758. };
  2759. Object.defineProperty(window.SVGPathSegLinetoRel.prototype, 'x', {
  2760. get: function() {
  2761. return this._x
  2762. },
  2763. set: function(x) {
  2764. this._x = x;
  2765. this._segmentChanged();
  2766. },
  2767. enumerable: true
  2768. });
  2769. Object.defineProperty(window.SVGPathSegLinetoRel.prototype, 'y', {
  2770. get: function() {
  2771. return this._y
  2772. },
  2773. set: function(y) {
  2774. this._y = y;
  2775. this._segmentChanged();
  2776. },
  2777. enumerable: true
  2778. });
  2779. window.SVGPathSegCurvetoCubicAbs = function(
  2780. owningPathSegList,
  2781. x,
  2782. y,
  2783. x1,
  2784. y1,
  2785. x2,
  2786. y2
  2787. ) {
  2788. window.SVGPathSeg.call(
  2789. this,
  2790. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS,
  2791. 'C',
  2792. owningPathSegList
  2793. );
  2794. this._x = x;
  2795. this._y = y;
  2796. this._x1 = x1;
  2797. this._y1 = y1;
  2798. this._x2 = x2;
  2799. this._y2 = y2;
  2800. };
  2801. window.SVGPathSegCurvetoCubicAbs.prototype = Object.create(
  2802. window.SVGPathSeg.prototype
  2803. );
  2804. window.SVGPathSegCurvetoCubicAbs.prototype.toString = function() {
  2805. return '[object SVGPathSegCurvetoCubicAbs]'
  2806. };
  2807. window.SVGPathSegCurvetoCubicAbs.prototype._asPathString = function() {
  2808. return (
  2809. this.pathSegTypeAsLetter +
  2810. ' ' +
  2811. this._x1 +
  2812. ' ' +
  2813. this._y1 +
  2814. ' ' +
  2815. this._x2 +
  2816. ' ' +
  2817. this._y2 +
  2818. ' ' +
  2819. this._x +
  2820. ' ' +
  2821. this._y
  2822. )
  2823. };
  2824. window.SVGPathSegCurvetoCubicAbs.prototype.clone = function() {
  2825. return new window.SVGPathSegCurvetoCubicAbs(
  2826. undefined,
  2827. this._x,
  2828. this._y,
  2829. this._x1,
  2830. this._y1,
  2831. this._x2,
  2832. this._y2
  2833. )
  2834. };
  2835. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, 'x', {
  2836. get: function() {
  2837. return this._x
  2838. },
  2839. set: function(x) {
  2840. this._x = x;
  2841. this._segmentChanged();
  2842. },
  2843. enumerable: true
  2844. });
  2845. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, 'y', {
  2846. get: function() {
  2847. return this._y
  2848. },
  2849. set: function(y) {
  2850. this._y = y;
  2851. this._segmentChanged();
  2852. },
  2853. enumerable: true
  2854. });
  2855. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, 'x1', {
  2856. get: function() {
  2857. return this._x1
  2858. },
  2859. set: function(x1) {
  2860. this._x1 = x1;
  2861. this._segmentChanged();
  2862. },
  2863. enumerable: true
  2864. });
  2865. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, 'y1', {
  2866. get: function() {
  2867. return this._y1
  2868. },
  2869. set: function(y1) {
  2870. this._y1 = y1;
  2871. this._segmentChanged();
  2872. },
  2873. enumerable: true
  2874. });
  2875. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, 'x2', {
  2876. get: function() {
  2877. return this._x2
  2878. },
  2879. set: function(x2) {
  2880. this._x2 = x2;
  2881. this._segmentChanged();
  2882. },
  2883. enumerable: true
  2884. });
  2885. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, 'y2', {
  2886. get: function() {
  2887. return this._y2
  2888. },
  2889. set: function(y2) {
  2890. this._y2 = y2;
  2891. this._segmentChanged();
  2892. },
  2893. enumerable: true
  2894. });
  2895. window.SVGPathSegCurvetoCubicRel = function(
  2896. owningPathSegList,
  2897. x,
  2898. y,
  2899. x1,
  2900. y1,
  2901. x2,
  2902. y2
  2903. ) {
  2904. window.SVGPathSeg.call(
  2905. this,
  2906. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL,
  2907. 'c',
  2908. owningPathSegList
  2909. );
  2910. this._x = x;
  2911. this._y = y;
  2912. this._x1 = x1;
  2913. this._y1 = y1;
  2914. this._x2 = x2;
  2915. this._y2 = y2;
  2916. };
  2917. window.SVGPathSegCurvetoCubicRel.prototype = Object.create(
  2918. window.SVGPathSeg.prototype
  2919. );
  2920. window.SVGPathSegCurvetoCubicRel.prototype.toString = function() {
  2921. return '[object SVGPathSegCurvetoCubicRel]'
  2922. };
  2923. window.SVGPathSegCurvetoCubicRel.prototype._asPathString = function() {
  2924. return (
  2925. this.pathSegTypeAsLetter +
  2926. ' ' +
  2927. this._x1 +
  2928. ' ' +
  2929. this._y1 +
  2930. ' ' +
  2931. this._x2 +
  2932. ' ' +
  2933. this._y2 +
  2934. ' ' +
  2935. this._x +
  2936. ' ' +
  2937. this._y
  2938. )
  2939. };
  2940. window.SVGPathSegCurvetoCubicRel.prototype.clone = function() {
  2941. return new window.SVGPathSegCurvetoCubicRel(
  2942. undefined,
  2943. this._x,
  2944. this._y,
  2945. this._x1,
  2946. this._y1,
  2947. this._x2,
  2948. this._y2
  2949. )
  2950. };
  2951. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, 'x', {
  2952. get: function() {
  2953. return this._x
  2954. },
  2955. set: function(x) {
  2956. this._x = x;
  2957. this._segmentChanged();
  2958. },
  2959. enumerable: true
  2960. });
  2961. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, 'y', {
  2962. get: function() {
  2963. return this._y
  2964. },
  2965. set: function(y) {
  2966. this._y = y;
  2967. this._segmentChanged();
  2968. },
  2969. enumerable: true
  2970. });
  2971. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, 'x1', {
  2972. get: function() {
  2973. return this._x1
  2974. },
  2975. set: function(x1) {
  2976. this._x1 = x1;
  2977. this._segmentChanged();
  2978. },
  2979. enumerable: true
  2980. });
  2981. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, 'y1', {
  2982. get: function() {
  2983. return this._y1
  2984. },
  2985. set: function(y1) {
  2986. this._y1 = y1;
  2987. this._segmentChanged();
  2988. },
  2989. enumerable: true
  2990. });
  2991. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, 'x2', {
  2992. get: function() {
  2993. return this._x2
  2994. },
  2995. set: function(x2) {
  2996. this._x2 = x2;
  2997. this._segmentChanged();
  2998. },
  2999. enumerable: true
  3000. });
  3001. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, 'y2', {
  3002. get: function() {
  3003. return this._y2
  3004. },
  3005. set: function(y2) {
  3006. this._y2 = y2;
  3007. this._segmentChanged();
  3008. },
  3009. enumerable: true
  3010. });
  3011. window.SVGPathSegCurvetoQuadraticAbs = function(
  3012. owningPathSegList,
  3013. x,
  3014. y,
  3015. x1,
  3016. y1
  3017. ) {
  3018. window.SVGPathSeg.call(
  3019. this,
  3020. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS,
  3021. 'Q',
  3022. owningPathSegList
  3023. );
  3024. this._x = x;
  3025. this._y = y;
  3026. this._x1 = x1;
  3027. this._y1 = y1;
  3028. };
  3029. window.SVGPathSegCurvetoQuadraticAbs.prototype = Object.create(
  3030. window.SVGPathSeg.prototype
  3031. );
  3032. window.SVGPathSegCurvetoQuadraticAbs.prototype.toString = function() {
  3033. return '[object SVGPathSegCurvetoQuadraticAbs]'
  3034. };
  3035. window.SVGPathSegCurvetoQuadraticAbs.prototype._asPathString = function() {
  3036. return (
  3037. this.pathSegTypeAsLetter +
  3038. ' ' +
  3039. this._x1 +
  3040. ' ' +
  3041. this._y1 +
  3042. ' ' +
  3043. this._x +
  3044. ' ' +
  3045. this._y
  3046. )
  3047. };
  3048. window.SVGPathSegCurvetoQuadraticAbs.prototype.clone = function() {
  3049. return new window.SVGPathSegCurvetoQuadraticAbs(
  3050. undefined,
  3051. this._x,
  3052. this._y,
  3053. this._x1,
  3054. this._y1
  3055. )
  3056. };
  3057. Object.defineProperty(window.SVGPathSegCurvetoQuadraticAbs.prototype, 'x', {
  3058. get: function() {
  3059. return this._x
  3060. },
  3061. set: function(x) {
  3062. this._x = x;
  3063. this._segmentChanged();
  3064. },
  3065. enumerable: true
  3066. });
  3067. Object.defineProperty(window.SVGPathSegCurvetoQuadraticAbs.prototype, 'y', {
  3068. get: function() {
  3069. return this._y
  3070. },
  3071. set: function(y) {
  3072. this._y = y;
  3073. this._segmentChanged();
  3074. },
  3075. enumerable: true
  3076. });
  3077. Object.defineProperty(
  3078. window.SVGPathSegCurvetoQuadraticAbs.prototype,
  3079. 'x1',
  3080. {
  3081. get: function() {
  3082. return this._x1
  3083. },
  3084. set: function(x1) {
  3085. this._x1 = x1;
  3086. this._segmentChanged();
  3087. },
  3088. enumerable: true
  3089. }
  3090. );
  3091. Object.defineProperty(
  3092. window.SVGPathSegCurvetoQuadraticAbs.prototype,
  3093. 'y1',
  3094. {
  3095. get: function() {
  3096. return this._y1
  3097. },
  3098. set: function(y1) {
  3099. this._y1 = y1;
  3100. this._segmentChanged();
  3101. },
  3102. enumerable: true
  3103. }
  3104. );
  3105. window.SVGPathSegCurvetoQuadraticRel = function(
  3106. owningPathSegList,
  3107. x,
  3108. y,
  3109. x1,
  3110. y1
  3111. ) {
  3112. window.SVGPathSeg.call(
  3113. this,
  3114. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL,
  3115. 'q',
  3116. owningPathSegList
  3117. );
  3118. this._x = x;
  3119. this._y = y;
  3120. this._x1 = x1;
  3121. this._y1 = y1;
  3122. };
  3123. window.SVGPathSegCurvetoQuadraticRel.prototype = Object.create(
  3124. window.SVGPathSeg.prototype
  3125. );
  3126. window.SVGPathSegCurvetoQuadraticRel.prototype.toString = function() {
  3127. return '[object SVGPathSegCurvetoQuadraticRel]'
  3128. };
  3129. window.SVGPathSegCurvetoQuadraticRel.prototype._asPathString = function() {
  3130. return (
  3131. this.pathSegTypeAsLetter +
  3132. ' ' +
  3133. this._x1 +
  3134. ' ' +
  3135. this._y1 +
  3136. ' ' +
  3137. this._x +
  3138. ' ' +
  3139. this._y
  3140. )
  3141. };
  3142. window.SVGPathSegCurvetoQuadraticRel.prototype.clone = function() {
  3143. return new window.SVGPathSegCurvetoQuadraticRel(
  3144. undefined,
  3145. this._x,
  3146. this._y,
  3147. this._x1,
  3148. this._y1
  3149. )
  3150. };
  3151. Object.defineProperty(window.SVGPathSegCurvetoQuadraticRel.prototype, 'x', {
  3152. get: function() {
  3153. return this._x
  3154. },
  3155. set: function(x) {
  3156. this._x = x;
  3157. this._segmentChanged();
  3158. },
  3159. enumerable: true
  3160. });
  3161. Object.defineProperty(window.SVGPathSegCurvetoQuadraticRel.prototype, 'y', {
  3162. get: function() {
  3163. return this._y
  3164. },
  3165. set: function(y) {
  3166. this._y = y;
  3167. this._segmentChanged();
  3168. },
  3169. enumerable: true
  3170. });
  3171. Object.defineProperty(
  3172. window.SVGPathSegCurvetoQuadraticRel.prototype,
  3173. 'x1',
  3174. {
  3175. get: function() {
  3176. return this._x1
  3177. },
  3178. set: function(x1) {
  3179. this._x1 = x1;
  3180. this._segmentChanged();
  3181. },
  3182. enumerable: true
  3183. }
  3184. );
  3185. Object.defineProperty(
  3186. window.SVGPathSegCurvetoQuadraticRel.prototype,
  3187. 'y1',
  3188. {
  3189. get: function() {
  3190. return this._y1
  3191. },
  3192. set: function(y1) {
  3193. this._y1 = y1;
  3194. this._segmentChanged();
  3195. },
  3196. enumerable: true
  3197. }
  3198. );
  3199. window.SVGPathSegArcAbs = function(
  3200. owningPathSegList,
  3201. x,
  3202. y,
  3203. r1,
  3204. r2,
  3205. angle,
  3206. largeArcFlag,
  3207. sweepFlag
  3208. ) {
  3209. window.SVGPathSeg.call(
  3210. this,
  3211. window.SVGPathSeg.PATHSEG_ARC_ABS,
  3212. 'A',
  3213. owningPathSegList
  3214. );
  3215. this._x = x;
  3216. this._y = y;
  3217. this._r1 = r1;
  3218. this._r2 = r2;
  3219. this._angle = angle;
  3220. this._largeArcFlag = largeArcFlag;
  3221. this._sweepFlag = sweepFlag;
  3222. };
  3223. window.SVGPathSegArcAbs.prototype = Object.create(
  3224. window.SVGPathSeg.prototype
  3225. );
  3226. window.SVGPathSegArcAbs.prototype.toString = function() {
  3227. return '[object SVGPathSegArcAbs]'
  3228. };
  3229. window.SVGPathSegArcAbs.prototype._asPathString = function() {
  3230. return (
  3231. this.pathSegTypeAsLetter +
  3232. ' ' +
  3233. this._r1 +
  3234. ' ' +
  3235. this._r2 +
  3236. ' ' +
  3237. this._angle +
  3238. ' ' +
  3239. (this._largeArcFlag ? '1' : '0') +
  3240. ' ' +
  3241. (this._sweepFlag ? '1' : '0') +
  3242. ' ' +
  3243. this._x +
  3244. ' ' +
  3245. this._y
  3246. )
  3247. };
  3248. window.SVGPathSegArcAbs.prototype.clone = function() {
  3249. return new window.SVGPathSegArcAbs(
  3250. undefined,
  3251. this._x,
  3252. this._y,
  3253. this._r1,
  3254. this._r2,
  3255. this._angle,
  3256. this._largeArcFlag,
  3257. this._sweepFlag
  3258. )
  3259. };
  3260. Object.defineProperty(window.SVGPathSegArcAbs.prototype, 'x', {
  3261. get: function() {
  3262. return this._x
  3263. },
  3264. set: function(x) {
  3265. this._x = x;
  3266. this._segmentChanged();
  3267. },
  3268. enumerable: true
  3269. });
  3270. Object.defineProperty(window.SVGPathSegArcAbs.prototype, 'y', {
  3271. get: function() {
  3272. return this._y
  3273. },
  3274. set: function(y) {
  3275. this._y = y;
  3276. this._segmentChanged();
  3277. },
  3278. enumerable: true
  3279. });
  3280. Object.defineProperty(window.SVGPathSegArcAbs.prototype, 'r1', {
  3281. get: function() {
  3282. return this._r1
  3283. },
  3284. set: function(r1) {
  3285. this._r1 = r1;
  3286. this._segmentChanged();
  3287. },
  3288. enumerable: true
  3289. });
  3290. Object.defineProperty(window.SVGPathSegArcAbs.prototype, 'r2', {
  3291. get: function() {
  3292. return this._r2
  3293. },
  3294. set: function(r2) {
  3295. this._r2 = r2;
  3296. this._segmentChanged();
  3297. },
  3298. enumerable: true
  3299. });
  3300. Object.defineProperty(window.SVGPathSegArcAbs.prototype, 'angle', {
  3301. get: function() {
  3302. return this._angle
  3303. },
  3304. set: function(angle) {
  3305. this._angle = angle;
  3306. this._segmentChanged();
  3307. },
  3308. enumerable: true
  3309. });
  3310. Object.defineProperty(window.SVGPathSegArcAbs.prototype, 'largeArcFlag', {
  3311. get: function() {
  3312. return this._largeArcFlag
  3313. },
  3314. set: function(largeArcFlag) {
  3315. this._largeArcFlag = largeArcFlag;
  3316. this._segmentChanged();
  3317. },
  3318. enumerable: true
  3319. });
  3320. Object.defineProperty(window.SVGPathSegArcAbs.prototype, 'sweepFlag', {
  3321. get: function() {
  3322. return this._sweepFlag
  3323. },
  3324. set: function(sweepFlag) {
  3325. this._sweepFlag = sweepFlag;
  3326. this._segmentChanged();
  3327. },
  3328. enumerable: true
  3329. });
  3330. window.SVGPathSegArcRel = function(
  3331. owningPathSegList,
  3332. x,
  3333. y,
  3334. r1,
  3335. r2,
  3336. angle,
  3337. largeArcFlag,
  3338. sweepFlag
  3339. ) {
  3340. window.SVGPathSeg.call(
  3341. this,
  3342. window.SVGPathSeg.PATHSEG_ARC_REL,
  3343. 'a',
  3344. owningPathSegList
  3345. );
  3346. this._x = x;
  3347. this._y = y;
  3348. this._r1 = r1;
  3349. this._r2 = r2;
  3350. this._angle = angle;
  3351. this._largeArcFlag = largeArcFlag;
  3352. this._sweepFlag = sweepFlag;
  3353. };
  3354. window.SVGPathSegArcRel.prototype = Object.create(
  3355. window.SVGPathSeg.prototype
  3356. );
  3357. window.SVGPathSegArcRel.prototype.toString = function() {
  3358. return '[object SVGPathSegArcRel]'
  3359. };
  3360. window.SVGPathSegArcRel.prototype._asPathString = function() {
  3361. return (
  3362. this.pathSegTypeAsLetter +
  3363. ' ' +
  3364. this._r1 +
  3365. ' ' +
  3366. this._r2 +
  3367. ' ' +
  3368. this._angle +
  3369. ' ' +
  3370. (this._largeArcFlag ? '1' : '0') +
  3371. ' ' +
  3372. (this._sweepFlag ? '1' : '0') +
  3373. ' ' +
  3374. this._x +
  3375. ' ' +
  3376. this._y
  3377. )
  3378. };
  3379. window.SVGPathSegArcRel.prototype.clone = function() {
  3380. return new window.SVGPathSegArcRel(
  3381. undefined,
  3382. this._x,
  3383. this._y,
  3384. this._r1,
  3385. this._r2,
  3386. this._angle,
  3387. this._largeArcFlag,
  3388. this._sweepFlag
  3389. )
  3390. };
  3391. Object.defineProperty(window.SVGPathSegArcRel.prototype, 'x', {
  3392. get: function() {
  3393. return this._x
  3394. },
  3395. set: function(x) {
  3396. this._x = x;
  3397. this._segmentChanged();
  3398. },
  3399. enumerable: true
  3400. });
  3401. Object.defineProperty(window.SVGPathSegArcRel.prototype, 'y', {
  3402. get: function() {
  3403. return this._y
  3404. },
  3405. set: function(y) {
  3406. this._y = y;
  3407. this._segmentChanged();
  3408. },
  3409. enumerable: true
  3410. });
  3411. Object.defineProperty(window.SVGPathSegArcRel.prototype, 'r1', {
  3412. get: function() {
  3413. return this._r1
  3414. },
  3415. set: function(r1) {
  3416. this._r1 = r1;
  3417. this._segmentChanged();
  3418. },
  3419. enumerable: true
  3420. });
  3421. Object.defineProperty(window.SVGPathSegArcRel.prototype, 'r2', {
  3422. get: function() {
  3423. return this._r2
  3424. },
  3425. set: function(r2) {
  3426. this._r2 = r2;
  3427. this._segmentChanged();
  3428. },
  3429. enumerable: true
  3430. });
  3431. Object.defineProperty(window.SVGPathSegArcRel.prototype, 'angle', {
  3432. get: function() {
  3433. return this._angle
  3434. },
  3435. set: function(angle) {
  3436. this._angle = angle;
  3437. this._segmentChanged();
  3438. },
  3439. enumerable: true
  3440. });
  3441. Object.defineProperty(window.SVGPathSegArcRel.prototype, 'largeArcFlag', {
  3442. get: function() {
  3443. return this._largeArcFlag
  3444. },
  3445. set: function(largeArcFlag) {
  3446. this._largeArcFlag = largeArcFlag;
  3447. this._segmentChanged();
  3448. },
  3449. enumerable: true
  3450. });
  3451. Object.defineProperty(window.SVGPathSegArcRel.prototype, 'sweepFlag', {
  3452. get: function() {
  3453. return this._sweepFlag
  3454. },
  3455. set: function(sweepFlag) {
  3456. this._sweepFlag = sweepFlag;
  3457. this._segmentChanged();
  3458. },
  3459. enumerable: true
  3460. });
  3461. window.SVGPathSegLinetoHorizontalAbs = function(owningPathSegList, x) {
  3462. window.SVGPathSeg.call(
  3463. this,
  3464. window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS,
  3465. 'H',
  3466. owningPathSegList
  3467. );
  3468. this._x = x;
  3469. };
  3470. window.SVGPathSegLinetoHorizontalAbs.prototype = Object.create(
  3471. window.SVGPathSeg.prototype
  3472. );
  3473. window.SVGPathSegLinetoHorizontalAbs.prototype.toString = function() {
  3474. return '[object SVGPathSegLinetoHorizontalAbs]'
  3475. };
  3476. window.SVGPathSegLinetoHorizontalAbs.prototype._asPathString = function() {
  3477. return this.pathSegTypeAsLetter + ' ' + this._x
  3478. };
  3479. window.SVGPathSegLinetoHorizontalAbs.prototype.clone = function() {
  3480. return new window.SVGPathSegLinetoHorizontalAbs(undefined, this._x)
  3481. };
  3482. Object.defineProperty(window.SVGPathSegLinetoHorizontalAbs.prototype, 'x', {
  3483. get: function() {
  3484. return this._x
  3485. },
  3486. set: function(x) {
  3487. this._x = x;
  3488. this._segmentChanged();
  3489. },
  3490. enumerable: true
  3491. });
  3492. window.SVGPathSegLinetoHorizontalRel = function(owningPathSegList, x) {
  3493. window.SVGPathSeg.call(
  3494. this,
  3495. window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL,
  3496. 'h',
  3497. owningPathSegList
  3498. );
  3499. this._x = x;
  3500. };
  3501. window.SVGPathSegLinetoHorizontalRel.prototype = Object.create(
  3502. window.SVGPathSeg.prototype
  3503. );
  3504. window.SVGPathSegLinetoHorizontalRel.prototype.toString = function() {
  3505. return '[object SVGPathSegLinetoHorizontalRel]'
  3506. };
  3507. window.SVGPathSegLinetoHorizontalRel.prototype._asPathString = function() {
  3508. return this.pathSegTypeAsLetter + ' ' + this._x
  3509. };
  3510. window.SVGPathSegLinetoHorizontalRel.prototype.clone = function() {
  3511. return new window.SVGPathSegLinetoHorizontalRel(undefined, this._x)
  3512. };
  3513. Object.defineProperty(window.SVGPathSegLinetoHorizontalRel.prototype, 'x', {
  3514. get: function() {
  3515. return this._x
  3516. },
  3517. set: function(x) {
  3518. this._x = x;
  3519. this._segmentChanged();
  3520. },
  3521. enumerable: true
  3522. });
  3523. window.SVGPathSegLinetoVerticalAbs = function(owningPathSegList, y) {
  3524. window.SVGPathSeg.call(
  3525. this,
  3526. window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS,
  3527. 'V',
  3528. owningPathSegList
  3529. );
  3530. this._y = y;
  3531. };
  3532. window.SVGPathSegLinetoVerticalAbs.prototype = Object.create(
  3533. window.SVGPathSeg.prototype
  3534. );
  3535. window.SVGPathSegLinetoVerticalAbs.prototype.toString = function() {
  3536. return '[object SVGPathSegLinetoVerticalAbs]'
  3537. };
  3538. window.SVGPathSegLinetoVerticalAbs.prototype._asPathString = function() {
  3539. return this.pathSegTypeAsLetter + ' ' + this._y
  3540. };
  3541. window.SVGPathSegLinetoVerticalAbs.prototype.clone = function() {
  3542. return new window.SVGPathSegLinetoVerticalAbs(undefined, this._y)
  3543. };
  3544. Object.defineProperty(window.SVGPathSegLinetoVerticalAbs.prototype, 'y', {
  3545. get: function() {
  3546. return this._y
  3547. },
  3548. set: function(y) {
  3549. this._y = y;
  3550. this._segmentChanged();
  3551. },
  3552. enumerable: true
  3553. });
  3554. window.SVGPathSegLinetoVerticalRel = function(owningPathSegList, y) {
  3555. window.SVGPathSeg.call(
  3556. this,
  3557. window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL,
  3558. 'v',
  3559. owningPathSegList
  3560. );
  3561. this._y = y;
  3562. };
  3563. window.SVGPathSegLinetoVerticalRel.prototype = Object.create(
  3564. window.SVGPathSeg.prototype
  3565. );
  3566. window.SVGPathSegLinetoVerticalRel.prototype.toString = function() {
  3567. return '[object SVGPathSegLinetoVerticalRel]'
  3568. };
  3569. window.SVGPathSegLinetoVerticalRel.prototype._asPathString = function() {
  3570. return this.pathSegTypeAsLetter + ' ' + this._y
  3571. };
  3572. window.SVGPathSegLinetoVerticalRel.prototype.clone = function() {
  3573. return new window.SVGPathSegLinetoVerticalRel(undefined, this._y)
  3574. };
  3575. Object.defineProperty(window.SVGPathSegLinetoVerticalRel.prototype, 'y', {
  3576. get: function() {
  3577. return this._y
  3578. },
  3579. set: function(y) {
  3580. this._y = y;
  3581. this._segmentChanged();
  3582. },
  3583. enumerable: true
  3584. });
  3585. window.SVGPathSegCurvetoCubicSmoothAbs = function(
  3586. owningPathSegList,
  3587. x,
  3588. y,
  3589. x2,
  3590. y2
  3591. ) {
  3592. window.SVGPathSeg.call(
  3593. this,
  3594. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS,
  3595. 'S',
  3596. owningPathSegList
  3597. );
  3598. this._x = x;
  3599. this._y = y;
  3600. this._x2 = x2;
  3601. this._y2 = y2;
  3602. };
  3603. window.SVGPathSegCurvetoCubicSmoothAbs.prototype = Object.create(
  3604. window.SVGPathSeg.prototype
  3605. );
  3606. window.SVGPathSegCurvetoCubicSmoothAbs.prototype.toString = function() {
  3607. return '[object SVGPathSegCurvetoCubicSmoothAbs]'
  3608. };
  3609. window.SVGPathSegCurvetoCubicSmoothAbs.prototype._asPathString = function() {
  3610. return (
  3611. this.pathSegTypeAsLetter +
  3612. ' ' +
  3613. this._x2 +
  3614. ' ' +
  3615. this._y2 +
  3616. ' ' +
  3617. this._x +
  3618. ' ' +
  3619. this._y
  3620. )
  3621. };
  3622. window.SVGPathSegCurvetoCubicSmoothAbs.prototype.clone = function() {
  3623. return new window.SVGPathSegCurvetoCubicSmoothAbs(
  3624. undefined,
  3625. this._x,
  3626. this._y,
  3627. this._x2,
  3628. this._y2
  3629. )
  3630. };
  3631. Object.defineProperty(
  3632. window.SVGPathSegCurvetoCubicSmoothAbs.prototype,
  3633. 'x',
  3634. {
  3635. get: function() {
  3636. return this._x
  3637. },
  3638. set: function(x) {
  3639. this._x = x;
  3640. this._segmentChanged();
  3641. },
  3642. enumerable: true
  3643. }
  3644. );
  3645. Object.defineProperty(
  3646. window.SVGPathSegCurvetoCubicSmoothAbs.prototype,
  3647. 'y',
  3648. {
  3649. get: function() {
  3650. return this._y
  3651. },
  3652. set: function(y) {
  3653. this._y = y;
  3654. this._segmentChanged();
  3655. },
  3656. enumerable: true
  3657. }
  3658. );
  3659. Object.defineProperty(
  3660. window.SVGPathSegCurvetoCubicSmoothAbs.prototype,
  3661. 'x2',
  3662. {
  3663. get: function() {
  3664. return this._x2
  3665. },
  3666. set: function(x2) {
  3667. this._x2 = x2;
  3668. this._segmentChanged();
  3669. },
  3670. enumerable: true
  3671. }
  3672. );
  3673. Object.defineProperty(
  3674. window.SVGPathSegCurvetoCubicSmoothAbs.prototype,
  3675. 'y2',
  3676. {
  3677. get: function() {
  3678. return this._y2
  3679. },
  3680. set: function(y2) {
  3681. this._y2 = y2;
  3682. this._segmentChanged();
  3683. },
  3684. enumerable: true
  3685. }
  3686. );
  3687. window.SVGPathSegCurvetoCubicSmoothRel = function(
  3688. owningPathSegList,
  3689. x,
  3690. y,
  3691. x2,
  3692. y2
  3693. ) {
  3694. window.SVGPathSeg.call(
  3695. this,
  3696. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL,
  3697. 's',
  3698. owningPathSegList
  3699. );
  3700. this._x = x;
  3701. this._y = y;
  3702. this._x2 = x2;
  3703. this._y2 = y2;
  3704. };
  3705. window.SVGPathSegCurvetoCubicSmoothRel.prototype = Object.create(
  3706. window.SVGPathSeg.prototype
  3707. );
  3708. window.SVGPathSegCurvetoCubicSmoothRel.prototype.toString = function() {
  3709. return '[object SVGPathSegCurvetoCubicSmoothRel]'
  3710. };
  3711. window.SVGPathSegCurvetoCubicSmoothRel.prototype._asPathString = function() {
  3712. return (
  3713. this.pathSegTypeAsLetter +
  3714. ' ' +
  3715. this._x2 +
  3716. ' ' +
  3717. this._y2 +
  3718. ' ' +
  3719. this._x +
  3720. ' ' +
  3721. this._y
  3722. )
  3723. };
  3724. window.SVGPathSegCurvetoCubicSmoothRel.prototype.clone = function() {
  3725. return new window.SVGPathSegCurvetoCubicSmoothRel(
  3726. undefined,
  3727. this._x,
  3728. this._y,
  3729. this._x2,
  3730. this._y2
  3731. )
  3732. };
  3733. Object.defineProperty(
  3734. window.SVGPathSegCurvetoCubicSmoothRel.prototype,
  3735. 'x',
  3736. {
  3737. get: function() {
  3738. return this._x
  3739. },
  3740. set: function(x) {
  3741. this._x = x;
  3742. this._segmentChanged();
  3743. },
  3744. enumerable: true
  3745. }
  3746. );
  3747. Object.defineProperty(
  3748. window.SVGPathSegCurvetoCubicSmoothRel.prototype,
  3749. 'y',
  3750. {
  3751. get: function() {
  3752. return this._y
  3753. },
  3754. set: function(y) {
  3755. this._y = y;
  3756. this._segmentChanged();
  3757. },
  3758. enumerable: true
  3759. }
  3760. );
  3761. Object.defineProperty(
  3762. window.SVGPathSegCurvetoCubicSmoothRel.prototype,
  3763. 'x2',
  3764. {
  3765. get: function() {
  3766. return this._x2
  3767. },
  3768. set: function(x2) {
  3769. this._x2 = x2;
  3770. this._segmentChanged();
  3771. },
  3772. enumerable: true
  3773. }
  3774. );
  3775. Object.defineProperty(
  3776. window.SVGPathSegCurvetoCubicSmoothRel.prototype,
  3777. 'y2',
  3778. {
  3779. get: function() {
  3780. return this._y2
  3781. },
  3782. set: function(y2) {
  3783. this._y2 = y2;
  3784. this._segmentChanged();
  3785. },
  3786. enumerable: true
  3787. }
  3788. );
  3789. window.SVGPathSegCurvetoQuadraticSmoothAbs = function(
  3790. owningPathSegList,
  3791. x,
  3792. y
  3793. ) {
  3794. window.SVGPathSeg.call(
  3795. this,
  3796. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS,
  3797. 'T',
  3798. owningPathSegList
  3799. );
  3800. this._x = x;
  3801. this._y = y;
  3802. };
  3803. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype = Object.create(
  3804. window.SVGPathSeg.prototype
  3805. );
  3806. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype.toString = function() {
  3807. return '[object SVGPathSegCurvetoQuadraticSmoothAbs]'
  3808. };
  3809. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype._asPathString = function() {
  3810. return this.pathSegTypeAsLetter + ' ' + this._x + ' ' + this._y
  3811. };
  3812. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype.clone = function() {
  3813. return new window.SVGPathSegCurvetoQuadraticSmoothAbs(
  3814. undefined,
  3815. this._x,
  3816. this._y
  3817. )
  3818. };
  3819. Object.defineProperty(
  3820. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype,
  3821. 'x',
  3822. {
  3823. get: function() {
  3824. return this._x
  3825. },
  3826. set: function(x) {
  3827. this._x = x;
  3828. this._segmentChanged();
  3829. },
  3830. enumerable: true
  3831. }
  3832. );
  3833. Object.defineProperty(
  3834. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype,
  3835. 'y',
  3836. {
  3837. get: function() {
  3838. return this._y
  3839. },
  3840. set: function(y) {
  3841. this._y = y;
  3842. this._segmentChanged();
  3843. },
  3844. enumerable: true
  3845. }
  3846. );
  3847. window.SVGPathSegCurvetoQuadraticSmoothRel = function(
  3848. owningPathSegList,
  3849. x,
  3850. y
  3851. ) {
  3852. window.SVGPathSeg.call(
  3853. this,
  3854. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL,
  3855. 't',
  3856. owningPathSegList
  3857. );
  3858. this._x = x;
  3859. this._y = y;
  3860. };
  3861. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype = Object.create(
  3862. window.SVGPathSeg.prototype
  3863. );
  3864. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype.toString = function() {
  3865. return '[object SVGPathSegCurvetoQuadraticSmoothRel]'
  3866. };
  3867. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype._asPathString = function() {
  3868. return this.pathSegTypeAsLetter + ' ' + this._x + ' ' + this._y
  3869. };
  3870. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype.clone = function() {
  3871. return new window.SVGPathSegCurvetoQuadraticSmoothRel(
  3872. undefined,
  3873. this._x,
  3874. this._y
  3875. )
  3876. };
  3877. Object.defineProperty(
  3878. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype,
  3879. 'x',
  3880. {
  3881. get: function() {
  3882. return this._x
  3883. },
  3884. set: function(x) {
  3885. this._x = x;
  3886. this._segmentChanged();
  3887. },
  3888. enumerable: true
  3889. }
  3890. );
  3891. Object.defineProperty(
  3892. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype,
  3893. 'y',
  3894. {
  3895. get: function() {
  3896. return this._y
  3897. },
  3898. set: function(y) {
  3899. this._y = y;
  3900. this._segmentChanged();
  3901. },
  3902. enumerable: true
  3903. }
  3904. );
  3905. // Add createSVGPathSeg* functions to window.SVGPathElement.
  3906. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-Interfacewindow.SVGPathElement.
  3907. window.SVGPathElement.prototype.createSVGPathSegClosePath = function() {
  3908. return new window.SVGPathSegClosePath(undefined)
  3909. };
  3910. window.SVGPathElement.prototype.createSVGPathSegMovetoAbs = function(x, y) {
  3911. return new window.SVGPathSegMovetoAbs(undefined, x, y)
  3912. };
  3913. window.SVGPathElement.prototype.createSVGPathSegMovetoRel = function(x, y) {
  3914. return new window.SVGPathSegMovetoRel(undefined, x, y)
  3915. };
  3916. window.SVGPathElement.prototype.createSVGPathSegLinetoAbs = function(x, y) {
  3917. return new window.SVGPathSegLinetoAbs(undefined, x, y)
  3918. };
  3919. window.SVGPathElement.prototype.createSVGPathSegLinetoRel = function(x, y) {
  3920. return new window.SVGPathSegLinetoRel(undefined, x, y)
  3921. };
  3922. window.SVGPathElement.prototype.createSVGPathSegCurvetoCubicAbs = function(
  3923. x,
  3924. y,
  3925. x1,
  3926. y1,
  3927. x2,
  3928. y2
  3929. ) {
  3930. return new window.SVGPathSegCurvetoCubicAbs(
  3931. undefined,
  3932. x,
  3933. y,
  3934. x1,
  3935. y1,
  3936. x2,
  3937. y2
  3938. )
  3939. };
  3940. window.SVGPathElement.prototype.createSVGPathSegCurvetoCubicRel = function(
  3941. x,
  3942. y,
  3943. x1,
  3944. y1,
  3945. x2,
  3946. y2
  3947. ) {
  3948. return new window.SVGPathSegCurvetoCubicRel(
  3949. undefined,
  3950. x,
  3951. y,
  3952. x1,
  3953. y1,
  3954. x2,
  3955. y2
  3956. )
  3957. };
  3958. window.SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticAbs = function(
  3959. x,
  3960. y,
  3961. x1,
  3962. y1
  3963. ) {
  3964. return new window.SVGPathSegCurvetoQuadraticAbs(undefined, x, y, x1, y1)
  3965. };
  3966. window.SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticRel = function(
  3967. x,
  3968. y,
  3969. x1,
  3970. y1
  3971. ) {
  3972. return new window.SVGPathSegCurvetoQuadraticRel(undefined, x, y, x1, y1)
  3973. };
  3974. window.SVGPathElement.prototype.createSVGPathSegArcAbs = function(
  3975. x,
  3976. y,
  3977. r1,
  3978. r2,
  3979. angle,
  3980. largeArcFlag,
  3981. sweepFlag
  3982. ) {
  3983. return new window.SVGPathSegArcAbs(
  3984. undefined,
  3985. x,
  3986. y,
  3987. r1,
  3988. r2,
  3989. angle,
  3990. largeArcFlag,
  3991. sweepFlag
  3992. )
  3993. };
  3994. window.SVGPathElement.prototype.createSVGPathSegArcRel = function(
  3995. x,
  3996. y,
  3997. r1,
  3998. r2,
  3999. angle,
  4000. largeArcFlag,
  4001. sweepFlag
  4002. ) {
  4003. return new window.SVGPathSegArcRel(
  4004. undefined,
  4005. x,
  4006. y,
  4007. r1,
  4008. r2,
  4009. angle,
  4010. largeArcFlag,
  4011. sweepFlag
  4012. )
  4013. };
  4014. window.SVGPathElement.prototype.createSVGPathSegLinetoHorizontalAbs = function(
  4015. x
  4016. ) {
  4017. return new window.SVGPathSegLinetoHorizontalAbs(undefined, x)
  4018. };
  4019. window.SVGPathElement.prototype.createSVGPathSegLinetoHorizontalRel = function(
  4020. x
  4021. ) {
  4022. return new window.SVGPathSegLinetoHorizontalRel(undefined, x)
  4023. };
  4024. window.SVGPathElement.prototype.createSVGPathSegLinetoVerticalAbs = function(
  4025. y
  4026. ) {
  4027. return new window.SVGPathSegLinetoVerticalAbs(undefined, y)
  4028. };
  4029. window.SVGPathElement.prototype.createSVGPathSegLinetoVerticalRel = function(
  4030. y
  4031. ) {
  4032. return new window.SVGPathSegLinetoVerticalRel(undefined, y)
  4033. };
  4034. window.SVGPathElement.prototype.createSVGPathSegCurvetoCubicSmoothAbs = function(
  4035. x,
  4036. y,
  4037. x2,
  4038. y2
  4039. ) {
  4040. return new window.SVGPathSegCurvetoCubicSmoothAbs(undefined, x, y, x2, y2)
  4041. };
  4042. window.SVGPathElement.prototype.createSVGPathSegCurvetoCubicSmoothRel = function(
  4043. x,
  4044. y,
  4045. x2,
  4046. y2
  4047. ) {
  4048. return new window.SVGPathSegCurvetoCubicSmoothRel(undefined, x, y, x2, y2)
  4049. };
  4050. window.SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticSmoothAbs = function(
  4051. x,
  4052. y
  4053. ) {
  4054. return new window.SVGPathSegCurvetoQuadraticSmoothAbs(undefined, x, y)
  4055. };
  4056. window.SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticSmoothRel = function(
  4057. x,
  4058. y
  4059. ) {
  4060. return new window.SVGPathSegCurvetoQuadraticSmoothRel(undefined, x, y)
  4061. };
  4062. if (!('getPathSegAtLength' in window.SVGPathElement.prototype)) {
  4063. // Add getPathSegAtLength to SVGPathElement.
  4064. // Spec: https://www.w3.org/TR/SVG11/single-page.html#paths-__svg__SVGPathElement__getPathSegAtLength
  4065. // This polyfill requires SVGPathElement.getTotalLength to implement the distance-along-a-path algorithm.
  4066. window.SVGPathElement.prototype.getPathSegAtLength = function(distance) {
  4067. if (distance === undefined || !isFinite(distance))
  4068. throw 'Invalid arguments.'
  4069. var measurementElement = document.createElementNS(
  4070. 'http://www.w3.org/2000/svg',
  4071. 'path'
  4072. );
  4073. measurementElement.setAttribute('d', this.getAttribute('d'));
  4074. var lastPathSegment = measurementElement.pathSegList.numberOfItems - 1;
  4075. // If the path is empty, return 0.
  4076. if (lastPathSegment <= 0) return 0
  4077. do {
  4078. measurementElement.pathSegList.removeItem(lastPathSegment);
  4079. if (distance > measurementElement.getTotalLength()) break
  4080. lastPathSegment--;
  4081. } while (lastPathSegment > 0)
  4082. return lastPathSegment
  4083. };
  4084. }
  4085. }
  4086. if (!('SVGPathSegList' in window)) {
  4087. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGPathSegList
  4088. window.SVGPathSegList = function(pathElement) {
  4089. this._pathElement = pathElement;
  4090. this._list = this._parsePath(this._pathElement.getAttribute('d'));
  4091. // Use a MutationObserver to catch changes to the path's "d" attribute.
  4092. this._mutationObserverConfig = {
  4093. attributes: true,
  4094. attributeFilter: ['d']
  4095. };
  4096. this._pathElementMutationObserver = new MutationObserver(
  4097. this._updateListFromPathMutations.bind(this)
  4098. );
  4099. this._pathElementMutationObserver.observe(
  4100. this._pathElement,
  4101. this._mutationObserverConfig
  4102. );
  4103. };
  4104. window.SVGPathSegList.prototype.classname = 'SVGPathSegList';
  4105. Object.defineProperty(window.SVGPathSegList.prototype, 'numberOfItems', {
  4106. get: function() {
  4107. this._checkPathSynchronizedToList();
  4108. return this._list.length
  4109. },
  4110. enumerable: true
  4111. });
  4112. // Add the pathSegList accessors to window.SVGPathElement.
  4113. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGAnimatedPathData
  4114. Object.defineProperty(window.SVGPathElement.prototype, 'pathSegList', {
  4115. get: function() {
  4116. if (!this._pathSegList)
  4117. this._pathSegList = new window.SVGPathSegList(this);
  4118. return this._pathSegList
  4119. },
  4120. enumerable: true
  4121. });
  4122. // FIXME: The following are not implemented and simply return window.SVGPathElement.pathSegList.
  4123. Object.defineProperty(
  4124. window.SVGPathElement.prototype,
  4125. 'normalizedPathSegList',
  4126. {
  4127. get: function() {
  4128. return this.pathSegList
  4129. },
  4130. enumerable: true
  4131. }
  4132. );
  4133. Object.defineProperty(
  4134. window.SVGPathElement.prototype,
  4135. 'animatedPathSegList',
  4136. {
  4137. get: function() {
  4138. return this.pathSegList
  4139. },
  4140. enumerable: true
  4141. }
  4142. );
  4143. Object.defineProperty(
  4144. window.SVGPathElement.prototype,
  4145. 'animatedNormalizedPathSegList',
  4146. {
  4147. get: function() {
  4148. return this.pathSegList
  4149. },
  4150. enumerable: true
  4151. }
  4152. );
  4153. // Process any pending mutations to the path element and update the list as needed.
  4154. // This should be the first call of all public functions and is needed because
  4155. // MutationObservers are not synchronous so we can have pending asynchronous mutations.
  4156. window.SVGPathSegList.prototype._checkPathSynchronizedToList = function() {
  4157. this._updateListFromPathMutations(
  4158. this._pathElementMutationObserver.takeRecords()
  4159. );
  4160. };
  4161. window.SVGPathSegList.prototype._updateListFromPathMutations = function(
  4162. mutationRecords
  4163. ) {
  4164. if (!this._pathElement) return
  4165. var hasPathMutations = false;
  4166. mutationRecords.forEach(function(record) {
  4167. if (record.attributeName == 'd') hasPathMutations = true;
  4168. });
  4169. if (hasPathMutations)
  4170. this._list = this._parsePath(this._pathElement.getAttribute('d'));
  4171. };
  4172. // Serialize the list and update the path's 'd' attribute.
  4173. window.SVGPathSegList.prototype._writeListToPath = function() {
  4174. this._pathElementMutationObserver.disconnect();
  4175. this._pathElement.setAttribute(
  4176. 'd',
  4177. window.SVGPathSegList._pathSegArrayAsString(this._list)
  4178. );
  4179. this._pathElementMutationObserver.observe(
  4180. this._pathElement,
  4181. this._mutationObserverConfig
  4182. );
  4183. };
  4184. // When a path segment changes the list needs to be synchronized back to the path element.
  4185. window.SVGPathSegList.prototype.segmentChanged = function(pathSeg) {
  4186. this._writeListToPath();
  4187. };
  4188. window.SVGPathSegList.prototype.clear = function() {
  4189. this._checkPathSynchronizedToList();
  4190. this._list.forEach(function(pathSeg) {
  4191. pathSeg._owningPathSegList = null;
  4192. });
  4193. this._list = [];
  4194. this._writeListToPath();
  4195. };
  4196. window.SVGPathSegList.prototype.initialize = function(newItem) {
  4197. this._checkPathSynchronizedToList();
  4198. this._list = [newItem];
  4199. newItem._owningPathSegList = this;
  4200. this._writeListToPath();
  4201. return newItem
  4202. };
  4203. window.SVGPathSegList.prototype._checkValidIndex = function(index) {
  4204. if (isNaN(index) || index < 0 || index >= this.numberOfItems)
  4205. throw 'INDEX_SIZE_ERR'
  4206. };
  4207. window.SVGPathSegList.prototype.getItem = function(index) {
  4208. this._checkPathSynchronizedToList();
  4209. this._checkValidIndex(index);
  4210. return this._list[index]
  4211. };
  4212. window.SVGPathSegList.prototype.insertItemBefore = function(
  4213. newItem,
  4214. index
  4215. ) {
  4216. this._checkPathSynchronizedToList();
  4217. // Spec: If the index is greater than or equal to numberOfItems, then the new item is appended to the end of the list.
  4218. if (index > this.numberOfItems) index = this.numberOfItems;
  4219. if (newItem._owningPathSegList) {
  4220. // SVG2 spec says to make a copy.
  4221. newItem = newItem.clone();
  4222. }
  4223. this._list.splice(index, 0, newItem);
  4224. newItem._owningPathSegList = this;
  4225. this._writeListToPath();
  4226. return newItem
  4227. };
  4228. window.SVGPathSegList.prototype.replaceItem = function(newItem, index) {
  4229. this._checkPathSynchronizedToList();
  4230. if (newItem._owningPathSegList) {
  4231. // SVG2 spec says to make a copy.
  4232. newItem = newItem.clone();
  4233. }
  4234. this._checkValidIndex(index);
  4235. this._list[index] = newItem;
  4236. newItem._owningPathSegList = this;
  4237. this._writeListToPath();
  4238. return newItem
  4239. };
  4240. window.SVGPathSegList.prototype.removeItem = function(index) {
  4241. this._checkPathSynchronizedToList();
  4242. this._checkValidIndex(index);
  4243. var item = this._list[index];
  4244. this._list.splice(index, 1);
  4245. this._writeListToPath();
  4246. return item
  4247. };
  4248. window.SVGPathSegList.prototype.appendItem = function(newItem) {
  4249. this._checkPathSynchronizedToList();
  4250. if (newItem._owningPathSegList) {
  4251. // SVG2 spec says to make a copy.
  4252. newItem = newItem.clone();
  4253. }
  4254. this._list.push(newItem);
  4255. newItem._owningPathSegList = this;
  4256. // TODO: Optimize this to just append to the existing attribute.
  4257. this._writeListToPath();
  4258. return newItem
  4259. };
  4260. window.SVGPathSegList._pathSegArrayAsString = function(pathSegArray) {
  4261. var string = '';
  4262. var first = true;
  4263. pathSegArray.forEach(function(pathSeg) {
  4264. if (first) {
  4265. first = false;
  4266. string += pathSeg._asPathString();
  4267. } else {
  4268. string += ' ' + pathSeg._asPathString();
  4269. }
  4270. });
  4271. return string
  4272. };
  4273. // This closely follows SVGPathParser::parsePath from Source/core/svg/SVGPathParser.cpp.
  4274. window.SVGPathSegList.prototype._parsePath = function(string) {
  4275. if (!string || string.length == 0) return []
  4276. var owningPathSegList = this;
  4277. var Builder = function() {
  4278. this.pathSegList = [];
  4279. };
  4280. Builder.prototype.appendSegment = function(pathSeg) {
  4281. this.pathSegList.push(pathSeg);
  4282. };
  4283. var Source = function(string) {
  4284. this._string = string;
  4285. this._currentIndex = 0;
  4286. this._endIndex = this._string.length;
  4287. this._previousCommand = window.SVGPathSeg.PATHSEG_UNKNOWN;
  4288. this._skipOptionalSpaces();
  4289. };
  4290. Source.prototype._isCurrentSpace = function() {
  4291. var character = this._string[this._currentIndex];
  4292. return (
  4293. character <= ' ' &&
  4294. (character == ' ' ||
  4295. character == '\n' ||
  4296. character == '\t' ||
  4297. character == '\r' ||
  4298. character == '\f')
  4299. )
  4300. };
  4301. Source.prototype._skipOptionalSpaces = function() {
  4302. while (this._currentIndex < this._endIndex && this._isCurrentSpace())
  4303. this._currentIndex++;
  4304. return this._currentIndex < this._endIndex
  4305. };
  4306. Source.prototype._skipOptionalSpacesOrDelimiter = function() {
  4307. if (
  4308. this._currentIndex < this._endIndex &&
  4309. !this._isCurrentSpace() &&
  4310. this._string.charAt(this._currentIndex) != ','
  4311. )
  4312. return false
  4313. if (this._skipOptionalSpaces()) {
  4314. if (
  4315. this._currentIndex < this._endIndex &&
  4316. this._string.charAt(this._currentIndex) == ','
  4317. ) {
  4318. this._currentIndex++;
  4319. this._skipOptionalSpaces();
  4320. }
  4321. }
  4322. return this._currentIndex < this._endIndex
  4323. };
  4324. Source.prototype.hasMoreData = function() {
  4325. return this._currentIndex < this._endIndex
  4326. };
  4327. Source.prototype.peekSegmentType = function() {
  4328. var lookahead = this._string[this._currentIndex];
  4329. return this._pathSegTypeFromChar(lookahead)
  4330. };
  4331. Source.prototype._pathSegTypeFromChar = function(lookahead) {
  4332. switch (lookahead) {
  4333. case 'Z':
  4334. case 'z':
  4335. return window.SVGPathSeg.PATHSEG_CLOSEPATH
  4336. case 'M':
  4337. return window.SVGPathSeg.PATHSEG_MOVETO_ABS
  4338. case 'm':
  4339. return window.SVGPathSeg.PATHSEG_MOVETO_REL
  4340. case 'L':
  4341. return window.SVGPathSeg.PATHSEG_LINETO_ABS
  4342. case 'l':
  4343. return window.SVGPathSeg.PATHSEG_LINETO_REL
  4344. case 'C':
  4345. return window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS
  4346. case 'c':
  4347. return window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL
  4348. case 'Q':
  4349. return window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS
  4350. case 'q':
  4351. return window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL
  4352. case 'A':
  4353. return window.SVGPathSeg.PATHSEG_ARC_ABS
  4354. case 'a':
  4355. return window.SVGPathSeg.PATHSEG_ARC_REL
  4356. case 'H':
  4357. return window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS
  4358. case 'h':
  4359. return window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL
  4360. case 'V':
  4361. return window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS
  4362. case 'v':
  4363. return window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL
  4364. case 'S':
  4365. return window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS
  4366. case 's':
  4367. return window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL
  4368. case 'T':
  4369. return window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS
  4370. case 't':
  4371. return window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL
  4372. default:
  4373. return window.SVGPathSeg.PATHSEG_UNKNOWN
  4374. }
  4375. };
  4376. Source.prototype._nextCommandHelper = function(
  4377. lookahead,
  4378. previousCommand
  4379. ) {
  4380. // Check for remaining coordinates in the current command.
  4381. if (
  4382. (lookahead == '+' ||
  4383. lookahead == '-' ||
  4384. lookahead == '.' ||
  4385. (lookahead >= '0' && lookahead <= '9')) &&
  4386. previousCommand != window.SVGPathSeg.PATHSEG_CLOSEPATH
  4387. ) {
  4388. if (previousCommand == window.SVGPathSeg.PATHSEG_MOVETO_ABS)
  4389. return window.SVGPathSeg.PATHSEG_LINETO_ABS
  4390. if (previousCommand == window.SVGPathSeg.PATHSEG_MOVETO_REL)
  4391. return window.SVGPathSeg.PATHSEG_LINETO_REL
  4392. return previousCommand
  4393. }
  4394. return window.SVGPathSeg.PATHSEG_UNKNOWN
  4395. };
  4396. Source.prototype.initialCommandIsMoveTo = function() {
  4397. // If the path is empty it is still valid, so return true.
  4398. if (!this.hasMoreData()) return true
  4399. var command = this.peekSegmentType();
  4400. // Path must start with moveTo.
  4401. return (
  4402. command == window.SVGPathSeg.PATHSEG_MOVETO_ABS ||
  4403. command == window.SVGPathSeg.PATHSEG_MOVETO_REL
  4404. )
  4405. };
  4406. // Parse a number from an SVG path. This very closely follows genericParseNumber(...) from Source/core/svg/SVGParserUtilities.cpp.
  4407. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-PathDataBNF
  4408. Source.prototype._parseNumber = function() {
  4409. var exponent = 0;
  4410. var integer = 0;
  4411. var frac = 1;
  4412. var decimal = 0;
  4413. var sign = 1;
  4414. var expsign = 1;
  4415. var startIndex = this._currentIndex;
  4416. this._skipOptionalSpaces();
  4417. // Read the sign.
  4418. if (
  4419. this._currentIndex < this._endIndex &&
  4420. this._string.charAt(this._currentIndex) == '+'
  4421. )
  4422. this._currentIndex++;
  4423. else if (
  4424. this._currentIndex < this._endIndex &&
  4425. this._string.charAt(this._currentIndex) == '-'
  4426. ) {
  4427. this._currentIndex++;
  4428. sign = -1;
  4429. }
  4430. if (
  4431. this._currentIndex == this._endIndex ||
  4432. ((this._string.charAt(this._currentIndex) < '0' ||
  4433. this._string.charAt(this._currentIndex) > '9') &&
  4434. this._string.charAt(this._currentIndex) != '.')
  4435. )
  4436. // The first character of a number must be one of [0-9+-.].
  4437. return undefined
  4438. // Read the integer part, build right-to-left.
  4439. var startIntPartIndex = this._currentIndex;
  4440. while (
  4441. this._currentIndex < this._endIndex &&
  4442. this._string.charAt(this._currentIndex) >= '0' &&
  4443. this._string.charAt(this._currentIndex) <= '9'
  4444. )
  4445. this._currentIndex++; // Advance to first non-digit.
  4446. if (this._currentIndex != startIntPartIndex) {
  4447. var scanIntPartIndex = this._currentIndex - 1;
  4448. var multiplier = 1;
  4449. while (scanIntPartIndex >= startIntPartIndex) {
  4450. integer +=
  4451. multiplier * (this._string.charAt(scanIntPartIndex--) - '0');
  4452. multiplier *= 10;
  4453. }
  4454. }
  4455. // Read the decimals.
  4456. if (
  4457. this._currentIndex < this._endIndex &&
  4458. this._string.charAt(this._currentIndex) == '.'
  4459. ) {
  4460. this._currentIndex++;
  4461. // There must be a least one digit following the .
  4462. if (
  4463. this._currentIndex >= this._endIndex ||
  4464. this._string.charAt(this._currentIndex) < '0' ||
  4465. this._string.charAt(this._currentIndex) > '9'
  4466. )
  4467. return undefined
  4468. while (
  4469. this._currentIndex < this._endIndex &&
  4470. this._string.charAt(this._currentIndex) >= '0' &&
  4471. this._string.charAt(this._currentIndex) <= '9'
  4472. ) {
  4473. frac *= 10;
  4474. decimal += (this._string.charAt(this._currentIndex) - '0') / frac;
  4475. this._currentIndex += 1;
  4476. }
  4477. }
  4478. // Read the exponent part.
  4479. if (
  4480. this._currentIndex != startIndex &&
  4481. this._currentIndex + 1 < this._endIndex &&
  4482. (this._string.charAt(this._currentIndex) == 'e' ||
  4483. this._string.charAt(this._currentIndex) == 'E') &&
  4484. this._string.charAt(this._currentIndex + 1) != 'x' &&
  4485. this._string.charAt(this._currentIndex + 1) != 'm'
  4486. ) {
  4487. this._currentIndex++;
  4488. // Read the sign of the exponent.
  4489. if (this._string.charAt(this._currentIndex) == '+') {
  4490. this._currentIndex++;
  4491. } else if (this._string.charAt(this._currentIndex) == '-') {
  4492. this._currentIndex++;
  4493. expsign = -1;
  4494. }
  4495. // There must be an exponent.
  4496. if (
  4497. this._currentIndex >= this._endIndex ||
  4498. this._string.charAt(this._currentIndex) < '0' ||
  4499. this._string.charAt(this._currentIndex) > '9'
  4500. )
  4501. return undefined
  4502. while (
  4503. this._currentIndex < this._endIndex &&
  4504. this._string.charAt(this._currentIndex) >= '0' &&
  4505. this._string.charAt(this._currentIndex) <= '9'
  4506. ) {
  4507. exponent *= 10;
  4508. exponent += this._string.charAt(this._currentIndex) - '0';
  4509. this._currentIndex++;
  4510. }
  4511. }
  4512. var number = integer + decimal;
  4513. number *= sign;
  4514. if (exponent) number *= Math.pow(10, expsign * exponent);
  4515. if (startIndex == this._currentIndex) return undefined
  4516. this._skipOptionalSpacesOrDelimiter();
  4517. return number
  4518. };
  4519. Source.prototype._parseArcFlag = function() {
  4520. if (this._currentIndex >= this._endIndex) return undefined
  4521. var flag = false;
  4522. var flagChar = this._string.charAt(this._currentIndex++);
  4523. if (flagChar == '0') flag = false;
  4524. else if (flagChar == '1') flag = true;
  4525. else return undefined
  4526. this._skipOptionalSpacesOrDelimiter();
  4527. return flag
  4528. };
  4529. Source.prototype.parseSegment = function() {
  4530. var lookahead = this._string[this._currentIndex];
  4531. var command = this._pathSegTypeFromChar(lookahead);
  4532. if (command == window.SVGPathSeg.PATHSEG_UNKNOWN) {
  4533. // Possibly an implicit command. Not allowed if this is the first command.
  4534. if (this._previousCommand == window.SVGPathSeg.PATHSEG_UNKNOWN)
  4535. return null
  4536. command = this._nextCommandHelper(lookahead, this._previousCommand);
  4537. if (command == window.SVGPathSeg.PATHSEG_UNKNOWN) return null
  4538. } else {
  4539. this._currentIndex++;
  4540. }
  4541. this._previousCommand = command;
  4542. switch (command) {
  4543. case window.SVGPathSeg.PATHSEG_MOVETO_REL:
  4544. return new window.SVGPathSegMovetoRel(
  4545. owningPathSegList,
  4546. this._parseNumber(),
  4547. this._parseNumber()
  4548. )
  4549. case window.SVGPathSeg.PATHSEG_MOVETO_ABS:
  4550. return new window.SVGPathSegMovetoAbs(
  4551. owningPathSegList,
  4552. this._parseNumber(),
  4553. this._parseNumber()
  4554. )
  4555. case window.SVGPathSeg.PATHSEG_LINETO_REL:
  4556. return new window.SVGPathSegLinetoRel(
  4557. owningPathSegList,
  4558. this._parseNumber(),
  4559. this._parseNumber()
  4560. )
  4561. case window.SVGPathSeg.PATHSEG_LINETO_ABS:
  4562. return new window.SVGPathSegLinetoAbs(
  4563. owningPathSegList,
  4564. this._parseNumber(),
  4565. this._parseNumber()
  4566. )
  4567. case window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL:
  4568. return new window.SVGPathSegLinetoHorizontalRel(
  4569. owningPathSegList,
  4570. this._parseNumber()
  4571. )
  4572. case window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS:
  4573. return new window.SVGPathSegLinetoHorizontalAbs(
  4574. owningPathSegList,
  4575. this._parseNumber()
  4576. )
  4577. case window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL:
  4578. return new window.SVGPathSegLinetoVerticalRel(
  4579. owningPathSegList,
  4580. this._parseNumber()
  4581. )
  4582. case window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS:
  4583. return new window.SVGPathSegLinetoVerticalAbs(
  4584. owningPathSegList,
  4585. this._parseNumber()
  4586. )
  4587. case window.SVGPathSeg.PATHSEG_CLOSEPATH:
  4588. this._skipOptionalSpaces();
  4589. return new window.SVGPathSegClosePath(owningPathSegList)
  4590. case window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL:
  4591. var points = {
  4592. x1: this._parseNumber(),
  4593. y1: this._parseNumber(),
  4594. x2: this._parseNumber(),
  4595. y2: this._parseNumber(),
  4596. x: this._parseNumber(),
  4597. y: this._parseNumber()
  4598. };
  4599. return new window.SVGPathSegCurvetoCubicRel(
  4600. owningPathSegList,
  4601. points.x,
  4602. points.y,
  4603. points.x1,
  4604. points.y1,
  4605. points.x2,
  4606. points.y2
  4607. )
  4608. case window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS:
  4609. var points = {
  4610. x1: this._parseNumber(),
  4611. y1: this._parseNumber(),
  4612. x2: this._parseNumber(),
  4613. y2: this._parseNumber(),
  4614. x: this._parseNumber(),
  4615. y: this._parseNumber()
  4616. };
  4617. return new window.SVGPathSegCurvetoCubicAbs(
  4618. owningPathSegList,
  4619. points.x,
  4620. points.y,
  4621. points.x1,
  4622. points.y1,
  4623. points.x2,
  4624. points.y2
  4625. )
  4626. case window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL:
  4627. var points = {
  4628. x2: this._parseNumber(),
  4629. y2: this._parseNumber(),
  4630. x: this._parseNumber(),
  4631. y: this._parseNumber()
  4632. };
  4633. return new window.SVGPathSegCurvetoCubicSmoothRel(
  4634. owningPathSegList,
  4635. points.x,
  4636. points.y,
  4637. points.x2,
  4638. points.y2
  4639. )
  4640. case window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS:
  4641. var points = {
  4642. x2: this._parseNumber(),
  4643. y2: this._parseNumber(),
  4644. x: this._parseNumber(),
  4645. y: this._parseNumber()
  4646. };
  4647. return new window.SVGPathSegCurvetoCubicSmoothAbs(
  4648. owningPathSegList,
  4649. points.x,
  4650. points.y,
  4651. points.x2,
  4652. points.y2
  4653. )
  4654. case window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL:
  4655. var points = {
  4656. x1: this._parseNumber(),
  4657. y1: this._parseNumber(),
  4658. x: this._parseNumber(),
  4659. y: this._parseNumber()
  4660. };
  4661. return new window.SVGPathSegCurvetoQuadraticRel(
  4662. owningPathSegList,
  4663. points.x,
  4664. points.y,
  4665. points.x1,
  4666. points.y1
  4667. )
  4668. case window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS:
  4669. var points = {
  4670. x1: this._parseNumber(),
  4671. y1: this._parseNumber(),
  4672. x: this._parseNumber(),
  4673. y: this._parseNumber()
  4674. };
  4675. return new window.SVGPathSegCurvetoQuadraticAbs(
  4676. owningPathSegList,
  4677. points.x,
  4678. points.y,
  4679. points.x1,
  4680. points.y1
  4681. )
  4682. case window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL:
  4683. return new window.SVGPathSegCurvetoQuadraticSmoothRel(
  4684. owningPathSegList,
  4685. this._parseNumber(),
  4686. this._parseNumber()
  4687. )
  4688. case window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS:
  4689. return new window.SVGPathSegCurvetoQuadraticSmoothAbs(
  4690. owningPathSegList,
  4691. this._parseNumber(),
  4692. this._parseNumber()
  4693. )
  4694. case window.SVGPathSeg.PATHSEG_ARC_REL:
  4695. var points = {
  4696. x1: this._parseNumber(),
  4697. y1: this._parseNumber(),
  4698. arcAngle: this._parseNumber(),
  4699. arcLarge: this._parseArcFlag(),
  4700. arcSweep: this._parseArcFlag(),
  4701. x: this._parseNumber(),
  4702. y: this._parseNumber()
  4703. };
  4704. return new window.SVGPathSegArcRel(
  4705. owningPathSegList,
  4706. points.x,
  4707. points.y,
  4708. points.x1,
  4709. points.y1,
  4710. points.arcAngle,
  4711. points.arcLarge,
  4712. points.arcSweep
  4713. )
  4714. case window.SVGPathSeg.PATHSEG_ARC_ABS:
  4715. var points = {
  4716. x1: this._parseNumber(),
  4717. y1: this._parseNumber(),
  4718. arcAngle: this._parseNumber(),
  4719. arcLarge: this._parseArcFlag(),
  4720. arcSweep: this._parseArcFlag(),
  4721. x: this._parseNumber(),
  4722. y: this._parseNumber()
  4723. };
  4724. return new window.SVGPathSegArcAbs(
  4725. owningPathSegList,
  4726. points.x,
  4727. points.y,
  4728. points.x1,
  4729. points.y1,
  4730. points.arcAngle,
  4731. points.arcLarge,
  4732. points.arcSweep
  4733. )
  4734. default:
  4735. throw 'Unknown path seg type.'
  4736. }
  4737. };
  4738. var builder = new Builder();
  4739. var source = new Source(string);
  4740. if (!source.initialCommandIsMoveTo()) return []
  4741. while (source.hasMoreData()) {
  4742. var pathSeg = source.parseSegment();
  4743. if (!pathSeg) return []
  4744. builder.appendSegment(pathSeg);
  4745. }
  4746. return builder.pathSegList
  4747. };
  4748. }
  4749. })();
  4750. // String.padEnd polyfill for IE11
  4751. //
  4752. // https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
  4753. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
  4754. if (!String.prototype.padEnd) {
  4755. String.prototype.padEnd = function padEnd(targetLength, padString) {
  4756. targetLength = targetLength >> 0; //floor if number or convert non-number to 0;
  4757. padString = String(typeof padString !== 'undefined' ? padString : ' ');
  4758. if (this.length > targetLength) {
  4759. return String(this)
  4760. } else {
  4761. targetLength = targetLength - this.length;
  4762. if (targetLength > padString.length) {
  4763. padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
  4764. }
  4765. return String(this) + padString.slice(0, targetLength)
  4766. }
  4767. };
  4768. }
  4769. // Object.assign polyfill for IE11
  4770. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign#Polyfill
  4771. if (typeof Object.assign !== 'function') {
  4772. // Must be writable: true, enumerable: false, configurable: true
  4773. Object.defineProperty(Object, 'assign', {
  4774. value: function assign(target, varArgs) {
  4775. if (target === null || target === undefined) {
  4776. throw new TypeError('Cannot convert undefined or null to object')
  4777. }
  4778. var to = Object(target);
  4779. for (var index = 1; index < arguments.length; index++) {
  4780. var nextSource = arguments[index];
  4781. if (nextSource !== null && nextSource !== undefined) {
  4782. for (var nextKey in nextSource) {
  4783. // Avoid bugs when hasOwnProperty is shadowed
  4784. if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
  4785. to[nextKey] = nextSource[nextKey];
  4786. }
  4787. }
  4788. }
  4789. }
  4790. return to
  4791. },
  4792. writable: true,
  4793. configurable: true
  4794. });
  4795. }
  4796. /* jshint ignore:end */
  4797. Chart.prototype.axis = function() {};
  4798. Chart.prototype.axis.labels = function(labels) {
  4799. var $$ = this.internal;
  4800. if (arguments.length) {
  4801. Object.keys(labels).forEach(function(axisId) {
  4802. $$.axis.setLabelText(axisId, labels[axisId]);
  4803. });
  4804. $$.axis.updateLabels();
  4805. }
  4806. // TODO: return some values?
  4807. };
  4808. Chart.prototype.axis.max = function(max) {
  4809. var $$ = this.internal,
  4810. config = $$.config;
  4811. if (arguments.length) {
  4812. if (typeof max === 'object') {
  4813. if (isValue(max.x)) {
  4814. config.axis_x_max = max.x;
  4815. }
  4816. if (isValue(max.y)) {
  4817. config.axis_y_max = max.y;
  4818. }
  4819. if (isValue(max.y2)) {
  4820. config.axis_y2_max = max.y2;
  4821. }
  4822. } else {
  4823. config.axis_y_max = config.axis_y2_max = max;
  4824. }
  4825. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true });
  4826. } else {
  4827. return {
  4828. x: config.axis_x_max,
  4829. y: config.axis_y_max,
  4830. y2: config.axis_y2_max
  4831. }
  4832. }
  4833. };
  4834. Chart.prototype.axis.min = function(min) {
  4835. var $$ = this.internal,
  4836. config = $$.config;
  4837. if (arguments.length) {
  4838. if (typeof min === 'object') {
  4839. if (isValue(min.x)) {
  4840. config.axis_x_min = min.x;
  4841. }
  4842. if (isValue(min.y)) {
  4843. config.axis_y_min = min.y;
  4844. }
  4845. if (isValue(min.y2)) {
  4846. config.axis_y2_min = min.y2;
  4847. }
  4848. } else {
  4849. config.axis_y_min = config.axis_y2_min = min;
  4850. }
  4851. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true });
  4852. } else {
  4853. return {
  4854. x: config.axis_x_min,
  4855. y: config.axis_y_min,
  4856. y2: config.axis_y2_min
  4857. }
  4858. }
  4859. };
  4860. Chart.prototype.axis.range = function(range) {
  4861. if (arguments.length) {
  4862. if (isDefined(range.max)) {
  4863. this.axis.max(range.max);
  4864. }
  4865. if (isDefined(range.min)) {
  4866. this.axis.min(range.min);
  4867. }
  4868. } else {
  4869. return {
  4870. max: this.axis.max(),
  4871. min: this.axis.min()
  4872. }
  4873. }
  4874. };
  4875. Chart.prototype.axis.types = function(types) {
  4876. const $$ = this.internal;
  4877. if (types === undefined) {
  4878. return {
  4879. y: $$.config.axis_y_type,
  4880. y2: $$.config.axis_y2_type
  4881. }
  4882. } else {
  4883. if (isDefined(types.y)) {
  4884. $$.config.axis_y_type = types.y;
  4885. }
  4886. if (isDefined(types.y2)) {
  4887. $$.config.axis_y2_type = types.y2;
  4888. }
  4889. $$.updateScales();
  4890. $$.redraw();
  4891. }
  4892. };
  4893. Chart.prototype.category = function(i, category) {
  4894. var $$ = this.internal,
  4895. config = $$.config;
  4896. if (arguments.length > 1) {
  4897. config.axis_x_categories[i] = category;
  4898. $$.redraw();
  4899. }
  4900. return config.axis_x_categories[i]
  4901. };
  4902. Chart.prototype.categories = function(categories) {
  4903. var $$ = this.internal,
  4904. config = $$.config;
  4905. if (!arguments.length) {
  4906. return config.axis_x_categories
  4907. }
  4908. config.axis_x_categories = categories;
  4909. $$.redraw();
  4910. return config.axis_x_categories
  4911. };
  4912. Chart.prototype.resize = function(size) {
  4913. var $$ = this.internal,
  4914. config = $$.config;
  4915. config.size_width = size ? size.width : null;
  4916. config.size_height = size ? size.height : null;
  4917. this.flush();
  4918. };
  4919. Chart.prototype.flush = function() {
  4920. var $$ = this.internal;
  4921. $$.updateAndRedraw({
  4922. withLegend: true,
  4923. withTransition: false,
  4924. withTransitionForTransform: false
  4925. });
  4926. };
  4927. Chart.prototype.destroy = function() {
  4928. var $$ = this.internal;
  4929. window.clearInterval($$.intervalForObserveInserted);
  4930. if ($$.resizeTimeout !== undefined) {
  4931. window.clearTimeout($$.resizeTimeout);
  4932. }
  4933. if (window.detachEvent) {
  4934. window.detachEvent('onresize', $$.resizeIfElementDisplayed);
  4935. } else if (window.removeEventListener) {
  4936. window.removeEventListener('resize', $$.resizeIfElementDisplayed);
  4937. } else {
  4938. var wrapper = window.onresize;
  4939. // check if no one else removed our wrapper and remove our resizeFunction from it
  4940. if (wrapper && wrapper.add && wrapper.remove) {
  4941. wrapper.remove($$.resizeFunction);
  4942. }
  4943. }
  4944. // Removes the inner resize functions
  4945. $$.resizeFunction.remove();
  4946. // Unbinds from the window focus event
  4947. $$.unbindWindowFocus();
  4948. $$.selectChart.classed('c3', false).html('');
  4949. // MEMO: this is needed because the reference of some elements will not be released, then memory leak will happen.
  4950. Object.keys($$).forEach(function(key) {
  4951. $$[key] = null;
  4952. });
  4953. return null
  4954. };
  4955. // TODO: fix
  4956. Chart.prototype.color = function(id) {
  4957. var $$ = this.internal;
  4958. return $$.color(id) // more patterns
  4959. };
  4960. Chart.prototype.data = function(targetIds) {
  4961. var targets = this.internal.data.targets;
  4962. return typeof targetIds === 'undefined'
  4963. ? targets
  4964. : targets.filter(function(t) {
  4965. return [].concat(targetIds).indexOf(t.id) >= 0
  4966. })
  4967. };
  4968. Chart.prototype.data.shown = function(targetIds) {
  4969. return this.internal.filterTargetsToShow(this.data(targetIds))
  4970. };
  4971. /**
  4972. * Get values of the data loaded in the chart.
  4973. *
  4974. * @param {String|Array} targetId This API returns the value of specified target.
  4975. * @param flat
  4976. * @return {Array} Data values
  4977. */
  4978. Chart.prototype.data.values = function(targetId, flat = true) {
  4979. let values = null;
  4980. if (targetId) {
  4981. const targets = this.data(targetId);
  4982. if (targets && isArray(targets)) {
  4983. values = targets.reduce((ret, v) => {
  4984. const dataValue = v.values.map(d => d.value);
  4985. if (flat) {
  4986. ret = ret.concat(dataValue);
  4987. } else {
  4988. ret.push(dataValue);
  4989. }
  4990. return ret
  4991. }, []);
  4992. }
  4993. }
  4994. return values
  4995. };
  4996. Chart.prototype.data.names = function(names) {
  4997. this.internal.clearLegendItemTextBoxCache();
  4998. return this.internal.updateDataAttributes('names', names)
  4999. };
  5000. Chart.prototype.data.colors = function(colors) {
  5001. return this.internal.updateDataAttributes('colors', colors)
  5002. };
  5003. Chart.prototype.data.axes = function(axes) {
  5004. return this.internal.updateDataAttributes('axes', axes)
  5005. };
  5006. Chart.prototype.data.stackNormalized = function(normalized) {
  5007. if (normalized === undefined) {
  5008. return this.internal.isStackNormalized()
  5009. }
  5010. this.internal.config.data_stack_normalize = !!normalized;
  5011. this.internal.redraw();
  5012. };
  5013. Chart.prototype.donut = function() {};
  5014. Chart.prototype.donut.padAngle = function(padAngle) {
  5015. if (padAngle === undefined) {
  5016. return this.internal.config.donut_padAngle
  5017. }
  5018. this.internal.config.donut_padAngle = padAngle;
  5019. this.flush();
  5020. };
  5021. Chart.prototype.flow = function(args) {
  5022. var $$ = this.internal,
  5023. targets,
  5024. data,
  5025. notfoundIds = [],
  5026. orgDataCount = $$.getMaxDataCount(),
  5027. dataCount,
  5028. domain,
  5029. baseTarget,
  5030. baseValue,
  5031. length = 0,
  5032. tail = 0,
  5033. diff,
  5034. to;
  5035. if (args.json) {
  5036. data = $$.convertJsonToData(args.json, args.keys);
  5037. } else if (args.rows) {
  5038. data = $$.convertRowsToData(args.rows);
  5039. } else if (args.columns) {
  5040. data = $$.convertColumnsToData(args.columns);
  5041. } else {
  5042. return
  5043. }
  5044. targets = $$.convertDataToTargets(data, true);
  5045. // Update/Add data
  5046. $$.data.targets.forEach(function(t) {
  5047. var found = false,
  5048. i,
  5049. j;
  5050. for (i = 0; i < targets.length; i++) {
  5051. if (t.id === targets[i].id) {
  5052. found = true;
  5053. if (t.values[t.values.length - 1]) {
  5054. tail = t.values[t.values.length - 1].index + 1;
  5055. }
  5056. length = targets[i].values.length;
  5057. for (j = 0; j < length; j++) {
  5058. targets[i].values[j].index = tail + j;
  5059. if (!$$.isTimeSeries()) {
  5060. targets[i].values[j].x = tail + j;
  5061. }
  5062. }
  5063. t.values = t.values.concat(targets[i].values);
  5064. targets.splice(i, 1);
  5065. break
  5066. }
  5067. }
  5068. if (!found) {
  5069. notfoundIds.push(t.id);
  5070. }
  5071. });
  5072. // Append null for not found targets
  5073. $$.data.targets.forEach(function(t) {
  5074. var i, j;
  5075. for (i = 0; i < notfoundIds.length; i++) {
  5076. if (t.id === notfoundIds[i]) {
  5077. tail = t.values[t.values.length - 1].index + 1;
  5078. for (j = 0; j < length; j++) {
  5079. t.values.push({
  5080. id: t.id,
  5081. index: tail + j,
  5082. x: $$.isTimeSeries() ? $$.getOtherTargetX(tail + j) : tail + j,
  5083. value: null
  5084. });
  5085. }
  5086. }
  5087. }
  5088. });
  5089. // Generate null values for new target
  5090. if ($$.data.targets.length) {
  5091. targets.forEach(function(t) {
  5092. var i,
  5093. missing = [];
  5094. for (i = $$.data.targets[0].values[0].index; i < tail; i++) {
  5095. missing.push({
  5096. id: t.id,
  5097. index: i,
  5098. x: $$.isTimeSeries() ? $$.getOtherTargetX(i) : i,
  5099. value: null
  5100. });
  5101. }
  5102. t.values.forEach(function(v) {
  5103. v.index += tail;
  5104. if (!$$.isTimeSeries()) {
  5105. v.x += tail;
  5106. }
  5107. });
  5108. t.values = missing.concat(t.values);
  5109. });
  5110. }
  5111. $$.data.targets = $$.data.targets.concat(targets); // add remained
  5112. // check data count because behavior needs to change when it's only one
  5113. dataCount = $$.getMaxDataCount();
  5114. baseTarget = $$.data.targets[0];
  5115. baseValue = baseTarget.values[0];
  5116. // Update length to flow if needed
  5117. if (isDefined(args.to)) {
  5118. length = 0;
  5119. to = $$.isTimeSeries() ? $$.parseDate(args.to) : args.to;
  5120. baseTarget.values.forEach(function(v) {
  5121. if (v.x < to) {
  5122. length++;
  5123. }
  5124. });
  5125. } else if (isDefined(args.length)) {
  5126. length = args.length;
  5127. }
  5128. // If only one data, update the domain to flow from left edge of the chart
  5129. if (!orgDataCount) {
  5130. if ($$.isTimeSeries()) {
  5131. if (baseTarget.values.length > 1) {
  5132. diff = baseTarget.values[baseTarget.values.length - 1].x - baseValue.x;
  5133. } else {
  5134. diff = baseValue.x - $$.getXDomain($$.data.targets)[0];
  5135. }
  5136. } else {
  5137. diff = 1;
  5138. }
  5139. domain = [baseValue.x - diff, baseValue.x];
  5140. $$.updateXDomain(null, true, true, false, domain);
  5141. } else if (orgDataCount === 1) {
  5142. if ($$.isTimeSeries()) {
  5143. diff =
  5144. (baseTarget.values[baseTarget.values.length - 1].x - baseValue.x) / 2;
  5145. domain = [new Date(+baseValue.x - diff), new Date(+baseValue.x + diff)];
  5146. $$.updateXDomain(null, true, true, false, domain);
  5147. }
  5148. }
  5149. // Set targets
  5150. $$.updateTargets($$.data.targets);
  5151. // Redraw with new targets
  5152. $$.redraw({
  5153. flow: {
  5154. index: baseValue.index,
  5155. length: length,
  5156. duration: isValue(args.duration)
  5157. ? args.duration
  5158. : $$.config.transition_duration,
  5159. done: args.done,
  5160. orgDataCount: orgDataCount
  5161. },
  5162. withLegend: true,
  5163. withTransition: orgDataCount > 1,
  5164. withTrimXDomain: false,
  5165. withUpdateXAxis: true
  5166. });
  5167. };
  5168. ChartInternal.prototype.generateFlow = function(args) {
  5169. var $$ = this,
  5170. config = $$.config,
  5171. d3 = $$.d3;
  5172. return function() {
  5173. var targets = args.targets,
  5174. flow = args.flow,
  5175. drawBar = args.drawBar,
  5176. drawLine = args.drawLine,
  5177. drawArea = args.drawArea,
  5178. cx = args.cx,
  5179. cy = args.cy,
  5180. xv = args.xv,
  5181. xForText = args.xForText,
  5182. yForText = args.yForText,
  5183. duration = args.duration;
  5184. var translateX,
  5185. scaleX = 1,
  5186. transform,
  5187. flowIndex = flow.index,
  5188. flowLength = flow.length,
  5189. flowStart = $$.getValueOnIndex($$.data.targets[0].values, flowIndex),
  5190. flowEnd = $$.getValueOnIndex(
  5191. $$.data.targets[0].values,
  5192. flowIndex + flowLength
  5193. ),
  5194. orgDomain = $$.x.domain(),
  5195. domain,
  5196. durationForFlow = flow.duration || duration,
  5197. done = flow.done || function() {},
  5198. wait = $$.generateWait();
  5199. var xgrid,
  5200. xgridLines,
  5201. mainRegion,
  5202. mainText,
  5203. mainBar,
  5204. mainLine,
  5205. mainArea,
  5206. mainCircle;
  5207. // set flag
  5208. $$.flowing = true;
  5209. // remove head data after rendered
  5210. $$.data.targets.forEach(function(d) {
  5211. d.values.splice(0, flowLength);
  5212. });
  5213. // update x domain to generate axis elements for flow
  5214. domain = $$.updateXDomain(targets, true, true);
  5215. // update elements related to x scale
  5216. if ($$.updateXGrid) {
  5217. $$.updateXGrid(true);
  5218. }
  5219. xgrid = $$.xgrid || d3.selectAll([]); // xgrid needs to be obtained after updateXGrid
  5220. xgridLines = $$.xgridLines || d3.selectAll([]);
  5221. mainRegion = $$.mainRegion || d3.selectAll([]);
  5222. mainText = $$.mainText || d3.selectAll([]);
  5223. mainBar = $$.mainBar || d3.selectAll([]);
  5224. mainLine = $$.mainLine || d3.selectAll([]);
  5225. mainArea = $$.mainArea || d3.selectAll([]);
  5226. mainCircle = $$.mainCircle || d3.selectAll([]);
  5227. // generate transform to flow
  5228. if (!flow.orgDataCount) {
  5229. // if empty
  5230. if ($$.data.targets[0].values.length !== 1) {
  5231. translateX = $$.x(orgDomain[0]) - $$.x(domain[0]);
  5232. } else {
  5233. if ($$.isTimeSeries()) {
  5234. flowStart = $$.getValueOnIndex($$.data.targets[0].values, 0);
  5235. flowEnd = $$.getValueOnIndex(
  5236. $$.data.targets[0].values,
  5237. $$.data.targets[0].values.length - 1
  5238. );
  5239. translateX = $$.x(flowStart.x) - $$.x(flowEnd.x);
  5240. } else {
  5241. translateX = diffDomain(domain) / 2;
  5242. }
  5243. }
  5244. } else if (
  5245. flow.orgDataCount === 1 ||
  5246. (flowStart && flowStart.x) === (flowEnd && flowEnd.x)
  5247. ) {
  5248. translateX = $$.x(orgDomain[0]) - $$.x(domain[0]);
  5249. } else {
  5250. if ($$.isTimeSeries()) {
  5251. translateX = $$.x(orgDomain[0]) - $$.x(domain[0]);
  5252. } else {
  5253. translateX = $$.x(flowStart.x) - $$.x(flowEnd.x);
  5254. }
  5255. }
  5256. scaleX = diffDomain(orgDomain) / diffDomain(domain);
  5257. transform = 'translate(' + translateX + ',0) scale(' + scaleX + ',1)';
  5258. $$.hideXGridFocus();
  5259. var flowTransition = d3
  5260. .transition()
  5261. .ease(d3.easeLinear)
  5262. .duration(durationForFlow);
  5263. wait.add($$.xAxis($$.axes.x, flowTransition));
  5264. wait.add(mainBar.transition(flowTransition).attr('transform', transform));
  5265. wait.add(mainLine.transition(flowTransition).attr('transform', transform));
  5266. wait.add(mainArea.transition(flowTransition).attr('transform', transform));
  5267. wait.add(mainCircle.transition(flowTransition).attr('transform', transform));
  5268. wait.add(mainText.transition(flowTransition).attr('transform', transform));
  5269. wait.add(
  5270. mainRegion
  5271. .filter($$.isRegionOnX)
  5272. .transition(flowTransition)
  5273. .attr('transform', transform)
  5274. );
  5275. wait.add(xgrid.transition(flowTransition).attr('transform', transform));
  5276. wait.add(xgridLines.transition(flowTransition).attr('transform', transform));
  5277. wait(function() {
  5278. var i,
  5279. shapes = [],
  5280. texts = [];
  5281. // remove flowed elements
  5282. if (flowLength) {
  5283. for (i = 0; i < flowLength; i++) {
  5284. shapes.push('.' + CLASS.shape + '-' + (flowIndex + i));
  5285. texts.push('.' + CLASS.text + '-' + (flowIndex + i));
  5286. }
  5287. $$.svg
  5288. .selectAll('.' + CLASS.shapes)
  5289. .selectAll(shapes)
  5290. .remove();
  5291. $$.svg
  5292. .selectAll('.' + CLASS.texts)
  5293. .selectAll(texts)
  5294. .remove();
  5295. $$.svg.select('.' + CLASS.xgrid).remove();
  5296. }
  5297. // draw again for removing flowed elements and reverting attr
  5298. xgrid
  5299. .attr('transform', null)
  5300. .attr('x1', $$.xgridAttr.x1)
  5301. .attr('x2', $$.xgridAttr.x2)
  5302. .attr('y1', $$.xgridAttr.y1)
  5303. .attr('y2', $$.xgridAttr.y2)
  5304. .style('opacity', $$.xgridAttr.opacity);
  5305. xgridLines.attr('transform', null);
  5306. xgridLines
  5307. .select('line')
  5308. .attr('x1', config.axis_rotated ? 0 : xv)
  5309. .attr('x2', config.axis_rotated ? $$.width : xv);
  5310. xgridLines
  5311. .select('text')
  5312. .attr('x', config.axis_rotated ? $$.width : 0)
  5313. .attr('y', xv);
  5314. mainBar.attr('transform', null).attr('d', drawBar);
  5315. mainLine.attr('transform', null).attr('d', drawLine);
  5316. mainArea.attr('transform', null).attr('d', drawArea);
  5317. mainCircle
  5318. .attr('transform', null)
  5319. .attr('cx', cx)
  5320. .attr('cy', cy);
  5321. mainText
  5322. .attr('transform', null)
  5323. .attr('x', xForText)
  5324. .attr('y', yForText)
  5325. .style('fill-opacity', $$.opacityForText.bind($$));
  5326. mainRegion.attr('transform', null);
  5327. mainRegion
  5328. .filter($$.isRegionOnX)
  5329. .attr('x', $$.regionX.bind($$))
  5330. .attr('width', $$.regionWidth.bind($$));
  5331. // callback for end of flow
  5332. done();
  5333. $$.flowing = false;
  5334. });
  5335. }
  5336. };
  5337. Chart.prototype.focus = function(targetIds) {
  5338. var $$ = this.internal,
  5339. candidates;
  5340. targetIds = $$.mapToTargetIds(targetIds)
  5341. ;(candidates = $$.svg.selectAll(
  5342. $$.selectorTargets(targetIds.filter($$.isTargetToShow, $$))
  5343. )),
  5344. this.revert();
  5345. this.defocus();
  5346. candidates.classed(CLASS.focused, true).classed(CLASS.defocused, false);
  5347. if ($$.hasArcType()) {
  5348. $$.expandArc(targetIds);
  5349. }
  5350. $$.toggleFocusLegend(targetIds, true);
  5351. $$.focusedTargetIds = targetIds;
  5352. $$.defocusedTargetIds = $$.defocusedTargetIds.filter(function(id) {
  5353. return targetIds.indexOf(id) < 0
  5354. });
  5355. };
  5356. Chart.prototype.defocus = function(targetIds) {
  5357. var $$ = this.internal,
  5358. candidates;
  5359. targetIds = $$.mapToTargetIds(targetIds)
  5360. ;(candidates = $$.svg.selectAll(
  5361. $$.selectorTargets(targetIds.filter($$.isTargetToShow, $$))
  5362. )),
  5363. candidates.classed(CLASS.focused, false).classed(CLASS.defocused, true);
  5364. if ($$.hasArcType()) {
  5365. $$.unexpandArc(targetIds);
  5366. }
  5367. $$.toggleFocusLegend(targetIds, false);
  5368. $$.focusedTargetIds = $$.focusedTargetIds.filter(function(id) {
  5369. return targetIds.indexOf(id) < 0
  5370. });
  5371. $$.defocusedTargetIds = targetIds;
  5372. };
  5373. Chart.prototype.revert = function(targetIds) {
  5374. var $$ = this.internal,
  5375. candidates;
  5376. targetIds = $$.mapToTargetIds(targetIds);
  5377. candidates = $$.svg.selectAll($$.selectorTargets(targetIds)); // should be for all targets
  5378. candidates.classed(CLASS.focused, false).classed(CLASS.defocused, false);
  5379. if ($$.hasArcType()) {
  5380. $$.unexpandArc(targetIds);
  5381. }
  5382. if ($$.config.legend_show) {
  5383. $$.showLegend(targetIds.filter($$.isLegendToShow.bind($$)));
  5384. $$.legend
  5385. .selectAll($$.selectorLegends(targetIds))
  5386. .filter(function() {
  5387. return $$.d3.select(this).classed(CLASS.legendItemFocused)
  5388. })
  5389. .classed(CLASS.legendItemFocused, false);
  5390. }
  5391. $$.focusedTargetIds = [];
  5392. $$.defocusedTargetIds = [];
  5393. };
  5394. Chart.prototype.xgrids = function(grids) {
  5395. var $$ = this.internal,
  5396. config = $$.config;
  5397. if (!grids) {
  5398. return config.grid_x_lines
  5399. }
  5400. config.grid_x_lines = grids;
  5401. $$.redrawWithoutRescale();
  5402. return config.grid_x_lines
  5403. };
  5404. Chart.prototype.xgrids.add = function(grids) {
  5405. var $$ = this.internal;
  5406. return this.xgrids($$.config.grid_x_lines.concat(grids ? grids : []))
  5407. };
  5408. Chart.prototype.xgrids.remove = function(params) {
  5409. // TODO: multiple
  5410. var $$ = this.internal;
  5411. $$.removeGridLines(params, true);
  5412. };
  5413. Chart.prototype.ygrids = function(grids) {
  5414. var $$ = this.internal,
  5415. config = $$.config;
  5416. if (!grids) {
  5417. return config.grid_y_lines
  5418. }
  5419. config.grid_y_lines = grids;
  5420. $$.redrawWithoutRescale();
  5421. return config.grid_y_lines
  5422. };
  5423. Chart.prototype.ygrids.add = function(grids) {
  5424. var $$ = this.internal;
  5425. return this.ygrids($$.config.grid_y_lines.concat(grids ? grids : []))
  5426. };
  5427. Chart.prototype.ygrids.remove = function(params) {
  5428. // TODO: multiple
  5429. var $$ = this.internal;
  5430. $$.removeGridLines(params, false);
  5431. };
  5432. Chart.prototype.groups = function(groups) {
  5433. var $$ = this.internal,
  5434. config = $$.config;
  5435. if (isUndefined(groups)) {
  5436. return config.data_groups
  5437. }
  5438. config.data_groups = groups;
  5439. $$.redraw();
  5440. return config.data_groups
  5441. };
  5442. Chart.prototype.legend = function() {};
  5443. Chart.prototype.legend.show = function(targetIds) {
  5444. var $$ = this.internal;
  5445. $$.showLegend($$.mapToTargetIds(targetIds));
  5446. $$.updateAndRedraw({ withLegend: true });
  5447. };
  5448. Chart.prototype.legend.hide = function(targetIds) {
  5449. var $$ = this.internal;
  5450. $$.hideLegend($$.mapToTargetIds(targetIds));
  5451. $$.updateAndRedraw({ withLegend: false });
  5452. };
  5453. Chart.prototype.load = function(args) {
  5454. var $$ = this.internal,
  5455. config = $$.config;
  5456. // update xs if specified
  5457. if (args.xs) {
  5458. $$.addXs(args.xs);
  5459. }
  5460. // update names if exists
  5461. if ('names' in args) {
  5462. Chart.prototype.data.names.bind(this)(args.names);
  5463. }
  5464. // update classes if exists
  5465. if ('classes' in args) {
  5466. Object.keys(args.classes).forEach(function(id) {
  5467. config.data_classes[id] = args.classes[id];
  5468. });
  5469. }
  5470. // update categories if exists
  5471. if ('categories' in args && $$.isCategorized()) {
  5472. config.axis_x_categories = args.categories;
  5473. }
  5474. // update axes if exists
  5475. if ('axes' in args) {
  5476. Object.keys(args.axes).forEach(function(id) {
  5477. config.data_axes[id] = args.axes[id];
  5478. });
  5479. }
  5480. // update colors if exists
  5481. if ('colors' in args) {
  5482. Object.keys(args.colors).forEach(function(id) {
  5483. config.data_colors[id] = args.colors[id];
  5484. });
  5485. }
  5486. // use cache if exists
  5487. if ('cacheIds' in args && $$.hasCaches(args.cacheIds)) {
  5488. $$.load($$.getCaches(args.cacheIds), args.done);
  5489. return
  5490. }
  5491. // unload if needed
  5492. if (args.unload) {
  5493. // TODO: do not unload if target will load (included in url/rows/columns)
  5494. $$.unload(
  5495. $$.mapToTargetIds(args.unload === true ? null : args.unload),
  5496. function() {
  5497. $$.loadFromArgs(args);
  5498. }
  5499. );
  5500. } else {
  5501. $$.loadFromArgs(args);
  5502. }
  5503. };
  5504. Chart.prototype.unload = function(args) {
  5505. var $$ = this.internal;
  5506. args = args || {};
  5507. if (args instanceof Array) {
  5508. args = { ids: args };
  5509. } else if (typeof args === 'string') {
  5510. args = { ids: [args] };
  5511. }
  5512. $$.unload($$.mapToTargetIds(args.ids), function() {
  5513. $$.redraw({
  5514. withUpdateOrgXDomain: true,
  5515. withUpdateXDomain: true,
  5516. withLegend: true
  5517. });
  5518. if (args.done) {
  5519. args.done();
  5520. }
  5521. });
  5522. };
  5523. Chart.prototype.pie = function() {};
  5524. Chart.prototype.pie.padAngle = function(padAngle) {
  5525. if (padAngle === undefined) {
  5526. return this.internal.config.pie_padAngle
  5527. }
  5528. this.internal.config.pie_padAngle = padAngle;
  5529. this.flush();
  5530. };
  5531. Chart.prototype.regions = function(regions) {
  5532. var $$ = this.internal,
  5533. config = $$.config;
  5534. if (!regions) {
  5535. return config.regions
  5536. }
  5537. config.regions = regions;
  5538. $$.redrawWithoutRescale();
  5539. return config.regions
  5540. };
  5541. Chart.prototype.regions.add = function(regions) {
  5542. var $$ = this.internal,
  5543. config = $$.config;
  5544. if (!regions) {
  5545. return config.regions
  5546. }
  5547. config.regions = config.regions.concat(regions);
  5548. $$.redrawWithoutRescale();
  5549. return config.regions
  5550. };
  5551. Chart.prototype.regions.remove = function(options) {
  5552. var $$ = this.internal,
  5553. config = $$.config,
  5554. duration,
  5555. classes,
  5556. regions;
  5557. options = options || {};
  5558. duration = getOption(options, 'duration', config.transition_duration);
  5559. classes = getOption(options, 'classes', [CLASS.region]);
  5560. regions = $$.main.select('.' + CLASS.regions).selectAll(
  5561. classes.map(function(c) {
  5562. return '.' + c
  5563. })
  5564. )
  5565. ;(duration ? regions.transition().duration(duration) : regions)
  5566. .style('opacity', 0)
  5567. .remove();
  5568. config.regions = config.regions.filter(function(region) {
  5569. var found = false;
  5570. if (!region['class']) {
  5571. return true
  5572. }
  5573. region['class'].split(' ').forEach(function(c) {
  5574. if (classes.indexOf(c) >= 0) {
  5575. found = true;
  5576. }
  5577. });
  5578. return !found
  5579. });
  5580. return config.regions
  5581. };
  5582. Chart.prototype.selected = function(targetId) {
  5583. var $$ = this.internal,
  5584. d3 = $$.d3;
  5585. return $$.main
  5586. .selectAll('.' + CLASS.shapes + $$.getTargetSelectorSuffix(targetId))
  5587. .selectAll('.' + CLASS.shape)
  5588. .filter(function() {
  5589. return d3.select(this).classed(CLASS.SELECTED)
  5590. })
  5591. .nodes()
  5592. .map(function(d) {
  5593. var data = d.__data__;
  5594. return data.data ? data.data : data
  5595. })
  5596. };
  5597. Chart.prototype.select = function(ids, indices, resetOther) {
  5598. var $$ = this.internal,
  5599. d3 = $$.d3,
  5600. config = $$.config;
  5601. if (!config.data_selection_enabled) {
  5602. return
  5603. }
  5604. $$.main
  5605. .selectAll('.' + CLASS.shapes)
  5606. .selectAll('.' + CLASS.shape)
  5607. .each(function(d, i) {
  5608. var shape = d3.select(this),
  5609. id = d.data ? d.data.id : d.id,
  5610. toggle = $$.getToggle(this, d).bind($$),
  5611. isTargetId =
  5612. config.data_selection_grouped || !ids || ids.indexOf(id) >= 0,
  5613. isTargetIndex = !indices || indices.indexOf(i) >= 0,
  5614. isSelected = shape.classed(CLASS.SELECTED);
  5615. // line/area selection not supported yet
  5616. if (shape.classed(CLASS.line) || shape.classed(CLASS.area)) {
  5617. return
  5618. }
  5619. if (isTargetId && isTargetIndex) {
  5620. if (config.data_selection_isselectable(d) && !isSelected) {
  5621. toggle(true, shape.classed(CLASS.SELECTED, true), d, i);
  5622. }
  5623. } else if (isDefined(resetOther) && resetOther) {
  5624. if (isSelected) {
  5625. toggle(false, shape.classed(CLASS.SELECTED, false), d, i);
  5626. }
  5627. }
  5628. });
  5629. };
  5630. Chart.prototype.unselect = function(ids, indices) {
  5631. var $$ = this.internal,
  5632. d3 = $$.d3,
  5633. config = $$.config;
  5634. if (!config.data_selection_enabled) {
  5635. return
  5636. }
  5637. $$.main
  5638. .selectAll('.' + CLASS.shapes)
  5639. .selectAll('.' + CLASS.shape)
  5640. .each(function(d, i) {
  5641. var shape = d3.select(this),
  5642. id = d.data ? d.data.id : d.id,
  5643. toggle = $$.getToggle(this, d).bind($$),
  5644. isTargetId =
  5645. config.data_selection_grouped || !ids || ids.indexOf(id) >= 0,
  5646. isTargetIndex = !indices || indices.indexOf(i) >= 0,
  5647. isSelected = shape.classed(CLASS.SELECTED);
  5648. // line/area selection not supported yet
  5649. if (shape.classed(CLASS.line) || shape.classed(CLASS.area)) {
  5650. return
  5651. }
  5652. if (isTargetId && isTargetIndex) {
  5653. if (config.data_selection_isselectable(d)) {
  5654. if (isSelected) {
  5655. toggle(false, shape.classed(CLASS.SELECTED, false), d, i);
  5656. }
  5657. }
  5658. }
  5659. });
  5660. };
  5661. Chart.prototype.show = function(targetIds, options) {
  5662. var $$ = this.internal,
  5663. targets;
  5664. targetIds = $$.mapToTargetIds(targetIds);
  5665. options = options || {};
  5666. $$.removeHiddenTargetIds(targetIds);
  5667. targets = $$.svg.selectAll($$.selectorTargets(targetIds));
  5668. targets
  5669. .transition()
  5670. .style('display', isIE() ? 'block' : 'initial', 'important')
  5671. .style('opacity', 1, 'important')
  5672. .call($$.endall, function() {
  5673. targets.style('opacity', null).style('opacity', 1);
  5674. });
  5675. if (options.withLegend) {
  5676. $$.showLegend(targetIds);
  5677. }
  5678. $$.redraw({
  5679. withUpdateOrgXDomain: true,
  5680. withUpdateXDomain: true,
  5681. withLegend: true
  5682. });
  5683. };
  5684. Chart.prototype.hide = function(targetIds, options) {
  5685. var $$ = this.internal,
  5686. targets;
  5687. targetIds = $$.mapToTargetIds(targetIds);
  5688. options = options || {};
  5689. $$.addHiddenTargetIds(targetIds);
  5690. targets = $$.svg.selectAll($$.selectorTargets(targetIds));
  5691. targets
  5692. .transition()
  5693. .style('opacity', 0, 'important')
  5694. .call($$.endall, function() {
  5695. targets.style('opacity', null).style('opacity', 0);
  5696. targets.style('display', 'none');
  5697. });
  5698. if (options.withLegend) {
  5699. $$.hideLegend(targetIds);
  5700. }
  5701. $$.redraw({
  5702. withUpdateOrgXDomain: true,
  5703. withUpdateXDomain: true,
  5704. withLegend: true
  5705. });
  5706. };
  5707. Chart.prototype.toggle = function(targetIds, options) {
  5708. var that = this,
  5709. $$ = this.internal;
  5710. $$.mapToTargetIds(targetIds).forEach(function(targetId) {
  5711. $$.isTargetToShow(targetId)
  5712. ? that.hide(targetId, options)
  5713. : that.show(targetId, options);
  5714. });
  5715. };
  5716. Chart.prototype.subchart = function() {};
  5717. Chart.prototype.subchart.isShown = function() {
  5718. const $$ = this.internal;
  5719. return $$.config.subchart_show
  5720. };
  5721. Chart.prototype.subchart.show = function() {
  5722. const $$ = this.internal;
  5723. if ($$.config.subchart_show) {
  5724. return
  5725. }
  5726. $$.config.subchart_show = true;
  5727. // insert DOM
  5728. $$.initSubchart();
  5729. // update dimensions with sub chart now visible
  5730. $$.updateDimension();
  5731. // insert brush (depends on sizes previously updated)
  5732. $$.initSubchartBrush();
  5733. // attach data
  5734. $$.updateTargetsForSubchart($$.getTargets());
  5735. // reset fade-in state
  5736. $$.mapToIds($$.data.targets).forEach(function(id) {
  5737. $$.withoutFadeIn[id] = false;
  5738. });
  5739. // redraw chart !
  5740. $$.updateAndRedraw();
  5741. // update visible targets !
  5742. $$.showTargets();
  5743. };
  5744. Chart.prototype.subchart.hide = function() {
  5745. const $$ = this.internal;
  5746. if (!$$.config.subchart_show) {
  5747. return
  5748. }
  5749. $$.config.subchart_show = false;
  5750. // remove DOM
  5751. $$.removeSubchart();
  5752. // re-render chart
  5753. $$.redraw();
  5754. };
  5755. Chart.prototype.tooltip = function() {};
  5756. Chart.prototype.tooltip.show = function(args) {
  5757. var $$ = this.internal,
  5758. targets,
  5759. data,
  5760. mouse = {};
  5761. // determine mouse position on the chart
  5762. if (args.mouse) {
  5763. mouse = args.mouse;
  5764. } else {
  5765. // determine focus data
  5766. if (args.data) {
  5767. data = args.data;
  5768. } else if (typeof args.x !== 'undefined') {
  5769. if (args.id) {
  5770. targets = $$.data.targets.filter(function(t) {
  5771. return t.id === args.id
  5772. });
  5773. } else {
  5774. targets = $$.data.targets;
  5775. }
  5776. data = $$.filterByX(targets, args.x).slice(0, 1)[0];
  5777. }
  5778. mouse = data ? $$.getMousePosition(data) : null;
  5779. }
  5780. // emulate mouse events to show
  5781. $$.dispatchEvent('mousemove', mouse);
  5782. $$.config.tooltip_onshow.call($$, data);
  5783. };
  5784. Chart.prototype.tooltip.hide = function() {
  5785. // TODO: get target data by checking the state of focus
  5786. this.internal.dispatchEvent('mouseout', 0);
  5787. this.internal.config.tooltip_onhide.call(this);
  5788. };
  5789. Chart.prototype.transform = function(type, targetIds) {
  5790. var $$ = this.internal,
  5791. options =
  5792. ['pie', 'donut'].indexOf(type) >= 0 ? { withTransform: true } : null;
  5793. $$.transformTo(targetIds, type, options);
  5794. };
  5795. ChartInternal.prototype.transformTo = function(
  5796. targetIds,
  5797. type,
  5798. optionsForRedraw
  5799. ) {
  5800. var $$ = this,
  5801. withTransitionForAxis = !$$.hasArcType(),
  5802. options = optionsForRedraw || {
  5803. withTransitionForAxis: withTransitionForAxis
  5804. };
  5805. options.withTransitionForTransform = false;
  5806. $$.transiting = false;
  5807. $$.setTargetType(targetIds, type);
  5808. $$.updateTargets($$.data.targets); // this is needed when transforming to arc
  5809. $$.updateAndRedraw(options);
  5810. };
  5811. Chart.prototype.x = function(x) {
  5812. var $$ = this.internal;
  5813. if (arguments.length) {
  5814. $$.updateTargetX($$.data.targets, x);
  5815. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true });
  5816. }
  5817. return $$.data.xs
  5818. };
  5819. Chart.prototype.xs = function(xs) {
  5820. var $$ = this.internal;
  5821. if (arguments.length) {
  5822. $$.updateTargetXs($$.data.targets, xs);
  5823. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true });
  5824. }
  5825. return $$.data.xs
  5826. };
  5827. Chart.prototype.zoom = function(domain) {
  5828. var $$ = this.internal;
  5829. if (domain) {
  5830. if ($$.isTimeSeries()) {
  5831. domain = domain.map(function(x) {
  5832. return $$.parseDate(x)
  5833. });
  5834. }
  5835. if ($$.config.subchart_show) {
  5836. $$.brush.selectionAsValue(domain, true);
  5837. } else {
  5838. $$.updateXDomain(null, true, false, false, domain);
  5839. $$.redraw({ withY: $$.config.zoom_rescale, withSubchart: false });
  5840. }
  5841. $$.config.zoom_onzoom.call(this, $$.x.orgDomain());
  5842. return domain
  5843. } else {
  5844. return $$.x.domain()
  5845. }
  5846. };
  5847. Chart.prototype.zoom.enable = function(enabled) {
  5848. var $$ = this.internal;
  5849. $$.config.zoom_enabled = enabled;
  5850. $$.updateAndRedraw();
  5851. };
  5852. Chart.prototype.unzoom = function() {
  5853. var $$ = this.internal;
  5854. if ($$.config.subchart_show) {
  5855. $$.brush.clear();
  5856. } else {
  5857. $$.updateXDomain(null, true, false, false, $$.subX.domain());
  5858. $$.redraw({ withY: $$.config.zoom_rescale, withSubchart: false });
  5859. }
  5860. };
  5861. Chart.prototype.zoom.max = function(max) {
  5862. var $$ = this.internal,
  5863. config = $$.config,
  5864. d3 = $$.d3;
  5865. if (max === 0 || max) {
  5866. config.zoom_x_max = d3.max([$$.orgXDomain[1], max]);
  5867. } else {
  5868. return config.zoom_x_max
  5869. }
  5870. };
  5871. Chart.prototype.zoom.min = function(min) {
  5872. var $$ = this.internal,
  5873. config = $$.config,
  5874. d3 = $$.d3;
  5875. if (min === 0 || min) {
  5876. config.zoom_x_min = d3.min([$$.orgXDomain[0], min]);
  5877. } else {
  5878. return config.zoom_x_min
  5879. }
  5880. };
  5881. Chart.prototype.zoom.range = function(range) {
  5882. if (arguments.length) {
  5883. if (isDefined(range.max)) {
  5884. this.domain.max(range.max);
  5885. }
  5886. if (isDefined(range.min)) {
  5887. this.domain.min(range.min);
  5888. }
  5889. } else {
  5890. return {
  5891. max: this.domain.max(),
  5892. min: this.domain.min()
  5893. }
  5894. }
  5895. };
  5896. ChartInternal.prototype.initPie = function() {
  5897. var $$ = this,
  5898. d3 = $$.d3;
  5899. $$.pie = d3
  5900. .pie()
  5901. .padAngle(this.getPadAngle.bind(this))
  5902. .value(function(d) {
  5903. return d.values.reduce(function(a, b) {
  5904. return a + b.value
  5905. }, 0)
  5906. });
  5907. let orderFct = $$.getOrderFunction();
  5908. // we need to reverse the returned order if asc or desc to have the slice in expected order.
  5909. if (orderFct && ($$.isOrderAsc() || $$.isOrderDesc())) {
  5910. let defaultSort = orderFct;
  5911. orderFct = (t1, t2) => defaultSort(t1, t2) * -1;
  5912. }
  5913. $$.pie.sort(orderFct || null);
  5914. };
  5915. ChartInternal.prototype.updateRadius = function() {
  5916. var $$ = this,
  5917. config = $$.config,
  5918. w = config.gauge_width || config.donut_width,
  5919. gaugeArcWidth =
  5920. $$.filterTargetsToShow($$.data.targets).length *
  5921. $$.config.gauge_arcs_minWidth;
  5922. $$.radiusExpanded =
  5923. (Math.min($$.arcWidth, $$.arcHeight) / 2) * ($$.hasType('gauge') ? 0.85 : 1);
  5924. $$.radius = $$.radiusExpanded * 0.95;
  5925. $$.innerRadiusRatio = w ? ($$.radius - w) / $$.radius : 0.6;
  5926. $$.innerRadius =
  5927. $$.hasType('donut') || $$.hasType('gauge')
  5928. ? $$.radius * $$.innerRadiusRatio
  5929. : 0;
  5930. $$.gaugeArcWidth = w
  5931. ? w
  5932. : gaugeArcWidth <= $$.radius - $$.innerRadius
  5933. ? $$.radius - $$.innerRadius
  5934. : gaugeArcWidth <= $$.radius
  5935. ? gaugeArcWidth
  5936. : $$.radius;
  5937. };
  5938. ChartInternal.prototype.getPadAngle = function() {
  5939. if (this.hasType('pie')) {
  5940. return this.config.pie_padAngle || 0
  5941. } else if (this.hasType('donut')) {
  5942. return this.config.donut_padAngle || 0
  5943. } else {
  5944. return 0
  5945. }
  5946. };
  5947. ChartInternal.prototype.updateArc = function() {
  5948. var $$ = this;
  5949. $$.svgArc = $$.getSvgArc();
  5950. $$.svgArcExpanded = $$.getSvgArcExpanded();
  5951. $$.svgArcExpandedSub = $$.getSvgArcExpanded(0.98);
  5952. };
  5953. ChartInternal.prototype.updateAngle = function(d) {
  5954. var $$ = this,
  5955. config = $$.config,
  5956. found = false,
  5957. index = 0,
  5958. gMin,
  5959. gMax,
  5960. gTic,
  5961. gValue;
  5962. if (!config) {
  5963. return null
  5964. }
  5965. $$.pie($$.filterTargetsToShow($$.data.targets)).forEach(function(t) {
  5966. if (!found && t.data.id === d.data.id) {
  5967. found = true;
  5968. d = t;
  5969. d.index = index;
  5970. }
  5971. index++;
  5972. });
  5973. if (isNaN(d.startAngle)) {
  5974. d.startAngle = 0;
  5975. }
  5976. if (isNaN(d.endAngle)) {
  5977. d.endAngle = d.startAngle;
  5978. }
  5979. if ($$.isGaugeType(d.data)) {
  5980. gMin = config.gauge_min;
  5981. gMax = config.gauge_max;
  5982. gTic = (Math.PI * (config.gauge_fullCircle ? 2 : 1)) / (gMax - gMin);
  5983. gValue = d.value < gMin ? 0 : d.value < gMax ? d.value - gMin : gMax - gMin;
  5984. d.startAngle = config.gauge_startingAngle;
  5985. d.endAngle = d.startAngle + gTic * gValue;
  5986. }
  5987. return found ? d : null
  5988. };
  5989. ChartInternal.prototype.getSvgArc = function() {
  5990. var $$ = this,
  5991. hasGaugeType = $$.hasType('gauge'),
  5992. singleArcWidth =
  5993. $$.gaugeArcWidth / $$.filterTargetsToShow($$.data.targets).length,
  5994. arc = $$.d3
  5995. .arc()
  5996. .outerRadius(function(d) {
  5997. return hasGaugeType ? $$.radius - singleArcWidth * d.index : $$.radius
  5998. })
  5999. .innerRadius(function(d) {
  6000. return hasGaugeType
  6001. ? $$.radius - singleArcWidth * (d.index + 1)
  6002. : $$.innerRadius
  6003. }),
  6004. newArc = function(d, withoutUpdate) {
  6005. var updated;
  6006. if (withoutUpdate) {
  6007. return arc(d)
  6008. } // for interpolate
  6009. updated = $$.updateAngle(d);
  6010. return updated ? arc(updated) : 'M 0 0'
  6011. };
  6012. // TODO: extends all function
  6013. newArc.centroid = arc.centroid;
  6014. return newArc
  6015. };
  6016. ChartInternal.prototype.getSvgArcExpanded = function(rate) {
  6017. rate = rate || 1;
  6018. var $$ = this,
  6019. hasGaugeType = $$.hasType('gauge'),
  6020. singleArcWidth =
  6021. $$.gaugeArcWidth / $$.filterTargetsToShow($$.data.targets).length,
  6022. expandWidth = Math.min(
  6023. $$.radiusExpanded * rate - $$.radius,
  6024. singleArcWidth * 0.8 - (1 - rate) * 100
  6025. ),
  6026. arc = $$.d3
  6027. .arc()
  6028. .outerRadius(function(d) {
  6029. return hasGaugeType
  6030. ? $$.radius - singleArcWidth * d.index + expandWidth
  6031. : $$.radiusExpanded * rate
  6032. })
  6033. .innerRadius(function(d) {
  6034. return hasGaugeType
  6035. ? $$.radius - singleArcWidth * (d.index + 1)
  6036. : $$.innerRadius
  6037. });
  6038. return function(d) {
  6039. var updated = $$.updateAngle(d);
  6040. return updated ? arc(updated) : 'M 0 0'
  6041. }
  6042. };
  6043. ChartInternal.prototype.getArc = function(d, withoutUpdate, force) {
  6044. return force || this.isArcType(d.data)
  6045. ? this.svgArc(d, withoutUpdate)
  6046. : 'M 0 0'
  6047. };
  6048. ChartInternal.prototype.transformForArcLabel = function(d) {
  6049. var $$ = this,
  6050. config = $$.config,
  6051. updated = $$.updateAngle(d),
  6052. c,
  6053. x,
  6054. y,
  6055. h,
  6056. ratio,
  6057. translate = '',
  6058. hasGauge = $$.hasType('gauge');
  6059. if (updated && !hasGauge) {
  6060. c = this.svgArc.centroid(updated);
  6061. x = isNaN(c[0]) ? 0 : c[0];
  6062. y = isNaN(c[1]) ? 0 : c[1];
  6063. h = Math.sqrt(x * x + y * y);
  6064. if ($$.hasType('donut') && config.donut_label_ratio) {
  6065. ratio = isFunction(config.donut_label_ratio)
  6066. ? config.donut_label_ratio(d, $$.radius, h)
  6067. : config.donut_label_ratio;
  6068. } else if ($$.hasType('pie') && config.pie_label_ratio) {
  6069. ratio = isFunction(config.pie_label_ratio)
  6070. ? config.pie_label_ratio(d, $$.radius, h)
  6071. : config.pie_label_ratio;
  6072. } else {
  6073. ratio =
  6074. $$.radius && h
  6075. ? ((36 / $$.radius > 0.375 ? 1.175 - 36 / $$.radius : 0.8) *
  6076. $$.radius) /
  6077. h
  6078. : 0;
  6079. }
  6080. translate = 'translate(' + x * ratio + ',' + y * ratio + ')';
  6081. } else if (
  6082. updated &&
  6083. hasGauge &&
  6084. $$.filterTargetsToShow($$.data.targets).length > 1
  6085. ) {
  6086. var y1 = Math.sin(updated.endAngle - Math.PI / 2);
  6087. x = Math.cos(updated.endAngle - Math.PI / 2) * ($$.radiusExpanded + 25);
  6088. y = y1 * ($$.radiusExpanded + 15 - Math.abs(y1 * 10)) + 3;
  6089. translate = 'translate(' + x + ',' + y + ')';
  6090. }
  6091. return translate
  6092. };
  6093. /**
  6094. * @deprecated Use `getRatio('arc', d)` instead.
  6095. */
  6096. ChartInternal.prototype.getArcRatio = function(d) {
  6097. return this.getRatio('arc', d)
  6098. };
  6099. ChartInternal.prototype.convertToArcData = function(d) {
  6100. return this.addName({
  6101. id: d.data.id,
  6102. value: d.value,
  6103. ratio: this.getRatio('arc', d),
  6104. index: d.index
  6105. })
  6106. };
  6107. ChartInternal.prototype.textForArcLabel = function(d) {
  6108. var $$ = this,
  6109. updated,
  6110. value,
  6111. ratio,
  6112. id,
  6113. format;
  6114. if (!$$.shouldShowArcLabel()) {
  6115. return ''
  6116. }
  6117. updated = $$.updateAngle(d);
  6118. value = updated ? updated.value : null;
  6119. ratio = $$.getRatio('arc', updated);
  6120. id = d.data.id;
  6121. if (!$$.hasType('gauge') && !$$.meetsArcLabelThreshold(ratio)) {
  6122. return ''
  6123. }
  6124. format = $$.getArcLabelFormat();
  6125. return format
  6126. ? format(value, ratio, id)
  6127. : $$.defaultArcValueFormat(value, ratio)
  6128. };
  6129. ChartInternal.prototype.textForGaugeMinMax = function(value, isMax) {
  6130. var $$ = this,
  6131. format = $$.getGaugeLabelExtents();
  6132. return format ? format(value, isMax) : value
  6133. };
  6134. ChartInternal.prototype.expandArc = function(targetIds) {
  6135. var $$ = this,
  6136. interval;
  6137. // MEMO: avoid to cancel transition
  6138. if ($$.transiting) {
  6139. interval = window.setInterval(function() {
  6140. if (!$$.transiting) {
  6141. window.clearInterval(interval);
  6142. if ($$.legend.selectAll('.c3-legend-item-focused').size() > 0) {
  6143. $$.expandArc(targetIds);
  6144. }
  6145. }
  6146. }, 10);
  6147. return
  6148. }
  6149. targetIds = $$.mapToTargetIds(targetIds);
  6150. $$.svg
  6151. .selectAll($$.selectorTargets(targetIds, '.' + CLASS.chartArc))
  6152. .each(function(d) {
  6153. if (!$$.shouldExpand(d.data.id)) {
  6154. return
  6155. }
  6156. $$.d3
  6157. .select(this)
  6158. .selectAll('path')
  6159. .transition()
  6160. .duration($$.expandDuration(d.data.id))
  6161. .attr('d', $$.svgArcExpanded)
  6162. .transition()
  6163. .duration($$.expandDuration(d.data.id) * 2)
  6164. .attr('d', $$.svgArcExpandedSub)
  6165. .each(function(d) {
  6166. if ($$.isDonutType(d.data)) ;
  6167. });
  6168. });
  6169. };
  6170. ChartInternal.prototype.unexpandArc = function(targetIds) {
  6171. var $$ = this;
  6172. if ($$.transiting) {
  6173. return
  6174. }
  6175. targetIds = $$.mapToTargetIds(targetIds);
  6176. $$.svg
  6177. .selectAll($$.selectorTargets(targetIds, '.' + CLASS.chartArc))
  6178. .selectAll('path')
  6179. .transition()
  6180. .duration(function(d) {
  6181. return $$.expandDuration(d.data.id)
  6182. })
  6183. .attr('d', $$.svgArc);
  6184. $$.svg.selectAll('.' + CLASS.arc);
  6185. };
  6186. ChartInternal.prototype.expandDuration = function(id) {
  6187. var $$ = this,
  6188. config = $$.config;
  6189. if ($$.isDonutType(id)) {
  6190. return config.donut_expand_duration
  6191. } else if ($$.isGaugeType(id)) {
  6192. return config.gauge_expand_duration
  6193. } else if ($$.isPieType(id)) {
  6194. return config.pie_expand_duration
  6195. } else {
  6196. return 50
  6197. }
  6198. };
  6199. ChartInternal.prototype.shouldExpand = function(id) {
  6200. var $$ = this,
  6201. config = $$.config;
  6202. return (
  6203. ($$.isDonutType(id) && config.donut_expand) ||
  6204. ($$.isGaugeType(id) && config.gauge_expand) ||
  6205. ($$.isPieType(id) && config.pie_expand)
  6206. )
  6207. };
  6208. ChartInternal.prototype.shouldShowArcLabel = function() {
  6209. var $$ = this,
  6210. config = $$.config,
  6211. shouldShow = true;
  6212. if ($$.hasType('donut')) {
  6213. shouldShow = config.donut_label_show;
  6214. } else if ($$.hasType('pie')) {
  6215. shouldShow = config.pie_label_show;
  6216. }
  6217. // when gauge, always true
  6218. return shouldShow
  6219. };
  6220. ChartInternal.prototype.meetsArcLabelThreshold = function(ratio) {
  6221. var $$ = this,
  6222. config = $$.config,
  6223. threshold = $$.hasType('donut')
  6224. ? config.donut_label_threshold
  6225. : config.pie_label_threshold;
  6226. return ratio >= threshold
  6227. };
  6228. ChartInternal.prototype.getArcLabelFormat = function() {
  6229. var $$ = this,
  6230. config = $$.config,
  6231. format = config.pie_label_format;
  6232. if ($$.hasType('gauge')) {
  6233. format = config.gauge_label_format;
  6234. } else if ($$.hasType('donut')) {
  6235. format = config.donut_label_format;
  6236. }
  6237. return format
  6238. };
  6239. ChartInternal.prototype.getGaugeLabelExtents = function() {
  6240. var $$ = this,
  6241. config = $$.config;
  6242. return config.gauge_label_extents
  6243. };
  6244. ChartInternal.prototype.getArcTitle = function() {
  6245. var $$ = this;
  6246. return $$.hasType('donut') ? $$.config.donut_title : ''
  6247. };
  6248. ChartInternal.prototype.updateTargetsForArc = function(targets) {
  6249. var $$ = this,
  6250. main = $$.main,
  6251. mainPies,
  6252. mainPieEnter,
  6253. classChartArc = $$.classChartArc.bind($$),
  6254. classArcs = $$.classArcs.bind($$),
  6255. classFocus = $$.classFocus.bind($$);
  6256. mainPies = main
  6257. .select('.' + CLASS.chartArcs)
  6258. .selectAll('.' + CLASS.chartArc)
  6259. .data($$.pie(targets))
  6260. .attr('class', function(d) {
  6261. return classChartArc(d) + classFocus(d.data)
  6262. });
  6263. mainPieEnter = mainPies
  6264. .enter()
  6265. .append('g')
  6266. .attr('class', classChartArc);
  6267. mainPieEnter.append('g').attr('class', classArcs);
  6268. mainPieEnter
  6269. .append('text')
  6270. .attr('dy', $$.hasType('gauge') ? '-.1em' : '.35em')
  6271. .style('opacity', 0)
  6272. .style('text-anchor', 'middle')
  6273. .style('pointer-events', 'none');
  6274. // MEMO: can not keep same color..., but not bad to update color in redraw
  6275. //mainPieUpdate.exit().remove();
  6276. };
  6277. ChartInternal.prototype.initArc = function() {
  6278. var $$ = this;
  6279. $$.arcs = $$.main
  6280. .select('.' + CLASS.chart)
  6281. .append('g')
  6282. .attr('class', CLASS.chartArcs)
  6283. .attr('transform', $$.getTranslate('arc'));
  6284. $$.arcs
  6285. .append('text')
  6286. .attr('class', CLASS.chartArcsTitle)
  6287. .style('text-anchor', 'middle')
  6288. .text($$.getArcTitle());
  6289. };
  6290. ChartInternal.prototype.redrawArc = function(
  6291. duration,
  6292. durationForExit,
  6293. withTransform
  6294. ) {
  6295. var $$ = this,
  6296. d3 = $$.d3,
  6297. config = $$.config,
  6298. main = $$.main,
  6299. arcs,
  6300. mainArc,
  6301. arcLabelLines,
  6302. mainArcLabelLine,
  6303. hasGaugeType = $$.hasType('gauge');
  6304. arcs = main
  6305. .selectAll('.' + CLASS.arcs)
  6306. .selectAll('.' + CLASS.arc)
  6307. .data($$.arcData.bind($$));
  6308. mainArc = arcs
  6309. .enter()
  6310. .append('path')
  6311. .attr('class', $$.classArc.bind($$))
  6312. .style('fill', function(d) {
  6313. return $$.color(d.data)
  6314. })
  6315. .style('cursor', function(d) {
  6316. return config.interaction_enabled && config.data_selection_isselectable(d)
  6317. ? 'pointer'
  6318. : null
  6319. })
  6320. .each(function(d) {
  6321. if ($$.isGaugeType(d.data)) {
  6322. d.startAngle = d.endAngle = config.gauge_startingAngle;
  6323. }
  6324. this._current = d;
  6325. })
  6326. .merge(arcs);
  6327. if (hasGaugeType) {
  6328. arcLabelLines = main
  6329. .selectAll('.' + CLASS.arcs)
  6330. .selectAll('.' + CLASS.arcLabelLine)
  6331. .data($$.arcData.bind($$));
  6332. mainArcLabelLine = arcLabelLines
  6333. .enter()
  6334. .append('rect')
  6335. .attr('class', function(d) {
  6336. return (
  6337. CLASS.arcLabelLine +
  6338. ' ' +
  6339. CLASS.target +
  6340. ' ' +
  6341. CLASS.target +
  6342. '-' +
  6343. d.data.id
  6344. )
  6345. })
  6346. .merge(arcLabelLines);
  6347. if ($$.filterTargetsToShow($$.data.targets).length === 1) {
  6348. mainArcLabelLine.style('display', 'none');
  6349. } else {
  6350. mainArcLabelLine
  6351. .style('fill', function(d) {
  6352. return $$.levelColor
  6353. ? $$.levelColor(
  6354. d.data.values.reduce(function(total, item) {
  6355. return total + item.value
  6356. }, 0)
  6357. )
  6358. : $$.color(d.data)
  6359. })
  6360. .style('display', config.gauge_labelLine_show ? '' : 'none')
  6361. .each(function(d) {
  6362. var lineLength = 0,
  6363. lineThickness = 2,
  6364. x = 0,
  6365. y = 0,
  6366. transform = '';
  6367. if ($$.hiddenTargetIds.indexOf(d.data.id) < 0) {
  6368. var updated = $$.updateAngle(d),
  6369. innerLineLength =
  6370. ($$.gaugeArcWidth /
  6371. $$.filterTargetsToShow($$.data.targets).length) *
  6372. (updated.index + 1),
  6373. lineAngle = updated.endAngle - Math.PI / 2,
  6374. arcInnerRadius = $$.radius - innerLineLength,
  6375. linePositioningAngle =
  6376. lineAngle - (arcInnerRadius === 0 ? 0 : 1 / arcInnerRadius);
  6377. lineLength = $$.radiusExpanded - $$.radius + innerLineLength;
  6378. x = Math.cos(linePositioningAngle) * arcInnerRadius;
  6379. y = Math.sin(linePositioningAngle) * arcInnerRadius;
  6380. transform =
  6381. 'rotate(' +
  6382. (lineAngle * 180) / Math.PI +
  6383. ', ' +
  6384. x +
  6385. ', ' +
  6386. y +
  6387. ')';
  6388. }
  6389. d3.select(this)
  6390. .attr('x', x)
  6391. .attr('y', y)
  6392. .attr('width', lineLength)
  6393. .attr('height', lineThickness)
  6394. .attr('transform', transform)
  6395. .style(
  6396. 'stroke-dasharray',
  6397. '0, ' + (lineLength + lineThickness) + ', 0'
  6398. );
  6399. });
  6400. }
  6401. }
  6402. mainArc
  6403. .attr('transform', function(d) {
  6404. return !$$.isGaugeType(d.data) && withTransform ? 'scale(0)' : ''
  6405. })
  6406. .on(
  6407. 'mouseover',
  6408. config.interaction_enabled
  6409. ? function(d) {
  6410. var updated, arcData;
  6411. if ($$.transiting) {
  6412. // skip while transiting
  6413. return
  6414. }
  6415. updated = $$.updateAngle(d);
  6416. if (updated) {
  6417. arcData = $$.convertToArcData(updated);
  6418. // transitions
  6419. $$.expandArc(updated.data.id);
  6420. $$.api.focus(updated.data.id);
  6421. $$.toggleFocusLegend(updated.data.id, true);
  6422. $$.config.data_onmouseover(arcData, this);
  6423. }
  6424. }
  6425. : null
  6426. )
  6427. .on(
  6428. 'mousemove',
  6429. config.interaction_enabled
  6430. ? function(d) {
  6431. var updated = $$.updateAngle(d),
  6432. arcData,
  6433. selectedData;
  6434. if (updated) {
  6435. (arcData = $$.convertToArcData(updated)),
  6436. (selectedData = [arcData]);
  6437. $$.showTooltip(selectedData, this);
  6438. }
  6439. }
  6440. : null
  6441. )
  6442. .on(
  6443. 'mouseout',
  6444. config.interaction_enabled
  6445. ? function(d) {
  6446. var updated, arcData;
  6447. if ($$.transiting) {
  6448. // skip while transiting
  6449. return
  6450. }
  6451. updated = $$.updateAngle(d);
  6452. if (updated) {
  6453. arcData = $$.convertToArcData(updated);
  6454. // transitions
  6455. $$.unexpandArc(updated.data.id);
  6456. $$.api.revert();
  6457. $$.revertLegend();
  6458. $$.hideTooltip();
  6459. $$.config.data_onmouseout(arcData, this);
  6460. }
  6461. }
  6462. : null
  6463. )
  6464. .on(
  6465. 'click',
  6466. config.interaction_enabled
  6467. ? function(d, i) {
  6468. var updated = $$.updateAngle(d),
  6469. arcData;
  6470. if (updated) {
  6471. arcData = $$.convertToArcData(updated);
  6472. if ($$.toggleShape) {
  6473. $$.toggleShape(this, arcData, i);
  6474. }
  6475. $$.config.data_onclick.call($$.api, arcData, this);
  6476. }
  6477. }
  6478. : null
  6479. )
  6480. .each(function() {
  6481. $$.transiting = true;
  6482. })
  6483. .transition()
  6484. .duration(duration)
  6485. .attrTween('d', function(d) {
  6486. var updated = $$.updateAngle(d),
  6487. interpolate;
  6488. if (!updated) {
  6489. return function() {
  6490. return 'M 0 0'
  6491. }
  6492. }
  6493. // if (this._current === d) {
  6494. // this._current = {
  6495. // startAngle: Math.PI*2,
  6496. // endAngle: Math.PI*2,
  6497. // };
  6498. // }
  6499. if (isNaN(this._current.startAngle)) {
  6500. this._current.startAngle = 0;
  6501. }
  6502. if (isNaN(this._current.endAngle)) {
  6503. this._current.endAngle = this._current.startAngle;
  6504. }
  6505. interpolate = d3.interpolate(this._current, updated);
  6506. this._current = interpolate(0);
  6507. return function(t) {
  6508. // prevents crashing the charts once in transition and chart.destroy() has been called
  6509. if ($$.config === null) {
  6510. return 'M 0 0'
  6511. }
  6512. var interpolated = interpolate(t);
  6513. interpolated.data = d.data; // data.id will be updated by interporator
  6514. return $$.getArc(interpolated, true)
  6515. }
  6516. })
  6517. .attr('transform', withTransform ? 'scale(1)' : '')
  6518. .style('fill', function(d) {
  6519. return $$.levelColor
  6520. ? $$.levelColor(
  6521. d.data.values.reduce(function(total, item) {
  6522. return total + item.value
  6523. }, 0)
  6524. )
  6525. : $$.color(d.data.id)
  6526. }) // Where gauge reading color would receive customization.
  6527. .call($$.endall, function() {
  6528. $$.transiting = false;
  6529. });
  6530. arcs
  6531. .exit()
  6532. .transition()
  6533. .duration(durationForExit)
  6534. .style('opacity', 0)
  6535. .remove();
  6536. main
  6537. .selectAll('.' + CLASS.chartArc)
  6538. .select('text')
  6539. .style('opacity', 0)
  6540. .attr('class', function(d) {
  6541. return $$.isGaugeType(d.data) ? CLASS.gaugeValue : ''
  6542. })
  6543. .text($$.textForArcLabel.bind($$))
  6544. .attr('transform', $$.transformForArcLabel.bind($$))
  6545. .style('font-size', function(d) {
  6546. return $$.isGaugeType(d.data) &&
  6547. $$.filterTargetsToShow($$.data.targets).length === 1
  6548. ? Math.round($$.radius / 5) + 'px'
  6549. : ''
  6550. })
  6551. .transition()
  6552. .duration(duration)
  6553. .style('opacity', function(d) {
  6554. return $$.isTargetToShow(d.data.id) && $$.isArcType(d.data) ? 1 : 0
  6555. });
  6556. main
  6557. .select('.' + CLASS.chartArcsTitle)
  6558. .style('opacity', $$.hasType('donut') || hasGaugeType ? 1 : 0);
  6559. if (hasGaugeType) {
  6560. let index = 0;
  6561. const backgroundArc = $$.arcs
  6562. .select('g.' + CLASS.chartArcsBackground)
  6563. .selectAll('path.' + CLASS.chartArcsBackground)
  6564. .data($$.data.targets);
  6565. backgroundArc
  6566. .enter()
  6567. .append('path')
  6568. .attr(
  6569. 'class',
  6570. (d, i) =>
  6571. CLASS.chartArcsBackground + ' ' + CLASS.chartArcsBackground + '-' + i
  6572. )
  6573. .merge(backgroundArc)
  6574. .attr('d', d1 => {
  6575. if ($$.hiddenTargetIds.indexOf(d1.id) >= 0) {
  6576. return 'M 0 0'
  6577. }
  6578. var d = {
  6579. data: [{ value: config.gauge_max }],
  6580. startAngle: config.gauge_startingAngle,
  6581. endAngle:
  6582. -1 *
  6583. config.gauge_startingAngle *
  6584. (config.gauge_fullCircle ? Math.PI : 1),
  6585. index: index++
  6586. };
  6587. return $$.getArc(d, true, true)
  6588. });
  6589. backgroundArc.exit().remove();
  6590. $$.arcs
  6591. .select('.' + CLASS.chartArcsGaugeUnit)
  6592. .attr('dy', '.75em')
  6593. .text(config.gauge_label_show ? config.gauge_units : '');
  6594. $$.arcs
  6595. .select('.' + CLASS.chartArcsGaugeMin)
  6596. .attr(
  6597. 'dx',
  6598. -1 *
  6599. ($$.innerRadius +
  6600. ($$.radius - $$.innerRadius) / (config.gauge_fullCircle ? 1 : 2)) +
  6601. 'px'
  6602. )
  6603. .attr('dy', '1.2em')
  6604. .text(
  6605. config.gauge_label_show
  6606. ? $$.textForGaugeMinMax(config.gauge_min, false)
  6607. : ''
  6608. );
  6609. $$.arcs
  6610. .select('.' + CLASS.chartArcsGaugeMax)
  6611. .attr(
  6612. 'dx',
  6613. $$.innerRadius +
  6614. ($$.radius - $$.innerRadius) / (config.gauge_fullCircle ? 1 : 2) +
  6615. 'px'
  6616. )
  6617. .attr('dy', '1.2em')
  6618. .text(
  6619. config.gauge_label_show
  6620. ? $$.textForGaugeMinMax(config.gauge_max, true)
  6621. : ''
  6622. );
  6623. }
  6624. };
  6625. ChartInternal.prototype.initGauge = function() {
  6626. var arcs = this.arcs;
  6627. if (this.hasType('gauge')) {
  6628. arcs.append('g').attr('class', CLASS.chartArcsBackground);
  6629. arcs
  6630. .append('text')
  6631. .attr('class', CLASS.chartArcsGaugeUnit)
  6632. .style('text-anchor', 'middle')
  6633. .style('pointer-events', 'none');
  6634. arcs
  6635. .append('text')
  6636. .attr('class', CLASS.chartArcsGaugeMin)
  6637. .style('text-anchor', 'middle')
  6638. .style('pointer-events', 'none');
  6639. arcs
  6640. .append('text')
  6641. .attr('class', CLASS.chartArcsGaugeMax)
  6642. .style('text-anchor', 'middle')
  6643. .style('pointer-events', 'none');
  6644. }
  6645. };
  6646. ChartInternal.prototype.getGaugeLabelHeight = function() {
  6647. return this.config.gauge_label_show ? 20 : 0
  6648. };
  6649. /**
  6650. * Store value into cache
  6651. *
  6652. * @param key
  6653. * @param value
  6654. */
  6655. ChartInternal.prototype.addToCache = function(key, value) {
  6656. this.cache[`$${key}`] = value;
  6657. };
  6658. /**
  6659. * Returns a cached value or undefined
  6660. *
  6661. * @param key
  6662. * @return {*}
  6663. */
  6664. ChartInternal.prototype.getFromCache = function(key) {
  6665. return this.cache[`$${key}`]
  6666. };
  6667. /**
  6668. * Reset cached data
  6669. */
  6670. ChartInternal.prototype.resetCache = function() {
  6671. Object.keys(this.cache)
  6672. .filter(key => /^\$/.test(key))
  6673. .forEach(key => {
  6674. delete this.cache[key];
  6675. });
  6676. };
  6677. // Old API that stores Targets
  6678. ChartInternal.prototype.hasCaches = function(ids) {
  6679. for (var i = 0; i < ids.length; i++) {
  6680. if (!(ids[i] in this.cache)) {
  6681. return false
  6682. }
  6683. }
  6684. return true
  6685. };
  6686. ChartInternal.prototype.addCache = function(id, target) {
  6687. this.cache[id] = this.cloneTarget(target);
  6688. };
  6689. ChartInternal.prototype.getCaches = function(ids) {
  6690. var targets = [],
  6691. i;
  6692. for (i = 0; i < ids.length; i++) {
  6693. if (ids[i] in this.cache) {
  6694. targets.push(this.cloneTarget(this.cache[ids[i]]));
  6695. }
  6696. }
  6697. return targets
  6698. };
  6699. ChartInternal.prototype.categoryName = function(i) {
  6700. var config = this.config;
  6701. return i < config.axis_x_categories.length ? config.axis_x_categories[i] : i
  6702. };
  6703. ChartInternal.prototype.generateTargetClass = function(targetId) {
  6704. return targetId || targetId === 0 ? ('-' + targetId).replace(/\s/g, '-') : ''
  6705. };
  6706. ChartInternal.prototype.generateClass = function(prefix, targetId) {
  6707. return ' ' + prefix + ' ' + prefix + this.generateTargetClass(targetId)
  6708. };
  6709. ChartInternal.prototype.classText = function(d) {
  6710. return this.generateClass(CLASS.text, d.index)
  6711. };
  6712. ChartInternal.prototype.classTexts = function(d) {
  6713. return this.generateClass(CLASS.texts, d.id)
  6714. };
  6715. ChartInternal.prototype.classShape = function(d) {
  6716. return this.generateClass(CLASS.shape, d.index)
  6717. };
  6718. ChartInternal.prototype.classShapes = function(d) {
  6719. return this.generateClass(CLASS.shapes, d.id)
  6720. };
  6721. ChartInternal.prototype.classLine = function(d) {
  6722. return this.classShape(d) + this.generateClass(CLASS.line, d.id)
  6723. };
  6724. ChartInternal.prototype.classLines = function(d) {
  6725. return this.classShapes(d) + this.generateClass(CLASS.lines, d.id)
  6726. };
  6727. ChartInternal.prototype.classCircle = function(d) {
  6728. return this.classShape(d) + this.generateClass(CLASS.circle, d.index)
  6729. };
  6730. ChartInternal.prototype.classCircles = function(d) {
  6731. return this.classShapes(d) + this.generateClass(CLASS.circles, d.id)
  6732. };
  6733. ChartInternal.prototype.classBar = function(d) {
  6734. return this.classShape(d) + this.generateClass(CLASS.bar, d.index)
  6735. };
  6736. ChartInternal.prototype.classBars = function(d) {
  6737. return this.classShapes(d) + this.generateClass(CLASS.bars, d.id)
  6738. };
  6739. ChartInternal.prototype.classArc = function(d) {
  6740. return this.classShape(d.data) + this.generateClass(CLASS.arc, d.data.id)
  6741. };
  6742. ChartInternal.prototype.classArcs = function(d) {
  6743. return this.classShapes(d.data) + this.generateClass(CLASS.arcs, d.data.id)
  6744. };
  6745. ChartInternal.prototype.classArea = function(d) {
  6746. return this.classShape(d) + this.generateClass(CLASS.area, d.id)
  6747. };
  6748. ChartInternal.prototype.classAreas = function(d) {
  6749. return this.classShapes(d) + this.generateClass(CLASS.areas, d.id)
  6750. };
  6751. ChartInternal.prototype.classRegion = function(d, i) {
  6752. return (
  6753. this.generateClass(CLASS.region, i) + ' ' + ('class' in d ? d['class'] : '')
  6754. )
  6755. };
  6756. ChartInternal.prototype.classEvent = function(d) {
  6757. return this.generateClass(CLASS.eventRect, d.index)
  6758. };
  6759. ChartInternal.prototype.classTarget = function(id) {
  6760. var $$ = this;
  6761. var additionalClassSuffix = $$.config.data_classes[id],
  6762. additionalClass = '';
  6763. if (additionalClassSuffix) {
  6764. additionalClass = ' ' + CLASS.target + '-' + additionalClassSuffix;
  6765. }
  6766. return $$.generateClass(CLASS.target, id) + additionalClass
  6767. };
  6768. ChartInternal.prototype.classFocus = function(d) {
  6769. return this.classFocused(d) + this.classDefocused(d)
  6770. };
  6771. ChartInternal.prototype.classFocused = function(d) {
  6772. return ' ' + (this.focusedTargetIds.indexOf(d.id) >= 0 ? CLASS.focused : '')
  6773. };
  6774. ChartInternal.prototype.classDefocused = function(d) {
  6775. return (
  6776. ' ' + (this.defocusedTargetIds.indexOf(d.id) >= 0 ? CLASS.defocused : '')
  6777. )
  6778. };
  6779. ChartInternal.prototype.classChartText = function(d) {
  6780. return CLASS.chartText + this.classTarget(d.id)
  6781. };
  6782. ChartInternal.prototype.classChartLine = function(d) {
  6783. return CLASS.chartLine + this.classTarget(d.id)
  6784. };
  6785. ChartInternal.prototype.classChartBar = function(d) {
  6786. return CLASS.chartBar + this.classTarget(d.id)
  6787. };
  6788. ChartInternal.prototype.classChartArc = function(d) {
  6789. return CLASS.chartArc + this.classTarget(d.data.id)
  6790. };
  6791. ChartInternal.prototype.getTargetSelectorSuffix = function(targetId) {
  6792. const targetClass = this.generateTargetClass(targetId);
  6793. if (window.CSS && window.CSS.escape) {
  6794. return window.CSS.escape(targetClass)
  6795. }
  6796. // fallback on imperfect method for old browsers (does not handles unicode)
  6797. return targetClass.replace(/([?!@#$%^&*()=+,.<>'":;\[\]\/|~`{}\\])/g, '\\$1')
  6798. };
  6799. ChartInternal.prototype.selectorTarget = function(id, prefix) {
  6800. return (prefix || '') + '.' + CLASS.target + this.getTargetSelectorSuffix(id)
  6801. };
  6802. ChartInternal.prototype.selectorTargets = function(ids, prefix) {
  6803. var $$ = this;
  6804. ids = ids || [];
  6805. return ids.length
  6806. ? ids.map(function(id) {
  6807. return $$.selectorTarget(id, prefix)
  6808. })
  6809. : null
  6810. };
  6811. ChartInternal.prototype.selectorLegend = function(id) {
  6812. return '.' + CLASS.legendItem + this.getTargetSelectorSuffix(id)
  6813. };
  6814. ChartInternal.prototype.selectorLegends = function(ids) {
  6815. var $$ = this;
  6816. return ids && ids.length
  6817. ? ids.map(function(id) {
  6818. return $$.selectorLegend(id)
  6819. })
  6820. : null
  6821. };
  6822. ChartInternal.prototype.getClipPath = function(id) {
  6823. return 'url(' + (isIE(9) ? '' : document.URL.split('#')[0]) + '#' + id + ')'
  6824. };
  6825. ChartInternal.prototype.appendClip = function(parent, id) {
  6826. return parent
  6827. .append('clipPath')
  6828. .attr('id', id)
  6829. .append('rect')
  6830. };
  6831. ChartInternal.prototype.getAxisClipX = function(forHorizontal) {
  6832. // axis line width + padding for left
  6833. var left = Math.max(30, this.margin.left);
  6834. return forHorizontal ? -(1 + left) : -(left - 1)
  6835. };
  6836. ChartInternal.prototype.getAxisClipY = function(forHorizontal) {
  6837. return forHorizontal ? -20 : -this.margin.top
  6838. };
  6839. ChartInternal.prototype.getXAxisClipX = function() {
  6840. var $$ = this;
  6841. return $$.getAxisClipX(!$$.config.axis_rotated)
  6842. };
  6843. ChartInternal.prototype.getXAxisClipY = function() {
  6844. var $$ = this;
  6845. return $$.getAxisClipY(!$$.config.axis_rotated)
  6846. };
  6847. ChartInternal.prototype.getYAxisClipX = function() {
  6848. var $$ = this;
  6849. return $$.config.axis_y_inner ? -1 : $$.getAxisClipX($$.config.axis_rotated)
  6850. };
  6851. ChartInternal.prototype.getYAxisClipY = function() {
  6852. var $$ = this;
  6853. return $$.getAxisClipY($$.config.axis_rotated)
  6854. };
  6855. ChartInternal.prototype.getAxisClipWidth = function(forHorizontal) {
  6856. var $$ = this,
  6857. left = Math.max(30, $$.margin.left),
  6858. right = Math.max(30, $$.margin.right);
  6859. // width + axis line width + padding for left/right
  6860. return forHorizontal ? $$.width + 2 + left + right : $$.margin.left + 20
  6861. };
  6862. ChartInternal.prototype.getAxisClipHeight = function(forHorizontal) {
  6863. // less than 20 is not enough to show the axis label 'outer' without legend
  6864. return (
  6865. (forHorizontal ? this.margin.bottom : this.margin.top + this.height) + 20
  6866. )
  6867. };
  6868. ChartInternal.prototype.getXAxisClipWidth = function() {
  6869. var $$ = this;
  6870. return $$.getAxisClipWidth(!$$.config.axis_rotated)
  6871. };
  6872. ChartInternal.prototype.getXAxisClipHeight = function() {
  6873. var $$ = this;
  6874. return $$.getAxisClipHeight(!$$.config.axis_rotated)
  6875. };
  6876. ChartInternal.prototype.getYAxisClipWidth = function() {
  6877. var $$ = this;
  6878. return (
  6879. $$.getAxisClipWidth($$.config.axis_rotated) +
  6880. ($$.config.axis_y_inner ? 20 : 0)
  6881. )
  6882. };
  6883. ChartInternal.prototype.getYAxisClipHeight = function() {
  6884. var $$ = this;
  6885. return $$.getAxisClipHeight($$.config.axis_rotated)
  6886. };
  6887. ChartInternal.prototype.generateColor = function() {
  6888. var $$ = this,
  6889. config = $$.config,
  6890. d3 = $$.d3,
  6891. colors = config.data_colors,
  6892. pattern = notEmpty(config.color_pattern)
  6893. ? config.color_pattern
  6894. : d3.schemeCategory10,
  6895. callback = config.data_color,
  6896. ids = [];
  6897. return function(d) {
  6898. var id = d.id || (d.data && d.data.id) || d,
  6899. color;
  6900. // if callback function is provided
  6901. if (colors[id] instanceof Function) {
  6902. color = colors[id](d);
  6903. }
  6904. // if specified, choose that color
  6905. else if (colors[id]) {
  6906. color = colors[id];
  6907. }
  6908. // if not specified, choose from pattern
  6909. else {
  6910. if (ids.indexOf(id) < 0) {
  6911. ids.push(id);
  6912. }
  6913. color = pattern[ids.indexOf(id) % pattern.length];
  6914. colors[id] = color;
  6915. }
  6916. return callback instanceof Function ? callback(color, d) : color
  6917. }
  6918. };
  6919. ChartInternal.prototype.generateLevelColor = function() {
  6920. var $$ = this,
  6921. config = $$.config,
  6922. colors = config.color_pattern,
  6923. threshold = config.color_threshold,
  6924. asValue = threshold.unit === 'value',
  6925. values =
  6926. threshold.values && threshold.values.length ? threshold.values : [],
  6927. max = threshold.max || 100;
  6928. return notEmpty(threshold) && notEmpty(colors)
  6929. ? function(value) {
  6930. var i,
  6931. v,
  6932. color = colors[colors.length - 1];
  6933. for (i = 0; i < values.length; i++) {
  6934. v = asValue ? value : (value * 100) / max;
  6935. if (v < values[i]) {
  6936. color = colors[i];
  6937. break
  6938. }
  6939. }
  6940. return color
  6941. }
  6942. : null
  6943. };
  6944. ChartInternal.prototype.getDefaultConfig = function() {
  6945. var config = {
  6946. bindto: '#chart',
  6947. svg_classname: undefined,
  6948. size_width: undefined,
  6949. size_height: undefined,
  6950. padding_left: undefined,
  6951. padding_right: undefined,
  6952. padding_top: undefined,
  6953. padding_bottom: undefined,
  6954. resize_auto: true,
  6955. zoom_enabled: false,
  6956. zoom_initialRange: undefined,
  6957. zoom_type: 'scroll',
  6958. zoom_disableDefaultBehavior: false,
  6959. zoom_privileged: false,
  6960. zoom_rescale: false,
  6961. zoom_onzoom: function() {},
  6962. zoom_onzoomstart: function() {},
  6963. zoom_onzoomend: function() {},
  6964. zoom_x_min: undefined,
  6965. zoom_x_max: undefined,
  6966. interaction_brighten: true,
  6967. interaction_enabled: true,
  6968. onmouseover: function() {},
  6969. onmouseout: function() {},
  6970. onresize: function() {},
  6971. onresized: function() {},
  6972. oninit: function() {},
  6973. onrendered: function() {},
  6974. transition_duration: 350,
  6975. data_epochs: 'epochs',
  6976. data_x: undefined,
  6977. data_xs: {},
  6978. data_xFormat: '%Y-%m-%d',
  6979. data_xLocaltime: true,
  6980. data_xSort: true,
  6981. data_idConverter: function(id) {
  6982. return id
  6983. },
  6984. data_names: {},
  6985. data_classes: {},
  6986. data_groups: [],
  6987. data_axes: {},
  6988. data_type: undefined,
  6989. data_types: {},
  6990. data_labels: {},
  6991. data_order: 'desc',
  6992. data_regions: {},
  6993. data_color: undefined,
  6994. data_colors: {},
  6995. data_hide: false,
  6996. data_filter: undefined,
  6997. data_selection_enabled: false,
  6998. data_selection_grouped: false,
  6999. data_selection_isselectable: function() {
  7000. return true
  7001. },
  7002. data_selection_multiple: true,
  7003. data_selection_draggable: false,
  7004. data_stack_normalize: false,
  7005. data_onclick: function() {},
  7006. data_onmouseover: function() {},
  7007. data_onmouseout: function() {},
  7008. data_onselected: function() {},
  7009. data_onunselected: function() {},
  7010. data_url: undefined,
  7011. data_headers: undefined,
  7012. data_json: undefined,
  7013. data_rows: undefined,
  7014. data_columns: undefined,
  7015. data_mimeType: undefined,
  7016. data_keys: undefined,
  7017. // configuration for no plot-able data supplied.
  7018. data_empty_label_text: '',
  7019. // subchart
  7020. subchart_show: false,
  7021. subchart_size_height: 60,
  7022. subchart_axis_x_show: true,
  7023. subchart_onbrush: function() {},
  7024. // color
  7025. color_pattern: [],
  7026. color_threshold: {},
  7027. // legend
  7028. legend_show: true,
  7029. legend_hide: false,
  7030. legend_position: 'bottom',
  7031. legend_inset_anchor: 'top-left',
  7032. legend_inset_x: 10,
  7033. legend_inset_y: 0,
  7034. legend_inset_step: undefined,
  7035. legend_item_onclick: undefined,
  7036. legend_item_onmouseover: undefined,
  7037. legend_item_onmouseout: undefined,
  7038. legend_equally: false,
  7039. legend_padding: 0,
  7040. legend_item_tile_width: 10,
  7041. legend_item_tile_height: 10,
  7042. // axis
  7043. axis_rotated: false,
  7044. axis_x_show: true,
  7045. axis_x_type: 'indexed',
  7046. axis_x_localtime: true,
  7047. axis_x_categories: [],
  7048. axis_x_tick_centered: false,
  7049. axis_x_tick_format: undefined,
  7050. axis_x_tick_culling: {},
  7051. axis_x_tick_culling_max: 10,
  7052. axis_x_tick_count: undefined,
  7053. axis_x_tick_fit: true,
  7054. axis_x_tick_values: null,
  7055. axis_x_tick_rotate: 0,
  7056. axis_x_tick_outer: true,
  7057. axis_x_tick_multiline: true,
  7058. axis_x_tick_multilineMax: 0,
  7059. axis_x_tick_width: null,
  7060. axis_x_max: undefined,
  7061. axis_x_min: undefined,
  7062. axis_x_padding: {},
  7063. axis_x_height: undefined,
  7064. axis_x_selection: undefined,
  7065. axis_x_label: {},
  7066. axis_x_inner: undefined,
  7067. axis_y_show: true,
  7068. axis_y_type: 'linear',
  7069. axis_y_max: undefined,
  7070. axis_y_min: undefined,
  7071. axis_y_inverted: false,
  7072. axis_y_center: undefined,
  7073. axis_y_inner: undefined,
  7074. axis_y_label: {},
  7075. axis_y_tick_format: undefined,
  7076. axis_y_tick_outer: true,
  7077. axis_y_tick_values: null,
  7078. axis_y_tick_rotate: 0,
  7079. axis_y_tick_count: undefined,
  7080. axis_y_tick_time_type: undefined,
  7081. axis_y_tick_time_interval: undefined,
  7082. axis_y_padding: {},
  7083. axis_y_default: undefined,
  7084. axis_y2_show: false,
  7085. axis_y2_type: 'linear',
  7086. axis_y2_max: undefined,
  7087. axis_y2_min: undefined,
  7088. axis_y2_inverted: false,
  7089. axis_y2_center: undefined,
  7090. axis_y2_inner: undefined,
  7091. axis_y2_label: {},
  7092. axis_y2_tick_format: undefined,
  7093. axis_y2_tick_outer: true,
  7094. axis_y2_tick_values: null,
  7095. axis_y2_tick_count: undefined,
  7096. axis_y2_padding: {},
  7097. axis_y2_default: undefined,
  7098. // grid
  7099. grid_x_show: false,
  7100. grid_x_type: 'tick',
  7101. grid_x_lines: [],
  7102. grid_y_show: false,
  7103. // not used
  7104. // grid_y_type: 'tick',
  7105. grid_y_lines: [],
  7106. grid_y_ticks: 10,
  7107. grid_focus_show: true,
  7108. grid_lines_front: true,
  7109. // point - point of each data
  7110. point_show: true,
  7111. point_r: 2.5,
  7112. point_sensitivity: 10,
  7113. point_focus_expand_enabled: true,
  7114. point_focus_expand_r: undefined,
  7115. point_select_r: undefined,
  7116. // line
  7117. line_connectNull: false,
  7118. line_step_type: 'step',
  7119. // bar
  7120. bar_width: undefined,
  7121. bar_width_ratio: 0.6,
  7122. bar_width_max: undefined,
  7123. bar_zerobased: true,
  7124. bar_space: 0,
  7125. // area
  7126. area_zerobased: true,
  7127. area_above: false,
  7128. // pie
  7129. pie_label_show: true,
  7130. pie_label_format: undefined,
  7131. pie_label_threshold: 0.05,
  7132. pie_label_ratio: undefined,
  7133. pie_expand: {},
  7134. pie_expand_duration: 50,
  7135. pie_padAngle: 0,
  7136. // gauge
  7137. gauge_fullCircle: false,
  7138. gauge_label_show: true,
  7139. gauge_labelLine_show: true,
  7140. gauge_label_format: undefined,
  7141. gauge_min: 0,
  7142. gauge_max: 100,
  7143. gauge_startingAngle: (-1 * Math.PI) / 2,
  7144. gauge_label_extents: undefined,
  7145. gauge_units: undefined,
  7146. gauge_width: undefined,
  7147. gauge_arcs_minWidth: 5,
  7148. gauge_expand: {},
  7149. gauge_expand_duration: 50,
  7150. // donut
  7151. donut_label_show: true,
  7152. donut_label_format: undefined,
  7153. donut_label_threshold: 0.05,
  7154. donut_label_ratio: undefined,
  7155. donut_width: undefined,
  7156. donut_title: '',
  7157. donut_expand: {},
  7158. donut_expand_duration: 50,
  7159. donut_padAngle: 0,
  7160. // spline
  7161. spline_interpolation_type: 'cardinal',
  7162. // stanford
  7163. stanford_lines: [],
  7164. stanford_regions: [],
  7165. stanford_texts: [],
  7166. stanford_scaleMin: undefined,
  7167. stanford_scaleMax: undefined,
  7168. stanford_scaleWidth: undefined,
  7169. stanford_scaleFormat: undefined,
  7170. stanford_scaleValues: undefined,
  7171. stanford_colors: undefined,
  7172. stanford_padding: {
  7173. top: 0,
  7174. right: 0,
  7175. bottom: 0,
  7176. left: 0
  7177. },
  7178. // region - region to change style
  7179. regions: [],
  7180. // tooltip - show when mouseover on each data
  7181. tooltip_show: true,
  7182. tooltip_grouped: true,
  7183. tooltip_order: undefined,
  7184. tooltip_format_title: undefined,
  7185. tooltip_format_name: undefined,
  7186. tooltip_format_value: undefined,
  7187. tooltip_horizontal: undefined,
  7188. tooltip_position: undefined,
  7189. tooltip_contents: function(
  7190. d,
  7191. defaultTitleFormat,
  7192. defaultValueFormat,
  7193. color
  7194. ) {
  7195. return this.getTooltipContent
  7196. ? this.getTooltipContent(
  7197. d,
  7198. defaultTitleFormat,
  7199. defaultValueFormat,
  7200. color
  7201. )
  7202. : ''
  7203. },
  7204. tooltip_init_show: false,
  7205. tooltip_init_x: 0,
  7206. tooltip_init_position: { top: '0px', left: '50px' },
  7207. tooltip_onshow: function() {},
  7208. tooltip_onhide: function() {},
  7209. // title
  7210. title_text: undefined,
  7211. title_padding: {
  7212. top: 0,
  7213. right: 0,
  7214. bottom: 0,
  7215. left: 0
  7216. },
  7217. title_position: 'top-center'
  7218. };
  7219. Object.keys(this.additionalConfig).forEach(function(key) {
  7220. config[key] = this.additionalConfig[key];
  7221. }, this);
  7222. return config
  7223. };
  7224. ChartInternal.prototype.additionalConfig = {};
  7225. ChartInternal.prototype.loadConfig = function(config) {
  7226. var this_config = this.config,
  7227. target,
  7228. keys,
  7229. read;
  7230. function find() {
  7231. var key = keys.shift();
  7232. // console.log("key =>", key, ", target =>", target);
  7233. if (key && target && typeof target === 'object' && key in target) {
  7234. target = target[key];
  7235. return find()
  7236. } else if (!key) {
  7237. return target
  7238. } else {
  7239. return undefined
  7240. }
  7241. }
  7242. Object.keys(this_config).forEach(function(key) {
  7243. target = config;
  7244. keys = key.split('_');
  7245. read = find();
  7246. // console.log("CONFIG : ", key, read);
  7247. if (isDefined(read)) {
  7248. this_config[key] = read;
  7249. }
  7250. });
  7251. };
  7252. ChartInternal.prototype.convertUrlToData = function(
  7253. url,
  7254. mimeType,
  7255. headers,
  7256. keys,
  7257. done
  7258. ) {
  7259. var $$ = this,
  7260. type = mimeType ? mimeType : 'csv',
  7261. f,
  7262. converter;
  7263. if (type === 'json') {
  7264. f = $$.d3.json;
  7265. converter = $$.convertJsonToData;
  7266. } else if (type === 'tsv') {
  7267. f = $$.d3.tsv;
  7268. converter = $$.convertXsvToData;
  7269. } else {
  7270. f = $$.d3.csv;
  7271. converter = $$.convertXsvToData;
  7272. }
  7273. f(url, headers)
  7274. .then(function(data) {
  7275. done.call($$, converter.call($$, data, keys));
  7276. })
  7277. .catch(function(error) {
  7278. throw error
  7279. });
  7280. };
  7281. ChartInternal.prototype.convertXsvToData = function(xsv) {
  7282. var keys = xsv.columns,
  7283. rows = xsv;
  7284. if (rows.length === 0) {
  7285. return {
  7286. keys,
  7287. rows: [keys.reduce((row, key) => Object.assign(row, { [key]: null }), {})]
  7288. }
  7289. } else {
  7290. // [].concat() is to convert result into a plain array otherwise
  7291. // test is not happy because rows have properties.
  7292. return { keys, rows: [].concat(xsv) }
  7293. }
  7294. };
  7295. ChartInternal.prototype.convertJsonToData = function(json, keys) {
  7296. var $$ = this,
  7297. new_rows = [],
  7298. targetKeys,
  7299. data;
  7300. if (keys) {
  7301. // when keys specified, json would be an array that includes objects
  7302. if (keys.x) {
  7303. targetKeys = keys.value.concat(keys.x);
  7304. $$.config.data_x = keys.x;
  7305. } else {
  7306. targetKeys = keys.value;
  7307. }
  7308. new_rows.push(targetKeys);
  7309. json.forEach(function(o) {
  7310. var new_row = [];
  7311. targetKeys.forEach(function(key) {
  7312. // convert undefined to null because undefined data will be removed in convertDataToTargets()
  7313. var v = $$.findValueInJson(o, key);
  7314. if (isUndefined(v)) {
  7315. v = null;
  7316. }
  7317. new_row.push(v);
  7318. });
  7319. new_rows.push(new_row);
  7320. });
  7321. data = $$.convertRowsToData(new_rows);
  7322. } else {
  7323. Object.keys(json).forEach(function(key) {
  7324. new_rows.push([key].concat(json[key]));
  7325. });
  7326. data = $$.convertColumnsToData(new_rows);
  7327. }
  7328. return data
  7329. };
  7330. /**
  7331. * Finds value from the given nested object by the given path.
  7332. * If it's not found, then this returns undefined.
  7333. * @param {Object} object the object
  7334. * @param {string} path the path
  7335. */
  7336. ChartInternal.prototype.findValueInJson = function(object, path) {
  7337. if (path in object) {
  7338. // If object has a key that contains . or [], return the key's value
  7339. // instead of searching for an inner object.
  7340. // See https://github.com/c3js/c3/issues/1691 for details.
  7341. return object[path]
  7342. }
  7343. path = path.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties (replace [] with .)
  7344. path = path.replace(/^\./, ''); // strip a leading dot
  7345. var pathArray = path.split('.');
  7346. for (var i = 0; i < pathArray.length; ++i) {
  7347. var k = pathArray[i];
  7348. if (k in object) {
  7349. object = object[k];
  7350. } else {
  7351. return
  7352. }
  7353. }
  7354. return object
  7355. };
  7356. /**
  7357. * Converts the rows to normalized data.
  7358. * @param {any[][]} rows The row data
  7359. * @return {Object}
  7360. */
  7361. ChartInternal.prototype.convertRowsToData = rows => {
  7362. const newRows = [];
  7363. const keys = rows[0];
  7364. for (let i = 1; i < rows.length; i++) {
  7365. const newRow = {};
  7366. for (let j = 0; j < rows[i].length; j++) {
  7367. if (isUndefined(rows[i][j])) {
  7368. throw new Error(
  7369. 'Source data is missing a component at (' + i + ',' + j + ')!'
  7370. )
  7371. }
  7372. newRow[keys[j]] = rows[i][j];
  7373. }
  7374. newRows.push(newRow);
  7375. }
  7376. return { keys, rows: newRows }
  7377. };
  7378. /**
  7379. * Converts the columns to normalized data.
  7380. * @param {any[][]} columns The column data
  7381. * @return {Object}
  7382. */
  7383. ChartInternal.prototype.convertColumnsToData = columns => {
  7384. const newRows = [];
  7385. const keys = [];
  7386. for (let i = 0; i < columns.length; i++) {
  7387. const key = columns[i][0];
  7388. for (let j = 1; j < columns[i].length; j++) {
  7389. if (isUndefined(newRows[j - 1])) {
  7390. newRows[j - 1] = {};
  7391. }
  7392. if (isUndefined(columns[i][j])) {
  7393. throw new Error(
  7394. 'Source data is missing a component at (' + i + ',' + j + ')!'
  7395. )
  7396. }
  7397. newRows[j - 1][key] = columns[i][j];
  7398. }
  7399. keys.push(key);
  7400. }
  7401. return { keys, rows: newRows }
  7402. };
  7403. /**
  7404. * Converts the data format into the target format.
  7405. * @param {!Object} data
  7406. * @param {!Array} data.keys Ordered list of target IDs.
  7407. * @param {!Array} data.rows Rows of data to convert.
  7408. * @param {boolean} appendXs True to append to $$.data.xs, False to replace.
  7409. * @return {!Array}
  7410. */
  7411. ChartInternal.prototype.convertDataToTargets = function(data, appendXs) {
  7412. var $$ = this,
  7413. config = $$.config,
  7414. targets,
  7415. ids,
  7416. xs,
  7417. keys,
  7418. epochs;
  7419. // handles format where keys are not orderly provided
  7420. if (isArray(data)) {
  7421. keys = Object.keys(data[0]);
  7422. } else {
  7423. keys = data.keys;
  7424. data = data.rows;
  7425. }
  7426. xs = keys.filter($$.isX, $$);
  7427. if (!$$.isStanfordGraphType()) {
  7428. ids = keys.filter($$.isNotX, $$);
  7429. } else {
  7430. epochs = keys.filter($$.isEpochs, $$);
  7431. ids = keys.filter($$.isNotXAndNotEpochs, $$);
  7432. if (xs.length !== 1 || epochs.length !== 1 || ids.length !== 1) {
  7433. throw new Error(
  7434. "You must define the 'x' key name and the 'epochs' for Stanford Diagrams"
  7435. )
  7436. }
  7437. }
  7438. // save x for update data by load when custom x and c3.x API
  7439. ids.forEach(function(id) {
  7440. var xKey = $$.getXKey(id);
  7441. if ($$.isCustomX() || $$.isTimeSeries()) {
  7442. // if included in input data
  7443. if (xs.indexOf(xKey) >= 0) {
  7444. $$.data.xs[id] = (appendXs && $$.data.xs[id]
  7445. ? $$.data.xs[id]
  7446. : []
  7447. ).concat(
  7448. data
  7449. .map(function(d) {
  7450. return d[xKey]
  7451. })
  7452. .filter(isValue)
  7453. .map(function(rawX, i) {
  7454. return $$.generateTargetX(rawX, id, i)
  7455. })
  7456. );
  7457. }
  7458. // if not included in input data, find from preloaded data of other id's x
  7459. else if (config.data_x) {
  7460. $$.data.xs[id] = $$.getOtherTargetXs();
  7461. }
  7462. // if not included in input data, find from preloaded data
  7463. else if (notEmpty(config.data_xs)) {
  7464. $$.data.xs[id] = $$.getXValuesOfXKey(xKey, $$.data.targets);
  7465. }
  7466. // MEMO: if no x included, use same x of current will be used
  7467. } else {
  7468. $$.data.xs[id] = data.map(function(d, i) {
  7469. return i
  7470. });
  7471. }
  7472. });
  7473. // check x is defined
  7474. ids.forEach(function(id) {
  7475. if (!$$.data.xs[id]) {
  7476. throw new Error('x is not defined for id = "' + id + '".')
  7477. }
  7478. });
  7479. // convert to target
  7480. targets = ids.map(function(id, index) {
  7481. var convertedId = config.data_idConverter(id);
  7482. return {
  7483. id: convertedId,
  7484. id_org: id,
  7485. values: data
  7486. .map(function(d, i) {
  7487. var xKey = $$.getXKey(id),
  7488. rawX = d[xKey],
  7489. value = d[id] !== null && !isNaN(d[id]) ? +d[id] : null,
  7490. x,
  7491. returnData;
  7492. // use x as categories if custom x and categorized
  7493. if ($$.isCustomX() && $$.isCategorized() && !isUndefined(rawX)) {
  7494. if (index === 0 && i === 0) {
  7495. config.axis_x_categories = [];
  7496. }
  7497. x = config.axis_x_categories.indexOf(rawX);
  7498. if (x === -1) {
  7499. x = config.axis_x_categories.length;
  7500. config.axis_x_categories.push(rawX);
  7501. }
  7502. } else {
  7503. x = $$.generateTargetX(rawX, id, i);
  7504. }
  7505. // mark as x = undefined if value is undefined and filter to remove after mapped
  7506. if (isUndefined(d[id]) || $$.data.xs[id].length <= i) {
  7507. x = undefined;
  7508. }
  7509. returnData = { x: x, value: value, id: convertedId };
  7510. if ($$.isStanfordGraphType()) {
  7511. returnData.epochs = d[epochs];
  7512. }
  7513. return returnData
  7514. })
  7515. .filter(function(v) {
  7516. return isDefined(v.x)
  7517. })
  7518. }
  7519. });
  7520. // finish targets
  7521. targets.forEach(function(t) {
  7522. var i;
  7523. // sort values by its x
  7524. if (config.data_xSort) {
  7525. t.values = t.values.sort(function(v1, v2) {
  7526. var x1 = v1.x || v1.x === 0 ? v1.x : Infinity,
  7527. x2 = v2.x || v2.x === 0 ? v2.x : Infinity;
  7528. return x1 - x2
  7529. });
  7530. }
  7531. // indexing each value
  7532. i = 0;
  7533. t.values.forEach(function(v) {
  7534. v.index = i++;
  7535. });
  7536. // this needs to be sorted because its index and value.index is identical
  7537. $$.data.xs[t.id].sort(function(v1, v2) {
  7538. return v1 - v2
  7539. });
  7540. });
  7541. // cache information about values
  7542. $$.hasNegativeValue = $$.hasNegativeValueInTargets(targets);
  7543. $$.hasPositiveValue = $$.hasPositiveValueInTargets(targets);
  7544. // set target types
  7545. if (config.data_type) {
  7546. $$.setTargetType(
  7547. $$.mapToIds(targets).filter(function(id) {
  7548. return !(id in config.data_types)
  7549. }),
  7550. config.data_type
  7551. );
  7552. }
  7553. // cache as original id keyed
  7554. targets.forEach(function(d) {
  7555. $$.addCache(d.id_org, d);
  7556. });
  7557. return targets
  7558. };
  7559. ChartInternal.prototype.isEpochs = function(key) {
  7560. var $$ = this,
  7561. config = $$.config;
  7562. return config.data_epochs && key === config.data_epochs
  7563. };
  7564. ChartInternal.prototype.isX = function(key) {
  7565. var $$ = this,
  7566. config = $$.config;
  7567. return (
  7568. (config.data_x && key === config.data_x) ||
  7569. (notEmpty(config.data_xs) && hasValue(config.data_xs, key))
  7570. )
  7571. };
  7572. ChartInternal.prototype.isNotX = function(key) {
  7573. return !this.isX(key)
  7574. };
  7575. ChartInternal.prototype.isNotXAndNotEpochs = function(key) {
  7576. return !this.isX(key) && !this.isEpochs(key)
  7577. };
  7578. /**
  7579. * Returns whether the normalized stack option is enabled or not.
  7580. *
  7581. * To be enabled it must also have data.groups defined.
  7582. *
  7583. * @return {boolean}
  7584. */
  7585. ChartInternal.prototype.isStackNormalized = function() {
  7586. return this.config.data_stack_normalize && this.config.data_groups.length > 0
  7587. };
  7588. /**
  7589. * Returns whether the axis is normalized or not.
  7590. *
  7591. * An axis is normalized as long as one of its associated target
  7592. * is normalized.
  7593. *
  7594. * @param axisId Axis ID (y or y2)
  7595. * @return {Boolean}
  7596. */
  7597. ChartInternal.prototype.isAxisNormalized = function(axisId) {
  7598. const $$ = this;
  7599. if (!$$.isStackNormalized()) {
  7600. // shortcut
  7601. return false
  7602. }
  7603. return $$.data.targets
  7604. .filter(target => $$.axis.getId(target.id) === axisId)
  7605. .some(target => $$.isTargetNormalized(target.id))
  7606. };
  7607. /**
  7608. * Returns whether the values for this target ID is normalized or not.
  7609. *
  7610. * To be normalized the option needs to be enabled and target needs
  7611. * to be defined in `data.groups`.
  7612. *
  7613. * @param targetId ID of the target
  7614. * @return {Boolean} True if the target is normalized, false otherwise.
  7615. */
  7616. ChartInternal.prototype.isTargetNormalized = function(targetId) {
  7617. const $$ = this;
  7618. return (
  7619. $$.isStackNormalized() &&
  7620. $$.config.data_groups.some(group => group.includes(targetId))
  7621. )
  7622. };
  7623. ChartInternal.prototype.getXKey = function(id) {
  7624. var $$ = this,
  7625. config = $$.config;
  7626. return config.data_x
  7627. ? config.data_x
  7628. : notEmpty(config.data_xs)
  7629. ? config.data_xs[id]
  7630. : null
  7631. };
  7632. /**
  7633. * Get sum of visible data per index for given axis.
  7634. *
  7635. * Expect axisId to be either 'y' or 'y2'.
  7636. *
  7637. * @private
  7638. * @param axisId Compute sum for data associated to given axis.
  7639. * @return {Array}
  7640. */
  7641. ChartInternal.prototype.getTotalPerIndex = function(axisId) {
  7642. const $$ = this;
  7643. if (!$$.isStackNormalized()) {
  7644. return null
  7645. }
  7646. const cached = $$.getFromCache('getTotalPerIndex');
  7647. if (cached !== undefined) {
  7648. return cached[axisId]
  7649. }
  7650. const sum = { y: [], y2: [] };
  7651. $$.data.targets
  7652. // keep only target that are normalized
  7653. .filter(target => $$.isTargetNormalized(target.id))
  7654. // keep only target that are visible
  7655. .filter(target => $$.isTargetToShow(target.id))
  7656. // compute sum per axis
  7657. .forEach(target => {
  7658. const sumByAxis = sum[$$.axis.getId(target.id)];
  7659. target.values.forEach((v, i) => {
  7660. if (!sumByAxis[i]) {
  7661. sumByAxis[i] = 0;
  7662. }
  7663. sumByAxis[i] += isNumber(v.value) ? v.value : 0;
  7664. });
  7665. });
  7666. $$.addToCache('getTotalPerIndex', sum);
  7667. return sum[axisId]
  7668. };
  7669. /**
  7670. * Get sum of visible data.
  7671. *
  7672. * Should be used for normalised data only since all values
  7673. * are expected to be positive.
  7674. *
  7675. * @private
  7676. * @return {Number}
  7677. */
  7678. ChartInternal.prototype.getTotalDataSum = function() {
  7679. const $$ = this;
  7680. const cached = $$.getFromCache('getTotalDataSum');
  7681. if (cached !== undefined) {
  7682. return cached
  7683. }
  7684. const totalDataSum = flattenArray(
  7685. $$.data.targets
  7686. .filter(target => $$.isTargetToShow(target.id))
  7687. .map(target => target.values)
  7688. )
  7689. .map(d => d.value)
  7690. .reduce((p, c) => p + c, 0);
  7691. $$.addToCache('getTotalDataSum', totalDataSum);
  7692. return totalDataSum
  7693. };
  7694. ChartInternal.prototype.getXValuesOfXKey = function(key, targets) {
  7695. var $$ = this,
  7696. xValues,
  7697. ids = targets && notEmpty(targets) ? $$.mapToIds(targets) : [];
  7698. ids.forEach(function(id) {
  7699. if ($$.getXKey(id) === key) {
  7700. xValues = $$.data.xs[id];
  7701. }
  7702. });
  7703. return xValues
  7704. };
  7705. ChartInternal.prototype.getXValue = function(id, i) {
  7706. var $$ = this;
  7707. return id in $$.data.xs && $$.data.xs[id] && isValue($$.data.xs[id][i])
  7708. ? $$.data.xs[id][i]
  7709. : i
  7710. };
  7711. ChartInternal.prototype.getOtherTargetXs = function() {
  7712. var $$ = this,
  7713. idsForX = Object.keys($$.data.xs);
  7714. return idsForX.length ? $$.data.xs[idsForX[0]] : null
  7715. };
  7716. ChartInternal.prototype.getOtherTargetX = function(index) {
  7717. var xs = this.getOtherTargetXs();
  7718. return xs && index < xs.length ? xs[index] : null
  7719. };
  7720. ChartInternal.prototype.addXs = function(xs) {
  7721. var $$ = this;
  7722. Object.keys(xs).forEach(function(id) {
  7723. $$.config.data_xs[id] = xs[id];
  7724. });
  7725. };
  7726. ChartInternal.prototype.addName = function(data) {
  7727. var $$ = this,
  7728. name;
  7729. if (data) {
  7730. name = $$.config.data_names[data.id];
  7731. data.name = name !== undefined ? name : data.id;
  7732. }
  7733. return data
  7734. };
  7735. ChartInternal.prototype.getValueOnIndex = function(values, index) {
  7736. var valueOnIndex = values.filter(function(v) {
  7737. return v.index === index
  7738. });
  7739. return valueOnIndex.length ? valueOnIndex[0] : null
  7740. };
  7741. ChartInternal.prototype.updateTargetX = function(targets, x) {
  7742. var $$ = this;
  7743. targets.forEach(function(t) {
  7744. t.values.forEach(function(v, i) {
  7745. v.x = $$.generateTargetX(x[i], t.id, i);
  7746. });
  7747. $$.data.xs[t.id] = x;
  7748. });
  7749. };
  7750. ChartInternal.prototype.updateTargetXs = function(targets, xs) {
  7751. var $$ = this;
  7752. targets.forEach(function(t) {
  7753. if (xs[t.id]) {
  7754. $$.updateTargetX([t], xs[t.id]);
  7755. }
  7756. });
  7757. };
  7758. ChartInternal.prototype.generateTargetX = function(rawX, id, index) {
  7759. var $$ = this,
  7760. x;
  7761. if ($$.isTimeSeries()) {
  7762. x = rawX ? $$.parseDate(rawX) : $$.parseDate($$.getXValue(id, index));
  7763. } else if ($$.isCustomX() && !$$.isCategorized()) {
  7764. x = isValue(rawX) ? +rawX : $$.getXValue(id, index);
  7765. } else {
  7766. x = index;
  7767. }
  7768. return x
  7769. };
  7770. ChartInternal.prototype.cloneTarget = function(target) {
  7771. return {
  7772. id: target.id,
  7773. id_org: target.id_org,
  7774. values: target.values.map(function(d) {
  7775. return {
  7776. x: d.x,
  7777. value: d.value,
  7778. id: d.id
  7779. }
  7780. })
  7781. }
  7782. };
  7783. ChartInternal.prototype.getMaxDataCount = function() {
  7784. var $$ = this;
  7785. return $$.d3.max($$.data.targets, function(t) {
  7786. return t.values.length
  7787. })
  7788. };
  7789. ChartInternal.prototype.mapToIds = function(targets) {
  7790. return targets.map(function(d) {
  7791. return d.id
  7792. })
  7793. };
  7794. ChartInternal.prototype.mapToTargetIds = function(ids) {
  7795. var $$ = this;
  7796. return ids ? [].concat(ids) : $$.mapToIds($$.data.targets)
  7797. };
  7798. ChartInternal.prototype.hasTarget = function(targets, id) {
  7799. var ids = this.mapToIds(targets),
  7800. i;
  7801. for (i = 0; i < ids.length; i++) {
  7802. if (ids[i] === id) {
  7803. return true
  7804. }
  7805. }
  7806. return false
  7807. };
  7808. ChartInternal.prototype.isTargetToShow = function(targetId) {
  7809. return this.hiddenTargetIds.indexOf(targetId) < 0
  7810. };
  7811. ChartInternal.prototype.isLegendToShow = function(targetId) {
  7812. return this.hiddenLegendIds.indexOf(targetId) < 0
  7813. };
  7814. /**
  7815. * Returns only visible targets.
  7816. *
  7817. * This is the same as calling {@link filterTargetsToShow} on $$.data.targets.
  7818. *
  7819. * @return {Array}
  7820. */
  7821. ChartInternal.prototype.getTargetsToShow = function() {
  7822. const $$ = this;
  7823. return $$.filterTargetsToShow($$.data.targets)
  7824. };
  7825. ChartInternal.prototype.filterTargetsToShow = function(targets) {
  7826. var $$ = this;
  7827. return targets.filter(function(t) {
  7828. return $$.isTargetToShow(t.id)
  7829. })
  7830. };
  7831. /**
  7832. * @return {Array} Returns all the targets attached to the chart, visible or not
  7833. */
  7834. ChartInternal.prototype.getTargets = function() {
  7835. const $$ = this;
  7836. return $$.data.targets
  7837. };
  7838. ChartInternal.prototype.mapTargetsToUniqueXs = function(targets) {
  7839. var $$ = this;
  7840. var xs = $$.d3
  7841. .set(
  7842. $$.d3.merge(
  7843. targets.map(function(t) {
  7844. return t.values.map(function(v) {
  7845. return +v.x
  7846. })
  7847. })
  7848. )
  7849. )
  7850. .values();
  7851. xs = $$.isTimeSeries()
  7852. ? xs.map(function(x) {
  7853. return new Date(+x)
  7854. })
  7855. : xs.map(function(x) {
  7856. return +x
  7857. });
  7858. return xs.sort(function(a, b) {
  7859. return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN
  7860. })
  7861. };
  7862. ChartInternal.prototype.addHiddenTargetIds = function(targetIds) {
  7863. targetIds = targetIds instanceof Array ? targetIds : new Array(targetIds);
  7864. for (var i = 0; i < targetIds.length; i++) {
  7865. if (this.hiddenTargetIds.indexOf(targetIds[i]) < 0) {
  7866. this.hiddenTargetIds = this.hiddenTargetIds.concat(targetIds[i]);
  7867. }
  7868. }
  7869. this.resetCache();
  7870. };
  7871. ChartInternal.prototype.removeHiddenTargetIds = function(targetIds) {
  7872. this.hiddenTargetIds = this.hiddenTargetIds.filter(function(id) {
  7873. return targetIds.indexOf(id) < 0
  7874. });
  7875. this.resetCache();
  7876. };
  7877. ChartInternal.prototype.addHiddenLegendIds = function(targetIds) {
  7878. targetIds = targetIds instanceof Array ? targetIds : new Array(targetIds);
  7879. for (var i = 0; i < targetIds.length; i++) {
  7880. if (this.hiddenLegendIds.indexOf(targetIds[i]) < 0) {
  7881. this.hiddenLegendIds = this.hiddenLegendIds.concat(targetIds[i]);
  7882. }
  7883. }
  7884. };
  7885. ChartInternal.prototype.removeHiddenLegendIds = function(targetIds) {
  7886. this.hiddenLegendIds = this.hiddenLegendIds.filter(function(id) {
  7887. return targetIds.indexOf(id) < 0
  7888. });
  7889. };
  7890. ChartInternal.prototype.getValuesAsIdKeyed = function(targets) {
  7891. var ys = {};
  7892. targets.forEach(function(t) {
  7893. ys[t.id] = [];
  7894. t.values.forEach(function(v) {
  7895. ys[t.id].push(v.value);
  7896. });
  7897. });
  7898. return ys
  7899. };
  7900. ChartInternal.prototype.checkValueInTargets = function(targets, checker) {
  7901. var ids = Object.keys(targets),
  7902. i,
  7903. j,
  7904. values;
  7905. for (i = 0; i < ids.length; i++) {
  7906. values = targets[ids[i]].values;
  7907. for (j = 0; j < values.length; j++) {
  7908. if (checker(values[j].value)) {
  7909. return true
  7910. }
  7911. }
  7912. }
  7913. return false
  7914. };
  7915. ChartInternal.prototype.hasNegativeValueInTargets = function(targets) {
  7916. return this.checkValueInTargets(targets, function(v) {
  7917. return v < 0
  7918. })
  7919. };
  7920. ChartInternal.prototype.hasPositiveValueInTargets = function(targets) {
  7921. return this.checkValueInTargets(targets, function(v) {
  7922. return v > 0
  7923. })
  7924. };
  7925. ChartInternal.prototype.isOrderDesc = function() {
  7926. var config = this.config;
  7927. return (
  7928. typeof config.data_order === 'string' &&
  7929. config.data_order.toLowerCase() === 'desc'
  7930. )
  7931. };
  7932. ChartInternal.prototype.isOrderAsc = function() {
  7933. var config = this.config;
  7934. return (
  7935. typeof config.data_order === 'string' &&
  7936. config.data_order.toLowerCase() === 'asc'
  7937. )
  7938. };
  7939. ChartInternal.prototype.getOrderFunction = function() {
  7940. var $$ = this,
  7941. config = $$.config,
  7942. orderAsc = $$.isOrderAsc(),
  7943. orderDesc = $$.isOrderDesc();
  7944. if (orderAsc || orderDesc) {
  7945. var reducer = function(p, c) {
  7946. return p + Math.abs(c.value)
  7947. };
  7948. return function(t1, t2) {
  7949. var t1Sum = t1.values.reduce(reducer, 0),
  7950. t2Sum = t2.values.reduce(reducer, 0);
  7951. return orderAsc ? t2Sum - t1Sum : t1Sum - t2Sum
  7952. }
  7953. } else if (isFunction(config.data_order)) {
  7954. return config.data_order
  7955. } else if (isArray(config.data_order)) {
  7956. var order = config.data_order;
  7957. return function(t1, t2) {
  7958. return order.indexOf(t1.id) - order.indexOf(t2.id)
  7959. }
  7960. }
  7961. };
  7962. ChartInternal.prototype.orderTargets = function(targets) {
  7963. var fct = this.getOrderFunction();
  7964. if (fct) {
  7965. targets.sort(fct);
  7966. }
  7967. return targets
  7968. };
  7969. /**
  7970. * Returns all the values from the given targets at the given index.
  7971. *
  7972. * @param {Array} targets
  7973. * @param {Number} index
  7974. * @return {Array}
  7975. */
  7976. ChartInternal.prototype.filterByIndex = function(targets, index) {
  7977. return this.d3.merge(
  7978. targets.map(t => t.values.filter(v => v.index === index))
  7979. )
  7980. };
  7981. ChartInternal.prototype.filterByX = function(targets, x) {
  7982. return this.d3
  7983. .merge(
  7984. targets.map(function(t) {
  7985. return t.values
  7986. })
  7987. )
  7988. .filter(function(v) {
  7989. return v.x - x === 0
  7990. })
  7991. };
  7992. ChartInternal.prototype.filterRemoveNull = function(data) {
  7993. return data.filter(function(d) {
  7994. return isValue(d.value)
  7995. })
  7996. };
  7997. ChartInternal.prototype.filterByXDomain = function(targets, xDomain) {
  7998. return targets.map(function(t) {
  7999. return {
  8000. id: t.id,
  8001. id_org: t.id_org,
  8002. values: t.values.filter(function(v) {
  8003. return xDomain[0] <= v.x && v.x <= xDomain[1]
  8004. })
  8005. }
  8006. })
  8007. };
  8008. ChartInternal.prototype.hasDataLabel = function() {
  8009. var config = this.config;
  8010. if (typeof config.data_labels === 'boolean' && config.data_labels) {
  8011. return true
  8012. } else if (
  8013. typeof config.data_labels === 'object' &&
  8014. notEmpty(config.data_labels)
  8015. ) {
  8016. return true
  8017. }
  8018. return false
  8019. };
  8020. ChartInternal.prototype.getDataLabelLength = function(min, max, key) {
  8021. var $$ = this,
  8022. lengths = [0, 0],
  8023. paddingCoef = 1.3;
  8024. $$.selectChart
  8025. .select('svg')
  8026. .selectAll('.dummy')
  8027. .data([min, max])
  8028. .enter()
  8029. .append('text')
  8030. .text(function(d) {
  8031. return $$.dataLabelFormat(d.id)(d)
  8032. })
  8033. .each(function(d, i) {
  8034. lengths[i] = getBBox(this)[key] * paddingCoef;
  8035. })
  8036. .remove();
  8037. return lengths
  8038. };
  8039. /**
  8040. * Returns true if the given data point is not arc type, otherwise false.
  8041. * @param {Object} d The data point
  8042. * @return {boolean}
  8043. */
  8044. ChartInternal.prototype.isNoneArc = function(d) {
  8045. return this.hasTarget(this.data.targets, d.id)
  8046. };
  8047. /**
  8048. * Returns true if the given data point is arc type, otherwise false.
  8049. * @param {Object} d The data point
  8050. * @return {boolean}
  8051. */
  8052. ChartInternal.prototype.isArc = function(d) {
  8053. return 'data' in d && this.hasTarget(this.data.targets, d.data.id)
  8054. };
  8055. /**
  8056. * Find the closest point from the given pos among the given targets or
  8057. * undefined if none satisfies conditions.
  8058. *
  8059. * @param {Array} targets
  8060. * @param {Array} pos An [x,y] coordinate
  8061. * @return {Object|undefined}
  8062. */
  8063. ChartInternal.prototype.findClosestFromTargets = function(targets, pos) {
  8064. const $$ = this;
  8065. // for each target, find the closest point
  8066. const candidates = targets
  8067. .map(t =>
  8068. $$.findClosest(
  8069. t.values,
  8070. pos,
  8071. $$.config.tooltip_horizontal
  8072. ? $$.horizontalDistance.bind($$)
  8073. : $$.dist.bind($$),
  8074. $$.config.point_sensitivity
  8075. )
  8076. )
  8077. .filter(v => v);
  8078. // returns the closest of candidates
  8079. if (candidates.length === 0) {
  8080. return undefined
  8081. } else if (candidates.length === 1) {
  8082. return candidates[0]
  8083. } else {
  8084. return $$.findClosest(candidates, pos, $$.dist.bind($$))
  8085. }
  8086. };
  8087. /**
  8088. * Find the closest point from the x value or undefined if none satisfies conditions.
  8089. *
  8090. * @param {Array} targets
  8091. * @param {Array} x A value on X axis
  8092. * @return {Object|undefined}
  8093. */
  8094. ChartInternal.prototype.findClosestFromTargetsByX = function(targets, x) {
  8095. let closest;
  8096. let diff;
  8097. targets.forEach(t => {
  8098. t.values.forEach(d => {
  8099. let newDiff = Math.abs(x - d.x);
  8100. if (diff === undefined || newDiff < diff) {
  8101. closest = d;
  8102. diff = newDiff;
  8103. }
  8104. });
  8105. });
  8106. return closest
  8107. };
  8108. /**
  8109. * Using given compute distance method, returns the closest data point from the
  8110. * given position.
  8111. *
  8112. * Giving optionally a minimum distance to satisfy.
  8113. *
  8114. * @param {Array} dataPoints List of DataPoints
  8115. * @param {Array} pos An [x,y] coordinate
  8116. * @param {Function} computeDist Function to compute distance between 2 points
  8117. * @param {Number} minDist Minimal distance to satisfy
  8118. * @return {Object|undefined} Closest data point
  8119. */
  8120. ChartInternal.prototype.findClosest = function(
  8121. dataPoints,
  8122. pos,
  8123. computeDist,
  8124. minDist = Infinity
  8125. ) {
  8126. const $$ = this;
  8127. let closest;
  8128. // find closest bar
  8129. dataPoints
  8130. .filter(v => v && $$.isBarType(v.id))
  8131. .forEach(function(v) {
  8132. if (!closest) {
  8133. const shape = $$.main
  8134. .select(
  8135. '.' +
  8136. CLASS.bars +
  8137. $$.getTargetSelectorSuffix(v.id) +
  8138. ' .' +
  8139. CLASS.bar +
  8140. '-' +
  8141. v.index
  8142. )
  8143. .node();
  8144. if ($$.isWithinBar(pos, shape)) {
  8145. closest = v;
  8146. }
  8147. }
  8148. });
  8149. // find closest point from non-bar
  8150. dataPoints
  8151. .filter(v => v && !$$.isBarType(v.id))
  8152. .forEach(v => {
  8153. let d = computeDist(v, pos);
  8154. if (d < minDist) {
  8155. minDist = d;
  8156. closest = v;
  8157. }
  8158. });
  8159. return closest
  8160. };
  8161. ChartInternal.prototype.dist = function(data, pos) {
  8162. var $$ = this,
  8163. config = $$.config,
  8164. xIndex = config.axis_rotated ? 1 : 0,
  8165. yIndex = config.axis_rotated ? 0 : 1,
  8166. y = $$.circleY(data, data.index),
  8167. x = $$.x(data.x);
  8168. return Math.sqrt(Math.pow(x - pos[xIndex], 2) + Math.pow(y - pos[yIndex], 2))
  8169. };
  8170. ChartInternal.prototype.horizontalDistance = function(data, pos) {
  8171. var $$ = this,
  8172. config = $$.config,
  8173. xIndex = config.axis_rotated ? 1 : 0,
  8174. x = $$.x(data.x);
  8175. return Math.abs(x - pos[xIndex])
  8176. };
  8177. ChartInternal.prototype.convertValuesToStep = function(values) {
  8178. var converted = [].concat(values),
  8179. i;
  8180. if (!this.isCategorized()) {
  8181. return values
  8182. }
  8183. for (i = values.length + 1; 0 < i; i--) {
  8184. converted[i] = converted[i - 1];
  8185. }
  8186. converted[0] = {
  8187. x: converted[0].x - 1,
  8188. value: converted[0].value,
  8189. id: converted[0].id
  8190. };
  8191. converted[values.length + 1] = {
  8192. x: converted[values.length].x + 1,
  8193. value: converted[values.length].value,
  8194. id: converted[values.length].id
  8195. };
  8196. return converted
  8197. };
  8198. /**
  8199. * Get ratio value
  8200. *
  8201. * @param {String} type Ratio for given type
  8202. * @param {Object} d Data value object
  8203. * @param {Boolean} asPercent Convert the return as percent or not
  8204. * @return {Number} Ratio value
  8205. * @private
  8206. */
  8207. ChartInternal.prototype.getRatio = function(type, d, asPercent = false) {
  8208. const $$ = this;
  8209. const api = $$.api;
  8210. let ratio = 0;
  8211. if (d && api.data.shown.call(api).length) {
  8212. ratio = d.ratio || d.value;
  8213. if (type === 'arc') {
  8214. if ($$.hasType('gauge')) {
  8215. ratio =
  8216. (d.endAngle - d.startAngle) /
  8217. (Math.PI * ($$.config.gauge_fullCircle ? 2 : 1));
  8218. } else {
  8219. const total = $$.getTotalDataSum();
  8220. ratio = d.value / total;
  8221. }
  8222. } else if (type === 'index') {
  8223. const total = $$.getTotalPerIndex($$.axis.getId(d.id));
  8224. d.ratio =
  8225. isNumber(d.value) && total && total[d.index] > 0
  8226. ? d.value / total[d.index]
  8227. : 0;
  8228. ratio = d.ratio;
  8229. }
  8230. }
  8231. return asPercent && ratio ? ratio * 100 : ratio
  8232. };
  8233. ChartInternal.prototype.updateDataAttributes = function(name, attrs) {
  8234. var $$ = this,
  8235. config = $$.config,
  8236. current = config['data_' + name];
  8237. if (typeof attrs === 'undefined') {
  8238. return current
  8239. }
  8240. Object.keys(attrs).forEach(function(id) {
  8241. current[id] = attrs[id];
  8242. });
  8243. $$.redraw({
  8244. withLegend: true
  8245. });
  8246. return current
  8247. };
  8248. ChartInternal.prototype.load = function(targets, args) {
  8249. var $$ = this;
  8250. if (targets) {
  8251. // filter loading targets if needed
  8252. if (args.filter) {
  8253. targets = targets.filter(args.filter);
  8254. }
  8255. // set type if args.types || args.type specified
  8256. if (args.type || args.types) {
  8257. targets.forEach(function(t) {
  8258. var type = args.types && args.types[t.id] ? args.types[t.id] : args.type;
  8259. $$.setTargetType(t.id, type);
  8260. });
  8261. }
  8262. // Update/Add data
  8263. $$.data.targets.forEach(function(d) {
  8264. for (var i = 0; i < targets.length; i++) {
  8265. if (d.id === targets[i].id) {
  8266. d.values = targets[i].values;
  8267. targets.splice(i, 1);
  8268. break
  8269. }
  8270. }
  8271. });
  8272. $$.data.targets = $$.data.targets.concat(targets); // add remained
  8273. }
  8274. // Set targets
  8275. $$.updateTargets($$.data.targets);
  8276. // Redraw with new targets
  8277. $$.redraw({
  8278. withUpdateOrgXDomain: true,
  8279. withUpdateXDomain: true,
  8280. withLegend: true
  8281. });
  8282. if (args.done) {
  8283. args.done();
  8284. }
  8285. };
  8286. ChartInternal.prototype.loadFromArgs = function(args) {
  8287. var $$ = this;
  8288. $$.resetCache();
  8289. if (args.data) {
  8290. $$.load($$.convertDataToTargets(args.data), args);
  8291. } else if (args.url) {
  8292. $$.convertUrlToData(
  8293. args.url,
  8294. args.mimeType,
  8295. args.headers,
  8296. args.keys,
  8297. function(data) {
  8298. $$.load($$.convertDataToTargets(data), args);
  8299. }
  8300. );
  8301. } else if (args.json) {
  8302. $$.load(
  8303. $$.convertDataToTargets($$.convertJsonToData(args.json, args.keys)),
  8304. args
  8305. );
  8306. } else if (args.rows) {
  8307. $$.load($$.convertDataToTargets($$.convertRowsToData(args.rows)), args);
  8308. } else if (args.columns) {
  8309. $$.load(
  8310. $$.convertDataToTargets($$.convertColumnsToData(args.columns)),
  8311. args
  8312. );
  8313. } else {
  8314. $$.load(null, args);
  8315. }
  8316. };
  8317. ChartInternal.prototype.unload = function(targetIds, done) {
  8318. var $$ = this;
  8319. $$.resetCache();
  8320. if (!done) {
  8321. done = function() {};
  8322. }
  8323. // filter existing target
  8324. targetIds = targetIds.filter(function(id) {
  8325. return $$.hasTarget($$.data.targets, id)
  8326. });
  8327. // If no target, call done and return
  8328. if (!targetIds || targetIds.length === 0) {
  8329. done();
  8330. return
  8331. }
  8332. $$.svg
  8333. .selectAll(
  8334. targetIds.map(function(id) {
  8335. return $$.selectorTarget(id)
  8336. })
  8337. )
  8338. .transition()
  8339. .style('opacity', 0)
  8340. .remove()
  8341. .call($$.endall, done);
  8342. targetIds.forEach(function(id) {
  8343. // Reset fadein for future load
  8344. $$.withoutFadeIn[id] = false;
  8345. // Remove target's elements
  8346. if ($$.legend) {
  8347. $$.legend
  8348. .selectAll('.' + CLASS.legendItem + $$.getTargetSelectorSuffix(id))
  8349. .remove();
  8350. }
  8351. // Remove target
  8352. $$.data.targets = $$.data.targets.filter(function(t) {
  8353. return t.id !== id
  8354. });
  8355. });
  8356. };
  8357. ChartInternal.prototype.getYDomainMin = function(targets) {
  8358. var $$ = this,
  8359. config = $$.config,
  8360. ids = $$.mapToIds(targets),
  8361. ys = $$.getValuesAsIdKeyed(targets),
  8362. j,
  8363. k,
  8364. baseId,
  8365. idsInGroup,
  8366. id,
  8367. hasNegativeValue;
  8368. if (config.data_groups.length > 0) {
  8369. hasNegativeValue = $$.hasNegativeValueInTargets(targets);
  8370. for (j = 0; j < config.data_groups.length; j++) {
  8371. // Determine baseId
  8372. idsInGroup = config.data_groups[j].filter(function(id) {
  8373. return ids.indexOf(id) >= 0
  8374. });
  8375. if (idsInGroup.length === 0) {
  8376. continue
  8377. }
  8378. baseId = idsInGroup[0];
  8379. // Consider negative values
  8380. if (hasNegativeValue && ys[baseId]) {
  8381. ys[baseId].forEach(function(v, i) {
  8382. ys[baseId][i] = v < 0 ? v : 0;
  8383. });
  8384. }
  8385. // Compute min
  8386. for (k = 1; k < idsInGroup.length; k++) {
  8387. id = idsInGroup[k];
  8388. if (!ys[id]) {
  8389. continue
  8390. }
  8391. ys[id].forEach(function(v, i) {
  8392. if (
  8393. $$.axis.getId(id) === $$.axis.getId(baseId) &&
  8394. ys[baseId] &&
  8395. !(hasNegativeValue && +v > 0)
  8396. ) {
  8397. ys[baseId][i] += +v;
  8398. }
  8399. });
  8400. }
  8401. }
  8402. }
  8403. return $$.d3.min(
  8404. Object.keys(ys).map(function(key) {
  8405. return $$.d3.min(ys[key])
  8406. })
  8407. )
  8408. };
  8409. ChartInternal.prototype.getYDomainMax = function(targets) {
  8410. var $$ = this,
  8411. config = $$.config,
  8412. ids = $$.mapToIds(targets),
  8413. ys = $$.getValuesAsIdKeyed(targets),
  8414. j,
  8415. k,
  8416. baseId,
  8417. idsInGroup,
  8418. id,
  8419. hasPositiveValue;
  8420. if (config.data_groups.length > 0) {
  8421. hasPositiveValue = $$.hasPositiveValueInTargets(targets);
  8422. for (j = 0; j < config.data_groups.length; j++) {
  8423. // Determine baseId
  8424. idsInGroup = config.data_groups[j].filter(function(id) {
  8425. return ids.indexOf(id) >= 0
  8426. });
  8427. if (idsInGroup.length === 0) {
  8428. continue
  8429. }
  8430. baseId = idsInGroup[0];
  8431. // Consider positive values
  8432. if (hasPositiveValue && ys[baseId]) {
  8433. ys[baseId].forEach(function(v, i) {
  8434. ys[baseId][i] = v > 0 ? v : 0;
  8435. });
  8436. }
  8437. // Compute max
  8438. for (k = 1; k < idsInGroup.length; k++) {
  8439. id = idsInGroup[k];
  8440. if (!ys[id]) {
  8441. continue
  8442. }
  8443. ys[id].forEach(function(v, i) {
  8444. if (
  8445. $$.axis.getId(id) === $$.axis.getId(baseId) &&
  8446. ys[baseId] &&
  8447. !(hasPositiveValue && +v < 0)
  8448. ) {
  8449. ys[baseId][i] += +v;
  8450. }
  8451. });
  8452. }
  8453. }
  8454. }
  8455. return $$.d3.max(
  8456. Object.keys(ys).map(function(key) {
  8457. return $$.d3.max(ys[key])
  8458. })
  8459. )
  8460. };
  8461. ChartInternal.prototype.getYDomain = function(targets, axisId, xDomain) {
  8462. var $$ = this,
  8463. config = $$.config;
  8464. if ($$.isAxisNormalized(axisId)) {
  8465. return [0, 100]
  8466. }
  8467. var targetsByAxisId = targets.filter(function(t) {
  8468. return $$.axis.getId(t.id) === axisId
  8469. }),
  8470. yTargets = xDomain
  8471. ? $$.filterByXDomain(targetsByAxisId, xDomain)
  8472. : targetsByAxisId,
  8473. yMin = axisId === 'y2' ? config.axis_y2_min : config.axis_y_min,
  8474. yMax = axisId === 'y2' ? config.axis_y2_max : config.axis_y_max,
  8475. yDomainMin = $$.getYDomainMin(yTargets),
  8476. yDomainMax = $$.getYDomainMax(yTargets),
  8477. domain,
  8478. domainLength,
  8479. padding_top,
  8480. padding_bottom,
  8481. center = axisId === 'y2' ? config.axis_y2_center : config.axis_y_center,
  8482. yDomainAbs,
  8483. lengths,
  8484. diff,
  8485. ratio,
  8486. isAllPositive,
  8487. isAllNegative,
  8488. isZeroBased =
  8489. ($$.hasType('bar', yTargets) && config.bar_zerobased) ||
  8490. ($$.hasType('area', yTargets) && config.area_zerobased),
  8491. isInverted =
  8492. axisId === 'y2' ? config.axis_y2_inverted : config.axis_y_inverted,
  8493. showHorizontalDataLabel = $$.hasDataLabel() && config.axis_rotated,
  8494. showVerticalDataLabel = $$.hasDataLabel() && !config.axis_rotated;
  8495. // MEMO: avoid inverting domain unexpectedly
  8496. yDomainMin = isValue(yMin)
  8497. ? yMin
  8498. : isValue(yMax)
  8499. ? yDomainMin < yMax
  8500. ? yDomainMin
  8501. : yMax - 10
  8502. : yDomainMin;
  8503. yDomainMax = isValue(yMax)
  8504. ? yMax
  8505. : isValue(yMin)
  8506. ? yMin < yDomainMax
  8507. ? yDomainMax
  8508. : yMin + 10
  8509. : yDomainMax;
  8510. if (yTargets.length === 0) {
  8511. // use current domain if target of axisId is none
  8512. return axisId === 'y2' ? $$.y2.domain() : $$.y.domain()
  8513. }
  8514. if (isNaN(yDomainMin)) {
  8515. // set minimum to zero when not number
  8516. yDomainMin = 0;
  8517. }
  8518. if (isNaN(yDomainMax)) {
  8519. // set maximum to have same value as yDomainMin
  8520. yDomainMax = yDomainMin;
  8521. }
  8522. if (yDomainMin === yDomainMax) {
  8523. yDomainMin < 0 ? (yDomainMax = 0) : (yDomainMin = 0);
  8524. }
  8525. isAllPositive = yDomainMin >= 0 && yDomainMax >= 0;
  8526. isAllNegative = yDomainMin <= 0 && yDomainMax <= 0;
  8527. // Cancel zerobased if axis_*_min / axis_*_max specified
  8528. if ((isValue(yMin) && isAllPositive) || (isValue(yMax) && isAllNegative)) {
  8529. isZeroBased = false;
  8530. }
  8531. // Bar/Area chart should be 0-based if all positive|negative
  8532. if (isZeroBased) {
  8533. if (isAllPositive) {
  8534. yDomainMin = 0;
  8535. }
  8536. if (isAllNegative) {
  8537. yDomainMax = 0;
  8538. }
  8539. }
  8540. domainLength = Math.abs(yDomainMax - yDomainMin);
  8541. padding_top = padding_bottom = domainLength * 0.1;
  8542. if (typeof center !== 'undefined') {
  8543. yDomainAbs = Math.max(Math.abs(yDomainMin), Math.abs(yDomainMax));
  8544. yDomainMax = center + yDomainAbs;
  8545. yDomainMin = center - yDomainAbs;
  8546. }
  8547. // add padding for data label
  8548. if (showHorizontalDataLabel) {
  8549. lengths = $$.getDataLabelLength(yDomainMin, yDomainMax, 'width');
  8550. diff = diffDomain($$.y.range());
  8551. ratio = [lengths[0] / diff, lengths[1] / diff];
  8552. padding_top += domainLength * (ratio[1] / (1 - ratio[0] - ratio[1]));
  8553. padding_bottom += domainLength * (ratio[0] / (1 - ratio[0] - ratio[1]));
  8554. } else if (showVerticalDataLabel) {
  8555. lengths = $$.getDataLabelLength(yDomainMin, yDomainMax, 'height');
  8556. const pixelsToAxisPadding = $$.getY(
  8557. config[`axis_${axisId}_type`],
  8558. // input domain as pixels
  8559. [0, config.axis_rotated ? $$.width : $$.height],
  8560. // output range as axis padding
  8561. [0, domainLength]
  8562. );
  8563. padding_top += pixelsToAxisPadding(lengths[1]);
  8564. padding_bottom += pixelsToAxisPadding(lengths[0]);
  8565. }
  8566. if (axisId === 'y' && notEmpty(config.axis_y_padding)) {
  8567. padding_top = $$.axis.getPadding(
  8568. config.axis_y_padding,
  8569. 'top',
  8570. padding_top,
  8571. domainLength
  8572. );
  8573. padding_bottom = $$.axis.getPadding(
  8574. config.axis_y_padding,
  8575. 'bottom',
  8576. padding_bottom,
  8577. domainLength
  8578. );
  8579. }
  8580. if (axisId === 'y2' && notEmpty(config.axis_y2_padding)) {
  8581. padding_top = $$.axis.getPadding(
  8582. config.axis_y2_padding,
  8583. 'top',
  8584. padding_top,
  8585. domainLength
  8586. );
  8587. padding_bottom = $$.axis.getPadding(
  8588. config.axis_y2_padding,
  8589. 'bottom',
  8590. padding_bottom,
  8591. domainLength
  8592. );
  8593. }
  8594. // Bar/Area chart should be 0-based if all positive|negative
  8595. if (isZeroBased) {
  8596. if (isAllPositive) {
  8597. padding_bottom = yDomainMin;
  8598. }
  8599. if (isAllNegative) {
  8600. padding_top = -yDomainMax;
  8601. }
  8602. }
  8603. domain = [yDomainMin - padding_bottom, yDomainMax + padding_top];
  8604. return isInverted ? domain.reverse() : domain
  8605. };
  8606. ChartInternal.prototype.getXDomainMin = function(targets) {
  8607. var $$ = this,
  8608. config = $$.config;
  8609. return isDefined(config.axis_x_min)
  8610. ? $$.isTimeSeries()
  8611. ? this.parseDate(config.axis_x_min)
  8612. : config.axis_x_min
  8613. : $$.d3.min(targets, function(t) {
  8614. return $$.d3.min(t.values, function(v) {
  8615. return v.x
  8616. })
  8617. })
  8618. };
  8619. ChartInternal.prototype.getXDomainMax = function(targets) {
  8620. var $$ = this,
  8621. config = $$.config;
  8622. return isDefined(config.axis_x_max)
  8623. ? $$.isTimeSeries()
  8624. ? this.parseDate(config.axis_x_max)
  8625. : config.axis_x_max
  8626. : $$.d3.max(targets, function(t) {
  8627. return $$.d3.max(t.values, function(v) {
  8628. return v.x
  8629. })
  8630. })
  8631. };
  8632. ChartInternal.prototype.getXDomainPadding = function(domain) {
  8633. var $$ = this,
  8634. config = $$.config,
  8635. diff = domain[1] - domain[0],
  8636. maxDataCount,
  8637. padding,
  8638. paddingLeft,
  8639. paddingRight;
  8640. if ($$.isCategorized()) {
  8641. padding = 0;
  8642. } else if ($$.hasType('bar')) {
  8643. maxDataCount = $$.getMaxDataCount();
  8644. padding = maxDataCount > 1 ? diff / (maxDataCount - 1) / 2 : 0.5;
  8645. } else {
  8646. padding = diff * 0.01;
  8647. }
  8648. if (
  8649. typeof config.axis_x_padding === 'object' &&
  8650. notEmpty(config.axis_x_padding)
  8651. ) {
  8652. paddingLeft = isValue(config.axis_x_padding.left)
  8653. ? config.axis_x_padding.left
  8654. : padding;
  8655. paddingRight = isValue(config.axis_x_padding.right)
  8656. ? config.axis_x_padding.right
  8657. : padding;
  8658. } else if (typeof config.axis_x_padding === 'number') {
  8659. paddingLeft = paddingRight = config.axis_x_padding;
  8660. } else {
  8661. paddingLeft = paddingRight = padding;
  8662. }
  8663. return { left: paddingLeft, right: paddingRight }
  8664. };
  8665. ChartInternal.prototype.getXDomain = function(targets) {
  8666. var $$ = this,
  8667. xDomain = [$$.getXDomainMin(targets), $$.getXDomainMax(targets)],
  8668. firstX = xDomain[0],
  8669. lastX = xDomain[1],
  8670. padding = $$.getXDomainPadding(xDomain),
  8671. min = 0,
  8672. max = 0;
  8673. // show center of x domain if min and max are the same
  8674. if (firstX - lastX === 0 && !$$.isCategorized()) {
  8675. if ($$.isTimeSeries()) {
  8676. firstX = new Date(firstX.getTime() * 0.5);
  8677. lastX = new Date(lastX.getTime() * 1.5);
  8678. } else {
  8679. firstX = firstX === 0 ? 1 : firstX * 0.5;
  8680. lastX = lastX === 0 ? -1 : lastX * 1.5;
  8681. }
  8682. }
  8683. if (firstX || firstX === 0) {
  8684. min = $$.isTimeSeries()
  8685. ? new Date(firstX.getTime() - padding.left)
  8686. : firstX - padding.left;
  8687. }
  8688. if (lastX || lastX === 0) {
  8689. max = $$.isTimeSeries()
  8690. ? new Date(lastX.getTime() + padding.right)
  8691. : lastX + padding.right;
  8692. }
  8693. return [min, max]
  8694. };
  8695. ChartInternal.prototype.updateXDomain = function(
  8696. targets,
  8697. withUpdateXDomain,
  8698. withUpdateOrgXDomain,
  8699. withTrim,
  8700. domain
  8701. ) {
  8702. var $$ = this,
  8703. config = $$.config;
  8704. if (withUpdateOrgXDomain) {
  8705. $$.x.domain(domain ? domain : $$.d3.extent($$.getXDomain(targets)));
  8706. $$.orgXDomain = $$.x.domain();
  8707. if (config.zoom_enabled) {
  8708. $$.zoom.update();
  8709. }
  8710. $$.subX.domain($$.x.domain());
  8711. if ($$.brush) {
  8712. $$.brush.updateScale($$.subX);
  8713. }
  8714. }
  8715. if (withUpdateXDomain) {
  8716. $$.x.domain(
  8717. domain
  8718. ? domain
  8719. : !$$.brush || $$.brush.empty()
  8720. ? $$.orgXDomain
  8721. : $$.brush.selectionAsValue()
  8722. );
  8723. }
  8724. // Trim domain when too big by zoom mousemove event
  8725. if (withTrim) {
  8726. $$.x.domain($$.trimXDomain($$.x.orgDomain()));
  8727. }
  8728. return $$.x.domain()
  8729. };
  8730. ChartInternal.prototype.trimXDomain = function(domain) {
  8731. var zoomDomain = this.getZoomDomain(),
  8732. min = zoomDomain[0],
  8733. max = zoomDomain[1];
  8734. if (domain[0] <= min) {
  8735. domain[1] = +domain[1] + (min - domain[0]);
  8736. domain[0] = min;
  8737. }
  8738. if (max <= domain[1]) {
  8739. domain[0] = +domain[0] - (domain[1] - max);
  8740. domain[1] = max;
  8741. }
  8742. return domain
  8743. };
  8744. ChartInternal.prototype.drag = function(mouse) {
  8745. var $$ = this,
  8746. config = $$.config,
  8747. main = $$.main,
  8748. d3 = $$.d3;
  8749. var sx, sy, mx, my, minX, maxX, minY, maxY;
  8750. if ($$.hasArcType()) {
  8751. return
  8752. }
  8753. if (!config.data_selection_enabled) {
  8754. return
  8755. } // do nothing if not selectable
  8756. if (!config.data_selection_multiple) {
  8757. return
  8758. } // skip when single selection because drag is used for multiple selection
  8759. sx = $$.dragStart[0];
  8760. sy = $$.dragStart[1];
  8761. mx = mouse[0];
  8762. my = mouse[1];
  8763. minX = Math.min(sx, mx);
  8764. maxX = Math.max(sx, mx);
  8765. minY = config.data_selection_grouped ? $$.margin.top : Math.min(sy, my);
  8766. maxY = config.data_selection_grouped ? $$.height : Math.max(sy, my);
  8767. main
  8768. .select('.' + CLASS.dragarea)
  8769. .attr('x', minX)
  8770. .attr('y', minY)
  8771. .attr('width', maxX - minX)
  8772. .attr('height', maxY - minY);
  8773. // TODO: binary search when multiple xs
  8774. main
  8775. .selectAll('.' + CLASS.shapes)
  8776. .selectAll('.' + CLASS.shape)
  8777. .each(function(d, i) {
  8778. if (!config.data_selection_isselectable(d)) {
  8779. return
  8780. }
  8781. var shape = d3.select(this),
  8782. isSelected = shape.classed(CLASS.SELECTED),
  8783. isIncluded = shape.classed(CLASS.INCLUDED),
  8784. _x,
  8785. _y,
  8786. _w,
  8787. _h,
  8788. toggle,
  8789. isWithin = false,
  8790. box;
  8791. if (shape.classed(CLASS.circle)) {
  8792. _x = shape.attr('cx') * 1;
  8793. _y = shape.attr('cy') * 1;
  8794. toggle = $$.togglePoint;
  8795. isWithin = minX < _x && _x < maxX && minY < _y && _y < maxY;
  8796. } else if (shape.classed(CLASS.bar)) {
  8797. box = getPathBox(this);
  8798. _x = box.x;
  8799. _y = box.y;
  8800. _w = box.width;
  8801. _h = box.height;
  8802. toggle = $$.togglePath;
  8803. isWithin =
  8804. !(maxX < _x || _x + _w < minX) && !(maxY < _y || _y + _h < minY);
  8805. } else {
  8806. // line/area selection not supported yet
  8807. return
  8808. }
  8809. if (isWithin ^ isIncluded) {
  8810. shape.classed(CLASS.INCLUDED, !isIncluded);
  8811. // TODO: included/unincluded callback here
  8812. shape.classed(CLASS.SELECTED, !isSelected);
  8813. toggle.call($$, !isSelected, shape, d, i);
  8814. }
  8815. });
  8816. };
  8817. ChartInternal.prototype.dragstart = function(mouse) {
  8818. var $$ = this,
  8819. config = $$.config;
  8820. if ($$.hasArcType()) {
  8821. return
  8822. }
  8823. if (!config.data_selection_enabled) {
  8824. return
  8825. } // do nothing if not selectable
  8826. $$.dragStart = mouse;
  8827. $$.main
  8828. .select('.' + CLASS.chart)
  8829. .append('rect')
  8830. .attr('class', CLASS.dragarea)
  8831. .style('opacity', 0.1);
  8832. $$.dragging = true;
  8833. };
  8834. ChartInternal.prototype.dragend = function() {
  8835. var $$ = this,
  8836. config = $$.config;
  8837. if ($$.hasArcType()) {
  8838. return
  8839. }
  8840. if (!config.data_selection_enabled) {
  8841. return
  8842. } // do nothing if not selectable
  8843. $$.main
  8844. .select('.' + CLASS.dragarea)
  8845. .transition()
  8846. .duration(100)
  8847. .style('opacity', 0)
  8848. .remove();
  8849. $$.main.selectAll('.' + CLASS.shape).classed(CLASS.INCLUDED, false);
  8850. $$.dragging = false;
  8851. };
  8852. ChartInternal.prototype.getYFormat = function(forArc) {
  8853. var $$ = this,
  8854. formatForY =
  8855. forArc && !$$.hasType('gauge') ? $$.defaultArcValueFormat : $$.yFormat,
  8856. formatForY2 =
  8857. forArc && !$$.hasType('gauge') ? $$.defaultArcValueFormat : $$.y2Format;
  8858. return function(v, ratio, id) {
  8859. var format = $$.axis.getId(id) === 'y2' ? formatForY2 : formatForY;
  8860. return format.call($$, v, ratio)
  8861. }
  8862. };
  8863. ChartInternal.prototype.yFormat = function(v) {
  8864. var $$ = this,
  8865. config = $$.config,
  8866. format = config.axis_y_tick_format
  8867. ? config.axis_y_tick_format
  8868. : $$.defaultValueFormat;
  8869. return format(v)
  8870. };
  8871. ChartInternal.prototype.y2Format = function(v) {
  8872. var $$ = this,
  8873. config = $$.config,
  8874. format = config.axis_y2_tick_format
  8875. ? config.axis_y2_tick_format
  8876. : $$.defaultValueFormat;
  8877. return format(v)
  8878. };
  8879. ChartInternal.prototype.defaultValueFormat = function(v) {
  8880. return isValue(v) ? +v : ''
  8881. };
  8882. ChartInternal.prototype.defaultArcValueFormat = function(v, ratio) {
  8883. return (ratio * 100).toFixed(1) + '%'
  8884. };
  8885. ChartInternal.prototype.dataLabelFormat = function(targetId) {
  8886. var $$ = this,
  8887. data_labels = $$.config.data_labels,
  8888. format,
  8889. defaultFormat = function(v) {
  8890. return isValue(v) ? +v : ''
  8891. };
  8892. // find format according to axis id
  8893. if (typeof data_labels.format === 'function') {
  8894. format = data_labels.format;
  8895. } else if (typeof data_labels.format === 'object') {
  8896. if (data_labels.format[targetId]) {
  8897. format =
  8898. data_labels.format[targetId] === true
  8899. ? defaultFormat
  8900. : data_labels.format[targetId];
  8901. } else {
  8902. format = function() {
  8903. return ''
  8904. };
  8905. }
  8906. } else {
  8907. format = defaultFormat;
  8908. }
  8909. return format
  8910. };
  8911. ChartInternal.prototype.initGrid = function() {
  8912. var $$ = this,
  8913. config = $$.config,
  8914. d3 = $$.d3;
  8915. $$.grid = $$.main
  8916. .append('g')
  8917. .attr('clip-path', $$.clipPathForGrid)
  8918. .attr('class', CLASS.grid);
  8919. if (config.grid_x_show) {
  8920. $$.grid.append('g').attr('class', CLASS.xgrids);
  8921. }
  8922. if (config.grid_y_show) {
  8923. $$.grid.append('g').attr('class', CLASS.ygrids);
  8924. }
  8925. if (config.grid_focus_show) {
  8926. $$.grid
  8927. .append('g')
  8928. .attr('class', CLASS.xgridFocus)
  8929. .append('line')
  8930. .attr('class', CLASS.xgridFocus);
  8931. }
  8932. $$.xgrid = d3.selectAll([]);
  8933. if (!config.grid_lines_front) {
  8934. $$.initGridLines();
  8935. }
  8936. };
  8937. ChartInternal.prototype.initGridLines = function() {
  8938. var $$ = this,
  8939. d3 = $$.d3;
  8940. $$.gridLines = $$.main
  8941. .append('g')
  8942. .attr('clip-path', $$.clipPathForGrid)
  8943. .attr('class', CLASS.grid + ' ' + CLASS.gridLines);
  8944. $$.gridLines.append('g').attr('class', CLASS.xgridLines);
  8945. $$.gridLines.append('g').attr('class', CLASS.ygridLines);
  8946. $$.xgridLines = d3.selectAll([]);
  8947. };
  8948. ChartInternal.prototype.updateXGrid = function(withoutUpdate) {
  8949. var $$ = this,
  8950. config = $$.config,
  8951. d3 = $$.d3,
  8952. xgridData = $$.generateGridData(config.grid_x_type, $$.x),
  8953. tickOffset = $$.isCategorized() ? $$.xAxis.tickOffset() : 0;
  8954. $$.xgridAttr = config.axis_rotated
  8955. ? {
  8956. x1: 0,
  8957. x2: $$.width,
  8958. y1: function(d) {
  8959. return $$.x(d) - tickOffset
  8960. },
  8961. y2: function(d) {
  8962. return $$.x(d) - tickOffset
  8963. }
  8964. }
  8965. : {
  8966. x1: function(d) {
  8967. return $$.x(d) + tickOffset
  8968. },
  8969. x2: function(d) {
  8970. return $$.x(d) + tickOffset
  8971. },
  8972. y1: 0,
  8973. y2: $$.height
  8974. };
  8975. $$.xgridAttr.opacity = function() {
  8976. var pos = +d3.select(this).attr(config.axis_rotated ? 'y1' : 'x1');
  8977. return pos === (config.axis_rotated ? $$.height : 0) ? 0 : 1
  8978. };
  8979. var xgrid = $$.main
  8980. .select('.' + CLASS.xgrids)
  8981. .selectAll('.' + CLASS.xgrid)
  8982. .data(xgridData);
  8983. var xgridEnter = xgrid
  8984. .enter()
  8985. .append('line')
  8986. .attr('class', CLASS.xgrid)
  8987. .attr('x1', $$.xgridAttr.x1)
  8988. .attr('x2', $$.xgridAttr.x2)
  8989. .attr('y1', $$.xgridAttr.y1)
  8990. .attr('y2', $$.xgridAttr.y2)
  8991. .style('opacity', 0);
  8992. $$.xgrid = xgridEnter.merge(xgrid);
  8993. if (!withoutUpdate) {
  8994. $$.xgrid
  8995. .attr('x1', $$.xgridAttr.x1)
  8996. .attr('x2', $$.xgridAttr.x2)
  8997. .attr('y1', $$.xgridAttr.y1)
  8998. .attr('y2', $$.xgridAttr.y2)
  8999. .style('opacity', $$.xgridAttr.opacity);
  9000. }
  9001. xgrid.exit().remove();
  9002. };
  9003. ChartInternal.prototype.updateYGrid = function() {
  9004. var $$ = this,
  9005. config = $$.config,
  9006. gridValues = $$.yAxis.tickValues() || $$.y.ticks(config.grid_y_ticks);
  9007. var ygrid = $$.main
  9008. .select('.' + CLASS.ygrids)
  9009. .selectAll('.' + CLASS.ygrid)
  9010. .data(gridValues);
  9011. var ygridEnter = ygrid
  9012. .enter()
  9013. .append('line')
  9014. // TODO: x1, x2, y1, y2, opacity need to be set here maybe
  9015. .attr('class', CLASS.ygrid);
  9016. $$.ygrid = ygridEnter.merge(ygrid);
  9017. $$.ygrid
  9018. .attr('x1', config.axis_rotated ? $$.y : 0)
  9019. .attr('x2', config.axis_rotated ? $$.y : $$.width)
  9020. .attr('y1', config.axis_rotated ? 0 : $$.y)
  9021. .attr('y2', config.axis_rotated ? $$.height : $$.y);
  9022. ygrid.exit().remove();
  9023. $$.smoothLines($$.ygrid, 'grid');
  9024. };
  9025. ChartInternal.prototype.gridTextAnchor = function(d) {
  9026. return d.position ? d.position : 'end'
  9027. };
  9028. ChartInternal.prototype.gridTextDx = function(d) {
  9029. return d.position === 'start' ? 4 : d.position === 'middle' ? 0 : -4
  9030. };
  9031. ChartInternal.prototype.xGridTextX = function(d) {
  9032. return d.position === 'start'
  9033. ? -this.height
  9034. : d.position === 'middle'
  9035. ? -this.height / 2
  9036. : 0
  9037. };
  9038. ChartInternal.prototype.yGridTextX = function(d) {
  9039. return d.position === 'start'
  9040. ? 0
  9041. : d.position === 'middle'
  9042. ? this.width / 2
  9043. : this.width
  9044. };
  9045. ChartInternal.prototype.updateGrid = function(duration) {
  9046. var $$ = this,
  9047. main = $$.main,
  9048. config = $$.config,
  9049. xgridLine,
  9050. xgridLineEnter,
  9051. ygridLine,
  9052. ygridLineEnter,
  9053. xv = $$.xv.bind($$),
  9054. yv = $$.yv.bind($$),
  9055. xGridTextX = $$.xGridTextX.bind($$),
  9056. yGridTextX = $$.yGridTextX.bind($$);
  9057. // hide if arc type
  9058. $$.grid.style('visibility', $$.hasArcType() ? 'hidden' : 'visible');
  9059. main.select('line.' + CLASS.xgridFocus).style('visibility', 'hidden');
  9060. if (config.grid_x_show) {
  9061. $$.updateXGrid();
  9062. }
  9063. xgridLine = main
  9064. .select('.' + CLASS.xgridLines)
  9065. .selectAll('.' + CLASS.xgridLine)
  9066. .data(config.grid_x_lines);
  9067. // enter
  9068. xgridLineEnter = xgridLine
  9069. .enter()
  9070. .append('g')
  9071. .attr('class', function(d) {
  9072. return CLASS.xgridLine + (d['class'] ? ' ' + d['class'] : '')
  9073. });
  9074. xgridLineEnter
  9075. .append('line')
  9076. .attr('x1', config.axis_rotated ? 0 : xv)
  9077. .attr('x2', config.axis_rotated ? $$.width : xv)
  9078. .attr('y1', config.axis_rotated ? xv : 0)
  9079. .attr('y2', config.axis_rotated ? xv : $$.height)
  9080. .style('opacity', 0);
  9081. xgridLineEnter
  9082. .append('text')
  9083. .attr('text-anchor', $$.gridTextAnchor)
  9084. .attr('transform', config.axis_rotated ? '' : 'rotate(-90)')
  9085. .attr('x', config.axis_rotated ? yGridTextX : xGridTextX)
  9086. .attr('y', xv)
  9087. .attr('dx', $$.gridTextDx)
  9088. .attr('dy', -5)
  9089. .style('opacity', 0);
  9090. // udpate
  9091. $$.xgridLines = xgridLineEnter.merge(xgridLine);
  9092. // done in d3.transition() of the end of this function
  9093. // exit
  9094. xgridLine
  9095. .exit()
  9096. .transition()
  9097. .duration(duration)
  9098. .style('opacity', 0)
  9099. .remove();
  9100. // Y-Grid
  9101. if (config.grid_y_show) {
  9102. $$.updateYGrid();
  9103. }
  9104. ygridLine = main
  9105. .select('.' + CLASS.ygridLines)
  9106. .selectAll('.' + CLASS.ygridLine)
  9107. .data(config.grid_y_lines);
  9108. // enter
  9109. ygridLineEnter = ygridLine
  9110. .enter()
  9111. .append('g')
  9112. .attr('class', function(d) {
  9113. return CLASS.ygridLine + (d['class'] ? ' ' + d['class'] : '')
  9114. });
  9115. ygridLineEnter
  9116. .append('line')
  9117. .attr('x1', config.axis_rotated ? yv : 0)
  9118. .attr('x2', config.axis_rotated ? yv : $$.width)
  9119. .attr('y1', config.axis_rotated ? 0 : yv)
  9120. .attr('y2', config.axis_rotated ? $$.height : yv)
  9121. .style('opacity', 0);
  9122. ygridLineEnter
  9123. .append('text')
  9124. .attr('text-anchor', $$.gridTextAnchor)
  9125. .attr('transform', config.axis_rotated ? 'rotate(-90)' : '')
  9126. .attr('x', config.axis_rotated ? xGridTextX : yGridTextX)
  9127. .attr('y', yv)
  9128. .attr('dx', $$.gridTextDx)
  9129. .attr('dy', -5)
  9130. .style('opacity', 0);
  9131. // update
  9132. $$.ygridLines = ygridLineEnter.merge(ygridLine);
  9133. $$.ygridLines
  9134. .select('line')
  9135. .transition()
  9136. .duration(duration)
  9137. .attr('x1', config.axis_rotated ? yv : 0)
  9138. .attr('x2', config.axis_rotated ? yv : $$.width)
  9139. .attr('y1', config.axis_rotated ? 0 : yv)
  9140. .attr('y2', config.axis_rotated ? $$.height : yv)
  9141. .style('opacity', 1);
  9142. $$.ygridLines
  9143. .select('text')
  9144. .transition()
  9145. .duration(duration)
  9146. .attr(
  9147. 'x',
  9148. config.axis_rotated ? $$.xGridTextX.bind($$) : $$.yGridTextX.bind($$)
  9149. )
  9150. .attr('y', yv)
  9151. .text(function(d) {
  9152. return d.text
  9153. })
  9154. .style('opacity', 1);
  9155. // exit
  9156. ygridLine
  9157. .exit()
  9158. .transition()
  9159. .duration(duration)
  9160. .style('opacity', 0)
  9161. .remove();
  9162. };
  9163. ChartInternal.prototype.redrawGrid = function(withTransition, transition) {
  9164. var $$ = this,
  9165. config = $$.config,
  9166. xv = $$.xv.bind($$),
  9167. lines = $$.xgridLines.select('line'),
  9168. texts = $$.xgridLines.select('text');
  9169. return [
  9170. (withTransition ? lines.transition(transition) : lines)
  9171. .attr('x1', config.axis_rotated ? 0 : xv)
  9172. .attr('x2', config.axis_rotated ? $$.width : xv)
  9173. .attr('y1', config.axis_rotated ? xv : 0)
  9174. .attr('y2', config.axis_rotated ? xv : $$.height)
  9175. .style('opacity', 1),
  9176. (withTransition ? texts.transition(transition) : texts)
  9177. .attr(
  9178. 'x',
  9179. config.axis_rotated ? $$.yGridTextX.bind($$) : $$.xGridTextX.bind($$)
  9180. )
  9181. .attr('y', xv)
  9182. .text(function(d) {
  9183. return d.text
  9184. })
  9185. .style('opacity', 1)
  9186. ]
  9187. };
  9188. ChartInternal.prototype.showXGridFocus = function(selectedData) {
  9189. var $$ = this,
  9190. config = $$.config,
  9191. dataToShow = selectedData.filter(function(d) {
  9192. return d && isValue(d.value)
  9193. }),
  9194. focusEl = $$.main.selectAll('line.' + CLASS.xgridFocus),
  9195. xx = $$.xx.bind($$);
  9196. if (!config.tooltip_show) {
  9197. return
  9198. }
  9199. // Hide when stanford plot exists
  9200. if ($$.hasType('stanford') || $$.hasArcType()) {
  9201. return
  9202. }
  9203. focusEl
  9204. .style('visibility', 'visible')
  9205. .data([dataToShow[0]])
  9206. .attr(config.axis_rotated ? 'y1' : 'x1', xx)
  9207. .attr(config.axis_rotated ? 'y2' : 'x2', xx);
  9208. $$.smoothLines(focusEl, 'grid');
  9209. };
  9210. ChartInternal.prototype.hideXGridFocus = function() {
  9211. this.main.select('line.' + CLASS.xgridFocus).style('visibility', 'hidden');
  9212. };
  9213. ChartInternal.prototype.updateXgridFocus = function() {
  9214. var $$ = this,
  9215. config = $$.config;
  9216. $$.main
  9217. .select('line.' + CLASS.xgridFocus)
  9218. .attr('x1', config.axis_rotated ? 0 : -10)
  9219. .attr('x2', config.axis_rotated ? $$.width : -10)
  9220. .attr('y1', config.axis_rotated ? -10 : 0)
  9221. .attr('y2', config.axis_rotated ? -10 : $$.height);
  9222. };
  9223. ChartInternal.prototype.generateGridData = function(type, scale) {
  9224. var $$ = this,
  9225. gridData = [],
  9226. xDomain,
  9227. firstYear,
  9228. lastYear,
  9229. i,
  9230. tickNum = $$.main
  9231. .select('.' + CLASS.axisX)
  9232. .selectAll('.tick')
  9233. .size();
  9234. if (type === 'year') {
  9235. xDomain = $$.getXDomain();
  9236. firstYear = xDomain[0].getFullYear();
  9237. lastYear = xDomain[1].getFullYear();
  9238. for (i = firstYear; i <= lastYear; i++) {
  9239. gridData.push(new Date(i + '-01-01 00:00:00'));
  9240. }
  9241. } else {
  9242. gridData = scale.ticks(10);
  9243. if (gridData.length > tickNum) {
  9244. // use only int
  9245. gridData = gridData.filter(function(d) {
  9246. return ('' + d).indexOf('.') < 0
  9247. });
  9248. }
  9249. }
  9250. return gridData
  9251. };
  9252. ChartInternal.prototype.getGridFilterToRemove = function(params) {
  9253. return params
  9254. ? function(line) {
  9255. var found = false
  9256. ;[].concat(params).forEach(function(param) {
  9257. if (
  9258. ('value' in param && line.value === param.value) ||
  9259. ('class' in param && line['class'] === param['class'])
  9260. ) {
  9261. found = true;
  9262. }
  9263. });
  9264. return found
  9265. }
  9266. : function() {
  9267. return true
  9268. }
  9269. };
  9270. ChartInternal.prototype.removeGridLines = function(params, forX) {
  9271. var $$ = this,
  9272. config = $$.config,
  9273. toRemove = $$.getGridFilterToRemove(params),
  9274. toShow = function(line) {
  9275. return !toRemove(line)
  9276. },
  9277. classLines = forX ? CLASS.xgridLines : CLASS.ygridLines,
  9278. classLine = forX ? CLASS.xgridLine : CLASS.ygridLine;
  9279. $$.main
  9280. .select('.' + classLines)
  9281. .selectAll('.' + classLine)
  9282. .filter(toRemove)
  9283. .transition()
  9284. .duration(config.transition_duration)
  9285. .style('opacity', 0)
  9286. .remove();
  9287. if (forX) {
  9288. config.grid_x_lines = config.grid_x_lines.filter(toShow);
  9289. } else {
  9290. config.grid_y_lines = config.grid_y_lines.filter(toShow);
  9291. }
  9292. };
  9293. ChartInternal.prototype.initEventRect = function() {
  9294. var $$ = this,
  9295. config = $$.config;
  9296. $$.main
  9297. .select('.' + CLASS.chart)
  9298. .append('g')
  9299. .attr('class', CLASS.eventRects)
  9300. .style('fill-opacity', 0);
  9301. $$.eventRect = $$.main
  9302. .select('.' + CLASS.eventRects)
  9303. .append('rect')
  9304. .attr('class', CLASS.eventRect);
  9305. // event rect handle zoom event as well
  9306. if (config.zoom_enabled && $$.zoom) {
  9307. $$.eventRect.call($$.zoom).on('dblclick.zoom', null);
  9308. if (config.zoom_initialRange) {
  9309. // WORKAROUND: Add transition to apply transform immediately when no subchart
  9310. $$.eventRect
  9311. .transition()
  9312. .duration(0)
  9313. .call($$.zoom.transform, $$.zoomTransform(config.zoom_initialRange));
  9314. }
  9315. }
  9316. };
  9317. ChartInternal.prototype.redrawEventRect = function() {
  9318. const $$ = this,
  9319. d3 = $$.d3,
  9320. config = $$.config;
  9321. function mouseout() {
  9322. $$.svg.select('.' + CLASS.eventRect).style('cursor', null);
  9323. $$.hideXGridFocus();
  9324. $$.hideTooltip();
  9325. $$.unexpandCircles();
  9326. $$.unexpandBars();
  9327. }
  9328. const isHoveringDataPoint = (mouse, closest) =>
  9329. closest &&
  9330. ($$.isBarType(closest.id) ||
  9331. $$.dist(closest, mouse) < config.point_sensitivity);
  9332. const withName = d => (d ? $$.addName(Object.assign({}, d)) : null);
  9333. // rects for mouseover
  9334. $$.main
  9335. .select('.' + CLASS.eventRects)
  9336. .style(
  9337. 'cursor',
  9338. config.zoom_enabled
  9339. ? config.axis_rotated
  9340. ? 'ns-resize'
  9341. : 'ew-resize'
  9342. : null
  9343. );
  9344. $$.eventRect
  9345. .attr('x', 0)
  9346. .attr('y', 0)
  9347. .attr('width', $$.width)
  9348. .attr('height', $$.height)
  9349. .on(
  9350. 'mouseout',
  9351. config.interaction_enabled
  9352. ? function() {
  9353. if (!config) {
  9354. return
  9355. } // chart is destroyed
  9356. if ($$.hasArcType()) {
  9357. return
  9358. }
  9359. if ($$.mouseover) {
  9360. config.data_onmouseout.call($$.api, $$.mouseover);
  9361. $$.mouseover = undefined;
  9362. }
  9363. mouseout();
  9364. }
  9365. : null
  9366. )
  9367. .on(
  9368. 'mousemove',
  9369. config.interaction_enabled
  9370. ? function() {
  9371. // do nothing when dragging
  9372. if ($$.dragging) {
  9373. return
  9374. }
  9375. const targetsToShow = $$.getTargetsToShow();
  9376. // do nothing if arc type
  9377. if ($$.hasArcType(targetsToShow)) {
  9378. return
  9379. }
  9380. const mouse = d3.mouse(this);
  9381. const closest = withName(
  9382. $$.findClosestFromTargets(targetsToShow, mouse)
  9383. );
  9384. const isMouseCloseToDataPoint = isHoveringDataPoint(mouse, closest);
  9385. // ensure onmouseout is always called if mousemove switch between 2 targets
  9386. if (
  9387. $$.mouseover &&
  9388. (!closest ||
  9389. closest.id !== $$.mouseover.id ||
  9390. closest.index !== $$.mouseover.index)
  9391. ) {
  9392. config.data_onmouseout.call($$.api, $$.mouseover);
  9393. $$.mouseover = undefined;
  9394. }
  9395. if (closest && !$$.mouseover) {
  9396. config.data_onmouseover.call($$.api, closest);
  9397. $$.mouseover = closest;
  9398. }
  9399. // show cursor as pointer if we're hovering a data point close enough
  9400. $$.svg
  9401. .select('.' + CLASS.eventRect)
  9402. .style('cursor', isMouseCloseToDataPoint ? 'pointer' : null);
  9403. // if tooltip not grouped, we want to display only data from closest data point
  9404. const showSingleDataPoint =
  9405. !config.tooltip_grouped || $$.hasType('stanford', targetsToShow);
  9406. // find data to highlight
  9407. let selectedData;
  9408. if (showSingleDataPoint) {
  9409. if (closest) {
  9410. selectedData = [closest];
  9411. }
  9412. } else {
  9413. let closestByX;
  9414. if (closest) {
  9415. // reuse closest value
  9416. closestByX = closest;
  9417. } else {
  9418. // try to find the closest value by X values from the mouse position
  9419. const mouseX = config.axis_rotated ? mouse[1] : mouse[0];
  9420. closestByX = $$.findClosestFromTargetsByX(
  9421. targetsToShow,
  9422. $$.x.invert(mouseX)
  9423. );
  9424. }
  9425. // highlight all data for this 'x' value
  9426. if (closestByX) {
  9427. selectedData = $$.filterByX(targetsToShow, closestByX.x);
  9428. }
  9429. }
  9430. // ensure we have data to show
  9431. if (!selectedData || selectedData.length === 0) {
  9432. return mouseout()
  9433. }
  9434. // inject names for each point
  9435. selectedData = selectedData.map(withName);
  9436. // show tooltip
  9437. $$.showTooltip(selectedData, this);
  9438. // expand points
  9439. if (config.point_focus_expand_enabled) {
  9440. $$.unexpandCircles();
  9441. selectedData.forEach(function(d) {
  9442. $$.expandCircles(d.index, d.id, false);
  9443. });
  9444. }
  9445. // expand bars
  9446. $$.unexpandBars();
  9447. selectedData.forEach(function(d) {
  9448. $$.expandBars(d.index, d.id, false);
  9449. });
  9450. // Show xgrid focus line
  9451. $$.showXGridFocus(selectedData);
  9452. }
  9453. : null
  9454. )
  9455. .on(
  9456. 'click',
  9457. config.interaction_enabled
  9458. ? function() {
  9459. const targetsToShow = $$.getTargetsToShow();
  9460. if ($$.hasArcType(targetsToShow)) {
  9461. return
  9462. }
  9463. const mouse = d3.mouse(this);
  9464. const closest = withName(
  9465. $$.findClosestFromTargets(targetsToShow, mouse)
  9466. );
  9467. if (!isHoveringDataPoint(mouse, closest)) {
  9468. return
  9469. }
  9470. // select if selection enabled
  9471. let sameXData;
  9472. if (!config.data_selection_grouped || $$.isStanfordType(closest)) {
  9473. sameXData = [closest];
  9474. } else {
  9475. sameXData = $$.filterByX(targetsToShow, closest.x);
  9476. }
  9477. // toggle selected state
  9478. sameXData.forEach(function(d) {
  9479. $$.main
  9480. .selectAll(
  9481. '.' + CLASS.shapes + $$.getTargetSelectorSuffix(d.id)
  9482. )
  9483. .selectAll('.' + CLASS.shape + '-' + d.index)
  9484. .each(function() {
  9485. if (
  9486. config.data_selection_grouped ||
  9487. $$.isWithinShape(this, d)
  9488. ) {
  9489. $$.toggleShape(this, d, d.index);
  9490. }
  9491. });
  9492. });
  9493. // call data_onclick on the closest data point
  9494. if (closest) {
  9495. const shape = $$.main
  9496. .selectAll(
  9497. '.' + CLASS.shapes + $$.getTargetSelectorSuffix(closest.id)
  9498. )
  9499. .select('.' + CLASS.shape + '-' + closest.index);
  9500. config.data_onclick.call($$.api, closest, shape.node());
  9501. }
  9502. }
  9503. : null
  9504. )
  9505. .call(
  9506. config.interaction_enabled && config.data_selection_draggable && $$.drag
  9507. ? d3
  9508. .drag()
  9509. .on('drag', function() {
  9510. $$.drag(d3.mouse(this));
  9511. })
  9512. .on('start', function() {
  9513. $$.dragstart(d3.mouse(this));
  9514. })
  9515. .on('end', function() {
  9516. $$.dragend();
  9517. })
  9518. : function() {}
  9519. );
  9520. };
  9521. ChartInternal.prototype.getMousePosition = function(data) {
  9522. var $$ = this;
  9523. return [$$.x(data.x), $$.getYScale(data.id)(data.value)]
  9524. };
  9525. ChartInternal.prototype.dispatchEvent = function(type, mouse) {
  9526. var $$ = this,
  9527. selector = '.' + CLASS.eventRect,
  9528. eventRect = $$.main.select(selector).node(),
  9529. box = eventRect.getBoundingClientRect(),
  9530. x = box.left + (mouse ? mouse[0] : 0),
  9531. y = box.top + (mouse ? mouse[1] : 0),
  9532. event = document.createEvent('MouseEvents');
  9533. event.initMouseEvent(
  9534. type,
  9535. true,
  9536. true,
  9537. window,
  9538. 0,
  9539. x,
  9540. y,
  9541. x,
  9542. y,
  9543. false,
  9544. false,
  9545. false,
  9546. false,
  9547. 0,
  9548. null
  9549. );
  9550. eventRect.dispatchEvent(event);
  9551. };
  9552. ChartInternal.prototype.initLegend = function() {
  9553. var $$ = this;
  9554. $$.legendItemTextBox = {};
  9555. $$.legendHasRendered = false;
  9556. $$.legend = $$.svg.append('g').attr('transform', $$.getTranslate('legend'));
  9557. if (!$$.config.legend_show) {
  9558. $$.legend.style('visibility', 'hidden');
  9559. $$.hiddenLegendIds = $$.mapToIds($$.data.targets);
  9560. return
  9561. }
  9562. // MEMO: call here to update legend box and tranlate for all
  9563. // MEMO: translate will be updated by this, so transform not needed in updateLegend()
  9564. $$.updateLegendWithDefaults();
  9565. };
  9566. ChartInternal.prototype.updateLegendWithDefaults = function() {
  9567. var $$ = this;
  9568. $$.updateLegend($$.mapToIds($$.data.targets), {
  9569. withTransform: false,
  9570. withTransitionForTransform: false,
  9571. withTransition: false
  9572. });
  9573. };
  9574. ChartInternal.prototype.updateSizeForLegend = function(
  9575. legendHeight,
  9576. legendWidth
  9577. ) {
  9578. var $$ = this,
  9579. config = $$.config,
  9580. insetLegendPosition = {
  9581. top: $$.isLegendTop
  9582. ? $$.getCurrentPaddingTop() + config.legend_inset_y + 5.5
  9583. : $$.currentHeight -
  9584. legendHeight -
  9585. $$.getCurrentPaddingBottom() -
  9586. config.legend_inset_y,
  9587. left: $$.isLegendLeft
  9588. ? $$.getCurrentPaddingLeft() + config.legend_inset_x + 0.5
  9589. : $$.currentWidth -
  9590. legendWidth -
  9591. $$.getCurrentPaddingRight() -
  9592. config.legend_inset_x +
  9593. 0.5
  9594. };
  9595. $$.margin3 = {
  9596. top: $$.isLegendRight
  9597. ? 0
  9598. : $$.isLegendInset
  9599. ? insetLegendPosition.top
  9600. : $$.currentHeight - legendHeight,
  9601. right: NaN,
  9602. bottom: 0,
  9603. left: $$.isLegendRight
  9604. ? $$.currentWidth - legendWidth
  9605. : $$.isLegendInset
  9606. ? insetLegendPosition.left
  9607. : 0
  9608. };
  9609. };
  9610. ChartInternal.prototype.transformLegend = function(withTransition) {
  9611. var $$ = this
  9612. ;(withTransition ? $$.legend.transition() : $$.legend).attr(
  9613. 'transform',
  9614. $$.getTranslate('legend')
  9615. );
  9616. };
  9617. ChartInternal.prototype.updateLegendStep = function(step) {
  9618. this.legendStep = step;
  9619. };
  9620. ChartInternal.prototype.updateLegendItemWidth = function(w) {
  9621. this.legendItemWidth = w;
  9622. };
  9623. ChartInternal.prototype.updateLegendItemHeight = function(h) {
  9624. this.legendItemHeight = h;
  9625. };
  9626. ChartInternal.prototype.getLegendWidth = function() {
  9627. var $$ = this;
  9628. return $$.config.legend_show
  9629. ? $$.isLegendRight || $$.isLegendInset
  9630. ? $$.legendItemWidth * ($$.legendStep + 1)
  9631. : $$.currentWidth
  9632. : 0
  9633. };
  9634. ChartInternal.prototype.getLegendHeight = function() {
  9635. var $$ = this,
  9636. h = 0;
  9637. if ($$.config.legend_show) {
  9638. if ($$.isLegendRight) {
  9639. h = $$.currentHeight;
  9640. } else {
  9641. h = Math.max(20, $$.legendItemHeight) * ($$.legendStep + 1);
  9642. }
  9643. }
  9644. return h
  9645. };
  9646. ChartInternal.prototype.opacityForLegend = function(legendItem) {
  9647. return legendItem.classed(CLASS.legendItemHidden) ? null : 1
  9648. };
  9649. ChartInternal.prototype.opacityForUnfocusedLegend = function(legendItem) {
  9650. return legendItem.classed(CLASS.legendItemHidden) ? null : 0.3
  9651. };
  9652. ChartInternal.prototype.toggleFocusLegend = function(targetIds, focus) {
  9653. var $$ = this;
  9654. targetIds = $$.mapToTargetIds(targetIds);
  9655. $$.legend
  9656. .selectAll('.' + CLASS.legendItem)
  9657. .filter(function(id) {
  9658. return targetIds.indexOf(id) >= 0
  9659. })
  9660. .classed(CLASS.legendItemFocused, focus)
  9661. .transition()
  9662. .duration(100)
  9663. .style('opacity', function() {
  9664. var opacity = focus ? $$.opacityForLegend : $$.opacityForUnfocusedLegend;
  9665. return opacity.call($$, $$.d3.select(this))
  9666. });
  9667. };
  9668. ChartInternal.prototype.revertLegend = function() {
  9669. var $$ = this,
  9670. d3 = $$.d3;
  9671. $$.legend
  9672. .selectAll('.' + CLASS.legendItem)
  9673. .classed(CLASS.legendItemFocused, false)
  9674. .transition()
  9675. .duration(100)
  9676. .style('opacity', function() {
  9677. return $$.opacityForLegend(d3.select(this))
  9678. });
  9679. };
  9680. ChartInternal.prototype.showLegend = function(targetIds) {
  9681. var $$ = this,
  9682. config = $$.config;
  9683. if (!config.legend_show) {
  9684. config.legend_show = true;
  9685. $$.legend.style('visibility', 'visible');
  9686. if (!$$.legendHasRendered) {
  9687. $$.updateLegendWithDefaults();
  9688. }
  9689. }
  9690. $$.removeHiddenLegendIds(targetIds);
  9691. $$.legend
  9692. .selectAll($$.selectorLegends(targetIds))
  9693. .style('visibility', 'visible')
  9694. .transition()
  9695. .style('opacity', function() {
  9696. return $$.opacityForLegend($$.d3.select(this))
  9697. });
  9698. };
  9699. ChartInternal.prototype.hideLegend = function(targetIds) {
  9700. var $$ = this,
  9701. config = $$.config;
  9702. if (config.legend_show && isEmpty(targetIds)) {
  9703. config.legend_show = false;
  9704. $$.legend.style('visibility', 'hidden');
  9705. }
  9706. $$.addHiddenLegendIds(targetIds);
  9707. $$.legend
  9708. .selectAll($$.selectorLegends(targetIds))
  9709. .style('opacity', 0)
  9710. .style('visibility', 'hidden');
  9711. };
  9712. ChartInternal.prototype.clearLegendItemTextBoxCache = function() {
  9713. this.legendItemTextBox = {};
  9714. };
  9715. ChartInternal.prototype.updateLegend = function(
  9716. targetIds,
  9717. options,
  9718. transitions
  9719. ) {
  9720. var $$ = this,
  9721. config = $$.config;
  9722. var xForLegend,
  9723. xForLegendText,
  9724. xForLegendRect,
  9725. yForLegend,
  9726. yForLegendText,
  9727. yForLegendRect,
  9728. x1ForLegendTile,
  9729. x2ForLegendTile,
  9730. yForLegendTile;
  9731. var paddingTop = 4,
  9732. paddingRight = 10,
  9733. maxWidth = 0,
  9734. maxHeight = 0,
  9735. posMin = 10,
  9736. tileWidth = config.legend_item_tile_width + 5;
  9737. var l,
  9738. totalLength = 0,
  9739. offsets = {},
  9740. widths = {},
  9741. heights = {},
  9742. margins = [0],
  9743. steps = {},
  9744. step = 0;
  9745. var withTransition, withTransitionForTransform;
  9746. var texts, rects, tiles, background;
  9747. // Skip elements when their name is set to null
  9748. targetIds = targetIds.filter(function(id) {
  9749. return !isDefined(config.data_names[id]) || config.data_names[id] !== null
  9750. });
  9751. options = options || {};
  9752. withTransition = getOption(options, 'withTransition', true);
  9753. withTransitionForTransform = getOption(
  9754. options,
  9755. 'withTransitionForTransform',
  9756. true
  9757. );
  9758. function getTextBox(textElement, id) {
  9759. if (!$$.legendItemTextBox[id]) {
  9760. $$.legendItemTextBox[id] = $$.getTextRect(
  9761. textElement.textContent,
  9762. CLASS.legendItem,
  9763. textElement
  9764. );
  9765. }
  9766. return $$.legendItemTextBox[id]
  9767. }
  9768. function updatePositions(textElement, id, index) {
  9769. var reset = index === 0,
  9770. isLast = index === targetIds.length - 1,
  9771. box = getTextBox(textElement, id),
  9772. itemWidth =
  9773. box.width +
  9774. tileWidth +
  9775. (isLast && !($$.isLegendRight || $$.isLegendInset) ? 0 : paddingRight) +
  9776. config.legend_padding,
  9777. itemHeight = box.height + paddingTop,
  9778. itemLength =
  9779. $$.isLegendRight || $$.isLegendInset ? itemHeight : itemWidth,
  9780. areaLength =
  9781. $$.isLegendRight || $$.isLegendInset
  9782. ? $$.getLegendHeight()
  9783. : $$.getLegendWidth(),
  9784. margin,
  9785. maxLength;
  9786. // MEMO: care about condifion of step, totalLength
  9787. function updateValues(id, withoutStep) {
  9788. if (!withoutStep) {
  9789. margin = (areaLength - totalLength - itemLength) / 2;
  9790. if (margin < posMin) {
  9791. margin = (areaLength - itemLength) / 2;
  9792. totalLength = 0;
  9793. step++;
  9794. }
  9795. }
  9796. steps[id] = step;
  9797. margins[step] = $$.isLegendInset ? 10 : margin;
  9798. offsets[id] = totalLength;
  9799. totalLength += itemLength;
  9800. }
  9801. if (reset) {
  9802. totalLength = 0;
  9803. step = 0;
  9804. maxWidth = 0;
  9805. maxHeight = 0;
  9806. }
  9807. if (config.legend_show && !$$.isLegendToShow(id)) {
  9808. widths[id] = heights[id] = steps[id] = offsets[id] = 0;
  9809. return
  9810. }
  9811. widths[id] = itemWidth;
  9812. heights[id] = itemHeight;
  9813. if (!maxWidth || itemWidth >= maxWidth) {
  9814. maxWidth = itemWidth;
  9815. }
  9816. if (!maxHeight || itemHeight >= maxHeight) {
  9817. maxHeight = itemHeight;
  9818. }
  9819. maxLength = $$.isLegendRight || $$.isLegendInset ? maxHeight : maxWidth;
  9820. if (config.legend_equally) {
  9821. Object.keys(widths).forEach(function(id) {
  9822. widths[id] = maxWidth;
  9823. });
  9824. Object.keys(heights).forEach(function(id) {
  9825. heights[id] = maxHeight;
  9826. });
  9827. margin = (areaLength - maxLength * targetIds.length) / 2;
  9828. if (margin < posMin) {
  9829. totalLength = 0;
  9830. step = 0;
  9831. targetIds.forEach(function(id) {
  9832. updateValues(id);
  9833. });
  9834. } else {
  9835. updateValues(id, true);
  9836. }
  9837. } else {
  9838. updateValues(id);
  9839. }
  9840. }
  9841. if ($$.isLegendInset) {
  9842. step = config.legend_inset_step
  9843. ? config.legend_inset_step
  9844. : targetIds.length;
  9845. $$.updateLegendStep(step);
  9846. }
  9847. if ($$.isLegendRight) {
  9848. xForLegend = function(id) {
  9849. return maxWidth * steps[id]
  9850. };
  9851. yForLegend = function(id) {
  9852. return margins[steps[id]] + offsets[id]
  9853. };
  9854. } else if ($$.isLegendInset) {
  9855. xForLegend = function(id) {
  9856. return maxWidth * steps[id] + 10
  9857. };
  9858. yForLegend = function(id) {
  9859. return margins[steps[id]] + offsets[id]
  9860. };
  9861. } else {
  9862. xForLegend = function(id) {
  9863. return margins[steps[id]] + offsets[id]
  9864. };
  9865. yForLegend = function(id) {
  9866. return maxHeight * steps[id]
  9867. };
  9868. }
  9869. xForLegendText = function(id, i) {
  9870. return xForLegend(id, i) + 4 + config.legend_item_tile_width
  9871. };
  9872. yForLegendText = function(id, i) {
  9873. return yForLegend(id, i) + 9
  9874. };
  9875. xForLegendRect = function(id, i) {
  9876. return xForLegend(id, i)
  9877. };
  9878. yForLegendRect = function(id, i) {
  9879. return yForLegend(id, i) - 5
  9880. };
  9881. x1ForLegendTile = function(id, i) {
  9882. return xForLegend(id, i) - 2
  9883. };
  9884. x2ForLegendTile = function(id, i) {
  9885. return xForLegend(id, i) - 2 + config.legend_item_tile_width
  9886. };
  9887. yForLegendTile = function(id, i) {
  9888. return yForLegend(id, i) + 4
  9889. };
  9890. // Define g for legend area
  9891. l = $$.legend
  9892. .selectAll('.' + CLASS.legendItem)
  9893. .data(targetIds)
  9894. .enter()
  9895. .append('g')
  9896. .attr('class', function(id) {
  9897. return $$.generateClass(CLASS.legendItem, id)
  9898. })
  9899. .style('visibility', function(id) {
  9900. return $$.isLegendToShow(id) ? 'visible' : 'hidden'
  9901. })
  9902. .style('cursor', function() {
  9903. return config.interaction_enabled ? 'pointer' : 'auto'
  9904. })
  9905. .on(
  9906. 'click',
  9907. config.interaction_enabled
  9908. ? function(id) {
  9909. if (config.legend_item_onclick) {
  9910. config.legend_item_onclick.call($$, id);
  9911. } else {
  9912. if ($$.d3.event.altKey) {
  9913. $$.api.hide();
  9914. $$.api.show(id);
  9915. } else {
  9916. $$.api.toggle(id);
  9917. $$.isTargetToShow(id) ? $$.api.focus(id) : $$.api.revert();
  9918. }
  9919. }
  9920. }
  9921. : null
  9922. )
  9923. .on(
  9924. 'mouseover',
  9925. config.interaction_enabled
  9926. ? function(id) {
  9927. if (config.legend_item_onmouseover) {
  9928. config.legend_item_onmouseover.call($$, id);
  9929. } else {
  9930. $$.d3.select(this).classed(CLASS.legendItemFocused, true);
  9931. if (!$$.transiting && $$.isTargetToShow(id)) {
  9932. $$.api.focus(id);
  9933. }
  9934. }
  9935. }
  9936. : null
  9937. )
  9938. .on(
  9939. 'mouseout',
  9940. config.interaction_enabled
  9941. ? function(id) {
  9942. if (config.legend_item_onmouseout) {
  9943. config.legend_item_onmouseout.call($$, id);
  9944. } else {
  9945. $$.d3.select(this).classed(CLASS.legendItemFocused, false);
  9946. $$.api.revert();
  9947. }
  9948. }
  9949. : null
  9950. );
  9951. l.append('text')
  9952. .text(function(id) {
  9953. return isDefined(config.data_names[id]) ? config.data_names[id] : id
  9954. })
  9955. .each(function(id, i) {
  9956. updatePositions(this, id, i);
  9957. })
  9958. .style('pointer-events', 'none')
  9959. .attr('x', $$.isLegendRight || $$.isLegendInset ? xForLegendText : -200)
  9960. .attr('y', $$.isLegendRight || $$.isLegendInset ? -200 : yForLegendText);
  9961. l.append('rect')
  9962. .attr('class', CLASS.legendItemEvent)
  9963. .style('fill-opacity', 0)
  9964. .attr('x', $$.isLegendRight || $$.isLegendInset ? xForLegendRect : -200)
  9965. .attr('y', $$.isLegendRight || $$.isLegendInset ? -200 : yForLegendRect);
  9966. l.append('line')
  9967. .attr('class', CLASS.legendItemTile)
  9968. .style('stroke', $$.color)
  9969. .style('pointer-events', 'none')
  9970. .attr('x1', $$.isLegendRight || $$.isLegendInset ? x1ForLegendTile : -200)
  9971. .attr('y1', $$.isLegendRight || $$.isLegendInset ? -200 : yForLegendTile)
  9972. .attr('x2', $$.isLegendRight || $$.isLegendInset ? x2ForLegendTile : -200)
  9973. .attr('y2', $$.isLegendRight || $$.isLegendInset ? -200 : yForLegendTile)
  9974. .attr('stroke-width', config.legend_item_tile_height);
  9975. // Set background for inset legend
  9976. background = $$.legend.select('.' + CLASS.legendBackground + ' rect');
  9977. if ($$.isLegendInset && maxWidth > 0 && background.size() === 0) {
  9978. background = $$.legend
  9979. .insert('g', '.' + CLASS.legendItem)
  9980. .attr('class', CLASS.legendBackground)
  9981. .append('rect');
  9982. }
  9983. texts = $$.legend
  9984. .selectAll('text')
  9985. .data(targetIds)
  9986. .text(function(id) {
  9987. return isDefined(config.data_names[id]) ? config.data_names[id] : id
  9988. }) // MEMO: needed for update
  9989. .each(function(id, i) {
  9990. updatePositions(this, id, i);
  9991. })
  9992. ;(withTransition ? texts.transition() : texts)
  9993. .attr('x', xForLegendText)
  9994. .attr('y', yForLegendText);
  9995. rects = $$.legend.selectAll('rect.' + CLASS.legendItemEvent).data(targetIds)
  9996. ;(withTransition ? rects.transition() : rects)
  9997. .attr('width', function(id) {
  9998. return widths[id]
  9999. })
  10000. .attr('height', function(id) {
  10001. return heights[id]
  10002. })
  10003. .attr('x', xForLegendRect)
  10004. .attr('y', yForLegendRect);
  10005. tiles = $$.legend.selectAll('line.' + CLASS.legendItemTile).data(targetIds)
  10006. ;(withTransition ? tiles.transition() : tiles)
  10007. .style(
  10008. 'stroke',
  10009. $$.levelColor
  10010. ? function(id) {
  10011. return $$.levelColor(
  10012. $$.cache[id].values.reduce(function(total, item) {
  10013. return total + item.value
  10014. }, 0)
  10015. )
  10016. }
  10017. : $$.color
  10018. )
  10019. .attr('x1', x1ForLegendTile)
  10020. .attr('y1', yForLegendTile)
  10021. .attr('x2', x2ForLegendTile)
  10022. .attr('y2', yForLegendTile);
  10023. if (background) {
  10024. (withTransition ? background.transition() : background)
  10025. .attr('height', $$.getLegendHeight() - 12)
  10026. .attr('width', maxWidth * (step + 1) + 10);
  10027. }
  10028. // toggle legend state
  10029. $$.legend
  10030. .selectAll('.' + CLASS.legendItem)
  10031. .classed(CLASS.legendItemHidden, function(id) {
  10032. return !$$.isTargetToShow(id)
  10033. });
  10034. // Update all to reflect change of legend
  10035. $$.updateLegendItemWidth(maxWidth);
  10036. $$.updateLegendItemHeight(maxHeight);
  10037. $$.updateLegendStep(step);
  10038. // Update size and scale
  10039. $$.updateSizes();
  10040. $$.updateScales();
  10041. $$.updateSvgSize();
  10042. // Update g positions
  10043. $$.transformAll(withTransitionForTransform, transitions);
  10044. $$.legendHasRendered = true;
  10045. };
  10046. ChartInternal.prototype.initRegion = function() {
  10047. var $$ = this;
  10048. $$.region = $$.main
  10049. .append('g')
  10050. .attr('clip-path', $$.clipPath)
  10051. .attr('class', CLASS.regions);
  10052. };
  10053. ChartInternal.prototype.updateRegion = function(duration) {
  10054. var $$ = this,
  10055. config = $$.config;
  10056. // hide if arc type
  10057. $$.region.style('visibility', $$.hasArcType() ? 'hidden' : 'visible');
  10058. var mainRegion = $$.main
  10059. .select('.' + CLASS.regions)
  10060. .selectAll('.' + CLASS.region)
  10061. .data(config.regions);
  10062. var g = mainRegion.enter().append('g');
  10063. g.append('rect')
  10064. .attr('x', $$.regionX.bind($$))
  10065. .attr('y', $$.regionY.bind($$))
  10066. .attr('width', $$.regionWidth.bind($$))
  10067. .attr('height', $$.regionHeight.bind($$))
  10068. .style('fill-opacity', function(d) {
  10069. return isValue(d.opacity) ? d.opacity : 0.1
  10070. });
  10071. g.append('text').text($$.labelRegion.bind($$));
  10072. $$.mainRegion = g.merge(mainRegion).attr('class', $$.classRegion.bind($$));
  10073. mainRegion
  10074. .exit()
  10075. .transition()
  10076. .duration(duration)
  10077. .style('opacity', 0)
  10078. .remove();
  10079. };
  10080. ChartInternal.prototype.redrawRegion = function(withTransition, transition) {
  10081. var $$ = this,
  10082. regions = $$.mainRegion,
  10083. regionLabels = $$.mainRegion.selectAll('text');
  10084. return [
  10085. (withTransition ? regions.transition(transition) : regions)
  10086. .attr('x', $$.regionX.bind($$))
  10087. .attr('y', $$.regionY.bind($$))
  10088. .attr('width', $$.regionWidth.bind($$))
  10089. .attr('height', $$.regionHeight.bind($$))
  10090. .style('fill-opacity', function(d) {
  10091. return isValue(d.opacity) ? d.opacity : 0.1
  10092. }),
  10093. (withTransition ? regionLabels.transition(transition) : regionLabels)
  10094. .attr('x', $$.labelOffsetX.bind($$))
  10095. .attr('y', $$.labelOffsetY.bind($$))
  10096. .attr('transform', $$.labelTransform.bind($$))
  10097. .attr('style', 'text-anchor: left;')
  10098. ]
  10099. };
  10100. ChartInternal.prototype.regionX = function(d) {
  10101. var $$ = this,
  10102. config = $$.config,
  10103. xPos,
  10104. yScale = d.axis === 'y' ? $$.y : $$.y2;
  10105. if (d.axis === 'y' || d.axis === 'y2') {
  10106. xPos = config.axis_rotated ? ('start' in d ? yScale(d.start) : 0) : 0;
  10107. } else {
  10108. xPos = config.axis_rotated
  10109. ? 0
  10110. : 'start' in d
  10111. ? $$.x($$.isTimeSeries() ? $$.parseDate(d.start) : d.start)
  10112. : 0;
  10113. }
  10114. return xPos
  10115. };
  10116. ChartInternal.prototype.regionY = function(d) {
  10117. var $$ = this,
  10118. config = $$.config,
  10119. yPos,
  10120. yScale = d.axis === 'y' ? $$.y : $$.y2;
  10121. if (d.axis === 'y' || d.axis === 'y2') {
  10122. yPos = config.axis_rotated ? 0 : 'end' in d ? yScale(d.end) : 0;
  10123. } else {
  10124. yPos = config.axis_rotated
  10125. ? 'start' in d
  10126. ? $$.x($$.isTimeSeries() ? $$.parseDate(d.start) : d.start)
  10127. : 0
  10128. : 0;
  10129. }
  10130. return yPos
  10131. };
  10132. ChartInternal.prototype.regionWidth = function(d) {
  10133. var $$ = this,
  10134. config = $$.config,
  10135. start = $$.regionX(d),
  10136. end,
  10137. yScale = d.axis === 'y' ? $$.y : $$.y2;
  10138. if (d.axis === 'y' || d.axis === 'y2') {
  10139. end = config.axis_rotated
  10140. ? 'end' in d
  10141. ? yScale(d.end)
  10142. : $$.width
  10143. : $$.width;
  10144. } else {
  10145. end = config.axis_rotated
  10146. ? $$.width
  10147. : 'end' in d
  10148. ? $$.x($$.isTimeSeries() ? $$.parseDate(d.end) : d.end)
  10149. : $$.width;
  10150. }
  10151. return end < start ? 0 : end - start
  10152. };
  10153. ChartInternal.prototype.regionHeight = function(d) {
  10154. var $$ = this,
  10155. config = $$.config,
  10156. start = this.regionY(d),
  10157. end,
  10158. yScale = d.axis === 'y' ? $$.y : $$.y2;
  10159. if (d.axis === 'y' || d.axis === 'y2') {
  10160. end = config.axis_rotated
  10161. ? $$.height
  10162. : 'start' in d
  10163. ? yScale(d.start)
  10164. : $$.height;
  10165. } else {
  10166. end = config.axis_rotated
  10167. ? 'end' in d
  10168. ? $$.x($$.isTimeSeries() ? $$.parseDate(d.end) : d.end)
  10169. : $$.height
  10170. : $$.height;
  10171. }
  10172. return end < start ? 0 : end - start
  10173. };
  10174. ChartInternal.prototype.isRegionOnX = function(d) {
  10175. return !d.axis || d.axis === 'x'
  10176. };
  10177. ChartInternal.prototype.labelRegion = function(d) {
  10178. return 'label' in d ? d.label : ''
  10179. };
  10180. ChartInternal.prototype.labelTransform = function(d) {
  10181. return 'vertical' in d && d.vertical ? 'rotate(90)' : ''
  10182. };
  10183. ChartInternal.prototype.labelOffsetX = function(d) {
  10184. var paddingX = 'paddingX' in d ? d.paddingX : 3;
  10185. var paddingY = 'paddingY' in d ? d.paddingY : 3;
  10186. return 'vertical' in d && d.vertical
  10187. ? this.regionY(d) + paddingY
  10188. : this.regionX(d) + paddingX
  10189. };
  10190. ChartInternal.prototype.labelOffsetY = function(d) {
  10191. var paddingX = 'paddingX' in d ? d.paddingX : 3;
  10192. var paddingY = 'paddingY' in d ? d.paddingY : 3;
  10193. return 'vertical' in d && d.vertical
  10194. ? -(this.regionX(d) + paddingX)
  10195. : this.regionY(d) + 10 + paddingY
  10196. };
  10197. function c3LogScale(d3, linearScale, logScale) {
  10198. var PROJECTION = [0.01, 10];
  10199. if (!linearScale) {
  10200. linearScale = d3.scaleLinear();
  10201. linearScale.range(PROJECTION);
  10202. }
  10203. if (!logScale) {
  10204. logScale = d3.scaleLog();
  10205. logScale.domain(PROJECTION);
  10206. logScale.nice();
  10207. }
  10208. // copied from https://github.com/compute-io/logspace
  10209. function logspace(a, b, len) {
  10210. var arr, end, tmp, d;
  10211. if (arguments.length < 3) {
  10212. len = 10;
  10213. } else {
  10214. if (len === 0) {
  10215. return []
  10216. }
  10217. }
  10218. // Calculate the increment:
  10219. end = len - 1;
  10220. d = (b - a) / end;
  10221. // Build the output array...
  10222. arr = new Array(len);
  10223. tmp = a;
  10224. arr[0] = Math.pow(10, tmp);
  10225. for (var i = 1; i < end; i++) {
  10226. tmp += d;
  10227. arr[i] = Math.pow(10, tmp);
  10228. }
  10229. arr[end] = Math.pow(10, b);
  10230. return arr
  10231. }
  10232. function scale(x) {
  10233. return logScale(linearScale(x))
  10234. }
  10235. scale.domain = function(x) {
  10236. if (!arguments.length) {
  10237. return linearScale.domain()
  10238. }
  10239. linearScale.domain(x);
  10240. return scale
  10241. };
  10242. scale.range = function(x) {
  10243. if (!arguments.length) {
  10244. return logScale.range()
  10245. }
  10246. logScale.range(x);
  10247. return scale
  10248. };
  10249. scale.ticks = function(m) {
  10250. return logspace(-2, 1, m || 10).map(function(v) {
  10251. return linearScale.invert(v)
  10252. })
  10253. };
  10254. scale.copy = function() {
  10255. return c3LogScale(d3, linearScale.copy(), logScale.copy())
  10256. };
  10257. return scale
  10258. }
  10259. ChartInternal.prototype.getScale = function(min, max, forTimeseries) {
  10260. return (forTimeseries ? this.d3.scaleTime() : this.d3.scaleLinear()).range([
  10261. min,
  10262. max
  10263. ])
  10264. };
  10265. ChartInternal.prototype.getX = function(min, max, domain, offset) {
  10266. var $$ = this,
  10267. scale = $$.getScale(min, max, $$.isTimeSeries()),
  10268. _scale = domain ? scale.domain(domain) : scale,
  10269. key;
  10270. // Define customized scale if categorized axis
  10271. if ($$.isCategorized()) {
  10272. offset =
  10273. offset ||
  10274. function() {
  10275. return 0
  10276. };
  10277. scale = function(d, raw) {
  10278. var v = _scale(d) + offset(d);
  10279. return raw ? v : Math.ceil(v)
  10280. };
  10281. } else {
  10282. scale = function(d, raw) {
  10283. var v = _scale(d);
  10284. return raw ? v : Math.ceil(v)
  10285. };
  10286. }
  10287. // define functions
  10288. for (key in _scale) {
  10289. scale[key] = _scale[key];
  10290. }
  10291. scale.orgDomain = function() {
  10292. return _scale.domain()
  10293. };
  10294. // define custom domain() for categorized axis
  10295. if ($$.isCategorized()) {
  10296. scale.domain = function(domain) {
  10297. if (!arguments.length) {
  10298. domain = this.orgDomain();
  10299. return [domain[0], domain[1] + 1]
  10300. }
  10301. _scale.domain(domain);
  10302. return scale
  10303. };
  10304. }
  10305. return scale
  10306. };
  10307. /**
  10308. * Creates and configures a D3 scale instance for the given type.
  10309. *
  10310. * By defaults it returns a Linear scale.
  10311. *
  10312. * @param {String} type Type of d3-scale to create. Type can be 'linear', 'time', 'timeseries' or 'log'.
  10313. * @param {Array} domain The scale domain such as [from, to]
  10314. * @param {Array} range The scale's range such as [from, to]
  10315. *
  10316. * @return A d3-scale instance
  10317. */
  10318. ChartInternal.prototype.getY = function(type, domain, range) {
  10319. let scale;
  10320. if (type === 'timeseries' || type === 'time') {
  10321. scale = this.d3.scaleTime();
  10322. } else if (type === 'log') {
  10323. scale = c3LogScale(this.d3);
  10324. } else if (type === 'linear' || type === undefined) {
  10325. scale = this.d3.scaleLinear();
  10326. } else {
  10327. throw new Error(`Invalid Y axis type: "${type}"`)
  10328. }
  10329. if (domain) {
  10330. scale.domain(domain);
  10331. }
  10332. if (range) {
  10333. scale.range(range);
  10334. }
  10335. return scale
  10336. };
  10337. ChartInternal.prototype.getYScale = function(id) {
  10338. return this.axis.getId(id) === 'y2' ? this.y2 : this.y
  10339. };
  10340. ChartInternal.prototype.getSubYScale = function(id) {
  10341. return this.axis.getId(id) === 'y2' ? this.subY2 : this.subY
  10342. };
  10343. ChartInternal.prototype.updateScales = function() {
  10344. var $$ = this,
  10345. config = $$.config,
  10346. forInit = !$$.x;
  10347. // update edges
  10348. $$.xMin = config.axis_rotated ? 1 : 0;
  10349. $$.xMax = config.axis_rotated ? $$.height : $$.width;
  10350. $$.yMin = config.axis_rotated ? 0 : $$.height;
  10351. $$.yMax = config.axis_rotated ? $$.width : 1;
  10352. $$.subXMin = $$.xMin;
  10353. $$.subXMax = $$.xMax;
  10354. $$.subYMin = config.axis_rotated ? 0 : $$.height2;
  10355. $$.subYMax = config.axis_rotated ? $$.width2 : 1;
  10356. // update scales
  10357. $$.x = $$.getX(
  10358. $$.xMin,
  10359. $$.xMax,
  10360. forInit ? undefined : $$.x.orgDomain(),
  10361. function() {
  10362. return $$.xAxis.tickOffset()
  10363. }
  10364. );
  10365. $$.y = $$.getY(
  10366. config.axis_y_type,
  10367. forInit ? config.axis_y_default : $$.y.domain(),
  10368. [$$.yMin, $$.yMax]
  10369. );
  10370. $$.y2 = $$.getY(
  10371. config.axis_y2_type,
  10372. forInit ? config.axis_y2_default : $$.y2.domain(),
  10373. [$$.yMin, $$.yMax]
  10374. );
  10375. $$.subX = $$.getX($$.xMin, $$.xMax, $$.orgXDomain, function(d) {
  10376. return d % 1 ? 0 : $$.subXAxis.tickOffset()
  10377. });
  10378. $$.subY = $$.getY(
  10379. config.axis_y_type,
  10380. forInit ? config.axis_y_default : $$.subY.domain(),
  10381. [$$.subYMin, $$.subYMax]
  10382. );
  10383. $$.subY2 = $$.getY(
  10384. config.axis_y2_type,
  10385. forInit ? config.axis_y2_default : $$.subY2.domain(),
  10386. [$$.subYMin, $$.subYMax]
  10387. );
  10388. // update axes
  10389. $$.xAxisTickFormat = $$.axis.getXAxisTickFormat();
  10390. $$.xAxisTickValues = $$.axis.getXAxisTickValues();
  10391. $$.yAxisTickValues = $$.axis.getYAxisTickValues();
  10392. $$.y2AxisTickValues = $$.axis.getY2AxisTickValues();
  10393. $$.xAxis = $$.axis.getXAxis(
  10394. $$.x,
  10395. $$.xOrient,
  10396. $$.xAxisTickFormat,
  10397. $$.xAxisTickValues,
  10398. config.axis_x_tick_outer
  10399. );
  10400. $$.subXAxis = $$.axis.getXAxis(
  10401. $$.subX,
  10402. $$.subXOrient,
  10403. $$.xAxisTickFormat,
  10404. $$.xAxisTickValues,
  10405. config.axis_x_tick_outer
  10406. );
  10407. $$.yAxis = $$.axis.getYAxis(
  10408. 'y',
  10409. $$.y,
  10410. $$.yOrient,
  10411. $$.yAxisTickValues,
  10412. config.axis_y_tick_outer
  10413. );
  10414. $$.y2Axis = $$.axis.getYAxis(
  10415. 'y2',
  10416. $$.y2,
  10417. $$.y2Orient,
  10418. $$.y2AxisTickValues,
  10419. config.axis_y2_tick_outer
  10420. );
  10421. // Set initialized scales to brush and zoom
  10422. if (!forInit) {
  10423. if ($$.brush) {
  10424. $$.brush.updateScale($$.subX);
  10425. }
  10426. }
  10427. // update for arc
  10428. if ($$.updateArc) {
  10429. $$.updateArc();
  10430. }
  10431. };
  10432. ChartInternal.prototype.selectPoint = function(target, d, i) {
  10433. var $$ = this,
  10434. config = $$.config,
  10435. cx = (config.axis_rotated ? $$.circleY : $$.circleX).bind($$),
  10436. cy = (config.axis_rotated ? $$.circleX : $$.circleY).bind($$),
  10437. r = $$.pointSelectR.bind($$);
  10438. config.data_onselected.call($$.api, d, target.node());
  10439. // add selected-circle on low layer g
  10440. $$.main
  10441. .select('.' + CLASS.selectedCircles + $$.getTargetSelectorSuffix(d.id))
  10442. .selectAll('.' + CLASS.selectedCircle + '-' + i)
  10443. .data([d])
  10444. .enter()
  10445. .append('circle')
  10446. .attr('class', function() {
  10447. return $$.generateClass(CLASS.selectedCircle, i)
  10448. })
  10449. .attr('cx', cx)
  10450. .attr('cy', cy)
  10451. .attr('stroke', function() {
  10452. return $$.color(d)
  10453. })
  10454. .attr('r', function(d) {
  10455. return $$.pointSelectR(d) * 1.4
  10456. })
  10457. .transition()
  10458. .duration(100)
  10459. .attr('r', r);
  10460. };
  10461. ChartInternal.prototype.unselectPoint = function(target, d, i) {
  10462. var $$ = this;
  10463. $$.config.data_onunselected.call($$.api, d, target.node());
  10464. // remove selected-circle from low layer g
  10465. $$.main
  10466. .select('.' + CLASS.selectedCircles + $$.getTargetSelectorSuffix(d.id))
  10467. .selectAll('.' + CLASS.selectedCircle + '-' + i)
  10468. .transition()
  10469. .duration(100)
  10470. .attr('r', 0)
  10471. .remove();
  10472. };
  10473. ChartInternal.prototype.togglePoint = function(selected, target, d, i) {
  10474. selected ? this.selectPoint(target, d, i) : this.unselectPoint(target, d, i);
  10475. };
  10476. ChartInternal.prototype.selectPath = function(target, d) {
  10477. var $$ = this;
  10478. $$.config.data_onselected.call($$, d, target.node());
  10479. if ($$.config.interaction_brighten) {
  10480. target
  10481. .transition()
  10482. .duration(100)
  10483. .style('fill', function() {
  10484. return $$.d3.rgb($$.color(d)).brighter(0.75)
  10485. });
  10486. }
  10487. };
  10488. ChartInternal.prototype.unselectPath = function(target, d) {
  10489. var $$ = this;
  10490. $$.config.data_onunselected.call($$, d, target.node());
  10491. if ($$.config.interaction_brighten) {
  10492. target
  10493. .transition()
  10494. .duration(100)
  10495. .style('fill', function() {
  10496. return $$.color(d)
  10497. });
  10498. }
  10499. };
  10500. ChartInternal.prototype.togglePath = function(selected, target, d, i) {
  10501. selected ? this.selectPath(target, d, i) : this.unselectPath(target, d, i);
  10502. };
  10503. ChartInternal.prototype.getToggle = function(that, d) {
  10504. var $$ = this,
  10505. toggle;
  10506. if (that.nodeName === 'circle') {
  10507. if ($$.isStepType(d)) {
  10508. // circle is hidden in step chart, so treat as within the click area
  10509. toggle = function() {}; // TODO: how to select step chart?
  10510. } else {
  10511. toggle = $$.togglePoint;
  10512. }
  10513. } else if (that.nodeName === 'path') {
  10514. toggle = $$.togglePath;
  10515. }
  10516. return toggle
  10517. };
  10518. ChartInternal.prototype.toggleShape = function(that, d, i) {
  10519. var $$ = this,
  10520. d3 = $$.d3,
  10521. config = $$.config,
  10522. shape = d3.select(that),
  10523. isSelected = shape.classed(CLASS.SELECTED),
  10524. toggle = $$.getToggle(that, d).bind($$);
  10525. if (config.data_selection_enabled && config.data_selection_isselectable(d)) {
  10526. if (!config.data_selection_multiple) {
  10527. $$.main
  10528. .selectAll(
  10529. '.' +
  10530. CLASS.shapes +
  10531. (config.data_selection_grouped
  10532. ? $$.getTargetSelectorSuffix(d.id)
  10533. : '')
  10534. )
  10535. .selectAll('.' + CLASS.shape)
  10536. .each(function(d, i) {
  10537. var shape = d3.select(this);
  10538. if (shape.classed(CLASS.SELECTED)) {
  10539. toggle(false, shape.classed(CLASS.SELECTED, false), d, i);
  10540. }
  10541. });
  10542. }
  10543. shape.classed(CLASS.SELECTED, !isSelected);
  10544. toggle(!isSelected, shape, d, i);
  10545. }
  10546. };
  10547. ChartInternal.prototype.initBar = function() {
  10548. var $$ = this;
  10549. $$.main
  10550. .select('.' + CLASS.chart)
  10551. .append('g')
  10552. .attr('class', CLASS.chartBars);
  10553. };
  10554. ChartInternal.prototype.updateTargetsForBar = function(targets) {
  10555. var $$ = this,
  10556. config = $$.config,
  10557. mainBars,
  10558. mainBarEnter,
  10559. classChartBar = $$.classChartBar.bind($$),
  10560. classBars = $$.classBars.bind($$),
  10561. classFocus = $$.classFocus.bind($$);
  10562. mainBars = $$.main
  10563. .select('.' + CLASS.chartBars)
  10564. .selectAll('.' + CLASS.chartBar)
  10565. .data(targets)
  10566. .attr('class', function(d) {
  10567. return classChartBar(d) + classFocus(d)
  10568. });
  10569. mainBarEnter = mainBars
  10570. .enter()
  10571. .append('g')
  10572. .attr('class', classChartBar)
  10573. .style('pointer-events', 'none');
  10574. // Bars for each data
  10575. mainBarEnter
  10576. .append('g')
  10577. .attr('class', classBars)
  10578. .style('cursor', function(d) {
  10579. return config.data_selection_isselectable(d) ? 'pointer' : null
  10580. });
  10581. };
  10582. ChartInternal.prototype.updateBar = function(durationForExit) {
  10583. var $$ = this,
  10584. barData = $$.barData.bind($$),
  10585. classBar = $$.classBar.bind($$),
  10586. initialOpacity = $$.initialOpacity.bind($$),
  10587. color = function(d) {
  10588. return $$.color(d.id)
  10589. };
  10590. var mainBar = $$.main
  10591. .selectAll('.' + CLASS.bars)
  10592. .selectAll('.' + CLASS.bar)
  10593. .data(barData);
  10594. var mainBarEnter = mainBar
  10595. .enter()
  10596. .append('path')
  10597. .attr('class', classBar)
  10598. .style('stroke', color)
  10599. .style('fill', color);
  10600. $$.mainBar = mainBarEnter.merge(mainBar).style('opacity', initialOpacity);
  10601. mainBar
  10602. .exit()
  10603. .transition()
  10604. .duration(durationForExit)
  10605. .style('opacity', 0);
  10606. };
  10607. ChartInternal.prototype.redrawBar = function(
  10608. drawBar,
  10609. withTransition,
  10610. transition
  10611. ) {
  10612. const $$ = this;
  10613. return [
  10614. (withTransition ? this.mainBar.transition(transition) : this.mainBar)
  10615. .attr('d', drawBar)
  10616. .style('stroke', this.color)
  10617. .style('fill', this.color)
  10618. .style('opacity', d => ($$.isTargetToShow(d.id) ? 1 : 0))
  10619. ]
  10620. };
  10621. ChartInternal.prototype.getBarW = function(axis, barTargetsNum) {
  10622. var $$ = this,
  10623. config = $$.config,
  10624. w =
  10625. typeof config.bar_width === 'number'
  10626. ? config.bar_width
  10627. : barTargetsNum
  10628. ? (axis.tickInterval() * config.bar_width_ratio) / barTargetsNum
  10629. : 0;
  10630. return config.bar_width_max && w > config.bar_width_max
  10631. ? config.bar_width_max
  10632. : w
  10633. };
  10634. ChartInternal.prototype.getBars = function(i, id) {
  10635. var $$ = this;
  10636. return (id
  10637. ? $$.main.selectAll('.' + CLASS.bars + $$.getTargetSelectorSuffix(id))
  10638. : $$.main
  10639. ).selectAll('.' + CLASS.bar + (isValue(i) ? '-' + i : ''))
  10640. };
  10641. ChartInternal.prototype.expandBars = function(i, id, reset) {
  10642. var $$ = this;
  10643. if (reset) {
  10644. $$.unexpandBars();
  10645. }
  10646. $$.getBars(i, id).classed(CLASS.EXPANDED, true);
  10647. };
  10648. ChartInternal.prototype.unexpandBars = function(i) {
  10649. var $$ = this;
  10650. $$.getBars(i).classed(CLASS.EXPANDED, false);
  10651. };
  10652. ChartInternal.prototype.generateDrawBar = function(barIndices, isSub) {
  10653. var $$ = this,
  10654. config = $$.config,
  10655. getPoints = $$.generateGetBarPoints(barIndices, isSub);
  10656. return function(d, i) {
  10657. // 4 points that make a bar
  10658. var points = getPoints(d, i);
  10659. // switch points if axis is rotated, not applicable for sub chart
  10660. var indexX = config.axis_rotated ? 1 : 0;
  10661. var indexY = config.axis_rotated ? 0 : 1;
  10662. var path =
  10663. 'M ' +
  10664. points[0][indexX] +
  10665. ',' +
  10666. points[0][indexY] +
  10667. ' ' +
  10668. 'L' +
  10669. points[1][indexX] +
  10670. ',' +
  10671. points[1][indexY] +
  10672. ' ' +
  10673. 'L' +
  10674. points[2][indexX] +
  10675. ',' +
  10676. points[2][indexY] +
  10677. ' ' +
  10678. 'L' +
  10679. points[3][indexX] +
  10680. ',' +
  10681. points[3][indexY] +
  10682. ' ' +
  10683. 'z';
  10684. return path
  10685. }
  10686. };
  10687. ChartInternal.prototype.generateGetBarPoints = function(barIndices, isSub) {
  10688. var $$ = this,
  10689. axis = isSub ? $$.subXAxis : $$.xAxis,
  10690. barTargetsNum = barIndices.__max__ + 1,
  10691. barW = $$.getBarW(axis, barTargetsNum),
  10692. barX = $$.getShapeX(barW, barTargetsNum, barIndices, !!isSub),
  10693. barY = $$.getShapeY(!!isSub),
  10694. barOffset = $$.getShapeOffset($$.isBarType, barIndices, !!isSub),
  10695. barSpaceOffset = barW * ($$.config.bar_space / 2),
  10696. yScale = isSub ? $$.getSubYScale : $$.getYScale;
  10697. return function(d, i) {
  10698. var y0 = yScale.call($$, d.id)(0),
  10699. offset = barOffset(d, i) || y0, // offset is for stacked bar chart
  10700. posX = barX(d),
  10701. posY = barY(d);
  10702. // fix posY not to overflow opposite quadrant
  10703. if ($$.config.axis_rotated) {
  10704. if ((0 < d.value && posY < y0) || (d.value < 0 && y0 < posY)) {
  10705. posY = y0;
  10706. }
  10707. }
  10708. posY -= y0 - offset;
  10709. // 4 points that make a bar
  10710. return [
  10711. [posX + barSpaceOffset, offset],
  10712. [posX + barSpaceOffset, posY],
  10713. [posX + barW - barSpaceOffset, posY],
  10714. [posX + barW - barSpaceOffset, offset]
  10715. ]
  10716. }
  10717. };
  10718. /**
  10719. * Returns whether the data point is within the given bar shape.
  10720. *
  10721. * @param mouse
  10722. * @param barShape
  10723. * @return {boolean}
  10724. */
  10725. ChartInternal.prototype.isWithinBar = function(mouse, barShape) {
  10726. return isWithinBox(mouse, getBBox(barShape), 2)
  10727. };
  10728. ChartInternal.prototype.getShapeIndices = function(typeFilter) {
  10729. var $$ = this,
  10730. config = $$.config,
  10731. indices = {},
  10732. i = 0,
  10733. j,
  10734. k;
  10735. $$.filterTargetsToShow($$.data.targets.filter(typeFilter, $$)).forEach(
  10736. function(d) {
  10737. for (j = 0; j < config.data_groups.length; j++) {
  10738. if (config.data_groups[j].indexOf(d.id) < 0) {
  10739. continue
  10740. }
  10741. for (k = 0; k < config.data_groups[j].length; k++) {
  10742. if (config.data_groups[j][k] in indices) {
  10743. indices[d.id] = indices[config.data_groups[j][k]];
  10744. break
  10745. }
  10746. }
  10747. }
  10748. if (isUndefined(indices[d.id])) {
  10749. indices[d.id] = i++;
  10750. }
  10751. }
  10752. );
  10753. indices.__max__ = i - 1;
  10754. return indices
  10755. };
  10756. ChartInternal.prototype.getShapeX = function(
  10757. offset,
  10758. targetsNum,
  10759. indices,
  10760. isSub
  10761. ) {
  10762. var $$ = this,
  10763. scale = isSub ? $$.subX : $$.x;
  10764. return function(d) {
  10765. var index = d.id in indices ? indices[d.id] : 0;
  10766. return d.x || d.x === 0 ? scale(d.x) - offset * (targetsNum / 2 - index) : 0
  10767. }
  10768. };
  10769. ChartInternal.prototype.getShapeY = function(isSub) {
  10770. const $$ = this;
  10771. return function(d) {
  10772. const scale = isSub ? $$.getSubYScale(d.id) : $$.getYScale(d.id);
  10773. return scale(
  10774. $$.isTargetNormalized(d.id) ? $$.getRatio('index', d, true) : d.value
  10775. )
  10776. }
  10777. };
  10778. ChartInternal.prototype.getShapeOffset = function(typeFilter, indices, isSub) {
  10779. var $$ = this,
  10780. targets = $$.orderTargets(
  10781. $$.filterTargetsToShow($$.data.targets.filter(typeFilter, $$))
  10782. ),
  10783. targetIds = targets.map(function(t) {
  10784. return t.id
  10785. });
  10786. return function(d, i) {
  10787. var scale = isSub ? $$.getSubYScale(d.id) : $$.getYScale(d.id),
  10788. y0 = scale(0),
  10789. offset = y0;
  10790. targets.forEach(function(t) {
  10791. const rowValues = $$.isStepType(d)
  10792. ? $$.convertValuesToStep(t.values)
  10793. : t.values;
  10794. const isTargetNormalized = $$.isTargetNormalized(d.id);
  10795. const values = rowValues.map(v =>
  10796. isTargetNormalized ? $$.getRatio('index', v, true) : v.value
  10797. );
  10798. if (t.id === d.id || indices[t.id] !== indices[d.id]) {
  10799. return
  10800. }
  10801. if (targetIds.indexOf(t.id) < targetIds.indexOf(d.id)) {
  10802. // check if the x values line up
  10803. if (isUndefined(rowValues[i]) || +rowValues[i].x !== +d.x) {
  10804. // "+" for timeseries
  10805. // if not, try to find the value that does line up
  10806. i = -1;
  10807. rowValues.forEach(function(v, j) {
  10808. const x1 = v.x.constructor === Date ? +v.x : v.x;
  10809. const x2 = d.x.constructor === Date ? +d.x : d.x;
  10810. if (x1 === x2) {
  10811. i = j;
  10812. }
  10813. });
  10814. }
  10815. if (i in rowValues && rowValues[i].value * d.value >= 0) {
  10816. offset += scale(values[i]) - y0;
  10817. }
  10818. }
  10819. });
  10820. return offset
  10821. }
  10822. };
  10823. ChartInternal.prototype.isWithinShape = function(that, d) {
  10824. var $$ = this,
  10825. shape = $$.d3.select(that),
  10826. isWithin;
  10827. if (!$$.isTargetToShow(d.id)) {
  10828. isWithin = false;
  10829. } else if (that.nodeName === 'circle') {
  10830. isWithin = $$.isStepType(d)
  10831. ? $$.isWithinStep(that, $$.getYScale(d.id)(d.value))
  10832. : $$.isWithinCircle(that, $$.pointSelectR(d) * 1.5);
  10833. } else if (that.nodeName === 'path') {
  10834. isWithin = shape.classed(CLASS.bar)
  10835. ? $$.isWithinBar($$.d3.mouse(that), that)
  10836. : true;
  10837. }
  10838. return isWithin
  10839. };
  10840. ChartInternal.prototype.getInterpolate = function(d) {
  10841. var $$ = this,
  10842. d3 = $$.d3,
  10843. types = {
  10844. linear: d3.curveLinear,
  10845. 'linear-closed': d3.curveLinearClosed,
  10846. basis: d3.curveBasis,
  10847. 'basis-open': d3.curveBasisOpen,
  10848. 'basis-closed': d3.curveBasisClosed,
  10849. bundle: d3.curveBundle,
  10850. cardinal: d3.curveCardinal,
  10851. 'cardinal-open': d3.curveCardinalOpen,
  10852. 'cardinal-closed': d3.curveCardinalClosed,
  10853. monotone: d3.curveMonotoneX,
  10854. step: d3.curveStep,
  10855. 'step-before': d3.curveStepBefore,
  10856. 'step-after': d3.curveStepAfter
  10857. },
  10858. type;
  10859. if ($$.isSplineType(d)) {
  10860. type = types[$$.config.spline_interpolation_type] || types.cardinal;
  10861. } else if ($$.isStepType(d)) {
  10862. type = types[$$.config.line_step_type];
  10863. } else {
  10864. type = types.linear;
  10865. }
  10866. return type
  10867. };
  10868. ChartInternal.prototype.initLine = function() {
  10869. var $$ = this;
  10870. $$.main
  10871. .select('.' + CLASS.chart)
  10872. .append('g')
  10873. .attr('class', CLASS.chartLines);
  10874. };
  10875. ChartInternal.prototype.updateTargetsForLine = function(targets) {
  10876. var $$ = this,
  10877. config = $$.config,
  10878. mainLines,
  10879. mainLineEnter,
  10880. classChartLine = $$.classChartLine.bind($$),
  10881. classLines = $$.classLines.bind($$),
  10882. classAreas = $$.classAreas.bind($$),
  10883. classCircles = $$.classCircles.bind($$),
  10884. classFocus = $$.classFocus.bind($$);
  10885. mainLines = $$.main
  10886. .select('.' + CLASS.chartLines)
  10887. .selectAll('.' + CLASS.chartLine)
  10888. .data(targets)
  10889. .attr('class', function(d) {
  10890. return classChartLine(d) + classFocus(d)
  10891. });
  10892. mainLineEnter = mainLines
  10893. .enter()
  10894. .append('g')
  10895. .attr('class', classChartLine)
  10896. .style('opacity', 0)
  10897. .style('pointer-events', 'none');
  10898. // Lines for each data
  10899. mainLineEnter.append('g').attr('class', classLines);
  10900. // Areas
  10901. mainLineEnter.append('g').attr('class', classAreas);
  10902. // Circles for each data point on lines
  10903. mainLineEnter.append('g').attr('class', function(d) {
  10904. return $$.generateClass(CLASS.selectedCircles, d.id)
  10905. });
  10906. mainLineEnter
  10907. .append('g')
  10908. .attr('class', classCircles)
  10909. .style('cursor', function(d) {
  10910. return config.data_selection_isselectable(d) ? 'pointer' : null
  10911. });
  10912. // Update date for selected circles
  10913. targets.forEach(function(t) {
  10914. $$.main
  10915. .selectAll('.' + CLASS.selectedCircles + $$.getTargetSelectorSuffix(t.id))
  10916. .selectAll('.' + CLASS.selectedCircle)
  10917. .each(function(d) {
  10918. d.value = t.values[d.index].value;
  10919. });
  10920. });
  10921. // MEMO: can not keep same color...
  10922. //mainLineUpdate.exit().remove();
  10923. };
  10924. ChartInternal.prototype.updateLine = function(durationForExit) {
  10925. var $$ = this;
  10926. var mainLine = $$.main
  10927. .selectAll('.' + CLASS.lines)
  10928. .selectAll('.' + CLASS.line)
  10929. .data($$.lineData.bind($$));
  10930. var mainLineEnter = mainLine
  10931. .enter()
  10932. .append('path')
  10933. .attr('class', $$.classLine.bind($$))
  10934. .style('stroke', $$.color);
  10935. $$.mainLine = mainLineEnter
  10936. .merge(mainLine)
  10937. .style('opacity', $$.initialOpacity.bind($$))
  10938. .style('shape-rendering', function(d) {
  10939. return $$.isStepType(d) ? 'crispEdges' : ''
  10940. })
  10941. .attr('transform', null);
  10942. mainLine
  10943. .exit()
  10944. .transition()
  10945. .duration(durationForExit)
  10946. .style('opacity', 0);
  10947. };
  10948. ChartInternal.prototype.redrawLine = function(
  10949. drawLine,
  10950. withTransition,
  10951. transition
  10952. ) {
  10953. return [
  10954. (withTransition ? this.mainLine.transition(transition) : this.mainLine)
  10955. .attr('d', drawLine)
  10956. .style('stroke', this.color)
  10957. .style('opacity', 1)
  10958. ]
  10959. };
  10960. ChartInternal.prototype.generateDrawLine = function(lineIndices, isSub) {
  10961. var $$ = this,
  10962. config = $$.config,
  10963. line = $$.d3.line(),
  10964. getPoints = $$.generateGetLinePoints(lineIndices, isSub),
  10965. yScaleGetter = isSub ? $$.getSubYScale : $$.getYScale,
  10966. xValue = function(d) {
  10967. return (isSub ? $$.subxx : $$.xx).call($$, d)
  10968. },
  10969. yValue = function(d, i) {
  10970. return config.data_groups.length > 0
  10971. ? getPoints(d, i)[0][1]
  10972. : yScaleGetter.call($$, d.id)(d.value)
  10973. };
  10974. line = config.axis_rotated
  10975. ? line.x(yValue).y(xValue)
  10976. : line.x(xValue).y(yValue);
  10977. if (!config.line_connectNull) {
  10978. line = line.defined(function(d) {
  10979. return d.value != null
  10980. });
  10981. }
  10982. return function(d) {
  10983. var values = config.line_connectNull
  10984. ? $$.filterRemoveNull(d.values)
  10985. : d.values,
  10986. x = isSub ? $$.subX : $$.x,
  10987. y = yScaleGetter.call($$, d.id),
  10988. x0 = 0,
  10989. y0 = 0,
  10990. path;
  10991. if ($$.isLineType(d)) {
  10992. if (config.data_regions[d.id]) {
  10993. path = $$.lineWithRegions(values, x, y, config.data_regions[d.id]);
  10994. } else {
  10995. if ($$.isStepType(d)) {
  10996. values = $$.convertValuesToStep(values);
  10997. }
  10998. path = line.curve($$.getInterpolate(d))(values);
  10999. }
  11000. } else {
  11001. if (values[0]) {
  11002. x0 = x(values[0].x);
  11003. y0 = y(values[0].value);
  11004. }
  11005. path = config.axis_rotated ? 'M ' + y0 + ' ' + x0 : 'M ' + x0 + ' ' + y0;
  11006. }
  11007. return path ? path : 'M 0 0'
  11008. }
  11009. };
  11010. ChartInternal.prototype.generateGetLinePoints = function(lineIndices, isSub) {
  11011. // partial duplication of generateGetBarPoints
  11012. var $$ = this,
  11013. config = $$.config,
  11014. lineTargetsNum = lineIndices.__max__ + 1,
  11015. x = $$.getShapeX(0, lineTargetsNum, lineIndices, !!isSub),
  11016. y = $$.getShapeY(!!isSub),
  11017. lineOffset = $$.getShapeOffset($$.isLineType, lineIndices, !!isSub),
  11018. yScale = isSub ? $$.getSubYScale : $$.getYScale;
  11019. return function(d, i) {
  11020. var y0 = yScale.call($$, d.id)(0),
  11021. offset = lineOffset(d, i) || y0, // offset is for stacked area chart
  11022. posX = x(d),
  11023. posY = y(d);
  11024. // fix posY not to overflow opposite quadrant
  11025. if (config.axis_rotated) {
  11026. if ((0 < d.value && posY < y0) || (d.value < 0 && y0 < posY)) {
  11027. posY = y0;
  11028. }
  11029. }
  11030. // 1 point that marks the line position
  11031. return [
  11032. [posX, posY - (y0 - offset)],
  11033. [posX, posY - (y0 - offset)], // needed for compatibility
  11034. [posX, posY - (y0 - offset)], // needed for compatibility
  11035. [posX, posY - (y0 - offset)] // needed for compatibility
  11036. ]
  11037. }
  11038. };
  11039. ChartInternal.prototype.lineWithRegions = function(d, x, y, _regions) {
  11040. var $$ = this,
  11041. config = $$.config,
  11042. prev = -1,
  11043. i,
  11044. j,
  11045. s = 'M',
  11046. sWithRegion,
  11047. xp,
  11048. yp,
  11049. dx,
  11050. dy,
  11051. dd,
  11052. diff,
  11053. diffx2,
  11054. xOffset = $$.isCategorized() ? 0.5 : 0,
  11055. xValue,
  11056. yValue,
  11057. regions = [];
  11058. function isWithinRegions(x, regions) {
  11059. var i;
  11060. for (i = 0; i < regions.length; i++) {
  11061. if (regions[i].start < x && x <= regions[i].end) {
  11062. return true
  11063. }
  11064. }
  11065. return false
  11066. }
  11067. // Check start/end of regions
  11068. if (isDefined(_regions)) {
  11069. for (i = 0; i < _regions.length; i++) {
  11070. regions[i] = {};
  11071. if (isUndefined(_regions[i].start)) {
  11072. regions[i].start = d[0].x;
  11073. } else {
  11074. regions[i].start = $$.isTimeSeries()
  11075. ? $$.parseDate(_regions[i].start)
  11076. : _regions[i].start;
  11077. }
  11078. if (isUndefined(_regions[i].end)) {
  11079. regions[i].end = d[d.length - 1].x;
  11080. } else {
  11081. regions[i].end = $$.isTimeSeries()
  11082. ? $$.parseDate(_regions[i].end)
  11083. : _regions[i].end;
  11084. }
  11085. }
  11086. }
  11087. // Set scales
  11088. xValue = config.axis_rotated
  11089. ? function(d) {
  11090. return y(d.value)
  11091. }
  11092. : function(d) {
  11093. return x(d.x)
  11094. };
  11095. yValue = config.axis_rotated
  11096. ? function(d) {
  11097. return x(d.x)
  11098. }
  11099. : function(d) {
  11100. return y(d.value)
  11101. };
  11102. // Define svg generator function for region
  11103. function generateM(points) {
  11104. return (
  11105. 'M' +
  11106. points[0][0] +
  11107. ' ' +
  11108. points[0][1] +
  11109. ' ' +
  11110. points[1][0] +
  11111. ' ' +
  11112. points[1][1]
  11113. )
  11114. }
  11115. if ($$.isTimeSeries()) {
  11116. sWithRegion = function(d0, d1, j, diff) {
  11117. var x0 = d0.x.getTime(),
  11118. x_diff = d1.x - d0.x,
  11119. xv0 = new Date(x0 + x_diff * j),
  11120. xv1 = new Date(x0 + x_diff * (j + diff)),
  11121. points;
  11122. if (config.axis_rotated) {
  11123. points = [
  11124. [y(yp(j)), x(xv0)],
  11125. [y(yp(j + diff)), x(xv1)]
  11126. ];
  11127. } else {
  11128. points = [
  11129. [x(xv0), y(yp(j))],
  11130. [x(xv1), y(yp(j + diff))]
  11131. ];
  11132. }
  11133. return generateM(points)
  11134. };
  11135. } else {
  11136. sWithRegion = function(d0, d1, j, diff) {
  11137. var points;
  11138. if (config.axis_rotated) {
  11139. points = [
  11140. [y(yp(j), true), x(xp(j))],
  11141. [y(yp(j + diff), true), x(xp(j + diff))]
  11142. ];
  11143. } else {
  11144. points = [
  11145. [x(xp(j), true), y(yp(j))],
  11146. [x(xp(j + diff), true), y(yp(j + diff))]
  11147. ];
  11148. }
  11149. return generateM(points)
  11150. };
  11151. }
  11152. // Generate
  11153. for (i = 0; i < d.length; i++) {
  11154. // Draw as normal
  11155. if (isUndefined(regions) || !isWithinRegions(d[i].x, regions)) {
  11156. s += ' ' + xValue(d[i]) + ' ' + yValue(d[i]);
  11157. }
  11158. // Draw with region // TODO: Fix for horizotal charts
  11159. else {
  11160. xp = $$.getScale(
  11161. d[i - 1].x + xOffset,
  11162. d[i].x + xOffset,
  11163. $$.isTimeSeries()
  11164. );
  11165. yp = $$.getScale(d[i - 1].value, d[i].value);
  11166. dx = x(d[i].x) - x(d[i - 1].x);
  11167. dy = y(d[i].value) - y(d[i - 1].value);
  11168. dd = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  11169. diff = 2 / dd;
  11170. diffx2 = diff * 2;
  11171. for (j = diff; j <= 1; j += diffx2) {
  11172. s += sWithRegion(d[i - 1], d[i], j, diff);
  11173. }
  11174. }
  11175. prev = d[i].x;
  11176. }
  11177. return s
  11178. };
  11179. ChartInternal.prototype.updateArea = function(durationForExit) {
  11180. var $$ = this,
  11181. d3 = $$.d3;
  11182. var mainArea = $$.main
  11183. .selectAll('.' + CLASS.areas)
  11184. .selectAll('.' + CLASS.area)
  11185. .data($$.lineData.bind($$));
  11186. var mainAreaEnter = mainArea
  11187. .enter()
  11188. .append('path')
  11189. .attr('class', $$.classArea.bind($$))
  11190. .style('fill', $$.color)
  11191. .style('opacity', function() {
  11192. $$.orgAreaOpacity = +d3.select(this).style('opacity');
  11193. return 0
  11194. });
  11195. $$.mainArea = mainAreaEnter
  11196. .merge(mainArea)
  11197. .style('opacity', $$.orgAreaOpacity);
  11198. mainArea
  11199. .exit()
  11200. .transition()
  11201. .duration(durationForExit)
  11202. .style('opacity', 0);
  11203. };
  11204. ChartInternal.prototype.redrawArea = function(
  11205. drawArea,
  11206. withTransition,
  11207. transition
  11208. ) {
  11209. return [
  11210. (withTransition ? this.mainArea.transition(transition) : this.mainArea)
  11211. .attr('d', drawArea)
  11212. .style('fill', this.color)
  11213. .style('opacity', this.orgAreaOpacity)
  11214. ]
  11215. };
  11216. ChartInternal.prototype.generateDrawArea = function(areaIndices, isSub) {
  11217. var $$ = this,
  11218. config = $$.config,
  11219. area = $$.d3.area(),
  11220. getPoints = $$.generateGetAreaPoints(areaIndices, isSub),
  11221. yScaleGetter = isSub ? $$.getSubYScale : $$.getYScale,
  11222. xValue = function(d) {
  11223. return (isSub ? $$.subxx : $$.xx).call($$, d)
  11224. },
  11225. value0 = function(d, i) {
  11226. return config.data_groups.length > 0
  11227. ? getPoints(d, i)[0][1]
  11228. : yScaleGetter.call($$, d.id)($$.getAreaBaseValue(d.id))
  11229. },
  11230. value1 = function(d, i) {
  11231. return config.data_groups.length > 0
  11232. ? getPoints(d, i)[1][1]
  11233. : yScaleGetter.call($$, d.id)(d.value)
  11234. };
  11235. area = config.axis_rotated
  11236. ? area
  11237. .x0(value0)
  11238. .x1(value1)
  11239. .y(xValue)
  11240. : area
  11241. .x(xValue)
  11242. .y0(config.area_above ? 0 : value0)
  11243. .y1(value1);
  11244. if (!config.line_connectNull) {
  11245. area = area.defined(function(d) {
  11246. return d.value !== null
  11247. });
  11248. }
  11249. return function(d) {
  11250. var values = config.line_connectNull
  11251. ? $$.filterRemoveNull(d.values)
  11252. : d.values,
  11253. x0 = 0,
  11254. y0 = 0,
  11255. path;
  11256. if ($$.isAreaType(d)) {
  11257. if ($$.isStepType(d)) {
  11258. values = $$.convertValuesToStep(values);
  11259. }
  11260. path = area.curve($$.getInterpolate(d))(values);
  11261. } else {
  11262. if (values[0]) {
  11263. x0 = $$.x(values[0].x);
  11264. y0 = $$.getYScale(d.id)(values[0].value);
  11265. }
  11266. path = config.axis_rotated ? 'M ' + y0 + ' ' + x0 : 'M ' + x0 + ' ' + y0;
  11267. }
  11268. return path ? path : 'M 0 0'
  11269. }
  11270. };
  11271. ChartInternal.prototype.getAreaBaseValue = function() {
  11272. return 0
  11273. };
  11274. ChartInternal.prototype.generateGetAreaPoints = function(areaIndices, isSub) {
  11275. // partial duplication of generateGetBarPoints
  11276. var $$ = this,
  11277. config = $$.config,
  11278. areaTargetsNum = areaIndices.__max__ + 1,
  11279. x = $$.getShapeX(0, areaTargetsNum, areaIndices, !!isSub),
  11280. y = $$.getShapeY(!!isSub),
  11281. areaOffset = $$.getShapeOffset($$.isAreaType, areaIndices, !!isSub),
  11282. yScale = isSub ? $$.getSubYScale : $$.getYScale;
  11283. return function(d, i) {
  11284. var y0 = yScale.call($$, d.id)(0),
  11285. offset = areaOffset(d, i) || y0, // offset is for stacked area chart
  11286. posX = x(d),
  11287. posY = y(d);
  11288. // fix posY not to overflow opposite quadrant
  11289. if (config.axis_rotated) {
  11290. if ((0 < d.value && posY < y0) || (d.value < 0 && y0 < posY)) {
  11291. posY = y0;
  11292. }
  11293. }
  11294. // 1 point that marks the area position
  11295. return [
  11296. [posX, offset],
  11297. [posX, posY - (y0 - offset)],
  11298. [posX, posY - (y0 - offset)], // needed for compatibility
  11299. [posX, offset] // needed for compatibility
  11300. ]
  11301. }
  11302. };
  11303. ChartInternal.prototype.updateCircle = function(cx, cy) {
  11304. var $$ = this;
  11305. var mainCircle = $$.main
  11306. .selectAll('.' + CLASS.circles)
  11307. .selectAll('.' + CLASS.circle)
  11308. .data($$.lineOrScatterOrStanfordData.bind($$));
  11309. var mainCircleEnter = mainCircle
  11310. .enter()
  11311. .append('circle')
  11312. .attr('shape-rendering', $$.isStanfordGraphType() ? 'crispEdges' : '')
  11313. .attr('class', $$.classCircle.bind($$))
  11314. .attr('cx', cx)
  11315. .attr('cy', cy)
  11316. .attr('r', $$.pointR.bind($$))
  11317. .style(
  11318. 'color',
  11319. $$.isStanfordGraphType() ? $$.getStanfordPointColor.bind($$) : $$.color
  11320. );
  11321. $$.mainCircle = mainCircleEnter
  11322. .merge(mainCircle)
  11323. .style(
  11324. 'opacity',
  11325. $$.isStanfordGraphType() ? 1 : $$.initialOpacityForCircle.bind($$)
  11326. );
  11327. mainCircle.exit().style('opacity', 0);
  11328. };
  11329. ChartInternal.prototype.redrawCircle = function(
  11330. cx,
  11331. cy,
  11332. withTransition,
  11333. transition
  11334. ) {
  11335. var $$ = this,
  11336. selectedCircles = $$.main.selectAll('.' + CLASS.selectedCircle);
  11337. return [
  11338. (withTransition ? $$.mainCircle.transition(transition) : $$.mainCircle)
  11339. .style('opacity', this.opacityForCircle.bind($$))
  11340. .style(
  11341. 'color',
  11342. $$.isStanfordGraphType() ? $$.getStanfordPointColor.bind($$) : $$.color
  11343. )
  11344. .attr('cx', cx)
  11345. .attr('cy', cy),
  11346. (withTransition ? selectedCircles.transition(transition) : selectedCircles)
  11347. .attr('cx', cx)
  11348. .attr('cy', cy)
  11349. ]
  11350. };
  11351. ChartInternal.prototype.circleX = function(d) {
  11352. return d.x || d.x === 0 ? this.x(d.x) : null
  11353. };
  11354. ChartInternal.prototype.updateCircleY = function() {
  11355. var $$ = this,
  11356. lineIndices,
  11357. getPoints;
  11358. if ($$.config.data_groups.length > 0) {
  11359. (lineIndices = $$.getShapeIndices($$.isLineType)),
  11360. (getPoints = $$.generateGetLinePoints(lineIndices));
  11361. $$.circleY = function(d, i) {
  11362. return getPoints(d, i)[0][1]
  11363. };
  11364. } else {
  11365. $$.circleY = function(d) {
  11366. return $$.getYScale(d.id)(d.value)
  11367. };
  11368. }
  11369. };
  11370. ChartInternal.prototype.getCircles = function(i, id) {
  11371. var $$ = this;
  11372. return (id
  11373. ? $$.main.selectAll('.' + CLASS.circles + $$.getTargetSelectorSuffix(id))
  11374. : $$.main
  11375. ).selectAll('.' + CLASS.circle + (isValue(i) ? '-' + i : ''))
  11376. };
  11377. ChartInternal.prototype.expandCircles = function(i, id, reset) {
  11378. var $$ = this,
  11379. r = $$.pointExpandedR.bind($$);
  11380. if (reset) {
  11381. $$.unexpandCircles();
  11382. }
  11383. $$.getCircles(i, id)
  11384. .classed(CLASS.EXPANDED, true)
  11385. .attr('r', r);
  11386. };
  11387. ChartInternal.prototype.unexpandCircles = function(i) {
  11388. var $$ = this,
  11389. r = $$.pointR.bind($$);
  11390. $$.getCircles(i)
  11391. .filter(function() {
  11392. return $$.d3.select(this).classed(CLASS.EXPANDED)
  11393. })
  11394. .classed(CLASS.EXPANDED, false)
  11395. .attr('r', r);
  11396. };
  11397. ChartInternal.prototype.pointR = function(d) {
  11398. var $$ = this,
  11399. config = $$.config;
  11400. return $$.isStepType(d)
  11401. ? 0
  11402. : isFunction(config.point_r)
  11403. ? config.point_r(d)
  11404. : config.point_r
  11405. };
  11406. ChartInternal.prototype.pointExpandedR = function(d) {
  11407. var $$ = this,
  11408. config = $$.config;
  11409. if (config.point_focus_expand_enabled) {
  11410. return isFunction(config.point_focus_expand_r)
  11411. ? config.point_focus_expand_r(d)
  11412. : config.point_focus_expand_r
  11413. ? config.point_focus_expand_r
  11414. : $$.pointR(d) * 1.75
  11415. } else {
  11416. return $$.pointR(d)
  11417. }
  11418. };
  11419. ChartInternal.prototype.pointSelectR = function(d) {
  11420. var $$ = this,
  11421. config = $$.config;
  11422. return isFunction(config.point_select_r)
  11423. ? config.point_select_r(d)
  11424. : config.point_select_r
  11425. ? config.point_select_r
  11426. : $$.pointR(d) * 4
  11427. };
  11428. ChartInternal.prototype.isWithinCircle = function(that, r) {
  11429. var d3 = this.d3,
  11430. mouse = d3.mouse(that),
  11431. d3_this = d3.select(that),
  11432. cx = +d3_this.attr('cx'),
  11433. cy = +d3_this.attr('cy');
  11434. return Math.sqrt(Math.pow(cx - mouse[0], 2) + Math.pow(cy - mouse[1], 2)) < r
  11435. };
  11436. ChartInternal.prototype.isWithinStep = function(that, y) {
  11437. return Math.abs(y - this.d3.mouse(that)[1]) < 30
  11438. };
  11439. ChartInternal.prototype.getCurrentWidth = function() {
  11440. var $$ = this,
  11441. config = $$.config;
  11442. return config.size_width ? config.size_width : $$.getParentWidth()
  11443. };
  11444. ChartInternal.prototype.getCurrentHeight = function() {
  11445. var $$ = this,
  11446. config = $$.config,
  11447. h = config.size_height ? config.size_height : $$.getParentHeight();
  11448. return h > 0
  11449. ? h
  11450. : 320 / ($$.hasType('gauge') && !config.gauge_fullCircle ? 2 : 1)
  11451. };
  11452. ChartInternal.prototype.getCurrentPaddingTop = function() {
  11453. var $$ = this,
  11454. config = $$.config,
  11455. padding = isValue(config.padding_top) ? config.padding_top : 0;
  11456. if ($$.title && $$.title.node()) {
  11457. padding += $$.getTitlePadding();
  11458. }
  11459. return padding
  11460. };
  11461. ChartInternal.prototype.getCurrentPaddingBottom = function() {
  11462. var config = this.config;
  11463. return isValue(config.padding_bottom) ? config.padding_bottom : 0
  11464. };
  11465. ChartInternal.prototype.getCurrentPaddingLeft = function(withoutRecompute) {
  11466. var $$ = this,
  11467. config = $$.config;
  11468. if (isValue(config.padding_left)) {
  11469. return config.padding_left
  11470. } else if (config.axis_rotated) {
  11471. return !config.axis_x_show || config.axis_x_inner
  11472. ? 1
  11473. : Math.max(ceil10($$.getAxisWidthByAxisId('x', withoutRecompute)), 40)
  11474. } else if (!config.axis_y_show || config.axis_y_inner) {
  11475. // && !config.axis_rotated
  11476. return $$.axis.getYAxisLabelPosition().isOuter ? 30 : 1
  11477. } else {
  11478. return ceil10($$.getAxisWidthByAxisId('y', withoutRecompute))
  11479. }
  11480. };
  11481. ChartInternal.prototype.getCurrentPaddingRight = function() {
  11482. var $$ = this,
  11483. config = $$.config,
  11484. padding = 0,
  11485. defaultPadding = 10,
  11486. legendWidthOnRight = $$.isLegendRight ? $$.getLegendWidth() + 20 : 0;
  11487. if (isValue(config.padding_right)) {
  11488. padding = config.padding_right + 1; // 1 is needed not to hide tick line
  11489. } else if (config.axis_rotated) {
  11490. padding = defaultPadding + legendWidthOnRight;
  11491. } else if (!config.axis_y2_show || config.axis_y2_inner) {
  11492. // && !config.axis_rotated
  11493. padding =
  11494. 2 +
  11495. legendWidthOnRight +
  11496. ($$.axis.getY2AxisLabelPosition().isOuter ? 20 : 0);
  11497. } else {
  11498. padding = ceil10($$.getAxisWidthByAxisId('y2')) + legendWidthOnRight;
  11499. }
  11500. if ($$.colorScale && $$.colorScale.node()) {
  11501. padding += $$.getColorScalePadding();
  11502. }
  11503. return padding
  11504. };
  11505. ChartInternal.prototype.getParentRectValue = function(key) {
  11506. var parent = this.selectChart.node(),
  11507. v;
  11508. while (parent && parent.tagName !== 'BODY') {
  11509. try {
  11510. v = parent.getBoundingClientRect()[key];
  11511. } catch (e) {
  11512. if (key === 'width') {
  11513. // In IE in certain cases getBoundingClientRect
  11514. // will cause an "unspecified error"
  11515. v = parent.offsetWidth;
  11516. }
  11517. }
  11518. if (v) {
  11519. break
  11520. }
  11521. parent = parent.parentNode;
  11522. }
  11523. return v
  11524. };
  11525. ChartInternal.prototype.getParentWidth = function() {
  11526. return this.getParentRectValue('width')
  11527. };
  11528. ChartInternal.prototype.getParentHeight = function() {
  11529. var h = this.selectChart.style('height');
  11530. return h.indexOf('px') > 0 ? +h.replace('px', '') : 0
  11531. };
  11532. ChartInternal.prototype.getSvgLeft = function(withoutRecompute) {
  11533. var $$ = this,
  11534. config = $$.config,
  11535. hasLeftAxisRect =
  11536. config.axis_rotated || (!config.axis_rotated && !config.axis_y_inner),
  11537. leftAxisClass = config.axis_rotated ? CLASS.axisX : CLASS.axisY,
  11538. leftAxis = $$.main.select('.' + leftAxisClass).node(),
  11539. svgRect =
  11540. leftAxis && hasLeftAxisRect
  11541. ? leftAxis.getBoundingClientRect()
  11542. : { right: 0 },
  11543. chartRect = $$.selectChart.node().getBoundingClientRect(),
  11544. hasArc = $$.hasArcType(),
  11545. svgLeft =
  11546. svgRect.right -
  11547. chartRect.left -
  11548. (hasArc ? 0 : $$.getCurrentPaddingLeft(withoutRecompute));
  11549. return svgLeft > 0 ? svgLeft : 0
  11550. };
  11551. ChartInternal.prototype.getAxisWidthByAxisId = function(id, withoutRecompute) {
  11552. var $$ = this,
  11553. position = $$.axis.getLabelPositionById(id);
  11554. return (
  11555. $$.axis.getMaxTickWidth(id, withoutRecompute) + (position.isInner ? 20 : 40)
  11556. )
  11557. };
  11558. ChartInternal.prototype.getHorizontalAxisHeight = function(axisId) {
  11559. var $$ = this,
  11560. config = $$.config,
  11561. h = 30;
  11562. if (axisId === 'x' && !config.axis_x_show) {
  11563. return 8
  11564. }
  11565. if (axisId === 'x' && config.axis_x_height) {
  11566. return config.axis_x_height
  11567. }
  11568. if (axisId === 'y' && !config.axis_y_show) {
  11569. return config.legend_show && !$$.isLegendRight && !$$.isLegendInset ? 10 : 1
  11570. }
  11571. if (axisId === 'y2' && !config.axis_y2_show) {
  11572. return $$.rotated_padding_top
  11573. }
  11574. // Calculate x axis height when tick rotated
  11575. if (axisId === 'x' && !config.axis_rotated && config.axis_x_tick_rotate) {
  11576. h =
  11577. 30 +
  11578. $$.axis.getMaxTickWidth(axisId) *
  11579. Math.cos((Math.PI * (90 - Math.abs(config.axis_x_tick_rotate))) / 180);
  11580. }
  11581. // Calculate y axis height when tick rotated
  11582. if (axisId === 'y' && config.axis_rotated && config.axis_y_tick_rotate) {
  11583. h =
  11584. 30 +
  11585. $$.axis.getMaxTickWidth(axisId) *
  11586. Math.cos((Math.PI * (90 - Math.abs(config.axis_y_tick_rotate))) / 180);
  11587. }
  11588. return (
  11589. h +
  11590. ($$.axis.getLabelPositionById(axisId).isInner ? 0 : 10) +
  11591. (axisId === 'y2' ? -10 : 0)
  11592. )
  11593. };
  11594. ChartInternal.prototype.initBrush = function(scale) {
  11595. var $$ = this,
  11596. d3 = $$.d3;
  11597. // TODO: dynamically change brushY/brushX according to axis_rotated.
  11598. $$.brush = ($$.config.axis_rotated ? d3.brushY() : d3.brushX())
  11599. .on('brush', function() {
  11600. var event = d3.event.sourceEvent;
  11601. if (event && event.type === 'zoom') {
  11602. return
  11603. }
  11604. $$.redrawForBrush();
  11605. })
  11606. .on('end', function() {
  11607. var event = d3.event.sourceEvent;
  11608. if (event && event.type === 'zoom') {
  11609. return
  11610. }
  11611. if ($$.brush.empty() && event && event.type !== 'end') {
  11612. $$.brush.clear();
  11613. }
  11614. });
  11615. $$.brush.updateExtent = function() {
  11616. var range = this.scale.range(),
  11617. extent;
  11618. if ($$.config.axis_rotated) {
  11619. extent = [
  11620. [0, range[0]],
  11621. [$$.width2, range[1]]
  11622. ];
  11623. } else {
  11624. extent = [
  11625. [range[0], 0],
  11626. [range[1], $$.height2]
  11627. ];
  11628. }
  11629. this.extent(extent);
  11630. return this
  11631. };
  11632. $$.brush.updateScale = function(scale) {
  11633. this.scale = scale;
  11634. return this
  11635. };
  11636. $$.brush.update = function(scale) {
  11637. this.updateScale(scale || $$.subX).updateExtent();
  11638. $$.context.select('.' + CLASS.brush).call(this);
  11639. };
  11640. $$.brush.clear = function() {
  11641. $$.context.select('.' + CLASS.brush).call($$.brush.move, null);
  11642. };
  11643. $$.brush.selection = function() {
  11644. return d3.brushSelection($$.context.select('.' + CLASS.brush).node())
  11645. };
  11646. $$.brush.selectionAsValue = function(selectionAsValue, withTransition) {
  11647. var selection, brush;
  11648. if (selectionAsValue) {
  11649. if ($$.context) {
  11650. selection = [
  11651. this.scale(selectionAsValue[0]),
  11652. this.scale(selectionAsValue[1])
  11653. ];
  11654. brush = $$.context.select('.' + CLASS.brush);
  11655. if (withTransition) {
  11656. brush = brush.transition();
  11657. }
  11658. $$.brush.move(brush, selection);
  11659. }
  11660. return []
  11661. }
  11662. selection = $$.brush.selection() || [0, 0];
  11663. return [this.scale.invert(selection[0]), this.scale.invert(selection[1])]
  11664. };
  11665. $$.brush.empty = function() {
  11666. var selection = $$.brush.selection();
  11667. return !selection || selection[0] === selection[1]
  11668. };
  11669. return $$.brush.updateScale(scale)
  11670. };
  11671. ChartInternal.prototype.initSubchart = function() {
  11672. var $$ = this,
  11673. config = $$.config,
  11674. context = ($$.context = $$.svg
  11675. .append('g')
  11676. .attr('transform', $$.getTranslate('context')));
  11677. // set style
  11678. context.style('visibility', 'visible');
  11679. // Define g for chart area
  11680. context
  11681. .append('g')
  11682. .attr('clip-path', $$.clipPathForSubchart)
  11683. .attr('class', CLASS.chart);
  11684. // Define g for bar chart area
  11685. context
  11686. .select('.' + CLASS.chart)
  11687. .append('g')
  11688. .attr('class', CLASS.chartBars);
  11689. // Define g for line chart area
  11690. context
  11691. .select('.' + CLASS.chart)
  11692. .append('g')
  11693. .attr('class', CLASS.chartLines);
  11694. // Add extent rect for Brush
  11695. context
  11696. .append('g')
  11697. .attr('clip-path', $$.clipPath)
  11698. .attr('class', CLASS.brush);
  11699. // ATTENTION: This must be called AFTER chart added
  11700. // Add Axis
  11701. $$.axes.subx = context
  11702. .append('g')
  11703. .attr('class', CLASS.axisX)
  11704. .attr('transform', $$.getTranslate('subx'))
  11705. .attr('clip-path', config.axis_rotated ? '' : $$.clipPathForXAxis);
  11706. };
  11707. ChartInternal.prototype.initSubchartBrush = function() {
  11708. var $$ = this;
  11709. // Add extent rect for Brush
  11710. $$.initBrush($$.subX).updateExtent();
  11711. $$.context.select('.' + CLASS.brush).call($$.brush);
  11712. };
  11713. ChartInternal.prototype.updateTargetsForSubchart = function(targets) {
  11714. var $$ = this,
  11715. context = $$.context,
  11716. config = $$.config,
  11717. contextLineEnter,
  11718. contextLine,
  11719. contextBarEnter,
  11720. contextBar,
  11721. classChartBar = $$.classChartBar.bind($$),
  11722. classBars = $$.classBars.bind($$),
  11723. classChartLine = $$.classChartLine.bind($$),
  11724. classLines = $$.classLines.bind($$),
  11725. classAreas = $$.classAreas.bind($$);
  11726. //-- Bar --//
  11727. contextBar = context
  11728. .select('.' + CLASS.chartBars)
  11729. .selectAll('.' + CLASS.chartBar)
  11730. .data(targets);
  11731. contextBarEnter = contextBar
  11732. .enter()
  11733. .append('g')
  11734. .style('opacity', 0);
  11735. contextBarEnter.merge(contextBar).attr('class', classChartBar);
  11736. // Bars for each data
  11737. contextBarEnter.append('g').attr('class', classBars);
  11738. //-- Line --//
  11739. contextLine = context
  11740. .select('.' + CLASS.chartLines)
  11741. .selectAll('.' + CLASS.chartLine)
  11742. .data(targets);
  11743. contextLineEnter = contextLine
  11744. .enter()
  11745. .append('g')
  11746. .style('opacity', 0);
  11747. contextLineEnter.merge(contextLine).attr('class', classChartLine);
  11748. // Lines for each data
  11749. contextLineEnter.append('g').attr('class', classLines);
  11750. // Area
  11751. contextLineEnter.append('g').attr('class', classAreas);
  11752. //-- Brush --//
  11753. context
  11754. .selectAll('.' + CLASS.brush + ' rect')
  11755. .attr(
  11756. config.axis_rotated ? 'width' : 'height',
  11757. config.axis_rotated ? $$.width2 : $$.height2
  11758. );
  11759. };
  11760. ChartInternal.prototype.updateBarForSubchart = function(durationForExit) {
  11761. var $$ = this;
  11762. var contextBar = $$.context
  11763. .selectAll('.' + CLASS.bars)
  11764. .selectAll('.' + CLASS.bar)
  11765. .data($$.barData.bind($$));
  11766. var contextBarEnter = contextBar
  11767. .enter()
  11768. .append('path')
  11769. .attr('class', $$.classBar.bind($$))
  11770. .style('stroke', 'none')
  11771. .style('fill', $$.color);
  11772. contextBar
  11773. .exit()
  11774. .transition()
  11775. .duration(durationForExit)
  11776. .style('opacity', 0)
  11777. .remove();
  11778. $$.contextBar = contextBarEnter
  11779. .merge(contextBar)
  11780. .style('opacity', $$.initialOpacity.bind($$));
  11781. };
  11782. ChartInternal.prototype.redrawBarForSubchart = function(
  11783. drawBarOnSub,
  11784. withTransition,
  11785. duration
  11786. ) {
  11787. (withTransition
  11788. ? this.contextBar.transition(Math.random().toString()).duration(duration)
  11789. : this.contextBar
  11790. )
  11791. .attr('d', drawBarOnSub)
  11792. .style('opacity', 1);
  11793. };
  11794. ChartInternal.prototype.updateLineForSubchart = function(durationForExit) {
  11795. var $$ = this;
  11796. var contextLine = $$.context
  11797. .selectAll('.' + CLASS.lines)
  11798. .selectAll('.' + CLASS.line)
  11799. .data($$.lineData.bind($$));
  11800. var contextLineEnter = contextLine
  11801. .enter()
  11802. .append('path')
  11803. .attr('class', $$.classLine.bind($$))
  11804. .style('stroke', $$.color);
  11805. contextLine
  11806. .exit()
  11807. .transition()
  11808. .duration(durationForExit)
  11809. .style('opacity', 0)
  11810. .remove();
  11811. $$.contextLine = contextLineEnter
  11812. .merge(contextLine)
  11813. .style('opacity', $$.initialOpacity.bind($$));
  11814. };
  11815. ChartInternal.prototype.redrawLineForSubchart = function(
  11816. drawLineOnSub,
  11817. withTransition,
  11818. duration
  11819. ) {
  11820. (withTransition
  11821. ? this.contextLine.transition(Math.random().toString()).duration(duration)
  11822. : this.contextLine
  11823. )
  11824. .attr('d', drawLineOnSub)
  11825. .style('opacity', 1);
  11826. };
  11827. ChartInternal.prototype.updateAreaForSubchart = function(durationForExit) {
  11828. var $$ = this,
  11829. d3 = $$.d3;
  11830. var contextArea = $$.context
  11831. .selectAll('.' + CLASS.areas)
  11832. .selectAll('.' + CLASS.area)
  11833. .data($$.lineData.bind($$));
  11834. var contextAreaEnter = contextArea
  11835. .enter()
  11836. .append('path')
  11837. .attr('class', $$.classArea.bind($$))
  11838. .style('fill', $$.color)
  11839. .style('opacity', function() {
  11840. $$.orgAreaOpacity = +d3.select(this).style('opacity');
  11841. return 0
  11842. });
  11843. contextArea
  11844. .exit()
  11845. .transition()
  11846. .duration(durationForExit)
  11847. .style('opacity', 0)
  11848. .remove();
  11849. $$.contextArea = contextAreaEnter.merge(contextArea).style('opacity', 0);
  11850. };
  11851. ChartInternal.prototype.redrawAreaForSubchart = function(
  11852. drawAreaOnSub,
  11853. withTransition,
  11854. duration
  11855. ) {
  11856. (withTransition
  11857. ? this.contextArea.transition(Math.random().toString()).duration(duration)
  11858. : this.contextArea
  11859. )
  11860. .attr('d', drawAreaOnSub)
  11861. .style('fill', this.color)
  11862. .style('opacity', this.orgAreaOpacity);
  11863. };
  11864. ChartInternal.prototype.redrawSubchart = function(
  11865. withSubchart,
  11866. transitions,
  11867. duration,
  11868. durationForExit,
  11869. areaIndices,
  11870. barIndices,
  11871. lineIndices
  11872. ) {
  11873. var $$ = this,
  11874. d3 = $$.d3,
  11875. drawAreaOnSub,
  11876. drawBarOnSub,
  11877. drawLineOnSub;
  11878. // reflect main chart to extent on subchart if zoomed
  11879. if (d3.event && d3.event.type === 'zoom') {
  11880. $$.brush.selectionAsValue($$.x.orgDomain());
  11881. }
  11882. // update subchart elements if needed
  11883. if (withSubchart) {
  11884. // extent rect
  11885. if (!$$.brush.empty()) {
  11886. $$.brush.selectionAsValue($$.x.orgDomain());
  11887. }
  11888. // setup drawer - MEMO: this must be called after axis updated
  11889. drawAreaOnSub = $$.generateDrawArea(areaIndices, true);
  11890. drawBarOnSub = $$.generateDrawBar(barIndices, true);
  11891. drawLineOnSub = $$.generateDrawLine(lineIndices, true);
  11892. $$.updateBarForSubchart(duration);
  11893. $$.updateLineForSubchart(duration);
  11894. $$.updateAreaForSubchart(duration);
  11895. $$.redrawBarForSubchart(drawBarOnSub, duration, duration);
  11896. $$.redrawLineForSubchart(drawLineOnSub, duration, duration);
  11897. $$.redrawAreaForSubchart(drawAreaOnSub, duration, duration);
  11898. }
  11899. };
  11900. ChartInternal.prototype.redrawForBrush = function() {
  11901. var $$ = this,
  11902. x = $$.x,
  11903. d3 = $$.d3,
  11904. s;
  11905. $$.redraw({
  11906. withTransition: false,
  11907. withY: $$.config.zoom_rescale,
  11908. withSubchart: false,
  11909. withUpdateXDomain: true,
  11910. withEventRect: false,
  11911. withDimension: false
  11912. });
  11913. // update zoom transation binded to event rect
  11914. s = d3.event.selection || $$.brush.scale.range();
  11915. $$.main
  11916. .select('.' + CLASS.eventRect)
  11917. .call(
  11918. $$.zoom.transform,
  11919. d3.zoomIdentity.scale($$.width / (s[1] - s[0])).translate(-s[0], 0)
  11920. );
  11921. $$.config.subchart_onbrush.call($$.api, x.orgDomain());
  11922. };
  11923. ChartInternal.prototype.transformContext = function(
  11924. withTransition,
  11925. transitions
  11926. ) {
  11927. var $$ = this,
  11928. subXAxis;
  11929. if (transitions && transitions.axisSubX) {
  11930. subXAxis = transitions.axisSubX;
  11931. } else {
  11932. subXAxis = $$.context.select('.' + CLASS.axisX);
  11933. if (withTransition) {
  11934. subXAxis = subXAxis.transition();
  11935. }
  11936. }
  11937. $$.context.attr('transform', $$.getTranslate('context'));
  11938. subXAxis.attr('transform', $$.getTranslate('subx'));
  11939. };
  11940. ChartInternal.prototype.getDefaultSelection = function() {
  11941. var $$ = this,
  11942. config = $$.config,
  11943. selection = isFunction(config.axis_x_selection)
  11944. ? config.axis_x_selection($$.getXDomain($$.data.targets))
  11945. : config.axis_x_selection;
  11946. if ($$.isTimeSeries()) {
  11947. selection = [$$.parseDate(selection[0]), $$.parseDate(selection[1])];
  11948. }
  11949. return selection
  11950. };
  11951. ChartInternal.prototype.removeSubchart = function() {
  11952. const $$ = this;
  11953. $$.brush = null;
  11954. $$.context.remove();
  11955. $$.context = null;
  11956. };
  11957. ChartInternal.prototype.initText = function() {
  11958. var $$ = this;
  11959. $$.main
  11960. .select('.' + CLASS.chart)
  11961. .append('g')
  11962. .attr('class', CLASS.chartTexts);
  11963. $$.mainText = $$.d3.selectAll([]);
  11964. };
  11965. ChartInternal.prototype.updateTargetsForText = function(targets) {
  11966. var $$ = this,
  11967. classChartText = $$.classChartText.bind($$),
  11968. classTexts = $$.classTexts.bind($$),
  11969. classFocus = $$.classFocus.bind($$);
  11970. var mainText = $$.main
  11971. .select('.' + CLASS.chartTexts)
  11972. .selectAll('.' + CLASS.chartText)
  11973. .data(targets);
  11974. var mainTextEnter = mainText
  11975. .enter()
  11976. .append('g')
  11977. .attr('class', classChartText)
  11978. .style('opacity', 0)
  11979. .style('pointer-events', 'none');
  11980. mainTextEnter.append('g').attr('class', classTexts);
  11981. mainTextEnter.merge(mainText).attr('class', function(d) {
  11982. return classChartText(d) + classFocus(d)
  11983. });
  11984. };
  11985. ChartInternal.prototype.updateText = function(
  11986. xForText,
  11987. yForText,
  11988. durationForExit
  11989. ) {
  11990. var $$ = this,
  11991. config = $$.config,
  11992. barOrLineData = $$.barOrLineData.bind($$),
  11993. classText = $$.classText.bind($$);
  11994. var mainText = $$.main
  11995. .selectAll('.' + CLASS.texts)
  11996. .selectAll('.' + CLASS.text)
  11997. .data(barOrLineData);
  11998. var mainTextEnter = mainText
  11999. .enter()
  12000. .append('text')
  12001. .attr('class', classText)
  12002. .attr('text-anchor', function(d) {
  12003. return config.axis_rotated ? (d.value < 0 ? 'end' : 'start') : 'middle'
  12004. })
  12005. .style('stroke', 'none')
  12006. .attr('x', xForText)
  12007. .attr('y', yForText)
  12008. .style('fill', function(d) {
  12009. return $$.color(d)
  12010. })
  12011. .style('fill-opacity', 0);
  12012. $$.mainText = mainTextEnter.merge(mainText).text(function(d, i, j) {
  12013. return $$.dataLabelFormat(d.id)(d.value, d.id, i, j)
  12014. });
  12015. mainText
  12016. .exit()
  12017. .transition()
  12018. .duration(durationForExit)
  12019. .style('fill-opacity', 0)
  12020. .remove();
  12021. };
  12022. ChartInternal.prototype.redrawText = function(
  12023. xForText,
  12024. yForText,
  12025. forFlow,
  12026. withTransition,
  12027. transition
  12028. ) {
  12029. return [
  12030. (withTransition ? this.mainText.transition(transition) : this.mainText)
  12031. .attr('x', xForText)
  12032. .attr('y', yForText)
  12033. .style('fill', this.color)
  12034. .style('fill-opacity', forFlow ? 0 : this.opacityForText.bind(this))
  12035. ]
  12036. };
  12037. ChartInternal.prototype.getTextRect = function(text, cls, element) {
  12038. var dummy = this.d3
  12039. .select('body')
  12040. .append('div')
  12041. .classed('c3', true),
  12042. svg = dummy
  12043. .append('svg')
  12044. .style('visibility', 'hidden')
  12045. .style('position', 'fixed')
  12046. .style('top', 0)
  12047. .style('left', 0),
  12048. font = this.d3.select(element).style('font'),
  12049. rect;
  12050. svg
  12051. .selectAll('.dummy')
  12052. .data([text])
  12053. .enter()
  12054. .append('text')
  12055. .classed(cls ? cls : '', true)
  12056. .style('font', font)
  12057. .text(text)
  12058. .each(function() {
  12059. rect = getBBox(this);
  12060. });
  12061. dummy.remove();
  12062. return rect
  12063. };
  12064. ChartInternal.prototype.generateXYForText = function(
  12065. areaIndices,
  12066. barIndices,
  12067. lineIndices,
  12068. forX
  12069. ) {
  12070. var $$ = this,
  12071. getAreaPoints = $$.generateGetAreaPoints(areaIndices, false),
  12072. getBarPoints = $$.generateGetBarPoints(barIndices, false),
  12073. getLinePoints = $$.generateGetLinePoints(lineIndices, false),
  12074. getter = forX ? $$.getXForText : $$.getYForText;
  12075. return function(d, i) {
  12076. var getPoints = $$.isAreaType(d)
  12077. ? getAreaPoints
  12078. : $$.isBarType(d)
  12079. ? getBarPoints
  12080. : getLinePoints;
  12081. return getter.call($$, getPoints(d, i), d, this)
  12082. }
  12083. };
  12084. ChartInternal.prototype.getXForText = function(points, d, textElement) {
  12085. var $$ = this,
  12086. box = getBBox(textElement),
  12087. xPos,
  12088. padding;
  12089. if ($$.config.axis_rotated) {
  12090. padding = $$.isBarType(d) ? 4 : 6;
  12091. xPos = points[2][1] + padding * (d.value < 0 ? -1 : 1);
  12092. } else {
  12093. xPos = $$.hasType('bar') ? (points[2][0] + points[0][0]) / 2 : points[0][0];
  12094. }
  12095. // show labels regardless of the domain if value is null
  12096. if (d.value === null) {
  12097. if (xPos > $$.width) {
  12098. xPos = $$.width - box.width;
  12099. } else if (xPos < 0) {
  12100. xPos = 4;
  12101. }
  12102. }
  12103. return xPos
  12104. };
  12105. ChartInternal.prototype.getYForText = function(points, d, textElement) {
  12106. var $$ = this,
  12107. box = getBBox(textElement),
  12108. yPos;
  12109. if ($$.config.axis_rotated) {
  12110. yPos = (points[0][0] + points[2][0] + box.height * 0.6) / 2;
  12111. } else {
  12112. yPos = points[2][1];
  12113. if (d.value < 0 || (d.value === 0 && !$$.hasPositiveValue)) {
  12114. yPos += box.height;
  12115. if ($$.isBarType(d) && $$.isSafari()) {
  12116. yPos -= 3;
  12117. } else if (!$$.isBarType(d) && $$.isChrome()) {
  12118. yPos += 3;
  12119. }
  12120. } else {
  12121. yPos += $$.isBarType(d) ? -3 : -6;
  12122. }
  12123. }
  12124. // show labels regardless of the domain if value is null
  12125. if (d.value === null && !$$.config.axis_rotated) {
  12126. if (yPos < box.height) {
  12127. yPos = box.height;
  12128. } else if (yPos > this.height) {
  12129. yPos = this.height - 4;
  12130. }
  12131. }
  12132. return yPos
  12133. };
  12134. ChartInternal.prototype.initTitle = function() {
  12135. var $$ = this;
  12136. $$.title = $$.svg
  12137. .append('text')
  12138. .text($$.config.title_text)
  12139. .attr('class', $$.CLASS.title);
  12140. };
  12141. ChartInternal.prototype.redrawTitle = function() {
  12142. var $$ = this;
  12143. $$.title.attr('x', $$.xForTitle.bind($$)).attr('y', $$.yForTitle.bind($$));
  12144. };
  12145. ChartInternal.prototype.xForTitle = function() {
  12146. var $$ = this,
  12147. config = $$.config,
  12148. position = config.title_position || 'left',
  12149. x;
  12150. if (position.indexOf('right') >= 0) {
  12151. x =
  12152. $$.currentWidth -
  12153. $$.getTextRect(
  12154. $$.title.node().textContent,
  12155. $$.CLASS.title,
  12156. $$.title.node()
  12157. ).width -
  12158. config.title_padding.right;
  12159. } else if (position.indexOf('center') >= 0) {
  12160. x = Math.max(
  12161. ($$.currentWidth -
  12162. $$.getTextRect(
  12163. $$.title.node().textContent,
  12164. $$.CLASS.title,
  12165. $$.title.node()
  12166. ).width) /
  12167. 2,
  12168. 0
  12169. );
  12170. } else {
  12171. // left
  12172. x = config.title_padding.left;
  12173. }
  12174. return x
  12175. };
  12176. ChartInternal.prototype.yForTitle = function() {
  12177. var $$ = this;
  12178. return (
  12179. $$.config.title_padding.top +
  12180. $$.getTextRect($$.title.node().textContent, $$.CLASS.title, $$.title.node())
  12181. .height
  12182. )
  12183. };
  12184. ChartInternal.prototype.getTitlePadding = function() {
  12185. var $$ = this;
  12186. return $$.yForTitle() + $$.config.title_padding.bottom
  12187. };
  12188. function powerOfTen(d) {
  12189. return d / Math.pow(10, Math.ceil(Math.log(d) / Math.LN10 - 1e-12)) === 1
  12190. }
  12191. ChartInternal.prototype.drawColorScale = function() {
  12192. var $$ = this,
  12193. d3 = $$.d3,
  12194. config = $$.config,
  12195. target = $$.data.targets[0],
  12196. barWidth,
  12197. barHeight,
  12198. axis,
  12199. points,
  12200. legendAxis,
  12201. axisScale,
  12202. inverseScale,
  12203. height;
  12204. barWidth = !isNaN(config.stanford_scaleWidth)
  12205. ? config.stanford_scaleWidth
  12206. : 20;
  12207. barHeight = 5;
  12208. if (barHeight < 0 || barWidth < 0) {
  12209. throw Error("Colorscale's barheight and barwidth must be greater than 0.")
  12210. }
  12211. height =
  12212. $$.height - config.stanford_padding.bottom - config.stanford_padding.top;
  12213. points = d3.range(config.stanford_padding.bottom, height, barHeight);
  12214. inverseScale = d3
  12215. .scaleSequential(target.colors)
  12216. .domain([points[points.length - 1], points[0]]);
  12217. if ($$.colorScale) {
  12218. $$.colorScale.remove();
  12219. }
  12220. $$.colorScale = $$.svg
  12221. .append('g')
  12222. .attr('width', 50)
  12223. .attr('height', height)
  12224. .attr('class', CLASS.colorScale);
  12225. $$.colorScale
  12226. .append('g')
  12227. .attr('transform', `translate(0, ${config.stanford_padding.top})`)
  12228. .selectAll('bars')
  12229. .data(points)
  12230. .enter()
  12231. .append('rect')
  12232. .attr('y', (d, i) => i * barHeight)
  12233. .attr('x', 0)
  12234. .attr('width', barWidth)
  12235. .attr('height', barHeight)
  12236. .attr('fill', function(d) {
  12237. return inverseScale(d)
  12238. });
  12239. // Legend Axis
  12240. axisScale = d3
  12241. .scaleLog()
  12242. .domain([target.minEpochs, target.maxEpochs])
  12243. .range([
  12244. points[0] +
  12245. config.stanford_padding.top +
  12246. points[points.length - 1] +
  12247. barHeight -
  12248. 1,
  12249. points[0] + config.stanford_padding.top
  12250. ]);
  12251. legendAxis = d3.axisRight(axisScale);
  12252. if (config.stanford_scaleFormat === 'pow10') {
  12253. legendAxis.tickValues([1, 10, 100, 1000, 10000, 100000, 1000000, 10000000]);
  12254. } else if (isFunction(config.stanford_scaleFormat)) {
  12255. legendAxis.tickFormat(config.stanford_scaleFormat);
  12256. } else {
  12257. legendAxis.tickFormat(d3.format('d'));
  12258. }
  12259. if (isFunction(config.stanford_scaleValues)) {
  12260. legendAxis.tickValues(
  12261. config.stanford_scaleValues(target.minEpochs, target.maxEpochs)
  12262. );
  12263. }
  12264. // Draw Axis
  12265. axis = $$.colorScale
  12266. .append('g')
  12267. .attr('class', 'legend axis')
  12268. .attr('transform', `translate(${barWidth},0)`)
  12269. .call(legendAxis);
  12270. if (config.stanford_scaleFormat === 'pow10') {
  12271. axis
  12272. .selectAll('.tick text')
  12273. .text(null)
  12274. .filter(powerOfTen)
  12275. .text(10)
  12276. .append('tspan')
  12277. .attr('dy', '-.7em') // https://bl.ocks.org/mbostock/6738229
  12278. .text(function(d) {
  12279. return Math.round(Math.log(d) / Math.LN10)
  12280. });
  12281. }
  12282. $$.colorScale.attr(
  12283. 'transform',
  12284. `translate(${$$.currentWidth - $$.xForColorScale()}, 0)`
  12285. );
  12286. };
  12287. ChartInternal.prototype.xForColorScale = function() {
  12288. var $$ = this;
  12289. return $$.config.stanford_padding.right + getBBox($$.colorScale.node()).width
  12290. };
  12291. ChartInternal.prototype.getColorScalePadding = function() {
  12292. var $$ = this;
  12293. return $$.xForColorScale() + $$.config.stanford_padding.left + 20
  12294. };
  12295. ChartInternal.prototype.isStanfordGraphType = function() {
  12296. var $$ = this;
  12297. return $$.config.data_type === 'stanford'
  12298. };
  12299. ChartInternal.prototype.initStanfordData = function() {
  12300. var $$ = this,
  12301. d3 = $$.d3,
  12302. config = $$.config,
  12303. target = $$.data.targets[0],
  12304. epochs,
  12305. maxEpochs,
  12306. minEpochs;
  12307. // Make larger values appear on top
  12308. target.values.sort(compareEpochs);
  12309. // Get array of epochs
  12310. epochs = target.values.map(a => a.epochs);
  12311. minEpochs = !isNaN(config.stanford_scaleMin)
  12312. ? config.stanford_scaleMin
  12313. : d3.min(epochs);
  12314. maxEpochs = !isNaN(config.stanford_scaleMax)
  12315. ? config.stanford_scaleMax
  12316. : d3.max(epochs);
  12317. if (minEpochs > maxEpochs) {
  12318. throw Error('Number of minEpochs has to be smaller than maxEpochs')
  12319. }
  12320. target.colors = isFunction(config.stanford_colors)
  12321. ? config.stanford_colors
  12322. : d3.interpolateHslLong(d3.hsl(250, 1, 0.5), d3.hsl(0, 1, 0.5));
  12323. target.colorscale = d3
  12324. .scaleSequentialLog(target.colors)
  12325. .domain([minEpochs, maxEpochs]);
  12326. target.minEpochs = minEpochs;
  12327. target.maxEpochs = maxEpochs;
  12328. };
  12329. ChartInternal.prototype.getStanfordPointColor = function(d) {
  12330. var $$ = this,
  12331. target = $$.data.targets[0];
  12332. return target.colorscale(d.epochs)
  12333. };
  12334. // http://jsfiddle.net/Xotic750/KtzLq/
  12335. ChartInternal.prototype.getCentroid = function(points) {
  12336. var area = getRegionArea(points);
  12337. var x = 0,
  12338. y = 0,
  12339. i,
  12340. j,
  12341. f,
  12342. point1,
  12343. point2;
  12344. for (i = 0, j = points.length - 1; i < points.length; j = i, i += 1) {
  12345. point1 = points[i];
  12346. point2 = points[j];
  12347. f = point1.x * point2.y - point2.x * point1.y;
  12348. x += (point1.x + point2.x) * f;
  12349. y += (point1.y + point2.y) * f;
  12350. }
  12351. f = area * 6;
  12352. return {
  12353. x: x / f,
  12354. y: y / f
  12355. }
  12356. };
  12357. ChartInternal.prototype.getStanfordTooltipTitle = function(d) {
  12358. var $$ = this,
  12359. labelX = $$.axis.getLabelText('x'),
  12360. labelY = $$.axis.getLabelText('y');
  12361. return `
  12362. <tr><th>${labelX ? sanitise(labelX) : 'x'}</th><th class='value'>${
  12363. d.x
  12364. }</th></tr>
  12365. <tr><th>${labelY ? sanitise(labelY) : 'y'}</th><th class='value'>${
  12366. d.value
  12367. }</th></tr>
  12368. `
  12369. };
  12370. ChartInternal.prototype.countEpochsInRegion = function(region) {
  12371. var $$ = this,
  12372. target = $$.data.targets[0],
  12373. total,
  12374. count;
  12375. total = target.values.reduce(
  12376. (accumulator, currentValue) => accumulator + Number(currentValue.epochs),
  12377. 0
  12378. );
  12379. count = target.values.reduce((accumulator, currentValue) => {
  12380. if (pointInRegion(currentValue, region)) {
  12381. return accumulator + Number(currentValue.epochs)
  12382. }
  12383. return accumulator
  12384. }, 0);
  12385. return {
  12386. value: count,
  12387. percentage: count !== 0 ? ((count / total) * 100).toFixed(1) : 0
  12388. }
  12389. };
  12390. var getRegionArea = function(points) {
  12391. // thanks to: https://stackoverflow.com/questions/16282330/find-centerpoint-of-polygon-in-javascript
  12392. var area = 0,
  12393. i,
  12394. j,
  12395. point1,
  12396. point2;
  12397. for (i = 0, j = points.length - 1; i < points.length; j = i, i += 1) {
  12398. point1 = points[i];
  12399. point2 = points[j];
  12400. area += point1.x * point2.y;
  12401. area -= point1.y * point2.x;
  12402. }
  12403. area /= 2;
  12404. return area
  12405. };
  12406. var pointInRegion = function(point, region) {
  12407. // thanks to: http://bl.ocks.org/bycoffe/5575904
  12408. // ray-casting algorithm based on
  12409. // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
  12410. let xi,
  12411. yi,
  12412. yj,
  12413. xj,
  12414. intersect,
  12415. x = point.x,
  12416. y = point.value,
  12417. inside = false;
  12418. for (let i = 0, j = region.length - 1; i < region.length; j = i++) {
  12419. xi = region[i].x;
  12420. yi = region[i].y;
  12421. xj = region[j].x;
  12422. yj = region[j].y;
  12423. intersect = yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi;
  12424. if (intersect) {
  12425. inside = !inside;
  12426. }
  12427. }
  12428. return inside
  12429. };
  12430. var compareEpochs = function(a, b) {
  12431. if (a.epochs < b.epochs) {
  12432. return -1
  12433. }
  12434. if (a.epochs > b.epochs) {
  12435. return 1
  12436. }
  12437. return 0
  12438. };
  12439. ChartInternal.prototype.initStanfordElements = function() {
  12440. var $$ = this;
  12441. // Avoid blocking eventRect
  12442. $$.stanfordElements = $$.main
  12443. .select('.' + CLASS.chart)
  12444. .append('g')
  12445. .attr('class', CLASS.stanfordElements);
  12446. $$.stanfordElements.append('g').attr('class', CLASS.stanfordLines);
  12447. $$.stanfordElements.append('g').attr('class', CLASS.stanfordTexts);
  12448. $$.stanfordElements.append('g').attr('class', CLASS.stanfordRegions);
  12449. };
  12450. ChartInternal.prototype.updateStanfordElements = function(duration) {
  12451. var $$ = this,
  12452. main = $$.main,
  12453. config = $$.config,
  12454. stanfordLine,
  12455. stanfordLineEnter,
  12456. stanfordRegion,
  12457. stanfordRegionEnter,
  12458. stanfordText,
  12459. stanfordTextEnter,
  12460. xvCustom = $$.xvCustom.bind($$),
  12461. yvCustom = $$.yvCustom.bind($$),
  12462. countPointsInRegion = $$.countEpochsInRegion.bind($$);
  12463. // Stanford-Lines
  12464. stanfordLine = main
  12465. .select('.' + CLASS.stanfordLines)
  12466. .style('shape-rendering', 'geometricprecision')
  12467. .selectAll('.' + CLASS.stanfordLine)
  12468. .data(config.stanford_lines);
  12469. // enter
  12470. stanfordLineEnter = stanfordLine
  12471. .enter()
  12472. .append('g')
  12473. .attr('class', function(d) {
  12474. return CLASS.stanfordLine + (d['class'] ? ' ' + d['class'] : '')
  12475. });
  12476. stanfordLineEnter
  12477. .append('line')
  12478. .attr('x1', d =>
  12479. config.axis_rotated ? yvCustom(d, 'value_y1') : xvCustom(d, 'value_x1')
  12480. )
  12481. .attr('x2', d =>
  12482. config.axis_rotated ? yvCustom(d, 'value_y2') : xvCustom(d, 'value_x2')
  12483. )
  12484. .attr('y1', d =>
  12485. config.axis_rotated ? xvCustom(d, 'value_x1') : yvCustom(d, 'value_y1')
  12486. )
  12487. .attr('y2', d =>
  12488. config.axis_rotated ? xvCustom(d, 'value_x2') : yvCustom(d, 'value_y2')
  12489. )
  12490. .style('opacity', 0);
  12491. // update
  12492. $$.stanfordLines = stanfordLineEnter.merge(stanfordLine);
  12493. $$.stanfordLines
  12494. .select('line')
  12495. .transition()
  12496. .duration(duration)
  12497. .attr('x1', d =>
  12498. config.axis_rotated ? yvCustom(d, 'value_y1') : xvCustom(d, 'value_x1')
  12499. )
  12500. .attr('x2', d =>
  12501. config.axis_rotated ? yvCustom(d, 'value_y2') : xvCustom(d, 'value_x2')
  12502. )
  12503. .attr('y1', d =>
  12504. config.axis_rotated ? xvCustom(d, 'value_x1') : yvCustom(d, 'value_y1')
  12505. )
  12506. .attr('y2', d =>
  12507. config.axis_rotated ? xvCustom(d, 'value_x2') : yvCustom(d, 'value_y2')
  12508. )
  12509. .style('opacity', 1);
  12510. // exit
  12511. stanfordLine
  12512. .exit()
  12513. .transition()
  12514. .duration(duration)
  12515. .style('opacity', 0)
  12516. .remove();
  12517. // Stanford-Text
  12518. stanfordText = main
  12519. .select('.' + CLASS.stanfordTexts)
  12520. .selectAll('.' + CLASS.stanfordText)
  12521. .data(config.stanford_texts);
  12522. // enter
  12523. stanfordTextEnter = stanfordText
  12524. .enter()
  12525. .append('g')
  12526. .attr('class', function(d) {
  12527. return CLASS.stanfordText + (d['class'] ? ' ' + d['class'] : '')
  12528. });
  12529. stanfordTextEnter
  12530. .append('text')
  12531. .attr('x', d => (config.axis_rotated ? yvCustom(d, 'y') : xvCustom(d, 'x')))
  12532. .attr('y', d => (config.axis_rotated ? xvCustom(d, 'x') : yvCustom(d, 'y')))
  12533. .style('opacity', 0);
  12534. // update
  12535. $$.stanfordTexts = stanfordTextEnter.merge(stanfordText);
  12536. $$.stanfordTexts
  12537. .select('text')
  12538. .transition()
  12539. .duration(duration)
  12540. .attr('x', d => (config.axis_rotated ? yvCustom(d, 'y') : xvCustom(d, 'x')))
  12541. .attr('y', d => (config.axis_rotated ? xvCustom(d, 'x') : yvCustom(d, 'y')))
  12542. .text(function(d) {
  12543. return d.content
  12544. })
  12545. .style('opacity', 1);
  12546. // exit
  12547. stanfordText
  12548. .exit()
  12549. .transition()
  12550. .duration(duration)
  12551. .style('opacity', 0)
  12552. .remove();
  12553. // Stanford-Regions
  12554. stanfordRegion = main
  12555. .select('.' + CLASS.stanfordRegions)
  12556. .selectAll('.' + CLASS.stanfordRegion)
  12557. .data(config.stanford_regions);
  12558. // enter
  12559. stanfordRegionEnter = stanfordRegion
  12560. .enter()
  12561. .append('g')
  12562. .attr('class', function(d) {
  12563. return CLASS.stanfordRegion + (d['class'] ? ' ' + d['class'] : '')
  12564. });
  12565. stanfordRegionEnter
  12566. .append('polygon')
  12567. .attr('points', d => {
  12568. return d.points
  12569. .map(value => {
  12570. return [
  12571. config.axis_rotated ? yvCustom(value, 'y') : xvCustom(value, 'x'),
  12572. config.axis_rotated ? xvCustom(value, 'x') : yvCustom(value, 'y')
  12573. ].join(',')
  12574. })
  12575. .join(' ')
  12576. })
  12577. .style('opacity', 0);
  12578. stanfordRegionEnter
  12579. .append('text')
  12580. .attr('x', d => $$.getCentroid(d.points).x)
  12581. .attr('y', d => $$.getCentroid(d.points).y)
  12582. .style('opacity', 0);
  12583. // update
  12584. $$.stanfordRegions = stanfordRegionEnter.merge(stanfordRegion);
  12585. $$.stanfordRegions
  12586. .select('polygon')
  12587. .transition()
  12588. .duration(duration)
  12589. .attr('points', d => {
  12590. return d.points
  12591. .map(value => {
  12592. return [
  12593. config.axis_rotated ? yvCustom(value, 'y') : xvCustom(value, 'x'),
  12594. config.axis_rotated ? xvCustom(value, 'x') : yvCustom(value, 'y')
  12595. ].join(',')
  12596. })
  12597. .join(' ')
  12598. })
  12599. .style('opacity', d => {
  12600. return d.opacity ? d.opacity : 0.2
  12601. });
  12602. $$.stanfordRegions
  12603. .select('text')
  12604. .transition()
  12605. .duration(duration)
  12606. .attr('x', d =>
  12607. config.axis_rotated
  12608. ? yvCustom($$.getCentroid(d.points), 'y')
  12609. : xvCustom($$.getCentroid(d.points), 'x')
  12610. )
  12611. .attr('y', d =>
  12612. config.axis_rotated
  12613. ? xvCustom($$.getCentroid(d.points), 'x')
  12614. : yvCustom($$.getCentroid(d.points), 'y')
  12615. )
  12616. .text(function(d) {
  12617. if (d.text) {
  12618. var value, percentage, temp;
  12619. if ($$.isStanfordGraphType()) {
  12620. temp = countPointsInRegion(d.points);
  12621. value = temp.value;
  12622. percentage = temp.percentage;
  12623. }
  12624. return d.text(value, percentage)
  12625. }
  12626. return ''
  12627. })
  12628. .attr('text-anchor', 'middle')
  12629. .attr('dominant-baseline', 'middle')
  12630. .style('opacity', 1);
  12631. // exit
  12632. stanfordRegion
  12633. .exit()
  12634. .transition()
  12635. .duration(duration)
  12636. .style('opacity', 0)
  12637. .remove();
  12638. };
  12639. ChartInternal.prototype.initTooltip = function() {
  12640. var $$ = this,
  12641. config = $$.config,
  12642. i;
  12643. $$.tooltip = $$.selectChart
  12644. .style('position', 'relative')
  12645. .append('div')
  12646. .attr('class', CLASS.tooltipContainer)
  12647. .style('position', 'absolute')
  12648. .style('pointer-events', 'none')
  12649. .style('display', 'none');
  12650. // Show tooltip if needed
  12651. if (config.tooltip_init_show) {
  12652. if ($$.isTimeSeries() && isString(config.tooltip_init_x)) {
  12653. config.tooltip_init_x = $$.parseDate(config.tooltip_init_x);
  12654. for (i = 0; i < $$.data.targets[0].values.length; i++) {
  12655. if ($$.data.targets[0].values[i].x - config.tooltip_init_x === 0) {
  12656. break
  12657. }
  12658. }
  12659. config.tooltip_init_x = i;
  12660. }
  12661. $$.tooltip.html(
  12662. config.tooltip_contents.call(
  12663. $$,
  12664. $$.data.targets.map(function(d) {
  12665. return $$.addName(d.values[config.tooltip_init_x])
  12666. }),
  12667. $$.axis.getXAxisTickFormat(),
  12668. $$.getYFormat($$.hasArcType()),
  12669. $$.color
  12670. )
  12671. );
  12672. $$.tooltip
  12673. .style('top', config.tooltip_init_position.top)
  12674. .style('left', config.tooltip_init_position.left)
  12675. .style('display', 'block');
  12676. }
  12677. };
  12678. ChartInternal.prototype.getTooltipSortFunction = function() {
  12679. var $$ = this,
  12680. config = $$.config;
  12681. if (config.data_groups.length === 0 || config.tooltip_order !== undefined) {
  12682. // if data are not grouped or if an order is specified
  12683. // for the tooltip values we sort them by their values
  12684. var order = config.tooltip_order;
  12685. if (order === undefined) {
  12686. order = config.data_order;
  12687. }
  12688. var valueOf = function(obj) {
  12689. return obj ? obj.value : null
  12690. };
  12691. // if data are not grouped, we sort them by their value
  12692. if (isString(order) && order.toLowerCase() === 'asc') {
  12693. return function(a, b) {
  12694. return valueOf(a) - valueOf(b)
  12695. }
  12696. } else if (isString(order) && order.toLowerCase() === 'desc') {
  12697. return function(a, b) {
  12698. return valueOf(b) - valueOf(a)
  12699. }
  12700. } else if (isFunction(order)) {
  12701. // if the function is from data_order we need
  12702. // to wrap the returned function in order to format
  12703. // the sorted value to the expected format
  12704. var sortFunction = order;
  12705. if (config.tooltip_order === undefined) {
  12706. sortFunction = function(a, b) {
  12707. return order(
  12708. a
  12709. ? {
  12710. id: a.id,
  12711. values: [a]
  12712. }
  12713. : null,
  12714. b
  12715. ? {
  12716. id: b.id,
  12717. values: [b]
  12718. }
  12719. : null
  12720. )
  12721. };
  12722. }
  12723. return sortFunction
  12724. } else if (isArray(order)) {
  12725. return function(a, b) {
  12726. return order.indexOf(a.id) - order.indexOf(b.id)
  12727. }
  12728. }
  12729. } else {
  12730. // if data are grouped, we follow the order of grouped targets
  12731. var ids = $$.orderTargets($$.data.targets).map(function(i) {
  12732. return i.id
  12733. });
  12734. // if it was either asc or desc we need to invert the order
  12735. // returned by orderTargets
  12736. if ($$.isOrderAsc() || $$.isOrderDesc()) {
  12737. ids = ids.reverse();
  12738. }
  12739. return function(a, b) {
  12740. return ids.indexOf(a.id) - ids.indexOf(b.id)
  12741. }
  12742. }
  12743. };
  12744. ChartInternal.prototype.getTooltipContent = function(
  12745. d,
  12746. defaultTitleFormat,
  12747. defaultValueFormat,
  12748. color
  12749. ) {
  12750. var $$ = this,
  12751. config = $$.config,
  12752. titleFormat = config.tooltip_format_title || defaultTitleFormat,
  12753. nameFormat =
  12754. config.tooltip_format_name ||
  12755. function(name) {
  12756. return name
  12757. },
  12758. text,
  12759. i,
  12760. title,
  12761. value,
  12762. name,
  12763. bgcolor;
  12764. var valueFormat = config.tooltip_format_value;
  12765. if (!valueFormat) {
  12766. valueFormat = $$.isTargetNormalized(d.id)
  12767. ? (v, ratio) => `${(ratio * 100).toFixed(2)}%`
  12768. : defaultValueFormat;
  12769. }
  12770. var tooltipSortFunction = this.getTooltipSortFunction();
  12771. if (tooltipSortFunction) {
  12772. d.sort(tooltipSortFunction);
  12773. }
  12774. for (i = 0; i < d.length; i++) {
  12775. if (!(d[i] && (d[i].value || d[i].value === 0))) {
  12776. continue
  12777. }
  12778. if ($$.isStanfordGraphType()) {
  12779. // Custom tooltip for stanford plots
  12780. if (!text) {
  12781. title = $$.getStanfordTooltipTitle(d[i]);
  12782. text = "<table class='" + $$.CLASS.tooltip + "'>" + title;
  12783. }
  12784. bgcolor = $$.getStanfordPointColor(d[i]);
  12785. name = sanitise(config.data_epochs); // Epochs key name
  12786. value = d[i].epochs;
  12787. } else {
  12788. // Regular tooltip
  12789. if (!text) {
  12790. title = sanitise(titleFormat ? titleFormat(d[i].x, d[i].index) : d[i].x);
  12791. text =
  12792. "<table class='" +
  12793. $$.CLASS.tooltip +
  12794. "'>" +
  12795. (title || title === 0
  12796. ? "<tr><th colspan='2'>" + title + '</th></tr>'
  12797. : '');
  12798. }
  12799. value = sanitise(
  12800. valueFormat(d[i].value, d[i].ratio, d[i].id, d[i].index, d)
  12801. );
  12802. if (value !== undefined) {
  12803. // Skip elements when their name is set to null
  12804. if (d[i].name === null) {
  12805. continue
  12806. }
  12807. name = sanitise(nameFormat(d[i].name, d[i].ratio, d[i].id, d[i].index));
  12808. bgcolor = $$.levelColor ? $$.levelColor(d[i].value) : color(d[i].id);
  12809. }
  12810. }
  12811. if (value !== undefined) {
  12812. text +=
  12813. "<tr class='" +
  12814. $$.CLASS.tooltipName +
  12815. '-' +
  12816. $$.getTargetSelectorSuffix(d[i].id) +
  12817. "'>";
  12818. text +=
  12819. "<td class='name'><span style='background-color:" +
  12820. bgcolor +
  12821. "'></span>" +
  12822. name +
  12823. '</td>';
  12824. text += "<td class='value'>" + value + '</td>';
  12825. text += '</tr>';
  12826. }
  12827. }
  12828. return text + '</table>'
  12829. };
  12830. ChartInternal.prototype.tooltipPosition = function(
  12831. dataToShow,
  12832. tWidth,
  12833. tHeight,
  12834. element
  12835. ) {
  12836. var $$ = this,
  12837. config = $$.config,
  12838. d3 = $$.d3;
  12839. var svgLeft, tooltipLeft, tooltipRight, tooltipTop, chartRight;
  12840. var forArc = $$.hasArcType(),
  12841. mouse = d3.mouse(element);
  12842. // Determin tooltip position
  12843. if (forArc) {
  12844. tooltipLeft =
  12845. ($$.width - ($$.isLegendRight ? $$.getLegendWidth() : 0)) / 2 + mouse[0];
  12846. tooltipTop =
  12847. ($$.hasType('gauge') ? $$.height : $$.height / 2) + mouse[1] + 20;
  12848. } else {
  12849. svgLeft = $$.getSvgLeft(true);
  12850. if (config.axis_rotated) {
  12851. tooltipLeft = svgLeft + mouse[0] + 100;
  12852. tooltipRight = tooltipLeft + tWidth;
  12853. chartRight = $$.currentWidth - $$.getCurrentPaddingRight();
  12854. tooltipTop = $$.x(dataToShow[0].x) + 20;
  12855. } else {
  12856. tooltipLeft =
  12857. svgLeft + $$.getCurrentPaddingLeft(true) + $$.x(dataToShow[0].x) + 20;
  12858. tooltipRight = tooltipLeft + tWidth;
  12859. chartRight = svgLeft + $$.currentWidth - $$.getCurrentPaddingRight();
  12860. tooltipTop = mouse[1] + 15;
  12861. }
  12862. if (tooltipRight > chartRight) {
  12863. // 20 is needed for Firefox to keep tooltip width
  12864. tooltipLeft -= tooltipRight - chartRight + 20;
  12865. }
  12866. if (tooltipTop + tHeight > $$.currentHeight) {
  12867. tooltipTop -= tHeight + 30;
  12868. }
  12869. }
  12870. if (tooltipTop < 0) {
  12871. tooltipTop = 0;
  12872. }
  12873. return {
  12874. top: tooltipTop,
  12875. left: tooltipLeft
  12876. }
  12877. };
  12878. ChartInternal.prototype.showTooltip = function(selectedData, element) {
  12879. var $$ = this,
  12880. config = $$.config;
  12881. var tWidth, tHeight, position;
  12882. var forArc = $$.hasArcType(),
  12883. dataToShow = selectedData.filter(function(d) {
  12884. return d && isValue(d.value)
  12885. }),
  12886. positionFunction =
  12887. config.tooltip_position || ChartInternal.prototype.tooltipPosition;
  12888. if (dataToShow.length === 0 || !config.tooltip_show) {
  12889. $$.hideTooltip();
  12890. return
  12891. }
  12892. $$.tooltip
  12893. .html(
  12894. config.tooltip_contents.call(
  12895. $$,
  12896. selectedData,
  12897. $$.axis.getXAxisTickFormat(),
  12898. $$.getYFormat(forArc),
  12899. $$.color
  12900. )
  12901. )
  12902. .style('display', 'block');
  12903. // Get tooltip dimensions
  12904. tWidth = $$.tooltip.property('offsetWidth');
  12905. tHeight = $$.tooltip.property('offsetHeight');
  12906. position = positionFunction.call(this, dataToShow, tWidth, tHeight, element);
  12907. // Set tooltip
  12908. $$.tooltip
  12909. .style('top', position.top + 'px')
  12910. .style('left', position.left + 'px');
  12911. };
  12912. ChartInternal.prototype.hideTooltip = function() {
  12913. this.tooltip.style('display', 'none');
  12914. };
  12915. ChartInternal.prototype.setTargetType = function(targetIds, type) {
  12916. var $$ = this,
  12917. config = $$.config;
  12918. $$.mapToTargetIds(targetIds).forEach(function(id) {
  12919. $$.withoutFadeIn[id] = type === config.data_types[id];
  12920. config.data_types[id] = type;
  12921. });
  12922. if (!targetIds) {
  12923. config.data_type = type;
  12924. }
  12925. };
  12926. ChartInternal.prototype.hasType = function(type, targets) {
  12927. var $$ = this,
  12928. types = $$.config.data_types,
  12929. has = false;
  12930. targets = targets || $$.data.targets;
  12931. if (targets && targets.length) {
  12932. targets.forEach(function(target) {
  12933. var t = types[target.id];
  12934. if ((t && t.indexOf(type) >= 0) || (!t && type === 'line')) {
  12935. has = true;
  12936. }
  12937. });
  12938. } else if (Object.keys(types).length) {
  12939. Object.keys(types).forEach(function(id) {
  12940. if (types[id] === type) {
  12941. has = true;
  12942. }
  12943. });
  12944. } else {
  12945. has = $$.config.data_type === type;
  12946. }
  12947. return has
  12948. };
  12949. ChartInternal.prototype.hasArcType = function(targets) {
  12950. return (
  12951. this.hasType('pie', targets) ||
  12952. this.hasType('donut', targets) ||
  12953. this.hasType('gauge', targets)
  12954. )
  12955. };
  12956. ChartInternal.prototype.isLineType = function(d) {
  12957. var config = this.config,
  12958. id = isString(d) ? d : d.id;
  12959. return (
  12960. !config.data_types[id] ||
  12961. ['line', 'spline', 'area', 'area-spline', 'step', 'area-step'].indexOf(
  12962. config.data_types[id]
  12963. ) >= 0
  12964. )
  12965. };
  12966. ChartInternal.prototype.isStepType = function(d) {
  12967. var id = isString(d) ? d : d.id;
  12968. return ['step', 'area-step'].indexOf(this.config.data_types[id]) >= 0
  12969. };
  12970. ChartInternal.prototype.isSplineType = function(d) {
  12971. var id = isString(d) ? d : d.id;
  12972. return ['spline', 'area-spline'].indexOf(this.config.data_types[id]) >= 0
  12973. };
  12974. ChartInternal.prototype.isAreaType = function(d) {
  12975. var id = isString(d) ? d : d.id;
  12976. return (
  12977. ['area', 'area-spline', 'area-step'].indexOf(this.config.data_types[id]) >=
  12978. 0
  12979. )
  12980. };
  12981. ChartInternal.prototype.isBarType = function(d) {
  12982. var id = isString(d) ? d : d.id;
  12983. return this.config.data_types[id] === 'bar'
  12984. };
  12985. ChartInternal.prototype.isScatterType = function(d) {
  12986. var id = isString(d) ? d : d.id;
  12987. return this.config.data_types[id] === 'scatter'
  12988. };
  12989. ChartInternal.prototype.isStanfordType = function(d) {
  12990. var id = isString(d) ? d : d.id;
  12991. return this.config.data_types[id] === 'stanford'
  12992. };
  12993. ChartInternal.prototype.isPieType = function(d) {
  12994. var id = isString(d) ? d : d.id;
  12995. return this.config.data_types[id] === 'pie'
  12996. };
  12997. ChartInternal.prototype.isGaugeType = function(d) {
  12998. var id = isString(d) ? d : d.id;
  12999. return this.config.data_types[id] === 'gauge'
  13000. };
  13001. ChartInternal.prototype.isDonutType = function(d) {
  13002. var id = isString(d) ? d : d.id;
  13003. return this.config.data_types[id] === 'donut'
  13004. };
  13005. ChartInternal.prototype.isArcType = function(d) {
  13006. return this.isPieType(d) || this.isDonutType(d) || this.isGaugeType(d)
  13007. };
  13008. ChartInternal.prototype.lineData = function(d) {
  13009. return this.isLineType(d) ? [d] : []
  13010. };
  13011. ChartInternal.prototype.arcData = function(d) {
  13012. return this.isArcType(d.data) ? [d] : []
  13013. };
  13014. /* not used
  13015. function scatterData(d) {
  13016. return isScatterType(d) ? d.values : [];
  13017. }
  13018. */
  13019. ChartInternal.prototype.barData = function(d) {
  13020. return this.isBarType(d) ? d.values : []
  13021. };
  13022. ChartInternal.prototype.lineOrScatterOrStanfordData = function(d) {
  13023. return this.isLineType(d) || this.isScatterType(d) || this.isStanfordType(d)
  13024. ? d.values
  13025. : []
  13026. };
  13027. ChartInternal.prototype.barOrLineData = function(d) {
  13028. return this.isBarType(d) || this.isLineType(d) ? d.values : []
  13029. };
  13030. ChartInternal.prototype.isSafari = function() {
  13031. var ua = window.navigator.userAgent;
  13032. return ua.indexOf('Safari') >= 0 && ua.indexOf('Chrome') < 0
  13033. };
  13034. ChartInternal.prototype.isChrome = function() {
  13035. var ua = window.navigator.userAgent;
  13036. return ua.indexOf('Chrome') >= 0
  13037. };
  13038. ChartInternal.prototype.initZoom = function() {
  13039. var $$ = this,
  13040. d3 = $$.d3,
  13041. config = $$.config,
  13042. startEvent;
  13043. $$.zoom = d3
  13044. .zoom()
  13045. .on('start', function() {
  13046. if (config.zoom_type !== 'scroll') {
  13047. return
  13048. }
  13049. var e = d3.event.sourceEvent;
  13050. if (e && e.type === 'brush') {
  13051. return
  13052. }
  13053. startEvent = e;
  13054. config.zoom_onzoomstart.call($$.api, e);
  13055. })
  13056. .on('zoom', function() {
  13057. if (config.zoom_type !== 'scroll') {
  13058. return
  13059. }
  13060. var e = d3.event.sourceEvent;
  13061. if (e && e.type === 'brush') {
  13062. return
  13063. }
  13064. $$.redrawForZoom();
  13065. config.zoom_onzoom.call($$.api, $$.x.orgDomain());
  13066. })
  13067. .on('end', function() {
  13068. if (config.zoom_type !== 'scroll') {
  13069. return
  13070. }
  13071. var e = d3.event.sourceEvent;
  13072. if (e && e.type === 'brush') {
  13073. return
  13074. }
  13075. // if click, do nothing. otherwise, click interaction will be canceled.
  13076. if (
  13077. e &&
  13078. startEvent.clientX === e.clientX &&
  13079. startEvent.clientY === e.clientY
  13080. ) {
  13081. return
  13082. }
  13083. config.zoom_onzoomend.call($$.api, $$.x.orgDomain());
  13084. });
  13085. $$.zoom.updateDomain = function() {
  13086. if (d3.event && d3.event.transform) {
  13087. $$.x.domain(d3.event.transform.rescaleX($$.subX).domain());
  13088. }
  13089. return this
  13090. };
  13091. $$.zoom.updateExtent = function() {
  13092. this.scaleExtent([1, Infinity])
  13093. .translateExtent([
  13094. [0, 0],
  13095. [$$.width, $$.height]
  13096. ])
  13097. .extent([
  13098. [0, 0],
  13099. [$$.width, $$.height]
  13100. ]);
  13101. return this
  13102. };
  13103. $$.zoom.update = function() {
  13104. return this.updateExtent().updateDomain()
  13105. };
  13106. return $$.zoom.updateExtent()
  13107. };
  13108. ChartInternal.prototype.zoomTransform = function(range) {
  13109. var $$ = this,
  13110. s = [$$.x(range[0]), $$.x(range[1])];
  13111. return $$.d3.zoomIdentity.scale($$.width / (s[1] - s[0])).translate(-s[0], 0)
  13112. };
  13113. ChartInternal.prototype.initDragZoom = function() {
  13114. const $$ = this;
  13115. const d3 = $$.d3;
  13116. const config = $$.config;
  13117. const context = ($$.context = $$.svg);
  13118. const brushXPos = $$.margin.left + 20.5;
  13119. const brushYPos = $$.margin.top + 0.5;
  13120. if (!(config.zoom_type === 'drag' && config.zoom_enabled)) {
  13121. return
  13122. }
  13123. const getZoomedDomain = selection =>
  13124. selection && selection.map(x => $$.x.invert(x));
  13125. const brush = ($$.dragZoomBrush = d3
  13126. .brushX()
  13127. .on('start', () => {
  13128. $$.api.unzoom();
  13129. $$.svg.select('.' + CLASS.dragZoom).classed('disabled', false);
  13130. config.zoom_onzoomstart.call($$.api, d3.event.sourceEvent);
  13131. })
  13132. .on('brush', () => {
  13133. config.zoom_onzoom.call($$.api, getZoomedDomain(d3.event.selection));
  13134. })
  13135. .on('end', () => {
  13136. if (d3.event.selection == null) {
  13137. return
  13138. }
  13139. const zoomedDomain = getZoomedDomain(d3.event.selection);
  13140. if (!config.zoom_disableDefaultBehavior) {
  13141. $$.api.zoom(zoomedDomain);
  13142. }
  13143. $$.svg.select('.' + CLASS.dragZoom).classed('disabled', true);
  13144. config.zoom_onzoomend.call($$.api, zoomedDomain);
  13145. }));
  13146. context
  13147. .append('g')
  13148. .classed(CLASS.dragZoom, true)
  13149. .attr('clip-path', $$.clipPath)
  13150. .attr('transform', 'translate(' + brushXPos + ',' + brushYPos + ')')
  13151. .call(brush);
  13152. };
  13153. ChartInternal.prototype.getZoomDomain = function() {
  13154. var $$ = this,
  13155. config = $$.config,
  13156. d3 = $$.d3,
  13157. min = d3.min([$$.orgXDomain[0], config.zoom_x_min]),
  13158. max = d3.max([$$.orgXDomain[1], config.zoom_x_max]);
  13159. return [min, max]
  13160. };
  13161. ChartInternal.prototype.redrawForZoom = function() {
  13162. var $$ = this,
  13163. d3 = $$.d3,
  13164. config = $$.config,
  13165. zoom = $$.zoom,
  13166. x = $$.x;
  13167. if (!config.zoom_enabled) {
  13168. return
  13169. }
  13170. if ($$.filterTargetsToShow($$.data.targets).length === 0) {
  13171. return
  13172. }
  13173. zoom.update();
  13174. if (config.zoom_disableDefaultBehavior) {
  13175. return
  13176. }
  13177. if ($$.isCategorized() && x.orgDomain()[0] === $$.orgXDomain[0]) {
  13178. x.domain([$$.orgXDomain[0] - 1e-10, x.orgDomain()[1]]);
  13179. }
  13180. $$.redraw({
  13181. withTransition: false,
  13182. withY: config.zoom_rescale,
  13183. withSubchart: false,
  13184. withEventRect: false,
  13185. withDimension: false
  13186. });
  13187. if (d3.event.sourceEvent && d3.event.sourceEvent.type === 'mousemove') {
  13188. $$.cancelClick = true;
  13189. }
  13190. };
  13191. export default c3;