473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

scrolled canvas ,strange problem

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, 137 views)
File Type: jpg attach2.jpg (40.5 KB, 138 views)
Attached Files
File Type: txt AAU.txt (57.4 KB, 343 views)
Sep 3 '11 #1
2 1563
bvdet
2,851 Expert Mod 2GB
#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
2,851 Expert Mod 2GB
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

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

Similar topics

6
by: Eric Boutin | last post by:
Hi ! I have a strange problem with a std::ostringstream.. code : #include <sstream> /*...*/ std::ostringstream ss(); ss << "\"\"" << libpath << "\"\" \"\"" << argfilename << "\"\"...
3
by: Andrew Mayo | last post by:
(note: reason for posting here; browser helper object is written in C++; C++ developers tend to know the intricacies of message handling; this looks like a Windows messaging issue) Microsoft...
0
by: João Santa Bárbara | last post by:
Hi all. i have a strange problem i have done a User Control, in wich i have placed a few buttons inside it and place it in an form. in the previous version of .NET (2003), i have try to access...
3
by: Sameh Halabi | last post by:
Hi I have a strange problem in C , I hope someone may help with it . the problem is as follows : I have an existing big structure (that I can't change) which contains in it groups of...
6
by: leonecla | last post by:
Hi everybody, I'm facing a very very strange problem with a very very simple C program... My goal should be to write to a binary file some numbers (integers), each one represented as a sequence...
1
by: Sam Kong | last post by:
Hello! Recently I had a strange problem with Visual C# 2005 beta 1. When I ran(F5 key) a program, <#if DEBUG> statement was not working. It ran as RELEASE mode. So I had to manually define...
12
by: ishtar2020 | last post by:
Hi everybody I've been writing my very first application in Python and everything is running smoothly, except for a strange problem that pops up every once in a while. I'm sure is the kind of...
0
by: riteshjain1982 | last post by:
Pagepostback.....strange problem Hi, I am at cient place and getting very strange problem with my asp.net application............when i call my web application from any desktop machine in lan...
1
by: Victor | last post by:
Hi guys, I have a very strange problem with scriptmanager here. I want to load a js (which is embed in the project) but everytime i try to load that, it gives me error like Specified argument was...
4
by: John Brock | last post by:
I have a .NET application that, among other things, creates Excel workbooks, and I have run into a very strange problem involving formulas on one worksheet that reference values on another...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.