u-charts.js 188 KB

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