By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

scrolled canvas ,strange problem

P: 18
i have written a program in tkinter to draw stock graph,there are two strange thing i can't understand
the attachment is 'AAU' data file(attachment 1).when you download it ,please save it in /tmp/AAU
there is a scrolled canvas in it ,

1.strang long line,please see my attachment.
i have see my data ,my formulation ,no wrong,but the line is so long!!
2.there is no motion event reaction in the right of canvas,only motion event reaction in the left of canvas.
3.i have round function ,but the output is still more fractions than 2.
Expand|Select|Wrap|Line Numbers
  1. #coding:utf-8
  2. from Tkinter import *
  3. import string
  4.  
  5. class  hdraw(object):
  6.     def  __init__(self,fname):
  7.         file='/tmp/'+fname
  8.         self.stock=fname
  9.         self.data=(open(file,'r').readlines())[1:-1]
  10.         self.data.reverse()
  11.         self.xsqueen=range(0,len(self.data)*2,2)
  12.         self.pairs=len(self.data)           
  13.         #Open,High,Low,Close,Volume,Adj Close
  14.         self.date         =[i.split(',')[0]   for  i  in  self.data]
  15.         self.open         =[string.atof(i.split(',')[1])  for  i  in  self.data]
  16.         self.high         =[string.atof(i.split(',')[2])  for  i  in  self.data]
  17.         self.low          =[string.atof(i.split(',')[3])  for  i  in  self.data]
  18.         self.close        =[string.atof(i.split(',')[4])  for  i  in  self.data]
  19.         self.vol          =[string.atof(i.split(',')[5])  for  i  in  self.data]
  20.         self.adjclose     =[string.atof(i.split(',')[6])  for  i  in  self.data]
  21.         self.ymax         =max(self.open+self.high+self.low+self.close)
  22.         self.ymin         =min(self.open+self.high+self.low+self.close)
  23.         self.width        =self.pairs*4
  24.         times=self.ymax//self.ymin+1
  25.         if  times  < 5:
  26.             self.height=times*180
  27.         elif   5<= times  <10:
  28.             self.height=times*100
  29.         elif    10 <= times  <20:
  30.             self.height=times*80
  31.         else:
  32.             self.height=times*50
  33.  
  34.  
  35.     def  getyposition(self,p):
  36.         yp=(50+self.height)-(p-self.ymin)*self.height/(self.ymax-self.ymin)
  37.         return yp
  38.  
  39.     def  getxposition(self,x):
  40.         xp=50+4*x
  41.         return  xp
  42.  
  43.     def myprint(self,event):
  44.         # convert the event coordinates to canvas coordinates
  45.         x = canv.canvasx(event.x)
  46.         y = canv.canvasy(event.y)
  47.         # find the poistion in the top right corner of your canvas widget
  48.         # less a little bit so as to see the displayed text
  49.         if  x  in  self.xsqueen :
  50.             ith=self.xsqueen.index(x)
  51.  
  52.              # delete previous displayed text
  53.             canv.delete('coords')
  54.  
  55.             # display the coordinates
  56.             canv.create_text(x,y,anchor=E,text='%s,%f,%f,%f,%f' % (self.date[ith],   \
  57.             round(self.open[ith],2),round(self.high[ith],2),round(self.low[ith],2)   \
  58.             ,round(self.close[ith],2)),tags='coords')
  59.  
  60.  
  61.     def  drawcanvas(self):
  62.         global  canv
  63.         print  self.stock
  64.         root.title('the history curve of %s'  %  self.stock)
  65.         canv = Canvas(root, bg='white', relief=SUNKEN)
  66.         canv.config(width=self.width+180,height=self.height+100)
  67.         canv.config(scrollregion=(0,0,self.width+180,self.height+100))     
  68.         canv.config(highlightthickness=0)
  69.         ybar = Scrollbar(root)
  70.         xbar=Scrollbar(root,orient='horizontal')
  71.         ybar.config(command=canv.yview)
  72.         xbar.config(command=canv.xview)                
  73.         canv.config(yscrollcommand=ybar.set)
  74.         canv.config(xscrollcommand=xbar.set)           
  75.         ybar.pack(side=RIGHT, fill=Y)
  76.         xbar.pack(side=BOTTOM,fill=X)                 
  77.         canv.pack(side=LEFT, expand=YES, fill=BOTH)
  78.         canv.create_text(20,20,text=self.stock)
  79.         # display the coordinates
  80.         canv.create_line(50,self.height+50,self.width+100,self.height+50,width=4)
  81.         canv.create_line(50,self.height+50,50,0,width=4)
  82.         canv.create_line(100+self.width,self.height+50,100+self.width,0,width=4)
  83.         canv.create_line(50,self.getyposition(self.ymax),self.width+100,self.getyposition(self.ymax),width=4)
  84.         #the last day line 
  85.         yyp=self.getyposition(self.close[-1])
  86.         canv.create_line(50,yyp,100+self.width,yyp,width=2)
  87.         #right  coordinates
  88.         canv.create_text(135+self.width,yyp,text=str(self.close[-1])+'    '+'100%')
  89.         #left  coordinates
  90.         canv.create_text(35,yyp,text=str(self.close[-1]))
  91.         #x  coordinates
  92.         for i in range(0,self.pairs,20):
  93.             canv.create_text(50+2*i,self.height+50,text=(self.date[i]).split('-')[1],anchor=N)
  94.         for i in range(0,self.pairs,120):
  95.              canv.create_text(50+2*i,self.height+65,text=(self.date[i]).split('-')[0],anchor=N)
  96.  
  97.         #draw  data
  98.         p=self.close[-1]
  99.         c=100
  100.         while  p*1.1<self.ymax:
  101.             c=c*1.1
  102.             p=p*1.1
  103.             yp=self.getyposition(p)
  104.             canv.create_text(135+self.width,yp,text=str(round(c))+'%')
  105.             canv.create_text(35,yp,text=str(round(p,2)))
  106.             canv.create_line(50,yp,100+self.width,yp)
  107.  
  108.         p=self.close[-1]
  109.         c=100
  110.         while  p*0.9>self.ymin  and  c*0.9>25:
  111.                 c=c*0.9
  112.                 p=p*0.9
  113.                 yp=self.getyposition(p)
  114.                 canv.create_text(135+self.width,yp,text=str(round(c))+'%')
  115.                 canv.create_text(35,yp,text=str(round(p,2)))
  116.                 canv.create_line(50,yp,100+self.width,yp)
  117.  
  118.         #max price line
  119.         canv.create_text(35,self.getyposition(self.ymax),text=str(self.ymax))
  120.         #min price line
  121.         canv.create_text(35,self.getyposition(self.ymin),text=str(self.ymin))
  122.  
  123.         canv.bind("<Motion>",self.myprint)
  124.         for i in range(0,self.pairs):
  125.             if  self.close[i]  >=  self.open[i] :
  126.                 colour='green'
  127.             else:
  128.                 colour='red'
  129.             #draw  k  line 
  130.             x1=self.getxposition(i)
  131.             y1=self.getyposition(self.open[i])
  132.             x2=x1
  133.             y2=self.getyposition(self.high[i])
  134.             x3=x1
  135.             y3=self.getyposition(self.low[i])
  136.             x4=x1
  137.             y4=self.getyposition(self.close[i])
  138.             canv.create_line(x2,y2,x3,y3,fill=colour)
  139.             canv.create_line(x1-2,y1,x1,y1,fill=colour)
  140.             canv.create_line(x1,y4,x1+2,y4,fill=colour)
  141.  
  142.  
  143. root = Tk()
  144. s=hdraw('AAU')
  145. s.drawcanvas()
  146.  
  147. root.mainloop()
  148.  
  149.  
Attached Images
File Type: jpg attach1.jpg (46.5 KB, 100 views)
File Type: jpg attach2.jpg (40.5 KB, 110 views)
Attached Files
File Type: txt AAU.txt (57.4 KB, 288 views)
Sep 3 '11 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
#3 Configure your text to 2 decimal places:
Expand|Select|Wrap|Line Numbers
  1.             canv.create_text(x,y,anchor=E,
  2.                              text='%s,%.2f,%.2f,%.2f,%.2f' % (self.date[ith],
  3.                                                               self.open[ith],
  4.                                                               self.high[ith],
  5.                                                               self.low[ith],
  6.                                                               self.close[ith]),
  7.                              tags='coords')
#2 There is no text displayed on the right part of the screen because 'x' is not in self.xsqueen.
Sep 4 '11 #2

bvdet
Expert Mod 2.5K+
P: 2,851
Your canvas is too big to display on my screen. Configure the canvas size to be smaller than the screen. Configuring the scrollregion takes care of the required size for your data.

It looks like you are creating 3 lines on top of one another. There must be something wrong with the calculation of the 'y' position in self.getyposition. At index 295, the calculated 'y' value difference is 395.5378. I don't think you intended that.
Sep 4 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.