Creating the graph view

Head over to views.py, where we'll create the LineChartView class:

class LineChartView(tk.Canvas):

    margin = 20

    def __init__(self, parent, chart_width, chart_height,
                 x_axis, y_axis, x_max, y_max):
        self.max_x = max_x
        self.max_y = max_y
        self.chart_width = chart_width
        self.chart_height = chart_height

LineChartView is a subclass of Canvas, so we'll be able to draw items directly on it. We'll accept a parent widget, height, and width for the chart portion, labels for the x and y axes as arguments, and the maximum values for x and y to display. We'll save the chart dimensions and maximum values for later use, and set a class property of 20 pixels for the margin width.

Let's start setting up this Canvas:

 view_width = chart_width ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.