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