EchartsLine.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <div class="echarts-line" :id="ids"></div>
  3. </template>
  4. <script>
  5. import * as echarts from 'echarts'
  6. export default {
  7. props: {
  8. legendChecked: {
  9. // 图例
  10. default: true
  11. },
  12. ids: {
  13. // id
  14. type: String,
  15. default: 'echartsline'
  16. },
  17. fontColor: {
  18. // 字体颜色
  19. type: String,
  20. default: '#89949F'
  21. },
  22. yLine: {
  23. // x轴分割线
  24. default: false
  25. },
  26. chartData: {
  27. // echarts·数据
  28. default: function () {
  29. return []
  30. }
  31. },
  32. // Y轴单位
  33. Yunit: {
  34. type: Boolean,
  35. default: true
  36. },
  37. theme: {
  38. default: function () {
  39. return ['#3da4fe', '#23cef2', '#65b664', '#00FDCB']
  40. }
  41. }
  42. },
  43. watch: {
  44. chartData: {
  45. // 深度监听,可监听到对象、数组的变化
  46. handler() {
  47. this.$nextTick(() => {
  48. if (this.chartData && this.chartData.length > 0) {
  49. this.dataValue(this.chartData)
  50. }
  51. })
  52. },
  53. deep: true, // 深度监听,可监听到对象、数组的变化
  54. immediate: true // 立刻执行
  55. }
  56. },
  57. data() {
  58. return {}
  59. },
  60. mounted() {},
  61. methods: {
  62. dataValue(newVal) {
  63. if (newVal && newVal.length > 0) {
  64. const seriesConfig = []
  65. const Yconfig = []
  66. const unit = []
  67. for (let i = 0; i < newVal.length; i++) {
  68. if (newVal[i].areaStyle) {
  69. const obj = {
  70. type: 'line',
  71. name: newVal[i].name,
  72. data: newVal[i].yData,
  73. smooth: newVal[i].smooth ? newVal[i].smooth : false,
  74. showSymbol: newVal[i].showSymbol ? newVal[i].showSymbol : false,
  75. lineStyle: {
  76. color: newVal[i].lineColor
  77. },
  78. areaStyle: {
  79. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  80. {
  81. offset: 0,
  82. color: newVal[i].areaColor
  83. },
  84. {
  85. offset: 1,
  86. color: 'rgba(0,0,0,0)'
  87. }
  88. ])
  89. }
  90. }
  91. if (i === 1) {
  92. obj.yAxisIndex = 1
  93. }
  94. seriesConfig.push(obj)
  95. if (this.Yunit) {
  96. Yconfig.push({
  97. type: 'value',
  98. name:
  99. (newVal.length > 1 ? newVal[i].name : '单位') +
  100. ':' +
  101. newVal[i].unit,
  102. nameTextStyle: {
  103. fontSize: 12,
  104. color: this.fontColor
  105. },
  106. splitNumber: 5,
  107. axisTick: {
  108. show: false
  109. },
  110. splitLine: {
  111. show: this.yLine,
  112. lineStyle: {
  113. color: 'rgba(153, 153, 153, 0.3)'
  114. }
  115. },
  116. axisLine: {
  117. show: false
  118. },
  119. axisLabel: {
  120. color: this.fontColor,
  121. fontSize: 12
  122. }
  123. })
  124. } else {
  125. Yconfig.push({
  126. type: 'value',
  127. nameTextStyle: {
  128. fontSize: 12,
  129. color: this.fontColor
  130. },
  131. splitNumber: 5,
  132. axisTick: {
  133. show: false
  134. },
  135. splitLine: {
  136. show: this.yLine,
  137. lineStyle: {
  138. color: 'rgba(153, 153, 153, 0.3)'
  139. }
  140. },
  141. axisLine: {
  142. show: false
  143. },
  144. axisLabel: {
  145. color: this.fontColor,
  146. fontSize: 12
  147. }
  148. })
  149. Yconfig[0].name = '(m³/s)'
  150. }
  151. unit.push(newVal[i].unit)
  152. } else {
  153. seriesConfig.push({
  154. type: 'line',
  155. name: newVal[i].name,
  156. data: newVal[i].yData,
  157. smooth: newVal[i].smooth ? newVal[i].smooth : false,
  158. symbolSize: newVal[i].showSymbol ? newVal[i].showSymbol : false,
  159. lineStyle: {
  160. color: newVal[i].lineColor
  161. }
  162. })
  163. Yconfig.push({
  164. type: 'value',
  165. name: newVal[i].name + ':' + newVal[i].unit,
  166. nameTextStyle: {
  167. fontSize: 12,
  168. color: this.fontColor
  169. },
  170. splitNumber: 5,
  171. axisTick: {
  172. show: false
  173. },
  174. splitLine: {
  175. show: this.yLine,
  176. lineStyle: {
  177. color: '#e5e5e5'
  178. }
  179. },
  180. axisLine: {
  181. show: false
  182. },
  183. axisLabel: {
  184. color: this.fontColor,
  185. fontSize: 12
  186. }
  187. })
  188. unit.push(newVal[i].unit)
  189. }
  190. }
  191. this.setEcharts(seriesConfig, Yconfig, newVal[0].xData, unit)
  192. }
  193. },
  194. setEcharts(series, Yconfig, dataAxis, unit) {
  195. let legend = []
  196. if (this.legendChecked) {
  197. legend = this.chartData.map((data, i) => {
  198. return {
  199. name: data.name,
  200. icon: 'roundRect',
  201. itemStyle: series[i].lineStyle
  202. }
  203. })
  204. } else {
  205. legend = []
  206. }
  207. const myChart = echarts.init(document.getElementById(this.ids))
  208. const option = {
  209. color: this.theme,
  210. tooltip: {
  211. trigger: 'axis',
  212. formatter: function (data) {
  213. if (!data.length) return
  214. if (data.length > 1) {
  215. let html = ''
  216. html = `<div class="toolTips"><div style="font-size: 12px;color: #fff">${data[0].axisValue}</div>`
  217. for (let i = 0; i < data.length; i++) {
  218. html += `<div style="font-size: 12px;color: #fff">${series[i].name}: ${data[i].value} ${unit[i]}</div>`
  219. }
  220. html += '</div>'
  221. return html
  222. } else {
  223. return `<div class="toolTips"><div style="font-size: 12px;color: #fff">${data[0].axisValue}: ${data[0].value} ${unit[0]}</div></div>`
  224. }
  225. },
  226. backgroundColor: '#487892',
  227. borderWidth: 0,
  228. padding: [3, 5],
  229. axisPointer: {
  230. lineStyle: {
  231. color: '#299acb'
  232. }
  233. }
  234. },
  235. legend: {
  236. top: 0,
  237. right: 'center',
  238. width: 240,
  239. data: legend,
  240. itemWidth: 15,
  241. itemHeight: 5,
  242. show: true,
  243. selectedMode: false,
  244. textStyle: {
  245. color: this.fontColor
  246. }
  247. },
  248. xAxis: {
  249. type: 'category',
  250. data: dataAxis,
  251. boundaryGap: false,
  252. axisTick: {
  253. show: true,
  254. inside: true
  255. },
  256. splitLine: {
  257. show: false
  258. },
  259. axisLine: {
  260. lineStyle: {
  261. color: 'rgba(135,142,148,0.5)'
  262. }
  263. },
  264. axisLabel: {
  265. color: this.fontColor,
  266. fontSize: 12
  267. }
  268. },
  269. yAxis: Yconfig,
  270. grid: {
  271. left: 20,
  272. right: 35,
  273. top: this.legendChecked ? 30 : 30,
  274. bottom: 0,
  275. containLabel: true
  276. },
  277. series: series
  278. }
  279. myChart.clear()
  280. myChart.setOption(option, true)
  281. }
  282. }
  283. }
  284. </script>
  285. <style scoped lang="scss">
  286. .echarts-line {
  287. width: 80%;
  288. height: 80%;
  289. }
  290. </style>