-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchart.py
81 lines (61 loc) · 2.11 KB
/
chart.py
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
import pygame
import matplotlib.pyplot as plt
import io
import csv
from collections import deque
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Real-time Line Chart")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Load CSV data
data = deque(maxlen=50) # To store the last 50 data points
x_values = []
y_values = {'PlayerVoiceTime': [], 'PlayerFacegen': [], 'Post-Chat Time': [], 'unaccounted': [], 'Elapsed Time': []}
def read_csv(file_name):
with open(file_name, 'r') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
x_values.append(float(row['End Time']))
for column in ['PlayerVoiceTime', 'PlayerFacegen', 'Post-Chat Time', 'unaccounted', 'Elapsed Time']:
y_values[column].append(float(row[column]))
# Load initial data from the CSV file
read_csv('log.csv')
# Initialize Matplotlib figure and axes
plt.ion()
fig, ax = plt.subplots(figsize=(6, 4))
lines = {column: ax.plot([], [], label=column, lw=2)[0] for column in y_values.keys()}
# Pygame clock
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update data from CSV file
read_csv('log.csv')
# Clear the screen
screen.fill(WHITE)
# Draw the Matplotlib line chart
for column, line in lines.items():
line.set_data(x_values, y_values[column])
ax.set_xlim(min(x_values), max(x_values))
ax.set_ylim(0, max(max(max(y_values.values())), 10)) # Adjust the y-axis limit as needed
ax.legend()
plt.pause(0.01) # Update the Matplotlib chart
# Save the Matplotlib chart as a PNG file
plt.savefig('chart.png', format='png', dpi=100)
# Load the PNG image using Pygame
chart_img = pygame.image.load('chart.png')
# Blit the chart image onto the Pygame screen
screen.blit(chart_img, (0, 0))
pygame.display.flip()
clock.tick(1) # Limit to 30 FPS
pygame.quit()