b9ac246cd788398c9d4a87235c9e176db8c5dbaa.svn-base 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. widgetMeta: {
  10. required: false,
  11. type: Object,
  12. default: () => {}
  13. },
  14. data: {
  15. required: true,
  16. default: () => {}
  17. },
  18. schema: {
  19. type: Array,
  20. required: true
  21. },
  22. chartOpt: {
  23. type: Object,
  24. required: false
  25. },
  26. chartStyle: {
  27. require: false,
  28. type: Object,
  29. default: () => {
  30. return {
  31. height: '500px'
  32. }
  33. }
  34. }
  35. },
  36. data() {
  37. return {
  38. chart: null
  39. }
  40. },
  41. watch: {
  42. data: {
  43. deep: true,
  44. handler: function(data) {
  45. this.renderChart(data)
  46. }
  47. },
  48. schema: {
  49. deep: true,
  50. handler: function() {
  51. this.renderChart(this.data)
  52. }
  53. }
  54. },
  55. mounted() {
  56. this.renderChart(this.data)
  57. this.$on('resized', this.handleResize)
  58. window.addEventListener('resize', this.handleResize)
  59. },
  60. beforeDestroy() {
  61. if (this.chart) {
  62. this.chart.dispose()
  63. }
  64. window.removeEventListener('resize', this.handleResize)
  65. },
  66. methods: {
  67. handleResize() {
  68. if (this.chart) {
  69. this.chart.resize()
  70. }
  71. },
  72. renderChart(data) {
  73. // console.log(this.$refs.chart)
  74. if (!this.$refs.chart) return
  75. if (data.length === 0 && this.chart) {
  76. this.chart.clear()
  77. this.chart.setOption({
  78. graphic: [{
  79. type: 'group',
  80. left: 'center',
  81. top: 'center',
  82. children: [
  83. {
  84. type: 'text',
  85. z: 100,
  86. left: 'center',
  87. top: 'middle',
  88. style: {
  89. fill: '#333',
  90. text: this.noDataDesc,
  91. font: '14px Microsoft YaHei'
  92. }
  93. }
  94. ]
  95. }]
  96. })
  97. return
  98. }
  99. let legend = []
  100. let yAxisData = []
  101. const seriesObj = {}
  102. if (this.schema.filter(schema => schema.asxAxis).length === 2) {
  103. const dimensions = this.schema.filter(schema => schema.asxAxis)
  104. const xAxisName = dimensions[0].name
  105. const legendName = dimensions[1].name
  106. const valueName = this.schema.find(schema => !schema.asxAxis).name
  107. yAxisData = this.data.map(item => {
  108. legend.push(item[legendName])
  109. return item[xAxisName]
  110. })
  111. yAxisData = Array.from(new Set(yAxisData))
  112. legend = Array.from(new Set(legend))
  113. legend = legend.map((legendItem, index) => {
  114. const seriesData = yAxisData.map(xAxisValue => {
  115. const item = data.find(dataItem => dataItem[legendName] === legendItem && dataItem[xAxisName] === xAxisValue)
  116. if (item) {
  117. return item[valueName]
  118. } else {
  119. return '-'
  120. }
  121. })
  122. legendItem += ''
  123. seriesObj[legendItem] = {
  124. name: legendItem,
  125. data: seriesData,
  126. type: 'bar'
  127. }
  128. return legendItem
  129. })
  130. } else {
  131. this.schema.forEach((schema, index) => {
  132. legend.push(schema.label || schema.name)
  133. if (!schema.asxAxis) {
  134. seriesObj[schema.name] = {
  135. name: schema.label || schema.name,
  136. data: [],
  137. // showSymbol: false,
  138. type: 'bar'
  139. }
  140. }
  141. data.forEach(item => {
  142. if (schema.asxAxis) {
  143. yAxisData.push(item[schema.name])
  144. } else {
  145. seriesObj[schema.name].data.push(item[schema.name])
  146. }
  147. })
  148. })
  149. }
  150. const option = {
  151. legend: {
  152. bottom: 0,
  153. type: 'scroll',
  154. data: legend
  155. },
  156. // color: ['#4097ff'],
  157. toolbox: {
  158. show: true,
  159. feature: {
  160. saveAsImage: {
  161. show: true
  162. },
  163. restore: {
  164. show: true
  165. },
  166. dataZoom: {
  167. show: false
  168. }
  169. }
  170. },
  171. grid: {
  172. top: '10%',
  173. left: '80px',
  174. bottom: '10%',
  175. right: '20px'
  176. },
  177. tooltip: {
  178. trigger: 'axis',
  179. axisPointer: {
  180. type: 'cross'
  181. }
  182. },
  183. xAxis: {
  184. type: 'value',
  185. axisLabel: {
  186. color: '#95a4bd'
  187. },
  188. axisLine: {
  189. lineStyle: {
  190. color: '#95a4bd'
  191. }
  192. }
  193. },
  194. yAxis: {
  195. type: 'category',
  196. data: yAxisData,
  197. axisLabel: {
  198. show: true,
  199. color: '#95a4bd'
  200. },
  201. axisLine: {
  202. lineStyle: {
  203. color: '#95a4bd'
  204. }
  205. },
  206. splitLine: {
  207. lineStyle: {
  208. type: 'dashed'
  209. }
  210. }
  211. },
  212. series: Object.values(seriesObj)
  213. }
  214. setTimeout(() => {
  215. if (!this.chart) {
  216. this.chart = echarts.init(this.$refs.chart, 'macarons')
  217. }
  218. this.chart.clear()
  219. this.chart.setOption(option)
  220. if (this.chartOpt) {
  221. this.chart.setOption(this.chartOpt)
  222. }
  223. }, 0)
  224. }
  225. }
  226. }
  227. </script>