-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.js
91 lines (76 loc) · 2.79 KB
/
Graph.js
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
export default class Graph {
constructor(ctx, width, height, realWidth, realHeight) {
ctx.save()
ctx.translate(realWidth / 2, realHeight / 2)
this.ctx = ctx
this.width = width
this.height = height
this.realWidth = realWidth
this.realHeight = realHeight
}
drawAxes() {
this.ctx.strokeStyle = 'rgba(255, 255, 255, 0.5)'
this.ctx.beginPath()
this.ctx.moveTo(0, -this.realHeight / 2)
this.ctx.lineTo(0, this.realHeight / 2)
this.ctx.moveTo(-this.realWidth / 2, 0)
this.ctx.lineTo(this.realWidth / 2, 0)
this.ctx.stroke()
this.ctx.fillStyle = 'rgba(255, 255, 255, 0.5)'
this.ctx.lineWidth = 1
const linesColor = 'rgba(255, 255, 255, 0.05)'
const ySpacing = this.realHeight / this.height
const xSpacing = ySpacing
this.width = Math.ceil(this.realWidth / xSpacing)
for (let i = 0; i < this.width / 2; i++) {
this.ctx.beginPath()
const x = i * xSpacing
const y = 0
if (xSpacing > 13) this.ctx.fillText(i, x - 3, y + 15)
this.ctx.beginPath()
this.ctx.strokeStyle = linesColor
this.ctx.moveTo(x, -this.realHeight / 2)
this.ctx.lineTo(x, this.realHeight / 2)
this.ctx.stroke()
}
for (let i = 0; i < this.width / 2; i++) {
this.ctx.beginPath()
const x = -(i * xSpacing)
const y = 0
if (xSpacing > 13) this.ctx.fillText(i, x - 3, y + 15)
this.ctx.beginPath()
this.ctx.strokeStyle = linesColor
this.ctx.moveTo(x, -this.realHeight / 2)
this.ctx.lineTo(x, this.realHeight / 2)
this.ctx.stroke()
}
let yNumber = this.height / 2
for (let i = 0; i <= this.height; i++) {
this.ctx.beginPath()
const x = 0
const y = i * ySpacing - this.realHeight / 2
if (ySpacing > 13) this.ctx.fillText(yNumber, x + 5, y + 3)
this.ctx.strokeStyle = linesColor
this.ctx.moveTo(-this.realWidth / 2, y)
this.ctx.lineTo(this.realWidth / 2, y)
this.ctx.stroke()
yNumber--
}
}
drawLine(x1, y1, x2, y2) {
if (Math.abs(y1) > this.height / 2 && Math.abs(y2) > this.height / 2)
return
this.ctx.lineWidth = 2
const spacingX = this.realWidth / this.width
const spacingY = this.realHeight / this.height
x1 = x1 * spacingX
y1 = -y1 * spacingY
x2 = x2 * spacingX
y2 = -y2 * spacingY
this.ctx.strokeStyle = 'rgba(200, 0, 0, 1)'
this.ctx.beginPath()
this.ctx.moveTo(x1, y1)
this.ctx.lineTo(x2, y2)
this.ctx.stroke()
}
}