index.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <template>
  2. <div
  3. :id="config.code + 'wrap'"
  4. class="bs-design-wrap bs-bar"
  5. style="width: 100%; height: 100%"
  6. >
  7. <div
  8. :id="`chart${config.code}`"
  9. style="width: 100%; height: 100%"
  10. />
  11. </div>
  12. </template>
  13. <script>
  14. import 'insert-css'
  15. import * as echarts from 'echarts'
  16. import commonMixins from 'data-room-ui/js/mixins/commonMixins.js'
  17. import paramsMixins from 'data-room-ui/js/mixins/paramsMixins'
  18. import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
  19. import cloneDeep from 'lodash/cloneDeep'
  20. export default {
  21. name: 'Candlestick',
  22. mixins: [paramsMixins, commonMixins, linkageMixins],
  23. props: {
  24. id: {
  25. type: String,
  26. default: ''
  27. },
  28. config: {
  29. type: Object,
  30. default: () => ({})
  31. }
  32. },
  33. data () {
  34. return {
  35. currentDeep: 0,
  36. mapList: [],
  37. charts: null,
  38. hasData: false,
  39. level: '',
  40. option: {},
  41. xData: [],
  42. yData: []
  43. }
  44. },
  45. computed: {
  46. },
  47. watch: {
  48. },
  49. mounted () {
  50. this.chartInit()
  51. // 监听窗口或者父盒子大小变化
  52. this.chartResize()
  53. },
  54. beforeDestroy () {
  55. this.charts?.clear()
  56. },
  57. methods: {
  58. chartResize () {
  59. window.addEventListener('resize', () => {
  60. if (this.charts) {
  61. this.charts.resize()
  62. }
  63. })
  64. const dom = document.getElementById(`${this.config.code}wrap`)
  65. if (dom) {
  66. this.resizeObserver = new ResizeObserver(() => {
  67. if (this.charts) {
  68. this.charts.resize()
  69. }
  70. })
  71. this.resizeObserver.observe(dom)
  72. }
  73. },
  74. chartInit () {
  75. const config = this.config
  76. // key和code相等,说明是一进来刷新,调用list接口
  77. if (this.config.code === this.config.key || this.isPreview) {
  78. // 改变数据
  79. this.changeDataByCode(config).then((res) => {
  80. // 改变样式
  81. // config = this.changeStyle(res)
  82. this.newChart(config)
  83. }).catch(() => {
  84. })
  85. } else {
  86. // 否则说明是更新,这里的更新只指更新数据(改变样式时是直接调取changeStyle方法),因为更新数据会改变key,调用chart接口
  87. this.changeData(config).then((res) => {
  88. // 初始化图表
  89. this.newChart(config)
  90. })
  91. }
  92. },
  93. /**
  94. * 数据格式化
  95. * 该方法继承自commonMixins
  96. * @param {*} config
  97. * @param {Array} data
  98. */
  99. dataFormatting (config, _data) {
  100. const data = _data?.data
  101. if (data && data.length) {
  102. this.xData = data.map(item => item[config.dataSource.xfield])
  103. this.yData = data.map(item => [item[config.dataSource.openField], item[config.dataSource.closeField], item[config.dataSource.lowField], item[config.dataSource.highField]])
  104. } else {
  105. this.xData = ['2017-10-24', '2017-10-25', '2017-10-26', '2017-10-27']
  106. this.yData = [
  107. [20, 34, 10, 38],
  108. [40, 35, 30, 50],
  109. [31, 38, 33, 44],
  110. [38, 15, 5, 42]
  111. ]
  112. }
  113. return config
  114. },
  115. // 更新图表数据
  116. updateChartData (config) {
  117. this.handleOption(config)
  118. this.charts.setOption(this.option)
  119. },
  120. changeStyle (config) {
  121. this.handleOption(config)
  122. this.charts.setOption(this.option)
  123. },
  124. /**
  125. * 初始化地图
  126. * 该方法继承自commonMixins
  127. * @param {*} config
  128. */
  129. async newChart (config) {
  130. this.charts = echarts.init(
  131. document.getElementById(`chart${this.config.code}`)
  132. )
  133. // 处理option,将配置项转换为echarts的option
  134. this.handleOption(config)
  135. this.charts.setOption(this.option)
  136. },
  137. /**
  138. * 处理配置项option
  139. * @param {*} config
  140. */
  141. handleOption (config) {
  142. this.option = {
  143. xAxis: [
  144. {
  145. show: true,
  146. name: config.customize.xAxis.name,
  147. nameGap: 30,
  148. data: this.xData,
  149. nameTextStyle: {
  150. color: config.customize.xAxis.nameColor,
  151. fontSize: config.customize.xAxis.nameSize
  152. },
  153. nameLocation: config.customize.xAxis.position,
  154. // 坐标轴刻度设置
  155. axisTick: {
  156. show: true,
  157. alignWithLabel: true,
  158. lineStyle: {
  159. width: config.customize.xAxis.tickWidth,
  160. color: config.customize.xAxis.tickColor
  161. }
  162. },
  163. // 是否显示坐标轴的轴线
  164. axisLine: {
  165. show: true,
  166. lineStyle: {
  167. color: config.customize.xAxis.lineColor,
  168. width: config.customize.xAxis.lineWidth
  169. }
  170. },
  171. // 坐标轴刻度标签
  172. axisLabel: {
  173. show: true,
  174. textStyle: {
  175. fontSize: config.customize.xAxis.labelSize,
  176. color: config.customize.xAxis.labelColor
  177. },
  178. margin: 30
  179. }
  180. }
  181. ],
  182. yAxis: {
  183. name: config.customize.yAxis.name,
  184. nameGap: 10,
  185. nameTextStyle: {
  186. color: config.customize.yAxis.nameColor,
  187. fontSize: config.customize.yAxis.nameSize
  188. },
  189. nameLocation: config.customize.yAxis.position,
  190. show: true,
  191. axisLabel: {
  192. show: true,
  193. textStyle: {
  194. fontSize: config.customize.yAxis.labelSize,
  195. color: config.customize.yAxis.labelColor
  196. },
  197. margin: 10
  198. },
  199. axisTick: {
  200. show: true,
  201. length: 1,
  202. lineStyle: {
  203. width: config.customize.yAxis.tickWidth,
  204. color: config.customize.yAxis.tickColor
  205. }
  206. },
  207. // 分隔线
  208. splitLine: {
  209. show: config.customize.gridShow, // yAxis.show配置为true时,该配置才有效
  210. lineStyle: {
  211. color: config.customize.gridColor,
  212. width: config.customize.gridWidth
  213. }
  214. },
  215. // y轴轴线是否显示
  216. axisLine: {
  217. show: true,
  218. lineStyle: {
  219. color: config.customize.yAxis.lineColor,
  220. width: config.customize.yAxis.lineWidth
  221. }
  222. }
  223. },
  224. tooltip: {
  225. // 显示提示框
  226. show: true,
  227. trigger: 'item',
  228. backgroundColor: 'rgba(50,50,50,0.7)',
  229. borderColor: '#333',
  230. borderWidth: 1,
  231. textStyle: {
  232. color: '#fff',
  233. fontSize: 12,
  234. fontWeight: 'normal'
  235. },
  236. axisPointer: {
  237. type: 'line'
  238. }
  239. },
  240. series: [
  241. {
  242. type: 'candlestick',
  243. label: {
  244. show: true,
  245. position: 'inside',
  246. color: '#fff',
  247. fontSize: 12
  248. },
  249. itemStyle: {
  250. color: config.customize.highColor,
  251. color0: config.customize.lowColor
  252. },
  253. data: this.yData
  254. }
  255. ]
  256. }
  257. }
  258. }
  259. }
  260. </script>
  261. <style lang="scss" scoped>
  262. @import '../../assets/style/echartStyle';
  263. .light-theme {
  264. background-color: #ffffff;
  265. color: #000000;
  266. }
  267. .auto-theme {
  268. background-color: rgba(0, 0, 0, 0);
  269. }
  270. .bs-design-wrap {
  271. position: relative;
  272. .button {
  273. position: absolute;
  274. z-index: 999;
  275. }
  276. }
  277. </style>