Browse Source

added graph scaling

subDesTagesMitExtraKaese 4 years ago
parent
commit
1d9d3383e7
1 changed files with 18 additions and 8 deletions
  1. 18 8
      raspberry-pi/gui/graph.py

+ 18 - 8
raspberry-pi/gui/graph.py

@@ -3,19 +3,20 @@ import time, math
 from PIL import Image, ImageDraw, ImageTk
 
 class Graph(tk.Canvas):
-  def __init__(self, root, **kwargs):
+  def __init__(self, root, scale=(-100, 500), **kwargs):
     self.root = root
     tk.Canvas.__init__(self, root, **kwargs)
     self.height = self.winfo_reqheight()
     self.width = self.winfo_reqwidth()
-    self.coord = [(10, 50), (20, 50), (100, 100)]
+    self.lastPoints = [(0, 0)] * 3
+    self.scale = scale
+    self.colors = [(100, 255, 100, 255), (255, 100, 100, 255) ,(100, 100, 255, 255)]
     
     self.drawBackground()
     self.canvas = self.bg.copy()
     self.image = self.create_image(0, 0, image=None, anchor='nw')
 
     self.bind("<Configure>", self.on_resize)
-    self.scale = 450 / 2
 
   def drawBackground(self):
     self.bg = Image.new('RGB', (self.width, self.height), (0,20,0))
@@ -34,14 +35,23 @@ class Graph(tk.Canvas):
     self.drawBackground()
     self.canvas = Image.blend(self.canvas, self.bg, 0.1)
 
+  def pointToCoord(self, point):
+    return ((point[0] - self.scale[0]) / (self.scale[1]-self.scale[0]) * self.width, 
+      self.height - (point[1] - self.scale[0]) / (self.scale[1]-self.scale[0]) * self.height - 1)
+
   def update(self, data):
-    self.coord = [self.coord[-1]]
-    for point in data[0]:
-      self.coord.append((point[0] / self.scale * self.width/2, point[1] / self.scale * self.height/2))
+    coord = [[self.pointToCoord(p)] for p in self.lastPoints]
+
+    for i in range(len(data)):
+      for point in data[i]:
+        coord[i].append(self.pointToCoord(point))
+
+    self.lastPoints = [line[-1] for line in data]
 
-    self.canvas = Image.blend(self.canvas, self.bg, 1/250)
+    self.canvas = Image.blend(self.canvas, self.bg, 1/200)
     draw = ImageDraw.Draw(self.canvas)
-    draw.line(self.coord, fill=(100, 255, 100, 255), width=int(self.height/100), joint='curve')
+    for i in range(len(coord)):
+      draw.line(coord[i], fill=self.colors[i], width=int(self.height/100), joint='curve')
 
     self.photo = ImageTk.PhotoImage(self.canvas)
     self.itemconfig(self.image, image=self.photo)