wxcharts.js 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  1. /*
  2. * charts for WeChat small app v1.0
  3. *
  4. * https://github.com/xiaolin3303/wx-charts
  5. * 2016-11-28
  6. *
  7. * Designed and built with all the love of Web
  8. */
  9. 'use strict';
  10. var config = {
  11. yAxisWidth: 15,
  12. yAxisSplit: 5,
  13. xAxisHeight: 15,
  14. xAxisLineHeight: 15,
  15. legendHeight: 15,
  16. yAxisTitleWidth: 15,
  17. padding: 12,
  18. columePadding: 3,
  19. fontSize: 10,
  20. dataPointShape: ['diamond', 'circle', 'triangle', 'rect'],
  21. colors: ['#7cb5ec', '#f7a35c', '#434348', '#90ed7d', '#f15c80', '#8085e9'],
  22. pieChartLinePadding: 25,
  23. pieChartTextPadding: 15,
  24. xAxisTextPadding: 3,
  25. titleColor: '#333333',
  26. titleFontSize: 20,
  27. subtitleColor: '#999999',
  28. subtitleFontSize: 15,
  29. toolTipPadding: 3,
  30. toolTipBackground: '#000000',
  31. toolTipOpacity: 0.7,
  32. toolTipLineHeight: 14,
  33. radarGridCount: 3,
  34. radarLabelTextMargin: 15
  35. };
  36. // Object.assign polyfill
  37. // https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
  38. function assign(target, varArgs) {
  39. if (target == null) {
  40. // TypeError if undefined or null
  41. throw new TypeError('Cannot convert undefined or null to object');
  42. }
  43. var to = Object(target);
  44. for (var index = 1; index < arguments.length; index++) {
  45. var nextSource = arguments[index];
  46. if (nextSource != null) {
  47. // Skip over if undefined or null
  48. for (var nextKey in nextSource) {
  49. // Avoid bugs when hasOwnProperty is shadowed
  50. if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
  51. to[nextKey] = nextSource[nextKey];
  52. }
  53. }
  54. }
  55. }
  56. return to;
  57. }
  58. var util = {
  59. toFixed: function toFixed(num, limit) {
  60. limit = limit || 2;
  61. if (this.isFloat(num)) {
  62. num = num.toFixed(limit);
  63. }
  64. return num;
  65. },
  66. isFloat: function isFloat(num) {
  67. return num % 1 !== 0;
  68. },
  69. approximatelyEqual: function approximatelyEqual(num1, num2) {
  70. return Math.abs(num1 - num2) < 1e-10;
  71. },
  72. isSameSign: function isSameSign(num1, num2) {
  73. return Math.abs(num1) === num1 && Math.abs(num2) === num2 || Math.abs(num1) !== num1 && Math.abs(num2) !== num2;
  74. },
  75. isSameXCoordinateArea: function isSameXCoordinateArea(p1, p2) {
  76. return this.isSameSign(p1.x, p2.x);
  77. },
  78. isCollision: function isCollision(obj1, obj2) {
  79. obj1.end = {};
  80. obj1.end.x = obj1.start.x + obj1.width;
  81. obj1.end.y = obj1.start.y - obj1.height;
  82. obj2.end = {};
  83. obj2.end.x = obj2.start.x + obj2.width;
  84. obj2.end.y = obj2.start.y - obj2.height;
  85. var flag = obj2.start.x > obj1.end.x || obj2.end.x < obj1.start.x || obj2.end.y > obj1.start.y || obj2.start.y < obj1.end.y;
  86. return !flag;
  87. }
  88. };
  89. function findRange(num, type, limit) {
  90. if (isNaN(num)) {
  91. throw new Error('[wxCharts] unvalid series data!');
  92. }
  93. limit = limit || 10;
  94. type = type ? type : 'upper';
  95. var multiple = 1;
  96. while (limit < 1) {
  97. limit *= 10;
  98. multiple *= 10;
  99. }
  100. if (type === 'upper') {
  101. num = Math.ceil(num * multiple);
  102. } else {
  103. num = Math.floor(num * multiple);
  104. }
  105. while (num % limit !== 0) {
  106. if (type === 'upper') {
  107. num++;
  108. } else {
  109. num--;
  110. }
  111. }
  112. return num / multiple;
  113. }
  114. function calValidDistance(distance, chartData, config, opts) {
  115. var dataChartAreaWidth = opts.width - config.padding - chartData.xAxisPoints[0];
  116. var dataChartWidth = chartData.eachSpacing * opts.categories.length;
  117. var validDistance = distance;
  118. if (distance >= 0) {
  119. validDistance = 0;
  120. } else if (Math.abs(distance) >= dataChartWidth - dataChartAreaWidth) {
  121. validDistance = dataChartAreaWidth - dataChartWidth;
  122. }
  123. return validDistance;
  124. }
  125. function isInAngleRange(angle, startAngle, endAngle) {
  126. function adjust(angle) {
  127. while (angle < 0) {
  128. angle += 2 * Math.PI;
  129. }
  130. while (angle > 2 * Math.PI) {
  131. angle -= 2 * Math.PI;
  132. }
  133. return angle;
  134. }
  135. angle = adjust(angle);
  136. startAngle = adjust(startAngle);
  137. endAngle = adjust(endAngle);
  138. if (startAngle > endAngle) {
  139. endAngle += 2 * Math.PI;
  140. if (angle < startAngle) {
  141. angle += 2 * Math.PI;
  142. }
  143. }
  144. return angle >= startAngle && angle <= endAngle;
  145. }
  146. function calRotateTranslate(x, y, h) {
  147. var xv = x;
  148. var yv = h - y;
  149. var transX = xv + (h - yv - xv) / Math.sqrt(2);
  150. transX *= -1;
  151. var transY = (h - yv) * (Math.sqrt(2) - 1) - (h - yv - xv) / Math.sqrt(2);
  152. return {
  153. transX: transX,
  154. transY: transY
  155. };
  156. }
  157. function createCurveControlPoints(points, i) {
  158. function isNotMiddlePoint(points, i) {
  159. if (points[i - 1] && points[i + 1]) {
  160. return points[i].y >= Math.max(points[i - 1].y, points[i + 1].y) || points[i].y <= Math.min(points[i - 1].y, points[i + 1].y);
  161. } else {
  162. return false;
  163. }
  164. }
  165. var a = 0.2;
  166. var b = 0.2;
  167. var pAx = null;
  168. var pAy = null;
  169. var pBx = null;
  170. var pBy = null;
  171. if (i < 1) {
  172. pAx = points[0].x + (points[1].x - points[0].x) * a;
  173. pAy = points[0].y + (points[1].y - points[0].y) * a;
  174. } else {
  175. pAx = points[i].x + (points[i + 1].x - points[i - 1].x) * a;
  176. pAy = points[i].y + (points[i + 1].y - points[i - 1].y) * a;
  177. }
  178. if (i > points.length - 3) {
  179. var last = points.length - 1;
  180. pBx = points[last].x - (points[last].x - points[last - 1].x) * b;
  181. pBy = points[last].y - (points[last].y - points[last - 1].y) * b;
  182. } else {
  183. pBx = points[i + 1].x - (points[i + 2].x - points[i].x) * b;
  184. pBy = points[i + 1].y - (points[i + 2].y - points[i].y) * b;
  185. }
  186. // fix issue https://github.com/xiaolin3303/wx-charts/issues/79
  187. if (isNotMiddlePoint(points, i + 1)) {
  188. pBy = points[i + 1].y;
  189. }
  190. if (isNotMiddlePoint(points, i)) {
  191. pAy = points[i].y;
  192. }
  193. return {
  194. ctrA: { x: pAx, y: pAy },
  195. ctrB: { x: pBx, y: pBy }
  196. };
  197. }
  198. function convertCoordinateOrigin(x, y, center) {
  199. return {
  200. x: center.x + x,
  201. y: center.y - y
  202. };
  203. }
  204. function avoidCollision(obj, target) {
  205. if (target) {
  206. // is collision test
  207. while (util.isCollision(obj, target)) {
  208. if (obj.start.x > 0) {
  209. obj.start.y--;
  210. } else if (obj.start.x < 0) {
  211. obj.start.y++;
  212. } else {
  213. if (obj.start.y > 0) {
  214. obj.start.y++;
  215. } else {
  216. obj.start.y--;
  217. }
  218. }
  219. }
  220. }
  221. return obj;
  222. }
  223. function fillSeriesColor(series, config) {
  224. var index = 0;
  225. return series.map(function (item) {
  226. if (!item.color) {
  227. item.color = config.colors[index];
  228. index = (index + 1) % config.colors.length;
  229. }
  230. return item;
  231. });
  232. }
  233. function getDataRange(minData, maxData) {
  234. var limit = 0;
  235. var range = maxData - minData;
  236. if (range >= 10000) {
  237. limit = 1000;
  238. } else if (range >= 1000) {
  239. limit = 100;
  240. } else if (range >= 100) {
  241. limit = 10;
  242. } else if (range >= 10) {
  243. limit = 5;
  244. } else if (range >= 1) {
  245. limit = 1;
  246. } else if (range >= 0.1) {
  247. limit = 0.1;
  248. } else {
  249. limit = 0.01;
  250. }
  251. return {
  252. minRange: findRange(minData, 'lower', limit),
  253. maxRange: findRange(maxData, 'upper', limit)
  254. };
  255. }
  256. function measureText(text) {
  257. var fontSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
  258. // wx canvas 未实现measureText方法, 此处自行实现
  259. text = String(text);
  260. var text = text.split('');
  261. var width = 0;
  262. text.forEach(function (item) {
  263. if (/[a-zA-Z]/.test(item)) {
  264. width += 7;
  265. } else if (/[0-9]/.test(item)) {
  266. width += 5.5;
  267. } else if (/\./.test(item)) {
  268. width += 2.7;
  269. } else if (/-/.test(item)) {
  270. width += 3.25;
  271. } else if (/[\u4e00-\u9fa5]/.test(item)) {
  272. width += 10;
  273. } else if (/\(|\)/.test(item)) {
  274. width += 3.73;
  275. } else if (/\s/.test(item)) {
  276. width += 2.5;
  277. } else if (/%/.test(item)) {
  278. width += 8;
  279. } else {
  280. width += 10;
  281. }
  282. });
  283. return width * fontSize / 10;
  284. }
  285. function dataCombine(series) {
  286. return series.reduce(function (a, b) {
  287. return (a.data ? a.data : a).concat(b.data);
  288. }, []);
  289. }
  290. function getSeriesDataItem(series, index) {
  291. var data = [];
  292. series.forEach(function (item) {
  293. if (item.data[index] !== null && typeof item.data[index] !== 'undefined') {
  294. var seriesItem = {};
  295. seriesItem.color = item.color;
  296. seriesItem.name = item.name;
  297. seriesItem.data = item.format ? item.format(item.data[index]) : item.data[index];
  298. data.push(seriesItem);
  299. }
  300. });
  301. return data;
  302. }
  303. function getMaxTextListLength(list) {
  304. var lengthList = list.map(function (item) {
  305. return measureText(item);
  306. });
  307. return Math.max.apply(null, lengthList);
  308. }
  309. function getRadarCoordinateSeries(length) {
  310. var eachAngle = 2 * Math.PI / length;
  311. var CoordinateSeries = [];
  312. for (var i = 0; i < length; i++) {
  313. CoordinateSeries.push(eachAngle * i);
  314. }
  315. return CoordinateSeries.map(function (item) {
  316. return -1 * item + Math.PI / 2;
  317. });
  318. }
  319. function getToolTipData(seriesData, calPoints, index, categories) {
  320. var option = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
  321. var textList = seriesData.map(function (item) {
  322. return {
  323. text: option.format ? option.format(item, categories[index]) : item.name + ': ' + item.data,
  324. color: item.color
  325. };
  326. });
  327. var validCalPoints = [];
  328. var offset = {
  329. x: 0,
  330. y: 0
  331. };
  332. calPoints.forEach(function (points) {
  333. if (typeof points[index] !== 'undefined' && points[index] !== null) {
  334. validCalPoints.push(points[index]);
  335. }
  336. });
  337. validCalPoints.forEach(function (item) {
  338. offset.x = Math.round(item.x);
  339. offset.y += item.y;
  340. });
  341. offset.y /= validCalPoints.length;
  342. return { textList: textList, offset: offset };
  343. }
  344. function findCurrentIndex(currentPoints, xAxisPoints, opts, config) {
  345. var offset = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 0;
  346. var currentIndex = -1;
  347. if (isInExactChartArea(currentPoints, opts, config)) {
  348. xAxisPoints.forEach(function (item, index) {
  349. if (currentPoints.x + offset > item) {
  350. currentIndex = index;
  351. }
  352. });
  353. }
  354. return currentIndex;
  355. }
  356. function isInExactChartArea(currentPoints, opts, config) {
  357. return currentPoints.x < opts.width - config.padding && currentPoints.x > config.padding + config.yAxisWidth + config.yAxisTitleWidth && currentPoints.y > config.padding && currentPoints.y < opts.height - config.legendHeight - config.xAxisHeight - config.padding;
  358. }
  359. function findRadarChartCurrentIndex(currentPoints, radarData, count) {
  360. var eachAngleArea = 2 * Math.PI / count;
  361. var currentIndex = -1;
  362. if (isInExactPieChartArea(currentPoints, radarData.center, radarData.radius)) {
  363. var fixAngle = function fixAngle(angle) {
  364. if (angle < 0) {
  365. angle += 2 * Math.PI;
  366. }
  367. if (angle > 2 * Math.PI) {
  368. angle -= 2 * Math.PI;
  369. }
  370. return angle;
  371. };
  372. var angle = Math.atan2(radarData.center.y - currentPoints.y, currentPoints.x - radarData.center.x);
  373. angle = -1 * angle;
  374. if (angle < 0) {
  375. angle += 2 * Math.PI;
  376. }
  377. var angleList = radarData.angleList.map(function (item) {
  378. item = fixAngle(-1 * item);
  379. return item;
  380. });
  381. angleList.forEach(function (item, index) {
  382. var rangeStart = fixAngle(item - eachAngleArea / 2);
  383. var rangeEnd = fixAngle(item + eachAngleArea / 2);
  384. if (rangeEnd < rangeStart) {
  385. rangeEnd += 2 * Math.PI;
  386. }
  387. if (angle >= rangeStart && angle <= rangeEnd || angle + 2 * Math.PI >= rangeStart && angle + 2 * Math.PI <= rangeEnd) {
  388. currentIndex = index;
  389. }
  390. });
  391. }
  392. return currentIndex;
  393. }
  394. function findPieChartCurrentIndex(currentPoints, pieData) {
  395. var currentIndex = -1;
  396. if (isInExactPieChartArea(currentPoints, pieData.center, pieData.radius)) {
  397. var angle = Math.atan2(pieData.center.y - currentPoints.y, currentPoints.x - pieData.center.x);
  398. angle = -angle;
  399. for (var i = 0, len = pieData.series.length; i < len; i++) {
  400. var item = pieData.series[i];
  401. if (isInAngleRange(angle, item._start_, item._start_ + item._proportion_ * 2 * Math.PI)) {
  402. currentIndex = i;
  403. break;
  404. }
  405. }
  406. }
  407. return currentIndex;
  408. }
  409. function isInExactPieChartArea(currentPoints, center, radius) {
  410. return Math.pow(currentPoints.x - center.x, 2) + Math.pow(currentPoints.y - center.y, 2) <= Math.pow(radius, 2);
  411. }
  412. function splitPoints(points) {
  413. var newPoints = [];
  414. var items = [];
  415. points.forEach(function (item, index) {
  416. if (item !== null) {
  417. items.push(item);
  418. } else {
  419. if (items.length) {
  420. newPoints.push(items);
  421. }
  422. items = [];
  423. }
  424. });
  425. if (items.length) {
  426. newPoints.push(items);
  427. }
  428. return newPoints;
  429. }
  430. function calLegendData(series, opts, config) {
  431. if (opts.legend === false) {
  432. return {
  433. legendList: [],
  434. legendHeight: 0
  435. };
  436. }
  437. var padding = 5;
  438. var marginTop = 8;
  439. var shapeWidth = 15;
  440. var legendList = [];
  441. var widthCount = 0;
  442. var currentRow = [];
  443. series.forEach(function (item) {
  444. var itemWidth = 3 * padding + shapeWidth + measureText(item.name || 'undefined');
  445. if (widthCount + itemWidth > opts.width) {
  446. legendList.push(currentRow);
  447. widthCount = itemWidth;
  448. currentRow = [item];
  449. } else {
  450. widthCount += itemWidth;
  451. currentRow.push(item);
  452. }
  453. });
  454. if (currentRow.length) {
  455. legendList.push(currentRow);
  456. }
  457. return {
  458. legendList: legendList,
  459. legendHeight: legendList.length * (config.fontSize + marginTop) + padding
  460. };
  461. }
  462. function calCategoriesData(categories, opts, config) {
  463. var result = {
  464. angle: 0,
  465. xAxisHeight: config.xAxisHeight
  466. };
  467. var _getXAxisPoints = getXAxisPoints(categories, opts, config),
  468. eachSpacing = _getXAxisPoints.eachSpacing;
  469. // get max length of categories text
  470. var categoriesTextLenth = categories.map(function (item) {
  471. return measureText(item);
  472. });
  473. var maxTextLength = Math.max.apply(this, categoriesTextLenth);
  474. if (maxTextLength + 2 * config.xAxisTextPadding > eachSpacing) {
  475. result.angle = 45 * Math.PI / 180;
  476. result.xAxisHeight = 2 * config.xAxisTextPadding + maxTextLength * Math.sin(result.angle);
  477. }
  478. return result;
  479. }
  480. function getRadarDataPoints(angleList, center, radius, series, opts) {
  481. var process = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 1;
  482. var radarOption = opts.extra.radar || {};
  483. radarOption.max = radarOption.max || 0;
  484. var maxData = Math.max(radarOption.max, Math.max.apply(null, dataCombine(series)));
  485. var data = [];
  486. series.forEach(function (each) {
  487. var listItem = {};
  488. listItem.color = each.color;
  489. listItem.data = [];
  490. each.data.forEach(function (item, index) {
  491. var tmp = {};
  492. tmp.angle = angleList[index];
  493. tmp.proportion = item / maxData;
  494. tmp.position = convertCoordinateOrigin(radius * tmp.proportion * process * Math.cos(tmp.angle), radius * tmp.proportion * process * Math.sin(tmp.angle), center);
  495. listItem.data.push(tmp);
  496. });
  497. data.push(listItem);
  498. });
  499. return data;
  500. }
  501. function getPieDataPoints(series) {
  502. var process = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
  503. var count = 0;
  504. var _start_ = 0;
  505. series.forEach(function (item) {
  506. item.data = item.data === null ? 0 : item.data;
  507. count += item.data;
  508. });
  509. series.forEach(function (item) {
  510. item.data = item.data === null ? 0 : item.data;
  511. item._proportion_ = item.data / count * process;
  512. });
  513. series.forEach(function (item) {
  514. item._start_ = _start_;
  515. _start_ += 2 * item._proportion_ * Math.PI;
  516. });
  517. return series;
  518. }
  519. function getPieTextMaxLength(series) {
  520. series = getPieDataPoints(series);
  521. var maxLength = 0;
  522. series.forEach(function (item) {
  523. var text = item.format ? item.format(+item._proportion_.toFixed(2)) : util.toFixed(item._proportion_ * 100) + '%';
  524. maxLength = Math.max(maxLength, measureText(text));
  525. });
  526. return maxLength;
  527. }
  528. function fixColumeData(points, eachSpacing, columnLen, index, config, opts) {
  529. return points.map(function (item) {
  530. if (item === null) {
  531. return null;
  532. }
  533. item.width = (eachSpacing - 2 * config.columePadding) / columnLen;
  534. if (opts.extra.column && opts.extra.column.width && +opts.extra.column.width > 0) {
  535. // customer column width
  536. item.width = Math.min(item.width, +opts.extra.column.width);
  537. } else {
  538. // default width should less tran 25px
  539. // don't ask me why, I don't know
  540. item.width = Math.min(item.width, 25);
  541. }
  542. item.x += (index + 0.5 - columnLen / 2) * item.width;
  543. return item;
  544. });
  545. }
  546. function getXAxisPoints(categories, opts, config) {
  547. var yAxisTotalWidth = config.yAxisWidth + config.yAxisTitleWidth;
  548. var spacingValid = opts.width - 2 * config.padding - yAxisTotalWidth;
  549. var dataCount = opts.enableScroll ? Math.min(5, categories.length) : categories.length;
  550. var eachSpacing = spacingValid / dataCount;
  551. var xAxisPoints = [];
  552. var startX = config.padding + yAxisTotalWidth;
  553. var endX = opts.width - config.padding;
  554. categories.forEach(function (item, index) {
  555. xAxisPoints.push(startX + index * eachSpacing);
  556. });
  557. if (opts.enableScroll === true) {
  558. xAxisPoints.push(startX + categories.length * eachSpacing);
  559. } else {
  560. xAxisPoints.push(endX);
  561. }
  562. return { xAxisPoints: xAxisPoints, startX: startX, endX: endX, eachSpacing: eachSpacing };
  563. }
  564. function getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config) {
  565. var process = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : 1;
  566. var points = [];
  567. var validHeight = opts.height - 2 * config.padding - config.xAxisHeight - config.legendHeight;
  568. data.forEach(function (item, index) {
  569. if (item === null) {
  570. points.push(null);
  571. } else {
  572. var point = {};
  573. point.x = xAxisPoints[index] + Math.round(eachSpacing / 2);
  574. var height = validHeight * (item - minRange) / (maxRange - minRange);
  575. height *= process;
  576. point.y = opts.height - config.xAxisHeight - config.legendHeight - Math.round(height) - config.padding;
  577. points.push(point);
  578. }
  579. });
  580. return points;
  581. }
  582. function getYAxisTextList(series, opts, config) {
  583. var data = dataCombine(series);
  584. // remove null from data
  585. data = data.filter(function (item) {
  586. return item !== null;
  587. });
  588. var minData = Math.min.apply(this, data);
  589. var maxData = Math.max.apply(this, data);
  590. if (typeof opts.yAxis.min === 'number') {
  591. minData = Math.min(opts.yAxis.min, minData);
  592. }
  593. if (typeof opts.yAxis.max === 'number') {
  594. maxData = Math.max(opts.yAxis.max, maxData);
  595. }
  596. // fix issue https://github.com/xiaolin3303/wx-charts/issues/9
  597. if (minData === maxData) {
  598. var rangeSpan = maxData || 1;
  599. minData -= rangeSpan;
  600. maxData += rangeSpan;
  601. }
  602. var dataRange = getDataRange(minData, maxData);
  603. var minRange = dataRange.minRange;
  604. var maxRange = dataRange.maxRange;
  605. var range = [];
  606. var eachRange = (maxRange - minRange) / config.yAxisSplit;
  607. for (var i = 0; i <= config.yAxisSplit; i++) {
  608. range.push(minRange + eachRange * i);
  609. }
  610. return range.reverse();
  611. }
  612. function calYAxisData(series, opts, config) {
  613. var ranges = getYAxisTextList(series, opts, config);
  614. var yAxisWidth = config.yAxisWidth;
  615. var rangesFormat = ranges.map(function (item) {
  616. item = util.toFixed(item, 2);
  617. item = opts.yAxis.format ? opts.yAxis.format(Number(item)) : item;
  618. yAxisWidth = Math.max(yAxisWidth, measureText(item) + 5);
  619. return item;
  620. });
  621. if (opts.yAxis.disabled === true) {
  622. yAxisWidth = 0;
  623. }
  624. return { rangesFormat: rangesFormat, ranges: ranges, yAxisWidth: yAxisWidth };
  625. }
  626. function drawPointShape(points, color, shape, context) {
  627. context.beginPath();
  628. context.setStrokeStyle("#ffffff");
  629. context.setLineWidth(1);
  630. context.setFillStyle(color);
  631. if (shape === 'diamond') {
  632. points.forEach(function (item, index) {
  633. if (item !== null) {
  634. context.moveTo(item.x, item.y - 4.5);
  635. context.lineTo(item.x - 4.5, item.y);
  636. context.lineTo(item.x, item.y + 4.5);
  637. context.lineTo(item.x + 4.5, item.y);
  638. context.lineTo(item.x, item.y - 4.5);
  639. }
  640. });
  641. } else if (shape === 'circle') {
  642. points.forEach(function (item, index) {
  643. if (item !== null) {
  644. context.moveTo(item.x + 3.5, item.y);
  645. context.arc(item.x, item.y, 4, 0, 2 * Math.PI, false);
  646. }
  647. });
  648. } else if (shape === 'rect') {
  649. points.forEach(function (item, index) {
  650. if (item !== null) {
  651. context.moveTo(item.x - 3.5, item.y - 3.5);
  652. context.rect(item.x - 3.5, item.y - 3.5, 7, 7);
  653. }
  654. });
  655. } else if (shape === 'triangle') {
  656. points.forEach(function (item, index) {
  657. if (item !== null) {
  658. context.moveTo(item.x, item.y - 4.5);
  659. context.lineTo(item.x - 4.5, item.y + 4.5);
  660. context.lineTo(item.x + 4.5, item.y + 4.5);
  661. context.lineTo(item.x, item.y - 4.5);
  662. }
  663. });
  664. }
  665. context.closePath();
  666. context.fill();
  667. context.stroke();
  668. }
  669. function drawRingTitle(opts, config, context) {
  670. var titlefontSize = opts.title.fontSize || config.titleFontSize;
  671. var subtitlefontSize = opts.subtitle.fontSize || config.subtitleFontSize;
  672. var title = opts.title.name || '';
  673. var subtitle = opts.subtitle.name || '';
  674. var titleFontColor = opts.title.color || config.titleColor;
  675. var subtitleFontColor = opts.subtitle.color || config.subtitleColor;
  676. var titleHeight = title ? titlefontSize : 0;
  677. var subtitleHeight = subtitle ? subtitlefontSize : 0;
  678. var margin = 5;
  679. if (subtitle) {
  680. var textWidth = measureText(subtitle, subtitlefontSize);
  681. var startX = (opts.width - textWidth) / 2 + (opts.subtitle.offsetX || 0);
  682. var startY = (opts.height - config.legendHeight + subtitlefontSize) / 2;
  683. if (title) {
  684. startY -= (titleHeight + margin) / 2;
  685. }
  686. context.beginPath();
  687. context.setFontSize(subtitlefontSize);
  688. context.setFillStyle(subtitleFontColor);
  689. context.fillText(subtitle, startX, startY);
  690. context.stroke();
  691. context.closePath();
  692. }
  693. if (title) {
  694. var _textWidth = measureText(title, titlefontSize);
  695. var _startX = (opts.width - _textWidth) / 2 + (opts.title.offsetX || 0);
  696. var _startY = (opts.height - config.legendHeight + titlefontSize) / 2;
  697. if (subtitle) {
  698. _startY += (subtitleHeight + margin) / 2;
  699. }
  700. context.beginPath();
  701. context.setFontSize(titlefontSize);
  702. context.setFillStyle(titleFontColor);
  703. context.fillText(title, _startX, _startY);
  704. context.stroke();
  705. context.closePath();
  706. }
  707. }
  708. function drawPointText(points, series, config, context) {
  709. // 绘制数据文案
  710. var data = series.data;
  711. context.beginPath();
  712. context.setFontSize(config.fontSize);
  713. context.setFillStyle('#666666');
  714. points.forEach(function (item, index) {
  715. if (item !== null) {
  716. var formatVal = series.format ? series.format(data[index]) : data[index];
  717. context.fillText(formatVal, item.x - measureText(formatVal) / 2, item.y - 2);
  718. }
  719. });
  720. context.closePath();
  721. context.stroke();
  722. }
  723. function drawRadarLabel(angleList, radius, centerPosition, opts, config, context) {
  724. var radarOption = opts.extra.radar || {};
  725. radius += config.radarLabelTextMargin;
  726. context.beginPath();
  727. context.setFontSize(config.fontSize);
  728. context.setFillStyle(radarOption.labelColor || '#666666');
  729. angleList.forEach(function (angle, index) {
  730. var pos = {
  731. x: radius * Math.cos(angle),
  732. y: radius * Math.sin(angle)
  733. };
  734. var posRelativeCanvas = convertCoordinateOrigin(pos.x, pos.y, centerPosition);
  735. var startX = posRelativeCanvas.x;
  736. var startY = posRelativeCanvas.y;
  737. if (util.approximatelyEqual(pos.x, 0)) {
  738. startX -= measureText(opts.categories[index] || '') / 2;
  739. } else if (pos.x < 0) {
  740. startX -= measureText(opts.categories[index] || '');
  741. }
  742. context.fillText(opts.categories[index] || '', startX, startY + config.fontSize / 2);
  743. });
  744. context.stroke();
  745. context.closePath();
  746. }
  747. function drawPieText(series, opts, config, context, radius, center) {
  748. var lineRadius = radius + config.pieChartLinePadding;
  749. var textObjectCollection = [];
  750. var lastTextObject = null;
  751. var seriesConvert = series.map(function (item) {
  752. var arc = 2 * Math.PI - (item._start_ + 2 * Math.PI * item._proportion_ / 2);
  753. var text = item.format ? item.format(+item._proportion_.toFixed(2)) : util.toFixed(item._proportion_ * 100) + '%';
  754. var color = item.color;
  755. return { arc: arc, text: text, color: color };
  756. });
  757. seriesConvert.forEach(function (item) {
  758. // line end
  759. var orginX1 = Math.cos(item.arc) * lineRadius;
  760. var orginY1 = Math.sin(item.arc) * lineRadius;
  761. // line start
  762. var orginX2 = Math.cos(item.arc) * radius;
  763. var orginY2 = Math.sin(item.arc) * radius;
  764. // text start
  765. var orginX3 = orginX1 >= 0 ? orginX1 + config.pieChartTextPadding : orginX1 - config.pieChartTextPadding;
  766. var orginY3 = orginY1;
  767. var textWidth = measureText(item.text);
  768. var startY = orginY3;
  769. if (lastTextObject && util.isSameXCoordinateArea(lastTextObject.start, { x: orginX3 })) {
  770. if (orginX3 > 0) {
  771. startY = Math.min(orginY3, lastTextObject.start.y);
  772. } else if (orginX1 < 0) {
  773. startY = Math.max(orginY3, lastTextObject.start.y);
  774. } else {
  775. if (orginY3 > 0) {
  776. startY = Math.max(orginY3, lastTextObject.start.y);
  777. } else {
  778. startY = Math.min(orginY3, lastTextObject.start.y);
  779. }
  780. }
  781. }
  782. if (orginX3 < 0) {
  783. orginX3 -= textWidth;
  784. }
  785. var textObject = {
  786. lineStart: {
  787. x: orginX2,
  788. y: orginY2
  789. },
  790. lineEnd: {
  791. x: orginX1,
  792. y: orginY1
  793. },
  794. start: {
  795. x: orginX3,
  796. y: startY
  797. },
  798. width: textWidth,
  799. height: config.fontSize,
  800. text: item.text,
  801. color: item.color
  802. };
  803. lastTextObject = avoidCollision(textObject, lastTextObject);
  804. textObjectCollection.push(lastTextObject);
  805. });
  806. textObjectCollection.forEach(function (item) {
  807. var lineStartPoistion = convertCoordinateOrigin(item.lineStart.x, item.lineStart.y, center);
  808. var lineEndPoistion = convertCoordinateOrigin(item.lineEnd.x, item.lineEnd.y, center);
  809. var textPosition = convertCoordinateOrigin(item.start.x, item.start.y, center);
  810. context.setLineWidth(1);
  811. context.setFontSize(config.fontSize);
  812. context.beginPath();
  813. context.setStrokeStyle(item.color);
  814. context.setFillStyle(item.color);
  815. context.moveTo(lineStartPoistion.x, lineStartPoistion.y);
  816. var curveStartX = item.start.x < 0 ? textPosition.x + item.width : textPosition.x;
  817. var textStartX = item.start.x < 0 ? textPosition.x - 5 : textPosition.x + 5;
  818. context.quadraticCurveTo(lineEndPoistion.x, lineEndPoistion.y, curveStartX, textPosition.y);
  819. context.moveTo(lineStartPoistion.x, lineStartPoistion.y);
  820. context.stroke();
  821. context.closePath();
  822. context.beginPath();
  823. context.moveTo(textPosition.x + item.width, textPosition.y);
  824. context.arc(curveStartX, textPosition.y, 2, 0, 2 * Math.PI);
  825. context.closePath();
  826. context.fill();
  827. context.beginPath();
  828. context.setFillStyle('#666666');
  829. context.fillText(item.text, textStartX, textPosition.y + 3);
  830. context.closePath();
  831. context.stroke();
  832. context.closePath();
  833. });
  834. }
  835. function drawToolTipSplitLine(offsetX, opts, config, context) {
  836. var startY = config.padding;
  837. var endY = opts.height - config.padding - config.xAxisHeight - config.legendHeight;
  838. context.beginPath();
  839. context.setStrokeStyle('#cccccc');
  840. context.setLineWidth(1);
  841. context.moveTo(offsetX, startY);
  842. context.lineTo(offsetX, endY);
  843. context.stroke();
  844. context.closePath();
  845. }
  846. function drawToolTip(textList, offset, opts, config, context) {
  847. var legendWidth = 4;
  848. var legendMarginRight = 5;
  849. var arrowWidth = 8;
  850. var isOverRightBorder = false;
  851. offset = assign({
  852. x: 0,
  853. y: 0
  854. }, offset);
  855. offset.y -= 8;
  856. var textWidth = textList.map(function (item) {
  857. return measureText(item.text);
  858. });
  859. var toolTipWidth = legendWidth + legendMarginRight + 4 * config.toolTipPadding + Math.max.apply(null, textWidth);
  860. var toolTipHeight = 2 * config.toolTipPadding + textList.length * config.toolTipLineHeight;
  861. // if beyond the right border
  862. if (offset.x - Math.abs(opts._scrollDistance_) + arrowWidth + toolTipWidth > opts.width) {
  863. isOverRightBorder = true;
  864. }
  865. // draw background rect
  866. context.beginPath();
  867. context.setFillStyle(opts.tooltip.option.background || config.toolTipBackground);
  868. context.setGlobalAlpha(config.toolTipOpacity);
  869. if (isOverRightBorder) {
  870. context.moveTo(offset.x, offset.y + 10);
  871. context.lineTo(offset.x - arrowWidth, offset.y + 10 - 5);
  872. context.lineTo(offset.x - arrowWidth, offset.y + 10 + 5);
  873. context.moveTo(offset.x, offset.y + 10);
  874. context.fillRect(offset.x - toolTipWidth - arrowWidth, offset.y, toolTipWidth, toolTipHeight);
  875. } else {
  876. context.moveTo(offset.x, offset.y + 10);
  877. context.lineTo(offset.x + arrowWidth, offset.y + 10 - 5);
  878. context.lineTo(offset.x + arrowWidth, offset.y + 10 + 5);
  879. context.moveTo(offset.x, offset.y + 10);
  880. context.fillRect(offset.x + arrowWidth, offset.y, toolTipWidth, toolTipHeight);
  881. }
  882. context.closePath();
  883. context.fill();
  884. context.setGlobalAlpha(1);
  885. // draw legend
  886. textList.forEach(function (item, index) {
  887. context.beginPath();
  888. context.setFillStyle(item.color);
  889. var startX = offset.x + arrowWidth + 2 * config.toolTipPadding;
  890. var startY = offset.y + (config.toolTipLineHeight - config.fontSize) / 2 + config.toolTipLineHeight * index + config.toolTipPadding;
  891. if (isOverRightBorder) {
  892. startX = offset.x - toolTipWidth - arrowWidth + 2 * config.toolTipPadding;
  893. }
  894. context.fillRect(startX, startY, legendWidth, config.fontSize);
  895. context.closePath();
  896. });
  897. // draw text list
  898. context.beginPath();
  899. context.setFontSize(config.fontSize);
  900. context.setFillStyle('#ffffff');
  901. textList.forEach(function (item, index) {
  902. var startX = offset.x + arrowWidth + 2 * config.toolTipPadding + legendWidth + legendMarginRight;
  903. if (isOverRightBorder) {
  904. startX = offset.x - toolTipWidth - arrowWidth + 2 * config.toolTipPadding + +legendWidth + legendMarginRight;
  905. }
  906. var startY = offset.y + (config.toolTipLineHeight - config.fontSize) / 2 + config.toolTipLineHeight * index + config.toolTipPadding;
  907. context.fillText(item.text, startX, startY + config.fontSize);
  908. });
  909. context.stroke();
  910. context.closePath();
  911. }
  912. function drawYAxisTitle(title, opts, config, context) {
  913. var startX = config.xAxisHeight + (opts.height - config.xAxisHeight - measureText(title)) / 2;
  914. context.save();
  915. context.beginPath();
  916. context.setFontSize(config.fontSize);
  917. context.setFillStyle(opts.yAxis.titleFontColor || '#333333');
  918. context.translate(0, opts.height);
  919. context.rotate(-90 * Math.PI / 180);
  920. context.fillText(title, startX, config.padding + 0.5 * config.fontSize);
  921. context.stroke();
  922. context.closePath();
  923. context.restore();
  924. }
  925. function drawColumnDataPoints(series, opts, config, context) {
  926. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  927. var _calYAxisData = calYAxisData(series, opts, config),
  928. ranges = _calYAxisData.ranges;
  929. var _getXAxisPoints = getXAxisPoints(opts.categories, opts, config),
  930. xAxisPoints = _getXAxisPoints.xAxisPoints,
  931. eachSpacing = _getXAxisPoints.eachSpacing;
  932. var minRange = ranges.pop();
  933. var maxRange = ranges.shift();
  934. context.save();
  935. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  936. context.translate(opts._scrollDistance_, 0);
  937. }
  938. series.forEach(function (eachSeries, seriesIndex) {
  939. var data = eachSeries.data;
  940. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  941. points = fixColumeData(points, eachSpacing, series.length, seriesIndex, config, opts);
  942. // 绘制柱状数据图
  943. context.beginPath();
  944. context.setFillStyle(eachSeries.color);
  945. points.forEach(function (item, index) {
  946. if (item !== null) {
  947. var startX = item.x - item.width / 2 + 1;
  948. var height = opts.height - item.y - config.padding - config.xAxisHeight - config.legendHeight;
  949. context.moveTo(startX, item.y);
  950. context.rect(startX, item.y, item.width - 2, height);
  951. }
  952. });
  953. context.closePath();
  954. context.fill();
  955. });
  956. series.forEach(function (eachSeries, seriesIndex) {
  957. var data = eachSeries.data;
  958. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  959. points = fixColumeData(points, eachSpacing, series.length, seriesIndex, config, opts);
  960. if (opts.dataLabel !== false && process === 1) {
  961. drawPointText(points, eachSeries, config, context);
  962. }
  963. });
  964. context.restore();
  965. return {
  966. xAxisPoints: xAxisPoints,
  967. eachSpacing: eachSpacing
  968. };
  969. }
  970. function drawAreaDataPoints(series, opts, config, context) {
  971. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  972. var _calYAxisData2 = calYAxisData(series, opts, config),
  973. ranges = _calYAxisData2.ranges;
  974. var _getXAxisPoints2 = getXAxisPoints(opts.categories, opts, config),
  975. xAxisPoints = _getXAxisPoints2.xAxisPoints,
  976. eachSpacing = _getXAxisPoints2.eachSpacing;
  977. var minRange = ranges.pop();
  978. var maxRange = ranges.shift();
  979. var endY = opts.height - config.padding - config.xAxisHeight - config.legendHeight;
  980. var calPoints = [];
  981. context.save();
  982. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  983. context.translate(opts._scrollDistance_, 0);
  984. }
  985. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  986. drawToolTipSplitLine(opts.tooltip.offset.x, opts, config, context);
  987. }
  988. series.forEach(function (eachSeries, seriesIndex) {
  989. var data = eachSeries.data;
  990. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  991. calPoints.push(points);
  992. var splitPointList = splitPoints(points);
  993. splitPointList.forEach(function (points) {
  994. // 绘制区域数据
  995. context.beginPath();
  996. context.setStrokeStyle(eachSeries.color);
  997. context.setFillStyle(eachSeries.color);
  998. context.setGlobalAlpha(0.6);
  999. context.setLineWidth(2);
  1000. if (points.length > 1) {
  1001. var firstPoint = points[0];
  1002. var lastPoint = points[points.length - 1];
  1003. context.moveTo(firstPoint.x, firstPoint.y);
  1004. if (opts.extra.lineStyle === 'curve') {
  1005. points.forEach(function (item, index) {
  1006. if (index > 0) {
  1007. var ctrlPoint = createCurveControlPoints(points, index - 1);
  1008. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item.y);
  1009. }
  1010. });
  1011. } else {
  1012. points.forEach(function (item, index) {
  1013. if (index > 0) {
  1014. context.lineTo(item.x, item.y);
  1015. }
  1016. });
  1017. }
  1018. context.lineTo(lastPoint.x, endY);
  1019. context.lineTo(firstPoint.x, endY);
  1020. context.lineTo(firstPoint.x, firstPoint.y);
  1021. } else {
  1022. var item = points[0];
  1023. context.moveTo(item.x - eachSpacing / 2, item.y);
  1024. context.lineTo(item.x + eachSpacing / 2, item.y);
  1025. context.lineTo(item.x + eachSpacing / 2, endY);
  1026. context.lineTo(item.x - eachSpacing / 2, endY);
  1027. context.moveTo(item.x - eachSpacing / 2, item.y);
  1028. }
  1029. context.closePath();
  1030. context.fill();
  1031. context.setGlobalAlpha(1);
  1032. });
  1033. if (opts.dataPointShape !== false) {
  1034. var shape = config.dataPointShape[seriesIndex % config.dataPointShape.length];
  1035. drawPointShape(points, eachSeries.color, shape, context);
  1036. }
  1037. });
  1038. if (opts.dataLabel !== false && process === 1) {
  1039. series.forEach(function (eachSeries, seriesIndex) {
  1040. var data = eachSeries.data;
  1041. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  1042. drawPointText(points, eachSeries, config, context);
  1043. });
  1044. }
  1045. context.restore();
  1046. return {
  1047. xAxisPoints: xAxisPoints,
  1048. calPoints: calPoints,
  1049. eachSpacing: eachSpacing
  1050. };
  1051. }
  1052. function drawLineDataPoints(series, opts, config, context) {
  1053. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  1054. var _calYAxisData3 = calYAxisData(series, opts, config),
  1055. ranges = _calYAxisData3.ranges;
  1056. var _getXAxisPoints3 = getXAxisPoints(opts.categories, opts, config),
  1057. xAxisPoints = _getXAxisPoints3.xAxisPoints,
  1058. eachSpacing = _getXAxisPoints3.eachSpacing;
  1059. var minRange = ranges.pop();
  1060. var maxRange = ranges.shift();
  1061. var calPoints = [];
  1062. context.save();
  1063. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  1064. context.translate(opts._scrollDistance_, 0);
  1065. }
  1066. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  1067. drawToolTipSplitLine(opts.tooltip.offset.x, opts, config, context);
  1068. }
  1069. series.forEach(function (eachSeries, seriesIndex) {
  1070. var data = eachSeries.data;
  1071. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  1072. calPoints.push(points);
  1073. var splitPointList = splitPoints(points);
  1074. splitPointList.forEach(function (points, index) {
  1075. context.beginPath();
  1076. context.setStrokeStyle(eachSeries.color);
  1077. context.setLineWidth(2);
  1078. if (points.length === 1) {
  1079. context.moveTo(points[0].x, points[0].y);
  1080. context.arc(points[0].x, points[0].y, 1, 0, 2 * Math.PI);
  1081. } else {
  1082. context.moveTo(points[0].x, points[0].y);
  1083. if (opts.extra.lineStyle === 'curve') {
  1084. points.forEach(function (item, index) {
  1085. if (index > 0) {
  1086. var ctrlPoint = createCurveControlPoints(points, index - 1);
  1087. context.bezierCurveTo(ctrlPoint.ctrA.x, ctrlPoint.ctrA.y, ctrlPoint.ctrB.x, ctrlPoint.ctrB.y, item.x, item.y);
  1088. }
  1089. });
  1090. } else {
  1091. points.forEach(function (item, index) {
  1092. if (index > 0) {
  1093. context.lineTo(item.x, item.y);
  1094. }
  1095. });
  1096. }
  1097. context.moveTo(points[0].x, points[0].y);
  1098. }
  1099. context.closePath();
  1100. context.stroke();
  1101. });
  1102. if (opts.dataPointShape !== false) {
  1103. var shape = config.dataPointShape[seriesIndex % config.dataPointShape.length];
  1104. drawPointShape(points, eachSeries.color, shape, context);
  1105. }
  1106. });
  1107. if (opts.dataLabel !== false && process === 1) {
  1108. series.forEach(function (eachSeries, seriesIndex) {
  1109. var data = eachSeries.data;
  1110. var points = getDataPoints(data, minRange, maxRange, xAxisPoints, eachSpacing, opts, config, process);
  1111. drawPointText(points, eachSeries, config, context);
  1112. });
  1113. }
  1114. context.restore();
  1115. return {
  1116. xAxisPoints: xAxisPoints,
  1117. calPoints: calPoints,
  1118. eachSpacing: eachSpacing
  1119. };
  1120. }
  1121. function drawToolTipBridge(opts, config, context, process) {
  1122. context.save();
  1123. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0 && opts.enableScroll === true) {
  1124. context.translate(opts._scrollDistance_, 0);
  1125. }
  1126. if (opts.tooltip && opts.tooltip.textList && opts.tooltip.textList.length && process === 1) {
  1127. drawToolTip(opts.tooltip.textList, opts.tooltip.offset, opts, config, context);
  1128. }
  1129. context.restore();
  1130. }
  1131. function drawXAxis(categories, opts, config, context) {
  1132. var _getXAxisPoints4 = getXAxisPoints(categories, opts, config),
  1133. xAxisPoints = _getXAxisPoints4.xAxisPoints,
  1134. startX = _getXAxisPoints4.startX,
  1135. endX = _getXAxisPoints4.endX,
  1136. eachSpacing = _getXAxisPoints4.eachSpacing;
  1137. var startY = opts.height - config.padding - config.xAxisHeight - config.legendHeight;
  1138. var endY = startY + config.xAxisLineHeight;
  1139. context.save();
  1140. if (opts._scrollDistance_ && opts._scrollDistance_ !== 0) {
  1141. context.translate(opts._scrollDistance_, 0);
  1142. }
  1143. context.beginPath();
  1144. context.setStrokeStyle(opts.xAxis.gridColor || "#cccccc");
  1145. if (opts.xAxis.disableGrid !== true) {
  1146. if (opts.xAxis.type === 'calibration') {
  1147. xAxisPoints.forEach(function (item, index) {
  1148. if (index > 0) {
  1149. context.moveTo(item - eachSpacing / 2, startY);
  1150. context.lineTo(item - eachSpacing / 2, startY + 4);
  1151. }
  1152. });
  1153. } else {
  1154. xAxisPoints.forEach(function (item, index) {
  1155. context.moveTo(item, startY);
  1156. context.lineTo(item, endY);
  1157. });
  1158. }
  1159. }
  1160. context.closePath();
  1161. context.stroke();
  1162. // 对X轴列表做抽稀处理
  1163. var validWidth = opts.width - 2 * config.padding - config.yAxisWidth - config.yAxisTitleWidth;
  1164. var maxXAxisListLength = Math.min(categories.length, Math.ceil(validWidth / config.fontSize / 1.5));
  1165. var ratio = Math.ceil(categories.length / maxXAxisListLength);
  1166. categories = categories.map(function (item, index) {
  1167. return index % ratio !== 0 ? '' : item;
  1168. });
  1169. if (config._xAxisTextAngle_ === 0) {
  1170. context.beginPath();
  1171. context.setFontSize(config.fontSize);
  1172. context.setFillStyle(opts.xAxis.fontColor || '#666666');
  1173. categories.forEach(function (item, index) {
  1174. var offset = eachSpacing / 2 - measureText(item) / 2;
  1175. context.fillText(item, xAxisPoints[index] + offset, startY + config.fontSize + 5);
  1176. });
  1177. context.closePath();
  1178. context.stroke();
  1179. } else {
  1180. categories.forEach(function (item, index) {
  1181. context.save();
  1182. context.beginPath();
  1183. context.setFontSize(config.fontSize);
  1184. context.setFillStyle(opts.xAxis.fontColor || '#666666');
  1185. var textWidth = measureText(item);
  1186. var offset = eachSpacing / 2 - textWidth;
  1187. var _calRotateTranslate = calRotateTranslate(xAxisPoints[index] + eachSpacing / 2, startY + config.fontSize / 2 + 5, opts.height),
  1188. transX = _calRotateTranslate.transX,
  1189. transY = _calRotateTranslate.transY;
  1190. context.rotate(-1 * config._xAxisTextAngle_);
  1191. context.translate(transX, transY);
  1192. context.fillText(item, xAxisPoints[index] + offset, startY + config.fontSize + 5);
  1193. context.closePath();
  1194. context.stroke();
  1195. context.restore();
  1196. });
  1197. }
  1198. context.restore();
  1199. }
  1200. function drawYAxisGrid(opts, config, context) {
  1201. var spacingValid = opts.height - 2 * config.padding - config.xAxisHeight - config.legendHeight;
  1202. var eachSpacing = Math.floor(spacingValid / config.yAxisSplit);
  1203. var yAxisTotalWidth = config.yAxisWidth + config.yAxisTitleWidth;
  1204. var startX = config.padding + yAxisTotalWidth;
  1205. var endX = opts.width - config.padding;
  1206. var points = [];
  1207. for (var i = 0; i < config.yAxisSplit; i++) {
  1208. points.push(config.padding + eachSpacing * i);
  1209. }
  1210. points.push(config.padding + eachSpacing * config.yAxisSplit + 2);
  1211. context.beginPath();
  1212. context.setStrokeStyle(opts.yAxis.gridColor || "#cccccc");
  1213. context.setLineWidth(1);
  1214. points.forEach(function (item, index) {
  1215. context.moveTo(startX, item);
  1216. context.lineTo(endX, item);
  1217. });
  1218. context.closePath();
  1219. context.stroke();
  1220. }
  1221. function drawYAxis(series, opts, config, context) {
  1222. if (opts.yAxis.disabled === true) {
  1223. return;
  1224. }
  1225. var _calYAxisData4 = calYAxisData(series, opts, config),
  1226. rangesFormat = _calYAxisData4.rangesFormat;
  1227. var yAxisTotalWidth = config.yAxisWidth + config.yAxisTitleWidth;
  1228. var spacingValid = opts.height - 2 * config.padding - config.xAxisHeight - config.legendHeight;
  1229. var eachSpacing = Math.floor(spacingValid / config.yAxisSplit);
  1230. var startX = config.padding + yAxisTotalWidth;
  1231. var endX = opts.width - config.padding;
  1232. var endY = opts.height - config.padding - config.xAxisHeight - config.legendHeight;
  1233. // set YAxis background
  1234. context.setFillStyle(opts.background || '#ffffff');
  1235. if (opts._scrollDistance_ < 0) {
  1236. context.fillRect(0, 0, startX, endY + config.xAxisHeight + 5);
  1237. }
  1238. context.fillRect(endX, 0, opts.width, endY + config.xAxisHeight + 5);
  1239. var points = [];
  1240. for (var i = 0; i <= config.yAxisSplit; i++) {
  1241. points.push(config.padding + eachSpacing * i);
  1242. }
  1243. context.stroke();
  1244. context.beginPath();
  1245. context.setFontSize(config.fontSize);
  1246. context.setFillStyle(opts.yAxis.fontColor || '#666666');
  1247. rangesFormat.forEach(function (item, index) {
  1248. var pos = points[index] ? points[index] : endY;
  1249. context.fillText(item, config.padding + config.yAxisTitleWidth, pos + config.fontSize / 2);
  1250. });
  1251. context.closePath();
  1252. context.stroke();
  1253. if (opts.yAxis.title) {
  1254. drawYAxisTitle(opts.yAxis.title, opts, config, context);
  1255. }
  1256. }
  1257. function drawLegend(series, opts, config, context) {
  1258. if (!opts.legend) {
  1259. return;
  1260. }
  1261. // each legend shape width 15px
  1262. // the spacing between shape and text in each legend is the `padding`
  1263. // each legend spacing is the `padding`
  1264. // legend margin top `config.padding`
  1265. var _calLegendData = calLegendData(series, opts, config),
  1266. legendList = _calLegendData.legendList;
  1267. var padding = 5;
  1268. var marginTop = 8;
  1269. var shapeWidth = 15;
  1270. legendList.forEach(function (itemList, listIndex) {
  1271. var width = 0;
  1272. itemList.forEach(function (item) {
  1273. item.name = item.name || 'undefined';
  1274. width += 3 * padding + measureText(item.name) + shapeWidth;
  1275. });
  1276. var startX = (opts.width - width) / 2 + padding;
  1277. var startY = opts.height - config.padding - config.legendHeight + listIndex * (config.fontSize + marginTop) + padding + marginTop;
  1278. context.setFontSize(config.fontSize);
  1279. itemList.forEach(function (item) {
  1280. switch (opts.type) {
  1281. case 'line':
  1282. context.beginPath();
  1283. context.setLineWidth(1);
  1284. context.setStrokeStyle(item.color);
  1285. context.moveTo(startX - 2, startY + 5);
  1286. context.lineTo(startX + 17, startY + 5);
  1287. context.stroke();
  1288. context.closePath();
  1289. context.beginPath();
  1290. context.setLineWidth(1);
  1291. context.setStrokeStyle('#ffffff');
  1292. context.setFillStyle(item.color);
  1293. context.moveTo(startX + 7.5, startY + 5);
  1294. context.arc(startX + 7.5, startY + 5, 4, 0, 2 * Math.PI);
  1295. context.fill();
  1296. context.stroke();
  1297. context.closePath();
  1298. break;
  1299. case 'pie':
  1300. case 'ring':
  1301. context.beginPath();
  1302. context.setFillStyle(item.color);
  1303. context.moveTo(startX + 7.5, startY + 5);
  1304. context.arc(startX + 7.5, startY + 5, 7, 0, 2 * Math.PI);
  1305. context.closePath();
  1306. context.fill();
  1307. break;
  1308. default:
  1309. context.beginPath();
  1310. context.setFillStyle(item.color);
  1311. context.moveTo(startX, startY);
  1312. context.rect(startX, startY, 15, 10);
  1313. context.closePath();
  1314. context.fill();
  1315. }
  1316. startX += padding + shapeWidth;
  1317. context.beginPath();
  1318. context.setFillStyle(opts.extra.legendTextColor || '#333333');
  1319. context.fillText(item.name, startX, startY + 9);
  1320. context.closePath();
  1321. context.stroke();
  1322. startX += measureText(item.name) + 2 * padding;
  1323. });
  1324. });
  1325. }
  1326. function drawPieDataPoints(series, opts, config, context) {
  1327. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  1328. var pieOption = opts.extra.pie || {};
  1329. series = getPieDataPoints(series, process);
  1330. var centerPosition = {
  1331. x: opts.width / 2,
  1332. y: (opts.height - config.legendHeight) / 2
  1333. };
  1334. var radius = Math.min(centerPosition.x - config.pieChartLinePadding - config.pieChartTextPadding - config._pieTextMaxLength_, centerPosition.y - config.pieChartLinePadding - config.pieChartTextPadding);
  1335. if (opts.dataLabel) {
  1336. radius -= 10;
  1337. } else {
  1338. radius -= 2 * config.padding;
  1339. }
  1340. series = series.map(function (eachSeries) {
  1341. eachSeries._start_ += (pieOption.offsetAngle || 0) * Math.PI / 180;
  1342. return eachSeries;
  1343. });
  1344. series.forEach(function (eachSeries) {
  1345. context.beginPath();
  1346. context.setLineWidth(2);
  1347. context.setStrokeStyle('#ffffff');
  1348. context.setFillStyle(eachSeries.color);
  1349. context.moveTo(centerPosition.x, centerPosition.y);
  1350. context.arc(centerPosition.x, centerPosition.y, radius, eachSeries._start_, eachSeries._start_ + 2 * eachSeries._proportion_ * Math.PI);
  1351. context.closePath();
  1352. context.fill();
  1353. if (opts.disablePieStroke !== true) {
  1354. context.stroke();
  1355. }
  1356. });
  1357. if (opts.type === 'ring') {
  1358. var innerPieWidth = radius * 0.6;
  1359. if (typeof opts.extra.ringWidth === 'number' && opts.extra.ringWidth > 0) {
  1360. innerPieWidth = Math.max(0, radius - opts.extra.ringWidth);
  1361. }
  1362. context.beginPath();
  1363. context.setFillStyle(opts.background || '#ffffff');
  1364. context.moveTo(centerPosition.x, centerPosition.y);
  1365. context.arc(centerPosition.x, centerPosition.y, innerPieWidth, 0, 2 * Math.PI);
  1366. context.closePath();
  1367. context.fill();
  1368. }
  1369. if (opts.dataLabel !== false && process === 1) {
  1370. // fix https://github.com/xiaolin3303/wx-charts/issues/132
  1371. var valid = false;
  1372. for (var i = 0, len = series.length; i < len; i++) {
  1373. if (series[i].data > 0) {
  1374. valid = true;
  1375. break;
  1376. }
  1377. }
  1378. if (valid) {
  1379. drawPieText(series, opts, config, context, radius, centerPosition);
  1380. }
  1381. }
  1382. if (process === 1 && opts.type === 'ring') {
  1383. drawRingTitle(opts, config, context);
  1384. }
  1385. return {
  1386. center: centerPosition,
  1387. radius: radius,
  1388. series: series
  1389. };
  1390. }
  1391. function drawRadarDataPoints(series, opts, config, context) {
  1392. var process = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 1;
  1393. var radarOption = opts.extra.radar || {};
  1394. var coordinateAngle = getRadarCoordinateSeries(opts.categories.length);
  1395. var centerPosition = {
  1396. x: opts.width / 2,
  1397. y: (opts.height - config.legendHeight) / 2
  1398. };
  1399. var radius = Math.min(centerPosition.x - (getMaxTextListLength(opts.categories) + config.radarLabelTextMargin), centerPosition.y - config.radarLabelTextMargin);
  1400. radius -= config.padding;
  1401. // draw grid
  1402. context.beginPath();
  1403. context.setLineWidth(1);
  1404. context.setStrokeStyle(radarOption.gridColor || "#cccccc");
  1405. coordinateAngle.forEach(function (angle) {
  1406. var pos = convertCoordinateOrigin(radius * Math.cos(angle), radius * Math.sin(angle), centerPosition);
  1407. context.moveTo(centerPosition.x, centerPosition.y);
  1408. context.lineTo(pos.x, pos.y);
  1409. });
  1410. context.stroke();
  1411. context.closePath();
  1412. // draw split line grid
  1413. var _loop = function _loop(i) {
  1414. var startPos = {};
  1415. context.beginPath();
  1416. context.setLineWidth(1);
  1417. context.setStrokeStyle(radarOption.gridColor || "#cccccc");
  1418. coordinateAngle.forEach(function (angle, index) {
  1419. var pos = convertCoordinateOrigin(radius / config.radarGridCount * i * Math.cos(angle), radius / config.radarGridCount * i * Math.sin(angle), centerPosition);
  1420. if (index === 0) {
  1421. startPos = pos;
  1422. context.moveTo(pos.x, pos.y);
  1423. } else {
  1424. context.lineTo(pos.x, pos.y);
  1425. }
  1426. });
  1427. context.lineTo(startPos.x, startPos.y);
  1428. context.stroke();
  1429. context.closePath();
  1430. };
  1431. for (var i = 1; i <= config.radarGridCount; i++) {
  1432. _loop(i);
  1433. }
  1434. var radarDataPoints = getRadarDataPoints(coordinateAngle, centerPosition, radius, series, opts, process);
  1435. radarDataPoints.forEach(function (eachSeries, seriesIndex) {
  1436. // 绘制区域数据
  1437. context.beginPath();
  1438. context.setFillStyle(eachSeries.color);
  1439. context.setGlobalAlpha(0.6);
  1440. eachSeries.data.forEach(function (item, index) {
  1441. if (index === 0) {
  1442. context.moveTo(item.position.x, item.position.y);
  1443. } else {
  1444. context.lineTo(item.position.x, item.position.y);
  1445. }
  1446. });
  1447. context.closePath();
  1448. context.fill();
  1449. context.setGlobalAlpha(1);
  1450. if (opts.dataPointShape !== false) {
  1451. var shape = config.dataPointShape[seriesIndex % config.dataPointShape.length];
  1452. var points = eachSeries.data.map(function (item) {
  1453. return item.position;
  1454. });
  1455. drawPointShape(points, eachSeries.color, shape, context);
  1456. }
  1457. });
  1458. // draw label text
  1459. drawRadarLabel(coordinateAngle, radius, centerPosition, opts, config, context);
  1460. return {
  1461. center: centerPosition,
  1462. radius: radius,
  1463. angleList: coordinateAngle
  1464. };
  1465. }
  1466. function drawCanvas(opts, context) {
  1467. context.draw();
  1468. }
  1469. var Timing = {
  1470. easeIn: function easeIn(pos) {
  1471. return Math.pow(pos, 3);
  1472. },
  1473. easeOut: function easeOut(pos) {
  1474. return Math.pow(pos - 1, 3) + 1;
  1475. },
  1476. easeInOut: function easeInOut(pos) {
  1477. if ((pos /= 0.5) < 1) {
  1478. return 0.5 * Math.pow(pos, 3);
  1479. } else {
  1480. return 0.5 * (Math.pow(pos - 2, 3) + 2);
  1481. }
  1482. },
  1483. linear: function linear(pos) {
  1484. return pos;
  1485. }
  1486. };
  1487. function Animation(opts) {
  1488. this.isStop = false;
  1489. opts.duration = typeof opts.duration === 'undefined' ? 1000 : opts.duration;
  1490. opts.timing = opts.timing || 'linear';
  1491. var delay = 17;
  1492. var createAnimationFrame = function createAnimationFrame() {
  1493. if (typeof requestAnimationFrame !== 'undefined') {
  1494. return requestAnimationFrame;
  1495. } else if (typeof setTimeout !== 'undefined') {
  1496. return function (step, delay) {
  1497. setTimeout(function () {
  1498. var timeStamp = +new Date();
  1499. step(timeStamp);
  1500. }, delay);
  1501. };
  1502. } else {
  1503. return function (step) {
  1504. step(null);
  1505. };
  1506. }
  1507. };
  1508. var animationFrame = createAnimationFrame();
  1509. var startTimeStamp = null;
  1510. var _step = function step(timestamp) {
  1511. if (timestamp === null || this.isStop === true) {
  1512. opts.onProcess && opts.onProcess(1);
  1513. opts.onAnimationFinish && opts.onAnimationFinish();
  1514. return;
  1515. }
  1516. if (startTimeStamp === null) {
  1517. startTimeStamp = timestamp;
  1518. }
  1519. if (timestamp - startTimeStamp < opts.duration) {
  1520. var process = (timestamp - startTimeStamp) / opts.duration;
  1521. var timingFunction = Timing[opts.timing];
  1522. process = timingFunction(process);
  1523. opts.onProcess && opts.onProcess(process);
  1524. animationFrame(_step, delay);
  1525. } else {
  1526. opts.onProcess && opts.onProcess(1);
  1527. opts.onAnimationFinish && opts.onAnimationFinish();
  1528. }
  1529. };
  1530. _step = _step.bind(this);
  1531. animationFrame(_step, delay);
  1532. }
  1533. // stop animation immediately
  1534. // and tigger onAnimationFinish
  1535. Animation.prototype.stop = function () {
  1536. this.isStop = true;
  1537. };
  1538. function drawCharts(type, opts, config, context) {
  1539. var _this = this;
  1540. var series = opts.series;
  1541. var categories = opts.categories;
  1542. series = fillSeriesColor(series, config);
  1543. var _calLegendData = calLegendData(series, opts, config),
  1544. legendHeight = _calLegendData.legendHeight;
  1545. config.legendHeight = legendHeight;
  1546. var _calYAxisData = calYAxisData(series, opts, config),
  1547. yAxisWidth = _calYAxisData.yAxisWidth;
  1548. config.yAxisWidth = yAxisWidth;
  1549. if (categories && categories.length) {
  1550. var _calCategoriesData = calCategoriesData(categories, opts, config),
  1551. xAxisHeight = _calCategoriesData.xAxisHeight,
  1552. angle = _calCategoriesData.angle;
  1553. config.xAxisHeight = xAxisHeight;
  1554. config._xAxisTextAngle_ = angle;
  1555. }
  1556. if (type === 'pie' || type === 'ring') {
  1557. config._pieTextMaxLength_ = opts.dataLabel === false ? 0 : getPieTextMaxLength(series);
  1558. }
  1559. var duration = opts.animation ? 1000 : 0;
  1560. this.animationInstance && this.animationInstance.stop();
  1561. switch (type) {
  1562. case 'line':
  1563. this.animationInstance = new Animation({
  1564. timing: 'easeIn',
  1565. duration: duration,
  1566. onProcess: function onProcess(process) {
  1567. drawYAxisGrid(opts, config, context);
  1568. var _drawLineDataPoints = drawLineDataPoints(series, opts, config, context, process),
  1569. xAxisPoints = _drawLineDataPoints.xAxisPoints,
  1570. calPoints = _drawLineDataPoints.calPoints,
  1571. eachSpacing = _drawLineDataPoints.eachSpacing;
  1572. _this.chartData.xAxisPoints = xAxisPoints;
  1573. _this.chartData.calPoints = calPoints;
  1574. _this.chartData.eachSpacing = eachSpacing;
  1575. drawXAxis(categories, opts, config, context);
  1576. drawLegend(opts.series, opts, config, context);
  1577. drawYAxis(series, opts, config, context);
  1578. drawToolTipBridge(opts, config, context, process);
  1579. drawCanvas(opts, context);
  1580. },
  1581. onAnimationFinish: function onAnimationFinish() {
  1582. _this.event.trigger('renderComplete');
  1583. }
  1584. });
  1585. break;
  1586. case 'column':
  1587. this.animationInstance = new Animation({
  1588. timing: 'easeIn',
  1589. duration: duration,
  1590. onProcess: function onProcess(process) {
  1591. drawYAxisGrid(opts, config, context);
  1592. var _drawColumnDataPoints = drawColumnDataPoints(series, opts, config, context, process),
  1593. xAxisPoints = _drawColumnDataPoints.xAxisPoints,
  1594. eachSpacing = _drawColumnDataPoints.eachSpacing;
  1595. _this.chartData.xAxisPoints = xAxisPoints;
  1596. _this.chartData.eachSpacing = eachSpacing;
  1597. drawXAxis(categories, opts, config, context);
  1598. drawLegend(opts.series, opts, config, context);
  1599. drawYAxis(series, opts, config, context);
  1600. drawCanvas(opts, context);
  1601. },
  1602. onAnimationFinish: function onAnimationFinish() {
  1603. _this.event.trigger('renderComplete');
  1604. }
  1605. });
  1606. break;
  1607. case 'area':
  1608. this.animationInstance = new Animation({
  1609. timing: 'easeIn',
  1610. duration: duration,
  1611. onProcess: function onProcess(process) {
  1612. drawYAxisGrid(opts, config, context);
  1613. var _drawAreaDataPoints = drawAreaDataPoints(series, opts, config, context, process),
  1614. xAxisPoints = _drawAreaDataPoints.xAxisPoints,
  1615. calPoints = _drawAreaDataPoints.calPoints,
  1616. eachSpacing = _drawAreaDataPoints.eachSpacing;
  1617. _this.chartData.xAxisPoints = xAxisPoints;
  1618. _this.chartData.calPoints = calPoints;
  1619. _this.chartData.eachSpacing = eachSpacing;
  1620. drawXAxis(categories, opts, config, context);
  1621. drawLegend(opts.series, opts, config, context);
  1622. drawYAxis(series, opts, config, context);
  1623. drawToolTipBridge(opts, config, context, process);
  1624. drawCanvas(opts, context);
  1625. },
  1626. onAnimationFinish: function onAnimationFinish() {
  1627. _this.event.trigger('renderComplete');
  1628. }
  1629. });
  1630. break;
  1631. case 'ring':
  1632. case 'pie':
  1633. this.animationInstance = new Animation({
  1634. timing: 'easeInOut',
  1635. duration: duration,
  1636. onProcess: function onProcess(process) {
  1637. _this.chartData.pieData = drawPieDataPoints(series, opts, config, context, process);
  1638. drawLegend(opts.series, opts, config, context);
  1639. drawCanvas(opts, context);
  1640. },
  1641. onAnimationFinish: function onAnimationFinish() {
  1642. _this.event.trigger('renderComplete');
  1643. }
  1644. });
  1645. break;
  1646. case 'radar':
  1647. this.animationInstance = new Animation({
  1648. timing: 'easeInOut',
  1649. duration: duration,
  1650. onProcess: function onProcess(process) {
  1651. _this.chartData.radarData = drawRadarDataPoints(series, opts, config, context, process);
  1652. drawLegend(opts.series, opts, config, context);
  1653. drawCanvas(opts, context);
  1654. },
  1655. onAnimationFinish: function onAnimationFinish() {
  1656. _this.event.trigger('renderComplete');
  1657. }
  1658. });
  1659. break;
  1660. }
  1661. }
  1662. // simple event implement
  1663. function Event() {
  1664. this.events = {};
  1665. }
  1666. Event.prototype.addEventListener = function (type, listener) {
  1667. this.events[type] = this.events[type] || [];
  1668. this.events[type].push(listener);
  1669. };
  1670. Event.prototype.trigger = function () {
  1671. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  1672. args[_key] = arguments[_key];
  1673. }
  1674. var type = args[0];
  1675. var params = args.slice(1);
  1676. if (!!this.events[type]) {
  1677. this.events[type].forEach(function (listener) {
  1678. try {
  1679. listener.apply(null, params);
  1680. } catch (e) {
  1681. console.error(e);
  1682. }
  1683. });
  1684. }
  1685. };
  1686. var Charts = function Charts(opts) {
  1687. opts.title = opts.title || {};
  1688. opts.subtitle = opts.subtitle || {};
  1689. opts.yAxis = opts.yAxis || {};
  1690. opts.xAxis = opts.xAxis || {};
  1691. opts.extra = opts.extra || {};
  1692. opts.legend = opts.legend === false ? false : true;
  1693. opts.animation = opts.animation === false ? false : true;
  1694. var config$$1 = assign({}, config);
  1695. config$$1.yAxisTitleWidth = opts.yAxis.disabled !== true && opts.yAxis.title ? config$$1.yAxisTitleWidth : 0;
  1696. config$$1.pieChartLinePadding = opts.dataLabel === false ? 0 : config$$1.pieChartLinePadding;
  1697. config$$1.pieChartTextPadding = opts.dataLabel === false ? 0 : config$$1.pieChartTextPadding;
  1698. this.opts = opts;
  1699. this.config = config$$1;
  1700. this.context = wx.createCanvasContext(opts.canvasId);
  1701. // store calcuated chart data
  1702. // such as chart point coordinate
  1703. this.chartData = {};
  1704. this.event = new Event();
  1705. this.scrollOption = {
  1706. currentOffset: 0,
  1707. startTouchX: 0,
  1708. distance: 0
  1709. };
  1710. drawCharts.call(this, opts.type, opts, config$$1, this.context);
  1711. };
  1712. Charts.prototype.updateData = function () {
  1713. var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  1714. this.opts.series = data.series || this.opts.series;
  1715. this.opts.categories = data.categories || this.opts.categories;
  1716. this.opts.title = assign({}, this.opts.title, data.title || {});
  1717. this.opts.subtitle = assign({}, this.opts.subtitle, data.subtitle || {});
  1718. drawCharts.call(this, this.opts.type, this.opts, this.config, this.context);
  1719. };
  1720. Charts.prototype.stopAnimation = function () {
  1721. this.animationInstance && this.animationInstance.stop();
  1722. };
  1723. Charts.prototype.addEventListener = function (type, listener) {
  1724. this.event.addEventListener(type, listener);
  1725. };
  1726. Charts.prototype.getCurrentDataIndex = function (e) {
  1727. var touches = e.touches && e.touches.length ? e.touches : e.changedTouches;
  1728. if (touches && touches.length) {
  1729. var _touches$ = touches[0],
  1730. x = _touches$.x,
  1731. y = _touches$.y;
  1732. if (this.opts.type === 'pie' || this.opts.type === 'ring') {
  1733. return findPieChartCurrentIndex({ x: x, y: y }, this.chartData.pieData);
  1734. } else if (this.opts.type === 'radar') {
  1735. return findRadarChartCurrentIndex({ x: x, y: y }, this.chartData.radarData, this.opts.categories.length);
  1736. } else {
  1737. return findCurrentIndex({ x: x, y: y }, this.chartData.xAxisPoints, this.opts, this.config, Math.abs(this.scrollOption.currentOffset));
  1738. }
  1739. }
  1740. return -1;
  1741. };
  1742. Charts.prototype.showToolTip = function (e) {
  1743. var option = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  1744. if (this.opts.type === 'line' || this.opts.type === 'area') {
  1745. var index = this.getCurrentDataIndex(e);
  1746. var currentOffset = this.scrollOption.currentOffset;
  1747. var opts = assign({}, this.opts, {
  1748. _scrollDistance_: currentOffset,
  1749. animation: false
  1750. });
  1751. if (index > -1) {
  1752. var seriesData = getSeriesDataItem(this.opts.series, index);
  1753. if (seriesData.length !== 0) {
  1754. var _getToolTipData = getToolTipData(seriesData, this.chartData.calPoints, index, this.opts.categories, option),
  1755. textList = _getToolTipData.textList,
  1756. offset = _getToolTipData.offset;
  1757. opts.tooltip = {
  1758. textList: textList,
  1759. offset: offset,
  1760. option: option
  1761. };
  1762. }
  1763. }
  1764. drawCharts.call(this, opts.type, opts, this.config, this.context);
  1765. }
  1766. };
  1767. Charts.prototype.scrollStart = function (e) {
  1768. if (e.touches[0] && this.opts.enableScroll === true) {
  1769. this.scrollOption.startTouchX = e.touches[0].x;
  1770. }
  1771. };
  1772. Charts.prototype.scroll = function (e) {
  1773. // TODO throtting...
  1774. if (e.touches[0] && this.opts.enableScroll === true) {
  1775. var _distance = e.touches[0].x - this.scrollOption.startTouchX;
  1776. var currentOffset = this.scrollOption.currentOffset;
  1777. var validDistance = calValidDistance(currentOffset + _distance, this.chartData, this.config, this.opts);
  1778. this.scrollOption.distance = _distance = validDistance - currentOffset;
  1779. var opts = assign({}, this.opts, {
  1780. _scrollDistance_: currentOffset + _distance,
  1781. animation: false
  1782. });
  1783. drawCharts.call(this, opts.type, opts, this.config, this.context);
  1784. }
  1785. };
  1786. Charts.prototype.scrollEnd = function (e) {
  1787. if (this.opts.enableScroll === true) {
  1788. var _scrollOption = this.scrollOption,
  1789. currentOffset = _scrollOption.currentOffset,
  1790. distance = _scrollOption.distance;
  1791. this.scrollOption.currentOffset = currentOffset + distance;
  1792. this.scrollOption.distance = 0;
  1793. }
  1794. };
  1795. module.exports = Charts;