graph.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import time, math
  2. import tkinter as tk
  3. from PIL import Image, ImageDraw, ImageTk, ImageFont
  4. class Graph(tk.Canvas):
  5. def __init__(self, root, scale=(-100, 550), **kwargs):
  6. self.root = root
  7. tk.Canvas.__init__(self, root, **kwargs)
  8. self.image = self.create_image(0, 0, image=None, anchor='nw')
  9. self.height = self.winfo_reqheight()
  10. self.width = self.winfo_reqwidth()
  11. # store last point of plot to connect the lines
  12. self.lastPoints = [(0, 0)] * 3
  13. # scale contains the (min, max) values of both axes
  14. self.scale = scale
  15. # appearance of the plots
  16. self.colors = [(100, 255, 100, 255), (255, 100, 100, 255) ,(100, 100, 255, 255)]
  17. self.font = ImageFont.truetype("gui/SourceSansPro-Semibold.otf", 12)
  18. self.lineWidth = 1
  19. # the background contains all static elements
  20. self.drawBackground()
  21. # the plots will be drawn on a separate canvas
  22. self.canvas = self.bg.copy()
  23. self.bind("<Configure>", self.on_resize)
  24. def drawBackground(self):
  25. self.bg = Image.new('RGB', (self.width, self.height), (0,10,0))
  26. draw = ImageDraw.Draw(self.bg)
  27. # draw x and y axis
  28. axes = self.pointToCoord((0, 0))
  29. draw.line([(0, axes[1]), (self.width, axes[1])], (60,127,127), self.lineWidth)
  30. draw.line([(axes[0], 0), (axes[0], self.height)], (60,127,127), self.lineWidth)
  31. for p in range(self.scale[0], self.scale[1], 10**int(math.log10(self.scale[1]-self.scale[0]))):
  32. tickPosX = self.pointToCoord((p, 0))
  33. tickPosY = self.pointToCoord((0, p))
  34. #draw grid
  35. draw.line([(tickPosX[0], self.height), (tickPosX[0], 0)], (60,127,60), 1)
  36. draw.line([(self.width, tickPosY[1]), (0, tickPosY[1])], (60,127,60), 1)
  37. # draw ticks
  38. draw.line([(tickPosX[0], tickPosX[1]+self.lineWidth*3), (tickPosX[0], tickPosX[1]-self.lineWidth*3)], (60,127,100), int(self.lineWidth/2))
  39. draw.line([(tickPosY[0]+self.lineWidth*3, tickPosY[1]), (tickPosY[0]-self.lineWidth*3, tickPosY[1])], (60,127,100), int(self.lineWidth/2))
  40. # draw tick labels
  41. draw.text((tickPosX[0]+3, tickPosX[1]+self.lineWidth*4), str(p) + " mm", font=self.font)
  42. if p != 0:
  43. draw.text((tickPosY[0]-self.lineWidth*4-35, tickPosY[1]), str(p) + " mm", font=self.font)
  44. def on_resize(self,event):
  45. self.width = max(100, event.width-4)
  46. self.height = max(100, event.height-4)
  47. self.lineWidth = int(max(min(self.width,self.height) / 100, 1))
  48. # resize the canvas
  49. self.canvas = self.canvas.resize((self.width, self.height))
  50. self.drawBackground()
  51. self.canvas = Image.blend(self.canvas, self.bg, 1)
  52. # convert physical space to screen space
  53. def pointToCoord(self, point):
  54. return ((point[0] - self.scale[0]) / (self.scale[1]-self.scale[0]) * self.width,
  55. self.height - (point[1] - self.scale[0]) / (self.scale[1]-self.scale[0]) * self.height - 1)
  56. def update(self, data):
  57. # load first point of line
  58. coord = [[self.pointToCoord(p)] for p in self.lastPoints]
  59. # append new points
  60. for i in range(len(data)):
  61. for point in data[i]:
  62. coord[i].append(self.pointToCoord(point))
  63. self.canvas = Image.blend(self.canvas, self.bg, 1/100)
  64. if len(data[0]) > 0:
  65. self.lastPoints = [line[-1] for line in data]
  66. #fade out old lines by mixing with background
  67. draw = ImageDraw.Draw(self.canvas)
  68. for i in range(len(coord)):
  69. draw.line(coord[i], fill=self.colors[i], width=self.lineWidth+2, joint='curve')
  70. # draw to tk.Canvas
  71. self.photo = ImageTk.PhotoImage(self.canvas)
  72. self.itemconfig(self.image, image=self.photo)
  73. def clear(self):
  74. self.canvas = self.bg.copy()