u-charts.js 169 KB

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