forked from cbrenton/564NetworkVisualization
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisgraph.py
More file actions
177 lines (155 loc) · 5.67 KB
/
visgraph.py
File metadata and controls
177 lines (155 loc) · 5.67 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env python
import random
# The Python OpenGL package can be found at
# http://PyOpenGL.sourceforge.net/
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
class Graph():
def __init__(self, size=20, yMin=0.0, yMax=1.0, yStep=0.2, data=None):
self.size = size
self.yMin = yMin
self.yMax = yMax
self.yRange = self.yMax - self.yMin
self.yStep = yStep
#self.fg = [0.2, 0.4, 0.4]
self.randomColor()
self.bg = [1.0, 1.0, 1.0]
if data:
self.data = data
else:
self.data = []
for i in range(self.size):
self.data.append(self.yMin)
def randomColor(self):
r = g = b = 3
while r + g + b > 2.5:
r = random.random()
g = random.random()
b = random.random()
self.fg = [r, g, b]
def updateData(self):
# How much variation can be introduced per step. Must be a float between
# 0.0 and 1.0, where 0.0 is "no change", and 1.0 is "up to 100% of the y
# range".
scaleFactor = 0.25
for i in range(self.size):
randInRange = random.random() * self.yRange + self.yMin
self.data[i] += randInRange * scaleFactor - (self.yRange *
scaleFactor / 2)
self.data[i] = min(self.yMax, max(self.yMin, self.data[i]))
def draw(self, left, right, top, bottom, filled=True):
if filled:
# Draw the graph borders.
glColor3f(0.0, 0.0, 0.0)
glBegin(GL_LINES)
glVertex2f(left, top)
glVertex2f(right, top)
glVertex2f(left, bottom)
glVertex2f(right, bottom)
glEnd()
glColor3f(self.fg[0], self.fg[1], self.fg[2])
if filled:
glBegin(GL_TRIANGLE_STRIP)
else:
glLineWidth(2)
glBegin(GL_LINE_STRIP)
for count, point in enumerate(self.data):
curX = left + (float)(count) * (right - left) / (float)(len(self.data) - 1)
curY = bottom + point * (top - bottom)
glVertex2f(curX, curY)
if filled:
glVertex2f(curX, bottom)
glEnd()
if not filled:
glLineWidth(1)
if filled:
# Draw the graph background.
glColor3f(self.bg[0], self.bg[1], self.bg[2])
glBegin(GL_QUADS)
glVertex2f(left, top)
glVertex2f(left, bottom)
glVertex2f(right, bottom)
glVertex2f(right, top)
glEnd()
glColor3f(1.0, 1.0, 1.0)
class TimeGraph(Graph):
def __init__(self, size=20, yMin=0.0, yMax=1.0, yStep=0.2, data=None):
Graph.__init__(self, size, yMin, yMax, yStep, data)
def updateData(self):
scaleFactor = 0.25
self.data.pop(0)
randInRange = random.random() * self.yRange + self.yMin
self.data.append(self.data[-1] + randInRange * scaleFactor -
(self.yRange * scaleFactor / 2))
self.data[-1] = min(self.yMax, max(self.yMin, self.data[-1]))
class StepGraph(Graph):
def __init__(self, size=20, yMin=0.0, yMax=1.0, yStep=0.2, data=None):
Graph.__init__(self, size, yMin, yMax, yStep, data)
self.numSteps=30
self.updateCount = 0
def updateData(self):
self.updateCount = (self.updateCount + 1) % 3
self.data.pop(0)
if self.updateCount == 0:
dStep = float(random.randint(0, self.numSteps / 2)) - \
float(self.numSteps) / 4
self.data.append(self.data[-1] + 1.0 / dStep)
self.data[-1] = min(1.0, max(0.0, self.data[-1]))
else:
self.data.append(self.data[-1])
class MultiGraph():
def __init__(self, sets=None):
self.sets = sets
bgNum = random.random()
self.fg = [random.random(), random.random(), random.random()]
self.bg = [1.0, 1.0, 1.0]
if not self.sets:
self.sets = []
def addGraph(self, graph):
self.sets.append(graph)
self.makeRandomColors()
def makeRandomColors(self):
num = len(self.sets)
if num > 10:
for i in range(num):
self.sets[i].fg = [random.random(), random.random(), random.random()]
return
colorList = []
fixedIndex = random.randint(0, 2)
seed = [random.random(), random.random(), random.random()]
step = 1.0 / float(num)
for i in range(num):
colorList.append([0, 0, 0])
for j in range(3):
if j == fixedIndex:
colorList[i][j] = 0.3
else:
colorList[i][j] = (seed[j] + step * i) % 1.0
for i in range(num):
curRGB = colorList[i]
self.sets[i].fg = curRGB
def updateData(self):
for graph in self.sets:
graph.updateData()
def draw(self, left, right, top, bottom):
# Draw the graph borders.
glColor3f(0.0, 0.0, 0.0)
glBegin(GL_LINES)
glVertex2f(left, top)
glVertex2f(right, top)
glVertex2f(left, bottom)
glVertex2f(right, bottom)
glEnd()
# Draw the graph data (first because OpenGL draws in reverse).
for graph in self.sets:
graph.draw(left, right, top, bottom, False)
# Draw the graph background.
glColor3f(self.bg[0], self.bg[1], self.bg[2])
glBegin(GL_QUADS)
glVertex2f(left, top)
glVertex2f(left, bottom)
glVertex2f(right, bottom)
glVertex2f(right, top)
glEnd()
glColor3f(1.0, 1.0, 1.0)