e639422e077d1516e0c961bd1b60a74f8dfdbbae.svn-base 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. legend.push(schema.Comment || schema.name)
  107. if (!schema.asxAxis) {
  108. seriesObj[schema.name] = {
  109. // name: schema.label || schema.name,
  110. name: schema.Comment || schema.name,
  111. data: [],
  112. // showSymbol: false,
  113. type: 'line'
  114. }
  115. }
  116. data.forEach(item => {
  117. if (schema.asxAxis && !xAxisData.includes(item[schema.name])) {
  118. xAxisData.push(item[schema.name])
  119. } else {
  120. seriesObj[schema.name].data.push(item[schema.name])
  121. }
  122. })
  123. })
  124. }
  125. const option = {
  126. legend: {
  127. bottom: 0,
  128. type: 'scroll',
  129. data: legend
  130. },
  131. // color: ['#4097ff'],
  132. toolbox: {
  133. show: true,
  134. itemSize: 12,
  135. top: 0,
  136. feature: {
  137. saveAsImage: {
  138. show: true
  139. },
  140. magicType: {
  141. type: ['line', 'bar']
  142. },
  143. restore: {
  144. show: true
  145. },
  146. dataZoom: {
  147. show: true
  148. }
  149. }
  150. },
  151. grid: {
  152. // show: true,
  153. top: '10px',
  154. left: '45px',
  155. right: '0',
  156. bottom: '50px'
  157. },
  158. tooltip: {
  159. trigger: 'axis',
  160. axisPointer: {
  161. type: 'cross'
  162. }
  163. },
  164. xAxis: {
  165. type: 'category',
  166. axisLabel: {
  167. color: '#95a4bd'
  168. },
  169. axisLine: {
  170. lineStyle: {
  171. color: '#95a4bd'
  172. }
  173. },
  174. data: xAxisData
  175. },
  176. yAxis: {
  177. axisLabel: {
  178. show: true,
  179. color: '#95a4bd',
  180. formatter: labelFormatter
  181. },
  182. axisLine: {
  183. lineStyle: {
  184. color: '#95a4bd'
  185. }
  186. },
  187. splitLine: {
  188. lineStyle: {
  189. type: 'dashed'
  190. }
  191. }
  192. },
  193. series: Object.values(seriesObj)
  194. }
  195. setTimeout(() => {
  196. if (!this.chart) {
  197. this.chart = echarts.init(this.$refs.chart, 'macarons')
  198. }
  199. this.chart.clear()
  200. this.chart.setOption(option)
  201. if (this.chartOpt) {
  202. this.chart.setOption(this.chartOpt)
  203. }
  204. }, 0)
  205. }
  206. }
  207. }
  208. </script>