10.10综合交易所原始源码_移动端
1
admin
2026-01-06 03043192ddf00f9a36b7454799a9152cd1b50a0b
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<template>
  <div id="cryptos">
    <div class="kline-charts">
      <ul class="flex px-8 pb-5 box-border justify-between text-grey fontStyle text-26"
        style="border-bottom:1px solid rgba(68,75,88,0.2);">
        <template v-if="!isChange">
          <li v-for="item in timeList" :key="item.id" class="mr-2" :class="{ 'textColor': item.id === timeValue.id }"
            @click="choiceTime(item)">{{ item.text }}</li>
        </template>
        <template v-else>
          <template v-if="isNight">
            <li v-for="item in timeList" :key="item.id" class="mr-2" :class="{ 'text-white': item.id === timeValue.id }"
              @click="choiceTime(item)">{{ item.text }}</li>
          </template>
          <template v-if="!isNight">
            <li v-for="item in timeList" :key="item.id" class="mr-2" :class="{ 'text-black': item.id === timeValue.id }"
              @click="choiceTime(item)">{{ item.text }}</li>
          </template>
        </template>
      </ul>
      <div class="relative">
        <div id="kline" class="h-192"></div>
        <div
          class="flex justify-center items-center text-center text-grey absolute left-0 top-0  w-full h-full z-10 mainBackground"
          v-if="chartLoading">
          <van-loading type="spinner"></van-loading>
        </div>
      </div>
      <ul class="flex px-8 py-5 box-border justify-between text-grey text-26" v-if="showBottom"
        style="border-top:1px solid rgba(68,75,88,0.2);">
        <template v-if="!isChange">
          <li v-for="item in subTechnicalIndicatorTypes" :key="item" class="mr-5"
            :class="{ 'textColor': typeValue === item }" @click="choiceType(item)">{{ item }}</li>
        </template>
        <template v-else>
          <template v-if="isNight">
            <li v-for="item in subTechnicalIndicatorTypes" :key="item" class="mr-5"
              :class="{ 'text-white': typeValue === item }" @click="choiceType(item)">{{ item }}</li>
          </template>
          <template v-if="!isNight">
            <li v-for="item in subTechnicalIndicatorTypes" :key="item" class="mr-5"
              :class="{ 'text-black': typeValue === item }" @click="choiceType(item)">{{ item }}</li>
          </template>
        </template>
      </ul>
    </div>
  </div>
</template>
<script>
import { init, dispose } from 'klinecharts'
let chart = null
import { _getKline } from "@/service/trade.api";
import config from './config'
import { Loading } from 'vant';
export default {
  name: 'KlineCharts',
  data() {
    return {
      // symbol: 'btc',
      dealInfo: {},//交易对信息
 
      timeValue: {}, // 当前k线周期
      subTechnicalIndicatorTypes: ['MA', 'EMA', 'BOLL', 'VOL', 'MACD', 'KDJ', 'RSI'],
      typeValue: 'VOL',//图形类型
      klinecharts: null,//K线图实例
      chart: null,
      // resolution: '1',//分辨率
      // lang: 'en', //语言
      chartLoading: true, //加载动画
      paneId: '',
      chartData: [], // 图表数据
      timer: null
    }
  },
  computed: {
    //时间列表
    timeList() {
      // console.log("ddddddddd", this.timeListNew, this.$route.query.type);
      if (this.$route.query.type == 'US-stocks') {
        return [
          { id: '1', time: '1', text: '1s' + this.$t('分'), ts: 1 * 60 * 1000 },
          { id: '15', time: '15', text: '15' + this.$t('分'), ts: 15 * 60 * 1000 },
          { id: '30', time: '30', text: '30' + this.$t('分'), ts: 30 * 60 * 1000 },
          { id: '45', time: '45', text: '45' + this.$t('分'), ts: 45 * 60 * 1000 },
          { id: '60', time: '60', text: '60' + this.$t('分'), ts: 60 * 60 * 1000 },
          { id: 'D', time: 'D', text: '1' + this.$t('天'), ts: 1 * 24 * 60 * 60 * 1000 },
          { id: 'W', time: 'W', text: '1' + this.$t('周'), ts: 7 * 24 * 60 * 60 * 1000 },
          { id: 'M', time: 'M', text: '1' + this.$t('月'), ts: 30 * 24 * 60 * 60 * 1000 },
        ]
      } else {
        return [
          { id: '1min', time: '1min', text: this.$t('分时'), ts: 1 * 60 * 1000 },
          { id: '1mins', time: '1min', text: '1' + this.$t('分'), ts: 1 * 60 * 1000 },
          { id: '5min', time: '5min', text: '5' + this.$t('分'), ts: 5 * 60 * 1000 },
          { id: '15min', time: '15min', text: '15' + this.$t('分'), ts: 15 * 60 * 1000 },
          { id: '30min', time: '30min', text: '30' + this.$t('分'), ts: 30 * 60 * 1000 },
          { id: '60min', time: '60min', text: '1' + this.$t('小时'), ts: 60 * 60 * 1000 },
          { id: '4hour', time: '4hour', text: '4' + this.$t('小时'), ts: 4 * 60 * 60 * 1000 },
          { id: '1day', time: '1day', text: '1' + this.$t('天'), ts: 24 * 60 * 60 * 1000 },
          { id: '1week', time: '1week', text: '1' + this.$t('周'), ts: 7 * 24 * 60 * 60 * 1000 },
          { id: '1mon', time: '1mon', text: '1' + this.$t('月'), ts: 30 * 24 * 60 * 60 * 1000 }
        ]
      }
    }
  },
  components: {
    [Loading.name]: Loading
  },
  props: {
    symbol: {
      type: String,
      default: ''
    },
    updateKey: {
      type: Number,
      default: 0
    },
    updateData: {
      type: Object,
      default() {
        return {}
      }
    },
    showBottom: {
      type: Boolean,
      default: true
    },
    isChangeLine: {
      type: Boolean,
      default: false
    },
    isNight: {
      type: Boolean,
      defalult: true
    },
    isChange: {
      type: Boolean,
      defalult: false
    }
  },
  mounted() {
    this.initData()
  },
  beforeUnmount() {
    dispose('kline')
  },
  watch: {
    isChangeLine(val) {
      this.clearTimer()
      this.fetchData()
    },
    updateKey() { // 更新charts
 
      const dataList = chart.getDataList()
      if (dataList.length > 0) {
        const lastData = dataList[dataList.length - 1]
        const nowData = this.updateData
        const timeValue = this.timeValue
        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: 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: (nowData.ts - lastData.ts) < timeValue.ts ? lastData.ts : (lastData.ts + timeValue.ts),
          volume: lastData.volume / 1
        }
        this.$nextTick(() => {
          this.setChartType()
          chart.updateData(newData)
        })
      }
    }
  },
  methods: {
    initData() {
      this.timeValue = this.timeList[0]
      chart = init('kline', config);
      chart.setOffsetRightSpace(25)
      chart.setDataSpace(10)
      chart.setPriceVolumePrecision(4, 2)
      chart.createTechnicalIndicator('MA', false, { id: 'candle_pane' });
      this.paneId = chart.createTechnicalIndicator('VOL');
      this.fetchData()
    },
    fetchData(time) { // 获取数据
      this.chartLoading = true
      _getKline(this.symbol, this.timeValue.time || '1').then(data => {
        this.chartLoading = false
        data.map(item => {
          item.timestamp = item.ts
        })
 
        let length = 2
        if (data[0].decimals) {
          length = data[0].decimals
        }
        //let length = data[0] ? data[0].close.toString().split('.')[1].length : 4
        chart.setPriceVolumePrecision(length, 2)
        this.setChartType()
        chart.applyNewData(data);
        this.$emit('updataLine', false)
      })
      // TODO:轮询,删掉上面那段,添加到定时器中
      // this.timer = setInterval(() => {
      //   _getKline(this.symbol, this.timeValue.time || time).then(data => {
      //     this.chartLoading = false
      //     data.map(item => {
      //       item.timestamp = item.ts
      //     })
      //     let str = data[0] ? data[0].close.toString() : ''
      //     let length = 2
      //     if (str.indexOf('.') != -1) {
      //       length = str.split('.')[1].length
      //     }
      //     //let length = data[0] ? data[0].close.toString().split('.')[1].length : 4
      //     chart.setPriceVolumePrecision(length, 2)
      //     this.setChartType()
      //     chart.applyNewData(data);
      //     this.$emit('updataLine', false)
      //   })
      // }, 1000);
    },
    setChartType() {
      let type = 'area'
      if (this.timeValue.id === '1') {
        type = 'area'
      } else {
        type = 'candle_solid'
      }
      chart.setStyleOptions({
        candle: {
          type
        }
      })
    },
    choiceTime(value) { // 选择周期
      this.timeValue = value
      this.clearTimer()
      this.fetchData()
    },
    choiceType(type) { // 选择副线
      this.typeValue = type
      chart.createTechnicalIndicator(type, false, { id: this.paneId })
    },
    clearTimer() {
      clearInterval(this.timer)
      this.timer = null
    },
  },
  deactivated() {
    this.clearTimer()
  }
}
</script>
<style lang="scss" scoped>
// #kline {
//   // min-height: 828px;
//   height: 1200px;
// }</style>