60f4d04be6bef4676879dfe5497263d56ca88d8f.svn-base 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. legend.push(schema.Comment || schema.name)
  134. if (!schema.asxAxis) {
  135. seriesObj[schema.name] = {
  136. // name: schema.label || schema.name,
  137. name: schema.Comment || schema.name,
  138. data: [],
  139. // showSymbol: false,
  140. type: 'bar'
  141. }
  142. }
  143. data.forEach(item => {
  144. if (schema.asxAxis) {
  145. yAxisData.push(item[schema.name])
  146. } else {
  147. seriesObj[schema.name].data.push(item[schema.name])
  148. }
  149. })
  150. })
  151. }
  152. const option = {
  153. legend: {
  154. bottom: 0,
  155. type: 'scroll',
  156. data: legend
  157. },
  158. // color: ['#4097ff'],
  159. toolbox: {
  160. show: true,
  161. feature: {
  162. saveAsImage: {
  163. show: true
  164. },
  165. restore: {
  166. show: true
  167. },
  168. dataZoom: {
  169. show: false
  170. }
  171. }
  172. },
  173. grid: {
  174. top: '10%',
  175. left: '80px',
  176. bottom: '20%',
  177. right: '20px'
  178. },
  179. tooltip: {
  180. trigger: 'axis',
  181. axisPointer: {
  182. type: 'cross'
  183. }
  184. },
  185. xAxis: {
  186. type: 'value',
  187. axisLabel: {
  188. color: '#95a4bd'
  189. },
  190. axisLine: {
  191. lineStyle: {
  192. color: '#95a4bd'
  193. }
  194. }
  195. },
  196. yAxis: {
  197. type: 'category',
  198. data: yAxisData,
  199. axisLabel: {
  200. show: true,
  201. color: '#95a4bd'
  202. },
  203. axisLine: {
  204. lineStyle: {
  205. color: '#95a4bd'
  206. }
  207. },
  208. splitLine: {
  209. lineStyle: {
  210. type: 'dashed'
  211. }
  212. }
  213. },
  214. series: Object.values(seriesObj)
  215. }
  216. setTimeout(() => {
  217. if (!this.chart) {
  218. this.chart = echarts.init(this.$refs.chart, 'macarons')
  219. }
  220. this.chart.clear()
  221. this.chart.setOption(option)
  222. if (this.chartOpt) {
  223. this.chart.setOption(this.chartOpt)
  224. }
  225. }, 0)
  226. }
  227. }
  228. }
  229. </script>