#----------------------------------------------------------- # Disegno grafici tk - By C.Fin 12/8/2018 #----------------------------------------------------------- try: import tkinter as tk except: import Tkinter as tk from math import log class MyGraph(object): #------------------------------------------------------- def __init__(self, parent, d): self.parent = parent self.d = d #calcolo costanti di conversione if d.xlog: K1 = (d.wid - 1.) / (log(d.xmax) - log(d.xmin)) K2 = -(K1 * log(d.xmin)) else: K1 = (d.wid - 1.) / (d.xmax - d.xmin) K2 = -(K1 * d.xmin) self.xK1 = K1 self.xK2 = K2 if d.ylog: K1 = (1. - d.hei) / (log(d.ymax) - log(d.ymin)) K2 = d.hei - 1 - K1*log(d.ymin) else: K1 = (1. - d.hei) / (d.ymax - d.ymin) K2 = d.hei - 1 - K1*d.ymin self.yK1 = K1 self.yK2 = K2 self._create_graph() self._create_axis() self._create_text() #------------------------------------------------------- def _create_graph(self): self.extcanv = tk.Canvas( self.parent, highlightthickness=0, bg='white', width=self.d.wid+self.d.margin_left+self.d.margin_right, height=self.d.hei+self.d.margin_top+self.d.margin_bottom ) self.canv = tk.Canvas( self.extcanv, highlightthickness=0, bg=self.d.paper, width=self.d.wid, height=self.d.hei ) self.extcanv.create_window( self.d.margin_left, self.d.margin_top, window=self.canv, anchor=tk.NW ) if self.d.border: b = [] b.append((self.d.margin_left-1, self.d.margin_top-1)) b.append((self.d.margin_left+self.d.wid, self.d.margin_top-1)) b.append((self.d.margin_left+self.d.wid, self.d.margin_top+self.d.hei)) b.append((self.d.margin_left-1, self.d.margin_top+self.d.hei)) b.append((self.d.margin_left-1, self.d.margin_top-1)) self.extcanv.create_line(b, fill='black') #------------------------------------------------------- def _create_axis(self): if self.d.axis and self.d.oriz_axis[0]: ylist = self.d.oriz_axis[0] self.oriz( ylist, self.d.axis_color, fill=self.d.oriz_axis[1], arrow=0 ) if self.d.axis and self.d.vert_axis[0]: xlist = self.d.vert_axis[0] self.vert( xlist, self.d.axis_color, fill=self.d.vert_axis[1], arrow=0 ) if self.d.origin: if not self.d.ylog: self.oriz([0.], self.d.origin_color, 1, self.d.origin_arrow) if not self.d.xlog: self.vert([0.], self.d.origin_color, 1, self.d.origin_arrow) #------------------------------------------------------- def _create_text(self): if 1: self.extcanv.create_text( self.d.margin_left+self.d.wid+self.d.margin_right-4, self.d.margin_top+self.d.hei+self.d.margin_bottom-4, text='By C.Fin 2018', font=('monospace', 5, 'normal'), fill='silver', anchor=tk.SE ) if not self.d.text: return if self.d.txt_y: self.tvert(self.d.txt_y) if self.d.txt_x: self.toriz(self.d.txt_x) if self.d.title: self.extcanv.create_text( self.d.margin_left//2, self.d.margin_top//2, text=self.d.title, font=('monospace', 12, 'bold'), anchor=tk.W ) if self.d.xtext: self.extcanv.create_text( (self.d.margin_left+self.d.wid+self.d.margin_right)//2, self.d.margin_top+self.d.hei+(self.d.margin_bottom//2)+8, text=self.d.xtext, font=('monospace', 12, 'bold') ) #------------------------------------------------------- #funzioni di conversione relative->pixel def _xmap(self, n): if self.d.xlog: return int(round(self.xK1*log(n) + self.xK2)) else: return int(round(self.xK1*n + self.xK2)) #------------------------------------------------------- #funzioni di conversione relative->pixel def _ymap(self, n): if self.d.ylog: return int(round(self.yK1*log(n) + self.yK2)) else: return int(round(self.yK1*n + self.yK2)) #------------------------------------------------------- #tracciamento asse orizzontale def oriz(self, el, color, fill=1, arrow=0): for n in el: y = self._ymap(n) if fill: it = self.canv.create_line(0, y, self.d.wid, y, fill=color) if arrow: self.canv.itemconfigure(it, arrow='last') else: self.canv.create_line(0, y, self.d.wid, y, fill=color, dash=[3, 3]) self.canv.create_line(0, y, self.d.lsegm, y, fill='black') self.canv.create_line(self.d.wid, y, self.d.wid-self.d.lsegm, y, fill='black') #------------------------------------------------------- #tracciamento asse verticale def vert(self, el, color, fill=1, arrow=0): for n in el: x = self._xmap(n) if fill: it = self.canv.create_line(x, 0, x, self.d.hei , fill=color) if arrow: self.canv.itemconfigure(it, arrow='first') else: self.canv.create_line(x, 0, x, self.d.hei, fill=color, dash=[3, 3]) self.canv.create_line(x, 0, x, self.d.lsegm , fill='black') self.canv.create_line(x, self.d.hei, x, self.d.hei-self.d.lsegm , fill='black') #------------------------------------------------------- def create_curve(self, data, fill='red'): data = [( self._xmap(x), self._ymap(y) ) for x, y in data] return self.canv.create_line(*data, fill=fill) #------------------------------------------------------- def update_curve(self, item, data): data = [( self._xmap(x), self._ymap(y) ) for x, y in data] d = [] for el in data: d.extend(list(el)) self.canv.coords(item, *d) #------------------------------------------------------- def tvert(self, el): for n in el: y = self._ymap(n) self.extcanv.create_text( self.d.margin_left-8, self.d.margin_top+y, text=self.d.ymask.format(n), font=('monospace', 8, 'normal'), anchor=tk.E ) #------------------------------------------------------- def toriz(self, el): for n, v in el: x = self._xmap(n) self.extcanv.create_text( self.d.margin_left+x, self.d.margin_top+self.d.hei+8, text=self.d.xmask.format(v), font=('monospace', 8, 'normal'), anchor=tk.N ) #------------------------------------------------------- def pack(self, *arg, **kw): self.extcanv.pack(*arg, **kw) #------------------------------------------------------- def grid(self, *arg, **kw): self.extcanv.grid(*arg, **kw) #-----------------------------------------------------------