made a graph feature

This commit is contained in:
wildercayden 2025-06-26 16:30:57 -04:00
parent 7106e68c1a
commit 5a0a5585f7
2 changed files with 64 additions and 29 deletions

View File

@ -1,24 +0,0 @@
import argparse
parser = argparse.ArgumentParser(description="Run TablePi client")
parser.add_argument(
'--table',
type=str,
default='DefaultTable',
help='Name of the NetworkTable (default: %(default)s)'
)
parser.add_argument(
'--ip',
type=str,
default='10.0.0.2',
help='IP address of the server (default: %(default)s)'
)
args = parser.parse_args()
table_name = args.table
server_ip = args.ip
print(f"Connecting to server {server_ip} using table '{table_name}'")

View File

@ -2,6 +2,9 @@ from networktables import NetworkTables
import configparser import configparser
import tkinter as tk import tkinter as tk
from tkinter import ttk from tkinter import ttk
import time
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
NetworkTables.initialize() NetworkTables.initialize()
NetworkTables.startServer() NetworkTables.startServer()
@ -17,8 +20,6 @@ root.geometry("300x150")
status_label = ttk.Label(root, text="Waiting for data...") status_label = ttk.Label(root, text="Waiting for data...")
status_label.pack(pady=20) status_label.pack(pady=20)
ttk.Button(root, text="Close", command=root.destroy).pack() ttk.Button(root, text="Close", command=root.destroy).pack()
def update_data(): def update_data():
@ -27,9 +28,67 @@ def update_data():
mem = table.getNumber("Memory", -1) mem = table.getNumber("Memory", -1)
status_label.config(text=f"CPU: {cpu}%, Temp: {temp}C Mem: {mem} MB") status_label.config(text=f"CPU: {cpu}%, Temp: {temp}C Mem: {mem} MB")
root.after(1000, update_data) root.after(1000, update_data)
print("Server started, waiting for data...") update_data()
update_data()
# Create second window (graph window)
graph_window = tk.Toplevel(root)
graph_window.title("Live Graphs")
graph_window.geometry("800x900")
# Store time and metric data
x_data = []
cpu_data = []
temp_data = []
mem_data = []
# Create 3 subplots (3 rows, 1 column)
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(8, 9), sharex=True)
fig.tight_layout(pad=3.0)
canvas = FigureCanvasTkAgg(fig, master=graph_window)
canvas.get_tk_widget().pack(fill=tk.BOTH, expand=True)
def add_data():
timestamp = time.strftime('%H:%M:%S')
x_data.append(timestamp)
cpu = table.getNumber("CPU Usage", -1)
temp = table.getNumber("Temperature", -1)
mem = table.getNumber("Memory", -1)
cpu_data.append(cpu)
temp_data.append(temp)
mem_data.append(mem)
# Clear each subplot
ax1.clear()
ax2.clear()
ax3.clear()
# Plot CPU
ax1.plot(x_data, cpu_data, marker='o', linestyle='-')
ax1.set_title("CPU Usage (%)")
ax1.tick_params(axis='x', rotation=45)
# Plot Temperature
ax2.plot(x_data, temp_data, marker='o', linestyle='-', color='orange')
ax2.set_title("Temperature (°C)")
ax2.tick_params(axis='x', rotation=45)
# Plot Memory
ax3.plot(x_data, mem_data, marker='o', linestyle='-', color='green')
ax3.set_title("Memory Usage (MB)")
ax3.set_xlabel("Time")
ax3.tick_params(axis='x', rotation=45)
# Redraw canvas
canvas.draw()
# Schedule next update
graph_window.after(2000, add_data)
add_data()
root.mainloop() root.mainloop()