Browse Source

added axes into graph

subDesTagesMitExtraKaese 4 years ago
parent
commit
585ba3183c
1 changed files with 21 additions and 5 deletions
  1. 21 5
      raspberry-pi/gui/graph.py

+ 21 - 5
raspberry-pi/gui/graph.py

@@ -11,6 +11,7 @@ class Graph(tk.Canvas):
     self.lastPoints = [(0, 0)] * 3
     self.scale = scale
     self.colors = [(100, 255, 100, 255), (255, 100, 100, 255) ,(100, 100, 255, 255)]
+    self.lineWidth = 1
     
     self.drawBackground()
     self.canvas = self.bg.copy()
@@ -21,17 +22,32 @@ class Graph(tk.Canvas):
   def drawBackground(self):
     self.bg = Image.new('RGB', (self.width, self.height), (0,20,0))
     draw = ImageDraw.Draw(self.bg)
-    draw.line([(0, self.height/2), (self.width, self.height/2)], (0,127,127), 1)
-    draw.line([(self.width/2, 0), (self.width/2, self.height)], (0,127,127), 1)
 
+    axes = self.pointToCoord((0, 0))
+
+    # draw x and y axis
+    draw.line([(0, axes[1]), (self.width, axes[1])], (60,127,127), self.lineWidth)
+    draw.line([(axes[0], 0), (axes[0], self.height)], (60,127,127), self.lineWidth)
+
+    # draw ticks
+    for p in range(self.scale[0], self.scale[1], 10**int(math.log10(self.scale[1]-self.scale[0]))):
+      tickPosX = self.pointToCoord((p, 0))
+      tickPosY = self.pointToCoord((0, p))
+      draw.line([(tickPosX[0], tickPosX[1]+self.lineWidth*3), (tickPosX[0], tickPosX[1]-self.lineWidth*3)], (60,127,127), int(self.lineWidth/2))
+      draw.line([(tickPosY[0]+self.lineWidth*3, tickPosY[1]), (tickPosY[0]-self.lineWidth*3, tickPosY[1])], (60,127,127), int(self.lineWidth/2))
+
+      draw.text((tickPosX[0]+3, tickPosX[1]+self.lineWidth*4), str(p) + " mm")
+      if p != 0:
+        draw.text((tickPosY[0]-self.lineWidth*4-30, tickPosY[1]), str(p) + " mm")
 
   def on_resize(self,event):
     self.width = max(100, event.width-4)
     self.height = max(100, event.height-4)
+    self.lineWidth = int(max(min(self.width,self.height) / 100, 1))
     # resize the canvas 
     self.canvas = self.canvas.resize((self.width, self.height))
     self.drawBackground()
-    self.canvas = Image.blend(self.canvas, self.bg, 0.1)
+    self.canvas = Image.blend(self.canvas, self.bg, 1)
 
   def pointToCoord(self, point):
     return ((point[0] - self.scale[0]) / (self.scale[1]-self.scale[0]) * self.width, 
@@ -46,10 +62,10 @@ class Graph(tk.Canvas):
 
     self.lastPoints = [line[-1] for line in data]
 
-    self.canvas = Image.blend(self.canvas, self.bg, 1/200)
+    self.canvas = Image.blend(self.canvas, self.bg, 1/100)
     draw = ImageDraw.Draw(self.canvas)
     for i in range(len(coord)):
-      draw.line(coord[i], fill=self.colors[i], width=int(self.height/100), joint='curve')
+      draw.line(coord[i], fill=self.colors[i], width=self.lineWidth+2, joint='curve')
 
     self.photo = ImageTk.PhotoImage(self.canvas)
     self.itemconfig(self.image, image=self.photo)