graph.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import tkinter as tk
  2. import time, math
  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.height = self.winfo_reqheight()
  9. self.width = self.winfo_reqwidth()
  10. self.lastPoints = [(0, 0)] * 3
  11. self.scale = scale
  12. self.colors = [(100, 255, 100, 255), (255, 100, 100, 255) ,(100, 100, 255, 255)]
  13. self.font = ImageFont.truetype("gui/SourceSansPro-Semibold.otf", 12)
  14. self.lineWidth = 1
  15. self.drawBackground()
  16. self.canvas = self.bg.copy()
  17. self.image = self.create_image(0, 0, image=None, anchor='nw')
  18. self.bind("<Configure>", self.on_resize)
  19. def drawBackground(self):
  20. self.bg = Image.new('RGB', (self.width, self.height), (0,20,0))
  21. draw = ImageDraw.Draw(self.bg)
  22. axes = self.pointToCoord((0, 0))
  23. # draw x and y axis
  24. draw.line([(0, axes[1]), (self.width, axes[1])], (60,127,127), self.lineWidth)
  25. draw.line([(axes[0], 0), (axes[0], self.height)], (60,127,127), self.lineWidth)
  26. # draw ticks
  27. for p in range(self.scale[0], self.scale[1], 10**int(math.log10(self.scale[1]-self.scale[0]))):
  28. tickPosX = self.pointToCoord((p, 0))
  29. tickPosY = self.pointToCoord((0, p))
  30. draw.line([(tickPosX[0], tickPosX[1]+self.lineWidth*3), (tickPosX[0], tickPosX[1]-self.lineWidth*3)], (60,127,127), int(self.lineWidth/2))
  31. draw.line([(tickPosY[0]+self.lineWidth*3, tickPosY[1]), (tickPosY[0]-self.lineWidth*3, tickPosY[1])], (60,127,127), int(self.lineWidth/2))
  32. draw.text((tickPosX[0]+3, tickPosX[1]+self.lineWidth*4), str(p) + " mm", font=self.font)
  33. if p != 0:
  34. draw.text((tickPosY[0]-self.lineWidth*4-35, tickPosY[1]), str(p) + " mm", font=self.font)
  35. def on_resize(self,event):
  36. self.width = max(100, event.width-4)
  37. self.height = max(100, event.height-4)
  38. self.lineWidth = int(max(min(self.width,self.height) / 100, 1))
  39. # resize the canvas
  40. self.canvas = self.canvas.resize((self.width, self.height))
  41. self.drawBackground()
  42. self.canvas = Image.blend(self.canvas, self.bg, 1)
  43. def pointToCoord(self, point):
  44. return ((point[0] - self.scale[0]) / (self.scale[1]-self.scale[0]) * self.width,
  45. self.height - (point[1] - self.scale[0]) / (self.scale[1]-self.scale[0]) * self.height - 1)
  46. def update(self, data):
  47. coord = [[self.pointToCoord(p)] for p in self.lastPoints]
  48. for i in range(len(data)):
  49. for point in data[i]:
  50. coord[i].append(self.pointToCoord(point))
  51. self.lastPoints = [line[-1] for line in data]
  52. self.canvas = Image.blend(self.canvas, self.bg, 1/100)
  53. draw = ImageDraw.Draw(self.canvas)
  54. for i in range(len(coord)):
  55. draw.line(coord[i], fill=self.colors[i], width=self.lineWidth+2, joint='curve')
  56. self.photo = ImageTk.PhotoImage(self.canvas)
  57. self.itemconfig(self.image, image=self.photo)
  58. def clear(self):
  59. self.canvas = self.bg.copy()