ucharts.js 180 KB

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