lineChart.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. smooth:encode.bool,<template>
  2. <div class="chart" :id="id" @mouseenter="handleMouseEnter"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. const colors = ["#00F5FC", "#D5920F", "#FA6894","#4cff37"];
  7. export default {
  8. props: {
  9. id: { type: String, required: true },
  10. dataset: { type: Array, required: true },
  11. encode: { type: Array, required: true },
  12. yAxisName: { type: String },
  13. xlabelFormat: { type: Function },
  14. tooltipFormat: { type: Function },
  15. colorIdx: { type: Number },
  16. colors: { type: Array, default: () => colors },
  17. top: { type: Number, default: 50 },
  18. right: { type: Number, default: 20 },
  19. bottom: { type: Number, default: 30 },
  20. left: { type: Number, default: 70 },
  21. legendRight: { type: Number, default: 20 },
  22. legendTop: { type: Number, default: 10 },
  23. markLine: { type: Object }, // 标线
  24. symbolSize: { type: Number, default: 4 },
  25. showArea: { type: Boolean, default: true },
  26. labelSize: { type: Number, default: 12 },
  27. isBig: { type: Boolean, default: false },
  28. autoStop: { type: Boolean, default: true }, // tooltip循环会否循环一轮后自动停止
  29. legendShow: { type: Boolean, default: true },
  30. },
  31. data() {
  32. return {
  33. option: {
  34. grid: {
  35. top: this.top,
  36. bottom: this.bottom,
  37. left: this.left,
  38. right: this.right,
  39. },
  40. legend: {
  41. right: this.legendRight,
  42. top: this.legendTop,
  43. icon: "circle",
  44. textStyle: {
  45. color: "#ffffff",
  46. fontSize: this.isBig ? 18 : this.labelSize,
  47. },
  48. selectedMode: false,
  49. show: this.legendShow,
  50. },
  51. tooltip: {
  52. trigger: "axis",
  53. confine: true,
  54. axisPointer: {
  55. lineStyle: { color: "#0081AC", type: "dashed" },
  56. },
  57. backgroundColor: "t",
  58. formatter: this.tooltipFormat,
  59. position: function (point, params, dom, rect, size) {
  60. let boxWidth = size.contentSize[0];
  61. let boxHeight = size.contentSize[1];
  62. return [point[0] - boxWidth / 2, point[1] - boxHeight - 10];
  63. },
  64. textStyle: {
  65. fontSize: this.labelSize,
  66. },
  67. },
  68. xAxis: {
  69. type: "category",
  70. axisLabel: {
  71. color: "#59BEED",
  72. fontSize: this.labelSize,
  73. interval: 0,
  74. },
  75. axisLine: {
  76. lineStyle: {
  77. color: "#1977C6",
  78. },
  79. },
  80. axisTick: false,
  81. },
  82. yAxis: {
  83. axisLabel: {
  84. color: "#59BEED",
  85. fontSize: this.labelSize,
  86. },
  87. nameTextStyle: {
  88. color: "#59BEED",
  89. fontSize: this.labelSize,
  90. },
  91. axisLine: {
  92. lineStyle: {
  93. color: "#1977C6",
  94. },
  95. },
  96. splitLine: {
  97. lineStyle: {
  98. color: "#082E81",
  99. type: "dashed",
  100. },
  101. },
  102. axisTick: false,
  103. },
  104. dataset: {},
  105. series: [],
  106. },
  107. i: 0,
  108. };
  109. },
  110. watch: {
  111. dataset: function (newV) {
  112. clearInterval(this.tooltipInterval);
  113. this.i = 0;
  114. const option = this.option;
  115. option.dataset.source = newV;
  116. this.chart.setOption(option);
  117. this.startInterval();
  118. },
  119. },
  120. mounted() {
  121. this.chart = echarts.init($(`#${this.id}`)[0]);
  122. const option = this.option;
  123. option.yAxis.name = this.yAxisName;
  124. if (this.xlabelFormat) {
  125. option.xAxis.axisLabel.formatter = this.xlabelFormat;
  126. }
  127. this.encode.map((encode, idx) => {
  128. option.series.push({
  129. name: encode.seriesName,
  130. type: "line",
  131. smooth:encode.bool,
  132. symbol: "circle",
  133. symbolSize: this.symbolSize,
  134. markLine: this.markLine,
  135. hoverAnimation: this.symbolSize === 4,
  136. encode: encode,
  137. lineStyle: { width: 2 , color:encode.col},
  138. itemStyle: {
  139. color: encode.col ? encode.col:this.colorIdx ? this.colors[this.colorIdx] : this.colors[idx],
  140. },
  141. areaStyle: {
  142. opacity: this.showArea ? 1 : 0,
  143. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  144. {
  145. offset: 0,
  146. color: encode.col ? encode.col:this.colorIdx ? this.colors[this.colorIdx] : this.colors[idx],
  147. },
  148. {
  149. offset: 1,
  150. color: "transparent",
  151. },
  152. ]),
  153. },
  154. });
  155. });
  156. option.dataset.source = this.dataset;
  157. if (this.tooltipFormat) {
  158. option.tooltip.formatter = this.tooltipFormat;
  159. }
  160. this.chart.setOption(option);
  161. this.chart.on("mousemove", (e) => {
  162. this.i = e.dataIndex + 1;
  163. });
  164. this.chart.on("globalout", () => {
  165. this.startInterval();
  166. });
  167. if (this.dataset.length) {
  168. const option = this.option;
  169. option.dataset.source = this.dataset;
  170. this.chart.setOption(option);
  171. this.startInterval();
  172. }
  173. },
  174. methods: {
  175. handleMouseEnter() {
  176. if (this.tooltipInterval) {
  177. clearInterval(this.tooltipInterval);
  178. }
  179. },
  180. startInterval() {
  181. this.tooltipInterval = setInterval(() => {
  182. if (this.i > this.dataset.length - 1) {
  183. if (this.autoStop) {
  184. this.stopInterval();
  185. } else {
  186. this.i = 0;
  187. }
  188. }
  189. this.chart.dispatchAction({
  190. type: "showTip",
  191. seriesIndex: 0,
  192. dataIndex: this.i,
  193. });
  194. this.i = this.i + 1;
  195. }, 2000);
  196. },
  197. stopInterval() {
  198. this.chart.dispatchAction({ type: "hideTip", seriesIndex: 0 });
  199. clearInterval(this.tooltipInterval);
  200. },
  201. },
  202. beforeDestroy() {
  203. this.stopInterval();
  204. },
  205. };
  206. </script>
  207. <style lang="less" scoped>
  208. .chart {
  209. width: 100%;
  210. height: 100%;
  211. }
  212. .line-tooltip {
  213. padding: 10px 20px;
  214. background: url("../../static/img/u231.png");
  215. background-size: 100% 100%;
  216. }
  217. .line-tooltip-dz {
  218. padding: 5px 10px;
  219. background: url("../../static/index/u261.png");
  220. background-size: 100% 100%;
  221. color: #333;
  222. }
  223. </style>