Advertisement
gregwa

mychart.py

Apr 5th, 2015
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. #!/usr/bin/python
  2. # mygraph.py
  3. import wx
  4. from datetime import date,datetime,time
  5. import time
  6. import math
  7.  
  8.  
  9. class Line(wx.Frame):
  10.     def __init__(self, parent, id, FrameTitle, IncomingData, ChartTitle):
  11.         wx.Frame.__init__(self, parent, id, FrameTitle, size=(1024, 768))
  12.         self.Bind(wx.EVT_PAINT, self.OnPaint)
  13.         self.BoxWidth = 790
  14.         self.BoxHeight = 690
  15.         self.ChartTitle = ChartTitle
  16.         self.data = []
  17.         self.SetData(IncomingData)
  18.         self.Centre()
  19.         self.Show(True)
  20.        
  21.     def DrawBox(self,dc):
  22.         #Horizontal
  23.         dc.DrawLine(10,10,800,10)
  24.         dc.DrawLine(10,700,800,700)
  25.         #Vertical
  26.         dc.DrawLine(10,10,10,700)
  27.         dc.DrawLine(800,10,800,700)
  28.        
  29.     def DrawAxis(self,dc):
  30.         #Horizontal
  31.         dc.DrawLine(60,580,700,580)
  32.         #Vertical
  33.         dc.DrawLine(60,580,60,80)
  34.            
  35.     def DrawValues(self,dc):
  36.         c2 = 0
  37.         for cntr in range(580,30,-50):
  38.             dc.SetPen(wx.Pen(wx.NamedColour('black'),1))   
  39.             dc.DrawLine(60,cntr,50,cntr)
  40.             dc.SetFont(wx.Font(10,wx.DEFAULT,wx.NORMAL,wx.BOLD))
  41.             dc.SetPen(wx.Pen(wx.NamedColour('black'),20))          
  42.             dc.DrawText(str(c2),26,cntr-7)
  43.             c2 = c2 + (50 * self.ScaleValue)
  44.            
  45.     def DrawBars(self,dc):
  46.         dc.SetPen(wx.Pen(wx.NamedColour('black'),5))       
  47.         for cntr in range(0,len(self.ValList)):
  48.             dc.DrawRectangle(84 + (cntr* 20),580,2,self.ValList[cntr]/-self.ScaleValue)
  49.    
  50.     def DrawRotText(self,dc,txt,x,y):
  51.         dc.SetFont(wx.Font(10,wx.DEFAULT,wx.NORMAL,wx.BOLD))
  52.         dc.SetPen(wx.Pen(wx.NamedColour('black'),20))
  53.         dc.DrawRotatedText(txt,x,y,-45)
  54.    
  55.     def DrawTitle(self,dc,txt):
  56.         dc.SetFont(wx.Font(20,wx.DEFAULT,wx.NORMAL,wx.BOLD))
  57.         dc.SetPen(wx.Pen(wx.NamedColour('black'),20))
  58.         #Get the length of the text to draw
  59.         vals = dc.GetFullTextExtent(txt)
  60.         # Returned (Width,height,Decent,externalLeading)
  61.         #Get the left position (x) to draw centered text
  62.         txtleft = (self.BoxWidth-vals[0])/2
  63.         dc.DrawText(txt,txtleft,30)
  64.         # Reset the pen size and colour
  65.         dc.SetPen(wx.Pen(wx.NamedColour('black'),2))
  66.    
  67.     def DrawDateTicks(self,dc,dcount):
  68.         for cntr in range(1,dcount+1):
  69.             dc.DrawLine(65+(cntr*20),580,65+(cntr*20),600)
  70.            
  71.     #==================================
  72.     #  Convert mm/dd/yy to unix timestamp
  73.     #==================================
  74.     def DateToStamp(self,x):
  75.         x = x+" 00:00:00"
  76.         return(time.mktime(time.strptime(x, "%m/%d/%Y %H:%M:%S")))         
  77.     #==================================
  78.     #  Convert mm/dd/yy to unix timestamp
  79.     #==================================
  80.     def Timestamp2Date(self,tstmp):
  81.         return datetime.fromtimestamp(int(tstmp)).strftime('%m/%d/%Y')
  82.     #==================================
  83.     #  Draw the dates in rotated text
  84.     #==================================
  85.     def DrawDates(self,dc,startdate,enddate):
  86.         sd = int(self.DateToStamp(startdate))
  87.         ed = int(self.DateToStamp(enddate))
  88.         ed = ed + 86400
  89.         stp = 1
  90.         for cntr in range(sd,ed,86400):
  91.             dt = self.Timestamp2Date(cntr)
  92.             self.DrawRotText(dc,dt,65+(stp*20),600)
  93.             stp = stp + 1
  94.     #==================================
  95.     # Round up to the nearest 500
  96.     #==================================
  97.     def roundup(self,x):
  98.         return int(math.ceil(x/500.0))*500 
  99.    
  100.     def SetData(self,DataToUse):
  101.         if type(DataToUse[1]) is tuple:
  102.             self.DateList=[]
  103.             self.ValList=[]
  104.             for l in DataToUse:
  105.                 self.DateList.append(l[0])
  106.                 self.ValList.append(l[1])
  107.             self.HiValue = self.roundup(max(self.ValList))
  108.             self.ScaleValue = self.HiValue/500
  109.         else:
  110.             self.ValList=[]
  111.             self.DateList=[]
  112.             for l in DataToUse:
  113.                 self.ValList.append(l)
  114.             self.HiValue = self.roundup(max(self.ValList))
  115.             self.ScaleValue = self.HiValue/500
  116.            
  117.     #==================================
  118.     #  Main routine
  119.     #==================================
  120.     def OnPaint(self,event):
  121.         dc = wx.PaintDC(self)
  122.         self.DrawBox(dc)
  123.         self.DrawAxis(dc)
  124.         self.DrawTitle(dc,self.ChartTitle)
  125.         ## Date Tics and dates
  126.         self.DrawDateTicks(dc,31)
  127.         leng = len(self.DateList)
  128.         if leng > 0:
  129.             sd = self.DateList[0]
  130.             ed = self.DateList[4]
  131.             self.DrawDates(dc,sd,ed)
  132.         else:
  133.             self.DrawDates(dc,"02/01/2015","03/01/2015")
  134.         # Value Tics - Draw 10 tics
  135.         self.DrawValues(dc)
  136.         # Finally Draw the data bars
  137.         self.DrawBars(dc)  
  138.            
  139. if __name__ == "__main__":
  140.     data = (300,20,47,96,1200,700,500,230,179,500,300,20,47,96,200,400,500,230,179,500,300,20,47,96,200,400,500,230,179,500,475,423)
  141.     #data = (("02/01/2015",169.63),("02/02/2015",188.81),("02/03/2015",61.85),("02/04/2015",94.53),("02/05/2015",235.85))  
  142.  
  143.     app = wx.App()
  144.     Line(None, -1, 'Bar Chart',data,"Monthly Sales - Colorado Springs")
  145.     app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement