#Autogenerated by ReportLab guiedit do not edit
import operator, palette, phrases
from reportlab.graphics.charts.piecharts import Pie
from reportlab.lib.formatters import DecimalFormatter
from reportlab.lib.colors import fidblue, fid_aa_palette, white
from reportlab.graphics.charts.legends import Legend
from rlextra.utils.vecround import vecRound
from reportlab.pdfbase.pdfmetrics import stringWidth, getFont
from reportlab.graphics.shapes import Drawing, _DrawingEditorMixin, Rect, Line, String, Group

class YPCDrawing(_DrawingEditorMixin,Drawing):
	def __init__(self,width=250,height=0,*args,**kw):
		apply(Drawing.__init__,(self,width,height)+args,kw)
		self._lang = kw.get('_lang',phrases._langDefault)
		self._commaFormatter = DecimalFormatter(0,thousandSep=phrases.thousandSep[self._lang])
		_labels = kw.get('_labels',['UK Gilts', 'Cash','UK Corporate Bond','UK Smaller Companies','Global Growth','Japan','Europe excluding UK','North America'])
		self._cdata = palette.ima_sort(_labels,kw.get('_cdata',[2550,9350,5100,3300,1500,2000,1000,4000]))
		data= palette.ima_sort(_labels,kw.get('_data',[3000,9000,5000,3500,1000,2500,0,3000]))
		_colors = palette.ima_sort(_labels,kw.get('_colors',fid_aa_palette),1)
		_labels = palette.ima_sort(_labels)
		strokeWidth = kw.get('_strokeWidth',1)
		n = len(data)
		self._deltay = deltay = kw.get('_deltay',12)
		self._dy = dy = kw.get('_dy',7)
		self._fontSize = fontSize = kw.get('_fontSize',7)
		self._fontName = fontName = kw.get('_fontName','Helvetica')
		self._titleFontName = titleFontName = kw.get('_titleFontName',fontName)
		self._titleFontSize = titleFontSize = kw.get('_titleFontSize',fontSize+2)
		self.height = height or 205+n*max(deltay,fontSize*1.2)
		self._x0=34
		self._x1=108
		self._x2=180
		self._add(self,Rect(0,0,self.width,self.height,strokeColor=fidblue,fillColor=white),name='cbg',validate=None,desc=None)
		self._add(self,Line(7.2,self.height-123,self.width-7.2,self.height-123,strokeWidth=strokeWidth,strokeColor=fidblue),name='midLine',validate=None,desc=None)
		self._add(self,Line(7.2,25,self.width-7.2,25,strokeWidth=strokeWidth,strokeColor=fidblue),name='lowLine',validate=None,desc=None)
		self._add(self,Pie(),name='chart',validate=None,desc=None)
		self.chart.height = self.chart.width = 114
		self.chart.x = (self.width-self.chart.width)/2.0
		self.chart.slices.strokeWidth = 0.5
		ypc = phrases.ypclabels[self._lang]
		self._add(self,String(self._x2-25,0,ypc.ci,fontName=titleFontName,fontSize=titleFontSize),name='cTitle',validate=None,desc=None)
		self._add(self,String(self._x0-10,self.height-29,'%',fontName=titleFontName,fontSize=titleFontSize),name='percenTitle',validate=None,desc=None)
		self._add(self,String(self._x1-10,self.height-29,phrases.cc[self._lang],fontName=titleFontName,fontSize=titleFontSize),name='amountTitle',validate=None,desc=None)
		self._add(self,String((self._x0+self._x1)*0.5-6,self.percenTitle.y+12,ypc.yp,fontName=titleFontName,fontSize=titleFontSize,textAnchor='middle'), name='sTitle',validate=None,desc=None)
		yt=self.lowLine.y1-titleFontSize*1.2-3
		self._add(self,String(self._x0,yt,'100',fontName=titleFontName,fontSize=titleFontSize,textAnchor='end'), name='percenTotal',validate=None,desc=None)
		self._add(self,String(self._x1,yt,'XXXX',fontName=titleFontName,fontSize=titleFontSize,textAnchor='end'), name='total',validate=None,desc=None)
		for i in xrange(n): self._add(self,String(self._x0,0,'',fontSize=fontSize,fontName=fontName,textAnchor='end'),name='percen'+`i`)
		for i in xrange(n): self._add(self,String(self._x1,0,'',fontSize=fontSize,fontName=fontName,textAnchor='end'),name='data'+`i`)
		for i in xrange(n): self._add(self,String(self._x2,0,'',fontSize=fontSize,fontName=fontName,textAnchor='end'),name='diff'+`i`)
		for i in xrange(n): self._add(self,String(self._x2+3,0,'',fontSize=fontSize,fontName=fontName,textAnchor='start'),name='mark'+`i`)
		self.chart.data = data
		self._labels = _labels
		self._colors = _colors
		self.chart.slices.strokeWidth     = 0
		self.chart.slices.strokeColor     = None

	def getContents(self):
		ypc = phrases.ypclabels[self._lang]
		data = self.chart.data
		cdata = self._cdata
		n = len(data)
		dy, deltay,fontSize,fontName = self._dy, self._deltay, self._fontSize, self._fontName
		ascent=getFont(fontName).face.ascent/1000.
		if ascent==0: ascent=0.718 # default (from helvetica)
		leading = fontSize*1.2
		height = self.cbg.height = self.height = 205+n*max(deltay,leading)
		self.midLine.y1 = self.midLine.y2 = height-175
		thisy = self.midLine.y1-5-leading + dy*0.5
		self.amountTitle.y = self.percenTitle.y = self.cTitle.y = self.midLine.y1+5
		self.sTitle.y = self.percenTitle.y+12
		for i in xrange(n): self.chart.slices[i].fillColor = self._colors[i]
		self.chart.y= height-126
		sum = float(reduce(operator.add,data))
		sumOK = abs(sum)>=1e-8
		percen = sumOK and vecRound(map(lambda x,sum=sum: 100*x/sum, data),100,1) or data[:]
		self.percenTotal.text = sumOK and '100' or '0'
		diff = data[:]
		mark = data[:]
		commaFormatter = self._commaFormatter
		for i in xrange(n): diff[i] = int(abs(data[i]-cdata[i]))
		def sset(self,name,i,val,thisy=thisy,deltay=deltay): s=getattr(self,name+`i`); s.y = thisy-i*deltay; s.text = val
		for i in xrange(n): sset(self,'percen',i,'%.0f'%percen[i])
		for i in xrange(n): sset(self,'data',i,commaFormatter(data[i]))
		for i in xrange(n): sset(self,'diff',i,diff[i] and commaFormatter(diff[i]) or '')
		for i in xrange(n): sset(self,'mark',i,data[i]<cdata[i] and ypc.more or (data[i]>cdata[i] and ypc.less or ''))
		self.total.text = commaFormatter(sum)
		return apply(Drawing.getContents,(self,))

if __name__=="__main__": #NORUNTESTS
	YPCDrawing().save(formats=['pdf'],outDir='.',fnRoot=None)
