62e3f80833e735fc3a3c89284d727886510c3c0d.svn-base 4.1 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. export default {
  8. props: {
  9. data: {
  10. required: true,
  11. default: () => { }
  12. },
  13. schema: {
  14. type: Array,
  15. required: true
  16. },
  17. chartOpt: {
  18. type: Object,
  19. required: false
  20. },
  21. chartStyle: {
  22. require: false,
  23. type: Object,
  24. default: () => {
  25. return {
  26. height: '500px'
  27. }
  28. }
  29. }
  30. },
  31. data() {
  32. return {
  33. chart: null
  34. }
  35. },
  36. watch: {
  37. data: {
  38. deep: true,
  39. handler: function (data) {
  40. this.renderChart(data)
  41. }
  42. },
  43. schema: {
  44. deep: true,
  45. handler: function () {
  46. this.renderChart(this.data)
  47. }
  48. }
  49. },
  50. mounted() {
  51. this.renderChart(this.data)
  52. this.$on('resized', this.handleResize)
  53. window.addEventListener('resize', this.handleResize)
  54. },
  55. beforeDestroy() {
  56. if (this.chart) {
  57. this.chart.dispose()
  58. }
  59. window.removeEventListener('resize', this.handleResize)
  60. },
  61. methods: {
  62. handleResize() {
  63. if (this.chart) {
  64. this.chart.resize()
  65. }
  66. },
  67. renderChart(data) {
  68. if (!this.$refs.chart || !data[0]) return
  69. const legend = []
  70. let seriesObj = {}
  71. if (this.schema.filter(schema => schema.asxAxis).length === 0) {
  72. seriesObj = {
  73. name: '',
  74. type: 'pie',
  75. radius: '55%',
  76. center: ['50%', '60%'],
  77. data: [],
  78. tooltip: {
  79. formatter: (params) => {
  80. return `${params.name}: ${params.percent}%`
  81. }
  82. },
  83. itemStyle: {
  84. emphasis: {
  85. shadowBlur: 10,
  86. shadowOffsetX: 0,
  87. shadowColor: 'rgba(0, 0, 0, 0.5)'
  88. }
  89. }
  90. }
  91. // only one row in data if no dimensions
  92. data = data[0]
  93. this.schema.forEach((schema, index) => {
  94. legend.push(schema.name)
  95. seriesObj.data[index] = seriesObj.data[index] || {}
  96. seriesObj.data[index].name = schema.name
  97. seriesObj.data[index].value = data[schema.name]
  98. })
  99. } else {
  100. seriesObj = {
  101. name: '',
  102. type: 'pie',
  103. radius: '55%',
  104. center: ['50%', '40%'],
  105. data: [],
  106. tooltip: {
  107. formatter: (params) => {
  108. return `${params.name}: ${params.percent}%`
  109. }
  110. },
  111. itemStyle: {
  112. emphasis: {
  113. shadowBlur: 10,
  114. shadowOffsetX: 0,
  115. shadowColor: 'rgba(0, 0, 0, 0.5)'
  116. }
  117. }
  118. }
  119. this.schema.forEach((schema, index) => {
  120. data.forEach((item, dataIndex) => {
  121. seriesObj.data[dataIndex] = seriesObj.data[dataIndex] || {}
  122. if (schema.asxAxis) {
  123. legend.push(item[schema.name])
  124. seriesObj.data[dataIndex].name = item[schema.name]
  125. } else {
  126. seriesObj.data[dataIndex].value = item[schema.name]
  127. }
  128. })
  129. })
  130. }
  131. const option = {
  132. legend: {
  133. type: 'scroll',
  134. data: legend,
  135. bottom: 0
  136. },
  137. toolbox: {
  138. show: true,
  139. itemSize: 12,
  140. feature: {
  141. saveAsImage: {
  142. show: true
  143. },
  144. // magicType: {
  145. // type: ['line', 'bar']
  146. // },
  147. restore: {
  148. show: true
  149. },
  150. dataZoom: {
  151. show: true
  152. }
  153. }
  154. },
  155. grid: {
  156. // show: true,
  157. top: '10px',
  158. left: '80px',
  159. right: '0',
  160. bottom: '45px'
  161. },
  162. tooltip: {
  163. trigger: 'item'
  164. },
  165. series: [seriesObj]
  166. }
  167. setTimeout(() => {
  168. if (!this.chart) {
  169. this.chart = echarts.init(this.$refs.chart, 'macarons')
  170. }
  171. this.chart.clear()
  172. this.chart.setOption(option)
  173. if (this.chartOpt) {
  174. this.chart.setOption(this.chartOpt)
  175. }
  176. }, 0)
  177. }
  178. }
  179. }
  180. </script>