#!/usr/bin/env python
#
# Copyright (C) 2007 by Brian Murray (brian@ubuntu.com)
# 
# Pychart is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
#
# Pychart is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
from pychart import *
import sys
import time

sourcepackage = sys.argv[1]
sourcepackagedata = sys.argv[1] + '/' + sys.argv[1] + '.data' 

line = file(sourcepackagedata).read().splitlines().pop().strip()
day = line.split(",")[0]
graphday = time.strftime('%Y%m%d%H', time.gmtime(int(day)))

elements = line.split(",")

statinfo = {
#    'total': { 'column':2 },
    'New': { 'column':3 },
    'Incomplete': { 'column':4 },
    'Confirmed': { 'column':5 },
    'Triaged': { 'column':6 },
    'In Progress': { 'column':7 },
    'Fix Committed': { 'column':8 },
    'Fix Released': { 'column':9 },
    'Invalid': { 'column':10 },
    '''Won't Fix''': { 'column':11 },
#    'undecided': { 'column':12 },
#    'wishlist': { 'column':13 },
#    'low': { 'column':14 },
#    'medium': { 'column':15 },
#    'high': { 'column':16 },
#    'critical': { 'column':17 },
    }

data = []

titleorder = ['New', 'Incomplete', 'Confirmed', 'Triaged', 'In Progress', 'Fix Committed', 'Fix Released', 'Invalid', '''Won't Fix'''] 
for title in titleorder:
    value = elements[statinfo[title]['column']-1].strip()
    data += [(title,int(value))]

output = canvas.init(fname="%s/pie-charts/%s-%s.svg" % (sourcepackage, sourcepackage, graphday),format="svg")
# this feature seems to be undocumented - I only found it in sample files
canvas.default_canvas().set_background(fill_style.white, 0, 0, 400, 400)

ar = area.T(size=(400,400), legend=legend.T(right_fudge=10, frame_line_style=line_style.white),
            x_grid_style = None, y_grid_style = None )

plot = pie_plot.T(data=data, arc_offsets=[], 
                  shadow = (2, -2, fill_style.white),
		  fill_styles = [fill_style.red, fill_style.gray50, fill_style.blue, fill_style.darkorchid, fill_style.yellow, fill_style.aquamarine1,  fill_style.green, fill_style.white, fill_style.black],
                  label_offset = 25,
                  arrow_style = arrow.a0)
# date watermark on the graph
text_box.T(loc=(5, 380), text="%s" % (graphday), fill_style=None, line_style=None).draw()

ar.add_plot(plot)
ar.draw(output)
