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

class SPCDrawing(_DrawingEditorMixin,Drawing):
	def __init__(self,width=250,height=0,*args,**kw):
		apply(Drawing.__init__,(self,width,height)+args,kw)
		_labels = kw.get('_labels',['UK Gilts', 'Cash','UK Corporate Bond','UK Smaller Companies','Global Growth','Japan','Europe excluding UK','North America'])
		data = palette.ima_sort(_labels,kw.get('_data',[2550,9350,5100,3300,1500,2000,1000,4000]))
		_colors = palette.ima_sort(_labels,kw.get('_colors',fid_aa_palette),1)
		_labels = palette.ima_sort(_labels)
		self._lang = kw.get('_lang',phrases._langDefault)
		self._commaFormatter = DecimalFormatter(0,thousandSep=phrases.thousandSep[self._lang])
		strokeWidth = kw.get('_strokeWidth',1)
		n = len(data)
		deltay = kw.get('_deltay',12)
		fontSize = kw.get('_fontSize',7)
		fontName = kw.get('_fontName','Helvetica')
		titleFontName = kw.get('_titleFontName','Helvetica')
		titleFontSize = kw.get('_titleFontSize',fontSize+2)
		self.height = height or 205+n*max(deltay,fontSize*1.2)
		self._x0=173
		self._x1=232
		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
		self._add(self,Legend(),name='legend',validate=None,desc=None)
		self.legend.columnMaximum  = 10
		self.legend.dx = 7
		self.legend.dy = 7
		self.legend.x = 7.2
		self.legend.alignment = 'right'
		self.legend.strokeWidth = 0.5
		self.legend.fontName = fontName
		self.legend.autoYPadding = 0
		self.legend.dxTextSpace = 3
		self.legend.fontSize = fontSize
		self.legend.deltay = deltay
		spc = phrases.spclabels[self._lang]
		self._add(self,String(self.legend.x,self.height-29,spc.inv,fontName=titleFontName,fontSize=titleFontSize),name='legendTitle',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-5,self.percenTitle.y+12,spc.os,fontName=titleFontName,fontSize=titleFontSize), name='sTitle',validate=None,desc=None)
		yt=self.lowLine.y1-titleFontSize*1.2-3
		self._add(self,String(self.lowLine.x1,yt,spc.total,fontName=titleFontName,fontSize=titleFontSize), name='tTitle',validate=None,desc=None)
		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)
		self.chart.data = data
		self._labels = _labels
		self._colors = _colors
		self.chart.slices.strokeWidth     = 0
		self.chart.slices.strokeColor     = None

	def getContents(self):
		n = len(self.chart.data)
		deltay,fontSize = self.legend.deltay, self.legend.fontSize
		height = self.cbg.height = self.height = 205+n*max(deltay,fontSize*1.2)
		self.midLine.y1 = self.midLine.y2 = height-175
		self.amountTitle.y = self.percenTitle.y = self.legendTitle.y = self.midLine.y1+5
		self.sTitle.y = self.percenTitle.y+12
		self.legend.y = self.midLine.y1-5
		self.legend.colorNamePairs=[]
		for i in xrange(n): self.chart.slices[i].fillColor = self._colors[i]
		for i in xrange(n): self.legend.colorNamePairs.append((self._colors[i],self._labels[i]))
		self.chart.y= height-126
		sum = float(reduce(operator.add,self.chart.data))
		percen = vecRound(map(lambda x,sum=sum: 100*x/sum, self.chart.data),100,1)
		def callout(self,g,x,y,cnp,x0=self._x0,percen=percen,x1=self._x1,data=self.chart.data,commaFormatter=self._commaFormatter): i=self.colorNamePairs.index(cnp); g.add(String(x0,y,'%.0f'%percen[i],fontName=self.fontName, fontSize=self.fontSize, textAnchor='end')); g.add(String(x1,y,commaFormatter(data[i]),fontName=self.fontName, fontSize=self.fontSize, textAnchor='end'))
		self.legend.callout = callout
		self.total.text = self._commaFormatter(sum)
		return apply(Drawing.getContents,(self,))

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