Overlay
js
import { init, registerOverlay } from 'klinecharts'
import './index.css'
function genData (timestamp = new Date().getTime(), length = 800) {
let basePrice = 5000
timestamp = Math.floor(timestamp / 1000 / 60) * 60 * 1000 - length * 60 * 1000
const dataList = []
for (let i = 0; i < length; i++) {
const prices = []
for (let j = 0; j < 4; j++) {
prices.push(basePrice + Math.random() * 60 - 30)
}
prices.sort()
const open = +(prices[Math.round(Math.random() * 3)].toFixed(2))
const high = +(prices[3].toFixed(2))
const low = +(prices[0].toFixed(2))
const close = +(prices[Math.round(Math.random() * 3)].toFixed(2))
const volume = Math.round(Math.random() * 100) + 10
const turnover = (open + high + low + close) / 4 * volume
dataList.push({ timestamp, open, high,low, close, volume, turnover })
basePrice = close
timestamp += 60 * 1000
}
return dataList
}
registerOverlay({
name: 'circle',
needDefaultPointFigure: true,
needDefaultXAxisFigure: true,
needDefaultYAxisFigure: true,
totalStep: 3,
createPointFigures: ({ coordinates }) => {
if (coordinates.length === 2) {
const xDis = Math.abs(coordinates[0].x - coordinates[1].x)
const yDis = Math.abs(coordinates[0].y - coordinates[1].y)
const radius = Math.sqrt(xDis * xDis + yDis * yDis)
return {
key: 'circle',
type: 'circle',
attrs: {
...coordinates[0],
r: radius
},
styles: {
style: 'stroke_fill'
}
}
}
return []
}
})
const chart = init('k-line-chart')
chart.applyNewData(genData())
function createOverlay (name) {
chart.createOverlay(name)
}
// 以下仅仅是为了协助代码演示,在实际项目中根据情况进行调整。
// The following is only for the purpose of assisting in code demonstration, and adjustments will be made according to the actual situation in the project.
const container = document.getElementById('container')
const buttonContainer = document.createElement('div')
buttonContainer.className = 'button-container'
const items = [
{ key: 'priceLine', text: '价格线(内置)-Price line(built-in)' },
{ key: 'circle', text: '圆(自定义)-Circle(custom)' }
]
items.forEach(({ key, text }) => {
const button = document.createElement('button')
button.innerText = text
button.addEventListener('click', () => { createOverlay(key) })
buttonContainer.appendChild(button)
})
container.appendChild(buttonContainer)
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
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
html
<div id="container">
<div id="k-line-chart" style="height:430px">
</div>
1
2
3
2
3
css
.button-container {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
margin-top: 10px;
padding: 10px 22px;
}
.button-container button {
padding: 2px 6px;
background-color: #1677FF;
border-radius: 4px;
font-size: 12px;
color: #fff;
outline: none;
border: none;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18