#!/usr/bin/env python
# coding: utf-8
# 数字のcsvファイルから円グラフを描く (C)KFC58 SYS
# (Draw pie charts from data on CSV file)
# Requirements: Python, ReportLab Graph
# Usage: cat data | python graph.py
# tested on python2.5 on MacOS X 10.4
# Note: The output file format is PDF. To use the output
#	on web, use pdf2jpeg to convert them to JPEG
# Example data:
#	20,20,30,5
#	6,2,3,1,9,8

import sys
from reportlab.lib.colors import *
from reportlab.graphics import renderPDF
from reportlab.graphics.shapes import *
from reportlab.graphics.charts.piecharts import *

alphabet='abcdefghijklmnopqrstuvwxyz'
counter=0

for line in sys.stdin:
    data=line.strip().split(',')
    # calculate summation of data
    # for percent calculation
    sum=0
    for atom in data:
        foo=atom.split(':')
        if len(foo)==1:
            sum+=int(atom)
        else:
            sum+=int(foo[1])
    
    # create labels
    labels=[]
    i=0
    while(i<len(data)):
        foo=data[i].split(':')
        if len(foo)==1:
            label=alphabet[i]
            data[i]=int(foo[0])
        else:
            label=foo[0]
            data[i]=int(foo[1])
        labels+=[label+" "+str(int(100*float(data[i])/sum))+"%"]
        i+=1
    
    out=''
    for i in labels:
        out+=i+', '
    print str(counter)+" = "+out[:-2]
    
    # finally, draw pie graph
    d=Drawing(200,150)
    pc=Pie()
    pc.data=data
    pc.labels=labels
    i=0
    while i<len(data):
        if i%2==1:
            pc.slices[i].labelRadius=1.4
        else:
            pc.slices[i].labelRadius=1.2
        i+=1
    
    pc.x,pc.y=45,20
    d.add(pc,str(counter))
    
    renderPDF.drawToFile(d,str(counter)+".pdf",str(counter))
    counter+=1

