u-charts.js 250 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815
  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.5-20211014',
  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. if (r0 + r2 > height) {
  2823. r0 = height;
  2824. r2 = 0;
  2825. r1 = height;
  2826. r3 = 0;
  2827. }
  2828. if (r0 + r2 > width / 2) {
  2829. r0 = width / 2;
  2830. r1 = width / 2;
  2831. r2 = 0;
  2832. r3 = 0;
  2833. }
  2834. r0 = r0 < 0 ? 0 : r0;
  2835. r1 = r1 < 0 ? 0 : r1;
  2836. r2 = r2 < 0 ? 0 : r2;
  2837. r3 = r3 < 0 ? 0 : r3;
  2838. context.arc(left + r3, top + r3, r3, -Math.PI, -Math.PI / 2);
  2839. context.arc(item.x - r0, top + r0, r0, -Math.PI / 2, 0);
  2840. context.arc(item.x - r1, top + width - r1, r1, 0, Math.PI / 2);
  2841. context.arc(left + r2, top + width - r2, r2, Math.PI / 2, Math.PI);
  2842. } else {
  2843. context.moveTo(startX, startY);
  2844. context.lineTo(item.x, startY);
  2845. context.lineTo(item.x, startY + item.width - 2);
  2846. context.lineTo(startX, startY + item.width - 2);
  2847. context.lineTo(startX, startY);
  2848. context.setLineWidth(1)
  2849. context.setStrokeStyle(strokeColor);
  2850. }
  2851. context.setFillStyle(fillColor);
  2852. context.closePath();
  2853. //context.stroke();
  2854. context.fill();
  2855. }
  2856. };
  2857. break;
  2858. case 'stack':
  2859. // 绘制堆叠数据图
  2860. var points = getBarStackDataPoints(data, minRange, maxRange, yAxisPoints, eachSpacing, opts, config, seriesIndex, series, process);
  2861. calPoints.push(points);
  2862. points = fixBarStackData(points, eachSpacing, series.length, seriesIndex, config, opts, series);
  2863. for (let i = 0; i < points.length; i++) {
  2864. let item = points[i];
  2865. if (item !== null && i > leftNum && i < rightNum) {
  2866. context.beginPath();
  2867. var fillColor = item.color || eachSeries.color;
  2868. var startX = item.x0;
  2869. context.setFillStyle(fillColor);
  2870. context.moveTo(startX, item.y - item.width/2);
  2871. context.fillRect(startX, item.y - item.width/2, item.height , item.width - 2);
  2872. context.closePath();
  2873. context.fill();
  2874. }
  2875. };
  2876. break;
  2877. }
  2878. });
  2879. if (opts.dataLabel !== false && process === 1) {
  2880. series.forEach(function(eachSeries, seriesIndex) {
  2881. let ranges, minRange, maxRange;
  2882. ranges = [].concat(opts.chartData.xAxisData.ranges);
  2883. maxRange = ranges.pop();
  2884. minRange = ranges.shift();
  2885. var data = eachSeries.data;
  2886. switch (columnOption.type) {
  2887. case 'group':
  2888. var points = getBarDataPoints(data, minRange, maxRange, yAxisPoints, eachSpacing, opts, config, process);
  2889. points = fixBarData(points, eachSpacing, series.length, seriesIndex, config, opts);
  2890. drawBarPointText(points, eachSeries, config, context, opts);
  2891. break;
  2892. case 'stack':
  2893. var points = getBarStackDataPoints(data, minRange, maxRange, yAxisPoints, eachSpacing, opts, config, seriesIndex, series, process);
  2894. drawBarPointText(points, eachSeries, config, context, opts);
  2895. break;
  2896. }
  2897. });
  2898. }
  2899. return {
  2900. yAxisPoints: yAxisPoints,
  2901. calPoints: calPoints,
  2902. eachSpacing: eachSpacing
  2903. };
  2904. }
  2905. function drawCandleDataPoints(series, seriesMA, opts, config, context) {
  2906. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  2907. var candleOption = assign({}, {
  2908. color: {},
  2909. average: {}
  2910. }, opts.extra.candle);
  2911. candleOption.color = assign({}, {
  2912. upLine: '#f04864',
  2913. upFill: '#f04864',
  2914. downLine: '#2fc25b',
  2915. downFill: '#2fc25b'
  2916. }, candleOption.color);
  2917. candleOption.average = assign({}, {
  2918. show: false,
  2919. name: [],
  2920. day: [],
  2921. color: config.color
  2922. }, candleOption.average);
  2923. opts.extra.candle = candleOption;
  2924. let xAxisData = opts.chartData.xAxisData,
  2925. xAxisPoints = xAxisData.xAxisPoints,
  2926. eachSpacing = xAxisData.eachSpacing;
  2927. let calPoints = [];
  2928. context.save();
  2929. let leftNum = -2;
  2930. let rightNum = xAxisPoints.length + 2;
  2931. let leftSpace = 0;
  2932. let rightSpace = opts.width + eachSpacing;
  2933. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2934. context.translate(opts._scrollDistance_, 0);
  2935. leftNum = Math.floor(-opts._scrollDistance_ / eachSpacing) - 2;
  2936. rightNum = leftNum + opts.xAxis.itemCount + 4;
  2937. leftSpace = -opts._scrollDistance_ - eachSpacing * 2 + opts.area[3];
  2938. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  2939. }
  2940. //画均线
  2941. if (candleOption.average.show || seriesMA) { //Merge pull request !12 from 邱贵翔
  2942. seriesMA.forEach(function(eachSeries, seriesIndex) {
  2943. let ranges, minRange, maxRange;
  2944. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2945. minRange = ranges.pop();
  2946. maxRange = ranges.shift();
  2947. var data = eachSeries.data;
  2948. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2949. var splitPointList = splitPoints(points,eachSeries);
  2950. for (let i = 0; i < splitPointList.length; i++) {
  2951. let points = splitPointList[i];
  2952. context.beginPath();
  2953. context.setStrokeStyle(eachSeries.color);
  2954. context.setLineWidth(1);
  2955. if (points.length === 1) {
  2956. context.moveTo(points[0].x, points[0].y);
  2957. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  2958. } else {
  2959. context.moveTo(points[0].x, points[0].y);
  2960. let startPoint = 0;
  2961. for (let j = 0; j < points.length; j++) {
  2962. let item = points[j];
  2963. if (startPoint == 0 && item.x > leftSpace) {
  2964. context.moveTo(item.x, item.y);
  2965. startPoint = 1;
  2966. }
  2967. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2968. var ctrlPoint = createCurveControlPoints(points, j - 1);
  2969. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x,
  2970. item.y);
  2971. }
  2972. }
  2973. context.moveTo(points[0].x, points[0].y);
  2974. }
  2975. context.closePath();
  2976. context.stroke();
  2977. }
  2978. });
  2979. }
  2980. //画K线
  2981. series.forEach(function(eachSeries, seriesIndex) {
  2982. let ranges, minRange, maxRange;
  2983. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2984. minRange = ranges.pop();
  2985. maxRange = ranges.shift();
  2986. var data = eachSeries.data;
  2987. var points = getCandleDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2988. calPoints.push(points);
  2989. var splitPointList = splitPoints(points,eachSeries);
  2990. for (let i = 0; i < splitPointList[0].length; i++) {
  2991. if (i > leftNum && i < rightNum) {
  2992. let item = splitPointList[0][i];
  2993. context.beginPath();
  2994. //如果上涨
  2995. if (data[i][1] - data[i][0] > 0) {
  2996. context.setStrokeStyle(candleOption.color.upLine);
  2997. context.setFillStyle(candleOption.color.upFill);
  2998. context.setLineWidth(1 * opts.pix);
  2999. context.moveTo(item[3].x, item[3].y); //顶点
  3000. context.lineTo(item[1].x, item[1].y); //收盘中间点
  3001. context.lineTo(item[1].x - eachSpacing / 4, item[1].y); //收盘左侧点
  3002. context.lineTo(item[0].x - eachSpacing / 4, item[0].y); //开盘左侧点
  3003. context.lineTo(item[0].x, item[0].y); //开盘中间点
  3004. context.lineTo(item[2].x, item[2].y); //底点
  3005. context.lineTo(item[0].x, item[0].y); //开盘中间点
  3006. context.lineTo(item[0].x + eachSpacing / 4, item[0].y); //开盘右侧点
  3007. context.lineTo(item[1].x + eachSpacing / 4, item[1].y); //收盘右侧点
  3008. context.lineTo(item[1].x, item[1].y); //收盘中间点
  3009. context.moveTo(item[3].x, item[3].y); //顶点
  3010. } else {
  3011. context.setStrokeStyle(candleOption.color.downLine);
  3012. context.setFillStyle(candleOption.color.downFill);
  3013. context.setLineWidth(1 * opts.pix);
  3014. context.moveTo(item[3].x, item[3].y); //顶点
  3015. context.lineTo(item[0].x, item[0].y); //开盘中间点
  3016. context.lineTo(item[0].x - eachSpacing / 4, item[0].y); //开盘左侧点
  3017. context.lineTo(item[1].x - eachSpacing / 4, item[1].y); //收盘左侧点
  3018. context.lineTo(item[1].x, item[1].y); //收盘中间点
  3019. context.lineTo(item[2].x, item[2].y); //底点
  3020. context.lineTo(item[1].x, item[1].y); //收盘中间点
  3021. context.lineTo(item[1].x + eachSpacing / 4, item[1].y); //收盘右侧点
  3022. context.lineTo(item[0].x + eachSpacing / 4, item[0].y); //开盘右侧点
  3023. context.lineTo(item[0].x, item[0].y); //开盘中间点
  3024. context.moveTo(item[3].x, item[3].y); //顶点
  3025. }
  3026. context.closePath();
  3027. context.fill();
  3028. context.stroke();
  3029. }
  3030. }
  3031. });
  3032. context.restore();
  3033. return {
  3034. xAxisPoints: xAxisPoints,
  3035. calPoints: calPoints,
  3036. eachSpacing: eachSpacing
  3037. };
  3038. }
  3039. function drawAreaDataPoints(series, opts, config, context) {
  3040. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3041. var areaOption = assign({}, {
  3042. type: 'straight',
  3043. opacity: 0.2,
  3044. addLine: false,
  3045. width: 2,
  3046. gradient: false
  3047. }, opts.extra.area);
  3048. let xAxisData = opts.chartData.xAxisData,
  3049. xAxisPoints = xAxisData.xAxisPoints,
  3050. eachSpacing = xAxisData.eachSpacing;
  3051. let endY = opts.height - opts.area[2];
  3052. let calPoints = [];
  3053. context.save();
  3054. let leftSpace = 0;
  3055. let rightSpace = opts.width + eachSpacing;
  3056. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3057. context.translate(opts._scrollDistance_, 0);
  3058. leftSpace = -opts._scrollDistance_ - eachSpacing * 2 + opts.area[3];
  3059. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  3060. }
  3061. series.forEach(function(eachSeries, seriesIndex) {
  3062. let ranges, minRange, maxRange;
  3063. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3064. minRange = ranges.pop();
  3065. maxRange = ranges.shift();
  3066. let data = eachSeries.data;
  3067. let points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3068. calPoints.push(points);
  3069. let splitPointList = splitPoints(points,eachSeries);
  3070. for (let i = 0; i < splitPointList.length; i++) {
  3071. let points = splitPointList[i];
  3072. // 绘制区域数
  3073. context.beginPath();
  3074. context.setStrokeStyle(hexToRgb(eachSeries.color, areaOption.opacity));
  3075. if (areaOption.gradient) {
  3076. let gradient = context.createLinearGradient(0, opts.area[0], 0, opts.height - opts.area[2]);
  3077. gradient.addColorStop('0', hexToRgb(eachSeries.color, areaOption.opacity));
  3078. gradient.addColorStop('1.0', hexToRgb("#FFFFFF", 0.1));
  3079. context.setFillStyle(gradient);
  3080. } else {
  3081. context.setFillStyle(hexToRgb(eachSeries.color, areaOption.opacity));
  3082. }
  3083. context.setLineWidth(areaOption.width * opts.pix);
  3084. if (points.length > 1) {
  3085. let firstPoint = points[0];
  3086. let lastPoint = points[points.length - 1];
  3087. context.moveTo(firstPoint.x, firstPoint.y);
  3088. let startPoint = 0;
  3089. if (areaOption.type === 'curve') {
  3090. for (let j = 0; j < points.length; j++) {
  3091. let item = points[j];
  3092. if (startPoint == 0 && item.x > leftSpace) {
  3093. context.moveTo(item.x, item.y);
  3094. startPoint = 1;
  3095. }
  3096. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3097. let ctrlPoint = createCurveControlPoints(points, j - 1);
  3098. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item.y);
  3099. }
  3100. };
  3101. }
  3102. if (areaOption.type === 'straight') {
  3103. for (let j = 0; j < points.length; j++) {
  3104. let item = points[j];
  3105. if (startPoint == 0 && item.x > leftSpace) {
  3106. context.moveTo(item.x, item.y);
  3107. startPoint = 1;
  3108. }
  3109. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3110. context.lineTo(item.x, item.y);
  3111. }
  3112. };
  3113. }
  3114. if (areaOption.type === 'step') {
  3115. for (let j = 0; j < points.length; j++) {
  3116. let item = points[j];
  3117. if (startPoint == 0 && item.x > leftSpace) {
  3118. context.moveTo(item.x, item.y);
  3119. startPoint = 1;
  3120. }
  3121. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3122. context.lineTo(item.x, points[j - 1].y);
  3123. context.lineTo(item.x, item.y);
  3124. }
  3125. };
  3126. }
  3127. context.lineTo(lastPoint.x, endY);
  3128. context.lineTo(firstPoint.x, endY);
  3129. context.lineTo(firstPoint.x, firstPoint.y);
  3130. } else {
  3131. let item = points[0];
  3132. context.moveTo(item.x - eachSpacing / 2, item.y);
  3133. context.lineTo(item.x + eachSpacing / 2, item.y);
  3134. context.lineTo(item.x + eachSpacing / 2, endY);
  3135. context.lineTo(item.x - eachSpacing / 2, endY);
  3136. context.moveTo(item.x - eachSpacing / 2, item.y);
  3137. }
  3138. context.closePath();
  3139. context.fill();
  3140. //画连线
  3141. if (areaOption.addLine) {
  3142. if (eachSeries.lineType == 'dash') {
  3143. let dashLength = eachSeries.dashLength ? eachSeries.dashLength : 8;
  3144. dashLength *= opts.pix;
  3145. context.setLineDash([dashLength, dashLength]);
  3146. }
  3147. context.beginPath();
  3148. context.setStrokeStyle(eachSeries.color);
  3149. context.setLineWidth(areaOption.width * opts.pix);
  3150. if (points.length === 1) {
  3151. context.moveTo(points[0].x, points[0].y);
  3152. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  3153. } else {
  3154. context.moveTo(points[0].x, points[0].y);
  3155. let startPoint = 0;
  3156. if (areaOption.type === 'curve') {
  3157. for (let j = 0; j < points.length; j++) {
  3158. let item = points[j];
  3159. if (startPoint == 0 && item.x > leftSpace) {
  3160. context.moveTo(item.x, item.y);
  3161. startPoint = 1;
  3162. }
  3163. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3164. let ctrlPoint = createCurveControlPoints(points, j - 1);
  3165. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item.y);
  3166. }
  3167. };
  3168. }
  3169. if (areaOption.type === 'straight') {
  3170. for (let j = 0; j < points.length; j++) {
  3171. let item = points[j];
  3172. if (startPoint == 0 && item.x > leftSpace) {
  3173. context.moveTo(item.x, item.y);
  3174. startPoint = 1;
  3175. }
  3176. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3177. context.lineTo(item.x, item.y);
  3178. }
  3179. };
  3180. }
  3181. if (areaOption.type === 'step') {
  3182. for (let j = 0; j < points.length; j++) {
  3183. let item = points[j];
  3184. if (startPoint == 0 && item.x > leftSpace) {
  3185. context.moveTo(item.x, item.y);
  3186. startPoint = 1;
  3187. }
  3188. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3189. context.lineTo(item.x, points[j - 1].y);
  3190. context.lineTo(item.x, item.y);
  3191. }
  3192. };
  3193. }
  3194. context.moveTo(points[0].x, points[0].y);
  3195. }
  3196. context.stroke();
  3197. context.setLineDash([]);
  3198. }
  3199. }
  3200. //画点
  3201. if (opts.dataPointShape !== false) {
  3202. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  3203. }
  3204. });
  3205. if (opts.dataLabel !== false && process === 1) {
  3206. series.forEach(function(eachSeries, seriesIndex) {
  3207. let ranges, minRange, maxRange;
  3208. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3209. minRange = ranges.pop();
  3210. maxRange = ranges.shift();
  3211. var data = eachSeries.data;
  3212. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3213. drawPointText(points, eachSeries, config, context, opts);
  3214. });
  3215. }
  3216. context.restore();
  3217. return {
  3218. xAxisPoints: xAxisPoints,
  3219. calPoints: calPoints,
  3220. eachSpacing: eachSpacing
  3221. };
  3222. }
  3223. function drawScatterDataPoints(series, opts, config, context) {
  3224. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3225. var scatterOption = assign({}, {
  3226. type: 'circle'
  3227. }, opts.extra.scatter);
  3228. let xAxisData = opts.chartData.xAxisData,
  3229. xAxisPoints = xAxisData.xAxisPoints,
  3230. eachSpacing = xAxisData.eachSpacing;
  3231. var calPoints = [];
  3232. context.save();
  3233. let leftSpace = 0;
  3234. let rightSpace = opts.width + eachSpacing;
  3235. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3236. context.translate(opts._scrollDistance_, 0);
  3237. leftSpace = -opts._scrollDistance_ - eachSpacing * 2 + opts.area[3];
  3238. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  3239. }
  3240. series.forEach(function(eachSeries, seriesIndex) {
  3241. let ranges, minRange, maxRange;
  3242. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3243. minRange = ranges.pop();
  3244. maxRange = ranges.shift();
  3245. var data = eachSeries.data;
  3246. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3247. context.beginPath();
  3248. context.setStrokeStyle(eachSeries.color);
  3249. context.setFillStyle(eachSeries.color);
  3250. context.setLineWidth(1 * opts.pix);
  3251. var shape = eachSeries.pointShape;
  3252. if (shape === 'diamond') {
  3253. points.forEach(function(item, index) {
  3254. if (item !== null) {
  3255. context.moveTo(item.x, item.y - 4.5);
  3256. context.lineTo(item.x - 4.5, item.y);
  3257. context.lineTo(item.x, item.y + 4.5);
  3258. context.lineTo(item.x + 4.5, item.y);
  3259. context.lineTo(item.x, item.y - 4.5);
  3260. }
  3261. });
  3262. } else if (shape === 'circle') {
  3263. points.forEach(function(item, index) {
  3264. if (item !== null) {
  3265. context.moveTo(item.x + 2.5 * opts.pix, item.y);
  3266. context.arc(item.x, item.y, 3 * opts.pix, 0, 2 * Math.PI, false);
  3267. }
  3268. });
  3269. } else if (shape === 'square') {
  3270. points.forEach(function(item, index) {
  3271. if (item !== null) {
  3272. context.moveTo(item.x - 3.5, item.y - 3.5);
  3273. context.rect(item.x - 3.5, item.y - 3.5, 7, 7);
  3274. }
  3275. });
  3276. } else if (shape === 'triangle') {
  3277. points.forEach(function(item, index) {
  3278. if (item !== null) {
  3279. context.moveTo(item.x, item.y - 4.5);
  3280. context.lineTo(item.x - 4.5, item.y + 4.5);
  3281. context.lineTo(item.x + 4.5, item.y + 4.5);
  3282. context.lineTo(item.x, item.y - 4.5);
  3283. }
  3284. });
  3285. } else if (shape === 'triangle') {
  3286. return;
  3287. }
  3288. context.closePath();
  3289. context.fill();
  3290. context.stroke();
  3291. });
  3292. if (opts.dataLabel !== false && process === 1) {
  3293. series.forEach(function(eachSeries, seriesIndex) {
  3294. let ranges, minRange, maxRange;
  3295. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3296. minRange = ranges.pop();
  3297. maxRange = ranges.shift();
  3298. var data = eachSeries.data;
  3299. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3300. drawPointText(points, eachSeries, config, context, opts);
  3301. });
  3302. }
  3303. context.restore();
  3304. return {
  3305. xAxisPoints: xAxisPoints,
  3306. calPoints: calPoints,
  3307. eachSpacing: eachSpacing
  3308. };
  3309. }
  3310. function drawBubbleDataPoints(series, opts, config, context) {
  3311. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3312. var bubbleOption = assign({}, {
  3313. opacity: 1,
  3314. border:2
  3315. }, opts.extra.bubble);
  3316. let xAxisData = opts.chartData.xAxisData,
  3317. xAxisPoints = xAxisData.xAxisPoints,
  3318. eachSpacing = xAxisData.eachSpacing;
  3319. var calPoints = [];
  3320. context.save();
  3321. let leftSpace = 0;
  3322. let rightSpace = opts.width + eachSpacing;
  3323. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3324. context.translate(opts._scrollDistance_, 0);
  3325. leftSpace = -opts._scrollDistance_ - eachSpacing * 2 + opts.area[3];
  3326. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  3327. }
  3328. series.forEach(function(eachSeries, seriesIndex) {
  3329. let ranges, minRange, maxRange;
  3330. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3331. minRange = ranges.pop();
  3332. maxRange = ranges.shift();
  3333. var data = eachSeries.data;
  3334. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3335. context.beginPath();
  3336. context.setStrokeStyle(eachSeries.color);
  3337. context.setLineWidth(bubbleOption.border * opts.pix);
  3338. context.setFillStyle(hexToRgb(eachSeries.color, bubbleOption.opacity));
  3339. points.forEach(function(item, index) {
  3340. context.moveTo(item.x + item.r, item.y);
  3341. context.arc(item.x, item.y, item.r * opts.pix, 0, 2 * Math.PI, false);
  3342. });
  3343. context.closePath();
  3344. context.fill();
  3345. context.stroke();
  3346. if (opts.dataLabel !== false && process === 1) {
  3347. points.forEach(function(item, index) {
  3348. context.beginPath();
  3349. var fontSize = series.textSize * opts.pix || config.fontSize;
  3350. context.setFontSize(fontSize);
  3351. context.setFillStyle(series.textColor || "#FFFFFF");
  3352. context.setTextAlign('center');
  3353. context.fillText(String(item.t), item.x, item.y + fontSize/2);
  3354. context.closePath();
  3355. context.stroke();
  3356. context.setTextAlign('left');
  3357. });
  3358. }
  3359. });
  3360. context.restore();
  3361. return {
  3362. xAxisPoints: xAxisPoints,
  3363. calPoints: calPoints,
  3364. eachSpacing: eachSpacing
  3365. };
  3366. }
  3367. function drawLineDataPoints(series, opts, config, context) {
  3368. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3369. var lineOption = assign({}, {
  3370. type: 'straight',
  3371. width: 2
  3372. }, opts.extra.line);
  3373. lineOption.width *= opts.pix;
  3374. let xAxisData = opts.chartData.xAxisData,
  3375. xAxisPoints = xAxisData.xAxisPoints,
  3376. eachSpacing = xAxisData.eachSpacing;
  3377. var calPoints = [];
  3378. context.save();
  3379. let leftSpace = 0;
  3380. let rightSpace = opts.width + eachSpacing;
  3381. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3382. context.translate(opts._scrollDistance_, 0);
  3383. leftSpace = -opts._scrollDistance_ - eachSpacing * 2 + opts.area[3];
  3384. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  3385. }
  3386. series.forEach(function(eachSeries, seriesIndex) {
  3387. let ranges, minRange, maxRange;
  3388. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3389. minRange = ranges.pop();
  3390. maxRange = ranges.shift();
  3391. var data = eachSeries.data;
  3392. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3393. calPoints.push(points);
  3394. var splitPointList = splitPoints(points,eachSeries);
  3395. if (eachSeries.lineType == 'dash') {
  3396. let dashLength = eachSeries.dashLength ? eachSeries.dashLength : 8;
  3397. dashLength *= opts.pix;
  3398. context.setLineDash([dashLength, dashLength]);
  3399. }
  3400. context.beginPath();
  3401. context.setStrokeStyle(eachSeries.color);
  3402. context.setLineWidth(lineOption.width);
  3403. splitPointList.forEach(function(points, index) {
  3404. if (points.length === 1) {
  3405. context.moveTo(points[0].x, points[0].y);
  3406. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  3407. } else {
  3408. context.moveTo(points[0].x, points[0].y);
  3409. let startPoint = 0;
  3410. if (lineOption.type === 'curve') {
  3411. for (let j = 0; j < points.length; j++) {
  3412. let item = points[j];
  3413. if (startPoint == 0 && item.x > leftSpace) {
  3414. context.moveTo(item.x, item.y);
  3415. startPoint = 1;
  3416. }
  3417. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3418. var ctrlPoint = createCurveControlPoints(points, j - 1);
  3419. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item.y);
  3420. }
  3421. };
  3422. }
  3423. if (lineOption.type === 'straight') {
  3424. for (let j = 0; j < points.length; j++) {
  3425. let item = points[j];
  3426. if (startPoint == 0 && item.x > leftSpace) {
  3427. context.moveTo(item.x, item.y);
  3428. startPoint = 1;
  3429. }
  3430. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3431. context.lineTo(item.x, item.y);
  3432. }
  3433. };
  3434. }
  3435. if (lineOption.type === 'step') {
  3436. for (let j = 0; j < points.length; j++) {
  3437. let item = points[j];
  3438. if (startPoint == 0 && item.x > leftSpace) {
  3439. context.moveTo(item.x, item.y);
  3440. startPoint = 1;
  3441. }
  3442. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3443. context.lineTo(item.x, points[j - 1].y);
  3444. context.lineTo(item.x, item.y);
  3445. }
  3446. };
  3447. }
  3448. context.moveTo(points[0].x, points[0].y);
  3449. }
  3450. });
  3451. context.stroke();
  3452. context.setLineDash([]);
  3453. if (opts.dataPointShape !== false) {
  3454. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  3455. }
  3456. });
  3457. if (opts.dataLabel !== false && process === 1) {
  3458. series.forEach(function(eachSeries, seriesIndex) {
  3459. let ranges, minRange, maxRange;
  3460. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3461. minRange = ranges.pop();
  3462. maxRange = ranges.shift();
  3463. var data = eachSeries.data;
  3464. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3465. drawPointText(points, eachSeries, config, context, opts);
  3466. });
  3467. }
  3468. context.restore();
  3469. return {
  3470. xAxisPoints: xAxisPoints,
  3471. calPoints: calPoints,
  3472. eachSpacing: eachSpacing
  3473. };
  3474. }
  3475. function drawMixDataPoints(series, opts, config, context) {
  3476. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3477. let columnOption = assign({}, {
  3478. width: eachSpacing / 2,
  3479. barBorderCircle: false,
  3480. barBorderRadius: [],
  3481. seriesGap: 2,
  3482. linearType: 'none',
  3483. linearOpacity: 1,
  3484. customColor: [],
  3485. colorStop: 0,
  3486. }, opts.extra.mix.column);
  3487. let xAxisData = opts.chartData.xAxisData,
  3488. xAxisPoints = xAxisData.xAxisPoints,
  3489. eachSpacing = xAxisData.eachSpacing;
  3490. let endY = opts.height - opts.area[2];
  3491. let calPoints = [];
  3492. var columnIndex = 0;
  3493. var columnLength = 0;
  3494. series.forEach(function(eachSeries, seriesIndex) {
  3495. if (eachSeries.type == 'column') {
  3496. columnLength += 1;
  3497. }
  3498. });
  3499. context.save();
  3500. let leftNum = -2;
  3501. let rightNum = xAxisPoints.length + 2;
  3502. let leftSpace = 0;
  3503. let rightSpace = opts.width + eachSpacing;
  3504. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3505. context.translate(opts._scrollDistance_, 0);
  3506. leftNum = Math.floor(-opts._scrollDistance_ / eachSpacing) - 2;
  3507. rightNum = leftNum + opts.xAxis.itemCount + 4;
  3508. leftSpace = -opts._scrollDistance_ - eachSpacing * 2 + opts.area[3];
  3509. rightSpace = leftSpace + (opts.xAxis.itemCount + 4) * eachSpacing;
  3510. }
  3511. columnOption.customColor = fillCustomColor(columnOption.linearType, columnOption.customColor, series, config);
  3512. series.forEach(function(eachSeries, seriesIndex) {
  3513. let ranges, minRange, maxRange;
  3514. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3515. minRange = ranges.pop();
  3516. maxRange = ranges.shift();
  3517. var data = eachSeries.data;
  3518. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3519. calPoints.push(points);
  3520. // 绘制柱状数据图
  3521. if (eachSeries.type == 'column') {
  3522. points = fixColumeData(points, eachSpacing, columnLength, columnIndex, config, opts);
  3523. for (let i = 0; i < points.length; i++) {
  3524. let item = points[i];
  3525. if (item !== null && i > leftNum && i < rightNum) {
  3526. var startX = item.x - item.width / 2;
  3527. var height = opts.height - item.y - opts.area[2];
  3528. context.beginPath();
  3529. var fillColor = item.color || eachSeries.color
  3530. var strokeColor = item.color || eachSeries.color
  3531. if (columnOption.linearType !== 'none') {
  3532. var grd = context.createLinearGradient(startX, item.y, startX, opts.height - opts.area[2]);
  3533. //透明渐变
  3534. if (columnOption.linearType == 'opacity') {
  3535. grd.addColorStop(0, hexToRgb(fillColor, columnOption.linearOpacity));
  3536. grd.addColorStop(1, hexToRgb(fillColor, 1));
  3537. } else {
  3538. grd.addColorStop(0, hexToRgb(columnOption.customColor[eachSeries.linearIndex], columnOption.linearOpacity));
  3539. grd.addColorStop(columnOption.colorStop, hexToRgb(columnOption.customColor[eachSeries.linearIndex], columnOption.linearOpacity));
  3540. grd.addColorStop(1, hexToRgb(fillColor, 1));
  3541. }
  3542. fillColor = grd
  3543. }
  3544. // 圆角边框
  3545. if ((columnOption.barBorderRadius && columnOption.barBorderRadius.length === 4) || columnOption.barBorderCircle) {
  3546. const left = startX;
  3547. const top = item.y;
  3548. const width = item.width;
  3549. const height = opts.height - opts.area[2] - item.y;
  3550. if (columnOption.barBorderCircle) {
  3551. columnOption.barBorderRadius = [width / 2, width / 2, 0, 0];
  3552. }
  3553. let [r0, r1, r2, r3] = columnOption.barBorderRadius;
  3554. let minRadius = Math.min(width/2,height/2);
  3555. r0 = r0 > minRadius ? minRadius : r0;
  3556. r1 = r1 > minRadius ? minRadius : r1;
  3557. r2 = r2 > minRadius ? minRadius : r2;
  3558. r3 = r3 > minRadius ? minRadius : r3;
  3559. r0 = r0 < 0 ? 0 : r0;
  3560. r1 = r1 < 0 ? 0 : r1;
  3561. r2 = r2 < 0 ? 0 : r2;
  3562. r3 = r3 < 0 ? 0 : r3;
  3563. context.arc(left + r0, top + r0, r0, -Math.PI, -Math.PI / 2);
  3564. context.arc(left + width - r1, top + r1, r1, -Math.PI / 2, 0);
  3565. context.arc(left + width - r2, top + height - r2, r2, 0, Math.PI / 2);
  3566. context.arc(left + r3, top + height - r3, r3, Math.PI / 2, Math.PI);
  3567. } else {
  3568. context.moveTo(startX, item.y);
  3569. context.lineTo(startX + item.width - 2, item.y);
  3570. context.lineTo(startX + item.width - 2, opts.height - opts.area[2]);
  3571. context.lineTo(startX, opts.height - opts.area[2]);
  3572. context.lineTo(startX, item.y);
  3573. context.setLineWidth(1)
  3574. context.setStrokeStyle(strokeColor);
  3575. }
  3576. context.setFillStyle(fillColor);
  3577. context.closePath();
  3578. context.fill();
  3579. }
  3580. }
  3581. columnIndex += 1;
  3582. }
  3583. //绘制区域图数据
  3584. if (eachSeries.type == 'area') {
  3585. let splitPointList = splitPoints(points,eachSeries);
  3586. for (let i = 0; i < splitPointList.length; i++) {
  3587. let points = splitPointList[i];
  3588. // 绘制区域数据
  3589. context.beginPath();
  3590. context.setStrokeStyle(eachSeries.color);
  3591. context.setFillStyle(hexToRgb(eachSeries.color, 0.2));
  3592. context.setLineWidth(2 * opts.pix);
  3593. if (points.length > 1) {
  3594. var firstPoint = points[0];
  3595. let lastPoint = points[points.length - 1];
  3596. context.moveTo(firstPoint.x, firstPoint.y);
  3597. let startPoint = 0;
  3598. if (eachSeries.style === 'curve') {
  3599. for (let j = 0; j < points.length; j++) {
  3600. let item = points[j];
  3601. if (startPoint == 0 && item.x > leftSpace) {
  3602. context.moveTo(item.x, item.y);
  3603. startPoint = 1;
  3604. }
  3605. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3606. var ctrlPoint = createCurveControlPoints(points, j - 1);
  3607. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item.y);
  3608. }
  3609. };
  3610. } else {
  3611. for (let j = 0; j < points.length; j++) {
  3612. let item = points[j];
  3613. if (startPoint == 0 && item.x > leftSpace) {
  3614. context.moveTo(item.x, item.y);
  3615. startPoint = 1;
  3616. }
  3617. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3618. context.lineTo(item.x, item.y);
  3619. }
  3620. };
  3621. }
  3622. context.lineTo(lastPoint.x, endY);
  3623. context.lineTo(firstPoint.x, endY);
  3624. context.lineTo(firstPoint.x, firstPoint.y);
  3625. } else {
  3626. let item = points[0];
  3627. context.moveTo(item.x - eachSpacing / 2, item.y);
  3628. context.lineTo(item.x + eachSpacing / 2, item.y);
  3629. context.lineTo(item.x + eachSpacing / 2, endY);
  3630. context.lineTo(item.x - eachSpacing / 2, endY);
  3631. context.moveTo(item.x - eachSpacing / 2, item.y);
  3632. }
  3633. context.closePath();
  3634. context.fill();
  3635. }
  3636. }
  3637. // 绘制折线数据图
  3638. if (eachSeries.type == 'line') {
  3639. var splitPointList = splitPoints(points,eachSeries);
  3640. splitPointList.forEach(function(points, index) {
  3641. if (eachSeries.lineType == 'dash') {
  3642. let dashLength = eachSeries.dashLength ? eachSeries.dashLength : 8;
  3643. dashLength *= opts.pix;
  3644. context.setLineDash([dashLength, dashLength]);
  3645. }
  3646. context.beginPath();
  3647. context.setStrokeStyle(eachSeries.color);
  3648. context.setLineWidth(2 * opts.pix);
  3649. if (points.length === 1) {
  3650. context.moveTo(points[0].x, points[0].y);
  3651. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  3652. } else {
  3653. context.moveTo(points[0].x, points[0].y);
  3654. let startPoint = 0;
  3655. if (eachSeries.style == 'curve') {
  3656. for (let j = 0; j < points.length; j++) {
  3657. let item = points[j];
  3658. if (startPoint == 0 && item.x > leftSpace) {
  3659. context.moveTo(item.x, item.y);
  3660. startPoint = 1;
  3661. }
  3662. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3663. var ctrlPoint = createCurveControlPoints(points, j - 1);
  3664. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y,
  3665. item.x, item.y);
  3666. }
  3667. }
  3668. } else {
  3669. for (let j = 0; j < points.length; j++) {
  3670. let item = points[j];
  3671. if (startPoint == 0 && item.x > leftSpace) {
  3672. context.moveTo(item.x, item.y);
  3673. startPoint = 1;
  3674. }
  3675. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  3676. context.lineTo(item.x, item.y);
  3677. }
  3678. }
  3679. }
  3680. context.moveTo(points[0].x, points[0].y);
  3681. }
  3682. context.stroke();
  3683. context.setLineDash([]);
  3684. });
  3685. }
  3686. // 绘制点数据图
  3687. if (eachSeries.type == 'point') {
  3688. eachSeries.addPoint = true;
  3689. }
  3690. if (eachSeries.addPoint == true && eachSeries.type !== 'column') {
  3691. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  3692. }
  3693. });
  3694. if (opts.dataLabel !== false && process === 1) {
  3695. var columnIndex = 0;
  3696. series.forEach(function(eachSeries, seriesIndex) {
  3697. let ranges, minRange, maxRange;
  3698. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  3699. minRange = ranges.pop();
  3700. maxRange = ranges.shift();
  3701. var data = eachSeries.data;
  3702. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  3703. if (eachSeries.type !== 'column') {
  3704. drawPointText(points, eachSeries, config, context, opts);
  3705. } else {
  3706. points = fixColumeData(points, eachSpacing, columnLength, columnIndex, config, opts);
  3707. drawPointText(points, eachSeries, config, context, opts);
  3708. columnIndex += 1;
  3709. }
  3710. });
  3711. }
  3712. context.restore();
  3713. return {
  3714. xAxisPoints: xAxisPoints,
  3715. calPoints: calPoints,
  3716. eachSpacing: eachSpacing,
  3717. }
  3718. }
  3719. function drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints) {
  3720. var toolTipOption = opts.extra.tooltip || {};
  3721. if (toolTipOption.horizentalLine && opts.tooltip && process === 1 && (opts.type == 'line' || opts.type == 'area' || opts.type == 'column' || opts.type == 'candle' || opts.type == 'mix')) {
  3722. drawToolTipHorizentalLine(opts, config, context, eachSpacing, xAxisPoints)
  3723. }
  3724. context.save();
  3725. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  3726. context.translate(opts._scrollDistance_, 0);
  3727. }
  3728. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  3729. drawToolTip(opts.tooltip.textList, opts.tooltip.offset, opts, config, context, eachSpacing, xAxisPoints);
  3730. }
  3731. context.restore();
  3732. }
  3733. function drawXAxis(categories, opts, config, context) {
  3734. let xAxisData = opts.chartData.xAxisData,
  3735. xAxisPoints = xAxisData.xAxisPoints,
  3736. startX = xAxisData.startX,
  3737. endX = xAxisData.endX,
  3738. eachSpacing = xAxisData.eachSpacing;
  3739. var boundaryGap = 'center';
  3740. if (opts.type == 'bar' || opts.type == 'line' || opts.type == 'area'|| opts.type == 'scatter' || opts.type == 'bubble') {
  3741. boundaryGap = opts.xAxis.boundaryGap;
  3742. }
  3743. var startY = opts.height - opts.area[2];
  3744. var endY = opts.area[0];
  3745. //绘制滚动条
  3746. if (opts.enableScroll && opts.xAxis.scrollShow) {
  3747. var scrollY = opts.height - opts.area[2] + config.xAxisHeight;
  3748. var scrollScreenWidth = endX - startX;
  3749. var scrollTotalWidth = eachSpacing * (xAxisPoints.length - 1);
  3750. var scrollWidth = scrollScreenWidth * scrollScreenWidth / scrollTotalWidth;
  3751. var scrollLeft = 0;
  3752. if (opts._scrollDistance_) {
  3753. scrollLeft = -opts._scrollDistance_ * (scrollScreenWidth) / scrollTotalWidth;
  3754. }
  3755. context.beginPath();
  3756. context.setLineCap('round');
  3757. context.setLineWidth(6 * opts.pix);
  3758. context.setStrokeStyle(opts.xAxis.scrollBackgroundColor || "#EFEBEF");
  3759. context.moveTo(startX, scrollY);
  3760. context.lineTo(endX, scrollY);
  3761. context.stroke();
  3762. context.closePath();
  3763. context.beginPath();
  3764. context.setLineCap('round');
  3765. context.setLineWidth(6 * opts.pix);
  3766. context.setStrokeStyle(opts.xAxis.scrollColor || "#A6A6A6");
  3767. context.moveTo(startX + scrollLeft, scrollY);
  3768. context.lineTo(startX + scrollLeft + scrollWidth, scrollY);
  3769. context.stroke();
  3770. context.closePath();
  3771. context.setLineCap('butt');
  3772. }
  3773. context.save();
  3774. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0) {
  3775. context.translate(opts._scrollDistance_, 0);
  3776. }
  3777. //绘制X轴刻度线
  3778. if (opts.xAxis.calibration === true) {
  3779. context.setStrokeStyle(opts.xAxis.gridColor || "#cccccc");
  3780. context.setLineCap('butt');
  3781. context.setLineWidth(1 * opts.pix);
  3782. xAxisPoints.forEach(function(item, index) {
  3783. if (index > 0) {
  3784. context.beginPath();
  3785. context.moveTo(item - eachSpacing / 2, startY);
  3786. context.lineTo(item - eachSpacing / 2, startY + 3 * opts.pix);
  3787. context.closePath();
  3788. context.stroke();
  3789. }
  3790. });
  3791. }
  3792. //绘制X轴网格
  3793. if (opts.xAxis.disableGrid !== true) {
  3794. context.setStrokeStyle(opts.xAxis.gridColor || "#cccccc");
  3795. context.setLineCap('butt');
  3796. context.setLineWidth(1 * opts.pix);
  3797. if (opts.xAxis.gridType == 'dash') {
  3798. context.setLineDash([opts.xAxis.dashLength * opts.pix, opts.xAxis.dashLength * opts.pix]);
  3799. }
  3800. opts.xAxis.gridEval = opts.xAxis.gridEval || 1;
  3801. xAxisPoints.forEach(function(item, index) {
  3802. if (index % opts.xAxis.gridEval == 0) {
  3803. context.beginPath();
  3804. context.moveTo(item, startY);
  3805. context.lineTo(item, endY);
  3806. context.stroke();
  3807. }
  3808. });
  3809. context.setLineDash([]);
  3810. }
  3811. //绘制X轴文案
  3812. if (opts.xAxis.disabled !== true) {
  3813. // 对X轴列表做抽稀处理
  3814. //默认全部显示X轴标签
  3815. let maxXAxisListLength = categories.length;
  3816. //如果设置了X轴单屏数量
  3817. if (opts.xAxis.labelCount) {
  3818. //如果设置X轴密度
  3819. if (opts.xAxis.itemCount) {
  3820. maxXAxisListLength = Math.ceil(categories.length / opts.xAxis.itemCount * opts.xAxis.labelCount);
  3821. } else {
  3822. maxXAxisListLength = opts.xAxis.labelCount;
  3823. }
  3824. maxXAxisListLength -= 1;
  3825. }
  3826. let ratio = Math.ceil(categories.length / maxXAxisListLength);
  3827. let newCategories = [];
  3828. let cgLength = categories.length;
  3829. for (let i = 0; i < cgLength; i++) {
  3830. if (i % ratio !== 0) {
  3831. newCategories.push("");
  3832. } else {
  3833. newCategories.push(categories[i]);
  3834. }
  3835. }
  3836. newCategories[cgLength - 1] = categories[cgLength - 1];
  3837. var xAxisFontSize = opts.xAxis.fontSize * opts.pix || config.fontSize;
  3838. if (config._xAxisTextAngle_ === 0) {
  3839. newCategories.forEach(function(item, index) {
  3840. var xitem = opts.xAxis.formatter ? opts.xAxis.formatter(item) : item;
  3841. var offset = -measureText(String(xitem), xAxisFontSize, context) / 2;
  3842. if (boundaryGap == 'center') {
  3843. offset += eachSpacing / 2;
  3844. }
  3845. var scrollHeight = 0;
  3846. if (opts.xAxis.scrollShow) {
  3847. scrollHeight = 6 * opts.pix;
  3848. }
  3849. context.beginPath();
  3850. context.setFontSize(xAxisFontSize);
  3851. context.setFillStyle(opts.xAxis.fontColor || opts.fontColor);
  3852. context.fillText(String(xitem), xAxisPoints[index] + offset, startY + xAxisFontSize + (config.xAxisHeight - scrollHeight - xAxisFontSize) / 2);
  3853. context.closePath();
  3854. context.stroke();
  3855. });
  3856. } else {
  3857. newCategories.forEach(function(item, index) {
  3858. var xitem = opts.xAxis.formatter ? opts.xAxis.formatter(item) : item;
  3859. context.save();
  3860. context.beginPath();
  3861. context.setFontSize(xAxisFontSize);
  3862. context.setFillStyle(opts.xAxis.fontColor || opts.fontColor);
  3863. var textWidth = measureText(String(xitem), xAxisFontSize, context);
  3864. var offset = -textWidth;
  3865. if (boundaryGap == 'center') {
  3866. offset += eachSpacing / 2;
  3867. }
  3868. var _calRotateTranslate = calRotateTranslate(xAxisPoints[index] + eachSpacing / 2, startY + xAxisFontSize / 2 + 5, opts.height),
  3869. transX = _calRotateTranslate.transX,
  3870. transY = _calRotateTranslate.transY;
  3871. context.rotate(-1 * config._xAxisTextAngle_);
  3872. context.translate(transX, transY);
  3873. context.fillText(String(xitem), xAxisPoints[index] + offset, startY + xAxisFontSize + 5);
  3874. context.closePath();
  3875. context.stroke();
  3876. context.restore();
  3877. });
  3878. }
  3879. }
  3880. context.restore();
  3881. //绘制X轴轴线
  3882. if (opts.xAxis.axisLine) {
  3883. context.beginPath();
  3884. context.setStrokeStyle(opts.xAxis.axisLineColor);
  3885. context.setLineWidth(1 * opts.pix);
  3886. context.moveTo(startX, opts.height - opts.area[2]);
  3887. context.lineTo(endX, opts.height - opts.area[2]);
  3888. context.stroke();
  3889. }
  3890. }
  3891. function drawYAxisGrid(categories, opts, config, context) {
  3892. if (opts.yAxis.disableGrid === true) {
  3893. return;
  3894. }
  3895. let spacingValid = opts.height - opts.area[0] - opts.area[2];
  3896. let eachSpacing = spacingValid / opts.yAxis.splitNumber;
  3897. let startX = opts.area[3];
  3898. let xAxisPoints = opts.chartData.xAxisData.xAxisPoints,
  3899. xAxiseachSpacing = opts.chartData.xAxisData.eachSpacing;
  3900. let TotalWidth = xAxiseachSpacing * (xAxisPoints.length - 1);
  3901. let endX = startX + TotalWidth;
  3902. let points = [];
  3903. let startY = 1
  3904. if (opts.xAxis.axisLine === false) {
  3905. startY = 0
  3906. }
  3907. for (let i = startY; i < opts.yAxis.splitNumber + 1; i++) {
  3908. points.push(opts.height - opts.area[2] - eachSpacing * i);
  3909. }
  3910. context.save();
  3911. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0) {
  3912. context.translate(opts._scrollDistance_, 0);
  3913. }
  3914. if (opts.yAxis.gridType == 'dash') {
  3915. context.setLineDash([opts.yAxis.dashLength * opts.pix, opts.yAxis.dashLength * opts.pix]);
  3916. }
  3917. context.setStrokeStyle(opts.yAxis.gridColor);
  3918. context.setLineWidth(1 * opts.pix);
  3919. points.forEach(function(item, index) {
  3920. context.beginPath();
  3921. context.moveTo(startX, item);
  3922. context.lineTo(endX, item);
  3923. context.stroke();
  3924. });
  3925. context.setLineDash([]);
  3926. context.restore();
  3927. }
  3928. function drawYAxis(series, opts, config, context) {
  3929. if (opts.yAxis.disabled === true) {
  3930. return;
  3931. }
  3932. var spacingValid = opts.height - opts.area[0] - opts.area[2];
  3933. var eachSpacing = spacingValid / opts.yAxis.splitNumber;
  3934. var startX = opts.area[3];
  3935. var endX = opts.width - opts.area[1];
  3936. var endY = opts.height - opts.area[2];
  3937. var fillEndY = endY + config.xAxisHeight;
  3938. if (opts.xAxis.scrollShow) {
  3939. fillEndY -= 3 * opts.pix;
  3940. }
  3941. if (opts.xAxis.rotateLabel) {
  3942. fillEndY = opts.height - opts.area[2] + opts.fontSize * opts.pix / 2;
  3943. }
  3944. // set YAxis background
  3945. context.beginPath();
  3946. context.setFillStyle(opts.background);
  3947. if (opts.enableScroll == true && opts.xAxis.scrollPosition && opts.xAxis.scrollPosition !== 'left') {
  3948. context.fillRect(0, 0, startX, fillEndY);
  3949. }
  3950. if (opts.enableScroll == true && opts.xAxis.scrollPosition && opts.xAxis.scrollPosition !== 'right') {
  3951. context.fillRect(endX, 0, opts.width, fillEndY);
  3952. }
  3953. context.closePath();
  3954. context.stroke();
  3955. let tStartLeft = opts.area[3];
  3956. let tStartRight = opts.width - opts.area[1];
  3957. let tStartCenter = opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2;
  3958. if (opts.yAxis.data) {
  3959. for (let i = 0; i < opts.yAxis.data.length; i++) {
  3960. let yData = opts.yAxis.data[i];
  3961. var points = [];
  3962. if(yData.type === 'categories'){
  3963. for (let i = 0; i <= yData.categories.length; i++) {
  3964. points.push(opts.area[0] + spacingValid / yData.categories.length / 2 + spacingValid / yData.categories.length * i);
  3965. }
  3966. }else{
  3967. for (let i = 0; i <= opts.yAxis.splitNumber; i++) {
  3968. points.push(opts.area[0] + eachSpacing * i);
  3969. }
  3970. }
  3971. if (yData.disabled !== true) {
  3972. let rangesFormat = opts.chartData.yAxisData.rangesFormat[i];
  3973. let yAxisFontSize = yData.fontSize ? yData.fontSize * opts.pix : config.fontSize;
  3974. let yAxisWidth = opts.chartData.yAxisData.yAxisWidth[i];
  3975. let textAlign = yData.textAlign || "right";
  3976. //画Y轴刻度及文案
  3977. rangesFormat.forEach(function(item, index) {
  3978. var pos = points[index] ? points[index] : endY;
  3979. context.beginPath();
  3980. context.setFontSize(yAxisFontSize);
  3981. context.setLineWidth(1 * opts.pix);
  3982. context.setStrokeStyle(yData.axisLineColor || '#cccccc');
  3983. context.setFillStyle(yData.fontColor || opts.fontColor);
  3984. let tmpstrat = 0;
  3985. let gapwidth = 4 * opts.pix;
  3986. if (yAxisWidth.position == 'left') {
  3987. //画刻度线
  3988. if (yData.calibration == true) {
  3989. context.moveTo(tStartLeft, pos);
  3990. context.lineTo(tStartLeft - 3 * opts.pix, pos);
  3991. gapwidth += 3 * opts.pix;
  3992. }
  3993. //画文字
  3994. switch (textAlign) {
  3995. case "left":
  3996. context.setTextAlign('left');
  3997. tmpstrat = tStartLeft - yAxisWidth.width
  3998. break;
  3999. case "right":
  4000. context.setTextAlign('right');
  4001. tmpstrat = tStartLeft - gapwidth
  4002. break;
  4003. default:
  4004. context.setTextAlign('center');
  4005. tmpstrat = tStartLeft - yAxisWidth.width / 2
  4006. }
  4007. context.fillText(String(item), tmpstrat, pos + yAxisFontSize / 2 - 3 * opts.pix);
  4008. } else if (yAxisWidth.position == 'right') {
  4009. //画刻度线
  4010. if (yData.calibration == true) {
  4011. context.moveTo(tStartRight, pos);
  4012. context.lineTo(tStartRight + 3 * opts.pix, pos);
  4013. gapwidth += 3 * opts.pix;
  4014. }
  4015. switch (textAlign) {
  4016. case "left":
  4017. context.setTextAlign('left');
  4018. tmpstrat = tStartRight + gapwidth
  4019. break;
  4020. case "right":
  4021. context.setTextAlign('right');
  4022. tmpstrat = tStartRight + yAxisWidth.width
  4023. break;
  4024. default:
  4025. context.setTextAlign('center');
  4026. tmpstrat = tStartRight + yAxisWidth.width / 2
  4027. }
  4028. context.fillText(String(item), tmpstrat, pos + yAxisFontSize / 2 - 3 * opts.pix);
  4029. } else if (yAxisWidth.position == 'center') {
  4030. //画刻度线
  4031. if (yData.calibration == true) {
  4032. context.moveTo(tStartCenter, pos);
  4033. context.lineTo(tStartCenter - 3 * opts.pix, pos);
  4034. gapwidth += 3 * opts.pix;
  4035. }
  4036. //画文字
  4037. switch (textAlign) {
  4038. case "left":
  4039. context.setTextAlign('left');
  4040. tmpstrat = tStartCenter - yAxisWidth.width
  4041. break;
  4042. case "right":
  4043. context.setTextAlign('right');
  4044. tmpstrat = tStartCenter - gapwidth
  4045. break;
  4046. default:
  4047. context.setTextAlign('center');
  4048. tmpstrat = tStartCenter - yAxisWidth.width / 2
  4049. }
  4050. context.fillText(String(item), tmpstrat, pos + yAxisFontSize / 2 - 3 * opts.pix);
  4051. }
  4052. context.closePath();
  4053. context.stroke();
  4054. context.setTextAlign('left');
  4055. });
  4056. //画Y轴轴线
  4057. if (yData.axisLine !== false) {
  4058. context.beginPath();
  4059. context.setStrokeStyle(yData.axisLineColor || '#cccccc');
  4060. context.setLineWidth(1 * opts.pix);
  4061. if (yAxisWidth.position == 'left') {
  4062. context.moveTo(tStartLeft, opts.height - opts.area[2]);
  4063. context.lineTo(tStartLeft, opts.area[0]);
  4064. } else if (yAxisWidth.position == 'right') {
  4065. context.moveTo(tStartRight, opts.height - opts.area[2]);
  4066. context.lineTo(tStartRight, opts.area[0]);
  4067. } else if (yAxisWidth.position == 'center') {
  4068. context.moveTo(tStartCenter, opts.height - opts.area[2]);
  4069. context.lineTo(tStartCenter, opts.area[0]);
  4070. }
  4071. context.stroke();
  4072. }
  4073. //画Y轴标题
  4074. if (opts.yAxis.showTitle) {
  4075. let titleFontSize = yData.titleFontSize * opts.pix || config.fontSize;
  4076. let title = yData.title;
  4077. context.beginPath();
  4078. context.setFontSize(titleFontSize);
  4079. context.setFillStyle(yData.titleFontColor || opts.fontColor);
  4080. if (yAxisWidth.position == 'left') {
  4081. context.fillText(title, tStartLeft - measureText(title, titleFontSize, context) / 2 + (yData.titleOffsetX || 0), opts.area[0] - (10 - (yData.titleOffsetY || 0)) * opts.pix);
  4082. } else if (yAxisWidth.position == 'right') {
  4083. context.fillText(title, tStartRight - measureText(title, titleFontSize, context) / 2 + (yData.titleOffsetX || 0), opts.area[0] - (10 - (yData.titleOffsetY || 0)) * opts.pix);
  4084. } else if (yAxisWidth.position == 'center') {
  4085. context.fillText(title, tStartCenter - measureText(title, titleFontSize, context) / 2 + (yData.titleOffsetX || 0), opts.area[0] - (10 - (yData.titleOffsetY || 0)) * opts.pix);
  4086. }
  4087. context.closePath();
  4088. context.stroke();
  4089. }
  4090. if (yAxisWidth.position == 'left') {
  4091. tStartLeft -= (yAxisWidth.width + opts.yAxis.padding * opts.pix);
  4092. } else {
  4093. tStartRight += yAxisWidth.width + opts.yAxis.padding * opts.pix;
  4094. }
  4095. }
  4096. }
  4097. }
  4098. }
  4099. function drawLegend(series, opts, config, context, chartData) {
  4100. if (opts.legend.show === false) {
  4101. return;
  4102. }
  4103. let legendData = chartData.legendData;
  4104. let legendList = legendData.points;
  4105. let legendArea = legendData.area;
  4106. let padding = opts.legend.padding * opts.pix;
  4107. let fontSize = opts.legend.fontSize * opts.pix;
  4108. let shapeWidth = 15 * opts.pix;
  4109. let shapeRight = 5 * opts.pix;
  4110. let itemGap = opts.legend.itemGap * opts.pix;
  4111. let lineHeight = Math.max(opts.legend.lineHeight * opts.pix, fontSize);
  4112. //画背景及边框
  4113. context.beginPath();
  4114. context.setLineWidth(opts.legend.borderWidth * opts.pix);
  4115. context.setStrokeStyle(opts.legend.borderColor);
  4116. context.setFillStyle(opts.legend.backgroundColor);
  4117. context.moveTo(legendArea.start.x, legendArea.start.y);
  4118. context.rect(legendArea.start.x, legendArea.start.y, legendArea.width, legendArea.height);
  4119. context.closePath();
  4120. context.fill();
  4121. context.stroke();
  4122. legendList.forEach(function(itemList, listIndex) {
  4123. let width = 0;
  4124. let height = 0;
  4125. width = legendData.widthArr[listIndex];
  4126. height = legendData.heightArr[listIndex];
  4127. let startX = 0;
  4128. let startY = 0;
  4129. if (opts.legend.position == 'top' || opts.legend.position == 'bottom') {
  4130. switch (opts.legend.float) {
  4131. case 'left':
  4132. startX = legendArea.start.x + padding;
  4133. break;
  4134. case 'right':
  4135. startX = legendArea.start.x + legendArea.width - width;
  4136. break;
  4137. default:
  4138. startX = legendArea.start.x + (legendArea.width - width) / 2;
  4139. }
  4140. startY = legendArea.start.y + padding + listIndex * lineHeight;
  4141. } else {
  4142. if (listIndex == 0) {
  4143. width = 0;
  4144. } else {
  4145. width = legendData.widthArr[listIndex - 1];
  4146. }
  4147. startX = legendArea.start.x + padding + width;
  4148. startY = legendArea.start.y + padding + (legendArea.height - height) / 2;
  4149. }
  4150. context.setFontSize(config.fontSize);
  4151. for (let i = 0; i < itemList.length; i++) {
  4152. let item = itemList[i];
  4153. item.area = [0, 0, 0, 0];
  4154. item.area[0] = startX;
  4155. item.area[1] = startY;
  4156. item.area[3] = startY + lineHeight;
  4157. context.beginPath();
  4158. context.setLineWidth(1 * opts.pix);
  4159. context.setStrokeStyle(item.show ? item.color : opts.legend.hiddenColor);
  4160. context.setFillStyle(item.show ? item.color : opts.legend.hiddenColor);
  4161. switch (item.legendShape) {
  4162. case 'line':
  4163. context.moveTo(startX, startY + 0.5 * lineHeight - 2 * opts.pix);
  4164. context.fillRect(startX, startY + 0.5 * lineHeight - 2 * opts.pix, 15 * opts.pix, 4 * opts.pix);
  4165. break;
  4166. case 'triangle':
  4167. context.moveTo(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight - 5 * opts.pix);
  4168. context.lineTo(startX + 2.5 * opts.pix, startY + 0.5 * lineHeight + 5 * opts.pix);
  4169. context.lineTo(startX + 12.5 * opts.pix, startY + 0.5 * lineHeight + 5 * opts.pix);
  4170. context.lineTo(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight - 5 * opts.pix);
  4171. break;
  4172. case 'diamond':
  4173. context.moveTo(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight - 5 * opts.pix);
  4174. context.lineTo(startX + 2.5 * opts.pix, startY + 0.5 * lineHeight);
  4175. context.lineTo(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight + 5 * opts.pix);
  4176. context.lineTo(startX + 12.5 * opts.pix, startY + 0.5 * lineHeight);
  4177. context.lineTo(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight - 5 * opts.pix);
  4178. break;
  4179. case 'circle':
  4180. context.moveTo(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight);
  4181. context.arc(startX + 7.5 * opts.pix, startY + 0.5 * lineHeight, 5 * opts.pix, 0, 2 * Math.PI);
  4182. break;
  4183. case 'rect':
  4184. context.moveTo(startX, startY + 0.5 * lineHeight - 5 * opts.pix);
  4185. context.fillRect(startX, startY + 0.5 * lineHeight - 5 * opts.pix, 15 * opts.pix, 10 * opts.pix);
  4186. break;
  4187. case 'square':
  4188. context.moveTo(startX + 5 * opts.pix, startY + 0.5 * lineHeight - 5 * opts.pix);
  4189. context.fillRect(startX + 5 * opts.pix, startY + 0.5 * lineHeight - 5 * opts.pix, 10 * opts.pix, 10 * opts.pix);
  4190. break;
  4191. case 'none':
  4192. break;
  4193. default:
  4194. context.moveTo(startX, startY + 0.5 * lineHeight - 5 * opts.pix);
  4195. context.fillRect(startX, startY + 0.5 * lineHeight - 5 * opts.pix, 15 * opts.pix, 10 * opts.pix);
  4196. }
  4197. context.closePath();
  4198. context.fill();
  4199. context.stroke();
  4200. startX += shapeWidth + shapeRight;
  4201. let fontTrans = 0.5 * lineHeight + 0.5 * fontSize - 2;
  4202. context.beginPath();
  4203. context.setFontSize(fontSize);
  4204. context.setFillStyle(item.show ? opts.legend.fontColor : opts.legend.hiddenColor);
  4205. context.fillText(item.name, startX, startY + fontTrans);
  4206. context.closePath();
  4207. context.stroke();
  4208. if (opts.legend.position == 'top' || opts.legend.position == 'bottom') {
  4209. startX += measureText(item.name, fontSize, context) + itemGap;
  4210. item.area[2] = startX;
  4211. } else {
  4212. item.area[2] = startX + measureText(item.name, fontSize, context) + itemGap;;
  4213. startX -= shapeWidth + shapeRight;
  4214. startY += lineHeight;
  4215. }
  4216. }
  4217. });
  4218. }
  4219. function drawPieDataPoints(series, opts, config, context) {
  4220. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4221. var pieOption = assign({}, {
  4222. activeOpacity: 0.5,
  4223. activeRadius: 10,
  4224. offsetAngle: 0,
  4225. labelWidth: 15,
  4226. ringWidth: 30,
  4227. customRadius: 0,
  4228. border: false,
  4229. borderWidth: 2,
  4230. borderColor: '#FFFFFF',
  4231. centerColor: '#FFFFFF',
  4232. linearType: 'none',
  4233. customColor: [],
  4234. }, opts.type == "pie" ? opts.extra.pie : opts.extra.ring);
  4235. var centerPosition = {
  4236. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  4237. y: opts.area[0] + (opts.height - opts.area[0] - opts.area[2]) / 2
  4238. };
  4239. if (config.pieChartLinePadding == 0) {
  4240. config.pieChartLinePadding = pieOption.activeRadius * opts.pix;
  4241. }
  4242. 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);
  4243. if (pieOption.customRadius > 0) {
  4244. radius = pieOption.customRadius * opts.pix;
  4245. }
  4246. series = getPieDataPoints(series, radius, process);
  4247. var activeRadius = pieOption.activeRadius * opts.pix;
  4248. pieOption.customColor = fillCustomColor(pieOption.linearType, pieOption.customColor, series, config);
  4249. series = series.map(function(eachSeries) {
  4250. eachSeries._start_ += (pieOption.offsetAngle) * Math.PI / 180;
  4251. return eachSeries;
  4252. });
  4253. series.forEach(function(eachSeries, seriesIndex) {
  4254. if (opts.tooltip) {
  4255. if (opts.tooltip.index == seriesIndex) {
  4256. context.beginPath();
  4257. context.setFillStyle(hexToRgb(eachSeries.color, pieOption.activeOpacity || 0.5));
  4258. context.moveTo(centerPosition.x, centerPosition.y);
  4259. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_ + activeRadius, eachSeries._start_, eachSeries._start_ + 2 * eachSeries._proportion_ * Math.PI);
  4260. context.closePath();
  4261. context.fill();
  4262. }
  4263. }
  4264. context.beginPath();
  4265. context.setLineWidth(pieOption.borderWidth * opts.pix);
  4266. context.lineJoin = "round";
  4267. context.setStrokeStyle(pieOption.borderColor);
  4268. var fillcolor = eachSeries.color;
  4269. if (pieOption.linearType == 'custom') {
  4270. var grd;
  4271. if(context.createCircularGradient){
  4272. grd = context.createCircularGradient(centerPosition.x, centerPosition.y, eachSeries._radius_)
  4273. }else{
  4274. grd = context.createRadialGradient(centerPosition.x, centerPosition.y, 0,centerPosition.x, centerPosition.y, eachSeries._radius_)
  4275. }
  4276. grd.addColorStop(0, hexToRgb(pieOption.customColor[eachSeries.linearIndex], 1))
  4277. grd.addColorStop(1, hexToRgb(eachSeries.color, 1))
  4278. fillcolor = grd
  4279. }
  4280. context.setFillStyle(fillcolor);
  4281. context.moveTo(centerPosition.x, centerPosition.y);
  4282. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_, eachSeries._start_, eachSeries._start_ + 2 * eachSeries._proportion_ * Math.PI);
  4283. context.closePath();
  4284. context.fill();
  4285. if (pieOption.border == true) {
  4286. context.stroke();
  4287. }
  4288. });
  4289. if (opts.type === 'ring') {
  4290. var innerPieWidth = radius * 0.6;
  4291. if (typeof pieOption.ringWidth === 'number' && pieOption.ringWidth > 0) {
  4292. innerPieWidth = Math.max(0, radius - pieOption.ringWidth * opts.pix);
  4293. }
  4294. context.beginPath();
  4295. context.setFillStyle(pieOption.centerColor);
  4296. context.moveTo(centerPosition.x, centerPosition.y);
  4297. context.arc(centerPosition.x, centerPosition.y, innerPieWidth, 0, 2 * Math.PI);
  4298. context.closePath();
  4299. context.fill();
  4300. }
  4301. if (opts.dataLabel !== false && process === 1) {
  4302. var valid = false;
  4303. for (var i = 0, len = series.length; i < len; i++) {
  4304. if (series[i].data > 0) {
  4305. valid = true;
  4306. break;
  4307. }
  4308. }
  4309. if (valid) {
  4310. drawPieText(series, opts, config, context, radius, centerPosition);
  4311. }
  4312. }
  4313. if (process === 1 && opts.type === 'ring') {
  4314. drawRingTitle(opts, config, context, centerPosition);
  4315. }
  4316. return {
  4317. center: centerPosition,
  4318. radius: radius,
  4319. series: series
  4320. };
  4321. }
  4322. function drawRoseDataPoints(series, opts, config, context) {
  4323. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4324. var roseOption = assign({}, {
  4325. type: 'area',
  4326. activeOpacity: 0.5,
  4327. activeRadius: 10,
  4328. offsetAngle: 0,
  4329. labelWidth: 15,
  4330. border: false,
  4331. borderWidth: 2,
  4332. borderColor: '#FFFFFF',
  4333. linearType: 'none',
  4334. customColor: [],
  4335. }, opts.extra.rose);
  4336. if (config.pieChartLinePadding == 0) {
  4337. config.pieChartLinePadding = roseOption.activeRadius * opts.pix;
  4338. }
  4339. var centerPosition = {
  4340. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  4341. y: opts.area[0] + (opts.height - opts.area[0] - opts.area[2]) / 2
  4342. };
  4343. 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);
  4344. var minRadius = roseOption.minRadius || radius * 0.5;
  4345. series = getRoseDataPoints(series, roseOption.type, minRadius, radius, process);
  4346. var activeRadius = roseOption.activeRadius * opts.pix;
  4347. roseOption.customColor = fillCustomColor(roseOption.linearType, roseOption.customColor, series, config);
  4348. series = series.map(function(eachSeries) {
  4349. eachSeries._start_ += (roseOption.offsetAngle || 0) * Math.PI / 180;
  4350. return eachSeries;
  4351. });
  4352. series.forEach(function(eachSeries, seriesIndex) {
  4353. if (opts.tooltip) {
  4354. if (opts.tooltip.index == seriesIndex) {
  4355. context.beginPath();
  4356. context.setFillStyle(hexToRgb(eachSeries.color, roseOption.activeOpacity || 0.5));
  4357. context.moveTo(centerPosition.x, centerPosition.y);
  4358. context.arc(centerPosition.x, centerPosition.y, activeRadius + eachSeries._radius_, eachSeries._start_, eachSeries._start_ + 2 * eachSeries._rose_proportion_ * Math.PI);
  4359. context.closePath();
  4360. context.fill();
  4361. }
  4362. }
  4363. context.beginPath();
  4364. context.setLineWidth(roseOption.borderWidth * opts.pix);
  4365. context.lineJoin = "round";
  4366. context.setStrokeStyle(roseOption.borderColor);
  4367. var fillcolor = eachSeries.color;
  4368. if (roseOption.linearType == 'custom') {
  4369. var grd;
  4370. if(context.createCircularGradient){
  4371. grd = context.createCircularGradient(centerPosition.x, centerPosition.y, eachSeries._radius_)
  4372. }else{
  4373. grd = context.createRadialGradient(centerPosition.x, centerPosition.y, 0,centerPosition.x, centerPosition.y, eachSeries._radius_)
  4374. }
  4375. grd.addColorStop(0, hexToRgb(roseOption.customColor[eachSeries.linearIndex], 1))
  4376. grd.addColorStop(1, hexToRgb(eachSeries.color, 1))
  4377. fillcolor = grd
  4378. }
  4379. context.setFillStyle(fillcolor);
  4380. context.moveTo(centerPosition.x, centerPosition.y);
  4381. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_, eachSeries._start_, eachSeries._start_ + 2 * eachSeries._rose_proportion_ * Math.PI);
  4382. context.closePath();
  4383. context.fill();
  4384. if (roseOption.border == true) {
  4385. context.stroke();
  4386. }
  4387. });
  4388. if (opts.dataLabel !== false && process === 1) {
  4389. var valid = false;
  4390. for (var i = 0, len = series.length; i < len; i++) {
  4391. if (series[i].data > 0) {
  4392. valid = true;
  4393. break;
  4394. }
  4395. }
  4396. if (valid) {
  4397. drawPieText(series, opts, config, context, radius, centerPosition);
  4398. }
  4399. }
  4400. return {
  4401. center: centerPosition,
  4402. radius: radius,
  4403. series: series
  4404. };
  4405. }
  4406. function drawArcbarDataPoints(series, opts, config, context) {
  4407. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4408. var arcbarOption = assign({}, {
  4409. startAngle: 0.75,
  4410. endAngle: 0.25,
  4411. type: 'default',
  4412. width: 12 ,
  4413. gap: 2 ,
  4414. linearType: 'none',
  4415. customColor: [],
  4416. }, opts.extra.arcbar);
  4417. series = getArcbarDataPoints(series, arcbarOption, process);
  4418. var centerPosition;
  4419. if (arcbarOption.centerX || arcbarOption.centerY) {
  4420. centerPosition = {
  4421. x: arcbarOption.centerX ? arcbarOption.centerX : opts.width / 2,
  4422. y: arcbarOption.centerY ? arcbarOption.centerY : opts.height / 2
  4423. };
  4424. } else {
  4425. centerPosition = {
  4426. x: opts.width / 2,
  4427. y: opts.height / 2
  4428. };
  4429. }
  4430. var radius;
  4431. if (arcbarOption.radius) {
  4432. radius = arcbarOption.radius;
  4433. } else {
  4434. radius = Math.min(centerPosition.x, centerPosition.y);
  4435. radius -= 5 * opts.pix;
  4436. radius -= arcbarOption.width / 2;
  4437. }
  4438. arcbarOption.customColor = fillCustomColor(arcbarOption.linearType, arcbarOption.customColor, series, config);
  4439. for (let i = 0; i < series.length; i++) {
  4440. let eachSeries = series[i];
  4441. //背景颜色
  4442. context.setLineWidth(arcbarOption.width * opts.pix);
  4443. context.setStrokeStyle(arcbarOption.backgroundColor || '#E9E9E9');
  4444. context.setLineCap('round');
  4445. context.beginPath();
  4446. if (arcbarOption.type == 'default') {
  4447. 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);
  4448. } else {
  4449. context.arc(centerPosition.x, centerPosition.y, radius - (arcbarOption.width * opts.pix + arcbarOption.gap * opts.pix) * i, 0, 2 * Math.PI, false);
  4450. }
  4451. context.stroke();
  4452. //进度条
  4453. var fillColor = eachSeries.color
  4454. if(arcbarOption.linearType == 'custom'){
  4455. var grd = context.createLinearGradient(centerPosition.x - radius, centerPosition.y, centerPosition.x + radius, centerPosition.y);
  4456. grd.addColorStop(1, hexToRgb(arcbarOption.customColor[eachSeries.linearIndex], 1))
  4457. grd.addColorStop(0, hexToRgb(eachSeries.color, 1))
  4458. fillColor = grd;
  4459. }
  4460. context.setLineWidth(arcbarOption.width * opts.pix);
  4461. context.setStrokeStyle(fillColor);
  4462. context.setLineCap('round');
  4463. context.beginPath();
  4464. 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);
  4465. context.stroke();
  4466. }
  4467. drawRingTitle(opts, config, context, centerPosition);
  4468. return {
  4469. center: centerPosition,
  4470. radius: radius,
  4471. series: series
  4472. };
  4473. }
  4474. function drawGaugeDataPoints(categories, series, opts, config, context) {
  4475. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  4476. var gaugeOption = assign({}, {
  4477. type: 'default',
  4478. startAngle: 0.75,
  4479. endAngle: 0.25,
  4480. width: 15,
  4481. labelOffset:13,
  4482. splitLine: {
  4483. fixRadius: 0,
  4484. splitNumber: 10,
  4485. width: 15,
  4486. color: '#FFFFFF',
  4487. childNumber: 5,
  4488. childWidth: 5
  4489. },
  4490. pointer: {
  4491. width: 15,
  4492. color: 'auto'
  4493. }
  4494. }, opts.extra.gauge);
  4495. if (gaugeOption.oldAngle == undefined) {
  4496. gaugeOption.oldAngle = gaugeOption.startAngle;
  4497. }
  4498. if (gaugeOption.oldData == undefined) {
  4499. gaugeOption.oldData = 0;
  4500. }
  4501. categories = getGaugeAxisPoints(categories, gaugeOption.startAngle, gaugeOption.endAngle);
  4502. var centerPosition = {
  4503. x: opts.width / 2,
  4504. y: opts.height / 2
  4505. };
  4506. var radius = Math.min(centerPosition.x, centerPosition.y);
  4507. radius -= 5 * opts.pix;
  4508. radius -= gaugeOption.width / 2;
  4509. var innerRadius = radius - gaugeOption.width;
  4510. var totalAngle = 0;
  4511. //判断仪表盘的样式:default百度样式,progress新样式
  4512. if (gaugeOption.type == 'progress') {
  4513. //## 第一步画中心圆形背景和进度条背景
  4514. //中心圆形背景
  4515. var pieRadius = radius - gaugeOption.width * 3;
  4516. context.beginPath();
  4517. let gradient = context.createLinearGradient(centerPosition.x, centerPosition.y - pieRadius, centerPosition.x, centerPosition.y + pieRadius);
  4518. //配置渐变填充(起点:中心点向上减半径;结束点中心点向下加半径)
  4519. gradient.addColorStop('0', hexToRgb(series[0].color, 0.3));
  4520. gradient.addColorStop('1.0', hexToRgb("#FFFFFF", 0.1));
  4521. context.setFillStyle(gradient);
  4522. context.arc(centerPosition.x, centerPosition.y, pieRadius, 0, 2 * Math.PI, false);
  4523. context.fill();
  4524. //画进度条背景
  4525. context.setLineWidth(gaugeOption.width);
  4526. context.setStrokeStyle(hexToRgb(series[0].color, 0.3));
  4527. context.setLineCap('round');
  4528. context.beginPath();
  4529. context.arc(centerPosition.x, centerPosition.y, innerRadius, gaugeOption.startAngle * Math.PI, gaugeOption.endAngle * Math.PI, false);
  4530. context.stroke();
  4531. //## 第二步画刻度线
  4532. totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  4533. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  4534. let childAngle = totalAngle / gaugeOption.splitLine.splitNumber / gaugeOption.splitLine.childNumber;
  4535. let startX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius;
  4536. let endX = -radius - gaugeOption.width - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.width;
  4537. context.save();
  4538. context.translate(centerPosition.x, centerPosition.y);
  4539. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  4540. let len = gaugeOption.splitLine.splitNumber * gaugeOption.splitLine.childNumber + 1;
  4541. let proc = series[0].data * process;
  4542. for (let i = 0; i < len; i++) {
  4543. context.beginPath();
  4544. //刻度线随进度变色
  4545. if (proc > (i / len)) {
  4546. context.setStrokeStyle(hexToRgb(series[0].color, 1));
  4547. } else {
  4548. context.setStrokeStyle(hexToRgb(series[0].color, 0.3));
  4549. }
  4550. context.setLineWidth(3 * opts.pix);
  4551. context.moveTo(startX, 0);
  4552. context.lineTo(endX, 0);
  4553. context.stroke();
  4554. context.rotate(childAngle * Math.PI);
  4555. }
  4556. context.restore();
  4557. //## 第三步画进度条
  4558. series = getArcbarDataPoints(series, gaugeOption, process);
  4559. context.setLineWidth(gaugeOption.width);
  4560. context.setStrokeStyle(series[0].color);
  4561. context.setLineCap('round');
  4562. context.beginPath();
  4563. context.arc(centerPosition.x, centerPosition.y, innerRadius, gaugeOption.startAngle * Math.PI, series[0]._proportion_ * Math.PI, false);
  4564. context.stroke();
  4565. //## 第四步画指针
  4566. let pointerRadius = radius - gaugeOption.width * 2.5;
  4567. context.save();
  4568. context.translate(centerPosition.x, centerPosition.y);
  4569. context.rotate((series[0]._proportion_ - 1) * Math.PI);
  4570. context.beginPath();
  4571. context.setLineWidth(gaugeOption.width / 3);
  4572. let gradient3 = context.createLinearGradient(0, -pointerRadius * 0.6, 0, pointerRadius * 0.6);
  4573. gradient3.addColorStop('0', hexToRgb('#FFFFFF', 0));
  4574. gradient3.addColorStop('0.5', hexToRgb(series[0].color, 1));
  4575. gradient3.addColorStop('1.0', hexToRgb('#FFFFFF', 0));
  4576. context.setStrokeStyle(gradient3);
  4577. context.arc(0, 0, pointerRadius, 0.85 * Math.PI, 1.15 * Math.PI, false);
  4578. context.stroke();
  4579. context.beginPath();
  4580. context.setLineWidth(1);
  4581. context.setStrokeStyle(series[0].color);
  4582. context.setFillStyle(series[0].color);
  4583. context.moveTo(-pointerRadius - gaugeOption.width / 3 / 2, -4);
  4584. context.lineTo(-pointerRadius - gaugeOption.width / 3 / 2 - 4, 0);
  4585. context.lineTo(-pointerRadius - gaugeOption.width / 3 / 2, 4);
  4586. context.lineTo(-pointerRadius - gaugeOption.width / 3 / 2, -4);
  4587. context.stroke();
  4588. context.fill();
  4589. context.restore();
  4590. //default百度样式
  4591. } else {
  4592. //画背景
  4593. context.setLineWidth(gaugeOption.width);
  4594. context.setLineCap('butt');
  4595. for (let i = 0; i < categories.length; i++) {
  4596. let eachCategories = categories[i];
  4597. context.beginPath();
  4598. context.setStrokeStyle(eachCategories.color);
  4599. context.arc(centerPosition.x, centerPosition.y, radius, eachCategories._startAngle_ * Math.PI, eachCategories._endAngle_ * Math.PI, false);
  4600. context.stroke();
  4601. }
  4602. context.save();
  4603. //画刻度线
  4604. totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  4605. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  4606. let childAngle = totalAngle / gaugeOption.splitLine.splitNumber / gaugeOption.splitLine.childNumber;
  4607. let startX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius;
  4608. let endX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.width;
  4609. let childendX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.childWidth;
  4610. context.translate(centerPosition.x, centerPosition.y);
  4611. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  4612. for (let i = 0; i < gaugeOption.splitLine.splitNumber + 1; i++) {
  4613. context.beginPath();
  4614. context.setStrokeStyle(gaugeOption.splitLine.color);
  4615. context.setLineWidth(2 * opts.pix);
  4616. context.moveTo(startX, 0);
  4617. context.lineTo(endX, 0);
  4618. context.stroke();
  4619. context.rotate(splitAngle * Math.PI);
  4620. }
  4621. context.restore();
  4622. context.save();
  4623. context.translate(centerPosition.x, centerPosition.y);
  4624. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  4625. for (let i = 0; i < gaugeOption.splitLine.splitNumber * gaugeOption.splitLine.childNumber + 1; i++) {
  4626. context.beginPath();
  4627. context.setStrokeStyle(gaugeOption.splitLine.color);
  4628. context.setLineWidth(1 * opts.pix);
  4629. context.moveTo(startX, 0);
  4630. context.lineTo(childendX, 0);
  4631. context.stroke();
  4632. context.rotate(childAngle * Math.PI);
  4633. }
  4634. context.restore();
  4635. //画指针
  4636. series = getGaugeDataPoints(series, categories, gaugeOption, process);
  4637. for (let i = 0; i < series.length; i++) {
  4638. let eachSeries = series[i];
  4639. context.save();
  4640. context.translate(centerPosition.x, centerPosition.y);
  4641. context.rotate((eachSeries._proportion_ - 1) * Math.PI);
  4642. context.beginPath();
  4643. context.setFillStyle(eachSeries.color);
  4644. context.moveTo(gaugeOption.pointer.width, 0);
  4645. context.lineTo(0, -gaugeOption.pointer.width / 2);
  4646. context.lineTo(-innerRadius, 0);
  4647. context.lineTo(0, gaugeOption.pointer.width / 2);
  4648. context.lineTo(gaugeOption.pointer.width, 0);
  4649. context.closePath();
  4650. context.fill();
  4651. context.beginPath();
  4652. context.setFillStyle('#FFFFFF');
  4653. context.arc(0, 0, gaugeOption.pointer.width / 6, 0, 2 * Math.PI, false);
  4654. context.fill();
  4655. context.restore();
  4656. }
  4657. if (opts.dataLabel !== false) {
  4658. drawGaugeLabel(gaugeOption, radius, centerPosition, opts, config, context);
  4659. }
  4660. }
  4661. //画仪表盘标题,副标题
  4662. drawRingTitle(opts, config, context, centerPosition);
  4663. if (process === 1 && opts.type === 'gauge') {
  4664. opts.extra.gauge.oldAngle = series[0]._proportion_;
  4665. opts.extra.gauge.oldData = series[0].data;
  4666. }
  4667. return {
  4668. center: centerPosition,
  4669. radius: radius,
  4670. innerRadius: innerRadius,
  4671. categories: categories,
  4672. totalAngle: totalAngle
  4673. };
  4674. }
  4675. function drawRadarDataPoints(series, opts, config, context) {
  4676. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4677. var radarOption = assign({}, {
  4678. gridColor: '#cccccc',
  4679. gridType: 'radar',
  4680. opacity: 0.2,
  4681. gridCount: 3,
  4682. border:false,
  4683. borderWidth:2
  4684. }, opts.extra.radar);
  4685. var coordinateAngle = getRadarCoordinateSeries(opts.categories.length);
  4686. var centerPosition = {
  4687. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  4688. y: opts.area[0] + (opts.height - opts.area[0] - opts.area[2]) / 2
  4689. };
  4690. var xr = (opts.width - opts.area[1] - opts.area[3]) / 2
  4691. var yr = (opts.height - opts.area[0] - opts.area[2]) / 2
  4692. var radius = Math.min(xr - (getMaxTextListLength(opts.categories, config.fontSize, context) + config.radarLabelTextMargin), yr - config.radarLabelTextMargin);
  4693. radius -= config.radarLabelTextMargin * opts.pix;
  4694. // 画分割线
  4695. context.beginPath();
  4696. context.setLineWidth(1 * opts.pix);
  4697. context.setStrokeStyle(radarOption.gridColor);
  4698. coordinateAngle.forEach(function(angle) {
  4699. var pos = convertCoordinateOrigin(radius * Math.cos(angle), radius * Math.sin(angle), centerPosition);
  4700. context.moveTo(centerPosition.x, centerPosition.y);
  4701. context.lineTo(pos.x, pos.y);
  4702. });
  4703. context.stroke();
  4704. context.closePath();
  4705. // 画背景网格
  4706. var _loop = function _loop(i) {
  4707. var startPos = {};
  4708. context.beginPath();
  4709. context.setLineWidth(1 * opts.pix);
  4710. context.setStrokeStyle(radarOption.gridColor);
  4711. if (radarOption.gridType == 'radar') {
  4712. coordinateAngle.forEach(function(angle, index) {
  4713. var pos = convertCoordinateOrigin(radius / radarOption.gridCount * i * Math.cos(angle), radius /
  4714. radarOption.gridCount * i * Math.sin(angle), centerPosition);
  4715. if (index === 0) {
  4716. startPos = pos;
  4717. context.moveTo(pos.x, pos.y);
  4718. } else {
  4719. context.lineTo(pos.x, pos.y);
  4720. }
  4721. });
  4722. context.lineTo(startPos.x, startPos.y);
  4723. } else {
  4724. var pos = convertCoordinateOrigin(radius / radarOption.gridCount * i * Math.cos(1.5), radius / radarOption.gridCount * i * Math.sin(1.5), centerPosition);
  4725. context.arc(centerPosition.x, centerPosition.y, centerPosition.y - pos.y, 0, 2 * Math.PI, false);
  4726. }
  4727. context.stroke();
  4728. context.closePath();
  4729. };
  4730. for (var i = 1; i <= radarOption.gridCount; i++) {
  4731. _loop(i);
  4732. }
  4733. var radarDataPoints = getRadarDataPoints(coordinateAngle, centerPosition, radius, series, opts, process);
  4734. radarDataPoints.forEach(function(eachSeries, seriesIndex) {
  4735. // 绘制区域数据
  4736. context.beginPath();
  4737. context.setLineWidth(radarOption.borderWidth * opts.pix);
  4738. context.setStrokeStyle(eachSeries.color);
  4739. context.setFillStyle(hexToRgb(eachSeries.color, radarOption.opacity));
  4740. eachSeries.data.forEach(function(item, index) {
  4741. if (index === 0) {
  4742. context.moveTo(item.position.x, item.position.y);
  4743. } else {
  4744. context.lineTo(item.position.x, item.position.y);
  4745. }
  4746. });
  4747. context.closePath();
  4748. context.fill();
  4749. if(radarOption.border === true){
  4750. context.stroke();
  4751. }
  4752. context.closePath();
  4753. if (opts.dataPointShape !== false) {
  4754. var points = eachSeries.data.map(function(item) {
  4755. return item.position;
  4756. });
  4757. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  4758. }
  4759. });
  4760. // draw label text
  4761. drawRadarLabel(coordinateAngle, radius, centerPosition, opts, config, context);
  4762. // draw dataLabel
  4763. if (opts.dataLabel !== false && process === 1) {
  4764. radarDataPoints.forEach(function(eachSeries, seriesIndex) {
  4765. context.beginPath();
  4766. var fontSize = eachSeries.textSize * opts.pix || config.fontSize;
  4767. context.setFontSize(fontSize);
  4768. context.setFillStyle(eachSeries.textColor || opts.fontColor);
  4769. eachSeries.data.forEach(function(item, index) {
  4770. //如果是中心点垂直的上下点位
  4771. if(Math.abs(item.position.x - centerPosition.x)<2){
  4772. //如果在上面
  4773. if(item.position.y < centerPosition.y){
  4774. context.setTextAlign('center');
  4775. context.fillText(item.value, item.position.x, item.position.y - 4);
  4776. }else{
  4777. context.setTextAlign('center');
  4778. context.fillText(item.value, item.position.x, item.position.y + fontSize + 2);
  4779. }
  4780. }else{
  4781. //如果在左侧
  4782. if(item.position.x < centerPosition.x){
  4783. context.setTextAlign('right');
  4784. context.fillText(item.value, item.position.x - 4, item.position.y + fontSize / 2 - 2);
  4785. }else{
  4786. context.setTextAlign('left');
  4787. context.fillText(item.value, item.position.x + 4, item.position.y + fontSize / 2 - 2);
  4788. }
  4789. }
  4790. });
  4791. context.closePath();
  4792. context.stroke();
  4793. });
  4794. context.setTextAlign('left');
  4795. }
  4796. return {
  4797. center: centerPosition,
  4798. radius: radius,
  4799. angleList: coordinateAngle
  4800. };
  4801. }
  4802. function normalInt(min, max, iter) {
  4803. iter = iter == 0 ? 1 : iter;
  4804. var arr = [];
  4805. for (var i = 0; i < iter; i++) {
  4806. arr[i] = Math.random();
  4807. };
  4808. return Math.floor(arr.reduce(function(i, j) {
  4809. return i + j
  4810. }) / iter * (max - min)) + min;
  4811. };
  4812. function collisionNew(area, points, width, height) {
  4813. var isIn = false;
  4814. for (let i = 0; i < points.length; i++) {
  4815. if (points[i].area) {
  4816. 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]) {
  4817. if (area[0] < 0 || area[1] < 0 || area[2] > width || area[3] > height) {
  4818. isIn = true;
  4819. break;
  4820. } else {
  4821. isIn = false;
  4822. }
  4823. } else {
  4824. isIn = true;
  4825. break;
  4826. }
  4827. }
  4828. }
  4829. return isIn;
  4830. };
  4831. function getBoundingBox(data) {
  4832. var bounds = {},coords;
  4833. bounds.xMin = 180;
  4834. bounds.xMax = 0;
  4835. bounds.yMin = 90;
  4836. bounds.yMax = 0
  4837. for (var i = 0; i < data.length; i++) {
  4838. var coorda = data[i].geometry.coordinates
  4839. for (var k = 0; k < coorda.length; k++) {
  4840. coords = coorda[k];
  4841. if (coords.length == 1) {
  4842. coords = coords[0]
  4843. }
  4844. for (var j = 0; j < coords.length; j++) {
  4845. var longitude = coords[j][0];
  4846. var latitude = coords[j][1];
  4847. var point = {
  4848. x: longitude,
  4849. y: latitude
  4850. }
  4851. bounds.xMin = bounds.xMin < point.x ? bounds.xMin : point.x;
  4852. bounds.xMax = bounds.xMax > point.x ? bounds.xMax : point.x;
  4853. bounds.yMin = bounds.yMin < point.y ? bounds.yMin : point.y;
  4854. bounds.yMax = bounds.yMax > point.y ? bounds.yMax : point.y;
  4855. }
  4856. }
  4857. }
  4858. return bounds;
  4859. }
  4860. function coordinateToPoint(latitude, longitude, bounds, scale, xoffset, yoffset) {
  4861. return {
  4862. x: (longitude - bounds.xMin) * scale + xoffset,
  4863. y: (bounds.yMax - latitude) * scale + yoffset
  4864. };
  4865. }
  4866. function pointToCoordinate(pointY, pointX, bounds, scale, xoffset, yoffset) {
  4867. return {
  4868. x: (pointX - xoffset) / scale + bounds.xMin,
  4869. y: bounds.yMax - (pointY - yoffset) / scale
  4870. };
  4871. }
  4872. function isRayIntersectsSegment(poi, s_poi, e_poi) {
  4873. if (s_poi[1] == e_poi[1]) {
  4874. return false;
  4875. }
  4876. if (s_poi[1] > poi[1] && e_poi[1] > poi[1]) {
  4877. return false;
  4878. }
  4879. if (s_poi[1] < poi[1] && e_poi[1] < poi[1]) {
  4880. return false;
  4881. }
  4882. if (s_poi[1] == poi[1] && e_poi[1] > poi[1]) {
  4883. return false;
  4884. }
  4885. if (e_poi[1] == poi[1] && s_poi[1] > poi[1]) {
  4886. return false;
  4887. }
  4888. if (s_poi[0] < poi[0] && e_poi[1] < poi[1]) {
  4889. return false;
  4890. }
  4891. let xseg = e_poi[0] - (e_poi[0] - s_poi[0]) * (e_poi[1] - poi[1]) / (e_poi[1] - s_poi[1]);
  4892. if (xseg < poi[0]) {
  4893. return false;
  4894. } else {
  4895. return true;
  4896. }
  4897. }
  4898. function isPoiWithinPoly(poi, poly, mercator) {
  4899. let sinsc = 0;
  4900. for (let i = 0; i < poly.length; i++) {
  4901. let epoly = poly[i][0];
  4902. if (poly.length == 1) {
  4903. epoly = poly[i][0]
  4904. }
  4905. for (let j = 0; j < epoly.length - 1; j++) {
  4906. let s_poi = epoly[j];
  4907. let e_poi = epoly[j + 1];
  4908. if (mercator) {
  4909. s_poi = lonlat2mercator(epoly[j][0], epoly[j][1]);
  4910. e_poi = lonlat2mercator(epoly[j + 1][0], epoly[j + 1][1]);
  4911. }
  4912. if (isRayIntersectsSegment(poi, s_poi, e_poi)) {
  4913. sinsc += 1;
  4914. }
  4915. }
  4916. }
  4917. if (sinsc % 2 == 1) {
  4918. return true;
  4919. } else {
  4920. return false;
  4921. }
  4922. }
  4923. function drawMapDataPoints(series, opts, config, context) {
  4924. var mapOption = assign({}, {
  4925. border: true,
  4926. mercator: false,
  4927. borderWidth: 1,
  4928. borderColor: '#666666',
  4929. fillOpacity: 0.6,
  4930. activeBorderColor: '#f04864',
  4931. activeFillColor: '#facc14',
  4932. activeFillOpacity: 1
  4933. }, opts.extra.map);
  4934. var coords, point;
  4935. var data = series;
  4936. var bounds = getBoundingBox(data);
  4937. if (mapOption.mercator) {
  4938. var max = lonlat2mercator(bounds.xMax, bounds.yMax)
  4939. var min = lonlat2mercator(bounds.xMin, bounds.yMin)
  4940. bounds.xMax = max[0]
  4941. bounds.yMax = max[1]
  4942. bounds.xMin = min[0]
  4943. bounds.yMin = min[1]
  4944. }
  4945. var xScale = opts.width / Math.abs(bounds.xMax - bounds.xMin);
  4946. var yScale = opts.height / Math.abs(bounds.yMax - bounds.yMin);
  4947. var scale = xScale < yScale ? xScale : yScale;
  4948. var xoffset = opts.width / 2 - Math.abs(bounds.xMax - bounds.xMin) / 2 * scale;
  4949. var yoffset = opts.height / 2 - Math.abs(bounds.yMax - bounds.yMin) / 2 * scale;
  4950. for (var i = 0; i < data.length; i++) {
  4951. context.beginPath();
  4952. context.setLineWidth(mapOption.borderWidth * opts.pix);
  4953. context.setStrokeStyle(mapOption.borderColor);
  4954. context.setFillStyle(hexToRgb(series[i].color, mapOption.fillOpacity));
  4955. if (opts.tooltip) {
  4956. if (opts.tooltip.index == i) {
  4957. context.setStrokeStyle(mapOption.activeBorderColor);
  4958. context.setFillStyle(hexToRgb(mapOption.activeFillColor, mapOption.activeFillOpacity));
  4959. }
  4960. }
  4961. var coorda = data[i].geometry.coordinates
  4962. for (var k = 0; k < coorda.length; k++) {
  4963. coords = coorda[k];
  4964. if (coords.length == 1) {
  4965. coords = coords[0]
  4966. }
  4967. for (var j = 0; j < coords.length; j++) {
  4968. var gaosi = Array(2);
  4969. if (mapOption.mercator) {
  4970. gaosi = lonlat2mercator(coords[j][0], coords[j][1])
  4971. } else {
  4972. gaosi = coords[j]
  4973. }
  4974. point = coordinateToPoint(gaosi[1], gaosi[0], bounds, scale, xoffset, yoffset)
  4975. if (j === 0) {
  4976. context.beginPath();
  4977. context.moveTo(point.x, point.y);
  4978. } else {
  4979. context.lineTo(point.x, point.y);
  4980. }
  4981. }
  4982. context.fill();
  4983. if (mapOption.border == true) {
  4984. context.stroke();
  4985. }
  4986. }
  4987. if (opts.dataLabel == true) {
  4988. var centerPoint = data[i].properties.centroid;
  4989. if (centerPoint) {
  4990. if (mapOption.mercator) {
  4991. centerPoint = lonlat2mercator(data[i].properties.centroid[0], data[i].properties.centroid[1])
  4992. }
  4993. point = coordinateToPoint(centerPoint[1], centerPoint[0], bounds, scale, xoffset, yoffset);
  4994. let fontSize = data[i].textSize * opts.pix || config.fontSize;
  4995. let text = data[i].properties.name;
  4996. context.beginPath();
  4997. context.setFontSize(fontSize)
  4998. context.setFillStyle(data[i].textColor || opts.fontColor)
  4999. context.fillText(text, point.x - measureText(text, fontSize, context) / 2, point.y + fontSize / 2);
  5000. context.closePath();
  5001. context.stroke();
  5002. }
  5003. }
  5004. }
  5005. opts.chartData.mapData = {
  5006. bounds: bounds,
  5007. scale: scale,
  5008. xoffset: xoffset,
  5009. yoffset: yoffset,
  5010. mercator: mapOption.mercator
  5011. }
  5012. drawToolTipBridge(opts, config, context, 1);
  5013. context.draw();
  5014. }
  5015. function getWordCloudPoint(opts, type, context) {
  5016. let points = opts.series;
  5017. switch (type) {
  5018. case 'normal':
  5019. for (let i = 0; i < points.length; i++) {
  5020. let text = points[i].name;
  5021. let tHeight = points[i].textSize * opts.pix;
  5022. let tWidth = measureText(text, tHeight, context);
  5023. let x, y;
  5024. let area;
  5025. let breaknum = 0;
  5026. while (true) {
  5027. breaknum++;
  5028. x = normalInt(-opts.width / 2, opts.width / 2, 5) - tWidth / 2;
  5029. y = normalInt(-opts.height / 2, opts.height / 2, 5) + tHeight / 2;
  5030. area = [x - 5 + opts.width / 2, y - 5 - tHeight + opts.height / 2, x + tWidth + 5 + opts.width / 2, y + 5 +
  5031. opts.height / 2
  5032. ];
  5033. let isCollision = collisionNew(area, points, opts.width, opts.height);
  5034. if (!isCollision) break;
  5035. if (breaknum == 1000) {
  5036. area = [-100, -100, -100, -100];
  5037. break;
  5038. }
  5039. };
  5040. points[i].area = area;
  5041. }
  5042. break;
  5043. case 'vertical':
  5044. function Spin() {
  5045. //获取均匀随机值,是否旋转,旋转的概率为(1-0.5)
  5046. if (Math.random() > 0.7) {
  5047. return true;
  5048. } else {
  5049. return false
  5050. };
  5051. };
  5052. for (let i = 0; i < points.length; i++) {
  5053. let text = points[i].name;
  5054. let tHeight = points[i].textSize * opts.pix;
  5055. let tWidth = measureText(text, tHeight, context);
  5056. let isSpin = Spin();
  5057. let x, y, area, areav;
  5058. let breaknum = 0;
  5059. while (true) {
  5060. breaknum++;
  5061. let isCollision;
  5062. if (isSpin) {
  5063. x = normalInt(-opts.width / 2, opts.width / 2, 5) - tWidth / 2;
  5064. y = normalInt(-opts.height / 2, opts.height / 2, 5) + tHeight / 2;
  5065. area = [y - 5 - tWidth + opts.width / 2, (-x - 5 + opts.height / 2), y + 5 + opts.width / 2, (-x + tHeight + 5 + opts.height / 2)];
  5066. 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];
  5067. isCollision = collisionNew(areav, points, opts.height, opts.width);
  5068. } else {
  5069. x = normalInt(-opts.width / 2, opts.width / 2, 5) - tWidth / 2;
  5070. y = normalInt(-opts.height / 2, opts.height / 2, 5) + tHeight / 2;
  5071. area = [x - 5 + opts.width / 2, y - 5 - tHeight + opts.height / 2, x + tWidth + 5 + opts.width / 2, y + 5 + opts.height / 2];
  5072. isCollision = collisionNew(area, points, opts.width, opts.height);
  5073. }
  5074. if (!isCollision) break;
  5075. if (breaknum == 1000) {
  5076. area = [-1000, -1000, -1000, -1000];
  5077. break;
  5078. }
  5079. };
  5080. if (isSpin) {
  5081. points[i].area = areav;
  5082. points[i].areav = area;
  5083. } else {
  5084. points[i].area = area;
  5085. }
  5086. points[i].rotate = isSpin;
  5087. };
  5088. break;
  5089. }
  5090. return points;
  5091. }
  5092. function drawWordCloudDataPoints(series, opts, config, context) {
  5093. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  5094. let wordOption = assign({}, {
  5095. type: 'normal',
  5096. autoColors: true
  5097. }, opts.extra.word);
  5098. if (!opts.chartData.wordCloudData) {
  5099. opts.chartData.wordCloudData = getWordCloudPoint(opts, wordOption.type, context);
  5100. }
  5101. context.beginPath();
  5102. context.setFillStyle(opts.background);
  5103. context.rect(0, 0, opts.width, opts.height);
  5104. context.fill();
  5105. context.save();
  5106. let points = opts.chartData.wordCloudData;
  5107. context.translate(opts.width / 2, opts.height / 2);
  5108. for (let i = 0; i < points.length; i++) {
  5109. context.save();
  5110. if (points[i].rotate) {
  5111. context.rotate(90 * Math.PI / 180);
  5112. }
  5113. let text = points[i].name;
  5114. let tHeight = points[i].textSize * opts.pix;
  5115. let tWidth = measureText(text, tHeight, context);
  5116. context.beginPath();
  5117. context.setStrokeStyle(points[i].color);
  5118. context.setFillStyle(points[i].color);
  5119. context.setFontSize(tHeight);
  5120. if (points[i].rotate) {
  5121. if (points[i].areav[0] > 0) {
  5122. if (opts.tooltip) {
  5123. if (opts.tooltip.index == i) {
  5124. 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);
  5125. } else {
  5126. 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);
  5127. }
  5128. } else {
  5129. 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);
  5130. }
  5131. }
  5132. } else {
  5133. if (points[i].area[0] > 0) {
  5134. if (opts.tooltip) {
  5135. if (opts.tooltip.index == i) {
  5136. 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);
  5137. } else {
  5138. 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);
  5139. }
  5140. } else {
  5141. 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);
  5142. }
  5143. }
  5144. }
  5145. context.stroke();
  5146. context.restore();
  5147. }
  5148. context.restore();
  5149. }
  5150. function drawFunnelDataPoints(series, opts, config, context) {
  5151. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  5152. let funnelOption = assign({}, {
  5153. type:'funnel',
  5154. activeWidth: 10,
  5155. activeOpacity: 0.3,
  5156. border: false,
  5157. borderWidth: 2,
  5158. borderColor: '#FFFFFF',
  5159. fillOpacity: 1,
  5160. labelAlign: 'right',
  5161. linearType: 'none',
  5162. customColor: [],
  5163. }, opts.extra.funnel);
  5164. let eachSpacing = (opts.height - opts.area[0] - opts.area[2]) / series.length;
  5165. let centerPosition = {
  5166. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  5167. y: opts.height - opts.area[2]
  5168. };
  5169. let activeWidth = funnelOption.activeWidth * opts.pix;
  5170. let radius = Math.min((opts.width - opts.area[1] - opts.area[3]) / 2 - activeWidth, (opts.height - opts.area[0] - opts.area[2]) / 2 - activeWidth);
  5171. series = getFunnelDataPoints(series, radius, funnelOption.type, eachSpacing, process);
  5172. context.save();
  5173. context.translate(centerPosition.x, centerPosition.y);
  5174. funnelOption.customColor = fillCustomColor(funnelOption.linearType, funnelOption.customColor, series, config);
  5175. if(funnelOption.type == 'pyramid'){
  5176. for (let i = 0; i < series.length; i++) {
  5177. if (i == series.length -1) {
  5178. if (opts.tooltip) {
  5179. if (opts.tooltip.index == i) {
  5180. context.beginPath();
  5181. context.setFillStyle(hexToRgb(series[i].color, funnelOption.activeOpacity));
  5182. context.moveTo(-activeWidth, -eachSpacing);
  5183. context.lineTo(-series[i].radius - activeWidth, 0);
  5184. context.lineTo(series[i].radius + activeWidth, 0);
  5185. context.lineTo(activeWidth, -eachSpacing);
  5186. context.lineTo(-activeWidth, -eachSpacing);
  5187. context.closePath();
  5188. context.fill();
  5189. }
  5190. }
  5191. series[i].funnelArea = [centerPosition.x - series[i].radius, centerPosition.y - eachSpacing * (i + 1), centerPosition.x + series[i].radius, centerPosition.y - eachSpacing * i];
  5192. context.beginPath();
  5193. context.setLineWidth(funnelOption.borderWidth * opts.pix);
  5194. context.setStrokeStyle(funnelOption.borderColor);
  5195. var fillColor = hexToRgb(series[i].color, funnelOption.fillOpacity);
  5196. if (funnelOption.linearType == 'custom') {
  5197. var grd = context.createLinearGradient(series[i].radius, -eachSpacing, -series[i].radius, -eachSpacing);
  5198. grd.addColorStop(0, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5199. grd.addColorStop(0.5, hexToRgb(funnelOption.customColor[series[i].linearIndex], funnelOption.fillOpacity));
  5200. grd.addColorStop(1, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5201. fillColor = grd
  5202. }
  5203. context.setFillStyle(fillColor);
  5204. context.moveTo(0, -eachSpacing);
  5205. context.lineTo(-series[i].radius, 0);
  5206. context.lineTo(series[i].radius, 0);
  5207. context.lineTo(0, -eachSpacing);
  5208. context.closePath();
  5209. context.fill();
  5210. if (funnelOption.border == true) {
  5211. context.stroke();
  5212. }
  5213. } else {
  5214. if (opts.tooltip) {
  5215. if (opts.tooltip.index == i) {
  5216. context.beginPath();
  5217. context.setFillStyle(hexToRgb(series[i].color, funnelOption.activeOpacity));
  5218. context.moveTo(0, 0);
  5219. context.lineTo(-series[i].radius - activeWidth, 0);
  5220. context.lineTo(-series[i + 1].radius - activeWidth, -eachSpacing);
  5221. context.lineTo(series[i + 1].radius + activeWidth, -eachSpacing);
  5222. context.lineTo(series[i].radius + activeWidth, 0);
  5223. context.lineTo(0, 0);
  5224. context.closePath();
  5225. context.fill();
  5226. }
  5227. }
  5228. series[i].funnelArea = [centerPosition.x - series[i].radius, centerPosition.y - eachSpacing * (i + 1), centerPosition.x + series[i].radius, centerPosition.y - eachSpacing * i];
  5229. context.beginPath();
  5230. context.setLineWidth(funnelOption.borderWidth * opts.pix);
  5231. context.setStrokeStyle(funnelOption.borderColor);
  5232. var fillColor = hexToRgb(series[i].color, funnelOption.fillOpacity);
  5233. if (funnelOption.linearType == 'custom') {
  5234. var grd = context.createLinearGradient(series[i].radius, -eachSpacing, -series[i].radius, -eachSpacing);
  5235. grd.addColorStop(0, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5236. grd.addColorStop(0.5, hexToRgb(funnelOption.customColor[series[i].linearIndex], funnelOption.fillOpacity));
  5237. grd.addColorStop(1, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5238. fillColor = grd
  5239. }
  5240. context.setFillStyle(fillColor);
  5241. context.moveTo(0, 0);
  5242. context.lineTo(-series[i].radius, 0);
  5243. context.lineTo(-series[i + 1].radius, -eachSpacing);
  5244. context.lineTo(series[i + 1].radius, -eachSpacing);
  5245. context.lineTo(series[i].radius, 0);
  5246. context.lineTo(0, 0);
  5247. context.closePath();
  5248. context.fill();
  5249. if (funnelOption.border == true) {
  5250. context.stroke();
  5251. }
  5252. }
  5253. context.translate(0, -eachSpacing)
  5254. }
  5255. }else{
  5256. for (let i = 0; i < series.length; i++) {
  5257. if (i == 0) {
  5258. if (opts.tooltip) {
  5259. if (opts.tooltip.index == i) {
  5260. context.beginPath();
  5261. context.setFillStyle(hexToRgb(series[i].color, funnelOption.activeOpacity));
  5262. context.moveTo(-activeWidth, 0);
  5263. context.lineTo(-series[i].radius - activeWidth, -eachSpacing);
  5264. context.lineTo(series[i].radius + activeWidth, -eachSpacing);
  5265. context.lineTo(activeWidth, 0);
  5266. context.lineTo(-activeWidth, 0);
  5267. context.closePath();
  5268. context.fill();
  5269. }
  5270. }
  5271. series[i].funnelArea = [centerPosition.x - series[i].radius, centerPosition.y - eachSpacing, centerPosition.x + series[i].radius, centerPosition.y];
  5272. context.beginPath();
  5273. context.setLineWidth(funnelOption.borderWidth * opts.pix);
  5274. context.setStrokeStyle(funnelOption.borderColor);
  5275. var fillColor = hexToRgb(series[i].color, funnelOption.fillOpacity);
  5276. if (funnelOption.linearType == 'custom') {
  5277. var grd = context.createLinearGradient(series[i].radius, -eachSpacing, -series[i].radius, -eachSpacing);
  5278. grd.addColorStop(0, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5279. grd.addColorStop(0.5, hexToRgb(funnelOption.customColor[series[i].linearIndex], funnelOption.fillOpacity));
  5280. grd.addColorStop(1, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5281. fillColor = grd
  5282. }
  5283. context.setFillStyle(fillColor);
  5284. context.moveTo(0, 0);
  5285. context.lineTo(-series[i].radius, -eachSpacing);
  5286. context.lineTo(series[i].radius, -eachSpacing);
  5287. context.lineTo(0, 0);
  5288. context.closePath();
  5289. context.fill();
  5290. if (funnelOption.border == true) {
  5291. context.stroke();
  5292. }
  5293. } else {
  5294. if (opts.tooltip) {
  5295. if (opts.tooltip.index == i) {
  5296. context.beginPath();
  5297. context.setFillStyle(hexToRgb(series[i].color, funnelOption.activeOpacity));
  5298. context.moveTo(0, 0);
  5299. context.lineTo(-series[i - 1].radius - activeWidth, 0);
  5300. context.lineTo(-series[i].radius - activeWidth, -eachSpacing);
  5301. context.lineTo(series[i].radius + activeWidth, -eachSpacing);
  5302. context.lineTo(series[i - 1].radius + activeWidth, 0);
  5303. context.lineTo(0, 0);
  5304. context.closePath();
  5305. context.fill();
  5306. }
  5307. }
  5308. series[i].funnelArea = [centerPosition.x - series[i].radius, centerPosition.y - eachSpacing * (i + 1), centerPosition.x + series[i].radius, centerPosition.y - eachSpacing * i];
  5309. context.beginPath();
  5310. context.setLineWidth(funnelOption.borderWidth * opts.pix);
  5311. context.setStrokeStyle(funnelOption.borderColor);
  5312. var fillColor = hexToRgb(series[i].color, funnelOption.fillOpacity);
  5313. if (funnelOption.linearType == 'custom') {
  5314. var grd = context.createLinearGradient(series[i].radius, -eachSpacing, -series[i].radius, -eachSpacing);
  5315. grd.addColorStop(0, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5316. grd.addColorStop(0.5, hexToRgb(funnelOption.customColor[series[i].linearIndex], funnelOption.fillOpacity));
  5317. grd.addColorStop(1, hexToRgb(series[i].color, funnelOption.fillOpacity));
  5318. fillColor = grd
  5319. }
  5320. context.setFillStyle(fillColor);
  5321. context.moveTo(0, 0);
  5322. context.lineTo(-series[i - 1].radius, 0);
  5323. context.lineTo(-series[i].radius, -eachSpacing);
  5324. context.lineTo(series[i].radius, -eachSpacing);
  5325. context.lineTo(series[i - 1].radius, 0);
  5326. context.lineTo(0, 0);
  5327. context.closePath();
  5328. context.fill();
  5329. if (funnelOption.border == true) {
  5330. context.stroke();
  5331. }
  5332. }
  5333. context.translate(0, -eachSpacing)
  5334. }
  5335. }
  5336. context.restore();
  5337. if (opts.dataLabel !== false && process === 1) {
  5338. drawFunnelText(series, opts, context, eachSpacing, funnelOption.labelAlign, activeWidth, centerPosition);
  5339. }
  5340. return {
  5341. center: centerPosition,
  5342. radius: radius,
  5343. series: series
  5344. };
  5345. }
  5346. function drawFunnelText(series, opts, context, eachSpacing, labelAlign, activeWidth, centerPosition) {
  5347. for (let i = 0; i < series.length; i++) {
  5348. let item = series[i];
  5349. let startX, endX, startY, fontSize;
  5350. let text = item.formatter ? item.formatter(item,i,series) : util.toFixed(item._proportion_ * 100) + '%';
  5351. if (labelAlign == 'right') {
  5352. if(opts.extra.funnel.type === 'pyramid'){
  5353. if (i == series.length -1) {
  5354. startX = (item.funnelArea[2] + centerPosition.x) / 2;
  5355. } else {
  5356. startX = (item.funnelArea[2] + series[i + 1].funnelArea[2]) / 2;
  5357. }
  5358. }else{
  5359. if (i == 0) {
  5360. startX = (item.funnelArea[2] + centerPosition.x) / 2;
  5361. } else {
  5362. startX = (item.funnelArea[2] + series[i - 1].funnelArea[2]) / 2;
  5363. }
  5364. }
  5365. endX = startX + activeWidth * 2;
  5366. startY = item.funnelArea[1] + eachSpacing / 2;
  5367. fontSize = item.textSize * opts.pix || opts.fontSize * opts.pix;
  5368. context.setLineWidth(1 * opts.pix);
  5369. context.setStrokeStyle(item.color);
  5370. context.setFillStyle(item.color);
  5371. context.beginPath();
  5372. context.moveTo(startX, startY);
  5373. context.lineTo(endX, startY);
  5374. context.stroke();
  5375. context.closePath();
  5376. context.beginPath();
  5377. context.moveTo(endX, startY);
  5378. context.arc(endX, startY, 2, 0, 2 * Math.PI);
  5379. context.closePath();
  5380. context.fill();
  5381. context.beginPath();
  5382. context.setFontSize(fontSize);
  5383. context.setFillStyle(item.textColor || opts.fontColor);
  5384. context.fillText(text, endX + 5, startY + fontSize / 2 - 2);
  5385. context.closePath();
  5386. context.stroke();
  5387. context.closePath();
  5388. } else {
  5389. if(opts.extra.funnel.type === 'pyramid'){
  5390. if (i == series.length -1) {
  5391. startX = (item.funnelArea[0] + centerPosition.x) / 2;
  5392. } else {
  5393. startX = (item.funnelArea[0] + series[i + 1].funnelArea[0]) / 2;
  5394. }
  5395. }else{
  5396. if (i == 0) {
  5397. startX = (item.funnelArea[0] + centerPosition.x) / 2;
  5398. } else {
  5399. startX = (item.funnelArea[0] + series[i - 1].funnelArea[0]) / 2;
  5400. }
  5401. }
  5402. endX = startX - activeWidth * 2;
  5403. startY = item.funnelArea[1] + eachSpacing / 2;
  5404. fontSize = item.textSize * opts.pix || opts.fontSize * opts.pix;
  5405. context.setLineWidth(1 * opts.pix);
  5406. context.setStrokeStyle(item.color);
  5407. context.setFillStyle(item.color);
  5408. context.beginPath();
  5409. context.moveTo(startX, startY);
  5410. context.lineTo(endX, startY);
  5411. context.stroke();
  5412. context.closePath();
  5413. context.beginPath();
  5414. context.moveTo(endX, startY);
  5415. context.arc(endX, startY, 2, 0, 2 * Math.PI);
  5416. context.closePath();
  5417. context.fill();
  5418. context.beginPath();
  5419. context.setFontSize(fontSize);
  5420. context.setFillStyle(item.textColor || opts.fontColor);
  5421. context.fillText(text, endX - 5 - measureText(text, fontSize, context), startY + fontSize / 2 - 2);
  5422. context.closePath();
  5423. context.stroke();
  5424. context.closePath();
  5425. }
  5426. }
  5427. }
  5428. function drawCanvas(opts, context) {
  5429. context.draw();
  5430. }
  5431. var Timing = {
  5432. easeIn: function easeIn(pos) {
  5433. return Math.pow(pos, 3);
  5434. },
  5435. easeOut: function easeOut(pos) {
  5436. return Math.pow(pos - 1, 3) + 1;
  5437. },
  5438. easeInOut: function easeInOut(pos) {
  5439. if ((pos /= 0.5) < 1) {
  5440. return 0.5 * Math.pow(pos, 3);
  5441. } else {
  5442. return 0.5 * (Math.pow(pos - 2, 3) + 2);
  5443. }
  5444. },
  5445. linear: function linear(pos) {
  5446. return pos;
  5447. }
  5448. };
  5449. function Animation(opts) {
  5450. this.isStop = false;
  5451. opts.duration = typeof opts.duration === 'undefined' ? 1000 : opts.duration;
  5452. opts.timing = opts.timing || 'easeInOut';
  5453. var delay = 17;
  5454. function createAnimationFrame() {
  5455. if (typeof setTimeout !== 'undefined') {
  5456. return function(step, delay) {
  5457. setTimeout(function() {
  5458. var timeStamp = +new Date();
  5459. step(timeStamp);
  5460. }, delay);
  5461. };
  5462. } else if (typeof requestAnimationFrame !== 'undefined') {
  5463. return requestAnimationFrame;
  5464. } else {
  5465. return function(step) {
  5466. step(null);
  5467. };
  5468. }
  5469. };
  5470. var animationFrame = createAnimationFrame();
  5471. var startTimeStamp = null;
  5472. var _step = function step(timestamp) {
  5473. if (timestamp === null || this.isStop === true) {
  5474. opts.onProcess && opts.onProcess(1);
  5475. opts.onAnimationFinish && opts.onAnimationFinish();
  5476. return;
  5477. }
  5478. if (startTimeStamp === null) {
  5479. startTimeStamp = timestamp;
  5480. }
  5481. if (timestamp - startTimeStamp < opts.duration) {
  5482. var process = (timestamp - startTimeStamp) / opts.duration;
  5483. var timingFunction = Timing[opts.timing];
  5484. process = timingFunction(process);
  5485. opts.onProcess && opts.onProcess(process);
  5486. animationFrame(_step, delay);
  5487. } else {
  5488. opts.onProcess && opts.onProcess(1);
  5489. opts.onAnimationFinish && opts.onAnimationFinish();
  5490. }
  5491. };
  5492. _step = _step.bind(this);
  5493. animationFrame(_step, delay);
  5494. }
  5495. Animation.prototype.stop = function() {
  5496. this.isStop = true;
  5497. };
  5498. function drawCharts(type, opts, config, context) {
  5499. var _this = this;
  5500. var series = opts.series;
  5501. //兼容ECharts饼图类数据格式
  5502. if (type === 'pie' || type === 'ring' || type === 'rose' || type === 'funnel') {
  5503. series = fixPieSeries(series, opts, config);
  5504. }
  5505. var categories = opts.categories;
  5506. series = fillSeries(series, opts, config);
  5507. var duration = opts.animation ? opts.duration : 0;
  5508. _this.animationInstance && _this.animationInstance.stop();
  5509. var seriesMA = null;
  5510. if (type == 'candle') {
  5511. let average = assign({}, opts.extra.candle.average);
  5512. if (average.show) {
  5513. seriesMA = calCandleMA(average.day, average.name, average.color, series[0].data);
  5514. seriesMA = fillSeries(seriesMA, opts, config);
  5515. opts.seriesMA = seriesMA;
  5516. } else if (opts.seriesMA) {
  5517. seriesMA = opts.seriesMA = fillSeries(opts.seriesMA, opts, config);
  5518. } else {
  5519. seriesMA = series;
  5520. }
  5521. } else {
  5522. seriesMA = series;
  5523. }
  5524. /* 过滤掉show=false的series */
  5525. opts._series_ = series = filterSeries(series);
  5526. //重新计算图表区域
  5527. opts.area = new Array(4);
  5528. //复位绘图区域
  5529. for (let j = 0; j < 4; j++) {
  5530. opts.area[j] = opts.padding[j] * opts.pix;
  5531. }
  5532. //通过计算三大区域:图例、X轴、Y轴的大小,确定绘图区域
  5533. var _calLegendData = calLegendData(seriesMA, opts, config, opts.chartData, context),
  5534. legendHeight = _calLegendData.area.wholeHeight,
  5535. legendWidth = _calLegendData.area.wholeWidth;
  5536. switch (opts.legend.position) {
  5537. case 'top':
  5538. opts.area[0] += legendHeight;
  5539. break;
  5540. case 'bottom':
  5541. opts.area[2] += legendHeight;
  5542. break;
  5543. case 'left':
  5544. opts.area[3] += legendWidth;
  5545. break;
  5546. case 'right':
  5547. opts.area[1] += legendWidth;
  5548. break;
  5549. }
  5550. let _calYAxisData = {},
  5551. yAxisWidth = 0;
  5552. 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') {
  5553. _calYAxisData = calYAxisData(series, opts, config, context);
  5554. yAxisWidth = _calYAxisData.yAxisWidth;
  5555. //如果显示Y轴标题
  5556. if (opts.yAxis.showTitle) {
  5557. let maxTitleHeight = 0;
  5558. for (let i = 0; i < opts.yAxis.data.length; i++) {
  5559. maxTitleHeight = Math.max(maxTitleHeight, opts.yAxis.data[i].titleFontSize ? opts.yAxis.data[i].titleFontSize * opts.pix : config.fontSize)
  5560. }
  5561. opts.area[0] += maxTitleHeight;
  5562. }
  5563. let rightIndex = 0,
  5564. leftIndex = 0;
  5565. //计算主绘图区域左右位置
  5566. for (let i = 0; i < yAxisWidth.length; i++) {
  5567. if (yAxisWidth[i].position == 'left') {
  5568. if (leftIndex > 0) {
  5569. opts.area[3] += yAxisWidth[i].width + opts.yAxis.padding * opts.pix;
  5570. } else {
  5571. opts.area[3] += yAxisWidth[i].width;
  5572. }
  5573. leftIndex += 1;
  5574. } else if (yAxisWidth[i].position == 'right') {
  5575. if (rightIndex > 0) {
  5576. opts.area[1] += yAxisWidth[i].width + opts.yAxis.padding * opts.pix;
  5577. } else {
  5578. opts.area[1] += yAxisWidth[i].width;
  5579. }
  5580. rightIndex += 1;
  5581. }
  5582. }
  5583. } else {
  5584. config.yAxisWidth = yAxisWidth;
  5585. }
  5586. opts.chartData.yAxisData = _calYAxisData;
  5587. if (opts.categories && opts.categories.length && opts.type !== 'radar' && opts.type !== 'gauge' && opts.type !== 'bar') {
  5588. opts.chartData.xAxisData = getXAxisPoints(opts.categories, opts, config);
  5589. let _calCategoriesData = calCategoriesData(opts.categories, opts, config, opts.chartData.xAxisData.eachSpacing, context),
  5590. xAxisHeight = _calCategoriesData.xAxisHeight,
  5591. angle = _calCategoriesData.angle;
  5592. config.xAxisHeight = xAxisHeight;
  5593. config._xAxisTextAngle_ = angle;
  5594. opts.area[2] += xAxisHeight;
  5595. opts.chartData.categoriesData = _calCategoriesData;
  5596. } else {
  5597. if (opts.type === 'line' || opts.type === 'area' || opts.type === 'scatter' || opts.type === 'bubble' || opts.type === 'bar') {
  5598. opts.chartData.xAxisData = calXAxisData(series, opts, config, context);
  5599. categories = opts.chartData.xAxisData.rangesFormat;
  5600. let _calCategoriesData = calCategoriesData(categories, opts, config, opts.chartData.xAxisData.eachSpacing, context),
  5601. xAxisHeight = _calCategoriesData.xAxisHeight,
  5602. angle = _calCategoriesData.angle;
  5603. config.xAxisHeight = xAxisHeight;
  5604. config._xAxisTextAngle_ = angle;
  5605. opts.area[2] += xAxisHeight;
  5606. opts.chartData.categoriesData = _calCategoriesData;
  5607. } else {
  5608. opts.chartData.xAxisData = {
  5609. xAxisPoints: []
  5610. };
  5611. }
  5612. }
  5613. //计算右对齐偏移距离
  5614. if (opts.enableScroll && opts.xAxis.scrollAlign == 'right' && opts._scrollDistance_ === undefined) {
  5615. let offsetLeft = 0,
  5616. xAxisPoints = opts.chartData.xAxisData.xAxisPoints,
  5617. startX = opts.chartData.xAxisData.startX,
  5618. endX = opts.chartData.xAxisData.endX,
  5619. eachSpacing = opts.chartData.xAxisData.eachSpacing;
  5620. let totalWidth = eachSpacing * (xAxisPoints.length - 1);
  5621. let screenWidth = endX - startX;
  5622. offsetLeft = screenWidth - totalWidth;
  5623. _this.scrollOption = {
  5624. currentOffset: offsetLeft,
  5625. startTouchX: offsetLeft,
  5626. distance: 0,
  5627. lastMoveTime: 0
  5628. };
  5629. opts._scrollDistance_ = offsetLeft;
  5630. }
  5631. if (type === 'pie' || type === 'ring' || type === 'rose') {
  5632. config._pieTextMaxLength_ = opts.dataLabel === false ? 0 : getPieTextMaxLength(seriesMA, config, context, opts);
  5633. }
  5634. switch (type) {
  5635. case 'word':
  5636. this.animationInstance = new Animation({
  5637. timing: opts.timing,
  5638. duration: duration,
  5639. onProcess: function(process) {
  5640. context.clearRect(0, 0, opts.width, opts.height);
  5641. if (opts.rotate) {
  5642. contextRotate(context, opts);
  5643. }
  5644. drawWordCloudDataPoints(series, opts, config, context, process);
  5645. drawCanvas(opts, context);
  5646. },
  5647. onAnimationFinish: function onAnimationFinish() {
  5648. _this.uevent.trigger('renderComplete');
  5649. }
  5650. });
  5651. break;
  5652. case 'map':
  5653. context.clearRect(0, 0, opts.width, opts.height);
  5654. drawMapDataPoints(series, opts, config, context);
  5655. break;
  5656. case 'funnel':
  5657. this.animationInstance = new Animation({
  5658. timing: opts.timing,
  5659. duration: duration,
  5660. onProcess: function(process) {
  5661. context.clearRect(0, 0, opts.width, opts.height);
  5662. if (opts.rotate) {
  5663. contextRotate(context, opts);
  5664. }
  5665. opts.chartData.funnelData = drawFunnelDataPoints(series, opts, config, context, process);
  5666. drawLegend(opts.series, opts, config, context, opts.chartData);
  5667. drawToolTipBridge(opts, config, context, process);
  5668. drawCanvas(opts, context);
  5669. },
  5670. onAnimationFinish: function onAnimationFinish() {
  5671. _this.uevent.trigger('renderComplete');
  5672. }
  5673. });
  5674. break;
  5675. case 'line':
  5676. this.animationInstance = new Animation({
  5677. timing: opts.timing,
  5678. duration: duration,
  5679. onProcess: function onProcess(process) {
  5680. context.clearRect(0, 0, opts.width, opts.height);
  5681. if (opts.rotate) {
  5682. contextRotate(context, opts);
  5683. }
  5684. drawYAxisGrid(categories, opts, config, context);
  5685. drawXAxis(categories, opts, config, context);
  5686. var _drawLineDataPoints = drawLineDataPoints(series, opts, config, context, process),
  5687. xAxisPoints = _drawLineDataPoints.xAxisPoints,
  5688. calPoints = _drawLineDataPoints.calPoints,
  5689. eachSpacing = _drawLineDataPoints.eachSpacing;
  5690. opts.chartData.xAxisPoints = xAxisPoints;
  5691. opts.chartData.calPoints = calPoints;
  5692. opts.chartData.eachSpacing = eachSpacing;
  5693. drawYAxis(series, opts, config, context);
  5694. if (opts.enableMarkLine !== false && process === 1) {
  5695. drawMarkLine(opts, config, context);
  5696. }
  5697. drawLegend(opts.series, opts, config, context, opts.chartData);
  5698. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5699. drawCanvas(opts, context);
  5700. },
  5701. onAnimationFinish: function onAnimationFinish() {
  5702. _this.uevent.trigger('renderComplete');
  5703. }
  5704. });
  5705. break;
  5706. case 'scatter':
  5707. this.animationInstance = new Animation({
  5708. timing: opts.timing,
  5709. duration: duration,
  5710. onProcess: function onProcess(process) {
  5711. context.clearRect(0, 0, opts.width, opts.height);
  5712. if (opts.rotate) {
  5713. contextRotate(context, opts);
  5714. }
  5715. drawYAxisGrid(categories, opts, config, context);
  5716. drawXAxis(categories, opts, config, context);
  5717. var _drawScatterDataPoints = drawScatterDataPoints(series, opts, config, context, process),
  5718. xAxisPoints = _drawScatterDataPoints.xAxisPoints,
  5719. calPoints = _drawScatterDataPoints.calPoints,
  5720. eachSpacing = _drawScatterDataPoints.eachSpacing;
  5721. opts.chartData.xAxisPoints = xAxisPoints;
  5722. opts.chartData.calPoints = calPoints;
  5723. opts.chartData.eachSpacing = eachSpacing;
  5724. drawYAxis(series, opts, config, context);
  5725. if (opts.enableMarkLine !== false && process === 1) {
  5726. drawMarkLine(opts, config, context);
  5727. }
  5728. drawLegend(opts.series, opts, config, context, opts.chartData);
  5729. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5730. drawCanvas(opts, context);
  5731. },
  5732. onAnimationFinish: function onAnimationFinish() {
  5733. _this.uevent.trigger('renderComplete');
  5734. }
  5735. });
  5736. break;
  5737. case 'bubble':
  5738. this.animationInstance = new Animation({
  5739. timing: opts.timing,
  5740. duration: duration,
  5741. onProcess: function onProcess(process) {
  5742. context.clearRect(0, 0, opts.width, opts.height);
  5743. if (opts.rotate) {
  5744. contextRotate(context, opts);
  5745. }
  5746. drawYAxisGrid(categories, opts, config, context);
  5747. drawXAxis(categories, opts, config, context);
  5748. var _drawBubbleDataPoints = drawBubbleDataPoints(series, opts, config, context, process),
  5749. xAxisPoints = _drawBubbleDataPoints.xAxisPoints,
  5750. calPoints = _drawBubbleDataPoints.calPoints,
  5751. eachSpacing = _drawBubbleDataPoints.eachSpacing;
  5752. opts.chartData.xAxisPoints = xAxisPoints;
  5753. opts.chartData.calPoints = calPoints;
  5754. opts.chartData.eachSpacing = eachSpacing;
  5755. drawYAxis(series, opts, config, context);
  5756. if (opts.enableMarkLine !== false && process === 1) {
  5757. drawMarkLine(opts, config, context);
  5758. }
  5759. drawLegend(opts.series, opts, config, context, opts.chartData);
  5760. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5761. drawCanvas(opts, context);
  5762. },
  5763. onAnimationFinish: function onAnimationFinish() {
  5764. _this.uevent.trigger('renderComplete');
  5765. }
  5766. });
  5767. break;
  5768. case 'mix':
  5769. this.animationInstance = new Animation({
  5770. timing: opts.timing,
  5771. duration: duration,
  5772. onProcess: function onProcess(process) {
  5773. context.clearRect(0, 0, opts.width, opts.height);
  5774. if (opts.rotate) {
  5775. contextRotate(context, opts);
  5776. }
  5777. drawYAxisGrid(categories, opts, config, context);
  5778. drawXAxis(categories, opts, config, context);
  5779. var _drawMixDataPoints = drawMixDataPoints(series, opts, config, context, process),
  5780. xAxisPoints = _drawMixDataPoints.xAxisPoints,
  5781. calPoints = _drawMixDataPoints.calPoints,
  5782. eachSpacing = _drawMixDataPoints.eachSpacing;
  5783. opts.chartData.xAxisPoints = xAxisPoints;
  5784. opts.chartData.calPoints = calPoints;
  5785. opts.chartData.eachSpacing = eachSpacing;
  5786. drawYAxis(series, opts, config, context);
  5787. if (opts.enableMarkLine !== false && process === 1) {
  5788. drawMarkLine(opts, config, context);
  5789. }
  5790. drawLegend(opts.series, opts, config, context, opts.chartData);
  5791. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5792. drawCanvas(opts, context);
  5793. },
  5794. onAnimationFinish: function onAnimationFinish() {
  5795. _this.uevent.trigger('renderComplete');
  5796. }
  5797. });
  5798. break;
  5799. case 'column':
  5800. this.animationInstance = new Animation({
  5801. timing: opts.timing,
  5802. duration: duration,
  5803. onProcess: function onProcess(process) {
  5804. context.clearRect(0, 0, opts.width, opts.height);
  5805. if (opts.rotate) {
  5806. contextRotate(context, opts);
  5807. }
  5808. drawYAxisGrid(categories, opts, config, context);
  5809. drawXAxis(categories, opts, config, context);
  5810. var _drawColumnDataPoints = drawColumnDataPoints(series, opts, config, context, process),
  5811. xAxisPoints = _drawColumnDataPoints.xAxisPoints,
  5812. calPoints = _drawColumnDataPoints.calPoints,
  5813. eachSpacing = _drawColumnDataPoints.eachSpacing;
  5814. opts.chartData.xAxisPoints = xAxisPoints;
  5815. opts.chartData.calPoints = calPoints;
  5816. opts.chartData.eachSpacing = eachSpacing;
  5817. drawYAxis(series, opts, config, context);
  5818. if (opts.enableMarkLine !== false && process === 1) {
  5819. drawMarkLine(opts, config, context);
  5820. }
  5821. drawLegend(opts.series, opts, config, context, opts.chartData);
  5822. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5823. drawCanvas(opts, context);
  5824. },
  5825. onAnimationFinish: function onAnimationFinish() {
  5826. _this.uevent.trigger('renderComplete');
  5827. }
  5828. });
  5829. break;
  5830. case 'bar':
  5831. this.animationInstance = new Animation({
  5832. timing: opts.timing,
  5833. duration: duration,
  5834. onProcess: function onProcess(process) {
  5835. context.clearRect(0, 0, opts.width, opts.height);
  5836. if (opts.rotate) {
  5837. contextRotate(context, opts);
  5838. }
  5839. drawXAxis(categories, opts, config, context);
  5840. var _drawBarDataPoints = drawBarDataPoints(series, opts, config, context, process),
  5841. yAxisPoints = _drawBarDataPoints.yAxisPoints,
  5842. calPoints = _drawBarDataPoints.calPoints,
  5843. eachSpacing = _drawBarDataPoints.eachSpacing;
  5844. opts.chartData.yAxisPoints = yAxisPoints;
  5845. opts.chartData.xAxisPoints = opts.chartData.xAxisData.xAxisPoints;
  5846. opts.chartData.calPoints = calPoints;
  5847. opts.chartData.eachSpacing = eachSpacing;
  5848. drawYAxis(series, opts, config, context);
  5849. if (opts.enableMarkLine !== false && process === 1) {
  5850. drawMarkLine(opts, config, context);
  5851. }
  5852. drawLegend(opts.series, opts, config, context, opts.chartData);
  5853. drawToolTipBridge(opts, config, context, process, eachSpacing, yAxisPoints);
  5854. drawCanvas(opts, context);
  5855. },
  5856. onAnimationFinish: function onAnimationFinish() {
  5857. _this.uevent.trigger('renderComplete');
  5858. }
  5859. });
  5860. break;
  5861. case 'area':
  5862. this.animationInstance = new Animation({
  5863. timing: opts.timing,
  5864. duration: duration,
  5865. onProcess: function onProcess(process) {
  5866. context.clearRect(0, 0, opts.width, opts.height);
  5867. if (opts.rotate) {
  5868. contextRotate(context, opts);
  5869. }
  5870. drawYAxisGrid(categories, opts, config, context);
  5871. drawXAxis(categories, opts, config, context);
  5872. var _drawAreaDataPoints = drawAreaDataPoints(series, opts, config, context, process),
  5873. xAxisPoints = _drawAreaDataPoints.xAxisPoints,
  5874. calPoints = _drawAreaDataPoints.calPoints,
  5875. eachSpacing = _drawAreaDataPoints.eachSpacing;
  5876. opts.chartData.xAxisPoints = xAxisPoints;
  5877. opts.chartData.calPoints = calPoints;
  5878. opts.chartData.eachSpacing = eachSpacing;
  5879. drawYAxis(series, opts, config, context);
  5880. if (opts.enableMarkLine !== false && process === 1) {
  5881. drawMarkLine(opts, config, context);
  5882. }
  5883. drawLegend(opts.series, opts, config, context, opts.chartData);
  5884. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  5885. drawCanvas(opts, context);
  5886. },
  5887. onAnimationFinish: function onAnimationFinish() {
  5888. _this.uevent.trigger('renderComplete');
  5889. }
  5890. });
  5891. break;
  5892. case 'ring':
  5893. case 'pie':
  5894. this.animationInstance = new Animation({
  5895. timing: opts.timing,
  5896. duration: duration,
  5897. onProcess: function onProcess(process) {
  5898. context.clearRect(0, 0, opts.width, opts.height);
  5899. if (opts.rotate) {
  5900. contextRotate(context, opts);
  5901. }
  5902. opts.chartData.pieData = drawPieDataPoints(series, opts, config, context, process);
  5903. drawLegend(opts.series, opts, config, context, opts.chartData);
  5904. drawToolTipBridge(opts, config, context, process);
  5905. drawCanvas(opts, context);
  5906. },
  5907. onAnimationFinish: function onAnimationFinish() {
  5908. _this.uevent.trigger('renderComplete');
  5909. }
  5910. });
  5911. break;
  5912. case 'rose':
  5913. this.animationInstance = new Animation({
  5914. timing: opts.timing,
  5915. duration: duration,
  5916. onProcess: function onProcess(process) {
  5917. context.clearRect(0, 0, opts.width, opts.height);
  5918. if (opts.rotate) {
  5919. contextRotate(context, opts);
  5920. }
  5921. opts.chartData.pieData = drawRoseDataPoints(series, opts, config, context, process);
  5922. drawLegend(opts.series, opts, config, context, opts.chartData);
  5923. drawToolTipBridge(opts, config, context, process);
  5924. drawCanvas(opts, context);
  5925. },
  5926. onAnimationFinish: function onAnimationFinish() {
  5927. _this.uevent.trigger('renderComplete');
  5928. }
  5929. });
  5930. break;
  5931. case 'radar':
  5932. this.animationInstance = new Animation({
  5933. timing: opts.timing,
  5934. duration: duration,
  5935. onProcess: function onProcess(process) {
  5936. context.clearRect(0, 0, opts.width, opts.height);
  5937. if (opts.rotate) {
  5938. contextRotate(context, opts);
  5939. }
  5940. opts.chartData.radarData = drawRadarDataPoints(series, opts, config, context, process);
  5941. drawLegend(opts.series, opts, config, context, opts.chartData);
  5942. drawToolTipBridge(opts, config, context, process);
  5943. drawCanvas(opts, context);
  5944. },
  5945. onAnimationFinish: function onAnimationFinish() {
  5946. _this.uevent.trigger('renderComplete');
  5947. }
  5948. });
  5949. break;
  5950. case 'arcbar':
  5951. this.animationInstance = new Animation({
  5952. timing: opts.timing,
  5953. duration: duration,
  5954. onProcess: function onProcess(process) {
  5955. context.clearRect(0, 0, opts.width, opts.height);
  5956. if (opts.rotate) {
  5957. contextRotate(context, opts);
  5958. }
  5959. opts.chartData.arcbarData = drawArcbarDataPoints(series, opts, config, context, process);
  5960. drawCanvas(opts, context);
  5961. },
  5962. onAnimationFinish: function onAnimationFinish() {
  5963. _this.uevent.trigger('renderComplete');
  5964. }
  5965. });
  5966. break;
  5967. case 'gauge':
  5968. this.animationInstance = new Animation({
  5969. timing: opts.timing,
  5970. duration: duration,
  5971. onProcess: function onProcess(process) {
  5972. context.clearRect(0, 0, opts.width, opts.height);
  5973. if (opts.rotate) {
  5974. contextRotate(context, opts);
  5975. }
  5976. opts.chartData.gaugeData = drawGaugeDataPoints(categories, series, opts, config, context, process);
  5977. drawCanvas(opts, context);
  5978. },
  5979. onAnimationFinish: function onAnimationFinish() {
  5980. _this.uevent.trigger('renderComplete');
  5981. }
  5982. });
  5983. break;
  5984. case 'candle':
  5985. this.animationInstance = new Animation({
  5986. timing: opts.timing,
  5987. duration: duration,
  5988. onProcess: function onProcess(process) {
  5989. context.clearRect(0, 0, opts.width, opts.height);
  5990. if (opts.rotate) {
  5991. contextRotate(context, opts);
  5992. }
  5993. drawYAxisGrid(categories, opts, config, context);
  5994. drawXAxis(categories, opts, config, context);
  5995. var _drawCandleDataPoints = drawCandleDataPoints(series, seriesMA, opts, config, context, process),
  5996. xAxisPoints = _drawCandleDataPoints.xAxisPoints,
  5997. calPoints = _drawCandleDataPoints.calPoints,
  5998. eachSpacing = _drawCandleDataPoints.eachSpacing;
  5999. opts.chartData.xAxisPoints = xAxisPoints;
  6000. opts.chartData.calPoints = calPoints;
  6001. opts.chartData.eachSpacing = eachSpacing;
  6002. drawYAxis(series, opts, config, context);
  6003. if (opts.enableMarkLine !== false && process === 1) {
  6004. drawMarkLine(opts, config, context);
  6005. }
  6006. if (seriesMA) {
  6007. drawLegend(seriesMA, opts, config, context, opts.chartData);
  6008. } else {
  6009. drawLegend(opts.series, opts, config, context, opts.chartData);
  6010. }
  6011. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  6012. drawCanvas(opts, context);
  6013. },
  6014. onAnimationFinish: function onAnimationFinish() {
  6015. _this.uevent.trigger('renderComplete');
  6016. }
  6017. });
  6018. break;
  6019. }
  6020. }
  6021. function uChartsEvent() {
  6022. this.events = {};
  6023. }
  6024. uChartsEvent.prototype.addEventListener = function(type, listener) {
  6025. this.events[type] = this.events[type] || [];
  6026. this.events[type].push(listener);
  6027. };
  6028. uChartsEvent.prototype.delEventListener = function(type) {
  6029. this.events[type] = [];
  6030. };
  6031. uChartsEvent.prototype.trigger = function() {
  6032. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  6033. args[_key] = arguments[_key];
  6034. }
  6035. var type = args[0];
  6036. var params = args.slice(1);
  6037. if (!!this.events[type]) {
  6038. this.events[type].forEach(function(listener) {
  6039. try {
  6040. listener.apply(null, params);
  6041. } catch (e) {
  6042. //console.log('[uCharts] '+e);
  6043. }
  6044. });
  6045. }
  6046. };
  6047. var uCharts = function uCharts(opts) {
  6048. opts.pix = opts.pixelRatio ? opts.pixelRatio : 1;
  6049. opts.fontSize = opts.fontSize ? opts.fontSize : 13;
  6050. opts.fontColor = opts.fontColor ? opts.fontColor : config.fontColor;
  6051. if (opts.background == "" || opts.background == "none") {
  6052. opts.background = "#FFFFFF"
  6053. }
  6054. opts.title = assign({}, opts.title);
  6055. opts.subtitle = assign({}, opts.subtitle);
  6056. opts.duration = opts.duration ? opts.duration : 1000;
  6057. opts.yAxis = assign({}, {
  6058. data: [],
  6059. showTitle: false,
  6060. disabled: false,
  6061. disableGrid: false,
  6062. splitNumber: 5,
  6063. gridType: 'solid',
  6064. dashLength: 4 * opts.pix,
  6065. gridColor: '#cccccc',
  6066. padding: 10,
  6067. fontColor: '#666666'
  6068. }, opts.yAxis);
  6069. opts.xAxis = assign({}, {
  6070. rotateLabel: false,
  6071. disabled: false,
  6072. disableGrid: false,
  6073. splitNumber: 5,
  6074. calibration:false,
  6075. gridType: 'solid',
  6076. dashLength: 4,
  6077. scrollAlign: 'left',
  6078. boundaryGap: 'center',
  6079. axisLine: true,
  6080. axisLineColor: '#cccccc'
  6081. }, opts.xAxis);
  6082. opts.xAxis.scrollPosition = opts.xAxis.scrollAlign;
  6083. opts.legend = assign({}, {
  6084. show: true,
  6085. position: 'bottom',
  6086. float: 'center',
  6087. backgroundColor: 'rgba(0,0,0,0)',
  6088. borderColor: 'rgba(0,0,0,0)',
  6089. borderWidth: 0,
  6090. padding: 5,
  6091. margin: 5,
  6092. itemGap: 10,
  6093. fontSize: opts.fontSize,
  6094. lineHeight: opts.fontSize,
  6095. fontColor: opts.fontColor,
  6096. formatter: {},
  6097. hiddenColor: '#CECECE'
  6098. }, opts.legend);
  6099. opts.extra = assign({}, opts.extra);
  6100. opts.rotate = opts.rotate ? true : false;
  6101. opts.animation = opts.animation ? true : false;
  6102. opts.rotate = opts.rotate ? true : false;
  6103. opts.canvas2d = opts.canvas2d ? true : false;
  6104. let config$$1 = JSON.parse(JSON.stringify(config));
  6105. config$$1.color = opts.color ? opts.color : config$$1.color;
  6106. config$$1.yAxisTitleWidth = opts.yAxis.disabled !== true && opts.yAxis.title ? config$$1.yAxisTitleWidth : 0;
  6107. if (opts.type == 'pie') {
  6108. config$$1.pieChartLinePadding = opts.dataLabel === false ? 0 : opts.extra.pie.labelWidth * opts.pix || config$$1.pieChartLinePadding * opts.pix;
  6109. }
  6110. if (opts.type == 'ring') {
  6111. config$$1.pieChartLinePadding = opts.dataLabel === false ? 0 : opts.extra.ring.labelWidth * opts.pix || config$$1.pieChartLinePadding * opts.pix;
  6112. }
  6113. if (opts.type == 'rose') {
  6114. config$$1.pieChartLinePadding = opts.dataLabel === false ? 0 : opts.extra.rose.labelWidth * opts.pix || config$$1.pieChartLinePadding * opts.pix;
  6115. }
  6116. config$$1.pieChartTextPadding = opts.dataLabel === false ? 0 : config$$1.pieChartTextPadding * opts.pix;
  6117. config$$1.yAxisSplit = opts.yAxis.splitNumber ? opts.yAxis.splitNumber : config.yAxisSplit;
  6118. //屏幕旋转
  6119. config$$1.rotate = opts.rotate;
  6120. if (opts.rotate) {
  6121. let tempWidth = opts.width;
  6122. let tempHeight = opts.height;
  6123. opts.width = tempHeight;
  6124. opts.height = tempWidth;
  6125. }
  6126. //适配高分屏
  6127. opts.padding = opts.padding ? opts.padding : config$$1.padding;
  6128. config$$1.yAxisWidth = config.yAxisWidth * opts.pix;
  6129. config$$1.xAxisHeight = config.xAxisHeight * opts.pix;
  6130. if (opts.enableScroll && opts.xAxis.scrollShow) {
  6131. config$$1.xAxisHeight += 6 * opts.pix;
  6132. }
  6133. config$$1.xAxisLineHeight = config.xAxisLineHeight * opts.pix;
  6134. config$$1.fontSize = opts.fontSize * opts.pix;
  6135. config$$1.titleFontSize = config.titleFontSize * opts.pix;
  6136. config$$1.subtitleFontSize = config.subtitleFontSize * opts.pix;
  6137. config$$1.toolTipPadding = config.toolTipPadding * opts.pix;
  6138. config$$1.toolTipLineHeight = config.toolTipLineHeight * opts.pix;
  6139. config$$1.columePadding = config.columePadding * opts.pix;
  6140. //this.context = opts.context ? opts.context : uni.createCanvasContext(opts.canvasId, opts.$this);
  6141. //v2.0版本后需要自行获取context并传入opts进行初始化,这么做是为了确保uCharts可以跨更多端使用,并保证了自定义组件this实例不被循环嵌套。如果您觉得不便请取消上面注释,采用v1.0版本的方式使用,对此给您带来的不便敬请谅解!
  6142. if(!opts.context){
  6143. throw new Error('[uCharts] 未获取到context!注意:v2.0版本后,需要自行获取canvas的绘图上下文并传入opts.context!');
  6144. }
  6145. this.context = opts.context;
  6146. if (!this.context.setTextAlign) {
  6147. this.context.setStrokeStyle = function(e) {
  6148. return this.strokeStyle = e;
  6149. }
  6150. this.context.setLineWidth = function(e) {
  6151. return this.lineWidth = e;
  6152. }
  6153. this.context.setLineCap = function(e) {
  6154. return this.lineCap = e;
  6155. }
  6156. this.context.setFontSize = function(e) {
  6157. return this.font = e + "px sans-serif";
  6158. }
  6159. this.context.setFillStyle = function(e) {
  6160. return this.fillStyle = e;
  6161. }
  6162. this.context.setTextAlign = function(e) {
  6163. return this.textAlign = e;
  6164. }
  6165. this.context.draw = function() {}
  6166. }
  6167. //兼容NVUEsetLineDash
  6168. if(!this.context.setLineDash){
  6169. this.context.setLineDash = function(e) {}
  6170. }
  6171. opts.chartData = {};
  6172. this.uevent = new uChartsEvent();
  6173. this.scrollOption = {
  6174. currentOffset: 0,
  6175. startTouchX: 0,
  6176. distance: 0,
  6177. lastMoveTime: 0
  6178. };
  6179. this.opts = opts;
  6180. this.config = config$$1;
  6181. drawCharts.call(this, opts.type, opts, config$$1, this.context);
  6182. };
  6183. uCharts.prototype.updateData = function() {
  6184. let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  6185. this.opts = assign({}, this.opts, data);
  6186. this.opts.updateData = true;
  6187. let scrollPosition = data.scrollPosition || 'current';
  6188. switch (scrollPosition) {
  6189. case 'current':
  6190. //this.opts._scrollDistance_ = this.scrollOption.currentOffset;
  6191. break;
  6192. case 'left':
  6193. this.opts._scrollDistance_ = 0;
  6194. this.scrollOption = {
  6195. currentOffset: 0,
  6196. startTouchX: 0,
  6197. distance: 0,
  6198. lastMoveTime: 0
  6199. };
  6200. break;
  6201. case 'right':
  6202. let _calYAxisData = calYAxisData(this.opts.series, this.opts, this.config, this.context), yAxisWidth = _calYAxisData.yAxisWidth;
  6203. this.config.yAxisWidth = yAxisWidth;
  6204. let offsetLeft = 0;
  6205. let _getXAxisPoints0 = getXAxisPoints(this.opts.categories, this.opts, this.config), xAxisPoints = _getXAxisPoints0.xAxisPoints,
  6206. startX = _getXAxisPoints0.startX,
  6207. endX = _getXAxisPoints0.endX,
  6208. eachSpacing = _getXAxisPoints0.eachSpacing;
  6209. let totalWidth = eachSpacing * (xAxisPoints.length - 1);
  6210. let screenWidth = endX - startX;
  6211. offsetLeft = screenWidth - totalWidth;
  6212. this.scrollOption = {
  6213. currentOffset: offsetLeft,
  6214. startTouchX: offsetLeft,
  6215. distance: 0,
  6216. lastMoveTime: 0
  6217. };
  6218. this.opts._scrollDistance_ = offsetLeft;
  6219. break;
  6220. }
  6221. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  6222. };
  6223. uCharts.prototype.zoom = function() {
  6224. var val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.opts.xAxis.itemCount;
  6225. if (this.opts.enableScroll !== true) {
  6226. console.log('[uCharts] 请启用滚动条后使用')
  6227. return;
  6228. }
  6229. //当前屏幕中间点
  6230. let centerPoint = Math.round(Math.abs(this.scrollOption.currentOffset) / this.opts.chartData.eachSpacing) + Math.round(this.opts.xAxis.itemCount / 2);
  6231. this.opts.animation = false;
  6232. this.opts.xAxis.itemCount = val.itemCount;
  6233. //重新计算x轴偏移距离
  6234. let _calYAxisData = calYAxisData(this.opts.series, this.opts, this.config, this.context),
  6235. yAxisWidth = _calYAxisData.yAxisWidth;
  6236. this.config.yAxisWidth = yAxisWidth;
  6237. let offsetLeft = 0;
  6238. let _getXAxisPoints0 = getXAxisPoints(this.opts.categories, this.opts, this.config),
  6239. xAxisPoints = _getXAxisPoints0.xAxisPoints,
  6240. startX = _getXAxisPoints0.startX,
  6241. endX = _getXAxisPoints0.endX,
  6242. eachSpacing = _getXAxisPoints0.eachSpacing;
  6243. let centerLeft = eachSpacing * centerPoint;
  6244. let screenWidth = endX - startX;
  6245. let MaxLeft = screenWidth - eachSpacing * (xAxisPoints.length - 1);
  6246. offsetLeft = screenWidth / 2 - centerLeft;
  6247. if (offsetLeft > 0) {
  6248. offsetLeft = 0;
  6249. }
  6250. if (offsetLeft < MaxLeft) {
  6251. offsetLeft = MaxLeft;
  6252. }
  6253. this.scrollOption = {
  6254. currentOffset: offsetLeft,
  6255. startTouchX: offsetLeft,
  6256. distance: 0,
  6257. lastMoveTime: 0
  6258. };
  6259. this.opts._scrollDistance_ = offsetLeft;
  6260. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  6261. };
  6262. uCharts.prototype.stopAnimation = function() {
  6263. this.animationInstance && this.animationInstance.stop();
  6264. };
  6265. uCharts.prototype.addEventListener = function(type, listener) {
  6266. this.uevent.addEventListener(type, listener);
  6267. };
  6268. uCharts.prototype.delEventListener = function(type) {
  6269. this.uevent.delEventListener(type);
  6270. };
  6271. uCharts.prototype.getCurrentDataIndex = function(e) {
  6272. var touches = null;
  6273. if (e.changedTouches) {
  6274. touches = e.changedTouches[0];
  6275. } else {
  6276. touches = e.mp.changedTouches[0];
  6277. }
  6278. if (touches) {
  6279. let _touches$ = getTouches(touches, this.opts, e);
  6280. if (this.opts.type === 'pie' || this.opts.type === 'ring') {
  6281. return findPieChartCurrentIndex({
  6282. x: _touches$.x,
  6283. y: _touches$.y
  6284. }, this.opts.chartData.pieData, this.opts);
  6285. } else if (this.opts.type === 'rose') {
  6286. return findRoseChartCurrentIndex({
  6287. x: _touches$.x,
  6288. y: _touches$.y
  6289. }, this.opts.chartData.pieData, this.opts);
  6290. } else if (this.opts.type === 'radar') {
  6291. return findRadarChartCurrentIndex({
  6292. x: _touches$.x,
  6293. y: _touches$.y
  6294. }, this.opts.chartData.radarData, this.opts.categories.length);
  6295. } else if (this.opts.type === 'funnel') {
  6296. return findFunnelChartCurrentIndex({
  6297. x: _touches$.x,
  6298. y: _touches$.y
  6299. }, this.opts.chartData.funnelData);
  6300. } else if (this.opts.type === 'map') {
  6301. return findMapChartCurrentIndex({
  6302. x: _touches$.x,
  6303. y: _touches$.y
  6304. }, this.opts);
  6305. } else if (this.opts.type === 'word') {
  6306. return findWordChartCurrentIndex({
  6307. x: _touches$.x,
  6308. y: _touches$.y
  6309. }, this.opts.chartData.wordCloudData);
  6310. } else if (this.opts.type === 'bar') {
  6311. return findBarChartCurrentIndex({
  6312. x: _touches$.x,
  6313. y: _touches$.y
  6314. }, this.opts.chartData.calPoints, this.opts, this.config, Math.abs(this.scrollOption.currentOffset));
  6315. } else {
  6316. return findCurrentIndex({
  6317. x: _touches$.x,
  6318. y: _touches$.y
  6319. }, this.opts.chartData.calPoints, this.opts, this.config, Math.abs(this.scrollOption.currentOffset));
  6320. }
  6321. }
  6322. return -1;
  6323. };
  6324. uCharts.prototype.getLegendDataIndex = function(e) {
  6325. var touches = null;
  6326. if (e.changedTouches) {
  6327. touches = e.changedTouches[0];
  6328. } else {
  6329. touches = e.mp.changedTouches[0];
  6330. }
  6331. if (touches) {
  6332. let _touches$ = getTouches(touches, this.opts, e);
  6333. return findLegendIndex({
  6334. x: _touches$.x,
  6335. y: _touches$.y
  6336. }, this.opts.chartData.legendData);
  6337. }
  6338. return -1;
  6339. };
  6340. uCharts.prototype.touchLegend = function(e) {
  6341. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  6342. var touches = null;
  6343. if (e.changedTouches) {
  6344. touches = e.changedTouches[0];
  6345. } else {
  6346. touches = e.mp.changedTouches[0];
  6347. }
  6348. if (touches) {
  6349. var _touches$ = getTouches(touches, this.opts, e);
  6350. var index = this.getLegendDataIndex(e);
  6351. if (index >= 0) {
  6352. if (this.opts.type == 'candle') {
  6353. this.opts.seriesMA[index].show = !this.opts.seriesMA[index].show;
  6354. } else {
  6355. this.opts.series[index].show = !this.opts.series[index].show;
  6356. }
  6357. this.opts.animation = option.animation ? true : false;
  6358. this.opts._scrollDistance_ = this.scrollOption.currentOffset;
  6359. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  6360. }
  6361. }
  6362. };
  6363. uCharts.prototype.showToolTip = function(e) {
  6364. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  6365. var touches = null;
  6366. if (e.changedTouches) {
  6367. touches = e.changedTouches[0];
  6368. } else {
  6369. touches = e.mp.changedTouches[0];
  6370. }
  6371. if (!touches) {
  6372. console.log("[uCharts] 未获取到event坐标信息");
  6373. }
  6374. var _touches$ = getTouches(touches, this.opts, e);
  6375. var currentOffset = this.scrollOption.currentOffset;
  6376. var opts = assign({}, this.opts, {
  6377. _scrollDistance_: currentOffset,
  6378. animation: false
  6379. });
  6380. if (this.opts.type === 'line' || this.opts.type === 'area' || this.opts.type === 'column' || this.opts.type === 'scatter' || this.opts.type === 'bubble') {
  6381. var current = this.getCurrentDataIndex(e);
  6382. var index = option.index == undefined ? current.index : option.index;
  6383. if (index > -1 || index.length>0) {
  6384. var seriesData = getSeriesDataItem(this.opts.series, index, current.group);
  6385. if (seriesData.length !== 0) {
  6386. var _getToolTipData = getToolTipData(seriesData, this.opts, index, current.group, this.opts.categories, option),
  6387. textList = _getToolTipData.textList,
  6388. offset = _getToolTipData.offset;
  6389. offset.y = _touches$.y;
  6390. opts.tooltip = {
  6391. textList: option.textList !== undefined ? option.textList : textList,
  6392. offset: option.offset !== undefined ? option.offset : offset,
  6393. option: option,
  6394. index: index
  6395. };
  6396. }
  6397. }
  6398. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6399. }
  6400. if (this.opts.type === 'bar') {
  6401. var current = this.getCurrentDataIndex(e);
  6402. var index = option.index == undefined ? current.index : option.index;
  6403. if (index > -1 || index.length>0) {
  6404. var seriesData = getSeriesDataItem(this.opts.series, index, current.group);
  6405. if (seriesData.length !== 0) {
  6406. var _getToolTipData = getToolTipData(seriesData, this.opts, index, current.group, this.opts.categories, option),
  6407. textList = _getToolTipData.textList,
  6408. offset = _getToolTipData.offset;
  6409. offset.x = _touches$.x;
  6410. opts.tooltip = {
  6411. textList: option.textList !== undefined ? option.textList : textList,
  6412. offset: option.offset !== undefined ? option.offset : offset,
  6413. option: option,
  6414. index: index
  6415. };
  6416. }
  6417. }
  6418. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6419. }
  6420. if (this.opts.type === 'mix') {
  6421. var current = this.getCurrentDataIndex(e);
  6422. var index = option.index == undefined ? current.index : option.index;
  6423. if (index > -1) {
  6424. var currentOffset = this.scrollOption.currentOffset;
  6425. var opts = assign({}, this.opts, {
  6426. _scrollDistance_: currentOffset,
  6427. animation: false
  6428. });
  6429. var seriesData = getSeriesDataItem(this.opts.series, index);
  6430. if (seriesData.length !== 0) {
  6431. var _getMixToolTipData = getMixToolTipData(seriesData, this.opts, index, this.opts.categories, option),
  6432. textList = _getMixToolTipData.textList,
  6433. offset = _getMixToolTipData.offset;
  6434. offset.y = _touches$.y;
  6435. opts.tooltip = {
  6436. textList: option.textList ? option.textList : textList,
  6437. offset: option.offset !== undefined ? option.offset : offset,
  6438. option: option,
  6439. index: index
  6440. };
  6441. }
  6442. }
  6443. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6444. }
  6445. if (this.opts.type === 'candle') {
  6446. var current = this.getCurrentDataIndex(e);
  6447. var index = option.index == undefined ? current.index : option.index;
  6448. if (index > -1) {
  6449. var currentOffset = this.scrollOption.currentOffset;
  6450. var opts = assign({}, this.opts, {
  6451. _scrollDistance_: currentOffset,
  6452. animation: false
  6453. });
  6454. var seriesData = getSeriesDataItem(this.opts.series, index);
  6455. if (seriesData.length !== 0) {
  6456. var _getToolTipData = getCandleToolTipData(this.opts.series[0].data, seriesData, this.opts, index, this.opts.categories, this.opts.extra.candle, option),
  6457. textList = _getToolTipData.textList,
  6458. offset = _getToolTipData.offset;
  6459. offset.y = _touches$.y;
  6460. opts.tooltip = {
  6461. textList: option.textList ? option.textList : textList,
  6462. offset: option.offset !== undefined ? option.offset : offset,
  6463. option: option,
  6464. index: index
  6465. };
  6466. }
  6467. }
  6468. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6469. }
  6470. if (this.opts.type === 'pie' || this.opts.type === 'ring' || this.opts.type === 'rose' || this.opts.type === 'funnel') {
  6471. var index = option.index == undefined ? this.getCurrentDataIndex(e) : option.index;
  6472. if (index > -1) {
  6473. var opts = assign({}, this.opts, {animation: false});
  6474. var seriesData = assign({}, opts._series_[index]);
  6475. var textList = [{
  6476. text: option.formatter ? option.formatter(seriesData, undefined, index, opts) : seriesData.name + ': ' + seriesData.data,
  6477. color: seriesData.color
  6478. }];
  6479. var offset = {
  6480. x: _touches$.x,
  6481. y: _touches$.y
  6482. };
  6483. opts.tooltip = {
  6484. textList: option.textList ? option.textList : textList,
  6485. offset: option.offset !== undefined ? option.offset : offset,
  6486. option: option,
  6487. index: index
  6488. };
  6489. }
  6490. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6491. }
  6492. if (this.opts.type === 'map') {
  6493. var index = option.index == undefined ? this.getCurrentDataIndex(e) : option.index;
  6494. if (index > -1) {
  6495. var opts = assign({}, this.opts, {animation: false});
  6496. var seriesData = assign({}, this.opts.series[index]);
  6497. seriesData.name = seriesData.properties.name
  6498. var textList = [{
  6499. text: option.formatter ? option.formatter(seriesData, undefined, index, this.opts) : seriesData.name,
  6500. color: seriesData.color
  6501. }];
  6502. var offset = {
  6503. x: _touches$.x,
  6504. y: _touches$.y
  6505. };
  6506. opts.tooltip = {
  6507. textList: option.textList ? option.textList : textList,
  6508. offset: option.offset !== undefined ? option.offset : offset,
  6509. option: option,
  6510. index: index
  6511. };
  6512. }
  6513. opts.updateData = false;
  6514. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6515. }
  6516. if (this.opts.type === 'word') {
  6517. var index = option.index == undefined ? this.getCurrentDataIndex(e) : option.index;
  6518. if (index > -1) {
  6519. var opts = assign({}, this.opts, {animation: false});
  6520. var seriesData = assign({}, this.opts.series[index]);
  6521. var textList = [{
  6522. text: option.formatter ? option.formatter(seriesData, undefined, index, this.opts) : seriesData.name,
  6523. color: seriesData.color
  6524. }];
  6525. var offset = {
  6526. x: _touches$.x,
  6527. y: _touches$.y
  6528. };
  6529. opts.tooltip = {
  6530. textList: option.textList ? option.textList : textList,
  6531. offset: option.offset !== undefined ? option.offset : offset,
  6532. option: option,
  6533. index: index
  6534. };
  6535. }
  6536. opts.updateData = false;
  6537. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6538. }
  6539. if (this.opts.type === 'radar') {
  6540. var index = option.index == undefined ? this.getCurrentDataIndex(e) : option.index;
  6541. if (index > -1) {
  6542. var opts = assign({}, this.opts, {animation: false});
  6543. var seriesData = getSeriesDataItem(this.opts.series, index);
  6544. if (seriesData.length !== 0) {
  6545. var textList = seriesData.map((item) => {
  6546. return {
  6547. text: option.formatter ? option.formatter(item, this.opts.categories[index], index, this.opts) : item.name + ': ' + item.data,
  6548. color: item.color
  6549. };
  6550. });
  6551. var offset = {
  6552. x: _touches$.x,
  6553. y: _touches$.y
  6554. };
  6555. opts.tooltip = {
  6556. textList: option.textList ? option.textList : textList,
  6557. offset: option.offset !== undefined ? option.offset : offset,
  6558. option: option,
  6559. index: index
  6560. };
  6561. }
  6562. }
  6563. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6564. }
  6565. };
  6566. uCharts.prototype.translate = function(distance) {
  6567. this.scrollOption = {
  6568. currentOffset: distance,
  6569. startTouchX: distance,
  6570. distance: 0,
  6571. lastMoveTime: 0
  6572. };
  6573. let opts = assign({}, this.opts, {
  6574. _scrollDistance_: distance,
  6575. animation: false
  6576. });
  6577. drawCharts.call(this, this.opts.type, opts, this.config, this.context);
  6578. };
  6579. uCharts.prototype.scrollStart = function(e) {
  6580. var touches = null;
  6581. if (e.changedTouches) {
  6582. touches = e.changedTouches[0];
  6583. } else {
  6584. touches = e.mp.changedTouches[0];
  6585. }
  6586. var _touches$ = getTouches(touches, this.opts, e);
  6587. if (touches && this.opts.enableScroll === true) {
  6588. this.scrollOption.startTouchX = _touches$.x;
  6589. }
  6590. };
  6591. uCharts.prototype.scroll = function(e) {
  6592. if (this.scrollOption.lastMoveTime === 0) {
  6593. this.scrollOption.lastMoveTime = Date.now();
  6594. }
  6595. let Limit = this.opts.touchMoveLimit || 60;
  6596. let currMoveTime = Date.now();
  6597. let duration = currMoveTime - this.scrollOption.lastMoveTime;
  6598. if (duration < Math.floor(1000 / Limit)) return;
  6599. this.scrollOption.lastMoveTime = currMoveTime;
  6600. var touches = null;
  6601. if (e.changedTouches) {
  6602. touches = e.changedTouches[0];
  6603. } else {
  6604. touches = e.mp.changedTouches[0];
  6605. }
  6606. if (touches && this.opts.enableScroll === true) {
  6607. var _touches$ = getTouches(touches, this.opts, e);
  6608. var _distance;
  6609. _distance = _touches$.x - this.scrollOption.startTouchX;
  6610. var currentOffset = this.scrollOption.currentOffset;
  6611. var validDistance = calValidDistance(this, currentOffset + _distance, this.opts.chartData, this.config, this.opts);
  6612. this.scrollOption.distance = _distance = validDistance - currentOffset;
  6613. var opts = assign({}, this.opts, {
  6614. _scrollDistance_: currentOffset + _distance,
  6615. animation: false
  6616. });
  6617. this.opts = opts;
  6618. drawCharts.call(this, opts.type, opts, this.config, this.context);
  6619. return currentOffset + _distance;
  6620. }
  6621. };
  6622. uCharts.prototype.scrollEnd = function(e) {
  6623. if (this.opts.enableScroll === true) {
  6624. var _scrollOption = this.scrollOption,
  6625. currentOffset = _scrollOption.currentOffset,
  6626. distance = _scrollOption.distance;
  6627. this.scrollOption.currentOffset = currentOffset + distance;
  6628. this.scrollOption.distance = 0;
  6629. }
  6630. };
  6631. if (typeof module === "object" && typeof module.exports === "object") {
  6632. module.exports = uCharts;
  6633. }
  6634. export default uCharts;