from graphics import GraphWin, Line, Point, Text

# Load data from CSV manually
data = []
with open('sample_40Points.csv', 'r') as file:
    next(file)  # Skip the header row
    for line in file:
        index, value = map(float, line.strip().split(','))
        data.append((index, value))

# Create a window
win = GraphWin("Line Graph", 800, 600)

# Get the dimensions of the window
width, height = win.getWidth(), win.getHeight()

# Calculate y_min and y_max
y_min = min(value for _, value in data)
y_max = max(value for _, value in data)
y_range = y_max - y_min

# Calculate scale factors based on the window size and data range
x_scale = (width - 100) / max(index for index, _ in data)
y_scale = (height - 100) / y_range

# Calculate the middle of the window for the x-axis, adjusted for y_min
y_mid = height // 2 + (y_min * y_scale) / 2

# Draw x-axis in the middle
x_axis = Line(Point(50, y_mid), Point(width - 50, y_mid))
x_axis.draw(win)

# Draw y-axis at the left
y_axis = Line(Point(50, height - 50), Point(50, 50))
y_axis.draw(win)

# Label the axes
x_label = Text(Point(width // 2, height - 20), "Index")
y_label = Text(Point(20, height // 2), "Value")
x_label.draw(win)
y_label.draw(win)

# Offset for better visibility
x_offset = 50

# Draw data points
for i in range(1, len(data)):
    x1 = x_offset + data[i - 1][0] * x_scale
    y1 = y_mid - (data[i - 1][1] * y_scale)
    x2 = x_offset + data[i][0] * x_scale
    y2 = y_mid - (data[i][1] * y_scale)
    line = Line(Point(x1, y1), Point(x2, y2))
    line.draw(win)

# Wait for user to close the window
win.getMouse()
win.close()
