index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div class="app-echats-kline">
  3. <template v-if="loading">
  4. <div class="app-echats-kline__tip">正在加载...</div>
  5. </template>
  6. <template v-else-if="isEmpty">
  7. <div class="app-echats-kline__tip">暂无数据</div>
  8. </template>
  9. <template v-else>
  10. <div class="app-echats-kline__container main">
  11. <ul class="legend">
  12. <li class="legend-item">开: {{ selectedItem.open }}</li>
  13. <li class="legend-item">收: {{ selectedItem.close }}</li>
  14. <li class="legend-item">高: {{ selectedItem.highest }}</li>
  15. <li class="legend-item">低: {{ selectedItem.lowest }}</li>
  16. <li class="legend-item">MA5: {{ selectedItem.ma5 }}</li>
  17. <li class="legend-item">MA10: {{ selectedItem.ma10 }}</li>
  18. <li class="legend-item">MA15: {{ selectedItem.ma15 }}</li>
  19. </ul>
  20. <app-echarts :option="options.candlestick" v-model:dataIndex="dataIndex" @ready="mainReady" />
  21. </div>
  22. <template v-if="showIndicator">
  23. <div class="app-echats-kline__container indicator">
  24. <!-- MACD -->
  25. <section class="section" v-if="activeSeriesType === ChartSeriesType.MACD">
  26. <ul class="legend">
  27. <li class="legend-item">MACD: {{ selectedItem.macd }}</li>
  28. <li class="legend-item">DIF: {{ selectedItem.dif }}</li>
  29. <li class="legend-item">DEA: {{ selectedItem.dea }}</li>
  30. </ul>
  31. <app-echarts :option="options.macd" v-model:dataIndex="dataIndex" @ready="indicatorReady" />
  32. </section>
  33. <!-- VOL -->
  34. <section class="section" v-if="activeSeriesType === ChartSeriesType.VOL">
  35. <ul class="legend">
  36. <li class="legend-item">VOL: {{ selectedItem.vol }}</li>
  37. </ul>
  38. <app-echarts :option="options.vol" v-model:dataIndex="dataIndex" @ready="indicatorReady" />
  39. </section>
  40. <!-- KDJ -->
  41. <section class="section" v-if="activeSeriesType === ChartSeriesType.KDJ">
  42. <ul class="legend">
  43. <li class="legend-item">K: {{ selectedItem.k }}</li>
  44. <li class="legend-item">D: {{ selectedItem.d }}</li>
  45. <li class="legend-item">J: {{ selectedItem.j }}</li>
  46. </ul>
  47. <app-echarts :option="options.kdj" v-model:dataIndex="dataIndex" @ready="indicatorReady" />
  48. </section>
  49. <!-- CCI -->
  50. <section class="section" v-if="activeSeriesType === ChartSeriesType.CCI">
  51. <ul class="legend">
  52. <li class="legend-item">CCI: {{ selectedItem.cci }}</li>
  53. </ul>
  54. <app-echarts :option="options.cci" v-model:dataIndex="dataIndex" @ready="indicatorReady" />
  55. </section>
  56. </div>
  57. <app-tab theme="menu" :data-list="chartSeriesTypeList" @change="tabChange" />
  58. </template>
  59. </template>
  60. </div>
  61. </template>
  62. <script lang="ts" setup>
  63. import { ref, PropType, watch, onBeforeUnmount } from 'vue'
  64. import { echarts } from '@/components/base/echarts/core'
  65. import { ChartCycleType, ChartSeriesType, getChartSeriesTypeList } from '@/constants/enum/chart'
  66. import { useCandlestickChart } from '@/hooks/echarts/candlestick'
  67. import AppEcharts from '@/components/base/echarts/index.vue'
  68. import AppTab from '@/components/base/tab/index.vue'
  69. import subscribe from '@/services/subscribe'
  70. const props = defineProps({
  71. goodscode: {
  72. type: String,
  73. required: true,
  74. },
  75. // 周期类型
  76. cycleType: {
  77. type: Number as PropType<ChartCycleType>,
  78. default: ChartCycleType.minutes,
  79. },
  80. // 是否显示指标
  81. showIndicator: {
  82. type: Boolean,
  83. default: true,
  84. },
  85. })
  86. const { loading, dataIndex, isEmpty, options, selectedItem, initData, initOptions } = useCandlestickChart(props.goodscode);
  87. const activeSeriesType = ref(ChartSeriesType.MACD); // 当前选中的指标
  88. const chartGroup = new Map<string, echarts.ECharts>(); // 图表联动实例组
  89. const chartSeriesTypeList = getChartSeriesTypeList();
  90. // 指标切换
  91. const tabChange = (index: number) => {
  92. activeSeriesType.value = chartSeriesTypeList[index].value;
  93. setTimeout(() => {
  94. initOptions();
  95. }, 0);
  96. }
  97. const mainReady = (chart: echarts.ECharts) => {
  98. chartGroup.set('main', chart);
  99. initOptions();
  100. }
  101. const indicatorReady = (chart: echarts.ECharts) => {
  102. chartGroup.set('indicator', chart);
  103. echarts.connect([...chartGroup.values()]); // 图表联动
  104. }
  105. // 行情订阅
  106. const sub = subscribe.addQuoteSubscribe([props.goodscode]);
  107. // 监听周期选择变化
  108. watch(() => props.cycleType, (val) => {
  109. initData(val);
  110. }, {
  111. immediate: true
  112. })
  113. onBeforeUnmount(() => {
  114. sub.stop();
  115. })
  116. </script>
  117. <style lang="less">
  118. @import './index.less';
  119. </style>