st-comp 0.0.45 → 0.0.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,336 @@
1
+ <template>
2
+ <div
3
+ class="kline"
4
+ @mousemove="isSelect = true"
5
+ @mouseout="isSelect = false"
6
+ >
7
+ <div class="kline-mainTips">
8
+ <st-klineTips :drawData="drawData" :activeIndex="activeIndex"/>
9
+ </div>
10
+ <div class="kline-mainChart" ref="mainChartRef"></div>
11
+ <div class="kline-sub">
12
+ <st-klineSub ref="klineSubRef" v-model="subIndicator" :config="config" :drawData="drawData" :activeIndex="activeIndex" :subIndicatorList="subIndicatorList" />
13
+ </div>
14
+ <st-klineUtils ref="klineUtilsRef" />
15
+ </div>
16
+ </template>
17
+
18
+ <script setup>
19
+ import { ref, onMounted, onUnmounted, watch, computed } from "vue"
20
+ import * as echarts from 'echarts'
21
+ import { getKlineDataApi, formatConfig } from './utils'
22
+ import dayjs from "dayjs"
23
+ import { stMath } from 'st-func'
24
+ const { round } = stMath
25
+
26
+ const props = defineProps({
27
+ code: { type: String, required: true }, // 品种代码
28
+ freqId: { type: String, required: true }, // 周期
29
+ freqDict: { type: Object, required: true }, // 周期字典
30
+ mainIndicator: { type: String, required: true }, // 主图指标
31
+ mainIndicatorList: { type: Array, required: true }, // 主图指标列表
32
+ subIndicatorList: { type: Array, required: true }, // 副图指标列表
33
+ config: { type: Object, default: () => ({}) }, // 配置
34
+ })
35
+
36
+ let mainChart
37
+ let mainDataZoomTimer = null // 主图数据缩放定时器
38
+ let isLoadHistory = false // 是否正在加载历史数据
39
+ let isloadAllHistory = false // 是否加载完全部历史数据
40
+ let resizeRo // dom元素监听事件
41
+ let highlightTimer = null // 高亮处理事件的定时器
42
+
43
+ const mainChartRef = ref() // 主图
44
+ const klineSubRef = ref() // 副图
45
+ const klineUtilsRef = ref() // k线辅助函数实例
46
+ const isSelect = ref(false) // 是否选中,选中可通过键盘按键操作
47
+ const drawData = ref({}) // 绘图数据
48
+ const activeIndex = ref(0) // 显示的index
49
+ const subIndicator = ref('VOL') // 副图指标
50
+
51
+ const config = computed(() => formatConfig(props.config))
52
+
53
+ watch(
54
+ () => [props.code, props.freqId, props.mainIndicator, subIndicator],
55
+ async () => {
56
+ isLoadHistory = false
57
+ isloadAllHistory = false
58
+ initChart()
59
+ },
60
+ { deep: true }
61
+ )
62
+
63
+ onMounted(async () => {
64
+ // 初始化指标计算方法
65
+ await klineUtilsRef.value.initTalib()
66
+ mainChart = echarts.init(mainChartRef.value)
67
+ klineSubRef.value.connect(mainChart)
68
+ addEventListener()
69
+ initChart()
70
+ // 绑定resize事件
71
+ let isFirst = true
72
+ resizeRo = new ResizeObserver(() => {
73
+ if (isFirst) {
74
+ isFirst = null
75
+ return
76
+ }
77
+ mainChart.resize()
78
+ })
79
+ resizeRo.observe(mainChartRef.value)
80
+ window.addEventListener('keydown', keyDownEvent)
81
+ })
82
+
83
+ onUnmounted(() => {
84
+ window.removeEventListener('keydown', keyDownEvent)
85
+ mainChart.off('datazoom')
86
+ mainChart.off('highlight')
87
+ mainChart.off('globalout')
88
+ mainChart.dispose()
89
+ resizeRo.disconnect()
90
+ resizeRo = null
91
+ })
92
+
93
+ // 项目初始化
94
+ const initChart = async () => {
95
+ const { addCount, preLoadCount } = config.value
96
+ const res = await getKlineData({
97
+ queryType: '1',
98
+ endTime: dayjs().format("YYYY-MM-DD HH:mm:ss"),
99
+ cycle: props.freqId,
100
+ limit: addCount + preLoadCount,
101
+ })
102
+ drawData.value = klineUtilsRef.value.formatDataByCount({
103
+ originDrawData: {},
104
+ addData: res,
105
+ mainIndicator: props.mainIndicator,
106
+ mainIndicatorList: props.mainIndicatorList,
107
+ subIndicator: subIndicator.value,
108
+ subIndicatorList: props.subIndicatorList,
109
+ config: config.value,
110
+ })
111
+ draw('main', { startValue: drawData.value.length - 1 - config.value.defaultShowCount, endValue: drawData.value.length - 1 })
112
+ draw('sub', { startValue: drawData.value.length - 1 - config.value.defaultShowCount, endValue: drawData.value.length - 1})
113
+ activeIndex.value = mainChart.getOption().dataZoom[0].endValue
114
+ }
115
+
116
+ // 绑定事件
117
+ const addEventListener = () => {
118
+ mainChart.on('datazoom', (params) => {
119
+ clearTimeout(mainDataZoomTimer)
120
+ mainDataZoomTimer = setTimeout(() => {
121
+ const { loadCheckCount, addCount, preLoadCount } = config.value
122
+ const { startValue } = mainChart.getOption().dataZoom[0]
123
+ // 加载数据
124
+ if (startValue < loadCheckCount && isLoadHistory === false && isloadAllHistory === false) {
125
+ // 左侧数据小于1000条,加载左侧数据
126
+ isLoadHistory = true
127
+ getMoreData({
128
+ queryType: '1',
129
+ endTime: drawData.value.startTime,
130
+ cycle: props.freqId,
131
+ limit: addCount + preLoadCount,
132
+ })
133
+ }
134
+ clearTimeout(mainDataZoomTimer)
135
+ }, 100)
136
+ })
137
+ // 高亮事件
138
+ mainChart.on('highlight', (data) => {
139
+ let index = data.dataIndex || -1
140
+ if (data.batch ) {
141
+ index = typeof data?.batch[0]?.dataIndex === 'number' ? data?.batch[0]?.dataIndex : -1
142
+ }
143
+ clearTimeout(highlightTimer)
144
+ highlightTimer = setTimeout(() => {
145
+ activeIndex.value = index
146
+ clearTimeout(highlightTimer)
147
+ }, 20)
148
+ })
149
+ // 移出图表事件
150
+ mainChart.on('globalout', () => {
151
+ const index = mainChart.getOption().dataZoom[0].endValue
152
+ activeIndex.value = index
153
+ })
154
+ }
155
+
156
+ // 键盘事件
157
+ const keyDownEvent = e => {
158
+ // 只有选中或者按ctrl才激活按键
159
+ if (!(e.ctrlKey || isSelect.value)) return
160
+ const option = mainChart.getOption()
161
+ let { startValue, endValue } = option.dataZoom[0]
162
+ mainChart.dispatchAction({
163
+ type: 'dataZoom',
164
+ ...klineUtilsRef.value.handleKeyDown(e, { startValue, endValue, maxIndex: option.xAxis[0].data.length - 1})
165
+ })
166
+ }
167
+
168
+ // 获取更多数据
169
+ const getMoreData = async(params) => {
170
+ const { addCount, preLoadCount, maxLoadCount } = config.value
171
+ const res = await getKlineData(params)
172
+ drawData.value = klineUtilsRef.value.formatDataByCount({
173
+ originDrawData: drawData.value,
174
+ addData: res.slice(0, res.length - 1),
175
+ params,
176
+ mainIndicator: props.mainIndicator,
177
+ mainIndicatorList: props.mainIndicatorList,
178
+ subIndicator: subIndicator.value,
179
+ subIndicatorList: props.subIndicatorList,
180
+ config: config.value,
181
+ })
182
+ const { startValue, endValue } = mainChart.getOption().dataZoom[0]
183
+ let dataZoomAddCount = addCount
184
+ if(drawData.value.length > maxLoadCount){
185
+ // 超出设置的最长加载数据条数
186
+ isloadAllHistory = true
187
+ } else if (res.length < addCount + preLoadCount) {
188
+ // 表示历史数据已加载完
189
+ isloadAllHistory = true
190
+ dataZoomAddCount = res.length - 1
191
+ }
192
+ isLoadHistory = false
193
+ draw('main', { startValue: startValue + dataZoomAddCount, endValue: endValue + dataZoomAddCount })
194
+ draw('sub', { startValue: startValue + dataZoomAddCount, endValue: endValue + dataZoomAddCount })
195
+ }
196
+
197
+ // 获取k线数据
198
+ const getKlineData = async(params) => {
199
+ const apiParams = {
200
+ contractType: 0, // 合约类型 0-主连 1-加权
201
+ variety: props.code, // 品种
202
+ right: 1, // 0-不复权 1-前复权 2-后复权
203
+ ...params,
204
+ }
205
+ const res = await getKlineDataApi(apiParams)
206
+ return res
207
+ }
208
+
209
+ // 绘制函数
210
+ const draw = (type, params = {}) => {
211
+ const { startValue, endValue, maxValueSpan } = params
212
+ const callBackMap = new Map([
213
+ [ "main", () => {
214
+ const { xAxisData, candlestickData, mainIndicatorData } = drawData.value
215
+ const indicatorSeries = mainIndicatorData.map((item) => {
216
+ return {
217
+ name: item.key,
218
+ type: 'line',
219
+ silent: true,
220
+ symbol: 'none',
221
+ data: item.data,
222
+ lineStyle: {
223
+ width: item.width || 1,
224
+ },
225
+ itemStyle: {
226
+ color: item.color,
227
+ }
228
+ }
229
+ })
230
+ mainChart.setOption({
231
+ animation: false,
232
+ grid: {
233
+ left: `${config.value.gridLeft}px`,
234
+ top: '20px',
235
+ right: `${config.value.gridRight}px`,
236
+ bottom: '6px',
237
+ },
238
+ dataZoom: [
239
+ {
240
+ type: 'inside',
241
+ startValue,
242
+ endValue,
243
+ maxValueSpan: config.value.maxShowCount,
244
+ }
245
+ ],
246
+ tooltip: {
247
+ trigger: 'axis',
248
+ appendToBody: true,
249
+ confine: true,
250
+ axisPointer: {
251
+ type: 'cross',
252
+ label: {
253
+ rich: {},
254
+ formatter: data => {
255
+ const { axisDimension, value } = data
256
+ if(axisDimension === 'x') {
257
+ return ''
258
+ } else {
259
+ return String(round(value))
260
+ }
261
+ }
262
+ },
263
+ },
264
+ formatter: () => '',
265
+ },
266
+ xAxis: {
267
+ show: false,
268
+ type: 'category',
269
+ data: xAxisData,
270
+ splitLine: {
271
+ show: false,
272
+ },
273
+ },
274
+ yAxis: {
275
+ type: 'value',
276
+ axisLine: {
277
+ show: true,
278
+ },
279
+ splitLine: {
280
+ show: true,
281
+ lineStyle: {
282
+ type: "dotted",
283
+ color: "#333",
284
+ },
285
+ },
286
+ min: (value) => round(value.min),
287
+ max: (value) => round(value.max),
288
+ },
289
+ series: [
290
+ {
291
+ type: 'candlestick',
292
+ data: candlestickData,
293
+ itemStyle: {
294
+ color: "transparent",
295
+ color0: "#00FFFF",
296
+ borderColor: "#FF0000",
297
+ borderColor0: "#00FFFF",
298
+ borderWidth: 1,
299
+ },
300
+ },
301
+ ...indicatorSeries,
302
+ ]
303
+ }, true)
304
+ }],
305
+ [ "sub", () => {
306
+ klineSubRef.value.draw({ startValue, endValue, maxValueSpan })
307
+ }],
308
+ ]);
309
+ const callBack = callBackMap.get(type);
310
+ if (callBack instanceof Function) {
311
+ callBack();
312
+ }
313
+ }
314
+ </script>
315
+
316
+ <style lang="scss" scoped>
317
+ .kline {
318
+ width: 100%;
319
+ height: 100%;
320
+ background: #000;
321
+ &-mainTips {
322
+ height: 26px;
323
+ }
324
+ &-mainChart {
325
+ width: 100%;
326
+ height: calc(65% - 26px);
327
+ }
328
+ &-subTips {
329
+ height: 16px;
330
+ }
331
+ &-sub {
332
+ width: 100%;
333
+ height: calc(35%);
334
+ }
335
+ }
336
+ </style>
@@ -0,0 +1,33 @@
1
+ import axios from 'axios'
2
+
3
+ const defaultConfig = {
4
+ defaultShowCount: 500, // 默认展示天数
5
+ addCount: 2000, // 首屏加载条数,滚动加载条数
6
+ maxLoadCount: 50000, // 最多加载多少条数据
7
+ maxShowCount: 5000, // 一屏幕最多展示多少条数据
8
+ preLoadCount: 800, // 预加载数据条数(用于计算指标线)
9
+ loadCheckCount: 500, // 加载数据检查条数
10
+ gridLeft: 60, // echarts绘图左侧grid距离
11
+ gridRight: 20, // echarts绘图右侧grid距离
12
+ }
13
+
14
+ // 获取配置
15
+ export const formatConfig = (config) => {
16
+ return {
17
+ ...defaultConfig,
18
+ ...config,
19
+ }
20
+ }
21
+
22
+ // 默认获取k线参数
23
+ export const getKlineDataApi = async(params) => {
24
+ const res = await axios({
25
+ method: 'post',
26
+ headers: {
27
+ token: '041fd377d5d5efc7e08e2ed5b61b0c8d',
28
+ },
29
+ url: 'http://invest.hzyotoy.com/common/qt/getSingleCycleSingleVariety',
30
+ data: params,
31
+ })
32
+ return res.data.body
33
+ }