6a6092f96c8143ea1f464842f3bf41665e2e2bcf.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. 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. console.log('renderChart', data)
  70. console.log('this.schema', this.schema)
  71. if (!this.$refs.chart) return
  72. let legend = []
  73. let xAxisData = []
  74. const seriesObj = {}
  75. if (this.schema.filter(schema => schema.asxAxis).length === 2) {
  76. console.log("进入了多个维度====================")
  77. const dimensions = this.schema.filter(schema => schema.asxAxis)
  78. console.log('dimensions', dimensions)
  79. const xAxisName = dimensions[0].name
  80. const legendName = dimensions[1].name
  81. // const legendName222 = dimensions[1].name
  82. console.log('data', this.data)
  83. const valueName = this.schema.find(schema => !schema.asxAxis).name
  84. xAxisData = this.data.map(item => {
  85. // legend.push(item[legendName])
  86. legend.push(item[legendName])
  87. return item[xAxisName]
  88. })
  89. xAxisData = Array.from(new Set(xAxisData))
  90. legend = Array.from(new Set(legend))
  91. console.log('legend', legend)
  92. legend = legend.map((legendItem, index) => {
  93. const seriesData = xAxisData.map(xAxisValue => {
  94. const item = data.find(dataItem => dataItem[legendName] === legendItem && dataItem[xAxisName] === xAxisValue)
  95. if (item) {
  96. return item[valueName]
  97. } else {
  98. return '-'
  99. }
  100. })
  101. legendItem += ''
  102. seriesObj[legendItem] = {
  103. name: legendItem,
  104. data: seriesData,
  105. type: 'bar'
  106. }
  107. return legendItem
  108. })
  109. } else {
  110. console.log("进入了1个维度====================")
  111. this.schema.forEach((schema, index) => {
  112. // legend.push(schema.label || schema.name)
  113. legend.push(schema.Comment || schema.name)
  114. if (!schema.asxAxis) {
  115. seriesObj[schema.name] = {
  116. // name: schema.label || schema.name,
  117. name: schema.Comment || schema.name,
  118. data: [],
  119. // showSymbol: false,
  120. type: 'bar'
  121. }
  122. }
  123. data.forEach(item => {
  124. if (schema.asxAxis) {
  125. xAxisData.push(item[schema.name])
  126. } else {
  127. seriesObj[schema.name].data.push(item[schema.name])
  128. }
  129. })
  130. })
  131. }
  132. const option = {
  133. legend: {
  134. bottom: 0,
  135. type: 'scroll',
  136. data: legend
  137. },
  138. // color: ['#4097ff'],
  139. toolbox: {
  140. show: true,
  141. top: 0,
  142. itemSize: 12,
  143. feature: {
  144. saveAsImage: {
  145. show: true
  146. },
  147. magicType: {
  148. type: ['line', 'bar']
  149. },
  150. restore: {
  151. show: true
  152. },
  153. dataZoom: {
  154. show: true
  155. }
  156. }
  157. },
  158. grid: {
  159. top: '10px',
  160. left: '45px',
  161. right: '0',
  162. bottom: '50px'
  163. },
  164. tooltip: {
  165. trigger: 'axis',
  166. axisPointer: {
  167. type: 'shadow'
  168. }
  169. },
  170. xAxis: {
  171. type: 'category',
  172. axisLabel: {
  173. color: '#95a4bd'
  174. },
  175. axisLine: {
  176. lineStyle: {
  177. color: '#95a4bd'
  178. }
  179. },
  180. splitArea: {
  181. show: true,
  182. interval: 0
  183. },
  184. data: xAxisData
  185. },
  186. yAxis: {
  187. axisLabel: {
  188. show: true,
  189. color: '#95a4bd',
  190. formatter: labelFormatter
  191. },
  192. axisLine: {
  193. lineStyle: {
  194. color: '#95a4bd'
  195. }
  196. },
  197. splitLine: {
  198. lineStyle: {
  199. type: 'dashed'
  200. }
  201. }
  202. },
  203. series: Object.values(seriesObj)
  204. }
  205. setTimeout(() => {
  206. if (!this.chart) {
  207. this.chart = echarts.init(this.$refs.chart, 'macarons')
  208. }
  209. this.chart.clear()
  210. this.chart.setOption(option)
  211. if (this.chartOpt) {
  212. this.chart.setOption(this.chartOpt)
  213. }
  214. }, 0)
  215. }
  216. }
  217. }
  218. </script>