03c82732e7a144820fc2630cf33d3ba79d74a0e9.svn-base 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <div ref="chart" :style="chartStyle" />
  3. </template>
  4. <script>
  5. import echarts from 'echarts'
  6. require('echarts/theme/macarons')
  7. import { labelFormatter } from './chartUtils'
  8. export default {
  9. props: {
  10. data: {
  11. required: true,
  12. default: () => { }
  13. },
  14. schema: {
  15. type: Array,
  16. required: true
  17. },
  18. chartOpt: {
  19. type: Object,
  20. required: false
  21. },
  22. chartStyle: {
  23. require: false,
  24. type: Object,
  25. default: () => {
  26. return {
  27. height: '500px'
  28. }
  29. }
  30. }
  31. },
  32. data() {
  33. return {
  34. chart: null
  35. }
  36. },
  37. watch: {
  38. data: {
  39. deep: true,
  40. handler: function (data) {
  41. this.renderChart(data)
  42. }
  43. },
  44. schema: {
  45. deep: true,
  46. handler: function () {
  47. this.renderChart(this.data)
  48. }
  49. }
  50. },
  51. mounted() {
  52. this.renderChart(this.data)
  53. this.$on('resized', this.handleResize)
  54. window.addEventListener('resize', this.handleResize)
  55. },
  56. beforeDestroy() {
  57. if (this.chart) {
  58. this.chart.dispose()
  59. }
  60. window.removeEventListener('resize', this.handleResize)
  61. },
  62. methods: {
  63. handleResize() {
  64. if (this.chart) {
  65. this.chart.resize()
  66. }
  67. },
  68. renderChart(data) {
  69. if (!this.$refs.chart) return
  70. const legend = []
  71. const xAxisData = []
  72. const seriesObj = {}
  73. this.schema.forEach((schema, index) => {
  74. legend.push(schema.Comment || schema.name)
  75. if (!schema.asxAxis) {
  76. if (schema.is_second == true) {
  77. seriesObj[schema.name] = {
  78. name: schema.Comment || schema.name,
  79. data: [],
  80. type: 'line'
  81. }
  82. } else {
  83. seriesObj[schema.name] = {
  84. name: schema.Comment || schema.name,
  85. data: [],
  86. type: 'bar'
  87. }
  88. }
  89. }
  90. data.forEach(item => {
  91. if (schema.asxAxis) {
  92. xAxisData.push(item[schema.name])
  93. } else {
  94. seriesObj[schema.name].data.push(item[schema.name])
  95. }
  96. })
  97. })
  98. const option = {
  99. legend: {
  100. type: 'scroll',
  101. data: legend,
  102. bottom: 0
  103. },
  104. // color: ['#4097ff'],
  105. toolbox: {
  106. show: true,
  107. top: 0,
  108. itemSize: 12,
  109. feature: {
  110. saveAsImage: {
  111. show: true
  112. },
  113. restore: {
  114. show: true
  115. },
  116. dataZoom: {
  117. show: true
  118. }
  119. }
  120. },
  121. grid: {
  122. // show: true,
  123. top: '10px',
  124. left: '45px',
  125. right: '0',
  126. bottom: '50px'
  127. },
  128. tooltip: {
  129. trigger: 'axis',
  130. axisPointer: {
  131. type: 'cross'
  132. }
  133. },
  134. xAxis: {
  135. type: 'category',
  136. axisLabel: {
  137. color: '#95a4bd'
  138. },
  139. axisLine: {
  140. lineStyle: {
  141. color: '#95a4bd'
  142. }
  143. },
  144. data: xAxisData
  145. },
  146. yAxis: {
  147. axisLabel: {
  148. show: true,
  149. color: '#95a4bd',
  150. formatter: labelFormatter
  151. },
  152. axisLine: {
  153. lineStyle: {
  154. color: '#95a4bd'
  155. }
  156. },
  157. splitLine: {
  158. lineStyle: {
  159. type: 'dashed'
  160. }
  161. }
  162. },
  163. series: Object.values(seriesObj)
  164. }
  165. setTimeout(() => {
  166. if (!this.chart) {
  167. this.chart = echarts.init(this.$refs.chart, 'macarons')
  168. }
  169. this.chart.clear()
  170. this.chart.setOption(option)
  171. if (this.chartOpt) {
  172. this.chart.setOption(this.chartOpt)
  173. }
  174. }, 0)
  175. }
  176. }
  177. }
  178. </script>