Blame view

telemetry/code/monitor/pyglet_test.py 2.25 KB
6afdc6b9   Christopher Stone   Attempt at drawin...
1
2
import pyglet
import math
caf04a11   Christopher Stone   Demo code now dra...
3
4
import random
import time
6afdc6b9   Christopher Stone   Attempt at drawin...
5
6
7
 
window = pyglet.window.Window()
window.set_caption("Test graph")
caf04a11   Christopher Stone   Demo code now dra...
8
9

starttime = time.time()
6afdc6b9   Christopher Stone   Attempt at drawin...
10
11
12
13
14
15
16
17
18
19
20
21
 
title = pyglet.text.Label('Test Graph',
                          font_name='Arkhip',
                          font_size=12,
                          x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='top')

                 
def round_sig(x, sig=2):
    return round(x, sig-int(math.floor(math.log10(abs(x))))-1)
                 
def drawgrid(xlines, ylines, xlimits, ylimits):
caf04a11   Christopher Stone   Demo code now dra...
22
    pyglet.gl.glColor4f(0.5, 0.5, 0.5, 1.0) 
6afdc6b9   Christopher Stone   Attempt at drawin...
23
24
25
26
27
28
29
30
31
32
33
34
35
    for xpos in range(0, window.width, window.width/xlines):
        pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (xpos, 0, xpos, window.height)))
        tagtext = str(round((xlimits[1]-xlimits[0])*(float(xpos)/window.width), 1))
        tag = pyglet.text.Label(tagtext, font_name='Arkhip', font_size=10, x=xpos-2, y=0, anchor_x='right', anchor_y='bottom')
        tag.draw()
    for ypos in range(0, window.width, window.width/ylines):
        pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (0, ypos, window.width, ypos)))
        tagtext = str(round((ylimits[1]-ylimits[0])*(float(ypos)/window.height), 1))
        tag = pyglet.text.Label(tagtext, font_name='Arkhip', font_size=10, x=0, y=ypos-2, anchor_x='left', anchor_y='top')
        tag.draw()


def plotline(xdata, ydata):
caf04a11   Christopher Stone   Demo code now dra...
36
37
38
39
40
41
42
43
44
45
46
    pyglet.gl.glColor4f(1, 0, 0, 1.0)
    points = []
    for n in range(max(len(xdata), len(ydata))):
        xpos = ((xdata[n]-min(xdata))*window.width)/(max(xdata)-min(xdata))
        xpos = int(xpos)
        ypos = ((ydata[n]-min(ydata))*window.height)/(max(ydata)-min(ydata))
        ypos = int(ypos)
        points.append([xpos, ypos])
    for n in range(len(points)-1):            
        pyglet.graphics.draw(2, pyglet.gl.GL_LINES, ('v2i', (points[n][0], points[n][1], points[n+1][0], points[n+1][1])))

6afdc6b9   Christopher Stone   Attempt at drawin...
47

caf04a11   Christopher Stone   Demo code now dra...
48
49
xdata = [0]
ydata = [0]
6afdc6b9   Christopher Stone   Attempt at drawin...
50

6afdc6b9   Christopher Stone   Attempt at drawin...
51
52
53
54

@window.event
def on_draw():
    window.clear()
caf04a11   Christopher Stone   Demo code now dra...
55
56
57
58
59
60
    ydata.append(random.uniform(0, 10))
    if len(ydata) > 100:
        del ydata[0]
    xdata.append(round(time.time() - starttime, 3))
    if len(xdata) > 100:
        del xdata[0]
6afdc6b9   Christopher Stone   Attempt at drawin...
61
    drawgrid(10, 12, [min(xdata), max(xdata)], [min(ydata), max(ydata)])
caf04a11   Christopher Stone   Demo code now dra...
62
63
    plotline(xdata, ydata)

6afdc6b9   Christopher Stone   Attempt at drawin...
64
65
    
pyglet.app.run()