diff --git a/pie_chart.py b/pie_chart.py new file mode 100644 index 0000000..ea4120e --- /dev/null +++ b/pie_chart.py @@ -0,0 +1,85 @@ +# Modified from: http://drumcoder.co.uk/blog/2010/nov/16/python-code-generate-svg-pie-chart/ + +import math + +class GeneratePie(): + def __init__(self, progress_percent): + self.progress_percent = int(progress_percent) + + def generate(self): + lSlices = (100 - self.progress_percent, self.progress_percent) # percentages to show in pie + + lOffsetX = 150 + lOffsetY = 150 + + lRadius = 100 + + def endpoint(pAngleInRadians, pRadius, pCentreOffsetX, pCentreOffsetY): + """ + Calculate position of point on circle given an angle, a radius, + and the location of the center of the circle + Zero line points west. + """ + lCosAngle = math.cos(pAngleInRadians) + lSinAngle = math.sin(pAngleInRadians) + lStartLineDestinationX = pCentreOffsetX - (lRadius * lCosAngle) + lStartLineDestinationY = pCentreOffsetY - (lRadius * lSinAngle) + + return (lStartLineDestinationX, lStartLineDestinationY) + + + GRADIENTS = ('myRadialGradientGreen', 'myRadialGradientOrange', + 'myRadialGradientGreen', 'myRadialGradientOrange') + DEGREES_IN_CIRCLE = 360.0 + lSvgPath = "" + lCurrentAngle = 0 + lTotalSlices = 0 + lIndex = 0 + lSvgPath = "" + for x in lSlices: + lTotalSlices += x + + for lSlice in lSlices: + lLineOneX, lLineOneY = endpoint(lCurrentAngle, lRadius, lOffsetX, lOffsetY) + lLineOne = "M%d,%d L%d,%d" % (lOffsetX, lOffsetY, lLineOneX, lLineOneY) + + lDegrees = (DEGREES_IN_CIRCLE / lTotalSlices) * lSlice + lRadians = math.radians(lDegrees) + lCurrentAngle += lRadians + lLineTwoX, lLineTwoY = endpoint(lCurrentAngle, lRadius, lOffsetX, lOffsetY) + + lRoute = 0 + if lDegrees > 180: + lRoute = 1 + lArc = "A%d,%d 0 %d,1 %d %d" % ( + lRadius, lRadius, lRoute, lLineTwoX, lLineTwoY) + lLineTwo = "L%d,%d" % (lOffsetX, lOffsetY) + + lPath = "%s %s %s" % (lLineOne, lArc, lLineTwo) + lGradient = GRADIENTS[lIndex] + lSvgPath += "" % ( + lPath, lGradient) + lIndex += 1 + + lSvg = """ + + + + + + + + + + + + + + + %s + + + """ % (lSvgPath, lOffsetX, lOffsetY) + + return lSvg