u-charts.js 191 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685
  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: 15,
  22. xAxisLineHeight: 15,
  23. legendHeight: 15,
  24. yAxisTitleWidth: 15,
  25. padding: [10, 10, 10, 10],
  26. pixelRatio: 1,
  27. rotate: false,
  28. columePadding: 3,
  29. fontSize: 13,
  30. //dataPointShape: ['diamond', 'circle', 'triangle', 'rect'],
  31. dataPointShape: ['circle', 'circle', 'circle', 'circle'],
  32. colors: ['#1890ff', '#2fc25b', '#facc14', '#f04864', '#8543e0', '#90ed7d'],
  33. pieChartLinePadding: 15,
  34. pieChartTextPadding: 5,
  35. xAxisTextPadding: 3,
  36. titleColor: '#333333',
  37. titleFontSize: 20,
  38. subtitleColor: '#999999',
  39. subtitleFontSize: 15,
  40. toolTipPadding: 3,
  41. toolTipBackground: '#000000',
  42. toolTipOpacity: 0.7,
  43. toolTipLineHeight: 20,
  44. radarLabelTextMargin: 15,
  45. gaugeLabelTextMargin: 15
  46. };
  47. let assign = function (target, ...varArgs) {
  48. if (target == null) {
  49. throw new TypeError('Cannot convert undefined or null to object');
  50. }
  51. if (!varArgs || varArgs.length <= 0) {
  52. return target;
  53. }
  54. // 深度合并对象
  55. function deepAssign(obj1, obj2) {
  56. for (let key in obj2) {
  57. obj1[key] = obj1[key] && obj1[key].toString() === "[object Object]" ?
  58. deepAssign(obj1[key], obj2[key]) : obj1[key] = obj2[key];
  59. }
  60. return obj1;
  61. }
  62. varArgs.forEach(val => {
  63. target = deepAssign(target, val);
  64. });
  65. return target;
  66. };
  67. var util = {
  68. toFixed: function toFixed(num, limit) {
  69. limit = limit || 2;
  70. if (this.isFloat(num)) {
  71. num = num.toFixed(limit);
  72. }
  73. return num;
  74. },
  75. isFloat: function isFloat(num) {
  76. return num % 1 !== 0;
  77. },
  78. approximatelyEqual: function approximatelyEqual(num1, num2) {
  79. return Math.abs(num1 - num2) < 1e-10;
  80. },
  81. isSameSign: function isSameSign(num1, num2) {
  82. return Math.abs(num1) === num1 && Math.abs(num2) === num2 || Math.abs(num1) !== num1 && Math.abs(num2) !== num2;
  83. },
  84. isSameXCoordinateArea: function isSameXCoordinateArea(p1, p2) {
  85. return this.isSameSign(p1.x, p2.x);
  86. },
  87. isCollision: function isCollision(obj1, obj2) {
  88. obj1.end = {};
  89. obj1.end.x = obj1.start.x + obj1.width;
  90. obj1.end.y = obj1.start.y - obj1.height;
  91. obj2.end = {};
  92. obj2.end.x = obj2.start.x + obj2.width;
  93. obj2.end.y = obj2.start.y - obj2.height;
  94. var flag = obj2.start.x > obj1.end.x || obj2.end.x < obj1.start.x || obj2.end.y > obj1.start.y || obj2.start.y < obj1.end.y;
  95. return !flag;
  96. }
  97. };
  98. //兼容H5点击事件
  99. function getH5Offset(e) {
  100. e.mp = {
  101. changedTouches: []
  102. };
  103. e.mp.changedTouches.push({
  104. x: e.offsetX,
  105. y: e.offsetY
  106. });
  107. return e;
  108. }
  109. // hex 转 rgba
  110. function hexToRgb(hexValue, opc) {
  111. var rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  112. var hex = hexValue.replace(rgx, function(m, r, g, b) {
  113. return r + r + g + g + b + b;
  114. });
  115. var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  116. var r = parseInt(rgb[1], 16);
  117. var g = parseInt(rgb[2], 16);
  118. var b = parseInt(rgb[3], 16);
  119. return 'rgba(' + r + ',' + g + ',' + b + ',' + opc + ')';
  120. }
  121. function findRange(num, type, limit) {
  122. if (isNaN(num)) {
  123. throw new Error('[uCharts] unvalid series data!');
  124. }
  125. limit = limit || 10;
  126. type = type ? type : 'upper';
  127. var multiple = 1;
  128. while (limit < 1) {
  129. limit *= 10;
  130. multiple *= 10;
  131. }
  132. if (type === 'upper') {
  133. num = Math.ceil(num * multiple);
  134. } else {
  135. num = Math.floor(num * multiple);
  136. }
  137. while (num % limit !== 0) {
  138. if (type === 'upper') {
  139. num++;
  140. } else {
  141. num--;
  142. }
  143. }
  144. return num / multiple;
  145. }
  146. function calCandleMA(dayArr, nameArr, colorArr, kdata) {
  147. let seriesTemp = [];
  148. for (let k = 0; k < dayArr.length; k++) {
  149. let seriesItem = {
  150. data: [],
  151. name: nameArr[k],
  152. color: colorArr[k]
  153. };
  154. for (let i = 0, len = kdata.length; i < len; i++) {
  155. if (i < dayArr[k]) {
  156. seriesItem.data.push(null);
  157. continue;
  158. }
  159. let sum = 0;
  160. for (let j = 0; j < dayArr[k]; j++) {
  161. sum += kdata[i - j][1];
  162. }
  163. seriesItem.data.push(+(sum / dayArr[k]).toFixed(3));
  164. }
  165. seriesTemp.push(seriesItem);
  166. }
  167. return seriesTemp;
  168. }
  169. function calValidDistance(self,distance, chartData, config, opts) {
  170. var dataChartAreaWidth = opts.width - opts.area[1] - opts.area[3];
  171. var dataChartWidth = chartData.eachSpacing * (opts.chartData.xAxisData.xAxisPoints.length-1);
  172. var validDistance = distance;
  173. if (distance >= 0) {
  174. validDistance = 0;
  175. self.event.trigger('scrollLeft');
  176. } else if (Math.abs(distance) >= dataChartWidth - dataChartAreaWidth) {
  177. validDistance = dataChartAreaWidth - dataChartWidth;
  178. self.event.trigger('scrollRight');
  179. }
  180. return validDistance;
  181. }
  182. function isInAngleRange(angle, startAngle, endAngle) {
  183. function adjust(angle) {
  184. while (angle < 0) {
  185. angle += 2 * Math.PI;
  186. }
  187. while (angle > 2 * Math.PI) {
  188. angle -= 2 * Math.PI;
  189. }
  190. return angle;
  191. }
  192. angle = adjust(angle);
  193. startAngle = adjust(startAngle);
  194. endAngle = adjust(endAngle);
  195. if (startAngle > endAngle) {
  196. endAngle += 2 * Math.PI;
  197. if (angle < startAngle) {
  198. angle += 2 * Math.PI;
  199. }
  200. }
  201. return angle >= startAngle && angle <= endAngle;
  202. }
  203. function calRotateTranslate(x, y, h) {
  204. var xv = x;
  205. var yv = h - y;
  206. var transX = xv + (h - yv - xv) / Math.sqrt(2);
  207. transX *= -1;
  208. var transY = (h - yv) * (Math.sqrt(2) - 1) - (h - yv - xv) / Math.sqrt(2);
  209. return {
  210. transX: transX,
  211. transY: transY
  212. };
  213. }
  214. function createCurveControlPoints(points, i) {
  215. function isNotMiddlePoint(points, i) {
  216. if (points[i - 1] && points[i + 1]) {
  217. return points[i].y >= Math.max(points[i - 1].y, points[i + 1].y) || points[i].y <= Math.min(points[i - 1].y,points[i + 1].y);
  218. } else {
  219. return false;
  220. }
  221. }
  222. function isNotMiddlePointX(points, i) {
  223. if (points[i - 1] && points[i + 1]) {
  224. return points[i].x >= Math.max(points[i - 1].x, points[i + 1].x) || points[i].x <= Math.min(points[i - 1].x,points[i + 1].x);
  225. } else {
  226. return false;
  227. }
  228. }
  229. var a = 0.2;
  230. var b = 0.2;
  231. var pAx = null;
  232. var pAy = null;
  233. var pBx = null;
  234. var pBy = null;
  235. if (i < 1) {
  236. pAx = points[0].x + (points[1].x - points[0].x) * a;
  237. pAy = points[0].y + (points[1].y - points[0].y) * a;
  238. } else {
  239. pAx = points[i].x + (points[i + 1].x - points[i - 1].x) * a;
  240. pAy = points[i].y + (points[i + 1].y - points[i - 1].y) * a;
  241. }
  242. if (i > points.length - 3) {
  243. var last = points.length - 1;
  244. pBx = points[last].x - (points[last].x - points[last - 1].x) * b;
  245. pBy = points[last].y - (points[last].y - points[last - 1].y) * b;
  246. } else {
  247. pBx = points[i + 1].x - (points[i + 2].x - points[i].x) * b;
  248. pBy = points[i + 1].y - (points[i + 2].y - points[i].y) * b;
  249. }
  250. if (isNotMiddlePoint(points, i + 1)) {
  251. pBy = points[i + 1].y;
  252. }
  253. if (isNotMiddlePoint(points, i)) {
  254. pAy = points[i].y;
  255. }
  256. if (isNotMiddlePointX(points, i + 1)) {
  257. pBx = points[i + 1].x;
  258. }
  259. if (isNotMiddlePointX(points, i)) {
  260. pAx = points[i].x;
  261. }
  262. if (pAy >= Math.max(points[i].y, points[i + 1].y) || pAy <= Math.min(points[i].y, points[i + 1].y)) {
  263. pAy = points[i].y;
  264. }
  265. if (pBy >= Math.max(points[i].y, points[i + 1].y) || pBy <= Math.min(points[i].y, points[i + 1].y)) {
  266. pBy = points[i + 1].y;
  267. }
  268. if (pAx >= Math.max(points[i].x, points[i + 1].x) || pAx <= Math.min(points[i].x, points[i + 1].x)) {
  269. pAx = points[i].x;
  270. }
  271. if (pBx >= Math.max(points[i].x, points[i + 1].x) || pBx <= Math.min(points[i].x, points[i + 1].x)) {
  272. pBx = points[i + 1].x;
  273. }
  274. return {
  275. ctrA: {
  276. x: pAx,
  277. y: pAy
  278. },
  279. ctrB: {
  280. x: pBx,
  281. y: pBy
  282. }
  283. };
  284. }
  285. function convertCoordinateOrigin(x, y, center) {
  286. return {
  287. x: center.x + x,
  288. y: center.y - y
  289. };
  290. }
  291. function avoidCollision(obj, target) {
  292. if (target) {
  293. // is collision test
  294. while (util.isCollision(obj, target)) {
  295. if (obj.start.x > 0) {
  296. obj.start.y--;
  297. } else if (obj.start.x < 0) {
  298. obj.start.y++;
  299. } else {
  300. if (obj.start.y > 0) {
  301. obj.start.y++;
  302. } else {
  303. obj.start.y--;
  304. }
  305. }
  306. }
  307. }
  308. return obj;
  309. }
  310. function fillSeries(series, opts, config) {
  311. var index = 0;
  312. return series.map(function(item) {
  313. if (!item.color) {
  314. item.color = config.colors[index];
  315. index = (index + 1) % config.colors.length;
  316. }
  317. if (!item.index) {
  318. item.index = 0;
  319. }
  320. if (!item.type) {
  321. item.type = opts.type;
  322. }
  323. if (typeof item.show == "undefined") {
  324. item.show = true;
  325. }
  326. if (!item.type) {
  327. item.type = opts.type;
  328. }
  329. if (!item.pointShape) {
  330. item.pointShape = "circle";
  331. }
  332. if (!item.legendShape) {
  333. switch (item.type) {
  334. case 'line':
  335. item.legendShape = "line";
  336. break;
  337. case 'column':
  338. item.legendShape = "rect";
  339. break;
  340. case 'area':
  341. item.legendShape = "triangle";
  342. break;
  343. default:
  344. item.legendShape = "circle";
  345. }
  346. }
  347. return item;
  348. });
  349. }
  350. function getDataRange(minData, maxData) {
  351. var limit = 0;
  352. var range = maxData - minData;
  353. if (range >= 10000) {
  354. limit = 1000;
  355. } else if (range >= 1000) {
  356. limit = 100;
  357. } else if (range >= 100) {
  358. limit = 10;
  359. } else if (range >= 10) {
  360. limit = 5;
  361. } else if (range >= 1) {
  362. limit = 1;
  363. } else if (range >= 0.1) {
  364. limit = 0.1;
  365. } else if (range >= 0.01) {
  366. limit = 0.01;
  367. } else if (range >= 0.001) {
  368. limit = 0.001;
  369. } else if (range >= 0.0001) {
  370. limit = 0.0001;
  371. } else if (range >= 0.00001) {
  372. limit = 0.00001;
  373. } else {
  374. limit = 0.000001;
  375. }
  376. return {
  377. minRange: findRange(minData, 'lower', limit),
  378. maxRange: findRange(maxData, 'upper', limit)
  379. };
  380. }
  381. function measureText(text) {
  382. var fontSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : config.fontSize;
  383. text = String(text);
  384. var text = text.split('');
  385. var width = 0;
  386. for (let i = 0; i < text.length; i++) {
  387. let item = text[i];
  388. if (/[a-zA-Z]/.test(item)) {
  389. width += 7;
  390. } else if (/[0-9]/.test(item)) {
  391. width += 5.5;
  392. } else if (/\./.test(item)) {
  393. width += 2.7;
  394. } else if (/-/.test(item)) {
  395. width += 3.25;
  396. } else if (/[\u4e00-\u9fa5]/.test(item)) {
  397. width += 10;
  398. } else if (/\(|\)/.test(item)) {
  399. width += 3.73;
  400. } else if (/\s/.test(item)) {
  401. width += 2.5;
  402. } else if (/%/.test(item)) {
  403. width += 8;
  404. } else {
  405. width += 10;
  406. }
  407. }
  408. return width * fontSize / 10;
  409. }
  410. function dataCombine(series) {
  411. return series.reduce(function(a, b) {
  412. return (a.data ? a.data : a).concat(b.data);
  413. }, []);
  414. }
  415. function dataCombineStack(series, len) {
  416. var sum = new Array(len);
  417. for (var j = 0; j < sum.length; j++) {
  418. sum[j] = 0;
  419. }
  420. for (var i = 0; i < series.length; i++) {
  421. for (var j = 0; j < sum.length; j++) {
  422. sum[j] += series[i].data[j];
  423. }
  424. }
  425. return series.reduce(function(a, b) {
  426. return (a.data ? a.data : a).concat(b.data).concat(sum);
  427. }, []);
  428. }
  429. function getTouches(touches, opts, e) {
  430. let x, y;
  431. if (touches.clientX) {
  432. if (opts.rotate) {
  433. y = opts.height - touches.clientX * opts.pixelRatio;
  434. x = (touches.pageY - e.currentTarget.offsetTop - (opts.height / opts.pixelRatio / 2) * (opts.pixelRatio - 1)) * 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.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. // modify by yxk
  1693. radius -= gaugeOption.width / 2 + config.gaugeLabelTextMargin + opts.extra.labelRadius * opts.pixelRatio;
  1694. let totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  1695. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  1696. let totalNumber = gaugeOption.endNumber - gaugeOption.startNumber;
  1697. let splitNumber = totalNumber / gaugeOption.splitLine.splitNumber;
  1698. let nowAngle = gaugeOption.startAngle;
  1699. let nowNumber = gaugeOption.startNumber;
  1700. for (let i = 0; i < gaugeOption.splitLine.splitNumber + 1; i++) {
  1701. var pos = {
  1702. x: radius * Math.cos(nowAngle * Math.PI),
  1703. y: radius * Math.sin(nowAngle * Math.PI)
  1704. };
  1705. var labelText = gaugeOption.labelFormat ? gaugeOption.labelFormat(nowNumber) : nowNumber;
  1706. // modify by yxk
  1707. pos.x += centerPosition.x - measureText(labelText) / 2 - opts.extra.labelCenterPosition * opts.pixelRatio;
  1708. pos.y += centerPosition.y;
  1709. var startX = pos.x;
  1710. var startY = pos.y;
  1711. context.beginPath();
  1712. context.setFontSize(config.fontSize);
  1713. context.setFillStyle(gaugeOption.labelColor || '#666666');
  1714. context.fillText(labelText, startX, startY + config.fontSize / 2);
  1715. context.closePath();
  1716. context.stroke();
  1717. nowAngle += splitAngle;
  1718. if (nowAngle >= 2) {
  1719. nowAngle = nowAngle % 2;
  1720. }
  1721. nowNumber += splitNumber;
  1722. }
  1723. }
  1724. function drawRadarLabel(angleList, radius, centerPosition, opts, config, context) {
  1725. var radarOption = opts.extra.radar || {};
  1726. radius += config.radarLabelTextMargin;
  1727. angleList.forEach(function(angle, index) {
  1728. var pos = {
  1729. x: radius * Math.cos(angle),
  1730. y: radius * Math.sin(angle)
  1731. };
  1732. var posRelativeCanvas = convertCoordinateOrigin(pos.x, pos.y, centerPosition);
  1733. var startX = posRelativeCanvas.x;
  1734. var startY = posRelativeCanvas.y;
  1735. if (util.approximatelyEqual(pos.x, 0)) {
  1736. startX -= measureText(opts.categories[index] || '') / 2;
  1737. } else if (pos.x < 0) {
  1738. startX -= measureText(opts.categories[index] || '');
  1739. }
  1740. context.beginPath();
  1741. context.setFontSize(config.fontSize);
  1742. context.setFillStyle(radarOption.labelColor || '#666666');
  1743. context.fillText(opts.categories[index] || '', startX, startY + config.fontSize / 2);
  1744. context.closePath();
  1745. context.stroke();
  1746. });
  1747. }
  1748. function drawPieText(series, opts, config, context, radius, center) {
  1749. var lineRadius = config.pieChartLinePadding;
  1750. var textObjectCollection = [];
  1751. var lastTextObject = null;
  1752. var seriesConvert = series.map(function(item) {
  1753. var text = item.format ? item.format(+item._proportion_.toFixed(2)) : util.toFixed(item._proportion_.toFixed(4) * 100) +'%';
  1754. if(item._rose_proportion_) item._proportion_=item._rose_proportion_;
  1755. var arc = 2 * Math.PI - (item._start_ + 2 * Math.PI * item._proportion_ / 2);
  1756. var color = item.color;
  1757. var radius = item._radius_;
  1758. return {
  1759. arc: arc,
  1760. text: text,
  1761. color: color,
  1762. radius: radius,
  1763. textColor: item.textColor,
  1764. textSize: item.textSize,
  1765. };
  1766. });
  1767. for (let i = 0; i < seriesConvert.length; i++) {
  1768. let item = seriesConvert[i];
  1769. // line end
  1770. let orginX1 = Math.cos(item.arc) * (item.radius + lineRadius);
  1771. let orginY1 = Math.sin(item.arc) * (item.radius + lineRadius);
  1772. // line start
  1773. let orginX2 = Math.cos(item.arc) * item.radius;
  1774. let orginY2 = Math.sin(item.arc) * item.radius;
  1775. // text start
  1776. let orginX3 = orginX1 >= 0 ? orginX1 + config.pieChartTextPadding : orginX1 - config.pieChartTextPadding;
  1777. let orginY3 = orginY1;
  1778. let textWidth = measureText(item.text,item.textSize||config.fontSize);
  1779. let startY = orginY3;
  1780. if (lastTextObject && util.isSameXCoordinateArea(lastTextObject.start, {
  1781. x: orginX3
  1782. })) {
  1783. if (orginX3 > 0) {
  1784. startY = Math.min(orginY3, lastTextObject.start.y);
  1785. } else if (orginX1 < 0) {
  1786. startY = Math.max(orginY3, lastTextObject.start.y);
  1787. } else {
  1788. if (orginY3 > 0) {
  1789. startY = Math.max(orginY3, lastTextObject.start.y);
  1790. } else {
  1791. startY = Math.min(orginY3, lastTextObject.start.y);
  1792. }
  1793. }
  1794. }
  1795. if (orginX3 < 0) {
  1796. orginX3 -= textWidth;
  1797. }
  1798. let textObject = {
  1799. lineStart: {
  1800. x: orginX2,
  1801. y: orginY2
  1802. },
  1803. lineEnd: {
  1804. x: orginX1,
  1805. y: orginY1
  1806. },
  1807. start: {
  1808. x: orginX3,
  1809. y: startY
  1810. },
  1811. width: textWidth,
  1812. height: config.fontSize,
  1813. text: item.text,
  1814. color: item.color,
  1815. textColor: item.textColor,
  1816. textSize: item.textSize
  1817. };
  1818. lastTextObject = avoidCollision(textObject, lastTextObject);
  1819. textObjectCollection.push(lastTextObject);
  1820. }
  1821. for (let i = 0; i < textObjectCollection.length; i++) {
  1822. let item = textObjectCollection[i];
  1823. let lineStartPoistion = convertCoordinateOrigin(item.lineStart.x, item.lineStart.y, center);
  1824. let lineEndPoistion = convertCoordinateOrigin(item.lineEnd.x, item.lineEnd.y, center);
  1825. let textPosition = convertCoordinateOrigin(item.start.x, item.start.y, center);
  1826. context.setLineWidth(1 * opts.pixelRatio);
  1827. context.setFontSize(config.fontSize);
  1828. context.beginPath();
  1829. context.setStrokeStyle(item.color);
  1830. context.setFillStyle(item.color);
  1831. context.moveTo(lineStartPoistion.x, lineStartPoistion.y);
  1832. let curveStartX = item.start.x < 0 ? textPosition.x + item.width : textPosition.x;
  1833. let textStartX = item.start.x < 0 ? textPosition.x - 5 : textPosition.x + 5;
  1834. context.quadraticCurveTo(lineEndPoistion.x, lineEndPoistion.y, curveStartX, textPosition.y);
  1835. context.moveTo(lineStartPoistion.x, lineStartPoistion.y);
  1836. context.stroke();
  1837. context.closePath();
  1838. context.beginPath();
  1839. context.moveTo(textPosition.x + item.width, textPosition.y);
  1840. context.arc(curveStartX, textPosition.y, 2, 0, 2 * Math.PI);
  1841. context.closePath();
  1842. context.fill();
  1843. context.beginPath();
  1844. context.setFontSize(item.textSize || config.fontSize);
  1845. context.setFillStyle(item.textColor || '#666666');
  1846. context.fillText(item.text, textStartX, textPosition.y + 3);
  1847. context.closePath();
  1848. context.stroke();
  1849. context.closePath();
  1850. }
  1851. }
  1852. function drawToolTipSplitLine(offsetX, opts, config, context) {
  1853. var toolTipOption = opts.extra.tooltip || {};
  1854. toolTipOption.gridType = toolTipOption.gridType == undefined ? 'solid' : toolTipOption.gridType;
  1855. toolTipOption.dashLength = toolTipOption.dashLength == undefined ? 4 : toolTipOption.dashLength;
  1856. var startY = opts.area[0];
  1857. var endY = opts.height - opts.area[2];
  1858. if (toolTipOption.gridType == 'dash') {
  1859. context.setLineDash([toolTipOption.dashLength, toolTipOption.dashLength]);
  1860. }
  1861. context.setStrokeStyle(toolTipOption.gridColor || '#cccccc');
  1862. context.setLineWidth(1 * opts.pixelRatio);
  1863. context.beginPath();
  1864. context.moveTo(offsetX, startY);
  1865. context.lineTo(offsetX, endY);
  1866. context.stroke();
  1867. context.setLineDash([]);
  1868. if (toolTipOption.xAxisLabel) {
  1869. let labelText = opts.categories[opts.tooltip.index];
  1870. context.setFontSize(config.fontSize);
  1871. let textWidth = measureText(labelText, config.fontSize);
  1872. let textX = offsetX - 0.5 * textWidth;
  1873. let textY = endY;
  1874. context.beginPath();
  1875. context.setFillStyle(hexToRgb(toolTipOption.labelBgColor || config.toolTipBackground, toolTipOption.labelBgOpacity || config.toolTipOpacity));
  1876. context.setStrokeStyle(toolTipOption.labelBgColor || config.toolTipBackground);
  1877. context.setLineWidth(1 * opts.pixelRatio);
  1878. context.rect(textX - config.toolTipPadding, textY, textWidth + 2 * config.toolTipPadding, config.fontSize + 2 * config.toolTipPadding);
  1879. context.closePath();
  1880. context.stroke();
  1881. context.fill();
  1882. context.beginPath();
  1883. context.setFontSize(config.fontSize);
  1884. context.setFillStyle(toolTipOption.labelFontColor || config.fontColor);
  1885. context.fillText(String(labelText), textX, textY + config.toolTipPadding + config.fontSize);
  1886. context.closePath();
  1887. context.stroke();
  1888. }
  1889. }
  1890. function drawMarkLine(opts, config, context) {
  1891. let markLineOption = assign({}, {
  1892. type: 'solid',
  1893. dashLength: 4,
  1894. data: []
  1895. }, opts.extra.markLine);
  1896. let startX = opts.area[3];
  1897. let endX = opts.width - opts.area[1];
  1898. let points = calMarkLineData(markLineOption.data, opts);
  1899. for (let i = 0; i < points.length; i++) {
  1900. let item = assign({}, {
  1901. lineColor: '#DE4A42',
  1902. showLabel: false,
  1903. labelFontColor: '#666666',
  1904. labelBgColor: '#DFE8FF',
  1905. labelBgOpacity: 0.8,
  1906. yAxisIndex: 0
  1907. }, points[i]);
  1908. if (markLineOption.type == 'dash') {
  1909. context.setLineDash([markLineOption.dashLength, markLineOption.dashLength]);
  1910. }
  1911. context.setStrokeStyle(item.lineColor);
  1912. context.setLineWidth(1 * opts.pixelRatio);
  1913. context.beginPath();
  1914. context.moveTo(startX, item.y);
  1915. context.lineTo(endX, item.y);
  1916. context.stroke();
  1917. context.setLineDash([]);
  1918. if (item.showLabel) {
  1919. let labelText = opts.yAxis.format ? opts.yAxis.format(Number(item.value)) : item.value;
  1920. context.setFontSize(config.fontSize);
  1921. let textWidth = measureText(labelText, config.fontSize);
  1922. let bgStartX = opts.padding[3] + config.yAxisTitleWidth - config.toolTipPadding;
  1923. let bgEndX = Math.max(opts.area[3], textWidth + config.toolTipPadding * 2);
  1924. let bgWidth = bgEndX - bgStartX;
  1925. let textX = bgStartX + (bgWidth - textWidth) / 2;
  1926. let textY = item.y;
  1927. context.setFillStyle(hexToRgb(item.labelBgColor, item.labelBgOpacity));
  1928. context.setStrokeStyle(item.labelBgColor);
  1929. context.setLineWidth(1 * opts.pixelRatio);
  1930. context.beginPath();
  1931. context.rect(bgStartX, textY - 0.5 * config.fontSize - config.toolTipPadding, bgWidth, config.fontSize + 2 * config.toolTipPadding);
  1932. context.closePath();
  1933. context.stroke();
  1934. context.fill();
  1935. context.beginPath();
  1936. context.setFontSize(config.fontSize);
  1937. context.setFillStyle(item.labelFontColor);
  1938. context.fillText(String(labelText), textX, textY + 0.5 * config.fontSize);
  1939. context.stroke();
  1940. }
  1941. }
  1942. }
  1943. function drawToolTipHorizentalLine(opts, config, context, eachSpacing, xAxisPoints) {
  1944. var toolTipOption = assign({}, {
  1945. gridType: 'solid',
  1946. dashLength: 4
  1947. }, opts.extra.tooltip);
  1948. var startX = opts.area[3];
  1949. var endX = opts.width - opts.area[1];
  1950. if (toolTipOption.gridType == 'dash') {
  1951. context.setLineDash([toolTipOption.dashLength, toolTipOption.dashLength]);
  1952. }
  1953. context.setStrokeStyle(toolTipOption.gridColor || '#cccccc');
  1954. context.setLineWidth(1 * opts.pixelRatio);
  1955. context.beginPath();
  1956. context.moveTo(startX, opts.tooltip.offset.y);
  1957. context.lineTo(endX, opts.tooltip.offset.y);
  1958. context.stroke();
  1959. context.setLineDash([]);
  1960. if (toolTipOption.yAxisLabel) {
  1961. let labelText = calTooltipYAxisData(opts.tooltip.offset.y, opts.series, opts, config, eachSpacing);
  1962. let widthArr = opts.chartData.yAxisData.yAxisWidth;
  1963. let tStartLeft=opts.area[3];
  1964. let tStartRight=opts.width-opts.area[1];
  1965. for(let i=0;i<labelText.length;i++){
  1966. context.setFontSize(config.fontSize);
  1967. let textWidth = measureText(labelText[i], config.fontSize);
  1968. let bgStartX,bgEndX,bgWidth;
  1969. if(widthArr[i].position == 'left'){
  1970. bgStartX = tStartLeft - widthArr[i].width;
  1971. bgEndX = Math.max(bgStartX, bgStartX + textWidth + config.toolTipPadding * 2);
  1972. }else{
  1973. bgStartX = tStartRight;
  1974. bgEndX = Math.max(bgStartX + widthArr[i].width, bgStartX + textWidth + config.toolTipPadding * 2);
  1975. }
  1976. bgWidth = bgEndX - bgStartX;
  1977. let textX = bgStartX + (bgWidth - textWidth) / 2;
  1978. let textY = opts.tooltip.offset.y;
  1979. context.beginPath();
  1980. context.setFillStyle(hexToRgb(toolTipOption.labelBgColor || config.toolTipBackground, toolTipOption.labelBgOpacity || config.toolTipOpacity));
  1981. context.setStrokeStyle(toolTipOption.labelBgColor || config.toolTipBackground);
  1982. context.setLineWidth(1 * opts.pixelRatio);
  1983. context.rect(bgStartX, textY - 0.5 * config.fontSize - config.toolTipPadding, bgWidth, config.fontSize + 2 * config.toolTipPadding);
  1984. context.closePath();
  1985. context.stroke();
  1986. context.fill();
  1987. context.beginPath();
  1988. context.setFontSize(config.fontSize);
  1989. context.setFillStyle(toolTipOption.labelFontColor || config.fontColor);
  1990. context.fillText(labelText[i], textX, textY + 0.5 * config.fontSize);
  1991. context.closePath();
  1992. context.stroke();
  1993. if(widthArr[i].position == 'left'){
  1994. tStartLeft -=(widthArr[i].width + opts.yAxis.padding);
  1995. }else{
  1996. tStartRight +=widthArr[i].width+ opts.yAxis.padding;
  1997. }
  1998. }
  1999. }
  2000. }
  2001. function drawToolTipSplitArea(offsetX, opts, config, context, eachSpacing) {
  2002. var toolTipOption = assign({}, {
  2003. activeBgColor: '#000000',
  2004. activeBgOpacity: 0.08
  2005. }, opts.extra.tooltip);
  2006. var startY = opts.area[0];
  2007. var endY = opts.height - opts.area[2];
  2008. context.beginPath();
  2009. context.setFillStyle(hexToRgb(toolTipOption.activeBgColor, toolTipOption.activeBgOpacity));
  2010. context.rect(offsetX - eachSpacing / 2, startY, eachSpacing, endY - startY);
  2011. context.closePath();
  2012. context.fill();
  2013. }
  2014. function drawToolTip(textList, offset, opts, config, context, eachSpacing, xAxisPoints) {
  2015. var toolTipOption = assign({}, {
  2016. showBox:true,
  2017. bgColor: '#000000',
  2018. bgOpacity: 0.7,
  2019. fontColor: '#FFFFFF'
  2020. }, opts.extra.tooltip);
  2021. var legendWidth = 4 * opts.pixelRatio;
  2022. var legendMarginRight = 5 * opts.pixelRatio;
  2023. var arrowWidth = 8 * opts.pixelRatio;
  2024. var isOverRightBorder = false;
  2025. if (opts.type == 'line' || opts.type == 'area' || opts.type == 'candle' || opts.type == 'mix') {
  2026. drawToolTipSplitLine(opts.tooltip.offset.x, opts, config, context);
  2027. }
  2028. offset = assign({
  2029. x: 0,
  2030. y: 0
  2031. }, offset);
  2032. offset.y -= 8 * opts.pixelRatio;
  2033. var textWidth = textList.map(function(item) {
  2034. return measureText(item.text, config.fontSize);
  2035. });
  2036. var toolTipWidth = legendWidth + legendMarginRight + 4 * config.toolTipPadding + Math.max.apply(null, textWidth);
  2037. var toolTipHeight = 2 * config.toolTipPadding + textList.length * config.toolTipLineHeight;
  2038. if(toolTipOption.showBox == false){ return }
  2039. // if beyond the right border
  2040. if (offset.x - Math.abs(opts._scrollDistance_) + arrowWidth + toolTipWidth > opts.width) {
  2041. isOverRightBorder = true;
  2042. }
  2043. if (toolTipHeight + offset.y > opts.height) {
  2044. offset.y = opts.height - toolTipHeight;
  2045. }
  2046. // draw background rect
  2047. context.beginPath();
  2048. context.setFillStyle(hexToRgb(toolTipOption.bgColor || config.toolTipBackground, toolTipOption.bgOpacity || config.toolTipOpacity));
  2049. if (isOverRightBorder) {
  2050. context.moveTo(offset.x, offset.y + 10 * opts.pixelRatio);
  2051. context.lineTo(offset.x - arrowWidth, offset.y + 10 * opts.pixelRatio - 5 * opts.pixelRatio);
  2052. context.lineTo(offset.x - arrowWidth, offset.y);
  2053. context.lineTo(offset.x - arrowWidth - Math.round(toolTipWidth), offset.y);
  2054. context.lineTo(offset.x - arrowWidth - Math.round(toolTipWidth), offset.y + toolTipHeight);
  2055. context.lineTo(offset.x - arrowWidth, offset.y + toolTipHeight);
  2056. context.lineTo(offset.x - arrowWidth, offset.y + 10 * opts.pixelRatio + 5 * opts.pixelRatio);
  2057. context.lineTo(offset.x, offset.y + 10 * opts.pixelRatio);
  2058. } else {
  2059. context.moveTo(offset.x, offset.y + 10 * opts.pixelRatio);
  2060. context.lineTo(offset.x + arrowWidth, offset.y + 10 * opts.pixelRatio - 5 * opts.pixelRatio);
  2061. context.lineTo(offset.x + arrowWidth, offset.y);
  2062. context.lineTo(offset.x + arrowWidth + Math.round(toolTipWidth), offset.y);
  2063. context.lineTo(offset.x + arrowWidth + Math.round(toolTipWidth), offset.y + toolTipHeight);
  2064. context.lineTo(offset.x + arrowWidth, offset.y + toolTipHeight);
  2065. context.lineTo(offset.x + arrowWidth, offset.y + 10 * opts.pixelRatio + 5 * opts.pixelRatio);
  2066. context.lineTo(offset.x, offset.y + 10 * opts.pixelRatio);
  2067. }
  2068. context.closePath();
  2069. context.fill();
  2070. // draw legend
  2071. textList.forEach(function(item, index) {
  2072. if (item.color !== null) {
  2073. context.beginPath();
  2074. context.setFillStyle(item.color);
  2075. var startX = offset.x + arrowWidth + 2 * config.toolTipPadding;
  2076. var startY = offset.y + (config.toolTipLineHeight - config.fontSize) / 2 + config.toolTipLineHeight * index +
  2077. config.toolTipPadding + 1;
  2078. if (isOverRightBorder) {
  2079. startX = offset.x - toolTipWidth - arrowWidth + 2 * config.toolTipPadding;
  2080. }
  2081. context.fillRect(startX, startY, legendWidth, config.fontSize);
  2082. context.closePath();
  2083. }
  2084. });
  2085. // draw text list
  2086. textList.forEach(function(item, index) {
  2087. var startX = offset.x + arrowWidth + 2 * config.toolTipPadding + legendWidth + legendMarginRight;
  2088. if (isOverRightBorder) {
  2089. startX = offset.x - toolTipWidth - arrowWidth + 2 * config.toolTipPadding + +legendWidth + legendMarginRight;
  2090. }
  2091. var startY = offset.y + (config.toolTipLineHeight - config.fontSize) / 2 + config.toolTipLineHeight * index +
  2092. config.toolTipPadding;
  2093. context.beginPath();
  2094. context.setFontSize(config.fontSize);
  2095. context.setFillStyle(toolTipOption.fontColor);
  2096. context.fillText(item.text, startX, startY + config.fontSize);
  2097. context.closePath();
  2098. context.stroke();
  2099. });
  2100. }
  2101. function drawYAxisTitle(title, opts, config, context) {
  2102. var startX = config.xAxisHeight + (opts.height - config.xAxisHeight - measureText(title)) / 2;
  2103. context.save();
  2104. context.beginPath();
  2105. context.setFontSize(config.fontSize);
  2106. context.setFillStyle(opts.yAxis.titleFontColor || '#333333');
  2107. context.translate(0, opts.height);
  2108. context.rotate(-90 * Math.PI / 180);
  2109. context.fillText(title, startX, opts.padding[3] + 0.5 * config.fontSize);
  2110. context.closePath();
  2111. context.stroke();
  2112. context.restore();
  2113. }
  2114. function drawColumnDataPoints(series, opts, config, context) {
  2115. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2116. let xAxisData = opts.chartData.xAxisData,
  2117. xAxisPoints = xAxisData.xAxisPoints,
  2118. eachSpacing = xAxisData.eachSpacing;
  2119. let columnOption = assign({}, {
  2120. type: 'group',
  2121. width: eachSpacing / 2,
  2122. meter: {
  2123. border: 4,
  2124. fillColor: '#FFFFFF'
  2125. }
  2126. }, opts.extra.column);
  2127. let calPoints = [];
  2128. context.save();
  2129. let leftNum=-2;
  2130. let rightNum=xAxisPoints.length+2;
  2131. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2132. context.translate(opts._scrollDistance_, 0);
  2133. leftNum=Math.floor(-opts._scrollDistance_/eachSpacing)-2;
  2134. rightNum=leftNum+opts.xAxis.itemCount+4;
  2135. }
  2136. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  2137. drawToolTipSplitArea(opts.tooltip.offset.x, opts, config, context, eachSpacing);
  2138. }
  2139. series.forEach(function(eachSeries, seriesIndex) {
  2140. let ranges,minRange,maxRange;
  2141. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2142. minRange = ranges.pop();
  2143. maxRange = ranges.shift();
  2144. var data = eachSeries.data;
  2145. switch (columnOption.type) {
  2146. case 'group':
  2147. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2148. var tooltipPoints = getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, seriesIndex, series, process);
  2149. calPoints.push(tooltipPoints);
  2150. points = fixColumeData(points, eachSpacing, series.length, seriesIndex, config, opts);
  2151. for(let i=0;i<points.length;i++){
  2152. let item=points[i];
  2153. if (item !== null && i>leftNum && i<rightNum) {
  2154. context.beginPath();
  2155. context.setStrokeStyle(item.color || eachSeries.color);
  2156. context.setLineWidth(1)
  2157. context.setFillStyle(item.color || eachSeries.color);
  2158. var startX = item.x - item.width / 2;
  2159. var height = opts.height - item.y - opts.area[2];
  2160. context.moveTo(startX, item.y);
  2161. context.lineTo(startX+item.width-2,item.y);
  2162. context.lineTo(startX+item.width-2,opts.height - opts.area[2]);
  2163. context.lineTo(startX,opts.height - opts.area[2]);
  2164. context.lineTo(startX,item.y);
  2165. context.closePath();
  2166. context.stroke();
  2167. context.fill();
  2168. }
  2169. };
  2170. break;
  2171. case 'stack':
  2172. // 绘制堆叠数据图
  2173. var points = getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, seriesIndex, series, process);
  2174. calPoints.push(points);
  2175. points = fixColumeStackData(points, eachSpacing, series.length, seriesIndex, config, opts, series);
  2176. for(let i=0;i<points.length;i++){
  2177. let item=points[i];
  2178. if (item !== null && i>leftNum && i<rightNum) {
  2179. context.beginPath();
  2180. context.setFillStyle(item.color || eachSeries.color);
  2181. var startX = item.x - item.width / 2 + 1;
  2182. var height = opts.height - item.y - opts.area[2];
  2183. var height0 = opts.height - item.y0 - opts.area[2];
  2184. if (seriesIndex > 0) {
  2185. height -= height0;
  2186. }
  2187. context.moveTo(startX, item.y);
  2188. context.fillRect(startX, item.y, item.width - 2, height);
  2189. context.closePath();
  2190. context.fill();
  2191. }
  2192. };
  2193. break;
  2194. case 'meter':
  2195. // 绘制温度计数据图
  2196. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2197. calPoints.push(points);
  2198. points = fixColumeMeterData(points, eachSpacing, series.length, seriesIndex, config, opts, columnOption.meter.border);
  2199. if (seriesIndex == 0) {
  2200. for(let i=0;i<points.length;i++){
  2201. let item=points[i];
  2202. if (item !== null && i>leftNum && i<rightNum) {
  2203. //画背景颜色
  2204. context.beginPath();
  2205. context.setFillStyle(columnOption.meter.fillColor);
  2206. var startX = item.x - item.width / 2;
  2207. var height = opts.height - item.y - opts.area[2];
  2208. context.moveTo(startX, item.y);
  2209. context.fillRect(startX, item.y, item.width, height);
  2210. context.closePath();
  2211. context.fill();
  2212. //画边框线
  2213. if (columnOption.meter.border > 0) {
  2214. context.beginPath();
  2215. context.setStrokeStyle(eachSeries.color);
  2216. context.setLineWidth(columnOption.meter.border * opts.pixelRatio);
  2217. context.moveTo(startX + columnOption.meter.border * 0.5, item.y + height);
  2218. context.lineTo(startX + columnOption.meter.border * 0.5, item.y + columnOption.meter.border * 0.5);
  2219. context.lineTo(startX + item.width - columnOption.meter.border * 0.5, item.y + columnOption.meter.border * 0.5);
  2220. context.lineTo(startX + item.width - columnOption.meter.border * 0.5, item.y + height);
  2221. context.stroke();
  2222. }
  2223. }
  2224. };
  2225. } else {
  2226. for(let i=0;i<points.length;i++){
  2227. let item=points[i];
  2228. if (item !== null && i>leftNum && i<rightNum) {
  2229. context.beginPath();
  2230. context.setFillStyle(item.color || eachSeries.color);
  2231. var startX = item.x - item.width / 2;
  2232. var height = opts.height - item.y - opts.area[2];
  2233. context.moveTo(startX, item.y);
  2234. context.fillRect(startX, item.y, item.width, height);
  2235. context.closePath();
  2236. context.fill();
  2237. }
  2238. };
  2239. }
  2240. break;
  2241. }
  2242. });
  2243. if (opts.dataLabel !== false && process === 1) {
  2244. series.forEach(function(eachSeries, seriesIndex) {
  2245. let ranges,minRange,maxRange;
  2246. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2247. minRange = ranges.pop();
  2248. maxRange = ranges.shift();
  2249. var data = eachSeries.data;
  2250. switch (columnOption.type) {
  2251. case 'group':
  2252. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2253. points = fixColumeData(points, eachSpacing, series.length, seriesIndex, config, opts);
  2254. drawPointText(points, eachSeries, config, context);
  2255. break;
  2256. case 'stack':
  2257. var points = getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, seriesIndex, series, process);
  2258. drawPointText(points, eachSeries, config, context);
  2259. break;
  2260. case 'meter':
  2261. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2262. drawPointText(points, eachSeries, config, context);
  2263. break;
  2264. }
  2265. });
  2266. }
  2267. context.restore();
  2268. return {
  2269. xAxisPoints: xAxisPoints,
  2270. calPoints: calPoints,
  2271. eachSpacing: eachSpacing
  2272. };
  2273. }
  2274. function drawCandleDataPoints(series, seriesMA, opts, config, context) {
  2275. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  2276. var candleOption = assign({}, {
  2277. color: {},
  2278. average: {}
  2279. }, opts.extra.candle);
  2280. candleOption.color = assign({}, {
  2281. upLine: '#f04864',
  2282. upFill: '#f04864',
  2283. downLine: '#2fc25b',
  2284. downFill: '#2fc25b'
  2285. }, candleOption.color);
  2286. candleOption.average = assign({}, {
  2287. show: false,
  2288. name: [],
  2289. day: [],
  2290. color: config.colors
  2291. }, candleOption.average);
  2292. opts.extra.candle = candleOption;
  2293. let xAxisData = opts.chartData.xAxisData,
  2294. xAxisPoints = xAxisData.xAxisPoints,
  2295. eachSpacing = xAxisData.eachSpacing;
  2296. let calPoints = [];
  2297. context.save();
  2298. let leftNum=-2;
  2299. let rightNum=xAxisPoints.length+2;
  2300. let leftSpace=0;
  2301. let rightSpace=opts.width+eachSpacing;
  2302. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2303. context.translate(opts._scrollDistance_, 0);
  2304. leftNum=Math.floor(-opts._scrollDistance_/eachSpacing)-2;
  2305. rightNum=leftNum+opts.xAxis.itemCount+4;
  2306. leftSpace=-opts._scrollDistance_-eachSpacing+opts.area[3];
  2307. rightSpace=leftSpace+(opts.xAxis.itemCount+4)*eachSpacing;
  2308. }
  2309. //画均线
  2310. if (candleOption.average.show || seriesMA) { //Merge pull request !12 from 邱贵翔
  2311. seriesMA.forEach(function(eachSeries, seriesIndex) {
  2312. let ranges,minRange,maxRange;
  2313. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2314. minRange = ranges.pop();
  2315. maxRange = ranges.shift();
  2316. var data = eachSeries.data;
  2317. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2318. var splitPointList = splitPoints(points);
  2319. for(let i=0;i<splitPointList.length;i++){
  2320. let points=splitPointList[i];
  2321. context.beginPath();
  2322. context.setStrokeStyle(eachSeries.color);
  2323. context.setLineWidth(1);
  2324. if (points.length === 1) {
  2325. context.moveTo(points[0].x, points[0].y);
  2326. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  2327. } else {
  2328. context.moveTo(points[0].x, points[0].y);
  2329. let startPoint=0;
  2330. for(let j=0;j<points.length;j++){
  2331. let item=points[j];
  2332. if(startPoint==0 && item.x > leftSpace){
  2333. context.moveTo(item.x, item.y);
  2334. startPoint=1;
  2335. }
  2336. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2337. var ctrlPoint = createCurveControlPoints(points, j - 1);
  2338. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y,item.x, item.y);
  2339. }
  2340. }
  2341. context.moveTo(points[0].x, points[0].y);
  2342. }
  2343. context.closePath();
  2344. context.stroke();
  2345. }
  2346. });
  2347. }
  2348. //画K线
  2349. series.forEach(function(eachSeries, seriesIndex) {
  2350. let ranges,minRange,maxRange;
  2351. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2352. minRange = ranges.pop();
  2353. maxRange = ranges.shift();
  2354. var data = eachSeries.data;
  2355. var points = getCandleDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2356. calPoints.push(points);
  2357. var splitPointList = splitPoints(points);
  2358. for(let i=0;i<splitPointList[0].length;i++){
  2359. if(i>leftNum && i<rightNum){
  2360. let item=splitPointList[0][i];
  2361. context.beginPath();
  2362. //如果上涨
  2363. if (data[i][1] - data[i][0] > 0) {
  2364. context.setStrokeStyle(candleOption.color.upLine);
  2365. context.setFillStyle(candleOption.color.upFill);
  2366. context.setLineWidth(1 * opts.pixelRatio);
  2367. context.moveTo(item[3].x, item[3].y); //顶点
  2368. context.lineTo(item[1].x, item[1].y); //收盘中间点
  2369. context.lineTo(item[1].x - eachSpacing / 4, item[1].y); //收盘左侧点
  2370. context.lineTo(item[0].x - eachSpacing / 4, item[0].y); //开盘左侧点
  2371. context.lineTo(item[0].x, item[0].y); //开盘中间点
  2372. context.lineTo(item[2].x, item[2].y); //底点
  2373. context.lineTo(item[0].x, item[0].y); //开盘中间点
  2374. context.lineTo(item[0].x + eachSpacing / 4, item[0].y); //开盘右侧点
  2375. context.lineTo(item[1].x + eachSpacing / 4, item[1].y); //收盘右侧点
  2376. context.lineTo(item[1].x, item[1].y); //收盘中间点
  2377. context.moveTo(item[3].x, item[3].y); //顶点
  2378. } else {
  2379. context.setStrokeStyle(candleOption.color.downLine);
  2380. context.setFillStyle(candleOption.color.downFill);
  2381. context.setLineWidth(1 * opts.pixelRatio);
  2382. context.moveTo(item[3].x, item[3].y); //顶点
  2383. context.lineTo(item[0].x, item[0].y); //开盘中间点
  2384. context.lineTo(item[0].x - eachSpacing / 4, item[0].y); //开盘左侧点
  2385. context.lineTo(item[1].x - eachSpacing / 4, item[1].y); //收盘左侧点
  2386. context.lineTo(item[1].x, item[1].y); //收盘中间点
  2387. context.lineTo(item[2].x, item[2].y); //底点
  2388. context.lineTo(item[1].x, item[1].y); //收盘中间点
  2389. context.lineTo(item[1].x + eachSpacing / 4, item[1].y); //收盘右侧点
  2390. context.lineTo(item[0].x + eachSpacing / 4, item[0].y); //开盘右侧点
  2391. context.lineTo(item[0].x, item[0].y); //开盘中间点
  2392. context.moveTo(item[3].x, item[3].y); //顶点
  2393. }
  2394. context.closePath();
  2395. context.fill();
  2396. context.stroke();
  2397. }
  2398. }
  2399. });
  2400. context.restore();
  2401. return {
  2402. xAxisPoints: xAxisPoints,
  2403. calPoints: calPoints,
  2404. eachSpacing: eachSpacing
  2405. };
  2406. }
  2407. function drawAreaDataPoints(series, opts, config, context) {
  2408. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2409. var areaOption = assign({},{
  2410. type: 'straight',
  2411. opacity: 0.2,
  2412. addLine: false,
  2413. width: 2,
  2414. gradient:false
  2415. },opts.extra.area);
  2416. let xAxisData = opts.chartData.xAxisData,
  2417. xAxisPoints = xAxisData.xAxisPoints,
  2418. eachSpacing = xAxisData.eachSpacing;
  2419. let endY = opts.height - opts.area[2];
  2420. let calPoints = [];
  2421. context.save();
  2422. let leftSpace=0;
  2423. let rightSpace=opts.width+eachSpacing;
  2424. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2425. context.translate(opts._scrollDistance_, 0);
  2426. leftSpace=-opts._scrollDistance_-eachSpacing+opts.area[3];
  2427. rightSpace=leftSpace+(opts.xAxis.itemCount+4)*eachSpacing;
  2428. }
  2429. series.forEach(function(eachSeries, seriesIndex) {
  2430. let ranges,minRange,maxRange;
  2431. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2432. minRange = ranges.pop();
  2433. maxRange = ranges.shift();
  2434. let data = eachSeries.data;
  2435. let points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2436. calPoints.push(points);
  2437. let splitPointList = splitPoints(points);
  2438. for (let i = 0; i < splitPointList.length; i++) {
  2439. let points = splitPointList[i];
  2440. // 绘制区域数
  2441. context.beginPath();
  2442. context.setStrokeStyle(hexToRgb(eachSeries.color, areaOption.opacity));
  2443. if(areaOption.gradient){
  2444. let gradient = context.createLinearGradient(0, opts.area[0], 0, opts.height-opts.area[2]);
  2445. gradient.addColorStop('0', hexToRgb(eachSeries.color, areaOption.opacity));
  2446. gradient.addColorStop('1.0',hexToRgb("#FFFFFF", 0.1));
  2447. context.setFillStyle(gradient);
  2448. }else{
  2449. context.setFillStyle(hexToRgb(eachSeries.color, areaOption.opacity));
  2450. }
  2451. context.setLineWidth(areaOption.width * opts.pixelRatio);
  2452. if (points.length > 1) {
  2453. let firstPoint = points[0];
  2454. let lastPoint = points[points.length - 1];
  2455. context.moveTo(firstPoint.x, firstPoint.y);
  2456. let startPoint=0;
  2457. if (areaOption.type === 'curve') {
  2458. for(let j=0;j<points.length;j++){
  2459. let item=points[j];
  2460. if(startPoint==0 && item.x > leftSpace){
  2461. context.moveTo(item.x, item.y);
  2462. startPoint=1;
  2463. }
  2464. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2465. let ctrlPoint = createCurveControlPoints(points, j - 1);
  2466. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y,item.x, item.y);
  2467. }
  2468. };
  2469. } else {
  2470. for(let j=0;j<points.length;j++){
  2471. let item=points[j];
  2472. if(startPoint==0 && item.x > leftSpace){
  2473. context.moveTo(item.x, item.y);
  2474. startPoint=1;
  2475. }
  2476. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2477. context.lineTo(item.x, item.y);
  2478. }
  2479. };
  2480. }
  2481. context.lineTo(lastPoint.x, endY);
  2482. context.lineTo(firstPoint.x, endY);
  2483. context.lineTo(firstPoint.x, firstPoint.y);
  2484. } else {
  2485. let item = points[0];
  2486. context.moveTo(item.x - eachSpacing / 2, item.y);
  2487. context.lineTo(item.x + eachSpacing / 2, item.y);
  2488. context.lineTo(item.x + eachSpacing / 2, endY);
  2489. context.lineTo(item.x - eachSpacing / 2, endY);
  2490. context.moveTo(item.x - eachSpacing / 2, item.y);
  2491. }
  2492. context.closePath();
  2493. context.fill();
  2494. //画连线
  2495. if (areaOption.addLine) {
  2496. if (eachSeries.lineType == 'dash') {
  2497. let dashLength = eachSeries.dashLength?eachSeries.dashLength:8;
  2498. dashLength *= opts.pixelRatio;
  2499. context.setLineDash([dashLength, dashLength]);
  2500. }
  2501. context.beginPath();
  2502. context.setStrokeStyle(eachSeries.color);
  2503. context.setLineWidth(areaOption.width * opts.pixelRatio);
  2504. if (points.length === 1) {
  2505. context.moveTo(points[0].x, points[0].y);
  2506. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  2507. } else {
  2508. context.moveTo(points[0].x, points[0].y);
  2509. let startPoint=0;
  2510. if (areaOption.type === 'curve') {
  2511. for(let j=0;j<points.length;j++){
  2512. let item=points[j];
  2513. if(startPoint==0 && item.x > leftSpace){
  2514. context.moveTo(item.x, item.y);
  2515. startPoint=1;
  2516. }
  2517. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2518. let ctrlPoint = createCurveControlPoints(points, j - 1);
  2519. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y,item.x,item.y);
  2520. }
  2521. };
  2522. } else {
  2523. for(let j=0;j<points.length;j++){
  2524. let item=points[j];
  2525. if(startPoint==0 && item.x > leftSpace){
  2526. context.moveTo(item.x, item.y);
  2527. startPoint=1;
  2528. }
  2529. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2530. context.lineTo(item.x, item.y);
  2531. }
  2532. };
  2533. }
  2534. context.moveTo(points[0].x, points[0].y);
  2535. }
  2536. context.stroke();
  2537. context.setLineDash([]);
  2538. }
  2539. }
  2540. //画点
  2541. if (opts.dataPointShape !== false) {
  2542. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  2543. }
  2544. });
  2545. if (opts.dataLabel !== false && process === 1) {
  2546. series.forEach(function(eachSeries, seriesIndex) {
  2547. let ranges,minRange,maxRange;
  2548. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2549. minRange = ranges.pop();
  2550. maxRange = ranges.shift();
  2551. var data = eachSeries.data;
  2552. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2553. drawPointText(points, eachSeries, config, context);
  2554. });
  2555. }
  2556. context.restore();
  2557. return {
  2558. xAxisPoints: xAxisPoints,
  2559. calPoints: calPoints,
  2560. eachSpacing: eachSpacing
  2561. };
  2562. }
  2563. function drawLineDataPoints(series, opts, config, context) {
  2564. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2565. var lineOption = assign({},{
  2566. type: 'straight',
  2567. width: 2
  2568. },opts.extra.line);
  2569. lineOption.width *=opts.pixelRatio;
  2570. let xAxisData = opts.chartData.xAxisData,
  2571. xAxisPoints = xAxisData.xAxisPoints,
  2572. eachSpacing = xAxisData.eachSpacing;
  2573. var calPoints = [];
  2574. context.save();
  2575. let leftSpace=0;
  2576. let rightSpace=opts.width+eachSpacing;
  2577. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2578. context.translate(opts._scrollDistance_, 0);
  2579. leftSpace=-opts._scrollDistance_-eachSpacing+opts.area[3];
  2580. rightSpace=leftSpace+(opts.xAxis.itemCount+4)*eachSpacing;
  2581. }
  2582. series.forEach(function(eachSeries, seriesIndex) {
  2583. let ranges,minRange,maxRange;
  2584. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2585. minRange = ranges.pop();
  2586. maxRange = ranges.shift();
  2587. var data = eachSeries.data;
  2588. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2589. calPoints.push(points);
  2590. var splitPointList = splitPoints(points);
  2591. if (eachSeries.lineType == 'dash') {
  2592. let dashLength = eachSeries.dashLength?eachSeries.dashLength:8;
  2593. dashLength *= opts.pixelRatio;
  2594. context.setLineDash([dashLength, dashLength]);
  2595. }
  2596. context.beginPath();
  2597. context.setStrokeStyle(eachSeries.color);
  2598. context.setLineWidth(lineOption.width);
  2599. splitPointList.forEach(function(points, index) {
  2600. if (points.length === 1) {
  2601. context.moveTo(points[0].x, points[0].y);
  2602. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  2603. } else {
  2604. context.moveTo(points[0].x, points[0].y);
  2605. let startPoint=0;
  2606. if (lineOption.type === 'curve') {
  2607. for(let j=0;j<points.length;j++){
  2608. let item=points[j];
  2609. if(startPoint==0 && item.x > leftSpace){
  2610. context.moveTo(item.x, item.y);
  2611. startPoint=1;
  2612. }
  2613. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2614. var ctrlPoint = createCurveControlPoints(points, j - 1);
  2615. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y,item.x, item.y);
  2616. }
  2617. };
  2618. }
  2619. if (lineOption.type === 'straight') {
  2620. for(let j=0;j<points.length;j++){
  2621. let item=points[j];
  2622. if(startPoint==0 && item.x > leftSpace){
  2623. context.moveTo(item.x, item.y);
  2624. startPoint=1;
  2625. }
  2626. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2627. context.lineTo(item.x, item.y);
  2628. }
  2629. };
  2630. }
  2631. if (lineOption.type === 'step') {
  2632. for(let j=0;j<points.length;j++){
  2633. let item=points[j];
  2634. if(startPoint==0 && item.x > leftSpace){
  2635. context.moveTo(item.x, item.y);
  2636. startPoint=1;
  2637. }
  2638. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2639. context.lineTo(item.x, points[j-1].y);
  2640. context.lineTo(item.x, item.y);
  2641. }
  2642. };
  2643. }
  2644. context.moveTo(points[0].x, points[0].y);
  2645. }
  2646. });
  2647. context.stroke();
  2648. context.setLineDash([]);
  2649. if (opts.dataPointShape !== false) {
  2650. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  2651. }
  2652. });
  2653. if (opts.dataLabel !== false && process === 1) {
  2654. series.forEach(function(eachSeries, seriesIndex) {
  2655. let ranges,minRange,maxRange;
  2656. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2657. minRange = ranges.pop();
  2658. maxRange = ranges.shift();
  2659. var data = eachSeries.data;
  2660. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2661. drawPointText(points, eachSeries, config, context);
  2662. });
  2663. }
  2664. context.restore();
  2665. return {
  2666. xAxisPoints: xAxisPoints,
  2667. calPoints: calPoints,
  2668. eachSpacing: eachSpacing
  2669. };
  2670. }
  2671. function drawMixDataPoints(series, opts, config, context) {
  2672. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2673. let xAxisData = opts.chartData.xAxisData,
  2674. xAxisPoints = xAxisData.xAxisPoints,
  2675. eachSpacing = xAxisData.eachSpacing;
  2676. let endY = opts.height - opts.area[2];
  2677. let calPoints = [];
  2678. var columnIndex = 0;
  2679. var columnLength = 0;
  2680. series.forEach(function(eachSeries, seriesIndex) {
  2681. if (eachSeries.type == 'column') {
  2682. columnLength += 1;
  2683. }
  2684. });
  2685. context.save();
  2686. let leftNum=-2;
  2687. let rightNum=xAxisPoints.length+2;
  2688. let leftSpace=0;
  2689. let rightSpace=opts.width+eachSpacing;
  2690. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2691. context.translate(opts._scrollDistance_, 0);
  2692. leftNum=Math.floor(-opts._scrollDistance_/eachSpacing)-2;
  2693. rightNum=leftNum+opts.xAxis.itemCount+4;
  2694. leftSpace=-opts._scrollDistance_-eachSpacing+opts.area[3];
  2695. rightSpace=leftSpace+(opts.xAxis.itemCount+4)*eachSpacing;
  2696. }
  2697. series.forEach(function(eachSeries, seriesIndex) {
  2698. let ranges,minRange,maxRange;
  2699. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2700. minRange = ranges.pop();
  2701. maxRange = ranges.shift();
  2702. var data = eachSeries.data;
  2703. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2704. calPoints.push(points);
  2705. // 绘制柱状数据图
  2706. if (eachSeries.type == 'column') {
  2707. points = fixColumeData(points, eachSpacing, columnLength, columnIndex, config, opts);
  2708. for(let i=0;i<points.length;i++){
  2709. let item=points[i];
  2710. if (item !== null && i>leftNum && i<rightNum) {
  2711. context.beginPath();
  2712. context.setStrokeStyle(item.color || eachSeries.color);
  2713. context.setLineWidth(1)
  2714. context.setFillStyle(item.color || eachSeries.color);
  2715. var startX = item.x - item.width / 2;
  2716. var height = opts.height - item.y - opts.area[2];
  2717. context.moveTo(startX, item.y);
  2718. context.moveTo(startX, item.y);
  2719. context.lineTo(startX+item.width-2,item.y);
  2720. context.lineTo(startX+item.width-2,opts.height - opts.area[2]);
  2721. context.lineTo(startX,opts.height - opts.area[2]);
  2722. context.lineTo(startX,item.y);
  2723. context.closePath();
  2724. context.stroke();
  2725. context.fill();
  2726. context.closePath();
  2727. context.fill();
  2728. }
  2729. }
  2730. columnIndex += 1;
  2731. }
  2732. //绘制区域图数据
  2733. if (eachSeries.type == 'area') {
  2734. let splitPointList = splitPoints(points);
  2735. for (let i = 0; i < splitPointList.length; i++) {
  2736. let points = splitPointList[i];
  2737. // 绘制区域数据
  2738. context.beginPath();
  2739. context.setStrokeStyle(eachSeries.color);
  2740. context.setFillStyle(hexToRgb(eachSeries.color, 0.2));
  2741. context.setLineWidth(2 * opts.pixelRatio);
  2742. if (points.length > 1) {
  2743. var firstPoint = points[0];
  2744. let lastPoint = points[points.length - 1];
  2745. context.moveTo(firstPoint.x, firstPoint.y);
  2746. let startPoint=0;
  2747. if (eachSeries.style === 'curve') {
  2748. for(let j=0;j<points.length;j++){
  2749. let item=points[j];
  2750. if(startPoint==0 && item.x > leftSpace){
  2751. context.moveTo(item.x, item.y);
  2752. startPoint=1;
  2753. }
  2754. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2755. var ctrlPoint = createCurveControlPoints(points, j - 1);
  2756. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item.y);
  2757. }
  2758. };
  2759. } else {
  2760. for(let j=0;j<points.length;j++){
  2761. let item=points[j];
  2762. if(startPoint==0 && item.x > leftSpace){
  2763. context.moveTo(item.x, item.y);
  2764. startPoint=1;
  2765. }
  2766. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2767. context.lineTo(item.x, item.y);
  2768. }
  2769. };
  2770. }
  2771. context.lineTo(lastPoint.x, endY);
  2772. context.lineTo(firstPoint.x, endY);
  2773. context.lineTo(firstPoint.x, firstPoint.y);
  2774. } else {
  2775. let item = points[0];
  2776. context.moveTo(item.x - eachSpacing / 2, item.y);
  2777. context.lineTo(item.x + eachSpacing / 2, item.y);
  2778. context.lineTo(item.x + eachSpacing / 2, endY);
  2779. context.lineTo(item.x - eachSpacing / 2, endY);
  2780. context.moveTo(item.x - eachSpacing / 2, item.y);
  2781. }
  2782. context.closePath();
  2783. context.fill();
  2784. }
  2785. }
  2786. // 绘制折线数据图
  2787. if (eachSeries.type == 'line') {
  2788. var splitPointList = splitPoints(points);
  2789. splitPointList.forEach(function(points, index) {
  2790. if (eachSeries.lineType == 'dash') {
  2791. let dashLength = eachSeries.dashLength?eachSeries.dashLength:8;
  2792. dashLength *= opts.pixelRatio;
  2793. context.setLineDash([dashLength, dashLength]);
  2794. }
  2795. context.beginPath();
  2796. context.setStrokeStyle(eachSeries.color);
  2797. context.setLineWidth(2 * opts.pixelRatio);
  2798. if (points.length === 1) {
  2799. context.moveTo(points[0].x, points[0].y);
  2800. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  2801. } else {
  2802. context.moveTo(points[0].x, points[0].y);
  2803. let startPoint=0;
  2804. if (eachSeries.style == 'curve') {
  2805. for(let j=0;j<points.length;j++){
  2806. let item=points[j];
  2807. if(startPoint==0 && item.x > leftSpace){
  2808. context.moveTo(item.x, item.y);
  2809. startPoint=1;
  2810. }
  2811. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2812. var ctrlPoint = createCurveControlPoints(points, j - 1);
  2813. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y,item.x,item.y);
  2814. }
  2815. }
  2816. } else {
  2817. for(let j=0;j<points.length;j++){
  2818. let item=points[j];
  2819. if(startPoint==0 && item.x > leftSpace){
  2820. context.moveTo(item.x, item.y);
  2821. startPoint=1;
  2822. }
  2823. if (j > 0 && item.x > leftSpace && item.x < rightSpace) {
  2824. context.lineTo(item.x, item.y);
  2825. }
  2826. }
  2827. }
  2828. context.moveTo(points[0].x, points[0].y);
  2829. }
  2830. context.stroke();
  2831. context.setLineDash([]);
  2832. });
  2833. }
  2834. // 绘制点数据图
  2835. if (eachSeries.type == 'point') {
  2836. eachSeries.addPoint = true;
  2837. }
  2838. if (eachSeries.addPoint == true && eachSeries.type !== 'column' ) {
  2839. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  2840. }
  2841. });
  2842. if (opts.dataLabel !== false && process === 1) {
  2843. var columnIndex = 0;
  2844. series.forEach(function(eachSeries, seriesIndex) {
  2845. let ranges,minRange,maxRange;
  2846. ranges = [].concat(opts.chartData.yAxisData.ranges[eachSeries.index]);
  2847. minRange = ranges.pop();
  2848. maxRange = ranges.shift();
  2849. var data = eachSeries.data;
  2850. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2851. if (eachSeries.type !== 'column') {
  2852. drawPointText(points, eachSeries, config, context);
  2853. } else {
  2854. points = fixColumeData(points, eachSpacing, columnLength, columnIndex, config, opts);
  2855. drawPointText(points, eachSeries, config, context);
  2856. columnIndex += 1;
  2857. }
  2858. });
  2859. }
  2860. context.restore();
  2861. return {
  2862. xAxisPoints: xAxisPoints,
  2863. calPoints: calPoints,
  2864. eachSpacing: eachSpacing,
  2865. }
  2866. }
  2867. function drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints) {
  2868. var toolTipOption = opts.extra.tooltip || {};
  2869. if (toolTipOption.horizentalLine && opts.tooltip && process === 1 && (opts.type == 'line' || opts.type == 'area' || opts.type == 'column' || opts.type == 'candle' || opts.type == 'mix')) {
  2870. drawToolTipHorizentalLine(opts, config, context, eachSpacing, xAxisPoints)
  2871. }
  2872. context.save();
  2873. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2874. context.translate(opts._scrollDistance_, 0);
  2875. }
  2876. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  2877. drawToolTip(opts.tooltip.textList, opts.tooltip.offset, opts, config, context, eachSpacing, xAxisPoints);
  2878. }
  2879. context.restore();
  2880. }
  2881. function drawXAxis(categories, opts, config, context) {
  2882. let xAxisData = opts.chartData.xAxisData,
  2883. xAxisPoints = xAxisData.xAxisPoints,
  2884. startX = xAxisData.startX,
  2885. endX = xAxisData.endX,
  2886. eachSpacing = xAxisData.eachSpacing;
  2887. var boundaryGap='center';
  2888. if (opts.type == 'line'||opts.type == 'area'){
  2889. boundaryGap=opts.xAxis.boundaryGap;
  2890. }
  2891. var startY = opts.height - opts.area[2];
  2892. var endY = opts.area[0];
  2893. //绘制滚动条
  2894. if (opts.enableScroll && opts.xAxis.scrollShow) {
  2895. var scrollY = opts.height - opts.area[2] + config.xAxisHeight;
  2896. var scrollScreenWidth = endX - startX;
  2897. var scrollTotalWidth = eachSpacing * (xAxisPoints.length - 1);
  2898. var scrollWidth = scrollScreenWidth * scrollScreenWidth / scrollTotalWidth;
  2899. var scrollLeft = 0;
  2900. if (opts._scrollDistance_) {
  2901. scrollLeft = -opts._scrollDistance_ * (scrollScreenWidth) / scrollTotalWidth;
  2902. }
  2903. context.beginPath();
  2904. context.setLineCap('round');
  2905. context.setLineWidth(6 * opts.pixelRatio);
  2906. context.setStrokeStyle(opts.xAxis.scrollBackgroundColor || "#EFEBEF");
  2907. context.moveTo(startX, scrollY);
  2908. context.lineTo(endX, scrollY);
  2909. context.stroke();
  2910. context.closePath();
  2911. context.beginPath();
  2912. context.setLineCap('round');
  2913. context.setLineWidth(6 * opts.pixelRatio);
  2914. context.setStrokeStyle(opts.xAxis.scrollColor || "#A6A6A6");
  2915. context.moveTo(startX + scrollLeft, scrollY);
  2916. context.lineTo(startX + scrollLeft + scrollWidth, scrollY);
  2917. context.stroke();
  2918. context.closePath();
  2919. context.setLineCap('butt');
  2920. }
  2921. context.save();
  2922. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0) {
  2923. context.translate(opts._scrollDistance_, 0);
  2924. }
  2925. //绘制X轴刻度线
  2926. if (opts.xAxis.calibration === true) {
  2927. context.setStrokeStyle(opts.xAxis.gridColor || "#cccccc");
  2928. context.setLineCap('butt');
  2929. context.setLineWidth(1 * opts.pixelRatio);
  2930. xAxisPoints.forEach(function(item, index) {
  2931. if (index > 0) {
  2932. context.beginPath();
  2933. context.moveTo(item - eachSpacing / 2, startY);
  2934. context.lineTo(item - eachSpacing / 2, startY + 3 * opts.pixelRatio);
  2935. context.closePath();
  2936. context.stroke();
  2937. }
  2938. });
  2939. }
  2940. //绘制X轴网格
  2941. if (opts.xAxis.disableGrid !== true) {
  2942. context.setStrokeStyle(opts.xAxis.gridColor || "#cccccc");
  2943. context.setLineCap('butt');
  2944. context.setLineWidth(1 * opts.pixelRatio);
  2945. if (opts.xAxis.gridType == 'dash') {
  2946. context.setLineDash([opts.xAxis.dashLength, opts.xAxis.dashLength]);
  2947. }
  2948. opts.xAxis.gridEval = opts.xAxis.gridEval || 1;
  2949. xAxisPoints.forEach(function(item, index) {
  2950. if (index % opts.xAxis.gridEval == 0) {
  2951. context.beginPath();
  2952. context.moveTo(item, startY);
  2953. context.lineTo(item, endY);
  2954. context.stroke();
  2955. }
  2956. });
  2957. context.setLineDash([]);
  2958. }
  2959. //绘制X轴文案
  2960. if (opts.xAxis.disabled !== true) {
  2961. // 对X轴列表做抽稀处理
  2962. //默认全部显示X轴标签
  2963. let maxXAxisListLength = categories.length;
  2964. //如果设置了X轴单屏数量
  2965. if (opts.xAxis.labelCount) {
  2966. //如果设置X轴密度
  2967. if (opts.xAxis.itemCount) {
  2968. maxXAxisListLength = Math.ceil(categories.length / opts.xAxis.itemCount * opts.xAxis.labelCount);
  2969. } else {
  2970. maxXAxisListLength = opts.xAxis.labelCount;
  2971. }
  2972. maxXAxisListLength -= 1;
  2973. }
  2974. let ratio = Math.ceil(categories.length / maxXAxisListLength);
  2975. let newCategories = [];
  2976. let cgLength = categories.length;
  2977. for (let i = 0; i < cgLength; i++) {
  2978. if (i % ratio !== 0) {
  2979. newCategories.push("");
  2980. } else {
  2981. newCategories.push(categories[i]);
  2982. }
  2983. }
  2984. newCategories[cgLength - 1] = categories[cgLength - 1];
  2985. var xAxisFontSize = opts.xAxis.fontSize || config.fontSize;
  2986. if (config._xAxisTextAngle_ === 0) {
  2987. newCategories.forEach(function(item, index) {
  2988. var offset = - measureText(String(item), xAxisFontSize) / 2;
  2989. if(boundaryGap == 'center'){
  2990. offset+=eachSpacing / 2;
  2991. }
  2992. var scrollHeight=0;
  2993. if(opts.xAxis.scrollShow){
  2994. scrollHeight=6*opts.pixelRatio;
  2995. }
  2996. context.beginPath();
  2997. context.setFontSize(xAxisFontSize);
  2998. context.setFillStyle(opts.xAxis.fontColor || '#666666');
  2999. context.fillText(String(item), xAxisPoints[index] + offset, startY + xAxisFontSize + (config.xAxisHeight - scrollHeight - xAxisFontSize) / 2);
  3000. context.closePath();
  3001. context.stroke();
  3002. });
  3003. } else {
  3004. newCategories.forEach(function(item, index) {
  3005. context.save();
  3006. context.beginPath();
  3007. context.setFontSize(xAxisFontSize);
  3008. context.setFillStyle(opts.xAxis.fontColor || '#666666');
  3009. var textWidth = measureText(String(item),xAxisFontSize);
  3010. var offset = - textWidth;
  3011. if(boundaryGap == 'center'){
  3012. offset+=eachSpacing / 2;
  3013. }
  3014. var _calRotateTranslate = calRotateTranslate(xAxisPoints[index] + eachSpacing / 2, startY + xAxisFontSize / 2 + 5, opts.height),
  3015. transX = _calRotateTranslate.transX,
  3016. transY = _calRotateTranslate.transY;
  3017. context.rotate(-1 * config._xAxisTextAngle_);
  3018. context.translate(transX, transY);
  3019. context.fillText(String(item), xAxisPoints[index] + offset, startY + xAxisFontSize + 5);
  3020. context.closePath();
  3021. context.stroke();
  3022. context.restore();
  3023. });
  3024. }
  3025. }
  3026. context.restore();
  3027. //绘制X轴轴线
  3028. if(opts.xAxis.axisLine){
  3029. context.beginPath();
  3030. context.setStrokeStyle(opts.xAxis.axisLineColor);
  3031. context.setLineWidth(1 * opts.pixelRatio);
  3032. context.moveTo(startX,opts.height-opts.area[2]);
  3033. context.lineTo(endX,opts.height-opts.area[2]);
  3034. context.stroke();
  3035. }
  3036. }
  3037. function drawYAxisGrid(categories, opts, config, context) {
  3038. if (opts.yAxis.disableGrid === true) {
  3039. return;
  3040. }
  3041. let spacingValid = opts.height - opts.area[0] - opts.area[2];
  3042. let eachSpacing = spacingValid / opts.yAxis.splitNumber;
  3043. let startX = opts.area[3];
  3044. let xAxisPoints = opts.chartData.xAxisData.xAxisPoints,
  3045. xAxiseachSpacing = opts.chartData.xAxisData.eachSpacing;
  3046. let TotalWidth = xAxiseachSpacing * (xAxisPoints.length - 1);
  3047. let endX = startX + TotalWidth;
  3048. let points = [];
  3049. for (let i = 0; i < opts.yAxis.splitNumber + 1; i++) {
  3050. points.push(opts.height - opts.area[2] - eachSpacing * i);
  3051. }
  3052. context.save();
  3053. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0) {
  3054. context.translate(opts._scrollDistance_, 0);
  3055. }
  3056. if (opts.yAxis.gridType == 'dash') {
  3057. context.setLineDash([opts.yAxis.dashLength, opts.yAxis.dashLength]);
  3058. }
  3059. context.setStrokeStyle(opts.yAxis.gridColor);
  3060. context.setLineWidth(1 * opts.pixelRatio);
  3061. points.forEach(function(item, index) {
  3062. context.beginPath();
  3063. context.moveTo(startX, item);
  3064. context.lineTo(endX, item);
  3065. context.stroke();
  3066. });
  3067. context.setLineDash([]);
  3068. context.restore();
  3069. }
  3070. function drawYAxis(series, opts, config, context) {
  3071. if (opts.yAxis.disabled === true) {
  3072. return;
  3073. }
  3074. var spacingValid = opts.height - opts.area[0] - opts.area[2];
  3075. var eachSpacing = spacingValid / opts.yAxis.splitNumber;
  3076. var startX = opts.area[3];
  3077. var endX = opts.width - opts.area[1];
  3078. var endY = opts.height - opts.area[2];
  3079. var fillEndY = endY + config.xAxisHeight;
  3080. if (opts.xAxis.scrollShow) {
  3081. fillEndY -= 3 * opts.pixelRatio;
  3082. }
  3083. if (opts.xAxis.rotateLabel){
  3084. fillEndY = opts.height - opts.area[2]+3;
  3085. }
  3086. // set YAxis background
  3087. context.beginPath();
  3088. context.setFillStyle(opts.background || '#ffffff');
  3089. if (opts._scrollDistance_ < 0) {
  3090. context.fillRect(0, 0, startX, fillEndY);
  3091. }
  3092. if(opts.enableScroll == true){
  3093. context.fillRect(endX, 0, opts.width, fillEndY);
  3094. }
  3095. context.closePath();
  3096. context.stroke();
  3097. var points = [];
  3098. for (let i = 0; i <= opts.yAxis.splitNumber; i++) {
  3099. points.push(opts.area[0] + eachSpacing * i);
  3100. }
  3101. let tStartLeft=opts.area[3];
  3102. let tStartRight=opts.width-opts.area[1];
  3103. for (let i = 0; i < opts.yAxis.data.length; i++) {
  3104. let yData = opts.yAxis.data[i];
  3105. if(yData.disabled !== true){
  3106. let rangesFormat = opts.chartData.yAxisData.rangesFormat[i];
  3107. let yAxisFontSize = yData.fontSize || config.fontSize;
  3108. let yAxisWidth = opts.chartData.yAxisData.yAxisWidth[i];
  3109. //画Y轴刻度及文案
  3110. rangesFormat.forEach(function(item, index) {
  3111. var pos = points[index] ? points[index] : endY;
  3112. context.beginPath();
  3113. context.setFontSize(yAxisFontSize);
  3114. context.setLineWidth(1*opts.pixelRatio);
  3115. context.setStrokeStyle(yData.axisLineColor||'#cccccc');
  3116. context.setFillStyle(yData.fontColor|| '#666666');
  3117. if(yAxisWidth.position=='left'){
  3118. context.fillText(String(item), tStartLeft - yAxisWidth.width , pos + yAxisFontSize / 2);
  3119. //画刻度线
  3120. if(yData.calibration==true){
  3121. context.moveTo(tStartLeft,pos);
  3122. context.lineTo(tStartLeft - 3*opts.pixelRatio,pos);
  3123. }
  3124. }else{
  3125. context.fillText(String(item), tStartRight + 4*opts.pixelRatio, pos + yAxisFontSize / 2);
  3126. //画刻度线
  3127. if(yData.calibration==true){
  3128. context.moveTo(tStartRight,pos);
  3129. context.lineTo(tStartRight + 3*opts.pixelRatio,pos);
  3130. }
  3131. }
  3132. context.closePath();
  3133. context.stroke();
  3134. });
  3135. //画Y轴轴线
  3136. if (yData.axisLine!==false) {
  3137. context.beginPath();
  3138. context.setStrokeStyle(yData.axisLineColor||'#cccccc');
  3139. context.setLineWidth(1 * opts.pixelRatio);
  3140. if(yAxisWidth.position=='left'){
  3141. context.moveTo(tStartLeft,opts.height-opts.area[2]);
  3142. context.lineTo(tStartLeft,opts.area[0]);
  3143. }else{
  3144. context.moveTo(tStartRight,opts.height-opts.area[2]);
  3145. context.lineTo(tStartRight,opts.area[0]);
  3146. }
  3147. context.stroke();
  3148. }
  3149. //画Y轴标题
  3150. if (opts.yAxis.showTitle) {
  3151. let titleFontSize = yData.titleFontSize || config.fontSize;
  3152. let title = yData.title;
  3153. context.beginPath();
  3154. context.setFontSize(titleFontSize);
  3155. context.setFillStyle(yData.titleFontColor || '#666666');
  3156. if(yAxisWidth.position=='left'){
  3157. context.fillText(title, tStartLeft - measureText(title,titleFontSize)/2, opts.area[0]-10*opts.pixelRatio);
  3158. }else{
  3159. context.fillText(title,tStartRight - measureText(title,titleFontSize)/2, opts.area[0]-10*opts.pixelRatio);
  3160. }
  3161. context.closePath();
  3162. context.stroke();
  3163. }
  3164. if(yAxisWidth.position=='left'){
  3165. tStartLeft -=(yAxisWidth.width + opts.yAxis.padding);
  3166. }else{
  3167. tStartRight +=yAxisWidth.width+ opts.yAxis.padding;
  3168. }
  3169. }
  3170. }
  3171. }
  3172. function drawLegend(series, opts, config, context, chartData) {
  3173. if (opts.legend.show === false) {
  3174. return;
  3175. }
  3176. let legendData = chartData.legendData;
  3177. let legendList = legendData.points;
  3178. let legendArea = legendData.area;
  3179. let padding = opts.legend.padding;
  3180. let fontSize = opts.legend.fontSize;
  3181. let shapeWidth = 15 * opts.pixelRatio;
  3182. let shapeRight = 5 * opts.pixelRatio;
  3183. let itemGap = opts.legend.itemGap;
  3184. let lineHeight = Math.max(opts.legend.lineHeight * opts.pixelRatio, fontSize);
  3185. //画背景及边框
  3186. context.beginPath();
  3187. context.setLineWidth(opts.legend.borderWidth);
  3188. context.setStrokeStyle(opts.legend.borderColor);
  3189. context.setFillStyle(opts.legend.backgroundColor);
  3190. context.moveTo(legendArea.start.x, legendArea.start.y);
  3191. context.rect(legendArea.start.x, legendArea.start.y, legendArea.width, legendArea.height);
  3192. context.closePath();
  3193. context.fill();
  3194. context.stroke();
  3195. legendList.forEach(function(itemList, listIndex) {
  3196. let width = 0;
  3197. let height = 0;
  3198. width = legendData.widthArr[listIndex];
  3199. height = legendData.heightArr[listIndex];
  3200. let startX = 0;
  3201. let startY = 0;
  3202. if (opts.legend.position == 'top' || opts.legend.position == 'bottom') {
  3203. startX = legendArea.start.x + (legendArea.width - width) / 2;
  3204. startY = legendArea.start.y + padding + listIndex * lineHeight;
  3205. } else {
  3206. if (listIndex == 0) {
  3207. width = 0;
  3208. } else {
  3209. width = legendData.widthArr[listIndex - 1];
  3210. }
  3211. startX = legendArea.start.x + padding + width;
  3212. startY = legendArea.start.y + padding + (legendArea.height - height) / 2;
  3213. }
  3214. context.setFontSize(config.fontSize);
  3215. for (let i = 0; i < itemList.length; i++) {
  3216. let item = itemList[i];
  3217. item.area = [0, 0, 0, 0];
  3218. item.area[0] = startX;
  3219. item.area[1] = startY;
  3220. item.area[3] = startY + lineHeight;
  3221. context.beginPath();
  3222. context.setLineWidth(1 * opts.pixelRatio);
  3223. context.setStrokeStyle(item.show ? item.color : opts.legend.hiddenColor);
  3224. context.setFillStyle(item.show ? item.color : opts.legend.hiddenColor);
  3225. switch (item.legendShape) {
  3226. case 'line':
  3227. context.moveTo(startX, startY + 0.5 * lineHeight - 2 * opts.pixelRatio);
  3228. context.fillRect(startX, startY + 0.5 * lineHeight - 2 * opts.pixelRatio, 15 * opts.pixelRatio, 4 * opts.pixelRatio);
  3229. break;
  3230. case 'triangle':
  3231. context.moveTo(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight - 5 * opts.pixelRatio);
  3232. context.lineTo(startX + 2.5 * opts.pixelRatio, startY + 0.5 * lineHeight + 5 * opts.pixelRatio);
  3233. context.lineTo(startX + 12.5 * opts.pixelRatio, startY + 0.5 * lineHeight + 5 * opts.pixelRatio);
  3234. context.lineTo(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight - 5 * opts.pixelRatio);
  3235. break;
  3236. case 'diamond':
  3237. context.moveTo(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight - 5 * opts.pixelRatio);
  3238. context.lineTo(startX + 2.5 * opts.pixelRatio, startY + 0.5 * lineHeight);
  3239. context.lineTo(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight + 5 * opts.pixelRatio);
  3240. context.lineTo(startX + 12.5 * opts.pixelRatio, startY + 0.5 * lineHeight);
  3241. context.lineTo(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight - 5 * opts.pixelRatio);
  3242. break;
  3243. case 'circle':
  3244. context.moveTo(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight);
  3245. context.arc(startX + 7.5 * opts.pixelRatio, startY + 0.5 * lineHeight, 5 * opts.pixelRatio, 0, 2 * Math.PI);
  3246. break;
  3247. case 'rect':
  3248. context.moveTo(startX, startY + 0.5 * lineHeight - 5 * opts.pixelRatio);
  3249. context.fillRect(startX, startY + 0.5 * lineHeight - 5 * opts.pixelRatio, 15 * opts.pixelRatio, 10 * opts.pixelRatio);
  3250. break;
  3251. default:
  3252. context.moveTo(startX, startY + 0.5 * lineHeight - 5 * opts.pixelRatio);
  3253. context.fillRect(startX, startY + 0.5 * lineHeight - 5 * opts.pixelRatio, 15 * opts.pixelRatio, 10 * opts.pixelRatio);
  3254. }
  3255. context.closePath();
  3256. context.fill();
  3257. context.stroke();
  3258. startX += shapeWidth + shapeRight;
  3259. let fontTrans = 0.5 * lineHeight + 0.5 * fontSize - 2;
  3260. context.beginPath();
  3261. context.setFontSize(fontSize);
  3262. context.setFillStyle(item.show ? opts.legend.fontColor : opts.legend.hiddenColor);
  3263. context.fillText(item.name, startX, startY + fontTrans);
  3264. context.closePath();
  3265. context.stroke();
  3266. if (opts.legend.position == 'top' || opts.legend.position == 'bottom') {
  3267. startX += measureText(item.name, fontSize) + itemGap;
  3268. item.area[2] = startX;
  3269. } else {
  3270. item.area[2] = startX + measureText(item.name, fontSize) + itemGap;;
  3271. startX -= shapeWidth + shapeRight;
  3272. startY += lineHeight;
  3273. }
  3274. }
  3275. });
  3276. }
  3277. function drawPieDataPoints(series, opts, config, context) {
  3278. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3279. var pieOption = assign({}, {
  3280. activeOpacity: 0.5,
  3281. activeRadius: 10 * opts.pixelRatio,
  3282. offsetAngle: 0,
  3283. labelWidth: 15 * opts.pixelRatio,
  3284. ringWidth: 0,
  3285. border:false,
  3286. borderWidth:2,
  3287. borderColor:'#FFFFFF'
  3288. }, opts.extra.pie);
  3289. var centerPosition = {
  3290. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  3291. y: opts.area[0] + (opts.height - opts.area[0] - opts.area[2]) / 2
  3292. };
  3293. if (config.pieChartLinePadding == 0) {
  3294. config.pieChartLinePadding = pieOption.activeRadius;
  3295. }
  3296. 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);
  3297. series = getPieDataPoints(series, radius, process);
  3298. var activeRadius = pieOption.activeRadius;
  3299. series = series.map(function(eachSeries) {
  3300. eachSeries._start_ += (pieOption.offsetAngle) * Math.PI / 180;
  3301. return eachSeries;
  3302. });
  3303. series.forEach(function(eachSeries, seriesIndex) {
  3304. if (opts.tooltip) {
  3305. if (opts.tooltip.index == seriesIndex) {
  3306. context.beginPath();
  3307. context.setFillStyle(hexToRgb(eachSeries.color, opts.extra.pie.activeOpacity || 0.5));
  3308. context.moveTo(centerPosition.x, centerPosition.y);
  3309. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_ + activeRadius, eachSeries._start_,
  3310. eachSeries._start_ + 2 *
  3311. eachSeries._proportion_ * Math.PI);
  3312. context.closePath();
  3313. context.fill();
  3314. }
  3315. }
  3316. context.beginPath();
  3317. context.setLineWidth(pieOption.borderWidth * opts.pixelRatio);
  3318. context.lineJoin = "round";
  3319. context.setStrokeStyle(pieOption.borderColor);
  3320. context.setFillStyle(eachSeries.color);
  3321. context.moveTo(centerPosition.x, centerPosition.y);
  3322. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_, eachSeries._start_, eachSeries._start_ + 2 * eachSeries._proportion_ * Math.PI);
  3323. context.closePath();
  3324. context.fill();
  3325. if (pieOption.border == true) {
  3326. context.stroke();
  3327. }
  3328. });
  3329. if (opts.type === 'ring') {
  3330. var innerPieWidth = radius * 0.6;
  3331. if (typeof opts.extra.pie.ringWidth === 'number' && opts.extra.pie.ringWidth > 0) {
  3332. innerPieWidth = Math.max(0, radius - opts.extra.pie.ringWidth);
  3333. }
  3334. context.beginPath();
  3335. context.setFillStyle(opts.background || '#ffffff');
  3336. context.moveTo(centerPosition.x, centerPosition.y);
  3337. context.arc(centerPosition.x, centerPosition.y, innerPieWidth, 0, 2 * Math.PI);
  3338. context.closePath();
  3339. context.fill();
  3340. }
  3341. if (opts.dataLabel !== false && process === 1) {
  3342. var valid = false;
  3343. for (var i = 0, len = series.length; i < len; i++) {
  3344. if (series[i].data > 0) {
  3345. valid = true;
  3346. break;
  3347. }
  3348. }
  3349. if (valid) {
  3350. drawPieText(series, opts, config, context, radius, centerPosition);
  3351. }
  3352. }
  3353. if (process === 1 && opts.type === 'ring') {
  3354. drawRingTitle(opts, config, context, centerPosition);
  3355. }
  3356. return {
  3357. center: centerPosition,
  3358. radius: radius,
  3359. series: series
  3360. };
  3361. }
  3362. function drawRoseDataPoints(series, opts, config, context) {
  3363. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3364. var roseOption = assign({}, {
  3365. type: 'area',
  3366. activeOpacity: 0.5,
  3367. activeRadius: 10 * opts.pixelRatio,
  3368. offsetAngle: 0,
  3369. labelWidth: 15 * opts.pixelRatio,
  3370. border:false,
  3371. borderWidth:2,
  3372. borderColor:'#FFFFFF'
  3373. }, opts.extra.rose);
  3374. if (config.pieChartLinePadding == 0) {
  3375. config.pieChartLinePadding = roseOption.activeRadius;
  3376. }
  3377. var centerPosition = {
  3378. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  3379. y: opts.area[0] + (opts.height - opts.area[0] - opts.area[2]) / 2
  3380. };
  3381. 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);
  3382. var minRadius = roseOption.minRadius || radius * 0.5;
  3383. series = getRoseDataPoints(series, roseOption.type, minRadius, radius, process);
  3384. var activeRadius = roseOption.activeRadius;
  3385. series = series.map(function(eachSeries) {
  3386. eachSeries._start_ += (roseOption.offsetAngle || 0) * Math.PI / 180;
  3387. return eachSeries;
  3388. });
  3389. series.forEach(function(eachSeries, seriesIndex) {
  3390. if (opts.tooltip) {
  3391. if (opts.tooltip.index == seriesIndex) {
  3392. context.beginPath();
  3393. context.setFillStyle(hexToRgb(eachSeries.color, roseOption.activeOpacity || 0.5));
  3394. context.moveTo(centerPosition.x, centerPosition.y);
  3395. context.arc(centerPosition.x, centerPosition.y, activeRadius + eachSeries._radius_, eachSeries._start_,
  3396. eachSeries._start_ + 2 * eachSeries._rose_proportion_ * Math.PI);
  3397. context.closePath();
  3398. context.fill();
  3399. }
  3400. }
  3401. context.beginPath();
  3402. context.setLineWidth(roseOption.borderWidth * opts.pixelRatio);
  3403. context.lineJoin = "round";
  3404. context.setStrokeStyle(roseOption.borderColor);
  3405. context.setFillStyle(eachSeries.color);
  3406. context.moveTo(centerPosition.x, centerPosition.y);
  3407. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_, eachSeries._start_, eachSeries._start_ + 2 *
  3408. eachSeries._rose_proportion_ * Math.PI);
  3409. context.closePath();
  3410. context.fill();
  3411. if (roseOption.border == true) {
  3412. context.stroke();
  3413. }
  3414. });
  3415. if (opts.dataLabel !== false && process === 1) {
  3416. var valid = false;
  3417. for (var i = 0, len = series.length; i < len; i++) {
  3418. if (series[i].data > 0) {
  3419. valid = true;
  3420. break;
  3421. }
  3422. }
  3423. if (valid) {
  3424. drawPieText(series, opts, config, context, radius, centerPosition);
  3425. }
  3426. }
  3427. return {
  3428. center: centerPosition,
  3429. radius: radius,
  3430. series: series
  3431. };
  3432. }
  3433. function drawArcbarDataPoints(series, opts, config, context) {
  3434. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3435. var arcbarOption = assign({}, {
  3436. startAngle: 0.75,
  3437. endAngle: 0.25,
  3438. type: 'default',
  3439. width: 12 * opts.pixelRatio,
  3440. gap:2 * opts.pixelRatio
  3441. }, opts.extra.arcbar);
  3442. series = getArcbarDataPoints(series, arcbarOption, process);
  3443. var centerPosition;
  3444. if(arcbarOption.center){
  3445. centerPosition=arcbarOption.center;
  3446. }else{
  3447. centerPosition= {
  3448. x: opts.width / 2,
  3449. y: opts.height / 2
  3450. };
  3451. }
  3452. var radius;
  3453. if(arcbarOption.radius){
  3454. radius=arcbarOption.radius;
  3455. }else{
  3456. radius = Math.min(centerPosition.x, centerPosition.y);
  3457. radius -= 5 * opts.pixelRatio;
  3458. radius -= arcbarOption.width / 2;
  3459. }
  3460. for (let i = 0; i < series.length; i++) {
  3461. let eachSeries = series[i];
  3462. //背景颜色
  3463. context.setLineWidth(arcbarOption.width);
  3464. context.setStrokeStyle(arcbarOption.backgroundColor || '#E9E9E9');
  3465. context.setLineCap('round');
  3466. context.beginPath();
  3467. if (arcbarOption.type == 'default') {
  3468. context.arc(centerPosition.x, centerPosition.y, radius-(arcbarOption.width+arcbarOption.gap)*i, arcbarOption.startAngle * Math.PI, arcbarOption.endAngle * Math.PI, false);
  3469. } else {
  3470. context.arc(centerPosition.x, centerPosition.y, radius-(arcbarOption.width+arcbarOption.gap)*i, 0, 2 * Math.PI, false);
  3471. }
  3472. context.stroke();
  3473. //进度条
  3474. context.setLineWidth(arcbarOption.width);
  3475. context.setStrokeStyle(eachSeries.color);
  3476. context.setLineCap('round');
  3477. context.beginPath();
  3478. context.arc(centerPosition.x, centerPosition.y, radius-(arcbarOption.width+arcbarOption.gap)*i, arcbarOption.startAngle * Math.PI, eachSeries._proportion_ * Math.PI, false);
  3479. context.stroke();
  3480. }
  3481. drawRingTitle(opts, config, context, centerPosition);
  3482. return {
  3483. center: centerPosition,
  3484. radius: radius,
  3485. series: series
  3486. };
  3487. }
  3488. function drawGaugeDataPoints(categories, series, opts, config, context) {
  3489. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  3490. var gaugeOption = assign({}, {
  3491. type:'default',
  3492. startAngle: 0.75,
  3493. endAngle: 0.25,
  3494. width: 15,
  3495. splitLine: {
  3496. fixRadius: 0,
  3497. splitNumber: 10,
  3498. width: 15,
  3499. color: '#FFFFFF',
  3500. childNumber: 5,
  3501. childWidth: 5
  3502. },
  3503. pointer: {
  3504. width: 15,
  3505. color: 'auto'
  3506. }
  3507. }, opts.extra.gauge);
  3508. if (gaugeOption.oldAngle == undefined) {
  3509. gaugeOption.oldAngle = gaugeOption.startAngle;
  3510. }
  3511. if (gaugeOption.oldData == undefined) {
  3512. gaugeOption.oldData = 0;
  3513. }
  3514. categories = getGaugeAxisPoints(categories, gaugeOption.startAngle, gaugeOption.endAngle);
  3515. var centerPosition = {
  3516. x: opts.width / 2,
  3517. y: opts.height / 2
  3518. };
  3519. var radius = Math.min(centerPosition.x, centerPosition.y);
  3520. radius -= 5 * opts.pixelRatio;
  3521. radius -= gaugeOption.width / 2;
  3522. var innerRadius = radius - gaugeOption.width;
  3523. var totalAngle=0;
  3524. //判断仪表盘的样式:default百度样式,progress新样式
  3525. if(gaugeOption.type == 'progress'){
  3526. //## 第一步画中心圆形背景和进度条背景
  3527. //中心圆形背景
  3528. var pieRadius = radius - gaugeOption.width*3;
  3529. context.beginPath();
  3530. let gradient = context.createLinearGradient(centerPosition.x, centerPosition.y-pieRadius, centerPosition.x , centerPosition.y+pieRadius);
  3531. //配置渐变填充(起点:中心点向上减半径;结束点中心点向下加半径)
  3532. gradient.addColorStop('0', hexToRgb(series[0].color, 0.3));
  3533. gradient.addColorStop('1.0',hexToRgb("#FFFFFF", 0.1));
  3534. context.setFillStyle(gradient);
  3535. context.arc(centerPosition.x, centerPosition.y, pieRadius, 0, 2*Math.PI, false);
  3536. context.fill();
  3537. //画进度条背景
  3538. context.setLineWidth(gaugeOption.width);
  3539. context.setStrokeStyle(hexToRgb(series[0].color, 0.3));
  3540. context.setLineCap('round');
  3541. context.beginPath();
  3542. context.arc(centerPosition.x, centerPosition.y, innerRadius , gaugeOption.startAngle * Math.PI, gaugeOption.endAngle *Math.PI, false);
  3543. context.stroke();
  3544. //## 第二步画刻度线
  3545. totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  3546. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  3547. let childAngle = totalAngle / gaugeOption.splitLine.splitNumber / gaugeOption.splitLine.childNumber;
  3548. let startX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius;
  3549. let endX = -radius - gaugeOption.width - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.width;
  3550. context.save();
  3551. context.translate(centerPosition.x, centerPosition.y);
  3552. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  3553. let len = gaugeOption.splitLine.splitNumber * gaugeOption.splitLine.childNumber + 1;
  3554. let proc = series[0].data * process;
  3555. for (let i = 0; i < len; i++) {
  3556. context.beginPath();
  3557. //刻度线随进度变色
  3558. if(proc>(i/len)){
  3559. context.setStrokeStyle(hexToRgb(series[0].color, 1));
  3560. }else{
  3561. context.setStrokeStyle(hexToRgb(series[0].color, 0.3));
  3562. }
  3563. context.setLineWidth(3 * opts.pixelRatio);
  3564. context.moveTo(startX, 0);
  3565. context.lineTo(endX, 0);
  3566. context.stroke();
  3567. context.rotate(childAngle * Math.PI);
  3568. }
  3569. context.restore();
  3570. //## 第三步画进度条
  3571. series = getArcbarDataPoints(series, gaugeOption, process);
  3572. context.setLineWidth(gaugeOption.width);
  3573. context.setStrokeStyle(series[0].color);
  3574. context.setLineCap('round');
  3575. context.beginPath();
  3576. context.arc(centerPosition.x, centerPosition.y, innerRadius , gaugeOption.startAngle * Math.PI, series[0]._proportion_ *Math.PI, false);
  3577. context.stroke();
  3578. //## 第四步画指针
  3579. let pointerRadius = radius - gaugeOption.width*2.5;
  3580. context.save();
  3581. context.translate(centerPosition.x, centerPosition.y);
  3582. context.rotate((series[0]._proportion_ - 1) * Math.PI);
  3583. context.beginPath();
  3584. context.setLineWidth(gaugeOption.width/3);
  3585. let gradient3 = context.createLinearGradient(0, -pointerRadius*0.6, 0 , pointerRadius*0.6);
  3586. gradient3.addColorStop('0', hexToRgb('#FFFFFF', 0));
  3587. gradient3.addColorStop('0.5', hexToRgb(series[0].color, 1));
  3588. gradient3.addColorStop('1.0', hexToRgb('#FFFFFF', 0));
  3589. context.setStrokeStyle(gradient3);
  3590. context.arc(0, 0, pointerRadius , 0.85* Math.PI, 1.15 * Math.PI, false);
  3591. context.stroke();
  3592. context.beginPath();
  3593. context.setLineWidth(1);
  3594. context.setStrokeStyle(series[0].color);
  3595. context.setFillStyle(series[0].color);
  3596. context.moveTo(-pointerRadius-gaugeOption.width/3/2,-4);
  3597. context.lineTo(-pointerRadius-gaugeOption.width/3/2-4,0);
  3598. context.lineTo(-pointerRadius-gaugeOption.width/3/2,4);
  3599. context.lineTo(-pointerRadius-gaugeOption.width/3/2,-4);
  3600. context.stroke();
  3601. context.fill();
  3602. context.restore();
  3603. //default百度样式
  3604. }else{
  3605. //画背景
  3606. context.setLineWidth(gaugeOption.width);
  3607. context.setLineCap('butt');
  3608. for (let i = 0; i < categories.length; i++) {
  3609. let eachCategories = categories[i];
  3610. context.beginPath();
  3611. context.setStrokeStyle(eachCategories.color);
  3612. context.arc(centerPosition.x, centerPosition.y, radius, eachCategories._startAngle_ * Math.PI, eachCategories._endAngle_ *Math.PI, false);
  3613. context.stroke();
  3614. }
  3615. context.save();
  3616. //画刻度线
  3617. totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  3618. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  3619. let childAngle = totalAngle / gaugeOption.splitLine.splitNumber / gaugeOption.splitLine.childNumber;
  3620. let startX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius;
  3621. let endX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.width;
  3622. let childendX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.childWidth;
  3623. context.translate(centerPosition.x, centerPosition.y);
  3624. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  3625. for (let i = 0; i < gaugeOption.splitLine.splitNumber + 1; i++) {
  3626. context.beginPath();
  3627. context.setStrokeStyle(gaugeOption.splitLine.color);
  3628. context.setLineWidth(2 * opts.pixelRatio);
  3629. context.moveTo(startX, 0);
  3630. context.lineTo(endX, 0);
  3631. context.stroke();
  3632. context.rotate(splitAngle * Math.PI);
  3633. }
  3634. context.restore();
  3635. context.save();
  3636. context.translate(centerPosition.x, centerPosition.y);
  3637. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  3638. for (let i = 0; i < gaugeOption.splitLine.splitNumber * gaugeOption.splitLine.childNumber + 1; i++) {
  3639. context.beginPath();
  3640. context.setStrokeStyle(gaugeOption.splitLine.color);
  3641. context.setLineWidth(1 * opts.pixelRatio);
  3642. context.moveTo(startX, 0);
  3643. context.lineTo(childendX, 0);
  3644. context.stroke();
  3645. context.rotate(childAngle * Math.PI);
  3646. }
  3647. context.restore();
  3648. //画指针
  3649. series = getGaugeDataPoints(series, categories, gaugeOption, process);
  3650. for (let i = 0; i < series.length; i++) {
  3651. let eachSeries = series[i];
  3652. context.save();
  3653. context.translate(centerPosition.x, centerPosition.y);
  3654. context.rotate((eachSeries._proportion_ - 1) * Math.PI);
  3655. context.beginPath();
  3656. context.setFillStyle(eachSeries.color);
  3657. context.moveTo(gaugeOption.pointer.width, 0);
  3658. context.lineTo(0, -gaugeOption.pointer.width / 2);
  3659. context.lineTo(-innerRadius, 0);
  3660. context.lineTo(0, gaugeOption.pointer.width / 2);
  3661. context.lineTo(gaugeOption.pointer.width, 0);
  3662. context.closePath();
  3663. context.fill();
  3664. context.beginPath();
  3665. context.setFillStyle('#FFFFFF');
  3666. context.arc(0, 0, gaugeOption.pointer.width / 6, 0, 2 * Math.PI, false);
  3667. context.fill();
  3668. context.restore();
  3669. }
  3670. if (opts.dataLabel !== false) {
  3671. drawGaugeLabel(gaugeOption, radius, centerPosition, opts, config, context);
  3672. }
  3673. }
  3674. //画仪表盘标题,副标题
  3675. drawRingTitle(opts, config, context, centerPosition);
  3676. if (process === 1 && opts.type === 'gauge') {
  3677. opts.extra.gauge.oldAngle = series[0]._proportion_;
  3678. opts.extra.gauge.oldData = series[0].data;
  3679. }
  3680. return {
  3681. center: centerPosition,
  3682. radius: radius,
  3683. innerRadius: innerRadius,
  3684. categories: categories,
  3685. totalAngle: totalAngle
  3686. };
  3687. }
  3688. function drawRadarDataPoints(series, opts, config, context) {
  3689. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  3690. var radarOption = assign({},{
  3691. gridColor: '#cccccc',
  3692. gridType: 'radar',
  3693. labelColor: '#666666',
  3694. opacity: 0.2,
  3695. gridCount:3
  3696. },opts.extra.radar);
  3697. var coordinateAngle = getRadarCoordinateSeries(opts.categories.length);
  3698. var centerPosition = {
  3699. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  3700. y: opts.area[0] + (opts.height - opts.area[0] - opts.area[2]) / 2
  3701. };
  3702. var radius = Math.min(centerPosition.x - (getMaxTextListLength(opts.categories) + config.radarLabelTextMargin),centerPosition.y - config.radarLabelTextMargin);
  3703. //TODO逻辑不对
  3704. radius -= opts.padding[1];
  3705. // 画分割线
  3706. context.beginPath();
  3707. context.setLineWidth(1 * opts.pixelRatio);
  3708. context.setStrokeStyle(radarOption.gridColor);
  3709. coordinateAngle.forEach(function(angle) {
  3710. var pos = convertCoordinateOrigin(radius * Math.cos(angle), radius * Math.sin(angle), centerPosition);
  3711. context.moveTo(centerPosition.x, centerPosition.y);
  3712. context.lineTo(pos.x, pos.y);
  3713. });
  3714. context.stroke();
  3715. context.closePath();
  3716. // 画背景网格
  3717. var _loop = function _loop(i) {
  3718. var startPos = {};
  3719. context.beginPath();
  3720. context.setLineWidth(1 * opts.pixelRatio);
  3721. context.setStrokeStyle(radarOption.gridColor);
  3722. if(radarOption.gridType=='radar'){
  3723. coordinateAngle.forEach(function(angle, index) {
  3724. var pos = convertCoordinateOrigin(radius / radarOption.gridCount * i * Math.cos(angle), radius / radarOption.gridCount * i * Math.sin(angle), centerPosition);
  3725. if (index === 0) {
  3726. startPos = pos;
  3727. context.moveTo(pos.x, pos.y);
  3728. } else {
  3729. context.lineTo(pos.x, pos.y);
  3730. }
  3731. });
  3732. context.lineTo(startPos.x, startPos.y);
  3733. }else{
  3734. var pos = convertCoordinateOrigin(radius / radarOption.gridCount * i * Math.cos(1.5), radius / radarOption.gridCount * i * Math.sin(1.5), centerPosition);
  3735. context.arc(centerPosition.x, centerPosition.y, centerPosition.y - pos.y, 0, 2 * Math.PI, false);
  3736. }
  3737. context.stroke();
  3738. context.closePath();
  3739. };
  3740. for (var i = 1; i <= radarOption.gridCount; i++) {
  3741. _loop(i);
  3742. }
  3743. var radarDataPoints = getRadarDataPoints(coordinateAngle, centerPosition, radius, series, opts, process);
  3744. radarDataPoints.forEach(function(eachSeries, seriesIndex) {
  3745. // 绘制区域数据
  3746. context.beginPath();
  3747. context.setFillStyle(hexToRgb(eachSeries.color, radarOption.opacity));
  3748. eachSeries.data.forEach(function(item, index) {
  3749. if (index === 0) {
  3750. context.moveTo(item.position.x, item.position.y);
  3751. } else {
  3752. context.lineTo(item.position.x, item.position.y);
  3753. }
  3754. });
  3755. context.closePath();
  3756. context.fill();
  3757. if (opts.dataPointShape !== false) {
  3758. var points = eachSeries.data.map(function(item) {
  3759. return item.position;
  3760. });
  3761. drawPointShape(points, eachSeries.color, eachSeries.pointShape, context, opts);
  3762. }
  3763. });
  3764. // draw label text
  3765. drawRadarLabel(coordinateAngle, radius, centerPosition, opts, config, context);
  3766. return {
  3767. center: centerPosition,
  3768. radius: radius,
  3769. angleList: coordinateAngle
  3770. };
  3771. }
  3772. function normalInt(min, max, iter) {
  3773. iter = iter==0?1:iter;
  3774. var arr = [];
  3775. for (var i = 0; i < iter; i++) {
  3776. arr[i] = Math.random();
  3777. };
  3778. return Math.floor(arr.reduce(function(i,j){return i+j})/iter*(max-min))+min;
  3779. };
  3780. function collisionNew(area,points,width,height){
  3781. var isIn=false;
  3782. for(let i=0;i<points.length;i++){
  3783. if(points[i].area){
  3784. 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]){
  3785. if(area[0]<0 || area[1]<0 || area[2]>width || area[3]>height){
  3786. isIn=true;
  3787. break;
  3788. }else{
  3789. isIn=false;
  3790. }
  3791. }else{
  3792. isIn=true;
  3793. break;
  3794. }
  3795. }
  3796. }
  3797. return isIn;
  3798. };
  3799. function getBoundingBox(data) {
  3800. var bounds = {}, coords;
  3801. bounds.xMin = 180;
  3802. bounds.xMax = 0;
  3803. bounds.yMin = 90;
  3804. bounds.yMax = 0
  3805. for (var i = 0; i < data.length; i++) {
  3806. var coorda = data[i].geometry.coordinates
  3807. for (var k = 0; k < coorda.length; k++) {
  3808. coords = coorda[k];
  3809. if (coords.length == 1) {
  3810. coords = coords[0]
  3811. }
  3812. for (var j = 0; j < coords.length; j++) {
  3813. var longitude = coords[j][0];
  3814. var latitude = coords[j][1];
  3815. var point = {
  3816. x: longitude,
  3817. y: latitude
  3818. }
  3819. bounds.xMin = bounds.xMin < point.x ? bounds.xMin : point.x;
  3820. bounds.xMax = bounds.xMax > point.x ? bounds.xMax : point.x;
  3821. bounds.yMin = bounds.yMin < point.y ? bounds.yMin : point.y;
  3822. bounds.yMax = bounds.yMax > point.y ? bounds.yMax : point.y;
  3823. }
  3824. }
  3825. }
  3826. return bounds;
  3827. }
  3828. function coordinateToPoint(latitude, longitude,bounds,scale,xoffset,yoffset) {
  3829. return {
  3830. x: (longitude - bounds.xMin) * scale+xoffset,
  3831. y: (bounds.yMax - latitude) * scale+yoffset
  3832. };
  3833. }
  3834. function pointToCoordinate(pointY, pointX,bounds,scale,xoffset,yoffset) {
  3835. return {
  3836. x: (pointX-xoffset)/scale+bounds.xMin,
  3837. y: bounds.yMax - (pointY-yoffset)/scale
  3838. };
  3839. }
  3840. function isRayIntersectsSegment(poi,s_poi,e_poi){
  3841. if (s_poi[1]==e_poi[1]){return false;}
  3842. if (s_poi[1]>poi[1] && e_poi[1]>poi[1]){return false;}
  3843. if (s_poi[1]<poi[1] && e_poi[1]<poi[1]){return false;}
  3844. if (s_poi[1]==poi[1] && e_poi[1]>poi[1]){return false;}
  3845. if (e_poi[1]==poi[1] && s_poi[1]>poi[1]){return false;}
  3846. if (s_poi[0]<poi[0] && e_poi[1]<poi[1]){return false;}
  3847. let xseg=e_poi[0]-(e_poi[0]-s_poi[0])*(e_poi[1]-poi[1])/(e_poi[1]-s_poi[1]);
  3848. if (xseg<poi[0]){
  3849. return false;
  3850. }else{
  3851. return true;
  3852. }
  3853. }
  3854. function isPoiWithinPoly(poi,poly){
  3855. let sinsc=0;
  3856. for (let i=0;i<poly.length;i++){
  3857. let epoly=poly[i][0];
  3858. if (poly.length == 1) {
  3859. epoly = poly[i][0]
  3860. }
  3861. for(let j=0;j<epoly.length-1;j++){
  3862. let s_poi=epoly[j];
  3863. let e_poi=epoly[j+1];
  3864. if (isRayIntersectsSegment(poi,s_poi,e_poi)){
  3865. sinsc+=1;
  3866. }
  3867. }
  3868. }
  3869. if(sinsc%2==1){
  3870. return true;
  3871. }else{
  3872. return false;
  3873. }
  3874. }
  3875. function drawMapDataPoints(series, opts, config, context) {
  3876. var mapOption=assign({},{
  3877. border:true,
  3878. borderWidth:1,
  3879. borderColor:'#666666',
  3880. fillOpacity:0.6,
  3881. activeBorderColor:'#f04864',
  3882. activeFillColor:'#facc14',
  3883. activeFillOpacity:1
  3884. },opts.extra.map);
  3885. var coords, point;
  3886. var data = series;
  3887. var bounds= getBoundingBox(data);
  3888. var xScale = opts.width / Math.abs(bounds.xMax - bounds.xMin);
  3889. var yScale = opts.height / Math.abs(bounds.yMax - bounds.yMin);
  3890. var scale = xScale < yScale ? xScale : yScale;
  3891. var xoffset=opts.width/2-Math.abs(bounds.xMax - bounds.xMin)/2*scale;
  3892. var yoffset=opts.height/2-Math.abs(bounds.yMax - bounds.yMin)/2*scale;
  3893. context.beginPath();
  3894. context.clearRect(0, 0, opts.width, opts.height);
  3895. context.setFillStyle(opts.background||'#FFFFFF');
  3896. context.rect(0,0,opts.width,opts.height);
  3897. context.fill();
  3898. for (var i = 0; i < data.length; i++) {
  3899. context.beginPath();
  3900. context.setLineWidth(mapOption.borderWidth * opts.pixelRatio);
  3901. context.setStrokeStyle(mapOption.borderColor);
  3902. context.setFillStyle(hexToRgb(series[i].color, mapOption.fillOpacity));
  3903. if (opts.tooltip) {
  3904. if (opts.tooltip.index == i ) {
  3905. context.setStrokeStyle(mapOption.activeBorderColor);
  3906. context.setFillStyle(hexToRgb(mapOption.activeFillColor, mapOption.activeFillOpacity));
  3907. }
  3908. }
  3909. var coorda = data[i].geometry.coordinates
  3910. for (var k = 0; k < coorda.length; k++) {
  3911. coords = coorda[k];
  3912. if (coords.length == 1) {
  3913. coords = coords[0]
  3914. }
  3915. for (var j = 0; j < coords.length; j++) {
  3916. point = coordinateToPoint(coords[j][1], coords[j][0],bounds,scale,xoffset,yoffset)
  3917. if (j === 0) {
  3918. context.beginPath();
  3919. context.moveTo(point.x, point.y);
  3920. } else {
  3921. context.lineTo(point.x, point.y);
  3922. }
  3923. }
  3924. context.fill();
  3925. if(mapOption.border == true){
  3926. context.stroke();
  3927. }
  3928. }
  3929. if(opts.dataLabel == true){
  3930. var centerPoint = data[i].properties.centroid;
  3931. if(centerPoint){
  3932. point = coordinateToPoint(centerPoint[1], centerPoint[0],bounds,scale,xoffset,yoffset);
  3933. let fontSize=data[i].textSize||config.fontSize;
  3934. let text=data[i].properties.name;
  3935. context.beginPath();
  3936. context.setFontSize(fontSize)
  3937. context.setFillStyle(data[i].textColor||'#666666')
  3938. context.fillText(text, point.x-measureText(text,fontSize)/2, point.y+fontSize/2);
  3939. context.closePath();
  3940. context.stroke();
  3941. }
  3942. }
  3943. }
  3944. opts.chartData.mapData={
  3945. bounds:bounds,
  3946. scale:scale,
  3947. xoffset:xoffset,
  3948. yoffset:yoffset
  3949. }
  3950. drawToolTipBridge(opts, config, context,1);
  3951. context.draw();
  3952. }
  3953. function getWordCloudPoint(opts,type){
  3954. let points = opts.series.sort(function(a,b){return parseInt(b.textSize)-parseInt(a.textSize);});
  3955. switch (type) {
  3956. case 'normal':
  3957. for (let i = 0; i < points.length; i++) {
  3958. let text = points[i].name;
  3959. let tHeight = points[i].textSize;
  3960. let tWidth = measureText(text,tHeight);
  3961. let x,y;
  3962. let area;
  3963. let breaknum=0;
  3964. while(true) {
  3965. breaknum++;
  3966. x = normalInt(-opts.width/2, opts.width/2,5) - tWidth/2;
  3967. y = normalInt(-opts.height/2, opts.height/2,5) + tHeight/2;
  3968. area=[x-5+opts.width/2,y-5-tHeight+opts.height/2,x+tWidth+5+opts.width/2,y+5+opts.height/2];
  3969. let isCollision = collisionNew(area,points,opts.width,opts.height);
  3970. if (!isCollision) break;
  3971. if (breaknum==1000){
  3972. area=[-100,-100,-100,-100];
  3973. break;
  3974. }
  3975. };
  3976. points[i].area=area;
  3977. }
  3978. break;
  3979. case 'vertical':
  3980. function Spin(){
  3981. //获取均匀随机值,是否旋转,旋转的概率为(1-0.5)
  3982. if (Math.random()>0.7) {
  3983. return true;
  3984. }else {return false};
  3985. };
  3986. for (let i = 0; i < points.length; i++) {
  3987. let text = points[i].name;
  3988. let tHeight = points[i].textSize;
  3989. let tWidth = measureText(text,tHeight);
  3990. let isSpin = Spin();
  3991. let x,y,area,areav;
  3992. let breaknum=0;
  3993. while(true) {
  3994. breaknum++;
  3995. let isCollision;
  3996. if (isSpin) {
  3997. x = normalInt(-opts.width/2, opts.width/2,5) - tWidth/2;
  3998. y = normalInt(-opts.height/2, opts.height/2,5)+tHeight/2;
  3999. area=[y-5-tWidth+opts.width/2,(-x-5+opts.height/2),y+5+opts.width/2,(-x+tHeight+5+opts.height/2)];
  4000. 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];
  4001. isCollision = collisionNew(areav,points,opts.height,opts.width);
  4002. }else{
  4003. x = normalInt(-opts.width/2, opts.width/2,5) - tWidth/2;
  4004. y = normalInt(-opts.height/2, opts.height/2,5)+tHeight/2;
  4005. area=[x-5+opts.width/2,y-5-tHeight+opts.height/2,x+tWidth+5+opts.width/2,y+5+opts.height/2];
  4006. isCollision = collisionNew(area,points,opts.width,opts.height);
  4007. }
  4008. if (!isCollision) break;
  4009. if (breaknum==1000){
  4010. area=[-1000,-1000,-1000,-1000];
  4011. break;
  4012. }
  4013. };
  4014. if (isSpin) {
  4015. points[i].area=areav;
  4016. points[i].areav=area;
  4017. }else{
  4018. points[i].area=area;
  4019. }
  4020. points[i].rotate=isSpin;
  4021. };
  4022. break;
  4023. }
  4024. return points;
  4025. }
  4026. function drawWordCloudDataPoints(series, opts, config, context) {
  4027. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4028. let wordOption = assign({},{
  4029. type: 'normal',
  4030. autoColors: true
  4031. },opts.extra.word);
  4032. context.beginPath();
  4033. context.setFillStyle(opts.background||'#FFFFFF');
  4034. context.rect(0,0,opts.width,opts.height);
  4035. context.fill();
  4036. context.save();
  4037. let points = opts.chartData.wordCloudData;
  4038. context.translate(opts.width/2,opts.height/2);
  4039. for(let i=0;i<points.length;i++){
  4040. context.save();
  4041. if(points[i].rotate){
  4042. context.rotate(90 * Math.PI / 180);
  4043. }
  4044. let text = points[i].name;
  4045. let tHeight = points[i].textSize;
  4046. let tWidth = measureText(text,tHeight);
  4047. context.beginPath();
  4048. context.setStrokeStyle(points[i].color);
  4049. context.setFillStyle(points[i].color);
  4050. context.setFontSize(tHeight);
  4051. if(points[i].rotate){
  4052. if(points[i].areav[0]>0){
  4053. if (opts.tooltip) {
  4054. if (opts.tooltip.index == i) {
  4055. 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);
  4056. }else{
  4057. 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);
  4058. }
  4059. }else{
  4060. 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);
  4061. }
  4062. }
  4063. }else{
  4064. if(points[i].area[0]>0){
  4065. if (opts.tooltip) {
  4066. if (opts.tooltip.index == i) {
  4067. 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);
  4068. }else{
  4069. 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);
  4070. }
  4071. }else{
  4072. 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);
  4073. }
  4074. }
  4075. }
  4076. context.stroke();
  4077. context.restore();
  4078. }
  4079. context.restore();
  4080. }
  4081. function drawFunnelDataPoints(series, opts, config, context) {
  4082. let process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  4083. let funnelOption = assign({},{
  4084. activeWidth:10,
  4085. activeOpacity:0.3,
  4086. border:false,
  4087. borderWidth:2,
  4088. borderColor:'#FFFFFF',
  4089. fillOpacity:1,
  4090. labelAlign:'right'
  4091. },opts.extra.funnel);
  4092. let eachSpacing = (opts.height - opts.area[0] - opts.area[2])/series.length;
  4093. let centerPosition = {
  4094. x: opts.area[3] + (opts.width - opts.area[1] - opts.area[3]) / 2,
  4095. y: opts.height-opts.area[2]
  4096. };
  4097. let activeWidth = funnelOption.activeWidth;
  4098. let radius = Math.min((opts.width - opts.area[1] - opts.area[3]) / 2 - activeWidth, (opts.height - opts.area[0] - opts.area[2]) / 2 - activeWidth);
  4099. series = getFunnelDataPoints(series, radius, process);
  4100. context.save();
  4101. context.translate(centerPosition.x,centerPosition.y);
  4102. for(let i=0;i<series.length;i++){
  4103. if(i==0){
  4104. if (opts.tooltip) {
  4105. if (opts.tooltip.index == i) {
  4106. context.beginPath();
  4107. context.setFillStyle(hexToRgb(series[i].color, funnelOption.activeOpacity));
  4108. context.moveTo(-activeWidth, 0);
  4109. context.lineTo(-series[i].radius-activeWidth, -eachSpacing);
  4110. context.lineTo(series[i].radius+activeWidth, -eachSpacing);
  4111. context.lineTo(activeWidth, 0);
  4112. context.lineTo(-activeWidth, 0);
  4113. context.closePath();
  4114. context.fill();
  4115. }
  4116. }
  4117. series[i].funnelArea=[centerPosition.x-series[i].radius,centerPosition.y-eachSpacing,centerPosition.x+series[i].radius,centerPosition.y];
  4118. context.beginPath();
  4119. context.setLineWidth(funnelOption.borderWidth * opts.pixelRatio);
  4120. context.setStrokeStyle(funnelOption.borderColor);
  4121. context.setFillStyle(hexToRgb(series[i].color, funnelOption.fillOpacity));
  4122. context.moveTo(0, 0);
  4123. context.lineTo(-series[i].radius, -eachSpacing);
  4124. context.lineTo(series[i].radius, -eachSpacing);
  4125. context.lineTo(0, 0);
  4126. context.closePath();
  4127. context.fill();
  4128. if(funnelOption.border == true){
  4129. context.stroke();
  4130. }
  4131. }else{
  4132. if (opts.tooltip) {
  4133. if (opts.tooltip.index == i) {
  4134. context.beginPath();
  4135. context.setFillStyle(hexToRgb(series[i].color, funnelOption.activeOpacity));
  4136. context.moveTo(0, 0);
  4137. context.lineTo(-series[i-1].radius-activeWidth, 0);
  4138. context.lineTo(-series[i].radius-activeWidth, -eachSpacing);
  4139. context.lineTo(series[i].radius+activeWidth, -eachSpacing);
  4140. context.lineTo(series[i-1].radius+activeWidth, 0);
  4141. context.lineTo(0, 0);
  4142. context.closePath();
  4143. context.fill();
  4144. }
  4145. }
  4146. series[i].funnelArea=[centerPosition.x-series[i].radius,centerPosition.y-eachSpacing*(i+1),centerPosition.x+series[i].radius,centerPosition.y-eachSpacing*i];
  4147. context.beginPath();
  4148. context.setLineWidth(funnelOption.borderWidth * opts.pixelRatio);
  4149. context.setStrokeStyle(funnelOption.borderColor);
  4150. context.setFillStyle(hexToRgb(series[i].color, funnelOption.fillOpacity));
  4151. context.moveTo(0, 0);
  4152. context.lineTo(-series[i-1].radius, 0);
  4153. context.lineTo(-series[i].radius, -eachSpacing);
  4154. context.lineTo(series[i].radius, -eachSpacing);
  4155. context.lineTo(series[i-1].radius, 0);
  4156. context.lineTo(0, 0);
  4157. context.closePath();
  4158. context.fill();
  4159. if(funnelOption.border == true){
  4160. context.stroke();
  4161. }
  4162. }
  4163. context.translate(0,-eachSpacing)
  4164. }
  4165. context.restore();
  4166. if (opts.dataLabel !== false && process === 1) {
  4167. drawFunnelText(series, opts, context, eachSpacing, funnelOption.labelAlign, activeWidth, centerPosition);
  4168. }
  4169. return {
  4170. center: centerPosition,
  4171. radius: radius,
  4172. series: series
  4173. };
  4174. }
  4175. function drawFunnelText(series, opts, context, eachSpacing, labelAlign,activeWidth, centerPosition){
  4176. for(let i=0;i<series.length;i++){
  4177. let item = series[i];
  4178. let startX,endX,startY,fontSize;
  4179. let text = item.format ? item.format(+item._proportion_.toFixed(2)) : util.toFixed(item._proportion_ * 100) +'%';
  4180. if(labelAlign == 'right'){
  4181. if(i==0){
  4182. startX=(item.funnelArea[2]+centerPosition.x)/2;
  4183. }else{
  4184. startX=(item.funnelArea[2]+series[i-1].funnelArea[2])/2;
  4185. }
  4186. endX=startX+activeWidth*2;
  4187. startY=item.funnelArea[1]+eachSpacing/2;
  4188. fontSize = item.textSize || opts.fontSize;
  4189. context.setLineWidth(1 * opts.pixelRatio);
  4190. context.setStrokeStyle(item.color);
  4191. context.setFillStyle(item.color);
  4192. context.beginPath();
  4193. context.moveTo(startX,startY );
  4194. context.lineTo(endX,startY);
  4195. context.stroke();
  4196. context.closePath();
  4197. context.beginPath();
  4198. context.moveTo(endX, startY);
  4199. context.arc(endX, startY, 2, 0, 2 * Math.PI);
  4200. context.closePath();
  4201. context.fill();
  4202. context.beginPath();
  4203. context.setFontSize(fontSize);
  4204. context.setFillStyle(item.textColor || '#666666');
  4205. context.fillText(text, endX+5, startY + fontSize/2 -2);
  4206. context.closePath();
  4207. context.stroke();
  4208. context.closePath();
  4209. }else{
  4210. if(i==0){
  4211. startX=(item.funnelArea[0]+centerPosition.x)/2;
  4212. }else{
  4213. startX=(item.funnelArea[0]+series[i-1].funnelArea[0])/2;
  4214. }
  4215. endX=startX-activeWidth*2;
  4216. startY=item.funnelArea[1]+eachSpacing/2;
  4217. fontSize = item.textSize || opts.fontSize;
  4218. context.setLineWidth(1 * opts.pixelRatio);
  4219. context.setStrokeStyle(item.color);
  4220. context.setFillStyle(item.color);
  4221. context.beginPath();
  4222. context.moveTo(startX,startY );
  4223. context.lineTo(endX,startY);
  4224. context.stroke();
  4225. context.closePath();
  4226. context.beginPath();
  4227. context.moveTo(endX, startY);
  4228. context.arc(endX, startY, 2, 0, 2 * Math.PI);
  4229. context.closePath();
  4230. context.fill();
  4231. context.beginPath();
  4232. context.setFontSize(fontSize);
  4233. context.setFillStyle(item.textColor || '#666666');
  4234. context.fillText(text, endX-5-measureText(text), startY + fontSize/2 -2);
  4235. context.closePath();
  4236. context.stroke();
  4237. context.closePath();
  4238. }
  4239. }
  4240. }
  4241. function drawCanvas(opts, context) {
  4242. context.draw();
  4243. }
  4244. var Timing = {
  4245. easeIn: function easeIn(pos) {
  4246. return Math.pow(pos, 3);
  4247. },
  4248. easeOut: function easeOut(pos) {
  4249. return Math.pow(pos - 1, 3) + 1;
  4250. },
  4251. easeInOut: function easeInOut(pos) {
  4252. if ((pos /= 0.5) < 1) {
  4253. return 0.5 * Math.pow(pos, 3);
  4254. } else {
  4255. return 0.5 * (Math.pow(pos - 2, 3) + 2);
  4256. }
  4257. },
  4258. linear: function linear(pos) {
  4259. return pos;
  4260. }
  4261. };
  4262. function Animation(opts) {
  4263. this.isStop = false;
  4264. opts.duration = typeof opts.duration === 'undefined' ? 1000 : opts.duration;
  4265. opts.timing = opts.timing || 'linear';
  4266. var delay = 17;
  4267. function createAnimationFrame() {
  4268. if (typeof setTimeout !== 'undefined') {
  4269. return function(step, delay) {
  4270. setTimeout(function() {
  4271. var timeStamp = +new Date();
  4272. step(timeStamp);
  4273. }, delay);
  4274. };
  4275. } else if (typeof requestAnimationFrame !== 'undefined') {
  4276. return requestAnimationFrame;
  4277. } else {
  4278. return function(step) {
  4279. step(null);
  4280. };
  4281. }
  4282. };
  4283. var animationFrame = createAnimationFrame();
  4284. var startTimeStamp = null;
  4285. var _step = function step(timestamp) {
  4286. if (timestamp === null || this.isStop === true) {
  4287. opts.onProcess && opts.onProcess(1);
  4288. opts.onAnimationFinish && opts.onAnimationFinish();
  4289. return;
  4290. }
  4291. if (startTimeStamp === null) {
  4292. startTimeStamp = timestamp;
  4293. }
  4294. if (timestamp - startTimeStamp < opts.duration) {
  4295. var process = (timestamp - startTimeStamp) / opts.duration;
  4296. var timingFunction = Timing[opts.timing];
  4297. process = timingFunction(process);
  4298. opts.onProcess && opts.onProcess(process);
  4299. animationFrame(_step, delay);
  4300. } else {
  4301. opts.onProcess && opts.onProcess(1);
  4302. opts.onAnimationFinish && opts.onAnimationFinish();
  4303. }
  4304. };
  4305. _step = _step.bind(this);
  4306. animationFrame(_step, delay);
  4307. }
  4308. // stop animation immediately
  4309. // and tigger onAnimationFinish
  4310. Animation.prototype.stop = function() {
  4311. this.isStop = true;
  4312. };
  4313. function drawCharts(type, opts, config, context) {
  4314. var _this = this;
  4315. var series = opts.series;
  4316. var categories = opts.categories;
  4317. series = fillSeries(series, opts, config);
  4318. var duration = opts.animation ? opts.duration : 0;
  4319. _this.animationInstance && _this.animationInstance.stop();
  4320. var seriesMA = null;
  4321. if (type == 'candle') {
  4322. let average = assign({}, opts.extra.candle.average);
  4323. if (average.show) {
  4324. seriesMA = calCandleMA(average.day, average.name, average.color, series[0].data);
  4325. seriesMA = fillSeries(seriesMA, opts, config);
  4326. opts.seriesMA = seriesMA;
  4327. } else if (opts.seriesMA) {
  4328. seriesMA = opts.seriesMA = fillSeries(opts.seriesMA, opts, config);
  4329. } else {
  4330. seriesMA = series;
  4331. }
  4332. } else {
  4333. seriesMA = series;
  4334. }
  4335. /* 过滤掉show=false的series */
  4336. opts._series_ = series = filterSeries(series);
  4337. //重新计算图表区域
  4338. opts.area = new Array(4);
  4339. //复位绘图区域
  4340. for (let j = 0; j < 4; j++) {
  4341. opts.area[j] = opts.padding[j];
  4342. }
  4343. //通过计算三大区域:图例、X轴、Y轴的大小,确定绘图区域
  4344. var _calLegendData = calLegendData(seriesMA, opts, config, opts.chartData),
  4345. legendHeight = _calLegendData.area.wholeHeight,
  4346. legendWidth = _calLegendData.area.wholeWidth;
  4347. switch (opts.legend.position) {
  4348. case 'top':
  4349. opts.area[0] += legendHeight;
  4350. break;
  4351. case 'bottom':
  4352. opts.area[2] += legendHeight;
  4353. break;
  4354. case 'left':
  4355. opts.area[3] += legendWidth;
  4356. break;
  4357. case 'right':
  4358. opts.area[1] += legendWidth;
  4359. break;
  4360. }
  4361. let _calYAxisData = {},yAxisWidth = 0;
  4362. if (opts.type === 'line' || opts.type === 'column' || opts.type === 'area' || opts.type === 'mix' || opts.type === 'candle') {
  4363. _calYAxisData = calYAxisData(series, opts, config);
  4364. yAxisWidth = _calYAxisData.yAxisWidth;
  4365. //如果显示Y轴标题
  4366. if(opts.yAxis.showTitle){
  4367. let maxTitleHeight=0;
  4368. for(let i=0;i<opts.yAxis.data.length;i++){
  4369. maxTitleHeight = Math.max(maxTitleHeight,opts.yAxis.data[i].titleFontSize?opts.yAxis.data[i].titleFontSize:config.fontSize)
  4370. }
  4371. opts.area[0] += (maxTitleHeight+6)*opts.pixelRatio;
  4372. }
  4373. let rightIndex=0,leftIndex=0;
  4374. //计算主绘图区域左右位置
  4375. for(let i=0;i<yAxisWidth.length;i++){
  4376. if(yAxisWidth[i].position=='left'){
  4377. if(leftIndex>0){
  4378. opts.area[3] += yAxisWidth[i].width + opts.yAxis.padding;
  4379. }else{
  4380. opts.area[3] += yAxisWidth[i].width;
  4381. }
  4382. leftIndex +=1;
  4383. }else{
  4384. if(rightIndex>0){
  4385. opts.area[1] += yAxisWidth[i].width + opts.yAxis.padding;
  4386. }else{
  4387. opts.area[1] += yAxisWidth[i].width;
  4388. }
  4389. rightIndex +=1;
  4390. }
  4391. }
  4392. }else{
  4393. config.yAxisWidth = yAxisWidth;
  4394. }
  4395. opts.chartData.yAxisData = _calYAxisData;
  4396. if (opts.categories && opts.categories.length) {
  4397. opts.chartData.xAxisData = getXAxisPoints(opts.categories, opts, config);
  4398. let _calCategoriesData = calCategoriesData(opts.categories, opts, config, opts.chartData.xAxisData.eachSpacing),
  4399. xAxisHeight = _calCategoriesData.xAxisHeight,
  4400. angle = _calCategoriesData.angle;
  4401. config.xAxisHeight = xAxisHeight;
  4402. config._xAxisTextAngle_ = angle;
  4403. opts.area[2] += xAxisHeight;
  4404. opts.chartData.categoriesData = _calCategoriesData;
  4405. }else{
  4406. if (opts.type === 'line' || opts.type === 'area' || opts.type === 'points') {
  4407. opts.chartData.xAxisData = calXAxisData(series, opts, config);
  4408. categories=opts.chartData.xAxisData.rangesFormat;
  4409. let _calCategoriesData = calCategoriesData(categories, opts, config, opts.chartData.xAxisData.eachSpacing),
  4410. xAxisHeight = _calCategoriesData.xAxisHeight,
  4411. angle = _calCategoriesData.angle;
  4412. config.xAxisHeight = xAxisHeight;
  4413. config._xAxisTextAngle_ = angle;
  4414. opts.area[2] += xAxisHeight;
  4415. opts.chartData.categoriesData = _calCategoriesData;
  4416. }else{
  4417. opts.chartData.xAxisData={
  4418. xAxisPoints: []
  4419. };
  4420. }
  4421. }
  4422. //计算右对齐偏移距离
  4423. if (opts.enableScroll && opts.xAxis.scrollAlign == 'right' && opts._scrollDistance_ === undefined) {
  4424. let offsetLeft = 0,
  4425. xAxisPoints = opts.chartData.xAxisData.xAxisPoints,
  4426. startX = opts.chartData.xAxisData.startX,
  4427. endX = opts.chartData.xAxisData.endX,
  4428. eachSpacing = opts.chartData.xAxisData.eachSpacing;
  4429. let totalWidth = eachSpacing * (xAxisPoints.length - 1);
  4430. let screenWidth = endX - startX;
  4431. offsetLeft = screenWidth - totalWidth;
  4432. _this.scrollOption = {
  4433. currentOffset: offsetLeft,
  4434. startTouchX: offsetLeft,
  4435. distance: 0,
  4436. lastMoveTime: 0
  4437. };
  4438. opts._scrollDistance_ = offsetLeft;
  4439. }
  4440. if (type === 'pie' || type === 'ring' || type === 'rose') {
  4441. config._pieTextMaxLength_ = opts.dataLabel === false ? 0 : getPieTextMaxLength(seriesMA);
  4442. }
  4443. switch (type) {
  4444. case 'word':
  4445. let wordOption = assign({},{
  4446. type: 'normal',
  4447. autoColors: true
  4448. },opts.extra.word);
  4449. if(opts.updateData==true || opts.updateData==undefined){
  4450. opts.chartData.wordCloudData=getWordCloudPoint(opts,wordOption.type);
  4451. }
  4452. this.animationInstance = new Animation({
  4453. timing: 'easeInOut',
  4454. duration: duration,
  4455. onProcess: function(process) {
  4456. context.clearRect(0, 0, opts.width, opts.height);
  4457. if (opts.rotate) {
  4458. contextRotate(context, opts);
  4459. }
  4460. drawWordCloudDataPoints(series, opts, config, context,process);
  4461. drawCanvas(opts, context);
  4462. },
  4463. onAnimationFinish: function onAnimationFinish() {
  4464. _this.event.trigger('renderComplete');
  4465. }
  4466. });
  4467. break;
  4468. case 'map':
  4469. context.clearRect(0, 0, opts.width, opts.height);
  4470. drawMapDataPoints(series, opts, config, context);
  4471. break;
  4472. case 'funnel':
  4473. this.animationInstance = new Animation({
  4474. timing: 'easeInOut',
  4475. duration: duration,
  4476. onProcess: function(process) {
  4477. context.clearRect(0, 0, opts.width, opts.height);
  4478. if (opts.rotate) {
  4479. contextRotate(context, opts);
  4480. }
  4481. opts.chartData.funnelData = drawFunnelDataPoints(series, opts, config, context, process);
  4482. drawLegend(opts.series, opts, config, context, opts.chartData);
  4483. drawToolTipBridge(opts, config, context, process);
  4484. drawCanvas(opts, context);
  4485. },
  4486. onAnimationFinish: function onAnimationFinish() {
  4487. _this.event.trigger('renderComplete');
  4488. }
  4489. });
  4490. break;
  4491. case 'line':
  4492. this.animationInstance = new Animation({
  4493. timing: 'easeIn',
  4494. duration: duration,
  4495. onProcess: function onProcess(process) {
  4496. context.clearRect(0, 0, opts.width, opts.height);
  4497. if (opts.rotate) {
  4498. contextRotate(context, opts);
  4499. }
  4500. drawYAxisGrid(categories, opts, config, context);
  4501. drawXAxis(categories, opts, config, context);
  4502. var _drawLineDataPoints = drawLineDataPoints(series, opts, config, context, process),
  4503. xAxisPoints = _drawLineDataPoints.xAxisPoints,
  4504. calPoints = _drawLineDataPoints.calPoints,
  4505. eachSpacing = _drawLineDataPoints.eachSpacing;
  4506. opts.chartData.xAxisPoints = xAxisPoints;
  4507. opts.chartData.calPoints = calPoints;
  4508. opts.chartData.eachSpacing = eachSpacing;
  4509. drawYAxis(series, opts, config, context);
  4510. if (opts.enableMarkLine !== false && process === 1) {
  4511. drawMarkLine(opts, config, context);
  4512. }
  4513. drawLegend(opts.series, opts, config, context, opts.chartData);
  4514. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  4515. drawCanvas(opts, context);
  4516. },
  4517. onAnimationFinish: function onAnimationFinish() {
  4518. _this.event.trigger('renderComplete');
  4519. }
  4520. });
  4521. break;
  4522. case 'mix':
  4523. this.animationInstance = new Animation({
  4524. timing: 'easeIn',
  4525. duration: duration,
  4526. onProcess: function onProcess(process) {
  4527. context.clearRect(0, 0, opts.width, opts.height);
  4528. if (opts.rotate) {
  4529. contextRotate(context, opts);
  4530. }
  4531. drawYAxisGrid(categories, opts, config, context);
  4532. drawXAxis(categories, opts, config, context);
  4533. var _drawMixDataPoints = drawMixDataPoints(series, opts, config, context, process),
  4534. xAxisPoints = _drawMixDataPoints.xAxisPoints,
  4535. calPoints = _drawMixDataPoints.calPoints,
  4536. eachSpacing = _drawMixDataPoints.eachSpacing;
  4537. opts.chartData.xAxisPoints = xAxisPoints;
  4538. opts.chartData.calPoints = calPoints;
  4539. opts.chartData.eachSpacing = eachSpacing;
  4540. drawYAxis(series, opts, config, context);
  4541. if (opts.enableMarkLine !== false && process === 1) {
  4542. drawMarkLine(opts, config, context);
  4543. }
  4544. drawLegend(opts.series, opts, config, context, opts.chartData);
  4545. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  4546. drawCanvas(opts, context);
  4547. },
  4548. onAnimationFinish: function onAnimationFinish() {
  4549. _this.event.trigger('renderComplete');
  4550. }
  4551. });
  4552. break;
  4553. case 'column':
  4554. this.animationInstance = new Animation({
  4555. timing: 'easeIn',
  4556. duration: duration,
  4557. onProcess: function onProcess(process) {
  4558. context.clearRect(0, 0, opts.width, opts.height);
  4559. if (opts.rotate) {
  4560. contextRotate(context, opts);
  4561. }
  4562. drawYAxisGrid(categories, opts, config, context);
  4563. drawXAxis(categories, opts, config, context);
  4564. var _drawColumnDataPoints = drawColumnDataPoints(series, opts, config, context, process),
  4565. xAxisPoints = _drawColumnDataPoints.xAxisPoints,
  4566. calPoints = _drawColumnDataPoints.calPoints,
  4567. eachSpacing = _drawColumnDataPoints.eachSpacing;
  4568. opts.chartData.xAxisPoints = xAxisPoints;
  4569. opts.chartData.calPoints = calPoints;
  4570. opts.chartData.eachSpacing = eachSpacing;
  4571. drawYAxis(series, opts, config, context);
  4572. if (opts.enableMarkLine !== false && process === 1) {
  4573. drawMarkLine(opts, config, context);
  4574. }
  4575. drawLegend(opts.series, opts, config, context, opts.chartData);
  4576. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  4577. drawCanvas(opts, context);
  4578. },
  4579. onAnimationFinish: function onAnimationFinish() {
  4580. _this.event.trigger('renderComplete');
  4581. }
  4582. });
  4583. break;
  4584. case 'area':
  4585. this.animationInstance = new Animation({
  4586. timing: 'easeIn',
  4587. duration: duration,
  4588. onProcess: function onProcess(process) {
  4589. context.clearRect(0, 0, opts.width, opts.height);
  4590. if (opts.rotate) {
  4591. contextRotate(context, opts);
  4592. }
  4593. drawYAxisGrid(categories, opts, config, context);
  4594. drawXAxis(categories, opts, config, context);
  4595. var _drawAreaDataPoints = drawAreaDataPoints(series, opts, config, context, process),
  4596. xAxisPoints = _drawAreaDataPoints.xAxisPoints,
  4597. calPoints = _drawAreaDataPoints.calPoints,
  4598. eachSpacing = _drawAreaDataPoints.eachSpacing;
  4599. opts.chartData.xAxisPoints = xAxisPoints;
  4600. opts.chartData.calPoints = calPoints;
  4601. opts.chartData.eachSpacing = eachSpacing;
  4602. drawYAxis(series, opts, config, context);
  4603. if (opts.enableMarkLine !== false && process === 1) {
  4604. drawMarkLine(opts, config, context);
  4605. }
  4606. drawLegend(opts.series, opts, config, context, opts.chartData);
  4607. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  4608. drawCanvas(opts, context);
  4609. },
  4610. onAnimationFinish: function onAnimationFinish() {
  4611. _this.event.trigger('renderComplete');
  4612. }
  4613. });
  4614. break;
  4615. case 'ring':
  4616. case 'pie':
  4617. this.animationInstance = new Animation({
  4618. timing: 'easeInOut',
  4619. duration: duration,
  4620. onProcess: function onProcess(process) {
  4621. context.clearRect(0, 0, opts.width, opts.height);
  4622. if (opts.rotate) {
  4623. contextRotate(context, opts);
  4624. }
  4625. opts.chartData.pieData = drawPieDataPoints(series, opts, config, context, process);
  4626. drawLegend(opts.series, opts, config, context, opts.chartData);
  4627. drawToolTipBridge(opts, config, context, process);
  4628. drawCanvas(opts, context);
  4629. },
  4630. onAnimationFinish: function onAnimationFinish() {
  4631. _this.event.trigger('renderComplete');
  4632. }
  4633. });
  4634. break;
  4635. case 'rose':
  4636. this.animationInstance = new Animation({
  4637. timing: 'easeInOut',
  4638. duration: duration,
  4639. onProcess: function onProcess(process) {
  4640. context.clearRect(0, 0, opts.width, opts.height);
  4641. if (opts.rotate) {
  4642. contextRotate(context, opts);
  4643. }
  4644. opts.chartData.pieData = drawRoseDataPoints(series, opts, config, context, process);
  4645. drawLegend(opts.series, opts, config, context, opts.chartData);
  4646. drawToolTipBridge(opts, config, context, process);
  4647. drawCanvas(opts, context);
  4648. },
  4649. onAnimationFinish: function onAnimationFinish() {
  4650. _this.event.trigger('renderComplete');
  4651. }
  4652. });
  4653. break;
  4654. case 'radar':
  4655. this.animationInstance = new Animation({
  4656. timing: 'easeInOut',
  4657. duration: duration,
  4658. onProcess: function onProcess(process) {
  4659. context.clearRect(0, 0, opts.width, opts.height);
  4660. if (opts.rotate) {
  4661. contextRotate(context, opts);
  4662. }
  4663. opts.chartData.radarData = drawRadarDataPoints(series, opts, config, context, process);
  4664. drawLegend(opts.series, opts, config, context, opts.chartData);
  4665. drawToolTipBridge(opts, config, context, process);
  4666. drawCanvas(opts, context);
  4667. },
  4668. onAnimationFinish: function onAnimationFinish() {
  4669. _this.event.trigger('renderComplete');
  4670. }
  4671. });
  4672. break;
  4673. case 'arcbar':
  4674. this.animationInstance = new Animation({
  4675. timing: 'easeInOut',
  4676. duration: duration,
  4677. onProcess: function onProcess(process) {
  4678. context.clearRect(0, 0, opts.width, opts.height);
  4679. if (opts.rotate) {
  4680. contextRotate(context, opts);
  4681. }
  4682. opts.chartData.arcbarData = drawArcbarDataPoints(series, opts, config, context, process);
  4683. drawCanvas(opts, context);
  4684. },
  4685. onAnimationFinish: function onAnimationFinish() {
  4686. _this.event.trigger('renderComplete');
  4687. }
  4688. });
  4689. break;
  4690. case 'gauge':
  4691. this.animationInstance = new Animation({
  4692. timing: 'easeInOut',
  4693. duration: duration,
  4694. onProcess: function onProcess(process) {
  4695. context.clearRect(0, 0, opts.width, opts.height);
  4696. if (opts.rotate) {
  4697. contextRotate(context, opts);
  4698. }
  4699. opts.chartData.gaugeData = drawGaugeDataPoints(categories, series, opts, config, context, process);
  4700. drawCanvas(opts, context);
  4701. },
  4702. onAnimationFinish: function onAnimationFinish() {
  4703. _this.event.trigger('renderComplete');
  4704. }
  4705. });
  4706. break;
  4707. case 'candle':
  4708. this.animationInstance = new Animation({
  4709. timing: 'easeIn',
  4710. duration: duration,
  4711. onProcess: function onProcess(process) {
  4712. context.clearRect(0, 0, opts.width, opts.height);
  4713. if (opts.rotate) {
  4714. contextRotate(context, opts);
  4715. }
  4716. drawYAxisGrid(categories, opts, config, context);
  4717. drawXAxis(categories, opts, config, context);
  4718. var _drawCandleDataPoints = drawCandleDataPoints(series, seriesMA, opts, config, context, process),
  4719. xAxisPoints = _drawCandleDataPoints.xAxisPoints,
  4720. calPoints = _drawCandleDataPoints.calPoints,
  4721. eachSpacing = _drawCandleDataPoints.eachSpacing;
  4722. opts.chartData.xAxisPoints = xAxisPoints;
  4723. opts.chartData.calPoints = calPoints;
  4724. opts.chartData.eachSpacing = eachSpacing;
  4725. drawYAxis(series, opts, config, context);
  4726. if (opts.enableMarkLine !== false && process === 1) {
  4727. drawMarkLine(opts, config, context);
  4728. }
  4729. if (seriesMA) {
  4730. drawLegend(seriesMA, opts, config, context, opts.chartData);
  4731. } else {
  4732. drawLegend(opts.series, opts, config, context, opts.chartData);
  4733. }
  4734. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  4735. drawCanvas(opts, context);
  4736. },
  4737. onAnimationFinish: function onAnimationFinish() {
  4738. _this.event.trigger('renderComplete');
  4739. }
  4740. });
  4741. break;
  4742. }
  4743. }
  4744. // simple event implement
  4745. function Event() {
  4746. this.events = {};
  4747. }
  4748. Event.prototype.addEventListener = function(type, listener) {
  4749. this.events[type] = this.events[type] || [];
  4750. this.events[type].push(listener);
  4751. };
  4752. Event.prototype.trigger = function() {
  4753. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  4754. args[_key] = arguments[_key];
  4755. }
  4756. var type = args[0];
  4757. var params = args.slice(1);
  4758. if (!!this.events[type]) {
  4759. this.events[type].forEach(function(listener) {
  4760. try {
  4761. listener.apply(null, params);
  4762. } catch (e) {
  4763. console.error(e);
  4764. }
  4765. });
  4766. }
  4767. };
  4768. var Charts = function Charts(opts) {
  4769. opts.pixelRatio = opts.pixelRatio ? opts.pixelRatio : 1;
  4770. opts.fontSize = opts.fontSize ? opts.fontSize * opts.pixelRatio : 13 * opts.pixelRatio;
  4771. opts.title = assign({}, opts.title);
  4772. opts.subtitle = assign({}, opts.subtitle);
  4773. opts.duration = opts.duration ? opts.duration : 1000;
  4774. opts.yAxis = assign({}, {
  4775. data:[],
  4776. showTitle:false,
  4777. disabled:false,
  4778. disableGrid:false,
  4779. splitNumber:5,
  4780. gridType: 'solid',
  4781. dashLength: 4 * opts.pixelRatio,
  4782. gridColor:'#cccccc',
  4783. padding:10,
  4784. fontColor:'#666666'
  4785. }, opts.yAxis);
  4786. opts.yAxis.dashLength *= opts.pixelRatio;
  4787. opts.yAxis.padding *= opts.pixelRatio;
  4788. opts.xAxis = assign({}, {
  4789. rotateLabel: false,
  4790. type: 'calibration',
  4791. gridType: 'solid',
  4792. dashLength: 4,
  4793. scrollAlign: 'left',
  4794. boundaryGap:'center',
  4795. axisLine:true,
  4796. axisLineColor:'#cccccc'
  4797. }, opts.xAxis);
  4798. opts.xAxis.dashLength *= opts.pixelRatio;
  4799. opts.legend = assign({}, {
  4800. show: true,
  4801. position: 'bottom',
  4802. float: 'center',
  4803. backgroundColor: 'rgba(0,0,0,0)',
  4804. borderColor: 'rgba(0,0,0,0)',
  4805. borderWidth: 0,
  4806. padding: 5,
  4807. margin: 5,
  4808. itemGap: 10,
  4809. fontSize: opts.fontSize,
  4810. lineHeight: opts.fontSize,
  4811. fontColor: '#333333',
  4812. format: {},
  4813. hiddenColor: '#CECECE'
  4814. }, opts.legend);
  4815. opts.legend.borderWidth = opts.legend.borderWidth * opts.pixelRatio;
  4816. opts.legend.itemGap = opts.legend.itemGap * opts.pixelRatio;
  4817. opts.legend.padding = opts.legend.padding * opts.pixelRatio;
  4818. opts.legend.margin = opts.legend.margin * opts.pixelRatio;
  4819. opts.extra = assign({}, opts.extra);
  4820. opts.rotate = opts.rotate ? true : false;
  4821. opts.animation = opts.animation ? true : false;
  4822. opts.rotate = opts.rotate ? true : false;
  4823. opts.canvas2d = opts.canvas2d ? true : false;
  4824. let config$$1 = JSON.parse(JSON.stringify(config));
  4825. config$$1.colors = opts.colors ? opts.colors : config$$1.colors;
  4826. config$$1.yAxisTitleWidth = opts.yAxis.disabled !== true && opts.yAxis.title ? config$$1.yAxisTitleWidth : 0;
  4827. if (opts.type == 'pie' || opts.type == 'ring') {
  4828. config$$1.pieChartLinePadding = opts.dataLabel === false ? 0 : opts.extra.pie.labelWidth * opts.pixelRatio || config$$1.pieChartLinePadding * opts.pixelRatio;
  4829. }
  4830. if (opts.type == 'rose') {
  4831. config$$1.pieChartLinePadding = opts.dataLabel === false ? 0 : opts.extra.rose.labelWidth * opts.pixelRatio || config$$1.pieChartLinePadding * opts.pixelRatio;
  4832. }
  4833. config$$1.pieChartTextPadding = opts.dataLabel === false ? 0 : config$$1.pieChartTextPadding * opts.pixelRatio;
  4834. config$$1.yAxisSplit = opts.yAxis.splitNumber ? opts.yAxis.splitNumber : config.yAxisSplit;
  4835. //屏幕旋转
  4836. config$$1.rotate = opts.rotate;
  4837. if (opts.rotate) {
  4838. let tempWidth = opts.width;
  4839. let tempHeight = opts.height;
  4840. opts.width = tempHeight;
  4841. opts.height = tempWidth;
  4842. }
  4843. //适配高分屏
  4844. opts.padding = opts.padding ? opts.padding : config$$1.padding;
  4845. for (let i = 0; i < 4; i++) {
  4846. opts.padding[i] *= opts.pixelRatio;
  4847. }
  4848. config$$1.yAxisWidth = config.yAxisWidth * opts.pixelRatio;
  4849. config$$1.xAxisHeight = config.xAxisHeight * opts.pixelRatio;
  4850. if (opts.enableScroll && opts.xAxis.scrollShow) {
  4851. config$$1.xAxisHeight += 6 * opts.pixelRatio;
  4852. }
  4853. config$$1.xAxisLineHeight = config.xAxisLineHeight * opts.pixelRatio;
  4854. config$$1.fontSize = opts.fontSize;
  4855. config$$1.titleFontSize = config.titleFontSize * opts.pixelRatio;
  4856. config$$1.subtitleFontSize = config.subtitleFontSize * opts.pixelRatio;
  4857. config$$1.toolTipPadding = config.toolTipPadding * opts.pixelRatio;
  4858. config$$1.toolTipLineHeight = config.toolTipLineHeight * opts.pixelRatio;
  4859. config$$1.columePadding = config.columePadding * opts.pixelRatio;
  4860. opts.$this = opts.$this ? opts.$this : this;
  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.getCurrentDataIndex = function(e) {
  4980. var touches = null;
  4981. if (e.changedTouches) {
  4982. touches = e.changedTouches[0];
  4983. } else {
  4984. touches = e.mp.changedTouches[0];
  4985. }
  4986. if (touches) {
  4987. let _touches$ = getTouches(touches, this.opts, e);
  4988. if (this.opts.type === 'pie' || this.opts.type === 'ring' || this.opts.type === 'rose') {
  4989. return findPieChartCurrentIndex({
  4990. x: _touches$.x,
  4991. y: _touches$.y
  4992. }, this.opts.chartData.pieData);
  4993. } else if (this.opts.type === 'radar') {
  4994. return findRadarChartCurrentIndex({
  4995. x: _touches$.x,
  4996. y: _touches$.y
  4997. }, this.opts.chartData.radarData, this.opts.categories.length);
  4998. } else if (this.opts.type === 'funnel') {
  4999. return findFunnelChartCurrentIndex({
  5000. x: _touches$.x,
  5001. y: _touches$.y
  5002. }, this.opts.chartData.funnelData);
  5003. } else if (this.opts.type === 'map') {
  5004. return findMapChartCurrentIndex({
  5005. x: _touches$.x,
  5006. y: _touches$.y
  5007. }, this.opts);
  5008. }else if (this.opts.type === 'word') {
  5009. return findWordChartCurrentIndex({
  5010. x: _touches$.x,
  5011. y: _touches$.y
  5012. }, this.opts.chartData.wordCloudData);
  5013. } else {
  5014. return findCurrentIndex({
  5015. x: _touches$.x,
  5016. y: _touches$.y
  5017. }, this.opts.chartData.calPoints, this.opts, this.config, Math.abs(this.scrollOption.currentOffset));
  5018. }
  5019. }
  5020. return -1;
  5021. };
  5022. Charts.prototype.getLegendDataIndex = function(e) {
  5023. var touches = null;
  5024. if (e.changedTouches) {
  5025. touches = e.changedTouches[0];
  5026. } else {
  5027. touches = e.mp.changedTouches[0];
  5028. }
  5029. if (touches) {
  5030. let _touches$ = getTouches(touches, this.opts, e);
  5031. return findLegendIndex({
  5032. x: _touches$.x,
  5033. y: _touches$.y
  5034. }, this.opts.chartData.legendData);
  5035. }
  5036. return -1;
  5037. };
  5038. Charts.prototype.touchLegend = function(e) {
  5039. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  5040. var touches = null;
  5041. if (e.changedTouches) {
  5042. touches = e.changedTouches[0];
  5043. } else {
  5044. touches = e.mp.changedTouches[0];
  5045. }
  5046. if (touches) {
  5047. var _touches$ = getTouches(touches, this.opts, e);
  5048. var index = this.getLegendDataIndex(e);
  5049. if (index >= 0) {
  5050. this.opts.series[index].show = !this.opts.series[index].show;
  5051. this.opts.animation = option.animation ? true : false;
  5052. this.opts._scrollDistance_= this.scrollOption.currentOffset;
  5053. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  5054. }
  5055. }
  5056. };
  5057. Charts.prototype.showToolTip = function(e) {
  5058. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  5059. var touches = null;
  5060. if (e.changedTouches) {
  5061. touches = e.changedTouches[0];
  5062. } else {
  5063. touches = e.mp.changedTouches[0];
  5064. }
  5065. if (!touches) {
  5066. console.log("touchError");
  5067. }
  5068. var _touches$ = getTouches(touches, this.opts, e);
  5069. var currentOffset = this.scrollOption.currentOffset;
  5070. var opts = assign({}, this.opts, {
  5071. _scrollDistance_: currentOffset,
  5072. animation: false
  5073. });
  5074. if (this.opts.type === 'line' || this.opts.type === 'area' || this.opts.type === 'column') {
  5075. var index = option.index==undefined? this.getCurrentDataIndex(e):option.index ;
  5076. if (index > -1) {
  5077. var seriesData = getSeriesDataItem(this.opts.series, index);
  5078. if (seriesData.length !== 0) {
  5079. var _getToolTipData = getToolTipData(seriesData, this.opts.chartData.calPoints, index, this.opts.categories,option),
  5080. textList = _getToolTipData.textList,
  5081. offset = _getToolTipData.offset;
  5082. offset.y = _touches$.y;
  5083. opts.tooltip = {
  5084. textList: option.textList?option.textList:textList,
  5085. offset: offset,
  5086. option: option,
  5087. index: index
  5088. };
  5089. }
  5090. }
  5091. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5092. }
  5093. if (this.opts.type === 'mix') {
  5094. var index = option.index==undefined? this.getCurrentDataIndex(e):option.index ;
  5095. if (index > -1) {
  5096. var currentOffset = this.scrollOption.currentOffset;
  5097. var opts = assign({}, this.opts, {
  5098. _scrollDistance_: currentOffset,
  5099. animation: false
  5100. });
  5101. var seriesData = getSeriesDataItem(this.opts.series, index);
  5102. if (seriesData.length !== 0) {
  5103. var _getMixToolTipData = getMixToolTipData(seriesData, this.opts.chartData.calPoints, index, this.opts.categories,option),
  5104. textList = _getMixToolTipData.textList,
  5105. offset = _getMixToolTipData.offset;
  5106. offset.y = _touches$.y;
  5107. opts.tooltip = {
  5108. textList: option.textList?option.textList:textList,
  5109. offset: offset,
  5110. option: option,
  5111. index: index
  5112. };
  5113. }
  5114. }
  5115. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5116. }
  5117. if (this.opts.type === 'candle') {
  5118. var index = option.index==undefined? this.getCurrentDataIndex(e):option.index ;
  5119. if (index > -1) {
  5120. var currentOffset = this.scrollOption.currentOffset;
  5121. var opts = assign({}, this.opts, {
  5122. _scrollDistance_: currentOffset,
  5123. animation: false
  5124. });
  5125. var seriesData = getSeriesDataItem(this.opts.series, index);
  5126. if (seriesData.length !== 0) {
  5127. var _getToolTipData = getCandleToolTipData(this.opts.series[0].data, seriesData, this.opts.chartData.calPoints,
  5128. index, this.opts.categories, this.opts.extra.candle, option),
  5129. textList = _getToolTipData.textList,
  5130. offset = _getToolTipData.offset;
  5131. offset.y = _touches$.y;
  5132. opts.tooltip = {
  5133. textList: option.textList?option.textList:textList,
  5134. offset: offset,
  5135. option: option,
  5136. index: index
  5137. };
  5138. }
  5139. }
  5140. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5141. }
  5142. if (this.opts.type === 'pie' || this.opts.type === 'ring' || this.opts.type === 'rose'||this.opts.type === 'funnel' ) {
  5143. var index = option.index==undefined? this.getCurrentDataIndex(e):option.index ;
  5144. if (index > -1) {
  5145. var currentOffset = this.scrollOption.currentOffset;
  5146. var opts = assign({}, this.opts, {
  5147. _scrollDistance_: currentOffset,
  5148. animation: false
  5149. });
  5150. var seriesData = this.opts._series_[index];
  5151. var textList = [{
  5152. text: option.format ? option.format(seriesData) : seriesData.name + ': ' + seriesData.data,
  5153. color: seriesData.color
  5154. }];
  5155. var offset = {
  5156. x: _touches$.x,
  5157. y: _touches$.y
  5158. };
  5159. opts.tooltip = {
  5160. textList: option.textList?option.textList:textList,
  5161. offset: offset,
  5162. option: option,
  5163. index: index
  5164. };
  5165. }
  5166. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5167. }
  5168. if (this.opts.type === 'map'||this.opts.type === 'word') {
  5169. var index = option.index==undefined? this.getCurrentDataIndex(e):option.index ;
  5170. if (index > -1) {
  5171. var currentOffset = this.scrollOption.currentOffset;
  5172. var opts = assign({}, this.opts, {
  5173. _scrollDistance_: currentOffset,
  5174. animation: false
  5175. });
  5176. var seriesData = this.opts._series_[index];
  5177. var textList = [{
  5178. text: option.format ? option.format(seriesData) : seriesData.properties.name ,
  5179. color: seriesData.color
  5180. }];
  5181. var offset = {
  5182. x: _touches$.x,
  5183. y: _touches$.y
  5184. };
  5185. opts.tooltip = {
  5186. textList: option.textList?option.textList:textList,
  5187. offset: offset,
  5188. option: option,
  5189. index: index
  5190. };
  5191. }
  5192. opts.updateData = false;
  5193. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5194. }
  5195. if (this.opts.type === 'radar') {
  5196. var index = option.index==undefined? this.getCurrentDataIndex(e):option.index ;
  5197. if (index > -1) {
  5198. var currentOffset = this.scrollOption.currentOffset;
  5199. var opts = assign({}, this.opts, {
  5200. _scrollDistance_: currentOffset,
  5201. animation: false
  5202. });
  5203. var seriesData = getSeriesDataItem(this.opts.series, index);
  5204. if (seriesData.length !== 0) {
  5205. var textList = seriesData.map(function(item) {
  5206. return {
  5207. text: option.format ? option.format(item) : item.name + ': ' + item.data,
  5208. color: item.color
  5209. };
  5210. });
  5211. var offset = {
  5212. x: _touches$.x,
  5213. y: _touches$.y
  5214. };
  5215. opts.tooltip = {
  5216. textList: option.textList?option.textList:textList,
  5217. offset: offset,
  5218. option: option,
  5219. index: index
  5220. };
  5221. }
  5222. }
  5223. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5224. }
  5225. };
  5226. Charts.prototype.translate = function(distance) {
  5227. this.scrollOption = {
  5228. currentOffset: distance,
  5229. startTouchX: distance,
  5230. distance: 0,
  5231. lastMoveTime: 0
  5232. };
  5233. let opts = assign({}, this.opts, {
  5234. _scrollDistance_: distance,
  5235. animation: false
  5236. });
  5237. drawCharts.call(this, this.opts.type, opts, this.config, this.context);
  5238. };
  5239. Charts.prototype.scrollStart = function(e) {
  5240. var touches = null;
  5241. if (e.changedTouches) {
  5242. touches = e.changedTouches[0];
  5243. } else {
  5244. touches = e.mp.changedTouches[0];
  5245. }
  5246. var _touches$ = getTouches(touches, this.opts, e);
  5247. if (touches && this.opts.enableScroll === true) {
  5248. this.scrollOption.startTouchX = _touches$.x;
  5249. }
  5250. };
  5251. Charts.prototype.scroll = function(e) {
  5252. if (this.scrollOption.lastMoveTime === 0) {
  5253. this.scrollOption.lastMoveTime = Date.now();
  5254. }
  5255. let Limit = this.opts.extra.touchMoveLimit || 20;
  5256. let currMoveTime = Date.now();
  5257. let duration = currMoveTime - this.scrollOption.lastMoveTime;
  5258. if (duration < Math.floor(1000 / Limit)) return;
  5259. this.scrollOption.lastMoveTime = currMoveTime;
  5260. var touches = null;
  5261. if (e.changedTouches) {
  5262. touches = e.changedTouches[0];
  5263. } else {
  5264. touches = e.mp.changedTouches[0];
  5265. }
  5266. if (touches && this.opts.enableScroll === true) {
  5267. var _touches$ = getTouches(touches, this.opts, e);
  5268. var _distance;
  5269. _distance = _touches$.x - this.scrollOption.startTouchX;
  5270. var currentOffset = this.scrollOption.currentOffset;
  5271. var validDistance = calValidDistance(this,currentOffset + _distance, this.opts.chartData, this.config, this.opts);
  5272. this.scrollOption.distance = _distance = validDistance - currentOffset;
  5273. var opts = assign({}, this.opts, {
  5274. _scrollDistance_: currentOffset + _distance,
  5275. animation: false
  5276. });
  5277. drawCharts.call(this, opts.type, opts, this.config, this.context);
  5278. return currentOffset + _distance;
  5279. }
  5280. };
  5281. Charts.prototype.scrollEnd = function(e) {
  5282. if (this.opts.enableScroll === true) {
  5283. var _scrollOption = this.scrollOption,
  5284. currentOffset = _scrollOption.currentOffset,
  5285. distance = _scrollOption.distance;
  5286. this.scrollOption.currentOffset = currentOffset + distance;
  5287. this.scrollOption.distance = 0;
  5288. }
  5289. };
  5290. if (typeof module === "object" && typeof module.exports === "object") {
  5291. module.exports = Charts;
  5292. //export default Charts;//建议使用nodejs的module导出方式,如报错请使用export方式导出
  5293. }