Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # mygraph.py
- import wx
- from datetime import date,datetime,time
- import time
- import math
- class Line(wx.Frame):
- def __init__(self, parent, id, FrameTitle, IncomingData, ChartTitle):
- wx.Frame.__init__(self, parent, id, FrameTitle, size=(1024, 768))
- self.Bind(wx.EVT_PAINT, self.OnPaint)
- self.BoxWidth = 790
- self.BoxHeight = 690
- self.ChartTitle = ChartTitle
- self.data = []
- self.SetData(IncomingData)
- self.Centre()
- self.Show(True)
- def DrawBox(self,dc):
- #Horizontal
- dc.DrawLine(10,10,800,10)
- dc.DrawLine(10,700,800,700)
- #Vertical
- dc.DrawLine(10,10,10,700)
- dc.DrawLine(800,10,800,700)
- def DrawAxis(self,dc):
- #Horizontal
- dc.DrawLine(60,580,700,580)
- #Vertical
- dc.DrawLine(60,580,60,80)
- def DrawValues(self,dc):
- c2 = 0
- for cntr in range(580,30,-50):
- dc.SetPen(wx.Pen(wx.NamedColour('black'),1))
- dc.DrawLine(60,cntr,50,cntr)
- dc.SetFont(wx.Font(10,wx.DEFAULT,wx.NORMAL,wx.BOLD))
- dc.SetPen(wx.Pen(wx.NamedColour('black'),20))
- dc.DrawText(str(c2),26,cntr-7)
- c2 = c2 + (50 * self.ScaleValue)
- def DrawBars(self,dc):
- dc.SetPen(wx.Pen(wx.NamedColour('black'),5))
- for cntr in range(0,len(self.ValList)):
- dc.DrawRectangle(84 + (cntr* 20),580,2,self.ValList[cntr]/-self.ScaleValue)
- def DrawRotText(self,dc,txt,x,y):
- dc.SetFont(wx.Font(10,wx.DEFAULT,wx.NORMAL,wx.BOLD))
- dc.SetPen(wx.Pen(wx.NamedColour('black'),20))
- dc.DrawRotatedText(txt,x,y,-45)
- def DrawTitle(self,dc,txt):
- dc.SetFont(wx.Font(20,wx.DEFAULT,wx.NORMAL,wx.BOLD))
- dc.SetPen(wx.Pen(wx.NamedColour('black'),20))
- #Get the length of the text to draw
- vals = dc.GetFullTextExtent(txt)
- # Returned (Width,height,Decent,externalLeading)
- #Get the left position (x) to draw centered text
- txtleft = (self.BoxWidth-vals[0])/2
- dc.DrawText(txt,txtleft,30)
- # Reset the pen size and colour
- dc.SetPen(wx.Pen(wx.NamedColour('black'),2))
- def DrawDateTicks(self,dc,dcount):
- for cntr in range(1,dcount+1):
- dc.DrawLine(65+(cntr*20),580,65+(cntr*20),600)
- #==================================
- # Convert mm/dd/yy to unix timestamp
- #==================================
- def DateToStamp(self,x):
- x = x+" 00:00:00"
- return(time.mktime(time.strptime(x, "%m/%d/%Y %H:%M:%S")))
- #==================================
- # Convert mm/dd/yy to unix timestamp
- #==================================
- def Timestamp2Date(self,tstmp):
- return datetime.fromtimestamp(int(tstmp)).strftime('%m/%d/%Y')
- #==================================
- # Draw the dates in rotated text
- #==================================
- def DrawDates(self,dc,startdate,enddate):
- sd = int(self.DateToStamp(startdate))
- ed = int(self.DateToStamp(enddate))
- ed = ed + 86400
- stp = 1
- for cntr in range(sd,ed,86400):
- dt = self.Timestamp2Date(cntr)
- self.DrawRotText(dc,dt,65+(stp*20),600)
- stp = stp + 1
- #==================================
- # Round up to the nearest 500
- #==================================
- def roundup(self,x):
- return int(math.ceil(x/500.0))*500
- def SetData(self,DataToUse):
- if type(DataToUse[1]) is tuple:
- self.DateList=[]
- self.ValList=[]
- for l in DataToUse:
- self.DateList.append(l[0])
- self.ValList.append(l[1])
- self.HiValue = self.roundup(max(self.ValList))
- self.ScaleValue = self.HiValue/500
- else:
- self.ValList=[]
- self.DateList=[]
- for l in DataToUse:
- self.ValList.append(l)
- self.HiValue = self.roundup(max(self.ValList))
- self.ScaleValue = self.HiValue/500
- #==================================
- # Main routine
- #==================================
- def OnPaint(self,event):
- dc = wx.PaintDC(self)
- self.DrawBox(dc)
- self.DrawAxis(dc)
- self.DrawTitle(dc,self.ChartTitle)
- ## Date Tics and dates
- self.DrawDateTicks(dc,31)
- leng = len(self.DateList)
- if leng > 0:
- sd = self.DateList[0]
- ed = self.DateList[4]
- self.DrawDates(dc,sd,ed)
- else:
- self.DrawDates(dc,"02/01/2015","03/01/2015")
- # Value Tics - Draw 10 tics
- self.DrawValues(dc)
- # Finally Draw the data bars
- self.DrawBars(dc)
- if __name__ == "__main__":
- 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)
- #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))
- app = wx.App()
- Line(None, -1, 'Bar Chart',data,"Monthly Sales - Colorado Springs")
- app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement