ucharts.js 190 KB

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