graph.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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,20,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. # draw ticks
  32. for p in range(self.scale[0], self.scale[1], 10**int(math.log10(self.scale[1]-self.scale[0]))):
  33. tickPosX = self.pointToCoord((p, 0))
  34. tickPosY = self.pointToCoord((0, p))
  35. draw.line([(tickPosX[0], tickPosX[1]+self.lineWidth*300), (tickPosX[0], tickPosX[1]-self.lineWidth*300)], (60,127,127), int(self.lineWidth/2))
  36. draw.line([(tickPosY[0]+self.lineWidth*300, tickPosY[1]), (tickPosY[0]-self.lineWidth*300, tickPosY[1])], (60,127,127), int(self.lineWidth/2))
  37. # draw tick labels
  38. draw.text((tickPosX[0]+3, tickPosX[1]+self.lineWidth*4), str(p) + " mm", font=self.font)
  39. if p != 0:
  40. draw.text((tickPosY[0]-self.lineWidth*4-35, tickPosY[1]), str(p) + " mm", font=self.font)
  41. def on_resize(self,event):
  42. self.width = max(100, event.width-4)
  43. self.height = max(100, event.height-4)
  44. self.lineWidth = int(max(min(self.width,self.height) / 100, 1))
  45. # resize the canvas
  46. self.canvas = self.canvas.resize((self.width, self.height))
  47. self.drawBackground()
  48. self.canvas = Image.blend(self.canvas, self.bg, 1)
  49. # convert physical space to screen space
  50. def pointToCoord(self, point):
  51. return ((point[0] - self.scale[0]) / (self.scale[1]-self.scale[0]) * self.width,
  52. self.height - (point[1] - self.scale[0]) / (self.scale[1]-self.scale[0]) * self.height - 1)
  53. def update(self, data):
  54. # load first point of line
  55. coord = [[self.pointToCoord(p)] for p in self.lastPoints]
  56. # append new points
  57. for i in range(len(data)):
  58. for point in data[i]:
  59. coord[i].append(self.pointToCoord(point))
  60. self.canvas = Image.blend(self.canvas, self.bg, 1/100)
  61. if len(data[0]) > 0:
  62. self.lastPoints = [line[-1] for line in data]
  63. #fade out old lines by mixing with background
  64. draw = ImageDraw.Draw(self.canvas)
  65. for i in range(len(coord)):
  66. draw.line(coord[i], fill=self.colors[i], width=self.lineWidth+2, joint='curve')
  67. # draw to tk.Canvas
  68. self.photo = ImageTk.PhotoImage(self.canvas)
  69. self.itemconfig(self.image, image=self.photo)
  70. def clear(self):
  71. self.canvas = self.bg.copy()