f2.js 475 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087
  1. (function (global, factory) {
  2. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  3. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  4. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.F2 = {}));
  5. }(this, (function (exports) { 'use strict';
  6. var isArrayLike = function (value) {
  7. /**
  8. * isArrayLike([1, 2, 3]) => true
  9. * isArrayLike(document.body.children) => true
  10. * isArrayLike('abc') => true
  11. * isArrayLike(Function) => false
  12. */
  13. return value !== null && typeof value !== 'function' && isFinite(value.length);
  14. };
  15. var contains = function (arr, value) {
  16. if (!isArrayLike(arr)) {
  17. return false;
  18. }
  19. return arr.indexOf(value) > -1;
  20. };
  21. var filter = function (arr, func) {
  22. if (!isArrayLike(arr)) {
  23. return arr;
  24. }
  25. var result = [];
  26. for (var index = 0; index < arr.length; index++) {
  27. var value = arr[index];
  28. if (func(value, index)) {
  29. result.push(value);
  30. }
  31. }
  32. return result;
  33. };
  34. var toString = {}.toString;
  35. var isType = function (value, type) {
  36. return toString.call(value) === '[object ' + type + ']';
  37. };
  38. /**
  39. * 是否为函数
  40. * @param {*} fn 对象
  41. * @return {Boolean} 是否函数
  42. */
  43. var isFunction = (function (value) {
  44. return isType(value, 'Function');
  45. });
  46. // isFinite,
  47. var isNil = function (value) {
  48. /**
  49. * isNil(null) => true
  50. * isNil() => true
  51. */
  52. return value === null || value === undefined;
  53. };
  54. var isArray = (function (value) {
  55. return Array.isArray ? Array.isArray(value) : isType(value, 'Array');
  56. });
  57. var isObject = (function (value) {
  58. /**
  59. * isObject({}) => true
  60. * isObject([1, 2, 3]) => true
  61. * isObject(Function) => true
  62. * isObject(null) => false
  63. */
  64. var type = typeof value;
  65. return value !== null && type === 'object' || type === 'function';
  66. });
  67. function each(elements, func) {
  68. if (!elements) {
  69. return;
  70. }
  71. var rst;
  72. if (isArray(elements)) {
  73. for (var i = 0, len = elements.length; i < len; i++) {
  74. rst = func(elements[i], i);
  75. if (rst === false) {
  76. break;
  77. }
  78. }
  79. } else if (isObject(elements)) {
  80. for (var k in elements) {
  81. if (elements.hasOwnProperty(k)) {
  82. rst = func(elements[k], k);
  83. if (rst === false) {
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. var keys = Object.keys ? function (obj) {
  91. return Object.keys(obj);
  92. } : function (obj) {
  93. var result = [];
  94. each(obj, function (value, key) {
  95. if (!(isFunction(obj) && key === 'prototype')) {
  96. result.push(key);
  97. }
  98. });
  99. return result;
  100. };
  101. function isMatch(obj, attrs) {
  102. var _keys = keys(attrs);
  103. var length = _keys.length;
  104. if (isNil(obj)) return !length;
  105. for (var i = 0; i < length; i += 1) {
  106. var key = _keys[i];
  107. if (attrs[key] !== obj[key] || !(key in obj)) {
  108. return false;
  109. }
  110. }
  111. return true;
  112. }
  113. var isObjectLike = function (value) {
  114. /**
  115. * isObjectLike({}) => true
  116. * isObjectLike([1, 2, 3]) => true
  117. * isObjectLike(Function) => false
  118. * isObjectLike(null) => false
  119. */
  120. return typeof value === 'object' && value !== null;
  121. };
  122. var isPlainObject = function (value) {
  123. /**
  124. * isObjectLike(new Foo) => false
  125. * isObjectLike([1, 2, 3]) => false
  126. * isObjectLike({ x: 0, y: 0 }) => true
  127. * isObjectLike(Object.create(null)) => true
  128. */
  129. if (!isObjectLike(value) || !isType(value, 'Object')) {
  130. return false;
  131. }
  132. if (Object.getPrototypeOf(value) === null) {
  133. return true;
  134. }
  135. var proto = value;
  136. while (Object.getPrototypeOf(proto) !== null) {
  137. proto = Object.getPrototypeOf(proto);
  138. }
  139. return Object.getPrototypeOf(value) === proto;
  140. };
  141. function find(arr, predicate) {
  142. if (!isArray(arr)) return null;
  143. var _predicate;
  144. if (isFunction(predicate)) {
  145. _predicate = predicate;
  146. }
  147. if (isPlainObject(predicate)) {
  148. _predicate = function (a) {
  149. return isMatch(a, predicate);
  150. };
  151. }
  152. if (_predicate) {
  153. for (var i = 0; i < arr.length; i += 1) {
  154. if (_predicate(arr[i])) {
  155. return arr[i];
  156. }
  157. }
  158. }
  159. return null;
  160. }
  161. var getRange = function (values) {
  162. // 存在 NaN 时,min,max 判定会出问题
  163. var filterValues = values.filter(function (v) {
  164. return !isNaN(v);
  165. });
  166. if (!filterValues.length) {
  167. // 如果没有数值则直接返回0
  168. return {
  169. min: 0,
  170. max: 0
  171. };
  172. }
  173. if (isArray(values[0])) {
  174. var tmp = [];
  175. for (var i = 0; i < values.length; i++) {
  176. tmp = tmp.concat(values[i]);
  177. }
  178. filterValues = tmp;
  179. }
  180. var max = Math.max.apply(null, filterValues);
  181. var min = Math.min.apply(null, filterValues);
  182. return {
  183. min: min,
  184. max: max
  185. };
  186. };
  187. var isString = (function (str) {
  188. return isType(str, 'String');
  189. });
  190. var uniq = function (arr) {
  191. var resultArr = [];
  192. each(arr, function (item) {
  193. if (!contains(resultArr, item)) {
  194. resultArr.push(item);
  195. }
  196. });
  197. return resultArr;
  198. };
  199. function head(o) {
  200. if (isArrayLike(o)) {
  201. return o[0];
  202. }
  203. return undefined;
  204. }
  205. function last(o) {
  206. if (isArrayLike(o)) {
  207. var arr = o;
  208. return arr[arr.length - 1];
  209. }
  210. return undefined;
  211. }
  212. var fixedBase = function (v, base) {
  213. var str = base.toString();
  214. var index = str.indexOf('.');
  215. if (index === -1) {
  216. return Math.round(v);
  217. }
  218. var length = str.substr(index + 1).length;
  219. if (length > 20) {
  220. length = 20;
  221. }
  222. return parseFloat(v.toFixed(length));
  223. };
  224. /**
  225. * 判断是否数字
  226. * @return {Boolean} 是否数字
  227. */
  228. var isNumber = function (value) {
  229. return isType(value, 'Number');
  230. };
  231. var toString$1 = (function (value) {
  232. if (isNil(value)) return '';
  233. return value.toString();
  234. });
  235. var lowerFirst = function (value) {
  236. var str = toString$1(value);
  237. return str.charAt(0).toLowerCase() + str.substring(1);
  238. };
  239. function substitute(str, o) {
  240. if (!str || !o) {
  241. return str;
  242. }
  243. return str.replace(/\\?\{([^{}]+)\}/g, function (match, name) {
  244. if (match.charAt(0) === '\\') {
  245. return match.slice(1);
  246. }
  247. return o[name] === undefined ? '' : o[name];
  248. });
  249. }
  250. var upperFirst = function (value) {
  251. var str = toString$1(value);
  252. return str.charAt(0).toUpperCase() + str.substring(1);
  253. };
  254. var toString$2 = {}.toString;
  255. var getType = function (value) {
  256. return toString$2.call(value).replace(/^\[object /, '').replace(/]$/, '');
  257. };
  258. /**
  259. * 是否是布尔类型
  260. *
  261. * @param {Object} value 测试的值
  262. * @return {Boolean}
  263. */
  264. var isBoolean = function (value) {
  265. return isType(value, 'Boolean');
  266. };
  267. var isDate = function (value) {
  268. return isType(value, 'Date');
  269. };
  270. var objectProto = Object.prototype;
  271. var isPrototype = function (value) {
  272. var Ctor = value && value.constructor;
  273. var proto = typeof Ctor === 'function' && Ctor.prototype || objectProto;
  274. return value === proto;
  275. };
  276. // FIXME: Mutable param should be forbidden in static lang.
  277. function _mix(dist, obj) {
  278. for (var key in obj) {
  279. if (obj.hasOwnProperty(key) && key !== 'constructor' && obj[key] !== undefined) {
  280. dist[key] = obj[key];
  281. }
  282. }
  283. }
  284. function mix(dist, src1, src2, src3) {
  285. if (src1) _mix(dist, src1);
  286. if (src2) _mix(dist, src2);
  287. if (src3) _mix(dist, src3);
  288. return dist;
  289. }
  290. var MAX_MIX_LEVEL = 5;
  291. function _deepMix(dist, src, level, maxLevel) {
  292. level = level || 0;
  293. maxLevel = maxLevel || MAX_MIX_LEVEL;
  294. for (var key in src) {
  295. if (src.hasOwnProperty(key)) {
  296. var value = src[key];
  297. if (value !== null && isPlainObject(value)) {
  298. if (!isPlainObject(dist[key])) {
  299. dist[key] = {};
  300. }
  301. if (level < maxLevel) {
  302. _deepMix(dist[key], value, level + 1, maxLevel);
  303. } else {
  304. dist[key] = src[key];
  305. }
  306. } else if (isArray(value)) {
  307. dist[key] = [];
  308. dist[key] = dist[key].concat(value);
  309. } else if (value !== undefined) {
  310. dist[key] = value;
  311. }
  312. }
  313. }
  314. } // todo 重写
  315. var deepMix = function (rst) {
  316. var args = [];
  317. for (var _i = 1; _i < arguments.length; _i++) {
  318. args[_i - 1] = arguments[_i];
  319. }
  320. for (var i = 0; i < args.length; i += 1) {
  321. _deepMix(rst, args[i]);
  322. }
  323. return rst;
  324. };
  325. var indexOf = function (arr, obj) {
  326. if (!isArrayLike(arr)) {
  327. return -1;
  328. }
  329. var m = Array.prototype.indexOf;
  330. if (m) {
  331. return m.call(arr, obj);
  332. }
  333. var index = -1;
  334. for (var i = 0; i < arr.length; i++) {
  335. if (arr[i] === obj) {
  336. index = i;
  337. break;
  338. }
  339. }
  340. return index;
  341. };
  342. var hasOwnProperty = Object.prototype.hasOwnProperty;
  343. function isEmpty(value) {
  344. /**
  345. * isEmpty(null) => true
  346. * isEmpty() => true
  347. * isEmpty(true) => true
  348. * isEmpty(1) => true
  349. * isEmpty([1, 2, 3]) => false
  350. * isEmpty('abc') => false
  351. * isEmpty({ a: 1 }) => false
  352. */
  353. if (isNil(value)) {
  354. return true;
  355. }
  356. if (isArrayLike(value)) {
  357. return !value.length;
  358. }
  359. var type = getType(value);
  360. if (type === 'Map' || type === 'Set') {
  361. return !value.size;
  362. }
  363. if (isPrototype(value)) {
  364. return !Object.keys(value).length;
  365. }
  366. for (var key in value) {
  367. if (hasOwnProperty.call(value, key)) {
  368. return false;
  369. }
  370. }
  371. return true;
  372. }
  373. var isEqual = function (value, other) {
  374. if (value === other) {
  375. return true;
  376. }
  377. if (!value || !other) {
  378. return false;
  379. }
  380. if (isString(value) || isString(other)) {
  381. return false;
  382. }
  383. if (isArrayLike(value) || isArrayLike(other)) {
  384. if (value.length !== other.length) {
  385. return false;
  386. }
  387. var rst = true;
  388. for (var i = 0; i < value.length; i++) {
  389. rst = isEqual(value[i], other[i]);
  390. if (!rst) {
  391. break;
  392. }
  393. }
  394. return rst;
  395. }
  396. if (isObjectLike(value) || isObjectLike(other)) {
  397. var valueKeys = Object.keys(value);
  398. var otherKeys = Object.keys(other);
  399. if (valueKeys.length !== otherKeys.length) {
  400. return false;
  401. }
  402. var rst = true;
  403. for (var i = 0; i < valueKeys.length; i++) {
  404. rst = isEqual(value[valueKeys[i]], other[valueKeys[i]]);
  405. if (!rst) {
  406. break;
  407. }
  408. }
  409. return rst;
  410. }
  411. return false;
  412. };
  413. var map = function (arr, func) {
  414. if (!isArrayLike(arr)) {
  415. // @ts-ignore
  416. return arr;
  417. }
  418. var result = [];
  419. for (var index = 0; index < arr.length; index++) {
  420. var value = arr[index];
  421. result.push(func(value, index));
  422. }
  423. return result;
  424. };
  425. function size(o) {
  426. if (isNil(o)) {
  427. return 0;
  428. }
  429. if (isArrayLike(o)) {
  430. return o.length;
  431. }
  432. return Object.keys(o).length;
  433. }
  434. function merge(dataArray) {
  435. var rst = [];
  436. for (var i = 0, len = dataArray.length; i < len; i++) {
  437. rst = rst.concat(dataArray[i]);
  438. }
  439. return rst;
  440. }
  441. function values(data, name) {
  442. var rst = [];
  443. var tmpMap = {};
  444. for (var i = 0, len = data.length; i < len; i++) {
  445. var obj = data[i];
  446. var value = obj[name];
  447. if (!isNil(value)) {
  448. if (!isArray(value)) {
  449. if (!tmpMap[value]) {
  450. rst.push(value);
  451. tmpMap[value] = true;
  452. }
  453. } else {
  454. each(value, function (val) {
  455. if (!tmpMap[val]) {
  456. rst.push(val);
  457. tmpMap[val] = true;
  458. }
  459. });
  460. }
  461. }
  462. }
  463. return rst;
  464. }
  465. function firstValue(data, name) {
  466. var rst = null;
  467. for (var i = 0, len = data.length; i < len; i++) {
  468. var obj = data[i];
  469. var value = obj[name];
  470. if (!isNil(value)) {
  471. if (isArray(value)) {
  472. rst = value[0];
  473. } else {
  474. rst = value;
  475. }
  476. break;
  477. }
  478. }
  479. return rst;
  480. }
  481. function groupToMap(data, fields) {
  482. if (!fields) {
  483. return {
  484. 0: data
  485. };
  486. }
  487. var callback = function callback(row) {
  488. var unique = '_';
  489. for (var i = 0, l = fields.length; i < l; i++) {
  490. unique += row[fields[i]] && row[fields[i]].toString();
  491. }
  492. return unique;
  493. };
  494. var groups = {};
  495. for (var i = 0, len = data.length; i < len; i++) {
  496. var row = data[i];
  497. var key = callback(row);
  498. if (groups[key]) {
  499. groups[key].push(row);
  500. } else {
  501. groups[key] = [row];
  502. }
  503. }
  504. return groups;
  505. }
  506. function group(data, fields, appendConditions) {
  507. if (appendConditions === void 0) {
  508. appendConditions = {};
  509. }
  510. if (!fields) {
  511. return [data];
  512. }
  513. var groups = groupToMap(data, fields);
  514. var array = [];
  515. if (fields.length === 1 && appendConditions[fields[0]]) {
  516. var _values = appendConditions[fields[0]];
  517. each(_values, function (value) {
  518. value = '_' + value;
  519. array.push(groups[value]);
  520. });
  521. } else {
  522. for (var i in groups) {
  523. array.push(groups[i]);
  524. }
  525. }
  526. return array;
  527. }
  528. function remove(arr, obj) {
  529. if (!arr) {
  530. return;
  531. }
  532. var index = arr.indexOf(obj);
  533. if (index !== -1) {
  534. arr.splice(index, 1);
  535. }
  536. }
  537. function getRange$1(values) {
  538. if (!values.length) {
  539. return {
  540. min: 0,
  541. max: 0
  542. };
  543. }
  544. var max = Math.max.apply(null, values);
  545. var min = Math.min.apply(null, values);
  546. return {
  547. min: min,
  548. max: max
  549. };
  550. }
  551. var array = /*#__PURE__*/Object.freeze({
  552. __proto__: null,
  553. merge: merge,
  554. values: values,
  555. firstValue: firstValue,
  556. group: group,
  557. groupToMap: groupToMap,
  558. remove: remove,
  559. getRange: getRange$1
  560. });
  561. /**
  562. * Detects support for options object argument in addEventListener.
  563. * https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Safely_detecting_option_support
  564. * @private
  565. */
  566. var supportsEventListenerOptions = function () {
  567. var supports = false;
  568. try {
  569. var options = Object.defineProperty({}, 'passive', {
  570. get: function get() {
  571. supports = true;
  572. }
  573. });
  574. window.addEventListener('e', null, options);
  575. } catch (e) {// continue regardless of error
  576. }
  577. return supports;
  578. }(); // Default passive to true as expected by Chrome for 'touchstart' and 'touchend' events.
  579. // https://github.com/chartjs/Chart.js/issues/4287
  580. var eventListenerOptions = supportsEventListenerOptions ? {
  581. passive: true
  582. } : false;
  583. /* global wx, my */
  584. // weixin miniprogram
  585. var isWx = typeof wx === 'object' && typeof wx.getSystemInfoSync === 'function'; // ant miniprogram
  586. var isMy = typeof my === 'object' && typeof my.getSystemInfoSync === 'function'; // in node
  587. var isNode = typeof global && !typeof window; // in browser
  588. var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined' && typeof window.sessionStorage !== 'undefined';
  589. function isCanvasElement(el) {
  590. if (!el || typeof el !== 'object') return false;
  591. if (el.nodeType === 1 && el.nodeName) {
  592. // HTMLCanvasElement
  593. return true;
  594. } // CanvasElement
  595. return !!el.isCanvasElement;
  596. }
  597. function getPixelRatio() {
  598. return window && window.devicePixelRatio || 1;
  599. }
  600. function getStyle(el, property) {
  601. return el.currentStyle ? el.currentStyle[property] : document.defaultView.getComputedStyle(el, null).getPropertyValue(property);
  602. }
  603. function getWidth(el) {
  604. var width = getStyle(el, 'width');
  605. if (width === 'auto') {
  606. width = el.offsetWidth;
  607. }
  608. return parseFloat(width);
  609. }
  610. function getHeight(el) {
  611. var height = getStyle(el, 'height');
  612. if (height === 'auto') {
  613. height = el.offsetHeight;
  614. }
  615. return parseFloat(height);
  616. }
  617. function getDomById(id) {
  618. if (!id) {
  619. return null;
  620. }
  621. return document.getElementById(id);
  622. }
  623. function getRelativePosition(point, canvas) {
  624. var canvasDom = canvas.get('el');
  625. if (!canvasDom) return point;
  626. var _canvasDom$getBoundin = canvasDom.getBoundingClientRect(),
  627. top = _canvasDom$getBoundin.top,
  628. left = _canvasDom$getBoundin.left;
  629. var paddingLeft = parseFloat(getStyle(canvasDom, 'padding-left'));
  630. var paddingTop = parseFloat(getStyle(canvasDom, 'padding-top'));
  631. var mouseX = point.x - left - paddingLeft;
  632. var mouseY = point.y - top - paddingTop;
  633. return {
  634. x: mouseX,
  635. y: mouseY
  636. };
  637. }
  638. function addEventListener(source, type, listener) {
  639. source.addEventListener(type, listener, eventListenerOptions);
  640. }
  641. function removeEventListener(source, type, listener) {
  642. source.removeEventListener(type, listener, eventListenerOptions);
  643. }
  644. function landscapePoint(point, canvas) {
  645. var landscape = canvas.get('landscape');
  646. if (!landscape) {
  647. return point;
  648. }
  649. if (isFunction(landscape)) {
  650. return landscape(point, canvas);
  651. } // 默认顺时针旋转90度
  652. var height = canvas.get('height');
  653. var x = point.y;
  654. var y = height - point.x;
  655. return {
  656. x: x,
  657. y: y
  658. };
  659. }
  660. function convertPoints(ev, canvas) {
  661. var touches = ev.touches; // 认为是mouse事件
  662. if (!touches) {
  663. var point = getRelativePosition({
  664. x: ev.clientX,
  665. y: ev.clientY
  666. }, canvas);
  667. return [landscapePoint(point, canvas)];
  668. } // 单指 touchend 后,touchs 会变空,最后的触点要从changedTouches里拿
  669. if (!touches.length) {
  670. // 为了防止万一,加个空逻辑
  671. touches = ev.changedTouches || [];
  672. }
  673. var points = [];
  674. for (var i = 0, len = touches.length; i < len; i++) {
  675. var touch = touches[i]; // x, y: 相对canvas原点的位置,clientX, clientY 相对于可视窗口的位置
  676. var x = touch.x,
  677. y = touch.y,
  678. clientX = touch.clientX,
  679. clientY = touch.clientY;
  680. var _point = void 0; // 小程序环境会有x,y
  681. if (isNumber(x) || isNumber(y)) {
  682. _point = {
  683. x: x,
  684. y: y
  685. };
  686. } else {
  687. // 浏览器环境再计算下canvas的相对位置
  688. _point = getRelativePosition({
  689. x: clientX,
  690. y: clientY
  691. }, canvas);
  692. }
  693. points.push(landscapePoint(_point, canvas));
  694. }
  695. return points;
  696. }
  697. function createEvent(event, chart) {
  698. var canvas = chart.get('canvas');
  699. var points = convertPoints(event, canvas); // touchend会没有points
  700. var point = points[0] || {};
  701. return {
  702. type: event.type,
  703. chart: chart,
  704. "native": event,
  705. x: point.x,
  706. y: point.y
  707. };
  708. }
  709. function measureText(text, font, ctx) {
  710. if (!ctx) {
  711. ctx = document.createElement('canvas').getContext('2d');
  712. }
  713. ctx.font = font || '12px sans-serif';
  714. return ctx.measureText(text);
  715. }
  716. /**
  717. * @fileOverview Utility for F2
  718. * @author dxq613 @gmail.com
  719. * @author sima.zhang1990@gmail.com
  720. */
  721. function isObjectValueEqual(a, b) {
  722. // for vue.js
  723. a = Object.assign({}, a);
  724. b = Object.assign({}, b);
  725. var aProps = Object.getOwnPropertyNames(a);
  726. var bProps = Object.getOwnPropertyNames(b);
  727. if (aProps.length !== bProps.length) {
  728. return false;
  729. }
  730. for (var i = 0, len = aProps.length; i < len; i++) {
  731. var propName = aProps[i];
  732. if (a[propName] !== b[propName]) {
  733. return false;
  734. }
  735. }
  736. return true;
  737. }
  738. function parsePadding(padding) {
  739. var top;
  740. var right;
  741. var bottom;
  742. var left;
  743. if (isNumber(padding) || isString(padding)) {
  744. top = bottom = left = right = padding;
  745. } else if (isArray(padding)) {
  746. top = padding[0];
  747. right = !isNil(padding[1]) ? padding[1] : padding[0];
  748. bottom = !isNil(padding[2]) ? padding[2] : padding[0];
  749. left = !isNil(padding[3]) ? padding[3] : right;
  750. }
  751. return [top, right, bottom, left];
  752. }
  753. function directionEnabled(mode, dir) {
  754. if (mode === undefined) {
  755. return true;
  756. } else if (typeof mode === 'string') {
  757. return mode.indexOf(dir) !== -1;
  758. }
  759. return false;
  760. }
  761. function toTimeStamp(value) {
  762. if (isString(value)) {
  763. if (value.indexOf('T') > 0) {
  764. value = new Date(value).getTime();
  765. } else {
  766. // new Date('2010/01/10') 和 new Date('2010-01-10') 的差别在于:
  767. // 如果仅有年月日时,前者是带有时区的: Fri Jan 10 2020 02:40:13 GMT+0800 (中国标准时间)
  768. // 后者会格式化成 Sun Jan 10 2010 08:00:00 GMT+0800 (中国标准时间)
  769. value = new Date(value.replace(/-/gi, '/')).getTime();
  770. }
  771. }
  772. if (isDate(value)) {
  773. value = value.getTime();
  774. }
  775. return value;
  776. }
  777. var Util = /*#__PURE__*/Object.freeze({
  778. __proto__: null,
  779. Array: array,
  780. upperFirst: upperFirst,
  781. lowerFirst: lowerFirst,
  782. isString: isString,
  783. isNumber: isNumber,
  784. isBoolean: isBoolean,
  785. isFunction: isFunction,
  786. isDate: isDate,
  787. isArray: isArray,
  788. isNil: isNil,
  789. isObject: isObject,
  790. isPlainObject: isPlainObject,
  791. isEqual: isEqual,
  792. deepMix: deepMix,
  793. mix: mix,
  794. each: each,
  795. uniq: uniq,
  796. find: find,
  797. isObjectValueEqual: isObjectValueEqual,
  798. parsePadding: parsePadding,
  799. directionEnabled: directionEnabled,
  800. toTimeStamp: toTimeStamp,
  801. substitute: substitute,
  802. isWx: isWx,
  803. isMy: isMy,
  804. isNode: isNode,
  805. isBrowser: isBrowser,
  806. isCanvasElement: isCanvasElement,
  807. getPixelRatio: getPixelRatio,
  808. getStyle: getStyle,
  809. getWidth: getWidth,
  810. getHeight: getHeight,
  811. getDomById: getDomById,
  812. getRelativePosition: getRelativePosition,
  813. addEventListener: addEventListener,
  814. removeEventListener: removeEventListener,
  815. createEvent: createEvent,
  816. convertPoints: convertPoints,
  817. measureText: measureText
  818. });
  819. /**
  820. * @fileOverview default theme
  821. * @author dxq613@gail.com
  822. */
  823. var color1 = '#E8E8E8'; // color of axis-line and axis-grid
  824. var color2 = '#808080'; // color of axis label
  825. var defaultAxis = {
  826. label: {
  827. fill: color2,
  828. fontSize: 10
  829. },
  830. line: {
  831. stroke: color1,
  832. lineWidth: 1
  833. },
  834. grid: {
  835. type: 'line',
  836. stroke: color1,
  837. lineWidth: 1,
  838. lineDash: [2]
  839. },
  840. tickLine: null,
  841. labelOffset: 7.5
  842. };
  843. var Theme = {
  844. fontFamily: '"Helvetica Neue", "San Francisco", Helvetica, Tahoma, Arial, "PingFang SC", "Hiragino Sans GB", "Heiti SC", "Microsoft YaHei", sans-serif',
  845. defaultColor: '#1890FF',
  846. pixelRatio: 1,
  847. padding: 'auto',
  848. appendPadding: 15,
  849. colors: ['#1890FF', '#2FC25B', '#FACC14', '#223273', '#8543E0', '#13C2C2', '#3436C7', '#F04864'],
  850. shapes: {
  851. line: ['line', 'dash'],
  852. point: ['circle', 'hollowCircle']
  853. },
  854. sizes: [4, 10],
  855. axis: {
  856. common: defaultAxis,
  857. // common axis configuration
  858. bottom: mix({}, defaultAxis, {
  859. grid: null
  860. }),
  861. left: mix({}, defaultAxis, {
  862. line: null
  863. }),
  864. right: mix({}, defaultAxis, {
  865. line: null
  866. }),
  867. circle: mix({}, defaultAxis, {
  868. line: null
  869. }),
  870. radius: mix({}, defaultAxis, {
  871. labelOffset: 4
  872. })
  873. },
  874. shape: {
  875. line: {
  876. lineWidth: 2,
  877. lineJoin: 'round',
  878. lineCap: 'round'
  879. },
  880. point: {
  881. lineWidth: 0,
  882. size: 3
  883. },
  884. area: {
  885. fillOpacity: 0.1
  886. }
  887. },
  888. _defaultAxis: defaultAxis
  889. };
  890. var lang = {
  891. general: {
  892. title: '这是一个图表,',
  893. withTitle: '这是一个关于“{title}”的图表。'
  894. },
  895. coord: {
  896. cartesian: 'X轴是{xLabel}Y轴是{yLabel}' // polar: '弧度是{xLabel}半径是{yLabel}'
  897. },
  898. scale: {
  899. linear: '数值型,数据最小值为{min},最大值为{max};',
  900. cat: '分类型, 分类类型有:{values};',
  901. timeCat: '时间型,时间范围从{start}到{end};'
  902. },
  903. geometry: {
  904. prefix: '共有{count}种分类组成,',
  905. oneData: '第{index}类是{name},数据是{values};',
  906. partData: '第{index}类是{name},共有{count}项数据,前{part}项是{values};',
  907. allData: '第{index}类是{name},有{count}项数据,分别是{values};'
  908. },
  909. legend: {
  910. prefix: '图例分类有:'
  911. }
  912. };
  913. var Global = {
  914. version: '3.8.9',
  915. scales: {},
  916. widthRatio: {
  917. column: 1 / 2,
  918. rose: 0.999999,
  919. multiplePie: 3 / 4
  920. },
  921. lineDash: [4, 4],
  922. lang: lang
  923. };
  924. Global.setTheme = function (theme) {
  925. deepMix(Global, theme);
  926. };
  927. Global.setTheme(Theme);
  928. function _defineProperty(obj, key, value) {
  929. if (key in obj) {
  930. Object.defineProperty(obj, key, {
  931. value: value,
  932. enumerable: true,
  933. configurable: true,
  934. writable: true
  935. });
  936. } else {
  937. obj[key] = value;
  938. }
  939. return obj;
  940. }
  941. function _extends() {
  942. _extends = Object.assign || function (target) {
  943. for (var i = 1; i < arguments.length; i++) {
  944. var source = arguments[i];
  945. for (var key in source) {
  946. if (Object.prototype.hasOwnProperty.call(source, key)) {
  947. target[key] = source[key];
  948. }
  949. }
  950. }
  951. return target;
  952. };
  953. return _extends.apply(this, arguments);
  954. }
  955. function _inheritsLoose(subClass, superClass) {
  956. subClass.prototype = Object.create(superClass.prototype);
  957. subClass.prototype.constructor = subClass;
  958. subClass.__proto__ = superClass;
  959. }
  960. function _assertThisInitialized(self) {
  961. if (self === void 0) {
  962. throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
  963. }
  964. return self;
  965. }
  966. var EVENT_AFTER_INIT = 'afterinit';
  967. var EVENT_BEFORE_RENDER = 'beforerender';
  968. var EVENT_AFTER_RENDER = 'afterrender';
  969. var EVENT_BEFORE_DATA_CHANGE = 'beforedatachange';
  970. var EVENT_AFTER_DATA_CHANGE = 'afterdatachange';
  971. var EVENT_AFTER_SIZE_CHANGE = '_aftersizechange';
  972. var EVENT_AFTER_GEOM_INIT = '_aftergeominit';
  973. var EVENT_BEFORE_GEOM_DRAW = 'beforegeomdraw';
  974. var EVENT_AFTER_GEOM_DRAW = 'aftergeomdraw';
  975. var EVENT_CLEAR = 'clear';
  976. var EVENT_CLEAR_INNER = 'clearinner';
  977. var EVENT_REPAINT = 'repaint';
  978. // 实现简单的事件机制
  979. var EventEmit = /*#__PURE__*/function () {
  980. function EventEmit() {
  981. this.__events = {};
  982. }
  983. var _proto = EventEmit.prototype;
  984. _proto.on = function on(type, listener) {
  985. if (!type || !listener) {
  986. return;
  987. }
  988. var events = this.__events[type] || [];
  989. events.push(listener);
  990. this.__events[type] = events;
  991. };
  992. _proto.emit = function emit(type, e) {
  993. var _this = this;
  994. if (isObject(type)) {
  995. e = type;
  996. type = e && e.type;
  997. }
  998. if (!type) {
  999. return;
  1000. }
  1001. var events = this.__events[type];
  1002. if (!events || !events.length) {
  1003. return;
  1004. }
  1005. events.forEach(function (listener) {
  1006. listener.call(_this, e);
  1007. });
  1008. };
  1009. _proto.off = function off(type, listener) {
  1010. var __events = this.__events;
  1011. var events = __events[type];
  1012. if (!events || !events.length) {
  1013. return;
  1014. } // 如果没有指定方法,则删除所有项
  1015. if (!listener) {
  1016. delete __events[type];
  1017. return;
  1018. } // 删除指定的 listener
  1019. for (var i = 0, len = events.length; i < len; i++) {
  1020. if (events[i] === listener) {
  1021. events.splice(i, 1);
  1022. i--;
  1023. }
  1024. }
  1025. };
  1026. return EventEmit;
  1027. }();
  1028. var Base = /*#__PURE__*/function (_Emit) {
  1029. _inheritsLoose(Base, _Emit);
  1030. var _proto = Base.prototype;
  1031. _proto.getDefaultCfg = function getDefaultCfg() {
  1032. return {};
  1033. };
  1034. function Base(cfg) {
  1035. var _this;
  1036. _this = _Emit.call(this) || this;
  1037. var attrs = {};
  1038. var defaultCfg = _this.getDefaultCfg();
  1039. _this._attrs = attrs;
  1040. mix(attrs, defaultCfg, cfg);
  1041. return _this;
  1042. }
  1043. _proto.get = function get(name) {
  1044. return this._attrs[name];
  1045. };
  1046. _proto.set = function set(name, value) {
  1047. this._attrs[name] = value;
  1048. };
  1049. _proto.destroy = function destroy() {
  1050. this._attrs = {};
  1051. this.destroyed = true;
  1052. };
  1053. return Base;
  1054. }(EventEmit);
  1055. var Plot = /*#__PURE__*/function () {
  1056. function Plot(cfg) {
  1057. mix(this, cfg);
  1058. this._init();
  1059. }
  1060. var _proto = Plot.prototype;
  1061. _proto._init = function _init() {
  1062. var self = this;
  1063. var start = self.start;
  1064. var end = self.end;
  1065. var xMin = Math.min(start.x, end.x);
  1066. var xMax = Math.max(start.x, end.x);
  1067. var yMin = Math.min(start.y, end.y);
  1068. var yMax = Math.max(start.y, end.y);
  1069. this.tl = {
  1070. x: xMin,
  1071. y: yMin
  1072. };
  1073. this.tr = {
  1074. x: xMax,
  1075. y: yMin
  1076. };
  1077. this.bl = {
  1078. x: xMin,
  1079. y: yMax
  1080. };
  1081. this.br = {
  1082. x: xMax,
  1083. y: yMax
  1084. };
  1085. this.width = xMax - xMin;
  1086. this.height = yMax - yMin;
  1087. }
  1088. /**
  1089. * reset
  1090. * @param {Object} start start point
  1091. * @param {Object} end end point
  1092. */
  1093. ;
  1094. _proto.reset = function reset(start, end) {
  1095. this.start = start;
  1096. this.end = end;
  1097. this._init();
  1098. }
  1099. /**
  1100. * check the point is in the range of plot
  1101. * @param {Number} x x value
  1102. * @param {[type]} y y value
  1103. * @return {Boolean} return the result
  1104. */
  1105. ;
  1106. _proto.isInRange = function isInRange(x, y) {
  1107. if (isObject(x)) {
  1108. y = x.y;
  1109. x = x.x;
  1110. }
  1111. var tl = this.tl;
  1112. var br = this.br;
  1113. return tl.x <= x && x <= br.x && tl.y <= y && y <= br.y;
  1114. };
  1115. return Plot;
  1116. }();
  1117. var Matrix = {
  1118. generateDefault: function generateDefault() {
  1119. return [1, 0, 0, 1, 0, 0];
  1120. },
  1121. isChanged: function isChanged(m) {
  1122. return m[0] !== 1 || m[1] !== 0 || m[2] !== 0 || m[3] !== 1 || m[4] !== 0 || m[5] !== 0;
  1123. },
  1124. multiply: function multiply(m1, m2) {
  1125. var m11 = m1[0] * m2[0] + m1[2] * m2[1];
  1126. var m12 = m1[1] * m2[0] + m1[3] * m2[1];
  1127. var m21 = m1[0] * m2[2] + m1[2] * m2[3];
  1128. var m22 = m1[1] * m2[2] + m1[3] * m2[3];
  1129. var dx = m1[0] * m2[4] + m1[2] * m2[5] + m1[4];
  1130. var dy = m1[1] * m2[4] + m1[3] * m2[5] + m1[5];
  1131. return [m11, m12, m21, m22, dx, dy];
  1132. },
  1133. scale: function scale(out, m, v) {
  1134. out[0] = m[0] * v[0];
  1135. out[1] = m[1] * v[0];
  1136. out[2] = m[2] * v[1];
  1137. out[3] = m[3] * v[1];
  1138. out[4] = m[4];
  1139. out[5] = m[5];
  1140. return out;
  1141. },
  1142. rotate: function rotate(out, m, radian) {
  1143. var c = Math.cos(radian);
  1144. var s = Math.sin(radian);
  1145. var m11 = m[0] * c + m[2] * s;
  1146. var m12 = m[1] * c + m[3] * s;
  1147. var m21 = m[0] * -s + m[2] * c;
  1148. var m22 = m[1] * -s + m[3] * c;
  1149. out[0] = m11;
  1150. out[1] = m12;
  1151. out[2] = m21;
  1152. out[3] = m22;
  1153. out[4] = m[4];
  1154. out[5] = m[5];
  1155. return out;
  1156. },
  1157. translate: function translate(out, m, v) {
  1158. out[0] = m[0];
  1159. out[1] = m[1];
  1160. out[2] = m[2];
  1161. out[3] = m[3];
  1162. out[4] = m[4] + m[0] * v[0] + m[2] * v[1];
  1163. out[5] = m[5] + m[1] * v[0] + m[3] * v[1];
  1164. return out;
  1165. },
  1166. transform: function transform(m, actions) {
  1167. var out = [].concat(m);
  1168. for (var i = 0, len = actions.length; i < len; i++) {
  1169. var action = actions[i];
  1170. switch (action[0]) {
  1171. case 't':
  1172. Matrix.translate(out, out, [action[1], action[2]]);
  1173. break;
  1174. case 's':
  1175. Matrix.scale(out, out, [action[1], action[2]]);
  1176. break;
  1177. case 'r':
  1178. Matrix.rotate(out, out, action[1]);
  1179. break;
  1180. }
  1181. }
  1182. return out;
  1183. }
  1184. };
  1185. /**
  1186. * 2 Dimensional Vector
  1187. * @module vector2
  1188. */
  1189. var Vector2 = {
  1190. /**
  1191. * Creates a new, empty vector2
  1192. *
  1193. * @return {vector2} a new 2D vector
  1194. */
  1195. create: function create() {
  1196. return [0, 0];
  1197. },
  1198. /**
  1199. * Calculates the length of a vector2
  1200. *
  1201. * @param {vector2} v vector to calculate length of
  1202. * @return {Number} length of v
  1203. */
  1204. length: function length(v) {
  1205. var x = v[0];
  1206. var y = v[1];
  1207. return Math.sqrt(x * x + y * y);
  1208. },
  1209. /**
  1210. * Normalize a vector2
  1211. *
  1212. * @param {vector2} out the receiving vector
  1213. * @param {vector2} v vector to normalize
  1214. * @return {vector2} out
  1215. */
  1216. normalize: function normalize(out, v) {
  1217. var len = this.length(v);
  1218. if (len === 0) {
  1219. out[0] = 0;
  1220. out[1] = 0;
  1221. } else {
  1222. out[0] = v[0] / len;
  1223. out[1] = v[1] / len;
  1224. }
  1225. return out;
  1226. },
  1227. /**
  1228. * Adds two vector2's
  1229. *
  1230. * @param {vector2} out the receiving vector
  1231. * @param {vector2} v1 the first operand
  1232. * @param {vector2} v2 the second operand
  1233. * @return {vector2} out
  1234. */
  1235. add: function add(out, v1, v2) {
  1236. out[0] = v1[0] + v2[0];
  1237. out[1] = v1[1] + v2[1];
  1238. return out;
  1239. },
  1240. /**
  1241. * Subtracts vector v2 from vector v1
  1242. *
  1243. * @param {vector2} out the receiving vector
  1244. * @param {vector2} v1 the first operand
  1245. * @param {vector2} v2 the second operand
  1246. * @return {vector2} out
  1247. */
  1248. sub: function sub(out, v1, v2) {
  1249. out[0] = v1[0] - v2[0];
  1250. out[1] = v1[1] - v2[1];
  1251. return out;
  1252. },
  1253. /**
  1254. * Scales a vector2 by a scalar number
  1255. *
  1256. * @param {vector2} out the receiving vector
  1257. * @param {vector2} v the vector to scale
  1258. * @param {Number} s amount to scale the vector by
  1259. * @return {vector2} out
  1260. */
  1261. scale: function scale(out, v, s) {
  1262. out[0] = v[0] * s;
  1263. out[1] = v[1] * s;
  1264. return out;
  1265. },
  1266. /**
  1267. * Calculates the dot product of two vector2's
  1268. *
  1269. * @param {vector2} v1 the first operand
  1270. * @param {vector2} v2 the second operand
  1271. * @return {Number} dot product of v1 and v2
  1272. */
  1273. dot: function dot(v1, v2) {
  1274. return v1[0] * v2[0] + v1[1] * v2[1];
  1275. },
  1276. /**
  1277. * Calculates the direction of two vector2's
  1278. *
  1279. * @param {vector2} v1 the first operand
  1280. * @param {vector2} v2 the second operand
  1281. * @return {Boolean} the direction of v1 and v2
  1282. */
  1283. direction: function direction(v1, v2) {
  1284. return v1[0] * v2[1] - v2[0] * v1[1];
  1285. },
  1286. /**
  1287. * Calculates the angle of two vector2's
  1288. *
  1289. * @param {vector2} v1 the first operand
  1290. * @param {vector2} v2 the second operand
  1291. * @return {Number} angle of v1 and v2
  1292. */
  1293. angle: function angle(v1, v2) {
  1294. var theta = this.dot(v1, v2) / (this.length(v1) * this.length(v2));
  1295. return Math.acos(theta);
  1296. },
  1297. /**
  1298. * Calculates the angle of two vector2's with direction
  1299. *
  1300. * @param {vector2} v1 the first operand
  1301. * @param {vector2} v2 the second operand
  1302. * @param {Boolean} direction the direction of two vector2's
  1303. * @return {Number} angle of v1 and v2
  1304. */
  1305. angleTo: function angleTo(v1, v2, direction) {
  1306. var angle = this.angle(v1, v2);
  1307. var angleLargeThanPI = this.direction(v1, v2) >= 0;
  1308. if (direction) {
  1309. if (angleLargeThanPI) {
  1310. return Math.PI * 2 - angle;
  1311. }
  1312. return angle;
  1313. }
  1314. if (angleLargeThanPI) {
  1315. return angle;
  1316. }
  1317. return Math.PI * 2 - angle;
  1318. },
  1319. /**
  1320. * whether a vector2 is zero vector
  1321. *
  1322. * @param {vector2} v vector to calculate
  1323. * @return {Boolean} is or not a zero vector
  1324. */
  1325. zero: function zero(v) {
  1326. return v[0] === 0 && v[1] === 0;
  1327. },
  1328. /**
  1329. * Calculates the euclidian distance between two vector2's
  1330. *
  1331. * @param {vector2} v1 the first operand
  1332. * @param {vector2} v2 the second operand
  1333. * @return {Number} distance between a and b
  1334. */
  1335. distance: function distance(v1, v2) {
  1336. var x = v2[0] - v1[0];
  1337. var y = v2[1] - v1[1];
  1338. return Math.sqrt(x * x + y * y);
  1339. },
  1340. /**
  1341. * Creates a new vector2 initialized with values from an existing vector
  1342. *
  1343. * @param {vector2} v vector to clone
  1344. * @return {Array} a new 2D vector
  1345. */
  1346. clone: function clone(v) {
  1347. return [v[0], v[1]];
  1348. },
  1349. /**
  1350. * Return the minimum of two vector2's
  1351. *
  1352. * @param {vector2} out the receiving vector
  1353. * @param {vector2} v1 the first operand
  1354. * @param {vector2} v2 the second operand
  1355. * @return {vector2} out
  1356. */
  1357. min: function min(out, v1, v2) {
  1358. out[0] = Math.min(v1[0], v2[0]);
  1359. out[1] = Math.min(v1[1], v2[1]);
  1360. return out;
  1361. },
  1362. /**
  1363. * Return the maximum of two vector2's
  1364. *
  1365. * @param {vector2} out the receiving vector
  1366. * @param {vector2} v1 the first operand
  1367. * @param {vector2} v2 the second operand
  1368. * @return {vector2} out
  1369. */
  1370. max: function max(out, v1, v2) {
  1371. out[0] = Math.max(v1[0], v2[0]);
  1372. out[1] = Math.max(v1[1], v2[1]);
  1373. return out;
  1374. },
  1375. /**
  1376. * Transforms the vector2 with a mat2d
  1377. *
  1378. * @param {vector2} out the receiving vector
  1379. * @param {vector2} v the vector to transform
  1380. * @param {mat2d} m matrix to transform with
  1381. * @return {vector2} out
  1382. */
  1383. transformMat2d: function transformMat2d(out, v, m) {
  1384. var x = v[0];
  1385. var y = v[1];
  1386. out[0] = m[0] * x + m[2] * y + m[4];
  1387. out[1] = m[1] * x + m[3] * y + m[5];
  1388. return out;
  1389. }
  1390. };
  1391. var defaultMatrix = [1, 0, 0, 1, 0, 0];
  1392. var Base$1 = /*#__PURE__*/function () {
  1393. var _proto = Base.prototype;
  1394. _proto._initDefaultCfg = function _initDefaultCfg() {};
  1395. function Base(cfg) {
  1396. this._initDefaultCfg();
  1397. mix(this, cfg);
  1398. var start;
  1399. var end;
  1400. if (this.plot) {
  1401. start = this.plot.bl;
  1402. end = this.plot.tr;
  1403. this.start = start;
  1404. this.end = end;
  1405. } else {
  1406. start = this.start;
  1407. end = this.end;
  1408. }
  1409. this.init(start, end);
  1410. }
  1411. _proto._scale = function _scale(s1, s2) {
  1412. var matrix = this.matrix;
  1413. var center = this.center;
  1414. Matrix.translate(matrix, matrix, [center.x, center.y]);
  1415. Matrix.scale(matrix, matrix, [s1, s2]);
  1416. Matrix.translate(matrix, matrix, [-center.x, -center.y]);
  1417. };
  1418. _proto.init = function init(start, end) {
  1419. this.matrix = [].concat(defaultMatrix); // 设置中心点
  1420. this.center = {
  1421. x: (end.x - start.x) / 2 + start.x,
  1422. y: (end.y - start.y) / 2 + start.y
  1423. };
  1424. if (this.scale) {
  1425. this._scale(this.scale[0], this.scale[1]);
  1426. }
  1427. };
  1428. _proto.convertPoint = function convertPoint(point) {
  1429. var _this$_convertPoint = this._convertPoint(point),
  1430. x = _this$_convertPoint.x,
  1431. y = _this$_convertPoint.y;
  1432. if (!Matrix.isChanged(this.matrix)) {
  1433. return {
  1434. x: x,
  1435. y: y
  1436. };
  1437. }
  1438. var vector = [x, y];
  1439. Vector2.transformMat2d(vector, vector, this.matrix);
  1440. return {
  1441. x: vector[0],
  1442. y: vector[1]
  1443. };
  1444. };
  1445. _proto.invertPoint = function invertPoint(point) {
  1446. return this._invertPoint(point);
  1447. };
  1448. _proto._convertPoint = function _convertPoint(point) {
  1449. return point;
  1450. };
  1451. _proto._invertPoint = function _invertPoint(point) {
  1452. return point;
  1453. };
  1454. _proto.reset = function reset(plot) {
  1455. this.plot = plot;
  1456. var bl = plot.bl,
  1457. tr = plot.tr;
  1458. this.start = bl;
  1459. this.end = tr;
  1460. this.init(bl, tr);
  1461. };
  1462. return Base;
  1463. }();
  1464. var Cartesian = /*#__PURE__*/function (_Base) {
  1465. _inheritsLoose(Cartesian, _Base);
  1466. function Cartesian() {
  1467. return _Base.apply(this, arguments) || this;
  1468. }
  1469. var _proto = Cartesian.prototype;
  1470. _proto._initDefaultCfg = function _initDefaultCfg() {
  1471. this.type = 'cartesian';
  1472. this.transposed = false;
  1473. this.isRect = true;
  1474. };
  1475. _proto.init = function init(start, end) {
  1476. _Base.prototype.init.call(this, start, end);
  1477. this.x = {
  1478. start: start.x,
  1479. end: end.x
  1480. };
  1481. this.y = {
  1482. start: start.y,
  1483. end: end.y
  1484. };
  1485. };
  1486. _proto._convertPoint = function _convertPoint(point) {
  1487. var self = this;
  1488. var transposed = self.transposed;
  1489. var xDim = transposed ? 'y' : 'x';
  1490. var yDim = transposed ? 'x' : 'y';
  1491. var x = self.x;
  1492. var y = self.y;
  1493. return {
  1494. x: x.start + (x.end - x.start) * point[xDim],
  1495. y: y.start + (y.end - y.start) * point[yDim]
  1496. };
  1497. };
  1498. _proto._invertPoint = function _invertPoint(point) {
  1499. var self = this;
  1500. var transposed = self.transposed;
  1501. var xDim = transposed ? 'y' : 'x';
  1502. var yDim = transposed ? 'x' : 'y';
  1503. var x = self.x;
  1504. var y = self.y;
  1505. var rst = {};
  1506. rst[xDim] = (point.x - x.start) / (x.end - x.start);
  1507. rst[yDim] = (point.y - y.start) / (y.end - y.start);
  1508. return rst;
  1509. };
  1510. return Cartesian;
  1511. }(Base$1);
  1512. Base$1.Cartesian = Cartesian;
  1513. Base$1.Rect = Cartesian;
  1514. /**
  1515. * @fileOverview the Attribute base class
  1516. */
  1517. function toScaleString(scale, value) {
  1518. if (isString(value)) {
  1519. return value;
  1520. }
  1521. return scale.invert(scale.scale(value));
  1522. }
  1523. /**
  1524. * 所有视觉通道属性的基类
  1525. * @class Attr
  1526. */
  1527. var AttributeBase = /*#__PURE__*/function () {
  1528. function AttributeBase(cfg) {
  1529. var _this = this;
  1530. /**
  1531. * 属性的类型
  1532. * @type {String}
  1533. */
  1534. this.type = 'base';
  1535. /**
  1536. * 属性的名称
  1537. * @type {String}
  1538. */
  1539. this.name = null;
  1540. /**
  1541. * 回调函数
  1542. * @type {Function}
  1543. */
  1544. this.method = null;
  1545. /**
  1546. * 备选的值数组
  1547. * @type {Array}
  1548. */
  1549. this.values = [];
  1550. /**
  1551. * 属性内部的度量
  1552. * @type {Array}
  1553. */
  1554. this.scales = [];
  1555. /**
  1556. * 是否通过线性取值, 如果未指定,则根据数值的类型判定
  1557. * @type {Boolean}
  1558. */
  1559. this.linear = null;
  1560. /**
  1561. * 当用户设置的 callback 返回 null 时, 应该返回默认 callback 中的值
  1562. */
  1563. var mixedCallback = null;
  1564. var defaultCallback = this.callback;
  1565. if (cfg.callback) {
  1566. var userCallback = cfg.callback;
  1567. mixedCallback = function mixedCallback() {
  1568. for (var _len = arguments.length, params = new Array(_len), _key = 0; _key < _len; _key++) {
  1569. params[_key] = arguments[_key];
  1570. }
  1571. var ret = userCallback.apply(void 0, params);
  1572. if (isNil(ret)) {
  1573. ret = defaultCallback.apply(_this, params);
  1574. }
  1575. return ret;
  1576. };
  1577. }
  1578. mix(this, cfg);
  1579. if (mixedCallback) {
  1580. mix(this, {
  1581. callback: mixedCallback
  1582. });
  1583. }
  1584. } // 获取属性值,将值映射到视觉通道
  1585. var _proto = AttributeBase.prototype;
  1586. _proto._getAttrValue = function _getAttrValue(scale, value) {
  1587. var values = this.values;
  1588. if (scale.isCategory && !this.linear) {
  1589. var index = scale.translate(value);
  1590. return values[index % values.length];
  1591. }
  1592. var percent = scale.scale(value);
  1593. return this.getLinearValue(percent);
  1594. }
  1595. /**
  1596. * 如果进行线性映射,返回对应的映射值
  1597. * @protected
  1598. * @param {Number} percent 百分比
  1599. * @return {*} 颜色值、形状、大小等
  1600. */
  1601. ;
  1602. _proto.getLinearValue = function getLinearValue(percent) {
  1603. var values = this.values;
  1604. var steps = values.length - 1;
  1605. var step = Math.floor(steps * percent);
  1606. var leftPercent = steps * percent - step;
  1607. var start = values[step];
  1608. var end = step === steps ? start : values[step + 1];
  1609. var rstValue = start + (end - start) * leftPercent;
  1610. return rstValue;
  1611. }
  1612. /**
  1613. * 默认的回调函数
  1614. * @param {*} value 回调函数的值
  1615. * @type {Function}
  1616. * @return {Array} 返回映射后的值
  1617. */
  1618. ;
  1619. _proto.callback = function callback(value) {
  1620. var self = this;
  1621. var scale = self.scales[0];
  1622. var rstValue = null;
  1623. if (scale.type === 'identity') {
  1624. rstValue = scale.value;
  1625. } else {
  1626. rstValue = self._getAttrValue(scale, value);
  1627. }
  1628. return rstValue;
  1629. }
  1630. /**
  1631. * 根据度量获取属性名
  1632. * @return {Array} dims of this Attribute
  1633. */
  1634. ;
  1635. _proto.getNames = function getNames() {
  1636. var scales = this.scales;
  1637. var names = this.names;
  1638. var length = Math.min(scales.length, names.length);
  1639. var rst = [];
  1640. for (var i = 0; i < length; i++) {
  1641. rst.push(names[i]);
  1642. }
  1643. return rst;
  1644. }
  1645. /**
  1646. * 根据度量获取维度名
  1647. * @return {Array} dims of this Attribute
  1648. */
  1649. ;
  1650. _proto.getFields = function getFields() {
  1651. var scales = this.scales;
  1652. var rst = [];
  1653. each(scales, function (scale) {
  1654. rst.push(scale.field);
  1655. });
  1656. return rst;
  1657. }
  1658. /**
  1659. * 根据名称获取度量
  1660. * @param {String} name the name of scale
  1661. * @return {Scale} scale
  1662. */
  1663. ;
  1664. _proto.getScale = function getScale(name) {
  1665. var scales = this.scales;
  1666. var names = this.names;
  1667. var index = names.indexOf(name);
  1668. return scales[index];
  1669. }
  1670. /**
  1671. * 映射数据
  1672. * @param {*} param1...paramn 多个数值
  1673. * @return {Array} 映射的值组成的数组
  1674. */
  1675. ;
  1676. _proto.mapping = function mapping() {
  1677. var scales = this.scales;
  1678. var callback = this.callback;
  1679. for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  1680. params[_key2] = arguments[_key2];
  1681. }
  1682. var values = params;
  1683. if (callback) {
  1684. for (var i = 0, len = params.length; i < len; i++) {
  1685. params[i] = this._toOriginParam(params[i], scales[i]);
  1686. }
  1687. values = callback.apply(this, params);
  1688. }
  1689. values = [].concat(values);
  1690. return values;
  1691. } // 原始的参数
  1692. ;
  1693. _proto._toOriginParam = function _toOriginParam(param, scale) {
  1694. var rst = param;
  1695. if (!scale.isLinear) {
  1696. if (isArray(param)) {
  1697. rst = [];
  1698. for (var i = 0, len = param.length; i < len; i++) {
  1699. rst.push(toScaleString(scale, param[i]));
  1700. }
  1701. } else {
  1702. rst = toScaleString(scale, param);
  1703. }
  1704. }
  1705. return rst;
  1706. };
  1707. return AttributeBase;
  1708. }();
  1709. var Position = /*#__PURE__*/function (_Base) {
  1710. _inheritsLoose(Position, _Base);
  1711. function Position(cfg) {
  1712. var _this;
  1713. _this = _Base.call(this, cfg) || this;
  1714. _this.names = ['x', 'y'];
  1715. _this.type = 'position';
  1716. return _this;
  1717. }
  1718. var _proto = Position.prototype;
  1719. _proto.mapping = function mapping(x, y) {
  1720. var scales = this.scales;
  1721. var coord = this.coord;
  1722. var scaleX = scales[0];
  1723. var scaleY = scales[1];
  1724. var rstX;
  1725. var rstY;
  1726. var obj;
  1727. if (isNil(x) || isNil(y)) {
  1728. return [];
  1729. }
  1730. if (isArray(y) && isArray(x)) {
  1731. rstX = [];
  1732. rstY = [];
  1733. for (var i = 0, j = 0, xLen = x.length, yLen = y.length; i < xLen && j < yLen; i++, j++) {
  1734. obj = coord.convertPoint({
  1735. x: scaleX.scale(x[i]),
  1736. y: scaleY.scale(y[j])
  1737. });
  1738. rstX.push(obj.x);
  1739. rstY.push(obj.y);
  1740. }
  1741. } else if (isArray(y)) {
  1742. x = scaleX.scale(x);
  1743. rstY = [];
  1744. each(y, function (yVal) {
  1745. yVal = scaleY.scale(yVal);
  1746. obj = coord.convertPoint({
  1747. x: x,
  1748. y: yVal
  1749. });
  1750. if (rstX && rstX !== obj.x) {
  1751. if (!isArray(rstX)) {
  1752. rstX = [rstX];
  1753. }
  1754. rstX.push(obj.x);
  1755. } else {
  1756. rstX = obj.x;
  1757. }
  1758. rstY.push(obj.y);
  1759. });
  1760. } else if (isArray(x)) {
  1761. y = scaleY.scale(y);
  1762. rstX = [];
  1763. each(x, function (xVal) {
  1764. xVal = scaleX.scale(xVal);
  1765. obj = coord.convertPoint({
  1766. x: xVal,
  1767. y: y
  1768. });
  1769. if (rstY && rstY !== obj.y) {
  1770. if (!isArray(rstY)) {
  1771. rstY = [rstY];
  1772. }
  1773. rstY.push(obj.y);
  1774. } else {
  1775. rstY = obj.y;
  1776. }
  1777. rstX.push(obj.x);
  1778. });
  1779. } else {
  1780. x = scaleX.scale(x);
  1781. y = scaleY.scale(y);
  1782. var point = coord.convertPoint({
  1783. x: x,
  1784. y: y
  1785. });
  1786. rstX = point.x;
  1787. rstY = point.y;
  1788. }
  1789. return [rstX, rstY];
  1790. };
  1791. return Position;
  1792. }(AttributeBase);
  1793. var Shape = /*#__PURE__*/function (_Base) {
  1794. _inheritsLoose(Shape, _Base);
  1795. function Shape(cfg) {
  1796. var _this;
  1797. _this = _Base.call(this, cfg) || this;
  1798. _this.names = ['shape'];
  1799. _this.type = 'shape';
  1800. _this.gradient = null;
  1801. return _this;
  1802. }
  1803. /**
  1804. * @override
  1805. */
  1806. var _proto = Shape.prototype;
  1807. _proto.getLinearValue = function getLinearValue(percent) {
  1808. var values = this.values;
  1809. var index = Math.round((values.length - 1) * percent);
  1810. return values[index];
  1811. };
  1812. return Shape;
  1813. }(AttributeBase);
  1814. var Size = /*#__PURE__*/function (_Base) {
  1815. _inheritsLoose(Size, _Base);
  1816. function Size(cfg) {
  1817. var _this;
  1818. _this = _Base.call(this, cfg) || this;
  1819. _this.names = ['size'];
  1820. _this.type = 'size';
  1821. _this.gradient = null;
  1822. return _this;
  1823. }
  1824. return Size;
  1825. }(AttributeBase);
  1826. function getValue(start, end, percent, index) {
  1827. var value = start[index] + (end[index] - start[index]) * percent;
  1828. return value;
  1829. } // convert to hex
  1830. function arr2hex(arr) {
  1831. return '#' + toRGBValue(arr[0]) + toRGBValue(arr[1]) + toRGBValue(arr[2]);
  1832. }
  1833. function toRGBValue(value) {
  1834. value = Math.round(value);
  1835. value = value.toString(16);
  1836. if (value.length === 1) {
  1837. value = '0' + value;
  1838. }
  1839. return value;
  1840. }
  1841. function calColor(colors, percent) {
  1842. var steps = colors.length - 1;
  1843. var step = Math.floor(steps * percent);
  1844. var left = steps * percent - step;
  1845. var start = colors[step];
  1846. var end = step === steps ? start : colors[step + 1];
  1847. var rgb = arr2hex([getValue(start, end, left, 0), getValue(start, end, left, 1), getValue(start, end, left, 2)]);
  1848. return rgb;
  1849. }
  1850. function hex2arr(str) {
  1851. var arr = [];
  1852. arr.push(parseInt(str.substr(1, 2), 16));
  1853. arr.push(parseInt(str.substr(3, 2), 16));
  1854. arr.push(parseInt(str.substr(5, 2), 16));
  1855. return arr;
  1856. }
  1857. var colorCache = {
  1858. black: '#000000',
  1859. blue: '#0000ff',
  1860. grey: '#808080',
  1861. green: '#008000',
  1862. orange: '#ffa500',
  1863. pink: '#ffc0cb',
  1864. purple: '#800080',
  1865. red: '#ff0000',
  1866. white: '#ffffff',
  1867. yellow: '#ffff00'
  1868. };
  1869. /**
  1870. * Returns a hexadecimal string representing this color in RGB space, such as #f7eaba.
  1871. * @param {String} color color value
  1872. * @return {String} Returns a hexadecimal string
  1873. */
  1874. function toHex(color) {
  1875. if (colorCache[color]) {
  1876. return colorCache[color];
  1877. }
  1878. if (color[0] === '#') {
  1879. if (color.length === 7) {
  1880. return color;
  1881. }
  1882. var hex = color.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, function (m, r, g, b) {
  1883. return '#' + r + r + g + g + b + b;
  1884. }); // hex3 to hex6
  1885. colorCache[color] = hex;
  1886. return hex;
  1887. } // rgb/rgba to hex
  1888. var rst = color.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);
  1889. rst.shift();
  1890. rst = arr2hex(rst);
  1891. colorCache[color] = rst;
  1892. return rst;
  1893. }
  1894. /**
  1895. * handle the gradient color
  1896. * @param {Array} colors the colors
  1897. * @return {String} return the color value
  1898. */
  1899. function gradient(colors) {
  1900. var points = [];
  1901. if (isString(colors)) {
  1902. colors = colors.split('-');
  1903. }
  1904. each(colors, function (color) {
  1905. if (color.indexOf('#') === -1) {
  1906. color = toHex(color);
  1907. }
  1908. points.push(hex2arr(color));
  1909. });
  1910. return function (percent) {
  1911. return calColor(points, percent);
  1912. };
  1913. }
  1914. var Color = /*#__PURE__*/function (_Base) {
  1915. _inheritsLoose(Color, _Base);
  1916. function Color(cfg) {
  1917. var _this;
  1918. _this = _Base.call(this, cfg) || this;
  1919. _this.names = ['color'];
  1920. _this.type = 'color';
  1921. _this.gradient = null;
  1922. if (isString(_this.values)) {
  1923. _this.linear = true;
  1924. }
  1925. return _this;
  1926. }
  1927. /**
  1928. * @override
  1929. */
  1930. var _proto = Color.prototype;
  1931. _proto.getLinearValue = function getLinearValue(percent) {
  1932. var gradient$1 = this.gradient;
  1933. if (!gradient$1) {
  1934. var values = this.values;
  1935. gradient$1 = gradient(values);
  1936. this.gradient = gradient$1;
  1937. }
  1938. return gradient$1(percent);
  1939. };
  1940. return Color;
  1941. }(AttributeBase);
  1942. var Attr = /*#__PURE__*/Object.freeze({
  1943. __proto__: null,
  1944. Position: Position,
  1945. Shape: Shape,
  1946. Size: Size,
  1947. Color: Color
  1948. });
  1949. var Shape$1 = {};
  1950. var ShapeBase = {
  1951. _coord: null,
  1952. /**
  1953. * draw the shape
  1954. * @param {Object} cfg options
  1955. * @param {Object} container container to store the shapes
  1956. */
  1957. draw: function draw(cfg, container) {
  1958. if (this.drawShape) {
  1959. this.drawShape(cfg, container);
  1960. }
  1961. },
  1962. /**
  1963. * set the coordinate instance
  1964. * @param {Coord} coord coordinate instance
  1965. */
  1966. setCoord: function setCoord(coord) {
  1967. this._coord = coord;
  1968. },
  1969. /**
  1970. * convert the normalized value to the canvas position
  1971. * @param {point} point the point to convert
  1972. * @return {point} point return the result
  1973. */
  1974. parsePoint: function parsePoint(point) {
  1975. var coord = this._coord;
  1976. if (coord.isPolar) {
  1977. if (point.x === 1) point.x = 0.9999999;
  1978. if (point.y === 1) point.y = 0.9999999;
  1979. }
  1980. return coord.convertPoint(point);
  1981. },
  1982. /**
  1983. * convert the normalized value to the canvas position
  1984. * @param {points} points the array that store the points
  1985. * @return {points} points return the result
  1986. */
  1987. parsePoints: function parsePoints(points) {
  1988. if (!points) return false;
  1989. var self = this;
  1990. var rst = [];
  1991. points.forEach(function (point) {
  1992. rst.push(self.parsePoint(point));
  1993. });
  1994. return rst;
  1995. }
  1996. };
  1997. var ShapeFactoryBase = {
  1998. defaultShapeType: null,
  1999. setCoord: function setCoord(coord) {
  2000. this._coord = coord;
  2001. },
  2002. getShape: function getShape(type) {
  2003. var self = this;
  2004. if (isArray(type)) {
  2005. type = type[0];
  2006. }
  2007. var shape = self[type] || self[self.defaultShapeType];
  2008. shape._coord = self._coord;
  2009. return shape;
  2010. },
  2011. getShapePoints: function getShapePoints(type, cfg) {
  2012. var shape = this.getShape(type);
  2013. var fn = shape.getPoints || shape.getShapePoints || this.getDefaultPoints;
  2014. var points = fn(cfg);
  2015. return points;
  2016. },
  2017. getDefaultPoints: function getDefaultPoints()
  2018. /* cfg */
  2019. {
  2020. return [];
  2021. },
  2022. drawShape: function drawShape(type, cfg, container) {
  2023. var shape = this.getShape(type);
  2024. if (!cfg.color) {
  2025. cfg.color = Global.colors[0];
  2026. }
  2027. return shape.draw(cfg, container);
  2028. }
  2029. };
  2030. Shape$1.registerFactory = function (factoryName, cfg) {
  2031. var className = upperFirst(factoryName);
  2032. var geomObj = mix({}, ShapeFactoryBase, cfg);
  2033. Shape$1[className] = geomObj;
  2034. geomObj.name = factoryName;
  2035. return geomObj;
  2036. };
  2037. Shape$1.registerShape = function (factoryName, shapeType, cfg) {
  2038. var className = upperFirst(factoryName);
  2039. var factory = Shape$1[className];
  2040. var shapeObj = mix({}, ShapeBase, cfg);
  2041. factory[shapeType] = shapeObj;
  2042. return shapeObj;
  2043. };
  2044. Shape$1.registShape = Shape$1.registerShape;
  2045. Shape$1.getShapeFactory = function (factoryName) {
  2046. var self = this;
  2047. factoryName = factoryName || 'point';
  2048. var className = upperFirst(factoryName);
  2049. return self[className];
  2050. };
  2051. function _mix$1(dist, obj) {
  2052. for (var key in obj) {
  2053. if (obj.hasOwnProperty(key) && key !== 'constructor' && obj[key] !== undefined) {
  2054. dist[key] = obj[key];
  2055. }
  2056. }
  2057. }
  2058. var mix$1 = function mix(dist, src1, src2, src3) {
  2059. if (src1) _mix$1(dist, src1);
  2060. if (src2) _mix$1(dist, src2);
  2061. if (src3) _mix$1(dist, src3);
  2062. return dist;
  2063. };
  2064. var mix_1 = mix$1;
  2065. var Adjust = /*#__PURE__*/function () {
  2066. var _proto = Adjust.prototype;
  2067. _proto._initDefaultCfg = function _initDefaultCfg() {
  2068. this.adjustNames = ['x', 'y']; // 调整的维度,默认,x,y都做调整
  2069. };
  2070. function Adjust(cfg) {
  2071. this._initDefaultCfg();
  2072. mix_1(this, cfg);
  2073. }
  2074. /**
  2075. * @override
  2076. */
  2077. _proto.processAdjust = function processAdjust()
  2078. /* dataArray */
  2079. {};
  2080. return Adjust;
  2081. }();
  2082. var base = Adjust;
  2083. function generateScaleAria(scale) {
  2084. var type = scale.type,
  2085. values = scale.values;
  2086. if (type === 'linear') {
  2087. return substitute(lang.scale.linear, scale);
  2088. }
  2089. if (type === 'cat') {
  2090. return substitute(lang.scale.cat, {
  2091. values: values.slice(0, 10).join(' ')
  2092. });
  2093. }
  2094. if (type === 'timeCat') {
  2095. var start = scale.getText(values[0]);
  2096. var end = scale.getText(values[values.length - 1]);
  2097. return substitute(lang.scale.timeCat, {
  2098. start: start,
  2099. end: end
  2100. });
  2101. }
  2102. return '';
  2103. }
  2104. function generateCoordAria(coord, xScale, yScale) {
  2105. var type = coord.type;
  2106. if (!lang.coord[type]) {
  2107. return '';
  2108. }
  2109. return substitute(lang.coord[type], {
  2110. xLabel: generateScaleAria(xScale),
  2111. yLabel: generateScaleAria(yScale)
  2112. });
  2113. }
  2114. var GROUP_ATTRS = ['color', 'size', 'shape'];
  2115. var FIELD_ORIGIN = '_origin';
  2116. var FIELD_ORIGIN_Y = '_originY';
  2117. function parseFields(field) {
  2118. if (isArray(field)) {
  2119. return field;
  2120. }
  2121. if (isString(field)) {
  2122. return field.split('*');
  2123. }
  2124. return [field];
  2125. }
  2126. /**
  2127. * The parent class for Geometry
  2128. * @class Geom
  2129. */
  2130. var Geom = /*#__PURE__*/function (_Base) {
  2131. _inheritsLoose(Geom, _Base);
  2132. function Geom() {
  2133. return _Base.apply(this, arguments) || this;
  2134. }
  2135. var _proto = Geom.prototype;
  2136. _proto.getDefaultCfg = function getDefaultCfg() {
  2137. return {
  2138. /**
  2139. * geometry type
  2140. * @type {String}
  2141. */
  2142. type: null,
  2143. /**
  2144. * the data of geometry
  2145. * @type {Array}
  2146. */
  2147. data: null,
  2148. /**
  2149. * the attrs of geo,etry
  2150. * @type {Object}
  2151. */
  2152. attrs: {},
  2153. scales: {},
  2154. /**
  2155. * group for storing the shapes
  2156. * @type {Canvas}
  2157. */
  2158. container: null,
  2159. /**
  2160. * style options
  2161. * @type {Object}
  2162. */
  2163. styleOptions: null,
  2164. chart: null,
  2165. shapeType: '',
  2166. /**
  2167. * wether to generate key points for each shape
  2168. * @protected
  2169. * @type {Boolean}
  2170. */
  2171. generatePoints: false,
  2172. attrOptions: {},
  2173. sortable: false,
  2174. startOnZero: true,
  2175. visible: true,
  2176. connectNulls: false,
  2177. // 是否丢弃没有值的分组。
  2178. ignoreEmptyGroup: false,
  2179. // 是否已经初始化
  2180. isInit: false
  2181. };
  2182. };
  2183. _proto.init = function init() {
  2184. var self = this;
  2185. var isInit = self.get('isInit');
  2186. if (isInit) {
  2187. return;
  2188. }
  2189. self._initAttrs();
  2190. self._processData();
  2191. self.set('isInit', true);
  2192. };
  2193. _proto._getGroupScales = function _getGroupScales() {
  2194. var self = this;
  2195. var scales = [];
  2196. each(GROUP_ATTRS, function (attrName) {
  2197. var attr = self.getAttr(attrName);
  2198. if (attr) {
  2199. var attrScales = attr.scales;
  2200. each(attrScales, function (scale) {
  2201. if (scale && scale.isCategory && scales.indexOf(scale) === -1) {
  2202. scales.push(scale);
  2203. }
  2204. });
  2205. }
  2206. });
  2207. return scales;
  2208. };
  2209. _proto._groupData = function _groupData(data) {
  2210. var self = this;
  2211. var colDefs = self.get('colDefs');
  2212. var groupScales = self._getGroupScales();
  2213. if (groupScales.length) {
  2214. var appendConditions = {};
  2215. var names = [];
  2216. each(groupScales, function (scale) {
  2217. var field = scale.field;
  2218. names.push(field);
  2219. if (colDefs && colDefs[field] && colDefs[field].values) {
  2220. // users have defined
  2221. appendConditions[scale.field] = colDefs[field].values;
  2222. }
  2223. });
  2224. return group(data, names, appendConditions);
  2225. }
  2226. return [data];
  2227. };
  2228. _proto._setAttrOptions = function _setAttrOptions(attrName, attrCfg) {
  2229. var options = this.get('attrOptions');
  2230. options[attrName] = attrCfg;
  2231. var attrs = this.get('attrs'); // 说明已经初始化过了
  2232. if (Object.keys(attrs).length) {
  2233. this._createAttr(attrName, attrCfg);
  2234. }
  2235. };
  2236. _proto._createAttrOption = function _createAttrOption(attrName, field, cfg, defaultValues) {
  2237. var attrCfg = {};
  2238. attrCfg.field = field;
  2239. if (cfg) {
  2240. if (isFunction(cfg)) {
  2241. attrCfg.callback = cfg;
  2242. } else {
  2243. attrCfg.values = cfg;
  2244. }
  2245. } else {
  2246. attrCfg.values = defaultValues;
  2247. }
  2248. this._setAttrOptions(attrName, attrCfg);
  2249. };
  2250. _proto._createAttr = function _createAttr(type, option) {
  2251. var self = this;
  2252. var attrs = self.get('attrs');
  2253. var coord = self.get('coord');
  2254. var className = upperFirst(type);
  2255. var fields = parseFields(option.field);
  2256. if (type === 'position') {
  2257. option.coord = coord;
  2258. }
  2259. var scales = [];
  2260. for (var i = 0, len = fields.length; i < len; i++) {
  2261. var field = fields[i];
  2262. var scale = self._createScale(field);
  2263. scales.push(scale);
  2264. }
  2265. if (type === 'position') {
  2266. var yScale = scales[1]; // 饼图的处理,但是还不知道为啥
  2267. if (coord.type === 'polar' && coord.transposed && self.hasAdjust('stack')) {
  2268. if (yScale.values.length) {
  2269. yScale.change({
  2270. nice: false,
  2271. min: 0,
  2272. max: Math.max.apply(null, yScale.values)
  2273. });
  2274. }
  2275. }
  2276. }
  2277. option.scales = scales;
  2278. var attr = new Attr[className](option);
  2279. attrs[type] = attr;
  2280. return attr;
  2281. };
  2282. _proto._initAttrs = function _initAttrs() {
  2283. var self = this;
  2284. var attrOptions = self.get('attrOptions');
  2285. for (var type in attrOptions) {
  2286. if (attrOptions.hasOwnProperty(type)) {
  2287. this._createAttr(type, attrOptions[type]);
  2288. }
  2289. }
  2290. };
  2291. _proto._createScale = function _createScale(field) {
  2292. var scales = this.get('scales');
  2293. var scale = scales[field];
  2294. if (!scale) {
  2295. scale = this.get('chart').createScale(field);
  2296. scales[field] = scale;
  2297. }
  2298. return scale;
  2299. };
  2300. _proto._processData = function _processData() {
  2301. var self = this;
  2302. var data = this.get('data');
  2303. var dataArray = [];
  2304. var groupedArray = this._groupData(data);
  2305. if (this.get('ignoreEmptyGroup')) {
  2306. var yScale = this.getYScale();
  2307. groupedArray = groupedArray.filter(function (group) {
  2308. return group.some(function (item) {
  2309. return typeof item[yScale.field] !== 'undefined';
  2310. });
  2311. });
  2312. }
  2313. for (var i = 0, len = groupedArray.length; i < len; i++) {
  2314. var subData = groupedArray[i];
  2315. var tempData = self._saveOrigin(subData);
  2316. if (this.hasAdjust('dodge')) {
  2317. self._numberic(tempData);
  2318. }
  2319. dataArray.push(tempData);
  2320. }
  2321. if (self.get('adjust')) {
  2322. self._adjustData(dataArray);
  2323. }
  2324. if (self.get('sortable')) {
  2325. self._sort(dataArray);
  2326. }
  2327. self.emit('afterprocessdata', {
  2328. dataArray: dataArray
  2329. });
  2330. self.set('mappingData', dataArray);
  2331. self.set('dataArray', dataArray);
  2332. return dataArray;
  2333. };
  2334. _proto._saveOrigin = function _saveOrigin(data) {
  2335. var rst = [];
  2336. for (var i = 0, len = data.length; i < len; i++) {
  2337. var origin = data[i];
  2338. var obj = {};
  2339. for (var k in origin) {
  2340. obj[k] = origin[k];
  2341. }
  2342. obj[FIELD_ORIGIN] = origin;
  2343. rst.push(obj);
  2344. }
  2345. return rst;
  2346. };
  2347. _proto._numberic = function _numberic(data) {
  2348. var positionAttr = this.getAttr('position');
  2349. var scales = positionAttr.scales;
  2350. for (var j = 0, len = data.length; j < len; j++) {
  2351. var obj = data[j];
  2352. var count = Math.min(2, scales.length);
  2353. for (var i = 0; i < count; i++) {
  2354. var scale = scales[i];
  2355. if (scale.isCategory) {
  2356. var field = scale.field;
  2357. obj[field] = scale.translate(obj[field]);
  2358. }
  2359. }
  2360. }
  2361. };
  2362. _proto._adjustData = function _adjustData(dataArray) {
  2363. var self = this;
  2364. var adjust = self.get('adjust');
  2365. if (adjust) {
  2366. var adjustType = upperFirst(adjust.type);
  2367. if (!base[adjustType]) {
  2368. throw new Error('not support such adjust : ' + adjust);
  2369. }
  2370. var xScale = self.getXScale();
  2371. var yScale = self.getYScale();
  2372. var cfg = mix({
  2373. xField: xScale.field,
  2374. yField: yScale.field
  2375. }, adjust);
  2376. var adjustObject = new base[adjustType](cfg);
  2377. adjustObject.processAdjust(dataArray);
  2378. if (adjustType === 'Stack') {
  2379. self._updateStackRange(yScale.field, yScale, dataArray);
  2380. }
  2381. }
  2382. };
  2383. _proto._updateStackRange = function _updateStackRange(field, scale, dataArray) {
  2384. var mergeArray = merge(dataArray);
  2385. var min = scale.min;
  2386. var max = scale.max;
  2387. for (var i = 0, len = mergeArray.length; i < len; i++) {
  2388. var obj = mergeArray[i];
  2389. var tmpMin = Math.min.apply(null, obj[field]);
  2390. var tmpMax = Math.max.apply(null, obj[field]);
  2391. if (tmpMin < min) {
  2392. min = tmpMin;
  2393. }
  2394. if (tmpMax > max) {
  2395. max = tmpMax;
  2396. }
  2397. }
  2398. if (min < scale.min || max > scale.max) {
  2399. scale.change({
  2400. min: min,
  2401. max: max
  2402. });
  2403. }
  2404. };
  2405. _proto._sort = function _sort(mappedArray) {
  2406. var self = this;
  2407. var xScale = self.getXScale();
  2408. var field = xScale.field,
  2409. type = xScale.type;
  2410. if (type !== 'identity' && xScale.values.length > 1) {
  2411. each(mappedArray, function (itemArr) {
  2412. itemArr.sort(function (obj1, obj2) {
  2413. if (type === 'timeCat') {
  2414. return toTimeStamp(obj1[FIELD_ORIGIN][field]) - toTimeStamp(obj2[FIELD_ORIGIN][field]);
  2415. }
  2416. return xScale.translate(obj1[FIELD_ORIGIN][field]) - xScale.translate(obj2[FIELD_ORIGIN][field]);
  2417. });
  2418. });
  2419. }
  2420. self.set('hasSorted', true);
  2421. self.set('dataArray', mappedArray);
  2422. };
  2423. _proto.paint = function paint() {
  2424. var self = this;
  2425. var dataArray = self.get('mappingData');
  2426. var mappedArray = [];
  2427. var shapeFactory = self.getShapeFactory();
  2428. shapeFactory.setCoord(self.get('coord'));
  2429. self._beforeMapping(dataArray);
  2430. for (var i = 0, len = dataArray.length; i < len; i++) {
  2431. var data = dataArray[i];
  2432. if (data.length) {
  2433. var mappedData = self._mapping(data);
  2434. mappedArray.push(mappedData);
  2435. self.draw(mappedData, shapeFactory);
  2436. }
  2437. }
  2438. self.set('dataArray', mappedArray);
  2439. this.generateAria();
  2440. };
  2441. _proto.getShapeFactory = function getShapeFactory() {
  2442. var shapeFactory = this.get('shapeFactory');
  2443. if (!shapeFactory) {
  2444. var shapeType = this.get('shapeType');
  2445. shapeFactory = Shape$1.getShapeFactory(shapeType);
  2446. this.set('shapeFactory', shapeFactory);
  2447. }
  2448. return shapeFactory;
  2449. };
  2450. _proto._mapping = function _mapping(data) {
  2451. var self = this;
  2452. var attrs = self.get('attrs');
  2453. var yField = self.getYScale().field; // 用来缓存转换的值,减少mapping耗时
  2454. var mappedCache = {};
  2455. var mappedData = new Array(data.length);
  2456. for (var k in attrs) {
  2457. if (attrs.hasOwnProperty(k)) {
  2458. var attr = attrs[k];
  2459. var names = attr.names;
  2460. var scales = attr.scales;
  2461. for (var i = 0, len = data.length; i < len; i++) {
  2462. var record = data[i];
  2463. var mappedRecord = _extends({}, record, mappedData[i]);
  2464. mappedRecord[FIELD_ORIGIN_Y] = record[yField]; // 获取视觉属性对应的value值
  2465. // 位置的缓存命中率低,还是每次单独计算
  2466. if (attr.type === 'position') {
  2467. var values = self._getAttrValues(attr, record);
  2468. for (var j = 0, _len = values.length; j < _len; j++) {
  2469. var val = values[j];
  2470. var name = names[j];
  2471. mappedRecord[name] = isArray(val) && val.length === 1 ? val[0] : val;
  2472. }
  2473. } else {
  2474. // 除了position其他都只有一项
  2475. var _name = names[0];
  2476. var field = scales[0].field;
  2477. var value = record[field];
  2478. var key = "" + _name + value;
  2479. var _values = mappedCache[key];
  2480. if (!_values) {
  2481. _values = self._getAttrValues(attr, record);
  2482. mappedCache[key] = _values;
  2483. }
  2484. mappedRecord[_name] = _values[0];
  2485. } // 设置新数组
  2486. mappedData[i] = mappedRecord;
  2487. }
  2488. }
  2489. }
  2490. return mappedData;
  2491. };
  2492. _proto._getAttrValues = function _getAttrValues(attr, record) {
  2493. var scales = attr.scales;
  2494. var params = [];
  2495. for (var i = 0, len = scales.length; i < len; i++) {
  2496. var scale = scales[i];
  2497. var field = scale.field;
  2498. if (scale.type === 'identity') {
  2499. params.push(scale.value);
  2500. } else {
  2501. params.push(record[field]);
  2502. }
  2503. }
  2504. var values = attr.mapping.apply(attr, params);
  2505. return values;
  2506. };
  2507. _proto.getAttrValue = function getAttrValue(attrName, record) {
  2508. var attr = this.getAttr(attrName);
  2509. var rst = null;
  2510. if (attr) {
  2511. var values = this._getAttrValues(attr, record);
  2512. rst = values[0];
  2513. }
  2514. return rst;
  2515. };
  2516. _proto._beforeMapping = function _beforeMapping(dataArray) {
  2517. var self = this;
  2518. if (self.get('generatePoints')) {
  2519. self._generatePoints(dataArray);
  2520. }
  2521. };
  2522. _proto.isInCircle = function isInCircle() {
  2523. var coord = this.get('coord');
  2524. return coord && coord.isPolar;
  2525. };
  2526. _proto.getCallbackCfg = function getCallbackCfg(fields, cfg, origin) {
  2527. if (!fields) {
  2528. return cfg;
  2529. }
  2530. var tmpCfg = {};
  2531. var params = fields.map(function (field) {
  2532. return origin[field];
  2533. });
  2534. each(cfg, function (v, k) {
  2535. if (isFunction(v)) {
  2536. tmpCfg[k] = v.apply(null, params);
  2537. } else {
  2538. tmpCfg[k] = v;
  2539. }
  2540. });
  2541. return tmpCfg;
  2542. };
  2543. _proto.getDrawCfg = function getDrawCfg(obj) {
  2544. var self = this;
  2545. var isInCircle = self.isInCircle();
  2546. var cfg = {
  2547. origin: obj,
  2548. x: obj.x,
  2549. y: obj.y,
  2550. color: obj.color,
  2551. size: obj.size,
  2552. shape: obj.shape,
  2553. isInCircle: isInCircle,
  2554. opacity: obj.opacity
  2555. };
  2556. var styleOptions = self.get('styleOptions');
  2557. if (styleOptions && styleOptions.style) {
  2558. cfg.style = self.getCallbackCfg(styleOptions.fields, styleOptions.style, obj[FIELD_ORIGIN]);
  2559. }
  2560. if (self.get('generatePoints')) {
  2561. cfg.points = obj.points;
  2562. cfg.nextPoints = obj.nextPoints;
  2563. }
  2564. if (isInCircle) {
  2565. cfg.center = self.get('coord').center;
  2566. }
  2567. return cfg;
  2568. };
  2569. _proto.draw = function draw(data, shapeFactory) {
  2570. var self = this;
  2571. var container = self.get('container');
  2572. var yScale = self.getYScale();
  2573. each(data, function (obj, index) {
  2574. if (yScale && isNil(obj._origin[yScale.field])) {
  2575. return;
  2576. }
  2577. obj.index = index;
  2578. var cfg = self.getDrawCfg(obj);
  2579. var shape = obj.shape;
  2580. self.drawShape(shape, obj, cfg, container, shapeFactory);
  2581. });
  2582. };
  2583. _proto.drawShape = function drawShape(shape, shapeData, cfg, container, shapeFactory) {
  2584. var gShape = shapeFactory.drawShape(shape, cfg, container);
  2585. if (gShape) {
  2586. each([].concat(gShape), function (s) {
  2587. s.set('origin', shapeData);
  2588. });
  2589. }
  2590. };
  2591. _proto._generatePoints = function _generatePoints(dataArray) {
  2592. var self = this;
  2593. var shapeFactory = self.getShapeFactory();
  2594. var shapeAttr = self.getAttr('shape');
  2595. each(dataArray, function (data) {
  2596. for (var i = 0, len = data.length; i < len; i++) {
  2597. var obj = data[i];
  2598. var cfg = self.createShapePointsCfg(obj);
  2599. var shape = shapeAttr ? self._getAttrValues(shapeAttr, obj) : null;
  2600. var points = shapeFactory.getShapePoints(shape, cfg);
  2601. obj.points = points;
  2602. }
  2603. }); // 添加nextPoints
  2604. each(dataArray, function (data, index) {
  2605. var nextData = dataArray[index + 1];
  2606. if (nextData) {
  2607. data[0].nextPoints = nextData[0].points;
  2608. }
  2609. });
  2610. } // 生成无障碍文本
  2611. ;
  2612. _proto.generateAria = function generateAria() {
  2613. var container = this.get('container');
  2614. var aria = container.get('aria');
  2615. if (!aria) {
  2616. return;
  2617. }
  2618. var ariaLables = [];
  2619. var coord = this.get('coord');
  2620. var xScale = this.getXScale();
  2621. var yScale = this.getYScale();
  2622. var coordAriaLabel = generateCoordAria(coord, xScale, yScale);
  2623. ariaLables.push(coordAriaLabel);
  2624. var _lang$geometry = lang.geometry,
  2625. prefix = _lang$geometry.prefix,
  2626. oneData = _lang$geometry.oneData,
  2627. partData = _lang$geometry.partData,
  2628. allData = _lang$geometry.allData;
  2629. var dataArray = this.get('dataArray');
  2630. var count = dataArray.length; // 只处理一个,不然太复杂
  2631. var groupScale = this._getGroupScales()[0];
  2632. if (groupScale) {
  2633. var prefixLabel = substitute(prefix, {
  2634. count: count
  2635. });
  2636. ariaLables.push(prefixLabel);
  2637. each(dataArray, function (data, index) {
  2638. var len = data.length;
  2639. if (!len) return;
  2640. var firstObj = data[0]._origin;
  2641. if (len === 1) {
  2642. ariaLables.push(substitute(oneData, {
  2643. index: index + 1,
  2644. count: len,
  2645. name: firstObj[groupScale.field],
  2646. values: firstObj[yScale.field]
  2647. }));
  2648. } else {
  2649. var template = len > 5 ? partData : allData;
  2650. var values = data.slice(0, 5).map(function (record) {
  2651. var _origin = record._origin;
  2652. var xValue = xScale.getText(_origin[xScale.field]);
  2653. var yValue = yScale.getText(_origin[yScale.field]);
  2654. return xValue + ":" + yValue;
  2655. });
  2656. ariaLables.push(substitute(template, {
  2657. index: index + 1,
  2658. count: len,
  2659. part: 3,
  2660. name: firstObj[groupScale.field],
  2661. values: values.join(' ')
  2662. }));
  2663. }
  2664. });
  2665. }
  2666. container.set('ariaLabel', ariaLables.join(''));
  2667. }
  2668. /**
  2669. * get the info of each shape
  2670. * @protected
  2671. * @param {Object} obj the data item
  2672. * @return {Object} cfg return the result
  2673. */
  2674. ;
  2675. _proto.createShapePointsCfg = function createShapePointsCfg(obj) {
  2676. var xScale = this.getXScale();
  2677. var yScale = this.getYScale();
  2678. var x = this._normalizeValues(obj[xScale.field], xScale);
  2679. var y;
  2680. if (yScale) {
  2681. y = this._normalizeValues(obj[yScale.field], yScale);
  2682. } else {
  2683. y = obj.y ? obj.y : 0.1;
  2684. }
  2685. return {
  2686. x: x,
  2687. y: y,
  2688. y0: yScale ? yScale.scale(this.getYMinValue()) : undefined
  2689. };
  2690. };
  2691. _proto.getYMinValue = function getYMinValue() {
  2692. var yScale = this.getYScale();
  2693. var min = yScale.min,
  2694. max = yScale.max;
  2695. var value;
  2696. if (this.get('startOnZero')) {
  2697. if (max <= 0 && min <= 0) {
  2698. value = max;
  2699. } else {
  2700. value = min >= 0 ? min : 0;
  2701. }
  2702. } else {
  2703. value = min;
  2704. }
  2705. return value;
  2706. };
  2707. _proto._normalizeValues = function _normalizeValues(values, scale) {
  2708. var rst = [];
  2709. if (isArray(values)) {
  2710. for (var i = 0, len = values.length; i < len; i++) {
  2711. var v = values[i];
  2712. rst.push(scale.scale(v));
  2713. }
  2714. } else {
  2715. rst = scale.scale(values);
  2716. }
  2717. return rst;
  2718. };
  2719. _proto.getAttr = function getAttr(name) {
  2720. return this.get('attrs')[name];
  2721. };
  2722. _proto.getXScale = function getXScale() {
  2723. return this.getAttr('position').scales[0];
  2724. };
  2725. _proto.getYScale = function getYScale() {
  2726. return this.getAttr('position').scales[1];
  2727. };
  2728. _proto.hasAdjust = function hasAdjust(adjust) {
  2729. return this.get('adjust') && this.get('adjust').type === adjust;
  2730. };
  2731. _proto._getSnap = function _getSnap(scale, item, arr) {
  2732. var i = 0;
  2733. var values;
  2734. var yField = this.getYScale().field; // 叠加的维度
  2735. if (this.hasAdjust('stack') && scale.field === yField) {
  2736. values = [];
  2737. arr.forEach(function (obj) {
  2738. values.push(obj[FIELD_ORIGIN_Y]);
  2739. });
  2740. for (var len = values.length; i < len; i++) {
  2741. if (values[0][0] > item) {
  2742. break;
  2743. }
  2744. if (values[values.length - 1][1] <= item) {
  2745. i = values.length - 1;
  2746. break;
  2747. }
  2748. if (values[i][0] <= item && values[i][1] > item) {
  2749. break;
  2750. }
  2751. }
  2752. } else {
  2753. values = scale.values;
  2754. values.sort(function (a, b) {
  2755. return a - b;
  2756. });
  2757. for (var _len2 = values.length; i < _len2; i++) {
  2758. // 如果只有1个点直接返回第1个点
  2759. if (_len2 <= 1) {
  2760. break;
  2761. } // 第1个点和第2个点之间
  2762. if ((values[0] + values[1]) / 2 > item) {
  2763. break;
  2764. } // 中间的点
  2765. if ((values[i - 1] + values[i]) / 2 <= item && (values[i + 1] + values[i]) / 2 > item) {
  2766. break;
  2767. } // 最后2个点
  2768. if ((values[values.length - 2] + values[values.length - 1]) / 2 <= item) {
  2769. i = values.length - 1;
  2770. break;
  2771. }
  2772. }
  2773. }
  2774. var result = values[i];
  2775. return result;
  2776. };
  2777. _proto.getSnapRecords = function getSnapRecords(point) {
  2778. var self = this;
  2779. var coord = self.get('coord');
  2780. var xScale = self.getXScale();
  2781. var yScale = self.getYScale();
  2782. var xfield = xScale.field;
  2783. var dataArray = self.get('dataArray');
  2784. if (!this.get('hasSorted')) {
  2785. this._sort(dataArray);
  2786. }
  2787. var rst = [];
  2788. var invertPoint = coord.invertPoint(point);
  2789. var invertPointX = invertPoint.x;
  2790. if (self.isInCircle() && !coord.transposed && invertPointX > (1 + xScale.rangeMax()) / 2) {
  2791. invertPointX = xScale.rangeMin();
  2792. }
  2793. var xValue = xScale.invert(invertPointX);
  2794. if (!xScale.isCategory) {
  2795. xValue = self._getSnap(xScale, xValue);
  2796. }
  2797. var tmp = [];
  2798. dataArray.forEach(function (data) {
  2799. data.forEach(function (obj) {
  2800. var originValue = isNil(obj[FIELD_ORIGIN]) ? obj[xfield] : obj[FIELD_ORIGIN][xfield];
  2801. if (self._isEqual(originValue, xValue, xScale)) {
  2802. tmp.push(obj);
  2803. }
  2804. });
  2805. }); // special for pie chart
  2806. if (this.hasAdjust('stack') && coord.isPolar && coord.transposed) {
  2807. if (invertPointX >= 0 && invertPointX <= 1) {
  2808. var yValue = yScale.invert(invertPoint.y);
  2809. yValue = self._getSnap(yScale, yValue, tmp);
  2810. tmp.forEach(function (obj) {
  2811. if (isArray(yValue) ? obj[FIELD_ORIGIN_Y].toString() === yValue.toString() : obj[FIELD_ORIGIN_Y] === yValue) {
  2812. rst.push(obj);
  2813. }
  2814. });
  2815. }
  2816. } else {
  2817. rst = tmp;
  2818. }
  2819. return rst;
  2820. };
  2821. _proto.getRecords = function getRecords(value) {
  2822. var _this = this;
  2823. var xScale = this.getXScale();
  2824. var dataArray = this.get('dataArray');
  2825. var xfield = xScale.field;
  2826. return dataArray.map(function (data) {
  2827. for (var len = data.length, i = len - 1; i >= 0; i--) {
  2828. var obj = data[i];
  2829. var originValue = isNil(obj[FIELD_ORIGIN]) ? obj[xfield] : obj[FIELD_ORIGIN][xfield];
  2830. if (_this._isEqual(originValue, value, xScale)) {
  2831. return obj;
  2832. }
  2833. }
  2834. return null;
  2835. });
  2836. };
  2837. _proto._isEqual = function _isEqual(originValue, value, scale) {
  2838. if (scale.type === 'timeCat') {
  2839. return toTimeStamp(originValue) === value;
  2840. }
  2841. return value === originValue;
  2842. };
  2843. _proto.position = function position(field) {
  2844. this._setAttrOptions('position', {
  2845. field: field
  2846. });
  2847. return this;
  2848. };
  2849. _proto.color = function color(field, values) {
  2850. this._createAttrOption('color', field, values, Global.colors);
  2851. return this;
  2852. };
  2853. _proto.size = function size(field, values) {
  2854. this._createAttrOption('size', field, values, Global.sizes);
  2855. return this;
  2856. };
  2857. _proto.shape = function shape(field, values) {
  2858. var type = this.get('type');
  2859. var shapes = Global.shapes[type] || [];
  2860. this._createAttrOption('shape', field, values, shapes);
  2861. return this;
  2862. };
  2863. _proto.style = function style(field, cfg) {
  2864. var styleOptions = this.get('styleOptions');
  2865. if (!styleOptions) {
  2866. styleOptions = {};
  2867. this.set('styleOptions', styleOptions);
  2868. }
  2869. if (isObject(field)) {
  2870. cfg = field;
  2871. field = null;
  2872. }
  2873. var fields;
  2874. if (field) {
  2875. fields = parseFields(field);
  2876. }
  2877. styleOptions.fields = fields;
  2878. styleOptions.style = cfg;
  2879. return this;
  2880. };
  2881. _proto.adjust = function adjust(type) {
  2882. if (isString(type)) {
  2883. type = {
  2884. type: type
  2885. };
  2886. }
  2887. this.set('adjust', type);
  2888. return this;
  2889. };
  2890. _proto.animate = function animate(cfg) {
  2891. this.set('animateCfg', cfg);
  2892. return this;
  2893. };
  2894. _proto.changeData = function changeData(data) {
  2895. this.set('data', data); // 改变数据后,情况度量,因为需要重新实例化
  2896. this.set('scales', {});
  2897. if (!this.get('isInit')) return;
  2898. this.set('isInit', false);
  2899. this.init();
  2900. };
  2901. _proto.clearInner = function clearInner() {
  2902. var container = this.get('container');
  2903. if (container) {
  2904. container.clear(); // container.setMatrix([ 1, 0, 0, 1, 0, 0 ]);
  2905. }
  2906. };
  2907. _proto.reset = function reset() {
  2908. this.set('isInit', false);
  2909. this.set('attrs', {});
  2910. this.set('attrOptions', {});
  2911. this.set('adjust', null);
  2912. this.clearInner();
  2913. };
  2914. _proto.clear = function clear() {
  2915. this.clearInner();
  2916. };
  2917. _proto.destroy = function destroy() {
  2918. this.set('isInit', false);
  2919. this.clear();
  2920. _Base.prototype.destroy.call(this);
  2921. };
  2922. _proto._display = function _display(visible) {
  2923. this.set('visible', visible);
  2924. var container = this.get('container');
  2925. var canvas = container.get('canvas');
  2926. container.set('visible', visible);
  2927. canvas.draw();
  2928. };
  2929. _proto.show = function show() {
  2930. this._display(true);
  2931. };
  2932. _proto.hide = function hide() {
  2933. this._display(false);
  2934. };
  2935. return Geom;
  2936. }(Base);
  2937. var methodCache = {};
  2938. /**
  2939. * 获取计算 ticks 的方法
  2940. * @param key 键值
  2941. * @returns 计算 ticks 的方法
  2942. */
  2943. function getTickMethod(key) {
  2944. return methodCache[key];
  2945. }
  2946. /**
  2947. * 注册计算 ticks 的方法
  2948. * @param key 键值
  2949. * @param method 方法
  2950. */
  2951. function registerTickMethod(key, method) {
  2952. methodCache[key] = method;
  2953. }
  2954. var Scale =
  2955. /** @class */
  2956. function () {
  2957. function Scale(cfg) {
  2958. /**
  2959. * 度量的类型
  2960. */
  2961. this.type = 'base';
  2962. /**
  2963. * 是否分类类型的度量
  2964. */
  2965. this.isCategory = false;
  2966. /**
  2967. * 是否线性度量,有linear, time 度量
  2968. */
  2969. this.isLinear = false;
  2970. /**
  2971. * 是否连续类型的度量,linear,time,log, pow, quantile, quantize 都支持
  2972. */
  2973. this.isContinuous = false;
  2974. /**
  2975. * 是否是常量的度量,传入和传出一致
  2976. */
  2977. this.isIdentity = false;
  2978. this.values = [];
  2979. this.range = [0, 1];
  2980. this.ticks = [];
  2981. this.__cfg__ = cfg;
  2982. this.initCfg();
  2983. this.init();
  2984. } // 对于原始值的必要转换,如分类、时间字段需转换成数值,用transform/map命名可能更好
  2985. Scale.prototype.translate = function (v) {
  2986. return v;
  2987. };
  2988. /** 重新初始化 */
  2989. Scale.prototype.change = function (cfg) {
  2990. // 覆盖配置项,而不替代
  2991. mix(this.__cfg__, cfg);
  2992. this.init();
  2993. };
  2994. Scale.prototype.clone = function () {
  2995. return this.constructor(this.__cfg__);
  2996. };
  2997. /** 获取坐标轴需要的ticks */
  2998. Scale.prototype.getTicks = function () {
  2999. var _this = this;
  3000. return map(this.ticks, function (tick, idx) {
  3001. if (isObject(tick)) {
  3002. // 仅当符合Tick类型时才有意义
  3003. return tick;
  3004. }
  3005. return {
  3006. text: _this.getText(tick, idx),
  3007. tickValue: tick,
  3008. value: _this.scale(tick)
  3009. };
  3010. });
  3011. };
  3012. /** 获取Tick的格式化结果 */
  3013. Scale.prototype.getText = function (value, key) {
  3014. var formatter = this.formatter;
  3015. var res = formatter ? formatter(value, key) : value;
  3016. if (isNil(res) || !isFunction(res.toString)) {
  3017. return '';
  3018. }
  3019. return res.toString();
  3020. }; // 获取配置项中的值,当前 scale 上的值可能会被修改
  3021. Scale.prototype.getConfig = function (key) {
  3022. return this.__cfg__[key];
  3023. }; // scale初始化
  3024. Scale.prototype.init = function () {
  3025. mix(this, this.__cfg__);
  3026. this.setDomain();
  3027. if (isEmpty(this.getConfig('ticks'))) {
  3028. this.ticks = this.calculateTicks();
  3029. }
  3030. }; // 子类上覆盖某些属性,不能直接在类上声明,否则会被覆盖
  3031. Scale.prototype.initCfg = function () {};
  3032. Scale.prototype.setDomain = function () {};
  3033. Scale.prototype.calculateTicks = function () {
  3034. var tickMethod = this.tickMethod;
  3035. var ticks = [];
  3036. if (isString(tickMethod)) {
  3037. var method = getTickMethod(tickMethod);
  3038. if (!method) {
  3039. throw new Error('There is no method to to calculate ticks!');
  3040. }
  3041. ticks = method(this);
  3042. } else if (isFunction(tickMethod)) {
  3043. ticks = tickMethod(this);
  3044. }
  3045. return ticks;
  3046. }; // range 的最小值
  3047. Scale.prototype.rangeMin = function () {
  3048. return head(this.range);
  3049. }; // range 的最大值
  3050. Scale.prototype.rangeMax = function () {
  3051. return last(this.range);
  3052. };
  3053. /** 定义域转 0~1 */
  3054. Scale.prototype.calcPercent = function (value, min, max) {
  3055. if (isNumber(value)) {
  3056. return (value - min) / (max - min);
  3057. }
  3058. return NaN;
  3059. };
  3060. /** 0~1转定义域 */
  3061. Scale.prototype.calcValue = function (percent, min, max) {
  3062. return min + percent * (max - min);
  3063. };
  3064. return Scale;
  3065. }();
  3066. /*! *****************************************************************************
  3067. Copyright (c) Microsoft Corporation.
  3068. Permission to use, copy, modify, and/or distribute this software for any
  3069. purpose with or without fee is hereby granted.
  3070. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  3071. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  3072. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  3073. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  3074. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  3075. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  3076. PERFORMANCE OF THIS SOFTWARE.
  3077. ***************************************************************************** */
  3078. /* global Reflect, Promise */
  3079. var extendStatics = function (d, b) {
  3080. extendStatics = Object.setPrototypeOf || {
  3081. __proto__: []
  3082. } instanceof Array && function (d, b) {
  3083. d.__proto__ = b;
  3084. } || function (d, b) {
  3085. for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
  3086. };
  3087. return extendStatics(d, b);
  3088. };
  3089. function __extends(d, b) {
  3090. extendStatics(d, b);
  3091. function __() {
  3092. this.constructor = d;
  3093. }
  3094. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  3095. }
  3096. function __spreadArrays() {
  3097. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  3098. for (var r = Array(s), k = 0, i = 0; i < il; i++) for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) r[k] = a[j];
  3099. return r;
  3100. }
  3101. /**
  3102. * 分类度量
  3103. * @class
  3104. */
  3105. var Category =
  3106. /** @class */
  3107. function (_super) {
  3108. __extends(Category, _super);
  3109. function Category() {
  3110. var _this = _super !== null && _super.apply(this, arguments) || this;
  3111. _this.type = 'cat';
  3112. _this.isCategory = true;
  3113. return _this;
  3114. }
  3115. Category.prototype.translate = function (value) {
  3116. var index = indexOf(this.values, value);
  3117. if (index === -1) {
  3118. return isNumber(value) ? value : NaN;
  3119. }
  3120. return index;
  3121. };
  3122. Category.prototype.scale = function (value) {
  3123. var order = this.translate(value); // 分类数据允许 0.5 范围内调整
  3124. // if (order < this.min - 0.5 || order > this.max + 0.5) {
  3125. // return NaN;
  3126. // }
  3127. var percent = this.calcPercent(order, this.min, this.max);
  3128. return this.calcValue(percent, this.rangeMin(), this.rangeMax());
  3129. };
  3130. Category.prototype.invert = function (scaledValue) {
  3131. var domainRange = this.max - this.min;
  3132. var percent = this.calcPercent(scaledValue, this.rangeMin(), this.rangeMax());
  3133. var idx = Math.round(domainRange * percent) + this.min;
  3134. if (idx < this.min || idx > this.max) {
  3135. return NaN;
  3136. }
  3137. return this.values[idx];
  3138. };
  3139. Category.prototype.getText = function (value) {
  3140. var args = [];
  3141. for (var _i = 1; _i < arguments.length; _i++) {
  3142. args[_i - 1] = arguments[_i];
  3143. }
  3144. var v = value; // value为index
  3145. if (isNumber(value) && !this.values.includes(value)) {
  3146. v = this.values[v];
  3147. }
  3148. return _super.prototype.getText.apply(this, __spreadArrays([v], args));
  3149. }; // 复写属性
  3150. Category.prototype.initCfg = function () {
  3151. this.tickMethod = 'cat';
  3152. }; // 设置 min, max
  3153. Category.prototype.setDomain = function () {
  3154. // 用户有可能设置 min
  3155. if (isNil(this.getConfig('min'))) {
  3156. this.min = 0;
  3157. }
  3158. if (isNil(this.getConfig('max'))) {
  3159. var size = this.values.length;
  3160. this.max = size > 1 ? size - 1 : size;
  3161. }
  3162. };
  3163. return Category;
  3164. }(Scale);
  3165. var token = /d{1,4}|M{1,4}|YY(?:YY)?|S{1,3}|Do|ZZ|Z|([HhMsDm])\1?|[aA]|"[^"]*"|'[^']*'/g;
  3166. var twoDigitsOptional = "[1-9]\\d?";
  3167. var twoDigits = "\\d\\d";
  3168. var threeDigits = "\\d{3}";
  3169. var fourDigits = "\\d{4}";
  3170. var word = "[^\\s]+";
  3171. var literal = /\[([^]*?)\]/gm;
  3172. function shorten(arr, sLen) {
  3173. var newArr = [];
  3174. for (var i = 0, len = arr.length; i < len; i++) {
  3175. newArr.push(arr[i].substr(0, sLen));
  3176. }
  3177. return newArr;
  3178. }
  3179. var monthUpdate = function (arrName) {
  3180. return function (v, i18n) {
  3181. var lowerCaseArr = i18n[arrName].map(function (v) {
  3182. return v.toLowerCase();
  3183. });
  3184. var index = lowerCaseArr.indexOf(v.toLowerCase());
  3185. if (index > -1) {
  3186. return index;
  3187. }
  3188. return null;
  3189. };
  3190. };
  3191. function assign(origObj) {
  3192. var args = [];
  3193. for (var _i = 1; _i < arguments.length; _i++) {
  3194. args[_i - 1] = arguments[_i];
  3195. }
  3196. for (var _a = 0, args_1 = args; _a < args_1.length; _a++) {
  3197. var obj = args_1[_a];
  3198. for (var key in obj) {
  3199. // @ts-ignore ex
  3200. origObj[key] = obj[key];
  3201. }
  3202. }
  3203. return origObj;
  3204. }
  3205. var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  3206. var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  3207. var monthNamesShort = shorten(monthNames, 3);
  3208. var dayNamesShort = shorten(dayNames, 3);
  3209. var defaultI18n = {
  3210. dayNamesShort: dayNamesShort,
  3211. dayNames: dayNames,
  3212. monthNamesShort: monthNamesShort,
  3213. monthNames: monthNames,
  3214. amPm: ["am", "pm"],
  3215. DoFn: function (dayOfMonth) {
  3216. return dayOfMonth + ["th", "st", "nd", "rd"][dayOfMonth % 10 > 3 ? 0 : (dayOfMonth - dayOfMonth % 10 !== 10 ? 1 : 0) * dayOfMonth % 10];
  3217. }
  3218. };
  3219. var globalI18n = assign({}, defaultI18n);
  3220. var setGlobalDateI18n = function (i18n) {
  3221. return globalI18n = assign(globalI18n, i18n);
  3222. };
  3223. var regexEscape = function (str) {
  3224. return str.replace(/[|\\{()[^$+*?.-]/g, "\\$&");
  3225. };
  3226. var pad = function (val, len) {
  3227. if (len === void 0) {
  3228. len = 2;
  3229. }
  3230. val = String(val);
  3231. while (val.length < len) {
  3232. val = "0" + val;
  3233. }
  3234. return val;
  3235. };
  3236. var formatFlags = {
  3237. D: function (dateObj) {
  3238. return String(dateObj.getDate());
  3239. },
  3240. DD: function (dateObj) {
  3241. return pad(dateObj.getDate());
  3242. },
  3243. Do: function (dateObj, i18n) {
  3244. return i18n.DoFn(dateObj.getDate());
  3245. },
  3246. d: function (dateObj) {
  3247. return String(dateObj.getDay());
  3248. },
  3249. dd: function (dateObj) {
  3250. return pad(dateObj.getDay());
  3251. },
  3252. ddd: function (dateObj, i18n) {
  3253. return i18n.dayNamesShort[dateObj.getDay()];
  3254. },
  3255. dddd: function (dateObj, i18n) {
  3256. return i18n.dayNames[dateObj.getDay()];
  3257. },
  3258. M: function (dateObj) {
  3259. return String(dateObj.getMonth() + 1);
  3260. },
  3261. MM: function (dateObj) {
  3262. return pad(dateObj.getMonth() + 1);
  3263. },
  3264. MMM: function (dateObj, i18n) {
  3265. return i18n.monthNamesShort[dateObj.getMonth()];
  3266. },
  3267. MMMM: function (dateObj, i18n) {
  3268. return i18n.monthNames[dateObj.getMonth()];
  3269. },
  3270. YY: function (dateObj) {
  3271. return pad(String(dateObj.getFullYear()), 4).substr(2);
  3272. },
  3273. YYYY: function (dateObj) {
  3274. return pad(dateObj.getFullYear(), 4);
  3275. },
  3276. h: function (dateObj) {
  3277. return String(dateObj.getHours() % 12 || 12);
  3278. },
  3279. hh: function (dateObj) {
  3280. return pad(dateObj.getHours() % 12 || 12);
  3281. },
  3282. H: function (dateObj) {
  3283. return String(dateObj.getHours());
  3284. },
  3285. HH: function (dateObj) {
  3286. return pad(dateObj.getHours());
  3287. },
  3288. m: function (dateObj) {
  3289. return String(dateObj.getMinutes());
  3290. },
  3291. mm: function (dateObj) {
  3292. return pad(dateObj.getMinutes());
  3293. },
  3294. s: function (dateObj) {
  3295. return String(dateObj.getSeconds());
  3296. },
  3297. ss: function (dateObj) {
  3298. return pad(dateObj.getSeconds());
  3299. },
  3300. S: function (dateObj) {
  3301. return String(Math.round(dateObj.getMilliseconds() / 100));
  3302. },
  3303. SS: function (dateObj) {
  3304. return pad(Math.round(dateObj.getMilliseconds() / 10), 2);
  3305. },
  3306. SSS: function (dateObj) {
  3307. return pad(dateObj.getMilliseconds(), 3);
  3308. },
  3309. a: function (dateObj, i18n) {
  3310. return dateObj.getHours() < 12 ? i18n.amPm[0] : i18n.amPm[1];
  3311. },
  3312. A: function (dateObj, i18n) {
  3313. return dateObj.getHours() < 12 ? i18n.amPm[0].toUpperCase() : i18n.amPm[1].toUpperCase();
  3314. },
  3315. ZZ: function (dateObj) {
  3316. var offset = dateObj.getTimezoneOffset();
  3317. return (offset > 0 ? "-" : "+") + pad(Math.floor(Math.abs(offset) / 60) * 100 + Math.abs(offset) % 60, 4);
  3318. },
  3319. Z: function (dateObj) {
  3320. var offset = dateObj.getTimezoneOffset();
  3321. return (offset > 0 ? "-" : "+") + pad(Math.floor(Math.abs(offset) / 60), 2) + ":" + pad(Math.abs(offset) % 60, 2);
  3322. }
  3323. };
  3324. var monthParse = function (v) {
  3325. return +v - 1;
  3326. };
  3327. var emptyDigits = [null, twoDigitsOptional];
  3328. var emptyWord = [null, word];
  3329. var amPm = ["isPm", word, function (v, i18n) {
  3330. var val = v.toLowerCase();
  3331. if (val === i18n.amPm[0]) {
  3332. return 0;
  3333. } else if (val === i18n.amPm[1]) {
  3334. return 1;
  3335. }
  3336. return null;
  3337. }];
  3338. var timezoneOffset = ["timezoneOffset", "[^\\s]*?[\\+\\-]\\d\\d:?\\d\\d|[^\\s]*?Z?", function (v) {
  3339. var parts = (v + "").match(/([+-]|\d\d)/gi);
  3340. if (parts) {
  3341. var minutes = +parts[1] * 60 + parseInt(parts[2], 10);
  3342. return parts[0] === "+" ? minutes : -minutes;
  3343. }
  3344. return 0;
  3345. }];
  3346. var parseFlags = {
  3347. D: ["day", twoDigitsOptional],
  3348. DD: ["day", twoDigits],
  3349. Do: ["day", twoDigitsOptional + word, function (v) {
  3350. return parseInt(v, 10);
  3351. }],
  3352. M: ["month", twoDigitsOptional, monthParse],
  3353. MM: ["month", twoDigits, monthParse],
  3354. YY: ["year", twoDigits, function (v) {
  3355. var now = new Date();
  3356. var cent = +("" + now.getFullYear()).substr(0, 2);
  3357. return +("" + (+v > 68 ? cent - 1 : cent) + v);
  3358. }],
  3359. h: ["hour", twoDigitsOptional, undefined, "isPm"],
  3360. hh: ["hour", twoDigits, undefined, "isPm"],
  3361. H: ["hour", twoDigitsOptional],
  3362. HH: ["hour", twoDigits],
  3363. m: ["minute", twoDigitsOptional],
  3364. mm: ["minute", twoDigits],
  3365. s: ["second", twoDigitsOptional],
  3366. ss: ["second", twoDigits],
  3367. YYYY: ["year", fourDigits],
  3368. S: ["millisecond", "\\d", function (v) {
  3369. return +v * 100;
  3370. }],
  3371. SS: ["millisecond", twoDigits, function (v) {
  3372. return +v * 10;
  3373. }],
  3374. SSS: ["millisecond", threeDigits],
  3375. d: emptyDigits,
  3376. dd: emptyDigits,
  3377. ddd: emptyWord,
  3378. dddd: emptyWord,
  3379. MMM: ["month", word, monthUpdate("monthNamesShort")],
  3380. MMMM: ["month", word, monthUpdate("monthNames")],
  3381. a: amPm,
  3382. A: amPm,
  3383. ZZ: timezoneOffset,
  3384. Z: timezoneOffset
  3385. }; // Some common format strings
  3386. var globalMasks = {
  3387. default: "ddd MMM DD YYYY HH:mm:ss",
  3388. shortDate: "M/D/YY",
  3389. mediumDate: "MMM D, YYYY",
  3390. longDate: "MMMM D, YYYY",
  3391. fullDate: "dddd, MMMM D, YYYY",
  3392. isoDate: "YYYY-MM-DD",
  3393. isoDateTime: "YYYY-MM-DDTHH:mm:ssZ",
  3394. shortTime: "HH:mm",
  3395. mediumTime: "HH:mm:ss",
  3396. longTime: "HH:mm:ss.SSS"
  3397. };
  3398. var setGlobalDateMasks = function (masks) {
  3399. return assign(globalMasks, masks);
  3400. };
  3401. /***
  3402. * Format a date
  3403. * @method format
  3404. * @param {Date|number} dateObj
  3405. * @param {string} mask Format of the date, i.e. 'mm-dd-yy' or 'shortDate'
  3406. * @returns {string} Formatted date string
  3407. */
  3408. var format = function (dateObj, mask, i18n) {
  3409. if (mask === void 0) {
  3410. mask = globalMasks["default"];
  3411. }
  3412. if (i18n === void 0) {
  3413. i18n = {};
  3414. }
  3415. if (typeof dateObj === "number") {
  3416. dateObj = new Date(dateObj);
  3417. }
  3418. if (Object.prototype.toString.call(dateObj) !== "[object Date]" || isNaN(dateObj.getTime())) {
  3419. throw new Error("Invalid Date pass to format");
  3420. }
  3421. mask = globalMasks[mask] || mask;
  3422. var literals = []; // Make literals inactive by replacing them with @@@
  3423. mask = mask.replace(literal, function ($0, $1) {
  3424. literals.push($1);
  3425. return "@@@";
  3426. });
  3427. var combinedI18nSettings = assign(assign({}, globalI18n), i18n); // Apply formatting rules
  3428. mask = mask.replace(token, function ($0) {
  3429. return formatFlags[$0](dateObj, combinedI18nSettings);
  3430. }); // Inline literal values back into the formatted value
  3431. return mask.replace(/@@@/g, function () {
  3432. return literals.shift();
  3433. });
  3434. };
  3435. /**
  3436. * Parse a date string into a Javascript Date object /
  3437. * @method parse
  3438. * @param {string} dateStr Date string
  3439. * @param {string} format Date parse format
  3440. * @param {i18n} I18nSettingsOptional Full or subset of I18N settings
  3441. * @returns {Date|null} Returns Date object. Returns null what date string is invalid or doesn't match format
  3442. */
  3443. function parse(dateStr, format, i18n) {
  3444. if (i18n === void 0) {
  3445. i18n = {};
  3446. }
  3447. if (typeof format !== "string") {
  3448. throw new Error("Invalid format in fecha parse");
  3449. } // Check to see if the format is actually a mask
  3450. format = globalMasks[format] || format; // Avoid regular expression denial of service, fail early for really long strings
  3451. // https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS
  3452. if (dateStr.length > 1000) {
  3453. return null;
  3454. } // Default to the beginning of the year.
  3455. var today = new Date();
  3456. var dateInfo = {
  3457. year: today.getFullYear(),
  3458. month: 0,
  3459. day: 1,
  3460. hour: 0,
  3461. minute: 0,
  3462. second: 0,
  3463. millisecond: 0,
  3464. isPm: null,
  3465. timezoneOffset: null
  3466. };
  3467. var parseInfo = [];
  3468. var literals = []; // Replace all the literals with @@@. Hopefully a string that won't exist in the format
  3469. var newFormat = format.replace(literal, function ($0, $1) {
  3470. literals.push(regexEscape($1));
  3471. return "@@@";
  3472. });
  3473. var specifiedFields = {};
  3474. var requiredFields = {}; // Change every token that we find into the correct regex
  3475. newFormat = regexEscape(newFormat).replace(token, function ($0) {
  3476. var info = parseFlags[$0];
  3477. var field = info[0],
  3478. regex = info[1],
  3479. requiredField = info[3]; // Check if the person has specified the same field twice. This will lead to confusing results.
  3480. if (specifiedFields[field]) {
  3481. throw new Error("Invalid format. " + field + " specified twice in format");
  3482. }
  3483. specifiedFields[field] = true; // Check if there are any required fields. For instance, 12 hour time requires AM/PM specified
  3484. if (requiredField) {
  3485. requiredFields[requiredField] = true;
  3486. }
  3487. parseInfo.push(info);
  3488. return "(" + regex + ")";
  3489. }); // Check all the required fields are present
  3490. Object.keys(requiredFields).forEach(function (field) {
  3491. if (!specifiedFields[field]) {
  3492. throw new Error("Invalid format. " + field + " is required in specified format");
  3493. }
  3494. }); // Add back all the literals after
  3495. newFormat = newFormat.replace(/@@@/g, function () {
  3496. return literals.shift();
  3497. }); // Check if the date string matches the format. If it doesn't return null
  3498. var matches = dateStr.match(new RegExp(newFormat, "i"));
  3499. if (!matches) {
  3500. return null;
  3501. }
  3502. var combinedI18nSettings = assign(assign({}, globalI18n), i18n); // For each match, call the parser function for that date part
  3503. for (var i = 1; i < matches.length; i++) {
  3504. var _a = parseInfo[i - 1],
  3505. field = _a[0],
  3506. parser = _a[2];
  3507. var value = parser ? parser(matches[i], combinedI18nSettings) : +matches[i]; // If the parser can't make sense of the value, return null
  3508. if (value == null) {
  3509. return null;
  3510. }
  3511. dateInfo[field] = value;
  3512. }
  3513. if (dateInfo.isPm === 1 && dateInfo.hour != null && +dateInfo.hour !== 12) {
  3514. dateInfo.hour = +dateInfo.hour + 12;
  3515. } else if (dateInfo.isPm === 0 && +dateInfo.hour === 12) {
  3516. dateInfo.hour = 0;
  3517. }
  3518. var dateWithoutTZ = new Date(dateInfo.year, dateInfo.month, dateInfo.day, dateInfo.hour, dateInfo.minute, dateInfo.second, dateInfo.millisecond);
  3519. var validateFields = [["month", "getMonth"], ["day", "getDate"], ["hour", "getHours"], ["minute", "getMinutes"], ["second", "getSeconds"]];
  3520. for (var i = 0, len = validateFields.length; i < len; i++) {
  3521. // Check to make sure the date field is within the allowed range. Javascript dates allows values
  3522. // outside the allowed range. If the values don't match the value was invalid
  3523. if (specifiedFields[validateFields[i][0]] && dateInfo[validateFields[i][0]] !== dateWithoutTZ[validateFields[i][1]]()) {
  3524. return null;
  3525. }
  3526. }
  3527. if (dateInfo.timezoneOffset == null) {
  3528. return dateWithoutTZ;
  3529. }
  3530. return new Date(Date.UTC(dateInfo.year, dateInfo.month, dateInfo.day, dateInfo.hour, dateInfo.minute - dateInfo.timezoneOffset, dateInfo.second, dateInfo.millisecond));
  3531. }
  3532. var fecha = {
  3533. format: format,
  3534. parse: parse,
  3535. defaultI18n: defaultI18n,
  3536. setGlobalDateI18n: setGlobalDateI18n,
  3537. setGlobalDateMasks: setGlobalDateMasks
  3538. };
  3539. var fecha1 = /*#__PURE__*/Object.freeze({
  3540. __proto__: null,
  3541. 'default': fecha,
  3542. assign: assign,
  3543. format: format,
  3544. parse: parse,
  3545. defaultI18n: defaultI18n,
  3546. setGlobalDateI18n: setGlobalDateI18n,
  3547. setGlobalDateMasks: setGlobalDateMasks
  3548. });
  3549. /**
  3550. * 二分右侧查找
  3551. * https://github.com/d3/d3-array/blob/master/src/bisector.js
  3552. */
  3553. function bisector (getter) {
  3554. /**
  3555. * x: 目标值
  3556. * lo: 起始位置
  3557. * hi: 结束位置
  3558. */
  3559. return function (a, x, _lo, _hi) {
  3560. var lo = isNil(_lo) ? 0 : _lo;
  3561. var hi = isNil(_hi) ? a.length : _hi;
  3562. while (lo < hi) {
  3563. var mid = lo + hi >>> 1;
  3564. if (getter(a[mid]) > x) {
  3565. hi = mid;
  3566. } else {
  3567. lo = mid + 1;
  3568. }
  3569. }
  3570. return lo;
  3571. };
  3572. }
  3573. var FORMAT_METHOD = 'format';
  3574. function timeFormat(time, mask) {
  3575. var method = fecha1[FORMAT_METHOD] || fecha[FORMAT_METHOD];
  3576. return method(time, mask);
  3577. }
  3578. /**
  3579. * 转换成时间戳
  3580. * @param value 时间值
  3581. */
  3582. function toTimeStamp$1(value) {
  3583. if (isString(value)) {
  3584. if (value.indexOf('T') > 0) {
  3585. value = new Date(value).getTime();
  3586. } else {
  3587. // new Date('2010/01/10') 和 new Date('2010-01-10') 的差别在于:
  3588. // 如果仅有年月日时,前者是带有时区的: Fri Jan 10 2020 02:40:13 GMT+0800 (中国标准时间)
  3589. // 后者会格式化成 Sun Jan 10 2010 08:00:00 GMT+0800 (中国标准时间)
  3590. value = new Date(value.replace(/-/gi, '/')).getTime();
  3591. }
  3592. }
  3593. if (isDate(value)) {
  3594. value = value.getTime();
  3595. }
  3596. return value;
  3597. }
  3598. var SECOND = 1000;
  3599. var MINUTE = 60 * SECOND;
  3600. var HOUR = 60 * MINUTE;
  3601. var DAY = 24 * HOUR;
  3602. var MONTH = DAY * 31;
  3603. var YEAR = DAY * 365;
  3604. var intervals = [['HH:mm:ss', SECOND], ['HH:mm:ss', SECOND * 10], ['HH:mm:ss', SECOND * 30], ['HH:mm', MINUTE], ['HH:mm', MINUTE * 10], ['HH:mm', MINUTE * 30], ['HH', HOUR], ['HH', HOUR * 6], ['HH', HOUR * 12], ['YYYY-MM-DD', DAY], ['YYYY-MM-DD', DAY * 4], ['YYYY-WW', DAY * 7], ['YYYY-MM', MONTH], ['YYYY-MM', MONTH * 4], ['YYYY-MM', MONTH * 6], ['YYYY', DAY * 380]];
  3605. function getTickInterval(min, max, tickCount) {
  3606. var target = (max - min) / tickCount;
  3607. var idx = bisector(function (o) {
  3608. return o[1];
  3609. })(intervals, target) - 1;
  3610. var interval = intervals[idx];
  3611. if (idx < 0) {
  3612. interval = intervals[0];
  3613. } else if (idx >= intervals.length) {
  3614. interval = last(intervals);
  3615. }
  3616. return interval;
  3617. }
  3618. /**
  3619. * 时间分类度量
  3620. * @class
  3621. */
  3622. var TimeCat =
  3623. /** @class */
  3624. function (_super) {
  3625. __extends(TimeCat, _super);
  3626. function TimeCat() {
  3627. var _this = _super !== null && _super.apply(this, arguments) || this;
  3628. _this.type = 'timeCat';
  3629. return _this;
  3630. }
  3631. /**
  3632. * @override
  3633. */
  3634. TimeCat.prototype.translate = function (value) {
  3635. value = toTimeStamp$1(value);
  3636. var index = this.values.indexOf(value);
  3637. if (index === -1) {
  3638. if (isNumber(value) && value < this.values.length) {
  3639. index = value;
  3640. } else {
  3641. index = NaN;
  3642. }
  3643. }
  3644. return index;
  3645. };
  3646. /**
  3647. * 由于时间类型数据需要转换一下,所以复写 getText
  3648. * @override
  3649. */
  3650. TimeCat.prototype.getText = function (value, tickIndex) {
  3651. var index = this.translate(value);
  3652. if (index > -1) {
  3653. var result = this.values[index];
  3654. var formatter = this.formatter;
  3655. result = formatter ? formatter(result, tickIndex) : timeFormat(result, this.mask);
  3656. return result;
  3657. }
  3658. return value;
  3659. };
  3660. TimeCat.prototype.initCfg = function () {
  3661. this.tickMethod = 'time-cat';
  3662. this.mask = 'YYYY-MM-DD';
  3663. this.tickCount = 7; // 一般时间数据会显示 7, 14, 30 天的数字
  3664. };
  3665. TimeCat.prototype.setDomain = function () {
  3666. var values = this.values; // 针对时间分类类型,会将时间统一转换为时间戳
  3667. each(values, function (v, i) {
  3668. values[i] = toTimeStamp$1(v);
  3669. });
  3670. values.sort(function (v1, v2) {
  3671. return v1 - v2;
  3672. });
  3673. _super.prototype.setDomain.call(this);
  3674. };
  3675. return TimeCat;
  3676. }(Category);
  3677. /**
  3678. * 连续度量的基类
  3679. * @class
  3680. */
  3681. var Continuous =
  3682. /** @class */
  3683. function (_super) {
  3684. __extends(Continuous, _super);
  3685. function Continuous() {
  3686. var _this = _super !== null && _super.apply(this, arguments) || this;
  3687. _this.isContinuous = true;
  3688. return _this;
  3689. }
  3690. Continuous.prototype.scale = function (value) {
  3691. if (isNil(value)) {
  3692. return NaN;
  3693. }
  3694. var rangeMin = this.rangeMin();
  3695. var rangeMax = this.rangeMax();
  3696. var max = this.max;
  3697. var min = this.min;
  3698. if (max === min) {
  3699. return rangeMin;
  3700. }
  3701. var percent = this.getScalePercent(value);
  3702. return rangeMin + percent * (rangeMax - rangeMin);
  3703. };
  3704. Continuous.prototype.init = function () {
  3705. _super.prototype.init.call(this); // init 完成后保证 min, max 包含 ticks 的范围
  3706. var ticks = this.ticks;
  3707. var firstTick = head(ticks);
  3708. var lastTick = last(ticks);
  3709. if (firstTick < this.min) {
  3710. this.min = firstTick;
  3711. }
  3712. if (lastTick > this.max) {
  3713. this.max = lastTick;
  3714. } // strict-limit 方式
  3715. if (!isNil(this.minLimit)) {
  3716. this.min = firstTick;
  3717. }
  3718. if (!isNil(this.maxLimit)) {
  3719. this.max = lastTick;
  3720. }
  3721. };
  3722. Continuous.prototype.setDomain = function () {
  3723. var _a = getRange(this.values),
  3724. min = _a.min,
  3725. max = _a.max;
  3726. if (isNil(this.min)) {
  3727. this.min = min;
  3728. }
  3729. if (isNil(this.max)) {
  3730. this.max = max;
  3731. }
  3732. if (this.min > this.max) {
  3733. this.min = min;
  3734. this.max = max;
  3735. }
  3736. };
  3737. Continuous.prototype.calculateTicks = function () {
  3738. var _this = this;
  3739. var ticks = _super.prototype.calculateTicks.call(this);
  3740. if (!this.nice) {
  3741. ticks = filter(ticks, function (tick) {
  3742. return tick >= _this.min && tick <= _this.max;
  3743. });
  3744. }
  3745. return ticks;
  3746. }; // 计算原始值值占的百分比
  3747. Continuous.prototype.getScalePercent = function (value) {
  3748. var max = this.max;
  3749. var min = this.min;
  3750. return (value - min) / (max - min);
  3751. };
  3752. Continuous.prototype.getInvertPercent = function (value) {
  3753. return (value - this.rangeMin()) / (this.rangeMax() - this.rangeMin());
  3754. };
  3755. return Continuous;
  3756. }(Scale);
  3757. /**
  3758. * 线性度量
  3759. * @class
  3760. */
  3761. var Linear =
  3762. /** @class */
  3763. function (_super) {
  3764. __extends(Linear, _super);
  3765. function Linear() {
  3766. var _this = _super !== null && _super.apply(this, arguments) || this;
  3767. _this.type = 'linear';
  3768. _this.isLinear = true;
  3769. return _this;
  3770. }
  3771. Linear.prototype.invert = function (value) {
  3772. var percent = this.getInvertPercent(value);
  3773. return this.min + percent * (this.max - this.min);
  3774. };
  3775. Linear.prototype.initCfg = function () {
  3776. this.tickMethod = 'wilkinson-extended';
  3777. this.nice = false;
  3778. };
  3779. return Linear;
  3780. }(Continuous);
  3781. // 虽然数学上 b 不支持负数,但是这里需要支持 负数
  3782. function calBase(a, b) {
  3783. var e = Math.E;
  3784. var value;
  3785. if (b >= 0) {
  3786. value = Math.pow(e, Math.log(b) / a); // 使用换底公式求底
  3787. } else {
  3788. value = Math.pow(e, Math.log(-b) / a) * -1; // 使用换底公式求底
  3789. }
  3790. return value;
  3791. }
  3792. function log(a, b) {
  3793. if (a === 1) {
  3794. return 1;
  3795. }
  3796. return Math.log(b) / Math.log(a);
  3797. }
  3798. function getLogPositiveMin(values, base, max) {
  3799. if (isNil(max)) {
  3800. max = Math.max.apply(null, values);
  3801. }
  3802. var positiveMin = max;
  3803. each(values, function (value) {
  3804. if (value > 0 && value < positiveMin) {
  3805. positiveMin = value;
  3806. }
  3807. });
  3808. if (positiveMin === max) {
  3809. positiveMin = max / base;
  3810. }
  3811. if (positiveMin > 1) {
  3812. positiveMin = 1;
  3813. }
  3814. return positiveMin;
  3815. }
  3816. /**
  3817. * Log 度量,处理非均匀分布
  3818. */
  3819. var Log =
  3820. /** @class */
  3821. function (_super) {
  3822. __extends(Log, _super);
  3823. function Log() {
  3824. var _this = _super !== null && _super.apply(this, arguments) || this;
  3825. _this.type = 'log';
  3826. return _this;
  3827. }
  3828. /**
  3829. * @override
  3830. */
  3831. Log.prototype.invert = function (value) {
  3832. var base = this.base;
  3833. var max = log(base, this.max);
  3834. var rangeMin = this.rangeMin();
  3835. var range = this.rangeMax() - rangeMin;
  3836. var min;
  3837. var positiveMin = this.positiveMin;
  3838. if (positiveMin) {
  3839. if (value === 0) {
  3840. return 0;
  3841. }
  3842. min = log(base, positiveMin / base);
  3843. var appendPercent = 1 / (max - min) * range; // 0 到 positiveMin的占比
  3844. if (value < appendPercent) {
  3845. // 落到 0 - positiveMin 之间
  3846. return value / appendPercent * positiveMin;
  3847. }
  3848. } else {
  3849. min = log(base, this.min);
  3850. }
  3851. var percent = (value - rangeMin) / range;
  3852. var tmp = percent * (max - min) + min;
  3853. return Math.pow(base, tmp);
  3854. };
  3855. Log.prototype.initCfg = function () {
  3856. this.tickMethod = 'log';
  3857. this.base = 10;
  3858. this.tickCount = 6;
  3859. this.nice = true;
  3860. }; // 设置
  3861. Log.prototype.setDomain = function () {
  3862. _super.prototype.setDomain.call(this);
  3863. var min = this.min;
  3864. if (min < 0) {
  3865. throw new Error('When you use log scale, the minimum value must be greater than zero!');
  3866. }
  3867. if (min === 0) {
  3868. this.positiveMin = getLogPositiveMin(this.values, this.base, this.max);
  3869. }
  3870. }; // 根据当前值获取占比
  3871. Log.prototype.getScalePercent = function (value) {
  3872. var max = this.max;
  3873. var min = this.min;
  3874. if (max === min) {
  3875. return 0;
  3876. } // 如果值小于等于0,则按照0处理
  3877. if (value <= 0) {
  3878. return 0;
  3879. }
  3880. var base = this.base;
  3881. var positiveMin = this.positiveMin; // 如果min == 0, 则根据比0大的最小值,计算比例关系。这个最小值作为坐标轴上的第二个tick,第一个是0但是不显示
  3882. if (positiveMin) {
  3883. min = positiveMin * 1 / base;
  3884. }
  3885. var percent; // 如果数值小于次小值,那么就计算 value / 次小值 占整体的比例
  3886. if (value < positiveMin) {
  3887. percent = value / positiveMin / (log(base, max) - log(base, min));
  3888. } else {
  3889. percent = (log(base, value) - log(base, min)) / (log(base, max) - log(base, min));
  3890. }
  3891. return percent;
  3892. };
  3893. return Log;
  3894. }(Continuous);
  3895. /**
  3896. * Pow 度量,处理非均匀分布
  3897. */
  3898. var Pow =
  3899. /** @class */
  3900. function (_super) {
  3901. __extends(Pow, _super);
  3902. function Pow() {
  3903. var _this = _super !== null && _super.apply(this, arguments) || this;
  3904. _this.type = 'pow';
  3905. return _this;
  3906. }
  3907. /**
  3908. * @override
  3909. */
  3910. Pow.prototype.invert = function (value) {
  3911. var percent = this.getInvertPercent(value);
  3912. var exponent = this.exponent;
  3913. var max = calBase(exponent, this.max);
  3914. var min = calBase(exponent, this.min);
  3915. var tmp = percent * (max - min) + min;
  3916. var factor = tmp >= 0 ? 1 : -1;
  3917. return Math.pow(tmp, exponent) * factor;
  3918. };
  3919. Pow.prototype.initCfg = function () {
  3920. this.tickMethod = 'pow';
  3921. this.exponent = 2;
  3922. this.tickCount = 5;
  3923. this.nice = true;
  3924. }; // 获取度量计算时,value占的定义域百分比
  3925. Pow.prototype.getScalePercent = function (value) {
  3926. var max = this.max;
  3927. var min = this.min;
  3928. if (max === min) {
  3929. return 0;
  3930. }
  3931. var exponent = this.exponent;
  3932. var percent = (calBase(exponent, value) - calBase(exponent, min)) / (calBase(exponent, max) - calBase(exponent, min));
  3933. return percent;
  3934. };
  3935. return Pow;
  3936. }(Continuous);
  3937. /**
  3938. * 时间度量
  3939. * @class
  3940. */
  3941. var Time =
  3942. /** @class */
  3943. function (_super) {
  3944. __extends(Time, _super);
  3945. function Time() {
  3946. var _this = _super !== null && _super.apply(this, arguments) || this;
  3947. _this.type = 'time';
  3948. return _this;
  3949. }
  3950. /**
  3951. * @override
  3952. */
  3953. Time.prototype.getText = function (value, index) {
  3954. var numberValue = this.translate(value);
  3955. var formatter = this.formatter;
  3956. return formatter ? formatter(numberValue, index) : timeFormat(numberValue, this.mask);
  3957. };
  3958. /**
  3959. * @override
  3960. */
  3961. Time.prototype.scale = function (value) {
  3962. var v = value;
  3963. if (isString(v) || isDate(v)) {
  3964. v = this.translate(v);
  3965. }
  3966. return _super.prototype.scale.call(this, v);
  3967. };
  3968. /**
  3969. * 将时间转换成数字
  3970. * @override
  3971. */
  3972. Time.prototype.translate = function (v) {
  3973. return toTimeStamp$1(v);
  3974. };
  3975. Time.prototype.initCfg = function () {
  3976. this.tickMethod = 'time-pretty';
  3977. this.mask = 'YYYY-MM-DD';
  3978. this.tickCount = 7;
  3979. this.nice = false;
  3980. };
  3981. Time.prototype.setDomain = function () {
  3982. var values = this.values; // 是否设置了 min, max,而不是直接取 this.min, this.max
  3983. var minConfig = this.getConfig('min');
  3984. var maxConfig = this.getConfig('max'); // 如果设置了 min,max 则转换成时间戳
  3985. if (!isNil(minConfig) || !isNumber(minConfig)) {
  3986. this.min = this.translate(this.min);
  3987. }
  3988. if (!isNil(maxConfig) || !isNumber(maxConfig)) {
  3989. this.max = this.translate(this.max);
  3990. } // 没有设置 min, max 时
  3991. if (values && values.length) {
  3992. // 重新计算最大最小值
  3993. var timeStamps_1 = [];
  3994. var min_1 = Infinity; // 最小值
  3995. var secondMin_1 = min_1; // 次小值
  3996. var max_1 = 0; // 使用一个循环,计算min,max,secondMin
  3997. each(values, function (v) {
  3998. var timeStamp = toTimeStamp$1(v);
  3999. if (isNaN(timeStamp)) {
  4000. throw new TypeError("Invalid Time: " + v + " in time scale!");
  4001. }
  4002. if (min_1 > timeStamp) {
  4003. secondMin_1 = min_1;
  4004. min_1 = timeStamp;
  4005. } else if (secondMin_1 > timeStamp) {
  4006. secondMin_1 = timeStamp;
  4007. }
  4008. if (max_1 < timeStamp) {
  4009. max_1 = timeStamp;
  4010. }
  4011. timeStamps_1.push(timeStamp);
  4012. }); // 存在多个值时,设置最小间距
  4013. if (values.length > 1) {
  4014. this.minTickInterval = secondMin_1 - min_1;
  4015. }
  4016. if (isNil(minConfig)) {
  4017. this.min = min_1;
  4018. }
  4019. if (isNil(maxConfig)) {
  4020. this.max = max_1;
  4021. }
  4022. }
  4023. };
  4024. return Time;
  4025. }(Linear);
  4026. /**
  4027. * 分段度量
  4028. */
  4029. var Quantize =
  4030. /** @class */
  4031. function (_super) {
  4032. __extends(Quantize, _super);
  4033. function Quantize() {
  4034. var _this = _super !== null && _super.apply(this, arguments) || this;
  4035. _this.type = 'quantize';
  4036. return _this;
  4037. }
  4038. Quantize.prototype.invert = function (value) {
  4039. var ticks = this.ticks;
  4040. var length = ticks.length;
  4041. var percent = this.getInvertPercent(value);
  4042. var minIndex = Math.floor(percent * (length - 1)); // 最后一个
  4043. if (minIndex >= length - 1) {
  4044. return last(ticks);
  4045. } // 超出左边界, 则取第一个
  4046. if (minIndex < 0) {
  4047. return head(ticks);
  4048. }
  4049. var minTick = ticks[minIndex];
  4050. var nextTick = ticks[minIndex + 1]; // 比当前值小的 tick 在度量上的占比
  4051. var minIndexPercent = minIndex / (length - 1);
  4052. var maxIndexPercent = (minIndex + 1) / (length - 1);
  4053. return minTick + (percent - minIndexPercent) / (maxIndexPercent - minIndexPercent) * (nextTick - minTick);
  4054. };
  4055. Quantize.prototype.initCfg = function () {
  4056. this.tickMethod = 'r-pretty';
  4057. this.tickCount = 5;
  4058. this.nice = true;
  4059. };
  4060. Quantize.prototype.calculateTicks = function () {
  4061. var ticks = _super.prototype.calculateTicks.call(this);
  4062. if (!this.nice) {
  4063. // 如果 nice = false ,补充 min, max
  4064. if (last(ticks) !== this.max) {
  4065. ticks.push(this.max);
  4066. }
  4067. if (head(ticks) !== this.min) {
  4068. ticks.unshift(this.min);
  4069. }
  4070. }
  4071. return ticks;
  4072. }; // 计算当前值在刻度中的占比
  4073. Quantize.prototype.getScalePercent = function (value) {
  4074. var ticks = this.ticks; // 超出左边界
  4075. if (value < head(ticks)) {
  4076. return 0;
  4077. } // 超出右边界
  4078. if (value > last(ticks)) {
  4079. return 1;
  4080. }
  4081. var minIndex = 0;
  4082. each(ticks, function (tick, index) {
  4083. if (value >= tick) {
  4084. minIndex = index;
  4085. } else {
  4086. return false;
  4087. }
  4088. });
  4089. return minIndex / (ticks.length - 1);
  4090. };
  4091. return Quantize;
  4092. }(Continuous);
  4093. var Quantile =
  4094. /** @class */
  4095. function (_super) {
  4096. __extends(Quantile, _super);
  4097. function Quantile() {
  4098. var _this = _super !== null && _super.apply(this, arguments) || this;
  4099. _this.type = 'quantile';
  4100. return _this;
  4101. }
  4102. Quantile.prototype.initCfg = function () {
  4103. this.tickMethod = 'quantile';
  4104. this.tickCount = 5;
  4105. this.nice = true;
  4106. };
  4107. return Quantile;
  4108. }(Quantize);
  4109. var map$1 = {};
  4110. function getClass(key) {
  4111. return map$1[key];
  4112. }
  4113. function registerClass(key, cls) {
  4114. if (getClass(key)) {
  4115. throw new Error("type '" + key + "' existed.");
  4116. }
  4117. map$1[key] = cls;
  4118. }
  4119. /**
  4120. * identity scale原则上是定义域和值域一致,scale/invert方法也是一致的
  4121. * 参考R的实现:https://github.com/r-lib/scales/blob/master/R/pal-identity.r
  4122. * 参考d3的实现(做了下转型):https://github.com/d3/d3-scale/blob/master/src/identity.js
  4123. */
  4124. var Identity =
  4125. /** @class */
  4126. function (_super) {
  4127. __extends(Identity, _super);
  4128. function Identity() {
  4129. var _this = _super !== null && _super.apply(this, arguments) || this;
  4130. _this.type = 'identity';
  4131. _this.isIdentity = true;
  4132. return _this;
  4133. }
  4134. Identity.prototype.calculateTicks = function () {
  4135. return this.values;
  4136. };
  4137. Identity.prototype.scale = function (value) {
  4138. // 如果传入的值不等于 identity 的值,则直接返回,用于一维图时的 dodge
  4139. if (this.values[0] !== value && isNumber(value)) {
  4140. return value;
  4141. }
  4142. return this.range[0];
  4143. };
  4144. Identity.prototype.invert = function (value) {
  4145. var range = this.range;
  4146. if (value < range[0] || value > range[1]) {
  4147. return NaN;
  4148. }
  4149. return this.values[0];
  4150. };
  4151. return Identity;
  4152. }(Scale);
  4153. var DEFAULT_Q = [1, 5, 2, 2.5, 4, 3];
  4154. var eps = Number.EPSILON * 100; // https://stackoverflow.com/questions/4467539/javascript-modulo-gives-a-negative-result-for-negative-numbers
  4155. function mod(n, m) {
  4156. return (n % m + m) % m;
  4157. }
  4158. function simplicity(q, Q, j, lmin, lmax, lstep) {
  4159. var n = size(Q);
  4160. var i = indexOf(Q, q);
  4161. var v = 0;
  4162. var m = mod(lmin, lstep);
  4163. if ((m < eps || lstep - m < eps) && lmin <= 0 && lmax >= 0) {
  4164. v = 1;
  4165. }
  4166. return 1 - i / (n - 1) - j + v;
  4167. }
  4168. function simplicityMax(q, Q, j) {
  4169. var n = size(Q);
  4170. var i = indexOf(Q, q);
  4171. var v = 1;
  4172. return 1 - i / (n - 1) - j + v;
  4173. }
  4174. function density(k, m, dmin, dmax, lmin, lmax) {
  4175. var r = (k - 1) / (lmax - lmin);
  4176. var rt = (m - 1) / (Math.max(lmax, dmax) - Math.min(dmin, lmin));
  4177. return 2 - Math.max(r / rt, rt / r);
  4178. }
  4179. function densityMax(k, m) {
  4180. if (k >= m) {
  4181. return 2 - (k - 1) / (m - 1);
  4182. }
  4183. return 1;
  4184. }
  4185. function coverage(dmin, dmax, lmin, lmax) {
  4186. var range = dmax - dmin;
  4187. return 1 - 0.5 * (Math.pow(dmax - lmax, 2) + Math.pow(dmin - lmin, 2)) / Math.pow(0.1 * range, 2);
  4188. }
  4189. function coverageMax(dmin, dmax, span) {
  4190. var range = dmax - dmin;
  4191. if (span > range) {
  4192. var half = (span - range) / 2;
  4193. return 1 - Math.pow(half, 2) / Math.pow(0.1 * range, 2);
  4194. }
  4195. return 1;
  4196. }
  4197. function legibility() {
  4198. return 1;
  4199. }
  4200. /**
  4201. * An Extension of Wilkinson's Algorithm for Position Tick Labels on Axes
  4202. * https://www.yuque.com/preview/yuque/0/2019/pdf/185317/1546999150858-45c3b9c2-4e86-4223-bf1a-8a732e8195ed.pdf
  4203. * @param dmin 最小值
  4204. * @param dmax 最大值
  4205. * @param m tick个数
  4206. * @param onlyLoose 是否允许扩展min、max,不绝对强制,例如[3, 97]
  4207. * @param Q nice numbers集合
  4208. * @param w 四个优化组件的权重
  4209. */
  4210. function extended(dmin, dmax, m, onlyLoose, Q, w) {
  4211. if (m === void 0) {
  4212. m = 5;
  4213. }
  4214. if (onlyLoose === void 0) {
  4215. onlyLoose = true;
  4216. }
  4217. if (Q === void 0) {
  4218. Q = DEFAULT_Q;
  4219. }
  4220. if (w === void 0) {
  4221. w = [0.25, 0.2, 0.5, 0.05];
  4222. } // 异常数据情况下,直接返回,防止 oom
  4223. if (typeof dmin !== 'number' || typeof dmax !== 'number') {
  4224. return {
  4225. min: 0,
  4226. max: 0,
  4227. ticks: []
  4228. };
  4229. }
  4230. if (dmin === dmax || m === 1) {
  4231. return {
  4232. min: dmin,
  4233. max: dmax,
  4234. ticks: [dmin]
  4235. };
  4236. }
  4237. var best = {
  4238. score: -2,
  4239. lmin: 0,
  4240. lmax: 0,
  4241. lstep: 0
  4242. };
  4243. var j = 1;
  4244. while (j < Infinity) {
  4245. for (var _i = 0, Q_1 = Q; _i < Q_1.length; _i++) {
  4246. var q = Q_1[_i];
  4247. var sm = simplicityMax(q, Q, j);
  4248. if (Number.isNaN(sm)) {
  4249. throw new Error('NaN');
  4250. }
  4251. if (w[0] * sm + w[1] + w[2] + w[3] < best.score) {
  4252. j = Infinity;
  4253. break;
  4254. }
  4255. var k = 2;
  4256. while (k < Infinity) {
  4257. var dm = densityMax(k, m);
  4258. if (w[0] * sm + w[1] + w[2] * dm + w[3] < best.score) {
  4259. break;
  4260. }
  4261. var delta = (dmax - dmin) / (k + 1) / j / q;
  4262. var z = Math.ceil(Math.log10(delta));
  4263. while (z < Infinity) {
  4264. var step = j * q * Math.pow(10, z);
  4265. var cm = coverageMax(dmin, dmax, step * (k - 1));
  4266. if (w[0] * sm + w[1] * cm + w[2] * dm + w[3] < best.score) {
  4267. break;
  4268. }
  4269. var minStart = Math.floor(dmax / step) * j - (k - 1) * j;
  4270. var maxStart = Math.ceil(dmin / step) * j;
  4271. if (minStart > maxStart) {
  4272. z = z + 1;
  4273. continue;
  4274. }
  4275. for (var start = minStart; start <= maxStart; start = start + 1) {
  4276. var lmin = start * (step / j);
  4277. var lmax = lmin + step * (k - 1);
  4278. var lstep = step;
  4279. var s = simplicity(q, Q, j, lmin, lmax, lstep);
  4280. var c = coverage(dmin, dmax, lmin, lmax);
  4281. var g = density(k, m, dmin, dmax, lmin, lmax);
  4282. var l = legibility();
  4283. var score = w[0] * s + w[1] * c + w[2] * g + w[3] * l;
  4284. if (score > best.score && (!onlyLoose || lmin <= dmin && lmax >= dmax)) {
  4285. best.lmin = lmin;
  4286. best.lmax = lmax;
  4287. best.lstep = lstep;
  4288. best.score = score;
  4289. }
  4290. }
  4291. z = z + 1;
  4292. }
  4293. k = k + 1;
  4294. }
  4295. }
  4296. j = j + 1;
  4297. } // 步长为浮点数时处理精度
  4298. var toFixed = Number.isInteger(best.lstep) ? 0 : Math.ceil(Math.abs(Math.log10(best.lstep)));
  4299. var range = [];
  4300. for (var tick = best.lmin; tick <= best.lmax; tick += best.lstep) {
  4301. range.push(tick);
  4302. }
  4303. var ticks = toFixed ? map(range, function (x) {
  4304. return Number.parseFloat(x.toFixed(toFixed));
  4305. }) : range;
  4306. return {
  4307. min: Math.min(dmin, head(ticks)),
  4308. max: Math.max(dmax, last(ticks)),
  4309. ticks: ticks
  4310. };
  4311. }
  4312. /**
  4313. * 计算分类 ticks
  4314. * @param cfg 度量的配置项
  4315. * @returns 计算后的 ticks
  4316. */
  4317. function calculateCatTicks(cfg) {
  4318. var values = cfg.values,
  4319. tickInterval = cfg.tickInterval,
  4320. tickCount = cfg.tickCount;
  4321. var ticks = values;
  4322. if (isNumber(tickInterval)) {
  4323. return filter(ticks, function (__, i) {
  4324. return i % tickInterval === 0;
  4325. });
  4326. }
  4327. var min = cfg.min,
  4328. max = cfg.max;
  4329. if (isNil(min)) {
  4330. min = 0;
  4331. }
  4332. if (isNil(max)) {
  4333. max = values.length - 1;
  4334. }
  4335. if (isNumber(tickCount) && tickCount < max - min) {
  4336. // 简单过滤,部分情况下小数的倍数也可以是整数
  4337. // tslint:disable-next-line: no-shadowed-variable
  4338. var ticks_1 = extended(min, max, tickCount, false, [1, 2, 5, 3, 4, 7, 6, 8, 9]).ticks;
  4339. var valid = filter(ticks_1, function (tick) {
  4340. return tick >= min && tick <= max;
  4341. });
  4342. return valid.map(function (index) {
  4343. return values[index];
  4344. });
  4345. }
  4346. return values.slice(min, max + 1);
  4347. }
  4348. function d3Linear(cfg) {
  4349. var min = cfg.min,
  4350. max = cfg.max,
  4351. nice = cfg.nice,
  4352. tickCount = cfg.tickCount;
  4353. var linear = new D3Linear();
  4354. linear.domain([min, max]);
  4355. if (nice) {
  4356. linear.nice(tickCount);
  4357. }
  4358. return linear.ticks(tickCount);
  4359. }
  4360. var DEFAULT_COUNT = 5;
  4361. var e10 = Math.sqrt(50);
  4362. var e5 = Math.sqrt(10);
  4363. var e2 = Math.sqrt(2); // https://github.com/d3/d3-scale
  4364. var D3Linear =
  4365. /** @class */
  4366. function () {
  4367. function D3Linear() {
  4368. this._domain = [0, 1];
  4369. }
  4370. D3Linear.prototype.domain = function (domain) {
  4371. if (domain) {
  4372. this._domain = Array.from(domain, Number);
  4373. return this;
  4374. }
  4375. return this._domain.slice();
  4376. };
  4377. D3Linear.prototype.nice = function (count) {
  4378. var _a, _b;
  4379. if (count === void 0) {
  4380. count = DEFAULT_COUNT;
  4381. }
  4382. var d = this._domain.slice();
  4383. var i0 = 0;
  4384. var i1 = this._domain.length - 1;
  4385. var start = this._domain[i0];
  4386. var stop = this._domain[i1];
  4387. var step;
  4388. if (stop < start) {
  4389. _a = [stop, start], start = _a[0], stop = _a[1];
  4390. _b = [i1, i0], i0 = _b[0], i1 = _b[1];
  4391. }
  4392. step = tickIncrement(start, stop, count);
  4393. if (step > 0) {
  4394. start = Math.floor(start / step) * step;
  4395. stop = Math.ceil(stop / step) * step;
  4396. step = tickIncrement(start, stop, count);
  4397. } else if (step < 0) {
  4398. start = Math.ceil(start * step) / step;
  4399. stop = Math.floor(stop * step) / step;
  4400. step = tickIncrement(start, stop, count);
  4401. }
  4402. if (step > 0) {
  4403. d[i0] = Math.floor(start / step) * step;
  4404. d[i1] = Math.ceil(stop / step) * step;
  4405. this.domain(d);
  4406. } else if (step < 0) {
  4407. d[i0] = Math.ceil(start * step) / step;
  4408. d[i1] = Math.floor(stop * step) / step;
  4409. this.domain(d);
  4410. }
  4411. return this;
  4412. };
  4413. D3Linear.prototype.ticks = function (count) {
  4414. if (count === void 0) {
  4415. count = DEFAULT_COUNT;
  4416. }
  4417. return d3ArrayTicks(this._domain[0], this._domain[this._domain.length - 1], count || DEFAULT_COUNT);
  4418. };
  4419. return D3Linear;
  4420. }();
  4421. function d3ArrayTicks(start, stop, count) {
  4422. var reverse;
  4423. var i = -1;
  4424. var n;
  4425. var ticks;
  4426. var step;
  4427. stop = +stop, start = +start, count = +count;
  4428. if (start === stop && count > 0) {
  4429. return [start];
  4430. } // tslint:disable-next-line
  4431. if (reverse = stop < start) {
  4432. n = start, start = stop, stop = n;
  4433. } // tslint:disable-next-line
  4434. if ((step = tickIncrement(start, stop, count)) === 0 || !isFinite(step)) {
  4435. return [];
  4436. }
  4437. if (step > 0) {
  4438. start = Math.ceil(start / step);
  4439. stop = Math.floor(stop / step);
  4440. ticks = new Array(n = Math.ceil(stop - start + 1));
  4441. while (++i < n) {
  4442. ticks[i] = (start + i) * step;
  4443. }
  4444. } else {
  4445. start = Math.floor(start * step);
  4446. stop = Math.ceil(stop * step);
  4447. ticks = new Array(n = Math.ceil(start - stop + 1));
  4448. while (++i < n) {
  4449. ticks[i] = (start - i) / step;
  4450. }
  4451. }
  4452. if (reverse) {
  4453. ticks.reverse();
  4454. }
  4455. return ticks;
  4456. }
  4457. function tickIncrement(start, stop, count) {
  4458. var step = (stop - start) / Math.max(0, count);
  4459. var power = Math.floor(Math.log(step) / Math.LN10);
  4460. var error = step / Math.pow(10, power);
  4461. return power >= 0 ? (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1) * Math.pow(10, power) : -Math.pow(10, -power) / (error >= e10 ? 10 : error >= e5 ? 5 : error >= e2 ? 2 : 1);
  4462. }
  4463. function snapMultiple(v, base, snapType) {
  4464. var div;
  4465. if (snapType === 'ceil') {
  4466. div = Math.ceil(v / base);
  4467. } else if (snapType === 'floor') {
  4468. div = Math.floor(v / base);
  4469. } else {
  4470. div = Math.round(v / base);
  4471. }
  4472. return div * base;
  4473. }
  4474. function intervalTicks(min, max, interval) {
  4475. // 变成 interval 的倍数
  4476. var minTick = snapMultiple(min, interval, 'floor');
  4477. var maxTick = snapMultiple(max, interval, 'ceil'); // 统一小数位数
  4478. minTick = fixedBase(minTick, interval);
  4479. maxTick = fixedBase(maxTick, interval);
  4480. var ticks = [];
  4481. for (var i = minTick; i <= maxTick; i = i + interval) {
  4482. var tickValue = fixedBase(i, interval); // 防止浮点数加法出现问题
  4483. ticks.push(tickValue);
  4484. }
  4485. return {
  4486. min: minTick,
  4487. max: maxTick,
  4488. ticks: ticks
  4489. };
  4490. }
  4491. /**
  4492. * 按照给定的 minLimit/maxLimit/tickCount 均匀计算出刻度 ticks
  4493. *
  4494. * @param cfg Scale 配置项
  4495. * @return ticks
  4496. */
  4497. function strictLimit(cfg, defaultMin, defaultMax) {
  4498. var _a;
  4499. var minLimit = cfg.minLimit,
  4500. maxLimit = cfg.maxLimit,
  4501. min = cfg.min,
  4502. max = cfg.max,
  4503. _b = cfg.tickCount,
  4504. tickCount = _b === void 0 ? 5 : _b;
  4505. var tickMin = isNil(minLimit) ? isNil(defaultMin) ? min : defaultMin : minLimit;
  4506. var tickMax = isNil(maxLimit) ? isNil(defaultMax) ? max : defaultMax : maxLimit;
  4507. if (tickMin > tickMax) {
  4508. _a = [tickMin, tickMax], tickMax = _a[0], tickMin = _a[1];
  4509. }
  4510. if (tickCount <= 2) {
  4511. return [tickMin, tickMax];
  4512. }
  4513. var step = (tickMax - tickMin) / (tickCount - 1);
  4514. var ticks = [];
  4515. for (var i = 0; i < tickCount; i++) {
  4516. ticks.push(tickMin + step * i);
  4517. }
  4518. return ticks;
  4519. }
  4520. function d3LinearTickMethod(cfg) {
  4521. var min = cfg.min,
  4522. max = cfg.max,
  4523. tickInterval = cfg.tickInterval,
  4524. minLimit = cfg.minLimit,
  4525. maxLimit = cfg.maxLimit;
  4526. var ticks = d3Linear(cfg);
  4527. if (!isNil(minLimit) || !isNil(maxLimit)) {
  4528. return strictLimit(cfg, head(ticks), last(ticks));
  4529. }
  4530. if (tickInterval) {
  4531. return intervalTicks(min, max, tickInterval).ticks;
  4532. }
  4533. return ticks;
  4534. }
  4535. /**
  4536. * 计算线性的 ticks,使用 wilkinson extended 方法
  4537. * @param cfg 度量的配置项
  4538. * @returns 计算后的 ticks
  4539. */
  4540. function linear(cfg) {
  4541. var min = cfg.min,
  4542. max = cfg.max,
  4543. tickCount = cfg.tickCount,
  4544. nice = cfg.nice,
  4545. tickInterval = cfg.tickInterval,
  4546. minLimit = cfg.minLimit,
  4547. maxLimit = cfg.maxLimit;
  4548. var ticks = extended(min, max, tickCount, nice).ticks;
  4549. if (!isNil(minLimit) || !isNil(maxLimit)) {
  4550. return strictLimit(cfg, head(ticks), last(ticks));
  4551. }
  4552. if (tickInterval) {
  4553. return intervalTicks(min, max, tickInterval).ticks;
  4554. }
  4555. return ticks;
  4556. }
  4557. /**
  4558. * 计算 log 的 ticks,考虑 min = 0 的场景
  4559. * @param cfg 度量的配置项
  4560. * @returns 计算后的 ticks
  4561. */
  4562. function calculateLogTicks(cfg) {
  4563. var base = cfg.base,
  4564. tickCount = cfg.tickCount,
  4565. min = cfg.min,
  4566. max = cfg.max,
  4567. values = cfg.values;
  4568. var minTick;
  4569. var maxTick = log(base, max);
  4570. if (min > 0) {
  4571. minTick = Math.floor(log(base, min));
  4572. } else {
  4573. var positiveMin = getLogPositiveMin(values, base, max);
  4574. minTick = Math.floor(log(base, positiveMin));
  4575. }
  4576. var count = maxTick - minTick;
  4577. var avg = Math.ceil(count / tickCount);
  4578. var ticks = [];
  4579. for (var i = minTick; i < maxTick + avg; i = i + avg) {
  4580. ticks.push(Math.pow(base, i));
  4581. }
  4582. if (min <= 0) {
  4583. // 最小值 <= 0 时显示 0
  4584. ticks.unshift(0);
  4585. }
  4586. return ticks;
  4587. }
  4588. function pretty(min, max, n) {
  4589. if (n === void 0) {
  4590. n = 5;
  4591. }
  4592. var res = {
  4593. max: 0,
  4594. min: 0,
  4595. ticks: []
  4596. };
  4597. if (min === max) {
  4598. return {
  4599. max: max,
  4600. min: min,
  4601. ticks: [min]
  4602. };
  4603. }
  4604. /*
  4605. R pretty:
  4606. https://svn.r-project.org/R/trunk/src/appl/pretty.c
  4607. https://www.rdocumentation.org/packages/base/versions/3.5.2/topics/pretty
  4608. */
  4609. var h = 1.5; // high.u.bias
  4610. var h5 = 0.5 + 1.5 * h; // u5.bias
  4611. // 反正我也不会调参,跳过所有判断步骤
  4612. var d = max - min;
  4613. var c = d / n; // 当d非常小的时候触发,但似乎没什么用
  4614. // const min_n = Math.floor(n / 3);
  4615. // const shrink_sml = Math.pow(2, 5);
  4616. // if (Math.log10(d) < -2) {
  4617. // c = (_.max([ Math.abs(max), Math.abs(min) ]) * shrink_sml) / min_n;
  4618. // }
  4619. var base = Math.pow(10, Math.floor(Math.log10(c)));
  4620. var toFixed = base < 1 ? Math.ceil(Math.abs(Math.log10(base))) : 0;
  4621. var unit = base;
  4622. if (2 * base - c < h * (c - unit)) {
  4623. unit = 2 * base;
  4624. if (5 * base - c < h5 * (c - unit)) {
  4625. unit = 5 * base;
  4626. if (10 * base - c < h * (c - unit)) {
  4627. unit = 10 * base;
  4628. }
  4629. }
  4630. }
  4631. var nu = Math.ceil(max / unit);
  4632. var ns = Math.floor(min / unit);
  4633. res.max = Math.max(nu * unit, max);
  4634. res.min = Math.min(ns * unit, min);
  4635. var x = Number.parseFloat((ns * unit).toFixed(toFixed));
  4636. while (x < max) {
  4637. res.ticks.push(x);
  4638. x += unit;
  4639. if (toFixed) {
  4640. x = Number.parseFloat(x.toFixed(toFixed));
  4641. }
  4642. }
  4643. res.ticks.push(x);
  4644. return res;
  4645. }
  4646. /**
  4647. * 计算 Pow 的 ticks
  4648. * @param cfg 度量的配置项
  4649. * @returns 计算后的 ticks
  4650. */
  4651. function calculatePowTicks(cfg) {
  4652. var exponent = cfg.exponent,
  4653. tickCount = cfg.tickCount;
  4654. var max = Math.ceil(calBase(exponent, cfg.max));
  4655. var min = Math.floor(calBase(exponent, cfg.min));
  4656. var ticks = pretty(min, max, tickCount).ticks;
  4657. return ticks.map(function (tick) {
  4658. var factor = tick >= 0 ? 1 : -1;
  4659. return Math.pow(tick, exponent) * factor;
  4660. });
  4661. }
  4662. /**
  4663. * 计算几分位 https://github.com/simple-statistics/simple-statistics/blob/master/src/quantile_sorted.js
  4664. * @param x 数组
  4665. * @param p 百分比
  4666. */
  4667. function quantileSorted(x, p) {
  4668. var idx = x.length * p;
  4669. /*if (x.length === 0) { // 当前场景这些条件不可能命中
  4670. throw new Error('quantile requires at least one value.');
  4671. } else if (p < 0 || p > 1) {
  4672. throw new Error('quantiles must be between 0 and 1');
  4673. } else */
  4674. if (p === 1) {
  4675. // If p is 1, directly return the last element
  4676. return x[x.length - 1];
  4677. } else if (p === 0) {
  4678. // If p is 0, directly return the first element
  4679. return x[0];
  4680. } else if (idx % 1 !== 0) {
  4681. // If p is not integer, return the next element in array
  4682. return x[Math.ceil(idx) - 1];
  4683. } else if (x.length % 2 === 0) {
  4684. // If the list has even-length, we'll take the average of this number
  4685. // and the next value, if there is one
  4686. return (x[idx - 1] + x[idx]) / 2;
  4687. } else {
  4688. // Finally, in the simple case of an integer value
  4689. // with an odd-length list, return the x value at the index.
  4690. return x[idx];
  4691. }
  4692. }
  4693. function calculateTicks(cfg) {
  4694. var tickCount = cfg.tickCount,
  4695. values = cfg.values;
  4696. if (!values || !values.length) {
  4697. return [];
  4698. }
  4699. var sorted = values.slice().sort(function (a, b) {
  4700. return a - b;
  4701. });
  4702. var ticks = [];
  4703. for (var i = 0; i < tickCount; i++) {
  4704. var p = i / (tickCount - 1);
  4705. ticks.push(quantileSorted(sorted, p));
  4706. }
  4707. return ticks;
  4708. }
  4709. /**
  4710. * 计算线性的 ticks,使用 R's pretty 方法
  4711. * @param cfg 度量的配置项
  4712. * @returns 计算后的 ticks
  4713. */
  4714. function linearPretty(cfg) {
  4715. var min = cfg.min,
  4716. max = cfg.max,
  4717. tickCount = cfg.tickCount,
  4718. tickInterval = cfg.tickInterval,
  4719. minLimit = cfg.minLimit,
  4720. maxLimit = cfg.maxLimit;
  4721. var ticks = pretty(min, max, tickCount).ticks;
  4722. if (!isNil(minLimit) || !isNil(maxLimit)) {
  4723. return strictLimit(cfg, head(ticks), last(ticks));
  4724. }
  4725. if (tickInterval) {
  4726. return intervalTicks(min, max, tickInterval).ticks;
  4727. }
  4728. return ticks;
  4729. }
  4730. function calculateTimeTicks(cfg) {
  4731. var min = cfg.min,
  4732. max = cfg.max,
  4733. minTickInterval = cfg.minTickInterval;
  4734. var tickInterval = cfg.tickInterval;
  4735. var tickCount = cfg.tickCount; // 指定 tickInterval 后 tickCount 不生效,需要重新计算
  4736. if (tickInterval) {
  4737. tickCount = Math.ceil((max - min) / tickInterval);
  4738. } else {
  4739. tickInterval = getTickInterval(min, max, tickCount)[1];
  4740. var count = (max - min) / tickInterval;
  4741. var ratio = count / tickCount;
  4742. if (ratio > 1) {
  4743. tickInterval = tickInterval * Math.ceil(ratio);
  4744. } // 如果设置了最小间距,则使用最小间距
  4745. if (minTickInterval && tickInterval < minTickInterval) {
  4746. tickInterval = minTickInterval;
  4747. }
  4748. }
  4749. var ticks = [];
  4750. for (var i = min; i < max + tickInterval; i += tickInterval) {
  4751. ticks.push(i);
  4752. }
  4753. return ticks;
  4754. }
  4755. /**
  4756. * 计算时间分类的 ticks, 保头,保尾
  4757. * @param cfg 度量的配置项
  4758. * @returns 计算后的 ticks
  4759. */
  4760. function calculateTimeCatTicks(cfg) {
  4761. var ticks = calculateCatTicks(cfg);
  4762. var lastValue = last(cfg.values);
  4763. if (lastValue !== last(ticks)) {
  4764. ticks.push(lastValue);
  4765. }
  4766. return ticks;
  4767. }
  4768. function getYear(date) {
  4769. return new Date(date).getFullYear();
  4770. }
  4771. function createYear(year) {
  4772. return new Date(year, 0, 1).getTime();
  4773. }
  4774. function getMonth(date) {
  4775. return new Date(date).getMonth();
  4776. }
  4777. function diffMonth(min, max) {
  4778. var minYear = getYear(min);
  4779. var maxYear = getYear(max);
  4780. var minMonth = getMonth(min);
  4781. var maxMonth = getMonth(max);
  4782. return (maxYear - minYear) * 12 + (maxMonth - minMonth) % 12;
  4783. }
  4784. function creatMonth(year, month) {
  4785. return new Date(year, month, 1).getTime();
  4786. }
  4787. function diffDay(min, max) {
  4788. return Math.ceil((max - min) / DAY);
  4789. }
  4790. function diffHour(min, max) {
  4791. return Math.ceil((max - min) / HOUR);
  4792. }
  4793. function diffMinus(min, max) {
  4794. return Math.ceil((max - min) / (60 * 1000));
  4795. }
  4796. /**
  4797. * 计算 time 的 ticks,对 month, year 进行 pretty 处理
  4798. * @param cfg 度量的配置项
  4799. * @returns 计算后的 ticks
  4800. */
  4801. function timePretty(cfg) {
  4802. var min = cfg.min,
  4803. max = cfg.max,
  4804. minTickInterval = cfg.minTickInterval,
  4805. tickCount = cfg.tickCount;
  4806. var tickInterval = cfg.tickInterval;
  4807. var ticks = []; // 指定 tickInterval 后 tickCount 不生效,需要重新计算
  4808. if (!tickInterval) {
  4809. tickInterval = (max - min) / tickCount; // 如果设置了最小间距,则使用最小间距
  4810. if (minTickInterval && tickInterval < minTickInterval) {
  4811. tickInterval = minTickInterval;
  4812. }
  4813. }
  4814. var minYear = getYear(min); // 如果间距大于 1 年,则将开始日期从整年开始
  4815. if (tickInterval > YEAR) {
  4816. var maxYear = getYear(max);
  4817. var yearInterval = Math.ceil(tickInterval / YEAR);
  4818. for (var i = minYear; i <= maxYear + yearInterval; i = i + yearInterval) {
  4819. ticks.push(createYear(i));
  4820. }
  4821. } else if (tickInterval > MONTH) {
  4822. // 大于月时
  4823. var monthInterval = Math.ceil(tickInterval / MONTH);
  4824. var mmMoth = getMonth(min);
  4825. var dMonths = diffMonth(min, max);
  4826. for (var i = 0; i <= dMonths + monthInterval; i = i + monthInterval) {
  4827. ticks.push(creatMonth(minYear, i + mmMoth));
  4828. }
  4829. } else if (tickInterval > DAY) {
  4830. // 大于天
  4831. var date = new Date(min);
  4832. var year = date.getFullYear();
  4833. var month = date.getMonth();
  4834. var mday = date.getDate();
  4835. var day = Math.ceil(tickInterval / DAY);
  4836. var ddays = diffDay(min, max);
  4837. for (var i = 0; i < ddays + day; i = i + day) {
  4838. ticks.push(new Date(year, month, mday + i).getTime());
  4839. }
  4840. } else if (tickInterval > HOUR) {
  4841. // 大于小时
  4842. var date = new Date(min);
  4843. var year = date.getFullYear();
  4844. var month = date.getMonth();
  4845. var day = date.getDate();
  4846. var hour = date.getHours();
  4847. var hours = Math.ceil(tickInterval / HOUR);
  4848. var dHours = diffHour(min, max);
  4849. for (var i = 0; i <= dHours + hours; i = i + hours) {
  4850. ticks.push(new Date(year, month, day, hour + i).getTime());
  4851. }
  4852. } else if (tickInterval > MINUTE) {
  4853. // 大于分钟
  4854. var dMinus = diffMinus(min, max);
  4855. var minutes = Math.ceil(tickInterval / MINUTE);
  4856. for (var i = 0; i <= dMinus + minutes; i = i + minutes) {
  4857. ticks.push(min + i * MINUTE);
  4858. }
  4859. } else {
  4860. // 小于分钟
  4861. var interval = tickInterval;
  4862. if (interval < SECOND) {
  4863. interval = SECOND;
  4864. }
  4865. var minSecond = Math.floor(min / SECOND) * SECOND;
  4866. var dSeconds = Math.ceil((max - min) / SECOND);
  4867. var seconds = Math.ceil(interval / SECOND);
  4868. for (var i = 0; i < dSeconds + seconds; i = i + seconds) {
  4869. ticks.push(minSecond + i * SECOND);
  4870. }
  4871. } // 最好是能从算法能解决这个问题,但是如果指定了 tickInterval,计算 ticks,也只能这么算,所以
  4872. // 打印警告提示
  4873. if (ticks.length >= 512) {
  4874. 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!");
  4875. }
  4876. return ticks;
  4877. }
  4878. registerTickMethod('cat', calculateCatTicks);
  4879. registerTickMethod('time-cat', calculateTimeCatTicks);
  4880. registerTickMethod('wilkinson-extended', linear);
  4881. registerTickMethod('r-pretty', linearPretty);
  4882. registerTickMethod('time', calculateTimeTicks);
  4883. registerTickMethod('time-pretty', timePretty);
  4884. registerTickMethod('log', calculateLogTicks);
  4885. registerTickMethod('pow', calculatePowTicks);
  4886. registerTickMethod('quantile', calculateTicks);
  4887. registerTickMethod('d3-linear', d3LinearTickMethod);
  4888. registerClass('cat', Category);
  4889. registerClass('category', Category);
  4890. registerClass('identity', Identity);
  4891. registerClass('linear', Linear);
  4892. registerClass('log', Log);
  4893. registerClass('pow', Pow);
  4894. registerClass('time', Time);
  4895. registerClass('timeCat', TimeCat);
  4896. registerClass('quantize', Quantize);
  4897. registerClass('quantile', Quantile);
  4898. // cat平均算法,保头保尾
  4899. var CatTick = (function (cfg) {
  4900. var values = cfg.values,
  4901. tickCount = cfg.tickCount;
  4902. if (!tickCount) {
  4903. return values;
  4904. }
  4905. if (values.length <= 1) {
  4906. return values;
  4907. } // 获取间隔步长, 最小是1
  4908. var step = parseInt(values.length / (tickCount - 1)) || 1;
  4909. var ticks = []; // 按间隔数取对应节点
  4910. for (var index = 0; index < values.length; index = index + step) {
  4911. ticks.push(values[index]);
  4912. }
  4913. var last = values[values.length - 1]; // 如果最后一个tick不等于原数据的最后一个
  4914. if (ticks[ticks.length - 1] !== last) {
  4915. if (ticks.length >= tickCount) {
  4916. // 如果当前的tick个数满足要求
  4917. ticks[ticks.length - 1] = last;
  4918. } else {
  4919. // 不满足tickCount则直接加入最后一个
  4920. ticks.push(last);
  4921. }
  4922. }
  4923. return ticks;
  4924. });
  4925. // 认为是nice的刻度
  4926. var SNAP_COUNT_ARRAY = [1, 1.2, 1.5, 2, 2.2, 2.4, 2.5, 3, 4, 5, 6, 7.5, 8, 10];
  4927. var DEFAULT_COUNT$1 = 5; // 默认刻度值
  4928. var LinearTick = (function (cfg) {
  4929. var _ref = cfg || {},
  4930. tickCount = _ref.tickCount,
  4931. tickInterval = _ref.tickInterval;
  4932. var _ref2 = cfg || {},
  4933. min = _ref2.min,
  4934. max = _ref2.max;
  4935. min = isNaN(min) ? 0 : min;
  4936. max = isNaN(max) ? 0 : max;
  4937. var count = tickCount && tickCount >= 2 ? tickCount : DEFAULT_COUNT$1; // 计算interval, 优先取tickInterval
  4938. var interval = tickInterval || getBestInterval({
  4939. tickCount: count,
  4940. max: max,
  4941. min: min
  4942. }); // 通过interval计算最小tick
  4943. var minTick = Math.floor(min / interval) * interval; // 如果指定了tickInterval, count 需要根据指定的tickInterval来算计
  4944. if (tickInterval) {
  4945. var intervalCount = Math.abs(Math.ceil((max - minTick) / tickInterval)) + 1; // tickCount 作为最小 count 处理
  4946. count = Math.max(count, intervalCount);
  4947. }
  4948. var ticks = [];
  4949. var tickLength = 0;
  4950. var fixedLength = getFixedLength(interval);
  4951. while (tickLength < count) {
  4952. ticks.push(toFixed(minTick + tickLength * interval, fixedLength));
  4953. tickLength++;
  4954. }
  4955. return ticks;
  4956. });
  4957. var DECIMAL_LENGTH = 12;
  4958. function getFactor(number) {
  4959. // 取正数
  4960. number = Math.abs(number);
  4961. var factor = 1;
  4962. if (number === 0) {
  4963. return factor;
  4964. } // 小于1,逐渐放大
  4965. if (number < 1) {
  4966. var count = 0;
  4967. while (number < 1) {
  4968. factor = factor / 10;
  4969. number = number * 10;
  4970. count++;
  4971. } // 浮点数计算出现问题
  4972. if (factor.toString().length > DECIMAL_LENGTH) {
  4973. factor = parseFloat(factor.toFixed(count));
  4974. }
  4975. return factor;
  4976. } // 大于10逐渐缩小
  4977. while (number > 10) {
  4978. factor = factor * 10;
  4979. number = number / 10;
  4980. }
  4981. return factor;
  4982. } // 获取最佳匹配刻度
  4983. function getBestInterval(_ref3) {
  4984. var tickCount = _ref3.tickCount,
  4985. min = _ref3.min,
  4986. max = _ref3.max;
  4987. // 如果最大最小相等,则直接按1处理
  4988. if (min === max) {
  4989. return 1 * getFactor(max);
  4990. } // 1.计算平均刻度间隔
  4991. var avgInterval = (max - min) / (tickCount - 1); // 2.数据标准归一化 映射到[1-10]区间
  4992. var factor = getFactor(avgInterval);
  4993. var calInterval = avgInterval / factor;
  4994. var calMax = max / factor;
  4995. var calMin = min / factor; // 根据平均值推算最逼近刻度值
  4996. var similarityIndex = 0;
  4997. for (var index = 0; index < SNAP_COUNT_ARRAY.length; index++) {
  4998. var item = SNAP_COUNT_ARRAY[index];
  4999. if (calInterval <= item) {
  5000. similarityIndex = index;
  5001. break;
  5002. }
  5003. }
  5004. var similarityInterval = getInterval(similarityIndex, tickCount, calMin, calMax); // 小数点位数还原到数据的位数, 因为similarityIndex有可能是小数,所以需要保留similarityIndex自己的小数位数
  5005. var fixedLength = getFixedLength(similarityInterval) + getFixedLength(factor);
  5006. return toFixed(similarityInterval * factor, fixedLength);
  5007. }
  5008. function getInterval(startIndex, tickCount, min, max) {
  5009. var verify = false;
  5010. var interval = SNAP_COUNT_ARRAY[startIndex]; // 刻度值校验,如果不满足,循环下去
  5011. for (var i = startIndex; i < SNAP_COUNT_ARRAY.length; i++) {
  5012. if (intervalIsVerify({
  5013. interval: SNAP_COUNT_ARRAY[i],
  5014. tickCount: tickCount,
  5015. max: max,
  5016. min: min
  5017. })) {
  5018. // 有符合条件的interval
  5019. interval = SNAP_COUNT_ARRAY[i];
  5020. verify = true;
  5021. break;
  5022. }
  5023. } // 如果不满足, 依次缩小10倍,再计算
  5024. if (!verify) {
  5025. return 10 * getInterval(0, tickCount, min / 10, max / 10);
  5026. }
  5027. return interval;
  5028. } // 刻度是否满足展示需求
  5029. function intervalIsVerify(_ref4) {
  5030. var interval = _ref4.interval,
  5031. tickCount = _ref4.tickCount,
  5032. max = _ref4.max,
  5033. min = _ref4.min;
  5034. var minTick = Math.floor(min / interval) * interval;
  5035. if (minTick + (tickCount - 1) * interval >= max) {
  5036. return true;
  5037. }
  5038. return false;
  5039. } // 计算小数点应该保留的位数
  5040. function getFixedLength(num) {
  5041. var str = num.toString();
  5042. var index = str.indexOf('.');
  5043. var indexOfExp = str.indexOf('e-');
  5044. var length = indexOfExp >= 0 ? parseInt(str.substr(indexOfExp + 2), 10) : str.substr(index + 1).length;
  5045. if (length > 20) {
  5046. // 最多保留20位小数
  5047. length = 20;
  5048. }
  5049. return length;
  5050. } // @antv/util fixedbase不支持科学计数法的判断,需要提mr
  5051. function toFixed(v, length) {
  5052. return parseFloat(v.toFixed(length));
  5053. }
  5054. var Linear$1 = getClass('linear');
  5055. var Identity$1 = getClass('identity');
  5056. var Category$1 = getClass('category');
  5057. var TimeCat$1 = getClass('timeCat'); // 覆盖0.3.x的 cat 方法
  5058. registerTickMethod('cat', CatTick);
  5059. registerTickMethod('time-cat', CatTick); // 覆盖linear 度量的tick算法
  5060. registerTickMethod('wilkinson-extended', LinearTick);
  5061. Scale.Linear = Linear$1;
  5062. Scale.Identity = Identity$1;
  5063. Scale.Category = Category$1;
  5064. Scale.Cat = Category$1;
  5065. Scale.TimeCat = TimeCat$1;
  5066. function isFullCircle(coord) {
  5067. if (!coord.isPolar) {
  5068. return false;
  5069. }
  5070. var startAngle = coord.startAngle;
  5071. var endAngle = coord.endAngle;
  5072. if (!isNil(startAngle) && !isNil(endAngle) && endAngle - startAngle < Math.PI * 2) {
  5073. return false;
  5074. }
  5075. return true;
  5076. }
  5077. function clearObj(obj) {
  5078. Object.keys(obj).forEach(function (key) {
  5079. delete obj[key];
  5080. });
  5081. }
  5082. var ScaleController = /*#__PURE__*/function () {
  5083. function ScaleController(cfg) {
  5084. // defs 列定义
  5085. this.defs = {}; // 已经实例化的scale
  5086. this.scales = {};
  5087. mix(this, cfg);
  5088. }
  5089. var _proto = ScaleController.prototype;
  5090. _proto.setFieldDef = function setFieldDef(field, cfg) {
  5091. var defs = this.defs;
  5092. if (isObject(field)) {
  5093. mix(defs, field);
  5094. } else {
  5095. defs[field] = cfg;
  5096. } // 因为可能同时变更多个scale,所以要把所有已实例化的scale都更新下
  5097. this.updateScales();
  5098. };
  5099. _proto._getDef = function _getDef(field) {
  5100. var defs = this.defs;
  5101. var def = null;
  5102. if (Global.scales[field] || defs[field]) {
  5103. def = mix({}, Global.scales[field]);
  5104. each(defs[field], function (v, k) {
  5105. if (isNil(v)) {
  5106. delete def[k];
  5107. } else {
  5108. def[k] = v;
  5109. }
  5110. });
  5111. }
  5112. return def;
  5113. };
  5114. _proto._getDefaultType = function _getDefaultType(field, data, def) {
  5115. if (def && def.type) {
  5116. return def.type;
  5117. }
  5118. var type = 'linear';
  5119. var value = firstValue(data, field);
  5120. if (isArray(value)) {
  5121. value = value[0];
  5122. }
  5123. if (isString(value)) {
  5124. type = 'cat';
  5125. }
  5126. return type;
  5127. };
  5128. _proto._getScaleDef = function _getScaleDef(type, field, data, def) {
  5129. var values$1;
  5130. if (def && def.values) {
  5131. values$1 = def.values;
  5132. } else {
  5133. values$1 = values(data, field);
  5134. }
  5135. var cfg = {
  5136. field: field,
  5137. values: values$1
  5138. };
  5139. if (type !== 'cat' && type !== 'timeCat') {
  5140. if (!def || !(def.min && def.max)) {
  5141. var _Array$getRange = getRange$1(values$1),
  5142. min = _Array$getRange.min,
  5143. max = _Array$getRange.max;
  5144. cfg.min = min;
  5145. cfg.max = max;
  5146. cfg.nice = true;
  5147. }
  5148. } else {
  5149. cfg.isRounding = false; // used for tickCount calculation
  5150. }
  5151. return cfg;
  5152. } // 调整range,为了让图形居中
  5153. ;
  5154. _proto._adjustRange = function _adjustRange(type, cfg) {
  5155. var range = cfg.range,
  5156. values = cfg.values; // 如果是线性, 或者有自定义range都不处理
  5157. if (type === 'linear' || range || !values) {
  5158. return cfg;
  5159. }
  5160. var count = values.length; // 单只有一条数据时,在中间显示
  5161. if (count === 1) {
  5162. cfg.range = [0.5, 1];
  5163. } else {
  5164. var chart = this.chart;
  5165. var coord = chart.get('coord');
  5166. var widthRatio = Global.widthRatio.multiplePie;
  5167. var offset = 0;
  5168. if (isFullCircle(coord)) {
  5169. if (!coord.transposed) {
  5170. cfg.range = [0, 1 - 1 / count];
  5171. } else {
  5172. offset = 1 / count * widthRatio;
  5173. cfg.range = [offset / 2, 1 - offset / 2];
  5174. }
  5175. } else {
  5176. // 为了让图形居中,所以才设置range
  5177. offset = 1 / count * 0.5; // 这里可能用0.25会更合理
  5178. cfg.range = [offset, 1 - offset];
  5179. }
  5180. }
  5181. return cfg;
  5182. };
  5183. _proto._getScaleCfg = function _getScaleCfg(field, data) {
  5184. var self = this;
  5185. var def = self._getDef(field);
  5186. if (!data || !data.length) {
  5187. if (def && def.type) {
  5188. def.field = field;
  5189. return {
  5190. type: def.type,
  5191. cfg: def
  5192. };
  5193. }
  5194. return {
  5195. type: 'identity',
  5196. cfg: {
  5197. value: field,
  5198. field: field.toString(),
  5199. values: [field]
  5200. }
  5201. };
  5202. }
  5203. var firstObj = data[0];
  5204. var firstValue$1 = firstObj[field];
  5205. if (firstValue$1 === null) {
  5206. firstValue$1 = firstValue(data, field);
  5207. }
  5208. if (isNumber(field) || isNil(firstValue$1) && !def) {
  5209. return {
  5210. type: 'identity',
  5211. cfg: {
  5212. value: field,
  5213. field: field.toString(),
  5214. values: [field]
  5215. }
  5216. };
  5217. }
  5218. var type = self._getDefaultType(field, data, def);
  5219. var cfg = self._getScaleDef(type, field, data, def);
  5220. def && mix(cfg, def);
  5221. cfg = this._adjustRange(type, cfg);
  5222. return {
  5223. type: type,
  5224. cfg: cfg
  5225. };
  5226. };
  5227. _proto.createScale = function createScale(field, data) {
  5228. var scales = this.scales;
  5229. var _this$_getScaleCfg = this._getScaleCfg(field, data),
  5230. type = _this$_getScaleCfg.type,
  5231. cfg = _this$_getScaleCfg.cfg;
  5232. var scale = scales[field]; // 如果已经存在,且类型相等时直接返回
  5233. if (scale && scale.type === type) {
  5234. scale.change(cfg);
  5235. return scale;
  5236. }
  5237. var Scale = getClass(type);
  5238. var newScale = new Scale(cfg);
  5239. scales[field] = newScale;
  5240. return newScale;
  5241. };
  5242. _proto._updateScale = function _updateScale(scale) {
  5243. var field = scale.field; // 因为每个field的数据都会不同
  5244. var data = this.chart._getScaleData(field);
  5245. var _this$_getScaleCfg2 = this._getScaleCfg(field, data),
  5246. cfg = _this$_getScaleCfg2.cfg;
  5247. scale.change(cfg);
  5248. };
  5249. _proto.updateScales = function updateScales() {
  5250. var _this = this;
  5251. var scales = this.scales; // 修改完列定义后,需要更新已经实例化的scale
  5252. // 如果是还没有实例化的,在geom初始化的时候会被实例化,所以这里可以不用更新
  5253. each(scales, function (scale) {
  5254. _this._updateScale(scale);
  5255. });
  5256. } // 调整scale从0开始
  5257. ;
  5258. _proto.adjustStartZero = function adjustStartZero(scale) {
  5259. var defs = this.defs;
  5260. var field = scale.field,
  5261. min = scale.min,
  5262. max = scale.max; // 如果有定义,则不处理
  5263. if (defs[field] && defs[field].min) {
  5264. return;
  5265. }
  5266. if (min > 0) {
  5267. scale.change({
  5268. min: 0
  5269. });
  5270. } else if (max < 0) {
  5271. scale.change({
  5272. max: 0
  5273. });
  5274. }
  5275. };
  5276. _proto.clear = function clear() {
  5277. // this.defs = {};
  5278. // this.scales = {};
  5279. clearObj(this.defs);
  5280. clearObj(this.scales);
  5281. this.data = null;
  5282. };
  5283. return ScaleController;
  5284. }();
  5285. var Abastract = /*#__PURE__*/function () {
  5286. var _proto = Abastract.prototype;
  5287. _proto._initDefaultCfg = function _initDefaultCfg() {
  5288. /**
  5289. * ticks
  5290. * @type {Array}
  5291. */
  5292. this.ticks = [];
  5293. /**
  5294. * the configuration for tickLine
  5295. * @type {Object}
  5296. */
  5297. this.tickLine = {};
  5298. /**
  5299. * the direction of ticks, 1 means clockwise
  5300. * @type {Number}
  5301. */
  5302. this.offsetFactor = 1;
  5303. /**
  5304. * the top container
  5305. * @type {container}
  5306. */
  5307. this.frontContainer = null;
  5308. /**
  5309. * the back container
  5310. * @type {[type]}
  5311. */
  5312. this.backContainer = null;
  5313. /**
  5314. * points for draw grid line
  5315. * @type {Array}
  5316. */
  5317. this.gridPoints = [];
  5318. };
  5319. function Abastract(cfg) {
  5320. this._initDefaultCfg();
  5321. mix(this, cfg);
  5322. this.draw();
  5323. }
  5324. _proto.draw = function draw() {
  5325. var line = this.line,
  5326. tickLine = this.tickLine,
  5327. label = this.label,
  5328. grid = this.grid;
  5329. grid && this.drawGrid(grid); // draw the grid lines
  5330. tickLine && this.drawTicks(tickLine); // draw the tickLine
  5331. line && this.drawLine(line); // draw axis line
  5332. label && this.drawLabels(); // draw ticks
  5333. };
  5334. _proto.drawTicks = function drawTicks(tickCfg) {
  5335. var self = this;
  5336. var ticks = self.ticks;
  5337. var length = tickCfg.length;
  5338. var container = self.getContainer(tickCfg.top);
  5339. each(ticks, function (tick) {
  5340. var start = self.getOffsetPoint(tick.value);
  5341. var end = self.getSidePoint(start, length);
  5342. var shape = container.addShape('line', {
  5343. className: 'axis-tick',
  5344. attrs: mix({
  5345. x1: start.x,
  5346. y1: start.y,
  5347. x2: end.x,
  5348. y2: end.y
  5349. }, tickCfg)
  5350. });
  5351. shape._id = self._id + '-ticks';
  5352. });
  5353. };
  5354. _proto.drawLabels = function drawLabels() {
  5355. var self = this;
  5356. var labelOffset = self.labelOffset;
  5357. var labels = self.labels;
  5358. each(labels, function (labelShape) {
  5359. var container = self.getContainer(labelShape.get('top'));
  5360. var start = self.getOffsetPoint(labelShape.get('value'));
  5361. var _self$getSidePoint = self.getSidePoint(start, labelOffset),
  5362. x = _self$getSidePoint.x,
  5363. y = _self$getSidePoint.y;
  5364. labelShape.attr(mix({
  5365. x: x,
  5366. y: y
  5367. }, self.getTextAlignInfo(start, labelOffset), labelShape.get('textStyle')));
  5368. labelShape._id = self._id + '-' + labelShape.attr('text');
  5369. container.add(labelShape);
  5370. });
  5371. };
  5372. _proto.drawLine = function drawLine() {};
  5373. _proto.drawGrid = function drawGrid(grid) {
  5374. var self = this;
  5375. var gridPoints = self.gridPoints,
  5376. ticks = self.ticks;
  5377. var gridCfg = grid;
  5378. var count = gridPoints.length;
  5379. each(gridPoints, function (subPoints, index) {
  5380. if (isFunction(grid)) {
  5381. var tick = ticks[index] || {};
  5382. var executedGrid = grid(tick.text, index, count);
  5383. gridCfg = executedGrid ? mix({}, Global._defaultAxis.grid, executedGrid) : null;
  5384. }
  5385. if (gridCfg) {
  5386. var type = gridCfg.type; // has two types: 'line' and 'arc'
  5387. var points = subPoints.points;
  5388. var container = self.getContainer(gridCfg.top);
  5389. var shape;
  5390. if (type === 'arc') {
  5391. var center = self.center,
  5392. startAngle = self.startAngle,
  5393. endAngle = self.endAngle;
  5394. var radius = Vector2.length([points[0].x - center.x, points[0].y - center.y]);
  5395. shape = container.addShape('Arc', {
  5396. className: 'axis-grid',
  5397. attrs: mix({
  5398. x: center.x,
  5399. y: center.y,
  5400. startAngle: startAngle,
  5401. endAngle: endAngle,
  5402. r: radius
  5403. }, gridCfg)
  5404. });
  5405. } else {
  5406. shape = container.addShape('Polyline', {
  5407. className: 'axis-grid',
  5408. attrs: mix({
  5409. points: points
  5410. }, gridCfg)
  5411. });
  5412. }
  5413. shape._id = subPoints._id;
  5414. }
  5415. });
  5416. };
  5417. _proto.getOffsetPoint = function getOffsetPoint() {};
  5418. _proto.getAxisVector = function getAxisVector() {};
  5419. _proto.getOffsetVector = function getOffsetVector(point, offset) {
  5420. var self = this;
  5421. var axisVector = self.getAxisVector(point);
  5422. var normal = Vector2.normalize([], axisVector);
  5423. var factor = self.offsetFactor;
  5424. var verticalVector = [normal[1] * -1 * factor, normal[0] * factor];
  5425. return Vector2.scale([], verticalVector, offset);
  5426. };
  5427. _proto.getSidePoint = function getSidePoint(point, offset) {
  5428. var self = this;
  5429. var offsetVector = self.getOffsetVector(point, offset);
  5430. return {
  5431. x: point.x + offsetVector[0],
  5432. y: point.y + offsetVector[1]
  5433. };
  5434. };
  5435. _proto.getTextAlignInfo = function getTextAlignInfo(point, offset) {
  5436. var self = this;
  5437. var offsetVector = self.getOffsetVector(point, offset);
  5438. var align;
  5439. var baseLine;
  5440. if (offsetVector[0] > 0) {
  5441. align = 'left';
  5442. } else if (offsetVector[0] < 0) {
  5443. align = 'right';
  5444. } else {
  5445. align = 'center';
  5446. }
  5447. if (offsetVector[1] > 0) {
  5448. baseLine = 'top';
  5449. } else if (offsetVector[1] < 0) {
  5450. baseLine = 'bottom';
  5451. } else {
  5452. baseLine = 'middle';
  5453. }
  5454. return {
  5455. textAlign: align,
  5456. textBaseline: baseLine
  5457. };
  5458. };
  5459. _proto.getContainer = function getContainer(isTop) {
  5460. var frontContainer = this.frontContainer,
  5461. backContainer = this.backContainer;
  5462. return isTop ? frontContainer : backContainer;
  5463. };
  5464. return Abastract;
  5465. }();
  5466. var Line = /*#__PURE__*/function (_Abstract) {
  5467. _inheritsLoose(Line, _Abstract);
  5468. function Line() {
  5469. return _Abstract.apply(this, arguments) || this;
  5470. }
  5471. var _proto = Line.prototype;
  5472. _proto._initDefaultCfg = function _initDefaultCfg() {
  5473. _Abstract.prototype._initDefaultCfg.call(this);
  5474. this.start = null;
  5475. this.end = null;
  5476. };
  5477. _proto.getOffsetPoint = function getOffsetPoint(value) {
  5478. var start = this.start,
  5479. end = this.end;
  5480. return {
  5481. x: start.x + (end.x - start.x) * value,
  5482. y: start.y + (end.y - start.y) * value
  5483. };
  5484. };
  5485. _proto.getAxisVector = function getAxisVector() {
  5486. var start = this.start,
  5487. end = this.end;
  5488. return [end.x - start.x, end.y - start.y];
  5489. };
  5490. _proto.drawLine = function drawLine(lineCfg) {
  5491. var container = this.getContainer(lineCfg.top);
  5492. var start = this.start,
  5493. end = this.end;
  5494. container.addShape('line', {
  5495. className: 'axis-line',
  5496. attrs: mix({
  5497. x1: start.x,
  5498. y1: start.y,
  5499. x2: end.x,
  5500. y2: end.y
  5501. }, lineCfg)
  5502. });
  5503. };
  5504. return Line;
  5505. }(Abastract);
  5506. Abastract.Line = Line;
  5507. function formatTicks(ticks) {
  5508. var tmp = ticks.slice(0);
  5509. if (tmp.length > 0) {
  5510. var first = tmp[0];
  5511. var last = tmp[tmp.length - 1];
  5512. if (first.value !== 0) {
  5513. tmp.unshift({
  5514. value: 0
  5515. });
  5516. }
  5517. if (last.value !== 1) {
  5518. tmp.push({
  5519. value: 1
  5520. });
  5521. }
  5522. }
  5523. return tmp;
  5524. }
  5525. var AxisController = /*#__PURE__*/function () {
  5526. function AxisController(cfg) {
  5527. this.axisCfg = {};
  5528. this.frontPlot = null;
  5529. this.backPlot = null;
  5530. this.axes = {}; // store the axes's options
  5531. mix(this, cfg);
  5532. }
  5533. var _proto = AxisController.prototype;
  5534. _proto._isHide = function _isHide(field) {
  5535. var axisCfg = this.axisCfg;
  5536. return !axisCfg || axisCfg[field] === false;
  5537. };
  5538. _proto._getLinePosition = function _getLinePosition(scale, dimType, index, transposed) {
  5539. var position = '';
  5540. var field = scale.field;
  5541. var axisCfg = this.axisCfg;
  5542. if (axisCfg[field] && axisCfg[field].position) {
  5543. position = axisCfg[field].position;
  5544. } else if (dimType === 'x') {
  5545. position = transposed ? 'left' : 'bottom';
  5546. } else if (dimType === 'y') {
  5547. position = index ? 'right' : 'left';
  5548. if (transposed) {
  5549. position = 'bottom';
  5550. }
  5551. }
  5552. return position;
  5553. };
  5554. _proto._getLineCfg = function _getLineCfg(coord, dimType, position) {
  5555. var start;
  5556. var end;
  5557. var factor = 1; // Mark clockwise or counterclockwise
  5558. if (dimType === 'x') {
  5559. start = {
  5560. x: 0,
  5561. y: 0
  5562. };
  5563. end = {
  5564. x: 1,
  5565. y: 0
  5566. };
  5567. } else {
  5568. if (position === 'right') {
  5569. // there will be several y axes
  5570. start = {
  5571. x: 1,
  5572. y: 0
  5573. };
  5574. end = {
  5575. x: 1,
  5576. y: 1
  5577. };
  5578. } else {
  5579. start = {
  5580. x: 0,
  5581. y: 0
  5582. };
  5583. end = {
  5584. x: 0,
  5585. y: 1
  5586. };
  5587. factor = -1;
  5588. }
  5589. }
  5590. if (coord.transposed) {
  5591. factor *= -1;
  5592. }
  5593. return {
  5594. offsetFactor: factor,
  5595. start: coord.convertPoint(start),
  5596. end: coord.convertPoint(end)
  5597. };
  5598. };
  5599. _proto._getCircleCfg = function _getCircleCfg(coord) {
  5600. return {
  5601. startAngle: coord.startAngle,
  5602. endAngle: coord.endAngle,
  5603. center: coord.center,
  5604. radius: coord.circleRadius
  5605. };
  5606. };
  5607. _proto._getRadiusCfg = function _getRadiusCfg(coord) {
  5608. var transposed = coord.transposed;
  5609. var start;
  5610. var end;
  5611. if (transposed) {
  5612. start = {
  5613. x: 0,
  5614. y: 0
  5615. };
  5616. end = {
  5617. x: 1,
  5618. y: 0
  5619. };
  5620. } else {
  5621. start = {
  5622. x: 0,
  5623. y: 0
  5624. };
  5625. end = {
  5626. x: 0,
  5627. y: 1
  5628. };
  5629. }
  5630. return {
  5631. offsetFactor: -1,
  5632. start: coord.convertPoint(start),
  5633. end: coord.convertPoint(end)
  5634. };
  5635. };
  5636. _proto._getAxisCfg = function _getAxisCfg(coord, scale, verticalScale, dimType, defaultCfg) {
  5637. var _this = this;
  5638. var self = this;
  5639. var axisCfg = this.axisCfg;
  5640. var ticks = scale.getTicks();
  5641. var cfg = deepMix({
  5642. ticks: ticks,
  5643. frontContainer: this.frontPlot,
  5644. backContainer: this.backPlot
  5645. }, defaultCfg, axisCfg[scale.field]);
  5646. var labels = [];
  5647. var label = cfg.label;
  5648. var count = ticks.length;
  5649. var maxWidth = 0;
  5650. var maxHeight = 0;
  5651. var labelCfg = label;
  5652. each(ticks, function (tick, index) {
  5653. if (isFunction(label)) {
  5654. var executedLabel = label(tick.text, index, count);
  5655. labelCfg = executedLabel ? mix({}, Global._defaultAxis.label, executedLabel) : null;
  5656. }
  5657. if (labelCfg) {
  5658. var textStyle = {};
  5659. if (labelCfg.textAlign) {
  5660. textStyle.textAlign = labelCfg.textAlign;
  5661. }
  5662. if (labelCfg.textBaseline) {
  5663. textStyle.textBaseline = labelCfg.textBaseline;
  5664. }
  5665. var container = labelCfg.top ? _this.frontPlot : _this.backPlot;
  5666. var axisLabel = container.addShape('text', {
  5667. className: 'axis-label',
  5668. aria: false,
  5669. attrs: mix({
  5670. x: 0,
  5671. y: 0,
  5672. text: tick.text,
  5673. fontFamily: self.chart.get('canvas').get('fontFamily')
  5674. }, labelCfg),
  5675. value: tick.value,
  5676. textStyle: textStyle,
  5677. top: labelCfg.top,
  5678. context: self.chart.get('canvas').get('context')
  5679. });
  5680. labels.push(axisLabel);
  5681. var _axisLabel$getBBox = axisLabel.getBBox(),
  5682. width = _axisLabel$getBBox.width,
  5683. height = _axisLabel$getBBox.height;
  5684. maxWidth = Math.max(maxWidth, width);
  5685. maxHeight = Math.max(maxHeight, height);
  5686. }
  5687. });
  5688. cfg.labels = labels;
  5689. cfg.maxWidth = maxWidth;
  5690. cfg.maxHeight = maxHeight;
  5691. return cfg;
  5692. };
  5693. _proto._createAxis = function _createAxis(coord, scale, verticalScale, dimType, index) {
  5694. if (index === void 0) {
  5695. index = '';
  5696. }
  5697. var self = this;
  5698. var coordType = coord.type;
  5699. var transposed = coord.transposed;
  5700. var type;
  5701. var key;
  5702. var defaultCfg;
  5703. if (coordType === 'cartesian' || coordType === 'rect') {
  5704. var position = self._getLinePosition(scale, dimType, index, transposed);
  5705. defaultCfg = Global.axis[position];
  5706. defaultCfg.position = position;
  5707. type = 'Line';
  5708. key = position;
  5709. } else {
  5710. if (dimType === 'x' && !transposed || dimType === 'y' && transposed) {
  5711. defaultCfg = Global.axis.circle;
  5712. type = 'Circle';
  5713. key = 'circle';
  5714. } else {
  5715. defaultCfg = Global.axis.radius;
  5716. type = 'Line';
  5717. key = 'radius';
  5718. }
  5719. }
  5720. var cfg = self._getAxisCfg(coord, scale, verticalScale, dimType, defaultCfg);
  5721. cfg.type = type;
  5722. cfg.dimType = dimType;
  5723. cfg.verticalScale = verticalScale;
  5724. cfg.index = index;
  5725. this.axes[key] = cfg;
  5726. };
  5727. _proto.createAxis = function createAxis(coord, xScale, yScales) {
  5728. var self = this;
  5729. if (xScale && !self._isHide(xScale.field)) {
  5730. self._createAxis(coord, xScale, yScales[0], 'x');
  5731. }
  5732. each(yScales, function (yScale, index) {
  5733. if (!self._isHide(yScale.field)) {
  5734. self._createAxis(coord, yScale, xScale, 'y', index);
  5735. }
  5736. });
  5737. var axes = this.axes;
  5738. var chart = self.chart;
  5739. if (chart._isAutoPadding()) {
  5740. var userPadding = parsePadding(chart.get('padding'));
  5741. var appendPadding = parsePadding(chart.get('appendPadding'));
  5742. var legendRange = chart.get('legendRange') || {
  5743. top: 0,
  5744. right: 0,
  5745. bottom: 0,
  5746. left: 0
  5747. };
  5748. 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]];
  5749. if (coord.isPolar) {
  5750. var circleAxis = axes.circle;
  5751. if (circleAxis) {
  5752. var maxHeight = circleAxis.maxHeight,
  5753. maxWidth = circleAxis.maxWidth,
  5754. labelOffset = circleAxis.labelOffset;
  5755. padding[0] += maxHeight + labelOffset;
  5756. padding[1] += maxWidth + labelOffset;
  5757. padding[2] += maxHeight + labelOffset;
  5758. padding[3] += maxWidth + labelOffset;
  5759. }
  5760. } else {
  5761. if (axes.right && userPadding[1] === 'auto') {
  5762. var _axes$right = axes.right,
  5763. _maxWidth = _axes$right.maxWidth,
  5764. _labelOffset = _axes$right.labelOffset;
  5765. padding[1] += _maxWidth + _labelOffset;
  5766. }
  5767. if (axes.left && userPadding[3] === 'auto') {
  5768. var _axes$left = axes.left,
  5769. _maxWidth2 = _axes$left.maxWidth,
  5770. _labelOffset2 = _axes$left.labelOffset;
  5771. padding[3] += _maxWidth2 + _labelOffset2;
  5772. }
  5773. if (axes.bottom && userPadding[2] === 'auto') {
  5774. var _axes$bottom = axes.bottom,
  5775. _maxHeight = _axes$bottom.maxHeight,
  5776. _labelOffset3 = _axes$bottom.labelOffset;
  5777. padding[2] += _maxHeight + _labelOffset3;
  5778. }
  5779. }
  5780. chart.set('_padding', padding);
  5781. chart._updateLayout(padding);
  5782. }
  5783. each(axes, function (axis) {
  5784. var type = axis.type,
  5785. grid = axis.grid,
  5786. verticalScale = axis.verticalScale,
  5787. ticks = axis.ticks,
  5788. dimType = axis.dimType,
  5789. position = axis.position,
  5790. index = axis.index;
  5791. var appendCfg;
  5792. if (coord.isPolar) {
  5793. if (type === 'Line') {
  5794. appendCfg = self._getRadiusCfg(coord);
  5795. } else if (type === 'Circle') {
  5796. appendCfg = self._getCircleCfg(coord);
  5797. }
  5798. } else {
  5799. appendCfg = self._getLineCfg(coord, dimType, position);
  5800. }
  5801. if (grid && verticalScale) {
  5802. var gridPoints = [];
  5803. var verticalTicks = formatTicks(verticalScale.getTicks());
  5804. each(ticks, function (tick) {
  5805. var subPoints = [];
  5806. each(verticalTicks, function (verticalTick) {
  5807. var x = dimType === 'x' ? tick.value : verticalTick.value;
  5808. var y = dimType === 'x' ? verticalTick.value : tick.value;
  5809. if (x >= 0 && x <= 1 && y >= 0 && y <= 1) {
  5810. var point = coord.convertPoint({
  5811. x: x,
  5812. y: y
  5813. });
  5814. subPoints.push(point);
  5815. }
  5816. });
  5817. gridPoints.push({
  5818. points: subPoints,
  5819. _id: 'axis-' + dimType + index + '-grid-' + tick.tickValue
  5820. });
  5821. });
  5822. axis.gridPoints = gridPoints;
  5823. if (coord.isPolar) {
  5824. axis.center = coord.center;
  5825. axis.startAngle = coord.startAngle;
  5826. axis.endAngle = coord.endAngle;
  5827. }
  5828. }
  5829. appendCfg._id = 'axis-' + dimType;
  5830. if (!isNil(index)) {
  5831. appendCfg._id = 'axis-' + dimType + index;
  5832. }
  5833. new Abastract[type](mix(axis, appendCfg));
  5834. });
  5835. };
  5836. _proto.clear = function clear() {
  5837. this.axes = {};
  5838. this.frontPlot.clear();
  5839. this.backPlot.clear();
  5840. };
  5841. return AxisController;
  5842. }();
  5843. var calcDirection = function calcDirection(start, end) {
  5844. var xDistance = end.x - start.x;
  5845. var yDistance = end.y - start.y; // x 的距离大于y 说明是横向,否则就是纵向
  5846. if (Math.abs(xDistance) > Math.abs(yDistance)) {
  5847. return xDistance > 0 ? 'right' : 'left';
  5848. }
  5849. return yDistance > 0 ? 'down' : 'up';
  5850. }; // 计算2点之间的距离
  5851. var calcDistance = function calcDistance(point1, point2) {
  5852. var xDistance = Math.abs(point2.x - point1.x);
  5853. var yDistance = Math.abs(point2.y - point1.y);
  5854. return Math.sqrt(xDistance * xDistance + yDistance * yDistance);
  5855. };
  5856. var getCenter = function getCenter(point1, point2) {
  5857. var x = point1.x + (point2.x - point1.x) / 2;
  5858. var y = point1.y + (point2.y - point1.y) / 2;
  5859. return {
  5860. x: x,
  5861. y: y
  5862. };
  5863. };
  5864. var PRESS_DELAY = 250;
  5865. var EventController = /*#__PURE__*/function () {
  5866. function EventController(_ref) {
  5867. var _this = this;
  5868. var canvas = _ref.canvas,
  5869. el = _ref.el;
  5870. _defineProperty(this, "_click", function (ev) {
  5871. var points = convertPoints(ev, _this.canvas);
  5872. ev.points = points;
  5873. _this.emitEvent('click', ev);
  5874. });
  5875. _defineProperty(this, "_start", function (ev) {
  5876. var points = convertPoints(ev, _this.canvas);
  5877. if (!points) {
  5878. return;
  5879. }
  5880. ev.points = points;
  5881. _this.emitEvent('touchstart', ev); // 防止上次的内容没有清理掉,重新reset下
  5882. _this.reset(); // 记录touch start 的时间
  5883. _this.startTime = Date.now(); // 记录touch start 的点
  5884. _this.startPoints = points;
  5885. if (points.length > 1) {
  5886. _this.startDistance = calcDistance(points[0], points[1]);
  5887. _this.center = getCenter(points[0], points[1]);
  5888. } else {
  5889. // 如果touchstart后停顿250ms, 则也触发press事件
  5890. _this.pressTimeout = setTimeout(function () {
  5891. // 这里固定触发press事件
  5892. var eventType = 'press';
  5893. var direction = 'none';
  5894. ev.direction = direction;
  5895. _this.emitStart(eventType, ev);
  5896. _this.emitEvent(eventType, ev);
  5897. _this.eventType = eventType;
  5898. _this.direction = direction;
  5899. }, PRESS_DELAY);
  5900. }
  5901. });
  5902. _defineProperty(this, "_move", function (ev) {
  5903. var points = convertPoints(ev, _this.canvas);
  5904. if (!points) return;
  5905. _this.clearPressTimeout();
  5906. ev.points = points;
  5907. _this.emitEvent('touchmove', ev);
  5908. var startPoints = _this.startPoints;
  5909. if (!startPoints) return; // 多指触控
  5910. if (points.length > 1) {
  5911. // touchstart的距离
  5912. var startDistance = _this.startDistance;
  5913. var currentDistance = calcDistance(points[0], points[1]);
  5914. ev.zoom = currentDistance / startDistance;
  5915. ev.center = _this.center; // 触发缩放事件
  5916. _this.emitStart('pinch', ev);
  5917. _this.emitEvent('pinch', ev);
  5918. } else {
  5919. var deltaX = points[0].x - startPoints[0].x;
  5920. var deltaY = points[0].y - startPoints[0].y;
  5921. var direction = _this.direction || calcDirection(startPoints[0], points[0]);
  5922. _this.direction = direction; // 获取press或者pan的事件类型
  5923. // press 按住滑动, pan表示平移
  5924. // 如果start后立刻move,则触发pan, 如果有停顿,则触发press
  5925. var eventType = _this.getEventType(points);
  5926. ev.direction = direction;
  5927. ev.deltaX = deltaX;
  5928. ev.deltaY = deltaY;
  5929. _this.emitStart(eventType, ev);
  5930. _this.emitEvent(eventType, ev); // 记录最后2次move的时间和坐标,为了给swipe事件用
  5931. var prevMoveTime = _this.lastMoveTime;
  5932. var now = Date.now(); // 最后2次的时间间隔一定要大于0,否则swipe没发计算
  5933. if (now - prevMoveTime > 0) {
  5934. _this.prevMoveTime = prevMoveTime;
  5935. _this.prevMovePoints = _this.lastMovePoints;
  5936. _this.lastMoveTime = now;
  5937. _this.lastMovePoints = points;
  5938. }
  5939. }
  5940. });
  5941. _defineProperty(this, "_end", function (ev) {
  5942. var points = convertPoints(ev, _this.canvas);
  5943. ev.points = points;
  5944. _this.emitEnd(ev);
  5945. _this.emitEvent('touchend', ev); // swipe事件处理, 在touchend之后触发
  5946. var lastMoveTime = _this.lastMoveTime;
  5947. var now = Date.now(); // 做这个判断是为了最后一次touchmove后到end前,还有一个停顿的过程
  5948. // 100 是拍的一个值,理论这个值会很短,一般不卡顿的话在10ms以内
  5949. if (now - lastMoveTime < 100) {
  5950. var prevMoveTime = _this.prevMoveTime || _this.startTime;
  5951. var intervalTime = lastMoveTime - prevMoveTime; // 时间间隔一定要大于0, 否则计算没意义
  5952. if (intervalTime > 0) {
  5953. var prevMovePoints = _this.prevMovePoints || _this.startPoints;
  5954. var lastMovePoints = _this.lastMovePoints; // move速率
  5955. var velocity = calcDistance(prevMovePoints[0], lastMovePoints[0]) / intervalTime; // 0.3 是参考hammerjs的设置
  5956. if (velocity > 0.3) {
  5957. ev.velocity = velocity;
  5958. ev.direction = calcDirection(prevMovePoints[0], lastMovePoints[0]);
  5959. _this.emitEvent('swipe', ev);
  5960. }
  5961. }
  5962. }
  5963. _this.reset();
  5964. var touches = ev.touches; // 当多指只释放了1指时也会触发end, 这时重新触发一次start
  5965. if (touches && touches.length > 0) {
  5966. _this._start(ev);
  5967. }
  5968. });
  5969. _defineProperty(this, "_cancel", function (ev) {
  5970. _this.emitEvent('touchcancel', ev);
  5971. _this.reset();
  5972. });
  5973. // canvasEl
  5974. this.canvas = canvas;
  5975. this.delegateEvent(el); // 用来记录当前触发的事件
  5976. this.processEvent = {};
  5977. }
  5978. var _proto = EventController.prototype;
  5979. _proto.delegateEvent = function delegateEvent(canvasEl) {
  5980. // 代理这几个事件
  5981. canvasEl.addEventListener('click', this._click);
  5982. canvasEl.addEventListener('touchstart', this._start);
  5983. canvasEl.addEventListener('touchmove', this._move);
  5984. canvasEl.addEventListener('touchend', this._end);
  5985. canvasEl.addEventListener('touchcancel', this._cancel);
  5986. };
  5987. _proto.emitEvent = function emitEvent(type, ev) {
  5988. var canvas = this.canvas;
  5989. canvas.emit(type, ev);
  5990. };
  5991. _proto.getEventType = function getEventType(points) {
  5992. var eventType = this.eventType,
  5993. canvas = this.canvas,
  5994. startTime = this.startTime,
  5995. startPoints = this.startPoints;
  5996. if (eventType) {
  5997. return eventType;
  5998. }
  5999. var type;
  6000. var panEventListeners = canvas.__events.pan; // 如果没有pan事件的监听,默认都是press
  6001. if (!panEventListeners || !panEventListeners.length) {
  6002. type = 'press';
  6003. } else {
  6004. // 如果有pan事件的处理,press则需要停顿250ms, 且移动距离小于10
  6005. var now = Date.now();
  6006. if (now - startTime > PRESS_DELAY && calcDistance(startPoints[0], points[0]) < 10) {
  6007. type = 'press';
  6008. } else {
  6009. type = 'pan';
  6010. }
  6011. }
  6012. this.eventType = type;
  6013. return type;
  6014. };
  6015. _proto.enable = function enable(eventType) {
  6016. this.processEvent[eventType] = true;
  6017. } // 是否进行中的事件
  6018. ;
  6019. _proto.isProcess = function isProcess(eventType) {
  6020. return this.processEvent[eventType];
  6021. } // 触发start事件
  6022. ;
  6023. _proto.emitStart = function emitStart(type, ev) {
  6024. if (this.isProcess(type)) {
  6025. return;
  6026. }
  6027. this.enable(type);
  6028. this.emitEvent(type + "start", ev);
  6029. } // 触发end事件
  6030. ;
  6031. _proto.emitEnd = function emitEnd(ev) {
  6032. var _this2 = this;
  6033. var processEvent = this.processEvent;
  6034. Object.keys(processEvent).forEach(function (type) {
  6035. _this2.emitEvent(type + "end", ev);
  6036. delete processEvent[type];
  6037. });
  6038. };
  6039. _proto.clearPressTimeout = function clearPressTimeout() {
  6040. if (this.pressTimeout) {
  6041. clearTimeout(this.pressTimeout);
  6042. this.pressTimeout = 0;
  6043. }
  6044. };
  6045. _proto.reset = function reset() {
  6046. this.clearPressTimeout();
  6047. this.startTime = 0;
  6048. this.startPoints = null;
  6049. this.startDistance = 0;
  6050. this.direction = null;
  6051. this.eventType = null;
  6052. this.pinch = false;
  6053. this.prevMoveTime = 0;
  6054. this.prevMovePoints = null;
  6055. this.lastMoveTime = 0;
  6056. this.lastMovePoints = null;
  6057. };
  6058. return EventController;
  6059. }();
  6060. var CanvasElement = /*#__PURE__*/function (_EventEmit) {
  6061. _inheritsLoose(CanvasElement, _EventEmit);
  6062. function CanvasElement(ctx) {
  6063. var _this;
  6064. _this = _EventEmit.call(this) || this;
  6065. _this.context = ctx; // canvas实际的宽高 (width/height) * pixelRatio
  6066. _this.width = 0;
  6067. _this.height = 0;
  6068. _this.style = {};
  6069. _this.currentStyle = {};
  6070. _this.attrs = {}; // 用来标识是CanvasElement实例
  6071. _this.isCanvasElement = true;
  6072. return _this;
  6073. }
  6074. var _proto = CanvasElement.prototype;
  6075. _proto.getContext = function getContext()
  6076. /* type */
  6077. {
  6078. return this.context;
  6079. };
  6080. _proto.getBoundingClientRect = function getBoundingClientRect() {
  6081. var width = this.width;
  6082. var height = this.height; // 默认都处理成可视窗口的顶部位置
  6083. return {
  6084. top: 0,
  6085. right: width,
  6086. bottom: height,
  6087. left: 0
  6088. };
  6089. };
  6090. _proto.setAttribute = function setAttribute(key, value) {
  6091. this.attrs[key] = value;
  6092. };
  6093. _proto.addEventListener = function addEventListener(type, listener) {
  6094. this.on(type, listener);
  6095. };
  6096. _proto.removeEventListener = function removeEventListener(type, listener) {
  6097. this.off(type, listener);
  6098. };
  6099. _proto.dispatchEvent = function dispatchEvent(type, e) {
  6100. this.emit(type, e);
  6101. };
  6102. return CanvasElement;
  6103. }(EventEmit);
  6104. function supportEventListener(canvas) {
  6105. if (!canvas) {
  6106. return false;
  6107. } // 非 HTMLCanvasElement
  6108. if (canvas.nodeType !== 1 || !canvas.nodeName || canvas.nodeName.toLowerCase() !== 'canvas') {
  6109. return false;
  6110. } // 微信小程序canvas.getContext('2d')时也是CanvasRenderingContext2D
  6111. // 也会有ctx.canvas, 而且nodeType也是1,所以还要在看下是否支持addEventListener
  6112. var support = false;
  6113. try {
  6114. canvas.addEventListener('eventTest', function () {
  6115. support = true;
  6116. });
  6117. canvas.dispatchEvent(new Event('eventTest'));
  6118. } catch (error) {
  6119. support = false;
  6120. }
  6121. return support;
  6122. }
  6123. var CanvasElement$1 = {
  6124. create: function create(ctx) {
  6125. if (!ctx) {
  6126. return null;
  6127. }
  6128. if (supportEventListener(ctx.canvas)) {
  6129. return ctx.canvas;
  6130. }
  6131. return new CanvasElement(ctx);
  6132. }
  6133. };
  6134. function _mod(n, m) {
  6135. return (n % m + m) % m;
  6136. }
  6137. function _addStop(steps, gradient) {
  6138. each(steps, function (item) {
  6139. item = item.split(':');
  6140. gradient.addColorStop(Number(item[0]), item[1]);
  6141. });
  6142. } // the string format: 'l(0) 0:#ffffff 0.5:#7ec2f3 1:#1890ff'
  6143. function _parseLineGradient(color, shape, context) {
  6144. var arr = color.split(' ');
  6145. var angle = arr[0].slice(2, arr[0].length - 1);
  6146. angle = _mod(parseFloat(angle) * Math.PI / 180, Math.PI * 2);
  6147. var steps = arr.slice(1);
  6148. var _shape$getBBox = shape.getBBox(),
  6149. minX = _shape$getBBox.minX,
  6150. minY = _shape$getBBox.minY,
  6151. maxX = _shape$getBBox.maxX,
  6152. maxY = _shape$getBBox.maxY;
  6153. var start;
  6154. var end;
  6155. if (angle >= 0 && angle < 0.5 * Math.PI) {
  6156. start = {
  6157. x: minX,
  6158. y: minY
  6159. };
  6160. end = {
  6161. x: maxX,
  6162. y: maxY
  6163. };
  6164. } else if (0.5 * Math.PI <= angle && angle < Math.PI) {
  6165. start = {
  6166. x: maxX,
  6167. y: minY
  6168. };
  6169. end = {
  6170. x: minX,
  6171. y: maxY
  6172. };
  6173. } else if (Math.PI <= angle && angle < 1.5 * Math.PI) {
  6174. start = {
  6175. x: maxX,
  6176. y: maxY
  6177. };
  6178. end = {
  6179. x: minX,
  6180. y: minY
  6181. };
  6182. } else {
  6183. start = {
  6184. x: minX,
  6185. y: maxY
  6186. };
  6187. end = {
  6188. x: maxX,
  6189. y: minY
  6190. };
  6191. }
  6192. var tanTheta = Math.tan(angle);
  6193. var tanTheta2 = tanTheta * tanTheta;
  6194. var x = (end.x - start.x + tanTheta * (end.y - start.y)) / (tanTheta2 + 1) + start.x;
  6195. var y = tanTheta * (end.x - start.x + tanTheta * (end.y - start.y)) / (tanTheta2 + 1) + start.y;
  6196. var gradient = context.createLinearGradient(start.x, start.y, x, y);
  6197. _addStop(steps, gradient);
  6198. return gradient;
  6199. } // the string format: 'r(0.5, 0.5, 0.1) 0:#ffffff 1:#1890ff'
  6200. function _parseRadialGradient(color, shape, context) {
  6201. var arr = color.split(' ');
  6202. var circleCfg = arr[0].slice(2, arr[0].length - 1);
  6203. circleCfg = circleCfg.split(',');
  6204. var fx = parseFloat(circleCfg[0]);
  6205. var fy = parseFloat(circleCfg[1]);
  6206. var fr = parseFloat(circleCfg[2]);
  6207. var steps = arr.slice(1); // if radius is 0, no gradient, stroke with the last color
  6208. if (fr === 0) {
  6209. var _color = steps[steps.length - 1];
  6210. return _color.split(':')[1];
  6211. }
  6212. var _shape$getBBox2 = shape.getBBox(),
  6213. width = _shape$getBBox2.width,
  6214. height = _shape$getBBox2.height,
  6215. minX = _shape$getBBox2.minX,
  6216. minY = _shape$getBBox2.minY;
  6217. var r = Math.sqrt(width * width + height * height) / 2;
  6218. var gradient = context.createRadialGradient(minX + width * fx, minY + height * fy, fr * r, minX + width / 2, minY + height / 2, r);
  6219. _addStop(steps, gradient);
  6220. return gradient;
  6221. }
  6222. function parseStyle(color, shape, context) {
  6223. if (color[1] === '(') {
  6224. try {
  6225. var firstCode = color[0];
  6226. if (firstCode === 'l') {
  6227. return _parseLineGradient(color, shape, context);
  6228. } else if (firstCode === 'r') {
  6229. return _parseRadialGradient(color, shape, context);
  6230. }
  6231. } catch (ev) {
  6232. console.error('error in parsing gradient string, please check if there are any extra whitespaces.');
  6233. console.error(ev);
  6234. }
  6235. }
  6236. return color;
  6237. }
  6238. var ALIAS_ATTRS_MAP = {
  6239. stroke: 'strokeStyle',
  6240. fill: 'fillStyle',
  6241. opacity: 'globalAlpha'
  6242. };
  6243. var SHAPE_ATTRS = ['fillStyle', 'font', 'globalAlpha', 'lineCap', 'lineWidth', 'lineJoin', 'miterLimit', 'shadowBlur', 'shadowColor', 'shadowOffsetX', 'shadowOffsetY', 'strokeStyle', 'textAlign', 'textBaseline', 'lineDash', 'shadow' // 兼容支付宝小程序
  6244. ];
  6245. var CLIP_SHAPES = ['circle', 'sector', 'polygon', 'rect', 'polyline'];
  6246. var Element = /*#__PURE__*/function () {
  6247. var _proto = Element.prototype;
  6248. _proto._initProperties = function _initProperties() {
  6249. this._attrs = {
  6250. zIndex: 0,
  6251. visible: true,
  6252. destroyed: false
  6253. };
  6254. };
  6255. function Element(cfg) {
  6256. this._initProperties();
  6257. mix(this._attrs, cfg);
  6258. var attrs = this._attrs.attrs;
  6259. if (attrs) {
  6260. this.initAttrs(attrs);
  6261. }
  6262. this.initTransform();
  6263. }
  6264. _proto.get = function get(name) {
  6265. return this._attrs[name];
  6266. };
  6267. _proto.set = function set(name, value) {
  6268. this._attrs[name] = value;
  6269. };
  6270. _proto.isGroup = function isGroup() {
  6271. return this.get('isGroup');
  6272. };
  6273. _proto.isShape = function isShape() {
  6274. return this.get('isShape');
  6275. };
  6276. _proto.initAttrs = function initAttrs(attrs) {
  6277. this.attr(mix(this.getDefaultAttrs(), attrs));
  6278. };
  6279. _proto.getDefaultAttrs = function getDefaultAttrs() {
  6280. return {};
  6281. };
  6282. _proto._setAttr = function _setAttr(name, value) {
  6283. var attrs = this._attrs.attrs;
  6284. if (name === 'clip') {
  6285. value = this._setAttrClip(value);
  6286. } else {
  6287. var alias = ALIAS_ATTRS_MAP[name];
  6288. if (alias) {
  6289. attrs[alias] = value;
  6290. }
  6291. }
  6292. attrs[name] = value;
  6293. };
  6294. _proto._getAttr = function _getAttr(name) {
  6295. return this._attrs.attrs[name];
  6296. } // _afterAttrsSet() {}
  6297. ;
  6298. _proto._setAttrClip = function _setAttrClip(clip) {
  6299. if (clip && CLIP_SHAPES.indexOf(clip._attrs.type) > -1) {
  6300. if (clip.get('canvas') === null) {
  6301. clip = Object.assign({}, clip);
  6302. }
  6303. clip.set('parent', this.get('parent'));
  6304. clip.set('context', this.get('context'));
  6305. return clip;
  6306. }
  6307. return null;
  6308. };
  6309. _proto.attr = function attr(name, value) {
  6310. var self = this;
  6311. if (self.get('destroyed')) return null;
  6312. var argumentsLen = arguments.length;
  6313. if (argumentsLen === 0) {
  6314. return self._attrs.attrs;
  6315. }
  6316. if (isObject(name)) {
  6317. this._attrs.bbox = null;
  6318. for (var k in name) {
  6319. self._setAttr(k, name[k]);
  6320. }
  6321. if (self._afterAttrsSet) {
  6322. self._afterAttrsSet();
  6323. }
  6324. return self;
  6325. }
  6326. if (argumentsLen === 2) {
  6327. this._attrs.bbox = null;
  6328. self._setAttr(name, value);
  6329. if (self._afterAttrsSet) {
  6330. self._afterAttrsSet();
  6331. }
  6332. return self;
  6333. }
  6334. return self._getAttr(name);
  6335. };
  6336. _proto.getParent = function getParent() {
  6337. return this.get('parent');
  6338. };
  6339. _proto.draw = function draw(context) {
  6340. if (this.get('destroyed')) {
  6341. return;
  6342. }
  6343. if (this.get('visible')) {
  6344. this.setContext(context);
  6345. this.drawInner(context);
  6346. this.restoreContext(context);
  6347. }
  6348. };
  6349. _proto.setContext = function setContext(context) {
  6350. var clip = this._attrs.attrs.clip;
  6351. context.save();
  6352. if (clip) {
  6353. clip.resetTransform(context);
  6354. clip.createPath(context);
  6355. context.clip();
  6356. }
  6357. this.resetContext(context);
  6358. this.resetTransform(context);
  6359. };
  6360. _proto.restoreContext = function restoreContext(context) {
  6361. context.restore();
  6362. };
  6363. _proto.resetContext = function resetContext(context) {
  6364. var elAttrs = this._attrs.attrs;
  6365. for (var k in elAttrs) {
  6366. if (SHAPE_ATTRS.indexOf(k) > -1) {
  6367. var v = elAttrs[k];
  6368. if ((k === 'fillStyle' || k === 'strokeStyle') && v) {
  6369. v = parseStyle(v, this, context);
  6370. }
  6371. if (k === 'lineDash' && context.setLineDash && isArray(v)) {
  6372. context.setLineDash(v);
  6373. } else {
  6374. context[k] = v;
  6375. }
  6376. }
  6377. }
  6378. };
  6379. _proto.hasFill = function hasFill() {
  6380. return this.get('canFill') && this._attrs.attrs.fillStyle;
  6381. };
  6382. _proto.hasStroke = function hasStroke() {
  6383. return this.get('canStroke') && this._attrs.attrs.strokeStyle;
  6384. };
  6385. _proto.drawInner = function drawInner()
  6386. /* context */
  6387. {};
  6388. _proto.show = function show() {
  6389. this.set('visible', true);
  6390. return this;
  6391. };
  6392. _proto.hide = function hide() {
  6393. this.set('visible', false);
  6394. return this;
  6395. };
  6396. _proto.isVisible = function isVisible() {
  6397. return this.get('visible');
  6398. };
  6399. _proto.getAriaLabel = function getAriaLabel() {
  6400. var _this$_attrs = this._attrs,
  6401. destroyed = _this$_attrs.destroyed,
  6402. visible = _this$_attrs.visible,
  6403. isShape = _this$_attrs.isShape,
  6404. aria = _this$_attrs.aria;
  6405. if (destroyed || !visible || isShape && !aria) {
  6406. return;
  6407. }
  6408. return this._getAriaLabel();
  6409. };
  6410. _proto._getAriaLabel = function _getAriaLabel() {
  6411. return this._attrs.ariaLabel;
  6412. };
  6413. _proto._removeFromParent = function _removeFromParent() {
  6414. var parent = this.get('parent');
  6415. if (parent) {
  6416. var children = parent.get('children');
  6417. remove(children, this);
  6418. }
  6419. return this;
  6420. };
  6421. _proto.remove = function remove(destroy) {
  6422. if (destroy) {
  6423. this.destroy();
  6424. } else {
  6425. this._removeFromParent();
  6426. }
  6427. };
  6428. _proto.destroy = function destroy() {
  6429. var destroyed = this.get('destroyed');
  6430. if (destroyed) {
  6431. return null;
  6432. }
  6433. this._removeFromParent();
  6434. this._attrs = {};
  6435. this.set('destroyed', true);
  6436. };
  6437. _proto.getBBox = function getBBox() {
  6438. return {
  6439. minX: 0,
  6440. maxX: 0,
  6441. minY: 0,
  6442. maxY: 0,
  6443. width: 0,
  6444. height: 0
  6445. };
  6446. };
  6447. _proto.initTransform = function initTransform() {
  6448. var attrs = this._attrs.attrs || {};
  6449. if (!attrs.matrix) {
  6450. attrs.matrix = [1, 0, 0, 1, 0, 0];
  6451. }
  6452. this._attrs.attrs = attrs;
  6453. };
  6454. _proto.getMatrix = function getMatrix() {
  6455. return this._attrs.attrs.matrix;
  6456. };
  6457. _proto.setMatrix = function setMatrix(m) {
  6458. this._attrs.attrs.matrix = [m[0], m[1], m[2], m[3], m[4], m[5]];
  6459. };
  6460. _proto.transform = function transform(actions) {
  6461. var matrix = this._attrs.attrs.matrix;
  6462. this._attrs.attrs.matrix = Matrix.transform(matrix, actions);
  6463. return this;
  6464. };
  6465. _proto.setTransform = function setTransform(actions) {
  6466. this._attrs.attrs.matrix = [1, 0, 0, 1, 0, 0];
  6467. return this.transform(actions);
  6468. };
  6469. _proto.translate = function translate(x, y) {
  6470. var matrix = this._attrs.attrs.matrix;
  6471. Matrix.translate(matrix, matrix, [x, y]);
  6472. };
  6473. _proto.rotate = function rotate(rad) {
  6474. var matrix = this._attrs.attrs.matrix;
  6475. Matrix.rotate(matrix, matrix, rad);
  6476. };
  6477. _proto.scale = function scale(sx, sy) {
  6478. var matrix = this._attrs.attrs.matrix;
  6479. Matrix.scale(matrix, matrix, [sx, sy]);
  6480. };
  6481. _proto.moveTo = function moveTo(x, y) {
  6482. var cx = this._attrs.x || 0;
  6483. var cy = this._attrs.y || 0;
  6484. this.translate(x - cx, y - cy);
  6485. this.set('x', x);
  6486. this.set('y', y);
  6487. };
  6488. _proto.apply = function apply(v) {
  6489. var m = this._attrs.attrs.matrix;
  6490. Vector2.transformMat2d(v, v, m);
  6491. return this;
  6492. };
  6493. _proto.resetTransform = function resetTransform(context) {
  6494. var mo = this._attrs.attrs.matrix;
  6495. if (Matrix.isChanged(mo)) {
  6496. context.transform(mo[0], mo[1], mo[2], mo[3], mo[4], mo[5]);
  6497. }
  6498. };
  6499. _proto.isDestroyed = function isDestroyed() {
  6500. return this.get('destroyed');
  6501. };
  6502. return Element;
  6503. }();
  6504. var Shape$2 = /*#__PURE__*/function (_Element) {
  6505. _inheritsLoose(Shape, _Element);
  6506. function Shape() {
  6507. return _Element.apply(this, arguments) || this;
  6508. }
  6509. var _proto = Shape.prototype;
  6510. _proto._initProperties = function _initProperties() {
  6511. this._attrs = {
  6512. zIndex: 0,
  6513. visible: true,
  6514. destroyed: false,
  6515. isShape: true,
  6516. attrs: {}
  6517. };
  6518. };
  6519. _proto.getType = function getType() {
  6520. return this._attrs.type;
  6521. };
  6522. _proto.drawInner = function drawInner(context) {
  6523. var self = this;
  6524. var attrs = self.get('attrs');
  6525. self.createPath(context);
  6526. var originOpacity = context.globalAlpha;
  6527. if (self.hasFill()) {
  6528. var fillOpacity = attrs.fillOpacity;
  6529. if (!isNil(fillOpacity) && fillOpacity !== 1) {
  6530. context.globalAlpha = fillOpacity;
  6531. context.fill();
  6532. context.globalAlpha = originOpacity;
  6533. } else {
  6534. context.fill();
  6535. }
  6536. }
  6537. if (self.hasStroke()) {
  6538. var lineWidth = attrs.lineWidth;
  6539. if (lineWidth > 0) {
  6540. var strokeOpacity = attrs.strokeOpacity;
  6541. if (!isNil(strokeOpacity) && strokeOpacity !== 1) {
  6542. context.globalAlpha = strokeOpacity;
  6543. }
  6544. context.stroke();
  6545. }
  6546. }
  6547. };
  6548. _proto.getBBox = function getBBox() {
  6549. var bbox = this._attrs.bbox;
  6550. if (!bbox) {
  6551. bbox = this.calculateBox();
  6552. if (bbox) {
  6553. bbox.x = bbox.minX;
  6554. bbox.y = bbox.minY;
  6555. bbox.width = bbox.maxX - bbox.minX;
  6556. bbox.height = bbox.maxY - bbox.minY;
  6557. }
  6558. this._attrs.bbox = bbox;
  6559. }
  6560. return bbox;
  6561. };
  6562. _proto.calculateBox = function calculateBox() {
  6563. return null;
  6564. };
  6565. _proto.createPath = function createPath() {};
  6566. return Shape;
  6567. }(Element);
  6568. function parseRadius(radius, width, height) {
  6569. radius = parsePadding(radius); // 都为0
  6570. if (!radius[0] && !radius[1] && !radius[2] && !radius[3]) {
  6571. return radius;
  6572. }
  6573. var minWidth = Math.max(radius[0] + radius[1], radius[2] + radius[3]);
  6574. var minHeight = Math.max(radius[0] + radius[3], radius[1] + radius[2]);
  6575. var scale = Math.min(width / minWidth, height / minHeight);
  6576. if (scale < 1) {
  6577. return radius.map(function (r) {
  6578. return r * scale;
  6579. });
  6580. }
  6581. return radius;
  6582. }
  6583. var Rect = /*#__PURE__*/function (_Shape) {
  6584. _inheritsLoose(Rect, _Shape);
  6585. function Rect() {
  6586. return _Shape.apply(this, arguments) || this;
  6587. }
  6588. var _proto = Rect.prototype;
  6589. _proto._initProperties = function _initProperties() {
  6590. _Shape.prototype._initProperties.call(this);
  6591. this._attrs.canFill = true;
  6592. this._attrs.canStroke = true;
  6593. this._attrs.type = 'rect';
  6594. };
  6595. _proto.getDefaultAttrs = function getDefaultAttrs() {
  6596. return {
  6597. x: 0,
  6598. y: 0,
  6599. width: 0,
  6600. height: 0,
  6601. radius: 0,
  6602. lineWidth: 0
  6603. };
  6604. };
  6605. _proto.createRadiusPath = function createRadiusPath(context, x, y, width, height, radius) {
  6606. radius = parseRadius(radius, width, height);
  6607. context.moveTo(x + radius[0], y);
  6608. context.lineTo(x + width - radius[1], y);
  6609. context.arc(x + width - radius[1], y + radius[1], radius[1], -Math.PI / 2, 0, false);
  6610. context.lineTo(x + width, y + height - radius[2]);
  6611. context.arc(x + width - radius[2], y + height - radius[2], radius[2], 0, Math.PI / 2, false);
  6612. context.lineTo(x + radius[3], y + height);
  6613. context.arc(x + radius[3], y + height - radius[3], radius[3], Math.PI / 2, Math.PI, false);
  6614. context.lineTo(x, y + radius[0]);
  6615. context.arc(x + radius[0], y + radius[0], radius[0], Math.PI, Math.PI * 3 / 2, false);
  6616. context.closePath();
  6617. };
  6618. _proto.createPath = function createPath(context) {
  6619. var self = this;
  6620. var attrs = self.get('attrs');
  6621. var x = attrs.x,
  6622. y = attrs.y,
  6623. width = attrs.width,
  6624. height = attrs.height,
  6625. radius = attrs.radius;
  6626. context.beginPath();
  6627. if (!radius || !(width * height)) {
  6628. context.rect(x, y, width, height);
  6629. } else {
  6630. this.createRadiusPath(context, x, y, width, height, radius);
  6631. }
  6632. };
  6633. _proto.calculateBox = function calculateBox() {
  6634. var attrs = this.get('attrs');
  6635. var x = attrs.x,
  6636. y = attrs.y,
  6637. width = attrs.width,
  6638. height = attrs.height;
  6639. return {
  6640. minX: x,
  6641. minY: y,
  6642. maxX: x + width,
  6643. maxY: y + height
  6644. };
  6645. };
  6646. return Rect;
  6647. }(Shape$2);
  6648. var imageCaches = {};
  6649. var ImageShape = /*#__PURE__*/function (_Rect) {
  6650. _inheritsLoose(ImageShape, _Rect);
  6651. function ImageShape() {
  6652. return _Rect.apply(this, arguments) || this;
  6653. }
  6654. var _proto = ImageShape.prototype;
  6655. _proto._initProperties = function _initProperties() {
  6656. _Rect.prototype._initProperties.call(this);
  6657. this._attrs.canFill = false;
  6658. this._attrs.canStroke = false;
  6659. this._attrs.loading = false;
  6660. this._attrs.image = null;
  6661. this._attrs.type = 'image';
  6662. };
  6663. _proto.draw = function draw(context) {
  6664. var _this = this;
  6665. // 如果图片还在loading中直接返回,等下次绘制
  6666. if (this.get('loading')) {
  6667. return;
  6668. } // 如果已经有image对象,直接绘制,会调用createPath绘制
  6669. var image = this.get('image');
  6670. if (image) {
  6671. _Rect.prototype.draw.call(this, context);
  6672. return;
  6673. }
  6674. var attrs = this.get('attrs');
  6675. var src = attrs.src;
  6676. if (src && window.Image) {
  6677. var cacheImage = this.get('cacheImage'); // 如果有缓存,则直接从缓存中拿
  6678. if (cacheImage && imageCaches[src]) {
  6679. this.set('image', imageCaches[src]);
  6680. this.draw(context);
  6681. return;
  6682. }
  6683. this.set('loading', true);
  6684. var _image = new Image(); // 设置跨域, 等同于 image.crossOrigin = 'anonymous'
  6685. _image.crossOrigin = '';
  6686. _image.onload = function () {
  6687. _this.set('loading', false);
  6688. _this.set('image', _image);
  6689. _this.draw(context);
  6690. }; // src 一定要在 crossOrigin 之后,否则 toDataURL 就会报 SecurityError
  6691. _image.src = src; // 设置全局缓存
  6692. if (cacheImage) {
  6693. imageCaches[src] = _image;
  6694. }
  6695. }
  6696. };
  6697. _proto.createPath = function createPath(context) {
  6698. var image = this.get('image');
  6699. this.drawImage(context, image);
  6700. };
  6701. _proto.drawImage = function drawImage(context, image) {
  6702. var _this$_attrs = this._attrs,
  6703. attrs = _this$_attrs.attrs,
  6704. destroyed = _this$_attrs.destroyed;
  6705. if (destroyed) {
  6706. return;
  6707. }
  6708. var x = attrs.x,
  6709. y = attrs.y,
  6710. width = attrs.width,
  6711. height = attrs.height,
  6712. sx = attrs.sx,
  6713. sy = attrs.sy,
  6714. swidth = attrs.swidth,
  6715. sheight = attrs.sheight,
  6716. radius = attrs.radius,
  6717. fillOpacity = attrs.fillOpacity;
  6718. if (radius) {
  6719. context.save();
  6720. this.createRadiusPath(context, x, y, width, height, radius);
  6721. context.clip();
  6722. } // 设置透明度
  6723. var originOpacity = context.globalAlpha;
  6724. if (!isNil(fillOpacity)) {
  6725. context.globalAlpha = fillOpacity;
  6726. }
  6727. if (!isNil(sx) && !isNil(sy) && !isNil(swidth) && !isNil(sheight)) {
  6728. context.drawImage(image, sx, sy, swidth, sheight, x, y, width, height);
  6729. } else {
  6730. context.drawImage(image, x, y, width, height);
  6731. }
  6732. context.globalAlpha = originOpacity;
  6733. if (radius) {
  6734. // 因为 save 和 restore 会一定程度上影响绘图性能,所以只在必要是调用
  6735. context.restore();
  6736. }
  6737. };
  6738. return ImageShape;
  6739. }(Rect);
  6740. var Circle = /*#__PURE__*/function (_Shape) {
  6741. _inheritsLoose(Circle, _Shape);
  6742. function Circle() {
  6743. return _Shape.apply(this, arguments) || this;
  6744. }
  6745. var _proto = Circle.prototype;
  6746. _proto._initProperties = function _initProperties() {
  6747. _Shape.prototype._initProperties.call(this);
  6748. this._attrs.canFill = true;
  6749. this._attrs.canStroke = true;
  6750. this._attrs.type = 'circle';
  6751. };
  6752. _proto.getDefaultAttrs = function getDefaultAttrs() {
  6753. return {
  6754. x: 0,
  6755. y: 0,
  6756. r: 0,
  6757. lineWidth: 0
  6758. };
  6759. };
  6760. _proto.createPath = function createPath(context) {
  6761. var attrs = this.get('attrs');
  6762. var x = attrs.x,
  6763. y = attrs.y,
  6764. r = attrs.r;
  6765. context.beginPath();
  6766. context.arc(x, y, r, 0, Math.PI * 2, false);
  6767. context.closePath();
  6768. };
  6769. _proto.calculateBox = function calculateBox() {
  6770. var attrs = this.get('attrs');
  6771. var x = attrs.x,
  6772. y = attrs.y,
  6773. r = attrs.r;
  6774. return {
  6775. minX: x - r,
  6776. maxX: x + r,
  6777. minY: y - r,
  6778. maxY: y + r
  6779. };
  6780. };
  6781. return Circle;
  6782. }(Shape$2);
  6783. var start = Vector2.create();
  6784. var end = Vector2.create();
  6785. var extremity = Vector2.create();
  6786. function getCubicBezierXYatT(startPt, controlPt1, controlPt2, endPt, T) {
  6787. var x = CubicN(T, startPt.x, controlPt1.x, controlPt2.x, endPt.x);
  6788. var y = CubicN(T, startPt.y, controlPt1.y, controlPt2.y, endPt.y);
  6789. return {
  6790. x: x,
  6791. y: y
  6792. };
  6793. } // cubic helper formula at T distance
  6794. function CubicN(T, a, b, c, d) {
  6795. var t2 = T * T;
  6796. var t3 = t2 * T;
  6797. 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;
  6798. }
  6799. function cubicBezierBounds(c) {
  6800. var minX = Infinity;
  6801. var maxX = -Infinity;
  6802. var minY = Infinity;
  6803. var maxY = -Infinity;
  6804. var s = {
  6805. x: c[0],
  6806. y: c[1]
  6807. };
  6808. var c1 = {
  6809. x: c[2],
  6810. y: c[3]
  6811. };
  6812. var c2 = {
  6813. x: c[4],
  6814. y: c[5]
  6815. };
  6816. var e = {
  6817. x: c[6],
  6818. y: c[7]
  6819. };
  6820. for (var t = 0; t < 100; t++) {
  6821. var pt = getCubicBezierXYatT(s, c1, c2, e, t / 100);
  6822. if (pt.x < minX) {
  6823. minX = pt.x;
  6824. }
  6825. if (pt.x > maxX) {
  6826. maxX = pt.x;
  6827. }
  6828. if (pt.y < minY) {
  6829. minY = pt.y;
  6830. }
  6831. if (pt.y > maxY) {
  6832. maxY = pt.y;
  6833. }
  6834. }
  6835. return {
  6836. minX: minX,
  6837. minY: minY,
  6838. maxX: maxX,
  6839. maxY: maxY
  6840. };
  6841. }
  6842. function getBBoxFromPoints(points, lineWidth) {
  6843. if (points.length === 0) {
  6844. return;
  6845. }
  6846. var p = points[0];
  6847. var left = p.x;
  6848. var right = p.x;
  6849. var top = p.y;
  6850. var bottom = p.y;
  6851. var len = points.length;
  6852. for (var i = 1; i < len; i++) {
  6853. p = points[i];
  6854. left = Math.min(left, p.x);
  6855. right = Math.max(right, p.x);
  6856. top = Math.min(top, p.y);
  6857. bottom = Math.max(bottom, p.y);
  6858. }
  6859. lineWidth = lineWidth / 2 || 0;
  6860. return {
  6861. minX: left - lineWidth,
  6862. minY: top - lineWidth,
  6863. maxX: right + lineWidth,
  6864. maxY: bottom + lineWidth
  6865. };
  6866. }
  6867. function getBBoxFromLine(x0, y0, x1, y1, lineWidth) {
  6868. lineWidth = lineWidth / 2 || 0;
  6869. return {
  6870. minX: Math.min(x0, x1) - lineWidth,
  6871. minY: Math.min(y0, y1) - lineWidth,
  6872. maxX: Math.max(x0, x1) + lineWidth,
  6873. maxY: Math.max(y0, y1) + lineWidth
  6874. };
  6875. }
  6876. function getBBoxFromArc(x, y, r, startAngle, endAngle, anticlockwise) {
  6877. var diff = Math.abs(startAngle - endAngle);
  6878. if (diff % (Math.PI * 2) < 1e-4 && diff > 1e-4) {
  6879. // Is a circle
  6880. return {
  6881. minX: x - r,
  6882. minY: y - r,
  6883. maxX: x + r,
  6884. maxY: y + r
  6885. };
  6886. }
  6887. start[0] = Math.cos(startAngle) * r + x;
  6888. start[1] = Math.sin(startAngle) * r + y;
  6889. end[0] = Math.cos(endAngle) * r + x;
  6890. end[1] = Math.sin(endAngle) * r + y;
  6891. var min = [0, 0];
  6892. var max = [0, 0];
  6893. Vector2.min(min, start, end);
  6894. Vector2.max(max, start, end); // Thresh to [0, Math.PI * 2]
  6895. startAngle = startAngle % (Math.PI * 2);
  6896. if (startAngle < 0) {
  6897. startAngle = startAngle + Math.PI * 2;
  6898. }
  6899. endAngle = endAngle % (Math.PI * 2);
  6900. if (endAngle < 0) {
  6901. endAngle = endAngle + Math.PI * 2;
  6902. }
  6903. if (startAngle > endAngle && !anticlockwise) {
  6904. endAngle += Math.PI * 2;
  6905. } else if (startAngle < endAngle && anticlockwise) {
  6906. startAngle += Math.PI * 2;
  6907. }
  6908. if (anticlockwise) {
  6909. var tmp = endAngle;
  6910. endAngle = startAngle;
  6911. startAngle = tmp;
  6912. }
  6913. for (var angle = 0; angle < endAngle; angle += Math.PI / 2) {
  6914. if (angle > startAngle) {
  6915. extremity[0] = Math.cos(angle) * r + x;
  6916. extremity[1] = Math.sin(angle) * r + y;
  6917. Vector2.min(min, extremity, min);
  6918. Vector2.max(max, extremity, max);
  6919. }
  6920. }
  6921. return {
  6922. minX: min[0],
  6923. minY: min[1],
  6924. maxX: max[0],
  6925. maxY: max[1]
  6926. };
  6927. }
  6928. function getBBoxFromBezierGroup(points, lineWidth) {
  6929. var minX = Infinity;
  6930. var maxX = -Infinity;
  6931. var minY = Infinity;
  6932. var maxY = -Infinity;
  6933. for (var i = 0, len = points.length; i < len; i++) {
  6934. var bbox = cubicBezierBounds(points[i]);
  6935. if (bbox.minX < minX) {
  6936. minX = bbox.minX;
  6937. }
  6938. if (bbox.maxX > maxX) {
  6939. maxX = bbox.maxX;
  6940. }
  6941. if (bbox.minY < minY) {
  6942. minY = bbox.minY;
  6943. }
  6944. if (bbox.maxY > maxY) {
  6945. maxY = bbox.maxY;
  6946. }
  6947. }
  6948. lineWidth = lineWidth / 2 || 0;
  6949. return {
  6950. minX: minX - lineWidth,
  6951. minY: minY - lineWidth,
  6952. maxX: maxX + lineWidth,
  6953. maxY: maxY + lineWidth
  6954. };
  6955. }
  6956. var Line$1 = /*#__PURE__*/function (_Shape) {
  6957. _inheritsLoose(Line, _Shape);
  6958. function Line() {
  6959. return _Shape.apply(this, arguments) || this;
  6960. }
  6961. var _proto = Line.prototype;
  6962. _proto._initProperties = function _initProperties() {
  6963. _Shape.prototype._initProperties.call(this);
  6964. this._attrs.canStroke = true;
  6965. this._attrs.type = 'line';
  6966. };
  6967. _proto.getDefaultAttrs = function getDefaultAttrs() {
  6968. return {
  6969. x1: 0,
  6970. y1: 0,
  6971. x2: 0,
  6972. y2: 0,
  6973. lineWidth: 1
  6974. };
  6975. };
  6976. _proto.createPath = function createPath(context) {
  6977. var attrs = this.get('attrs');
  6978. var x1 = attrs.x1,
  6979. y1 = attrs.y1,
  6980. x2 = attrs.x2,
  6981. y2 = attrs.y2;
  6982. context.beginPath();
  6983. context.moveTo(x1, y1);
  6984. context.lineTo(x2, y2);
  6985. };
  6986. _proto.calculateBox = function calculateBox() {
  6987. var attrs = this.get('attrs');
  6988. var x1 = attrs.x1,
  6989. y1 = attrs.y1,
  6990. x2 = attrs.x2,
  6991. y2 = attrs.y2,
  6992. lineWidth = attrs.lineWidth;
  6993. return getBBoxFromLine(x1, y1, x2, y2, lineWidth);
  6994. };
  6995. return Line;
  6996. }(Shape$2);
  6997. var Polygon = /*#__PURE__*/function (_Shape) {
  6998. _inheritsLoose(Polygon, _Shape);
  6999. function Polygon() {
  7000. return _Shape.apply(this, arguments) || this;
  7001. }
  7002. var _proto = Polygon.prototype;
  7003. _proto._initProperties = function _initProperties() {
  7004. _Shape.prototype._initProperties.call(this);
  7005. this._attrs.canFill = true;
  7006. this._attrs.canStroke = true;
  7007. this._attrs.type = 'polygon';
  7008. };
  7009. _proto.getDefaultAttrs = function getDefaultAttrs() {
  7010. return {
  7011. points: null,
  7012. lineWidth: 0
  7013. };
  7014. };
  7015. _proto.createPath = function createPath(context) {
  7016. var self = this;
  7017. var attrs = self.get('attrs');
  7018. var points = attrs.points;
  7019. context.beginPath();
  7020. for (var i = 0, len = points.length; i < len; i++) {
  7021. var point = points[i];
  7022. if (i === 0) {
  7023. context.moveTo(point.x, point.y);
  7024. } else {
  7025. context.lineTo(point.x, point.y);
  7026. }
  7027. }
  7028. context.closePath();
  7029. };
  7030. _proto.calculateBox = function calculateBox() {
  7031. var attrs = this.get('attrs');
  7032. var points = attrs.points;
  7033. return getBBoxFromPoints(points);
  7034. };
  7035. return Polygon;
  7036. }(Shape$2);
  7037. /**
  7038. * @fileOverview convert the line to curve
  7039. * @author dxq613@gmail.com
  7040. */
  7041. function getPoint(v) {
  7042. return [v.x, v.y];
  7043. }
  7044. function smoothBezier(points, smooth, isLoop, constraint) {
  7045. var cps = [];
  7046. var prevPoint;
  7047. var nextPoint;
  7048. var hasConstraint = !!constraint;
  7049. var min;
  7050. var max;
  7051. var point;
  7052. var len;
  7053. var l;
  7054. var i;
  7055. if (hasConstraint) {
  7056. min = [Infinity, Infinity];
  7057. max = [-Infinity, -Infinity];
  7058. for (i = 0, l = points.length; i < l; i++) {
  7059. point = getPoint(points[i]);
  7060. Vector2.min(min, min, point);
  7061. Vector2.max(max, max, point);
  7062. }
  7063. Vector2.min(min, min, constraint[0]);
  7064. Vector2.max(max, max, constraint[1]);
  7065. }
  7066. for (i = 0, len = points.length; i < len; i++) {
  7067. point = getPoint(points[i]);
  7068. if (isLoop) {
  7069. prevPoint = getPoint(points[i ? i - 1 : len - 1]);
  7070. nextPoint = getPoint(points[(i + 1) % len]);
  7071. } else {
  7072. if (i === 0 || i === len - 1) {
  7073. cps.push([point[0], point[1]]);
  7074. continue;
  7075. } else {
  7076. prevPoint = getPoint(points[i - 1]);
  7077. nextPoint = getPoint(points[i + 1]);
  7078. }
  7079. }
  7080. var v = Vector2.sub([], nextPoint, prevPoint);
  7081. Vector2.scale(v, v, smooth);
  7082. var d0 = Vector2.distance(point, prevPoint);
  7083. var d1 = Vector2.distance(point, nextPoint);
  7084. var sum = d0 + d1;
  7085. if (sum !== 0) {
  7086. d0 /= sum;
  7087. d1 /= sum;
  7088. }
  7089. var v1 = Vector2.scale([], v, -d0);
  7090. var v2 = Vector2.scale([], v, d1);
  7091. var cp0 = Vector2.add([], point, v1);
  7092. var cp1 = Vector2.add([], point, v2);
  7093. if (hasConstraint) {
  7094. Vector2.max(cp0, cp0, min);
  7095. Vector2.min(cp0, cp0, max);
  7096. Vector2.max(cp1, cp1, min);
  7097. Vector2.min(cp1, cp1, max);
  7098. }
  7099. cps.push([cp0[0], cp0[1]]);
  7100. cps.push([cp1[0], cp1[1]]);
  7101. }
  7102. if (isLoop) {
  7103. cps.push(cps.shift());
  7104. }
  7105. return cps;
  7106. }
  7107. function catmullRom2bezier(pointList, z, constraint) {
  7108. var isLoop = !!z;
  7109. var controlPointList = smoothBezier(pointList, 0.4, isLoop, constraint);
  7110. var len = pointList.length;
  7111. var d1 = [];
  7112. var cp1;
  7113. var cp2;
  7114. var p;
  7115. for (var i = 0; i < len - 1; i++) {
  7116. cp1 = controlPointList[i * 2];
  7117. cp2 = controlPointList[i * 2 + 1];
  7118. p = pointList[i + 1];
  7119. d1.push(['C', cp1[0], cp1[1], cp2[0], cp2[1], p.x, p.y]);
  7120. }
  7121. if (isLoop) {
  7122. cp1 = controlPointList[len];
  7123. cp2 = controlPointList[len + 1];
  7124. p = pointList[0];
  7125. d1.push(['C', cp1[0], cp1[1], cp2[0], cp2[1], p.x, p.y]);
  7126. }
  7127. return d1;
  7128. }
  7129. function _filterPoints(points) {
  7130. var filteredPoints = [];
  7131. for (var i = 0, len = points.length; i < len; i++) {
  7132. var point = points[i];
  7133. if (!isNaN(point.x) && !isNaN(point.y)) {
  7134. filteredPoints.push(point);
  7135. }
  7136. }
  7137. return filteredPoints;
  7138. }
  7139. var Polyline = /*#__PURE__*/function (_Shape) {
  7140. _inheritsLoose(Polyline, _Shape);
  7141. function Polyline() {
  7142. return _Shape.apply(this, arguments) || this;
  7143. }
  7144. var _proto = Polyline.prototype;
  7145. _proto._initProperties = function _initProperties() {
  7146. _Shape.prototype._initProperties.call(this);
  7147. this._attrs.canFill = true;
  7148. this._attrs.canStroke = true;
  7149. this._attrs.type = 'polyline';
  7150. };
  7151. _proto.getDefaultAttrs = function getDefaultAttrs() {
  7152. return {
  7153. points: null,
  7154. lineWidth: 1,
  7155. smooth: false
  7156. };
  7157. };
  7158. _proto.createPath = function createPath(context) {
  7159. var self = this;
  7160. var attrs = self.get('attrs');
  7161. var points = attrs.points,
  7162. smooth = attrs.smooth;
  7163. var filteredPoints = _filterPoints(points);
  7164. context.beginPath();
  7165. if (filteredPoints.length) {
  7166. context.moveTo(filteredPoints[0].x, filteredPoints[0].y);
  7167. if (smooth) {
  7168. var constaint = [[0, 0], [1, 1]];
  7169. var sps = catmullRom2bezier(filteredPoints, false, constaint);
  7170. for (var i = 0, n = sps.length; i < n; i++) {
  7171. var sp = sps[i];
  7172. context.bezierCurveTo(sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]);
  7173. }
  7174. } else {
  7175. var _i;
  7176. var l;
  7177. for (_i = 1, l = filteredPoints.length - 1; _i < l; _i++) {
  7178. context.lineTo(filteredPoints[_i].x, filteredPoints[_i].y);
  7179. }
  7180. context.lineTo(filteredPoints[l].x, filteredPoints[l].y);
  7181. }
  7182. }
  7183. };
  7184. _proto.calculateBox = function calculateBox() {
  7185. var attrs = this.get('attrs');
  7186. var points = attrs.points,
  7187. smooth = attrs.smooth,
  7188. lineWidth = attrs.lineWidth;
  7189. var filteredPoints = _filterPoints(points);
  7190. if (smooth) {
  7191. var newPoints = [];
  7192. var constaint = [[0, 0], [1, 1]];
  7193. var sps = catmullRom2bezier(filteredPoints, false, constaint);
  7194. for (var i = 0, n = sps.length; i < n; i++) {
  7195. var sp = sps[i];
  7196. if (i === 0) {
  7197. newPoints.push([filteredPoints[0].x, filteredPoints[0].y, sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]]);
  7198. } else {
  7199. var lastPoint = sps[i - 1];
  7200. newPoints.push([lastPoint[5], lastPoint[6], sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]]);
  7201. }
  7202. }
  7203. return getBBoxFromBezierGroup(newPoints, lineWidth);
  7204. }
  7205. return getBBoxFromPoints(filteredPoints, lineWidth);
  7206. };
  7207. return Polyline;
  7208. }(Shape$2);
  7209. var Arc = /*#__PURE__*/function (_Shape) {
  7210. _inheritsLoose(Arc, _Shape);
  7211. function Arc() {
  7212. return _Shape.apply(this, arguments) || this;
  7213. }
  7214. var _proto = Arc.prototype;
  7215. _proto._initProperties = function _initProperties() {
  7216. _Shape.prototype._initProperties.call(this);
  7217. this._attrs.canStroke = true;
  7218. this._attrs.canFill = true;
  7219. this._attrs.type = 'arc';
  7220. };
  7221. _proto.getDefaultAttrs = function getDefaultAttrs() {
  7222. return {
  7223. x: 0,
  7224. y: 0,
  7225. r: 0,
  7226. startAngle: 0,
  7227. endAngle: Math.PI * 2,
  7228. anticlockwise: false,
  7229. lineWidth: 1
  7230. };
  7231. };
  7232. _proto.createPath = function createPath(context) {
  7233. var attrs = this.get('attrs');
  7234. var x = attrs.x,
  7235. y = attrs.y,
  7236. r = attrs.r,
  7237. startAngle = attrs.startAngle,
  7238. endAngle = attrs.endAngle,
  7239. anticlockwise = attrs.anticlockwise;
  7240. context.beginPath();
  7241. if (startAngle !== endAngle) {
  7242. context.arc(x, y, r, startAngle, endAngle, anticlockwise);
  7243. }
  7244. };
  7245. _proto.calculateBox = function calculateBox() {
  7246. var attrs = this.get('attrs');
  7247. var x = attrs.x,
  7248. y = attrs.y,
  7249. r = attrs.r,
  7250. startAngle = attrs.startAngle,
  7251. endAngle = attrs.endAngle,
  7252. anticlockwise = attrs.anticlockwise;
  7253. return getBBoxFromArc(x, y, r, startAngle, endAngle, anticlockwise);
  7254. };
  7255. return Arc;
  7256. }(Shape$2);
  7257. var Sector = /*#__PURE__*/function (_Shape) {
  7258. _inheritsLoose(Sector, _Shape);
  7259. function Sector() {
  7260. return _Shape.apply(this, arguments) || this;
  7261. }
  7262. var _proto = Sector.prototype;
  7263. _proto._initProperties = function _initProperties() {
  7264. _Shape.prototype._initProperties.call(this);
  7265. this._attrs.canFill = true;
  7266. this._attrs.canStroke = true;
  7267. this._attrs.type = 'sector';
  7268. };
  7269. _proto.getDefaultAttrs = function getDefaultAttrs() {
  7270. return {
  7271. x: 0,
  7272. y: 0,
  7273. lineWidth: 0,
  7274. r: 0,
  7275. r0: 0,
  7276. startAngle: 0,
  7277. endAngle: Math.PI * 2,
  7278. anticlockwise: false
  7279. };
  7280. };
  7281. _proto.createPath = function createPath(context) {
  7282. var attrs = this.get('attrs');
  7283. var x = attrs.x,
  7284. y = attrs.y,
  7285. startAngle = attrs.startAngle,
  7286. endAngle = attrs.endAngle,
  7287. r = attrs.r,
  7288. r0 = attrs.r0,
  7289. anticlockwise = attrs.anticlockwise;
  7290. context.beginPath();
  7291. var unitX = Math.cos(startAngle);
  7292. var unitY = Math.sin(startAngle);
  7293. context.moveTo(unitX * r0 + x, unitY * r0 + y);
  7294. context.lineTo(unitX * r + x, unitY * r + y); // 当扇形的角度非常小的时候,就不进行弧线的绘制;或者整个只有1个扇形时,会出现end<0的情况不绘制
  7295. if (Math.abs(endAngle - startAngle) > 0.0001 || startAngle === 0 && endAngle < 0) {
  7296. context.arc(x, y, r, startAngle, endAngle, anticlockwise);
  7297. context.lineTo(Math.cos(endAngle) * r0 + x, Math.sin(endAngle) * r0 + y);
  7298. if (r0 !== 0) {
  7299. context.arc(x, y, r0, endAngle, startAngle, !anticlockwise);
  7300. }
  7301. }
  7302. context.closePath();
  7303. };
  7304. _proto.calculateBox = function calculateBox() {
  7305. var attrs = this.get('attrs');
  7306. var x = attrs.x,
  7307. y = attrs.y,
  7308. r = attrs.r,
  7309. r0 = attrs.r0,
  7310. startAngle = attrs.startAngle,
  7311. endAngle = attrs.endAngle,
  7312. anticlockwise = attrs.anticlockwise;
  7313. var outerBBox = getBBoxFromArc(x, y, r, startAngle, endAngle, anticlockwise);
  7314. var innerBBox = getBBoxFromArc(x, y, r0, startAngle, endAngle, anticlockwise);
  7315. return {
  7316. minX: Math.min(outerBBox.minX, innerBBox.minX),
  7317. minY: Math.min(outerBBox.minY, innerBBox.minY),
  7318. maxX: Math.max(outerBBox.maxX, innerBBox.maxX),
  7319. maxY: Math.max(outerBBox.maxY, innerBBox.maxY)
  7320. };
  7321. };
  7322. return Sector;
  7323. }(Shape$2);
  7324. var Rect$1 = {
  7325. calcRotatedBox: function calcRotatedBox(_ref) {
  7326. var width = _ref.width,
  7327. height = _ref.height,
  7328. rotate = _ref.rotate;
  7329. var absRotate = Math.abs(rotate);
  7330. return {
  7331. width: Math.abs(width * Math.cos(absRotate) + height * Math.sin(absRotate)),
  7332. height: Math.abs(height * Math.cos(absRotate) + width * Math.sin(absRotate))
  7333. };
  7334. }
  7335. };
  7336. var textWidthCacheCounter = 0;
  7337. var textWidthCache = {};
  7338. var TEXT_CACHE_MAX = 5000;
  7339. var Text = /*#__PURE__*/function (_Shape) {
  7340. _inheritsLoose(Text, _Shape);
  7341. function Text() {
  7342. return _Shape.apply(this, arguments) || this;
  7343. }
  7344. var _proto = Text.prototype;
  7345. _proto._initProperties = function _initProperties() {
  7346. _Shape.prototype._initProperties.call(this);
  7347. this._attrs.canFill = true;
  7348. this._attrs.canStroke = true;
  7349. this._attrs.type = 'text';
  7350. };
  7351. _proto.getDefaultAttrs = function getDefaultAttrs() {
  7352. return {
  7353. lineWidth: 0,
  7354. lineCount: 1,
  7355. fontSize: 12,
  7356. fontFamily: 'sans-serif',
  7357. fontStyle: 'normal',
  7358. fontWeight: 'normal',
  7359. fontVariant: 'normal',
  7360. textAlign: 'start',
  7361. textBaseline: 'bottom',
  7362. lineHeight: null,
  7363. textArr: null
  7364. };
  7365. };
  7366. _proto._getFontStyle = function _getFontStyle() {
  7367. var attrs = this._attrs.attrs;
  7368. var fontSize = attrs.fontSize,
  7369. fontFamily = attrs.fontFamily,
  7370. fontWeight = attrs.fontWeight,
  7371. fontStyle = attrs.fontStyle,
  7372. fontVariant = attrs.fontVariant;
  7373. return fontStyle + " " + fontVariant + " " + fontWeight + " " + fontSize + "px " + fontFamily;
  7374. };
  7375. _proto._afterAttrsSet = function _afterAttrsSet() {
  7376. var attrs = this._attrs.attrs;
  7377. attrs.font = this._getFontStyle();
  7378. if (attrs.text) {
  7379. var text = attrs.text;
  7380. var textArr = null;
  7381. var lineCount = 1;
  7382. if (isString(text) && text.indexOf('\n') !== -1) {
  7383. textArr = text.split('\n');
  7384. lineCount = textArr.length;
  7385. }
  7386. attrs.lineCount = lineCount;
  7387. attrs.textArr = textArr;
  7388. }
  7389. this.set('attrs', attrs);
  7390. };
  7391. _proto._getTextHeight = function _getTextHeight() {
  7392. var attrs = this._attrs.attrs;
  7393. if (attrs.height) {
  7394. return attrs.height;
  7395. }
  7396. var lineCount = attrs.lineCount;
  7397. var fontSize = attrs.fontSize * 1;
  7398. if (lineCount > 1) {
  7399. var spaceingY = this._getSpaceingY();
  7400. return fontSize * lineCount + spaceingY * (lineCount - 1);
  7401. }
  7402. return fontSize;
  7403. };
  7404. _proto._getSpaceingY = function _getSpaceingY() {
  7405. var attrs = this._attrs.attrs;
  7406. var lineHeight = attrs.lineHeight;
  7407. var fontSize = attrs.fontSize * 1;
  7408. return lineHeight ? lineHeight - fontSize : fontSize * 0.14;
  7409. };
  7410. _proto.drawInner = function drawInner(context) {
  7411. var self = this;
  7412. var attrs = self._attrs.attrs;
  7413. var text = attrs.text;
  7414. var x = attrs.x;
  7415. var y = attrs.y;
  7416. if (isNil(text) || isNaN(x) || isNaN(y)) {
  7417. // text will be 0
  7418. return;
  7419. }
  7420. var textArr = attrs.textArr;
  7421. var fontSize = attrs.fontSize * 1;
  7422. var spaceingY = self._getSpaceingY();
  7423. if (attrs.rotate) {
  7424. // do rotation
  7425. context.translate(x, y);
  7426. context.rotate(attrs.rotate);
  7427. x = 0;
  7428. y = 0;
  7429. }
  7430. var textBaseline = attrs.textBaseline;
  7431. var height;
  7432. if (textArr) {
  7433. height = self._getTextHeight();
  7434. }
  7435. var subY; // context.beginPath();
  7436. if (self.hasFill()) {
  7437. var fillOpacity = attrs.fillOpacity;
  7438. if (!isNil(fillOpacity) && fillOpacity !== 1) {
  7439. context.globalAlpha = fillOpacity;
  7440. }
  7441. if (textArr) {
  7442. for (var i = 0, len = textArr.length; i < len; i++) {
  7443. var subText = textArr[i];
  7444. subY = y + i * (spaceingY + fontSize) - height + fontSize; // bottom;
  7445. if (textBaseline === 'middle') {
  7446. subY += height - fontSize - (height - fontSize) / 2;
  7447. }
  7448. if (textBaseline === 'top') {
  7449. subY += height - fontSize;
  7450. }
  7451. context.fillText(subText, x, subY);
  7452. }
  7453. } else {
  7454. context.fillText(text, x, y);
  7455. }
  7456. }
  7457. if (self.hasStroke()) {
  7458. if (textArr) {
  7459. for (var _i = 0, _len = textArr.length; _i < _len; _i++) {
  7460. var _subText = textArr[_i];
  7461. subY = y + _i * (spaceingY + fontSize) - height + fontSize; // bottom;
  7462. if (textBaseline === 'middle') {
  7463. subY += height - fontSize - (height - fontSize) / 2;
  7464. }
  7465. if (textBaseline === 'top') {
  7466. subY += height - fontSize;
  7467. }
  7468. context.strokeText(_subText, x, subY);
  7469. }
  7470. } else {
  7471. context.strokeText(text, x, y);
  7472. }
  7473. }
  7474. };
  7475. _proto._getAriaLabel = function _getAriaLabel() {
  7476. return this._attrs.attrs.text;
  7477. };
  7478. _proto.calculateBox = function calculateBox() {
  7479. var self = this;
  7480. var attrs = self._attrs.attrs;
  7481. var x = attrs.x,
  7482. y = attrs.y,
  7483. textAlign = attrs.textAlign,
  7484. textBaseline = attrs.textBaseline;
  7485. var width = self._getTextWidth(); // attrs.width
  7486. if (!width) {
  7487. return {
  7488. minX: x,
  7489. minY: y,
  7490. maxX: x,
  7491. maxY: y
  7492. };
  7493. }
  7494. var height = self._getTextHeight(); // attrs.height
  7495. if (attrs.rotate) {
  7496. var rotatedBox = Rect$1.calcRotatedBox({
  7497. width: width,
  7498. height: height,
  7499. rotate: attrs.rotate
  7500. });
  7501. width = rotatedBox.width;
  7502. height = rotatedBox.height;
  7503. }
  7504. var point = {
  7505. x: x,
  7506. y: y - height
  7507. }; // default textAlign: start, textBaseline: bottom
  7508. if (textAlign) {
  7509. if (textAlign === 'end' || textAlign === 'right') {
  7510. point.x -= width;
  7511. } else if (textAlign === 'center') {
  7512. point.x -= width / 2;
  7513. }
  7514. }
  7515. if (textBaseline) {
  7516. if (textBaseline === 'top') {
  7517. point.y += height;
  7518. } else if (textBaseline === 'middle') {
  7519. point.y += height / 2;
  7520. }
  7521. }
  7522. return {
  7523. minX: point.x,
  7524. minY: point.y,
  7525. maxX: point.x + width,
  7526. maxY: point.y + height
  7527. };
  7528. };
  7529. _proto._getTextWidth = function _getTextWidth() {
  7530. var attrs = this._attrs.attrs;
  7531. if (attrs.width) {
  7532. return attrs.width;
  7533. }
  7534. var text = attrs.text;
  7535. var context = this.get('context');
  7536. if (isNil(text)) return undefined;
  7537. var font = attrs.font;
  7538. var textArr = attrs.textArr;
  7539. var key = text + '' + font;
  7540. if (textWidthCache[key]) {
  7541. return textWidthCache[key];
  7542. }
  7543. var width = 0;
  7544. if (textArr) {
  7545. for (var i = 0, length = textArr.length; i < length; i++) {
  7546. var subText = textArr[i];
  7547. width = Math.max(width, measureText(subText, font, context).width);
  7548. }
  7549. } else {
  7550. width = measureText(text, font, context).width;
  7551. }
  7552. if (textWidthCacheCounter > TEXT_CACHE_MAX) {
  7553. textWidthCacheCounter = 0;
  7554. textWidthCache = {};
  7555. }
  7556. textWidthCacheCounter++;
  7557. textWidthCache[key] = width;
  7558. return width;
  7559. };
  7560. return Text;
  7561. }(Shape$2);
  7562. var Custom = /*#__PURE__*/function (_Shape) {
  7563. _inheritsLoose(Custom, _Shape);
  7564. function Custom() {
  7565. return _Shape.apply(this, arguments) || this;
  7566. }
  7567. var _proto = Custom.prototype;
  7568. _proto._initProperties = function _initProperties() {
  7569. _Shape.prototype._initProperties.call(this);
  7570. this._attrs.canFill = true;
  7571. this._attrs.canStroke = true;
  7572. this._attrs.createPath = null;
  7573. this._attrs.type = 'custom';
  7574. };
  7575. _proto.createPath = function createPath(context) {
  7576. var createPath = this.get('createPath');
  7577. createPath && createPath.call(this, context);
  7578. };
  7579. _proto.calculateBox = function calculateBox() {
  7580. var calculateBox = this.get('calculateBox');
  7581. return calculateBox && calculateBox.call(this);
  7582. };
  7583. return Custom;
  7584. }(Shape$2);
  7585. var SYMBOLS = {
  7586. circle: function circle(x, y, r, ctx) {
  7587. ctx.arc(x, y, r, 0, Math.PI * 2, false);
  7588. },
  7589. square: function square(x, y, r, ctx) {
  7590. ctx.moveTo(x - r, y - r);
  7591. ctx.lineTo(x + r, y - r);
  7592. ctx.lineTo(x + r, y + r);
  7593. ctx.lineTo(x - r, y + r);
  7594. ctx.closePath();
  7595. }
  7596. };
  7597. var Marker = /*#__PURE__*/function (_Shape) {
  7598. _inheritsLoose(Marker, _Shape);
  7599. function Marker() {
  7600. return _Shape.apply(this, arguments) || this;
  7601. }
  7602. var _proto = Marker.prototype;
  7603. _proto._initProperties = function _initProperties() {
  7604. _Shape.prototype._initProperties.call(this);
  7605. this._attrs.canFill = true;
  7606. this._attrs.canStroke = true;
  7607. this._attrs.type = 'marker';
  7608. };
  7609. _proto.getDefaultAttrs = function getDefaultAttrs() {
  7610. return {
  7611. x: 0,
  7612. y: 0,
  7613. lineWidth: 0
  7614. };
  7615. };
  7616. _proto.createPath = function createPath(context) {
  7617. var attrs = this.get('attrs');
  7618. var x = attrs.x,
  7619. y = attrs.y,
  7620. radius = attrs.radius;
  7621. var symbol = attrs.symbol || 'circle';
  7622. var method;
  7623. if (isFunction(symbol)) {
  7624. method = symbol;
  7625. } else {
  7626. method = SYMBOLS[symbol];
  7627. }
  7628. context.beginPath();
  7629. method(x, y, radius, context, this);
  7630. };
  7631. _proto.calculateBox = function calculateBox() {
  7632. var attrs = this.get('attrs');
  7633. var x = attrs.x,
  7634. y = attrs.y,
  7635. radius = attrs.radius;
  7636. return {
  7637. minX: x - radius,
  7638. minY: y - radius,
  7639. maxX: x + radius,
  7640. maxY: y + radius
  7641. };
  7642. };
  7643. return Marker;
  7644. }(Shape$2);
  7645. Shape$2.Rect = Rect;
  7646. Shape$2.Image = ImageShape;
  7647. Shape$2.Circle = Circle;
  7648. Shape$2.Line = Line$1;
  7649. Shape$2.Polygon = Polygon;
  7650. Shape$2.Polyline = Polyline;
  7651. Shape$2.Arc = Arc;
  7652. Shape$2.Sector = Sector;
  7653. Shape$2.Text = Text;
  7654. Shape$2.Custom = Custom;
  7655. Shape$2.Marker = Marker;
  7656. var SHAPE_MAP = {};
  7657. var INDEX = '_INDEX';
  7658. function getComparer(compare) {
  7659. return function (left, right) {
  7660. var result = compare(left, right);
  7661. return result === 0 ? left[INDEX] - right[INDEX] : result;
  7662. };
  7663. }
  7664. var Container = {
  7665. getGroupClass: function getGroupClass() {},
  7666. getChildren: function getChildren() {
  7667. return this.get('children');
  7668. },
  7669. addShape: function addShape(type, cfg) {
  7670. if (cfg === void 0) {
  7671. cfg = {};
  7672. }
  7673. var shapeType = SHAPE_MAP[type];
  7674. if (!shapeType) {
  7675. shapeType = upperFirst(type);
  7676. SHAPE_MAP[type] = shapeType;
  7677. }
  7678. var shape = new Shape$2[shapeType](cfg);
  7679. this.add(shape);
  7680. return shape;
  7681. },
  7682. addGroup: function addGroup(cfg) {
  7683. var groupClass = this.getGroupClass();
  7684. var rst = new groupClass(cfg);
  7685. this.add(rst);
  7686. return rst;
  7687. },
  7688. contain: function contain(item) {
  7689. var children = this.get('children');
  7690. return children.indexOf(item) > -1;
  7691. },
  7692. sort: function sort() {
  7693. var children = this.get('children');
  7694. for (var i = 0, len = children.length; i < len; i++) {
  7695. var child = children[i];
  7696. child[INDEX] = i;
  7697. }
  7698. children.sort(getComparer(function (obj1, obj2) {
  7699. return obj1.get('zIndex') - obj2.get('zIndex');
  7700. }));
  7701. return this;
  7702. },
  7703. drawChildren: function drawChildren(context) {
  7704. var children = this.get('children');
  7705. for (var i = 0, len = children.length; i < len; i++) {
  7706. var child = children[i];
  7707. child.draw(context);
  7708. }
  7709. return this;
  7710. },
  7711. clear: function clear() {
  7712. var children = this.get('children');
  7713. while (children.length !== 0) {
  7714. children[children.length - 1].remove(true);
  7715. }
  7716. return this;
  7717. },
  7718. add: function add(items) {
  7719. var self = this;
  7720. var children = self.get('children');
  7721. if (!isArray(items)) {
  7722. items = [items];
  7723. }
  7724. for (var i = 0, len = items.length; i < len; i++) {
  7725. var item = items[i];
  7726. var parent = item.get('parent');
  7727. if (parent) {
  7728. var descendants = parent.get('children');
  7729. remove(descendants, item);
  7730. }
  7731. self._setEvn(item);
  7732. children.push(item);
  7733. }
  7734. return self;
  7735. },
  7736. _setEvn: function _setEvn(item) {
  7737. var self = this;
  7738. var _self$_attrs = self._attrs,
  7739. context = _self$_attrs.context,
  7740. canvas = _self$_attrs.canvas,
  7741. aria = _self$_attrs.aria;
  7742. var _item$_attrs = item._attrs,
  7743. isGroup = _item$_attrs.isGroup,
  7744. type = _item$_attrs.type;
  7745. item._attrs.parent = self;
  7746. item._attrs.context = context;
  7747. item._attrs.canvas = canvas; // 是否需要无障碍处理
  7748. if (aria && item._attrs.aria !== false) {
  7749. item._attrs.aria = aria;
  7750. }
  7751. if (type === 'text' && canvas && canvas.get('fontFamily')) {
  7752. item._attrs.attrs.fontFamily = item._attrs.attrs.fontFamily || canvas.get('fontFamily');
  7753. }
  7754. var clip = item._attrs.attrs.clip;
  7755. if (clip) {
  7756. clip._attrs.parent = self;
  7757. clip._attrs.context = context;
  7758. clip._attrs.canvas = canvas;
  7759. }
  7760. if (isGroup) {
  7761. var children = item._attrs.children;
  7762. for (var i = 0, len = children.length; i < len; i++) {
  7763. item._setEvn(children[i]);
  7764. }
  7765. }
  7766. },
  7767. _getAriaLabel: function _getAriaLabel() {
  7768. var _this$_attrs = this._attrs,
  7769. aria = _this$_attrs.aria,
  7770. ariaLabel = _this$_attrs.ariaLabel,
  7771. children = _this$_attrs.children; // 主动关闭
  7772. if (!aria) return;
  7773. var childAriaLabels = [];
  7774. if (children && children.length) {
  7775. for (var i = 0, len = children.length; i < len; i++) {
  7776. var _childAriaLabel = children[i].getAriaLabel();
  7777. if (_childAriaLabel) {
  7778. childAriaLabels.push(_childAriaLabel);
  7779. }
  7780. }
  7781. }
  7782. var childAriaLabel = childAriaLabels.join(' '); // 2个都有时拼接成完整句子
  7783. if (ariaLabel && childAriaLabel) {
  7784. return ariaLabel + " " + childAriaLabel + " ";
  7785. } // 只有1个,或者都没有
  7786. return ariaLabel || childAriaLabel;
  7787. }
  7788. };
  7789. var Group = /*#__PURE__*/function (_Rect) {
  7790. _inheritsLoose(Group, _Rect);
  7791. function Group() {
  7792. return _Rect.apply(this, arguments) || this;
  7793. }
  7794. var _proto = Group.prototype;
  7795. _proto._initProperties = function _initProperties() {
  7796. this._attrs = {
  7797. type: 'group',
  7798. zIndex: 0,
  7799. visible: true,
  7800. destroyed: false,
  7801. isGroup: true,
  7802. canFill: true,
  7803. canStroke: true,
  7804. attrs: {},
  7805. children: []
  7806. };
  7807. };
  7808. _proto.getBBox = function getBBox() {
  7809. var self = this;
  7810. var minX = Infinity;
  7811. var maxX = -Infinity;
  7812. var minY = Infinity;
  7813. var maxY = -Infinity;
  7814. var children = self.get('children');
  7815. for (var i = 0, length = children.length; i < length; i++) {
  7816. var child = children[i];
  7817. if (child.get('visible')) {
  7818. var box = child.getBBox();
  7819. if (!box) {
  7820. continue;
  7821. }
  7822. var leftTop = [box.minX, box.minY];
  7823. var leftBottom = [box.minX, box.maxY];
  7824. var rightTop = [box.maxX, box.minY];
  7825. var rightBottom = [box.maxX, box.maxY];
  7826. var matrix = child.attr('matrix');
  7827. Vector2.transformMat2d(leftTop, leftTop, matrix);
  7828. Vector2.transformMat2d(leftBottom, leftBottom, matrix);
  7829. Vector2.transformMat2d(rightTop, rightTop, matrix);
  7830. Vector2.transformMat2d(rightBottom, rightBottom, matrix);
  7831. minX = Math.min(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0], minX);
  7832. maxX = Math.max(leftTop[0], leftBottom[0], rightTop[0], rightBottom[0], maxX);
  7833. minY = Math.min(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1], minY);
  7834. maxY = Math.max(leftTop[1], leftBottom[1], rightTop[1], rightBottom[1], maxY);
  7835. }
  7836. }
  7837. return {
  7838. minX: minX,
  7839. minY: minY,
  7840. maxX: maxX,
  7841. maxY: maxY,
  7842. x: minX,
  7843. y: minY,
  7844. width: maxX - minX,
  7845. height: maxY - minY
  7846. };
  7847. };
  7848. _proto.createPath = function createPath(context) {
  7849. var attrs = this.get('attrs'); // 只有在有fillStyle或strokeStyle 时才需要绘制
  7850. if (!attrs.fillStyle && !attrs.strokeStyle) {
  7851. return;
  7852. }
  7853. _Rect.prototype.createPath.call(this, context);
  7854. };
  7855. _proto.drawInner = function drawInner(context) {
  7856. _Rect.prototype.drawInner.call(this, context);
  7857. this.drawChildren(context);
  7858. };
  7859. _proto.destroy = function destroy() {
  7860. if (this.get('destroyed')) {
  7861. return;
  7862. }
  7863. this.clear();
  7864. _Rect.prototype.destroy.call(this);
  7865. };
  7866. return Group;
  7867. }(Rect);
  7868. mix(Group.prototype, Container, {
  7869. getGroupClass: function getGroupClass() {
  7870. return Group;
  7871. }
  7872. });
  7873. var requestAnimationFrame = typeof window === 'object' && window.requestAnimationFrame ? window.requestAnimationFrame : function (fn) {
  7874. return setTimeout(fn, 16);
  7875. };
  7876. var Canvas = /*#__PURE__*/function (_EventEmit) {
  7877. _inheritsLoose(Canvas, _EventEmit);
  7878. var _proto = Canvas.prototype;
  7879. _proto.get = function get(name) {
  7880. return this._attrs[name];
  7881. };
  7882. _proto.set = function set(name, value) {
  7883. this._attrs[name] = value;
  7884. };
  7885. function Canvas(cfg) {
  7886. var _this;
  7887. _this = _EventEmit.call(this) || this;
  7888. var title = cfg.title;
  7889. var ariaLabel = title ? substitute(lang.general.withTitle, {
  7890. title: title
  7891. }) : lang.general.title;
  7892. _this._attrs = mix({
  7893. type: 'canvas',
  7894. children: [],
  7895. ariaLabel: ariaLabel
  7896. }, cfg);
  7897. _this._initPixelRatio();
  7898. _this._initCanvas();
  7899. return _this;
  7900. }
  7901. _proto._initPixelRatio = function _initPixelRatio() {
  7902. var pixelRatio = this.get('pixelRatio');
  7903. if (!pixelRatio) {
  7904. this.set('pixelRatio', getPixelRatio());
  7905. }
  7906. };
  7907. _proto.beforeDraw = function beforeDraw() {
  7908. var context = this._attrs.context;
  7909. var el = this._attrs.el;
  7910. context && context.clearRect && context.clearRect(0, 0, el.width, el.height);
  7911. };
  7912. _proto._initCanvas = function _initCanvas() {
  7913. var self = this;
  7914. var el = self.get('el');
  7915. var context = self.get('context');
  7916. if (!el && !context) {
  7917. throw new Error('Please specify the id, el or context of the chart!');
  7918. }
  7919. var canvas;
  7920. if (el) {
  7921. // DOMElement or String
  7922. canvas = isString(el) ? getDomById(el) : el;
  7923. } else {
  7924. // 说明没有指定el
  7925. canvas = CanvasElement$1.create(context);
  7926. }
  7927. if (context && canvas && !canvas.getContext) {
  7928. canvas.getContext = function () {
  7929. return context;
  7930. };
  7931. }
  7932. var width = self.get('width');
  7933. if (!width) {
  7934. width = getWidth(canvas);
  7935. }
  7936. var height = self.get('height');
  7937. if (!height) {
  7938. height = getHeight(canvas);
  7939. }
  7940. self.set('canvas', this);
  7941. self.set('el', canvas);
  7942. self.set('context', context || canvas.getContext('2d'));
  7943. self.changeSize(width, height); // 初始化事件控制器
  7944. var eventController = new EventController({
  7945. canvas: this,
  7946. el: canvas
  7947. });
  7948. self.set('eventController', eventController);
  7949. };
  7950. _proto.changeSize = function changeSize(width, height) {
  7951. var pixelRatio = this.get('pixelRatio');
  7952. var canvasDOM = this.get('el'); // HTMLCanvasElement or canvasElement
  7953. // 浏览器环境设置style样式
  7954. if (canvasDOM.style) {
  7955. canvasDOM.style.width = width + 'px';
  7956. canvasDOM.style.height = height + 'px';
  7957. }
  7958. if (isCanvasElement(canvasDOM)) {
  7959. canvasDOM.width = width * pixelRatio;
  7960. canvasDOM.height = height * pixelRatio;
  7961. if (pixelRatio !== 1) {
  7962. var ctx = this.get('context');
  7963. ctx.scale(pixelRatio, pixelRatio);
  7964. }
  7965. }
  7966. this.set('width', width);
  7967. this.set('height', height);
  7968. };
  7969. _proto.getWidth = function getWidth() {
  7970. var pixelRatio = this.get('pixelRatio');
  7971. var width = this.get('width');
  7972. return width * pixelRatio;
  7973. };
  7974. _proto.getHeight = function getHeight() {
  7975. var pixelRatio = this.get('pixelRatio');
  7976. var height = this.get('height');
  7977. return height * pixelRatio;
  7978. };
  7979. _proto.getPointByClient = function getPointByClient(clientX, clientY) {
  7980. var el = this.get('el');
  7981. var bbox = el.getBoundingClientRect();
  7982. var width = bbox.right - bbox.left;
  7983. var height = bbox.bottom - bbox.top;
  7984. return {
  7985. x: (clientX - bbox.left) * (el.width / width),
  7986. y: (clientY - bbox.top) * (el.height / height)
  7987. };
  7988. };
  7989. _proto._beginDraw = function _beginDraw() {
  7990. this._attrs.toDraw = true;
  7991. };
  7992. _proto._endDraw = function _endDraw() {
  7993. this._attrs.toDraw = false;
  7994. };
  7995. _proto.draw = function draw() {
  7996. var self = this;
  7997. function drawInner() {
  7998. self.set('animateHandler', requestAnimationFrame(function () {
  7999. self.set('animateHandler', undefined);
  8000. if (self.get('toDraw')) {
  8001. drawInner();
  8002. }
  8003. }));
  8004. self.beforeDraw();
  8005. try {
  8006. var context = self._attrs.context;
  8007. self.drawChildren(context); // 支付宝,微信小程序,需要调context.draw才能完成绘制, 所以这里直接判断是否有.draw方法
  8008. if (context.draw) {
  8009. context.draw();
  8010. } // 设置无障碍文本
  8011. self.setAriaLabel();
  8012. } catch (ev) {
  8013. console.warn('error in draw canvas, detail as:');
  8014. console.warn(ev);
  8015. self._endDraw();
  8016. }
  8017. self._endDraw();
  8018. }
  8019. if (self.get('destroyed')) {
  8020. return;
  8021. }
  8022. if (self.get('animateHandler')) {
  8023. this._beginDraw();
  8024. } else {
  8025. drawInner();
  8026. }
  8027. } // 设置无障碍文本
  8028. ;
  8029. _proto.setAriaLabel = function setAriaLabel() {
  8030. var el = this._attrs.el;
  8031. var ariaLabel = this._getAriaLabel();
  8032. if (ariaLabel && el.setAttribute) {
  8033. el.setAttribute('aria-label', ariaLabel);
  8034. }
  8035. };
  8036. _proto.destroy = function destroy() {
  8037. if (this.get('destroyed')) {
  8038. return;
  8039. } // 需要清理 canvas 画布内容,否则会导致 spa 应用 ios 下 canvas 白屏
  8040. // https://stackoverflow.com/questions/52532614/total-canvas-memory-use-exceeds-the-maximum-limit-safari-12
  8041. // https://github.com/antvis/F2/issues/630
  8042. var el = this.get('el');
  8043. el.width = 0;
  8044. el.height = 0;
  8045. this.clear();
  8046. this._attrs = {};
  8047. this.set('destroyed', true);
  8048. };
  8049. _proto.isDestroyed = function isDestroyed() {
  8050. return this.get('destroyed');
  8051. };
  8052. return Canvas;
  8053. }(EventEmit);
  8054. mix(Canvas.prototype, Container, {
  8055. getGroupClass: function getGroupClass() {
  8056. return Group;
  8057. }
  8058. });
  8059. var engines = {};
  8060. function registerEngine(name, engine) {
  8061. engines[name] = engine;
  8062. }
  8063. function getEngine(name) {
  8064. var G = engines[name];
  8065. if (G) {
  8066. return G;
  8067. }
  8068. return {
  8069. Canvas: Canvas,
  8070. Group: Group,
  8071. Shape: Shape$2
  8072. };
  8073. }
  8074. function createCanvas(cfg) {
  8075. var renderer = cfg.renderer;
  8076. var G = getEngine(renderer);
  8077. return new G.Canvas(cfg);
  8078. }
  8079. var G = /*#__PURE__*/Object.freeze({
  8080. __proto__: null,
  8081. registerEngine: registerEngine,
  8082. getEngine: getEngine,
  8083. createCanvas: createCanvas,
  8084. Canvas: Canvas,
  8085. Group: Group,
  8086. Shape: Shape$2,
  8087. Matrix: Matrix,
  8088. Vector2: Vector2
  8089. });
  8090. function getClip(coord) {
  8091. var start = coord.start;
  8092. var end = coord.end;
  8093. var width = end.x - start.x;
  8094. var height = Math.abs(end.y - start.y);
  8095. var margin = 10;
  8096. var clip;
  8097. if (coord.isPolar) {
  8098. var circleRadius = coord.circleRadius,
  8099. center = coord.center,
  8100. startAngle = coord.startAngle,
  8101. endAngle = coord.endAngle;
  8102. clip = new Shape$2.Sector({
  8103. attrs: {
  8104. x: center.x,
  8105. y: center.y,
  8106. r: circleRadius,
  8107. r0: 0,
  8108. startAngle: startAngle,
  8109. endAngle: endAngle
  8110. }
  8111. });
  8112. } else {
  8113. clip = new Shape$2.Rect({
  8114. attrs: {
  8115. x: start.x,
  8116. y: end.y - margin,
  8117. width: width,
  8118. height: height + 2 * margin
  8119. }
  8120. });
  8121. }
  8122. clip.isClip = true;
  8123. return clip;
  8124. }
  8125. function isPointInPlot(point, plot) {
  8126. var x = point.x,
  8127. y = point.y;
  8128. var tl = plot.tl,
  8129. tr = plot.tr,
  8130. br = plot.br;
  8131. return x >= tl.x && x <= tr.x && y >= tl.y && y <= br.y;
  8132. }
  8133. var Helper = /*#__PURE__*/Object.freeze({
  8134. __proto__: null,
  8135. getClip: getClip,
  8136. isPointInPlot: isPointInPlot
  8137. });
  8138. function compare(a, b) {
  8139. return a - b;
  8140. }
  8141. function _isScaleExist(scales, compareScale) {
  8142. var flag = false;
  8143. each(scales, function (scale) {
  8144. var scaleValues = [].concat(scale.values);
  8145. var compareScaleValues = [].concat(compareScale.values);
  8146. if (scale.type === compareScale.type && scale.field === compareScale.field && scaleValues.sort(compare).toString() === compareScaleValues.sort(compare).toString()) {
  8147. flag = true;
  8148. return;
  8149. }
  8150. });
  8151. return flag;
  8152. }
  8153. var Chart = /*#__PURE__*/function (_Base) {
  8154. _inheritsLoose(Chart, _Base);
  8155. Chart.initPlugins = function initPlugins() {
  8156. return {
  8157. _plugins: [],
  8158. _cacheId: 0,
  8159. register: function register(plugins) {
  8160. var p = this._plugins;
  8161. [].concat(plugins).forEach(function (plugin) {
  8162. if (p.indexOf(plugin) === -1) {
  8163. p.push(plugin);
  8164. }
  8165. });
  8166. this._cacheId++;
  8167. },
  8168. unregister: function unregister(plugins) {
  8169. var p = this._plugins;
  8170. [].concat(plugins).forEach(function (plugin) {
  8171. var idx = p.indexOf(plugin);
  8172. if (idx !== -1) {
  8173. p.splice(idx, 1);
  8174. }
  8175. });
  8176. this._cacheId++;
  8177. },
  8178. clear: function clear() {
  8179. this._plugins = [];
  8180. this._cacheId++;
  8181. },
  8182. count: function count() {
  8183. return this._plugins.length;
  8184. },
  8185. getAll: function getAll() {
  8186. return this._plugins;
  8187. },
  8188. notify: function notify(chart, hook, args) {
  8189. var descriptors = this.descriptors(chart);
  8190. var ilen = descriptors.length;
  8191. var i;
  8192. var descriptor;
  8193. var plugin;
  8194. var params;
  8195. var method;
  8196. for (i = 0; i < ilen; ++i) {
  8197. descriptor = descriptors[i];
  8198. plugin = descriptor.plugin;
  8199. method = plugin[hook];
  8200. if (typeof method === 'function') {
  8201. params = [chart].concat(args || []);
  8202. if (method.apply(plugin, params) === false) {
  8203. return false;
  8204. }
  8205. }
  8206. }
  8207. return true;
  8208. },
  8209. descriptors: function descriptors(chart) {
  8210. var cache = chart._plugins || (chart._plugins = {});
  8211. if (cache.id === this._cacheId) {
  8212. return cache.descriptors;
  8213. }
  8214. var plugins = [];
  8215. var descriptors = [];
  8216. this._plugins.concat(chart && chart.get('plugins') || []).forEach(function (plugin) {
  8217. var idx = plugins.indexOf(plugin);
  8218. if (idx !== -1) {
  8219. return;
  8220. }
  8221. plugins.push(plugin);
  8222. descriptors.push({
  8223. plugin: plugin
  8224. });
  8225. });
  8226. cache.descriptors = descriptors;
  8227. cache.id = this._cacheId;
  8228. return descriptors;
  8229. }
  8230. };
  8231. };
  8232. var _proto = Chart.prototype;
  8233. _proto.getDefaultCfg = function getDefaultCfg() {
  8234. return {
  8235. /**
  8236. * the id of canvas
  8237. * @type {String}
  8238. */
  8239. id: null,
  8240. /** 图表渲染引擎 */
  8241. renderer: 'canvas',
  8242. rendered: false,
  8243. /**
  8244. * padding
  8245. * @type {Array|Number}
  8246. */
  8247. padding: Global.padding,
  8248. /**
  8249. * data
  8250. * @type {Array}
  8251. */
  8252. data: null,
  8253. /**
  8254. * scales of chart
  8255. * @type {Object}
  8256. */
  8257. scales: {},
  8258. /**
  8259. * @private
  8260. * geometry instances
  8261. * @type {Array}
  8262. */
  8263. geoms: [],
  8264. /**
  8265. * scale configuration
  8266. * @type {Object}
  8267. */
  8268. colDefs: null,
  8269. pixelRatio: Global.pixelRatio,
  8270. /**
  8271. * filter options
  8272. * @type {Object}
  8273. */
  8274. filters: null,
  8275. appendPadding: Global.appendPadding
  8276. };
  8277. };
  8278. _proto._syncYScales = function _syncYScales() {
  8279. var syncY = this.get('syncY');
  8280. if (!syncY) {
  8281. return;
  8282. }
  8283. var geoms = this.get('geoms');
  8284. var syncScales = [];
  8285. var min = [];
  8286. var max = [];
  8287. each(geoms, function (geom) {
  8288. var yScale = geom.getYScale();
  8289. if (yScale.isLinear) {
  8290. syncScales.push(yScale);
  8291. min.push(yScale.min);
  8292. max.push(yScale.max);
  8293. }
  8294. });
  8295. min = Math.min.apply(null, min);
  8296. max = Math.max.apply(null, max);
  8297. each(syncScales, function (scale) {
  8298. scale.change({
  8299. min: min
  8300. });
  8301. scale.change({
  8302. max: max
  8303. });
  8304. });
  8305. };
  8306. _proto._getFieldsForLegend = function _getFieldsForLegend() {
  8307. var fields = [];
  8308. var geoms = this.get('geoms');
  8309. each(geoms, function (geom) {
  8310. var attrOptions = geom.get('attrOptions');
  8311. var attrCfg = attrOptions.color;
  8312. if (attrCfg && attrCfg.field && isString(attrCfg.field)) {
  8313. var arr = attrCfg.field.split('*');
  8314. each(arr, function (item) {
  8315. if (fields.indexOf(item) === -1) {
  8316. fields.push(item);
  8317. }
  8318. });
  8319. }
  8320. });
  8321. return fields;
  8322. };
  8323. _proto._getScaleData = function _getScaleData(field) {
  8324. var data = this.get('data');
  8325. var filteredData = this.get('filteredData');
  8326. if (filteredData.length) {
  8327. var legendFields = this._getFieldsForLegend();
  8328. if (legendFields.indexOf(field) === -1) {
  8329. data = filteredData;
  8330. }
  8331. }
  8332. return data;
  8333. } // _updateScales() {
  8334. // const scaleController = this.get('scaleController');
  8335. // scaleController.updateScales();
  8336. // this._adjustScale();
  8337. // }
  8338. ;
  8339. _proto._adjustScale = function _adjustScale() {
  8340. var self = this;
  8341. var scaleController = self.get('scaleController'); // 看起来是为了让柱状图最小或最大都默认从0开始
  8342. var geoms = this.get('geoms');
  8343. for (var i = 0; i < geoms.length; i++) {
  8344. var geom = geoms[i];
  8345. if (geom.get('type') === 'interval') {
  8346. var yScale = geom.getYScale();
  8347. scaleController.adjustStartZero(yScale);
  8348. }
  8349. }
  8350. };
  8351. _proto._removeGeoms = function _removeGeoms() {
  8352. var geoms = this.get('geoms');
  8353. while (geoms.length > 0) {
  8354. var geom = geoms.shift();
  8355. geom.destroy();
  8356. }
  8357. };
  8358. _proto._clearGeoms = function _clearGeoms() {
  8359. var geoms = this.get('geoms');
  8360. for (var i = 0, length = geoms.length; i < length; i++) {
  8361. var geom = geoms[i];
  8362. geom.clear();
  8363. }
  8364. };
  8365. _proto._clearInner = function _clearInner() {
  8366. this._clearGeoms();
  8367. Chart.plugins.notify(this, 'clearInner');
  8368. this.emit(EVENT_CLEAR_INNER);
  8369. this.get('axisController') && this.get('axisController').clear();
  8370. };
  8371. _proto._initFilteredData = function _initFilteredData() {
  8372. var filters = this.get('filters');
  8373. var data = this.get('data') || [];
  8374. if (filters) {
  8375. data = data.filter(function (obj) {
  8376. var rst = true;
  8377. each(filters, function (fn, k) {
  8378. if (fn) {
  8379. rst = fn(obj[k], obj);
  8380. if (!rst) {
  8381. return false;
  8382. }
  8383. }
  8384. });
  8385. return rst;
  8386. });
  8387. }
  8388. this.set('filteredData', data);
  8389. };
  8390. _proto._changeGeomsData = function _changeGeomsData() {
  8391. var geoms = this.get('geoms');
  8392. var data = this.get('filteredData');
  8393. for (var i = 0, length = geoms.length; i < length; i++) {
  8394. var geom = geoms[i];
  8395. geom.changeData(data);
  8396. }
  8397. };
  8398. _proto._initGeom = function _initGeom(geom) {
  8399. if (geom.get('isInit')) {
  8400. return;
  8401. }
  8402. var coord = this.get('coord');
  8403. var data = this.get('filteredData');
  8404. var colDefs = this.get('colDefs');
  8405. var middlePlot = this.get('middlePlot');
  8406. geom.set('chart', this);
  8407. geom.set('container', middlePlot.addGroup());
  8408. geom.set('data', data);
  8409. geom.set('coord', coord);
  8410. geom.set('colDefs', colDefs);
  8411. geom.init();
  8412. this.emit(EVENT_AFTER_GEOM_INIT, geom);
  8413. };
  8414. _proto._initGeoms = function _initGeoms() {
  8415. var geoms = this.get('geoms');
  8416. for (var i = 0, length = geoms.length; i < length; i++) {
  8417. this._initGeom(geoms[i]);
  8418. }
  8419. };
  8420. _proto._initCoord = function _initCoord() {
  8421. var plot = this.get('plotRange');
  8422. var coordCfg = mix({
  8423. type: 'cartesian'
  8424. }, this.get('coordCfg'), {
  8425. plot: plot
  8426. });
  8427. var type = coordCfg.type;
  8428. var C = Base$1[upperFirst(type)];
  8429. var coord = new C(coordCfg);
  8430. this.set('coord', coord);
  8431. };
  8432. _proto._initLayout = function _initLayout() {
  8433. var padding = this.get('_padding');
  8434. if (!padding) {
  8435. padding = this.get('margin') || this.get('padding');
  8436. padding = parsePadding(padding);
  8437. }
  8438. var top = padding[0] === 'auto' ? 0 : padding[0];
  8439. var right = padding[1] === 'auto' ? 0 : padding[1];
  8440. var bottom = padding[2] === 'auto' ? 0 : padding[2];
  8441. var left = padding[3] === 'auto' ? 0 : padding[3];
  8442. var width = this.get('width');
  8443. var height = this.get('height');
  8444. var start = {
  8445. x: left,
  8446. y: top
  8447. };
  8448. var end = {
  8449. x: width - right,
  8450. y: height - bottom
  8451. };
  8452. var plot = this.get('plot');
  8453. if (plot) {
  8454. plot.reset(start, end);
  8455. return;
  8456. }
  8457. var newPlot = new Plot({
  8458. start: start,
  8459. end: end
  8460. });
  8461. this.set('plotRange', newPlot);
  8462. this.set('plot', newPlot);
  8463. };
  8464. _proto._initCanvas = function _initCanvas() {
  8465. var self = this;
  8466. try {
  8467. var canvas = createCanvas({
  8468. renderer: self.get('renderer'),
  8469. el: self.get('el') || self.get('id'),
  8470. context: self.get('context'),
  8471. pixelRatio: self.get('pixelRatio'),
  8472. width: self.get('width'),
  8473. height: self.get('height'),
  8474. fontFamily: Global.fontFamily,
  8475. aria: self.get('aria'),
  8476. title: self.get('title'),
  8477. landscape: self.get('landscape')
  8478. });
  8479. self.set('canvas', canvas);
  8480. self.set('el', canvas.get('el'));
  8481. self.set('width', canvas.get('width'));
  8482. self.set('height', canvas.get('height'));
  8483. } catch (error) {
  8484. throw error;
  8485. }
  8486. Chart.plugins.notify(self, 'afterCanvasInit');
  8487. };
  8488. _proto._initLayers = function _initLayers() {
  8489. var canvas = this.get('canvas');
  8490. this.set('backPlot', canvas.addGroup());
  8491. this.set('middlePlot', canvas.addGroup({
  8492. zIndex: 10
  8493. }));
  8494. this.set('frontPlot', canvas.addGroup({
  8495. zIndex: 20
  8496. }));
  8497. };
  8498. _proto._initEvents = function _initEvents() {
  8499. var _this2 = this;
  8500. // 数据更新后的一些更新
  8501. this.on(EVENT_AFTER_DATA_CHANGE, function () {
  8502. // 数据更新后,重新设置filterdata
  8503. _this2._initFilteredData(); // 更新geoms里的数据
  8504. _this2._changeGeomsData();
  8505. }); // 大小变化后的一些更新
  8506. this.on(EVENT_AFTER_SIZE_CHANGE, function () {
  8507. _this2._initLayout(); // layout变化后,坐标轴也需要做相应的变化
  8508. var coord = _this2.get('coord');
  8509. if (coord) {
  8510. coord.reset(_this2.get('plot'));
  8511. }
  8512. });
  8513. };
  8514. _proto._initScaleController = function _initScaleController() {
  8515. var scaleController = new ScaleController({
  8516. chart: this
  8517. }); // 让colDefs 和 scaleController.defs 用同一个对象,这样就不用考虑同步的问题
  8518. this.set('colDefs', scaleController.defs); // 已经实例化的scales 也保持统一个对象
  8519. this.set('scales', scaleController.scales);
  8520. this.set('scaleController', scaleController);
  8521. };
  8522. _proto._clearScaleController = function _clearScaleController() {
  8523. var scaleController = this.get('scaleController');
  8524. scaleController.clear();
  8525. };
  8526. _proto._init = function _init() {
  8527. var self = this;
  8528. self._initCanvas();
  8529. self._initLayout();
  8530. self._initLayers();
  8531. self._initEvents();
  8532. self._initScaleController();
  8533. self.set('axisController', new AxisController({
  8534. frontPlot: self.get('frontPlot').addGroup({
  8535. className: 'axisContainer'
  8536. }),
  8537. backPlot: self.get('backPlot').addGroup({
  8538. className: 'axisContainer'
  8539. }),
  8540. chart: self
  8541. }));
  8542. Chart.plugins.notify(self, 'init');
  8543. };
  8544. function Chart(cfg) {
  8545. var _this;
  8546. _this = _Base.call(this, cfg) || this;
  8547. var self = _assertThisInitialized(_this);
  8548. each(Geom, function (geomConstructor, className) {
  8549. var methodName = lowerFirst(className);
  8550. self[methodName] = function (cfg) {
  8551. var geom = new geomConstructor(cfg);
  8552. self.addGeom(geom);
  8553. return geom;
  8554. };
  8555. });
  8556. self._init();
  8557. return _this;
  8558. }
  8559. _proto.init = function init() {
  8560. // 初始filterData
  8561. this._initFilteredData(); // initialization coordinate instance
  8562. this._initCoord();
  8563. Chart.plugins.notify(this, 'beforeGeomInit'); // init all geometry instances
  8564. this._initGeoms(); // 多 Y 轴的情况时,统一 Y 轴的数值范围。
  8565. this._syncYScales(); // do some adjust for data
  8566. this._adjustScale();
  8567. this.emit(EVENT_AFTER_INIT);
  8568. }
  8569. /**
  8570. * set data and some scale configuration
  8571. * @chainable
  8572. * @param {Array} data the dataset to visualize
  8573. * @param {Object} colDefs the configuration for scales
  8574. * @return {Chart} return the chart instance
  8575. */
  8576. ;
  8577. _proto.source = function source(data, colDefs) {
  8578. this.set('data', data);
  8579. if (colDefs) {
  8580. this.scale(colDefs);
  8581. }
  8582. return this;
  8583. };
  8584. _proto.scale = function scale(field, cfg) {
  8585. var scaleController = this.get('scaleController');
  8586. scaleController.setFieldDef(field, cfg);
  8587. return this;
  8588. }
  8589. /**
  8590. * configure the axis
  8591. * @chainable
  8592. * @param {String|Boolean} field the field name of data
  8593. * @param {Object} cfg configuration for axis
  8594. * @return {Chart} return the chart instance
  8595. */
  8596. ;
  8597. _proto.axis = function axis(field, cfg) {
  8598. var axisController = this.get('axisController');
  8599. if (!field) {
  8600. axisController.axisCfg = null;
  8601. } else {
  8602. axisController.axisCfg = axisController.axisCfg || {};
  8603. axisController.axisCfg[field] = cfg;
  8604. }
  8605. return this;
  8606. }
  8607. /**
  8608. * configure the coordinate
  8609. * @chainable
  8610. * @param {String} type set the type of coodinate
  8611. * @param {Object} cfg configuration for coordinate
  8612. * @return {Chart} return the chart instance
  8613. */
  8614. ;
  8615. _proto.coord = function coord(type, cfg) {
  8616. var coordCfg;
  8617. if (isObject(type)) {
  8618. coordCfg = type;
  8619. } else {
  8620. coordCfg = cfg || {};
  8621. coordCfg.type = type || 'cartesian';
  8622. }
  8623. this.set('coordCfg', coordCfg);
  8624. return this;
  8625. };
  8626. _proto.filter = function filter(field, condition) {
  8627. var filters = this.get('filters') || {};
  8628. filters[field] = condition;
  8629. this.set('filters', filters); // 如果已经render过,则再重新触发一次change
  8630. if (this.get('rendered')) {
  8631. this.emit(EVENT_AFTER_DATA_CHANGE, this.get('data'));
  8632. }
  8633. }
  8634. /**
  8635. * render the chart
  8636. * @chainable
  8637. * @return {Chart} return the chart instance
  8638. */
  8639. ;
  8640. _proto.render = function render() {
  8641. var rendered = this.get('rendered');
  8642. var canvas = this.get('canvas');
  8643. var geoms = this.get('geoms'); // 已经渲染过
  8644. if (rendered) {
  8645. this._initGeoms();
  8646. this._adjustScale();
  8647. } else {
  8648. this.init();
  8649. this.set('rendered', true);
  8650. }
  8651. this.emit(EVENT_BEFORE_RENDER);
  8652. Chart.plugins.notify(this, 'beforeGeomDraw');
  8653. this._renderAxis();
  8654. var middlePlot = this.get('middlePlot');
  8655. if (this.get('limitInPlot') && !middlePlot.attr('clip')) {
  8656. var coord = this.get('coord');
  8657. var clip = getClip(coord);
  8658. clip.set('canvas', middlePlot.get('canvas'));
  8659. middlePlot.attr('clip', clip);
  8660. }
  8661. this.emit(EVENT_BEFORE_GEOM_DRAW);
  8662. for (var i = 0, length = geoms.length; i < length; i++) {
  8663. var geom = geoms[i];
  8664. geom.paint();
  8665. }
  8666. this.emit(EVENT_AFTER_GEOM_DRAW);
  8667. Chart.plugins.notify(this, 'afterGeomDraw');
  8668. canvas.sort();
  8669. this.get('frontPlot').sort();
  8670. Chart.plugins.notify(this, 'beforeCanvasDraw');
  8671. canvas.draw();
  8672. this.emit(EVENT_AFTER_RENDER);
  8673. return this;
  8674. }
  8675. /**
  8676. * clear the chart, include geometris and all the shapes
  8677. * @chainable
  8678. * @return {Chart} return the chart
  8679. */
  8680. ;
  8681. _proto.clear = function clear() {
  8682. Chart.plugins.notify(this, 'clear');
  8683. this.emit(EVENT_CLEAR);
  8684. this._clearInner();
  8685. this._removeGeoms();
  8686. this._clearScaleController();
  8687. this.set('legendItems', null);
  8688. this.set('filters', null);
  8689. this.set('isUpdate', false);
  8690. this.set('_padding', null);
  8691. this.set('rendered', false);
  8692. var canvas = this.get('canvas');
  8693. canvas.draw();
  8694. return this;
  8695. };
  8696. _proto.repaint = function repaint() {
  8697. // 如果在没有render之前就repaint的,就直接return退出
  8698. var rendered = this.get('rendered');
  8699. if (!rendered) {
  8700. return;
  8701. }
  8702. this.set('isUpdate', true);
  8703. this.set('legendItems', null);
  8704. Chart.plugins.notify(this, 'repaint');
  8705. this._clearInner();
  8706. this.emit(EVENT_REPAINT);
  8707. this.render();
  8708. };
  8709. _proto.changeData = function changeData(data) {
  8710. this.emit(EVENT_BEFORE_DATA_CHANGE, data);
  8711. this.set('data', data);
  8712. Chart.plugins.notify(this, 'changeData');
  8713. this.emit(EVENT_AFTER_DATA_CHANGE, data);
  8714. this.set('_padding', null);
  8715. this.repaint();
  8716. };
  8717. _proto.changeSize = function changeSize(width, height) {
  8718. if (width) {
  8719. this.set('width', width);
  8720. } else {
  8721. width = this.get('width');
  8722. }
  8723. if (height) {
  8724. this.set('height', height);
  8725. } else {
  8726. height = this.get('height');
  8727. }
  8728. var canvas = this.get('canvas');
  8729. canvas.changeSize(width, height);
  8730. this.emit(EVENT_AFTER_SIZE_CHANGE, {
  8731. width: width,
  8732. height: height
  8733. });
  8734. this.repaint();
  8735. return this;
  8736. };
  8737. _proto.destroy = function destroy() {
  8738. this.clear();
  8739. var canvas = this.get('canvas');
  8740. canvas.destroy();
  8741. Chart.plugins.notify(this, 'afterCanvasDestroyed');
  8742. if (this._interactions) {
  8743. each(this._interactions, function (interaction) {
  8744. interaction.destroy();
  8745. });
  8746. }
  8747. _Base.prototype.destroy.call(this);
  8748. }
  8749. /**
  8750. * calculate dataset's position on canvas
  8751. * @param {Object} record the dataset
  8752. * @return {Object} return the position
  8753. */
  8754. ;
  8755. _proto.getPosition = function getPosition(record) {
  8756. var self = this;
  8757. var coord = self.get('coord');
  8758. var xScale = self.getXScale();
  8759. var xField = xScale.field;
  8760. var yScales = self.getYScales(); // default first
  8761. var yScale = yScales[0];
  8762. var yField = yScale.field;
  8763. for (var i = 0, len = yScales.length; i < len; i++) {
  8764. var scale = yScales[i];
  8765. var field = scale.field;
  8766. if (record[field]) {
  8767. yScale = scale;
  8768. yField = field;
  8769. break;
  8770. }
  8771. }
  8772. var x = xScale.scale(record[xField]);
  8773. var y = yScale.scale(record[yField]);
  8774. return coord.convertPoint({
  8775. x: x,
  8776. y: y
  8777. });
  8778. }
  8779. /**
  8780. * get the data item of the point
  8781. * @param {Object} point canvas position
  8782. * @return {Object} return the data item
  8783. */
  8784. ;
  8785. _proto.getRecord = function getRecord(point) {
  8786. var self = this;
  8787. var coord = self.get('coord');
  8788. var xScale = self.getXScale();
  8789. var yScale = self.getYScales()[0];
  8790. var invertPoint = coord.invertPoint(point);
  8791. var record = {};
  8792. record[xScale.field] = xScale.invert(invertPoint.x);
  8793. record[yScale.field] = yScale.invert(invertPoint.y);
  8794. return record;
  8795. }
  8796. /**
  8797. * get the dataset of the point
  8798. * @param {Object} point canvas position
  8799. * @return {Array} return the dataset
  8800. **/
  8801. ;
  8802. _proto.getSnapRecords = function getSnapRecords(point) {
  8803. var geom = this.get('geoms')[0];
  8804. var data = [];
  8805. if (geom) {
  8806. // need to judge
  8807. data = geom.getSnapRecords(point);
  8808. }
  8809. return data;
  8810. }
  8811. /**
  8812. * creat scale instances
  8813. * @param {String} field field name of data
  8814. * @return {Scale} return the scale
  8815. */
  8816. ;
  8817. _proto.createScale = function createScale(field) {
  8818. var data = this._getScaleData(field);
  8819. var scaleController = this.get('scaleController');
  8820. return scaleController.createScale(field, data);
  8821. }
  8822. /**
  8823. * @protected
  8824. * add geometry instance to geoms
  8825. * @param {Geom} geom geometry instance
  8826. */
  8827. ;
  8828. _proto.addGeom = function addGeom(geom) {
  8829. var geoms = this.get('geoms');
  8830. geoms.push(geom);
  8831. }
  8832. /**
  8833. * get the scale of x axis
  8834. * @return {Scale} return the scale
  8835. */
  8836. ;
  8837. _proto.getXScale = function getXScale() {
  8838. var self = this;
  8839. var geoms = self.get('geoms');
  8840. var xScale = geoms[0].getXScale();
  8841. return xScale;
  8842. }
  8843. /**
  8844. * get the scale of y axis
  8845. * @return {Array} return the scale
  8846. */
  8847. ;
  8848. _proto.getYScales = function getYScales() {
  8849. var geoms = this.get('geoms');
  8850. var rst = [];
  8851. each(geoms, function (geom) {
  8852. var yScale = geom.getYScale();
  8853. if (rst.indexOf(yScale) === -1) {
  8854. rst.push(yScale);
  8855. }
  8856. });
  8857. return rst;
  8858. };
  8859. _proto.getLegendItems = function getLegendItems() {
  8860. if (this.get('legendItems')) {
  8861. return this.get('legendItems');
  8862. }
  8863. var legendItems = {};
  8864. var scales = [];
  8865. var geoms = this.get('geoms');
  8866. each(geoms, function (geom) {
  8867. var colorAttr = geom.getAttr('color');
  8868. if (colorAttr) {
  8869. var scale = colorAttr.getScale('color'); // 只支持分类图例
  8870. if (scale.isCategory && !_isScaleExist(scales, scale)) {
  8871. scales.push(scale);
  8872. var field = scale.field;
  8873. var ticks = scale.getTicks();
  8874. var items = [];
  8875. each(ticks, function (tick) {
  8876. var text = tick.text;
  8877. var name = text;
  8878. var scaleValue = tick.value;
  8879. var value = scale.invert(scaleValue);
  8880. var color = colorAttr.mapping(value).join('') || Global.defaultColor;
  8881. var marker = {
  8882. fill: color,
  8883. radius: 3,
  8884. symbol: 'circle',
  8885. stroke: '#fff'
  8886. };
  8887. items.push({
  8888. name: name,
  8889. // for display
  8890. dataValue: value,
  8891. // the origin value
  8892. checked: true,
  8893. marker: marker
  8894. });
  8895. });
  8896. legendItems[field] = items;
  8897. }
  8898. }
  8899. });
  8900. this.set('legendItems', legendItems);
  8901. return legendItems;
  8902. } // register the plugins
  8903. ;
  8904. _proto.registerPlugins = function registerPlugins(plugins) {
  8905. var self = this;
  8906. var chartPlugins = self.get('plugins') || [];
  8907. if (!isArray(chartPlugins)) {
  8908. chartPlugins = [chartPlugins];
  8909. }
  8910. [].concat(plugins).forEach(function (plugin) {
  8911. if (chartPlugins.indexOf(plugin) === -1) {
  8912. plugin.init && plugin.init(self); // init
  8913. chartPlugins.push(plugin);
  8914. }
  8915. });
  8916. Chart.plugins._cacheId++;
  8917. self.set('plugins', chartPlugins);
  8918. };
  8919. _proto._renderAxis = function _renderAxis() {
  8920. var axisController = this.get('axisController');
  8921. var xScale = this.getXScale();
  8922. var yScales = this.getYScales();
  8923. var coord = this.get('coord');
  8924. Chart.plugins.notify(this, 'beforeRenderAxis');
  8925. axisController.createAxis(coord, xScale, yScales);
  8926. };
  8927. _proto._isAutoPadding = function _isAutoPadding() {
  8928. if (this.get('_padding')) {
  8929. return false;
  8930. }
  8931. var padding = this.get('padding');
  8932. if (isArray(padding)) {
  8933. return padding.indexOf('auto') !== -1;
  8934. }
  8935. return padding === 'auto';
  8936. };
  8937. _proto._updateLayout = function _updateLayout(padding) {
  8938. var width = this.get('width');
  8939. var height = this.get('height');
  8940. var start = {
  8941. x: padding[3],
  8942. y: padding[0]
  8943. };
  8944. var end = {
  8945. x: width - padding[1],
  8946. y: height - padding[2]
  8947. };
  8948. var plot = this.get('plot');
  8949. var coord = this.get('coord');
  8950. plot.reset(start, end);
  8951. coord.reset(plot);
  8952. }
  8953. /**
  8954. * 是否为横屏展示
  8955. *
  8956. * @param {Boolean} landscape 是否为横屏
  8957. */
  8958. ;
  8959. _proto.landscape = function landscape(_landscape) {
  8960. var canvas = this.get('canvas');
  8961. canvas.set('landscape', _landscape);
  8962. };
  8963. return Chart;
  8964. }(Base);
  8965. Chart.plugins = Chart.initPlugins();
  8966. var track = function track() {
  8967. return null;
  8968. };
  8969. /**
  8970. * @fileOverview shape util
  8971. * @author dxq613@gmail.com
  8972. */
  8973. function splitPoints(obj) {
  8974. var points = [];
  8975. var x = obj.x;
  8976. var y = obj.y;
  8977. y = isArray(y) ? y : [y];
  8978. y.forEach(function (yItem, index) {
  8979. var point = {
  8980. x: isArray(x) ? x[index] : x,
  8981. y: yItem
  8982. };
  8983. points.push(point);
  8984. });
  8985. return points;
  8986. }
  8987. function splitArray(data, yField, connectNulls) {
  8988. if (!data.length) return [];
  8989. var arr = [];
  8990. var tmp = [];
  8991. var yValue;
  8992. each(data, function (obj) {
  8993. yValue = obj._origin ? obj._origin[yField] : obj[yField];
  8994. if (connectNulls) {
  8995. if (!isNil(yValue)) {
  8996. tmp.push(obj);
  8997. }
  8998. } else {
  8999. if (isArray(yValue) && isNil(yValue[0]) || isNil(yValue)) {
  9000. if (tmp.length) {
  9001. arr.push(tmp);
  9002. tmp = [];
  9003. }
  9004. } else {
  9005. tmp.push(obj);
  9006. }
  9007. }
  9008. });
  9009. if (tmp.length) {
  9010. arr.push(tmp);
  9011. }
  9012. return arr;
  9013. }
  9014. var SHAPES = ['circle', 'hollowCircle', 'rect'];
  9015. var Point = Shape$1.registerFactory('point', {
  9016. defaultShapeType: 'circle',
  9017. getDefaultPoints: function getDefaultPoints(pointInfo) {
  9018. return splitPoints(pointInfo);
  9019. }
  9020. });
  9021. function getPointsCfg(cfg) {
  9022. var style = {
  9023. lineWidth: 0,
  9024. stroke: cfg.color,
  9025. fill: cfg.color
  9026. };
  9027. if (cfg.size) {
  9028. style.size = cfg.size;
  9029. }
  9030. mix(style, cfg.style);
  9031. return mix({}, Global.shape.point, style);
  9032. }
  9033. function drawShape(cfg, container, shape) {
  9034. if (cfg.size === 0) return;
  9035. var pointCfg = getPointsCfg(cfg);
  9036. var size = pointCfg.r || pointCfg.size;
  9037. var x = cfg.x;
  9038. var y = !isArray(cfg.y) ? [cfg.y] : cfg.y;
  9039. if (shape === 'hollowCircle') {
  9040. pointCfg.lineWidth = 1;
  9041. pointCfg.fill = null;
  9042. }
  9043. for (var i = 0, len = y.length; i < len; i++) {
  9044. if (shape === 'rect') {
  9045. return container.addShape('Rect', {
  9046. className: 'point',
  9047. attrs: mix({
  9048. x: x - size,
  9049. y: y[i] - size,
  9050. width: size * 2,
  9051. height: size * 2
  9052. }, pointCfg)
  9053. });
  9054. }
  9055. return container.addShape('Circle', {
  9056. className: 'point',
  9057. attrs: mix({
  9058. x: x,
  9059. y: y[i],
  9060. r: size
  9061. }, pointCfg)
  9062. });
  9063. }
  9064. }
  9065. each(SHAPES, function (shapeType) {
  9066. Shape$1.registerShape('point', shapeType, {
  9067. draw: function draw(cfg, container) {
  9068. return drawShape(cfg, container, shapeType);
  9069. }
  9070. });
  9071. });
  9072. var Point$1 = /*#__PURE__*/function (_Geom) {
  9073. _inheritsLoose(Point, _Geom);
  9074. function Point() {
  9075. return _Geom.apply(this, arguments) || this;
  9076. }
  9077. var _proto = Point.prototype;
  9078. _proto.getDefaultCfg = function getDefaultCfg() {
  9079. var cfg = _Geom.prototype.getDefaultCfg.call(this);
  9080. cfg.type = 'point';
  9081. cfg.shapeType = 'point';
  9082. cfg.generatePoints = false;
  9083. return cfg;
  9084. };
  9085. _proto.draw = function draw(data, shapeFactory) {
  9086. var self = this;
  9087. var container = self.get('container');
  9088. each(data, function (obj) {
  9089. var shape = obj.shape;
  9090. var cfg = self.getDrawCfg(obj);
  9091. if (isArray(obj.y)) {
  9092. var hasStack = self.hasAdjust('stack');
  9093. each(obj.y, function (y, idx) {
  9094. cfg.y = y;
  9095. if (!hasStack || idx !== 0) {
  9096. self.drawShape(shape, obj, cfg, container, shapeFactory);
  9097. }
  9098. });
  9099. } else if (!isNil(obj.y)) {
  9100. self.drawShape(shape, obj, cfg, container, shapeFactory);
  9101. }
  9102. });
  9103. };
  9104. return Point;
  9105. }(Geom);
  9106. Geom.Point = Point$1;
  9107. var Line$2 = Shape$1.registerFactory('line', {
  9108. defaultShapeType: 'line'
  9109. });
  9110. function getStyle$1(cfg) {
  9111. var style = {
  9112. strokeStyle: cfg.color
  9113. };
  9114. if (cfg.size >= 0) {
  9115. style.lineWidth = cfg.size;
  9116. }
  9117. mix(style, cfg.style);
  9118. return mix({}, Global.shape.line, style);
  9119. }
  9120. function drawLines(cfg, container, style, smooth) {
  9121. var points = cfg.points;
  9122. if (points.length && isArray(points[0].y)) {
  9123. var topPoints = [];
  9124. var bottomPoints = [];
  9125. for (var i = 0, len = points.length; i < len; i++) {
  9126. var point = points[i];
  9127. var tmp = splitPoints(point);
  9128. bottomPoints.push(tmp[0]);
  9129. topPoints.push(tmp[1]);
  9130. }
  9131. if (cfg.isInCircle) {
  9132. topPoints.push(topPoints[0]);
  9133. bottomPoints.push(bottomPoints[0]);
  9134. }
  9135. if (cfg.isStack) {
  9136. return container.addShape('Polyline', {
  9137. className: 'line',
  9138. attrs: mix({
  9139. points: topPoints,
  9140. smooth: smooth
  9141. }, style)
  9142. });
  9143. }
  9144. var topShape = container.addShape('Polyline', {
  9145. className: 'line',
  9146. attrs: mix({
  9147. points: topPoints,
  9148. smooth: smooth
  9149. }, style)
  9150. });
  9151. var bottomShape = container.addShape('Polyline', {
  9152. className: 'line',
  9153. attrs: mix({
  9154. points: bottomPoints,
  9155. smooth: smooth
  9156. }, style)
  9157. });
  9158. return [topShape, bottomShape];
  9159. }
  9160. if (cfg.isInCircle) {
  9161. points.push(points[0]);
  9162. }
  9163. return container.addShape('Polyline', {
  9164. className: 'line',
  9165. attrs: mix({
  9166. points: points,
  9167. smooth: smooth
  9168. }, style)
  9169. });
  9170. }
  9171. var SHAPES$1 = ['line', 'smooth', 'dash'];
  9172. each(SHAPES$1, function (shapeType) {
  9173. Shape$1.registerShape('line', shapeType, {
  9174. draw: function draw(cfg, container) {
  9175. var smooth = shapeType === 'smooth';
  9176. var style = getStyle$1(cfg);
  9177. if (shapeType === 'dash') {
  9178. style.lineDash = Global.lineDash;
  9179. }
  9180. return drawLines(cfg, container, style, smooth);
  9181. }
  9182. });
  9183. });
  9184. var Path = /*#__PURE__*/function (_Geom) {
  9185. _inheritsLoose(Path, _Geom);
  9186. function Path() {
  9187. return _Geom.apply(this, arguments) || this;
  9188. }
  9189. var _proto = Path.prototype;
  9190. _proto.getDefaultCfg = function getDefaultCfg() {
  9191. var cfg = _Geom.prototype.getDefaultCfg.call(this);
  9192. cfg.type = 'path';
  9193. cfg.shapeType = 'line';
  9194. return cfg;
  9195. };
  9196. _proto.getDrawCfg = function getDrawCfg(obj) {
  9197. var cfg = _Geom.prototype.getDrawCfg.call(this, obj);
  9198. cfg.isStack = this.hasAdjust('stack');
  9199. return cfg;
  9200. };
  9201. _proto.draw = function draw(data, shapeFactory) {
  9202. var self = this;
  9203. var container = self.get('container');
  9204. var yScale = self.getYScale();
  9205. var connectNulls = self.get('connectNulls');
  9206. var splitArrayObj = splitArray(data, yScale.field, connectNulls);
  9207. var cfg = this.getDrawCfg(data[0]);
  9208. cfg.origin = data;
  9209. each(splitArrayObj, function (subData, splitedIndex) {
  9210. cfg.splitedIndex = splitedIndex;
  9211. cfg.points = subData;
  9212. self.drawShape(cfg.shape, data[0], cfg, container, shapeFactory);
  9213. });
  9214. };
  9215. return Path;
  9216. }(Geom);
  9217. Geom.Path = Path;
  9218. var Line$3 = /*#__PURE__*/function (_Path) {
  9219. _inheritsLoose(Line, _Path);
  9220. function Line() {
  9221. return _Path.apply(this, arguments) || this;
  9222. }
  9223. var _proto = Line.prototype;
  9224. _proto.getDefaultCfg = function getDefaultCfg() {
  9225. var cfg = _Path.prototype.getDefaultCfg.call(this);
  9226. cfg.type = 'line';
  9227. cfg.sortable = true;
  9228. return cfg;
  9229. };
  9230. return Line;
  9231. }(Path);
  9232. Geom.Line = Line$3;
  9233. function equals(v1, v2) {
  9234. return Math.abs(v1 - v2) < 0.00001;
  9235. }
  9236. function notEmpty(value) {
  9237. return !isNaN(value) && !isNil(value);
  9238. }
  9239. function filterPoints(points) {
  9240. var filteredPoints = []; // filter the point which x or y is NaN
  9241. for (var i = 0, len = points.length; i < len; i++) {
  9242. var point = points[i];
  9243. if (notEmpty(point.x) && notEmpty(point.y)) {
  9244. filteredPoints.push(point);
  9245. }
  9246. }
  9247. return filteredPoints;
  9248. }
  9249. function equalsCenter(points, center) {
  9250. var eqls = true;
  9251. each(points, function (point) {
  9252. if (!equals(point.x, center.x) || !equals(point.y, center.y)) {
  9253. eqls = false;
  9254. return false;
  9255. }
  9256. });
  9257. return eqls;
  9258. }
  9259. function drawRectShape(topPoints, bottomPoints, container, style, isSmooth) {
  9260. var shape;
  9261. var points = topPoints.concat(bottomPoints);
  9262. if (isSmooth) {
  9263. shape = container.addShape('Custom', {
  9264. className: 'area',
  9265. attrs: mix({
  9266. points: points
  9267. }, style),
  9268. createPath: function createPath(context) {
  9269. var constaint = [[0, 0], [1, 1]];
  9270. var points = filterPoints(this._attrs.attrs.points);
  9271. var pointsLen = points.length;
  9272. var topPoints = points.slice(0, pointsLen / 2);
  9273. var bottomPoints = points.slice(pointsLen / 2, pointsLen);
  9274. var topSps = catmullRom2bezier(topPoints, false, constaint);
  9275. context.beginPath();
  9276. context.moveTo(topPoints[0].x, topPoints[0].y);
  9277. for (var i = 0, n = topSps.length; i < n; i++) {
  9278. var sp = topSps[i];
  9279. context.bezierCurveTo(sp[1], sp[2], sp[3], sp[4], sp[5], sp[6]);
  9280. }
  9281. if (bottomPoints.length) {
  9282. var bottomSps = catmullRom2bezier(bottomPoints, false, constaint);
  9283. context.lineTo(bottomPoints[0].x, bottomPoints[0].y);
  9284. for (var _i = 0, _n = bottomSps.length; _i < _n; _i++) {
  9285. var _sp = bottomSps[_i];
  9286. context.bezierCurveTo(_sp[1], _sp[2], _sp[3], _sp[4], _sp[5], _sp[6]);
  9287. }
  9288. }
  9289. context.closePath();
  9290. },
  9291. calculateBox: function calculateBox() {
  9292. var points = filterPoints(this._attrs.attrs.points);
  9293. return getBBoxFromPoints(points);
  9294. }
  9295. });
  9296. } else {
  9297. shape = container.addShape('Polyline', {
  9298. className: 'area',
  9299. attrs: mix({
  9300. points: points
  9301. }, style)
  9302. });
  9303. }
  9304. return shape;
  9305. }
  9306. function drawShape$1(cfg, container, isSmooth) {
  9307. var self = this;
  9308. var points = cfg.points;
  9309. var topPoints = [];
  9310. var bottomPoints = [];
  9311. each(points, function (point) {
  9312. bottomPoints.push(point[0]);
  9313. topPoints.push(point[1]);
  9314. });
  9315. var style = mix({
  9316. fillStyle: cfg.color
  9317. }, Global.shape.area, cfg.style);
  9318. bottomPoints.reverse();
  9319. topPoints = self.parsePoints(topPoints);
  9320. bottomPoints = self.parsePoints(bottomPoints);
  9321. if (cfg.isInCircle) {
  9322. topPoints.push(topPoints[0]);
  9323. bottomPoints.unshift(bottomPoints[bottomPoints.length - 1]);
  9324. if (equalsCenter(bottomPoints, cfg.center)) {
  9325. bottomPoints = [];
  9326. }
  9327. }
  9328. return drawRectShape(topPoints, bottomPoints, container, style, isSmooth);
  9329. }
  9330. var Area = Shape$1.registerFactory('area', {
  9331. defaultShapeType: 'area',
  9332. getDefaultPoints: function getDefaultPoints(obj) {
  9333. var x = obj.x;
  9334. var y = obj.y;
  9335. var y0 = obj.y0;
  9336. y = isArray(y) ? y : [y0, y];
  9337. var points = [];
  9338. points.push({
  9339. x: x,
  9340. y: y[0]
  9341. }, {
  9342. x: x,
  9343. y: y[1]
  9344. });
  9345. return points;
  9346. }
  9347. });
  9348. var SHAPES$2 = ['area', 'smooth'];
  9349. each(SHAPES$2, function (shapeType) {
  9350. Shape$1.registerShape('area', shapeType, {
  9351. draw: function draw(cfg, container) {
  9352. var smooth = shapeType === 'smooth';
  9353. return drawShape$1.call(this, cfg, container, smooth);
  9354. }
  9355. });
  9356. });
  9357. var Area$1 = /*#__PURE__*/function (_Geom) {
  9358. _inheritsLoose(Area, _Geom);
  9359. function Area() {
  9360. return _Geom.apply(this, arguments) || this;
  9361. }
  9362. var _proto = Area.prototype;
  9363. /**
  9364. * get the default configuration
  9365. * @protected
  9366. * @return {Object} return the result
  9367. */
  9368. _proto.getDefaultCfg = function getDefaultCfg() {
  9369. var cfg = _Geom.prototype.getDefaultCfg.call(this);
  9370. cfg.type = 'area';
  9371. cfg.shapeType = 'area';
  9372. cfg.generatePoints = true;
  9373. cfg.sortable = true;
  9374. return cfg;
  9375. };
  9376. _proto.draw = function draw(data, shapeFactory) {
  9377. var self = this;
  9378. var container = self.get('container');
  9379. var cfg = this.getDrawCfg(data[0]);
  9380. var yScale = self.getYScale();
  9381. var connectNulls = self.get('connectNulls');
  9382. var splitArrayfn = splitArray(data, yScale.field, connectNulls);
  9383. cfg.origin = data;
  9384. each(splitArrayfn, function (subData, splitedIndex) {
  9385. cfg.splitedIndex = splitedIndex;
  9386. var points = subData.map(function (obj) {
  9387. return obj.points;
  9388. });
  9389. cfg.points = points;
  9390. self.drawShape(cfg.shape, data[0], cfg, container, shapeFactory);
  9391. });
  9392. };
  9393. return Area;
  9394. }(Geom);
  9395. Geom.Area = Area$1;
  9396. /**
  9397. * @fileOverview Utility for calculate the with ratui in x axis
  9398. * @author sima.zhang1990@gmail.com
  9399. * @author dxq613@gmail.com
  9400. */
  9401. var SizeMixin = {
  9402. initEvent: function initEvent() {
  9403. var _this = this;
  9404. var chart = this.get('chart');
  9405. if (!chart) {
  9406. return;
  9407. }
  9408. chart.on(EVENT_AFTER_SIZE_CHANGE, function () {
  9409. _this.set('_width', null);
  9410. });
  9411. },
  9412. getDefaultSize: function getDefaultSize() {
  9413. var defaultSize = this.get('defaultSize');
  9414. if (!defaultSize) {
  9415. var coord = this.get('coord');
  9416. var xScale = this.getXScale();
  9417. var dataArray = this.get('dataArray');
  9418. var values = uniq(xScale.values);
  9419. var count = values.length;
  9420. var range = xScale.range;
  9421. var normalizeSize = 1 / count;
  9422. var widthRatio = 1;
  9423. if (coord && coord.isPolar) {
  9424. if (coord.transposed && count > 1) {
  9425. widthRatio = Global.widthRatio.multiplePie;
  9426. } else {
  9427. widthRatio = Global.widthRatio.rose;
  9428. }
  9429. } else {
  9430. if (xScale.isLinear) {
  9431. normalizeSize *= range[1] - range[0];
  9432. }
  9433. widthRatio = Global.widthRatio.column;
  9434. }
  9435. normalizeSize *= widthRatio;
  9436. if (this.hasAdjust('dodge')) {
  9437. normalizeSize = normalizeSize / dataArray.length;
  9438. }
  9439. defaultSize = normalizeSize;
  9440. this.set('defaultSize', defaultSize);
  9441. }
  9442. return defaultSize;
  9443. },
  9444. getDimWidth: function getDimWidth(dimName) {
  9445. var coord = this.get('coord');
  9446. var start = coord.convertPoint({
  9447. x: 0,
  9448. y: 0
  9449. });
  9450. var end = coord.convertPoint({
  9451. x: dimName === 'x' ? 1 : 0,
  9452. y: dimName === 'x' ? 0 : 1
  9453. });
  9454. var width = 0;
  9455. if (start && end) {
  9456. width = Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2));
  9457. }
  9458. return width;
  9459. },
  9460. _getWidth: function _getWidth() {
  9461. var width = this.get('_width');
  9462. if (!width) {
  9463. var coord = this.get('coord');
  9464. if (coord && coord.isPolar && !coord.transposed) {
  9465. width = (coord.endAngle - coord.startAngle) * coord.circleRadius;
  9466. } else {
  9467. width = this.getDimWidth('x');
  9468. }
  9469. this.set('_width', width);
  9470. }
  9471. return width;
  9472. },
  9473. _toNormalizedSize: function _toNormalizedSize(size) {
  9474. var width = this._getWidth();
  9475. return size / width;
  9476. },
  9477. _toCoordSize: function _toCoordSize(normalizeSize) {
  9478. var width = this._getWidth();
  9479. return width * normalizeSize;
  9480. },
  9481. getNormalizedSize: function getNormalizedSize(obj) {
  9482. var size = this.getAttrValue('size', obj);
  9483. if (isNil(size)) {
  9484. size = this.getDefaultSize();
  9485. } else {
  9486. size = this._toNormalizedSize(size);
  9487. }
  9488. return size;
  9489. },
  9490. getSize: function getSize(obj) {
  9491. var size = this.getAttrValue('size', obj);
  9492. if (isNil(size)) {
  9493. var normalizeSize = this.getDefaultSize();
  9494. size = this._toCoordSize(normalizeSize);
  9495. }
  9496. return size;
  9497. }
  9498. };
  9499. function getRectPoints(cfg) {
  9500. var x = cfg.x,
  9501. y = cfg.y,
  9502. y0 = cfg.y0,
  9503. size = cfg.size;
  9504. var ymin = y0;
  9505. var ymax = y;
  9506. if (isArray(y)) {
  9507. ymax = y[1];
  9508. ymin = y[0];
  9509. }
  9510. var xmin;
  9511. var xmax;
  9512. if (isArray(x)) {
  9513. xmin = x[0];
  9514. xmax = x[1];
  9515. } else {
  9516. xmin = x - size / 2;
  9517. xmax = x + size / 2;
  9518. }
  9519. return [{
  9520. x: xmin,
  9521. y: ymin
  9522. }, {
  9523. x: xmin,
  9524. y: ymax
  9525. }, {
  9526. x: xmax,
  9527. y: ymax
  9528. }, {
  9529. x: xmax,
  9530. y: ymin
  9531. }];
  9532. }
  9533. function getRectRange(points) {
  9534. var xValues = [];
  9535. var yValues = [];
  9536. for (var i = 0, len = points.length; i < len; i++) {
  9537. var point = points[i];
  9538. xValues.push(point.x);
  9539. yValues.push(point.y);
  9540. }
  9541. var xMin = Math.min.apply(null, xValues);
  9542. var yMin = Math.min.apply(null, yValues);
  9543. var xMax = Math.max.apply(null, xValues);
  9544. var yMax = Math.max.apply(null, yValues);
  9545. return {
  9546. x: xMin,
  9547. y: yMin,
  9548. width: xMax - xMin,
  9549. height: yMax - yMin
  9550. };
  9551. }
  9552. function getMiddlePoint(a, b) {
  9553. var x = (a.x - b.x) / 2 + b.x;
  9554. var y = (a.y - b.y) / 2 + b.y;
  9555. return {
  9556. x: x,
  9557. y: y
  9558. };
  9559. }
  9560. var Interval = Shape$1.registerFactory('interval', {
  9561. defaultShapeType: 'rect',
  9562. getDefaultPoints: function getDefaultPoints(cfg) {
  9563. return getRectPoints(cfg);
  9564. }
  9565. });
  9566. Shape$1.registerShape('interval', 'rect', {
  9567. draw: function draw(cfg, container) {
  9568. var points = this.parsePoints(cfg.points);
  9569. var style = mix({
  9570. fill: cfg.color
  9571. }, Global.shape.interval, cfg.style);
  9572. if (cfg.isInCircle) {
  9573. var newPoints = points.slice(0);
  9574. if (this._coord.transposed) {
  9575. newPoints = [points[0], points[3], points[2], points[1]];
  9576. }
  9577. var _cfg$center = cfg.center,
  9578. x = _cfg$center.x,
  9579. y = _cfg$center.y;
  9580. var v = [1, 0];
  9581. var v0 = [newPoints[0].x - x, newPoints[0].y - y];
  9582. var v1 = [newPoints[1].x - x, newPoints[1].y - y];
  9583. var v2 = [newPoints[2].x - x, newPoints[2].y - y];
  9584. var startAngle = Vector2.angleTo(v, v1);
  9585. var endAngle = Vector2.angleTo(v, v2);
  9586. var r0 = Vector2.length(v0);
  9587. var r = Vector2.length(v1);
  9588. if (startAngle >= 1.5 * Math.PI) {
  9589. startAngle = startAngle - 2 * Math.PI;
  9590. }
  9591. if (endAngle >= 1.5 * Math.PI) {
  9592. endAngle = endAngle - 2 * Math.PI;
  9593. }
  9594. return container.addShape('Sector', {
  9595. className: 'interval',
  9596. attrs: mix({
  9597. x: x,
  9598. y: y,
  9599. r: r,
  9600. r0: r0,
  9601. startAngle: startAngle,
  9602. endAngle: endAngle
  9603. }, style)
  9604. });
  9605. }
  9606. var rectCfg = getRectRange(points);
  9607. return container.addShape('rect', {
  9608. className: 'interval',
  9609. attrs: mix(rectCfg, style)
  9610. });
  9611. }
  9612. }); // 金字塔 和 漏斗图
  9613. ['pyramid', 'funnel'].forEach(function (shapeType) {
  9614. Shape$1.registerShape('interval', shapeType, {
  9615. getPoints: function getPoints(cfg) {
  9616. cfg.size = cfg.size * 2; // 漏斗图的 size 是柱状图的两倍
  9617. return getRectPoints(cfg);
  9618. },
  9619. draw: function draw(cfg, container) {
  9620. var points = this.parsePoints(cfg.points);
  9621. var nextPoints = this.parsePoints(cfg.nextPoints);
  9622. var polygonPoints = null;
  9623. if (nextPoints) {
  9624. polygonPoints = [points[0], points[1], nextPoints[1], nextPoints[0]];
  9625. } else {
  9626. polygonPoints = [points[0], points[1]]; // pyramid 顶部是三角形,所以取中心点就好了,funnel顶部是长方形
  9627. if (shapeType === 'pyramid') {
  9628. polygonPoints.push(getMiddlePoint(points[2], points[3]));
  9629. } else {
  9630. polygonPoints.push(points[2], points[3]);
  9631. }
  9632. }
  9633. var attrs = mix({
  9634. fill: cfg.color,
  9635. points: polygonPoints
  9636. }, Global.shape.interval, cfg.style);
  9637. return container.addShape('polygon', {
  9638. className: 'interval',
  9639. attrs: attrs
  9640. });
  9641. }
  9642. });
  9643. });
  9644. var Interval$1 = /*#__PURE__*/function (_Geom) {
  9645. _inheritsLoose(Interval, _Geom);
  9646. var _proto = Interval.prototype;
  9647. _proto.getDefaultCfg = function getDefaultCfg() {
  9648. var cfg = _Geom.prototype.getDefaultCfg.call(this);
  9649. cfg.type = 'interval';
  9650. cfg.shapeType = 'interval';
  9651. cfg.generatePoints = true;
  9652. return cfg;
  9653. };
  9654. function Interval(cfg) {
  9655. var _this;
  9656. _this = _Geom.call(this, cfg) || this;
  9657. mix(_assertThisInitialized(_this), SizeMixin);
  9658. return _this;
  9659. }
  9660. _proto.init = function init() {
  9661. _Geom.prototype.init.call(this); // 绑定事件
  9662. this.initEvent();
  9663. };
  9664. _proto.createShapePointsCfg = function createShapePointsCfg(obj) {
  9665. var cfg = _Geom.prototype.createShapePointsCfg.call(this, obj);
  9666. cfg.size = this.getNormalizedSize(obj);
  9667. return cfg;
  9668. };
  9669. _proto.clearInner = function clearInner() {
  9670. _Geom.prototype.clearInner.call(this);
  9671. this.set('defaultSize', null);
  9672. };
  9673. return Interval;
  9674. }(Geom);
  9675. Geom.Interval = Interval$1;
  9676. var Polygon$1 = Shape$1.registerFactory('polygon', {
  9677. defaultShapeType: 'polygon',
  9678. getDefaultPoints: function getDefaultPoints(pointInfo) {
  9679. var points = [];
  9680. var x = pointInfo.x,
  9681. y = pointInfo.y;
  9682. for (var i = 0, len = x.length; i < len; i++) {
  9683. points.push({
  9684. x: x[i],
  9685. y: y[i]
  9686. });
  9687. }
  9688. return points;
  9689. }
  9690. });
  9691. Shape$1.registerShape('polygon', 'polygon', {
  9692. draw: function draw(cfg, container) {
  9693. var points = this.parsePoints(cfg.points);
  9694. var style = mix({
  9695. fill: cfg.color,
  9696. points: points
  9697. }, cfg.style);
  9698. return container.addShape('Polygon', {
  9699. className: 'polygon',
  9700. attrs: style
  9701. });
  9702. }
  9703. });
  9704. var Polygon$2 = /*#__PURE__*/function (_Geom) {
  9705. _inheritsLoose(Polygon, _Geom);
  9706. function Polygon() {
  9707. return _Geom.apply(this, arguments) || this;
  9708. }
  9709. var _proto = Polygon.prototype;
  9710. _proto.getDefaultCfg = function getDefaultCfg() {
  9711. var cfg = _Geom.prototype.getDefaultCfg.call(this);
  9712. cfg.type = 'polygon';
  9713. cfg.shapeType = 'polygon';
  9714. cfg.generatePoints = true;
  9715. return cfg;
  9716. };
  9717. _proto.createShapePointsCfg = function createShapePointsCfg(obj) {
  9718. var cfg = _Geom.prototype.createShapePointsCfg.call(this, obj);
  9719. var self = this;
  9720. var x = cfg.x;
  9721. var y = cfg.y;
  9722. var temp;
  9723. if (!(isArray(x) && isArray(y))) {
  9724. var xScale = self.getXScale();
  9725. var yScale = self.getYScale();
  9726. var xCount = xScale.values ? xScale.values.length : xScale.ticks.length;
  9727. var yCount = yScale.values ? yScale.values.length : yScale.ticks.length;
  9728. var xOffset = 0.5 * 1 / xCount;
  9729. var yOffset = 0.5 * 1 / yCount;
  9730. if (xScale.isCategory && yScale.isCategory) {
  9731. x = [x - xOffset, x - xOffset, x + xOffset, x + xOffset];
  9732. y = [y - yOffset, y + yOffset, y + yOffset, y - yOffset];
  9733. } else if (isArray(x)) {
  9734. temp = x;
  9735. x = [temp[0], temp[0], temp[1], temp[1]];
  9736. y = [y - yOffset / 2, y + yOffset / 2, y + yOffset / 2, y - yOffset / 2];
  9737. } else if (isArray(y)) {
  9738. temp = y;
  9739. y = [temp[0], temp[1], temp[1], temp[0]];
  9740. x = [x - xOffset / 2, x - xOffset / 2, x + xOffset / 2, x + xOffset / 2];
  9741. }
  9742. cfg.x = x;
  9743. cfg.y = y;
  9744. }
  9745. return cfg;
  9746. };
  9747. return Polygon;
  9748. }(Geom);
  9749. Geom.Polygon = Polygon$2;
  9750. function _sortValue(value) {
  9751. var sorted = value.sort(function (a, b) {
  9752. return a < b ? 1 : -1;
  9753. });
  9754. var length = sorted.length;
  9755. if (length < 4) {
  9756. var min = sorted[length - 1];
  9757. for (var i = 0; i < 4 - length; i++) {
  9758. sorted.push(min);
  9759. }
  9760. }
  9761. return sorted;
  9762. } // from left bottom corner, and clockwise
  9763. function getCandlePoints(x, y, width) {
  9764. var yValues = _sortValue(y);
  9765. var points = [{
  9766. x: x,
  9767. y: yValues[0]
  9768. }, {
  9769. x: x,
  9770. y: yValues[1]
  9771. }, {
  9772. x: x - width / 2,
  9773. y: yValues[2]
  9774. }, {
  9775. x: x - width / 2,
  9776. y: yValues[1]
  9777. }, {
  9778. x: x + width / 2,
  9779. y: yValues[1]
  9780. }, {
  9781. x: x + width / 2,
  9782. y: yValues[2]
  9783. }, {
  9784. x: x,
  9785. y: yValues[2]
  9786. }, {
  9787. x: x,
  9788. y: yValues[3]
  9789. }];
  9790. return points;
  9791. }
  9792. var Schema = Shape$1.registerFactory('schema', {});
  9793. Shape$1.registerShape('schema', 'candle', {
  9794. getPoints: function getPoints(cfg) {
  9795. return getCandlePoints(cfg.x, cfg.y, cfg.size);
  9796. },
  9797. draw: function draw(cfg, container) {
  9798. var points = this.parsePoints(cfg.points);
  9799. var style = mix({
  9800. stroke: cfg.color,
  9801. fill: cfg.color,
  9802. lineWidth: 1
  9803. }, cfg.style);
  9804. return container.addShape('Custom', {
  9805. className: 'schema',
  9806. attrs: style,
  9807. createPath: function createPath(ctx) {
  9808. ctx.beginPath();
  9809. ctx.moveTo(points[0].x, points[0].y);
  9810. ctx.lineTo(points[1].x, points[1].y);
  9811. ctx.moveTo(points[2].x, points[2].y);
  9812. for (var i = 3; i < 6; i++) {
  9813. ctx.lineTo(points[i].x, points[i].y);
  9814. }
  9815. ctx.closePath();
  9816. ctx.moveTo(points[6].x, points[6].y);
  9817. ctx.lineTo(points[7].x, points[7].y);
  9818. }
  9819. });
  9820. }
  9821. });
  9822. var Schema$1 = /*#__PURE__*/function (_Geom) {
  9823. _inheritsLoose(Schema, _Geom);
  9824. var _proto = Schema.prototype;
  9825. _proto.getDefaultCfg = function getDefaultCfg() {
  9826. var cfg = _Geom.prototype.getDefaultCfg.call(this);
  9827. cfg.type = 'schema';
  9828. cfg.shapeType = 'schema';
  9829. cfg.generatePoints = true;
  9830. return cfg;
  9831. };
  9832. function Schema(cfg) {
  9833. var _this;
  9834. _this = _Geom.call(this, cfg) || this;
  9835. mix(_assertThisInitialized(_this), SizeMixin);
  9836. return _this;
  9837. }
  9838. _proto.init = function init() {
  9839. _Geom.prototype.init.call(this); // 绑定事件
  9840. this.initEvent();
  9841. };
  9842. _proto.createShapePointsCfg = function createShapePointsCfg(obj) {
  9843. var cfg = _Geom.prototype.createShapePointsCfg.call(this, obj);
  9844. cfg.size = this.getNormalizedSize(obj);
  9845. return cfg;
  9846. };
  9847. _proto.clearInner = function clearInner() {
  9848. _Geom.prototype.clearInner.call(this);
  9849. this.set('defaultSize', null);
  9850. };
  9851. return Schema;
  9852. }(Geom);
  9853. Geom.Schema = Schema$1;
  9854. var toString$3 = {}.toString;
  9855. var isType$1 = function isType(value, type) {
  9856. return toString$3.call(value) === '[object ' + type + ']';
  9857. };
  9858. var isType_1 = isType$1;
  9859. var isArray$1 = Array.isArray ? Array.isArray : function (value) {
  9860. return isType_1(value, 'Array');
  9861. };
  9862. var isArray_1 = isArray$1;
  9863. // isFinite,
  9864. var isNil$1 = function isNil(value) {
  9865. /**
  9866. * isNil(null) => true
  9867. * isNil() => true
  9868. */
  9869. return value === null || value === undefined;
  9870. };
  9871. var isNil_1 = isNil$1;
  9872. function _inheritsLoose$1(subClass, superClass) {
  9873. subClass.prototype = Object.create(superClass.prototype);
  9874. subClass.prototype.constructor = subClass;
  9875. subClass.__proto__ = superClass;
  9876. }
  9877. var Stack = /*#__PURE__*/function (_Adjust) {
  9878. _inheritsLoose$1(Stack, _Adjust);
  9879. function Stack() {
  9880. return _Adjust.apply(this, arguments) || this;
  9881. }
  9882. var _proto = Stack.prototype;
  9883. _proto._initDefaultCfg = function _initDefaultCfg() {
  9884. this.xField = null; // 调整对应的 x 方向对应的字段名称
  9885. this.yField = null; // 调整对应的 y 方向对应的字段名称
  9886. };
  9887. _proto.processAdjust = function processAdjust(dataArray) {
  9888. this.processStack(dataArray);
  9889. };
  9890. _proto.processStack = function processStack(dataArray) {
  9891. var self = this;
  9892. var xField = self.xField;
  9893. var yField = self.yField;
  9894. var count = dataArray.length;
  9895. var stackCache = {
  9896. positive: {},
  9897. negative: {}
  9898. }; // 层叠顺序翻转
  9899. if (self.reverseOrder) {
  9900. dataArray = dataArray.slice(0).reverse();
  9901. }
  9902. for (var i = 0; i < count; i++) {
  9903. var data = dataArray[i];
  9904. for (var j = 0, len = data.length; j < len; j++) {
  9905. var item = data[j];
  9906. var x = item[xField] || 0;
  9907. var y = item[yField];
  9908. var xkey = x.toString();
  9909. y = isArray_1(y) ? y[1] : y;
  9910. if (!isNil_1(y)) {
  9911. var direction = y >= 0 ? 'positive' : 'negative';
  9912. if (!stackCache[direction][xkey]) {
  9913. stackCache[direction][xkey] = 0;
  9914. }
  9915. item[yField] = [stackCache[direction][xkey], y + stackCache[direction][xkey]];
  9916. stackCache[direction][xkey] += y;
  9917. }
  9918. }
  9919. }
  9920. };
  9921. return Stack;
  9922. }(base);
  9923. base.Stack = Stack;
  9924. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
  9925. return typeof obj;
  9926. } : function (obj) {
  9927. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  9928. };
  9929. var isObject$1 = function isObject(value) {
  9930. /**
  9931. * isObject({}) => true
  9932. * isObject([1, 2, 3]) => true
  9933. * isObject(Function) => true
  9934. * isObject(null) => false
  9935. */
  9936. var type = typeof value === 'undefined' ? 'undefined' : _typeof(value);
  9937. return value !== null && type === 'object' || type === 'function';
  9938. };
  9939. var isObject_1 = isObject$1;
  9940. var each$1 = function each(elements, func) {
  9941. if (!elements) {
  9942. return;
  9943. }
  9944. var rst = void 0;
  9945. if (isArray_1(elements)) {
  9946. for (var i = 0, len = elements.length; i < len; i++) {
  9947. rst = func(elements[i], i);
  9948. if (rst === false) {
  9949. break;
  9950. }
  9951. }
  9952. } else if (isObject_1(elements)) {
  9953. for (var k in elements) {
  9954. if (elements.hasOwnProperty(k)) {
  9955. rst = func(elements[k], k);
  9956. if (rst === false) {
  9957. break;
  9958. }
  9959. }
  9960. }
  9961. }
  9962. };
  9963. var each_1 = each$1;
  9964. function _inheritsLoose$2(subClass, superClass) {
  9965. subClass.prototype = Object.create(superClass.prototype);
  9966. subClass.prototype.constructor = subClass;
  9967. subClass.__proto__ = superClass;
  9968. }
  9969. var MARGIN_RATIO = 1 / 2;
  9970. var DODGE_RATIO = 1 / 2;
  9971. var Dodge = /*#__PURE__*/function (_Adjust) {
  9972. _inheritsLoose$2(Dodge, _Adjust);
  9973. function Dodge() {
  9974. return _Adjust.apply(this, arguments) || this;
  9975. }
  9976. var _proto = Dodge.prototype;
  9977. _proto._initDefaultCfg = function _initDefaultCfg() {
  9978. /**
  9979. * 调整过程中,2个数据的间距
  9980. * @type {Number}
  9981. */
  9982. this.marginRatio = MARGIN_RATIO;
  9983. /**
  9984. * 调整占单位宽度的比例,例如:占2个分类间距的 1/2
  9985. * @type {Number}
  9986. */
  9987. this.dodgeRatio = DODGE_RATIO;
  9988. this.adjustNames = ['x', 'y']; // 调整的维度,默认,x,y都做调整
  9989. };
  9990. _proto.getDodgeOffset = function getDodgeOffset(range, index, count) {
  9991. var self = this;
  9992. var pre = range.pre;
  9993. var next = range.next;
  9994. var tickLength = next - pre;
  9995. var width = tickLength * self.dodgeRatio / count;
  9996. var margin = self.marginRatio * width;
  9997. var offset = 1 / 2 * (tickLength - count * width - (count - 1) * margin) + ((index + 1) * width + index * margin) - 1 / 2 * width - 1 / 2 * tickLength;
  9998. return (pre + next) / 2 + offset;
  9999. };
  10000. _proto.processAdjust = function processAdjust(dataArray) {
  10001. var self = this;
  10002. var count = dataArray.length;
  10003. var xField = self.xField;
  10004. each_1(dataArray, function (data, index) {
  10005. for (var i = 0, len = data.length; i < len; i++) {
  10006. var obj = data[i];
  10007. var value = obj[xField];
  10008. var range = {
  10009. pre: len === 1 ? value - 1 : value - 0.5,
  10010. next: len === 1 ? value + 1 : value + 0.5
  10011. };
  10012. var dodgeValue = self.getDodgeOffset(range, index, count);
  10013. obj[xField] = dodgeValue;
  10014. }
  10015. });
  10016. };
  10017. return Dodge;
  10018. }(base);
  10019. base.Dodge = Dodge;
  10020. /**
  10021. * 是否为函数
  10022. * @param {*} fn 对象
  10023. * @return {Boolean} 是否函数
  10024. */
  10025. var isFunction$1 = function isFunction(value) {
  10026. return isType_1(value, 'Function');
  10027. };
  10028. var isFunction_1 = isFunction$1;
  10029. /**
  10030. * @param {Array} arr The array to iterate over.
  10031. * @param {Function} [fn] The iteratee invoked per element.
  10032. * @return {*} Returns the maximum value.
  10033. * @example
  10034. *
  10035. * var objects = [{ 'n': 1 }, { 'n': 2 }];
  10036. *
  10037. * maxBy(objects, function(o) { return o.n; });
  10038. * // => { 'n': 2 }
  10039. *
  10040. * maxBy(objects, 'n');
  10041. * // => { 'n': 2 }
  10042. */
  10043. var maxBy = function maxBy(arr, fn) {
  10044. if (!isArray_1(arr)) {
  10045. return undefined;
  10046. }
  10047. var max = arr[0];
  10048. var maxData = void 0;
  10049. if (isFunction_1(fn)) {
  10050. maxData = fn(arr[0]);
  10051. } else {
  10052. maxData = arr[0][fn];
  10053. }
  10054. var data = void 0;
  10055. each_1(arr, function (val) {
  10056. if (isFunction_1(fn)) {
  10057. data = fn(val);
  10058. } else {
  10059. data = val[fn];
  10060. }
  10061. if (data > maxData) {
  10062. max = val;
  10063. maxData = data;
  10064. }
  10065. });
  10066. return max;
  10067. };
  10068. var maxBy_1 = maxBy;
  10069. var merge$1 = function merge(dataArray) {
  10070. var rst = [];
  10071. for (var i = 0; i < dataArray.length; i++) {
  10072. rst = rst.concat(dataArray[i]);
  10073. }
  10074. return rst;
  10075. };
  10076. var merge_1 = merge$1;
  10077. function _inheritsLoose$3(subClass, superClass) {
  10078. subClass.prototype = Object.create(superClass.prototype);
  10079. subClass.prototype.constructor = subClass;
  10080. subClass.__proto__ = superClass;
  10081. }
  10082. var ArrayUtil = {
  10083. merge: merge_1
  10084. };
  10085. var Symmetric = /*#__PURE__*/function (_Adjust) {
  10086. _inheritsLoose$3(Symmetric, _Adjust);
  10087. function Symmetric() {
  10088. return _Adjust.apply(this, arguments) || this;
  10089. }
  10090. var _proto = Symmetric.prototype;
  10091. _proto._initDefaultCfg = function _initDefaultCfg() {
  10092. this.xField = null; // 调整对应的 x 方向对应的字段名称
  10093. this.yField = null; // 调整对应的 y 方向对应的字段名称
  10094. this.cacheMax = null; // 缓存的最大值
  10095. this.adjustNames = ['y']; // Only support stack y
  10096. this.groupFields = null; // 参与分组的数据维度
  10097. }; // 获取最大的y值
  10098. _proto._getMax = function _getMax(dim) {
  10099. var self = this;
  10100. var mergeData = self.mergeData;
  10101. var maxRecord = maxBy_1(mergeData, function (obj) {
  10102. var value = obj[dim];
  10103. if (isArray_1(value)) {
  10104. return Math.max.apply(null, value);
  10105. }
  10106. return value;
  10107. });
  10108. var maxValue = maxRecord[dim];
  10109. var max = isArray_1(maxValue) ? Math.max.apply(null, maxValue) : maxValue;
  10110. return max;
  10111. }; // 获取每个字段最大的值
  10112. _proto._getXValuesMax = function _getXValuesMax() {
  10113. var self = this;
  10114. var yField = self.yField;
  10115. var xField = self.xField;
  10116. var cache = {};
  10117. var mergeData = self.mergeData;
  10118. each_1(mergeData, function (obj) {
  10119. var xValue = obj[xField];
  10120. var yValue = obj[yField];
  10121. var max = isArray_1(yValue) ? Math.max.apply(null, yValue) : yValue;
  10122. cache[xValue] = cache[xValue] || 0;
  10123. if (cache[xValue] < max) {
  10124. cache[xValue] = max;
  10125. }
  10126. });
  10127. return cache;
  10128. }; // 入口函数
  10129. _proto.processAdjust = function processAdjust(dataArray) {
  10130. var self = this;
  10131. var mergeData = ArrayUtil.merge(dataArray);
  10132. self.mergeData = mergeData;
  10133. self._processSymmetric(dataArray);
  10134. self.mergeData = null;
  10135. }; // 处理对称
  10136. _proto._processSymmetric = function _processSymmetric(dataArray) {
  10137. var self = this;
  10138. var xField = self.xField;
  10139. var yField = self.yField;
  10140. var max = self._getMax(yField);
  10141. var first = dataArray[0][0];
  10142. var cache;
  10143. if (first && isArray_1(first[yField])) {
  10144. cache = self._getXValuesMax();
  10145. }
  10146. each_1(dataArray, function (data) {
  10147. each_1(data, function (obj) {
  10148. var value = obj[yField];
  10149. var offset;
  10150. if (isArray_1(value)) {
  10151. var xValue = obj[xField];
  10152. var valueMax = cache[xValue];
  10153. offset = (max - valueMax) / 2;
  10154. var tmp = [];
  10155. /* eslint-disable no-loop-func */
  10156. each_1(value, function (subVal) {
  10157. // 多个字段
  10158. tmp.push(offset + subVal);
  10159. });
  10160. /* eslint-enable no-loop-func */
  10161. obj[yField] = tmp;
  10162. } else {
  10163. offset = (max - value) / 2;
  10164. obj[yField] = [offset, value + offset];
  10165. }
  10166. });
  10167. });
  10168. };
  10169. return Symmetric;
  10170. }(base);
  10171. base.Symmetric = Symmetric;
  10172. var Polar = /*#__PURE__*/function (_Base) {
  10173. _inheritsLoose(Polar, _Base);
  10174. function Polar() {
  10175. return _Base.apply(this, arguments) || this;
  10176. }
  10177. var _proto = Polar.prototype;
  10178. _proto._initDefaultCfg = function _initDefaultCfg() {
  10179. this.type = 'polar';
  10180. this.startAngle = -Math.PI / 2;
  10181. this.endAngle = Math.PI * 3 / 2;
  10182. this.inner = 0;
  10183. this.innerRadius = 0; // alias
  10184. this.isPolar = true;
  10185. this.transposed = false;
  10186. this.center = null;
  10187. this.radius = null; // relative, 0 ~ 1
  10188. };
  10189. _proto.init = function init(start, end) {
  10190. _Base.prototype.init.call(this, start, end);
  10191. var self = this;
  10192. var inner = self.inner || self.innerRadius;
  10193. var width = Math.abs(end.x - start.x);
  10194. var height = Math.abs(end.y - start.y);
  10195. var maxRadius;
  10196. var center;
  10197. if (self.startAngle === -Math.PI && self.endAngle === 0) {
  10198. maxRadius = Math.min(width / 2, height);
  10199. center = {
  10200. x: (start.x + end.x) / 2,
  10201. y: start.y
  10202. };
  10203. } else {
  10204. maxRadius = Math.min(width, height) / 2;
  10205. center = {
  10206. x: (start.x + end.x) / 2,
  10207. y: (start.y + end.y) / 2
  10208. };
  10209. }
  10210. var radius = self.radius;
  10211. if (radius > 0 && radius <= 1) {
  10212. maxRadius = maxRadius * radius;
  10213. }
  10214. this.x = {
  10215. start: self.startAngle,
  10216. end: self.endAngle
  10217. };
  10218. this.y = {
  10219. start: maxRadius * inner,
  10220. end: maxRadius
  10221. };
  10222. this.center = center;
  10223. this.circleRadius = maxRadius; // the radius value in px
  10224. };
  10225. _proto._convertPoint = function _convertPoint(point) {
  10226. var self = this;
  10227. var center = self.center;
  10228. var transposed = self.transposed;
  10229. var xDim = transposed ? 'y' : 'x';
  10230. var yDim = transposed ? 'x' : 'y';
  10231. var x = self.x;
  10232. var y = self.y;
  10233. var angle = x.start + (x.end - x.start) * point[xDim];
  10234. var radius = y.start + (y.end - y.start) * point[yDim];
  10235. return {
  10236. x: center.x + Math.cos(angle) * radius,
  10237. y: center.y + Math.sin(angle) * radius
  10238. };
  10239. };
  10240. _proto._invertPoint = function _invertPoint(point) {
  10241. var self = this;
  10242. var center = self.center,
  10243. transposed = self.transposed,
  10244. x = self.x,
  10245. y = self.y;
  10246. var xDim = transposed ? 'y' : 'x';
  10247. var yDim = transposed ? 'x' : 'y';
  10248. var m = [1, 0, 0, 1, 0, 0];
  10249. Matrix.rotate(m, m, x.start);
  10250. var startV = [1, 0];
  10251. Vector2.transformMat2d(startV, startV, m);
  10252. startV = [startV[0], startV[1]];
  10253. var pointV = [point.x - center.x, point.y - center.y];
  10254. if (Vector2.zero(pointV)) {
  10255. return {
  10256. x: 0,
  10257. y: 0
  10258. };
  10259. }
  10260. var theta = Vector2.angleTo(startV, pointV, x.end < x.start);
  10261. if (Math.abs(theta - Math.PI * 2) < 0.001) {
  10262. theta = 0;
  10263. }
  10264. var l = Vector2.length(pointV);
  10265. var percentX = theta / (x.end - x.start);
  10266. percentX = x.end - x.start > 0 ? percentX : -percentX;
  10267. var percentY = (l - y.start) / (y.end - y.start);
  10268. var rst = {};
  10269. rst[xDim] = percentX;
  10270. rst[yDim] = percentY;
  10271. return rst;
  10272. };
  10273. return Polar;
  10274. }(Base$1);
  10275. Base$1.Polar = Polar;
  10276. var Circle$1 = /*#__PURE__*/function (_Abstract) {
  10277. _inheritsLoose(Circle, _Abstract);
  10278. function Circle() {
  10279. return _Abstract.apply(this, arguments) || this;
  10280. }
  10281. var _proto = Circle.prototype;
  10282. _proto._initDefaultCfg = function _initDefaultCfg() {
  10283. _Abstract.prototype._initDefaultCfg.call(this);
  10284. this.startAngle = -Math.PI / 2; // start angle,in radian
  10285. this.endAngle = Math.PI * 3 / 2; // end angle, in radian
  10286. this.radius = null; // radius
  10287. this.center = null; // center
  10288. };
  10289. _proto.getOffsetPoint = function getOffsetPoint(value) {
  10290. var startAngle = this.startAngle,
  10291. endAngle = this.endAngle;
  10292. var angle = startAngle + (endAngle - startAngle) * value;
  10293. return this._getCirclePoint(angle);
  10294. };
  10295. _proto._getCirclePoint = function _getCirclePoint(angle, radius) {
  10296. var self = this;
  10297. var center = self.center;
  10298. radius = radius || self.radius;
  10299. return {
  10300. x: center.x + Math.cos(angle) * radius,
  10301. y: center.y + Math.sin(angle) * radius
  10302. };
  10303. };
  10304. _proto.getTextAlignInfo = function getTextAlignInfo(point, offset) {
  10305. var self = this;
  10306. var offsetVector = self.getOffsetVector(point, offset);
  10307. var align;
  10308. var baseLine = 'middle';
  10309. if (offsetVector[0] > 0) {
  10310. align = 'left';
  10311. } else if (offsetVector[0] < 0) {
  10312. align = 'right';
  10313. } else {
  10314. align = 'center';
  10315. if (offsetVector[1] > 0) {
  10316. baseLine = 'top';
  10317. } else if (offsetVector[1] < 0) {
  10318. baseLine = 'bottom';
  10319. }
  10320. }
  10321. return {
  10322. textAlign: align,
  10323. textBaseline: baseLine
  10324. };
  10325. };
  10326. _proto.getAxisVector = function getAxisVector(point) {
  10327. var center = this.center;
  10328. var factor = this.offsetFactor;
  10329. return [(point.y - center.y) * factor, (point.x - center.x) * -1 * factor];
  10330. };
  10331. _proto.drawLine = function drawLine(lineCfg) {
  10332. var center = this.center,
  10333. radius = this.radius,
  10334. startAngle = this.startAngle,
  10335. endAngle = this.endAngle;
  10336. var container = this.getContainer(lineCfg.top);
  10337. container.addShape('arc', {
  10338. className: 'axis-line',
  10339. attrs: mix({
  10340. x: center.x,
  10341. y: center.y,
  10342. r: radius,
  10343. startAngle: startAngle,
  10344. endAngle: endAngle
  10345. }, lineCfg)
  10346. });
  10347. };
  10348. return Circle;
  10349. }(Abastract);
  10350. Abastract.Circle = Circle$1;
  10351. var KEYWORDS_PERCENT = {
  10352. min: 0,
  10353. median: 0.5,
  10354. max: 1
  10355. };
  10356. var GuideBase = /*#__PURE__*/function () {
  10357. var _proto = GuideBase.prototype;
  10358. _proto._initDefaultCfg = function _initDefaultCfg() {};
  10359. function GuideBase(cfg) {
  10360. this._initDefaultCfg();
  10361. deepMix(this, cfg);
  10362. }
  10363. _proto._getNormalizedValue = function _getNormalizedValue(val, scale) {
  10364. var rst;
  10365. if (isNil(KEYWORDS_PERCENT[val])) {
  10366. rst = scale.scale(val);
  10367. } else {
  10368. rst = KEYWORDS_PERCENT[val];
  10369. }
  10370. return rst;
  10371. };
  10372. _proto.parsePercentPoint = function parsePercentPoint(coord, position) {
  10373. var xPercent = parseFloat(position[0]) / 100;
  10374. var yPercent = parseFloat(position[1]) / 100;
  10375. var start = coord.start;
  10376. var end = coord.end;
  10377. var width = Math.abs(start.x - end.x);
  10378. var height = Math.abs(start.y - end.y);
  10379. var x = width * xPercent + Math.min(start.x, end.x);
  10380. var y = height * yPercent + Math.min(start.y, end.y);
  10381. return {
  10382. x: x,
  10383. y: y
  10384. };
  10385. };
  10386. _proto.parsePoint = function parsePoint(coord, position) {
  10387. var self = this;
  10388. var xScale = self.xScale;
  10389. var yScales = self.yScales;
  10390. if (isFunction(position)) {
  10391. position = position(xScale, yScales); // position 必须是对象
  10392. } // 如果数据格式是 ['50%', '50%'] 的格式
  10393. // fix: 原始数据中可能会包含 'xxx5%xxx' 这样的数据,需要判断下 https://github.com/antvis/f2/issues/590
  10394. if (isString(position[0]) && position[0].indexOf('%') !== -1 && !isNaN(position[0].slice(0, -1))) {
  10395. return this.parsePercentPoint(coord, position);
  10396. }
  10397. var x = self._getNormalizedValue(position[0], xScale);
  10398. var y = self._getNormalizedValue(position[1], yScales[0]);
  10399. var point = coord.convertPoint({
  10400. x: x,
  10401. y: y
  10402. });
  10403. if (self.limitInPlot) {
  10404. // limit in chart plotRange
  10405. if (x >= 0 && x <= 1 && y >= 0 && y <= 1) {
  10406. return point;
  10407. }
  10408. return null;
  10409. }
  10410. return point;
  10411. }
  10412. /**
  10413. * render the guide component
  10414. * @param {Coord} coord coordinate instance
  10415. * @param {Canvas.Group} group the container
  10416. */
  10417. ;
  10418. _proto.render = function render()
  10419. /* coord,group */
  10420. {};
  10421. _proto.repaint = function repaint() {
  10422. this.remove();
  10423. var coord = this.coord,
  10424. container = this.container,
  10425. canvas = this.canvas;
  10426. if (container && !container.isDestroyed()) {
  10427. this.render(coord, container);
  10428. canvas.draw();
  10429. }
  10430. };
  10431. _proto.remove = function remove() {
  10432. var element = this.element;
  10433. element && element.remove(true);
  10434. };
  10435. _proto.changeVisible = function changeVisible(visible) {
  10436. var self = this;
  10437. self.visible = visible;
  10438. var element = self.element;
  10439. if (!element) return;
  10440. if (element.set) {
  10441. element.set('visible', visible);
  10442. } else {
  10443. element.style.display = visible ? '' : 'none';
  10444. }
  10445. };
  10446. return GuideBase;
  10447. }();
  10448. var Arc$1 = /*#__PURE__*/function (_GuideBase) {
  10449. _inheritsLoose(Arc, _GuideBase);
  10450. function Arc() {
  10451. return _GuideBase.apply(this, arguments) || this;
  10452. }
  10453. var _proto = Arc.prototype;
  10454. _proto._initDefaultCfg = function _initDefaultCfg() {
  10455. this.type = 'arc';
  10456. /**
  10457. * start point
  10458. * @type {Array | Function}
  10459. */
  10460. this.start = [];
  10461. /**
  10462. * end point
  10463. * @type {Array | Function}
  10464. */
  10465. this.end = [];
  10466. /**
  10467. * style configuration
  10468. * @type {Object}
  10469. */
  10470. this.style = {
  10471. stroke: '#999',
  10472. lineWidth: 1
  10473. };
  10474. };
  10475. _proto.render = function render(coord, container) {
  10476. var self = this;
  10477. var start = self.parsePoint(coord, self.start);
  10478. var end = self.parsePoint(coord, self.end);
  10479. if (!start || !end) {
  10480. return;
  10481. }
  10482. var coordCenter = coord.center;
  10483. var radius = Math.sqrt((start.x - coordCenter.x) * (start.x - coordCenter.x) + (start.y - coordCenter.y) * (start.y - coordCenter.y));
  10484. var startAngle = Math.atan2(start.y - coordCenter.y, start.x - coordCenter.x);
  10485. var endAngle = Math.atan2(end.y - coordCenter.y, end.x - coordCenter.x);
  10486. var shape = container.addShape('arc', {
  10487. className: 'guide-arc',
  10488. attrs: mix({
  10489. x: coordCenter.x,
  10490. y: coordCenter.y,
  10491. r: radius,
  10492. startAngle: startAngle,
  10493. endAngle: endAngle
  10494. }, self.style)
  10495. });
  10496. self.element = shape;
  10497. return shape;
  10498. };
  10499. return Arc;
  10500. }(GuideBase);
  10501. GuideBase.Arc = Arc$1;
  10502. function getOffsetFromAlign(alignX, alignY, width, height) {
  10503. var result = [];
  10504. if (alignX === 'left' && alignY === 'top') {
  10505. result[0] = 0;
  10506. result[1] = 0;
  10507. } else if (alignX === 'right' && alignY === 'top') {
  10508. result[0] = -width;
  10509. result[1] = 0;
  10510. } else if (alignX === 'left' && alignY === 'bottom') {
  10511. result[0] = 0;
  10512. result[1] = Math.floor(-height);
  10513. } else if (alignX === 'right' && alignY === 'bottom') {
  10514. result[0] = Math.floor(-width);
  10515. result[1] = Math.floor(-height);
  10516. } else if (alignX === 'right' && alignY === 'middle') {
  10517. result[0] = Math.floor(-width);
  10518. result[1] = Math.floor(-height / 2);
  10519. } else if (alignX === 'left' && alignY === 'middle') {
  10520. result[0] = 0;
  10521. result[1] = Math.floor(-height / 2);
  10522. } else if (alignX === 'center' && alignY === 'bottom') {
  10523. result[0] = Math.floor(-width / 2);
  10524. result[1] = Math.floor(-height);
  10525. } else if (alignX === 'center' && alignY === 'top') {
  10526. result[0] = Math.floor(-width / 2);
  10527. result[1] = 0;
  10528. } else {
  10529. result[0] = Math.floor(-width / 2);
  10530. result[1] = Math.floor(-height / 2);
  10531. }
  10532. return result;
  10533. }
  10534. function modifyCSS(DOM, CSS) {
  10535. for (var key in CSS) {
  10536. if (CSS.hasOwnProperty(key)) {
  10537. DOM.style[key] = CSS[key];
  10538. }
  10539. }
  10540. return DOM;
  10541. }
  10542. function createDom(str) {
  10543. var container = document.createElement('div');
  10544. str = str.replace(/(^\s*)|(\s*$)/g, '');
  10545. container.innerHTML = '' + str;
  10546. return container.childNodes[0];
  10547. }
  10548. var Html = /*#__PURE__*/function (_GuideBase) {
  10549. _inheritsLoose(Html, _GuideBase);
  10550. function Html() {
  10551. return _GuideBase.apply(this, arguments) || this;
  10552. }
  10553. var _proto = Html.prototype;
  10554. _proto._initDefaultCfg = function _initDefaultCfg() {
  10555. this.type = 'html';
  10556. /**
  10557. * dom position
  10558. * @type {Object | Array}
  10559. */
  10560. this.position = null;
  10561. /**
  10562. * alignment for horizontal direction,can be 'left','center','right'
  10563. * @type {String}
  10564. */
  10565. this.alignX = 'center';
  10566. /**
  10567. * alignment for vertical direction,can be 'top', 'middle', 'bottom'
  10568. * @type {String}
  10569. */
  10570. this.alignY = 'middle';
  10571. /**
  10572. * offset for horizontal direction
  10573. * @type {Number}
  10574. */
  10575. this.offsetX = null;
  10576. /**
  10577. * offset for vertical direction
  10578. * @type {Number}
  10579. */
  10580. this.offsetY = null;
  10581. /**
  10582. * the html string
  10583. *@type {String | Function}
  10584. */
  10585. this.html = null;
  10586. } // override paint
  10587. ;
  10588. _proto.render = function render(coord, container) {
  10589. var self = this;
  10590. var position = self.parsePoint(coord, self.position);
  10591. if (!position) {
  10592. return;
  10593. }
  10594. var myNode = createDom(self.html);
  10595. myNode = modifyCSS(myNode, {
  10596. position: 'absolute',
  10597. top: Math.floor(position.y) + 'px',
  10598. left: Math.floor(position.x) + 'px',
  10599. visibility: 'hidden'
  10600. });
  10601. var canvasDom = container.get('canvas').get('el');
  10602. var parentNode = canvasDom.parentNode;
  10603. parentNode = modifyCSS(parentNode, {
  10604. position: 'relative'
  10605. });
  10606. var wrapperNode = createDom('<div class="guideWapper" style="position: absolute;top: 0; left: 0;"></div>');
  10607. parentNode.appendChild(wrapperNode);
  10608. wrapperNode.appendChild(myNode);
  10609. var canvasOffsetTop = canvasDom.offsetTop;
  10610. var canvasOffsetLeft = canvasDom.offsetLeft;
  10611. var alignX = self.alignX,
  10612. alignY = self.alignY,
  10613. offsetX = self.offsetX,
  10614. offsetY = self.offsetY;
  10615. var width = getWidth(myNode);
  10616. var height = getHeight(myNode);
  10617. var newOffset = getOffsetFromAlign(alignX, alignY, width, height);
  10618. position.x = position.x + newOffset[0] + canvasOffsetLeft;
  10619. position.y = position.y + newOffset[1] + canvasOffsetTop;
  10620. if (offsetX) {
  10621. position.x += offsetX;
  10622. }
  10623. if (offsetY) {
  10624. position.y += offsetY;
  10625. }
  10626. modifyCSS(myNode, {
  10627. top: Math.floor(position.y) + 'px',
  10628. left: Math.floor(position.x) + 'px',
  10629. visibility: 'visible'
  10630. });
  10631. self.element = wrapperNode;
  10632. };
  10633. _proto.remove = function remove() {
  10634. var element = this.element;
  10635. element && element.parentNode && element.parentNode.removeChild(element);
  10636. };
  10637. return Html;
  10638. }(GuideBase);
  10639. GuideBase.Html = Html;
  10640. var Line$4 = /*#__PURE__*/function (_GuideBase) {
  10641. _inheritsLoose(Line, _GuideBase);
  10642. function Line() {
  10643. return _GuideBase.apply(this, arguments) || this;
  10644. }
  10645. var _proto = Line.prototype;
  10646. _proto._initDefaultCfg = function _initDefaultCfg() {
  10647. this.type = 'line';
  10648. this.start = [];
  10649. this.end = [];
  10650. this.style = {
  10651. stroke: '#000',
  10652. lineWidth: 1
  10653. };
  10654. };
  10655. _proto.render = function render(coord, container) {
  10656. var points = [];
  10657. points[0] = this.parsePoint(coord, this.start);
  10658. points[1] = this.parsePoint(coord, this.end);
  10659. if (!points[0] || !points[1]) {
  10660. return;
  10661. }
  10662. var shape = container.addShape('Line', {
  10663. className: 'guide-line',
  10664. attrs: mix({
  10665. x1: points[0].x,
  10666. y1: points[0].y,
  10667. x2: points[1].x,
  10668. y2: points[1].y
  10669. }, this.style)
  10670. });
  10671. this.element = shape;
  10672. return shape;
  10673. };
  10674. return Line;
  10675. }(GuideBase);
  10676. GuideBase.Line = Line$4;
  10677. var Rect$2 = /*#__PURE__*/function (_GuideBase) {
  10678. _inheritsLoose(Rect, _GuideBase);
  10679. function Rect() {
  10680. return _GuideBase.apply(this, arguments) || this;
  10681. }
  10682. var _proto = Rect.prototype;
  10683. _proto._initDefaultCfg = function _initDefaultCfg() {
  10684. this.type = 'rect';
  10685. this.start = [];
  10686. this.end = [];
  10687. this.style = {
  10688. fill: '#CCD7EB',
  10689. opacity: 0.4
  10690. };
  10691. };
  10692. _proto.render = function render(coord, container) {
  10693. var start = this.parsePoint(coord, this.start);
  10694. var end = this.parsePoint(coord, this.end);
  10695. if (!start || !end) {
  10696. return;
  10697. }
  10698. var shape = container.addShape('rect', {
  10699. className: 'guide-rect',
  10700. attrs: mix({
  10701. x: Math.min(start.x, end.x),
  10702. y: Math.min(start.y, end.y),
  10703. width: Math.abs(end.x - start.x),
  10704. height: Math.abs(start.y - end.y)
  10705. }, this.style)
  10706. });
  10707. this.element = shape;
  10708. return shape;
  10709. };
  10710. return Rect;
  10711. }(GuideBase);
  10712. GuideBase.Rect = Rect$2;
  10713. var Text$1 = /*#__PURE__*/function (_GuideBase) {
  10714. _inheritsLoose(Text, _GuideBase);
  10715. function Text() {
  10716. return _GuideBase.apply(this, arguments) || this;
  10717. }
  10718. var _proto = Text.prototype;
  10719. _proto._initDefaultCfg = function _initDefaultCfg() {
  10720. this.type = 'text';
  10721. /**
  10722. * the position of text
  10723. * @type {Function | Array}
  10724. */
  10725. this.position = null;
  10726. /**
  10727. * the display content
  10728. * @type {String}
  10729. */
  10730. this.content = null;
  10731. /**
  10732. * style configuration for text
  10733. * @type {Object}
  10734. */
  10735. this.style = {
  10736. fill: '#000'
  10737. };
  10738. /**
  10739. * offset of horizontal direction
  10740. * @type {Number}
  10741. */
  10742. this.offsetX = 0;
  10743. /**
  10744. * offset of vertical direction
  10745. * @type {Number}
  10746. */
  10747. this.offsetY = 0;
  10748. };
  10749. _proto.render = function render(coord, container) {
  10750. var position = this.position;
  10751. var point = this.parsePoint(coord, position);
  10752. if (!point) {
  10753. return;
  10754. }
  10755. var content = this.content,
  10756. style = this.style,
  10757. offsetX = this.offsetX,
  10758. offsetY = this.offsetY;
  10759. if (offsetX) {
  10760. point.x += offsetX;
  10761. }
  10762. if (offsetY) {
  10763. point.y += offsetY;
  10764. }
  10765. var shape = container.addShape('text', {
  10766. className: 'guide-text',
  10767. attrs: mix({
  10768. x: point.x,
  10769. y: point.y,
  10770. text: content
  10771. }, style)
  10772. });
  10773. this.element = shape;
  10774. return shape;
  10775. };
  10776. return Text;
  10777. }(GuideBase);
  10778. GuideBase.Text = Text$1;
  10779. var Tag = /*#__PURE__*/function (_GuideBase) {
  10780. _inheritsLoose(Tag, _GuideBase);
  10781. function Tag() {
  10782. return _GuideBase.apply(this, arguments) || this;
  10783. }
  10784. var _proto = Tag.prototype;
  10785. _proto._initDefaultCfg = function _initDefaultCfg() {
  10786. this.type = 'tag';
  10787. this.position = null;
  10788. this.content = null;
  10789. this.direct = 'tl';
  10790. this.autoAdjust = true;
  10791. this.offsetX = 0;
  10792. this.offsetY = 0;
  10793. this.side = 4;
  10794. this.background = {
  10795. padding: 5,
  10796. radius: 2,
  10797. fill: '#1890FF'
  10798. };
  10799. this.textStyle = {
  10800. fontSize: 12,
  10801. fill: '#fff',
  10802. textAlign: 'center',
  10803. textBaseline: 'middle'
  10804. };
  10805. this.withPoint = true;
  10806. this.pointStyle = {
  10807. fill: '#1890FF',
  10808. r: 3,
  10809. lineWidth: 1,
  10810. stroke: '#fff'
  10811. };
  10812. };
  10813. _proto._getDirect = function _getDirect(container, point, tagWidth, tagHeight) {
  10814. var direct = this.direct;
  10815. var side = this.side;
  10816. var canvas = container.get('canvas');
  10817. var clientWidth = canvas.get('width');
  10818. var clientHeight = canvas.get('height');
  10819. var x = point.x,
  10820. y = point.y;
  10821. var vertical = direct[0];
  10822. var horizontal = direct[1]; // adjust for vertical direction
  10823. if (vertical === 't' && y - side - tagHeight < 0) {
  10824. vertical = 'b';
  10825. } else if (vertical === 'b' && y + side + tagHeight > clientHeight) {
  10826. vertical = 't';
  10827. } // adjust for horizontal direction
  10828. var diff = vertical === 'c' ? side : 0;
  10829. if (horizontal === 'l' && x - diff - tagWidth < 0) {
  10830. horizontal = 'r';
  10831. } else if (horizontal === 'r' && x + diff + tagWidth > clientWidth) {
  10832. horizontal = 'l';
  10833. } else if (horizontal === 'c') {
  10834. if (tagWidth / 2 + x + diff > clientWidth) {
  10835. horizontal = 'l';
  10836. } else if (x - tagWidth / 2 - diff < 0) {
  10837. horizontal = 'r';
  10838. }
  10839. }
  10840. direct = vertical + horizontal;
  10841. return direct;
  10842. };
  10843. _proto.render = function render(coord, container) {
  10844. var position = this.parsePoint(coord, this.position);
  10845. if (!position) {
  10846. return;
  10847. } // 数据不在显示范围内时,x/y 会为NaN
  10848. if (isNaN(position.x) || isNaN(position.y)) {
  10849. return;
  10850. }
  10851. var content = this.content,
  10852. background = this.background,
  10853. textStyle = this.textStyle;
  10854. var shapes = [];
  10855. var wrapperContainer = container.addGroup({
  10856. className: 'guide-tag'
  10857. });
  10858. if (this.withPoint) {
  10859. var pointShape = wrapperContainer.addShape('Circle', {
  10860. className: 'guide-tag-point',
  10861. attrs: mix({
  10862. x: position.x,
  10863. y: position.y
  10864. }, this.pointStyle)
  10865. });
  10866. shapes.push(pointShape);
  10867. }
  10868. var tagContainer = wrapperContainer.addGroup(); // create a text shape
  10869. var tagText = tagContainer.addShape('text', {
  10870. className: 'guide-tag-text',
  10871. zIndex: 1,
  10872. attrs: mix({
  10873. x: 0,
  10874. y: 0,
  10875. text: content
  10876. }, textStyle)
  10877. });
  10878. shapes.push(tagText); // create background box
  10879. var textBBox = tagText.getBBox();
  10880. var padding = parsePadding(background.padding);
  10881. var tagWidth = textBBox.width + padding[1] + padding[3];
  10882. var tagHeight = textBBox.height + padding[0] + padding[2];
  10883. var yMin = textBBox.minY - padding[0];
  10884. var xMin = textBBox.minX - padding[3];
  10885. var tagBg = tagContainer.addShape('rect', {
  10886. className: 'guide-tag-bg',
  10887. zIndex: -1,
  10888. attrs: mix({
  10889. x: xMin,
  10890. y: yMin,
  10891. width: tagWidth,
  10892. height: tagHeight
  10893. }, background)
  10894. });
  10895. shapes.push(tagBg);
  10896. var direct = this.autoAdjust ? this._getDirect(container, position, tagWidth, tagHeight) : this.direct;
  10897. var side = this.side;
  10898. var x = position.x + this.offsetX;
  10899. var y = position.y + this.offsetY;
  10900. var arrowPoints;
  10901. var radius = parsePadding(background.radius);
  10902. if (direct === 'tl') {
  10903. arrowPoints = [{
  10904. x: tagWidth + xMin - side - 1,
  10905. y: tagHeight + yMin - 1
  10906. }, // 这个 1 是为了防止出现白边
  10907. {
  10908. x: tagWidth + xMin,
  10909. y: tagHeight + yMin - 1
  10910. }, {
  10911. x: tagWidth + xMin,
  10912. y: tagHeight + side + yMin
  10913. }];
  10914. radius[2] = 0;
  10915. x = x - tagWidth;
  10916. y = y - side - tagHeight;
  10917. } else if (direct === 'cl') {
  10918. arrowPoints = [{
  10919. x: tagWidth + xMin - 1,
  10920. y: (tagHeight - side) / 2 + yMin - 1
  10921. }, {
  10922. x: tagWidth + xMin - 1,
  10923. y: (tagHeight + side) / 2 + yMin + 1
  10924. }, {
  10925. x: tagWidth + side + xMin,
  10926. y: tagHeight / 2 + yMin
  10927. }];
  10928. x = x - tagWidth - side;
  10929. y = y - tagHeight / 2;
  10930. } else if (direct === 'bl') {
  10931. arrowPoints = [{
  10932. x: tagWidth + xMin,
  10933. y: -side + yMin
  10934. }, {
  10935. x: tagWidth + xMin - side - 1,
  10936. y: yMin + 1
  10937. }, {
  10938. x: tagWidth + xMin,
  10939. y: yMin + 1
  10940. }];
  10941. radius[1] = 0;
  10942. x = x - tagWidth;
  10943. y = y + side;
  10944. } else if (direct === 'bc') {
  10945. arrowPoints = [{
  10946. x: tagWidth / 2 + xMin,
  10947. y: -side + yMin
  10948. }, {
  10949. x: (tagWidth - side) / 2 + xMin - 1,
  10950. y: yMin + 1
  10951. }, {
  10952. x: (tagWidth + side) / 2 + xMin + 1,
  10953. y: yMin + 1
  10954. }];
  10955. x = x - tagWidth / 2;
  10956. y = y + side;
  10957. } else if (direct === 'br') {
  10958. arrowPoints = [{
  10959. x: xMin,
  10960. y: yMin - side
  10961. }, {
  10962. x: xMin,
  10963. y: yMin + 1
  10964. }, {
  10965. x: xMin + side + 1,
  10966. y: yMin + 1
  10967. }];
  10968. radius[0] = 0;
  10969. y = y + side;
  10970. } else if (direct === 'cr') {
  10971. arrowPoints = [{
  10972. x: xMin - side,
  10973. y: tagHeight / 2 + yMin
  10974. }, {
  10975. x: xMin + 1,
  10976. y: (tagHeight - side) / 2 + yMin - 1
  10977. }, {
  10978. x: xMin + 1,
  10979. y: (tagHeight + side) / 2 + yMin + 1
  10980. }];
  10981. x = x + side;
  10982. y = y - tagHeight / 2;
  10983. } else if (direct === 'tr') {
  10984. arrowPoints = [{
  10985. x: xMin,
  10986. y: tagHeight + side + yMin
  10987. }, {
  10988. x: xMin,
  10989. y: tagHeight + yMin - 1
  10990. }, {
  10991. x: side + xMin + 1,
  10992. y: tagHeight + yMin - 1
  10993. }];
  10994. radius[3] = 0;
  10995. y = y - tagHeight - side;
  10996. } else if (direct === 'tc') {
  10997. arrowPoints = [{
  10998. x: (tagWidth - side) / 2 + xMin - 1,
  10999. y: tagHeight + yMin - 1
  11000. }, {
  11001. x: (tagWidth + side) / 2 + xMin + 1,
  11002. y: tagHeight + yMin - 1
  11003. }, {
  11004. x: tagWidth / 2 + xMin,
  11005. y: tagHeight + side + yMin
  11006. }];
  11007. x = x - tagWidth / 2;
  11008. y = y - tagHeight - side;
  11009. }
  11010. var sideShape = tagContainer.addShape('Polygon', {
  11011. className: 'guide-tag-side',
  11012. zIndex: 0,
  11013. attrs: {
  11014. points: arrowPoints,
  11015. fill: background.fill
  11016. }
  11017. });
  11018. shapes.push(sideShape);
  11019. tagBg.attr('radius', radius);
  11020. tagContainer.moveTo(x - xMin, y - yMin);
  11021. tagContainer.sort();
  11022. this.element = wrapperContainer;
  11023. return shapes;
  11024. };
  11025. return Tag;
  11026. }(GuideBase);
  11027. GuideBase.Tag = Tag;
  11028. var Point$2 = /*#__PURE__*/function (_GuideBase) {
  11029. _inheritsLoose(Point, _GuideBase);
  11030. function Point() {
  11031. return _GuideBase.apply(this, arguments) || this;
  11032. }
  11033. var _proto = Point.prototype;
  11034. _proto._initDefaultCfg = function _initDefaultCfg() {
  11035. this.type = 'point';
  11036. this.position = null;
  11037. this.offsetX = 0;
  11038. this.offsetY = 0;
  11039. this.style = {
  11040. fill: '#1890FF',
  11041. r: 3,
  11042. lineWidth: 1,
  11043. stroke: '#fff'
  11044. };
  11045. };
  11046. _proto.render = function render(coord, container) {
  11047. var position = this.parsePoint(coord, this.position);
  11048. if (!position) return null;
  11049. var shape = container.addShape('Circle', {
  11050. className: 'guide-point',
  11051. attrs: mix({
  11052. x: position.x + this.offsetX,
  11053. y: position.y + this.offsetY
  11054. }, this.style)
  11055. });
  11056. this.element = shape;
  11057. return shape;
  11058. };
  11059. return Point;
  11060. }(GuideBase);
  11061. GuideBase.Point = Point$2;
  11062. var MARKER_RADIUS = 3;
  11063. var List = /*#__PURE__*/function () {
  11064. var _proto = List.prototype;
  11065. _proto.getDefaultCfg = function getDefaultCfg() {
  11066. return {
  11067. showTitle: false,
  11068. /**
  11069. * title string
  11070. * @type {?String}
  11071. */
  11072. title: null,
  11073. /**
  11074. * items array
  11075. * @type {?Array}
  11076. */
  11077. items: null,
  11078. /**
  11079. * offset between title and items
  11080. * @type {Number}
  11081. */
  11082. titleGap: 12,
  11083. /**
  11084. * offset between each item
  11085. * @type {Number}
  11086. */
  11087. itemGap: 10,
  11088. /**
  11089. * the offset between each item in vertical direaction
  11090. * @type {Number}
  11091. */
  11092. itemMarginBottom: 12,
  11093. /**
  11094. * the formatter for item text
  11095. * @type {[type]}
  11096. */
  11097. itemFormatter: null,
  11098. itemWidth: null,
  11099. /**
  11100. * offset between marker and text
  11101. * @type {Number}
  11102. */
  11103. wordSpace: 6,
  11104. x: 0,
  11105. y: 0,
  11106. layout: 'horizontal',
  11107. /**
  11108. * the join string of `name` and `value`
  11109. * @type {String}
  11110. */
  11111. joinString: ': '
  11112. };
  11113. };
  11114. function List(cfg) {
  11115. deepMix(this, this.getDefaultCfg(), cfg);
  11116. this._init();
  11117. this._renderTitle();
  11118. this._renderItems();
  11119. }
  11120. _proto._init = function _init() {
  11121. var parent = this.parent;
  11122. if (!parent) return;
  11123. var container = parent.addGroup({
  11124. zIndex: this.zIndex || 0
  11125. });
  11126. this.container = container;
  11127. var wrapper = container.addGroup();
  11128. this.wrapper = wrapper;
  11129. var itemsGroup = wrapper.addGroup({
  11130. className: 'itemsGroup'
  11131. });
  11132. this.itemsGroup = itemsGroup;
  11133. };
  11134. _proto._renderTitle = function _renderTitle(title) {
  11135. title = title || this.title;
  11136. var titleShape = this.titleShape;
  11137. var titleHeight = 0;
  11138. if (this.showTitle && title) {
  11139. if (titleShape && !titleShape.get('destroyed')) {
  11140. titleShape.attr('text', title);
  11141. } else {
  11142. var wrapper = this.wrapper,
  11143. titleStyle = this.titleStyle;
  11144. titleShape = wrapper.addShape('text', {
  11145. className: 'title',
  11146. attrs: mix({
  11147. x: 0,
  11148. y: 0,
  11149. text: title
  11150. }, titleStyle)
  11151. });
  11152. this.titleShape = titleShape;
  11153. }
  11154. titleHeight = titleShape.getBBox().height + this.titleGap;
  11155. }
  11156. this._titleHeight = titleHeight;
  11157. };
  11158. _proto._renderItems = function _renderItems(items) {
  11159. var self = this;
  11160. items = items || self.items;
  11161. if (!items) {
  11162. return;
  11163. }
  11164. if (self.reversed) {
  11165. items.reverse();
  11166. }
  11167. each(items, function (item, index) {
  11168. self._addItem(item, index);
  11169. });
  11170. if (items.length > 1) {
  11171. this._adjustItems();
  11172. }
  11173. this._renderBackground();
  11174. };
  11175. _proto._renderBackground = function _renderBackground() {
  11176. var background = this.background;
  11177. if (background) {
  11178. var container = this.container;
  11179. var wrapper = this.wrapper;
  11180. var _wrapper$getBBox = wrapper.getBBox(),
  11181. minX = _wrapper$getBBox.minX,
  11182. minY = _wrapper$getBBox.minY,
  11183. width = _wrapper$getBBox.width,
  11184. height = _wrapper$getBBox.height;
  11185. var padding = background.padding || [0, 0, 0, 0];
  11186. padding = parsePadding(padding);
  11187. var attrs = mix({
  11188. x: minX - padding[3],
  11189. y: minY - padding[0],
  11190. width: width + padding[1] + padding[3],
  11191. height: height + padding[0] + padding[2]
  11192. }, background);
  11193. var backShape = this.backShape;
  11194. if (backShape) {
  11195. backShape.attr(attrs);
  11196. } else {
  11197. backShape = container.addShape('Rect', {
  11198. zIndex: -1,
  11199. attrs: attrs
  11200. });
  11201. }
  11202. this.backShape = backShape;
  11203. container.sort();
  11204. }
  11205. };
  11206. _proto._addItem = function _addItem(item) {
  11207. var itemsGroup = this.itemsGroup;
  11208. var itemGroup = itemsGroup.addGroup({
  11209. name: item.name,
  11210. value: item.value,
  11211. dataValue: item.dataValue,
  11212. checked: item.checked
  11213. });
  11214. var unCheckStyle = this.unCheckStyle,
  11215. unCheckColor = this.unCheckColor,
  11216. nameStyle = this.nameStyle,
  11217. valueStyle = this.valueStyle,
  11218. wordSpace = this.wordSpace;
  11219. var marker = item.marker,
  11220. value = item.value;
  11221. var startX = 0;
  11222. if (unCheckColor) {
  11223. unCheckStyle.fill = unCheckColor;
  11224. }
  11225. if (marker) {
  11226. var radius = marker.radius || MARKER_RADIUS;
  11227. var markerAttrs = mix({
  11228. x: radius,
  11229. y: this._titleHeight
  11230. }, marker);
  11231. if (item.checked === false) {
  11232. mix(markerAttrs, unCheckStyle);
  11233. }
  11234. var markerShape = itemGroup.addShape('marker', {
  11235. className: 'item-marker',
  11236. attrs: markerAttrs
  11237. });
  11238. startX += markerShape.getBBox().width + wordSpace;
  11239. }
  11240. var nameText;
  11241. var name = item.name;
  11242. if (name) {
  11243. var joinString = this.joinString || '';
  11244. name = value ? name + joinString : name;
  11245. nameText = itemGroup.addShape('text', {
  11246. className: 'name',
  11247. attrs: mix({
  11248. x: startX,
  11249. y: this._titleHeight,
  11250. text: this._formatItemValue(name)
  11251. }, nameStyle, item.checked === false ? unCheckStyle : null)
  11252. });
  11253. }
  11254. if (value) {
  11255. var valueX = startX;
  11256. if (nameText) {
  11257. valueX += nameText.getBBox().width;
  11258. }
  11259. itemGroup.addShape('text', {
  11260. className: 'value',
  11261. attrs: mix({
  11262. x: valueX,
  11263. y: this._titleHeight,
  11264. text: value
  11265. }, valueStyle, item.checked === false ? unCheckStyle : null)
  11266. });
  11267. }
  11268. return itemGroup;
  11269. };
  11270. _proto._formatItemValue = function _formatItemValue(value) {
  11271. var formatter = this.itemFormatter;
  11272. if (formatter) {
  11273. value = formatter.call(this, value);
  11274. }
  11275. return value;
  11276. };
  11277. _proto._getMaxItemWidth = function _getMaxItemWidth() {
  11278. var width;
  11279. var itemWidth = this.itemWidth;
  11280. if (isNumber(itemWidth) || isNil(itemWidth)) {
  11281. return itemWidth;
  11282. }
  11283. if (itemWidth === 'auto') {
  11284. var itemsGroup = this.itemsGroup;
  11285. var children = itemsGroup.get('children');
  11286. var count = children.length;
  11287. var maxItemWidth = 0;
  11288. for (var i = 0; i < count; i++) {
  11289. var _children$i$getBBox = children[i].getBBox(),
  11290. _width = _children$i$getBBox.width;
  11291. maxItemWidth = Math.max(maxItemWidth, _width);
  11292. }
  11293. var maxLength = this.maxLength;
  11294. var itemGap = this.itemGap;
  11295. var twoAvgWidth = (maxLength - itemGap) / 2;
  11296. var threeAvgWidth = (maxLength - itemGap * 2) / 3;
  11297. if (count === 2) {
  11298. width = Math.max(maxItemWidth, twoAvgWidth);
  11299. } else {
  11300. // 1. max <= 3Avg, 3Avg
  11301. // 2. 3Avg < max && max < 2avg, 2avg
  11302. // 3. max > 2avg, max, one column
  11303. if (maxItemWidth <= threeAvgWidth) {
  11304. width = threeAvgWidth;
  11305. } else if (maxItemWidth <= twoAvgWidth) {
  11306. width = twoAvgWidth;
  11307. } else {
  11308. width = maxItemWidth;
  11309. }
  11310. }
  11311. return width;
  11312. }
  11313. };
  11314. _proto._adjustHorizontal = function _adjustHorizontal() {
  11315. var maxLength = this.maxLength,
  11316. itemsGroup = this.itemsGroup;
  11317. var children = itemsGroup.get('children');
  11318. var itemGap = this.itemGap,
  11319. itemMarginBottom = this.itemMarginBottom;
  11320. var titleHeight = this._titleHeight;
  11321. var row = 0;
  11322. var rowWidth = 0;
  11323. var width;
  11324. var height;
  11325. var itemWidth = this._getMaxItemWidth();
  11326. var legendHitBoxes = [];
  11327. for (var i = 0, len = children.length; i < len; i++) {
  11328. var child = children[i];
  11329. var box = child.getBBox();
  11330. var childHeight = box.height;
  11331. var childWidth = box.width;
  11332. width = itemWidth || childWidth;
  11333. height = childHeight + itemMarginBottom;
  11334. if (width - (maxLength - rowWidth) > 0.0001) {
  11335. row++;
  11336. rowWidth = 0;
  11337. }
  11338. child.moveTo(rowWidth, row * height);
  11339. legendHitBoxes.push({
  11340. x: rowWidth,
  11341. y: row * height + titleHeight - childHeight / 2,
  11342. width: childWidth * 1.375,
  11343. height: childHeight * 1.375
  11344. });
  11345. rowWidth += width + itemGap;
  11346. }
  11347. this.legendHitBoxes = legendHitBoxes;
  11348. return;
  11349. };
  11350. _proto._adjustVertical = function _adjustVertical() {
  11351. var maxLength = this.maxLength,
  11352. itemsGroup = this.itemsGroup;
  11353. var itemGap = this.itemGap,
  11354. itemMarginBottom = this.itemMarginBottom,
  11355. itemWidth = this.itemWidth;
  11356. var titleHeight = this._titleHeight;
  11357. var children = itemsGroup.get('children');
  11358. var colHeight = 0;
  11359. var width;
  11360. var height;
  11361. var maxItemWidth = 0;
  11362. var totalWidth = 0;
  11363. var legendHitBoxes = [];
  11364. for (var i = 0, length = children.length; i < length; i++) {
  11365. var child = children[i];
  11366. var bbox = child.getBBox();
  11367. width = bbox.width;
  11368. height = bbox.height;
  11369. if (isNumber(itemWidth)) {
  11370. maxItemWidth = itemWidth + itemGap;
  11371. } else if (width > maxItemWidth) {
  11372. maxItemWidth = width + itemGap;
  11373. }
  11374. if (maxLength - colHeight < height) {
  11375. colHeight = 0;
  11376. totalWidth += maxItemWidth;
  11377. child.moveTo(totalWidth, 0);
  11378. legendHitBoxes.push({
  11379. x: totalWidth,
  11380. y: titleHeight - height / 2,
  11381. width: width * 1.375,
  11382. height: height * 1.375
  11383. });
  11384. } else {
  11385. child.moveTo(totalWidth, colHeight);
  11386. legendHitBoxes.push({
  11387. x: totalWidth,
  11388. y: colHeight - height / 2 + titleHeight,
  11389. width: width * 1.375,
  11390. height: height * 1.375
  11391. });
  11392. }
  11393. colHeight += height + itemMarginBottom;
  11394. }
  11395. this.legendHitBoxes = legendHitBoxes;
  11396. return;
  11397. };
  11398. _proto._adjustItems = function _adjustItems() {
  11399. var layout = this.layout;
  11400. if (layout === 'horizontal') {
  11401. this._adjustHorizontal();
  11402. } else {
  11403. this._adjustVertical();
  11404. }
  11405. };
  11406. _proto.moveTo = function moveTo(x, y) {
  11407. this.x = x;
  11408. this.y = y;
  11409. var container = this.container;
  11410. container && container.moveTo(x, y);
  11411. return this;
  11412. };
  11413. _proto.setItems = function setItems(items) {
  11414. this.clearItems();
  11415. this._renderItems(items);
  11416. };
  11417. _proto.setTitle = function setTitle(title) {
  11418. this._renderTitle(title);
  11419. };
  11420. _proto.clearItems = function clearItems() {
  11421. var itemsGroup = this.itemsGroup;
  11422. itemsGroup.clear();
  11423. };
  11424. _proto.getWidth = function getWidth() {
  11425. var container = this.container;
  11426. var bbox = container.getBBox();
  11427. return bbox.width;
  11428. };
  11429. _proto.getHeight = function getHeight() {
  11430. var container = this.container;
  11431. var bbox = container.getBBox();
  11432. return bbox.height;
  11433. };
  11434. _proto.show = function show() {
  11435. var container = this.container;
  11436. container.show();
  11437. };
  11438. _proto.hide = function hide() {
  11439. var container = this.container;
  11440. container.hide();
  11441. };
  11442. _proto.clear = function clear() {
  11443. var container = this.container;
  11444. container.clear();
  11445. container.remove(true);
  11446. };
  11447. return List;
  11448. }();
  11449. var TextBox = /*#__PURE__*/function () {
  11450. var _proto = TextBox.prototype;
  11451. _proto.getDefaultCfg = function getDefaultCfg() {
  11452. return {
  11453. x: 0,
  11454. y: 0,
  11455. content: '',
  11456. textStyle: {
  11457. fontSize: 12,
  11458. fill: '#fff',
  11459. textAlign: 'center',
  11460. textBaseline: 'middle',
  11461. fontFamily: 'Arial'
  11462. },
  11463. background: {
  11464. radius: 1,
  11465. fill: 'rgba(0, 0, 0, 0.65)',
  11466. padding: [3, 5]
  11467. },
  11468. width: 0,
  11469. height: 0,
  11470. className: ''
  11471. };
  11472. };
  11473. function TextBox(cfg) {
  11474. deepMix(this, this.getDefaultCfg(), cfg);
  11475. this._init();
  11476. var content = this.content,
  11477. x = this.x,
  11478. y = this.y;
  11479. if (!isNil(content)) {
  11480. this.updateContent(content);
  11481. }
  11482. this.updatePosition(x, y);
  11483. }
  11484. _proto._init = function _init() {
  11485. var content = this.content,
  11486. textStyle = this.textStyle,
  11487. background = this.background,
  11488. className = this.className,
  11489. visible = this.visible,
  11490. context = this.context;
  11491. var container = new Group({
  11492. context: context,
  11493. className: className,
  11494. zIndex: 0,
  11495. visible: visible
  11496. });
  11497. var text = container.addShape('Text', {
  11498. className: className + '-text',
  11499. zIndex: 1,
  11500. attrs: mix({
  11501. text: content,
  11502. x: 0,
  11503. y: 0
  11504. }, textStyle)
  11505. });
  11506. var backgroundShape = container.addShape('Rect', {
  11507. className: className + '-bg',
  11508. zIndex: -1,
  11509. attrs: mix({
  11510. x: 0,
  11511. y: 0,
  11512. width: 0,
  11513. height: 0
  11514. }, background)
  11515. });
  11516. container.sort();
  11517. this.container = container;
  11518. this.textShape = text;
  11519. this.backgroundShape = backgroundShape;
  11520. };
  11521. _proto._getBBox = function _getBBox() {
  11522. var textShape = this.textShape;
  11523. var background = this.background;
  11524. var textBBox = textShape.getBBox();
  11525. var padding = parsePadding(background.padding);
  11526. var width = textBBox.width + padding[1] + padding[3];
  11527. var height = textBBox.height + padding[0] + padding[2];
  11528. var x = textBBox.minX - padding[3];
  11529. var y = textBBox.minY - padding[0];
  11530. return {
  11531. x: x,
  11532. y: y,
  11533. width: width,
  11534. height: height
  11535. };
  11536. };
  11537. _proto.updateContent = function updateContent(text) {
  11538. var textShape = this.textShape,
  11539. backgroundShape = this.backgroundShape;
  11540. if (!isNil(text)) {
  11541. if (!isObject(text)) {
  11542. text = {
  11543. text: text
  11544. };
  11545. }
  11546. textShape.attr(text); // update box shape
  11547. var _this$_getBBox = this._getBBox(),
  11548. x = _this$_getBBox.x,
  11549. y = _this$_getBBox.y,
  11550. tipWidth = _this$_getBBox.width,
  11551. tipHeight = _this$_getBBox.height;
  11552. var width = this.width || tipWidth;
  11553. var height = this.height || tipHeight;
  11554. backgroundShape.attr({
  11555. x: x,
  11556. y: y,
  11557. width: width,
  11558. height: height
  11559. });
  11560. this._width = width;
  11561. this._height = height;
  11562. this.content = text.text;
  11563. }
  11564. };
  11565. _proto.updatePosition = function updatePosition(x, y) {
  11566. var container = this.container;
  11567. var _this$_getBBox2 = this._getBBox(),
  11568. xMin = _this$_getBBox2.x,
  11569. yMin = _this$_getBBox2.y;
  11570. container.moveTo(x - xMin, y - yMin);
  11571. this.x = x - xMin;
  11572. this.y = y - yMin;
  11573. };
  11574. _proto.getWidth = function getWidth() {
  11575. return this._width;
  11576. };
  11577. _proto.getHeight = function getHeight() {
  11578. return this._height;
  11579. };
  11580. _proto.show = function show() {
  11581. this.container.show();
  11582. };
  11583. _proto.hide = function hide() {
  11584. this.container.hide();
  11585. };
  11586. _proto.clear = function clear() {
  11587. var container = this.container;
  11588. container.clear();
  11589. container.remove(true);
  11590. this.container = null;
  11591. this.textShape = null;
  11592. this.backgroundShape = null;
  11593. };
  11594. return TextBox;
  11595. }();
  11596. var GAP = 4;
  11597. /**
  11598. * TODOList:
  11599. * 1. 移除 fixed 参数
  11600. */
  11601. var Tooltip = /*#__PURE__*/function () {
  11602. var _proto = Tooltip.prototype;
  11603. _proto.getDefaultCfg = function getDefaultCfg() {
  11604. return {
  11605. /**
  11606. * wether show the crosshairs
  11607. * @type {Object}
  11608. */
  11609. showCrosshairs: false,
  11610. /**
  11611. * the style for crosshairs
  11612. * @type {Object}
  11613. */
  11614. crosshairsStyle: {
  11615. stroke: 'rgba(0, 0, 0, 0.25)',
  11616. lineWidth: 1
  11617. },
  11618. /**
  11619. * the type of crosshairs, optional value is 'x', 'y' or 'xy', default is 'y'
  11620. */
  11621. crosshairsType: 'y',
  11622. /**
  11623. * show or hide the x axis tip
  11624. */
  11625. showXTip: false,
  11626. /**
  11627. * show or hide the y axis tip
  11628. */
  11629. showYTip: false,
  11630. xTip: null,
  11631. xTipBackground: {
  11632. radius: 1,
  11633. fill: 'rgba(0, 0, 0, 0.65)',
  11634. padding: [3, 5]
  11635. },
  11636. xTipTextStyle: {
  11637. fontSize: 12,
  11638. fill: '#fff',
  11639. textAlign: 'center',
  11640. textBaseline: 'middle'
  11641. },
  11642. yTip: null,
  11643. yTipBackground: {
  11644. radius: 1,
  11645. fill: 'rgba(0, 0, 0, 0.65)',
  11646. padding: [3, 5]
  11647. },
  11648. yTipTextStyle: {
  11649. fontSize: 12,
  11650. fill: '#fff',
  11651. textAlign: 'center',
  11652. textBaseline: 'middle'
  11653. },
  11654. /**
  11655. * the style for tooltip container's background
  11656. * @type {Object}
  11657. */
  11658. background: null,
  11659. /**
  11660. * layout, can be horizontal or vertical
  11661. * @type {String}
  11662. */
  11663. layout: 'horizontal',
  11664. offsetX: 0,
  11665. offsetY: 0
  11666. };
  11667. };
  11668. function Tooltip(cfg) {
  11669. deepMix(this, this.getDefaultCfg(), cfg);
  11670. var frontPlot = this.frontPlot,
  11671. custom = this.custom;
  11672. if (!custom) {
  11673. // custom means user do customize
  11674. var container = new List(mix({
  11675. parent: frontPlot,
  11676. zIndex: 3
  11677. }, cfg));
  11678. this.container = container;
  11679. var fixed = this.fixed,
  11680. background = this.background;
  11681. if (!fixed) {
  11682. this.tooltipArrow = frontPlot.addShape('Polygon', {
  11683. className: 'tooltip-arrow',
  11684. visible: false,
  11685. zIndex: 2,
  11686. attrs: mix({
  11687. points: []
  11688. }, background)
  11689. });
  11690. }
  11691. }
  11692. if (this.showXTip) {
  11693. var xTipBackground = this.xTipBackground,
  11694. xTipTextStyle = this.xTipTextStyle;
  11695. var xTipBox = new TextBox({
  11696. context: frontPlot.get('context'),
  11697. className: 'xTip',
  11698. background: xTipBackground,
  11699. textStyle: xTipTextStyle,
  11700. visible: false
  11701. });
  11702. frontPlot.add(xTipBox.container);
  11703. this.xTipBox = xTipBox;
  11704. }
  11705. if (this.showYTip) {
  11706. var yTipBackground = this.yTipBackground,
  11707. yTipTextStyle = this.yTipTextStyle;
  11708. var yTipBox = new TextBox({
  11709. context: frontPlot.get('context'),
  11710. className: 'yTip',
  11711. background: yTipBackground,
  11712. textStyle: yTipTextStyle,
  11713. visible: false
  11714. });
  11715. frontPlot.add(yTipBox.container);
  11716. this.yTipBox = yTipBox;
  11717. }
  11718. if (this.showCrosshairs) {
  11719. this._renderCrosshairs();
  11720. }
  11721. frontPlot.sort();
  11722. }
  11723. _proto.setContent = function setContent(title, items) {
  11724. this.title = title;
  11725. this.items = items;
  11726. if (!this.custom) {
  11727. var container = this.container;
  11728. container.setTitle(title);
  11729. container.setItems(items);
  11730. }
  11731. };
  11732. _proto.setYTipContent = function setYTipContent(val) {
  11733. var yTip = this.yTip;
  11734. if (isFunction(yTip)) {
  11735. val = yTip(val);
  11736. } else {
  11737. val = mix({
  11738. text: val
  11739. }, yTip);
  11740. }
  11741. this.yTipBox && this.yTipBox.updateContent(val);
  11742. };
  11743. _proto.setYTipPosition = function setYTipPosition(pos) {
  11744. var plotRange = this.plotRange;
  11745. var crosshairsShapeX = this.crosshairsShapeX;
  11746. if (this.showYTip) {
  11747. var yTipBox = this.yTipBox;
  11748. var yTipHeight = yTipBox.getHeight();
  11749. var yTipWidth = yTipBox.getWidth();
  11750. var posX = plotRange.tl.x - yTipWidth;
  11751. var posY = pos - yTipHeight / 2;
  11752. if (posY <= plotRange.tl.y) {
  11753. posY = plotRange.tl.y;
  11754. }
  11755. if (posY + yTipHeight >= plotRange.br.y) {
  11756. posY = plotRange.br.y - yTipHeight;
  11757. }
  11758. if (posX < 0) {
  11759. posX = plotRange.tl.x;
  11760. crosshairsShapeX && crosshairsShapeX.attr('x1', plotRange.tl.x + yTipWidth);
  11761. }
  11762. yTipBox.updatePosition(posX, posY);
  11763. }
  11764. };
  11765. _proto.setXTipContent = function setXTipContent(val) {
  11766. var xTip = this.xTip;
  11767. if (isFunction(xTip)) {
  11768. val = xTip(val);
  11769. } else {
  11770. val = mix({
  11771. text: val
  11772. }, xTip);
  11773. }
  11774. this.xTipBox && this.xTipBox.updateContent(val);
  11775. };
  11776. _proto.setXTipPosition = function setXTipPosition(pos) {
  11777. var showXTip = this.showXTip,
  11778. canvas = this.canvas,
  11779. plotRange = this.plotRange,
  11780. xTipBox = this.xTipBox,
  11781. crosshairsShapeY = this.crosshairsShapeY;
  11782. if (showXTip) {
  11783. // const el = canvas.get('el');
  11784. // const canvasHeight = Util.getHeight(el);
  11785. var canvasHeight = canvas.get('height');
  11786. var xTipWidth = xTipBox.getWidth();
  11787. var xTipHeight = xTipBox.getHeight();
  11788. var posX = pos - xTipWidth / 2;
  11789. var posY = plotRange.br.y;
  11790. if (posX <= plotRange.tl.x) {
  11791. posX = plotRange.tl.x;
  11792. }
  11793. if (posX + xTipWidth >= plotRange.tr.x) {
  11794. posX = plotRange.tr.x - xTipWidth;
  11795. }
  11796. if (canvasHeight - posY < xTipHeight) {
  11797. posY -= xTipHeight;
  11798. }
  11799. xTipBox.updatePosition(posX, posY);
  11800. crosshairsShapeY && crosshairsShapeY.attr('y1', posY);
  11801. }
  11802. };
  11803. _proto.setXCrosshairPosition = function setXCrosshairPosition(pos) {
  11804. this.crosshairsShapeX && this.crosshairsShapeX.moveTo(0, pos);
  11805. };
  11806. _proto.setYCrosshairPosition = function setYCrosshairPosition(pos) {
  11807. this.crosshairsShapeY && this.crosshairsShapeY.moveTo(pos, 0);
  11808. };
  11809. _proto.setPosition = function setPosition(items) {
  11810. var container = this.container,
  11811. plotRange = this.plotRange,
  11812. offsetX = this.offsetX,
  11813. offsetY = this.offsetY,
  11814. fixed = this.fixed,
  11815. tooltipArrow = this.tooltipArrow;
  11816. if (!container) {
  11817. return;
  11818. }
  11819. var containerBBox = container.container.getBBox();
  11820. var minX = containerBBox.minX,
  11821. minY = containerBBox.minY,
  11822. width = containerBBox.width,
  11823. height = containerBBox.height;
  11824. var tl = plotRange.tl,
  11825. tr = plotRange.tr;
  11826. var posX = 0;
  11827. var posY = tl.y - height - GAP + offsetY;
  11828. if (posY < 0) {
  11829. posY = 0;
  11830. }
  11831. if (fixed) {
  11832. var x = (tl.x + tr.x) / 2;
  11833. posX = x - width / 2 + offsetX;
  11834. } else {
  11835. var _x;
  11836. if (items.length > 1) {
  11837. _x = (items[0].x + items[items.length - 1].x) / 2;
  11838. } else {
  11839. _x = items[0].x;
  11840. }
  11841. posX = _x - width / 2 + offsetX;
  11842. if (posX < tl.x) {
  11843. posX = tl.x;
  11844. }
  11845. if (posX + width > tr.x) {
  11846. posX = tr.x - width;
  11847. }
  11848. if (tooltipArrow) {
  11849. var arrowY = posY + height;
  11850. tooltipArrow.attr('points', [{
  11851. x: _x - 3,
  11852. y: arrowY
  11853. }, {
  11854. x: _x + 3,
  11855. y: arrowY
  11856. }, {
  11857. x: _x,
  11858. y: arrowY + GAP
  11859. }]);
  11860. var backShape = container.backShape;
  11861. var radius = parsePadding(backShape.attr('radius'));
  11862. if (_x === tl.x) {
  11863. radius[3] = 0;
  11864. tooltipArrow.attr('points', [{
  11865. x: tl.x,
  11866. y: arrowY
  11867. }, {
  11868. x: tl.x + GAP,
  11869. y: arrowY
  11870. }, {
  11871. x: tl.x,
  11872. y: arrowY + GAP
  11873. }]);
  11874. } else if (_x === tr.x) {
  11875. radius[2] = 0;
  11876. tooltipArrow.attr('points', [{
  11877. x: tr.x - GAP,
  11878. y: arrowY
  11879. }, {
  11880. x: tr.x,
  11881. y: arrowY
  11882. }, {
  11883. x: tr.x,
  11884. y: arrowY + GAP
  11885. }]);
  11886. }
  11887. backShape.attr('radius', radius);
  11888. }
  11889. }
  11890. container.moveTo(posX - minX, posY - minY);
  11891. };
  11892. _proto.setMarkers = function setMarkers(cfg) {
  11893. if (cfg === void 0) {
  11894. cfg = {};
  11895. }
  11896. var self = this;
  11897. var _cfg = cfg,
  11898. items = _cfg.items,
  11899. style = _cfg.style,
  11900. type = _cfg.type;
  11901. var markerGroup = self._getMarkerGroup(type);
  11902. if (type === 'circle') {
  11903. for (var i = 0, length = items.length; i < length; i++) {
  11904. var item = items[i];
  11905. markerGroup.addShape('marker', {
  11906. className: 'tooltip-circle-marker',
  11907. attrs: mix({
  11908. x: item.x,
  11909. y: item.y,
  11910. stroke: item.color
  11911. }, style)
  11912. });
  11913. }
  11914. } else {
  11915. markerGroup.addShape('rect', {
  11916. className: 'tooltip-rect-marker',
  11917. attrs: style
  11918. });
  11919. }
  11920. };
  11921. _proto.clearMarkers = function clearMarkers() {
  11922. var markerGroup = this.markerGroup;
  11923. markerGroup && markerGroup.clear();
  11924. };
  11925. _proto.show = function show() {
  11926. var crosshairsShapeX = this.crosshairsShapeX;
  11927. var crosshairsShapeY = this.crosshairsShapeY;
  11928. var markerGroup = this.markerGroup;
  11929. var container = this.container;
  11930. var tooltipArrow = this.tooltipArrow;
  11931. var xTipBox = this.xTipBox;
  11932. var yTipBox = this.yTipBox;
  11933. var canvas = this.canvas;
  11934. crosshairsShapeX && crosshairsShapeX.show();
  11935. crosshairsShapeY && crosshairsShapeY.show();
  11936. markerGroup && markerGroup.show();
  11937. container && container.show();
  11938. tooltipArrow && tooltipArrow.show();
  11939. xTipBox && xTipBox.show();
  11940. yTipBox && yTipBox.show();
  11941. canvas.draw();
  11942. };
  11943. _proto.hide = function hide() {
  11944. var crosshairsShapeX = this.crosshairsShapeX;
  11945. var crosshairsShapeY = this.crosshairsShapeY;
  11946. var markerGroup = this.markerGroup;
  11947. var container = this.container;
  11948. var tooltipArrow = this.tooltipArrow;
  11949. var xTipBox = this.xTipBox;
  11950. var yTipBox = this.yTipBox;
  11951. crosshairsShapeX && crosshairsShapeX.hide();
  11952. crosshairsShapeY && crosshairsShapeY.hide();
  11953. markerGroup && markerGroup.hide();
  11954. container && container.hide();
  11955. tooltipArrow && tooltipArrow.hide();
  11956. xTipBox && xTipBox.hide();
  11957. yTipBox && yTipBox.hide();
  11958. };
  11959. _proto.destroy = function destroy() {
  11960. var crosshairsShapeX = this.crosshairsShapeX;
  11961. var crosshairsShapeY = this.crosshairsShapeY;
  11962. var markerGroup = this.markerGroup;
  11963. var container = this.container;
  11964. var tooltipArrow = this.tooltipArrow;
  11965. var xTipBox = this.xTipBox;
  11966. var yTipBox = this.yTipBox;
  11967. crosshairsShapeX && crosshairsShapeX.remove(true);
  11968. crosshairsShapeY && crosshairsShapeY.remove(true);
  11969. markerGroup && markerGroup.remove(true);
  11970. tooltipArrow && tooltipArrow.remove(true);
  11971. container && container.clear();
  11972. xTipBox && xTipBox.clear();
  11973. yTipBox && yTipBox.clear();
  11974. this.destroyed = true;
  11975. };
  11976. _proto._getMarkerGroup = function _getMarkerGroup(type) {
  11977. var markerGroup = this.markerGroup;
  11978. if (!markerGroup) {
  11979. if (type === 'circle') {
  11980. markerGroup = this.frontPlot.addGroup({
  11981. zIndex: 1
  11982. });
  11983. this.frontPlot.sort();
  11984. } else {
  11985. markerGroup = this.backPlot.addGroup();
  11986. }
  11987. this.markerGroup = markerGroup;
  11988. } else {
  11989. markerGroup.clear();
  11990. }
  11991. return markerGroup;
  11992. };
  11993. _proto._renderCrosshairs = function _renderCrosshairs() {
  11994. var crosshairsType = this.crosshairsType,
  11995. crosshairsStyle = this.crosshairsStyle,
  11996. frontPlot = this.frontPlot,
  11997. plotRange = this.plotRange;
  11998. var tl = plotRange.tl,
  11999. br = plotRange.br;
  12000. if (directionEnabled(crosshairsType, 'x')) {
  12001. this.crosshairsShapeX = frontPlot.addShape('Line', {
  12002. className: 'tooltip-crosshairs-x',
  12003. zIndex: 0,
  12004. visible: false,
  12005. attrs: mix({
  12006. x1: tl.x,
  12007. y1: 0,
  12008. x2: br.x,
  12009. y2: 0
  12010. }, crosshairsStyle)
  12011. });
  12012. }
  12013. if (directionEnabled(crosshairsType, 'y')) {
  12014. this.crosshairsShapeY = frontPlot.addShape('Line', {
  12015. className: 'tooltip-crosshairs-y',
  12016. zIndex: 0,
  12017. visible: false,
  12018. attrs: mix({
  12019. x1: 0,
  12020. y1: br.y,
  12021. x2: 0,
  12022. y2: tl.y
  12023. }, crosshairsStyle)
  12024. });
  12025. }
  12026. };
  12027. return Tooltip;
  12028. }();
  12029. Global.tooltip = deepMix({
  12030. triggerOn: 'press',
  12031. triggerOff: 'pressend',
  12032. alwaysShow: false,
  12033. showTitle: false,
  12034. showCrosshairs: false,
  12035. crosshairsStyle: {
  12036. stroke: 'rgba(0, 0, 0, 0.25)',
  12037. lineWidth: 1
  12038. },
  12039. showTooltipMarker: true,
  12040. background: {
  12041. radius: 1,
  12042. fill: 'rgba(0, 0, 0, 0.65)',
  12043. padding: [3, 5]
  12044. },
  12045. titleStyle: {
  12046. fontSize: 12,
  12047. fill: '#fff',
  12048. textAlign: 'start',
  12049. textBaseline: 'top'
  12050. },
  12051. nameStyle: {
  12052. fontSize: 12,
  12053. fill: 'rgba(255, 255, 255, 0.65)',
  12054. textAlign: 'start',
  12055. textBaseline: 'middle'
  12056. },
  12057. valueStyle: {
  12058. fontSize: 12,
  12059. fill: '#fff',
  12060. textAlign: 'start',
  12061. textBaseline: 'middle'
  12062. },
  12063. showItemMarker: true,
  12064. itemMarkerStyle: {
  12065. radius: 3,
  12066. symbol: 'circle',
  12067. lineWidth: 1,
  12068. stroke: '#fff'
  12069. },
  12070. layout: 'horizontal',
  12071. snap: false
  12072. }, Global.tooltip || {});
  12073. function _getTooltipValueScale(geom) {
  12074. var colorAttr = geom.getAttr('color');
  12075. if (colorAttr) {
  12076. var colorScale = colorAttr.getScale(colorAttr.type);
  12077. if (colorScale.isLinear) {
  12078. return colorScale;
  12079. }
  12080. }
  12081. var xScale = geom.getXScale();
  12082. var yScale = geom.getYScale();
  12083. if (yScale) {
  12084. return yScale;
  12085. }
  12086. return xScale;
  12087. }
  12088. function getTooltipName(geom, origin) {
  12089. var name;
  12090. var nameScale;
  12091. var groupScales = geom._getGroupScales();
  12092. if (groupScales.length) {
  12093. each(groupScales, function (scale) {
  12094. nameScale = scale;
  12095. return false;
  12096. });
  12097. }
  12098. if (nameScale) {
  12099. var field = nameScale.field;
  12100. name = nameScale.getText(origin[field]);
  12101. } else {
  12102. var valueScale = _getTooltipValueScale(geom);
  12103. name = valueScale.alias || valueScale.field;
  12104. }
  12105. return name;
  12106. }
  12107. function getTooltipValue(geom, origin) {
  12108. var scale = _getTooltipValueScale(geom);
  12109. return scale.getText(origin[scale.field]);
  12110. }
  12111. function getTooltipTitle(geom, origin) {
  12112. var position = geom.getAttr('position');
  12113. var field = position.getFields()[0];
  12114. var scale = geom.get('scales')[field];
  12115. return scale.getText(origin[scale.field]);
  12116. }
  12117. function _indexOfArray(items, item) {
  12118. var rst = -1;
  12119. each(items, function (sub, index) {
  12120. if (sub.title === item.title && sub.name === item.name && sub.value === item.value && sub.color === item.color) {
  12121. rst = index;
  12122. return false;
  12123. }
  12124. });
  12125. return rst;
  12126. }
  12127. function _uniqItems(items) {
  12128. var tmp = [];
  12129. each(items, function (item) {
  12130. var index = _indexOfArray(tmp, item);
  12131. if (index === -1) {
  12132. tmp.push(item);
  12133. } else {
  12134. tmp[index] = item;
  12135. }
  12136. });
  12137. return tmp;
  12138. }
  12139. function isEqual$1(arr1, arr2) {
  12140. return JSON.stringify(arr1) === JSON.stringify(arr2);
  12141. }
  12142. var TooltipController = /*#__PURE__*/function () {
  12143. function TooltipController(cfg) {
  12144. var _this = this;
  12145. _defineProperty(this, "handleShowEvent", function (ev) {
  12146. var chart = _this.chart;
  12147. if (!_this.enable) return;
  12148. var plot = chart.get('plotRange');
  12149. var point = createEvent(ev, chart);
  12150. if (!isPointInPlot(point, plot) && !_this._tooltipCfg.alwaysShow) {
  12151. // not in chart plot
  12152. _this.hideTooltip();
  12153. return;
  12154. }
  12155. var lastTimeStamp = _this.timeStamp;
  12156. var timeStamp = +new Date();
  12157. if (timeStamp - lastTimeStamp > 16) {
  12158. _this.showTooltip(point);
  12159. _this.timeStamp = timeStamp;
  12160. }
  12161. });
  12162. _defineProperty(this, "handleHideEvent", function () {
  12163. if (!_this.enable) return;
  12164. _this.hideTooltip();
  12165. });
  12166. this.enable = true;
  12167. this.cfg = {};
  12168. this.tooltip = null;
  12169. this.chart = null;
  12170. this.timeStamp = 0;
  12171. mix(this, cfg);
  12172. var _chart = this.chart;
  12173. var canvas = _chart.get('canvas');
  12174. this.canvas = canvas;
  12175. this.canvasDom = canvas.get('el');
  12176. }
  12177. var _proto = TooltipController.prototype;
  12178. _proto._setCrosshairsCfg = function _setCrosshairsCfg() {
  12179. var self = this;
  12180. var chart = self.chart;
  12181. var defaultCfg = mix({}, Global.tooltip);
  12182. var geoms = chart.get('geoms');
  12183. var shapes = [];
  12184. each(geoms, function (geom) {
  12185. var type = geom.get('type');
  12186. if (shapes.indexOf(type) === -1) {
  12187. shapes.push(type);
  12188. }
  12189. });
  12190. var coordType = chart.get('coord').type;
  12191. if (geoms.length && (coordType === 'cartesian' || coordType === 'rect')) {
  12192. if (shapes.length === 1 && ['line', 'area', 'path', 'point'].indexOf(shapes[0]) !== -1) {
  12193. mix(defaultCfg, {
  12194. showCrosshairs: true
  12195. });
  12196. }
  12197. }
  12198. return defaultCfg;
  12199. };
  12200. _proto._getMaxLength = function _getMaxLength(cfg) {
  12201. if (cfg === void 0) {
  12202. cfg = {};
  12203. }
  12204. var _cfg = cfg,
  12205. layout = _cfg.layout,
  12206. plotRange = _cfg.plotRange;
  12207. return layout === 'horizontal' ? plotRange.br.x - plotRange.bl.x : plotRange.bl.y - plotRange.tr.y;
  12208. };
  12209. _proto.render = function render() {
  12210. var self = this;
  12211. if (self.tooltip) {
  12212. return;
  12213. }
  12214. var chart = self.chart;
  12215. var canvas = chart.get('canvas');
  12216. var frontPlot = chart.get('frontPlot').addGroup({
  12217. className: 'tooltipContainer',
  12218. zIndex: 10
  12219. });
  12220. var backPlot = chart.get('backPlot').addGroup({
  12221. className: 'tooltipContainer'
  12222. });
  12223. var plotRange = chart.get('plotRange');
  12224. var coord = chart.get('coord');
  12225. var defaultCfg = self._setCrosshairsCfg();
  12226. var cfg = self.cfg; // 通过 chart.tooltip() 接口传入的 tooltip 配置项
  12227. var tooltipCfg = deepMix({
  12228. plotRange: plotRange,
  12229. frontPlot: frontPlot,
  12230. backPlot: backPlot,
  12231. canvas: canvas,
  12232. fixed: coord.transposed || coord.isPolar
  12233. }, defaultCfg, cfg); // 创建 tooltip 实例需要的配置,不应该修改 this.cfg,即用户传入的配置
  12234. tooltipCfg.maxLength = self._getMaxLength(tooltipCfg);
  12235. this._tooltipCfg = tooltipCfg;
  12236. var tooltip = new Tooltip(tooltipCfg);
  12237. self.tooltip = tooltip; // 需要保持tooltip一直显示
  12238. if (tooltipCfg.alwaysShow && self.prePoint) {
  12239. this.showTooltip(self.prePoint);
  12240. }
  12241. self.bindEvents();
  12242. };
  12243. _proto.clear = function clear() {
  12244. var tooltip = this.tooltip;
  12245. if (tooltip) {
  12246. tooltip.destroy();
  12247. this.unBindEvents();
  12248. }
  12249. this.tooltip = null;
  12250. this._lastActive = null;
  12251. };
  12252. _proto._getTooltipMarkerStyle = function _getTooltipMarkerStyle(cfg) {
  12253. if (cfg === void 0) {
  12254. cfg = {};
  12255. }
  12256. var _cfg2 = cfg,
  12257. type = _cfg2.type,
  12258. items = _cfg2.items;
  12259. var tooltipCfg = this._tooltipCfg;
  12260. if (type === 'rect') {
  12261. var x;
  12262. var y;
  12263. var width;
  12264. var height;
  12265. var chart = this.chart;
  12266. var _chart$get = chart.get('plotRange'),
  12267. tl = _chart$get.tl,
  12268. br = _chart$get.br;
  12269. var coord = chart.get('coord');
  12270. var firstItem = items[0];
  12271. var lastItem = items[items.length - 1];
  12272. var intervalWidth = firstItem.width;
  12273. if (coord.transposed) {
  12274. x = tl.x;
  12275. y = lastItem.y - intervalWidth * 0.75;
  12276. width = br.x - tl.x;
  12277. height = firstItem.y - lastItem.y + 1.5 * intervalWidth;
  12278. } else {
  12279. x = firstItem.x - intervalWidth * 0.75;
  12280. y = tl.y;
  12281. width = lastItem.x - firstItem.x + 1.5 * intervalWidth;
  12282. height = br.y - tl.y;
  12283. }
  12284. cfg.style = mix({
  12285. x: x,
  12286. y: y,
  12287. width: width,
  12288. height: height,
  12289. fill: '#CCD6EC',
  12290. opacity: 0.3
  12291. }, tooltipCfg.tooltipMarkerStyle);
  12292. } else {
  12293. cfg.style = mix({
  12294. radius: 4,
  12295. fill: '#fff',
  12296. lineWidth: 2
  12297. }, tooltipCfg.tooltipMarkerStyle);
  12298. }
  12299. return cfg;
  12300. };
  12301. _proto._setTooltip = function _setTooltip(point, items, tooltipMarkerCfg) {
  12302. if (tooltipMarkerCfg === void 0) {
  12303. tooltipMarkerCfg = {};
  12304. }
  12305. this.prePoint = point;
  12306. var lastActive = this._lastActive;
  12307. var tooltip = this.tooltip;
  12308. var cfg = this._tooltipCfg;
  12309. items = _uniqItems(items);
  12310. var chart = this.chart;
  12311. var coord = chart.get('coord');
  12312. var yScale = chart.getYScales()[0];
  12313. var snap = cfg.snap;
  12314. if (snap === false && yScale.isLinear) {
  12315. var invertPoint = coord.invertPoint(point);
  12316. var plot = chart.get('plotRange');
  12317. var tip;
  12318. var pos;
  12319. if (isPointInPlot(point, plot)) {
  12320. if (coord.transposed) {
  12321. tip = yScale.invert(invertPoint.x);
  12322. pos = point.x;
  12323. tooltip.setXTipContent(tip);
  12324. tooltip.setXTipPosition(pos);
  12325. tooltip.setYCrosshairPosition(pos);
  12326. } else {
  12327. tip = yScale.invert(invertPoint.y);
  12328. pos = point.y;
  12329. tooltip.setYTipContent(tip);
  12330. tooltip.setYTipPosition(pos);
  12331. tooltip.setXCrosshairPosition(pos);
  12332. }
  12333. }
  12334. }
  12335. if (cfg.onShow) {
  12336. cfg.onShow({
  12337. x: point.x,
  12338. y: point.y,
  12339. tooltip: tooltip,
  12340. items: items,
  12341. tooltipMarkerCfg: tooltipMarkerCfg
  12342. });
  12343. }
  12344. if (isEqual$1(lastActive, items)) {
  12345. if (snap === false && (directionEnabled(cfg.crosshairsType, 'y') || cfg.showYTip)) {
  12346. var canvas = this.chart.get('canvas');
  12347. canvas.draw();
  12348. }
  12349. return;
  12350. }
  12351. this._lastActive = items;
  12352. var onChange = cfg.onChange;
  12353. if (onChange) {
  12354. onChange({
  12355. x: point.x,
  12356. y: point.y,
  12357. tooltip: tooltip,
  12358. items: items,
  12359. tooltipMarkerCfg: tooltipMarkerCfg
  12360. });
  12361. }
  12362. var first = items[0];
  12363. var title = first.title || first.name;
  12364. var xTipPosX = first.x;
  12365. if (items.length > 1) {
  12366. xTipPosX = (items[0].x + items[items.length - 1].x) / 2;
  12367. }
  12368. tooltip.setContent(title, items, coord.transposed);
  12369. tooltip.setPosition(items, point);
  12370. if (coord.transposed) {
  12371. var yTipPosY = first.y;
  12372. if (items.length > 1) {
  12373. yTipPosY = (items[0].y + items[items.length - 1].y) / 2;
  12374. }
  12375. tooltip.setYTipContent(title);
  12376. tooltip.setYTipPosition(yTipPosY);
  12377. tooltip.setXCrosshairPosition(yTipPosY);
  12378. if (snap) {
  12379. tooltip.setXTipContent(first.value);
  12380. tooltip.setXTipPosition(xTipPosX);
  12381. tooltip.setYCrosshairPosition(xTipPosX);
  12382. }
  12383. } else {
  12384. tooltip.setXTipContent(title);
  12385. tooltip.setXTipPosition(xTipPosX);
  12386. tooltip.setYCrosshairPosition(xTipPosX);
  12387. if (snap) {
  12388. tooltip.setYTipContent(first.value);
  12389. tooltip.setYTipPosition(first.y);
  12390. tooltip.setXCrosshairPosition(first.y);
  12391. }
  12392. }
  12393. var markerItems = tooltipMarkerCfg.items;
  12394. if (cfg.showTooltipMarker && markerItems.length) {
  12395. tooltipMarkerCfg = this._getTooltipMarkerStyle(tooltipMarkerCfg);
  12396. tooltip.setMarkers(tooltipMarkerCfg);
  12397. } else {
  12398. tooltip.clearMarkers();
  12399. }
  12400. tooltip.show();
  12401. };
  12402. _proto.showTooltip = function showTooltip(point) {
  12403. var self = this;
  12404. var chart = self.chart;
  12405. var tooltipMarkerType;
  12406. var tooltipMarkerItems = [];
  12407. var items = [];
  12408. var cfg = self._tooltipCfg;
  12409. var showItemMarker = cfg.showItemMarker,
  12410. itemMarkerStyle = cfg.itemMarkerStyle,
  12411. alwaysShow = cfg.alwaysShow;
  12412. var marker;
  12413. if (showItemMarker) {
  12414. marker = itemMarkerStyle;
  12415. }
  12416. var geoms = chart.get('geoms');
  12417. var coord = chart.get('coord');
  12418. each(geoms, function (geom) {
  12419. if (geom.get('visible')) {
  12420. var type = geom.get('type');
  12421. var records = geom.getSnapRecords(point);
  12422. var adjust = geom.get('adjust'); // 漏斗图和金子塔图tooltip位置有问题,暂时不开放显示
  12423. if (type === 'interval' && adjust && adjust.type === 'symmetric') {
  12424. return;
  12425. }
  12426. each(records, function (record) {
  12427. var x = record.x,
  12428. y = record.y,
  12429. _origin = record._origin,
  12430. color = record.color;
  12431. if ((x || !isNaN(x)) && (y || !isNaN(y))) {
  12432. var tooltipItem = {
  12433. x: x,
  12434. y: isArray(y) ? y[1] : y,
  12435. color: color || Global.defaultColor,
  12436. origin: _origin,
  12437. name: getTooltipName(geom, _origin),
  12438. value: getTooltipValue(geom, _origin),
  12439. title: getTooltipTitle(geom, _origin)
  12440. };
  12441. if (marker) {
  12442. tooltipItem.marker = mix({
  12443. fill: color || Global.defaultColor
  12444. }, marker);
  12445. }
  12446. items.push(tooltipItem);
  12447. if (['line', 'area', 'path'].indexOf(type) !== -1) {
  12448. tooltipMarkerType = 'circle';
  12449. tooltipMarkerItems.push(tooltipItem);
  12450. } else if (type === 'interval' && (coord.type === 'cartesian' || coord.type === 'rect')) {
  12451. tooltipMarkerType = 'rect';
  12452. tooltipItem.width = geom.getSize(record._origin);
  12453. tooltipMarkerItems.push(tooltipItem);
  12454. }
  12455. }
  12456. });
  12457. }
  12458. });
  12459. if (items.length) {
  12460. var tooltipMarkerCfg = {
  12461. items: tooltipMarkerItems,
  12462. type: tooltipMarkerType
  12463. };
  12464. self._setTooltip(point, items, tooltipMarkerCfg);
  12465. return;
  12466. }
  12467. if (!alwaysShow) {
  12468. self.hideTooltip();
  12469. }
  12470. };
  12471. _proto.hideTooltip = function hideTooltip() {
  12472. var cfg = this._tooltipCfg;
  12473. this._lastActive = null;
  12474. var tooltip = this.tooltip;
  12475. if (tooltip) {
  12476. tooltip.hide();
  12477. if (cfg.onHide) {
  12478. cfg.onHide({
  12479. tooltip: tooltip
  12480. });
  12481. }
  12482. var canvas = this.chart.get('canvas');
  12483. canvas.draw();
  12484. }
  12485. };
  12486. _proto._handleEvent = function _handleEvent(methodName, method, action) {
  12487. var canvas = this.canvas;
  12488. each([].concat(methodName), function (aMethod) {
  12489. if (action === 'bind') {
  12490. canvas.on(aMethod, method);
  12491. } else {
  12492. canvas.off(aMethod, method);
  12493. }
  12494. });
  12495. };
  12496. _proto.bindEvents = function bindEvents() {
  12497. var cfg = this._tooltipCfg;
  12498. var triggerOn = cfg.triggerOn,
  12499. triggerOff = cfg.triggerOff,
  12500. alwaysShow = cfg.alwaysShow;
  12501. triggerOn && this._handleEvent(triggerOn, this.handleShowEvent, 'bind'); // 如果 !alwaysShow, 则在手势离开后就隐藏
  12502. if (!alwaysShow) {
  12503. this._handleEvent(triggerOff, this.handleHideEvent, 'bind');
  12504. }
  12505. };
  12506. _proto.unBindEvents = function unBindEvents() {
  12507. var cfg = this._tooltipCfg;
  12508. var triggerOn = cfg.triggerOn,
  12509. triggerOff = cfg.triggerOff,
  12510. alwaysShow = cfg.alwaysShow;
  12511. triggerOn && this._handleEvent(triggerOn, this.handleShowEvent, 'unBind');
  12512. if (!alwaysShow) {
  12513. this._handleEvent(triggerOff, this.handleHideEvent, 'unBind');
  12514. }
  12515. };
  12516. return TooltipController;
  12517. }();
  12518. function init(chart) {
  12519. var tooltipController = new TooltipController({
  12520. chart: chart
  12521. });
  12522. chart.set('tooltipController', tooltipController);
  12523. chart.tooltip = function (enable, cfg) {
  12524. if (isObject(enable)) {
  12525. cfg = enable;
  12526. enable = true;
  12527. }
  12528. tooltipController.enable = enable;
  12529. if (cfg) {
  12530. tooltipController.cfg = cfg;
  12531. }
  12532. return this;
  12533. };
  12534. }
  12535. function afterGeomDraw(chart) {
  12536. var tooltipController = chart.get('tooltipController');
  12537. tooltipController.render();
  12538. chart.showTooltip = function (point) {
  12539. tooltipController.showTooltip(point);
  12540. return this;
  12541. };
  12542. chart.hideTooltip = function () {
  12543. tooltipController.hideTooltip();
  12544. return this;
  12545. };
  12546. }
  12547. function clearInner(chart) {
  12548. var tooltipController = chart.get('tooltipController');
  12549. tooltipController.clear();
  12550. }
  12551. var tooltip = {
  12552. init: init,
  12553. afterGeomDraw: afterGeomDraw,
  12554. clearInner: clearInner
  12555. };
  12556. var Tooltip$1 = /*#__PURE__*/Object.freeze({
  12557. __proto__: null,
  12558. init: init,
  12559. afterGeomDraw: afterGeomDraw,
  12560. clearInner: clearInner,
  12561. 'default': tooltip
  12562. });
  12563. Global.guide = deepMix({
  12564. line: {
  12565. style: {
  12566. stroke: '#a3a3a3',
  12567. lineWidth: 1
  12568. },
  12569. top: true
  12570. },
  12571. text: {
  12572. style: {
  12573. fill: '#787878',
  12574. textAlign: 'center',
  12575. textBaseline: 'middle'
  12576. },
  12577. offsetX: 0,
  12578. offsetY: 0,
  12579. top: true
  12580. },
  12581. rect: {
  12582. style: {
  12583. fill: '#fafafa'
  12584. },
  12585. top: false
  12586. },
  12587. arc: {
  12588. style: {
  12589. stroke: '#a3a3a3'
  12590. },
  12591. top: true
  12592. },
  12593. html: {
  12594. offsetX: 0,
  12595. offsetY: 0,
  12596. alignX: 'center',
  12597. alignY: 'middle'
  12598. },
  12599. tag: {
  12600. top: true,
  12601. offsetX: 0,
  12602. offsetY: 0,
  12603. side: 4,
  12604. background: {
  12605. padding: 5,
  12606. radius: 2,
  12607. fill: '#1890FF'
  12608. },
  12609. textStyle: {
  12610. fontSize: 12,
  12611. fill: '#fff',
  12612. textAlign: 'center',
  12613. textBaseline: 'middle'
  12614. }
  12615. },
  12616. point: {
  12617. top: true,
  12618. offsetX: 0,
  12619. offsetY: 0,
  12620. style: {
  12621. fill: '#fff',
  12622. r: 3,
  12623. lineWidth: 2,
  12624. stroke: '#1890ff'
  12625. }
  12626. }
  12627. }, Global.guide || {});
  12628. var GuideController = /*#__PURE__*/function () {
  12629. function GuideController(cfg) {
  12630. this.guides = [];
  12631. this.xScale = null;
  12632. this.yScales = null;
  12633. this.guideShapes = [];
  12634. mix(this, cfg);
  12635. }
  12636. var _proto = GuideController.prototype;
  12637. _proto._toString = function _toString(position) {
  12638. if (isFunction(position)) {
  12639. position = position(this.xScale, this.yScales);
  12640. }
  12641. position = position.toString();
  12642. return position;
  12643. };
  12644. _proto._getId = function _getId(shape, guide) {
  12645. var id = guide.id;
  12646. if (!id) {
  12647. var type = guide.type;
  12648. if (type === 'arc' || type === 'line' || type === 'rect') {
  12649. id = this._toString(guide.start) + '-' + this._toString(guide.end);
  12650. } else {
  12651. id = this._toString(guide.position);
  12652. }
  12653. }
  12654. return id;
  12655. };
  12656. _proto.paint = function paint(coord) {
  12657. var self = this;
  12658. var chart = self.chart,
  12659. guides = self.guides,
  12660. xScale = self.xScale,
  12661. yScales = self.yScales;
  12662. var guideShapes = [];
  12663. each(guides, function (guide, idx) {
  12664. guide.xScale = xScale;
  12665. guide.yScales = yScales;
  12666. var container;
  12667. if (guide.type === 'regionFilter') {
  12668. // TODO: RegionFilter support animation
  12669. guide.chart = chart;
  12670. } else {
  12671. container = guide.top ? self.frontPlot : self.backPlot;
  12672. }
  12673. guide.coord = coord;
  12674. guide.container = container;
  12675. guide.canvas = chart.get('canvas');
  12676. var shape = guide.render(coord, container);
  12677. if (shape) {
  12678. var id = self._getId(shape, guide);
  12679. [].concat(shape).forEach(function (s) {
  12680. s._id = s.get('className') + '-' + id;
  12681. s.set('index', idx);
  12682. guideShapes.push(s);
  12683. });
  12684. }
  12685. });
  12686. self.guideShapes = guideShapes;
  12687. };
  12688. _proto.clear = function clear() {
  12689. this.reset();
  12690. this.guides = [];
  12691. return this;
  12692. };
  12693. _proto.reset = function reset() {
  12694. var guides = this.guides;
  12695. each(guides, function (guide) {
  12696. guide.remove();
  12697. });
  12698. };
  12699. _proto._createGuide = function _createGuide(type, cfg) {
  12700. var ClassName = upperFirst(type);
  12701. var guide = new GuideBase[ClassName](deepMix({}, Global.guide[type], cfg));
  12702. this.guides.push(guide);
  12703. return guide;
  12704. };
  12705. _proto.line = function line(cfg) {
  12706. if (cfg === void 0) {
  12707. cfg = {};
  12708. }
  12709. return this._createGuide('line', cfg);
  12710. };
  12711. _proto.text = function text(cfg) {
  12712. if (cfg === void 0) {
  12713. cfg = {};
  12714. }
  12715. return this._createGuide('text', cfg);
  12716. };
  12717. _proto.arc = function arc(cfg) {
  12718. if (cfg === void 0) {
  12719. cfg = {};
  12720. }
  12721. return this._createGuide('arc', cfg);
  12722. };
  12723. _proto.html = function html(cfg) {
  12724. if (cfg === void 0) {
  12725. cfg = {};
  12726. }
  12727. return this._createGuide('html', cfg);
  12728. };
  12729. _proto.rect = function rect(cfg) {
  12730. if (cfg === void 0) {
  12731. cfg = {};
  12732. }
  12733. return this._createGuide('rect', cfg);
  12734. };
  12735. _proto.tag = function tag(cfg) {
  12736. if (cfg === void 0) {
  12737. cfg = {};
  12738. }
  12739. return this._createGuide('tag', cfg);
  12740. };
  12741. _proto.point = function point(cfg) {
  12742. if (cfg === void 0) {
  12743. cfg = {};
  12744. }
  12745. return this._createGuide('point', cfg);
  12746. };
  12747. _proto.regionFilter = function regionFilter(cfg) {
  12748. if (cfg === void 0) {
  12749. cfg = {};
  12750. }
  12751. return this._createGuide('regionFilter', cfg);
  12752. };
  12753. return GuideController;
  12754. }();
  12755. function init$1(chart) {
  12756. var guideController = new GuideController({
  12757. frontPlot: chart.get('frontPlot').addGroup({
  12758. zIndex: 20,
  12759. className: 'guideContainer'
  12760. }),
  12761. backPlot: chart.get('backPlot').addGroup({
  12762. className: 'guideContainer'
  12763. })
  12764. });
  12765. chart.set('guideController', guideController);
  12766. /**
  12767. * 为图表添加 guide
  12768. * @return {GuideController} 返回 guide 控制器
  12769. */
  12770. chart.guide = function () {
  12771. return guideController;
  12772. };
  12773. }
  12774. function afterGeomDraw$1(chart) {
  12775. var guideController = chart.get('guideController');
  12776. if (!guideController.guides.length) {
  12777. return;
  12778. }
  12779. var xScale = chart.getXScale();
  12780. var yScales = chart.getYScales();
  12781. var coord = chart.get('coord');
  12782. guideController.xScale = xScale;
  12783. guideController.yScales = yScales;
  12784. guideController.chart = chart; // for regionFilter
  12785. guideController.paint(coord);
  12786. }
  12787. function clear(chart) {
  12788. chart.get('guideController').clear();
  12789. }
  12790. function repaint(chart) {
  12791. chart.get('guideController').reset();
  12792. }
  12793. var guide = {
  12794. init: init$1,
  12795. afterGeomDraw: afterGeomDraw$1,
  12796. clear: clear,
  12797. repaint: repaint
  12798. };
  12799. var Guide = /*#__PURE__*/Object.freeze({
  12800. __proto__: null,
  12801. init: init$1,
  12802. afterGeomDraw: afterGeomDraw$1,
  12803. clear: clear,
  12804. repaint: repaint,
  12805. 'default': guide
  12806. });
  12807. var LEGEND_GAP = 12;
  12808. var MARKER_SIZE = 3;
  12809. var DEFAULT_CFG = {
  12810. itemMarginBottom: 12,
  12811. itemGap: 10,
  12812. showTitle: false,
  12813. titleStyle: {
  12814. fontSize: 12,
  12815. fill: '#808080',
  12816. textAlign: 'start',
  12817. textBaseline: 'top'
  12818. },
  12819. nameStyle: {
  12820. fill: '#808080',
  12821. fontSize: 12,
  12822. textAlign: 'start',
  12823. textBaseline: 'middle'
  12824. },
  12825. valueStyle: {
  12826. fill: '#000000',
  12827. fontSize: 12,
  12828. textAlign: 'start',
  12829. textBaseline: 'middle'
  12830. },
  12831. unCheckStyle: {
  12832. fill: '#bfbfbf'
  12833. },
  12834. itemWidth: 'auto',
  12835. wordSpace: 6,
  12836. selectedMode: 'multiple' // 'multiple' or 'single'
  12837. }; // Register the default configuration for Legend
  12838. Global.legend = deepMix({
  12839. common: DEFAULT_CFG,
  12840. // common legend configuration
  12841. right: mix({
  12842. position: 'right',
  12843. layout: 'vertical'
  12844. }, DEFAULT_CFG),
  12845. left: mix({
  12846. position: 'left',
  12847. layout: 'vertical'
  12848. }, DEFAULT_CFG),
  12849. top: mix({
  12850. position: 'top',
  12851. layout: 'horizontal'
  12852. }, DEFAULT_CFG),
  12853. bottom: mix({
  12854. position: 'bottom',
  12855. layout: 'horizontal'
  12856. }, DEFAULT_CFG)
  12857. }, Global.legend || {});
  12858. function getPaddingByPos(pos, appendPadding) {
  12859. var padding = 0;
  12860. appendPadding = parsePadding(appendPadding);
  12861. switch (pos) {
  12862. case 'top':
  12863. padding = appendPadding[0];
  12864. break;
  12865. case 'right':
  12866. padding = appendPadding[1];
  12867. break;
  12868. case 'bottom':
  12869. padding = appendPadding[2];
  12870. break;
  12871. case 'left':
  12872. padding = appendPadding[3];
  12873. break;
  12874. }
  12875. return padding;
  12876. }
  12877. var LegendController = /*#__PURE__*/function () {
  12878. function LegendController(cfg) {
  12879. var _this = this;
  12880. _defineProperty(this, "handleEvent", function (ev) {
  12881. var self = _this;
  12882. function findItem(x, y) {
  12883. var result = null;
  12884. var legends = self.legends;
  12885. each(legends, function (legendItems) {
  12886. each(legendItems, function (legend) {
  12887. var itemsGroup = legend.itemsGroup,
  12888. legendHitBoxes = legend.legendHitBoxes;
  12889. var children = itemsGroup.get('children');
  12890. if (children.length) {
  12891. var legendPosX = legend.x;
  12892. var legendPosY = legend.y;
  12893. each(legendHitBoxes, function (box, index) {
  12894. if (x >= box.x + legendPosX && x <= box.x + box.width + legendPosX && y >= box.y + legendPosY && y <= box.height + box.y + legendPosY) {
  12895. // inbox
  12896. result = {
  12897. clickedItem: children[index],
  12898. clickedLegend: legend
  12899. };
  12900. return false;
  12901. }
  12902. });
  12903. }
  12904. });
  12905. });
  12906. return result;
  12907. }
  12908. var chart = self.chart;
  12909. var _createEvent = createEvent(ev, chart),
  12910. x = _createEvent.x,
  12911. y = _createEvent.y;
  12912. var clicked = findItem(x, y);
  12913. if (clicked && clicked.clickedLegend.clickable !== false) {
  12914. var clickedItem = clicked.clickedItem,
  12915. clickedLegend = clicked.clickedLegend;
  12916. if (clickedLegend.onClick) {
  12917. ev.clickedItem = clickedItem;
  12918. clickedLegend.onClick(ev);
  12919. } else if (!clickedLegend.custom) {
  12920. var checked = clickedItem.get('checked');
  12921. var value = clickedItem.get('dataValue');
  12922. var filteredVals = clickedLegend.filteredVals,
  12923. field = clickedLegend.field,
  12924. selectedMode = clickedLegend.selectedMode;
  12925. var isSingeSelected = selectedMode === 'single';
  12926. if (isSingeSelected) {
  12927. chart.filter(field, function (val) {
  12928. return val === value;
  12929. });
  12930. } else {
  12931. if (checked) {
  12932. filteredVals.push(value);
  12933. } else {
  12934. remove(filteredVals, value);
  12935. }
  12936. chart.filter(field, function (val) {
  12937. return filteredVals.indexOf(val) === -1;
  12938. });
  12939. }
  12940. chart.repaint();
  12941. }
  12942. }
  12943. });
  12944. this.legendCfg = {};
  12945. this.enable = true;
  12946. this.position = 'top';
  12947. mix(this, cfg);
  12948. var _chart = this.chart;
  12949. this.canvasDom = _chart.get('canvas').get('el');
  12950. this.clear();
  12951. }
  12952. var _proto = LegendController.prototype;
  12953. _proto.addLegend = function addLegend(scale, items, filteredVals) {
  12954. var self = this;
  12955. var legendCfg = self.legendCfg;
  12956. var field = scale.field;
  12957. var fieldCfg = legendCfg[field];
  12958. if (fieldCfg === false) {
  12959. return null;
  12960. }
  12961. if (fieldCfg && fieldCfg.custom) {
  12962. self.addCustomLegend(field);
  12963. } else {
  12964. var position = legendCfg.position || self.position;
  12965. if (fieldCfg && fieldCfg.position) {
  12966. position = fieldCfg.position;
  12967. }
  12968. if (scale.isCategory) {
  12969. self._addCategoryLegend(scale, items, position, filteredVals);
  12970. }
  12971. }
  12972. };
  12973. _proto.addCustomLegend = function addCustomLegend(field) {
  12974. var self = this;
  12975. var legendCfg = self.legendCfg;
  12976. if (field && legendCfg[field]) {
  12977. legendCfg = legendCfg[field];
  12978. }
  12979. var position = legendCfg.position || self.position;
  12980. var legends = self.legends;
  12981. legends[position] = legends[position] || [];
  12982. var items = legendCfg.items;
  12983. if (!items) {
  12984. return null;
  12985. }
  12986. var container = self.container;
  12987. each(items, function (item) {
  12988. if (!isPlainObject(item.marker)) {
  12989. item.marker = {
  12990. symbol: item.marker || 'circle',
  12991. fill: item.fill,
  12992. radius: MARKER_SIZE
  12993. };
  12994. } else {
  12995. item.marker.radius = item.marker.radius || MARKER_SIZE;
  12996. }
  12997. item.checked = isNil(item.checked) ? true : item.checked;
  12998. item.name = item.name || item.value;
  12999. });
  13000. var legend = new List(deepMix({}, Global.legend[position], legendCfg, {
  13001. maxLength: self._getMaxLength(position),
  13002. items: items,
  13003. parent: container
  13004. }));
  13005. legends[position].push(legend);
  13006. };
  13007. _proto.clear = function clear() {
  13008. var legends = this.legends;
  13009. each(legends, function (legendItems) {
  13010. each(legendItems, function (legend) {
  13011. legend.clear();
  13012. });
  13013. });
  13014. this.legends = {};
  13015. this.unBindEvents();
  13016. };
  13017. _proto._isFiltered = function _isFiltered(scale, values, value) {
  13018. var rst = false;
  13019. each(values, function (val) {
  13020. rst = rst || scale.getText(val) === scale.getText(value);
  13021. if (rst) {
  13022. return false;
  13023. }
  13024. });
  13025. return rst;
  13026. };
  13027. _proto._getMaxLength = function _getMaxLength(position) {
  13028. var chart = this.chart;
  13029. var appendPadding = parsePadding(chart.get('appendPadding'));
  13030. return position === 'right' || position === 'left' ? chart.get('height') - (appendPadding[0] + appendPadding[2]) : chart.get('width') - (appendPadding[1] + appendPadding[3]);
  13031. };
  13032. _proto._addCategoryLegend = function _addCategoryLegend(scale, items, position, filteredVals) {
  13033. var self = this;
  13034. var legendCfg = self.legendCfg,
  13035. legends = self.legends,
  13036. container = self.container,
  13037. chart = self.chart;
  13038. var field = scale.field;
  13039. legends[position] = legends[position] || [];
  13040. var symbol = 'circle';
  13041. if (legendCfg[field] && legendCfg[field].marker) {
  13042. symbol = legendCfg[field].marker;
  13043. } else if (legendCfg.marker) {
  13044. symbol = legendCfg.marker;
  13045. }
  13046. each(items, function (item) {
  13047. if (isPlainObject(symbol)) {
  13048. mix(item.marker, symbol);
  13049. } else {
  13050. item.marker.symbol = symbol;
  13051. }
  13052. if (filteredVals) {
  13053. item.checked = !self._isFiltered(scale, filteredVals, item.dataValue);
  13054. }
  13055. });
  13056. var legendItems = chart.get('legendItems');
  13057. legendItems[field] = items;
  13058. var lastCfg = deepMix({}, Global.legend[position], legendCfg[field] || legendCfg, {
  13059. maxLength: self._getMaxLength(position),
  13060. items: items,
  13061. field: field,
  13062. filteredVals: filteredVals,
  13063. parent: container
  13064. });
  13065. if (lastCfg.showTitle) {
  13066. deepMix(lastCfg, {
  13067. title: scale.alias || scale.field
  13068. });
  13069. }
  13070. var legend = new List(lastCfg);
  13071. legends[position].push(legend);
  13072. return legend;
  13073. };
  13074. _proto._alignLegend = function _alignLegend(legend, pre, position) {
  13075. var self = this;
  13076. var _self$plotRange = self.plotRange,
  13077. tl = _self$plotRange.tl,
  13078. bl = _self$plotRange.bl;
  13079. var chart = self.chart;
  13080. var offsetX = legend.offsetX || 0;
  13081. var offsetY = legend.offsetY || 0;
  13082. var chartWidth = chart.get('width');
  13083. var chartHeight = chart.get('height');
  13084. var appendPadding = parsePadding(chart.get('appendPadding'));
  13085. var legendHeight = legend.getHeight();
  13086. var legendWidth = legend.getWidth();
  13087. var x = 0;
  13088. var y = 0;
  13089. if (position === 'left' || position === 'right') {
  13090. var verticalAlign = legend.verticalAlign || 'middle';
  13091. var height = Math.abs(tl.y - bl.y);
  13092. x = position === 'left' ? appendPadding[3] : chartWidth - legendWidth - appendPadding[1];
  13093. y = (height - legendHeight) / 2 + tl.y;
  13094. if (verticalAlign === 'top') {
  13095. y = tl.y;
  13096. } else if (verticalAlign === 'bottom') {
  13097. y = bl.y - legendHeight;
  13098. }
  13099. if (pre) {
  13100. y = pre.get('y') - legendHeight - LEGEND_GAP;
  13101. }
  13102. } else {
  13103. var align = legend.align || 'left';
  13104. x = appendPadding[3];
  13105. if (align === 'center') {
  13106. x = chartWidth / 2 - legendWidth / 2;
  13107. } else if (align === 'right') {
  13108. x = chartWidth - (legendWidth + appendPadding[1]);
  13109. }
  13110. y = position === 'top' ? appendPadding[0] + Math.abs(legend.container.getBBox().minY) : chartHeight - legendHeight;
  13111. if (pre) {
  13112. var preWidth = pre.getWidth();
  13113. x = pre.x + preWidth + LEGEND_GAP;
  13114. }
  13115. }
  13116. if (position === 'bottom' && offsetY > 0) {
  13117. offsetY = 0;
  13118. }
  13119. if (position === 'right' && offsetX > 0) {
  13120. offsetX = 0;
  13121. }
  13122. legend.moveTo(x + offsetX, y + offsetY);
  13123. };
  13124. _proto.alignLegends = function alignLegends() {
  13125. var self = this;
  13126. var legends = self.legends;
  13127. each(legends, function (legendItems, position) {
  13128. each(legendItems, function (legend, index) {
  13129. var pre = legendItems[index - 1];
  13130. self._alignLegend(legend, pre, position);
  13131. });
  13132. });
  13133. return self;
  13134. };
  13135. _proto.bindEvents = function bindEvents() {
  13136. var legendCfg = this.legendCfg;
  13137. var triggerOn = legendCfg.triggerOn || 'touchstart';
  13138. addEventListener(this.canvasDom, triggerOn, this.handleEvent);
  13139. };
  13140. _proto.unBindEvents = function unBindEvents() {
  13141. var legendCfg = this.legendCfg;
  13142. var triggerOn = legendCfg.triggerOn || 'touchstart';
  13143. removeEventListener(this.canvasDom, triggerOn, this.handleEvent);
  13144. };
  13145. return LegendController;
  13146. }();
  13147. function init$2(chart) {
  13148. var legendController = new LegendController({
  13149. container: chart.get('backPlot').addGroup(),
  13150. plotRange: chart.get('plotRange'),
  13151. chart: chart
  13152. });
  13153. chart.set('legendController', legendController);
  13154. chart.legend = function (field, cfg) {
  13155. var legendCfg = legendController.legendCfg;
  13156. legendController.enable = true;
  13157. if (isBoolean(field)) {
  13158. legendController.enable = field;
  13159. legendCfg = cfg || {};
  13160. } else if (isObject(field)) {
  13161. legendCfg = field;
  13162. } else {
  13163. legendCfg[field] = cfg;
  13164. }
  13165. legendController.legendCfg = legendCfg;
  13166. return this;
  13167. };
  13168. }
  13169. function beforeGeomDraw(chart) {
  13170. var legendController = chart.get('legendController');
  13171. if (!legendController.enable) return null; // legend is not displayed
  13172. var legendCfg = legendController.legendCfg,
  13173. container = legendController.container;
  13174. if (legendCfg && legendCfg.custom) {
  13175. legendController.addCustomLegend();
  13176. } else {
  13177. var legendItems = chart.getLegendItems();
  13178. var scales = chart.get('scales');
  13179. var filters = chart.get('filters');
  13180. each(legendItems, function (items, field) {
  13181. var scale = scales[field];
  13182. var values = scale.values;
  13183. var filteredVals;
  13184. if (filters && filters[field]) {
  13185. filteredVals = values.filter(function (v) {
  13186. return !filters[field](v);
  13187. });
  13188. } else {
  13189. filteredVals = [];
  13190. }
  13191. legendController.addLegend(scale, items, filteredVals);
  13192. });
  13193. }
  13194. if (legendCfg && legendCfg.clickable !== false) {
  13195. legendController.bindEvents();
  13196. }
  13197. var legends = legendController.legends;
  13198. var legendRange = {
  13199. top: 0,
  13200. right: 0,
  13201. bottom: 0,
  13202. left: 0
  13203. };
  13204. each(legends, function (legendItems, position) {
  13205. var padding = 0;
  13206. each(legendItems, function (legend) {
  13207. var width = legend.getWidth();
  13208. var height = legend.getHeight();
  13209. if (position === 'top' || position === 'bottom') {
  13210. padding = Math.max(padding, height);
  13211. if (legend.offsetY > 0) {
  13212. padding += legend.offsetY;
  13213. }
  13214. } else {
  13215. padding = Math.max(padding, width);
  13216. if (legend.offsetX > 0) {
  13217. padding += legend.offsetX;
  13218. }
  13219. }
  13220. });
  13221. legendRange[position] = padding + getPaddingByPos(position, chart.get('appendPadding'));
  13222. });
  13223. chart.set('legendRange', legendRange);
  13224. if (Object.keys(legends).length) {
  13225. container.set('ariaLabel', lang.legend.prefix);
  13226. } else {
  13227. container.set('ariaLabel', null);
  13228. }
  13229. }
  13230. function afterGeomDraw$2(chart) {
  13231. var legendController = chart.get('legendController');
  13232. legendController.alignLegends();
  13233. }
  13234. function clearInner$1(chart) {
  13235. var legendController = chart.get('legendController');
  13236. legendController.clear();
  13237. chart.set('legendRange', null);
  13238. }
  13239. var legend = {
  13240. init: init$2,
  13241. beforeGeomDraw: beforeGeomDraw,
  13242. afterGeomDraw: afterGeomDraw$2,
  13243. clearInner: clearInner$1
  13244. };
  13245. var Legend = /*#__PURE__*/Object.freeze({
  13246. __proto__: null,
  13247. init: init$2,
  13248. beforeGeomDraw: beforeGeomDraw,
  13249. afterGeomDraw: afterGeomDraw$2,
  13250. clearInner: clearInner$1,
  13251. 'default': legend
  13252. });
  13253. var clock = typeof performance === 'object' && performance.now ? performance : Date;
  13254. var Timeline = /*#__PURE__*/function () {
  13255. function Timeline() {
  13256. this.anims = [];
  13257. this.time = null;
  13258. this.playing = false;
  13259. this.canvas = [];
  13260. }
  13261. var _proto = Timeline.prototype;
  13262. _proto.play = function play() {
  13263. var self = this;
  13264. self.time = clock.now();
  13265. self.playing = true;
  13266. function step() {
  13267. if (self.playing) {
  13268. requestAnimationFrame(step);
  13269. self.update();
  13270. }
  13271. }
  13272. requestAnimationFrame(step);
  13273. };
  13274. _proto.stop = function stop() {
  13275. this.playing = false;
  13276. this.time = null;
  13277. this.canvas = [];
  13278. };
  13279. _proto.pushAnim = function pushAnim(animInfo) {
  13280. // 先启动动画
  13281. if (!this.playing) {
  13282. this.play();
  13283. }
  13284. var delay = animInfo.delay,
  13285. duration = animInfo.duration;
  13286. var startTime = this.time + delay;
  13287. var endTime = startTime + duration;
  13288. animInfo.startTime = startTime;
  13289. animInfo.endTime = endTime;
  13290. this.anims.push(animInfo);
  13291. };
  13292. _proto.update = function update() {
  13293. var currentTime = clock.now();
  13294. this.canvas = [];
  13295. if (!this.anims.length) {
  13296. this.stop();
  13297. return;
  13298. }
  13299. for (var i = 0; i < this.anims.length; i++) {
  13300. var propertyAnim = this.anims[i];
  13301. if (currentTime < propertyAnim.startTime || propertyAnim.hasEnded) {
  13302. continue;
  13303. }
  13304. var shape = propertyAnim.shape; // shape
  13305. if (shape.get('destroyed')) {
  13306. this.anims.splice(i, 1);
  13307. i--;
  13308. continue;
  13309. }
  13310. var startState = propertyAnim.startState,
  13311. endState = propertyAnim.endState,
  13312. interpolate = propertyAnim.interpolate,
  13313. duration = propertyAnim.duration;
  13314. if (currentTime >= propertyAnim.startTime && !propertyAnim.hasStarted) {
  13315. propertyAnim.hasStarted = true;
  13316. if (propertyAnim.onStart) {
  13317. propertyAnim.onStart();
  13318. }
  13319. }
  13320. var t = (currentTime - propertyAnim.startTime) / duration;
  13321. t = Math.max(0, Math.min(t, 1));
  13322. t = propertyAnim.easing(t);
  13323. if (propertyAnim.onFrame) {
  13324. propertyAnim.onFrame(t);
  13325. } else {
  13326. for (var key in interpolate) {
  13327. var diff = interpolate[key];
  13328. var value = diff(t);
  13329. var newValue = void 0;
  13330. if (key === 'points') {
  13331. newValue = [];
  13332. var aLen = Math.max(startState.points.length, endState.points.length);
  13333. for (var j = 0; j < aLen; j += 2) {
  13334. newValue.push({
  13335. x: value[j],
  13336. y: value[j + 1]
  13337. });
  13338. }
  13339. } else {
  13340. newValue = value;
  13341. }
  13342. shape._attrs.attrs[key] = newValue;
  13343. shape._attrs.bbox = null; // should clear calculated bbox
  13344. }
  13345. }
  13346. var canvas = shape.get('canvas');
  13347. if (this.canvas.indexOf(canvas) === -1) {
  13348. this.canvas.push(canvas);
  13349. }
  13350. if (propertyAnim.onUpdate) {
  13351. propertyAnim.onUpdate(t);
  13352. }
  13353. if (currentTime >= propertyAnim.endTime && !propertyAnim.hasEnded) {
  13354. propertyAnim.hasEnded = true;
  13355. if (propertyAnim.onEnd) {
  13356. propertyAnim.onEnd();
  13357. }
  13358. }
  13359. if (t === 1) {
  13360. // end
  13361. this.anims.splice(i, 1);
  13362. i--;
  13363. }
  13364. }
  13365. this.canvas.map(function (c) {
  13366. c.draw();
  13367. return c;
  13368. });
  13369. this.time = clock.now();
  13370. };
  13371. return Timeline;
  13372. }();
  13373. function linear$1(k) {
  13374. return k;
  13375. }
  13376. function quadraticIn(k) {
  13377. return k * k;
  13378. }
  13379. function quadraticOut(k) {
  13380. return k * (2 - k);
  13381. }
  13382. function quadraticInOut(k) {
  13383. if ((k *= 2) < 1) {
  13384. return 0.5 * k * k;
  13385. }
  13386. return -0.5 * (--k * (k - 2) - 1);
  13387. }
  13388. function cubicIn(k) {
  13389. return k * k * k;
  13390. }
  13391. function cubicOut(k) {
  13392. return --k * k * k + 1;
  13393. }
  13394. function cubicInOut(k) {
  13395. if ((k *= 2) < 1) {
  13396. return 0.5 * k * k * k;
  13397. }
  13398. return 0.5 * ((k -= 2) * k * k + 2);
  13399. }
  13400. function elasticIn(k) {
  13401. var s;
  13402. var a = 0.1;
  13403. var p = 0.4;
  13404. if (k === 0) return 0;
  13405. if (k === 1) return 1;
  13406. if (!a || a < 1) {
  13407. a = 1;
  13408. s = p / 4;
  13409. } else {
  13410. s = p / (2 * Math.PI) * Math.asin(1 / a);
  13411. }
  13412. return -(a * Math.pow(2, 10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p));
  13413. }
  13414. function elasticOut(k) {
  13415. var s;
  13416. var a = 0.1;
  13417. var p = 0.4;
  13418. if (k === 0) return 0;
  13419. if (k === 1) return 1;
  13420. if (!a || a < 1) {
  13421. a = 1;
  13422. s = p / 4;
  13423. } else {
  13424. s = p / (2 * Math.PI) * Math.asin(1 / a);
  13425. }
  13426. return a * Math.pow(2, -10 * k) * Math.sin((k - s) * (2 * Math.PI) / p) + 1;
  13427. }
  13428. function elasticInOut(k) {
  13429. var s;
  13430. var a = 0.1;
  13431. var p = 0.4;
  13432. if (k === 0) return 0;
  13433. if (k === 1) return 1;
  13434. if (!a || a < 1) {
  13435. a = 1;
  13436. s = p / 4;
  13437. } else {
  13438. s = p / (2 * Math.PI) * Math.asin(1 / a);
  13439. }
  13440. if ((k *= 2) < 1) {
  13441. return -0.5 * (a * Math.pow(2, 10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p));
  13442. }
  13443. return a * Math.pow(2, -10 * (k -= 1)) * Math.sin((k - s) * (2 * Math.PI) / p) * 0.5 + 1;
  13444. }
  13445. function backIn(k) {
  13446. var s = 1.70158;
  13447. return k * k * ((s + 1) * k - s);
  13448. }
  13449. function backOut(k) {
  13450. var s = 1.70158;
  13451. return (k = k - 1) * k * ((s + 1) * k + s) + 1;
  13452. }
  13453. function backInOut(k) {
  13454. var s = 1.70158 * 1.525;
  13455. if ((k *= 2) < 1) {
  13456. return 0.5 * (k * k * ((s + 1) * k - s));
  13457. }
  13458. return 0.5 * ((k -= 2) * k * ((s + 1) * k + s) + 2);
  13459. }
  13460. function bounceIn(k) {
  13461. return 1 - bounceOut(1 - k);
  13462. }
  13463. function bounceOut(k) {
  13464. if ((k /= 1) < 1 / 2.75) {
  13465. return 7.5625 * k * k;
  13466. } else if (k < 2 / 2.75) {
  13467. return 7.5625 * (k -= 1.5 / 2.75) * k + 0.75;
  13468. } else if (k < 2.5 / 2.75) {
  13469. return 7.5625 * (k -= 2.25 / 2.75) * k + 0.9375;
  13470. }
  13471. return 7.5625 * (k -= 2.625 / 2.75) * k + 0.984375;
  13472. }
  13473. function bounceInOut(k) {
  13474. if (k < 0.5) {
  13475. return bounceIn(k * 2) * 0.5;
  13476. }
  13477. return bounceOut(k * 2 - 1) * 0.5 + 0.5;
  13478. }
  13479. var Easing = /*#__PURE__*/Object.freeze({
  13480. __proto__: null,
  13481. linear: linear$1,
  13482. quadraticIn: quadraticIn,
  13483. quadraticOut: quadraticOut,
  13484. quadraticInOut: quadraticInOut,
  13485. cubicIn: cubicIn,
  13486. cubicOut: cubicOut,
  13487. cubicInOut: cubicInOut,
  13488. elasticIn: elasticIn,
  13489. elasticOut: elasticOut,
  13490. elasticInOut: elasticInOut,
  13491. backIn: backIn,
  13492. backOut: backOut,
  13493. backInOut: backInOut,
  13494. bounceIn: bounceIn,
  13495. bounceOut: bounceOut,
  13496. bounceInOut: bounceInOut
  13497. });
  13498. function plainArray(arr) {
  13499. var result = [];
  13500. for (var i = 0, len = arr.length; i < len; i++) {
  13501. if (arr[i]) {
  13502. result.push(arr[i].x);
  13503. result.push(arr[i].y);
  13504. }
  13505. }
  13506. return result;
  13507. }
  13508. function interpolateNumber(a, b) {
  13509. a = +a;
  13510. b -= a;
  13511. return function (t) {
  13512. return a + b * t;
  13513. };
  13514. }
  13515. function interpolateArray(a, b) {
  13516. var nb = b ? b.length : 0;
  13517. var na = a ? Math.min(nb, a.length) : 0;
  13518. var x = new Array(na);
  13519. var c = new Array(nb);
  13520. var i;
  13521. for (i = 0; i < na; ++i) {
  13522. x[i] = interpolateNumber(a[i], b[i]);
  13523. }
  13524. for (; i < nb; ++i) {
  13525. c[i] = b[i];
  13526. }
  13527. return function (t) {
  13528. for (i = 0; i < na; ++i) {
  13529. c[i] = x[i](t);
  13530. }
  13531. return c;
  13532. };
  13533. }
  13534. var Animator = /*#__PURE__*/function () {
  13535. function Animator(shape, source, timeline) {
  13536. this.hasStarted = false;
  13537. this.hasEnded = false;
  13538. this.shape = shape;
  13539. this.source = source;
  13540. this.timeline = timeline;
  13541. this.animate = null;
  13542. } // delay, attrs, duration, easing
  13543. var _proto = Animator.prototype;
  13544. _proto.to = function to(cfg) {
  13545. if (cfg === void 0) {
  13546. cfg = {};
  13547. }
  13548. var delay = cfg.delay || 0;
  13549. var attrs = cfg.attrs || {};
  13550. var duration = cfg.duration || 1000;
  13551. var easing; // 缓动函数
  13552. if (typeof cfg.easing === 'function') {
  13553. easing = cfg.easing;
  13554. } else {
  13555. easing = Easing[cfg.easing] || linear$1;
  13556. }
  13557. var animInfo = {
  13558. shape: this.shape,
  13559. delay: delay,
  13560. duration: duration,
  13561. easing: easing
  13562. };
  13563. var interpolate = {}; // 差值函数
  13564. for (var attrName in attrs) {
  13565. var startValue = this.source[attrName];
  13566. var endValue = attrs[attrName];
  13567. if (attrName === 'points') {
  13568. startValue = plainArray(startValue);
  13569. endValue = plainArray(endValue);
  13570. interpolate.points = interpolateArray(startValue, endValue);
  13571. this.source.points = startValue;
  13572. attrs.points = endValue;
  13573. } else if (attrName === 'matrix') {
  13574. interpolate.matrix = interpolateArray(startValue, endValue);
  13575. } else {
  13576. interpolate[attrName] = interpolateNumber(startValue, endValue);
  13577. }
  13578. }
  13579. animInfo.interpolate = interpolate;
  13580. animInfo.startState = this.source;
  13581. animInfo.endState = attrs;
  13582. this.timeline.pushAnim(animInfo);
  13583. this.animate = animInfo;
  13584. return this;
  13585. };
  13586. _proto.onFrame = function onFrame(callback) {
  13587. // 自定义每一帧动画的动作
  13588. if (this.animate) {
  13589. this.animate.onFrame = function (frame) {
  13590. callback(frame);
  13591. };
  13592. }
  13593. return this;
  13594. };
  13595. _proto.onStart = function onStart(callback) {
  13596. if (this.animate) {
  13597. this.animate.onStart = function () {
  13598. callback();
  13599. };
  13600. }
  13601. return this;
  13602. };
  13603. _proto.onUpdate = function onUpdate(callback) {
  13604. if (this.animate) {
  13605. this.animate.onUpdate = function (frame) {
  13606. callback(frame);
  13607. };
  13608. }
  13609. return this;
  13610. };
  13611. _proto.onEnd = function onEnd(callback) {
  13612. if (this.animate) {
  13613. this.animate.onEnd = function () {
  13614. callback();
  13615. };
  13616. }
  13617. return this;
  13618. };
  13619. return Animator;
  13620. }();
  13621. var defaultAnimationCfg = {
  13622. appear: {
  13623. duration: 450,
  13624. easing: 'quadraticOut'
  13625. },
  13626. // 'appear' animation options
  13627. update: {
  13628. duration: 300,
  13629. easing: 'quadraticOut'
  13630. },
  13631. // 'update' animation options
  13632. enter: {
  13633. duration: 300,
  13634. easing: 'quadraticOut'
  13635. },
  13636. // 'enter' animation options
  13637. leave: {
  13638. duration: 350,
  13639. easing: 'quadraticIn'
  13640. } // 'leave' animation options
  13641. };
  13642. var Animate = {
  13643. defaultCfg: {},
  13644. Action: {},
  13645. getAnimation: function getAnimation(geomType, coord, animationType) {
  13646. var geomAnimateCfg = this.defaultCfg[geomType];
  13647. if (geomAnimateCfg) {
  13648. var animation = geomAnimateCfg[animationType];
  13649. if (isFunction(animation)) {
  13650. return animation(coord);
  13651. }
  13652. }
  13653. return false;
  13654. },
  13655. getAnimateCfg: function getAnimateCfg(geomType, animationType) {
  13656. var defaultCfg = defaultAnimationCfg[animationType];
  13657. var geomConfig = this.defaultCfg[geomType];
  13658. if (geomConfig && geomConfig.cfg && geomConfig.cfg[animationType]) {
  13659. return deepMix({}, defaultCfg, geomConfig.cfg[animationType]);
  13660. }
  13661. return defaultCfg;
  13662. },
  13663. registerAnimation: function registerAnimation(animationName, animationFun) {
  13664. var _extends2;
  13665. if (!this.Action) {
  13666. this.Action = {};
  13667. }
  13668. this.Action = _extends({}, this.Action, (_extends2 = {}, _extends2[animationName] = animationFun, _extends2));
  13669. }
  13670. };
  13671. /**
  13672. * Utility
  13673. * @author sima.zhang1990@gmail.com
  13674. */
  13675. function getCoordInfo(coord) {
  13676. var start = coord.start;
  13677. var end = coord.end;
  13678. return {
  13679. start: start,
  13680. end: end,
  13681. width: end.x - start.x,
  13682. height: Math.abs(end.y - start.y)
  13683. };
  13684. }
  13685. function getScaledMatrix(shape, v, direct) {
  13686. var scaledMatrix;
  13687. shape.apply(v);
  13688. var x = v[0];
  13689. var y = v[1];
  13690. if (direct === 'x') {
  13691. shape.transform([['t', x, y], ['s', 0.01, 1], ['t', -x, -y]]);
  13692. var matrix = shape.getMatrix();
  13693. scaledMatrix = Matrix.transform(matrix, [['t', x, y], ['s', 100, 1], ['t', -x, -y]]);
  13694. } else if (direct === 'y') {
  13695. shape.transform([['t', x, y], ['s', 1, 0.01], ['t', -x, -y]]);
  13696. var _matrix = shape.getMatrix();
  13697. scaledMatrix = Matrix.transform(_matrix, [['t', x, y], ['s', 1, 100], ['t', -x, -y]]);
  13698. } else if (direct === 'xy') {
  13699. shape.transform([['t', x, y], ['s', 0.01, 0.01], ['t', -x, -y]]);
  13700. var _matrix2 = shape.getMatrix();
  13701. scaledMatrix = Matrix.transform(_matrix2, [['t', x, y], ['s', 100, 100], ['t', -x, -y]]);
  13702. }
  13703. return scaledMatrix;
  13704. }
  13705. function getAnimateParam(animateCfg, index, id) {
  13706. var result = {};
  13707. if (animateCfg.delay) {
  13708. result.delay = isFunction(animateCfg.delay) ? animateCfg.delay(index, id) : animateCfg.delay;
  13709. }
  13710. result.easing = animateCfg.easing;
  13711. result.duration = animateCfg.duration;
  13712. result.delay = animateCfg.delay;
  13713. return result;
  13714. }
  13715. function doAnimation(shape, endState, animateCfg, callback) {
  13716. var id = shape._id;
  13717. var index = shape.get('index');
  13718. var _getAnimateParam = getAnimateParam(animateCfg, index, id),
  13719. easing = _getAnimateParam.easing,
  13720. delay = _getAnimateParam.delay,
  13721. duration = _getAnimateParam.duration;
  13722. var anim = shape.animate().to({
  13723. attrs: endState,
  13724. duration: duration,
  13725. delay: delay,
  13726. easing: easing
  13727. });
  13728. if (callback) {
  13729. anim.onEnd(function () {
  13730. callback();
  13731. });
  13732. }
  13733. }
  13734. /**
  13735. * Animation functions for shape
  13736. * @author sima.zhang1990@gmail.com
  13737. */
  13738. /*
  13739. function waveIn(shape, animateCfg, coord) {
  13740. const clip = Helpers.getClip(coord);
  13741. clip.set('canvas', shape.get('canvas'));
  13742. shape.attr('clip', clip);
  13743. const onEnd = function() {
  13744. shape.attr('clip', null);
  13745. clip.remove(true);
  13746. };
  13747. Helpers.doAnimation(clip, clip.endState, animateCfg, onEnd);
  13748. }
  13749. function scaleInX(shape, animateCfg) {
  13750. const box = shape.getBBox();
  13751. const points = shape.get('origin').points;
  13752. let x;
  13753. const y = (box.minY + box.maxY) / 2;
  13754. if (points[0].y - points[1].y > 0) { // 当顶点在零点之下
  13755. x = box.maxX;
  13756. } else {
  13757. x = box.minX;
  13758. }
  13759. const scaledMatrix = Helpers.getScaledMatrix(shape, [ x, y ], 'x');
  13760. Helpers.doAnimation(shape, { matrix: scaledMatrix }, animateCfg);
  13761. }
  13762. function scaleInY(shape, animateCfg) {
  13763. const box = shape.getBBox();
  13764. const points = shape.get('origin').points;
  13765. const x = (box.minX + box.maxX) / 2;
  13766. let y;
  13767. if (points[0].y - points[1].y <= 0) { // 当顶点在零点之下
  13768. y = box.maxY;
  13769. } else {
  13770. y = box.minY;
  13771. }
  13772. const scaledMatrix = Helpers.getScaledMatrix(shape, [ x, y ], 'x');
  13773. Helpers.doAnimation(shape, { matrix: scaledMatrix }, animateCfg);
  13774. }
  13775. */
  13776. function fadeIn(shape, animateCfg) {
  13777. var fillOpacity = isNil(shape.attr('fillOpacity')) ? 1 : shape.attr('fillOpacity');
  13778. var strokeOpacity = isNil(shape.attr('strokeOpacity')) ? 1 : shape.attr('strokeOpacity');
  13779. shape.attr('fillOpacity', 0);
  13780. shape.attr('strokeOpacity', 0);
  13781. var endState = {
  13782. fillOpacity: fillOpacity,
  13783. strokeOpacity: strokeOpacity
  13784. };
  13785. doAnimation(shape, endState, animateCfg);
  13786. }
  13787. var ShapeAction = /*#__PURE__*/Object.freeze({
  13788. __proto__: null,
  13789. fadeIn: fadeIn
  13790. });
  13791. /**
  13792. * Group animate functions
  13793. * @author sima.zhang1990@gmail.com
  13794. */
  13795. function _groupScaleIn(container, animateCfg, coord, zeroY, type) {
  13796. var _getCoordInfo = getCoordInfo(coord),
  13797. start = _getCoordInfo.start,
  13798. end = _getCoordInfo.end,
  13799. width = _getCoordInfo.width,
  13800. height = _getCoordInfo.height;
  13801. var x;
  13802. var y;
  13803. var clip = new Shape$2.Rect({
  13804. attrs: {
  13805. x: start.x,
  13806. y: end.y,
  13807. width: width,
  13808. height: height
  13809. }
  13810. });
  13811. if (type === 'y') {
  13812. x = start.x + width / 2;
  13813. y = zeroY.y < start.y ? zeroY.y : start.y;
  13814. } else if (type === 'x') {
  13815. x = zeroY.x > start.x ? zeroY.x : start.x;
  13816. y = start.y + height / 2;
  13817. } else if (type === 'xy') {
  13818. if (coord.isPolar) {
  13819. x = coord.center.x;
  13820. y = coord.center.y;
  13821. } else {
  13822. x = (start.x + end.x) / 2;
  13823. y = (start.y + end.y) / 2;
  13824. }
  13825. }
  13826. var endMatrix = getScaledMatrix(clip, [x, y], type);
  13827. clip.isClip = true;
  13828. clip.endState = {
  13829. matrix: endMatrix
  13830. };
  13831. clip.set('canvas', container.get('canvas'));
  13832. container.attr('clip', clip);
  13833. var onEnd = function onEnd() {
  13834. container.attr('clip', null);
  13835. clip.remove(true);
  13836. };
  13837. doAnimation(clip, clip.endState, animateCfg, onEnd);
  13838. }
  13839. function _shapeScale(container, animateCfg, type) {
  13840. var shapes = container.get('children');
  13841. var x;
  13842. var y;
  13843. var endMatrix;
  13844. for (var i = 0, len = shapes.length; i < len; i++) {
  13845. var shape = shapes[i];
  13846. var box = shape.getBBox();
  13847. x = (box.minX + box.maxX) / 2;
  13848. y = (box.minY + box.maxY) / 2;
  13849. endMatrix = getScaledMatrix(shape, [x, y], type);
  13850. doAnimation(shape, {
  13851. matrix: endMatrix
  13852. }, animateCfg);
  13853. }
  13854. }
  13855. function groupScaleInX(container, animateCfg, coord, zeroY) {
  13856. _groupScaleIn(container, animateCfg, coord, zeroY, 'x');
  13857. }
  13858. function groupScaleInY(container, animateCfg, coord, zeroY) {
  13859. _groupScaleIn(container, animateCfg, coord, zeroY, 'y');
  13860. }
  13861. function groupScaleInXY(container, animateCfg, coord, zeroY) {
  13862. _groupScaleIn(container, animateCfg, coord, zeroY, 'xy');
  13863. }
  13864. function shapesScaleInX(container, animateCfg) {
  13865. _shapeScale(container, animateCfg, 'x');
  13866. }
  13867. function shapesScaleInY(container, animateCfg) {
  13868. _shapeScale(container, animateCfg, 'y');
  13869. }
  13870. function shapesScaleInXY(container, animateCfg) {
  13871. _shapeScale(container, animateCfg, 'xy');
  13872. }
  13873. function groupWaveIn(container, animateCfg, coord) {
  13874. var clip = getClip(coord);
  13875. clip.set('canvas', container.get('canvas'));
  13876. container.attr('clip', clip);
  13877. var onEnd = function onEnd() {
  13878. container.attr('clip', null);
  13879. clip.remove(true);
  13880. };
  13881. var endState = {};
  13882. if (coord.isPolar) {
  13883. var startAngle = coord.startAngle,
  13884. endAngle = coord.endAngle;
  13885. endState.endAngle = endAngle;
  13886. clip.attr('endAngle', startAngle);
  13887. } else {
  13888. var start = coord.start,
  13889. end = coord.end;
  13890. var width = Math.abs(start.x - end.x);
  13891. var height = Math.abs(start.y - end.y);
  13892. if (coord.isTransposed) {
  13893. clip.attr('height', 0);
  13894. endState.height = height;
  13895. } else {
  13896. clip.attr('width', 0);
  13897. endState.width = width;
  13898. }
  13899. }
  13900. doAnimation(clip, endState, animateCfg, onEnd);
  13901. }
  13902. var GroupAction = /*#__PURE__*/Object.freeze({
  13903. __proto__: null,
  13904. groupWaveIn: groupWaveIn,
  13905. groupScaleInX: groupScaleInX,
  13906. groupScaleInY: groupScaleInY,
  13907. groupScaleInXY: groupScaleInXY,
  13908. shapesScaleInX: shapesScaleInX,
  13909. shapesScaleInY: shapesScaleInY,
  13910. shapesScaleInXY: shapesScaleInXY
  13911. });
  13912. /**
  13913. * Handle the detail animations
  13914. * @author sima.zhang1990@gmail.com
  13915. */
  13916. var timeline;
  13917. Element.prototype.animate = function () {
  13918. var attrs = mix({}, this.get('attrs'));
  13919. return new Animator(this, attrs, timeline);
  13920. };
  13921. Chart.prototype.animate = function (cfg) {
  13922. this.set('animate', cfg);
  13923. return this;
  13924. };
  13925. Animate.Action = ShapeAction;
  13926. Animate.defaultCfg = {
  13927. interval: {
  13928. enter: function enter(coord) {
  13929. if (coord.isPolar && coord.transposed) {
  13930. // for pie chart
  13931. return function (shape) {
  13932. shape.set('zIndex', -1);
  13933. var container = shape.get('parent');
  13934. container.sort();
  13935. };
  13936. }
  13937. return fadeIn;
  13938. }
  13939. },
  13940. area: {
  13941. enter: function enter(coord) {
  13942. if (coord.isPolar) return null;
  13943. return fadeIn;
  13944. }
  13945. },
  13946. line: {
  13947. enter: function enter(coord) {
  13948. if (coord.isPolar) return null;
  13949. return fadeIn;
  13950. }
  13951. },
  13952. path: {
  13953. enter: function enter(coord) {
  13954. if (coord.isPolar) return null;
  13955. return fadeIn;
  13956. }
  13957. }
  13958. };
  13959. var GROUP_ANIMATION = {
  13960. line: function line(coord) {
  13961. if (coord.isPolar) {
  13962. return groupScaleInXY;
  13963. }
  13964. return groupWaveIn;
  13965. },
  13966. area: function area(coord) {
  13967. if (coord.isPolar) {
  13968. return groupScaleInXY;
  13969. }
  13970. return groupWaveIn;
  13971. },
  13972. path: function path(coord) {
  13973. if (coord.isPolar) {
  13974. return groupScaleInXY;
  13975. }
  13976. return groupWaveIn;
  13977. },
  13978. point: function point() {
  13979. return shapesScaleInXY;
  13980. },
  13981. interval: function interval(coord) {
  13982. var result;
  13983. if (coord.isPolar) {
  13984. // polar coodinate
  13985. result = groupScaleInXY;
  13986. if (coord.transposed) {
  13987. // pie chart
  13988. result = groupWaveIn;
  13989. }
  13990. } else {
  13991. result = coord.transposed ? groupScaleInX : groupScaleInY;
  13992. }
  13993. return result;
  13994. },
  13995. schema: function schema() {
  13996. return groupWaveIn;
  13997. }
  13998. };
  13999. function diff(fromAttrs, toAttrs) {
  14000. var endState = {};
  14001. for (var k in toAttrs) {
  14002. if (isNumber(fromAttrs[k]) && fromAttrs[k] !== toAttrs[k]) {
  14003. endState[k] = toAttrs[k];
  14004. } else if (isArray(fromAttrs[k]) && JSON.stringify(fromAttrs[k]) !== JSON.stringify(toAttrs[k])) {
  14005. endState[k] = toAttrs[k];
  14006. }
  14007. }
  14008. return endState;
  14009. } // Add a unique id identifier to each shape
  14010. function _getShapeId(geom, dataObj, geomIdx) {
  14011. var type = geom.get('type');
  14012. var id = 'geom' + geomIdx + '-' + type;
  14013. var xScale = geom.getXScale();
  14014. var yScale = geom.getYScale();
  14015. var xField = xScale.field || 'x';
  14016. var yField = yScale.field || 'y';
  14017. var yVal = dataObj[yField];
  14018. var xVal;
  14019. if (xScale.isIdentity) {
  14020. xVal = xScale.value;
  14021. } else {
  14022. xVal = dataObj[xField];
  14023. }
  14024. if (type === 'interval' || type === 'schema') {
  14025. id += '-' + xVal;
  14026. } else if (type === 'line' || type === 'area' || type === 'path') {
  14027. id += '-' + type;
  14028. } else {
  14029. id += xScale.isCategory ? '-' + xVal : '-' + xVal + '-' + yVal;
  14030. }
  14031. var groupScales = geom._getGroupScales();
  14032. each(groupScales, function (groupScale) {
  14033. var field = groupScale.field;
  14034. if (groupScale.type !== 'identity') {
  14035. id += '-' + dataObj[field];
  14036. }
  14037. });
  14038. return id;
  14039. } // get geometry's shapes
  14040. function getShapes(geoms, chart, coord) {
  14041. var shapes = [];
  14042. each(geoms, function (geom, geomIdx) {
  14043. var geomContainer = geom.get('container');
  14044. var geomShapes = geomContainer.get('children');
  14045. var type = geom.get('type');
  14046. var animateCfg = isNil(geom.get('animateCfg')) ? _getAnimateCfgByShapeType(type, chart) : geom.get('animateCfg');
  14047. if (animateCfg !== false) {
  14048. each(geomShapes, function (shape, index) {
  14049. if (shape.get('className') === type) {
  14050. shape._id = _getShapeId(geom, shape.get('origin')._origin, geomIdx);
  14051. shape.set('coord', coord);
  14052. shape.set('animateCfg', animateCfg);
  14053. shape.set('index', index);
  14054. shapes.push(shape);
  14055. }
  14056. });
  14057. }
  14058. geom.set('shapes', geomShapes);
  14059. });
  14060. return shapes;
  14061. }
  14062. function cache(shapes) {
  14063. var rst = {};
  14064. for (var i = 0, len = shapes.length; i < len; i++) {
  14065. var shape = shapes[i];
  14066. if (!shape._id || shape.isClip) continue;
  14067. var id = shape._id;
  14068. rst[id] = {
  14069. _id: id,
  14070. type: shape.get('type'),
  14071. // the type of shape
  14072. attrs: mix({}, shape._attrs.attrs),
  14073. // the graphics attributes of shape
  14074. className: shape.get('className'),
  14075. geomType: shape.get('className'),
  14076. index: shape.get('index'),
  14077. coord: shape.get('coord'),
  14078. animateCfg: shape.get('animateCfg')
  14079. };
  14080. }
  14081. return rst;
  14082. }
  14083. function getAnimate(geomType, coord, animationType, animationName) {
  14084. var result;
  14085. if (isFunction(animationName)) {
  14086. result = animationName;
  14087. } else if (isString(animationName)) {
  14088. result = Animate.Action[animationName];
  14089. } else {
  14090. result = Animate.getAnimation(geomType, coord, animationType);
  14091. }
  14092. return result;
  14093. }
  14094. function getAnimateCfg(geomType, animationType, animateCfg) {
  14095. if (animateCfg === false || isObject(animateCfg) && animateCfg[animationType] === false) {
  14096. return false;
  14097. }
  14098. var defaultCfg = Animate.getAnimateCfg(geomType, animationType);
  14099. if (animateCfg && animateCfg[animationType]) {
  14100. return deepMix({}, defaultCfg, animateCfg[animationType]);
  14101. }
  14102. return defaultCfg;
  14103. }
  14104. function addAnimate(cache, shapes, canvas) {
  14105. var animate;
  14106. var animateCfg; // the order of animation: leave -> update -> enter
  14107. var updateShapes = [];
  14108. var newShapes = [];
  14109. each(shapes, function (shape) {
  14110. var result = cache[shape._id];
  14111. if (!result) {
  14112. newShapes.push(shape);
  14113. } else {
  14114. shape.set('cacheShape', result);
  14115. updateShapes.push(shape);
  14116. delete cache[shape._id];
  14117. }
  14118. }); // first do the leave animation
  14119. each(cache, function (deletedShape) {
  14120. var className = deletedShape.className,
  14121. coord = deletedShape.coord,
  14122. _id = deletedShape._id,
  14123. attrs = deletedShape.attrs,
  14124. index = deletedShape.index,
  14125. type = deletedShape.type;
  14126. animateCfg = getAnimateCfg(className, 'leave', deletedShape.animateCfg);
  14127. if (animateCfg === false) return true;
  14128. animate = getAnimate(className, coord, 'leave', animateCfg.animation);
  14129. if (isFunction(animate)) {
  14130. var tempShape = canvas.addShape(type, {
  14131. attrs: attrs,
  14132. index: index,
  14133. canvas: canvas,
  14134. className: className
  14135. });
  14136. tempShape._id = _id;
  14137. animate(tempShape, animateCfg, coord);
  14138. }
  14139. }); // then do the update animation
  14140. each(updateShapes, function (updateShape) {
  14141. var className = updateShape.get('className');
  14142. animateCfg = getAnimateCfg(className, 'update', updateShape.get('animateCfg'));
  14143. if (animateCfg === false) return true;
  14144. var coord = updateShape.get('coord');
  14145. var cacheAttrs = updateShape.get('cacheShape').attrs;
  14146. var endState = diff(cacheAttrs, updateShape._attrs.attrs); // 判断如果属性相同的话就不进行变换
  14147. if (Object.keys(endState).length) {
  14148. animate = getAnimate(className, coord, 'update', animateCfg.animation);
  14149. if (isFunction(animate)) {
  14150. animate(updateShape, animateCfg, coord);
  14151. } else {
  14152. var startState = {};
  14153. each(endState, function (value, key) {
  14154. startState[key] = cacheAttrs[key];
  14155. });
  14156. updateShape.attr(startState);
  14157. updateShape.animate().to({
  14158. attrs: endState,
  14159. duration: animateCfg.duration,
  14160. easing: animateCfg.easing,
  14161. delay: animateCfg.delay
  14162. }).onEnd(function () {
  14163. updateShape.set('cacheShape', null);
  14164. });
  14165. }
  14166. }
  14167. }); // last, enter animation
  14168. each(newShapes, function (newShape) {
  14169. // 新图形元素的进场元素
  14170. var className = newShape.get('className');
  14171. var coord = newShape.get('coord');
  14172. animateCfg = getAnimateCfg(className, 'enter', newShape.get('animateCfg'));
  14173. if (animateCfg === false) return true;
  14174. animate = getAnimate(className, coord, 'enter', animateCfg.animation);
  14175. if (isFunction(animate)) {
  14176. if (className === 'interval' && coord.isPolar && coord.transposed) {
  14177. var index = newShape.get('index');
  14178. var lastShape = updateShapes[index - 1];
  14179. animate(newShape, animateCfg, lastShape);
  14180. } else {
  14181. animate(newShape, animateCfg, coord);
  14182. }
  14183. }
  14184. });
  14185. }
  14186. function _getAnimateCfgByShapeType(type, chart) {
  14187. if (!type) {
  14188. return null;
  14189. }
  14190. var animateCfg = chart.get('animate');
  14191. if (type.indexOf('guide-tag') > -1) {
  14192. type = 'guide-tag';
  14193. }
  14194. if (isObject(animateCfg)) {
  14195. return animateCfg[type];
  14196. }
  14197. if (animateCfg === false) {
  14198. return false;
  14199. }
  14200. return null;
  14201. }
  14202. function afterCanvasInit()
  14203. /* chart */
  14204. {
  14205. timeline = new Timeline();
  14206. timeline.play();
  14207. }
  14208. function beforeCanvasDraw(chart) {
  14209. if (chart.get('animate') === false) {
  14210. return;
  14211. }
  14212. var isUpdate = chart.get('isUpdate');
  14213. var canvas = chart.get('canvas');
  14214. var coord = chart.get('coord');
  14215. var geoms = chart.get('geoms');
  14216. var caches = canvas.get('caches') || [];
  14217. if (caches.length === 0) {
  14218. isUpdate = false;
  14219. }
  14220. var cacheShapes = getShapes(geoms, chart, coord);
  14221. var _chart$get = chart.get('axisController'),
  14222. frontPlot = _chart$get.frontPlot,
  14223. backPlot = _chart$get.backPlot;
  14224. var axisShapes = frontPlot.get('children').concat(backPlot.get('children'));
  14225. var guideShapes = [];
  14226. if (chart.get('guideController')) {
  14227. guideShapes = chart.get('guideController').guideShapes;
  14228. }
  14229. var componentShapes = [];
  14230. axisShapes.concat(guideShapes).forEach(function (s) {
  14231. var className = s.get('className');
  14232. var animateCfg = _getAnimateCfgByShapeType(className, chart);
  14233. s.set('coord', coord);
  14234. s.set('animateCfg', animateCfg);
  14235. componentShapes.push(s);
  14236. cacheShapes.push(s);
  14237. });
  14238. canvas.set('caches', cache(cacheShapes));
  14239. if (isUpdate) {
  14240. addAnimate(caches, cacheShapes, canvas);
  14241. } else {
  14242. // do the appear animation
  14243. var animateCfg;
  14244. var animate;
  14245. each(geoms, function (geom) {
  14246. var type = geom.get('type');
  14247. var geomCfg = isNil(geom.get('animateCfg')) ? _getAnimateCfgByShapeType(type, chart) : geom.get('animateCfg');
  14248. if (geomCfg !== false) {
  14249. animateCfg = getAnimateCfg(type, 'appear', geomCfg);
  14250. animate = getAnimate(type, coord, 'appear', animateCfg.animation);
  14251. if (isFunction(animate)) {
  14252. var shapes = geom.get('shapes');
  14253. each(shapes, function (shape) {
  14254. animate(shape, animateCfg, coord);
  14255. });
  14256. } else if (GROUP_ANIMATION[type]) {
  14257. // do the default animation
  14258. animate = GroupAction[animateCfg.animation] || GROUP_ANIMATION[type](coord);
  14259. var yScale = geom.getYScale();
  14260. var zeroY = coord.convertPoint({
  14261. x: 0,
  14262. y: yScale.scale(geom.getYMinValue())
  14263. });
  14264. var container = geom.get('container');
  14265. animate && animate(container, animateCfg, coord, zeroY);
  14266. }
  14267. }
  14268. }); // do the animation of components
  14269. each(componentShapes, function (shape) {
  14270. var animateCfg = shape.get('animateCfg');
  14271. var className = shape.get('className');
  14272. if (animateCfg && animateCfg.appear) {
  14273. // if user configure
  14274. var defaultCfg = Animate.getAnimateCfg(className, 'appear');
  14275. var appearCfg = deepMix({}, defaultCfg, animateCfg.appear);
  14276. var _animate = getAnimate(className, coord, 'appear', appearCfg.animation);
  14277. if (isFunction(_animate)) {
  14278. _animate(shape, appearCfg, coord);
  14279. }
  14280. }
  14281. });
  14282. }
  14283. }
  14284. function afterCanvasDestroyed()
  14285. /* chart */
  14286. {
  14287. timeline.stop();
  14288. }
  14289. var detail = {
  14290. afterCanvasInit: afterCanvasInit,
  14291. beforeCanvasDraw: beforeCanvasDraw,
  14292. afterCanvasDestroyed: afterCanvasDestroyed
  14293. };
  14294. var Animation = /*#__PURE__*/Object.freeze({
  14295. __proto__: null,
  14296. afterCanvasInit: afterCanvasInit,
  14297. beforeCanvasDraw: beforeCanvasDraw,
  14298. afterCanvasDestroyed: afterCanvasDestroyed,
  14299. 'default': detail
  14300. });
  14301. Chart._Interactions = {};
  14302. Chart.registerInteraction = function (type, constructor) {
  14303. Chart._Interactions[type] = constructor;
  14304. };
  14305. Chart.getInteraction = function (type) {
  14306. return Chart._Interactions[type];
  14307. };
  14308. Chart.prototype.interaction = function (type, cfg) {
  14309. var interactions = this._interactions || {};
  14310. if (interactions[type]) {
  14311. // if reprated, destroy last
  14312. interactions[type].destroy();
  14313. }
  14314. var Ctor = Chart.getInteraction(type);
  14315. var interact = new Ctor(cfg, this);
  14316. interactions[type] = interact;
  14317. this._interactions = interactions;
  14318. return this;
  14319. };
  14320. Chart.prototype.clearInteraction = function (type) {
  14321. var interactions = this._interactions;
  14322. if (!interactions) return;
  14323. if (type) {
  14324. interactions[type] && interactions[type].destroy();
  14325. delete interactions[type];
  14326. } else {
  14327. each(interactions, function (interaction, key) {
  14328. interaction.destroy();
  14329. delete interactions[key];
  14330. });
  14331. }
  14332. return this;
  14333. };
  14334. function isValuesEqual(values, newValues) {
  14335. if (values.length !== newValues.length) {
  14336. return false;
  14337. }
  14338. var lastIndex = values.length - 1;
  14339. return values[0] === newValues[0] && values[lastIndex] === newValues[lastIndex];
  14340. } // 不同交互之间共享的上下文
  14341. var defaultRange = [0, 1];
  14342. var Context = /*#__PURE__*/function () {
  14343. // 最开始的原始值
  14344. // 当前显示的范围
  14345. // 缩放最小的点数
  14346. // 最小的缩放比例, 默认通过minCount计算
  14347. // minScale = 0.01;
  14348. // 交互开始时,ticks个数,主要为了每次缩放后,更新ticks个数
  14349. // lastTickCount;
  14350. function Context(chart) {
  14351. var _this = this;
  14352. _defineProperty(this, "chart", null);
  14353. _defineProperty(this, "values", null);
  14354. _defineProperty(this, "range", defaultRange);
  14355. _defineProperty(this, "startRange", defaultRange);
  14356. _defineProperty(this, "minCount", 10);
  14357. _defineProperty(this, "_afterinit", function () {
  14358. // 初始化value值
  14359. var scale = _this.getPinchScale(); // 记录原始全量数据
  14360. var values = [].concat(scale.values);
  14361. _this.values = values; // 最小的缩放比例
  14362. if (!_this.minScale) {
  14363. _this.minScale = _this.minCount / values.length;
  14364. } // 初始化的时候有设置range,则初始化成默认比例
  14365. if (_this.range !== defaultRange) {
  14366. _this.updateRange(_this.range);
  14367. _this.updateTicks();
  14368. }
  14369. });
  14370. _defineProperty(this, "_afterdatachange", function () {
  14371. _this.updateRange(_this.range);
  14372. });
  14373. this.chart = chart;
  14374. this._initEvent(chart);
  14375. }
  14376. var _proto = Context.prototype;
  14377. _proto._initEvent = function _initEvent(chart) {
  14378. // 在整体初始化后还需要设置一些初始状态
  14379. chart.on(EVENT_AFTER_INIT, this._afterinit);
  14380. chart.on(EVENT_AFTER_DATA_CHANGE, this._afterdatachange);
  14381. } // 缩放的主轴scale
  14382. ;
  14383. _proto.getPinchScale = function getPinchScale() {
  14384. var chart = this.chart; // 默认缩放x轴
  14385. var scale = chart.getXScale();
  14386. return scale;
  14387. } // 跟随轴的scale
  14388. ;
  14389. _proto.getFollowScale = function getFollowScale() {
  14390. var chart = this.chart; // 默认缩放x轴
  14391. var scales = chart.getYScales() || [];
  14392. return scales[0];
  14393. };
  14394. _proto.start = function start() {
  14395. var range = this.range;
  14396. var scale = this.getPinchScale();
  14397. var start = range[0],
  14398. end = range[1]; // 记录交互起始的范围
  14399. this.startRange = [start, end]; // 记录开始时的ticks个数
  14400. this.lastTickCount = scale.tickCount;
  14401. };
  14402. _proto.doZoom = function doZoom(leftScale, rightScale, zoom) {
  14403. var range = this.startRange,
  14404. minScale = this.minScale;
  14405. var start = range[0],
  14406. end = range[1];
  14407. var zoomOffset = 1 - zoom;
  14408. var rangeLen = end - start;
  14409. var rangeOffset = rangeLen * zoomOffset;
  14410. var leftOffset = rangeOffset * leftScale;
  14411. var rightOffset = rangeOffset * rightScale;
  14412. var newStart = Math.max(0, start - leftOffset);
  14413. var newEnd = Math.min(1, end + rightOffset);
  14414. var newRange = [newStart, newEnd]; // 如果已经到了最小比例,则不能再继续再放大
  14415. if (newEnd - newStart < minScale) {
  14416. return;
  14417. }
  14418. this.updateRange(newRange);
  14419. };
  14420. _proto.doMove = function doMove(ratio) {
  14421. // 不管是0, 还是其他,都不用处理
  14422. if (!ratio) return;
  14423. var range = this.startRange;
  14424. var start = range[0],
  14425. end = range[1];
  14426. var rangeLen = end - start;
  14427. var rangeOffset = rangeLen * ratio;
  14428. var newStart = start - rangeOffset;
  14429. var newEnd = end - rangeOffset; // 处理边界值
  14430. var newRange;
  14431. if (newStart < 0) {
  14432. newRange = [0, rangeLen];
  14433. } else if (newEnd > 1) {
  14434. newRange = [1 - rangeLen, 1];
  14435. } else {
  14436. newRange = [newStart, newEnd];
  14437. }
  14438. this.updateRange(newRange);
  14439. };
  14440. _proto.updateRange = function updateRange(range) {
  14441. var values = this.values; // 0, 1 的范围之间
  14442. var start = range[0],
  14443. end = range[1]; // start 不能小于0
  14444. start = Math.max(0, start); // end 不能大于1
  14445. end = Math.min(1, end); // 设置当前的范围
  14446. this.range = [start, end];
  14447. var len = values.length;
  14448. var valueStart = start * len;
  14449. var valueEnd = end * len; // 从原始数据里截取需要显示的数据
  14450. var newValues = values.slice(valueStart, valueEnd);
  14451. this.repaint(newValues);
  14452. };
  14453. _proto.repaint = function repaint(newValues) {
  14454. var chart = this.chart;
  14455. var scale = this.getPinchScale();
  14456. var currentValues = scale.values,
  14457. ticks = scale.ticks; // 如果新数组和当前显示的数组相同,则不更新
  14458. if (isValuesEqual(currentValues, newValues)) {
  14459. return;
  14460. } // 更新主轴values
  14461. this.updateScale(scale, {
  14462. ticks: ticks,
  14463. values: newValues
  14464. });
  14465. this.updateFollowScale(scale, newValues);
  14466. chart.repaint();
  14467. };
  14468. _proto.updateFollowScale = function updateFollowScale(pinchScale, pinchValues) {
  14469. var chart = this.chart;
  14470. var followScale = this.getFollowScale();
  14471. var pinchField = pinchScale.field,
  14472. pinchScaleType = pinchScale.type;
  14473. var followField = followScale.field; // 根据主轴的value值,找到所有从轴的value值
  14474. var values = []; // 转成map,让查找性能更高
  14475. var pinchValueMap = {};
  14476. pinchValues.forEach(function (item) {
  14477. pinchValueMap[item] = true;
  14478. });
  14479. var data = chart.get('data');
  14480. data.forEach(function (item) {
  14481. if (pinchScaleType === 'timeCat') {
  14482. var value = toTimeStamp(item[pinchField]);
  14483. if (pinchValueMap[value]) {
  14484. values.push(item[followField]);
  14485. }
  14486. }
  14487. });
  14488. var _getRange = getRange$1(values),
  14489. min = _getRange.min,
  14490. max = _getRange.max;
  14491. this.updateScale(followScale, {
  14492. min: min,
  14493. max: max,
  14494. nice: true
  14495. });
  14496. };
  14497. _proto.updateScale = function updateScale(scale, cfg) {
  14498. if (!scale) {
  14499. return;
  14500. }
  14501. scale.change(cfg);
  14502. } // 上一次的tick个数
  14503. ;
  14504. _proto.updateTicks = function updateTicks() {
  14505. var chart = this.chart,
  14506. values = this.values;
  14507. var scale = this.getPinchScale();
  14508. var currentValues = scale.values,
  14509. tickCount = scale.tickCount; // 根据当前数据的比例,和定义的tickCount计算应该需要多少个ticks
  14510. var newTickCount = Math.round(tickCount * values.length / currentValues.length);
  14511. var catTicks = getTickMethod('cat');
  14512. var ticks = catTicks({
  14513. tickCount: newTickCount,
  14514. values: values
  14515. });
  14516. this.updateScale(scale, {
  14517. ticks: ticks,
  14518. values: currentValues
  14519. }); // 更新完后,需要重新绘制一次
  14520. chart.repaint();
  14521. };
  14522. _proto.destroy = function destroy() {
  14523. var chart = this.chart;
  14524. chart.off(EVENT_AFTER_INIT, this._afterinit);
  14525. chart.off(EVENT_AFTER_DATA_CHANGE, this._afterdatachange);
  14526. };
  14527. return Context;
  14528. }();
  14529. var Base$2 = /*#__PURE__*/function () {
  14530. var _proto = Base.prototype;
  14531. // 交互的上下文
  14532. _proto.getDefaultCfg = function getDefaultCfg() {
  14533. return {};
  14534. };
  14535. _proto.getInteractionContext = function getInteractionContext(chart) {
  14536. var interactionContext = chart.get('interactionContext');
  14537. if (interactionContext) {
  14538. return interactionContext;
  14539. }
  14540. interactionContext = new Context(chart);
  14541. chart.set('interactionContext', interactionContext);
  14542. return interactionContext;
  14543. };
  14544. function Base(cfg, chart) {
  14545. var _this = this;
  14546. _defineProperty(this, "type", '');
  14547. _defineProperty(this, "startEvent", 'touchstart');
  14548. _defineProperty(this, "processEvent", 'touchmove');
  14549. _defineProperty(this, "endEvent", 'touchend');
  14550. _defineProperty(this, "resetEvent", null);
  14551. _defineProperty(this, "context", null);
  14552. _defineProperty(this, "_start", function (ev) {
  14553. _this.preStart && _this.preStart(ev);
  14554. _this.start(ev);
  14555. _this.onStart && _this.onStart(ev);
  14556. });
  14557. _defineProperty(this, "_process", function (ev) {
  14558. _this.preProcess && _this.preProcess(ev);
  14559. _this.process(ev);
  14560. _this.onProcess && _this.onProcess(ev);
  14561. });
  14562. _defineProperty(this, "_end", function (ev) {
  14563. _this.preEnd && _this.preEnd(ev);
  14564. _this.end(ev);
  14565. _this.onEnd && _this.onEnd(ev);
  14566. });
  14567. _defineProperty(this, "_reset", function (ev) {
  14568. _this.preReset && _this.preReset(ev);
  14569. _this.reset(ev);
  14570. _this.onReset && _this.onReset(ev);
  14571. });
  14572. mix(this, this.getDefaultCfg(), cfg);
  14573. this.context = this.getInteractionContext(chart);
  14574. this.chart = chart; // 只处理range, 暂时先这么处理后面再看情况调整
  14575. var range = this.range;
  14576. if (range) {
  14577. this.context.range = range;
  14578. }
  14579. this._bindEvents(chart);
  14580. }
  14581. _proto._bindEvents = function _bindEvents(chart) {
  14582. var startEvent = this.startEvent,
  14583. processEvent = this.processEvent,
  14584. endEvent = this.endEvent,
  14585. resetEvent = this.resetEvent;
  14586. var canvas = chart.get('canvas'); // 统一绑定事件
  14587. canvas.on(startEvent, this._start);
  14588. canvas.on(processEvent, this._process);
  14589. canvas.on(endEvent, this._end);
  14590. canvas.on(resetEvent, this._reset);
  14591. };
  14592. _proto._clearEvents = function _clearEvents() {
  14593. var chart = this.chart,
  14594. startEvent = this.startEvent,
  14595. processEvent = this.processEvent,
  14596. endEvent = this.endEvent,
  14597. resetEvent = this.resetEvent;
  14598. var canvas = chart.get('canvas'); // 统一绑定事件
  14599. canvas.off(startEvent, this._start);
  14600. canvas.off(processEvent, this._process);
  14601. canvas.off(endEvent, this._end);
  14602. canvas.off(resetEvent, this._start);
  14603. };
  14604. // override
  14605. _proto.start = function start() {} // override
  14606. ;
  14607. _proto.process = function process() {} // override
  14608. ;
  14609. _proto.end = function end() {} // override
  14610. ;
  14611. _proto.reset = function reset() {};
  14612. _proto.destroy = function destroy() {
  14613. this.context.destroy();
  14614. this._clearEvents();
  14615. };
  14616. return Base;
  14617. }();
  14618. var Pan = /*#__PURE__*/function (_Base) {
  14619. _inheritsLoose(Pan, _Base);
  14620. function Pan() {
  14621. return _Base.apply(this, arguments) || this;
  14622. }
  14623. var _proto = Pan.prototype;
  14624. _proto.getDefaultCfg = function getDefaultCfg() {
  14625. return {
  14626. type: 'pan',
  14627. startEvent: 'panstart',
  14628. processEvent: 'pan',
  14629. endEvent: 'panend'
  14630. };
  14631. };
  14632. _proto.start = function start() {
  14633. var context = this.context;
  14634. context.start();
  14635. };
  14636. _proto.process = function process(e) {
  14637. var direction = e.direction,
  14638. deltaX = e.deltaX;
  14639. if (direction === 'up' || direction === 'down') {
  14640. return;
  14641. }
  14642. e.preventDefault && e.preventDefault();
  14643. var context = this.context;
  14644. var chart = context.chart;
  14645. var coord = chart.get('coord');
  14646. var start = coord.start,
  14647. end = coord.end;
  14648. var coordWidth = end.x - start.x;
  14649. var ratio = deltaX / coordWidth;
  14650. context.doMove(ratio);
  14651. };
  14652. return Pan;
  14653. }(Base$2);
  14654. var Pinch = /*#__PURE__*/function (_Base) {
  14655. _inheritsLoose(Pinch, _Base);
  14656. var _proto = Pinch.prototype;
  14657. _proto.getDefaultCfg = function getDefaultCfg() {
  14658. return {
  14659. type: 'pinch',
  14660. startEvent: 'pinchstart',
  14661. processEvent: 'pinch',
  14662. endEvent: 'pinchend'
  14663. };
  14664. };
  14665. function Pinch(cfg, chart) {
  14666. var _this;
  14667. _this = _Base.call(this, cfg, chart) || this;
  14668. var _assertThisInitialize = _assertThisInitialized(_this),
  14669. context = _assertThisInitialize.context;
  14670. mix(context, cfg);
  14671. return _this;
  14672. }
  14673. _proto.start = function start() {
  14674. var context = this.context;
  14675. context.start();
  14676. };
  14677. _proto.process = function process(e) {
  14678. e.preventDefault && e.preventDefault();
  14679. var zoom = e.zoom,
  14680. center = e.center;
  14681. var context = this.context;
  14682. var chart = context.chart;
  14683. var coord = chart.get('coord');
  14684. var start = coord.start,
  14685. end = coord.end;
  14686. var coordWidth = end.x - start.x;
  14687. var leftLen = Math.abs(center.x - start.x);
  14688. var rightLen = Math.abs(end.x - center.x); // 计算左右缩放的比例
  14689. var leftScale = leftLen / coordWidth;
  14690. var rightScale = rightLen / coordWidth;
  14691. context.doZoom(leftScale, rightScale, zoom);
  14692. };
  14693. _proto.end = function end() {
  14694. // 缩放完成后再更新ticks
  14695. var context = this.context;
  14696. context.updateTicks();
  14697. };
  14698. return Pinch;
  14699. }(Base$2);
  14700. Chart.registerInteraction('pan', Pan);
  14701. Chart.registerInteraction('pinch', Pinch);
  14702. /**
  14703. * Default, without interactins
  14704. */
  14705. var Component = {
  14706. // 为了兼容之前版本
  14707. Marker: Shape$2.Marker,
  14708. Tooltip: Tooltip
  14709. }; // register plugins
  14710. Chart.plugins.register([Tooltip$1, Legend, Guide, Animation]); // 默认添加交互
  14711. var index = {
  14712. Component: Component,
  14713. Global: Global,
  14714. Chart: Chart,
  14715. Shape: Shape$1,
  14716. G: G,
  14717. Util: Util,
  14718. Helper: Helper,
  14719. track: track,
  14720. Animate: Animate
  14721. };
  14722. exports.Animate = Animate;
  14723. exports.Chart = Chart;
  14724. exports.Component = Component;
  14725. exports.G = G;
  14726. exports.Global = Global;
  14727. exports.Helper = Helper;
  14728. exports.Shape = Shape$1;
  14729. exports.Util = Util;
  14730. exports.default = index;
  14731. exports.track = track;
  14732. Object.defineProperty(exports, '__esModule', { value: true });
  14733. })));