f1b22a91260d751e61889658528499ab2807c0b0.svn-base 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.label || schema.name)
  75. if (!schema.asxAxis) {
  76. seriesObj[schema.name] = {
  77. name: schema.label || schema.name,
  78. data: [],
  79. stack: 'All',
  80. // showSymbol: false,
  81. type: 'bar'
  82. }
  83. }
  84. data.forEach(item => {
  85. if (schema.asxAxis) {
  86. xAxisData.push(item[schema.name])
  87. } else {
  88. seriesObj[schema.name].data.push(item[schema.name])
  89. }
  90. })
  91. })
  92. const option = {
  93. legend: {
  94. type: 'scroll',
  95. data: legend,
  96. bottom: 0
  97. },
  98. // color: ['#4097ff'],
  99. toolbox: {
  100. show: true,
  101. top: 0,
  102. itemSize: 12,
  103. feature: {
  104. saveAsImage: {
  105. show: true
  106. },
  107. magicType: {
  108. type: ['line', 'bar']
  109. },
  110. restore: {
  111. show: true
  112. },
  113. dataZoom: {
  114. show: true
  115. }
  116. }
  117. },
  118. grid: {
  119. // show: true,
  120. top: '10px',
  121. left: '45px',
  122. right: '0',
  123. bottom: '45px'
  124. },
  125. tooltip: {
  126. trigger: 'axis',
  127. axisPointer: {
  128. type: 'cross'
  129. }
  130. },
  131. xAxis: {
  132. type: 'category',
  133. axisLabel: {
  134. color: '#95a4bd'
  135. },
  136. axisLine: {
  137. lineStyle: {
  138. color: '#95a4bd'
  139. }
  140. },
  141. data: xAxisData
  142. },
  143. yAxis: {
  144. axisLabel: {
  145. show: true,
  146. color: '#95a4bd',
  147. formatter: labelFormatter
  148. },
  149. axisLine: {
  150. lineStyle: {
  151. color: '#95a4bd'
  152. }
  153. },
  154. splitLine: {
  155. lineStyle: {
  156. type: 'dashed'
  157. }
  158. }
  159. },
  160. series: Object.values(seriesObj)
  161. }
  162. setTimeout(() => {
  163. if (!this.chart) {
  164. this.chart = echarts.init(this.$refs.chart, 'macarons')
  165. }
  166. this.chart.clear()
  167. this.chart.setOption(option)
  168. if (this.chartOpt) {
  169. this.chart.setOption(this.chartOpt)
  170. }
  171. }, 0)
  172. }
  173. }
  174. }
  175. </script>