lxf
2025-07-05 872daddd00287cd44963483ba15f997d25a3da53
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<template>
  <div id="kline" class="boxDisplay" :style="{ height: `${props.height || defaultH}px`, position: 'relative' }"
    v-if="defaultH">
  </div>
  <ul class="flex px-4 py-4 box-border justify-between indicator-box" v-if="showBottom"
    style="border-top:1px solid rgba(68,75,88,0.2);">
    <li v-for="item in subTechnicalIndicatorTypes" :key="item" class="mr-2" :class="{ 'textColor': typeValue === item }"
      @click="choiceType(item)">{{ item }}
    </li>
  </ul>
</template>
 
<script setup>
import { init, dispose } from 'klinecharts'
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import config from './config'
import { SET_STAGE } from '@/store/types.store';
import fakeData from './fake-data'
import { _getKline } from "@/service/trade.api";
import { WS_URL } from '@/config'
import { useQuotesStore } from '@/store/quotes.store'
import { useI18n } from "vue-i18n";
const { t } = useI18n()
const quotesStore = useQuotesStore()
 
let chart = null
const paneId = ref('')
const typeValue = ref('MA')//图形类型
const subTechnicalIndicatorTypes = ref(['MA', 'EMA', 'BOLL', 'VOL', 'MACD', 'KDJ', 'RSI'])
 
const data = ref(fakeData)
 
const defaultH = ref(0)
const loading = ref(false)
 
const socket = ref(null)
 
const emits = defineEmits(['data', 'loading'])
 
onMounted(() => {
  defaultH.value = window.innerHeight - 94
  nextTick(async () => {
    await initData1()
    await initData()
    startQuoteScoket()
  })
})
 
const props = defineProps({
  symbol: {
    type: String
  },
  height: {
    type: Number
  },
  chartType: {
    type: String,
    default: 'candle_solid'
  },
  showBottom: {
    type: Boolean,
    default: true
  },
  isShowsolid: {
    type: Boolean,
    default: false
  }
})
 
const startQuoteScoket = () => {
  closeSocket()
  socket.value = new WebSocket(`${WS_URL}/1/${props.symbol}`)
  socket.value.onmessage = (evt) => {
    const { data } = evt
    let { code, data: _data } = JSON.parse(data)
  // _data[0].high = Math.floor(Math.random() * 100) + 1
  // _data[0].low = Math.floor(Math.random() * 100) + 1
  // _data[0].close = Math.floor(Math.random() * 100) + 1
  // _data[0].open = Math.floor(Math.random() * 100) + 1
    if (code / 1 === 0) {
      emits('data', _data[0])
      updateCharts(_data[0])
    }
  }
}
 
onBeforeUnmount(() => {
  closeSocket()
})
 
const closeSocket = () => {
  socket.value && socket.value.close()
  socket.value = null
}
 
 
const updateCharts = async (nowData) => {
  const dataList = chart.getDataList()
  if (!dataList || dataList.length === 0) {
    return
  }
  const lastData = dataList[dataList.length - 1]
  // const nowData = this.updateData
  // const timeValue = this.timeValue
  // nowData.timestamp = nowData.ts
  // 防止停盘后柱状图自动刷新修改
  if ((nowData.timestamp - lastData.timestamp) >= quotesStore.seconds) {
    data.value = await _getKline(props.symbol, quotesStore.stage === 'timeSharing' ? '1min' : quotesStore.stage)
    chart.applyNewData(data.value);
    return false
  }
 
  const newData = {
    close: nowData.close / 1,
    current_time: lastData.current_time,
    high: lastData.high > nowData.close / 1 ? lastData.high : (nowData.close / 1),
    // high: lastData.high,
    line: quotesStore.stage === 'timeSharing' ? '1min' : quotesStore.stage, // timeValue.id,
    low: lastData.low < nowData.close / 1 ? lastData.low : (nowData.close / 1),
    // low: lastData.low,
    open: lastData.open,
    symbol: lastData.symbol,
    // ts: lastData.ts, //
    // timestamp: lastData.timestamp, //
    // timestamp: nowData.ts || nowData.timestamp, //
    timestamp: (nowData.timestamp - lastData.timestamp) < quotesStore.seconds ? lastData.timestamp : (lastData.timestamp + quotesStore.seconds),
    // volume: lastData.volume / 1,
    volume: nowData.volume / 1
  }
  nextTick(() => {
    chart.setStyleOptions({
      candle: {
        type: props.chartType
      },
      yAxis: {
        width: quotesStore.stage === 'timeSharing' ? 0 : null,
      }
    })
    chart.updateData(newData)
  })
}
 
const initData = async () => {
  chart = init('kline', config);
  chart.setOffsetRightSpace(15)
  chart.setDataSpace(10)
  if (props.isShowsolid) {
    chart.createTechnicalIndicator('MA', false, { id: 'candle_pane' });
    paneId.value = chart.createTechnicalIndicator('MA');
  }
 
  // this.fetchData()
  chart.setStyleOptions({
    candle: {
      type: props.chartType
    },
    yAxis: {
      width: quotesStore.stage === 'timeSharing' ? 0 : null,
    }
  })
  // quotesStore[SET_STAGE]({ stage: 'timeSharing', seconds: 1000 })
 
  if (!quotesStore.stage) {
    quotesStore[SET_STAGE]({ stage: '1min', seconds: 1000 })
  }
 
  emits('loading', true)
  try {
    data.value = await _getKline(props.symbol, quotesStore.stage === 'timeSharing' ? '1min' : quotesStore.stage)
  } catch (e) {
    emits('loading', false)
  }
  emits('loading', false)
  let length = 2
  try {
    length = data.value[data.value.length - 1].decimals
  } catch (e) {
    length = 2
  }
  chart.setPriceVolumePrecision(length, 2)
  localStorage.setItem('kline', JSON.stringify(data.value))
 
  nextTick(() => {
    chart.applyNewData(data.value);
  })
}
const initData1 = async () => {
  chart = init('kline', config);
  chart.setOffsetRightSpace(15)
  chart.setDataSpace(10)
  let length = 2
  data.value = JSON.parse(localStorage.getItem('kline'))
  try {
    length = data.value[data.value.length - 1].decimals
  } catch (e) {
    length = 2
  }
  chart.setPriceVolumePrecision(length, 2)
  // if (props.type === 'candle_solid') {
  //     chart.createTechnicalIndicator('MA', false, { id: 'candle_pane' });
  //     paneId.value = chart.createTechnicalIndicator('VOL');
  // }
 
  // this.fetchData()
  chart.setStyleOptions({
    candle: {
      type: props.chartType
    }
  })
  if (!quotesStore.stage) {
    quotesStore[SET_STAGE]({ stage: '1min', seconds: 1000 })
  }
  // quotesStore[SET_STAGE]({ stage: 'timeSharing', seconds: 1000 })
 
  // console.log(data.value)
 
  nextTick(() => {
    chart.applyNewData(data.value);
  })
}
const choiceType = (type) => { // 选择副线
  typeValue.value = type
  chart.createTechnicalIndicator(type, false, { id: paneId.value })
}
 
</script>
<style lang="scss" scoped>
.textColor {
  color: $color_main;
}
 
.indicator-box {
  font-size: 12px;
  color: $text_color;
}
</style>