index.js 608 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995
  1. (function (factory) {
  2. typeof define === 'function' && define.amd ? define(factory) :
  3. factory();
  4. }((function () { 'use strict';
  5. var isArrayLike = function (value) {
  6. /**
  7. * isArrayLike([1, 2, 3]) => true
  8. * isArrayLike(document.body.children) => true
  9. * isArrayLike('abc') => true
  10. * isArrayLike(Function) => false
  11. */
  12. return value !== null && typeof value !== 'function' && isFinite(value.length);
  13. };
  14. var contains = function (arr, value) {
  15. if (!isArrayLike(arr)) {
  16. return false;
  17. }
  18. return arr.indexOf(value) > -1;
  19. };
  20. var filter = function (arr, func) {
  21. if (!isArrayLike(arr)) {
  22. return arr;
  23. }
  24. var result = [];
  25. for (var index = 0; index < arr.length; index++) {
  26. var value = arr[index];
  27. if (func(value, index)) {
  28. result.push(value);
  29. }
  30. }
  31. return result;
  32. };
  33. /**
  34. * Flattens `array` a single level deep.
  35. *
  36. * @param {Array} arr The array to inspect.
  37. * @param {Array} values The values to exclude.
  38. * @return {Array} Returns the new array of filtered values.
  39. * @example
  40. * difference([2, 1], [2, 3]); // => [1]
  41. */
  42. var difference = function (arr, values) {
  43. if (values === void 0) { values = []; }
  44. return filter(arr, function (value) { return !contains(values, value); });
  45. };
  46. var toString = {}.toString;
  47. var isType = function (value, type) { return toString.call(value) === '[object ' + type + ']'; };
  48. /**
  49. * 是否为函数
  50. * @param {*} fn 对象
  51. * @return {Boolean} 是否函数
  52. */
  53. var isFunction = (function (value) {
  54. return isType(value, 'Function');
  55. });
  56. // isFinite,
  57. var isNil = function (value) {
  58. /**
  59. * isNil(null) => true
  60. * isNil() => true
  61. */
  62. return value === null || value === undefined;
  63. };
  64. var isArray = (function (value) {
  65. return Array.isArray ?
  66. Array.isArray(value) :
  67. isType(value, 'Array');
  68. });
  69. var isObject = (function (value) {
  70. /**
  71. * isObject({}) => true
  72. * isObject([1, 2, 3]) => true
  73. * isObject(Function) => true
  74. * isObject(null) => false
  75. */
  76. var type = typeof value;
  77. return value !== null && type === 'object' || type === 'function';
  78. });
  79. function each(elements, func) {
  80. if (!elements) {
  81. return;
  82. }
  83. var rst;
  84. if (isArray(elements)) {
  85. for (var i = 0, len = elements.length; i < len; i++) {
  86. rst = func(elements[i], i);
  87. if (rst === false) {
  88. break;
  89. }
  90. }
  91. }
  92. else if (isObject(elements)) {
  93. for (var k in elements) {
  94. if (elements.hasOwnProperty(k)) {
  95. rst = func(elements[k], k);
  96. if (rst === false) {
  97. break;
  98. }
  99. }
  100. }
  101. }
  102. }
  103. var keys = Object.keys ? function (obj) { return Object.keys(obj); } : function (obj) {
  104. var result = [];
  105. each(obj, function (value, key) {
  106. if (!(isFunction(obj) && key === 'prototype')) {
  107. result.push(key);
  108. }
  109. });
  110. return result;
  111. };
  112. function isMatch(obj, attrs) {
  113. var _keys = keys(attrs);
  114. var length = _keys.length;
  115. if (isNil(obj))
  116. return !length;
  117. for (var i = 0; i < length; i += 1) {
  118. var key = _keys[i];
  119. if (attrs[key] !== obj[key] || !(key in obj)) {
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. var isObjectLike = function (value) {
  126. /**
  127. * isObjectLike({}) => true
  128. * isObjectLike([1, 2, 3]) => true
  129. * isObjectLike(Function) => false
  130. * isObjectLike(null) => false
  131. */
  132. return typeof value === 'object' && value !== null;
  133. };
  134. var isPlainObject = function (value) {
  135. /**
  136. * isObjectLike(new Foo) => false
  137. * isObjectLike([1, 2, 3]) => false
  138. * isObjectLike({ x: 0, y: 0 }) => true
  139. * isObjectLike(Object.create(null)) => true
  140. */
  141. if (!isObjectLike(value) || !isType(value, 'Object')) {
  142. return false;
  143. }
  144. if (Object.getPrototypeOf(value) === null) {
  145. return true;
  146. }
  147. var proto = value;
  148. while (Object.getPrototypeOf(proto) !== null) {
  149. proto = Object.getPrototypeOf(proto);
  150. }
  151. return Object.getPrototypeOf(value) === proto;
  152. };
  153. function find(arr, predicate) {
  154. if (!isArray(arr))
  155. return null;
  156. var _predicate;
  157. if (isFunction(predicate)) {
  158. _predicate = predicate;
  159. }
  160. if (isPlainObject(predicate)) {
  161. _predicate = function (a) { return isMatch(a, predicate); };
  162. }
  163. if (_predicate) {
  164. for (var i = 0; i < arr.length; i += 1) {
  165. if (_predicate(arr[i])) {
  166. return arr[i];
  167. }
  168. }
  169. }
  170. return null;
  171. }
  172. function findIndex(arr, predicate, fromIndex) {
  173. if (fromIndex === void 0) { fromIndex = 0; }
  174. for (var i = fromIndex; i < arr.length; i++) {
  175. if (predicate(arr[i], i)) {
  176. // 找到终止循环
  177. return i;
  178. }
  179. }
  180. return -1;
  181. }
  182. var firstValue = function (data, name) {
  183. var rst = null;
  184. for (var i = 0; i < data.length; i++) {
  185. var obj = data[i];
  186. var value = obj[name];
  187. if (!isNil(value)) {
  188. if (isArray(value)) {
  189. rst = value[0]; // todo 这里是否应该使用递归,调用 firstValue @绝云
  190. }
  191. else {
  192. rst = value;
  193. }
  194. break;
  195. }
  196. }
  197. return rst;
  198. };
  199. /**
  200. * Flattens `array` a single level deep.
  201. *
  202. * @param {Array} arr The array to flatten.
  203. * @return {Array} Returns the new flattened array.
  204. * @example
  205. *
  206. * flatten([1, [2, [3, [4]], 5]]); // => [1, 2, [3, [4]], 5]
  207. */
  208. var flatten = function (arr) {
  209. if (!isArray(arr)) {
  210. return [];
  211. }
  212. var rst = [];
  213. for (var i = 0; i < arr.length; i++) {
  214. rst = rst.concat(arr[i]);
  215. }
  216. return rst;
  217. };
  218. /**
  219. * Flattens `array` a single level deep.
  220. *
  221. * @param {Array} arr The array to flatten.
  222. * @param {Array} result The array to return.
  223. * @return {Array} Returns the new flattened array.
  224. * @example
  225. *
  226. * flattenDeep([1, [2, [3, [4]], 5]]); // => [1, 2, 3, 4, 5]
  227. */
  228. var flattenDeep = function (arr, result) {
  229. if (result === void 0) { result = []; }
  230. if (!isArray(arr)) {
  231. result.push(arr);
  232. }
  233. else {
  234. for (var i = 0; i < arr.length; i += 1) {
  235. flattenDeep(arr[i], result);
  236. }
  237. }
  238. return result;
  239. };
  240. /**
  241. * @param {Array} arr The array to iterate over.
  242. * @return {*} Returns the maximum value.
  243. * @example
  244. *
  245. * max([1, 2]);
  246. * // => 2
  247. *
  248. * max([]);
  249. * // => undefined
  250. *
  251. * const data = new Array(1250010).fill(1).map((d,idx) => idx);
  252. *
  253. * max(data);
  254. * // => 1250010
  255. * // Math.max(...data) will encounter "Maximum call stack size exceeded" error
  256. */
  257. var getMax = (function (arr) {
  258. if (!isArray(arr)) {
  259. return undefined;
  260. }
  261. return arr.reduce(function (prev, curr) {
  262. return Math.max(prev, curr);
  263. }, arr[0]);
  264. });
  265. /**
  266. * @param {Array} arr The array to iterate over.
  267. * @return {*} Returns the minimum value.
  268. * @example
  269. *
  270. * min([1, 2]);
  271. * // => 1
  272. *
  273. * min([]);
  274. * // => undefined
  275. *
  276. * const data = new Array(1250010).fill(1).map((d,idx) => idx);
  277. *
  278. * min(data);
  279. * // => 1250010
  280. * // Math.min(...data) will encounter "Maximum call stack size exceeded" error
  281. */
  282. var getMin = (function (arr) {
  283. if (!isArray(arr)) {
  284. return undefined;
  285. }
  286. return arr.reduce(function (prev, curr) {
  287. return Math.min(prev, curr);
  288. }, arr[0]);
  289. });
  290. var getRange = function (values) {
  291. // 存在 NaN 时,min,max 判定会出问题
  292. var filterValues = values.filter(function (v) { return !isNaN(v); });
  293. if (!filterValues.length) {
  294. // 如果没有数值则直接返回0
  295. return {
  296. min: 0,
  297. max: 0,
  298. };
  299. }
  300. if (isArray(values[0])) {
  301. var tmp = [];
  302. for (var i = 0; i < values.length; i++) {
  303. tmp = tmp.concat(values[i]);
  304. }
  305. filterValues = tmp;
  306. }
  307. var max = getMax(filterValues);
  308. var min = getMin(filterValues);
  309. return {
  310. min: min,
  311. max: max,
  312. };
  313. };
  314. var arrPrototype = Array.prototype;
  315. var splice = arrPrototype.splice;
  316. var indexOf = arrPrototype.indexOf;
  317. var pull = function (arr) {
  318. var values = [];
  319. for (var _i = 1; _i < arguments.length; _i++) {
  320. values[_i - 1] = arguments[_i];
  321. }
  322. for (var i = 0; i < values.length; i++) {
  323. var value = values[i];
  324. var fromIndex = -1;
  325. while ((fromIndex = indexOf.call(arr, value)) > -1) {
  326. splice.call(arr, fromIndex, 1);
  327. }
  328. }
  329. return arr;
  330. };
  331. var splice$1 = Array.prototype.splice;
  332. var pullAt = function pullAt(arr, indexes) {
  333. if (!isArrayLike(arr)) {
  334. return [];
  335. }
  336. var length = arr ? indexes.length : 0;
  337. var last = length - 1;
  338. while (length--) {
  339. var previous = void 0;
  340. var index = indexes[length];
  341. if (length === last || index !== previous) {
  342. previous = index;
  343. splice$1.call(arr, index, 1);
  344. }
  345. }
  346. return arr;
  347. };
  348. var reduce = function (arr, fn, init) {
  349. if (!isArray(arr) && !isPlainObject(arr)) {
  350. return arr;
  351. }
  352. var result = init;
  353. each(arr, function (data, i) {
  354. result = fn(result, data, i);
  355. });
  356. return result;
  357. };
  358. var remove = function (arr, predicate) {
  359. /**
  360. * const arr = [1, 2, 3, 4]
  361. * const evens = remove(arr, n => n % 2 == 0)
  362. * console.log(arr) // => [1, 3]
  363. * console.log(evens) // => [2, 4]
  364. */
  365. var result = [];
  366. if (!isArrayLike(arr)) {
  367. return result;
  368. }
  369. var i = -1;
  370. var indexes = [];
  371. var length = arr.length;
  372. while (++i < length) {
  373. var value = arr[i];
  374. if (predicate(value, i, arr)) {
  375. result.push(value);
  376. indexes.push(i);
  377. }
  378. }
  379. pullAt(arr, indexes);
  380. return result;
  381. };
  382. var isString = (function (str) {
  383. return isType(str, 'String');
  384. });
  385. function sortBy(arr, key) {
  386. var comparer;
  387. if (isFunction(key)) {
  388. comparer = function (a, b) { return key(a) - key(b); };
  389. }
  390. else {
  391. var keys_1 = [];
  392. if (isString(key)) {
  393. keys_1.push(key);
  394. }
  395. else if (isArray(key)) {
  396. keys_1 = key;
  397. }
  398. comparer = function (a, b) {
  399. for (var i = 0; i < keys_1.length; i += 1) {
  400. var prop = keys_1[i];
  401. if (a[prop] > b[prop]) {
  402. return 1;
  403. }
  404. if (a[prop] < b[prop]) {
  405. return -1;
  406. }
  407. }
  408. return 0;
  409. };
  410. }
  411. arr.sort(comparer);
  412. return arr;
  413. }
  414. function uniq(arr, cache) {
  415. if (cache === void 0) { cache = new Map(); }
  416. var r = [];
  417. if (Array.isArray(arr)) {
  418. for (var i = 0, len = arr.length; i < len; i++) {
  419. var item = arr[i];
  420. // 加一个 cache,提升性能
  421. if (!cache.has(item)) {
  422. r.push(item);
  423. cache.set(item, true);
  424. }
  425. }
  426. }
  427. return r;
  428. }
  429. var union = function () {
  430. var sources = [];
  431. for (var _i = 0; _i < arguments.length; _i++) {
  432. sources[_i] = arguments[_i];
  433. }
  434. return uniq([].concat.apply([], sources));
  435. };
  436. var valuesOfKey = (function (data, name) {
  437. var rst = [];
  438. var tmpMap = {};
  439. for (var i = 0; i < data.length; i++) {
  440. var obj = data[i];
  441. var value = obj[name];
  442. if (!isNil(value)) {
  443. // flatten
  444. if (!isArray(value)) {
  445. value = [value];
  446. }
  447. for (var j = 0; j < value.length; j++) {
  448. var val = value[j];
  449. // unique
  450. if (!tmpMap[val]) {
  451. rst.push(val);
  452. tmpMap[val] = true;
  453. }
  454. }
  455. }
  456. }
  457. return rst;
  458. });
  459. function head(o) {
  460. if (isArrayLike(o)) {
  461. return o[0];
  462. }
  463. return undefined;
  464. }
  465. function last(o) {
  466. if (isArrayLike(o)) {
  467. var arr = o;
  468. return arr[arr.length - 1];
  469. }
  470. return undefined;
  471. }
  472. function startsWith(arr, e) {
  473. return (isArray(arr) || isString(arr)) ? arr[0] === e : false;
  474. }
  475. function endsWith(arr, e) {
  476. return (isArray(arr) || isString(arr)) ? arr[arr.length - 1] === e : false;
  477. }
  478. /**
  479. * 只要有一个不满足条件就返回 false
  480. * @param arr
  481. * @param func
  482. */
  483. var every = function (arr, func) {
  484. for (var i = 0; i < arr.length; i++) {
  485. if (!func(arr[i], i))
  486. return false;
  487. }
  488. return true;
  489. };
  490. /**
  491. * 只要有一个满足条件就返回 true
  492. * @param arr
  493. * @param func
  494. */
  495. var some = function (arr, func) {
  496. for (var i = 0; i < arr.length; i++) {
  497. if (func(arr[i], i))
  498. return true;
  499. }
  500. return false;
  501. };
  502. var hasOwnProperty = Object.prototype.hasOwnProperty;
  503. function groupBy(data, condition) {
  504. if (!condition || !isArray(data)) {
  505. return {};
  506. }
  507. var result = {};
  508. // 兼容方法和 字符串的写法
  509. var predicate = isFunction(condition) ? condition : function (item) { return item[condition]; };
  510. var key;
  511. for (var i = 0; i < data.length; i++) {
  512. var item = data[i];
  513. key = predicate(item);
  514. if (hasOwnProperty.call(result, key)) {
  515. result[key].push(item);
  516. }
  517. else {
  518. result[key] = [item];
  519. }
  520. }
  521. return result;
  522. }
  523. /**
  524. * 将数据分组成 map
  525. * @param data
  526. * @param condition
  527. */
  528. function groupToMap(data, condition) {
  529. if (!condition) {
  530. return {
  531. 0: data,
  532. };
  533. }
  534. if (!isFunction(condition)) {
  535. // 如果是字符串,则按照 a*b 风格成数组
  536. var paramscondition_1 = isArray(condition) ? condition : condition.replace(/\s+/g, '').split('*');
  537. condition = function (row) {
  538. var unique = '_'; // 避免出现数字作为Key的情况,会进行按照数字的排序
  539. // 根据字段列表的值,拼接成 key
  540. for (var i = 0, l = paramscondition_1.length; i < l; i++) {
  541. unique += row[paramscondition_1[i]] && row[paramscondition_1[i]].toString();
  542. }
  543. return unique;
  544. };
  545. }
  546. return groupBy(data, condition);
  547. }
  548. var group = (function (data, condition) {
  549. if (!condition) {
  550. // 没有条件,则自身改成数组
  551. return [data];
  552. }
  553. var groups = groupToMap(data, condition);
  554. var array = [];
  555. for (var i in groups) {
  556. array.push(groups[i]);
  557. }
  558. return array;
  559. });
  560. /**
  561. * 获取封装的事件
  562. * @protected
  563. * @param {Object} obj 对象
  564. * @param {String} action 事件名称
  565. * @return {Function} 返回事件处理函数
  566. */
  567. function getWrapBehavior(obj, action) {
  568. return obj['_wrap_' + action];
  569. }
  570. /**
  571. * 封装事件,便于使用上下文this,和便于解除事件时使用
  572. * @protected
  573. * @param {Object} obj 对象
  574. * @param {String} action 事件名称
  575. * @return {Function} 返回事件处理函数
  576. */
  577. function wrapBehavior(obj, action) {
  578. if (obj['_wrap_' + action]) {
  579. return obj['_wrap_' + action];
  580. }
  581. var method = function (e) {
  582. obj[action](e);
  583. };
  584. obj['_wrap_' + action] = method;
  585. return method;
  586. }
  587. var numColorCache = {};
  588. function numberToColor(num) {
  589. // 增加缓存
  590. var color = numColorCache[num];
  591. if (!color) {
  592. var str = num.toString(16);
  593. for (var i = str.length; i < 6; i++) {
  594. str = '0' + str;
  595. }
  596. color = '#' + str;
  597. numColorCache[num] = color;
  598. }
  599. return color;
  600. }
  601. function parseRadius(radius) {
  602. var r1 = 0, r2 = 0, r3 = 0, r4 = 0;
  603. if (isArray(radius)) {
  604. if (radius.length === 1) {
  605. r1 = r2 = r3 = r4 = radius[0];
  606. }
  607. else if (radius.length === 2) {
  608. r1 = r3 = radius[0];
  609. r2 = r4 = radius[1];
  610. }
  611. else if (radius.length === 3) {
  612. r1 = radius[0];
  613. r2 = r4 = radius[1];
  614. r3 = radius[2];
  615. }
  616. else {
  617. r1 = radius[0];
  618. r2 = radius[1];
  619. r3 = radius[2];
  620. r4 = radius[3];
  621. }
  622. }
  623. else {
  624. r1 = r2 = r3 = r4 = radius;
  625. }
  626. return {
  627. r1: r1,
  628. r2: r2,
  629. r3: r3,
  630. r4: r4
  631. };
  632. }
  633. var clamp = function (a, min, max) {
  634. if (a < min) {
  635. return min;
  636. }
  637. else if (a > max) {
  638. return max;
  639. }
  640. return a;
  641. };
  642. var fixedBase = function (v, base) {
  643. var str = base.toString();
  644. var index = str.indexOf('.');
  645. if (index === -1) {
  646. return Math.round(v);
  647. }
  648. var length = str.substr(index + 1).length;
  649. if (length > 20) {
  650. length = 20;
  651. }
  652. return parseFloat(v.toFixed(length));
  653. };
  654. /**
  655. * 判断是否数字
  656. * @return {Boolean} 是否数字
  657. */
  658. var isNumber = function (value) {
  659. return isType(value, 'Number');
  660. };
  661. var isDecimal = function (num) {
  662. return isNumber(num) && num % 1 !== 0;
  663. };
  664. var isEven = function (num) {
  665. return isNumber(num) && num % 2 === 0;
  666. };
  667. var isInteger = Number.isInteger ? Number.isInteger : function (num) {
  668. return isNumber(num) && num % 1 === 0;
  669. };
  670. var isNegative = function (num) {
  671. return isNumber(num) && num < 0;
  672. };
  673. var PRECISION = 0.00001; // numbers less than this is considered as 0
  674. function isNumberEqual(a, b, precision) {
  675. if (precision === void 0) { precision = PRECISION; }
  676. return Math.abs((a - b)) < precision;
  677. }
  678. var isOdd = function (num) {
  679. return isNumber(num) && num % 2 !== 0;
  680. };
  681. var isPositive = function (num) {
  682. return isNumber(num) && num > 0;
  683. };
  684. /**
  685. * @param {Array} arr The array to iterate over.
  686. * @param {Function} [fn] The iteratee invoked per element.
  687. * @return {*} Returns the maximum value.
  688. * @example
  689. *
  690. * var objects = [{ 'n': 1 }, { 'n': 2 }];
  691. *
  692. * maxBy(objects, function(o) { return o.n; });
  693. * // => { 'n': 2 }
  694. *
  695. * maxBy(objects, 'n');
  696. * // => { 'n': 2 }
  697. */
  698. var maxBy = (function (arr, fn) {
  699. if (!isArray(arr)) {
  700. return undefined;
  701. }
  702. var maxItem;
  703. var max = -Infinity;
  704. for (var i = 0; i < arr.length; i++) {
  705. var item = arr[i];
  706. var v = isFunction(fn) ? fn(item) : item[fn];
  707. if (v > max) {
  708. maxItem = item;
  709. max = v;
  710. }
  711. }
  712. return maxItem;
  713. });
  714. /**
  715. * @param {Array} arr The array to iterate over.
  716. * @param {Function} [fn] The iteratee invoked per element.
  717. * @return {*} Returns the minimum value.
  718. * @example
  719. *
  720. * var objects = [{ 'n': 1 }, { 'n': 2 }];
  721. *
  722. * minBy(objects, function(o) { return o.n; });
  723. * // => { 'n': 1 }
  724. *
  725. * minBy(objects, 'n');
  726. * // => { 'n': 1 }
  727. */
  728. var minBy = (function (arr, fn) {
  729. if (!isArray(arr)) {
  730. return undefined;
  731. }
  732. var minItem;
  733. var min = Infinity;
  734. for (var i = 0; i < arr.length; i++) {
  735. var item = arr[i];
  736. var v = isFunction(fn) ? fn(item) : item[fn];
  737. if (v < min) {
  738. minItem = item;
  739. min = v;
  740. }
  741. }
  742. return minItem;
  743. });
  744. var mod = function (n, m) {
  745. return ((n % m) + m) % m;
  746. };
  747. var DEGREE = 180 / Math.PI;
  748. var toDegree = function (radian) {
  749. return DEGREE * radian;
  750. };
  751. var RADIAN = Math.PI / 180;
  752. var toRadian = function (degree) {
  753. return RADIAN * degree;
  754. };
  755. var has = (function (obj, key) { return obj.hasOwnProperty(key); });
  756. // @ts-ignore
  757. var values = Object.values ? function (obj) { return Object.values(obj); } : function (obj) {
  758. var result = [];
  759. each(obj, function (value, key) {
  760. if (!(isFunction(obj) && key === 'prototype')) {
  761. result.push(value);
  762. }
  763. });
  764. return result;
  765. };
  766. var hasValue = (function (obj, value) { return contains(values(obj), value); });
  767. var toString$1 = (function (value) {
  768. if (isNil(value))
  769. return '';
  770. return value.toString();
  771. });
  772. var lowerCase = function (str) {
  773. return toString$1(str).toLowerCase();
  774. };
  775. var lowerFirst = function (value) {
  776. var str = toString$1(value);
  777. return str.charAt(0).toLowerCase() + str.substring(1);
  778. };
  779. function substitute(str, o) {
  780. if (!str || !o) {
  781. return str;
  782. }
  783. return str.replace(/\\?\{([^{}]+)\}/g, function (match, name) {
  784. if (match.charAt(0) === '\\') {
  785. return match.slice(1);
  786. }
  787. return (o[name] === undefined) ? '' : o[name];
  788. });
  789. }
  790. var upperCase = function (str) {
  791. return toString$1(str).toUpperCase();
  792. };
  793. var upperFirst = function (value) {
  794. var str = toString$1(value);
  795. return str.charAt(0).toUpperCase() + str.substring(1);
  796. };
  797. var toString$2 = {}.toString;
  798. var getType = function (value) {
  799. return toString$2.call(value).replace(/^\[object /, '').replace(/]$/, '');
  800. };
  801. /**
  802. * 是否是参数类型
  803. *
  804. * @param {Object} value 测试的值
  805. * @return {Boolean}
  806. */
  807. var isArguments = function (value) {
  808. return isType(value, 'Arguments');
  809. };
  810. /**
  811. * 是否是布尔类型
  812. *
  813. * @param {Object} value 测试的值
  814. * @return {Boolean}
  815. */
  816. var isBoolean = function (value) {
  817. return isType(value, 'Boolean');
  818. };
  819. var isDate = function (value) {
  820. return isType(value, 'Date');
  821. };
  822. /**
  823. * 是否是参数类型
  824. *
  825. * @param {Object} value 测试的值
  826. * @return {Boolean}
  827. */
  828. var isError = function (value) {
  829. return isType(value, 'Error');
  830. };
  831. /**
  832. * 判断是否为有限数
  833. * @return {Boolean}
  834. */
  835. function _isFinite (value) {
  836. return isNumber(value) && isFinite(value);
  837. }
  838. var isNull = function (value) {
  839. return value === null;
  840. };
  841. var objectProto = Object.prototype;
  842. var isPrototype = function (value) {
  843. var Ctor = value && value.constructor;
  844. var proto = (typeof Ctor === 'function' && Ctor.prototype) || objectProto;
  845. return value === proto;
  846. };
  847. var isRegExp = function (str) {
  848. return isType(str, 'RegExp');
  849. };
  850. var isUndefined = function (value) {
  851. return value === undefined;
  852. };
  853. /**
  854. * 判断是否HTML元素
  855. * @return {Boolean} 是否HTML元素
  856. */
  857. var isElement = function (o) {
  858. return o instanceof Element || o instanceof HTMLDocument;
  859. };
  860. function requestAnimationFrame(fn) {
  861. var method = window.requestAnimationFrame ||
  862. window.webkitRequestAnimationFrame ||
  863. // @ts-ignore
  864. window.mozRequestAnimationFrame ||
  865. // @ts-ignore
  866. window.msRequestAnimationFrame ||
  867. function (f) {
  868. return setTimeout(f, 16);
  869. };
  870. return method(fn);
  871. }
  872. function cancelAnimationFrame(handler) {
  873. var method = window.cancelAnimationFrame ||
  874. window.webkitCancelAnimationFrame ||
  875. // @ts-ignore
  876. window.mozCancelAnimationFrame ||
  877. // @ts-ignore
  878. window.msCancelAnimationFrame ||
  879. clearTimeout;
  880. method(handler);
  881. }
  882. // FIXME: Mutable param should be forbidden in static lang.
  883. function _mix(dist, obj) {
  884. for (var key in obj) {
  885. if (obj.hasOwnProperty(key) && key !== 'constructor' && obj[key] !== undefined) {
  886. dist[key] = obj[key];
  887. }
  888. }
  889. }
  890. function mix(dist, src1, src2, src3) {
  891. if (src1)
  892. _mix(dist, src1);
  893. if (src2)
  894. _mix(dist, src2);
  895. if (src3)
  896. _mix(dist, src3);
  897. return dist;
  898. }
  899. var augment = function () {
  900. var args = [];
  901. for (var _i = 0; _i < arguments.length; _i++) {
  902. args[_i] = arguments[_i];
  903. }
  904. var c = args[0];
  905. for (var i = 1; i < args.length; i++) {
  906. var obj = args[i];
  907. if (isFunction(obj)) {
  908. obj = obj.prototype;
  909. }
  910. mix(c.prototype, obj);
  911. }
  912. };
  913. var clone = function (obj) {
  914. if (typeof obj !== 'object' || obj === null) {
  915. return obj;
  916. }
  917. var rst;
  918. if (isArray(obj)) {
  919. rst = [];
  920. for (var i = 0, l = obj.length; i < l; i++) {
  921. if (typeof obj[i] === 'object' && obj[i] != null) {
  922. rst[i] = clone(obj[i]);
  923. }
  924. else {
  925. rst[i] = obj[i];
  926. }
  927. }
  928. }
  929. else {
  930. rst = {};
  931. for (var k in obj) {
  932. if (typeof obj[k] === 'object' && obj[k] != null) {
  933. rst[k] = clone(obj[k]);
  934. }
  935. else {
  936. rst[k] = obj[k];
  937. }
  938. }
  939. }
  940. return rst;
  941. };
  942. function debounce(func, wait, immediate) {
  943. var timeout;
  944. return function () {
  945. var context = this, args = arguments;
  946. var later = function () {
  947. timeout = null;
  948. if (!immediate) {
  949. func.apply(context, args);
  950. }
  951. };
  952. var callNow = immediate && !timeout;
  953. clearTimeout(timeout);
  954. timeout = setTimeout(later, wait);
  955. if (callNow) {
  956. func.apply(context, args);
  957. }
  958. };
  959. }
  960. /**
  961. * _.memoize(calColor);
  962. * _.memoize(calColor, (...args) => args[0]);
  963. * @param f
  964. * @param resolver
  965. */
  966. var memoize = (function (f, resolver) {
  967. if (!isFunction(f)) {
  968. throw new TypeError('Expected a function');
  969. }
  970. var memoized = function () {
  971. var args = [];
  972. for (var _i = 0; _i < arguments.length; _i++) {
  973. args[_i] = arguments[_i];
  974. }
  975. // 使用方法构造 key,如果不存在 resolver,则直接取第一个参数作为 key
  976. var key = resolver ? resolver.apply(this, args) : args[0];
  977. var cache = memoized.cache;
  978. if (cache.has(key)) {
  979. return cache.get(key);
  980. }
  981. var result = f.apply(this, args);
  982. // 缓存起来
  983. cache.set(key, result);
  984. return result;
  985. };
  986. memoized.cache = new Map();
  987. return memoized;
  988. });
  989. var MAX_MIX_LEVEL = 5;
  990. function _deepMix(dist, src, level, maxLevel) {
  991. level = level || 0;
  992. maxLevel = maxLevel || MAX_MIX_LEVEL;
  993. for (var key in src) {
  994. if (src.hasOwnProperty(key)) {
  995. var value = src[key];
  996. if (value !== null && isPlainObject(value)) {
  997. if (!isPlainObject(dist[key])) {
  998. dist[key] = {};
  999. }
  1000. if (level < maxLevel) {
  1001. _deepMix(dist[key], value, level + 1, maxLevel);
  1002. }
  1003. else {
  1004. dist[key] = src[key];
  1005. }
  1006. }
  1007. else if (isArray(value)) {
  1008. dist[key] = [];
  1009. dist[key] = dist[key].concat(value);
  1010. }
  1011. else if (value !== undefined) {
  1012. dist[key] = value;
  1013. }
  1014. }
  1015. }
  1016. }
  1017. // todo 重写
  1018. var deepMix = function (rst) {
  1019. var args = [];
  1020. for (var _i = 1; _i < arguments.length; _i++) {
  1021. args[_i - 1] = arguments[_i];
  1022. }
  1023. for (var i = 0; i < args.length; i += 1) {
  1024. _deepMix(rst, args[i]);
  1025. }
  1026. return rst;
  1027. };
  1028. var extend = function (subclass, superclass, overrides, staticOverrides) {
  1029. // 如果只提供父类构造函数,则自动生成子类构造函数
  1030. if (!isFunction(superclass)) {
  1031. overrides = superclass;
  1032. superclass = subclass;
  1033. subclass = function () { };
  1034. }
  1035. var create = Object.create ?
  1036. function (proto, c) {
  1037. return Object.create(proto, {
  1038. constructor: {
  1039. value: c
  1040. }
  1041. });
  1042. } :
  1043. function (proto, c) {
  1044. function Tmp() { }
  1045. Tmp.prototype = proto;
  1046. var o = new Tmp();
  1047. o.constructor = c;
  1048. return o;
  1049. };
  1050. var superObj = create(superclass.prototype, subclass); // new superclass(),//实例化父类作为子类的prototype
  1051. subclass.prototype = mix(superObj, subclass.prototype); // 指定子类的prototype
  1052. subclass.superclass = create(superclass.prototype, superclass);
  1053. mix(superObj, overrides);
  1054. mix(subclass, staticOverrides);
  1055. return subclass;
  1056. };
  1057. var indexOf$1 = function (arr, obj) {
  1058. if (!isArrayLike(arr)) {
  1059. return -1;
  1060. }
  1061. var m = Array.prototype.indexOf;
  1062. if (m) {
  1063. return m.call(arr, obj);
  1064. }
  1065. var index = -1;
  1066. for (var i = 0; i < arr.length; i++) {
  1067. if (arr[i] === obj) {
  1068. index = i;
  1069. break;
  1070. }
  1071. }
  1072. return index;
  1073. };
  1074. var hasOwnProperty$1 = Object.prototype.hasOwnProperty;
  1075. function isEmpty(value) {
  1076. /**
  1077. * isEmpty(null) => true
  1078. * isEmpty() => true
  1079. * isEmpty(true) => true
  1080. * isEmpty(1) => true
  1081. * isEmpty([1, 2, 3]) => false
  1082. * isEmpty('abc') => false
  1083. * isEmpty({ a: 1 }) => false
  1084. */
  1085. if (isNil(value)) {
  1086. return true;
  1087. }
  1088. if (isArrayLike(value)) {
  1089. return !value.length;
  1090. }
  1091. var type = getType(value);
  1092. if (type === 'Map' || type === 'Set') {
  1093. return !value.size;
  1094. }
  1095. if (isPrototype(value)) {
  1096. return !Object.keys(value).length;
  1097. }
  1098. for (var key in value) {
  1099. if (hasOwnProperty$1.call(value, key)) {
  1100. return false;
  1101. }
  1102. }
  1103. return true;
  1104. }
  1105. var isEqual = function (value, other) {
  1106. if (value === other) {
  1107. return true;
  1108. }
  1109. if (!value || !other) {
  1110. return false;
  1111. }
  1112. if (isString(value) || isString(other)) {
  1113. return false;
  1114. }
  1115. if (isArrayLike(value) || isArrayLike(other)) {
  1116. if (value.length !== other.length) {
  1117. return false;
  1118. }
  1119. var rst = true;
  1120. for (var i = 0; i < value.length; i++) {
  1121. rst = isEqual(value[i], other[i]);
  1122. if (!rst) {
  1123. break;
  1124. }
  1125. }
  1126. return rst;
  1127. }
  1128. if (isObjectLike(value) || isObjectLike(other)) {
  1129. var valueKeys = Object.keys(value);
  1130. var otherKeys = Object.keys(other);
  1131. if (valueKeys.length !== otherKeys.length) {
  1132. return false;
  1133. }
  1134. var rst = true;
  1135. for (var i = 0; i < valueKeys.length; i++) {
  1136. rst = isEqual(value[valueKeys[i]], other[valueKeys[i]]);
  1137. if (!rst) {
  1138. break;
  1139. }
  1140. }
  1141. return rst;
  1142. }
  1143. return false;
  1144. };
  1145. /**
  1146. * @param {*} value The value to compare.
  1147. * @param {*} other The other value to compare.
  1148. * @param {Function} [fn] The function to customize comparisons.
  1149. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  1150. * @example
  1151. *
  1152. * function isGreeting(value) {
  1153. * return /^h(?:i|ello)$/.test(value);
  1154. * }
  1155. *
  1156. * function customizer(objValue, othValue) {
  1157. * if (isGreeting(objValue) && isGreeting(othValue)) {
  1158. * return true;
  1159. * }
  1160. * }
  1161. *
  1162. * var array = ['hello', 'goodbye'];
  1163. * var other = ['hi', 'goodbye'];
  1164. *
  1165. * isEqualWith(array, other, customizer); // => true
  1166. */
  1167. var isEqualWith = (function (value, other, fn) {
  1168. if (!isFunction(fn)) {
  1169. return isEqual(value, other);
  1170. }
  1171. return !!fn(value, other);
  1172. });
  1173. var map = function (arr, func) {
  1174. if (!isArrayLike(arr)) {
  1175. // @ts-ignore
  1176. return arr;
  1177. }
  1178. var result = [];
  1179. for (var index = 0; index < arr.length; index++) {
  1180. var value = arr[index];
  1181. result.push(func(value, index));
  1182. }
  1183. return result;
  1184. };
  1185. var identity = function (v) { return v; };
  1186. var mapValues = (function (object, func) {
  1187. if (func === void 0) { func = identity; }
  1188. var r = {};
  1189. if (isObject(object) && !isNil(object)) {
  1190. Object.keys(object).forEach(function (key) {
  1191. // @ts-ignore
  1192. r[key] = func(object[key], key);
  1193. });
  1194. }
  1195. return r;
  1196. });
  1197. /**
  1198. * https://github.com/developit/dlv/blob/master/index.js
  1199. * @param obj
  1200. * @param key
  1201. * @param defaultValue
  1202. */
  1203. var get = (function (obj, key, defaultValue) {
  1204. var p = 0;
  1205. var keyArr = isString(key) ? key.split('.') : key;
  1206. while (obj && p < keyArr.length) {
  1207. obj = obj[keyArr[p++]];
  1208. }
  1209. return (obj === undefined || p < keyArr.length) ? defaultValue : obj;
  1210. });
  1211. /**
  1212. * https://github.com/developit/dlv/blob/master/index.js
  1213. * @param obj
  1214. * @param path
  1215. * @param value
  1216. */
  1217. var set = (function (obj, path, value) {
  1218. var o = obj;
  1219. var keyArr = isString(path) ? path.split('.') : path;
  1220. keyArr.forEach(function (key, idx) {
  1221. // 不是最后一个
  1222. if (idx < keyArr.length - 1) {
  1223. if (!isObject(o[key])) {
  1224. o[key] = isNumber(keyArr[idx + 1]) ? [] : {};
  1225. }
  1226. o = o[key];
  1227. }
  1228. else {
  1229. o[key] = value;
  1230. }
  1231. });
  1232. return obj;
  1233. });
  1234. var hasOwnProperty$2 = Object.prototype.hasOwnProperty;
  1235. var pick = (function (object, keys) {
  1236. if (object === null || !isPlainObject(object)) {
  1237. return {};
  1238. }
  1239. var result = {};
  1240. each(keys, function (key) {
  1241. if (hasOwnProperty$2.call(object, key)) {
  1242. result[key] = object[key];
  1243. }
  1244. });
  1245. return result;
  1246. });
  1247. var omit = (function (obj, keys) {
  1248. return reduce(obj, function (r, curr, key) {
  1249. if (!keys.includes(key)) {
  1250. r[key] = curr;
  1251. }
  1252. return r;
  1253. }, {});
  1254. });
  1255. var throttle = (function (func, wait, options) {
  1256. var timeout, context, args, result;
  1257. var previous = 0;
  1258. if (!options)
  1259. options = {};
  1260. var later = function () {
  1261. previous = options.leading === false ? 0 : Date.now();
  1262. timeout = null;
  1263. result = func.apply(context, args);
  1264. if (!timeout)
  1265. context = args = null;
  1266. };
  1267. var throttled = function () {
  1268. var now = Date.now();
  1269. if (!previous && options.leading === false)
  1270. previous = now;
  1271. var remaining = wait - (now - previous);
  1272. context = this;
  1273. args = arguments;
  1274. if (remaining <= 0 || remaining > wait) {
  1275. if (timeout) {
  1276. clearTimeout(timeout);
  1277. timeout = null;
  1278. }
  1279. previous = now;
  1280. result = func.apply(context, args);
  1281. if (!timeout)
  1282. context = args = null;
  1283. }
  1284. else if (!timeout && options.trailing !== false) {
  1285. timeout = setTimeout(later, remaining);
  1286. }
  1287. return result;
  1288. };
  1289. throttled.cancel = function () {
  1290. clearTimeout(timeout);
  1291. previous = 0;
  1292. timeout = context = args = null;
  1293. };
  1294. return throttled;
  1295. });
  1296. var toArray = (function (value) {
  1297. return isArrayLike(value) ? Array.prototype.slice.call(value) : [];
  1298. });
  1299. var map$1 = {};
  1300. var uniqueId = (function (prefix) {
  1301. prefix = prefix || 'g';
  1302. if (!map$1[prefix]) {
  1303. map$1[prefix] = 1;
  1304. }
  1305. else {
  1306. map$1[prefix] += 1;
  1307. }
  1308. return prefix + map$1[prefix];
  1309. });
  1310. var noop = (function () { });
  1311. var identity$1 = (function (v) { return v; });
  1312. function size(o) {
  1313. if (isNil(o)) {
  1314. return 0;
  1315. }
  1316. if (isArrayLike(o)) {
  1317. return o.length;
  1318. }
  1319. return Object.keys(o).length;
  1320. }
  1321. /*! *****************************************************************************
  1322. Copyright (c) Microsoft Corporation.
  1323. Permission to use, copy, modify, and/or distribute this software for any
  1324. purpose with or without fee is hereby granted.
  1325. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  1326. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  1327. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  1328. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  1329. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  1330. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  1331. PERFORMANCE OF THIS SOFTWARE.
  1332. ***************************************************************************** */
  1333. /* global Reflect, Promise */
  1334. var extendStatics = function(d, b) {
  1335. extendStatics = Object.setPrototypeOf ||
  1336. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  1337. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  1338. return extendStatics(d, b);
  1339. };
  1340. function __extends(d, b) {
  1341. if (typeof b !== "function" && b !== null)
  1342. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  1343. extendStatics(d, b);
  1344. function __() { this.constructor = d; }
  1345. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  1346. }
  1347. /** @deprecated */
  1348. function __spreadArrays() {
  1349. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  1350. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  1351. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  1352. r[k] = a[j];
  1353. return r;
  1354. }
  1355. function __spreadArray(to, from, pack) {
  1356. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  1357. if (ar || !(i in from)) {
  1358. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  1359. ar[i] = from[i];
  1360. }
  1361. }
  1362. return to.concat(ar || Array.prototype.slice.call(from));
  1363. }
  1364. var ctx;
  1365. /**
  1366. * 计算文本的宽度
  1367. */
  1368. var measureTextWidth = memoize(function (text, font) {
  1369. if (font === void 0) { font = {}; }
  1370. var fontSize = font.fontSize, fontFamily = font.fontFamily, fontWeight = font.fontWeight, fontStyle = font.fontStyle, fontVariant = font.fontVariant;
  1371. if (!ctx) {
  1372. ctx = document.createElement('canvas').getContext('2d');
  1373. }
  1374. ctx.font = [fontStyle, fontVariant, fontWeight, fontSize + "px", fontFamily].join(' ');
  1375. return ctx.measureText(isString(text) ? text : '').width;
  1376. }, function (text, font) {
  1377. if (font === void 0) { font = {}; }
  1378. return __spreadArrays([text], values(font)).join('');
  1379. });
  1380. /**
  1381. * 获取文本的 ... 文本。
  1382. * 算法(减少每次 measureText 的长度,measureText 的性能跟字符串时间相关):
  1383. * 1. 先通过 STEP 逐步计算,找到最后一个小于 maxWidth 的字符串
  1384. * 2. 然后对最后这个字符串二分计算
  1385. * @param text 需要计算的文本, 由于历史原因 除了支持string,还支持空值,number和数组等
  1386. * @param maxWidth 最大宽度
  1387. * @param font 字体
  1388. * @param str 要替换的文本
  1389. */
  1390. var getEllipsisText = (function (text, maxWidth, font, str) {
  1391. if (str === void 0) { str = '...'; }
  1392. var STEP = 16; // 每次 16,调参工程师
  1393. var PLACEHOLDER_WIDTH = measureTextWidth(str, font);
  1394. var leftText = !isString(text) ? toString$1(text) : text;
  1395. var leftWidth = maxWidth;
  1396. var r = []; // 最终的分段字符串
  1397. var currentText;
  1398. var currentWidth;
  1399. if (measureTextWidth(text, font) <= maxWidth) {
  1400. return text;
  1401. }
  1402. // 首先通过 step 计算,找出最大的未超出长度的
  1403. // eslint-disable-next-line no-constant-condition
  1404. while (true) {
  1405. // 更新字符串
  1406. currentText = leftText.substr(0, STEP);
  1407. // 计算宽度
  1408. currentWidth = measureTextWidth(currentText, font);
  1409. // 超出剩余宽度,则停止
  1410. if (currentWidth + PLACEHOLDER_WIDTH > leftWidth) {
  1411. if (currentWidth > leftWidth) {
  1412. break;
  1413. }
  1414. }
  1415. r.push(currentText);
  1416. // 没有超出,则计算剩余宽度
  1417. leftWidth -= currentWidth;
  1418. leftText = leftText.substr(STEP);
  1419. // 字符串整体没有超出
  1420. if (!leftText) {
  1421. return r.join('');
  1422. }
  1423. }
  1424. // 最下的最后一个 STEP,使用 1 递增(用二分效果更高)
  1425. // eslint-disable-next-line no-constant-condition
  1426. while (true) {
  1427. // 更新字符串
  1428. currentText = leftText.substr(0, 1);
  1429. // 计算宽度
  1430. currentWidth = measureTextWidth(currentText, font);
  1431. // 超出剩余宽度,则停止
  1432. if (currentWidth + PLACEHOLDER_WIDTH > leftWidth) {
  1433. break;
  1434. }
  1435. r.push(currentText);
  1436. // 没有超出,则计算剩余宽度
  1437. leftWidth -= currentWidth;
  1438. leftText = leftText.substr(1);
  1439. if (!leftText) {
  1440. return r.join('');
  1441. }
  1442. }
  1443. return "" + r.join('') + str;
  1444. });
  1445. /**
  1446. * k-v 存储
  1447. */
  1448. var default_1 = /** @class */ (function () {
  1449. function default_1() {
  1450. this.map = {};
  1451. }
  1452. default_1.prototype.has = function (key) {
  1453. return this.map[key] !== undefined;
  1454. };
  1455. default_1.prototype.get = function (key, def) {
  1456. var v = this.map[key];
  1457. return v === undefined ? def : v;
  1458. };
  1459. default_1.prototype.set = function (key, value) {
  1460. this.map[key] = value;
  1461. };
  1462. default_1.prototype.clear = function () {
  1463. this.map = {};
  1464. };
  1465. default_1.prototype.delete = function (key) {
  1466. delete this.map[key];
  1467. };
  1468. default_1.prototype.size = function () {
  1469. return Object.keys(this.map).length;
  1470. };
  1471. return default_1;
  1472. }());
  1473. // array
  1474. var esm = /*#__PURE__*/Object.freeze({
  1475. __proto__: null,
  1476. contains: contains,
  1477. includes: contains,
  1478. difference: difference,
  1479. find: find,
  1480. findIndex: findIndex,
  1481. firstValue: firstValue,
  1482. flatten: flatten,
  1483. flattenDeep: flattenDeep,
  1484. getRange: getRange,
  1485. pull: pull,
  1486. pullAt: pullAt,
  1487. reduce: reduce,
  1488. remove: remove,
  1489. sortBy: sortBy,
  1490. union: union,
  1491. uniq: uniq,
  1492. valuesOfKey: valuesOfKey,
  1493. head: head,
  1494. last: last,
  1495. startsWith: startsWith,
  1496. endsWith: endsWith,
  1497. filter: filter,
  1498. every: every,
  1499. some: some,
  1500. group: group,
  1501. groupBy: groupBy,
  1502. groupToMap: groupToMap,
  1503. getWrapBehavior: getWrapBehavior,
  1504. wrapBehavior: wrapBehavior,
  1505. number2color: numberToColor,
  1506. parseRadius: parseRadius,
  1507. clamp: clamp,
  1508. fixedBase: fixedBase,
  1509. isDecimal: isDecimal,
  1510. isEven: isEven,
  1511. isInteger: isInteger,
  1512. isNegative: isNegative,
  1513. isNumberEqual: isNumberEqual,
  1514. isOdd: isOdd,
  1515. isPositive: isPositive,
  1516. max: getMax,
  1517. maxBy: maxBy,
  1518. min: getMin,
  1519. minBy: minBy,
  1520. mod: mod,
  1521. toDegree: toDegree,
  1522. toInteger: parseInt,
  1523. toRadian: toRadian,
  1524. forIn: each,
  1525. has: has,
  1526. hasKey: has,
  1527. hasValue: hasValue,
  1528. keys: keys,
  1529. isMatch: isMatch,
  1530. values: values,
  1531. lowerCase: lowerCase,
  1532. lowerFirst: lowerFirst,
  1533. substitute: substitute,
  1534. upperCase: upperCase,
  1535. upperFirst: upperFirst,
  1536. getType: getType,
  1537. isArguments: isArguments,
  1538. isArray: isArray,
  1539. isArrayLike: isArrayLike,
  1540. isBoolean: isBoolean,
  1541. isDate: isDate,
  1542. isError: isError,
  1543. isFunction: isFunction,
  1544. isFinite: _isFinite,
  1545. isNil: isNil,
  1546. isNull: isNull,
  1547. isNumber: isNumber,
  1548. isObject: isObject,
  1549. isObjectLike: isObjectLike,
  1550. isPlainObject: isPlainObject,
  1551. isPrototype: isPrototype,
  1552. isRegExp: isRegExp,
  1553. isString: isString,
  1554. isType: isType,
  1555. isUndefined: isUndefined,
  1556. isElement: isElement,
  1557. requestAnimationFrame: requestAnimationFrame,
  1558. clearAnimationFrame: cancelAnimationFrame,
  1559. augment: augment,
  1560. clone: clone,
  1561. debounce: debounce,
  1562. memoize: memoize,
  1563. deepMix: deepMix,
  1564. each: each,
  1565. extend: extend,
  1566. indexOf: indexOf$1,
  1567. isEmpty: isEmpty,
  1568. isEqual: isEqual,
  1569. isEqualWith: isEqualWith,
  1570. map: map,
  1571. mapValues: mapValues,
  1572. mix: mix,
  1573. assign: mix,
  1574. get: get,
  1575. set: set,
  1576. pick: pick,
  1577. omit: omit,
  1578. throttle: throttle,
  1579. toArray: toArray,
  1580. toString: toString$1,
  1581. uniqueId: uniqueId,
  1582. noop: noop,
  1583. identity: identity$1,
  1584. size: size,
  1585. measureTextWidth: measureTextWidth,
  1586. getEllipsisText: getEllipsisText,
  1587. Cache: default_1
  1588. });
  1589. function merge(dataArray) {
  1590. var rst = [];
  1591. for (var i = 0, len = dataArray.length; i < len; i++) {
  1592. rst = rst.concat(dataArray[i]);
  1593. }
  1594. return rst;
  1595. }
  1596. function values$1(data, name) {
  1597. var rst = [];
  1598. var tmpMap = {};
  1599. for (var i = 0, len = data.length; i < len; i++) {
  1600. var obj = data[i];
  1601. var value = obj[name];
  1602. if (!isNil(value)) {
  1603. if (!isArray(value)) {
  1604. if (!tmpMap[value]) {
  1605. rst.push(value);
  1606. tmpMap[value] = true;
  1607. }
  1608. } else {
  1609. each(value, function (val) {
  1610. if (!tmpMap[val]) {
  1611. rst.push(val);
  1612. tmpMap[val] = true;
  1613. }
  1614. });
  1615. }
  1616. }
  1617. }
  1618. return rst;
  1619. }
  1620. function firstValue$1(data, name) {
  1621. var rst = null;
  1622. for (var i = 0, len = data.length; i < len; i++) {
  1623. var obj = data[i];
  1624. var value = obj[name];
  1625. if (!isNil(value)) {
  1626. if (isArray(value)) {
  1627. rst = value[0];
  1628. } else {
  1629. rst = value;
  1630. }
  1631. break;
  1632. }
  1633. }
  1634. return rst;
  1635. }
  1636. function groupToMap$1(data, fields) {
  1637. if (!fields) {
  1638. return {
  1639. 0: data
  1640. };
  1641. }
  1642. var callback = function callback(row) {
  1643. var unique = '_';
  1644. for (var i = 0, l = fields.length; i < l; i++) {
  1645. unique += row[fields[i]] && row[fields[i]].toString();
  1646. }
  1647. return unique;
  1648. };
  1649. var groups = {};
  1650. for (var i = 0, len = data.length; i < len; i++) {
  1651. var row = data[i];
  1652. var key = callback(row);
  1653. if (groups[key]) {
  1654. groups[key].push(row);
  1655. } else {
  1656. groups[key] = [row];
  1657. }
  1658. }
  1659. return groups;
  1660. }
  1661. function group$1(data, fields) {
  1662. var appendConditions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  1663. if (!fields) {
  1664. return [data];
  1665. }
  1666. var groups = groupToMap$1(data, fields);
  1667. var array = [];
  1668. if (fields.length === 1 && appendConditions[fields[0]]) {
  1669. var _values = appendConditions[fields[0]];
  1670. each(_values, function (value) {
  1671. value = '_' + value;
  1672. array.push(groups[value]);
  1673. });
  1674. } else {
  1675. for (var i in groups) {
  1676. array.push(groups[i]);
  1677. }
  1678. }
  1679. return array;
  1680. }
  1681. function remove$1(arr, obj) {
  1682. if (!arr) {
  1683. return;
  1684. }
  1685. var index = arr.indexOf(obj);
  1686. if (index !== -1) {
  1687. arr.splice(index, 1);
  1688. }
  1689. }
  1690. function getRange$1(values) {
  1691. if (!values.length) {
  1692. return {
  1693. min: 0,
  1694. max: 0
  1695. };
  1696. }
  1697. var max = Math.max.apply(null, values);
  1698. var min = Math.min.apply(null, values);
  1699. return {
  1700. min,
  1701. max
  1702. };
  1703. }
  1704. var array = /*#__PURE__*/Object.freeze({
  1705. __proto__: null,
  1706. merge: merge,
  1707. values: values$1,
  1708. firstValue: firstValue$1,
  1709. group: group$1,
  1710. groupToMap: groupToMap$1,
  1711. remove: remove$1,
  1712. getRange: getRange$1
  1713. });
  1714. /**
  1715. * Detects support for options object argument in addEventListener.
  1716. * https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support
  1717. * @private
  1718. */
  1719. var supportsEventListenerOptions = function () {
  1720. var supports = false;
  1721. try {
  1722. var options = Object.defineProperty({}, 'passive', {
  1723. get() {
  1724. supports = true;
  1725. }
  1726. });
  1727. window.addEventListener('e', null, options);
  1728. } catch (e) {// continue regardless of error
  1729. }
  1730. return supports;
  1731. }(); // Default passive to true as expected by Chrome for 'touchstart' and 'touchend' events.
  1732. // https://github.com/chartjs/Chart.js/issues/4287
  1733. var eventListenerOptions = supportsEventListenerOptions ? {
  1734. passive: true
  1735. } : false;
  1736. /* global wx, my */
  1737. // weixin miniprogram
  1738. var isWx = typeof wx === 'object' && typeof wx.getSystemInfoSync === 'function'; // ant miniprogram
  1739. var isMy = typeof my === 'object' && typeof my.getSystemInfoSync === 'function'; // in node
  1740. var isNode = typeof global && !typeof window; // in browser
  1741. var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.sessionStorage !== 'undefined';
  1742. function isCanvasElement(el) {
  1743. if (!el || typeof el !== 'object') return false;
  1744. if (el.nodeType === 1 && el.nodeName) {
  1745. // HTMLCanvasElement
  1746. return true;
  1747. } // CanvasElement
  1748. return !!el.isCanvasElement;
  1749. }
  1750. function getPixelRatio() {
  1751. return window && window.devicePixelRatio || 1;
  1752. }
  1753. function getStyle(el, property) {
  1754. return el.currentStyle ? el.currentStyle[property] : document.defaultView.getComputedStyle(el, null).getPropertyValue(property);
  1755. }
  1756. function getWidth(el) {
  1757. var width = getStyle(el, 'width');
  1758. if (width === 'auto') {
  1759. width = el.offsetWidth;
  1760. }
  1761. return parseFloat(width);
  1762. }
  1763. function getHeight(el) {
  1764. var height = getStyle(el, 'height');
  1765. if (height === 'auto') {
  1766. height = el.offsetHeight;
  1767. }
  1768. return parseFloat(height);
  1769. }
  1770. function getDomById(id) {
  1771. if (!id) {
  1772. return null;
  1773. }
  1774. return document.getElementById(id);
  1775. }
  1776. function getRelativePosition(point, canvas) {
  1777. var canvasDom = canvas.get('el');
  1778. if (!canvasDom) return point;
  1779. var {
  1780. top,
  1781. right,
  1782. bottom,
  1783. left
  1784. } = canvasDom.getBoundingClientRect();
  1785. var paddingLeft = parseFloat(getStyle(canvasDom, 'padding-left'));
  1786. var paddingTop = parseFloat(getStyle(canvasDom, 'padding-top'));
  1787. var paddingRight = parseFloat(getStyle(canvasDom, 'padding-right'));
  1788. var paddingBottom = parseFloat(getStyle(canvasDom, 'padding-bottom'));
  1789. var width = right - left - paddingLeft - paddingRight;
  1790. var height = bottom - top - paddingTop - paddingBottom;
  1791. var pixelRatio = canvas.get('pixelRatio');
  1792. var mouseX = (point.x - left - paddingLeft) / width * canvasDom.width / pixelRatio;
  1793. var mouseY = (point.y - top - paddingTop) / height * canvasDom.height / pixelRatio;
  1794. return {
  1795. x: mouseX,
  1796. y: mouseY
  1797. };
  1798. }
  1799. function addEventListener(source, type, listener) {
  1800. source.addEventListener(type, listener, eventListenerOptions);
  1801. }
  1802. function removeEventListener(source, type, listener) {
  1803. source.removeEventListener(type, listener, eventListenerOptions);
  1804. }
  1805. function createEventObj(type, chart, x, y, nativeEvent) {
  1806. return {
  1807. type,
  1808. chart,
  1809. native: nativeEvent || null,
  1810. x: x !== undefined ? x : null,
  1811. y: y !== undefined ? y : null
  1812. };
  1813. }
  1814. function createEvent(event, chart) {
  1815. var type = event.type;
  1816. var clientPoint; // 说明是touch相关事件
  1817. if (event.touches) {
  1818. // https://developer.mozilla.org/zh-CN/docs/Web/API/TouchEvent/changedTouches
  1819. // 这里直接拿changedTouches就可以了,不管是touchstart, touchmove, touchend changedTouches 都是有的
  1820. // 为了以防万一,做个空判断
  1821. var touch = event.changedTouches[0] || {}; // x, y: 相对canvas原点的位置,clientX, clientY 相对于可视窗口的位置
  1822. var {
  1823. x,
  1824. y,
  1825. clientX,
  1826. clientY
  1827. } = touch; // 小程序环境会有x,y,这里就直接返回
  1828. if (x && y) {
  1829. return createEventObj(type, chart, x, y, event);
  1830. }
  1831. clientPoint = {
  1832. x: clientX,
  1833. y: clientY
  1834. };
  1835. } else {
  1836. // mouse相关事件
  1837. clientPoint = {
  1838. x: event.clientX,
  1839. y: event.clientY
  1840. };
  1841. } // 理论上应该是只有有在浏览器环境才会走到这里
  1842. var canvas = chart.get('canvas'); // 通过clientX, clientY 计算x, y
  1843. var point = getRelativePosition(clientPoint, canvas);
  1844. return createEventObj(type, chart, point.x, point.y, event);
  1845. }
  1846. function measureText(text, font, ctx) {
  1847. if (!ctx) {
  1848. ctx = document.createElement('canvas').getContext('2d');
  1849. }
  1850. ctx.font = font || '12px sans-serif';
  1851. return ctx.measureText(text);
  1852. }
  1853. /**
  1854. * @fileOverview Utility for F2
  1855. * @author dxq613 @gmail.com
  1856. * @author sima.zhang1990@gmail.com
  1857. */
  1858. function isObjectValueEqual(a, b) {
  1859. // for vue.js
  1860. a = Object.assign({}, a);
  1861. b = Object.assign({}, b);
  1862. var aProps = Object.getOwnPropertyNames(a);
  1863. var bProps = Object.getOwnPropertyNames(b);
  1864. if (aProps.length !== bProps.length) {
  1865. return false;
  1866. }
  1867. for (var i = 0, len = aProps.length; i < len; i++) {
  1868. var propName = aProps[i];
  1869. if (a[propName] !== b[propName]) {
  1870. return false;
  1871. }
  1872. }
  1873. return true;
  1874. }
  1875. function parsePadding(padding) {
  1876. var top;
  1877. var right;
  1878. var bottom;
  1879. var left;
  1880. if (isNumber(padding) || isString(padding)) {
  1881. top = bottom = left = right = padding;
  1882. } else if (isArray(padding)) {
  1883. top = padding[0];
  1884. right = !isNil(padding[1]) ? padding[1] : padding[0];
  1885. bottom = !isNil(padding[2]) ? padding[2] : padding[0];
  1886. left = !isNil(padding[3]) ? padding[3] : right;
  1887. }
  1888. return [top, right, bottom, left];
  1889. }
  1890. function directionEnabled(mode, dir) {
  1891. if (mode === undefined) {
  1892. return true;
  1893. } else if (typeof mode === 'string') {
  1894. return mode.indexOf(dir) !== -1;
  1895. }
  1896. return false;
  1897. }
  1898. function toTimeStamp(value) {
  1899. if (isString(value)) {
  1900. if (value.indexOf('T') > 0) {
  1901. value = new Date(value).getTime();
  1902. } else {
  1903. // new Date('2010/01/10') 和 new Date('2010-01-10') 的差别在于:
  1904. // 如果仅有年月日时,前者是带有时区的: Fri Jan 10 2020 02:40:13 GMT+0800 (中国标准时间)
  1905. // 后者会格式化成 Sun Jan 10 2010 08:00:00 GMT+0800 (中国标准时间)
  1906. value = new Date(value.replace(/-/gi, '/')).getTime();
  1907. }
  1908. }
  1909. if (isDate(value)) {
  1910. value = value.getTime();
  1911. }
  1912. return value;
  1913. }
  1914. var Util = /*#__PURE__*/Object.freeze({
  1915. __proto__: null,
  1916. Array: array,
  1917. upperFirst: upperFirst,
  1918. lowerFirst: lowerFirst,
  1919. isString: isString,
  1920. isNumber: isNumber,
  1921. isBoolean: isBoolean,
  1922. isFunction: isFunction,
  1923. isDate: isDate,
  1924. isArray: isArray,
  1925. isNil: isNil,
  1926. isObject: isObject,
  1927. isPlainObject: isPlainObject,
  1928. isEqual: isEqual,
  1929. deepMix: deepMix,
  1930. mix: mix,
  1931. each: each,
  1932. uniq: uniq,
  1933. find: find,
  1934. isObjectValueEqual: isObjectValueEqual,
  1935. parsePadding: parsePadding,
  1936. directionEnabled: directionEnabled,
  1937. toTimeStamp: toTimeStamp,
  1938. isWx: isWx,
  1939. isMy: isMy,
  1940. isNode: isNode,
  1941. isBrowser: isBrowser,
  1942. isCanvasElement: isCanvasElement,
  1943. getPixelRatio: getPixelRatio,
  1944. getStyle: getStyle,
  1945. getWidth: getWidth,
  1946. getHeight: getHeight,
  1947. getDomById: getDomById,
  1948. getRelativePosition: getRelativePosition,
  1949. addEventListener: addEventListener,
  1950. removeEventListener: removeEventListener,
  1951. createEvent: createEvent,
  1952. measureText: measureText
  1953. });
  1954. /**
  1955. * @fileOverview default theme
  1956. * @author dxq613@gail.com
  1957. */
  1958. var color1 = '#E8E8E8'; // color of axis-line and axis-grid
  1959. var color2 = '#808080'; // color of axis label
  1960. var defaultAxis = {
  1961. label: {
  1962. fill: color2,
  1963. fontSize: 10
  1964. },
  1965. line: {
  1966. stroke: color1,
  1967. lineWidth: 1
  1968. },
  1969. grid: {
  1970. type: 'line',
  1971. stroke: color1,
  1972. lineWidth: 1,
  1973. lineDash: [2]
  1974. },
  1975. tickLine: null,
  1976. labelOffset: 7.5
  1977. };
  1978. var Theme = {
  1979. fontFamily: '"Helvetica Neue", "San Francisco", Helvetica, Tahoma, Arial, "PingFang SC", "Hiragino Sans GB", "Heiti SC", "Microsoft YaHei", sans-serif',
  1980. defaultColor: '#1890FF',
  1981. pixelRatio: 1,
  1982. padding: 'auto',
  1983. appendPadding: 15,
  1984. colors: ['#1890FF', '#2FC25B', '#FACC14', '#223273', '#8543E0', '#13C2C2', '#3436C7', '#F04864'],
  1985. shapes: {
  1986. line: ['line', 'dash'],
  1987. point: ['circle', 'hollowCircle']
  1988. },
  1989. sizes: [4, 10],
  1990. axis: {
  1991. common: defaultAxis,
  1992. // common axis configuration
  1993. bottom: mix({}, defaultAxis, {
  1994. grid: null
  1995. }),
  1996. left: mix({}, defaultAxis, {
  1997. line: null
  1998. }),
  1999. right: mix({}, defaultAxis, {
  2000. line: null
  2001. }),
  2002. circle: mix({}, defaultAxis, {
  2003. line: null
  2004. }),
  2005. radius: mix({}, defaultAxis, {
  2006. labelOffset: 4
  2007. })
  2008. },
  2009. shape: {
  2010. line: {
  2011. lineWidth: 2,
  2012. lineJoin: 'round',
  2013. lineCap: 'round'
  2014. },
  2015. point: {
  2016. lineWidth: 0,
  2017. size: 3
  2018. },
  2019. area: {
  2020. fillOpacity: 0.1
  2021. }
  2022. },
  2023. _defaultAxis: defaultAxis
  2024. };
  2025. var Global = {
  2026. version: '3.7.8',
  2027. scales: {},
  2028. widthRatio: {
  2029. column: 1 / 2,
  2030. rose: 0.999999,
  2031. multiplePie: 3 / 4
  2032. },
  2033. lineDash: [4, 4]
  2034. };
  2035. Global.setTheme = function (theme) {
  2036. deepMix(Global, theme);
  2037. };
  2038. Global.setTheme(Theme);
  2039. var EVENT_AFTER_INIT = 'afterinit';
  2040. var EVENT_BEFORE_RENDER = 'beforerender';
  2041. var EVENT_AFTER_RENDER = 'afterrender';
  2042. var EVENT_BEFORE_DATA_CHANGE = 'beforedatachange';
  2043. var EVENT_AFTER_DATA_CHANGE = 'afterdatachange';
  2044. var EVENT_AFTER_SIZE_CHANGE = '_aftersizechange';
  2045. var EVENT_AFTER_GEOM_INIT = '_aftergeominit';
  2046. var EVENT_BEFORE_GEOM_DRAW = 'beforegeomdraw';
  2047. var EVENT_AFTER_GEOM_DRAW = 'aftergeomdraw';
  2048. var EVENT_CLEAR = 'clear';
  2049. var EVENT_CLEAR_INNER = 'clearinner';
  2050. var EVENT_REPAINT = 'repaint';
  2051. // 实现简单的事件机制
  2052. class EventEmit {
  2053. constructor() {
  2054. this.__events = {};
  2055. }
  2056. on(type, listener) {
  2057. if (!type || !listener) {
  2058. return;
  2059. }
  2060. var events = this.__events[type] || [];
  2061. events.push(listener);
  2062. this.__events[type] = events;
  2063. }
  2064. emit(type, e) {
  2065. var _this = this;
  2066. if (isObject(type)) {
  2067. e = type;
  2068. type = e && e.type;
  2069. }
  2070. if (!type) {
  2071. return;
  2072. }
  2073. var events = this.__events[type];
  2074. if (!events || !events.length) {
  2075. return;
  2076. }
  2077. events.forEach(function (listener) {
  2078. listener.call(_this, e);
  2079. });
  2080. }
  2081. off(type, listener) {
  2082. var __events = this.__events;
  2083. var events = __events[type];
  2084. if (!events || !events.length) {
  2085. return;
  2086. } // 如果没有指定方法,则删除所有项
  2087. if (!listener) {
  2088. delete __events[type];
  2089. return;
  2090. } // 删除指定的 listener
  2091. for (var i = 0, len = events.length; i < len; i++) {
  2092. if (events[i] === listener) {
  2093. events.splice(i, 1);
  2094. i--;
  2095. }
  2096. }
  2097. }
  2098. }
  2099. /**
  2100. * @fileOverview Base class of chart and geometry
  2101. * @author dxq613@gmail.com
  2102. */
  2103. class Base extends EventEmit {
  2104. getDefaultCfg() {
  2105. return {};
  2106. }
  2107. constructor(cfg) {
  2108. super();
  2109. var attrs = {};
  2110. var defaultCfg = this.getDefaultCfg();
  2111. this._attrs = attrs;
  2112. mix(attrs, defaultCfg, cfg);
  2113. }
  2114. get(name) {
  2115. return this._attrs[name];
  2116. }
  2117. set(name, value) {
  2118. this._attrs[name] = value;
  2119. }
  2120. destroy() {
  2121. this._attrs = {};
  2122. this.destroyed = true;
  2123. }
  2124. }
  2125. class Plot {
  2126. constructor(cfg) {
  2127. mix(this, cfg);
  2128. this._init();
  2129. }
  2130. _init() {
  2131. var self = this;
  2132. var start = self.start;
  2133. var end = self.end;
  2134. var xMin = Math.min(start.x, end.x);
  2135. var xMax = Math.max(start.x, end.x);
  2136. var yMin = Math.min(start.y, end.y);
  2137. var yMax = Math.max(start.y, end.y);
  2138. this.tl = {
  2139. x: xMin,
  2140. y: yMin
  2141. };
  2142. this.tr = {
  2143. x: xMax,
  2144. y: yMin
  2145. };
  2146. this.bl = {
  2147. x: xMin,
  2148. y: yMax
  2149. };
  2150. this.br = {
  2151. x: xMax,
  2152. y: yMax
  2153. };
  2154. this.width = xMax - xMin;
  2155. this.height = yMax - yMin;
  2156. }
  2157. /**
  2158. * reset
  2159. * @param {Object} start start point
  2160. * @param {Object} end end point
  2161. */
  2162. reset(start, end) {
  2163. this.start = start;
  2164. this.end = end;
  2165. this._init();
  2166. }
  2167. /**
  2168. * check the point is in the range of plot
  2169. * @param {Number} x x value
  2170. * @param {[type]} y y value
  2171. * @return {Boolean} return the result
  2172. */
  2173. isInRange(x, y) {
  2174. if (isObject(x)) {
  2175. y = x.y;
  2176. x = x.x;
  2177. }
  2178. var tl = this.tl;
  2179. var br = this.br;
  2180. return tl.x <= x && x <= br.x && tl.y <= y && y <= br.y;
  2181. }
  2182. }
  2183. var Matrix = {
  2184. generateDefault() {
  2185. return [1, 0, 0, 1, 0, 0];
  2186. },
  2187. isChanged(m) {
  2188. return m[0] !== 1 || m[1] !== 0 || m[2] !== 0 || m[3] !== 1 || m[4] !== 0 || m[5] !== 0;
  2189. },
  2190. multiply(m1, m2) {
  2191. var m11 = m1[0] * m2[0] + m1[2] * m2[1];
  2192. var m12 = m1[1] * m2[0] + m1[3] * m2[1];
  2193. var m21 = m1[0] * m2[2] + m1[2] * m2[3];
  2194. var m22 = m1[1] * m2[2] + m1[3] * m2[3];
  2195. var dx = m1[0] * m2[4] + m1[2] * m2[5] + m1[4];
  2196. var dy = m1[1] * m2[4] + m1[3] * m2[5] + m1[5];
  2197. return [m11, m12, m21, m22, dx, dy];
  2198. },
  2199. scale(out, m, v) {
  2200. out[0] = m[0] * v[0];
  2201. out[1] = m[1] * v[0];
  2202. out[2] = m[2] * v[1];
  2203. out[3] = m[3] * v[1];
  2204. out[4] = m[4];
  2205. out[5] = m[5];
  2206. return out;
  2207. },
  2208. rotate(out, m, radian) {
  2209. var c = Math.cos(radian);
  2210. var s = Math.sin(radian);
  2211. var m11 = m[0] * c + m[2] * s;
  2212. var m12 = m[1] * c + m[3] * s;
  2213. var m21 = m[0] * -s + m[2] * c;
  2214. var m22 = m[1] * -s + m[3] * c;
  2215. out[0] = m11;
  2216. out[1] = m12;
  2217. out[2] = m21;
  2218. out[3] = m22;
  2219. out[4] = m[4];
  2220. out[5] = m[5];
  2221. return out;
  2222. },
  2223. translate(out, m, v) {
  2224. out[0] = m[0];
  2225. out[1] = m[1];
  2226. out[2] = m[2];
  2227. out[3] = m[3];
  2228. out[4] = m[4] + m[0] * v[0] + m[2] * v[1];
  2229. out[5] = m[5] + m[1] * v[0] + m[3] * v[1];
  2230. return out;
  2231. },
  2232. transform(m, actions) {
  2233. var out = [].concat(m);
  2234. for (var i = 0, len = actions.length; i < len; i++) {
  2235. var action = actions[i];
  2236. switch (action[0]) {
  2237. case 't':
  2238. Matrix.translate(out, out, [action[1], action[2]]);
  2239. break;
  2240. case 's':
  2241. Matrix.scale(out, out, [action[1], action[2]]);
  2242. break;
  2243. case 'r':
  2244. Matrix.rotate(out, out, action[1]);
  2245. break;
  2246. }
  2247. }
  2248. return out;
  2249. }
  2250. };
  2251. /**
  2252. * 2 Dimensional Vector
  2253. * @module vector2
  2254. */
  2255. var Vector2 = {
  2256. /**
  2257. * Creates a new, empty vector2
  2258. *
  2259. * @return {vector2} a new 2D vector
  2260. */
  2261. create() {
  2262. return [0, 0];
  2263. },
  2264. /**
  2265. * Calculates the length of a vector2
  2266. *
  2267. * @param {vector2} v vector to calculate length of
  2268. * @return {Number} length of v
  2269. */
  2270. length(v) {
  2271. var x = v[0];
  2272. var y = v[1];
  2273. return Math.sqrt(x * x + y * y);
  2274. },
  2275. /**
  2276. * Normalize a vector2
  2277. *
  2278. * @param {vector2} out the receiving vector
  2279. * @param {vector2} v vector to normalize
  2280. * @return {vector2} out
  2281. */
  2282. normalize(out, v) {
  2283. var len = this.length(v);
  2284. if (len === 0) {
  2285. out[0] = 0;
  2286. out[1] = 0;
  2287. } else {
  2288. out[0] = v[0] / len;
  2289. out[1] = v[1] / len;
  2290. }
  2291. return out;
  2292. },
  2293. /**
  2294. * Adds two vector2's
  2295. *
  2296. * @param {vector2} out the receiving vector
  2297. * @param {vector2} v1 the first operand
  2298. * @param {vector2} v2 the second operand
  2299. * @return {vector2} out
  2300. */
  2301. add(out, v1, v2) {
  2302. out[0] = v1[0] + v2[0];
  2303. out[1] = v1[1] + v2[1];
  2304. return out;
  2305. },
  2306. /**
  2307. * Subtracts vector v2 from vector v1
  2308. *
  2309. * @param {vector2} out the receiving vector
  2310. * @param {vector2} v1 the first operand
  2311. * @param {vector2} v2 the second operand
  2312. * @return {vector2} out
  2313. */
  2314. sub(out, v1, v2) {
  2315. out[0] = v1[0] - v2[0];
  2316. out[1] = v1[1] - v2[1];
  2317. return out;
  2318. },
  2319. /**
  2320. * Scales a vector2 by a scalar number
  2321. *
  2322. * @param {vector2} out the receiving vector
  2323. * @param {vector2} v the vector to scale
  2324. * @param {Number} s amount to scale the vector by
  2325. * @return {vector2} out
  2326. */
  2327. scale(out, v, s) {
  2328. out[0] = v[0] * s;
  2329. out[1] = v[1] * s;
  2330. return out;
  2331. },
  2332. /**
  2333. * Calculates the dot product of two vector2's
  2334. *
  2335. * @param {vector2} v1 the first operand
  2336. * @param {vector2} v2 the second operand
  2337. * @return {Number} dot product of v1 and v2
  2338. */
  2339. dot(v1, v2) {
  2340. return v1[0] * v2[0] + v1[1] * v2[1];
  2341. },
  2342. /**
  2343. * Calculates the direction of two vector2's
  2344. *
  2345. * @param {vector2} v1 the first operand
  2346. * @param {vector2} v2 the second operand
  2347. * @return {Boolean} the direction of v1 and v2
  2348. */
  2349. direction(v1, v2) {
  2350. return v1[0] * v2[1] - v2[0] * v1[1];
  2351. },
  2352. /**
  2353. * Calculates the angle of two vector2's
  2354. *
  2355. * @param {vector2} v1 the first operand
  2356. * @param {vector2} v2 the second operand
  2357. * @return {Number} angle of v1 and v2
  2358. */
  2359. angle(v1, v2) {
  2360. var theta = this.dot(v1, v2) / (this.length(v1) * this.length(v2));
  2361. return Math.acos(theta);
  2362. },
  2363. /**
  2364. * Calculates the angle of two vector2's with direction
  2365. *
  2366. * @param {vector2} v1 the first operand
  2367. * @param {vector2} v2 the second operand
  2368. * @param {Boolean} direction the direction of two vector2's
  2369. * @return {Number} angle of v1 and v2
  2370. */
  2371. angleTo(v1, v2, direction) {
  2372. var angle = this.angle(v1, v2);
  2373. var angleLargeThanPI = this.direction(v1, v2) >= 0;
  2374. if (direction) {
  2375. if (angleLargeThanPI) {
  2376. return Math.PI * 2 - angle;
  2377. }
  2378. return angle;
  2379. }
  2380. if (angleLargeThanPI) {
  2381. return angle;
  2382. }
  2383. return Math.PI * 2 - angle;
  2384. },
  2385. /**
  2386. * whether a vector2 is zero vector
  2387. *
  2388. * @param {vector2} v vector to calculate
  2389. * @return {Boolean} is or not a zero vector
  2390. */
  2391. zero(v) {
  2392. return v[0] === 0 && v[1] === 0;
  2393. },
  2394. /**
  2395. * Calculates the euclidian distance between two vector2's
  2396. *
  2397. * @param {vector2} v1 the first operand
  2398. * @param {vector2} v2 the second operand
  2399. * @return {Number} distance between a and b
  2400. */
  2401. distance(v1, v2) {
  2402. var x = v2[0] - v1[0];
  2403. var y = v2[1] - v1[1];
  2404. return Math.sqrt(x * x + y * y);
  2405. },
  2406. /**
  2407. * Creates a new vector2 initialized with values from an existing vector
  2408. *
  2409. * @param {vector2} v vector to clone
  2410. * @return {Array} a new 2D vector
  2411. */
  2412. clone(v) {
  2413. return [v[0], v[1]];
  2414. },
  2415. /**
  2416. * Return the minimum of two vector2's
  2417. *
  2418. * @param {vector2} out the receiving vector
  2419. * @param {vector2} v1 the first operand
  2420. * @param {vector2} v2 the second operand
  2421. * @return {vector2} out
  2422. */
  2423. min(out, v1, v2) {
  2424. out[0] = Math.min(v1[0], v2[0]);
  2425. out[1] = Math.min(v1[1], v2[1]);
  2426. return out;
  2427. },
  2428. /**
  2429. * Return the maximum of two vector2's
  2430. *
  2431. * @param {vector2} out the receiving vector
  2432. * @param {vector2} v1 the first operand
  2433. * @param {vector2} v2 the second operand
  2434. * @return {vector2} out
  2435. */
  2436. max(out, v1, v2) {
  2437. out[0] = Math.max(v1[0], v2[0]);
  2438. out[1] = Math.max(v1[1], v2[1]);
  2439. return out;
  2440. },
  2441. /**
  2442. * Transforms the vector2 with a mat2d
  2443. *
  2444. * @param {vector2} out the receiving vector
  2445. * @param {vector2} v the vector to transform
  2446. * @param {mat2d} m matrix to transform with
  2447. * @return {vector2} out
  2448. */
  2449. transformMat2d(out, v, m) {
  2450. var x = v[0];
  2451. var y = v[1];
  2452. out[0] = m[0] * x + m[2] * y + m[4];
  2453. out[1] = m[1] * x + m[3] * y + m[5];
  2454. return out;
  2455. }
  2456. };
  2457. var defaultMatrix = [1, 0, 0, 1, 0, 0];
  2458. class Base$1 {
  2459. _initDefaultCfg() {}
  2460. constructor(cfg) {
  2461. this._initDefaultCfg();
  2462. mix(this, cfg);
  2463. var start;
  2464. var end;
  2465. if (this.plot) {
  2466. start = this.plot.bl;
  2467. end = this.plot.tr;
  2468. this.start = start;
  2469. this.end = end;
  2470. } else {
  2471. start = this.start;
  2472. end = this.end;
  2473. }
  2474. this.init(start, end);
  2475. }
  2476. _scale(s1, s2) {
  2477. var matrix = this.matrix;
  2478. var center = this.center;
  2479. Matrix.translate(matrix, matrix, [center.x, center.y]);
  2480. Matrix.scale(matrix, matrix, [s1, s2]);
  2481. Matrix.translate(matrix, matrix, [-center.x, -center.y]);
  2482. }
  2483. init(start, end) {
  2484. this.matrix = [].concat(defaultMatrix); // 设置中心点
  2485. this.center = {
  2486. x: (end.x - start.x) / 2 + start.x,
  2487. y: (end.y - start.y) / 2 + start.y
  2488. };
  2489. if (this.scale) {
  2490. this._scale(this.scale[0], this.scale[1]);
  2491. }
  2492. }
  2493. convertPoint(point) {
  2494. var {
  2495. x,
  2496. y
  2497. } = this._convertPoint(point);
  2498. if (!Matrix.isChanged(this.matrix)) {
  2499. return {
  2500. x,
  2501. y
  2502. };
  2503. }
  2504. var vector = [x, y];
  2505. Vector2.transformMat2d(vector, vector, this.matrix);
  2506. return {
  2507. x: vector[0],
  2508. y: vector[1]
  2509. };
  2510. }
  2511. invertPoint(point) {
  2512. return this._invertPoint(point);
  2513. }
  2514. _convertPoint(point) {
  2515. return point;
  2516. }
  2517. _invertPoint(point) {
  2518. return point;
  2519. }
  2520. reset(plot) {
  2521. this.plot = plot;
  2522. var {
  2523. bl,
  2524. tr
  2525. } = plot;
  2526. this.start = bl;
  2527. this.end = tr;
  2528. this.init(bl, tr);
  2529. }
  2530. }
  2531. class Cartesian extends Base$1 {
  2532. _initDefaultCfg() {
  2533. this.type = 'cartesian';
  2534. this.transposed = false;
  2535. this.isRect = true;
  2536. }
  2537. init(start, end) {
  2538. super.init(start, end);
  2539. this.x = {
  2540. start: start.x,
  2541. end: end.x
  2542. };
  2543. this.y = {
  2544. start: start.y,
  2545. end: end.y
  2546. };
  2547. }
  2548. _convertPoint(point) {
  2549. var self = this;
  2550. var transposed = self.transposed;
  2551. var xDim = transposed ? 'y' : 'x';
  2552. var yDim = transposed ? 'x' : 'y';
  2553. var x = self.x;
  2554. var y = self.y;
  2555. return {
  2556. x: x.start + (x.end - x.start) * point[xDim],
  2557. y: y.start + (y.end - y.start) * point[yDim]
  2558. };
  2559. }
  2560. _invertPoint(point) {
  2561. var self = this;
  2562. var transposed = self.transposed;
  2563. var xDim = transposed ? 'y' : 'x';
  2564. var yDim = transposed ? 'x' : 'y';
  2565. var x = self.x;
  2566. var y = self.y;
  2567. var rst = {};
  2568. rst[xDim] = (point.x - x.start) / (x.end - x.start);
  2569. rst[yDim] = (point.y - y.start) / (y.end - y.start);
  2570. return rst;
  2571. }
  2572. }
  2573. Base$1.Cartesian = Cartesian;
  2574. Base$1.Rect = Cartesian;
  2575. /**
  2576. * @fileOverview the Attribute base class
  2577. */
  2578. function toScaleString(scale, value) {
  2579. if (isString(value)) {
  2580. return value;
  2581. }
  2582. return scale.invert(scale.scale(value));
  2583. }
  2584. /**
  2585. * 所有视觉通道属性的基类
  2586. * @class Attr
  2587. */
  2588. class AttributeBase {
  2589. constructor(cfg) {
  2590. var _this = this;
  2591. /**
  2592. * 属性的类型
  2593. * @type {String}
  2594. */
  2595. this.type = 'base';
  2596. /**
  2597. * 属性的名称
  2598. * @type {String}
  2599. */
  2600. this.name = null;
  2601. /**
  2602. * 回调函数
  2603. * @type {Function}
  2604. */
  2605. this.method = null;
  2606. /**
  2607. * 备选的值数组
  2608. * @type {Array}
  2609. */
  2610. this.values = [];
  2611. /**
  2612. * 属性内部的度量
  2613. * @type {Array}
  2614. */
  2615. this.scales = [];
  2616. /**
  2617. * 是否通过线性取值, 如果未指定,则根据数值的类型判定
  2618. * @type {Boolean}
  2619. */
  2620. this.linear = null;
  2621. /**
  2622. * 当用户设置的 callback 返回 null 时, 应该返回默认 callback 中的值
  2623. */
  2624. var mixedCallback = null;
  2625. var defaultCallback = this.callback;
  2626. if (cfg.callback) {
  2627. var userCallback = cfg.callback;
  2628. mixedCallback = function mixedCallback() {
  2629. for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
  2630. params[_key] = arguments[_key];
  2631. }
  2632. var ret = userCallback(...params);
  2633. if (isNil(ret)) {
  2634. ret = defaultCallback.apply(_this, params);
  2635. }
  2636. return ret;
  2637. };
  2638. }
  2639. mix(this, cfg);
  2640. if (mixedCallback) {
  2641. mix(this, {
  2642. callback: mixedCallback
  2643. });
  2644. }
  2645. } // 获取属性值,将值映射到视觉通道
  2646. _getAttrValue(scale, value) {
  2647. var values = this.values;
  2648. if (scale.isCategory && !this.linear) {
  2649. var index = scale.translate(value);
  2650. return values[index % values.length];
  2651. }
  2652. var percent = scale.scale(value);
  2653. return this.getLinearValue(percent);
  2654. }
  2655. /**
  2656. * 如果进行线性映射,返回对应的映射值
  2657. * @protected
  2658. * @param {Number} percent 百分比
  2659. * @return {*} 颜色值、形状、大小等
  2660. */
  2661. getLinearValue(percent) {
  2662. var values = this.values;
  2663. var steps = values.length - 1;
  2664. var step = Math.floor(steps * percent);
  2665. var leftPercent = steps * percent - step;
  2666. var start = values[step];
  2667. var end = step === steps ? start : values[step + 1];
  2668. var rstValue = start + (end - start) * leftPercent;
  2669. return rstValue;
  2670. }
  2671. /**
  2672. * 默认的回调函数
  2673. * @param {*} value 回调函数的值
  2674. * @type {Function}
  2675. * @return {Array} 返回映射后的值
  2676. */
  2677. callback(value) {
  2678. var self = this;
  2679. var scale = self.scales[0];
  2680. var rstValue = null;
  2681. if (scale.type === 'identity') {
  2682. rstValue = scale.value;
  2683. } else {
  2684. rstValue = self._getAttrValue(scale, value);
  2685. }
  2686. return rstValue;
  2687. }
  2688. /**
  2689. * 根据度量获取属性名
  2690. * @return {Array} dims of this Attribute
  2691. */
  2692. getNames() {
  2693. var scales = this.scales;
  2694. var names = this.names;
  2695. var length = Math.min(scales.length, names.length);
  2696. var rst = [];
  2697. for (var i = 0; i < length; i++) {
  2698. rst.push(names[i]);
  2699. }
  2700. return rst;
  2701. }
  2702. /**
  2703. * 根据度量获取维度名
  2704. * @return {Array} dims of this Attribute
  2705. */
  2706. getFields() {
  2707. var scales = this.scales;
  2708. var rst = [];
  2709. each(scales, function (scale) {
  2710. rst.push(scale.field);
  2711. });
  2712. return rst;
  2713. }
  2714. /**
  2715. * 根据名称获取度量
  2716. * @param {String} name the name of scale
  2717. * @return {Scale} scale
  2718. */
  2719. getScale(name) {
  2720. var scales = this.scales;
  2721. var names = this.names;
  2722. var index = names.indexOf(name);
  2723. return scales[index];
  2724. }
  2725. /**
  2726. * 映射数据
  2727. * @param {*} param1...paramn 多个数值
  2728. * @return {Array} 映射的值组成的数组
  2729. */
  2730. mapping() {
  2731. var scales = this.scales;
  2732. var callback = this.callback;
  2733. for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  2734. params[_key2] = arguments[_key2];
  2735. }
  2736. var values = params;
  2737. if (callback) {
  2738. for (var i = 0, len = params.length; i < len; i++) {
  2739. params[i] = this._toOriginParam(params[i], scales[i]);
  2740. }
  2741. values = callback.apply(this, params);
  2742. }
  2743. values = [].concat(values);
  2744. return values;
  2745. } // 原始的参数
  2746. _toOriginParam(param, scale) {
  2747. var rst = param;
  2748. if (!scale.isLinear) {
  2749. if (isArray(param)) {
  2750. rst = [];
  2751. for (var i = 0, len = param.length; i < len; i++) {
  2752. rst.push(toScaleString(scale, param[i]));
  2753. }
  2754. } else {
  2755. rst = toScaleString(scale, param);
  2756. }
  2757. }
  2758. return rst;
  2759. }
  2760. }
  2761. class Position extends AttributeBase {
  2762. constructor(cfg) {
  2763. super(cfg);
  2764. this.names = ['x', 'y'];
  2765. this.type = 'position';
  2766. }
  2767. mapping(x, y) {
  2768. var scales = this.scales;
  2769. var coord = this.coord;
  2770. var scaleX = scales[0];
  2771. var scaleY = scales[1];
  2772. var rstX;
  2773. var rstY;
  2774. var obj;
  2775. if (isNil(x) || isNil(y)) {
  2776. return [];
  2777. }
  2778. if (isArray(y) && isArray(x)) {
  2779. rstX = [];
  2780. rstY = [];
  2781. for (var i = 0, j = 0, xLen = x.length, yLen = y.length; i < xLen && j < yLen; i++, j++) {
  2782. obj = coord.convertPoint({
  2783. x: scaleX.scale(x[i]),
  2784. y: scaleY.scale(y[j])
  2785. });
  2786. rstX.push(obj.x);
  2787. rstY.push(obj.y);
  2788. }
  2789. } else if (isArray(y)) {
  2790. x = scaleX.scale(x);
  2791. rstY = [];
  2792. each(y, function (yVal) {
  2793. yVal = scaleY.scale(yVal);
  2794. obj = coord.convertPoint({
  2795. x,
  2796. y: yVal
  2797. });
  2798. if (rstX && rstX !== obj.x) {
  2799. if (!isArray(rstX)) {
  2800. rstX = [rstX];
  2801. }
  2802. rstX.push(obj.x);
  2803. } else {
  2804. rstX = obj.x;
  2805. }
  2806. rstY.push(obj.y);
  2807. });
  2808. } else if (isArray(x)) {
  2809. y = scaleY.scale(y);
  2810. rstX = [];
  2811. each(x, function (xVal) {
  2812. xVal = scaleX.scale(xVal);
  2813. obj = coord.convertPoint({
  2814. x: xVal,
  2815. y
  2816. });
  2817. if (rstY && rstY !== obj.y) {
  2818. if (!isArray(rstY)) {
  2819. rstY = [rstY];
  2820. }
  2821. rstY.push(obj.y);
  2822. } else {
  2823. rstY = obj.y;
  2824. }
  2825. rstX.push(obj.x);
  2826. });
  2827. } else {
  2828. x = scaleX.scale(x);
  2829. y = scaleY.scale(y);
  2830. var point = coord.convertPoint({
  2831. x,
  2832. y
  2833. });
  2834. rstX = point.x;
  2835. rstY = point.y;
  2836. }
  2837. return [rstX, rstY];
  2838. }
  2839. }
  2840. class Shape extends AttributeBase {
  2841. constructor(cfg) {
  2842. super(cfg);
  2843. this.names = ['shape'];
  2844. this.type = 'shape';
  2845. this.gradient = null;
  2846. }
  2847. /**
  2848. * @override
  2849. */
  2850. getLinearValue(percent) {
  2851. var values = this.values;
  2852. var index = Math.round((values.length - 1) * percent);
  2853. return values[index];
  2854. }
  2855. }
  2856. class Size extends AttributeBase {
  2857. constructor(cfg) {
  2858. super(cfg);
  2859. this.names = ['size'];
  2860. this.type = 'size';
  2861. this.gradient = null;
  2862. }
  2863. }
  2864. function getValue(start, end, percent, index) {
  2865. var value = start[index] + (end[index] - start[index]) * percent;
  2866. return value;
  2867. } // convert to hex
  2868. function arr2hex(arr) {
  2869. return '#' + toRGBValue(arr[0]) + toRGBValue(arr[1]) + toRGBValue(arr[2]);
  2870. }
  2871. function toRGBValue(value) {
  2872. value = Math.round(value);
  2873. value = value.toString(16);
  2874. if (value.length === 1) {
  2875. value = '0' + value;
  2876. }
  2877. return value;
  2878. }
  2879. function calColor(colors, percent) {
  2880. var steps = colors.length - 1;
  2881. var step = Math.floor(steps * percent);
  2882. var left = steps * percent - step;
  2883. var start = colors[step];
  2884. var end = step === steps ? start : colors[step + 1];
  2885. var rgb = arr2hex([getValue(start, end, left, 0), getValue(start, end, left, 1), getValue(start, end, left, 2)]);
  2886. return rgb;
  2887. }
  2888. function hex2arr(str) {
  2889. var arr = [];
  2890. arr.push(parseInt(str.substr(1, 2), 16));
  2891. arr.push(parseInt(str.substr(3, 2), 16));
  2892. arr.push(parseInt(str.substr(5, 2), 16));
  2893. return arr;
  2894. }
  2895. var colorCache = {
  2896. black: '#000000',
  2897. blue: '#0000ff',
  2898. grey: '#808080',
  2899. green: '#008000',
  2900. orange: '#ffa500',
  2901. pink: '#ffc0cb',
  2902. purple: '#800080',
  2903. red: '#ff0000',
  2904. white: '#ffffff',
  2905. yellow: '#ffff00'
  2906. };
  2907. /**
  2908. * Returns a hexadecimal string representing this color in RGB space, such as #f7eaba.
  2909. * @param {String} color color value
  2910. * @return {String} Returns a hexadecimal string
  2911. */
  2912. function toHex(color) {
  2913. if (colorCache[color]) {
  2914. return colorCache[color];
  2915. }
  2916. if (color[0] === '#') {
  2917. if (color.length === 7) {
  2918. return color;
  2919. }
  2920. var hex = color.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, function (m, r, g, b) {
  2921. return '#' + r + r + g + g + b + b;
  2922. }); // hex3 to hex6
  2923. colorCache[color] = hex;
  2924. return hex;
  2925. } // rgb/rgba to hex
  2926. var rst = color.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
  2927. rst.shift();
  2928. rst = arr2hex(rst);
  2929. colorCache[color] = rst;
  2930. return rst;
  2931. }
  2932. /**
  2933. * handle the gradient color
  2934. * @param {Array} colors the colors
  2935. * @return {String} return the color value
  2936. */
  2937. function gradient(colors) {
  2938. var points = [];
  2939. if (isString(colors)) {
  2940. colors = colors.split('-');
  2941. }
  2942. each(colors, function (color) {
  2943. if (color.indexOf('#') === -1) {
  2944. color = toHex(color);
  2945. }
  2946. points.push(hex2arr(color));
  2947. });
  2948. return function (percent) {
  2949. return calColor(points, percent);
  2950. };
  2951. }
  2952. class Color extends AttributeBase {
  2953. constructor(cfg) {
  2954. super(cfg);
  2955. this.names = ['color'];
  2956. this.type = 'color';
  2957. this.gradient = null;
  2958. if (isString(this.values)) {
  2959. this.linear = true;
  2960. }
  2961. }
  2962. /**
  2963. * @override
  2964. */
  2965. getLinearValue(percent) {
  2966. var gradient$1 = this.gradient;
  2967. if (!gradient$1) {
  2968. var values = this.values;
  2969. gradient$1 = gradient(values);
  2970. this.gradient = gradient$1;
  2971. }
  2972. return gradient$1(percent);
  2973. }
  2974. }
  2975. var Attr = /*#__PURE__*/Object.freeze({
  2976. __proto__: null,
  2977. Position: Position,
  2978. Shape: Shape,
  2979. Size: Size,
  2980. Color: Color
  2981. });
  2982. var Shape$1 = {};
  2983. var ShapeBase = {
  2984. _coord: null,
  2985. /**
  2986. * draw the shape
  2987. * @param {Object} cfg options
  2988. * @param {Object} container container to store the shapes
  2989. */
  2990. draw(cfg, container) {
  2991. if (this.drawShape) {
  2992. this.drawShape(cfg, container);
  2993. }
  2994. },
  2995. /**
  2996. * set the coordinate instance
  2997. * @param {Coord} coord coordinate instance
  2998. */
  2999. setCoord(coord) {
  3000. this._coord = coord;
  3001. },
  3002. /**
  3003. * convert the normalized value to the canvas position
  3004. * @param {point} point the point to convert
  3005. * @return {point} point return the result
  3006. */
  3007. parsePoint(point) {
  3008. var coord = this._coord;
  3009. if (coord.isPolar) {
  3010. if (point.x === 1) point.x = 0.9999999;
  3011. if (point.y === 1) point.y = 0.9999999;
  3012. }
  3013. return coord.convertPoint(point);
  3014. },
  3015. /**
  3016. * convert the normalized value to the canvas position
  3017. * @param {points} points the array that store the points
  3018. * @return {points} points return the result
  3019. */
  3020. parsePoints(points) {
  3021. if (!points) return false;
  3022. var self = this;
  3023. var rst = [];
  3024. points.forEach(function (point) {
  3025. rst.push(self.parsePoint(point));
  3026. });
  3027. return rst;
  3028. }
  3029. };
  3030. var ShapeFactoryBase = {
  3031. defaultShapeType: null,
  3032. setCoord(coord) {
  3033. this._coord = coord;
  3034. },
  3035. getShape(type) {
  3036. var self = this;
  3037. if (isArray(type)) {
  3038. type = type[0];
  3039. }
  3040. var shape = self[type] || self[self.defaultShapeType];
  3041. shape._coord = self._coord;
  3042. return shape;
  3043. },
  3044. getShapePoints(type, cfg) {
  3045. var shape = this.getShape(type);
  3046. var fn = shape.getPoints || shape.getShapePoints || this.getDefaultPoints;
  3047. var points = fn(cfg);
  3048. return points;
  3049. },
  3050. getDefaultPoints()
  3051. /* cfg */
  3052. {
  3053. return [];
  3054. },
  3055. drawShape(type, cfg, container) {
  3056. var shape = this.getShape(type);
  3057. if (!cfg.color) {
  3058. cfg.color = Global.colors[0];
  3059. }
  3060. return shape.draw(cfg, container);
  3061. }
  3062. };
  3063. Shape$1.registerFactory = function (factoryName, cfg) {
  3064. var className = upperFirst(factoryName);
  3065. var geomObj = mix({}, ShapeFactoryBase, cfg);
  3066. Shape$1[className] = geomObj;
  3067. geomObj.name = factoryName;
  3068. return geomObj;
  3069. };
  3070. Shape$1.registerShape = function (factoryName, shapeType, cfg) {
  3071. var className = upperFirst(factoryName);
  3072. var factory = Shape$1[className];
  3073. var shapeObj = mix({}, ShapeBase, cfg);
  3074. factory[shapeType] = shapeObj;
  3075. return shapeObj;
  3076. };
  3077. Shape$1.registShape = Shape$1.registerShape;
  3078. Shape$1.getShapeFactory = function (factoryName) {
  3079. var self = this;
  3080. factoryName = factoryName || 'point';
  3081. var className = upperFirst(factoryName);
  3082. return self[className];
  3083. };
  3084. function _mix$1(dist, obj) {
  3085. for (var key in obj) {
  3086. if (obj.hasOwnProperty(key) && key !== 'constructor' && obj[key] !== undefined) {
  3087. dist[key] = obj[key];
  3088. }
  3089. }
  3090. }
  3091. var mix$1 = function mix(dist, src1, src2, src3) {
  3092. if (src1) _mix$1(dist, src1);
  3093. if (src2) _mix$1(dist, src2);
  3094. if (src3) _mix$1(dist, src3);
  3095. return dist;
  3096. };
  3097. var mix_1 = mix$1;
  3098. var Adjust =
  3099. /*#__PURE__*/
  3100. function () {
  3101. var _proto = Adjust.prototype;
  3102. _proto._initDefaultCfg = function _initDefaultCfg() {
  3103. this.adjustNames = ['x', 'y']; // 调整的维度,默认,x,y都做调整
  3104. };
  3105. function Adjust(cfg) {
  3106. this._initDefaultCfg();
  3107. mix_1(this, cfg);
  3108. }
  3109. /**
  3110. * @override
  3111. */
  3112. _proto.processAdjust = function processAdjust()
  3113. /* dataArray */
  3114. {};
  3115. return Adjust;
  3116. }();
  3117. var base = Adjust;
  3118. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  3119. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  3120. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  3121. var GROUP_ATTRS = ['color', 'size', 'shape'];
  3122. var FIELD_ORIGIN = '_origin';
  3123. var FIELD_ORIGIN_Y = '_originY';
  3124. function parseFields(field) {
  3125. if (isArray(field)) {
  3126. return field;
  3127. }
  3128. if (isString(field)) {
  3129. return field.split('*');
  3130. }
  3131. return [field];
  3132. }
  3133. /**
  3134. * The parent class for Geometry
  3135. * @class Geom
  3136. */
  3137. class Geom extends Base {
  3138. getDefaultCfg() {
  3139. return {
  3140. /**
  3141. * geometry type
  3142. * @type {String}
  3143. */
  3144. type: null,
  3145. /**
  3146. * the data of geometry
  3147. * @type {Array}
  3148. */
  3149. data: null,
  3150. /**
  3151. * the attrs of geo,etry
  3152. * @type {Object}
  3153. */
  3154. attrs: {},
  3155. scales: {},
  3156. /**
  3157. * group for storing the shapes
  3158. * @type {Canvas}
  3159. */
  3160. container: null,
  3161. /**
  3162. * style options
  3163. * @type {Object}
  3164. */
  3165. styleOptions: null,
  3166. chart: null,
  3167. shapeType: '',
  3168. /**
  3169. * wether to generate key points for each shape
  3170. * @protected
  3171. * @type {Boolean}
  3172. */
  3173. generatePoints: false,
  3174. attrOptions: {},
  3175. sortable: false,
  3176. startOnZero: true,
  3177. visible: true,
  3178. connectNulls: false,
  3179. // 是否丢弃没有值的分组。
  3180. ignoreEmptyGroup: false,
  3181. // 是否已经初始化
  3182. isInit: false
  3183. };
  3184. }
  3185. init() {
  3186. var self = this;
  3187. var isInit = self.get('isInit');
  3188. if (isInit) {
  3189. return;
  3190. }
  3191. self._initAttrs();
  3192. self._processData();
  3193. self.set('isInit', true);
  3194. }
  3195. _getGroupScales() {
  3196. var self = this;
  3197. var scales = [];
  3198. each(GROUP_ATTRS, function (attrName) {
  3199. var attr = self.getAttr(attrName);
  3200. if (attr) {
  3201. var attrScales = attr.scales;
  3202. each(attrScales, function (scale) {
  3203. if (scale && scale.isCategory && scales.indexOf(scale) === -1) {
  3204. scales.push(scale);
  3205. }
  3206. });
  3207. }
  3208. });
  3209. return scales;
  3210. }
  3211. _groupData(data) {
  3212. var self = this;
  3213. var colDefs = self.get('colDefs');
  3214. var groupScales = self._getGroupScales();
  3215. if (groupScales.length) {
  3216. var appendConditions = {};
  3217. var names = [];
  3218. each(groupScales, function (scale) {
  3219. var field = scale.field;
  3220. names.push(field);
  3221. if (colDefs && colDefs[field] && colDefs[field].values) {
  3222. // users have defined
  3223. appendConditions[scale.field] = colDefs[field].values;
  3224. }
  3225. });
  3226. return group$1(data, names, appendConditions);
  3227. }
  3228. return [data];
  3229. }
  3230. _setAttrOptions(attrName, attrCfg) {
  3231. var options = this.get('attrOptions');
  3232. options[attrName] = attrCfg;
  3233. var attrs = this.get('attrs'); // 说明已经初始化过了
  3234. if (Object.keys(attrs).length) {
  3235. this._createAttr(attrName, attrCfg);
  3236. }
  3237. }
  3238. _createAttrOption(attrName, field, cfg, defaultValues) {
  3239. var attrCfg = {};
  3240. attrCfg.field = field;
  3241. if (cfg) {
  3242. if (isFunction(cfg)) {
  3243. attrCfg.callback = cfg;
  3244. } else {
  3245. attrCfg.values = cfg;
  3246. }
  3247. } else {
  3248. attrCfg.values = defaultValues;
  3249. }
  3250. this._setAttrOptions(attrName, attrCfg);
  3251. }
  3252. _createAttr(type, option) {
  3253. var self = this;
  3254. var attrs = self.get('attrs');
  3255. var coord = self.get('coord');
  3256. var className = upperFirst(type);
  3257. var fields = parseFields(option.field);
  3258. if (type === 'position') {
  3259. option.coord = coord;
  3260. }
  3261. var scales = [];
  3262. for (var i = 0, len = fields.length; i < len; i++) {
  3263. var field = fields[i];
  3264. var scale = self._createScale(field);
  3265. scales.push(scale);
  3266. }
  3267. if (type === 'position') {
  3268. var yScale = scales[1]; // 饼图的处理,但是还不知道为啥
  3269. if (coord.type === 'polar' && coord.transposed && self.hasAdjust('stack')) {
  3270. if (yScale.values.length) {
  3271. yScale.change({
  3272. nice: false,
  3273. min: 0,
  3274. max: Math.max.apply(null, yScale.values)
  3275. });
  3276. }
  3277. }
  3278. }
  3279. option.scales = scales;
  3280. var attr = new Attr[className](option);
  3281. attrs[type] = attr;
  3282. return attr;
  3283. }
  3284. _initAttrs() {
  3285. var self = this;
  3286. var attrOptions = self.get('attrOptions');
  3287. for (var type in attrOptions) {
  3288. if (attrOptions.hasOwnProperty(type)) {
  3289. this._createAttr(type, attrOptions[type]);
  3290. }
  3291. }
  3292. }
  3293. _createScale(field) {
  3294. var scales = this.get('scales');
  3295. var scale = scales[field];
  3296. if (!scale) {
  3297. scale = this.get('chart').createScale(field);
  3298. scales[field] = scale;
  3299. }
  3300. return scale;
  3301. }
  3302. _processData() {
  3303. var self = this;
  3304. var data = this.get('data');
  3305. var dataArray = [];
  3306. var groupedArray = this._groupData(data);
  3307. if (this.get('ignoreEmptyGroup')) {
  3308. var yScale = this.getYScale();
  3309. groupedArray = groupedArray.filter(function (group) {
  3310. return group.some(function (item) {
  3311. return typeof item[yScale.field] !== 'undefined';
  3312. });
  3313. });
  3314. }
  3315. for (var i = 0, len = groupedArray.length; i < len; i++) {
  3316. var subData = groupedArray[i];
  3317. var tempData = self._saveOrigin(subData);
  3318. if (this.hasAdjust('dodge')) {
  3319. self._numberic(tempData);
  3320. }
  3321. dataArray.push(tempData);
  3322. }
  3323. if (self.get('adjust')) {
  3324. self._adjustData(dataArray);
  3325. }
  3326. if (self.get('sortable')) {
  3327. self._sort(dataArray);
  3328. }
  3329. self.emit('afterprocessdata', {
  3330. dataArray
  3331. });
  3332. self.set('mappingData', dataArray);
  3333. self.set('dataArray', dataArray);
  3334. return dataArray;
  3335. }
  3336. _saveOrigin(data) {
  3337. var rst = [];
  3338. for (var i = 0, len = data.length; i < len; i++) {
  3339. var origin = data[i];
  3340. var obj = {};
  3341. for (var k in origin) {
  3342. obj[k] = origin[k];
  3343. }
  3344. obj[FIELD_ORIGIN] = origin;
  3345. rst.push(obj);
  3346. }
  3347. return rst;
  3348. }
  3349. _numberic(data) {
  3350. var positionAttr = this.getAttr('position');
  3351. var scales = positionAttr.scales;
  3352. for (var j = 0, len = data.length; j < len; j++) {
  3353. var obj = data[j];
  3354. var count = Math.min(2, scales.length);
  3355. for (var i = 0; i < count; i++) {
  3356. var scale = scales[i];
  3357. if (scale.isCategory) {
  3358. var field = scale.field;
  3359. obj[field] = scale.translate(obj[field]);
  3360. }
  3361. }
  3362. }
  3363. }
  3364. _adjustData(dataArray) {
  3365. var self = this;
  3366. var adjust = self.get('adjust');
  3367. if (adjust) {
  3368. var adjustType = upperFirst(adjust.type);
  3369. if (!base[adjustType]) {
  3370. throw new Error('not support such adjust : ' + adjust);
  3371. }
  3372. var xScale = self.getXScale();
  3373. var yScale = self.getYScale();
  3374. var cfg = mix({
  3375. xField: xScale.field,
  3376. yField: yScale.field
  3377. }, adjust);
  3378. var adjustObject = new base[adjustType](cfg);
  3379. adjustObject.processAdjust(dataArray);
  3380. if (adjustType === 'Stack') {
  3381. self._updateStackRange(yScale.field, yScale, dataArray);
  3382. }
  3383. }
  3384. }
  3385. _updateStackRange(field, scale, dataArray) {
  3386. var mergeArray = merge(dataArray);
  3387. var min = scale.min;
  3388. var max = scale.max;
  3389. for (var i = 0, len = mergeArray.length; i < len; i++) {
  3390. var obj = mergeArray[i];
  3391. var tmpMin = Math.min.apply(null, obj[field]);
  3392. var tmpMax = Math.max.apply(null, obj[field]);
  3393. if (tmpMin < min) {
  3394. min = tmpMin;
  3395. }
  3396. if (tmpMax > max) {
  3397. max = tmpMax;
  3398. }
  3399. }
  3400. if (min < scale.min || max > scale.max) {
  3401. scale.change({
  3402. min,
  3403. max
  3404. });
  3405. }
  3406. }
  3407. _sort(mappedArray) {
  3408. var self = this;
  3409. var xScale = self.getXScale();
  3410. var {
  3411. field,
  3412. type
  3413. } = xScale;
  3414. if (type !== 'identity' && xScale.values.length > 1) {
  3415. each(mappedArray, function (itemArr) {
  3416. itemArr.sort(function (obj1, obj2) {
  3417. if (type === 'timeCat') {
  3418. return toTimeStamp(obj1[FIELD_ORIGIN][field]) - toTimeStamp(obj2[FIELD_ORIGIN][field]);
  3419. }
  3420. return xScale.translate(obj1[FIELD_ORIGIN][field]) - xScale.translate(obj2[FIELD_ORIGIN][field]);
  3421. });
  3422. });
  3423. }
  3424. self.set('hasSorted', true);
  3425. self.set('dataArray', mappedArray);
  3426. }
  3427. paint() {
  3428. var self = this;
  3429. var dataArray = self.get('mappingData');
  3430. var mappedArray = [];
  3431. var shapeFactory = self.getShapeFactory();
  3432. shapeFactory.setCoord(self.get('coord'));
  3433. self._beforeMapping(dataArray);
  3434. for (var i = 0, len = dataArray.length; i < len; i++) {
  3435. var data = dataArray[i];
  3436. if (data.length) {
  3437. var mappedData = self._mapping(data);
  3438. mappedArray.push(mappedData);
  3439. self.draw(mappedData, shapeFactory);
  3440. }
  3441. }
  3442. self.set('dataArray', mappedArray);
  3443. }
  3444. getShapeFactory() {
  3445. var shapeFactory = this.get('shapeFactory');
  3446. if (!shapeFactory) {
  3447. var shapeType = this.get('shapeType');
  3448. shapeFactory = Shape$1.getShapeFactory(shapeType);
  3449. this.set('shapeFactory', shapeFactory);
  3450. }
  3451. return shapeFactory;
  3452. }
  3453. _mapping(data) {
  3454. var self = this;
  3455. var attrs = self.get('attrs');
  3456. var yField = self.getYScale().field; // 用来缓存转换的值,减少mapping耗时
  3457. var mappedCache = {};
  3458. var mappedData = new Array(data.length);
  3459. for (var k in attrs) {
  3460. if (attrs.hasOwnProperty(k)) {
  3461. var attr = attrs[k];
  3462. var names = attr.names;
  3463. var scales = attr.scales;
  3464. for (var i = 0, len = data.length; i < len; i++) {
  3465. var record = data[i];
  3466. var mappedRecord = _objectSpread(_objectSpread({}, record), mappedData[i]);
  3467. mappedRecord[FIELD_ORIGIN_Y] = record[yField]; // 获取视觉属性对应的value值
  3468. // 位置的缓存命中率低,还是每次单独计算
  3469. if (attr.type === 'position') {
  3470. var values = self._getAttrValues(attr, record);
  3471. for (var j = 0, _len = values.length; j < _len; j++) {
  3472. var val = values[j];
  3473. var name = names[j];
  3474. mappedRecord[name] = isArray(val) && val.length === 1 ? val[0] : val;
  3475. }
  3476. } else {
  3477. // 除了position其他都只有一项
  3478. var _name = names[0];
  3479. var field = scales[0].field;
  3480. var value = record[field];
  3481. var key = "".concat(_name).concat(value);
  3482. var _values = mappedCache[key];
  3483. if (!_values) {
  3484. _values = self._getAttrValues(attr, record);
  3485. mappedCache[key] = _values;
  3486. }
  3487. mappedRecord[_name] = _values[0];
  3488. } // 设置新数组
  3489. mappedData[i] = mappedRecord;
  3490. }
  3491. }
  3492. }
  3493. return mappedData;
  3494. }
  3495. _getAttrValues(attr, record) {
  3496. var scales = attr.scales;
  3497. var params = [];
  3498. for (var i = 0, len = scales.length; i < len; i++) {
  3499. var scale = scales[i];
  3500. var field = scale.field;
  3501. if (scale.type === 'identity') {
  3502. params.push(scale.value);
  3503. } else {
  3504. params.push(record[field]);
  3505. }
  3506. }
  3507. var values = attr.mapping(...params);
  3508. return values;
  3509. }
  3510. getAttrValue(attrName, record) {
  3511. var attr = this.getAttr(attrName);
  3512. var rst = null;
  3513. if (attr) {
  3514. var values = this._getAttrValues(attr, record);
  3515. rst = values[0];
  3516. }
  3517. return rst;
  3518. }
  3519. _beforeMapping(dataArray) {
  3520. var self = this;
  3521. if (self.get('generatePoints')) {
  3522. self._generatePoints(dataArray);
  3523. }
  3524. }
  3525. isInCircle() {
  3526. var coord = this.get('coord');
  3527. return coord && coord.isPolar;
  3528. }
  3529. getCallbackCfg(fields, cfg, origin) {
  3530. if (!fields) {
  3531. return cfg;
  3532. }
  3533. var tmpCfg = {};
  3534. var params = fields.map(function (field) {
  3535. return origin[field];
  3536. });
  3537. each(cfg, function (v, k) {
  3538. if (isFunction(v)) {
  3539. tmpCfg[k] = v.apply(null, params);
  3540. } else {
  3541. tmpCfg[k] = v;
  3542. }
  3543. });
  3544. return tmpCfg;
  3545. }
  3546. getDrawCfg(obj) {
  3547. var self = this;
  3548. var isInCircle = self.isInCircle();
  3549. var cfg = {
  3550. origin: obj,
  3551. x: obj.x,
  3552. y: obj.y,
  3553. color: obj.color,
  3554. size: obj.size,
  3555. shape: obj.shape,
  3556. isInCircle,
  3557. opacity: obj.opacity
  3558. };
  3559. var styleOptions = self.get('styleOptions');
  3560. if (styleOptions && styleOptions.style) {
  3561. cfg.style = self.getCallbackCfg(styleOptions.fields, styleOptions.style, obj[FIELD_ORIGIN]);
  3562. }
  3563. if (self.get('generatePoints')) {
  3564. cfg.points = obj.points;
  3565. cfg.nextPoints = obj.nextPoints;
  3566. }
  3567. if (isInCircle) {
  3568. cfg.center = self.get('coord').center;
  3569. }
  3570. return cfg;
  3571. }
  3572. draw(data, shapeFactory) {
  3573. var self = this;
  3574. var container = self.get('container');
  3575. var yScale = self.getYScale();
  3576. each(data, function (obj, index) {
  3577. if (yScale && isNil(obj._origin[yScale.field])) {
  3578. return;
  3579. }
  3580. obj.index = index;
  3581. var cfg = self.getDrawCfg(obj);
  3582. var shape = obj.shape;
  3583. self.drawShape(shape, obj, cfg, container, shapeFactory);
  3584. });
  3585. }
  3586. drawShape(shape, shapeData, cfg, container, shapeFactory) {
  3587. var gShape = shapeFactory.drawShape(shape, cfg, container);
  3588. if (gShape) {
  3589. each([].concat(gShape), function (s) {
  3590. s.set('origin', shapeData);
  3591. });
  3592. }
  3593. }
  3594. _generatePoints(dataArray) {
  3595. var self = this;
  3596. var shapeFactory = self.getShapeFactory();
  3597. var shapeAttr = self.getAttr('shape');
  3598. each(dataArray, function (data) {
  3599. for (var i = 0, len = data.length; i < len; i++) {
  3600. var obj = data[i];
  3601. var cfg = self.createShapePointsCfg(obj);
  3602. var shape = shapeAttr ? self._getAttrValues(shapeAttr, obj) : null;
  3603. var points = shapeFactory.getShapePoints(shape, cfg);
  3604. obj.points = points;
  3605. }
  3606. }); // 添加nextPoints
  3607. each(dataArray, function (data, index) {
  3608. var nextData = dataArray[index + 1];
  3609. if (nextData) {
  3610. data[0].nextPoints = nextData[0].points;
  3611. }
  3612. });
  3613. }
  3614. /**
  3615. * get the info of each shape
  3616. * @protected
  3617. * @param {Object} obj the data item
  3618. * @return {Object} cfg return the result
  3619. */
  3620. createShapePointsCfg(obj) {
  3621. var xScale = this.getXScale();
  3622. var yScale = this.getYScale();
  3623. var x = this._normalizeValues(obj[xScale.field], xScale);
  3624. var y;
  3625. if (yScale) {
  3626. y = this._normalizeValues(obj[yScale.field], yScale);
  3627. } else {
  3628. y = obj.y ? obj.y : 0.1;
  3629. }
  3630. return {
  3631. x,
  3632. y,
  3633. y0: yScale ? yScale.scale(this.getYMinValue()) : undefined
  3634. };
  3635. }
  3636. getYMinValue() {
  3637. var yScale = this.getYScale();
  3638. var {
  3639. min,
  3640. max
  3641. } = yScale;
  3642. var value;
  3643. if (this.get('startOnZero')) {
  3644. if (max <= 0 && min <= 0) {
  3645. value = max;
  3646. } else {
  3647. value = min >= 0 ? min : 0;
  3648. }
  3649. } else {
  3650. value = min;
  3651. }
  3652. return value;
  3653. }
  3654. _normalizeValues(values, scale) {
  3655. var rst = [];
  3656. if (isArray(values)) {
  3657. for (var i = 0, len = values.length; i < len; i++) {
  3658. var v = values[i];
  3659. rst.push(scale.scale(v));
  3660. }
  3661. } else {
  3662. rst = scale.scale(values);
  3663. }
  3664. return rst;
  3665. }
  3666. getAttr(name) {
  3667. return this.get('attrs')[name];
  3668. }
  3669. getXScale() {
  3670. return this.getAttr('position').scales[0];
  3671. }
  3672. getYScale() {
  3673. return this.getAttr('position').scales[1];
  3674. }
  3675. hasAdjust(adjust) {
  3676. return this.get('adjust') && this.get('adjust').type === adjust;
  3677. }
  3678. _getSnap(scale, item, arr) {
  3679. var i = 0;
  3680. var values;
  3681. var yField = this.getYScale().field; // 叠加的维度
  3682. if (this.hasAdjust('stack') && scale.field === yField) {
  3683. values = [];
  3684. arr.forEach(function (obj) {
  3685. values.push(obj[FIELD_ORIGIN_Y]);
  3686. });
  3687. for (var len = values.length; i < len; i++) {
  3688. if (values[0][0] > item) {
  3689. break;
  3690. }
  3691. if (values[values.length - 1][1] <= item) {
  3692. i = values.length - 1;
  3693. break;
  3694. }
  3695. if (values[i][0] <= item && values[i][1] > item) {
  3696. break;
  3697. }
  3698. }
  3699. } else {
  3700. values = scale.values;
  3701. values.sort(function (a, b) {
  3702. return a - b;
  3703. });
  3704. for (var _len2 = values.length; i < _len2; i++) {
  3705. // 如果只有1个点直接返回第1个点
  3706. if (_len2 <= 1) {
  3707. break;
  3708. } // 第1个点和第2个点之间
  3709. if ((values[0] + values[1]) / 2 > item) {
  3710. break;
  3711. } // 中间的点
  3712. if ((values[i - 1] + values[i]) / 2 <= item && (values[i + 1] + values[i]) / 2 > item) {
  3713. break;
  3714. } // 最后2个点
  3715. if ((values[values.length - 2] + values[values.length - 1]) / 2 <= item) {
  3716. i = values.length - 1;
  3717. break;
  3718. }
  3719. }
  3720. }
  3721. var result = values[i];
  3722. return result;
  3723. }
  3724. getSnapRecords(point) {
  3725. var self = this;
  3726. var coord = self.get('coord');
  3727. var xScale = self.getXScale();
  3728. var yScale = self.getYScale();
  3729. var xfield = xScale.field;
  3730. var dataArray = self.get('dataArray');
  3731. if (!this.get('hasSorted')) {
  3732. this._sort(dataArray);
  3733. }
  3734. var rst = [];
  3735. var invertPoint = coord.invertPoint(point);
  3736. var invertPointX = invertPoint.x;
  3737. if (self.isInCircle() && !coord.transposed && invertPointX > (1 + xScale.rangeMax()) / 2) {
  3738. invertPointX = xScale.rangeMin();
  3739. }
  3740. var xValue = xScale.invert(invertPointX);
  3741. if (!xScale.isCategory) {
  3742. xValue = self._getSnap(xScale, xValue);
  3743. }
  3744. var tmp = [];
  3745. dataArray.forEach(function (data) {
  3746. data.forEach(function (obj) {
  3747. var originValue = isNil(obj[FIELD_ORIGIN]) ? obj[xfield] : obj[FIELD_ORIGIN][xfield];
  3748. if (self._isEqual(originValue, xValue, xScale)) {
  3749. tmp.push(obj);
  3750. }
  3751. });
  3752. }); // special for pie chart
  3753. if (this.hasAdjust('stack') && coord.isPolar && coord.transposed) {
  3754. if (invertPointX >= 0 && invertPointX <= 1) {
  3755. var yValue = yScale.invert(invertPoint.y);
  3756. yValue = self._getSnap(yScale, yValue, tmp);
  3757. tmp.forEach(function (obj) {
  3758. if (isArray(yValue) ? obj[FIELD_ORIGIN_Y].toString() === yValue.toString() : obj[FIELD_ORIGIN_Y] === yValue) {
  3759. rst.push(obj);
  3760. }
  3761. });
  3762. }
  3763. } else {
  3764. rst = tmp;
  3765. }
  3766. return rst;
  3767. }
  3768. getRecords(value) {
  3769. var _this = this;
  3770. var xScale = this.getXScale();
  3771. var dataArray = this.get('dataArray');
  3772. var xfield = xScale.field;
  3773. return dataArray.map(function (data) {
  3774. for (var len = data.length, i = len - 1; i >= 0; i--) {
  3775. var obj = data[i];
  3776. var originValue = isNil(obj[FIELD_ORIGIN]) ? obj[xfield] : obj[FIELD_ORIGIN][xfield];
  3777. if (_this._isEqual(originValue, value, xScale)) {
  3778. return obj;
  3779. }
  3780. }
  3781. return null;
  3782. });
  3783. }
  3784. _isEqual(originValue, value, scale) {
  3785. if (scale.type === 'timeCat') {
  3786. return toTimeStamp(originValue) === value;
  3787. }
  3788. return value === originValue;
  3789. }
  3790. position(field) {
  3791. this._setAttrOptions('position', {
  3792. field
  3793. });
  3794. return this;
  3795. }
  3796. color(field, values) {
  3797. this._createAttrOption('color', field, values, Global.colors);
  3798. return this;
  3799. }
  3800. size(field, values) {
  3801. this._createAttrOption('size', field, values, Global.sizes);
  3802. return this;
  3803. }
  3804. shape(field, values) {
  3805. var type = this.get('type');
  3806. var shapes = Global.shapes[type] || [];
  3807. this._createAttrOption('shape', field, values, shapes);
  3808. return this;
  3809. }
  3810. style(field, cfg) {
  3811. var styleOptions = this.get('styleOptions');
  3812. if (!styleOptions) {
  3813. styleOptions = {};
  3814. this.set('styleOptions', styleOptions);
  3815. }
  3816. if (isObject(field)) {
  3817. cfg = field;
  3818. field = null;
  3819. }
  3820. var fields;
  3821. if (field) {
  3822. fields = parseFields(field);
  3823. }
  3824. styleOptions.fields = fields;
  3825. styleOptions.style = cfg;
  3826. return this;
  3827. }
  3828. adjust(type) {
  3829. if (isString(type)) {
  3830. type = {
  3831. type
  3832. };
  3833. }
  3834. this.set('adjust', type);
  3835. return this;
  3836. }
  3837. animate(cfg) {
  3838. this.set('animateCfg', cfg);
  3839. return this;
  3840. }
  3841. changeData(data) {
  3842. this.set('data', data); // 改变数据后,情况度量,因为需要重新实例化
  3843. this.set('scales', {});
  3844. if (!this.get('isInit')) return;
  3845. this.set('isInit', false);
  3846. this.init();
  3847. }
  3848. clearInner() {
  3849. var container = this.get('container');
  3850. if (container) {
  3851. container.clear(); // container.setMatrix([ 1, 0, 0, 1, 0, 0 ]);
  3852. }
  3853. }
  3854. reset() {
  3855. this.set('isInit', false);
  3856. this.set('attrs', {});
  3857. this.set('attrOptions', {});
  3858. this.set('adjust', null);
  3859. this.clearInner();
  3860. }
  3861. clear() {
  3862. this.clearInner();
  3863. }
  3864. destroy() {
  3865. this.set('isInit', false);
  3866. this.clear();
  3867. super.destroy();
  3868. }
  3869. _display(visible) {
  3870. this.set('visible', visible);
  3871. var container = this.get('container');
  3872. var canvas = container.get('canvas');
  3873. container.set('visible', visible);
  3874. canvas.draw();
  3875. }
  3876. show() {
  3877. this._display(true);
  3878. }
  3879. hide() {
  3880. this._display(false);
  3881. }
  3882. }
  3883. var methodCache = {};
  3884. /**
  3885. * 获取计算 ticks 的方法
  3886. * @param key 键值
  3887. * @returns 计算 ticks 的方法
  3888. */
  3889. function getTickMethod(key) {
  3890. return methodCache[key];
  3891. }
  3892. /**
  3893. * 注册计算 ticks 的方法
  3894. * @param key 键值
  3895. * @param method 方法
  3896. */
  3897. function registerTickMethod(key, method) {
  3898. methodCache[key] = method;
  3899. }
  3900. var Scale = /** @class */ (function () {
  3901. function Scale(cfg) {
  3902. /**
  3903. * 度量的类型
  3904. */
  3905. this.type = 'base';
  3906. /**
  3907. * 是否分类类型的度量
  3908. */
  3909. this.isCategory = false;
  3910. /**
  3911. * 是否线性度量,有linear, time 度量
  3912. */
  3913. this.isLinear = false;
  3914. /**
  3915. * 是否连续类型的度量,linear,time,log, pow, quantile, quantize 都支持
  3916. */
  3917. this.isContinuous = false;
  3918. /**
  3919. * 是否是常量的度量,传入和传出一致
  3920. */
  3921. this.isIdentity = false;
  3922. this.values = [];
  3923. this.range = [0, 1];
  3924. this.ticks = [];
  3925. this.__cfg__ = cfg;
  3926. this.initCfg();
  3927. this.init();
  3928. }
  3929. // 对于原始值的必要转换,如分类、时间字段需转换成数值,用transform/map命名可能更好
  3930. Scale.prototype.translate = function (v) {
  3931. return v;
  3932. };
  3933. /** 重新初始化 */
  3934. Scale.prototype.change = function (cfg) {
  3935. // 覆盖配置项,而不替代
  3936. mix(this.__cfg__, cfg);
  3937. this.init();
  3938. };
  3939. Scale.prototype.clone = function () {
  3940. return this.constructor(this.__cfg__);
  3941. };
  3942. /** 获取坐标轴需要的ticks */
  3943. Scale.prototype.getTicks = function () {
  3944. var _this = this;
  3945. return map(this.ticks, function (tick, idx) {
  3946. if (isObject(tick)) {
  3947. // 仅当符合Tick类型时才有意义
  3948. return tick;
  3949. }
  3950. return {
  3951. text: _this.getText(tick, idx),
  3952. tickValue: tick,
  3953. value: _this.scale(tick), // scaled
  3954. };
  3955. });
  3956. };
  3957. /** 获取Tick的格式化结果 */
  3958. Scale.prototype.getText = function (value, key) {
  3959. var formatter = this.formatter;
  3960. var res = formatter ? formatter(value, key) : value;
  3961. if (isNil(res) || !isFunction(res.toString)) {
  3962. return '';
  3963. }
  3964. return res.toString();
  3965. };
  3966. // 获取配置项中的值,当前 scale 上的值可能会被修改
  3967. Scale.prototype.getConfig = function (key) {
  3968. return this.__cfg__[key];
  3969. };
  3970. // scale初始化
  3971. Scale.prototype.init = function () {
  3972. mix(this, this.__cfg__);
  3973. this.setDomain();
  3974. if (isEmpty(this.getConfig('ticks'))) {
  3975. this.ticks = this.calculateTicks();
  3976. }
  3977. };
  3978. // 子类上覆盖某些属性,不能直接在类上声明,否则会被覆盖
  3979. Scale.prototype.initCfg = function () { };
  3980. Scale.prototype.setDomain = function () { };
  3981. Scale.prototype.calculateTicks = function () {
  3982. var tickMethod = this.tickMethod;
  3983. var ticks = [];
  3984. if (isString(tickMethod)) {
  3985. var method = getTickMethod(tickMethod);
  3986. if (!method) {
  3987. throw new Error('There is no method to to calculate ticks!');
  3988. }
  3989. ticks = method(this);
  3990. }
  3991. else if (isFunction(tickMethod)) {
  3992. ticks = tickMethod(this);
  3993. }
  3994. return ticks;
  3995. };
  3996. // range 的最小值
  3997. Scale.prototype.rangeMin = function () {
  3998. return this.range[0];
  3999. };
  4000. // range 的最大值
  4001. Scale.prototype.rangeMax = function () {
  4002. return this.range[1];
  4003. };
  4004. /** 定义域转 0~1 */
  4005. Scale.prototype.calcPercent = function (value, min, max) {
  4006. if (isNumber(value)) {
  4007. return (value - min) / (max - min);
  4008. }
  4009. return NaN;
  4010. };
  4011. /** 0~1转定义域 */
  4012. Scale.prototype.calcValue = function (percent, min, max) {
  4013. return min + percent * (max - min);
  4014. };
  4015. return Scale;
  4016. }());
  4017. /**
  4018. * 分类度量
  4019. * @class
  4020. */
  4021. var Category = /** @class */ (function (_super) {
  4022. __extends(Category, _super);
  4023. function Category() {
  4024. var _this = _super !== null && _super.apply(this, arguments) || this;
  4025. _this.type = 'cat';
  4026. _this.isCategory = true;
  4027. return _this;
  4028. }
  4029. Category.prototype.buildIndexMap = function () {
  4030. if (!this.translateIndexMap) {
  4031. this.translateIndexMap = new Map();
  4032. // 重新构建缓存
  4033. for (var i = 0; i < this.values.length; i++) {
  4034. this.translateIndexMap.set(this.values[i], i);
  4035. }
  4036. }
  4037. };
  4038. Category.prototype.translate = function (value) {
  4039. // 按需构建 map
  4040. this.buildIndexMap();
  4041. // 找得到
  4042. var idx = this.translateIndexMap.get(value);
  4043. if (idx === undefined) {
  4044. idx = isNumber(value) ? value : NaN;
  4045. }
  4046. return idx;
  4047. };
  4048. Category.prototype.scale = function (value) {
  4049. var order = this.translate(value);
  4050. // 分类数据允许 0.5 范围内调整
  4051. // if (order < this.min - 0.5 || order > this.max + 0.5) {
  4052. // return NaN;
  4053. // }
  4054. var percent = this.calcPercent(order, this.min, this.max);
  4055. return this.calcValue(percent, this.rangeMin(), this.rangeMax());
  4056. };
  4057. Category.prototype.invert = function (scaledValue) {
  4058. var domainRange = this.max - this.min;
  4059. var percent = this.calcPercent(scaledValue, this.rangeMin(), this.rangeMax());
  4060. var idx = Math.round(domainRange * percent) + this.min;
  4061. if (idx < this.min || idx > this.max) {
  4062. return NaN;
  4063. }
  4064. return this.values[idx];
  4065. };
  4066. Category.prototype.getText = function (value) {
  4067. var args = [];
  4068. for (var _i = 1; _i < arguments.length; _i++) {
  4069. args[_i - 1] = arguments[_i];
  4070. }
  4071. var v = value;
  4072. // value为index
  4073. if (isNumber(value) && !this.values.includes(value)) {
  4074. v = this.values[v];
  4075. }
  4076. return _super.prototype.getText.apply(this, __spreadArray([v], args, false));
  4077. };
  4078. // 复写属性
  4079. Category.prototype.initCfg = function () {
  4080. this.tickMethod = 'cat';
  4081. };
  4082. // 设置 min, max
  4083. Category.prototype.setDomain = function () {
  4084. // 用户有可能设置 min
  4085. if (isNil(this.getConfig('min'))) {
  4086. this.min = 0;
  4087. }
  4088. if (isNil(this.getConfig('max'))) {
  4089. var size = this.values.length;
  4090. this.max = size > 1 ? size - 1 : size;
  4091. }
  4092. // scale.init 的时候清除缓存
  4093. if (this.translateIndexMap) {
  4094. this.translateIndexMap = undefined;
  4095. }
  4096. };
  4097. return Category;
  4098. }(Scale));
  4099. var token = /d{1,4}|M{1,4}|YY(?:YY)?|S{1,3}|Do|ZZ|Z|([HhMsDm])\1?|[aA]|"[^"]*"|'[^']*'/g;
  4100. var twoDigitsOptional = "[1-9]\\d?";
  4101. var twoDigits = "\\d\\d";
  4102. var threeDigits = "\\d{3}";
  4103. var fourDigits = "\\d{4}";
  4104. var word = "[^\\s]+";
  4105. var literal = /\[([^]*?)\]/gm;
  4106. function shorten(arr, sLen) {
  4107. var newArr = [];
  4108. for (var i = 0, len = arr.length; i < len; i++) {
  4109. newArr.push(arr[i].substr(0, sLen));
  4110. }
  4111. return newArr;
  4112. }
  4113. var monthUpdate = function (arrName) { return function (v, i18n) {
  4114. var lowerCaseArr = i18n[arrName].map(function (v) { return v.toLowerCase(); });
  4115. var index = lowerCaseArr.indexOf(v.toLowerCase());
  4116. if (index > -1) {
  4117. return index;
  4118. }
  4119. return null;
  4120. }; };
  4121. function assign(origObj) {
  4122. var args = [];
  4123. for (var _i = 1; _i < arguments.length; _i++) {
  4124. args[_i - 1] = arguments[_i];
  4125. }
  4126. for (var _a = 0, args_1 = args; _a < args_1.length; _a++) {
  4127. var obj = args_1[_a];
  4128. for (var key in obj) {
  4129. // @ts-ignore ex
  4130. origObj[key] = obj[key];
  4131. }
  4132. }
  4133. return origObj;
  4134. }
  4135. var dayNames = [
  4136. "Sunday",
  4137. "Monday",
  4138. "Tuesday",
  4139. "Wednesday",
  4140. "Thursday",
  4141. "Friday",
  4142. "Saturday"
  4143. ];
  4144. var monthNames = [
  4145. "January",
  4146. "February",
  4147. "March",
  4148. "April",
  4149. "May",
  4150. "June",
  4151. "July",
  4152. "August",
  4153. "September",
  4154. "October",
  4155. "November",
  4156. "December"
  4157. ];
  4158. var monthNamesShort = shorten(monthNames, 3);
  4159. var dayNamesShort = shorten(dayNames, 3);
  4160. var defaultI18n = {
  4161. dayNamesShort: dayNamesShort,
  4162. dayNames: dayNames,
  4163. monthNamesShort: monthNamesShort,
  4164. monthNames: monthNames,
  4165. amPm: ["am", "pm"],
  4166. DoFn: function (dayOfMonth) {
  4167. return (dayOfMonth +
  4168. ["th", "st", "nd", "rd"][dayOfMonth % 10 > 3
  4169. ? 0
  4170. : ((dayOfMonth - (dayOfMonth % 10) !== 10 ? 1 : 0) * dayOfMonth) % 10]);
  4171. }
  4172. };
  4173. var globalI18n = assign({}, defaultI18n);
  4174. var setGlobalDateI18n = function (i18n) {
  4175. return (globalI18n = assign(globalI18n, i18n));
  4176. };
  4177. var regexEscape = function (str) {
  4178. return str.replace(/[|\\{()[^$+*?.-]/g, "\\$&");
  4179. };
  4180. var pad = function (val, len) {
  4181. if (len === void 0) { len = 2; }
  4182. val = String(val);
  4183. while (val.length < len) {
  4184. val = "0" + val;
  4185. }
  4186. return val;
  4187. };
  4188. var formatFlags = {
  4189. D: function (dateObj) { return String(dateObj.getDate()); },
  4190. DD: function (dateObj) { return pad(dateObj.getDate()); },
  4191. Do: function (dateObj, i18n) {
  4192. return i18n.DoFn(dateObj.getDate());
  4193. },
  4194. d: function (dateObj) { return String(dateObj.getDay()); },
  4195. dd: function (dateObj) { return pad(dateObj.getDay()); },
  4196. ddd: function (dateObj, i18n) {
  4197. return i18n.dayNamesShort[dateObj.getDay()];
  4198. },
  4199. dddd: function (dateObj, i18n) {
  4200. return i18n.dayNames[dateObj.getDay()];
  4201. },
  4202. M: function (dateObj) { return String(dateObj.getMonth() + 1); },
  4203. MM: function (dateObj) { return pad(dateObj.getMonth() + 1); },
  4204. MMM: function (dateObj, i18n) {
  4205. return i18n.monthNamesShort[dateObj.getMonth()];
  4206. },
  4207. MMMM: function (dateObj, i18n) {
  4208. return i18n.monthNames[dateObj.getMonth()];
  4209. },
  4210. YY: function (dateObj) {
  4211. return pad(String(dateObj.getFullYear()), 4).substr(2);
  4212. },
  4213. YYYY: function (dateObj) { return pad(dateObj.getFullYear(), 4); },
  4214. h: function (dateObj) { return String(dateObj.getHours() % 12 || 12); },
  4215. hh: function (dateObj) { return pad(dateObj.getHours() % 12 || 12); },
  4216. H: function (dateObj) { return String(dateObj.getHours()); },
  4217. HH: function (dateObj) { return pad(dateObj.getHours()); },
  4218. m: function (dateObj) { return String(dateObj.getMinutes()); },
  4219. mm: function (dateObj) { return pad(dateObj.getMinutes()); },
  4220. s: function (dateObj) { return String(dateObj.getSeconds()); },
  4221. ss: function (dateObj) { return pad(dateObj.getSeconds()); },
  4222. S: function (dateObj) {
  4223. return String(Math.round(dateObj.getMilliseconds() / 100));
  4224. },
  4225. SS: function (dateObj) {
  4226. return pad(Math.round(dateObj.getMilliseconds() / 10), 2);
  4227. },
  4228. SSS: function (dateObj) { return pad(dateObj.getMilliseconds(), 3); },
  4229. a: function (dateObj, i18n) {
  4230. return dateObj.getHours() < 12 ? i18n.amPm[0] : i18n.amPm[1];
  4231. },
  4232. A: function (dateObj, i18n) {
  4233. return dateObj.getHours() < 12
  4234. ? i18n.amPm[0].toUpperCase()
  4235. : i18n.amPm[1].toUpperCase();
  4236. },
  4237. ZZ: function (dateObj) {
  4238. var offset = dateObj.getTimezoneOffset();
  4239. return ((offset > 0 ? "-" : "+") +
  4240. pad(Math.floor(Math.abs(offset) / 60) * 100 + (Math.abs(offset) % 60), 4));
  4241. },
  4242. Z: function (dateObj) {
  4243. var offset = dateObj.getTimezoneOffset();
  4244. return ((offset > 0 ? "-" : "+") +
  4245. pad(Math.floor(Math.abs(offset) / 60), 2) +
  4246. ":" +
  4247. pad(Math.abs(offset) % 60, 2));
  4248. }
  4249. };
  4250. var monthParse = function (v) { return +v - 1; };
  4251. var emptyDigits = [null, twoDigitsOptional];
  4252. var emptyWord = [null, word];
  4253. var amPm = [
  4254. "isPm",
  4255. word,
  4256. function (v, i18n) {
  4257. var val = v.toLowerCase();
  4258. if (val === i18n.amPm[0]) {
  4259. return 0;
  4260. }
  4261. else if (val === i18n.amPm[1]) {
  4262. return 1;
  4263. }
  4264. return null;
  4265. }
  4266. ];
  4267. var timezoneOffset = [
  4268. "timezoneOffset",
  4269. "[^\\s]*?[\\+\\-]\\d\\d:?\\d\\d|[^\\s]*?Z?",
  4270. function (v) {
  4271. var parts = (v + "").match(/([+-]|\d\d)/gi);
  4272. if (parts) {
  4273. var minutes = +parts[1] * 60 + parseInt(parts[2], 10);
  4274. return parts[0] === "+" ? minutes : -minutes;
  4275. }
  4276. return 0;
  4277. }
  4278. ];
  4279. var parseFlags = {
  4280. D: ["day", twoDigitsOptional],
  4281. DD: ["day", twoDigits],
  4282. Do: ["day", twoDigitsOptional + word, function (v) { return parseInt(v, 10); }],
  4283. M: ["month", twoDigitsOptional, monthParse],
  4284. MM: ["month", twoDigits, monthParse],
  4285. YY: [
  4286. "year",
  4287. twoDigits,
  4288. function (v) {
  4289. var now = new Date();
  4290. var cent = +("" + now.getFullYear()).substr(0, 2);
  4291. return +("" + (+v > 68 ? cent - 1 : cent) + v);
  4292. }
  4293. ],
  4294. h: ["hour", twoDigitsOptional, undefined, "isPm"],
  4295. hh: ["hour", twoDigits, undefined, "isPm"],
  4296. H: ["hour", twoDigitsOptional],
  4297. HH: ["hour", twoDigits],
  4298. m: ["minute", twoDigitsOptional],
  4299. mm: ["minute", twoDigits],
  4300. s: ["second", twoDigitsOptional],
  4301. ss: ["second", twoDigits],
  4302. YYYY: ["year", fourDigits],
  4303. S: ["millisecond", "\\d", function (v) { return +v * 100; }],
  4304. SS: ["millisecond", twoDigits, function (v) { return +v * 10; }],
  4305. SSS: ["millisecond", threeDigits],
  4306. d: emptyDigits,
  4307. dd: emptyDigits,
  4308. ddd: emptyWord,
  4309. dddd: emptyWord,
  4310. MMM: ["month", word, monthUpdate("monthNamesShort")],
  4311. MMMM: ["month", word, monthUpdate("monthNames")],
  4312. a: amPm,
  4313. A: amPm,
  4314. ZZ: timezoneOffset,
  4315. Z: timezoneOffset
  4316. };
  4317. // Some common format strings
  4318. var globalMasks = {
  4319. default: "ddd MMM DD YYYY HH:mm:ss",
  4320. shortDate: "M/D/YY",
  4321. mediumDate: "MMM D, YYYY",
  4322. longDate: "MMMM D, YYYY",
  4323. fullDate: "dddd, MMMM D, YYYY",
  4324. isoDate: "YYYY-MM-DD",
  4325. isoDateTime: "YYYY-MM-DDTHH:mm:ssZ",
  4326. shortTime: "HH:mm",
  4327. mediumTime: "HH:mm:ss",
  4328. longTime: "HH:mm:ss.SSS"
  4329. };
  4330. var setGlobalDateMasks = function (masks) { return assign(globalMasks, masks); };
  4331. /***
  4332. * Format a date
  4333. * @method format
  4334. * @param {Date|number} dateObj
  4335. * @param {string} mask Format of the date, i.e. 'mm-dd-yy' or 'shortDate'
  4336. * @returns {string} Formatted date string
  4337. */
  4338. var format = function (dateObj, mask, i18n) {
  4339. if (mask === void 0) { mask = globalMasks["default"]; }
  4340. if (i18n === void 0) { i18n = {}; }
  4341. if (typeof dateObj === "number") {
  4342. dateObj = new Date(dateObj);
  4343. }
  4344. if (Object.prototype.toString.call(dateObj) !== "[object Date]" ||
  4345. isNaN(dateObj.getTime())) {
  4346. throw new Error("Invalid Date pass to format");
  4347. }
  4348. mask = globalMasks[mask] || mask;
  4349. var literals = [];
  4350. // Make literals inactive by replacing them with @@@
  4351. mask = mask.replace(literal, function ($0, $1) {
  4352. literals.push($1);
  4353. return "@@@";
  4354. });
  4355. var combinedI18nSettings = assign(assign({}, globalI18n), i18n);
  4356. // Apply formatting rules
  4357. mask = mask.replace(token, function ($0) {
  4358. return formatFlags[$0](dateObj, combinedI18nSettings);
  4359. });
  4360. // Inline literal values back into the formatted value
  4361. return mask.replace(/@@@/g, function () { return literals.shift(); });
  4362. };
  4363. /**
  4364. * Parse a date string into a Javascript Date object /
  4365. * @method parse
  4366. * @param {string} dateStr Date string
  4367. * @param {string} format Date parse format
  4368. * @param {i18n} I18nSettingsOptional Full or subset of I18N settings
  4369. * @returns {Date|null} Returns Date object. Returns null what date string is invalid or doesn't match format
  4370. */
  4371. function parse(dateStr, format, i18n) {
  4372. if (i18n === void 0) { i18n = {}; }
  4373. if (typeof format !== "string") {
  4374. throw new Error("Invalid format in fecha parse");
  4375. }
  4376. // Check to see if the format is actually a mask
  4377. format = globalMasks[format] || format;
  4378. // Avoid regular expression denial of service, fail early for really long strings
  4379. // https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS
  4380. if (dateStr.length > 1000) {
  4381. return null;
  4382. }
  4383. // Default to the beginning of the year.
  4384. var today = new Date();
  4385. var dateInfo = {
  4386. year: today.getFullYear(),
  4387. month: 0,
  4388. day: 1,
  4389. hour: 0,
  4390. minute: 0,
  4391. second: 0,
  4392. millisecond: 0,
  4393. isPm: null,
  4394. timezoneOffset: null
  4395. };
  4396. var parseInfo = [];
  4397. var literals = [];
  4398. // Replace all the literals with @@@. Hopefully a string that won't exist in the format
  4399. var newFormat = format.replace(literal, function ($0, $1) {
  4400. literals.push(regexEscape($1));
  4401. return "@@@";
  4402. });
  4403. var specifiedFields = {};
  4404. var requiredFields = {};
  4405. // Change every token that we find into the correct regex
  4406. newFormat = regexEscape(newFormat).replace(token, function ($0) {
  4407. var info = parseFlags[$0];
  4408. var field = info[0], regex = info[1], requiredField = info[3];
  4409. // Check if the person has specified the same field twice. This will lead to confusing results.
  4410. if (specifiedFields[field]) {
  4411. throw new Error("Invalid format. " + field + " specified twice in format");
  4412. }
  4413. specifiedFields[field] = true;
  4414. // Check if there are any required fields. For instance, 12 hour time requires AM/PM specified
  4415. if (requiredField) {
  4416. requiredFields[requiredField] = true;
  4417. }
  4418. parseInfo.push(info);
  4419. return "(" + regex + ")";
  4420. });
  4421. // Check all the required fields are present
  4422. Object.keys(requiredFields).forEach(function (field) {
  4423. if (!specifiedFields[field]) {
  4424. throw new Error("Invalid format. " + field + " is required in specified format");
  4425. }
  4426. });
  4427. // Add back all the literals after
  4428. newFormat = newFormat.replace(/@@@/g, function () { return literals.shift(); });
  4429. // Check if the date string matches the format. If it doesn't return null
  4430. var matches = dateStr.match(new RegExp(newFormat, "i"));
  4431. if (!matches) {
  4432. return null;
  4433. }
  4434. var combinedI18nSettings = assign(assign({}, globalI18n), i18n);
  4435. // For each match, call the parser function for that date part
  4436. for (var i = 1; i < matches.length; i++) {
  4437. var _a = parseInfo[i - 1], field = _a[0], parser = _a[2];
  4438. var value = parser
  4439. ? parser(matches[i], combinedI18nSettings)
  4440. : +matches[i];
  4441. // If the parser can't make sense of the value, return null
  4442. if (value == null) {
  4443. return null;
  4444. }
  4445. dateInfo[field] = value;
  4446. }
  4447. if (dateInfo.isPm === 1 && dateInfo.hour != null && +dateInfo.hour !== 12) {
  4448. dateInfo.hour = +dateInfo.hour + 12;
  4449. }
  4450. else if (dateInfo.isPm === 0 && +dateInfo.hour === 12) {
  4451. dateInfo.hour = 0;
  4452. }
  4453. var dateWithoutTZ = new Date(dateInfo.year, dateInfo.month, dateInfo.day, dateInfo.hour, dateInfo.minute, dateInfo.second, dateInfo.millisecond);
  4454. var validateFields = [
  4455. ["month", "getMonth"],
  4456. ["day", "getDate"],
  4457. ["hour", "getHours"],
  4458. ["minute", "getMinutes"],
  4459. ["second", "getSeconds"]
  4460. ];
  4461. for (var i = 0, len = validateFields.length; i < len; i++) {
  4462. // Check to make sure the date field is within the allowed range. Javascript dates allows values
  4463. // outside the allowed range. If the values don't match the value was invalid
  4464. if (specifiedFields[validateFields[i][0]] &&
  4465. dateInfo[validateFields[i][0]] !== dateWithoutTZ[validateFields[i][1]]()) {
  4466. return null;
  4467. }
  4468. }
  4469. if (dateInfo.timezoneOffset == null) {
  4470. return dateWithoutTZ;
  4471. }
  4472. return new Date(Date.UTC(dateInfo.year, dateInfo.month, dateInfo.day, dateInfo.hour, dateInfo.minute - dateInfo.timezoneOffset, dateInfo.second, dateInfo.millisecond));
  4473. }
  4474. var fecha = {
  4475. format: format,
  4476. parse: parse,
  4477. defaultI18n: defaultI18n,
  4478. setGlobalDateI18n: setGlobalDateI18n,
  4479. setGlobalDateMasks: setGlobalDateMasks
  4480. };
  4481. var fecha1 = /*#__PURE__*/Object.freeze({
  4482. __proto__: null,
  4483. 'default': fecha,
  4484. assign: assign,
  4485. format: format,
  4486. parse: parse,
  4487. defaultI18n: defaultI18n,
  4488. setGlobalDateI18n: setGlobalDateI18n,
  4489. setGlobalDateMasks: setGlobalDateMasks
  4490. });
  4491. /**
  4492. * 二分右侧查找
  4493. * https://github.com/d3/d3-array/blob/master/src/bisector.js
  4494. */
  4495. function bisector (getter) {
  4496. /**
  4497. * x: 目标值
  4498. * lo: 起始位置
  4499. * hi: 结束位置
  4500. */
  4501. return function (a, x, _lo, _hi) {
  4502. var lo = isNil(_lo) ? 0 : _lo;
  4503. var hi = isNil(_hi) ? a.length : _hi;
  4504. while (lo < hi) {
  4505. var mid = (lo + hi) >>> 1;
  4506. if (getter(a[mid]) > x) {
  4507. hi = mid;
  4508. }
  4509. else {
  4510. lo = mid + 1;
  4511. }
  4512. }
  4513. return lo;
  4514. };
  4515. }
  4516. var FORMAT_METHOD = 'format';
  4517. function timeFormat(time, mask) {
  4518. var method = fecha1[FORMAT_METHOD] || fecha[FORMAT_METHOD];
  4519. return method(time, mask);
  4520. }
  4521. /**
  4522. * 转换成时间戳
  4523. * @param value 时间值
  4524. */
  4525. function toTimeStamp$1(value) {
  4526. if (isString(value)) {
  4527. if (value.indexOf('T') > 0) {
  4528. value = new Date(value).getTime();
  4529. }
  4530. else {
  4531. // new Date('2010/01/10') 和 new Date('2010-01-10') 的差别在于:
  4532. // 如果仅有年月日时,前者是带有时区的: Fri Jan 10 2020 02:40:13 GMT+0800 (中国标准时间)
  4533. // 后者会格式化成 Sun Jan 10 2010 08:00:00 GMT+0800 (中国标准时间)
  4534. value = new Date(value.replace(/-/gi, '/')).getTime();
  4535. }
  4536. }
  4537. if (isDate(value)) {
  4538. value = value.getTime();
  4539. }
  4540. return value;
  4541. }
  4542. var SECOND = 1000;
  4543. var MINUTE = 60 * SECOND;
  4544. var HOUR = 60 * MINUTE;
  4545. var DAY = 24 * HOUR;
  4546. var MONTH = DAY * 31;
  4547. var YEAR = DAY * 365;
  4548. var intervals = [
  4549. ['HH:mm:ss', SECOND],
  4550. ['HH:mm:ss', SECOND * 10],
  4551. ['HH:mm:ss', SECOND * 30],
  4552. ['HH:mm', MINUTE],
  4553. ['HH:mm', MINUTE * 10],
  4554. ['HH:mm', MINUTE * 30],
  4555. ['HH', HOUR],
  4556. ['HH', HOUR * 6],
  4557. ['HH', HOUR * 12],
  4558. ['YYYY-MM-DD', DAY],
  4559. ['YYYY-MM-DD', DAY * 4],
  4560. ['YYYY-WW', DAY * 7],
  4561. ['YYYY-MM', MONTH],
  4562. ['YYYY-MM', MONTH * 4],
  4563. ['YYYY-MM', MONTH * 6],
  4564. ['YYYY', DAY * 380], // 借鉴echarts,保证每个周期累加时不会碰到恰巧不够的问题
  4565. ];
  4566. function getTickInterval(min, max, tickCount) {
  4567. var target = (max - min) / tickCount;
  4568. var idx = bisector(function (o) { return o[1]; })(intervals, target) - 1;
  4569. var interval = intervals[idx];
  4570. if (idx < 0) {
  4571. interval = intervals[0];
  4572. }
  4573. else if (idx >= intervals.length) {
  4574. interval = last(intervals);
  4575. }
  4576. return interval;
  4577. }
  4578. /**
  4579. * 时间分类度量
  4580. * @class
  4581. */
  4582. var TimeCat = /** @class */ (function (_super) {
  4583. __extends(TimeCat, _super);
  4584. function TimeCat() {
  4585. var _this = _super !== null && _super.apply(this, arguments) || this;
  4586. _this.type = 'timeCat';
  4587. return _this;
  4588. }
  4589. /**
  4590. * @override
  4591. */
  4592. TimeCat.prototype.translate = function (value) {
  4593. value = toTimeStamp$1(value);
  4594. var index = this.values.indexOf(value);
  4595. if (index === -1) {
  4596. if (isNumber(value) && value < this.values.length) {
  4597. index = value;
  4598. }
  4599. else {
  4600. index = NaN;
  4601. }
  4602. }
  4603. return index;
  4604. };
  4605. /**
  4606. * 由于时间类型数据需要转换一下,所以复写 getText
  4607. * @override
  4608. */
  4609. TimeCat.prototype.getText = function (value, tickIndex) {
  4610. var index = this.translate(value);
  4611. if (index > -1) {
  4612. var result = this.values[index];
  4613. var formatter = this.formatter;
  4614. result = formatter ? formatter(result, tickIndex) : timeFormat(result, this.mask);
  4615. return result;
  4616. }
  4617. return value;
  4618. };
  4619. TimeCat.prototype.initCfg = function () {
  4620. this.tickMethod = 'time-cat';
  4621. this.mask = 'YYYY-MM-DD';
  4622. this.tickCount = 7; // 一般时间数据会显示 7, 14, 30 天的数字
  4623. };
  4624. TimeCat.prototype.setDomain = function () {
  4625. var values = this.values;
  4626. // 针对时间分类类型,会将时间统一转换为时间戳
  4627. each(values, function (v, i) {
  4628. values[i] = toTimeStamp$1(v);
  4629. });
  4630. values.sort(function (v1, v2) {
  4631. return v1 - v2;
  4632. });
  4633. _super.prototype.setDomain.call(this);
  4634. };
  4635. return TimeCat;
  4636. }(Category));
  4637. /**
  4638. * 连续度量的基类
  4639. * @class
  4640. */
  4641. var Continuous = /** @class */ (function (_super) {
  4642. __extends(Continuous, _super);
  4643. function Continuous() {
  4644. var _this = _super !== null && _super.apply(this, arguments) || this;
  4645. _this.isContinuous = true;
  4646. return _this;
  4647. }
  4648. Continuous.prototype.scale = function (value) {
  4649. if (isNil(value)) {
  4650. return NaN;
  4651. }
  4652. var rangeMin = this.rangeMin();
  4653. var rangeMax = this.rangeMax();
  4654. var max = this.max;
  4655. var min = this.min;
  4656. if (max === min) {
  4657. return rangeMin;
  4658. }
  4659. var percent = this.getScalePercent(value);
  4660. return rangeMin + percent * (rangeMax - rangeMin);
  4661. };
  4662. Continuous.prototype.init = function () {
  4663. _super.prototype.init.call(this);
  4664. // init 完成后保证 min, max 包含 ticks 的范围
  4665. var ticks = this.ticks;
  4666. var firstTick = head(ticks);
  4667. var lastTick = last(ticks);
  4668. if (firstTick < this.min) {
  4669. this.min = firstTick;
  4670. }
  4671. if (lastTick > this.max) {
  4672. this.max = lastTick;
  4673. }
  4674. // strict-limit 方式
  4675. if (!isNil(this.minLimit)) {
  4676. this.min = firstTick;
  4677. }
  4678. if (!isNil(this.maxLimit)) {
  4679. this.max = lastTick;
  4680. }
  4681. };
  4682. Continuous.prototype.setDomain = function () {
  4683. var _a = getRange(this.values), min = _a.min, max = _a.max;
  4684. if (isNil(this.min)) {
  4685. this.min = min;
  4686. }
  4687. if (isNil(this.max)) {
  4688. this.max = max;
  4689. }
  4690. if (this.min > this.max) {
  4691. this.min = min;
  4692. this.max = max;
  4693. }
  4694. };
  4695. Continuous.prototype.calculateTicks = function () {
  4696. var _this = this;
  4697. var ticks = _super.prototype.calculateTicks.call(this);
  4698. if (!this.nice) {
  4699. ticks = filter(ticks, function (tick) {
  4700. return tick >= _this.min && tick <= _this.max;
  4701. });
  4702. }
  4703. return ticks;
  4704. };
  4705. // 计算原始值值占的百分比
  4706. Continuous.prototype.getScalePercent = function (value) {
  4707. var max = this.max;
  4708. var min = this.min;
  4709. return (value - min) / (max - min);
  4710. };
  4711. Continuous.prototype.getInvertPercent = function (value) {
  4712. return (value - this.rangeMin()) / (this.rangeMax() - this.rangeMin());
  4713. };
  4714. return Continuous;
  4715. }(Scale));
  4716. /**
  4717. * 线性度量
  4718. * @class
  4719. */
  4720. var Linear = /** @class */ (function (_super) {
  4721. __extends(Linear, _super);
  4722. function Linear() {
  4723. var _this = _super !== null && _super.apply(this, arguments) || this;
  4724. _this.type = 'linear';
  4725. _this.isLinear = true;
  4726. return _this;
  4727. }
  4728. Linear.prototype.invert = function (value) {
  4729. var percent = this.getInvertPercent(value);
  4730. return this.min + percent * (this.max - this.min);
  4731. };
  4732. Linear.prototype.initCfg = function () {
  4733. this.tickMethod = 'wilkinson-extended';
  4734. this.nice = false;
  4735. };
  4736. return Linear;
  4737. }(Continuous));
  4738. // 求以a为次幂,结果为b的基数,如 x^^a = b;求x
  4739. // 虽然数学上 b 不支持负数,但是这里需要支持 负数
  4740. function calBase(a, b) {
  4741. var e = Math.E;
  4742. var value;
  4743. if (b >= 0) {
  4744. value = Math.pow(e, Math.log(b) / a); // 使用换底公式求底
  4745. }
  4746. else {
  4747. value = Math.pow(e, Math.log(-b) / a) * -1; // 使用换底公式求底
  4748. }
  4749. return value;
  4750. }
  4751. function log(a, b) {
  4752. if (a === 1) {
  4753. return 1;
  4754. }
  4755. return Math.log(b) / Math.log(a);
  4756. }
  4757. function getLogPositiveMin(values, base, max) {
  4758. if (isNil(max)) {
  4759. max = Math.max.apply(null, values);
  4760. }
  4761. var positiveMin = max;
  4762. each(values, function (value) {
  4763. if (value > 0 && value < positiveMin) {
  4764. positiveMin = value;
  4765. }
  4766. });
  4767. if (positiveMin === max) {
  4768. positiveMin = max / base;
  4769. }
  4770. if (positiveMin > 1) {
  4771. positiveMin = 1;
  4772. }
  4773. return positiveMin;
  4774. }
  4775. /**
  4776. * Log 度量,处理非均匀分布
  4777. */
  4778. var Log = /** @class */ (function (_super) {
  4779. __extends(Log, _super);
  4780. function Log() {
  4781. var _this = _super !== null && _super.apply(this, arguments) || this;
  4782. _this.type = 'log';
  4783. return _this;
  4784. }
  4785. /**
  4786. * @override
  4787. */
  4788. Log.prototype.invert = function (value) {
  4789. var base = this.base;
  4790. var max = log(base, this.max);
  4791. var rangeMin = this.rangeMin();
  4792. var range = this.rangeMax() - rangeMin;
  4793. var min;
  4794. var positiveMin = this.positiveMin;
  4795. if (positiveMin) {
  4796. if (value === 0) {
  4797. return 0;
  4798. }
  4799. min = log(base, positiveMin / base);
  4800. var appendPercent = (1 / (max - min)) * range; // 0 到 positiveMin的占比
  4801. if (value < appendPercent) {
  4802. // 落到 0 - positiveMin 之间
  4803. return (value / appendPercent) * positiveMin;
  4804. }
  4805. }
  4806. else {
  4807. min = log(base, this.min);
  4808. }
  4809. var percent = (value - rangeMin) / range;
  4810. var tmp = percent * (max - min) + min;
  4811. return Math.pow(base, tmp);
  4812. };
  4813. Log.prototype.initCfg = function () {
  4814. this.tickMethod = 'log';
  4815. this.base = 10;
  4816. this.tickCount = 6;
  4817. this.nice = true;
  4818. };
  4819. // 设置
  4820. Log.prototype.setDomain = function () {
  4821. _super.prototype.setDomain.call(this);
  4822. var min = this.min;
  4823. if (min < 0) {
  4824. throw new Error('When you use log scale, the minimum value must be greater than zero!');
  4825. }
  4826. if (min === 0) {
  4827. this.positiveMin = getLogPositiveMin(this.values, this.base, this.max);
  4828. }
  4829. };
  4830. // 根据当前值获取占比
  4831. Log.prototype.getScalePercent = function (value) {
  4832. var max = this.max;
  4833. var min = this.min;
  4834. if (max === min) {
  4835. return 0;
  4836. }
  4837. // 如果值小于等于0,则按照0处理
  4838. if (value <= 0) {
  4839. return 0;
  4840. }
  4841. var base = this.base;
  4842. var positiveMin = this.positiveMin;
  4843. // 如果min == 0, 则根据比0大的最小值,计算比例关系。这个最小值作为坐标轴上的第二个tick,第一个是0但是不显示
  4844. if (positiveMin) {
  4845. min = (positiveMin * 1) / base;
  4846. }
  4847. var percent;
  4848. // 如果数值小于次小值,那么就计算 value / 次小值 占整体的比例
  4849. if (value < positiveMin) {
  4850. percent = value / positiveMin / (log(base, max) - log(base, min));
  4851. }
  4852. else {
  4853. percent = (log(base, value) - log(base, min)) / (log(base, max) - log(base, min));
  4854. }
  4855. return percent;
  4856. };
  4857. return Log;
  4858. }(Continuous));
  4859. /**
  4860. * Pow 度量,处理非均匀分布
  4861. */
  4862. var Pow = /** @class */ (function (_super) {
  4863. __extends(Pow, _super);
  4864. function Pow() {
  4865. var _this = _super !== null && _super.apply(this, arguments) || this;
  4866. _this.type = 'pow';
  4867. return _this;
  4868. }
  4869. /**
  4870. * @override
  4871. */
  4872. Pow.prototype.invert = function (value) {
  4873. var percent = this.getInvertPercent(value);
  4874. var exponent = this.exponent;
  4875. var max = calBase(exponent, this.max);
  4876. var min = calBase(exponent, this.min);
  4877. var tmp = percent * (max - min) + min;
  4878. var factor = tmp >= 0 ? 1 : -1;
  4879. return Math.pow(tmp, exponent) * factor;
  4880. };
  4881. Pow.prototype.initCfg = function () {
  4882. this.tickMethod = 'pow';
  4883. this.exponent = 2;
  4884. this.tickCount = 5;
  4885. this.nice = true;
  4886. };
  4887. // 获取度量计算时,value占的定义域百分比
  4888. Pow.prototype.getScalePercent = function (value) {
  4889. var max = this.max;
  4890. var min = this.min;
  4891. if (max === min) {
  4892. return 0;
  4893. }
  4894. var exponent = this.exponent;
  4895. var percent = (calBase(exponent, value) - calBase(exponent, min)) / (calBase(exponent, max) - calBase(exponent, min));
  4896. return percent;
  4897. };
  4898. return Pow;
  4899. }(Continuous));
  4900. /**
  4901. * 时间度量
  4902. * @class
  4903. */
  4904. var Time = /** @class */ (function (_super) {
  4905. __extends(Time, _super);
  4906. function Time() {
  4907. var _this = _super !== null && _super.apply(this, arguments) || this;
  4908. _this.type = 'time';
  4909. return _this;
  4910. }
  4911. /**
  4912. * @override
  4913. */
  4914. Time.prototype.getText = function (value, index) {
  4915. var numberValue = this.translate(value);
  4916. var formatter = this.formatter;
  4917. return formatter ? formatter(numberValue, index) : timeFormat(numberValue, this.mask);
  4918. };
  4919. /**
  4920. * @override
  4921. */
  4922. Time.prototype.scale = function (value) {
  4923. var v = value;
  4924. if (isString(v) || isDate(v)) {
  4925. v = this.translate(v);
  4926. }
  4927. return _super.prototype.scale.call(this, v);
  4928. };
  4929. /**
  4930. * 将时间转换成数字
  4931. * @override
  4932. */
  4933. Time.prototype.translate = function (v) {
  4934. return toTimeStamp$1(v);
  4935. };
  4936. Time.prototype.initCfg = function () {
  4937. this.tickMethod = 'time-pretty';
  4938. this.mask = 'YYYY-MM-DD';
  4939. this.tickCount = 7;
  4940. this.nice = false;
  4941. };
  4942. Time.prototype.setDomain = function () {
  4943. var values = this.values;
  4944. // 是否设置了 min, max,而不是直接取 this.min, this.max
  4945. var minConfig = this.getConfig('min');
  4946. var maxConfig = this.getConfig('max');
  4947. // 如果设置了 min,max 则转换成时间戳
  4948. if (!isNil(minConfig) || !isNumber(minConfig)) {
  4949. this.min = this.translate(this.min);
  4950. }
  4951. if (!isNil(maxConfig) || !isNumber(maxConfig)) {
  4952. this.max = this.translate(this.max);
  4953. }
  4954. // 没有设置 min, max 时
  4955. if (values && values.length) {
  4956. // 重新计算最大最小值
  4957. var timeStamps_1 = [];
  4958. var min_1 = Infinity; // 最小值
  4959. var secondMin_1 = min_1; // 次小值
  4960. var max_1 = 0;
  4961. // 使用一个循环,计算min,max,secondMin
  4962. each(values, function (v) {
  4963. var timeStamp = toTimeStamp$1(v);
  4964. if (isNaN(timeStamp)) {
  4965. throw new TypeError("Invalid Time: " + v + " in time scale!");
  4966. }
  4967. if (min_1 > timeStamp) {
  4968. secondMin_1 = min_1;
  4969. min_1 = timeStamp;
  4970. }
  4971. else if (secondMin_1 > timeStamp) {
  4972. secondMin_1 = timeStamp;
  4973. }
  4974. if (max_1 < timeStamp) {
  4975. max_1 = timeStamp;
  4976. }
  4977. timeStamps_1.push(timeStamp);
  4978. });
  4979. // 存在多个值时,设置最小间距
  4980. if (values.length > 1) {
  4981. this.minTickInterval = secondMin_1 - min_1;
  4982. }
  4983. if (isNil(minConfig)) {
  4984. this.min = min_1;
  4985. }
  4986. if (isNil(maxConfig)) {
  4987. this.max = max_1;
  4988. }
  4989. }
  4990. };
  4991. return Time;
  4992. }(Linear));
  4993. /**
  4994. * 分段度量
  4995. */
  4996. var Quantize = /** @class */ (function (_super) {
  4997. __extends(Quantize, _super);
  4998. function Quantize() {
  4999. var _this = _super !== null && _super.apply(this, arguments) || this;
  5000. _this.type = 'quantize';
  5001. return _this;
  5002. }
  5003. Quantize.prototype.invert = function (value) {
  5004. var ticks = this.ticks;
  5005. var length = ticks.length;
  5006. var percent = this.getInvertPercent(value);
  5007. var minIndex = Math.floor(percent * (length - 1));
  5008. // 最后一个
  5009. if (minIndex >= length - 1) {
  5010. return last(ticks);
  5011. }
  5012. // 超出左边界, 则取第一个
  5013. if (minIndex < 0) {
  5014. return head(ticks);
  5015. }
  5016. var minTick = ticks[minIndex];
  5017. var nextTick = ticks[minIndex + 1];
  5018. // 比当前值小的 tick 在度量上的占比
  5019. var minIndexPercent = minIndex / (length - 1);
  5020. var maxIndexPercent = (minIndex + 1) / (length - 1);
  5021. return minTick + (percent - minIndexPercent) / (maxIndexPercent - minIndexPercent) * (nextTick - minTick);
  5022. };
  5023. Quantize.prototype.initCfg = function () {
  5024. this.tickMethod = 'r-pretty';
  5025. this.tickCount = 5;
  5026. this.nice = true;
  5027. };
  5028. Quantize.prototype.calculateTicks = function () {
  5029. var ticks = _super.prototype.calculateTicks.call(this);
  5030. if (!this.nice) { // 如果 nice = false ,补充 min, max
  5031. if (last(ticks) !== this.max) {
  5032. ticks.push(this.max);
  5033. }
  5034. if (head(ticks) !== this.min) {
  5035. ticks.unshift(this.min);
  5036. }
  5037. }
  5038. return ticks;
  5039. };
  5040. // 计算当前值在刻度中的占比
  5041. Quantize.prototype.getScalePercent = function (value) {
  5042. var ticks = this.ticks;
  5043. // 超出左边界
  5044. if (value < head(ticks)) {
  5045. return 0;
  5046. }
  5047. // 超出右边界
  5048. if (value > last(ticks)) {
  5049. return 1;
  5050. }
  5051. var minIndex = 0;
  5052. each(ticks, function (tick, index) {
  5053. if (value >= tick) {
  5054. minIndex = index;
  5055. }
  5056. else {
  5057. return false;
  5058. }
  5059. });
  5060. return minIndex / (ticks.length - 1);
  5061. };
  5062. return Quantize;
  5063. }(Continuous));
  5064. var Quantile = /** @class */ (function (_super) {
  5065. __extends(Quantile, _super);
  5066. function Quantile() {
  5067. var _this = _super !== null && _super.apply(this, arguments) || this;
  5068. _this.type = 'quantile';
  5069. return _this;
  5070. }
  5071. Quantile.prototype.initCfg = function () {
  5072. this.tickMethod = 'quantile';
  5073. this.tickCount = 5;
  5074. this.nice = true;
  5075. };
  5076. return Quantile;
  5077. }(Quantize));
  5078. var map$2 = {};
  5079. function getClass(key) {
  5080. return map$2[key];
  5081. }
  5082. function registerClass(key, cls) {
  5083. if (getClass(key)) {
  5084. throw new Error("type '" + key + "' existed.");
  5085. }
  5086. map$2[key] = cls;
  5087. }
  5088. /**
  5089. * identity scale原则上是定义域和值域一致,scale/invert方法也是一致的
  5090. * 参考R的实现:https://github.com/r-lib/scales/blob/master/R/pal-identity.r
  5091. * 参考d3的实现(做了下转型):https://github.com/d3/d3-scale/blob/master/src/identity.js
  5092. */
  5093. var Identity = /** @class */ (function (_super) {
  5094. __extends(Identity, _super);
  5095. function Identity() {
  5096. var _this = _super !== null && _super.apply(this, arguments) || this;
  5097. _this.type = 'identity';
  5098. _this.isIdentity = true;
  5099. return _this;
  5100. }
  5101. Identity.prototype.calculateTicks = function () {
  5102. return this.values;
  5103. };
  5104. Identity.prototype.scale = function (value) {
  5105. // 如果传入的值不等于 identity 的值,则直接返回,用于一维图时的 dodge
  5106. if (this.values[0] !== value && isNumber(value)) {
  5107. return value;
  5108. }
  5109. return this.range[0];
  5110. };
  5111. Identity.prototype.invert = function (value) {
  5112. var range = this.range;
  5113. if (value < range[0] || value > range[1]) {
  5114. return NaN;
  5115. }
  5116. return this.values[0];
  5117. };
  5118. return Identity;
  5119. }(Scale));
  5120. // 为了解决 js 运算的精度问题
  5121. function prettyNumber(n) {
  5122. return Math.abs(n) < 1e-15 ? n : parseFloat(n.toFixed(15));
  5123. }
  5124. var DEFAULT_Q = [1, 5, 2, 2.5, 4, 3];
  5125. var eps = Number.EPSILON * 100;
  5126. function mod$1(n, m) {
  5127. return ((n % m) + m) % m;
  5128. }
  5129. function round(n) {
  5130. return Math.round(n * 1e12) / 1e12;
  5131. }
  5132. function simplicity(q, Q, j, lmin, lmax, lstep) {
  5133. var n = size(Q);
  5134. var i = indexOf$1(Q, q);
  5135. var v = 0;
  5136. var m = mod$1(lmin, lstep);
  5137. if ((m < eps || lstep - m < eps) && lmin <= 0 && lmax >= 0) {
  5138. v = 1;
  5139. }
  5140. return 1 - i / (n - 1) - j + v;
  5141. }
  5142. function simplicityMax(q, Q, j) {
  5143. var n = size(Q);
  5144. var i = indexOf$1(Q, q);
  5145. var v = 1;
  5146. return 1 - i / (n - 1) - j + v;
  5147. }
  5148. function density(k, m, dMin, dMax, lMin, lMax) {
  5149. var r = (k - 1) / (lMax - lMin);
  5150. var rt = (m - 1) / (Math.max(lMax, dMax) - Math.min(dMin, lMin));
  5151. return 2 - Math.max(r / rt, rt / r);
  5152. }
  5153. function densityMax(k, m) {
  5154. if (k >= m) {
  5155. return 2 - (k - 1) / (m - 1);
  5156. }
  5157. return 1;
  5158. }
  5159. function coverage(dMin, dMax, lMin, lMax) {
  5160. var range = dMax - dMin;
  5161. return 1 - (0.5 * (Math.pow((dMax - lMax), 2) + Math.pow((dMin - lMin), 2))) / Math.pow((0.1 * range), 2);
  5162. }
  5163. function coverageMax(dMin, dMax, span) {
  5164. var range = dMax - dMin;
  5165. if (span > range) {
  5166. var half = (span - range) / 2;
  5167. return 1 - Math.pow(half, 2) / Math.pow((0.1 * range), 2);
  5168. }
  5169. return 1;
  5170. }
  5171. function legibility() {
  5172. return 1;
  5173. }
  5174. /**
  5175. * An Extension of Wilkinson's Algorithm for Position Tick Labels on Axes
  5176. * https://www.yuque.com/preview/yuque/0/2019/pdf/185317/1546999150858-45c3b9c2-4e86-4223-bf1a-8a732e8195ed.pdf
  5177. * @param dMin 最小值
  5178. * @param dMax 最大值
  5179. * @param m tick个数
  5180. * @param onlyLoose 是否允许扩展min、max,不绝对强制,例如[3, 97]
  5181. * @param Q nice numbers集合
  5182. * @param w 四个优化组件的权重
  5183. */
  5184. function extended(dMin, dMax, n, onlyLoose, Q, w) {
  5185. if (n === void 0) { n = 5; }
  5186. if (onlyLoose === void 0) { onlyLoose = true; }
  5187. if (Q === void 0) { Q = DEFAULT_Q; }
  5188. if (w === void 0) { w = [0.25, 0.2, 0.5, 0.05]; }
  5189. // 处理小于 0 和小数的 tickCount
  5190. var m = n < 0 ? 0 : Math.round(n);
  5191. // nan 也会导致异常
  5192. if (Number.isNaN(dMin) || Number.isNaN(dMax) || typeof dMin !== 'number' || typeof dMax !== 'number' || !m) {
  5193. return {
  5194. min: 0,
  5195. max: 0,
  5196. ticks: [],
  5197. };
  5198. }
  5199. // js 极大值极小值问题,差值小于 1e-15 会导致计算出错
  5200. if (dMax - dMin < 1e-15 || m === 1) {
  5201. return {
  5202. min: dMin,
  5203. max: dMax,
  5204. ticks: [dMin],
  5205. };
  5206. }
  5207. var best = {
  5208. score: -2,
  5209. lmin: 0,
  5210. lmax: 0,
  5211. lstep: 0,
  5212. };
  5213. var j = 1;
  5214. while (j < Infinity) {
  5215. for (var i = 0; i < Q.length; i += 1) {
  5216. var q = Q[i];
  5217. var sm = simplicityMax(q, Q, j);
  5218. if (w[0] * sm + w[1] + w[2] + w[3] < best.score) {
  5219. j = Infinity;
  5220. break;
  5221. }
  5222. var k = 2;
  5223. while (k < Infinity) {
  5224. var dm = densityMax(k, m);
  5225. if (w[0] * sm + w[1] + w[2] * dm + w[3] < best.score) {
  5226. break;
  5227. }
  5228. var delta = (dMax - dMin) / (k + 1) / j / q;
  5229. var z = Math.ceil(Math.log10(delta));
  5230. while (z < Infinity) {
  5231. var step = j * q * Math.pow(10, z);
  5232. var cm = coverageMax(dMin, dMax, step * (k - 1));
  5233. if (w[0] * sm + w[1] * cm + w[2] * dm + w[3] < best.score) {
  5234. break;
  5235. }
  5236. var minStart = Math.floor(dMax / step) * j - (k - 1) * j;
  5237. var maxStart = Math.ceil(dMin / step) * j;
  5238. if (minStart <= maxStart) {
  5239. var count = maxStart - minStart;
  5240. for (var i_1 = 0; i_1 <= count; i_1 += 1) {
  5241. var start = minStart + i_1;
  5242. var lMin = start * (step / j);
  5243. var lMax = lMin + step * (k - 1);
  5244. var lStep = step;
  5245. var s = simplicity(q, Q, j, lMin, lMax, lStep);
  5246. var c = coverage(dMin, dMax, lMin, lMax);
  5247. var g = density(k, m, dMin, dMax, lMin, lMax);
  5248. var l = legibility();
  5249. var score = w[0] * s + w[1] * c + w[2] * g + w[3] * l;
  5250. if (score > best.score && (!onlyLoose || (lMin <= dMin && lMax >= dMax))) {
  5251. best.lmin = lMin;
  5252. best.lmax = lMax;
  5253. best.lstep = lStep;
  5254. best.score = score;
  5255. }
  5256. }
  5257. }
  5258. z += 1;
  5259. }
  5260. k += 1;
  5261. }
  5262. }
  5263. j += 1;
  5264. }
  5265. // 处理精度问题,保证这三个数没有精度问题
  5266. var lmax = prettyNumber(best.lmax);
  5267. var lmin = prettyNumber(best.lmin);
  5268. var lstep = prettyNumber(best.lstep);
  5269. // 加 round 是为处理 extended(0.94, 1, 5)
  5270. // 保证生成的 tickCount 没有精度问题
  5271. var tickCount = Math.floor(round((lmax - lmin) / lstep)) + 1;
  5272. var ticks = new Array(tickCount);
  5273. // 少用乘法:防止出现 -1.2 + 1.2 * 3 = 2.3999999999999995 的情况
  5274. ticks[0] = prettyNumber(lmin);
  5275. for (var i = 1; i < tickCount; i++) {
  5276. ticks[i] = prettyNumber(ticks[i - 1] + lstep);
  5277. }
  5278. return {
  5279. min: Math.min(dMin, head(ticks)),
  5280. max: Math.max(dMax, last(ticks)),
  5281. ticks: ticks,
  5282. };
  5283. }
  5284. /**
  5285. * 计算分类 ticks
  5286. * @param cfg 度量的配置项
  5287. * @returns 计算后的 ticks
  5288. */
  5289. function calculateCatTicks(cfg) {
  5290. var values = cfg.values, tickInterval = cfg.tickInterval, tickCount = cfg.tickCount;
  5291. var ticks = values;
  5292. if (isNumber(tickInterval)) {
  5293. return filter(ticks, function (__, i) { return i % tickInterval === 0; });
  5294. }
  5295. var min = cfg.min, max = cfg.max;
  5296. if (isNil(min)) {
  5297. min = 0;
  5298. }
  5299. if (isNil(max)) {
  5300. max = values.length - 1;
  5301. }
  5302. if (isNumber(tickCount) && tickCount < max - min) {
  5303. // 简单过滤,部分情况下小数的倍数也可以是整数
  5304. // tslint:disable-next-line: no-shadowed-variable
  5305. var ticks_1 = extended(min, max, tickCount, false, [1, 2, 5, 3, 4, 7, 6, 8, 9]).ticks;
  5306. var valid = filter(ticks_1, function (tick) { return tick >= min && tick <= max; });
  5307. return valid.map(function (index) { return values[index]; });
  5308. }
  5309. return values.slice(min, max + 1);
  5310. }
  5311. function d3Linear(cfg) {
  5312. var min = cfg.min, max = cfg.max, nice = cfg.nice, tickCount = cfg.tickCount;
  5313. var linear = new D3Linear();
  5314. linear.domain([min, max]);
  5315. if (nice) {
  5316. linear.nice(tickCount);
  5317. }
  5318. return linear.ticks(tickCount);
  5319. }
  5320. var DEFAULT_COUNT = 5;
  5321. var e10 = Math.sqrt(50);
  5322. var e5 = Math.sqrt(10);
  5323. var e2 = Math.sqrt(2);
  5324. // https://github.com/d3/d3-scale
  5325. var D3Linear = /** @class */ (function () {
  5326. function D3Linear() {
  5327. this._domain = [0, 1];
  5328. }
  5329. D3Linear.prototype.domain = function (domain) {
  5330. if (domain) {
  5331. this._domain = Array.from(domain, Number);
  5332. return this;
  5333. }
  5334. return this._domain.slice();
  5335. };
  5336. D3Linear.prototype.nice = function (count) {
  5337. var _a, _b;
  5338. if (count === void 0) { count = DEFAULT_COUNT; }
  5339. var d = this._domain.slice();
  5340. var i0 = 0;
  5341. var i1 = this._domain.length - 1;
  5342. var start = this._domain[i0];
  5343. var stop = this._domain[i1];
  5344. var step;
  5345. if (stop < start) {
  5346. _a = [stop, start], start = _a[0], stop = _a[1];
  5347. _b = [i1, i0], i0 = _b[0], i1 = _b[1];
  5348. }
  5349. step = tickIncrement(start, stop, count);
  5350. if (step > 0) {
  5351. start = Math.floor(start / step) * step;
  5352. stop = Math.ceil(stop / step) * step;
  5353. step = tickIncrement(start, stop, count);
  5354. }
  5355. else if (step < 0) {
  5356. start = Math.ceil(start * step) / step;
  5357. stop = Math.floor(stop * step) / step;
  5358. step = tickIncrement(start, stop, count);
  5359. }
  5360. if (step > 0) {
  5361. d[i0] = Math.floor(start / step) * step;
  5362. d[i1] = Math.ceil(stop / step) * step;
  5363. this.domain(d);
  5364. }
  5365. else if (step < 0) {
  5366. d[i0] = Math.ceil(start * step) / step;
  5367. d[i1] = Math.floor(stop * step) / step;
  5368. this.domain(d);
  5369. }
  5370. return this;
  5371. };
  5372. D3Linear.prototype.ticks = function (count) {
  5373. if (count === void 0) { count = DEFAULT_COUNT; }
  5374. return d3ArrayTicks(this._domain[0], this._domain[this._domain.length - 1], count || DEFAULT_COUNT);
  5375. };
  5376. return D3Linear;
  5377. }());
  5378. function d3ArrayTicks(start, stop, count) {
  5379. var reverse;
  5380. var i = -1;
  5381. var n;
  5382. var ticks;
  5383. var step;
  5384. (stop = +stop), (start = +start), (count = +count);
  5385. if (start === stop && count > 0) {
  5386. return [start];
  5387. }
  5388. // tslint:disable-next-line
  5389. if ((reverse = stop < start)) {
  5390. (n = start), (start = stop), (stop = n);
  5391. }
  5392. // tslint:disable-next-line
  5393. if ((step = tickIncrement(start, stop, count)) === 0 || !isFinite(step)) {
  5394. return [];
  5395. }
  5396. if (step > 0) {
  5397. start = Math.ceil(start / step);
  5398. stop = Math.floor(stop / step);
  5399. ticks = new Array((n = Math.ceil(stop - start + 1)));
  5400. while (++i < n) {
  5401. ticks[i] = (start + i) * step;
  5402. }
  5403. }
  5404. else {
  5405. start = Math.floor(start * step);
  5406. stop = Math.ceil(stop * step);
  5407. ticks = new Array((n = Math.ceil(start - stop + 1)));
  5408. while (++i < n) {
  5409. ticks[i] = (start - i) / step;
  5410. }
  5411. }
  5412. if (reverse) {
  5413. ticks.reverse();
  5414. }
  5415. return ticks;
  5416. }
  5417. function tickIncrement(start, stop, count) {
  5418. var step = (stop - start) / Math.max(0, count);
  5419. var power = Math.floor(Math.log(step) / Math.LN10);
  5420. var error = step / Math.pow(10, power);
  5421. return power >= 0
  5422. ? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(10, power)
  5423. : -Math.pow(10, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
  5424. }
  5425. function snapMultiple(v, base, snapType) {
  5426. var div;
  5427. if (snapType === 'ceil') {
  5428. div = Math.ceil(v / base);
  5429. }
  5430. else if (snapType === 'floor') {
  5431. div = Math.floor(v / base);
  5432. }
  5433. else {
  5434. div = Math.round(v / base);
  5435. }
  5436. return div * base;
  5437. }
  5438. function intervalTicks(min, max, interval) {
  5439. // 变成 interval 的倍数
  5440. var minTick = snapMultiple(min, interval, 'floor');
  5441. var maxTick = snapMultiple(max, interval, 'ceil');
  5442. // 统一小数位数
  5443. minTick = fixedBase(minTick, interval);
  5444. maxTick = fixedBase(maxTick, interval);
  5445. var ticks = [];
  5446. for (var i = minTick; i <= maxTick; i = i + interval) {
  5447. var tickValue = fixedBase(i, interval); // 防止浮点数加法出现问题
  5448. ticks.push(tickValue);
  5449. }
  5450. return {
  5451. min: minTick,
  5452. max: maxTick,
  5453. ticks: ticks
  5454. };
  5455. }
  5456. /**
  5457. * 按照给定的 minLimit/maxLimit/tickCount 均匀计算出刻度 ticks
  5458. *
  5459. * @param cfg Scale 配置项
  5460. * @return ticks
  5461. */
  5462. function strictLimit(cfg, defaultMin, defaultMax) {
  5463. var _a;
  5464. var minLimit = cfg.minLimit, maxLimit = cfg.maxLimit, min = cfg.min, max = cfg.max, _b = cfg.tickCount, tickCount = _b === void 0 ? 5 : _b;
  5465. var tickMin = isNil(minLimit) ? (isNil(defaultMin) ? min : defaultMin) : minLimit;
  5466. var tickMax = isNil(maxLimit) ? (isNil(defaultMax) ? max : defaultMax) : maxLimit;
  5467. if (tickMin > tickMax) {
  5468. _a = [tickMin, tickMax], tickMax = _a[0], tickMin = _a[1];
  5469. }
  5470. if (tickCount <= 2) {
  5471. return [tickMin, tickMax];
  5472. }
  5473. var step = (tickMax - tickMin) / (tickCount - 1);
  5474. var ticks = [];
  5475. for (var i = 0; i < tickCount; i++) {
  5476. ticks.push(tickMin + step * i);
  5477. }
  5478. return ticks;
  5479. }
  5480. function d3LinearTickMethod(cfg) {
  5481. var min = cfg.min, max = cfg.max, tickInterval = cfg.tickInterval, minLimit = cfg.minLimit, maxLimit = cfg.maxLimit;
  5482. var ticks = d3Linear(cfg);
  5483. if (!isNil(minLimit) || !isNil(maxLimit)) {
  5484. return strictLimit(cfg, head(ticks), last(ticks));
  5485. }
  5486. if (tickInterval) {
  5487. return intervalTicks(min, max, tickInterval).ticks;
  5488. }
  5489. return ticks;
  5490. }
  5491. /**
  5492. * 计算线性的 ticks,使用 wilkinson extended 方法
  5493. * @param cfg 度量的配置项
  5494. * @returns 计算后的 ticks
  5495. */
  5496. function linear(cfg) {
  5497. var min = cfg.min, max = cfg.max, tickCount = cfg.tickCount, nice = cfg.nice, tickInterval = cfg.tickInterval, minLimit = cfg.minLimit, maxLimit = cfg.maxLimit;
  5498. var ticks = extended(min, max, tickCount, nice).ticks;
  5499. if (!isNil(minLimit) || !isNil(maxLimit)) {
  5500. return strictLimit(cfg, head(ticks), last(ticks));
  5501. }
  5502. if (tickInterval) {
  5503. return intervalTicks(min, max, tickInterval).ticks;
  5504. }
  5505. return ticks;
  5506. }
  5507. /**
  5508. * 计算 log 的 ticks,考虑 min = 0 的场景
  5509. * @param cfg 度量的配置项
  5510. * @returns 计算后的 ticks
  5511. */
  5512. function calculateLogTicks(cfg) {
  5513. var base = cfg.base, tickCount = cfg.tickCount, min = cfg.min, max = cfg.max, values = cfg.values;
  5514. var minTick;
  5515. var maxTick = log(base, max);
  5516. if (min > 0) {
  5517. minTick = Math.floor(log(base, min));
  5518. }
  5519. else {
  5520. var positiveMin = getLogPositiveMin(values, base, max);
  5521. minTick = Math.floor(log(base, positiveMin));
  5522. }
  5523. var count = maxTick - minTick;
  5524. var avg = Math.ceil(count / tickCount);
  5525. var ticks = [];
  5526. for (var i = minTick; i < maxTick + avg; i = i + avg) {
  5527. ticks.push(Math.pow(base, i));
  5528. }
  5529. if (min <= 0) {
  5530. // 最小值 <= 0 时显示 0
  5531. ticks.unshift(0);
  5532. }
  5533. return ticks;
  5534. }
  5535. function pretty(min, max, m) {
  5536. if (m === void 0) { m = 5; }
  5537. if (min === max) {
  5538. return {
  5539. max: max,
  5540. min: min,
  5541. ticks: [min],
  5542. };
  5543. }
  5544. var n = m < 0 ? 0 : Math.round(m);
  5545. if (n === 0)
  5546. return { max: max, min: min, ticks: [] };
  5547. /*
  5548. R pretty:
  5549. https://svn.r-project.org/R/trunk/src/appl/pretty.c
  5550. https://www.rdocumentation.org/packages/base/versions/3.5.2/topics/pretty
  5551. */
  5552. var h = 1.5; // high.u.bias
  5553. var h5 = 0.5 + 1.5 * h; // u5.bias
  5554. // 反正我也不会调参,跳过所有判断步骤
  5555. var d = max - min;
  5556. var c = d / n;
  5557. // 当d非常小的时候触发,但似乎没什么用
  5558. // const min_n = Math.floor(n / 3);
  5559. // const shrink_sml = Math.pow(2, 5);
  5560. // if (Math.log10(d) < -2) {
  5561. // c = (_.max([ Math.abs(max), Math.abs(min) ]) * shrink_sml) / min_n;
  5562. // }
  5563. var base = Math.pow(10, Math.floor(Math.log10(c)));
  5564. var unit = base;
  5565. if (2 * base - c < h * (c - unit)) {
  5566. unit = 2 * base;
  5567. if (5 * base - c < h5 * (c - unit)) {
  5568. unit = 5 * base;
  5569. if (10 * base - c < h * (c - unit)) {
  5570. unit = 10 * base;
  5571. }
  5572. }
  5573. }
  5574. var nu = Math.ceil(max / unit);
  5575. var ns = Math.floor(min / unit);
  5576. var hi = Math.max(nu * unit, max);
  5577. var lo = Math.min(ns * unit, min);
  5578. var size = Math.floor((hi - lo) / unit) + 1;
  5579. var ticks = new Array(size);
  5580. for (var i = 0; i < size; i++) {
  5581. ticks[i] = prettyNumber(lo + i * unit);
  5582. }
  5583. return {
  5584. min: lo,
  5585. max: hi,
  5586. ticks: ticks,
  5587. };
  5588. }
  5589. /**
  5590. * 计算 Pow 的 ticks
  5591. * @param cfg 度量的配置项
  5592. * @returns 计算后的 ticks
  5593. */
  5594. function calculatePowTicks(cfg) {
  5595. var exponent = cfg.exponent, tickCount = cfg.tickCount;
  5596. var max = Math.ceil(calBase(exponent, cfg.max));
  5597. var min = Math.floor(calBase(exponent, cfg.min));
  5598. var ticks = pretty(min, max, tickCount).ticks;
  5599. return ticks.map(function (tick) {
  5600. var factor = tick >= 0 ? 1 : -1;
  5601. return Math.pow(tick, exponent) * factor;
  5602. });
  5603. }
  5604. /**
  5605. * 计算几分位 https://github.com/simple-statistics/simple-statistics/blob/master/src/quantile_sorted.js
  5606. * @param x 数组
  5607. * @param p 百分比
  5608. */
  5609. function quantileSorted(x, p) {
  5610. var idx = x.length * p;
  5611. /*if (x.length === 0) { // 当前场景这些条件不可能命中
  5612. throw new Error('quantile requires at least one value.');
  5613. } else if (p < 0 || p > 1) {
  5614. throw new Error('quantiles must be between 0 and 1');
  5615. } else */
  5616. if (p === 1) {
  5617. // If p is 1, directly return the last element
  5618. return x[x.length - 1];
  5619. }
  5620. else if (p === 0) {
  5621. // If p is 0, directly return the first element
  5622. return x[0];
  5623. }
  5624. else if (idx % 1 !== 0) {
  5625. // If p is not integer, return the next element in array
  5626. return x[Math.ceil(idx) - 1];
  5627. }
  5628. else if (x.length % 2 === 0) {
  5629. // If the list has even-length, we'll take the average of this number
  5630. // and the next value, if there is one
  5631. return (x[idx - 1] + x[idx]) / 2;
  5632. }
  5633. else {
  5634. // Finally, in the simple case of an integer value
  5635. // with an odd-length list, return the x value at the index.
  5636. return x[idx];
  5637. }
  5638. }
  5639. function calculateTicks(cfg) {
  5640. var tickCount = cfg.tickCount, values = cfg.values;
  5641. if (!values || !values.length) {
  5642. return [];
  5643. }
  5644. var sorted = values.slice().sort(function (a, b) {
  5645. return a - b;
  5646. });
  5647. var ticks = [];
  5648. for (var i = 0; i < tickCount; i++) {
  5649. var p = i / (tickCount - 1);
  5650. ticks.push(quantileSorted(sorted, p));
  5651. }
  5652. return ticks;
  5653. }
  5654. /**
  5655. * 计算线性的 ticks,使用 R's pretty 方法
  5656. * @param cfg 度量的配置项
  5657. * @returns 计算后的 ticks
  5658. */
  5659. function linearPretty(cfg) {
  5660. var min = cfg.min, max = cfg.max, tickCount = cfg.tickCount, tickInterval = cfg.tickInterval, minLimit = cfg.minLimit, maxLimit = cfg.maxLimit;
  5661. var ticks = pretty(min, max, tickCount).ticks;
  5662. if (!isNil(minLimit) || !isNil(maxLimit)) {
  5663. return strictLimit(cfg, head(ticks), last(ticks));
  5664. }
  5665. if (tickInterval) {
  5666. return intervalTicks(min, max, tickInterval).ticks;
  5667. }
  5668. return ticks;
  5669. }
  5670. function calculateTimeTicks(cfg) {
  5671. var min = cfg.min, max = cfg.max, minTickInterval = cfg.minTickInterval;
  5672. var tickInterval = cfg.tickInterval;
  5673. var tickCount = cfg.tickCount;
  5674. // 指定 tickInterval 后 tickCount 不生效,需要重新计算
  5675. if (tickInterval) {
  5676. tickCount = Math.ceil((max - min) / tickInterval);
  5677. }
  5678. else {
  5679. tickInterval = getTickInterval(min, max, tickCount)[1];
  5680. var count = (max - min) / tickInterval;
  5681. var ratio = count / tickCount;
  5682. if (ratio > 1) {
  5683. tickInterval = tickInterval * Math.ceil(ratio);
  5684. }
  5685. // 如果设置了最小间距,则使用最小间距
  5686. if (minTickInterval && tickInterval < minTickInterval) {
  5687. tickInterval = minTickInterval;
  5688. }
  5689. }
  5690. var ticks = [];
  5691. for (var i = min; i < max + tickInterval; i += tickInterval) {
  5692. ticks.push(i);
  5693. }
  5694. return ticks;
  5695. }
  5696. /**
  5697. * 计算时间分类的 ticks, 保头,保尾
  5698. * @param cfg 度量的配置项
  5699. * @returns 计算后的 ticks
  5700. */
  5701. function calculateTimeCatTicks(cfg) {
  5702. var ticks = calculateCatTicks(cfg);
  5703. var lastValue = last(cfg.values);
  5704. if (lastValue !== last(ticks)) {
  5705. ticks.push(lastValue);
  5706. }
  5707. return ticks;
  5708. }
  5709. function getYear(date) {
  5710. return new Date(date).getFullYear();
  5711. }
  5712. function createYear(year) {
  5713. return new Date(year, 0, 1).getTime();
  5714. }
  5715. function getMonth(date) {
  5716. return new Date(date).getMonth();
  5717. }
  5718. function diffMonth(min, max) {
  5719. var minYear = getYear(min);
  5720. var maxYear = getYear(max);
  5721. var minMonth = getMonth(min);
  5722. var maxMonth = getMonth(max);
  5723. return (maxYear - minYear) * 12 + ((maxMonth - minMonth) % 12);
  5724. }
  5725. function creatMonth(year, month) {
  5726. return new Date(year, month, 1).getTime();
  5727. }
  5728. function diffDay(min, max) {
  5729. return Math.ceil((max - min) / DAY);
  5730. }
  5731. function diffHour(min, max) {
  5732. return Math.ceil((max - min) / HOUR);
  5733. }
  5734. function diffMinus(min, max) {
  5735. return Math.ceil((max - min) / (60 * 1000));
  5736. }
  5737. /**
  5738. * 计算 time 的 ticks,对 month, year 进行 pretty 处理
  5739. * @param cfg 度量的配置项
  5740. * @returns 计算后的 ticks
  5741. */
  5742. function timePretty(cfg) {
  5743. var min = cfg.min, max = cfg.max, minTickInterval = cfg.minTickInterval, tickCount = cfg.tickCount;
  5744. var tickInterval = cfg.tickInterval;
  5745. var ticks = [];
  5746. // 指定 tickInterval 后 tickCount 不生效,需要重新计算
  5747. if (!tickInterval) {
  5748. tickInterval = (max - min) / tickCount;
  5749. // 如果设置了最小间距,则使用最小间距
  5750. if (minTickInterval && tickInterval < minTickInterval) {
  5751. tickInterval = minTickInterval;
  5752. }
  5753. }
  5754. var minYear = getYear(min);
  5755. // 如果间距大于 1 年,则将开始日期从整年开始
  5756. if (tickInterval > YEAR) {
  5757. var maxYear = getYear(max);
  5758. var yearInterval = Math.ceil(tickInterval / YEAR);
  5759. for (var i = minYear; i <= maxYear + yearInterval; i = i + yearInterval) {
  5760. ticks.push(createYear(i));
  5761. }
  5762. }
  5763. else if (tickInterval > MONTH) {
  5764. // 大于月时
  5765. var monthInterval = Math.ceil(tickInterval / MONTH);
  5766. var mmMoth = getMonth(min);
  5767. var dMonths = diffMonth(min, max);
  5768. for (var i = 0; i <= dMonths + monthInterval; i = i + monthInterval) {
  5769. ticks.push(creatMonth(minYear, i + mmMoth));
  5770. }
  5771. }
  5772. else if (tickInterval > DAY) {
  5773. // 大于天
  5774. var date = new Date(min);
  5775. var year = date.getFullYear();
  5776. var month = date.getMonth();
  5777. var mday = date.getDate();
  5778. var day = Math.ceil(tickInterval / DAY);
  5779. var ddays = diffDay(min, max);
  5780. for (var i = 0; i < ddays + day; i = i + day) {
  5781. ticks.push(new Date(year, month, mday + i).getTime());
  5782. }
  5783. }
  5784. else if (tickInterval > HOUR) {
  5785. // 大于小时
  5786. var date = new Date(min);
  5787. var year = date.getFullYear();
  5788. var month = date.getMonth();
  5789. var day = date.getDate();
  5790. var hour = date.getHours();
  5791. var hours = Math.ceil(tickInterval / HOUR);
  5792. var dHours = diffHour(min, max);
  5793. for (var i = 0; i <= dHours + hours; i = i + hours) {
  5794. ticks.push(new Date(year, month, day, hour + i).getTime());
  5795. }
  5796. }
  5797. else if (tickInterval > MINUTE) {
  5798. // 大于分钟
  5799. var dMinus = diffMinus(min, max);
  5800. var minutes = Math.ceil(tickInterval / MINUTE);
  5801. for (var i = 0; i <= dMinus + minutes; i = i + minutes) {
  5802. ticks.push(min + i * MINUTE);
  5803. }
  5804. }
  5805. else {
  5806. // 小于分钟
  5807. var interval = tickInterval;
  5808. if (interval < SECOND) {
  5809. interval = SECOND;
  5810. }
  5811. var minSecond = Math.floor(min / SECOND) * SECOND;
  5812. var dSeconds = Math.ceil((max - min) / SECOND);
  5813. var seconds = Math.ceil(interval / SECOND);
  5814. for (var i = 0; i < dSeconds + seconds; i = i + seconds) {
  5815. ticks.push(minSecond + i * SECOND);
  5816. }
  5817. }
  5818. // 最好是能从算法能解决这个问题,但是如果指定了 tickInterval,计算 ticks,也只能这么算,所以
  5819. // 打印警告提示
  5820. if (ticks.length >= 512) {
  5821. console.warn("Notice: current ticks length(" + ticks.length + ") >= 512, may cause performance issues, even out of memory. Because of the configure \"tickInterval\"(in milliseconds, current is " + tickInterval + ") is too small, increase the value to solve the problem!");
  5822. }
  5823. return ticks;
  5824. }
  5825. registerTickMethod('cat', calculateCatTicks);
  5826. registerTickMethod('time-cat', calculateTimeCatTicks);
  5827. registerTickMethod('wilkinson-extended', linear);
  5828. registerTickMethod('r-pretty', linearPretty);
  5829. registerTickMethod('time', calculateTimeTicks);
  5830. registerTickMethod('time-pretty', timePretty);
  5831. registerTickMethod('log', calculateLogTicks);
  5832. registerTickMethod('pow', calculatePowTicks);
  5833. registerTickMethod('quantile', calculateTicks);
  5834. registerTickMethod('d3-linear', d3LinearTickMethod);
  5835. registerClass('cat', Category);
  5836. registerClass('category', Category);
  5837. registerClass('identity', Identity);
  5838. registerClass('linear', Linear);
  5839. registerClass('log', Log);
  5840. registerClass('pow', Pow);
  5841. registerClass('time', Time);
  5842. registerClass('timeCat', TimeCat);
  5843. registerClass('quantize', Quantize);
  5844. registerClass('quantile', Quantile);
  5845. // cat平均算法,保头保尾
  5846. var CatTick = (function (cfg) {
  5847. var {
  5848. values,
  5849. tickCount
  5850. } = cfg;
  5851. if (!tickCount) {
  5852. return values;
  5853. }
  5854. if (values.length <= 1) {
  5855. return values;
  5856. } // 获取间隔步长, 最小是1
  5857. var step = parseInt(values.length / (tickCount - 1)) || 1;
  5858. var ticks = []; // 按间隔数取对应节点
  5859. for (var index = 0; index < values.length; index = index + step) {
  5860. ticks.push(values[index]);
  5861. }
  5862. var last = values[values.length - 1]; // 如果最后一个tick不等于原数据的最后一个
  5863. if (ticks[ticks.length - 1] !== last) {
  5864. if (ticks.length >= tickCount) {
  5865. // 如果当前的tick个数满足要求
  5866. ticks[ticks.length - 1] = last;
  5867. } else {
  5868. // 不满足tickCount则直接加入最后一个
  5869. ticks.push(last);
  5870. }
  5871. }
  5872. return ticks;
  5873. });
  5874. // 认为是nice的刻度
  5875. var SNAP_COUNT_ARRAY = [1, 1.2, 1.5, 2, 2.2, 2.4, 2.5, 3, 4, 5, 6, 7.5, 8, 10];
  5876. var DEFAULT_COUNT$1 = 5; // 默认刻度值
  5877. var LinearTick = (function (cfg) {
  5878. var {
  5879. tickCount,
  5880. tickInterval
  5881. } = cfg || {};
  5882. var {
  5883. min,
  5884. max
  5885. } = cfg || {};
  5886. min = isNaN(min) ? 0 : min;
  5887. max = isNaN(max) ? 0 : max;
  5888. var count = tickCount && tickCount >= 2 ? tickCount : DEFAULT_COUNT$1; // 计算interval, 优先取tickInterval
  5889. var interval = tickInterval || getBestInterval({
  5890. tickCount: count,
  5891. max,
  5892. min
  5893. }); // 通过interval计算最小tick
  5894. var minTick = Math.floor(min / interval) * interval; // 如果指定了tickInterval, count 需要根据指定的tickInterval来算计
  5895. if (tickInterval) {
  5896. var intervalCount = Math.abs(Math.ceil((max - minTick) / tickInterval)) + 1; // tickCount 作为最小 count 处理
  5897. count = Math.max(count, intervalCount);
  5898. }
  5899. var ticks = [];
  5900. var tickLength = 0;
  5901. var fixedLength = getFixedLength(interval);
  5902. while (tickLength < count) {
  5903. ticks.push(toFixed(minTick + tickLength * interval, fixedLength));
  5904. tickLength++;
  5905. }
  5906. return ticks;
  5907. });
  5908. var DECIMAL_LENGTH = 12;
  5909. function getFactor(number) {
  5910. // 取正数
  5911. number = Math.abs(number);
  5912. var factor = 1;
  5913. if (number === 0) {
  5914. return factor;
  5915. } // 小于1,逐渐放大
  5916. if (number < 1) {
  5917. var count = 0;
  5918. while (number < 1) {
  5919. factor = factor / 10;
  5920. number = number * 10;
  5921. count++;
  5922. } // 浮点数计算出现问题
  5923. if (factor.toString().length > DECIMAL_LENGTH) {
  5924. factor = parseFloat(factor.toFixed(count));
  5925. }
  5926. return factor;
  5927. } // 大于10逐渐缩小
  5928. while (number > 10) {
  5929. factor = factor * 10;
  5930. number = number / 10;
  5931. }
  5932. return factor;
  5933. } // 获取最佳匹配刻度
  5934. function getBestInterval(_ref) {
  5935. var {
  5936. tickCount,
  5937. min,
  5938. max
  5939. } = _ref;
  5940. // 如果最大最小相等,则直接按1处理
  5941. if (min === max) {
  5942. return 1 * getFactor(max);
  5943. } // 1.计算平均刻度间隔
  5944. var avgInterval = (max - min) / (tickCount - 1); // 2.数据标准归一化 映射到[1-10]区间
  5945. var factor = getFactor(avgInterval);
  5946. var calInterval = avgInterval / factor;
  5947. var calMax = max / factor;
  5948. var calMin = min / factor; // 根据平均值推算最逼近刻度值
  5949. var similarityIndex = 0;
  5950. for (var index = 0; index < SNAP_COUNT_ARRAY.length; index++) {
  5951. var item = SNAP_COUNT_ARRAY[index];
  5952. if (calInterval <= item) {
  5953. similarityIndex = index;
  5954. break;
  5955. }
  5956. }
  5957. var similarityInterval = getInterval(similarityIndex, tickCount, calMin, calMax); // 小数点位数还原到数据的位数, 因为similarityIndex有可能是小数,所以需要保留similarityIndex自己的小数位数
  5958. var fixedLength = getFixedLength(similarityInterval) + getFixedLength(factor);
  5959. return toFixed(similarityInterval * factor, fixedLength);
  5960. }
  5961. function getInterval(startIndex, tickCount, min, max) {
  5962. var verify = false;
  5963. var interval = SNAP_COUNT_ARRAY[startIndex]; // 刻度值校验,如果不满足,循环下去
  5964. for (var i = startIndex; i < SNAP_COUNT_ARRAY.length; i++) {
  5965. if (intervalIsVerify({
  5966. interval: SNAP_COUNT_ARRAY[i],
  5967. tickCount,
  5968. max,
  5969. min
  5970. })) {
  5971. // 有符合条件的interval
  5972. interval = SNAP_COUNT_ARRAY[i];
  5973. verify = true;
  5974. break;
  5975. }
  5976. } // 如果不满足, 依次缩小10倍,再计算
  5977. if (!verify) {
  5978. return 10 * getInterval(0, tickCount, min / 10, max / 10);
  5979. }
  5980. return interval;
  5981. } // 刻度是否满足展示需求
  5982. function intervalIsVerify(_ref2) {
  5983. var {
  5984. interval,
  5985. tickCount,
  5986. max,
  5987. min
  5988. } = _ref2;
  5989. var minTick = Math.floor(min / interval) * interval;
  5990. if (minTick + (tickCount - 1) * interval >= max) {
  5991. return true;
  5992. }
  5993. return false;
  5994. } // 计算小数点应该保留的位数
  5995. function getFixedLength(num) {
  5996. var str = num.toString();
  5997. var index = str.indexOf('.');
  5998. var indexOfExp = str.indexOf('e-');
  5999. var length = indexOfExp >= 0 ? parseInt(str.substr(indexOfExp + 2), 10) : str.substr(index + 1).length;
  6000. if (length > 20) {
  6001. // 最多保留20位小数
  6002. length = 20;
  6003. }
  6004. return length;
  6005. } // @antv/util fixedbase不支持科学计数法的判断,需要提mr
  6006. function toFixed(v, length) {
  6007. return parseFloat(v.toFixed(length));
  6008. }
  6009. var Linear$1 = getClass('linear');
  6010. var Identity$1 = getClass('identity');
  6011. var Category$1 = getClass('category');
  6012. var TimeCat$1 = getClass('timeCat'); // 覆盖0.3.x的 cat 方法
  6013. registerTickMethod('cat', CatTick);
  6014. registerTickMethod('time-cat', CatTick); // 覆盖linear 度量的tick算法
  6015. registerTickMethod('wilkinson-extended', LinearTick);
  6016. Scale.Linear = Linear$1;
  6017. Scale.Identity = Identity$1;
  6018. Scale.Category = Category$1;
  6019. Scale.Cat = Category$1;
  6020. Scale.TimeCat = TimeCat$1;
  6021. function isFullCircle(coord) {
  6022. if (!coord.isPolar) {
  6023. return false;
  6024. }
  6025. var startAngle = coord.startAngle;
  6026. var endAngle = coord.endAngle;
  6027. if (!isNil(startAngle) && !isNil(endAngle) && endAngle - startAngle < Math.PI * 2) {
  6028. return false;
  6029. }
  6030. return true;
  6031. }
  6032. function clearObj(obj) {
  6033. Object.keys(obj).forEach(function (key) {
  6034. delete obj[key];
  6035. });
  6036. }
  6037. class ScaleController {
  6038. constructor(cfg) {
  6039. // defs 列定义
  6040. this.defs = {}; // 已经实例化的scale
  6041. this.scales = {};
  6042. mix(this, cfg);
  6043. }
  6044. setFieldDef(field, cfg) {
  6045. var {
  6046. defs
  6047. } = this;
  6048. if (isObject(field)) {
  6049. mix(defs, field);
  6050. } else {
  6051. defs[field] = cfg;
  6052. } // 因为可能同时变更多个scale,所以要把所有已实例化的scale都更新下
  6053. this.updateScales();
  6054. }
  6055. _getDef(field) {
  6056. var defs = this.defs;
  6057. var def = null;
  6058. if (Global.scales[field] || defs[field]) {
  6059. def = mix({}, Global.scales[field]);
  6060. each(defs[field], function (v, k) {
  6061. if (isNil(v)) {
  6062. delete def[k];
  6063. } else {
  6064. def[k] = v;
  6065. }
  6066. });
  6067. }
  6068. return def;
  6069. }
  6070. _getDefaultType(field, data, def) {
  6071. if (def && def.type) {
  6072. return def.type;
  6073. }
  6074. var type = 'linear';
  6075. var value = firstValue$1(data, field);
  6076. if (isArray(value)) {
  6077. value = value[0];
  6078. }
  6079. if (isString(value)) {
  6080. type = 'cat';
  6081. }
  6082. return type;
  6083. }
  6084. _getScaleDef(type, field, data, def) {
  6085. var values;
  6086. if (def && def.values) {
  6087. values = def.values;
  6088. } else {
  6089. values = values$1(data, field);
  6090. }
  6091. var cfg = {
  6092. field,
  6093. values
  6094. };
  6095. if (type !== 'cat' && type !== 'timeCat') {
  6096. if (!def || !(def.min && def.max)) {
  6097. var {
  6098. min,
  6099. max
  6100. } = getRange$1(values);
  6101. cfg.min = min;
  6102. cfg.max = max;
  6103. cfg.nice = true;
  6104. }
  6105. } else {
  6106. cfg.isRounding = false; // used for tickCount calculation
  6107. }
  6108. return cfg;
  6109. } // 调整range,为了让图形居中
  6110. _adjustRange(type, cfg) {
  6111. var {
  6112. range,
  6113. values
  6114. } = cfg; // 如果是线性, 或者有自定义range都不处理
  6115. if (type === 'linear' || range || !values) {
  6116. return cfg;
  6117. }
  6118. var count = values.length; // 单只有一条数据时,在中间显示
  6119. if (count === 1) {
  6120. cfg.range = [0.5, 1];
  6121. } else {
  6122. var {
  6123. chart
  6124. } = this;
  6125. var coord = chart.get('coord');
  6126. var widthRatio = Global.widthRatio.multiplePie;
  6127. var offset = 0;
  6128. if (isFullCircle(coord)) {
  6129. if (!coord.transposed) {
  6130. cfg.range = [0, 1 - 1 / count];
  6131. } else {
  6132. offset = 1 / count * widthRatio;
  6133. cfg.range = [offset / 2, 1 - offset / 2];
  6134. }
  6135. } else {
  6136. // 为了让图形居中,所以才设置range
  6137. offset = 1 / count * 0.5; // 这里可能用0.25会更合理
  6138. cfg.range = [offset, 1 - offset];
  6139. }
  6140. }
  6141. return cfg;
  6142. }
  6143. _getScaleCfg(field, data) {
  6144. var self = this;
  6145. var def = self._getDef(field);
  6146. if (!data || !data.length) {
  6147. if (def && def.type) {
  6148. def.field = field;
  6149. return {
  6150. type: def.type,
  6151. cfg: def
  6152. };
  6153. }
  6154. return {
  6155. type: 'identity',
  6156. cfg: {
  6157. value: field,
  6158. field: field.toString(),
  6159. values: [field]
  6160. }
  6161. };
  6162. }
  6163. var firstObj = data[0];
  6164. var firstValue = firstObj[field];
  6165. if (firstValue === null) {
  6166. firstValue = firstValue$1(data, field);
  6167. }
  6168. if (isNumber(field) || isNil(firstValue) && !def) {
  6169. return {
  6170. type: 'identity',
  6171. cfg: {
  6172. value: field,
  6173. field: field.toString(),
  6174. values: [field]
  6175. }
  6176. };
  6177. }
  6178. var type = self._getDefaultType(field, data, def);
  6179. var cfg = self._getScaleDef(type, field, data, def);
  6180. def && mix(cfg, def);
  6181. cfg = this._adjustRange(type, cfg);
  6182. return {
  6183. type,
  6184. cfg
  6185. };
  6186. }
  6187. createScale(field, data) {
  6188. var {
  6189. scales
  6190. } = this;
  6191. var {
  6192. type,
  6193. cfg
  6194. } = this._getScaleCfg(field, data);
  6195. var scale = scales[field]; // 如果已经存在,且类型相等时直接返回
  6196. if (scale && scale.type === type) {
  6197. scale.change(cfg);
  6198. return scale;
  6199. }
  6200. var Scale = getClass(type);
  6201. var newScale = new Scale(cfg);
  6202. scales[field] = newScale;
  6203. return newScale;
  6204. }
  6205. _updateScale(scale) {
  6206. var {
  6207. field
  6208. } = scale; // 因为每个field的数据都会不同
  6209. var data = this.chart._getScaleData(field);
  6210. var {
  6211. cfg
  6212. } = this._getScaleCfg(field, data);
  6213. scale.change(cfg);
  6214. }
  6215. updateScales() {
  6216. var _this = this;
  6217. var {
  6218. scales
  6219. } = this; // 修改完列定义后,需要更新已经实例化的scale
  6220. // 如果是还没有实例化的,在geom初始化的时候会被实例化,所以这里可以不用更新
  6221. each(scales, function (scale) {
  6222. _this._updateScale(scale);
  6223. });
  6224. } // 调整scale从0开始
  6225. adjustStartZero(scale) {
  6226. var {
  6227. defs
  6228. } = this;
  6229. var {
  6230. field,
  6231. min,
  6232. max
  6233. } = scale; // 如果有定义,则不处理
  6234. if (defs[field] && defs[field].min) {
  6235. return;
  6236. }
  6237. if (min > 0) {
  6238. scale.change({
  6239. min: 0
  6240. });
  6241. } else if (max < 0) {
  6242. scale.change({
  6243. max: 0
  6244. });
  6245. }
  6246. }
  6247. clear() {
  6248. // this.defs = {};
  6249. // this.scales = {};
  6250. clearObj(this.defs);
  6251. clearObj(this.scales);
  6252. this.data = null;
  6253. }
  6254. }
  6255. class Abastract {
  6256. _initDefaultCfg() {
  6257. /**
  6258. * ticks
  6259. * @type {Array}
  6260. */
  6261. this.ticks = [];
  6262. /**
  6263. * the configuration for tickLine
  6264. * @type {Object}
  6265. */
  6266. this.tickLine = {};
  6267. /**
  6268. * the direction of ticks, 1 means clockwise
  6269. * @type {Number}
  6270. */
  6271. this.offsetFactor = 1;
  6272. /**
  6273. * the top container
  6274. * @type {container}
  6275. */
  6276. this.frontContainer = null;
  6277. /**
  6278. * the back container
  6279. * @type {[type]}
  6280. */
  6281. this.backContainer = null;
  6282. /**
  6283. * points for draw grid line
  6284. * @type {Array}
  6285. */
  6286. this.gridPoints = [];
  6287. }
  6288. constructor(cfg) {
  6289. this._initDefaultCfg();
  6290. mix(this, cfg);
  6291. this.draw();
  6292. }
  6293. draw() {
  6294. var {
  6295. line,
  6296. tickLine,
  6297. label,
  6298. grid
  6299. } = this;
  6300. grid && this.drawGrid(grid); // draw the grid lines
  6301. tickLine && this.drawTicks(tickLine); // draw the tickLine
  6302. line && this.drawLine(line); // draw axis line
  6303. label && this.drawLabels(); // draw ticks
  6304. }
  6305. drawTicks(tickCfg) {
  6306. var self = this;
  6307. var ticks = self.ticks;
  6308. var length = tickCfg.length;
  6309. var container = self.getContainer(tickCfg.top);
  6310. each(ticks, function (tick) {
  6311. var start = self.getOffsetPoint(tick.value);
  6312. var end = self.getSidePoint(start, length);
  6313. var shape = container.addShape('line', {
  6314. className: 'axis-tick',
  6315. attrs: mix({
  6316. x1: start.x,
  6317. y1: start.y,
  6318. x2: end.x,
  6319. y2: end.y
  6320. }, tickCfg)
  6321. });
  6322. shape._id = self._id + '-ticks';
  6323. });
  6324. }
  6325. drawLabels() {
  6326. var self = this;
  6327. var labelOffset = self.labelOffset;
  6328. var labels = self.labels;
  6329. each(labels, function (labelShape) {
  6330. var container = self.getContainer(labelShape.get('top'));
  6331. var start = self.getOffsetPoint(labelShape.get('value'));
  6332. var {
  6333. x,
  6334. y
  6335. } = self.getSidePoint(start, labelOffset);
  6336. labelShape.attr(mix({
  6337. x,
  6338. y
  6339. }, self.getTextAlignInfo(start, labelOffset), labelShape.get('textStyle')));
  6340. labelShape._id = self._id + '-' + labelShape.attr('text');
  6341. container.add(labelShape);
  6342. });
  6343. }
  6344. drawLine() {}
  6345. drawGrid(grid) {
  6346. var self = this;
  6347. var {
  6348. gridPoints,
  6349. ticks
  6350. } = self;
  6351. var gridCfg = grid;
  6352. var count = gridPoints.length;
  6353. each(gridPoints, function (subPoints, index) {
  6354. if (isFunction(grid)) {
  6355. var tick = ticks[index] || {};
  6356. var executedGrid = grid(tick.text, index, count);
  6357. gridCfg = executedGrid ? mix({}, Global._defaultAxis.grid, executedGrid) : null;
  6358. }
  6359. if (gridCfg) {
  6360. var type = gridCfg.type; // has two types: 'line' and 'arc'
  6361. var points = subPoints.points;
  6362. var container = self.getContainer(gridCfg.top);
  6363. var shape;
  6364. if (type === 'arc') {
  6365. var {
  6366. center,
  6367. startAngle,
  6368. endAngle
  6369. } = self;
  6370. var radius = Vector2.length([points[0].x - center.x, points[0].y - center.y]);
  6371. shape = container.addShape('Arc', {
  6372. className: 'axis-grid',
  6373. attrs: mix({
  6374. x: center.x,
  6375. y: center.y,
  6376. startAngle,
  6377. endAngle,
  6378. r: radius
  6379. }, gridCfg)
  6380. });
  6381. } else {
  6382. shape = container.addShape('Polyline', {
  6383. className: 'axis-grid',
  6384. attrs: mix({
  6385. points
  6386. }, gridCfg)
  6387. });
  6388. }
  6389. shape._id = subPoints._id;
  6390. }
  6391. });
  6392. }
  6393. getOffsetPoint() {}
  6394. getAxisVector() {}
  6395. getOffsetVector(point, offset) {
  6396. var self = this;
  6397. var axisVector = self.getAxisVector(point);
  6398. var normal = Vector2.normalize([], axisVector);
  6399. var factor = self.offsetFactor;
  6400. var verticalVector = [normal[1] * -1 * factor, normal[0] * factor];
  6401. return Vector2.scale([], verticalVector, offset);
  6402. }
  6403. getSidePoint(point, offset) {
  6404. var self = this;
  6405. var offsetVector = self.getOffsetVector(point, offset);
  6406. return {
  6407. x: point.x + offsetVector[0],
  6408. y: point.y + offsetVector[1]
  6409. };
  6410. }
  6411. getTextAlignInfo(point, offset) {
  6412. var self = this;
  6413. var offsetVector = self.getOffsetVector(point, offset);
  6414. var align;
  6415. var baseLine;
  6416. if (offsetVector[0] > 0) {
  6417. align = 'left';
  6418. } else if (offsetVector[0] < 0) {
  6419. align = 'right';
  6420. } else {
  6421. align = 'center';
  6422. }
  6423. if (offsetVector[1] > 0) {
  6424. baseLine = 'top';
  6425. } else if (offsetVector[1] < 0) {
  6426. baseLine = 'bottom';
  6427. } else {
  6428. baseLine = 'middle';
  6429. }
  6430. return {
  6431. textAlign: align,
  6432. textBaseline: baseLine
  6433. };
  6434. }
  6435. getContainer(isTop) {
  6436. var {
  6437. frontContainer,
  6438. backContainer
  6439. } = this;
  6440. return isTop ? frontContainer : backContainer;
  6441. }
  6442. }
  6443. class Line extends Abastract {
  6444. _initDefaultCfg() {
  6445. super._initDefaultCfg();
  6446. this.start = null;
  6447. this.end = null;
  6448. }
  6449. getOffsetPoint(value) {
  6450. var {
  6451. start,
  6452. end
  6453. } = this;
  6454. return {
  6455. x: start.x + (end.x - start.x) * value,
  6456. y: start.y + (end.y - start.y) * value
  6457. };
  6458. }
  6459. getAxisVector() {
  6460. var {
  6461. start,
  6462. end
  6463. } = this;
  6464. return [end.x - start.x, end.y - start.y];
  6465. }
  6466. drawLine(lineCfg) {
  6467. var container = this.getContainer(lineCfg.top);
  6468. var {
  6469. start,
  6470. end
  6471. } = this;
  6472. container.addShape('line', {
  6473. className: 'axis-line',
  6474. attrs: mix({
  6475. x1: start.x,
  6476. y1: start.y,
  6477. x2: end.x,
  6478. y2: end.y
  6479. }, lineCfg)
  6480. });
  6481. }
  6482. }
  6483. Abastract.Line = Line;
  6484. function _defineProperty$1(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  6485. var calcDirection = function calcDirection(start, end) {
  6486. var xDistance = end.x - start.x;
  6487. var yDistance = end.y - start.y; // x 的距离大于y 说明是横向,否则就是纵向
  6488. if (Math.abs(xDistance) > Math.abs(yDistance)) {
  6489. return xDistance > 0 ? 'right' : 'left';
  6490. }
  6491. return yDistance > 0 ? 'down' : 'up';
  6492. }; // 计算2点之间的距离
  6493. var calcDistance = function calcDistance(point1, point2) {
  6494. var xDistance = Math.abs(point2.x - point1.x);
  6495. var yDistance = Math.abs(point2.y - point1.y);
  6496. return Math.sqrt(xDistance * xDistance + yDistance * yDistance);
  6497. };
  6498. var getCenter = function getCenter(point1, point2) {
  6499. var x = point1.x + (point2.x - point1.x) / 2;
  6500. var y = point1.y + (point2.y - point1.y) / 2;
  6501. return {
  6502. x,
  6503. y
  6504. };
  6505. };
  6506. var convertPoints = function convertPoints(ev, canvas) {
  6507. var touches = ev.touches; // 认为是mouse事件
  6508. if (!touches) {
  6509. var point = getRelativePosition({
  6510. x: ev.clientX,
  6511. y: ev.clientY
  6512. }, canvas);
  6513. return [point];
  6514. }
  6515. var points = [];
  6516. var len = touches.length;
  6517. for (var i = 0; i < len; i++) {
  6518. var touch = touches[i]; // x, y: 相对canvas原点的位置,clientX, clientY 相对于可视窗口的位置
  6519. var {
  6520. x,
  6521. y,
  6522. clientX,
  6523. clientY
  6524. } = touch;
  6525. var _point = void 0; // 小程序环境会有x,y
  6526. if (isNumber(x) || isNumber(y)) {
  6527. _point = {
  6528. x,
  6529. y
  6530. };
  6531. } else {
  6532. // 浏览器环境再计算下canvas的相对位置
  6533. _point = getRelativePosition({
  6534. x: clientX,
  6535. y: clientY
  6536. }, canvas);
  6537. }
  6538. points.push(_point);
  6539. }
  6540. return points;
  6541. };
  6542. var PRESS_DELAY = 250;
  6543. class EventController {
  6544. constructor(_ref) {
  6545. var _this = this;
  6546. var {
  6547. canvas,
  6548. el
  6549. } = _ref;
  6550. _defineProperty$1(this, "_click", function (ev) {
  6551. var points = convertPoints(ev, _this.canvas);
  6552. ev.points = points;
  6553. _this.emitEvent('click', ev);
  6554. });
  6555. _defineProperty$1(this, "_start", function (ev) {
  6556. var points = convertPoints(ev, _this.canvas);
  6557. if (!points) {
  6558. return;
  6559. }
  6560. ev.points = points;
  6561. _this.emitEvent('touchstart', ev); // 防止上次的内容没有清理掉,重新reset下
  6562. _this.reset(); // 记录touch start 的时间
  6563. _this.startTime = Date.now(); // 记录touch start 的点
  6564. _this.startPoints = points;
  6565. if (points.length > 1) {
  6566. _this.startDistance = calcDistance(points[0], points[1]);
  6567. _this.center = getCenter(points[0], points[1]);
  6568. } else {
  6569. // 如果touchstart后停顿250ms, 则也触发press事件
  6570. _this.pressTimeout = setTimeout(function () {
  6571. // 这里固定触发press事件
  6572. var eventType = 'press';
  6573. var direction = 'none';
  6574. ev.direction = direction;
  6575. _this.emitStart(eventType, ev);
  6576. _this.emitEvent(eventType, ev);
  6577. _this.eventType = eventType;
  6578. _this.direction = direction;
  6579. }, PRESS_DELAY);
  6580. }
  6581. });
  6582. _defineProperty$1(this, "_move", function (ev) {
  6583. var points = convertPoints(ev, _this.canvas);
  6584. if (!points) return;
  6585. _this.clearPressTimeout();
  6586. ev.points = points;
  6587. _this.emitEvent('touchmove', ev);
  6588. var startPoints = _this.startPoints;
  6589. if (!startPoints) return; // 多指触控
  6590. if (points.length > 1) {
  6591. // touchstart的距离
  6592. var startDistance = _this.startDistance;
  6593. var currentDistance = calcDistance(points[0], points[1]);
  6594. ev.zoom = currentDistance / startDistance;
  6595. ev.center = _this.center; // 触发缩放事件
  6596. _this.emitStart('pinch', ev);
  6597. _this.emitEvent('pinch', ev);
  6598. } else {
  6599. var deltaX = points[0].x - startPoints[0].x;
  6600. var deltaY = points[0].y - startPoints[0].y;
  6601. var direction = _this.direction || calcDirection(startPoints[0], points[0]);
  6602. _this.direction = direction; // 获取press或者pan的事件类型
  6603. // press 按住滑动, pan表示平移
  6604. // 如果start后立刻move,则触发pan, 如果有停顿,则触发press
  6605. var eventType = _this.getEventType(points);
  6606. ev.direction = direction;
  6607. ev.deltaX = deltaX;
  6608. ev.deltaY = deltaY;
  6609. _this.emitStart(eventType, ev);
  6610. _this.emitEvent(eventType, ev); // 记录最后2次move的时间和坐标,为了给swipe事件用
  6611. var prevMoveTime = _this.lastMoveTime;
  6612. var now = Date.now(); // 最后2次的时间间隔一定要大于0,否则swipe没发计算
  6613. if (now - prevMoveTime > 0) {
  6614. _this.prevMoveTime = prevMoveTime;
  6615. _this.prevMovePoints = _this.lastMovePoints;
  6616. _this.lastMoveTime = now;
  6617. _this.lastMovePoints = points;
  6618. }
  6619. }
  6620. });
  6621. _defineProperty$1(this, "_end", function (ev) {
  6622. _this.emitEnd(ev);
  6623. _this.emitEvent('touchend', ev); // swipe事件处理, 在touchend之后触发
  6624. var lastMoveTime = _this.lastMoveTime;
  6625. var now = Date.now(); // 做这个判断是为了最后一次touchmove后到end前,还有一个停顿的过程
  6626. // 100 是拍的一个值,理论这个值会很短,一般不卡顿的话在10ms以内
  6627. if (now - lastMoveTime < 100) {
  6628. var prevMoveTime = _this.prevMoveTime || _this.startTime;
  6629. var intervalTime = lastMoveTime - prevMoveTime; // 时间间隔一定要大于0, 否则计算没意义
  6630. if (intervalTime > 0) {
  6631. var prevMovePoints = _this.prevMovePoints || _this.startPoints;
  6632. var lastMovePoints = _this.lastMovePoints; // move速率
  6633. var velocity = calcDistance(prevMovePoints[0], lastMovePoints[0]) / intervalTime; // 0.3 是参考hammerjs的设置
  6634. if (velocity > 0.3) {
  6635. ev.velocity = velocity;
  6636. ev.direction = calcDirection(prevMovePoints[0], lastMovePoints[0]);
  6637. _this.emitEvent('swipe', ev);
  6638. }
  6639. }
  6640. }
  6641. _this.reset();
  6642. var touches = ev.touches; // 当多指只释放了1指时也会触发end, 这时重新触发一次start
  6643. if (touches && touches.length > 0) {
  6644. _this._start(ev);
  6645. }
  6646. });
  6647. _defineProperty$1(this, "_cancel", function (ev) {
  6648. _this.emitEvent('touchcancel', ev);
  6649. _this.reset();
  6650. });
  6651. // canvasEl
  6652. this.canvas = canvas;
  6653. this.delegateEvent(el); // 用来记录当前触发的事件
  6654. this.processEvent = {};
  6655. }
  6656. delegateEvent(canvasEl) {
  6657. // 代理这几个事件
  6658. canvasEl.addEventListener('click', this._click);
  6659. canvasEl.addEventListener('touchstart', this._start);
  6660. canvasEl.addEventListener('touchmove', this._move);
  6661. canvasEl.addEventListener('touchend', this._end);
  6662. canvasEl.addEventListener('touchcancel', this._cancel);
  6663. }
  6664. emitEvent(type, ev) {
  6665. var canvas = this.canvas;
  6666. canvas.emit(type, ev);
  6667. }
  6668. getEventType(points) {
  6669. var {
  6670. eventType,
  6671. canvas,
  6672. startTime,
  6673. startPoints
  6674. } = this;
  6675. if (eventType) {
  6676. return eventType;
  6677. }
  6678. var type;
  6679. var panEventListeners = canvas.__events.pan; // 如果没有pan事件的监听,默认都是press
  6680. if (!panEventListeners || !panEventListeners.length) {
  6681. type = 'press';
  6682. } else {
  6683. // 如果有pan事件的处理,press则需要停顿250ms, 且移动距离小于10
  6684. var now = Date.now();
  6685. if (now - startTime > PRESS_DELAY && calcDistance(startPoints[0], points[0]) < 10) {
  6686. type = 'press';
  6687. } else {
  6688. type = 'pan';
  6689. }
  6690. }
  6691. this.eventType = type;
  6692. return type;
  6693. }
  6694. enable(eventType) {
  6695. this.processEvent[eventType] = true;
  6696. } // 是否进行中的事件
  6697. isProcess(eventType) {
  6698. return this.processEvent[eventType];
  6699. } // 触发start事件
  6700. emitStart(type, ev) {
  6701. if (this.isProcess(type)) {
  6702. return;
  6703. }
  6704. this.enable(type);
  6705. this.emitEvent("".concat(type, "start"), ev);
  6706. } // 触发end事件
  6707. emitEnd(ev) {
  6708. var _this2 = this;
  6709. var processEvent = this.processEvent;
  6710. Object.keys(processEvent).forEach(function (type) {
  6711. _this2.emitEvent("".concat(type, "end"), ev);
  6712. delete processEvent[type];
  6713. });
  6714. }
  6715. clearPressTimeout() {
  6716. if (this.pressTimeout) {
  6717. clearTimeout(this.pressTimeout);
  6718. this.pressTimeout = 0;
  6719. }
  6720. }
  6721. reset() {
  6722. this.clearPressTimeout();
  6723. this.startTime = 0;
  6724. this.startPoints = null;
  6725. this.startDistance = 0;
  6726. this.direction = null;
  6727. this.eventType = null;
  6728. this.pinch = false;
  6729. this.prevMoveTime = 0;
  6730. this.prevMovePoints = null;
  6731. this.lastMoveTime = 0;
  6732. this.lastMovePoints = null;
  6733. }
  6734. }
  6735. class CanvasElement extends EventEmit {
  6736. constructor(ctx) {
  6737. super();
  6738. this.context = ctx; // canvas实际的宽高 (width/height) * pixelRatio
  6739. this.width = 0;
  6740. this.height = 0;
  6741. this.style = {};
  6742. this.currentStyle = {}; // 用来标识是CanvasElement实例
  6743. this.isCanvasElement = true;
  6744. }
  6745. getContext()
  6746. /* type */
  6747. {
  6748. return this.context;
  6749. }
  6750. getBoundingClientRect() {
  6751. var width = this.width;
  6752. var height = this.height; // 默认都处理成可视窗口的顶部位置
  6753. return {
  6754. top: 0,
  6755. right: width,
  6756. bottom: height,
  6757. left: 0
  6758. };
  6759. }
  6760. addEventListener(type, listener) {
  6761. this.on(type, listener);
  6762. }
  6763. removeEventListener(type, listener) {
  6764. this.off(type, listener);
  6765. }
  6766. dispatchEvent(type, e) {
  6767. this.emit(type, e);
  6768. }
  6769. }
  6770. function supportEventListener(canvas) {
  6771. if (!canvas) {
  6772. return false;
  6773. } // 非 HTMLCanvasElement
  6774. if (canvas.nodeType !== 1 || !canvas.nodeName || canvas.nodeName.toLowerCase() !== 'canvas') {
  6775. return false;
  6776. } // 微信小程序canvas.getContext('2d')时也是CanvasRenderingContext2D
  6777. // 也会有ctx.canvas, 而且nodeType也是1,所以还要在看下是否支持addEventListener
  6778. var support = false;
  6779. try {
  6780. canvas.addEventListener('eventTest', function () {
  6781. support = true;
  6782. });
  6783. canvas.dispatchEvent(new Event('eventTest'));
  6784. } catch (error) {
  6785. support = false;
  6786. }
  6787. return support;
  6788. }
  6789. var CanvasElement$1 = {
  6790. create(ctx) {
  6791. if (!ctx) {
  6792. return null;
  6793. }
  6794. if (supportEventListener(ctx.canvas)) {
  6795. return ctx.canvas;
  6796. }
  6797. return new CanvasElement(ctx);
  6798. }
  6799. };
  6800. function _mod(n, m) {
  6801. return (n % m + m) % m;
  6802. }
  6803. function _addStop(steps, gradient) {
  6804. each(steps, function (item) {
  6805. item = item.split(':');
  6806. gradient.addColorStop(Number(item[0]), item[1]);
  6807. });
  6808. } // the string format: 'l(0) 0:#ffffff 0.5:#7ec2f3 1:#1890ff'
  6809. function _parseLineGradient(color, shape, context) {
  6810. var arr = color.split(' ');
  6811. var angle = arr[0].slice(2, arr[0].length - 1);
  6812. angle = _mod(parseFloat(angle) * Math.PI / 180, Math.PI * 2);
  6813. var steps = arr.slice(1);
  6814. var {
  6815. minX,
  6816. minY,
  6817. maxX,
  6818. maxY
  6819. } = shape.getBBox();
  6820. var start;
  6821. var end;
  6822. if (angle >= 0 && angle < 0.5 * Math.PI) {
  6823. start = {
  6824. x: minX,
  6825. y: minY
  6826. };
  6827. end = {
  6828. x: maxX,
  6829. y: maxY
  6830. };
  6831. } else if (0.5 * Math.PI <= angle && angle < Math.PI) {
  6832. start = {
  6833. x: maxX,
  6834. y: minY
  6835. };
  6836. end = {
  6837. x: minX,
  6838. y: maxY
  6839. };
  6840. } else if (Math.PI <= angle && angle < 1.5 * Math.PI) {
  6841. start = {
  6842. x: maxX,
  6843. y: maxY
  6844. };
  6845. end = {
  6846. x: minX,
  6847. y: minY
  6848. };
  6849. } else {
  6850. start = {
  6851. x: minX,
  6852. y: maxY
  6853. };
  6854. end = {
  6855. x: maxX,
  6856. y: minY
  6857. };
  6858. }
  6859. var tanTheta = Math.tan(angle);
  6860. var tanTheta2 = tanTheta * tanTheta;
  6861. var x = (end.x - start.x + tanTheta * (end.y - start.y)) / (tanTheta2 + 1) + start.x;
  6862. var y = tanTheta * (end.x - start.x + tanTheta * (end.y - start.y)) / (tanTheta2 + 1) + start.y;
  6863. var gradient = context.createLinearGradient(start.x, start.y, x, y);
  6864. _addStop(steps, gradient);
  6865. return gradient;
  6866. } // the string format: 'r(0.5, 0.5, 0.1) 0:#ffffff 1:#1890ff'
  6867. function _parseRadialGradient(color, shape, context) {
  6868. var arr = color.split(' ');
  6869. var circleCfg = arr[0].slice(2, arr[0].length - 1);
  6870. circleCfg = circleCfg.split(',');
  6871. var fx = parseFloat(circleCfg[0]);
  6872. var fy = parseFloat(circleCfg[1]);
  6873. var fr = parseFloat(circleCfg[2]);
  6874. var steps = arr.slice(1); // if radius is 0, no gradient, stroke with the last color
  6875. if (fr === 0) {
  6876. var _color = steps[steps.length - 1];
  6877. return _color.split(':')[1];
  6878. }
  6879. var {
  6880. width,
  6881. height,
  6882. minX,
  6883. minY
  6884. } = shape.getBBox();
  6885. var r = Math.sqrt(width * width + height * height) / 2;
  6886. var gradient = context.createRadialGradient(minX + width * fx, minY + height * fy, fr * r, minX + width / 2, minY + height / 2, r);
  6887. _addStop(steps, gradient);
  6888. return gradient;
  6889. }
  6890. function parseStyle(color, shape, context) {
  6891. if (color[1] === '(') {
  6892. try {
  6893. var firstCode = color[0];
  6894. if (firstCode === 'l') {
  6895. return _parseLineGradient(color, shape, context);
  6896. } else if (firstCode === 'r') {
  6897. return _parseRadialGradient(color, shape, context);
  6898. }
  6899. } catch (ev) {
  6900. console.error('error in parsing gradient string, please check if there are any extra whitespaces.');
  6901. console.error(ev);
  6902. }
  6903. }
  6904. return color;
  6905. }
  6906. var ALIAS_ATTRS_MAP = {
  6907. stroke: 'strokeStyle',
  6908. fill: 'fillStyle',
  6909. opacity: 'globalAlpha'
  6910. };
  6911. var SHAPE_ATTRS = ['fillStyle', 'font', 'globalAlpha', 'lineCap', 'lineWidth', 'lineJoin', 'miterLimit', 'shadowBlur', 'shadowColor', 'shadowOffsetX', 'shadowOffsetY', 'strokeStyle', 'textAlign', 'textBaseline', 'lineDash', 'shadow' // 兼容支付宝小程序
  6912. ];
  6913. var CLIP_SHAPES = ['circle', 'sector', 'polygon', 'rect', 'polyline'];
  6914. class Element$1 {
  6915. _initProperties() {
  6916. this._attrs = {
  6917. zIndex: 0,
  6918. visible: true,
  6919. destroyed: false
  6920. };
  6921. }
  6922. constructor(cfg) {
  6923. this._initProperties();
  6924. mix(this._attrs, cfg);
  6925. var attrs = this._attrs.attrs;
  6926. if (attrs) {
  6927. this.initAttrs(attrs);
  6928. }
  6929. this.initTransform();
  6930. }
  6931. get(name) {
  6932. return this._attrs[name];
  6933. }
  6934. set(name, value) {
  6935. this._attrs[name] = value;
  6936. }
  6937. isGroup() {
  6938. return this.get('isGroup');
  6939. }
  6940. isShape() {
  6941. return this.get('isShape');
  6942. }
  6943. initAttrs(attrs) {
  6944. this.attr(mix(this.getDefaultAttrs(), attrs));
  6945. }
  6946. getDefaultAttrs() {
  6947. return {};
  6948. }
  6949. _setAttr(name, value) {
  6950. var attrs = this._attrs.attrs;
  6951. if (name === 'clip') {
  6952. value = this._setAttrClip(value);
  6953. } else {
  6954. var alias = ALIAS_ATTRS_MAP[name];
  6955. if (alias) {
  6956. attrs[alias] = value;
  6957. }
  6958. }
  6959. attrs[name] = value;
  6960. }
  6961. _getAttr(name) {
  6962. return this._attrs.attrs[name];
  6963. } // _afterAttrsSet() {}
  6964. _setAttrClip(clip) {
  6965. if (clip && CLIP_SHAPES.indexOf(clip._attrs.type) > -1) {
  6966. if (clip.get('canvas') === null) {
  6967. clip = Object.assign({}, clip);
  6968. }
  6969. clip.set('parent', this.get('parent'));
  6970. clip.set('context', this.get('context'));
  6971. return clip;
  6972. }
  6973. return null;
  6974. }
  6975. attr(name, value) {
  6976. var self = this;
  6977. if (self.get('destroyed')) return null;
  6978. var argumentsLen = arguments.length;
  6979. if (argumentsLen === 0) {
  6980. return self._attrs.attrs;
  6981. }
  6982. if (isObject(name)) {
  6983. this._attrs.bbox = null;
  6984. for (var k in name) {
  6985. self._setAttr(k, name[k]);
  6986. }
  6987. if (self._afterAttrsSet) {
  6988. self._afterAttrsSet();
  6989. }
  6990. return self;
  6991. }
  6992. if (argumentsLen === 2) {
  6993. this._attrs.bbox = null;
  6994. self._setAttr(name, value);
  6995. if (self._afterAttrsSet) {
  6996. self._afterAttrsSet();
  6997. }
  6998. return self;
  6999. }
  7000. return self._getAttr(name);
  7001. }
  7002. getParent() {
  7003. return this.get('parent');
  7004. }
  7005. draw(context) {
  7006. if (this.get('destroyed')) {
  7007. return;
  7008. }
  7009. if (this.get('visible')) {
  7010. this.setContext(context);
  7011. this.drawInner(context);
  7012. this.restoreContext(context);
  7013. }
  7014. }
  7015. setContext(context) {
  7016. var clip = this._attrs.attrs.clip;
  7017. context.save();
  7018. if (clip) {
  7019. clip.resetTransform(context);
  7020. clip.createPath(context);
  7021. context.clip();
  7022. }
  7023. this.resetContext(context);
  7024. this.resetTransform(context);
  7025. }
  7026. restoreContext(context) {
  7027. context.restore();
  7028. }
  7029. resetContext(context) {
  7030. var elAttrs = this._attrs.attrs;
  7031. if (!this._attrs.isGroup) {
  7032. for (var k in elAttrs) {
  7033. if (SHAPE_ATTRS.indexOf(k) > -1) {
  7034. var v = elAttrs[k];
  7035. if (k === 'fillStyle' || k === 'strokeStyle') {
  7036. v = parseStyle(v, this, context);
  7037. }
  7038. if (k === 'lineDash' && context.setLineDash && isArray(v)) {
  7039. context.setLineDash(v);
  7040. } else {
  7041. context[k] = v;
  7042. }
  7043. }
  7044. }
  7045. }
  7046. }
  7047. hasFill() {
  7048. return this.get('canFill') && this._attrs.attrs.fillStyle;
  7049. }
  7050. hasStroke() {
  7051. return this.get('canStroke') && this._attrs.attrs.strokeStyle;
  7052. }
  7053. drawInner()
  7054. /* context */
  7055. {}
  7056. show() {
  7057. this.set('visible', true);
  7058. return this;
  7059. }
  7060. hide() {
  7061. this.set('visible', false);
  7062. return this;
  7063. }
  7064. isVisible() {
  7065. return this.get('visible');
  7066. }
  7067. _removeFromParent() {
  7068. var parent = this.get('parent');
  7069. if (parent) {
  7070. var children = parent.get('children');
  7071. remove$1(children, this);
  7072. }
  7073. return this;
  7074. }
  7075. remove(destroy) {
  7076. if (destroy) {
  7077. this.destroy();
  7078. } else {
  7079. this._removeFromParent();
  7080. }
  7081. }
  7082. destroy() {
  7083. var destroyed = this.get('destroyed');
  7084. if (destroyed) {
  7085. return null;
  7086. }
  7087. this._removeFromParent();
  7088. this._attrs = {};
  7089. this.set('destroyed', true);
  7090. }
  7091. getBBox() {
  7092. return {
  7093. minX: 0,
  7094. maxX: 0,
  7095. minY: 0,
  7096. maxY: 0,
  7097. width: 0,
  7098. height: 0
  7099. };
  7100. }
  7101. initTransform() {
  7102. var attrs = this._attrs.attrs || {};
  7103. if (!attrs.matrix) {
  7104. attrs.matrix = [1, 0, 0, 1, 0, 0];
  7105. }
  7106. this._attrs.attrs = attrs;
  7107. }
  7108. getMatrix() {
  7109. return this._attrs.attrs.matrix;
  7110. }
  7111. setMatrix(m) {
  7112. this._attrs.attrs.matrix = [m[0], m[1], m[2], m[3], m[4], m[5]];
  7113. }
  7114. transform(actions) {
  7115. var matrix = this._attrs.attrs.matrix;
  7116. this._attrs.attrs.matrix = Matrix.transform(matrix, actions);
  7117. return this;
  7118. }
  7119. setTransform(actions) {
  7120. this._attrs.attrs.matrix = [1, 0, 0, 1, 0, 0];
  7121. return this.transform(actions);
  7122. }
  7123. translate(x, y) {
  7124. var matrix = this._attrs.attrs.matrix;
  7125. Matrix.translate(matrix, matrix, [x, y]);
  7126. }
  7127. rotate(rad) {
  7128. var matrix = this._attrs.attrs.matrix;
  7129. Matrix.rotate(matrix, matrix, rad);
  7130. }
  7131. scale(sx, sy) {
  7132. var matrix = this._attrs.attrs.matrix;
  7133. Matrix.scale(matrix, matrix, [sx, sy]);
  7134. }
  7135. moveTo(x, y) {
  7136. var cx = this._attrs.x || 0;
  7137. var cy = this._attrs.y || 0;
  7138. this.translate(x - cx, y - cy);
  7139. this.set('x', x);
  7140. this.set('y', y);
  7141. }
  7142. apply(v) {
  7143. var m = this._attrs.attrs.matrix;
  7144. Vector2.transformMat2d(v, v, m);
  7145. return this;
  7146. }
  7147. resetTransform(context) {
  7148. var mo = this._attrs.attrs.matrix;
  7149. if (Matrix.isChanged(mo)) {
  7150. context.transform(mo[0], mo[1], mo[2], mo[3], mo[4], mo[5]);
  7151. }
  7152. }
  7153. isDestroyed() {
  7154. return this.get('destroyed');
  7155. }
  7156. }
  7157. class Shape$2 extends Element$1 {
  7158. _initProperties() {
  7159. this._attrs = {
  7160. zIndex: 0,
  7161. visible: true,
  7162. destroyed: false,
  7163. isShape: true,
  7164. attrs: {}
  7165. };
  7166. }
  7167. getType() {
  7168. return this._attrs.type;
  7169. }
  7170. drawInner(context) {
  7171. var self = this;
  7172. var attrs = self.get('attrs');
  7173. self.createPath(context);
  7174. var originOpacity = context.globalAlpha;
  7175. if (self.hasFill()) {
  7176. var fillOpacity = attrs.fillOpacity;
  7177. if (!isNil(fillOpacity) && fillOpacity !== 1) {
  7178. context.globalAlpha = fillOpacity;
  7179. context.fill();
  7180. context.globalAlpha = originOpacity;
  7181. } else {
  7182. context.fill();
  7183. }
  7184. }
  7185. if (self.hasStroke()) {
  7186. var lineWidth = attrs.lineWidth;
  7187. if (lineWidth > 0) {
  7188. var strokeOpacity = attrs.strokeOpacity;
  7189. if (!isNil(strokeOpacity) && strokeOpacity !== 1) {
  7190. context.globalAlpha = strokeOpacity;
  7191. }
  7192. context.stroke();
  7193. }
  7194. }
  7195. }
  7196. getBBox() {
  7197. var bbox = this._attrs.bbox;
  7198. if (!bbox) {
  7199. bbox = this.calculateBox();
  7200. if (bbox) {
  7201. bbox.x = bbox.minX;
  7202. bbox.y = bbox.minY;
  7203. bbox.width = bbox.maxX - bbox.minX;
  7204. bbox.height = bbox.maxY - bbox.minY;
  7205. }
  7206. this._attrs.bbox = bbox;
  7207. }
  7208. return bbox;
  7209. }
  7210. calculateBox() {
  7211. return null;
  7212. }
  7213. createPath() {}
  7214. }
  7215. var SHAPE_MAP = {};
  7216. var INDEX = '_INDEX';
  7217. function getComparer(compare) {
  7218. return function (left, right) {
  7219. var result = compare(left, right);
  7220. return result === 0 ? left[INDEX] - right[INDEX] : result;
  7221. };
  7222. }
  7223. var Container = {
  7224. getGroupClass() {},
  7225. getChildren() {
  7226. return this.get('children');
  7227. },
  7228. addShape(type) {
  7229. var cfg = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  7230. var canvas = this.get('canvas');
  7231. var shapeType = SHAPE_MAP[type];
  7232. if (!shapeType) {
  7233. shapeType = upperFirst(type);
  7234. SHAPE_MAP[type] = shapeType;
  7235. }
  7236. cfg.canvas = canvas;
  7237. if (shapeType === 'Text' && canvas && canvas.get('fontFamily')) {
  7238. cfg.attrs.fontFamily = cfg.attrs.fontFamily || canvas.get('fontFamily');
  7239. }
  7240. var shape = new Shape$2[shapeType](cfg);
  7241. this.add(shape);
  7242. return shape;
  7243. },
  7244. addGroup(cfg) {
  7245. var canvas = this.get('canvas');
  7246. var groupClass = this.getGroupClass();
  7247. cfg = mix({}, cfg);
  7248. cfg.canvas = canvas;
  7249. cfg.parent = this;
  7250. var rst = new groupClass(cfg);
  7251. this.add(rst);
  7252. return rst;
  7253. },
  7254. contain(item) {
  7255. var children = this.get('children');
  7256. return children.indexOf(item) > -1;
  7257. },
  7258. sort() {
  7259. var children = this.get('children');
  7260. for (var i = 0, len = children.length; i < len; i++) {
  7261. var child = children[i];
  7262. child[INDEX] = i;
  7263. }
  7264. children.sort(getComparer(function (obj1, obj2) {
  7265. return obj1.get('zIndex') - obj2.get('zIndex');
  7266. }));
  7267. return this;
  7268. },
  7269. drawInner(context) {
  7270. var children = this.get('children');
  7271. for (var i = 0, len = children.length; i < len; i++) {
  7272. var child = children[i];
  7273. child.draw(context);
  7274. }
  7275. return this;
  7276. },
  7277. clear() {
  7278. var children = this.get('children');
  7279. while (children.length !== 0) {
  7280. children[children.length - 1].remove(true);
  7281. }
  7282. return this;
  7283. },
  7284. add(items) {
  7285. var self = this;
  7286. var children = self.get('children');
  7287. if (!isArray(items)) {
  7288. items = [items];
  7289. }
  7290. for (var i = 0, len = items.length; i < len; i++) {
  7291. var item = items[i];
  7292. var parent = item.get('parent');
  7293. if (parent) {
  7294. var descendants = parent.get('children');
  7295. remove$1(descendants, item);
  7296. }
  7297. self._setEvn(item);
  7298. children.push(item);
  7299. }
  7300. return self;
  7301. },
  7302. _setEvn(item) {
  7303. var self = this;
  7304. item._attrs.parent = self;
  7305. item._attrs.context = self._attrs.context;
  7306. item._attrs.canvas = self._attrs.canvas;
  7307. var clip = item._attrs.attrs.clip;
  7308. if (clip) {
  7309. clip.set('parent', self);
  7310. clip.set('context', self.get('context'));
  7311. }
  7312. if (item._attrs.isGroup) {
  7313. var children = item._attrs.children;
  7314. for (var i = 0, len = children.length; i < len; i++) {
  7315. item._setEvn(children[i]);
  7316. }
  7317. }
  7318. }
  7319. };
  7320. class Group extends Element$1 {
  7321. _initProperties() {
  7322. this._attrs = {
  7323. zIndex: 0,
  7324. visible: true,
  7325. destroyed: false,
  7326. isGroup: true,
  7327. children: []
  7328. };
  7329. }
  7330. getBBox() {
  7331. var self = this;
  7332. var minX = Infinity;
  7333. var maxX = -Infinity;
  7334. var minY = Infinity;
  7335. var maxY = -Infinity;
  7336. var children = self.get('children');
  7337. for (var i = 0, length = children.length; i < length; i++) {
  7338. var child = children[i];
  7339. if (child.get('visible')) {
  7340. var box = child.getBBox();
  7341. if (!box) {
  7342. continue;
  7343. }
  7344. var leftTop = [box.minX, box.minY];
  7345. var leftBottom = [box.minX, box.maxY];
  7346. var rightTop = [box.maxX, box.minY];
  7347. var rightBottom = [box.maxX, box.maxY];
  7348. var matrix = child.attr('matrix');
  7349. Vector2.transformMat2d(leftTop, leftTop, matrix);
  7350. Vector2.transformMat2d(leftBottom, leftBottom, matrix);
  7351. Vector2.transformMat2d(rightTop, rightTop, matrix);
  7352. Vector2.transformMat2d(rightBottom, rightBottom, matrix);
  7353. minX = Math.min(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0], minX);
  7354. maxX = Math.max(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0], maxX);
  7355. minY = Math.min(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1], minY);
  7356. maxY = Math.max(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1], maxY);
  7357. }
  7358. }
  7359. return {
  7360. minX,
  7361. minY,
  7362. maxX,
  7363. maxY,
  7364. x: minX,
  7365. y: minY,
  7366. width: maxX - minX,
  7367. height: maxY - minY
  7368. };
  7369. }
  7370. destroy() {
  7371. if (this.get('destroyed')) {
  7372. return;
  7373. }
  7374. this.clear();
  7375. super.destroy();
  7376. }
  7377. }
  7378. mix(Group.prototype, Container, {
  7379. getGroupClass() {
  7380. return Group;
  7381. }
  7382. });
  7383. var requestAnimationFrame$1 = typeof window === 'object' && window.requestAnimationFrame ? window.requestAnimationFrame : function (fn) {
  7384. return setTimeout(fn, 16);
  7385. };
  7386. class Canvas extends EventEmit {
  7387. get(name) {
  7388. return this._attrs[name];
  7389. }
  7390. set(name, value) {
  7391. this._attrs[name] = value;
  7392. }
  7393. constructor(cfg) {
  7394. super();
  7395. this._attrs = mix({
  7396. type: 'canvas',
  7397. children: []
  7398. }, cfg);
  7399. this._initPixelRatio();
  7400. this._initCanvas();
  7401. }
  7402. _initPixelRatio() {
  7403. var pixelRatio = this.get('pixelRatio');
  7404. if (!pixelRatio) {
  7405. this.set('pixelRatio', getPixelRatio());
  7406. }
  7407. }
  7408. beforeDraw() {
  7409. var context = this._attrs.context;
  7410. var el = this._attrs.el;
  7411. context && context.clearRect && context.clearRect(0, 0, el.width, el.height);
  7412. }
  7413. _initCanvas() {
  7414. var self = this;
  7415. var el = self.get('el');
  7416. var context = self.get('context');
  7417. if (!el && !context) {
  7418. throw new Error('Please specify the id, el or context of the chart!');
  7419. }
  7420. var canvas;
  7421. if (el) {
  7422. // DOMElement or String
  7423. canvas = isString(el) ? getDomById(el) : el;
  7424. } else {
  7425. // 说明没有指定el
  7426. canvas = CanvasElement$1.create(context);
  7427. }
  7428. if (context && canvas && !canvas.getContext) {
  7429. canvas.getContext = function () {
  7430. return context;
  7431. };
  7432. }
  7433. var width = self.get('width');
  7434. if (!width) {
  7435. width = getWidth(canvas);
  7436. }
  7437. var height = self.get('height');
  7438. if (!height) {
  7439. height = getHeight(canvas);
  7440. }
  7441. self.set('canvas', this);
  7442. self.set('el', canvas);
  7443. self.set('context', context || canvas.getContext('2d'));
  7444. self.changeSize(width, height); // 初始化事件控制器
  7445. var eventController = new EventController({
  7446. canvas: this,
  7447. el: canvas
  7448. });
  7449. self.set('eventController', eventController);
  7450. }
  7451. changeSize(width, height) {
  7452. var pixelRatio = this.get('pixelRatio');
  7453. var canvasDOM = this.get('el'); // HTMLCanvasElement or canvasElement
  7454. // 浏览器环境设置style样式
  7455. if (canvasDOM.style) {
  7456. canvasDOM.style.width = width + 'px';
  7457. canvasDOM.style.height = height + 'px';
  7458. }
  7459. if (isCanvasElement(canvasDOM)) {
  7460. canvasDOM.width = width * pixelRatio;
  7461. canvasDOM.height = height * pixelRatio;
  7462. if (pixelRatio !== 1) {
  7463. var ctx = this.get('context');
  7464. ctx.scale(pixelRatio, pixelRatio);
  7465. }
  7466. }
  7467. this.set('width', width);
  7468. this.set('height', height);
  7469. }
  7470. getWidth() {
  7471. var pixelRatio = this.get('pixelRatio');
  7472. var width = this.get('width');
  7473. return width * pixelRatio;
  7474. }
  7475. getHeight() {
  7476. var pixelRatio = this.get('pixelRatio');
  7477. var height = this.get('height');
  7478. return height * pixelRatio;
  7479. }
  7480. getPointByClient(clientX, clientY) {
  7481. var el = this.get('el');
  7482. var bbox = el.getBoundingClientRect();
  7483. var width = bbox.right - bbox.left;
  7484. var height = bbox.bottom - bbox.top;
  7485. return {
  7486. x: (clientX - bbox.left) * (el.width / width),
  7487. y: (clientY - bbox.top) * (el.height / height)
  7488. };
  7489. }
  7490. _beginDraw() {
  7491. this._attrs.toDraw = true;
  7492. }
  7493. _endDraw() {
  7494. this._attrs.toDraw = false;
  7495. }
  7496. draw() {
  7497. var self = this;
  7498. function drawInner() {
  7499. self.set('animateHandler', requestAnimationFrame$1(function () {
  7500. self.set('animateHandler', undefined);
  7501. if (self.get('toDraw')) {
  7502. drawInner();
  7503. }
  7504. }));
  7505. self.beforeDraw();
  7506. try {
  7507. var context = self._attrs.context;
  7508. self.drawInner(context); // 支付宝,微信小程序,需要调context.draw才能完成绘制, 所以这里直接判断是否有.draw方法
  7509. if (context.draw) {
  7510. context.draw();
  7511. }
  7512. } catch (ev) {
  7513. console.warn('error in draw canvas, detail as:');
  7514. console.warn(ev);
  7515. self._endDraw();
  7516. }
  7517. self._endDraw();
  7518. }
  7519. if (self.get('destroyed')) {
  7520. return;
  7521. }
  7522. if (self.get('animateHandler')) {
  7523. this._beginDraw();
  7524. } else {
  7525. drawInner();
  7526. }
  7527. }
  7528. destroy() {
  7529. if (this.get('destroyed')) {
  7530. return;
  7531. } // 需要清理 canvas 画布内容,否则会导致 spa 应用 ios 下 canvas 白屏
  7532. // https://stackoverflow.com/questions/52532614/total-canvas-memory-use-exceeds-the-maximum-limit-safari-12
  7533. // https://github.com/antvis/F2/issues/630
  7534. var el = this.get('el');
  7535. el.width = 0;
  7536. el.height = 0;
  7537. this.clear();
  7538. this._attrs = {};
  7539. this.set('destroyed', true);
  7540. }
  7541. isDestroyed() {
  7542. return this.get('destroyed');
  7543. }
  7544. }
  7545. mix(Canvas.prototype, Container, {
  7546. getGroupClass() {
  7547. return Group;
  7548. }
  7549. });
  7550. function parseRadius$1(radius, width, height) {
  7551. radius = parsePadding(radius); // 都为0
  7552. if (!radius[0] && !radius[1] && !radius[2] && !radius[3]) {
  7553. return radius;
  7554. }
  7555. var minWidth = Math.max(radius[0] + radius[1], radius[2] + radius[3]);
  7556. var minHeight = Math.max(radius[0] + radius[3], radius[1] + radius[2]);
  7557. var scale = Math.min(width / minWidth, height / minHeight);
  7558. if (scale < 1) {
  7559. return radius.map(function (r) {
  7560. return r * scale;
  7561. });
  7562. }
  7563. return radius;
  7564. }
  7565. class Rect extends Shape$2 {
  7566. _initProperties() {
  7567. super._initProperties();
  7568. this._attrs.canFill = true;
  7569. this._attrs.canStroke = true;
  7570. this._attrs.type = 'rect';
  7571. }
  7572. getDefaultAttrs() {
  7573. return {
  7574. x: 0,
  7575. y: 0,
  7576. width: 0,
  7577. height: 0,
  7578. radius: 0,
  7579. lineWidth: 0
  7580. };
  7581. }
  7582. createPath(context) {
  7583. var self = this;
  7584. var attrs = self.get('attrs');
  7585. var {
  7586. x,
  7587. y,
  7588. width,
  7589. height,
  7590. radius
  7591. } = attrs;
  7592. context.beginPath();
  7593. if (!radius || !(width * height)) {
  7594. context.rect(x, y, width, height);
  7595. } else {
  7596. radius = parseRadius$1(radius, width, height);
  7597. context.moveTo(x + radius[0], y);
  7598. context.lineTo(x + width - radius[1], y);
  7599. context.arc(x + width - radius[1], y + radius[1], radius[1], -Math.PI / 2, 0, false);
  7600. context.lineTo(x + width, y + height - radius[2]);
  7601. context.arc(x + width - radius[2], y + height - radius[2], radius[2], 0, Math.PI / 2, false);
  7602. context.lineTo(x + radius[3], y + height);
  7603. context.arc(x + radius[3], y + height - radius[3], radius[3], Math.PI / 2, Math.PI, false);
  7604. context.lineTo(x, y + radius[0]);
  7605. context.arc(x + radius[0], y + radius[0], radius[0], Math.PI, Math.PI * 3 / 2, false);
  7606. context.closePath();
  7607. }
  7608. }
  7609. calculateBox() {
  7610. var attrs = this.get('attrs');
  7611. var {
  7612. x,
  7613. y,
  7614. width,
  7615. height
  7616. } = attrs;
  7617. return {
  7618. minX: x,
  7619. minY: y,
  7620. maxX: x + width,
  7621. maxY: y + height
  7622. };
  7623. }
  7624. }
  7625. Shape$2.Rect = Rect;
  7626. class ImageShape extends Shape$2 {
  7627. _initProperties() {
  7628. super._initProperties();
  7629. this._attrs.canFill = false;
  7630. this._attrs.canStroke = false;
  7631. this._attrs.loading = false;
  7632. this._attrs.image = null;
  7633. this._attrs.type = 'image';
  7634. }
  7635. getDefaultAttrs() {
  7636. return {
  7637. x: 0,
  7638. y: 0,
  7639. width: 0,
  7640. height: 0
  7641. };
  7642. }
  7643. createPath(context) {
  7644. var _this = this;
  7645. var attrs = this.get('attrs');
  7646. var {
  7647. src
  7648. } = attrs;
  7649. if (this.get('loading')) {
  7650. return;
  7651. }
  7652. var image = this.get('image');
  7653. if (image) {
  7654. this.drawImage(context, image);
  7655. } else {
  7656. if (src && Image) {
  7657. this.set('loading', true);
  7658. var _image = new Image();
  7659. _image.src = src; // 设置跨域
  7660. _image.crossOrigin = 'Anonymous';
  7661. _image.onload = function () {
  7662. _this.set('loading', false);
  7663. _this.set('image', _image);
  7664. _this.drawImage(context, _image);
  7665. };
  7666. }
  7667. }
  7668. }
  7669. drawImage(context, image) {
  7670. var attrs = this.get('attrs');
  7671. var {
  7672. x,
  7673. y,
  7674. width,
  7675. height,
  7676. sx,
  7677. sy,
  7678. swidth,
  7679. sheight
  7680. } = attrs;
  7681. if (!isNil(sx) && !isNil(sy) && !isNil(swidth) && !isNil(sheight)) {
  7682. context.drawImage(image, sx, sy, swidth, sheight, x, y, width, height);
  7683. } else {
  7684. context.drawImage(image, x, y, width, height);
  7685. }
  7686. }
  7687. calculateBox() {
  7688. var attrs = this.get('attrs');
  7689. var {
  7690. x,
  7691. y,
  7692. width,
  7693. height
  7694. } = attrs; // 和rect一样
  7695. return {
  7696. minX: x,
  7697. minY: y,
  7698. maxX: x + width,
  7699. maxY: y + height
  7700. };
  7701. }
  7702. }
  7703. Shape$2.Image = ImageShape;
  7704. class Circle extends Shape$2 {
  7705. _initProperties() {
  7706. super._initProperties();
  7707. this._attrs.canFill = true;
  7708. this._attrs.canStroke = true;
  7709. this._attrs.type = 'circle';
  7710. }
  7711. getDefaultAttrs() {
  7712. return {
  7713. x: 0,
  7714. y: 0,
  7715. r: 0,
  7716. lineWidth: 0
  7717. };
  7718. }
  7719. createPath(context) {
  7720. var attrs = this.get('attrs');
  7721. var {
  7722. x,
  7723. y,
  7724. r
  7725. } = attrs;
  7726. context.beginPath();
  7727. context.arc(x, y, r, 0, Math.PI * 2, false);
  7728. context.closePath();
  7729. }
  7730. calculateBox() {
  7731. var attrs = this.get('attrs');
  7732. var {
  7733. x,
  7734. y,
  7735. r
  7736. } = attrs;
  7737. return {
  7738. minX: x - r,
  7739. maxX: x + r,
  7740. minY: y - r,
  7741. maxY: y + r
  7742. };
  7743. }
  7744. }
  7745. Shape$2.Circle = Circle;
  7746. var start = Vector2.create();
  7747. var end = Vector2.create();
  7748. var extremity = Vector2.create();
  7749. function getCubicBezierXYatT(startPt, controlPt1, controlPt2, endPt, T) {
  7750. var x = CubicN(T, startPt.x, controlPt1.x, controlPt2.x, endPt.x);
  7751. var y = CubicN(T, startPt.y, controlPt1.y, controlPt2.y, endPt.y);
  7752. return {
  7753. x,
  7754. y
  7755. };
  7756. } // cubic helper formula at T distance
  7757. function CubicN(T, a, b, c, d) {
  7758. var t2 = T * T;
  7759. var t3 = t2 * T;
  7760. return a + (-a * 3 + T * (3 * a - a * T)) * T + (3 * b + T * (-6 * b + b * 3 * T)) * T + (c * 3 - c * 3 * T) * t2 + d * t3;
  7761. }
  7762. function cubicBezierBounds(c) {
  7763. var minX = Infinity;
  7764. var maxX = -Infinity;
  7765. var minY = Infinity;
  7766. var maxY = -Infinity;
  7767. var s = {
  7768. x: c[0],
  7769. y: c[1]
  7770. };
  7771. var c1 = {
  7772. x: c[2],
  7773. y: c[3]
  7774. };
  7775. var c2 = {
  7776. x: c[4],
  7777. y: c[5]
  7778. };
  7779. var e = {
  7780. x: c[6],
  7781. y: c[7]
  7782. };
  7783. for (var t = 0; t < 100; t++) {
  7784. var pt = getCubicBezierXYatT(s, c1, c2, e, t / 100);
  7785. if (pt.x < minX) {
  7786. minX = pt.x;
  7787. }
  7788. if (pt.x > maxX) {
  7789. maxX = pt.x;
  7790. }
  7791. if (pt.y < minY) {
  7792. minY = pt.y;
  7793. }
  7794. if (pt.y > maxY) {
  7795. maxY = pt.y;
  7796. }
  7797. }
  7798. return {
  7799. minX,
  7800. minY,
  7801. maxX,
  7802. maxY
  7803. };
  7804. }
  7805. function getBBoxFromPoints(points, lineWidth) {
  7806. if (points.length === 0) {
  7807. return;
  7808. }
  7809. var p = points[0];
  7810. var left = p.x;
  7811. var right = p.x;
  7812. var top = p.y;
  7813. var bottom = p.y;
  7814. var len = points.length;
  7815. for (var i = 1; i < len; i++) {
  7816. p = points[i];
  7817. left = Math.min(left, p.x);
  7818. right = Math.max(right, p.x);
  7819. top = Math.min(top, p.y);
  7820. bottom = Math.max(bottom, p.y);
  7821. }
  7822. lineWidth = lineWidth / 2 || 0;
  7823. return {
  7824. minX: left - lineWidth,
  7825. minY: top - lineWidth,
  7826. maxX: right + lineWidth,
  7827. maxY: bottom + lineWidth
  7828. };
  7829. }
  7830. function getBBoxFromLine(x0, y0, x1, y1, lineWidth) {
  7831. lineWidth = lineWidth / 2 || 0;
  7832. return {
  7833. minX: Math.min(x0, x1) - lineWidth,
  7834. minY: Math.min(y0, y1) - lineWidth,
  7835. maxX: Math.max(x0, x1) + lineWidth,
  7836. maxY: Math.max(y0, y1) + lineWidth
  7837. };
  7838. }
  7839. function getBBoxFromArc(x, y, r, startAngle, endAngle, anticlockwise) {
  7840. var diff = Math.abs(startAngle - endAngle);
  7841. if (diff % (Math.PI * 2) < 1e-4 && diff > 1e-4) {
  7842. // Is a circle
  7843. return {
  7844. minX: x - r,
  7845. minY: y - r,
  7846. maxX: x + r,
  7847. maxY: y + r
  7848. };
  7849. }
  7850. start[0] = Math.cos(startAngle) * r + x;
  7851. start[1] = Math.sin(startAngle) * r + y;
  7852. end[0] = Math.cos(endAngle) * r + x;
  7853. end[1] = Math.sin(endAngle) * r + y;
  7854. var min = [0, 0];
  7855. var max = [0, 0];
  7856. Vector2.min(min, start, end);
  7857. Vector2.max(max, start, end); // Thresh to [0, Math.PI * 2]
  7858. startAngle = startAngle % (Math.PI * 2);
  7859. if (startAngle < 0) {
  7860. startAngle = startAngle + Math.PI * 2;
  7861. }
  7862. endAngle = endAngle % (Math.PI * 2);
  7863. if (endAngle < 0) {
  7864. endAngle = endAngle + Math.PI * 2;
  7865. }
  7866. if (startAngle > endAngle && !anticlockwise) {
  7867. endAngle += Math.PI * 2;
  7868. } else if (startAngle < endAngle && anticlockwise) {
  7869. startAngle += Math.PI * 2;
  7870. }
  7871. if (anticlockwise) {
  7872. var tmp = endAngle;
  7873. endAngle = startAngle;
  7874. startAngle = tmp;
  7875. }
  7876. for (var angle = 0; angle < endAngle; angle += Math.PI / 2) {
  7877. if (angle > startAngle) {
  7878. extremity[0] = Math.cos(angle) * r + x;
  7879. extremity[1] = Math.sin(angle) * r + y;
  7880. Vector2.min(min, extremity, min);
  7881. Vector2.max(max, extremity, max);
  7882. }
  7883. }
  7884. return {
  7885. minX: min[0],
  7886. minY: min[1],
  7887. maxX: max[0],
  7888. maxY: max[1]
  7889. };
  7890. }
  7891. function getBBoxFromBezierGroup(points, lineWidth) {
  7892. var minX = Infinity;
  7893. var maxX = -Infinity;
  7894. var minY = Infinity;
  7895. var maxY = -Infinity;
  7896. for (var i = 0, len = points.length; i < len; i++) {
  7897. var bbox = cubicBezierBounds(points[i]);
  7898. if (bbox.minX < minX) {
  7899. minX = bbox.minX;
  7900. }
  7901. if (bbox.maxX > maxX) {
  7902. maxX = bbox.maxX;
  7903. }
  7904. if (bbox.minY < minY) {
  7905. minY = bbox.minY;
  7906. }
  7907. if (bbox.maxY > maxY) {
  7908. maxY = bbox.maxY;
  7909. }
  7910. }
  7911. lineWidth = lineWidth / 2 || 0;
  7912. return {
  7913. minX: minX - lineWidth,
  7914. minY: minY - lineWidth,
  7915. maxX: maxX + lineWidth,
  7916. maxY: maxY + lineWidth
  7917. };
  7918. }
  7919. class Line$1 extends Shape$2 {
  7920. _initProperties() {
  7921. super._initProperties();
  7922. this._attrs.canStroke = true;
  7923. this._attrs.type = 'line';
  7924. }
  7925. getDefaultAttrs() {
  7926. return {
  7927. x1: 0,
  7928. y1: 0,
  7929. x2: 0,
  7930. y2: 0,
  7931. lineWidth: 1
  7932. };
  7933. }
  7934. createPath(context) {
  7935. var attrs = this.get('attrs');
  7936. var {
  7937. x1,
  7938. y1,
  7939. x2,
  7940. y2
  7941. } = attrs;
  7942. context.beginPath();
  7943. context.moveTo(x1, y1);
  7944. context.lineTo(x2, y2);
  7945. }
  7946. calculateBox() {
  7947. var attrs = this.get('attrs');
  7948. var {
  7949. x1,
  7950. y1,
  7951. x2,
  7952. y2,
  7953. lineWidth
  7954. } = attrs;
  7955. return getBBoxFromLine(x1, y1, x2, y2, lineWidth);
  7956. }
  7957. }
  7958. Shape$2.Line = Line$1;
  7959. class Polygon extends Shape$2 {
  7960. _initProperties() {
  7961. super._initProperties();
  7962. this._attrs.canFill = true;
  7963. this._attrs.canStroke = true;
  7964. this._attrs.type = 'polygon';
  7965. }
  7966. getDefaultAttrs() {
  7967. return {
  7968. points: null,
  7969. lineWidth: 0
  7970. };
  7971. }
  7972. createPath(context) {
  7973. var self = this;
  7974. var attrs = self.get('attrs');
  7975. var points = attrs.points;
  7976. context.beginPath();
  7977. for (var i = 0, len = points.length; i < len; i++) {
  7978. var point = points[i];
  7979. if (i === 0) {
  7980. context.moveTo(point.x, point.y);
  7981. } else {
  7982. context.lineTo(point.x, point.y);
  7983. }
  7984. }
  7985. context.closePath();
  7986. }
  7987. calculateBox() {
  7988. var attrs = this.get('attrs');
  7989. var {
  7990. points
  7991. } = attrs;
  7992. return getBBoxFromPoints(points);
  7993. }
  7994. }
  7995. Shape$2.Polygon = Polygon;
  7996. /**
  7997. * @fileOverview convert the line to curve
  7998. * @author dxq613@gmail.com
  7999. */
  8000. function getPoint(v) {
  8001. return [v.x, v.y];
  8002. }
  8003. function smoothBezier(points, smooth, isLoop, constraint) {
  8004. var cps = [];
  8005. var prevPoint;
  8006. var nextPoint;
  8007. var hasConstraint = !!constraint;
  8008. var min;
  8009. var max;
  8010. var point;
  8011. var len;
  8012. var l;
  8013. var i;
  8014. if (hasConstraint) {
  8015. min = [Infinity, Infinity];
  8016. max = [-Infinity, -Infinity];
  8017. for (i = 0, l = points.length; i < l; i++) {
  8018. point = getPoint(points[i]);
  8019. Vector2.min(min, min, point);
  8020. Vector2.max(max, max, point);
  8021. }
  8022. Vector2.min(min, min, constraint[0]);
  8023. Vector2.max(max, max, constraint[1]);
  8024. }
  8025. for (i = 0, len = points.length; i < len; i++) {
  8026. point = getPoint(points[i]);
  8027. if (isLoop) {
  8028. prevPoint = getPoint(points[i ? i - 1 : len - 1]);
  8029. nextPoint = getPoint(points[(i + 1) % len]);
  8030. } else {
  8031. if (i === 0 || i === len - 1) {
  8032. cps.push([point[0], point[1]]);
  8033. continue;
  8034. } else {
  8035. prevPoint = getPoint(points[i - 1]);
  8036. nextPoint = getPoint(points[i + 1]);
  8037. }
  8038. }
  8039. var v = Vector2.sub([], nextPoint, prevPoint);
  8040. Vector2.scale(v, v, smooth);
  8041. var d0 = Vector2.distance(point, prevPoint);
  8042. var d1 = Vector2.distance(point, nextPoint);
  8043. var sum = d0 + d1;
  8044. if (sum !== 0) {
  8045. d0 /= sum;
  8046. d1 /= sum;
  8047. }
  8048. var v1 = Vector2.scale([], v, -d0);
  8049. var v2 = Vector2.scale([], v, d1);
  8050. var cp0 = Vector2.add([], point, v1);
  8051. var cp1 = Vector2.add([], point, v2);
  8052. if (hasConstraint) {
  8053. Vector2.max(cp0, cp0, min);
  8054. Vector2.min(cp0, cp0, max);
  8055. Vector2.max(cp1, cp1, min);
  8056. Vector2.min(cp1, cp1, max);
  8057. }
  8058. cps.push([cp0[0], cp0[1]]);
  8059. cps.push([cp1[0], cp1[1]]);
  8060. }
  8061. if (isLoop) {
  8062. cps.push(cps.shift());
  8063. }
  8064. return cps;
  8065. }
  8066. function catmullRom2bezier(pointList, z, constraint) {
  8067. var isLoop = !!z;
  8068. var controlPointList = smoothBezier(pointList, 0.4, isLoop, constraint);
  8069. var len = pointList.length;
  8070. var d1 = [];
  8071. var cp1;
  8072. var cp2;
  8073. var p;
  8074. for (var i = 0; i < len - 1; i++) {
  8075. cp1 = controlPointList[i * 2];
  8076. cp2 = controlPointList[i * 2 + 1];
  8077. p = pointList[i + 1];
  8078. d1.push(['C', cp1[0], cp1[1], cp2[0], cp2[1], p.x, p.y]);
  8079. }
  8080. if (isLoop) {
  8081. cp1 = controlPointList[len];
  8082. cp2 = controlPointList[len + 1];
  8083. p = pointList[0];
  8084. d1.push(['C', cp1[0], cp1[1], cp2[0], cp2[1], p.x, p.y]);
  8085. }
  8086. return d1;
  8087. }
  8088. function _filterPoints(points) {
  8089. var filteredPoints = [];
  8090. for (var i = 0, len = points.length; i < len; i++) {
  8091. var point = points[i];
  8092. if (!isNaN(point.x) && !isNaN(point.y)) {
  8093. filteredPoints.push(point);
  8094. }
  8095. }
  8096. return filteredPoints;
  8097. }
  8098. class Polyline extends Shape$2 {
  8099. _initProperties() {
  8100. super._initProperties();
  8101. this._attrs.canFill = true;
  8102. this._attrs.canStroke = true;
  8103. this._attrs.type = 'polyline';
  8104. }
  8105. getDefaultAttrs() {
  8106. return {
  8107. points: null,
  8108. lineWidth: 1,
  8109. smooth: false
  8110. };
  8111. }
  8112. createPath(context) {
  8113. var self = this;
  8114. var attrs = self.get('attrs');
  8115. var {
  8116. points,
  8117. smooth
  8118. } = attrs;
  8119. var filteredPoints = _filterPoints(points);
  8120. context.beginPath();
  8121. if (filteredPoints.length) {
  8122. context.moveTo(filteredPoints[0].x, filteredPoints[0].y);
  8123. if (smooth) {
  8124. var constaint = [[0, 0], [1, 1]];
  8125. var sps = catmullRom2bezier(filteredPoints, false, constaint);
  8126. for (var i = 0, n = sps.length; i < n; i++) {
  8127. var sp = sps[i];
  8128. context.bezierCurveTo(sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]);
  8129. }
  8130. } else {
  8131. var _i;
  8132. var l;
  8133. for (_i = 1, l = filteredPoints.length - 1; _i < l; _i++) {
  8134. context.lineTo(filteredPoints[_i].x, filteredPoints[_i].y);
  8135. }
  8136. context.lineTo(filteredPoints[l].x, filteredPoints[l].y);
  8137. }
  8138. }
  8139. }
  8140. calculateBox() {
  8141. var attrs = this.get('attrs');
  8142. var {
  8143. points,
  8144. smooth,
  8145. lineWidth
  8146. } = attrs;
  8147. var filteredPoints = _filterPoints(points);
  8148. if (smooth) {
  8149. var newPoints = [];
  8150. var constaint = [[0, 0], [1, 1]];
  8151. var sps = catmullRom2bezier(filteredPoints, false, constaint);
  8152. for (var i = 0, n = sps.length; i < n; i++) {
  8153. var sp = sps[i];
  8154. if (i === 0) {
  8155. newPoints.push([filteredPoints[0].x, filteredPoints[0].y, sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]]);
  8156. } else {
  8157. var lastPoint = sps[i - 1];
  8158. newPoints.push([lastPoint[5], lastPoint[6], sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]]);
  8159. }
  8160. }
  8161. return getBBoxFromBezierGroup(newPoints, lineWidth);
  8162. }
  8163. return getBBoxFromPoints(filteredPoints, lineWidth);
  8164. }
  8165. }
  8166. Shape$2.Polyline = Polyline;
  8167. class Arc extends Shape$2 {
  8168. _initProperties() {
  8169. super._initProperties();
  8170. this._attrs.canStroke = true;
  8171. this._attrs.canFill = true;
  8172. this._attrs.type = 'arc';
  8173. }
  8174. getDefaultAttrs() {
  8175. return {
  8176. x: 0,
  8177. y: 0,
  8178. r: 0,
  8179. startAngle: 0,
  8180. endAngle: Math.PI * 2,
  8181. anticlockwise: false,
  8182. lineWidth: 1
  8183. };
  8184. }
  8185. createPath(context) {
  8186. var attrs = this.get('attrs');
  8187. var {
  8188. x,
  8189. y,
  8190. r,
  8191. startAngle,
  8192. endAngle,
  8193. anticlockwise
  8194. } = attrs;
  8195. context.beginPath();
  8196. if (startAngle !== endAngle) {
  8197. context.arc(x, y, r, startAngle, endAngle, anticlockwise);
  8198. }
  8199. }
  8200. calculateBox() {
  8201. var attrs = this.get('attrs');
  8202. var {
  8203. x,
  8204. y,
  8205. r,
  8206. startAngle,
  8207. endAngle,
  8208. anticlockwise
  8209. } = attrs;
  8210. return getBBoxFromArc(x, y, r, startAngle, endAngle, anticlockwise);
  8211. }
  8212. }
  8213. Shape$2.Arc = Arc;
  8214. class Sector extends Shape$2 {
  8215. _initProperties() {
  8216. super._initProperties();
  8217. this._attrs.canFill = true;
  8218. this._attrs.canStroke = true;
  8219. this._attrs.type = 'sector';
  8220. }
  8221. getDefaultAttrs() {
  8222. return {
  8223. x: 0,
  8224. y: 0,
  8225. lineWidth: 0,
  8226. r: 0,
  8227. r0: 0,
  8228. startAngle: 0,
  8229. endAngle: Math.PI * 2,
  8230. anticlockwise: false
  8231. };
  8232. }
  8233. createPath(context) {
  8234. var attrs = this.get('attrs');
  8235. var {
  8236. x,
  8237. y,
  8238. startAngle,
  8239. endAngle,
  8240. r,
  8241. r0,
  8242. anticlockwise
  8243. } = attrs;
  8244. context.beginPath();
  8245. var unitX = Math.cos(startAngle);
  8246. var unitY = Math.sin(startAngle);
  8247. context.moveTo(unitX * r0 + x, unitY * r0 + y);
  8248. context.lineTo(unitX * r + x, unitY * r + y); // 当扇形的角度非常小的时候,就不进行弧线的绘制;或者整个只有1个扇形时,会出现end<0的情况不绘制
  8249. if (Math.abs(endAngle - startAngle) > 0.0001 || startAngle === 0 && endAngle < 0) {
  8250. context.arc(x, y, r, startAngle, endAngle, anticlockwise);
  8251. context.lineTo(Math.cos(endAngle) * r0 + x, Math.sin(endAngle) * r0 + y);
  8252. if (r0 !== 0) {
  8253. context.arc(x, y, r0, endAngle, startAngle, !anticlockwise);
  8254. }
  8255. }
  8256. context.closePath();
  8257. }
  8258. calculateBox() {
  8259. var attrs = this.get('attrs');
  8260. var {
  8261. x,
  8262. y,
  8263. r,
  8264. r0,
  8265. startAngle,
  8266. endAngle,
  8267. anticlockwise
  8268. } = attrs;
  8269. var outerBBox = getBBoxFromArc(x, y, r, startAngle, endAngle, anticlockwise);
  8270. var innerBBox = getBBoxFromArc(x, y, r0, startAngle, endAngle, anticlockwise);
  8271. return {
  8272. minX: Math.min(outerBBox.minX, innerBBox.minX),
  8273. minY: Math.min(outerBBox.minY, innerBBox.minY),
  8274. maxX: Math.max(outerBBox.maxX, innerBBox.maxX),
  8275. maxY: Math.max(outerBBox.maxY, innerBBox.maxY)
  8276. };
  8277. }
  8278. }
  8279. Shape$2.Sector = Sector;
  8280. var Rect$1 = {
  8281. calcRotatedBox(_ref) {
  8282. var {
  8283. width,
  8284. height,
  8285. rotate
  8286. } = _ref;
  8287. var absRotate = Math.abs(rotate);
  8288. return {
  8289. width: Math.abs(width * Math.cos(absRotate) + height * Math.sin(absRotate)),
  8290. height: Math.abs(height * Math.cos(absRotate) + width * Math.sin(absRotate))
  8291. };
  8292. }
  8293. };
  8294. var textWidthCacheCounter = 0;
  8295. var textWidthCache = {};
  8296. var TEXT_CACHE_MAX = 5000;
  8297. class Text extends Shape$2 {
  8298. _initProperties() {
  8299. super._initProperties();
  8300. this._attrs.canFill = true;
  8301. this._attrs.canStroke = true;
  8302. this._attrs.type = 'text';
  8303. }
  8304. getDefaultAttrs() {
  8305. return {
  8306. lineWidth: 0,
  8307. lineCount: 1,
  8308. fontSize: 12,
  8309. fontFamily: 'sans-serif',
  8310. fontStyle: 'normal',
  8311. fontWeight: 'normal',
  8312. fontVariant: 'normal',
  8313. textAlign: 'start',
  8314. textBaseline: 'bottom',
  8315. lineHeight: null,
  8316. textArr: null
  8317. };
  8318. }
  8319. _getFontStyle() {
  8320. var attrs = this._attrs.attrs;
  8321. var {
  8322. fontSize,
  8323. fontFamily,
  8324. fontWeight,
  8325. fontStyle,
  8326. fontVariant
  8327. } = attrs;
  8328. return "".concat(fontStyle, " ").concat(fontVariant, " ").concat(fontWeight, " ").concat(fontSize, "px ").concat(fontFamily);
  8329. }
  8330. _afterAttrsSet() {
  8331. var attrs = this._attrs.attrs;
  8332. attrs.font = this._getFontStyle();
  8333. if (attrs.text) {
  8334. var text = attrs.text;
  8335. var textArr = null;
  8336. var lineCount = 1;
  8337. if (isString(text) && text.indexOf('\n') !== -1) {
  8338. textArr = text.split('\n');
  8339. lineCount = textArr.length;
  8340. }
  8341. attrs.lineCount = lineCount;
  8342. attrs.textArr = textArr;
  8343. }
  8344. this.set('attrs', attrs);
  8345. }
  8346. _getTextHeight() {
  8347. var attrs = this._attrs.attrs;
  8348. if (attrs.height) {
  8349. return attrs.height;
  8350. }
  8351. var lineCount = attrs.lineCount;
  8352. var fontSize = attrs.fontSize * 1;
  8353. if (lineCount > 1) {
  8354. var spaceingY = this._getSpaceingY();
  8355. return fontSize * lineCount + spaceingY * (lineCount - 1);
  8356. }
  8357. return fontSize;
  8358. }
  8359. _getSpaceingY() {
  8360. var attrs = this._attrs.attrs;
  8361. var lineHeight = attrs.lineHeight;
  8362. var fontSize = attrs.fontSize * 1;
  8363. return lineHeight ? lineHeight - fontSize : fontSize * 0.14;
  8364. }
  8365. drawInner(context) {
  8366. var self = this;
  8367. var attrs = self._attrs.attrs;
  8368. var text = attrs.text;
  8369. var x = attrs.x;
  8370. var y = attrs.y;
  8371. if (isNil(text) || isNaN(x) || isNaN(y)) {
  8372. // text will be 0
  8373. return;
  8374. }
  8375. var textArr = attrs.textArr;
  8376. var fontSize = attrs.fontSize * 1;
  8377. var spaceingY = self._getSpaceingY();
  8378. if (attrs.rotate) {
  8379. // do rotation
  8380. context.translate(x, y);
  8381. context.rotate(attrs.rotate);
  8382. x = 0;
  8383. y = 0;
  8384. }
  8385. var textBaseline = attrs.textBaseline;
  8386. var height;
  8387. if (textArr) {
  8388. height = self._getTextHeight();
  8389. }
  8390. var subY; // context.beginPath();
  8391. if (self.hasFill()) {
  8392. var fillOpacity = attrs.fillOpacity;
  8393. if (!isNil(fillOpacity) && fillOpacity !== 1) {
  8394. context.globalAlpha = fillOpacity;
  8395. }
  8396. if (textArr) {
  8397. for (var i = 0, len = textArr.length; i < len; i++) {
  8398. var subText = textArr[i];
  8399. subY = y + i * (spaceingY + fontSize) - height + fontSize; // bottom;
  8400. if (textBaseline === 'middle') {
  8401. subY += height - fontSize - (height - fontSize) / 2;
  8402. }
  8403. if (textBaseline === 'top') {
  8404. subY += height - fontSize;
  8405. }
  8406. context.fillText(subText, x, subY);
  8407. }
  8408. } else {
  8409. context.fillText(text, x, y);
  8410. }
  8411. }
  8412. if (self.hasStroke()) {
  8413. if (textArr) {
  8414. for (var _i = 0, _len = textArr.length; _i < _len; _i++) {
  8415. var _subText = textArr[_i];
  8416. subY = y + _i * (spaceingY + fontSize) - height + fontSize; // bottom;
  8417. if (textBaseline === 'middle') {
  8418. subY += height - fontSize - (height - fontSize) / 2;
  8419. }
  8420. if (textBaseline === 'top') {
  8421. subY += height - fontSize;
  8422. }
  8423. context.strokeText(_subText, x, subY);
  8424. }
  8425. } else {
  8426. context.strokeText(text, x, y);
  8427. }
  8428. }
  8429. }
  8430. calculateBox() {
  8431. var self = this;
  8432. var attrs = self._attrs.attrs;
  8433. var {
  8434. x,
  8435. y,
  8436. textAlign,
  8437. textBaseline
  8438. } = attrs;
  8439. var width = self._getTextWidth(); // attrs.width
  8440. if (!width) {
  8441. return {
  8442. minX: x,
  8443. minY: y,
  8444. maxX: x,
  8445. maxY: y
  8446. };
  8447. }
  8448. var height = self._getTextHeight(); // attrs.height
  8449. if (attrs.rotate) {
  8450. var rotatedBox = Rect$1.calcRotatedBox({
  8451. width,
  8452. height,
  8453. rotate: attrs.rotate
  8454. });
  8455. width = rotatedBox.width;
  8456. height = rotatedBox.height;
  8457. }
  8458. var point = {
  8459. x,
  8460. y: y - height
  8461. }; // default textAlign: start, textBaseline: bottom
  8462. if (textAlign) {
  8463. if (textAlign === 'end' || textAlign === 'right') {
  8464. point.x -= width;
  8465. } else if (textAlign === 'center') {
  8466. point.x -= width / 2;
  8467. }
  8468. }
  8469. if (textBaseline) {
  8470. if (textBaseline === 'top') {
  8471. point.y += height;
  8472. } else if (textBaseline === 'middle') {
  8473. point.y += height / 2;
  8474. }
  8475. }
  8476. return {
  8477. minX: point.x,
  8478. minY: point.y,
  8479. maxX: point.x + width,
  8480. maxY: point.y + height
  8481. };
  8482. }
  8483. _getTextWidth() {
  8484. var attrs = this._attrs.attrs;
  8485. if (attrs.width) {
  8486. return attrs.width;
  8487. }
  8488. var text = attrs.text;
  8489. var context = this.get('context');
  8490. if (isNil(text)) return undefined;
  8491. var font = attrs.font;
  8492. var textArr = attrs.textArr;
  8493. var key = text + '' + font;
  8494. if (textWidthCache[key]) {
  8495. return textWidthCache[key];
  8496. }
  8497. var width = 0;
  8498. if (textArr) {
  8499. for (var i = 0, length = textArr.length; i < length; i++) {
  8500. var subText = textArr[i];
  8501. width = Math.max(width, measureText(subText, font, context).width);
  8502. }
  8503. } else {
  8504. width = measureText(text, font, context).width;
  8505. }
  8506. if (textWidthCacheCounter > TEXT_CACHE_MAX) {
  8507. textWidthCacheCounter = 0;
  8508. textWidthCache = {};
  8509. }
  8510. textWidthCacheCounter++;
  8511. textWidthCache[key] = width;
  8512. return width;
  8513. }
  8514. }
  8515. Shape$2.Text = Text;
  8516. class Custom extends Shape$2 {
  8517. _initProperties() {
  8518. super._initProperties();
  8519. this._attrs.canFill = true;
  8520. this._attrs.canStroke = true;
  8521. this._attrs.createPath = null;
  8522. this._attrs.type = 'custom';
  8523. }
  8524. createPath(context) {
  8525. var createPath = this.get('createPath');
  8526. createPath && createPath.call(this, context);
  8527. }
  8528. calculateBox() {
  8529. var calculateBox = this.get('calculateBox');
  8530. return calculateBox && calculateBox.call(this);
  8531. }
  8532. }
  8533. Shape$2.Custom = Custom;
  8534. var G = /*#__PURE__*/Object.freeze({
  8535. __proto__: null,
  8536. Canvas: Canvas,
  8537. Group: Group,
  8538. Shape: Shape$2,
  8539. Matrix: Matrix,
  8540. Vector2: Vector2
  8541. });
  8542. function formatTicks(ticks) {
  8543. var tmp = ticks.slice(0);
  8544. if (tmp.length > 0) {
  8545. var first = tmp[0];
  8546. var last = tmp[tmp.length - 1];
  8547. if (first.value !== 0) {
  8548. tmp.unshift({
  8549. value: 0
  8550. });
  8551. }
  8552. if (last.value !== 1) {
  8553. tmp.push({
  8554. value: 1
  8555. });
  8556. }
  8557. }
  8558. return tmp;
  8559. }
  8560. class AxisController {
  8561. constructor(cfg) {
  8562. this.axisCfg = {};
  8563. this.frontPlot = null;
  8564. this.backPlot = null;
  8565. this.axes = {}; // store the axes's options
  8566. mix(this, cfg);
  8567. }
  8568. _isHide(field) {
  8569. var axisCfg = this.axisCfg;
  8570. return !axisCfg || axisCfg[field] === false;
  8571. }
  8572. _getLinePosition(scale, dimType, index, transposed) {
  8573. var position = '';
  8574. var field = scale.field;
  8575. var axisCfg = this.axisCfg;
  8576. if (axisCfg[field] && axisCfg[field].position) {
  8577. position = axisCfg[field].position;
  8578. } else if (dimType === 'x') {
  8579. position = transposed ? 'left' : 'bottom';
  8580. } else if (dimType === 'y') {
  8581. position = index ? 'right' : 'left';
  8582. if (transposed) {
  8583. position = 'bottom';
  8584. }
  8585. }
  8586. return position;
  8587. }
  8588. _getLineCfg(coord, dimType, position) {
  8589. var start;
  8590. var end;
  8591. var factor = 1; // Mark clockwise or counterclockwise
  8592. if (dimType === 'x') {
  8593. start = {
  8594. x: 0,
  8595. y: 0
  8596. };
  8597. end = {
  8598. x: 1,
  8599. y: 0
  8600. };
  8601. } else {
  8602. if (position === 'right') {
  8603. // there will be several y axes
  8604. start = {
  8605. x: 1,
  8606. y: 0
  8607. };
  8608. end = {
  8609. x: 1,
  8610. y: 1
  8611. };
  8612. } else {
  8613. start = {
  8614. x: 0,
  8615. y: 0
  8616. };
  8617. end = {
  8618. x: 0,
  8619. y: 1
  8620. };
  8621. factor = -1;
  8622. }
  8623. }
  8624. if (coord.transposed) {
  8625. factor *= -1;
  8626. }
  8627. return {
  8628. offsetFactor: factor,
  8629. start: coord.convertPoint(start),
  8630. end: coord.convertPoint(end)
  8631. };
  8632. }
  8633. _getCircleCfg(coord) {
  8634. return {
  8635. startAngle: coord.startAngle,
  8636. endAngle: coord.endAngle,
  8637. center: coord.center,
  8638. radius: coord.circleRadius
  8639. };
  8640. }
  8641. _getRadiusCfg(coord) {
  8642. var transposed = coord.transposed;
  8643. var start;
  8644. var end;
  8645. if (transposed) {
  8646. start = {
  8647. x: 0,
  8648. y: 0
  8649. };
  8650. end = {
  8651. x: 1,
  8652. y: 0
  8653. };
  8654. } else {
  8655. start = {
  8656. x: 0,
  8657. y: 0
  8658. };
  8659. end = {
  8660. x: 0,
  8661. y: 1
  8662. };
  8663. }
  8664. return {
  8665. offsetFactor: -1,
  8666. start: coord.convertPoint(start),
  8667. end: coord.convertPoint(end)
  8668. };
  8669. }
  8670. _getAxisCfg(coord, scale, verticalScale, dimType, defaultCfg) {
  8671. var self = this;
  8672. var axisCfg = this.axisCfg;
  8673. var ticks = scale.getTicks();
  8674. var cfg = deepMix({
  8675. ticks,
  8676. frontContainer: this.frontPlot,
  8677. backContainer: this.backPlot
  8678. }, defaultCfg, axisCfg[scale.field]);
  8679. var labels = [];
  8680. var label = cfg.label;
  8681. var count = ticks.length;
  8682. var maxWidth = 0;
  8683. var maxHeight = 0;
  8684. var labelCfg = label;
  8685. each(ticks, function (tick, index) {
  8686. if (isFunction(label)) {
  8687. var executedLabel = label(tick.text, index, count);
  8688. labelCfg = executedLabel ? mix({}, Global._defaultAxis.label, executedLabel) : null;
  8689. }
  8690. if (labelCfg) {
  8691. var textStyle = {};
  8692. if (labelCfg.textAlign) {
  8693. textStyle.textAlign = labelCfg.textAlign;
  8694. }
  8695. if (labelCfg.textBaseline) {
  8696. textStyle.textBaseline = labelCfg.textBaseline;
  8697. }
  8698. var axisLabel = new Shape$2.Text({
  8699. className: 'axis-label',
  8700. attrs: mix({
  8701. x: 0,
  8702. y: 0,
  8703. text: tick.text,
  8704. fontFamily: self.chart.get('canvas').get('fontFamily')
  8705. }, labelCfg),
  8706. value: tick.value,
  8707. textStyle,
  8708. top: labelCfg.top,
  8709. context: self.chart.get('canvas').get('context')
  8710. });
  8711. labels.push(axisLabel);
  8712. var {
  8713. width,
  8714. height
  8715. } = axisLabel.getBBox();
  8716. maxWidth = Math.max(maxWidth, width);
  8717. maxHeight = Math.max(maxHeight, height);
  8718. }
  8719. });
  8720. cfg.labels = labels;
  8721. cfg.maxWidth = maxWidth;
  8722. cfg.maxHeight = maxHeight;
  8723. return cfg;
  8724. }
  8725. _createAxis(coord, scale, verticalScale, dimType) {
  8726. var index = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : '';
  8727. var self = this;
  8728. var coordType = coord.type;
  8729. var transposed = coord.transposed;
  8730. var type;
  8731. var key;
  8732. var defaultCfg;
  8733. if (coordType === 'cartesian' || coordType === 'rect') {
  8734. var position = self._getLinePosition(scale, dimType, index, transposed);
  8735. defaultCfg = Global.axis[position];
  8736. defaultCfg.position = position;
  8737. type = 'Line';
  8738. key = position;
  8739. } else {
  8740. if (dimType === 'x' && !transposed || dimType === 'y' && transposed) {
  8741. defaultCfg = Global.axis.circle;
  8742. type = 'Circle';
  8743. key = 'circle';
  8744. } else {
  8745. defaultCfg = Global.axis.radius;
  8746. type = 'Line';
  8747. key = 'radius';
  8748. }
  8749. }
  8750. var cfg = self._getAxisCfg(coord, scale, verticalScale, dimType, defaultCfg);
  8751. cfg.type = type;
  8752. cfg.dimType = dimType;
  8753. cfg.verticalScale = verticalScale;
  8754. cfg.index = index;
  8755. this.axes[key] = cfg;
  8756. }
  8757. createAxis(coord, xScale, yScales) {
  8758. var self = this;
  8759. if (xScale && !self._isHide(xScale.field)) {
  8760. self._createAxis(coord, xScale, yScales[0], 'x');
  8761. }
  8762. each(yScales, function (yScale, index) {
  8763. if (!self._isHide(yScale.field)) {
  8764. self._createAxis(coord, yScale, xScale, 'y', index);
  8765. }
  8766. });
  8767. var axes = this.axes;
  8768. var chart = self.chart;
  8769. if (chart._isAutoPadding()) {
  8770. var userPadding = parsePadding(chart.get('padding'));
  8771. var appendPadding = parsePadding(chart.get('appendPadding'));
  8772. var legendRange = chart.get('legendRange') || {
  8773. top: 0,
  8774. right: 0,
  8775. bottom: 0,
  8776. left: 0
  8777. };
  8778. var padding = [userPadding[0] === 'auto' ? legendRange.top + appendPadding[0] * 2 : userPadding[0], userPadding[1] === 'auto' ? legendRange.right + appendPadding[1] : userPadding[1], userPadding[2] === 'auto' ? legendRange.bottom + appendPadding[2] : userPadding[2], userPadding[3] === 'auto' ? legendRange.left + appendPadding[3] : userPadding[3]];
  8779. if (coord.isPolar) {
  8780. var circleAxis = axes.circle;
  8781. if (circleAxis) {
  8782. var {
  8783. maxHeight,
  8784. maxWidth,
  8785. labelOffset
  8786. } = circleAxis;
  8787. padding[0] += maxHeight + labelOffset;
  8788. padding[1] += maxWidth + labelOffset;
  8789. padding[2] += maxHeight + labelOffset;
  8790. padding[3] += maxWidth + labelOffset;
  8791. }
  8792. } else {
  8793. if (axes.right && userPadding[1] === 'auto') {
  8794. var {
  8795. maxWidth: _maxWidth,
  8796. labelOffset: _labelOffset
  8797. } = axes.right;
  8798. padding[1] += _maxWidth + _labelOffset;
  8799. }
  8800. if (axes.left && userPadding[3] === 'auto') {
  8801. var {
  8802. maxWidth: _maxWidth2,
  8803. labelOffset: _labelOffset2
  8804. } = axes.left;
  8805. padding[3] += _maxWidth2 + _labelOffset2;
  8806. }
  8807. if (axes.bottom && userPadding[2] === 'auto') {
  8808. var {
  8809. maxHeight: _maxHeight,
  8810. labelOffset: _labelOffset3
  8811. } = axes.bottom;
  8812. padding[2] += _maxHeight + _labelOffset3;
  8813. }
  8814. }
  8815. chart.set('_padding', padding);
  8816. chart._updateLayout(padding);
  8817. }
  8818. each(axes, function (axis) {
  8819. var {
  8820. type,
  8821. grid,
  8822. verticalScale,
  8823. ticks,
  8824. dimType,
  8825. position,
  8826. index
  8827. } = axis;
  8828. var appendCfg;
  8829. if (coord.isPolar) {
  8830. if (type === 'Line') {
  8831. appendCfg = self._getRadiusCfg(coord);
  8832. } else if (type === 'Circle') {
  8833. appendCfg = self._getCircleCfg(coord);
  8834. }
  8835. } else {
  8836. appendCfg = self._getLineCfg(coord, dimType, position);
  8837. }
  8838. if (grid && verticalScale) {
  8839. var gridPoints = [];
  8840. var verticalTicks = formatTicks(verticalScale.getTicks());
  8841. each(ticks, function (tick) {
  8842. var subPoints = [];
  8843. each(verticalTicks, function (verticalTick) {
  8844. var x = dimType === 'x' ? tick.value : verticalTick.value;
  8845. var y = dimType === 'x' ? verticalTick.value : tick.value;
  8846. if (x >= 0 && x <= 1 && y >= 0 && y <= 1) {
  8847. var point = coord.convertPoint({
  8848. x,
  8849. y
  8850. });
  8851. subPoints.push(point);
  8852. }
  8853. });
  8854. gridPoints.push({
  8855. points: subPoints,
  8856. _id: 'axis-' + dimType + index + '-grid-' + tick.tickValue
  8857. });
  8858. });
  8859. axis.gridPoints = gridPoints;
  8860. if (coord.isPolar) {
  8861. axis.center = coord.center;
  8862. axis.startAngle = coord.startAngle;
  8863. axis.endAngle = coord.endAngle;
  8864. }
  8865. }
  8866. appendCfg._id = 'axis-' + dimType;
  8867. if (!isNil(index)) {
  8868. appendCfg._id = 'axis-' + dimType + index;
  8869. }
  8870. new Abastract[type](mix(axis, appendCfg));
  8871. });
  8872. }
  8873. clear() {
  8874. this.axes = {};
  8875. this.frontPlot.clear();
  8876. this.backPlot.clear();
  8877. }
  8878. }
  8879. function getClip(coord) {
  8880. var start = coord.start;
  8881. var end = coord.end;
  8882. var width = end.x - start.x;
  8883. var height = Math.abs(end.y - start.y);
  8884. var margin = 10;
  8885. var clip;
  8886. if (coord.isPolar) {
  8887. var {
  8888. circleRadius,
  8889. center,
  8890. startAngle,
  8891. endAngle
  8892. } = coord;
  8893. clip = new Shape$2.Sector({
  8894. attrs: {
  8895. x: center.x,
  8896. y: center.y,
  8897. r: circleRadius,
  8898. r0: 0,
  8899. startAngle,
  8900. endAngle
  8901. }
  8902. });
  8903. } else {
  8904. clip = new Shape$2.Rect({
  8905. attrs: {
  8906. x: start.x,
  8907. y: end.y - margin,
  8908. width,
  8909. height: height + 2 * margin
  8910. }
  8911. });
  8912. }
  8913. clip.isClip = true;
  8914. return clip;
  8915. }
  8916. function isPointInPlot(point, plot) {
  8917. var {
  8918. x,
  8919. y
  8920. } = point;
  8921. var {
  8922. tl,
  8923. tr,
  8924. br
  8925. } = plot;
  8926. return x >= tl.x && x <= tr.x && y >= tl.y && y <= br.y;
  8927. }
  8928. var Helper = /*#__PURE__*/Object.freeze({
  8929. __proto__: null,
  8930. getClip: getClip,
  8931. isPointInPlot: isPointInPlot
  8932. });
  8933. function compare(a, b) {
  8934. return a - b;
  8935. }
  8936. function _isScaleExist(scales, compareScale) {
  8937. var flag = false;
  8938. each(scales, function (scale) {
  8939. var scaleValues = [].concat(scale.values);
  8940. var compareScaleValues = [].concat(compareScale.values);
  8941. if (scale.type === compareScale.type && scale.field === compareScale.field && scaleValues.sort(compare).toString() === compareScaleValues.sort(compare).toString()) {
  8942. flag = true;
  8943. return;
  8944. }
  8945. });
  8946. return flag;
  8947. }
  8948. class Chart extends Base {
  8949. static initPlugins() {
  8950. return {
  8951. _plugins: [],
  8952. _cacheId: 0,
  8953. register(plugins) {
  8954. var p = this._plugins;
  8955. [].concat(plugins).forEach(function (plugin) {
  8956. if (p.indexOf(plugin) === -1) {
  8957. p.push(plugin);
  8958. }
  8959. });
  8960. this._cacheId++;
  8961. },
  8962. unregister(plugins) {
  8963. var p = this._plugins;
  8964. [].concat(plugins).forEach(function (plugin) {
  8965. var idx = p.indexOf(plugin);
  8966. if (idx !== -1) {
  8967. p.splice(idx, 1);
  8968. }
  8969. });
  8970. this._cacheId++;
  8971. },
  8972. clear() {
  8973. this._plugins = [];
  8974. this._cacheId++;
  8975. },
  8976. count() {
  8977. return this._plugins.length;
  8978. },
  8979. getAll() {
  8980. return this._plugins;
  8981. },
  8982. notify(chart, hook, args) {
  8983. var descriptors = this.descriptors(chart);
  8984. var ilen = descriptors.length;
  8985. var i;
  8986. var descriptor;
  8987. var plugin;
  8988. var params;
  8989. var method;
  8990. for (i = 0; i < ilen; ++i) {
  8991. descriptor = descriptors[i];
  8992. plugin = descriptor.plugin;
  8993. method = plugin[hook];
  8994. if (typeof method === 'function') {
  8995. params = [chart].concat(args || []);
  8996. if (method.apply(plugin, params) === false) {
  8997. return false;
  8998. }
  8999. }
  9000. }
  9001. return true;
  9002. },
  9003. descriptors(chart) {
  9004. var cache = chart._plugins || (chart._plugins = {});
  9005. if (cache.id === this._cacheId) {
  9006. return cache.descriptors;
  9007. }
  9008. var plugins = [];
  9009. var descriptors = [];
  9010. this._plugins.concat(chart && chart.get('plugins') || []).forEach(function (plugin) {
  9011. var idx = plugins.indexOf(plugin);
  9012. if (idx !== -1) {
  9013. return;
  9014. }
  9015. plugins.push(plugin);
  9016. descriptors.push({
  9017. plugin
  9018. });
  9019. });
  9020. cache.descriptors = descriptors;
  9021. cache.id = this._cacheId;
  9022. return descriptors;
  9023. }
  9024. };
  9025. }
  9026. getDefaultCfg() {
  9027. return {
  9028. /**
  9029. * the id of canvas
  9030. * @type {String}
  9031. */
  9032. id: null,
  9033. rendered: false,
  9034. /**
  9035. * padding
  9036. * @type {Array|Number}
  9037. */
  9038. padding: Global.padding,
  9039. /**
  9040. * data
  9041. * @type {Array}
  9042. */
  9043. data: null,
  9044. /**
  9045. * scales of chart
  9046. * @type {Object}
  9047. */
  9048. scales: {},
  9049. /**
  9050. * @private
  9051. * geometry instances
  9052. * @type {Array}
  9053. */
  9054. geoms: [],
  9055. /**
  9056. * scale configuration
  9057. * @type {Object}
  9058. */
  9059. colDefs: null,
  9060. pixelRatio: Global.pixelRatio,
  9061. /**
  9062. * filter options
  9063. * @type {Object}
  9064. */
  9065. filters: null,
  9066. appendPadding: Global.appendPadding
  9067. };
  9068. }
  9069. _syncYScales() {
  9070. var syncY = this.get('syncY');
  9071. if (!syncY) {
  9072. return;
  9073. }
  9074. var geoms = this.get('geoms');
  9075. var syncScales = [];
  9076. var min = [];
  9077. var max = [];
  9078. each(geoms, function (geom) {
  9079. var yScale = geom.getYScale();
  9080. if (yScale.isLinear) {
  9081. syncScales.push(yScale);
  9082. min.push(yScale.min);
  9083. max.push(yScale.max);
  9084. }
  9085. });
  9086. min = Math.min.apply(null, min);
  9087. max = Math.max.apply(null, max);
  9088. each(syncScales, function (scale) {
  9089. scale.change({
  9090. min
  9091. });
  9092. scale.change({
  9093. max
  9094. });
  9095. });
  9096. }
  9097. _getFieldsForLegend() {
  9098. var fields = [];
  9099. var geoms = this.get('geoms');
  9100. each(geoms, function (geom) {
  9101. var attrOptions = geom.get('attrOptions');
  9102. var attrCfg = attrOptions.color;
  9103. if (attrCfg && attrCfg.field && isString(attrCfg.field)) {
  9104. var arr = attrCfg.field.split('*');
  9105. each(arr, function (item) {
  9106. if (fields.indexOf(item) === -1) {
  9107. fields.push(item);
  9108. }
  9109. });
  9110. }
  9111. });
  9112. return fields;
  9113. }
  9114. _getScaleData(field) {
  9115. var data = this.get('data');
  9116. var filteredData = this.get('filteredData');
  9117. if (filteredData.length) {
  9118. var legendFields = this._getFieldsForLegend();
  9119. if (legendFields.indexOf(field) === -1) {
  9120. data = filteredData;
  9121. }
  9122. }
  9123. return data;
  9124. } // _updateScales() {
  9125. // const scaleController = this.get('scaleController');
  9126. // scaleController.updateScales();
  9127. // this._adjustScale();
  9128. // }
  9129. _adjustScale() {
  9130. var self = this;
  9131. var scaleController = self.get('scaleController'); // 看起来是为了让柱状图最小或最大都默认从0开始
  9132. var geoms = this.get('geoms');
  9133. for (var i = 0; i < geoms.length; i++) {
  9134. var geom = geoms[i];
  9135. if (geom.get('type') === 'interval') {
  9136. var yScale = geom.getYScale();
  9137. scaleController.adjustStartZero(yScale);
  9138. }
  9139. }
  9140. }
  9141. _removeGeoms() {
  9142. var geoms = this.get('geoms');
  9143. while (geoms.length > 0) {
  9144. var geom = geoms.shift();
  9145. geom.destroy();
  9146. }
  9147. }
  9148. _clearGeoms() {
  9149. var geoms = this.get('geoms');
  9150. for (var i = 0, length = geoms.length; i < length; i++) {
  9151. var geom = geoms[i];
  9152. geom.clear();
  9153. }
  9154. }
  9155. _clearInner() {
  9156. this._clearGeoms();
  9157. Chart.plugins.notify(this, 'clearInner');
  9158. this.emit(EVENT_CLEAR_INNER);
  9159. this.get('axisController') && this.get('axisController').clear();
  9160. }
  9161. _initFilteredData() {
  9162. var filters = this.get('filters');
  9163. var data = this.get('data') || [];
  9164. if (filters) {
  9165. data = data.filter(function (obj) {
  9166. var rst = true;
  9167. each(filters, function (fn, k) {
  9168. if (fn) {
  9169. rst = fn(obj[k], obj);
  9170. if (!rst) {
  9171. return false;
  9172. }
  9173. }
  9174. });
  9175. return rst;
  9176. });
  9177. }
  9178. this.set('filteredData', data);
  9179. }
  9180. _changeGeomsData() {
  9181. var geoms = this.get('geoms');
  9182. var data = this.get('filteredData');
  9183. for (var i = 0, length = geoms.length; i < length; i++) {
  9184. var geom = geoms[i];
  9185. geom.changeData(data);
  9186. }
  9187. }
  9188. _initGeom(geom) {
  9189. if (geom.get('isInit')) {
  9190. return;
  9191. }
  9192. var coord = this.get('coord');
  9193. var data = this.get('filteredData');
  9194. var colDefs = this.get('colDefs');
  9195. var middlePlot = this.get('middlePlot');
  9196. geom.set('chart', this);
  9197. geom.set('container', middlePlot.addGroup());
  9198. geom.set('data', data);
  9199. geom.set('coord', coord);
  9200. geom.set('colDefs', colDefs);
  9201. geom.init();
  9202. this.emit(EVENT_AFTER_GEOM_INIT, geom);
  9203. }
  9204. _initGeoms() {
  9205. var geoms = this.get('geoms');
  9206. for (var i = 0, length = geoms.length; i < length; i++) {
  9207. this._initGeom(geoms[i]);
  9208. }
  9209. }
  9210. _initCoord() {
  9211. var plot = this.get('plotRange');
  9212. var coordCfg = mix({
  9213. type: 'cartesian'
  9214. }, this.get('coordCfg'), {
  9215. plot
  9216. });
  9217. var type = coordCfg.type;
  9218. var C = Base$1[upperFirst(type)];
  9219. var coord = new C(coordCfg);
  9220. this.set('coord', coord);
  9221. }
  9222. _initLayout() {
  9223. var padding = this.get('_padding');
  9224. if (!padding) {
  9225. padding = this.get('margin') || this.get('padding');
  9226. padding = parsePadding(padding);
  9227. }
  9228. var top = padding[0] === 'auto' ? 0 : padding[0];
  9229. var right = padding[1] === 'auto' ? 0 : padding[1];
  9230. var bottom = padding[2] === 'auto' ? 0 : padding[2];
  9231. var left = padding[3] === 'auto' ? 0 : padding[3];
  9232. var width = this.get('width');
  9233. var height = this.get('height');
  9234. var start = {
  9235. x: left,
  9236. y: top
  9237. };
  9238. var end = {
  9239. x: width - right,
  9240. y: height - bottom
  9241. };
  9242. var plot = this.get('plot');
  9243. if (plot) {
  9244. plot.reset(start, end);
  9245. return;
  9246. }
  9247. var newPlot = new Plot({
  9248. start,
  9249. end
  9250. });
  9251. this.set('plotRange', newPlot);
  9252. this.set('plot', newPlot);
  9253. }
  9254. _initCanvas() {
  9255. var self = this;
  9256. try {
  9257. var canvas = new Canvas({
  9258. el: self.get('el') || self.get('id'),
  9259. context: self.get('context'),
  9260. pixelRatio: self.get('pixelRatio'),
  9261. width: self.get('width'),
  9262. height: self.get('height'),
  9263. fontFamily: Global.fontFamily
  9264. });
  9265. self.set('canvas', canvas);
  9266. self.set('el', canvas.get('el'));
  9267. self.set('width', canvas.get('width'));
  9268. self.set('height', canvas.get('height'));
  9269. } catch (error) {
  9270. throw error;
  9271. }
  9272. Chart.plugins.notify(self, 'afterCanvasInit');
  9273. }
  9274. _initLayers() {
  9275. var canvas = this.get('canvas');
  9276. this.set('backPlot', canvas.addGroup());
  9277. this.set('middlePlot', canvas.addGroup({
  9278. zIndex: 10
  9279. }));
  9280. this.set('frontPlot', canvas.addGroup({
  9281. zIndex: 20
  9282. }));
  9283. }
  9284. _initEvents() {
  9285. var _this = this;
  9286. // 数据更新后的一些更新
  9287. this.on(EVENT_AFTER_DATA_CHANGE, function () {
  9288. // 数据更新后,重新设置filterdata
  9289. _this._initFilteredData(); // 更新geoms里的数据
  9290. _this._changeGeomsData();
  9291. }); // 大小变化后的一些更新
  9292. this.on(EVENT_AFTER_SIZE_CHANGE, function () {
  9293. _this._initLayout(); // layout变化后,坐标轴也需要做相应的变化
  9294. var coord = _this.get('coord');
  9295. if (coord) {
  9296. coord.reset(_this.get('plot'));
  9297. }
  9298. });
  9299. }
  9300. _initScaleController() {
  9301. var scaleController = new ScaleController({
  9302. chart: this
  9303. }); // 让colDefs 和 scaleController.defs 用同一个对象,这样就不用考虑同步的问题
  9304. this.set('colDefs', scaleController.defs); // 已经实例化的scales 也保持统一个对象
  9305. this.set('scales', scaleController.scales);
  9306. this.set('scaleController', scaleController);
  9307. }
  9308. _clearScaleController() {
  9309. var scaleController = this.get('scaleController');
  9310. scaleController.clear();
  9311. }
  9312. _init() {
  9313. var self = this;
  9314. self._initCanvas();
  9315. self._initLayout();
  9316. self._initLayers();
  9317. self._initEvents();
  9318. self._initScaleController();
  9319. self.set('axisController', new AxisController({
  9320. frontPlot: self.get('frontPlot').addGroup({
  9321. className: 'axisContainer'
  9322. }),
  9323. backPlot: self.get('backPlot').addGroup({
  9324. className: 'axisContainer'
  9325. }),
  9326. chart: self
  9327. }));
  9328. Chart.plugins.notify(self, 'init');
  9329. }
  9330. constructor(cfg) {
  9331. super(cfg);
  9332. var self = this;
  9333. each(Geom, function (geomConstructor, className) {
  9334. var methodName = lowerFirst(className);
  9335. self[methodName] = function (cfg) {
  9336. var geom = new geomConstructor(cfg);
  9337. self.addGeom(geom);
  9338. return geom;
  9339. };
  9340. });
  9341. self._init();
  9342. }
  9343. init() {
  9344. // 初始filterData
  9345. this._initFilteredData(); // initialization coordinate instance
  9346. this._initCoord();
  9347. Chart.plugins.notify(this, 'beforeGeomInit'); // init all geometry instances
  9348. this._initGeoms(); // 多 Y 轴的情况时,统一 Y 轴的数值范围。
  9349. this._syncYScales(); // do some adjust for data
  9350. this._adjustScale();
  9351. this.emit(EVENT_AFTER_INIT);
  9352. }
  9353. /**
  9354. * set data and some scale configuration
  9355. * @chainable
  9356. * @param {Array} data the dataset to visualize
  9357. * @param {Object} colDefs the configuration for scales
  9358. * @return {Chart} return the chart instance
  9359. */
  9360. source(data, colDefs) {
  9361. this.set('data', data);
  9362. if (colDefs) {
  9363. this.scale(colDefs);
  9364. }
  9365. return this;
  9366. }
  9367. scale(field, cfg) {
  9368. var scaleController = this.get('scaleController');
  9369. scaleController.setFieldDef(field, cfg);
  9370. return this;
  9371. }
  9372. /**
  9373. * configure the axis
  9374. * @chainable
  9375. * @param {String|Boolean} field the field name of data
  9376. * @param {Object} cfg configuration for axis
  9377. * @return {Chart} return the chart instance
  9378. */
  9379. axis(field, cfg) {
  9380. var axisController = this.get('axisController');
  9381. if (!field) {
  9382. axisController.axisCfg = null;
  9383. } else {
  9384. axisController.axisCfg = axisController.axisCfg || {};
  9385. axisController.axisCfg[field] = cfg;
  9386. }
  9387. return this;
  9388. }
  9389. /**
  9390. * configure the coordinate
  9391. * @chainable
  9392. * @param {String} type set the type of coodinate
  9393. * @param {Object} cfg configuration for coordinate
  9394. * @return {Chart} return the chart instance
  9395. */
  9396. coord(type, cfg) {
  9397. var coordCfg;
  9398. if (isObject(type)) {
  9399. coordCfg = type;
  9400. } else {
  9401. coordCfg = cfg || {};
  9402. coordCfg.type = type || 'cartesian';
  9403. }
  9404. this.set('coordCfg', coordCfg);
  9405. return this;
  9406. }
  9407. filter(field, condition) {
  9408. var filters = this.get('filters') || {};
  9409. filters[field] = condition;
  9410. this.set('filters', filters); // 如果已经render过,则再重新触发一次change
  9411. if (this.get('rendered')) {
  9412. this.emit(EVENT_AFTER_DATA_CHANGE, this.get('data'));
  9413. }
  9414. }
  9415. /**
  9416. * render the chart
  9417. * @chainable
  9418. * @return {Chart} return the chart instance
  9419. */
  9420. render() {
  9421. var rendered = this.get('rendered');
  9422. var canvas = this.get('canvas');
  9423. var geoms = this.get('geoms'); // 已经渲染过
  9424. if (rendered) {
  9425. this._initGeoms();
  9426. this._adjustScale();
  9427. } else {
  9428. this.init();
  9429. this.set('rendered', true);
  9430. }
  9431. this.emit(EVENT_BEFORE_RENDER);
  9432. Chart.plugins.notify(this, 'beforeGeomDraw');
  9433. this._renderAxis();
  9434. var middlePlot = this.get('middlePlot');
  9435. if (this.get('limitInPlot') && !middlePlot.attr('clip')) {
  9436. var coord = this.get('coord');
  9437. var clip = getClip(coord);
  9438. clip.set('canvas', middlePlot.get('canvas'));
  9439. middlePlot.attr('clip', clip);
  9440. }
  9441. this.emit(EVENT_BEFORE_GEOM_DRAW);
  9442. for (var i = 0, length = geoms.length; i < length; i++) {
  9443. var geom = geoms[i];
  9444. geom.paint();
  9445. }
  9446. this.emit(EVENT_AFTER_GEOM_DRAW);
  9447. Chart.plugins.notify(this, 'afterGeomDraw');
  9448. canvas.sort();
  9449. this.get('frontPlot').sort();
  9450. Chart.plugins.notify(this, 'beforeCanvasDraw');
  9451. canvas.draw();
  9452. this.emit(EVENT_AFTER_RENDER);
  9453. return this;
  9454. }
  9455. /**
  9456. * clear the chart, include geometris and all the shapes
  9457. * @chainable
  9458. * @return {Chart} return the chart
  9459. */
  9460. clear() {
  9461. Chart.plugins.notify(this, 'clear');
  9462. this.emit(EVENT_CLEAR);
  9463. this._clearInner();
  9464. this._removeGeoms();
  9465. this._clearScaleController();
  9466. this.set('legendItems', null);
  9467. this.set('filters', null);
  9468. this.set('isUpdate', false);
  9469. this.set('_padding', null);
  9470. this.set('rendered', false);
  9471. var canvas = this.get('canvas');
  9472. canvas.draw();
  9473. return this;
  9474. }
  9475. repaint() {
  9476. // 如果在没有render之前就repaint的,就直接return退出
  9477. var rendered = this.get('rendered');
  9478. if (!rendered) {
  9479. return;
  9480. }
  9481. this.set('isUpdate', true);
  9482. this.set('legendItems', null);
  9483. Chart.plugins.notify(this, 'repaint');
  9484. this._clearInner();
  9485. this.emit(EVENT_REPAINT);
  9486. this.render();
  9487. }
  9488. changeData(data) {
  9489. this.emit(EVENT_BEFORE_DATA_CHANGE, data);
  9490. this.set('data', data);
  9491. Chart.plugins.notify(this, 'changeData');
  9492. this.emit(EVENT_AFTER_DATA_CHANGE, data);
  9493. this.set('_padding', null);
  9494. this.repaint();
  9495. }
  9496. changeSize(width, height) {
  9497. if (width) {
  9498. this.set('width', width);
  9499. } else {
  9500. width = this.get('width');
  9501. }
  9502. if (height) {
  9503. this.set('height', height);
  9504. } else {
  9505. height = this.get('height');
  9506. }
  9507. var canvas = this.get('canvas');
  9508. canvas.changeSize(width, height);
  9509. this.emit(EVENT_AFTER_SIZE_CHANGE, {
  9510. width,
  9511. height
  9512. });
  9513. this.repaint();
  9514. return this;
  9515. }
  9516. destroy() {
  9517. this.clear();
  9518. var canvas = this.get('canvas');
  9519. canvas.destroy();
  9520. Chart.plugins.notify(this, 'afterCanvasDestroyed');
  9521. if (this._interactions) {
  9522. each(this._interactions, function (interaction) {
  9523. interaction.destroy();
  9524. });
  9525. }
  9526. super.destroy();
  9527. }
  9528. /**
  9529. * calculate dataset's position on canvas
  9530. * @param {Object} record the dataset
  9531. * @return {Object} return the position
  9532. */
  9533. getPosition(record) {
  9534. var self = this;
  9535. var coord = self.get('coord');
  9536. var xScale = self.getXScale();
  9537. var xField = xScale.field;
  9538. var yScales = self.getYScales(); // default first
  9539. var yScale = yScales[0];
  9540. var yField = yScale.field;
  9541. for (var i = 0, len = yScales.length; i < len; i++) {
  9542. var scale = yScales[i];
  9543. var field = scale.field;
  9544. if (record[field]) {
  9545. yScale = scale;
  9546. yField = field;
  9547. break;
  9548. }
  9549. }
  9550. var x = xScale.scale(record[xField]);
  9551. var y = yScale.scale(record[yField]);
  9552. return coord.convertPoint({
  9553. x,
  9554. y
  9555. });
  9556. }
  9557. /**
  9558. * get the data item of the point
  9559. * @param {Object} point canvas position
  9560. * @return {Object} return the data item
  9561. */
  9562. getRecord(point) {
  9563. var self = this;
  9564. var coord = self.get('coord');
  9565. var xScale = self.getXScale();
  9566. var yScale = self.getYScales()[0];
  9567. var invertPoint = coord.invertPoint(point);
  9568. var record = {};
  9569. record[xScale.field] = xScale.invert(invertPoint.x);
  9570. record[yScale.field] = yScale.invert(invertPoint.y);
  9571. return record;
  9572. }
  9573. /**
  9574. * get the dataset of the point
  9575. * @param {Object} point canvas position
  9576. * @return {Array} return the dataset
  9577. **/
  9578. getSnapRecords(point) {
  9579. var geom = this.get('geoms')[0];
  9580. var data = [];
  9581. if (geom) {
  9582. // need to judge
  9583. data = geom.getSnapRecords(point);
  9584. }
  9585. return data;
  9586. }
  9587. /**
  9588. * creat scale instances
  9589. * @param {String} field field name of data
  9590. * @return {Scale} return the scale
  9591. */
  9592. createScale(field) {
  9593. var data = this._getScaleData(field);
  9594. var scaleController = this.get('scaleController');
  9595. return scaleController.createScale(field, data);
  9596. }
  9597. /**
  9598. * @protected
  9599. * add geometry instance to geoms
  9600. * @param {Geom} geom geometry instance
  9601. */
  9602. addGeom(geom) {
  9603. var geoms = this.get('geoms');
  9604. geoms.push(geom);
  9605. }
  9606. /**
  9607. * get the scale of x axis
  9608. * @return {Scale} return the scale
  9609. */
  9610. getXScale() {
  9611. var self = this;
  9612. var geoms = self.get('geoms');
  9613. var xScale = geoms[0].getXScale();
  9614. return xScale;
  9615. }
  9616. /**
  9617. * get the scale of y axis
  9618. * @return {Array} return the scale
  9619. */
  9620. getYScales() {
  9621. var geoms = this.get('geoms');
  9622. var rst = [];
  9623. each(geoms, function (geom) {
  9624. var yScale = geom.getYScale();
  9625. if (rst.indexOf(yScale) === -1) {
  9626. rst.push(yScale);
  9627. }
  9628. });
  9629. return rst;
  9630. }
  9631. getLegendItems() {
  9632. if (this.get('legendItems')) {
  9633. return this.get('legendItems');
  9634. }
  9635. var legendItems = {};
  9636. var scales = [];
  9637. var geoms = this.get('geoms');
  9638. each(geoms, function (geom) {
  9639. var colorAttr = geom.getAttr('color');
  9640. if (colorAttr) {
  9641. var scale = colorAttr.getScale('color'); // 只支持分类图例
  9642. if (scale.isCategory && !_isScaleExist(scales, scale)) {
  9643. scales.push(scale);
  9644. var field = scale.field;
  9645. var ticks = scale.getTicks();
  9646. var items = [];
  9647. each(ticks, function (tick) {
  9648. var text = tick.text;
  9649. var name = text;
  9650. var scaleValue = tick.value;
  9651. var value = scale.invert(scaleValue);
  9652. var color = colorAttr.mapping(value).join('') || Global.defaultColor;
  9653. var marker = {
  9654. fill: color,
  9655. radius: 3,
  9656. symbol: 'circle',
  9657. stroke: '#fff'
  9658. };
  9659. items.push({
  9660. name,
  9661. // for display
  9662. dataValue: value,
  9663. // the origin value
  9664. checked: true,
  9665. marker
  9666. });
  9667. });
  9668. legendItems[field] = items;
  9669. }
  9670. }
  9671. });
  9672. this.set('legendItems', legendItems);
  9673. return legendItems;
  9674. } // register the plugins
  9675. registerPlugins(plugins) {
  9676. var self = this;
  9677. var chartPlugins = self.get('plugins') || [];
  9678. if (!isArray(chartPlugins)) {
  9679. chartPlugins = [chartPlugins];
  9680. }
  9681. [].concat(plugins).forEach(function (plugin) {
  9682. if (chartPlugins.indexOf(plugin) === -1) {
  9683. plugin.init && plugin.init(self); // init
  9684. chartPlugins.push(plugin);
  9685. }
  9686. });
  9687. Chart.plugins._cacheId++;
  9688. self.set('plugins', chartPlugins);
  9689. }
  9690. _renderAxis() {
  9691. var axisController = this.get('axisController');
  9692. var xScale = this.getXScale();
  9693. var yScales = this.getYScales();
  9694. var coord = this.get('coord');
  9695. Chart.plugins.notify(this, 'beforeRenderAxis');
  9696. axisController.createAxis(coord, xScale, yScales);
  9697. }
  9698. _isAutoPadding() {
  9699. if (this.get('_padding')) {
  9700. return false;
  9701. }
  9702. var padding = this.get('padding');
  9703. if (isArray(padding)) {
  9704. return padding.indexOf('auto') !== -1;
  9705. }
  9706. return padding === 'auto';
  9707. }
  9708. _updateLayout(padding) {
  9709. var width = this.get('width');
  9710. var height = this.get('height');
  9711. var start = {
  9712. x: padding[3],
  9713. y: padding[0]
  9714. };
  9715. var end = {
  9716. x: width - padding[1],
  9717. y: height - padding[2]
  9718. };
  9719. var plot = this.get('plot');
  9720. var coord = this.get('coord');
  9721. plot.reset(start, end);
  9722. coord.reset(plot);
  9723. }
  9724. }
  9725. Chart.plugins = Chart.initPlugins();
  9726. var track = function track() {
  9727. return null;
  9728. };
  9729. /**
  9730. * @fileOverview shape util
  9731. * @author dxq613@gmail.com
  9732. */
  9733. function splitPoints(obj) {
  9734. var points = [];
  9735. var x = obj.x;
  9736. var y = obj.y;
  9737. y = isArray(y) ? y : [y];
  9738. y.forEach(function (yItem, index) {
  9739. var point = {
  9740. x: isArray(x) ? x[index] : x,
  9741. y: yItem
  9742. };
  9743. points.push(point);
  9744. });
  9745. return points;
  9746. }
  9747. function splitArray(data, yField, connectNulls) {
  9748. if (!data.length) return [];
  9749. var arr = [];
  9750. var tmp = [];
  9751. var yValue;
  9752. each(data, function (obj) {
  9753. yValue = obj._origin ? obj._origin[yField] : obj[yField];
  9754. if (connectNulls) {
  9755. if (!isNil(yValue)) {
  9756. tmp.push(obj);
  9757. }
  9758. } else {
  9759. if (isArray(yValue) && isNil(yValue[0]) || isNil(yValue)) {
  9760. if (tmp.length) {
  9761. arr.push(tmp);
  9762. tmp = [];
  9763. }
  9764. } else {
  9765. tmp.push(obj);
  9766. }
  9767. }
  9768. });
  9769. if (tmp.length) {
  9770. arr.push(tmp);
  9771. }
  9772. return arr;
  9773. }
  9774. var SHAPES = ['circle', 'hollowCircle', 'rect'];
  9775. var Point = Shape$1.registerFactory('point', {
  9776. defaultShapeType: 'circle',
  9777. getDefaultPoints(pointInfo) {
  9778. return splitPoints(pointInfo);
  9779. }
  9780. });
  9781. function getPointsCfg(cfg) {
  9782. var style = {
  9783. lineWidth: 0,
  9784. stroke: cfg.color,
  9785. fill: cfg.color
  9786. };
  9787. if (cfg.size) {
  9788. style.size = cfg.size;
  9789. }
  9790. mix(style, cfg.style);
  9791. return mix({}, Global.shape.point, style);
  9792. }
  9793. function drawShape(cfg, container, shape) {
  9794. if (cfg.size === 0) return;
  9795. var pointCfg = getPointsCfg(cfg);
  9796. var size = pointCfg.r || pointCfg.size;
  9797. var x = cfg.x;
  9798. var y = !isArray(cfg.y) ? [cfg.y] : cfg.y;
  9799. if (shape === 'hollowCircle') {
  9800. pointCfg.lineWidth = 1;
  9801. pointCfg.fill = null;
  9802. }
  9803. for (var i = 0, len = y.length; i < len; i++) {
  9804. if (shape === 'rect') {
  9805. return container.addShape('Rect', {
  9806. className: 'point',
  9807. attrs: mix({
  9808. x: x - size,
  9809. y: y[i] - size,
  9810. width: size * 2,
  9811. height: size * 2
  9812. }, pointCfg)
  9813. });
  9814. }
  9815. return container.addShape('Circle', {
  9816. className: 'point',
  9817. attrs: mix({
  9818. x,
  9819. y: y[i],
  9820. r: size
  9821. }, pointCfg)
  9822. });
  9823. }
  9824. }
  9825. each(SHAPES, function (shapeType) {
  9826. Shape$1.registerShape('point', shapeType, {
  9827. draw(cfg, container) {
  9828. return drawShape(cfg, container, shapeType);
  9829. }
  9830. });
  9831. });
  9832. class Point$1 extends Geom {
  9833. getDefaultCfg() {
  9834. var cfg = super.getDefaultCfg();
  9835. cfg.type = 'point';
  9836. cfg.shapeType = 'point';
  9837. cfg.generatePoints = false;
  9838. return cfg;
  9839. }
  9840. draw(data, shapeFactory) {
  9841. var self = this;
  9842. var container = self.get('container');
  9843. each(data, function (obj) {
  9844. var shape = obj.shape;
  9845. var cfg = self.getDrawCfg(obj);
  9846. if (isArray(obj.y)) {
  9847. var hasStack = self.hasAdjust('stack');
  9848. each(obj.y, function (y, idx) {
  9849. cfg.y = y;
  9850. if (!hasStack || idx !== 0) {
  9851. self.drawShape(shape, obj, cfg, container, shapeFactory);
  9852. }
  9853. });
  9854. } else if (!isNil(obj.y)) {
  9855. self.drawShape(shape, obj, cfg, container, shapeFactory);
  9856. }
  9857. });
  9858. }
  9859. }
  9860. Geom.Point = Point$1;
  9861. var Line$2 = Shape$1.registerFactory('line', {
  9862. defaultShapeType: 'line'
  9863. });
  9864. function getStyle$1(cfg) {
  9865. var style = {
  9866. strokeStyle: cfg.color
  9867. };
  9868. if (cfg.size >= 0) {
  9869. style.lineWidth = cfg.size;
  9870. }
  9871. mix(style, cfg.style);
  9872. return mix({}, Global.shape.line, style);
  9873. }
  9874. function drawLines(cfg, container, style, smooth) {
  9875. var points = cfg.points;
  9876. if (points.length && isArray(points[0].y)) {
  9877. var topPoints = [];
  9878. var bottomPoints = [];
  9879. for (var i = 0, len = points.length; i < len; i++) {
  9880. var point = points[i];
  9881. var tmp = splitPoints(point);
  9882. bottomPoints.push(tmp[0]);
  9883. topPoints.push(tmp[1]);
  9884. }
  9885. if (cfg.isInCircle) {
  9886. topPoints.push(topPoints[0]);
  9887. bottomPoints.push(bottomPoints[0]);
  9888. }
  9889. if (cfg.isStack) {
  9890. return container.addShape('Polyline', {
  9891. className: 'line',
  9892. attrs: mix({
  9893. points: topPoints,
  9894. smooth
  9895. }, style)
  9896. });
  9897. }
  9898. var topShape = container.addShape('Polyline', {
  9899. className: 'line',
  9900. attrs: mix({
  9901. points: topPoints,
  9902. smooth
  9903. }, style)
  9904. });
  9905. var bottomShape = container.addShape('Polyline', {
  9906. className: 'line',
  9907. attrs: mix({
  9908. points: bottomPoints,
  9909. smooth
  9910. }, style)
  9911. });
  9912. return [topShape, bottomShape];
  9913. }
  9914. if (cfg.isInCircle) {
  9915. points.push(points[0]);
  9916. }
  9917. return container.addShape('Polyline', {
  9918. className: 'line',
  9919. attrs: mix({
  9920. points,
  9921. smooth
  9922. }, style)
  9923. });
  9924. }
  9925. var SHAPES$1 = ['line', 'smooth', 'dash'];
  9926. each(SHAPES$1, function (shapeType) {
  9927. Shape$1.registerShape('line', shapeType, {
  9928. draw(cfg, container) {
  9929. var smooth = shapeType === 'smooth';
  9930. var style = getStyle$1(cfg);
  9931. if (shapeType === 'dash') {
  9932. style.lineDash = Global.lineDash;
  9933. }
  9934. return drawLines(cfg, container, style, smooth);
  9935. }
  9936. });
  9937. });
  9938. class Path extends Geom {
  9939. getDefaultCfg() {
  9940. var cfg = super.getDefaultCfg();
  9941. cfg.type = 'path';
  9942. cfg.shapeType = 'line';
  9943. return cfg;
  9944. }
  9945. getDrawCfg(obj) {
  9946. var cfg = super.getDrawCfg(obj);
  9947. cfg.isStack = this.hasAdjust('stack');
  9948. return cfg;
  9949. }
  9950. draw(data, shapeFactory) {
  9951. var self = this;
  9952. var container = self.get('container');
  9953. var yScale = self.getYScale();
  9954. var connectNulls = self.get('connectNulls');
  9955. var splitArrayObj = splitArray(data, yScale.field, connectNulls);
  9956. var cfg = this.getDrawCfg(data[0]);
  9957. cfg.origin = data;
  9958. each(splitArrayObj, function (subData, splitedIndex) {
  9959. cfg.splitedIndex = splitedIndex;
  9960. cfg.points = subData;
  9961. self.drawShape(cfg.shape, data[0], cfg, container, shapeFactory);
  9962. });
  9963. }
  9964. }
  9965. Geom.Path = Path;
  9966. class Line$3 extends Path {
  9967. getDefaultCfg() {
  9968. var cfg = super.getDefaultCfg();
  9969. cfg.type = 'line';
  9970. cfg.sortable = true;
  9971. return cfg;
  9972. }
  9973. }
  9974. Geom.Line = Line$3;
  9975. function equals(v1, v2) {
  9976. return Math.abs(v1 - v2) < 0.00001;
  9977. }
  9978. function notEmpty(value) {
  9979. return !isNaN(value) && !isNil(value);
  9980. }
  9981. function filterPoints(points) {
  9982. var filteredPoints = []; // filter the point which x or y is NaN
  9983. for (var i = 0, len = points.length; i < len; i++) {
  9984. var point = points[i];
  9985. if (notEmpty(point.x) && notEmpty(point.y)) {
  9986. filteredPoints.push(point);
  9987. }
  9988. }
  9989. return filteredPoints;
  9990. }
  9991. function equalsCenter(points, center) {
  9992. var eqls = true;
  9993. each(points, function (point) {
  9994. if (!equals(point.x, center.x) || !equals(point.y, center.y)) {
  9995. eqls = false;
  9996. return false;
  9997. }
  9998. });
  9999. return eqls;
  10000. }
  10001. function drawRectShape(topPoints, bottomPoints, container, style, isSmooth) {
  10002. var shape;
  10003. var points = topPoints.concat(bottomPoints);
  10004. if (isSmooth) {
  10005. shape = container.addShape('Custom', {
  10006. className: 'area',
  10007. attrs: mix({
  10008. points
  10009. }, style),
  10010. createPath(context) {
  10011. var constaint = [[0, 0], [1, 1]];
  10012. var points = filterPoints(this._attrs.attrs.points);
  10013. var pointsLen = points.length;
  10014. var topPoints = points.slice(0, pointsLen / 2);
  10015. var bottomPoints = points.slice(pointsLen / 2, pointsLen);
  10016. var topSps = catmullRom2bezier(topPoints, false, constaint);
  10017. context.beginPath();
  10018. context.moveTo(topPoints[0].x, topPoints[0].y);
  10019. for (var i = 0, n = topSps.length; i < n; i++) {
  10020. var sp = topSps[i];
  10021. context.bezierCurveTo(sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]);
  10022. }
  10023. if (bottomPoints.length) {
  10024. var bottomSps = catmullRom2bezier(bottomPoints, false, constaint);
  10025. context.lineTo(bottomPoints[0].x, bottomPoints[0].y);
  10026. for (var _i = 0, _n = bottomSps.length; _i < _n; _i++) {
  10027. var _sp = bottomSps[_i];
  10028. context.bezierCurveTo(_sp[1], _sp[2], _sp[3], _sp[4], _sp[5], _sp[6]);
  10029. }
  10030. }
  10031. context.closePath();
  10032. },
  10033. calculateBox() {
  10034. var points = filterPoints(this._attrs.attrs.points);
  10035. return getBBoxFromPoints(points);
  10036. }
  10037. });
  10038. } else {
  10039. shape = container.addShape('Polyline', {
  10040. className: 'area',
  10041. attrs: mix({
  10042. points
  10043. }, style)
  10044. });
  10045. }
  10046. return shape;
  10047. }
  10048. function drawShape$1(cfg, container, isSmooth) {
  10049. var self = this;
  10050. var points = cfg.points;
  10051. var topPoints = [];
  10052. var bottomPoints = [];
  10053. each(points, function (point) {
  10054. bottomPoints.push(point[0]);
  10055. topPoints.push(point[1]);
  10056. });
  10057. var style = mix({
  10058. fillStyle: cfg.color
  10059. }, Global.shape.area, cfg.style);
  10060. bottomPoints.reverse();
  10061. topPoints = self.parsePoints(topPoints);
  10062. bottomPoints = self.parsePoints(bottomPoints);
  10063. if (cfg.isInCircle) {
  10064. topPoints.push(topPoints[0]);
  10065. bottomPoints.unshift(bottomPoints[bottomPoints.length - 1]);
  10066. if (equalsCenter(bottomPoints, cfg.center)) {
  10067. bottomPoints = [];
  10068. }
  10069. }
  10070. return drawRectShape(topPoints, bottomPoints, container, style, isSmooth);
  10071. }
  10072. var Area = Shape$1.registerFactory('area', {
  10073. defaultShapeType: 'area',
  10074. getDefaultPoints(obj) {
  10075. var x = obj.x;
  10076. var y = obj.y;
  10077. var y0 = obj.y0;
  10078. y = isArray(y) ? y : [y0, y];
  10079. var points = [];
  10080. points.push({
  10081. x,
  10082. y: y[0]
  10083. }, {
  10084. x,
  10085. y: y[1]
  10086. });
  10087. return points;
  10088. }
  10089. });
  10090. var SHAPES$2 = ['area', 'smooth'];
  10091. each(SHAPES$2, function (shapeType) {
  10092. Shape$1.registerShape('area', shapeType, {
  10093. draw(cfg, container) {
  10094. var smooth = shapeType === 'smooth';
  10095. return drawShape$1.call(this, cfg, container, smooth);
  10096. }
  10097. });
  10098. });
  10099. /**
  10100. * @fileOverview area geometry
  10101. * @author dxq613 @gmail.com
  10102. * @author sima.zhang1990@gmail.com
  10103. */
  10104. class Area$1 extends Geom {
  10105. /**
  10106. * get the default configuration
  10107. * @protected
  10108. * @return {Object} return the result
  10109. */
  10110. getDefaultCfg() {
  10111. var cfg = super.getDefaultCfg();
  10112. cfg.type = 'area';
  10113. cfg.shapeType = 'area';
  10114. cfg.generatePoints = true;
  10115. cfg.sortable = true;
  10116. return cfg;
  10117. }
  10118. draw(data, shapeFactory) {
  10119. var self = this;
  10120. var container = self.get('container');
  10121. var cfg = this.getDrawCfg(data[0]);
  10122. var yScale = self.getYScale();
  10123. var connectNulls = self.get('connectNulls');
  10124. var splitArrayfn = splitArray(data, yScale.field, connectNulls);
  10125. cfg.origin = data;
  10126. each(splitArrayfn, function (subData, splitedIndex) {
  10127. cfg.splitedIndex = splitedIndex;
  10128. var points = subData.map(function (obj) {
  10129. return obj.points;
  10130. });
  10131. cfg.points = points;
  10132. self.drawShape(cfg.shape, data[0], cfg, container, shapeFactory);
  10133. });
  10134. }
  10135. }
  10136. Geom.Area = Area$1;
  10137. /**
  10138. * @fileOverview Utility for calculate the with ratui in x axis
  10139. * @author sima.zhang1990@gmail.com
  10140. * @author dxq613@gmail.com
  10141. */
  10142. var SizeMixin = {
  10143. initEvent() {
  10144. var _this = this;
  10145. var chart = this.get('chart');
  10146. if (!chart) {
  10147. return;
  10148. }
  10149. chart.on(EVENT_AFTER_SIZE_CHANGE, function () {
  10150. _this.set('_width', null);
  10151. });
  10152. },
  10153. getDefaultSize() {
  10154. var defaultSize = this.get('defaultSize');
  10155. if (!defaultSize) {
  10156. var coord = this.get('coord');
  10157. var xScale = this.getXScale();
  10158. var dataArray = this.get('dataArray');
  10159. var values = uniq(xScale.values);
  10160. var count = values.length;
  10161. var range = xScale.range;
  10162. var normalizeSize = 1 / count;
  10163. var widthRatio = 1;
  10164. if (coord && coord.isPolar) {
  10165. if (coord.transposed && count > 1) {
  10166. widthRatio = Global.widthRatio.multiplePie;
  10167. } else {
  10168. widthRatio = Global.widthRatio.rose;
  10169. }
  10170. } else {
  10171. if (xScale.isLinear) {
  10172. normalizeSize *= range[1] - range[0];
  10173. }
  10174. widthRatio = Global.widthRatio.column;
  10175. }
  10176. normalizeSize *= widthRatio;
  10177. if (this.hasAdjust('dodge')) {
  10178. normalizeSize = normalizeSize / dataArray.length;
  10179. }
  10180. defaultSize = normalizeSize;
  10181. this.set('defaultSize', defaultSize);
  10182. }
  10183. return defaultSize;
  10184. },
  10185. getDimWidth(dimName) {
  10186. var coord = this.get('coord');
  10187. var start = coord.convertPoint({
  10188. x: 0,
  10189. y: 0
  10190. });
  10191. var end = coord.convertPoint({
  10192. x: dimName === 'x' ? 1 : 0,
  10193. y: dimName === 'x' ? 0 : 1
  10194. });
  10195. var width = 0;
  10196. if (start && end) {
  10197. width = Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2));
  10198. }
  10199. return width;
  10200. },
  10201. _getWidth() {
  10202. var width = this.get('_width');
  10203. if (!width) {
  10204. var coord = this.get('coord');
  10205. if (coord && coord.isPolar && !coord.transposed) {
  10206. width = (coord.endAngle - coord.startAngle) * coord.circleRadius;
  10207. } else {
  10208. width = this.getDimWidth('x');
  10209. }
  10210. this.set('_width', width);
  10211. }
  10212. return width;
  10213. },
  10214. _toNormalizedSize(size) {
  10215. var width = this._getWidth();
  10216. return size / width;
  10217. },
  10218. _toCoordSize(normalizeSize) {
  10219. var width = this._getWidth();
  10220. return width * normalizeSize;
  10221. },
  10222. getNormalizedSize(obj) {
  10223. var size = this.getAttrValue('size', obj);
  10224. if (isNil(size)) {
  10225. size = this.getDefaultSize();
  10226. } else {
  10227. size = this._toNormalizedSize(size);
  10228. }
  10229. return size;
  10230. },
  10231. getSize(obj) {
  10232. var size = this.getAttrValue('size', obj);
  10233. if (isNil(size)) {
  10234. var normalizeSize = this.getDefaultSize();
  10235. size = this._toCoordSize(normalizeSize);
  10236. }
  10237. return size;
  10238. }
  10239. };
  10240. function getRectPoints(cfg) {
  10241. var {
  10242. x,
  10243. y,
  10244. y0,
  10245. size
  10246. } = cfg;
  10247. var ymin = y0;
  10248. var ymax = y;
  10249. if (isArray(y)) {
  10250. ymax = y[1];
  10251. ymin = y[0];
  10252. }
  10253. var xmin;
  10254. var xmax;
  10255. if (isArray(x)) {
  10256. xmin = x[0];
  10257. xmax = x[1];
  10258. } else {
  10259. xmin = x - size / 2;
  10260. xmax = x + size / 2;
  10261. }
  10262. return [{
  10263. x: xmin,
  10264. y: ymin
  10265. }, {
  10266. x: xmin,
  10267. y: ymax
  10268. }, {
  10269. x: xmax,
  10270. y: ymax
  10271. }, {
  10272. x: xmax,
  10273. y: ymin
  10274. }];
  10275. }
  10276. function getRectRange(points) {
  10277. var xValues = [];
  10278. var yValues = [];
  10279. for (var i = 0, len = points.length; i < len; i++) {
  10280. var point = points[i];
  10281. xValues.push(point.x);
  10282. yValues.push(point.y);
  10283. }
  10284. var xMin = Math.min.apply(null, xValues);
  10285. var yMin = Math.min.apply(null, yValues);
  10286. var xMax = Math.max.apply(null, xValues);
  10287. var yMax = Math.max.apply(null, yValues);
  10288. return {
  10289. x: xMin,
  10290. y: yMin,
  10291. width: xMax - xMin,
  10292. height: yMax - yMin
  10293. };
  10294. }
  10295. function getMiddlePoint(a, b) {
  10296. var x = (a.x - b.x) / 2 + b.x;
  10297. var y = (a.y - b.y) / 2 + b.y;
  10298. return {
  10299. x,
  10300. y
  10301. };
  10302. }
  10303. var Interval = Shape$1.registerFactory('interval', {
  10304. defaultShapeType: 'rect',
  10305. getDefaultPoints(cfg) {
  10306. return getRectPoints(cfg);
  10307. }
  10308. });
  10309. Shape$1.registerShape('interval', 'rect', {
  10310. draw(cfg, container) {
  10311. var points = this.parsePoints(cfg.points);
  10312. var style = mix({
  10313. fill: cfg.color
  10314. }, Global.shape.interval, cfg.style);
  10315. if (cfg.isInCircle) {
  10316. var newPoints = points.slice(0);
  10317. if (this._coord.transposed) {
  10318. newPoints = [points[0], points[3], points[2], points[1]];
  10319. }
  10320. var {
  10321. x,
  10322. y
  10323. } = cfg.center;
  10324. var v = [1, 0];
  10325. var v0 = [newPoints[0].x - x, newPoints[0].y - y];
  10326. var v1 = [newPoints[1].x - x, newPoints[1].y - y];
  10327. var v2 = [newPoints[2].x - x, newPoints[2].y - y];
  10328. var startAngle = Vector2.angleTo(v, v1);
  10329. var endAngle = Vector2.angleTo(v, v2);
  10330. var r0 = Vector2.length(v0);
  10331. var r = Vector2.length(v1);
  10332. if (startAngle >= 1.5 * Math.PI) {
  10333. startAngle = startAngle - 2 * Math.PI;
  10334. }
  10335. if (endAngle >= 1.5 * Math.PI) {
  10336. endAngle = endAngle - 2 * Math.PI;
  10337. }
  10338. return container.addShape('Sector', {
  10339. className: 'interval',
  10340. attrs: mix({
  10341. x,
  10342. y,
  10343. r,
  10344. r0,
  10345. startAngle,
  10346. endAngle
  10347. }, style)
  10348. });
  10349. }
  10350. var rectCfg = getRectRange(points);
  10351. return container.addShape('rect', {
  10352. className: 'interval',
  10353. attrs: mix(rectCfg, style)
  10354. });
  10355. }
  10356. }); // 金字塔 和 漏斗图
  10357. ['pyramid', 'funnel'].forEach(function (shapeType) {
  10358. Shape$1.registerShape('interval', shapeType, {
  10359. getPoints(cfg) {
  10360. cfg.size = cfg.size * 2; // 漏斗图的 size 是柱状图的两倍
  10361. return getRectPoints(cfg);
  10362. },
  10363. draw(cfg, container) {
  10364. var points = this.parsePoints(cfg.points);
  10365. var nextPoints = this.parsePoints(cfg.nextPoints);
  10366. var polygonPoints = null;
  10367. if (nextPoints) {
  10368. polygonPoints = [points[0], points[1], nextPoints[1], nextPoints[0]];
  10369. } else {
  10370. polygonPoints = [points[0], points[1]]; // pyramid 顶部是三角形,所以取中心点就好了,funnel顶部是长方形
  10371. if (shapeType === 'pyramid') {
  10372. polygonPoints.push(getMiddlePoint(points[2], points[3]));
  10373. } else {
  10374. polygonPoints.push(points[2], points[3]);
  10375. }
  10376. }
  10377. var attrs = mix({
  10378. fill: cfg.color,
  10379. points: polygonPoints
  10380. }, Global.shape.interval, cfg.style);
  10381. return container.addShape('polygon', {
  10382. className: 'interval',
  10383. attrs
  10384. });
  10385. }
  10386. });
  10387. });
  10388. class Interval$1 extends Geom {
  10389. getDefaultCfg() {
  10390. var cfg = super.getDefaultCfg();
  10391. cfg.type = 'interval';
  10392. cfg.shapeType = 'interval';
  10393. cfg.generatePoints = true;
  10394. return cfg;
  10395. }
  10396. constructor(cfg) {
  10397. super(cfg);
  10398. mix(this, SizeMixin);
  10399. }
  10400. init() {
  10401. super.init(); // 绑定事件
  10402. this.initEvent();
  10403. }
  10404. createShapePointsCfg(obj) {
  10405. var cfg = super.createShapePointsCfg(obj);
  10406. cfg.size = this.getNormalizedSize(obj);
  10407. return cfg;
  10408. }
  10409. clearInner() {
  10410. super.clearInner();
  10411. this.set('defaultSize', null);
  10412. }
  10413. }
  10414. Geom.Interval = Interval$1;
  10415. var Polygon$1 = Shape$1.registerFactory('polygon', {
  10416. defaultShapeType: 'polygon',
  10417. getDefaultPoints(pointInfo) {
  10418. var points = [];
  10419. var {
  10420. x,
  10421. y
  10422. } = pointInfo;
  10423. for (var i = 0, len = x.length; i < len; i++) {
  10424. points.push({
  10425. x: x[i],
  10426. y: y[i]
  10427. });
  10428. }
  10429. return points;
  10430. }
  10431. });
  10432. Shape$1.registerShape('polygon', 'polygon', {
  10433. draw(cfg, container) {
  10434. var points = this.parsePoints(cfg.points);
  10435. var style = mix({
  10436. fill: cfg.color,
  10437. points
  10438. }, cfg.style);
  10439. return container.addShape('Polygon', {
  10440. className: 'polygon',
  10441. attrs: style
  10442. });
  10443. }
  10444. });
  10445. class Polygon$2 extends Geom {
  10446. getDefaultCfg() {
  10447. var cfg = super.getDefaultCfg();
  10448. cfg.type = 'polygon';
  10449. cfg.shapeType = 'polygon';
  10450. cfg.generatePoints = true;
  10451. return cfg;
  10452. }
  10453. createShapePointsCfg(obj) {
  10454. var cfg = super.createShapePointsCfg(obj);
  10455. var self = this;
  10456. var x = cfg.x;
  10457. var y = cfg.y;
  10458. var temp;
  10459. if (!(isArray(x) && isArray(y))) {
  10460. var xScale = self.getXScale();
  10461. var yScale = self.getYScale();
  10462. var xCount = xScale.values ? xScale.values.length : xScale.ticks.length;
  10463. var yCount = yScale.values ? yScale.values.length : yScale.ticks.length;
  10464. var xOffset = 0.5 * 1 / xCount;
  10465. var yOffset = 0.5 * 1 / yCount;
  10466. if (xScale.isCategory && yScale.isCategory) {
  10467. x = [x - xOffset, x - xOffset, x + xOffset, x + xOffset];
  10468. y = [y - yOffset, y + yOffset, y + yOffset, y - yOffset];
  10469. } else if (isArray(x)) {
  10470. temp = x;
  10471. x = [temp[0], temp[0], temp[1], temp[1]];
  10472. y = [y - yOffset / 2, y + yOffset / 2, y + yOffset / 2, y - yOffset / 2];
  10473. } else if (isArray(y)) {
  10474. temp = y;
  10475. y = [temp[0], temp[1], temp[1], temp[0]];
  10476. x = [x - xOffset / 2, x - xOffset / 2, x + xOffset / 2, x + xOffset / 2];
  10477. }
  10478. cfg.x = x;
  10479. cfg.y = y;
  10480. }
  10481. return cfg;
  10482. }
  10483. }
  10484. Geom.Polygon = Polygon$2;
  10485. function _sortValue(value) {
  10486. var sorted = value.sort(function (a, b) {
  10487. return a < b ? 1 : -1;
  10488. });
  10489. var length = sorted.length;
  10490. if (length < 4) {
  10491. var min = sorted[length - 1];
  10492. for (var i = 0; i < 4 - length; i++) {
  10493. sorted.push(min);
  10494. }
  10495. }
  10496. return sorted;
  10497. } // from left bottom corner, and clockwise
  10498. function getCandlePoints(x, y, width) {
  10499. var yValues = _sortValue(y);
  10500. var points = [{
  10501. x,
  10502. y: yValues[0]
  10503. }, {
  10504. x,
  10505. y: yValues[1]
  10506. }, {
  10507. x: x - width / 2,
  10508. y: yValues[2]
  10509. }, {
  10510. x: x - width / 2,
  10511. y: yValues[1]
  10512. }, {
  10513. x: x + width / 2,
  10514. y: yValues[1]
  10515. }, {
  10516. x: x + width / 2,
  10517. y: yValues[2]
  10518. }, {
  10519. x,
  10520. y: yValues[2]
  10521. }, {
  10522. x,
  10523. y: yValues[3]
  10524. }];
  10525. return points;
  10526. }
  10527. var Schema = Shape$1.registerFactory('schema', {});
  10528. Shape$1.registerShape('schema', 'candle', {
  10529. getPoints(cfg) {
  10530. return getCandlePoints(cfg.x, cfg.y, cfg.size);
  10531. },
  10532. draw(cfg, container) {
  10533. var points = this.parsePoints(cfg.points);
  10534. var style = mix({
  10535. stroke: cfg.color,
  10536. fill: cfg.color,
  10537. lineWidth: 1
  10538. }, cfg.style);
  10539. return container.addShape('Custom', {
  10540. className: 'schema',
  10541. attrs: style,
  10542. createPath(ctx) {
  10543. ctx.beginPath();
  10544. ctx.moveTo(points[0].x, points[0].y);
  10545. ctx.lineTo(points[1].x, points[1].y);
  10546. ctx.moveTo(points[2].x, points[2].y);
  10547. for (var i = 3; i < 6; i++) {
  10548. ctx.lineTo(points[i].x, points[i].y);
  10549. }
  10550. ctx.closePath();
  10551. ctx.moveTo(points[6].x, points[6].y);
  10552. ctx.lineTo(points[7].x, points[7].y);
  10553. }
  10554. });
  10555. }
  10556. });
  10557. class Schema$1 extends Geom {
  10558. getDefaultCfg() {
  10559. var cfg = super.getDefaultCfg();
  10560. cfg.type = 'schema';
  10561. cfg.shapeType = 'schema';
  10562. cfg.generatePoints = true;
  10563. return cfg;
  10564. }
  10565. constructor(cfg) {
  10566. super(cfg);
  10567. mix(this, SizeMixin);
  10568. }
  10569. init() {
  10570. super.init(); // 绑定事件
  10571. this.initEvent();
  10572. }
  10573. createShapePointsCfg(obj) {
  10574. var cfg = super.createShapePointsCfg(obj);
  10575. cfg.size = this.getNormalizedSize(obj);
  10576. return cfg;
  10577. }
  10578. clearInner() {
  10579. super.clearInner();
  10580. this.set('defaultSize', null);
  10581. }
  10582. }
  10583. Geom.Schema = Schema$1;
  10584. var toString$3 = {}.toString;
  10585. var isType$1 = function isType(value, type) {
  10586. return toString$3.call(value) === '[object ' + type + ']';
  10587. };
  10588. var isType_1 = isType$1;
  10589. var isArray$1 = Array.isArray ? Array.isArray : function (value) {
  10590. return isType_1(value, 'Array');
  10591. };
  10592. var isArray_1 = isArray$1;
  10593. // isFinite,
  10594. var isNil$1 = function isNil(value) {
  10595. /**
  10596. * isNil(null) => true
  10597. * isNil() => true
  10598. */
  10599. return value === null || value === undefined;
  10600. };
  10601. var isNil_1 = isNil$1;
  10602. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  10603. var Stack =
  10604. /*#__PURE__*/
  10605. function (_Adjust) {
  10606. _inheritsLoose(Stack, _Adjust);
  10607. function Stack() {
  10608. return _Adjust.apply(this, arguments) || this;
  10609. }
  10610. var _proto = Stack.prototype;
  10611. _proto._initDefaultCfg = function _initDefaultCfg() {
  10612. this.xField = null; // 调整对应的 x 方向对应的字段名称
  10613. this.yField = null; // 调整对应的 y 方向对应的字段名称
  10614. };
  10615. _proto.processAdjust = function processAdjust(dataArray) {
  10616. this.processStack(dataArray);
  10617. };
  10618. _proto.processStack = function processStack(dataArray) {
  10619. var self = this;
  10620. var xField = self.xField;
  10621. var yField = self.yField;
  10622. var count = dataArray.length;
  10623. var stackCache = {
  10624. positive: {},
  10625. negative: {}
  10626. }; // 层叠顺序翻转
  10627. if (self.reverseOrder) {
  10628. dataArray = dataArray.slice(0).reverse();
  10629. }
  10630. for (var i = 0; i < count; i++) {
  10631. var data = dataArray[i];
  10632. for (var j = 0, len = data.length; j < len; j++) {
  10633. var item = data[j];
  10634. var x = item[xField] || 0;
  10635. var y = item[yField];
  10636. var xkey = x.toString();
  10637. y = isArray_1(y) ? y[1] : y;
  10638. if (!isNil_1(y)) {
  10639. var direction = y >= 0 ? 'positive' : 'negative';
  10640. if (!stackCache[direction][xkey]) {
  10641. stackCache[direction][xkey] = 0;
  10642. }
  10643. item[yField] = [stackCache[direction][xkey], y + stackCache[direction][xkey]];
  10644. stackCache[direction][xkey] += y;
  10645. }
  10646. }
  10647. }
  10648. };
  10649. return Stack;
  10650. }(base);
  10651. base.Stack = Stack;
  10652. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  10653. var isObject$1 = function isObject(value) {
  10654. /**
  10655. * isObject({}) => true
  10656. * isObject([1, 2, 3]) => true
  10657. * isObject(Function) => true
  10658. * isObject(null) => false
  10659. */
  10660. var type = typeof value === 'undefined' ? 'undefined' : _typeof(value);
  10661. return value !== null && type === 'object' || type === 'function';
  10662. };
  10663. var isObject_1 = isObject$1;
  10664. var each$1 = function each(elements, func) {
  10665. if (!elements) {
  10666. return;
  10667. }
  10668. var rst = void 0;
  10669. if (isArray_1(elements)) {
  10670. for (var i = 0, len = elements.length; i < len; i++) {
  10671. rst = func(elements[i], i);
  10672. if (rst === false) {
  10673. break;
  10674. }
  10675. }
  10676. } else if (isObject_1(elements)) {
  10677. for (var k in elements) {
  10678. if (elements.hasOwnProperty(k)) {
  10679. rst = func(elements[k], k);
  10680. if (rst === false) {
  10681. break;
  10682. }
  10683. }
  10684. }
  10685. }
  10686. };
  10687. var each_1 = each$1;
  10688. function _inheritsLoose$1(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  10689. var MARGIN_RATIO = 1 / 2;
  10690. var DODGE_RATIO = 1 / 2;
  10691. var Dodge =
  10692. /*#__PURE__*/
  10693. function (_Adjust) {
  10694. _inheritsLoose$1(Dodge, _Adjust);
  10695. function Dodge() {
  10696. return _Adjust.apply(this, arguments) || this;
  10697. }
  10698. var _proto = Dodge.prototype;
  10699. _proto._initDefaultCfg = function _initDefaultCfg() {
  10700. /**
  10701. * 调整过程中,2个数据的间距
  10702. * @type {Number}
  10703. */
  10704. this.marginRatio = MARGIN_RATIO;
  10705. /**
  10706. * 调整占单位宽度的比例,例如:占2个分类间距的 1/2
  10707. * @type {Number}
  10708. */
  10709. this.dodgeRatio = DODGE_RATIO;
  10710. this.adjustNames = ['x', 'y']; // 调整的维度,默认,x,y都做调整
  10711. };
  10712. _proto.getDodgeOffset = function getDodgeOffset(range, index, count) {
  10713. var self = this;
  10714. var pre = range.pre;
  10715. var next = range.next;
  10716. var tickLength = next - pre;
  10717. var width = tickLength * self.dodgeRatio / count;
  10718. var margin = self.marginRatio * width;
  10719. var offset = 1 / 2 * (tickLength - count * width - (count - 1) * margin) + ((index + 1) * width + index * margin) - 1 / 2 * width - 1 / 2 * tickLength;
  10720. return (pre + next) / 2 + offset;
  10721. };
  10722. _proto.processAdjust = function processAdjust(dataArray) {
  10723. var self = this;
  10724. var count = dataArray.length;
  10725. var xField = self.xField;
  10726. each_1(dataArray, function (data, index) {
  10727. for (var i = 0, len = data.length; i < len; i++) {
  10728. var obj = data[i];
  10729. var value = obj[xField];
  10730. var range = {
  10731. pre: len === 1 ? value - 1 : value - 0.5,
  10732. next: len === 1 ? value + 1 : value + 0.5
  10733. };
  10734. var dodgeValue = self.getDodgeOffset(range, index, count);
  10735. obj[xField] = dodgeValue;
  10736. }
  10737. });
  10738. };
  10739. return Dodge;
  10740. }(base);
  10741. base.Dodge = Dodge;
  10742. /**
  10743. * 是否为函数
  10744. * @param {*} fn 对象
  10745. * @return {Boolean} 是否函数
  10746. */
  10747. var isFunction$1 = function isFunction(value) {
  10748. return isType_1(value, 'Function');
  10749. };
  10750. var isFunction_1 = isFunction$1;
  10751. /**
  10752. * @param {Array} arr The array to iterate over.
  10753. * @param {Function} [fn] The iteratee invoked per element.
  10754. * @return {*} Returns the maximum value.
  10755. * @example
  10756. *
  10757. * var objects = [{ 'n': 1 }, { 'n': 2 }];
  10758. *
  10759. * maxBy(objects, function(o) { return o.n; });
  10760. * // => { 'n': 2 }
  10761. *
  10762. * maxBy(objects, 'n');
  10763. * // => { 'n': 2 }
  10764. */
  10765. var maxBy$1 = function maxBy(arr, fn) {
  10766. if (!isArray_1(arr)) {
  10767. return undefined;
  10768. }
  10769. var max = arr[0];
  10770. var maxData = void 0;
  10771. if (isFunction_1(fn)) {
  10772. maxData = fn(arr[0]);
  10773. } else {
  10774. maxData = arr[0][fn];
  10775. }
  10776. var data = void 0;
  10777. each_1(arr, function (val) {
  10778. if (isFunction_1(fn)) {
  10779. data = fn(val);
  10780. } else {
  10781. data = val[fn];
  10782. }
  10783. if (data > maxData) {
  10784. max = val;
  10785. maxData = data;
  10786. }
  10787. });
  10788. return max;
  10789. };
  10790. var maxBy_1 = maxBy$1;
  10791. var merge$1 = function merge(dataArray) {
  10792. var rst = [];
  10793. for (var i = 0; i < dataArray.length; i++) {
  10794. rst = rst.concat(dataArray[i]);
  10795. }
  10796. return rst;
  10797. };
  10798. var merge_1 = merge$1;
  10799. function _inheritsLoose$2(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  10800. var ArrayUtil = {
  10801. merge: merge_1
  10802. };
  10803. var Symmetric =
  10804. /*#__PURE__*/
  10805. function (_Adjust) {
  10806. _inheritsLoose$2(Symmetric, _Adjust);
  10807. function Symmetric() {
  10808. return _Adjust.apply(this, arguments) || this;
  10809. }
  10810. var _proto = Symmetric.prototype;
  10811. _proto._initDefaultCfg = function _initDefaultCfg() {
  10812. this.xField = null; // 调整对应的 x 方向对应的字段名称
  10813. this.yField = null; // 调整对应的 y 方向对应的字段名称
  10814. this.cacheMax = null; // 缓存的最大值
  10815. this.adjustNames = ['y']; // Only support stack y
  10816. this.groupFields = null; // 参与分组的数据维度
  10817. }; // 获取最大的y值
  10818. _proto._getMax = function _getMax(dim) {
  10819. var self = this;
  10820. var mergeData = self.mergeData;
  10821. var maxRecord = maxBy_1(mergeData, function (obj) {
  10822. var value = obj[dim];
  10823. if (isArray_1(value)) {
  10824. return Math.max.apply(null, value);
  10825. }
  10826. return value;
  10827. });
  10828. var maxValue = maxRecord[dim];
  10829. var max = isArray_1(maxValue) ? Math.max.apply(null, maxValue) : maxValue;
  10830. return max;
  10831. }; // 获取每个字段最大的值
  10832. _proto._getXValuesMax = function _getXValuesMax() {
  10833. var self = this;
  10834. var yField = self.yField;
  10835. var xField = self.xField;
  10836. var cache = {};
  10837. var mergeData = self.mergeData;
  10838. each_1(mergeData, function (obj) {
  10839. var xValue = obj[xField];
  10840. var yValue = obj[yField];
  10841. var max = isArray_1(yValue) ? Math.max.apply(null, yValue) : yValue;
  10842. cache[xValue] = cache[xValue] || 0;
  10843. if (cache[xValue] < max) {
  10844. cache[xValue] = max;
  10845. }
  10846. });
  10847. return cache;
  10848. }; // 入口函数
  10849. _proto.processAdjust = function processAdjust(dataArray) {
  10850. var self = this;
  10851. var mergeData = ArrayUtil.merge(dataArray);
  10852. self.mergeData = mergeData;
  10853. self._processSymmetric(dataArray);
  10854. self.mergeData = null;
  10855. }; // 处理对称
  10856. _proto._processSymmetric = function _processSymmetric(dataArray) {
  10857. var self = this;
  10858. var xField = self.xField;
  10859. var yField = self.yField;
  10860. var max = self._getMax(yField);
  10861. var first = dataArray[0][0];
  10862. var cache;
  10863. if (first && isArray_1(first[yField])) {
  10864. cache = self._getXValuesMax();
  10865. }
  10866. each_1(dataArray, function (data) {
  10867. each_1(data, function (obj) {
  10868. var value = obj[yField];
  10869. var offset;
  10870. if (isArray_1(value)) {
  10871. var xValue = obj[xField];
  10872. var valueMax = cache[xValue];
  10873. offset = (max - valueMax) / 2;
  10874. var tmp = [];
  10875. /* eslint-disable no-loop-func */
  10876. each_1(value, function (subVal) {
  10877. // 多个字段
  10878. tmp.push(offset + subVal);
  10879. });
  10880. /* eslint-enable no-loop-func */
  10881. obj[yField] = tmp;
  10882. } else {
  10883. offset = (max - value) / 2;
  10884. obj[yField] = [offset, value + offset];
  10885. }
  10886. });
  10887. });
  10888. };
  10889. return Symmetric;
  10890. }(base);
  10891. base.Symmetric = Symmetric;
  10892. class Polar extends Base$1 {
  10893. _initDefaultCfg() {
  10894. this.type = 'polar';
  10895. this.startAngle = -Math.PI / 2;
  10896. this.endAngle = Math.PI * 3 / 2;
  10897. this.inner = 0;
  10898. this.innerRadius = 0; // alias
  10899. this.isPolar = true;
  10900. this.transposed = false;
  10901. this.center = null;
  10902. this.radius = null; // relative, 0 ~ 1
  10903. }
  10904. init(start, end) {
  10905. super.init(start, end);
  10906. var self = this;
  10907. var inner = self.inner || self.innerRadius;
  10908. var width = Math.abs(end.x - start.x);
  10909. var height = Math.abs(end.y - start.y);
  10910. var maxRadius;
  10911. var center;
  10912. if (self.startAngle === -Math.PI && self.endAngle === 0) {
  10913. maxRadius = Math.min(width / 2, height);
  10914. center = {
  10915. x: (start.x + end.x) / 2,
  10916. y: start.y
  10917. };
  10918. } else {
  10919. maxRadius = Math.min(width, height) / 2;
  10920. center = {
  10921. x: (start.x + end.x) / 2,
  10922. y: (start.y + end.y) / 2
  10923. };
  10924. }
  10925. var radius = self.radius;
  10926. if (radius > 0 && radius <= 1) {
  10927. maxRadius = maxRadius * radius;
  10928. }
  10929. this.x = {
  10930. start: self.startAngle,
  10931. end: self.endAngle
  10932. };
  10933. this.y = {
  10934. start: maxRadius * inner,
  10935. end: maxRadius
  10936. };
  10937. this.center = center;
  10938. this.circleRadius = maxRadius; // the radius value in px
  10939. }
  10940. _convertPoint(point) {
  10941. var self = this;
  10942. var center = self.center;
  10943. var transposed = self.transposed;
  10944. var xDim = transposed ? 'y' : 'x';
  10945. var yDim = transposed ? 'x' : 'y';
  10946. var x = self.x;
  10947. var y = self.y;
  10948. var angle = x.start + (x.end - x.start) * point[xDim];
  10949. var radius = y.start + (y.end - y.start) * point[yDim];
  10950. return {
  10951. x: center.x + Math.cos(angle) * radius,
  10952. y: center.y + Math.sin(angle) * radius
  10953. };
  10954. }
  10955. _invertPoint(point) {
  10956. var self = this;
  10957. var {
  10958. center,
  10959. transposed,
  10960. x,
  10961. y
  10962. } = self;
  10963. var xDim = transposed ? 'y' : 'x';
  10964. var yDim = transposed ? 'x' : 'y';
  10965. var m = [1, 0, 0, 1, 0, 0];
  10966. Matrix.rotate(m, m, x.start);
  10967. var startV = [1, 0];
  10968. Vector2.transformMat2d(startV, startV, m);
  10969. startV = [startV[0], startV[1]];
  10970. var pointV = [point.x - center.x, point.y - center.y];
  10971. if (Vector2.zero(pointV)) {
  10972. return {
  10973. x: 0,
  10974. y: 0
  10975. };
  10976. }
  10977. var theta = Vector2.angleTo(startV, pointV, x.end < x.start);
  10978. if (Math.abs(theta - Math.PI * 2) < 0.001) {
  10979. theta = 0;
  10980. }
  10981. var l = Vector2.length(pointV);
  10982. var percentX = theta / (x.end - x.start);
  10983. percentX = x.end - x.start > 0 ? percentX : -percentX;
  10984. var percentY = (l - y.start) / (y.end - y.start);
  10985. var rst = {};
  10986. rst[xDim] = percentX;
  10987. rst[yDim] = percentY;
  10988. return rst;
  10989. }
  10990. }
  10991. Base$1.Polar = Polar;
  10992. class Circle$1 extends Abastract {
  10993. _initDefaultCfg() {
  10994. super._initDefaultCfg();
  10995. this.startAngle = -Math.PI / 2; // start angle,in radian
  10996. this.endAngle = Math.PI * 3 / 2; // end angle, in radian
  10997. this.radius = null; // radius
  10998. this.center = null; // center
  10999. }
  11000. getOffsetPoint(value) {
  11001. var {
  11002. startAngle,
  11003. endAngle
  11004. } = this;
  11005. var angle = startAngle + (endAngle - startAngle) * value;
  11006. return this._getCirclePoint(angle);
  11007. }
  11008. _getCirclePoint(angle, radius) {
  11009. var self = this;
  11010. var center = self.center;
  11011. radius = radius || self.radius;
  11012. return {
  11013. x: center.x + Math.cos(angle) * radius,
  11014. y: center.y + Math.sin(angle) * radius
  11015. };
  11016. }
  11017. getTextAlignInfo(point, offset) {
  11018. var self = this;
  11019. var offsetVector = self.getOffsetVector(point, offset);
  11020. var align;
  11021. var baseLine = 'middle';
  11022. if (offsetVector[0] > 0) {
  11023. align = 'left';
  11024. } else if (offsetVector[0] < 0) {
  11025. align = 'right';
  11026. } else {
  11027. align = 'center';
  11028. if (offsetVector[1] > 0) {
  11029. baseLine = 'top';
  11030. } else if (offsetVector[1] < 0) {
  11031. baseLine = 'bottom';
  11032. }
  11033. }
  11034. return {
  11035. textAlign: align,
  11036. textBaseline: baseLine
  11037. };
  11038. }
  11039. getAxisVector(point) {
  11040. var center = this.center;
  11041. var factor = this.offsetFactor;
  11042. return [(point.y - center.y) * factor, (point.x - center.x) * -1 * factor];
  11043. }
  11044. drawLine(lineCfg) {
  11045. var {
  11046. center,
  11047. radius,
  11048. startAngle,
  11049. endAngle
  11050. } = this;
  11051. var container = this.getContainer(lineCfg.top);
  11052. container.addShape('arc', {
  11053. className: 'axis-line',
  11054. attrs: mix({
  11055. x: center.x,
  11056. y: center.y,
  11057. r: radius,
  11058. startAngle,
  11059. endAngle
  11060. }, lineCfg)
  11061. });
  11062. }
  11063. }
  11064. Abastract.Circle = Circle$1;
  11065. var KEYWORDS_PERCENT = {
  11066. min: 0,
  11067. median: 0.5,
  11068. max: 1
  11069. };
  11070. class GuideBase {
  11071. _initDefaultCfg() {}
  11072. constructor(cfg) {
  11073. this._initDefaultCfg();
  11074. deepMix(this, cfg);
  11075. }
  11076. _getNormalizedValue(val, scale) {
  11077. var rst;
  11078. if (isNil(KEYWORDS_PERCENT[val])) {
  11079. rst = scale.scale(val);
  11080. } else {
  11081. rst = KEYWORDS_PERCENT[val];
  11082. }
  11083. return rst;
  11084. }
  11085. parsePercentPoint(coord, position) {
  11086. var xPercent = parseFloat(position[0]) / 100;
  11087. var yPercent = parseFloat(position[1]) / 100;
  11088. var start = coord.start;
  11089. var end = coord.end;
  11090. var width = Math.abs(start.x - end.x);
  11091. var height = Math.abs(start.y - end.y);
  11092. var x = width * xPercent + Math.min(start.x, end.x);
  11093. var y = height * yPercent + Math.min(start.y, end.y);
  11094. return {
  11095. x,
  11096. y
  11097. };
  11098. }
  11099. parsePoint(coord, position) {
  11100. var self = this;
  11101. var xScale = self.xScale;
  11102. var yScales = self.yScales;
  11103. if (isFunction(position)) {
  11104. position = position(xScale, yScales); // position 必须是对象
  11105. } // 如果数据格式是 ['50%', '50%'] 的格式
  11106. // fix: 原始数据中可能会包含 'xxx5%xxx' 这样的数据,需要判断下 https://github.com/antvis/f2/issues/590
  11107. if (isString(position[0]) && position[0].indexOf('%') !== -1 && !isNaN(position[0].slice(0, -1))) {
  11108. return this.parsePercentPoint(coord, position);
  11109. }
  11110. var x = self._getNormalizedValue(position[0], xScale);
  11111. var y = self._getNormalizedValue(position[1], yScales[0]);
  11112. var point = coord.convertPoint({
  11113. x,
  11114. y
  11115. });
  11116. if (self.limitInPlot) {
  11117. // limit in chart plotRange
  11118. if (x >= 0 && x <= 1 && y >= 0 && y <= 1) {
  11119. return point;
  11120. }
  11121. return null;
  11122. }
  11123. return point;
  11124. }
  11125. /**
  11126. * render the guide component
  11127. * @param {Coord} coord coordinate instance
  11128. * @param {Canvas.Group} group the container
  11129. */
  11130. render()
  11131. /* coord,group */
  11132. {}
  11133. repaint() {
  11134. this.remove();
  11135. var {
  11136. coord,
  11137. container,
  11138. canvas
  11139. } = this;
  11140. if (container && !container.isDestroyed()) {
  11141. this.render(coord, container);
  11142. canvas.draw();
  11143. }
  11144. }
  11145. remove() {
  11146. var {
  11147. element
  11148. } = this;
  11149. element && element.remove(true);
  11150. }
  11151. changeVisible(visible) {
  11152. var self = this;
  11153. self.visible = visible;
  11154. var element = self.element;
  11155. if (!element) return;
  11156. if (element.set) {
  11157. element.set('visible', visible);
  11158. } else {
  11159. element.style.display = visible ? '' : 'none';
  11160. }
  11161. }
  11162. }
  11163. class Arc$1 extends GuideBase {
  11164. _initDefaultCfg() {
  11165. this.type = 'arc';
  11166. /**
  11167. * start point
  11168. * @type {Array | Function}
  11169. */
  11170. this.start = [];
  11171. /**
  11172. * end point
  11173. * @type {Array | Function}
  11174. */
  11175. this.end = [];
  11176. /**
  11177. * style configuration
  11178. * @type {Object}
  11179. */
  11180. this.style = {
  11181. stroke: '#999',
  11182. lineWidth: 1
  11183. };
  11184. }
  11185. render(coord, container) {
  11186. var self = this;
  11187. var start = self.parsePoint(coord, self.start);
  11188. var end = self.parsePoint(coord, self.end);
  11189. if (!start || !end) {
  11190. return;
  11191. }
  11192. var coordCenter = coord.center;
  11193. var radius = Math.sqrt((start.x - coordCenter.x) * (start.x - coordCenter.x) + (start.y - coordCenter.y) * (start.y - coordCenter.y));
  11194. var startAngle = Math.atan2(start.y - coordCenter.y, start.x - coordCenter.x);
  11195. var endAngle = Math.atan2(end.y - coordCenter.y, end.x - coordCenter.x);
  11196. var shape = container.addShape('arc', {
  11197. className: 'guide-arc',
  11198. attrs: mix({
  11199. x: coordCenter.x,
  11200. y: coordCenter.y,
  11201. r: radius,
  11202. startAngle,
  11203. endAngle
  11204. }, self.style)
  11205. });
  11206. self.element = shape;
  11207. return shape;
  11208. }
  11209. }
  11210. GuideBase.Arc = Arc$1;
  11211. function getOffsetFromAlign(alignX, alignY, width, height) {
  11212. var result = [];
  11213. if (alignX === 'left' && alignY === 'top') {
  11214. result[0] = 0;
  11215. result[1] = 0;
  11216. } else if (alignX === 'right' && alignY === 'top') {
  11217. result[0] = -width;
  11218. result[1] = 0;
  11219. } else if (alignX === 'left' && alignY === 'bottom') {
  11220. result[0] = 0;
  11221. result[1] = Math.floor(-height);
  11222. } else if (alignX === 'right' && alignY === 'bottom') {
  11223. result[0] = Math.floor(-width);
  11224. result[1] = Math.floor(-height);
  11225. } else if (alignX === 'right' && alignY === 'middle') {
  11226. result[0] = Math.floor(-width);
  11227. result[1] = Math.floor(-height / 2);
  11228. } else if (alignX === 'left' && alignY === 'middle') {
  11229. result[0] = 0;
  11230. result[1] = Math.floor(-height / 2);
  11231. } else if (alignX === 'center' && alignY === 'bottom') {
  11232. result[0] = Math.floor(-width / 2);
  11233. result[1] = Math.floor(-height);
  11234. } else if (alignX === 'center' && alignY === 'top') {
  11235. result[0] = Math.floor(-width / 2);
  11236. result[1] = 0;
  11237. } else {
  11238. result[0] = Math.floor(-width / 2);
  11239. result[1] = Math.floor(-height / 2);
  11240. }
  11241. return result;
  11242. }
  11243. function modifyCSS(DOM, CSS) {
  11244. for (var key in CSS) {
  11245. if (CSS.hasOwnProperty(key)) {
  11246. DOM.style[key] = CSS[key];
  11247. }
  11248. }
  11249. return DOM;
  11250. }
  11251. function createDom(str) {
  11252. var container = document.createElement('div');
  11253. str = str.replace(/(^\s*)|(\s*$)/g, '');
  11254. container.innerHTML = '' + str;
  11255. return container.childNodes[0];
  11256. }
  11257. class Html extends GuideBase {
  11258. _initDefaultCfg() {
  11259. this.type = 'html';
  11260. /**
  11261. * dom position
  11262. * @type {Object | Array}
  11263. */
  11264. this.position = null;
  11265. /**
  11266. * alignment for horizontal direction,can be 'left','center','right'
  11267. * @type {String}
  11268. */
  11269. this.alignX = 'center';
  11270. /**
  11271. * alignment for vertical direction,can be 'top', 'middle', 'bottom'
  11272. * @type {String}
  11273. */
  11274. this.alignY = 'middle';
  11275. /**
  11276. * offset for horizontal direction
  11277. * @type {Number}
  11278. */
  11279. this.offsetX = null;
  11280. /**
  11281. * offset for vertical direction
  11282. * @type {Number}
  11283. */
  11284. this.offsetY = null;
  11285. /**
  11286. * the html string
  11287. *@type {String | Function}
  11288. */
  11289. this.html = null;
  11290. } // override paint
  11291. render(coord, container) {
  11292. var self = this;
  11293. var position = self.parsePoint(coord, self.position);
  11294. if (!position) {
  11295. return;
  11296. }
  11297. var myNode = createDom(self.html);
  11298. myNode = modifyCSS(myNode, {
  11299. position: 'absolute',
  11300. top: Math.floor(position.y) + 'px',
  11301. left: Math.floor(position.x) + 'px',
  11302. visibility: 'hidden'
  11303. });
  11304. var canvasDom = container.get('canvas').get('el');
  11305. var parentNode = canvasDom.parentNode;
  11306. parentNode = modifyCSS(parentNode, {
  11307. position: 'relative'
  11308. });
  11309. var wrapperNode = createDom('<div class="guideWapper" style="position: absolute;top: 0; left: 0;"></div>');
  11310. parentNode.appendChild(wrapperNode);
  11311. wrapperNode.appendChild(myNode);
  11312. var canvasOffsetTop = canvasDom.offsetTop;
  11313. var canvasOffsetLeft = canvasDom.offsetLeft;
  11314. var {
  11315. alignX,
  11316. alignY,
  11317. offsetX,
  11318. offsetY
  11319. } = self;
  11320. var width = getWidth(myNode);
  11321. var height = getHeight(myNode);
  11322. var newOffset = getOffsetFromAlign(alignX, alignY, width, height);
  11323. position.x = position.x + newOffset[0] + canvasOffsetLeft;
  11324. position.y = position.y + newOffset[1] + canvasOffsetTop;
  11325. if (offsetX) {
  11326. position.x += offsetX;
  11327. }
  11328. if (offsetY) {
  11329. position.y += offsetY;
  11330. }
  11331. modifyCSS(myNode, {
  11332. top: Math.floor(position.y) + 'px',
  11333. left: Math.floor(position.x) + 'px',
  11334. visibility: 'visible'
  11335. });
  11336. self.element = wrapperNode;
  11337. }
  11338. remove() {
  11339. var element = this.element;
  11340. element && element.parentNode && element.parentNode.removeChild(element);
  11341. }
  11342. }
  11343. GuideBase.Html = Html;
  11344. class Line$4 extends GuideBase {
  11345. _initDefaultCfg() {
  11346. this.type = 'line';
  11347. this.start = [];
  11348. this.end = [];
  11349. this.style = {
  11350. stroke: '#000',
  11351. lineWidth: 1
  11352. };
  11353. }
  11354. render(coord, container) {
  11355. var points = [];
  11356. points[0] = this.parsePoint(coord, this.start);
  11357. points[1] = this.parsePoint(coord, this.end);
  11358. if (!points[0] || !points[1]) {
  11359. return;
  11360. }
  11361. var shape = container.addShape('Line', {
  11362. className: 'guide-line',
  11363. attrs: mix({
  11364. x1: points[0].x,
  11365. y1: points[0].y,
  11366. x2: points[1].x,
  11367. y2: points[1].y
  11368. }, this.style)
  11369. });
  11370. this.element = shape;
  11371. return shape;
  11372. }
  11373. }
  11374. GuideBase.Line = Line$4;
  11375. class Rect$2 extends GuideBase {
  11376. _initDefaultCfg() {
  11377. this.type = 'rect';
  11378. this.start = [];
  11379. this.end = [];
  11380. this.style = {
  11381. fill: '#CCD7EB',
  11382. opacity: 0.4
  11383. };
  11384. }
  11385. render(coord, container) {
  11386. var start = this.parsePoint(coord, this.start);
  11387. var end = this.parsePoint(coord, this.end);
  11388. if (!start || !end) {
  11389. return;
  11390. }
  11391. var shape = container.addShape('rect', {
  11392. className: 'guide-rect',
  11393. attrs: mix({
  11394. x: Math.min(start.x, end.x),
  11395. y: Math.min(start.y, end.y),
  11396. width: Math.abs(end.x - start.x),
  11397. height: Math.abs(start.y - end.y)
  11398. }, this.style)
  11399. });
  11400. this.element = shape;
  11401. return shape;
  11402. }
  11403. }
  11404. GuideBase.Rect = Rect$2;
  11405. class Text$1 extends GuideBase {
  11406. _initDefaultCfg() {
  11407. this.type = 'text';
  11408. /**
  11409. * the position of text
  11410. * @type {Function | Array}
  11411. */
  11412. this.position = null;
  11413. /**
  11414. * the display content
  11415. * @type {String}
  11416. */
  11417. this.content = null;
  11418. /**
  11419. * style configuration for text
  11420. * @type {Object}
  11421. */
  11422. this.style = {
  11423. fill: '#000'
  11424. };
  11425. /**
  11426. * offset of horizontal direction
  11427. * @type {Number}
  11428. */
  11429. this.offsetX = 0;
  11430. /**
  11431. * offset of vertical direction
  11432. * @type {Number}
  11433. */
  11434. this.offsetY = 0;
  11435. }
  11436. render(coord, container) {
  11437. var position = this.position;
  11438. var point = this.parsePoint(coord, position);
  11439. if (!point) {
  11440. return;
  11441. }
  11442. var {
  11443. content,
  11444. style,
  11445. offsetX,
  11446. offsetY
  11447. } = this;
  11448. if (offsetX) {
  11449. point.x += offsetX;
  11450. }
  11451. if (offsetY) {
  11452. point.y += offsetY;
  11453. }
  11454. var shape = container.addShape('text', {
  11455. className: 'guide-text',
  11456. attrs: mix({
  11457. x: point.x,
  11458. y: point.y,
  11459. text: content
  11460. }, style)
  11461. });
  11462. this.element = shape;
  11463. return shape;
  11464. }
  11465. }
  11466. GuideBase.Text = Text$1;
  11467. class Tag extends GuideBase {
  11468. _initDefaultCfg() {
  11469. this.type = 'tag';
  11470. this.position = null;
  11471. this.content = null;
  11472. this.direct = 'tl';
  11473. this.autoAdjust = true;
  11474. this.offsetX = 0;
  11475. this.offsetY = 0;
  11476. this.side = 4;
  11477. this.background = {
  11478. padding: 5,
  11479. radius: 2,
  11480. fill: '#1890FF'
  11481. };
  11482. this.textStyle = {
  11483. fontSize: 12,
  11484. fill: '#fff',
  11485. textAlign: 'center',
  11486. textBaseline: 'middle'
  11487. };
  11488. this.withPoint = true;
  11489. this.pointStyle = {
  11490. fill: '#1890FF',
  11491. r: 3,
  11492. lineWidth: 1,
  11493. stroke: '#fff'
  11494. };
  11495. }
  11496. _getDirect(container, point, tagWidth, tagHeight) {
  11497. var direct = this.direct;
  11498. var side = this.side;
  11499. var canvas = container.get('canvas');
  11500. var clientWidth = canvas.get('width');
  11501. var clientHeight = canvas.get('height');
  11502. var {
  11503. x,
  11504. y
  11505. } = point;
  11506. var vertical = direct[0];
  11507. var horizontal = direct[1]; // adjust for vertical direction
  11508. if (vertical === 't' && y - side - tagHeight < 0) {
  11509. vertical = 'b';
  11510. } else if (vertical === 'b' && y + side + tagHeight > clientHeight) {
  11511. vertical = 't';
  11512. } // adjust for horizontal direction
  11513. var diff = vertical === 'c' ? side : 0;
  11514. if (horizontal === 'l' && x - diff - tagWidth < 0) {
  11515. horizontal = 'r';
  11516. } else if (horizontal === 'r' && x + diff + tagWidth > clientWidth) {
  11517. horizontal = 'l';
  11518. } else if (horizontal === 'c') {
  11519. if (tagWidth / 2 + x + diff > clientWidth) {
  11520. horizontal = 'l';
  11521. } else if (x - tagWidth / 2 - diff < 0) {
  11522. horizontal = 'r';
  11523. }
  11524. }
  11525. direct = vertical + horizontal;
  11526. return direct;
  11527. }
  11528. render(coord, container) {
  11529. var position = this.parsePoint(coord, this.position);
  11530. if (!position) {
  11531. return;
  11532. } // 数据不在显示范围内时,x/y 会为NaN
  11533. if (isNaN(position.x) || isNaN(position.y)) {
  11534. return;
  11535. }
  11536. var {
  11537. content,
  11538. background,
  11539. textStyle
  11540. } = this;
  11541. var shapes = [];
  11542. var wrapperContainer = container.addGroup({
  11543. className: 'guide-tag'
  11544. });
  11545. if (this.withPoint) {
  11546. var pointShape = wrapperContainer.addShape('Circle', {
  11547. className: 'guide-tag-point',
  11548. attrs: mix({
  11549. x: position.x,
  11550. y: position.y
  11551. }, this.pointStyle)
  11552. });
  11553. shapes.push(pointShape);
  11554. }
  11555. var tagContainer = wrapperContainer.addGroup(); // create a text shape
  11556. var tagText = tagContainer.addShape('text', {
  11557. className: 'guide-tag-text',
  11558. zIndex: 1,
  11559. attrs: mix({
  11560. x: 0,
  11561. y: 0,
  11562. text: content
  11563. }, textStyle)
  11564. });
  11565. shapes.push(tagText); // create background box
  11566. var textBBox = tagText.getBBox();
  11567. var padding = parsePadding(background.padding);
  11568. var tagWidth = textBBox.width + padding[1] + padding[3];
  11569. var tagHeight = textBBox.height + padding[0] + padding[2];
  11570. var yMin = textBBox.minY - padding[0];
  11571. var xMin = textBBox.minX - padding[3];
  11572. var tagBg = tagContainer.addShape('rect', {
  11573. className: 'guide-tag-bg',
  11574. zIndex: -1,
  11575. attrs: mix({
  11576. x: xMin,
  11577. y: yMin,
  11578. width: tagWidth,
  11579. height: tagHeight
  11580. }, background)
  11581. });
  11582. shapes.push(tagBg);
  11583. var direct = this.autoAdjust ? this._getDirect(container, position, tagWidth, tagHeight) : this.direct;
  11584. var side = this.side;
  11585. var x = position.x + this.offsetX;
  11586. var y = position.y + this.offsetY;
  11587. var arrowPoints;
  11588. var radius = parsePadding(background.radius);
  11589. if (direct === 'tl') {
  11590. arrowPoints = [{
  11591. x: tagWidth + xMin - side - 1,
  11592. y: tagHeight + yMin - 1
  11593. }, // 这个 1 是为了防止出现白边
  11594. {
  11595. x: tagWidth + xMin,
  11596. y: tagHeight + yMin - 1
  11597. }, {
  11598. x: tagWidth + xMin,
  11599. y: tagHeight + side + yMin
  11600. }];
  11601. radius[2] = 0;
  11602. x = x - tagWidth;
  11603. y = y - side - tagHeight;
  11604. } else if (direct === 'cl') {
  11605. arrowPoints = [{
  11606. x: tagWidth + xMin - 1,
  11607. y: (tagHeight - side) / 2 + yMin - 1
  11608. }, {
  11609. x: tagWidth + xMin - 1,
  11610. y: (tagHeight + side) / 2 + yMin + 1
  11611. }, {
  11612. x: tagWidth + side + xMin,
  11613. y: tagHeight / 2 + yMin
  11614. }];
  11615. x = x - tagWidth - side;
  11616. y = y - tagHeight / 2;
  11617. } else if (direct === 'bl') {
  11618. arrowPoints = [{
  11619. x: tagWidth + xMin,
  11620. y: -side + yMin
  11621. }, {
  11622. x: tagWidth + xMin - side - 1,
  11623. y: yMin + 1
  11624. }, {
  11625. x: tagWidth + xMin,
  11626. y: yMin + 1
  11627. }];
  11628. radius[1] = 0;
  11629. x = x - tagWidth;
  11630. y = y + side;
  11631. } else if (direct === 'bc') {
  11632. arrowPoints = [{
  11633. x: tagWidth / 2 + xMin,
  11634. y: -side + yMin
  11635. }, {
  11636. x: (tagWidth - side) / 2 + xMin - 1,
  11637. y: yMin + 1
  11638. }, {
  11639. x: (tagWidth + side) / 2 + xMin + 1,
  11640. y: yMin + 1
  11641. }];
  11642. x = x - tagWidth / 2;
  11643. y = y + side;
  11644. } else if (direct === 'br') {
  11645. arrowPoints = [{
  11646. x: xMin,
  11647. y: yMin - side
  11648. }, {
  11649. x: xMin,
  11650. y: yMin + 1
  11651. }, {
  11652. x: xMin + side + 1,
  11653. y: yMin + 1
  11654. }];
  11655. radius[0] = 0;
  11656. y = y + side;
  11657. } else if (direct === 'cr') {
  11658. arrowPoints = [{
  11659. x: xMin - side,
  11660. y: tagHeight / 2 + yMin
  11661. }, {
  11662. x: xMin + 1,
  11663. y: (tagHeight - side) / 2 + yMin - 1
  11664. }, {
  11665. x: xMin + 1,
  11666. y: (tagHeight + side) / 2 + yMin + 1
  11667. }];
  11668. x = x + side;
  11669. y = y - tagHeight / 2;
  11670. } else if (direct === 'tr') {
  11671. arrowPoints = [{
  11672. x: xMin,
  11673. y: tagHeight + side + yMin
  11674. }, {
  11675. x: xMin,
  11676. y: tagHeight + yMin - 1
  11677. }, {
  11678. x: side + xMin + 1,
  11679. y: tagHeight + yMin - 1
  11680. }];
  11681. radius[3] = 0;
  11682. y = y - tagHeight - side;
  11683. } else if (direct === 'tc') {
  11684. arrowPoints = [{
  11685. x: (tagWidth - side) / 2 + xMin - 1,
  11686. y: tagHeight + yMin - 1
  11687. }, {
  11688. x: (tagWidth + side) / 2 + xMin + 1,
  11689. y: tagHeight + yMin - 1
  11690. }, {
  11691. x: tagWidth / 2 + xMin,
  11692. y: tagHeight + side + yMin
  11693. }];
  11694. x = x - tagWidth / 2;
  11695. y = y - tagHeight - side;
  11696. }
  11697. var sideShape = tagContainer.addShape('Polygon', {
  11698. className: 'guide-tag-side',
  11699. zIndex: 0,
  11700. attrs: {
  11701. points: arrowPoints,
  11702. fill: background.fill
  11703. }
  11704. });
  11705. shapes.push(sideShape);
  11706. tagBg.attr('radius', radius);
  11707. tagContainer.moveTo(x - xMin, y - yMin);
  11708. tagContainer.sort();
  11709. this.element = wrapperContainer;
  11710. return shapes;
  11711. }
  11712. }
  11713. GuideBase.Tag = Tag;
  11714. class Point$2 extends GuideBase {
  11715. _initDefaultCfg() {
  11716. this.type = 'point';
  11717. this.position = null;
  11718. this.offsetX = 0;
  11719. this.offsetY = 0;
  11720. this.style = {
  11721. fill: '#1890FF',
  11722. r: 3,
  11723. lineWidth: 1,
  11724. stroke: '#fff'
  11725. };
  11726. }
  11727. render(coord, container) {
  11728. var position = this.parsePoint(coord, this.position);
  11729. if (!position) return null;
  11730. var shape = container.addShape('Circle', {
  11731. className: 'guide-point',
  11732. attrs: mix({
  11733. x: position.x + this.offsetX,
  11734. y: position.y + this.offsetY
  11735. }, this.style)
  11736. });
  11737. this.element = shape;
  11738. return shape;
  11739. }
  11740. }
  11741. GuideBase.Point = Point$2;
  11742. /**
  11743. * marker shapes,used for tooltip and legend
  11744. */
  11745. var SYMBOLS = {
  11746. circle(x, y, r, ctx) {
  11747. ctx.arc(x, y, r, 0, Math.PI * 2, false);
  11748. },
  11749. square(x, y, r, ctx) {
  11750. ctx.moveTo(x - r, y - r);
  11751. ctx.lineTo(x + r, y - r);
  11752. ctx.lineTo(x + r, y + r);
  11753. ctx.lineTo(x - r, y + r);
  11754. ctx.closePath();
  11755. }
  11756. };
  11757. class Marker extends Shape$2 {
  11758. _initProperties() {
  11759. super._initProperties();
  11760. this._attrs.canFill = true;
  11761. this._attrs.canStroke = true;
  11762. this._attrs.type = 'marker';
  11763. }
  11764. getDefaultAttrs() {
  11765. return {
  11766. x: 0,
  11767. y: 0,
  11768. lineWidth: 0
  11769. };
  11770. }
  11771. createPath(context) {
  11772. var attrs = this.get('attrs');
  11773. var {
  11774. x,
  11775. y,
  11776. radius
  11777. } = attrs;
  11778. var symbol = attrs.symbol || 'circle';
  11779. var method;
  11780. if (isFunction(symbol)) {
  11781. method = symbol;
  11782. } else {
  11783. method = SYMBOLS[symbol];
  11784. }
  11785. context.beginPath();
  11786. method(x, y, radius, context, this);
  11787. }
  11788. calculateBox() {
  11789. var attrs = this.get('attrs');
  11790. var {
  11791. x,
  11792. y,
  11793. radius
  11794. } = attrs;
  11795. return {
  11796. minX: x - radius,
  11797. minY: y - radius,
  11798. maxX: x + radius,
  11799. maxY: y + radius
  11800. };
  11801. }
  11802. }
  11803. var MARKER_RADIUS = 3;
  11804. class List {
  11805. getDefaultCfg() {
  11806. return {
  11807. showTitle: false,
  11808. /**
  11809. * title string
  11810. * @type {?String}
  11811. */
  11812. title: null,
  11813. /**
  11814. * items array
  11815. * @type {?Array}
  11816. */
  11817. items: null,
  11818. /**
  11819. * offset between title and items
  11820. * @type {Number}
  11821. */
  11822. titleGap: 12,
  11823. /**
  11824. * offset between each item
  11825. * @type {Number}
  11826. */
  11827. itemGap: 10,
  11828. /**
  11829. * the offset between each item in vertical direaction
  11830. * @type {Number}
  11831. */
  11832. itemMarginBottom: 12,
  11833. /**
  11834. * the formatter for item text
  11835. * @type {[type]}
  11836. */
  11837. itemFormatter: null,
  11838. itemWidth: null,
  11839. /**
  11840. * offset between marker and text
  11841. * @type {Number}
  11842. */
  11843. wordSpace: 6,
  11844. x: 0,
  11845. y: 0,
  11846. layout: 'horizontal',
  11847. /**
  11848. * the join string of `name` and `value`
  11849. * @type {String}
  11850. */
  11851. joinString: ': '
  11852. };
  11853. }
  11854. constructor(cfg) {
  11855. deepMix(this, this.getDefaultCfg(), cfg);
  11856. this._init();
  11857. this._renderTitle();
  11858. this._renderItems();
  11859. }
  11860. _init() {
  11861. var container = new Group({
  11862. zIndex: this.zIndex || 0
  11863. });
  11864. this.container = container;
  11865. var wrapper = container.addGroup();
  11866. this.wrapper = wrapper;
  11867. var itemsGroup = wrapper.addGroup({
  11868. className: 'itemsGroup'
  11869. });
  11870. this.itemsGroup = itemsGroup;
  11871. if (this.parent) {
  11872. this.parent.add(container);
  11873. }
  11874. }
  11875. _renderTitle(title) {
  11876. title = title || this.title;
  11877. var titleShape = this.titleShape;
  11878. var titleHeight = 0;
  11879. if (this.showTitle && title) {
  11880. if (titleShape && !titleShape.get('destroyed')) {
  11881. titleShape.attr('text', title);
  11882. } else {
  11883. var {
  11884. wrapper,
  11885. titleStyle
  11886. } = this;
  11887. titleShape = wrapper.addShape('text', {
  11888. className: 'title',
  11889. attrs: mix({
  11890. x: 0,
  11891. y: 0,
  11892. text: title
  11893. }, titleStyle)
  11894. });
  11895. this.titleShape = titleShape;
  11896. }
  11897. titleHeight = titleShape.getBBox().height + this.titleGap;
  11898. }
  11899. this._titleHeight = titleHeight;
  11900. }
  11901. _renderItems(items) {
  11902. var self = this;
  11903. items = items || self.items;
  11904. if (!items) {
  11905. return;
  11906. }
  11907. if (self.reversed) {
  11908. items.reverse();
  11909. }
  11910. each(items, function (item, index) {
  11911. self._addItem(item, index);
  11912. });
  11913. if (items.length > 1) {
  11914. this._adjustItems();
  11915. }
  11916. this._renderBackground();
  11917. }
  11918. _renderBackground() {
  11919. var background = this.background;
  11920. if (background) {
  11921. var container = this.container;
  11922. var wrapper = this.wrapper;
  11923. var {
  11924. minX,
  11925. minY,
  11926. width,
  11927. height
  11928. } = wrapper.getBBox();
  11929. var padding = background.padding || [0, 0, 0, 0];
  11930. padding = parsePadding(padding);
  11931. var attrs = mix({
  11932. x: minX - padding[3],
  11933. y: minY - padding[0],
  11934. width: width + padding[1] + padding[3],
  11935. height: height + padding[0] + padding[2]
  11936. }, background);
  11937. var backShape = this.backShape;
  11938. if (backShape) {
  11939. backShape.attr(attrs);
  11940. } else {
  11941. backShape = container.addShape('Rect', {
  11942. zIndex: -1,
  11943. attrs
  11944. });
  11945. }
  11946. this.backShape = backShape;
  11947. container.sort();
  11948. }
  11949. }
  11950. _addItem(item) {
  11951. var itemsGroup = this.itemsGroup;
  11952. var itemGroup = itemsGroup.addGroup({
  11953. name: item.name,
  11954. value: item.value,
  11955. dataValue: item.dataValue,
  11956. checked: item.checked
  11957. });
  11958. var {
  11959. unCheckStyle,
  11960. unCheckColor,
  11961. nameStyle,
  11962. valueStyle,
  11963. wordSpace
  11964. } = this;
  11965. var {
  11966. marker,
  11967. value
  11968. } = item;
  11969. var startX = 0;
  11970. if (unCheckColor) {
  11971. unCheckStyle.fill = unCheckColor;
  11972. }
  11973. if (marker) {
  11974. var radius = marker.radius || MARKER_RADIUS;
  11975. var markerAttrs = mix({
  11976. x: radius,
  11977. y: this._titleHeight
  11978. }, marker);
  11979. if (item.checked === false) {
  11980. mix(markerAttrs, unCheckStyle);
  11981. }
  11982. var markerShape = new Marker({
  11983. className: 'item-marker',
  11984. attrs: markerAttrs
  11985. });
  11986. itemGroup.add(markerShape);
  11987. startX += markerShape.getBBox().width + wordSpace;
  11988. }
  11989. var nameText;
  11990. var name = item.name;
  11991. if (name) {
  11992. var joinString = this.joinString || '';
  11993. name = value ? name + joinString : name;
  11994. nameText = itemGroup.addShape('text', {
  11995. className: 'name',
  11996. attrs: mix({
  11997. x: startX,
  11998. y: this._titleHeight,
  11999. text: this._formatItemValue(name)
  12000. }, nameStyle, item.checked === false ? unCheckStyle : null)
  12001. });
  12002. }
  12003. if (value) {
  12004. var valueX = startX;
  12005. if (nameText) {
  12006. valueX += nameText.getBBox().width;
  12007. }
  12008. itemGroup.addShape('text', {
  12009. className: 'value',
  12010. attrs: mix({
  12011. x: valueX,
  12012. y: this._titleHeight,
  12013. text: value
  12014. }, valueStyle, item.checked === false ? unCheckStyle : null)
  12015. });
  12016. }
  12017. return itemGroup;
  12018. }
  12019. _formatItemValue(value) {
  12020. var formatter = this.itemFormatter;
  12021. if (formatter) {
  12022. value = formatter.call(this, value);
  12023. }
  12024. return value;
  12025. }
  12026. _getMaxItemWidth() {
  12027. var width;
  12028. var itemWidth = this.itemWidth;
  12029. if (isNumber(itemWidth) || isNil(itemWidth)) {
  12030. return itemWidth;
  12031. }
  12032. if (itemWidth === 'auto') {
  12033. var itemsGroup = this.itemsGroup;
  12034. var children = itemsGroup.get('children');
  12035. var count = children.length;
  12036. var maxItemWidth = 0;
  12037. for (var i = 0; i < count; i++) {
  12038. var {
  12039. width: _width
  12040. } = children[i].getBBox();
  12041. maxItemWidth = Math.max(maxItemWidth, _width);
  12042. }
  12043. var maxLength = this.maxLength;
  12044. var itemGap = this.itemGap;
  12045. var twoAvgWidth = (maxLength - itemGap) / 2;
  12046. var threeAvgWidth = (maxLength - itemGap * 2) / 3;
  12047. if (count === 2) {
  12048. width = Math.max(maxItemWidth, twoAvgWidth);
  12049. } else {
  12050. // 1. max <= 3Avg, 3Avg
  12051. // 2. 3Avg < max && max < 2avg, 2avg
  12052. // 3. max > 2avg, max, one column
  12053. if (maxItemWidth <= threeAvgWidth) {
  12054. width = threeAvgWidth;
  12055. } else if (maxItemWidth <= twoAvgWidth) {
  12056. width = twoAvgWidth;
  12057. } else {
  12058. width = maxItemWidth;
  12059. }
  12060. }
  12061. return width;
  12062. }
  12063. }
  12064. _adjustHorizontal() {
  12065. var {
  12066. maxLength,
  12067. itemsGroup
  12068. } = this;
  12069. var children = itemsGroup.get('children');
  12070. var {
  12071. itemGap,
  12072. itemMarginBottom
  12073. } = this;
  12074. var titleHeight = this._titleHeight;
  12075. var row = 0;
  12076. var rowWidth = 0;
  12077. var width;
  12078. var height;
  12079. var itemWidth = this._getMaxItemWidth();
  12080. var legendHitBoxes = [];
  12081. for (var i = 0, len = children.length; i < len; i++) {
  12082. var child = children[i];
  12083. var box = child.getBBox();
  12084. var childHeight = box.height;
  12085. var childWidth = box.width;
  12086. width = itemWidth || childWidth;
  12087. height = childHeight + itemMarginBottom;
  12088. if (width - (maxLength - rowWidth) > 0.0001) {
  12089. row++;
  12090. rowWidth = 0;
  12091. }
  12092. child.moveTo(rowWidth, row * height);
  12093. legendHitBoxes.push({
  12094. x: rowWidth,
  12095. y: row * height + titleHeight - childHeight / 2,
  12096. width: childWidth * 1.375,
  12097. height: childHeight * 1.375
  12098. });
  12099. rowWidth += width + itemGap;
  12100. }
  12101. this.legendHitBoxes = legendHitBoxes;
  12102. return;
  12103. }
  12104. _adjustVertical() {
  12105. var {
  12106. maxLength,
  12107. itemsGroup
  12108. } = this;
  12109. var {
  12110. itemGap,
  12111. itemMarginBottom,
  12112. itemWidth
  12113. } = this;
  12114. var titleHeight = this._titleHeight;
  12115. var children = itemsGroup.get('children');
  12116. var colHeight = 0;
  12117. var width;
  12118. var height;
  12119. var maxItemWidth = 0;
  12120. var totalWidth = 0;
  12121. var legendHitBoxes = [];
  12122. for (var i = 0, length = children.length; i < length; i++) {
  12123. var child = children[i];
  12124. var bbox = child.getBBox();
  12125. width = bbox.width;
  12126. height = bbox.height;
  12127. if (isNumber(itemWidth)) {
  12128. maxItemWidth = itemWidth + itemGap;
  12129. } else if (width > maxItemWidth) {
  12130. maxItemWidth = width + itemGap;
  12131. }
  12132. if (maxLength - colHeight < height) {
  12133. colHeight = 0;
  12134. totalWidth += maxItemWidth;
  12135. child.moveTo(totalWidth, 0);
  12136. legendHitBoxes.push({
  12137. x: totalWidth,
  12138. y: titleHeight - height / 2,
  12139. width: width * 1.375,
  12140. height: height * 1.375
  12141. });
  12142. } else {
  12143. child.moveTo(totalWidth, colHeight);
  12144. legendHitBoxes.push({
  12145. x: totalWidth,
  12146. y: colHeight - height / 2 + titleHeight,
  12147. width: width * 1.375,
  12148. height: height * 1.375
  12149. });
  12150. }
  12151. colHeight += height + itemMarginBottom;
  12152. }
  12153. this.legendHitBoxes = legendHitBoxes;
  12154. return;
  12155. }
  12156. _adjustItems() {
  12157. var layout = this.layout;
  12158. if (layout === 'horizontal') {
  12159. this._adjustHorizontal();
  12160. } else {
  12161. this._adjustVertical();
  12162. }
  12163. }
  12164. moveTo(x, y) {
  12165. this.x = x;
  12166. this.y = y;
  12167. var container = this.container;
  12168. container && container.moveTo(x, y);
  12169. return this;
  12170. }
  12171. setItems(items) {
  12172. this.clearItems();
  12173. this._renderItems(items);
  12174. }
  12175. setTitle(title) {
  12176. this._renderTitle(title);
  12177. }
  12178. clearItems() {
  12179. var itemsGroup = this.itemsGroup;
  12180. itemsGroup.clear();
  12181. }
  12182. getWidth() {
  12183. var container = this.container;
  12184. var bbox = container.getBBox();
  12185. return bbox.width;
  12186. }
  12187. getHeight() {
  12188. var container = this.container;
  12189. var bbox = container.getBBox();
  12190. return bbox.height;
  12191. }
  12192. show() {
  12193. var container = this.container;
  12194. container.show();
  12195. }
  12196. hide() {
  12197. var container = this.container;
  12198. container.hide();
  12199. }
  12200. clear() {
  12201. var container = this.container;
  12202. container.clear();
  12203. container.remove(true);
  12204. }
  12205. }
  12206. class TextBox {
  12207. getDefaultCfg() {
  12208. return {
  12209. x: 0,
  12210. y: 0,
  12211. content: '',
  12212. textStyle: {
  12213. fontSize: 12,
  12214. fill: '#fff',
  12215. textAlign: 'center',
  12216. textBaseline: 'middle',
  12217. fontFamily: 'Arial'
  12218. },
  12219. background: {
  12220. radius: 1,
  12221. fill: 'rgba(0, 0, 0, 0.65)',
  12222. padding: [3, 5]
  12223. },
  12224. width: 0,
  12225. height: 0,
  12226. className: ''
  12227. };
  12228. }
  12229. constructor(cfg) {
  12230. deepMix(this, this.getDefaultCfg(), cfg);
  12231. this._init();
  12232. var {
  12233. content,
  12234. x,
  12235. y
  12236. } = this;
  12237. if (!isNil(content)) {
  12238. this.updateContent(content);
  12239. }
  12240. this.updatePosition(x, y);
  12241. }
  12242. _init() {
  12243. var {
  12244. content,
  12245. textStyle,
  12246. background,
  12247. className,
  12248. visible,
  12249. context
  12250. } = this;
  12251. var container = new Group({
  12252. context,
  12253. className,
  12254. zIndex: 0,
  12255. visible
  12256. });
  12257. var text = container.addShape('Text', {
  12258. className: className + '-text',
  12259. zIndex: 1,
  12260. attrs: mix({
  12261. text: content,
  12262. x: 0,
  12263. y: 0
  12264. }, textStyle)
  12265. });
  12266. var backgroundShape = container.addShape('Rect', {
  12267. className: className + '-bg',
  12268. zIndex: -1,
  12269. attrs: mix({
  12270. x: 0,
  12271. y: 0,
  12272. width: 0,
  12273. height: 0
  12274. }, background)
  12275. });
  12276. container.sort();
  12277. this.container = container;
  12278. this.textShape = text;
  12279. this.backgroundShape = backgroundShape;
  12280. }
  12281. _getBBox() {
  12282. var textShape = this.textShape;
  12283. var background = this.background;
  12284. var textBBox = textShape.getBBox();
  12285. var padding = parsePadding(background.padding);
  12286. var width = textBBox.width + padding[1] + padding[3];
  12287. var height = textBBox.height + padding[0] + padding[2];
  12288. var x = textBBox.minX - padding[3];
  12289. var y = textBBox.minY - padding[0];
  12290. return {
  12291. x,
  12292. y,
  12293. width,
  12294. height
  12295. };
  12296. }
  12297. updateContent(text) {
  12298. var {
  12299. textShape,
  12300. backgroundShape
  12301. } = this;
  12302. if (!isNil(text)) {
  12303. if (!isObject(text)) {
  12304. text = {
  12305. text
  12306. };
  12307. }
  12308. textShape.attr(text); // update box shape
  12309. var {
  12310. x,
  12311. y,
  12312. width: tipWidth,
  12313. height: tipHeight
  12314. } = this._getBBox();
  12315. var width = this.width || tipWidth;
  12316. var height = this.height || tipHeight;
  12317. backgroundShape.attr({
  12318. x,
  12319. y,
  12320. width,
  12321. height
  12322. });
  12323. this._width = width;
  12324. this._height = height;
  12325. this.content = text.text;
  12326. }
  12327. }
  12328. updatePosition(x, y) {
  12329. var container = this.container;
  12330. var {
  12331. x: xMin,
  12332. y: yMin
  12333. } = this._getBBox();
  12334. container.moveTo(x - xMin, y - yMin);
  12335. this.x = x - xMin;
  12336. this.y = y - yMin;
  12337. }
  12338. getWidth() {
  12339. return this._width;
  12340. }
  12341. getHeight() {
  12342. return this._height;
  12343. }
  12344. show() {
  12345. this.container.show();
  12346. }
  12347. hide() {
  12348. this.container.hide();
  12349. }
  12350. clear() {
  12351. var container = this.container;
  12352. container.clear();
  12353. container.remove(true);
  12354. this.container = null;
  12355. this.textShape = null;
  12356. this.backgroundShape = null;
  12357. }
  12358. }
  12359. var GAP = 4;
  12360. /**
  12361. * TODOList:
  12362. * 1. 移除 fixed 参数
  12363. */
  12364. class Tooltip {
  12365. getDefaultCfg() {
  12366. return {
  12367. /**
  12368. * wether show the crosshairs
  12369. * @type {Object}
  12370. */
  12371. showCrosshairs: false,
  12372. /**
  12373. * the style for crosshairs
  12374. * @type {Object}
  12375. */
  12376. crosshairsStyle: {
  12377. stroke: 'rgba(0, 0, 0, 0.25)',
  12378. lineWidth: 1
  12379. },
  12380. /**
  12381. * the type of crosshairs, optional value is 'x', 'y' or 'xy', default is 'y'
  12382. */
  12383. crosshairsType: 'y',
  12384. /**
  12385. * show or hide the x axis tip
  12386. */
  12387. showXTip: false,
  12388. /**
  12389. * show or hide the y axis tip
  12390. */
  12391. showYTip: false,
  12392. xTip: null,
  12393. xTipBackground: {
  12394. radius: 1,
  12395. fill: 'rgba(0, 0, 0, 0.65)',
  12396. padding: [3, 5]
  12397. },
  12398. xTipTextStyle: {
  12399. fontSize: 12,
  12400. fill: '#fff',
  12401. textAlign: 'center',
  12402. textBaseline: 'middle'
  12403. },
  12404. yTip: null,
  12405. yTipBackground: {
  12406. radius: 1,
  12407. fill: 'rgba(0, 0, 0, 0.65)',
  12408. padding: [3, 5]
  12409. },
  12410. yTipTextStyle: {
  12411. fontSize: 12,
  12412. fill: '#fff',
  12413. textAlign: 'center',
  12414. textBaseline: 'middle'
  12415. },
  12416. /**
  12417. * the style for tooltip container's background
  12418. * @type {Object}
  12419. */
  12420. background: null,
  12421. /**
  12422. * layout, can be horizontal or vertical
  12423. * @type {String}
  12424. */
  12425. layout: 'horizontal',
  12426. offsetX: 0,
  12427. offsetY: 0
  12428. };
  12429. }
  12430. constructor(cfg) {
  12431. deepMix(this, this.getDefaultCfg(), cfg);
  12432. var {
  12433. frontPlot,
  12434. custom
  12435. } = this;
  12436. if (!custom) {
  12437. // custom means user do customize
  12438. var container = new List(mix({
  12439. parent: frontPlot,
  12440. zIndex: 3
  12441. }, cfg));
  12442. this.container = container;
  12443. var {
  12444. fixed,
  12445. background
  12446. } = this;
  12447. if (!fixed) {
  12448. this.tooltipArrow = frontPlot.addShape('Polygon', {
  12449. className: 'tooltip-arrow',
  12450. visible: false,
  12451. zIndex: 2,
  12452. attrs: mix({
  12453. points: []
  12454. }, background)
  12455. });
  12456. }
  12457. }
  12458. if (this.showXTip) {
  12459. var {
  12460. xTipBackground,
  12461. xTipTextStyle
  12462. } = this;
  12463. var xTipBox = new TextBox({
  12464. context: frontPlot.get('context'),
  12465. className: 'xTip',
  12466. background: xTipBackground,
  12467. textStyle: xTipTextStyle,
  12468. visible: false
  12469. });
  12470. frontPlot.add(xTipBox.container);
  12471. this.xTipBox = xTipBox;
  12472. }
  12473. if (this.showYTip) {
  12474. var {
  12475. yTipBackground,
  12476. yTipTextStyle
  12477. } = this;
  12478. var yTipBox = new TextBox({
  12479. context: frontPlot.get('context'),
  12480. className: 'yTip',
  12481. background: yTipBackground,
  12482. textStyle: yTipTextStyle,
  12483. visible: false
  12484. });
  12485. frontPlot.add(yTipBox.container);
  12486. this.yTipBox = yTipBox;
  12487. }
  12488. if (this.showCrosshairs) {
  12489. this._renderCrosshairs();
  12490. }
  12491. frontPlot.sort();
  12492. }
  12493. setContent(title, items) {
  12494. this.title = title;
  12495. this.items = items;
  12496. if (!this.custom) {
  12497. var container = this.container;
  12498. container.setTitle(title);
  12499. container.setItems(items);
  12500. }
  12501. }
  12502. setYTipContent(val) {
  12503. var yTip = this.yTip;
  12504. if (isFunction(yTip)) {
  12505. val = yTip(val);
  12506. } else {
  12507. val = mix({
  12508. text: val
  12509. }, yTip);
  12510. }
  12511. this.yTipBox && this.yTipBox.updateContent(val);
  12512. }
  12513. setYTipPosition(pos) {
  12514. var plotRange = this.plotRange;
  12515. var crosshairsShapeX = this.crosshairsShapeX;
  12516. if (this.showYTip) {
  12517. var yTipBox = this.yTipBox;
  12518. var yTipHeight = yTipBox.getHeight();
  12519. var yTipWidth = yTipBox.getWidth();
  12520. var posX = plotRange.tl.x - yTipWidth;
  12521. var posY = pos - yTipHeight / 2;
  12522. if (posY <= plotRange.tl.y) {
  12523. posY = plotRange.tl.y;
  12524. }
  12525. if (posY + yTipHeight >= plotRange.br.y) {
  12526. posY = plotRange.br.y - yTipHeight;
  12527. }
  12528. if (posX < 0) {
  12529. posX = plotRange.tl.x;
  12530. crosshairsShapeX && crosshairsShapeX.attr('x1', plotRange.tl.x + yTipWidth);
  12531. }
  12532. yTipBox.updatePosition(posX, posY);
  12533. }
  12534. }
  12535. setXTipContent(val) {
  12536. var xTip = this.xTip;
  12537. if (isFunction(xTip)) {
  12538. val = xTip(val);
  12539. } else {
  12540. val = mix({
  12541. text: val
  12542. }, xTip);
  12543. }
  12544. this.xTipBox && this.xTipBox.updateContent(val);
  12545. }
  12546. setXTipPosition(pos) {
  12547. var {
  12548. showXTip,
  12549. canvas,
  12550. plotRange,
  12551. xTipBox,
  12552. crosshairsShapeY
  12553. } = this;
  12554. if (showXTip) {
  12555. // const el = canvas.get('el');
  12556. // const canvasHeight = Util.getHeight(el);
  12557. var canvasHeight = canvas.get('height');
  12558. var xTipWidth = xTipBox.getWidth();
  12559. var xTipHeight = xTipBox.getHeight();
  12560. var posX = pos - xTipWidth / 2;
  12561. var posY = plotRange.br.y;
  12562. if (posX <= plotRange.tl.x) {
  12563. posX = plotRange.tl.x;
  12564. }
  12565. if (posX + xTipWidth >= plotRange.tr.x) {
  12566. posX = plotRange.tr.x - xTipWidth;
  12567. }
  12568. if (canvasHeight - posY < xTipHeight) {
  12569. posY -= xTipHeight;
  12570. }
  12571. xTipBox.updatePosition(posX, posY);
  12572. crosshairsShapeY && crosshairsShapeY.attr('y1', posY);
  12573. }
  12574. }
  12575. setXCrosshairPosition(pos) {
  12576. this.crosshairsShapeX && this.crosshairsShapeX.moveTo(0, pos);
  12577. }
  12578. setYCrosshairPosition(pos) {
  12579. this.crosshairsShapeY && this.crosshairsShapeY.moveTo(pos, 0);
  12580. }
  12581. setPosition(items) {
  12582. var {
  12583. container,
  12584. plotRange,
  12585. offsetX,
  12586. offsetY,
  12587. fixed,
  12588. tooltipArrow
  12589. } = this;
  12590. if (!container) {
  12591. return;
  12592. }
  12593. var containerBBox = container.container.getBBox();
  12594. var {
  12595. minX,
  12596. minY,
  12597. width,
  12598. height
  12599. } = containerBBox;
  12600. var {
  12601. tl,
  12602. tr
  12603. } = plotRange;
  12604. var posX = 0;
  12605. var posY = tl.y - height - GAP + offsetY;
  12606. if (fixed) {
  12607. var x = (tl.x + tr.x) / 2;
  12608. posX = x - width / 2 + offsetX;
  12609. } else {
  12610. var _x;
  12611. if (items.length > 1) {
  12612. _x = (items[0].x + items[items.length - 1].x) / 2;
  12613. } else {
  12614. _x = items[0].x;
  12615. }
  12616. posX = _x - width / 2 + offsetX;
  12617. if (posX < tl.x) {
  12618. posX = tl.x;
  12619. }
  12620. if (posX + width > tr.x) {
  12621. posX = tr.x - width;
  12622. }
  12623. if (tooltipArrow) {
  12624. tooltipArrow.attr('points', [{
  12625. x: _x - 3,
  12626. y: tl.y - GAP + offsetY
  12627. }, {
  12628. x: _x + 3,
  12629. y: tl.y - GAP + offsetY
  12630. }, {
  12631. x: _x,
  12632. y: tl.y + offsetY
  12633. }]);
  12634. var backShape = container.backShape;
  12635. var radius = parsePadding(backShape.attr('radius'));
  12636. if (_x === tl.x) {
  12637. radius[3] = 0;
  12638. tooltipArrow.attr('points', [{
  12639. x: tl.x,
  12640. y: tl.y + offsetY
  12641. }, {
  12642. x: tl.x,
  12643. y: tl.y - GAP + offsetY
  12644. }, {
  12645. x: tl.x + GAP,
  12646. y: tl.y - GAP + offsetY
  12647. }]);
  12648. } else if (_x === tr.x) {
  12649. radius[2] = 0;
  12650. tooltipArrow.attr('points', [{
  12651. x: tr.x,
  12652. y: tl.y + offsetY
  12653. }, {
  12654. x: tr.x - GAP,
  12655. y: tl.y - GAP + offsetY
  12656. }, {
  12657. x: tr.x,
  12658. y: tl.y - GAP + offsetY
  12659. }]);
  12660. }
  12661. backShape.attr('radius', radius);
  12662. }
  12663. }
  12664. container.moveTo(posX - minX, posY - minY);
  12665. }
  12666. setMarkers() {
  12667. var cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  12668. var self = this;
  12669. var {
  12670. items,
  12671. style,
  12672. type
  12673. } = cfg;
  12674. var markerGroup = self._getMarkerGroup(type);
  12675. if (type === 'circle') {
  12676. for (var i = 0, length = items.length; i < length; i++) {
  12677. var item = items[i];
  12678. var marker = new Marker({
  12679. className: 'tooltip-circle-marker',
  12680. attrs: mix({
  12681. x: item.x,
  12682. y: item.y,
  12683. stroke: item.color
  12684. }, style)
  12685. });
  12686. markerGroup.add(marker);
  12687. }
  12688. } else {
  12689. markerGroup.addShape('rect', {
  12690. className: 'tooltip-rect-marker',
  12691. attrs: style
  12692. });
  12693. }
  12694. }
  12695. clearMarkers() {
  12696. var markerGroup = this.markerGroup;
  12697. markerGroup && markerGroup.clear();
  12698. }
  12699. show() {
  12700. var crosshairsShapeX = this.crosshairsShapeX;
  12701. var crosshairsShapeY = this.crosshairsShapeY;
  12702. var markerGroup = this.markerGroup;
  12703. var container = this.container;
  12704. var tooltipArrow = this.tooltipArrow;
  12705. var xTipBox = this.xTipBox;
  12706. var yTipBox = this.yTipBox;
  12707. var canvas = this.canvas;
  12708. crosshairsShapeX && crosshairsShapeX.show();
  12709. crosshairsShapeY && crosshairsShapeY.show();
  12710. markerGroup && markerGroup.show();
  12711. container && container.show();
  12712. tooltipArrow && tooltipArrow.show();
  12713. xTipBox && xTipBox.show();
  12714. yTipBox && yTipBox.show();
  12715. canvas.draw();
  12716. }
  12717. hide() {
  12718. var crosshairsShapeX = this.crosshairsShapeX;
  12719. var crosshairsShapeY = this.crosshairsShapeY;
  12720. var markerGroup = this.markerGroup;
  12721. var container = this.container;
  12722. var tooltipArrow = this.tooltipArrow;
  12723. var xTipBox = this.xTipBox;
  12724. var yTipBox = this.yTipBox;
  12725. crosshairsShapeX && crosshairsShapeX.hide();
  12726. crosshairsShapeY && crosshairsShapeY.hide();
  12727. markerGroup && markerGroup.hide();
  12728. container && container.hide();
  12729. tooltipArrow && tooltipArrow.hide();
  12730. xTipBox && xTipBox.hide();
  12731. yTipBox && yTipBox.hide();
  12732. }
  12733. destroy() {
  12734. var crosshairsShapeX = this.crosshairsShapeX;
  12735. var crosshairsShapeY = this.crosshairsShapeY;
  12736. var markerGroup = this.markerGroup;
  12737. var container = this.container;
  12738. var tooltipArrow = this.tooltipArrow;
  12739. var xTipBox = this.xTipBox;
  12740. var yTipBox = this.yTipBox;
  12741. crosshairsShapeX && crosshairsShapeX.remove(true);
  12742. crosshairsShapeY && crosshairsShapeY.remove(true);
  12743. markerGroup && markerGroup.remove(true);
  12744. tooltipArrow && tooltipArrow.remove(true);
  12745. container && container.clear();
  12746. xTipBox && xTipBox.clear();
  12747. yTipBox && yTipBox.clear();
  12748. this.destroyed = true;
  12749. }
  12750. _getMarkerGroup(type) {
  12751. var markerGroup = this.markerGroup;
  12752. if (!markerGroup) {
  12753. if (type === 'circle') {
  12754. markerGroup = this.frontPlot.addGroup({
  12755. zIndex: 1
  12756. });
  12757. this.frontPlot.sort();
  12758. } else {
  12759. markerGroup = this.backPlot.addGroup();
  12760. }
  12761. this.markerGroup = markerGroup;
  12762. } else {
  12763. markerGroup.clear();
  12764. }
  12765. return markerGroup;
  12766. }
  12767. _renderCrosshairs() {
  12768. var {
  12769. crosshairsType,
  12770. crosshairsStyle,
  12771. frontPlot,
  12772. plotRange
  12773. } = this;
  12774. var {
  12775. tl,
  12776. br
  12777. } = plotRange;
  12778. if (directionEnabled(crosshairsType, 'x')) {
  12779. this.crosshairsShapeX = frontPlot.addShape('Line', {
  12780. className: 'tooltip-crosshairs-x',
  12781. zIndex: 0,
  12782. visible: false,
  12783. attrs: mix({
  12784. x1: tl.x,
  12785. y1: 0,
  12786. x2: br.x,
  12787. y2: 0
  12788. }, crosshairsStyle)
  12789. });
  12790. }
  12791. if (directionEnabled(crosshairsType, 'y')) {
  12792. this.crosshairsShapeY = frontPlot.addShape('Line', {
  12793. className: 'tooltip-crosshairs-y',
  12794. zIndex: 0,
  12795. visible: false,
  12796. attrs: mix({
  12797. x1: 0,
  12798. y1: br.y,
  12799. x2: 0,
  12800. y2: tl.y
  12801. }, crosshairsStyle)
  12802. });
  12803. }
  12804. }
  12805. }
  12806. function _defineProperty$2(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  12807. Global.tooltip = deepMix({
  12808. triggerOn: 'press',
  12809. triggerOff: 'pressend',
  12810. alwaysShow: false,
  12811. showTitle: false,
  12812. showCrosshairs: false,
  12813. crosshairsStyle: {
  12814. stroke: 'rgba(0, 0, 0, 0.25)',
  12815. lineWidth: 1
  12816. },
  12817. showTooltipMarker: true,
  12818. background: {
  12819. radius: 1,
  12820. fill: 'rgba(0, 0, 0, 0.65)',
  12821. padding: [3, 5]
  12822. },
  12823. titleStyle: {
  12824. fontSize: 12,
  12825. fill: '#fff',
  12826. textAlign: 'start',
  12827. textBaseline: 'top'
  12828. },
  12829. nameStyle: {
  12830. fontSize: 12,
  12831. fill: 'rgba(255, 255, 255, 0.65)',
  12832. textAlign: 'start',
  12833. textBaseline: 'middle'
  12834. },
  12835. valueStyle: {
  12836. fontSize: 12,
  12837. fill: '#fff',
  12838. textAlign: 'start',
  12839. textBaseline: 'middle'
  12840. },
  12841. showItemMarker: true,
  12842. itemMarkerStyle: {
  12843. radius: 3,
  12844. symbol: 'circle',
  12845. lineWidth: 1,
  12846. stroke: '#fff'
  12847. },
  12848. layout: 'horizontal',
  12849. snap: false
  12850. }, Global.tooltip || {});
  12851. function _getTooltipValueScale(geom) {
  12852. var colorAttr = geom.getAttr('color');
  12853. if (colorAttr) {
  12854. var colorScale = colorAttr.getScale(colorAttr.type);
  12855. if (colorScale.isLinear) {
  12856. return colorScale;
  12857. }
  12858. }
  12859. var xScale = geom.getXScale();
  12860. var yScale = geom.getYScale();
  12861. if (yScale) {
  12862. return yScale;
  12863. }
  12864. return xScale;
  12865. }
  12866. function getTooltipName(geom, origin) {
  12867. var name;
  12868. var nameScale;
  12869. var groupScales = geom._getGroupScales();
  12870. if (groupScales.length) {
  12871. each(groupScales, function (scale) {
  12872. nameScale = scale;
  12873. return false;
  12874. });
  12875. }
  12876. if (nameScale) {
  12877. var field = nameScale.field;
  12878. name = nameScale.getText(origin[field]);
  12879. } else {
  12880. var valueScale = _getTooltipValueScale(geom);
  12881. name = valueScale.alias || valueScale.field;
  12882. }
  12883. return name;
  12884. }
  12885. function getTooltipValue(geom, origin) {
  12886. var scale = _getTooltipValueScale(geom);
  12887. return scale.getText(origin[scale.field]);
  12888. }
  12889. function getTooltipTitle(geom, origin) {
  12890. var position = geom.getAttr('position');
  12891. var field = position.getFields()[0];
  12892. var scale = geom.get('scales')[field];
  12893. return scale.getText(origin[scale.field]);
  12894. }
  12895. function _indexOfArray(items, item) {
  12896. var rst = -1;
  12897. each(items, function (sub, index) {
  12898. if (sub.title === item.title && sub.name === item.name && sub.value === item.value && sub.color === item.color) {
  12899. rst = index;
  12900. return false;
  12901. }
  12902. });
  12903. return rst;
  12904. }
  12905. function _uniqItems(items) {
  12906. var tmp = [];
  12907. each(items, function (item) {
  12908. var index = _indexOfArray(tmp, item);
  12909. if (index === -1) {
  12910. tmp.push(item);
  12911. } else {
  12912. tmp[index] = item;
  12913. }
  12914. });
  12915. return tmp;
  12916. }
  12917. function isEqual$1(arr1, arr2) {
  12918. return JSON.stringify(arr1) === JSON.stringify(arr2);
  12919. }
  12920. class TooltipController {
  12921. constructor(cfg) {
  12922. var _this = this;
  12923. _defineProperty$2(this, "handleShowEvent", function (ev) {
  12924. var chart = _this.chart;
  12925. if (!_this.enable) return;
  12926. var plot = chart.get('plotRange');
  12927. var point = createEvent(ev, chart);
  12928. if (!isPointInPlot(point, plot) && !_this._tooltipCfg.alwaysShow) {
  12929. // not in chart plot
  12930. _this.hideTooltip();
  12931. return;
  12932. }
  12933. var lastTimeStamp = _this.timeStamp;
  12934. var timeStamp = +new Date();
  12935. if (timeStamp - lastTimeStamp > 16) {
  12936. _this.showTooltip(point);
  12937. _this.timeStamp = timeStamp;
  12938. }
  12939. });
  12940. _defineProperty$2(this, "handleHideEvent", function () {
  12941. if (!_this.enable) return;
  12942. _this.hideTooltip();
  12943. });
  12944. this.enable = true;
  12945. this.cfg = {};
  12946. this.tooltip = null;
  12947. this.chart = null;
  12948. this.timeStamp = 0;
  12949. mix(this, cfg);
  12950. var _chart = this.chart;
  12951. var canvas = _chart.get('canvas');
  12952. this.canvas = canvas;
  12953. this.canvasDom = canvas.get('el');
  12954. }
  12955. _setCrosshairsCfg() {
  12956. var self = this;
  12957. var chart = self.chart;
  12958. var defaultCfg = mix({}, Global.tooltip);
  12959. var geoms = chart.get('geoms');
  12960. var shapes = [];
  12961. each(geoms, function (geom) {
  12962. var type = geom.get('type');
  12963. if (shapes.indexOf(type) === -1) {
  12964. shapes.push(type);
  12965. }
  12966. });
  12967. var coordType = chart.get('coord').type;
  12968. if (geoms.length && (coordType === 'cartesian' || coordType === 'rect')) {
  12969. if (shapes.length === 1 && ['line', 'area', 'path', 'point'].indexOf(shapes[0]) !== -1) {
  12970. mix(defaultCfg, {
  12971. showCrosshairs: true
  12972. });
  12973. }
  12974. }
  12975. return defaultCfg;
  12976. }
  12977. _getMaxLength() {
  12978. var cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  12979. var {
  12980. layout,
  12981. plotRange
  12982. } = cfg;
  12983. return layout === 'horizontal' ? plotRange.br.x - plotRange.bl.x : plotRange.bl.y - plotRange.tr.y;
  12984. }
  12985. render() {
  12986. var self = this;
  12987. if (self.tooltip) {
  12988. return;
  12989. }
  12990. var chart = self.chart;
  12991. var canvas = chart.get('canvas');
  12992. var frontPlot = chart.get('frontPlot').addGroup({
  12993. className: 'tooltipContainer',
  12994. zIndex: 10
  12995. });
  12996. var backPlot = chart.get('backPlot').addGroup({
  12997. className: 'tooltipContainer'
  12998. });
  12999. var plotRange = chart.get('plotRange');
  13000. var coord = chart.get('coord');
  13001. var defaultCfg = self._setCrosshairsCfg();
  13002. var cfg = self.cfg; // 通过 chart.tooltip() 接口传入的 tooltip 配置项
  13003. var tooltipCfg = deepMix({
  13004. plotRange,
  13005. frontPlot,
  13006. backPlot,
  13007. canvas,
  13008. fixed: coord.transposed || coord.isPolar
  13009. }, defaultCfg, cfg); // 创建 tooltip 实例需要的配置,不应该修改 this.cfg,即用户传入的配置
  13010. tooltipCfg.maxLength = self._getMaxLength(tooltipCfg);
  13011. this._tooltipCfg = tooltipCfg;
  13012. var tooltip = new Tooltip(tooltipCfg);
  13013. self.tooltip = tooltip; // 需要保持tooltip一直显示
  13014. if (tooltipCfg.alwaysShow && self.prePoint) {
  13015. this.showTooltip(self.prePoint);
  13016. }
  13017. self.bindEvents();
  13018. }
  13019. clear() {
  13020. var tooltip = this.tooltip;
  13021. if (tooltip) {
  13022. tooltip.destroy();
  13023. this.unBindEvents();
  13024. }
  13025. this.tooltip = null;
  13026. this._lastActive = null;
  13027. }
  13028. _getTooltipMarkerStyle() {
  13029. var cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  13030. var {
  13031. type,
  13032. items
  13033. } = cfg;
  13034. var tooltipCfg = this._tooltipCfg;
  13035. if (type === 'rect') {
  13036. var x;
  13037. var y;
  13038. var width;
  13039. var height;
  13040. var chart = this.chart;
  13041. var {
  13042. tl,
  13043. br
  13044. } = chart.get('plotRange');
  13045. var coord = chart.get('coord');
  13046. var firstItem = items[0];
  13047. var lastItem = items[items.length - 1];
  13048. var intervalWidth = firstItem.width;
  13049. if (coord.transposed) {
  13050. x = tl.x;
  13051. y = lastItem.y - intervalWidth * 0.75;
  13052. width = br.x - tl.x;
  13053. height = firstItem.y - lastItem.y + 1.5 * intervalWidth;
  13054. } else {
  13055. x = firstItem.x - intervalWidth * 0.75;
  13056. y = tl.y;
  13057. width = lastItem.x - firstItem.x + 1.5 * intervalWidth;
  13058. height = br.y - tl.y;
  13059. }
  13060. cfg.style = mix({
  13061. x,
  13062. y,
  13063. width,
  13064. height,
  13065. fill: '#CCD6EC',
  13066. opacity: 0.3
  13067. }, tooltipCfg.tooltipMarkerStyle);
  13068. } else {
  13069. cfg.style = mix({
  13070. radius: 4,
  13071. fill: '#fff',
  13072. lineWidth: 2
  13073. }, tooltipCfg.tooltipMarkerStyle);
  13074. }
  13075. return cfg;
  13076. }
  13077. _setTooltip(point, items) {
  13078. var tooltipMarkerCfg = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  13079. this.prePoint = point;
  13080. var lastActive = this._lastActive;
  13081. var tooltip = this.tooltip;
  13082. var cfg = this._tooltipCfg;
  13083. items = _uniqItems(items);
  13084. var chart = this.chart;
  13085. var coord = chart.get('coord');
  13086. var yScale = chart.getYScales()[0];
  13087. var snap = cfg.snap;
  13088. if (snap === false && yScale.isLinear) {
  13089. var invertPoint = coord.invertPoint(point);
  13090. var plot = chart.get('plotRange');
  13091. var tip;
  13092. var pos;
  13093. if (isPointInPlot(point, plot)) {
  13094. if (coord.transposed) {
  13095. tip = yScale.invert(invertPoint.x);
  13096. pos = point.x;
  13097. tooltip.setXTipContent(tip);
  13098. tooltip.setXTipPosition(pos);
  13099. tooltip.setYCrosshairPosition(pos);
  13100. } else {
  13101. tip = yScale.invert(invertPoint.y);
  13102. pos = point.y;
  13103. tooltip.setYTipContent(tip);
  13104. tooltip.setYTipPosition(pos);
  13105. tooltip.setXCrosshairPosition(pos);
  13106. }
  13107. }
  13108. }
  13109. if (cfg.onShow) {
  13110. cfg.onShow({
  13111. x: point.x,
  13112. y: point.y,
  13113. tooltip,
  13114. items,
  13115. tooltipMarkerCfg
  13116. });
  13117. }
  13118. if (isEqual$1(lastActive, items)) {
  13119. if (snap === false && (directionEnabled(cfg.crosshairsType, 'y') || cfg.showYTip)) {
  13120. var canvas = this.chart.get('canvas');
  13121. canvas.draw();
  13122. }
  13123. return;
  13124. }
  13125. this._lastActive = items;
  13126. var onChange = cfg.onChange;
  13127. if (onChange) {
  13128. onChange({
  13129. x: point.x,
  13130. y: point.y,
  13131. tooltip,
  13132. items,
  13133. tooltipMarkerCfg
  13134. });
  13135. }
  13136. var first = items[0];
  13137. var title = first.title || first.name;
  13138. var xTipPosX = first.x;
  13139. if (items.length > 1) {
  13140. xTipPosX = (items[0].x + items[items.length - 1].x) / 2;
  13141. }
  13142. tooltip.setContent(title, items, coord.transposed);
  13143. tooltip.setPosition(items, point);
  13144. if (coord.transposed) {
  13145. var yTipPosY = first.y;
  13146. if (items.length > 1) {
  13147. yTipPosY = (items[0].y + items[items.length - 1].y) / 2;
  13148. }
  13149. tooltip.setYTipContent(title);
  13150. tooltip.setYTipPosition(yTipPosY);
  13151. tooltip.setXCrosshairPosition(yTipPosY);
  13152. if (snap) {
  13153. tooltip.setXTipContent(first.value);
  13154. tooltip.setXTipPosition(xTipPosX);
  13155. tooltip.setYCrosshairPosition(xTipPosX);
  13156. }
  13157. } else {
  13158. tooltip.setXTipContent(title);
  13159. tooltip.setXTipPosition(xTipPosX);
  13160. tooltip.setYCrosshairPosition(xTipPosX);
  13161. if (snap) {
  13162. tooltip.setYTipContent(first.value);
  13163. tooltip.setYTipPosition(first.y);
  13164. tooltip.setXCrosshairPosition(first.y);
  13165. }
  13166. }
  13167. var markerItems = tooltipMarkerCfg.items;
  13168. if (cfg.showTooltipMarker && markerItems.length) {
  13169. tooltipMarkerCfg = this._getTooltipMarkerStyle(tooltipMarkerCfg);
  13170. tooltip.setMarkers(tooltipMarkerCfg);
  13171. } else {
  13172. tooltip.clearMarkers();
  13173. }
  13174. tooltip.show();
  13175. }
  13176. showTooltip(point) {
  13177. var self = this;
  13178. var chart = self.chart;
  13179. var tooltipMarkerType;
  13180. var tooltipMarkerItems = [];
  13181. var items = [];
  13182. var cfg = self._tooltipCfg;
  13183. var {
  13184. showItemMarker,
  13185. itemMarkerStyle,
  13186. alwaysShow
  13187. } = cfg;
  13188. var marker;
  13189. if (showItemMarker) {
  13190. marker = itemMarkerStyle;
  13191. }
  13192. var geoms = chart.get('geoms');
  13193. var coord = chart.get('coord');
  13194. each(geoms, function (geom) {
  13195. if (geom.get('visible')) {
  13196. var type = geom.get('type');
  13197. var records = geom.getSnapRecords(point);
  13198. var adjust = geom.get('adjust'); // 漏斗图和金子塔图tooltip位置有问题,暂时不开放显示
  13199. if (type === 'interval' && adjust && adjust.type === 'symmetric') {
  13200. return;
  13201. }
  13202. each(records, function (record) {
  13203. if (record.x && record.y) {
  13204. var {
  13205. x,
  13206. y,
  13207. _origin,
  13208. color
  13209. } = record;
  13210. var tooltipItem = {
  13211. x,
  13212. y: isArray(y) ? y[1] : y,
  13213. color: color || Global.defaultColor,
  13214. origin: _origin,
  13215. name: getTooltipName(geom, _origin),
  13216. value: getTooltipValue(geom, _origin),
  13217. title: getTooltipTitle(geom, _origin)
  13218. };
  13219. if (marker) {
  13220. tooltipItem.marker = mix({
  13221. fill: color || Global.defaultColor
  13222. }, marker);
  13223. }
  13224. items.push(tooltipItem);
  13225. if (['line', 'area', 'path'].indexOf(type) !== -1) {
  13226. tooltipMarkerType = 'circle';
  13227. tooltipMarkerItems.push(tooltipItem);
  13228. } else if (type === 'interval' && (coord.type === 'cartesian' || coord.type === 'rect')) {
  13229. tooltipMarkerType = 'rect';
  13230. tooltipItem.width = geom.getSize(record._origin);
  13231. tooltipMarkerItems.push(tooltipItem);
  13232. }
  13233. }
  13234. });
  13235. }
  13236. });
  13237. if (items.length) {
  13238. var tooltipMarkerCfg = {
  13239. items: tooltipMarkerItems,
  13240. type: tooltipMarkerType
  13241. };
  13242. self._setTooltip(point, items, tooltipMarkerCfg);
  13243. return;
  13244. }
  13245. if (!alwaysShow) {
  13246. self.hideTooltip();
  13247. }
  13248. }
  13249. hideTooltip() {
  13250. var cfg = this._tooltipCfg;
  13251. this._lastActive = null;
  13252. var tooltip = this.tooltip;
  13253. if (tooltip) {
  13254. tooltip.hide();
  13255. if (cfg.onHide) {
  13256. cfg.onHide({
  13257. tooltip
  13258. });
  13259. }
  13260. var canvas = this.chart.get('canvas');
  13261. canvas.draw();
  13262. }
  13263. }
  13264. _handleEvent(methodName, method, action) {
  13265. var canvas = this.canvas;
  13266. each([].concat(methodName), function (aMethod) {
  13267. if (action === 'bind') {
  13268. canvas.on(aMethod, method);
  13269. } else {
  13270. canvas.off(aMethod, method);
  13271. }
  13272. });
  13273. }
  13274. bindEvents() {
  13275. var cfg = this._tooltipCfg;
  13276. var {
  13277. triggerOn,
  13278. triggerOff,
  13279. alwaysShow
  13280. } = cfg;
  13281. triggerOn && this._handleEvent(triggerOn, this.handleShowEvent, 'bind'); // 如果 !alwaysShow, 则在手势离开后就隐藏
  13282. if (!alwaysShow) {
  13283. this._handleEvent(triggerOff, this.handleHideEvent, 'bind');
  13284. }
  13285. }
  13286. unBindEvents() {
  13287. var cfg = this._tooltipCfg;
  13288. var {
  13289. triggerOn,
  13290. triggerOff,
  13291. alwaysShow
  13292. } = cfg;
  13293. triggerOn && this._handleEvent(triggerOn, this.handleShowEvent, 'unBind');
  13294. if (!alwaysShow) {
  13295. this._handleEvent(triggerOff, this.handleHideEvent, 'unBind');
  13296. }
  13297. }
  13298. }
  13299. function init(chart) {
  13300. var tooltipController = new TooltipController({
  13301. chart
  13302. });
  13303. chart.set('tooltipController', tooltipController);
  13304. chart.tooltip = function (enable, cfg) {
  13305. if (isObject(enable)) {
  13306. cfg = enable;
  13307. enable = true;
  13308. }
  13309. tooltipController.enable = enable;
  13310. if (cfg) {
  13311. tooltipController.cfg = cfg;
  13312. }
  13313. return this;
  13314. };
  13315. }
  13316. function afterGeomDraw(chart) {
  13317. var tooltipController = chart.get('tooltipController');
  13318. tooltipController.render();
  13319. chart.showTooltip = function (point) {
  13320. tooltipController.showTooltip(point);
  13321. return this;
  13322. };
  13323. chart.hideTooltip = function () {
  13324. tooltipController.hideTooltip();
  13325. return this;
  13326. };
  13327. }
  13328. function clearInner(chart) {
  13329. var tooltipController = chart.get('tooltipController');
  13330. tooltipController.clear();
  13331. }
  13332. var tooltip = {
  13333. init,
  13334. afterGeomDraw,
  13335. clearInner
  13336. };
  13337. var Tooltip$1 = /*#__PURE__*/Object.freeze({
  13338. __proto__: null,
  13339. init: init,
  13340. afterGeomDraw: afterGeomDraw,
  13341. clearInner: clearInner,
  13342. 'default': tooltip
  13343. });
  13344. Global.guide = deepMix({
  13345. line: {
  13346. style: {
  13347. stroke: '#a3a3a3',
  13348. lineWidth: 1
  13349. },
  13350. top: true
  13351. },
  13352. text: {
  13353. style: {
  13354. fill: '#787878',
  13355. textAlign: 'center',
  13356. textBaseline: 'middle'
  13357. },
  13358. offsetX: 0,
  13359. offsetY: 0,
  13360. top: true
  13361. },
  13362. rect: {
  13363. style: {
  13364. fill: '#fafafa'
  13365. },
  13366. top: false
  13367. },
  13368. arc: {
  13369. style: {
  13370. stroke: '#a3a3a3'
  13371. },
  13372. top: true
  13373. },
  13374. html: {
  13375. offsetX: 0,
  13376. offsetY: 0,
  13377. alignX: 'center',
  13378. alignY: 'middle'
  13379. },
  13380. tag: {
  13381. top: true,
  13382. offsetX: 0,
  13383. offsetY: 0,
  13384. side: 4,
  13385. background: {
  13386. padding: 5,
  13387. radius: 2,
  13388. fill: '#1890FF'
  13389. },
  13390. textStyle: {
  13391. fontSize: 12,
  13392. fill: '#fff',
  13393. textAlign: 'center',
  13394. textBaseline: 'middle'
  13395. }
  13396. },
  13397. point: {
  13398. top: true,
  13399. offsetX: 0,
  13400. offsetY: 0,
  13401. style: {
  13402. fill: '#fff',
  13403. r: 3,
  13404. lineWidth: 2,
  13405. stroke: '#1890ff'
  13406. }
  13407. }
  13408. }, Global.guide || {});
  13409. class GuideController {
  13410. constructor(cfg) {
  13411. this.guides = [];
  13412. this.xScale = null;
  13413. this.yScales = null;
  13414. this.guideShapes = [];
  13415. mix(this, cfg);
  13416. }
  13417. _toString(position) {
  13418. if (isFunction(position)) {
  13419. position = position(this.xScale, this.yScales);
  13420. }
  13421. position = position.toString();
  13422. return position;
  13423. }
  13424. _getId(shape, guide) {
  13425. var id = guide.id;
  13426. if (!id) {
  13427. var type = guide.type;
  13428. if (type === 'arc' || type === 'line' || type === 'rect') {
  13429. id = this._toString(guide.start) + '-' + this._toString(guide.end);
  13430. } else {
  13431. id = this._toString(guide.position);
  13432. }
  13433. }
  13434. return id;
  13435. }
  13436. paint(coord) {
  13437. var self = this;
  13438. var {
  13439. chart,
  13440. guides,
  13441. xScale,
  13442. yScales
  13443. } = self;
  13444. var guideShapes = [];
  13445. each(guides, function (guide, idx) {
  13446. guide.xScale = xScale;
  13447. guide.yScales = yScales;
  13448. var container;
  13449. if (guide.type === 'regionFilter') {
  13450. // TODO: RegionFilter support animation
  13451. guide.chart = chart;
  13452. } else {
  13453. container = guide.top ? self.frontPlot : self.backPlot;
  13454. }
  13455. guide.coord = coord;
  13456. guide.container = container;
  13457. guide.canvas = chart.get('canvas');
  13458. var shape = guide.render(coord, container);
  13459. if (shape) {
  13460. var id = self._getId(shape, guide);
  13461. [].concat(shape).forEach(function (s) {
  13462. s._id = s.get('className') + '-' + id;
  13463. s.set('index', idx);
  13464. guideShapes.push(s);
  13465. });
  13466. }
  13467. });
  13468. self.guideShapes = guideShapes;
  13469. }
  13470. clear() {
  13471. this.reset();
  13472. this.guides = [];
  13473. return this;
  13474. }
  13475. reset() {
  13476. var guides = this.guides;
  13477. each(guides, function (guide) {
  13478. guide.remove();
  13479. });
  13480. }
  13481. _createGuide(type, cfg) {
  13482. var ClassName = upperFirst(type);
  13483. var guide = new GuideBase[ClassName](deepMix({}, Global.guide[type], cfg));
  13484. this.guides.push(guide);
  13485. return guide;
  13486. }
  13487. line() {
  13488. var cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  13489. return this._createGuide('line', cfg);
  13490. }
  13491. text() {
  13492. var cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  13493. return this._createGuide('text', cfg);
  13494. }
  13495. arc() {
  13496. var cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  13497. return this._createGuide('arc', cfg);
  13498. }
  13499. html() {
  13500. var cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  13501. return this._createGuide('html', cfg);
  13502. }
  13503. rect() {
  13504. var cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  13505. return this._createGuide('rect', cfg);
  13506. }
  13507. tag() {
  13508. var cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  13509. return this._createGuide('tag', cfg);
  13510. }
  13511. point() {
  13512. var cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  13513. return this._createGuide('point', cfg);
  13514. }
  13515. regionFilter() {
  13516. var cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  13517. return this._createGuide('regionFilter', cfg);
  13518. }
  13519. }
  13520. function init$1(chart) {
  13521. var guideController = new GuideController({
  13522. frontPlot: chart.get('frontPlot').addGroup({
  13523. zIndex: 20,
  13524. className: 'guideContainer'
  13525. }),
  13526. backPlot: chart.get('backPlot').addGroup({
  13527. className: 'guideContainer'
  13528. })
  13529. });
  13530. chart.set('guideController', guideController);
  13531. /**
  13532. * 为图表添加 guide
  13533. * @return {GuideController} 返回 guide 控制器
  13534. */
  13535. chart.guide = function () {
  13536. return guideController;
  13537. };
  13538. }
  13539. function afterGeomDraw$1(chart) {
  13540. var guideController = chart.get('guideController');
  13541. if (!guideController.guides.length) {
  13542. return;
  13543. }
  13544. var xScale = chart.getXScale();
  13545. var yScales = chart.getYScales();
  13546. var coord = chart.get('coord');
  13547. guideController.xScale = xScale;
  13548. guideController.yScales = yScales;
  13549. guideController.chart = chart; // for regionFilter
  13550. guideController.paint(coord);
  13551. }
  13552. function clear(chart) {
  13553. chart.get('guideController').clear();
  13554. }
  13555. function repaint(chart) {
  13556. chart.get('guideController').reset();
  13557. }
  13558. var guide = {
  13559. init: init$1,
  13560. afterGeomDraw: afterGeomDraw$1,
  13561. clear,
  13562. repaint
  13563. };
  13564. var Guide = /*#__PURE__*/Object.freeze({
  13565. __proto__: null,
  13566. init: init$1,
  13567. afterGeomDraw: afterGeomDraw$1,
  13568. clear: clear,
  13569. repaint: repaint,
  13570. 'default': guide
  13571. });
  13572. function _defineProperty$3(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  13573. var LEGEND_GAP = 12;
  13574. var MARKER_SIZE = 3;
  13575. var DEFAULT_CFG = {
  13576. itemMarginBottom: 12,
  13577. itemGap: 10,
  13578. showTitle: false,
  13579. titleStyle: {
  13580. fontSize: 12,
  13581. fill: '#808080',
  13582. textAlign: 'start',
  13583. textBaseline: 'top'
  13584. },
  13585. nameStyle: {
  13586. fill: '#808080',
  13587. fontSize: 12,
  13588. textAlign: 'start',
  13589. textBaseline: 'middle'
  13590. },
  13591. valueStyle: {
  13592. fill: '#000000',
  13593. fontSize: 12,
  13594. textAlign: 'start',
  13595. textBaseline: 'middle'
  13596. },
  13597. unCheckStyle: {
  13598. fill: '#bfbfbf'
  13599. },
  13600. itemWidth: 'auto',
  13601. wordSpace: 6,
  13602. selectedMode: 'multiple' // 'multiple' or 'single'
  13603. }; // Register the default configuration for Legend
  13604. Global.legend = deepMix({
  13605. common: DEFAULT_CFG,
  13606. // common legend configuration
  13607. right: mix({
  13608. position: 'right',
  13609. layout: 'vertical'
  13610. }, DEFAULT_CFG),
  13611. left: mix({
  13612. position: 'left',
  13613. layout: 'vertical'
  13614. }, DEFAULT_CFG),
  13615. top: mix({
  13616. position: 'top',
  13617. layout: 'horizontal'
  13618. }, DEFAULT_CFG),
  13619. bottom: mix({
  13620. position: 'bottom',
  13621. layout: 'horizontal'
  13622. }, DEFAULT_CFG)
  13623. }, Global.legend || {});
  13624. function getPaddingByPos(pos, appendPadding) {
  13625. var padding = 0;
  13626. appendPadding = parsePadding(appendPadding);
  13627. switch (pos) {
  13628. case 'top':
  13629. padding = appendPadding[0];
  13630. break;
  13631. case 'right':
  13632. padding = appendPadding[1];
  13633. break;
  13634. case 'bottom':
  13635. padding = appendPadding[2];
  13636. break;
  13637. case 'left':
  13638. padding = appendPadding[3];
  13639. break;
  13640. }
  13641. return padding;
  13642. }
  13643. class LegendController {
  13644. constructor(cfg) {
  13645. var _this = this;
  13646. _defineProperty$3(this, "handleEvent", function (ev) {
  13647. var self = _this;
  13648. function findItem(x, y) {
  13649. var result = null;
  13650. var legends = self.legends;
  13651. each(legends, function (legendItems) {
  13652. each(legendItems, function (legend) {
  13653. var {
  13654. itemsGroup,
  13655. legendHitBoxes
  13656. } = legend;
  13657. var children = itemsGroup.get('children');
  13658. if (children.length) {
  13659. var legendPosX = legend.x;
  13660. var legendPosY = legend.y;
  13661. each(legendHitBoxes, function (box, index) {
  13662. if (x >= box.x + legendPosX && x <= box.x + box.width + legendPosX && y >= box.y + legendPosY && y <= box.height + box.y + legendPosY) {
  13663. // inbox
  13664. result = {
  13665. clickedItem: children[index],
  13666. clickedLegend: legend
  13667. };
  13668. return false;
  13669. }
  13670. });
  13671. }
  13672. });
  13673. });
  13674. return result;
  13675. }
  13676. var chart = self.chart;
  13677. var {
  13678. x,
  13679. y
  13680. } = createEvent(ev, chart);
  13681. var clicked = findItem(x, y);
  13682. if (clicked && clicked.clickedLegend.clickable !== false) {
  13683. var {
  13684. clickedItem,
  13685. clickedLegend
  13686. } = clicked;
  13687. if (clickedLegend.onClick) {
  13688. ev.clickedItem = clickedItem;
  13689. clickedLegend.onClick(ev);
  13690. } else if (!clickedLegend.custom) {
  13691. var checked = clickedItem.get('checked');
  13692. var value = clickedItem.get('dataValue');
  13693. var {
  13694. filteredVals,
  13695. field,
  13696. selectedMode
  13697. } = clickedLegend;
  13698. var isSingeSelected = selectedMode === 'single';
  13699. if (isSingeSelected) {
  13700. chart.filter(field, function (val) {
  13701. return val === value;
  13702. });
  13703. } else {
  13704. if (checked) {
  13705. filteredVals.push(value);
  13706. } else {
  13707. remove$1(filteredVals, value);
  13708. }
  13709. chart.filter(field, function (val) {
  13710. return filteredVals.indexOf(val) === -1;
  13711. });
  13712. }
  13713. chart.repaint();
  13714. }
  13715. }
  13716. });
  13717. this.legendCfg = {};
  13718. this.enable = true;
  13719. this.position = 'top';
  13720. mix(this, cfg);
  13721. var _chart = this.chart;
  13722. this.canvasDom = _chart.get('canvas').get('el');
  13723. this.clear();
  13724. }
  13725. addLegend(scale, items, filteredVals) {
  13726. var self = this;
  13727. var legendCfg = self.legendCfg;
  13728. var field = scale.field;
  13729. var fieldCfg = legendCfg[field];
  13730. if (fieldCfg === false) {
  13731. return null;
  13732. }
  13733. if (fieldCfg && fieldCfg.custom) {
  13734. self.addCustomLegend(field);
  13735. } else {
  13736. var position = legendCfg.position || self.position;
  13737. if (fieldCfg && fieldCfg.position) {
  13738. position = fieldCfg.position;
  13739. }
  13740. if (scale.isCategory) {
  13741. self._addCategoryLegend(scale, items, position, filteredVals);
  13742. }
  13743. }
  13744. }
  13745. addCustomLegend(field) {
  13746. var self = this;
  13747. var legendCfg = self.legendCfg;
  13748. if (field && legendCfg[field]) {
  13749. legendCfg = legendCfg[field];
  13750. }
  13751. var position = legendCfg.position || self.position;
  13752. var legends = self.legends;
  13753. legends[position] = legends[position] || [];
  13754. var items = legendCfg.items;
  13755. if (!items) {
  13756. return null;
  13757. }
  13758. var container = self.container;
  13759. each(items, function (item) {
  13760. if (!isPlainObject(item.marker)) {
  13761. item.marker = {
  13762. symbol: item.marker || 'circle',
  13763. fill: item.fill,
  13764. radius: MARKER_SIZE
  13765. };
  13766. } else {
  13767. item.marker.radius = item.marker.radius || MARKER_SIZE;
  13768. }
  13769. item.checked = isNil(item.checked) ? true : item.checked;
  13770. item.name = item.name || item.value;
  13771. });
  13772. var legend = new List(deepMix({}, Global.legend[position], legendCfg, {
  13773. maxLength: self._getMaxLength(position),
  13774. items,
  13775. parent: container
  13776. }));
  13777. legends[position].push(legend);
  13778. }
  13779. clear() {
  13780. var legends = this.legends;
  13781. each(legends, function (legendItems) {
  13782. each(legendItems, function (legend) {
  13783. legend.clear();
  13784. });
  13785. });
  13786. this.legends = {};
  13787. this.unBindEvents();
  13788. }
  13789. _isFiltered(scale, values, value) {
  13790. var rst = false;
  13791. each(values, function (val) {
  13792. rst = rst || scale.getText(val) === scale.getText(value);
  13793. if (rst) {
  13794. return false;
  13795. }
  13796. });
  13797. return rst;
  13798. }
  13799. _getMaxLength(position) {
  13800. var chart = this.chart;
  13801. var appendPadding = parsePadding(chart.get('appendPadding'));
  13802. return position === 'right' || position === 'left' ? chart.get('height') - (appendPadding[0] + appendPadding[2]) : chart.get('width') - (appendPadding[1] + appendPadding[3]);
  13803. }
  13804. _addCategoryLegend(scale, items, position, filteredVals) {
  13805. var self = this;
  13806. var {
  13807. legendCfg,
  13808. legends,
  13809. container,
  13810. chart
  13811. } = self;
  13812. var field = scale.field;
  13813. legends[position] = legends[position] || [];
  13814. var symbol = 'circle';
  13815. if (legendCfg[field] && legendCfg[field].marker) {
  13816. symbol = legendCfg[field].marker;
  13817. } else if (legendCfg.marker) {
  13818. symbol = legendCfg.marker;
  13819. }
  13820. each(items, function (item) {
  13821. if (isPlainObject(symbol)) {
  13822. mix(item.marker, symbol);
  13823. } else {
  13824. item.marker.symbol = symbol;
  13825. }
  13826. if (filteredVals) {
  13827. item.checked = !self._isFiltered(scale, filteredVals, item.dataValue);
  13828. }
  13829. });
  13830. var legendItems = chart.get('legendItems');
  13831. legendItems[field] = items;
  13832. var lastCfg = deepMix({}, Global.legend[position], legendCfg[field] || legendCfg, {
  13833. maxLength: self._getMaxLength(position),
  13834. items,
  13835. field,
  13836. filteredVals,
  13837. parent: container
  13838. });
  13839. if (lastCfg.showTitle) {
  13840. deepMix(lastCfg, {
  13841. title: scale.alias || scale.field
  13842. });
  13843. }
  13844. var legend = new List(lastCfg);
  13845. legends[position].push(legend);
  13846. return legend;
  13847. }
  13848. _alignLegend(legend, pre, position) {
  13849. var self = this;
  13850. var {
  13851. tl,
  13852. bl
  13853. } = self.plotRange;
  13854. var chart = self.chart;
  13855. var offsetX = legend.offsetX || 0;
  13856. var offsetY = legend.offsetY || 0;
  13857. var chartWidth = chart.get('width');
  13858. var chartHeight = chart.get('height');
  13859. var appendPadding = parsePadding(chart.get('appendPadding'));
  13860. var legendHeight = legend.getHeight();
  13861. var legendWidth = legend.getWidth();
  13862. var x = 0;
  13863. var y = 0;
  13864. if (position === 'left' || position === 'right') {
  13865. var verticalAlign = legend.verticalAlign || 'middle';
  13866. var height = Math.abs(tl.y - bl.y);
  13867. x = position === 'left' ? appendPadding[3] : chartWidth - legendWidth - appendPadding[1];
  13868. y = (height - legendHeight) / 2 + tl.y;
  13869. if (verticalAlign === 'top') {
  13870. y = tl.y;
  13871. } else if (verticalAlign === 'bottom') {
  13872. y = bl.y - legendHeight;
  13873. }
  13874. if (pre) {
  13875. y = pre.get('y') - legendHeight - LEGEND_GAP;
  13876. }
  13877. } else {
  13878. var align = legend.align || 'left';
  13879. x = appendPadding[3];
  13880. if (align === 'center') {
  13881. x = chartWidth / 2 - legendWidth / 2;
  13882. } else if (align === 'right') {
  13883. x = chartWidth - (legendWidth + appendPadding[1]);
  13884. }
  13885. y = position === 'top' ? appendPadding[0] + Math.abs(legend.container.getBBox().minY) : chartHeight - legendHeight;
  13886. if (pre) {
  13887. var preWidth = pre.getWidth();
  13888. x = pre.x + preWidth + LEGEND_GAP;
  13889. }
  13890. }
  13891. if (position === 'bottom' && offsetY > 0) {
  13892. offsetY = 0;
  13893. }
  13894. if (position === 'right' && offsetX > 0) {
  13895. offsetX = 0;
  13896. }
  13897. legend.moveTo(x + offsetX, y + offsetY);
  13898. }
  13899. alignLegends() {
  13900. var self = this;
  13901. var legends = self.legends;
  13902. each(legends, function (legendItems, position) {
  13903. each(legendItems, function (legend, index) {
  13904. var pre = legendItems[index - 1];
  13905. self._alignLegend(legend, pre, position);
  13906. });
  13907. });
  13908. return self;
  13909. }
  13910. bindEvents() {
  13911. var legendCfg = this.legendCfg;
  13912. var triggerOn = legendCfg.triggerOn || 'touchstart';
  13913. addEventListener(this.canvasDom, triggerOn, this.handleEvent);
  13914. }
  13915. unBindEvents() {
  13916. var legendCfg = this.legendCfg;
  13917. var triggerOn = legendCfg.triggerOn || 'touchstart';
  13918. removeEventListener(this.canvasDom, triggerOn, this.handleEvent);
  13919. }
  13920. }
  13921. function init$2(chart) {
  13922. var legendController = new LegendController({
  13923. container: chart.get('backPlot'),
  13924. plotRange: chart.get('plotRange'),
  13925. chart
  13926. });
  13927. chart.set('legendController', legendController);
  13928. chart.legend = function (field, cfg) {
  13929. var legendCfg = legendController.legendCfg;
  13930. legendController.enable = true;
  13931. if (isBoolean(field)) {
  13932. legendController.enable = field;
  13933. legendCfg = cfg || {};
  13934. } else if (isObject(field)) {
  13935. legendCfg = field;
  13936. } else {
  13937. legendCfg[field] = cfg;
  13938. }
  13939. legendController.legendCfg = legendCfg;
  13940. return this;
  13941. };
  13942. }
  13943. function beforeGeomDraw(chart) {
  13944. var legendController = chart.get('legendController');
  13945. if (!legendController.enable) return null; // legend is not displayed
  13946. var legendCfg = legendController.legendCfg;
  13947. if (legendCfg && legendCfg.custom) {
  13948. legendController.addCustomLegend();
  13949. } else {
  13950. var legendItems = chart.getLegendItems();
  13951. var scales = chart.get('scales');
  13952. var filters = chart.get('filters');
  13953. each(legendItems, function (items, field) {
  13954. var scale = scales[field];
  13955. var values = scale.values;
  13956. var filteredVals;
  13957. if (filters && filters[field]) {
  13958. filteredVals = values.filter(function (v) {
  13959. return !filters[field](v);
  13960. });
  13961. } else {
  13962. filteredVals = [];
  13963. }
  13964. legendController.addLegend(scale, items, filteredVals);
  13965. });
  13966. }
  13967. if (legendCfg && legendCfg.clickable !== false) {
  13968. legendController.bindEvents();
  13969. }
  13970. var legends = legendController.legends;
  13971. var legendRange = {
  13972. top: 0,
  13973. right: 0,
  13974. bottom: 0,
  13975. left: 0
  13976. };
  13977. each(legends, function (legendItems, position) {
  13978. var padding = 0;
  13979. each(legendItems, function (legend) {
  13980. var width = legend.getWidth();
  13981. var height = legend.getHeight();
  13982. if (position === 'top' || position === 'bottom') {
  13983. padding = Math.max(padding, height);
  13984. if (legend.offsetY > 0) {
  13985. padding += legend.offsetY;
  13986. }
  13987. } else {
  13988. padding = Math.max(padding, width);
  13989. if (legend.offsetX > 0) {
  13990. padding += legend.offsetX;
  13991. }
  13992. }
  13993. });
  13994. legendRange[position] = padding + getPaddingByPos(position, chart.get('appendPadding'));
  13995. });
  13996. chart.set('legendRange', legendRange);
  13997. }
  13998. function afterGeomDraw$2(chart) {
  13999. var legendController = chart.get('legendController');
  14000. legendController.alignLegends();
  14001. }
  14002. function clearInner$1(chart) {
  14003. var legendController = chart.get('legendController');
  14004. legendController.clear();
  14005. chart.set('legendRange', null);
  14006. }
  14007. var legend = {
  14008. init: init$2,
  14009. beforeGeomDraw,
  14010. afterGeomDraw: afterGeomDraw$2,
  14011. clearInner: clearInner$1
  14012. };
  14013. var Legend = /*#__PURE__*/Object.freeze({
  14014. __proto__: null,
  14015. init: init$2,
  14016. beforeGeomDraw: beforeGeomDraw,
  14017. afterGeomDraw: afterGeomDraw$2,
  14018. clearInner: clearInner$1,
  14019. 'default': legend
  14020. });
  14021. var clock = typeof performance === 'object' && performance.now ? performance : Date;
  14022. class Timeline {
  14023. constructor() {
  14024. this.anims = [];
  14025. this.time = null;
  14026. this.playing = false;
  14027. this.canvas = [];
  14028. }
  14029. play() {
  14030. var self = this;
  14031. self.time = clock.now();
  14032. self.playing = true;
  14033. function step() {
  14034. if (self.playing) {
  14035. requestAnimationFrame$1(step);
  14036. self.update();
  14037. }
  14038. }
  14039. requestAnimationFrame$1(step);
  14040. }
  14041. stop() {
  14042. this.playing = false;
  14043. this.time = null;
  14044. this.canvas = [];
  14045. }
  14046. pushAnim(animInfo) {
  14047. this.anims.push(animInfo);
  14048. if (this.playing) {
  14049. return;
  14050. }
  14051. this.play();
  14052. }
  14053. update() {
  14054. var currentTime = clock.now();
  14055. this.canvas = [];
  14056. if (!this.anims.length) {
  14057. this.stop();
  14058. return;
  14059. }
  14060. for (var i = 0; i < this.anims.length; i++) {
  14061. var propertyAnim = this.anims[i];
  14062. if (currentTime < propertyAnim.startTime || propertyAnim.hasEnded) {
  14063. continue;
  14064. }
  14065. var shape = propertyAnim.shape; // shape
  14066. if (shape.get('destroyed')) {
  14067. this.anims.splice(i, 1);
  14068. i--;
  14069. continue;
  14070. }
  14071. var {
  14072. startState,
  14073. endState,
  14074. interpolate,
  14075. duration
  14076. } = propertyAnim;
  14077. if (currentTime >= propertyAnim.startTime && !propertyAnim.hasStarted) {
  14078. propertyAnim.hasStarted = true;
  14079. if (propertyAnim.onStart) {
  14080. propertyAnim.onStart();
  14081. }
  14082. }
  14083. var t = (currentTime - propertyAnim.startTime) / duration;
  14084. t = Math.max(0, Math.min(t, 1));
  14085. t = propertyAnim.easing(t);
  14086. if (propertyAnim.onFrame) {
  14087. propertyAnim.onFrame(t);
  14088. } else {
  14089. for (var key in interpolate) {
  14090. var diff = interpolate[key];
  14091. var value = diff(t);
  14092. var newValue = void 0;
  14093. if (key === 'points') {
  14094. newValue = [];
  14095. var aLen = Math.max(startState.points.length, endState.points.length);
  14096. for (var j = 0; j < aLen; j += 2) {
  14097. newValue.push({
  14098. x: value[j],
  14099. y: value[j + 1]
  14100. });
  14101. }
  14102. } else {
  14103. newValue = value;
  14104. }
  14105. shape._attrs.attrs[key] = newValue;
  14106. shape._attrs.bbox = null; // should clear calculated bbox
  14107. }
  14108. }
  14109. var canvas = shape.get('canvas');
  14110. if (this.canvas.indexOf(canvas) === -1) {
  14111. this.canvas.push(canvas);
  14112. }
  14113. if (propertyAnim.onUpdate) {
  14114. propertyAnim.onUpdate(t);
  14115. }
  14116. if (currentTime >= propertyAnim.endTime && !propertyAnim.hasEnded) {
  14117. propertyAnim.hasEnded = true;
  14118. if (propertyAnim.onEnd) {
  14119. propertyAnim.onEnd();
  14120. }
  14121. }
  14122. if (t === 1) {
  14123. // end
  14124. this.anims.splice(i, 1);
  14125. i--;
  14126. }
  14127. }
  14128. this.canvas.map(function (c) {
  14129. c.draw();
  14130. return c;
  14131. });
  14132. this.time = clock.now();
  14133. }
  14134. }
  14135. function linear$1(k) {
  14136. return k;
  14137. }
  14138. function quadraticIn(k) {
  14139. return k * k;
  14140. }
  14141. function quadraticOut(k) {
  14142. return k * (2 - k);
  14143. }
  14144. function quadraticInOut(k) {
  14145. if ((k *= 2) < 1) {
  14146. return 0.5 * k * k;
  14147. }
  14148. return -0.5 * (--k * (k - 2) - 1);
  14149. }
  14150. function cubicIn(k) {
  14151. return k * k * k;
  14152. }
  14153. function cubicOut(k) {
  14154. return --k * k * k + 1;
  14155. }
  14156. function cubicInOut(k) {
  14157. if ((k *= 2) < 1) {
  14158. return 0.5 * k * k * k;
  14159. }
  14160. return 0.5 * ((k -= 2) * k * k + 2);
  14161. }
  14162. function elasticIn(k) {
  14163. var s;
  14164. var a = 0.1;
  14165. var p = 0.4;
  14166. if (k === 0) return 0;
  14167. if (k === 1) return 1;
  14168. if (!a || a < 1) {
  14169. a = 1;
  14170. s = p / 4;
  14171. } else {
  14172. s = p / (2 * Math.PI) * Math.asin(1 / a);
  14173. }
  14174. return -(a * Math.pow(2, 10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p));
  14175. }
  14176. function elasticOut(k) {
  14177. var s;
  14178. var a = 0.1;
  14179. var p = 0.4;
  14180. if (k === 0) return 0;
  14181. if (k === 1) return 1;
  14182. if (!a || a < 1) {
  14183. a = 1;
  14184. s = p / 4;
  14185. } else {
  14186. s = p / (2 * Math.PI) * Math.asin(1 / a);
  14187. }
  14188. return a * Math.pow(2, -10 * k) * Math.sin((k - s) * (2 * Math.PI) / p) + 1;
  14189. }
  14190. function elasticInOut(k) {
  14191. var s;
  14192. var a = 0.1;
  14193. var p = 0.4;
  14194. if (k === 0) return 0;
  14195. if (k === 1) return 1;
  14196. if (!a || a < 1) {
  14197. a = 1;
  14198. s = p / 4;
  14199. } else {
  14200. s = p / (2 * Math.PI) * Math.asin(1 / a);
  14201. }
  14202. if ((k *= 2) < 1) {
  14203. return -0.5 * (a * Math.pow(2, 10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p));
  14204. }
  14205. return a * Math.pow(2, -10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p) * 0.5 + 1;
  14206. }
  14207. function backIn(k) {
  14208. var s = 1.70158;
  14209. return k * k * ((s + 1) * k - s);
  14210. }
  14211. function backOut(k) {
  14212. var s = 1.70158;
  14213. return (k = k - 1) * k * ((s + 1) * k + s) + 1;
  14214. }
  14215. function backInOut(k) {
  14216. var s = 1.70158 * 1.525;
  14217. if ((k *= 2) < 1) {
  14218. return 0.5 * (k * k * ((s + 1) * k - s));
  14219. }
  14220. return 0.5 * ((k -= 2) * k * ((s + 1) * k + s) + 2);
  14221. }
  14222. function bounceIn(k) {
  14223. return 1 - bounceOut(1 - k);
  14224. }
  14225. function bounceOut(k) {
  14226. if ((k /= 1) < 1 / 2.75) {
  14227. return 7.5625 * k * k;
  14228. } else if (k < 2 / 2.75) {
  14229. return 7.5625 * (k -= 1.5 / 2.75) * k + 0.75;
  14230. } else if (k < 2.5 / 2.75) {
  14231. return 7.5625 * (k -= 2.25 / 2.75) * k + 0.9375;
  14232. }
  14233. return 7.5625 * (k -= 2.625 / 2.75) * k + 0.984375;
  14234. }
  14235. function bounceInOut(k) {
  14236. if (k < 0.5) {
  14237. return bounceIn(k * 2) * 0.5;
  14238. }
  14239. return bounceOut(k * 2 - 1) * 0.5 + 0.5;
  14240. }
  14241. var Easing = /*#__PURE__*/Object.freeze({
  14242. __proto__: null,
  14243. linear: linear$1,
  14244. quadraticIn: quadraticIn,
  14245. quadraticOut: quadraticOut,
  14246. quadraticInOut: quadraticInOut,
  14247. cubicIn: cubicIn,
  14248. cubicOut: cubicOut,
  14249. cubicInOut: cubicInOut,
  14250. elasticIn: elasticIn,
  14251. elasticOut: elasticOut,
  14252. elasticInOut: elasticInOut,
  14253. backIn: backIn,
  14254. backOut: backOut,
  14255. backInOut: backInOut,
  14256. bounceIn: bounceIn,
  14257. bounceOut: bounceOut,
  14258. bounceInOut: bounceInOut
  14259. });
  14260. function plainArray(arr) {
  14261. var result = [];
  14262. for (var i = 0, len = arr.length; i < len; i++) {
  14263. if (arr[i]) {
  14264. result.push(arr[i].x);
  14265. result.push(arr[i].y);
  14266. }
  14267. }
  14268. return result;
  14269. }
  14270. function interpolateNumber(a, b) {
  14271. a = +a;
  14272. b -= a;
  14273. return function (t) {
  14274. return a + b * t;
  14275. };
  14276. }
  14277. function interpolateArray(a, b) {
  14278. var nb = b ? b.length : 0;
  14279. var na = a ? Math.min(nb, a.length) : 0;
  14280. var x = new Array(na);
  14281. var c = new Array(nb);
  14282. var i;
  14283. for (i = 0; i < na; ++i) {
  14284. x[i] = interpolateNumber(a[i], b[i]);
  14285. }
  14286. for (; i < nb; ++i) {
  14287. c[i] = b[i];
  14288. }
  14289. return function (t) {
  14290. for (i = 0; i < na; ++i) {
  14291. c[i] = x[i](t);
  14292. }
  14293. return c;
  14294. };
  14295. }
  14296. class Animator {
  14297. constructor(shape, source, timeline) {
  14298. this.hasStarted = false;
  14299. this.hasEnded = false;
  14300. this.shape = shape;
  14301. this.source = source;
  14302. this.timeline = timeline;
  14303. this.animate = null;
  14304. } // delay, attrs, duration, easing
  14305. to() {
  14306. var cfg = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  14307. var delay = cfg.delay || 0;
  14308. var attrs = cfg.attrs || {};
  14309. var duration = cfg.duration || 1000;
  14310. var easing; // 缓动函数
  14311. if (typeof cfg.easing === 'function') {
  14312. easing = cfg.easing;
  14313. } else {
  14314. easing = Easing[cfg.easing] || linear$1;
  14315. }
  14316. var animInfo = {
  14317. shape: this.shape,
  14318. startTime: this.timeline.time + delay,
  14319. duration,
  14320. easing
  14321. };
  14322. var interpolate = {}; // 差值函数
  14323. for (var attrName in attrs) {
  14324. var startValue = this.source[attrName];
  14325. var endValue = attrs[attrName];
  14326. if (attrName === 'points') {
  14327. startValue = plainArray(startValue);
  14328. endValue = plainArray(endValue);
  14329. interpolate.points = interpolateArray(startValue, endValue);
  14330. this.source.points = startValue;
  14331. attrs.points = endValue;
  14332. } else if (attrName === 'matrix') {
  14333. interpolate.matrix = interpolateArray(startValue, endValue);
  14334. } else {
  14335. interpolate[attrName] = interpolateNumber(startValue, endValue);
  14336. }
  14337. }
  14338. animInfo.interpolate = interpolate;
  14339. animInfo.startState = this.source;
  14340. animInfo.endState = attrs;
  14341. animInfo.endTime = animInfo.startTime + duration;
  14342. this.timeline.pushAnim(animInfo);
  14343. this.animate = animInfo;
  14344. return this;
  14345. }
  14346. onFrame(callback) {
  14347. // 自定义每一帧动画的动作
  14348. if (this.animate) {
  14349. this.animate.onFrame = function (frame) {
  14350. callback(frame);
  14351. };
  14352. }
  14353. return this;
  14354. }
  14355. onStart(callback) {
  14356. if (this.animate) {
  14357. this.animate.onStart = function () {
  14358. callback();
  14359. };
  14360. }
  14361. return this;
  14362. }
  14363. onUpdate(callback) {
  14364. if (this.animate) {
  14365. this.animate.onUpdate = function (frame) {
  14366. callback(frame);
  14367. };
  14368. }
  14369. return this;
  14370. }
  14371. onEnd(callback) {
  14372. if (this.animate) {
  14373. this.animate.onEnd = function () {
  14374. callback();
  14375. };
  14376. }
  14377. return this;
  14378. }
  14379. }
  14380. function ownKeys$1(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  14381. function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys$1(Object(source), true).forEach(function (key) { _defineProperty$4(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys$1(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  14382. function _defineProperty$4(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  14383. var defaultAnimationCfg = {
  14384. appear: {
  14385. duration: 450,
  14386. easing: 'quadraticOut'
  14387. },
  14388. // 'appear' animation options
  14389. update: {
  14390. duration: 300,
  14391. easing: 'quadraticOut'
  14392. },
  14393. // 'update' animation options
  14394. enter: {
  14395. duration: 300,
  14396. easing: 'quadraticOut'
  14397. },
  14398. // 'enter' animation options
  14399. leave: {
  14400. duration: 350,
  14401. easing: 'quadraticIn'
  14402. } // 'leave' animation options
  14403. };
  14404. var Animate = {
  14405. defaultCfg: {},
  14406. Action: {},
  14407. getAnimation(geomType, coord, animationType) {
  14408. var geomAnimateCfg = this.defaultCfg[geomType];
  14409. if (geomAnimateCfg) {
  14410. var animation = geomAnimateCfg[animationType];
  14411. if (isFunction(animation)) {
  14412. return animation(coord);
  14413. }
  14414. }
  14415. return false;
  14416. },
  14417. getAnimateCfg(geomType, animationType) {
  14418. var defaultCfg = defaultAnimationCfg[animationType];
  14419. var geomConfig = this.defaultCfg[geomType];
  14420. if (geomConfig && geomConfig.cfg && geomConfig.cfg[animationType]) {
  14421. return deepMix({}, defaultCfg, geomConfig.cfg[animationType]);
  14422. }
  14423. return defaultCfg;
  14424. },
  14425. registerAnimation(animationName, animationFun) {
  14426. if (!this.Action) {
  14427. this.Action = {};
  14428. }
  14429. this.Action = _objectSpread$1(_objectSpread$1({}, this.Action), {}, {
  14430. [animationName]: animationFun
  14431. });
  14432. }
  14433. };
  14434. /**
  14435. * Utility
  14436. * @author sima.zhang1990@gmail.com
  14437. */
  14438. function getCoordInfo(coord) {
  14439. var start = coord.start;
  14440. var end = coord.end;
  14441. return {
  14442. start,
  14443. end,
  14444. width: end.x - start.x,
  14445. height: Math.abs(end.y - start.y)
  14446. };
  14447. }
  14448. function getScaledMatrix(shape, v, direct) {
  14449. var scaledMatrix;
  14450. shape.apply(v);
  14451. var x = v[0];
  14452. var y = v[1];
  14453. if (direct === 'x') {
  14454. shape.transform([['t', x, y], ['s', 0.01, 1], ['t', -x, -y]]);
  14455. var matrix = shape.getMatrix();
  14456. scaledMatrix = Matrix.transform(matrix, [['t', x, y], ['s', 100, 1], ['t', -x, -y]]);
  14457. } else if (direct === 'y') {
  14458. shape.transform([['t', x, y], ['s', 1, 0.01], ['t', -x, -y]]);
  14459. var _matrix = shape.getMatrix();
  14460. scaledMatrix = Matrix.transform(_matrix, [['t', x, y], ['s', 1, 100], ['t', -x, -y]]);
  14461. } else if (direct === 'xy') {
  14462. shape.transform([['t', x, y], ['s', 0.01, 0.01], ['t', -x, -y]]);
  14463. var _matrix2 = shape.getMatrix();
  14464. scaledMatrix = Matrix.transform(_matrix2, [['t', x, y], ['s', 100, 100], ['t', -x, -y]]);
  14465. }
  14466. return scaledMatrix;
  14467. }
  14468. function getAnimateParam(animateCfg, index, id) {
  14469. var result = {};
  14470. if (animateCfg.delay) {
  14471. result.delay = isFunction(animateCfg.delay) ? animateCfg.delay(index, id) : animateCfg.delay;
  14472. }
  14473. result.easing = animateCfg.easing;
  14474. result.duration = animateCfg.duration;
  14475. result.delay = animateCfg.delay;
  14476. return result;
  14477. }
  14478. function doAnimation(shape, endState, animateCfg, callback) {
  14479. var id = shape._id;
  14480. var index = shape.get('index');
  14481. var {
  14482. easing,
  14483. delay,
  14484. duration
  14485. } = getAnimateParam(animateCfg, index, id);
  14486. var anim = shape.animate().to({
  14487. attrs: endState,
  14488. duration,
  14489. delay,
  14490. easing
  14491. });
  14492. if (callback) {
  14493. anim.onEnd(function () {
  14494. callback();
  14495. });
  14496. }
  14497. }
  14498. /**
  14499. * Animation functions for shape
  14500. * @author sima.zhang1990@gmail.com
  14501. */
  14502. /*
  14503. function waveIn(shape, animateCfg, coord) {
  14504. const clip = Helpers.getClip(coord);
  14505. clip.set('canvas', shape.get('canvas'));
  14506. shape.attr('clip', clip);
  14507. const onEnd = function() {
  14508. shape.attr('clip', null);
  14509. clip.remove(true);
  14510. };
  14511. Helpers.doAnimation(clip, clip.endState, animateCfg, onEnd);
  14512. }
  14513. function scaleInX(shape, animateCfg) {
  14514. const box = shape.getBBox();
  14515. const points = shape.get('origin').points;
  14516. let x;
  14517. const y = (box.minY + box.maxY) / 2;
  14518. if (points[0].y - points[1].y > 0) { // 当顶点在零点之下
  14519. x = box.maxX;
  14520. } else {
  14521. x = box.minX;
  14522. }
  14523. const scaledMatrix = Helpers.getScaledMatrix(shape, [ x, y ], 'x');
  14524. Helpers.doAnimation(shape, { matrix: scaledMatrix }, animateCfg);
  14525. }
  14526. function scaleInY(shape, animateCfg) {
  14527. const box = shape.getBBox();
  14528. const points = shape.get('origin').points;
  14529. const x = (box.minX + box.maxX) / 2;
  14530. let y;
  14531. if (points[0].y - points[1].y <= 0) { // 当顶点在零点之下
  14532. y = box.maxY;
  14533. } else {
  14534. y = box.minY;
  14535. }
  14536. const scaledMatrix = Helpers.getScaledMatrix(shape, [ x, y ], 'x');
  14537. Helpers.doAnimation(shape, { matrix: scaledMatrix }, animateCfg);
  14538. }
  14539. */
  14540. function fadeIn(shape, animateCfg) {
  14541. var fillOpacity = isNil(shape.attr('fillOpacity')) ? 1 : shape.attr('fillOpacity');
  14542. var strokeOpacity = isNil(shape.attr('strokeOpacity')) ? 1 : shape.attr('strokeOpacity');
  14543. shape.attr('fillOpacity', 0);
  14544. shape.attr('strokeOpacity', 0);
  14545. var endState = {
  14546. fillOpacity,
  14547. strokeOpacity
  14548. };
  14549. doAnimation(shape, endState, animateCfg);
  14550. }
  14551. var ShapeAction = /*#__PURE__*/Object.freeze({
  14552. __proto__: null,
  14553. fadeIn: fadeIn
  14554. });
  14555. /**
  14556. * Group animate functions
  14557. * @author sima.zhang1990@gmail.com
  14558. */
  14559. function _groupScaleIn(container, animateCfg, coord, zeroY, type) {
  14560. var {
  14561. start,
  14562. end,
  14563. width,
  14564. height
  14565. } = getCoordInfo(coord);
  14566. var x;
  14567. var y;
  14568. var clip = new Shape$2.Rect({
  14569. attrs: {
  14570. x: start.x,
  14571. y: end.y,
  14572. width,
  14573. height
  14574. }
  14575. });
  14576. if (type === 'y') {
  14577. x = start.x + width / 2;
  14578. y = zeroY.y < start.y ? zeroY.y : start.y;
  14579. } else if (type === 'x') {
  14580. x = zeroY.x > start.x ? zeroY.x : start.x;
  14581. y = start.y + height / 2;
  14582. } else if (type === 'xy') {
  14583. if (coord.isPolar) {
  14584. x = coord.center.x;
  14585. y = coord.center.y;
  14586. } else {
  14587. x = (start.x + end.x) / 2;
  14588. y = (start.y + end.y) / 2;
  14589. }
  14590. }
  14591. var endMatrix = getScaledMatrix(clip, [x, y], type);
  14592. clip.isClip = true;
  14593. clip.endState = {
  14594. matrix: endMatrix
  14595. };
  14596. clip.set('canvas', container.get('canvas'));
  14597. container.attr('clip', clip);
  14598. var onEnd = function onEnd() {
  14599. container.attr('clip', null);
  14600. clip.remove(true);
  14601. };
  14602. doAnimation(clip, clip.endState, animateCfg, onEnd);
  14603. }
  14604. function _shapeScale(container, animateCfg, type) {
  14605. var shapes = container.get('children');
  14606. var x;
  14607. var y;
  14608. var endMatrix;
  14609. for (var i = 0, len = shapes.length; i < len; i++) {
  14610. var shape = shapes[i];
  14611. var box = shape.getBBox();
  14612. x = (box.minX + box.maxX) / 2;
  14613. y = (box.minY + box.maxY) / 2;
  14614. endMatrix = getScaledMatrix(shape, [x, y], type);
  14615. doAnimation(shape, {
  14616. matrix: endMatrix
  14617. }, animateCfg);
  14618. }
  14619. }
  14620. function groupScaleInX(container, animateCfg, coord, zeroY) {
  14621. _groupScaleIn(container, animateCfg, coord, zeroY, 'x');
  14622. }
  14623. function groupScaleInY(container, animateCfg, coord, zeroY) {
  14624. _groupScaleIn(container, animateCfg, coord, zeroY, 'y');
  14625. }
  14626. function groupScaleInXY(container, animateCfg, coord, zeroY) {
  14627. _groupScaleIn(container, animateCfg, coord, zeroY, 'xy');
  14628. }
  14629. function shapesScaleInX(container, animateCfg) {
  14630. _shapeScale(container, animateCfg, 'x');
  14631. }
  14632. function shapesScaleInY(container, animateCfg) {
  14633. _shapeScale(container, animateCfg, 'y');
  14634. }
  14635. function shapesScaleInXY(container, animateCfg) {
  14636. _shapeScale(container, animateCfg, 'xy');
  14637. }
  14638. function groupWaveIn(container, animateCfg, coord) {
  14639. var clip = getClip(coord);
  14640. clip.set('canvas', container.get('canvas'));
  14641. container.attr('clip', clip);
  14642. var onEnd = function onEnd() {
  14643. container.attr('clip', null);
  14644. clip.remove(true);
  14645. };
  14646. var endState = {};
  14647. if (coord.isPolar) {
  14648. var {
  14649. startAngle,
  14650. endAngle
  14651. } = coord;
  14652. endState.endAngle = endAngle;
  14653. clip.attr('endAngle', startAngle);
  14654. } else {
  14655. var {
  14656. start,
  14657. end
  14658. } = coord;
  14659. var width = Math.abs(start.x - end.x);
  14660. var height = Math.abs(start.y - end.y);
  14661. if (coord.isTransposed) {
  14662. clip.attr('height', 0);
  14663. endState.height = height;
  14664. } else {
  14665. clip.attr('width', 0);
  14666. endState.width = width;
  14667. }
  14668. }
  14669. doAnimation(clip, endState, animateCfg, onEnd);
  14670. }
  14671. var GroupAction = /*#__PURE__*/Object.freeze({
  14672. __proto__: null,
  14673. groupWaveIn: groupWaveIn,
  14674. groupScaleInX: groupScaleInX,
  14675. groupScaleInY: groupScaleInY,
  14676. groupScaleInXY: groupScaleInXY,
  14677. shapesScaleInX: shapesScaleInX,
  14678. shapesScaleInY: shapesScaleInY,
  14679. shapesScaleInXY: shapesScaleInXY
  14680. });
  14681. /**
  14682. * Handle the detail animations
  14683. * @author sima.zhang1990@gmail.com
  14684. */
  14685. var timeline;
  14686. Element$1.prototype.animate = function () {
  14687. var attrs = mix({}, this.get('attrs'));
  14688. return new Animator(this, attrs, timeline);
  14689. };
  14690. Chart.prototype.animate = function (cfg) {
  14691. this.set('animate', cfg);
  14692. return this;
  14693. };
  14694. Animate.Action = ShapeAction;
  14695. Animate.defaultCfg = {
  14696. interval: {
  14697. enter(coord) {
  14698. if (coord.isPolar && coord.transposed) {
  14699. // for pie chart
  14700. return function (shape) {
  14701. shape.set('zIndex', -1);
  14702. var container = shape.get('parent');
  14703. container.sort();
  14704. };
  14705. }
  14706. return fadeIn;
  14707. }
  14708. },
  14709. area: {
  14710. enter(coord) {
  14711. if (coord.isPolar) return null;
  14712. return fadeIn;
  14713. }
  14714. },
  14715. line: {
  14716. enter(coord) {
  14717. if (coord.isPolar) return null;
  14718. return fadeIn;
  14719. }
  14720. },
  14721. path: {
  14722. enter(coord) {
  14723. if (coord.isPolar) return null;
  14724. return fadeIn;
  14725. }
  14726. }
  14727. };
  14728. var GROUP_ANIMATION = {
  14729. line(coord) {
  14730. if (coord.isPolar) {
  14731. return groupScaleInXY;
  14732. }
  14733. return groupWaveIn;
  14734. },
  14735. area(coord) {
  14736. if (coord.isPolar) {
  14737. return groupScaleInXY;
  14738. }
  14739. return groupWaveIn;
  14740. },
  14741. path(coord) {
  14742. if (coord.isPolar) {
  14743. return groupScaleInXY;
  14744. }
  14745. return groupWaveIn;
  14746. },
  14747. point() {
  14748. return shapesScaleInXY;
  14749. },
  14750. interval(coord) {
  14751. var result;
  14752. if (coord.isPolar) {
  14753. // polar coodinate
  14754. result = groupScaleInXY;
  14755. if (coord.transposed) {
  14756. // pie chart
  14757. result = groupWaveIn;
  14758. }
  14759. } else {
  14760. result = coord.transposed ? groupScaleInX : groupScaleInY;
  14761. }
  14762. return result;
  14763. },
  14764. schema() {
  14765. return groupWaveIn;
  14766. }
  14767. };
  14768. function diff(fromAttrs, toAttrs) {
  14769. var endState = {};
  14770. for (var k in toAttrs) {
  14771. if (isNumber(fromAttrs[k]) && fromAttrs[k] !== toAttrs[k]) {
  14772. endState[k] = toAttrs[k];
  14773. } else if (isArray(fromAttrs[k]) && JSON.stringify(fromAttrs[k]) !== JSON.stringify(toAttrs[k])) {
  14774. endState[k] = toAttrs[k];
  14775. }
  14776. }
  14777. return endState;
  14778. } // Add a unique id identifier to each shape
  14779. function _getShapeId(geom, dataObj, geomIdx) {
  14780. var type = geom.get('type');
  14781. var id = 'geom' + geomIdx + '-' + type;
  14782. var xScale = geom.getXScale();
  14783. var yScale = geom.getYScale();
  14784. var xField = xScale.field || 'x';
  14785. var yField = yScale.field || 'y';
  14786. var yVal = dataObj[yField];
  14787. var xVal;
  14788. if (xScale.isIdentity) {
  14789. xVal = xScale.value;
  14790. } else {
  14791. xVal = dataObj[xField];
  14792. }
  14793. if (type === 'interval' || type === 'schema') {
  14794. id += '-' + xVal;
  14795. } else if (type === 'line' || type === 'area' || type === 'path') {
  14796. id += '-' + type;
  14797. } else {
  14798. id += xScale.isCategory ? '-' + xVal : '-' + xVal + '-' + yVal;
  14799. }
  14800. var groupScales = geom._getGroupScales();
  14801. each(groupScales, function (groupScale) {
  14802. var field = groupScale.field;
  14803. if (groupScale.type !== 'identity') {
  14804. id += '-' + dataObj[field];
  14805. }
  14806. });
  14807. return id;
  14808. } // get geometry's shapes
  14809. function getShapes(geoms, chart, coord) {
  14810. var shapes = [];
  14811. each(geoms, function (geom, geomIdx) {
  14812. var geomContainer = geom.get('container');
  14813. var geomShapes = geomContainer.get('children');
  14814. var type = geom.get('type');
  14815. var animateCfg = isNil(geom.get('animateCfg')) ? _getAnimateCfgByShapeType(type, chart) : geom.get('animateCfg');
  14816. if (animateCfg !== false) {
  14817. each(geomShapes, function (shape, index) {
  14818. if (shape.get('className') === type) {
  14819. shape._id = _getShapeId(geom, shape.get('origin')._origin, geomIdx);
  14820. shape.set('coord', coord);
  14821. shape.set('animateCfg', animateCfg);
  14822. shape.set('index', index);
  14823. shapes.push(shape);
  14824. }
  14825. });
  14826. }
  14827. geom.set('shapes', geomShapes);
  14828. });
  14829. return shapes;
  14830. }
  14831. function cache(shapes) {
  14832. var rst = {};
  14833. for (var i = 0, len = shapes.length; i < len; i++) {
  14834. var shape = shapes[i];
  14835. if (!shape._id || shape.isClip) continue;
  14836. var id = shape._id;
  14837. rst[id] = {
  14838. _id: id,
  14839. type: shape.get('type'),
  14840. // the type of shape
  14841. attrs: mix({}, shape._attrs.attrs),
  14842. // the graphics attributes of shape
  14843. className: shape.get('className'),
  14844. geomType: shape.get('className'),
  14845. index: shape.get('index'),
  14846. coord: shape.get('coord'),
  14847. animateCfg: shape.get('animateCfg')
  14848. };
  14849. }
  14850. return rst;
  14851. }
  14852. function getAnimate(geomType, coord, animationType, animationName) {
  14853. var result;
  14854. if (isFunction(animationName)) {
  14855. result = animationName;
  14856. } else if (isString(animationName)) {
  14857. result = Animate.Action[animationName];
  14858. } else {
  14859. result = Animate.getAnimation(geomType, coord, animationType);
  14860. }
  14861. return result;
  14862. }
  14863. function getAnimateCfg(geomType, animationType, animateCfg) {
  14864. if (animateCfg === false || isObject(animateCfg) && animateCfg[animationType] === false) {
  14865. return false;
  14866. }
  14867. var defaultCfg = Animate.getAnimateCfg(geomType, animationType);
  14868. if (animateCfg && animateCfg[animationType]) {
  14869. return deepMix({}, defaultCfg, animateCfg[animationType]);
  14870. }
  14871. return defaultCfg;
  14872. }
  14873. function addAnimate(cache, shapes, canvas) {
  14874. var animate;
  14875. var animateCfg; // the order of animation: leave -> update -> enter
  14876. var updateShapes = [];
  14877. var newShapes = [];
  14878. each(shapes, function (shape) {
  14879. var result = cache[shape._id];
  14880. if (!result) {
  14881. newShapes.push(shape);
  14882. } else {
  14883. shape.set('cacheShape', result);
  14884. updateShapes.push(shape);
  14885. delete cache[shape._id];
  14886. }
  14887. }); // first do the leave animation
  14888. each(cache, function (deletedShape) {
  14889. var {
  14890. className,
  14891. coord,
  14892. _id,
  14893. attrs,
  14894. index,
  14895. type
  14896. } = deletedShape;
  14897. animateCfg = getAnimateCfg(className, 'leave', deletedShape.animateCfg);
  14898. if (animateCfg === false) return true;
  14899. animate = getAnimate(className, coord, 'leave', animateCfg.animation);
  14900. if (isFunction(animate)) {
  14901. var tempShape = canvas.addShape(type, {
  14902. attrs,
  14903. index,
  14904. canvas,
  14905. className
  14906. });
  14907. tempShape._id = _id;
  14908. animate(tempShape, animateCfg, coord);
  14909. }
  14910. }); // then do the update animation
  14911. each(updateShapes, function (updateShape) {
  14912. var className = updateShape.get('className');
  14913. animateCfg = getAnimateCfg(className, 'update', updateShape.get('animateCfg'));
  14914. if (animateCfg === false) return true;
  14915. var coord = updateShape.get('coord');
  14916. var cacheAttrs = updateShape.get('cacheShape').attrs;
  14917. var endState = diff(cacheAttrs, updateShape._attrs.attrs); // 判断如果属性相同的话就不进行变换
  14918. if (Object.keys(endState).length) {
  14919. animate = getAnimate(className, coord, 'update', animateCfg.animation);
  14920. if (isFunction(animate)) {
  14921. animate(updateShape, animateCfg, coord);
  14922. } else {
  14923. updateShape.attr(cacheAttrs);
  14924. updateShape.animate().to({
  14925. attrs: endState,
  14926. duration: animateCfg.duration,
  14927. easing: animateCfg.easing,
  14928. delay: animateCfg.delay
  14929. }).onEnd(function () {
  14930. updateShape.set('cacheShape', null);
  14931. });
  14932. }
  14933. }
  14934. }); // last, enter animation
  14935. each(newShapes, function (newShape) {
  14936. // 新图形元素的进场元素
  14937. var className = newShape.get('className');
  14938. var coord = newShape.get('coord');
  14939. animateCfg = getAnimateCfg(className, 'enter', newShape.get('animateCfg'));
  14940. if (animateCfg === false) return true;
  14941. animate = getAnimate(className, coord, 'enter', animateCfg.animation);
  14942. if (isFunction(animate)) {
  14943. if (className === 'interval' && coord.isPolar && coord.transposed) {
  14944. var index = newShape.get('index');
  14945. var lastShape = updateShapes[index - 1];
  14946. animate(newShape, animateCfg, lastShape);
  14947. } else {
  14948. animate(newShape, animateCfg, coord);
  14949. }
  14950. }
  14951. });
  14952. }
  14953. function _getAnimateCfgByShapeType(type, chart) {
  14954. if (!type) {
  14955. return null;
  14956. }
  14957. var animateCfg = chart.get('animate');
  14958. if (type.indexOf('guide-tag') > -1) {
  14959. type = 'guide-tag';
  14960. }
  14961. if (isObject(animateCfg)) {
  14962. return animateCfg[type];
  14963. }
  14964. if (animateCfg === false) {
  14965. return false;
  14966. }
  14967. return null;
  14968. }
  14969. function afterCanvasInit()
  14970. /* chart */
  14971. {
  14972. timeline = new Timeline();
  14973. timeline.play();
  14974. }
  14975. function beforeCanvasDraw(chart) {
  14976. if (chart.get('animate') === false) {
  14977. return;
  14978. }
  14979. var isUpdate = chart.get('isUpdate');
  14980. var canvas = chart.get('canvas');
  14981. var coord = chart.get('coord');
  14982. var geoms = chart.get('geoms');
  14983. var caches = canvas.get('caches') || [];
  14984. if (caches.length === 0) {
  14985. isUpdate = false;
  14986. }
  14987. var cacheShapes = getShapes(geoms, chart, coord);
  14988. var {
  14989. frontPlot,
  14990. backPlot
  14991. } = chart.get('axisController');
  14992. var axisShapes = frontPlot.get('children').concat(backPlot.get('children'));
  14993. var guideShapes = [];
  14994. if (chart.get('guideController')) {
  14995. guideShapes = chart.get('guideController').guideShapes;
  14996. }
  14997. var componentShapes = [];
  14998. axisShapes.concat(guideShapes).forEach(function (s) {
  14999. var className = s.get('className');
  15000. var animateCfg = _getAnimateCfgByShapeType(className, chart);
  15001. s.set('coord', coord);
  15002. s.set('animateCfg', animateCfg);
  15003. componentShapes.push(s);
  15004. cacheShapes.push(s);
  15005. });
  15006. canvas.set('caches', cache(cacheShapes));
  15007. if (isUpdate) {
  15008. addAnimate(caches, cacheShapes, canvas);
  15009. } else {
  15010. // do the appear animation
  15011. var animateCfg;
  15012. var animate;
  15013. each(geoms, function (geom) {
  15014. var type = geom.get('type');
  15015. var geomCfg = isNil(geom.get('animateCfg')) ? _getAnimateCfgByShapeType(type, chart) : geom.get('animateCfg');
  15016. if (geomCfg !== false) {
  15017. animateCfg = getAnimateCfg(type, 'appear', geomCfg);
  15018. animate = getAnimate(type, coord, 'appear', animateCfg.animation);
  15019. if (isFunction(animate)) {
  15020. var shapes = geom.get('shapes');
  15021. each(shapes, function (shape) {
  15022. animate(shape, animateCfg, coord);
  15023. });
  15024. } else if (GROUP_ANIMATION[type]) {
  15025. // do the default animation
  15026. animate = GroupAction[animateCfg.animation] || GROUP_ANIMATION[type](coord);
  15027. var yScale = geom.getYScale();
  15028. var zeroY = coord.convertPoint({
  15029. x: 0,
  15030. y: yScale.scale(geom.getYMinValue())
  15031. });
  15032. var container = geom.get('container');
  15033. animate && animate(container, animateCfg, coord, zeroY);
  15034. }
  15035. }
  15036. }); // do the animation of components
  15037. each(componentShapes, function (shape) {
  15038. var animateCfg = shape.get('animateCfg');
  15039. var className = shape.get('className');
  15040. if (animateCfg && animateCfg.appear) {
  15041. // if user configure
  15042. var defaultCfg = Animate.getAnimateCfg(className, 'appear');
  15043. var appearCfg = deepMix({}, defaultCfg, animateCfg.appear);
  15044. var _animate = getAnimate(className, coord, 'appear', appearCfg.animation);
  15045. if (isFunction(_animate)) {
  15046. _animate(shape, appearCfg, coord);
  15047. }
  15048. }
  15049. });
  15050. }
  15051. }
  15052. function afterCanvasDestroyed()
  15053. /* chart */
  15054. {
  15055. timeline.stop();
  15056. }
  15057. var detail = {
  15058. afterCanvasInit,
  15059. beforeCanvasDraw,
  15060. afterCanvasDestroyed
  15061. };
  15062. var Animation = /*#__PURE__*/Object.freeze({
  15063. __proto__: null,
  15064. afterCanvasInit: afterCanvasInit,
  15065. beforeCanvasDraw: beforeCanvasDraw,
  15066. afterCanvasDestroyed: afterCanvasDestroyed,
  15067. 'default': detail
  15068. });
  15069. Chart._Interactions = {};
  15070. Chart.registerInteraction = function (type, constructor) {
  15071. Chart._Interactions[type] = constructor;
  15072. };
  15073. Chart.getInteraction = function (type) {
  15074. return Chart._Interactions[type];
  15075. };
  15076. Chart.prototype.interaction = function (type, cfg) {
  15077. var interactions = this._interactions || {};
  15078. if (interactions[type]) {
  15079. // if reprated, destroy last
  15080. interactions[type].destroy();
  15081. }
  15082. var Ctor = Chart.getInteraction(type);
  15083. var interact = new Ctor(cfg, this);
  15084. interactions[type] = interact;
  15085. this._interactions = interactions;
  15086. return this;
  15087. };
  15088. Chart.prototype.clearInteraction = function (type) {
  15089. var interactions = this._interactions;
  15090. if (!interactions) return;
  15091. if (type) {
  15092. interactions[type] && interactions[type].destroy();
  15093. delete interactions[type];
  15094. } else {
  15095. each(interactions, function (interaction, key) {
  15096. interaction.destroy();
  15097. delete interactions[key];
  15098. });
  15099. }
  15100. return this;
  15101. };
  15102. function _defineProperty$5(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  15103. function isValuesEqual(values, newValues) {
  15104. if (values.length !== newValues.length) {
  15105. return false;
  15106. }
  15107. var lastIndex = values.length - 1;
  15108. return values[0] === newValues[0] && values[lastIndex] === newValues[lastIndex];
  15109. } // 不同交互之间共享的上下文
  15110. var defaultRange = [0, 1];
  15111. class Context {
  15112. // 最开始的原始值
  15113. // 当前显示的范围
  15114. // 缩放最小的点数
  15115. // 最小的缩放比例, 默认通过minCount计算
  15116. // minScale = 0.01;
  15117. // 交互开始时,ticks个数,主要为了每次缩放后,更新ticks个数
  15118. // lastTickCount;
  15119. constructor(chart) {
  15120. var _this = this;
  15121. _defineProperty$5(this, "chart", null);
  15122. _defineProperty$5(this, "values", null);
  15123. _defineProperty$5(this, "range", defaultRange);
  15124. _defineProperty$5(this, "startRange", defaultRange);
  15125. _defineProperty$5(this, "minCount", 10);
  15126. _defineProperty$5(this, "_afterinit", function () {
  15127. // 初始化value值
  15128. var scale = _this.getPinchScale(); // 记录原始全量数据
  15129. var values = [].concat(scale.values);
  15130. _this.values = values; // 最小的缩放比例
  15131. if (!_this.minScale) {
  15132. _this.minScale = _this.minCount / values.length;
  15133. } // 初始化的时候有设置range,则初始化成默认比例
  15134. if (_this.range !== defaultRange) {
  15135. _this.updateRange(_this.range);
  15136. _this.updateTicks();
  15137. }
  15138. });
  15139. _defineProperty$5(this, "_afterdatachange", function () {
  15140. _this.updateRange(_this.range);
  15141. });
  15142. this.chart = chart;
  15143. this._initEvent(chart);
  15144. }
  15145. _initEvent(chart) {
  15146. // 在整体初始化后还需要设置一些初始状态
  15147. chart.on(EVENT_AFTER_INIT, this._afterinit);
  15148. chart.on(EVENT_AFTER_DATA_CHANGE, this._afterdatachange);
  15149. } // 缩放的主轴scale
  15150. getPinchScale() {
  15151. var {
  15152. chart
  15153. } = this; // 默认缩放x轴
  15154. var scale = chart.getXScale();
  15155. return scale;
  15156. } // 跟随轴的scale
  15157. getFollowScale() {
  15158. var {
  15159. chart
  15160. } = this; // 默认缩放x轴
  15161. var scales = chart.getYScales() || [];
  15162. return scales[0];
  15163. }
  15164. start() {
  15165. var {
  15166. range
  15167. } = this;
  15168. var scale = this.getPinchScale();
  15169. var [start, end] = range; // 记录交互起始的范围
  15170. this.startRange = [start, end]; // 记录开始时的ticks个数
  15171. this.lastTickCount = scale.tickCount;
  15172. }
  15173. doZoom(leftScale, rightScale, zoom) {
  15174. var {
  15175. startRange: range,
  15176. minScale
  15177. } = this;
  15178. var [start, end] = range;
  15179. var zoomOffset = 1 - zoom;
  15180. var rangeLen = end - start;
  15181. var rangeOffset = rangeLen * zoomOffset;
  15182. var leftOffset = rangeOffset * leftScale;
  15183. var rightOffset = rangeOffset * rightScale;
  15184. var newStart = Math.max(0, start - leftOffset);
  15185. var newEnd = Math.min(1, end + rightOffset);
  15186. var newRange = [newStart, newEnd]; // 如果已经到了最小比例,则不能再继续再放大
  15187. if (newEnd - newStart < minScale) {
  15188. return;
  15189. }
  15190. this.updateRange(newRange);
  15191. }
  15192. doMove(ratio) {
  15193. // 不管是0, 还是其他,都不用处理
  15194. if (!ratio) return;
  15195. var {
  15196. startRange: range
  15197. } = this;
  15198. var [start, end] = range;
  15199. var rangeLen = end - start;
  15200. var rangeOffset = rangeLen * ratio;
  15201. var newStart = start - rangeOffset;
  15202. var newEnd = end - rangeOffset; // 处理边界值
  15203. var newRange;
  15204. if (newStart < 0) {
  15205. newRange = [0, rangeLen];
  15206. } else if (newEnd > 1) {
  15207. newRange = [1 - rangeLen, 1];
  15208. } else {
  15209. newRange = [newStart, newEnd];
  15210. }
  15211. this.updateRange(newRange);
  15212. }
  15213. updateRange(range) {
  15214. var {
  15215. values
  15216. } = this; // 0, 1 的范围之间
  15217. var [start, end] = range; // start 不能小于0
  15218. start = Math.max(0, start); // end 不能大于1
  15219. end = Math.min(1, end); // 设置当前的范围
  15220. this.range = [start, end];
  15221. var len = values.length;
  15222. var valueStart = start * len;
  15223. var valueEnd = end * len; // 从原始数据里截取需要显示的数据
  15224. var newValues = values.slice(valueStart, valueEnd);
  15225. this.repaint(newValues);
  15226. }
  15227. repaint(newValues) {
  15228. var {
  15229. chart
  15230. } = this;
  15231. var scale = this.getPinchScale();
  15232. var {
  15233. values: currentValues,
  15234. ticks
  15235. } = scale; // 如果新数组和当前显示的数组相同,则不更新
  15236. if (isValuesEqual(currentValues, newValues)) {
  15237. return;
  15238. } // 更新主轴values
  15239. this.updateScale(scale, {
  15240. ticks,
  15241. values: newValues
  15242. });
  15243. this.updateFollowScale(scale, newValues);
  15244. chart.repaint();
  15245. }
  15246. updateFollowScale(pinchScale, pinchValues) {
  15247. var {
  15248. chart
  15249. } = this;
  15250. var followScale = this.getFollowScale();
  15251. var {
  15252. field: pinchField,
  15253. type: pinchScaleType
  15254. } = pinchScale;
  15255. var {
  15256. field: followField
  15257. } = followScale; // 根据主轴的value值,找到所有从轴的value值
  15258. var values = []; // 转成map,让查找性能更高
  15259. var pinchValueMap = {};
  15260. pinchValues.forEach(function (item) {
  15261. pinchValueMap[item] = true;
  15262. });
  15263. var data = chart.get('data');
  15264. data.forEach(function (item) {
  15265. if (pinchScaleType === 'timeCat') {
  15266. var value = toTimeStamp(item[pinchField]);
  15267. if (pinchValueMap[value]) {
  15268. values.push(item[followField]);
  15269. }
  15270. }
  15271. });
  15272. var {
  15273. min,
  15274. max
  15275. } = getRange$1(values);
  15276. this.updateScale(followScale, {
  15277. min,
  15278. max,
  15279. nice: true
  15280. });
  15281. }
  15282. updateScale(scale, cfg) {
  15283. if (!scale) {
  15284. return;
  15285. }
  15286. scale.change(cfg);
  15287. } // 上一次的tick个数
  15288. updateTicks() {
  15289. var {
  15290. chart,
  15291. values
  15292. } = this;
  15293. var scale = this.getPinchScale();
  15294. var {
  15295. values: currentValues,
  15296. tickCount
  15297. } = scale; // 根据当前数据的比例,和定义的tickCount计算应该需要多少个ticks
  15298. var newTickCount = Math.round(tickCount * values.length / currentValues.length);
  15299. var catTicks = getTickMethod('cat');
  15300. var ticks = catTicks({
  15301. tickCount: newTickCount,
  15302. values
  15303. });
  15304. this.updateScale(scale, {
  15305. ticks,
  15306. values: currentValues
  15307. }); // 更新完后,需要重新绘制一次
  15308. chart.repaint();
  15309. }
  15310. destroy() {
  15311. var {
  15312. chart
  15313. } = this;
  15314. chart.off(EVENT_AFTER_INIT, this._afterinit);
  15315. chart.off(EVENT_AFTER_DATA_CHANGE, this._afterdatachange);
  15316. }
  15317. }
  15318. function _defineProperty$6(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  15319. class Base$2 {
  15320. // 交互的上下文
  15321. getDefaultCfg() {
  15322. return {};
  15323. }
  15324. getInteractionContext(chart) {
  15325. var interactionContext = chart.get('interactionContext');
  15326. if (interactionContext) {
  15327. return interactionContext;
  15328. }
  15329. interactionContext = new Context(chart);
  15330. chart.set('interactionContext', interactionContext);
  15331. return interactionContext;
  15332. }
  15333. constructor(cfg, chart) {
  15334. var _this = this;
  15335. _defineProperty$6(this, "type", '');
  15336. _defineProperty$6(this, "startEvent", 'touchstart');
  15337. _defineProperty$6(this, "processEvent", 'touchmove');
  15338. _defineProperty$6(this, "endEvent", 'touchend');
  15339. _defineProperty$6(this, "resetEvent", null);
  15340. _defineProperty$6(this, "context", null);
  15341. _defineProperty$6(this, "_start", function (ev) {
  15342. _this.preStart && _this.preStart(ev);
  15343. _this.start(ev);
  15344. _this.onStart && _this.onStart(ev);
  15345. });
  15346. _defineProperty$6(this, "_process", function (ev) {
  15347. _this.preProcess && _this.preProcess(ev);
  15348. _this.process(ev);
  15349. _this.onProcess && _this.onProcess(ev);
  15350. });
  15351. _defineProperty$6(this, "_end", function (ev) {
  15352. _this.preEnd && _this.preEnd(ev);
  15353. _this.end(ev);
  15354. _this.onEnd && _this.onEnd(ev);
  15355. });
  15356. _defineProperty$6(this, "_reset", function (ev) {
  15357. _this.preReset && _this.preReset(ev);
  15358. _this.reset(ev);
  15359. _this.onReset && _this.onReset(ev);
  15360. });
  15361. mix(this, this.getDefaultCfg(), cfg);
  15362. this.context = this.getInteractionContext(chart);
  15363. this.chart = chart; // 只处理range, 暂时先这么处理后面再看情况调整
  15364. var {
  15365. range
  15366. } = this;
  15367. if (range) {
  15368. this.context.range = range;
  15369. }
  15370. this._bindEvents(chart);
  15371. }
  15372. _bindEvents(chart) {
  15373. var {
  15374. startEvent,
  15375. processEvent,
  15376. endEvent,
  15377. resetEvent
  15378. } = this;
  15379. var canvas = chart.get('canvas'); // 统一绑定事件
  15380. canvas.on(startEvent, this._start);
  15381. canvas.on(processEvent, this._process);
  15382. canvas.on(endEvent, this._end);
  15383. canvas.on(resetEvent, this._reset);
  15384. }
  15385. _clearEvents() {
  15386. var {
  15387. chart,
  15388. startEvent,
  15389. processEvent,
  15390. endEvent,
  15391. resetEvent
  15392. } = this;
  15393. var canvas = chart.get('canvas'); // 统一绑定事件
  15394. canvas.off(startEvent, this._start);
  15395. canvas.off(processEvent, this._process);
  15396. canvas.off(endEvent, this._end);
  15397. canvas.off(resetEvent, this._start);
  15398. }
  15399. // override
  15400. start() {} // override
  15401. process() {} // override
  15402. end() {} // override
  15403. reset() {}
  15404. destroy() {
  15405. this.context.destroy();
  15406. this._clearEvents();
  15407. }
  15408. }
  15409. class Pan extends Base$2 {
  15410. getDefaultCfg() {
  15411. return {
  15412. type: 'pan',
  15413. startEvent: 'panstart',
  15414. processEvent: 'pan',
  15415. endEvent: 'panend'
  15416. };
  15417. }
  15418. start() {
  15419. var {
  15420. context
  15421. } = this;
  15422. context.start();
  15423. }
  15424. process(e) {
  15425. var {
  15426. direction,
  15427. deltaX
  15428. } = e;
  15429. if (direction === 'up' || direction === 'down') {
  15430. return;
  15431. }
  15432. e.preventDefault && e.preventDefault();
  15433. var {
  15434. context
  15435. } = this;
  15436. var chart = context.chart;
  15437. var coord = chart.get('coord');
  15438. var {
  15439. start,
  15440. end
  15441. } = coord;
  15442. var coordWidth = end.x - start.x;
  15443. var ratio = deltaX / coordWidth;
  15444. context.doMove(ratio);
  15445. }
  15446. }
  15447. class Pinch extends Base$2 {
  15448. getDefaultCfg() {
  15449. return {
  15450. type: 'pinch',
  15451. startEvent: 'pinchstart',
  15452. processEvent: 'pinch',
  15453. endEvent: 'pinchend'
  15454. };
  15455. }
  15456. constructor(cfg, chart) {
  15457. super(cfg, chart);
  15458. var {
  15459. context
  15460. } = this;
  15461. mix(context, cfg);
  15462. }
  15463. start() {
  15464. var {
  15465. context
  15466. } = this;
  15467. context.start();
  15468. }
  15469. process(e) {
  15470. e.preventDefault && e.preventDefault();
  15471. var {
  15472. zoom,
  15473. center
  15474. } = e;
  15475. var {
  15476. context
  15477. } = this;
  15478. var {
  15479. chart
  15480. } = context;
  15481. var coord = chart.get('coord');
  15482. var {
  15483. start,
  15484. end
  15485. } = coord;
  15486. var coordWidth = end.x - start.x;
  15487. var leftLen = Math.abs(center.x - start.x);
  15488. var rightLen = Math.abs(end.x - center.x); // 计算左右缩放的比例
  15489. var leftScale = leftLen / coordWidth;
  15490. var rightScale = rightLen / coordWidth;
  15491. context.doZoom(leftScale, rightScale, zoom);
  15492. }
  15493. end() {
  15494. // 缩放完成后再更新ticks
  15495. var {
  15496. context
  15497. } = this;
  15498. context.updateTicks();
  15499. }
  15500. }
  15501. Chart.registerInteraction('pan', Pan);
  15502. Chart.registerInteraction('pinch', Pinch);
  15503. /**
  15504. * Default, without interactins
  15505. */
  15506. var Component$1 = {
  15507. Marker
  15508. }; // register plugins
  15509. Chart.plugins.register([Tooltip$1, Legend, Guide, Animation]); // 默认添加交互
  15510. var F2 = {
  15511. Component: Component$1,
  15512. Global,
  15513. Chart,
  15514. Shape: Shape$1,
  15515. G,
  15516. Util,
  15517. Helper,
  15518. track,
  15519. Animate
  15520. };
  15521. var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  15522. function unwrapExports (x) {
  15523. return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
  15524. }
  15525. function createCommonjsModule(fn, module) {
  15526. return module = { exports: {} }, fn(module, module.exports), module.exports;
  15527. }
  15528. var array$1 = createCommonjsModule(function (module, exports) {
  15529. exports.__esModule = true;
  15530. exports.merge = merge;
  15531. exports.values = values;
  15532. exports.firstValue = firstValue;
  15533. exports.group = group;
  15534. exports.groupToMap = groupToMap;
  15535. exports.remove = remove;
  15536. exports.getRange = getRange;
  15537. function merge(dataArray) {
  15538. var rst = [];
  15539. for (var i = 0, len = dataArray.length; i < len; i++) {
  15540. rst = rst.concat(dataArray[i]);
  15541. }
  15542. return rst;
  15543. }
  15544. function values(data, name) {
  15545. var rst = [];
  15546. var tmpMap = {};
  15547. for (var i = 0, len = data.length; i < len; i++) {
  15548. var obj = data[i];
  15549. var value = obj[name];
  15550. if (!(0, esm.isNil)(value)) {
  15551. if (!(0, esm.isArray)(value)) {
  15552. if (!tmpMap[value]) {
  15553. rst.push(value);
  15554. tmpMap[value] = true;
  15555. }
  15556. } else {
  15557. (0, esm.each)(value, function (val) {
  15558. if (!tmpMap[val]) {
  15559. rst.push(val);
  15560. tmpMap[val] = true;
  15561. }
  15562. });
  15563. }
  15564. }
  15565. }
  15566. return rst;
  15567. }
  15568. function firstValue(data, name) {
  15569. var rst = null;
  15570. for (var i = 0, len = data.length; i < len; i++) {
  15571. var obj = data[i];
  15572. var value = obj[name];
  15573. if (!(0, esm.isNil)(value)) {
  15574. if ((0, esm.isArray)(value)) {
  15575. rst = value[0];
  15576. } else {
  15577. rst = value;
  15578. }
  15579. break;
  15580. }
  15581. }
  15582. return rst;
  15583. }
  15584. function groupToMap(data, fields) {
  15585. if (!fields) {
  15586. return {
  15587. 0: data
  15588. };
  15589. }
  15590. var callback = function callback(row) {
  15591. var unique = '_';
  15592. for (var i = 0, l = fields.length; i < l; i++) {
  15593. unique += row[fields[i]] && row[fields[i]].toString();
  15594. }
  15595. return unique;
  15596. };
  15597. var groups = {};
  15598. for (var i = 0, len = data.length; i < len; i++) {
  15599. var row = data[i];
  15600. var key = callback(row);
  15601. if (groups[key]) {
  15602. groups[key].push(row);
  15603. } else {
  15604. groups[key] = [row];
  15605. }
  15606. }
  15607. return groups;
  15608. }
  15609. function group(data, fields, appendConditions) {
  15610. if (appendConditions === void 0) {
  15611. appendConditions = {};
  15612. }
  15613. if (!fields) {
  15614. return [data];
  15615. }
  15616. var groups = groupToMap(data, fields);
  15617. var array = [];
  15618. if (fields.length === 1 && appendConditions[fields[0]]) {
  15619. var _values = appendConditions[fields[0]];
  15620. (0, esm.each)(_values, function (value) {
  15621. value = '_' + value;
  15622. array.push(groups[value]);
  15623. });
  15624. } else {
  15625. for (var i in groups) {
  15626. array.push(groups[i]);
  15627. }
  15628. }
  15629. return array;
  15630. }
  15631. function remove(arr, obj) {
  15632. if (!arr) {
  15633. return;
  15634. }
  15635. var index = arr.indexOf(obj);
  15636. if (index !== -1) {
  15637. arr.splice(index, 1);
  15638. }
  15639. }
  15640. function getRange(values) {
  15641. if (!values.length) {
  15642. return {
  15643. min: 0,
  15644. max: 0
  15645. };
  15646. }
  15647. var max = Math.max.apply(null, values);
  15648. var min = Math.min.apply(null, values);
  15649. return {
  15650. min: min,
  15651. max: max
  15652. };
  15653. }
  15654. });
  15655. unwrapExports(array$1);
  15656. var array_1 = array$1.merge;
  15657. var array_2 = array$1.values;
  15658. var array_3 = array$1.firstValue;
  15659. var array_4 = array$1.group;
  15660. var array_5 = array$1.groupToMap;
  15661. var array_6 = array$1.remove;
  15662. var array_7 = array$1.getRange;
  15663. var dom = createCommonjsModule(function (module, exports) {
  15664. exports.__esModule = true;
  15665. exports.isCanvasElement = isCanvasElement;
  15666. exports.getPixelRatio = getPixelRatio;
  15667. exports.getStyle = getStyle;
  15668. exports.getWidth = getWidth;
  15669. exports.getHeight = getHeight;
  15670. exports.getDomById = getDomById;
  15671. exports.getRelativePosition = getRelativePosition;
  15672. exports.addEventListener = addEventListener;
  15673. exports.removeEventListener = removeEventListener;
  15674. exports.createEvent = createEvent;
  15675. exports.measureText = measureText;
  15676. exports.isBrowser = exports.isNode = exports.isMy = exports.isWx = void 0;
  15677. /**
  15678. * Detects support for options object argument in addEventListener.
  15679. * https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support
  15680. * @private
  15681. */
  15682. var supportsEventListenerOptions = function () {
  15683. var supports = false;
  15684. try {
  15685. var options = Object.defineProperty({}, 'passive', {
  15686. get: function get() {
  15687. supports = true;
  15688. }
  15689. });
  15690. window.addEventListener('e', null, options);
  15691. } catch (e) {// continue regardless of error
  15692. }
  15693. return supports;
  15694. }(); // Default passive to true as expected by Chrome for 'touchstart' and 'touchend' events.
  15695. // https://github.com/chartjs/Chart.js/issues/4287
  15696. var eventListenerOptions = supportsEventListenerOptions ? {
  15697. passive: true
  15698. } : false;
  15699. /* global wx, my */
  15700. // weixin miniprogram
  15701. var isWx = typeof wx === 'object' && typeof wx.getSystemInfoSync === 'function'; // ant miniprogram
  15702. exports.isWx = isWx;
  15703. var isMy = typeof my === 'object' && typeof my.getSystemInfoSync === 'function'; // in node
  15704. exports.isMy = isMy;
  15705. var isNode = typeof commonjsGlobal && !typeof window; // in browser
  15706. exports.isNode = isNode;
  15707. var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.sessionStorage !== 'undefined';
  15708. exports.isBrowser = isBrowser;
  15709. function isCanvasElement(el) {
  15710. if (!el || typeof el !== 'object') return false;
  15711. if (el.nodeType === 1 && el.nodeName) {
  15712. // HTMLCanvasElement
  15713. return true;
  15714. } // CanvasElement
  15715. return !!el.isCanvasElement;
  15716. }
  15717. function getPixelRatio() {
  15718. return window && window.devicePixelRatio || 1;
  15719. }
  15720. function getStyle(el, property) {
  15721. return el.currentStyle ? el.currentStyle[property] : document.defaultView.getComputedStyle(el, null).getPropertyValue(property);
  15722. }
  15723. function getWidth(el) {
  15724. var width = getStyle(el, 'width');
  15725. if (width === 'auto') {
  15726. width = el.offsetWidth;
  15727. }
  15728. return parseFloat(width);
  15729. }
  15730. function getHeight(el) {
  15731. var height = getStyle(el, 'height');
  15732. if (height === 'auto') {
  15733. height = el.offsetHeight;
  15734. }
  15735. return parseFloat(height);
  15736. }
  15737. function getDomById(id) {
  15738. if (!id) {
  15739. return null;
  15740. }
  15741. return document.getElementById(id);
  15742. }
  15743. function getRelativePosition(point, canvas) {
  15744. var canvasDom = canvas.get('el');
  15745. if (!canvasDom) return point;
  15746. var _canvasDom$getBoundin = canvasDom.getBoundingClientRect(),
  15747. top = _canvasDom$getBoundin.top,
  15748. right = _canvasDom$getBoundin.right,
  15749. bottom = _canvasDom$getBoundin.bottom,
  15750. left = _canvasDom$getBoundin.left;
  15751. var paddingLeft = parseFloat(getStyle(canvasDom, 'padding-left'));
  15752. var paddingTop = parseFloat(getStyle(canvasDom, 'padding-top'));
  15753. var paddingRight = parseFloat(getStyle(canvasDom, 'padding-right'));
  15754. var paddingBottom = parseFloat(getStyle(canvasDom, 'padding-bottom'));
  15755. var width = right - left - paddingLeft - paddingRight;
  15756. var height = bottom - top - paddingTop - paddingBottom;
  15757. var pixelRatio = canvas.get('pixelRatio');
  15758. var mouseX = (point.x - left - paddingLeft) / width * canvasDom.width / pixelRatio;
  15759. var mouseY = (point.y - top - paddingTop) / height * canvasDom.height / pixelRatio;
  15760. return {
  15761. x: mouseX,
  15762. y: mouseY
  15763. };
  15764. }
  15765. function addEventListener(source, type, listener) {
  15766. source.addEventListener(type, listener, eventListenerOptions);
  15767. }
  15768. function removeEventListener(source, type, listener) {
  15769. source.removeEventListener(type, listener, eventListenerOptions);
  15770. }
  15771. function createEventObj(type, chart, x, y, nativeEvent) {
  15772. return {
  15773. type: type,
  15774. chart: chart,
  15775. "native": nativeEvent || null,
  15776. x: x !== undefined ? x : null,
  15777. y: y !== undefined ? y : null
  15778. };
  15779. }
  15780. function createEvent(event, chart) {
  15781. var type = event.type;
  15782. var clientPoint; // 说明是touch相关事件
  15783. if (event.touches) {
  15784. // https://developer.mozilla.org/zh-CN/docs/Web/API/TouchEvent/changedTouches
  15785. // 这里直接拿changedTouches就可以了,不管是touchstart, touchmove, touchend changedTouches 都是有的
  15786. // 为了以防万一,做个空判断
  15787. var touch = event.changedTouches[0] || {}; // x, y: 相对canvas原点的位置,clientX, clientY 相对于可视窗口的位置
  15788. var x = touch.x,
  15789. y = touch.y,
  15790. clientX = touch.clientX,
  15791. clientY = touch.clientY; // 小程序环境会有x,y,这里就直接返回
  15792. if (x && y) {
  15793. return createEventObj(type, chart, x, y, event);
  15794. }
  15795. clientPoint = {
  15796. x: clientX,
  15797. y: clientY
  15798. };
  15799. } else {
  15800. // mouse相关事件
  15801. clientPoint = {
  15802. x: event.clientX,
  15803. y: event.clientY
  15804. };
  15805. } // 理论上应该是只有有在浏览器环境才会走到这里
  15806. var canvas = chart.get('canvas'); // 通过clientX, clientY 计算x, y
  15807. var point = getRelativePosition(clientPoint, canvas);
  15808. return createEventObj(type, chart, point.x, point.y, event);
  15809. }
  15810. function measureText(text, font, ctx) {
  15811. if (!ctx) {
  15812. ctx = document.createElement('canvas').getContext('2d');
  15813. }
  15814. ctx.font = font || '12px sans-serif';
  15815. return ctx.measureText(text);
  15816. }
  15817. });
  15818. unwrapExports(dom);
  15819. var dom_1 = dom.isCanvasElement;
  15820. var dom_2 = dom.getPixelRatio;
  15821. var dom_3 = dom.getStyle;
  15822. var dom_4 = dom.getWidth;
  15823. var dom_5 = dom.getHeight;
  15824. var dom_6 = dom.getDomById;
  15825. var dom_7 = dom.getRelativePosition;
  15826. var dom_8 = dom.addEventListener;
  15827. var dom_9 = dom.removeEventListener;
  15828. var dom_10 = dom.createEvent;
  15829. var dom_11 = dom.measureText;
  15830. var dom_12 = dom.isBrowser;
  15831. var dom_13 = dom.isNode;
  15832. var dom_14 = dom.isMy;
  15833. var dom_15 = dom.isWx;
  15834. var common = createCommonjsModule(function (module, exports) {
  15835. exports.__esModule = true;
  15836. var _exportNames = {
  15837. isObjectValueEqual: true,
  15838. parsePadding: true,
  15839. directionEnabled: true,
  15840. toTimeStamp: true,
  15841. upperFirst: true,
  15842. lowerFirst: true,
  15843. isString: true,
  15844. isNumber: true,
  15845. isBoolean: true,
  15846. isFunction: true,
  15847. isDate: true,
  15848. isArray: true,
  15849. isNil: true,
  15850. isObject: true,
  15851. isPlainObject: true,
  15852. isEqual: true,
  15853. deepMix: true,
  15854. mix: true,
  15855. each: true,
  15856. uniq: true,
  15857. find: true,
  15858. Array: true
  15859. };
  15860. exports.isObjectValueEqual = isObjectValueEqual;
  15861. exports.parsePadding = parsePadding;
  15862. exports.directionEnabled = directionEnabled;
  15863. exports.toTimeStamp = toTimeStamp;
  15864. exports.Array = void 0;
  15865. exports.upperFirst = esm.upperFirst;
  15866. exports.lowerFirst = esm.lowerFirst;
  15867. exports.isString = esm.isString;
  15868. exports.isNumber = esm.isNumber;
  15869. exports.isBoolean = esm.isBoolean;
  15870. exports.isFunction = esm.isFunction;
  15871. exports.isDate = esm.isDate;
  15872. exports.isArray = esm.isArray;
  15873. exports.isNil = esm.isNil;
  15874. exports.isObject = esm.isObject;
  15875. exports.isPlainObject = esm.isPlainObject;
  15876. exports.isEqual = esm.isEqual;
  15877. exports.deepMix = esm.deepMix;
  15878. exports.mix = esm.mix;
  15879. exports.each = esm.each;
  15880. exports.uniq = esm.uniq;
  15881. exports.find = esm.find;
  15882. var ArrayUtil = _interopRequireWildcard(array$1);
  15883. exports.Array = ArrayUtil;
  15884. Object.keys(dom).forEach(function (key) {
  15885. if (key === "default" || key === "__esModule") return;
  15886. if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
  15887. if (key in exports && exports[key] === dom[key]) return;
  15888. exports[key] = dom[key];
  15889. });
  15890. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
  15891. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  15892. /**
  15893. * @fileOverview Utility for F2
  15894. * @author dxq613 @gmail.com
  15895. * @author sima.zhang1990@gmail.com
  15896. */
  15897. function isObjectValueEqual(a, b) {
  15898. // for vue.js
  15899. a = Object.assign({}, a);
  15900. b = Object.assign({}, b);
  15901. var aProps = Object.getOwnPropertyNames(a);
  15902. var bProps = Object.getOwnPropertyNames(b);
  15903. if (aProps.length !== bProps.length) {
  15904. return false;
  15905. }
  15906. for (var i = 0, len = aProps.length; i < len; i++) {
  15907. var propName = aProps[i];
  15908. if (a[propName] !== b[propName]) {
  15909. return false;
  15910. }
  15911. }
  15912. return true;
  15913. }
  15914. function parsePadding(padding) {
  15915. var top;
  15916. var right;
  15917. var bottom;
  15918. var left;
  15919. if ((0, esm.isNumber)(padding) || (0, esm.isString)(padding)) {
  15920. top = bottom = left = right = padding;
  15921. } else if ((0, esm.isArray)(padding)) {
  15922. top = padding[0];
  15923. right = !(0, esm.isNil)(padding[1]) ? padding[1] : padding[0];
  15924. bottom = !(0, esm.isNil)(padding[2]) ? padding[2] : padding[0];
  15925. left = !(0, esm.isNil)(padding[3]) ? padding[3] : right;
  15926. }
  15927. return [top, right, bottom, left];
  15928. }
  15929. function directionEnabled(mode, dir) {
  15930. if (mode === undefined) {
  15931. return true;
  15932. } else if (typeof mode === 'string') {
  15933. return mode.indexOf(dir) !== -1;
  15934. }
  15935. return false;
  15936. }
  15937. function toTimeStamp(value) {
  15938. if ((0, esm.isString)(value)) {
  15939. if (value.indexOf('T') > 0) {
  15940. value = new Date(value).getTime();
  15941. } else {
  15942. // new Date('2010/01/10') 和 new Date('2010-01-10') 的差别在于:
  15943. // 如果仅有年月日时,前者是带有时区的: Fri Jan 10 2020 02:40:13 GMT+0800 (中国标准时间)
  15944. // 后者会格式化成 Sun Jan 10 2010 08:00:00 GMT+0800 (中国标准时间)
  15945. value = new Date(value.replace(/-/gi, '/')).getTime();
  15946. }
  15947. }
  15948. if ((0, esm.isDate)(value)) {
  15949. value = value.getTime();
  15950. }
  15951. return value;
  15952. }
  15953. });
  15954. unwrapExports(common);
  15955. var common_1 = common.isObjectValueEqual;
  15956. var common_2 = common.parsePadding;
  15957. var common_3 = common.directionEnabled;
  15958. var common_4 = common.toTimeStamp;
  15959. var common_5 = common.Array;
  15960. var common_6 = common.upperFirst;
  15961. var common_7 = common.lowerFirst;
  15962. var common_8 = common.isString;
  15963. var common_9 = common.isNumber;
  15964. var common_10 = common.isBoolean;
  15965. var common_11 = common.isFunction;
  15966. var common_12 = common.isDate;
  15967. var common_13 = common.isArray;
  15968. var common_14 = common.isNil;
  15969. var common_15 = common.isObject;
  15970. var common_16 = common.isPlainObject;
  15971. var common_17 = common.isEqual;
  15972. var common_18 = common.deepMix;
  15973. var common_19 = common.mix;
  15974. var common_20 = common.each;
  15975. var common_21 = common.uniq;
  15976. var common_22 = common.find;
  15977. var emit = createCommonjsModule(function (module, exports) {
  15978. exports.__esModule = true;
  15979. exports["default"] = void 0;
  15980. // 实现简单的事件机制
  15981. var EventEmit = /*#__PURE__*/function () {
  15982. function EventEmit() {
  15983. this.__events = {};
  15984. }
  15985. var _proto = EventEmit.prototype;
  15986. _proto.on = function on(type, listener) {
  15987. if (!type || !listener) {
  15988. return;
  15989. }
  15990. var events = this.__events[type] || [];
  15991. events.push(listener);
  15992. this.__events[type] = events;
  15993. };
  15994. _proto.emit = function emit(type, e) {
  15995. var _this = this;
  15996. if ((0, common.isObject)(type)) {
  15997. e = type;
  15998. type = e && e.type;
  15999. }
  16000. if (!type) {
  16001. return;
  16002. }
  16003. var events = this.__events[type];
  16004. if (!events || !events.length) {
  16005. return;
  16006. }
  16007. events.forEach(function (listener) {
  16008. listener.call(_this, e);
  16009. });
  16010. };
  16011. _proto.off = function off(type, listener) {
  16012. var __events = this.__events;
  16013. var events = __events[type];
  16014. if (!events || !events.length) {
  16015. return;
  16016. } // 如果没有指定方法,则删除所有项
  16017. if (!listener) {
  16018. delete __events[type];
  16019. return;
  16020. } // 删除指定的 listener
  16021. for (var i = 0, len = events.length; i < len; i++) {
  16022. if (events[i] === listener) {
  16023. events.splice(i, 1);
  16024. i--;
  16025. }
  16026. }
  16027. };
  16028. return EventEmit;
  16029. }();
  16030. var _default = EventEmit;
  16031. exports["default"] = _default;
  16032. });
  16033. unwrapExports(emit);
  16034. var controller = createCommonjsModule(function (module, exports) {
  16035. exports.__esModule = true;
  16036. exports["default"] = void 0;
  16037. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  16038. // 计算滑动的方向
  16039. var calcDirection = function calcDirection(start, end) {
  16040. var xDistance = end.x - start.x;
  16041. var yDistance = end.y - start.y; // x 的距离大于y 说明是横向,否则就是纵向
  16042. if (Math.abs(xDistance) > Math.abs(yDistance)) {
  16043. return xDistance > 0 ? 'right' : 'left';
  16044. }
  16045. return yDistance > 0 ? 'down' : 'up';
  16046. }; // 计算2点之间的距离
  16047. var calcDistance = function calcDistance(point1, point2) {
  16048. var xDistance = Math.abs(point2.x - point1.x);
  16049. var yDistance = Math.abs(point2.y - point1.y);
  16050. return Math.sqrt(xDistance * xDistance + yDistance * yDistance);
  16051. };
  16052. var getCenter = function getCenter(point1, point2) {
  16053. var x = point1.x + (point2.x - point1.x) / 2;
  16054. var y = point1.y + (point2.y - point1.y) / 2;
  16055. return {
  16056. x: x,
  16057. y: y
  16058. };
  16059. };
  16060. var convertPoints = function convertPoints(ev, canvas) {
  16061. var touches = ev.touches; // 认为是mouse事件
  16062. if (!touches) {
  16063. var point = (0, dom.getRelativePosition)({
  16064. x: ev.clientX,
  16065. y: ev.clientY
  16066. }, canvas);
  16067. return [point];
  16068. }
  16069. var points = [];
  16070. var len = touches.length;
  16071. for (var i = 0; i < len; i++) {
  16072. var touch = touches[i]; // x, y: 相对canvas原点的位置,clientX, clientY 相对于可视窗口的位置
  16073. var x = touch.x,
  16074. y = touch.y,
  16075. clientX = touch.clientX,
  16076. clientY = touch.clientY;
  16077. var _point = void 0; // 小程序环境会有x,y
  16078. if ((0, common.isNumber)(x) || (0, common.isNumber)(y)) {
  16079. _point = {
  16080. x: x,
  16081. y: y
  16082. };
  16083. } else {
  16084. // 浏览器环境再计算下canvas的相对位置
  16085. _point = (0, dom.getRelativePosition)({
  16086. x: clientX,
  16087. y: clientY
  16088. }, canvas);
  16089. }
  16090. points.push(_point);
  16091. }
  16092. return points;
  16093. };
  16094. var PRESS_DELAY = 250;
  16095. var EventController = /*#__PURE__*/function () {
  16096. function EventController(_ref) {
  16097. var _this = this;
  16098. var canvas = _ref.canvas,
  16099. el = _ref.el;
  16100. _defineProperty(this, "_click", function (ev) {
  16101. var points = convertPoints(ev, _this.canvas);
  16102. ev.points = points;
  16103. _this.emitEvent('click', ev);
  16104. });
  16105. _defineProperty(this, "_start", function (ev) {
  16106. var points = convertPoints(ev, _this.canvas);
  16107. if (!points) {
  16108. return;
  16109. }
  16110. ev.points = points;
  16111. _this.emitEvent('touchstart', ev); // 防止上次的内容没有清理掉,重新reset下
  16112. _this.reset(); // 记录touch start 的时间
  16113. _this.startTime = Date.now(); // 记录touch start 的点
  16114. _this.startPoints = points;
  16115. if (points.length > 1) {
  16116. _this.startDistance = calcDistance(points[0], points[1]);
  16117. _this.center = getCenter(points[0], points[1]);
  16118. } else {
  16119. // 如果touchstart后停顿250ms, 则也触发press事件
  16120. _this.pressTimeout = setTimeout(function () {
  16121. // 这里固定触发press事件
  16122. var eventType = 'press';
  16123. var direction = 'none';
  16124. ev.direction = direction;
  16125. _this.emitStart(eventType, ev);
  16126. _this.emitEvent(eventType, ev);
  16127. _this.eventType = eventType;
  16128. _this.direction = direction;
  16129. }, PRESS_DELAY);
  16130. }
  16131. });
  16132. _defineProperty(this, "_move", function (ev) {
  16133. var points = convertPoints(ev, _this.canvas);
  16134. if (!points) return;
  16135. _this.clearPressTimeout();
  16136. ev.points = points;
  16137. _this.emitEvent('touchmove', ev);
  16138. var startPoints = _this.startPoints;
  16139. if (!startPoints) return; // 多指触控
  16140. if (points.length > 1) {
  16141. // touchstart的距离
  16142. var startDistance = _this.startDistance;
  16143. var currentDistance = calcDistance(points[0], points[1]);
  16144. ev.zoom = currentDistance / startDistance;
  16145. ev.center = _this.center; // 触发缩放事件
  16146. _this.emitStart('pinch', ev);
  16147. _this.emitEvent('pinch', ev);
  16148. } else {
  16149. var deltaX = points[0].x - startPoints[0].x;
  16150. var deltaY = points[0].y - startPoints[0].y;
  16151. var direction = _this.direction || calcDirection(startPoints[0], points[0]);
  16152. _this.direction = direction; // 获取press或者pan的事件类型
  16153. // press 按住滑动, pan表示平移
  16154. // 如果start后立刻move,则触发pan, 如果有停顿,则触发press
  16155. var eventType = _this.getEventType(points);
  16156. ev.direction = direction;
  16157. ev.deltaX = deltaX;
  16158. ev.deltaY = deltaY;
  16159. _this.emitStart(eventType, ev);
  16160. _this.emitEvent(eventType, ev); // 记录最后2次move的时间和坐标,为了给swipe事件用
  16161. var prevMoveTime = _this.lastMoveTime;
  16162. var now = Date.now(); // 最后2次的时间间隔一定要大于0,否则swipe没发计算
  16163. if (now - prevMoveTime > 0) {
  16164. _this.prevMoveTime = prevMoveTime;
  16165. _this.prevMovePoints = _this.lastMovePoints;
  16166. _this.lastMoveTime = now;
  16167. _this.lastMovePoints = points;
  16168. }
  16169. }
  16170. });
  16171. _defineProperty(this, "_end", function (ev) {
  16172. _this.emitEnd(ev);
  16173. _this.emitEvent('touchend', ev); // swipe事件处理, 在touchend之后触发
  16174. var lastMoveTime = _this.lastMoveTime;
  16175. var now = Date.now(); // 做这个判断是为了最后一次touchmove后到end前,还有一个停顿的过程
  16176. // 100 是拍的一个值,理论这个值会很短,一般不卡顿的话在10ms以内
  16177. if (now - lastMoveTime < 100) {
  16178. var prevMoveTime = _this.prevMoveTime || _this.startTime;
  16179. var intervalTime = lastMoveTime - prevMoveTime; // 时间间隔一定要大于0, 否则计算没意义
  16180. if (intervalTime > 0) {
  16181. var prevMovePoints = _this.prevMovePoints || _this.startPoints;
  16182. var lastMovePoints = _this.lastMovePoints; // move速率
  16183. var velocity = calcDistance(prevMovePoints[0], lastMovePoints[0]) / intervalTime; // 0.3 是参考hammerjs的设置
  16184. if (velocity > 0.3) {
  16185. ev.velocity = velocity;
  16186. ev.direction = calcDirection(prevMovePoints[0], lastMovePoints[0]);
  16187. _this.emitEvent('swipe', ev);
  16188. }
  16189. }
  16190. }
  16191. _this.reset();
  16192. var touches = ev.touches; // 当多指只释放了1指时也会触发end, 这时重新触发一次start
  16193. if (touches && touches.length > 0) {
  16194. _this._start(ev);
  16195. }
  16196. });
  16197. _defineProperty(this, "_cancel", function (ev) {
  16198. _this.emitEvent('touchcancel', ev);
  16199. _this.reset();
  16200. });
  16201. // canvasEl
  16202. this.canvas = canvas;
  16203. this.delegateEvent(el); // 用来记录当前触发的事件
  16204. this.processEvent = {};
  16205. }
  16206. var _proto = EventController.prototype;
  16207. _proto.delegateEvent = function delegateEvent(canvasEl) {
  16208. // 代理这几个事件
  16209. canvasEl.addEventListener('click', this._click);
  16210. canvasEl.addEventListener('touchstart', this._start);
  16211. canvasEl.addEventListener('touchmove', this._move);
  16212. canvasEl.addEventListener('touchend', this._end);
  16213. canvasEl.addEventListener('touchcancel', this._cancel);
  16214. };
  16215. _proto.emitEvent = function emitEvent(type, ev) {
  16216. var canvas = this.canvas;
  16217. canvas.emit(type, ev);
  16218. };
  16219. _proto.getEventType = function getEventType(points) {
  16220. var eventType = this.eventType,
  16221. canvas = this.canvas,
  16222. startTime = this.startTime,
  16223. startPoints = this.startPoints;
  16224. if (eventType) {
  16225. return eventType;
  16226. }
  16227. var type;
  16228. var panEventListeners = canvas.__events.pan; // 如果没有pan事件的监听,默认都是press
  16229. if (!panEventListeners || !panEventListeners.length) {
  16230. type = 'press';
  16231. } else {
  16232. // 如果有pan事件的处理,press则需要停顿250ms, 且移动距离小于10
  16233. var now = Date.now();
  16234. if (now - startTime > PRESS_DELAY && calcDistance(startPoints[0], points[0]) < 10) {
  16235. type = 'press';
  16236. } else {
  16237. type = 'pan';
  16238. }
  16239. }
  16240. this.eventType = type;
  16241. return type;
  16242. };
  16243. _proto.enable = function enable(eventType) {
  16244. this.processEvent[eventType] = true;
  16245. } // 是否进行中的事件
  16246. ;
  16247. _proto.isProcess = function isProcess(eventType) {
  16248. return this.processEvent[eventType];
  16249. } // 触发start事件
  16250. ;
  16251. _proto.emitStart = function emitStart(type, ev) {
  16252. if (this.isProcess(type)) {
  16253. return;
  16254. }
  16255. this.enable(type);
  16256. this.emitEvent(type + "start", ev);
  16257. } // 触发end事件
  16258. ;
  16259. _proto.emitEnd = function emitEnd(ev) {
  16260. var _this2 = this;
  16261. var processEvent = this.processEvent;
  16262. Object.keys(processEvent).forEach(function (type) {
  16263. _this2.emitEvent(type + "end", ev);
  16264. delete processEvent[type];
  16265. });
  16266. };
  16267. _proto.clearPressTimeout = function clearPressTimeout() {
  16268. if (this.pressTimeout) {
  16269. clearTimeout(this.pressTimeout);
  16270. this.pressTimeout = 0;
  16271. }
  16272. };
  16273. _proto.reset = function reset() {
  16274. this.clearPressTimeout();
  16275. this.startTime = 0;
  16276. this.startPoints = null;
  16277. this.startDistance = 0;
  16278. this.direction = null;
  16279. this.eventType = null;
  16280. this.pinch = false;
  16281. this.prevMoveTime = 0;
  16282. this.prevMovePoints = null;
  16283. this.lastMoveTime = 0;
  16284. this.lastMovePoints = null;
  16285. };
  16286. return EventController;
  16287. }();
  16288. var _default = EventController;
  16289. exports["default"] = _default;
  16290. });
  16291. unwrapExports(controller);
  16292. var canvasElement = createCommonjsModule(function (module, exports) {
  16293. exports.__esModule = true;
  16294. exports["default"] = void 0;
  16295. var _emit = _interopRequireDefault(emit);
  16296. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  16297. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  16298. var CanvasElement = /*#__PURE__*/function (_EventEmit) {
  16299. _inheritsLoose(CanvasElement, _EventEmit);
  16300. function CanvasElement(ctx) {
  16301. var _this;
  16302. _this = _EventEmit.call(this) || this;
  16303. _this.context = ctx; // canvas实际的宽高 (width/height) * pixelRatio
  16304. _this.width = 0;
  16305. _this.height = 0;
  16306. _this.style = {};
  16307. _this.currentStyle = {}; // 用来标识是CanvasElement实例
  16308. _this.isCanvasElement = true;
  16309. return _this;
  16310. }
  16311. var _proto = CanvasElement.prototype;
  16312. _proto.getContext = function getContext()
  16313. /* type */
  16314. {
  16315. return this.context;
  16316. };
  16317. _proto.getBoundingClientRect = function getBoundingClientRect() {
  16318. var width = this.width;
  16319. var height = this.height; // 默认都处理成可视窗口的顶部位置
  16320. return {
  16321. top: 0,
  16322. right: width,
  16323. bottom: height,
  16324. left: 0
  16325. };
  16326. };
  16327. _proto.addEventListener = function addEventListener(type, listener) {
  16328. this.on(type, listener);
  16329. };
  16330. _proto.removeEventListener = function removeEventListener(type, listener) {
  16331. this.off(type, listener);
  16332. };
  16333. _proto.dispatchEvent = function dispatchEvent(type, e) {
  16334. this.emit(type, e);
  16335. };
  16336. return CanvasElement;
  16337. }(_emit["default"]);
  16338. function supportEventListener(canvas) {
  16339. if (!canvas) {
  16340. return false;
  16341. } // 非 HTMLCanvasElement
  16342. if (canvas.nodeType !== 1 || !canvas.nodeName || canvas.nodeName.toLowerCase() !== 'canvas') {
  16343. return false;
  16344. } // 微信小程序canvas.getContext('2d')时也是CanvasRenderingContext2D
  16345. // 也会有ctx.canvas, 而且nodeType也是1,所以还要在看下是否支持addEventListener
  16346. var support = false;
  16347. try {
  16348. canvas.addEventListener('eventTest', function () {
  16349. support = true;
  16350. });
  16351. canvas.dispatchEvent(new Event('eventTest'));
  16352. } catch (error) {
  16353. support = false;
  16354. }
  16355. return support;
  16356. }
  16357. var _default = {
  16358. create: function create(ctx) {
  16359. if (!ctx) {
  16360. return null;
  16361. }
  16362. if (supportEventListener(ctx.canvas)) {
  16363. return ctx.canvas;
  16364. }
  16365. return new CanvasElement(ctx);
  16366. }
  16367. };
  16368. exports["default"] = _default;
  16369. });
  16370. unwrapExports(canvasElement);
  16371. var matrix = createCommonjsModule(function (module, exports) {
  16372. exports.__esModule = true;
  16373. exports["default"] = void 0;
  16374. var Matrix = {
  16375. generateDefault: function generateDefault() {
  16376. return [1, 0, 0, 1, 0, 0];
  16377. },
  16378. isChanged: function isChanged(m) {
  16379. return m[0] !== 1 || m[1] !== 0 || m[2] !== 0 || m[3] !== 1 || m[4] !== 0 || m[5] !== 0;
  16380. },
  16381. multiply: function multiply(m1, m2) {
  16382. var m11 = m1[0] * m2[0] + m1[2] * m2[1];
  16383. var m12 = m1[1] * m2[0] + m1[3] * m2[1];
  16384. var m21 = m1[0] * m2[2] + m1[2] * m2[3];
  16385. var m22 = m1[1] * m2[2] + m1[3] * m2[3];
  16386. var dx = m1[0] * m2[4] + m1[2] * m2[5] + m1[4];
  16387. var dy = m1[1] * m2[4] + m1[3] * m2[5] + m1[5];
  16388. return [m11, m12, m21, m22, dx, dy];
  16389. },
  16390. scale: function scale(out, m, v) {
  16391. out[0] = m[0] * v[0];
  16392. out[1] = m[1] * v[0];
  16393. out[2] = m[2] * v[1];
  16394. out[3] = m[3] * v[1];
  16395. out[4] = m[4];
  16396. out[5] = m[5];
  16397. return out;
  16398. },
  16399. rotate: function rotate(out, m, radian) {
  16400. var c = Math.cos(radian);
  16401. var s = Math.sin(radian);
  16402. var m11 = m[0] * c + m[2] * s;
  16403. var m12 = m[1] * c + m[3] * s;
  16404. var m21 = m[0] * -s + m[2] * c;
  16405. var m22 = m[1] * -s + m[3] * c;
  16406. out[0] = m11;
  16407. out[1] = m12;
  16408. out[2] = m21;
  16409. out[3] = m22;
  16410. out[4] = m[4];
  16411. out[5] = m[5];
  16412. return out;
  16413. },
  16414. translate: function translate(out, m, v) {
  16415. out[0] = m[0];
  16416. out[1] = m[1];
  16417. out[2] = m[2];
  16418. out[3] = m[3];
  16419. out[4] = m[4] + m[0] * v[0] + m[2] * v[1];
  16420. out[5] = m[5] + m[1] * v[0] + m[3] * v[1];
  16421. return out;
  16422. },
  16423. transform: function transform(m, actions) {
  16424. var out = [].concat(m);
  16425. for (var i = 0, len = actions.length; i < len; i++) {
  16426. var action = actions[i];
  16427. switch (action[0]) {
  16428. case 't':
  16429. Matrix.translate(out, out, [action[1], action[2]]);
  16430. break;
  16431. case 's':
  16432. Matrix.scale(out, out, [action[1], action[2]]);
  16433. break;
  16434. case 'r':
  16435. Matrix.rotate(out, out, action[1]);
  16436. break;
  16437. }
  16438. }
  16439. return out;
  16440. }
  16441. };
  16442. var _default = Matrix;
  16443. exports["default"] = _default;
  16444. });
  16445. unwrapExports(matrix);
  16446. var vector2 = createCommonjsModule(function (module, exports) {
  16447. exports.__esModule = true;
  16448. exports["default"] = void 0;
  16449. /**
  16450. * 2 Dimensional Vector
  16451. * @module vector2
  16452. */
  16453. var _default = {
  16454. /**
  16455. * Creates a new, empty vector2
  16456. *
  16457. * @return {vector2} a new 2D vector
  16458. */
  16459. create: function create() {
  16460. return [0, 0];
  16461. },
  16462. /**
  16463. * Calculates the length of a vector2
  16464. *
  16465. * @param {vector2} v vector to calculate length of
  16466. * @return {Number} length of v
  16467. */
  16468. length: function length(v) {
  16469. var x = v[0];
  16470. var y = v[1];
  16471. return Math.sqrt(x * x + y * y);
  16472. },
  16473. /**
  16474. * Normalize a vector2
  16475. *
  16476. * @param {vector2} out the receiving vector
  16477. * @param {vector2} v vector to normalize
  16478. * @return {vector2} out
  16479. */
  16480. normalize: function normalize(out, v) {
  16481. var len = this.length(v);
  16482. if (len === 0) {
  16483. out[0] = 0;
  16484. out[1] = 0;
  16485. } else {
  16486. out[0] = v[0] / len;
  16487. out[1] = v[1] / len;
  16488. }
  16489. return out;
  16490. },
  16491. /**
  16492. * Adds two vector2's
  16493. *
  16494. * @param {vector2} out the receiving vector
  16495. * @param {vector2} v1 the first operand
  16496. * @param {vector2} v2 the second operand
  16497. * @return {vector2} out
  16498. */
  16499. add: function add(out, v1, v2) {
  16500. out[0] = v1[0] + v2[0];
  16501. out[1] = v1[1] + v2[1];
  16502. return out;
  16503. },
  16504. /**
  16505. * Subtracts vector v2 from vector v1
  16506. *
  16507. * @param {vector2} out the receiving vector
  16508. * @param {vector2} v1 the first operand
  16509. * @param {vector2} v2 the second operand
  16510. * @return {vector2} out
  16511. */
  16512. sub: function sub(out, v1, v2) {
  16513. out[0] = v1[0] - v2[0];
  16514. out[1] = v1[1] - v2[1];
  16515. return out;
  16516. },
  16517. /**
  16518. * Scales a vector2 by a scalar number
  16519. *
  16520. * @param {vector2} out the receiving vector
  16521. * @param {vector2} v the vector to scale
  16522. * @param {Number} s amount to scale the vector by
  16523. * @return {vector2} out
  16524. */
  16525. scale: function scale(out, v, s) {
  16526. out[0] = v[0] * s;
  16527. out[1] = v[1] * s;
  16528. return out;
  16529. },
  16530. /**
  16531. * Calculates the dot product of two vector2's
  16532. *
  16533. * @param {vector2} v1 the first operand
  16534. * @param {vector2} v2 the second operand
  16535. * @return {Number} dot product of v1 and v2
  16536. */
  16537. dot: function dot(v1, v2) {
  16538. return v1[0] * v2[0] + v1[1] * v2[1];
  16539. },
  16540. /**
  16541. * Calculates the direction of two vector2's
  16542. *
  16543. * @param {vector2} v1 the first operand
  16544. * @param {vector2} v2 the second operand
  16545. * @return {Boolean} the direction of v1 and v2
  16546. */
  16547. direction: function direction(v1, v2) {
  16548. return v1[0] * v2[1] - v2[0] * v1[1];
  16549. },
  16550. /**
  16551. * Calculates the angle of two vector2's
  16552. *
  16553. * @param {vector2} v1 the first operand
  16554. * @param {vector2} v2 the second operand
  16555. * @return {Number} angle of v1 and v2
  16556. */
  16557. angle: function angle(v1, v2) {
  16558. var theta = this.dot(v1, v2) / (this.length(v1) * this.length(v2));
  16559. return Math.acos(theta);
  16560. },
  16561. /**
  16562. * Calculates the angle of two vector2's with direction
  16563. *
  16564. * @param {vector2} v1 the first operand
  16565. * @param {vector2} v2 the second operand
  16566. * @param {Boolean} direction the direction of two vector2's
  16567. * @return {Number} angle of v1 and v2
  16568. */
  16569. angleTo: function angleTo(v1, v2, direction) {
  16570. var angle = this.angle(v1, v2);
  16571. var angleLargeThanPI = this.direction(v1, v2) >= 0;
  16572. if (direction) {
  16573. if (angleLargeThanPI) {
  16574. return Math.PI * 2 - angle;
  16575. }
  16576. return angle;
  16577. }
  16578. if (angleLargeThanPI) {
  16579. return angle;
  16580. }
  16581. return Math.PI * 2 - angle;
  16582. },
  16583. /**
  16584. * whether a vector2 is zero vector
  16585. *
  16586. * @param {vector2} v vector to calculate
  16587. * @return {Boolean} is or not a zero vector
  16588. */
  16589. zero: function zero(v) {
  16590. return v[0] === 0 && v[1] === 0;
  16591. },
  16592. /**
  16593. * Calculates the euclidian distance between two vector2's
  16594. *
  16595. * @param {vector2} v1 the first operand
  16596. * @param {vector2} v2 the second operand
  16597. * @return {Number} distance between a and b
  16598. */
  16599. distance: function distance(v1, v2) {
  16600. var x = v2[0] - v1[0];
  16601. var y = v2[1] - v1[1];
  16602. return Math.sqrt(x * x + y * y);
  16603. },
  16604. /**
  16605. * Creates a new vector2 initialized with values from an existing vector
  16606. *
  16607. * @param {vector2} v vector to clone
  16608. * @return {Array} a new 2D vector
  16609. */
  16610. clone: function clone(v) {
  16611. return [v[0], v[1]];
  16612. },
  16613. /**
  16614. * Return the minimum of two vector2's
  16615. *
  16616. * @param {vector2} out the receiving vector
  16617. * @param {vector2} v1 the first operand
  16618. * @param {vector2} v2 the second operand
  16619. * @return {vector2} out
  16620. */
  16621. min: function min(out, v1, v2) {
  16622. out[0] = Math.min(v1[0], v2[0]);
  16623. out[1] = Math.min(v1[1], v2[1]);
  16624. return out;
  16625. },
  16626. /**
  16627. * Return the maximum of two vector2's
  16628. *
  16629. * @param {vector2} out the receiving vector
  16630. * @param {vector2} v1 the first operand
  16631. * @param {vector2} v2 the second operand
  16632. * @return {vector2} out
  16633. */
  16634. max: function max(out, v1, v2) {
  16635. out[0] = Math.max(v1[0], v2[0]);
  16636. out[1] = Math.max(v1[1], v2[1]);
  16637. return out;
  16638. },
  16639. /**
  16640. * Transforms the vector2 with a mat2d
  16641. *
  16642. * @param {vector2} out the receiving vector
  16643. * @param {vector2} v the vector to transform
  16644. * @param {mat2d} m matrix to transform with
  16645. * @return {vector2} out
  16646. */
  16647. transformMat2d: function transformMat2d(out, v, m) {
  16648. var x = v[0];
  16649. var y = v[1];
  16650. out[0] = m[0] * x + m[2] * y + m[4];
  16651. out[1] = m[1] * x + m[3] * y + m[5];
  16652. return out;
  16653. }
  16654. };
  16655. exports["default"] = _default;
  16656. });
  16657. unwrapExports(vector2);
  16658. var styleParse = createCommonjsModule(function (module, exports) {
  16659. exports.__esModule = true;
  16660. exports.parseStyle = parseStyle;
  16661. exports["default"] = void 0;
  16662. function _mod(n, m) {
  16663. return (n % m + m) % m;
  16664. }
  16665. function _addStop(steps, gradient) {
  16666. (0, common.each)(steps, function (item) {
  16667. item = item.split(':');
  16668. gradient.addColorStop(Number(item[0]), item[1]);
  16669. });
  16670. } // the string format: 'l(0) 0:#ffffff 0.5:#7ec2f3 1:#1890ff'
  16671. function _parseLineGradient(color, shape, context) {
  16672. var arr = color.split(' ');
  16673. var angle = arr[0].slice(2, arr[0].length - 1);
  16674. angle = _mod(parseFloat(angle) * Math.PI / 180, Math.PI * 2);
  16675. var steps = arr.slice(1);
  16676. var _shape$getBBox = shape.getBBox(),
  16677. minX = _shape$getBBox.minX,
  16678. minY = _shape$getBBox.minY,
  16679. maxX = _shape$getBBox.maxX,
  16680. maxY = _shape$getBBox.maxY;
  16681. var start;
  16682. var end;
  16683. if (angle >= 0 && angle < 0.5 * Math.PI) {
  16684. start = {
  16685. x: minX,
  16686. y: minY
  16687. };
  16688. end = {
  16689. x: maxX,
  16690. y: maxY
  16691. };
  16692. } else if (0.5 * Math.PI <= angle && angle < Math.PI) {
  16693. start = {
  16694. x: maxX,
  16695. y: minY
  16696. };
  16697. end = {
  16698. x: minX,
  16699. y: maxY
  16700. };
  16701. } else if (Math.PI <= angle && angle < 1.5 * Math.PI) {
  16702. start = {
  16703. x: maxX,
  16704. y: maxY
  16705. };
  16706. end = {
  16707. x: minX,
  16708. y: minY
  16709. };
  16710. } else {
  16711. start = {
  16712. x: minX,
  16713. y: maxY
  16714. };
  16715. end = {
  16716. x: maxX,
  16717. y: minY
  16718. };
  16719. }
  16720. var tanTheta = Math.tan(angle);
  16721. var tanTheta2 = tanTheta * tanTheta;
  16722. var x = (end.x - start.x + tanTheta * (end.y - start.y)) / (tanTheta2 + 1) + start.x;
  16723. var y = tanTheta * (end.x - start.x + tanTheta * (end.y - start.y)) / (tanTheta2 + 1) + start.y;
  16724. var gradient = context.createLinearGradient(start.x, start.y, x, y);
  16725. _addStop(steps, gradient);
  16726. return gradient;
  16727. } // the string format: 'r(0.5, 0.5, 0.1) 0:#ffffff 1:#1890ff'
  16728. function _parseRadialGradient(color, shape, context) {
  16729. var arr = color.split(' ');
  16730. var circleCfg = arr[0].slice(2, arr[0].length - 1);
  16731. circleCfg = circleCfg.split(',');
  16732. var fx = parseFloat(circleCfg[0]);
  16733. var fy = parseFloat(circleCfg[1]);
  16734. var fr = parseFloat(circleCfg[2]);
  16735. var steps = arr.slice(1); // if radius is 0, no gradient, stroke with the last color
  16736. if (fr === 0) {
  16737. var _color = steps[steps.length - 1];
  16738. return _color.split(':')[1];
  16739. }
  16740. var _shape$getBBox2 = shape.getBBox(),
  16741. width = _shape$getBBox2.width,
  16742. height = _shape$getBBox2.height,
  16743. minX = _shape$getBBox2.minX,
  16744. minY = _shape$getBBox2.minY;
  16745. var r = Math.sqrt(width * width + height * height) / 2;
  16746. var gradient = context.createRadialGradient(minX + width * fx, minY + height * fy, fr * r, minX + width / 2, minY + height / 2, r);
  16747. _addStop(steps, gradient);
  16748. return gradient;
  16749. }
  16750. function parseStyle(color, shape, context) {
  16751. if (color[1] === '(') {
  16752. try {
  16753. var firstCode = color[0];
  16754. if (firstCode === 'l') {
  16755. return _parseLineGradient(color, shape, context);
  16756. } else if (firstCode === 'r') {
  16757. return _parseRadialGradient(color, shape, context);
  16758. }
  16759. } catch (ev) {
  16760. console.error('error in parsing gradient string, please check if there are any extra whitespaces.');
  16761. console.error(ev);
  16762. }
  16763. }
  16764. return color;
  16765. }
  16766. var _default = {
  16767. parseStyle: parseStyle
  16768. };
  16769. exports["default"] = _default;
  16770. });
  16771. unwrapExports(styleParse);
  16772. var styleParse_1 = styleParse.parseStyle;
  16773. var element = createCommonjsModule(function (module, exports) {
  16774. exports.__esModule = true;
  16775. exports["default"] = void 0;
  16776. var _matrix = _interopRequireDefault(matrix);
  16777. var _vector = _interopRequireDefault(vector2);
  16778. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  16779. var ALIAS_ATTRS_MAP = {
  16780. stroke: 'strokeStyle',
  16781. fill: 'fillStyle',
  16782. opacity: 'globalAlpha'
  16783. };
  16784. var SHAPE_ATTRS = ['fillStyle', 'font', 'globalAlpha', 'lineCap', 'lineWidth', 'lineJoin', 'miterLimit', 'shadowBlur', 'shadowColor', 'shadowOffsetX', 'shadowOffsetY', 'strokeStyle', 'textAlign', 'textBaseline', 'lineDash', 'shadow' // 兼容支付宝小程序
  16785. ];
  16786. var CLIP_SHAPES = ['circle', 'sector', 'polygon', 'rect', 'polyline'];
  16787. var Element = /*#__PURE__*/function () {
  16788. var _proto = Element.prototype;
  16789. _proto._initProperties = function _initProperties() {
  16790. this._attrs = {
  16791. zIndex: 0,
  16792. visible: true,
  16793. destroyed: false
  16794. };
  16795. };
  16796. function Element(cfg) {
  16797. this._initProperties();
  16798. (0, common.mix)(this._attrs, cfg);
  16799. var attrs = this._attrs.attrs;
  16800. if (attrs) {
  16801. this.initAttrs(attrs);
  16802. }
  16803. this.initTransform();
  16804. }
  16805. _proto.get = function get(name) {
  16806. return this._attrs[name];
  16807. };
  16808. _proto.set = function set(name, value) {
  16809. this._attrs[name] = value;
  16810. };
  16811. _proto.isGroup = function isGroup() {
  16812. return this.get('isGroup');
  16813. };
  16814. _proto.isShape = function isShape() {
  16815. return this.get('isShape');
  16816. };
  16817. _proto.initAttrs = function initAttrs(attrs) {
  16818. this.attr((0, common.mix)(this.getDefaultAttrs(), attrs));
  16819. };
  16820. _proto.getDefaultAttrs = function getDefaultAttrs() {
  16821. return {};
  16822. };
  16823. _proto._setAttr = function _setAttr(name, value) {
  16824. var attrs = this._attrs.attrs;
  16825. if (name === 'clip') {
  16826. value = this._setAttrClip(value);
  16827. } else {
  16828. var alias = ALIAS_ATTRS_MAP[name];
  16829. if (alias) {
  16830. attrs[alias] = value;
  16831. }
  16832. }
  16833. attrs[name] = value;
  16834. };
  16835. _proto._getAttr = function _getAttr(name) {
  16836. return this._attrs.attrs[name];
  16837. } // _afterAttrsSet() {}
  16838. ;
  16839. _proto._setAttrClip = function _setAttrClip(clip) {
  16840. if (clip && CLIP_SHAPES.indexOf(clip._attrs.type) > -1) {
  16841. if (clip.get('canvas') === null) {
  16842. clip = Object.assign({}, clip);
  16843. }
  16844. clip.set('parent', this.get('parent'));
  16845. clip.set('context', this.get('context'));
  16846. return clip;
  16847. }
  16848. return null;
  16849. };
  16850. _proto.attr = function attr(name, value) {
  16851. var self = this;
  16852. if (self.get('destroyed')) return null;
  16853. var argumentsLen = arguments.length;
  16854. if (argumentsLen === 0) {
  16855. return self._attrs.attrs;
  16856. }
  16857. if ((0, common.isObject)(name)) {
  16858. this._attrs.bbox = null;
  16859. for (var k in name) {
  16860. self._setAttr(k, name[k]);
  16861. }
  16862. if (self._afterAttrsSet) {
  16863. self._afterAttrsSet();
  16864. }
  16865. return self;
  16866. }
  16867. if (argumentsLen === 2) {
  16868. this._attrs.bbox = null;
  16869. self._setAttr(name, value);
  16870. if (self._afterAttrsSet) {
  16871. self._afterAttrsSet();
  16872. }
  16873. return self;
  16874. }
  16875. return self._getAttr(name);
  16876. };
  16877. _proto.getParent = function getParent() {
  16878. return this.get('parent');
  16879. };
  16880. _proto.draw = function draw(context) {
  16881. if (this.get('destroyed')) {
  16882. return;
  16883. }
  16884. if (this.get('visible')) {
  16885. this.setContext(context);
  16886. this.drawInner(context);
  16887. this.restoreContext(context);
  16888. }
  16889. };
  16890. _proto.setContext = function setContext(context) {
  16891. var clip = this._attrs.attrs.clip;
  16892. context.save();
  16893. if (clip) {
  16894. clip.resetTransform(context);
  16895. clip.createPath(context);
  16896. context.clip();
  16897. }
  16898. this.resetContext(context);
  16899. this.resetTransform(context);
  16900. };
  16901. _proto.restoreContext = function restoreContext(context) {
  16902. context.restore();
  16903. };
  16904. _proto.resetContext = function resetContext(context) {
  16905. var elAttrs = this._attrs.attrs;
  16906. if (!this._attrs.isGroup) {
  16907. for (var k in elAttrs) {
  16908. if (SHAPE_ATTRS.indexOf(k) > -1) {
  16909. var v = elAttrs[k];
  16910. if (k === 'fillStyle' || k === 'strokeStyle') {
  16911. v = (0, styleParse.parseStyle)(v, this, context);
  16912. }
  16913. if (k === 'lineDash' && context.setLineDash && (0, common.isArray)(v)) {
  16914. context.setLineDash(v);
  16915. } else {
  16916. context[k] = v;
  16917. }
  16918. }
  16919. }
  16920. }
  16921. };
  16922. _proto.hasFill = function hasFill() {
  16923. return this.get('canFill') && this._attrs.attrs.fillStyle;
  16924. };
  16925. _proto.hasStroke = function hasStroke() {
  16926. return this.get('canStroke') && this._attrs.attrs.strokeStyle;
  16927. };
  16928. _proto.drawInner = function drawInner()
  16929. /* context */
  16930. {};
  16931. _proto.show = function show() {
  16932. this.set('visible', true);
  16933. return this;
  16934. };
  16935. _proto.hide = function hide() {
  16936. this.set('visible', false);
  16937. return this;
  16938. };
  16939. _proto.isVisible = function isVisible() {
  16940. return this.get('visible');
  16941. };
  16942. _proto._removeFromParent = function _removeFromParent() {
  16943. var parent = this.get('parent');
  16944. if (parent) {
  16945. var children = parent.get('children');
  16946. common.Array.remove(children, this);
  16947. }
  16948. return this;
  16949. };
  16950. _proto.remove = function remove(destroy) {
  16951. if (destroy) {
  16952. this.destroy();
  16953. } else {
  16954. this._removeFromParent();
  16955. }
  16956. };
  16957. _proto.destroy = function destroy() {
  16958. var destroyed = this.get('destroyed');
  16959. if (destroyed) {
  16960. return null;
  16961. }
  16962. this._removeFromParent();
  16963. this._attrs = {};
  16964. this.set('destroyed', true);
  16965. };
  16966. _proto.getBBox = function getBBox() {
  16967. return {
  16968. minX: 0,
  16969. maxX: 0,
  16970. minY: 0,
  16971. maxY: 0,
  16972. width: 0,
  16973. height: 0
  16974. };
  16975. };
  16976. _proto.initTransform = function initTransform() {
  16977. var attrs = this._attrs.attrs || {};
  16978. if (!attrs.matrix) {
  16979. attrs.matrix = [1, 0, 0, 1, 0, 0];
  16980. }
  16981. this._attrs.attrs = attrs;
  16982. };
  16983. _proto.getMatrix = function getMatrix() {
  16984. return this._attrs.attrs.matrix;
  16985. };
  16986. _proto.setMatrix = function setMatrix(m) {
  16987. this._attrs.attrs.matrix = [m[0], m[1], m[2], m[3], m[4], m[5]];
  16988. };
  16989. _proto.transform = function transform(actions) {
  16990. var matrix = this._attrs.attrs.matrix;
  16991. this._attrs.attrs.matrix = _matrix["default"].transform(matrix, actions);
  16992. return this;
  16993. };
  16994. _proto.setTransform = function setTransform(actions) {
  16995. this._attrs.attrs.matrix = [1, 0, 0, 1, 0, 0];
  16996. return this.transform(actions);
  16997. };
  16998. _proto.translate = function translate(x, y) {
  16999. var matrix = this._attrs.attrs.matrix;
  17000. _matrix["default"].translate(matrix, matrix, [x, y]);
  17001. };
  17002. _proto.rotate = function rotate(rad) {
  17003. var matrix = this._attrs.attrs.matrix;
  17004. _matrix["default"].rotate(matrix, matrix, rad);
  17005. };
  17006. _proto.scale = function scale(sx, sy) {
  17007. var matrix = this._attrs.attrs.matrix;
  17008. _matrix["default"].scale(matrix, matrix, [sx, sy]);
  17009. };
  17010. _proto.moveTo = function moveTo(x, y) {
  17011. var cx = this._attrs.x || 0;
  17012. var cy = this._attrs.y || 0;
  17013. this.translate(x - cx, y - cy);
  17014. this.set('x', x);
  17015. this.set('y', y);
  17016. };
  17017. _proto.apply = function apply(v) {
  17018. var m = this._attrs.attrs.matrix;
  17019. _vector["default"].transformMat2d(v, v, m);
  17020. return this;
  17021. };
  17022. _proto.resetTransform = function resetTransform(context) {
  17023. var mo = this._attrs.attrs.matrix;
  17024. if (_matrix["default"].isChanged(mo)) {
  17025. context.transform(mo[0], mo[1], mo[2], mo[3], mo[4], mo[5]);
  17026. }
  17027. };
  17028. _proto.isDestroyed = function isDestroyed() {
  17029. return this.get('destroyed');
  17030. };
  17031. return Element;
  17032. }();
  17033. var _default = Element;
  17034. exports["default"] = _default;
  17035. });
  17036. unwrapExports(element);
  17037. var shape = createCommonjsModule(function (module, exports) {
  17038. exports.__esModule = true;
  17039. exports["default"] = void 0;
  17040. var _element = _interopRequireDefault(element);
  17041. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  17042. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  17043. var Shape = /*#__PURE__*/function (_Element) {
  17044. _inheritsLoose(Shape, _Element);
  17045. function Shape() {
  17046. return _Element.apply(this, arguments) || this;
  17047. }
  17048. var _proto = Shape.prototype;
  17049. _proto._initProperties = function _initProperties() {
  17050. this._attrs = {
  17051. zIndex: 0,
  17052. visible: true,
  17053. destroyed: false,
  17054. isShape: true,
  17055. attrs: {}
  17056. };
  17057. };
  17058. _proto.getType = function getType() {
  17059. return this._attrs.type;
  17060. };
  17061. _proto.drawInner = function drawInner(context) {
  17062. var self = this;
  17063. var attrs = self.get('attrs');
  17064. self.createPath(context);
  17065. var originOpacity = context.globalAlpha;
  17066. if (self.hasFill()) {
  17067. var fillOpacity = attrs.fillOpacity;
  17068. if (!(0, common.isNil)(fillOpacity) && fillOpacity !== 1) {
  17069. context.globalAlpha = fillOpacity;
  17070. context.fill();
  17071. context.globalAlpha = originOpacity;
  17072. } else {
  17073. context.fill();
  17074. }
  17075. }
  17076. if (self.hasStroke()) {
  17077. var lineWidth = attrs.lineWidth;
  17078. if (lineWidth > 0) {
  17079. var strokeOpacity = attrs.strokeOpacity;
  17080. if (!(0, common.isNil)(strokeOpacity) && strokeOpacity !== 1) {
  17081. context.globalAlpha = strokeOpacity;
  17082. }
  17083. context.stroke();
  17084. }
  17085. }
  17086. };
  17087. _proto.getBBox = function getBBox() {
  17088. var bbox = this._attrs.bbox;
  17089. if (!bbox) {
  17090. bbox = this.calculateBox();
  17091. if (bbox) {
  17092. bbox.x = bbox.minX;
  17093. bbox.y = bbox.minY;
  17094. bbox.width = bbox.maxX - bbox.minX;
  17095. bbox.height = bbox.maxY - bbox.minY;
  17096. }
  17097. this._attrs.bbox = bbox;
  17098. }
  17099. return bbox;
  17100. };
  17101. _proto.calculateBox = function calculateBox() {
  17102. return null;
  17103. };
  17104. _proto.createPath = function createPath() {};
  17105. return Shape;
  17106. }(_element["default"]);
  17107. var _default = Shape;
  17108. exports["default"] = _default;
  17109. });
  17110. unwrapExports(shape);
  17111. var container = createCommonjsModule(function (module, exports) {
  17112. exports.__esModule = true;
  17113. exports["default"] = void 0;
  17114. var _shape = _interopRequireDefault(shape);
  17115. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  17116. var SHAPE_MAP = {};
  17117. var INDEX = '_INDEX';
  17118. function getComparer(compare) {
  17119. return function (left, right) {
  17120. var result = compare(left, right);
  17121. return result === 0 ? left[INDEX] - right[INDEX] : result;
  17122. };
  17123. }
  17124. var _default = {
  17125. getGroupClass: function getGroupClass() {},
  17126. getChildren: function getChildren() {
  17127. return this.get('children');
  17128. },
  17129. addShape: function addShape(type, cfg) {
  17130. if (cfg === void 0) {
  17131. cfg = {};
  17132. }
  17133. var canvas = this.get('canvas');
  17134. var shapeType = SHAPE_MAP[type];
  17135. if (!shapeType) {
  17136. shapeType = (0, common.upperFirst)(type);
  17137. SHAPE_MAP[type] = shapeType;
  17138. }
  17139. cfg.canvas = canvas;
  17140. if (shapeType === 'Text' && canvas && canvas.get('fontFamily')) {
  17141. cfg.attrs.fontFamily = cfg.attrs.fontFamily || canvas.get('fontFamily');
  17142. }
  17143. var shape = new _shape["default"][shapeType](cfg);
  17144. this.add(shape);
  17145. return shape;
  17146. },
  17147. addGroup: function addGroup(cfg) {
  17148. var canvas = this.get('canvas');
  17149. var groupClass = this.getGroupClass();
  17150. cfg = (0, common.mix)({}, cfg);
  17151. cfg.canvas = canvas;
  17152. cfg.parent = this;
  17153. var rst = new groupClass(cfg);
  17154. this.add(rst);
  17155. return rst;
  17156. },
  17157. contain: function contain(item) {
  17158. var children = this.get('children');
  17159. return children.indexOf(item) > -1;
  17160. },
  17161. sort: function sort() {
  17162. var children = this.get('children');
  17163. for (var i = 0, len = children.length; i < len; i++) {
  17164. var child = children[i];
  17165. child[INDEX] = i;
  17166. }
  17167. children.sort(getComparer(function (obj1, obj2) {
  17168. return obj1.get('zIndex') - obj2.get('zIndex');
  17169. }));
  17170. return this;
  17171. },
  17172. drawInner: function drawInner(context) {
  17173. var children = this.get('children');
  17174. for (var i = 0, len = children.length; i < len; i++) {
  17175. var child = children[i];
  17176. child.draw(context);
  17177. }
  17178. return this;
  17179. },
  17180. clear: function clear() {
  17181. var children = this.get('children');
  17182. while (children.length !== 0) {
  17183. children[children.length - 1].remove(true);
  17184. }
  17185. return this;
  17186. },
  17187. add: function add(items) {
  17188. var self = this;
  17189. var children = self.get('children');
  17190. if (!(0, common.isArray)(items)) {
  17191. items = [items];
  17192. }
  17193. for (var i = 0, len = items.length; i < len; i++) {
  17194. var item = items[i];
  17195. var parent = item.get('parent');
  17196. if (parent) {
  17197. var descendants = parent.get('children');
  17198. common.Array.remove(descendants, item);
  17199. }
  17200. self._setEvn(item);
  17201. children.push(item);
  17202. }
  17203. return self;
  17204. },
  17205. _setEvn: function _setEvn(item) {
  17206. var self = this;
  17207. item._attrs.parent = self;
  17208. item._attrs.context = self._attrs.context;
  17209. item._attrs.canvas = self._attrs.canvas;
  17210. var clip = item._attrs.attrs.clip;
  17211. if (clip) {
  17212. clip.set('parent', self);
  17213. clip.set('context', self.get('context'));
  17214. }
  17215. if (item._attrs.isGroup) {
  17216. var children = item._attrs.children;
  17217. for (var i = 0, len = children.length; i < len; i++) {
  17218. item._setEvn(children[i]);
  17219. }
  17220. }
  17221. }
  17222. };
  17223. exports["default"] = _default;
  17224. });
  17225. unwrapExports(container);
  17226. var group$2 = createCommonjsModule(function (module, exports) {
  17227. exports.__esModule = true;
  17228. exports["default"] = void 0;
  17229. var _element = _interopRequireDefault(element);
  17230. var _container = _interopRequireDefault(container);
  17231. var _vector = _interopRequireDefault(vector2);
  17232. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  17233. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  17234. var Group = /*#__PURE__*/function (_Element) {
  17235. _inheritsLoose(Group, _Element);
  17236. function Group() {
  17237. return _Element.apply(this, arguments) || this;
  17238. }
  17239. var _proto = Group.prototype;
  17240. _proto._initProperties = function _initProperties() {
  17241. this._attrs = {
  17242. zIndex: 0,
  17243. visible: true,
  17244. destroyed: false,
  17245. isGroup: true,
  17246. children: []
  17247. };
  17248. };
  17249. _proto.getBBox = function getBBox() {
  17250. var self = this;
  17251. var minX = Infinity;
  17252. var maxX = -Infinity;
  17253. var minY = Infinity;
  17254. var maxY = -Infinity;
  17255. var children = self.get('children');
  17256. for (var i = 0, length = children.length; i < length; i++) {
  17257. var child = children[i];
  17258. if (child.get('visible')) {
  17259. var box = child.getBBox();
  17260. if (!box) {
  17261. continue;
  17262. }
  17263. var leftTop = [box.minX, box.minY];
  17264. var leftBottom = [box.minX, box.maxY];
  17265. var rightTop = [box.maxX, box.minY];
  17266. var rightBottom = [box.maxX, box.maxY];
  17267. var matrix = child.attr('matrix');
  17268. _vector["default"].transformMat2d(leftTop, leftTop, matrix);
  17269. _vector["default"].transformMat2d(leftBottom, leftBottom, matrix);
  17270. _vector["default"].transformMat2d(rightTop, rightTop, matrix);
  17271. _vector["default"].transformMat2d(rightBottom, rightBottom, matrix);
  17272. minX = Math.min(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0], minX);
  17273. maxX = Math.max(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0], maxX);
  17274. minY = Math.min(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1], minY);
  17275. maxY = Math.max(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1], maxY);
  17276. }
  17277. }
  17278. return {
  17279. minX: minX,
  17280. minY: minY,
  17281. maxX: maxX,
  17282. maxY: maxY,
  17283. x: minX,
  17284. y: minY,
  17285. width: maxX - minX,
  17286. height: maxY - minY
  17287. };
  17288. };
  17289. _proto.destroy = function destroy() {
  17290. if (this.get('destroyed')) {
  17291. return;
  17292. }
  17293. this.clear();
  17294. _Element.prototype.destroy.call(this);
  17295. };
  17296. return Group;
  17297. }(_element["default"]);
  17298. (0, common.mix)(Group.prototype, _container["default"], {
  17299. getGroupClass: function getGroupClass() {
  17300. return Group;
  17301. }
  17302. });
  17303. var _default = Group;
  17304. exports["default"] = _default;
  17305. });
  17306. unwrapExports(group$2);
  17307. var requestAnimationFrame_1 = createCommonjsModule(function (module, exports) {
  17308. exports.__esModule = true;
  17309. exports.requestAnimationFrame = void 0;
  17310. var requestAnimationFrame = typeof window === 'object' && window.requestAnimationFrame ? window.requestAnimationFrame : function (fn) {
  17311. return setTimeout(fn, 16);
  17312. };
  17313. exports.requestAnimationFrame = requestAnimationFrame;
  17314. });
  17315. unwrapExports(requestAnimationFrame_1);
  17316. var requestAnimationFrame_2 = requestAnimationFrame_1.requestAnimationFrame;
  17317. var canvas = createCommonjsModule(function (module, exports) {
  17318. exports.__esModule = true;
  17319. exports["default"] = void 0;
  17320. var _emit = _interopRequireDefault(emit);
  17321. var _controller = _interopRequireDefault(controller);
  17322. var _canvasElement = _interopRequireDefault(canvasElement);
  17323. var _container = _interopRequireDefault(container);
  17324. var _group = _interopRequireDefault(group$2);
  17325. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  17326. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  17327. var Canvas = /*#__PURE__*/function (_EventEmit) {
  17328. _inheritsLoose(Canvas, _EventEmit);
  17329. var _proto = Canvas.prototype;
  17330. _proto.get = function get(name) {
  17331. return this._attrs[name];
  17332. };
  17333. _proto.set = function set(name, value) {
  17334. this._attrs[name] = value;
  17335. };
  17336. function Canvas(cfg) {
  17337. var _this;
  17338. _this = _EventEmit.call(this) || this;
  17339. _this._attrs = (0, common.mix)({
  17340. type: 'canvas',
  17341. children: []
  17342. }, cfg);
  17343. _this._initPixelRatio();
  17344. _this._initCanvas();
  17345. return _this;
  17346. }
  17347. _proto._initPixelRatio = function _initPixelRatio() {
  17348. var pixelRatio = this.get('pixelRatio');
  17349. if (!pixelRatio) {
  17350. this.set('pixelRatio', (0, common.getPixelRatio)());
  17351. }
  17352. };
  17353. _proto.beforeDraw = function beforeDraw() {
  17354. var context = this._attrs.context;
  17355. var el = this._attrs.el;
  17356. context && context.clearRect && context.clearRect(0, 0, el.width, el.height);
  17357. };
  17358. _proto._initCanvas = function _initCanvas() {
  17359. var self = this;
  17360. var el = self.get('el');
  17361. var context = self.get('context');
  17362. if (!el && !context) {
  17363. throw new Error('Please specify the id, el or context of the chart!');
  17364. }
  17365. var canvas;
  17366. if (el) {
  17367. // DOMElement or String
  17368. canvas = (0, common.isString)(el) ? (0, common.getDomById)(el) : el;
  17369. } else {
  17370. // 说明没有指定el
  17371. canvas = _canvasElement["default"].create(context);
  17372. }
  17373. if (context && canvas && !canvas.getContext) {
  17374. canvas.getContext = function () {
  17375. return context;
  17376. };
  17377. }
  17378. var width = self.get('width');
  17379. if (!width) {
  17380. width = (0, common.getWidth)(canvas);
  17381. }
  17382. var height = self.get('height');
  17383. if (!height) {
  17384. height = (0, common.getHeight)(canvas);
  17385. }
  17386. self.set('canvas', this);
  17387. self.set('el', canvas);
  17388. self.set('context', context || canvas.getContext('2d'));
  17389. self.changeSize(width, height); // 初始化事件控制器
  17390. var eventController = new _controller["default"]({
  17391. canvas: this,
  17392. el: canvas
  17393. });
  17394. self.set('eventController', eventController);
  17395. };
  17396. _proto.changeSize = function changeSize(width, height) {
  17397. var pixelRatio = this.get('pixelRatio');
  17398. var canvasDOM = this.get('el'); // HTMLCanvasElement or canvasElement
  17399. // 浏览器环境设置style样式
  17400. if (canvasDOM.style) {
  17401. canvasDOM.style.width = width + 'px';
  17402. canvasDOM.style.height = height + 'px';
  17403. }
  17404. if ((0, common.isCanvasElement)(canvasDOM)) {
  17405. canvasDOM.width = width * pixelRatio;
  17406. canvasDOM.height = height * pixelRatio;
  17407. if (pixelRatio !== 1) {
  17408. var ctx = this.get('context');
  17409. ctx.scale(pixelRatio, pixelRatio);
  17410. }
  17411. }
  17412. this.set('width', width);
  17413. this.set('height', height);
  17414. };
  17415. _proto.getWidth = function getWidth() {
  17416. var pixelRatio = this.get('pixelRatio');
  17417. var width = this.get('width');
  17418. return width * pixelRatio;
  17419. };
  17420. _proto.getHeight = function getHeight() {
  17421. var pixelRatio = this.get('pixelRatio');
  17422. var height = this.get('height');
  17423. return height * pixelRatio;
  17424. };
  17425. _proto.getPointByClient = function getPointByClient(clientX, clientY) {
  17426. var el = this.get('el');
  17427. var bbox = el.getBoundingClientRect();
  17428. var width = bbox.right - bbox.left;
  17429. var height = bbox.bottom - bbox.top;
  17430. return {
  17431. x: (clientX - bbox.left) * (el.width / width),
  17432. y: (clientY - bbox.top) * (el.height / height)
  17433. };
  17434. };
  17435. _proto._beginDraw = function _beginDraw() {
  17436. this._attrs.toDraw = true;
  17437. };
  17438. _proto._endDraw = function _endDraw() {
  17439. this._attrs.toDraw = false;
  17440. };
  17441. _proto.draw = function draw() {
  17442. var self = this;
  17443. function drawInner() {
  17444. self.set('animateHandler', (0, requestAnimationFrame_1.requestAnimationFrame)(function () {
  17445. self.set('animateHandler', undefined);
  17446. if (self.get('toDraw')) {
  17447. drawInner();
  17448. }
  17449. }));
  17450. self.beforeDraw();
  17451. try {
  17452. var context = self._attrs.context;
  17453. self.drawInner(context); // 支付宝,微信小程序,需要调context.draw才能完成绘制, 所以这里直接判断是否有.draw方法
  17454. if (context.draw) {
  17455. context.draw();
  17456. }
  17457. } catch (ev) {
  17458. console.warn('error in draw canvas, detail as:');
  17459. console.warn(ev);
  17460. self._endDraw();
  17461. }
  17462. self._endDraw();
  17463. }
  17464. if (self.get('destroyed')) {
  17465. return;
  17466. }
  17467. if (self.get('animateHandler')) {
  17468. this._beginDraw();
  17469. } else {
  17470. drawInner();
  17471. }
  17472. };
  17473. _proto.destroy = function destroy() {
  17474. if (this.get('destroyed')) {
  17475. return;
  17476. } // 需要清理 canvas 画布内容,否则会导致 spa 应用 ios 下 canvas 白屏
  17477. // https://stackoverflow.com/questions/52532614/total-canvas-memory-use-exceeds-the-maximum-limit-safari-12
  17478. // https://github.com/antvis/F2/issues/630
  17479. var el = this.get('el');
  17480. el.width = 0;
  17481. el.height = 0;
  17482. this.clear();
  17483. this._attrs = {};
  17484. this.set('destroyed', true);
  17485. };
  17486. _proto.isDestroyed = function isDestroyed() {
  17487. return this.get('destroyed');
  17488. };
  17489. return Canvas;
  17490. }(_emit["default"]);
  17491. (0, common.mix)(Canvas.prototype, _container["default"], {
  17492. getGroupClass: function getGroupClass() {
  17493. return _group["default"];
  17494. }
  17495. });
  17496. var _default = Canvas;
  17497. exports["default"] = _default;
  17498. });
  17499. unwrapExports(canvas);
  17500. var rect = createCommonjsModule(function (module, exports) {
  17501. exports.__esModule = true;
  17502. exports["default"] = void 0;
  17503. var _shape = _interopRequireDefault(shape);
  17504. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  17505. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  17506. // 为了处理radius 大于 width 或 height 的场景
  17507. function parseRadius(radius, width, height) {
  17508. radius = (0, common.parsePadding)(radius); // 都为0
  17509. if (!radius[0] && !radius[1] && !radius[2] && !radius[3]) {
  17510. return radius;
  17511. }
  17512. var minWidth = Math.max(radius[0] + radius[1], radius[2] + radius[3]);
  17513. var minHeight = Math.max(radius[0] + radius[3], radius[1] + radius[2]);
  17514. var scale = Math.min(width / minWidth, height / minHeight);
  17515. if (scale < 1) {
  17516. return radius.map(function (r) {
  17517. return r * scale;
  17518. });
  17519. }
  17520. return radius;
  17521. }
  17522. var Rect = /*#__PURE__*/function (_Shape) {
  17523. _inheritsLoose(Rect, _Shape);
  17524. function Rect() {
  17525. return _Shape.apply(this, arguments) || this;
  17526. }
  17527. var _proto = Rect.prototype;
  17528. _proto._initProperties = function _initProperties() {
  17529. _Shape.prototype._initProperties.call(this);
  17530. this._attrs.canFill = true;
  17531. this._attrs.canStroke = true;
  17532. this._attrs.type = 'rect';
  17533. };
  17534. _proto.getDefaultAttrs = function getDefaultAttrs() {
  17535. return {
  17536. x: 0,
  17537. y: 0,
  17538. width: 0,
  17539. height: 0,
  17540. radius: 0,
  17541. lineWidth: 0
  17542. };
  17543. };
  17544. _proto.createPath = function createPath(context) {
  17545. var self = this;
  17546. var attrs = self.get('attrs');
  17547. var x = attrs.x,
  17548. y = attrs.y,
  17549. width = attrs.width,
  17550. height = attrs.height,
  17551. radius = attrs.radius;
  17552. context.beginPath();
  17553. if (!radius || !(width * height)) {
  17554. context.rect(x, y, width, height);
  17555. } else {
  17556. radius = parseRadius(radius, width, height);
  17557. context.moveTo(x + radius[0], y);
  17558. context.lineTo(x + width - radius[1], y);
  17559. context.arc(x + width - radius[1], y + radius[1], radius[1], -Math.PI / 2, 0, false);
  17560. context.lineTo(x + width, y + height - radius[2]);
  17561. context.arc(x + width - radius[2], y + height - radius[2], radius[2], 0, Math.PI / 2, false);
  17562. context.lineTo(x + radius[3], y + height);
  17563. context.arc(x + radius[3], y + height - radius[3], radius[3], Math.PI / 2, Math.PI, false);
  17564. context.lineTo(x, y + radius[0]);
  17565. context.arc(x + radius[0], y + radius[0], radius[0], Math.PI, Math.PI * 3 / 2, false);
  17566. context.closePath();
  17567. }
  17568. };
  17569. _proto.calculateBox = function calculateBox() {
  17570. var attrs = this.get('attrs');
  17571. var x = attrs.x,
  17572. y = attrs.y,
  17573. width = attrs.width,
  17574. height = attrs.height;
  17575. return {
  17576. minX: x,
  17577. minY: y,
  17578. maxX: x + width,
  17579. maxY: y + height
  17580. };
  17581. };
  17582. return Rect;
  17583. }(_shape["default"]);
  17584. _shape["default"].Rect = Rect;
  17585. var _default = Rect;
  17586. exports["default"] = _default;
  17587. });
  17588. unwrapExports(rect);
  17589. var image = createCommonjsModule(function (module, exports) {
  17590. exports.__esModule = true;
  17591. exports["default"] = void 0;
  17592. var _shape = _interopRequireDefault(shape);
  17593. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  17594. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  17595. var ImageShape = /*#__PURE__*/function (_Shape) {
  17596. _inheritsLoose(ImageShape, _Shape);
  17597. function ImageShape() {
  17598. return _Shape.apply(this, arguments) || this;
  17599. }
  17600. var _proto = ImageShape.prototype;
  17601. _proto._initProperties = function _initProperties() {
  17602. _Shape.prototype._initProperties.call(this);
  17603. this._attrs.canFill = false;
  17604. this._attrs.canStroke = false;
  17605. this._attrs.loading = false;
  17606. this._attrs.image = null;
  17607. this._attrs.type = 'image';
  17608. };
  17609. _proto.getDefaultAttrs = function getDefaultAttrs() {
  17610. return {
  17611. x: 0,
  17612. y: 0,
  17613. width: 0,
  17614. height: 0
  17615. };
  17616. };
  17617. _proto.createPath = function createPath(context) {
  17618. var _this = this;
  17619. var attrs = this.get('attrs');
  17620. var src = attrs.src;
  17621. if (this.get('loading')) {
  17622. return;
  17623. }
  17624. var image = this.get('image');
  17625. if (image) {
  17626. this.drawImage(context, image);
  17627. } else {
  17628. if (src && Image) {
  17629. this.set('loading', true);
  17630. var _image = new Image();
  17631. _image.src = src; // 设置跨域
  17632. _image.crossOrigin = 'Anonymous';
  17633. _image.onload = function () {
  17634. _this.set('loading', false);
  17635. _this.set('image', _image);
  17636. _this.drawImage(context, _image);
  17637. };
  17638. }
  17639. }
  17640. };
  17641. _proto.drawImage = function drawImage(context, image) {
  17642. var attrs = this.get('attrs');
  17643. var x = attrs.x,
  17644. y = attrs.y,
  17645. width = attrs.width,
  17646. height = attrs.height,
  17647. sx = attrs.sx,
  17648. sy = attrs.sy,
  17649. swidth = attrs.swidth,
  17650. sheight = attrs.sheight;
  17651. if (!(0, common.isNil)(sx) && !(0, common.isNil)(sy) && !(0, common.isNil)(swidth) && !(0, common.isNil)(sheight)) {
  17652. context.drawImage(image, sx, sy, swidth, sheight, x, y, width, height);
  17653. } else {
  17654. context.drawImage(image, x, y, width, height);
  17655. }
  17656. };
  17657. _proto.calculateBox = function calculateBox() {
  17658. var attrs = this.get('attrs');
  17659. var x = attrs.x,
  17660. y = attrs.y,
  17661. width = attrs.width,
  17662. height = attrs.height; // 和rect一样
  17663. return {
  17664. minX: x,
  17665. minY: y,
  17666. maxX: x + width,
  17667. maxY: y + height
  17668. };
  17669. };
  17670. return ImageShape;
  17671. }(_shape["default"]);
  17672. _shape["default"].Image = ImageShape;
  17673. var _default = ImageShape;
  17674. exports["default"] = _default;
  17675. });
  17676. unwrapExports(image);
  17677. var circle = createCommonjsModule(function (module, exports) {
  17678. exports.__esModule = true;
  17679. exports["default"] = void 0;
  17680. var _shape = _interopRequireDefault(shape);
  17681. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  17682. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  17683. var Circle = /*#__PURE__*/function (_Shape) {
  17684. _inheritsLoose(Circle, _Shape);
  17685. function Circle() {
  17686. return _Shape.apply(this, arguments) || this;
  17687. }
  17688. var _proto = Circle.prototype;
  17689. _proto._initProperties = function _initProperties() {
  17690. _Shape.prototype._initProperties.call(this);
  17691. this._attrs.canFill = true;
  17692. this._attrs.canStroke = true;
  17693. this._attrs.type = 'circle';
  17694. };
  17695. _proto.getDefaultAttrs = function getDefaultAttrs() {
  17696. return {
  17697. x: 0,
  17698. y: 0,
  17699. r: 0,
  17700. lineWidth: 0
  17701. };
  17702. };
  17703. _proto.createPath = function createPath(context) {
  17704. var attrs = this.get('attrs');
  17705. var x = attrs.x,
  17706. y = attrs.y,
  17707. r = attrs.r;
  17708. context.beginPath();
  17709. context.arc(x, y, r, 0, Math.PI * 2, false);
  17710. context.closePath();
  17711. };
  17712. _proto.calculateBox = function calculateBox() {
  17713. var attrs = this.get('attrs');
  17714. var x = attrs.x,
  17715. y = attrs.y,
  17716. r = attrs.r;
  17717. return {
  17718. minX: x - r,
  17719. maxX: x + r,
  17720. minY: y - r,
  17721. maxY: y + r
  17722. };
  17723. };
  17724. return Circle;
  17725. }(_shape["default"]);
  17726. _shape["default"].Circle = Circle;
  17727. var _default = Circle;
  17728. exports["default"] = _default;
  17729. });
  17730. unwrapExports(circle);
  17731. var bbox = createCommonjsModule(function (module, exports) {
  17732. exports.__esModule = true;
  17733. exports.getBBoxFromPoints = getBBoxFromPoints;
  17734. exports.getBBoxFromLine = getBBoxFromLine;
  17735. exports.getBBoxFromArc = getBBoxFromArc;
  17736. exports.getBBoxFromBezierGroup = getBBoxFromBezierGroup;
  17737. var _vector = _interopRequireDefault(vector2);
  17738. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  17739. var start = _vector["default"].create();
  17740. var end = _vector["default"].create();
  17741. var extremity = _vector["default"].create();
  17742. function getCubicBezierXYatT(startPt, controlPt1, controlPt2, endPt, T) {
  17743. var x = CubicN(T, startPt.x, controlPt1.x, controlPt2.x, endPt.x);
  17744. var y = CubicN(T, startPt.y, controlPt1.y, controlPt2.y, endPt.y);
  17745. return {
  17746. x: x,
  17747. y: y
  17748. };
  17749. } // cubic helper formula at T distance
  17750. function CubicN(T, a, b, c, d) {
  17751. var t2 = T * T;
  17752. var t3 = t2 * T;
  17753. return a + (-a * 3 + T * (3 * a - a * T)) * T + (3 * b + T * (-6 * b + b * 3 * T)) * T + (c * 3 - c * 3 * T) * t2 + d * t3;
  17754. }
  17755. function cubicBezierBounds(c) {
  17756. var minX = Infinity;
  17757. var maxX = -Infinity;
  17758. var minY = Infinity;
  17759. var maxY = -Infinity;
  17760. var s = {
  17761. x: c[0],
  17762. y: c[1]
  17763. };
  17764. var c1 = {
  17765. x: c[2],
  17766. y: c[3]
  17767. };
  17768. var c2 = {
  17769. x: c[4],
  17770. y: c[5]
  17771. };
  17772. var e = {
  17773. x: c[6],
  17774. y: c[7]
  17775. };
  17776. for (var t = 0; t < 100; t++) {
  17777. var pt = getCubicBezierXYatT(s, c1, c2, e, t / 100);
  17778. if (pt.x < minX) {
  17779. minX = pt.x;
  17780. }
  17781. if (pt.x > maxX) {
  17782. maxX = pt.x;
  17783. }
  17784. if (pt.y < minY) {
  17785. minY = pt.y;
  17786. }
  17787. if (pt.y > maxY) {
  17788. maxY = pt.y;
  17789. }
  17790. }
  17791. return {
  17792. minX: minX,
  17793. minY: minY,
  17794. maxX: maxX,
  17795. maxY: maxY
  17796. };
  17797. }
  17798. function getBBoxFromPoints(points, lineWidth) {
  17799. if (points.length === 0) {
  17800. return;
  17801. }
  17802. var p = points[0];
  17803. var left = p.x;
  17804. var right = p.x;
  17805. var top = p.y;
  17806. var bottom = p.y;
  17807. var len = points.length;
  17808. for (var i = 1; i < len; i++) {
  17809. p = points[i];
  17810. left = Math.min(left, p.x);
  17811. right = Math.max(right, p.x);
  17812. top = Math.min(top, p.y);
  17813. bottom = Math.max(bottom, p.y);
  17814. }
  17815. lineWidth = lineWidth / 2 || 0;
  17816. return {
  17817. minX: left - lineWidth,
  17818. minY: top - lineWidth,
  17819. maxX: right + lineWidth,
  17820. maxY: bottom + lineWidth
  17821. };
  17822. }
  17823. function getBBoxFromLine(x0, y0, x1, y1, lineWidth) {
  17824. lineWidth = lineWidth / 2 || 0;
  17825. return {
  17826. minX: Math.min(x0, x1) - lineWidth,
  17827. minY: Math.min(y0, y1) - lineWidth,
  17828. maxX: Math.max(x0, x1) + lineWidth,
  17829. maxY: Math.max(y0, y1) + lineWidth
  17830. };
  17831. }
  17832. function getBBoxFromArc(x, y, r, startAngle, endAngle, anticlockwise) {
  17833. var diff = Math.abs(startAngle - endAngle);
  17834. if (diff % (Math.PI * 2) < 1e-4 && diff > 1e-4) {
  17835. // Is a circle
  17836. return {
  17837. minX: x - r,
  17838. minY: y - r,
  17839. maxX: x + r,
  17840. maxY: y + r
  17841. };
  17842. }
  17843. start[0] = Math.cos(startAngle) * r + x;
  17844. start[1] = Math.sin(startAngle) * r + y;
  17845. end[0] = Math.cos(endAngle) * r + x;
  17846. end[1] = Math.sin(endAngle) * r + y;
  17847. var min = [0, 0];
  17848. var max = [0, 0];
  17849. _vector["default"].min(min, start, end);
  17850. _vector["default"].max(max, start, end); // Thresh to [0, Math.PI * 2]
  17851. startAngle = startAngle % (Math.PI * 2);
  17852. if (startAngle < 0) {
  17853. startAngle = startAngle + Math.PI * 2;
  17854. }
  17855. endAngle = endAngle % (Math.PI * 2);
  17856. if (endAngle < 0) {
  17857. endAngle = endAngle + Math.PI * 2;
  17858. }
  17859. if (startAngle > endAngle && !anticlockwise) {
  17860. endAngle += Math.PI * 2;
  17861. } else if (startAngle < endAngle && anticlockwise) {
  17862. startAngle += Math.PI * 2;
  17863. }
  17864. if (anticlockwise) {
  17865. var tmp = endAngle;
  17866. endAngle = startAngle;
  17867. startAngle = tmp;
  17868. }
  17869. for (var angle = 0; angle < endAngle; angle += Math.PI / 2) {
  17870. if (angle > startAngle) {
  17871. extremity[0] = Math.cos(angle) * r + x;
  17872. extremity[1] = Math.sin(angle) * r + y;
  17873. _vector["default"].min(min, extremity, min);
  17874. _vector["default"].max(max, extremity, max);
  17875. }
  17876. }
  17877. return {
  17878. minX: min[0],
  17879. minY: min[1],
  17880. maxX: max[0],
  17881. maxY: max[1]
  17882. };
  17883. }
  17884. function getBBoxFromBezierGroup(points, lineWidth) {
  17885. var minX = Infinity;
  17886. var maxX = -Infinity;
  17887. var minY = Infinity;
  17888. var maxY = -Infinity;
  17889. for (var i = 0, len = points.length; i < len; i++) {
  17890. var bbox = cubicBezierBounds(points[i]);
  17891. if (bbox.minX < minX) {
  17892. minX = bbox.minX;
  17893. }
  17894. if (bbox.maxX > maxX) {
  17895. maxX = bbox.maxX;
  17896. }
  17897. if (bbox.minY < minY) {
  17898. minY = bbox.minY;
  17899. }
  17900. if (bbox.maxY > maxY) {
  17901. maxY = bbox.maxY;
  17902. }
  17903. }
  17904. lineWidth = lineWidth / 2 || 0;
  17905. return {
  17906. minX: minX - lineWidth,
  17907. minY: minY - lineWidth,
  17908. maxX: maxX + lineWidth,
  17909. maxY: maxY + lineWidth
  17910. };
  17911. }
  17912. });
  17913. unwrapExports(bbox);
  17914. var bbox_1 = bbox.getBBoxFromPoints;
  17915. var bbox_2 = bbox.getBBoxFromLine;
  17916. var bbox_3 = bbox.getBBoxFromArc;
  17917. var bbox_4 = bbox.getBBoxFromBezierGroup;
  17918. var line = createCommonjsModule(function (module, exports) {
  17919. exports.__esModule = true;
  17920. exports["default"] = void 0;
  17921. var _shape = _interopRequireDefault(shape);
  17922. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  17923. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  17924. var Line = /*#__PURE__*/function (_Shape) {
  17925. _inheritsLoose(Line, _Shape);
  17926. function Line() {
  17927. return _Shape.apply(this, arguments) || this;
  17928. }
  17929. var _proto = Line.prototype;
  17930. _proto._initProperties = function _initProperties() {
  17931. _Shape.prototype._initProperties.call(this);
  17932. this._attrs.canStroke = true;
  17933. this._attrs.type = 'line';
  17934. };
  17935. _proto.getDefaultAttrs = function getDefaultAttrs() {
  17936. return {
  17937. x1: 0,
  17938. y1: 0,
  17939. x2: 0,
  17940. y2: 0,
  17941. lineWidth: 1
  17942. };
  17943. };
  17944. _proto.createPath = function createPath(context) {
  17945. var attrs = this.get('attrs');
  17946. var x1 = attrs.x1,
  17947. y1 = attrs.y1,
  17948. x2 = attrs.x2,
  17949. y2 = attrs.y2;
  17950. context.beginPath();
  17951. context.moveTo(x1, y1);
  17952. context.lineTo(x2, y2);
  17953. };
  17954. _proto.calculateBox = function calculateBox() {
  17955. var attrs = this.get('attrs');
  17956. var x1 = attrs.x1,
  17957. y1 = attrs.y1,
  17958. x2 = attrs.x2,
  17959. y2 = attrs.y2,
  17960. lineWidth = attrs.lineWidth;
  17961. return (0, bbox.getBBoxFromLine)(x1, y1, x2, y2, lineWidth);
  17962. };
  17963. return Line;
  17964. }(_shape["default"]);
  17965. _shape["default"].Line = Line;
  17966. var _default = Line;
  17967. exports["default"] = _default;
  17968. });
  17969. unwrapExports(line);
  17970. var polygon = createCommonjsModule(function (module, exports) {
  17971. exports.__esModule = true;
  17972. exports["default"] = void 0;
  17973. var _shape = _interopRequireDefault(shape);
  17974. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  17975. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  17976. var Polygon = /*#__PURE__*/function (_Shape) {
  17977. _inheritsLoose(Polygon, _Shape);
  17978. function Polygon() {
  17979. return _Shape.apply(this, arguments) || this;
  17980. }
  17981. var _proto = Polygon.prototype;
  17982. _proto._initProperties = function _initProperties() {
  17983. _Shape.prototype._initProperties.call(this);
  17984. this._attrs.canFill = true;
  17985. this._attrs.canStroke = true;
  17986. this._attrs.type = 'polygon';
  17987. };
  17988. _proto.getDefaultAttrs = function getDefaultAttrs() {
  17989. return {
  17990. points: null,
  17991. lineWidth: 0
  17992. };
  17993. };
  17994. _proto.createPath = function createPath(context) {
  17995. var self = this;
  17996. var attrs = self.get('attrs');
  17997. var points = attrs.points;
  17998. context.beginPath();
  17999. for (var i = 0, len = points.length; i < len; i++) {
  18000. var point = points[i];
  18001. if (i === 0) {
  18002. context.moveTo(point.x, point.y);
  18003. } else {
  18004. context.lineTo(point.x, point.y);
  18005. }
  18006. }
  18007. context.closePath();
  18008. };
  18009. _proto.calculateBox = function calculateBox() {
  18010. var attrs = this.get('attrs');
  18011. var points = attrs.points;
  18012. return (0, bbox.getBBoxFromPoints)(points);
  18013. };
  18014. return Polygon;
  18015. }(_shape["default"]);
  18016. _shape["default"].Polygon = Polygon;
  18017. var _default = Polygon;
  18018. exports["default"] = _default;
  18019. });
  18020. unwrapExports(polygon);
  18021. var smooth = createCommonjsModule(function (module, exports) {
  18022. exports.__esModule = true;
  18023. exports.smooth = catmullRom2bezier;
  18024. var _vector = _interopRequireDefault(vector2);
  18025. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  18026. /**
  18027. * @fileOverview convert the line to curve
  18028. * @author dxq613@gmail.com
  18029. */
  18030. function getPoint(v) {
  18031. return [v.x, v.y];
  18032. }
  18033. function smoothBezier(points, smooth, isLoop, constraint) {
  18034. var cps = [];
  18035. var prevPoint;
  18036. var nextPoint;
  18037. var hasConstraint = !!constraint;
  18038. var min;
  18039. var max;
  18040. var point;
  18041. var len;
  18042. var l;
  18043. var i;
  18044. if (hasConstraint) {
  18045. min = [Infinity, Infinity];
  18046. max = [-Infinity, -Infinity];
  18047. for (i = 0, l = points.length; i < l; i++) {
  18048. point = getPoint(points[i]);
  18049. _vector["default"].min(min, min, point);
  18050. _vector["default"].max(max, max, point);
  18051. }
  18052. _vector["default"].min(min, min, constraint[0]);
  18053. _vector["default"].max(max, max, constraint[1]);
  18054. }
  18055. for (i = 0, len = points.length; i < len; i++) {
  18056. point = getPoint(points[i]);
  18057. if (isLoop) {
  18058. prevPoint = getPoint(points[i ? i - 1 : len - 1]);
  18059. nextPoint = getPoint(points[(i + 1) % len]);
  18060. } else {
  18061. if (i === 0 || i === len - 1) {
  18062. cps.push([point[0], point[1]]);
  18063. continue;
  18064. } else {
  18065. prevPoint = getPoint(points[i - 1]);
  18066. nextPoint = getPoint(points[i + 1]);
  18067. }
  18068. }
  18069. var v = _vector["default"].sub([], nextPoint, prevPoint);
  18070. _vector["default"].scale(v, v, smooth);
  18071. var d0 = _vector["default"].distance(point, prevPoint);
  18072. var d1 = _vector["default"].distance(point, nextPoint);
  18073. var sum = d0 + d1;
  18074. if (sum !== 0) {
  18075. d0 /= sum;
  18076. d1 /= sum;
  18077. }
  18078. var v1 = _vector["default"].scale([], v, -d0);
  18079. var v2 = _vector["default"].scale([], v, d1);
  18080. var cp0 = _vector["default"].add([], point, v1);
  18081. var cp1 = _vector["default"].add([], point, v2);
  18082. if (hasConstraint) {
  18083. _vector["default"].max(cp0, cp0, min);
  18084. _vector["default"].min(cp0, cp0, max);
  18085. _vector["default"].max(cp1, cp1, min);
  18086. _vector["default"].min(cp1, cp1, max);
  18087. }
  18088. cps.push([cp0[0], cp0[1]]);
  18089. cps.push([cp1[0], cp1[1]]);
  18090. }
  18091. if (isLoop) {
  18092. cps.push(cps.shift());
  18093. }
  18094. return cps;
  18095. }
  18096. function catmullRom2bezier(pointList, z, constraint) {
  18097. var isLoop = !!z;
  18098. var controlPointList = smoothBezier(pointList, 0.4, isLoop, constraint);
  18099. var len = pointList.length;
  18100. var d1 = [];
  18101. var cp1;
  18102. var cp2;
  18103. var p;
  18104. for (var i = 0; i < len - 1; i++) {
  18105. cp1 = controlPointList[i * 2];
  18106. cp2 = controlPointList[i * 2 + 1];
  18107. p = pointList[i + 1];
  18108. d1.push(['C', cp1[0], cp1[1], cp2[0], cp2[1], p.x, p.y]);
  18109. }
  18110. if (isLoop) {
  18111. cp1 = controlPointList[len];
  18112. cp2 = controlPointList[len + 1];
  18113. p = pointList[0];
  18114. d1.push(['C', cp1[0], cp1[1], cp2[0], cp2[1], p.x, p.y]);
  18115. }
  18116. return d1;
  18117. }
  18118. });
  18119. unwrapExports(smooth);
  18120. var smooth_1 = smooth.smooth;
  18121. var polyline = createCommonjsModule(function (module, exports) {
  18122. exports.__esModule = true;
  18123. exports["default"] = void 0;
  18124. var _shape = _interopRequireDefault(shape);
  18125. var Smooth = _interopRequireWildcard(smooth);
  18126. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
  18127. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  18128. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  18129. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  18130. // filter the point which x or y is NaN
  18131. function _filterPoints(points) {
  18132. var filteredPoints = [];
  18133. for (var i = 0, len = points.length; i < len; i++) {
  18134. var point = points[i];
  18135. if (!isNaN(point.x) && !isNaN(point.y)) {
  18136. filteredPoints.push(point);
  18137. }
  18138. }
  18139. return filteredPoints;
  18140. }
  18141. var Polyline = /*#__PURE__*/function (_Shape) {
  18142. _inheritsLoose(Polyline, _Shape);
  18143. function Polyline() {
  18144. return _Shape.apply(this, arguments) || this;
  18145. }
  18146. var _proto = Polyline.prototype;
  18147. _proto._initProperties = function _initProperties() {
  18148. _Shape.prototype._initProperties.call(this);
  18149. this._attrs.canFill = true;
  18150. this._attrs.canStroke = true;
  18151. this._attrs.type = 'polyline';
  18152. };
  18153. _proto.getDefaultAttrs = function getDefaultAttrs() {
  18154. return {
  18155. points: null,
  18156. lineWidth: 1,
  18157. smooth: false
  18158. };
  18159. };
  18160. _proto.createPath = function createPath(context) {
  18161. var self = this;
  18162. var attrs = self.get('attrs');
  18163. var points = attrs.points,
  18164. smooth = attrs.smooth;
  18165. var filteredPoints = _filterPoints(points);
  18166. context.beginPath();
  18167. if (filteredPoints.length) {
  18168. context.moveTo(filteredPoints[0].x, filteredPoints[0].y);
  18169. if (smooth) {
  18170. var constaint = [[0, 0], [1, 1]];
  18171. var sps = Smooth.smooth(filteredPoints, false, constaint);
  18172. for (var i = 0, n = sps.length; i < n; i++) {
  18173. var sp = sps[i];
  18174. context.bezierCurveTo(sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]);
  18175. }
  18176. } else {
  18177. var _i;
  18178. var l;
  18179. for (_i = 1, l = filteredPoints.length - 1; _i < l; _i++) {
  18180. context.lineTo(filteredPoints[_i].x, filteredPoints[_i].y);
  18181. }
  18182. context.lineTo(filteredPoints[l].x, filteredPoints[l].y);
  18183. }
  18184. }
  18185. };
  18186. _proto.calculateBox = function calculateBox() {
  18187. var attrs = this.get('attrs');
  18188. var points = attrs.points,
  18189. smooth = attrs.smooth,
  18190. lineWidth = attrs.lineWidth;
  18191. var filteredPoints = _filterPoints(points);
  18192. if (smooth) {
  18193. var newPoints = [];
  18194. var constaint = [[0, 0], [1, 1]];
  18195. var sps = Smooth.smooth(filteredPoints, false, constaint);
  18196. for (var i = 0, n = sps.length; i < n; i++) {
  18197. var sp = sps[i];
  18198. if (i === 0) {
  18199. newPoints.push([filteredPoints[0].x, filteredPoints[0].y, sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]]);
  18200. } else {
  18201. var lastPoint = sps[i - 1];
  18202. newPoints.push([lastPoint[5], lastPoint[6], sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]]);
  18203. }
  18204. }
  18205. return (0, bbox.getBBoxFromBezierGroup)(newPoints, lineWidth);
  18206. }
  18207. return (0, bbox.getBBoxFromPoints)(filteredPoints, lineWidth);
  18208. };
  18209. return Polyline;
  18210. }(_shape["default"]);
  18211. _shape["default"].Polyline = Polyline;
  18212. var _default = Polyline;
  18213. exports["default"] = _default;
  18214. });
  18215. unwrapExports(polyline);
  18216. var arc = createCommonjsModule(function (module, exports) {
  18217. exports.__esModule = true;
  18218. exports["default"] = void 0;
  18219. var _shape = _interopRequireDefault(shape);
  18220. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  18221. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  18222. var Arc = /*#__PURE__*/function (_Shape) {
  18223. _inheritsLoose(Arc, _Shape);
  18224. function Arc() {
  18225. return _Shape.apply(this, arguments) || this;
  18226. }
  18227. var _proto = Arc.prototype;
  18228. _proto._initProperties = function _initProperties() {
  18229. _Shape.prototype._initProperties.call(this);
  18230. this._attrs.canStroke = true;
  18231. this._attrs.canFill = true;
  18232. this._attrs.type = 'arc';
  18233. };
  18234. _proto.getDefaultAttrs = function getDefaultAttrs() {
  18235. return {
  18236. x: 0,
  18237. y: 0,
  18238. r: 0,
  18239. startAngle: 0,
  18240. endAngle: Math.PI * 2,
  18241. anticlockwise: false,
  18242. lineWidth: 1
  18243. };
  18244. };
  18245. _proto.createPath = function createPath(context) {
  18246. var attrs = this.get('attrs');
  18247. var x = attrs.x,
  18248. y = attrs.y,
  18249. r = attrs.r,
  18250. startAngle = attrs.startAngle,
  18251. endAngle = attrs.endAngle,
  18252. anticlockwise = attrs.anticlockwise;
  18253. context.beginPath();
  18254. if (startAngle !== endAngle) {
  18255. context.arc(x, y, r, startAngle, endAngle, anticlockwise);
  18256. }
  18257. };
  18258. _proto.calculateBox = function calculateBox() {
  18259. var attrs = this.get('attrs');
  18260. var x = attrs.x,
  18261. y = attrs.y,
  18262. r = attrs.r,
  18263. startAngle = attrs.startAngle,
  18264. endAngle = attrs.endAngle,
  18265. anticlockwise = attrs.anticlockwise;
  18266. return (0, bbox.getBBoxFromArc)(x, y, r, startAngle, endAngle, anticlockwise);
  18267. };
  18268. return Arc;
  18269. }(_shape["default"]);
  18270. _shape["default"].Arc = Arc;
  18271. var _default = Arc;
  18272. exports["default"] = _default;
  18273. });
  18274. unwrapExports(arc);
  18275. var sector = createCommonjsModule(function (module, exports) {
  18276. exports.__esModule = true;
  18277. exports["default"] = void 0;
  18278. var _shape = _interopRequireDefault(shape);
  18279. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  18280. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  18281. var Sector = /*#__PURE__*/function (_Shape) {
  18282. _inheritsLoose(Sector, _Shape);
  18283. function Sector() {
  18284. return _Shape.apply(this, arguments) || this;
  18285. }
  18286. var _proto = Sector.prototype;
  18287. _proto._initProperties = function _initProperties() {
  18288. _Shape.prototype._initProperties.call(this);
  18289. this._attrs.canFill = true;
  18290. this._attrs.canStroke = true;
  18291. this._attrs.type = 'sector';
  18292. };
  18293. _proto.getDefaultAttrs = function getDefaultAttrs() {
  18294. return {
  18295. x: 0,
  18296. y: 0,
  18297. lineWidth: 0,
  18298. r: 0,
  18299. r0: 0,
  18300. startAngle: 0,
  18301. endAngle: Math.PI * 2,
  18302. anticlockwise: false
  18303. };
  18304. };
  18305. _proto.createPath = function createPath(context) {
  18306. var attrs = this.get('attrs');
  18307. var x = attrs.x,
  18308. y = attrs.y,
  18309. startAngle = attrs.startAngle,
  18310. endAngle = attrs.endAngle,
  18311. r = attrs.r,
  18312. r0 = attrs.r0,
  18313. anticlockwise = attrs.anticlockwise;
  18314. context.beginPath();
  18315. var unitX = Math.cos(startAngle);
  18316. var unitY = Math.sin(startAngle);
  18317. context.moveTo(unitX * r0 + x, unitY * r0 + y);
  18318. context.lineTo(unitX * r + x, unitY * r + y); // 当扇形的角度非常小的时候,就不进行弧线的绘制;或者整个只有1个扇形时,会出现end<0的情况不绘制
  18319. if (Math.abs(endAngle - startAngle) > 0.0001 || startAngle === 0 && endAngle < 0) {
  18320. context.arc(x, y, r, startAngle, endAngle, anticlockwise);
  18321. context.lineTo(Math.cos(endAngle) * r0 + x, Math.sin(endAngle) * r0 + y);
  18322. if (r0 !== 0) {
  18323. context.arc(x, y, r0, endAngle, startAngle, !anticlockwise);
  18324. }
  18325. }
  18326. context.closePath();
  18327. };
  18328. _proto.calculateBox = function calculateBox() {
  18329. var attrs = this.get('attrs');
  18330. var x = attrs.x,
  18331. y = attrs.y,
  18332. r = attrs.r,
  18333. r0 = attrs.r0,
  18334. startAngle = attrs.startAngle,
  18335. endAngle = attrs.endAngle,
  18336. anticlockwise = attrs.anticlockwise;
  18337. var outerBBox = (0, bbox.getBBoxFromArc)(x, y, r, startAngle, endAngle, anticlockwise);
  18338. var innerBBox = (0, bbox.getBBoxFromArc)(x, y, r0, startAngle, endAngle, anticlockwise);
  18339. return {
  18340. minX: Math.min(outerBBox.minX, innerBBox.minX),
  18341. minY: Math.min(outerBBox.minY, innerBBox.minY),
  18342. maxX: Math.max(outerBBox.maxX, innerBBox.maxX),
  18343. maxY: Math.max(outerBBox.maxY, innerBBox.maxY)
  18344. };
  18345. };
  18346. return Sector;
  18347. }(_shape["default"]);
  18348. _shape["default"].Sector = Sector;
  18349. var _default = Sector;
  18350. exports["default"] = _default;
  18351. });
  18352. unwrapExports(sector);
  18353. var rect$1 = createCommonjsModule(function (module, exports) {
  18354. exports.__esModule = true;
  18355. exports["default"] = void 0;
  18356. var Rect = {
  18357. calcRotatedBox: function calcRotatedBox(_ref) {
  18358. var width = _ref.width,
  18359. height = _ref.height,
  18360. rotate = _ref.rotate;
  18361. var absRotate = Math.abs(rotate);
  18362. return {
  18363. width: Math.abs(width * Math.cos(absRotate) + height * Math.sin(absRotate)),
  18364. height: Math.abs(height * Math.cos(absRotate) + width * Math.sin(absRotate))
  18365. };
  18366. }
  18367. };
  18368. var _default = Rect;
  18369. exports["default"] = _default;
  18370. });
  18371. unwrapExports(rect$1);
  18372. var text = createCommonjsModule(function (module, exports) {
  18373. exports.__esModule = true;
  18374. exports["default"] = void 0;
  18375. var _shape = _interopRequireDefault(shape);
  18376. var _rect = _interopRequireDefault(rect$1);
  18377. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  18378. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  18379. var textWidthCacheCounter = 0;
  18380. var textWidthCache = {};
  18381. var TEXT_CACHE_MAX = 5000;
  18382. var Text = /*#__PURE__*/function (_Shape) {
  18383. _inheritsLoose(Text, _Shape);
  18384. function Text() {
  18385. return _Shape.apply(this, arguments) || this;
  18386. }
  18387. var _proto = Text.prototype;
  18388. _proto._initProperties = function _initProperties() {
  18389. _Shape.prototype._initProperties.call(this);
  18390. this._attrs.canFill = true;
  18391. this._attrs.canStroke = true;
  18392. this._attrs.type = 'text';
  18393. };
  18394. _proto.getDefaultAttrs = function getDefaultAttrs() {
  18395. return {
  18396. lineWidth: 0,
  18397. lineCount: 1,
  18398. fontSize: 12,
  18399. fontFamily: 'sans-serif',
  18400. fontStyle: 'normal',
  18401. fontWeight: 'normal',
  18402. fontVariant: 'normal',
  18403. textAlign: 'start',
  18404. textBaseline: 'bottom',
  18405. lineHeight: null,
  18406. textArr: null
  18407. };
  18408. };
  18409. _proto._getFontStyle = function _getFontStyle() {
  18410. var attrs = this._attrs.attrs;
  18411. var fontSize = attrs.fontSize,
  18412. fontFamily = attrs.fontFamily,
  18413. fontWeight = attrs.fontWeight,
  18414. fontStyle = attrs.fontStyle,
  18415. fontVariant = attrs.fontVariant;
  18416. return fontStyle + " " + fontVariant + " " + fontWeight + " " + fontSize + "px " + fontFamily;
  18417. };
  18418. _proto._afterAttrsSet = function _afterAttrsSet() {
  18419. var attrs = this._attrs.attrs;
  18420. attrs.font = this._getFontStyle();
  18421. if (attrs.text) {
  18422. var text = attrs.text;
  18423. var textArr = null;
  18424. var lineCount = 1;
  18425. if ((0, common.isString)(text) && text.indexOf('\n') !== -1) {
  18426. textArr = text.split('\n');
  18427. lineCount = textArr.length;
  18428. }
  18429. attrs.lineCount = lineCount;
  18430. attrs.textArr = textArr;
  18431. }
  18432. this.set('attrs', attrs);
  18433. };
  18434. _proto._getTextHeight = function _getTextHeight() {
  18435. var attrs = this._attrs.attrs;
  18436. if (attrs.height) {
  18437. return attrs.height;
  18438. }
  18439. var lineCount = attrs.lineCount;
  18440. var fontSize = attrs.fontSize * 1;
  18441. if (lineCount > 1) {
  18442. var spaceingY = this._getSpaceingY();
  18443. return fontSize * lineCount + spaceingY * (lineCount - 1);
  18444. }
  18445. return fontSize;
  18446. };
  18447. _proto._getSpaceingY = function _getSpaceingY() {
  18448. var attrs = this._attrs.attrs;
  18449. var lineHeight = attrs.lineHeight;
  18450. var fontSize = attrs.fontSize * 1;
  18451. return lineHeight ? lineHeight - fontSize : fontSize * 0.14;
  18452. };
  18453. _proto.drawInner = function drawInner(context) {
  18454. var self = this;
  18455. var attrs = self._attrs.attrs;
  18456. var text = attrs.text;
  18457. var x = attrs.x;
  18458. var y = attrs.y;
  18459. if ((0, common.isNil)(text) || isNaN(x) || isNaN(y)) {
  18460. // text will be 0
  18461. return;
  18462. }
  18463. var textArr = attrs.textArr;
  18464. var fontSize = attrs.fontSize * 1;
  18465. var spaceingY = self._getSpaceingY();
  18466. if (attrs.rotate) {
  18467. // do rotation
  18468. context.translate(x, y);
  18469. context.rotate(attrs.rotate);
  18470. x = 0;
  18471. y = 0;
  18472. }
  18473. var textBaseline = attrs.textBaseline;
  18474. var height;
  18475. if (textArr) {
  18476. height = self._getTextHeight();
  18477. }
  18478. var subY; // context.beginPath();
  18479. if (self.hasFill()) {
  18480. var fillOpacity = attrs.fillOpacity;
  18481. if (!(0, common.isNil)(fillOpacity) && fillOpacity !== 1) {
  18482. context.globalAlpha = fillOpacity;
  18483. }
  18484. if (textArr) {
  18485. for (var i = 0, len = textArr.length; i < len; i++) {
  18486. var subText = textArr[i];
  18487. subY = y + i * (spaceingY + fontSize) - height + fontSize; // bottom;
  18488. if (textBaseline === 'middle') {
  18489. subY += height - fontSize - (height - fontSize) / 2;
  18490. }
  18491. if (textBaseline === 'top') {
  18492. subY += height - fontSize;
  18493. }
  18494. context.fillText(subText, x, subY);
  18495. }
  18496. } else {
  18497. context.fillText(text, x, y);
  18498. }
  18499. }
  18500. if (self.hasStroke()) {
  18501. if (textArr) {
  18502. for (var _i = 0, _len = textArr.length; _i < _len; _i++) {
  18503. var _subText = textArr[_i];
  18504. subY = y + _i * (spaceingY + fontSize) - height + fontSize; // bottom;
  18505. if (textBaseline === 'middle') {
  18506. subY += height - fontSize - (height - fontSize) / 2;
  18507. }
  18508. if (textBaseline === 'top') {
  18509. subY += height - fontSize;
  18510. }
  18511. context.strokeText(_subText, x, subY);
  18512. }
  18513. } else {
  18514. context.strokeText(text, x, y);
  18515. }
  18516. }
  18517. };
  18518. _proto.calculateBox = function calculateBox() {
  18519. var self = this;
  18520. var attrs = self._attrs.attrs;
  18521. var x = attrs.x,
  18522. y = attrs.y,
  18523. textAlign = attrs.textAlign,
  18524. textBaseline = attrs.textBaseline;
  18525. var width = self._getTextWidth(); // attrs.width
  18526. if (!width) {
  18527. return {
  18528. minX: x,
  18529. minY: y,
  18530. maxX: x,
  18531. maxY: y
  18532. };
  18533. }
  18534. var height = self._getTextHeight(); // attrs.height
  18535. if (attrs.rotate) {
  18536. var rotatedBox = _rect["default"].calcRotatedBox({
  18537. width: width,
  18538. height: height,
  18539. rotate: attrs.rotate
  18540. });
  18541. width = rotatedBox.width;
  18542. height = rotatedBox.height;
  18543. }
  18544. var point = {
  18545. x: x,
  18546. y: y - height
  18547. }; // default textAlign: start, textBaseline: bottom
  18548. if (textAlign) {
  18549. if (textAlign === 'end' || textAlign === 'right') {
  18550. point.x -= width;
  18551. } else if (textAlign === 'center') {
  18552. point.x -= width / 2;
  18553. }
  18554. }
  18555. if (textBaseline) {
  18556. if (textBaseline === 'top') {
  18557. point.y += height;
  18558. } else if (textBaseline === 'middle') {
  18559. point.y += height / 2;
  18560. }
  18561. }
  18562. return {
  18563. minX: point.x,
  18564. minY: point.y,
  18565. maxX: point.x + width,
  18566. maxY: point.y + height
  18567. };
  18568. };
  18569. _proto._getTextWidth = function _getTextWidth() {
  18570. var attrs = this._attrs.attrs;
  18571. if (attrs.width) {
  18572. return attrs.width;
  18573. }
  18574. var text = attrs.text;
  18575. var context = this.get('context');
  18576. if ((0, common.isNil)(text)) return undefined;
  18577. var font = attrs.font;
  18578. var textArr = attrs.textArr;
  18579. var key = text + '' + font;
  18580. if (textWidthCache[key]) {
  18581. return textWidthCache[key];
  18582. }
  18583. var width = 0;
  18584. if (textArr) {
  18585. for (var i = 0, length = textArr.length; i < length; i++) {
  18586. var subText = textArr[i];
  18587. width = Math.max(width, (0, common.measureText)(subText, font, context).width);
  18588. }
  18589. } else {
  18590. width = (0, common.measureText)(text, font, context).width;
  18591. }
  18592. if (textWidthCacheCounter > TEXT_CACHE_MAX) {
  18593. textWidthCacheCounter = 0;
  18594. textWidthCache = {};
  18595. }
  18596. textWidthCacheCounter++;
  18597. textWidthCache[key] = width;
  18598. return width;
  18599. };
  18600. return Text;
  18601. }(_shape["default"]);
  18602. _shape["default"].Text = Text;
  18603. var _default = Text;
  18604. exports["default"] = _default;
  18605. });
  18606. unwrapExports(text);
  18607. var custom = createCommonjsModule(function (module, exports) {
  18608. exports.__esModule = true;
  18609. exports["default"] = void 0;
  18610. var _shape = _interopRequireDefault(shape);
  18611. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  18612. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  18613. var Custom = /*#__PURE__*/function (_Shape) {
  18614. _inheritsLoose(Custom, _Shape);
  18615. function Custom() {
  18616. return _Shape.apply(this, arguments) || this;
  18617. }
  18618. var _proto = Custom.prototype;
  18619. _proto._initProperties = function _initProperties() {
  18620. _Shape.prototype._initProperties.call(this);
  18621. this._attrs.canFill = true;
  18622. this._attrs.canStroke = true;
  18623. this._attrs.createPath = null;
  18624. this._attrs.type = 'custom';
  18625. };
  18626. _proto.createPath = function createPath(context) {
  18627. var createPath = this.get('createPath');
  18628. createPath && createPath.call(this, context);
  18629. };
  18630. _proto.calculateBox = function calculateBox() {
  18631. var calculateBox = this.get('calculateBox');
  18632. return calculateBox && calculateBox.call(this);
  18633. };
  18634. return Custom;
  18635. }(_shape["default"]);
  18636. _shape["default"].Custom = Custom;
  18637. var _default = Custom;
  18638. exports["default"] = _default;
  18639. });
  18640. unwrapExports(custom);
  18641. var graphic = createCommonjsModule(function (module, exports) {
  18642. exports.__esModule = true;
  18643. var _canvas = _interopRequireDefault(canvas);
  18644. exports.Canvas = _canvas["default"];
  18645. var _group = _interopRequireDefault(group$2);
  18646. exports.Group = _group["default"];
  18647. var _shape = _interopRequireDefault(shape);
  18648. exports.Shape = _shape["default"];
  18649. var _matrix = _interopRequireDefault(matrix);
  18650. exports.Matrix = _matrix["default"];
  18651. var _vector = _interopRequireDefault(vector2);
  18652. exports.Vector2 = _vector["default"];
  18653. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  18654. });
  18655. unwrapExports(graphic);
  18656. var graphic_1 = graphic.Canvas;
  18657. var graphic_2 = graphic.Group;
  18658. var graphic_3 = graphic.Shape;
  18659. var graphic_4 = graphic.Matrix;
  18660. var graphic_5 = graphic.Vector2;
  18661. var pieLabel = createCommonjsModule(function (module, exports) {
  18662. exports.__esModule = true;
  18663. exports.init = init;
  18664. exports.afterGeomDraw = afterGeomDraw;
  18665. exports.clearInner = clearInner;
  18666. exports["default"] = void 0;
  18667. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  18668. var DEFAULT_CFG = {
  18669. anchorOffset: 5,
  18670. // 锚点的偏移量
  18671. inflectionOffset: 15,
  18672. // 拐点的偏移量
  18673. sidePadding: 20,
  18674. // 文本距离画布四边的距离
  18675. lineHeight: 32,
  18676. // 文本的行高
  18677. adjustOffset: 15,
  18678. // 发生调整时的偏移量
  18679. skipOverlapLabels: false,
  18680. // 是否不展示重叠的文本
  18681. triggerOn: 'touchstart',
  18682. // 点击行为触发的时间类型
  18683. activeShape: false,
  18684. // 当有图形被选中的时候,是否激活图形
  18685. activeStyle: {
  18686. offset: 1,
  18687. appendRadius: 8,
  18688. fillOpacity: 0.5
  18689. },
  18690. label1OffsetY: -1,
  18691. label2OffsetY: 1
  18692. };
  18693. function getEndPoint(center, angle, r) {
  18694. return {
  18695. x: center.x + r * Math.cos(angle),
  18696. y: center.y + r * Math.sin(angle)
  18697. };
  18698. } // 计算中间角度
  18699. function getMiddleAngle(startAngle, endAngle) {
  18700. if (endAngle < startAngle) {
  18701. endAngle += Math.PI * 2;
  18702. }
  18703. return (endAngle + startAngle) / 2;
  18704. } // 判断两个矩形是否相交
  18705. function isOverlap(label1, label2) {
  18706. var label1BBox = label1.getBBox();
  18707. var label2BBox = label2.getBBox();
  18708. return Math.max(label1BBox.minX, label2BBox.minX) <= Math.min(label1BBox.maxX, label2BBox.maxX) && Math.max(label1BBox.minY, label2BBox.minY) <= Math.min(label1BBox.maxY, label2BBox.maxY);
  18709. }
  18710. var controller = /*#__PURE__*/function () {
  18711. function controller(cfg) {
  18712. var _this = this;
  18713. _defineProperty(this, "_handleEvent", function (ev) {
  18714. var self = _this;
  18715. var chart = self.chart,
  18716. drawnLabels = self.drawnLabels,
  18717. pieLabelCfg = self.pieLabelCfg;
  18718. var onClick = pieLabelCfg.onClick,
  18719. activeShape = pieLabelCfg.activeShape;
  18720. var canvasEvent = (0, common.createEvent)(ev, chart);
  18721. var x = canvasEvent.x,
  18722. y = canvasEvent.y; // 查找被点击的 label
  18723. var clickedShape;
  18724. for (var i = 0, len = drawnLabels.length; i < len; i++) {
  18725. var shape = drawnLabels[i];
  18726. var bbox = shape.getBBox(); // 通过最小包围盒来判断击中情况
  18727. if (x >= bbox.minX && x <= bbox.maxX && y >= bbox.minY && y <= bbox.maxY) {
  18728. clickedShape = shape;
  18729. break;
  18730. }
  18731. }
  18732. var pieData = chart.getSnapRecords({
  18733. x: x,
  18734. y: y
  18735. });
  18736. if (clickedShape) {
  18737. canvasEvent.data = clickedShape.get('data');
  18738. } else if (pieData.length) {
  18739. // 击中饼图扇形区域
  18740. canvasEvent.data = pieData[0]._origin;
  18741. }
  18742. onClick && onClick(canvasEvent);
  18743. canvasEvent.data && activeShape && _this._activeShape(canvasEvent.data);
  18744. });
  18745. (0, common.mix)(this, cfg);
  18746. var _chart = this.chart;
  18747. this.canvasDom = _chart.get('canvas').get('el');
  18748. }
  18749. var _proto = controller.prototype;
  18750. _proto.renderLabels = function renderLabels() {
  18751. var self = this;
  18752. var chart = self.chart,
  18753. pieLabelCfg = self.pieLabelCfg,
  18754. labelGroup = self.labelGroup;
  18755. var halves = [[], // left
  18756. [] // right
  18757. ]; // 存储左右 labels
  18758. var geom = chart.get('geoms')[0];
  18759. var shapes = geom.get('container').get('children');
  18760. var anchorOffset = pieLabelCfg.anchorOffset,
  18761. inflectionOffset = pieLabelCfg.inflectionOffset,
  18762. label1 = pieLabelCfg.label1,
  18763. label2 = pieLabelCfg.label2,
  18764. lineHeight = pieLabelCfg.lineHeight,
  18765. skipOverlapLabels = pieLabelCfg.skipOverlapLabels,
  18766. label1OffsetY = pieLabelCfg.label1OffsetY,
  18767. label2OffsetY = pieLabelCfg.label2OffsetY;
  18768. var coord = chart.get('coord');
  18769. var center = coord.center,
  18770. radius = coord.circleRadius;
  18771. shapes.forEach(function (shape) {
  18772. var _shape$_attrs$attrs = shape._attrs.attrs,
  18773. startAngle = _shape$_attrs$attrs.startAngle,
  18774. endAngle = _shape$_attrs$attrs.endAngle;
  18775. var middleAngle = getMiddleAngle(startAngle, endAngle);
  18776. var anchorPoint = getEndPoint(center, middleAngle, radius + anchorOffset);
  18777. var inflectionPoint = getEndPoint(center, middleAngle, radius + inflectionOffset);
  18778. var origin = shape.get('origin');
  18779. var _origin = origin._origin,
  18780. color = origin.color;
  18781. var label = {
  18782. _anchor: anchorPoint,
  18783. _inflection: inflectionPoint,
  18784. _data: _origin,
  18785. x: inflectionPoint.x,
  18786. y: inflectionPoint.y,
  18787. r: radius + inflectionOffset,
  18788. fill: color
  18789. };
  18790. var textGroup = new graphic.Group({
  18791. context: chart.get('canvas').get('context'),
  18792. // 兼容 node、小程序环境
  18793. data: _origin // 存储原始数据
  18794. });
  18795. var textAttrs = {
  18796. x: 0,
  18797. y: 0,
  18798. fontSize: 12,
  18799. lineHeight: 12,
  18800. fill: '#808080'
  18801. };
  18802. if ((0, common.isFunction)(label1)) {
  18803. textGroup.addShape('Text', {
  18804. attrs: (0, common.mix)({
  18805. textBaseline: 'bottom'
  18806. }, textAttrs, label1(_origin, color)),
  18807. data: _origin,
  18808. // 存储原始数据
  18809. offsetY: label1OffsetY
  18810. });
  18811. }
  18812. if ((0, common.isFunction)(label2)) {
  18813. textGroup.addShape('Text', {
  18814. attrs: (0, common.mix)({
  18815. textBaseline: 'top'
  18816. }, textAttrs, label2(_origin, color)),
  18817. data: _origin,
  18818. // 存储原始数据
  18819. offsetY: label2OffsetY
  18820. });
  18821. }
  18822. label.textGroup = textGroup; // 判断文本的方向
  18823. if (anchorPoint.x < center.x) {
  18824. label._side = 'left';
  18825. halves[0].push(label);
  18826. } else {
  18827. label._side = 'right';
  18828. halves[1].push(label);
  18829. }
  18830. });
  18831. var drawnLabels = [];
  18832. if (skipOverlapLabels) {
  18833. var lastLabel; // 存储上一个 label 对象,用于检测文本是否重叠
  18834. var labels = halves[1].concat(halves[0]); // 顺时针
  18835. for (var i = 0, len = labels.length; i < len; i++) {
  18836. var label = labels[i];
  18837. var textGroup = self._drawLabel(label);
  18838. if (lastLabel) {
  18839. if (isOverlap(textGroup, lastLabel)) {
  18840. // 重叠了就不绘制
  18841. continue;
  18842. }
  18843. }
  18844. labelGroup.add(textGroup);
  18845. self._drawLabelLine(label);
  18846. lastLabel = textGroup;
  18847. drawnLabels.push(textGroup);
  18848. }
  18849. } else {
  18850. var height = chart.get('height');
  18851. var maxCountForOneSide = parseInt(height / lineHeight, 10);
  18852. halves.forEach(function (half) {
  18853. if (half.length > maxCountForOneSide) {
  18854. half.splice(maxCountForOneSide, half.length - maxCountForOneSide);
  18855. }
  18856. half.sort(function (a, b) {
  18857. return a.y - b.y;
  18858. });
  18859. var labels = self._antiCollision(half);
  18860. drawnLabels = drawnLabels.concat(labels);
  18861. });
  18862. }
  18863. this.drawnLabels = drawnLabels;
  18864. };
  18865. _proto.bindEvents = function bindEvents() {
  18866. var pieLabelCfg = this.pieLabelCfg;
  18867. var triggerOn = pieLabelCfg.triggerOn || 'touchstart';
  18868. (0, common.addEventListener)(this.canvasDom, triggerOn, this._handleEvent);
  18869. };
  18870. _proto.unBindEvents = function unBindEvents() {
  18871. var pieLabelCfg = this.pieLabelCfg;
  18872. var triggerOn = pieLabelCfg.triggerOn || 'touchstart';
  18873. (0, common.removeEventListener)(this.canvasDom, triggerOn, this._handleEvent);
  18874. };
  18875. _proto.clear = function clear() {
  18876. this.labelGroup && this.labelGroup.clear();
  18877. this.halo && this.halo.remove(true);
  18878. this.lastSelectedData = null;
  18879. this.drawnLabels = [];
  18880. this.unBindEvents();
  18881. };
  18882. _proto._drawLabel = function _drawLabel(label) {
  18883. var pieLabelCfg = this.pieLabelCfg,
  18884. chart = this.chart;
  18885. var canvasWidth = chart.get('width');
  18886. var sidePadding = pieLabelCfg.sidePadding;
  18887. var y = label.y,
  18888. textGroup = label.textGroup;
  18889. var children = textGroup.get('children');
  18890. var textAttrs = {
  18891. textAlign: label._side === 'left' ? 'left' : 'right',
  18892. x: label._side === 'left' ? sidePadding : canvasWidth - sidePadding
  18893. };
  18894. children.forEach(function (child) {
  18895. child.attr(textAttrs);
  18896. child.attr('y', y + child.get('offsetY'));
  18897. });
  18898. return textGroup;
  18899. };
  18900. _proto._drawLabelLine = function _drawLabelLine(label, maxLabelWidth) {
  18901. var chart = this.chart,
  18902. pieLabelCfg = this.pieLabelCfg,
  18903. labelGroup = this.labelGroup;
  18904. var canvasWidth = chart.get('width');
  18905. var sidePadding = pieLabelCfg.sidePadding,
  18906. adjustOffset = pieLabelCfg.adjustOffset,
  18907. lineStyle = pieLabelCfg.lineStyle,
  18908. anchorStyle = pieLabelCfg.anchorStyle,
  18909. skipOverlapLabels = pieLabelCfg.skipOverlapLabels;
  18910. var _anchor = label._anchor,
  18911. _inflection = label._inflection,
  18912. fill = label.fill,
  18913. y = label.y;
  18914. var lastPoint = {
  18915. x: label._side === 'left' ? sidePadding : canvasWidth - sidePadding,
  18916. y: y
  18917. };
  18918. var points = [_anchor, _inflection, lastPoint];
  18919. if (!skipOverlapLabels && _inflection.y !== y) {
  18920. // 展示全部文本文本位置做过调整
  18921. if (_inflection.y < y) {
  18922. // 文本被调整下去了,则添加拐点连接线
  18923. var point1 = _inflection;
  18924. var point2 = {
  18925. x: label._side === 'left' ? lastPoint.x + maxLabelWidth + adjustOffset : lastPoint.x - maxLabelWidth - adjustOffset,
  18926. y: _inflection.y
  18927. };
  18928. var point3 = {
  18929. x: label._side === 'left' ? lastPoint.x + maxLabelWidth : lastPoint.x - maxLabelWidth,
  18930. y: lastPoint.y
  18931. };
  18932. points = [_anchor, point1, point2, point3, lastPoint];
  18933. if (label._side === 'right' && point2.x < point1.x || label._side === 'left' && point2.x > point1.x) {
  18934. points = [_anchor, point3, lastPoint];
  18935. }
  18936. } else {
  18937. points = [_anchor, {
  18938. x: _inflection.x,
  18939. y: y
  18940. }, lastPoint];
  18941. }
  18942. }
  18943. labelGroup.addShape('Polyline', {
  18944. attrs: (0, common.mix)({
  18945. points: points,
  18946. lineWidth: 1,
  18947. stroke: fill
  18948. }, lineStyle)
  18949. }); // 绘制锚点
  18950. labelGroup.addShape('Circle', {
  18951. attrs: (0, common.mix)({
  18952. x: _anchor.x,
  18953. y: _anchor.y,
  18954. r: 2,
  18955. fill: fill
  18956. }, anchorStyle)
  18957. });
  18958. };
  18959. _proto._antiCollision = function _antiCollision(half) {
  18960. var self = this;
  18961. var chart = self.chart,
  18962. pieLabelCfg = self.pieLabelCfg;
  18963. var coord = chart.get('coord');
  18964. var canvasHeight = chart.get('height');
  18965. var center = coord.center,
  18966. r = coord.circleRadius;
  18967. var inflectionOffset = pieLabelCfg.inflectionOffset,
  18968. lineHeight = pieLabelCfg.lineHeight;
  18969. var startY = center.y - r - inflectionOffset - lineHeight;
  18970. var overlapping = true;
  18971. var totalH = canvasHeight;
  18972. var i;
  18973. var maxY = 0;
  18974. var minY = Number.MIN_VALUE;
  18975. var maxLabelWidth = 0;
  18976. var boxes = half.map(function (label) {
  18977. var labelY = label.y;
  18978. if (labelY > maxY) {
  18979. maxY = labelY;
  18980. }
  18981. if (labelY < minY) {
  18982. minY = labelY;
  18983. }
  18984. var textGroup = label.textGroup;
  18985. var labelWidth = textGroup.getBBox().width;
  18986. if (labelWidth >= maxLabelWidth) {
  18987. maxLabelWidth = labelWidth;
  18988. }
  18989. return {
  18990. size: lineHeight,
  18991. targets: [labelY - startY]
  18992. };
  18993. });
  18994. if (maxY - startY > totalH) {
  18995. totalH = maxY - startY;
  18996. }
  18997. var iteratorBoxed = function iteratorBoxed(boxes) {
  18998. boxes.forEach(function (box) {
  18999. var target = (Math.min.apply(minY, box.targets) + Math.max.apply(minY, box.targets)) / 2;
  19000. box.pos = Math.min(Math.max(minY, target - box.size / 2), totalH - box.size);
  19001. });
  19002. };
  19003. while (overlapping) {
  19004. iteratorBoxed(boxes); // detect overlapping and join boxes
  19005. overlapping = false;
  19006. i = boxes.length;
  19007. while (i--) {
  19008. if (i > 0) {
  19009. var previousBox = boxes[i - 1];
  19010. var box = boxes[i];
  19011. if (previousBox.pos + previousBox.size > box.pos) {
  19012. // overlapping
  19013. previousBox.size += box.size;
  19014. previousBox.targets = previousBox.targets.concat(box.targets); // overflow, shift up
  19015. if (previousBox.pos + previousBox.size > totalH) {
  19016. previousBox.pos = totalH - previousBox.size;
  19017. }
  19018. boxes.splice(i, 1); // removing box
  19019. overlapping = true;
  19020. }
  19021. }
  19022. }
  19023. }
  19024. i = 0;
  19025. boxes.forEach(function (b) {
  19026. var posInCompositeBox = startY; // middle of the label
  19027. b.targets.forEach(function () {
  19028. half[i].y = b.pos + posInCompositeBox + lineHeight / 2;
  19029. posInCompositeBox += lineHeight;
  19030. i++;
  19031. });
  19032. });
  19033. var drawnLabels = [];
  19034. half.forEach(function (label) {
  19035. var textGroup = self._drawLabel(label);
  19036. var labelGroup = self.labelGroup;
  19037. labelGroup.add(textGroup);
  19038. self._drawLabelLine(label, maxLabelWidth);
  19039. drawnLabels.push(textGroup);
  19040. });
  19041. return drawnLabels;
  19042. };
  19043. _proto._getSelectedShapeByData = function _getSelectedShapeByData(data) {
  19044. var selectedShape = null;
  19045. var chart = this.chart;
  19046. var geom = chart.get('geoms')[0];
  19047. var container = geom.get('container');
  19048. var children = container.get('children');
  19049. (0, common.each)(children, function (child) {
  19050. if (child.get('isShape') && child.get('className') === geom.get('type')) {
  19051. // get geometry's shape
  19052. var shapeData = child.get('origin')._origin;
  19053. if ((0, common.isObjectValueEqual)(shapeData, data)) {
  19054. selectedShape = child;
  19055. return false;
  19056. }
  19057. }
  19058. });
  19059. return selectedShape;
  19060. };
  19061. _proto._activeShape = function _activeShape(data) {
  19062. var chart = this.chart,
  19063. lastSelectedData = this.lastSelectedData,
  19064. pieLabelCfg = this.pieLabelCfg;
  19065. if (data === lastSelectedData) {
  19066. return;
  19067. }
  19068. this.lastSelectedData = data;
  19069. var activeStyle = pieLabelCfg.activeStyle;
  19070. var selectedShape = this._getSelectedShapeByData(data);
  19071. var _selectedShape$_attrs = selectedShape._attrs.attrs,
  19072. x = _selectedShape$_attrs.x,
  19073. y = _selectedShape$_attrs.y,
  19074. startAngle = _selectedShape$_attrs.startAngle,
  19075. endAngle = _selectedShape$_attrs.endAngle,
  19076. r = _selectedShape$_attrs.r,
  19077. fill = _selectedShape$_attrs.fill;
  19078. var frontPlot = chart.get('frontPlot');
  19079. this.halo && this.halo.remove(true);
  19080. var halo = frontPlot.addShape('sector', {
  19081. attrs: (0, common.mix)({
  19082. x: x,
  19083. y: y,
  19084. r: r + activeStyle.offset + activeStyle.appendRadius,
  19085. r0: r + activeStyle.offset,
  19086. fill: fill,
  19087. startAngle: startAngle,
  19088. endAngle: endAngle
  19089. }, activeStyle)
  19090. });
  19091. this.halo = halo;
  19092. chart.get('canvas').draw();
  19093. };
  19094. return controller;
  19095. }();
  19096. function init(chart) {
  19097. var frontPlot = chart.get('frontPlot');
  19098. var labelGroup = frontPlot.addGroup({
  19099. className: 'pie-label',
  19100. zIndex: 0
  19101. });
  19102. var pieLabelController = new controller({
  19103. chart: chart,
  19104. labelGroup: labelGroup
  19105. });
  19106. chart.set('pieLabelController', pieLabelController);
  19107. chart.pieLabel = function (cfg) {
  19108. cfg = (0, common.deepMix)({}, DEFAULT_CFG, cfg);
  19109. pieLabelController.pieLabelCfg = cfg;
  19110. return this;
  19111. };
  19112. }
  19113. function afterGeomDraw(chart) {
  19114. var controller = chart.get('pieLabelController');
  19115. if (controller.pieLabelCfg) {
  19116. // 用户配置了饼图文本
  19117. controller.renderLabels();
  19118. controller.bindEvents(); // 绑定事件
  19119. }
  19120. }
  19121. function clearInner(chart) {
  19122. var controller = chart.get('pieLabelController');
  19123. if (controller.pieLabelCfg) {
  19124. // 用户配置了饼图文本
  19125. controller.clear();
  19126. }
  19127. }
  19128. var _default = {
  19129. init: init,
  19130. afterGeomDraw: afterGeomDraw,
  19131. clearInner: clearInner
  19132. };
  19133. exports["default"] = _default;
  19134. });
  19135. var PieLabel = unwrapExports(pieLabel);
  19136. var pieLabel_1 = pieLabel.init;
  19137. var pieLabel_2 = pieLabel.afterGeomDraw;
  19138. var pieLabel_3 = pieLabel.clearInner;
  19139. F2.Chart.plugins.register(PieLabel);
  19140. function wrapEvent(e) {
  19141. if (!e) return;
  19142. if (!e.preventDefault) {
  19143. e.preventDefault = function() {};
  19144. }
  19145. return e;
  19146. }
  19147. Component({
  19148. /**
  19149. * 组件的属性列表
  19150. */
  19151. properties: {
  19152. onInit: {
  19153. type: 'Function',
  19154. value: () => {}
  19155. }
  19156. },
  19157. /**
  19158. * 组件的初始数据
  19159. */
  19160. data: {
  19161. },
  19162. ready() {
  19163. const query = wx.createSelectorQuery().in(this);
  19164. query.select('.f2-canvas')
  19165. .fields({
  19166. node: true,
  19167. size: true
  19168. })
  19169. .exec(res => {
  19170. const { node, width, height } = res[0];
  19171. const context = node.getContext('2d');
  19172. const pixelRatio = wx.getSystemInfoSync().pixelRatio;
  19173. // 高清设置
  19174. node.width = width * pixelRatio;
  19175. node.height = height * pixelRatio;
  19176. const config = { context, width, height, pixelRatio };
  19177. const chart = this.data.onInit(F2, config);
  19178. if (chart) {
  19179. this.chart = chart;
  19180. this.canvasEl = chart.get('el');
  19181. }
  19182. });
  19183. },
  19184. /**
  19185. * 组件的方法列表
  19186. */
  19187. methods: {
  19188. touchStart(e) {
  19189. const canvasEl = this.canvasEl;
  19190. if (!canvasEl) {
  19191. return;
  19192. }
  19193. canvasEl.dispatchEvent('touchstart', wrapEvent(e));
  19194. },
  19195. touchMove(e) {
  19196. const canvasEl = this.canvasEl;
  19197. if (!canvasEl) {
  19198. return;
  19199. }
  19200. canvasEl.dispatchEvent('touchmove', wrapEvent(e));
  19201. },
  19202. touchEnd(e) {
  19203. const canvasEl = this.canvasEl;
  19204. if (!canvasEl) {
  19205. return;
  19206. }
  19207. canvasEl.dispatchEvent('touchend', wrapEvent(e));
  19208. }
  19209. }
  19210. });
  19211. })));