u-charts.js 250 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804
  1. /*
  2. * uCharts®
  3. * 高性能跨平台图表库,支持H5、APP、小程序(微信/支付宝/百度/头条/QQ/360)、Vue、Taro等支持canvas的框架平台
  4. * Copyright (c) 2021 QIUN®秋云 https://www.ucharts.cn All rights reserved.
  5. * Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  6. * 复制使用请保留本段注释,感谢支持开源!
  7. *
  8. * uCharts®官方网站
  9. * https://www.uCharts.cn
  10. *
  11. * 开源地址:
  12. * https://gitee.com/uCharts/uCharts
  13. *
  14. * uni-app插件市场地址:
  15. * http://ext.dcloud.net.cn/plugin?id=271
  16. *
  17. */
  18. 'use strict';
  19. var config = {
  20. version: 'v2.3.7-20220122',
  21. yAxisWidth: 15,
  22. yAxisSplit: 5,
  23. xAxisHeight: 22,
  24. xAxisLineHeight: 22,
  25. legendHeight: 15,
  26. yAxisTitleWidth: 15,
  27. padding: [10, 10, 10, 10],
  28. pixelRatio: 1,
  29. rotate: false,
  30. columePadding: 3,
  31. fontSize: 13,
  32. fontColor: '#666666',
  33. dataPointShape: ['circle', 'circle', 'circle', 'circle'],
  34. color: ['#1890FF', '#91CB74', '#FAC858', '#EE6666', '#73C0DE', '#3CA272', '#FC8452', '#9A60B4', '#ea7ccc'],
  35. linearColor: ['#0EE2F8', '#2BDCA8', '#FA7D8D', '#EB88E2', '#2AE3A0', '#0EE2F8', '#EB88E2', '#6773E3', '#F78A85'],
  36. pieChartLinePadding: 15,
  37. pieChartTextPadding: 5,
  38. xAxisTextPadding: 3,
  39. titleColor: '#333333',
  40. titleFontSize: 20,
  41. subtitleColor: '#999999',
  42. subtitleFontSize: 15,
  43. toolTipPadding: 3,
  44. toolTipBackground: '#000000',
  45. toolTipOpacity: 0.7,
  46. toolTipLineHeight: 20,
  47. radarLabelTextMargin: 13,
  48. gaugeLabelTextMargin: 13
  49. };
  50. var assign = function(target, ...varArgs) {
  51. if (target == null) {
  52. throw new TypeError('[uCharts] Cannot convert undefined or null to object');
  53. }
  54. if (!varArgs || varArgs.length <= 0) {
  55. return target;
  56. }
  57. // 深度合并对象
  58. function deepAssign(obj1, obj2) {
  59. for (let key in obj2) {
  60. obj1[key] = obj1[key] && obj1[key].toString() === "[object Object]" ?
  61. deepAssign(obj1[key], obj2[key]) : obj1[key] = obj2[key];
  62. }
  63. return obj1;
  64. }
  65. varArgs.forEach(val => {
  66. target = deepAssign(target, val);
  67. });
  68. return target;
  69. };
  70. var util = {
  71. toFixed: function toFixed(num, limit) {
  72. limit = limit || 2;
  73. if (this.isFloat(num)) {
  74. num = num.toFixed(limit);
  75. }
  76. return num;
  77. },
  78. isFloat: function isFloat(num) {
  79. return num % 1 !== 0;
  80. },
  81. approximatelyEqual: function approximatelyEqual(num1, num2) {
  82. return Math.abs(num1 - num2) < 1e-10;
  83. },
  84. isSameSign: function isSameSign(num1, num2) {
  85. return Math.abs(num1) === num1 && Math.abs(num2) === num2 || Math.abs(num1) !== num1 && Math.abs(num2) !== num2;
  86. },
  87. isSameXCoordinateArea: function isSameXCoordinateArea(p1, p2) {
  88. return this.isSameSign(p1.x, p2.x);
  89. },
  90. isCollision: function isCollision(obj1, obj2) {
  91. obj1.end = {};
  92. obj1.end.x = obj1.start.x + obj1.width;
  93. obj1.end.y = obj1.start.y - obj1.height;
  94. obj2.end = {};
  95. obj2.end.x = obj2.start.x + obj2.width;
  96. obj2.end.y = obj2.start.y - obj2.height;
  97. var flag = obj2.start.x > obj1.end.x || obj2.end.x < obj1.start.x || obj2.end.y > obj1.start.y || obj2.start.y < obj1.end.y;
  98. return !flag;
  99. }
  100. };
  101. //兼容H5点击事件
  102. function getH5Offset(e) {
  103. e.mp = {
  104. changedTouches: []
  105. };
  106. e.mp.changedTouches.push({
  107. x: e.offsetX,
  108. y: e.offsetY
  109. });
  110. return e;
  111. }
  112. // 经纬度转墨卡托
  113. function lonlat2mercator(longitude, latitude) {
  114. var mercator = Array(2);
  115. var x = longitude * 20037508.34 / 180;
  116. var y = Math.log(Math.tan((90 + latitude) * Math.PI / 360)) / (Math.PI / 180);
  117. y = y * 20037508.34 / 180;
  118. mercator[0] = x;
  119. mercator[1] = y;
  120. return mercator;
  121. }
  122. // 墨卡托转经纬度
  123. function mercator2lonlat(longitude, latitude) {
  124. var lonlat = Array(2)
  125. var x = longitude / 20037508.34 * 180;
  126. var y = latitude / 20037508.34 * 180;
  127. y = 180 / Math.PI * (2 * Math.atan(Math.exp(y * Math.PI / 180)) - Math.PI / 2);
  128. lonlat[0] = x;
  129. lonlat[1] = y;
  130. return lonlat;
  131. }
  132. // hex 转 rgba
  133. function hexToRgb(hexValue, opc) {
  134. var rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  135. var hex = hexValue.replace(rgx, function(m, r, g, b) {
  136. return r + r + g + g + b + b;
  137. });
  138. var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  139. var r = parseInt(rgb[1], 16);
  140. var g = parseInt(rgb[2], 16);
  141. var b = parseInt(rgb[3], 16);
  142. return 'rgba(' + r + ',' + g + ',' + b + ',' + opc + ')';
  143. }
  144. function findRange(num, type, limit) {
  145. if (isNaN(num)) {
  146. throw new Error('[uCharts] series数据需为Number格式');
  147. }
  148. limit = limit || 10;
  149. type = type ? type : 'upper';
  150. var multiple = 1;
  151. while (limit < 1) {
  152. limit *= 10;
  153. multiple *= 10;
  154. }
  155. if (type === 'upper') {
  156. num = Math.ceil(num * multiple);
  157. } else {
  158. num = Math.floor(num * multiple);
  159. }
  160. while (num % limit !== 0) {
  161. if (type === 'upper') {
  162. num++;
  163. } else {
  164. num--;
  165. }
  166. }
  167. return num / multiple;
  168. }
  169. function calCandleMA(dayArr, nameArr, colorArr, kdata) {
  170. let seriesTemp = [];
  171. for (let k = 0; k < dayArr.length; k++) {
  172. let seriesItem = {
  173. data: [],
  174. name: nameArr[k],
  175. color: colorArr[k]
  176. };
  177. for (let i = 0, len = kdata.length; i < len; i++) {
  178. if (i < dayArr[k]) {
  179. seriesItem.data.push(null);
  180. continue;
  181. }
  182. let sum = 0;
  183. for (let j = 0; j < dayArr[k]; j++) {
  184. sum += kdata[i - j][1];
  185. }
  186. seriesItem.data.push(+(sum / dayArr[k]).toFixed(3));
  187. }
  188. seriesTemp.push(seriesItem);
  189. }
  190. return seriesTemp;
  191. }
  192. function calValidDistance(self, distance, chartData, config, opts) {
  193. var dataChartAreaWidth = opts.width - opts.area[1] - opts.area[3];
  194. var dataChartWidth = chartData.eachSpacing * (opts.chartData.xAxisData.xAxisPoints.length - 1);
  195. var validDistance = distance;
  196. if (distance >= 0) {
  197. validDistance = 0;
  198. self.uevent.trigger('scrollLeft');
  199. self.scrollOption.position = 'left'
  200. opts.xAxis.scrollPosition = 'left';
  201. } else if (Math.abs(distance) >= dataChartWidth - dataChartAreaWidth) {
  202. validDistance = dataChartAreaWidth - dataChartWidth;
  203. self.uevent.trigger('scrollRight');
  204. self.scrollOption.position = 'right'
  205. opts.xAxis.scrollPosition = 'right';
  206. } else {
  207. self.scrollOption.position = distance
  208. opts.xAxis.scrollPosition = distance;
  209. }
  210. return validDistance;
  211. }
  212. function isInAngleRange(angle, startAngle, endAngle) {
  213. function adjust(angle) {
  214. while (angle < 0) {
  215. angle += 2 * Math.PI;
  216. }
  217. while (angle > 2 * Math.PI) {
  218. angle -= 2 * Math.PI;
  219. }
  220. return angle;
  221. }
  222. angle = adjust(angle);
  223. startAngle = adjust(startAngle);
  224. endAngle = adjust(endAngle);
  225. if (startAngle > endAngle) {
  226. endAngle += 2 * Math.PI;
  227. if (angle < startAngle) {
  228. angle += 2 * Math.PI;
  229. }
  230. }
  231. return angle >= startAngle && angle <= endAngle;
  232. }
  233. function calRotateTranslate(x, y, h) {
  234. var xv = x;
  235. var yv = h - y;
  236. var transX = xv + (h - yv - xv) / Math.sqrt(2);
  237. transX *= -1;
  238. var transY = (h - yv) * (Math.sqrt(2) - 1) - (h - yv - xv) / Math.sqrt(2);
  239. return {
  240. transX: transX,
  241. transY: transY
  242. };
  243. }
  244. function createCurveControlPoints(points, i) {
  245. function isNotMiddlePoint(points, i) {
  246. if (points[i - 1] && points[i + 1]) {
  247. return points[i].y >= Math.max(points[i - 1].y, points[i + 1].y) || points[i].y <= Math.min(points[i - 1].y,
  248. points[i + 1].y);
  249. } else {
  250. return false;
  251. }
  252. }
  253. function isNotMiddlePointX(points, i) {
  254. if (points[i - 1] && points[i + 1]) {
  255. return points[i].x >= Math.max(points[i - 1].x, points[i + 1].x) || points[i].x <= Math.min(points[i - 1].x,
  256. points[i + 1].x);
  257. } else {
  258. return false;
  259. }
  260. }
  261. var a = 0.2;
  262. var b = 0.2;
  263. var pAx = null;
  264. var pAy = null;
  265. var pBx = null;
  266. var pBy = null;
  267. if (i < 1) {
  268. pAx = points[0].x + (points[1].x - points[0].x) * a;
  269. pAy = points[0].y + (points[1].y - points[0].y) * a;
  270. } else {
  271. pAx = points[i].x + (points[i + 1].x - points[i - 1].x) * a;
  272. pAy = points[i].y + (points[i + 1].y - points[i - 1].y) * a;
  273. }
  274. if (i > points.length - 3) {
  275. var last = points.length - 1;
  276. pBx = points[last].x - (points[last].x - points[last - 1].x) * b;
  277. pBy = points[last].y - (points[last].y - points[last - 1].y) * b;
  278. } else {
  279. pBx = points[i + 1].x - (points[i + 2].x - points[i].x) * b;
  280. pBy = points[i + 1].y - (points[i + 2].y - points[i].y) * b;
  281. }
  282. if (isNotMiddlePoint(points, i + 1)) {
  283. pBy = points[i + 1].y;
  284. }
  285. if (isNotMiddlePoint(points, i)) {
  286. pAy = points[i].y;
  287. }
  288. if (isNotMiddlePointX(points, i + 1)) {
  289. pBx = points[i + 1].x;
  290. }
  291. if (isNotMiddlePointX(points, i)) {
  292. pAx = points[i].x;
  293. }
  294. if (pAy >= Math.max(points[i].y, points[i + 1].y) || pAy <= Math.min(points[i].y, points[i + 1].y)) {
  295. pAy = points[i].y;
  296. }
  297. if (pBy >= Math.max(points[i].y, points[i + 1].y) || pBy <= Math.min(points[i].y, points[i + 1].y)) {
  298. pBy = points[i + 1].y;
  299. }
  300. if (pAx >= Math.max(points[i].x, points[i + 1].x) || pAx <= Math.min(points[i].x, points[i + 1].x)) {
  301. pAx = points[i].x;
  302. }
  303. if (pBx >= Math.max(points[i].x, points[i + 1].x) || pBx <= Math.min(points[i].x, points[i + 1].x)) {
  304. pBx = points[i + 1].x;
  305. }
  306. return {
  307. ctrA: {
  308. x: pAx,
  309. y: pAy
  310. },
  311. ctrB: {
  312. x: pBx,
  313. y: pBy
  314. }
  315. };
  316. }
  317. function convertCoordinateOrigin(x, y, center) {
  318. return {
  319. x: center.x + x,
  320. y: center.y - y
  321. };
  322. }
  323. function avoidCollision(obj, target) {
  324. if (target) {
  325. // is collision test
  326. while (util.isCollision(obj, target)) {
  327. if (obj.start.x > 0) {
  328. obj.start.y--;
  329. } else if (obj.start.x < 0) {
  330. obj.start.y++;
  331. } else {
  332. if (obj.start.y > 0) {
  333. obj.start.y++;
  334. } else {
  335. obj.start.y--;
  336. }
  337. }
  338. }
  339. }
  340. return obj;
  341. }
  342. function fixPieSeries(series, opts, config){
  343. let pieSeriesArr = [];
  344. if(series.length>0 && series[0].data.constructor.toString().indexOf('Array') > -1){
  345. opts._pieSeries_ = series;
  346. let oldseries = series[0].data;
  347. for (var i = 0; i < oldseries.length; i++) {
  348. oldseries[i].formatter = series[0].formatter;
  349. oldseries[i].data = oldseries[i].value;
  350. pieSeriesArr.push(oldseries[i]);
  351. }
  352. opts.series = pieSeriesArr;
  353. }else{
  354. pieSeriesArr = series;
  355. }
  356. return pieSeriesArr;
  357. }
  358. function fillSeries(series, opts, config) {
  359. var index = 0;
  360. for (var i = 0; i < series.length; i++) {
  361. let item = series[i];
  362. if (!item.color) {
  363. item.color = config.color[index];
  364. index = (index + 1) % config.color.length;
  365. }
  366. if (!item.linearIndex) {
  367. item.linearIndex = i;
  368. }
  369. if (!item.index) {
  370. item.index = 0;
  371. }
  372. if (!item.type) {
  373. item.type = opts.type;
  374. }
  375. if (typeof item.show == "undefined") {
  376. item.show = true;
  377. }
  378. if (!item.type) {
  379. item.type = opts.type;
  380. }
  381. if (!item.pointShape) {
  382. item.pointShape = "circle";
  383. }
  384. if (!item.legendShape) {
  385. switch (item.type) {
  386. case 'line':
  387. item.legendShape = "line";
  388. break;
  389. case 'column':
  390. item.legendShape = "rect";
  391. break;
  392. case 'area':
  393. item.legendShape = "triangle";
  394. break;
  395. case 'bar':
  396. item.legendShape = "rect";
  397. break;
  398. default:
  399. item.legendShape = "circle";
  400. }
  401. }
  402. }
  403. return series;
  404. }
  405. function fillCustomColor(linearType, customColor, series, config) {
  406. var newcolor = customColor || [];
  407. if (linearType == 'custom' && newcolor.length == 0 ) {
  408. newcolor = config.linearColor;
  409. }
  410. if (linearType == 'custom' && newcolor.length < series.length) {
  411. let chazhi = series.length - newcolor.length;
  412. for (var i = 0; i < chazhi; i++) {
  413. newcolor.push(config.linearColor[(i + 1) % config.linearColor.length]);
  414. }
  415. }
  416. return newcolor;
  417. }
  418. function getDataRange(minData, maxData) {
  419. var limit = 0;
  420. var range = maxData - minData;
  421. if (range >= 10000) {
  422. limit = 1000;
  423. } else if (range >= 1000) {
  424. limit = 100;
  425. } else if (range >= 100) {
  426. limit = 10;
  427. } else if (range >= 10) {
  428. limit = 5;
  429. } else if (range >= 1) {
  430. limit = 1;
  431. } else if (range >= 0.1) {
  432. limit = 0.1;
  433. } else if (range >= 0.01) {
  434. limit = 0.01;
  435. } else if (range >= 0.001) {
  436. limit = 0.001;
  437. } else if (range >= 0.0001) {
  438. limit = 0.0001;
  439. } else if (range >= 0.00001) {
  440. limit = 0.00001;
  441. } else {
  442. limit = 0.000001;
  443. }
  444. return {
  445. minRange: findRange(minData, 'lower', limit),
  446. maxRange: findRange(maxData, 'upper', limit)
  447. };
  448. }
  449. function measureText(text, fontSize, context) {
  450. var width = 0;
  451. text = String(text);
  452. // #ifdef MP-ALIPAY || MP-BAIDU || APP-NVUE
  453. context = false;
  454. // #endif
  455. if (context !== false && context !== undefined && context.setFontSize && context.measureText) {
  456. context.setFontSize(fontSize);
  457. return context.measureText(text).width;
  458. } else {
  459. var text = text.split('');
  460. for (let i = 0; i < text.length; i++) {
  461. let item = text[i];
  462. if (/[a-zA-Z]/.test(item)) {
  463. width += 7;
  464. } else if (/[0-9]/.test(item)) {
  465. width += 5.5;
  466. } else if (/\./.test(item)) {
  467. width += 2.7;
  468. } else if (/-/.test(item)) {
  469. width += 3.25;
  470. } else if (/:/.test(item)) {
  471. width += 2.5;
  472. } else if (/[\u4e00-\u9fa5]/.test(item)) {
  473. width += 10;
  474. } else if (/\(|\)/.test(item)) {
  475. width += 3.73;
  476. } else if (/\s/.test(item)) {
  477. width += 2.5;
  478. } else if (/%/.test(item)) {
  479. width += 8;
  480. } else {
  481. width += 10;
  482. }
  483. }
  484. return width * fontSize / 10;
  485. }
  486. }
  487. function dataCombine(series) {
  488. return series.reduce(function(a, b) {
  489. return (a.data ? a.data : a).concat(b.data);
  490. }, []);
  491. }
  492. function dataCombineStack(series, len) {
  493. var sum = new Array(len);
  494. for (var j = 0; j < sum.length; j++) {
  495. sum[j] = 0;
  496. }
  497. for (var i = 0; i < series.length; i++) {
  498. for (var j = 0; j < sum.length; j++) {
  499. sum[j] += series[i].data[j];
  500. }
  501. }
  502. return series.reduce(function(a, b) {
  503. return (a.data ? a.data : a).concat(b.data).concat(sum);
  504. }, []);
  505. }
  506. function getTouches(touches, opts, e) {
  507. let x, y;
  508. if (touches.clientX) {
  509. if (opts.rotate) {
  510. y = opts.height - touches.clientX * opts.pix;
  511. x = (touches.pageY - e.currentTarget.offsetTop - (opts.height / opts.pix / 2) * (opts.pix - 1)) * opts.pix;
  512. } else {
  513. x = touches.clientX * opts.pix;
  514. y = (touches.pageY - e.currentTarget.offsetTop - (opts.height / opts.pix / 2) * (opts.pix - 1)) * opts.pix;
  515. }
  516. } else {
  517. if (opts.rotate) {
  518. y = opts.height - touches.x * opts.pix;
  519. x = touches.y * opts.pix;
  520. } else {
  521. x = touches.x * opts.pix;
  522. y = touches.y * opts.pix;
  523. }
  524. }
  525. return {
  526. x: x,
  527. y: y
  528. }
  529. }
  530. function getSeriesDataItem(series, index, group) {
  531. var data = [];
  532. var newSeries = [];
  533. var indexIsArr = index.constructor.toString().indexOf('Array') > -1;
  534. if(indexIsArr){
  535. let tempSeries = filterSeries(series);
  536. for (var i = 0; i < group.length; i++) {
  537. newSeries.push(tempSeries[group[i]]);
  538. }
  539. }else{
  540. newSeries = series;
  541. };
  542. for (let i = 0; i < newSeries.length; i++) {
  543. let item = newSeries[i];
  544. let tmpindex = -1;
  545. if(indexIsArr){
  546. tmpindex = index[i];
  547. }else{
  548. tmpindex = index;
  549. }
  550. if (item.data[tmpindex] !== null && typeof item.data[tmpindex] !== 'undefined' && item.show) {
  551. let seriesItem = {};
  552. seriesItem.color = item.color;
  553. seriesItem.type = item.type;
  554. seriesItem.style = item.style;
  555. seriesItem.pointShape = item.pointShape;
  556. seriesItem.disableLegend = item.disableLegend;
  557. seriesItem.name = item.name;
  558. seriesItem.show = item.show;
  559. seriesItem.data = item.formatter ? item.formatter(item.data[tmpindex]) : item.data[tmpindex];
  560. data.push(seriesItem);
  561. }
  562. }
  563. return data;
  564. }
  565. function getMaxTextListLength(list, fontSize, context) {
  566. var lengthList = list.map(function(item) {
  567. return measureText(item, fontSize, context);
  568. });
  569. return Math.max.apply(null, lengthList);
  570. }
  571. function getRadarCoordinateSeries(length) {
  572. var eachAngle = 2 * Math.PI / length;
  573. var CoordinateSeries = [];
  574. for (var i = 0; i < length; i++) {
  575. CoordinateSeries.push(eachAngle * i);
  576. }
  577. return CoordinateSeries.map(function(item) {
  578. return -1 * item + Math.PI / 2;
  579. });
  580. }
  581. function getToolTipData(seriesData, opts, index, group, categories) {
  582. var option = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {};
  583. var calPoints = opts.chartData.calPoints?opts.chartData.calPoints:[];
  584. let points = {};
  585. if(group.length > 0){
  586. let filterPoints = [];
  587. for (let i = 0; i < group.length; i++) {
  588. filterPoints.push(calPoints[group[i]])
  589. }
  590. points = filterPoints[0][index[0]];
  591. }else{
  592. points = calPoints[0][index];
  593. };
  594. var textList = seriesData.map(function(item) {
  595. let titleText = null;
  596. if (opts.categories && opts.categories.length>0) {
  597. titleText = categories[index];
  598. };
  599. return {
  600. text: option.formatter ? option.formatter(item, titleText, index, opts) : item.name + ': ' + item.data,
  601. color: item.color
  602. };
  603. });
  604. var offset = {
  605. x: Math.round(points.x),
  606. y: Math.round(points.y)
  607. };
  608. return {
  609. textList: textList,
  610. offset: offset
  611. };
  612. }
  613. function getMixToolTipData(seriesData, opts, index, categories) {
  614. var option = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
  615. var points = opts.chartData.xAxisPoints[index] + opts.chartData.eachSpacing / 2;
  616. var textList = seriesData.map(function(item) {
  617. return {
  618. text: option.formatter ? option.formatter(item, categories[index], index, opts) : item.name + ': ' + item.data,
  619. color: item.color,
  620. disableLegend: item.disableLegend ? true : false
  621. };
  622. });
  623. textList = textList.filter(function(item) {
  624. if (item.disableLegend !== true) {
  625. return item;
  626. }
  627. });
  628. var offset = {
  629. x: Math.round(points),
  630. y: 0
  631. };
  632. return {
  633. textList: textList,
  634. offset: offset
  635. };
  636. }
  637. function getCandleToolTipData(series, seriesData, opts, index, categories, extra) {
  638. var option = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : {};
  639. var calPoints = opts.chartData.calPoints;
  640. let upColor = extra.color.upFill;
  641. let downColor = extra.color.downFill;
  642. //颜色顺序为开盘,收盘,最低,最高
  643. let color = [upColor, upColor, downColor, upColor];
  644. var textList = [];
  645. seriesData.map(function(item) {
  646. if (index == 0) {
  647. if (item.data[1] - item.data[0] < 0) {
  648. color[1] = downColor;
  649. } else {
  650. color[1] = upColor;
  651. }
  652. } else {
  653. if (item.data[0] < series[index - 1][1]) {
  654. color[0] = downColor;
  655. }
  656. if (item.data[1] < item.data[0]) {
  657. color[1] = downColor;
  658. }
  659. if (item.data[2] > series[index - 1][1]) {
  660. color[2] = upColor;
  661. }
  662. if (item.data[3] < series[index - 1][1]) {
  663. color[3] = downColor;
  664. }
  665. }
  666. let text1 = {
  667. text: '开盘:' + item.data[0],
  668. color: color[0]
  669. };
  670. let text2 = {
  671. text: '收盘:' + item.data[1],
  672. color: color[1]
  673. };
  674. let text3 = {
  675. text: '最低:' + item.data[2],
  676. color: color[2]
  677. };
  678. let text4 = {
  679. text: '最高:' + item.data[3],
  680. color: color[3]
  681. };
  682. textList.push(text1, text2, text3, text4);
  683. });
  684. var validCalPoints = [];
  685. var offset = {
  686. x: 0,
  687. y: 0
  688. };
  689. for (let i = 0; i < calPoints.length; i++) {
  690. let points = calPoints[i];
  691. if (typeof points[index] !== 'undefined' && points[index] !== null) {
  692. validCalPoints.push(points[index]);
  693. }
  694. }
  695. offset.x = Math.round(validCalPoints[0][0].x);
  696. return {
  697. textList: textList,
  698. offset: offset
  699. };
  700. }
  701. function filterSeries(series) {
  702. let tempSeries = [];
  703. for (let i = 0; i < series.length; i++) {
  704. if (series[i].show == true) {
  705. tempSeries.push(series[i])
  706. }
  707. }
  708. return tempSeries;
  709. }
  710. function findCurrentIndex(currentPoints, calPoints, opts, config) {
  711. var offset = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
  712. var current={ index:-1, group:[] };
  713. var spacing = opts.chartData.eachSpacing / 2;
  714. let xAxisPoints = [];
  715. if (calPoints && calPoints.length > 0) {
  716. if (!opts.categories) {
  717. spacing = 0;
  718. }else{
  719. for (let i = 1; i < opts.chartData.xAxisPoints.length; i++) {
  720. xAxisPoints.push(opts.chartData.xAxisPoints[i] - spacing);
  721. }
  722. if ((opts.type == 'line' || opts.type == 'area') && opts.xAxis.boundaryGap == 'justify') {
  723. xAxisPoints = opts.chartData.xAxisPoints;
  724. }
  725. }
  726. if (isInExactChartArea(currentPoints, opts, config)) {
  727. if (!opts.categories) {
  728. let timePoints = Array(calPoints.length);
  729. for (let i = 0; i < calPoints.length; i++) {
  730. timePoints[i] = Array(calPoints[i].length)
  731. for (let j = 0; j < calPoints[i].length; j++) {
  732. timePoints[i][j] = (Math.abs(calPoints[i][j].x - currentPoints.x));
  733. }
  734. };
  735. let pointValue = Array(timePoints.length);
  736. let pointIndex = Array(timePoints.length);
  737. for (let i = 0; i < timePoints.length; i++) {
  738. pointValue[i] = Math.min.apply(null, timePoints[i]);
  739. pointIndex[i] = timePoints[i].indexOf(pointValue[i]);
  740. }
  741. let minValue = Math.min.apply(null, pointValue);
  742. current.index = [];
  743. for (let i = 0; i < pointValue.length; i++) {
  744. if(pointValue[i] == minValue){
  745. current.group.push(i);
  746. current.index.push(pointIndex[i]);
  747. }
  748. };
  749. }else{
  750. xAxisPoints.forEach(function(item, index) {
  751. if (currentPoints.x + offset + spacing > item) {
  752. current.index = index;
  753. }
  754. });
  755. }
  756. }
  757. }
  758. return current;
  759. }
  760. function findBarChartCurrentIndex(currentPoints, calPoints, opts, config) {
  761. var offset = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
  762. var current={ index:-1, group:[] };
  763. var spacing = opts.chartData.eachSpacing / 2;
  764. let yAxisPoints = opts.chartData.yAxisPoints;
  765. if (calPoints && calPoints.length > 0) {
  766. if (isInExactChartArea(currentPoints, opts, config)) {
  767. yAxisPoints.forEach(function(item, index) {
  768. if (currentPoints.y + offset + spacing > item) {
  769. current.index = index;
  770. }
  771. });
  772. }
  773. }
  774. return current;
  775. }
  776. function findLegendIndex(currentPoints, legendData, opts) {
  777. let currentIndex = -1;
  778. let gap = 0;
  779. if (isInExactLegendArea(currentPoints, legendData.area)) {
  780. let points = legendData.points;
  781. let index = -1;
  782. for (let i = 0, len = points.length; i < len; i++) {
  783. let item = points[i];
  784. for (let j = 0; j < item.length; j++) {
  785. index += 1;
  786. let area = item[j]['area'];
  787. if (area && currentPoints.x > area[0] - gap && currentPoints.x < area[2] + gap && currentPoints.y > area[1] - gap && currentPoints.y < area[3] + gap) {
  788. currentIndex = index;
  789. break;
  790. }
  791. }
  792. }
  793. return currentIndex;
  794. }
  795. return currentIndex;
  796. }
  797. function isInExactLegendArea(currentPoints, area) {
  798. return currentPoints.x > area.start.x && currentPoints.x < area.end.x && currentPoints.y > area.start.y && currentPoints.y < area.end.y;
  799. }
  800. function isInExactChartArea(currentPoints, opts, config) {
  801. return currentPoints.x <= opts.width - opts.area[1] + 10 && currentPoints.x >= opts.area[3] - 10 && currentPoints.y >= opts.area[0] && currentPoints.y <= opts.height - opts.area[2];
  802. }
  803. function findRadarChartCurrentIndex(currentPoints, radarData, count) {
  804. var eachAngleArea = 2 * Math.PI / count;
  805. var currentIndex = -1;
  806. if (isInExactPieChartArea(currentPoints, radarData.center, radarData.radius)) {
  807. var fixAngle = function fixAngle(angle) {
  808. if (angle < 0) {
  809. angle += 2 * Math.PI;
  810. }
  811. if (angle > 2 * Math.PI) {
  812. angle -= 2 * Math.PI;
  813. }
  814. return angle;
  815. };
  816. var angle = Math.atan2(radarData.center.y - currentPoints.y, currentPoints.x - radarData.center.x);
  817. angle = -1 * angle;
  818. if (angle < 0) {
  819. angle += 2 * Math.PI;
  820. }
  821. var angleList = radarData.angleList.map(function(item) {
  822. item = fixAngle(-1 * item);
  823. return item;
  824. });
  825. angleList.forEach(function(item, index) {
  826. var rangeStart = fixAngle(item - eachAngleArea / 2);
  827. var rangeEnd = fixAngle(item + eachAngleArea / 2);
  828. if (rangeEnd < rangeStart) {
  829. rangeEnd += 2 * Math.PI;
  830. }
  831. if (angle >= rangeStart && angle <= rangeEnd || angle + 2 * Math.PI >= rangeStart && angle + 2 * Math.PI <= rangeEnd) {
  832. currentIndex = index;
  833. }
  834. });
  835. }
  836. return currentIndex;
  837. }
  838. function findFunnelChartCurrentIndex(currentPoints, funnelData) {
  839. var currentIndex = -1;
  840. for (var i = 0, len = funnelData.series.length; i < len; i++) {
  841. var item = funnelData.series[i];
  842. if (currentPoints.x > item.funnelArea[0] && currentPoints.x < item.funnelArea[2] && currentPoints.y > item.funnelArea[1] && currentPoints.y < item.funnelArea[3]) {
  843. currentIndex = i;
  844. break;
  845. }
  846. }
  847. return currentIndex;
  848. }
  849. function findWordChartCurrentIndex(currentPoints, wordData) {
  850. var currentIndex = -1;
  851. for (var i = 0, len = wordData.length; i < len; i++) {
  852. var item = wordData[i];
  853. if (currentPoints.x > item.area[0] && currentPoints.x < item.area[2] && currentPoints.y > item.area[1] && currentPoints.y < item.area[3]) {
  854. currentIndex = i;
  855. break;
  856. }
  857. }
  858. return currentIndex;
  859. }
  860. function findMapChartCurrentIndex(currentPoints, opts) {
  861. var currentIndex = -1;
  862. var cData = opts.chartData.mapData;
  863. var data = opts.series;
  864. var tmp = pointToCoordinate(currentPoints.y, currentPoints.x, cData.bounds, cData.scale, cData.xoffset, cData.yoffset);
  865. var poi = [tmp.x, tmp.y];
  866. for (var i = 0, len = data.length; i < len; i++) {
  867. var item = data[i].geometry.coordinates;
  868. if (isPoiWithinPoly(poi, item, opts.chartData.mapData.mercator)) {
  869. currentIndex = i;
  870. break;
  871. }
  872. }
  873. return currentIndex;
  874. }
  875. function findRoseChartCurrentIndex(currentPoints, pieData, opts) {
  876. var currentIndex = -1;
  877. var series = getRoseDataPoints(opts._series_, opts.extra.rose.type, pieData.radius, pieData.radius);
  878. if (pieData && pieData.center && isInExactPieChartArea(currentPoints, pieData.center, pieData.radius)) {
  879. var angle = Math.atan2(pieData.center.y - currentPoints.y, currentPoints.x - pieData.center.x);
  880. angle = -angle;
  881. if(opts.extra.rose && opts.extra.rose.offsetAngle){
  882. angle = angle - opts.extra.rose.offsetAngle * Math.PI / 180;
  883. }
  884. for (var i = 0, len = series.length; i < len; i++) {
  885. if (isInAngleRange(angle, series[i]._start_, series[i]._start_ + series[i]._rose_proportion_ * 2 * Math.PI)) {
  886. currentIndex = i;
  887. break;
  888. }
  889. }
  890. }
  891. return currentIndex;
  892. }
  893. function findPieChartCurrentIndex(currentPoints, pieData, opts) {
  894. var currentIndex = -1;
  895. var series = getPieDataPoints(pieData.series);
  896. if (pieData && pieData.center && isInExactPieChartArea(currentPoints, pieData.center, pieData.radius)) {
  897. var angle = Math.atan2(pieData.center.y - currentPoints.y, currentPoints.x - pieData.center.x);
  898. angle = -angle;
  899. if(opts.extra.pie && opts.extra.pie.offsetAngle){
  900. angle = angle - opts.extra.pie.offsetAngle * Math.PI / 180;
  901. }
  902. if(opts.extra.ring && opts.extra.ring.offsetAngle){
  903. angle = angle - opts.extra.ring.offsetAngle * Math.PI / 180;
  904. }
  905. for (var i = 0, len = series.length; i < len; i++) {
  906. if (isInAngleRange(angle, series[i]._start_, series[i]._start_ + series[i]._proportion_ * 2 * Math.PI)) {
  907. currentIndex = i;
  908. break;
  909. }
  910. }
  911. }
  912. return currentIndex;
  913. }
  914. function isInExactPieChartArea(currentPoints, center, radius) {
  915. return Math.pow(currentPoints.x - center.x, 2) + Math.pow(currentPoints.y - center.y, 2) <= Math.pow(radius, 2);
  916. }
  917. function splitPoints(points,eachSeries) {
  918. var newPoints = [];
  919. var items = [];
  920. points.forEach(function(item, index) {
  921. if(eachSeries.connectNulls){
  922. if (item !== null) {
  923. items.push(item);
  924. }
  925. }else{
  926. if (item !== null) {
  927. items.push(item);
  928. } else {
  929. if (items.length) {
  930. newPoints.push(items);
  931. }
  932. items = [];
  933. }
  934. }
  935. });
  936. if (items.length) {
  937. newPoints.push(items);
  938. }
  939. return newPoints;
  940. }
  941. function calLegendData(series, opts, config, chartData, context) {
  942. let legendData = {
  943. area: {
  944. start: {
  945. x: 0,
  946. y: 0
  947. },
  948. end: {
  949. x: 0,
  950. y: 0
  951. },
  952. width: 0,
  953. height: 0,
  954. wholeWidth: 0,
  955. wholeHeight: 0
  956. },
  957. points: [],
  958. widthArr: [],
  959. heightArr: []
  960. };
  961. if (opts.legend.show === false) {
  962. chartData.legendData = legendData;
  963. return legendData;
  964. }
  965. let padding = opts.legend.padding * opts.pix;
  966. let margin = opts.legend.margin * opts.pix;
  967. let fontSize = opts.legend.fontSize ? opts.legend.fontSize * opts.pix : config.fontSize;
  968. let shapeWidth = 15 * opts.pix;
  969. let shapeRight = 5 * opts.pix;
  970. let lineHeight = Math.max(opts.legend.lineHeight * opts.pix, fontSize);
  971. if (opts.legend.position == 'top' || opts.legend.position == 'bottom') {
  972. let legendList = [];
  973. let widthCount = 0;
  974. let widthCountArr = [];
  975. let currentRow = [];
  976. for (let i = 0; i < series.length; i++) {
  977. let item = series[i];
  978. let itemWidth = shapeWidth + shapeRight + measureText(item.name || 'undefined', fontSize, context) + opts.legend.itemGap * opts.pix;
  979. if (widthCount + itemWidth > opts.width - opts.area[1] - opts.area[3]) {
  980. legendList.push(currentRow);
  981. widthCountArr.push(widthCount - opts.legend.itemGap * opts.pix);
  982. widthCount = itemWidth;
  983. currentRow = [item];
  984. } else {
  985. widthCount += itemWidth;
  986. currentRow.push(item);
  987. }
  988. }
  989. if (currentRow.length) {
  990. legendList.push(currentRow);
  991. widthCountArr.push(widthCount - opts.legend.itemGap * opts.pix);
  992. legendData.widthArr = widthCountArr;
  993. let legendWidth = Math.max.apply(null, widthCountArr);
  994. switch (opts.legend.float) {
  995. case 'left':
  996. legendData.area.start.x = opts.area[3];
  997. legendData.area.end.x = opts.area[3] + legendWidth + 2 * padding;
  998. break;
  999. case 'right':
  1000. legendData.area.start.x = opts.width - opts.area[1] - legendWidth - 2 * padding;
  1001. legendData.area.end.x = opts.width - opts.area[1];
  1002. break;
  1003. default:
  1004. legendData.area.start.x = (opts.width - legendWidth) / 2 - padding;
  1005. legendData.area.end.x = (opts.width + legendWidth) / 2 + padding;
  1006. }
  1007. legendData.area.width = legendWidth + 2 * padding;
  1008. legendData.area.wholeWidth = legendWidth + 2 * padding;
  1009. legendData.area.height = legendList.length * lineHeight + 2 * padding;
  1010. legendData.area.wholeHeight = legendList.length * lineHeight + 2 * padding + 2 * margin;
  1011. legendData.points = legendList;
  1012. }
  1013. } else {
  1014. let len = series.length;
  1015. let maxHeight = opts.height - opts.area[0] - opts.area[2] - 2 * margin - 2 * padding;
  1016. let maxLength = Math.min(Math.floor(maxHeight / lineHeight), len);
  1017. legendData.area.height = maxLength * lineHeight + padding * 2;
  1018. legendData.area.wholeHeight = maxLength * lineHeight + padding * 2;
  1019. switch (opts.legend.float) {
  1020. case 'top':
  1021. legendData.area.start.y = opts.area[0] + margin;
  1022. legendData.area.end.y = opts.area[0] + margin + legendData.area.height;
  1023. break;
  1024. case 'bottom':
  1025. legendData.area.start.y = opts.height - opts.area[2] - margin - legendData.area.height;
  1026. legendData.area.end.y = opts.height - opts.area[2] - margin;
  1027. break;
  1028. default:
  1029. legendData.area.start.y = (opts.height - legendData.area.height) / 2;
  1030. legendData.area.end.y = (opts.height + legendData.area.height) / 2;
  1031. }
  1032. let lineNum = len % maxLength === 0 ? len / maxLength : Math.floor((len / maxLength) + 1);
  1033. let currentRow = [];
  1034. for (let i = 0; i < lineNum; i++) {
  1035. let temp = series.slice(i * maxLength, i * maxLength + maxLength);
  1036. currentRow.push(temp);
  1037. }
  1038. legendData.points = currentRow;
  1039. if (currentRow.length) {
  1040. for (let i = 0; i < currentRow.length; i++) {
  1041. let item = currentRow[i];
  1042. let maxWidth = 0;
  1043. for (let j = 0; j < item.length; j++) {
  1044. let itemWidth = shapeWidth + shapeRight + measureText(item[j].name || 'undefined', fontSize, context) + opts.legend.itemGap * opts.pix;
  1045. if (itemWidth > maxWidth) {
  1046. maxWidth = itemWidth;
  1047. }
  1048. }
  1049. legendData.widthArr.push(maxWidth);
  1050. legendData.heightArr.push(item.length * lineHeight + padding * 2);
  1051. }
  1052. let legendWidth = 0
  1053. for (let i = 0; i < legendData.widthArr.length; i++) {
  1054. legendWidth += legendData.widthArr[i];
  1055. }
  1056. legendData.area.width = legendWidth - opts.legend.itemGap * opts.pix + 2 * padding;
  1057. legendData.area.wholeWidth = legendData.area.width + padding;
  1058. }
  1059. }
  1060. switch (opts.legend.position) {
  1061. case 'top':
  1062. legendData.area.start.y = opts.area[0] + margin;
  1063. legendData.area.end.y = opts.area[0] + margin + legendData.area.height;
  1064. break;
  1065. case 'bottom':
  1066. legendData.area.start.y = opts.height - opts.area[2] - legendData.area.height - margin;
  1067. legendData.area.end.y = opts.height - opts.area[2] - margin;
  1068. break;
  1069. case 'left':
  1070. legendData.area.start.x = opts.area[3];
  1071. legendData.area.end.x = opts.area[3] + legendData.area.width;
  1072. break;
  1073. case 'right':
  1074. legendData.area.start.x = opts.width - opts.area[1] - legendData.area.width;
  1075. legendData.area.end.x = opts.width - opts.area[1];
  1076. break;
  1077. }
  1078. chartData.legendData = legendData;
  1079. return legendData;
  1080. }
  1081. function calCategoriesData(categories, opts, config, eachSpacing, context) {
  1082. var result = {
  1083. angle: 0,
  1084. xAxisHeight: config.xAxisHeight
  1085. };
  1086. var categoriesTextLenth = categories.map(function(item) {
  1087. return measureText(item, opts.xAxis.fontSize * opts.pix || config.fontSize, context);
  1088. });
  1089. var maxTextLength = Math.max.apply(this, categoriesTextLenth);
  1090. if (opts.xAxis.rotateLabel == true && maxTextLength + 2 * config.xAxisTextPadding > eachSpacing) {
  1091. result.angle = 45 * Math.PI / 180;
  1092. result.xAxisHeight = 2 * config.xAxisTextPadding + maxTextLength * Math.sin(result.angle);
  1093. }
  1094. return result;
  1095. }
  1096. function getXAxisTextList(series, opts, config, stack) {
  1097. var index = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : -1;
  1098. var data;
  1099. if (stack == 'stack') {
  1100. data = dataCombineStack(series, opts.categories.length);
  1101. } else {
  1102. data = dataCombine(series);
  1103. }
  1104. var sorted = [];
  1105. // remove null from data
  1106. data = data.filter(function(item) {
  1107. //return item !== null;
  1108. if (typeof item === 'object' && item !== null) {
  1109. if (item.constructor.toString().indexOf('Array') > -1) {
  1110. return item !== null;
  1111. } else {
  1112. return item.value !== null;
  1113. }
  1114. } else {
  1115. return item !== null;
  1116. }
  1117. });
  1118. data.map(function(item) {
  1119. if (typeof item === 'object') {
  1120. if (item.constructor.toString().indexOf('Array') > -1) {
  1121. if (opts.type == 'candle') {
  1122. item.map(function(subitem) {
  1123. sorted.push(subitem);
  1124. })
  1125. } else {
  1126. sorted.push(item[0]);
  1127. }
  1128. } else {
  1129. sorted.push(item.value);
  1130. }
  1131. } else {
  1132. sorted.push(item);
  1133. }
  1134. })
  1135. var minData = 0;
  1136. var maxData = 0;
  1137. if (sorted.length > 0) {
  1138. minData = Math.min.apply(this, sorted);
  1139. maxData = Math.max.apply(this, sorted);
  1140. }
  1141. //为了兼容v1.9.0之前的项目
  1142. if (index > -1) {
  1143. if (typeof opts.xAxis.data[index].min === 'number') {
  1144. minData = Math.min(opts.xAxis.data[index].min, minData);
  1145. }
  1146. if (typeof opts.xAxis.data[index].max === 'number') {
  1147. maxData = Math.max(opts.xAxis.data[index].max, maxData);
  1148. }
  1149. } else {
  1150. if (typeof opts.xAxis.min === 'number') {
  1151. minData = Math.min(opts.xAxis.min, minData);
  1152. }
  1153. if (typeof opts.xAxis.max === 'number') {
  1154. maxData = Math.max(opts.xAxis.max, maxData);
  1155. }
  1156. }
  1157. if (minData === maxData) {
  1158. var rangeSpan = maxData || 10;
  1159. maxData += rangeSpan;
  1160. }
  1161. //var dataRange = getDataRange(minData, maxData);
  1162. var minRange = minData;
  1163. var maxRange = maxData;
  1164. var range = [];
  1165. var eachRange = (maxRange - minRange) / opts.xAxis.splitNumber;
  1166. for (var i = 0; i <= opts.xAxis.splitNumber; i++) {
  1167. range.push(minRange + eachRange * i);
  1168. }
  1169. return range;
  1170. }
  1171. function calXAxisData(series, opts, config, context) {
  1172. //堆叠图重算Y轴
  1173. var columnstyle = assign({}, {
  1174. type: ""
  1175. }, opts.extra.bar);
  1176. var result = {
  1177. angle: 0,
  1178. xAxisHeight: config.xAxisHeight
  1179. };
  1180. result.ranges = getXAxisTextList(series, opts, config, columnstyle.type);
  1181. result.rangesFormat = result.ranges.map(function(item) {
  1182. //item = opts.xAxis.formatter ? opts.xAxis.formatter(item) : util.toFixed(item, 2);
  1183. item = util.toFixed(item, 2);
  1184. return item;
  1185. });
  1186. var xAxisScaleValues = result.ranges.map(function(item) {
  1187. // 如果刻度值是浮点数,则保留两位小数
  1188. item = util.toFixed(item, 2);
  1189. // 若有自定义格式则调用自定义的格式化函数
  1190. //item = opts.xAxis.formatter ? opts.xAxis.formatter(Number(item)) : item;
  1191. return item;
  1192. });
  1193. result = Object.assign(result, getXAxisPoints(xAxisScaleValues, opts, config));
  1194. // 计算X轴刻度的属性譬如每个刻度的间隔,刻度的起始点\结束点以及总长
  1195. var eachSpacing = result.eachSpacing;
  1196. var textLength = xAxisScaleValues.map(function(item) {
  1197. return measureText(item, opts.xAxis.fontSize * opts.pix || config.fontSize, context);
  1198. });
  1199. // get max length of categories text
  1200. var maxTextLength = Math.max.apply(this, textLength);
  1201. // 如果刻度值文本内容过长,则将其逆时针旋转45°
  1202. if (maxTextLength + 2 * config.xAxisTextPadding > eachSpacing) {
  1203. result.angle = 45 * Math.PI / 180;
  1204. result.xAxisHeight = 2 * config.xAxisTextPadding + maxTextLength * Math.sin(result.angle);
  1205. }
  1206. if (opts.xAxis.disabled === true) {
  1207. result.xAxisHeight = 0;
  1208. }
  1209. return result;
  1210. }
  1211. function getRadarDataPoints(angleList, center, radius, series, opts) {
  1212. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  1213. var radarOption = opts.extra.radar || {};
  1214. radarOption.max = radarOption.max || 0;
  1215. var maxData = Math.max(radarOption.max, Math.max.apply(null, dataCombine(series)));
  1216. var data = [];
  1217. for (let i = 0; i < series.length; i++) {
  1218. let each = series[i];
  1219. let listItem = {};
  1220. listItem.color = each.color;
  1221. listItem.legendShape = each.legendShape;
  1222. listItem.pointShape = each.pointShape;
  1223. listItem.data = [];
  1224. each.data.forEach(function(item, index) {
  1225. let tmp = {};
  1226. tmp.angle = angleList[index];
  1227. tmp.proportion = item / maxData;
  1228. tmp.value = item;
  1229. tmp.position = convertCoordinateOrigin(radius * tmp.proportion * process * Math.cos(tmp.angle), radius * tmp.proportion * process * Math.sin(tmp.angle), center);
  1230. listItem.data.push(tmp);
  1231. });
  1232. data.push(listItem);
  1233. }
  1234. return data;
  1235. }
  1236. function getPieDataPoints(series, radius) {
  1237. var process = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  1238. var count = 0;
  1239. var _start_ = 0;
  1240. for (let i = 0; i < series.length; i++) {
  1241. let item = series[i];
  1242. item.data = item.data === null ? 0 : item.data;
  1243. count += item.data;
  1244. }
  1245. for (let i = 0; i < series.length; i++) {
  1246. let item = series[i];
  1247. item.data = item.data === null ? 0 : item.data;
  1248. if (count === 0) {
  1249. item._proportion_ = 1 / series.length * process;
  1250. } else {
  1251. item._proportion_ = item.data / count * process;
  1252. }
  1253. item._radius_ = radius;
  1254. }
  1255. for (let i = 0; i < series.length; i++) {
  1256. let item = series[i];
  1257. item._start_ = _start_;
  1258. _start_ += 2 * item._proportion_ * Math.PI;
  1259. }
  1260. return series;
  1261. }
  1262. function getFunnelDataPoints(series, radius, type, eachSpacing) {
  1263. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  1264. series = series.sort(function(a, b) {
  1265. return parseInt(b.data) - parseInt(a.data);
  1266. });
  1267. for (let i = 0; i < series.length; i++) {
  1268. if(type == 'funnel'){
  1269. series[i].radius = series[i].data / series[0].data * radius * process;
  1270. }else{
  1271. series[i].radius = (eachSpacing * (series.length - i)) / (eachSpacing * series.length) * radius * process;
  1272. }
  1273. series[i]._proportion_ = series[i].data / series[0].data;
  1274. }
  1275. if(type !== 'pyramid'){
  1276. series.reverse();
  1277. }
  1278. return series;
  1279. }
  1280. function getRoseDataPoints(series, type, minRadius, radius) {
  1281. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  1282. var count = 0;
  1283. var _start_ = 0;
  1284. var dataArr = [];
  1285. for (let i = 0; i < series.length; i++) {
  1286. let item = series[i];
  1287. item.data = item.data === null ? 0 : item.data;
  1288. count += item.data;
  1289. dataArr.push(item.data);
  1290. }
  1291. var minData = Math.min.apply(null, dataArr);
  1292. var maxData = Math.max.apply(null, dataArr);
  1293. var radiusLength = radius - minRadius;
  1294. for (let i = 0; i < series.length; i++) {
  1295. let item = series[i];
  1296. item.data = item.data === null ? 0 : item.data;
  1297. if (count === 0) {
  1298. item._proportion_ = 1 / series.length * process;
  1299. item._rose_proportion_ = 1 / series.length * process;
  1300. } else {
  1301. item._proportion_ = item.data / count * process;
  1302. if(type == 'area'){
  1303. item._rose_proportion_ = 1 / series.length * process;
  1304. }else{
  1305. item._rose_proportion_ = item.data / count * process;
  1306. }
  1307. }
  1308. item._radius_ = minRadius + radiusLength * ((item.data - minData) / (maxData - minData)) || radius;
  1309. }
  1310. for (let i = 0; i < series.length; i++) {
  1311. let item = series[i];
  1312. item._start_ = _start_;
  1313. _start_ += 2 * item._rose_proportion_ * Math.PI;
  1314. }
  1315. return series;
  1316. }
  1317. function getArcbarDataPoints(series, arcbarOption) {
  1318. var process = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  1319. if (process == 1) {
  1320. process = 0.999999;
  1321. }
  1322. for (let i = 0; i < series.length; i++) {
  1323. let item = series[i];
  1324. item.data = item.data === null ? 0 : item.data;
  1325. let totalAngle;
  1326. if (arcbarOption.type == 'circle') {
  1327. totalAngle = 2;
  1328. } else {
  1329. if (arcbarOption.endAngle < arcbarOption.startAngle) {
  1330. totalAngle = 2 + arcbarOption.endAngle - arcbarOption.startAngle;
  1331. } else {
  1332. totalAngle = arcbarOption.startAngle - arcbarOption.endAngle;
  1333. }
  1334. }
  1335. item._proportion_ = totalAngle * item.data * process + arcbarOption.startAngle;
  1336. if (item._proportion_ >= 2) {
  1337. item._proportion_ = item._proportion_ % 2;
  1338. }
  1339. }
  1340. return series;
  1341. }
  1342. function getGaugeAxisPoints(categories, startAngle, endAngle) {
  1343. let totalAngle = startAngle - endAngle + 1;
  1344. let tempStartAngle = startAngle;
  1345. for (let i = 0; i < categories.length; i++) {
  1346. categories[i].value = categories[i].value === null ? 0 : categories[i].value;
  1347. categories[i]._startAngle_ = tempStartAngle;
  1348. categories[i]._endAngle_ = totalAngle * categories[i].value + startAngle;
  1349. if (categories[i]._endAngle_ >= 2) {
  1350. categories[i]._endAngle_ = categories[i]._endAngle_ % 2;
  1351. }
  1352. tempStartAngle = categories[i]._endAngle_;
  1353. }
  1354. return categories;
  1355. }
  1356. function getGaugeDataPoints(series, categories, gaugeOption) {
  1357. let process = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
  1358. for (let i = 0; i < series.length; i++) {
  1359. let item = series[i];
  1360. item.data = item.data === null ? 0 : item.data;
  1361. if (gaugeOption.pointer.color == 'auto') {
  1362. for (let i = 0; i < categories.length; i++) {
  1363. if (item.data <= categories[i].value) {
  1364. item.color = categories[i].color;
  1365. break;
  1366. }
  1367. }
  1368. } else {
  1369. item.color = gaugeOption.pointer.color;
  1370. }
  1371. let totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  1372. item._endAngle_ = totalAngle * item.data + gaugeOption.startAngle;
  1373. item._oldAngle_ = gaugeOption.oldAngle;
  1374. if (gaugeOption.oldAngle < gaugeOption.endAngle) {
  1375. item._oldAngle_ += 2;
  1376. }
  1377. if (item.data >= gaugeOption.oldData) {
  1378. item._proportion_ = (item._endAngle_ - item._oldAngle_) * process + gaugeOption.oldAngle;
  1379. } else {
  1380. item._proportion_ = item._oldAngle_ - (item._oldAngle_ - item._endAngle_) * process;
  1381. }
  1382. if (item._proportion_ >= 2) {
  1383. item._proportion_ = item._proportion_ % 2;
  1384. }
  1385. }
  1386. return series;
  1387. }
  1388. function getPieTextMaxLength(series, config, context, opts) {
  1389. series = getPieDataPoints(series);
  1390. let maxLength = 0;
  1391. for (let i = 0; i < series.length; i++) {
  1392. let item = series[i];
  1393. let text = item.formatter ? item.formatter(+item._proportion_.toFixed(2)) : util.toFixed(item._proportion_ * 100) + '%';
  1394. maxLength = Math.max(maxLength, measureText(text, item.textSize * opts.pix || config.fontSize, context));
  1395. }
  1396. return maxLength;
  1397. }
  1398. function fixColumeData(points, eachSpacing, columnLen, index, config, opts) {
  1399. return points.map(function(item) {
  1400. if (item === null) {
  1401. return null;
  1402. }
  1403. var seriesGap = 0;
  1404. var categoryGap = 0;
  1405. if (opts.type == 'mix') {
  1406. seriesGap = opts.extra.mix.column.seriesGap * opts.pix || 0;
  1407. categoryGap = opts.extra.mix.column.categoryGap * opts.pix || 0;
  1408. } else {
  1409. seriesGap = opts.extra.column.seriesGap * opts.pix || 0;
  1410. categoryGap = opts.extra.column.categoryGap * opts.pix || 0;
  1411. }
  1412. seriesGap = Math.min(seriesGap, eachSpacing / columnLen)
  1413. categoryGap = Math.min(categoryGap, eachSpacing / columnLen)
  1414. item.width = Math.ceil((eachSpacing - 2 * categoryGap - seriesGap * (columnLen - 1)) / columnLen);
  1415. if (opts.extra.mix && opts.extra.mix.column.width && +opts.extra.mix.column.width > 0) {
  1416. item.width = Math.min(item.width, +opts.extra.mix.column.width * opts.pix);
  1417. }
  1418. if (opts.extra.column && opts.extra.column.width && +opts.extra.column.width > 0) {
  1419. item.width = Math.min(item.width, +opts.extra.column.width * opts.pix);
  1420. }
  1421. if (item.width <= 0) {
  1422. item.width = 1;
  1423. }
  1424. item.x += (index + 0.5 - columnLen / 2) * (item.width + seriesGap);
  1425. return item;
  1426. });
  1427. }
  1428. function fixBarData(points, eachSpacing, columnLen, index, config, opts) {
  1429. return points.map(function(item) {
  1430. if (item === null) {
  1431. return null;
  1432. }
  1433. var seriesGap = 0;
  1434. var categoryGap = 0;
  1435. seriesGap = opts.extra.bar.seriesGap * opts.pix || 0;
  1436. categoryGap = opts.extra.bar.categoryGap * opts.pix || 0;
  1437. seriesGap = Math.min(seriesGap, eachSpacing / columnLen)
  1438. categoryGap = Math.min(categoryGap, eachSpacing / columnLen)
  1439. item.width = Math.ceil((eachSpacing - 2 * categoryGap - seriesGap * (columnLen - 1)) / columnLen);
  1440. if (opts.extra.bar && opts.extra.bar.width && +opts.extra.bar.width > 0) {
  1441. item.width = Math.min(item.width, +opts.extra.bar.width * opts.pix);
  1442. }
  1443. if (item.width <= 0) {
  1444. item.width = 1;
  1445. }
  1446. item.y += (index + 0.5 - columnLen / 2) * (item.width + seriesGap);
  1447. return item;
  1448. });
  1449. }
  1450. function fixColumeMeterData(points, eachSpacing, columnLen, index, config, opts, border) {
  1451. var categoryGap = opts.extra.column.categoryGap * opts.pix || 0;
  1452. return points.map(function(item) {
  1453. if (item === null) {
  1454. return null;
  1455. }
  1456. item.width = Math.ceil(eachSpacing - 2 * categoryGap);
  1457. if (opts.extra.column && opts.extra.column.width && +opts.extra.column.width > 0) {
  1458. item.width = Math.min(item.width, +opts.extra.column.width * opts.pix);
  1459. }
  1460. if (index > 0) {
  1461. item.width -= 2 * border;
  1462. }
  1463. return item;
  1464. });
  1465. }
  1466. function fixColumeStackData(points, eachSpacing, columnLen, index, config, opts, series) {
  1467. var categoryGap = opts.extra.column.categoryGap * opts.pix || 0;
  1468. return points.map(function(item, indexn) {
  1469. if (item === null) {
  1470. return null;
  1471. }
  1472. item.width = Math.ceil(eachSpacing - 2 * categoryGap);
  1473. if (opts.extra.column && opts.extra.column.width && +opts.extra.column.width > 0) {
  1474. item.width = Math.min(item.width, +opts.extra.column.width * opts.pix);
  1475. }
  1476. if (item.width <= 0) {
  1477. item.width = 1;
  1478. }
  1479. return item;
  1480. });
  1481. }
  1482. function fixBarStackData(points, eachSpacing, columnLen, index, config, opts, series) {
  1483. var categoryGap = opts.extra.bar.categoryGap * opts.pix || 0;
  1484. return points.map(function(item, indexn) {
  1485. if (item === null) {
  1486. return null;
  1487. }
  1488. item.width = Math.ceil(eachSpacing - 2 * categoryGap);
  1489. if (opts.extra.bar && opts.extra.bar.width && +opts.extra.bar.width > 0) {
  1490. item.width = Math.min(item.width, +opts.extra.bar.width * opts.pix);
  1491. }
  1492. if (item.width <= 0) {
  1493. item.width = 1;
  1494. }
  1495. return item;
  1496. });
  1497. }
  1498. function getXAxisPoints(categories, opts, config) {
  1499. var spacingValid = opts.width - opts.area[1] - opts.area[3];
  1500. var dataCount = opts.enableScroll ? Math.min(opts.xAxis.itemCount, categories.length) : categories.length;
  1501. if ((opts.type == 'line' || opts.type == 'area' || opts.type == 'scatter' || opts.type == 'bubble' || opts.type == 'bar') && dataCount > 1 && opts.xAxis.boundaryGap == 'justify') {
  1502. dataCount -= 1;
  1503. }
  1504. var eachSpacing = spacingValid / dataCount;
  1505. var xAxisPoints = [];
  1506. var startX = opts.area[3];
  1507. var endX = opts.width - opts.area[1];
  1508. categories.forEach(function(item, index) {
  1509. xAxisPoints.push(startX + index * eachSpacing);
  1510. });
  1511. if (opts.xAxis.boundaryGap !== 'justify') {
  1512. if (opts.enableScroll === true) {
  1513. xAxisPoints.push(startX + categories.length * eachSpacing);
  1514. } else {
  1515. xAxisPoints.push(endX);
  1516. }
  1517. }
  1518. return {
  1519. xAxisPoints: xAxisPoints,
  1520. startX: startX,
  1521. endX: endX,
  1522. eachSpacing: eachSpacing
  1523. };
  1524. }
  1525. function getCandleDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config) {
  1526. var process = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 1;
  1527. var points = [];
  1528. var validHeight = opts.height - opts.area[0] - opts.area[2];
  1529. data.forEach(function(item, index) {
  1530. if (item === null) {
  1531. points.push(null);
  1532. } else {
  1533. var cPoints = [];
  1534. item.forEach(function(items, indexs) {
  1535. var point = {};
  1536. point.x = xAxisPoints[index] + Math.round(eachSpacing / 2);
  1537. var value = items.value || items;
  1538. var height = validHeight * (value - minRange) / (maxRange - minRange);
  1539. height *= process;
  1540. point.y = opts.height - Math.round(height) - opts.area[2];
  1541. cPoints.push(point);
  1542. });
  1543. points.push(cPoints);
  1544. }
  1545. });
  1546. return points;
  1547. }
  1548. function getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config) {
  1549. var process = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 1;
  1550. var boundaryGap = 'center';
  1551. if (opts.type == 'line' || opts.type == 'area' || opts.type == 'scatter' || opts.type == 'bubble' ) {
  1552. boundaryGap = opts.xAxis.boundaryGap;
  1553. }
  1554. var points = [];
  1555. var validHeight = opts.height - opts.area[0] - opts.area[2];
  1556. var validWidth = opts.width - opts.area[1] - opts.area[3];
  1557. data.forEach(function(item, index) {
  1558. if (item === null) {
  1559. points.push(null);
  1560. } else {
  1561. var point = {};
  1562. point.color = item.color;
  1563. point.x = xAxisPoints[index];
  1564. var value = item;
  1565. if (typeof item === 'object' && item !== null) {
  1566. if (item.constructor.toString().indexOf('Array') > -1) {
  1567. let xranges, xminRange, xmaxRange;
  1568. xranges = [].concat(opts.chartData.xAxisData.ranges);
  1569. xminRange = xranges.shift();
  1570. xmaxRange = xranges.pop();
  1571. value = item[1];
  1572. point.x = opts.area[3] + validWidth * (item[0] - xminRange) / (xmaxRange - xminRange);
  1573. if(opts.type == 'bubble'){
  1574. point.r = item[2];
  1575. point.t = item[3];
  1576. }
  1577. } else {
  1578. value = item.value;
  1579. }
  1580. }
  1581. if (boundaryGap == 'center') {
  1582. point.x += eachSpacing / 2;
  1583. }
  1584. var height = validHeight * (value - minRange) / (maxRange - minRange);
  1585. height *= process;
  1586. point.y = opts.height - height - opts.area[2];
  1587. points.push(point);
  1588. }
  1589. });
  1590. return points;
  1591. }
  1592. function getBarDataPoints(data, minRange, maxRange, yAxisPoints, eachSpacing, opts, config) {
  1593. var process = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 1;
  1594. var points = [];
  1595. var validHeight = opts.height - opts.area[0] - opts.area[2];
  1596. var validWidth = opts.width - opts.area[1] - opts.area[3];
  1597. data.forEach(function(item, index) {
  1598. if (item === null) {
  1599. points.push(null);
  1600. } else {
  1601. var point = {};
  1602. point.color = item.color;
  1603. point.y = yAxisPoints[index];
  1604. var value = item;
  1605. if (typeof item === 'object' && item !== null) {
  1606. value = item.value;
  1607. }
  1608. var height = validWidth * (value - minRange) / (maxRange - minRange);
  1609. height *= process;
  1610. point.height = height;
  1611. point.value = value;
  1612. point.x = height + opts.area[3];
  1613. points.push(point);
  1614. }
  1615. });
  1616. return points;
  1617. }
  1618. function getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, seriesIndex, stackSeries) {
  1619. var process = arguments.length > 9 && arguments[9] !== undefined ? arguments[9] : 1;
  1620. var points = [];
  1621. var validHeight = opts.height - opts.area[0] - opts.area[2];
  1622. data.forEach(function(item, index) {
  1623. if (item === null) {
  1624. points.push(null);
  1625. } else {
  1626. var point = {};
  1627. point.color = item.color;
  1628. point.x = xAxisPoints[index] + Math.round(eachSpacing / 2);
  1629. if (seriesIndex > 0) {
  1630. var value = 0;
  1631. for (let i = 0; i <= seriesIndex; i++) {
  1632. value += stackSeries[i].data[index];
  1633. }
  1634. var value0 = value - item;
  1635. var height = validHeight * (value - minRange) / (maxRange - minRange);
  1636. var height0 = validHeight * (value0 - minRange) / (maxRange - minRange);
  1637. } else {
  1638. var value = item;
  1639. var height = validHeight * (value - minRange) / (maxRange - minRange);
  1640. var height0 = 0;
  1641. }
  1642. var heightc = height0;
  1643. height *= process;
  1644. heightc *= process;
  1645. point.y = opts.height - Math.round(height) - opts.area[2];
  1646. point.y0 = opts.height - Math.round(heightc) - opts.area[2];
  1647. points.push(point);
  1648. }
  1649. });
  1650. return points;
  1651. }
  1652. function getBarStackDataPoints(data, minRange, maxRange, yAxisPoints, eachSpacing, opts, config, seriesIndex, stackSeries) {
  1653. var process = arguments.length > 9 && arguments[9] !== undefined ? arguments[9] : 1;
  1654. var points = [];
  1655. var validHeight = opts.width - opts.area[1] - opts.area[3];
  1656. data.forEach(function(item, index) {
  1657. if (item === null) {
  1658. points.push(null);
  1659. } else {
  1660. var point = {};
  1661. point.color = item.color;
  1662. point.y = yAxisPoints[index];
  1663. if (seriesIndex > 0) {
  1664. var value = 0;
  1665. for (let i = 0; i <= seriesIndex; i++) {
  1666. value += stackSeries[i].data[index];
  1667. }
  1668. var value0 = value - item;
  1669. var height = validHeight * (value - minRange) / (maxRange - minRange);
  1670. var height0 = validHeight * (value0 - minRange) / (maxRange - minRange);
  1671. } else {
  1672. var value = item;
  1673. var height = validHeight * (value - minRange) / (maxRange - minRange);
  1674. var height0 = 0;
  1675. }
  1676. var heightc = height0;
  1677. height *= process;
  1678. heightc *= process;
  1679. point.height = height - heightc;
  1680. point.x = opts.area[3] + height;
  1681. point.x0 = opts.area[3] + heightc;
  1682. points.push(point);
  1683. }
  1684. });
  1685. return points;
  1686. }
  1687. function getYAxisTextList(series, opts, config, stack, yData) {
  1688. var index = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : -1;
  1689. var data;
  1690. if (stack == 'stack') {
  1691. data = dataCombineStack(series, opts.categories.length);
  1692. } else {
  1693. data = dataCombine(series);
  1694. }
  1695. var sorted = [];
  1696. // remove null from data
  1697. data = data.filter(function(item) {
  1698. //return item !== null;
  1699. if (typeof item === 'object' && item !== null) {
  1700. if (item.constructor.toString().indexOf('Array') > -1) {
  1701. return item !== null;
  1702. } else {
  1703. return item.value !== null;
  1704. }
  1705. } else {
  1706. return item !== null;
  1707. }
  1708. });
  1709. data.map(function(item) {
  1710. if (typeof item === 'object') {
  1711. if (item.constructor.toString().indexOf('Array') > -1) {
  1712. if (opts.type == 'candle') {
  1713. item.map(function(subitem) {
  1714. sorted.push(subitem);
  1715. })
  1716. } else {
  1717. sorted.push(item[1]);
  1718. }
  1719. } else {
  1720. sorted.push(item.value);
  1721. }
  1722. } else {
  1723. sorted.push(item);
  1724. }
  1725. })
  1726. var minData = yData.min || 0;
  1727. var maxData = yData.max || 0;
  1728. if (sorted.length > 0) {
  1729. minData = Math.min.apply(this, sorted);
  1730. maxData = Math.max.apply(this, sorted);
  1731. }
  1732. //为了兼容v1.9.0之前的项目
  1733. // if (index > -1) {
  1734. // if (typeof opts.yAxis.data[index].min === 'number') {
  1735. // minData = Math.min(opts.yAxis.data[index].min, minData);
  1736. // }
  1737. // if (typeof opts.yAxis.data[index].max === 'number') {
  1738. // maxData = Math.max(opts.yAxis.data[index].max, maxData);
  1739. // }
  1740. // } else {
  1741. // if (typeof opts.yAxis.min === 'number') {
  1742. // minData = Math.min(opts.yAxis.min, minData);
  1743. // }
  1744. // if (typeof opts.yAxis.max === 'number') {
  1745. // maxData = Math.max(opts.yAxis.max, maxData);
  1746. // }
  1747. // }
  1748. if (minData === maxData) {
  1749. var rangeSpan = maxData || 10;
  1750. maxData += rangeSpan;
  1751. }
  1752. var dataRange = getDataRange(minData, maxData);
  1753. var minRange = yData.min === undefined || yData.min === null ? dataRange.minRange : yData.min;
  1754. var maxRange = yData.max === undefined || yData.min === null ? dataRange.maxRange : yData.max;
  1755. var range = [];
  1756. var eachRange = (maxRange - minRange) / opts.yAxis.splitNumber;
  1757. for (var i = 0; i <= opts.yAxis.splitNumber; i++) {
  1758. range.push(minRange + eachRange * i);
  1759. }
  1760. return range.reverse();
  1761. }
  1762. function calYAxisData(series, opts, config, context) {
  1763. //堆叠图重算Y轴
  1764. var columnstyle = assign({}, {
  1765. type: ""
  1766. }, opts.extra.column);
  1767. //如果是多Y轴,重新计算
  1768. var YLength = opts.yAxis.data.length;
  1769. var newSeries = new Array(YLength);
  1770. if (YLength > 0) {
  1771. for (let i = 0; i < YLength; i++) {
  1772. newSeries[i] = [];
  1773. for (let j = 0; j < series.length; j++) {
  1774. if (series[j].index == i) {
  1775. newSeries[i].push(series[j]);
  1776. }
  1777. }
  1778. }
  1779. var rangesArr = new Array(YLength);
  1780. var rangesFormatArr = new Array(YLength);
  1781. var yAxisWidthArr = new Array(YLength);
  1782. for (let i = 0; i < YLength; i++) {
  1783. let yData = opts.yAxis.data[i];
  1784. //如果总开关不显示,强制每个Y轴为不显示
  1785. if (opts.yAxis.disabled == true) {
  1786. yData.disabled = true;
  1787. }
  1788. if(yData.type === 'categories'){
  1789. if(!yData.formatter){
  1790. yData.formatter = (val) => {return val + (yData.unit || '')};
  1791. }
  1792. yData.categories = yData.categories || opts.categories;
  1793. rangesArr[i] = yData.categories;
  1794. }else{
  1795. if(!yData.formatter){
  1796. yData.formatter = (val) => {return val.toFixed(yData.tofix) + (yData.unit || '')};
  1797. }
  1798. rangesArr[i] = getYAxisTextList(newSeries[i], opts, config, columnstyle.type, yData, i);
  1799. }
  1800. let yAxisFontSizes = yData.fontSize * opts.pix || config.fontSize;
  1801. yAxisWidthArr[i] = {
  1802. position: yData.position ? yData.position : 'left',
  1803. width: 0
  1804. };
  1805. rangesFormatArr[i] = rangesArr[i].map(function(items) {
  1806. items = yData.formatter(items);
  1807. yAxisWidthArr[i].width = Math.max(yAxisWidthArr[i].width, measureText(items, yAxisFontSizes, context) + 5);
  1808. return items;
  1809. });
  1810. let calibration = yData.calibration ? 4 * opts.pix : 0;
  1811. yAxisWidthArr[i].width += calibration + 3 * opts.pix;
  1812. if (yData.disabled === true) {
  1813. yAxisWidthArr[i].width = 0;
  1814. }
  1815. }
  1816. } else {
  1817. var rangesArr = new Array(1);
  1818. var rangesFormatArr = new Array(1);
  1819. var yAxisWidthArr = new Array(1);
  1820. if(opts.type === 'bar'){
  1821. rangesArr[0] = opts.categories;
  1822. if(!opts.yAxis.formatter){
  1823. opts.yAxis.formatter = (val) => {return val + (opts.yAxis.unit || '')}
  1824. }
  1825. }else{
  1826. if(!opts.yAxis.formatter){
  1827. opts.yAxis.formatter = (val) => {return val.toFixed(opts.yAxis.tofix ) + (opts.yAxis.unit || '')}
  1828. }
  1829. rangesArr[0] = getYAxisTextList(series, opts, config, columnstyle.type, {});
  1830. }
  1831. yAxisWidthArr[0] = {
  1832. position: 'left',
  1833. width: 0
  1834. };
  1835. var yAxisFontSize = opts.yAxis.fontSize * opts.pix || config.fontSize;
  1836. rangesFormatArr[0] = rangesArr[0].map(function(item) {
  1837. item = opts.yAxis.formatter(item);
  1838. yAxisWidthArr[0].width = Math.max(yAxisWidthArr[0].width, measureText(item, yAxisFontSize, context) + 5);
  1839. return item;
  1840. });
  1841. yAxisWidthArr[0].width += 3 * opts.pix;
  1842. if (opts.yAxis.disabled === true) {
  1843. yAxisWidthArr[0] = {
  1844. position: 'left',
  1845. width: 0
  1846. };
  1847. opts.yAxis.data[0] = {
  1848. disabled: true
  1849. };
  1850. } else {
  1851. opts.yAxis.data[0] = {
  1852. disabled: false,
  1853. position: 'left',
  1854. max: opts.yAxis.max,
  1855. min: opts.yAxis.min,
  1856. formatter: opts.yAxis.formatter
  1857. };
  1858. if(opts.type === 'bar'){
  1859. opts.yAxis.data[0].categories = opts.categories;
  1860. opts.yAxis.data[0].type = 'categories';
  1861. }
  1862. }
  1863. }
  1864. return {
  1865. rangesFormat: rangesFormatArr,
  1866. ranges: rangesArr,
  1867. yAxisWidth: yAxisWidthArr
  1868. };
  1869. }
  1870. function calTooltipYAxisData(point, series, opts, config, eachSpacing) {
  1871. let ranges = [].concat(opts.chartData.yAxisData.ranges);
  1872. let spacingValid = opts.height - opts.area[0] - opts.area[2];
  1873. let minAxis = opts.area[0];
  1874. let items = [];
  1875. for (let i = 0; i < ranges.length; i++) {
  1876. let maxVal = ranges[i].shift();
  1877. let minVal = ranges[i].pop();
  1878. let item = maxVal - (maxVal - minVal) * (point - minAxis) / spacingValid;
  1879. item = opts.yAxis.data[i].formatter ? opts.yAxis.data[i].formatter(item) : item.toFixed(0);
  1880. items.push(String(item))
  1881. }
  1882. return items;
  1883. }
  1884. function calMarkLineData(points, opts) {
  1885. let minRange, maxRange;
  1886. let spacingValid = opts.height - opts.area[0] - opts.area[2];
  1887. for (let i = 0; i < points.length; i++) {
  1888. points[i].yAxisIndex = points[i].yAxisIndex ? points[i].yAxisIndex : 0;
  1889. let range = [].concat(opts.chartData.yAxisData.ranges[points[i].yAxisIndex]);
  1890. minRange = range.pop();
  1891. maxRange = range.shift();
  1892. let height = spacingValid * (points[i].value - minRange) / (maxRange - minRange);
  1893. points[i].y = opts.height - Math.round(height) - opts.area[2];
  1894. }
  1895. return points;
  1896. }
  1897. function contextRotate(context, opts) {
  1898. if (opts.rotateLock !== true) {
  1899. context.translate(opts.height, 0);
  1900. context.rotate(90 * Math.PI / 180);
  1901. } else if (opts._rotate_ !== true) {
  1902. context.translate(opts.height, 0);
  1903. context.rotate(90 * Math.PI / 180);
  1904. opts._rotate_ = true;
  1905. }
  1906. }
  1907. function drawPointShape(points, color, shape, context, opts) {
  1908. context.beginPath();
  1909. if (opts.dataPointShapeType == 'hollow') {
  1910. context.setStrokeStyle(color);
  1911. context.setFillStyle(opts.background);
  1912. context.setLineWidth(2 * opts.pix);
  1913. } else {
  1914. context.setStrokeStyle("#ffffff");
  1915. context.setFillStyle(color);
  1916. context.setLineWidth(1 * opts.pix);
  1917. }
  1918. if (shape === 'diamond') {
  1919. points.forEach(function(item, index) {
  1920. if (item !== null) {
  1921. context.moveTo(item.x, item.y - 4.5);
  1922. context.lineTo(item.x - 4.5, item.y);
  1923. context.lineTo(item.x, item.y + 4.5);
  1924. context.lineTo(item.x + 4.5, item.y);
  1925. context.lineTo(item.x, item.y - 4.5);
  1926. }
  1927. });
  1928. } else if (shape === 'circle') {
  1929. points.forEach(function(item, index) {
  1930. if (item !== null) {
  1931. context.moveTo(item.x + 2.5 * opts.pix, item.y);
  1932. context.arc(item.x, item.y, 3 * opts.pix, 0, 2 * Math.PI, false);
  1933. }
  1934. });
  1935. } else if (shape === 'square') {
  1936. points.forEach(function(item, index) {
  1937. if (item !== null) {
  1938. context.moveTo(item.x - 3.5, item.y - 3.5);
  1939. context.rect(item.x - 3.5, item.y - 3.5, 7, 7);
  1940. }
  1941. });
  1942. } else if (shape === 'triangle') {
  1943. points.forEach(function(item, index) {
  1944. if (item !== null) {
  1945. context.moveTo(item.x, item.y - 4.5);
  1946. context.lineTo(item.x - 4.5, item.y + 4.5);
  1947. context.lineTo(item.x + 4.5, item.y + 4.5);
  1948. context.lineTo(item.x, item.y - 4.5);
  1949. }
  1950. });
  1951. } else if (shape === 'triangle') {
  1952. return;
  1953. }
  1954. context.closePath();
  1955. context.fill();
  1956. context.stroke();
  1957. }
  1958. function drawRingTitle(opts, config, context, center) {
  1959. var titlefontSize = opts.title.fontSize || config.titleFontSize;
  1960. var subtitlefontSize = opts.subtitle.fontSize || config.subtitleFontSize;
  1961. var title = opts.title.name || '';
  1962. var subtitle = opts.subtitle.name || '';
  1963. var titleFontColor = opts.title.color || opts.fontColor;
  1964. var subtitleFontColor = opts.subtitle.color || opts.fontColor;
  1965. var titleHeight = title ? titlefontSize : 0;
  1966. var subtitleHeight = subtitle ? subtitlefontSize : 0;
  1967. var margin = 5;
  1968. if (subtitle) {
  1969. var textWidth = measureText(subtitle, subtitlefontSize * opts.pix, context);
  1970. var startX = center.x - textWidth / 2 + (opts.subtitle.offsetX|| 0) * opts.pix ;
  1971. var startY = center.y + subtitlefontSize * opts.pix / 2 + (opts.subtitle.offsetY || 0) * opts.pix;
  1972. if (title) {
  1973. startY += (titleHeight * opts.pix + margin) / 2;
  1974. }
  1975. context.beginPath();
  1976. context.setFontSize(subtitlefontSize * opts.pix);
  1977. context.setFillStyle(subtitleFontColor);
  1978. context.fillText(subtitle, startX, startY);
  1979. context.closePath();
  1980. context.stroke();
  1981. }
  1982. if (title) {
  1983. var _textWidth = measureText(title, titlefontSize * opts.pix, context);
  1984. var _startX = center.x - _textWidth / 2 + (opts.title.offsetX || 0);
  1985. var _startY = center.y + titlefontSize * opts.pix / 2 + (opts.title.offsetY || 0) * opts.pix;
  1986. if (subtitle) {
  1987. _startY -= (subtitleHeight * opts.pix + margin) / 2;
  1988. }
  1989. context.beginPath();
  1990. context.setFontSize(titlefontSize * opts.pix);
  1991. context.setFillStyle(titleFontColor);
  1992. context.fillText(title, _startX, _startY);
  1993. context.closePath();
  1994. context.stroke();
  1995. }
  1996. }
  1997. function drawPointText(points, series, config, context, opts) {
  1998. // 绘制数据文案
  1999. var data = series.data;
  2000. var textOffset = series.textOffset ? series.textOffset : 0;
  2001. points.forEach(function(item, index) {
  2002. if (item !== null) {
  2003. context.beginPath();
  2004. var fontSize = series.textSize ? series.textSize * opts.pix : config.fontSize;
  2005. context.setFontSize(fontSize);
  2006. context.setFillStyle(series.textColor || opts.fontColor);
  2007. var value = data[index]
  2008. if (typeof data[index] === 'object' && data[index] !== null) {
  2009. if (data[index].constructor.toString().indexOf('Array')>-1) {
  2010. value = data[index][1];
  2011. } else {
  2012. value = data[index].value
  2013. }
  2014. }
  2015. var formatVal = series.formatter ? series.formatter(value,index) : value;
  2016. context.setTextAlign('center');
  2017. context.fillText(String(formatVal), item.x, item.y - 4 + textOffset * opts.pix);
  2018. context.closePath();
  2019. context.stroke();
  2020. context.setTextAlign('left');
  2021. }
  2022. });
  2023. }
  2024. function drawBarPointText(points, series, config, context, opts) {
  2025. // 绘制数据文案
  2026. var data = series.data;
  2027. var textOffset = series.textOffset ? series.textOffset : 0;
  2028. points.forEach(function(item, index) {
  2029. if (item !== null) {
  2030. context.beginPath();
  2031. var fontSize = series.textSize ? series.textSize * opts.pix : config.fontSize;
  2032. context.setFontSize(fontSize);
  2033. context.setFillStyle(series.textColor || opts.fontColor);
  2034. var value = data[index]
  2035. if (typeof data[index] === 'object' && data[index] !== null) {
  2036. value = data[index].value ;
  2037. }
  2038. var formatVal = series.formatter ? series.formatter(value,index) : value;
  2039. context.setTextAlign('left');
  2040. context.fillText(String(formatVal), item.x + 4 * opts.pix , item.y + fontSize / 2 - 3 );
  2041. context.closePath();
  2042. context.stroke();
  2043. }
  2044. });
  2045. }
  2046. function drawGaugeLabel(gaugeOption, radius, centerPosition, opts, config, context) {
  2047. radius -= gaugeOption.width / 2 + gaugeOption.labelOffset * opts.pix;
  2048. let totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  2049. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  2050. let totalNumber = gaugeOption.endNumber - gaugeOption.startNumber;
  2051. let splitNumber = totalNumber / gaugeOption.splitLine.splitNumber;
  2052. let nowAngle = gaugeOption.startAngle;
  2053. let nowNumber = gaugeOption.startNumber;
  2054. for (let i = 0; i < gaugeOption.splitLine.splitNumber + 1; i++) {
  2055. var pos = {
  2056. x: radius * Math.cos(nowAngle * Math.PI),
  2057. y: radius * Math.sin(nowAngle * Math.PI)
  2058. };
  2059. var labelText = gaugeOption.formatter ? gaugeOption.formatter(nowNumber) : nowNumber;
  2060. pos.x += centerPosition.x - measureText(labelText, config.fontSize, context) / 2;
  2061. pos.y += centerPosition.y;
  2062. var startX = pos.x;
  2063. var startY = pos.y;
  2064. context.beginPath();
  2065. context.setFontSize(config.fontSize);
  2066. context.setFillStyle(gaugeOption.labelColor || opts.fontColor);
  2067. context.fillText(labelText, startX, startY + config.fontSize / 2);
  2068. context.closePath();
  2069. context.stroke();
  2070. nowAngle += splitAngle;
  2071. if (nowAngle >= 2) {
  2072. nowAngle = nowAngle % 2;
  2073. }
  2074. nowNumber += splitNumber;
  2075. }
  2076. }
  2077. function drawRadarLabel(angleList, radius, centerPosition, opts, config, context) {
  2078. var radarOption = opts.extra.radar || {};
  2079. radius += config.radarLabelTextMargin * opts.pix;
  2080. angleList.forEach(function(angle, index) {
  2081. var pos = {
  2082. x: radius * Math.cos(angle),
  2083. y: radius * Math.sin(angle)
  2084. };
  2085. var posRelativeCanvas = convertCoordinateOrigin(pos.x, pos.y, centerPosition);
  2086. var startX = posRelativeCanvas.x;
  2087. var startY = posRelativeCanvas.y;
  2088. if (util.approximatelyEqual(pos.x, 0)) {
  2089. startX -= measureText(opts.categories[index] || '', config.fontSize, context) / 2;
  2090. } else if (pos.x < 0) {
  2091. startX -= measureText(opts.categories[index] || '', config.fontSize, context);
  2092. }
  2093. context.beginPath();
  2094. context.setFontSize(config.fontSize);
  2095. context.setFillStyle(radarOption.labelColor || opts.fontColor);
  2096. context.fillText(opts.categories[index] || '', startX, startY + config.fontSize / 2);
  2097. context.closePath();
  2098. context.stroke();
  2099. });
  2100. }
  2101. function drawPieText(series, opts, config, context, radius, center) {
  2102. var lineRadius = config.pieChartLinePadding;
  2103. var textObjectCollection = [];
  2104. var lastTextObject = null;
  2105. var seriesConvert = series.map(function(item,index,series) {
  2106. var text = item.formatter ? item.formatter(item,index,series) : util.toFixed(item._proportion_.toFixed(4) * 100) + '%';
  2107. var arc = 2 * Math.PI - (item._start_ + 2 * Math.PI * item._proportion_ / 2);
  2108. if (item._rose_proportion_) {
  2109. arc = 2 * Math.PI - (item._start_ + 2 * Math.PI * item._rose_proportion_ / 2);
  2110. }
  2111. var color = item.color;
  2112. var radius = item._radius_;
  2113. return {
  2114. arc: arc,
  2115. text: text,
  2116. color: color,
  2117. radius: radius,
  2118. textColor: item.textColor,
  2119. textSize: item.textSize,
  2120. };
  2121. });
  2122. for (let i = 0; i < seriesConvert.length; i++) {
  2123. let item = seriesConvert[i];
  2124. // line end
  2125. let orginX1 = Math.cos(item.arc) * (item.radius + lineRadius);
  2126. let orginY1 = Math.sin(item.arc) * (item.radius + lineRadius);
  2127. // line start
  2128. let orginX2 = Math.cos(item.arc) * item.radius;
  2129. let orginY2 = Math.sin(item.arc) * item.radius;
  2130. // text start
  2131. let orginX3 = orginX1 >= 0 ? orginX1 + config.pieChartTextPadding : orginX1 - config.pieChartTextPadding;
  2132. let orginY3 = orginY1;
  2133. let textWidth = measureText(item.text, item.textSize * opts.pix || config.fontSize, context);
  2134. let startY = orginY3;
  2135. if (lastTextObject && util.isSameXCoordinateArea(lastTextObject.start, {
  2136. x: orginX3
  2137. })) {
  2138. if (orginX3 > 0) {
  2139. startY = Math.min(orginY3, lastTextObject.start.y);
  2140. } else if (orginX1 < 0) {
  2141. startY = Math.max(orginY3, lastTextObject.start.y);
  2142. } else {
  2143. if (orginY3 > 0) {
  2144. startY = Math.max(orginY3, lastTextObject.start.y);
  2145. } else {
  2146. startY = Math.min(orginY3, lastTextObject.start.y);
  2147. }
  2148. }
  2149. }
  2150. if (orginX3 < 0) {
  2151. orginX3 -= textWidth;
  2152. }
  2153. let textObject = {
  2154. lineStart: {
  2155. x: orginX2,
  2156. y: orginY2
  2157. },
  2158. lineEnd: {
  2159. x: orginX1,
  2160. y: orginY1
  2161. },
  2162. start: {
  2163. x: orginX3,
  2164. y: startY
  2165. },
  2166. width: textWidth,
  2167. height: config.fontSize,
  2168. text: item.text,
  2169. color: item.color,
  2170. textColor: item.textColor,
  2171. textSize: item.textSize
  2172. };
  2173. lastTextObject = avoidCollision(textObject, lastTextObject);
  2174. textObjectCollection.push(lastTextObject);
  2175. }
  2176. for (let i = 0; i < textObjectCollection.length; i++) {
  2177. let item = textObjectCollection[i];
  2178. let lineStartPoistion = convertCoordinateOrigin(item.lineStart.x, item.lineStart.y, center);
  2179. let lineEndPoistion = convertCoordinateOrigin(item.lineEnd.x, item.lineEnd.y, center);
  2180. let textPosition = convertCoordinateOrigin(item.start.x, item.start.y, center);
  2181. context.setLineWidth(1 * opts.pix);
  2182. context.setFontSize(item.textSize * opts.pix || config.fontSize);
  2183. context.beginPath();
  2184. context.setStrokeStyle(item.color);
  2185. context.setFillStyle(item.color);
  2186. context.moveTo(lineStartPoistion.x, lineStartPoistion.y);
  2187. let curveStartX = item.start.x < 0 ? textPosition.x + item.width : textPosition.x;
  2188. let textStartX = item.start.x < 0 ? textPosition.x - 5 : textPosition.x + 5;
  2189. context.quadraticCurveTo(lineEndPoistion.x, lineEndPoistion.y, curveStartX, textPosition.y);
  2190. context.moveTo(lineStartPoistion.x, lineStartPoistion.y);
  2191. context.stroke();
  2192. context.closePath();
  2193. context.beginPath();
  2194. context.moveTo(textPosition.x + item.width, textPosition.y);
  2195. context.arc(curveStartX, textPosition.y, 2, 0, 2 * Math.PI);
  2196. context.closePath();
  2197. context.fill();
  2198. context.beginPath();
  2199. context.setFontSize(item.textSize * opts.pix || config.fontSize);
  2200. context.setFillStyle(item.textColor || opts.fontColor);
  2201. context.fillText(item.text, textStartX, textPosition.y + 3);
  2202. context.closePath();
  2203. context.stroke();
  2204. context.closePath();
  2205. }
  2206. }
  2207. function drawToolTipSplitLine(offsetX, opts, config, context) {
  2208. var toolTipOption = opts.extra.tooltip || {};
  2209. toolTipOption.gridType = toolTipOption.gridType == undefined ? 'solid' : toolTipOption.gridType;
  2210. toolTipOption.dashLength = toolTipOption.dashLength == undefined ? 4 : toolTipOption.dashLength;
  2211. var startY = opts.area[0];
  2212. var endY = opts.height - opts.area[2];
  2213. if (toolTipOption.gridType == 'dash') {
  2214. context.setLineDash([toolTipOption.dashLength, toolTipOption.dashLength]);
  2215. }
  2216. context.setStrokeStyle(toolTipOption.gridColor || '#cccccc');
  2217. context.setLineWidth(1 * opts.pix);
  2218. context.beginPath();
  2219. context.moveTo(offsetX, startY);
  2220. context.lineTo(offsetX, endY);
  2221. context.stroke();
  2222. context.setLineDash([]);
  2223. if (toolTipOption.xAxisLabel) {
  2224. let labelText = opts.categories[opts.tooltip.index];
  2225. context.setFontSize(config.fontSize);
  2226. let textWidth = measureText(labelText, config.fontSize, context);
  2227. let textX = offsetX - 0.5 * textWidth;
  2228. let textY = endY;
  2229. context.beginPath();
  2230. context.setFillStyle(hexToRgb(toolTipOption.labelBgColor || config.toolTipBackground, toolTipOption.labelBgOpacity || config.toolTipOpacity));
  2231. context.setStrokeStyle(toolTipOption.labelBgColor || config.toolTipBackground);
  2232. context.setLineWidth(1 * opts.pix);
  2233. context.rect(textX - config.toolTipPadding, textY, textWidth + 2 * config.toolTipPadding, config.fontSize + 2 * config.toolTipPadding);
  2234. context.closePath();
  2235. context.stroke();
  2236. context.fill();
  2237. context.beginPath();
  2238. context.setFontSize(config.fontSize);
  2239. context.setFillStyle(toolTipOption.labelFontColor || opts.fontColor);
  2240. context.fillText(String(labelText), textX, textY + config.toolTipPadding + config.fontSize);
  2241. context.closePath();
  2242. context.stroke();
  2243. }
  2244. }
  2245. function drawMarkLine(opts, config, context) {
  2246. let markLineOption = assign({}, {
  2247. type: 'solid',
  2248. dashLength: 4,
  2249. data: []
  2250. }, opts.extra.markLine);
  2251. let startX = opts.area[3];
  2252. let endX = opts.width - opts.area[1];
  2253. let points = calMarkLineData(markLineOption.data, opts);
  2254. for (let i = 0; i < points.length; i++) {
  2255. let item = assign({}, {
  2256. lineColor: '#DE4A42',
  2257. showLabel: false,
  2258. labelFontColor: '#666666',
  2259. labelBgColor: '#DFE8FF',
  2260. labelBgOpacity: 0.8,
  2261. yAxisIndex: 0
  2262. }, points[i]);
  2263. if (markLineOption.type == 'dash') {
  2264. context.setLineDash([markLineOption.dashLength, markLineOption.dashLength]);
  2265. }
  2266. context.setStrokeStyle(item.lineColor);
  2267. context.setLineWidth(1 * opts.pix);
  2268. context.beginPath();
  2269. context.moveTo(startX, item.y);
  2270. context.lineTo(endX, item.y);
  2271. context.stroke();
  2272. context.setLineDash([]);
  2273. if (item.showLabel) {
  2274. let labelText = opts.yAxis.formatter ? opts.yAxis.formatter(item.value) : item.value;
  2275. context.setFontSize(config.fontSize);
  2276. let textWidth = measureText(labelText, config.fontSize, context);
  2277. let yAxisWidth = opts.chartData.yAxisData.yAxisWidth[0].width;
  2278. let bgStartX = opts.area[3] - textWidth - config.toolTipPadding * 2;
  2279. let bgEndX = opts.area[3];
  2280. let bgWidth = bgEndX - bgStartX;
  2281. let textX = bgEndX - config.toolTipPadding;
  2282. let textY = item.y;
  2283. context.setFillStyle(hexToRgb(item.labelBgColor, item.labelBgOpacity));
  2284. context.setStrokeStyle(item.labelBgColor);
  2285. context.setLineWidth(1 * opts.pix);
  2286. context.beginPath();
  2287. context.rect(bgStartX, textY - 0.5 * config.fontSize - config.toolTipPadding, bgWidth, config.fontSize + 2 * config.toolTipPadding);
  2288. context.closePath();
  2289. context.stroke();
  2290. context.fill();
  2291. context.setFontSize(config.fontSize);
  2292. context.setTextAlign('right');
  2293. context.setFillStyle(item.labelFontColor);
  2294. context.fillText(String(labelText), textX, textY + 0.5 * config.fontSize);
  2295. context.stroke();
  2296. context.setTextAlign('left');
  2297. }
  2298. }
  2299. }
  2300. function drawToolTipHorizentalLine(opts, config, context, eachSpacing, xAxisPoints) {
  2301. var toolTipOption = assign({}, {
  2302. gridType: 'solid',
  2303. dashLength: 4
  2304. }, opts.extra.tooltip);
  2305. var startX = opts.area[3];
  2306. var endX = opts.width - opts.area[1];
  2307. if (toolTipOption.gridType == 'dash') {
  2308. context.setLineDash([toolTipOption.dashLength, toolTipOption.dashLength]);
  2309. }
  2310. context.setStrokeStyle(toolTipOption.gridColor || '#cccccc');
  2311. context.setLineWidth(1 * opts.pix);
  2312. context.beginPath();
  2313. context.moveTo(startX, opts.tooltip.offset.y);
  2314. context.lineTo(endX, opts.tooltip.offset.y);
  2315. context.stroke();
  2316. context.setLineDash([]);
  2317. if (toolTipOption.yAxisLabel) {
  2318. let labelText = calTooltipYAxisData(opts.tooltip.offset.y, opts.series, opts, config, eachSpacing);
  2319. let widthArr = opts.chartData.yAxisData.yAxisWidth;
  2320. let tStartLeft = opts.area[3];
  2321. let tStartRight = opts.width - opts.area[1];
  2322. for (let i = 0; i < labelText.length; i++) {
  2323. context.setFontSize(config.fontSize);
  2324. let textWidth = measureText(labelText[i], config.fontSize, context);
  2325. let bgStartX, bgEndX, bgWidth;
  2326. if (widthArr[i].position == 'left') {
  2327. bgStartX = tStartLeft - widthArr[i].width;
  2328. bgEndX = Math.max(bgStartX, bgStartX + textWidth + config.toolTipPadding * 2);
  2329. } else {
  2330. bgStartX = tStartRight;
  2331. bgEndX = Math.max(bgStartX + widthArr[i].width, bgStartX + textWidth + config.toolTipPadding * 2);
  2332. }
  2333. bgWidth = bgEndX - bgStartX;
  2334. let textX = bgStartX + (bgWidth - textWidth) / 2;
  2335. let textY = opts.tooltip.offset.y;
  2336. context.beginPath();
  2337. context.setFillStyle(hexToRgb(toolTipOption.labelBgColor || config.toolTipBackground, toolTipOption.labelBgOpacity || config.toolTipOpacity));
  2338. context.setStrokeStyle(toolTipOption.labelBgColor || config.toolTipBackground);
  2339. context.setLineWidth(1 * opts.pix);
  2340. context.rect(bgStartX, textY - 0.5 * config.fontSize - config.toolTipPadding, bgWidth, config.fontSize + 2 *
  2341. config.toolTipPadding);
  2342. context.closePath();
  2343. context.stroke();
  2344. context.fill();
  2345. context.beginPath();
  2346. context.setFontSize(config.fontSize);
  2347. context.setFillStyle(toolTipOption.labelFontColor || opts.fontColor);
  2348. context.fillText(labelText[i], textX, textY + 0.5 * config.fontSize);
  2349. context.closePath();
  2350. context.stroke();
  2351. if (widthArr[i].position == 'left') {
  2352. tStartLeft -= (widthArr[i].width + opts.yAxis.padding * opts.pix);
  2353. } else {
  2354. tStartRight += widthArr[i].width + opts.yAxis.padding * opts.pix;
  2355. }
  2356. }
  2357. }
  2358. }
  2359. function drawToolTipSplitArea(offsetX, opts, config, context, eachSpacing) {
  2360. var toolTipOption = assign({}, {
  2361. activeBgColor: '#000000',
  2362. activeBgOpacity: 0.08
  2363. }, opts.extra.column);
  2364. var startY = opts.area[0];
  2365. var endY = opts.height - opts.area[2];
  2366. context.beginPath();
  2367. context.setFillStyle(hexToRgb(toolTipOption.activeBgColor, toolTipOption.activeBgOpacity));
  2368. context.rect(offsetX - eachSpacing / 2, startY, eachSpacing, endY - startY);
  2369. context.closePath();
  2370. context.fill();
  2371. context.setFillStyle("#FFFFFF");
  2372. }
  2373. function drawBarToolTipSplitArea(offsetX, opts, config, context, eachSpacing) {
  2374. var toolTipOption = assign({}, {
  2375. activeBgColor: '#000000',
  2376. activeBgOpacity: 0.08
  2377. }, opts.extra.bar);
  2378. var startX = opts.area[3];
  2379. var endX = opts.width - opts.area[1];
  2380. context.beginPath();
  2381. context.setFillStyle(hexToRgb(toolTipOption.activeBgColor, toolTipOption.activeBgOpacity));
  2382. context.rect( startX ,offsetX - eachSpacing / 2 , endX - startX,eachSpacing);
  2383. context.closePath();
  2384. context.fill();
  2385. context.setFillStyle("#FFFFFF");
  2386. }
  2387. function drawToolTip(textList, offset, opts, config, context, eachSpacing, xAxisPoints) {
  2388. var toolTipOption = assign({}, {
  2389. showBox: true,
  2390. showArrow: true,
  2391. showCategory: false,
  2392. bgColor: '#000000',
  2393. bgOpacity: 0.7,
  2394. borderColor: '#000000',
  2395. borderWidth: 0,
  2396. borderRadius: 0,
  2397. borderOpacity: 0.7,
  2398. fontColor: '#FFFFFF',
  2399. splitLine: true,
  2400. }, opts.extra.tooltip);
  2401. if(toolTipOption.showCategory==true && opts.categories){
  2402. textList.unshift({text:opts.categories[opts.tooltip.index],color:null})
  2403. }
  2404. var legendWidth = 4 * opts.pix;
  2405. var legendMarginRight = 5 * opts.pix;
  2406. var arrowWidth = toolTipOption.showArrow ? 8 * opts.pix : 0;
  2407. var isOverRightBorder = false;
  2408. if (opts.type == 'line' || opts.type == 'area' || opts.type == 'candle' || opts.type == 'mix') {
  2409. if (toolTipOption.splitLine == true) {
  2410. drawToolTipSplitLine(opts.tooltip.offset.x, opts, config, context);
  2411. }
  2412. }
  2413. offset = assign({
  2414. x: 0,
  2415. y: 0
  2416. }, offset);
  2417. offset.y -= 8 * opts.pix;
  2418. var textWidth = textList.map(function(item) {
  2419. return measureText(item.text, config.fontSize, context);
  2420. });
  2421. var toolTipWidth = legendWidth + legendMarginRight + 4 * config.toolTipPadding + Math.max.apply(null, textWidth);
  2422. var toolTipHeight = 2 * config.toolTipPadding + textList.length * config.toolTipLineHeight;
  2423. if (toolTipOption.showBox == false) {
  2424. return
  2425. }
  2426. // if beyond the right border
  2427. if (offset.x - Math.abs(opts._scrollDistance_ || 0) + arrowWidth + toolTipWidth > opts.width) {
  2428. isOverRightBorder = true;
  2429. }
  2430. if (toolTipHeight + offset.y > opts.height) {
  2431. offset.y = opts.height - toolTipHeight;
  2432. }
  2433. // draw background rect
  2434. context.beginPath();
  2435. context.setFillStyle(hexToRgb(toolTipOption.bgColor || config.toolTipBackground, toolTipOption.bgOpacity || config.toolTipOpacity));
  2436. context.setLineWidth(toolTipOption.borderWidth * opts.pix);
  2437. context.setStrokeStyle(hexToRgb(toolTipOption.borderColor, toolTipOption.borderOpacity));
  2438. var radius = toolTipOption.borderRadius;
  2439. if (isOverRightBorder) {
  2440. if (toolTipOption.showArrow) {
  2441. context.moveTo(offset.x, offset.y + 10 * opts.pix);
  2442. context.lineTo(offset.x - arrowWidth, offset.y + 10 * opts.pix + 5 * opts.pix);
  2443. }
  2444. context.arc(offset.x - arrowWidth - radius, offset.y + toolTipHeight - radius, radius, 0, Math.PI / 2, false);
  2445. context.arc(offset.x - arrowWidth - Math.round(toolTipWidth) + radius, offset.y + toolTipHeight - radius, radius,
  2446. Math.PI / 2, Math.PI, false);
  2447. context.arc(offset.x - arrowWidth - Math.round(toolTipWidth) + radius, offset.y + radius, radius, -Math.PI, -Math.PI / 2, false);
  2448. context.arc(offset.x - arrowWidth - radius, offset.y + radius, radius, -Math.PI / 2, 0, false);
  2449. if (toolTipOption.showArrow) {
  2450. context.lineTo(offset.x - arrowWidth, offset.y + 10 * opts.pix - 5 * opts.pix);
  2451. context.lineTo(offset.x, offset.y + 10 * opts.pix);
  2452. }
  2453. } else {
  2454. if (toolTipOption.showArrow) {
  2455. context.moveTo(offset.x, offset.y + 10 * opts.pix);
  2456. context.lineTo(offset.x + arrowWidth, offset.y + 10 * opts.pix - 5 * opts.pix);
  2457. }
  2458. context.arc(offset.x + arrowWidth + radius, offset.y + radius, radius, -Math.PI, -Math.PI / 2, false);
  2459. context.arc(offset.x + arrowWidth + Math.round(toolTipWidth) - radius, offset.y + radius, radius, -Math.PI / 2, 0,
  2460. false);
  2461. context.arc(offset.x + arrowWidth + Math.round(toolTipWidth) - radius, offset.y + toolTipHeight - radius, radius, 0,
  2462. Math.PI / 2, false);
  2463. context.arc(offset.x + arrowWidth + radius, offset.y + toolTipHeight - radius, radius, Math.PI / 2, Math.PI, false);
  2464. if (toolTipOption.showArrow) {
  2465. context.lineTo(offset.x + arrowWidth, offset.y + 10 * opts.pix + 5 * opts.pix);
  2466. context.lineTo(offset.x, offset.y + 10 * opts.pix);
  2467. }
  2468. }
  2469. context.closePath();
  2470. context.fill();
  2471. if (toolTipOption.borderWidth > 0) {
  2472. context.stroke();
  2473. }
  2474. // draw legend
  2475. textList.forEach(function(item, index) {
  2476. if (item.color !== null) {
  2477. context.beginPath();
  2478. context.setFillStyle(item.color);
  2479. var startX = offset.x + arrowWidth + 2 * config.toolTipPadding;
  2480. var startY = offset.y + (config.toolTipLineHeight - config.fontSize) / 2 + config.toolTipLineHeight * index + config.toolTipPadding + 1;
  2481. if (isOverRightBorder) {
  2482. startX = offset.x - toolTipWidth - arrowWidth + 2 * config.toolTipPadding;
  2483. }
  2484. context.fillRect(startX, startY, legendWidth, config.fontSize);
  2485. context.closePath();
  2486. }
  2487. });
  2488. // draw text list
  2489. textList.forEach(function(item, index) {
  2490. var startX = offset.x + arrowWidth + 2 * config.toolTipPadding + legendWidth + legendMarginRight;
  2491. if (isOverRightBorder) {
  2492. startX = offset.x - toolTipWidth - arrowWidth + 2 * config.toolTipPadding + +legendWidth + legendMarginRight;
  2493. }
  2494. var startY = offset.y + (config.toolTipLineHeight - config.fontSize) / 2 + config.toolTipLineHeight * index + config.toolTipPadding;
  2495. context.beginPath();
  2496. context.setFontSize(config.fontSize);
  2497. context.setFillStyle(toolTipOption.fontColor);
  2498. context.fillText(item.text, startX, startY + config.fontSize);
  2499. context.closePath();
  2500. context.stroke();
  2501. });
  2502. }
  2503. function drawColumnDataPoints(series, opts, config, context) {
  2504. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2505. let xAxisData = opts.chartData.xAxisData,
  2506. xAxisPoints = xAxisData.xAxisPoints,
  2507. eachSpacing = xAxisData.eachSpacing;
  2508. let columnOption = assign({}, {
  2509. type: 'group',
  2510. width: eachSpacing / 2,
  2511. meterBorder: 4,
  2512. meterFillColor: '#FFFFFF',
  2513. barBorderCircle: false,
  2514. barBorderRadius: [],
  2515. seriesGap: 2,
  2516. linearType: 'none',
  2517. linearOpacity: 1,
  2518. customColor: [],
  2519. colorStop: 0,
  2520. }, opts.extra.column);
  2521. let calPoints = [];
  2522. context.save();
  2523. let leftNum = -2;
  2524. let rightNum = xAxisPoints.length + 2;
  2525. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2526. context.translate(opts._scrollDistance_, 0);
  2527. leftNum = Math.floor(-opts._scrollDistance_ / eachSpacing) - 2;
  2528. rightNum = leftNum + opts.xAxis.itemCount + 4;
  2529. }
  2530. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  2531. drawToolTipSplitArea(opts.tooltip.offset.x, opts, config, context, eachSpacing);
  2532. }
  2533. columnOption.customColor = fillCustomColor(columnOption.linearType, columnOption.customColor, series, config);
  2534. series.forEach(function(eachSeries, seriesIndex) {
  2535. let ranges, minRange, maxRange;
  2536. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2537. minRange = ranges.pop();
  2538. maxRange = ranges.shift();
  2539. var data = eachSeries.data;
  2540. switch (columnOption.type) {
  2541. case 'group':
  2542. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2543. var tooltipPoints = getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, seriesIndex, series, process);
  2544. calPoints.push(tooltipPoints);
  2545. points = fixColumeData(points, eachSpacing, series.length, seriesIndex, config, opts);
  2546. for (let i = 0; i < points.length; i++) {
  2547. let item = points[i];
  2548. //fix issues/I27B1N yyoinge & Joeshu
  2549. if (item !== null && i > leftNum && i < rightNum) {
  2550. var startX = item.x - item.width / 2;
  2551. var height = opts.height - item.y - opts.area[2];
  2552. context.beginPath();
  2553. var fillColor = item.color || eachSeries.color
  2554. var strokeColor = item.color || eachSeries.color
  2555. if (columnOption.linearType !== 'none') {
  2556. var grd = context.createLinearGradient(startX, item.y, startX, opts.height - opts.area[2]);
  2557. //透明渐变
  2558. if (columnOption.linearType == 'opacity') {
  2559. grd.addColorStop(0, hexToRgb(fillColor, columnOption.linearOpacity));
  2560. grd.addColorStop(1, hexToRgb(fillColor, 1));
  2561. } else {
  2562. grd.addColorStop(0, hexToRgb(columnOption.customColor[eachSeries.linearIndex], columnOption.linearOpacity));
  2563. grd.addColorStop(columnOption.colorStop, hexToRgb(columnOption.customColor[eachSeries.linearIndex],columnOption.linearOpacity));
  2564. grd.addColorStop(1, hexToRgb(fillColor, 1));
  2565. }
  2566. fillColor = grd
  2567. }
  2568. // 圆角边框
  2569. if ((columnOption.barBorderRadius && columnOption.barBorderRadius.length === 4) || columnOption.barBorderCircle === true) {
  2570. const left = startX;
  2571. const top = item.y;
  2572. const width = item.width;
  2573. const height = opts.height - opts.area[2] - item.y;
  2574. if (columnOption.barBorderCircle) {
  2575. columnOption.barBorderRadius = [width / 2, width / 2, 0, 0];
  2576. }
  2577. let [r0, r1, r2, r3] = columnOption.barBorderRadius;
  2578. let minRadius = Math.min(width/2,height/2);
  2579. r0 = r0 > minRadius ? minRadius : r0;
  2580. r1 = r1 > minRadius ? minRadius : r1;
  2581. r2 = r2 > minRadius ? minRadius : r2;
  2582. r3 = r3 > minRadius ? minRadius : r3;
  2583. r0 = r0 < 0 ? 0 : r0;
  2584. r1 = r1 < 0 ? 0 : r1;
  2585. r2 = r2 < 0 ? 0 : r2;
  2586. r3 = r3 < 0 ? 0 : r3;
  2587. context.arc(left + r0, top + r0, r0, -Math.PI, -Math.PI / 2);
  2588. context.arc(left + width - r1, top + r1, r1, -Math.PI / 2, 0);
  2589. context.arc(left + width - r2, top + height - r2, r2, 0, Math.PI / 2);
  2590. context.arc(left + r3, top + height - r3, r3, Math.PI / 2, Math.PI);
  2591. } else {
  2592. context.moveTo(startX, item.y);
  2593. context.lineTo(startX + item.width - 2, item.y);
  2594. context.lineTo(startX + item.width - 2, opts.height - opts.area[2]);
  2595. context.lineTo(startX, opts.height - opts.area[2]);
  2596. context.lineTo(startX, item.y);
  2597. context.setLineWidth(1)
  2598. context.setStrokeStyle(strokeColor);
  2599. }
  2600. context.setFillStyle(fillColor);
  2601. context.closePath();
  2602. //context.stroke();
  2603. context.fill();
  2604. }
  2605. };
  2606. break;
  2607. case 'stack':
  2608. // 绘制堆叠数据图
  2609. var points = getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, seriesIndex, series, process);
  2610. calPoints.push(points);
  2611. points = fixColumeStackData(points, eachSpacing, series.length, seriesIndex, config, opts, series);
  2612. for (let i = 0; i < points.length; i++) {
  2613. let item = points[i];
  2614. if (item !== null && i > leftNum && i < rightNum) {
  2615. context.beginPath();
  2616. var fillColor = item.color || eachSeries.color;
  2617. var startX = item.x - item.width / 2 + 1;
  2618. var height = opts.height - item.y - opts.area[2];
  2619. var height0 = opts.height - item.y0 - opts.area[2];
  2620. if (seriesIndex > 0) {
  2621. height -= height0;
  2622. }
  2623. context.setFillStyle(fillColor);
  2624. context.moveTo(startX, item.y);
  2625. context.fillRect(startX, item.y, item.width - 2, height);
  2626. context.closePath();
  2627. context.fill();
  2628. }
  2629. };
  2630. break;
  2631. case 'meter':
  2632. // 绘制温度计数据图
  2633. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2634. calPoints.push(points);
  2635. points = fixColumeMeterData(points, eachSpacing, series.length, seriesIndex, config, opts, columnOption.meterBorder);
  2636. if (seriesIndex == 0) {
  2637. for (let i = 0; i < points.length; i++) {
  2638. let item = points[i];
  2639. if (item !== null && i > leftNum && i < rightNum) {
  2640. //画背景颜色
  2641. context.beginPath();
  2642. context.setFillStyle(columnOption.meterFillColor);
  2643. var startX = item.x - item.width / 2;
  2644. var height = opts.height - item.y - opts.area[2];
  2645. if (columnOption.barBorderCircle) {
  2646. var barBorderRadius = (item.width - columnOption.meterBorder*2) / 2;
  2647. if(barBorderRadius>height){
  2648. barBorderRadius = height;
  2649. }
  2650. context.moveTo(startX + columnOption.meterBorder, opts.height - opts.area[2]);
  2651. context.lineTo(startX + columnOption.meterBorder, item.y + barBorderRadius);
  2652. context.arc(startX + item.width/2, item.y + barBorderRadius, barBorderRadius, -Math.PI, 0);
  2653. context.lineTo(startX + item.width - columnOption.meterBorder , opts.height - opts.area[2]);
  2654. context.lineTo(startX, opts.height - opts.area[2]);
  2655. context.fill();
  2656. }else{
  2657. context.moveTo(startX, item.y);
  2658. context.fillRect(startX, item.y, item.width, height);
  2659. context.closePath();
  2660. context.fill();
  2661. }
  2662. //画边框线
  2663. if (columnOption.meterBorder > 0) {
  2664. context.beginPath();
  2665. context.setStrokeStyle(eachSeries.color);
  2666. context.setLineWidth(columnOption.meterBorder * opts.pix);
  2667. if (columnOption.barBorderCircle) {
  2668. var barBorderRadius = (item.width - columnOption.meterBorder)/ 2;
  2669. if(barBorderRadius>height){
  2670. barBorderRadius = height;
  2671. }
  2672. context.moveTo(startX + columnOption.meterBorder * 0.5, opts.height - opts.area[2]);
  2673. context.lineTo(startX + columnOption.meterBorder * 0.5, item.y + barBorderRadius);
  2674. context.arc(startX + item.width/2, item.y + barBorderRadius - columnOption.meterBorder * 0.5, barBorderRadius, -Math.PI, 0);
  2675. context.lineTo(startX + item.width - columnOption.meterBorder * 0.5, opts.height - opts.area[2]);
  2676. }else{
  2677. context.moveTo(startX + columnOption.meterBorder * 0.5, item.y + height);
  2678. context.lineTo(startX + columnOption.meterBorder * 0.5, item.y + columnOption.meterBorder * 0.5);
  2679. context.lineTo(startX + item.width - columnOption.meterBorder * 0.5, item.y + columnOption.meterBorder * 0.5);
  2680. context.lineTo(startX + item.width - columnOption.meterBorder * 0.5, item.y + height);
  2681. }
  2682. context.stroke();
  2683. }
  2684. }
  2685. };
  2686. } else {
  2687. for (let i = 0; i < points.length; i++) {
  2688. let item = points[i];
  2689. if (item !== null && i > leftNum && i < rightNum) {
  2690. context.beginPath();
  2691. context.setFillStyle(item.color || eachSeries.color);
  2692. var startX = item.x - item.width / 2;
  2693. var height = opts.height - item.y - opts.area[2];
  2694. if (columnOption.barBorderCircle) {
  2695. var barBorderRadius = item.width / 2;
  2696. if(barBorderRadius>height){
  2697. barBorderRadius = height;
  2698. }
  2699. context.moveTo(startX, opts.height - opts.area[2]);
  2700. context.arc(startX + barBorderRadius, item.y + barBorderRadius, barBorderRadius, -Math.PI, -Math.PI / 2);
  2701. context.arc(startX + item.width - barBorderRadius, item.y + barBorderRadius, barBorderRadius, -Math.PI / 2, 0);
  2702. context.lineTo(startX + item.width, opts.height - opts.area[2]);
  2703. context.lineTo(startX, opts.height - opts.area[2]);
  2704. context.fill();
  2705. }else{
  2706. context.moveTo(startX, item.y);
  2707. context.fillRect(startX, item.y, item.width, height);
  2708. context.closePath();
  2709. context.fill();
  2710. }
  2711. }
  2712. };
  2713. }
  2714. break;
  2715. }
  2716. });
  2717. if (opts.dataLabel !== false && process === 1) {
  2718. series.forEach(function(eachSeries, seriesIndex) {
  2719. let ranges, minRange, maxRange;
  2720. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2721. minRange = ranges.pop();
  2722. maxRange = ranges.shift();
  2723. var data = eachSeries.data;
  2724. switch (columnOption.type) {
  2725. case 'group':
  2726. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2727. points = fixColumeData(points, eachSpacing, series.length, seriesIndex, config, opts);
  2728. drawPointText(points, eachSeries, config, context, opts);
  2729. break;
  2730. case 'stack':
  2731. var points = getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, seriesIndex, series, process);
  2732. drawPointText(points, eachSeries, config, context, opts);
  2733. break;
  2734. case 'meter':
  2735. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2736. drawPointText(points, eachSeries, config, context, opts);
  2737. break;
  2738. }
  2739. });
  2740. }
  2741. context.restore();
  2742. return {
  2743. xAxisPoints: xAxisPoints,
  2744. calPoints: calPoints,
  2745. eachSpacing: eachSpacing
  2746. };
  2747. }
  2748. function drawBarDataPoints(series, opts, config, context) {
  2749. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2750. let yAxisPoints = [];
  2751. let eachSpacing = (opts.height - opts.area[0] - opts.area[2])/opts.categories.length;
  2752. for (let i = 0; i < opts.categories.length; i++) {
  2753. yAxisPoints.push(opts.area[0] + eachSpacing / 2 + eachSpacing * i);
  2754. }
  2755. let columnOption = assign({}, {
  2756. type: 'group',
  2757. width: eachSpacing / 2,
  2758. meterBorder: 4,
  2759. meterFillColor: '#FFFFFF',
  2760. barBorderCircle: false,
  2761. barBorderRadius: [],
  2762. seriesGap: 2,
  2763. linearType: 'none',
  2764. linearOpacity: 1,
  2765. customColor: [],
  2766. colorStop: 0,
  2767. }, opts.extra.bar);
  2768. let calPoints = [];
  2769. context.save();
  2770. let leftNum = -2;
  2771. let rightNum = yAxisPoints.length + 2;
  2772. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  2773. drawBarToolTipSplitArea(opts.tooltip.offset.y, opts, config, context, eachSpacing);
  2774. }
  2775. columnOption.customColor = fillCustomColor(columnOption.linearType, columnOption.customColor, series, config);
  2776. series.forEach(function(eachSeries, seriesIndex) {
  2777. let ranges, minRange, maxRange;
  2778. ranges = [].concat(opts.chartData.xAxisData.ranges);
  2779. maxRange = ranges.pop();
  2780. minRange = ranges.shift();
  2781. var data = eachSeries.data;
  2782. switch (columnOption.type) {
  2783. case 'group':
  2784. var points = getBarDataPoints(data, minRange, maxRange, yAxisPoints, eachSpacing, opts, config, process);
  2785. var tooltipPoints = getBarStackDataPoints(data, minRange, maxRange, yAxisPoints, eachSpacing, opts, config, seriesIndex, series, process);
  2786. calPoints.push(tooltipPoints);
  2787. points = fixBarData(points, eachSpacing, series.length, seriesIndex, config, opts);
  2788. for (let i = 0; i < points.length; i++) {
  2789. let item = points[i];
  2790. //fix issues/I27B1N yyoinge & Joeshu
  2791. if (item !== null && i > leftNum && i < rightNum) {
  2792. //var startX = item.x - item.width / 2;
  2793. var startX = opts.area[3];
  2794. var startY = item.y - item.width / 2;
  2795. var height = item.height;
  2796. context.beginPath();
  2797. var fillColor = item.color || eachSeries.color
  2798. var strokeColor = item.color || eachSeries.color
  2799. if (columnOption.linearType !== 'none') {
  2800. var grd = context.createLinearGradient(startX, item.y, item.x, item.y);
  2801. //透明渐变
  2802. if (columnOption.linearType == 'opacity') {
  2803. grd.addColorStop(0, hexToRgb(fillColor, columnOption.linearOpacity));
  2804. grd.addColorStop(1, hexToRgb(fillColor, 1));
  2805. } else {
  2806. grd.addColorStop(0, hexToRgb(columnOption.customColor[eachSeries.linearIndex], columnOption.linearOpacity));
  2807. grd.addColorStop(columnOption.colorStop, hexToRgb(columnOption.customColor[eachSeries.linearIndex],columnOption.linearOpacity));
  2808. grd.addColorStop(1, hexToRgb(fillColor, 1));
  2809. }
  2810. fillColor = grd
  2811. }
  2812. // 圆角边框
  2813. if ((columnOption.barBorderRadius && columnOption.barBorderRadius.length === 4) || columnOption.barBorderCircle === true) {
  2814. const left = startX;
  2815. const width = item.width;
  2816. const top = item.y - item.width / 2;
  2817. const height = item.heigh;
  2818. if (columnOption.barBorderCircle) {
  2819. columnOption.barBorderRadius = [width / 2, width / 2, 0, 0];
  2820. }
  2821. let [r0, r1, r2, r3] = columnOption.barBorderRadius;
  2822. let minRadius = Math.min(width/2,height/2);
  2823. r0 = r0 > minRadius ? minRadius : r0;
  2824. r1 = r1 > minRadius ? minRadius : r1;
  2825. r2 = r2 > minRadius ? minRadius : r2;
  2826. r3 = r3 > minRadius ? minRadius : r3;
  2827. r0 = r0 < 0 ? 0 : r0;
  2828. r1 = r1 < 0 ? 0 : r1;
  2829. r2 = r2 < 0 ? 0 : r2;
  2830. r3 = r3 < 0 ? 0 : r3;
  2831. context.arc(left + r3, top + r3, r3, -Math.PI, -Math.PI / 2);
  2832. context.arc(item.x - r0, top + r0, r0, -Math.PI / 2, 0);
  2833. context.arc(item.x - r1, top + width - r1, r1, 0, Math.PI / 2);
  2834. context.arc(left + r2, top + width - r2, r2, Math.PI / 2, Math.PI);
  2835. } else {
  2836. context.moveTo(startX, startY);
  2837. context.lineTo(item.x, startY);
  2838. context.lineTo(item.x, startY + item.width - 2);
  2839. context.lineTo(startX, startY + item.width - 2);
  2840. context.lineTo(startX, startY);
  2841. context.setLineWidth(1)
  2842. context.setStrokeStyle(strokeColor);
  2843. }
  2844. context.setFillStyle(fillColor);
  2845. context.closePath();
  2846. //context.stroke();
  2847. context.fill();
  2848. }
  2849. };
  2850. break;
  2851. case 'stack':
  2852. // 绘制堆叠数据图
  2853. var points = getBarStackDataPoints(data, minRange, maxRange, yAxisPoints, eachSpacing, opts, config, seriesIndex, series, process);
  2854. calPoints.push(points);
  2855. points = fixBarStackData(points, eachSpacing, series.length, seriesIndex, config, opts, series);
  2856. for (let i = 0; i < points.length; i++) {
  2857. let item = points[i];
  2858. if (item !== null && i > leftNum && i < rightNum) {
  2859. context.beginPath();
  2860. var fillColor = item.color || eachSeries.color;
  2861. var startX = item.x0;
  2862. context.setFillStyle(fillColor);
  2863. context.moveTo(startX, item.y - item.width/2);
  2864. context.fillRect(startX, item.y - item.width/2, item.height , item.width - 2);
  2865. context.closePath();
  2866. context.fill();
  2867. }
  2868. };
  2869. break;
  2870. }
  2871. });
  2872. if (opts.dataLabel !== false && process === 1) {
  2873. series.forEach(function(eachSeries, seriesIndex) {
  2874. let ranges, minRange, maxRange;
  2875. ranges = [].concat(opts.chartData.xAxisData.ranges);
  2876. maxRange = ranges.pop();
  2877. minRange = ranges.shift();
  2878. var data = eachSeries.data;
  2879. switch (columnOption.type) {
  2880. case 'group':
  2881. var points = getBarDataPoints(data, minRange, maxRange, yAxisPoints, eachSpacing, opts, config, process);
  2882. points = fixBarData(points, eachSpacing, series.length, seriesIndex, config, opts);
  2883. drawBarPointText(points, eachSeries, config, context, opts);
  2884. break;
  2885. case 'stack':
  2886. var points = getBarStackDataPoints(data, minRange, maxRange, yAxisPoints, eachSpacing, opts, config, seriesIndex, series, process);
  2887. drawBarPointText(points, eachSeries, config, context, opts);
  2888. break;
  2889. }
  2890. });
  2891. }
  2892. return {
  2893. yAxisPoints: yAxisPoints,
  2894. calPoints: calPoints,
  2895. eachSpacing: eachSpacing
  2896. };
  2897. }
  2898. function drawCandleDataPoints(series, seriesMA, opts, config, context) {
  2899. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  2900. var candleOption = assign({}, {
  2901. color: {},
  2902. average: {}
  2903. }, opts.extra.candle);
  2904. candleOption.color = assign({}, {
  2905. upLine: '#f04864',
  2906. upFill: '#f04864',
  2907. downLine: '#2fc25b',
  2908. downFill: '#2fc25b'
  2909. }, candleOption.color);
  2910. candleOption.average = assign({}, {
  2911. show: false,
  2912. name: [],
  2913. day: [],
  2914. color: config.color
  2915. }, candleOption.average);
  2916. opts.extra.candle = candleOption;
  2917. let xAxisData = opts.chartData.xAxisData,
  2918. xAxisPoints = xAxisData.xAxisPoints,
  2919. eachSpacing = xAxisData.eachSpacing;
  2920. let calPoints = [];
  2921. context.save();
  2922. let leftNum = -2;
  2923. let rightNum = xAxisPoints.length + 2;
  2924. let leftSpace = 0;
  2925. let rightSpace = opts.width + eachSpacing;
  2926. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2927. context.translate(opts._scrollDistance_, 0);
  2928. leftNum = Math.floor(-opts._scrollDistance_ / eachSpacing) - 2;
  2929. rightNum = leftNum + opts.xAxis.itemCount + 4;
  2930. leftSpace = -opts._scrollDistance_ - eachSpacing * 2 + opts.area[3];
  2931. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  2932. }
  2933. //画均线
  2934. if (candleOption.average.show || seriesMA) { //Merge pull request !12 from 邱贵翔
  2935. seriesMA.forEach(function(eachSeries, seriesIndex) {
  2936. let ranges, minRange, maxRange;
  2937. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2938. minRange = ranges.pop();
  2939. maxRange = ranges.shift();
  2940. var data = eachSeries.data;
  2941. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2942. var splitPointList = splitPoints(points,eachSeries);
  2943. for (let i = 0; i < splitPointList.length; i++) {
  2944. let points = splitPointList[i];
  2945. context.beginPath();
  2946. context.setStrokeStyle(eachSeries.color);
  2947. context.setLineWidth(1);
  2948. if (points.length === 1) {
  2949. context.moveTo(points[0].x, points[0].y);
  2950. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  2951. } else {
  2952. context.moveTo(points[0].x, points[0].y);
  2953. let startPoint = 0;
  2954. for (let j = 0; j < points.length; j++) {
  2955. let item = points[j];
  2956. if (startPoint == 0 && item.x > leftSpace) {
  2957. context.moveTo(item.x, item.y);
  2958. startPoint = 1;
  2959. }
  2960. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2961. var ctrlPoint = createCurveControlPoints(points, j - 1);
  2962. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x,
  2963. item.y);
  2964. }
  2965. }
  2966. context.moveTo(points[0].x, points[0].y);
  2967. }
  2968. context.closePath();
  2969. context.stroke();
  2970. }
  2971. });
  2972. }
  2973. //画K线
  2974. series.forEach(function(eachSeries, seriesIndex) {
  2975. let ranges, minRange, maxRange;
  2976. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2977. minRange = ranges.pop();
  2978. maxRange = ranges.shift();
  2979. var data = eachSeries.data;
  2980. var points = getCandleDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2981. calPoints.push(points);
  2982. var splitPointList = splitPoints(points,eachSeries);
  2983. for (let i = 0; i < splitPointList[0].length; i++) {
  2984. if (i > leftNum && i < rightNum) {
  2985. let item = splitPointList[0][i];
  2986. context.beginPath();
  2987. //如果上涨
  2988. if (data[i][1] - data[i][0] > 0) {
  2989. context.setStrokeStyle(candleOption.color.upLine);
  2990. context.setFillStyle(candleOption.color.upFill);
  2991. context.setLineWidth(1 * opts.pix);
  2992. context.moveTo(item[3].x, item[3].y); //顶点
  2993. context.lineTo(item[1].x, item[1].y); //收盘中间点
  2994. context.lineTo(item[1].x - eachSpacing / 4, item[1].y); //收盘左侧点
  2995. context.lineTo(item[0].x - eachSpacing / 4, item[0].y); //开盘左侧点
  2996. context.lineTo(item[0].x, item[0].y); //开盘中间点
  2997. context.lineTo(item[2].x, item[2].y); //底点
  2998. context.lineTo(item[0].x, item[0].y); //开盘中间点
  2999. context.lineTo(item[0].x + eachSpacing / 4, item[0].y); //开盘右侧点
  3000. context.lineTo(item[1].x + eachSpacing / 4, item[1].y); //收盘右侧点
  3001. context.lineTo(item[1].x, item[1].y); //收盘中间点
  3002. context.moveTo(item[3].x, item[3].y); //顶点
  3003. } else {
  3004. context.setStrokeStyle(candleOption.color.downLine);
  3005. context.setFillStyle(candleOption.color.downFill);
  3006. context.setLineWidth(1 * opts.pix);
  3007. context.moveTo(item[3].x, item[3].y); //顶点
  3008. context.lineTo(item[0].x, item[0].y); //开盘中间点
  3009. context.lineTo(item[0].x - eachSpacing / 4, item[0].y); //开盘左侧点
  3010. context.lineTo(item[1].x - eachSpacing / 4, item[1].y); //收盘左侧点
  3011. context.lineTo(item[1].x, item[1].y); //收盘中间点
  3012. context.lineTo(item[2].x, item[2].y); //底点
  3013. context.lineTo(item[1].x, item[1].y); //收盘中间点
  3014. context.lineTo(item[1].x + eachSpacing / 4, item[1].y); //收盘右侧点
  3015. context.lineTo(item[0].x + eachSpacing / 4, item[0].y); //开盘右侧点
  3016. context.lineTo(item[0].x, item[0].y); //开盘中间点
  3017. context.moveTo(item[3].x, item[3].y); //顶点
  3018. }
  3019. context.closePath();
  3020. context.fill();
  3021. context.stroke();
  3022. }
  3023. }
  3024. });
  3025. context.restore();
  3026. return {
  3027. xAxisPoints: xAxisPoints,
  3028. calPoints: calPoints,
  3029. eachSpacing: eachSpacing
  3030. };
  3031. }
  3032. function drawAreaDataPoints(series, opts, config, context) {
  3033. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3034. var areaOption = assign({}, {
  3035. type: 'straight',
  3036. opacity: 0.2,
  3037. addLine: false,
  3038. width: 2,
  3039. gradient: false
  3040. }, opts.extra.area);
  3041. let xAxisData = opts.chartData.xAxisData,
  3042. xAxisPoints = xAxisData.xAxisPoints,
  3043. eachSpacing = xAxisData.eachSpacing;
  3044. let endY = opts.height - opts.area[2];
  3045. let calPoints = [];
  3046. context.save();
  3047. let leftSpace = 0;
  3048. let rightSpace = opts.width + eachSpacing;
  3049. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3050. context.translate(opts._scrollDistance_, 0);
  3051. leftSpace = -opts._scrollDistance_ - eachSpacing * 2 + opts.area[3];
  3052. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  3053. }
  3054. series.forEach(function(eachSeries, seriesIndex) {
  3055. let ranges, minRange, maxRange;
  3056. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3057. minRange = ranges.pop();
  3058. maxRange = ranges.shift();
  3059. let data = eachSeries.data;
  3060. let points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3061. calPoints.push(points);
  3062. let splitPointList = splitPoints(points,eachSeries);
  3063. for (let i = 0; i < splitPointList.length; i++) {
  3064. let points = splitPointList[i];
  3065. // 绘制区域数
  3066. context.beginPath();
  3067. context.setStrokeStyle(hexToRgb(eachSeries.color, areaOption.opacity));
  3068. if (areaOption.gradient) {
  3069. let gradient = context.createLinearGradient(0, opts.area[0], 0, opts.height - opts.area[2]);
  3070. gradient.addColorStop('0', hexToRgb(eachSeries.color, areaOption.opacity));
  3071. gradient.addColorStop('1.0', hexToRgb("#FFFFFF", 0.1));
  3072. context.setFillStyle(gradient);
  3073. } else {
  3074. context.setFillStyle(hexToRgb(eachSeries.color, areaOption.opacity));
  3075. }
  3076. context.setLineWidth(areaOption.width * opts.pix);
  3077. if (points.length > 1) {
  3078. let firstPoint = points[0];
  3079. let lastPoint = points[points.length - 1];
  3080. context.moveTo(firstPoint.x, firstPoint.y);
  3081. let startPoint = 0;
  3082. if (areaOption.type === 'curve') {
  3083. for (let j = 0; j < points.length; j++) {
  3084. let item = points[j];
  3085. if (startPoint == 0 && item.x > leftSpace) {
  3086. context.moveTo(item.x, item.y);
  3087. startPoint = 1;
  3088. }
  3089. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3090. let ctrlPoint = createCurveControlPoints(points, j - 1);
  3091. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item.y);
  3092. }
  3093. };
  3094. }
  3095. if (areaOption.type === 'straight') {
  3096. for (let j = 0; j < points.length; j++) {
  3097. let item = points[j];
  3098. if (startPoint == 0 && item.x > leftSpace) {
  3099. context.moveTo(item.x, item.y);
  3100. startPoint = 1;
  3101. }
  3102. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3103. context.lineTo(item.x, item.y);
  3104. }
  3105. };
  3106. }
  3107. if (areaOption.type === 'step') {
  3108. for (let j = 0; j < points.length; j++) {
  3109. let item = points[j];
  3110. if (startPoint == 0 && item.x > leftSpace) {
  3111. context.moveTo(item.x, item.y);
  3112. startPoint = 1;
  3113. }
  3114. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3115. context.lineTo(item.x, points[j - 1].y);
  3116. context.lineTo(item.x, item.y);
  3117. }
  3118. };
  3119. }
  3120. context.lineTo(lastPoint.x, endY);
  3121. context.lineTo(firstPoint.x, endY);
  3122. context.lineTo(firstPoint.x, firstPoint.y);
  3123. } else {
  3124. let item = points[0];
  3125. context.moveTo(item.x - eachSpacing / 2, item.y);
  3126. context.lineTo(item.x + eachSpacing / 2, item.y);
  3127. context.lineTo(item.x + eachSpacing / 2, endY);
  3128. context.lineTo(item.x - eachSpacing / 2, endY);
  3129. context.moveTo(item.x - eachSpacing / 2, item.y);
  3130. }
  3131. context.closePath();
  3132. context.fill();
  3133. //画连线
  3134. if (areaOption.addLine) {
  3135. if (eachSeries.lineType == 'dash') {
  3136. let dashLength = eachSeries.dashLength ? eachSeries.dashLength : 8;
  3137. dashLength *= opts.pix;
  3138. context.setLineDash([dashLength, dashLength]);
  3139. }
  3140. context.beginPath();
  3141. context.setStrokeStyle(eachSeries.color);
  3142. context.setLineWidth(areaOption.width * opts.pix);
  3143. if (points.length === 1) {
  3144. context.moveTo(points[0].x, points[0].y);
  3145. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  3146. } else {
  3147. context.moveTo(points[0].x, points[0].y);
  3148. let startPoint = 0;
  3149. if (areaOption.type === 'curve') {
  3150. for (let j = 0; j < points.length; j++) {
  3151. let item = points[j];
  3152. if (startPoint == 0 && item.x > leftSpace) {
  3153. context.moveTo(item.x, item.y);
  3154. startPoint = 1;
  3155. }
  3156. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3157. let ctrlPoint = createCurveControlPoints(points, j - 1);
  3158. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item.y);
  3159. }
  3160. };
  3161. }
  3162. if (areaOption.type === 'straight') {
  3163. for (let j = 0; j < points.length; j++) {
  3164. let item = points[j];
  3165. if (startPoint == 0 && item.x > leftSpace) {
  3166. context.moveTo(item.x, item.y);
  3167. startPoint = 1;
  3168. }
  3169. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3170. context.lineTo(item.x, item.y);
  3171. }
  3172. };
  3173. }
  3174. if (areaOption.type === 'step') {
  3175. for (let j = 0; j < points.length; j++) {
  3176. let item = points[j];
  3177. if (startPoint == 0 && item.x > leftSpace) {
  3178. context.moveTo(item.x, item.y);
  3179. startPoint = 1;
  3180. }
  3181. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3182. context.lineTo(item.x, points[j - 1].y);
  3183. context.lineTo(item.x, item.y);
  3184. }
  3185. };
  3186. }
  3187. context.moveTo(points[0].x, points[0].y);
  3188. }
  3189. context.stroke();
  3190. context.setLineDash([]);
  3191. }
  3192. }
  3193. //画点
  3194. if (opts.dataPointShape !== false) {
  3195. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  3196. }
  3197. });
  3198. if (opts.dataLabel !== false && process === 1) {
  3199. series.forEach(function(eachSeries, seriesIndex) {
  3200. let ranges, minRange, maxRange;
  3201. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3202. minRange = ranges.pop();
  3203. maxRange = ranges.shift();
  3204. var data = eachSeries.data;
  3205. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3206. drawPointText(points, eachSeries, config, context, opts);
  3207. });
  3208. }
  3209. context.restore();
  3210. return {
  3211. xAxisPoints: xAxisPoints,
  3212. calPoints: calPoints,
  3213. eachSpacing: eachSpacing
  3214. };
  3215. }
  3216. function drawScatterDataPoints(series, opts, config, context) {
  3217. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3218. var scatterOption = assign({}, {
  3219. type: 'circle'
  3220. }, opts.extra.scatter);
  3221. let xAxisData = opts.chartData.xAxisData,
  3222. xAxisPoints = xAxisData.xAxisPoints,
  3223. eachSpacing = xAxisData.eachSpacing;
  3224. var calPoints = [];
  3225. context.save();
  3226. let leftSpace = 0;
  3227. let rightSpace = opts.width + eachSpacing;
  3228. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3229. context.translate(opts._scrollDistance_, 0);
  3230. leftSpace = -opts._scrollDistance_ - eachSpacing * 2 + opts.area[3];
  3231. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  3232. }
  3233. series.forEach(function(eachSeries, seriesIndex) {
  3234. let ranges, minRange, maxRange;
  3235. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3236. minRange = ranges.pop();
  3237. maxRange = ranges.shift();
  3238. var data = eachSeries.data;
  3239. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3240. context.beginPath();
  3241. context.setStrokeStyle(eachSeries.color);
  3242. context.setFillStyle(eachSeries.color);
  3243. context.setLineWidth(1 * opts.pix);
  3244. var shape = eachSeries.pointShape;
  3245. if (shape === 'diamond') {
  3246. points.forEach(function(item, index) {
  3247. if (item !== null) {
  3248. context.moveTo(item.x, item.y - 4.5);
  3249. context.lineTo(item.x - 4.5, item.y);
  3250. context.lineTo(item.x, item.y + 4.5);
  3251. context.lineTo(item.x + 4.5, item.y);
  3252. context.lineTo(item.x, item.y - 4.5);
  3253. }
  3254. });
  3255. } else if (shape === 'circle') {
  3256. points.forEach(function(item, index) {
  3257. if (item !== null) {
  3258. context.moveTo(item.x + 2.5 * opts.pix, item.y);
  3259. context.arc(item.x, item.y, 3 * opts.pix, 0, 2 * Math.PI, false);
  3260. }
  3261. });
  3262. } else if (shape === 'square') {
  3263. points.forEach(function(item, index) {
  3264. if (item !== null) {
  3265. context.moveTo(item.x - 3.5, item.y - 3.5);
  3266. context.rect(item.x - 3.5, item.y - 3.5, 7, 7);
  3267. }
  3268. });
  3269. } else if (shape === 'triangle') {
  3270. points.forEach(function(item, index) {
  3271. if (item !== null) {
  3272. context.moveTo(item.x, item.y - 4.5);
  3273. context.lineTo(item.x - 4.5, item.y + 4.5);
  3274. context.lineTo(item.x + 4.5, item.y + 4.5);
  3275. context.lineTo(item.x, item.y - 4.5);
  3276. }
  3277. });
  3278. } else if (shape === 'triangle') {
  3279. return;
  3280. }
  3281. context.closePath();
  3282. context.fill();
  3283. context.stroke();
  3284. });
  3285. if (opts.dataLabel !== false && process === 1) {
  3286. series.forEach(function(eachSeries, seriesIndex) {
  3287. let ranges, minRange, maxRange;
  3288. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3289. minRange = ranges.pop();
  3290. maxRange = ranges.shift();
  3291. var data = eachSeries.data;
  3292. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3293. drawPointText(points, eachSeries, config, context, opts);
  3294. });
  3295. }
  3296. context.restore();
  3297. return {
  3298. xAxisPoints: xAxisPoints,
  3299. calPoints: calPoints,
  3300. eachSpacing: eachSpacing
  3301. };
  3302. }
  3303. function drawBubbleDataPoints(series, opts, config, context) {
  3304. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3305. var bubbleOption = assign({}, {
  3306. opacity: 1,
  3307. border:2
  3308. }, opts.extra.bubble);
  3309. let xAxisData = opts.chartData.xAxisData,
  3310. xAxisPoints = xAxisData.xAxisPoints,
  3311. eachSpacing = xAxisData.eachSpacing;
  3312. var calPoints = [];
  3313. context.save();
  3314. let leftSpace = 0;
  3315. let rightSpace = opts.width + eachSpacing;
  3316. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3317. context.translate(opts._scrollDistance_, 0);
  3318. leftSpace = -opts._scrollDistance_ - eachSpacing * 2 + opts.area[3];
  3319. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  3320. }
  3321. series.forEach(function(eachSeries, seriesIndex) {
  3322. let ranges, minRange, maxRange;
  3323. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3324. minRange = ranges.pop();
  3325. maxRange = ranges.shift();
  3326. var data = eachSeries.data;
  3327. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3328. context.beginPath();
  3329. context.setStrokeStyle(eachSeries.color);
  3330. context.setLineWidth(bubbleOption.border * opts.pix);
  3331. context.setFillStyle(hexToRgb(eachSeries.color, bubbleOption.opacity));
  3332. points.forEach(function(item, index) {
  3333. context.moveTo(item.x + item.r, item.y);
  3334. context.arc(item.x, item.y, item.r * opts.pix, 0, 2 * Math.PI, false);
  3335. });
  3336. context.closePath();
  3337. context.fill();
  3338. context.stroke();
  3339. if (opts.dataLabel !== false && process === 1) {
  3340. points.forEach(function(item, index) {
  3341. context.beginPath();
  3342. var fontSize = series.textSize * opts.pix || config.fontSize;
  3343. context.setFontSize(fontSize);
  3344. context.setFillStyle(series.textColor || "#FFFFFF");
  3345. context.setTextAlign('center');
  3346. context.fillText(String(item.t), item.x, item.y + fontSize/2);
  3347. context.closePath();
  3348. context.stroke();
  3349. context.setTextAlign('left');
  3350. });
  3351. }
  3352. });
  3353. context.restore();
  3354. return {
  3355. xAxisPoints: xAxisPoints,
  3356. calPoints: calPoints,
  3357. eachSpacing: eachSpacing
  3358. };
  3359. }
  3360. function drawLineDataPoints(series, opts, config, context) {
  3361. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3362. var lineOption = assign({}, {
  3363. type: 'straight',
  3364. width: 2
  3365. }, opts.extra.line);
  3366. lineOption.width *= opts.pix;
  3367. let xAxisData = opts.chartData.xAxisData,
  3368. xAxisPoints = xAxisData.xAxisPoints,
  3369. eachSpacing = xAxisData.eachSpacing;
  3370. var calPoints = [];
  3371. context.save();
  3372. let leftSpace = 0;
  3373. let rightSpace = opts.width + eachSpacing;
  3374. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3375. context.translate(opts._scrollDistance_, 0);
  3376. leftSpace = -opts._scrollDistance_ - eachSpacing * 2 + opts.area[3];
  3377. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  3378. }
  3379. series.forEach(function(eachSeries, seriesIndex) {
  3380. let ranges, minRange, maxRange;
  3381. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3382. minRange = ranges.pop();
  3383. maxRange = ranges.shift();
  3384. var data = eachSeries.data;
  3385. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3386. calPoints.push(points);
  3387. var splitPointList = splitPoints(points,eachSeries);
  3388. if (eachSeries.lineType == 'dash') {
  3389. let dashLength = eachSeries.dashLength ? eachSeries.dashLength : 8;
  3390. dashLength *= opts.pix;
  3391. context.setLineDash([dashLength, dashLength]);
  3392. }
  3393. context.beginPath();
  3394. context.setStrokeStyle(eachSeries.color);
  3395. context.setLineWidth(lineOption.width);
  3396. splitPointList.forEach(function(points, index) {
  3397. if (points.length === 1) {
  3398. context.moveTo(points[0].x, points[0].y);
  3399. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  3400. } else {
  3401. context.moveTo(points[0].x, points[0].y);
  3402. let startPoint = 0;
  3403. if (lineOption.type === 'curve') {
  3404. for (let j = 0; j < points.length; j++) {
  3405. let item = points[j];
  3406. if (startPoint == 0 && item.x > leftSpace) {
  3407. context.moveTo(item.x, item.y);
  3408. startPoint = 1;
  3409. }
  3410. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3411. var ctrlPoint = createCurveControlPoints(points, j - 1);
  3412. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item.y);
  3413. }
  3414. };
  3415. }
  3416. if (lineOption.type === 'straight') {
  3417. for (let j = 0; j < points.length; j++) {
  3418. let item = points[j];
  3419. if (startPoint == 0 && item.x > leftSpace) {
  3420. context.moveTo(item.x, item.y);
  3421. startPoint = 1;
  3422. }
  3423. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3424. context.lineTo(item.x, item.y);
  3425. }
  3426. };
  3427. }
  3428. if (lineOption.type === 'step') {
  3429. for (let j = 0; j < points.length; j++) {
  3430. let item = points[j];
  3431. if (startPoint == 0 && item.x > leftSpace) {
  3432. context.moveTo(item.x, item.y);
  3433. startPoint = 1;
  3434. }
  3435. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3436. context.lineTo(item.x, points[j - 1].y);
  3437. context.lineTo(item.x, item.y);
  3438. }
  3439. };
  3440. }
  3441. context.moveTo(points[0].x, points[0].y);
  3442. }
  3443. });
  3444. context.stroke();
  3445. context.setLineDash([]);
  3446. if (opts.dataPointShape !== false) {
  3447. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  3448. }
  3449. });
  3450. if (opts.dataLabel !== false && process === 1) {
  3451. series.forEach(function(eachSeries, seriesIndex) {
  3452. let ranges, minRange, maxRange;
  3453. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3454. minRange = ranges.pop();
  3455. maxRange = ranges.shift();
  3456. var data = eachSeries.data;
  3457. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3458. drawPointText(points, eachSeries, config, context, opts);
  3459. });
  3460. }
  3461. context.restore();
  3462. return {
  3463. xAxisPoints: xAxisPoints,
  3464. calPoints: calPoints,
  3465. eachSpacing: eachSpacing
  3466. };
  3467. }
  3468. function drawMixDataPoints(series, opts, config, context) {
  3469. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3470. let columnOption = assign({}, {
  3471. width: eachSpacing / 2,
  3472. barBorderCircle: false,
  3473. barBorderRadius: [],
  3474. seriesGap: 2,
  3475. linearType: 'none',
  3476. linearOpacity: 1,
  3477. customColor: [],
  3478. colorStop: 0,
  3479. }, opts.extra.mix.column);
  3480. let xAxisData = opts.chartData.xAxisData,
  3481. xAxisPoints = xAxisData.xAxisPoints,
  3482. eachSpacing = xAxisData.eachSpacing;
  3483. let endY = opts.height - opts.area[2];
  3484. let calPoints = [];
  3485. var columnIndex = 0;
  3486. var columnLength = 0;
  3487. series.forEach(function(eachSeries, seriesIndex) {
  3488. if (eachSeries.type == 'column') {
  3489. columnLength += 1;
  3490. }
  3491. });
  3492. context.save();
  3493. let leftNum = -2;
  3494. let rightNum = xAxisPoints.length + 2;
  3495. let leftSpace = 0;
  3496. let rightSpace = opts.width + eachSpacing;
  3497. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3498. context.translate(opts._scrollDistance_, 0);
  3499. leftNum = Math.floor(-opts._scrollDistance_ / eachSpacing) - 2;
  3500. rightNum = leftNum + opts.xAxis.itemCount + 4;
  3501. leftSpace = -opts._scrollDistance_ - eachSpacing * 2 + opts.area[3];
  3502. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  3503. }
  3504. columnOption.customColor = fillCustomColor(columnOption.linearType, columnOption.customColor, series, config);
  3505. series.forEach(function(eachSeries, seriesIndex) {
  3506. let ranges, minRange, maxRange;
  3507. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3508. minRange = ranges.pop();
  3509. maxRange = ranges.shift();
  3510. var data = eachSeries.data;
  3511. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3512. calPoints.push(points);
  3513. // 绘制柱状数据图
  3514. if (eachSeries.type == 'column') {
  3515. points = fixColumeData(points, eachSpacing, columnLength, columnIndex, config, opts);
  3516. for (let i = 0; i < points.length; i++) {
  3517. let item = points[i];
  3518. if (item !== null && i > leftNum && i < rightNum) {
  3519. var startX = item.x - item.width / 2;
  3520. var height = opts.height - item.y - opts.area[2];
  3521. context.beginPath();
  3522. var fillColor = item.color || eachSeries.color
  3523. var strokeColor = item.color || eachSeries.color
  3524. if (columnOption.linearType !== 'none') {
  3525. var grd = context.createLinearGradient(startX, item.y, startX, opts.height - opts.area[2]);
  3526. //透明渐变
  3527. if (columnOption.linearType == 'opacity') {
  3528. grd.addColorStop(0, hexToRgb(fillColor, columnOption.linearOpacity));
  3529. grd.addColorStop(1, hexToRgb(fillColor, 1));
  3530. } else {
  3531. grd.addColorStop(0, hexToRgb(columnOption.customColor[eachSeries.linearIndex], columnOption.linearOpacity));
  3532. grd.addColorStop(columnOption.colorStop, hexToRgb(columnOption.customColor[eachSeries.linearIndex], columnOption.linearOpacity));
  3533. grd.addColorStop(1, hexToRgb(fillColor, 1));
  3534. }
  3535. fillColor = grd
  3536. }
  3537. // 圆角边框
  3538. if ((columnOption.barBorderRadius && columnOption.barBorderRadius.length === 4) || columnOption.barBorderCircle) {
  3539. const left = startX;
  3540. const top = item.y;
  3541. const width = item.width;
  3542. const height = opts.height - opts.area[2] - item.y;
  3543. if (columnOption.barBorderCircle) {
  3544. columnOption.barBorderRadius = [width / 2, width / 2, 0, 0];
  3545. }
  3546. let [r0, r1, r2, r3] = columnOption.barBorderRadius;
  3547. let minRadius = Math.min(width/2,height/2);
  3548. r0 = r0 > minRadius ? minRadius : r0;
  3549. r1 = r1 > minRadius ? minRadius : r1;
  3550. r2 = r2 > minRadius ? minRadius : r2;
  3551. r3 = r3 > minRadius ? minRadius : r3;
  3552. r0 = r0 < 0 ? 0 : r0;
  3553. r1 = r1 < 0 ? 0 : r1;
  3554. r2 = r2 < 0 ? 0 : r2;
  3555. r3 = r3 < 0 ? 0 : r3;
  3556. context.arc(left + r0, top + r0, r0, -Math.PI, -Math.PI / 2);
  3557. context.arc(left + width - r1, top + r1, r1, -Math.PI / 2, 0);
  3558. context.arc(left + width - r2, top + height - r2, r2, 0, Math.PI / 2);
  3559. context.arc(left + r3, top + height - r3, r3, Math.PI / 2, Math.PI);
  3560. } else {
  3561. context.moveTo(startX, item.y);
  3562. context.lineTo(startX + item.width - 2, item.y);
  3563. context.lineTo(startX + item.width - 2, opts.height - opts.area[2]);
  3564. context.lineTo(startX, opts.height - opts.area[2]);
  3565. context.lineTo(startX, item.y);
  3566. context.setLineWidth(1)
  3567. context.setStrokeStyle(strokeColor);
  3568. }
  3569. context.setFillStyle(fillColor);
  3570. context.closePath();
  3571. context.fill();
  3572. }
  3573. }
  3574. columnIndex += 1;
  3575. }
  3576. //绘制区域图数据
  3577. if (eachSeries.type == 'area') {
  3578. let splitPointList = splitPoints(points,eachSeries);
  3579. for (let i = 0; i < splitPointList.length; i++) {
  3580. let points = splitPointList[i];
  3581. // 绘制区域数据
  3582. context.beginPath();
  3583. context.setStrokeStyle(eachSeries.color);
  3584. context.setFillStyle(hexToRgb(eachSeries.color, 0.2));
  3585. context.setLineWidth(2 * opts.pix);
  3586. if (points.length > 1) {
  3587. var firstPoint = points[0];
  3588. let lastPoint = points[points.length - 1];
  3589. context.moveTo(firstPoint.x, firstPoint.y);
  3590. let startPoint = 0;
  3591. if (eachSeries.style === 'curve') {
  3592. for (let j = 0; j < points.length; j++) {
  3593. let item = points[j];
  3594. if (startPoint == 0 && item.x > leftSpace) {
  3595. context.moveTo(item.x, item.y);
  3596. startPoint = 1;
  3597. }
  3598. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3599. var ctrlPoint = createCurveControlPoints(points, j - 1);
  3600. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item.y);
  3601. }
  3602. };
  3603. } else {
  3604. for (let j = 0; j < points.length; j++) {
  3605. let item = points[j];
  3606. if (startPoint == 0 && item.x > leftSpace) {
  3607. context.moveTo(item.x, item.y);
  3608. startPoint = 1;
  3609. }
  3610. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3611. context.lineTo(item.x, item.y);
  3612. }
  3613. };
  3614. }
  3615. context.lineTo(lastPoint.x, endY);
  3616. context.lineTo(firstPoint.x, endY);
  3617. context.lineTo(firstPoint.x, firstPoint.y);
  3618. } else {
  3619. let item = points[0];
  3620. context.moveTo(item.x - eachSpacing / 2, item.y);
  3621. context.lineTo(item.x + eachSpacing / 2, item.y);
  3622. context.lineTo(item.x + eachSpacing / 2, endY);
  3623. context.lineTo(item.x - eachSpacing / 2, endY);
  3624. context.moveTo(item.x - eachSpacing / 2, item.y);
  3625. }
  3626. context.closePath();
  3627. context.fill();
  3628. }
  3629. }
  3630. // 绘制折线数据图
  3631. if (eachSeries.type == 'line') {
  3632. var splitPointList = splitPoints(points,eachSeries);
  3633. splitPointList.forEach(function(points, index) {
  3634. if (eachSeries.lineType == 'dash') {
  3635. let dashLength = eachSeries.dashLength ? eachSeries.dashLength : 8;
  3636. dashLength *= opts.pix;
  3637. context.setLineDash([dashLength, dashLength]);
  3638. }
  3639. context.beginPath();
  3640. context.setStrokeStyle(eachSeries.color);
  3641. context.setLineWidth(2 * opts.pix);
  3642. if (points.length === 1) {
  3643. context.moveTo(points[0].x, points[0].y);
  3644. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  3645. } else {
  3646. context.moveTo(points[0].x, points[0].y);
  3647. let startPoint = 0;
  3648. if (eachSeries.style == 'curve') {
  3649. for (let j = 0; j < points.length; j++) {
  3650. let item = points[j];
  3651. if (startPoint == 0 && item.x > leftSpace) {
  3652. context.moveTo(item.x, item.y);
  3653. startPoint = 1;
  3654. }
  3655. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3656. var ctrlPoint = createCurveControlPoints(points, j - 1);
  3657. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y,
  3658. item.x, item.y);
  3659. }
  3660. }
  3661. } else {
  3662. for (let j = 0; j < points.length; j++) {
  3663. let item = points[j];
  3664. if (startPoint == 0 && item.x > leftSpace) {
  3665. context.moveTo(item.x, item.y);
  3666. startPoint = 1;
  3667. }
  3668. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3669. context.lineTo(item.x, item.y);
  3670. }
  3671. }
  3672. }
  3673. context.moveTo(points[0].x, points[0].y);
  3674. }
  3675. context.stroke();
  3676. context.setLineDash([]);
  3677. });
  3678. }
  3679. // 绘制点数据图
  3680. if (eachSeries.type == 'point') {
  3681. eachSeries.addPoint = true;
  3682. }
  3683. if (eachSeries.addPoint == true && eachSeries.type !== 'column') {
  3684. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  3685. }
  3686. });
  3687. if (opts.dataLabel !== false && process === 1) {
  3688. var columnIndex = 0;
  3689. series.forEach(function(eachSeries, seriesIndex) {
  3690. let ranges, minRange, maxRange;
  3691. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3692. minRange = ranges.pop();
  3693. maxRange = ranges.shift();
  3694. var data = eachSeries.data;
  3695. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3696. if (eachSeries.type !== 'column') {
  3697. drawPointText(points, eachSeries, config, context, opts);
  3698. } else {
  3699. points = fixColumeData(points, eachSpacing, columnLength, columnIndex, config, opts);
  3700. drawPointText(points, eachSeries, config, context, opts);
  3701. columnIndex += 1;
  3702. }
  3703. });
  3704. }
  3705. context.restore();
  3706. return {
  3707. xAxisPoints: xAxisPoints,
  3708. calPoints: calPoints,
  3709. eachSpacing: eachSpacing,
  3710. }
  3711. }
  3712. function drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints) {
  3713. var toolTipOption = opts.extra.tooltip || {};
  3714. if (toolTipOption.horizentalLine && opts.tooltip && process === 1 && (opts.type == 'line' || opts.type == 'area' || opts.type == 'column' || opts.type == 'candle' || opts.type == 'mix')) {
  3715. drawToolTipHorizentalLine(opts, config, context, eachSpacing, xAxisPoints)
  3716. }
  3717. context.save();
  3718. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3719. context.translate(opts._scrollDistance_, 0);
  3720. }
  3721. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  3722. drawToolTip(opts.tooltip.textList, opts.tooltip.offset, opts, config, context, eachSpacing, xAxisPoints);
  3723. }
  3724. context.restore();
  3725. }
  3726. function drawXAxis(categories, opts, config, context) {
  3727. let xAxisData = opts.chartData.xAxisData,
  3728. xAxisPoints = xAxisData.xAxisPoints,
  3729. startX = xAxisData.startX,
  3730. endX = xAxisData.endX,
  3731. eachSpacing = xAxisData.eachSpacing;
  3732. var boundaryGap = 'center';
  3733. if (opts.type == 'bar' || opts.type == 'line' || opts.type == 'area'|| opts.type == 'scatter' || opts.type == 'bubble') {
  3734. boundaryGap = opts.xAxis.boundaryGap;
  3735. }
  3736. var startY = opts.height - opts.area[2];
  3737. var endY = opts.area[0];
  3738. //绘制滚动条
  3739. if (opts.enableScroll && opts.xAxis.scrollShow) {
  3740. var scrollY = opts.height - opts.area[2] + config.xAxisHeight;
  3741. var scrollScreenWidth = endX - startX;
  3742. var scrollTotalWidth = eachSpacing * (xAxisPoints.length - 1);
  3743. var scrollWidth = scrollScreenWidth * scrollScreenWidth / scrollTotalWidth;
  3744. var scrollLeft = 0;
  3745. if (opts._scrollDistance_) {
  3746. scrollLeft = -opts._scrollDistance_ * (scrollScreenWidth) / scrollTotalWidth;
  3747. }
  3748. context.beginPath();
  3749. context.setLineCap('round');
  3750. context.setLineWidth(6 * opts.pix);
  3751. context.setStrokeStyle(opts.xAxis.scrollBackgroundColor || "#EFEBEF");
  3752. context.moveTo(startX, scrollY);
  3753. context.lineTo(endX, scrollY);
  3754. context.stroke();
  3755. context.closePath();
  3756. context.beginPath();
  3757. context.setLineCap('round');
  3758. context.setLineWidth(6 * opts.pix);
  3759. context.setStrokeStyle(opts.xAxis.scrollColor || "#A6A6A6");
  3760. context.moveTo(startX + scrollLeft, scrollY);
  3761. context.lineTo(startX + scrollLeft + scrollWidth, scrollY);
  3762. context.stroke();
  3763. context.closePath();
  3764. context.setLineCap('butt');
  3765. }
  3766. context.save();
  3767. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0) {
  3768. context.translate(opts._scrollDistance_, 0);
  3769. }
  3770. //绘制X轴刻度线
  3771. if (opts.xAxis.calibration === true) {
  3772. context.setStrokeStyle(opts.xAxis.gridColor || "#cccccc");
  3773. context.setLineCap('butt');
  3774. context.setLineWidth(1 * opts.pix);
  3775. xAxisPoints.forEach(function(item, index) {
  3776. if (index > 0) {
  3777. context.beginPath();
  3778. context.moveTo(item - eachSpacing / 2, startY);
  3779. context.lineTo(item - eachSpacing / 2, startY + 3 * opts.pix);
  3780. context.closePath();
  3781. context.stroke();
  3782. }
  3783. });
  3784. }
  3785. //绘制X轴网格
  3786. if (opts.xAxis.disableGrid !== true) {
  3787. context.setStrokeStyle(opts.xAxis.gridColor || "#cccccc");
  3788. context.setLineCap('butt');
  3789. context.setLineWidth(1 * opts.pix);
  3790. if (opts.xAxis.gridType == 'dash') {
  3791. context.setLineDash([opts.xAxis.dashLength * opts.pix, opts.xAxis.dashLength * opts.pix]);
  3792. }
  3793. opts.xAxis.gridEval = opts.xAxis.gridEval || 1;
  3794. xAxisPoints.forEach(function(item, index) {
  3795. if (index % opts.xAxis.gridEval == 0) {
  3796. context.beginPath();
  3797. context.moveTo(item, startY);
  3798. context.lineTo(item, endY);
  3799. context.stroke();
  3800. }
  3801. });
  3802. context.setLineDash([]);
  3803. }
  3804. //绘制X轴文案
  3805. if (opts.xAxis.disabled !== true) {
  3806. // 对X轴列表做抽稀处理
  3807. //默认全部显示X轴标签
  3808. let maxXAxisListLength = categories.length;
  3809. //如果设置了X轴单屏数量
  3810. if (opts.xAxis.labelCount) {
  3811. //如果设置X轴密度
  3812. if (opts.xAxis.itemCount) {
  3813. maxXAxisListLength = Math.ceil(categories.length / opts.xAxis.itemCount * opts.xAxis.labelCount);
  3814. } else {
  3815. maxXAxisListLength = opts.xAxis.labelCount;
  3816. }
  3817. maxXAxisListLength -= 1;
  3818. }
  3819. let ratio = Math.ceil(categories.length / maxXAxisListLength);
  3820. let newCategories = [];
  3821. let cgLength = categories.length;
  3822. for (let i = 0; i < cgLength; i++) {
  3823. if (i % ratio !== 0) {
  3824. newCategories.push("");
  3825. } else {
  3826. newCategories.push(categories[i]);
  3827. }
  3828. }
  3829. newCategories[cgLength - 1] = categories[cgLength - 1];
  3830. var xAxisFontSize = opts.xAxis.fontSize * opts.pix || config.fontSize;
  3831. if (config._xAxisTextAngle_ === 0) {
  3832. newCategories.forEach(function(item, index) {
  3833. var xitem = opts.xAxis.formatter ? opts.xAxis.formatter(item) : item;
  3834. var offset = -measureText(String(xitem), xAxisFontSize, context) / 2;
  3835. if (boundaryGap == 'center') {
  3836. offset += eachSpacing / 2;
  3837. }
  3838. var scrollHeight = 0;
  3839. if (opts.xAxis.scrollShow) {
  3840. scrollHeight = 6 * opts.pix;
  3841. }
  3842. context.beginPath();
  3843. context.setFontSize(xAxisFontSize);
  3844. context.setFillStyle(opts.xAxis.fontColor || opts.fontColor);
  3845. context.fillText(String(xitem), xAxisPoints[index] + offset, startY + xAxisFontSize + (config.xAxisHeight - scrollHeight - xAxisFontSize) / 2);
  3846. context.closePath();
  3847. context.stroke();
  3848. });
  3849. } else {
  3850. newCategories.forEach(function(item, index) {
  3851. var xitem = opts.xAxis.formatter ? opts.xAxis.formatter(item) : item;
  3852. context.save();
  3853. context.beginPath();
  3854. context.setFontSize(xAxisFontSize);
  3855. context.setFillStyle(opts.xAxis.fontColor || opts.fontColor);
  3856. var textWidth = measureText(String(xitem), xAxisFontSize, context);
  3857. var offset = -textWidth;
  3858. if (boundaryGap == 'center') {
  3859. offset += eachSpacing / 2;
  3860. }
  3861. var _calRotateTranslate = calRotateTranslate(xAxisPoints[index] + eachSpacing / 2, startY + xAxisFontSize / 2 + 5, opts.height),
  3862. transX = _calRotateTranslate.transX,
  3863. transY = _calRotateTranslate.transY;
  3864. context.rotate(-1 * config._xAxisTextAngle_);
  3865. context.translate(transX, transY);
  3866. context.fillText(String(xitem), xAxisPoints[index] + offset, startY + xAxisFontSize + 5);
  3867. context.closePath();
  3868. context.stroke();
  3869. context.restore();
  3870. });
  3871. }
  3872. }
  3873. context.restore();
  3874. //绘制X轴轴线
  3875. if (opts.xAxis.axisLine) {
  3876. context.beginPath();
  3877. context.setStrokeStyle(opts.xAxis.axisLineColor);
  3878. context.setLineWidth(1 * opts.pix);
  3879. context.moveTo(startX, opts.height - opts.area[2]);
  3880. context.lineTo(endX, opts.height - opts.area[2]);
  3881. context.stroke();
  3882. }
  3883. }
  3884. function drawYAxisGrid(categories, opts, config, context) {
  3885. if (opts.yAxis.disableGrid === true) {
  3886. return;
  3887. }
  3888. let spacingValid = opts.height - opts.area[0] - opts.area[2];
  3889. let eachSpacing = spacingValid / opts.yAxis.splitNumber;
  3890. let startX = opts.area[3];
  3891. let xAxisPoints = opts.chartData.xAxisData.xAxisPoints,
  3892. xAxiseachSpacing = opts.chartData.xAxisData.eachSpacing;
  3893. let TotalWidth = xAxiseachSpacing * (xAxisPoints.length - 1);
  3894. let endX = startX + TotalWidth;
  3895. let points = [];
  3896. let startY = 1
  3897. if (opts.xAxis.axisLine === false) {
  3898. startY = 0
  3899. }
  3900. for (let i = startY; i < opts.yAxis.splitNumber + 1; i++) {
  3901. points.push(opts.height - opts.area[2] - eachSpacing * i);
  3902. }
  3903. context.save();
  3904. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0) {
  3905. context.translate(opts._scrollDistance_, 0);
  3906. }
  3907. if (opts.yAxis.gridType == 'dash') {
  3908. context.setLineDash([opts.yAxis.dashLength * opts.pix, opts.yAxis.dashLength * opts.pix]);
  3909. }
  3910. context.setStrokeStyle(opts.yAxis.gridColor);
  3911. context.setLineWidth(1 * opts.pix);
  3912. points.forEach(function(item, index) {
  3913. context.beginPath();
  3914. context.moveTo(startX, item);
  3915. context.lineTo(endX, item);
  3916. context.stroke();
  3917. });
  3918. context.setLineDash([]);
  3919. context.restore();
  3920. }
  3921. function drawYAxis(series, opts, config, context) {
  3922. if (opts.yAxis.disabled === true) {
  3923. return;
  3924. }
  3925. var spacingValid = opts.height - opts.area[0] - opts.area[2];
  3926. var eachSpacing = spacingValid / opts.yAxis.splitNumber;
  3927. var startX = opts.area[3];
  3928. var endX = opts.width - opts.area[1];
  3929. var endY = opts.height - opts.area[2];
  3930. var fillEndY = endY + config.xAxisHeight;
  3931. if (opts.xAxis.scrollShow) {
  3932. fillEndY -= 3 * opts.pix;
  3933. }
  3934. if (opts.xAxis.rotateLabel) {
  3935. fillEndY = opts.height - opts.area[2] + opts.fontSize * opts.pix / 2;
  3936. }
  3937. // set YAxis background
  3938. context.beginPath();
  3939. context.setFillStyle(opts.background);
  3940. if (opts.enableScroll == true && opts.xAxis.scrollPosition && opts.xAxis.scrollPosition !== 'left') {
  3941. context.fillRect(0, 0, startX, fillEndY);
  3942. }
  3943. if (opts.enableScroll == true && opts.xAxis.scrollPosition && opts.xAxis.scrollPosition !== 'right') {
  3944. context.fillRect(endX, 0, opts.width, fillEndY);
  3945. }
  3946. context.closePath();
  3947. context.stroke();
  3948. let tStartLeft = opts.area[3];
  3949. let tStartRight = opts.width - opts.area[1];
  3950. let tStartCenter = opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2;
  3951. if (opts.yAxis.data) {
  3952. for (let i = 0; i < opts.yAxis.data.length; i++) {
  3953. let yData = opts.yAxis.data[i];
  3954. var points = [];
  3955. if(yData.type === 'categories'){
  3956. for (let i = 0; i <= yData.categories.length; i++) {
  3957. points.push(opts.area[0] + spacingValid / yData.categories.length / 2 + spacingValid / yData.categories.length * i);
  3958. }
  3959. }else{
  3960. for (let i = 0; i <= opts.yAxis.splitNumber; i++) {
  3961. points.push(opts.area[0] + eachSpacing * i);
  3962. }
  3963. }
  3964. if (yData.disabled !== true) {
  3965. let rangesFormat = opts.chartData.yAxisData.rangesFormat[i];
  3966. let yAxisFontSize = yData.fontSize ? yData.fontSize * opts.pix : config.fontSize;
  3967. let yAxisWidth = opts.chartData.yAxisData.yAxisWidth[i];
  3968. let textAlign = yData.textAlign || "right";
  3969. //画Y轴刻度及文案
  3970. rangesFormat.forEach(function(item, index) {
  3971. var pos = points[index] ? points[index] : endY;
  3972. context.beginPath();
  3973. context.setFontSize(yAxisFontSize);
  3974. context.setLineWidth(1 * opts.pix);
  3975. context.setStrokeStyle(yData.axisLineColor || '#cccccc');
  3976. context.setFillStyle(yData.fontColor || opts.fontColor);
  3977. let tmpstrat = 0;
  3978. let gapwidth = 4 * opts.pix;
  3979. if (yAxisWidth.position == 'left') {
  3980. //画刻度线
  3981. if (yData.calibration == true) {
  3982. context.moveTo(tStartLeft, pos);
  3983. context.lineTo(tStartLeft - 3 * opts.pix, pos);
  3984. gapwidth += 3 * opts.pix;
  3985. }
  3986. //画文字
  3987. switch (textAlign) {
  3988. case "left":
  3989. context.setTextAlign('left');
  3990. tmpstrat = tStartLeft - yAxisWidth.width
  3991. break;
  3992. case "right":
  3993. context.setTextAlign('right');
  3994. tmpstrat = tStartLeft - gapwidth
  3995. break;
  3996. default:
  3997. context.setTextAlign('center');
  3998. tmpstrat = tStartLeft - yAxisWidth.width / 2
  3999. }
  4000. context.fillText(String(item), tmpstrat, pos + yAxisFontSize / 2 - 3 * opts.pix);
  4001. } else if (yAxisWidth.position == 'right') {
  4002. //画刻度线
  4003. if (yData.calibration == true) {
  4004. context.moveTo(tStartRight, pos);
  4005. context.lineTo(tStartRight + 3 * opts.pix, pos);
  4006. gapwidth += 3 * opts.pix;
  4007. }
  4008. switch (textAlign) {
  4009. case "left":
  4010. context.setTextAlign('left');
  4011. tmpstrat = tStartRight + gapwidth
  4012. break;
  4013. case "right":
  4014. context.setTextAlign('right');
  4015. tmpstrat = tStartRight + yAxisWidth.width
  4016. break;
  4017. default:
  4018. context.setTextAlign('center');
  4019. tmpstrat = tStartRight + yAxisWidth.width / 2
  4020. }
  4021. context.fillText(String(item), tmpstrat, pos + yAxisFontSize / 2 - 3 * opts.pix);
  4022. } else if (yAxisWidth.position == 'center') {
  4023. //画刻度线
  4024. if (yData.calibration == true) {
  4025. context.moveTo(tStartCenter, pos);
  4026. context.lineTo(tStartCenter - 3 * opts.pix, pos);
  4027. gapwidth += 3 * opts.pix;
  4028. }
  4029. //画文字
  4030. switch (textAlign) {
  4031. case "left":
  4032. context.setTextAlign('left');
  4033. tmpstrat = tStartCenter - yAxisWidth.width
  4034. break;
  4035. case "right":
  4036. context.setTextAlign('right');
  4037. tmpstrat = tStartCenter - gapwidth
  4038. break;
  4039. default:
  4040. context.setTextAlign('center');
  4041. tmpstrat = tStartCenter - yAxisWidth.width / 2
  4042. }
  4043. context.fillText(String(item), tmpstrat, pos + yAxisFontSize / 2 - 3 * opts.pix);
  4044. }
  4045. context.closePath();
  4046. context.stroke();
  4047. context.setTextAlign('left');
  4048. });
  4049. //画Y轴轴线
  4050. if (yData.axisLine !== false) {
  4051. context.beginPath();
  4052. context.setStrokeStyle(yData.axisLineColor || '#cccccc');
  4053. context.setLineWidth(1 * opts.pix);
  4054. if (yAxisWidth.position == 'left') {
  4055. context.moveTo(tStartLeft, opts.height - opts.area[2]);
  4056. context.lineTo(tStartLeft, opts.area[0]);
  4057. } else if (yAxisWidth.position == 'right') {
  4058. context.moveTo(tStartRight, opts.height - opts.area[2]);
  4059. context.lineTo(tStartRight, opts.area[0]);
  4060. } else if (yAxisWidth.position == 'center') {
  4061. context.moveTo(tStartCenter, opts.height - opts.area[2]);
  4062. context.lineTo(tStartCenter, opts.area[0]);
  4063. }
  4064. context.stroke();
  4065. }
  4066. //画Y轴标题
  4067. if (opts.yAxis.showTitle) {
  4068. let titleFontSize = yData.titleFontSize * opts.pix || config.fontSize;
  4069. let title = yData.title;
  4070. context.beginPath();
  4071. context.setFontSize(titleFontSize);
  4072. context.setFillStyle(yData.titleFontColor || opts.fontColor);
  4073. if (yAxisWidth.position == 'left') {
  4074. context.fillText(title, tStartLeft - measureText(title, titleFontSize, context) / 2 + (yData.titleOffsetX || 0), opts.area[0] - (10 - (yData.titleOffsetY || 0)) * opts.pix);
  4075. } else if (yAxisWidth.position == 'right') {
  4076. context.fillText(title, tStartRight - measureText(title, titleFontSize, context) / 2 + (yData.titleOffsetX || 0), opts.area[0] - (10 - (yData.titleOffsetY || 0)) * opts.pix);
  4077. } else if (yAxisWidth.position == 'center') {
  4078. context.fillText(title, tStartCenter - measureText(title, titleFontSize, context) / 2 + (yData.titleOffsetX || 0), opts.area[0] - (10 - (yData.titleOffsetY || 0)) * opts.pix);
  4079. }
  4080. context.closePath();
  4081. context.stroke();
  4082. }
  4083. if (yAxisWidth.position == 'left') {
  4084. tStartLeft -= (yAxisWidth.width + opts.yAxis.padding * opts.pix);
  4085. } else {
  4086. tStartRight += yAxisWidth.width + opts.yAxis.padding * opts.pix;
  4087. }
  4088. }
  4089. }
  4090. }
  4091. }
  4092. function drawLegend(series, opts, config, context, chartData) {
  4093. if (opts.legend.show === false) {
  4094. return;
  4095. }
  4096. let legendData = chartData.legendData;
  4097. let legendList = legendData.points;
  4098. let legendArea = legendData.area;
  4099. let padding = opts.legend.padding * opts.pix;
  4100. let fontSize = opts.legend.fontSize * opts.pix;
  4101. let shapeWidth = 15 * opts.pix;
  4102. let shapeRight = 5 * opts.pix;
  4103. let itemGap = opts.legend.itemGap * opts.pix;
  4104. let lineHeight = Math.max(opts.legend.lineHeight * opts.pix, fontSize);
  4105. //画背景及边框
  4106. context.beginPath();
  4107. context.setLineWidth(opts.legend.borderWidth * opts.pix);
  4108. context.setStrokeStyle(opts.legend.borderColor);
  4109. context.setFillStyle(opts.legend.backgroundColor);
  4110. context.moveTo(legendArea.start.x, legendArea.start.y);
  4111. context.rect(legendArea.start.x, legendArea.start.y, legendArea.width, legendArea.height);
  4112. context.closePath();
  4113. context.fill();
  4114. context.stroke();
  4115. legendList.forEach(function(itemList, listIndex) {
  4116. let width = 0;
  4117. let height = 0;
  4118. width = legendData.widthArr[listIndex];
  4119. height = legendData.heightArr[listIndex];
  4120. let startX = 0;
  4121. let startY = 0;
  4122. if (opts.legend.position == 'top' || opts.legend.position == 'bottom') {
  4123. switch (opts.legend.float) {
  4124. case 'left':
  4125. startX = legendArea.start.x + padding;
  4126. break;
  4127. case 'right':
  4128. startX = legendArea.start.x + legendArea.width - width;
  4129. break;
  4130. default:
  4131. startX = legendArea.start.x + (legendArea.width - width) / 2;
  4132. }
  4133. startY = legendArea.start.y + padding + listIndex * lineHeight;
  4134. } else {
  4135. if (listIndex == 0) {
  4136. width = 0;
  4137. } else {
  4138. width = legendData.widthArr[listIndex - 1];
  4139. }
  4140. startX = legendArea.start.x + padding + width;
  4141. startY = legendArea.start.y + padding + (legendArea.height - height) / 2;
  4142. }
  4143. context.setFontSize(config.fontSize);
  4144. for (let i = 0; i < itemList.length; i++) {
  4145. let item = itemList[i];
  4146. item.area = [0, 0, 0, 0];
  4147. item.area[0] = startX;
  4148. item.area[1] = startY;
  4149. item.area[3] = startY + lineHeight;
  4150. context.beginPath();
  4151. context.setLineWidth(1 * opts.pix);
  4152. context.setStrokeStyle(item.show ? item.color : opts.legend.hiddenColor);
  4153. context.setFillStyle(item.show ? item.color : opts.legend.hiddenColor);
  4154. switch (item.legendShape) {
  4155. case 'line':
  4156. context.moveTo(startX, startY + 0.5 * lineHeight - 2 * opts.pix);
  4157. context.fillRect(startX, startY + 0.5 * lineHeight - 2 * opts.pix, 15 * opts.pix, 4 * opts.pix);
  4158. break;
  4159. case 'triangle':
  4160. context.moveTo(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight - 5 * opts.pix);
  4161. context.lineTo(startX + 2.5 * opts.pix, startY + 0.5 * lineHeight + 5 * opts.pix);
  4162. context.lineTo(startX + 12.5 * opts.pix, startY + 0.5 * lineHeight + 5 * opts.pix);
  4163. context.lineTo(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight - 5 * opts.pix);
  4164. break;
  4165. case 'diamond':
  4166. context.moveTo(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight - 5 * opts.pix);
  4167. context.lineTo(startX + 2.5 * opts.pix, startY + 0.5 * lineHeight);
  4168. context.lineTo(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight + 5 * opts.pix);
  4169. context.lineTo(startX + 12.5 * opts.pix, startY + 0.5 * lineHeight);
  4170. context.lineTo(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight - 5 * opts.pix);
  4171. break;
  4172. case 'circle':
  4173. context.moveTo(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight);
  4174. context.arc(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight, 5 * opts.pix, 0, 2 * Math.PI);
  4175. break;
  4176. case 'rect':
  4177. context.moveTo(startX, startY + 0.5 * lineHeight - 5 * opts.pix);
  4178. context.fillRect(startX, startY + 0.5 * lineHeight - 5 * opts.pix, 15 * opts.pix, 10 * opts.pix);
  4179. break;
  4180. case 'square':
  4181. context.moveTo(startX + 5 * opts.pix, startY + 0.5 * lineHeight - 5 * opts.pix);
  4182. context.fillRect(startX + 5 * opts.pix, startY + 0.5 * lineHeight - 5 * opts.pix, 10 * opts.pix, 10 * opts.pix);
  4183. break;
  4184. case 'none':
  4185. break;
  4186. default:
  4187. context.moveTo(startX, startY + 0.5 * lineHeight - 5 * opts.pix);
  4188. context.fillRect(startX, startY + 0.5 * lineHeight - 5 * opts.pix, 15 * opts.pix, 10 * opts.pix);
  4189. }
  4190. context.closePath();
  4191. context.fill();
  4192. context.stroke();
  4193. startX += shapeWidth + shapeRight;
  4194. let fontTrans = 0.5 * lineHeight + 0.5 * fontSize - 2;
  4195. context.beginPath();
  4196. context.setFontSize(fontSize);
  4197. context.setFillStyle(item.show ? opts.legend.fontColor : opts.legend.hiddenColor);
  4198. context.fillText(item.name, startX, startY + fontTrans);
  4199. context.closePath();
  4200. context.stroke();
  4201. if (opts.legend.position == 'top' || opts.legend.position == 'bottom') {
  4202. startX += measureText(item.name, fontSize, context) + itemGap;
  4203. item.area[2] = startX;
  4204. } else {
  4205. item.area[2] = startX + measureText(item.name, fontSize, context) + itemGap;;
  4206. startX -= shapeWidth + shapeRight;
  4207. startY += lineHeight;
  4208. }
  4209. }
  4210. });
  4211. }
  4212. function drawPieDataPoints(series, opts, config, context) {
  4213. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4214. var pieOption = assign({}, {
  4215. activeOpacity: 0.5,
  4216. activeRadius: 10,
  4217. offsetAngle: 0,
  4218. labelWidth: 15,
  4219. ringWidth: 30,
  4220. customRadius: 0,
  4221. border: false,
  4222. borderWidth: 2,
  4223. borderColor: '#FFFFFF',
  4224. centerColor: '#FFFFFF',
  4225. linearType: 'none',
  4226. customColor: [],
  4227. }, opts.type == "pie" ? opts.extra.pie : opts.extra.ring);
  4228. var centerPosition = {
  4229. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  4230. y: opts.area[0] + (opts.height - opts.area[0] - opts.area[2]) / 2
  4231. };
  4232. if (config.pieChartLinePadding == 0) {
  4233. config.pieChartLinePadding = pieOption.activeRadius * opts.pix;
  4234. }
  4235. var radius = Math.min((opts.width - opts.area[1] - opts.area[3]) / 2 - config.pieChartLinePadding - config.pieChartTextPadding - config._pieTextMaxLength_, (opts.height - opts.area[0] - opts.area[2]) / 2 - config.pieChartLinePadding - config.pieChartTextPadding);
  4236. if (pieOption.customRadius > 0) {
  4237. radius = pieOption.customRadius * opts.pix;
  4238. }
  4239. series = getPieDataPoints(series, radius, process);
  4240. var activeRadius = pieOption.activeRadius * opts.pix;
  4241. pieOption.customColor = fillCustomColor(pieOption.linearType, pieOption.customColor, series, config);
  4242. series = series.map(function(eachSeries) {
  4243. eachSeries._start_ += (pieOption.offsetAngle) * Math.PI / 180;
  4244. return eachSeries;
  4245. });
  4246. series.forEach(function(eachSeries, seriesIndex) {
  4247. if (opts.tooltip) {
  4248. if (opts.tooltip.index == seriesIndex) {
  4249. context.beginPath();
  4250. context.setFillStyle(hexToRgb(eachSeries.color, pieOption.activeOpacity || 0.5));
  4251. context.moveTo(centerPosition.x, centerPosition.y);
  4252. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_ + activeRadius, eachSeries._start_, eachSeries._start_ + 2 * eachSeries._proportion_ * Math.PI);
  4253. context.closePath();
  4254. context.fill();
  4255. }
  4256. }
  4257. context.beginPath();
  4258. context.setLineWidth(pieOption.borderWidth * opts.pix);
  4259. context.lineJoin = "round";
  4260. context.setStrokeStyle(pieOption.borderColor);
  4261. var fillcolor = eachSeries.color;
  4262. if (pieOption.linearType == 'custom') {
  4263. var grd;
  4264. if(context.createCircularGradient){
  4265. grd = context.createCircularGradient(centerPosition.x, centerPosition.y, eachSeries._radius_)
  4266. }else{
  4267. grd = context.createRadialGradient(centerPosition.x, centerPosition.y, 0,centerPosition.x, centerPosition.y, eachSeries._radius_)
  4268. }
  4269. grd.addColorStop(0, hexToRgb(pieOption.customColor[eachSeries.linearIndex], 1))
  4270. grd.addColorStop(1, hexToRgb(eachSeries.color, 1))
  4271. fillcolor = grd
  4272. }
  4273. context.setFillStyle(fillcolor);
  4274. context.moveTo(centerPosition.x, centerPosition.y);
  4275. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_, eachSeries._start_, eachSeries._start_ + 2 * eachSeries._proportion_ * Math.PI);
  4276. context.closePath();
  4277. context.fill();
  4278. if (pieOption.border == true) {
  4279. context.stroke();
  4280. }
  4281. });
  4282. if (opts.type === 'ring') {
  4283. var innerPieWidth = radius * 0.6;
  4284. if (typeof pieOption.ringWidth === 'number' && pieOption.ringWidth > 0) {
  4285. innerPieWidth = Math.max(0, radius - pieOption.ringWidth * opts.pix);
  4286. }
  4287. context.beginPath();
  4288. context.setFillStyle(pieOption.centerColor);
  4289. context.moveTo(centerPosition.x, centerPosition.y);
  4290. context.arc(centerPosition.x, centerPosition.y, innerPieWidth, 0, 2 * Math.PI);
  4291. context.closePath();
  4292. context.fill();
  4293. }
  4294. if (opts.dataLabel !== false && process === 1) {
  4295. var valid = false;
  4296. for (var i = 0, len = series.length; i < len; i++) {
  4297. if (series[i].data > 0) {
  4298. valid = true;
  4299. break;
  4300. }
  4301. }
  4302. if (valid) {
  4303. drawPieText(series, opts, config, context, radius, centerPosition);
  4304. }
  4305. }
  4306. if (process === 1 && opts.type === 'ring') {
  4307. drawRingTitle(opts, config, context, centerPosition);
  4308. }
  4309. return {
  4310. center: centerPosition,
  4311. radius: radius,
  4312. series: series
  4313. };
  4314. }
  4315. function drawRoseDataPoints(series, opts, config, context) {
  4316. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4317. var roseOption = assign({}, {
  4318. type: 'area',
  4319. activeOpacity: 0.5,
  4320. activeRadius: 10,
  4321. offsetAngle: 0,
  4322. labelWidth: 15,
  4323. border: false,
  4324. borderWidth: 2,
  4325. borderColor: '#FFFFFF',
  4326. linearType: 'none',
  4327. customColor: [],
  4328. }, opts.extra.rose);
  4329. if (config.pieChartLinePadding == 0) {
  4330. config.pieChartLinePadding = roseOption.activeRadius * opts.pix;
  4331. }
  4332. var centerPosition = {
  4333. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  4334. y: opts.area[0] + (opts.height - opts.area[0] - opts.area[2]) / 2
  4335. };
  4336. var radius = Math.min((opts.width - opts.area[1] - opts.area[3]) / 2 - config.pieChartLinePadding - config.pieChartTextPadding - config._pieTextMaxLength_, (opts.height - opts.area[0] - opts.area[2]) / 2 - config.pieChartLinePadding - config.pieChartTextPadding);
  4337. var minRadius = roseOption.minRadius || radius * 0.5;
  4338. series = getRoseDataPoints(series, roseOption.type, minRadius, radius, process);
  4339. var activeRadius = roseOption.activeRadius * opts.pix;
  4340. roseOption.customColor = fillCustomColor(roseOption.linearType, roseOption.customColor, series, config);
  4341. series = series.map(function(eachSeries) {
  4342. eachSeries._start_ += (roseOption.offsetAngle || 0) * Math.PI / 180;
  4343. return eachSeries;
  4344. });
  4345. series.forEach(function(eachSeries, seriesIndex) {
  4346. if (opts.tooltip) {
  4347. if (opts.tooltip.index == seriesIndex) {
  4348. context.beginPath();
  4349. context.setFillStyle(hexToRgb(eachSeries.color, roseOption.activeOpacity || 0.5));
  4350. context.moveTo(centerPosition.x, centerPosition.y);
  4351. context.arc(centerPosition.x, centerPosition.y, activeRadius + eachSeries._radius_, eachSeries._start_, eachSeries._start_ + 2 * eachSeries._rose_proportion_ * Math.PI);
  4352. context.closePath();
  4353. context.fill();
  4354. }
  4355. }
  4356. context.beginPath();
  4357. context.setLineWidth(roseOption.borderWidth * opts.pix);
  4358. context.lineJoin = "round";
  4359. context.setStrokeStyle(roseOption.borderColor);
  4360. var fillcolor = eachSeries.color;
  4361. if (roseOption.linearType == 'custom') {
  4362. var grd;
  4363. if(context.createCircularGradient){
  4364. grd = context.createCircularGradient(centerPosition.x, centerPosition.y, eachSeries._radius_)
  4365. }else{
  4366. grd = context.createRadialGradient(centerPosition.x, centerPosition.y, 0,centerPosition.x, centerPosition.y, eachSeries._radius_)
  4367. }
  4368. grd.addColorStop(0, hexToRgb(roseOption.customColor[eachSeries.linearIndex], 1))
  4369. grd.addColorStop(1, hexToRgb(eachSeries.color, 1))
  4370. fillcolor = grd
  4371. }
  4372. context.setFillStyle(fillcolor);
  4373. context.moveTo(centerPosition.x, centerPosition.y);
  4374. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_, eachSeries._start_, eachSeries._start_ + 2 * eachSeries._rose_proportion_ * Math.PI);
  4375. context.closePath();
  4376. context.fill();
  4377. if (roseOption.border == true) {
  4378. context.stroke();
  4379. }
  4380. });
  4381. if (opts.dataLabel !== false && process === 1) {
  4382. var valid = false;
  4383. for (var i = 0, len = series.length; i < len; i++) {
  4384. if (series[i].data > 0) {
  4385. valid = true;
  4386. break;
  4387. }
  4388. }
  4389. if (valid) {
  4390. drawPieText(series, opts, config, context, radius, centerPosition);
  4391. }
  4392. }
  4393. return {
  4394. center: centerPosition,
  4395. radius: radius,
  4396. series: series
  4397. };
  4398. }
  4399. function drawArcbarDataPoints(series, opts, config, context) {
  4400. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4401. var arcbarOption = assign({}, {
  4402. startAngle: 0.75,
  4403. endAngle: 0.25,
  4404. type: 'default',
  4405. width: 12 ,
  4406. gap: 2 ,
  4407. linearType: 'none',
  4408. customColor: [],
  4409. }, opts.extra.arcbar);
  4410. series = getArcbarDataPoints(series, arcbarOption, process);
  4411. var centerPosition;
  4412. if (arcbarOption.centerX || arcbarOption.centerY) {
  4413. centerPosition = {
  4414. x: arcbarOption.centerX ? arcbarOption.centerX : opts.width / 2,
  4415. y: arcbarOption.centerY ? arcbarOption.centerY : opts.height / 2
  4416. };
  4417. } else {
  4418. centerPosition = {
  4419. x: opts.width / 2,
  4420. y: opts.height / 2
  4421. };
  4422. }
  4423. var radius;
  4424. if (arcbarOption.radius) {
  4425. radius = arcbarOption.radius;
  4426. } else {
  4427. radius = Math.min(centerPosition.x, centerPosition.y);
  4428. radius -= 5 * opts.pix;
  4429. radius -= arcbarOption.width / 2;
  4430. }
  4431. arcbarOption.customColor = fillCustomColor(arcbarOption.linearType, arcbarOption.customColor, series, config);
  4432. for (let i = 0; i < series.length; i++) {
  4433. let eachSeries = series[i];
  4434. //背景颜色
  4435. context.setLineWidth(arcbarOption.width * opts.pix);
  4436. context.setStrokeStyle(arcbarOption.backgroundColor || '#E9E9E9');
  4437. context.setLineCap('round');
  4438. context.beginPath();
  4439. if (arcbarOption.type == 'default') {
  4440. context.arc(centerPosition.x, centerPosition.y, radius - (arcbarOption.width * opts.pix + arcbarOption.gap * opts.pix) * i, arcbarOption.startAngle * Math.PI, arcbarOption.endAngle * Math.PI, false);
  4441. } else {
  4442. context.arc(centerPosition.x, centerPosition.y, radius - (arcbarOption.width * opts.pix + arcbarOption.gap * opts.pix) * i, 0, 2 * Math.PI, false);
  4443. }
  4444. context.stroke();
  4445. //进度条
  4446. var fillColor = eachSeries.color
  4447. if(arcbarOption.linearType == 'custom'){
  4448. var grd = context.createLinearGradient(centerPosition.x - radius, centerPosition.y, centerPosition.x + radius, centerPosition.y);
  4449. grd.addColorStop(1, hexToRgb(arcbarOption.customColor[eachSeries.linearIndex], 1))
  4450. grd.addColorStop(0, hexToRgb(eachSeries.color, 1))
  4451. fillColor = grd;
  4452. }
  4453. context.setLineWidth(arcbarOption.width * opts.pix);
  4454. context.setStrokeStyle(fillColor);
  4455. context.setLineCap('round');
  4456. context.beginPath();
  4457. context.arc(centerPosition.x, centerPosition.y, radius - (arcbarOption.width * opts.pix + arcbarOption.gap * opts.pix) * i, arcbarOption.startAngle * Math.PI, eachSeries._proportion_ * Math.PI, false);
  4458. context.stroke();
  4459. }
  4460. drawRingTitle(opts, config, context, centerPosition);
  4461. return {
  4462. center: centerPosition,
  4463. radius: radius,
  4464. series: series
  4465. };
  4466. }
  4467. function drawGaugeDataPoints(categories, series, opts, config, context) {
  4468. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  4469. var gaugeOption = assign({}, {
  4470. type: 'default',
  4471. startAngle: 0.75,
  4472. endAngle: 0.25,
  4473. width: 15,
  4474. labelOffset:13,
  4475. splitLine: {
  4476. fixRadius: 0,
  4477. splitNumber: 10,
  4478. width: 15,
  4479. color: '#FFFFFF',
  4480. childNumber: 5,
  4481. childWidth: 5
  4482. },
  4483. pointer: {
  4484. width: 15,
  4485. color: 'auto'
  4486. }
  4487. }, opts.extra.gauge);
  4488. if (gaugeOption.oldAngle == undefined) {
  4489. gaugeOption.oldAngle = gaugeOption.startAngle;
  4490. }
  4491. if (gaugeOption.oldData == undefined) {
  4492. gaugeOption.oldData = 0;
  4493. }
  4494. categories = getGaugeAxisPoints(categories, gaugeOption.startAngle, gaugeOption.endAngle);
  4495. var centerPosition = {
  4496. x: opts.width / 2,
  4497. y: opts.height / 2
  4498. };
  4499. var radius = Math.min(centerPosition.x, centerPosition.y);
  4500. radius -= 5 * opts.pix;
  4501. radius -= gaugeOption.width / 2;
  4502. var innerRadius = radius - gaugeOption.width;
  4503. var totalAngle = 0;
  4504. //判断仪表盘的样式:default百度样式,progress新样式
  4505. if (gaugeOption.type == 'progress') {
  4506. //## 第一步画中心圆形背景和进度条背景
  4507. //中心圆形背景
  4508. var pieRadius = radius - gaugeOption.width * 3;
  4509. context.beginPath();
  4510. let gradient = context.createLinearGradient(centerPosition.x, centerPosition.y - pieRadius, centerPosition.x, centerPosition.y + pieRadius);
  4511. //配置渐变填充(起点:中心点向上减半径;结束点中心点向下加半径)
  4512. gradient.addColorStop('0', hexToRgb(series[0].color, 0.3));
  4513. gradient.addColorStop('1.0', hexToRgb("#FFFFFF", 0.1));
  4514. context.setFillStyle(gradient);
  4515. context.arc(centerPosition.x, centerPosition.y, pieRadius, 0, 2 * Math.PI, false);
  4516. context.fill();
  4517. //画进度条背景
  4518. context.setLineWidth(gaugeOption.width);
  4519. context.setStrokeStyle(hexToRgb(series[0].color, 0.3));
  4520. context.setLineCap('round');
  4521. context.beginPath();
  4522. context.arc(centerPosition.x, centerPosition.y, innerRadius, gaugeOption.startAngle * Math.PI, gaugeOption.endAngle * Math.PI, false);
  4523. context.stroke();
  4524. //## 第二步画刻度线
  4525. totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  4526. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  4527. let childAngle = totalAngle / gaugeOption.splitLine.splitNumber / gaugeOption.splitLine.childNumber;
  4528. let startX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius;
  4529. let endX = -radius - gaugeOption.width - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.width;
  4530. context.save();
  4531. context.translate(centerPosition.x, centerPosition.y);
  4532. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  4533. let len = gaugeOption.splitLine.splitNumber * gaugeOption.splitLine.childNumber + 1;
  4534. let proc = series[0].data * process;
  4535. for (let i = 0; i < len; i++) {
  4536. context.beginPath();
  4537. //刻度线随进度变色
  4538. if (proc > (i / len)) {
  4539. context.setStrokeStyle(hexToRgb(series[0].color, 1));
  4540. } else {
  4541. context.setStrokeStyle(hexToRgb(series[0].color, 0.3));
  4542. }
  4543. context.setLineWidth(3 * opts.pix);
  4544. context.moveTo(startX, 0);
  4545. context.lineTo(endX, 0);
  4546. context.stroke();
  4547. context.rotate(childAngle * Math.PI);
  4548. }
  4549. context.restore();
  4550. //## 第三步画进度条
  4551. series = getArcbarDataPoints(series, gaugeOption, process);
  4552. context.setLineWidth(gaugeOption.width);
  4553. context.setStrokeStyle(series[0].color);
  4554. context.setLineCap('round');
  4555. context.beginPath();
  4556. context.arc(centerPosition.x, centerPosition.y, innerRadius, gaugeOption.startAngle * Math.PI, series[0]._proportion_ * Math.PI, false);
  4557. context.stroke();
  4558. //## 第四步画指针
  4559. let pointerRadius = radius - gaugeOption.width * 2.5;
  4560. context.save();
  4561. context.translate(centerPosition.x, centerPosition.y);
  4562. context.rotate((series[0]._proportion_ - 1) * Math.PI);
  4563. context.beginPath();
  4564. context.setLineWidth(gaugeOption.width / 3);
  4565. let gradient3 = context.createLinearGradient(0, -pointerRadius * 0.6, 0, pointerRadius * 0.6);
  4566. gradient3.addColorStop('0', hexToRgb('#FFFFFF', 0));
  4567. gradient3.addColorStop('0.5', hexToRgb(series[0].color, 1));
  4568. gradient3.addColorStop('1.0', hexToRgb('#FFFFFF', 0));
  4569. context.setStrokeStyle(gradient3);
  4570. context.arc(0, 0, pointerRadius, 0.85 * Math.PI, 1.15 * Math.PI, false);
  4571. context.stroke();
  4572. context.beginPath();
  4573. context.setLineWidth(1);
  4574. context.setStrokeStyle(series[0].color);
  4575. context.setFillStyle(series[0].color);
  4576. context.moveTo(-pointerRadius - gaugeOption.width / 3 / 2, -4);
  4577. context.lineTo(-pointerRadius - gaugeOption.width / 3 / 2 - 4, 0);
  4578. context.lineTo(-pointerRadius - gaugeOption.width / 3 / 2, 4);
  4579. context.lineTo(-pointerRadius - gaugeOption.width / 3 / 2, -4);
  4580. context.stroke();
  4581. context.fill();
  4582. context.restore();
  4583. //default百度样式
  4584. } else {
  4585. //画背景
  4586. context.setLineWidth(gaugeOption.width);
  4587. context.setLineCap('butt');
  4588. for (let i = 0; i < categories.length; i++) {
  4589. let eachCategories = categories[i];
  4590. context.beginPath();
  4591. context.setStrokeStyle(eachCategories.color);
  4592. context.arc(centerPosition.x, centerPosition.y, radius, eachCategories._startAngle_ * Math.PI, eachCategories._endAngle_ * Math.PI, false);
  4593. context.stroke();
  4594. }
  4595. context.save();
  4596. //画刻度线
  4597. totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  4598. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  4599. let childAngle = totalAngle / gaugeOption.splitLine.splitNumber / gaugeOption.splitLine.childNumber;
  4600. let startX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius;
  4601. let endX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.width;
  4602. let childendX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.childWidth;
  4603. context.translate(centerPosition.x, centerPosition.y);
  4604. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  4605. for (let i = 0; i < gaugeOption.splitLine.splitNumber + 1; i++) {
  4606. context.beginPath();
  4607. context.setStrokeStyle(gaugeOption.splitLine.color);
  4608. context.setLineWidth(2 * opts.pix);
  4609. context.moveTo(startX, 0);
  4610. context.lineTo(endX, 0);
  4611. context.stroke();
  4612. context.rotate(splitAngle * Math.PI);
  4613. }
  4614. context.restore();
  4615. context.save();
  4616. context.translate(centerPosition.x, centerPosition.y);
  4617. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  4618. for (let i = 0; i < gaugeOption.splitLine.splitNumber * gaugeOption.splitLine.childNumber + 1; i++) {
  4619. context.beginPath();
  4620. context.setStrokeStyle(gaugeOption.splitLine.color);
  4621. context.setLineWidth(1 * opts.pix);
  4622. context.moveTo(startX, 0);
  4623. context.lineTo(childendX, 0);
  4624. context.stroke();
  4625. context.rotate(childAngle * Math.PI);
  4626. }
  4627. context.restore();
  4628. //画指针
  4629. series = getGaugeDataPoints(series, categories, gaugeOption, process);
  4630. for (let i = 0; i < series.length; i++) {
  4631. let eachSeries = series[i];
  4632. context.save();
  4633. context.translate(centerPosition.x, centerPosition.y);
  4634. context.rotate((eachSeries._proportion_ - 1) * Math.PI);
  4635. context.beginPath();
  4636. context.setFillStyle(eachSeries.color);
  4637. context.moveTo(gaugeOption.pointer.width, 0);
  4638. context.lineTo(0, -gaugeOption.pointer.width / 2);
  4639. context.lineTo(-innerRadius, 0);
  4640. context.lineTo(0, gaugeOption.pointer.width / 2);
  4641. context.lineTo(gaugeOption.pointer.width, 0);
  4642. context.closePath();
  4643. context.fill();
  4644. context.beginPath();
  4645. context.setFillStyle('#FFFFFF');
  4646. context.arc(0, 0, gaugeOption.pointer.width / 6, 0, 2 * Math.PI, false);
  4647. context.fill();
  4648. context.restore();
  4649. }
  4650. if (opts.dataLabel !== false) {
  4651. drawGaugeLabel(gaugeOption, radius, centerPosition, opts, config, context);
  4652. }
  4653. }
  4654. //画仪表盘标题,副标题
  4655. drawRingTitle(opts, config, context, centerPosition);
  4656. if (process === 1 && opts.type === 'gauge') {
  4657. opts.extra.gauge.oldAngle = series[0]._proportion_;
  4658. opts.extra.gauge.oldData = series[0].data;
  4659. }
  4660. return {
  4661. center: centerPosition,
  4662. radius: radius,
  4663. innerRadius: innerRadius,
  4664. categories: categories,
  4665. totalAngle: totalAngle
  4666. };
  4667. }
  4668. function drawRadarDataPoints(series, opts, config, context) {
  4669. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4670. var radarOption = assign({}, {
  4671. gridColor: '#cccccc',
  4672. gridType: 'radar',
  4673. opacity: 0.2,
  4674. gridCount: 3,
  4675. border:false,
  4676. borderWidth:2
  4677. }, opts.extra.radar);
  4678. var coordinateAngle = getRadarCoordinateSeries(opts.categories.length);
  4679. var centerPosition = {
  4680. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  4681. y: opts.area[0] + (opts.height - opts.area[0] - opts.area[2]) / 2
  4682. };
  4683. var xr = (opts.width - opts.area[1] - opts.area[3]) / 2
  4684. var yr = (opts.height - opts.area[0] - opts.area[2]) / 2
  4685. var radius = Math.min(xr - (getMaxTextListLength(opts.categories, config.fontSize, context) + config.radarLabelTextMargin), yr - config.radarLabelTextMargin);
  4686. radius -= config.radarLabelTextMargin * opts.pix;
  4687. // 画分割线
  4688. context.beginPath();
  4689. context.setLineWidth(1 * opts.pix);
  4690. context.setStrokeStyle(radarOption.gridColor);
  4691. coordinateAngle.forEach(function(angle) {
  4692. var pos = convertCoordinateOrigin(radius * Math.cos(angle), radius * Math.sin(angle), centerPosition);
  4693. context.moveTo(centerPosition.x, centerPosition.y);
  4694. context.lineTo(pos.x, pos.y);
  4695. });
  4696. context.stroke();
  4697. context.closePath();
  4698. // 画背景网格
  4699. var _loop = function _loop(i) {
  4700. var startPos = {};
  4701. context.beginPath();
  4702. context.setLineWidth(1 * opts.pix);
  4703. context.setStrokeStyle(radarOption.gridColor);
  4704. if (radarOption.gridType == 'radar') {
  4705. coordinateAngle.forEach(function(angle, index) {
  4706. var pos = convertCoordinateOrigin(radius / radarOption.gridCount * i * Math.cos(angle), radius /
  4707. radarOption.gridCount * i * Math.sin(angle), centerPosition);
  4708. if (index === 0) {
  4709. startPos = pos;
  4710. context.moveTo(pos.x, pos.y);
  4711. } else {
  4712. context.lineTo(pos.x, pos.y);
  4713. }
  4714. });
  4715. context.lineTo(startPos.x, startPos.y);
  4716. } else {
  4717. var pos = convertCoordinateOrigin(radius / radarOption.gridCount * i * Math.cos(1.5), radius / radarOption.gridCount * i * Math.sin(1.5), centerPosition);
  4718. context.arc(centerPosition.x, centerPosition.y, centerPosition.y - pos.y, 0, 2 * Math.PI, false);
  4719. }
  4720. context.stroke();
  4721. context.closePath();
  4722. };
  4723. for (var i = 1; i <= radarOption.gridCount; i++) {
  4724. _loop(i);
  4725. }
  4726. var radarDataPoints = getRadarDataPoints(coordinateAngle, centerPosition, radius, series, opts, process);
  4727. radarDataPoints.forEach(function(eachSeries, seriesIndex) {
  4728. // 绘制区域数据
  4729. context.beginPath();
  4730. context.setLineWidth(radarOption.borderWidth * opts.pix);
  4731. context.setStrokeStyle(eachSeries.color);
  4732. context.setFillStyle(hexToRgb(eachSeries.color, radarOption.opacity));
  4733. eachSeries.data.forEach(function(item, index) {
  4734. if (index === 0) {
  4735. context.moveTo(item.position.x, item.position.y);
  4736. } else {
  4737. context.lineTo(item.position.x, item.position.y);
  4738. }
  4739. });
  4740. context.closePath();
  4741. context.fill();
  4742. if(radarOption.border === true){
  4743. context.stroke();
  4744. }
  4745. context.closePath();
  4746. if (opts.dataPointShape !== false) {
  4747. var points = eachSeries.data.map(function(item) {
  4748. return item.position;
  4749. });
  4750. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  4751. }
  4752. });
  4753. // draw label text
  4754. drawRadarLabel(coordinateAngle, radius, centerPosition, opts, config, context);
  4755. // draw dataLabel
  4756. if (opts.dataLabel !== false && process === 1) {
  4757. radarDataPoints.forEach(function(eachSeries, seriesIndex) {
  4758. context.beginPath();
  4759. var fontSize = eachSeries.textSize * opts.pix || config.fontSize;
  4760. context.setFontSize(fontSize);
  4761. context.setFillStyle(eachSeries.textColor || opts.fontColor);
  4762. eachSeries.data.forEach(function(item, index) {
  4763. //如果是中心点垂直的上下点位
  4764. if(Math.abs(item.position.x - centerPosition.x)<2){
  4765. //如果在上面
  4766. if(item.position.y < centerPosition.y){
  4767. context.setTextAlign('center');
  4768. context.fillText(item.value, item.position.x, item.position.y - 4);
  4769. }else{
  4770. context.setTextAlign('center');
  4771. context.fillText(item.value, item.position.x, item.position.y + fontSize + 2);
  4772. }
  4773. }else{
  4774. //如果在左侧
  4775. if(item.position.x < centerPosition.x){
  4776. context.setTextAlign('right');
  4777. context.fillText(item.value, item.position.x - 4, item.position.y + fontSize / 2 - 2);
  4778. }else{
  4779. context.setTextAlign('left');
  4780. context.fillText(item.value, item.position.x + 4, item.position.y + fontSize / 2 - 2);
  4781. }
  4782. }
  4783. });
  4784. context.closePath();
  4785. context.stroke();
  4786. });
  4787. context.setTextAlign('left');
  4788. }
  4789. return {
  4790. center: centerPosition,
  4791. radius: radius,
  4792. angleList: coordinateAngle
  4793. };
  4794. }
  4795. function normalInt(min, max, iter) {
  4796. iter = iter == 0 ? 1 : iter;
  4797. var arr = [];
  4798. for (var i = 0; i < iter; i++) {
  4799. arr[i] = Math.random();
  4800. };
  4801. return Math.floor(arr.reduce(function(i, j) {
  4802. return i + j
  4803. }) / iter * (max - min)) + min;
  4804. };
  4805. function collisionNew(area, points, width, height) {
  4806. var isIn = false;
  4807. for (let i = 0; i < points.length; i++) {
  4808. if (points[i].area) {
  4809. if (area[3] < points[i].area[1] || area[0] > points[i].area[2] || area[1] > points[i].area[3] || area[2] < points[i].area[0]) {
  4810. if (area[0] < 0 || area[1] < 0 || area[2] > width || area[3] > height) {
  4811. isIn = true;
  4812. break;
  4813. } else {
  4814. isIn = false;
  4815. }
  4816. } else {
  4817. isIn = true;
  4818. break;
  4819. }
  4820. }
  4821. }
  4822. return isIn;
  4823. };
  4824. function getBoundingBox(data) {
  4825. var bounds = {},coords;
  4826. bounds.xMin = 180;
  4827. bounds.xMax = 0;
  4828. bounds.yMin = 90;
  4829. bounds.yMax = 0
  4830. for (var i = 0; i < data.length; i++) {
  4831. var coorda = data[i].geometry.coordinates
  4832. for (var k = 0; k < coorda.length; k++) {
  4833. coords = coorda[k];
  4834. if (coords.length == 1) {
  4835. coords = coords[0]
  4836. }
  4837. for (var j = 0; j < coords.length; j++) {
  4838. var longitude = coords[j][0];
  4839. var latitude = coords[j][1];
  4840. var point = {
  4841. x: longitude,
  4842. y: latitude
  4843. }
  4844. bounds.xMin = bounds.xMin < point.x ? bounds.xMin : point.x;
  4845. bounds.xMax = bounds.xMax > point.x ? bounds.xMax : point.x;
  4846. bounds.yMin = bounds.yMin < point.y ? bounds.yMin : point.y;
  4847. bounds.yMax = bounds.yMax > point.y ? bounds.yMax : point.y;
  4848. }
  4849. }
  4850. }
  4851. return bounds;
  4852. }
  4853. function coordinateToPoint(latitude, longitude, bounds, scale, xoffset, yoffset) {
  4854. return {
  4855. x: (longitude - bounds.xMin) * scale + xoffset,
  4856. y: (bounds.yMax - latitude) * scale + yoffset
  4857. };
  4858. }
  4859. function pointToCoordinate(pointY, pointX, bounds, scale, xoffset, yoffset) {
  4860. return {
  4861. x: (pointX - xoffset) / scale + bounds.xMin,
  4862. y: bounds.yMax - (pointY - yoffset) / scale
  4863. };
  4864. }
  4865. function isRayIntersectsSegment(poi, s_poi, e_poi) {
  4866. if (s_poi[1] == e_poi[1]) {
  4867. return false;
  4868. }
  4869. if (s_poi[1] > poi[1] && e_poi[1] > poi[1]) {
  4870. return false;
  4871. }
  4872. if (s_poi[1] < poi[1] && e_poi[1] < poi[1]) {
  4873. return false;
  4874. }
  4875. if (s_poi[1] == poi[1] && e_poi[1] > poi[1]) {
  4876. return false;
  4877. }
  4878. if (e_poi[1] == poi[1] && s_poi[1] > poi[1]) {
  4879. return false;
  4880. }
  4881. if (s_poi[0] < poi[0] && e_poi[1] < poi[1]) {
  4882. return false;
  4883. }
  4884. let xseg = e_poi[0] - (e_poi[0] - s_poi[0]) * (e_poi[1] - poi[1]) / (e_poi[1] - s_poi[1]);
  4885. if (xseg < poi[0]) {
  4886. return false;
  4887. } else {
  4888. return true;
  4889. }
  4890. }
  4891. function isPoiWithinPoly(poi, poly, mercator) {
  4892. let sinsc = 0;
  4893. for (let i = 0; i < poly.length; i++) {
  4894. let epoly = poly[i][0];
  4895. if (poly.length == 1) {
  4896. epoly = poly[i][0]
  4897. }
  4898. for (let j = 0; j < epoly.length - 1; j++) {
  4899. let s_poi = epoly[j];
  4900. let e_poi = epoly[j + 1];
  4901. if (mercator) {
  4902. s_poi = lonlat2mercator(epoly[j][0], epoly[j][1]);
  4903. e_poi = lonlat2mercator(epoly[j + 1][0], epoly[j + 1][1]);
  4904. }
  4905. if (isRayIntersectsSegment(poi, s_poi, e_poi)) {
  4906. sinsc += 1;
  4907. }
  4908. }
  4909. }
  4910. if (sinsc % 2 == 1) {
  4911. return true;
  4912. } else {
  4913. return false;
  4914. }
  4915. }
  4916. function drawMapDataPoints(series, opts, config, context) {
  4917. var mapOption = assign({}, {
  4918. border: true,
  4919. mercator: false,
  4920. borderWidth: 1,
  4921. borderColor: '#666666',
  4922. fillOpacity: 0.6,
  4923. activeBorderColor: '#f04864',
  4924. activeFillColor: '#facc14',
  4925. activeFillOpacity: 1
  4926. }, opts.extra.map);
  4927. var coords, point;
  4928. var data = series;
  4929. var bounds = getBoundingBox(data);
  4930. if (mapOption.mercator) {
  4931. var max = lonlat2mercator(bounds.xMax, bounds.yMax)
  4932. var min = lonlat2mercator(bounds.xMin, bounds.yMin)
  4933. bounds.xMax = max[0]
  4934. bounds.yMax = max[1]
  4935. bounds.xMin = min[0]
  4936. bounds.yMin = min[1]
  4937. }
  4938. var xScale = opts.width / Math.abs(bounds.xMax - bounds.xMin);
  4939. var yScale = opts.height / Math.abs(bounds.yMax - bounds.yMin);
  4940. var scale = xScale < yScale ? xScale : yScale;
  4941. var xoffset = opts.width / 2 - Math.abs(bounds.xMax - bounds.xMin) / 2 * scale;
  4942. var yoffset = opts.height / 2 - Math.abs(bounds.yMax - bounds.yMin) / 2 * scale;
  4943. for (var i = 0; i < data.length; i++) {
  4944. context.beginPath();
  4945. context.setLineWidth(mapOption.borderWidth * opts.pix);
  4946. context.setStrokeStyle(mapOption.borderColor);
  4947. context.setFillStyle(hexToRgb(series[i].color, mapOption.fillOpacity));
  4948. if (opts.tooltip) {
  4949. if (opts.tooltip.index == i) {
  4950. context.setStrokeStyle(mapOption.activeBorderColor);
  4951. context.setFillStyle(hexToRgb(mapOption.activeFillColor, mapOption.activeFillOpacity));
  4952. }
  4953. }
  4954. var coorda = data[i].geometry.coordinates
  4955. for (var k = 0; k < coorda.length; k++) {
  4956. coords = coorda[k];
  4957. if (coords.length == 1) {
  4958. coords = coords[0]
  4959. }
  4960. for (var j = 0; j < coords.length; j++) {
  4961. var gaosi = Array(2);
  4962. if (mapOption.mercator) {
  4963. gaosi = lonlat2mercator(coords[j][0], coords[j][1])
  4964. } else {
  4965. gaosi = coords[j]
  4966. }
  4967. point = coordinateToPoint(gaosi[1], gaosi[0], bounds, scale, xoffset, yoffset)
  4968. if (j === 0) {
  4969. context.beginPath();
  4970. context.moveTo(point.x, point.y);
  4971. } else {
  4972. context.lineTo(point.x, point.y);
  4973. }
  4974. }
  4975. context.fill();
  4976. if (mapOption.border == true) {
  4977. context.stroke();
  4978. }
  4979. }
  4980. if (opts.dataLabel == true) {
  4981. var centerPoint = data[i].properties.centroid;
  4982. if (centerPoint) {
  4983. if (mapOption.mercator) {
  4984. centerPoint = lonlat2mercator(data[i].properties.centroid[0], data[i].properties.centroid[1])
  4985. }
  4986. point = coordinateToPoint(centerPoint[1], centerPoint[0], bounds, scale, xoffset, yoffset);
  4987. let fontSize = data[i].textSize * opts.pix || config.fontSize;
  4988. let text = data[i].properties.name;
  4989. context.beginPath();
  4990. context.setFontSize(fontSize)
  4991. context.setFillStyle(data[i].textColor || opts.fontColor)
  4992. context.fillText(text, point.x - measureText(text, fontSize, context) / 2, point.y + fontSize / 2);
  4993. context.closePath();
  4994. context.stroke();
  4995. }
  4996. }
  4997. }
  4998. opts.chartData.mapData = {
  4999. bounds: bounds,
  5000. scale: scale,
  5001. xoffset: xoffset,
  5002. yoffset: yoffset,
  5003. mercator: mapOption.mercator
  5004. }
  5005. drawToolTipBridge(opts, config, context, 1);
  5006. context.draw();
  5007. }
  5008. function getWordCloudPoint(opts, type, context) {
  5009. let points = opts.series;
  5010. switch (type) {
  5011. case 'normal':
  5012. for (let i = 0; i < points.length; i++) {
  5013. let text = points[i].name;
  5014. let tHeight = points[i].textSize * opts.pix;
  5015. let tWidth = measureText(text, tHeight, context);
  5016. let x, y;
  5017. let area;
  5018. let breaknum = 0;
  5019. while (true) {
  5020. breaknum++;
  5021. x = normalInt(-opts.width / 2, opts.width / 2, 5) - tWidth / 2;
  5022. y = normalInt(-opts.height / 2, opts.height / 2, 5) + tHeight / 2;
  5023. area = [x - 5 + opts.width / 2, y - 5 - tHeight + opts.height / 2, x + tWidth + 5 + opts.width / 2, y + 5 +
  5024. opts.height / 2
  5025. ];
  5026. let isCollision = collisionNew(area, points, opts.width, opts.height);
  5027. if (!isCollision) break;
  5028. if (breaknum == 1000) {
  5029. area = [-100, -100, -100, -100];
  5030. break;
  5031. }
  5032. };
  5033. points[i].area = area;
  5034. }
  5035. break;
  5036. case 'vertical':
  5037. function Spin() {
  5038. //获取均匀随机值,是否旋转,旋转的概率为(1-0.5)
  5039. if (Math.random() > 0.7) {
  5040. return true;
  5041. } else {
  5042. return false
  5043. };
  5044. };
  5045. for (let i = 0; i < points.length; i++) {
  5046. let text = points[i].name;
  5047. let tHeight = points[i].textSize * opts.pix;
  5048. let tWidth = measureText(text, tHeight, context);
  5049. let isSpin = Spin();
  5050. let x, y, area, areav;
  5051. let breaknum = 0;
  5052. while (true) {
  5053. breaknum++;
  5054. let isCollision;
  5055. if (isSpin) {
  5056. x = normalInt(-opts.width / 2, opts.width / 2, 5) - tWidth / 2;
  5057. y = normalInt(-opts.height / 2, opts.height / 2, 5) + tHeight / 2;
  5058. area = [y - 5 - tWidth + opts.width / 2, (-x - 5 + opts.height / 2), y + 5 + opts.width / 2, (-x + tHeight + 5 + opts.height / 2)];
  5059. areav = [opts.width - (opts.width / 2 - opts.height / 2) - (-x + tHeight + 5 + opts.height / 2) - 5, (opts.height / 2 - opts.width / 2) + (y - 5 - tWidth + opts.width / 2) - 5, opts.width - (opts.width / 2 - opts.height / 2) - (-x + tHeight + 5 + opts.height / 2) + tHeight, (opts.height / 2 - opts.width / 2) + (y - 5 - tWidth + opts.width / 2) + tWidth + 5];
  5060. isCollision = collisionNew(areav, points, opts.height, opts.width);
  5061. } else {
  5062. x = normalInt(-opts.width / 2, opts.width / 2, 5) - tWidth / 2;
  5063. y = normalInt(-opts.height / 2, opts.height / 2, 5) + tHeight / 2;
  5064. area = [x - 5 + opts.width / 2, y - 5 - tHeight + opts.height / 2, x + tWidth + 5 + opts.width / 2, y + 5 + opts.height / 2];
  5065. isCollision = collisionNew(area, points, opts.width, opts.height);
  5066. }
  5067. if (!isCollision) break;
  5068. if (breaknum == 1000) {
  5069. area = [-1000, -1000, -1000, -1000];
  5070. break;
  5071. }
  5072. };
  5073. if (isSpin) {
  5074. points[i].area = areav;
  5075. points[i].areav = area;
  5076. } else {
  5077. points[i].area = area;
  5078. }
  5079. points[i].rotate = isSpin;
  5080. };
  5081. break;
  5082. }
  5083. return points;
  5084. }
  5085. function drawWordCloudDataPoints(series, opts, config, context) {
  5086. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  5087. let wordOption = assign({}, {
  5088. type: 'normal',
  5089. autoColors: true
  5090. }, opts.extra.word);
  5091. if (!opts.chartData.wordCloudData) {
  5092. opts.chartData.wordCloudData = getWordCloudPoint(opts, wordOption.type, context);
  5093. }
  5094. context.beginPath();
  5095. context.setFillStyle(opts.background);
  5096. context.rect(0, 0, opts.width, opts.height);
  5097. context.fill();
  5098. context.save();
  5099. let points = opts.chartData.wordCloudData;
  5100. context.translate(opts.width / 2, opts.height / 2);
  5101. for (let i = 0; i < points.length; i++) {
  5102. context.save();
  5103. if (points[i].rotate) {
  5104. context.rotate(90 * Math.PI / 180);
  5105. }
  5106. let text = points[i].name;
  5107. let tHeight = points[i].textSize * opts.pix;
  5108. let tWidth = measureText(text, tHeight, context);
  5109. context.beginPath();
  5110. context.setStrokeStyle(points[i].color);
  5111. context.setFillStyle(points[i].color);
  5112. context.setFontSize(tHeight);
  5113. if (points[i].rotate) {
  5114. if (points[i].areav[0] > 0) {
  5115. if (opts.tooltip) {
  5116. if (opts.tooltip.index == i) {
  5117. context.strokeText(text, (points[i].areav[0] + 5 - opts.width / 2) * process - tWidth * (1 - process) / 2, (points[i].areav[1] + 5 + tHeight - opts.height / 2) * process);
  5118. } else {
  5119. context.fillText(text, (points[i].areav[0] + 5 - opts.width / 2) * process - tWidth * (1 - process) / 2, (points[i].areav[1] + 5 + tHeight - opts.height / 2) * process);
  5120. }
  5121. } else {
  5122. context.fillText(text, (points[i].areav[0] + 5 - opts.width / 2) * process - tWidth * (1 - process) / 2, (points[i].areav[1] + 5 + tHeight - opts.height / 2) * process);
  5123. }
  5124. }
  5125. } else {
  5126. if (points[i].area[0] > 0) {
  5127. if (opts.tooltip) {
  5128. if (opts.tooltip.index == i) {
  5129. context.strokeText(text, (points[i].area[0] + 5 - opts.width / 2) * process - tWidth * (1 - process) / 2, (points[i].area[1] + 5 + tHeight - opts.height / 2) * process);
  5130. } else {
  5131. context.fillText(text, (points[i].area[0] + 5 - opts.width / 2) * process - tWidth * (1 - process) / 2, (points[i].area[1] + 5 + tHeight - opts.height / 2) * process);
  5132. }
  5133. } else {
  5134. context.fillText(text, (points[i].area[0] + 5 - opts.width / 2) * process - tWidth * (1 - process) / 2, (points[i].area[1] + 5 + tHeight - opts.height / 2) * process);
  5135. }
  5136. }
  5137. }
  5138. context.stroke();
  5139. context.restore();
  5140. }
  5141. context.restore();
  5142. }
  5143. function drawFunnelDataPoints(series, opts, config, context) {
  5144. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  5145. let funnelOption = assign({}, {
  5146. type:'funnel',
  5147. activeWidth: 10,
  5148. activeOpacity: 0.3,
  5149. border: false,
  5150. borderWidth: 2,
  5151. borderColor: '#FFFFFF',
  5152. fillOpacity: 1,
  5153. labelAlign: 'right',
  5154. linearType: 'none',
  5155. customColor: [],
  5156. }, opts.extra.funnel);
  5157. let eachSpacing = (opts.height - opts.area[0] - opts.area[2]) / series.length;
  5158. let centerPosition = {
  5159. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  5160. y: opts.height - opts.area[2]
  5161. };
  5162. let activeWidth = funnelOption.activeWidth * opts.pix;
  5163. let radius = Math.min((opts.width - opts.area[1] - opts.area[3]) / 2 - activeWidth, (opts.height - opts.area[0] - opts.area[2]) / 2 - activeWidth);
  5164. series = getFunnelDataPoints(series, radius, funnelOption.type, eachSpacing, process);
  5165. context.save();
  5166. context.translate(centerPosition.x, centerPosition.y);
  5167. funnelOption.customColor = fillCustomColor(funnelOption.linearType, funnelOption.customColor, series, config);
  5168. if(funnelOption.type == 'pyramid'){
  5169. for (let i = 0; i < series.length; i++) {
  5170. if (i == series.length -1) {
  5171. if (opts.tooltip) {
  5172. if (opts.tooltip.index == i) {
  5173. context.beginPath();
  5174. context.setFillStyle(hexToRgb(series[i].color, funnelOption.activeOpacity));
  5175. context.moveTo(-activeWidth, -eachSpacing);
  5176. context.lineTo(-series[i].radius - activeWidth, 0);
  5177. context.lineTo(series[i].radius + activeWidth, 0);
  5178. context.lineTo(activeWidth, -eachSpacing);
  5179. context.lineTo(-activeWidth, -eachSpacing);
  5180. context.closePath();
  5181. context.fill();
  5182. }
  5183. }
  5184. series[i].funnelArea = [centerPosition.x - series[i].radius, centerPosition.y - eachSpacing * (i + 1), centerPosition.x + series[i].radius, centerPosition.y - eachSpacing * i];
  5185. context.beginPath();
  5186. context.setLineWidth(funnelOption.borderWidth * opts.pix);
  5187. context.setStrokeStyle(funnelOption.borderColor);
  5188. var fillColor = hexToRgb(series[i].color, funnelOption.fillOpacity);
  5189. if (funnelOption.linearType == 'custom') {
  5190. var grd = context.createLinearGradient(series[i].radius, -eachSpacing, -series[i].radius, -eachSpacing);
  5191. grd.addColorStop(0, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5192. grd.addColorStop(0.5, hexToRgb(funnelOption.customColor[series[i].linearIndex], funnelOption.fillOpacity));
  5193. grd.addColorStop(1, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5194. fillColor = grd
  5195. }
  5196. context.setFillStyle(fillColor);
  5197. context.moveTo(0, -eachSpacing);
  5198. context.lineTo(-series[i].radius, 0);
  5199. context.lineTo(series[i].radius, 0);
  5200. context.lineTo(0, -eachSpacing);
  5201. context.closePath();
  5202. context.fill();
  5203. if (funnelOption.border == true) {
  5204. context.stroke();
  5205. }
  5206. } else {
  5207. if (opts.tooltip) {
  5208. if (opts.tooltip.index == i) {
  5209. context.beginPath();
  5210. context.setFillStyle(hexToRgb(series[i].color, funnelOption.activeOpacity));
  5211. context.moveTo(0, 0);
  5212. context.lineTo(-series[i].radius - activeWidth, 0);
  5213. context.lineTo(-series[i + 1].radius - activeWidth, -eachSpacing);
  5214. context.lineTo(series[i + 1].radius + activeWidth, -eachSpacing);
  5215. context.lineTo(series[i].radius + activeWidth, 0);
  5216. context.lineTo(0, 0);
  5217. context.closePath();
  5218. context.fill();
  5219. }
  5220. }
  5221. series[i].funnelArea = [centerPosition.x - series[i].radius, centerPosition.y - eachSpacing * (i + 1), centerPosition.x + series[i].radius, centerPosition.y - eachSpacing * i];
  5222. context.beginPath();
  5223. context.setLineWidth(funnelOption.borderWidth * opts.pix);
  5224. context.setStrokeStyle(funnelOption.borderColor);
  5225. var fillColor = hexToRgb(series[i].color, funnelOption.fillOpacity);
  5226. if (funnelOption.linearType == 'custom') {
  5227. var grd = context.createLinearGradient(series[i].radius, -eachSpacing, -series[i].radius, -eachSpacing);
  5228. grd.addColorStop(0, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5229. grd.addColorStop(0.5, hexToRgb(funnelOption.customColor[series[i].linearIndex], funnelOption.fillOpacity));
  5230. grd.addColorStop(1, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5231. fillColor = grd
  5232. }
  5233. context.setFillStyle(fillColor);
  5234. context.moveTo(0, 0);
  5235. context.lineTo(-series[i].radius, 0);
  5236. context.lineTo(-series[i + 1].radius, -eachSpacing);
  5237. context.lineTo(series[i + 1].radius, -eachSpacing);
  5238. context.lineTo(series[i].radius, 0);
  5239. context.lineTo(0, 0);
  5240. context.closePath();
  5241. context.fill();
  5242. if (funnelOption.border == true) {
  5243. context.stroke();
  5244. }
  5245. }
  5246. context.translate(0, -eachSpacing)
  5247. }
  5248. }else{
  5249. for (let i = 0; i < series.length; i++) {
  5250. if (i == 0) {
  5251. if (opts.tooltip) {
  5252. if (opts.tooltip.index == i) {
  5253. context.beginPath();
  5254. context.setFillStyle(hexToRgb(series[i].color, funnelOption.activeOpacity));
  5255. context.moveTo(-activeWidth, 0);
  5256. context.lineTo(-series[i].radius - activeWidth, -eachSpacing);
  5257. context.lineTo(series[i].radius + activeWidth, -eachSpacing);
  5258. context.lineTo(activeWidth, 0);
  5259. context.lineTo(-activeWidth, 0);
  5260. context.closePath();
  5261. context.fill();
  5262. }
  5263. }
  5264. series[i].funnelArea = [centerPosition.x - series[i].radius, centerPosition.y - eachSpacing, centerPosition.x + series[i].radius, centerPosition.y];
  5265. context.beginPath();
  5266. context.setLineWidth(funnelOption.borderWidth * opts.pix);
  5267. context.setStrokeStyle(funnelOption.borderColor);
  5268. var fillColor = hexToRgb(series[i].color, funnelOption.fillOpacity);
  5269. if (funnelOption.linearType == 'custom') {
  5270. var grd = context.createLinearGradient(series[i].radius, -eachSpacing, -series[i].radius, -eachSpacing);
  5271. grd.addColorStop(0, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5272. grd.addColorStop(0.5, hexToRgb(funnelOption.customColor[series[i].linearIndex], funnelOption.fillOpacity));
  5273. grd.addColorStop(1, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5274. fillColor = grd
  5275. }
  5276. context.setFillStyle(fillColor);
  5277. context.moveTo(0, 0);
  5278. context.lineTo(-series[i].radius, -eachSpacing);
  5279. context.lineTo(series[i].radius, -eachSpacing);
  5280. context.lineTo(0, 0);
  5281. context.closePath();
  5282. context.fill();
  5283. if (funnelOption.border == true) {
  5284. context.stroke();
  5285. }
  5286. } else {
  5287. if (opts.tooltip) {
  5288. if (opts.tooltip.index == i) {
  5289. context.beginPath();
  5290. context.setFillStyle(hexToRgb(series[i].color, funnelOption.activeOpacity));
  5291. context.moveTo(0, 0);
  5292. context.lineTo(-series[i - 1].radius - activeWidth, 0);
  5293. context.lineTo(-series[i].radius - activeWidth, -eachSpacing);
  5294. context.lineTo(series[i].radius + activeWidth, -eachSpacing);
  5295. context.lineTo(series[i - 1].radius + activeWidth, 0);
  5296. context.lineTo(0, 0);
  5297. context.closePath();
  5298. context.fill();
  5299. }
  5300. }
  5301. series[i].funnelArea = [centerPosition.x - series[i].radius, centerPosition.y - eachSpacing * (i + 1), centerPosition.x + series[i].radius, centerPosition.y - eachSpacing * i];
  5302. context.beginPath();
  5303. context.setLineWidth(funnelOption.borderWidth * opts.pix);
  5304. context.setStrokeStyle(funnelOption.borderColor);
  5305. var fillColor = hexToRgb(series[i].color, funnelOption.fillOpacity);
  5306. if (funnelOption.linearType == 'custom') {
  5307. var grd = context.createLinearGradient(series[i].radius, -eachSpacing, -series[i].radius, -eachSpacing);
  5308. grd.addColorStop(0, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5309. grd.addColorStop(0.5, hexToRgb(funnelOption.customColor[series[i].linearIndex], funnelOption.fillOpacity));
  5310. grd.addColorStop(1, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5311. fillColor = grd
  5312. }
  5313. context.setFillStyle(fillColor);
  5314. context.moveTo(0, 0);
  5315. context.lineTo(-series[i - 1].radius, 0);
  5316. context.lineTo(-series[i].radius, -eachSpacing);
  5317. context.lineTo(series[i].radius, -eachSpacing);
  5318. context.lineTo(series[i - 1].radius, 0);
  5319. context.lineTo(0, 0);
  5320. context.closePath();
  5321. context.fill();
  5322. if (funnelOption.border == true) {
  5323. context.stroke();
  5324. }
  5325. }
  5326. context.translate(0, -eachSpacing)
  5327. }
  5328. }
  5329. context.restore();
  5330. if (opts.dataLabel !== false && process === 1) {
  5331. drawFunnelText(series, opts, context, eachSpacing, funnelOption.labelAlign, activeWidth, centerPosition);
  5332. }
  5333. return {
  5334. center: centerPosition,
  5335. radius: radius,
  5336. series: series
  5337. };
  5338. }
  5339. function drawFunnelText(series, opts, context, eachSpacing, labelAlign, activeWidth, centerPosition) {
  5340. for (let i = 0; i < series.length; i++) {
  5341. let item = series[i];
  5342. let startX, endX, startY, fontSize;
  5343. let text = item.formatter ? item.formatter(item,i,series) : util.toFixed(item._proportion_ * 100) + '%';
  5344. if (labelAlign == 'right') {
  5345. if(opts.extra.funnel.type === 'pyramid'){
  5346. if (i == series.length -1) {
  5347. startX = (item.funnelArea[2] + centerPosition.x) / 2;
  5348. } else {
  5349. startX = (item.funnelArea[2] + series[i + 1].funnelArea[2]) / 2;
  5350. }
  5351. }else{
  5352. if (i == 0) {
  5353. startX = (item.funnelArea[2] + centerPosition.x) / 2;
  5354. } else {
  5355. startX = (item.funnelArea[2] + series[i - 1].funnelArea[2]) / 2;
  5356. }
  5357. }
  5358. endX = startX + activeWidth * 2;
  5359. startY = item.funnelArea[1] + eachSpacing / 2;
  5360. fontSize = item.textSize * opts.pix || opts.fontSize * opts.pix;
  5361. context.setLineWidth(1 * opts.pix);
  5362. context.setStrokeStyle(item.color);
  5363. context.setFillStyle(item.color);
  5364. context.beginPath();
  5365. context.moveTo(startX, startY);
  5366. context.lineTo(endX, startY);
  5367. context.stroke();
  5368. context.closePath();
  5369. context.beginPath();
  5370. context.moveTo(endX, startY);
  5371. context.arc(endX, startY, 2, 0, 2 * Math.PI);
  5372. context.closePath();
  5373. context.fill();
  5374. context.beginPath();
  5375. context.setFontSize(fontSize);
  5376. context.setFillStyle(item.textColor || opts.fontColor);
  5377. context.fillText(text, endX + 5, startY + fontSize / 2 - 2);
  5378. context.closePath();
  5379. context.stroke();
  5380. context.closePath();
  5381. } else {
  5382. if(opts.extra.funnel.type === 'pyramid'){
  5383. if (i == series.length -1) {
  5384. startX = (item.funnelArea[0] + centerPosition.x) / 2;
  5385. } else {
  5386. startX = (item.funnelArea[0] + series[i + 1].funnelArea[0]) / 2;
  5387. }
  5388. }else{
  5389. if (i == 0) {
  5390. startX = (item.funnelArea[0] + centerPosition.x) / 2;
  5391. } else {
  5392. startX = (item.funnelArea[0] + series[i - 1].funnelArea[0]) / 2;
  5393. }
  5394. }
  5395. endX = startX - activeWidth * 2;
  5396. startY = item.funnelArea[1] + eachSpacing / 2;
  5397. fontSize = item.textSize * opts.pix || opts.fontSize * opts.pix;
  5398. context.setLineWidth(1 * opts.pix);
  5399. context.setStrokeStyle(item.color);
  5400. context.setFillStyle(item.color);
  5401. context.beginPath();
  5402. context.moveTo(startX, startY);
  5403. context.lineTo(endX, startY);
  5404. context.stroke();
  5405. context.closePath();
  5406. context.beginPath();
  5407. context.moveTo(endX, startY);
  5408. context.arc(endX, startY, 2, 0, 2 * Math.PI);
  5409. context.closePath();
  5410. context.fill();
  5411. context.beginPath();
  5412. context.setFontSize(fontSize);
  5413. context.setFillStyle(item.textColor || opts.fontColor);
  5414. context.fillText(text, endX - 5 - measureText(text, fontSize, context), startY + fontSize / 2 - 2);
  5415. context.closePath();
  5416. context.stroke();
  5417. context.closePath();
  5418. }
  5419. }
  5420. }
  5421. function drawCanvas(opts, context) {
  5422. context.draw();
  5423. }
  5424. var Timing = {
  5425. easeIn: function easeIn(pos) {
  5426. return Math.pow(pos, 3);
  5427. },
  5428. easeOut: function easeOut(pos) {
  5429. return Math.pow(pos - 1, 3) + 1;
  5430. },
  5431. easeInOut: function easeInOut(pos) {
  5432. if ((pos /= 0.5) < 1) {
  5433. return 0.5 * Math.pow(pos, 3);
  5434. } else {
  5435. return 0.5 * (Math.pow(pos - 2, 3) + 2);
  5436. }
  5437. },
  5438. linear: function linear(pos) {
  5439. return pos;
  5440. }
  5441. };
  5442. function Animation(opts) {
  5443. this.isStop = false;
  5444. opts.duration = typeof opts.duration === 'undefined' ? 1000 : opts.duration;
  5445. opts.timing = opts.timing || 'easeInOut';
  5446. var delay = 17;
  5447. function createAnimationFrame() {
  5448. if (typeof setTimeout !== 'undefined') {
  5449. return function(step, delay) {
  5450. setTimeout(function() {
  5451. var timeStamp = +new Date();
  5452. step(timeStamp);
  5453. }, delay);
  5454. };
  5455. } else if (typeof requestAnimationFrame !== 'undefined') {
  5456. return requestAnimationFrame;
  5457. } else {
  5458. return function(step) {
  5459. step(null);
  5460. };
  5461. }
  5462. };
  5463. var animationFrame = createAnimationFrame();
  5464. var startTimeStamp = null;
  5465. var _step = function step(timestamp) {
  5466. if (timestamp === null || this.isStop === true) {
  5467. opts.onProcess && opts.onProcess(1);
  5468. opts.onAnimationFinish && opts.onAnimationFinish();
  5469. return;
  5470. }
  5471. if (startTimeStamp === null) {
  5472. startTimeStamp = timestamp;
  5473. }
  5474. if (timestamp - startTimeStamp < opts.duration) {
  5475. var process = (timestamp - startTimeStamp) / opts.duration;
  5476. var timingFunction = Timing[opts.timing];
  5477. process = timingFunction(process);
  5478. opts.onProcess && opts.onProcess(process);
  5479. animationFrame(_step, delay);
  5480. } else {
  5481. opts.onProcess && opts.onProcess(1);
  5482. opts.onAnimationFinish && opts.onAnimationFinish();
  5483. }
  5484. };
  5485. _step = _step.bind(this);
  5486. animationFrame(_step, delay);
  5487. }
  5488. Animation.prototype.stop = function() {
  5489. this.isStop = true;
  5490. };
  5491. function drawCharts(type, opts, config, context) {
  5492. var _this = this;
  5493. var series = opts.series;
  5494. //兼容ECharts饼图类数据格式
  5495. if (type === 'pie' || type === 'ring' || type === 'rose' || type === 'funnel') {
  5496. series = fixPieSeries(series, opts, config);
  5497. }
  5498. var categories = opts.categories;
  5499. series = fillSeries(series, opts, config);
  5500. var duration = opts.animation ? opts.duration : 0;
  5501. _this.animationInstance && _this.animationInstance.stop();
  5502. var seriesMA = null;
  5503. if (type == 'candle') {
  5504. let average = assign({}, opts.extra.candle.average);
  5505. if (average.show) {
  5506. seriesMA = calCandleMA(average.day, average.name, average.color, series[0].data);
  5507. seriesMA = fillSeries(seriesMA, opts, config);
  5508. opts.seriesMA = seriesMA;
  5509. } else if (opts.seriesMA) {
  5510. seriesMA = opts.seriesMA = fillSeries(opts.seriesMA, opts, config);
  5511. } else {
  5512. seriesMA = series;
  5513. }
  5514. } else {
  5515. seriesMA = series;
  5516. }
  5517. /* 过滤掉show=false的series */
  5518. opts._series_ = series = filterSeries(series);
  5519. //重新计算图表区域
  5520. opts.area = new Array(4);
  5521. //复位绘图区域
  5522. for (let j = 0; j < 4; j++) {
  5523. opts.area[j] = opts.padding[j] * opts.pix;
  5524. }
  5525. //通过计算三大区域:图例、X轴、Y轴的大小,确定绘图区域
  5526. var _calLegendData = calLegendData(seriesMA, opts, config, opts.chartData, context),
  5527. legendHeight = _calLegendData.area.wholeHeight,
  5528. legendWidth = _calLegendData.area.wholeWidth;
  5529. switch (opts.legend.position) {
  5530. case 'top':
  5531. opts.area[0] += legendHeight;
  5532. break;
  5533. case 'bottom':
  5534. opts.area[2] += legendHeight;
  5535. break;
  5536. case 'left':
  5537. opts.area[3] += legendWidth;
  5538. break;
  5539. case 'right':
  5540. opts.area[1] += legendWidth;
  5541. break;
  5542. }
  5543. let _calYAxisData = {},
  5544. yAxisWidth = 0;
  5545. if (opts.type === 'line' || opts.type === 'column' || opts.type === 'area' || opts.type === 'mix' || opts.type === 'candle' || opts.type === 'scatter' || opts.type === 'bubble' || opts.type === 'bar') {
  5546. _calYAxisData = calYAxisData(series, opts, config, context);
  5547. yAxisWidth = _calYAxisData.yAxisWidth;
  5548. //如果显示Y轴标题
  5549. if (opts.yAxis.showTitle) {
  5550. let maxTitleHeight = 0;
  5551. for (let i = 0; i < opts.yAxis.data.length; i++) {
  5552. maxTitleHeight = Math.max(maxTitleHeight, opts.yAxis.data[i].titleFontSize ? opts.yAxis.data[i].titleFontSize * opts.pix : config.fontSize)
  5553. }
  5554. opts.area[0] += maxTitleHeight;
  5555. }
  5556. let rightIndex = 0,
  5557. leftIndex = 0;
  5558. //计算主绘图区域左右位置
  5559. for (let i = 0; i < yAxisWidth.length; i++) {
  5560. if (yAxisWidth[i].position == 'left') {
  5561. if (leftIndex > 0) {
  5562. opts.area[3] += yAxisWidth[i].width + opts.yAxis.padding * opts.pix;
  5563. } else {
  5564. opts.area[3] += yAxisWidth[i].width;
  5565. }
  5566. leftIndex += 1;
  5567. } else if (yAxisWidth[i].position == 'right') {
  5568. if (rightIndex > 0) {
  5569. opts.area[1] += yAxisWidth[i].width + opts.yAxis.padding * opts.pix;
  5570. } else {
  5571. opts.area[1] += yAxisWidth[i].width;
  5572. }
  5573. rightIndex += 1;
  5574. }
  5575. }
  5576. } else {
  5577. config.yAxisWidth = yAxisWidth;
  5578. }
  5579. opts.chartData.yAxisData = _calYAxisData;
  5580. if (opts.categories && opts.categories.length && opts.type !== 'radar' && opts.type !== 'gauge' && opts.type !== 'bar') {
  5581. opts.chartData.xAxisData = getXAxisPoints(opts.categories, opts, config);
  5582. let _calCategoriesData = calCategoriesData(opts.categories, opts, config, opts.chartData.xAxisData.eachSpacing, context),
  5583. xAxisHeight = _calCategoriesData.xAxisHeight,
  5584. angle = _calCategoriesData.angle;
  5585. config.xAxisHeight = xAxisHeight;
  5586. config._xAxisTextAngle_ = angle;
  5587. opts.area[2] += xAxisHeight;
  5588. opts.chartData.categoriesData = _calCategoriesData;
  5589. } else {
  5590. if (opts.type === 'line' || opts.type === 'area' || opts.type === 'scatter' || opts.type === 'bubble' || opts.type === 'bar') {
  5591. opts.chartData.xAxisData = calXAxisData(series, opts, config, context);
  5592. categories = opts.chartData.xAxisData.rangesFormat;
  5593. let _calCategoriesData = calCategoriesData(categories, opts, config, opts.chartData.xAxisData.eachSpacing, context),
  5594. xAxisHeight = _calCategoriesData.xAxisHeight,
  5595. angle = _calCategoriesData.angle;
  5596. config.xAxisHeight = xAxisHeight;
  5597. config._xAxisTextAngle_ = angle;
  5598. opts.area[2] += xAxisHeight;
  5599. opts.chartData.categoriesData = _calCategoriesData;
  5600. } else {
  5601. opts.chartData.xAxisData = {
  5602. xAxisPoints: []
  5603. };
  5604. }
  5605. }
  5606. //计算右对齐偏移距离
  5607. if (opts.enableScroll && opts.xAxis.scrollAlign == 'right' && opts._scrollDistance_ === undefined) {
  5608. let offsetLeft = 0,
  5609. xAxisPoints = opts.chartData.xAxisData.xAxisPoints,
  5610. startX = opts.chartData.xAxisData.startX,
  5611. endX = opts.chartData.xAxisData.endX,
  5612. eachSpacing = opts.chartData.xAxisData.eachSpacing;
  5613. let totalWidth = eachSpacing * (xAxisPoints.length - 1);
  5614. let screenWidth = endX - startX;
  5615. offsetLeft = screenWidth - totalWidth;
  5616. _this.scrollOption = {
  5617. currentOffset: offsetLeft,
  5618. startTouchX: offsetLeft,
  5619. distance: 0,
  5620. lastMoveTime: 0
  5621. };
  5622. opts._scrollDistance_ = offsetLeft;
  5623. }
  5624. if (type === 'pie' || type === 'ring' || type === 'rose') {
  5625. config._pieTextMaxLength_ = opts.dataLabel === false ? 0 : getPieTextMaxLength(seriesMA, config, context, opts);
  5626. }
  5627. switch (type) {
  5628. case 'word':
  5629. this.animationInstance = new Animation({
  5630. timing: opts.timing,
  5631. duration: duration,
  5632. onProcess: function(process) {
  5633. context.clearRect(0, 0, opts.width, opts.height);
  5634. if (opts.rotate) {
  5635. contextRotate(context, opts);
  5636. }
  5637. drawWordCloudDataPoints(series, opts, config, context, process);
  5638. drawCanvas(opts, context);
  5639. },
  5640. onAnimationFinish: function onAnimationFinish() {
  5641. _this.uevent.trigger('renderComplete');
  5642. }
  5643. });
  5644. break;
  5645. case 'map':
  5646. context.clearRect(0, 0, opts.width, opts.height);
  5647. drawMapDataPoints(series, opts, config, context);
  5648. break;
  5649. case 'funnel':
  5650. this.animationInstance = new Animation({
  5651. timing: opts.timing,
  5652. duration: duration,
  5653. onProcess: function(process) {
  5654. context.clearRect(0, 0, opts.width, opts.height);
  5655. if (opts.rotate) {
  5656. contextRotate(context, opts);
  5657. }
  5658. opts.chartData.funnelData = drawFunnelDataPoints(series, opts, config, context, process);
  5659. drawLegend(opts.series, opts, config, context, opts.chartData);
  5660. drawToolTipBridge(opts, config, context, process);
  5661. drawCanvas(opts, context);
  5662. },
  5663. onAnimationFinish: function onAnimationFinish() {
  5664. _this.uevent.trigger('renderComplete');
  5665. }
  5666. });
  5667. break;
  5668. case 'line':
  5669. this.animationInstance = new Animation({
  5670. timing: opts.timing,
  5671. duration: duration,
  5672. onProcess: function onProcess(process) {
  5673. context.clearRect(0, 0, opts.width, opts.height);
  5674. if (opts.rotate) {
  5675. contextRotate(context, opts);
  5676. }
  5677. drawYAxisGrid(categories, opts, config, context);
  5678. drawXAxis(categories, opts, config, context);
  5679. var _drawLineDataPoints = drawLineDataPoints(series, opts, config, context, process),
  5680. xAxisPoints = _drawLineDataPoints.xAxisPoints,
  5681. calPoints = _drawLineDataPoints.calPoints,
  5682. eachSpacing = _drawLineDataPoints.eachSpacing;
  5683. opts.chartData.xAxisPoints = xAxisPoints;
  5684. opts.chartData.calPoints = calPoints;
  5685. opts.chartData.eachSpacing = eachSpacing;
  5686. drawYAxis(series, opts, config, context);
  5687. if (opts.enableMarkLine !== false && process === 1) {
  5688. drawMarkLine(opts, config, context);
  5689. }
  5690. drawLegend(opts.series, opts, config, context, opts.chartData);
  5691. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5692. drawCanvas(opts, context);
  5693. },
  5694. onAnimationFinish: function onAnimationFinish() {
  5695. _this.uevent.trigger('renderComplete');
  5696. }
  5697. });
  5698. break;
  5699. case 'scatter':
  5700. this.animationInstance = new Animation({
  5701. timing: opts.timing,
  5702. duration: duration,
  5703. onProcess: function onProcess(process) {
  5704. context.clearRect(0, 0, opts.width, opts.height);
  5705. if (opts.rotate) {
  5706. contextRotate(context, opts);
  5707. }
  5708. drawYAxisGrid(categories, opts, config, context);
  5709. drawXAxis(categories, opts, config, context);
  5710. var _drawScatterDataPoints = drawScatterDataPoints(series, opts, config, context, process),
  5711. xAxisPoints = _drawScatterDataPoints.xAxisPoints,
  5712. calPoints = _drawScatterDataPoints.calPoints,
  5713. eachSpacing = _drawScatterDataPoints.eachSpacing;
  5714. opts.chartData.xAxisPoints = xAxisPoints;
  5715. opts.chartData.calPoints = calPoints;
  5716. opts.chartData.eachSpacing = eachSpacing;
  5717. drawYAxis(series, opts, config, context);
  5718. if (opts.enableMarkLine !== false && process === 1) {
  5719. drawMarkLine(opts, config, context);
  5720. }
  5721. drawLegend(opts.series, opts, config, context, opts.chartData);
  5722. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5723. drawCanvas(opts, context);
  5724. },
  5725. onAnimationFinish: function onAnimationFinish() {
  5726. _this.uevent.trigger('renderComplete');
  5727. }
  5728. });
  5729. break;
  5730. case 'bubble':
  5731. this.animationInstance = new Animation({
  5732. timing: opts.timing,
  5733. duration: duration,
  5734. onProcess: function onProcess(process) {
  5735. context.clearRect(0, 0, opts.width, opts.height);
  5736. if (opts.rotate) {
  5737. contextRotate(context, opts);
  5738. }
  5739. drawYAxisGrid(categories, opts, config, context);
  5740. drawXAxis(categories, opts, config, context);
  5741. var _drawBubbleDataPoints = drawBubbleDataPoints(series, opts, config, context, process),
  5742. xAxisPoints = _drawBubbleDataPoints.xAxisPoints,
  5743. calPoints = _drawBubbleDataPoints.calPoints,
  5744. eachSpacing = _drawBubbleDataPoints.eachSpacing;
  5745. opts.chartData.xAxisPoints = xAxisPoints;
  5746. opts.chartData.calPoints = calPoints;
  5747. opts.chartData.eachSpacing = eachSpacing;
  5748. drawYAxis(series, opts, config, context);
  5749. if (opts.enableMarkLine !== false && process === 1) {
  5750. drawMarkLine(opts, config, context);
  5751. }
  5752. drawLegend(opts.series, opts, config, context, opts.chartData);
  5753. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5754. drawCanvas(opts, context);
  5755. },
  5756. onAnimationFinish: function onAnimationFinish() {
  5757. _this.uevent.trigger('renderComplete');
  5758. }
  5759. });
  5760. break;
  5761. case 'mix':
  5762. this.animationInstance = new Animation({
  5763. timing: opts.timing,
  5764. duration: duration,
  5765. onProcess: function onProcess(process) {
  5766. context.clearRect(0, 0, opts.width, opts.height);
  5767. if (opts.rotate) {
  5768. contextRotate(context, opts);
  5769. }
  5770. drawYAxisGrid(categories, opts, config, context);
  5771. drawXAxis(categories, opts, config, context);
  5772. var _drawMixDataPoints = drawMixDataPoints(series, opts, config, context, process),
  5773. xAxisPoints = _drawMixDataPoints.xAxisPoints,
  5774. calPoints = _drawMixDataPoints.calPoints,
  5775. eachSpacing = _drawMixDataPoints.eachSpacing;
  5776. opts.chartData.xAxisPoints = xAxisPoints;
  5777. opts.chartData.calPoints = calPoints;
  5778. opts.chartData.eachSpacing = eachSpacing;
  5779. drawYAxis(series, opts, config, context);
  5780. if (opts.enableMarkLine !== false && process === 1) {
  5781. drawMarkLine(opts, config, context);
  5782. }
  5783. drawLegend(opts.series, opts, config, context, opts.chartData);
  5784. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5785. drawCanvas(opts, context);
  5786. },
  5787. onAnimationFinish: function onAnimationFinish() {
  5788. _this.uevent.trigger('renderComplete');
  5789. }
  5790. });
  5791. break;
  5792. case 'column':
  5793. this.animationInstance = new Animation({
  5794. timing: opts.timing,
  5795. duration: duration,
  5796. onProcess: function onProcess(process) {
  5797. context.clearRect(0, 0, opts.width, opts.height);
  5798. if (opts.rotate) {
  5799. contextRotate(context, opts);
  5800. }
  5801. drawYAxisGrid(categories, opts, config, context);
  5802. drawXAxis(categories, opts, config, context);
  5803. var _drawColumnDataPoints = drawColumnDataPoints(series, opts, config, context, process),
  5804. xAxisPoints = _drawColumnDataPoints.xAxisPoints,
  5805. calPoints = _drawColumnDataPoints.calPoints,
  5806. eachSpacing = _drawColumnDataPoints.eachSpacing;
  5807. opts.chartData.xAxisPoints = xAxisPoints;
  5808. opts.chartData.calPoints = calPoints;
  5809. opts.chartData.eachSpacing = eachSpacing;
  5810. drawYAxis(series, opts, config, context);
  5811. if (opts.enableMarkLine !== false && process === 1) {
  5812. drawMarkLine(opts, config, context);
  5813. }
  5814. drawLegend(opts.series, opts, config, context, opts.chartData);
  5815. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5816. drawCanvas(opts, context);
  5817. },
  5818. onAnimationFinish: function onAnimationFinish() {
  5819. _this.uevent.trigger('renderComplete');
  5820. }
  5821. });
  5822. break;
  5823. case 'bar':
  5824. this.animationInstance = new Animation({
  5825. timing: opts.timing,
  5826. duration: duration,
  5827. onProcess: function onProcess(process) {
  5828. context.clearRect(0, 0, opts.width, opts.height);
  5829. if (opts.rotate) {
  5830. contextRotate(context, opts);
  5831. }
  5832. drawXAxis(categories, opts, config, context);
  5833. var _drawBarDataPoints = drawBarDataPoints(series, opts, config, context, process),
  5834. yAxisPoints = _drawBarDataPoints.yAxisPoints,
  5835. calPoints = _drawBarDataPoints.calPoints,
  5836. eachSpacing = _drawBarDataPoints.eachSpacing;
  5837. opts.chartData.yAxisPoints = yAxisPoints;
  5838. opts.chartData.xAxisPoints = opts.chartData.xAxisData.xAxisPoints;
  5839. opts.chartData.calPoints = calPoints;
  5840. opts.chartData.eachSpacing = eachSpacing;
  5841. drawYAxis(series, opts, config, context);
  5842. if (opts.enableMarkLine !== false && process === 1) {
  5843. drawMarkLine(opts, config, context);
  5844. }
  5845. drawLegend(opts.series, opts, config, context, opts.chartData);
  5846. drawToolTipBridge(opts, config, context, process, eachSpacing, yAxisPoints);
  5847. drawCanvas(opts, context);
  5848. },
  5849. onAnimationFinish: function onAnimationFinish() {
  5850. _this.uevent.trigger('renderComplete');
  5851. }
  5852. });
  5853. break;
  5854. case 'area':
  5855. this.animationInstance = new Animation({
  5856. timing: opts.timing,
  5857. duration: duration,
  5858. onProcess: function onProcess(process) {
  5859. context.clearRect(0, 0, opts.width, opts.height);
  5860. if (opts.rotate) {
  5861. contextRotate(context, opts);
  5862. }
  5863. drawYAxisGrid(categories, opts, config, context);
  5864. drawXAxis(categories, opts, config, context);
  5865. var _drawAreaDataPoints = drawAreaDataPoints(series, opts, config, context, process),
  5866. xAxisPoints = _drawAreaDataPoints.xAxisPoints,
  5867. calPoints = _drawAreaDataPoints.calPoints,
  5868. eachSpacing = _drawAreaDataPoints.eachSpacing;
  5869. opts.chartData.xAxisPoints = xAxisPoints;
  5870. opts.chartData.calPoints = calPoints;
  5871. opts.chartData.eachSpacing = eachSpacing;
  5872. drawYAxis(series, opts, config, context);
  5873. if (opts.enableMarkLine !== false && process === 1) {
  5874. drawMarkLine(opts, config, context);
  5875. }
  5876. drawLegend(opts.series, opts, config, context, opts.chartData);
  5877. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5878. drawCanvas(opts, context);
  5879. },
  5880. onAnimationFinish: function onAnimationFinish() {
  5881. _this.uevent.trigger('renderComplete');
  5882. }
  5883. });
  5884. break;
  5885. case 'ring':
  5886. case 'pie':
  5887. this.animationInstance = new Animation({
  5888. timing: opts.timing,
  5889. duration: duration,
  5890. onProcess: function onProcess(process) {
  5891. context.clearRect(0, 0, opts.width, opts.height);
  5892. if (opts.rotate) {
  5893. contextRotate(context, opts);
  5894. }
  5895. opts.chartData.pieData = drawPieDataPoints(series, opts, config, context, process);
  5896. drawLegend(opts.series, opts, config, context, opts.chartData);
  5897. drawToolTipBridge(opts, config, context, process);
  5898. drawCanvas(opts, context);
  5899. },
  5900. onAnimationFinish: function onAnimationFinish() {
  5901. _this.uevent.trigger('renderComplete');
  5902. }
  5903. });
  5904. break;
  5905. case 'rose':
  5906. this.animationInstance = new Animation({
  5907. timing: opts.timing,
  5908. duration: duration,
  5909. onProcess: function onProcess(process) {
  5910. context.clearRect(0, 0, opts.width, opts.height);
  5911. if (opts.rotate) {
  5912. contextRotate(context, opts);
  5913. }
  5914. opts.chartData.pieData = drawRoseDataPoints(series, opts, config, context, process);
  5915. drawLegend(opts.series, opts, config, context, opts.chartData);
  5916. drawToolTipBridge(opts, config, context, process);
  5917. drawCanvas(opts, context);
  5918. },
  5919. onAnimationFinish: function onAnimationFinish() {
  5920. _this.uevent.trigger('renderComplete');
  5921. }
  5922. });
  5923. break;
  5924. case 'radar':
  5925. this.animationInstance = new Animation({
  5926. timing: opts.timing,
  5927. duration: duration,
  5928. onProcess: function onProcess(process) {
  5929. context.clearRect(0, 0, opts.width, opts.height);
  5930. if (opts.rotate) {
  5931. contextRotate(context, opts);
  5932. }
  5933. opts.chartData.radarData = drawRadarDataPoints(series, opts, config, context, process);
  5934. drawLegend(opts.series, opts, config, context, opts.chartData);
  5935. drawToolTipBridge(opts, config, context, process);
  5936. drawCanvas(opts, context);
  5937. },
  5938. onAnimationFinish: function onAnimationFinish() {
  5939. _this.uevent.trigger('renderComplete');
  5940. }
  5941. });
  5942. break;
  5943. case 'arcbar':
  5944. this.animationInstance = new Animation({
  5945. timing: opts.timing,
  5946. duration: duration,
  5947. onProcess: function onProcess(process) {
  5948. context.clearRect(0, 0, opts.width, opts.height);
  5949. if (opts.rotate) {
  5950. contextRotate(context, opts);
  5951. }
  5952. opts.chartData.arcbarData = drawArcbarDataPoints(series, opts, config, context, process);
  5953. drawCanvas(opts, context);
  5954. },
  5955. onAnimationFinish: function onAnimationFinish() {
  5956. _this.uevent.trigger('renderComplete');
  5957. }
  5958. });
  5959. break;
  5960. case 'gauge':
  5961. this.animationInstance = new Animation({
  5962. timing: opts.timing,
  5963. duration: duration,
  5964. onProcess: function onProcess(process) {
  5965. context.clearRect(0, 0, opts.width, opts.height);
  5966. if (opts.rotate) {
  5967. contextRotate(context, opts);
  5968. }
  5969. opts.chartData.gaugeData = drawGaugeDataPoints(categories, series, opts, config, context, process);
  5970. drawCanvas(opts, context);
  5971. },
  5972. onAnimationFinish: function onAnimationFinish() {
  5973. _this.uevent.trigger('renderComplete');
  5974. }
  5975. });
  5976. break;
  5977. case 'candle':
  5978. this.animationInstance = new Animation({
  5979. timing: opts.timing,
  5980. duration: duration,
  5981. onProcess: function onProcess(process) {
  5982. context.clearRect(0, 0, opts.width, opts.height);
  5983. if (opts.rotate) {
  5984. contextRotate(context, opts);
  5985. }
  5986. drawYAxisGrid(categories, opts, config, context);
  5987. drawXAxis(categories, opts, config, context);
  5988. var _drawCandleDataPoints = drawCandleDataPoints(series, seriesMA, opts, config, context, process),
  5989. xAxisPoints = _drawCandleDataPoints.xAxisPoints,
  5990. calPoints = _drawCandleDataPoints.calPoints,
  5991. eachSpacing = _drawCandleDataPoints.eachSpacing;
  5992. opts.chartData.xAxisPoints = xAxisPoints;
  5993. opts.chartData.calPoints = calPoints;
  5994. opts.chartData.eachSpacing = eachSpacing;
  5995. drawYAxis(series, opts, config, context);
  5996. if (opts.enableMarkLine !== false && process === 1) {
  5997. drawMarkLine(opts, config, context);
  5998. }
  5999. if (seriesMA) {
  6000. drawLegend(seriesMA, opts, config, context, opts.chartData);
  6001. } else {
  6002. drawLegend(opts.series, opts, config, context, opts.chartData);
  6003. }
  6004. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  6005. drawCanvas(opts, context);
  6006. },
  6007. onAnimationFinish: function onAnimationFinish() {
  6008. _this.uevent.trigger('renderComplete');
  6009. }
  6010. });
  6011. break;
  6012. }
  6013. }
  6014. function uChartsEvent() {
  6015. this.events = {};
  6016. }
  6017. uChartsEvent.prototype.addEventListener = function(type, listener) {
  6018. this.events[type] = this.events[type] || [];
  6019. this.events[type].push(listener);
  6020. };
  6021. uChartsEvent.prototype.delEventListener = function(type) {
  6022. this.events[type] = [];
  6023. };
  6024. uChartsEvent.prototype.trigger = function() {
  6025. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  6026. args[_key] = arguments[_key];
  6027. }
  6028. var type = args[0];
  6029. var params = args.slice(1);
  6030. if (!!this.events[type]) {
  6031. this.events[type].forEach(function(listener) {
  6032. try {
  6033. listener.apply(null, params);
  6034. } catch (e) {
  6035. //console.log('[uCharts] '+e);
  6036. }
  6037. });
  6038. }
  6039. };
  6040. var uCharts = function uCharts(opts) {
  6041. opts.pix = opts.pixelRatio ? opts.pixelRatio : 1;
  6042. opts.fontSize = opts.fontSize ? opts.fontSize : 13;
  6043. opts.fontColor = opts.fontColor ? opts.fontColor : config.fontColor;
  6044. if (opts.background == "" || opts.background == "none") {
  6045. opts.background = "#FFFFFF"
  6046. }
  6047. opts.title = assign({}, opts.title);
  6048. opts.subtitle = assign({}, opts.subtitle);
  6049. opts.duration = opts.duration ? opts.duration : 1000;
  6050. opts.yAxis = assign({}, {
  6051. data: [],
  6052. showTitle: false,
  6053. disabled: false,
  6054. disableGrid: false,
  6055. splitNumber: 5,
  6056. gridType: 'solid',
  6057. dashLength: 4 * opts.pix,
  6058. gridColor: '#cccccc',
  6059. padding: 10,
  6060. fontColor: '#666666'
  6061. }, opts.yAxis);
  6062. opts.xAxis = assign({}, {
  6063. rotateLabel: false,
  6064. disabled: false,
  6065. disableGrid: false,
  6066. splitNumber: 5,
  6067. calibration:false,
  6068. gridType: 'solid',
  6069. dashLength: 4,
  6070. scrollAlign: 'left',
  6071. boundaryGap: 'center',
  6072. axisLine: true,
  6073. axisLineColor: '#cccccc'
  6074. }, opts.xAxis);
  6075. opts.xAxis.scrollPosition = opts.xAxis.scrollAlign;
  6076. opts.legend = assign({}, {
  6077. show: true,
  6078. position: 'bottom',
  6079. float: 'center',
  6080. backgroundColor: 'rgba(0,0,0,0)',
  6081. borderColor: 'rgba(0,0,0,0)',
  6082. borderWidth: 0,
  6083. padding: 5,
  6084. margin: 5,
  6085. itemGap: 10,
  6086. fontSize: opts.fontSize,
  6087. lineHeight: opts.fontSize,
  6088. fontColor: opts.fontColor,
  6089. formatter: {},
  6090. hiddenColor: '#CECECE'
  6091. }, opts.legend);
  6092. opts.extra = assign({}, opts.extra);
  6093. opts.rotate = opts.rotate ? true : false;
  6094. opts.animation = opts.animation ? true : false;
  6095. opts.rotate = opts.rotate ? true : false;
  6096. opts.canvas2d = opts.canvas2d ? true : false;
  6097. let config$$1 = JSON.parse(JSON.stringify(config));
  6098. config$$1.color = opts.color ? opts.color : config$$1.color;
  6099. config$$1.yAxisTitleWidth = opts.yAxis.disabled !== true && opts.yAxis.title ? config$$1.yAxisTitleWidth : 0;
  6100. if (opts.type == 'pie') {
  6101. config$$1.pieChartLinePadding = opts.dataLabel === false ? 0 : opts.extra.pie.labelWidth * opts.pix || config$$1.pieChartLinePadding * opts.pix;
  6102. }
  6103. if (opts.type == 'ring') {
  6104. config$$1.pieChartLinePadding = opts.dataLabel === false ? 0 : opts.extra.ring.labelWidth * opts.pix || config$$1.pieChartLinePadding * opts.pix;
  6105. }
  6106. if (opts.type == 'rose') {
  6107. config$$1.pieChartLinePadding = opts.dataLabel === false ? 0 : opts.extra.rose.labelWidth * opts.pix || config$$1.pieChartLinePadding * opts.pix;
  6108. }
  6109. config$$1.pieChartTextPadding = opts.dataLabel === false ? 0 : config$$1.pieChartTextPadding * opts.pix;
  6110. config$$1.yAxisSplit = opts.yAxis.splitNumber ? opts.yAxis.splitNumber : config.yAxisSplit;
  6111. //屏幕旋转
  6112. config$$1.rotate = opts.rotate;
  6113. if (opts.rotate) {
  6114. let tempWidth = opts.width;
  6115. let tempHeight = opts.height;
  6116. opts.width = tempHeight;
  6117. opts.height = tempWidth;
  6118. }
  6119. //适配高分屏
  6120. opts.padding = opts.padding ? opts.padding : config$$1.padding;
  6121. config$$1.yAxisWidth = config.yAxisWidth * opts.pix;
  6122. config$$1.xAxisHeight = config.xAxisHeight * opts.pix;
  6123. if (opts.enableScroll && opts.xAxis.scrollShow) {
  6124. config$$1.xAxisHeight += 6 * opts.pix;
  6125. }
  6126. config$$1.xAxisLineHeight = config.xAxisLineHeight * opts.pix;
  6127. config$$1.fontSize = opts.fontSize * opts.pix;
  6128. config$$1.titleFontSize = config.titleFontSize * opts.pix;
  6129. config$$1.subtitleFontSize = config.subtitleFontSize * opts.pix;
  6130. config$$1.toolTipPadding = config.toolTipPadding * opts.pix;
  6131. config$$1.toolTipLineHeight = config.toolTipLineHeight * opts.pix;
  6132. config$$1.columePadding = config.columePadding * opts.pix;
  6133. //this.context = opts.context ? opts.context : uni.createCanvasContext(opts.canvasId, opts.$this);
  6134. //v2.0版本后需要自行获取context并传入opts进行初始化,这么做是为了确保uCharts可以跨更多端使用,并保证了自定义组件this实例不被循环嵌套。如果您觉得不便请取消上面注释,采用v1.0版本的方式使用,对此给您带来的不便敬请谅解!
  6135. if(!opts.context){
  6136. throw new Error('[uCharts] 未获取到context!注意:v2.0版本后,需要自行获取canvas的绘图上下文并传入opts.context!');
  6137. }
  6138. this.context = opts.context;
  6139. if (!this.context.setTextAlign) {
  6140. this.context.setStrokeStyle = function(e) {
  6141. return this.strokeStyle = e;
  6142. }
  6143. this.context.setLineWidth = function(e) {
  6144. return this.lineWidth = e;
  6145. }
  6146. this.context.setLineCap = function(e) {
  6147. return this.lineCap = e;
  6148. }
  6149. this.context.setFontSize = function(e) {
  6150. return this.font = e + "px sans-serif";
  6151. }
  6152. this.context.setFillStyle = function(e) {
  6153. return this.fillStyle = e;
  6154. }
  6155. this.context.setTextAlign = function(e) {
  6156. return this.textAlign = e;
  6157. }
  6158. this.context.draw = function() {}
  6159. }
  6160. //兼容NVUEsetLineDash
  6161. if(!this.context.setLineDash){
  6162. this.context.setLineDash = function(e) {}
  6163. }
  6164. opts.chartData = {};
  6165. this.uevent = new uChartsEvent();
  6166. this.scrollOption = {
  6167. currentOffset: 0,
  6168. startTouchX: 0,
  6169. distance: 0,
  6170. lastMoveTime: 0
  6171. };
  6172. this.opts = opts;
  6173. this.config = config$$1;
  6174. drawCharts.call(this, opts.type, opts, config$$1, this.context);
  6175. };
  6176. uCharts.prototype.updateData = function() {
  6177. let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  6178. this.opts = assign({}, this.opts, data);
  6179. this.opts.updateData = true;
  6180. let scrollPosition = data.scrollPosition || 'current';
  6181. switch (scrollPosition) {
  6182. case 'current':
  6183. //this.opts._scrollDistance_ = this.scrollOption.currentOffset;
  6184. break;
  6185. case 'left':
  6186. this.opts._scrollDistance_ = 0;
  6187. this.scrollOption = {
  6188. currentOffset: 0,
  6189. startTouchX: 0,
  6190. distance: 0,
  6191. lastMoveTime: 0
  6192. };
  6193. break;
  6194. case 'right':
  6195. let _calYAxisData = calYAxisData(this.opts.series, this.opts, this.config, this.context), yAxisWidth = _calYAxisData.yAxisWidth;
  6196. this.config.yAxisWidth = yAxisWidth;
  6197. let offsetLeft = 0;
  6198. let _getXAxisPoints0 = getXAxisPoints(this.opts.categories, this.opts, this.config), xAxisPoints = _getXAxisPoints0.xAxisPoints,
  6199. startX = _getXAxisPoints0.startX,
  6200. endX = _getXAxisPoints0.endX,
  6201. eachSpacing = _getXAxisPoints0.eachSpacing;
  6202. let totalWidth = eachSpacing * (xAxisPoints.length - 1);
  6203. let screenWidth = endX - startX;
  6204. offsetLeft = screenWidth - totalWidth;
  6205. this.scrollOption = {
  6206. currentOffset: offsetLeft,
  6207. startTouchX: offsetLeft,
  6208. distance: 0,
  6209. lastMoveTime: 0
  6210. };
  6211. this.opts._scrollDistance_ = offsetLeft;
  6212. break;
  6213. }
  6214. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  6215. };
  6216. uCharts.prototype.zoom = function() {
  6217. var val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.opts.xAxis.itemCount;
  6218. if (this.opts.enableScroll !== true) {
  6219. console.log('[uCharts] 请启用滚动条后使用')
  6220. return;
  6221. }
  6222. //当前屏幕中间点
  6223. let centerPoint = Math.round(Math.abs(this.scrollOption.currentOffset) / this.opts.chartData.eachSpacing) + Math.round(this.opts.xAxis.itemCount / 2);
  6224. this.opts.animation = false;
  6225. this.opts.xAxis.itemCount = val.itemCount;
  6226. //重新计算x轴偏移距离
  6227. let _calYAxisData = calYAxisData(this.opts.series, this.opts, this.config, this.context),
  6228. yAxisWidth = _calYAxisData.yAxisWidth;
  6229. this.config.yAxisWidth = yAxisWidth;
  6230. let offsetLeft = 0;
  6231. let _getXAxisPoints0 = getXAxisPoints(this.opts.categories, this.opts, this.config),
  6232. xAxisPoints = _getXAxisPoints0.xAxisPoints,
  6233. startX = _getXAxisPoints0.startX,
  6234. endX = _getXAxisPoints0.endX,
  6235. eachSpacing = _getXAxisPoints0.eachSpacing;
  6236. let centerLeft = eachSpacing * centerPoint;
  6237. let screenWidth = endX - startX;
  6238. let MaxLeft = screenWidth - eachSpacing * (xAxisPoints.length - 1);
  6239. offsetLeft = screenWidth / 2 - centerLeft;
  6240. if (offsetLeft > 0) {
  6241. offsetLeft = 0;
  6242. }
  6243. if (offsetLeft < MaxLeft) {
  6244. offsetLeft = MaxLeft;
  6245. }
  6246. this.scrollOption = {
  6247. currentOffset: offsetLeft,
  6248. startTouchX: offsetLeft,
  6249. distance: 0,
  6250. lastMoveTime: 0
  6251. };
  6252. this.opts._scrollDistance_ = offsetLeft;
  6253. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  6254. };
  6255. uCharts.prototype.stopAnimation = function() {
  6256. this.animationInstance && this.animationInstance.stop();
  6257. };
  6258. uCharts.prototype.addEventListener = function(type, listener) {
  6259. this.uevent.addEventListener(type, listener);
  6260. };
  6261. uCharts.prototype.delEventListener = function(type) {
  6262. this.uevent.delEventListener(type);
  6263. };
  6264. uCharts.prototype.getCurrentDataIndex = function(e) {
  6265. var touches = null;
  6266. if (e.changedTouches) {
  6267. touches = e.changedTouches[0];
  6268. } else {
  6269. touches = e.mp.changedTouches[0];
  6270. }
  6271. if (touches) {
  6272. let _touches$ = getTouches(touches, this.opts, e);
  6273. if (this.opts.type === 'pie' || this.opts.type === 'ring') {
  6274. return findPieChartCurrentIndex({
  6275. x: _touches$.x,
  6276. y: _touches$.y
  6277. }, this.opts.chartData.pieData, this.opts);
  6278. } else if (this.opts.type === 'rose') {
  6279. return findRoseChartCurrentIndex({
  6280. x: _touches$.x,
  6281. y: _touches$.y
  6282. }, this.opts.chartData.pieData, this.opts);
  6283. } else if (this.opts.type === 'radar') {
  6284. return findRadarChartCurrentIndex({
  6285. x: _touches$.x,
  6286. y: _touches$.y
  6287. }, this.opts.chartData.radarData, this.opts.categories.length);
  6288. } else if (this.opts.type === 'funnel') {
  6289. return findFunnelChartCurrentIndex({
  6290. x: _touches$.x,
  6291. y: _touches$.y
  6292. }, this.opts.chartData.funnelData);
  6293. } else if (this.opts.type === 'map') {
  6294. return findMapChartCurrentIndex({
  6295. x: _touches$.x,
  6296. y: _touches$.y
  6297. }, this.opts);
  6298. } else if (this.opts.type === 'word') {
  6299. return findWordChartCurrentIndex({
  6300. x: _touches$.x,
  6301. y: _touches$.y
  6302. }, this.opts.chartData.wordCloudData);
  6303. } else if (this.opts.type === 'bar') {
  6304. return findBarChartCurrentIndex({
  6305. x: _touches$.x,
  6306. y: _touches$.y
  6307. }, this.opts.chartData.calPoints, this.opts, this.config, Math.abs(this.scrollOption.currentOffset));
  6308. } else {
  6309. return findCurrentIndex({
  6310. x: _touches$.x,
  6311. y: _touches$.y
  6312. }, this.opts.chartData.calPoints, this.opts, this.config, Math.abs(this.scrollOption.currentOffset));
  6313. }
  6314. }
  6315. return -1;
  6316. };
  6317. uCharts.prototype.getLegendDataIndex = function(e) {
  6318. var touches = null;
  6319. if (e.changedTouches) {
  6320. touches = e.changedTouches[0];
  6321. } else {
  6322. touches = e.mp.changedTouches[0];
  6323. }
  6324. if (touches) {
  6325. let _touches$ = getTouches(touches, this.opts, e);
  6326. return findLegendIndex({
  6327. x: _touches$.x,
  6328. y: _touches$.y
  6329. }, this.opts.chartData.legendData);
  6330. }
  6331. return -1;
  6332. };
  6333. uCharts.prototype.touchLegend = function(e) {
  6334. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  6335. var touches = null;
  6336. if (e.changedTouches) {
  6337. touches = e.changedTouches[0];
  6338. } else {
  6339. touches = e.mp.changedTouches[0];
  6340. }
  6341. if (touches) {
  6342. var _touches$ = getTouches(touches, this.opts, e);
  6343. var index = this.getLegendDataIndex(e);
  6344. if (index >= 0) {
  6345. if (this.opts.type == 'candle') {
  6346. this.opts.seriesMA[index].show = !this.opts.seriesMA[index].show;
  6347. } else {
  6348. this.opts.series[index].show = !this.opts.series[index].show;
  6349. }
  6350. this.opts.animation = option.animation ? true : false;
  6351. this.opts._scrollDistance_ = this.scrollOption.currentOffset;
  6352. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  6353. }
  6354. }
  6355. };
  6356. uCharts.prototype.showToolTip = function(e) {
  6357. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  6358. var touches = null;
  6359. if (e.changedTouches) {
  6360. touches = e.changedTouches[0];
  6361. } else {
  6362. touches = e.mp.changedTouches[0];
  6363. }
  6364. if (!touches) {
  6365. console.log("[uCharts] 未获取到event坐标信息");
  6366. }
  6367. var _touches$ = getTouches(touches, this.opts, e);
  6368. var currentOffset = this.scrollOption.currentOffset;
  6369. var opts = assign({}, this.opts, {
  6370. _scrollDistance_: currentOffset,
  6371. animation: false
  6372. });
  6373. if (this.opts.type === 'line' || this.opts.type === 'area' || this.opts.type === 'column' || this.opts.type === 'scatter' || this.opts.type === 'bubble') {
  6374. var current = this.getCurrentDataIndex(e);
  6375. var index = option.index == undefined ? current.index : option.index;
  6376. if (index > -1 || index.length>0) {
  6377. var seriesData = getSeriesDataItem(this.opts.series, index, current.group);
  6378. if (seriesData.length !== 0) {
  6379. var _getToolTipData = getToolTipData(seriesData, this.opts, index, current.group, this.opts.categories, option),
  6380. textList = _getToolTipData.textList,
  6381. offset = _getToolTipData.offset;
  6382. offset.y = _touches$.y;
  6383. opts.tooltip = {
  6384. textList: option.textList !== undefined ? option.textList : textList,
  6385. offset: option.offset !== undefined ? option.offset : offset,
  6386. option: option,
  6387. index: index
  6388. };
  6389. }
  6390. }
  6391. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6392. }
  6393. if (this.opts.type === 'bar') {
  6394. var current = this.getCurrentDataIndex(e);
  6395. var index = option.index == undefined ? current.index : option.index;
  6396. if (index > -1 || index.length>0) {
  6397. var seriesData = getSeriesDataItem(this.opts.series, index, current.group);
  6398. if (seriesData.length !== 0) {
  6399. var _getToolTipData = getToolTipData(seriesData, this.opts, index, current.group, this.opts.categories, option),
  6400. textList = _getToolTipData.textList,
  6401. offset = _getToolTipData.offset;
  6402. offset.x = _touches$.x;
  6403. opts.tooltip = {
  6404. textList: option.textList !== undefined ? option.textList : textList,
  6405. offset: option.offset !== undefined ? option.offset : offset,
  6406. option: option,
  6407. index: index
  6408. };
  6409. }
  6410. }
  6411. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6412. }
  6413. if (this.opts.type === 'mix') {
  6414. var current = this.getCurrentDataIndex(e);
  6415. var index = option.index == undefined ? current.index : option.index;
  6416. if (index > -1) {
  6417. var currentOffset = this.scrollOption.currentOffset;
  6418. var opts = assign({}, this.opts, {
  6419. _scrollDistance_: currentOffset,
  6420. animation: false
  6421. });
  6422. var seriesData = getSeriesDataItem(this.opts.series, index);
  6423. if (seriesData.length !== 0) {
  6424. var _getMixToolTipData = getMixToolTipData(seriesData, this.opts, index, this.opts.categories, option),
  6425. textList = _getMixToolTipData.textList,
  6426. offset = _getMixToolTipData.offset;
  6427. offset.y = _touches$.y;
  6428. opts.tooltip = {
  6429. textList: option.textList ? option.textList : textList,
  6430. offset: option.offset !== undefined ? option.offset : offset,
  6431. option: option,
  6432. index: index
  6433. };
  6434. }
  6435. }
  6436. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6437. }
  6438. if (this.opts.type === 'candle') {
  6439. var current = this.getCurrentDataIndex(e);
  6440. var index = option.index == undefined ? current.index : option.index;
  6441. if (index > -1) {
  6442. var currentOffset = this.scrollOption.currentOffset;
  6443. var opts = assign({}, this.opts, {
  6444. _scrollDistance_: currentOffset,
  6445. animation: false
  6446. });
  6447. var seriesData = getSeriesDataItem(this.opts.series, index);
  6448. if (seriesData.length !== 0) {
  6449. var _getToolTipData = getCandleToolTipData(this.opts.series[0].data, seriesData, this.opts, index, this.opts.categories, this.opts.extra.candle, option),
  6450. textList = _getToolTipData.textList,
  6451. offset = _getToolTipData.offset;
  6452. offset.y = _touches$.y;
  6453. opts.tooltip = {
  6454. textList: option.textList ? option.textList : textList,
  6455. offset: option.offset !== undefined ? option.offset : offset,
  6456. option: option,
  6457. index: index
  6458. };
  6459. }
  6460. }
  6461. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6462. }
  6463. if (this.opts.type === 'pie' || this.opts.type === 'ring' || this.opts.type === 'rose' || this.opts.type === 'funnel') {
  6464. var index = option.index == undefined ? this.getCurrentDataIndex(e) : option.index;
  6465. if (index > -1) {
  6466. var opts = assign({}, this.opts, {animation: false});
  6467. var seriesData = assign({}, opts._series_[index]);
  6468. var textList = [{
  6469. text: option.formatter ? option.formatter(seriesData, undefined, index, opts) : seriesData.name + ': ' + seriesData.data,
  6470. color: seriesData.color
  6471. }];
  6472. var offset = {
  6473. x: _touches$.x,
  6474. y: _touches$.y
  6475. };
  6476. opts.tooltip = {
  6477. textList: option.textList ? option.textList : textList,
  6478. offset: option.offset !== undefined ? option.offset : offset,
  6479. option: option,
  6480. index: index
  6481. };
  6482. }
  6483. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6484. }
  6485. if (this.opts.type === 'map') {
  6486. var index = option.index == undefined ? this.getCurrentDataIndex(e) : option.index;
  6487. if (index > -1) {
  6488. var opts = assign({}, this.opts, {animation: false});
  6489. var seriesData = assign({}, this.opts.series[index]);
  6490. seriesData.name = seriesData.properties.name
  6491. var textList = [{
  6492. text: option.formatter ? option.formatter(seriesData, undefined, index, this.opts) : seriesData.name,
  6493. color: seriesData.color
  6494. }];
  6495. var offset = {
  6496. x: _touches$.x,
  6497. y: _touches$.y
  6498. };
  6499. opts.tooltip = {
  6500. textList: option.textList ? option.textList : textList,
  6501. offset: option.offset !== undefined ? option.offset : offset,
  6502. option: option,
  6503. index: index
  6504. };
  6505. }
  6506. opts.updateData = false;
  6507. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6508. }
  6509. if (this.opts.type === 'word') {
  6510. var index = option.index == undefined ? this.getCurrentDataIndex(e) : option.index;
  6511. if (index > -1) {
  6512. var opts = assign({}, this.opts, {animation: false});
  6513. var seriesData = assign({}, this.opts.series[index]);
  6514. var textList = [{
  6515. text: option.formatter ? option.formatter(seriesData, undefined, index, this.opts) : seriesData.name,
  6516. color: seriesData.color
  6517. }];
  6518. var offset = {
  6519. x: _touches$.x,
  6520. y: _touches$.y
  6521. };
  6522. opts.tooltip = {
  6523. textList: option.textList ? option.textList : textList,
  6524. offset: option.offset !== undefined ? option.offset : offset,
  6525. option: option,
  6526. index: index
  6527. };
  6528. }
  6529. opts.updateData = false;
  6530. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6531. }
  6532. if (this.opts.type === 'radar') {
  6533. var index = option.index == undefined ? this.getCurrentDataIndex(e) : option.index;
  6534. if (index > -1) {
  6535. var opts = assign({}, this.opts, {animation: false});
  6536. var seriesData = getSeriesDataItem(this.opts.series, index);
  6537. if (seriesData.length !== 0) {
  6538. var textList = seriesData.map((item) => {
  6539. return {
  6540. text: option.formatter ? option.formatter(item, this.opts.categories[index], index, this.opts) : item.name + ': ' + item.data,
  6541. color: item.color
  6542. };
  6543. });
  6544. var offset = {
  6545. x: _touches$.x,
  6546. y: _touches$.y
  6547. };
  6548. opts.tooltip = {
  6549. textList: option.textList ? option.textList : textList,
  6550. offset: option.offset !== undefined ? option.offset : offset,
  6551. option: option,
  6552. index: index
  6553. };
  6554. }
  6555. }
  6556. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6557. }
  6558. };
  6559. uCharts.prototype.translate = function(distance) {
  6560. this.scrollOption = {
  6561. currentOffset: distance,
  6562. startTouchX: distance,
  6563. distance: 0,
  6564. lastMoveTime: 0
  6565. };
  6566. let opts = assign({}, this.opts, {
  6567. _scrollDistance_: distance,
  6568. animation: false
  6569. });
  6570. drawCharts.call(this, this.opts.type, opts, this.config, this.context);
  6571. };
  6572. uCharts.prototype.scrollStart = function(e) {
  6573. var touches = null;
  6574. if (e.changedTouches) {
  6575. touches = e.changedTouches[0];
  6576. } else {
  6577. touches = e.mp.changedTouches[0];
  6578. }
  6579. var _touches$ = getTouches(touches, this.opts, e);
  6580. if (touches && this.opts.enableScroll === true) {
  6581. this.scrollOption.startTouchX = _touches$.x;
  6582. }
  6583. };
  6584. uCharts.prototype.scroll = function(e) {
  6585. if (this.scrollOption.lastMoveTime === 0) {
  6586. this.scrollOption.lastMoveTime = Date.now();
  6587. }
  6588. let Limit = this.opts.touchMoveLimit || 60;
  6589. let currMoveTime = Date.now();
  6590. let duration = currMoveTime - this.scrollOption.lastMoveTime;
  6591. if (duration < Math.floor(1000 / Limit)) return;
  6592. this.scrollOption.lastMoveTime = currMoveTime;
  6593. var touches = null;
  6594. if (e.changedTouches) {
  6595. touches = e.changedTouches[0];
  6596. } else {
  6597. touches = e.mp.changedTouches[0];
  6598. }
  6599. if (touches && this.opts.enableScroll === true) {
  6600. var _touches$ = getTouches(touches, this.opts, e);
  6601. var _distance;
  6602. _distance = _touches$.x - this.scrollOption.startTouchX;
  6603. var currentOffset = this.scrollOption.currentOffset;
  6604. var validDistance = calValidDistance(this, currentOffset + _distance, this.opts.chartData, this.config, this.opts);
  6605. this.scrollOption.distance = _distance = validDistance - currentOffset;
  6606. var opts = assign({}, this.opts, {
  6607. _scrollDistance_: currentOffset + _distance,
  6608. animation: false
  6609. });
  6610. this.opts = opts;
  6611. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6612. return currentOffset + _distance;
  6613. }
  6614. };
  6615. uCharts.prototype.scrollEnd = function(e) {
  6616. if (this.opts.enableScroll === true) {
  6617. var _scrollOption = this.scrollOption,
  6618. currentOffset = _scrollOption.currentOffset,
  6619. distance = _scrollOption.distance;
  6620. this.scrollOption.currentOffset = currentOffset + distance;
  6621. this.scrollOption.distance = 0;
  6622. }
  6623. };
  6624. export default uCharts;