c3.js 353 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322
  1. /* @license C3.js v0.4.21 | (c) C3 Team and other contributors | http://c3js.org/ */
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  4. typeof define === 'function' && define.amd ? define(factory) :
  5. (global.c3 = factory());
  6. }(this, (function () { 'use strict';
  7. var CLASS = {
  8. target: 'c3-target',
  9. chart: 'c3-chart',
  10. chartLine: 'c3-chart-line',
  11. chartLines: 'c3-chart-lines',
  12. chartBar: 'c3-chart-bar',
  13. chartBars: 'c3-chart-bars',
  14. chartText: 'c3-chart-text',
  15. chartTexts: 'c3-chart-texts',
  16. chartArc: 'c3-chart-arc',
  17. chartArcs: 'c3-chart-arcs',
  18. chartArcsTitle: 'c3-chart-arcs-title',
  19. chartArcsBackground: 'c3-chart-arcs-background',
  20. chartArcsGaugeUnit: 'c3-chart-arcs-gauge-unit',
  21. chartArcsGaugeMax: 'c3-chart-arcs-gauge-max',
  22. chartArcsGaugeMin: 'c3-chart-arcs-gauge-min',
  23. selectedCircle: 'c3-selected-circle',
  24. selectedCircles: 'c3-selected-circles',
  25. eventRect: 'c3-event-rect',
  26. eventRects: 'c3-event-rects',
  27. eventRectsSingle: 'c3-event-rects-single',
  28. eventRectsMultiple: 'c3-event-rects-multiple',
  29. zoomRect: 'c3-zoom-rect',
  30. brush: 'c3-brush',
  31. focused: 'c3-focused',
  32. defocused: 'c3-defocused',
  33. region: 'c3-region',
  34. regions: 'c3-regions',
  35. title: 'c3-title',
  36. tooltipContainer: 'c3-tooltip-container',
  37. tooltip: 'c3-tooltip',
  38. tooltipName: 'c3-tooltip-name',
  39. shape: 'c3-shape',
  40. shapes: 'c3-shapes',
  41. line: 'c3-line',
  42. lines: 'c3-lines',
  43. bar: 'c3-bar',
  44. bars: 'c3-bars',
  45. circle: 'c3-circle',
  46. circles: 'c3-circles',
  47. arc: 'c3-arc',
  48. arcLabelLine: 'c3-arc-label-line',
  49. arcs: 'c3-arcs',
  50. area: 'c3-area',
  51. areas: 'c3-areas',
  52. empty: 'c3-empty',
  53. text: 'c3-text',
  54. texts: 'c3-texts',
  55. gaugeValue: 'c3-gauge-value',
  56. grid: 'c3-grid',
  57. gridLines: 'c3-grid-lines',
  58. xgrid: 'c3-xgrid',
  59. xgrids: 'c3-xgrids',
  60. xgridLine: 'c3-xgrid-line',
  61. xgridLines: 'c3-xgrid-lines',
  62. xgridFocus: 'c3-xgrid-focus',
  63. ygrid: 'c3-ygrid',
  64. ygrids: 'c3-ygrids',
  65. ygridLine: 'c3-ygrid-line',
  66. ygridLines: 'c3-ygrid-lines',
  67. axis: 'c3-axis',
  68. axisX: 'c3-axis-x',
  69. axisXLabel: 'c3-axis-x-label',
  70. axisY: 'c3-axis-y',
  71. axisYLabel: 'c3-axis-y-label',
  72. axisY2: 'c3-axis-y2',
  73. axisY2Label: 'c3-axis-y2-label',
  74. legendBackground: 'c3-legend-background',
  75. legendItem: 'c3-legend-item',
  76. legendItemEvent: 'c3-legend-item-event',
  77. legendItemTile: 'c3-legend-item-tile',
  78. legendItemHidden: 'c3-legend-item-hidden',
  79. legendItemFocused: 'c3-legend-item-focused',
  80. dragarea: 'c3-dragarea',
  81. EXPANDED: '_expanded_',
  82. SELECTED: '_selected_',
  83. INCLUDED: '_included_'
  84. };
  85. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
  86. return typeof obj;
  87. } : function (obj) {
  88. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  89. };
  90. var classCallCheck = function (instance, Constructor) {
  91. if (!(instance instanceof Constructor)) {
  92. throw new TypeError("Cannot call a class as a function");
  93. }
  94. };
  95. var inherits = function (subClass, superClass) {
  96. if (typeof superClass !== "function" && superClass !== null) {
  97. throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
  98. }
  99. subClass.prototype = Object.create(superClass && superClass.prototype, {
  100. constructor: {
  101. value: subClass,
  102. enumerable: false,
  103. writable: true,
  104. configurable: true
  105. }
  106. });
  107. if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
  108. };
  109. var possibleConstructorReturn = function (self, call) {
  110. if (!self) {
  111. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  112. }
  113. return call && (typeof call === "object" || typeof call === "function") ? call : self;
  114. };
  115. var isValue = function isValue(v) {
  116. return v || v === 0;
  117. };
  118. var isFunction = function isFunction(o) {
  119. return typeof o === 'function';
  120. };
  121. var isArray = function isArray(o) {
  122. return Array.isArray(o);
  123. };
  124. var isString = function isString(o) {
  125. return typeof o === 'string';
  126. };
  127. var isUndefined = function isUndefined(v) {
  128. return typeof v === 'undefined';
  129. };
  130. var isDefined = function isDefined(v) {
  131. return typeof v !== 'undefined';
  132. };
  133. var ceil10 = function ceil10(v) {
  134. return Math.ceil(v / 10) * 10;
  135. };
  136. var asHalfPixel = function asHalfPixel(n) {
  137. return Math.ceil(n) + 0.5;
  138. };
  139. var diffDomain = function diffDomain(d) {
  140. return d[1] - d[0];
  141. };
  142. var isEmpty = function isEmpty(o) {
  143. return typeof o === 'undefined' || o === null || isString(o) && o.length === 0 || (typeof o === 'undefined' ? 'undefined' : _typeof(o)) === 'object' && Object.keys(o).length === 0;
  144. };
  145. var notEmpty = function notEmpty(o) {
  146. return !c3_chart_internal_fn.isEmpty(o);
  147. };
  148. var getOption = function getOption(options, key, defaultValue) {
  149. return isDefined(options[key]) ? options[key] : defaultValue;
  150. };
  151. var hasValue = function hasValue(dict, value) {
  152. var found = false;
  153. Object.keys(dict).forEach(function (key) {
  154. if (dict[key] === value) {
  155. found = true;
  156. }
  157. });
  158. return found;
  159. };
  160. var sanitise = function sanitise(str) {
  161. return typeof str === 'string' ? str.replace(/</g, '&lt;').replace(/>/g, '&gt;') : str;
  162. };
  163. var getPathBox = function getPathBox(path) {
  164. var box = path.getBoundingClientRect(),
  165. items = [path.pathSegList.getItem(0), path.pathSegList.getItem(1)],
  166. minX = items[0].x,
  167. minY = Math.min(items[0].y, items[1].y);
  168. return { x: minX, y: minY, width: box.width, height: box.height };
  169. };
  170. var c3_axis_fn;
  171. var c3_axis_internal_fn;
  172. function AxisInternal(component, params) {
  173. var internal = this;
  174. internal.component = component;
  175. internal.params = params || {};
  176. internal.d3 = component.d3;
  177. internal.scale = internal.d3.scale.linear();
  178. internal.range;
  179. internal.orient = "bottom";
  180. internal.innerTickSize = 6;
  181. internal.outerTickSize = this.params.withOuterTick ? 6 : 0;
  182. internal.tickPadding = 3;
  183. internal.tickValues = null;
  184. internal.tickFormat;
  185. internal.tickArguments;
  186. internal.tickOffset = 0;
  187. internal.tickCulling = true;
  188. internal.tickCentered;
  189. internal.tickTextCharSize;
  190. internal.tickTextRotate = internal.params.tickTextRotate;
  191. internal.tickLength;
  192. internal.axis = internal.generateAxis();
  193. }
  194. c3_axis_internal_fn = AxisInternal.prototype;
  195. c3_axis_internal_fn.axisX = function (selection, x, tickOffset) {
  196. selection.attr("transform", function (d) {
  197. return "translate(" + Math.ceil(x(d) + tickOffset) + ", 0)";
  198. });
  199. };
  200. c3_axis_internal_fn.axisY = function (selection, y) {
  201. selection.attr("transform", function (d) {
  202. return "translate(0," + Math.ceil(y(d)) + ")";
  203. });
  204. };
  205. c3_axis_internal_fn.scaleExtent = function (domain) {
  206. var start = domain[0],
  207. stop = domain[domain.length - 1];
  208. return start < stop ? [start, stop] : [stop, start];
  209. };
  210. c3_axis_internal_fn.generateTicks = function (scale) {
  211. var internal = this;
  212. var i,
  213. domain,
  214. ticks = [];
  215. if (scale.ticks) {
  216. return scale.ticks.apply(scale, internal.tickArguments);
  217. }
  218. domain = scale.domain();
  219. for (i = Math.ceil(domain[0]); i < domain[1]; i++) {
  220. ticks.push(i);
  221. }
  222. if (ticks.length > 0 && ticks[0] > 0) {
  223. ticks.unshift(ticks[0] - (ticks[1] - ticks[0]));
  224. }
  225. return ticks;
  226. };
  227. c3_axis_internal_fn.copyScale = function () {
  228. var internal = this;
  229. var newScale = internal.scale.copy(),
  230. domain;
  231. if (internal.params.isCategory) {
  232. domain = internal.scale.domain();
  233. newScale.domain([domain[0], domain[1] - 1]);
  234. }
  235. return newScale;
  236. };
  237. c3_axis_internal_fn.textFormatted = function (v) {
  238. var internal = this,
  239. formatted = internal.tickFormat ? internal.tickFormat(v) : v;
  240. return typeof formatted !== 'undefined' ? formatted : '';
  241. };
  242. c3_axis_internal_fn.updateRange = function () {
  243. var internal = this;
  244. internal.range = internal.scale.rangeExtent ? internal.scale.rangeExtent() : internal.scaleExtent(internal.scale.range());
  245. return internal.range;
  246. };
  247. c3_axis_internal_fn.updateTickTextCharSize = function (tick) {
  248. var internal = this;
  249. if (internal.tickTextCharSize) {
  250. return internal.tickTextCharSize;
  251. }
  252. var size = {
  253. h: 11.5,
  254. w: 5.5
  255. };
  256. tick.select('text').text(function (d) {
  257. return internal.textFormatted(d);
  258. }).each(function (d) {
  259. var box = this.getBoundingClientRect(),
  260. text = internal.textFormatted(d),
  261. h = box.height,
  262. w = text ? box.width / text.length : undefined;
  263. if (h && w) {
  264. size.h = h;
  265. size.w = w;
  266. }
  267. }).text('');
  268. internal.tickTextCharSize = size;
  269. return size;
  270. };
  271. c3_axis_internal_fn.transitionise = function (selection) {
  272. return this.params.withoutTransition ? selection : this.d3.transition(selection);
  273. };
  274. c3_axis_internal_fn.isVertical = function () {
  275. return this.orient === 'left' || this.orient === 'right';
  276. };
  277. c3_axis_internal_fn.tspanData = function (d, i, ticks, scale) {
  278. var internal = this;
  279. var splitted = internal.params.tickMultiline ? internal.splitTickText(d, ticks, scale) : [].concat(internal.textFormatted(d));
  280. return splitted.map(function (s) {
  281. return { index: i, splitted: s, length: splitted.length };
  282. });
  283. };
  284. c3_axis_internal_fn.splitTickText = function (d, ticks, scale) {
  285. var internal = this,
  286. tickText = internal.textFormatted(d),
  287. maxWidth = internal.params.tickWidth,
  288. subtext,
  289. spaceIndex,
  290. textWidth,
  291. splitted = [];
  292. if (Object.prototype.toString.call(tickText) === "[object Array]") {
  293. return tickText;
  294. }
  295. if (!maxWidth || maxWidth <= 0) {
  296. maxWidth = internal.isVertical() ? 95 : internal.params.isCategory ? Math.ceil(scale(ticks[1]) - scale(ticks[0])) - 12 : 110;
  297. }
  298. function split(splitted, text) {
  299. spaceIndex = undefined;
  300. for (var i = 1; i < text.length; i++) {
  301. if (text.charAt(i) === ' ') {
  302. spaceIndex = i;
  303. }
  304. subtext = text.substr(0, i + 1);
  305. textWidth = internal.tickTextCharSize.w * subtext.length;
  306. // if text width gets over tick width, split by space index or crrent index
  307. if (maxWidth < textWidth) {
  308. return split(splitted.concat(text.substr(0, spaceIndex ? spaceIndex : i)), text.slice(spaceIndex ? spaceIndex + 1 : i));
  309. }
  310. }
  311. return splitted.concat(text);
  312. }
  313. return split(splitted, tickText + "");
  314. };
  315. c3_axis_internal_fn.updateTickLength = function () {
  316. var internal = this;
  317. internal.tickLength = Math.max(internal.innerTickSize, 0) + internal.tickPadding;
  318. };
  319. c3_axis_internal_fn.lineY2 = function (d) {
  320. var internal = this,
  321. tickPosition = internal.scale(d) + (internal.tickCentered ? 0 : internal.tickOffset);
  322. return internal.range[0] < tickPosition && tickPosition < internal.range[1] ? internal.innerTickSize : 0;
  323. };
  324. c3_axis_internal_fn.textY = function () {
  325. var internal = this,
  326. rotate = internal.tickTextRotate;
  327. return rotate ? 11.5 - 2.5 * (rotate / 15) * (rotate > 0 ? 1 : -1) : internal.tickLength;
  328. };
  329. c3_axis_internal_fn.textTransform = function () {
  330. var internal = this,
  331. rotate = internal.tickTextRotate;
  332. return rotate ? "rotate(" + rotate + ")" : "";
  333. };
  334. c3_axis_internal_fn.textTextAnchor = function () {
  335. var internal = this,
  336. rotate = internal.tickTextRotate;
  337. return rotate ? rotate > 0 ? "start" : "end" : "middle";
  338. };
  339. c3_axis_internal_fn.tspanDx = function () {
  340. var internal = this,
  341. rotate = internal.tickTextRotate;
  342. return rotate ? 8 * Math.sin(Math.PI * (rotate / 180)) : 0;
  343. };
  344. c3_axis_internal_fn.tspanDy = function (d, i) {
  345. var internal = this,
  346. dy = internal.tickTextCharSize.h;
  347. if (i === 0) {
  348. if (internal.isVertical()) {
  349. dy = -((d.length - 1) * (internal.tickTextCharSize.h / 2) - 3);
  350. } else {
  351. dy = ".71em";
  352. }
  353. }
  354. return dy;
  355. };
  356. c3_axis_internal_fn.generateAxis = function () {
  357. var internal = this,
  358. d3 = internal.d3,
  359. params = internal.params;
  360. function axis(g) {
  361. g.each(function () {
  362. var g = axis.g = d3.select(this);
  363. var scale0 = this.__chart__ || internal.scale,
  364. scale1 = this.__chart__ = internal.copyScale();
  365. var ticks = internal.tickValues ? internal.tickValues : internal.generateTicks(scale1),
  366. tick = g.selectAll(".tick").data(ticks, scale1),
  367. tickEnter = tick.enter().insert("g", ".domain").attr("class", "tick").style("opacity", 1e-6),
  368. // MEMO: No exit transition. The reason is this transition affects max tick width calculation because old tick will be included in the ticks.
  369. tickExit = tick.exit().remove(),
  370. tickUpdate = internal.transitionise(tick).style("opacity", 1),
  371. tickTransform,
  372. tickX,
  373. tickY;
  374. if (params.isCategory) {
  375. internal.tickOffset = Math.ceil((scale1(1) - scale1(0)) / 2);
  376. tickX = internal.tickCentered ? 0 : internal.tickOffset;
  377. tickY = internal.tickCentered ? internal.tickOffset : 0;
  378. } else {
  379. internal.tickOffset = tickX = 0;
  380. }
  381. tickEnter.append("line");
  382. tickEnter.append("text");
  383. internal.updateRange();
  384. internal.updateTickLength();
  385. internal.updateTickTextCharSize(g.select('.tick'));
  386. var lineUpdate = tickUpdate.select("line"),
  387. textUpdate = tickUpdate.select("text"),
  388. tspanUpdate = tick.select("text").selectAll('tspan').data(function (d, i) {
  389. return internal.tspanData(d, i, ticks, scale1);
  390. });
  391. tspanUpdate.enter().append('tspan');
  392. tspanUpdate.exit().remove();
  393. tspanUpdate.text(function (d) {
  394. return d.splitted;
  395. });
  396. var path = g.selectAll(".domain").data([0]),
  397. pathUpdate = (path.enter().append("path").attr("class", "domain"), internal.transitionise(path));
  398. // TODO: each attr should be one function and change its behavior by internal.orient, probably
  399. switch (internal.orient) {
  400. case "bottom":
  401. {
  402. tickTransform = internal.axisX;
  403. lineUpdate.attr("x1", tickX).attr("x2", tickX).attr("y2", function (d, i) {
  404. return internal.lineY2(d, i);
  405. });
  406. textUpdate.attr("x", 0).attr("y", function (d, i) {
  407. return internal.textY(d, i);
  408. }).attr("transform", function (d, i) {
  409. return internal.textTransform(d, i);
  410. }).style("text-anchor", function (d, i) {
  411. return internal.textTextAnchor(d, i);
  412. });
  413. tspanUpdate.attr('x', 0).attr("dy", function (d, i) {
  414. return internal.tspanDy(d, i);
  415. }).attr('dx', function (d, i) {
  416. return internal.tspanDx(d, i);
  417. });
  418. pathUpdate.attr("d", "M" + internal.range[0] + "," + internal.outerTickSize + "V0H" + internal.range[1] + "V" + internal.outerTickSize);
  419. break;
  420. }
  421. case "top":
  422. {
  423. // TODO: rotated tick text
  424. tickTransform = internal.axisX;
  425. lineUpdate.attr("x2", 0).attr("y2", -internal.innerTickSize);
  426. textUpdate.attr("x", 0).attr("y", -internal.tickLength).style("text-anchor", "middle");
  427. tspanUpdate.attr('x', 0).attr("dy", "0em");
  428. pathUpdate.attr("d", "M" + internal.range[0] + "," + -internal.outerTickSize + "V0H" + internal.range[1] + "V" + -internal.outerTickSize);
  429. break;
  430. }
  431. case "left":
  432. {
  433. tickTransform = internal.axisY;
  434. lineUpdate.attr("x2", -internal.innerTickSize).attr("y1", tickY).attr("y2", tickY);
  435. textUpdate.attr("x", -internal.tickLength).attr("y", internal.tickOffset).style("text-anchor", "end");
  436. tspanUpdate.attr('x', -internal.tickLength).attr("dy", function (d, i) {
  437. return internal.tspanDy(d, i);
  438. });
  439. pathUpdate.attr("d", "M" + -internal.outerTickSize + "," + internal.range[0] + "H0V" + internal.range[1] + "H" + -internal.outerTickSize);
  440. break;
  441. }
  442. case "right":
  443. {
  444. tickTransform = internal.axisY;
  445. lineUpdate.attr("x2", internal.innerTickSize).attr("y2", 0);
  446. textUpdate.attr("x", internal.tickLength).attr("y", 0).style("text-anchor", "start");
  447. tspanUpdate.attr('x', internal.tickLength).attr("dy", function (d, i) {
  448. return internal.tspanDy(d, i);
  449. });
  450. pathUpdate.attr("d", "M" + internal.outerTickSize + "," + internal.range[0] + "H0V" + internal.range[1] + "H" + internal.outerTickSize);
  451. break;
  452. }
  453. }
  454. if (scale1.rangeBand) {
  455. var x = scale1,
  456. dx = x.rangeBand() / 2;
  457. scale0 = scale1 = function scale1(d) {
  458. return x(d) + dx;
  459. };
  460. } else if (scale0.rangeBand) {
  461. scale0 = scale1;
  462. } else {
  463. tickExit.call(tickTransform, scale1, internal.tickOffset);
  464. }
  465. tickEnter.call(tickTransform, scale0, internal.tickOffset);
  466. tickUpdate.call(tickTransform, scale1, internal.tickOffset);
  467. });
  468. }
  469. axis.scale = function (x) {
  470. if (!arguments.length) {
  471. return internal.scale;
  472. }
  473. internal.scale = x;
  474. return axis;
  475. };
  476. axis.orient = function (x) {
  477. if (!arguments.length) {
  478. return internal.orient;
  479. }
  480. internal.orient = x in { top: 1, right: 1, bottom: 1, left: 1 } ? x + "" : "bottom";
  481. return axis;
  482. };
  483. axis.tickFormat = function (format) {
  484. if (!arguments.length) {
  485. return internal.tickFormat;
  486. }
  487. internal.tickFormat = format;
  488. return axis;
  489. };
  490. axis.tickCentered = function (isCentered) {
  491. if (!arguments.length) {
  492. return internal.tickCentered;
  493. }
  494. internal.tickCentered = isCentered;
  495. return axis;
  496. };
  497. axis.tickOffset = function () {
  498. return internal.tickOffset;
  499. };
  500. axis.tickInterval = function () {
  501. var interval, length;
  502. if (params.isCategory) {
  503. interval = internal.tickOffset * 2;
  504. } else {
  505. length = axis.g.select('path.domain').node().getTotalLength() - internal.outerTickSize * 2;
  506. interval = length / axis.g.selectAll('line').size();
  507. }
  508. return interval === Infinity ? 0 : interval;
  509. };
  510. axis.ticks = function () {
  511. if (!arguments.length) {
  512. return internal.tickArguments;
  513. }
  514. internal.tickArguments = arguments;
  515. return axis;
  516. };
  517. axis.tickCulling = function (culling) {
  518. if (!arguments.length) {
  519. return internal.tickCulling;
  520. }
  521. internal.tickCulling = culling;
  522. return axis;
  523. };
  524. axis.tickValues = function (x) {
  525. if (typeof x === 'function') {
  526. internal.tickValues = function () {
  527. return x(internal.scale.domain());
  528. };
  529. } else {
  530. if (!arguments.length) {
  531. return internal.tickValues;
  532. }
  533. internal.tickValues = x;
  534. }
  535. return axis;
  536. };
  537. return axis;
  538. };
  539. var Axis = function (_Component) {
  540. inherits(Axis, _Component);
  541. function Axis(owner) {
  542. classCallCheck(this, Axis);
  543. var fn = {
  544. fn: c3_axis_fn,
  545. internal: {
  546. fn: c3_axis_internal_fn
  547. }
  548. };
  549. var _this = possibleConstructorReturn(this, (Axis.__proto__ || Object.getPrototypeOf(Axis)).call(this, owner, 'axis', fn));
  550. _this.d3 = owner.d3;
  551. _this.internal = AxisInternal;
  552. return _this;
  553. }
  554. return Axis;
  555. }(Component);
  556. c3_axis_fn = Axis.prototype;
  557. c3_axis_fn.init = function init() {
  558. var $$ = this.owner,
  559. config = $$.config,
  560. main = $$.main;
  561. $$.axes.x = main.append("g").attr("class", CLASS.axis + ' ' + CLASS.axisX).attr("clip-path", $$.clipPathForXAxis).attr("transform", $$.getTranslate('x')).style("visibility", config.axis_x_show ? 'visible' : 'hidden');
  562. $$.axes.x.append("text").attr("class", CLASS.axisXLabel).attr("transform", config.axis_rotated ? "rotate(-90)" : "").style("text-anchor", this.textAnchorForXAxisLabel.bind(this));
  563. $$.axes.y = main.append("g").attr("class", CLASS.axis + ' ' + CLASS.axisY).attr("clip-path", config.axis_y_inner ? "" : $$.clipPathForYAxis).attr("transform", $$.getTranslate('y')).style("visibility", config.axis_y_show ? 'visible' : 'hidden');
  564. $$.axes.y.append("text").attr("class", CLASS.axisYLabel).attr("transform", config.axis_rotated ? "" : "rotate(-90)").style("text-anchor", this.textAnchorForYAxisLabel.bind(this));
  565. $$.axes.y2 = main.append("g").attr("class", CLASS.axis + ' ' + CLASS.axisY2)
  566. // clip-path?
  567. .attr("transform", $$.getTranslate('y2')).style("visibility", config.axis_y2_show ? 'visible' : 'hidden');
  568. $$.axes.y2.append("text").attr("class", CLASS.axisY2Label).attr("transform", config.axis_rotated ? "" : "rotate(-90)").style("text-anchor", this.textAnchorForY2AxisLabel.bind(this));
  569. };
  570. c3_axis_fn.getXAxis = function getXAxis(scale, orient, tickFormat, tickValues, withOuterTick, withoutTransition, withoutRotateTickText) {
  571. var $$ = this.owner,
  572. config = $$.config,
  573. axisParams = {
  574. isCategory: $$.isCategorized(),
  575. withOuterTick: withOuterTick,
  576. tickMultiline: config.axis_x_tick_multiline,
  577. tickWidth: config.axis_x_tick_width,
  578. tickTextRotate: withoutRotateTickText ? 0 : config.axis_x_tick_rotate,
  579. withoutTransition: withoutTransition
  580. },
  581. axis = new this.internal(this, axisParams).axis.scale(scale).orient(orient);
  582. if ($$.isTimeSeries() && tickValues && typeof tickValues !== "function") {
  583. tickValues = tickValues.map(function (v) {
  584. return $$.parseDate(v);
  585. });
  586. }
  587. // Set tick
  588. axis.tickFormat(tickFormat).tickValues(tickValues);
  589. if ($$.isCategorized()) {
  590. axis.tickCentered(config.axis_x_tick_centered);
  591. if (isEmpty(config.axis_x_tick_culling)) {
  592. config.axis_x_tick_culling = false;
  593. }
  594. }
  595. return axis;
  596. };
  597. c3_axis_fn.updateXAxisTickValues = function updateXAxisTickValues(targets, axis) {
  598. var $$ = this.owner,
  599. config = $$.config,
  600. tickValues;
  601. if (config.axis_x_tick_fit || config.axis_x_tick_count) {
  602. tickValues = this.generateTickValues($$.mapTargetsToUniqueXs(targets), config.axis_x_tick_count, $$.isTimeSeries());
  603. }
  604. if (axis) {
  605. axis.tickValues(tickValues);
  606. } else {
  607. $$.xAxis.tickValues(tickValues);
  608. $$.subXAxis.tickValues(tickValues);
  609. }
  610. return tickValues;
  611. };
  612. c3_axis_fn.getYAxis = function getYAxis(scale, orient, tickFormat, tickValues, withOuterTick, withoutTransition, withoutRotateTickText) {
  613. var $$ = this.owner,
  614. config = $$.config,
  615. axisParams = {
  616. withOuterTick: withOuterTick,
  617. withoutTransition: withoutTransition,
  618. tickTextRotate: withoutRotateTickText ? 0 : config.axis_y_tick_rotate
  619. },
  620. axis = new this.internal(this, axisParams).axis.scale(scale).orient(orient).tickFormat(tickFormat);
  621. if ($$.isTimeSeriesY()) {
  622. axis.ticks($$.d3.time[config.axis_y_tick_time_value], config.axis_y_tick_time_interval);
  623. } else {
  624. axis.tickValues(tickValues);
  625. }
  626. return axis;
  627. };
  628. c3_axis_fn.getId = function getId(id) {
  629. var config = this.owner.config;
  630. return id in config.data_axes ? config.data_axes[id] : 'y';
  631. };
  632. c3_axis_fn.getXAxisTickFormat = function getXAxisTickFormat() {
  633. var $$ = this.owner,
  634. config = $$.config,
  635. format = $$.isTimeSeries() ? $$.defaultAxisTimeFormat : $$.isCategorized() ? $$.categoryName : function (v) {
  636. return v < 0 ? v.toFixed(0) : v;
  637. };
  638. if (config.axis_x_tick_format) {
  639. if (isFunction(config.axis_x_tick_format)) {
  640. format = config.axis_x_tick_format;
  641. } else if ($$.isTimeSeries()) {
  642. format = function format(date) {
  643. return date ? $$.axisTimeFormat(config.axis_x_tick_format)(date) : "";
  644. };
  645. }
  646. }
  647. return isFunction(format) ? function (v) {
  648. return format.call($$, v);
  649. } : format;
  650. };
  651. c3_axis_fn.getTickValues = function getTickValues(tickValues, axis) {
  652. return tickValues ? tickValues : axis ? axis.tickValues() : undefined;
  653. };
  654. c3_axis_fn.getXAxisTickValues = function getXAxisTickValues() {
  655. return this.getTickValues(this.owner.config.axis_x_tick_values, this.owner.xAxis);
  656. };
  657. c3_axis_fn.getYAxisTickValues = function getYAxisTickValues() {
  658. return this.getTickValues(this.owner.config.axis_y_tick_values, this.owner.yAxis);
  659. };
  660. c3_axis_fn.getY2AxisTickValues = function getY2AxisTickValues() {
  661. return this.getTickValues(this.owner.config.axis_y2_tick_values, this.owner.y2Axis);
  662. };
  663. c3_axis_fn.getLabelOptionByAxisId = function getLabelOptionByAxisId(axisId) {
  664. var $$ = this.owner,
  665. config = $$.config,
  666. option;
  667. if (axisId === 'y') {
  668. option = config.axis_y_label;
  669. } else if (axisId === 'y2') {
  670. option = config.axis_y2_label;
  671. } else if (axisId === 'x') {
  672. option = config.axis_x_label;
  673. }
  674. return option;
  675. };
  676. c3_axis_fn.getLabelText = function getLabelText(axisId) {
  677. var option = this.getLabelOptionByAxisId(axisId);
  678. return isString(option) ? option : option ? option.text : null;
  679. };
  680. c3_axis_fn.setLabelText = function setLabelText(axisId, text) {
  681. var $$ = this.owner,
  682. config = $$.config,
  683. option = this.getLabelOptionByAxisId(axisId);
  684. if (isString(option)) {
  685. if (axisId === 'y') {
  686. config.axis_y_label = text;
  687. } else if (axisId === 'y2') {
  688. config.axis_y2_label = text;
  689. } else if (axisId === 'x') {
  690. config.axis_x_label = text;
  691. }
  692. } else if (option) {
  693. option.text = text;
  694. }
  695. };
  696. c3_axis_fn.getLabelPosition = function getLabelPosition(axisId, defaultPosition) {
  697. var option = this.getLabelOptionByAxisId(axisId),
  698. position = option && (typeof option === 'undefined' ? 'undefined' : _typeof(option)) === 'object' && option.position ? option.position : defaultPosition;
  699. return {
  700. isInner: position.indexOf('inner') >= 0,
  701. isOuter: position.indexOf('outer') >= 0,
  702. isLeft: position.indexOf('left') >= 0,
  703. isCenter: position.indexOf('center') >= 0,
  704. isRight: position.indexOf('right') >= 0,
  705. isTop: position.indexOf('top') >= 0,
  706. isMiddle: position.indexOf('middle') >= 0,
  707. isBottom: position.indexOf('bottom') >= 0
  708. };
  709. };
  710. c3_axis_fn.getXAxisLabelPosition = function getXAxisLabelPosition() {
  711. return this.getLabelPosition('x', this.owner.config.axis_rotated ? 'inner-top' : 'inner-right');
  712. };
  713. c3_axis_fn.getYAxisLabelPosition = function getYAxisLabelPosition() {
  714. return this.getLabelPosition('y', this.owner.config.axis_rotated ? 'inner-right' : 'inner-top');
  715. };
  716. c3_axis_fn.getY2AxisLabelPosition = function getY2AxisLabelPosition() {
  717. return this.getLabelPosition('y2', this.owner.config.axis_rotated ? 'inner-right' : 'inner-top');
  718. };
  719. c3_axis_fn.getLabelPositionById = function getLabelPositionById(id) {
  720. return id === 'y2' ? this.getY2AxisLabelPosition() : id === 'y' ? this.getYAxisLabelPosition() : this.getXAxisLabelPosition();
  721. };
  722. c3_axis_fn.textForXAxisLabel = function textForXAxisLabel() {
  723. return this.getLabelText('x');
  724. };
  725. c3_axis_fn.textForYAxisLabel = function textForYAxisLabel() {
  726. return this.getLabelText('y');
  727. };
  728. c3_axis_fn.textForY2AxisLabel = function textForY2AxisLabel() {
  729. return this.getLabelText('y2');
  730. };
  731. c3_axis_fn.xForAxisLabel = function xForAxisLabel(forHorizontal, position) {
  732. var $$ = this.owner;
  733. if (forHorizontal) {
  734. return position.isLeft ? 0 : position.isCenter ? $$.width / 2 : $$.width;
  735. } else {
  736. return position.isBottom ? -$$.height : position.isMiddle ? -$$.height / 2 : 0;
  737. }
  738. };
  739. c3_axis_fn.dxForAxisLabel = function dxForAxisLabel(forHorizontal, position) {
  740. if (forHorizontal) {
  741. return position.isLeft ? "0.5em" : position.isRight ? "-0.5em" : "0";
  742. } else {
  743. return position.isTop ? "-0.5em" : position.isBottom ? "0.5em" : "0";
  744. }
  745. };
  746. c3_axis_fn.textAnchorForAxisLabel = function textAnchorForAxisLabel(forHorizontal, position) {
  747. if (forHorizontal) {
  748. return position.isLeft ? 'start' : position.isCenter ? 'middle' : 'end';
  749. } else {
  750. return position.isBottom ? 'start' : position.isMiddle ? 'middle' : 'end';
  751. }
  752. };
  753. c3_axis_fn.xForXAxisLabel = function xForXAxisLabel() {
  754. return this.xForAxisLabel(!this.owner.config.axis_rotated, this.getXAxisLabelPosition());
  755. };
  756. c3_axis_fn.xForYAxisLabel = function xForYAxisLabel() {
  757. return this.xForAxisLabel(this.owner.config.axis_rotated, this.getYAxisLabelPosition());
  758. };
  759. c3_axis_fn.xForY2AxisLabel = function xForY2AxisLabel() {
  760. return this.xForAxisLabel(this.owner.config.axis_rotated, this.getY2AxisLabelPosition());
  761. };
  762. c3_axis_fn.dxForXAxisLabel = function dxForXAxisLabel() {
  763. return this.dxForAxisLabel(!this.owner.config.axis_rotated, this.getXAxisLabelPosition());
  764. };
  765. c3_axis_fn.dxForYAxisLabel = function dxForYAxisLabel() {
  766. return this.dxForAxisLabel(this.owner.config.axis_rotated, this.getYAxisLabelPosition());
  767. };
  768. c3_axis_fn.dxForY2AxisLabel = function dxForY2AxisLabel() {
  769. return this.dxForAxisLabel(this.owner.config.axis_rotated, this.getY2AxisLabelPosition());
  770. };
  771. c3_axis_fn.dyForXAxisLabel = function dyForXAxisLabel() {
  772. var $$ = this.owner,
  773. config = $$.config,
  774. position = this.getXAxisLabelPosition();
  775. if (config.axis_rotated) {
  776. return position.isInner ? "1.2em" : -25 - this.getMaxTickWidth('x');
  777. } else {
  778. return position.isInner ? "-0.5em" : config.axis_x_height ? config.axis_x_height - 10 : "3em";
  779. }
  780. };
  781. c3_axis_fn.dyForYAxisLabel = function dyForYAxisLabel() {
  782. var $$ = this.owner,
  783. position = this.getYAxisLabelPosition();
  784. if ($$.config.axis_rotated) {
  785. return position.isInner ? "-0.5em" : "3em";
  786. } else {
  787. return position.isInner ? "1.2em" : -10 - ($$.config.axis_y_inner ? 0 : this.getMaxTickWidth('y') + 10);
  788. }
  789. };
  790. c3_axis_fn.dyForY2AxisLabel = function dyForY2AxisLabel() {
  791. var $$ = this.owner,
  792. position = this.getY2AxisLabelPosition();
  793. if ($$.config.axis_rotated) {
  794. return position.isInner ? "1.2em" : "-2.2em";
  795. } else {
  796. return position.isInner ? "-0.5em" : 15 + ($$.config.axis_y2_inner ? 0 : this.getMaxTickWidth('y2') + 15);
  797. }
  798. };
  799. c3_axis_fn.textAnchorForXAxisLabel = function textAnchorForXAxisLabel() {
  800. var $$ = this.owner;
  801. return this.textAnchorForAxisLabel(!$$.config.axis_rotated, this.getXAxisLabelPosition());
  802. };
  803. c3_axis_fn.textAnchorForYAxisLabel = function textAnchorForYAxisLabel() {
  804. var $$ = this.owner;
  805. return this.textAnchorForAxisLabel($$.config.axis_rotated, this.getYAxisLabelPosition());
  806. };
  807. c3_axis_fn.textAnchorForY2AxisLabel = function textAnchorForY2AxisLabel() {
  808. var $$ = this.owner;
  809. return this.textAnchorForAxisLabel($$.config.axis_rotated, this.getY2AxisLabelPosition());
  810. };
  811. c3_axis_fn.getMaxTickWidth = function getMaxTickWidth(id, withoutRecompute) {
  812. var $$ = this.owner,
  813. config = $$.config,
  814. maxWidth = 0,
  815. targetsToShow,
  816. scale,
  817. axis,
  818. dummy,
  819. svg;
  820. if (withoutRecompute && $$.currentMaxTickWidths[id]) {
  821. return $$.currentMaxTickWidths[id];
  822. }
  823. if ($$.svg) {
  824. targetsToShow = $$.filterTargetsToShow($$.data.targets);
  825. if (id === 'y') {
  826. scale = $$.y.copy().domain($$.getYDomain(targetsToShow, 'y'));
  827. axis = this.getYAxis(scale, $$.yOrient, config.axis_y_tick_format, $$.yAxisTickValues, false, true, true);
  828. } else if (id === 'y2') {
  829. scale = $$.y2.copy().domain($$.getYDomain(targetsToShow, 'y2'));
  830. axis = this.getYAxis(scale, $$.y2Orient, config.axis_y2_tick_format, $$.y2AxisTickValues, false, true, true);
  831. } else {
  832. scale = $$.x.copy().domain($$.getXDomain(targetsToShow));
  833. axis = this.getXAxis(scale, $$.xOrient, $$.xAxisTickFormat, $$.xAxisTickValues, false, true, true);
  834. this.updateXAxisTickValues(targetsToShow, axis);
  835. }
  836. dummy = $$.d3.select('body').append('div').classed('c3', true);
  837. svg = dummy.append("svg").style('visibility', 'hidden').style('position', 'fixed').style('top', 0).style('left', 0), svg.append('g').call(axis).each(function () {
  838. $$.d3.select(this).selectAll('text').each(function () {
  839. var box = this.getBoundingClientRect();
  840. if (maxWidth < box.width) {
  841. maxWidth = box.width;
  842. }
  843. });
  844. dummy.remove();
  845. });
  846. }
  847. $$.currentMaxTickWidths[id] = maxWidth <= 0 ? $$.currentMaxTickWidths[id] : maxWidth;
  848. return $$.currentMaxTickWidths[id];
  849. };
  850. c3_axis_fn.updateLabels = function updateLabels(withTransition) {
  851. var $$ = this.owner;
  852. var axisXLabel = $$.main.select('.' + CLASS.axisX + ' .' + CLASS.axisXLabel),
  853. axisYLabel = $$.main.select('.' + CLASS.axisY + ' .' + CLASS.axisYLabel),
  854. axisY2Label = $$.main.select('.' + CLASS.axisY2 + ' .' + CLASS.axisY2Label);
  855. (withTransition ? axisXLabel.transition() : axisXLabel).attr("x", this.xForXAxisLabel.bind(this)).attr("dx", this.dxForXAxisLabel.bind(this)).attr("dy", this.dyForXAxisLabel.bind(this)).text(this.textForXAxisLabel.bind(this));
  856. (withTransition ? axisYLabel.transition() : axisYLabel).attr("x", this.xForYAxisLabel.bind(this)).attr("dx", this.dxForYAxisLabel.bind(this)).attr("dy", this.dyForYAxisLabel.bind(this)).text(this.textForYAxisLabel.bind(this));
  857. (withTransition ? axisY2Label.transition() : axisY2Label).attr("x", this.xForY2AxisLabel.bind(this)).attr("dx", this.dxForY2AxisLabel.bind(this)).attr("dy", this.dyForY2AxisLabel.bind(this)).text(this.textForY2AxisLabel.bind(this));
  858. };
  859. c3_axis_fn.getPadding = function getPadding(padding, key, defaultValue, domainLength) {
  860. var p = typeof padding === 'number' ? padding : padding[key];
  861. if (!isValue(p)) {
  862. return defaultValue;
  863. }
  864. if (padding.unit === 'ratio') {
  865. return padding[key] * domainLength;
  866. }
  867. // assume padding is pixels if unit is not specified
  868. return this.convertPixelsToAxisPadding(p, domainLength);
  869. };
  870. c3_axis_fn.convertPixelsToAxisPadding = function convertPixelsToAxisPadding(pixels, domainLength) {
  871. var $$ = this.owner,
  872. length = $$.config.axis_rotated ? $$.width : $$.height;
  873. return domainLength * (pixels / length);
  874. };
  875. c3_axis_fn.generateTickValues = function generateTickValues(values, tickCount, forTimeSeries) {
  876. var tickValues = values,
  877. targetCount,
  878. start,
  879. end,
  880. count,
  881. interval,
  882. i,
  883. tickValue;
  884. if (tickCount) {
  885. targetCount = isFunction(tickCount) ? tickCount() : tickCount;
  886. // compute ticks according to tickCount
  887. if (targetCount === 1) {
  888. tickValues = [values[0]];
  889. } else if (targetCount === 2) {
  890. tickValues = [values[0], values[values.length - 1]];
  891. } else if (targetCount > 2) {
  892. count = targetCount - 2;
  893. start = values[0];
  894. end = values[values.length - 1];
  895. interval = (end - start) / (count + 1);
  896. // re-construct unique values
  897. tickValues = [start];
  898. for (i = 0; i < count; i++) {
  899. tickValue = +start + interval * (i + 1);
  900. tickValues.push(forTimeSeries ? new Date(tickValue) : tickValue);
  901. }
  902. tickValues.push(end);
  903. }
  904. }
  905. if (!forTimeSeries) {
  906. tickValues = tickValues.sort(function (a, b) {
  907. return a - b;
  908. });
  909. }
  910. return tickValues;
  911. };
  912. c3_axis_fn.generateTransitions = function generateTransitions(duration) {
  913. var $$ = this.owner,
  914. axes = $$.axes;
  915. return {
  916. axisX: duration ? axes.x.transition().duration(duration) : axes.x,
  917. axisY: duration ? axes.y.transition().duration(duration) : axes.y,
  918. axisY2: duration ? axes.y2.transition().duration(duration) : axes.y2,
  919. axisSubX: duration ? axes.subx.transition().duration(duration) : axes.subx
  920. };
  921. };
  922. c3_axis_fn.redraw = function redraw(transitions, isHidden) {
  923. var $$ = this.owner;
  924. $$.axes.x.style("opacity", isHidden ? 0 : 1);
  925. $$.axes.y.style("opacity", isHidden ? 0 : 1);
  926. $$.axes.y2.style("opacity", isHidden ? 0 : 1);
  927. $$.axes.subx.style("opacity", isHidden ? 0 : 1);
  928. transitions.axisX.call($$.xAxis);
  929. transitions.axisY.call($$.yAxis);
  930. transitions.axisY2.call($$.y2Axis);
  931. transitions.axisSubX.call($$.subXAxis);
  932. };
  933. var c3 = { version: "0.4.21" };
  934. var c3_chart_fn;
  935. var c3_chart_internal_fn;
  936. function Component(owner, componentKey, fn) {
  937. this.owner = owner;
  938. c3.chart.internal[componentKey] = fn;
  939. }
  940. function Chart(config) {
  941. var $$ = this.internal = new ChartInternal(this);
  942. $$.loadConfig(config);
  943. $$.beforeInit(config);
  944. $$.init();
  945. $$.afterInit(config);
  946. // bind "this" to nested API
  947. (function bindThis(fn, target, argThis) {
  948. Object.keys(fn).forEach(function (key) {
  949. target[key] = fn[key].bind(argThis);
  950. if (Object.keys(fn[key]).length > 0) {
  951. bindThis(fn[key], target[key], argThis);
  952. }
  953. });
  954. })(c3_chart_fn, this, this);
  955. }
  956. function ChartInternal(api) {
  957. var $$ = this;
  958. $$.d3 = window.d3 ? window.d3 : typeof require !== 'undefined' ? require("d3") : undefined;
  959. $$.api = api;
  960. $$.config = $$.getDefaultConfig();
  961. $$.data = {};
  962. $$.cache = {};
  963. $$.axes = {};
  964. }
  965. c3.generate = function (config) {
  966. return new Chart(config);
  967. };
  968. c3.chart = {
  969. fn: Chart.prototype,
  970. internal: {
  971. fn: ChartInternal.prototype
  972. }
  973. };
  974. c3_chart_fn = c3.chart.fn;
  975. c3_chart_internal_fn = c3.chart.internal.fn;
  976. c3_chart_internal_fn.beforeInit = function () {
  977. // can do something
  978. };
  979. c3_chart_internal_fn.afterInit = function () {
  980. // can do something
  981. };
  982. c3_chart_internal_fn.init = function () {
  983. var $$ = this,
  984. config = $$.config;
  985. $$.initParams();
  986. if (config.data_url) {
  987. $$.convertUrlToData(config.data_url, config.data_mimeType, config.data_headers, config.data_keys, $$.initWithData);
  988. } else if (config.data_json) {
  989. $$.initWithData($$.convertJsonToData(config.data_json, config.data_keys));
  990. } else if (config.data_rows) {
  991. $$.initWithData($$.convertRowsToData(config.data_rows));
  992. } else if (config.data_columns) {
  993. $$.initWithData($$.convertColumnsToData(config.data_columns));
  994. } else {
  995. throw Error('url or json or rows or columns is required.');
  996. }
  997. };
  998. c3_chart_internal_fn.initParams = function () {
  999. var $$ = this,
  1000. d3 = $$.d3,
  1001. config = $$.config;
  1002. // MEMO: clipId needs to be unique because it conflicts when multiple charts exist
  1003. $$.clipId = "c3-" + +new Date() + '-clip', $$.clipIdForXAxis = $$.clipId + '-xaxis', $$.clipIdForYAxis = $$.clipId + '-yaxis', $$.clipIdForGrid = $$.clipId + '-grid', $$.clipIdForSubchart = $$.clipId + '-subchart', $$.clipPath = $$.getClipPath($$.clipId), $$.clipPathForXAxis = $$.getClipPath($$.clipIdForXAxis), $$.clipPathForYAxis = $$.getClipPath($$.clipIdForYAxis);
  1004. $$.clipPathForGrid = $$.getClipPath($$.clipIdForGrid), $$.clipPathForSubchart = $$.getClipPath($$.clipIdForSubchart), $$.dragStart = null;
  1005. $$.dragging = false;
  1006. $$.flowing = false;
  1007. $$.cancelClick = false;
  1008. $$.mouseover = false;
  1009. $$.transiting = false;
  1010. $$.color = $$.generateColor();
  1011. $$.levelColor = $$.generateLevelColor();
  1012. $$.dataTimeFormat = config.data_xLocaltime ? d3.time.format : d3.time.format.utc;
  1013. $$.axisTimeFormat = config.axis_x_localtime ? d3.time.format : d3.time.format.utc;
  1014. $$.defaultAxisTimeFormat = $$.axisTimeFormat.multi([[".%L", function (d) {
  1015. return d.getMilliseconds();
  1016. }], [":%S", function (d) {
  1017. return d.getSeconds();
  1018. }], ["%I:%M", function (d) {
  1019. return d.getMinutes();
  1020. }], ["%I %p", function (d) {
  1021. return d.getHours();
  1022. }], ["%-m/%-d", function (d) {
  1023. return d.getDay() && d.getDate() !== 1;
  1024. }], ["%-m/%-d", function (d) {
  1025. return d.getDate() !== 1;
  1026. }], ["%-m/%-d", function (d) {
  1027. return d.getMonth();
  1028. }], ["%Y/%-m/%-d", function () {
  1029. return true;
  1030. }]]);
  1031. $$.hiddenTargetIds = [];
  1032. $$.hiddenLegendIds = [];
  1033. $$.focusedTargetIds = [];
  1034. $$.defocusedTargetIds = [];
  1035. $$.xOrient = config.axis_rotated ? "left" : "bottom";
  1036. $$.yOrient = config.axis_rotated ? config.axis_y_inner ? "top" : "bottom" : config.axis_y_inner ? "right" : "left";
  1037. $$.y2Orient = config.axis_rotated ? config.axis_y2_inner ? "bottom" : "top" : config.axis_y2_inner ? "left" : "right";
  1038. $$.subXOrient = config.axis_rotated ? "left" : "bottom";
  1039. $$.isLegendRight = config.legend_position === 'right';
  1040. $$.isLegendInset = config.legend_position === 'inset';
  1041. $$.isLegendTop = config.legend_inset_anchor === 'top-left' || config.legend_inset_anchor === 'top-right';
  1042. $$.isLegendLeft = config.legend_inset_anchor === 'top-left' || config.legend_inset_anchor === 'bottom-left';
  1043. $$.legendStep = 0;
  1044. $$.legendItemWidth = 0;
  1045. $$.legendItemHeight = 0;
  1046. $$.currentMaxTickWidths = {
  1047. x: 0,
  1048. y: 0,
  1049. y2: 0
  1050. };
  1051. $$.rotated_padding_left = 30;
  1052. $$.rotated_padding_right = config.axis_rotated && !config.axis_x_show ? 0 : 30;
  1053. $$.rotated_padding_top = 5;
  1054. $$.withoutFadeIn = {};
  1055. $$.intervalForObserveInserted = undefined;
  1056. $$.axes.subx = d3.selectAll([]); // needs when excluding subchart.js
  1057. };
  1058. c3_chart_internal_fn.initChartElements = function () {
  1059. if (this.initBar) {
  1060. this.initBar();
  1061. }
  1062. if (this.initLine) {
  1063. this.initLine();
  1064. }
  1065. if (this.initArc) {
  1066. this.initArc();
  1067. }
  1068. if (this.initGauge) {
  1069. this.initGauge();
  1070. }
  1071. if (this.initText) {
  1072. this.initText();
  1073. }
  1074. };
  1075. c3_chart_internal_fn.initWithData = function (data) {
  1076. var $$ = this,
  1077. d3 = $$.d3,
  1078. config = $$.config;
  1079. var defs,
  1080. main,
  1081. binding = true;
  1082. $$.axis = new Axis($$);
  1083. if ($$.initPie) {
  1084. $$.initPie();
  1085. }
  1086. if ($$.initBrush) {
  1087. $$.initBrush();
  1088. }
  1089. if ($$.initZoom) {
  1090. $$.initZoom();
  1091. }
  1092. if (!config.bindto) {
  1093. $$.selectChart = d3.selectAll([]);
  1094. } else if (typeof config.bindto.node === 'function') {
  1095. $$.selectChart = config.bindto;
  1096. } else {
  1097. $$.selectChart = d3.select(config.bindto);
  1098. }
  1099. if ($$.selectChart.empty()) {
  1100. $$.selectChart = d3.select(document.createElement('div')).style('opacity', 0);
  1101. $$.observeInserted($$.selectChart);
  1102. binding = false;
  1103. }
  1104. $$.selectChart.html("").classed("c3", true);
  1105. // Init data as targets
  1106. $$.data.xs = {};
  1107. $$.data.targets = $$.convertDataToTargets(data);
  1108. if (config.data_filter) {
  1109. $$.data.targets = $$.data.targets.filter(config.data_filter);
  1110. }
  1111. // Set targets to hide if needed
  1112. if (config.data_hide) {
  1113. $$.addHiddenTargetIds(config.data_hide === true ? $$.mapToIds($$.data.targets) : config.data_hide);
  1114. }
  1115. if (config.legend_hide) {
  1116. $$.addHiddenLegendIds(config.legend_hide === true ? $$.mapToIds($$.data.targets) : config.legend_hide);
  1117. }
  1118. // Init sizes and scales
  1119. $$.updateSizes();
  1120. $$.updateScales();
  1121. // Set domains for each scale
  1122. $$.x.domain(d3.extent($$.getXDomain($$.data.targets)));
  1123. $$.y.domain($$.getYDomain($$.data.targets, 'y'));
  1124. $$.y2.domain($$.getYDomain($$.data.targets, 'y2'));
  1125. $$.subX.domain($$.x.domain());
  1126. $$.subY.domain($$.y.domain());
  1127. $$.subY2.domain($$.y2.domain());
  1128. // Save original x domain for zoom update
  1129. $$.orgXDomain = $$.x.domain();
  1130. // Set initialized scales to brush and zoom
  1131. if ($$.brush) {
  1132. $$.brush.scale($$.subX);
  1133. }
  1134. if (config.zoom_enabled) {
  1135. $$.zoom.scale($$.x);
  1136. }
  1137. /*-- Basic Elements --*/
  1138. // Define svgs
  1139. $$.svg = $$.selectChart.append("svg").style("overflow", "hidden").on('mouseenter', function () {
  1140. return config.onmouseover.call($$);
  1141. }).on('mouseleave', function () {
  1142. return config.onmouseout.call($$);
  1143. });
  1144. if ($$.config.svg_classname) {
  1145. $$.svg.attr('class', $$.config.svg_classname);
  1146. }
  1147. // Define defs
  1148. defs = $$.svg.append("defs");
  1149. $$.clipChart = $$.appendClip(defs, $$.clipId);
  1150. $$.clipXAxis = $$.appendClip(defs, $$.clipIdForXAxis);
  1151. $$.clipYAxis = $$.appendClip(defs, $$.clipIdForYAxis);
  1152. $$.clipGrid = $$.appendClip(defs, $$.clipIdForGrid);
  1153. $$.clipSubchart = $$.appendClip(defs, $$.clipIdForSubchart);
  1154. $$.updateSvgSize();
  1155. // Define regions
  1156. main = $$.main = $$.svg.append("g").attr("transform", $$.getTranslate('main'));
  1157. if ($$.initSubchart) {
  1158. $$.initSubchart();
  1159. }
  1160. if ($$.initTooltip) {
  1161. $$.initTooltip();
  1162. }
  1163. if ($$.initLegend) {
  1164. $$.initLegend();
  1165. }
  1166. if ($$.initTitle) {
  1167. $$.initTitle();
  1168. }
  1169. /*-- Main Region --*/
  1170. // text when empty
  1171. main.append("text").attr("class", CLASS.text + ' ' + CLASS.empty).attr("text-anchor", "middle") // horizontal centering of text at x position in all browsers.
  1172. .attr("dominant-baseline", "middle"); // vertical centering of text at y position in all browsers, except IE.
  1173. // Regions
  1174. $$.initRegion();
  1175. // Grids
  1176. $$.initGrid();
  1177. // Define g for chart area
  1178. main.append('g').attr("clip-path", $$.clipPath).attr('class', CLASS.chart);
  1179. // Grid lines
  1180. if (config.grid_lines_front) {
  1181. $$.initGridLines();
  1182. }
  1183. // Cover whole with rects for events
  1184. $$.initEventRect();
  1185. // Define g for chart
  1186. $$.initChartElements();
  1187. // if zoom privileged, insert rect to forefront
  1188. // TODO: is this needed?
  1189. main.insert('rect', config.zoom_privileged ? null : 'g.' + CLASS.regions).attr('class', CLASS.zoomRect).attr('width', $$.width).attr('height', $$.height).style('opacity', 0).on("dblclick.zoom", null);
  1190. // Set default extent if defined
  1191. if (config.axis_x_extent) {
  1192. $$.brush.extent($$.getDefaultExtent());
  1193. }
  1194. // Add Axis
  1195. $$.axis.init();
  1196. // Set targets
  1197. $$.updateTargets($$.data.targets);
  1198. // Draw with targets
  1199. if (binding) {
  1200. $$.updateDimension();
  1201. $$.config.oninit.call($$);
  1202. $$.redraw({
  1203. withTransition: false,
  1204. withTransform: true,
  1205. withUpdateXDomain: true,
  1206. withUpdateOrgXDomain: true,
  1207. withTransitionForAxis: false
  1208. });
  1209. }
  1210. // Bind resize event
  1211. $$.bindResize();
  1212. // export element of the chart
  1213. $$.api.element = $$.selectChart.node();
  1214. };
  1215. c3_chart_internal_fn.smoothLines = function (el, type) {
  1216. var $$ = this;
  1217. if (type === 'grid') {
  1218. el.each(function () {
  1219. var g = $$.d3.select(this),
  1220. x1 = g.attr('x1'),
  1221. x2 = g.attr('x2'),
  1222. y1 = g.attr('y1'),
  1223. y2 = g.attr('y2');
  1224. g.attr({
  1225. 'x1': Math.ceil(x1),
  1226. 'x2': Math.ceil(x2),
  1227. 'y1': Math.ceil(y1),
  1228. 'y2': Math.ceil(y2)
  1229. });
  1230. });
  1231. }
  1232. };
  1233. c3_chart_internal_fn.updateSizes = function () {
  1234. var $$ = this,
  1235. config = $$.config;
  1236. var legendHeight = $$.legend ? $$.getLegendHeight() : 0,
  1237. legendWidth = $$.legend ? $$.getLegendWidth() : 0,
  1238. legendHeightForBottom = $$.isLegendRight || $$.isLegendInset ? 0 : legendHeight,
  1239. hasArc = $$.hasArcType(),
  1240. xAxisHeight = config.axis_rotated || hasArc ? 0 : $$.getHorizontalAxisHeight('x'),
  1241. subchartHeight = config.subchart_show && !hasArc ? config.subchart_size_height + xAxisHeight : 0;
  1242. $$.currentWidth = $$.getCurrentWidth();
  1243. $$.currentHeight = $$.getCurrentHeight();
  1244. // for main
  1245. $$.margin = config.axis_rotated ? {
  1246. top: $$.getHorizontalAxisHeight('y2') + $$.getCurrentPaddingTop(),
  1247. right: hasArc ? 0 : $$.getCurrentPaddingRight(),
  1248. bottom: $$.getHorizontalAxisHeight('y') + legendHeightForBottom + $$.getCurrentPaddingBottom(),
  1249. left: subchartHeight + (hasArc ? 0 : $$.getCurrentPaddingLeft())
  1250. } : {
  1251. top: 4 + $$.getCurrentPaddingTop(), // for top tick text
  1252. right: hasArc ? 0 : $$.getCurrentPaddingRight(),
  1253. bottom: xAxisHeight + subchartHeight + legendHeightForBottom + $$.getCurrentPaddingBottom(),
  1254. left: hasArc ? 0 : $$.getCurrentPaddingLeft()
  1255. };
  1256. // for subchart
  1257. $$.margin2 = config.axis_rotated ? {
  1258. top: $$.margin.top,
  1259. right: NaN,
  1260. bottom: 20 + legendHeightForBottom,
  1261. left: $$.rotated_padding_left
  1262. } : {
  1263. top: $$.currentHeight - subchartHeight - legendHeightForBottom,
  1264. right: NaN,
  1265. bottom: xAxisHeight + legendHeightForBottom,
  1266. left: $$.margin.left
  1267. };
  1268. // for legend
  1269. $$.margin3 = {
  1270. top: 0,
  1271. right: NaN,
  1272. bottom: 0,
  1273. left: 0
  1274. };
  1275. if ($$.updateSizeForLegend) {
  1276. $$.updateSizeForLegend(legendHeight, legendWidth);
  1277. }
  1278. $$.width = $$.currentWidth - $$.margin.left - $$.margin.right;
  1279. $$.height = $$.currentHeight - $$.margin.top - $$.margin.bottom;
  1280. if ($$.width < 0) {
  1281. $$.width = 0;
  1282. }
  1283. if ($$.height < 0) {
  1284. $$.height = 0;
  1285. }
  1286. $$.width2 = config.axis_rotated ? $$.margin.left - $$.rotated_padding_left - $$.rotated_padding_right : $$.width;
  1287. $$.height2 = config.axis_rotated ? $$.height : $$.currentHeight - $$.margin2.top - $$.margin2.bottom;
  1288. if ($$.width2 < 0) {
  1289. $$.width2 = 0;
  1290. }
  1291. if ($$.height2 < 0) {
  1292. $$.height2 = 0;
  1293. }
  1294. // for arc
  1295. $$.arcWidth = $$.width - ($$.isLegendRight ? legendWidth + 10 : 0);
  1296. $$.arcHeight = $$.height - ($$.isLegendRight ? 0 : 10);
  1297. if ($$.hasType('gauge') && !config.gauge_fullCircle) {
  1298. $$.arcHeight += $$.height - $$.getGaugeLabelHeight();
  1299. }
  1300. if ($$.updateRadius) {
  1301. $$.updateRadius();
  1302. }
  1303. if ($$.isLegendRight && hasArc) {
  1304. $$.margin3.left = $$.arcWidth / 2 + $$.radiusExpanded * 1.1;
  1305. }
  1306. };
  1307. c3_chart_internal_fn.updateTargets = function (targets) {
  1308. var $$ = this;
  1309. /*-- Main --*/
  1310. //-- Text --//
  1311. $$.updateTargetsForText(targets);
  1312. //-- Bar --//
  1313. $$.updateTargetsForBar(targets);
  1314. //-- Line --//
  1315. $$.updateTargetsForLine(targets);
  1316. //-- Arc --//
  1317. if ($$.hasArcType() && $$.updateTargetsForArc) {
  1318. $$.updateTargetsForArc(targets);
  1319. }
  1320. /*-- Sub --*/
  1321. if ($$.updateTargetsForSubchart) {
  1322. $$.updateTargetsForSubchart(targets);
  1323. }
  1324. // Fade-in each chart
  1325. $$.showTargets();
  1326. };
  1327. c3_chart_internal_fn.showTargets = function () {
  1328. var $$ = this;
  1329. $$.svg.selectAll('.' + CLASS.target).filter(function (d) {
  1330. return $$.isTargetToShow(d.id);
  1331. }).transition().duration($$.config.transition_duration).style("opacity", 1);
  1332. };
  1333. c3_chart_internal_fn.redraw = function (options, transitions) {
  1334. var $$ = this,
  1335. main = $$.main,
  1336. d3 = $$.d3,
  1337. config = $$.config;
  1338. var areaIndices = $$.getShapeIndices($$.isAreaType),
  1339. barIndices = $$.getShapeIndices($$.isBarType),
  1340. lineIndices = $$.getShapeIndices($$.isLineType);
  1341. var withY, withSubchart, withTransition, withTransitionForExit, withTransitionForAxis, withTransform, withUpdateXDomain, withUpdateOrgXDomain, withTrimXDomain, withLegend, withEventRect, withDimension, withUpdateXAxis;
  1342. var hideAxis = $$.hasArcType();
  1343. var drawArea, drawBar, drawLine, xForText, yForText;
  1344. var duration, durationForExit, durationForAxis;
  1345. var waitForDraw, flow;
  1346. var targetsToShow = $$.filterTargetsToShow($$.data.targets),
  1347. tickValues,
  1348. i,
  1349. intervalForCulling,
  1350. xDomainForZoom;
  1351. var xv = $$.xv.bind($$),
  1352. cx,
  1353. cy;
  1354. options = options || {};
  1355. withY = getOption(options, "withY", true);
  1356. withSubchart = getOption(options, "withSubchart", true);
  1357. withTransition = getOption(options, "withTransition", true);
  1358. withTransform = getOption(options, "withTransform", false);
  1359. withUpdateXDomain = getOption(options, "withUpdateXDomain", false);
  1360. withUpdateOrgXDomain = getOption(options, "withUpdateOrgXDomain", false);
  1361. withTrimXDomain = getOption(options, "withTrimXDomain", true);
  1362. withUpdateXAxis = getOption(options, "withUpdateXAxis", withUpdateXDomain);
  1363. withLegend = getOption(options, "withLegend", false);
  1364. withEventRect = getOption(options, "withEventRect", true);
  1365. withDimension = getOption(options, "withDimension", true);
  1366. withTransitionForExit = getOption(options, "withTransitionForExit", withTransition);
  1367. withTransitionForAxis = getOption(options, "withTransitionForAxis", withTransition);
  1368. duration = withTransition ? config.transition_duration : 0;
  1369. durationForExit = withTransitionForExit ? duration : 0;
  1370. durationForAxis = withTransitionForAxis ? duration : 0;
  1371. transitions = transitions || $$.axis.generateTransitions(durationForAxis);
  1372. // update legend and transform each g
  1373. if (withLegend && config.legend_show) {
  1374. $$.updateLegend($$.mapToIds($$.data.targets), options, transitions);
  1375. } else if (withDimension) {
  1376. // need to update dimension (e.g. axis.y.tick.values) because y tick values should change
  1377. // no need to update axis in it because they will be updated in redraw()
  1378. $$.updateDimension(true);
  1379. }
  1380. // MEMO: needed for grids calculation
  1381. if ($$.isCategorized() && targetsToShow.length === 0) {
  1382. $$.x.domain([0, $$.axes.x.selectAll('.tick').size()]);
  1383. }
  1384. if (targetsToShow.length) {
  1385. $$.updateXDomain(targetsToShow, withUpdateXDomain, withUpdateOrgXDomain, withTrimXDomain);
  1386. if (!config.axis_x_tick_values) {
  1387. tickValues = $$.axis.updateXAxisTickValues(targetsToShow);
  1388. }
  1389. } else {
  1390. $$.xAxis.tickValues([]);
  1391. $$.subXAxis.tickValues([]);
  1392. }
  1393. if (config.zoom_rescale && !options.flow) {
  1394. xDomainForZoom = $$.x.orgDomain();
  1395. }
  1396. $$.y.domain($$.getYDomain(targetsToShow, 'y', xDomainForZoom));
  1397. $$.y2.domain($$.getYDomain(targetsToShow, 'y2', xDomainForZoom));
  1398. if (!config.axis_y_tick_values && config.axis_y_tick_count) {
  1399. $$.yAxis.tickValues($$.axis.generateTickValues($$.y.domain(), config.axis_y_tick_count));
  1400. }
  1401. if (!config.axis_y2_tick_values && config.axis_y2_tick_count) {
  1402. $$.y2Axis.tickValues($$.axis.generateTickValues($$.y2.domain(), config.axis_y2_tick_count));
  1403. }
  1404. // axes
  1405. $$.axis.redraw(transitions, hideAxis);
  1406. // Update axis label
  1407. $$.axis.updateLabels(withTransition);
  1408. // show/hide if manual culling needed
  1409. if ((withUpdateXDomain || withUpdateXAxis) && targetsToShow.length) {
  1410. if (config.axis_x_tick_culling && tickValues) {
  1411. for (i = 1; i < tickValues.length; i++) {
  1412. if (tickValues.length / i < config.axis_x_tick_culling_max) {
  1413. intervalForCulling = i;
  1414. break;
  1415. }
  1416. }
  1417. $$.svg.selectAll('.' + CLASS.axisX + ' .tick text').each(function (e) {
  1418. var index = tickValues.indexOf(e);
  1419. if (index >= 0) {
  1420. d3.select(this).style('display', index % intervalForCulling ? 'none' : 'block');
  1421. }
  1422. });
  1423. } else {
  1424. $$.svg.selectAll('.' + CLASS.axisX + ' .tick text').style('display', 'block');
  1425. }
  1426. }
  1427. // setup drawer - MEMO: these must be called after axis updated
  1428. drawArea = $$.generateDrawArea ? $$.generateDrawArea(areaIndices, false) : undefined;
  1429. drawBar = $$.generateDrawBar ? $$.generateDrawBar(barIndices) : undefined;
  1430. drawLine = $$.generateDrawLine ? $$.generateDrawLine(lineIndices, false) : undefined;
  1431. xForText = $$.generateXYForText(areaIndices, barIndices, lineIndices, true);
  1432. yForText = $$.generateXYForText(areaIndices, barIndices, lineIndices, false);
  1433. // Update sub domain
  1434. if (withY) {
  1435. $$.subY.domain($$.getYDomain(targetsToShow, 'y'));
  1436. $$.subY2.domain($$.getYDomain(targetsToShow, 'y2'));
  1437. }
  1438. // xgrid focus
  1439. $$.updateXgridFocus();
  1440. // Data empty label positioning and text.
  1441. main.select("text." + CLASS.text + '.' + CLASS.empty).attr("x", $$.width / 2).attr("y", $$.height / 2).text(config.data_empty_label_text).transition().style('opacity', targetsToShow.length ? 0 : 1);
  1442. // grid
  1443. $$.updateGrid(duration);
  1444. // rect for regions
  1445. $$.updateRegion(duration);
  1446. // bars
  1447. $$.updateBar(durationForExit);
  1448. // lines, areas and cricles
  1449. $$.updateLine(durationForExit);
  1450. $$.updateArea(durationForExit);
  1451. $$.updateCircle();
  1452. // text
  1453. if ($$.hasDataLabel()) {
  1454. $$.updateText(durationForExit);
  1455. }
  1456. // title
  1457. if ($$.redrawTitle) {
  1458. $$.redrawTitle();
  1459. }
  1460. // arc
  1461. if ($$.redrawArc) {
  1462. $$.redrawArc(duration, durationForExit, withTransform);
  1463. }
  1464. // subchart
  1465. if ($$.redrawSubchart) {
  1466. $$.redrawSubchart(withSubchart, transitions, duration, durationForExit, areaIndices, barIndices, lineIndices);
  1467. }
  1468. // circles for select
  1469. main.selectAll('.' + CLASS.selectedCircles).filter($$.isBarType.bind($$)).selectAll('circle').remove();
  1470. // event rects will redrawn when flow called
  1471. if (config.interaction_enabled && !options.flow && withEventRect) {
  1472. $$.redrawEventRect();
  1473. if ($$.updateZoom) {
  1474. $$.updateZoom();
  1475. }
  1476. }
  1477. // update circleY based on updated parameters
  1478. $$.updateCircleY();
  1479. // generate circle x/y functions depending on updated params
  1480. cx = ($$.config.axis_rotated ? $$.circleY : $$.circleX).bind($$);
  1481. cy = ($$.config.axis_rotated ? $$.circleX : $$.circleY).bind($$);
  1482. if (options.flow) {
  1483. flow = $$.generateFlow({
  1484. targets: targetsToShow,
  1485. flow: options.flow,
  1486. duration: options.flow.duration,
  1487. drawBar: drawBar,
  1488. drawLine: drawLine,
  1489. drawArea: drawArea,
  1490. cx: cx,
  1491. cy: cy,
  1492. xv: xv,
  1493. xForText: xForText,
  1494. yForText: yForText
  1495. });
  1496. }
  1497. if ((duration || flow) && $$.isTabVisible()) {
  1498. // Only use transition if tab visible. See #938.
  1499. // transition should be derived from one transition
  1500. d3.transition().duration(duration).each(function () {
  1501. var transitionsToWait = [];
  1502. // redraw and gather transitions
  1503. [$$.redrawBar(drawBar, true), $$.redrawLine(drawLine, true), $$.redrawArea(drawArea, true), $$.redrawCircle(cx, cy, true), $$.redrawText(xForText, yForText, options.flow, true), $$.redrawRegion(true), $$.redrawGrid(true)].forEach(function (transitions) {
  1504. transitions.forEach(function (transition) {
  1505. transitionsToWait.push(transition);
  1506. });
  1507. });
  1508. // Wait for end of transitions to call flow and onrendered callback
  1509. waitForDraw = $$.generateWait();
  1510. transitionsToWait.forEach(function (t) {
  1511. waitForDraw.add(t);
  1512. });
  1513. }).call(waitForDraw, function () {
  1514. if (flow) {
  1515. flow();
  1516. }
  1517. if (config.onrendered) {
  1518. config.onrendered.call($$);
  1519. }
  1520. });
  1521. } else {
  1522. $$.redrawBar(drawBar);
  1523. $$.redrawLine(drawLine);
  1524. $$.redrawArea(drawArea);
  1525. $$.redrawCircle(cx, cy);
  1526. $$.redrawText(xForText, yForText, options.flow);
  1527. $$.redrawRegion();
  1528. $$.redrawGrid();
  1529. if (config.onrendered) {
  1530. config.onrendered.call($$);
  1531. }
  1532. }
  1533. // update fadein condition
  1534. $$.mapToIds($$.data.targets).forEach(function (id) {
  1535. $$.withoutFadeIn[id] = true;
  1536. });
  1537. };
  1538. c3_chart_internal_fn.updateAndRedraw = function (options) {
  1539. var $$ = this,
  1540. config = $$.config,
  1541. transitions;
  1542. options = options || {};
  1543. // same with redraw
  1544. options.withTransition = getOption(options, "withTransition", true);
  1545. options.withTransform = getOption(options, "withTransform", false);
  1546. options.withLegend = getOption(options, "withLegend", false);
  1547. // NOT same with redraw
  1548. options.withUpdateXDomain = true;
  1549. options.withUpdateOrgXDomain = true;
  1550. options.withTransitionForExit = false;
  1551. options.withTransitionForTransform = getOption(options, "withTransitionForTransform", options.withTransition);
  1552. // MEMO: this needs to be called before updateLegend and it means this ALWAYS needs to be called)
  1553. $$.updateSizes();
  1554. // MEMO: called in updateLegend in redraw if withLegend
  1555. if (!(options.withLegend && config.legend_show)) {
  1556. transitions = $$.axis.generateTransitions(options.withTransitionForAxis ? config.transition_duration : 0);
  1557. // Update scales
  1558. $$.updateScales();
  1559. $$.updateSvgSize();
  1560. // Update g positions
  1561. $$.transformAll(options.withTransitionForTransform, transitions);
  1562. }
  1563. // Draw with new sizes & scales
  1564. $$.redraw(options, transitions);
  1565. };
  1566. c3_chart_internal_fn.redrawWithoutRescale = function () {
  1567. this.redraw({
  1568. withY: false,
  1569. withSubchart: false,
  1570. withEventRect: false,
  1571. withTransitionForAxis: false
  1572. });
  1573. };
  1574. c3_chart_internal_fn.isTimeSeries = function () {
  1575. return this.config.axis_x_type === 'timeseries';
  1576. };
  1577. c3_chart_internal_fn.isCategorized = function () {
  1578. return this.config.axis_x_type.indexOf('categor') >= 0;
  1579. };
  1580. c3_chart_internal_fn.isCustomX = function () {
  1581. var $$ = this,
  1582. config = $$.config;
  1583. return !$$.isTimeSeries() && (config.data_x || notEmpty(config.data_xs));
  1584. };
  1585. c3_chart_internal_fn.isTimeSeriesY = function () {
  1586. return this.config.axis_y_type === 'timeseries';
  1587. };
  1588. c3_chart_internal_fn.getTranslate = function (target) {
  1589. var $$ = this,
  1590. config = $$.config,
  1591. x,
  1592. y;
  1593. if (target === 'main') {
  1594. x = asHalfPixel($$.margin.left);
  1595. y = asHalfPixel($$.margin.top);
  1596. } else if (target === 'context') {
  1597. x = asHalfPixel($$.margin2.left);
  1598. y = asHalfPixel($$.margin2.top);
  1599. } else if (target === 'legend') {
  1600. x = $$.margin3.left;
  1601. y = $$.margin3.top;
  1602. } else if (target === 'x') {
  1603. x = 0;
  1604. y = config.axis_rotated ? 0 : $$.height;
  1605. } else if (target === 'y') {
  1606. x = 0;
  1607. y = config.axis_rotated ? $$.height : 0;
  1608. } else if (target === 'y2') {
  1609. x = config.axis_rotated ? 0 : $$.width;
  1610. y = config.axis_rotated ? 1 : 0;
  1611. } else if (target === 'subx') {
  1612. x = 0;
  1613. y = config.axis_rotated ? 0 : $$.height2;
  1614. } else if (target === 'arc') {
  1615. x = $$.arcWidth / 2;
  1616. y = $$.arcHeight / 2 - ($$.hasType('gauge') ? 6 : 0); // to prevent wrong display of min and max label
  1617. }
  1618. return "translate(" + x + "," + y + ")";
  1619. };
  1620. c3_chart_internal_fn.initialOpacity = function (d) {
  1621. return d.value !== null && this.withoutFadeIn[d.id] ? 1 : 0;
  1622. };
  1623. c3_chart_internal_fn.initialOpacityForCircle = function (d) {
  1624. return d.value !== null && this.withoutFadeIn[d.id] ? this.opacityForCircle(d) : 0;
  1625. };
  1626. c3_chart_internal_fn.opacityForCircle = function (d) {
  1627. var isPointShouldBeShown = isFunction(this.config.point_show) ? this.config.point_show(d) : this.config.point_show;
  1628. var opacity = isPointShouldBeShown ? 1 : 0;
  1629. return isValue(d.value) ? this.isScatterType(d) ? 0.5 : opacity : 0;
  1630. };
  1631. c3_chart_internal_fn.opacityForText = function () {
  1632. return this.hasDataLabel() ? 1 : 0;
  1633. };
  1634. c3_chart_internal_fn.xx = function (d) {
  1635. return d ? this.x(d.x) : null;
  1636. };
  1637. c3_chart_internal_fn.xv = function (d) {
  1638. var $$ = this,
  1639. value = d.value;
  1640. if ($$.isTimeSeries()) {
  1641. value = $$.parseDate(d.value);
  1642. } else if ($$.isCategorized() && typeof d.value === 'string') {
  1643. value = $$.config.axis_x_categories.indexOf(d.value);
  1644. }
  1645. return Math.ceil($$.x(value));
  1646. };
  1647. c3_chart_internal_fn.yv = function (d) {
  1648. var $$ = this,
  1649. yScale = d.axis && d.axis === 'y2' ? $$.y2 : $$.y;
  1650. return Math.ceil(yScale(d.value));
  1651. };
  1652. c3_chart_internal_fn.subxx = function (d) {
  1653. return d ? this.subX(d.x) : null;
  1654. };
  1655. c3_chart_internal_fn.transformMain = function (withTransition, transitions) {
  1656. var $$ = this,
  1657. xAxis,
  1658. yAxis,
  1659. y2Axis;
  1660. if (transitions && transitions.axisX) {
  1661. xAxis = transitions.axisX;
  1662. } else {
  1663. xAxis = $$.main.select('.' + CLASS.axisX);
  1664. if (withTransition) {
  1665. xAxis = xAxis.transition();
  1666. }
  1667. }
  1668. if (transitions && transitions.axisY) {
  1669. yAxis = transitions.axisY;
  1670. } else {
  1671. yAxis = $$.main.select('.' + CLASS.axisY);
  1672. if (withTransition) {
  1673. yAxis = yAxis.transition();
  1674. }
  1675. }
  1676. if (transitions && transitions.axisY2) {
  1677. y2Axis = transitions.axisY2;
  1678. } else {
  1679. y2Axis = $$.main.select('.' + CLASS.axisY2);
  1680. if (withTransition) {
  1681. y2Axis = y2Axis.transition();
  1682. }
  1683. }
  1684. (withTransition ? $$.main.transition() : $$.main).attr("transform", $$.getTranslate('main'));
  1685. xAxis.attr("transform", $$.getTranslate('x'));
  1686. yAxis.attr("transform", $$.getTranslate('y'));
  1687. y2Axis.attr("transform", $$.getTranslate('y2'));
  1688. $$.main.select('.' + CLASS.chartArcs).attr("transform", $$.getTranslate('arc'));
  1689. };
  1690. c3_chart_internal_fn.transformAll = function (withTransition, transitions) {
  1691. var $$ = this;
  1692. $$.transformMain(withTransition, transitions);
  1693. if ($$.config.subchart_show) {
  1694. $$.transformContext(withTransition, transitions);
  1695. }
  1696. if ($$.legend) {
  1697. $$.transformLegend(withTransition);
  1698. }
  1699. };
  1700. c3_chart_internal_fn.updateSvgSize = function () {
  1701. var $$ = this,
  1702. brush = $$.svg.select(".c3-brush .background");
  1703. $$.svg.attr('width', $$.currentWidth).attr('height', $$.currentHeight);
  1704. $$.svg.selectAll(['#' + $$.clipId, '#' + $$.clipIdForGrid]).select('rect').attr('width', $$.width).attr('height', $$.height);
  1705. $$.svg.select('#' + $$.clipIdForXAxis).select('rect').attr('x', $$.getXAxisClipX.bind($$)).attr('y', $$.getXAxisClipY.bind($$)).attr('width', $$.getXAxisClipWidth.bind($$)).attr('height', $$.getXAxisClipHeight.bind($$));
  1706. $$.svg.select('#' + $$.clipIdForYAxis).select('rect').attr('x', $$.getYAxisClipX.bind($$)).attr('y', $$.getYAxisClipY.bind($$)).attr('width', $$.getYAxisClipWidth.bind($$)).attr('height', $$.getYAxisClipHeight.bind($$));
  1707. $$.svg.select('#' + $$.clipIdForSubchart).select('rect').attr('width', $$.width).attr('height', brush.size() ? brush.attr('height') : 0);
  1708. $$.svg.select('.' + CLASS.zoomRect).attr('width', $$.width).attr('height', $$.height);
  1709. // MEMO: parent div's height will be bigger than svg when <!DOCTYPE html>
  1710. $$.selectChart.style('max-height', $$.currentHeight + "px");
  1711. };
  1712. c3_chart_internal_fn.updateDimension = function (withoutAxis) {
  1713. var $$ = this;
  1714. if (!withoutAxis) {
  1715. if ($$.config.axis_rotated) {
  1716. $$.axes.x.call($$.xAxis);
  1717. $$.axes.subx.call($$.subXAxis);
  1718. } else {
  1719. $$.axes.y.call($$.yAxis);
  1720. $$.axes.y2.call($$.y2Axis);
  1721. }
  1722. }
  1723. $$.updateSizes();
  1724. $$.updateScales();
  1725. $$.updateSvgSize();
  1726. $$.transformAll(false);
  1727. };
  1728. c3_chart_internal_fn.observeInserted = function (selection) {
  1729. var $$ = this,
  1730. observer;
  1731. if (typeof MutationObserver === 'undefined') {
  1732. window.console.error("MutationObserver not defined.");
  1733. return;
  1734. }
  1735. observer = new MutationObserver(function (mutations) {
  1736. mutations.forEach(function (mutation) {
  1737. if (mutation.type === 'childList' && mutation.previousSibling) {
  1738. observer.disconnect();
  1739. // need to wait for completion of load because size calculation requires the actual sizes determined after that completion
  1740. $$.intervalForObserveInserted = window.setInterval(function () {
  1741. // parentNode will NOT be null when completed
  1742. if (selection.node().parentNode) {
  1743. window.clearInterval($$.intervalForObserveInserted);
  1744. $$.updateDimension();
  1745. if ($$.brush) {
  1746. $$.brush.update();
  1747. }
  1748. $$.config.oninit.call($$);
  1749. $$.redraw({
  1750. withTransform: true,
  1751. withUpdateXDomain: true,
  1752. withUpdateOrgXDomain: true,
  1753. withTransition: false,
  1754. withTransitionForTransform: false,
  1755. withLegend: true
  1756. });
  1757. selection.transition().style('opacity', 1);
  1758. }
  1759. }, 10);
  1760. }
  1761. });
  1762. });
  1763. observer.observe(selection.node(), { attributes: true, childList: true, characterData: true });
  1764. };
  1765. c3_chart_internal_fn.bindResize = function () {
  1766. var $$ = this,
  1767. config = $$.config;
  1768. $$.resizeFunction = $$.generateResize(); // need to call .remove
  1769. $$.resizeFunction.add(function () {
  1770. config.onresize.call($$);
  1771. });
  1772. if (config.resize_auto) {
  1773. $$.resizeFunction.add(function () {
  1774. if ($$.resizeTimeout !== undefined) {
  1775. window.clearTimeout($$.resizeTimeout);
  1776. }
  1777. $$.resizeTimeout = window.setTimeout(function () {
  1778. delete $$.resizeTimeout;
  1779. $$.api.flush();
  1780. }, 100);
  1781. });
  1782. }
  1783. $$.resizeFunction.add(function () {
  1784. config.onresized.call($$);
  1785. });
  1786. $$.resizeIfElementDisplayed = function () {
  1787. // if element not displayed skip it
  1788. if ($$.api == null || !$$.api.element.offsetParent) {
  1789. return;
  1790. }
  1791. $$.resizeFunction();
  1792. };
  1793. if (window.attachEvent) {
  1794. window.attachEvent('onresize', $$.resizeIfElementDisplayed);
  1795. } else if (window.addEventListener) {
  1796. window.addEventListener('resize', $$.resizeIfElementDisplayed, false);
  1797. } else {
  1798. // fallback to this, if this is a very old browser
  1799. var wrapper = window.onresize;
  1800. if (!wrapper) {
  1801. // create a wrapper that will call all charts
  1802. wrapper = $$.generateResize();
  1803. } else if (!wrapper.add || !wrapper.remove) {
  1804. // there is already a handler registered, make sure we call it too
  1805. wrapper = $$.generateResize();
  1806. wrapper.add(window.onresize);
  1807. }
  1808. // add this graph to the wrapper, we will be removed if the user calls destroy
  1809. wrapper.add($$.resizeFunction);
  1810. window.onresize = function () {
  1811. // if element not displayed skip it
  1812. if (!$$.api.element.offsetParent) {
  1813. return;
  1814. }
  1815. wrapper();
  1816. };
  1817. }
  1818. };
  1819. c3_chart_internal_fn.generateResize = function () {
  1820. var resizeFunctions = [];
  1821. function callResizeFunctions() {
  1822. resizeFunctions.forEach(function (f) {
  1823. f();
  1824. });
  1825. }
  1826. callResizeFunctions.add = function (f) {
  1827. resizeFunctions.push(f);
  1828. };
  1829. callResizeFunctions.remove = function (f) {
  1830. for (var i = 0; i < resizeFunctions.length; i++) {
  1831. if (resizeFunctions[i] === f) {
  1832. resizeFunctions.splice(i, 1);
  1833. break;
  1834. }
  1835. }
  1836. };
  1837. return callResizeFunctions;
  1838. };
  1839. c3_chart_internal_fn.endall = function (transition, callback) {
  1840. var n = 0;
  1841. transition.each(function () {
  1842. ++n;
  1843. }).each("end", function () {
  1844. if (! --n) {
  1845. callback.apply(this, arguments);
  1846. }
  1847. });
  1848. };
  1849. c3_chart_internal_fn.generateWait = function () {
  1850. var transitionsToWait = [],
  1851. f = function f(transition, callback) {
  1852. var timer = setInterval(function () {
  1853. var done = 0;
  1854. transitionsToWait.forEach(function (t) {
  1855. if (t.empty()) {
  1856. done += 1;
  1857. return;
  1858. }
  1859. try {
  1860. t.transition();
  1861. } catch (e) {
  1862. done += 1;
  1863. }
  1864. });
  1865. if (done === transitionsToWait.length) {
  1866. clearInterval(timer);
  1867. if (callback) {
  1868. callback();
  1869. }
  1870. }
  1871. }, 10);
  1872. };
  1873. f.add = function (transition) {
  1874. transitionsToWait.push(transition);
  1875. };
  1876. return f;
  1877. };
  1878. c3_chart_internal_fn.parseDate = function (date) {
  1879. var $$ = this,
  1880. parsedDate;
  1881. if (date instanceof Date) {
  1882. parsedDate = date;
  1883. } else if (typeof date === 'string') {
  1884. parsedDate = $$.dataTimeFormat($$.config.data_xFormat).parse(date);
  1885. } else if ((typeof date === 'undefined' ? 'undefined' : _typeof(date)) === 'object') {
  1886. parsedDate = new Date(+date);
  1887. } else if (typeof date === 'number' && !isNaN(date)) {
  1888. parsedDate = new Date(+date);
  1889. }
  1890. if (!parsedDate || isNaN(+parsedDate)) {
  1891. window.console.error("Failed to parse x '" + date + "' to Date object");
  1892. }
  1893. return parsedDate;
  1894. };
  1895. c3_chart_internal_fn.isTabVisible = function () {
  1896. var hidden;
  1897. if (typeof document.hidden !== "undefined") {
  1898. // Opera 12.10 and Firefox 18 and later support
  1899. hidden = "hidden";
  1900. } else if (typeof document.mozHidden !== "undefined") {
  1901. hidden = "mozHidden";
  1902. } else if (typeof document.msHidden !== "undefined") {
  1903. hidden = "msHidden";
  1904. } else if (typeof document.webkitHidden !== "undefined") {
  1905. hidden = "webkitHidden";
  1906. }
  1907. return document[hidden] ? false : true;
  1908. };
  1909. c3_chart_internal_fn.isValue = isValue;
  1910. c3_chart_internal_fn.isFunction = isFunction;
  1911. c3_chart_internal_fn.isString = isString;
  1912. c3_chart_internal_fn.isUndefined = isUndefined;
  1913. c3_chart_internal_fn.isDefined = isDefined;
  1914. c3_chart_internal_fn.ceil10 = ceil10;
  1915. c3_chart_internal_fn.asHalfPixel = asHalfPixel;
  1916. c3_chart_internal_fn.diffDomain = diffDomain;
  1917. c3_chart_internal_fn.isEmpty = isEmpty;
  1918. c3_chart_internal_fn.notEmpty = notEmpty;
  1919. c3_chart_internal_fn.notEmpty = notEmpty;
  1920. c3_chart_internal_fn.getOption = getOption;
  1921. c3_chart_internal_fn.hasValue = hasValue;
  1922. c3_chart_internal_fn.sanitise = sanitise;
  1923. c3_chart_internal_fn.getPathBox = getPathBox;
  1924. c3_chart_internal_fn.CLASS = CLASS;
  1925. /* jshint ignore:start */
  1926. // PhantomJS doesn't have support for Function.prototype.bind, which has caused confusion. Use
  1927. // this polyfill to avoid the confusion.
  1928. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind#Polyfill
  1929. if (!Function.prototype.bind) {
  1930. Function.prototype.bind = function (oThis) {
  1931. if (typeof this !== 'function') {
  1932. // closest thing possible to the ECMAScript 5
  1933. // internal IsCallable function
  1934. throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable');
  1935. }
  1936. var aArgs = Array.prototype.slice.call(arguments, 1),
  1937. fToBind = this,
  1938. fNOP = function fNOP() {},
  1939. fBound = function fBound() {
  1940. return fToBind.apply(this instanceof fNOP ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments)));
  1941. };
  1942. fNOP.prototype = this.prototype;
  1943. fBound.prototype = new fNOP();
  1944. return fBound;
  1945. };
  1946. }
  1947. // SVGPathSeg API polyfill
  1948. // https://github.com/progers/pathseg
  1949. //
  1950. // This is a drop-in replacement for the SVGPathSeg and SVGPathSegList APIs that were removed from
  1951. // SVG2 (https://lists.w3.org/Archives/Public/www-svg/2015Jun/0044.html), including the latest spec
  1952. // changes which were implemented in Firefox 43 and Chrome 46.
  1953. (function () {
  1954. if (!("SVGPathSeg" in window)) {
  1955. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGPathSeg
  1956. window.SVGPathSeg = function (type, typeAsLetter, owningPathSegList) {
  1957. this.pathSegType = type;
  1958. this.pathSegTypeAsLetter = typeAsLetter;
  1959. this._owningPathSegList = owningPathSegList;
  1960. };
  1961. window.SVGPathSeg.prototype.classname = "SVGPathSeg";
  1962. window.SVGPathSeg.PATHSEG_UNKNOWN = 0;
  1963. window.SVGPathSeg.PATHSEG_CLOSEPATH = 1;
  1964. window.SVGPathSeg.PATHSEG_MOVETO_ABS = 2;
  1965. window.SVGPathSeg.PATHSEG_MOVETO_REL = 3;
  1966. window.SVGPathSeg.PATHSEG_LINETO_ABS = 4;
  1967. window.SVGPathSeg.PATHSEG_LINETO_REL = 5;
  1968. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS = 6;
  1969. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL = 7;
  1970. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS = 8;
  1971. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL = 9;
  1972. window.SVGPathSeg.PATHSEG_ARC_ABS = 10;
  1973. window.SVGPathSeg.PATHSEG_ARC_REL = 11;
  1974. window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS = 12;
  1975. window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL = 13;
  1976. window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS = 14;
  1977. window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL = 15;
  1978. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS = 16;
  1979. window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL = 17;
  1980. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS = 18;
  1981. window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL = 19;
  1982. // Notify owning PathSegList on any changes so they can be synchronized back to the path element.
  1983. window.SVGPathSeg.prototype._segmentChanged = function () {
  1984. if (this._owningPathSegList) this._owningPathSegList.segmentChanged(this);
  1985. };
  1986. window.SVGPathSegClosePath = function (owningPathSegList) {
  1987. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CLOSEPATH, "z", owningPathSegList);
  1988. };
  1989. window.SVGPathSegClosePath.prototype = Object.create(window.SVGPathSeg.prototype);
  1990. window.SVGPathSegClosePath.prototype.toString = function () {
  1991. return "[object SVGPathSegClosePath]";
  1992. };
  1993. window.SVGPathSegClosePath.prototype._asPathString = function () {
  1994. return this.pathSegTypeAsLetter;
  1995. };
  1996. window.SVGPathSegClosePath.prototype.clone = function () {
  1997. return new window.SVGPathSegClosePath(undefined);
  1998. };
  1999. window.SVGPathSegMovetoAbs = function (owningPathSegList, x, y) {
  2000. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_MOVETO_ABS, "M", owningPathSegList);
  2001. this._x = x;
  2002. this._y = y;
  2003. };
  2004. window.SVGPathSegMovetoAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  2005. window.SVGPathSegMovetoAbs.prototype.toString = function () {
  2006. return "[object SVGPathSegMovetoAbs]";
  2007. };
  2008. window.SVGPathSegMovetoAbs.prototype._asPathString = function () {
  2009. return this.pathSegTypeAsLetter + " " + this._x + " " + this._y;
  2010. };
  2011. window.SVGPathSegMovetoAbs.prototype.clone = function () {
  2012. return new window.SVGPathSegMovetoAbs(undefined, this._x, this._y);
  2013. };
  2014. Object.defineProperty(window.SVGPathSegMovetoAbs.prototype, "x", { get: function get() {
  2015. return this._x;
  2016. }, set: function set(x) {
  2017. this._x = x;this._segmentChanged();
  2018. }, enumerable: true });
  2019. Object.defineProperty(window.SVGPathSegMovetoAbs.prototype, "y", { get: function get() {
  2020. return this._y;
  2021. }, set: function set(y) {
  2022. this._y = y;this._segmentChanged();
  2023. }, enumerable: true });
  2024. window.SVGPathSegMovetoRel = function (owningPathSegList, x, y) {
  2025. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_MOVETO_REL, "m", owningPathSegList);
  2026. this._x = x;
  2027. this._y = y;
  2028. };
  2029. window.SVGPathSegMovetoRel.prototype = Object.create(window.SVGPathSeg.prototype);
  2030. window.SVGPathSegMovetoRel.prototype.toString = function () {
  2031. return "[object SVGPathSegMovetoRel]";
  2032. };
  2033. window.SVGPathSegMovetoRel.prototype._asPathString = function () {
  2034. return this.pathSegTypeAsLetter + " " + this._x + " " + this._y;
  2035. };
  2036. window.SVGPathSegMovetoRel.prototype.clone = function () {
  2037. return new window.SVGPathSegMovetoRel(undefined, this._x, this._y);
  2038. };
  2039. Object.defineProperty(window.SVGPathSegMovetoRel.prototype, "x", { get: function get() {
  2040. return this._x;
  2041. }, set: function set(x) {
  2042. this._x = x;this._segmentChanged();
  2043. }, enumerable: true });
  2044. Object.defineProperty(window.SVGPathSegMovetoRel.prototype, "y", { get: function get() {
  2045. return this._y;
  2046. }, set: function set(y) {
  2047. this._y = y;this._segmentChanged();
  2048. }, enumerable: true });
  2049. window.SVGPathSegLinetoAbs = function (owningPathSegList, x, y) {
  2050. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_LINETO_ABS, "L", owningPathSegList);
  2051. this._x = x;
  2052. this._y = y;
  2053. };
  2054. window.SVGPathSegLinetoAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  2055. window.SVGPathSegLinetoAbs.prototype.toString = function () {
  2056. return "[object SVGPathSegLinetoAbs]";
  2057. };
  2058. window.SVGPathSegLinetoAbs.prototype._asPathString = function () {
  2059. return this.pathSegTypeAsLetter + " " + this._x + " " + this._y;
  2060. };
  2061. window.SVGPathSegLinetoAbs.prototype.clone = function () {
  2062. return new window.SVGPathSegLinetoAbs(undefined, this._x, this._y);
  2063. };
  2064. Object.defineProperty(window.SVGPathSegLinetoAbs.prototype, "x", { get: function get() {
  2065. return this._x;
  2066. }, set: function set(x) {
  2067. this._x = x;this._segmentChanged();
  2068. }, enumerable: true });
  2069. Object.defineProperty(window.SVGPathSegLinetoAbs.prototype, "y", { get: function get() {
  2070. return this._y;
  2071. }, set: function set(y) {
  2072. this._y = y;this._segmentChanged();
  2073. }, enumerable: true });
  2074. window.SVGPathSegLinetoRel = function (owningPathSegList, x, y) {
  2075. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_LINETO_REL, "l", owningPathSegList);
  2076. this._x = x;
  2077. this._y = y;
  2078. };
  2079. window.SVGPathSegLinetoRel.prototype = Object.create(window.SVGPathSeg.prototype);
  2080. window.SVGPathSegLinetoRel.prototype.toString = function () {
  2081. return "[object SVGPathSegLinetoRel]";
  2082. };
  2083. window.SVGPathSegLinetoRel.prototype._asPathString = function () {
  2084. return this.pathSegTypeAsLetter + " " + this._x + " " + this._y;
  2085. };
  2086. window.SVGPathSegLinetoRel.prototype.clone = function () {
  2087. return new window.SVGPathSegLinetoRel(undefined, this._x, this._y);
  2088. };
  2089. Object.defineProperty(window.SVGPathSegLinetoRel.prototype, "x", { get: function get() {
  2090. return this._x;
  2091. }, set: function set(x) {
  2092. this._x = x;this._segmentChanged();
  2093. }, enumerable: true });
  2094. Object.defineProperty(window.SVGPathSegLinetoRel.prototype, "y", { get: function get() {
  2095. return this._y;
  2096. }, set: function set(y) {
  2097. this._y = y;this._segmentChanged();
  2098. }, enumerable: true });
  2099. window.SVGPathSegCurvetoCubicAbs = function (owningPathSegList, x, y, x1, y1, x2, y2) {
  2100. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS, "C", owningPathSegList);
  2101. this._x = x;
  2102. this._y = y;
  2103. this._x1 = x1;
  2104. this._y1 = y1;
  2105. this._x2 = x2;
  2106. this._y2 = y2;
  2107. };
  2108. window.SVGPathSegCurvetoCubicAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  2109. window.SVGPathSegCurvetoCubicAbs.prototype.toString = function () {
  2110. return "[object SVGPathSegCurvetoCubicAbs]";
  2111. };
  2112. window.SVGPathSegCurvetoCubicAbs.prototype._asPathString = function () {
  2113. return this.pathSegTypeAsLetter + " " + this._x1 + " " + this._y1 + " " + this._x2 + " " + this._y2 + " " + this._x + " " + this._y;
  2114. };
  2115. window.SVGPathSegCurvetoCubicAbs.prototype.clone = function () {
  2116. return new window.SVGPathSegCurvetoCubicAbs(undefined, this._x, this._y, this._x1, this._y1, this._x2, this._y2);
  2117. };
  2118. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, "x", { get: function get() {
  2119. return this._x;
  2120. }, set: function set(x) {
  2121. this._x = x;this._segmentChanged();
  2122. }, enumerable: true });
  2123. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, "y", { get: function get() {
  2124. return this._y;
  2125. }, set: function set(y) {
  2126. this._y = y;this._segmentChanged();
  2127. }, enumerable: true });
  2128. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, "x1", { get: function get() {
  2129. return this._x1;
  2130. }, set: function set(x1) {
  2131. this._x1 = x1;this._segmentChanged();
  2132. }, enumerable: true });
  2133. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, "y1", { get: function get() {
  2134. return this._y1;
  2135. }, set: function set(y1) {
  2136. this._y1 = y1;this._segmentChanged();
  2137. }, enumerable: true });
  2138. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, "x2", { get: function get() {
  2139. return this._x2;
  2140. }, set: function set(x2) {
  2141. this._x2 = x2;this._segmentChanged();
  2142. }, enumerable: true });
  2143. Object.defineProperty(window.SVGPathSegCurvetoCubicAbs.prototype, "y2", { get: function get() {
  2144. return this._y2;
  2145. }, set: function set(y2) {
  2146. this._y2 = y2;this._segmentChanged();
  2147. }, enumerable: true });
  2148. window.SVGPathSegCurvetoCubicRel = function (owningPathSegList, x, y, x1, y1, x2, y2) {
  2149. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL, "c", owningPathSegList);
  2150. this._x = x;
  2151. this._y = y;
  2152. this._x1 = x1;
  2153. this._y1 = y1;
  2154. this._x2 = x2;
  2155. this._y2 = y2;
  2156. };
  2157. window.SVGPathSegCurvetoCubicRel.prototype = Object.create(window.SVGPathSeg.prototype);
  2158. window.SVGPathSegCurvetoCubicRel.prototype.toString = function () {
  2159. return "[object SVGPathSegCurvetoCubicRel]";
  2160. };
  2161. window.SVGPathSegCurvetoCubicRel.prototype._asPathString = function () {
  2162. return this.pathSegTypeAsLetter + " " + this._x1 + " " + this._y1 + " " + this._x2 + " " + this._y2 + " " + this._x + " " + this._y;
  2163. };
  2164. window.SVGPathSegCurvetoCubicRel.prototype.clone = function () {
  2165. return new window.SVGPathSegCurvetoCubicRel(undefined, this._x, this._y, this._x1, this._y1, this._x2, this._y2);
  2166. };
  2167. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, "x", { get: function get() {
  2168. return this._x;
  2169. }, set: function set(x) {
  2170. this._x = x;this._segmentChanged();
  2171. }, enumerable: true });
  2172. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, "y", { get: function get() {
  2173. return this._y;
  2174. }, set: function set(y) {
  2175. this._y = y;this._segmentChanged();
  2176. }, enumerable: true });
  2177. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, "x1", { get: function get() {
  2178. return this._x1;
  2179. }, set: function set(x1) {
  2180. this._x1 = x1;this._segmentChanged();
  2181. }, enumerable: true });
  2182. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, "y1", { get: function get() {
  2183. return this._y1;
  2184. }, set: function set(y1) {
  2185. this._y1 = y1;this._segmentChanged();
  2186. }, enumerable: true });
  2187. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, "x2", { get: function get() {
  2188. return this._x2;
  2189. }, set: function set(x2) {
  2190. this._x2 = x2;this._segmentChanged();
  2191. }, enumerable: true });
  2192. Object.defineProperty(window.SVGPathSegCurvetoCubicRel.prototype, "y2", { get: function get() {
  2193. return this._y2;
  2194. }, set: function set(y2) {
  2195. this._y2 = y2;this._segmentChanged();
  2196. }, enumerable: true });
  2197. window.SVGPathSegCurvetoQuadraticAbs = function (owningPathSegList, x, y, x1, y1) {
  2198. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS, "Q", owningPathSegList);
  2199. this._x = x;
  2200. this._y = y;
  2201. this._x1 = x1;
  2202. this._y1 = y1;
  2203. };
  2204. window.SVGPathSegCurvetoQuadraticAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  2205. window.SVGPathSegCurvetoQuadraticAbs.prototype.toString = function () {
  2206. return "[object SVGPathSegCurvetoQuadraticAbs]";
  2207. };
  2208. window.SVGPathSegCurvetoQuadraticAbs.prototype._asPathString = function () {
  2209. return this.pathSegTypeAsLetter + " " + this._x1 + " " + this._y1 + " " + this._x + " " + this._y;
  2210. };
  2211. window.SVGPathSegCurvetoQuadraticAbs.prototype.clone = function () {
  2212. return new window.SVGPathSegCurvetoQuadraticAbs(undefined, this._x, this._y, this._x1, this._y1);
  2213. };
  2214. Object.defineProperty(window.SVGPathSegCurvetoQuadraticAbs.prototype, "x", { get: function get() {
  2215. return this._x;
  2216. }, set: function set(x) {
  2217. this._x = x;this._segmentChanged();
  2218. }, enumerable: true });
  2219. Object.defineProperty(window.SVGPathSegCurvetoQuadraticAbs.prototype, "y", { get: function get() {
  2220. return this._y;
  2221. }, set: function set(y) {
  2222. this._y = y;this._segmentChanged();
  2223. }, enumerable: true });
  2224. Object.defineProperty(window.SVGPathSegCurvetoQuadraticAbs.prototype, "x1", { get: function get() {
  2225. return this._x1;
  2226. }, set: function set(x1) {
  2227. this._x1 = x1;this._segmentChanged();
  2228. }, enumerable: true });
  2229. Object.defineProperty(window.SVGPathSegCurvetoQuadraticAbs.prototype, "y1", { get: function get() {
  2230. return this._y1;
  2231. }, set: function set(y1) {
  2232. this._y1 = y1;this._segmentChanged();
  2233. }, enumerable: true });
  2234. window.SVGPathSegCurvetoQuadraticRel = function (owningPathSegList, x, y, x1, y1) {
  2235. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL, "q", owningPathSegList);
  2236. this._x = x;
  2237. this._y = y;
  2238. this._x1 = x1;
  2239. this._y1 = y1;
  2240. };
  2241. window.SVGPathSegCurvetoQuadraticRel.prototype = Object.create(window.SVGPathSeg.prototype);
  2242. window.SVGPathSegCurvetoQuadraticRel.prototype.toString = function () {
  2243. return "[object SVGPathSegCurvetoQuadraticRel]";
  2244. };
  2245. window.SVGPathSegCurvetoQuadraticRel.prototype._asPathString = function () {
  2246. return this.pathSegTypeAsLetter + " " + this._x1 + " " + this._y1 + " " + this._x + " " + this._y;
  2247. };
  2248. window.SVGPathSegCurvetoQuadraticRel.prototype.clone = function () {
  2249. return new window.SVGPathSegCurvetoQuadraticRel(undefined, this._x, this._y, this._x1, this._y1);
  2250. };
  2251. Object.defineProperty(window.SVGPathSegCurvetoQuadraticRel.prototype, "x", { get: function get() {
  2252. return this._x;
  2253. }, set: function set(x) {
  2254. this._x = x;this._segmentChanged();
  2255. }, enumerable: true });
  2256. Object.defineProperty(window.SVGPathSegCurvetoQuadraticRel.prototype, "y", { get: function get() {
  2257. return this._y;
  2258. }, set: function set(y) {
  2259. this._y = y;this._segmentChanged();
  2260. }, enumerable: true });
  2261. Object.defineProperty(window.SVGPathSegCurvetoQuadraticRel.prototype, "x1", { get: function get() {
  2262. return this._x1;
  2263. }, set: function set(x1) {
  2264. this._x1 = x1;this._segmentChanged();
  2265. }, enumerable: true });
  2266. Object.defineProperty(window.SVGPathSegCurvetoQuadraticRel.prototype, "y1", { get: function get() {
  2267. return this._y1;
  2268. }, set: function set(y1) {
  2269. this._y1 = y1;this._segmentChanged();
  2270. }, enumerable: true });
  2271. window.SVGPathSegArcAbs = function (owningPathSegList, x, y, r1, r2, angle, largeArcFlag, sweepFlag) {
  2272. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_ARC_ABS, "A", owningPathSegList);
  2273. this._x = x;
  2274. this._y = y;
  2275. this._r1 = r1;
  2276. this._r2 = r2;
  2277. this._angle = angle;
  2278. this._largeArcFlag = largeArcFlag;
  2279. this._sweepFlag = sweepFlag;
  2280. };
  2281. window.SVGPathSegArcAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  2282. window.SVGPathSegArcAbs.prototype.toString = function () {
  2283. return "[object SVGPathSegArcAbs]";
  2284. };
  2285. window.SVGPathSegArcAbs.prototype._asPathString = function () {
  2286. return this.pathSegTypeAsLetter + " " + this._r1 + " " + this._r2 + " " + this._angle + " " + (this._largeArcFlag ? "1" : "0") + " " + (this._sweepFlag ? "1" : "0") + " " + this._x + " " + this._y;
  2287. };
  2288. window.SVGPathSegArcAbs.prototype.clone = function () {
  2289. return new window.SVGPathSegArcAbs(undefined, this._x, this._y, this._r1, this._r2, this._angle, this._largeArcFlag, this._sweepFlag);
  2290. };
  2291. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "x", { get: function get() {
  2292. return this._x;
  2293. }, set: function set(x) {
  2294. this._x = x;this._segmentChanged();
  2295. }, enumerable: true });
  2296. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "y", { get: function get() {
  2297. return this._y;
  2298. }, set: function set(y) {
  2299. this._y = y;this._segmentChanged();
  2300. }, enumerable: true });
  2301. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "r1", { get: function get() {
  2302. return this._r1;
  2303. }, set: function set(r1) {
  2304. this._r1 = r1;this._segmentChanged();
  2305. }, enumerable: true });
  2306. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "r2", { get: function get() {
  2307. return this._r2;
  2308. }, set: function set(r2) {
  2309. this._r2 = r2;this._segmentChanged();
  2310. }, enumerable: true });
  2311. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "angle", { get: function get() {
  2312. return this._angle;
  2313. }, set: function set(angle) {
  2314. this._angle = angle;this._segmentChanged();
  2315. }, enumerable: true });
  2316. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "largeArcFlag", { get: function get() {
  2317. return this._largeArcFlag;
  2318. }, set: function set(largeArcFlag) {
  2319. this._largeArcFlag = largeArcFlag;this._segmentChanged();
  2320. }, enumerable: true });
  2321. Object.defineProperty(window.SVGPathSegArcAbs.prototype, "sweepFlag", { get: function get() {
  2322. return this._sweepFlag;
  2323. }, set: function set(sweepFlag) {
  2324. this._sweepFlag = sweepFlag;this._segmentChanged();
  2325. }, enumerable: true });
  2326. window.SVGPathSegArcRel = function (owningPathSegList, x, y, r1, r2, angle, largeArcFlag, sweepFlag) {
  2327. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_ARC_REL, "a", owningPathSegList);
  2328. this._x = x;
  2329. this._y = y;
  2330. this._r1 = r1;
  2331. this._r2 = r2;
  2332. this._angle = angle;
  2333. this._largeArcFlag = largeArcFlag;
  2334. this._sweepFlag = sweepFlag;
  2335. };
  2336. window.SVGPathSegArcRel.prototype = Object.create(window.SVGPathSeg.prototype);
  2337. window.SVGPathSegArcRel.prototype.toString = function () {
  2338. return "[object SVGPathSegArcRel]";
  2339. };
  2340. window.SVGPathSegArcRel.prototype._asPathString = function () {
  2341. return this.pathSegTypeAsLetter + " " + this._r1 + " " + this._r2 + " " + this._angle + " " + (this._largeArcFlag ? "1" : "0") + " " + (this._sweepFlag ? "1" : "0") + " " + this._x + " " + this._y;
  2342. };
  2343. window.SVGPathSegArcRel.prototype.clone = function () {
  2344. return new window.SVGPathSegArcRel(undefined, this._x, this._y, this._r1, this._r2, this._angle, this._largeArcFlag, this._sweepFlag);
  2345. };
  2346. Object.defineProperty(window.SVGPathSegArcRel.prototype, "x", { get: function get() {
  2347. return this._x;
  2348. }, set: function set(x) {
  2349. this._x = x;this._segmentChanged();
  2350. }, enumerable: true });
  2351. Object.defineProperty(window.SVGPathSegArcRel.prototype, "y", { get: function get() {
  2352. return this._y;
  2353. }, set: function set(y) {
  2354. this._y = y;this._segmentChanged();
  2355. }, enumerable: true });
  2356. Object.defineProperty(window.SVGPathSegArcRel.prototype, "r1", { get: function get() {
  2357. return this._r1;
  2358. }, set: function set(r1) {
  2359. this._r1 = r1;this._segmentChanged();
  2360. }, enumerable: true });
  2361. Object.defineProperty(window.SVGPathSegArcRel.prototype, "r2", { get: function get() {
  2362. return this._r2;
  2363. }, set: function set(r2) {
  2364. this._r2 = r2;this._segmentChanged();
  2365. }, enumerable: true });
  2366. Object.defineProperty(window.SVGPathSegArcRel.prototype, "angle", { get: function get() {
  2367. return this._angle;
  2368. }, set: function set(angle) {
  2369. this._angle = angle;this._segmentChanged();
  2370. }, enumerable: true });
  2371. Object.defineProperty(window.SVGPathSegArcRel.prototype, "largeArcFlag", { get: function get() {
  2372. return this._largeArcFlag;
  2373. }, set: function set(largeArcFlag) {
  2374. this._largeArcFlag = largeArcFlag;this._segmentChanged();
  2375. }, enumerable: true });
  2376. Object.defineProperty(window.SVGPathSegArcRel.prototype, "sweepFlag", { get: function get() {
  2377. return this._sweepFlag;
  2378. }, set: function set(sweepFlag) {
  2379. this._sweepFlag = sweepFlag;this._segmentChanged();
  2380. }, enumerable: true });
  2381. window.SVGPathSegLinetoHorizontalAbs = function (owningPathSegList, x) {
  2382. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS, "H", owningPathSegList);
  2383. this._x = x;
  2384. };
  2385. window.SVGPathSegLinetoHorizontalAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  2386. window.SVGPathSegLinetoHorizontalAbs.prototype.toString = function () {
  2387. return "[object SVGPathSegLinetoHorizontalAbs]";
  2388. };
  2389. window.SVGPathSegLinetoHorizontalAbs.prototype._asPathString = function () {
  2390. return this.pathSegTypeAsLetter + " " + this._x;
  2391. };
  2392. window.SVGPathSegLinetoHorizontalAbs.prototype.clone = function () {
  2393. return new window.SVGPathSegLinetoHorizontalAbs(undefined, this._x);
  2394. };
  2395. Object.defineProperty(window.SVGPathSegLinetoHorizontalAbs.prototype, "x", { get: function get() {
  2396. return this._x;
  2397. }, set: function set(x) {
  2398. this._x = x;this._segmentChanged();
  2399. }, enumerable: true });
  2400. window.SVGPathSegLinetoHorizontalRel = function (owningPathSegList, x) {
  2401. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL, "h", owningPathSegList);
  2402. this._x = x;
  2403. };
  2404. window.SVGPathSegLinetoHorizontalRel.prototype = Object.create(window.SVGPathSeg.prototype);
  2405. window.SVGPathSegLinetoHorizontalRel.prototype.toString = function () {
  2406. return "[object SVGPathSegLinetoHorizontalRel]";
  2407. };
  2408. window.SVGPathSegLinetoHorizontalRel.prototype._asPathString = function () {
  2409. return this.pathSegTypeAsLetter + " " + this._x;
  2410. };
  2411. window.SVGPathSegLinetoHorizontalRel.prototype.clone = function () {
  2412. return new window.SVGPathSegLinetoHorizontalRel(undefined, this._x);
  2413. };
  2414. Object.defineProperty(window.SVGPathSegLinetoHorizontalRel.prototype, "x", { get: function get() {
  2415. return this._x;
  2416. }, set: function set(x) {
  2417. this._x = x;this._segmentChanged();
  2418. }, enumerable: true });
  2419. window.SVGPathSegLinetoVerticalAbs = function (owningPathSegList, y) {
  2420. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS, "V", owningPathSegList);
  2421. this._y = y;
  2422. };
  2423. window.SVGPathSegLinetoVerticalAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  2424. window.SVGPathSegLinetoVerticalAbs.prototype.toString = function () {
  2425. return "[object SVGPathSegLinetoVerticalAbs]";
  2426. };
  2427. window.SVGPathSegLinetoVerticalAbs.prototype._asPathString = function () {
  2428. return this.pathSegTypeAsLetter + " " + this._y;
  2429. };
  2430. window.SVGPathSegLinetoVerticalAbs.prototype.clone = function () {
  2431. return new window.SVGPathSegLinetoVerticalAbs(undefined, this._y);
  2432. };
  2433. Object.defineProperty(window.SVGPathSegLinetoVerticalAbs.prototype, "y", { get: function get() {
  2434. return this._y;
  2435. }, set: function set(y) {
  2436. this._y = y;this._segmentChanged();
  2437. }, enumerable: true });
  2438. window.SVGPathSegLinetoVerticalRel = function (owningPathSegList, y) {
  2439. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL, "v", owningPathSegList);
  2440. this._y = y;
  2441. };
  2442. window.SVGPathSegLinetoVerticalRel.prototype = Object.create(window.SVGPathSeg.prototype);
  2443. window.SVGPathSegLinetoVerticalRel.prototype.toString = function () {
  2444. return "[object SVGPathSegLinetoVerticalRel]";
  2445. };
  2446. window.SVGPathSegLinetoVerticalRel.prototype._asPathString = function () {
  2447. return this.pathSegTypeAsLetter + " " + this._y;
  2448. };
  2449. window.SVGPathSegLinetoVerticalRel.prototype.clone = function () {
  2450. return new window.SVGPathSegLinetoVerticalRel(undefined, this._y);
  2451. };
  2452. Object.defineProperty(window.SVGPathSegLinetoVerticalRel.prototype, "y", { get: function get() {
  2453. return this._y;
  2454. }, set: function set(y) {
  2455. this._y = y;this._segmentChanged();
  2456. }, enumerable: true });
  2457. window.SVGPathSegCurvetoCubicSmoothAbs = function (owningPathSegList, x, y, x2, y2) {
  2458. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS, "S", owningPathSegList);
  2459. this._x = x;
  2460. this._y = y;
  2461. this._x2 = x2;
  2462. this._y2 = y2;
  2463. };
  2464. window.SVGPathSegCurvetoCubicSmoothAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  2465. window.SVGPathSegCurvetoCubicSmoothAbs.prototype.toString = function () {
  2466. return "[object SVGPathSegCurvetoCubicSmoothAbs]";
  2467. };
  2468. window.SVGPathSegCurvetoCubicSmoothAbs.prototype._asPathString = function () {
  2469. return this.pathSegTypeAsLetter + " " + this._x2 + " " + this._y2 + " " + this._x + " " + this._y;
  2470. };
  2471. window.SVGPathSegCurvetoCubicSmoothAbs.prototype.clone = function () {
  2472. return new window.SVGPathSegCurvetoCubicSmoothAbs(undefined, this._x, this._y, this._x2, this._y2);
  2473. };
  2474. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothAbs.prototype, "x", { get: function get() {
  2475. return this._x;
  2476. }, set: function set(x) {
  2477. this._x = x;this._segmentChanged();
  2478. }, enumerable: true });
  2479. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothAbs.prototype, "y", { get: function get() {
  2480. return this._y;
  2481. }, set: function set(y) {
  2482. this._y = y;this._segmentChanged();
  2483. }, enumerable: true });
  2484. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothAbs.prototype, "x2", { get: function get() {
  2485. return this._x2;
  2486. }, set: function set(x2) {
  2487. this._x2 = x2;this._segmentChanged();
  2488. }, enumerable: true });
  2489. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothAbs.prototype, "y2", { get: function get() {
  2490. return this._y2;
  2491. }, set: function set(y2) {
  2492. this._y2 = y2;this._segmentChanged();
  2493. }, enumerable: true });
  2494. window.SVGPathSegCurvetoCubicSmoothRel = function (owningPathSegList, x, y, x2, y2) {
  2495. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL, "s", owningPathSegList);
  2496. this._x = x;
  2497. this._y = y;
  2498. this._x2 = x2;
  2499. this._y2 = y2;
  2500. };
  2501. window.SVGPathSegCurvetoCubicSmoothRel.prototype = Object.create(window.SVGPathSeg.prototype);
  2502. window.SVGPathSegCurvetoCubicSmoothRel.prototype.toString = function () {
  2503. return "[object SVGPathSegCurvetoCubicSmoothRel]";
  2504. };
  2505. window.SVGPathSegCurvetoCubicSmoothRel.prototype._asPathString = function () {
  2506. return this.pathSegTypeAsLetter + " " + this._x2 + " " + this._y2 + " " + this._x + " " + this._y;
  2507. };
  2508. window.SVGPathSegCurvetoCubicSmoothRel.prototype.clone = function () {
  2509. return new window.SVGPathSegCurvetoCubicSmoothRel(undefined, this._x, this._y, this._x2, this._y2);
  2510. };
  2511. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothRel.prototype, "x", { get: function get() {
  2512. return this._x;
  2513. }, set: function set(x) {
  2514. this._x = x;this._segmentChanged();
  2515. }, enumerable: true });
  2516. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothRel.prototype, "y", { get: function get() {
  2517. return this._y;
  2518. }, set: function set(y) {
  2519. this._y = y;this._segmentChanged();
  2520. }, enumerable: true });
  2521. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothRel.prototype, "x2", { get: function get() {
  2522. return this._x2;
  2523. }, set: function set(x2) {
  2524. this._x2 = x2;this._segmentChanged();
  2525. }, enumerable: true });
  2526. Object.defineProperty(window.SVGPathSegCurvetoCubicSmoothRel.prototype, "y2", { get: function get() {
  2527. return this._y2;
  2528. }, set: function set(y2) {
  2529. this._y2 = y2;this._segmentChanged();
  2530. }, enumerable: true });
  2531. window.SVGPathSegCurvetoQuadraticSmoothAbs = function (owningPathSegList, x, y) {
  2532. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS, "T", owningPathSegList);
  2533. this._x = x;
  2534. this._y = y;
  2535. };
  2536. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype = Object.create(window.SVGPathSeg.prototype);
  2537. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype.toString = function () {
  2538. return "[object SVGPathSegCurvetoQuadraticSmoothAbs]";
  2539. };
  2540. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype._asPathString = function () {
  2541. return this.pathSegTypeAsLetter + " " + this._x + " " + this._y;
  2542. };
  2543. window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype.clone = function () {
  2544. return new window.SVGPathSegCurvetoQuadraticSmoothAbs(undefined, this._x, this._y);
  2545. };
  2546. Object.defineProperty(window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype, "x", { get: function get() {
  2547. return this._x;
  2548. }, set: function set(x) {
  2549. this._x = x;this._segmentChanged();
  2550. }, enumerable: true });
  2551. Object.defineProperty(window.SVGPathSegCurvetoQuadraticSmoothAbs.prototype, "y", { get: function get() {
  2552. return this._y;
  2553. }, set: function set(y) {
  2554. this._y = y;this._segmentChanged();
  2555. }, enumerable: true });
  2556. window.SVGPathSegCurvetoQuadraticSmoothRel = function (owningPathSegList, x, y) {
  2557. window.SVGPathSeg.call(this, window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL, "t", owningPathSegList);
  2558. this._x = x;
  2559. this._y = y;
  2560. };
  2561. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype = Object.create(window.SVGPathSeg.prototype);
  2562. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype.toString = function () {
  2563. return "[object SVGPathSegCurvetoQuadraticSmoothRel]";
  2564. };
  2565. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype._asPathString = function () {
  2566. return this.pathSegTypeAsLetter + " " + this._x + " " + this._y;
  2567. };
  2568. window.SVGPathSegCurvetoQuadraticSmoothRel.prototype.clone = function () {
  2569. return new window.SVGPathSegCurvetoQuadraticSmoothRel(undefined, this._x, this._y);
  2570. };
  2571. Object.defineProperty(window.SVGPathSegCurvetoQuadraticSmoothRel.prototype, "x", { get: function get() {
  2572. return this._x;
  2573. }, set: function set(x) {
  2574. this._x = x;this._segmentChanged();
  2575. }, enumerable: true });
  2576. Object.defineProperty(window.SVGPathSegCurvetoQuadraticSmoothRel.prototype, "y", { get: function get() {
  2577. return this._y;
  2578. }, set: function set(y) {
  2579. this._y = y;this._segmentChanged();
  2580. }, enumerable: true });
  2581. // Add createSVGPathSeg* functions to window.SVGPathElement.
  2582. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-Interfacewindow.SVGPathElement.
  2583. window.SVGPathElement.prototype.createSVGPathSegClosePath = function () {
  2584. return new window.SVGPathSegClosePath(undefined);
  2585. };
  2586. window.SVGPathElement.prototype.createSVGPathSegMovetoAbs = function (x, y) {
  2587. return new window.SVGPathSegMovetoAbs(undefined, x, y);
  2588. };
  2589. window.SVGPathElement.prototype.createSVGPathSegMovetoRel = function (x, y) {
  2590. return new window.SVGPathSegMovetoRel(undefined, x, y);
  2591. };
  2592. window.SVGPathElement.prototype.createSVGPathSegLinetoAbs = function (x, y) {
  2593. return new window.SVGPathSegLinetoAbs(undefined, x, y);
  2594. };
  2595. window.SVGPathElement.prototype.createSVGPathSegLinetoRel = function (x, y) {
  2596. return new window.SVGPathSegLinetoRel(undefined, x, y);
  2597. };
  2598. window.SVGPathElement.prototype.createSVGPathSegCurvetoCubicAbs = function (x, y, x1, y1, x2, y2) {
  2599. return new window.SVGPathSegCurvetoCubicAbs(undefined, x, y, x1, y1, x2, y2);
  2600. };
  2601. window.SVGPathElement.prototype.createSVGPathSegCurvetoCubicRel = function (x, y, x1, y1, x2, y2) {
  2602. return new window.SVGPathSegCurvetoCubicRel(undefined, x, y, x1, y1, x2, y2);
  2603. };
  2604. window.SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticAbs = function (x, y, x1, y1) {
  2605. return new window.SVGPathSegCurvetoQuadraticAbs(undefined, x, y, x1, y1);
  2606. };
  2607. window.SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticRel = function (x, y, x1, y1) {
  2608. return new window.SVGPathSegCurvetoQuadraticRel(undefined, x, y, x1, y1);
  2609. };
  2610. window.SVGPathElement.prototype.createSVGPathSegArcAbs = function (x, y, r1, r2, angle, largeArcFlag, sweepFlag) {
  2611. return new window.SVGPathSegArcAbs(undefined, x, y, r1, r2, angle, largeArcFlag, sweepFlag);
  2612. };
  2613. window.SVGPathElement.prototype.createSVGPathSegArcRel = function (x, y, r1, r2, angle, largeArcFlag, sweepFlag) {
  2614. return new window.SVGPathSegArcRel(undefined, x, y, r1, r2, angle, largeArcFlag, sweepFlag);
  2615. };
  2616. window.SVGPathElement.prototype.createSVGPathSegLinetoHorizontalAbs = function (x) {
  2617. return new window.SVGPathSegLinetoHorizontalAbs(undefined, x);
  2618. };
  2619. window.SVGPathElement.prototype.createSVGPathSegLinetoHorizontalRel = function (x) {
  2620. return new window.SVGPathSegLinetoHorizontalRel(undefined, x);
  2621. };
  2622. window.SVGPathElement.prototype.createSVGPathSegLinetoVerticalAbs = function (y) {
  2623. return new window.SVGPathSegLinetoVerticalAbs(undefined, y);
  2624. };
  2625. window.SVGPathElement.prototype.createSVGPathSegLinetoVerticalRel = function (y) {
  2626. return new window.SVGPathSegLinetoVerticalRel(undefined, y);
  2627. };
  2628. window.SVGPathElement.prototype.createSVGPathSegCurvetoCubicSmoothAbs = function (x, y, x2, y2) {
  2629. return new window.SVGPathSegCurvetoCubicSmoothAbs(undefined, x, y, x2, y2);
  2630. };
  2631. window.SVGPathElement.prototype.createSVGPathSegCurvetoCubicSmoothRel = function (x, y, x2, y2) {
  2632. return new window.SVGPathSegCurvetoCubicSmoothRel(undefined, x, y, x2, y2);
  2633. };
  2634. window.SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticSmoothAbs = function (x, y) {
  2635. return new window.SVGPathSegCurvetoQuadraticSmoothAbs(undefined, x, y);
  2636. };
  2637. window.SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticSmoothRel = function (x, y) {
  2638. return new window.SVGPathSegCurvetoQuadraticSmoothRel(undefined, x, y);
  2639. };
  2640. if (!("getPathSegAtLength" in window.SVGPathElement.prototype)) {
  2641. // Add getPathSegAtLength to SVGPathElement.
  2642. // Spec: https://www.w3.org/TR/SVG11/single-page.html#paths-__svg__SVGPathElement__getPathSegAtLength
  2643. // This polyfill requires SVGPathElement.getTotalLength to implement the distance-along-a-path algorithm.
  2644. window.SVGPathElement.prototype.getPathSegAtLength = function (distance) {
  2645. if (distance === undefined || !isFinite(distance)) throw "Invalid arguments.";
  2646. var measurementElement = document.createElementNS("http://www.w3.org/2000/svg", "path");
  2647. measurementElement.setAttribute("d", this.getAttribute("d"));
  2648. var lastPathSegment = measurementElement.pathSegList.numberOfItems - 1;
  2649. // If the path is empty, return 0.
  2650. if (lastPathSegment <= 0) return 0;
  2651. do {
  2652. measurementElement.pathSegList.removeItem(lastPathSegment);
  2653. if (distance > measurementElement.getTotalLength()) break;
  2654. lastPathSegment--;
  2655. } while (lastPathSegment > 0);
  2656. return lastPathSegment;
  2657. };
  2658. }
  2659. }
  2660. if (!("SVGPathSegList" in window)) {
  2661. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGPathSegList
  2662. window.SVGPathSegList = function (pathElement) {
  2663. this._pathElement = pathElement;
  2664. this._list = this._parsePath(this._pathElement.getAttribute("d"));
  2665. // Use a MutationObserver to catch changes to the path's "d" attribute.
  2666. this._mutationObserverConfig = { "attributes": true, "attributeFilter": ["d"] };
  2667. this._pathElementMutationObserver = new MutationObserver(this._updateListFromPathMutations.bind(this));
  2668. this._pathElementMutationObserver.observe(this._pathElement, this._mutationObserverConfig);
  2669. };
  2670. window.SVGPathSegList.prototype.classname = "SVGPathSegList";
  2671. Object.defineProperty(window.SVGPathSegList.prototype, "numberOfItems", {
  2672. get: function get() {
  2673. this._checkPathSynchronizedToList();
  2674. return this._list.length;
  2675. },
  2676. enumerable: true
  2677. });
  2678. // Add the pathSegList accessors to window.SVGPathElement.
  2679. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGAnimatedPathData
  2680. Object.defineProperty(window.SVGPathElement.prototype, "pathSegList", {
  2681. get: function get() {
  2682. if (!this._pathSegList) this._pathSegList = new window.SVGPathSegList(this);
  2683. return this._pathSegList;
  2684. },
  2685. enumerable: true
  2686. });
  2687. // FIXME: The following are not implemented and simply return window.SVGPathElement.pathSegList.
  2688. Object.defineProperty(window.SVGPathElement.prototype, "normalizedPathSegList", { get: function get() {
  2689. return this.pathSegList;
  2690. }, enumerable: true });
  2691. Object.defineProperty(window.SVGPathElement.prototype, "animatedPathSegList", { get: function get() {
  2692. return this.pathSegList;
  2693. }, enumerable: true });
  2694. Object.defineProperty(window.SVGPathElement.prototype, "animatedNormalizedPathSegList", { get: function get() {
  2695. return this.pathSegList;
  2696. }, enumerable: true });
  2697. // Process any pending mutations to the path element and update the list as needed.
  2698. // This should be the first call of all public functions and is needed because
  2699. // MutationObservers are not synchronous so we can have pending asynchronous mutations.
  2700. window.SVGPathSegList.prototype._checkPathSynchronizedToList = function () {
  2701. this._updateListFromPathMutations(this._pathElementMutationObserver.takeRecords());
  2702. };
  2703. window.SVGPathSegList.prototype._updateListFromPathMutations = function (mutationRecords) {
  2704. if (!this._pathElement) return;
  2705. var hasPathMutations = false;
  2706. mutationRecords.forEach(function (record) {
  2707. if (record.attributeName == "d") hasPathMutations = true;
  2708. });
  2709. if (hasPathMutations) this._list = this._parsePath(this._pathElement.getAttribute("d"));
  2710. };
  2711. // Serialize the list and update the path's 'd' attribute.
  2712. window.SVGPathSegList.prototype._writeListToPath = function () {
  2713. this._pathElementMutationObserver.disconnect();
  2714. this._pathElement.setAttribute("d", window.SVGPathSegList._pathSegArrayAsString(this._list));
  2715. this._pathElementMutationObserver.observe(this._pathElement, this._mutationObserverConfig);
  2716. };
  2717. // When a path segment changes the list needs to be synchronized back to the path element.
  2718. window.SVGPathSegList.prototype.segmentChanged = function (pathSeg) {
  2719. this._writeListToPath();
  2720. };
  2721. window.SVGPathSegList.prototype.clear = function () {
  2722. this._checkPathSynchronizedToList();
  2723. this._list.forEach(function (pathSeg) {
  2724. pathSeg._owningPathSegList = null;
  2725. });
  2726. this._list = [];
  2727. this._writeListToPath();
  2728. };
  2729. window.SVGPathSegList.prototype.initialize = function (newItem) {
  2730. this._checkPathSynchronizedToList();
  2731. this._list = [newItem];
  2732. newItem._owningPathSegList = this;
  2733. this._writeListToPath();
  2734. return newItem;
  2735. };
  2736. window.SVGPathSegList.prototype._checkValidIndex = function (index) {
  2737. if (isNaN(index) || index < 0 || index >= this.numberOfItems) throw "INDEX_SIZE_ERR";
  2738. };
  2739. window.SVGPathSegList.prototype.getItem = function (index) {
  2740. this._checkPathSynchronizedToList();
  2741. this._checkValidIndex(index);
  2742. return this._list[index];
  2743. };
  2744. window.SVGPathSegList.prototype.insertItemBefore = function (newItem, index) {
  2745. this._checkPathSynchronizedToList();
  2746. // Spec: If the index is greater than or equal to numberOfItems, then the new item is appended to the end of the list.
  2747. if (index > this.numberOfItems) index = this.numberOfItems;
  2748. if (newItem._owningPathSegList) {
  2749. // SVG2 spec says to make a copy.
  2750. newItem = newItem.clone();
  2751. }
  2752. this._list.splice(index, 0, newItem);
  2753. newItem._owningPathSegList = this;
  2754. this._writeListToPath();
  2755. return newItem;
  2756. };
  2757. window.SVGPathSegList.prototype.replaceItem = function (newItem, index) {
  2758. this._checkPathSynchronizedToList();
  2759. if (newItem._owningPathSegList) {
  2760. // SVG2 spec says to make a copy.
  2761. newItem = newItem.clone();
  2762. }
  2763. this._checkValidIndex(index);
  2764. this._list[index] = newItem;
  2765. newItem._owningPathSegList = this;
  2766. this._writeListToPath();
  2767. return newItem;
  2768. };
  2769. window.SVGPathSegList.prototype.removeItem = function (index) {
  2770. this._checkPathSynchronizedToList();
  2771. this._checkValidIndex(index);
  2772. var item = this._list[index];
  2773. this._list.splice(index, 1);
  2774. this._writeListToPath();
  2775. return item;
  2776. };
  2777. window.SVGPathSegList.prototype.appendItem = function (newItem) {
  2778. this._checkPathSynchronizedToList();
  2779. if (newItem._owningPathSegList) {
  2780. // SVG2 spec says to make a copy.
  2781. newItem = newItem.clone();
  2782. }
  2783. this._list.push(newItem);
  2784. newItem._owningPathSegList = this;
  2785. // TODO: Optimize this to just append to the existing attribute.
  2786. this._writeListToPath();
  2787. return newItem;
  2788. };
  2789. window.SVGPathSegList._pathSegArrayAsString = function (pathSegArray) {
  2790. var string = "";
  2791. var first = true;
  2792. pathSegArray.forEach(function (pathSeg) {
  2793. if (first) {
  2794. first = false;
  2795. string += pathSeg._asPathString();
  2796. } else {
  2797. string += " " + pathSeg._asPathString();
  2798. }
  2799. });
  2800. return string;
  2801. };
  2802. // This closely follows SVGPathParser::parsePath from Source/core/svg/SVGPathParser.cpp.
  2803. window.SVGPathSegList.prototype._parsePath = function (string) {
  2804. if (!string || string.length == 0) return [];
  2805. var owningPathSegList = this;
  2806. var Builder = function Builder() {
  2807. this.pathSegList = [];
  2808. };
  2809. Builder.prototype.appendSegment = function (pathSeg) {
  2810. this.pathSegList.push(pathSeg);
  2811. };
  2812. var Source = function Source(string) {
  2813. this._string = string;
  2814. this._currentIndex = 0;
  2815. this._endIndex = this._string.length;
  2816. this._previousCommand = window.SVGPathSeg.PATHSEG_UNKNOWN;
  2817. this._skipOptionalSpaces();
  2818. };
  2819. Source.prototype._isCurrentSpace = function () {
  2820. var character = this._string[this._currentIndex];
  2821. return character <= " " && (character == " " || character == "\n" || character == "\t" || character == "\r" || character == "\f");
  2822. };
  2823. Source.prototype._skipOptionalSpaces = function () {
  2824. while (this._currentIndex < this._endIndex && this._isCurrentSpace()) {
  2825. this._currentIndex++;
  2826. }return this._currentIndex < this._endIndex;
  2827. };
  2828. Source.prototype._skipOptionalSpacesOrDelimiter = function () {
  2829. if (this._currentIndex < this._endIndex && !this._isCurrentSpace() && this._string.charAt(this._currentIndex) != ",") return false;
  2830. if (this._skipOptionalSpaces()) {
  2831. if (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) == ",") {
  2832. this._currentIndex++;
  2833. this._skipOptionalSpaces();
  2834. }
  2835. }
  2836. return this._currentIndex < this._endIndex;
  2837. };
  2838. Source.prototype.hasMoreData = function () {
  2839. return this._currentIndex < this._endIndex;
  2840. };
  2841. Source.prototype.peekSegmentType = function () {
  2842. var lookahead = this._string[this._currentIndex];
  2843. return this._pathSegTypeFromChar(lookahead);
  2844. };
  2845. Source.prototype._pathSegTypeFromChar = function (lookahead) {
  2846. switch (lookahead) {
  2847. case "Z":
  2848. case "z":
  2849. return window.SVGPathSeg.PATHSEG_CLOSEPATH;
  2850. case "M":
  2851. return window.SVGPathSeg.PATHSEG_MOVETO_ABS;
  2852. case "m":
  2853. return window.SVGPathSeg.PATHSEG_MOVETO_REL;
  2854. case "L":
  2855. return window.SVGPathSeg.PATHSEG_LINETO_ABS;
  2856. case "l":
  2857. return window.SVGPathSeg.PATHSEG_LINETO_REL;
  2858. case "C":
  2859. return window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS;
  2860. case "c":
  2861. return window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL;
  2862. case "Q":
  2863. return window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS;
  2864. case "q":
  2865. return window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL;
  2866. case "A":
  2867. return window.SVGPathSeg.PATHSEG_ARC_ABS;
  2868. case "a":
  2869. return window.SVGPathSeg.PATHSEG_ARC_REL;
  2870. case "H":
  2871. return window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS;
  2872. case "h":
  2873. return window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL;
  2874. case "V":
  2875. return window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS;
  2876. case "v":
  2877. return window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL;
  2878. case "S":
  2879. return window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS;
  2880. case "s":
  2881. return window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL;
  2882. case "T":
  2883. return window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS;
  2884. case "t":
  2885. return window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL;
  2886. default:
  2887. return window.SVGPathSeg.PATHSEG_UNKNOWN;
  2888. }
  2889. };
  2890. Source.prototype._nextCommandHelper = function (lookahead, previousCommand) {
  2891. // Check for remaining coordinates in the current command.
  2892. if ((lookahead == "+" || lookahead == "-" || lookahead == "." || lookahead >= "0" && lookahead <= "9") && previousCommand != window.SVGPathSeg.PATHSEG_CLOSEPATH) {
  2893. if (previousCommand == window.SVGPathSeg.PATHSEG_MOVETO_ABS) return window.SVGPathSeg.PATHSEG_LINETO_ABS;
  2894. if (previousCommand == window.SVGPathSeg.PATHSEG_MOVETO_REL) return window.SVGPathSeg.PATHSEG_LINETO_REL;
  2895. return previousCommand;
  2896. }
  2897. return window.SVGPathSeg.PATHSEG_UNKNOWN;
  2898. };
  2899. Source.prototype.initialCommandIsMoveTo = function () {
  2900. // If the path is empty it is still valid, so return true.
  2901. if (!this.hasMoreData()) return true;
  2902. var command = this.peekSegmentType();
  2903. // Path must start with moveTo.
  2904. return command == window.SVGPathSeg.PATHSEG_MOVETO_ABS || command == window.SVGPathSeg.PATHSEG_MOVETO_REL;
  2905. };
  2906. // Parse a number from an SVG path. This very closely follows genericParseNumber(...) from Source/core/svg/SVGParserUtilities.cpp.
  2907. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-PathDataBNF
  2908. Source.prototype._parseNumber = function () {
  2909. var exponent = 0;
  2910. var integer = 0;
  2911. var frac = 1;
  2912. var decimal = 0;
  2913. var sign = 1;
  2914. var expsign = 1;
  2915. var startIndex = this._currentIndex;
  2916. this._skipOptionalSpaces();
  2917. // Read the sign.
  2918. if (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) == "+") this._currentIndex++;else if (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) == "-") {
  2919. this._currentIndex++;
  2920. sign = -1;
  2921. }
  2922. if (this._currentIndex == this._endIndex || (this._string.charAt(this._currentIndex) < "0" || this._string.charAt(this._currentIndex) > "9") && this._string.charAt(this._currentIndex) != ".")
  2923. // The first character of a number must be one of [0-9+-.].
  2924. return undefined;
  2925. // Read the integer part, build right-to-left.
  2926. var startIntPartIndex = this._currentIndex;
  2927. while (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) >= "0" && this._string.charAt(this._currentIndex) <= "9") {
  2928. this._currentIndex++;
  2929. } // Advance to first non-digit.
  2930. if (this._currentIndex != startIntPartIndex) {
  2931. var scanIntPartIndex = this._currentIndex - 1;
  2932. var multiplier = 1;
  2933. while (scanIntPartIndex >= startIntPartIndex) {
  2934. integer += multiplier * (this._string.charAt(scanIntPartIndex--) - "0");
  2935. multiplier *= 10;
  2936. }
  2937. }
  2938. // Read the decimals.
  2939. if (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) == ".") {
  2940. this._currentIndex++;
  2941. // There must be a least one digit following the .
  2942. if (this._currentIndex >= this._endIndex || this._string.charAt(this._currentIndex) < "0" || this._string.charAt(this._currentIndex) > "9") return undefined;
  2943. while (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) >= "0" && this._string.charAt(this._currentIndex) <= "9") {
  2944. frac *= 10;
  2945. decimal += (this._string.charAt(this._currentIndex) - "0") / frac;
  2946. this._currentIndex += 1;
  2947. }
  2948. }
  2949. // Read the exponent part.
  2950. if (this._currentIndex != startIndex && this._currentIndex + 1 < this._endIndex && (this._string.charAt(this._currentIndex) == "e" || this._string.charAt(this._currentIndex) == "E") && this._string.charAt(this._currentIndex + 1) != "x" && this._string.charAt(this._currentIndex + 1) != "m") {
  2951. this._currentIndex++;
  2952. // Read the sign of the exponent.
  2953. if (this._string.charAt(this._currentIndex) == "+") {
  2954. this._currentIndex++;
  2955. } else if (this._string.charAt(this._currentIndex) == "-") {
  2956. this._currentIndex++;
  2957. expsign = -1;
  2958. }
  2959. // There must be an exponent.
  2960. if (this._currentIndex >= this._endIndex || this._string.charAt(this._currentIndex) < "0" || this._string.charAt(this._currentIndex) > "9") return undefined;
  2961. while (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) >= "0" && this._string.charAt(this._currentIndex) <= "9") {
  2962. exponent *= 10;
  2963. exponent += this._string.charAt(this._currentIndex) - "0";
  2964. this._currentIndex++;
  2965. }
  2966. }
  2967. var number = integer + decimal;
  2968. number *= sign;
  2969. if (exponent) number *= Math.pow(10, expsign * exponent);
  2970. if (startIndex == this._currentIndex) return undefined;
  2971. this._skipOptionalSpacesOrDelimiter();
  2972. return number;
  2973. };
  2974. Source.prototype._parseArcFlag = function () {
  2975. if (this._currentIndex >= this._endIndex) return undefined;
  2976. var flag = false;
  2977. var flagChar = this._string.charAt(this._currentIndex++);
  2978. if (flagChar == "0") flag = false;else if (flagChar == "1") flag = true;else return undefined;
  2979. this._skipOptionalSpacesOrDelimiter();
  2980. return flag;
  2981. };
  2982. Source.prototype.parseSegment = function () {
  2983. var lookahead = this._string[this._currentIndex];
  2984. var command = this._pathSegTypeFromChar(lookahead);
  2985. if (command == window.SVGPathSeg.PATHSEG_UNKNOWN) {
  2986. // Possibly an implicit command. Not allowed if this is the first command.
  2987. if (this._previousCommand == window.SVGPathSeg.PATHSEG_UNKNOWN) return null;
  2988. command = this._nextCommandHelper(lookahead, this._previousCommand);
  2989. if (command == window.SVGPathSeg.PATHSEG_UNKNOWN) return null;
  2990. } else {
  2991. this._currentIndex++;
  2992. }
  2993. this._previousCommand = command;
  2994. switch (command) {
  2995. case window.SVGPathSeg.PATHSEG_MOVETO_REL:
  2996. return new window.SVGPathSegMovetoRel(owningPathSegList, this._parseNumber(), this._parseNumber());
  2997. case window.SVGPathSeg.PATHSEG_MOVETO_ABS:
  2998. return new window.SVGPathSegMovetoAbs(owningPathSegList, this._parseNumber(), this._parseNumber());
  2999. case window.SVGPathSeg.PATHSEG_LINETO_REL:
  3000. return new window.SVGPathSegLinetoRel(owningPathSegList, this._parseNumber(), this._parseNumber());
  3001. case window.SVGPathSeg.PATHSEG_LINETO_ABS:
  3002. return new window.SVGPathSegLinetoAbs(owningPathSegList, this._parseNumber(), this._parseNumber());
  3003. case window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL:
  3004. return new window.SVGPathSegLinetoHorizontalRel(owningPathSegList, this._parseNumber());
  3005. case window.SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS:
  3006. return new window.SVGPathSegLinetoHorizontalAbs(owningPathSegList, this._parseNumber());
  3007. case window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL:
  3008. return new window.SVGPathSegLinetoVerticalRel(owningPathSegList, this._parseNumber());
  3009. case window.SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS:
  3010. return new window.SVGPathSegLinetoVerticalAbs(owningPathSegList, this._parseNumber());
  3011. case window.SVGPathSeg.PATHSEG_CLOSEPATH:
  3012. this._skipOptionalSpaces();
  3013. return new window.SVGPathSegClosePath(owningPathSegList);
  3014. case window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL:
  3015. var points = { x1: this._parseNumber(), y1: this._parseNumber(), x2: this._parseNumber(), y2: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber() };
  3016. return new window.SVGPathSegCurvetoCubicRel(owningPathSegList, points.x, points.y, points.x1, points.y1, points.x2, points.y2);
  3017. case window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS:
  3018. var points = { x1: this._parseNumber(), y1: this._parseNumber(), x2: this._parseNumber(), y2: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber() };
  3019. return new window.SVGPathSegCurvetoCubicAbs(owningPathSegList, points.x, points.y, points.x1, points.y1, points.x2, points.y2);
  3020. case window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL:
  3021. var points = { x2: this._parseNumber(), y2: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber() };
  3022. return new window.SVGPathSegCurvetoCubicSmoothRel(owningPathSegList, points.x, points.y, points.x2, points.y2);
  3023. case window.SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS:
  3024. var points = { x2: this._parseNumber(), y2: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber() };
  3025. return new window.SVGPathSegCurvetoCubicSmoothAbs(owningPathSegList, points.x, points.y, points.x2, points.y2);
  3026. case window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL:
  3027. var points = { x1: this._parseNumber(), y1: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber() };
  3028. return new window.SVGPathSegCurvetoQuadraticRel(owningPathSegList, points.x, points.y, points.x1, points.y1);
  3029. case window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS:
  3030. var points = { x1: this._parseNumber(), y1: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber() };
  3031. return new window.SVGPathSegCurvetoQuadraticAbs(owningPathSegList, points.x, points.y, points.x1, points.y1);
  3032. case window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL:
  3033. return new window.SVGPathSegCurvetoQuadraticSmoothRel(owningPathSegList, this._parseNumber(), this._parseNumber());
  3034. case window.SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS:
  3035. return new window.SVGPathSegCurvetoQuadraticSmoothAbs(owningPathSegList, this._parseNumber(), this._parseNumber());
  3036. case window.SVGPathSeg.PATHSEG_ARC_REL:
  3037. var points = { x1: this._parseNumber(), y1: this._parseNumber(), arcAngle: this._parseNumber(), arcLarge: this._parseArcFlag(), arcSweep: this._parseArcFlag(), x: this._parseNumber(), y: this._parseNumber() };
  3038. return new window.SVGPathSegArcRel(owningPathSegList, points.x, points.y, points.x1, points.y1, points.arcAngle, points.arcLarge, points.arcSweep);
  3039. case window.SVGPathSeg.PATHSEG_ARC_ABS:
  3040. var points = { x1: this._parseNumber(), y1: this._parseNumber(), arcAngle: this._parseNumber(), arcLarge: this._parseArcFlag(), arcSweep: this._parseArcFlag(), x: this._parseNumber(), y: this._parseNumber() };
  3041. return new window.SVGPathSegArcAbs(owningPathSegList, points.x, points.y, points.x1, points.y1, points.arcAngle, points.arcLarge, points.arcSweep);
  3042. default:
  3043. throw "Unknown path seg type.";
  3044. }
  3045. };
  3046. var builder = new Builder();
  3047. var source = new Source(string);
  3048. if (!source.initialCommandIsMoveTo()) return [];
  3049. while (source.hasMoreData()) {
  3050. var pathSeg = source.parseSegment();
  3051. if (!pathSeg) return [];
  3052. builder.appendSegment(pathSeg);
  3053. }
  3054. return builder.pathSegList;
  3055. };
  3056. }
  3057. })();
  3058. /* jshint ignore:end */
  3059. c3_chart_fn.axis = function () {};
  3060. c3_chart_fn.axis.labels = function (labels) {
  3061. var $$ = this.internal;
  3062. if (arguments.length) {
  3063. Object.keys(labels).forEach(function (axisId) {
  3064. $$.axis.setLabelText(axisId, labels[axisId]);
  3065. });
  3066. $$.axis.updateLabels();
  3067. }
  3068. // TODO: return some values?
  3069. };
  3070. c3_chart_fn.axis.max = function (max) {
  3071. var $$ = this.internal,
  3072. config = $$.config;
  3073. if (arguments.length) {
  3074. if ((typeof max === 'undefined' ? 'undefined' : _typeof(max)) === 'object') {
  3075. if (isValue(max.x)) {
  3076. config.axis_x_max = max.x;
  3077. }
  3078. if (isValue(max.y)) {
  3079. config.axis_y_max = max.y;
  3080. }
  3081. if (isValue(max.y2)) {
  3082. config.axis_y2_max = max.y2;
  3083. }
  3084. } else {
  3085. config.axis_y_max = config.axis_y2_max = max;
  3086. }
  3087. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true });
  3088. } else {
  3089. return {
  3090. x: config.axis_x_max,
  3091. y: config.axis_y_max,
  3092. y2: config.axis_y2_max
  3093. };
  3094. }
  3095. };
  3096. c3_chart_fn.axis.min = function (min) {
  3097. var $$ = this.internal,
  3098. config = $$.config;
  3099. if (arguments.length) {
  3100. if ((typeof min === 'undefined' ? 'undefined' : _typeof(min)) === 'object') {
  3101. if (isValue(min.x)) {
  3102. config.axis_x_min = min.x;
  3103. }
  3104. if (isValue(min.y)) {
  3105. config.axis_y_min = min.y;
  3106. }
  3107. if (isValue(min.y2)) {
  3108. config.axis_y2_min = min.y2;
  3109. }
  3110. } else {
  3111. config.axis_y_min = config.axis_y2_min = min;
  3112. }
  3113. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true });
  3114. } else {
  3115. return {
  3116. x: config.axis_x_min,
  3117. y: config.axis_y_min,
  3118. y2: config.axis_y2_min
  3119. };
  3120. }
  3121. };
  3122. c3_chart_fn.axis.range = function (range) {
  3123. if (arguments.length) {
  3124. if (isDefined(range.max)) {
  3125. this.axis.max(range.max);
  3126. }
  3127. if (isDefined(range.min)) {
  3128. this.axis.min(range.min);
  3129. }
  3130. } else {
  3131. return {
  3132. max: this.axis.max(),
  3133. min: this.axis.min()
  3134. };
  3135. }
  3136. };
  3137. c3_chart_fn.category = function (i, category) {
  3138. var $$ = this.internal,
  3139. config = $$.config;
  3140. if (arguments.length > 1) {
  3141. config.axis_x_categories[i] = category;
  3142. $$.redraw();
  3143. }
  3144. return config.axis_x_categories[i];
  3145. };
  3146. c3_chart_fn.categories = function (categories) {
  3147. var $$ = this.internal,
  3148. config = $$.config;
  3149. if (!arguments.length) {
  3150. return config.axis_x_categories;
  3151. }
  3152. config.axis_x_categories = categories;
  3153. $$.redraw();
  3154. return config.axis_x_categories;
  3155. };
  3156. c3_chart_fn.resize = function (size) {
  3157. var $$ = this.internal,
  3158. config = $$.config;
  3159. config.size_width = size ? size.width : null;
  3160. config.size_height = size ? size.height : null;
  3161. this.flush();
  3162. };
  3163. c3_chart_fn.flush = function () {
  3164. var $$ = this.internal;
  3165. $$.updateAndRedraw({ withLegend: true, withTransition: false, withTransitionForTransform: false });
  3166. };
  3167. c3_chart_fn.destroy = function () {
  3168. var $$ = this.internal;
  3169. window.clearInterval($$.intervalForObserveInserted);
  3170. if ($$.resizeTimeout !== undefined) {
  3171. window.clearTimeout($$.resizeTimeout);
  3172. }
  3173. if (window.detachEvent) {
  3174. window.detachEvent('onresize', $$.resizeIfElementDisplayed);
  3175. } else if (window.removeEventListener) {
  3176. window.removeEventListener('resize', $$.resizeIfElementDisplayed);
  3177. } else {
  3178. var wrapper = window.onresize;
  3179. // check if no one else removed our wrapper and remove our resizeFunction from it
  3180. if (wrapper && wrapper.add && wrapper.remove) {
  3181. wrapper.remove($$.resizeFunction);
  3182. }
  3183. }
  3184. // remove the inner resize functions
  3185. $$.resizeFunction.remove();
  3186. $$.selectChart.classed('c3', false).html("");
  3187. // MEMO: this is needed because the reference of some elements will not be released, then memory leak will happen.
  3188. Object.keys($$).forEach(function (key) {
  3189. $$[key] = null;
  3190. });
  3191. return null;
  3192. };
  3193. // TODO: fix
  3194. c3_chart_fn.color = function (id) {
  3195. var $$ = this.internal;
  3196. return $$.color(id); // more patterns
  3197. };
  3198. c3_chart_fn.data = function (targetIds) {
  3199. var targets = this.internal.data.targets;
  3200. return typeof targetIds === 'undefined' ? targets : targets.filter(function (t) {
  3201. return [].concat(targetIds).indexOf(t.id) >= 0;
  3202. });
  3203. };
  3204. c3_chart_fn.data.shown = function (targetIds) {
  3205. return this.internal.filterTargetsToShow(this.data(targetIds));
  3206. };
  3207. c3_chart_fn.data.values = function (targetId) {
  3208. var targets,
  3209. values = null;
  3210. if (targetId) {
  3211. targets = this.data(targetId);
  3212. values = targets[0] ? targets[0].values.map(function (d) {
  3213. return d.value;
  3214. }) : null;
  3215. }
  3216. return values;
  3217. };
  3218. c3_chart_fn.data.names = function (names) {
  3219. this.internal.clearLegendItemTextBoxCache();
  3220. return this.internal.updateDataAttributes('names', names);
  3221. };
  3222. c3_chart_fn.data.colors = function (colors) {
  3223. return this.internal.updateDataAttributes('colors', colors);
  3224. };
  3225. c3_chart_fn.data.axes = function (axes) {
  3226. return this.internal.updateDataAttributes('axes', axes);
  3227. };
  3228. c3_chart_fn.flow = function (args) {
  3229. var $$ = this.internal,
  3230. targets,
  3231. data,
  3232. notfoundIds = [],
  3233. orgDataCount = $$.getMaxDataCount(),
  3234. dataCount,
  3235. domain,
  3236. baseTarget,
  3237. baseValue,
  3238. length = 0,
  3239. tail = 0,
  3240. diff,
  3241. to;
  3242. if (args.json) {
  3243. data = $$.convertJsonToData(args.json, args.keys);
  3244. } else if (args.rows) {
  3245. data = $$.convertRowsToData(args.rows);
  3246. } else if (args.columns) {
  3247. data = $$.convertColumnsToData(args.columns);
  3248. } else {
  3249. return;
  3250. }
  3251. targets = $$.convertDataToTargets(data, true);
  3252. // Update/Add data
  3253. $$.data.targets.forEach(function (t) {
  3254. var found = false,
  3255. i,
  3256. j;
  3257. for (i = 0; i < targets.length; i++) {
  3258. if (t.id === targets[i].id) {
  3259. found = true;
  3260. if (t.values[t.values.length - 1]) {
  3261. tail = t.values[t.values.length - 1].index + 1;
  3262. }
  3263. length = targets[i].values.length;
  3264. for (j = 0; j < length; j++) {
  3265. targets[i].values[j].index = tail + j;
  3266. if (!$$.isTimeSeries()) {
  3267. targets[i].values[j].x = tail + j;
  3268. }
  3269. }
  3270. t.values = t.values.concat(targets[i].values);
  3271. targets.splice(i, 1);
  3272. break;
  3273. }
  3274. }
  3275. if (!found) {
  3276. notfoundIds.push(t.id);
  3277. }
  3278. });
  3279. // Append null for not found targets
  3280. $$.data.targets.forEach(function (t) {
  3281. var i, j;
  3282. for (i = 0; i < notfoundIds.length; i++) {
  3283. if (t.id === notfoundIds[i]) {
  3284. tail = t.values[t.values.length - 1].index + 1;
  3285. for (j = 0; j < length; j++) {
  3286. t.values.push({
  3287. id: t.id,
  3288. index: tail + j,
  3289. x: $$.isTimeSeries() ? $$.getOtherTargetX(tail + j) : tail + j,
  3290. value: null
  3291. });
  3292. }
  3293. }
  3294. }
  3295. });
  3296. // Generate null values for new target
  3297. if ($$.data.targets.length) {
  3298. targets.forEach(function (t) {
  3299. var i,
  3300. missing = [];
  3301. for (i = $$.data.targets[0].values[0].index; i < tail; i++) {
  3302. missing.push({
  3303. id: t.id,
  3304. index: i,
  3305. x: $$.isTimeSeries() ? $$.getOtherTargetX(i) : i,
  3306. value: null
  3307. });
  3308. }
  3309. t.values.forEach(function (v) {
  3310. v.index += tail;
  3311. if (!$$.isTimeSeries()) {
  3312. v.x += tail;
  3313. }
  3314. });
  3315. t.values = missing.concat(t.values);
  3316. });
  3317. }
  3318. $$.data.targets = $$.data.targets.concat(targets); // add remained
  3319. // check data count because behavior needs to change when it's only one
  3320. dataCount = $$.getMaxDataCount();
  3321. baseTarget = $$.data.targets[0];
  3322. baseValue = baseTarget.values[0];
  3323. // Update length to flow if needed
  3324. if (isDefined(args.to)) {
  3325. length = 0;
  3326. to = $$.isTimeSeries() ? $$.parseDate(args.to) : args.to;
  3327. baseTarget.values.forEach(function (v) {
  3328. if (v.x < to) {
  3329. length++;
  3330. }
  3331. });
  3332. } else if (isDefined(args.length)) {
  3333. length = args.length;
  3334. }
  3335. // If only one data, update the domain to flow from left edge of the chart
  3336. if (!orgDataCount) {
  3337. if ($$.isTimeSeries()) {
  3338. if (baseTarget.values.length > 1) {
  3339. diff = baseTarget.values[baseTarget.values.length - 1].x - baseValue.x;
  3340. } else {
  3341. diff = baseValue.x - $$.getXDomain($$.data.targets)[0];
  3342. }
  3343. } else {
  3344. diff = 1;
  3345. }
  3346. domain = [baseValue.x - diff, baseValue.x];
  3347. $$.updateXDomain(null, true, true, false, domain);
  3348. } else if (orgDataCount === 1) {
  3349. if ($$.isTimeSeries()) {
  3350. diff = (baseTarget.values[baseTarget.values.length - 1].x - baseValue.x) / 2;
  3351. domain = [new Date(+baseValue.x - diff), new Date(+baseValue.x + diff)];
  3352. $$.updateXDomain(null, true, true, false, domain);
  3353. }
  3354. }
  3355. // Set targets
  3356. $$.updateTargets($$.data.targets);
  3357. // Redraw with new targets
  3358. $$.redraw({
  3359. flow: {
  3360. index: baseValue.index,
  3361. length: length,
  3362. duration: isValue(args.duration) ? args.duration : $$.config.transition_duration,
  3363. done: args.done,
  3364. orgDataCount: orgDataCount
  3365. },
  3366. withLegend: true,
  3367. withTransition: orgDataCount > 1,
  3368. withTrimXDomain: false,
  3369. withUpdateXAxis: true
  3370. });
  3371. };
  3372. c3_chart_internal_fn.generateFlow = function (args) {
  3373. var $$ = this,
  3374. config = $$.config,
  3375. d3 = $$.d3;
  3376. return function () {
  3377. var targets = args.targets,
  3378. flow = args.flow,
  3379. drawBar = args.drawBar,
  3380. drawLine = args.drawLine,
  3381. drawArea = args.drawArea,
  3382. cx = args.cx,
  3383. cy = args.cy,
  3384. xv = args.xv,
  3385. xForText = args.xForText,
  3386. yForText = args.yForText,
  3387. duration = args.duration;
  3388. var translateX,
  3389. scaleX = 1,
  3390. transform,
  3391. flowIndex = flow.index,
  3392. flowLength = flow.length,
  3393. flowStart = $$.getValueOnIndex($$.data.targets[0].values, flowIndex),
  3394. flowEnd = $$.getValueOnIndex($$.data.targets[0].values, flowIndex + flowLength),
  3395. orgDomain = $$.x.domain(),
  3396. domain,
  3397. durationForFlow = flow.duration || duration,
  3398. done = flow.done || function () {},
  3399. wait = $$.generateWait();
  3400. var xgrid = $$.xgrid || d3.selectAll([]),
  3401. xgridLines = $$.xgridLines || d3.selectAll([]),
  3402. mainRegion = $$.mainRegion || d3.selectAll([]),
  3403. mainText = $$.mainText || d3.selectAll([]),
  3404. mainBar = $$.mainBar || d3.selectAll([]),
  3405. mainLine = $$.mainLine || d3.selectAll([]),
  3406. mainArea = $$.mainArea || d3.selectAll([]),
  3407. mainCircle = $$.mainCircle || d3.selectAll([]);
  3408. // set flag
  3409. $$.flowing = true;
  3410. // remove head data after rendered
  3411. $$.data.targets.forEach(function (d) {
  3412. d.values.splice(0, flowLength);
  3413. });
  3414. // update x domain to generate axis elements for flow
  3415. domain = $$.updateXDomain(targets, true, true);
  3416. // update elements related to x scale
  3417. if ($$.updateXGrid) {
  3418. $$.updateXGrid(true);
  3419. }
  3420. // generate transform to flow
  3421. if (!flow.orgDataCount) {
  3422. // if empty
  3423. if ($$.data.targets[0].values.length !== 1) {
  3424. translateX = $$.x(orgDomain[0]) - $$.x(domain[0]);
  3425. } else {
  3426. if ($$.isTimeSeries()) {
  3427. flowStart = $$.getValueOnIndex($$.data.targets[0].values, 0);
  3428. flowEnd = $$.getValueOnIndex($$.data.targets[0].values, $$.data.targets[0].values.length - 1);
  3429. translateX = $$.x(flowStart.x) - $$.x(flowEnd.x);
  3430. } else {
  3431. translateX = diffDomain(domain) / 2;
  3432. }
  3433. }
  3434. } else if (flow.orgDataCount === 1 || (flowStart && flowStart.x) === (flowEnd && flowEnd.x)) {
  3435. translateX = $$.x(orgDomain[0]) - $$.x(domain[0]);
  3436. } else {
  3437. if ($$.isTimeSeries()) {
  3438. translateX = $$.x(orgDomain[0]) - $$.x(domain[0]);
  3439. } else {
  3440. translateX = $$.x(flowStart.x) - $$.x(flowEnd.x);
  3441. }
  3442. }
  3443. scaleX = diffDomain(orgDomain) / diffDomain(domain);
  3444. transform = 'translate(' + translateX + ',0) scale(' + scaleX + ',1)';
  3445. $$.hideXGridFocus();
  3446. d3.transition().ease('linear').duration(durationForFlow).each(function () {
  3447. wait.add($$.axes.x.transition().call($$.xAxis));
  3448. wait.add(mainBar.transition().attr('transform', transform));
  3449. wait.add(mainLine.transition().attr('transform', transform));
  3450. wait.add(mainArea.transition().attr('transform', transform));
  3451. wait.add(mainCircle.transition().attr('transform', transform));
  3452. wait.add(mainText.transition().attr('transform', transform));
  3453. wait.add(mainRegion.filter($$.isRegionOnX).transition().attr('transform', transform));
  3454. wait.add(xgrid.transition().attr('transform', transform));
  3455. wait.add(xgridLines.transition().attr('transform', transform));
  3456. }).call(wait, function () {
  3457. var i,
  3458. shapes = [],
  3459. texts = [],
  3460. eventRects = [];
  3461. // remove flowed elements
  3462. if (flowLength) {
  3463. for (i = 0; i < flowLength; i++) {
  3464. shapes.push('.' + CLASS.shape + '-' + (flowIndex + i));
  3465. texts.push('.' + CLASS.text + '-' + (flowIndex + i));
  3466. eventRects.push('.' + CLASS.eventRect + '-' + (flowIndex + i));
  3467. }
  3468. $$.svg.selectAll('.' + CLASS.shapes).selectAll(shapes).remove();
  3469. $$.svg.selectAll('.' + CLASS.texts).selectAll(texts).remove();
  3470. $$.svg.selectAll('.' + CLASS.eventRects).selectAll(eventRects).remove();
  3471. $$.svg.select('.' + CLASS.xgrid).remove();
  3472. }
  3473. // draw again for removing flowed elements and reverting attr
  3474. xgrid.attr('transform', null).attr($$.xgridAttr);
  3475. xgridLines.attr('transform', null);
  3476. xgridLines.select('line').attr("x1", config.axis_rotated ? 0 : xv).attr("x2", config.axis_rotated ? $$.width : xv);
  3477. xgridLines.select('text').attr("x", config.axis_rotated ? $$.width : 0).attr("y", xv);
  3478. mainBar.attr('transform', null).attr("d", drawBar);
  3479. mainLine.attr('transform', null).attr("d", drawLine);
  3480. mainArea.attr('transform', null).attr("d", drawArea);
  3481. mainCircle.attr('transform', null).attr("cx", cx).attr("cy", cy);
  3482. mainText.attr('transform', null).attr('x', xForText).attr('y', yForText).style('fill-opacity', $$.opacityForText.bind($$));
  3483. mainRegion.attr('transform', null);
  3484. mainRegion.select('rect').filter($$.isRegionOnX).attr("x", $$.regionX.bind($$)).attr("width", $$.regionWidth.bind($$));
  3485. if (config.interaction_enabled) {
  3486. $$.redrawEventRect();
  3487. }
  3488. // callback for end of flow
  3489. done();
  3490. $$.flowing = false;
  3491. });
  3492. };
  3493. };
  3494. c3_chart_fn.focus = function (targetIds) {
  3495. var $$ = this.internal,
  3496. candidates;
  3497. targetIds = $$.mapToTargetIds(targetIds);
  3498. candidates = $$.svg.selectAll($$.selectorTargets(targetIds.filter($$.isTargetToShow, $$))), this.revert();
  3499. this.defocus();
  3500. candidates.classed(CLASS.focused, true).classed(CLASS.defocused, false);
  3501. if ($$.hasArcType()) {
  3502. $$.expandArc(targetIds);
  3503. }
  3504. $$.toggleFocusLegend(targetIds, true);
  3505. $$.focusedTargetIds = targetIds;
  3506. $$.defocusedTargetIds = $$.defocusedTargetIds.filter(function (id) {
  3507. return targetIds.indexOf(id) < 0;
  3508. });
  3509. };
  3510. c3_chart_fn.defocus = function (targetIds) {
  3511. var $$ = this.internal,
  3512. candidates;
  3513. targetIds = $$.mapToTargetIds(targetIds);
  3514. candidates = $$.svg.selectAll($$.selectorTargets(targetIds.filter($$.isTargetToShow, $$))), candidates.classed(CLASS.focused, false).classed(CLASS.defocused, true);
  3515. if ($$.hasArcType()) {
  3516. $$.unexpandArc(targetIds);
  3517. }
  3518. $$.toggleFocusLegend(targetIds, false);
  3519. $$.focusedTargetIds = $$.focusedTargetIds.filter(function (id) {
  3520. return targetIds.indexOf(id) < 0;
  3521. });
  3522. $$.defocusedTargetIds = targetIds;
  3523. };
  3524. c3_chart_fn.revert = function (targetIds) {
  3525. var $$ = this.internal,
  3526. candidates;
  3527. targetIds = $$.mapToTargetIds(targetIds);
  3528. candidates = $$.svg.selectAll($$.selectorTargets(targetIds)); // should be for all targets
  3529. candidates.classed(CLASS.focused, false).classed(CLASS.defocused, false);
  3530. if ($$.hasArcType()) {
  3531. $$.unexpandArc(targetIds);
  3532. }
  3533. if ($$.config.legend_show) {
  3534. $$.showLegend(targetIds.filter($$.isLegendToShow.bind($$)));
  3535. $$.legend.selectAll($$.selectorLegends(targetIds)).filter(function () {
  3536. return $$.d3.select(this).classed(CLASS.legendItemFocused);
  3537. }).classed(CLASS.legendItemFocused, false);
  3538. }
  3539. $$.focusedTargetIds = [];
  3540. $$.defocusedTargetIds = [];
  3541. };
  3542. c3_chart_fn.xgrids = function (grids) {
  3543. var $$ = this.internal,
  3544. config = $$.config;
  3545. if (!grids) {
  3546. return config.grid_x_lines;
  3547. }
  3548. config.grid_x_lines = grids;
  3549. $$.redrawWithoutRescale();
  3550. return config.grid_x_lines;
  3551. };
  3552. c3_chart_fn.xgrids.add = function (grids) {
  3553. var $$ = this.internal;
  3554. return this.xgrids($$.config.grid_x_lines.concat(grids ? grids : []));
  3555. };
  3556. c3_chart_fn.xgrids.remove = function (params) {
  3557. // TODO: multiple
  3558. var $$ = this.internal;
  3559. $$.removeGridLines(params, true);
  3560. };
  3561. c3_chart_fn.ygrids = function (grids) {
  3562. var $$ = this.internal,
  3563. config = $$.config;
  3564. if (!grids) {
  3565. return config.grid_y_lines;
  3566. }
  3567. config.grid_y_lines = grids;
  3568. $$.redrawWithoutRescale();
  3569. return config.grid_y_lines;
  3570. };
  3571. c3_chart_fn.ygrids.add = function (grids) {
  3572. var $$ = this.internal;
  3573. return this.ygrids($$.config.grid_y_lines.concat(grids ? grids : []));
  3574. };
  3575. c3_chart_fn.ygrids.remove = function (params) {
  3576. // TODO: multiple
  3577. var $$ = this.internal;
  3578. $$.removeGridLines(params, false);
  3579. };
  3580. c3_chart_fn.groups = function (groups) {
  3581. var $$ = this.internal,
  3582. config = $$.config;
  3583. if (isUndefined(groups)) {
  3584. return config.data_groups;
  3585. }
  3586. config.data_groups = groups;
  3587. $$.redraw();
  3588. return config.data_groups;
  3589. };
  3590. c3_chart_fn.legend = function () {};
  3591. c3_chart_fn.legend.show = function (targetIds) {
  3592. var $$ = this.internal;
  3593. $$.showLegend($$.mapToTargetIds(targetIds));
  3594. $$.updateAndRedraw({ withLegend: true });
  3595. };
  3596. c3_chart_fn.legend.hide = function (targetIds) {
  3597. var $$ = this.internal;
  3598. $$.hideLegend($$.mapToTargetIds(targetIds));
  3599. $$.updateAndRedraw({ withLegend: true });
  3600. };
  3601. c3_chart_fn.load = function (args) {
  3602. var $$ = this.internal,
  3603. config = $$.config;
  3604. // update xs if specified
  3605. if (args.xs) {
  3606. $$.addXs(args.xs);
  3607. }
  3608. // update names if exists
  3609. if ('names' in args) {
  3610. c3_chart_fn.data.names.bind(this)(args.names);
  3611. }
  3612. // update classes if exists
  3613. if ('classes' in args) {
  3614. Object.keys(args.classes).forEach(function (id) {
  3615. config.data_classes[id] = args.classes[id];
  3616. });
  3617. }
  3618. // update categories if exists
  3619. if ('categories' in args && $$.isCategorized()) {
  3620. config.axis_x_categories = args.categories;
  3621. }
  3622. // update axes if exists
  3623. if ('axes' in args) {
  3624. Object.keys(args.axes).forEach(function (id) {
  3625. config.data_axes[id] = args.axes[id];
  3626. });
  3627. }
  3628. // update colors if exists
  3629. if ('colors' in args) {
  3630. Object.keys(args.colors).forEach(function (id) {
  3631. config.data_colors[id] = args.colors[id];
  3632. });
  3633. }
  3634. // use cache if exists
  3635. if ('cacheIds' in args && $$.hasCaches(args.cacheIds)) {
  3636. $$.load($$.getCaches(args.cacheIds), args.done);
  3637. return;
  3638. }
  3639. // unload if needed
  3640. if ('unload' in args) {
  3641. // TODO: do not unload if target will load (included in url/rows/columns)
  3642. $$.unload($$.mapToTargetIds(typeof args.unload === 'boolean' && args.unload ? null : args.unload), function () {
  3643. $$.loadFromArgs(args);
  3644. });
  3645. } else {
  3646. $$.loadFromArgs(args);
  3647. }
  3648. };
  3649. c3_chart_fn.unload = function (args) {
  3650. var $$ = this.internal;
  3651. args = args || {};
  3652. if (args instanceof Array) {
  3653. args = { ids: args };
  3654. } else if (typeof args === 'string') {
  3655. args = { ids: [args] };
  3656. }
  3657. $$.unload($$.mapToTargetIds(args.ids), function () {
  3658. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true, withLegend: true });
  3659. if (args.done) {
  3660. args.done();
  3661. }
  3662. });
  3663. };
  3664. c3_chart_fn.regions = function (regions) {
  3665. var $$ = this.internal,
  3666. config = $$.config;
  3667. if (!regions) {
  3668. return config.regions;
  3669. }
  3670. config.regions = regions;
  3671. $$.redrawWithoutRescale();
  3672. return config.regions;
  3673. };
  3674. c3_chart_fn.regions.add = function (regions) {
  3675. var $$ = this.internal,
  3676. config = $$.config;
  3677. if (!regions) {
  3678. return config.regions;
  3679. }
  3680. config.regions = config.regions.concat(regions);
  3681. $$.redrawWithoutRescale();
  3682. return config.regions;
  3683. };
  3684. c3_chart_fn.regions.remove = function (options) {
  3685. var $$ = this.internal,
  3686. config = $$.config,
  3687. duration,
  3688. classes,
  3689. regions;
  3690. options = options || {};
  3691. duration = $$.getOption(options, "duration", config.transition_duration);
  3692. classes = $$.getOption(options, "classes", [CLASS.region]);
  3693. regions = $$.main.select('.' + CLASS.regions).selectAll(classes.map(function (c) {
  3694. return '.' + c;
  3695. }));
  3696. (duration ? regions.transition().duration(duration) : regions).style('opacity', 0).remove();
  3697. config.regions = config.regions.filter(function (region) {
  3698. var found = false;
  3699. if (!region['class']) {
  3700. return true;
  3701. }
  3702. region['class'].split(' ').forEach(function (c) {
  3703. if (classes.indexOf(c) >= 0) {
  3704. found = true;
  3705. }
  3706. });
  3707. return !found;
  3708. });
  3709. return config.regions;
  3710. };
  3711. c3_chart_fn.selected = function (targetId) {
  3712. var $$ = this.internal,
  3713. d3 = $$.d3;
  3714. return d3.merge($$.main.selectAll('.' + CLASS.shapes + $$.getTargetSelectorSuffix(targetId)).selectAll('.' + CLASS.shape).filter(function () {
  3715. return d3.select(this).classed(CLASS.SELECTED);
  3716. }).map(function (d) {
  3717. return d.map(function (d) {
  3718. var data = d.__data__;return data.data ? data.data : data;
  3719. });
  3720. }));
  3721. };
  3722. c3_chart_fn.select = function (ids, indices, resetOther) {
  3723. var $$ = this.internal,
  3724. d3 = $$.d3,
  3725. config = $$.config;
  3726. if (!config.data_selection_enabled) {
  3727. return;
  3728. }
  3729. $$.main.selectAll('.' + CLASS.shapes).selectAll('.' + CLASS.shape).each(function (d, i) {
  3730. var shape = d3.select(this),
  3731. id = d.data ? d.data.id : d.id,
  3732. toggle = $$.getToggle(this, d).bind($$),
  3733. isTargetId = config.data_selection_grouped || !ids || ids.indexOf(id) >= 0,
  3734. isTargetIndex = !indices || indices.indexOf(i) >= 0,
  3735. isSelected = shape.classed(CLASS.SELECTED);
  3736. // line/area selection not supported yet
  3737. if (shape.classed(CLASS.line) || shape.classed(CLASS.area)) {
  3738. return;
  3739. }
  3740. if (isTargetId && isTargetIndex) {
  3741. if (config.data_selection_isselectable(d) && !isSelected) {
  3742. toggle(true, shape.classed(CLASS.SELECTED, true), d, i);
  3743. }
  3744. } else if (isDefined(resetOther) && resetOther) {
  3745. if (isSelected) {
  3746. toggle(false, shape.classed(CLASS.SELECTED, false), d, i);
  3747. }
  3748. }
  3749. });
  3750. };
  3751. c3_chart_fn.unselect = function (ids, indices) {
  3752. var $$ = this.internal,
  3753. d3 = $$.d3,
  3754. config = $$.config;
  3755. if (!config.data_selection_enabled) {
  3756. return;
  3757. }
  3758. $$.main.selectAll('.' + CLASS.shapes).selectAll('.' + CLASS.shape).each(function (d, i) {
  3759. var shape = d3.select(this),
  3760. id = d.data ? d.data.id : d.id,
  3761. toggle = $$.getToggle(this, d).bind($$),
  3762. isTargetId = config.data_selection_grouped || !ids || ids.indexOf(id) >= 0,
  3763. isTargetIndex = !indices || indices.indexOf(i) >= 0,
  3764. isSelected = shape.classed(CLASS.SELECTED);
  3765. // line/area selection not supported yet
  3766. if (shape.classed(CLASS.line) || shape.classed(CLASS.area)) {
  3767. return;
  3768. }
  3769. if (isTargetId && isTargetIndex) {
  3770. if (config.data_selection_isselectable(d)) {
  3771. if (isSelected) {
  3772. toggle(false, shape.classed(CLASS.SELECTED, false), d, i);
  3773. }
  3774. }
  3775. }
  3776. });
  3777. };
  3778. c3_chart_fn.show = function (targetIds, options) {
  3779. var $$ = this.internal,
  3780. targets;
  3781. targetIds = $$.mapToTargetIds(targetIds);
  3782. options = options || {};
  3783. $$.removeHiddenTargetIds(targetIds);
  3784. targets = $$.svg.selectAll($$.selectorTargets(targetIds));
  3785. targets.transition().style('opacity', 1, 'important').call($$.endall, function () {
  3786. targets.style('opacity', null).style('opacity', 1);
  3787. });
  3788. if (options.withLegend) {
  3789. $$.showLegend(targetIds);
  3790. }
  3791. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true, withLegend: true });
  3792. };
  3793. c3_chart_fn.hide = function (targetIds, options) {
  3794. var $$ = this.internal,
  3795. targets;
  3796. targetIds = $$.mapToTargetIds(targetIds);
  3797. options = options || {};
  3798. $$.addHiddenTargetIds(targetIds);
  3799. targets = $$.svg.selectAll($$.selectorTargets(targetIds));
  3800. targets.transition().style('opacity', 0, 'important').call($$.endall, function () {
  3801. targets.style('opacity', null).style('opacity', 0);
  3802. });
  3803. if (options.withLegend) {
  3804. $$.hideLegend(targetIds);
  3805. }
  3806. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true, withLegend: true });
  3807. };
  3808. c3_chart_fn.toggle = function (targetIds, options) {
  3809. var that = this,
  3810. $$ = this.internal;
  3811. $$.mapToTargetIds(targetIds).forEach(function (targetId) {
  3812. $$.isTargetToShow(targetId) ? that.hide(targetId, options) : that.show(targetId, options);
  3813. });
  3814. };
  3815. c3_chart_fn.tooltip = function () {};
  3816. c3_chart_fn.tooltip.show = function (args) {
  3817. var $$ = this.internal,
  3818. index,
  3819. mouse;
  3820. // determine mouse position on the chart
  3821. if (args.mouse) {
  3822. mouse = args.mouse;
  3823. }
  3824. // determine focus data
  3825. if (args.data) {
  3826. if ($$.isMultipleX()) {
  3827. // if multiple xs, target point will be determined by mouse
  3828. mouse = [$$.x(args.data.x), $$.getYScale(args.data.id)(args.data.value)];
  3829. index = null;
  3830. } else {
  3831. // TODO: when tooltip_grouped = false
  3832. index = isValue(args.data.index) ? args.data.index : $$.getIndexByX(args.data.x);
  3833. }
  3834. } else if (typeof args.x !== 'undefined') {
  3835. index = $$.getIndexByX(args.x);
  3836. } else if (typeof args.index !== 'undefined') {
  3837. index = args.index;
  3838. }
  3839. // emulate mouse events to show
  3840. $$.dispatchEvent('mouseover', index, mouse);
  3841. $$.dispatchEvent('mousemove', index, mouse);
  3842. $$.config.tooltip_onshow.call($$, args.data);
  3843. };
  3844. c3_chart_fn.tooltip.hide = function () {
  3845. // TODO: get target data by checking the state of focus
  3846. this.internal.dispatchEvent('mouseout', 0);
  3847. this.internal.config.tooltip_onhide.call(this);
  3848. };
  3849. c3_chart_fn.transform = function (type, targetIds) {
  3850. var $$ = this.internal,
  3851. options = ['pie', 'donut'].indexOf(type) >= 0 ? { withTransform: true } : null;
  3852. $$.transformTo(targetIds, type, options);
  3853. };
  3854. c3_chart_internal_fn.transformTo = function (targetIds, type, optionsForRedraw) {
  3855. var $$ = this,
  3856. withTransitionForAxis = !$$.hasArcType(),
  3857. options = optionsForRedraw || { withTransitionForAxis: withTransitionForAxis };
  3858. options.withTransitionForTransform = false;
  3859. $$.transiting = false;
  3860. $$.setTargetType(targetIds, type);
  3861. $$.updateTargets($$.data.targets); // this is needed when transforming to arc
  3862. $$.updateAndRedraw(options);
  3863. };
  3864. c3_chart_fn.x = function (x) {
  3865. var $$ = this.internal;
  3866. if (arguments.length) {
  3867. $$.updateTargetX($$.data.targets, x);
  3868. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true });
  3869. }
  3870. return $$.data.xs;
  3871. };
  3872. c3_chart_fn.xs = function (xs) {
  3873. var $$ = this.internal;
  3874. if (arguments.length) {
  3875. $$.updateTargetXs($$.data.targets, xs);
  3876. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true });
  3877. }
  3878. return $$.data.xs;
  3879. };
  3880. c3_chart_fn.zoom = function (domain) {
  3881. var $$ = this.internal;
  3882. if (domain) {
  3883. if ($$.isTimeSeries()) {
  3884. domain = domain.map(function (x) {
  3885. return $$.parseDate(x);
  3886. });
  3887. }
  3888. $$.brush.extent(domain);
  3889. $$.redraw({ withUpdateXDomain: true, withY: $$.config.zoom_rescale });
  3890. $$.config.zoom_onzoom.call(this, $$.x.orgDomain());
  3891. }
  3892. return $$.brush.extent();
  3893. };
  3894. c3_chart_fn.zoom.enable = function (enabled) {
  3895. var $$ = this.internal;
  3896. $$.config.zoom_enabled = enabled;
  3897. $$.updateAndRedraw();
  3898. };
  3899. c3_chart_fn.unzoom = function () {
  3900. var $$ = this.internal;
  3901. $$.brush.clear().update();
  3902. $$.redraw({ withUpdateXDomain: true });
  3903. };
  3904. c3_chart_fn.zoom.max = function (max) {
  3905. var $$ = this.internal,
  3906. config = $$.config,
  3907. d3 = $$.d3;
  3908. if (max === 0 || max) {
  3909. config.zoom_x_max = d3.max([$$.orgXDomain[1], max]);
  3910. } else {
  3911. return config.zoom_x_max;
  3912. }
  3913. };
  3914. c3_chart_fn.zoom.min = function (min) {
  3915. var $$ = this.internal,
  3916. config = $$.config,
  3917. d3 = $$.d3;
  3918. if (min === 0 || min) {
  3919. config.zoom_x_min = d3.min([$$.orgXDomain[0], min]);
  3920. } else {
  3921. return config.zoom_x_min;
  3922. }
  3923. };
  3924. c3_chart_fn.zoom.range = function (range) {
  3925. if (arguments.length) {
  3926. if (isDefined(range.max)) {
  3927. this.domain.max(range.max);
  3928. }
  3929. if (isDefined(range.min)) {
  3930. this.domain.min(range.min);
  3931. }
  3932. } else {
  3933. return {
  3934. max: this.domain.max(),
  3935. min: this.domain.min()
  3936. };
  3937. }
  3938. };
  3939. c3_chart_internal_fn.initPie = function () {
  3940. var $$ = this,
  3941. d3 = $$.d3;
  3942. $$.pie = d3.layout.pie().value(function (d) {
  3943. return d.values.reduce(function (a, b) {
  3944. return a + b.value;
  3945. }, 0);
  3946. });
  3947. $$.pie.sort($$.getOrderFunction() || null);
  3948. };
  3949. c3_chart_internal_fn.updateRadius = function () {
  3950. var $$ = this,
  3951. config = $$.config,
  3952. w = config.gauge_width || config.donut_width,
  3953. gaugeArcWidth = $$.filterTargetsToShow($$.data.targets).length * $$.config.gauge_arcs_minWidth;
  3954. $$.radiusExpanded = Math.min($$.arcWidth, $$.arcHeight) / 2 * ($$.hasType('gauge') ? 0.85 : 1);
  3955. $$.radius = $$.radiusExpanded * 0.95;
  3956. $$.innerRadiusRatio = w ? ($$.radius - w) / $$.radius : 0.6;
  3957. $$.innerRadius = $$.hasType('donut') || $$.hasType('gauge') ? $$.radius * $$.innerRadiusRatio : 0;
  3958. $$.gaugeArcWidth = w ? w : gaugeArcWidth <= $$.radius - $$.innerRadius ? $$.radius - $$.innerRadius : gaugeArcWidth <= $$.radius ? gaugeArcWidth : $$.radius;
  3959. };
  3960. c3_chart_internal_fn.updateArc = function () {
  3961. var $$ = this;
  3962. $$.svgArc = $$.getSvgArc();
  3963. $$.svgArcExpanded = $$.getSvgArcExpanded();
  3964. $$.svgArcExpandedSub = $$.getSvgArcExpanded(0.98);
  3965. };
  3966. c3_chart_internal_fn.updateAngle = function (d) {
  3967. var $$ = this,
  3968. config = $$.config,
  3969. found = false,
  3970. index = 0,
  3971. gMin,
  3972. gMax,
  3973. gTic,
  3974. gValue;
  3975. if (!config) {
  3976. return null;
  3977. }
  3978. $$.pie($$.filterTargetsToShow($$.data.targets)).forEach(function (t) {
  3979. if (!found && t.data.id === d.data.id) {
  3980. found = true;
  3981. d = t;
  3982. d.index = index;
  3983. }
  3984. index++;
  3985. });
  3986. if (isNaN(d.startAngle)) {
  3987. d.startAngle = 0;
  3988. }
  3989. if (isNaN(d.endAngle)) {
  3990. d.endAngle = d.startAngle;
  3991. }
  3992. if ($$.isGaugeType(d.data)) {
  3993. gMin = config.gauge_min;
  3994. gMax = config.gauge_max;
  3995. gTic = Math.PI * (config.gauge_fullCircle ? 2 : 1) / (gMax - gMin);
  3996. gValue = d.value < gMin ? 0 : d.value < gMax ? d.value - gMin : gMax - gMin;
  3997. d.startAngle = config.gauge_startingAngle;
  3998. d.endAngle = d.startAngle + gTic * gValue;
  3999. }
  4000. return found ? d : null;
  4001. };
  4002. c3_chart_internal_fn.getSvgArc = function () {
  4003. var $$ = this,
  4004. hasGaugeType = $$.hasType('gauge'),
  4005. singleArcWidth = $$.gaugeArcWidth / $$.filterTargetsToShow($$.data.targets).length,
  4006. arc = $$.d3.svg.arc().outerRadius(function (d) {
  4007. return hasGaugeType ? $$.radius - singleArcWidth * d.index : $$.radius;
  4008. }).innerRadius(function (d) {
  4009. return hasGaugeType ? $$.radius - singleArcWidth * (d.index + 1) : $$.innerRadius;
  4010. }),
  4011. newArc = function newArc(d, withoutUpdate) {
  4012. var updated;
  4013. if (withoutUpdate) {
  4014. return arc(d);
  4015. } // for interpolate
  4016. updated = $$.updateAngle(d);
  4017. return updated ? arc(updated) : "M 0 0";
  4018. };
  4019. // TODO: extends all function
  4020. newArc.centroid = arc.centroid;
  4021. return newArc;
  4022. };
  4023. c3_chart_internal_fn.getSvgArcExpanded = function (rate) {
  4024. rate = rate || 1;
  4025. var $$ = this,
  4026. hasGaugeType = $$.hasType('gauge'),
  4027. singleArcWidth = $$.gaugeArcWidth / $$.filterTargetsToShow($$.data.targets).length,
  4028. expandWidth = Math.min($$.radiusExpanded * rate - $$.radius, singleArcWidth * 0.8 - (1 - rate) * 100),
  4029. arc = $$.d3.svg.arc().outerRadius(function (d) {
  4030. return hasGaugeType ? $$.radius - singleArcWidth * d.index + expandWidth : $$.radiusExpanded * rate;
  4031. }).innerRadius(function (d) {
  4032. return hasGaugeType ? $$.radius - singleArcWidth * (d.index + 1) : $$.innerRadius;
  4033. });
  4034. return function (d) {
  4035. var updated = $$.updateAngle(d);
  4036. return updated ? arc(updated) : "M 0 0";
  4037. };
  4038. };
  4039. c3_chart_internal_fn.getArc = function (d, withoutUpdate, force) {
  4040. return force || this.isArcType(d.data) ? this.svgArc(d, withoutUpdate) : "M 0 0";
  4041. };
  4042. c3_chart_internal_fn.transformForArcLabel = function (d) {
  4043. var $$ = this,
  4044. config = $$.config,
  4045. updated = $$.updateAngle(d),
  4046. c,
  4047. x,
  4048. y,
  4049. h,
  4050. ratio,
  4051. translate = "",
  4052. hasGauge = $$.hasType('gauge');
  4053. if (updated && !hasGauge) {
  4054. c = this.svgArc.centroid(updated);
  4055. x = isNaN(c[0]) ? 0 : c[0];
  4056. y = isNaN(c[1]) ? 0 : c[1];
  4057. h = Math.sqrt(x * x + y * y);
  4058. if ($$.hasType('donut') && config.donut_label_ratio) {
  4059. ratio = isFunction(config.donut_label_ratio) ? config.donut_label_ratio(d, $$.radius, h) : config.donut_label_ratio;
  4060. } else if ($$.hasType('pie') && config.pie_label_ratio) {
  4061. ratio = isFunction(config.pie_label_ratio) ? config.pie_label_ratio(d, $$.radius, h) : config.pie_label_ratio;
  4062. } else {
  4063. ratio = $$.radius && h ? (36 / $$.radius > 0.375 ? 1.175 - 36 / $$.radius : 0.8) * $$.radius / h : 0;
  4064. }
  4065. translate = "translate(" + x * ratio + ',' + y * ratio + ")";
  4066. } else if (updated && hasGauge && $$.filterTargetsToShow($$.data.targets).length > 1) {
  4067. var y1 = Math.sin(updated.endAngle - Math.PI / 2);
  4068. x = Math.cos(updated.endAngle - Math.PI / 2) * ($$.radiusExpanded + 25);
  4069. y = y1 * ($$.radiusExpanded + 15 - Math.abs(y1 * 10)) + 3;
  4070. translate = "translate(" + x + ',' + y + ")";
  4071. }
  4072. return translate;
  4073. };
  4074. c3_chart_internal_fn.getArcRatio = function (d) {
  4075. var $$ = this,
  4076. config = $$.config,
  4077. whole = Math.PI * ($$.hasType('gauge') && !config.gauge_fullCircle ? 1 : 2);
  4078. return d ? (d.endAngle - d.startAngle) / whole : null;
  4079. };
  4080. c3_chart_internal_fn.convertToArcData = function (d) {
  4081. return this.addName({
  4082. id: d.data.id,
  4083. value: d.value,
  4084. ratio: this.getArcRatio(d),
  4085. index: d.index
  4086. });
  4087. };
  4088. c3_chart_internal_fn.textForArcLabel = function (d) {
  4089. var $$ = this,
  4090. updated,
  4091. value,
  4092. ratio,
  4093. id,
  4094. format;
  4095. if (!$$.shouldShowArcLabel()) {
  4096. return "";
  4097. }
  4098. updated = $$.updateAngle(d);
  4099. value = updated ? updated.value : null;
  4100. ratio = $$.getArcRatio(updated);
  4101. id = d.data.id;
  4102. if (!$$.hasType('gauge') && !$$.meetsArcLabelThreshold(ratio)) {
  4103. return "";
  4104. }
  4105. format = $$.getArcLabelFormat();
  4106. return format ? format(value, ratio, id) : $$.defaultArcValueFormat(value, ratio);
  4107. };
  4108. c3_chart_internal_fn.textForGaugeMinMax = function (value, isMax) {
  4109. var $$ = this,
  4110. format = $$.getGaugeLabelExtents();
  4111. return format ? format(value, isMax) : value;
  4112. };
  4113. c3_chart_internal_fn.expandArc = function (targetIds) {
  4114. var $$ = this,
  4115. interval;
  4116. // MEMO: avoid to cancel transition
  4117. if ($$.transiting) {
  4118. interval = window.setInterval(function () {
  4119. if (!$$.transiting) {
  4120. window.clearInterval(interval);
  4121. if ($$.legend.selectAll('.c3-legend-item-focused').size() > 0) {
  4122. $$.expandArc(targetIds);
  4123. }
  4124. }
  4125. }, 10);
  4126. return;
  4127. }
  4128. targetIds = $$.mapToTargetIds(targetIds);
  4129. $$.svg.selectAll($$.selectorTargets(targetIds, '.' + CLASS.chartArc)).each(function (d) {
  4130. if (!$$.shouldExpand(d.data.id)) {
  4131. return;
  4132. }
  4133. $$.d3.select(this).selectAll('path').transition().duration($$.expandDuration(d.data.id)).attr("d", $$.svgArcExpanded).transition().duration($$.expandDuration(d.data.id) * 2).attr("d", $$.svgArcExpandedSub).each(function (d) {
  4134. if ($$.isDonutType(d.data)) {
  4135. // callback here
  4136. }
  4137. });
  4138. });
  4139. };
  4140. c3_chart_internal_fn.unexpandArc = function (targetIds) {
  4141. var $$ = this;
  4142. if ($$.transiting) {
  4143. return;
  4144. }
  4145. targetIds = $$.mapToTargetIds(targetIds);
  4146. $$.svg.selectAll($$.selectorTargets(targetIds, '.' + CLASS.chartArc)).selectAll('path').transition().duration(function (d) {
  4147. return $$.expandDuration(d.data.id);
  4148. }).attr("d", $$.svgArc);
  4149. $$.svg.selectAll('.' + CLASS.arc);
  4150. };
  4151. c3_chart_internal_fn.expandDuration = function (id) {
  4152. var $$ = this,
  4153. config = $$.config;
  4154. if ($$.isDonutType(id)) {
  4155. return config.donut_expand_duration;
  4156. } else if ($$.isGaugeType(id)) {
  4157. return config.gauge_expand_duration;
  4158. } else if ($$.isPieType(id)) {
  4159. return config.pie_expand_duration;
  4160. } else {
  4161. return 50;
  4162. }
  4163. };
  4164. c3_chart_internal_fn.shouldExpand = function (id) {
  4165. var $$ = this,
  4166. config = $$.config;
  4167. return $$.isDonutType(id) && config.donut_expand || $$.isGaugeType(id) && config.gauge_expand || $$.isPieType(id) && config.pie_expand;
  4168. };
  4169. c3_chart_internal_fn.shouldShowArcLabel = function () {
  4170. var $$ = this,
  4171. config = $$.config,
  4172. shouldShow = true;
  4173. if ($$.hasType('donut')) {
  4174. shouldShow = config.donut_label_show;
  4175. } else if ($$.hasType('pie')) {
  4176. shouldShow = config.pie_label_show;
  4177. }
  4178. // when gauge, always true
  4179. return shouldShow;
  4180. };
  4181. c3_chart_internal_fn.meetsArcLabelThreshold = function (ratio) {
  4182. var $$ = this,
  4183. config = $$.config,
  4184. threshold = $$.hasType('donut') ? config.donut_label_threshold : config.pie_label_threshold;
  4185. return ratio >= threshold;
  4186. };
  4187. c3_chart_internal_fn.getArcLabelFormat = function () {
  4188. var $$ = this,
  4189. config = $$.config,
  4190. format = config.pie_label_format;
  4191. if ($$.hasType('gauge')) {
  4192. format = config.gauge_label_format;
  4193. } else if ($$.hasType('donut')) {
  4194. format = config.donut_label_format;
  4195. }
  4196. return format;
  4197. };
  4198. c3_chart_internal_fn.getGaugeLabelExtents = function () {
  4199. var $$ = this,
  4200. config = $$.config;
  4201. return config.gauge_label_extents;
  4202. };
  4203. c3_chart_internal_fn.getArcTitle = function () {
  4204. var $$ = this;
  4205. return $$.hasType('donut') ? $$.config.donut_title : "";
  4206. };
  4207. c3_chart_internal_fn.updateTargetsForArc = function (targets) {
  4208. var $$ = this,
  4209. main = $$.main,
  4210. mainPieUpdate,
  4211. mainPieEnter,
  4212. classChartArc = $$.classChartArc.bind($$),
  4213. classArcs = $$.classArcs.bind($$),
  4214. classFocus = $$.classFocus.bind($$);
  4215. mainPieUpdate = main.select('.' + CLASS.chartArcs).selectAll('.' + CLASS.chartArc).data($$.pie(targets)).attr("class", function (d) {
  4216. return classChartArc(d) + classFocus(d.data);
  4217. });
  4218. mainPieEnter = mainPieUpdate.enter().append("g").attr("class", classChartArc);
  4219. mainPieEnter.append('g').attr('class', classArcs);
  4220. mainPieEnter.append("text").attr("dy", $$.hasType('gauge') ? "-.1em" : ".35em").style("opacity", 0).style("text-anchor", "middle").style("pointer-events", "none");
  4221. // MEMO: can not keep same color..., but not bad to update color in redraw
  4222. //mainPieUpdate.exit().remove();
  4223. };
  4224. c3_chart_internal_fn.initArc = function () {
  4225. var $$ = this;
  4226. $$.arcs = $$.main.select('.' + CLASS.chart).append("g").attr("class", CLASS.chartArcs).attr("transform", $$.getTranslate('arc'));
  4227. $$.arcs.append('text').attr('class', CLASS.chartArcsTitle).style("text-anchor", "middle").text($$.getArcTitle());
  4228. };
  4229. c3_chart_internal_fn.redrawArc = function (duration, durationForExit, withTransform) {
  4230. var $$ = this,
  4231. d3 = $$.d3,
  4232. config = $$.config,
  4233. main = $$.main,
  4234. mainArc,
  4235. backgroundArc,
  4236. mainArcLabelLine,
  4237. hasGaugeType = $$.hasType('gauge');
  4238. mainArc = main.selectAll('.' + CLASS.arcs).selectAll('.' + CLASS.arc).data($$.arcData.bind($$));
  4239. mainArc.enter().append('path').attr("class", $$.classArc.bind($$)).style("fill", function (d) {
  4240. return $$.color(d.data);
  4241. }).style("cursor", function (d) {
  4242. return config.interaction_enabled && config.data_selection_isselectable(d) ? "pointer" : null;
  4243. }).each(function (d) {
  4244. if ($$.isGaugeType(d.data)) {
  4245. d.startAngle = d.endAngle = config.gauge_startingAngle;
  4246. }
  4247. this._current = d;
  4248. });
  4249. if (hasGaugeType) {
  4250. mainArcLabelLine = main.selectAll('.' + CLASS.arcs).selectAll('.' + CLASS.arcLabelLine).data($$.arcData.bind($$));
  4251. mainArcLabelLine.enter().append('rect').attr("class", function (d) {
  4252. return CLASS.arcLabelLine + ' ' + CLASS.target + ' ' + CLASS.target + '-' + d.data.id;
  4253. });
  4254. if ($$.filterTargetsToShow($$.data.targets).length === 1) {
  4255. mainArcLabelLine.style("display", "none");
  4256. } else {
  4257. mainArcLabelLine.style("fill", function (d) {
  4258. return config.color_pattern.length > 0 ? $$.levelColor(d.data.values[0].value) : $$.color(d.data);
  4259. }).style("display", config.gauge_labelLine_show ? "" : "none").each(function (d) {
  4260. var lineLength = 0,
  4261. lineThickness = 2,
  4262. x = 0,
  4263. y = 0,
  4264. transform = "";
  4265. if ($$.hiddenTargetIds.indexOf(d.data.id) < 0) {
  4266. var updated = $$.updateAngle(d),
  4267. innerLineLength = $$.gaugeArcWidth / $$.filterTargetsToShow($$.data.targets).length * (updated.index + 1),
  4268. lineAngle = updated.endAngle - Math.PI / 2,
  4269. arcInnerRadius = $$.radius - innerLineLength,
  4270. linePositioningAngle = lineAngle - (arcInnerRadius === 0 ? 0 : 1 / arcInnerRadius);
  4271. lineLength = $$.radiusExpanded - $$.radius + innerLineLength;
  4272. x = Math.cos(linePositioningAngle) * arcInnerRadius;
  4273. y = Math.sin(linePositioningAngle) * arcInnerRadius;
  4274. transform = "rotate(" + lineAngle * 180 / Math.PI + ", " + x + ", " + y + ")";
  4275. }
  4276. d3.select(this).attr({ x: x, y: y, width: lineLength, height: lineThickness, transform: transform }).style("stroke-dasharray", "0, " + (lineLength + lineThickness) + ", 0");
  4277. });
  4278. }
  4279. }
  4280. mainArc.attr("transform", function (d) {
  4281. return !$$.isGaugeType(d.data) && withTransform ? "scale(0)" : "";
  4282. }).on('mouseover', config.interaction_enabled ? function (d) {
  4283. var updated, arcData;
  4284. if ($$.transiting) {
  4285. // skip while transiting
  4286. return;
  4287. }
  4288. updated = $$.updateAngle(d);
  4289. if (updated) {
  4290. arcData = $$.convertToArcData(updated);
  4291. // transitions
  4292. $$.expandArc(updated.data.id);
  4293. $$.api.focus(updated.data.id);
  4294. $$.toggleFocusLegend(updated.data.id, true);
  4295. $$.config.data_onmouseover(arcData, this);
  4296. }
  4297. } : null).on('mousemove', config.interaction_enabled ? function (d) {
  4298. var updated = $$.updateAngle(d),
  4299. arcData,
  4300. selectedData;
  4301. if (updated) {
  4302. arcData = $$.convertToArcData(updated), selectedData = [arcData];
  4303. $$.showTooltip(selectedData, this);
  4304. }
  4305. } : null).on('mouseout', config.interaction_enabled ? function (d) {
  4306. var updated, arcData;
  4307. if ($$.transiting) {
  4308. // skip while transiting
  4309. return;
  4310. }
  4311. updated = $$.updateAngle(d);
  4312. if (updated) {
  4313. arcData = $$.convertToArcData(updated);
  4314. // transitions
  4315. $$.unexpandArc(updated.data.id);
  4316. $$.api.revert();
  4317. $$.revertLegend();
  4318. $$.hideTooltip();
  4319. $$.config.data_onmouseout(arcData, this);
  4320. }
  4321. } : null).on('click', config.interaction_enabled ? function (d, i) {
  4322. var updated = $$.updateAngle(d),
  4323. arcData;
  4324. if (updated) {
  4325. arcData = $$.convertToArcData(updated);
  4326. if ($$.toggleShape) {
  4327. $$.toggleShape(this, arcData, i);
  4328. }
  4329. $$.config.data_onclick.call($$.api, arcData, this);
  4330. }
  4331. } : null).each(function () {
  4332. $$.transiting = true;
  4333. }).transition().duration(duration).attrTween("d", function (d) {
  4334. var updated = $$.updateAngle(d),
  4335. interpolate;
  4336. if (!updated) {
  4337. return function () {
  4338. return "M 0 0";
  4339. };
  4340. }
  4341. // if (this._current === d) {
  4342. // this._current = {
  4343. // startAngle: Math.PI*2,
  4344. // endAngle: Math.PI*2,
  4345. // };
  4346. // }
  4347. if (isNaN(this._current.startAngle)) {
  4348. this._current.startAngle = 0;
  4349. }
  4350. if (isNaN(this._current.endAngle)) {
  4351. this._current.endAngle = this._current.startAngle;
  4352. }
  4353. interpolate = d3.interpolate(this._current, updated);
  4354. this._current = interpolate(0);
  4355. return function (t) {
  4356. var interpolated = interpolate(t);
  4357. interpolated.data = d.data; // data.id will be updated by interporator
  4358. return $$.getArc(interpolated, true);
  4359. };
  4360. }).attr("transform", withTransform ? "scale(1)" : "").style("fill", function (d) {
  4361. return $$.levelColor ? $$.levelColor(d.data.values[0].value) : $$.color(d.data.id);
  4362. }) // Where gauge reading color would receive customization.
  4363. .call($$.endall, function () {
  4364. $$.transiting = false;
  4365. });
  4366. mainArc.exit().transition().duration(durationForExit).style('opacity', 0).remove();
  4367. main.selectAll('.' + CLASS.chartArc).select('text').style("opacity", 0).attr('class', function (d) {
  4368. return $$.isGaugeType(d.data) ? CLASS.gaugeValue : '';
  4369. }).text($$.textForArcLabel.bind($$)).attr("transform", $$.transformForArcLabel.bind($$)).style('font-size', function (d) {
  4370. return $$.isGaugeType(d.data) && $$.filterTargetsToShow($$.data.targets).length === 1 ? Math.round($$.radius / 5) + 'px' : '';
  4371. }).transition().duration(duration).style("opacity", function (d) {
  4372. return $$.isTargetToShow(d.data.id) && $$.isArcType(d.data) ? 1 : 0;
  4373. });
  4374. main.select('.' + CLASS.chartArcsTitle).style("opacity", $$.hasType('donut') || hasGaugeType ? 1 : 0);
  4375. if (hasGaugeType) {
  4376. var index = 0;
  4377. backgroundArc = $$.arcs.select('g.' + CLASS.chartArcsBackground).selectAll('path.' + CLASS.chartArcsBackground).data($$.data.targets);
  4378. backgroundArc.enter().append("path");
  4379. backgroundArc.attr("class", function (d, i) {
  4380. return CLASS.chartArcsBackground + ' ' + CLASS.chartArcsBackground + '-' + i;
  4381. }).attr("d", function (d1) {
  4382. if ($$.hiddenTargetIds.indexOf(d1.id) >= 0) {
  4383. return "M 0 0";
  4384. }
  4385. var d = {
  4386. data: [{ value: config.gauge_max }],
  4387. startAngle: config.gauge_startingAngle,
  4388. endAngle: -1 * config.gauge_startingAngle * (config.gauge_fullCircle ? Math.PI : 1),
  4389. index: index++
  4390. };
  4391. return $$.getArc(d, true, true);
  4392. });
  4393. backgroundArc.exit().remove();
  4394. $$.arcs.select('.' + CLASS.chartArcsGaugeUnit).attr("dy", ".75em").text(config.gauge_label_show ? config.gauge_units : '');
  4395. $$.arcs.select('.' + CLASS.chartArcsGaugeMin).attr("dx", -1 * ($$.innerRadius + ($$.radius - $$.innerRadius) / (config.gauge_fullCircle ? 1 : 2)) + "px").attr("dy", "1.2em").text(config.gauge_label_show ? $$.textForGaugeMinMax(config.gauge_min, false) : '');
  4396. $$.arcs.select('.' + CLASS.chartArcsGaugeMax).attr("dx", $$.innerRadius + ($$.radius - $$.innerRadius) / (config.gauge_fullCircle ? 1 : 2) + "px").attr("dy", "1.2em").text(config.gauge_label_show ? $$.textForGaugeMinMax(config.gauge_max, true) : '');
  4397. }
  4398. };
  4399. c3_chart_internal_fn.initGauge = function () {
  4400. var arcs = this.arcs;
  4401. if (this.hasType('gauge')) {
  4402. arcs.append('g').attr("class", CLASS.chartArcsBackground);
  4403. arcs.append("text").attr("class", CLASS.chartArcsGaugeUnit).style("text-anchor", "middle").style("pointer-events", "none");
  4404. arcs.append("text").attr("class", CLASS.chartArcsGaugeMin).style("text-anchor", "middle").style("pointer-events", "none");
  4405. arcs.append("text").attr("class", CLASS.chartArcsGaugeMax).style("text-anchor", "middle").style("pointer-events", "none");
  4406. }
  4407. };
  4408. c3_chart_internal_fn.getGaugeLabelHeight = function () {
  4409. return this.config.gauge_label_show ? 20 : 0;
  4410. };
  4411. c3_chart_internal_fn.hasCaches = function (ids) {
  4412. for (var i = 0; i < ids.length; i++) {
  4413. if (!(ids[i] in this.cache)) {
  4414. return false;
  4415. }
  4416. }
  4417. return true;
  4418. };
  4419. c3_chart_internal_fn.addCache = function (id, target) {
  4420. this.cache[id] = this.cloneTarget(target);
  4421. };
  4422. c3_chart_internal_fn.getCaches = function (ids) {
  4423. var targets = [],
  4424. i;
  4425. for (i = 0; i < ids.length; i++) {
  4426. if (ids[i] in this.cache) {
  4427. targets.push(this.cloneTarget(this.cache[ids[i]]));
  4428. }
  4429. }
  4430. return targets;
  4431. };
  4432. c3_chart_internal_fn.categoryName = function (i) {
  4433. var config = this.config;
  4434. return i < config.axis_x_categories.length ? config.axis_x_categories[i] : i;
  4435. };
  4436. c3_chart_internal_fn.generateClass = function (prefix, targetId) {
  4437. return " " + prefix + " " + prefix + this.getTargetSelectorSuffix(targetId);
  4438. };
  4439. c3_chart_internal_fn.classText = function (d) {
  4440. return this.generateClass(CLASS.text, d.index);
  4441. };
  4442. c3_chart_internal_fn.classTexts = function (d) {
  4443. return this.generateClass(CLASS.texts, d.id);
  4444. };
  4445. c3_chart_internal_fn.classShape = function (d) {
  4446. return this.generateClass(CLASS.shape, d.index);
  4447. };
  4448. c3_chart_internal_fn.classShapes = function (d) {
  4449. return this.generateClass(CLASS.shapes, d.id);
  4450. };
  4451. c3_chart_internal_fn.classLine = function (d) {
  4452. return this.classShape(d) + this.generateClass(CLASS.line, d.id);
  4453. };
  4454. c3_chart_internal_fn.classLines = function (d) {
  4455. return this.classShapes(d) + this.generateClass(CLASS.lines, d.id);
  4456. };
  4457. c3_chart_internal_fn.classCircle = function (d) {
  4458. return this.classShape(d) + this.generateClass(CLASS.circle, d.index);
  4459. };
  4460. c3_chart_internal_fn.classCircles = function (d) {
  4461. return this.classShapes(d) + this.generateClass(CLASS.circles, d.id);
  4462. };
  4463. c3_chart_internal_fn.classBar = function (d) {
  4464. return this.classShape(d) + this.generateClass(CLASS.bar, d.index);
  4465. };
  4466. c3_chart_internal_fn.classBars = function (d) {
  4467. return this.classShapes(d) + this.generateClass(CLASS.bars, d.id);
  4468. };
  4469. c3_chart_internal_fn.classArc = function (d) {
  4470. return this.classShape(d.data) + this.generateClass(CLASS.arc, d.data.id);
  4471. };
  4472. c3_chart_internal_fn.classArcs = function (d) {
  4473. return this.classShapes(d.data) + this.generateClass(CLASS.arcs, d.data.id);
  4474. };
  4475. c3_chart_internal_fn.classArea = function (d) {
  4476. return this.classShape(d) + this.generateClass(CLASS.area, d.id);
  4477. };
  4478. c3_chart_internal_fn.classAreas = function (d) {
  4479. return this.classShapes(d) + this.generateClass(CLASS.areas, d.id);
  4480. };
  4481. c3_chart_internal_fn.classRegion = function (d, i) {
  4482. return this.generateClass(CLASS.region, i) + ' ' + ('class' in d ? d['class'] : '');
  4483. };
  4484. c3_chart_internal_fn.classEvent = function (d) {
  4485. return this.generateClass(CLASS.eventRect, d.index);
  4486. };
  4487. c3_chart_internal_fn.classTarget = function (id) {
  4488. var $$ = this;
  4489. var additionalClassSuffix = $$.config.data_classes[id],
  4490. additionalClass = '';
  4491. if (additionalClassSuffix) {
  4492. additionalClass = ' ' + CLASS.target + '-' + additionalClassSuffix;
  4493. }
  4494. return $$.generateClass(CLASS.target, id) + additionalClass;
  4495. };
  4496. c3_chart_internal_fn.classFocus = function (d) {
  4497. return this.classFocused(d) + this.classDefocused(d);
  4498. };
  4499. c3_chart_internal_fn.classFocused = function (d) {
  4500. return ' ' + (this.focusedTargetIds.indexOf(d.id) >= 0 ? CLASS.focused : '');
  4501. };
  4502. c3_chart_internal_fn.classDefocused = function (d) {
  4503. return ' ' + (this.defocusedTargetIds.indexOf(d.id) >= 0 ? CLASS.defocused : '');
  4504. };
  4505. c3_chart_internal_fn.classChartText = function (d) {
  4506. return CLASS.chartText + this.classTarget(d.id);
  4507. };
  4508. c3_chart_internal_fn.classChartLine = function (d) {
  4509. return CLASS.chartLine + this.classTarget(d.id);
  4510. };
  4511. c3_chart_internal_fn.classChartBar = function (d) {
  4512. return CLASS.chartBar + this.classTarget(d.id);
  4513. };
  4514. c3_chart_internal_fn.classChartArc = function (d) {
  4515. return CLASS.chartArc + this.classTarget(d.data.id);
  4516. };
  4517. c3_chart_internal_fn.getTargetSelectorSuffix = function (targetId) {
  4518. return targetId || targetId === 0 ? ('-' + targetId).replace(/[\s?!@#$%^&*()_=+,.<>'":;\[\]\/|~`{}\\]/g, '-') : '';
  4519. };
  4520. c3_chart_internal_fn.selectorTarget = function (id, prefix) {
  4521. return (prefix || '') + '.' + CLASS.target + this.getTargetSelectorSuffix(id);
  4522. };
  4523. c3_chart_internal_fn.selectorTargets = function (ids, prefix) {
  4524. var $$ = this;
  4525. ids = ids || [];
  4526. return ids.length ? ids.map(function (id) {
  4527. return $$.selectorTarget(id, prefix);
  4528. }) : null;
  4529. };
  4530. c3_chart_internal_fn.selectorLegend = function (id) {
  4531. return '.' + CLASS.legendItem + this.getTargetSelectorSuffix(id);
  4532. };
  4533. c3_chart_internal_fn.selectorLegends = function (ids) {
  4534. var $$ = this;
  4535. return ids && ids.length ? ids.map(function (id) {
  4536. return $$.selectorLegend(id);
  4537. }) : null;
  4538. };
  4539. c3_chart_internal_fn.getClipPath = function (id) {
  4540. var isIE9 = window.navigator.appVersion.toLowerCase().indexOf("msie 9.") >= 0;
  4541. return "url(" + (isIE9 ? "" : document.URL.split('#')[0]) + "#" + id + ")";
  4542. };
  4543. c3_chart_internal_fn.appendClip = function (parent, id) {
  4544. return parent.append("clipPath").attr("id", id).append("rect");
  4545. };
  4546. c3_chart_internal_fn.getAxisClipX = function (forHorizontal) {
  4547. // axis line width + padding for left
  4548. var left = Math.max(30, this.margin.left);
  4549. return forHorizontal ? -(1 + left) : -(left - 1);
  4550. };
  4551. c3_chart_internal_fn.getAxisClipY = function (forHorizontal) {
  4552. return forHorizontal ? -20 : -this.margin.top;
  4553. };
  4554. c3_chart_internal_fn.getXAxisClipX = function () {
  4555. var $$ = this;
  4556. return $$.getAxisClipX(!$$.config.axis_rotated);
  4557. };
  4558. c3_chart_internal_fn.getXAxisClipY = function () {
  4559. var $$ = this;
  4560. return $$.getAxisClipY(!$$.config.axis_rotated);
  4561. };
  4562. c3_chart_internal_fn.getYAxisClipX = function () {
  4563. var $$ = this;
  4564. return $$.config.axis_y_inner ? -1 : $$.getAxisClipX($$.config.axis_rotated);
  4565. };
  4566. c3_chart_internal_fn.getYAxisClipY = function () {
  4567. var $$ = this;
  4568. return $$.getAxisClipY($$.config.axis_rotated);
  4569. };
  4570. c3_chart_internal_fn.getAxisClipWidth = function (forHorizontal) {
  4571. var $$ = this,
  4572. left = Math.max(30, $$.margin.left),
  4573. right = Math.max(30, $$.margin.right);
  4574. // width + axis line width + padding for left/right
  4575. return forHorizontal ? $$.width + 2 + left + right : $$.margin.left + 20;
  4576. };
  4577. c3_chart_internal_fn.getAxisClipHeight = function (forHorizontal) {
  4578. // less than 20 is not enough to show the axis label 'outer' without legend
  4579. return (forHorizontal ? this.margin.bottom : this.margin.top + this.height) + 20;
  4580. };
  4581. c3_chart_internal_fn.getXAxisClipWidth = function () {
  4582. var $$ = this;
  4583. return $$.getAxisClipWidth(!$$.config.axis_rotated);
  4584. };
  4585. c3_chart_internal_fn.getXAxisClipHeight = function () {
  4586. var $$ = this;
  4587. return $$.getAxisClipHeight(!$$.config.axis_rotated);
  4588. };
  4589. c3_chart_internal_fn.getYAxisClipWidth = function () {
  4590. var $$ = this;
  4591. return $$.getAxisClipWidth($$.config.axis_rotated) + ($$.config.axis_y_inner ? 20 : 0);
  4592. };
  4593. c3_chart_internal_fn.getYAxisClipHeight = function () {
  4594. var $$ = this;
  4595. return $$.getAxisClipHeight($$.config.axis_rotated);
  4596. };
  4597. c3_chart_internal_fn.generateColor = function () {
  4598. var $$ = this,
  4599. config = $$.config,
  4600. d3 = $$.d3,
  4601. colors = config.data_colors,
  4602. pattern = notEmpty(config.color_pattern) ? config.color_pattern : d3.scale.category10().range(),
  4603. callback = config.data_color,
  4604. ids = [];
  4605. return function (d) {
  4606. var id = d.id || d.data && d.data.id || d,
  4607. color;
  4608. // if callback function is provided
  4609. if (colors[id] instanceof Function) {
  4610. color = colors[id](d);
  4611. }
  4612. // if specified, choose that color
  4613. else if (colors[id]) {
  4614. color = colors[id];
  4615. }
  4616. // if not specified, choose from pattern
  4617. else {
  4618. if (ids.indexOf(id) < 0) {
  4619. ids.push(id);
  4620. }
  4621. color = pattern[ids.indexOf(id) % pattern.length];
  4622. colors[id] = color;
  4623. }
  4624. return callback instanceof Function ? callback(color, d) : color;
  4625. };
  4626. };
  4627. c3_chart_internal_fn.generateLevelColor = function () {
  4628. var $$ = this,
  4629. config = $$.config,
  4630. colors = config.color_pattern,
  4631. threshold = config.color_threshold,
  4632. asValue = threshold.unit === 'value',
  4633. values = threshold.values && threshold.values.length ? threshold.values : [],
  4634. max = threshold.max || 100;
  4635. return notEmpty(config.color_threshold) ? function (value) {
  4636. var i,
  4637. v,
  4638. color = colors[colors.length - 1];
  4639. for (i = 0; i < values.length; i++) {
  4640. v = asValue ? value : value * 100 / max;
  4641. if (v < values[i]) {
  4642. color = colors[i];
  4643. break;
  4644. }
  4645. }
  4646. return color;
  4647. } : null;
  4648. };
  4649. c3_chart_internal_fn.getDefaultConfig = function () {
  4650. var config = {
  4651. bindto: '#chart',
  4652. svg_classname: undefined,
  4653. size_width: undefined,
  4654. size_height: undefined,
  4655. padding_left: undefined,
  4656. padding_right: undefined,
  4657. padding_top: undefined,
  4658. padding_bottom: undefined,
  4659. resize_auto: true,
  4660. zoom_enabled: false,
  4661. zoom_extent: undefined,
  4662. zoom_privileged: false,
  4663. zoom_rescale: false,
  4664. zoom_onzoom: function zoom_onzoom() {},
  4665. zoom_onzoomstart: function zoom_onzoomstart() {},
  4666. zoom_onzoomend: function zoom_onzoomend() {},
  4667. zoom_x_min: undefined,
  4668. zoom_x_max: undefined,
  4669. interaction_brighten: true,
  4670. interaction_enabled: true,
  4671. onmouseover: function onmouseover() {},
  4672. onmouseout: function onmouseout() {},
  4673. onresize: function onresize() {},
  4674. onresized: function onresized() {},
  4675. oninit: function oninit() {},
  4676. onrendered: function onrendered() {},
  4677. transition_duration: 350,
  4678. data_x: undefined,
  4679. data_xs: {},
  4680. data_xFormat: '%Y-%m-%d',
  4681. data_xLocaltime: true,
  4682. data_xSort: true,
  4683. data_idConverter: function data_idConverter(id) {
  4684. return id;
  4685. },
  4686. data_names: {},
  4687. data_classes: {},
  4688. data_groups: [],
  4689. data_axes: {},
  4690. data_type: undefined,
  4691. data_types: {},
  4692. data_labels: {},
  4693. data_order: 'desc',
  4694. data_regions: {},
  4695. data_color: undefined,
  4696. data_colors: {},
  4697. data_hide: false,
  4698. data_filter: undefined,
  4699. data_selection_enabled: false,
  4700. data_selection_grouped: false,
  4701. data_selection_isselectable: function data_selection_isselectable() {
  4702. return true;
  4703. },
  4704. data_selection_multiple: true,
  4705. data_selection_draggable: false,
  4706. data_onclick: function data_onclick() {},
  4707. data_onmouseover: function data_onmouseover() {},
  4708. data_onmouseout: function data_onmouseout() {},
  4709. data_onselected: function data_onselected() {},
  4710. data_onunselected: function data_onunselected() {},
  4711. data_url: undefined,
  4712. data_headers: undefined,
  4713. data_json: undefined,
  4714. data_rows: undefined,
  4715. data_columns: undefined,
  4716. data_mimeType: undefined,
  4717. data_keys: undefined,
  4718. // configuration for no plot-able data supplied.
  4719. data_empty_label_text: "",
  4720. // subchart
  4721. subchart_show: false,
  4722. subchart_size_height: 60,
  4723. subchart_axis_x_show: true,
  4724. subchart_onbrush: function subchart_onbrush() {},
  4725. // color
  4726. color_pattern: [],
  4727. color_threshold: {},
  4728. // legend
  4729. legend_show: true,
  4730. legend_hide: false,
  4731. legend_position: 'bottom',
  4732. legend_inset_anchor: 'top-left',
  4733. legend_inset_x: 10,
  4734. legend_inset_y: 0,
  4735. legend_inset_step: undefined,
  4736. legend_item_onclick: undefined,
  4737. legend_item_onmouseover: undefined,
  4738. legend_item_onmouseout: undefined,
  4739. legend_equally: false,
  4740. legend_padding: 0,
  4741. legend_item_tile_width: 10,
  4742. legend_item_tile_height: 10,
  4743. // axis
  4744. axis_rotated: false,
  4745. axis_x_show: true,
  4746. axis_x_type: 'indexed',
  4747. axis_x_localtime: true,
  4748. axis_x_categories: [],
  4749. axis_x_tick_centered: false,
  4750. axis_x_tick_format: undefined,
  4751. axis_x_tick_culling: {},
  4752. axis_x_tick_culling_max: 10,
  4753. axis_x_tick_count: undefined,
  4754. axis_x_tick_fit: true,
  4755. axis_x_tick_values: null,
  4756. axis_x_tick_rotate: 0,
  4757. axis_x_tick_outer: true,
  4758. axis_x_tick_multiline: true,
  4759. axis_x_tick_width: null,
  4760. axis_x_max: undefined,
  4761. axis_x_min: undefined,
  4762. axis_x_padding: {},
  4763. axis_x_height: undefined,
  4764. axis_x_extent: undefined,
  4765. axis_x_label: {},
  4766. axis_y_show: true,
  4767. axis_y_type: undefined,
  4768. axis_y_max: undefined,
  4769. axis_y_min: undefined,
  4770. axis_y_inverted: false,
  4771. axis_y_center: undefined,
  4772. axis_y_inner: undefined,
  4773. axis_y_label: {},
  4774. axis_y_tick_format: undefined,
  4775. axis_y_tick_outer: true,
  4776. axis_y_tick_values: null,
  4777. axis_y_tick_rotate: 0,
  4778. axis_y_tick_count: undefined,
  4779. axis_y_tick_time_value: undefined,
  4780. axis_y_tick_time_interval: undefined,
  4781. axis_y_padding: {},
  4782. axis_y_default: undefined,
  4783. axis_y2_show: false,
  4784. axis_y2_max: undefined,
  4785. axis_y2_min: undefined,
  4786. axis_y2_inverted: false,
  4787. axis_y2_center: undefined,
  4788. axis_y2_inner: undefined,
  4789. axis_y2_label: {},
  4790. axis_y2_tick_format: undefined,
  4791. axis_y2_tick_outer: true,
  4792. axis_y2_tick_values: null,
  4793. axis_y2_tick_count: undefined,
  4794. axis_y2_padding: {},
  4795. axis_y2_default: undefined,
  4796. // grid
  4797. grid_x_show: false,
  4798. grid_x_type: 'tick',
  4799. grid_x_lines: [],
  4800. grid_y_show: false,
  4801. // not used
  4802. // grid_y_type: 'tick',
  4803. grid_y_lines: [],
  4804. grid_y_ticks: 10,
  4805. grid_focus_show: true,
  4806. grid_lines_front: true,
  4807. // point - point of each data
  4808. point_show: true,
  4809. point_r: 2.5,
  4810. point_sensitivity: 10,
  4811. point_focus_expand_enabled: true,
  4812. point_focus_expand_r: undefined,
  4813. point_select_r: undefined,
  4814. // line
  4815. line_connectNull: false,
  4816. line_step_type: 'step',
  4817. // bar
  4818. bar_width: undefined,
  4819. bar_width_ratio: 0.6,
  4820. bar_width_max: undefined,
  4821. bar_zerobased: true,
  4822. bar_space: 0,
  4823. // area
  4824. area_zerobased: true,
  4825. area_above: false,
  4826. // pie
  4827. pie_label_show: true,
  4828. pie_label_format: undefined,
  4829. pie_label_threshold: 0.05,
  4830. pie_label_ratio: undefined,
  4831. pie_expand: {},
  4832. pie_expand_duration: 50,
  4833. // gauge
  4834. gauge_fullCircle: false,
  4835. gauge_label_show: true,
  4836. gauge_labelLine_show: true,
  4837. gauge_label_format: undefined,
  4838. gauge_min: 0,
  4839. gauge_max: 100,
  4840. gauge_startingAngle: -1 * Math.PI / 2,
  4841. gauge_label_extents: undefined,
  4842. gauge_units: undefined,
  4843. gauge_width: undefined,
  4844. gauge_arcs_minWidth: 5,
  4845. gauge_expand: {},
  4846. gauge_expand_duration: 50,
  4847. // donut
  4848. donut_label_show: true,
  4849. donut_label_format: undefined,
  4850. donut_label_threshold: 0.05,
  4851. donut_label_ratio: undefined,
  4852. donut_width: undefined,
  4853. donut_title: "",
  4854. donut_expand: {},
  4855. donut_expand_duration: 50,
  4856. // spline
  4857. spline_interpolation_type: 'cardinal',
  4858. // region - region to change style
  4859. regions: [],
  4860. // tooltip - show when mouseover on each data
  4861. tooltip_show: true,
  4862. tooltip_grouped: true,
  4863. tooltip_order: undefined,
  4864. tooltip_format_title: undefined,
  4865. tooltip_format_name: undefined,
  4866. tooltip_format_value: undefined,
  4867. tooltip_position: undefined,
  4868. tooltip_contents: function tooltip_contents(d, defaultTitleFormat, defaultValueFormat, color) {
  4869. return this.getTooltipContent ? this.getTooltipContent(d, defaultTitleFormat, defaultValueFormat, color) : '';
  4870. },
  4871. tooltip_init_show: false,
  4872. tooltip_init_x: 0,
  4873. tooltip_init_position: { top: '0px', left: '50px' },
  4874. tooltip_onshow: function tooltip_onshow() {},
  4875. tooltip_onhide: function tooltip_onhide() {},
  4876. // title
  4877. title_text: undefined,
  4878. title_padding: {
  4879. top: 0,
  4880. right: 0,
  4881. bottom: 0,
  4882. left: 0
  4883. },
  4884. title_position: 'top-center'
  4885. };
  4886. Object.keys(this.additionalConfig).forEach(function (key) {
  4887. config[key] = this.additionalConfig[key];
  4888. }, this);
  4889. return config;
  4890. };
  4891. c3_chart_internal_fn.additionalConfig = {};
  4892. c3_chart_internal_fn.loadConfig = function (config) {
  4893. var this_config = this.config,
  4894. target,
  4895. keys,
  4896. read;
  4897. function find() {
  4898. var key = keys.shift();
  4899. // console.log("key =>", key, ", target =>", target);
  4900. if (key && target && (typeof target === 'undefined' ? 'undefined' : _typeof(target)) === 'object' && key in target) {
  4901. target = target[key];
  4902. return find();
  4903. } else if (!key) {
  4904. return target;
  4905. } else {
  4906. return undefined;
  4907. }
  4908. }
  4909. Object.keys(this_config).forEach(function (key) {
  4910. target = config;
  4911. keys = key.split('_');
  4912. read = find();
  4913. // console.log("CONFIG : ", key, read);
  4914. if (isDefined(read)) {
  4915. this_config[key] = read;
  4916. }
  4917. });
  4918. };
  4919. c3_chart_internal_fn.convertUrlToData = function (url, mimeType, headers, keys, done) {
  4920. var $$ = this,
  4921. type = mimeType ? mimeType : 'csv';
  4922. var req = $$.d3.xhr(url);
  4923. if (headers) {
  4924. Object.keys(headers).forEach(function (header) {
  4925. req.header(header, headers[header]);
  4926. });
  4927. }
  4928. req.get(function (error, data) {
  4929. var d;
  4930. var dataResponse = data.response || data.responseText; // Fixes IE9 XHR issue; see #1345
  4931. if (!data) {
  4932. throw new Error(error.responseURL + ' ' + error.status + ' (' + error.statusText + ')');
  4933. }
  4934. if (type === 'json') {
  4935. d = $$.convertJsonToData(JSON.parse(dataResponse), keys);
  4936. } else if (type === 'tsv') {
  4937. d = $$.convertTsvToData(dataResponse);
  4938. } else {
  4939. d = $$.convertCsvToData(dataResponse);
  4940. }
  4941. done.call($$, d);
  4942. });
  4943. };
  4944. c3_chart_internal_fn.convertXsvToData = function (xsv, parser) {
  4945. var rows = parser.parseRows(xsv),
  4946. d;
  4947. if (rows.length === 1) {
  4948. d = [{}];
  4949. rows[0].forEach(function (id) {
  4950. d[0][id] = null;
  4951. });
  4952. } else {
  4953. d = parser.parse(xsv);
  4954. }
  4955. return d;
  4956. };
  4957. c3_chart_internal_fn.convertCsvToData = function (csv) {
  4958. return this.convertXsvToData(csv, this.d3.csv);
  4959. };
  4960. c3_chart_internal_fn.convertTsvToData = function (tsv) {
  4961. return this.convertXsvToData(tsv, this.d3.tsv);
  4962. };
  4963. c3_chart_internal_fn.convertJsonToData = function (json, keys) {
  4964. var $$ = this,
  4965. new_rows = [],
  4966. targetKeys,
  4967. data;
  4968. if (keys) {
  4969. // when keys specified, json would be an array that includes objects
  4970. if (keys.x) {
  4971. targetKeys = keys.value.concat(keys.x);
  4972. $$.config.data_x = keys.x;
  4973. } else {
  4974. targetKeys = keys.value;
  4975. }
  4976. new_rows.push(targetKeys);
  4977. json.forEach(function (o) {
  4978. var new_row = [];
  4979. targetKeys.forEach(function (key) {
  4980. // convert undefined to null because undefined data will be removed in convertDataToTargets()
  4981. var v = $$.findValueInJson(o, key);
  4982. if (isUndefined(v)) {
  4983. v = null;
  4984. }
  4985. new_row.push(v);
  4986. });
  4987. new_rows.push(new_row);
  4988. });
  4989. data = $$.convertRowsToData(new_rows);
  4990. } else {
  4991. Object.keys(json).forEach(function (key) {
  4992. new_rows.push([key].concat(json[key]));
  4993. });
  4994. data = $$.convertColumnsToData(new_rows);
  4995. }
  4996. return data;
  4997. };
  4998. c3_chart_internal_fn.findValueInJson = function (object, path) {
  4999. path = path.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties (replace [] with .)
  5000. path = path.replace(/^\./, ''); // strip a leading dot
  5001. var pathArray = path.split('.');
  5002. for (var i = 0; i < pathArray.length; ++i) {
  5003. var k = pathArray[i];
  5004. if (k in object) {
  5005. object = object[k];
  5006. } else {
  5007. return;
  5008. }
  5009. }
  5010. return object;
  5011. };
  5012. /**
  5013. * Converts the rows to normalized data.
  5014. * @param {any[][]} rows The row data
  5015. * @return {Object[]}
  5016. */
  5017. c3_chart_internal_fn.convertRowsToData = function (rows) {
  5018. var newRows = [];
  5019. var keys = rows[0];
  5020. for (var i = 1; i < rows.length; i++) {
  5021. var newRow = {};
  5022. for (var j = 0; j < rows[i].length; j++) {
  5023. if (isUndefined(rows[i][j])) {
  5024. throw new Error("Source data is missing a component at (" + i + "," + j + ")!");
  5025. }
  5026. newRow[keys[j]] = rows[i][j];
  5027. }
  5028. newRows.push(newRow);
  5029. }
  5030. return newRows;
  5031. };
  5032. /**
  5033. * Converts the columns to normalized data.
  5034. * @param {any[][]} columns The column data
  5035. * @return {Object[]}
  5036. */
  5037. c3_chart_internal_fn.convertColumnsToData = function (columns) {
  5038. var newRows = [];
  5039. for (var i = 0; i < columns.length; i++) {
  5040. var key = columns[i][0];
  5041. for (var j = 1; j < columns[i].length; j++) {
  5042. if (isUndefined(newRows[j - 1])) {
  5043. newRows[j - 1] = {};
  5044. }
  5045. if (isUndefined(columns[i][j])) {
  5046. throw new Error("Source data is missing a component at (" + i + "," + j + ")!");
  5047. }
  5048. newRows[j - 1][key] = columns[i][j];
  5049. }
  5050. }
  5051. return newRows;
  5052. };
  5053. c3_chart_internal_fn.convertDataToTargets = function (data, appendXs) {
  5054. var $$ = this,
  5055. config = $$.config,
  5056. ids = $$.d3.keys(data[0]).filter($$.isNotX, $$),
  5057. xs = $$.d3.keys(data[0]).filter($$.isX, $$),
  5058. targets;
  5059. // save x for update data by load when custom x and c3.x API
  5060. ids.forEach(function (id) {
  5061. var xKey = $$.getXKey(id);
  5062. if ($$.isCustomX() || $$.isTimeSeries()) {
  5063. // if included in input data
  5064. if (xs.indexOf(xKey) >= 0) {
  5065. $$.data.xs[id] = (appendXs && $$.data.xs[id] ? $$.data.xs[id] : []).concat(data.map(function (d) {
  5066. return d[xKey];
  5067. }).filter(isValue).map(function (rawX, i) {
  5068. return $$.generateTargetX(rawX, id, i);
  5069. }));
  5070. }
  5071. // if not included in input data, find from preloaded data of other id's x
  5072. else if (config.data_x) {
  5073. $$.data.xs[id] = $$.getOtherTargetXs();
  5074. }
  5075. // if not included in input data, find from preloaded data
  5076. else if (notEmpty(config.data_xs)) {
  5077. $$.data.xs[id] = $$.getXValuesOfXKey(xKey, $$.data.targets);
  5078. }
  5079. // MEMO: if no x included, use same x of current will be used
  5080. } else {
  5081. $$.data.xs[id] = data.map(function (d, i) {
  5082. return i;
  5083. });
  5084. }
  5085. });
  5086. // check x is defined
  5087. ids.forEach(function (id) {
  5088. if (!$$.data.xs[id]) {
  5089. throw new Error('x is not defined for id = "' + id + '".');
  5090. }
  5091. });
  5092. // convert to target
  5093. targets = ids.map(function (id, index) {
  5094. var convertedId = config.data_idConverter(id);
  5095. return {
  5096. id: convertedId,
  5097. id_org: id,
  5098. values: data.map(function (d, i) {
  5099. var xKey = $$.getXKey(id),
  5100. rawX = d[xKey],
  5101. value = d[id] !== null && !isNaN(d[id]) ? +d[id] : null,
  5102. x;
  5103. // use x as categories if custom x and categorized
  5104. if ($$.isCustomX() && $$.isCategorized() && !isUndefined(rawX)) {
  5105. if (index === 0 && i === 0) {
  5106. config.axis_x_categories = [];
  5107. }
  5108. x = config.axis_x_categories.indexOf(rawX);
  5109. if (x === -1) {
  5110. x = config.axis_x_categories.length;
  5111. config.axis_x_categories.push(rawX);
  5112. }
  5113. } else {
  5114. x = $$.generateTargetX(rawX, id, i);
  5115. }
  5116. // mark as x = undefined if value is undefined and filter to remove after mapped
  5117. if (isUndefined(d[id]) || $$.data.xs[id].length <= i) {
  5118. x = undefined;
  5119. }
  5120. return { x: x, value: value, id: convertedId };
  5121. }).filter(function (v) {
  5122. return isDefined(v.x);
  5123. })
  5124. };
  5125. });
  5126. // finish targets
  5127. targets.forEach(function (t) {
  5128. var i;
  5129. // sort values by its x
  5130. if (config.data_xSort) {
  5131. t.values = t.values.sort(function (v1, v2) {
  5132. var x1 = v1.x || v1.x === 0 ? v1.x : Infinity,
  5133. x2 = v2.x || v2.x === 0 ? v2.x : Infinity;
  5134. return x1 - x2;
  5135. });
  5136. }
  5137. // indexing each value
  5138. i = 0;
  5139. t.values.forEach(function (v) {
  5140. v.index = i++;
  5141. });
  5142. // this needs to be sorted because its index and value.index is identical
  5143. $$.data.xs[t.id].sort(function (v1, v2) {
  5144. return v1 - v2;
  5145. });
  5146. });
  5147. // cache information about values
  5148. $$.hasNegativeValue = $$.hasNegativeValueInTargets(targets);
  5149. $$.hasPositiveValue = $$.hasPositiveValueInTargets(targets);
  5150. // set target types
  5151. if (config.data_type) {
  5152. $$.setTargetType($$.mapToIds(targets).filter(function (id) {
  5153. return !(id in config.data_types);
  5154. }), config.data_type);
  5155. }
  5156. // cache as original id keyed
  5157. targets.forEach(function (d) {
  5158. $$.addCache(d.id_org, d);
  5159. });
  5160. return targets;
  5161. };
  5162. c3_chart_internal_fn.isX = function (key) {
  5163. var $$ = this,
  5164. config = $$.config;
  5165. return config.data_x && key === config.data_x || notEmpty(config.data_xs) && hasValue(config.data_xs, key);
  5166. };
  5167. c3_chart_internal_fn.isNotX = function (key) {
  5168. return !this.isX(key);
  5169. };
  5170. c3_chart_internal_fn.getXKey = function (id) {
  5171. var $$ = this,
  5172. config = $$.config;
  5173. return config.data_x ? config.data_x : notEmpty(config.data_xs) ? config.data_xs[id] : null;
  5174. };
  5175. c3_chart_internal_fn.getXValuesOfXKey = function (key, targets) {
  5176. var $$ = this,
  5177. xValues,
  5178. ids = targets && notEmpty(targets) ? $$.mapToIds(targets) : [];
  5179. ids.forEach(function (id) {
  5180. if ($$.getXKey(id) === key) {
  5181. xValues = $$.data.xs[id];
  5182. }
  5183. });
  5184. return xValues;
  5185. };
  5186. c3_chart_internal_fn.getIndexByX = function (x) {
  5187. var $$ = this,
  5188. data = $$.filterByX($$.data.targets, x);
  5189. return data.length ? data[0].index : null;
  5190. };
  5191. c3_chart_internal_fn.getXValue = function (id, i) {
  5192. var $$ = this;
  5193. return id in $$.data.xs && $$.data.xs[id] && isValue($$.data.xs[id][i]) ? $$.data.xs[id][i] : i;
  5194. };
  5195. c3_chart_internal_fn.getOtherTargetXs = function () {
  5196. var $$ = this,
  5197. idsForX = Object.keys($$.data.xs);
  5198. return idsForX.length ? $$.data.xs[idsForX[0]] : null;
  5199. };
  5200. c3_chart_internal_fn.getOtherTargetX = function (index) {
  5201. var xs = this.getOtherTargetXs();
  5202. return xs && index < xs.length ? xs[index] : null;
  5203. };
  5204. c3_chart_internal_fn.addXs = function (xs) {
  5205. var $$ = this;
  5206. Object.keys(xs).forEach(function (id) {
  5207. $$.config.data_xs[id] = xs[id];
  5208. });
  5209. };
  5210. c3_chart_internal_fn.hasMultipleX = function (xs) {
  5211. return this.d3.set(Object.keys(xs).map(function (id) {
  5212. return xs[id];
  5213. })).size() > 1;
  5214. };
  5215. c3_chart_internal_fn.isMultipleX = function () {
  5216. return notEmpty(this.config.data_xs) || !this.config.data_xSort || this.hasType('scatter');
  5217. };
  5218. c3_chart_internal_fn.addName = function (data) {
  5219. var $$ = this,
  5220. name;
  5221. if (data) {
  5222. name = $$.config.data_names[data.id];
  5223. data.name = name !== undefined ? name : data.id;
  5224. }
  5225. return data;
  5226. };
  5227. c3_chart_internal_fn.getValueOnIndex = function (values, index) {
  5228. var valueOnIndex = values.filter(function (v) {
  5229. return v.index === index;
  5230. });
  5231. return valueOnIndex.length ? valueOnIndex[0] : null;
  5232. };
  5233. c3_chart_internal_fn.updateTargetX = function (targets, x) {
  5234. var $$ = this;
  5235. targets.forEach(function (t) {
  5236. t.values.forEach(function (v, i) {
  5237. v.x = $$.generateTargetX(x[i], t.id, i);
  5238. });
  5239. $$.data.xs[t.id] = x;
  5240. });
  5241. };
  5242. c3_chart_internal_fn.updateTargetXs = function (targets, xs) {
  5243. var $$ = this;
  5244. targets.forEach(function (t) {
  5245. if (xs[t.id]) {
  5246. $$.updateTargetX([t], xs[t.id]);
  5247. }
  5248. });
  5249. };
  5250. c3_chart_internal_fn.generateTargetX = function (rawX, id, index) {
  5251. var $$ = this,
  5252. x;
  5253. if ($$.isTimeSeries()) {
  5254. x = rawX ? $$.parseDate(rawX) : $$.parseDate($$.getXValue(id, index));
  5255. } else if ($$.isCustomX() && !$$.isCategorized()) {
  5256. x = isValue(rawX) ? +rawX : $$.getXValue(id, index);
  5257. } else {
  5258. x = index;
  5259. }
  5260. return x;
  5261. };
  5262. c3_chart_internal_fn.cloneTarget = function (target) {
  5263. return {
  5264. id: target.id,
  5265. id_org: target.id_org,
  5266. values: target.values.map(function (d) {
  5267. return { x: d.x, value: d.value, id: d.id };
  5268. })
  5269. };
  5270. };
  5271. c3_chart_internal_fn.updateXs = function () {
  5272. var $$ = this;
  5273. if ($$.data.targets.length) {
  5274. $$.xs = [];
  5275. $$.data.targets[0].values.forEach(function (v) {
  5276. $$.xs[v.index] = v.x;
  5277. });
  5278. }
  5279. };
  5280. c3_chart_internal_fn.getPrevX = function (i) {
  5281. var x = this.xs[i - 1];
  5282. return typeof x !== 'undefined' ? x : null;
  5283. };
  5284. c3_chart_internal_fn.getNextX = function (i) {
  5285. var x = this.xs[i + 1];
  5286. return typeof x !== 'undefined' ? x : null;
  5287. };
  5288. c3_chart_internal_fn.getMaxDataCount = function () {
  5289. var $$ = this;
  5290. return $$.d3.max($$.data.targets, function (t) {
  5291. return t.values.length;
  5292. });
  5293. };
  5294. c3_chart_internal_fn.getMaxDataCountTarget = function (targets) {
  5295. var length = targets.length,
  5296. max = 0,
  5297. maxTarget;
  5298. if (length > 1) {
  5299. targets.forEach(function (t) {
  5300. if (t.values.length > max) {
  5301. maxTarget = t;
  5302. max = t.values.length;
  5303. }
  5304. });
  5305. } else {
  5306. maxTarget = length ? targets[0] : null;
  5307. }
  5308. return maxTarget;
  5309. };
  5310. c3_chart_internal_fn.getEdgeX = function (targets) {
  5311. var $$ = this;
  5312. return !targets.length ? [0, 0] : [$$.d3.min(targets, function (t) {
  5313. return t.values[0].x;
  5314. }), $$.d3.max(targets, function (t) {
  5315. return t.values[t.values.length - 1].x;
  5316. })];
  5317. };
  5318. c3_chart_internal_fn.mapToIds = function (targets) {
  5319. return targets.map(function (d) {
  5320. return d.id;
  5321. });
  5322. };
  5323. c3_chart_internal_fn.mapToTargetIds = function (ids) {
  5324. var $$ = this;
  5325. return ids ? [].concat(ids) : $$.mapToIds($$.data.targets);
  5326. };
  5327. c3_chart_internal_fn.hasTarget = function (targets, id) {
  5328. var ids = this.mapToIds(targets),
  5329. i;
  5330. for (i = 0; i < ids.length; i++) {
  5331. if (ids[i] === id) {
  5332. return true;
  5333. }
  5334. }
  5335. return false;
  5336. };
  5337. c3_chart_internal_fn.isTargetToShow = function (targetId) {
  5338. return this.hiddenTargetIds.indexOf(targetId) < 0;
  5339. };
  5340. c3_chart_internal_fn.isLegendToShow = function (targetId) {
  5341. return this.hiddenLegendIds.indexOf(targetId) < 0;
  5342. };
  5343. c3_chart_internal_fn.filterTargetsToShow = function (targets) {
  5344. var $$ = this;
  5345. return targets.filter(function (t) {
  5346. return $$.isTargetToShow(t.id);
  5347. });
  5348. };
  5349. c3_chart_internal_fn.mapTargetsToUniqueXs = function (targets) {
  5350. var $$ = this;
  5351. var xs = $$.d3.set($$.d3.merge(targets.map(function (t) {
  5352. return t.values.map(function (v) {
  5353. return +v.x;
  5354. });
  5355. }))).values();
  5356. xs = $$.isTimeSeries() ? xs.map(function (x) {
  5357. return new Date(+x);
  5358. }) : xs.map(function (x) {
  5359. return +x;
  5360. });
  5361. return xs.sort(function (a, b) {
  5362. return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;
  5363. });
  5364. };
  5365. c3_chart_internal_fn.addHiddenTargetIds = function (targetIds) {
  5366. targetIds = targetIds instanceof Array ? targetIds : new Array(targetIds);
  5367. for (var i = 0; i < targetIds.length; i++) {
  5368. if (this.hiddenTargetIds.indexOf(targetIds[i]) < 0) {
  5369. this.hiddenTargetIds = this.hiddenTargetIds.concat(targetIds[i]);
  5370. }
  5371. }
  5372. };
  5373. c3_chart_internal_fn.removeHiddenTargetIds = function (targetIds) {
  5374. this.hiddenTargetIds = this.hiddenTargetIds.filter(function (id) {
  5375. return targetIds.indexOf(id) < 0;
  5376. });
  5377. };
  5378. c3_chart_internal_fn.addHiddenLegendIds = function (targetIds) {
  5379. targetIds = targetIds instanceof Array ? targetIds : new Array(targetIds);
  5380. for (var i = 0; i < targetIds.length; i++) {
  5381. if (this.hiddenLegendIds.indexOf(targetIds[i]) < 0) {
  5382. this.hiddenLegendIds = this.hiddenLegendIds.concat(targetIds[i]);
  5383. }
  5384. }
  5385. };
  5386. c3_chart_internal_fn.removeHiddenLegendIds = function (targetIds) {
  5387. this.hiddenLegendIds = this.hiddenLegendIds.filter(function (id) {
  5388. return targetIds.indexOf(id) < 0;
  5389. });
  5390. };
  5391. c3_chart_internal_fn.getValuesAsIdKeyed = function (targets) {
  5392. var ys = {};
  5393. targets.forEach(function (t) {
  5394. ys[t.id] = [];
  5395. t.values.forEach(function (v) {
  5396. ys[t.id].push(v.value);
  5397. });
  5398. });
  5399. return ys;
  5400. };
  5401. c3_chart_internal_fn.checkValueInTargets = function (targets, checker) {
  5402. var ids = Object.keys(targets),
  5403. i,
  5404. j,
  5405. values;
  5406. for (i = 0; i < ids.length; i++) {
  5407. values = targets[ids[i]].values;
  5408. for (j = 0; j < values.length; j++) {
  5409. if (checker(values[j].value)) {
  5410. return true;
  5411. }
  5412. }
  5413. }
  5414. return false;
  5415. };
  5416. c3_chart_internal_fn.hasNegativeValueInTargets = function (targets) {
  5417. return this.checkValueInTargets(targets, function (v) {
  5418. return v < 0;
  5419. });
  5420. };
  5421. c3_chart_internal_fn.hasPositiveValueInTargets = function (targets) {
  5422. return this.checkValueInTargets(targets, function (v) {
  5423. return v > 0;
  5424. });
  5425. };
  5426. c3_chart_internal_fn.isOrderDesc = function () {
  5427. var config = this.config;
  5428. return typeof config.data_order === 'string' && config.data_order.toLowerCase() === 'desc';
  5429. };
  5430. c3_chart_internal_fn.isOrderAsc = function () {
  5431. var config = this.config;
  5432. return typeof config.data_order === 'string' && config.data_order.toLowerCase() === 'asc';
  5433. };
  5434. c3_chart_internal_fn.getOrderFunction = function () {
  5435. var $$ = this,
  5436. config = $$.config,
  5437. orderAsc = $$.isOrderAsc(),
  5438. orderDesc = $$.isOrderDesc();
  5439. if (orderAsc || orderDesc) {
  5440. return function (t1, t2) {
  5441. var reducer = function reducer(p, c) {
  5442. return p + Math.abs(c.value);
  5443. };
  5444. var t1Sum = t1.values.reduce(reducer, 0),
  5445. t2Sum = t2.values.reduce(reducer, 0);
  5446. return orderDesc ? t2Sum - t1Sum : t1Sum - t2Sum;
  5447. };
  5448. } else if (isFunction(config.data_order)) {
  5449. return config.data_order;
  5450. } else if (isArray(config.data_order)) {
  5451. var order = config.data_order;
  5452. return function (t1, t2) {
  5453. return order.indexOf(t1.id) - order.indexOf(t2.id);
  5454. };
  5455. }
  5456. };
  5457. c3_chart_internal_fn.orderTargets = function (targets) {
  5458. var fct = this.getOrderFunction();
  5459. if (fct) {
  5460. targets.sort(fct);
  5461. if (this.isOrderAsc() || this.isOrderDesc()) {
  5462. targets.reverse();
  5463. }
  5464. }
  5465. return targets;
  5466. };
  5467. c3_chart_internal_fn.filterByX = function (targets, x) {
  5468. return this.d3.merge(targets.map(function (t) {
  5469. return t.values;
  5470. })).filter(function (v) {
  5471. return v.x - x === 0;
  5472. });
  5473. };
  5474. c3_chart_internal_fn.filterRemoveNull = function (data) {
  5475. return data.filter(function (d) {
  5476. return isValue(d.value);
  5477. });
  5478. };
  5479. c3_chart_internal_fn.filterByXDomain = function (targets, xDomain) {
  5480. return targets.map(function (t) {
  5481. return {
  5482. id: t.id,
  5483. id_org: t.id_org,
  5484. values: t.values.filter(function (v) {
  5485. return xDomain[0] <= v.x && v.x <= xDomain[1];
  5486. })
  5487. };
  5488. });
  5489. };
  5490. c3_chart_internal_fn.hasDataLabel = function () {
  5491. var config = this.config;
  5492. if (typeof config.data_labels === 'boolean' && config.data_labels) {
  5493. return true;
  5494. } else if (_typeof(config.data_labels) === 'object' && notEmpty(config.data_labels)) {
  5495. return true;
  5496. }
  5497. return false;
  5498. };
  5499. c3_chart_internal_fn.getDataLabelLength = function (min, max, key) {
  5500. var $$ = this,
  5501. lengths = [0, 0],
  5502. paddingCoef = 1.3;
  5503. $$.selectChart.select('svg').selectAll('.dummy').data([min, max]).enter().append('text').text(function (d) {
  5504. return $$.dataLabelFormat(d.id)(d);
  5505. }).each(function (d, i) {
  5506. lengths[i] = this.getBoundingClientRect()[key] * paddingCoef;
  5507. }).remove();
  5508. return lengths;
  5509. };
  5510. c3_chart_internal_fn.isNoneArc = function (d) {
  5511. return this.hasTarget(this.data.targets, d.id);
  5512. }, c3_chart_internal_fn.isArc = function (d) {
  5513. return 'data' in d && this.hasTarget(this.data.targets, d.data.id);
  5514. };
  5515. c3_chart_internal_fn.findSameXOfValues = function (values, index) {
  5516. var i,
  5517. targetX = values[index].x,
  5518. sames = [];
  5519. for (i = index - 1; i >= 0; i--) {
  5520. if (targetX !== values[i].x) {
  5521. break;
  5522. }
  5523. sames.push(values[i]);
  5524. }
  5525. for (i = index; i < values.length; i++) {
  5526. if (targetX !== values[i].x) {
  5527. break;
  5528. }
  5529. sames.push(values[i]);
  5530. }
  5531. return sames;
  5532. };
  5533. c3_chart_internal_fn.findClosestFromTargets = function (targets, pos) {
  5534. var $$ = this,
  5535. candidates;
  5536. // map to array of closest points of each target
  5537. candidates = targets.map(function (target) {
  5538. return $$.findClosest(target.values, pos);
  5539. });
  5540. // decide closest point and return
  5541. return $$.findClosest(candidates, pos);
  5542. };
  5543. c3_chart_internal_fn.findClosest = function (values, pos) {
  5544. var $$ = this,
  5545. minDist = $$.config.point_sensitivity,
  5546. closest;
  5547. // find mouseovering bar
  5548. values.filter(function (v) {
  5549. return v && $$.isBarType(v.id);
  5550. }).forEach(function (v) {
  5551. var shape = $$.main.select('.' + CLASS.bars + $$.getTargetSelectorSuffix(v.id) + ' .' + CLASS.bar + '-' + v.index).node();
  5552. if (!closest && $$.isWithinBar(shape)) {
  5553. closest = v;
  5554. }
  5555. });
  5556. // find closest point from non-bar
  5557. values.filter(function (v) {
  5558. return v && !$$.isBarType(v.id);
  5559. }).forEach(function (v) {
  5560. var d = $$.dist(v, pos);
  5561. if (d < minDist) {
  5562. minDist = d;
  5563. closest = v;
  5564. }
  5565. });
  5566. return closest;
  5567. };
  5568. c3_chart_internal_fn.dist = function (data, pos) {
  5569. var $$ = this,
  5570. config = $$.config,
  5571. xIndex = config.axis_rotated ? 1 : 0,
  5572. yIndex = config.axis_rotated ? 0 : 1,
  5573. y = $$.circleY(data, data.index),
  5574. x = $$.x(data.x);
  5575. return Math.sqrt(Math.pow(x - pos[xIndex], 2) + Math.pow(y - pos[yIndex], 2));
  5576. };
  5577. c3_chart_internal_fn.convertValuesToStep = function (values) {
  5578. var converted = [].concat(values),
  5579. i;
  5580. if (!this.isCategorized()) {
  5581. return values;
  5582. }
  5583. for (i = values.length + 1; 0 < i; i--) {
  5584. converted[i] = converted[i - 1];
  5585. }
  5586. converted[0] = {
  5587. x: converted[0].x - 1,
  5588. value: converted[0].value,
  5589. id: converted[0].id
  5590. };
  5591. converted[values.length + 1] = {
  5592. x: converted[values.length].x + 1,
  5593. value: converted[values.length].value,
  5594. id: converted[values.length].id
  5595. };
  5596. return converted;
  5597. };
  5598. c3_chart_internal_fn.updateDataAttributes = function (name, attrs) {
  5599. var $$ = this,
  5600. config = $$.config,
  5601. current = config['data_' + name];
  5602. if (typeof attrs === 'undefined') {
  5603. return current;
  5604. }
  5605. Object.keys(attrs).forEach(function (id) {
  5606. current[id] = attrs[id];
  5607. });
  5608. $$.redraw({ withLegend: true });
  5609. return current;
  5610. };
  5611. c3_chart_internal_fn.load = function (targets, args) {
  5612. var $$ = this;
  5613. if (targets) {
  5614. // filter loading targets if needed
  5615. if (args.filter) {
  5616. targets = targets.filter(args.filter);
  5617. }
  5618. // set type if args.types || args.type specified
  5619. if (args.type || args.types) {
  5620. targets.forEach(function (t) {
  5621. var type = args.types && args.types[t.id] ? args.types[t.id] : args.type;
  5622. $$.setTargetType(t.id, type);
  5623. });
  5624. }
  5625. // Update/Add data
  5626. $$.data.targets.forEach(function (d) {
  5627. for (var i = 0; i < targets.length; i++) {
  5628. if (d.id === targets[i].id) {
  5629. d.values = targets[i].values;
  5630. targets.splice(i, 1);
  5631. break;
  5632. }
  5633. }
  5634. });
  5635. $$.data.targets = $$.data.targets.concat(targets); // add remained
  5636. }
  5637. // Set targets
  5638. $$.updateTargets($$.data.targets);
  5639. // Redraw with new targets
  5640. $$.redraw({ withUpdateOrgXDomain: true, withUpdateXDomain: true, withLegend: true });
  5641. if (args.done) {
  5642. args.done();
  5643. }
  5644. };
  5645. c3_chart_internal_fn.loadFromArgs = function (args) {
  5646. var $$ = this;
  5647. if (args.data) {
  5648. $$.load($$.convertDataToTargets(args.data), args);
  5649. } else if (args.url) {
  5650. $$.convertUrlToData(args.url, args.mimeType, args.headers, args.keys, function (data) {
  5651. $$.load($$.convertDataToTargets(data), args);
  5652. });
  5653. } else if (args.json) {
  5654. $$.load($$.convertDataToTargets($$.convertJsonToData(args.json, args.keys)), args);
  5655. } else if (args.rows) {
  5656. $$.load($$.convertDataToTargets($$.convertRowsToData(args.rows)), args);
  5657. } else if (args.columns) {
  5658. $$.load($$.convertDataToTargets($$.convertColumnsToData(args.columns)), args);
  5659. } else {
  5660. $$.load(null, args);
  5661. }
  5662. };
  5663. c3_chart_internal_fn.unload = function (targetIds, done) {
  5664. var $$ = this;
  5665. if (!done) {
  5666. done = function done() {};
  5667. }
  5668. // filter existing target
  5669. targetIds = targetIds.filter(function (id) {
  5670. return $$.hasTarget($$.data.targets, id);
  5671. });
  5672. // If no target, call done and return
  5673. if (!targetIds || targetIds.length === 0) {
  5674. done();
  5675. return;
  5676. }
  5677. $$.svg.selectAll(targetIds.map(function (id) {
  5678. return $$.selectorTarget(id);
  5679. })).transition().style('opacity', 0).remove().call($$.endall, done);
  5680. targetIds.forEach(function (id) {
  5681. // Reset fadein for future load
  5682. $$.withoutFadeIn[id] = false;
  5683. // Remove target's elements
  5684. if ($$.legend) {
  5685. $$.legend.selectAll('.' + CLASS.legendItem + $$.getTargetSelectorSuffix(id)).remove();
  5686. }
  5687. // Remove target
  5688. $$.data.targets = $$.data.targets.filter(function (t) {
  5689. return t.id !== id;
  5690. });
  5691. });
  5692. };
  5693. c3_chart_internal_fn.getYDomainMin = function (targets) {
  5694. var $$ = this,
  5695. config = $$.config,
  5696. ids = $$.mapToIds(targets),
  5697. ys = $$.getValuesAsIdKeyed(targets),
  5698. j,
  5699. k,
  5700. baseId,
  5701. idsInGroup,
  5702. id,
  5703. hasNegativeValue;
  5704. if (config.data_groups.length > 0) {
  5705. hasNegativeValue = $$.hasNegativeValueInTargets(targets);
  5706. for (j = 0; j < config.data_groups.length; j++) {
  5707. // Determine baseId
  5708. idsInGroup = config.data_groups[j].filter(function (id) {
  5709. return ids.indexOf(id) >= 0;
  5710. });
  5711. if (idsInGroup.length === 0) {
  5712. continue;
  5713. }
  5714. baseId = idsInGroup[0];
  5715. // Consider negative values
  5716. if (hasNegativeValue && ys[baseId]) {
  5717. ys[baseId].forEach(function (v, i) {
  5718. ys[baseId][i] = v < 0 ? v : 0;
  5719. });
  5720. }
  5721. // Compute min
  5722. for (k = 1; k < idsInGroup.length; k++) {
  5723. id = idsInGroup[k];
  5724. if (!ys[id]) {
  5725. continue;
  5726. }
  5727. ys[id].forEach(function (v, i) {
  5728. if ($$.axis.getId(id) === $$.axis.getId(baseId) && ys[baseId] && !(hasNegativeValue && +v > 0)) {
  5729. ys[baseId][i] += +v;
  5730. }
  5731. });
  5732. }
  5733. }
  5734. }
  5735. return $$.d3.min(Object.keys(ys).map(function (key) {
  5736. return $$.d3.min(ys[key]);
  5737. }));
  5738. };
  5739. c3_chart_internal_fn.getYDomainMax = function (targets) {
  5740. var $$ = this,
  5741. config = $$.config,
  5742. ids = $$.mapToIds(targets),
  5743. ys = $$.getValuesAsIdKeyed(targets),
  5744. j,
  5745. k,
  5746. baseId,
  5747. idsInGroup,
  5748. id,
  5749. hasPositiveValue;
  5750. if (config.data_groups.length > 0) {
  5751. hasPositiveValue = $$.hasPositiveValueInTargets(targets);
  5752. for (j = 0; j < config.data_groups.length; j++) {
  5753. // Determine baseId
  5754. idsInGroup = config.data_groups[j].filter(function (id) {
  5755. return ids.indexOf(id) >= 0;
  5756. });
  5757. if (idsInGroup.length === 0) {
  5758. continue;
  5759. }
  5760. baseId = idsInGroup[0];
  5761. // Consider positive values
  5762. if (hasPositiveValue && ys[baseId]) {
  5763. ys[baseId].forEach(function (v, i) {
  5764. ys[baseId][i] = v > 0 ? v : 0;
  5765. });
  5766. }
  5767. // Compute max
  5768. for (k = 1; k < idsInGroup.length; k++) {
  5769. id = idsInGroup[k];
  5770. if (!ys[id]) {
  5771. continue;
  5772. }
  5773. ys[id].forEach(function (v, i) {
  5774. if ($$.axis.getId(id) === $$.axis.getId(baseId) && ys[baseId] && !(hasPositiveValue && +v < 0)) {
  5775. ys[baseId][i] += +v;
  5776. }
  5777. });
  5778. }
  5779. }
  5780. }
  5781. return $$.d3.max(Object.keys(ys).map(function (key) {
  5782. return $$.d3.max(ys[key]);
  5783. }));
  5784. };
  5785. c3_chart_internal_fn.getYDomain = function (targets, axisId, xDomain) {
  5786. var $$ = this,
  5787. config = $$.config,
  5788. targetsByAxisId = targets.filter(function (t) {
  5789. return $$.axis.getId(t.id) === axisId;
  5790. }),
  5791. yTargets = xDomain ? $$.filterByXDomain(targetsByAxisId, xDomain) : targetsByAxisId,
  5792. yMin = axisId === 'y2' ? config.axis_y2_min : config.axis_y_min,
  5793. yMax = axisId === 'y2' ? config.axis_y2_max : config.axis_y_max,
  5794. yDomainMin = $$.getYDomainMin(yTargets),
  5795. yDomainMax = $$.getYDomainMax(yTargets),
  5796. domain,
  5797. domainLength,
  5798. padding,
  5799. padding_top,
  5800. padding_bottom,
  5801. center = axisId === 'y2' ? config.axis_y2_center : config.axis_y_center,
  5802. yDomainAbs,
  5803. lengths,
  5804. diff,
  5805. ratio,
  5806. isAllPositive,
  5807. isAllNegative,
  5808. isZeroBased = $$.hasType('bar', yTargets) && config.bar_zerobased || $$.hasType('area', yTargets) && config.area_zerobased,
  5809. isInverted = axisId === 'y2' ? config.axis_y2_inverted : config.axis_y_inverted,
  5810. showHorizontalDataLabel = $$.hasDataLabel() && config.axis_rotated,
  5811. showVerticalDataLabel = $$.hasDataLabel() && !config.axis_rotated;
  5812. // MEMO: avoid inverting domain unexpectedly
  5813. yDomainMin = isValue(yMin) ? yMin : isValue(yMax) ? yDomainMin < yMax ? yDomainMin : yMax - 10 : yDomainMin;
  5814. yDomainMax = isValue(yMax) ? yMax : isValue(yMin) ? yMin < yDomainMax ? yDomainMax : yMin + 10 : yDomainMax;
  5815. if (yTargets.length === 0) {
  5816. // use current domain if target of axisId is none
  5817. return axisId === 'y2' ? $$.y2.domain() : $$.y.domain();
  5818. }
  5819. if (isNaN(yDomainMin)) {
  5820. // set minimum to zero when not number
  5821. yDomainMin = 0;
  5822. }
  5823. if (isNaN(yDomainMax)) {
  5824. // set maximum to have same value as yDomainMin
  5825. yDomainMax = yDomainMin;
  5826. }
  5827. if (yDomainMin === yDomainMax) {
  5828. yDomainMin < 0 ? yDomainMax = 0 : yDomainMin = 0;
  5829. }
  5830. isAllPositive = yDomainMin >= 0 && yDomainMax >= 0;
  5831. isAllNegative = yDomainMin <= 0 && yDomainMax <= 0;
  5832. // Cancel zerobased if axis_*_min / axis_*_max specified
  5833. if (isValue(yMin) && isAllPositive || isValue(yMax) && isAllNegative) {
  5834. isZeroBased = false;
  5835. }
  5836. // Bar/Area chart should be 0-based if all positive|negative
  5837. if (isZeroBased) {
  5838. if (isAllPositive) {
  5839. yDomainMin = 0;
  5840. }
  5841. if (isAllNegative) {
  5842. yDomainMax = 0;
  5843. }
  5844. }
  5845. domainLength = Math.abs(yDomainMax - yDomainMin);
  5846. padding = padding_top = padding_bottom = domainLength * 0.1;
  5847. if (typeof center !== 'undefined') {
  5848. yDomainAbs = Math.max(Math.abs(yDomainMin), Math.abs(yDomainMax));
  5849. yDomainMax = center + yDomainAbs;
  5850. yDomainMin = center - yDomainAbs;
  5851. }
  5852. // add padding for data label
  5853. if (showHorizontalDataLabel) {
  5854. lengths = $$.getDataLabelLength(yDomainMin, yDomainMax, 'width');
  5855. diff = diffDomain($$.y.range());
  5856. ratio = [lengths[0] / diff, lengths[1] / diff];
  5857. padding_top += domainLength * (ratio[1] / (1 - ratio[0] - ratio[1]));
  5858. padding_bottom += domainLength * (ratio[0] / (1 - ratio[0] - ratio[1]));
  5859. } else if (showVerticalDataLabel) {
  5860. lengths = $$.getDataLabelLength(yDomainMin, yDomainMax, 'height');
  5861. padding_top += $$.axis.convertPixelsToAxisPadding(lengths[1], domainLength);
  5862. padding_bottom += $$.axis.convertPixelsToAxisPadding(lengths[0], domainLength);
  5863. }
  5864. if (axisId === 'y' && notEmpty(config.axis_y_padding)) {
  5865. padding_top = $$.axis.getPadding(config.axis_y_padding, 'top', padding_top, domainLength);
  5866. padding_bottom = $$.axis.getPadding(config.axis_y_padding, 'bottom', padding_bottom, domainLength);
  5867. }
  5868. if (axisId === 'y2' && notEmpty(config.axis_y2_padding)) {
  5869. padding_top = $$.axis.getPadding(config.axis_y2_padding, 'top', padding_top, domainLength);
  5870. padding_bottom = $$.axis.getPadding(config.axis_y2_padding, 'bottom', padding_bottom, domainLength);
  5871. }
  5872. // Bar/Area chart should be 0-based if all positive|negative
  5873. if (isZeroBased) {
  5874. if (isAllPositive) {
  5875. padding_bottom = yDomainMin;
  5876. }
  5877. if (isAllNegative) {
  5878. padding_top = -yDomainMax;
  5879. }
  5880. }
  5881. domain = [yDomainMin - padding_bottom, yDomainMax + padding_top];
  5882. return isInverted ? domain.reverse() : domain;
  5883. };
  5884. c3_chart_internal_fn.getXDomainMin = function (targets) {
  5885. var $$ = this,
  5886. config = $$.config;
  5887. return isDefined(config.axis_x_min) ? $$.isTimeSeries() ? this.parseDate(config.axis_x_min) : config.axis_x_min : $$.d3.min(targets, function (t) {
  5888. return $$.d3.min(t.values, function (v) {
  5889. return v.x;
  5890. });
  5891. });
  5892. };
  5893. c3_chart_internal_fn.getXDomainMax = function (targets) {
  5894. var $$ = this,
  5895. config = $$.config;
  5896. return isDefined(config.axis_x_max) ? $$.isTimeSeries() ? this.parseDate(config.axis_x_max) : config.axis_x_max : $$.d3.max(targets, function (t) {
  5897. return $$.d3.max(t.values, function (v) {
  5898. return v.x;
  5899. });
  5900. });
  5901. };
  5902. c3_chart_internal_fn.getXDomainPadding = function (domain) {
  5903. var $$ = this,
  5904. config = $$.config,
  5905. diff = domain[1] - domain[0],
  5906. maxDataCount,
  5907. padding,
  5908. paddingLeft,
  5909. paddingRight;
  5910. if ($$.isCategorized()) {
  5911. padding = 0;
  5912. } else if ($$.hasType('bar')) {
  5913. maxDataCount = $$.getMaxDataCount();
  5914. padding = maxDataCount > 1 ? diff / (maxDataCount - 1) / 2 : 0.5;
  5915. } else {
  5916. padding = diff * 0.01;
  5917. }
  5918. if (_typeof(config.axis_x_padding) === 'object' && notEmpty(config.axis_x_padding)) {
  5919. paddingLeft = isValue(config.axis_x_padding.left) ? config.axis_x_padding.left : padding;
  5920. paddingRight = isValue(config.axis_x_padding.right) ? config.axis_x_padding.right : padding;
  5921. } else if (typeof config.axis_x_padding === 'number') {
  5922. paddingLeft = paddingRight = config.axis_x_padding;
  5923. } else {
  5924. paddingLeft = paddingRight = padding;
  5925. }
  5926. return { left: paddingLeft, right: paddingRight };
  5927. };
  5928. c3_chart_internal_fn.getXDomain = function (targets) {
  5929. var $$ = this,
  5930. xDomain = [$$.getXDomainMin(targets), $$.getXDomainMax(targets)],
  5931. firstX = xDomain[0],
  5932. lastX = xDomain[1],
  5933. padding = $$.getXDomainPadding(xDomain),
  5934. min = 0,
  5935. max = 0;
  5936. // show center of x domain if min and max are the same
  5937. if (firstX - lastX === 0 && !$$.isCategorized()) {
  5938. if ($$.isTimeSeries()) {
  5939. firstX = new Date(firstX.getTime() * 0.5);
  5940. lastX = new Date(lastX.getTime() * 1.5);
  5941. } else {
  5942. firstX = firstX === 0 ? 1 : firstX * 0.5;
  5943. lastX = lastX === 0 ? -1 : lastX * 1.5;
  5944. }
  5945. }
  5946. if (firstX || firstX === 0) {
  5947. min = $$.isTimeSeries() ? new Date(firstX.getTime() - padding.left) : firstX - padding.left;
  5948. }
  5949. if (lastX || lastX === 0) {
  5950. max = $$.isTimeSeries() ? new Date(lastX.getTime() + padding.right) : lastX + padding.right;
  5951. }
  5952. return [min, max];
  5953. };
  5954. c3_chart_internal_fn.updateXDomain = function (targets, withUpdateXDomain, withUpdateOrgXDomain, withTrim, domain) {
  5955. var $$ = this,
  5956. config = $$.config;
  5957. if (withUpdateOrgXDomain) {
  5958. $$.x.domain(domain ? domain : $$.d3.extent($$.getXDomain(targets)));
  5959. $$.orgXDomain = $$.x.domain();
  5960. if (config.zoom_enabled) {
  5961. $$.zoom.scale($$.x).updateScaleExtent();
  5962. }
  5963. $$.subX.domain($$.x.domain());
  5964. if ($$.brush) {
  5965. $$.brush.scale($$.subX);
  5966. }
  5967. }
  5968. if (withUpdateXDomain) {
  5969. $$.x.domain(domain ? domain : !$$.brush || $$.brush.empty() ? $$.orgXDomain : $$.brush.extent());
  5970. if (config.zoom_enabled) {
  5971. $$.zoom.scale($$.x).updateScaleExtent();
  5972. }
  5973. }
  5974. // Trim domain when too big by zoom mousemove event
  5975. if (withTrim) {
  5976. $$.x.domain($$.trimXDomain($$.x.orgDomain()));
  5977. }
  5978. return $$.x.domain();
  5979. };
  5980. c3_chart_internal_fn.trimXDomain = function (domain) {
  5981. var zoomDomain = this.getZoomDomain(),
  5982. min = zoomDomain[0],
  5983. max = zoomDomain[1];
  5984. if (domain[0] <= min) {
  5985. domain[1] = +domain[1] + (min - domain[0]);
  5986. domain[0] = min;
  5987. }
  5988. if (max <= domain[1]) {
  5989. domain[0] = +domain[0] - (domain[1] - max);
  5990. domain[1] = max;
  5991. }
  5992. return domain;
  5993. };
  5994. c3_chart_internal_fn.drag = function (mouse) {
  5995. var $$ = this,
  5996. config = $$.config,
  5997. main = $$.main,
  5998. d3 = $$.d3;
  5999. var sx, sy, mx, my, minX, maxX, minY, maxY;
  6000. if ($$.hasArcType()) {
  6001. return;
  6002. }
  6003. if (!config.data_selection_enabled) {
  6004. return;
  6005. } // do nothing if not selectable
  6006. if (config.zoom_enabled && !$$.zoom.altDomain) {
  6007. return;
  6008. } // skip if zoomable because of conflict drag dehavior
  6009. if (!config.data_selection_multiple) {
  6010. return;
  6011. } // skip when single selection because drag is used for multiple selection
  6012. sx = $$.dragStart[0];
  6013. sy = $$.dragStart[1];
  6014. mx = mouse[0];
  6015. my = mouse[1];
  6016. minX = Math.min(sx, mx);
  6017. maxX = Math.max(sx, mx);
  6018. minY = config.data_selection_grouped ? $$.margin.top : Math.min(sy, my);
  6019. maxY = config.data_selection_grouped ? $$.height : Math.max(sy, my);
  6020. main.select('.' + CLASS.dragarea).attr('x', minX).attr('y', minY).attr('width', maxX - minX).attr('height', maxY - minY);
  6021. // TODO: binary search when multiple xs
  6022. main.selectAll('.' + CLASS.shapes).selectAll('.' + CLASS.shape).filter(function (d) {
  6023. return config.data_selection_isselectable(d);
  6024. }).each(function (d, i) {
  6025. var shape = d3.select(this),
  6026. isSelected = shape.classed(CLASS.SELECTED),
  6027. isIncluded = shape.classed(CLASS.INCLUDED),
  6028. _x,
  6029. _y,
  6030. _w,
  6031. _h,
  6032. toggle,
  6033. isWithin = false,
  6034. box;
  6035. if (shape.classed(CLASS.circle)) {
  6036. _x = shape.attr("cx") * 1;
  6037. _y = shape.attr("cy") * 1;
  6038. toggle = $$.togglePoint;
  6039. isWithin = minX < _x && _x < maxX && minY < _y && _y < maxY;
  6040. } else if (shape.classed(CLASS.bar)) {
  6041. box = getPathBox(this);
  6042. _x = box.x;
  6043. _y = box.y;
  6044. _w = box.width;
  6045. _h = box.height;
  6046. toggle = $$.togglePath;
  6047. isWithin = !(maxX < _x || _x + _w < minX) && !(maxY < _y || _y + _h < minY);
  6048. } else {
  6049. // line/area selection not supported yet
  6050. return;
  6051. }
  6052. if (isWithin ^ isIncluded) {
  6053. shape.classed(CLASS.INCLUDED, !isIncluded);
  6054. // TODO: included/unincluded callback here
  6055. shape.classed(CLASS.SELECTED, !isSelected);
  6056. toggle.call($$, !isSelected, shape, d, i);
  6057. }
  6058. });
  6059. };
  6060. c3_chart_internal_fn.dragstart = function (mouse) {
  6061. var $$ = this,
  6062. config = $$.config;
  6063. if ($$.hasArcType()) {
  6064. return;
  6065. }
  6066. if (!config.data_selection_enabled) {
  6067. return;
  6068. } // do nothing if not selectable
  6069. $$.dragStart = mouse;
  6070. $$.main.select('.' + CLASS.chart).append('rect').attr('class', CLASS.dragarea).style('opacity', 0.1);
  6071. $$.dragging = true;
  6072. };
  6073. c3_chart_internal_fn.dragend = function () {
  6074. var $$ = this,
  6075. config = $$.config;
  6076. if ($$.hasArcType()) {
  6077. return;
  6078. }
  6079. if (!config.data_selection_enabled) {
  6080. return;
  6081. } // do nothing if not selectable
  6082. $$.main.select('.' + CLASS.dragarea).transition().duration(100).style('opacity', 0).remove();
  6083. $$.main.selectAll('.' + CLASS.shape).classed(CLASS.INCLUDED, false);
  6084. $$.dragging = false;
  6085. };
  6086. c3_chart_internal_fn.getYFormat = function (forArc) {
  6087. var $$ = this,
  6088. formatForY = forArc && !$$.hasType('gauge') ? $$.defaultArcValueFormat : $$.yFormat,
  6089. formatForY2 = forArc && !$$.hasType('gauge') ? $$.defaultArcValueFormat : $$.y2Format;
  6090. return function (v, ratio, id) {
  6091. var format = $$.axis.getId(id) === 'y2' ? formatForY2 : formatForY;
  6092. return format.call($$, v, ratio);
  6093. };
  6094. };
  6095. c3_chart_internal_fn.yFormat = function (v) {
  6096. var $$ = this,
  6097. config = $$.config,
  6098. format = config.axis_y_tick_format ? config.axis_y_tick_format : $$.defaultValueFormat;
  6099. return format(v);
  6100. };
  6101. c3_chart_internal_fn.y2Format = function (v) {
  6102. var $$ = this,
  6103. config = $$.config,
  6104. format = config.axis_y2_tick_format ? config.axis_y2_tick_format : $$.defaultValueFormat;
  6105. return format(v);
  6106. };
  6107. c3_chart_internal_fn.defaultValueFormat = function (v) {
  6108. return isValue(v) ? +v : "";
  6109. };
  6110. c3_chart_internal_fn.defaultArcValueFormat = function (v, ratio) {
  6111. return (ratio * 100).toFixed(1) + '%';
  6112. };
  6113. c3_chart_internal_fn.dataLabelFormat = function (targetId) {
  6114. var $$ = this,
  6115. data_labels = $$.config.data_labels,
  6116. format,
  6117. defaultFormat = function defaultFormat(v) {
  6118. return isValue(v) ? +v : "";
  6119. };
  6120. // find format according to axis id
  6121. if (typeof data_labels.format === 'function') {
  6122. format = data_labels.format;
  6123. } else if (_typeof(data_labels.format) === 'object') {
  6124. if (data_labels.format[targetId]) {
  6125. format = data_labels.format[targetId] === true ? defaultFormat : data_labels.format[targetId];
  6126. } else {
  6127. format = function format() {
  6128. return '';
  6129. };
  6130. }
  6131. } else {
  6132. format = defaultFormat;
  6133. }
  6134. return format;
  6135. };
  6136. c3_chart_internal_fn.initGrid = function () {
  6137. var $$ = this,
  6138. config = $$.config,
  6139. d3 = $$.d3;
  6140. $$.grid = $$.main.append('g').attr("clip-path", $$.clipPathForGrid).attr('class', CLASS.grid);
  6141. if (config.grid_x_show) {
  6142. $$.grid.append("g").attr("class", CLASS.xgrids);
  6143. }
  6144. if (config.grid_y_show) {
  6145. $$.grid.append('g').attr('class', CLASS.ygrids);
  6146. }
  6147. if (config.grid_focus_show) {
  6148. $$.grid.append('g').attr("class", CLASS.xgridFocus).append('line').attr('class', CLASS.xgridFocus);
  6149. }
  6150. $$.xgrid = d3.selectAll([]);
  6151. if (!config.grid_lines_front) {
  6152. $$.initGridLines();
  6153. }
  6154. };
  6155. c3_chart_internal_fn.initGridLines = function () {
  6156. var $$ = this,
  6157. d3 = $$.d3;
  6158. $$.gridLines = $$.main.append('g').attr("clip-path", $$.clipPathForGrid).attr('class', CLASS.grid + ' ' + CLASS.gridLines);
  6159. $$.gridLines.append('g').attr("class", CLASS.xgridLines);
  6160. $$.gridLines.append('g').attr('class', CLASS.ygridLines);
  6161. $$.xgridLines = d3.selectAll([]);
  6162. };
  6163. c3_chart_internal_fn.updateXGrid = function (withoutUpdate) {
  6164. var $$ = this,
  6165. config = $$.config,
  6166. d3 = $$.d3,
  6167. xgridData = $$.generateGridData(config.grid_x_type, $$.x),
  6168. tickOffset = $$.isCategorized() ? $$.xAxis.tickOffset() : 0;
  6169. $$.xgridAttr = config.axis_rotated ? {
  6170. 'x1': 0,
  6171. 'x2': $$.width,
  6172. 'y1': function y1(d) {
  6173. return $$.x(d) - tickOffset;
  6174. },
  6175. 'y2': function y2(d) {
  6176. return $$.x(d) - tickOffset;
  6177. }
  6178. } : {
  6179. 'x1': function x1(d) {
  6180. return $$.x(d) + tickOffset;
  6181. },
  6182. 'x2': function x2(d) {
  6183. return $$.x(d) + tickOffset;
  6184. },
  6185. 'y1': 0,
  6186. 'y2': $$.height
  6187. };
  6188. $$.xgrid = $$.main.select('.' + CLASS.xgrids).selectAll('.' + CLASS.xgrid).data(xgridData);
  6189. $$.xgrid.enter().append('line').attr("class", CLASS.xgrid);
  6190. if (!withoutUpdate) {
  6191. $$.xgrid.attr($$.xgridAttr).style("opacity", function () {
  6192. return +d3.select(this).attr(config.axis_rotated ? 'y1' : 'x1') === (config.axis_rotated ? $$.height : 0) ? 0 : 1;
  6193. });
  6194. }
  6195. $$.xgrid.exit().remove();
  6196. };
  6197. c3_chart_internal_fn.updateYGrid = function () {
  6198. var $$ = this,
  6199. config = $$.config,
  6200. gridValues = $$.yAxis.tickValues() || $$.y.ticks(config.grid_y_ticks);
  6201. $$.ygrid = $$.main.select('.' + CLASS.ygrids).selectAll('.' + CLASS.ygrid).data(gridValues);
  6202. $$.ygrid.enter().append('line').attr('class', CLASS.ygrid);
  6203. $$.ygrid.attr("x1", config.axis_rotated ? $$.y : 0).attr("x2", config.axis_rotated ? $$.y : $$.width).attr("y1", config.axis_rotated ? 0 : $$.y).attr("y2", config.axis_rotated ? $$.height : $$.y);
  6204. $$.ygrid.exit().remove();
  6205. $$.smoothLines($$.ygrid, 'grid');
  6206. };
  6207. c3_chart_internal_fn.gridTextAnchor = function (d) {
  6208. return d.position ? d.position : "end";
  6209. };
  6210. c3_chart_internal_fn.gridTextDx = function (d) {
  6211. return d.position === 'start' ? 4 : d.position === 'middle' ? 0 : -4;
  6212. };
  6213. c3_chart_internal_fn.xGridTextX = function (d) {
  6214. return d.position === 'start' ? -this.height : d.position === 'middle' ? -this.height / 2 : 0;
  6215. };
  6216. c3_chart_internal_fn.yGridTextX = function (d) {
  6217. return d.position === 'start' ? 0 : d.position === 'middle' ? this.width / 2 : this.width;
  6218. };
  6219. c3_chart_internal_fn.updateGrid = function (duration) {
  6220. var $$ = this,
  6221. main = $$.main,
  6222. config = $$.config,
  6223. xgridLine,
  6224. ygridLine,
  6225. yv;
  6226. // hide if arc type
  6227. $$.grid.style('visibility', $$.hasArcType() ? 'hidden' : 'visible');
  6228. main.select('line.' + CLASS.xgridFocus).style("visibility", "hidden");
  6229. if (config.grid_x_show) {
  6230. $$.updateXGrid();
  6231. }
  6232. $$.xgridLines = main.select('.' + CLASS.xgridLines).selectAll('.' + CLASS.xgridLine).data(config.grid_x_lines);
  6233. // enter
  6234. xgridLine = $$.xgridLines.enter().append('g').attr("class", function (d) {
  6235. return CLASS.xgridLine + (d['class'] ? ' ' + d['class'] : '');
  6236. });
  6237. xgridLine.append('line').style("opacity", 0);
  6238. xgridLine.append('text').attr("text-anchor", $$.gridTextAnchor).attr("transform", config.axis_rotated ? "" : "rotate(-90)").attr('dx', $$.gridTextDx).attr('dy', -5).style("opacity", 0);
  6239. // udpate
  6240. // done in d3.transition() of the end of this function
  6241. // exit
  6242. $$.xgridLines.exit().transition().duration(duration).style("opacity", 0).remove();
  6243. // Y-Grid
  6244. if (config.grid_y_show) {
  6245. $$.updateYGrid();
  6246. }
  6247. $$.ygridLines = main.select('.' + CLASS.ygridLines).selectAll('.' + CLASS.ygridLine).data(config.grid_y_lines);
  6248. // enter
  6249. ygridLine = $$.ygridLines.enter().append('g').attr("class", function (d) {
  6250. return CLASS.ygridLine + (d['class'] ? ' ' + d['class'] : '');
  6251. });
  6252. ygridLine.append('line').style("opacity", 0);
  6253. ygridLine.append('text').attr("text-anchor", $$.gridTextAnchor).attr("transform", config.axis_rotated ? "rotate(-90)" : "").attr('dx', $$.gridTextDx).attr('dy', -5).style("opacity", 0);
  6254. // update
  6255. yv = $$.yv.bind($$);
  6256. $$.ygridLines.select('line').transition().duration(duration).attr("x1", config.axis_rotated ? yv : 0).attr("x2", config.axis_rotated ? yv : $$.width).attr("y1", config.axis_rotated ? 0 : yv).attr("y2", config.axis_rotated ? $$.height : yv).style("opacity", 1);
  6257. $$.ygridLines.select('text').transition().duration(duration).attr("x", config.axis_rotated ? $$.xGridTextX.bind($$) : $$.yGridTextX.bind($$)).attr("y", yv).text(function (d) {
  6258. return d.text;
  6259. }).style("opacity", 1);
  6260. // exit
  6261. $$.ygridLines.exit().transition().duration(duration).style("opacity", 0).remove();
  6262. };
  6263. c3_chart_internal_fn.redrawGrid = function (withTransition) {
  6264. var $$ = this,
  6265. config = $$.config,
  6266. xv = $$.xv.bind($$),
  6267. lines = $$.xgridLines.select('line'),
  6268. texts = $$.xgridLines.select('text');
  6269. return [(withTransition ? lines.transition() : lines).attr("x1", config.axis_rotated ? 0 : xv).attr("x2", config.axis_rotated ? $$.width : xv).attr("y1", config.axis_rotated ? xv : 0).attr("y2", config.axis_rotated ? xv : $$.height).style("opacity", 1), (withTransition ? texts.transition() : texts).attr("x", config.axis_rotated ? $$.yGridTextX.bind($$) : $$.xGridTextX.bind($$)).attr("y", xv).text(function (d) {
  6270. return d.text;
  6271. }).style("opacity", 1)];
  6272. };
  6273. c3_chart_internal_fn.showXGridFocus = function (selectedData) {
  6274. var $$ = this,
  6275. config = $$.config,
  6276. dataToShow = selectedData.filter(function (d) {
  6277. return d && isValue(d.value);
  6278. }),
  6279. focusEl = $$.main.selectAll('line.' + CLASS.xgridFocus),
  6280. xx = $$.xx.bind($$);
  6281. if (!config.tooltip_show) {
  6282. return;
  6283. }
  6284. // Hide when scatter plot exists
  6285. if ($$.hasType('scatter') || $$.hasArcType()) {
  6286. return;
  6287. }
  6288. focusEl.style("visibility", "visible").data([dataToShow[0]]).attr(config.axis_rotated ? 'y1' : 'x1', xx).attr(config.axis_rotated ? 'y2' : 'x2', xx);
  6289. $$.smoothLines(focusEl, 'grid');
  6290. };
  6291. c3_chart_internal_fn.hideXGridFocus = function () {
  6292. this.main.select('line.' + CLASS.xgridFocus).style("visibility", "hidden");
  6293. };
  6294. c3_chart_internal_fn.updateXgridFocus = function () {
  6295. var $$ = this,
  6296. config = $$.config;
  6297. $$.main.select('line.' + CLASS.xgridFocus).attr("x1", config.axis_rotated ? 0 : -10).attr("x2", config.axis_rotated ? $$.width : -10).attr("y1", config.axis_rotated ? -10 : 0).attr("y2", config.axis_rotated ? -10 : $$.height);
  6298. };
  6299. c3_chart_internal_fn.generateGridData = function (type, scale) {
  6300. var $$ = this,
  6301. gridData = [],
  6302. xDomain,
  6303. firstYear,
  6304. lastYear,
  6305. i,
  6306. tickNum = $$.main.select("." + CLASS.axisX).selectAll('.tick').size();
  6307. if (type === 'year') {
  6308. xDomain = $$.getXDomain();
  6309. firstYear = xDomain[0].getFullYear();
  6310. lastYear = xDomain[1].getFullYear();
  6311. for (i = firstYear; i <= lastYear; i++) {
  6312. gridData.push(new Date(i + '-01-01 00:00:00'));
  6313. }
  6314. } else {
  6315. gridData = scale.ticks(10);
  6316. if (gridData.length > tickNum) {
  6317. // use only int
  6318. gridData = gridData.filter(function (d) {
  6319. return ("" + d).indexOf('.') < 0;
  6320. });
  6321. }
  6322. }
  6323. return gridData;
  6324. };
  6325. c3_chart_internal_fn.getGridFilterToRemove = function (params) {
  6326. return params ? function (line) {
  6327. var found = false;
  6328. [].concat(params).forEach(function (param) {
  6329. if ('value' in param && line.value === param.value || 'class' in param && line['class'] === param['class']) {
  6330. found = true;
  6331. }
  6332. });
  6333. return found;
  6334. } : function () {
  6335. return true;
  6336. };
  6337. };
  6338. c3_chart_internal_fn.removeGridLines = function (params, forX) {
  6339. var $$ = this,
  6340. config = $$.config,
  6341. toRemove = $$.getGridFilterToRemove(params),
  6342. toShow = function toShow(line) {
  6343. return !toRemove(line);
  6344. },
  6345. classLines = forX ? CLASS.xgridLines : CLASS.ygridLines,
  6346. classLine = forX ? CLASS.xgridLine : CLASS.ygridLine;
  6347. $$.main.select('.' + classLines).selectAll('.' + classLine).filter(toRemove).transition().duration(config.transition_duration).style('opacity', 0).remove();
  6348. if (forX) {
  6349. config.grid_x_lines = config.grid_x_lines.filter(toShow);
  6350. } else {
  6351. config.grid_y_lines = config.grid_y_lines.filter(toShow);
  6352. }
  6353. };
  6354. c3_chart_internal_fn.initEventRect = function () {
  6355. var $$ = this;
  6356. $$.main.select('.' + CLASS.chart).append("g").attr("class", CLASS.eventRects).style('fill-opacity', 0);
  6357. };
  6358. c3_chart_internal_fn.redrawEventRect = function () {
  6359. var $$ = this,
  6360. config = $$.config,
  6361. eventRectUpdate,
  6362. maxDataCountTarget,
  6363. isMultipleX = $$.isMultipleX();
  6364. // rects for mouseover
  6365. var eventRects = $$.main.select('.' + CLASS.eventRects).style('cursor', config.zoom_enabled ? config.axis_rotated ? 'ns-resize' : 'ew-resize' : null).classed(CLASS.eventRectsMultiple, isMultipleX).classed(CLASS.eventRectsSingle, !isMultipleX);
  6366. // clear old rects
  6367. eventRects.selectAll('.' + CLASS.eventRect).remove();
  6368. // open as public variable
  6369. $$.eventRect = eventRects.selectAll('.' + CLASS.eventRect);
  6370. if (isMultipleX) {
  6371. eventRectUpdate = $$.eventRect.data([0]);
  6372. // enter : only one rect will be added
  6373. $$.generateEventRectsForMultipleXs(eventRectUpdate.enter());
  6374. // update
  6375. $$.updateEventRect(eventRectUpdate);
  6376. // exit : not needed because always only one rect exists
  6377. } else {
  6378. // Set data and update $$.eventRect
  6379. maxDataCountTarget = $$.getMaxDataCountTarget($$.data.targets);
  6380. eventRects.datum(maxDataCountTarget ? maxDataCountTarget.values : []);
  6381. $$.eventRect = eventRects.selectAll('.' + CLASS.eventRect);
  6382. eventRectUpdate = $$.eventRect.data(function (d) {
  6383. return d;
  6384. });
  6385. // enter
  6386. $$.generateEventRectsForSingleX(eventRectUpdate.enter());
  6387. // update
  6388. $$.updateEventRect(eventRectUpdate);
  6389. // exit
  6390. eventRectUpdate.exit().remove();
  6391. }
  6392. };
  6393. c3_chart_internal_fn.updateEventRect = function (eventRectUpdate) {
  6394. var $$ = this,
  6395. config = $$.config,
  6396. x,
  6397. y,
  6398. w,
  6399. h,
  6400. rectW,
  6401. rectX;
  6402. // set update selection if null
  6403. eventRectUpdate = eventRectUpdate || $$.eventRect.data(function (d) {
  6404. return d;
  6405. });
  6406. if ($$.isMultipleX()) {
  6407. // TODO: rotated not supported yet
  6408. x = 0;
  6409. y = 0;
  6410. w = $$.width;
  6411. h = $$.height;
  6412. } else {
  6413. if (($$.isCustomX() || $$.isTimeSeries()) && !$$.isCategorized()) {
  6414. // update index for x that is used by prevX and nextX
  6415. $$.updateXs();
  6416. rectW = function rectW(d) {
  6417. var prevX = $$.getPrevX(d.index),
  6418. nextX = $$.getNextX(d.index);
  6419. // if there this is a single data point make the eventRect full width (or height)
  6420. if (prevX === null && nextX === null) {
  6421. return config.axis_rotated ? $$.height : $$.width;
  6422. }
  6423. if (prevX === null) {
  6424. prevX = $$.x.domain()[0];
  6425. }
  6426. if (nextX === null) {
  6427. nextX = $$.x.domain()[1];
  6428. }
  6429. return Math.max(0, ($$.x(nextX) - $$.x(prevX)) / 2);
  6430. };
  6431. rectX = function rectX(d) {
  6432. var prevX = $$.getPrevX(d.index),
  6433. nextX = $$.getNextX(d.index),
  6434. thisX = $$.data.xs[d.id][d.index];
  6435. // if there this is a single data point position the eventRect at 0
  6436. if (prevX === null && nextX === null) {
  6437. return 0;
  6438. }
  6439. if (prevX === null) {
  6440. prevX = $$.x.domain()[0];
  6441. }
  6442. return ($$.x(thisX) + $$.x(prevX)) / 2;
  6443. };
  6444. } else {
  6445. rectW = $$.getEventRectWidth();
  6446. rectX = function rectX(d) {
  6447. return $$.x(d.x) - rectW / 2;
  6448. };
  6449. }
  6450. x = config.axis_rotated ? 0 : rectX;
  6451. y = config.axis_rotated ? rectX : 0;
  6452. w = config.axis_rotated ? $$.width : rectW;
  6453. h = config.axis_rotated ? rectW : $$.height;
  6454. }
  6455. eventRectUpdate.attr('class', $$.classEvent.bind($$)).attr("x", x).attr("y", y).attr("width", w).attr("height", h);
  6456. };
  6457. c3_chart_internal_fn.generateEventRectsForSingleX = function (eventRectEnter) {
  6458. var $$ = this,
  6459. d3 = $$.d3,
  6460. config = $$.config;
  6461. eventRectEnter.append("rect").attr("class", $$.classEvent.bind($$)).style("cursor", config.data_selection_enabled && config.data_selection_grouped ? "pointer" : null).on('mouseover', function (d) {
  6462. var index = d.index;
  6463. if ($$.dragging || $$.flowing) {
  6464. return;
  6465. } // do nothing while dragging/flowing
  6466. if ($$.hasArcType()) {
  6467. return;
  6468. }
  6469. // Expand shapes for selection
  6470. if (config.point_focus_expand_enabled) {
  6471. $$.expandCircles(index, null, true);
  6472. }
  6473. $$.expandBars(index, null, true);
  6474. // Call event handler
  6475. $$.main.selectAll('.' + CLASS.shape + '-' + index).each(function (d) {
  6476. config.data_onmouseover.call($$.api, d);
  6477. });
  6478. }).on('mouseout', function (d) {
  6479. var index = d.index;
  6480. if (!$$.config) {
  6481. return;
  6482. } // chart is destroyed
  6483. if ($$.hasArcType()) {
  6484. return;
  6485. }
  6486. $$.hideXGridFocus();
  6487. $$.hideTooltip();
  6488. // Undo expanded shapes
  6489. $$.unexpandCircles();
  6490. $$.unexpandBars();
  6491. // Call event handler
  6492. $$.main.selectAll('.' + CLASS.shape + '-' + index).each(function (d) {
  6493. config.data_onmouseout.call($$.api, d);
  6494. });
  6495. }).on('mousemove', function (d) {
  6496. var selectedData,
  6497. index = d.index,
  6498. eventRect = $$.svg.select('.' + CLASS.eventRect + '-' + index);
  6499. if ($$.dragging || $$.flowing) {
  6500. return;
  6501. } // do nothing while dragging/flowing
  6502. if ($$.hasArcType()) {
  6503. return;
  6504. }
  6505. if ($$.isStepType(d) && $$.config.line_step_type === 'step-after' && d3.mouse(this)[0] < $$.x($$.getXValue(d.id, index))) {
  6506. index -= 1;
  6507. }
  6508. // Show tooltip
  6509. selectedData = $$.filterTargetsToShow($$.data.targets).map(function (t) {
  6510. return $$.addName($$.getValueOnIndex(t.values, index));
  6511. });
  6512. if (config.tooltip_grouped) {
  6513. $$.showTooltip(selectedData, this);
  6514. $$.showXGridFocus(selectedData);
  6515. }
  6516. if (config.tooltip_grouped && (!config.data_selection_enabled || config.data_selection_grouped)) {
  6517. return;
  6518. }
  6519. $$.main.selectAll('.' + CLASS.shape + '-' + index).each(function () {
  6520. d3.select(this).classed(CLASS.EXPANDED, true);
  6521. if (config.data_selection_enabled) {
  6522. eventRect.style('cursor', config.data_selection_grouped ? 'pointer' : null);
  6523. }
  6524. if (!config.tooltip_grouped) {
  6525. $$.hideXGridFocus();
  6526. $$.hideTooltip();
  6527. if (!config.data_selection_grouped) {
  6528. $$.unexpandCircles(index);
  6529. $$.unexpandBars(index);
  6530. }
  6531. }
  6532. }).filter(function (d) {
  6533. return $$.isWithinShape(this, d);
  6534. }).each(function (d) {
  6535. if (config.data_selection_enabled && (config.data_selection_grouped || config.data_selection_isselectable(d))) {
  6536. eventRect.style('cursor', 'pointer');
  6537. }
  6538. if (!config.tooltip_grouped) {
  6539. $$.showTooltip([d], this);
  6540. $$.showXGridFocus([d]);
  6541. if (config.point_focus_expand_enabled) {
  6542. $$.expandCircles(index, d.id, true);
  6543. }
  6544. $$.expandBars(index, d.id, true);
  6545. }
  6546. });
  6547. }).on('click', function (d) {
  6548. var index = d.index;
  6549. if ($$.hasArcType() || !$$.toggleShape) {
  6550. return;
  6551. }
  6552. if ($$.cancelClick) {
  6553. $$.cancelClick = false;
  6554. return;
  6555. }
  6556. if ($$.isStepType(d) && config.line_step_type === 'step-after' && d3.mouse(this)[0] < $$.x($$.getXValue(d.id, index))) {
  6557. index -= 1;
  6558. }
  6559. $$.main.selectAll('.' + CLASS.shape + '-' + index).each(function (d) {
  6560. if (config.data_selection_grouped || $$.isWithinShape(this, d)) {
  6561. $$.toggleShape(this, d, index);
  6562. $$.config.data_onclick.call($$.api, d, this);
  6563. }
  6564. });
  6565. }).call(config.data_selection_draggable && $$.drag ? d3.behavior.drag().origin(Object).on('drag', function () {
  6566. $$.drag(d3.mouse(this));
  6567. }).on('dragstart', function () {
  6568. $$.dragstart(d3.mouse(this));
  6569. }).on('dragend', function () {
  6570. $$.dragend();
  6571. }) : function () {});
  6572. };
  6573. c3_chart_internal_fn.generateEventRectsForMultipleXs = function (eventRectEnter) {
  6574. var $$ = this,
  6575. d3 = $$.d3,
  6576. config = $$.config;
  6577. function mouseout() {
  6578. $$.svg.select('.' + CLASS.eventRect).style('cursor', null);
  6579. $$.hideXGridFocus();
  6580. $$.hideTooltip();
  6581. $$.unexpandCircles();
  6582. $$.unexpandBars();
  6583. }
  6584. eventRectEnter.append('rect').attr('x', 0).attr('y', 0).attr('width', $$.width).attr('height', $$.height).attr('class', CLASS.eventRect).on('mouseout', function () {
  6585. if (!$$.config) {
  6586. return;
  6587. } // chart is destroyed
  6588. if ($$.hasArcType()) {
  6589. return;
  6590. }
  6591. mouseout();
  6592. }).on('mousemove', function () {
  6593. var targetsToShow = $$.filterTargetsToShow($$.data.targets);
  6594. var mouse, closest, sameXData, selectedData;
  6595. if ($$.dragging) {
  6596. return;
  6597. } // do nothing when dragging
  6598. if ($$.hasArcType(targetsToShow)) {
  6599. return;
  6600. }
  6601. mouse = d3.mouse(this);
  6602. closest = $$.findClosestFromTargets(targetsToShow, mouse);
  6603. if ($$.mouseover && (!closest || closest.id !== $$.mouseover.id)) {
  6604. config.data_onmouseout.call($$.api, $$.mouseover);
  6605. $$.mouseover = undefined;
  6606. }
  6607. if (!closest) {
  6608. mouseout();
  6609. return;
  6610. }
  6611. if ($$.isScatterType(closest) || !config.tooltip_grouped) {
  6612. sameXData = [closest];
  6613. } else {
  6614. sameXData = $$.filterByX(targetsToShow, closest.x);
  6615. }
  6616. // show tooltip when cursor is close to some point
  6617. selectedData = sameXData.map(function (d) {
  6618. return $$.addName(d);
  6619. });
  6620. $$.showTooltip(selectedData, this);
  6621. // expand points
  6622. if (config.point_focus_expand_enabled) {
  6623. $$.expandCircles(closest.index, closest.id, true);
  6624. }
  6625. $$.expandBars(closest.index, closest.id, true);
  6626. // Show xgrid focus line
  6627. $$.showXGridFocus(selectedData);
  6628. // Show cursor as pointer if point is close to mouse position
  6629. if ($$.isBarType(closest.id) || $$.dist(closest, mouse) < config.point_sensitivity) {
  6630. $$.svg.select('.' + CLASS.eventRect).style('cursor', 'pointer');
  6631. if (!$$.mouseover) {
  6632. config.data_onmouseover.call($$.api, closest);
  6633. $$.mouseover = closest;
  6634. }
  6635. }
  6636. }).on('click', function () {
  6637. var targetsToShow = $$.filterTargetsToShow($$.data.targets);
  6638. var mouse, closest;
  6639. if ($$.hasArcType(targetsToShow)) {
  6640. return;
  6641. }
  6642. mouse = d3.mouse(this);
  6643. closest = $$.findClosestFromTargets(targetsToShow, mouse);
  6644. if (!closest) {
  6645. return;
  6646. }
  6647. // select if selection enabled
  6648. if ($$.isBarType(closest.id) || $$.dist(closest, mouse) < config.point_sensitivity) {
  6649. $$.main.selectAll('.' + CLASS.shapes + $$.getTargetSelectorSuffix(closest.id)).selectAll('.' + CLASS.shape + '-' + closest.index).each(function () {
  6650. if (config.data_selection_grouped || $$.isWithinShape(this, closest)) {
  6651. $$.toggleShape(this, closest, closest.index);
  6652. $$.config.data_onclick.call($$.api, closest, this);
  6653. }
  6654. });
  6655. }
  6656. }).call(config.data_selection_draggable && $$.drag ? d3.behavior.drag().origin(Object).on('drag', function () {
  6657. $$.drag(d3.mouse(this));
  6658. }).on('dragstart', function () {
  6659. $$.dragstart(d3.mouse(this));
  6660. }).on('dragend', function () {
  6661. $$.dragend();
  6662. }) : function () {});
  6663. };
  6664. c3_chart_internal_fn.dispatchEvent = function (type, index, mouse) {
  6665. var $$ = this,
  6666. selector = '.' + CLASS.eventRect + (!$$.isMultipleX() ? '-' + index : ''),
  6667. eventRect = $$.main.select(selector).node(),
  6668. box = eventRect.getBoundingClientRect(),
  6669. x = box.left + (mouse ? mouse[0] : 0),
  6670. y = box.top + (mouse ? mouse[1] : 0),
  6671. event = document.createEvent("MouseEvents");
  6672. event.initMouseEvent(type, true, true, window, 0, x, y, x, y, false, false, false, false, 0, null);
  6673. eventRect.dispatchEvent(event);
  6674. };
  6675. c3_chart_internal_fn.initLegend = function () {
  6676. var $$ = this;
  6677. $$.legendItemTextBox = {};
  6678. $$.legendHasRendered = false;
  6679. $$.legend = $$.svg.append("g").attr("transform", $$.getTranslate('legend'));
  6680. if (!$$.config.legend_show) {
  6681. $$.legend.style('visibility', 'hidden');
  6682. $$.hiddenLegendIds = $$.mapToIds($$.data.targets);
  6683. return;
  6684. }
  6685. // MEMO: call here to update legend box and tranlate for all
  6686. // MEMO: translate will be upated by this, so transform not needed in updateLegend()
  6687. $$.updateLegendWithDefaults();
  6688. };
  6689. c3_chart_internal_fn.updateLegendWithDefaults = function () {
  6690. var $$ = this;
  6691. $$.updateLegend($$.mapToIds($$.data.targets), { withTransform: false, withTransitionForTransform: false, withTransition: false });
  6692. };
  6693. c3_chart_internal_fn.updateSizeForLegend = function (legendHeight, legendWidth) {
  6694. var $$ = this,
  6695. config = $$.config,
  6696. insetLegendPosition = {
  6697. top: $$.isLegendTop ? $$.getCurrentPaddingTop() + config.legend_inset_y + 5.5 : $$.currentHeight - legendHeight - $$.getCurrentPaddingBottom() - config.legend_inset_y,
  6698. left: $$.isLegendLeft ? $$.getCurrentPaddingLeft() + config.legend_inset_x + 0.5 : $$.currentWidth - legendWidth - $$.getCurrentPaddingRight() - config.legend_inset_x + 0.5
  6699. };
  6700. $$.margin3 = {
  6701. top: $$.isLegendRight ? 0 : $$.isLegendInset ? insetLegendPosition.top : $$.currentHeight - legendHeight,
  6702. right: NaN,
  6703. bottom: 0,
  6704. left: $$.isLegendRight ? $$.currentWidth - legendWidth : $$.isLegendInset ? insetLegendPosition.left : 0
  6705. };
  6706. };
  6707. c3_chart_internal_fn.transformLegend = function (withTransition) {
  6708. var $$ = this;
  6709. (withTransition ? $$.legend.transition() : $$.legend).attr("transform", $$.getTranslate('legend'));
  6710. };
  6711. c3_chart_internal_fn.updateLegendStep = function (step) {
  6712. this.legendStep = step;
  6713. };
  6714. c3_chart_internal_fn.updateLegendItemWidth = function (w) {
  6715. this.legendItemWidth = w;
  6716. };
  6717. c3_chart_internal_fn.updateLegendItemHeight = function (h) {
  6718. this.legendItemHeight = h;
  6719. };
  6720. c3_chart_internal_fn.getLegendWidth = function () {
  6721. var $$ = this;
  6722. return $$.config.legend_show ? $$.isLegendRight || $$.isLegendInset ? $$.legendItemWidth * ($$.legendStep + 1) : $$.currentWidth : 0;
  6723. };
  6724. c3_chart_internal_fn.getLegendHeight = function () {
  6725. var $$ = this,
  6726. h = 0;
  6727. if ($$.config.legend_show) {
  6728. if ($$.isLegendRight) {
  6729. h = $$.currentHeight;
  6730. } else {
  6731. h = Math.max(20, $$.legendItemHeight) * ($$.legendStep + 1);
  6732. }
  6733. }
  6734. return h;
  6735. };
  6736. c3_chart_internal_fn.opacityForLegend = function (legendItem) {
  6737. return legendItem.classed(CLASS.legendItemHidden) ? null : 1;
  6738. };
  6739. c3_chart_internal_fn.opacityForUnfocusedLegend = function (legendItem) {
  6740. return legendItem.classed(CLASS.legendItemHidden) ? null : 0.3;
  6741. };
  6742. c3_chart_internal_fn.toggleFocusLegend = function (targetIds, focus) {
  6743. var $$ = this;
  6744. targetIds = $$.mapToTargetIds(targetIds);
  6745. $$.legend.selectAll('.' + CLASS.legendItem).filter(function (id) {
  6746. return targetIds.indexOf(id) >= 0;
  6747. }).classed(CLASS.legendItemFocused, focus).transition().duration(100).style('opacity', function () {
  6748. var opacity = focus ? $$.opacityForLegend : $$.opacityForUnfocusedLegend;
  6749. return opacity.call($$, $$.d3.select(this));
  6750. });
  6751. };
  6752. c3_chart_internal_fn.revertLegend = function () {
  6753. var $$ = this,
  6754. d3 = $$.d3;
  6755. $$.legend.selectAll('.' + CLASS.legendItem).classed(CLASS.legendItemFocused, false).transition().duration(100).style('opacity', function () {
  6756. return $$.opacityForLegend(d3.select(this));
  6757. });
  6758. };
  6759. c3_chart_internal_fn.showLegend = function (targetIds) {
  6760. var $$ = this,
  6761. config = $$.config;
  6762. if (!config.legend_show) {
  6763. config.legend_show = true;
  6764. $$.legend.style('visibility', 'visible');
  6765. if (!$$.legendHasRendered) {
  6766. $$.updateLegendWithDefaults();
  6767. }
  6768. }
  6769. $$.removeHiddenLegendIds(targetIds);
  6770. $$.legend.selectAll($$.selectorLegends(targetIds)).style('visibility', 'visible').transition().style('opacity', function () {
  6771. return $$.opacityForLegend($$.d3.select(this));
  6772. });
  6773. };
  6774. c3_chart_internal_fn.hideLegend = function (targetIds) {
  6775. var $$ = this,
  6776. config = $$.config;
  6777. if (config.legend_show && isEmpty(targetIds)) {
  6778. config.legend_show = false;
  6779. $$.legend.style('visibility', 'hidden');
  6780. }
  6781. $$.addHiddenLegendIds(targetIds);
  6782. $$.legend.selectAll($$.selectorLegends(targetIds)).style('opacity', 0).style('visibility', 'hidden');
  6783. };
  6784. c3_chart_internal_fn.clearLegendItemTextBoxCache = function () {
  6785. this.legendItemTextBox = {};
  6786. };
  6787. c3_chart_internal_fn.updateLegend = function (targetIds, options, transitions) {
  6788. var $$ = this,
  6789. config = $$.config;
  6790. var xForLegend, xForLegendText, xForLegendRect, yForLegend, yForLegendText, yForLegendRect, x1ForLegendTile, x2ForLegendTile, yForLegendTile;
  6791. var paddingTop = 4,
  6792. paddingRight = 10,
  6793. maxWidth = 0,
  6794. maxHeight = 0,
  6795. posMin = 10,
  6796. tileWidth = config.legend_item_tile_width + 5;
  6797. var l,
  6798. totalLength = 0,
  6799. offsets = {},
  6800. widths = {},
  6801. heights = {},
  6802. margins = [0],
  6803. steps = {},
  6804. step = 0;
  6805. var withTransition, withTransitionForTransform;
  6806. var texts, rects, tiles, background;
  6807. // Skip elements when their name is set to null
  6808. targetIds = targetIds.filter(function (id) {
  6809. return !isDefined(config.data_names[id]) || config.data_names[id] !== null;
  6810. });
  6811. options = options || {};
  6812. withTransition = getOption(options, "withTransition", true);
  6813. withTransitionForTransform = getOption(options, "withTransitionForTransform", true);
  6814. function getTextBox(textElement, id) {
  6815. if (!$$.legendItemTextBox[id]) {
  6816. $$.legendItemTextBox[id] = $$.getTextRect(textElement.textContent, CLASS.legendItem, textElement);
  6817. }
  6818. return $$.legendItemTextBox[id];
  6819. }
  6820. function updatePositions(textElement, id, index) {
  6821. var reset = index === 0,
  6822. isLast = index === targetIds.length - 1,
  6823. box = getTextBox(textElement, id),
  6824. itemWidth = box.width + tileWidth + (isLast && !($$.isLegendRight || $$.isLegendInset) ? 0 : paddingRight) + config.legend_padding,
  6825. itemHeight = box.height + paddingTop,
  6826. itemLength = $$.isLegendRight || $$.isLegendInset ? itemHeight : itemWidth,
  6827. areaLength = $$.isLegendRight || $$.isLegendInset ? $$.getLegendHeight() : $$.getLegendWidth(),
  6828. margin,
  6829. maxLength;
  6830. // MEMO: care about condifion of step, totalLength
  6831. function updateValues(id, withoutStep) {
  6832. if (!withoutStep) {
  6833. margin = (areaLength - totalLength - itemLength) / 2;
  6834. if (margin < posMin) {
  6835. margin = (areaLength - itemLength) / 2;
  6836. totalLength = 0;
  6837. step++;
  6838. }
  6839. }
  6840. steps[id] = step;
  6841. margins[step] = $$.isLegendInset ? 10 : margin;
  6842. offsets[id] = totalLength;
  6843. totalLength += itemLength;
  6844. }
  6845. if (reset) {
  6846. totalLength = 0;
  6847. step = 0;
  6848. maxWidth = 0;
  6849. maxHeight = 0;
  6850. }
  6851. if (config.legend_show && !$$.isLegendToShow(id)) {
  6852. widths[id] = heights[id] = steps[id] = offsets[id] = 0;
  6853. return;
  6854. }
  6855. widths[id] = itemWidth;
  6856. heights[id] = itemHeight;
  6857. if (!maxWidth || itemWidth >= maxWidth) {
  6858. maxWidth = itemWidth;
  6859. }
  6860. if (!maxHeight || itemHeight >= maxHeight) {
  6861. maxHeight = itemHeight;
  6862. }
  6863. maxLength = $$.isLegendRight || $$.isLegendInset ? maxHeight : maxWidth;
  6864. if (config.legend_equally) {
  6865. Object.keys(widths).forEach(function (id) {
  6866. widths[id] = maxWidth;
  6867. });
  6868. Object.keys(heights).forEach(function (id) {
  6869. heights[id] = maxHeight;
  6870. });
  6871. margin = (areaLength - maxLength * targetIds.length) / 2;
  6872. if (margin < posMin) {
  6873. totalLength = 0;
  6874. step = 0;
  6875. targetIds.forEach(function (id) {
  6876. updateValues(id);
  6877. });
  6878. } else {
  6879. updateValues(id, true);
  6880. }
  6881. } else {
  6882. updateValues(id);
  6883. }
  6884. }
  6885. if ($$.isLegendInset) {
  6886. step = config.legend_inset_step ? config.legend_inset_step : targetIds.length;
  6887. $$.updateLegendStep(step);
  6888. }
  6889. if ($$.isLegendRight) {
  6890. xForLegend = function xForLegend(id) {
  6891. return maxWidth * steps[id];
  6892. };
  6893. yForLegend = function yForLegend(id) {
  6894. return margins[steps[id]] + offsets[id];
  6895. };
  6896. } else if ($$.isLegendInset) {
  6897. xForLegend = function xForLegend(id) {
  6898. return maxWidth * steps[id] + 10;
  6899. };
  6900. yForLegend = function yForLegend(id) {
  6901. return margins[steps[id]] + offsets[id];
  6902. };
  6903. } else {
  6904. xForLegend = function xForLegend(id) {
  6905. return margins[steps[id]] + offsets[id];
  6906. };
  6907. yForLegend = function yForLegend(id) {
  6908. return maxHeight * steps[id];
  6909. };
  6910. }
  6911. xForLegendText = function xForLegendText(id, i) {
  6912. return xForLegend(id, i) + 4 + config.legend_item_tile_width;
  6913. };
  6914. yForLegendText = function yForLegendText(id, i) {
  6915. return yForLegend(id, i) + 9;
  6916. };
  6917. xForLegendRect = function xForLegendRect(id, i) {
  6918. return xForLegend(id, i);
  6919. };
  6920. yForLegendRect = function yForLegendRect(id, i) {
  6921. return yForLegend(id, i) - 5;
  6922. };
  6923. x1ForLegendTile = function x1ForLegendTile(id, i) {
  6924. return xForLegend(id, i) - 2;
  6925. };
  6926. x2ForLegendTile = function x2ForLegendTile(id, i) {
  6927. return xForLegend(id, i) - 2 + config.legend_item_tile_width;
  6928. };
  6929. yForLegendTile = function yForLegendTile(id, i) {
  6930. return yForLegend(id, i) + 4;
  6931. };
  6932. // Define g for legend area
  6933. l = $$.legend.selectAll('.' + CLASS.legendItem).data(targetIds).enter().append('g').attr('class', function (id) {
  6934. return $$.generateClass(CLASS.legendItem, id);
  6935. }).style('visibility', function (id) {
  6936. return $$.isLegendToShow(id) ? 'visible' : 'hidden';
  6937. }).style('cursor', 'pointer').on('click', function (id) {
  6938. if (config.legend_item_onclick) {
  6939. config.legend_item_onclick.call($$, id);
  6940. } else {
  6941. if ($$.d3.event.altKey) {
  6942. $$.api.hide();
  6943. $$.api.show(id);
  6944. } else {
  6945. $$.api.toggle(id);
  6946. $$.isTargetToShow(id) ? $$.api.focus(id) : $$.api.revert();
  6947. }
  6948. }
  6949. }).on('mouseover', function (id) {
  6950. if (config.legend_item_onmouseover) {
  6951. config.legend_item_onmouseover.call($$, id);
  6952. } else {
  6953. $$.d3.select(this).classed(CLASS.legendItemFocused, true);
  6954. if (!$$.transiting && $$.isTargetToShow(id)) {
  6955. $$.api.focus(id);
  6956. }
  6957. }
  6958. }).on('mouseout', function (id) {
  6959. if (config.legend_item_onmouseout) {
  6960. config.legend_item_onmouseout.call($$, id);
  6961. } else {
  6962. $$.d3.select(this).classed(CLASS.legendItemFocused, false);
  6963. $$.api.revert();
  6964. }
  6965. });
  6966. l.append('text').text(function (id) {
  6967. return isDefined(config.data_names[id]) ? config.data_names[id] : id;
  6968. }).each(function (id, i) {
  6969. updatePositions(this, id, i);
  6970. }).style("pointer-events", "none").attr('x', $$.isLegendRight || $$.isLegendInset ? xForLegendText : -200).attr('y', $$.isLegendRight || $$.isLegendInset ? -200 : yForLegendText);
  6971. l.append('rect').attr("class", CLASS.legendItemEvent).style('fill-opacity', 0).attr('x', $$.isLegendRight || $$.isLegendInset ? xForLegendRect : -200).attr('y', $$.isLegendRight || $$.isLegendInset ? -200 : yForLegendRect);
  6972. l.append('line').attr('class', CLASS.legendItemTile).style('stroke', $$.color).style("pointer-events", "none").attr('x1', $$.isLegendRight || $$.isLegendInset ? x1ForLegendTile : -200).attr('y1', $$.isLegendRight || $$.isLegendInset ? -200 : yForLegendTile).attr('x2', $$.isLegendRight || $$.isLegendInset ? x2ForLegendTile : -200).attr('y2', $$.isLegendRight || $$.isLegendInset ? -200 : yForLegendTile).attr('stroke-width', config.legend_item_tile_height);
  6973. // Set background for inset legend
  6974. background = $$.legend.select('.' + CLASS.legendBackground + ' rect');
  6975. if ($$.isLegendInset && maxWidth > 0 && background.size() === 0) {
  6976. background = $$.legend.insert('g', '.' + CLASS.legendItem).attr("class", CLASS.legendBackground).append('rect');
  6977. }
  6978. texts = $$.legend.selectAll('text').data(targetIds).text(function (id) {
  6979. return isDefined(config.data_names[id]) ? config.data_names[id] : id;
  6980. }) // MEMO: needed for update
  6981. .each(function (id, i) {
  6982. updatePositions(this, id, i);
  6983. });
  6984. (withTransition ? texts.transition() : texts).attr('x', xForLegendText).attr('y', yForLegendText);
  6985. rects = $$.legend.selectAll('rect.' + CLASS.legendItemEvent).data(targetIds);
  6986. (withTransition ? rects.transition() : rects).attr('width', function (id) {
  6987. return widths[id];
  6988. }).attr('height', function (id) {
  6989. return heights[id];
  6990. }).attr('x', xForLegendRect).attr('y', yForLegendRect);
  6991. tiles = $$.legend.selectAll('line.' + CLASS.legendItemTile).data(targetIds);
  6992. (withTransition ? tiles.transition() : tiles).style('stroke', $$.levelColor ? function (id) {
  6993. return $$.levelColor($$.cache[id].values[0].value);
  6994. } : $$.color).attr('x1', x1ForLegendTile).attr('y1', yForLegendTile).attr('x2', x2ForLegendTile).attr('y2', yForLegendTile);
  6995. if (background) {
  6996. (withTransition ? background.transition() : background).attr('height', $$.getLegendHeight() - 12).attr('width', maxWidth * (step + 1) + 10);
  6997. }
  6998. // toggle legend state
  6999. $$.legend.selectAll('.' + CLASS.legendItem).classed(CLASS.legendItemHidden, function (id) {
  7000. return !$$.isTargetToShow(id);
  7001. });
  7002. // Update all to reflect change of legend
  7003. $$.updateLegendItemWidth(maxWidth);
  7004. $$.updateLegendItemHeight(maxHeight);
  7005. $$.updateLegendStep(step);
  7006. // Update size and scale
  7007. $$.updateSizes();
  7008. $$.updateScales();
  7009. $$.updateSvgSize();
  7010. // Update g positions
  7011. $$.transformAll(withTransitionForTransform, transitions);
  7012. $$.legendHasRendered = true;
  7013. };
  7014. c3_chart_internal_fn.initRegion = function () {
  7015. var $$ = this;
  7016. $$.region = $$.main.append('g').attr("clip-path", $$.clipPath).attr("class", CLASS.regions);
  7017. };
  7018. c3_chart_internal_fn.updateRegion = function (duration) {
  7019. var $$ = this,
  7020. config = $$.config;
  7021. // hide if arc type
  7022. $$.region.style('visibility', $$.hasArcType() ? 'hidden' : 'visible');
  7023. $$.mainRegion = $$.main.select('.' + CLASS.regions).selectAll('.' + CLASS.region).data(config.regions);
  7024. $$.mainRegion.enter().append('g').append('rect').style("fill-opacity", 0);
  7025. $$.mainRegion.attr('class', $$.classRegion.bind($$));
  7026. $$.mainRegion.exit().transition().duration(duration).style("opacity", 0).remove();
  7027. };
  7028. c3_chart_internal_fn.redrawRegion = function (withTransition) {
  7029. var $$ = this,
  7030. regions = $$.mainRegion.selectAll('rect').each(function () {
  7031. // data is binded to g and it's not transferred to rect (child node) automatically,
  7032. // then data of each rect has to be updated manually.
  7033. // TODO: there should be more efficient way to solve this?
  7034. var parentData = $$.d3.select(this.parentNode).datum();
  7035. $$.d3.select(this).datum(parentData);
  7036. }),
  7037. x = $$.regionX.bind($$),
  7038. y = $$.regionY.bind($$),
  7039. w = $$.regionWidth.bind($$),
  7040. h = $$.regionHeight.bind($$);
  7041. return [(withTransition ? regions.transition() : regions).attr("x", x).attr("y", y).attr("width", w).attr("height", h).style("fill-opacity", function (d) {
  7042. return isValue(d.opacity) ? d.opacity : 0.1;
  7043. })];
  7044. };
  7045. c3_chart_internal_fn.regionX = function (d) {
  7046. var $$ = this,
  7047. config = $$.config,
  7048. xPos,
  7049. yScale = d.axis === 'y' ? $$.y : $$.y2;
  7050. if (d.axis === 'y' || d.axis === 'y2') {
  7051. xPos = config.axis_rotated ? 'start' in d ? yScale(d.start) : 0 : 0;
  7052. } else {
  7053. xPos = config.axis_rotated ? 0 : 'start' in d ? $$.x($$.isTimeSeries() ? $$.parseDate(d.start) : d.start) : 0;
  7054. }
  7055. return xPos;
  7056. };
  7057. c3_chart_internal_fn.regionY = function (d) {
  7058. var $$ = this,
  7059. config = $$.config,
  7060. yPos,
  7061. yScale = d.axis === 'y' ? $$.y : $$.y2;
  7062. if (d.axis === 'y' || d.axis === 'y2') {
  7063. yPos = config.axis_rotated ? 0 : 'end' in d ? yScale(d.end) : 0;
  7064. } else {
  7065. yPos = config.axis_rotated ? 'start' in d ? $$.x($$.isTimeSeries() ? $$.parseDate(d.start) : d.start) : 0 : 0;
  7066. }
  7067. return yPos;
  7068. };
  7069. c3_chart_internal_fn.regionWidth = function (d) {
  7070. var $$ = this,
  7071. config = $$.config,
  7072. start = $$.regionX(d),
  7073. end,
  7074. yScale = d.axis === 'y' ? $$.y : $$.y2;
  7075. if (d.axis === 'y' || d.axis === 'y2') {
  7076. end = config.axis_rotated ? 'end' in d ? yScale(d.end) : $$.width : $$.width;
  7077. } else {
  7078. end = config.axis_rotated ? $$.width : 'end' in d ? $$.x($$.isTimeSeries() ? $$.parseDate(d.end) : d.end) : $$.width;
  7079. }
  7080. return end < start ? 0 : end - start;
  7081. };
  7082. c3_chart_internal_fn.regionHeight = function (d) {
  7083. var $$ = this,
  7084. config = $$.config,
  7085. start = this.regionY(d),
  7086. end,
  7087. yScale = d.axis === 'y' ? $$.y : $$.y2;
  7088. if (d.axis === 'y' || d.axis === 'y2') {
  7089. end = config.axis_rotated ? $$.height : 'start' in d ? yScale(d.start) : $$.height;
  7090. } else {
  7091. end = config.axis_rotated ? 'end' in d ? $$.x($$.isTimeSeries() ? $$.parseDate(d.end) : d.end) : $$.height : $$.height;
  7092. }
  7093. return end < start ? 0 : end - start;
  7094. };
  7095. c3_chart_internal_fn.isRegionOnX = function (d) {
  7096. return !d.axis || d.axis === 'x';
  7097. };
  7098. c3_chart_internal_fn.getScale = function (min, max, forTimeseries) {
  7099. return (forTimeseries ? this.d3.time.scale() : this.d3.scale.linear()).range([min, max]);
  7100. };
  7101. c3_chart_internal_fn.getX = function (min, max, domain, offset) {
  7102. var $$ = this,
  7103. scale = $$.getScale(min, max, $$.isTimeSeries()),
  7104. _scale = domain ? scale.domain(domain) : scale,
  7105. key;
  7106. // Define customized scale if categorized axis
  7107. if ($$.isCategorized()) {
  7108. offset = offset || function () {
  7109. return 0;
  7110. };
  7111. scale = function scale(d, raw) {
  7112. var v = _scale(d) + offset(d);
  7113. return raw ? v : Math.ceil(v);
  7114. };
  7115. } else {
  7116. scale = function scale(d, raw) {
  7117. var v = _scale(d);
  7118. return raw ? v : Math.ceil(v);
  7119. };
  7120. }
  7121. // define functions
  7122. for (key in _scale) {
  7123. scale[key] = _scale[key];
  7124. }
  7125. scale.orgDomain = function () {
  7126. return _scale.domain();
  7127. };
  7128. // define custom domain() for categorized axis
  7129. if ($$.isCategorized()) {
  7130. scale.domain = function (domain) {
  7131. if (!arguments.length) {
  7132. domain = this.orgDomain();
  7133. return [domain[0], domain[1] + 1];
  7134. }
  7135. _scale.domain(domain);
  7136. return scale;
  7137. };
  7138. }
  7139. return scale;
  7140. };
  7141. c3_chart_internal_fn.getY = function (min, max, domain) {
  7142. var scale = this.getScale(min, max, this.isTimeSeriesY());
  7143. if (domain) {
  7144. scale.domain(domain);
  7145. }
  7146. return scale;
  7147. };
  7148. c3_chart_internal_fn.getYScale = function (id) {
  7149. return this.axis.getId(id) === 'y2' ? this.y2 : this.y;
  7150. };
  7151. c3_chart_internal_fn.getSubYScale = function (id) {
  7152. return this.axis.getId(id) === 'y2' ? this.subY2 : this.subY;
  7153. };
  7154. c3_chart_internal_fn.updateScales = function () {
  7155. var $$ = this,
  7156. config = $$.config,
  7157. forInit = !$$.x;
  7158. // update edges
  7159. $$.xMin = config.axis_rotated ? 1 : 0;
  7160. $$.xMax = config.axis_rotated ? $$.height : $$.width;
  7161. $$.yMin = config.axis_rotated ? 0 : $$.height;
  7162. $$.yMax = config.axis_rotated ? $$.width : 1;
  7163. $$.subXMin = $$.xMin;
  7164. $$.subXMax = $$.xMax;
  7165. $$.subYMin = config.axis_rotated ? 0 : $$.height2;
  7166. $$.subYMax = config.axis_rotated ? $$.width2 : 1;
  7167. // update scales
  7168. $$.x = $$.getX($$.xMin, $$.xMax, forInit ? undefined : $$.x.orgDomain(), function () {
  7169. return $$.xAxis.tickOffset();
  7170. });
  7171. $$.y = $$.getY($$.yMin, $$.yMax, forInit ? config.axis_y_default : $$.y.domain());
  7172. $$.y2 = $$.getY($$.yMin, $$.yMax, forInit ? config.axis_y2_default : $$.y2.domain());
  7173. $$.subX = $$.getX($$.xMin, $$.xMax, $$.orgXDomain, function (d) {
  7174. return d % 1 ? 0 : $$.subXAxis.tickOffset();
  7175. });
  7176. $$.subY = $$.getY($$.subYMin, $$.subYMax, forInit ? config.axis_y_default : $$.subY.domain());
  7177. $$.subY2 = $$.getY($$.subYMin, $$.subYMax, forInit ? config.axis_y2_default : $$.subY2.domain());
  7178. // update axes
  7179. $$.xAxisTickFormat = $$.axis.getXAxisTickFormat();
  7180. $$.xAxisTickValues = $$.axis.getXAxisTickValues();
  7181. $$.yAxisTickValues = $$.axis.getYAxisTickValues();
  7182. $$.y2AxisTickValues = $$.axis.getY2AxisTickValues();
  7183. $$.xAxis = $$.axis.getXAxis($$.x, $$.xOrient, $$.xAxisTickFormat, $$.xAxisTickValues, config.axis_x_tick_outer);
  7184. $$.subXAxis = $$.axis.getXAxis($$.subX, $$.subXOrient, $$.xAxisTickFormat, $$.xAxisTickValues, config.axis_x_tick_outer);
  7185. $$.yAxis = $$.axis.getYAxis($$.y, $$.yOrient, config.axis_y_tick_format, $$.yAxisTickValues, config.axis_y_tick_outer);
  7186. $$.y2Axis = $$.axis.getYAxis($$.y2, $$.y2Orient, config.axis_y2_tick_format, $$.y2AxisTickValues, config.axis_y2_tick_outer);
  7187. // Set initialized scales to brush and zoom
  7188. if (!forInit) {
  7189. if ($$.brush) {
  7190. $$.brush.scale($$.subX);
  7191. }
  7192. if (config.zoom_enabled) {
  7193. $$.zoom.scale($$.x);
  7194. }
  7195. }
  7196. // update for arc
  7197. if ($$.updateArc) {
  7198. $$.updateArc();
  7199. }
  7200. };
  7201. c3_chart_internal_fn.selectPoint = function (target, d, i) {
  7202. var $$ = this,
  7203. config = $$.config,
  7204. cx = (config.axis_rotated ? $$.circleY : $$.circleX).bind($$),
  7205. cy = (config.axis_rotated ? $$.circleX : $$.circleY).bind($$),
  7206. r = $$.pointSelectR.bind($$);
  7207. config.data_onselected.call($$.api, d, target.node());
  7208. // add selected-circle on low layer g
  7209. $$.main.select('.' + CLASS.selectedCircles + $$.getTargetSelectorSuffix(d.id)).selectAll('.' + CLASS.selectedCircle + '-' + i).data([d]).enter().append('circle').attr("class", function () {
  7210. return $$.generateClass(CLASS.selectedCircle, i);
  7211. }).attr("cx", cx).attr("cy", cy).attr("stroke", function () {
  7212. return $$.color(d);
  7213. }).attr("r", function (d) {
  7214. return $$.pointSelectR(d) * 1.4;
  7215. }).transition().duration(100).attr("r", r);
  7216. };
  7217. c3_chart_internal_fn.unselectPoint = function (target, d, i) {
  7218. var $$ = this;
  7219. $$.config.data_onunselected.call($$.api, d, target.node());
  7220. // remove selected-circle from low layer g
  7221. $$.main.select('.' + CLASS.selectedCircles + $$.getTargetSelectorSuffix(d.id)).selectAll('.' + CLASS.selectedCircle + '-' + i).transition().duration(100).attr('r', 0).remove();
  7222. };
  7223. c3_chart_internal_fn.togglePoint = function (selected, target, d, i) {
  7224. selected ? this.selectPoint(target, d, i) : this.unselectPoint(target, d, i);
  7225. };
  7226. c3_chart_internal_fn.selectPath = function (target, d) {
  7227. var $$ = this;
  7228. $$.config.data_onselected.call($$, d, target.node());
  7229. if ($$.config.interaction_brighten) {
  7230. target.transition().duration(100).style("fill", function () {
  7231. return $$.d3.rgb($$.color(d)).brighter(0.75);
  7232. });
  7233. }
  7234. };
  7235. c3_chart_internal_fn.unselectPath = function (target, d) {
  7236. var $$ = this;
  7237. $$.config.data_onunselected.call($$, d, target.node());
  7238. if ($$.config.interaction_brighten) {
  7239. target.transition().duration(100).style("fill", function () {
  7240. return $$.color(d);
  7241. });
  7242. }
  7243. };
  7244. c3_chart_internal_fn.togglePath = function (selected, target, d, i) {
  7245. selected ? this.selectPath(target, d, i) : this.unselectPath(target, d, i);
  7246. };
  7247. c3_chart_internal_fn.getToggle = function (that, d) {
  7248. var $$ = this,
  7249. toggle;
  7250. if (that.nodeName === 'circle') {
  7251. if ($$.isStepType(d)) {
  7252. // circle is hidden in step chart, so treat as within the click area
  7253. toggle = function toggle() {}; // TODO: how to select step chart?
  7254. } else {
  7255. toggle = $$.togglePoint;
  7256. }
  7257. } else if (that.nodeName === 'path') {
  7258. toggle = $$.togglePath;
  7259. }
  7260. return toggle;
  7261. };
  7262. c3_chart_internal_fn.toggleShape = function (that, d, i) {
  7263. var $$ = this,
  7264. d3 = $$.d3,
  7265. config = $$.config,
  7266. shape = d3.select(that),
  7267. isSelected = shape.classed(CLASS.SELECTED),
  7268. toggle = $$.getToggle(that, d).bind($$);
  7269. if (config.data_selection_enabled && config.data_selection_isselectable(d)) {
  7270. if (!config.data_selection_multiple) {
  7271. $$.main.selectAll('.' + CLASS.shapes + (config.data_selection_grouped ? $$.getTargetSelectorSuffix(d.id) : "")).selectAll('.' + CLASS.shape).each(function (d, i) {
  7272. var shape = d3.select(this);
  7273. if (shape.classed(CLASS.SELECTED)) {
  7274. toggle(false, shape.classed(CLASS.SELECTED, false), d, i);
  7275. }
  7276. });
  7277. }
  7278. shape.classed(CLASS.SELECTED, !isSelected);
  7279. toggle(!isSelected, shape, d, i);
  7280. }
  7281. };
  7282. c3_chart_internal_fn.initBar = function () {
  7283. var $$ = this;
  7284. $$.main.select('.' + CLASS.chart).append("g").attr("class", CLASS.chartBars);
  7285. };
  7286. c3_chart_internal_fn.updateTargetsForBar = function (targets) {
  7287. var $$ = this,
  7288. config = $$.config,
  7289. mainBarUpdate,
  7290. mainBarEnter,
  7291. classChartBar = $$.classChartBar.bind($$),
  7292. classBars = $$.classBars.bind($$),
  7293. classFocus = $$.classFocus.bind($$);
  7294. mainBarUpdate = $$.main.select('.' + CLASS.chartBars).selectAll('.' + CLASS.chartBar).data(targets).attr('class', function (d) {
  7295. return classChartBar(d) + classFocus(d);
  7296. });
  7297. mainBarEnter = mainBarUpdate.enter().append('g').attr('class', classChartBar).style("pointer-events", "none");
  7298. // Bars for each data
  7299. mainBarEnter.append('g').attr("class", classBars).style("cursor", function (d) {
  7300. return config.data_selection_isselectable(d) ? "pointer" : null;
  7301. });
  7302. };
  7303. c3_chart_internal_fn.updateBar = function (durationForExit) {
  7304. var $$ = this,
  7305. barData = $$.barData.bind($$),
  7306. classBar = $$.classBar.bind($$),
  7307. initialOpacity = $$.initialOpacity.bind($$),
  7308. color = function color(d) {
  7309. return $$.color(d.id);
  7310. };
  7311. $$.mainBar = $$.main.selectAll('.' + CLASS.bars).selectAll('.' + CLASS.bar).data(barData);
  7312. $$.mainBar.enter().append('path').attr("class", classBar).style("stroke", color).style("fill", color);
  7313. $$.mainBar.style("opacity", initialOpacity);
  7314. $$.mainBar.exit().transition().duration(durationForExit).remove();
  7315. };
  7316. c3_chart_internal_fn.redrawBar = function (drawBar, withTransition) {
  7317. return [(withTransition ? this.mainBar.transition(Math.random().toString()) : this.mainBar).attr('d', drawBar).style("stroke", this.color).style("fill", this.color).style("opacity", 1)];
  7318. };
  7319. c3_chart_internal_fn.getBarW = function (axis, barTargetsNum) {
  7320. var $$ = this,
  7321. config = $$.config,
  7322. w = typeof config.bar_width === 'number' ? config.bar_width : barTargetsNum ? axis.tickInterval() * config.bar_width_ratio / barTargetsNum : 0;
  7323. return config.bar_width_max && w > config.bar_width_max ? config.bar_width_max : w;
  7324. };
  7325. c3_chart_internal_fn.getBars = function (i, id) {
  7326. var $$ = this;
  7327. return (id ? $$.main.selectAll('.' + CLASS.bars + $$.getTargetSelectorSuffix(id)) : $$.main).selectAll('.' + CLASS.bar + (isValue(i) ? '-' + i : ''));
  7328. };
  7329. c3_chart_internal_fn.expandBars = function (i, id, reset) {
  7330. var $$ = this;
  7331. if (reset) {
  7332. $$.unexpandBars();
  7333. }
  7334. $$.getBars(i, id).classed(CLASS.EXPANDED, true);
  7335. };
  7336. c3_chart_internal_fn.unexpandBars = function (i) {
  7337. var $$ = this;
  7338. $$.getBars(i).classed(CLASS.EXPANDED, false);
  7339. };
  7340. c3_chart_internal_fn.generateDrawBar = function (barIndices, isSub) {
  7341. var $$ = this,
  7342. config = $$.config,
  7343. getPoints = $$.generateGetBarPoints(barIndices, isSub);
  7344. return function (d, i) {
  7345. // 4 points that make a bar
  7346. var points = getPoints(d, i);
  7347. // switch points if axis is rotated, not applicable for sub chart
  7348. var indexX = config.axis_rotated ? 1 : 0;
  7349. var indexY = config.axis_rotated ? 0 : 1;
  7350. var path = 'M ' + points[0][indexX] + ',' + points[0][indexY] + ' ' + 'L' + points[1][indexX] + ',' + points[1][indexY] + ' ' + 'L' + points[2][indexX] + ',' + points[2][indexY] + ' ' + 'L' + points[3][indexX] + ',' + points[3][indexY] + ' ' + 'z';
  7351. return path;
  7352. };
  7353. };
  7354. c3_chart_internal_fn.generateGetBarPoints = function (barIndices, isSub) {
  7355. var $$ = this,
  7356. axis = isSub ? $$.subXAxis : $$.xAxis,
  7357. barTargetsNum = barIndices.__max__ + 1,
  7358. barW = $$.getBarW(axis, barTargetsNum),
  7359. barX = $$.getShapeX(barW, barTargetsNum, barIndices, !!isSub),
  7360. barY = $$.getShapeY(!!isSub),
  7361. barOffset = $$.getShapeOffset($$.isBarType, barIndices, !!isSub),
  7362. barSpaceOffset = barW * ($$.config.bar_space / 2),
  7363. yScale = isSub ? $$.getSubYScale : $$.getYScale;
  7364. return function (d, i) {
  7365. var y0 = yScale.call($$, d.id)(0),
  7366. offset = barOffset(d, i) || y0,
  7367. // offset is for stacked bar chart
  7368. posX = barX(d),
  7369. posY = barY(d);
  7370. // fix posY not to overflow opposite quadrant
  7371. if ($$.config.axis_rotated) {
  7372. if (0 < d.value && posY < y0 || d.value < 0 && y0 < posY) {
  7373. posY = y0;
  7374. }
  7375. }
  7376. // 4 points that make a bar
  7377. return [[posX + barSpaceOffset, offset], [posX + barSpaceOffset, posY - (y0 - offset)], [posX + barW - barSpaceOffset, posY - (y0 - offset)], [posX + barW - barSpaceOffset, offset]];
  7378. };
  7379. };
  7380. c3_chart_internal_fn.isWithinBar = function (that) {
  7381. var mouse = this.d3.mouse(that),
  7382. box = that.getBoundingClientRect(),
  7383. seg0 = that.pathSegList.getItem(0),
  7384. seg1 = that.pathSegList.getItem(1),
  7385. x = Math.min(seg0.x, seg1.x),
  7386. y = Math.min(seg0.y, seg1.y),
  7387. w = box.width,
  7388. h = box.height,
  7389. offset = 2,
  7390. sx = x - offset,
  7391. ex = x + w + offset,
  7392. sy = y + h + offset,
  7393. ey = y - offset;
  7394. return sx < mouse[0] && mouse[0] < ex && ey < mouse[1] && mouse[1] < sy;
  7395. };
  7396. c3_chart_internal_fn.getShapeIndices = function (typeFilter) {
  7397. var $$ = this,
  7398. config = $$.config,
  7399. indices = {},
  7400. i = 0,
  7401. j,
  7402. k;
  7403. $$.filterTargetsToShow($$.data.targets.filter(typeFilter, $$)).forEach(function (d) {
  7404. for (j = 0; j < config.data_groups.length; j++) {
  7405. if (config.data_groups[j].indexOf(d.id) < 0) {
  7406. continue;
  7407. }
  7408. for (k = 0; k < config.data_groups[j].length; k++) {
  7409. if (config.data_groups[j][k] in indices) {
  7410. indices[d.id] = indices[config.data_groups[j][k]];
  7411. break;
  7412. }
  7413. }
  7414. }
  7415. if (isUndefined(indices[d.id])) {
  7416. indices[d.id] = i++;
  7417. }
  7418. });
  7419. indices.__max__ = i - 1;
  7420. return indices;
  7421. };
  7422. c3_chart_internal_fn.getShapeX = function (offset, targetsNum, indices, isSub) {
  7423. var $$ = this,
  7424. scale = isSub ? $$.subX : $$.x;
  7425. return function (d) {
  7426. var index = d.id in indices ? indices[d.id] : 0;
  7427. return d.x || d.x === 0 ? scale(d.x) - offset * (targetsNum / 2 - index) : 0;
  7428. };
  7429. };
  7430. c3_chart_internal_fn.getShapeY = function (isSub) {
  7431. var $$ = this;
  7432. return function (d) {
  7433. var scale = isSub ? $$.getSubYScale(d.id) : $$.getYScale(d.id);
  7434. return scale(d.value);
  7435. };
  7436. };
  7437. c3_chart_internal_fn.getShapeOffset = function (typeFilter, indices, isSub) {
  7438. var $$ = this,
  7439. targets = $$.orderTargets($$.filterTargetsToShow($$.data.targets.filter(typeFilter, $$))),
  7440. targetIds = targets.map(function (t) {
  7441. return t.id;
  7442. });
  7443. return function (d, i) {
  7444. var scale = isSub ? $$.getSubYScale(d.id) : $$.getYScale(d.id),
  7445. y0 = scale(0),
  7446. offset = y0;
  7447. targets.forEach(function (t) {
  7448. var values = $$.isStepType(d) ? $$.convertValuesToStep(t.values) : t.values;
  7449. if (t.id === d.id || indices[t.id] !== indices[d.id]) {
  7450. return;
  7451. }
  7452. if (targetIds.indexOf(t.id) < targetIds.indexOf(d.id)) {
  7453. // check if the x values line up
  7454. if (typeof values[i] === 'undefined' || +values[i].x !== +d.x) {
  7455. // "+" for timeseries
  7456. // if not, try to find the value that does line up
  7457. i = -1;
  7458. values.forEach(function (v, j) {
  7459. if (v.x === d.x) {
  7460. i = j;
  7461. }
  7462. });
  7463. }
  7464. if (i in values && values[i].value * d.value >= 0) {
  7465. offset += scale(values[i].value) - y0;
  7466. }
  7467. }
  7468. });
  7469. return offset;
  7470. };
  7471. };
  7472. c3_chart_internal_fn.isWithinShape = function (that, d) {
  7473. var $$ = this,
  7474. shape = $$.d3.select(that),
  7475. isWithin;
  7476. if (!$$.isTargetToShow(d.id)) {
  7477. isWithin = false;
  7478. } else if (that.nodeName === 'circle') {
  7479. isWithin = $$.isStepType(d) ? $$.isWithinStep(that, $$.getYScale(d.id)(d.value)) : $$.isWithinCircle(that, $$.pointSelectR(d) * 1.5);
  7480. } else if (that.nodeName === 'path') {
  7481. isWithin = shape.classed(CLASS.bar) ? $$.isWithinBar(that) : true;
  7482. }
  7483. return isWithin;
  7484. };
  7485. c3_chart_internal_fn.getInterpolate = function (d) {
  7486. var $$ = this,
  7487. interpolation = $$.isInterpolationType($$.config.spline_interpolation_type) ? $$.config.spline_interpolation_type : 'cardinal';
  7488. return $$.isSplineType(d) ? interpolation : $$.isStepType(d) ? $$.config.line_step_type : "linear";
  7489. };
  7490. c3_chart_internal_fn.initLine = function () {
  7491. var $$ = this;
  7492. $$.main.select('.' + CLASS.chart).append("g").attr("class", CLASS.chartLines);
  7493. };
  7494. c3_chart_internal_fn.updateTargetsForLine = function (targets) {
  7495. var $$ = this,
  7496. config = $$.config,
  7497. mainLineUpdate,
  7498. mainLineEnter,
  7499. classChartLine = $$.classChartLine.bind($$),
  7500. classLines = $$.classLines.bind($$),
  7501. classAreas = $$.classAreas.bind($$),
  7502. classCircles = $$.classCircles.bind($$),
  7503. classFocus = $$.classFocus.bind($$);
  7504. mainLineUpdate = $$.main.select('.' + CLASS.chartLines).selectAll('.' + CLASS.chartLine).data(targets).attr('class', function (d) {
  7505. return classChartLine(d) + classFocus(d);
  7506. });
  7507. mainLineEnter = mainLineUpdate.enter().append('g').attr('class', classChartLine).style('opacity', 0).style("pointer-events", "none");
  7508. // Lines for each data
  7509. mainLineEnter.append('g').attr("class", classLines);
  7510. // Areas
  7511. mainLineEnter.append('g').attr('class', classAreas);
  7512. // Circles for each data point on lines
  7513. mainLineEnter.append('g').attr("class", function (d) {
  7514. return $$.generateClass(CLASS.selectedCircles, d.id);
  7515. });
  7516. mainLineEnter.append('g').attr("class", classCircles).style("cursor", function (d) {
  7517. return config.data_selection_isselectable(d) ? "pointer" : null;
  7518. });
  7519. // Update date for selected circles
  7520. targets.forEach(function (t) {
  7521. $$.main.selectAll('.' + CLASS.selectedCircles + $$.getTargetSelectorSuffix(t.id)).selectAll('.' + CLASS.selectedCircle).each(function (d) {
  7522. d.value = t.values[d.index].value;
  7523. });
  7524. });
  7525. // MEMO: can not keep same color...
  7526. //mainLineUpdate.exit().remove();
  7527. };
  7528. c3_chart_internal_fn.updateLine = function (durationForExit) {
  7529. var $$ = this;
  7530. $$.mainLine = $$.main.selectAll('.' + CLASS.lines).selectAll('.' + CLASS.line).data($$.lineData.bind($$));
  7531. $$.mainLine.enter().append('path').attr('class', $$.classLine.bind($$)).style("stroke", $$.color);
  7532. $$.mainLine.style("opacity", $$.initialOpacity.bind($$)).style('shape-rendering', function (d) {
  7533. return $$.isStepType(d) ? 'crispEdges' : '';
  7534. }).attr('transform', null);
  7535. $$.mainLine.exit().transition().duration(durationForExit).style('opacity', 0).remove();
  7536. };
  7537. c3_chart_internal_fn.redrawLine = function (drawLine, withTransition) {
  7538. return [(withTransition ? this.mainLine.transition(Math.random().toString()) : this.mainLine).attr("d", drawLine).style("stroke", this.color).style("opacity", 1)];
  7539. };
  7540. c3_chart_internal_fn.generateDrawLine = function (lineIndices, isSub) {
  7541. var $$ = this,
  7542. config = $$.config,
  7543. line = $$.d3.svg.line(),
  7544. getPoints = $$.generateGetLinePoints(lineIndices, isSub),
  7545. yScaleGetter = isSub ? $$.getSubYScale : $$.getYScale,
  7546. xValue = function xValue(d) {
  7547. return (isSub ? $$.subxx : $$.xx).call($$, d);
  7548. },
  7549. yValue = function yValue(d, i) {
  7550. return config.data_groups.length > 0 ? getPoints(d, i)[0][1] : yScaleGetter.call($$, d.id)(d.value);
  7551. };
  7552. line = config.axis_rotated ? line.x(yValue).y(xValue) : line.x(xValue).y(yValue);
  7553. if (!config.line_connectNull) {
  7554. line = line.defined(function (d) {
  7555. return d.value != null;
  7556. });
  7557. }
  7558. return function (d) {
  7559. var values = config.line_connectNull ? $$.filterRemoveNull(d.values) : d.values,
  7560. x = isSub ? $$.x : $$.subX,
  7561. y = yScaleGetter.call($$, d.id),
  7562. x0 = 0,
  7563. y0 = 0,
  7564. path;
  7565. if ($$.isLineType(d)) {
  7566. if (config.data_regions[d.id]) {
  7567. path = $$.lineWithRegions(values, x, y, config.data_regions[d.id]);
  7568. } else {
  7569. if ($$.isStepType(d)) {
  7570. values = $$.convertValuesToStep(values);
  7571. }
  7572. path = line.interpolate($$.getInterpolate(d))(values);
  7573. }
  7574. } else {
  7575. if (values[0]) {
  7576. x0 = x(values[0].x);
  7577. y0 = y(values[0].value);
  7578. }
  7579. path = config.axis_rotated ? "M " + y0 + " " + x0 : "M " + x0 + " " + y0;
  7580. }
  7581. return path ? path : "M 0 0";
  7582. };
  7583. };
  7584. c3_chart_internal_fn.generateGetLinePoints = function (lineIndices, isSub) {
  7585. // partial duplication of generateGetBarPoints
  7586. var $$ = this,
  7587. config = $$.config,
  7588. lineTargetsNum = lineIndices.__max__ + 1,
  7589. x = $$.getShapeX(0, lineTargetsNum, lineIndices, !!isSub),
  7590. y = $$.getShapeY(!!isSub),
  7591. lineOffset = $$.getShapeOffset($$.isLineType, lineIndices, !!isSub),
  7592. yScale = isSub ? $$.getSubYScale : $$.getYScale;
  7593. return function (d, i) {
  7594. var y0 = yScale.call($$, d.id)(0),
  7595. offset = lineOffset(d, i) || y0,
  7596. // offset is for stacked area chart
  7597. posX = x(d),
  7598. posY = y(d);
  7599. // fix posY not to overflow opposite quadrant
  7600. if (config.axis_rotated) {
  7601. if (0 < d.value && posY < y0 || d.value < 0 && y0 < posY) {
  7602. posY = y0;
  7603. }
  7604. }
  7605. // 1 point that marks the line position
  7606. return [[posX, posY - (y0 - offset)], [posX, posY - (y0 - offset)], // needed for compatibility
  7607. [posX, posY - (y0 - offset)], // needed for compatibility
  7608. [posX, posY - (y0 - offset)] // needed for compatibility
  7609. ];
  7610. };
  7611. };
  7612. c3_chart_internal_fn.lineWithRegions = function (d, x, y, _regions) {
  7613. var $$ = this,
  7614. config = $$.config,
  7615. prev = -1,
  7616. i,
  7617. j,
  7618. s = "M",
  7619. sWithRegion,
  7620. xp,
  7621. yp,
  7622. dx,
  7623. dy,
  7624. dd,
  7625. diff,
  7626. diffx2,
  7627. xOffset = $$.isCategorized() ? 0.5 : 0,
  7628. xValue,
  7629. yValue,
  7630. regions = [];
  7631. function isWithinRegions(x, regions) {
  7632. var i;
  7633. for (i = 0; i < regions.length; i++) {
  7634. if (regions[i].start < x && x <= regions[i].end) {
  7635. return true;
  7636. }
  7637. }
  7638. return false;
  7639. }
  7640. // Check start/end of regions
  7641. if (isDefined(_regions)) {
  7642. for (i = 0; i < _regions.length; i++) {
  7643. regions[i] = {};
  7644. if (isUndefined(_regions[i].start)) {
  7645. regions[i].start = d[0].x;
  7646. } else {
  7647. regions[i].start = $$.isTimeSeries() ? $$.parseDate(_regions[i].start) : _regions[i].start;
  7648. }
  7649. if (isUndefined(_regions[i].end)) {
  7650. regions[i].end = d[d.length - 1].x;
  7651. } else {
  7652. regions[i].end = $$.isTimeSeries() ? $$.parseDate(_regions[i].end) : _regions[i].end;
  7653. }
  7654. }
  7655. }
  7656. // Set scales
  7657. xValue = config.axis_rotated ? function (d) {
  7658. return y(d.value);
  7659. } : function (d) {
  7660. return x(d.x);
  7661. };
  7662. yValue = config.axis_rotated ? function (d) {
  7663. return x(d.x);
  7664. } : function (d) {
  7665. return y(d.value);
  7666. };
  7667. // Define svg generator function for region
  7668. function generateM(points) {
  7669. return 'M' + points[0][0] + ' ' + points[0][1] + ' ' + points[1][0] + ' ' + points[1][1];
  7670. }
  7671. if ($$.isTimeSeries()) {
  7672. sWithRegion = function sWithRegion(d0, d1, j, diff) {
  7673. var x0 = d0.x.getTime(),
  7674. x_diff = d1.x - d0.x,
  7675. xv0 = new Date(x0 + x_diff * j),
  7676. xv1 = new Date(x0 + x_diff * (j + diff)),
  7677. points;
  7678. if (config.axis_rotated) {
  7679. points = [[y(yp(j)), x(xv0)], [y(yp(j + diff)), x(xv1)]];
  7680. } else {
  7681. points = [[x(xv0), y(yp(j))], [x(xv1), y(yp(j + diff))]];
  7682. }
  7683. return generateM(points);
  7684. };
  7685. } else {
  7686. sWithRegion = function sWithRegion(d0, d1, j, diff) {
  7687. var points;
  7688. if (config.axis_rotated) {
  7689. points = [[y(yp(j), true), x(xp(j))], [y(yp(j + diff), true), x(xp(j + diff))]];
  7690. } else {
  7691. points = [[x(xp(j), true), y(yp(j))], [x(xp(j + diff), true), y(yp(j + diff))]];
  7692. }
  7693. return generateM(points);
  7694. };
  7695. }
  7696. // Generate
  7697. for (i = 0; i < d.length; i++) {
  7698. // Draw as normal
  7699. if (isUndefined(regions) || !isWithinRegions(d[i].x, regions)) {
  7700. s += " " + xValue(d[i]) + " " + yValue(d[i]);
  7701. }
  7702. // Draw with region // TODO: Fix for horizotal charts
  7703. else {
  7704. xp = $$.getScale(d[i - 1].x + xOffset, d[i].x + xOffset, $$.isTimeSeries());
  7705. yp = $$.getScale(d[i - 1].value, d[i].value);
  7706. dx = x(d[i].x) - x(d[i - 1].x);
  7707. dy = y(d[i].value) - y(d[i - 1].value);
  7708. dd = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
  7709. diff = 2 / dd;
  7710. diffx2 = diff * 2;
  7711. for (j = diff; j <= 1; j += diffx2) {
  7712. s += sWithRegion(d[i - 1], d[i], j, diff);
  7713. }
  7714. }
  7715. prev = d[i].x;
  7716. }
  7717. return s;
  7718. };
  7719. c3_chart_internal_fn.updateArea = function (durationForExit) {
  7720. var $$ = this,
  7721. d3 = $$.d3;
  7722. $$.mainArea = $$.main.selectAll('.' + CLASS.areas).selectAll('.' + CLASS.area).data($$.lineData.bind($$));
  7723. $$.mainArea.enter().append('path').attr("class", $$.classArea.bind($$)).style("fill", $$.color).style("opacity", function () {
  7724. $$.orgAreaOpacity = +d3.select(this).style('opacity');return 0;
  7725. });
  7726. $$.mainArea.style("opacity", $$.orgAreaOpacity);
  7727. $$.mainArea.exit().transition().duration(durationForExit).style('opacity', 0).remove();
  7728. };
  7729. c3_chart_internal_fn.redrawArea = function (drawArea, withTransition) {
  7730. return [(withTransition ? this.mainArea.transition(Math.random().toString()) : this.mainArea).attr("d", drawArea).style("fill", this.color).style("opacity", this.orgAreaOpacity)];
  7731. };
  7732. c3_chart_internal_fn.generateDrawArea = function (areaIndices, isSub) {
  7733. var $$ = this,
  7734. config = $$.config,
  7735. area = $$.d3.svg.area(),
  7736. getPoints = $$.generateGetAreaPoints(areaIndices, isSub),
  7737. yScaleGetter = isSub ? $$.getSubYScale : $$.getYScale,
  7738. xValue = function xValue(d) {
  7739. return (isSub ? $$.subxx : $$.xx).call($$, d);
  7740. },
  7741. value0 = function value0(d, i) {
  7742. return config.data_groups.length > 0 ? getPoints(d, i)[0][1] : yScaleGetter.call($$, d.id)($$.getAreaBaseValue(d.id));
  7743. },
  7744. value1 = function value1(d, i) {
  7745. return config.data_groups.length > 0 ? getPoints(d, i)[1][1] : yScaleGetter.call($$, d.id)(d.value);
  7746. };
  7747. area = config.axis_rotated ? area.x0(value0).x1(value1).y(xValue) : area.x(xValue).y0(config.area_above ? 0 : value0).y1(value1);
  7748. if (!config.line_connectNull) {
  7749. area = area.defined(function (d) {
  7750. return d.value !== null;
  7751. });
  7752. }
  7753. return function (d) {
  7754. var values = config.line_connectNull ? $$.filterRemoveNull(d.values) : d.values,
  7755. x0 = 0,
  7756. y0 = 0,
  7757. path;
  7758. if ($$.isAreaType(d)) {
  7759. if ($$.isStepType(d)) {
  7760. values = $$.convertValuesToStep(values);
  7761. }
  7762. path = area.interpolate($$.getInterpolate(d))(values);
  7763. } else {
  7764. if (values[0]) {
  7765. x0 = $$.x(values[0].x);
  7766. y0 = $$.getYScale(d.id)(values[0].value);
  7767. }
  7768. path = config.axis_rotated ? "M " + y0 + " " + x0 : "M " + x0 + " " + y0;
  7769. }
  7770. return path ? path : "M 0 0";
  7771. };
  7772. };
  7773. c3_chart_internal_fn.getAreaBaseValue = function () {
  7774. return 0;
  7775. };
  7776. c3_chart_internal_fn.generateGetAreaPoints = function (areaIndices, isSub) {
  7777. // partial duplication of generateGetBarPoints
  7778. var $$ = this,
  7779. config = $$.config,
  7780. areaTargetsNum = areaIndices.__max__ + 1,
  7781. x = $$.getShapeX(0, areaTargetsNum, areaIndices, !!isSub),
  7782. y = $$.getShapeY(!!isSub),
  7783. areaOffset = $$.getShapeOffset($$.isAreaType, areaIndices, !!isSub),
  7784. yScale = isSub ? $$.getSubYScale : $$.getYScale;
  7785. return function (d, i) {
  7786. var y0 = yScale.call($$, d.id)(0),
  7787. offset = areaOffset(d, i) || y0,
  7788. // offset is for stacked area chart
  7789. posX = x(d),
  7790. posY = y(d);
  7791. // fix posY not to overflow opposite quadrant
  7792. if (config.axis_rotated) {
  7793. if (0 < d.value && posY < y0 || d.value < 0 && y0 < posY) {
  7794. posY = y0;
  7795. }
  7796. }
  7797. // 1 point that marks the area position
  7798. return [[posX, offset], [posX, posY - (y0 - offset)], [posX, posY - (y0 - offset)], // needed for compatibility
  7799. [posX, offset] // needed for compatibility
  7800. ];
  7801. };
  7802. };
  7803. c3_chart_internal_fn.updateCircle = function () {
  7804. var $$ = this;
  7805. $$.mainCircle = $$.main.selectAll('.' + CLASS.circles).selectAll('.' + CLASS.circle).data($$.lineOrScatterData.bind($$));
  7806. $$.mainCircle.enter().append("circle").attr("class", $$.classCircle.bind($$)).attr("r", $$.pointR.bind($$)).style("fill", $$.color);
  7807. $$.mainCircle.style("opacity", $$.initialOpacityForCircle.bind($$));
  7808. $$.mainCircle.exit().remove();
  7809. };
  7810. c3_chart_internal_fn.redrawCircle = function (cx, cy, withTransition) {
  7811. var selectedCircles = this.main.selectAll('.' + CLASS.selectedCircle);
  7812. return [(withTransition ? this.mainCircle.transition(Math.random().toString()) : this.mainCircle).style('opacity', this.opacityForCircle.bind(this)).style("fill", this.color).attr("cx", cx).attr("cy", cy), (withTransition ? selectedCircles.transition(Math.random().toString()) : selectedCircles).attr("cx", cx).attr("cy", cy)];
  7813. };
  7814. c3_chart_internal_fn.circleX = function (d) {
  7815. return d.x || d.x === 0 ? this.x(d.x) : null;
  7816. };
  7817. c3_chart_internal_fn.updateCircleY = function () {
  7818. var $$ = this,
  7819. lineIndices,
  7820. getPoints;
  7821. if ($$.config.data_groups.length > 0) {
  7822. lineIndices = $$.getShapeIndices($$.isLineType), getPoints = $$.generateGetLinePoints(lineIndices);
  7823. $$.circleY = function (d, i) {
  7824. return getPoints(d, i)[0][1];
  7825. };
  7826. } else {
  7827. $$.circleY = function (d) {
  7828. return $$.getYScale(d.id)(d.value);
  7829. };
  7830. }
  7831. };
  7832. c3_chart_internal_fn.getCircles = function (i, id) {
  7833. var $$ = this;
  7834. return (id ? $$.main.selectAll('.' + CLASS.circles + $$.getTargetSelectorSuffix(id)) : $$.main).selectAll('.' + CLASS.circle + (isValue(i) ? '-' + i : ''));
  7835. };
  7836. c3_chart_internal_fn.expandCircles = function (i, id, reset) {
  7837. var $$ = this,
  7838. r = $$.pointExpandedR.bind($$);
  7839. if (reset) {
  7840. $$.unexpandCircles();
  7841. }
  7842. $$.getCircles(i, id).classed(CLASS.EXPANDED, true).attr('r', r);
  7843. };
  7844. c3_chart_internal_fn.unexpandCircles = function (i) {
  7845. var $$ = this,
  7846. r = $$.pointR.bind($$);
  7847. $$.getCircles(i).filter(function () {
  7848. return $$.d3.select(this).classed(CLASS.EXPANDED);
  7849. }).classed(CLASS.EXPANDED, false).attr('r', r);
  7850. };
  7851. c3_chart_internal_fn.pointR = function (d) {
  7852. var $$ = this,
  7853. config = $$.config;
  7854. return $$.isStepType(d) ? 0 : isFunction(config.point_r) ? config.point_r(d) : config.point_r;
  7855. };
  7856. c3_chart_internal_fn.pointExpandedR = function (d) {
  7857. var $$ = this,
  7858. config = $$.config;
  7859. if (config.point_focus_expand_enabled) {
  7860. return isFunction(config.point_focus_expand_r) ? config.point_focus_expand_r(d) : config.point_focus_expand_r ? config.point_focus_expand_r : $$.pointR(d) * 1.75;
  7861. } else {
  7862. return $$.pointR(d);
  7863. }
  7864. };
  7865. c3_chart_internal_fn.pointSelectR = function (d) {
  7866. var $$ = this,
  7867. config = $$.config;
  7868. return isFunction(config.point_select_r) ? config.point_select_r(d) : config.point_select_r ? config.point_select_r : $$.pointR(d) * 4;
  7869. };
  7870. c3_chart_internal_fn.isWithinCircle = function (that, r) {
  7871. var d3 = this.d3,
  7872. mouse = d3.mouse(that),
  7873. d3_this = d3.select(that),
  7874. cx = +d3_this.attr("cx"),
  7875. cy = +d3_this.attr("cy");
  7876. return Math.sqrt(Math.pow(cx - mouse[0], 2) + Math.pow(cy - mouse[1], 2)) < r;
  7877. };
  7878. c3_chart_internal_fn.isWithinStep = function (that, y) {
  7879. return Math.abs(y - this.d3.mouse(that)[1]) < 30;
  7880. };
  7881. c3_chart_internal_fn.getCurrentWidth = function () {
  7882. var $$ = this,
  7883. config = $$.config;
  7884. return config.size_width ? config.size_width : $$.getParentWidth();
  7885. };
  7886. c3_chart_internal_fn.getCurrentHeight = function () {
  7887. var $$ = this,
  7888. config = $$.config,
  7889. h = config.size_height ? config.size_height : $$.getParentHeight();
  7890. return h > 0 ? h : 320 / ($$.hasType('gauge') && !config.gauge_fullCircle ? 2 : 1);
  7891. };
  7892. c3_chart_internal_fn.getCurrentPaddingTop = function () {
  7893. var $$ = this,
  7894. config = $$.config,
  7895. padding = isValue(config.padding_top) ? config.padding_top : 0;
  7896. if ($$.title && $$.title.node()) {
  7897. padding += $$.getTitlePadding();
  7898. }
  7899. return padding;
  7900. };
  7901. c3_chart_internal_fn.getCurrentPaddingBottom = function () {
  7902. var config = this.config;
  7903. return isValue(config.padding_bottom) ? config.padding_bottom : 0;
  7904. };
  7905. c3_chart_internal_fn.getCurrentPaddingLeft = function (withoutRecompute) {
  7906. var $$ = this,
  7907. config = $$.config;
  7908. if (isValue(config.padding_left)) {
  7909. return config.padding_left;
  7910. } else if (config.axis_rotated) {
  7911. return !config.axis_x_show ? 1 : Math.max(ceil10($$.getAxisWidthByAxisId('x', withoutRecompute)), 40);
  7912. } else if (!config.axis_y_show || config.axis_y_inner) {
  7913. // && !config.axis_rotated
  7914. return $$.axis.getYAxisLabelPosition().isOuter ? 30 : 1;
  7915. } else {
  7916. return ceil10($$.getAxisWidthByAxisId('y', withoutRecompute));
  7917. }
  7918. };
  7919. c3_chart_internal_fn.getCurrentPaddingRight = function () {
  7920. var $$ = this,
  7921. config = $$.config,
  7922. defaultPadding = 10,
  7923. legendWidthOnRight = $$.isLegendRight ? $$.getLegendWidth() + 20 : 0;
  7924. if (isValue(config.padding_right)) {
  7925. return config.padding_right + 1; // 1 is needed not to hide tick line
  7926. } else if (config.axis_rotated) {
  7927. return defaultPadding + legendWidthOnRight;
  7928. } else if (!config.axis_y2_show || config.axis_y2_inner) {
  7929. // && !config.axis_rotated
  7930. return 2 + legendWidthOnRight + ($$.axis.getY2AxisLabelPosition().isOuter ? 20 : 0);
  7931. } else {
  7932. return ceil10($$.getAxisWidthByAxisId('y2')) + legendWidthOnRight;
  7933. }
  7934. };
  7935. c3_chart_internal_fn.getParentRectValue = function (key) {
  7936. var parent = this.selectChart.node(),
  7937. v;
  7938. while (parent && parent.tagName !== 'BODY') {
  7939. try {
  7940. v = parent.getBoundingClientRect()[key];
  7941. } catch (e) {
  7942. if (key === 'width') {
  7943. // In IE in certain cases getBoundingClientRect
  7944. // will cause an "unspecified error"
  7945. v = parent.offsetWidth;
  7946. }
  7947. }
  7948. if (v) {
  7949. break;
  7950. }
  7951. parent = parent.parentNode;
  7952. }
  7953. return v;
  7954. };
  7955. c3_chart_internal_fn.getParentWidth = function () {
  7956. return this.getParentRectValue('width');
  7957. };
  7958. c3_chart_internal_fn.getParentHeight = function () {
  7959. var h = this.selectChart.style('height');
  7960. return h.indexOf('px') > 0 ? +h.replace('px', '') : 0;
  7961. };
  7962. c3_chart_internal_fn.getSvgLeft = function (withoutRecompute) {
  7963. var $$ = this,
  7964. config = $$.config,
  7965. hasLeftAxisRect = config.axis_rotated || !config.axis_rotated && !config.axis_y_inner,
  7966. leftAxisClass = config.axis_rotated ? CLASS.axisX : CLASS.axisY,
  7967. leftAxis = $$.main.select('.' + leftAxisClass).node(),
  7968. svgRect = leftAxis && hasLeftAxisRect ? leftAxis.getBoundingClientRect() : { right: 0 },
  7969. chartRect = $$.selectChart.node().getBoundingClientRect(),
  7970. hasArc = $$.hasArcType(),
  7971. svgLeft = svgRect.right - chartRect.left - (hasArc ? 0 : $$.getCurrentPaddingLeft(withoutRecompute));
  7972. return svgLeft > 0 ? svgLeft : 0;
  7973. };
  7974. c3_chart_internal_fn.getAxisWidthByAxisId = function (id, withoutRecompute) {
  7975. var $$ = this,
  7976. position = $$.axis.getLabelPositionById(id);
  7977. return $$.axis.getMaxTickWidth(id, withoutRecompute) + (position.isInner ? 20 : 40);
  7978. };
  7979. c3_chart_internal_fn.getHorizontalAxisHeight = function (axisId) {
  7980. var $$ = this,
  7981. config = $$.config,
  7982. h = 30;
  7983. if (axisId === 'x' && !config.axis_x_show) {
  7984. return 8;
  7985. }
  7986. if (axisId === 'x' && config.axis_x_height) {
  7987. return config.axis_x_height;
  7988. }
  7989. if (axisId === 'y' && !config.axis_y_show) {
  7990. return config.legend_show && !$$.isLegendRight && !$$.isLegendInset ? 10 : 1;
  7991. }
  7992. if (axisId === 'y2' && !config.axis_y2_show) {
  7993. return $$.rotated_padding_top;
  7994. }
  7995. // Calculate x axis height when tick rotated
  7996. if (axisId === 'x' && !config.axis_rotated && config.axis_x_tick_rotate) {
  7997. h = 30 + $$.axis.getMaxTickWidth(axisId) * Math.cos(Math.PI * (90 - config.axis_x_tick_rotate) / 180);
  7998. }
  7999. // Calculate y axis height when tick rotated
  8000. if (axisId === 'y' && config.axis_rotated && config.axis_y_tick_rotate) {
  8001. h = 30 + $$.axis.getMaxTickWidth(axisId) * Math.cos(Math.PI * (90 - config.axis_y_tick_rotate) / 180);
  8002. }
  8003. return h + ($$.axis.getLabelPositionById(axisId).isInner ? 0 : 10) + (axisId === 'y2' ? -10 : 0);
  8004. };
  8005. c3_chart_internal_fn.getEventRectWidth = function () {
  8006. return Math.max(0, this.xAxis.tickInterval());
  8007. };
  8008. c3_chart_internal_fn.initBrush = function () {
  8009. var $$ = this,
  8010. d3 = $$.d3;
  8011. $$.brush = d3.svg.brush().on("brush", function () {
  8012. $$.redrawForBrush();
  8013. });
  8014. $$.brush.update = function () {
  8015. if ($$.context) {
  8016. $$.context.select('.' + CLASS.brush).call(this);
  8017. }
  8018. return this;
  8019. };
  8020. $$.brush.scale = function (scale) {
  8021. return $$.config.axis_rotated ? this.y(scale) : this.x(scale);
  8022. };
  8023. };
  8024. c3_chart_internal_fn.initSubchart = function () {
  8025. var $$ = this,
  8026. config = $$.config,
  8027. context = $$.context = $$.svg.append("g").attr("transform", $$.getTranslate('context')),
  8028. visibility = config.subchart_show ? 'visible' : 'hidden';
  8029. context.style('visibility', visibility);
  8030. // Define g for chart area
  8031. context.append('g').attr("clip-path", $$.clipPathForSubchart).attr('class', CLASS.chart);
  8032. // Define g for bar chart area
  8033. context.select('.' + CLASS.chart).append("g").attr("class", CLASS.chartBars);
  8034. // Define g for line chart area
  8035. context.select('.' + CLASS.chart).append("g").attr("class", CLASS.chartLines);
  8036. // Add extent rect for Brush
  8037. context.append("g").attr("clip-path", $$.clipPath).attr("class", CLASS.brush).call($$.brush);
  8038. // ATTENTION: This must be called AFTER chart added
  8039. // Add Axis
  8040. $$.axes.subx = context.append("g").attr("class", CLASS.axisX).attr("transform", $$.getTranslate('subx')).attr("clip-path", config.axis_rotated ? "" : $$.clipPathForXAxis).style("visibility", config.subchart_axis_x_show ? visibility : 'hidden');
  8041. };
  8042. c3_chart_internal_fn.updateTargetsForSubchart = function (targets) {
  8043. var $$ = this,
  8044. context = $$.context,
  8045. config = $$.config,
  8046. contextLineEnter,
  8047. contextLineUpdate,
  8048. contextBarEnter,
  8049. contextBarUpdate,
  8050. classChartBar = $$.classChartBar.bind($$),
  8051. classBars = $$.classBars.bind($$),
  8052. classChartLine = $$.classChartLine.bind($$),
  8053. classLines = $$.classLines.bind($$),
  8054. classAreas = $$.classAreas.bind($$);
  8055. if (config.subchart_show) {
  8056. //-- Bar --//
  8057. contextBarUpdate = context.select('.' + CLASS.chartBars).selectAll('.' + CLASS.chartBar).data(targets).attr('class', classChartBar);
  8058. contextBarEnter = contextBarUpdate.enter().append('g').style('opacity', 0).attr('class', classChartBar);
  8059. // Bars for each data
  8060. contextBarEnter.append('g').attr("class", classBars);
  8061. //-- Line --//
  8062. contextLineUpdate = context.select('.' + CLASS.chartLines).selectAll('.' + CLASS.chartLine).data(targets).attr('class', classChartLine);
  8063. contextLineEnter = contextLineUpdate.enter().append('g').style('opacity', 0).attr('class', classChartLine);
  8064. // Lines for each data
  8065. contextLineEnter.append("g").attr("class", classLines);
  8066. // Area
  8067. contextLineEnter.append("g").attr("class", classAreas);
  8068. //-- Brush --//
  8069. context.selectAll('.' + CLASS.brush + ' rect').attr(config.axis_rotated ? "width" : "height", config.axis_rotated ? $$.width2 : $$.height2);
  8070. }
  8071. };
  8072. c3_chart_internal_fn.updateBarForSubchart = function (durationForExit) {
  8073. var $$ = this;
  8074. $$.contextBar = $$.context.selectAll('.' + CLASS.bars).selectAll('.' + CLASS.bar).data($$.barData.bind($$));
  8075. $$.contextBar.enter().append('path').attr("class", $$.classBar.bind($$)).style("stroke", 'none').style("fill", $$.color);
  8076. $$.contextBar.style("opacity", $$.initialOpacity.bind($$));
  8077. $$.contextBar.exit().transition().duration(durationForExit).style('opacity', 0).remove();
  8078. };
  8079. c3_chart_internal_fn.redrawBarForSubchart = function (drawBarOnSub, withTransition, duration) {
  8080. (withTransition ? this.contextBar.transition(Math.random().toString()).duration(duration) : this.contextBar).attr('d', drawBarOnSub).style('opacity', 1);
  8081. };
  8082. c3_chart_internal_fn.updateLineForSubchart = function (durationForExit) {
  8083. var $$ = this;
  8084. $$.contextLine = $$.context.selectAll('.' + CLASS.lines).selectAll('.' + CLASS.line).data($$.lineData.bind($$));
  8085. $$.contextLine.enter().append('path').attr('class', $$.classLine.bind($$)).style('stroke', $$.color);
  8086. $$.contextLine.style("opacity", $$.initialOpacity.bind($$));
  8087. $$.contextLine.exit().transition().duration(durationForExit).style('opacity', 0).remove();
  8088. };
  8089. c3_chart_internal_fn.redrawLineForSubchart = function (drawLineOnSub, withTransition, duration) {
  8090. (withTransition ? this.contextLine.transition(Math.random().toString()).duration(duration) : this.contextLine).attr("d", drawLineOnSub).style('opacity', 1);
  8091. };
  8092. c3_chart_internal_fn.updateAreaForSubchart = function (durationForExit) {
  8093. var $$ = this,
  8094. d3 = $$.d3;
  8095. $$.contextArea = $$.context.selectAll('.' + CLASS.areas).selectAll('.' + CLASS.area).data($$.lineData.bind($$));
  8096. $$.contextArea.enter().append('path').attr("class", $$.classArea.bind($$)).style("fill", $$.color).style("opacity", function () {
  8097. $$.orgAreaOpacity = +d3.select(this).style('opacity');return 0;
  8098. });
  8099. $$.contextArea.style("opacity", 0);
  8100. $$.contextArea.exit().transition().duration(durationForExit).style('opacity', 0).remove();
  8101. };
  8102. c3_chart_internal_fn.redrawAreaForSubchart = function (drawAreaOnSub, withTransition, duration) {
  8103. (withTransition ? this.contextArea.transition(Math.random().toString()).duration(duration) : this.contextArea).attr("d", drawAreaOnSub).style("fill", this.color).style("opacity", this.orgAreaOpacity);
  8104. };
  8105. c3_chart_internal_fn.redrawSubchart = function (withSubchart, transitions, duration, durationForExit, areaIndices, barIndices, lineIndices) {
  8106. var $$ = this,
  8107. d3 = $$.d3,
  8108. config = $$.config,
  8109. drawAreaOnSub,
  8110. drawBarOnSub,
  8111. drawLineOnSub;
  8112. $$.context.style('visibility', config.subchart_show ? 'visible' : 'hidden');
  8113. // subchart
  8114. if (config.subchart_show) {
  8115. // reflect main chart to extent on subchart if zoomed
  8116. if (d3.event && d3.event.type === 'zoom') {
  8117. $$.brush.extent($$.x.orgDomain()).update();
  8118. }
  8119. // update subchart elements if needed
  8120. if (withSubchart) {
  8121. // extent rect
  8122. if (!$$.brush.empty()) {
  8123. $$.brush.extent($$.x.orgDomain()).update();
  8124. }
  8125. // setup drawer - MEMO: this must be called after axis updated
  8126. drawAreaOnSub = $$.generateDrawArea(areaIndices, true);
  8127. drawBarOnSub = $$.generateDrawBar(barIndices, true);
  8128. drawLineOnSub = $$.generateDrawLine(lineIndices, true);
  8129. $$.updateBarForSubchart(duration);
  8130. $$.updateLineForSubchart(duration);
  8131. $$.updateAreaForSubchart(duration);
  8132. $$.redrawBarForSubchart(drawBarOnSub, duration, duration);
  8133. $$.redrawLineForSubchart(drawLineOnSub, duration, duration);
  8134. $$.redrawAreaForSubchart(drawAreaOnSub, duration, duration);
  8135. }
  8136. }
  8137. };
  8138. c3_chart_internal_fn.redrawForBrush = function () {
  8139. var $$ = this,
  8140. x = $$.x;
  8141. $$.redraw({
  8142. withTransition: false,
  8143. withY: $$.config.zoom_rescale,
  8144. withSubchart: false,
  8145. withUpdateXDomain: true,
  8146. withDimension: false
  8147. });
  8148. $$.config.subchart_onbrush.call($$.api, x.orgDomain());
  8149. };
  8150. c3_chart_internal_fn.transformContext = function (withTransition, transitions) {
  8151. var $$ = this,
  8152. subXAxis;
  8153. if (transitions && transitions.axisSubX) {
  8154. subXAxis = transitions.axisSubX;
  8155. } else {
  8156. subXAxis = $$.context.select('.' + CLASS.axisX);
  8157. if (withTransition) {
  8158. subXAxis = subXAxis.transition();
  8159. }
  8160. }
  8161. $$.context.attr("transform", $$.getTranslate('context'));
  8162. subXAxis.attr("transform", $$.getTranslate('subx'));
  8163. };
  8164. c3_chart_internal_fn.getDefaultExtent = function () {
  8165. var $$ = this,
  8166. config = $$.config,
  8167. extent = isFunction(config.axis_x_extent) ? config.axis_x_extent($$.getXDomain($$.data.targets)) : config.axis_x_extent;
  8168. if ($$.isTimeSeries()) {
  8169. extent = [$$.parseDate(extent[0]), $$.parseDate(extent[1])];
  8170. }
  8171. return extent;
  8172. };
  8173. c3_chart_internal_fn.initText = function () {
  8174. var $$ = this;
  8175. $$.main.select('.' + CLASS.chart).append("g").attr("class", CLASS.chartTexts);
  8176. $$.mainText = $$.d3.selectAll([]);
  8177. };
  8178. c3_chart_internal_fn.updateTargetsForText = function (targets) {
  8179. var $$ = this,
  8180. mainTextUpdate,
  8181. mainTextEnter,
  8182. classChartText = $$.classChartText.bind($$),
  8183. classTexts = $$.classTexts.bind($$),
  8184. classFocus = $$.classFocus.bind($$);
  8185. mainTextUpdate = $$.main.select('.' + CLASS.chartTexts).selectAll('.' + CLASS.chartText).data(targets).attr('class', function (d) {
  8186. return classChartText(d) + classFocus(d);
  8187. });
  8188. mainTextEnter = mainTextUpdate.enter().append('g').attr('class', classChartText).style('opacity', 0).style("pointer-events", "none");
  8189. mainTextEnter.append('g').attr('class', classTexts);
  8190. };
  8191. c3_chart_internal_fn.updateText = function (durationForExit) {
  8192. var $$ = this,
  8193. config = $$.config,
  8194. barOrLineData = $$.barOrLineData.bind($$),
  8195. classText = $$.classText.bind($$);
  8196. $$.mainText = $$.main.selectAll('.' + CLASS.texts).selectAll('.' + CLASS.text).data(barOrLineData);
  8197. $$.mainText.enter().append('text').attr("class", classText).attr('text-anchor', function (d) {
  8198. return config.axis_rotated ? d.value < 0 ? 'end' : 'start' : 'middle';
  8199. }).style("stroke", 'none').style("fill", function (d) {
  8200. return $$.color(d);
  8201. }).style("fill-opacity", 0);
  8202. $$.mainText.text(function (d, i, j) {
  8203. return $$.dataLabelFormat(d.id)(d.value, d.id, i, j);
  8204. });
  8205. $$.mainText.exit().transition().duration(durationForExit).style('fill-opacity', 0).remove();
  8206. };
  8207. c3_chart_internal_fn.redrawText = function (xForText, yForText, forFlow, withTransition) {
  8208. return [(withTransition ? this.mainText.transition() : this.mainText).attr('x', xForText).attr('y', yForText).style("fill", this.color).style("fill-opacity", forFlow ? 0 : this.opacityForText.bind(this))];
  8209. };
  8210. c3_chart_internal_fn.getTextRect = function (text, cls, element) {
  8211. var dummy = this.d3.select('body').append('div').classed('c3', true),
  8212. svg = dummy.append("svg").style('visibility', 'hidden').style('position', 'fixed').style('top', 0).style('left', 0),
  8213. font = this.d3.select(element).style('font'),
  8214. rect;
  8215. svg.selectAll('.dummy').data([text]).enter().append('text').classed(cls ? cls : "", true).style('font', font).text(text).each(function () {
  8216. rect = this.getBoundingClientRect();
  8217. });
  8218. dummy.remove();
  8219. return rect;
  8220. };
  8221. c3_chart_internal_fn.generateXYForText = function (areaIndices, barIndices, lineIndices, forX) {
  8222. var $$ = this,
  8223. getAreaPoints = $$.generateGetAreaPoints(areaIndices, false),
  8224. getBarPoints = $$.generateGetBarPoints(barIndices, false),
  8225. getLinePoints = $$.generateGetLinePoints(lineIndices, false),
  8226. getter = forX ? $$.getXForText : $$.getYForText;
  8227. return function (d, i) {
  8228. var getPoints = $$.isAreaType(d) ? getAreaPoints : $$.isBarType(d) ? getBarPoints : getLinePoints;
  8229. return getter.call($$, getPoints(d, i), d, this);
  8230. };
  8231. };
  8232. c3_chart_internal_fn.getXForText = function (points, d, textElement) {
  8233. var $$ = this,
  8234. box = textElement.getBoundingClientRect(),
  8235. xPos,
  8236. padding;
  8237. if ($$.config.axis_rotated) {
  8238. padding = $$.isBarType(d) ? 4 : 6;
  8239. xPos = points[2][1] + padding * (d.value < 0 ? -1 : 1);
  8240. } else {
  8241. xPos = $$.hasType('bar') ? (points[2][0] + points[0][0]) / 2 : points[0][0];
  8242. }
  8243. // show labels regardless of the domain if value is null
  8244. if (d.value === null) {
  8245. if (xPos > $$.width) {
  8246. xPos = $$.width - box.width;
  8247. } else if (xPos < 0) {
  8248. xPos = 4;
  8249. }
  8250. }
  8251. return xPos;
  8252. };
  8253. c3_chart_internal_fn.getYForText = function (points, d, textElement) {
  8254. var $$ = this,
  8255. box = textElement.getBoundingClientRect(),
  8256. yPos;
  8257. if ($$.config.axis_rotated) {
  8258. yPos = (points[0][0] + points[2][0] + box.height * 0.6) / 2;
  8259. } else {
  8260. yPos = points[2][1];
  8261. if (d.value < 0 || d.value === 0 && !$$.hasPositiveValue) {
  8262. yPos += box.height;
  8263. if ($$.isBarType(d) && $$.isSafari()) {
  8264. yPos -= 3;
  8265. } else if (!$$.isBarType(d) && $$.isChrome()) {
  8266. yPos += 3;
  8267. }
  8268. } else {
  8269. yPos += $$.isBarType(d) ? -3 : -6;
  8270. }
  8271. }
  8272. // show labels regardless of the domain if value is null
  8273. if (d.value === null && !$$.config.axis_rotated) {
  8274. if (yPos < box.height) {
  8275. yPos = box.height;
  8276. } else if (yPos > this.height) {
  8277. yPos = this.height - 4;
  8278. }
  8279. }
  8280. return yPos;
  8281. };
  8282. c3_chart_internal_fn.initTitle = function () {
  8283. var $$ = this;
  8284. $$.title = $$.svg.append("text").text($$.config.title_text).attr("class", $$.CLASS.title);
  8285. };
  8286. c3_chart_internal_fn.redrawTitle = function () {
  8287. var $$ = this;
  8288. $$.title.attr("x", $$.xForTitle.bind($$)).attr("y", $$.yForTitle.bind($$));
  8289. };
  8290. c3_chart_internal_fn.xForTitle = function () {
  8291. var $$ = this,
  8292. config = $$.config,
  8293. position = config.title_position || 'left',
  8294. x;
  8295. if (position.indexOf('right') >= 0) {
  8296. x = $$.currentWidth - $$.getTextRect($$.title.node().textContent, $$.CLASS.title, $$.title.node()).width - config.title_padding.right;
  8297. } else if (position.indexOf('center') >= 0) {
  8298. x = ($$.currentWidth - $$.getTextRect($$.title.node().textContent, $$.CLASS.title, $$.title.node()).width) / 2;
  8299. } else {
  8300. // left
  8301. x = config.title_padding.left;
  8302. }
  8303. return x;
  8304. };
  8305. c3_chart_internal_fn.yForTitle = function () {
  8306. var $$ = this;
  8307. return $$.config.title_padding.top + $$.getTextRect($$.title.node().textContent, $$.CLASS.title, $$.title.node()).height;
  8308. };
  8309. c3_chart_internal_fn.getTitlePadding = function () {
  8310. var $$ = this;
  8311. return $$.yForTitle() + $$.config.title_padding.bottom;
  8312. };
  8313. c3_chart_internal_fn.initTooltip = function () {
  8314. var $$ = this,
  8315. config = $$.config,
  8316. i;
  8317. $$.tooltip = $$.selectChart.style("position", "relative").append("div").attr('class', CLASS.tooltipContainer).style("position", "absolute").style("pointer-events", "none").style("display", "none");
  8318. // Show tooltip if needed
  8319. if (config.tooltip_init_show) {
  8320. if ($$.isTimeSeries() && isString(config.tooltip_init_x)) {
  8321. config.tooltip_init_x = $$.parseDate(config.tooltip_init_x);
  8322. for (i = 0; i < $$.data.targets[0].values.length; i++) {
  8323. if ($$.data.targets[0].values[i].x - config.tooltip_init_x === 0) {
  8324. break;
  8325. }
  8326. }
  8327. config.tooltip_init_x = i;
  8328. }
  8329. $$.tooltip.html(config.tooltip_contents.call($$, $$.data.targets.map(function (d) {
  8330. return $$.addName(d.values[config.tooltip_init_x]);
  8331. }), $$.axis.getXAxisTickFormat(), $$.getYFormat($$.hasArcType()), $$.color));
  8332. $$.tooltip.style("top", config.tooltip_init_position.top).style("left", config.tooltip_init_position.left).style("display", "block");
  8333. }
  8334. };
  8335. c3_chart_internal_fn.getTooltipSortFunction = function () {
  8336. var $$ = this,
  8337. config = $$.config;
  8338. if (config.data_groups.length === 0 || config.tooltip_order !== undefined) {
  8339. // if data are not grouped or if an order is specified
  8340. // for the tooltip values we sort them by their values
  8341. var order = config.tooltip_order;
  8342. if (order === undefined) {
  8343. order = config.data_order;
  8344. }
  8345. var valueOf = function valueOf(obj) {
  8346. return obj ? obj.value : null;
  8347. };
  8348. // if data are not grouped, we sort them by their value
  8349. if (isString(order) && order.toLowerCase() === 'asc') {
  8350. return function (a, b) {
  8351. return valueOf(a) - valueOf(b);
  8352. };
  8353. } else if (isString(order) && order.toLowerCase() === 'desc') {
  8354. return function (a, b) {
  8355. return valueOf(b) - valueOf(a);
  8356. };
  8357. } else if (isFunction(order)) {
  8358. // if the function is from data_order we need
  8359. // to wrap the returned function in order to format
  8360. // the sorted value to the expected format
  8361. var sortFunction = order;
  8362. if (config.tooltip_order === undefined) {
  8363. sortFunction = function sortFunction(a, b) {
  8364. return order(a ? {
  8365. id: a.id,
  8366. values: [a]
  8367. } : null, b ? {
  8368. id: b.id,
  8369. values: [b]
  8370. } : null);
  8371. };
  8372. }
  8373. return sortFunction;
  8374. } else if (isArray(order)) {
  8375. return function (a, b) {
  8376. return order.indexOf(a.id) - order.indexOf(b.id);
  8377. };
  8378. }
  8379. } else {
  8380. // if data are grouped, we follow the order of grouped targets
  8381. var ids = $$.orderTargets($$.data.targets).map(function (i) {
  8382. return i.id;
  8383. });
  8384. // if it was either asc or desc we need to invert the order
  8385. // returned by orderTargets
  8386. if ($$.isOrderAsc() || $$.isOrderDesc()) {
  8387. ids = ids.reverse();
  8388. }
  8389. return function (a, b) {
  8390. return ids.indexOf(a.id) - ids.indexOf(b.id);
  8391. };
  8392. }
  8393. };
  8394. c3_chart_internal_fn.getTooltipContent = function (d, defaultTitleFormat, defaultValueFormat, color) {
  8395. var $$ = this,
  8396. config = $$.config,
  8397. titleFormat = config.tooltip_format_title || defaultTitleFormat,
  8398. nameFormat = config.tooltip_format_name || function (name) {
  8399. return name;
  8400. },
  8401. valueFormat = config.tooltip_format_value || defaultValueFormat,
  8402. text,
  8403. i,
  8404. title,
  8405. value,
  8406. name,
  8407. bgcolor;
  8408. var tooltipSortFunction = this.getTooltipSortFunction();
  8409. if (tooltipSortFunction) {
  8410. d.sort(tooltipSortFunction);
  8411. }
  8412. for (i = 0; i < d.length; i++) {
  8413. if (!(d[i] && (d[i].value || d[i].value === 0))) {
  8414. continue;
  8415. }
  8416. if (!text) {
  8417. title = sanitise(titleFormat ? titleFormat(d[i].x) : d[i].x);
  8418. text = "<table class='" + $$.CLASS.tooltip + "'>" + (title || title === 0 ? "<tr><th colspan='2'>" + title + "</th></tr>" : "");
  8419. }
  8420. value = sanitise(valueFormat(d[i].value, d[i].ratio, d[i].id, d[i].index, d));
  8421. if (value !== undefined) {
  8422. // Skip elements when their name is set to null
  8423. if (d[i].name === null) {
  8424. continue;
  8425. }
  8426. name = sanitise(nameFormat(d[i].name, d[i].ratio, d[i].id, d[i].index));
  8427. bgcolor = $$.levelColor ? $$.levelColor(d[i].value) : color(d[i].id);
  8428. text += "<tr class='" + $$.CLASS.tooltipName + "-" + $$.getTargetSelectorSuffix(d[i].id) + "'>";
  8429. text += "<td class='name'><span style='background-color:" + bgcolor + "'></span>" + name + "</td>";
  8430. text += "<td class='value'>" + value + "</td>";
  8431. text += "</tr>";
  8432. }
  8433. }
  8434. return text + "</table>";
  8435. };
  8436. c3_chart_internal_fn.tooltipPosition = function (dataToShow, tWidth, tHeight, element) {
  8437. var $$ = this,
  8438. config = $$.config,
  8439. d3 = $$.d3;
  8440. var svgLeft, tooltipLeft, tooltipRight, tooltipTop, chartRight;
  8441. var forArc = $$.hasArcType(),
  8442. mouse = d3.mouse(element);
  8443. // Determin tooltip position
  8444. if (forArc) {
  8445. tooltipLeft = ($$.width - ($$.isLegendRight ? $$.getLegendWidth() : 0)) / 2 + mouse[0];
  8446. tooltipTop = ($$.hasType('gauge') ? $$.height : $$.height / 2) + mouse[1] + 20;
  8447. } else {
  8448. svgLeft = $$.getSvgLeft(true);
  8449. if (config.axis_rotated) {
  8450. tooltipLeft = svgLeft + mouse[0] + 100;
  8451. tooltipRight = tooltipLeft + tWidth;
  8452. chartRight = $$.currentWidth - $$.getCurrentPaddingRight();
  8453. tooltipTop = $$.x(dataToShow[0].x) + 20;
  8454. } else {
  8455. tooltipLeft = svgLeft + $$.getCurrentPaddingLeft(true) + $$.x(dataToShow[0].x) + 20;
  8456. tooltipRight = tooltipLeft + tWidth;
  8457. chartRight = svgLeft + $$.currentWidth - $$.getCurrentPaddingRight();
  8458. tooltipTop = mouse[1] + 15;
  8459. }
  8460. if (tooltipRight > chartRight) {
  8461. // 20 is needed for Firefox to keep tooltip width
  8462. tooltipLeft -= tooltipRight - chartRight + 20;
  8463. }
  8464. if (tooltipTop + tHeight > $$.currentHeight) {
  8465. tooltipTop -= tHeight + 30;
  8466. }
  8467. }
  8468. if (tooltipTop < 0) {
  8469. tooltipTop = 0;
  8470. }
  8471. return { top: tooltipTop, left: tooltipLeft };
  8472. };
  8473. c3_chart_internal_fn.showTooltip = function (selectedData, element) {
  8474. var $$ = this,
  8475. config = $$.config;
  8476. var tWidth, tHeight, position;
  8477. var forArc = $$.hasArcType(),
  8478. dataToShow = selectedData.filter(function (d) {
  8479. return d && isValue(d.value);
  8480. }),
  8481. positionFunction = config.tooltip_position || c3_chart_internal_fn.tooltipPosition;
  8482. if (dataToShow.length === 0 || !config.tooltip_show) {
  8483. return;
  8484. }
  8485. $$.tooltip.html(config.tooltip_contents.call($$, selectedData, $$.axis.getXAxisTickFormat(), $$.getYFormat(forArc), $$.color)).style("display", "block");
  8486. // Get tooltip dimensions
  8487. tWidth = $$.tooltip.property('offsetWidth');
  8488. tHeight = $$.tooltip.property('offsetHeight');
  8489. position = positionFunction.call(this, dataToShow, tWidth, tHeight, element);
  8490. // Set tooltip
  8491. $$.tooltip.style("top", position.top + "px").style("left", position.left + 'px');
  8492. };
  8493. c3_chart_internal_fn.hideTooltip = function () {
  8494. this.tooltip.style("display", "none");
  8495. };
  8496. c3_chart_internal_fn.setTargetType = function (targetIds, type) {
  8497. var $$ = this,
  8498. config = $$.config;
  8499. $$.mapToTargetIds(targetIds).forEach(function (id) {
  8500. $$.withoutFadeIn[id] = type === config.data_types[id];
  8501. config.data_types[id] = type;
  8502. });
  8503. if (!targetIds) {
  8504. config.data_type = type;
  8505. }
  8506. };
  8507. c3_chart_internal_fn.hasType = function (type, targets) {
  8508. var $$ = this,
  8509. types = $$.config.data_types,
  8510. has = false;
  8511. targets = targets || $$.data.targets;
  8512. if (targets && targets.length) {
  8513. targets.forEach(function (target) {
  8514. var t = types[target.id];
  8515. if (t && t.indexOf(type) >= 0 || !t && type === 'line') {
  8516. has = true;
  8517. }
  8518. });
  8519. } else if (Object.keys(types).length) {
  8520. Object.keys(types).forEach(function (id) {
  8521. if (types[id] === type) {
  8522. has = true;
  8523. }
  8524. });
  8525. } else {
  8526. has = $$.config.data_type === type;
  8527. }
  8528. return has;
  8529. };
  8530. c3_chart_internal_fn.hasArcType = function (targets) {
  8531. return this.hasType('pie', targets) || this.hasType('donut', targets) || this.hasType('gauge', targets);
  8532. };
  8533. c3_chart_internal_fn.isLineType = function (d) {
  8534. var config = this.config,
  8535. id = isString(d) ? d : d.id;
  8536. return !config.data_types[id] || ['line', 'spline', 'area', 'area-spline', 'step', 'area-step'].indexOf(config.data_types[id]) >= 0;
  8537. };
  8538. c3_chart_internal_fn.isStepType = function (d) {
  8539. var id = isString(d) ? d : d.id;
  8540. return ['step', 'area-step'].indexOf(this.config.data_types[id]) >= 0;
  8541. };
  8542. c3_chart_internal_fn.isSplineType = function (d) {
  8543. var id = isString(d) ? d : d.id;
  8544. return ['spline', 'area-spline'].indexOf(this.config.data_types[id]) >= 0;
  8545. };
  8546. c3_chart_internal_fn.isAreaType = function (d) {
  8547. var id = isString(d) ? d : d.id;
  8548. return ['area', 'area-spline', 'area-step'].indexOf(this.config.data_types[id]) >= 0;
  8549. };
  8550. c3_chart_internal_fn.isBarType = function (d) {
  8551. var id = isString(d) ? d : d.id;
  8552. return this.config.data_types[id] === 'bar';
  8553. };
  8554. c3_chart_internal_fn.isScatterType = function (d) {
  8555. var id = isString(d) ? d : d.id;
  8556. return this.config.data_types[id] === 'scatter';
  8557. };
  8558. c3_chart_internal_fn.isPieType = function (d) {
  8559. var id = isString(d) ? d : d.id;
  8560. return this.config.data_types[id] === 'pie';
  8561. };
  8562. c3_chart_internal_fn.isGaugeType = function (d) {
  8563. var id = isString(d) ? d : d.id;
  8564. return this.config.data_types[id] === 'gauge';
  8565. };
  8566. c3_chart_internal_fn.isDonutType = function (d) {
  8567. var id = isString(d) ? d : d.id;
  8568. return this.config.data_types[id] === 'donut';
  8569. };
  8570. c3_chart_internal_fn.isArcType = function (d) {
  8571. return this.isPieType(d) || this.isDonutType(d) || this.isGaugeType(d);
  8572. };
  8573. c3_chart_internal_fn.lineData = function (d) {
  8574. return this.isLineType(d) ? [d] : [];
  8575. };
  8576. c3_chart_internal_fn.arcData = function (d) {
  8577. return this.isArcType(d.data) ? [d] : [];
  8578. };
  8579. /* not used
  8580. function scatterData(d) {
  8581. return isScatterType(d) ? d.values : [];
  8582. }
  8583. */
  8584. c3_chart_internal_fn.barData = function (d) {
  8585. return this.isBarType(d) ? d.values : [];
  8586. };
  8587. c3_chart_internal_fn.lineOrScatterData = function (d) {
  8588. return this.isLineType(d) || this.isScatterType(d) ? d.values : [];
  8589. };
  8590. c3_chart_internal_fn.barOrLineData = function (d) {
  8591. return this.isBarType(d) || this.isLineType(d) ? d.values : [];
  8592. };
  8593. c3_chart_internal_fn.isInterpolationType = function (type) {
  8594. return ['linear', 'linear-closed', 'basis', 'basis-open', 'basis-closed', 'bundle', 'cardinal', 'cardinal-open', 'cardinal-closed', 'monotone'].indexOf(type) >= 0;
  8595. };
  8596. c3_chart_internal_fn.isSafari = function () {
  8597. var ua = window.navigator.userAgent;
  8598. return ua.indexOf('Safari') >= 0 && ua.indexOf('Chrome') < 0;
  8599. };
  8600. c3_chart_internal_fn.isChrome = function () {
  8601. var ua = window.navigator.userAgent;
  8602. return ua.indexOf('Chrome') >= 0;
  8603. };
  8604. c3_chart_internal_fn.initZoom = function () {
  8605. var $$ = this,
  8606. d3 = $$.d3,
  8607. config = $$.config,
  8608. startEvent;
  8609. $$.zoom = d3.behavior.zoom().on("zoomstart", function () {
  8610. startEvent = d3.event.sourceEvent;
  8611. $$.zoom.altDomain = d3.event.sourceEvent.altKey ? $$.x.orgDomain() : null;
  8612. config.zoom_onzoomstart.call($$.api, d3.event.sourceEvent);
  8613. }).on("zoom", function () {
  8614. $$.redrawForZoom.call($$);
  8615. }).on('zoomend', function () {
  8616. var event = d3.event.sourceEvent;
  8617. // if click, do nothing. otherwise, click interaction will be canceled.
  8618. if (event && startEvent.clientX === event.clientX && startEvent.clientY === event.clientY) {
  8619. return;
  8620. }
  8621. $$.redrawEventRect();
  8622. $$.updateZoom();
  8623. config.zoom_onzoomend.call($$.api, $$.x.orgDomain());
  8624. });
  8625. $$.zoom.scale = function (scale) {
  8626. return config.axis_rotated ? this.y(scale) : this.x(scale);
  8627. };
  8628. $$.zoom.orgScaleExtent = function () {
  8629. var extent = config.zoom_extent ? config.zoom_extent : [1, 10];
  8630. return [extent[0], Math.max($$.getMaxDataCount() / extent[1], extent[1])];
  8631. };
  8632. $$.zoom.updateScaleExtent = function () {
  8633. var ratio = diffDomain($$.x.orgDomain()) / diffDomain($$.getZoomDomain()),
  8634. extent = this.orgScaleExtent();
  8635. this.scaleExtent([extent[0] * ratio, extent[1] * ratio]);
  8636. return this;
  8637. };
  8638. };
  8639. c3_chart_internal_fn.getZoomDomain = function () {
  8640. var $$ = this,
  8641. config = $$.config,
  8642. d3 = $$.d3,
  8643. min = d3.min([$$.orgXDomain[0], config.zoom_x_min]),
  8644. max = d3.max([$$.orgXDomain[1], config.zoom_x_max]);
  8645. return [min, max];
  8646. };
  8647. c3_chart_internal_fn.updateZoom = function () {
  8648. var $$ = this,
  8649. z = $$.config.zoom_enabled ? $$.zoom : function () {};
  8650. $$.main.select('.' + CLASS.zoomRect).call(z).on("dblclick.zoom", null);
  8651. $$.main.selectAll('.' + CLASS.eventRect).call(z).on("dblclick.zoom", null);
  8652. };
  8653. c3_chart_internal_fn.redrawForZoom = function () {
  8654. var $$ = this,
  8655. d3 = $$.d3,
  8656. config = $$.config,
  8657. zoom = $$.zoom,
  8658. x = $$.x;
  8659. if (!config.zoom_enabled) {
  8660. return;
  8661. }
  8662. if ($$.filterTargetsToShow($$.data.targets).length === 0) {
  8663. return;
  8664. }
  8665. if (d3.event.sourceEvent.type === 'mousemove' && zoom.altDomain) {
  8666. x.domain(zoom.altDomain);
  8667. zoom.scale(x).updateScaleExtent();
  8668. return;
  8669. }
  8670. if ($$.isCategorized() && x.orgDomain()[0] === $$.orgXDomain[0]) {
  8671. x.domain([$$.orgXDomain[0] - 1e-10, x.orgDomain()[1]]);
  8672. }
  8673. $$.redraw({
  8674. withTransition: false,
  8675. withY: config.zoom_rescale,
  8676. withSubchart: false,
  8677. withEventRect: false,
  8678. withDimension: false
  8679. });
  8680. if (d3.event.sourceEvent.type === 'mousemove') {
  8681. $$.cancelClick = true;
  8682. }
  8683. config.zoom_onzoom.call($$.api, x.orgDomain());
  8684. };
  8685. return c3;
  8686. })));