forked from SheepTester/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.html
More file actions
120 lines (118 loc) · 3.19 KB
/
graph.html
File metadata and controls
120 lines (118 loc) · 3.19 KB
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
<!doctype html>
<html>
<head>
<title>ugwa views</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
html,
body {
height: 100%;
}
body {
margin: 0;
display: flex;
background-color: black;
}
#canvas {
flex: auto;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script>
const params = new URL(window.location).searchParams
const proms = []
const end = new Date()
const date = params.get('start') ? new Date(params.get('start')) : new Date()
if (!params.get('start')) {
date.setHours(date.getHours() - 4)
}
let max = 0
function getViews (date, i) {
const time = date.getHours() + ':' + date.getMinutes().toString().padStart(2, '0')
const request = `https://sheep.thingkingland.app/counter/UGWA_${
date.toISOString().slice(0, 15)
}/`
return fetch(request).then(r => r.text()).then(num => {
num = +num
if (num > max) max = num
return { num, time, i, request }
})
}
let i = 0
for (; date <= end; date.setMinutes(date.getMinutes() + 10), i++) {
proms.push(getViews(date, i))
}
date.setMinutes(date.getMinutes() - 10)
i--
const canvas = document.getElementById('canvas')
const c = canvas.getContext('2d')
const width = window.innerWidth
const height = window.innerHeight
const dpr = window.devicePixelRatio
canvas.width = width * dpr
canvas.height = height * dpr
c.scale(dpr, dpr)
const padding = 40
function wait (time) {
return new Promise(resolve => setTimeout(resolve, time))
}
Promise.all(proms).then(graph => {
function render () {
c.clearRect(0, 0, width, height)
const col = (width - 2 * padding) / (graph.length - 1)
c.strokeStyle = '#e91e63'
c.lineWidth = 2
c.textAlign = 'center'
c.font = '10px monospace'
c.textBaseline = 'bottom'
c.beginPath()
for (const { num, time, i } of graph) {
const x = i * col + padding
const y = height - padding - num / max * (height - 2 * padding)
if (i === 0) {
c.moveTo(x, y)
} else {
c.lineTo(x, y)
}
c.fillStyle = 'white'
c.fillText(num, x, y)
c.fillStyle = 'rgba(255, 255, 255, 0.5)'
c.fillText(time, x, height - padding + (i % 2 === 0 ? 20 : 30))
}
c.stroke()
c.fillStyle = 'rgba(233, 30, 99, 0.2)'
c.beginPath()
c.moveTo(width - padding, height - padding)
c.lineTo(padding, height - padding)
for (const { num, i } of graph) {
const x = i * col + padding
const y = height - padding - num / max * (height - 2 * padding)
c.lineTo(x, y)
}
c.closePath()
c.fill()
console.log('rendered')
}
render()
;(async () => {
while (true) {
await wait(1000)
if (date + 10 <= end) {
date.setMinutes(date.getMinutes() + 10)
i++
}
await getViews(date, i)
.then(data => {
graph[i] = data
})
render()
}
})()
})
</script>
</body>
</html>