LineChart.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <div :class="className" :style="{ height: height, width: width }" />
  3. </template>
  4. <script>
  5. import echarts from 'echarts'
  6. require('echarts/theme/macarons') // echarts theme
  7. import resize from './mixins/resize'
  8. export default {
  9. mixins: [resize],
  10. props: {
  11. className: {
  12. type: String,
  13. default: 'chart'
  14. },
  15. width: {
  16. type: String,
  17. default: '100%'
  18. },
  19. height: {
  20. type: String,
  21. default: '350px'
  22. },
  23. autoResize: {
  24. type: Boolean,
  25. default: true
  26. },
  27. chartData: {
  28. type: Array,
  29. required: true
  30. }
  31. },
  32. data() {
  33. return {
  34. chart: null
  35. }
  36. },
  37. watch: {
  38. chartData: {
  39. deep: true,
  40. handler(val) {
  41. this.setOptions(val)
  42. }
  43. }
  44. },
  45. mounted() {
  46. this.$nextTick(() => {
  47. this.initChart()
  48. })
  49. },
  50. beforeDestroy() {
  51. if (!this.chart) {
  52. return
  53. }
  54. this.chart.dispose()
  55. this.chart = null
  56. },
  57. methods: {
  58. initChart() {
  59. this.chart = echarts.init(this.$el, 'macarons')
  60. this.setOptions(this.chartData)
  61. },
  62. setOptions(chartData) {
  63. console.log(chartData.length)
  64. //循环一下第一个数据,把日期取出来
  65. if(chartData.length > 0){
  66. const dates = chartData[0].data.map(item => item.date)
  67. //管理员
  68. let data1 = [];
  69. for(let item of chartData[0].data) {
  70. data1.push(item.count)
  71. }
  72. //个人会员
  73. let data2 = [];
  74. for(let item of chartData[1].data) {
  75. data2.push(item.count)
  76. }
  77. //政务会员
  78. let data3 = [];
  79. for(let item of chartData[2].data) {
  80. data3.push(item.count)
  81. }
  82. //企业会员
  83. let data4 = [];
  84. for(let item of chartData[3].data) {
  85. data4.push(item.count)
  86. }
  87. //调研员
  88. let data5 = [];
  89. for(let item of chartData[4].data) {
  90. data5.push(item.count)
  91. }
  92. this.chart.setOption({
  93. xAxis: {
  94. data: dates,
  95. boundaryGap: false,
  96. axisTick: {
  97. show: false
  98. }
  99. },
  100. grid: {
  101. left: 20,
  102. right: 10,
  103. bottom: 20,
  104. top: 30,
  105. containLabel: true
  106. },
  107. tooltip: {
  108. trigger: 'axis',
  109. axisPointer: {
  110. type: 'cross'
  111. },
  112. padding: [5, 10]
  113. },
  114. yAxis: {
  115. axisTick: {
  116. show: false
  117. },
  118. minInterval: 1,
  119. axisLabel: {
  120. formatter: function (value) {
  121. return value
  122. }
  123. }
  124. },
  125. legend: {
  126. data: ['管理员','个人会员','政务会员','企业会员','调研员']
  127. //管理员=10000
  128. //个人会员=1
  129. //政务会员=2
  130. //企业会员=3
  131. //调研员=4
  132. },
  133. series: [{
  134. name: '管理员',
  135. smooth: true,
  136. type: 'line',
  137. itemStyle: {
  138. normal: {
  139. color: '#5570F1',
  140. lineStyle: {
  141. color: '#5570F1',
  142. width: 2
  143. }
  144. }
  145. },
  146. data: data1,
  147. animationDuration: 2800,
  148. animationEasing: 'quadraticOut'
  149. },{
  150. name: '个人会员',
  151. smooth: true,
  152. type: 'line',
  153. itemStyle: {
  154. normal: {
  155. color: '#FFCC91',
  156. lineStyle: {
  157. color: '#FFCC91',
  158. width: 2
  159. }
  160. }
  161. },
  162. data: data2,
  163. animationDuration: 2800,
  164. animationEasing: 'quadraticOut'
  165. },{
  166. name: '政务会员',
  167. smooth: true,
  168. type: 'line',
  169. itemStyle: {
  170. normal: {
  171. color: '#91FFA5',
  172. lineStyle: {
  173. color: '#91FFA5',
  174. width: 2
  175. }
  176. }
  177. },
  178. data: data3,
  179. animationDuration: 2800,
  180. animationEasing: 'quadraticOut'
  181. },{
  182. name: '企业会员',
  183. smooth: true,
  184. type: 'line',
  185. itemStyle: {
  186. normal: {
  187. color: '#FF9191',
  188. lineStyle: {
  189. color: '#FF9191',
  190. width: 2
  191. }
  192. }
  193. },
  194. data: data3,
  195. animationDuration: 2800,
  196. animationEasing: 'quadraticOut'
  197. },{
  198. name: '调研员',
  199. smooth: true,
  200. type: 'line',
  201. itemStyle: {
  202. normal: {
  203. color: '#97A5EB',
  204. lineStyle: {
  205. color: '#97A5EB',
  206. width: 2
  207. }
  208. }
  209. },
  210. data: data3,
  211. animationDuration: 2800,
  212. animationEasing: 'quadraticOut'
  213. }]
  214. })
  215. }
  216. }
  217. }
  218. }
  219. </script>