選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

5464 行
183 KiB

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