u-charts.js 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056
  1. /*
  2. * uCharts v1.7.0.20190630
  3. * uni-app平台高性能跨全端图表,支持H5、APP、小程序(微信/支付宝/百度/头条)
  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. */
  18. 'use strict';
  19. var config = {
  20. yAxisWidth: 15,
  21. yAxisSplit: 5,
  22. xAxisHeight: 15,
  23. xAxisLineHeight: 15,
  24. legendHeight: 15,
  25. yAxisTitleWidth: 15,
  26. padding: 12,
  27. pixelRatio: 1, //适配H5高分屏
  28. rotate: false, //横屏模式
  29. columePadding: 3,
  30. fontSize: 13,
  31. //dataPointShape: ['diamond', 'circle', 'triangle', 'rect'],
  32. dataPointShape: ['circle', 'circle', 'circle', 'circle'], //仿F2图例样式改为圆点
  33. // colors: ['#1890ff', '#2fc25b', '#facc14', '#f04864', '#8543e0', '#90ed7d'],
  34. colors:['#1890ff', '#2fc25b', '#facc14', '#f04864', '#8543e0', '#90ed7d', '#5caf20', '#30b0d7','#ff6d58','#1A79B9','#f97723','#f3792c'],
  35. pieChartLinePadding: 15,
  36. pieChartTextPadding: 5,
  37. xAxisTextPadding: 3,
  38. titleColor: '#333333',
  39. titleFontSize: 20,
  40. subtitleColor: '#999999',
  41. subtitleFontSize: 15,
  42. toolTipPadding: 3,
  43. toolTipBackground: '#000000',
  44. toolTipOpacity: 0.7,
  45. toolTipLineHeight: 20,
  46. radarGridCount: 3,
  47. radarLabelTextMargin: 15,
  48. gaugeLabelTextMargin: 15
  49. };
  50. // Object.assign polyfill
  51. // https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
  52. function assign(target, varArgs) {
  53. if (target == null) {
  54. // TypeError if undefined or 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. // Skip over if undefined or null
  62. for (var nextKey in nextSource) {
  63. // Avoid bugs when hasOwnProperty is shadowed
  64. if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
  65. to[nextKey] = nextSource[nextKey];
  66. }
  67. }
  68. }
  69. }
  70. return to;
  71. }
  72. var util = {
  73. toFixed: function toFixed(num, limit) {
  74. limit = limit || 2;
  75. if (this.isFloat(num)) {
  76. num = num.toFixed(limit);
  77. }
  78. return num;
  79. },
  80. isFloat: function isFloat(num) {
  81. return num % 1 !== 0;
  82. },
  83. approximatelyEqual: function approximatelyEqual(num1, num2) {
  84. return Math.abs(num1 - num2) < 1e-10;
  85. },
  86. isSameSign: function isSameSign(num1, num2) {
  87. return Math.abs(num1) === num1 && Math.abs(num2) === num2 || Math.abs(num1) !== num1 && Math.abs(num2) !== num2;
  88. },
  89. isSameXCoordinateArea: function isSameXCoordinateArea(p1, p2) {
  90. return this.isSameSign(p1.x, p2.x);
  91. },
  92. isCollision: function isCollision(obj1, obj2) {
  93. obj1.end = {};
  94. obj1.end.x = obj1.start.x + obj1.width;
  95. obj1.end.y = obj1.start.y - obj1.height;
  96. obj2.end = {};
  97. obj2.end.x = obj2.start.x + obj2.width;
  98. obj2.end.y = obj2.start.y - obj2.height;
  99. var flag = obj2.start.x > obj1.end.x || obj2.end.x < obj1.start.x || obj2.end.y > obj1.start.y || obj2.start.y <
  100. obj1.end.y;
  101. return !flag;
  102. }
  103. };
  104. // hex 转 rgba
  105. function hexToRgb(hexValue, opc) {
  106. var rgx = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  107. var hex = hexValue.replace(rgx, function(m, r, g, b) {
  108. return r + r + g + g + b + b;
  109. });
  110. var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  111. var r = parseInt(rgb[1], 16);
  112. var g = parseInt(rgb[2], 16);
  113. var b = parseInt(rgb[3], 16);
  114. return 'rgba(' + r + ',' + g + ',' + b + ',' + opc + ')';
  115. }
  116. function findRange(num, type, limit) {
  117. if (isNaN(num)) {
  118. throw new Error('[wxCharts] unvalid series data!');
  119. }
  120. limit = limit || 10;
  121. type = type ? type : 'upper';
  122. var multiple = 1;
  123. while (limit < 1) {
  124. limit *= 10;
  125. multiple *= 10;
  126. }
  127. if (type === 'upper') {
  128. num = Math.ceil(num * multiple);
  129. } else {
  130. num = Math.floor(num * multiple);
  131. }
  132. while (num % limit !== 0) {
  133. if (type === 'upper') {
  134. num++;
  135. } else {
  136. num--;
  137. }
  138. }
  139. return num / multiple;
  140. }
  141. function calCandleMA(dayArr, nameArr, colorArr, kdata) {
  142. let seriesTemp = [];
  143. for (let k = 0; k < dayArr.length; k++) {
  144. let seriesItem = {
  145. data: [],
  146. name: nameArr[k],
  147. color: colorArr[k]
  148. };
  149. for (let i = 0, len = kdata.length; i < len; i++) {
  150. if (i < dayArr[k]) {
  151. seriesItem.data.push(null);
  152. continue;
  153. }
  154. let sum = 0;
  155. for (let j = 0; j < dayArr[k]; j++) {
  156. sum += kdata[i - j][1];
  157. }
  158. seriesItem.data.push(+(sum / dayArr[k]).toFixed(3));
  159. }
  160. seriesTemp.push(seriesItem);
  161. }
  162. return seriesTemp;
  163. }
  164. function calValidDistance(distance, chartData, config, opts) {
  165. var dataChartAreaWidth = opts.width - config.padding - chartData.xAxisPoints[0];
  166. var dataChartWidth = chartData.eachSpacing * opts.categories.length;
  167. var validDistance = distance;
  168. if (distance >= 0) {
  169. validDistance = 0;
  170. } else if (Math.abs(distance) >= dataChartWidth - dataChartAreaWidth) {
  171. validDistance = dataChartAreaWidth - dataChartWidth;
  172. }
  173. return validDistance;
  174. }
  175. function isInAngleRange(angle, startAngle, endAngle) {
  176. function adjust(angle) {
  177. while (angle < 0) {
  178. angle += 2 * Math.PI;
  179. }
  180. while (angle > 2 * Math.PI) {
  181. angle -= 2 * Math.PI;
  182. }
  183. return angle;
  184. }
  185. angle = adjust(angle);
  186. startAngle = adjust(startAngle);
  187. endAngle = adjust(endAngle);
  188. if (startAngle > endAngle) {
  189. endAngle += 2 * Math.PI;
  190. if (angle < startAngle) {
  191. angle += 2 * Math.PI;
  192. }
  193. }
  194. return angle >= startAngle && angle <= endAngle;
  195. }
  196. function calRotateTranslate(x, y, h) {
  197. var xv = x;
  198. var yv = h - y;
  199. var transX = xv + (h - yv - xv) / Math.sqrt(2);
  200. transX *= -1;
  201. var transY = (h - yv) * (Math.sqrt(2) - 1) - (h - yv - xv) / Math.sqrt(2);
  202. return {
  203. transX: transX,
  204. transY: transY
  205. };
  206. }
  207. function createCurveControlPoints(points, i) {
  208. function isNotMiddlePoint(points, i) {
  209. if (points[i - 1] && points[i + 1]) {
  210. return points[i].y >= Math.max(points[i - 1].y, points[i + 1].y) || points[i].y <= Math.min(points[i - 1].y, points[
  211. i + 1].y);
  212. } else {
  213. return false;
  214. }
  215. }
  216. var a = 0.2;
  217. var b = 0.2;
  218. var pAx = null;
  219. var pAy = null;
  220. var pBx = null;
  221. var pBy = null;
  222. if (i < 1) {
  223. pAx = points[0].x + (points[1].x - points[0].x) * a;
  224. pAy = points[0].y + (points[1].y - points[0].y) * a;
  225. } else {
  226. pAx = points[i].x + (points[i + 1].x - points[i - 1].x) * a;
  227. pAy = points[i].y + (points[i + 1].y - points[i - 1].y) * a;
  228. }
  229. if (i > points.length - 3) {
  230. var last = points.length - 1;
  231. pBx = points[last].x - (points[last].x - points[last - 1].x) * b;
  232. pBy = points[last].y - (points[last].y - points[last - 1].y) * b;
  233. } else {
  234. pBx = points[i + 1].x - (points[i + 2].x - points[i].x) * b;
  235. pBy = points[i + 1].y - (points[i + 2].y - points[i].y) * b;
  236. }
  237. // fix issue https://github.com/xiaolin3303/wx-charts/issues/79
  238. if (isNotMiddlePoint(points, i + 1)) {
  239. pBy = points[i + 1].y;
  240. }
  241. if (isNotMiddlePoint(points, i)) {
  242. pAy = points[i].y;
  243. }
  244. return {
  245. ctrA: {
  246. x: pAx,
  247. y: pAy
  248. },
  249. ctrB: {
  250. x: pBx,
  251. y: pBy
  252. }
  253. };
  254. }
  255. function convertCoordinateOrigin(x, y, center) {
  256. return {
  257. x: center.x + x,
  258. y: center.y - y
  259. };
  260. }
  261. function avoidCollision(obj, target) {
  262. if (target) {
  263. // is collision test
  264. while (util.isCollision(obj, target)) {
  265. if (obj.start.x > 0) {
  266. obj.start.y--;
  267. } else if (obj.start.x < 0) {
  268. obj.start.y++;
  269. } else {
  270. if (obj.start.y > 0) {
  271. obj.start.y++;
  272. } else {
  273. obj.start.y--;
  274. }
  275. }
  276. }
  277. }
  278. return obj;
  279. }
  280. function fillSeriesColor(series, config) {
  281. var index = 0;
  282. return series.map(function(item) {
  283. if (!item.color) {
  284. item.color = config.colors[index];
  285. index = (index + 1) % config.colors.length;
  286. }
  287. return item;
  288. });
  289. }
  290. function fillSeriesType(series, opts) {
  291. return series.map(function(item) {
  292. if (!item.type) {
  293. item.type = opts.type;
  294. }
  295. return item;
  296. });
  297. }
  298. function getDataRange(minData, maxData) {
  299. var limit = 0;
  300. var range = maxData - minData;
  301. if (range >= 10000) {
  302. limit = 1000;
  303. } else if (range >= 1000) {
  304. limit = 100;
  305. } else if (range >= 100) {
  306. limit = 10;
  307. } else if (range >= 10) {
  308. limit = 5;
  309. } else if (range >= 1) {
  310. limit = 1;
  311. } else if (range >= 0.1) {
  312. limit = 0.1;
  313. } else {
  314. limit = 0.01;
  315. }
  316. return {
  317. minRange: findRange(minData, 'lower', limit),
  318. maxRange: findRange(maxData, 'upper', limit)
  319. };
  320. }
  321. function measureText(text) {
  322. var fontSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : config.fontSize;
  323. // wx canvas 未实现measureText方法, 此处自行实现
  324. // 适配修改初始字体10px为其他大小的方法
  325. text = String(text);
  326. var text = text.split('');
  327. var width = 0;
  328. for (let i = 0; i < text.length; i++) {
  329. let item = text[i];
  330. if (/[a-zA-Z]/.test(item)) {
  331. width += 7;
  332. } else if (/[0-9]/.test(item)) {
  333. width += 5.5;
  334. } else if (/\./.test(item)) {
  335. width += 2.7;
  336. } else if (/-/.test(item)) {
  337. width += 3.25;
  338. } else if (/[\u4e00-\u9fa5]/.test(item)) {
  339. width += 10;
  340. } else if (/\(|\)/.test(item)) {
  341. width += 3.73;
  342. } else if (/\s/.test(item)) {
  343. width += 2.5;
  344. } else if (/%/.test(item)) {
  345. width += 8;
  346. } else {
  347. width += 10;
  348. }
  349. }
  350. return width * fontSize / 10;
  351. }
  352. function dataCombine(series) {
  353. return series.reduce(function(a, b) {
  354. return (a.data ? a.data : a).concat(b.data);
  355. }, []);
  356. }
  357. function dataCombineStack(series) {
  358. var sum = new Array(series[0].data.length);
  359. for (var j = 0; j < sum.length; j++) {
  360. sum[j] = 0;
  361. }
  362. for (var i = 0; i < series.length; i++) {
  363. for (var j = 0; j < sum.length; j++) {
  364. sum[j] += series[i].data[j];
  365. }
  366. }
  367. return series.reduce(function(a, b) {
  368. return (a.data ? a.data : a).concat(b.data).concat(sum);
  369. }, []);
  370. }
  371. function getTouches(touches, opts, e) {
  372. let x, y;
  373. if (touches.clientX) {
  374. if (opts.rotate) { //适配横屏
  375. y = opts.height - touches.clientX * opts.pixelRatio;
  376. x = (touches.pageY - e.currentTarget.offsetTop - (opts.height / opts.pixelRatio / 2) * (opts.pixelRatio - 1)) *
  377. opts.pixelRatio;
  378. } else {
  379. x = touches.clientX * opts.pixelRatio;
  380. y = (touches.pageY - e.currentTarget.offsetTop - (opts.height / opts.pixelRatio / 2) * (opts.pixelRatio - 1)) *
  381. opts.pixelRatio;
  382. }
  383. } else {
  384. if (opts.rotate) { //适配横屏
  385. y = opts.height - touches.x * opts.pixelRatio;
  386. x = touches.y * opts.pixelRatio;
  387. } else {
  388. x = touches.x * opts.pixelRatio;
  389. y = touches.y * opts.pixelRatio;
  390. }
  391. }
  392. return {
  393. x: x,
  394. y: y
  395. }
  396. }
  397. function getSeriesDataItem(series, index) {
  398. var data = [];
  399. for (let i = 0; i < series.length; i++) {
  400. let item = series[i];
  401. if (item.data[index] !== null && typeof item.data[index] !== 'undefined') {
  402. let seriesItem = {};
  403. seriesItem.color = item.color;
  404. seriesItem.type = item.type;
  405. seriesItem.style = item.style;
  406. seriesItem.shape = item.shape;
  407. seriesItem.disableLegend = item.disableLegend;
  408. seriesItem.name = item.name;
  409. seriesItem.data = item.format ? item.format(item.data[index]) : item.data[index];
  410. data.push(seriesItem);
  411. }
  412. }
  413. return data;
  414. }
  415. function getMaxTextListLength(list) {
  416. var lengthList = list.map(function(item) {
  417. return measureText(item);
  418. });
  419. return Math.max.apply(null, lengthList);
  420. }
  421. function getRadarCoordinateSeries(length) {
  422. var eachAngle = 2 * Math.PI / length;
  423. var CoordinateSeries = [];
  424. for (var i = 0; i < length; i++) {
  425. CoordinateSeries.push(eachAngle * i);
  426. }
  427. return CoordinateSeries.map(function(item) {
  428. return -1 * item + Math.PI / 2;
  429. });
  430. }
  431. function getToolTipData(seriesData, calPoints, index, categories) {
  432. var option = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
  433. var textList = seriesData.map(function(item) {
  434. return {
  435. text: option.format ? option.format(item, categories[index]) : item.name + ': ' + item.data,
  436. color: item.color
  437. };
  438. });
  439. var validCalPoints = [];
  440. var offset = {
  441. x: 0,
  442. y: 0
  443. };
  444. for (let i = 0; i < calPoints.length; i++) {
  445. let points = calPoints[i];
  446. if (typeof points[index] !== 'undefined' && points[index] !== null) {
  447. validCalPoints.push(points[index]);
  448. }
  449. }
  450. for (let i = 0; i < validCalPoints.length; i++) {
  451. let item = validCalPoints[i];
  452. offset.x = Math.round(item.x);
  453. offset.y += item.y;
  454. }
  455. offset.y /= validCalPoints.length;
  456. return {
  457. textList: textList,
  458. offset: offset
  459. };
  460. }
  461. function getMixToolTipData(seriesData, calPoints, index, categories) {
  462. var option = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
  463. var textList = seriesData.map(function(item) {
  464. return {
  465. text: option.format ? option.format(item, categories[index]) : item.name + ': ' + item.data,
  466. color: item.color,
  467. disableLegend: item.disableLegend ? true : false
  468. };
  469. });
  470. textList = textList.filter(function(item) {
  471. if (item.disableLegend !== true) {
  472. return item;
  473. }
  474. });
  475. var validCalPoints = [];
  476. var offset = {
  477. x: 0,
  478. y: 0
  479. };
  480. for (let i = 0; i < calPoints.length; i++) {
  481. let points = calPoints[i];
  482. if (typeof points[index] !== 'undefined' && points[index] !== null) {
  483. validCalPoints.push(points[index]);
  484. }
  485. }
  486. for (let i = 0; i < validCalPoints.length; i++) {
  487. let item = validCalPoints[i];
  488. offset.x = Math.round(item.x);
  489. offset.y += item.y;
  490. }
  491. offset.y /= validCalPoints.length;
  492. return {
  493. textList: textList,
  494. offset: offset
  495. };
  496. }
  497. function getCandleToolTipData(series, seriesData, calPoints, index, categories, extra) {
  498. var option = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : {};
  499. let upColor = extra.color.upFill;
  500. let downColor = extra.color.downFill;
  501. //颜色顺序为开盘,收盘,最低,最高
  502. let color = [upColor, upColor, downColor, upColor];
  503. var textList = [];
  504. let text0 = {
  505. text: categories[index],
  506. color: null
  507. };
  508. textList.push(text0);
  509. seriesData.map(function(item) {
  510. //console.log(color)
  511. if (index == 0 && item.data[1] - item.data[0] < 0) {
  512. color[1] = downColor;
  513. } else {
  514. if (item.data[0] < series[index - 1][1]) {
  515. color[0] = downColor;
  516. }
  517. if (item.data[1] < item.data[0]) {
  518. color[1] = downColor;
  519. }
  520. if (item.data[2] > series[index - 1][1]) {
  521. color[2] = upColor;
  522. }
  523. if (item.data[3] < series[index - 1][1]) {
  524. color[3] = downColor;
  525. }
  526. }
  527. let text1 = {
  528. text: '开盘:' + item.data[0],
  529. color: color[0]
  530. };
  531. let text2 = {
  532. text: '收盘:' + item.data[1],
  533. color: color[1]
  534. };
  535. let text3 = {
  536. text: '最低:' + item.data[2],
  537. color: color[2]
  538. };
  539. let text4 = {
  540. text: '最高:' + item.data[3],
  541. color: color[3]
  542. };
  543. textList.push(text1, text2, text3, text4);
  544. });
  545. var validCalPoints = [];
  546. var offset = {
  547. x: 0,
  548. y: 0
  549. };
  550. for (let i = 0; i < calPoints.length; i++) {
  551. let points = calPoints[i];
  552. if (typeof points[index] !== 'undefined' && points[index] !== null) {
  553. validCalPoints.push(points[index]);
  554. }
  555. }
  556. offset.x = Math.round(validCalPoints[0][0].x);
  557. return {
  558. textList: textList,
  559. offset: offset
  560. };
  561. }
  562. function findCurrentIndex(currentPoints, xAxisPoints, opts, config) {
  563. var offset = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
  564. var currentIndex = -1;
  565. if (isInExactChartArea(currentPoints, opts, config)) {
  566. xAxisPoints.forEach(function(item, index) {
  567. if (currentPoints.x + offset > item) {
  568. currentIndex = index;
  569. }
  570. });
  571. }
  572. return currentIndex;
  573. }
  574. function isInExactChartArea(currentPoints, opts, config) {
  575. return currentPoints.x < opts.width - config.padding && currentPoints.x > config.padding + config.yAxisWidth + config.yAxisTitleWidth &&
  576. currentPoints.y > config.padding && currentPoints.y < opts.height - config.legendHeight - config.xAxisHeight - config
  577. .padding;
  578. }
  579. function findRadarChartCurrentIndex(currentPoints, radarData, count) {
  580. var eachAngleArea = 2 * Math.PI / count;
  581. var currentIndex = -1;
  582. if (isInExactPieChartArea(currentPoints, radarData.center, radarData.radius)) {
  583. var fixAngle = function fixAngle(angle) {
  584. if (angle < 0) {
  585. angle += 2 * Math.PI;
  586. }
  587. if (angle > 2 * Math.PI) {
  588. angle -= 2 * Math.PI;
  589. }
  590. return angle;
  591. };
  592. var angle = Math.atan2(radarData.center.y - currentPoints.y, currentPoints.x - radarData.center.x);
  593. angle = -1 * angle;
  594. if (angle < 0) {
  595. angle += 2 * Math.PI;
  596. }
  597. var angleList = radarData.angleList.map(function(item) {
  598. item = fixAngle(-1 * item);
  599. return item;
  600. });
  601. angleList.forEach(function(item, index) {
  602. var rangeStart = fixAngle(item - eachAngleArea / 2);
  603. var rangeEnd = fixAngle(item + eachAngleArea / 2);
  604. if (rangeEnd < rangeStart) {
  605. rangeEnd += 2 * Math.PI;
  606. }
  607. if (angle >= rangeStart && angle <= rangeEnd || angle + 2 * Math.PI >= rangeStart && angle + 2 * Math.PI <=
  608. rangeEnd) {
  609. currentIndex = index;
  610. }
  611. });
  612. }
  613. return currentIndex;
  614. }
  615. function findPieChartCurrentIndex(currentPoints, pieData) {
  616. var currentIndex = -1;
  617. if (isInExactPieChartArea(currentPoints, pieData.center, pieData.radius)) {
  618. var angle = Math.atan2(pieData.center.y - currentPoints.y, currentPoints.x - pieData.center.x);
  619. angle = -angle;
  620. for (var i = 0, len = pieData.series.length; i < len; i++) {
  621. var item = pieData.series[i];
  622. if (isInAngleRange(angle, item._start_, item._start_ + item._proportion_ * 2 * Math.PI)) {
  623. currentIndex = i;
  624. break;
  625. }
  626. }
  627. }
  628. return currentIndex;
  629. }
  630. function isInExactPieChartArea(currentPoints, center, radius) {
  631. return Math.pow(currentPoints.x - center.x, 2) + Math.pow(currentPoints.y - center.y, 2) <= Math.pow(radius, 2);
  632. }
  633. function splitPoints(points) {
  634. var newPoints = [];
  635. var items = [];
  636. points.forEach(function(item, index) {
  637. if (item !== null) {
  638. items.push(item);
  639. } else {
  640. if (items.length) {
  641. newPoints.push(items);
  642. }
  643. items = [];
  644. }
  645. });
  646. if (items.length) {
  647. newPoints.push(items);
  648. }
  649. return newPoints;
  650. }
  651. function calLegendData(series, opts, config) {
  652. if (opts.legend === false) {
  653. return {
  654. legendList: [],
  655. legendHeight: 0
  656. };
  657. }
  658. //适配H5高分屏
  659. var padding = 5 * opts.pixelRatio;
  660. var marginTop = 8 * opts.pixelRatio;
  661. var shapeWidth = 15 * opts.pixelRatio;
  662. var legendList = [];
  663. var widthCount = 0;
  664. var currentRow = [];
  665. for (let i = 0; i < series.length; i++) {
  666. let item = series[i];
  667. let itemWidth = 3 * padding + shapeWidth + measureText(item.name || 'undefined');
  668. if (widthCount + itemWidth > opts.width) {
  669. legendList.push(currentRow);
  670. widthCount = itemWidth;
  671. currentRow = [item];
  672. } else {
  673. widthCount += itemWidth;
  674. currentRow.push(item);
  675. }
  676. }
  677. if (currentRow.length) {
  678. legendList.push(currentRow);
  679. }
  680. return {
  681. legendList: legendList,
  682. legendHeight: legendList.length * (config.fontSize + marginTop) + padding
  683. };
  684. }
  685. function calCategoriesData(categories, opts, config) {
  686. var result = {
  687. angle: 0,
  688. xAxisHeight: config.xAxisHeight
  689. };
  690. var _getXAxisPoints = getXAxisPoints(categories, opts, config),
  691. eachSpacing = _getXAxisPoints.eachSpacing;
  692. // get max length of categories text
  693. var categoriesTextLenth = categories.map(function(item) {
  694. return measureText(item);
  695. });
  696. var maxTextLength = Math.max.apply(this, categoriesTextLenth);
  697. if (opts.xAxis.rotateLabel == true && maxTextLength + 2 * config.xAxisTextPadding > eachSpacing) {
  698. result.angle = 45 * Math.PI / 180;
  699. result.xAxisHeight = 2 * config.xAxisTextPadding + maxTextLength * Math.sin(result.angle);
  700. }
  701. return result;
  702. }
  703. function getRadarDataPoints(angleList, center, radius, series, opts) {
  704. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  705. var radarOption = opts.extra.radar || {};
  706. radarOption.max = radarOption.max || 0;
  707. var maxData = Math.max(radarOption.max, Math.max.apply(null, dataCombine(series)));
  708. var data = [];
  709. for (let i = 0; i < series.length; i++) {
  710. let each = series[i];
  711. let listItem = {};
  712. listItem.color = each.color;
  713. listItem.data = [];
  714. each.data.forEach(function(item, index) {
  715. let tmp = {};
  716. tmp.angle = angleList[index];
  717. tmp.proportion = item / maxData;
  718. tmp.position = convertCoordinateOrigin(radius * tmp.proportion * process * Math.cos(tmp.angle), radius * tmp.proportion *
  719. process * Math.sin(tmp.angle), center);
  720. listItem.data.push(tmp);
  721. });
  722. data.push(listItem);
  723. }
  724. return data;
  725. }
  726. function getPieDataPoints(series, radius) {
  727. var process = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  728. var count = 0;
  729. var _start_ = 0;
  730. for (let i = 0; i < series.length; i++) {
  731. let item = series[i];
  732. item.data = item.data === null ? 0 : item.data;
  733. count += item.data;
  734. }
  735. for (let i = 0; i < series.length; i++) {
  736. let item = series[i];
  737. item.data = item.data === null ? 0 : item.data;
  738. if (count === 0) {
  739. item._proportion_ = 1 / series.length * process;
  740. } else {
  741. item._proportion_ = item.data / count * process;
  742. }
  743. item._radius_=radius;
  744. }
  745. for (let i = 0; i < series.length; i++) {
  746. let item = series[i];
  747. item._start_ = _start_;
  748. _start_ += 2 * item._proportion_ * Math.PI;
  749. }
  750. return series;
  751. }
  752. function getRoseDataPoints(series, type, minRadius, radius) {
  753. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  754. var count = 0;
  755. var _start_ = 0;
  756. var dataArr=[];
  757. for (let i = 0; i < series.length; i++) {
  758. let item = series[i];
  759. item.data = item.data === null ? 0 : item.data;
  760. count += item.data;
  761. dataArr.push(item.data);
  762. }
  763. var minData = dataArr.pop();
  764. var maxData = dataArr.shift();
  765. var radiusLength = radius - minRadius;
  766. for (let i = 0; i < series.length; i++) {
  767. let item = series[i];
  768. item.data = item.data === null ? 0 : item.data;
  769. if (count === 0 || type == 'area') {
  770. item._proportion_ = 1 / series.length * process;
  771. } else {
  772. item._proportion_ = item.data / count * process;
  773. }
  774. item._radius_ = minRadius + radiusLength * ( (item.data - minData)/(maxData-minData) );
  775. }
  776. for (let i = 0; i < series.length; i++) {
  777. let item = series[i];
  778. item._start_ = _start_;
  779. _start_ += 2 * item._proportion_ * Math.PI;
  780. }
  781. return series;
  782. }
  783. function getArcbarDataPoints(series, arcbarOption) {
  784. var process = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  785. if (process == 1) {
  786. process = 0.999999;
  787. }
  788. for (let i = 0; i < series.length; i++) {
  789. let item = series[i];
  790. item.data = item.data === null ? 0 : item.data;
  791. let totalAngle;
  792. if (arcbarOption.type == 'default') {
  793. totalAngle = arcbarOption.startAngle - arcbarOption.endAngle + 1;
  794. } else {
  795. totalAngle = 2;
  796. }
  797. item._proportion_ = totalAngle * item.data * process + arcbarOption.startAngle;
  798. if (item._proportion_ >= 2) {
  799. item._proportion_ = item._proportion_ % 2;
  800. }
  801. }
  802. return series;
  803. }
  804. function getGaugeAxisPoints(categories, startAngle, endAngle) {
  805. let totalAngle = startAngle - endAngle + 1;
  806. let tempStartAngle = startAngle;
  807. for (let i = 0; i < categories.length; i++) {
  808. categories[i].value = categories[i].value === null ? 0 : categories[i].value;
  809. categories[i]._startAngle_ = tempStartAngle;
  810. categories[i]._endAngle_ = totalAngle * categories[i].value + startAngle;
  811. if (categories[i]._endAngle_ >= 2) {
  812. categories[i]._endAngle_ = categories[i]._endAngle_ % 2;
  813. }
  814. tempStartAngle = categories[i]._endAngle_;
  815. }
  816. return categories;
  817. }
  818. function getGaugeDataPoints(series, categories, gaugeOption) {
  819. let process = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
  820. for (let i = 0; i < series.length; i++) {
  821. let item = series[i];
  822. item.data = item.data === null ? 0 : item.data;
  823. if (gaugeOption.pointer.color == 'auto') {
  824. for (let i = 0; i < categories.length; i++) {
  825. if (item.data <= categories[i].value) {
  826. item.color = categories[i].color;
  827. break;
  828. }
  829. }
  830. } else {
  831. item.color = gaugeOption.pointer.color;
  832. }
  833. let totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  834. item._endAngle_ = totalAngle * item.data + gaugeOption.startAngle;
  835. item._oldAngle_ = gaugeOption.oldAngle;
  836. if (gaugeOption.oldAngle < gaugeOption.endAngle) {
  837. item._oldAngle_ += 2;
  838. }
  839. if (item.data >= gaugeOption.oldData) {
  840. item._proportion_ = (item._endAngle_ - item._oldAngle_) * process + gaugeOption.oldAngle;
  841. } else {
  842. item._proportion_ = item._oldAngle_ - (item._oldAngle_ - item._endAngle_) * process;
  843. }
  844. if (item._proportion_ >= 2) {
  845. item._proportion_ = item._proportion_ % 2;
  846. }
  847. }
  848. return series;
  849. }
  850. function getPieTextMaxLength(series) {
  851. series = getPieDataPoints(series);
  852. let maxLength = 0;
  853. for (let i = 0; i < series.length; i++) {
  854. let item = series[i];
  855. let text = item.format ? item.format(+item._proportion_.toFixed(2)) : util.toFixed(item._proportion_ * 100) + '%';
  856. maxLength = Math.max(maxLength, measureText(text));
  857. }
  858. return maxLength;
  859. }
  860. function fixColumeData(points, eachSpacing, columnLen, index, config, opts) {
  861. return points.map(function(item) {
  862. if (item === null) {
  863. return null;
  864. }
  865. item.width = (eachSpacing - 2 * config.columePadding) / columnLen;
  866. if (opts.extra.column && opts.extra.column.width && +opts.extra.column.width > 0) {
  867. // customer column width
  868. item.width = Math.min(item.width, +opts.extra.column.width);
  869. } else {
  870. // default width should less tran 25px
  871. // don't ask me why, I don't know
  872. item.width = Math.min(item.width, 25);
  873. }
  874. item.x += (index + 0.5 - columnLen / 2) * item.width;
  875. return item;
  876. });
  877. }
  878. function fixColumeMeterData(points, eachSpacing, columnLen, index, config, opts, border) {
  879. return points.map(function(item) {
  880. if (item === null) {
  881. return null;
  882. }
  883. item.width = eachSpacing - 2 * config.columePadding;
  884. if (opts.extra.column && opts.extra.column.width && +opts.extra.column.width > 0) {
  885. item.width = Math.min(item.width, +opts.extra.column.width);
  886. } else {
  887. item.width = Math.min(item.width, 25);
  888. }
  889. if (index > 0) {
  890. item.width -= 2 * border;
  891. }
  892. return item;
  893. });
  894. }
  895. function fixColumeStackData(points, eachSpacing, columnLen, index, config, opts, series) {
  896. return points.map(function(item, indexn) {
  897. if (item === null) {
  898. return null;
  899. }
  900. item.width = eachSpacing - 2 * config.columePadding;
  901. if (opts.extra.column && opts.extra.column.width && +opts.extra.column.width > 0) {
  902. item.width = Math.min(item.width, +opts.extra.column.width);
  903. } else {
  904. item.width = Math.min(item.width, 25);
  905. }
  906. return item;
  907. });
  908. }
  909. function getXAxisPoints(categories, opts, config) {
  910. var yAxisTotalWidth = config.yAxisWidth + config.yAxisTitleWidth;
  911. var spacingValid = opts.width - 2 * config.padding - yAxisTotalWidth;
  912. var dataCount = opts.enableScroll ? Math.min(opts.xAxis.itemCount, categories.length) : categories.length;
  913. var eachSpacing = spacingValid / dataCount;
  914. var xAxisPoints = [];
  915. var startX = config.padding + yAxisTotalWidth;
  916. var endX = opts.width - config.padding;
  917. categories.forEach(function(item, index) {
  918. xAxisPoints.push(startX + index * eachSpacing);
  919. });
  920. if (opts.enableScroll === true) {
  921. xAxisPoints.push(startX + categories.length * eachSpacing);
  922. } else {
  923. xAxisPoints.push(endX);
  924. }
  925. return {
  926. xAxisPoints: xAxisPoints,
  927. startX: startX,
  928. endX: endX,
  929. eachSpacing: eachSpacing
  930. };
  931. }
  932. function getCandleDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config) {
  933. var process = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 1;
  934. var points = [];
  935. var validHeight = opts.height - 2 * config.padding - config.xAxisHeight - config.legendHeight;
  936. data.forEach(function(item, index) {
  937. if (item === null) {
  938. points.push(null);
  939. } else {
  940. var cPoints = [];
  941. item.forEach(function(items, indexs) {
  942. var point = {};
  943. point.x = xAxisPoints[index] + Math.round(eachSpacing / 2);
  944. var value = items.value || items;
  945. var height = validHeight * (value - minRange) / (maxRange - minRange);
  946. height *= process;
  947. point.y = opts.height - config.xAxisHeight - config.legendHeight - Math.round(height) - config.padding;
  948. cPoints.push(point);
  949. });
  950. points.push(cPoints);
  951. }
  952. });
  953. return points;
  954. }
  955. function getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config) {
  956. var process = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 1;
  957. var points = [];
  958. var validHeight = opts.height - 2 * config.padding - config.xAxisHeight - config.legendHeight;
  959. data.forEach(function(item, index) {
  960. if (item === null) {
  961. points.push(null);
  962. } else {
  963. var point = {};
  964. point.color = item.color;
  965. point.x = xAxisPoints[index] + Math.round(eachSpacing / 2);
  966. var value = item;
  967. if (typeof item === 'object' && item !== null) {
  968. value = item.value
  969. }
  970. var height = validHeight * (value - minRange) / (maxRange - minRange);
  971. height *= process;
  972. point.y = opts.height - config.xAxisHeight - config.legendHeight - Math.round(height) - config.padding;
  973. points.push(point);
  974. }
  975. });
  976. return points;
  977. }
  978. function getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, seriesIndex, stackSeries) {
  979. var process = arguments.length > 9 && arguments[9] !== undefined ? arguments[9] : 1;
  980. var points = [];
  981. var validHeight = opts.height - 2 * config.padding - config.xAxisHeight - config.legendHeight;
  982. data.forEach(function(item, index) {
  983. if (item === null) {
  984. points.push(null);
  985. } else {
  986. var point = {};
  987. point.color = item.color;
  988. point.x = xAxisPoints[index] + Math.round(eachSpacing / 2);
  989. if (seriesIndex > 0) {
  990. var value = 0;
  991. for (let i = 0; i <= seriesIndex; i++) {
  992. value += stackSeries[i].data[index];
  993. }
  994. var value0 = value - item;
  995. var height = validHeight * (value - minRange) / (maxRange - minRange);
  996. var height0 = validHeight * (value0 - minRange) / (maxRange - minRange);
  997. } else {
  998. var value = item;
  999. var height = validHeight * (value - minRange) / (maxRange - minRange);
  1000. var height0 = 0;
  1001. }
  1002. var heightc = height0;
  1003. height *= process;
  1004. heightc *= process;
  1005. point.y = opts.height - config.xAxisHeight - config.legendHeight - Math.round(height) - config.padding;
  1006. point.y0 = opts.height - config.xAxisHeight - config.legendHeight - Math.round(heightc) - config.padding;
  1007. points.push(point);
  1008. }
  1009. });
  1010. return points;
  1011. }
  1012. function getYAxisTextList(series, opts, config, stack) {
  1013. var data;
  1014. if (stack == 'stack') {
  1015. //data = dataCombine(series);
  1016. data = dataCombineStack(series);
  1017. } else {
  1018. data = dataCombine(series);
  1019. }
  1020. var sorted = [];
  1021. // remove null from data
  1022. data = data.filter(function(item) {
  1023. //return item !== null;
  1024. if (typeof item === 'object' && item !== null) {
  1025. //判断是否为数组
  1026. if (item.constructor == Array) {
  1027. return item !== null;
  1028. } else {
  1029. return item.value !== null;
  1030. }
  1031. } else {
  1032. return item !== null;
  1033. }
  1034. });
  1035. //var minData = Math.min.apply(this, data);
  1036. //var maxData = Math.max.apply(this, data);
  1037. data.map((item) => {
  1038. if (typeof item === 'object') {
  1039. if (item.constructor == Array) {
  1040. item.map((subitem) => {
  1041. sorted.push(subitem);
  1042. })
  1043. } else {
  1044. sorted.push(item.value);
  1045. }
  1046. } else {
  1047. sorted.push(item);
  1048. }
  1049. //typeof item === 'object' ? sorted.push(item.value) : sorted.push(item)
  1050. })
  1051. var minData = 0;
  1052. var maxData = 0;
  1053. if (sorted.length > 0) {
  1054. minData = Math.min.apply(this, sorted);
  1055. maxData = Math.max.apply(this, sorted);
  1056. }
  1057. if (typeof opts.yAxis.min === 'number') {
  1058. minData = Math.min(opts.yAxis.min, minData);
  1059. }
  1060. if (typeof opts.yAxis.max === 'number') {
  1061. maxData = Math.max(opts.yAxis.max, maxData);
  1062. }
  1063. // fix issue https://github.com/xiaolin3303/wx-charts/issues/9
  1064. if (minData === maxData) {
  1065. var rangeSpan = maxData || 10;
  1066. //minData -= rangeSpan;
  1067. maxData += rangeSpan;
  1068. }
  1069. var dataRange = getDataRange(minData, maxData);
  1070. var minRange = dataRange.minRange;
  1071. var maxRange = dataRange.maxRange;
  1072. var range = [];
  1073. var eachRange = (maxRange - minRange) / config.yAxisSplit;
  1074. for (var i = 0; i <= config.yAxisSplit; i++) {
  1075. range.push(minRange + eachRange * i);
  1076. }
  1077. return range.reverse();
  1078. }
  1079. function calYAxisData(series, opts, config) {
  1080. //堆叠图重算Y轴
  1081. var columnstyle = assign({}, opts.extra.column || {
  1082. "type": ""
  1083. });
  1084. var ranges = getYAxisTextList(series, opts, config, columnstyle.type);
  1085. var yAxisWidth = config.yAxisWidth;
  1086. var rangesFormat = ranges.map(function(item) {
  1087. item = util.toFixed(item, 2);
  1088. item = opts.yAxis.format ? opts.yAxis.format(Number(item)) : item;
  1089. yAxisWidth = Math.max(yAxisWidth, measureText(item) + 5);
  1090. return item;
  1091. });
  1092. if (opts.yAxis.disabled === true) {
  1093. yAxisWidth = 0;
  1094. }
  1095. return {
  1096. rangesFormat: rangesFormat,
  1097. ranges: ranges,
  1098. yAxisWidth: yAxisWidth
  1099. };
  1100. }
  1101. function calTooltipYAxisData(point, series, opts, config, eachSpacing) {
  1102. var ranges = getYAxisTextList(series, opts, config);
  1103. var spacingValid = opts.height - 2 * config.padding - config.xAxisHeight - config.legendHeight;
  1104. let maxVal = ranges[0];
  1105. let minVal = ranges[ranges.length - 1];
  1106. let minAxis = config.padding;
  1107. let maxAxis = config.padding + spacingValid;
  1108. let item = maxVal - (maxVal - minVal) * (point - minAxis) / (maxAxis - minAxis);
  1109. item = opts.yAxis.format ? opts.yAxis.format(Number(item)) : item;
  1110. return item;
  1111. }
  1112. function contextRotate(context, opts) {
  1113. if (opts.rotateLock !== true) {
  1114. context.translate(opts.height, 0);
  1115. context.rotate(90 * Math.PI / 180);
  1116. } else if (opts._rotate_ !== true) {
  1117. context.translate(opts.height, 0);
  1118. context.rotate(90 * Math.PI / 180);
  1119. opts._rotate_ = true;
  1120. }
  1121. }
  1122. function drawPointShape(points, color, shape, context, opts) {
  1123. context.beginPath();
  1124. context.setStrokeStyle("#ffffff");
  1125. context.setLineWidth(1 * opts.pixelRatio);
  1126. context.setFillStyle(color);
  1127. if (shape === 'diamond') {
  1128. points.forEach(function(item, index) {
  1129. if (item !== null) {
  1130. context.moveTo(item.x, item.y - 4.5);
  1131. context.lineTo(item.x - 4.5, item.y);
  1132. context.lineTo(item.x, item.y + 4.5);
  1133. context.lineTo(item.x + 4.5, item.y);
  1134. context.lineTo(item.x, item.y - 4.5);
  1135. }
  1136. });
  1137. } else if (shape === 'circle') {
  1138. points.forEach(function(item, index) {
  1139. if (item !== null) {
  1140. context.moveTo(item.x + 3.5 * opts.pixelRatio, item.y);
  1141. context.arc(item.x, item.y, 4 * opts.pixelRatio, 0, 2 * Math.PI, false);
  1142. }
  1143. });
  1144. } else if (shape === 'rect') {
  1145. points.forEach(function(item, index) {
  1146. if (item !== null) {
  1147. context.moveTo(item.x - 3.5, item.y - 3.5);
  1148. context.rect(item.x - 3.5, item.y - 3.5, 7, 7);
  1149. }
  1150. });
  1151. } else if (shape === 'triangle') {
  1152. points.forEach(function(item, index) {
  1153. if (item !== null) {
  1154. context.moveTo(item.x, item.y - 4.5);
  1155. context.lineTo(item.x - 4.5, item.y + 4.5);
  1156. context.lineTo(item.x + 4.5, item.y + 4.5);
  1157. context.lineTo(item.x, item.y - 4.5);
  1158. }
  1159. });
  1160. }
  1161. context.closePath();
  1162. context.fill();
  1163. context.stroke();
  1164. }
  1165. function drawRingTitle(opts, config, context) {
  1166. var titlefontSize = opts.title.fontSize || config.titleFontSize;
  1167. var subtitlefontSize = opts.subtitle.fontSize || config.subtitleFontSize;
  1168. var title = opts.title.name || '';
  1169. var subtitle = opts.subtitle.name || '';
  1170. var titleFontColor = opts.title.color || config.titleColor;
  1171. var subtitleFontColor = opts.subtitle.color || config.subtitleColor;
  1172. var titleHeight = title ? titlefontSize : 0;
  1173. var subtitleHeight = subtitle ? subtitlefontSize : 0;
  1174. var margin = 5;
  1175. if (subtitle) {
  1176. var textWidth = measureText(subtitle, subtitlefontSize);
  1177. var startX = (opts.width - textWidth) / 2 + (opts.subtitle.offsetX || 0);
  1178. var startY = ((opts.height - config.legendHeight + subtitlefontSize) / 2) + (opts.subtitle.offsetY || 0);
  1179. if (title) {
  1180. startY -= (titleHeight + margin) / 2;
  1181. }
  1182. context.beginPath();
  1183. context.setFontSize(subtitlefontSize);
  1184. context.setFillStyle(subtitleFontColor);
  1185. context.fillText(subtitle, startX, startY);
  1186. context.closePath();
  1187. context.stroke();
  1188. }
  1189. if (title) {
  1190. var _textWidth = measureText(title, titlefontSize);
  1191. var _startX = (opts.width - _textWidth) / 2 + (opts.title.offsetX || 0);
  1192. var _startY = ((opts.height - config.legendHeight + titlefontSize) / 2) + (opts.title.offsetY || 0);
  1193. if (subtitle) {
  1194. _startY += (subtitleHeight + margin) / 2;
  1195. }
  1196. context.beginPath();
  1197. context.setFontSize(titlefontSize);
  1198. context.setFillStyle(titleFontColor);
  1199. context.fillText(title, _startX, _startY);
  1200. context.closePath();
  1201. context.stroke();
  1202. }
  1203. }
  1204. function drawPointText(points, series, config, context) {
  1205. // 绘制数据文案
  1206. var data = series.data;
  1207. var textColor = series.textColor == undefined ? '#666666' : series.textColor;
  1208. points.forEach(function(item, index) {
  1209. if (item !== null) {
  1210. //var formatVal = series.format ? series.format(data[index]) : data[index];
  1211. context.beginPath();
  1212. context.setFontSize(config.fontSize);
  1213. context.setFillStyle(textColor);
  1214. var value = data[index]
  1215. if (typeof data[index] === 'object' && data[index] !== null) {
  1216. value = data[index].value
  1217. }
  1218. var formatVal = series.format ? series.format(value) : value;
  1219. context.fillText(formatVal, item.x - measureText(formatVal) / 2, item.y - 2);
  1220. context.closePath();
  1221. context.stroke();
  1222. }
  1223. });
  1224. }
  1225. function drawGaugeLabel(gaugeOption, radius, centerPosition, opts, config, context) {
  1226. radius -= gaugeOption.width / 2 + config.gaugeLabelTextMargin;
  1227. let totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  1228. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  1229. let totalNumber = gaugeOption.endNumber - gaugeOption.startNumber;
  1230. let splitNumber = totalNumber / gaugeOption.splitLine.splitNumber;
  1231. let nowAngle = gaugeOption.startAngle;
  1232. let nowNumber = gaugeOption.startNumber;
  1233. for (let i = 0; i < gaugeOption.splitLine.splitNumber + 1; i++) {
  1234. var pos = {
  1235. x: radius * Math.cos(nowAngle * Math.PI),
  1236. y: radius * Math.sin(nowAngle * Math.PI)
  1237. };
  1238. pos.x += centerPosition.x - measureText(nowNumber) / 2;
  1239. pos.y += centerPosition.y;
  1240. var startX = pos.x;
  1241. var startY = pos.y;
  1242. context.beginPath();
  1243. context.setFontSize(config.fontSize);
  1244. context.setFillStyle(gaugeOption.labelColor || '#666666');
  1245. context.fillText(nowNumber, startX, startY + config.fontSize / 2);
  1246. context.closePath();
  1247. context.stroke();
  1248. nowAngle += splitAngle;
  1249. if (nowAngle >= 2) {
  1250. nowAngle = nowAngle % 2;
  1251. }
  1252. nowNumber += splitNumber;
  1253. }
  1254. }
  1255. function drawRadarLabel(angleList, radius, centerPosition, opts, config, context) {
  1256. var radarOption = opts.extra.radar || {};
  1257. radius += config.radarLabelTextMargin;
  1258. angleList.forEach(function(angle, index) {
  1259. var pos = {
  1260. x: radius * Math.cos(angle),
  1261. y: radius * Math.sin(angle)
  1262. };
  1263. var posRelativeCanvas = convertCoordinateOrigin(pos.x, pos.y, centerPosition);
  1264. var startX = posRelativeCanvas.x;
  1265. var startY = posRelativeCanvas.y;
  1266. if (util.approximatelyEqual(pos.x, 0)) {
  1267. startX -= measureText(opts.categories[index] || '') / 2;
  1268. } else if (pos.x < 0) {
  1269. startX -= measureText(opts.categories[index] || '');
  1270. }
  1271. context.beginPath();
  1272. context.setFontSize(config.fontSize);
  1273. context.setFillStyle(radarOption.labelColor || '#666666');
  1274. context.fillText(opts.categories[index] || '', startX, startY + config.fontSize / 2);
  1275. context.closePath();
  1276. context.stroke();
  1277. });
  1278. }
  1279. function drawPieText(series, opts, config, context, radius, center) {
  1280. var lineRadius = config.pieChartLinePadding;
  1281. var textObjectCollection = [];
  1282. var lastTextObject = null;
  1283. var seriesConvert = series.map(function(item) {
  1284. var arc = 2 * Math.PI - (item._start_ + 2 * Math.PI * item._proportion_ / 2);
  1285. var text = item.format ? item.format(+item._proportion_.toFixed(2)) : util.toFixed(item._proportion_ * 100) + '%';
  1286. var color = item.color;
  1287. var radius = item._radius_;
  1288. return {
  1289. arc: arc,
  1290. text: text,
  1291. color: color,
  1292. radius: radius
  1293. };
  1294. });
  1295. for (let i = 0; i < seriesConvert.length; i++) {
  1296. let item = seriesConvert[i];
  1297. // line end
  1298. let orginX1 = Math.cos(item.arc) * (item.radius+lineRadius);
  1299. let orginY1 = Math.sin(item.arc) * (item.radius+lineRadius);
  1300. // line start
  1301. let orginX2 = Math.cos(item.arc) * item.radius;
  1302. let orginY2 = Math.sin(item.arc) * item.radius;
  1303. // text start
  1304. let orginX3 = orginX1 >= 0 ? orginX1 + config.pieChartTextPadding : orginX1 - config.pieChartTextPadding;
  1305. let orginY3 = orginY1;
  1306. let textWidth = measureText(item.text);
  1307. let startY = orginY3;
  1308. if (lastTextObject && util.isSameXCoordinateArea(lastTextObject.start, {
  1309. x: orginX3
  1310. })) {
  1311. if (orginX3 > 0) {
  1312. startY = Math.min(orginY3, lastTextObject.start.y);
  1313. } else if (orginX1 < 0) {
  1314. startY = Math.max(orginY3, lastTextObject.start.y);
  1315. } else {
  1316. if (orginY3 > 0) {
  1317. startY = Math.max(orginY3, lastTextObject.start.y);
  1318. } else {
  1319. startY = Math.min(orginY3, lastTextObject.start.y);
  1320. }
  1321. }
  1322. }
  1323. if (orginX3 < 0) {
  1324. orginX3 -= textWidth;
  1325. }
  1326. let textObject = {
  1327. lineStart: {
  1328. x: orginX2,
  1329. y: orginY2
  1330. },
  1331. lineEnd: {
  1332. x: orginX1,
  1333. y: orginY1
  1334. },
  1335. start: {
  1336. x: orginX3,
  1337. y: startY
  1338. },
  1339. width: textWidth,
  1340. height: config.fontSize,
  1341. text: item.text,
  1342. color: item.color
  1343. };
  1344. lastTextObject = avoidCollision(textObject, lastTextObject);
  1345. textObjectCollection.push(lastTextObject);
  1346. }
  1347. for (let i = 0; i < textObjectCollection.length; i++) {
  1348. let item = textObjectCollection[i];
  1349. let lineStartPoistion = convertCoordinateOrigin(item.lineStart.x, item.lineStart.y, center);
  1350. let lineEndPoistion = convertCoordinateOrigin(item.lineEnd.x, item.lineEnd.y, center);
  1351. let textPosition = convertCoordinateOrigin(item.start.x, item.start.y, center);
  1352. context.setLineWidth(1 * opts.pixelRatio);
  1353. context.setFontSize(config.fontSize);
  1354. context.beginPath();
  1355. context.setStrokeStyle(item.color);
  1356. context.setFillStyle(item.color);
  1357. context.moveTo(lineStartPoistion.x, lineStartPoistion.y);
  1358. let curveStartX = item.start.x < 0 ? textPosition.x + item.width : textPosition.x;
  1359. let textStartX = item.start.x < 0 ? textPosition.x - 5 : textPosition.x + 5;
  1360. context.quadraticCurveTo(lineEndPoistion.x, lineEndPoistion.y, curveStartX, textPosition.y);
  1361. context.moveTo(lineStartPoistion.x, lineStartPoistion.y);
  1362. context.stroke();
  1363. context.closePath();
  1364. context.beginPath();
  1365. context.moveTo(textPosition.x + item.width, textPosition.y);
  1366. context.arc(curveStartX, textPosition.y, 2, 0, 2 * Math.PI);
  1367. context.closePath();
  1368. context.fill();
  1369. context.beginPath();
  1370. context.setFontSize(config.fontSize);
  1371. context.setFillStyle('#666666');
  1372. context.fillText(item.text, textStartX, textPosition.y + 3);
  1373. context.closePath();
  1374. context.stroke();
  1375. context.closePath();
  1376. }
  1377. }
  1378. function drawToolTipSplitLine(offsetX, opts, config, context) {
  1379. var toolTipOption = opts.extra.tooltip || {};
  1380. toolTipOption.gridType = toolTipOption.gridType == undefined ? 'solid' : toolTipOption.gridType;
  1381. toolTipOption.dashLength = toolTipOption.dashLength == undefined ? 4 : toolTipOption.dashLength;
  1382. var startY = config.padding;
  1383. var endY = opts.height - config.padding - config.xAxisHeight - config.legendHeight;
  1384. if (toolTipOption.gridType == 'dash') {
  1385. context.setLineDash([toolTipOption.dashLength, toolTipOption.dashLength]);
  1386. }
  1387. context.beginPath();
  1388. context.setStrokeStyle(toolTipOption.gridColor || '#cccccc');
  1389. context.setLineWidth(1 * opts.pixelRatio);
  1390. context.moveTo(offsetX, startY);
  1391. context.lineTo(offsetX, endY);
  1392. context.closePath();
  1393. context.stroke();
  1394. context.setLineDash([]);
  1395. if (toolTipOption.xAxisLabel) {
  1396. let lableText = opts.categories[opts.tooltip.index];
  1397. context.setFontSize(config.fontSize);
  1398. let textWidth = context.measureText(lableText).width;
  1399. let textX = offsetX - config.toolTipPadding - 0.5 * textWidth;
  1400. let textY = endY;
  1401. context.beginPath();
  1402. context.setFillStyle(hexToRgb(toolTipOption.labelBgColor || config.toolTipBackground, toolTipOption.labelBgOpacity ||
  1403. config.toolTipOpacity));
  1404. context.setStrokeStyle(toolTipOption.labelBgColor || config.toolTipBackground);
  1405. context.setLineWidth(1 * opts.pixelRatio);
  1406. context.rect(textX, textY, textWidth + 2 * config.toolTipPadding, config.fontSize + 2 * config.toolTipPadding);
  1407. context.closePath();
  1408. context.stroke();
  1409. context.fill();
  1410. context.beginPath();
  1411. context.setFontSize(config.fontSize);
  1412. context.setFillStyle(toolTipOption.labelFontColor || config.fontColor);
  1413. context.fillText(lableText, textX + 2 * config.toolTipPadding, textY + config.toolTipPadding + config.fontSize);
  1414. context.closePath();
  1415. context.stroke();
  1416. }
  1417. }
  1418. function drawToolTipHorizentalLine(opts, config, context, eachSpacing, xAxisPoints) {
  1419. var toolTipOption = opts.extra.tooltip || {};
  1420. toolTipOption.gridType = toolTipOption.gridType == undefined ? 'solid' : toolTipOption.gridType;
  1421. toolTipOption.dashLength = toolTipOption.dashLength == undefined ? 4 : toolTipOption.dashLength;
  1422. var startX = config.padding + config.yAxisWidth + config.yAxisTitleWidth;
  1423. var endX = opts.width - config.padding;
  1424. if (toolTipOption.gridType == 'dash') {
  1425. context.setLineDash([toolTipOption.dashLength, toolTipOption.dashLength]);
  1426. }
  1427. context.beginPath();
  1428. context.setStrokeStyle(toolTipOption.gridColor || '#cccccc');
  1429. context.setLineWidth(1 * opts.pixelRatio);
  1430. context.moveTo(startX, opts.tooltip.offset.y);
  1431. context.lineTo(endX, opts.tooltip.offset.y);
  1432. context.closePath();
  1433. context.stroke();
  1434. context.setLineDash([]);
  1435. if (toolTipOption.yAxisLabel) {
  1436. let lableText = calTooltipYAxisData(opts.tooltip.offset.y, opts.series, opts, config, eachSpacing);
  1437. context.setFontSize(config.fontSize);
  1438. let textWidth = context.measureText(lableText).width;
  1439. let textX = startX - 2 * config.toolTipPadding - textWidth;
  1440. let textY = opts.tooltip.offset.y;
  1441. context.beginPath();
  1442. context.setFillStyle(hexToRgb(toolTipOption.labelBgColor || config.toolTipBackground, toolTipOption.labelBgOpacity ||
  1443. config.toolTipOpacity));
  1444. context.setStrokeStyle(toolTipOption.labelBgColor || config.toolTipBackground);
  1445. context.setLineWidth(1 * opts.pixelRatio);
  1446. context.rect(textX, textY - 0.5 * config.fontSize - config.toolTipPadding, textWidth + 2 * config.toolTipPadding,
  1447. config.fontSize + 2 * config.toolTipPadding);
  1448. context.closePath();
  1449. context.stroke();
  1450. context.fill();
  1451. context.beginPath();
  1452. context.setFontSize(config.fontSize);
  1453. context.setFillStyle(toolTipOption.labelFontColor || config.fontColor);
  1454. context.fillText(lableText, textX + config.toolTipPadding, textY + 0.5 * config.fontSize);
  1455. context.closePath();
  1456. context.stroke();
  1457. }
  1458. }
  1459. function drawToolTipSplitArea(offsetX, opts, config, context, eachSpacing) {
  1460. var toolTipOption = opts.extra.tooltip || {
  1461. activeBgColor: '#000000',
  1462. activeBgOpacity: 0.08
  1463. };
  1464. toolTipOption.activeBgColor = toolTipOption.activeBgColor ? toolTipOption.activeBgColor : '#000000';
  1465. toolTipOption.activeBgOpacity = toolTipOption.activeBgOpacity ? toolTipOption.activeBgOpacity : 0.08;
  1466. var startY = config.padding;
  1467. var endY = opts.height - config.padding - config.xAxisHeight - config.legendHeight;
  1468. context.beginPath();
  1469. context.setFillStyle(hexToRgb(toolTipOption.activeBgColor, toolTipOption.activeBgOpacity));
  1470. context.rect(offsetX - eachSpacing / 2, startY, eachSpacing, endY - startY);
  1471. context.closePath();
  1472. context.fill();
  1473. }
  1474. function drawToolTip(textList, offset, opts, config, context, eachSpacing, xAxisPoints) {
  1475. var toolTipOption = opts.extra.tooltip || {
  1476. bgColor: '#000000',
  1477. bgOpacity: 0.7,
  1478. fontColor: '#FFFFFF'
  1479. };
  1480. toolTipOption.bgColor = toolTipOption.bgColor ? toolTipOption.bgColor : '#000000';
  1481. toolTipOption.bgOpacity = toolTipOption.bgOpacity ? toolTipOption.bgOpacity : 0.7;
  1482. toolTipOption.fontColor = toolTipOption.fontColor ? toolTipOption.fontColor : '#FFFFFF';
  1483. var legendWidth = 4 * opts.pixelRatio;
  1484. var legendMarginRight = 5 * opts.pixelRatio;
  1485. var arrowWidth = 8 * opts.pixelRatio;
  1486. var isOverRightBorder = false;
  1487. if (opts.type == 'line' || opts.type == 'area' || opts.type == 'candle' || opts.type == 'mix') {
  1488. drawToolTipSplitLine(opts.tooltip.offset.x, opts, config, context);
  1489. }
  1490. offset = assign({
  1491. x: 0,
  1492. y: 0
  1493. }, offset);
  1494. offset.y -= 8 * opts.pixelRatio;
  1495. var textWidth = textList.map(function(item) {
  1496. return measureText(item.text);
  1497. });
  1498. var toolTipWidth = legendWidth + legendMarginRight + 4 * config.toolTipPadding + Math.max.apply(null, textWidth);
  1499. var toolTipHeight = 2 * config.toolTipPadding + textList.length * config.toolTipLineHeight;
  1500. // if beyond the right border
  1501. if (offset.x - Math.abs(opts._scrollDistance_) + arrowWidth + toolTipWidth > opts.width) {
  1502. isOverRightBorder = true;
  1503. }
  1504. // draw background rect
  1505. context.beginPath();
  1506. context.setFillStyle(hexToRgb(toolTipOption.bgColor || config.toolTipBackground, toolTipOption.bgOpacity || config.toolTipOpacity));
  1507. if (isOverRightBorder) {
  1508. context.moveTo(offset.x, offset.y + 10 * opts.pixelRatio);
  1509. context.lineTo(offset.x - arrowWidth, offset.y + 10 * opts.pixelRatio - 5 * opts.pixelRatio);
  1510. context.lineTo(offset.x - arrowWidth, offset.y);
  1511. context.lineTo(offset.x - arrowWidth - Math.round(toolTipWidth), offset.y);
  1512. context.lineTo(offset.x - arrowWidth - Math.round(toolTipWidth), offset.y + toolTipHeight);
  1513. context.lineTo(offset.x - arrowWidth, offset.y + toolTipHeight);
  1514. context.lineTo(offset.x - arrowWidth, offset.y + 10 * opts.pixelRatio + 5 * opts.pixelRatio);
  1515. context.lineTo(offset.x, offset.y + 10 * opts.pixelRatio);
  1516. } else {
  1517. context.moveTo(offset.x, offset.y + 10 * opts.pixelRatio);
  1518. context.lineTo(offset.x + arrowWidth, offset.y + 10 * opts.pixelRatio - 5 * opts.pixelRatio);
  1519. context.lineTo(offset.x + arrowWidth, offset.y);
  1520. context.lineTo(offset.x + arrowWidth + Math.round(toolTipWidth), offset.y);
  1521. context.lineTo(offset.x + arrowWidth + Math.round(toolTipWidth), offset.y + toolTipHeight);
  1522. context.lineTo(offset.x + arrowWidth, offset.y + toolTipHeight);
  1523. context.lineTo(offset.x + arrowWidth, offset.y + 10 * opts.pixelRatio + 5 * opts.pixelRatio);
  1524. context.lineTo(offset.x, offset.y + 10 * opts.pixelRatio);
  1525. }
  1526. context.closePath();
  1527. context.fill();
  1528. // draw legend
  1529. textList.forEach(function(item, index) {
  1530. if (item.color !== null) {
  1531. context.beginPath();
  1532. context.setFillStyle(item.color);
  1533. var startX = offset.x + arrowWidth + 2 * config.toolTipPadding;
  1534. var startY = offset.y + (config.toolTipLineHeight - config.fontSize) / 2 + config.toolTipLineHeight * index +
  1535. config.toolTipPadding + 1;
  1536. if (isOverRightBorder) {
  1537. startX = offset.x - toolTipWidth - arrowWidth + 2 * config.toolTipPadding;
  1538. }
  1539. context.fillRect(startX, startY, legendWidth, config.fontSize);
  1540. context.closePath();
  1541. }
  1542. });
  1543. // draw text list
  1544. textList.forEach(function(item, index) {
  1545. var startX = offset.x + arrowWidth + 2 * config.toolTipPadding + legendWidth + legendMarginRight;
  1546. if (isOverRightBorder) {
  1547. startX = offset.x - toolTipWidth - arrowWidth + 2 * config.toolTipPadding + +legendWidth + legendMarginRight;
  1548. }
  1549. var startY = offset.y + (config.toolTipLineHeight - config.fontSize) / 2 + config.toolTipLineHeight * index +
  1550. config.toolTipPadding;
  1551. context.beginPath();
  1552. context.setFontSize(config.fontSize);
  1553. context.setFillStyle(toolTipOption.fontColor);
  1554. context.fillText(item.text, startX, startY + config.fontSize);
  1555. context.closePath();
  1556. context.stroke();
  1557. });
  1558. }
  1559. function drawYAxisTitle(title, opts, config, context) {
  1560. var startX = config.xAxisHeight + (opts.height - config.xAxisHeight - measureText(title)) / 2;
  1561. context.save();
  1562. context.beginPath();
  1563. context.setFontSize(config.fontSize);
  1564. context.setFillStyle(opts.yAxis.titleFontColor || '#333333');
  1565. context.translate(0, opts.height);
  1566. context.rotate(-90 * Math.PI / 180);
  1567. context.fillText(title, startX, config.padding + 0.5 * config.fontSize);
  1568. context.closePath();
  1569. context.stroke();
  1570. context.restore();
  1571. }
  1572. function drawColumnDataPoints(series, opts, config, context) {
  1573. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  1574. var columnOption = opts.extra.column || {
  1575. type: {},
  1576. meter: {}
  1577. };
  1578. columnOption.type = columnOption.type == undefined ? 'group' : columnOption.type;
  1579. columnOption.meter = columnOption.meter || {}
  1580. columnOption.meter.border = columnOption.meter.border == undefined ? 4 : columnOption.meter.border;
  1581. columnOption.meter.fillColor = columnOption.meter.fillColor == undefined ? '#FFFFFF' : columnOption.meter.fillColor;
  1582. var _calYAxisData = calYAxisData(series, opts, config),
  1583. ranges = _calYAxisData.ranges;
  1584. var _getXAxisPoints = getXAxisPoints(opts.categories, opts, config),
  1585. xAxisPoints = _getXAxisPoints.xAxisPoints,
  1586. eachSpacing = _getXAxisPoints.eachSpacing;
  1587. var minRange = ranges.pop();
  1588. var maxRange = ranges.shift();
  1589. var calPoints = [];
  1590. context.save();
  1591. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  1592. context.translate(opts._scrollDistance_, 0);
  1593. }
  1594. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  1595. drawToolTipSplitArea(opts.tooltip.offset.x, opts, config, context, eachSpacing);
  1596. }
  1597. series.forEach(function(eachSeries, seriesIndex) {
  1598. var data = eachSeries.data;
  1599. switch (columnOption.type) {
  1600. case 'group':
  1601. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  1602. var tooltipPoints = getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config,
  1603. seriesIndex, series, process);
  1604. calPoints.push(tooltipPoints);
  1605. points = fixColumeData(points, eachSpacing, series.length, seriesIndex, config, opts);
  1606. points.forEach(function(item, index) {
  1607. if (item !== null) {
  1608. context.beginPath();
  1609. context.setFillStyle(item.color || eachSeries.color);
  1610. var startX = item.x - item.width / 2 + 1;
  1611. var height = opts.height - item.y - config.padding - config.xAxisHeight - config.legendHeight;
  1612. context.moveTo(startX, item.y);
  1613. context.fillRect(startX, item.y, item.width - 2, height);
  1614. context.closePath();
  1615. context.fill();
  1616. }
  1617. });
  1618. break;
  1619. case 'stack':
  1620. // 绘制堆叠数据图
  1621. var points = getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, seriesIndex,
  1622. series, process);
  1623. calPoints.push(points);
  1624. points = fixColumeStackData(points, eachSpacing, series.length, seriesIndex, config, opts, series);
  1625. points.forEach(function(item, index) {
  1626. if (item !== null) {
  1627. context.beginPath();
  1628. context.setFillStyle(item.color || eachSeries.color);
  1629. var startX = item.x - item.width / 2 + 1;
  1630. var height = opts.height - item.y - config.padding - config.xAxisHeight - config.legendHeight;
  1631. var height0 = opts.height - item.y0 - config.padding - config.xAxisHeight - config.legendHeight;
  1632. if (seriesIndex > 0) {
  1633. height -= height0;
  1634. }
  1635. context.moveTo(startX, item.y);
  1636. context.fillRect(startX, item.y, item.width - 2, height);
  1637. context.closePath();
  1638. context.fill();
  1639. }
  1640. });
  1641. break;
  1642. case 'meter':
  1643. // 绘制温度计数据图
  1644. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  1645. calPoints.push(points);
  1646. points = fixColumeMeterData(points, eachSpacing, series.length, seriesIndex, config, opts, columnOption.meter.border);
  1647. if (seriesIndex == 0) {
  1648. points.forEach(function(item, index) {
  1649. if (item !== null) {
  1650. //画背景颜色
  1651. context.beginPath();
  1652. context.setFillStyle(columnOption.meter.fillColor);
  1653. var startX = item.x - item.width / 2 + 1;
  1654. var height = opts.height - item.y - config.padding - config.xAxisHeight - config.legendHeight;
  1655. context.moveTo(startX, item.y);
  1656. context.fillRect(startX, item.y, item.width - 2, height);
  1657. context.closePath();
  1658. context.fill();
  1659. //画边框线
  1660. context.beginPath();
  1661. context.setStrokeStyle(eachSeries.color);
  1662. context.setLineWidth(columnOption.meter.border * opts.pixelRatio);
  1663. context.moveTo(startX + columnOption.meter.border * 0.5, item.y + height);
  1664. context.lineTo(startX + columnOption.meter.border * 0.5, item.y + columnOption.meter.border * 0.5);
  1665. context.lineTo(startX + item.width - columnOption.meter.border, item.y + columnOption.meter.border * 0.5);
  1666. context.lineTo(startX + item.width - columnOption.meter.border, item.y + height);
  1667. context.stroke();
  1668. }
  1669. });
  1670. } else {
  1671. points.forEach(function(item, index) {
  1672. if (item !== null) {
  1673. context.beginPath();
  1674. context.setFillStyle(item.color || eachSeries.color);
  1675. var startX = item.x - item.width / 2 + 1;
  1676. var height = opts.height - item.y - config.padding - config.xAxisHeight - config.legendHeight;
  1677. context.moveTo(startX, item.y);
  1678. context.rect(startX, item.y, item.width - 2, height);
  1679. context.closePath();
  1680. context.fill();
  1681. }
  1682. });
  1683. }
  1684. break;
  1685. }
  1686. });
  1687. if (opts.dataLabel !== false && process === 1) {
  1688. series.forEach(function(eachSeries, seriesIndex) {
  1689. var data = eachSeries.data;
  1690. switch (columnOption.type) {
  1691. case 'group':
  1692. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  1693. points = fixColumeData(points, eachSpacing, series.length, seriesIndex, config, opts);
  1694. drawPointText(points, eachSeries, config, context);
  1695. break;
  1696. case 'stack':
  1697. var points = getStackDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, seriesIndex,
  1698. series, process);
  1699. drawPointText(points, eachSeries, config, context);
  1700. break;
  1701. case 'meter':
  1702. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  1703. drawPointText(points, eachSeries, config, context);
  1704. break;
  1705. }
  1706. });
  1707. }
  1708. context.restore();
  1709. return {
  1710. xAxisPoints: xAxisPoints,
  1711. calPoints: calPoints,
  1712. eachSpacing: eachSpacing
  1713. };
  1714. }
  1715. function drawCandleDataPoints(series, seriesMA, opts, config, context) {
  1716. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  1717. var candleOption = opts.extra.candle || {
  1718. color: {},
  1719. average: {}
  1720. };
  1721. candleOption.color.upLine = candleOption.color.upLine ? candleOption.color.upLine : '#f04864';
  1722. candleOption.color.upFill = candleOption.color.upFill ? candleOption.color.upFill : '#f04864';
  1723. candleOption.color.downLine = candleOption.color.downLine ? candleOption.color.downLine : '#2fc25b';
  1724. candleOption.color.downFill = candleOption.color.downFill ? candleOption.color.downFill : '#2fc25b';
  1725. candleOption.average.show = candleOption.average.show === true ? true : false;
  1726. candleOption.average.name = candleOption.average.name ? candleOption.average.name : [];
  1727. candleOption.average.day = candleOption.average.day ? candleOption.average.day : [];
  1728. candleOption.average.color = candleOption.average.color ? candleOption.average.color : ['#1890ff', '#2fc25b',
  1729. '#facc14', '#f04864', '#8543e0', '#90ed7d'
  1730. ];
  1731. opts.extra.candle = candleOption;
  1732. var _calYAxisData5 = calYAxisData(series, opts, config),
  1733. ranges = _calYAxisData5.ranges;
  1734. var _getXAxisPoints5 = getXAxisPoints(opts.categories, opts, config),
  1735. xAxisPoints = _getXAxisPoints5.xAxisPoints,
  1736. eachSpacing = _getXAxisPoints5.eachSpacing;
  1737. var minRange = ranges.pop();
  1738. var maxRange = ranges.shift();
  1739. var calPoints = [];
  1740. context.save();
  1741. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  1742. context.translate(opts._scrollDistance_, 0);
  1743. }
  1744. //画均线
  1745. if (candleOption.average.show) {
  1746. seriesMA.forEach(function(eachSeries, seriesIndex) {
  1747. var data = eachSeries.data;
  1748. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  1749. //calPoints.push(points);
  1750. var splitPointList = splitPoints(points);
  1751. splitPointList.forEach(function(points, index) {
  1752. context.beginPath();
  1753. context.setStrokeStyle(eachSeries.color);
  1754. context.setLineWidth(1);
  1755. if (points.length === 1) {
  1756. context.moveTo(points[0].x, points[0].y);
  1757. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  1758. } else {
  1759. context.moveTo(points[0].x, points[0].y);
  1760. points.forEach(function(item, index) {
  1761. if (index > 0) {
  1762. var ctrlPoint = createCurveControlPoints(points, index - 1);
  1763. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item
  1764. .y);
  1765. }
  1766. });
  1767. context.moveTo(points[0].x, points[0].y);
  1768. }
  1769. context.closePath();
  1770. context.stroke();
  1771. });
  1772. });
  1773. }
  1774. //画K线
  1775. series.forEach(function(eachSeries, seriesIndex) {
  1776. var data = eachSeries.data;
  1777. var points = getCandleDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  1778. calPoints.push(points);
  1779. var splitPointList = splitPoints(points);
  1780. splitPointList = splitPointList[0];
  1781. splitPointList.forEach(function(points, index) {
  1782. context.beginPath();
  1783. //如果上涨
  1784. if (data[index][1] - data[index][0] > 0) {
  1785. context.setStrokeStyle(candleOption.color.upLine);
  1786. context.setFillStyle(candleOption.color.upFill);
  1787. context.setLineWidth(1 * opts.pixelRatio);
  1788. context.moveTo(points[3].x, points[3].y); //顶点
  1789. context.lineTo(points[1].x, points[1].y); //收盘中间点
  1790. context.lineTo(points[1].x - eachSpacing / 4, points[1].y); //收盘左侧点
  1791. context.lineTo(points[0].x - eachSpacing / 4, points[0].y); //开盘左侧点
  1792. context.lineTo(points[0].x, points[0].y); //开盘中间点
  1793. context.lineTo(points[2].x, points[2].y); //底点
  1794. context.lineTo(points[0].x, points[0].y); //开盘中间点
  1795. context.lineTo(points[0].x + eachSpacing / 4, points[0].y); //开盘右侧点
  1796. context.lineTo(points[1].x + eachSpacing / 4, points[1].y); //收盘右侧点
  1797. context.lineTo(points[1].x, points[1].y); //收盘中间点
  1798. context.moveTo(points[3].x, points[3].y); //顶点
  1799. } else {
  1800. context.setStrokeStyle(candleOption.color.downLine);
  1801. context.setFillStyle(candleOption.color.downFill);
  1802. context.setLineWidth(1 * opts.pixelRatio);
  1803. context.moveTo(points[3].x, points[3].y); //顶点
  1804. context.lineTo(points[0].x, points[0].y); //开盘中间点
  1805. context.lineTo(points[0].x - eachSpacing / 4, points[0].y); //开盘左侧点
  1806. context.lineTo(points[1].x - eachSpacing / 4, points[1].y); //收盘左侧点
  1807. context.lineTo(points[1].x, points[1].y); //收盘中间点
  1808. context.lineTo(points[2].x, points[2].y); //底点
  1809. context.lineTo(points[1].x, points[1].y); //收盘中间点
  1810. context.lineTo(points[1].x + eachSpacing / 4, points[1].y); //收盘右侧点
  1811. context.lineTo(points[0].x + eachSpacing / 4, points[0].y); //开盘右侧点
  1812. context.lineTo(points[0].x, points[0].y); //开盘中间点
  1813. context.moveTo(points[3].x, points[3].y); //顶点
  1814. }
  1815. context.closePath();
  1816. context.fill();
  1817. context.stroke();
  1818. });
  1819. });
  1820. context.restore();
  1821. return {
  1822. xAxisPoints: xAxisPoints,
  1823. calPoints: calPoints,
  1824. eachSpacing: eachSpacing
  1825. };
  1826. }
  1827. function drawAreaDataPoints(series, opts, config, context) {
  1828. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  1829. var areaOption = opts.extra.area || {
  1830. type: 'straight',
  1831. opacity: 0.5,
  1832. addLine: false,
  1833. width: 2
  1834. };
  1835. areaOption.type = areaOption.type ? areaOption.type : 'straight';
  1836. areaOption.opacity = areaOption.opacity ? areaOption.opacity : 0.2;
  1837. areaOption.addLine = areaOption.addLine == true ? true : false;
  1838. areaOption.width = areaOption.width ? areaOption.width : 2;
  1839. var _calYAxisData2 = calYAxisData(series, opts, config),
  1840. ranges = _calYAxisData2.ranges;
  1841. var _getXAxisPoints2 = getXAxisPoints(opts.categories, opts, config),
  1842. xAxisPoints = _getXAxisPoints2.xAxisPoints,
  1843. eachSpacing = _getXAxisPoints2.eachSpacing;
  1844. var minRange = ranges.pop();
  1845. var maxRange = ranges.shift();
  1846. var endY = opts.height - config.padding - config.xAxisHeight - config.legendHeight;
  1847. var calPoints = [];
  1848. context.save();
  1849. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  1850. context.translate(opts._scrollDistance_, 0);
  1851. }
  1852. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  1853. drawToolTipSplitLine(opts.tooltip.offset.x, opts, config, context);
  1854. }
  1855. series.forEach(function(eachSeries, seriesIndex) {
  1856. let data = eachSeries.data;
  1857. let points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  1858. calPoints.push(points);
  1859. let splitPointList = splitPoints(points);
  1860. for (let i = 0; i < splitPointList.length; i++) {
  1861. let points = splitPointList[i];
  1862. // 绘制区域数
  1863. context.beginPath();
  1864. context.setStrokeStyle(hexToRgb(eachSeries.color, areaOption.opacity));
  1865. context.setFillStyle(hexToRgb(eachSeries.color, areaOption.opacity));
  1866. context.setLineWidth(areaOption.width * opts.pixelRatio);
  1867. if (points.length > 1) {
  1868. let firstPoint = points[0];
  1869. let lastPoint = points[points.length - 1];
  1870. context.moveTo(firstPoint.x, firstPoint.y);
  1871. if (areaOption.type === 'curve') {
  1872. points.forEach(function(item, index) {
  1873. if (index > 0) {
  1874. let ctrlPoint = createCurveControlPoints(points, index - 1);
  1875. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item
  1876. .y);
  1877. }
  1878. });
  1879. } else {
  1880. points.forEach(function(item, index) {
  1881. if (index > 0) {
  1882. context.lineTo(item.x, item.y);
  1883. }
  1884. });
  1885. }
  1886. context.lineTo(lastPoint.x, endY);
  1887. context.lineTo(firstPoint.x, endY);
  1888. context.lineTo(firstPoint.x, firstPoint.y);
  1889. } else {
  1890. let item = points[0];
  1891. context.moveTo(item.x - eachSpacing / 2, item.y);
  1892. context.lineTo(item.x + eachSpacing / 2, item.y);
  1893. context.lineTo(item.x + eachSpacing / 2, endY);
  1894. context.lineTo(item.x - eachSpacing / 2, endY);
  1895. context.moveTo(item.x - eachSpacing / 2, item.y);
  1896. }
  1897. context.closePath();
  1898. context.fill();
  1899. //画连线
  1900. if (areaOption.addLine) {
  1901. context.beginPath();
  1902. context.setStrokeStyle(eachSeries.color);
  1903. context.setLineWidth(areaOption.width * opts.pixelRatio);
  1904. if (points.length === 1) {
  1905. context.moveTo(points[0].x, points[0].y);
  1906. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  1907. } else {
  1908. context.moveTo(points[0].x, points[0].y);
  1909. if (areaOption.type === 'curve') {
  1910. points.forEach(function(item, index) {
  1911. if (index > 0) {
  1912. let ctrlPoint = createCurveControlPoints(points, index - 1);
  1913. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x,
  1914. item.y);
  1915. }
  1916. });
  1917. } else {
  1918. points.forEach(function(item, index) {
  1919. if (index > 0) {
  1920. context.lineTo(item.x, item.y);
  1921. }
  1922. });
  1923. }
  1924. context.moveTo(points[0].x, points[0].y);
  1925. }
  1926. context.closePath();
  1927. context.stroke();
  1928. }
  1929. }
  1930. //画点
  1931. if (opts.dataPointShape !== false) {
  1932. var shape = config.dataPointShape[seriesIndex % config.dataPointShape.length];
  1933. drawPointShape(points, eachSeries.color, shape, context, opts);
  1934. }
  1935. });
  1936. if (opts.dataLabel !== false && process === 1) {
  1937. series.forEach(function(eachSeries, seriesIndex) {
  1938. var data = eachSeries.data;
  1939. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  1940. drawPointText(points, eachSeries, config, context);
  1941. });
  1942. }
  1943. context.restore();
  1944. return {
  1945. xAxisPoints: xAxisPoints,
  1946. calPoints: calPoints,
  1947. eachSpacing: eachSpacing
  1948. };
  1949. }
  1950. function drawLineDataPoints(series, opts, config, context) {
  1951. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  1952. var lineOption = opts.extra.line || {
  1953. type: 'straight',
  1954. width: 2
  1955. };
  1956. lineOption.type = lineOption.type ? lineOption.type : 'straight';
  1957. lineOption.width = lineOption.width ? lineOption.width : 2;
  1958. var _calYAxisData3 = calYAxisData(series, opts, config),
  1959. ranges = _calYAxisData3.ranges;
  1960. var _getXAxisPoints3 = getXAxisPoints(opts.categories, opts, config),
  1961. xAxisPoints = _getXAxisPoints3.xAxisPoints,
  1962. eachSpacing = _getXAxisPoints3.eachSpacing;
  1963. var minRange = ranges.pop();
  1964. var maxRange = ranges.shift();
  1965. var calPoints = [];
  1966. context.save();
  1967. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  1968. context.translate(opts._scrollDistance_, 0);
  1969. }
  1970. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  1971. drawToolTipSplitLine(opts.tooltip.offset.x, opts, config, context);
  1972. }
  1973. series.forEach(function(eachSeries, seriesIndex) {
  1974. var data = eachSeries.data;
  1975. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  1976. calPoints.push(points);
  1977. var splitPointList = splitPoints(points);
  1978. splitPointList.forEach(function(points, index) {
  1979. context.beginPath();
  1980. context.setStrokeStyle(eachSeries.color);
  1981. context.setLineWidth(lineOption.width * opts.pixelRatio);
  1982. if (points.length === 1) {
  1983. context.moveTo(points[0].x, points[0].y);
  1984. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  1985. } else {
  1986. context.moveTo(points[0].x, points[0].y);
  1987. if (lineOption.type === 'curve') {
  1988. points.forEach(function(item, index) {
  1989. if (index > 0) {
  1990. var ctrlPoint = createCurveControlPoints(points, index - 1);
  1991. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item
  1992. .y);
  1993. }
  1994. });
  1995. } else {
  1996. points.forEach(function(item, index) {
  1997. if (index > 0) {
  1998. context.lineTo(item.x, item.y);
  1999. }
  2000. });
  2001. }
  2002. context.moveTo(points[0].x, points[0].y);
  2003. }
  2004. context.closePath();
  2005. context.stroke();
  2006. });
  2007. if (opts.dataPointShape !== false) {
  2008. var shape = config.dataPointShape[seriesIndex % config.dataPointShape.length];
  2009. drawPointShape(points, eachSeries.color, shape, context, opts);
  2010. }
  2011. });
  2012. if (opts.dataLabel !== false && process === 1) {
  2013. series.forEach(function(eachSeries, seriesIndex) {
  2014. var data = eachSeries.data;
  2015. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2016. drawPointText(points, eachSeries, config, context);
  2017. });
  2018. }
  2019. context.restore();
  2020. return {
  2021. xAxisPoints: xAxisPoints,
  2022. calPoints: calPoints,
  2023. eachSpacing: eachSpacing
  2024. };
  2025. }
  2026. function drawMixDataPoints(series, opts, config, context) {
  2027. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2028. var _calYAxisData6 = calYAxisData(series, opts, config),
  2029. ranges = _calYAxisData6.ranges;
  2030. var _getXAxisPoints6 = getXAxisPoints(opts.categories, opts, config),
  2031. xAxisPoints = _getXAxisPoints6.xAxisPoints,
  2032. eachSpacing = _getXAxisPoints6.eachSpacing;
  2033. var minRange = ranges.pop();
  2034. var maxRange = ranges.shift();
  2035. var endY = opts.height - config.padding - config.xAxisHeight - config.legendHeight;
  2036. var calPoints = [];
  2037. var columnIndex = 0;
  2038. var columnLength = 0;
  2039. series.forEach(function(eachSeries, seriesIndex) {
  2040. if (eachSeries.type == 'column') {
  2041. columnLength += 1;
  2042. }
  2043. });
  2044. context.save();
  2045. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2046. context.translate(opts._scrollDistance_, 0);
  2047. }
  2048. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  2049. drawToolTipSplitLine(opts.tooltip.offset.x, opts, config, context);
  2050. }
  2051. series.forEach(function(eachSeries, seriesIndex) {
  2052. var data = eachSeries.data;
  2053. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2054. calPoints.push(points);
  2055. // 绘制柱状数据图
  2056. if (eachSeries.type == 'column') {
  2057. points = fixColumeData(points, eachSpacing, columnLength, columnIndex, config, opts);
  2058. points.forEach(function(item, index) {
  2059. if (item !== null) {
  2060. context.beginPath();
  2061. context.setFillStyle(item.color || eachSeries.color);
  2062. var startX = item.x - item.width / 2 + 1;
  2063. var height = opts.height - item.y - config.padding - config.xAxisHeight - config.legendHeight;
  2064. context.moveTo(startX, item.y);
  2065. context.rect(startX, item.y, item.width - 2, height);
  2066. context.closePath();
  2067. context.fill();
  2068. }
  2069. });
  2070. columnIndex += 1;
  2071. }
  2072. //绘制区域图数据
  2073. if (eachSeries.type == 'area') {
  2074. let splitPointList = splitPoints(points);
  2075. for (let i = 0; i < splitPointList.length; i++) {
  2076. let points = splitPointList[i];
  2077. // 绘制区域数据
  2078. context.beginPath();
  2079. context.setStrokeStyle(eachSeries.color);
  2080. context.setFillStyle(eachSeries.color);
  2081. context.setGlobalAlpha(0.2);
  2082. context.setLineWidth(2 * opts.pixelRatio);
  2083. if (points.length > 1) {
  2084. var firstPoint = points[0];
  2085. let lastPoint = points[points.length - 1];
  2086. context.moveTo(firstPoint.x, firstPoint.y);
  2087. if (eachSeries.style === 'curve') {
  2088. points.forEach(function(item, index) {
  2089. if (index > 0) {
  2090. var ctrlPoint = createCurveControlPoints(points, index - 1);
  2091. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x,
  2092. item.y);
  2093. }
  2094. });
  2095. } else {
  2096. points.forEach(function(item, index) {
  2097. if (index > 0) {
  2098. context.lineTo(item.x, item.y);
  2099. }
  2100. });
  2101. }
  2102. context.lineTo(lastPoint.x, endY);
  2103. context.lineTo(firstPoint.x, endY);
  2104. context.lineTo(firstPoint.x, firstPoint.y);
  2105. } else {
  2106. let item = points[0];
  2107. context.moveTo(item.x - eachSpacing / 2, item.y);
  2108. context.lineTo(item.x + eachSpacing / 2, item.y);
  2109. context.lineTo(item.x + eachSpacing / 2, endY);
  2110. context.lineTo(item.x - eachSpacing / 2, endY);
  2111. context.moveTo(item.x - eachSpacing / 2, item.y);
  2112. }
  2113. context.closePath();
  2114. context.fill();
  2115. context.setGlobalAlpha(1);
  2116. }
  2117. }
  2118. // 绘制折线数据图
  2119. if (eachSeries.type == 'line') {
  2120. var splitPointList = splitPoints(points);
  2121. splitPointList.forEach(function(points, index) {
  2122. context.beginPath();
  2123. context.setStrokeStyle(eachSeries.color);
  2124. context.setLineWidth(2 * opts.pixelRatio);
  2125. if (points.length === 1) {
  2126. context.moveTo(points[0].x, points[0].y);
  2127. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  2128. } else {
  2129. context.moveTo(points[0].x, points[0].y);
  2130. if (eachSeries.style == 'curve') {
  2131. points.forEach(function(item, index) {
  2132. if (index > 0) {
  2133. var ctrlPoint = createCurveControlPoints(points, index - 1);
  2134. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x,
  2135. item.y);
  2136. }
  2137. });
  2138. } else {
  2139. points.forEach(function(item, index) {
  2140. if (index > 0) {
  2141. context.lineTo(item.x, item.y);
  2142. }
  2143. });
  2144. }
  2145. context.moveTo(points[0].x, points[0].y);
  2146. }
  2147. context.closePath();
  2148. context.stroke();
  2149. });
  2150. }
  2151. // 绘制点数据图
  2152. if (eachSeries.type == 'point') {
  2153. var splitPointList = splitPoints(points);
  2154. splitPointList.forEach(function(points, index) {
  2155. context.beginPath();
  2156. context.setStrokeStyle(eachSeries.color);
  2157. context.setLineWidth(2 * opts.pixelRatio);
  2158. context.moveTo(points[0].x, points[0].y);
  2159. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  2160. context.closePath();
  2161. context.stroke();
  2162. });
  2163. }
  2164. if (opts.dataPointShape !== false && eachSeries.type !== 'column') {
  2165. var shape = config.dataPointShape[seriesIndex % config.dataPointShape.length];
  2166. drawPointShape(points, eachSeries.color, shape, context, opts);
  2167. }
  2168. });
  2169. if (opts.dataLabel !== false && process === 1) {
  2170. var columnIndex = 0;
  2171. series.forEach(function(eachSeries, seriesIndex) {
  2172. var data = eachSeries.data;
  2173. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  2174. if (eachSeries.type !== 'column') {
  2175. drawPointText(points, eachSeries, config, context);
  2176. } else {
  2177. points = fixColumeData(points, eachSpacing, columnLength, columnIndex, config, opts);
  2178. drawPointText(points, eachSeries, config, context);
  2179. columnIndex += 1;
  2180. }
  2181. });
  2182. }
  2183. context.restore();
  2184. return {
  2185. xAxisPoints: xAxisPoints,
  2186. calPoints: calPoints,
  2187. eachSpacing: eachSpacing
  2188. };
  2189. }
  2190. function drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints) {
  2191. var toolTipOption = opts.extra.tooltip || {};
  2192. if (toolTipOption.horizentalLine && opts.tooltip && process === 1 && (opts.type == 'line' || opts.type == 'area' ||
  2193. opts.type == 'column' || opts.type == 'candle' || opts.type == 'mix')) {
  2194. drawToolTipHorizentalLine(opts, config, context, eachSpacing, xAxisPoints)
  2195. }
  2196. context.save();
  2197. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  2198. context.translate(opts._scrollDistance_, 0);
  2199. }
  2200. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  2201. drawToolTip(opts.tooltip.textList, opts.tooltip.offset, opts, config, context, eachSpacing, xAxisPoints);
  2202. }
  2203. context.restore();
  2204. }
  2205. function drawXAxis(categories, opts, config, context) {
  2206. var _getXAxisPoints4 = getXAxisPoints(categories, opts, config),
  2207. xAxisPoints = _getXAxisPoints4.xAxisPoints,
  2208. startX = _getXAxisPoints4.startX,
  2209. endX = _getXAxisPoints4.endX,
  2210. eachSpacing = _getXAxisPoints4.eachSpacing;
  2211. var startY = opts.height - config.padding - config.xAxisHeight - config.legendHeight;
  2212. var endY = config.padding;
  2213. //绘制滚动条
  2214. if (opts.enableScroll && opts.xAxis.scrollShow) {
  2215. var scrollY = opts.height - config.padding - config.legendHeight + 6 * opts.pixelRatio;
  2216. var scrollScreenWidth = endX - startX;
  2217. var scrollTotalWidth = eachSpacing * (xAxisPoints.length - 1);
  2218. var scrollWidth = scrollScreenWidth * scrollScreenWidth / scrollTotalWidth;
  2219. var scrollLeft = 0;
  2220. if (opts._scrollDistance_) {
  2221. scrollLeft = -opts._scrollDistance_ * (scrollScreenWidth) / scrollTotalWidth;
  2222. }
  2223. context.beginPath();
  2224. context.setLineCap('round');
  2225. context.setLineWidth(6 * opts.pixelRatio);
  2226. context.setStrokeStyle(opts.xAxis.scrollBackgroundColor || "#EFEBEF");
  2227. context.moveTo(startX, scrollY);
  2228. context.lineTo(endX, scrollY);
  2229. context.stroke();
  2230. context.closePath();
  2231. context.beginPath();
  2232. context.setLineCap('round');
  2233. context.setLineWidth(6 * opts.pixelRatio);
  2234. context.setStrokeStyle(opts.xAxis.scrollColor || "#A6A6A6");
  2235. context.moveTo(startX + scrollLeft, scrollY);
  2236. context.lineTo(startX + scrollLeft + scrollWidth, scrollY);
  2237. context.stroke();
  2238. context.setLineCap('butt');
  2239. context.closePath();
  2240. }
  2241. context.save();
  2242. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0) {
  2243. context.translate(opts._scrollDistance_, 0);
  2244. }
  2245. context.beginPath();
  2246. context.setStrokeStyle(opts.xAxis.gridColor || "#cccccc");
  2247. context.setLineCap('butt');
  2248. context.setLineWidth(1 * opts.pixelRatio);
  2249. if (opts.xAxis.gridType == 'dash') {
  2250. context.setLineDash([opts.xAxis.dashLength, opts.xAxis.dashLength]);
  2251. }
  2252. if (opts.xAxis.disableGrid !== true) {
  2253. if (opts.xAxis.type === 'calibration') {
  2254. xAxisPoints.forEach(function(item, index) {
  2255. if (index > 0) {
  2256. context.moveTo(item - eachSpacing / 2, startY);
  2257. context.lineTo(item - eachSpacing / 2, startY + 4 * opts.pixelRatio);
  2258. }
  2259. });
  2260. } else {
  2261. xAxisPoints.forEach(function(item, index) {
  2262. context.moveTo(item, startY);
  2263. context.lineTo(item, endY);
  2264. });
  2265. }
  2266. }
  2267. context.closePath();
  2268. context.stroke();
  2269. context.setLineDash([]);
  2270. //不绘制X轴
  2271. if (opts.xAxis.disabled !== true) {
  2272. // 对X轴列表做抽稀处理
  2273. let validWidth = opts.width - 2 * config.padding - config.yAxisWidth - config.yAxisTitleWidth;
  2274. //默认全部显示X轴标签
  2275. let maxXAxisListLength = categories.length;
  2276. //如果不旋转X轴文案
  2277. if (config._xAxisTextAngle_ === 0) {
  2278. //如果设置了X轴单屏数量
  2279. if (opts.xAxis.labelCount) {
  2280. //如果设置X轴密度
  2281. if (opts.xAxis.itemCount) {
  2282. maxXAxisListLength = Math.ceil(categories.length / opts.xAxis.itemCount * opts.xAxis.labelCount);
  2283. } else {
  2284. maxXAxisListLength = opts.xAxis.labelCount;
  2285. }
  2286. maxXAxisListLength -= 1;
  2287. }
  2288. } else {
  2289. //旋转标签文案
  2290. maxXAxisListLength = Math.min(categories.length, Math.ceil(validWidth / config.fontSize / 1.5));
  2291. }
  2292. let ratio = Math.ceil(categories.length / maxXAxisListLength);
  2293. let newCategories = [];
  2294. let cgLength = categories.length;
  2295. for (let i = 0; i < cgLength; i++) {
  2296. if (i % ratio !== 0) {
  2297. newCategories.push("");
  2298. } else {
  2299. newCategories.push(categories[i]);
  2300. }
  2301. }
  2302. newCategories[cgLength - 1] = categories[cgLength - 1];
  2303. /*
  2304. categories = categories.map(function (item, index) {
  2305. return index % ratio !== 0 ? '' : item;
  2306. });*/
  2307. if (config._xAxisTextAngle_ === 0) {
  2308. newCategories.forEach(function(item, index) {
  2309. var offset = eachSpacing / 2 - measureText(item) / 2;
  2310. context.beginPath();
  2311. context.setFontSize(config.fontSize);
  2312. context.setFillStyle(opts.xAxis.fontColor || '#666666');
  2313. context.fillText(item, xAxisPoints[index] + offset, startY + config.fontSize + 5);
  2314. context.closePath();
  2315. context.stroke();
  2316. });
  2317. } else {
  2318. newCategories.forEach(function(item, index) {
  2319. context.save();
  2320. context.beginPath();
  2321. context.setFontSize(config.fontSize);
  2322. context.setFillStyle(opts.xAxis.fontColor || '#666666');
  2323. var textWidth = measureText(item);
  2324. var offset = eachSpacing / 2 - textWidth;
  2325. var _calRotateTranslate = calRotateTranslate(xAxisPoints[index] + eachSpacing / 2, startY + config.fontSize / 2 +
  2326. 5, opts.height),
  2327. transX = _calRotateTranslate.transX,
  2328. transY = _calRotateTranslate.transY;
  2329. context.rotate(-1 * config._xAxisTextAngle_);
  2330. context.translate(transX, transY);
  2331. context.fillText(item, xAxisPoints[index] + offset, startY + config.fontSize + 5);
  2332. context.closePath();
  2333. context.stroke();
  2334. context.restore();
  2335. });
  2336. }
  2337. }
  2338. context.restore();
  2339. }
  2340. function drawYAxisGrid(categories, opts, config, context) {
  2341. if (opts.yAxis.disableGrid === true) {
  2342. return;
  2343. }
  2344. var spacingValid = opts.height - 2 * config.padding - config.xAxisHeight - config.legendHeight;
  2345. var eachSpacing = Math.floor(spacingValid / config.yAxisSplit);
  2346. var yAxisTotalWidth = config.yAxisWidth + config.yAxisTitleWidth;
  2347. var startX = config.padding + yAxisTotalWidth;
  2348. var _getXAxisPoints4 = getXAxisPoints(categories, opts, config),
  2349. xAxisPoints = _getXAxisPoints4.xAxisPoints,
  2350. xAxiseachSpacing = _getXAxisPoints4.eachSpacing;
  2351. var TotalWidth = xAxiseachSpacing * (xAxisPoints.length - 1);
  2352. var endX = startX + TotalWidth;
  2353. var points = [];
  2354. for (var i = 0; i < config.yAxisSplit; i++) {
  2355. points.push(config.padding + eachSpacing * i);
  2356. }
  2357. points.push(config.padding + eachSpacing * config.yAxisSplit + 2);
  2358. context.save();
  2359. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0) {
  2360. context.translate(opts._scrollDistance_, 0);
  2361. }
  2362. if (opts.yAxis.gridType == 'dash') {
  2363. context.setLineDash([opts.yAxis.dashLength, opts.yAxis.dashLength]);
  2364. }
  2365. context.beginPath();
  2366. context.setStrokeStyle(opts.yAxis.gridColor || "#cccccc");
  2367. context.setLineWidth(1 * opts.pixelRatio);
  2368. points.forEach(function(item, index) {
  2369. context.moveTo(startX, item);
  2370. context.lineTo(endX, item);
  2371. });
  2372. context.closePath();
  2373. context.stroke();
  2374. context.setLineDash([]);
  2375. context.restore();
  2376. }
  2377. function drawYAxis(series, opts, config, context) {
  2378. if (opts.yAxis.disabled === true) {
  2379. return;
  2380. }
  2381. var _calYAxisData4 = calYAxisData(series, opts, config),
  2382. rangesFormat = _calYAxisData4.rangesFormat;
  2383. var yAxisTotalWidth = config.yAxisWidth + config.yAxisTitleWidth;
  2384. var spacingValid = opts.height - 2 * config.padding - config.xAxisHeight - config.legendHeight;
  2385. var eachSpacing = Math.floor(spacingValid / config.yAxisSplit);
  2386. var startX = config.padding + yAxisTotalWidth;
  2387. var endX = opts.width - config.padding;
  2388. var endY = opts.height - config.padding - config.xAxisHeight - config.legendHeight + config.xAxisTextPadding;
  2389. // set YAxis background
  2390. context.beginPath();
  2391. context.setFillStyle(opts.background || '#ffffff');
  2392. if (opts._scrollDistance_ < 0) {
  2393. context.fillRect(0, 0, startX, endY + config.xAxisHeight);
  2394. }
  2395. context.fillRect(endX, 0, opts.width, endY + config.xAxisHeight);
  2396. context.closePath();
  2397. context.stroke();
  2398. var points = [];
  2399. for (var i = 0; i <= config.yAxisSplit; i++) {
  2400. points.push(config.padding + eachSpacing * i);
  2401. }
  2402. rangesFormat.forEach(function(item, index) {
  2403. var pos = points[index] ? points[index] : endY;
  2404. context.beginPath();
  2405. context.setFontSize(config.fontSize);
  2406. context.setFillStyle(opts.yAxis.fontColor || '#666666');
  2407. context.fillText(item, config.padding + config.yAxisTitleWidth, pos + config.fontSize / 2);
  2408. context.closePath();
  2409. context.stroke();
  2410. });
  2411. if (opts.yAxis.title) {
  2412. drawYAxisTitle(opts.yAxis.title, opts, config, context);
  2413. }
  2414. }
  2415. function drawLegend(series, opts, config, context) {
  2416. if (opts.legend === false) {
  2417. return;
  2418. }
  2419. // each legend shape width 15px
  2420. // the spacing between shape and text in each legend is the `padding`
  2421. // each legend spacing is the `padding`
  2422. // legend margin top `config.padding`
  2423. var _calLegendData = calLegendData(series, opts, config),
  2424. legendList = _calLegendData.legendList;
  2425. var padding = 5 * opts.pixelRatio;
  2426. var marginTop = 10 * opts.pixelRatio;
  2427. var shapeWidth = 15 * opts.pixelRatio;
  2428. legendList.forEach(function(itemList, listIndex) {
  2429. var width = 0;
  2430. for (let i = 0; i < itemList.length; i++) {
  2431. let item = itemList[i];
  2432. item.name = item.name || 'undefined';
  2433. width += 3 * padding + measureText(item.name) + shapeWidth;
  2434. }
  2435. var startX = (opts.width - width) / 2 + padding;
  2436. var startY = opts.height - config.padding - config.legendHeight + listIndex * (config.fontSize + marginTop) +
  2437. padding + marginTop;
  2438. context.setFontSize(config.fontSize);
  2439. for (let i = 0; i < itemList.length; i++) {
  2440. let item = itemList[i];
  2441. switch (opts.type) {
  2442. case 'line':
  2443. context.beginPath();
  2444. context.setLineWidth(1 * opts.pixelRatio);
  2445. context.setStrokeStyle(item.color);
  2446. context.setFillStyle(item.color);
  2447. context.moveTo(startX + 7.5 * opts.pixelRatio, startY + 5 * opts.pixelRatio);
  2448. context.arc(startX + 7.5 * opts.pixelRatio, startY + 5 * opts.pixelRatio, 6 * opts.pixelRatio, 0, 2 * Math.PI);
  2449. context.closePath();
  2450. context.fill();
  2451. context.stroke();
  2452. break;
  2453. case 'pie':
  2454. context.beginPath();
  2455. context.setLineWidth(1 * opts.pixelRatio);
  2456. context.setStrokeStyle(item.color);
  2457. context.setFillStyle(item.color);
  2458. context.moveTo(startX + 7.5 * opts.pixelRatio, startY + 5 * opts.pixelRatio);
  2459. context.arc(startX + 7.5 * opts.pixelRatio, startY + 5 * opts.pixelRatio, 6 * opts.pixelRatio, 0, 2 * Math.PI);
  2460. context.closePath();
  2461. context.fill();
  2462. context.stroke();
  2463. break;
  2464. case 'ring':
  2465. case 'rose':
  2466. context.beginPath();
  2467. context.setLineWidth(1 * opts.pixelRatio);
  2468. context.setStrokeStyle(item.color);
  2469. context.setFillStyle(item.color);
  2470. context.moveTo(startX + 7.5 * opts.pixelRatio, startY + 5 * opts.pixelRatio);
  2471. context.arc(startX + 7.5 * opts.pixelRatio, startY + 5 * opts.pixelRatio, 6 * opts.pixelRatio, 0, 2 * Math.PI);
  2472. context.closePath();
  2473. context.fill();
  2474. context.stroke();
  2475. break;
  2476. //圆弧进度图不显示图例
  2477. case 'gauge':
  2478. break;
  2479. case 'arcbar':
  2480. break;
  2481. default:
  2482. context.beginPath();
  2483. context.setLineWidth(1 * opts.pixelRatio);
  2484. context.setStrokeStyle(item.color);
  2485. context.setFillStyle(item.color);
  2486. context.moveTo(startX, startY);
  2487. context.fillRect(startX, startY, 15 * opts.pixelRatio, 10 * opts.pixelRatio);
  2488. context.closePath();
  2489. context.fill();
  2490. context.stroke();
  2491. }
  2492. startX += padding + shapeWidth;
  2493. context.beginPath();
  2494. context.setFontSize(config.fontSize);
  2495. context.setFillStyle(opts.extra.legendTextColor || '#333333');
  2496. context.fillText(item.name, startX, startY + 6 * opts.pixelRatio + 3 * opts.pixelRatio);
  2497. context.closePath();
  2498. context.stroke();
  2499. startX += measureText(item.name) + 2 * padding;
  2500. }
  2501. });
  2502. }
  2503. function drawPieDataPoints(series, opts, config, context) {
  2504. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2505. var pieOption = opts.extra.pie || {};
  2506. var centerPosition = {
  2507. x: opts.width / 2,
  2508. y: (opts.height - config.legendHeight) / 2
  2509. };
  2510. var radius = Math.min(centerPosition.x - config.pieChartLinePadding - config.pieChartTextPadding - config._pieTextMaxLength_,
  2511. centerPosition.y - config.pieChartLinePadding - config.pieChartTextPadding);
  2512. if (opts.dataLabel) {
  2513. radius -= 10;
  2514. } else {
  2515. radius -= 2 * config.padding;
  2516. }
  2517. series = getPieDataPoints(series, radius, process);
  2518. var activeRadius = config.pieChartLinePadding / 2;
  2519. series = series.map(function(eachSeries) {
  2520. eachSeries._start_ += (pieOption.offsetAngle || 0) * Math.PI / 180;
  2521. return eachSeries;
  2522. });
  2523. series.forEach(function(eachSeries, seriesIndex) {
  2524. if (opts.tooltip) {
  2525. if (opts.tooltip.index == seriesIndex) {
  2526. context.beginPath();
  2527. context.setFillStyle(hexToRgb(eachSeries.color, opts.extra.pie.activeOpacity || 0.5));
  2528. context.moveTo(centerPosition.x, centerPosition.y);
  2529. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_+activeRadius, eachSeries._start_, eachSeries._start_ + 2 *
  2530. eachSeries._proportion_ * Math.PI);
  2531. context.closePath();
  2532. context.fill();
  2533. }
  2534. }
  2535. context.beginPath();
  2536. context.setLineWidth(2 * opts.pixelRatio);
  2537. context.lineJoin = "round";
  2538. context.setStrokeStyle('#ffffff');
  2539. context.setFillStyle(eachSeries.color);
  2540. context.moveTo(centerPosition.x, centerPosition.y);
  2541. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_, eachSeries._start_, eachSeries._start_ + 2 * eachSeries._proportion_ *
  2542. Math.PI);
  2543. context.closePath();
  2544. context.fill();
  2545. if (opts.disablePieStroke !== true) {
  2546. context.stroke();
  2547. }
  2548. });
  2549. if (opts.type === 'ring') {
  2550. var innerPieWidth = radius * 0.6;
  2551. if (typeof opts.extra.pie.ringWidth === 'number' && opts.extra.pie.ringWidth > 0) {
  2552. innerPieWidth = Math.max(0, radius - opts.extra.pie.ringWidth);
  2553. }
  2554. context.beginPath();
  2555. context.setFillStyle(opts.background || '#ffffff');
  2556. context.moveTo(centerPosition.x, centerPosition.y);
  2557. context.arc(centerPosition.x, centerPosition.y, innerPieWidth, 0, 2 * Math.PI);
  2558. context.closePath();
  2559. context.fill();
  2560. }
  2561. if (opts.dataLabel !== false && process === 1) {
  2562. // fix https://github.com/xiaolin3303/wx-charts/issues/132
  2563. var valid = false;
  2564. for (var i = 0, len = series.length; i < len; i++) {
  2565. if (series[i].data > 0) {
  2566. valid = true;
  2567. break;
  2568. }
  2569. }
  2570. if (valid) {
  2571. drawPieText(series, opts, config, context, radius, centerPosition);
  2572. }
  2573. }
  2574. if (process === 1 && opts.type === 'ring') {
  2575. drawRingTitle(opts, config, context);
  2576. }
  2577. return {
  2578. center: centerPosition,
  2579. radius: radius,
  2580. series: series
  2581. };
  2582. }
  2583. function drawRoseDataPoints(series, opts, config, context) {
  2584. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2585. var roseOption = opts.extra.rose || {};
  2586. roseOption.type = roseOption.type || 'area';
  2587. var centerPosition = {
  2588. x: opts.width / 2,
  2589. y: (opts.height - config.legendHeight) / 2
  2590. };
  2591. var radius = Math.min(centerPosition.x - config.pieChartLinePadding - config.pieChartTextPadding - config._pieTextMaxLength_,
  2592. centerPosition.y - config.pieChartLinePadding - config.pieChartTextPadding);
  2593. if (opts.dataLabel) {
  2594. radius -= 10;
  2595. } else {
  2596. radius -= 2 * config.padding;
  2597. }
  2598. var minRadius = roseOption.minRadius || radius*0.5;
  2599. series = getRoseDataPoints(series, roseOption.type, minRadius, radius, process);
  2600. var activeRadius = config.pieChartLinePadding / 2;
  2601. series = series.map(function(eachSeries) {
  2602. eachSeries._start_ += (roseOption.offsetAngle || 0) * Math.PI / 180;
  2603. return eachSeries;
  2604. });
  2605. series.forEach(function(eachSeries, seriesIndex) {
  2606. if (opts.tooltip) {
  2607. if (opts.tooltip.index == seriesIndex) {
  2608. context.beginPath();
  2609. context.setFillStyle(hexToRgb(eachSeries.color, roseOption.activeOpacity || 0.5));
  2610. context.moveTo(centerPosition.x, centerPosition.y);
  2611. context.arc(centerPosition.x, centerPosition.y, activeRadius+eachSeries._radius_, eachSeries._start_, eachSeries._start_ + 2 * eachSeries._proportion_ * Math.PI);
  2612. context.closePath();
  2613. context.fill();
  2614. }
  2615. }
  2616. context.beginPath();
  2617. context.setLineWidth(2 * opts.pixelRatio);
  2618. context.lineJoin = "round";
  2619. context.setStrokeStyle('#ffffff');
  2620. context.setFillStyle(eachSeries.color);
  2621. context.moveTo(centerPosition.x, centerPosition.y);
  2622. context.arc(centerPosition.x, centerPosition.y, eachSeries._radius_ , eachSeries._start_, eachSeries._start_ + 2 * eachSeries._proportion_ *Math.PI);
  2623. context.closePath();
  2624. context.fill();
  2625. if (opts.disablePieStroke !== true) {
  2626. context.stroke();
  2627. }
  2628. });
  2629. if (opts.dataLabel !== false && process === 1) {
  2630. // fix https://github.com/xiaolin3303/wx-charts/issues/132
  2631. var valid = false;
  2632. for (var i = 0, len = series.length; i < len; i++) {
  2633. if (series[i].data > 0) {
  2634. valid = true;
  2635. break;
  2636. }
  2637. }
  2638. if (valid) {
  2639. drawPieText(series, opts, config, context, radius, centerPosition);
  2640. }
  2641. }
  2642. return {
  2643. center: centerPosition,
  2644. radius: radius,
  2645. series: series
  2646. };
  2647. }
  2648. function drawArcbarDataPoints(series, opts, config, context) {
  2649. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2650. var arcbarOption = opts.extra.arcbar || {};
  2651. arcbarOption.startAngle = arcbarOption.startAngle ? arcbarOption.startAngle : 0.75;
  2652. arcbarOption.endAngle = arcbarOption.endAngle ? arcbarOption.endAngle : 0.25;
  2653. arcbarOption.type = arcbarOption.type ? arcbarOption.type : 'default';
  2654. series = getArcbarDataPoints(series, arcbarOption, process);
  2655. var centerPosition = {
  2656. x: opts.width / 2,
  2657. y: (opts.height) / 2
  2658. };
  2659. var radius = Math.min(centerPosition.x, centerPosition.y);
  2660. if (typeof arcbarOption.width === 'number' && arcbarOption.width > 0) {
  2661. arcbarOption.width = arcbarOption.width;
  2662. } else {
  2663. arcbarOption.width = 12 * opts.pixelRatio;
  2664. }
  2665. radius -= config.padding + arcbarOption.width / 2;
  2666. //背景颜色
  2667. context.setLineWidth(arcbarOption.width); // 设置圆环的宽度
  2668. context.setStrokeStyle(arcbarOption.backgroundColor || '#E9E9E9'); // 设置圆环的颜色
  2669. context.setLineCap('round'); // 设置圆环端点的形状
  2670. context.beginPath(); //开始一个新的路径
  2671. if (arcbarOption.type == 'default') {
  2672. context.arc(centerPosition.x, centerPosition.y, radius, arcbarOption.startAngle * Math.PI, arcbarOption.endAngle *
  2673. Math.PI, false);
  2674. } else {
  2675. context.arc(centerPosition.x, centerPosition.y, radius, 0, 2 * Math.PI, false);
  2676. }
  2677. context.stroke(); //对当前路径进行描边
  2678. for (let i = 0; i < series.length; i++) {
  2679. let eachSeries = series[i];
  2680. context.setLineWidth(arcbarOption.width);
  2681. context.setStrokeStyle(eachSeries.color);
  2682. context.setLineCap('round');
  2683. context.beginPath();
  2684. context.arc(centerPosition.x, centerPosition.y, radius, arcbarOption.startAngle * Math.PI, eachSeries._proportion_ *
  2685. Math.PI, false);
  2686. context.stroke();
  2687. }
  2688. drawRingTitle(opts, config, context);
  2689. return {
  2690. center: centerPosition,
  2691. radius: radius,
  2692. series: series
  2693. };
  2694. }
  2695. function drawGaugeDataPoints(categories, series, opts, config, context) {
  2696. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  2697. var gaugeOption = opts.extra.gauge || {};
  2698. gaugeOption.startAngle = gaugeOption.startAngle ? gaugeOption.startAngle : 0.75;
  2699. if (gaugeOption.oldAngle == undefined) {
  2700. gaugeOption.oldAngle = gaugeOption.startAngle;
  2701. }
  2702. if (gaugeOption.oldData == undefined) {
  2703. gaugeOption.oldData = 0;
  2704. }
  2705. gaugeOption.endAngle = gaugeOption.endAngle ? gaugeOption.endAngle : 0.25;
  2706. categories = getGaugeAxisPoints(categories, gaugeOption.startAngle, gaugeOption.endAngle);
  2707. var centerPosition = {
  2708. x: opts.width / 2,
  2709. y: (opts.height) / 2
  2710. };
  2711. var radius = Math.min(centerPosition.x, centerPosition.y);
  2712. if (typeof gaugeOption.width === 'number' && gaugeOption.width > 0) {
  2713. gaugeOption.width = gaugeOption.width;
  2714. } else {
  2715. gaugeOption.width = 15 * opts.pixelRatio;
  2716. }
  2717. radius -= config.padding + gaugeOption.width / 2;
  2718. var innerRadius = radius - gaugeOption.width;
  2719. //画背景
  2720. context.setLineWidth(gaugeOption.width);
  2721. context.setLineCap('butt');
  2722. for (let i = 0; i < categories.length; i++) {
  2723. let eachCategories = categories[i];
  2724. context.beginPath();
  2725. context.setStrokeStyle(eachCategories.color);
  2726. context.arc(centerPosition.x, centerPosition.y, radius, eachCategories._startAngle_ * Math.PI, eachCategories._endAngle_ *
  2727. Math.PI, false);
  2728. context.stroke();
  2729. }
  2730. context.save();
  2731. //画刻度线
  2732. let totalAngle = gaugeOption.startAngle - gaugeOption.endAngle + 1;
  2733. gaugeOption.splitLine.fixRadius = gaugeOption.splitLine.fixRadius ? gaugeOption.splitLine.fixRadius : 0;
  2734. gaugeOption.splitLine.splitNumber = gaugeOption.splitLine.splitNumber ? gaugeOption.splitLine.splitNumber : 10;
  2735. gaugeOption.splitLine.width = gaugeOption.splitLine.width ? gaugeOption.splitLine.width : 15 * opts.pixelRatio;
  2736. gaugeOption.splitLine.color = gaugeOption.splitLine.color ? gaugeOption.splitLine.color : '#FFFFFF';
  2737. gaugeOption.splitLine.childNumber = gaugeOption.splitLine.childNumber ? gaugeOption.splitLine.childNumber : 5;
  2738. gaugeOption.splitLine.childWidth = gaugeOption.splitLine.childWidth ? gaugeOption.splitLine.childWidth : 5 * opts.pixelRatio;
  2739. let splitAngle = totalAngle / gaugeOption.splitLine.splitNumber;
  2740. let childAngle = totalAngle / gaugeOption.splitLine.splitNumber / gaugeOption.splitLine.childNumber;
  2741. let startX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius;
  2742. let endX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.width;
  2743. let childendX = -radius - gaugeOption.width * 0.5 - gaugeOption.splitLine.fixRadius + gaugeOption.splitLine.childWidth;
  2744. context.translate(centerPosition.x, centerPosition.y);
  2745. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  2746. for (let i = 0; i < gaugeOption.splitLine.splitNumber + 1; i++) {
  2747. context.beginPath();
  2748. context.setStrokeStyle(gaugeOption.splitLine.color);
  2749. context.setLineWidth(2 * opts.pixelRatio);
  2750. context.moveTo(startX, 0);
  2751. context.lineTo(endX, 0);
  2752. context.stroke();
  2753. context.rotate(splitAngle * Math.PI);
  2754. }
  2755. context.restore();
  2756. context.save();
  2757. context.translate(centerPosition.x, centerPosition.y);
  2758. context.rotate((gaugeOption.startAngle - 1) * Math.PI);
  2759. for (let i = 0; i < gaugeOption.splitLine.splitNumber * gaugeOption.splitLine.childNumber + 1; i++) {
  2760. context.beginPath();
  2761. context.setStrokeStyle(gaugeOption.splitLine.color);
  2762. context.setLineWidth(1 * opts.pixelRatio);
  2763. context.moveTo(startX, 0);
  2764. context.lineTo(childendX, 0);
  2765. context.stroke();
  2766. context.rotate(childAngle * Math.PI);
  2767. }
  2768. context.restore();
  2769. //画指针
  2770. gaugeOption.pointer.width = gaugeOption.pointer.width ? gaugeOption.pointer.width : 15 * opts.pixelRatio;
  2771. if (gaugeOption.pointer.color == undefined || gaugeOption.pointer.color == 'auto') {
  2772. gaugeOption.pointer.color == 'auto';
  2773. } else {
  2774. gaugeOption.pointer.color == gaugeOption.pointer.color;
  2775. }
  2776. series = getGaugeDataPoints(series, categories, gaugeOption, process);
  2777. for (let i = 0; i < series.length; i++) {
  2778. let eachSeries = series[i];
  2779. context.save();
  2780. context.translate(centerPosition.x, centerPosition.y);
  2781. context.rotate((eachSeries._proportion_ - 1) * Math.PI);
  2782. context.beginPath();
  2783. context.setFillStyle(eachSeries.color);
  2784. context.moveTo(gaugeOption.pointer.width, 0);
  2785. context.lineTo(0, -gaugeOption.pointer.width / 2);
  2786. context.lineTo(-innerRadius, 0);
  2787. context.lineTo(0, gaugeOption.pointer.width / 2);
  2788. context.lineTo(gaugeOption.pointer.width, 0);
  2789. context.closePath();
  2790. context.fill();
  2791. context.beginPath();
  2792. context.setFillStyle('#FFFFFF');
  2793. context.arc(0, 0, gaugeOption.pointer.width / 6, 0, 2 * Math.PI, false);
  2794. context.fill();
  2795. context.restore();
  2796. }
  2797. if (opts.dataLabel !== false) {
  2798. drawGaugeLabel(gaugeOption, radius, centerPosition, opts, config, context);
  2799. }
  2800. drawRingTitle(opts, config, context);
  2801. if (process === 1 && opts.type === 'gauge') {
  2802. gaugeOption.oldAngle = series[0]._proportion_;
  2803. gaugeOption.oldData = series[0].data;
  2804. }
  2805. return {
  2806. center: centerPosition,
  2807. radius: radius,
  2808. innerRadius: innerRadius,
  2809. categories: categories,
  2810. totalAngle: totalAngle
  2811. };
  2812. }
  2813. function drawRadarDataPoints(series, opts, config, context) {
  2814. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  2815. var radarOption = opts.extra.radar || {};
  2816. var coordinateAngle = getRadarCoordinateSeries(opts.categories.length);
  2817. var centerPosition = {
  2818. x: opts.width / 2,
  2819. y: (opts.height - config.legendHeight) / 2
  2820. };
  2821. var radius = Math.min(centerPosition.x - (getMaxTextListLength(opts.categories) + config.radarLabelTextMargin),
  2822. centerPosition.y - config.radarLabelTextMargin);
  2823. radius -= config.padding;
  2824. // draw grid
  2825. context.beginPath();
  2826. context.setLineWidth(1 * opts.pixelRatio);
  2827. context.setStrokeStyle(radarOption.gridColor || "#cccccc");
  2828. coordinateAngle.forEach(function (angle) {
  2829. var pos = convertCoordinateOrigin(radius * Math.cos(angle), radius * Math.sin(angle), centerPosition);
  2830. context.moveTo(centerPosition.x, centerPosition.y);
  2831. context.lineTo(pos.x, pos.y);
  2832. });
  2833. context.stroke();
  2834. context.closePath();
  2835. // draw split line grid
  2836. var _loop = function _loop(i) {
  2837. var startPos = {};
  2838. context.beginPath();
  2839. context.setLineWidth(1 * opts.pixelRatio);
  2840. context.setStrokeStyle(radarOption.gridColor || "#cccccc");
  2841. coordinateAngle.forEach(function (angle, index) {
  2842. var pos = convertCoordinateOrigin(radius / config.radarGridCount * i * Math.cos(angle), radius / config.radarGridCount * i * Math.sin(angle), centerPosition);
  2843. if (index === 0) {
  2844. startPos = pos;
  2845. context.moveTo(pos.x, pos.y);
  2846. } else {
  2847. context.lineTo(pos.x, pos.y);
  2848. }
  2849. });
  2850. context.lineTo(startPos.x, startPos.y);
  2851. context.stroke();
  2852. context.closePath();
  2853. };
  2854. for (var i = 1; i <= config.radarGridCount; i++) {
  2855. _loop(i);
  2856. }
  2857. var radarDataPoints = getRadarDataPoints(coordinateAngle, centerPosition, radius, series, opts, process);
  2858. radarDataPoints.forEach(function(eachSeries, seriesIndex) {
  2859. // 绘制区域数据
  2860. context.beginPath();
  2861. context.setFillStyle(eachSeries.color);
  2862. context.setGlobalAlpha(0.3);
  2863. eachSeries.data.forEach(function(item, index) {
  2864. if (index === 0) {
  2865. context.moveTo(item.position.x, item.position.y);
  2866. } else {
  2867. context.lineTo(item.position.x, item.position.y);
  2868. }
  2869. });
  2870. context.closePath();
  2871. context.fill();
  2872. context.setGlobalAlpha(1);
  2873. if (opts.dataPointShape !== false) {
  2874. var shape = config.dataPointShape[seriesIndex % config.dataPointShape.length];
  2875. var points = eachSeries.data.map(function(item) {
  2876. return item.position;
  2877. });
  2878. drawPointShape(points, eachSeries.color, shape, context, opts);
  2879. }
  2880. });
  2881. // draw label text
  2882. drawRadarLabel(coordinateAngle, radius, centerPosition, opts, config, context);
  2883. return {
  2884. center: centerPosition,
  2885. radius: radius,
  2886. angleList: coordinateAngle
  2887. };
  2888. }
  2889. function drawCanvas(opts, context) {
  2890. context.draw();
  2891. }
  2892. var Timing = {
  2893. easeIn: function easeIn(pos) {
  2894. return Math.pow(pos, 3);
  2895. },
  2896. easeOut: function easeOut(pos) {
  2897. return Math.pow(pos - 1, 3) + 1;
  2898. },
  2899. easeInOut: function easeInOut(pos) {
  2900. if ((pos /= 0.5) < 1) {
  2901. return 0.5 * Math.pow(pos, 3);
  2902. } else {
  2903. return 0.5 * (Math.pow(pos - 2, 3) + 2);
  2904. }
  2905. },
  2906. linear: function linear(pos) {
  2907. return pos;
  2908. }
  2909. };
  2910. function Animation(opts) {
  2911. this.isStop = false;
  2912. opts.duration = typeof opts.duration === 'undefined' ? 1000 : opts.duration;
  2913. opts.timing = opts.timing || 'linear';
  2914. var delay = 17;
  2915. var createAnimationFrame = function createAnimationFrame() {
  2916. if (typeof requestAnimationFrame !== 'undefined') {
  2917. return requestAnimationFrame;
  2918. } else if (typeof setTimeout !== 'undefined') {
  2919. return function(step, delay) {
  2920. setTimeout(function() {
  2921. var timeStamp = +new Date();
  2922. step(timeStamp);
  2923. }, delay);
  2924. };
  2925. } else {
  2926. return function(step) {
  2927. step(null);
  2928. };
  2929. }
  2930. };
  2931. var animationFrame = createAnimationFrame();
  2932. var startTimeStamp = null;
  2933. var _step = function step(timestamp) {
  2934. if (timestamp === null || this.isStop === true) {
  2935. opts.onProcess && opts.onProcess(1);
  2936. opts.onAnimationFinish && opts.onAnimationFinish();
  2937. return;
  2938. }
  2939. if (startTimeStamp === null) {
  2940. startTimeStamp = timestamp;
  2941. }
  2942. if (timestamp - startTimeStamp < opts.duration) {
  2943. var process = (timestamp - startTimeStamp) / opts.duration;
  2944. var timingFunction = Timing[opts.timing];
  2945. process = timingFunction(process);
  2946. opts.onProcess && opts.onProcess(process);
  2947. animationFrame(_step, delay);
  2948. } else {
  2949. opts.onProcess && opts.onProcess(1);
  2950. opts.onAnimationFinish && opts.onAnimationFinish();
  2951. }
  2952. };
  2953. _step = _step.bind(this);
  2954. animationFrame(_step, delay);
  2955. }
  2956. // stop animation immediately
  2957. // and tigger onAnimationFinish
  2958. Animation.prototype.stop = function() {
  2959. this.isStop = true;
  2960. };
  2961. function drawCharts(type, opts, config, context) {
  2962. var _this = this;
  2963. var series = opts.series;
  2964. var categories = opts.categories;
  2965. series = fillSeriesColor(series, config);
  2966. series = fillSeriesType(series, opts);
  2967. let seriesMA = null;
  2968. if (type == 'candle') {
  2969. let average = assign({}, opts.extra.candle.average);
  2970. if (average.show) {
  2971. seriesMA = calCandleMA(average.day, average.name, average.color, series[0].data);
  2972. opts.seriesMA = seriesMA;
  2973. }
  2974. }
  2975. var _calLegendData = calLegendData(series, opts, config),
  2976. legendHeight = _calLegendData.legendHeight;
  2977. config.legendHeight = legendHeight;
  2978. var _calYAxisData = calYAxisData(series, opts, config),
  2979. yAxisWidth = _calYAxisData.yAxisWidth;
  2980. config.yAxisWidth = yAxisWidth;
  2981. if (categories && categories.length) {
  2982. var _calCategoriesData = calCategoriesData(categories, opts, config),
  2983. xAxisHeight = _calCategoriesData.xAxisHeight,
  2984. angle = _calCategoriesData.angle;
  2985. config.xAxisHeight = xAxisHeight;
  2986. config._xAxisTextAngle_ = angle;
  2987. }
  2988. if (type === 'pie' || type === 'ring' || type === 'rose') {
  2989. config._pieTextMaxLength_ = opts.dataLabel === false ? 0 : getPieTextMaxLength(series);
  2990. }
  2991. var duration = opts.animation ? 1000 : 0;
  2992. this.animationInstance && this.animationInstance.stop();
  2993. //先清空画布,不然百度和支付宝ToolTip有重影
  2994. context.clearRect(0, 0, opts.width, opts.height);
  2995. switch (type) {
  2996. case 'line':
  2997. this.animationInstance = new Animation({
  2998. timing: 'easeIn',
  2999. duration: duration,
  3000. onProcess: function onProcess(process) {
  3001. if (opts.rotate) {
  3002. contextRotate(context, opts);
  3003. }
  3004. drawYAxisGrid(categories, opts, config, context);
  3005. drawXAxis(categories, opts, config, context);
  3006. var _drawLineDataPoints = drawLineDataPoints(series, opts, config, context, process),
  3007. xAxisPoints = _drawLineDataPoints.xAxisPoints,
  3008. calPoints = _drawLineDataPoints.calPoints,
  3009. eachSpacing = _drawLineDataPoints.eachSpacing;
  3010. _this.chartData.xAxisPoints = xAxisPoints;
  3011. _this.chartData.calPoints = calPoints;
  3012. _this.chartData.eachSpacing = eachSpacing;
  3013. drawLegend(opts.series, opts, config, context);
  3014. drawYAxis(series, opts, config, context);
  3015. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  3016. drawCanvas(opts, context);
  3017. },
  3018. onAnimationFinish: function onAnimationFinish() {
  3019. _this.event.trigger('renderComplete');
  3020. }
  3021. });
  3022. break;
  3023. case 'mix':
  3024. this.animationInstance = new Animation({
  3025. timing: 'easeIn',
  3026. duration: duration,
  3027. onProcess: function onProcess(process) {
  3028. if (opts.rotate) {
  3029. contextRotate(context, opts);
  3030. }
  3031. drawYAxisGrid(categories, opts, config, context);
  3032. drawXAxis(categories, opts, config, context);
  3033. var _drawMixDataPoints = drawMixDataPoints(series, opts, config, context, process),
  3034. xAxisPoints = _drawMixDataPoints.xAxisPoints,
  3035. calPoints = _drawMixDataPoints.calPoints,
  3036. eachSpacing = _drawMixDataPoints.eachSpacing;
  3037. _this.chartData.xAxisPoints = xAxisPoints;
  3038. _this.chartData.calPoints = calPoints;
  3039. _this.chartData.eachSpacing = eachSpacing;
  3040. drawLegend(opts.series, opts, config, context);
  3041. drawYAxis(series, opts, config, context);
  3042. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  3043. drawCanvas(opts, context);
  3044. },
  3045. onAnimationFinish: function onAnimationFinish() {
  3046. _this.event.trigger('renderComplete');
  3047. }
  3048. });
  3049. break;
  3050. case 'column':
  3051. this.animationInstance = new Animation({
  3052. timing: 'easeIn',
  3053. duration: duration,
  3054. onProcess: function onProcess(process) {
  3055. if (opts.rotate) {
  3056. contextRotate(context, opts);
  3057. }
  3058. drawYAxisGrid(categories, opts, config, context);
  3059. drawXAxis(categories, opts, config, context);
  3060. var _drawColumnDataPoints = drawColumnDataPoints(series, opts, config, context, process),
  3061. xAxisPoints = _drawColumnDataPoints.xAxisPoints,
  3062. calPoints = _drawColumnDataPoints.calPoints,
  3063. eachSpacing = _drawColumnDataPoints.eachSpacing;
  3064. _this.chartData.xAxisPoints = xAxisPoints;
  3065. _this.chartData.calPoints = calPoints;
  3066. _this.chartData.eachSpacing = eachSpacing;
  3067. drawLegend(opts.series, opts, config, context);
  3068. drawYAxis(series, opts, config, context);
  3069. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  3070. drawCanvas(opts, context);
  3071. },
  3072. onAnimationFinish: function onAnimationFinish() {
  3073. _this.event.trigger('renderComplete');
  3074. }
  3075. });
  3076. break;
  3077. case 'area':
  3078. this.animationInstance = new Animation({
  3079. timing: 'easeIn',
  3080. duration: duration,
  3081. onProcess: function onProcess(process) {
  3082. if (opts.rotate) {
  3083. contextRotate(context, opts);
  3084. }
  3085. drawYAxisGrid(categories, opts, config, context);
  3086. drawXAxis(categories, opts, config, context);
  3087. var _drawAreaDataPoints = drawAreaDataPoints(series, opts, config, context, process),
  3088. xAxisPoints = _drawAreaDataPoints.xAxisPoints,
  3089. calPoints = _drawAreaDataPoints.calPoints,
  3090. eachSpacing = _drawAreaDataPoints.eachSpacing;
  3091. _this.chartData.xAxisPoints = xAxisPoints;
  3092. _this.chartData.calPoints = calPoints;
  3093. _this.chartData.eachSpacing = eachSpacing;
  3094. drawLegend(opts.series, opts, config, context);
  3095. drawYAxis(series, opts, config, context);
  3096. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  3097. drawCanvas(opts, context);
  3098. },
  3099. onAnimationFinish: function onAnimationFinish() {
  3100. _this.event.trigger('renderComplete');
  3101. }
  3102. });
  3103. break;
  3104. case 'ring':
  3105. case 'pie':
  3106. this.animationInstance = new Animation({
  3107. timing: 'easeInOut',
  3108. duration: duration,
  3109. onProcess: function onProcess(process) {
  3110. if (opts.rotate) {
  3111. contextRotate(context, opts);
  3112. }
  3113. _this.chartData.pieData = drawPieDataPoints(series, opts, config, context, process);
  3114. drawLegend(opts.series, opts, config, context);
  3115. drawToolTipBridge(opts, config, context, process);
  3116. drawCanvas(opts, context);
  3117. },
  3118. onAnimationFinish: function onAnimationFinish() {
  3119. _this.event.trigger('renderComplete');
  3120. }
  3121. });
  3122. break;
  3123. case 'rose':
  3124. this.animationInstance = new Animation({
  3125. timing: 'easeInOut',
  3126. duration: duration,
  3127. onProcess: function onProcess(process) {
  3128. if (opts.rotate) {
  3129. contextRotate(context, opts);
  3130. }
  3131. _this.chartData.pieData = drawRoseDataPoints(series, opts, config, context, process);
  3132. drawLegend(opts.series, opts, config, context);
  3133. drawToolTipBridge(opts, config, context, process);
  3134. drawCanvas(opts, context);
  3135. },
  3136. onAnimationFinish: function onAnimationFinish() {
  3137. _this.event.trigger('renderComplete');
  3138. }
  3139. });
  3140. break;
  3141. case 'radar':
  3142. this.animationInstance = new Animation({
  3143. timing: 'easeInOut',
  3144. duration: duration,
  3145. onProcess: function onProcess(process) {
  3146. if (opts.rotate) {
  3147. contextRotate(context, opts);
  3148. }
  3149. _this.chartData.radarData = drawRadarDataPoints(series, opts, config, context, process);
  3150. drawLegend(opts.series, opts, config, context);
  3151. drawToolTipBridge(opts, config, context, process);
  3152. drawCanvas(opts, context);
  3153. },
  3154. onAnimationFinish: function onAnimationFinish() {
  3155. _this.event.trigger('renderComplete');
  3156. }
  3157. });
  3158. break;
  3159. case 'arcbar':
  3160. this.animationInstance = new Animation({
  3161. timing: 'easeInOut',
  3162. duration: duration,
  3163. onProcess: function onProcess(process) {
  3164. if (opts.rotate) {
  3165. contextRotate(context, opts);
  3166. }
  3167. _this.chartData.arcbarData = drawArcbarDataPoints(series, opts, config, context, process);
  3168. drawCanvas(opts, context);
  3169. },
  3170. onAnimationFinish: function onAnimationFinish() {
  3171. _this.event.trigger('renderComplete');
  3172. }
  3173. });
  3174. break;
  3175. case 'gauge':
  3176. this.animationInstance = new Animation({
  3177. timing: 'easeInOut',
  3178. duration: duration,
  3179. onProcess: function onProcess(process) {
  3180. if (opts.rotate) {
  3181. contextRotate(context, opts);
  3182. }
  3183. _this.chartData.gaugeData = drawGaugeDataPoints(categories, series, opts, config, context, process);
  3184. drawCanvas(opts, context);
  3185. },
  3186. onAnimationFinish: function onAnimationFinish() {
  3187. _this.event.trigger('renderComplete');
  3188. }
  3189. });
  3190. break;
  3191. case 'candle':
  3192. this.animationInstance = new Animation({
  3193. timing: 'easeIn',
  3194. duration: duration,
  3195. onProcess: function onProcess(process) {
  3196. if (opts.rotate) {
  3197. contextRotate(context, opts);
  3198. }
  3199. drawYAxisGrid(categories, opts, config, context);
  3200. drawXAxis(categories, opts, config, context);
  3201. var _drawCandleDataPoints = drawCandleDataPoints(series, seriesMA, opts, config, context, process),
  3202. xAxisPoints = _drawCandleDataPoints.xAxisPoints,
  3203. calPoints = _drawCandleDataPoints.calPoints,
  3204. eachSpacing = _drawCandleDataPoints.eachSpacing;
  3205. _this.chartData.xAxisPoints = xAxisPoints;
  3206. _this.chartData.calPoints = calPoints;
  3207. _this.chartData.eachSpacing = eachSpacing;
  3208. if (seriesMA) {
  3209. drawLegend(seriesMA, opts, config, context);
  3210. } else {
  3211. drawLegend(opts.series, opts, config, context);
  3212. }
  3213. drawYAxis(series, opts, config, context);
  3214. drawToolTipBridge(opts, config, context, process, eachSpacing, xAxisPoints);
  3215. drawCanvas(opts, context);
  3216. },
  3217. onAnimationFinish: function onAnimationFinish() {
  3218. _this.event.trigger('renderComplete');
  3219. }
  3220. });
  3221. break;
  3222. }
  3223. }
  3224. // simple event implement
  3225. function Event() {
  3226. this.events = {};
  3227. }
  3228. Event.prototype.addEventListener = function(type, listener) {
  3229. this.events[type] = this.events[type] || [];
  3230. this.events[type].push(listener);
  3231. };
  3232. Event.prototype.trigger = function() {
  3233. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  3234. args[_key] = arguments[_key];
  3235. }
  3236. var type = args[0];
  3237. var params = args.slice(1);
  3238. if (!!this.events[type]) {
  3239. this.events[type].forEach(function(listener) {
  3240. try {
  3241. listener.apply(null, params);
  3242. } catch (e) {
  3243. console.error(e);
  3244. }
  3245. });
  3246. }
  3247. };
  3248. var Charts = function Charts(opts) {
  3249. opts.fontSize = opts.fontSize ? opts.fontSize * opts.pixelRatio : 13 * opts.pixelRatio;
  3250. opts.title = opts.title || {};
  3251. opts.subtitle = opts.subtitle || {};
  3252. opts.yAxis = opts.yAxis || {};
  3253. opts.yAxis.gridType = opts.yAxis.gridType ? opts.yAxis.gridType : 'solid';
  3254. opts.yAxis.dashLength = opts.yAxis.dashLength ? opts.yAxis.dashLength : 4 * opts.pixelRatio;
  3255. opts.xAxis = opts.xAxis || {};
  3256. opts.xAxis.rotateLabel = opts.xAxis.rotateLabel ? true : false;
  3257. opts.xAxis.type = opts.xAxis.type ? opts.xAxis.type : 'calibration';
  3258. opts.xAxis.gridType = opts.xAxis.gridType ? opts.xAxis.gridType : 'solid';
  3259. opts.xAxis.dashLength = opts.xAxis.dashLength ? opts.xAxis.dashLength : 4 * opts.pixelRatio;
  3260. // opts.xAxis.itemCount = opts.xAxis.itemCount ? opts.xAxis.itemCount : 5;
  3261. opts.xAxis.scrollAlign = opts.xAxis.scrollAlign ? opts.xAxis.scrollAlign : 'left';
  3262. opts.extra = opts.extra || {};
  3263. opts.legend = opts.legend === false ? false : true;
  3264. opts.rotate = opts.rotate ? true : false;
  3265. opts.animation = opts.animation === false ? false : true;
  3266. var config$$1 = assign({}, config);
  3267. config$$1.yAxisTitleWidth = opts.yAxis.disabled !== true && opts.yAxis.title ? config$$1.yAxisTitleWidth : 0;
  3268. if (opts.type == 'pie' || opts.type == 'ring' ) {
  3269. config$$1.pieChartLinePadding = opts.dataLabel === false ? 0 : opts.extra.pie.lableWidth*opts.pixelRatio || config$$1.pieChartLinePadding *opts.pixelRatio;
  3270. }
  3271. config$$1.pieChartTextPadding = opts.dataLabel === false ? 0 : config$$1.pieChartTextPadding * opts.pixelRatio;
  3272. config$$1.yAxisSplit = opts.yAxis.splitNumber ? opts.yAxis.splitNumber : config.yAxisSplit;
  3273. //屏幕旋转
  3274. config$$1.rotate = opts.rotate;
  3275. if (opts.rotate) {
  3276. let tempWidth = opts.width;
  3277. let tempHeight = opts.height;
  3278. opts.width = tempHeight;
  3279. opts.height = tempWidth;
  3280. }
  3281. //适配H5高分屏
  3282. config$$1.yAxisWidth = config.yAxisWidth * opts.pixelRatio;
  3283. config$$1.xAxisHeight = config.xAxisHeight * opts.pixelRatio;
  3284. if (opts.enableScroll && opts.xAxis.scrollShow) {
  3285. config$$1.xAxisHeight += 6 * opts.pixelRatio;
  3286. }
  3287. config$$1.xAxisLineHeight = config.xAxisLineHeight * opts.pixelRatio;
  3288. config$$1.legendHeight = config.legendHeight * opts.pixelRatio;
  3289. //config$$1.yAxisTitleWidth=config.yAxisTitleWidth*opts.pixelRatio;
  3290. config$$1.padding = config.padding * opts.pixelRatio;
  3291. config$$1.fontSize = opts.fontSize;
  3292. config$$1.titleFontSize = config.titleFontSize * opts.pixelRatio;
  3293. config$$1.subtitleFontSize = config.subtitleFontSize * opts.pixelRatio;
  3294. config$$1.toolTipPadding = config.toolTipPadding * opts.pixelRatio;
  3295. config$$1.toolTipLineHeight = config.toolTipLineHeight * opts.pixelRatio;
  3296. config$$1.columePadding = config.columePadding * opts.pixelRatio;
  3297. //config$$1.xAxisTextPadding=config.xAxisTextPadding*opts.pixelRatio;
  3298. //向配置中传入当前pixelRatio及字体大小
  3299. config.pixelRatio = opts.pixelRatio;
  3300. config.fontSize = opts.fontSize;
  3301. config.rotate = opts.rotate;
  3302. this.opts = opts;
  3303. this.config = config$$1;
  3304. opts.$this = opts.$this ? opts.$this : this;
  3305. this.context = uni.createCanvasContext(opts.canvasId, opts.$this);
  3306. // store calcuated chart data
  3307. // such as chart point coordinate
  3308. this.chartData = {};
  3309. this.event = new Event();
  3310. this.scrollOption = {
  3311. currentOffset: 0,
  3312. startTouchX: 0,
  3313. distance: 0,
  3314. lastMoveTime: 0
  3315. };
  3316. //计算右对齐偏移距离
  3317. if (opts.enableScroll && opts.xAxis.scrollAlign == 'right') {
  3318. let _calYAxisData = calYAxisData(opts.series, opts, config$$1),
  3319. yAxisWidth = _calYAxisData.yAxisWidth;
  3320. config$$1.yAxisWidth = yAxisWidth;
  3321. let offsetLeft = 0;
  3322. let _getXAxisPoints0 = getXAxisPoints(opts.categories, opts, config$$1),
  3323. xAxisPoints = _getXAxisPoints0.xAxisPoints,
  3324. startX = _getXAxisPoints0.startX,
  3325. endX = _getXAxisPoints0.endX,
  3326. eachSpacing = _getXAxisPoints0.eachSpacing;
  3327. let totalWidth = eachSpacing * (xAxisPoints.length - 1);
  3328. let screenWidth = endX - startX;
  3329. offsetLeft = screenWidth - totalWidth;
  3330. this.scrollOption = {
  3331. currentOffset: offsetLeft,
  3332. startTouchX: offsetLeft,
  3333. distance: 0,
  3334. lastMoveTime: 0
  3335. };
  3336. opts._scrollDistance_ = offsetLeft;
  3337. }
  3338. drawCharts.call(this, opts.type, opts, config$$1, this.context);
  3339. };
  3340. Charts.prototype.updateData = function() {
  3341. let data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  3342. this.opts = assign({}, this.opts, data);
  3343. this.opts.yAxis.format = data.unit ? ((val)=>{return val.toFixed(data.fixed||0)+(data.unit||'')}) : this.opts.yAxis.format;
  3344. let scrollPosition = data.scrollPosition || 'current';
  3345. switch (scrollPosition) {
  3346. case 'current':
  3347. this.opts._scrollDistance_ = this.scrollOption.currentOffset;
  3348. break;
  3349. case 'left':
  3350. this.opts._scrollDistance_ = 0;
  3351. this.scrollOption = {
  3352. currentOffset: 0,
  3353. startTouchX: 0,
  3354. distance: 0,
  3355. lastMoveTime: 0
  3356. };
  3357. break;
  3358. case 'right':
  3359. let _calYAxisData = calYAxisData(this.opts.series, this.opts, this.config),
  3360. yAxisWidth = _calYAxisData.yAxisWidth;
  3361. this.config.yAxisWidth = yAxisWidth;
  3362. let offsetLeft = 0;
  3363. let _getXAxisPoints0 = getXAxisPoints(this.opts.categories, this.opts, this.config),
  3364. xAxisPoints = _getXAxisPoints0.xAxisPoints,
  3365. startX = _getXAxisPoints0.startX,
  3366. endX = _getXAxisPoints0.endX,
  3367. eachSpacing = _getXAxisPoints0.eachSpacing;
  3368. let totalWidth = eachSpacing * (xAxisPoints.length - 1);
  3369. let screenWidth = endX - startX;
  3370. offsetLeft = screenWidth - totalWidth;
  3371. this.scrollOption = {
  3372. currentOffset: offsetLeft,
  3373. startTouchX: offsetLeft,
  3374. distance: 0,
  3375. lastMoveTime: 0
  3376. };
  3377. this.opts._scrollDistance_ = offsetLeft;
  3378. break;
  3379. }
  3380. let animation = data.animation == undefined ? this.opts.animation : data.animation;
  3381. this.opts.animation = animation;
  3382. this.opts.title = assign({}, this.opts.title, data.title || {});
  3383. this.opts.subtitle = assign({}, this.opts.subtitle, data.subtitle || {});
  3384. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  3385. };
  3386. Charts.prototype.zoom = function() {
  3387. var val = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.opts.xAxis.itemCount;
  3388. if (this.opts.enableScroll !== true) {
  3389. console.log('请启用滚动条后使用!')
  3390. return;
  3391. }
  3392. //当前屏幕中间点
  3393. let centerPoint = Math.round(Math.abs(this.scrollOption.currentOffset) / this.chartData.eachSpacing) + Math.round(
  3394. this.opts.xAxis.itemCount / 2);
  3395. this.opts.animation = false;
  3396. this.opts.xAxis.itemCount = val.itemCount;
  3397. //重新计算x轴偏移距离
  3398. let _calYAxisData = calYAxisData(this.opts.series, this.opts, this.config),
  3399. yAxisWidth = _calYAxisData.yAxisWidth;
  3400. this.config.yAxisWidth = yAxisWidth;
  3401. let offsetLeft = 0;
  3402. let _getXAxisPoints0 = getXAxisPoints(this.opts.categories, this.opts, this.config),
  3403. xAxisPoints = _getXAxisPoints0.xAxisPoints,
  3404. startX = _getXAxisPoints0.startX,
  3405. endX = _getXAxisPoints0.endX,
  3406. eachSpacing = _getXAxisPoints0.eachSpacing;
  3407. let centerLeft = eachSpacing * centerPoint;
  3408. let screenWidth = endX - startX;
  3409. let MaxLeft = screenWidth - eachSpacing * (xAxisPoints.length - 1);
  3410. offsetLeft = screenWidth / 2 - centerLeft;
  3411. if (offsetLeft > 0) {
  3412. offsetLeft = 0;
  3413. }
  3414. if (offsetLeft < MaxLeft) {
  3415. offsetLeft = MaxLeft;
  3416. }
  3417. this.scrollOption = {
  3418. currentOffset: offsetLeft,
  3419. startTouchX: offsetLeft,
  3420. distance: 0,
  3421. lastMoveTime: 0
  3422. };
  3423. this.opts._scrollDistance_ = offsetLeft;
  3424. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  3425. };
  3426. Charts.prototype.stopAnimation = function() {
  3427. this.animationInstance && this.animationInstance.stop();
  3428. };
  3429. Charts.prototype.addEventListener = function(type, listener) {
  3430. this.event.addEventListener(type, listener);
  3431. };
  3432. Charts.prototype.getCurrentDataIndex = function(e) {
  3433. var touches = e.mp.changedTouches[0] || e.changedTouches[0];
  3434. if (touches) {
  3435. var _touches$ = getTouches(touches, this.opts, e);
  3436. if (this.opts.type === 'pie' || this.opts.type === 'ring' || this.opts.type === 'rose') {
  3437. return findPieChartCurrentIndex({
  3438. x: _touches$.x,
  3439. y: _touches$.y
  3440. }, this.chartData.pieData);
  3441. } else if (this.opts.type === 'radar') {
  3442. return findRadarChartCurrentIndex({
  3443. x: _touches$.x,
  3444. y: _touches$.y
  3445. }, this.chartData.radarData, this.opts.categories.length);
  3446. } else {
  3447. return findCurrentIndex({
  3448. x: _touches$.x,
  3449. y: _touches$.y
  3450. }, this.chartData.xAxisPoints, this.opts, this.config, Math.abs(this.scrollOption.currentOffset));
  3451. }
  3452. }
  3453. return -1;
  3454. };
  3455. Charts.prototype.showToolTip = function(e) {
  3456. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  3457. var touches = e.mp.changedTouches[0] || e.changedTouches[0];
  3458. var _touches$ = getTouches(touches, this.opts, e);
  3459. if (this.opts.type === 'line' || this.opts.type === 'area' || this.opts.type === 'column') {
  3460. var index = this.getCurrentDataIndex(e);
  3461. var currentOffset = this.scrollOption.currentOffset;
  3462. var opts = assign({}, this.opts, {
  3463. _scrollDistance_: currentOffset,
  3464. animation: false
  3465. });
  3466. if (index > -1) {
  3467. var seriesData = getSeriesDataItem(this.opts.series, index);
  3468. if (seriesData.length !== 0) {
  3469. var _getToolTipData = getToolTipData(seriesData, this.chartData.calPoints, index, this.opts.categories, option),
  3470. textList = _getToolTipData.textList,
  3471. offset = _getToolTipData.offset;
  3472. offset.y = _touches$.y;
  3473. opts.tooltip = {
  3474. textList: textList,
  3475. offset: offset,
  3476. option: option,
  3477. index: index
  3478. };
  3479. }
  3480. }
  3481. drawCharts.call(this, opts.type, opts, this.config, this.context);
  3482. }
  3483. if (this.opts.type === 'mix') {
  3484. var index = this.getCurrentDataIndex(e);
  3485. var currentOffset = this.scrollOption.currentOffset;
  3486. var opts = assign({}, this.opts, {
  3487. _scrollDistance_: currentOffset,
  3488. animation: false
  3489. });
  3490. if (index > -1) {
  3491. var seriesData = getSeriesDataItem(this.opts.series, index);
  3492. if (seriesData.length !== 0) {
  3493. var _getMixToolTipData = getMixToolTipData(seriesData, this.chartData.calPoints, index, this.opts.categories,option),
  3494. textList = _getMixToolTipData.textList,
  3495. offset = _getMixToolTipData.offset;
  3496. offset.y = _touches$.y;
  3497. opts.tooltip = {
  3498. textList: textList,
  3499. offset: offset,
  3500. option: option,
  3501. index: index
  3502. };
  3503. }
  3504. }
  3505. drawCharts.call(this, opts.type, opts, this.config, this.context);
  3506. }
  3507. if (this.opts.type === 'candle') {
  3508. var index = this.getCurrentDataIndex(e);
  3509. var currentOffset = this.scrollOption.currentOffset;
  3510. var opts = assign({}, this.opts, {
  3511. _scrollDistance_: currentOffset,
  3512. animation: false
  3513. });
  3514. if (index > -1) {
  3515. var seriesData = getSeriesDataItem(this.opts.series, index);
  3516. if (seriesData.length !== 0) {
  3517. var _getToolTipData = getCandleToolTipData(this.opts.series[0].data, seriesData, this.chartData.calPoints, index,this.opts.categories, this.opts.extra.candle, option),
  3518. textList = _getToolTipData.textList,
  3519. offset = _getToolTipData.offset;
  3520. offset.y = _touches$.y;
  3521. opts.tooltip = {
  3522. textList: textList,
  3523. offset: offset,
  3524. option: option,
  3525. index: index
  3526. };
  3527. }
  3528. }
  3529. drawCharts.call(this, opts.type, opts, this.config, this.context);
  3530. }
  3531. if (this.opts.type === 'pie' || this.opts.type === 'ring' || this.opts.type === 'rose') {
  3532. var index = this.getCurrentDataIndex(e);
  3533. var currentOffset = this.scrollOption.currentOffset;
  3534. var opts = assign({}, this.opts, {
  3535. _scrollDistance_: currentOffset,
  3536. animation: false
  3537. });
  3538. if (index > -1) {
  3539. var seriesData = this.opts.series[index];
  3540. var textList = [{
  3541. text: option.format ? option.format(seriesData) : seriesData.name + ': ' + seriesData.data,
  3542. color: seriesData.color
  3543. }];
  3544. var offset = {
  3545. x: _touches$.x,
  3546. y: _touches$.y
  3547. };
  3548. opts.tooltip = {
  3549. textList: textList,
  3550. offset: offset,
  3551. option: option,
  3552. index: index
  3553. };
  3554. }
  3555. drawCharts.call(this, opts.type, opts, this.config, this.context);
  3556. }
  3557. if (this.opts.type === 'radar') {
  3558. var index = this.getCurrentDataIndex(e);
  3559. var currentOffset = this.scrollOption.currentOffset;
  3560. var opts = assign({}, this.opts, {
  3561. _scrollDistance_: currentOffset,
  3562. animation: false
  3563. });
  3564. if (index > -1) {
  3565. var seriesData = getSeriesDataItem(this.opts.series, index);
  3566. if (seriesData.length !== 0) {
  3567. var textList = seriesData.map(function(item) {
  3568. return {
  3569. text: option.format ? option.format(item) : item.name + ': ' + item.data,
  3570. color: item.color
  3571. };
  3572. });
  3573. var offset = {
  3574. x: _touches$.x,
  3575. y: _touches$.y
  3576. };
  3577. opts.tooltip = {
  3578. textList: textList,
  3579. offset: offset,
  3580. option: option,
  3581. index: index
  3582. };
  3583. }
  3584. }
  3585. drawCharts.call(this, opts.type, opts, this.config, this.context);
  3586. }
  3587. };
  3588. Charts.prototype.translate = function(distance) {
  3589. this.scrollOption = {
  3590. currentOffset: distance,
  3591. startTouchX: distance,
  3592. distance: 0,
  3593. lastMoveTime: 0
  3594. };
  3595. let opts = assign({}, this.opts, {
  3596. _scrollDistance_: distance,
  3597. animation: false
  3598. });
  3599. drawCharts.call(this, this.opts.type, opts, this.config, this.context);
  3600. };
  3601. Charts.prototype.scrollStart = function(e) {
  3602. var touches = e.mp.changedTouches[0] || e.changedTouches[0];
  3603. var _touches$ = getTouches(touches, this.opts, e);
  3604. if (touches && this.opts.enableScroll === true) {
  3605. if (touches.x) {
  3606. this.scrollOption.startTouchX = _touches$.x;
  3607. } else {
  3608. this.scrollOption.startTouchX = _touches$.clientX;
  3609. }
  3610. }
  3611. };
  3612. Charts.prototype.scroll = function(e) {
  3613. if (this.scrollOption.lastMoveTime === 0) {
  3614. this.scrollOption.lastMoveTime = Date.now();
  3615. }
  3616. let Limit = this.opts.extra.touchMoveLimit || 20;
  3617. let currMoveTime = Date.now();
  3618. let duration = currMoveTime - this.scrollOption.lastMoveTime;
  3619. if (duration < Math.floor(1000 / Limit)) return;
  3620. this.scrollOption.lastMoveTime = currMoveTime;
  3621. var touches = e.mp.changedTouches[0] || e.changedTouches[0];
  3622. var _touches$ = getTouches(touches, this.opts, e);
  3623. if (touches && this.opts.enableScroll === true) {
  3624. var _distance;
  3625. if (touches.x) {
  3626. _distance = _touches$.x - this.scrollOption.startTouchX;
  3627. } else {
  3628. _distance = _touches$.clientX - this.scrollOption.startTouchX;
  3629. }
  3630. var currentOffset = this.scrollOption.currentOffset;
  3631. var validDistance = calValidDistance(currentOffset + _distance, this.chartData, this.config, this.opts);
  3632. this.scrollOption.distance = _distance = validDistance - currentOffset;
  3633. var opts = assign({}, this.opts, {
  3634. _scrollDistance_: currentOffset + _distance,
  3635. animation: false
  3636. });
  3637. drawCharts.call(this, opts.type, opts, this.config, this.context);
  3638. return currentOffset + _distance;
  3639. }
  3640. };
  3641. Charts.prototype.scrollEnd = function(e) {
  3642. if (this.opts.enableScroll === true) {
  3643. var _scrollOption = this.scrollOption,
  3644. currentOffset = _scrollOption.currentOffset,
  3645. distance = _scrollOption.distance;
  3646. this.scrollOption.currentOffset = currentOffset + distance;
  3647. this.scrollOption.distance = 0;
  3648. }
  3649. };
  3650. module.exports = Charts;