4749ab28fd6d7d7cf3b74468089db504075030ba.svn-base 3.7 KB

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