u-charts.js 191 KB

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