# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - BarChart Parser

    This parser is used to create bar charts based on CSS code from http://concepts.waetech.com/bargraph/.
    
    Syntax:
        {{{#!BarChart
           title
           name=value
           ...
        }}}
        
        {{{#!BarChart
           name=value
           ...
        }}}

    Parameters:
        title: the title of the bar chart
        name=value: name is used as description. The value is used to set the length of the bar. The sum of all values
                    is 100%. 

    Examples:
       {{{#!BarChart
       yellow=10
       blue=20
       black=50
       white = 90
       orange=10
       green=20
       }}}
       
       {{{#!BarChart
       Colors
       yellow=10
       blue=20
       black=50
       white = 90
       orange=10
       green=20
       }}}   

    @copyright:
        Reimar Bauer:
            2006-07-28 intitial version for MoinMoin 1.5.4-1

    
    @license: GNU GPL, see COPYING for details. 
    
    For the css part I have send a request to joshua@waetech.com
"""



def barcss():
    return """
    <!--
    code from
    http://concepts.waetech.com/bargraph/
    !-->

    <style type="text/css">
      .graph {
        background-color: #C8C8C8;
        border: solid 1px black;
      }

      .graph td {
        font-family: verdana, arial, sans serif;
      }

      .graph thead th {
        border-bottom: double 3px black;
        font-family: verdana, arial, sans serif;
        padding: 1em;
      }

      .graph tfoot td {
        border-top: solid 1px #999999;
        font-size: x-small;
        text-align: center;
        padding: 0.5em;
        color: #666666;
      }

      .bar {
        background-color: white;
        text-align: right;
        border-left: solid 1px black;
        padding-right: 0.5em;
        width: 700px;
      }

      .bar div {
        border-top: solid 2px #0077DD;
        background-color: #004080;
        border-bottom: solid 2px #002266;
        text-align: right;
        color: white;
        float: left;
        padding-top: 0;
        height: 1em;
      }
      body {
        background-color: white;
      }
    </style>"""
    
class Parser:

    def __init__(self, raw, request, **kw):
        self.raw = raw
        self.request = request
        
    def format(self, formatter):
        raw = self.raw
        raw  = raw.split('\n')
        request = self.request
        _ = request.getText
    
        pagename = formatter.page.page_name
    
        request.write(barcss())   
        
        title = ''
        if not '=' in raw[0]:
            title = raw[0]
    
        html = """
        <table width='730' class='graph' cellspacing='6' cellpadding='0'>
        <thead>"""
        
        if title != '':
            html += "<tr><th colspan='3'>%(title)s</th></tr>" % { "title" : title}
        
        html += "</thead><tbody>"
      
        sum = 0  
        n = 0
        for arg in raw:
            if '=' in arg:  
                name, value = arg.split('=', 1) 
                sum += float(value)
                n += 1
      
        for arg in raw:
            if '=' in arg:  
                name, value = arg.split('=', 1)
                line =  """
                <tr>
                <td width='100'>%(name)s</td><td width='700' class='bar'><div style='width: %(value)s'></div>%(value)s</td></tr>""" % {
                "name" : name, 
                "value" : str(round((float(value)/sum) * 100 ,1))+'%',
                }
             
                html += line
          
        html += "</tbody></table>"   
      
        request.write(html)
    
    
  
