line.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. line.vue<template>
  2. <div>
  3. <!-- <div class="chart-title">{{title}}</div> -->
  4. <div id="echarts"></div>
  5. </div>
  6. </template>
  7. <script>
  8. import * as echarts from 'echarts';
  9. export default {
  10. name: 'Echarts',
  11. props: {
  12. 'width': {
  13. type: Number,
  14. default: 800
  15. },
  16. 'height': {
  17. type: Number,
  18. default: 400
  19. },
  20. 'title': {
  21. type: String,
  22. default: '表格标题'
  23. },
  24. 'source': {
  25. type: Object,
  26. default: function () {
  27. return {
  28. rows: [],
  29. data: []
  30. }
  31. }
  32. }
  33. },
  34. data() {
  35. return {}
  36. },
  37. watch: {
  38. 'source': {
  39. deep: true,
  40. handler: function () {
  41. this.$nextTick(() => {
  42. this.initEcharts()
  43. })
  44. }
  45. }
  46. },
  47. mounted() {
  48. this.initEcharts()
  49. },
  50. computed: {
  51. 'seriesParse': function () {
  52. if (this.source.data.length == 0) {
  53. return []
  54. }
  55. let result2 = this.source.data.map((item) => {
  56. return {
  57. name: item.title,
  58. data: [...item.data],
  59. type: 'line',
  60. }
  61. })
  62. result2.shift()
  63. return result2
  64. },
  65. 'xAxisParse': function () {
  66. return this.source.rows
  67. },
  68. 'legendSelectArr': function () {
  69. let selectedArr = this.seriesParse.map((item) => {
  70. return item.name
  71. })
  72. return selectedArr
  73. }
  74. },
  75. methods: {
  76. initEcharts() {
  77. if (!this.myChart) {
  78. this.myChart = echarts.init(document.getElementById('echarts'));
  79. this.myChart.off("legendselectchanged");
  80. this.myChart.on('legendselectchanged', (params) => {
  81. // console.log(params)
  82. this.selectedItem(params.name)
  83. })
  84. }
  85. this.myChart.resize({
  86. height: this.height
  87. });
  88. let selected = {}
  89. this.legendSelectArr.forEach((item, index) => {
  90. if (index == 0) {
  91. selected[item] = true
  92. } else {
  93. selected[item] = false
  94. }
  95. })
  96. this.myChart.setOption({
  97. legend: {
  98. orient: 'vertical',
  99. y: 'top',
  100. x: 'right',
  101. selected: selected,
  102. selector: true
  103. },
  104. title: {
  105. text: this.title,
  106. show: !!this.title,
  107. textAlign: 'auto'
  108. },
  109. tooltip: {
  110. trigger: 'axis'
  111. },
  112. xAxis: {
  113. data: [...this.xAxisParse]
  114. },
  115. yAxis: {},
  116. series: [...this.seriesParse]
  117. }, true);
  118. },
  119. selectedItem(name) {
  120. let selected = {}
  121. this.legendSelectArr.forEach((item, index) => {
  122. if (item == name) {
  123. selected[item] = true
  124. } else {
  125. selected[item] = false
  126. }
  127. })
  128. this.myChart.setOption({
  129. legend: {
  130. orient: 'vertical',
  131. y: 'top',
  132. x: 'right',
  133. selected: selected,
  134. selector: true
  135. },
  136. title: {
  137. text: this.title,
  138. show: !!this.title,
  139. textAlign: 'auto'
  140. },
  141. tooltip: {
  142. trigger: 'axis'
  143. },
  144. xAxis: {
  145. data: [...this.xAxisParse]
  146. },
  147. yAxis: {},
  148. series: [...this.seriesParse]
  149. }, true);
  150. }
  151. }
  152. }
  153. </script>
  154. <style>
  155. #echarts {
  156. width: 100%;
  157. height: 400px;
  158. margin-left: auto;
  159. margin-right: auto;
  160. /* float: left; */
  161. }
  162. .chart-title {
  163. text-align: center;
  164. }
  165. </style>