import pygame class graph: def __init__ ( self, screen ): self.screen = screen self.width = screen.get_width() self.height = screen.get_height() self.graphOriginY = int ( float ( self.height ) * .8 ) self.graphOriginX = ( self.height - self.graphOriginY ) self.labelColor = ( 0, 200, 0 ) pygame.font.init() self.labelFont = pygame.font.SysFont ( "", 20 ) def drawAxes ( self ): pygame.draw.line ( self.screen, self.labelColor, ( self.graphOriginX, self.graphOriginY ), ( self.graphOriginX, 0 ) ) pygame.draw.line ( self.screen, self.labelColor, f.graphOriginX, self.graphOriginY ), f.width, self.graphOriginY ) ) def drawHorizontalHash ( self ): for i in range ( self.graphOriginX, self.width, 25 ): pygame.draw.line ( self.screen, self.labelColor, self.graphOriginY - 5 ), self.graphOriginY + 5 ) ) def drawVerticalHash ( self ): for i in range ( self.graphOriginY, 0, -25 ): pygame.draw.line ( self.screen, self.labelColor, f.graphOriginX - 5, i ), f.graphOriginX + 5, i ) ) def drawGrid ( self ): for x in range ( self.graphOriginX + 50, width, 50 ): for y in range ( self.graphOriginY, 0, -50 ): pygame.draw.line ( self.screen, self.labelColor, y ), ( x, y ) ) def drawScale ( self ): for i in range ( self.graphOriginY, 0, -50 ): value = float ( i ) / float ( self.graphOriginY ) * ( self.dataMax - self.dataMin ) + self.dataMin labelSurf = self.labelFont.render ( "{:5.2f}".format ( value ), True, self.labelColor ) self.screen.blit ( labelSurf, lf.graphOriginX - labelSurf.get_width() - 10, self.graphOriginY - i ) ) def graphLine ( self, data, labels, start, end ): myData = list() dataMin = None dataMax = None graphWidth = self.width - self.graphOriginX dataWidth = int ( graphWidth / 25 ) for dataPoint in self.walkValues ( , end, dataWidth ): value = float ( data [ dataPoint ] ) if value < dataMin or dataMin == dataMin = value if value > dataMax or dataMax == dataMax = value myData.append ( value ) scale = ( self.graphOriginY ) / ( dataMax - dataMin ) self.dataMin = dataMin self.dataMax = dataMax points = list() for index, pointVal in enumerate ( myData ): points.append ( ( index * 25 + self.graphOriginX, self.graphOriginY - int ( ( pointVal - dataMin ) * scale ) ) ) labelSurf = self.labelFont.render ( s [ index ], True, self.labelColor ) labelSurf = e.transform.rotate ( labelSurf, 45 ) self.screen.blit ( labelSurf, ( index * 25 + graphOriginX - labelSurf.get_width() + 5, self.graphOriginY + 5 ) ) pygame.draw.lines ( self.screen, ( 250, 0, 0 ), False, points ) def setupGraph ( self ): self.drawAxes() self.drawHorizontalHash() self.drawVerticalHash() self.drawGrid() def walkValues ( self, start, end, size ): listSize = end - start sampleSpacing = int ( listSize / size ) for i in range ( , end - sampleSpacing, sampleSpacing ): yield i yield end - 1 class dataFile: columns = list() def __init__ ( self ): pass def loadFile ( self, fileName, separator ): dataFile = file ( fileName, "r" ) firstTime = True for line in dataFile.readlines(): lineParts = line.split ( separator ) if firstTime == True: for index, cell in enumerate ( lineParts ): self.columns.append ( list() ) self.columns [ index ].append ( strip ( "\r\n" ) ) firstTime = False else: for index, cell in enumerate ( lineParts ): self.columns [ index ].append ( strip ( "\r\n" ) ) def getColumn ( self, column, columnType = None ): if columnType == None: return self.columns [ column ] elif columnType == "INT": output = list() for value in self.columns [ column ]: output.append ( int ( value ) ) return output elif columnType == "FLOAT": output = list() for value in self.columns [ column ]: output.append ( float ( value ) ) return output def printTable ( self ): columnIndex = 0 for i in range ( 25 ): text = "" for column in self.columns: text += column [ i ] + "\t" print text df = dataFile() df.loadFile ( "tempdata.csv", "," ) pygame.display.init() screen = pygame.display.set_mode ( ( 1024, 600 ) ) chart = graph ( screen ) chart.setupGraph() labels = list() dates = df.getColumn ( 1 ) times = df.getColumn ( 2 ) for i in range ( len ( dates ) ): labels.append ( dates [ i ] + " " + times [ i ] ) chart.graphLine ( df.getColumn ( 3 ), labels, 0, 8000 ) chart.drawScale() pygame.display.flip() raw_input() pygame.quit()