graph.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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=(-50, 450), **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. step = 10**int(math.log10(self.scale[1]-self.scale[0]))
  32. begin = math.floor(self.scale[0] / step) * step
  33. end = math.ceil(self.scale[1] / step) * step
  34. halfStep = int(step / 2)
  35. # label x and y axis
  36. draw.text(self.pointToCoord(((end+begin)/2+25, -30)), "x in mm", font=self.font, align='center')
  37. draw.text(self.pointToCoord((-45, (end+begin)/2+25)), "y in mm", font=self.font, align='left')
  38. for p in range(begin, end, halfStep):
  39. tickPosX = self.pointToCoord((p, 0))
  40. tickPosY = self.pointToCoord((0, p))
  41. #draw grid
  42. draw.line([(tickPosX[0], self.height), (tickPosX[0], 0)], (60,127,60), 1)
  43. draw.line([(self.width, tickPosY[1]), (0, tickPosY[1])], (60,127,60), 1)
  44. for p in range(begin, end, step):
  45. tickPosX = self.pointToCoord((p, 0))
  46. tickPosY = self.pointToCoord((0, p))
  47. # draw ticks
  48. draw.line([(tickPosX[0], tickPosX[1]+self.lineWidth*3), (tickPosX[0], tickPosX[1]-self.lineWidth*3)], (60,127,100), int(self.lineWidth/2))
  49. draw.line([(tickPosY[0]+self.lineWidth*3, tickPosY[1]), (tickPosY[0]-self.lineWidth*3, tickPosY[1])], (60,127,100), int(self.lineWidth/2))
  50. # draw tick labels
  51. draw.text((tickPosX[0]+3, tickPosX[1]+self.lineWidth*1), str(p), font=self.font)
  52. if p != 0:
  53. draw.text((tickPosY[0]+self.lineWidth*2, tickPosY[1]), str(p), font=self.font, align='right')
  54. def on_resize(self,event):
  55. self.width = max(100, event.width-4)
  56. self.height = max(100, event.height-4)
  57. self.lineWidth = int(max(min(self.width,self.height) / 100, 1))
  58. # resize the canvas
  59. self.canvas = self.canvas.resize((self.width, self.height))
  60. self.drawBackground()
  61. self.canvas = Image.blend(self.canvas, self.bg, 1)
  62. # convert physical space to screen space
  63. def pointToCoord(self, point):
  64. return ((point[0] - self.scale[0]) / (self.scale[1]-self.scale[0]) * self.width,
  65. self.height - (point[1] - self.scale[0]) / (self.scale[1]-self.scale[0]) * self.height - 1)
  66. def update(self, data):
  67. # load first point of line
  68. coord = [[self.pointToCoord(p)] for p in self.lastPoints if p]
  69. newPoints = False
  70. # append new points
  71. for i in range(len(data)):
  72. for point in data[i]:
  73. coord[i].append(self.pointToCoord(point))
  74. self.lastPoints[i] = point
  75. newPoints = True
  76. self.canvas = Image.blend(self.canvas, self.bg, 1/100)
  77. if newPoints:
  78. #fade out old lines by mixing with background
  79. draw = ImageDraw.Draw(self.canvas)
  80. for i in range(len(coord)):
  81. draw.line(coord[i], fill=self.colors[i], width=self.lineWidth+2, joint='curve')
  82. # draw to tk.Canvas
  83. self.photo = ImageTk.PhotoImage(self.canvas)
  84. self.itemconfig(self.image, image=self.photo)
  85. def clear(self):
  86. self.canvas = self.bg.copy()