-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.ts
161 lines (143 loc) · 3.32 KB
/
plot.ts
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
interface DrawLineParameters {
context: CanvasRenderingContext2D
width?: number
height?: number
pixelRatio?: number
offsetX?: number
lineWidth?: number
composite?: string
dashes?: number[]
zoom?: number
color?: string
data?: ArrayLike<number>
fn?: (x: number) => number
}
export const drawLine = ({
context: ctx,
width: w = ctx.canvas.width,
height = ctx.canvas.height,
pixelRatio: p = window.devicePixelRatio,
offsetX: ox = 50,
lineWidth = 1,
zoom = 1,
color = '#fff',
data = [],
fn = i => data[i | 0] ?? data[data.length - 1],
}: DrawLineParameters) => {
ox /= 100
w *= p
height *= p
ctx.save()
ctx.lineWidth = lineWidth * p
ctx.lineJoin = 'round'
ctx.strokeStyle = color
const k = lineWidth * p + p
const hk = k * 0.5
const hw = w * 0.5 + hk
const h = height - k
// prettier-ignore
const step = Math.max(
0.00001,
(
zoom
* 2 // we need to move two periods
) / Math.max(1, data.length - 1)
)
if (!isFinite(step)) return
const sx = 2 / (w * p)
const cf = data.length / (w * p * zoom)
// panning
const ds = cf * w * p
let i = ((data.length - ds) / cf) * ox
let cx = 0
let cy = 0
let x = -1
const calc = (y: number) => {
cx = (x + 1) * hw - hk
cy = (1 - (y + 1) * 0.5) * h + hk
}
calc(fn(0))
ctx.beginPath()
ctx.moveTo(cx, cy)
for (x = -1; x <= 1; x += sx) {
calc(fn(i++ * cf))
ctx.lineTo(cx, cy)
}
calc(fn(i++ * cf))
ctx.lineTo(cx, cy)
ctx.lineTo(cx, cy)
ctx.stroke()
ctx.restore()
}
// interface DrawXParameters {
// context: CanvasRenderingContext2D
// width?: number
// height?: number
// pixelRatio?: number
// lineWidth?: number
// dashes?: number[]
// color?: string
// }
// export const drawX = ({
// context: ctx,
// width: w = ctx.canvas.width,
// height: h = ctx.canvas.height,
// pixelRatio = window.devicePixelRatio,
// lineWidth = 1,
// dashes = [2 * pixelRatio, 2 * pixelRatio],
// color = '#fff',
// }: DrawXParameters) => {
// ctx.save()
// ctx.beginPath()
// ctx.setLineDash(dashes)
// ctx.lineWidth = lineWidth * pixelRatio
// ctx.strokeStyle = color
// ctx.moveTo(0, h / 2)
// ctx.lineTo(w, h / 2)
// ctx.stroke()
// ctx.restore()
// }
// interface DrawYParameters {
// context: CanvasRenderingContext2D
// width?: number
// height?: number
// pixelRatio?: number
// lineWidth?: number
// dashes?: number[]
// segments?: number
// zoom?: number
// colors?: string[]
// }
// export const drawY = ({
// context: ctx,
// width: w = ctx.canvas.width,
// height: h = ctx.canvas.height,
// pixelRatio = window.devicePixelRatio,
// dashes = [2 * pixelRatio, 2 * pixelRatio],
// segments = 4,
// lineWidth = 1,
// zoom = 1,
// colors = ['#363636', '#282828', '#313131', '#282828'],
// }: DrawYParameters) => {
// ctx.save()
// ctx.setLineDash(dashes)
// ctx.lineWidth = lineWidth * pixelRatio
// let i = 0
// let cx = 0
// const step = (zoom * 2) / segments
// let x = -1
// const calc = () => {
// cx = (x + 1) * 0.5 * w
// }
// for (x = -1 * zoom, i = 0; x <= 1; x += step) {
// calc()
// if (cx < 0) continue
// if (cx > w) break
// ctx.beginPath()
// ctx.strokeStyle = colors[i++ % 4]
// ctx.moveTo(cx, 0)
// ctx.lineTo(cx, h)
// ctx.stroke()
// }
// ctx.restore()
// }