物管理前端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

232 lines
6.9 KiB

  1. (function (global) {
  2. global.echarts_tools = global.echarts_tools || {};
  3. /**
  4. * echarts tooltip轮播
  5. * @param chart ECharts实例
  6. * @param chartOption echarts的配置信息
  7. * @param options object 选项
  8. * {
  9. * interval 轮播时间间隔单位毫秒默认为2000
  10. * loopSeries boolean类型默认为false
  11. * true表示循环所有series的tooltipfalse则显示指定seriesIndex的tooltip
  12. * seriesIndex 默认为0指定某个系列option中的series索引循环显示tooltip
  13. * 当loopSeries为true时从seriesIndex系列开始执行
  14. * updateData 自定义更新数据的函数默认为null
  15. * 用于类似于分页的效果比如总数据有20条chart一次只显示5条全部数据可以分4次显示
  16. * }
  17. * @returns {{clearLoop: clearLoop}}
  18. */
  19. echarts_tools.loopShowTooltip = function (chart, chartOption, options) {
  20. let defaultOptions = {
  21. interval: 2000,
  22. loopSeries: true,
  23. seriesIndex: 0,
  24. updateData: null
  25. };
  26. if (!chart || !chartOption) {
  27. return;
  28. }
  29. let dataIndex = 0; // 数据索引,初始化为-1,是为了判断是否是第一次执行
  30. let seriesIndex = 0; // 系列索引
  31. let timeTicket = 0;
  32. let seriesLen = chartOption.series.length; // 系列个数
  33. let dataLen = 0; // 某个系列数据个数
  34. let chartType; // 系列类型
  35. let first = true;
  36. let lastShowSeriesIndex = 0;
  37. let lastShowDataIndex = 0;
  38. if (seriesLen === 0) {
  39. return;
  40. }
  41. //待处理列表
  42. //不循环series时seriesIndex指定显示tooltip的系列,不指定默认为0,指定多个则默认为第一个
  43. //循环series时seriesIndex指定循环的series,不指定则从0开始循环所有series,指定单个则相当于不循环,指定多个
  44. if (options) {
  45. options.interval = options.interval || defaultOptions.interval;
  46. options.loopSeries = options.loopSeries || defaultOptions.loopSeries;
  47. options.seriesIndex = options.seriesIndex || defaultOptions.seriesIndex;
  48. options.updateData = options.updateData || defaultOptions.updateData;
  49. } else {
  50. options = defaultOptions;
  51. }
  52. //如果设置的seriesIndex无效,则默认为0
  53. if (options.seriesIndex < 0 || options.seriesIndex >= seriesLen) {
  54. seriesIndex = 0;
  55. } else {
  56. seriesIndex = options.seriesIndex;
  57. }
  58. /**
  59. * 清除定时器
  60. */
  61. function clearLoop() {
  62. if (timeTicket) {
  63. clearInterval(timeTicket);
  64. timeTicket = 0;
  65. }
  66. chart.off("mousemove", stopAutoShow);
  67. zRender.off("mousemove", zRenderMouseMove);
  68. zRender.off("globalout", zRenderGlobalOut);
  69. }
  70. /**
  71. * 取消高亮
  72. */
  73. function cancelHighlight() {
  74. /**
  75. * 如果dataIndex为0表示上次系列完成显示如果是循环系列且系列索引为0则上次是seriesLen-1否则为seriesIndex-1
  76. * 如果不是循环系列则就是当前系列
  77. * 如果dataIndex>0则就是当前系列
  78. */
  79. let tempSeriesIndex =
  80. dataIndex === 0
  81. ? options.loopSeries
  82. ? seriesIndex === 0
  83. ? seriesLen - 1
  84. : seriesIndex - 1
  85. : seriesIndex
  86. : seriesIndex;
  87. let tempType = chartOption.series[tempSeriesIndex].type;
  88. if (tempType === "pie" || tempType === "radar") {
  89. chart.dispatchAction({
  90. type: "downplay",
  91. seriesIndex: lastShowSeriesIndex,
  92. dataIndex: lastShowDataIndex
  93. }); //wait 系列序号为0且循环系列,则要判断上次的系列类型是否是pie、radar
  94. }
  95. }
  96. /**
  97. * 自动轮播tooltip
  98. */
  99. function autoShowTip() {
  100. let invalidSeries = 0;
  101. let invalidData = 0;
  102. function showTip() {
  103. //判断是否更新数据
  104. if (dataIndex === 0 && !first && typeof options.updateData === "function") {
  105. options.updateData();
  106. chart.setOption(chartOption);
  107. }
  108. let series = chartOption.series;
  109. let currSeries = series[seriesIndex];
  110. if (
  111. !series ||
  112. series.length === 0 ||
  113. !currSeries ||
  114. !currSeries.type ||
  115. !currSeries.data ||
  116. !currSeries.data.length
  117. ) {
  118. return;
  119. }
  120. chartType = currSeries.type; // 系列类型
  121. dataLen = currSeries.data.length; // 某个系列的数据个数
  122. let tipParams = { seriesIndex: seriesIndex };
  123. switch (chartType) {
  124. case "pie":
  125. case "map":
  126. case "chord":
  127. tipParams.name = currSeries.data[dataIndex].name;
  128. break;
  129. case "radar": // 雷达图
  130. tipParams.seriesIndex = seriesIndex;
  131. tipParams.dataIndex = dataIndex;
  132. break;
  133. default:
  134. tipParams.dataIndex = dataIndex;
  135. break;
  136. }
  137. if (chartType === "pie" || chartType === "radar") {
  138. if (!first) {
  139. cancelHighlight();
  140. }
  141. // 高亮当前图形
  142. chart.dispatchAction({
  143. type: "highlight",
  144. seriesIndex: seriesIndex,
  145. dataIndex: dataIndex
  146. });
  147. }
  148. // 显示 tooltip
  149. tipParams.type = "showTip";
  150. // 防止updateData时先处理tooltip后刷新数据导出tooltip显示不正确
  151. setTimeout(() => {
  152. chart.dispatchAction(tipParams);
  153. }, 0);
  154. lastShowSeriesIndex = seriesIndex;
  155. lastShowDataIndex = dataIndex;
  156. dataIndex = (dataIndex + 1) % dataLen;
  157. if (options.loopSeries && dataIndex === 0) {
  158. // 数据索引归0表示当前系列数据已经循环完
  159. invalidData = 0;
  160. seriesIndex = (seriesIndex + 1) % seriesLen;
  161. if (seriesIndex === options.seriesIndex) {
  162. invalidSeries = 0;
  163. }
  164. }
  165. first = false;
  166. }
  167. showTip();
  168. timeTicket = setInterval(showTip, options.interval);
  169. }
  170. // 关闭轮播
  171. function stopAutoShow() {
  172. if (timeTicket) {
  173. clearInterval(timeTicket);
  174. timeTicket = 0;
  175. if (chartType === "pie" || chartType === "radar") {
  176. cancelHighlight();
  177. }
  178. }
  179. }
  180. let zRender = chart.getZr();
  181. function zRenderMouseMove(param) {
  182. if (param.event) {
  183. //阻止canvas上的鼠标移动事件冒泡
  184. param.event.cancelBubble = true;
  185. }
  186. stopAutoShow();
  187. }
  188. // 离开echarts图时恢复自动轮播
  189. function zRenderGlobalOut() {
  190. if (!timeTicket) {
  191. autoShowTip();
  192. }
  193. }
  194. // 鼠标在echarts图上时停止轮播
  195. chart.on("mousemove", stopAutoShow);
  196. zRender.on("mousemove", zRenderMouseMove);
  197. zRender.on("globalout", zRenderGlobalOut);
  198. autoShowTip();
  199. return {
  200. clearLoop: clearLoop
  201. };
  202. };
  203. })(window);