1
2026-04-30 c4579e2766c7147db493231040e4cc8eeaec26ce
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<template>
  <div >
    <div class="kline-charts">
      <ul class="flex px-32 pb-20 box-border justify-between text-grey fontStyle font-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-10" :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-10"
              :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-10"
              :class="{ 'text-black': item.id === timeValue.id }" @click="choiceTime(item)">{{ item.text }}</li>
          </template>
        </template>
      </ul>
      <div class="chart-with-tools flex">
        <div class="relative flex-1 min-w-0">
          <div id="kline" class="h-828"></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>
        <!-- 铅笔图标:控制绘图工具栏显示/隐藏 -->
        <div class="draw-tools-wrap flex">
          <div class="pencil-trigger draw-tool-item" :class="[isNight ? 'draw-toolbar-night' : 'draw-toolbar-light', { active: showDrawToolbar }]"
            :title="showDrawToolbar ? $t('隐藏绘图工具') : $t('显示绘图工具')" @click="showDrawToolbar = !showDrawToolbar">
            <svg viewBox="0 0 24 24" width="20" height="20"><path fill="currentColor" d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25zM20.71 7.04c.39-.39.39-1.02 0-1.41l-2.34-2.34c-.39-.39-1.02-.39-1.41 0l-1.83 1.83 3.75 3.75 1.83-1.83z"/></svg>
          </div>
          <!-- 绘图工具栏 -->
          <div v-show="showDrawToolbar" class="draw-toolbar" :class="isNight ? 'draw-toolbar-night' : 'draw-toolbar-light'">
          <div class="draw-tool-item" :class="{ active: activeDrawTool === 'fibonacciLine' }" title="斐波那契" @click="onDrawToolClick('fibonacciLine')">
            <svg viewBox="0 0 24 24" width="20" height="20"><path fill="currentColor" d="M4 20L20 4l-2-2L2 18v4h2z"/><circle cx="6" cy="18" r="1.5"/><circle cx="18" cy="6" r="1.5"/></svg>
          </div>
          <div class="draw-tool-item" :class="{ active: activeDrawTool === 'segment' }" title="线段" @click="onDrawToolClick('segment')">
            <svg viewBox="0 0 24 24" width="20" height="20"><line x1="4" y1="20" x2="20" y2="4" stroke="currentColor" stroke-width="2"/><circle cx="4" cy="20" r="2"/><circle cx="20" cy="4" r="2"/></svg>
          </div>
          <div class="draw-tool-item" :class="{ active: activeDrawTool === 'horizontalStraightLine' }" title="水平线" @click="onDrawToolClick('horizontalStraightLine')">
            <svg viewBox="0 0 24 24" width="20" height="20"><line x1="2" y1="12" x2="22" y2="12" stroke="currentColor" stroke-width="2"/><circle cx="12" cy="12" r="2"/></svg>
          </div>
          <div class="draw-tool-item" :class="{ active: activeDrawTool === 'horizontalSegment' }" title="水平线段" @click="onDrawToolClick('horizontalSegment')">
            <svg viewBox="0 0 24 24" width="20" height="20"><line x1="6" y1="12" x2="18" y2="12" stroke="currentColor" stroke-width="2"/><circle cx="6" cy="12" r="2"/><circle cx="18" cy="12" r="2"/></svg>
          </div>
          <div class="draw-tool-item" :class="{ active: activeDrawTool === 'verticalStraightLine' }" title="垂直线" @click="onDrawToolClick('verticalStraightLine')">
            <svg viewBox="0 0 24 24" width="20" height="20"><line x1="12" y1="2" x2="12" y2="22" stroke="currentColor" stroke-width="2"/><circle cx="12" cy="12" r="2"/></svg>
          </div>
          <div class="draw-tool-item" :class="{ active: activeDrawTool === 'verticalSegment' }" title="垂直线段" @click="onDrawToolClick('verticalSegment')">
            <svg viewBox="0 0 24 24" width="20" height="20"><line x1="12" y1="6" x2="12" y2="18" stroke="currentColor" stroke-width="2"/><circle cx="12" cy="6" r="2"/><circle cx="12" cy="18" r="2"/></svg>
          </div>
          <div class="draw-tool-item" :class="{ active: activeDrawTool === 'rect' }" title="矩形" @click="onDrawToolClick('rect')">
            <svg viewBox="0 0 24 24" width="20" height="20"><rect x="5" y="7" width="14" height="10" fill="none" stroke="currentColor" stroke-width="2"/><circle cx="5" cy="7" r="1.5"/><circle cx="19" cy="17" r="1.5"/></svg>
          </div>
          <div class="draw-tool-item" :class="{ active: activeDrawTool === 'triangle' }" title="三角形" @click="onDrawToolClick('triangle')">
            <svg viewBox="0 0 24 24" width="20" height="20"><path d="M12 5L4 19h16L12 5z" fill="none" stroke="currentColor" stroke-width="2"/><circle cx="12" cy="5" r="1.5"/><circle cx="4" cy="19" r="1.5"/><circle cx="20" cy="19" r="1.5"/></svg>
          </div>
          <div class="draw-tool-item" :class="{ active: activeDrawTool === 'parallelogram' }" title="平行四边形" @click="onDrawToolClick('parallelogram')">
            <svg viewBox="0 0 24 24" width="20" height="20"><path d="M6 6h12l-4 12H2L6 6z" fill="none" stroke="currentColor" stroke-width="2"/><circle cx="6" cy="6" r="1.5"/><circle cx="18" cy="6" r="1.5"/><circle cx="14" cy="18" r="1.5"/></svg>
          </div>
          <div class="draw-tool-item" title="清除全部" @click="onDrawToolClick('remove')">
            <svg viewBox="0 0 24 24" width="20" height="20"><path fill="currentColor" d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z"/></svg>
          </div>
          </div>
        </div>
      </div>
      <ul class="flex  px-32 py-20 box-border justify-between text-grey font-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-20"
            :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-20"
              :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-20"
              :class="{ 'text-black': typeValue === item }" @click="choiceType(item)">{{ item }}</li>
          </template>
        </template>
      </ul>
    </div>
  </div>
</template>
<script>
import { init, dispose } from 'klinecharts'
import { _getKline } from "@/service/trade.api";
import config from './config'
import { clearAllTimers } from '@/utils/utis.js'
import { Loading } from 'vant';
import { customShapeTemplates } from './drawTools'
 
/** K 线 X 轴/十字光标等展示用 IANA 时区(美国东部,自动夏令时) */
const KLINE_DISPLAY_TIMEZONE = 'America/New_York'
let chart = null
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,
      activeDrawTool: '', // 当前选中的绘图工具
      showDrawToolbar: true // 绘图工具栏是否显示,由铅笔图标切换
    }
  },
  computed: {
    timeList() {
      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() {
    this.clearTimer()
    dispose('kline')
  },
  watch: {
    isChangeLine(val) {
      this.clearTimer()
      this.fetchData()
    },
    updateKey() { // 更新charts:只更新 this.updateData 的 close,其余沿用最后一根
      const dataList = chart.getDataList()
      if (dataList.length > 0) {
        const lastData = dataList[dataList.length - 1]
        const nowData = this.updateData
        const newData = {
          ...lastData,
          close: nowData.close != null && nowData.close !== '' ? (nowData.close / 1) : lastData.close
        }
        this.$nextTick(() => {
          this.setChartType()
          chart.updateData(newData)
        })
      }
    }
  },
  methods: {
    initData() {
      this.timeValue = this.timeList.find(t => t.id === '15min') || this.timeList[0]
      chart = init('kline', config);
      chart.setTimezone(KLINE_DISPLAY_TIMEZONE)
      chart.setOffsetRightSpace(25)
      chart.setDataSpace(10)
      chart.setPriceVolumePrecision(4, 2)
      chart.createTechnicalIndicator('MA', false, { id: 'candle_pane' });
      this.paneId = chart.createTechnicalIndicator('VOL');
      // 注册自定义绘图图形:矩形、三角形、平行四边形
      chart.addShapeTemplate(customShapeTemplates)
      this.fetchData()
    },
    fetchData(time) { // 获取数据
      // this.chartLoading = true
      _getKline(this.symbol, this.timeValue.time || '1min').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) // 关闭计时器
      })
      // 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)
        })
      }, 10000);
    },
    setChartType() {
      let type = 'area'
      if (this.timeValue.id === '1min') {
        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
      clearAllTimers()
    },
    onDrawToolClick(name) {
      if (!chart) return
      if (name === 'remove') {
        chart.removeShape()
        this.activeDrawTool = ''
        return
      }
      this.activeDrawTool = name
      chart.createShape(name, 'candle_pane')
    },
  },
  deactivated() {
    this.clearTimer()
  }
}
</script>
<style lang="scss" scoped>
@import "@/assets/init.scss";
 
.chart-with-tools {
  position: relative;
}
 
.draw-tools-wrap {
  flex-shrink: 0;
  display: flex;
  border-left: 1px solid rgba(68, 75, 88, 0.2);
}
 
.draw-tools-wrap .pencil-trigger {
  width: 44px;
  min-width: 44px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 0;
  cursor: pointer;
  transition: background 0.2s;
  border-radius: 0;
}
.pencil-trigger.draw-toolbar-light {
  background: rgba(255, 255, 255, 0.98);
  color: #333;
}
.pencil-trigger.draw-toolbar-light:hover,
.pencil-trigger.draw-toolbar-light.active {
  background: rgba(0, 0, 0, 0.06);
  color: #2196f3;
}
.pencil-trigger.draw-toolbar-night {
  background: rgba(17, 26, 46, 0.98);
  color: #d9d9d9;
}
.pencil-trigger.draw-toolbar-night:hover,
.pencil-trigger.draw-toolbar-night.active {
  background: rgba(255, 255, 255, 0.08);
  color: #42a5f5;
}
 
.draw-toolbar {
  width: 44px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 0;
  gap: 4px;
}
 
.draw-toolbar-light {
  background: rgba(255, 255, 255, 0.98);
  color: #333;
}
 
.draw-toolbar-night {
  background: rgba(17, 26, 46, 0.98);
  color: #d9d9d9;
  border-left-color: rgba(68, 75, 88, 0.4);
}
 
.draw-tool-item {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.2s;
}
 
.draw-toolbar-light .draw-tool-item:hover {
  background: rgba(0, 0, 0, 0.06);
}
 
.draw-toolbar-light .draw-tool-item.active {
  background: rgba(33, 150, 243, 0.15);
  color: #2196f3;
}
 
.draw-toolbar-night .draw-tool-item:hover {
  background: rgba(255, 255, 255, 0.08);
}
 
.draw-toolbar-night .draw-tool-item.active {
  background: rgba(33, 150, 243, 0.25);
  color: #42a5f5;
}
 
.draw-tool-item svg {
  display: block;
}
</style>