u-charts.js 188 KB

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