596f2ac5e634fd741504c44c746ca2ca9e4b4e50.svn-base 4.8 KB

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