fa00f71ea6e65ce4dfa0a4b39fab9679fb770e3b.svn-base 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. console.log('xAxisName======', legendName)
  82. console.log('legendName======', legendName)
  83. var second_name = this.schema.filter(function (i) {
  84. return i.is_second == true;
  85. })
  86. console.log('second_name', second_name)
  87. console.log('data', this.data)
  88. const valueName = this.schema.find(schema => !schema.asxAxis).name
  89. console.log('valueName======', valueName)
  90. xAxisData = this.data.map(item => {
  91. legend.push(item[legendName])
  92. return item[xAxisName]
  93. })
  94. xAxisData = Array.from(new Set(xAxisData))
  95. legend = Array.from(new Set(legend))
  96. console.log('legend======', legend)
  97. console.log('xAxisData======', xAxisData)
  98. legend = legend.map((legendItem, index) => {
  99. const seriesData = xAxisData.map(xAxisValue => {
  100. const item = data.find(dataItem => dataItem[legendName] === legendItem && dataItem[xAxisName] === xAxisValue)
  101. console.log('item==============', item)
  102. if (item) {
  103. return item[valueName]
  104. } else {
  105. return '-'
  106. }
  107. })
  108. legendItem += ''
  109. console.log('legendItem==============', legendItem)
  110. seriesObj[legendItem] = {
  111. name: legendItem,
  112. data: seriesData,
  113. type: 'line'
  114. }
  115. return legendItem
  116. })
  117. } else {
  118. console.log("进入了1个维度====================")
  119. this.schema.forEach((schema, index) => {
  120. legend.push(schema.Comment || schema.name)
  121. // if (!schema.asxAxis) {
  122. // seriesObj[schema.name] = {
  123. // name: schema.Comment || schema.name,
  124. // data: [],
  125. // type: 'bar'
  126. // }
  127. // }
  128. if (!schema.asxAxis) {
  129. if (schema.is_second == true) {
  130. seriesObj[schema.name] = {
  131. name: schema.Comment || schema.name,
  132. data: [],
  133. type: 'line'
  134. }
  135. } else {
  136. seriesObj[schema.name] = {
  137. name: schema.Comment || schema.name,
  138. data: [],
  139. type: 'bar'
  140. }
  141. }
  142. }
  143. data.forEach(item => {
  144. if (schema.asxAxis) {
  145. xAxisData.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. top: 0,
  162. itemSize: 12,
  163. feature: {
  164. saveAsImage: {
  165. show: true
  166. },
  167. // magicType: {
  168. // type: ['line', 'bar']
  169. // },
  170. restore: {
  171. show: true
  172. },
  173. dataZoom: {
  174. show: true
  175. }
  176. }
  177. },
  178. grid: {
  179. // show: true,
  180. top: '10px',
  181. left: '74px',
  182. right: '30px',
  183. bottom: '50px'
  184. },
  185. tooltip: {
  186. trigger: 'axis',
  187. axisPointer: {
  188. type: 'shadow'
  189. }
  190. },
  191. xAxis: {
  192. type: 'category',
  193. axisLabel: {
  194. color: '#95a4bd'
  195. },
  196. axisLine: {
  197. lineStyle: {
  198. color: '#95a4bd'
  199. }
  200. },
  201. splitArea: {
  202. show: true,
  203. interval: 0
  204. },
  205. data: xAxisData
  206. },
  207. yAxis: {
  208. type: 'value',
  209. axisLabel: {
  210. show: true,
  211. color: '#95a4bd',
  212. formatter: labelFormatter
  213. },
  214. axisLine: {
  215. lineStyle: {
  216. color: '#95a4bd'
  217. }
  218. },
  219. splitLine: {
  220. lineStyle: {
  221. type: 'dashed'
  222. }
  223. }
  224. },
  225. series: Object.values(seriesObj)
  226. }
  227. setTimeout(() => {
  228. if (!this.chart) {
  229. this.chart = echarts.init(this.$refs.chart, 'macarons')
  230. }
  231. this.chart.clear()
  232. this.chart.setOption(option)
  233. if (this.chartOpt) {
  234. this.chart.setOption(this.chartOpt)
  235. }
  236. }, 0)
  237. }
  238. }
  239. }
  240. </script>