index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <template>
  2. <div
  3. class="bs-design-wrap bs-bar"
  4. style="width: 100%; height: 100%"
  5. >
  6. <div
  7. :id="`chart${config.code}`"
  8. style="width: 100%; height: 100%"
  9. />
  10. </div>
  11. </template>
  12. <script>
  13. import 'insert-css'
  14. import * as echarts from 'echarts'
  15. import commonMixins from 'data-room-ui/js/mixins/commonMixins.js'
  16. import paramsMixins from 'data-room-ui/js/mixins/paramsMixins'
  17. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  18. export default {
  19. name: 'MapCharts',
  20. mixins: [paramsMixins, commonMixins, linkageMixins],
  21. props: {
  22. id: {
  23. type: String,
  24. default: ''
  25. },
  26. config: {
  27. type: Object,
  28. default: () => ({})
  29. }
  30. },
  31. data () {
  32. return {
  33. currentDeep: 0,
  34. mapList: [],
  35. charts: null,
  36. hasData: false,
  37. level: '',
  38. option: {},
  39. xData: [],
  40. yData: []
  41. }
  42. },
  43. computed: {
  44. Data () {
  45. return JSON.parse(JSON.stringify(this.config))
  46. }
  47. },
  48. watch: {
  49. Data: {
  50. handler (newVal, oldVal) {
  51. if (newVal.w !== oldVal.w || newVal.h !== oldVal.h) {
  52. this.$nextTick(() => {
  53. this.charts.resize()
  54. })
  55. }
  56. },
  57. deep: true
  58. }
  59. },
  60. mounted () {
  61. this.chartInit()
  62. },
  63. beforeDestroy () {
  64. this.charts?.clear()
  65. },
  66. methods: {
  67. chartInit () {
  68. const config = this.config
  69. // key和code相等,说明是一进来刷新,调用list接口
  70. if (this.config.code === this.config.key || this.isPreview) {
  71. // 改变数据
  72. this.changeDataByCode(config).then((res) => {
  73. // 改变样式
  74. // config = this.changeStyle(res)
  75. this.newChart(config)
  76. }).catch(() => {
  77. })
  78. } else {
  79. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  80. this.changeData(config).then((res) => {
  81. // 初始化图表
  82. this.newChart(config)
  83. })
  84. }
  85. },
  86. /**
  87. * 数据格式化
  88. * 该方法继承自commonMixins
  89. * @param {*} config
  90. * @param {Array} data
  91. */
  92. dataFormatting (config, _data) {
  93. console.log('dataFormatting', _data)
  94. const data = _data?.data
  95. if (data && data.length) {
  96. this.xData = data.map(item => item[config.dataSource.x])
  97. this.yData = data.map(item => [item[config.dataSource.openField], item[config.dataSource.closeField], item[config.dataSource.lowField], item[config.dataSource.highField]])
  98. } else {
  99. this.xData = ['2017-10-24', '2017-10-25', '2017-10-26', '2017-10-27']
  100. this.yData = [
  101. [20, 34, 10, 38],
  102. [40, 35, 30, 50],
  103. [31, 38, 33, 44],
  104. [38, 15, 5, 42]
  105. ]
  106. }
  107. return config
  108. },
  109. // 更新图表数据
  110. updateChartData (config) {
  111. this.handleOption(config)
  112. this.charts.setOption(this.option)
  113. },
  114. /**
  115. * 初始化地图
  116. * 该方法继承自commonMixins
  117. * @param {*} config
  118. */
  119. async newChart (config) {
  120. this.charts = echarts.init(
  121. document.getElementById(`chart${this.config.code}`)
  122. )
  123. // 处理option,将配置项转换为echarts的option
  124. this.handleOption(config)
  125. this.charts.setOption(this.option)
  126. },
  127. /**
  128. * 处理配置项option
  129. * @param {*} config
  130. */
  131. handleOption (config) {
  132. this.option = {
  133. xAxis: {
  134. data: this.xData
  135. },
  136. yAxis: {},
  137. series: [
  138. {
  139. type: 'candlestick',
  140. data: this.yData
  141. }
  142. ]
  143. }
  144. }
  145. }
  146. }
  147. </script>
  148. <style lang="scss" scoped>
  149. @import '../../assets/style/echartStyle';
  150. .light-theme {
  151. background-color: #ffffff;
  152. color: #000000;
  153. }
  154. .auto-theme {
  155. background-color: rgba(0, 0, 0, 0);
  156. }
  157. .bs-design-wrap {
  158. position: relative;
  159. .button {
  160. position: absolute;
  161. z-index: 999;
  162. }
  163. }
  164. </style>