-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.py
More file actions
119 lines (96 loc) · 3.24 KB
/
Graph.py
File metadata and controls
119 lines (96 loc) · 3.24 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
import pygame as pg
import sys
import time
import os
from pygame.locals import *
# Commandline flags
TXT = ("-txt" in sys.argv)
PNG = ("-png" in sys.argv)
# Consts and init
dataLen = 12
margin = 50
(width, height) = (1000, 700)
sep = (width-2*margin)/(dataLen-1)
maxdata, data, mindata = [-50000]*dataLen, [0]*dataLen, [+50000]*dataLen
top = 200 # Room for axis graphs
skip = 5 # Skip frames
skipcnt = 1 #
lines = []
cnt = top
#offsets=[113, -377, 11, 59, 185, 1, -42, 215, -11, 239, -236, 256]
offsets=[0]*dataLen
multi=[3,1,2,1]
# Pygame init
pg.init()
window = pg.display.set_mode((width,height), RESIZABLE)
pg.display.set_caption("Graph")
canvas = pg.PixelArray(window)
white, black = Color(255,255,255), Color(0,0,0)
colors = [Color(175,0,0), Color(0,175,0), Color(0,0,175), Color(128,128,0)]
# Funcs
def screenshot():
os.system("scrot --focused graphs/"+str(int(time.time()))+".png")
#pg.image.save(pg.display.get_surface(), "graphs/"+str(int(time.time()))+".png")
def exit():
if PNG: screenshot()
sys.exit()
while True:
# Handle events
for event in pg.event.get():
if event.type == KEYDOWN and event.key == K_ESCAPE: exit()
if event.type == QUIT: exit()
if event.type == VIDEORESIZE:
width, height = event.size
window = pg.display.set_mode((width,height), RESIZABLE)
sep = (width-2*margin)/(dataLen-1)
pg.draw.rect(window, black, (0,0,width,height))
# Read data
read = sys.stdin.readline()[:-1]
if(read==""): exit()
if TXT: print read
# Skip frames
skipcnt += 1
if not skipcnt%skip == 0: continue
# Parse data
exdata = list(data)
data = [int(s) for s in read.split(" ")[1:]]
# Clear old lines
clear = 10
pg.draw.line(window, black, (0,cnt-1+clear/2),(width,cnt-1+clear/2), clear)
for _,v1,v2 in lines: pg.draw.line(window, black, v1, v2, 1)
# Normalize data somewhat
for i in range(len(data)):
maxdata[i], mindata[i] = max(data[i], maxdata[i]), min(data[i], mindata[i])
# Offset
if i<6: data[i] += offsets[i]
# Multiply
data[i] *= multi[i/3]
# Draw data lines
for i in range(len(data)):
xorigin = margin+i*sep
pg.draw.aaline(window, white,
(xorigin + (width/1000.0)*(exdata[i]/50.0), cnt-1),
(xorigin + (width/1000.0)*(data[i]/50.0), cnt))
pg.draw.line(window, colors[i/3], (xorigin,top-5),(xorigin,height), 1)
# Draw axis lines
lines = []
for i in range(4):
xx, yy = margin+sep+sep*i*3, 100
div = 15
lines += [
(colors[0], (xx,yy), (xx+data[0+i*3]/div,yy+data[1+i*3]/div)),
(colors[1], (xx,yy), (xx+data[1+i*3]/div,yy+data[2+i*3]/div)),
(colors[2], (xx,yy), (xx+data[2+i*3]/div,yy+data[0+i*3]/div)),
]
for c,v1,v2 in lines: pg.draw.line(window, c, v1, v2, 1)
pg.display.flip()
cnt += 1
if cnt>=height-10:
# Print min and max
#sys.stderr.write( str(maxdata)+"\n" )
#sys.stderr.write( str(mindata)+"\n" )
cnt = top-5
pg.draw.line(window, black, (0,cnt-1+clear/2),(width,cnt-1+clear/2), clear)
cnt = top
# Save screenshot
if PNG: screenshot()