Attachment 'BarChart-1.5.4-1.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - BarChart Parser
   4 
   5     This parser is used to create bar charts based on CSS code from http://concepts.waetech.com/bargraph/.
   6     
   7     Syntax:
   8         {{{#!BarChart
   9            title
  10            name=value
  11            ...
  12         }}}
  13         
  14         {{{#!BarChart
  15            name=value
  16            ...
  17         }}}
  18 
  19     Parameters:
  20         title: the title of the bar chart
  21         name=value: name is used as description. The value is used to set the length of the bar. The sum of all values
  22                     is 100%. 
  23 
  24     Examples:
  25        {{{#!BarChart
  26        yellow=10
  27        blue=20
  28        black=50
  29        white = 90
  30        orange=10
  31        green=20
  32        }}}
  33        
  34        {{{#!BarChart
  35        Colors
  36        yellow=10
  37        blue=20
  38        black=50
  39        white = 90
  40        orange=10
  41        green=20
  42        }}}   
  43 
  44     @copyright:
  45         Reimar Bauer:
  46             2006-07-28 intitial version for MoinMoin 1.5.4-1
  47 
  48     
  49     @license: GNU GPL, see COPYING for details. 
  50     
  51     For the css part I have send a request to joshua@waetech.com
  52 """
  53 
  54 
  55 
  56 def barcss():
  57     return """
  58     <!--
  59     code from
  60     http://concepts.waetech.com/bargraph/
  61     !-->
  62 
  63     <style type="text/css">
  64       .graph {
  65         background-color: #C8C8C8;
  66         border: solid 1px black;
  67       }
  68 
  69       .graph td {
  70         font-family: verdana, arial, sans serif;
  71       }
  72 
  73       .graph thead th {
  74         border-bottom: double 3px black;
  75         font-family: verdana, arial, sans serif;
  76         padding: 1em;
  77       }
  78 
  79       .graph tfoot td {
  80         border-top: solid 1px #999999;
  81         font-size: x-small;
  82         text-align: center;
  83         padding: 0.5em;
  84         color: #666666;
  85       }
  86 
  87       .bar {
  88         background-color: white;
  89         text-align: right;
  90         border-left: solid 1px black;
  91         padding-right: 0.5em;
  92         width: 700px;
  93       }
  94 
  95       .bar div {
  96         border-top: solid 2px #0077DD;
  97         background-color: #004080;
  98         border-bottom: solid 2px #002266;
  99         text-align: right;
 100         color: white;
 101         float: left;
 102         padding-top: 0;
 103         height: 1em;
 104       }
 105       body {
 106         background-color: white;
 107       }
 108     </style>"""
 109     
 110 class Parser:
 111 
 112     def __init__(self, raw, request, **kw):
 113         self.raw = raw
 114         self.request = request
 115         
 116     def format(self, formatter):
 117         raw = self.raw
 118         raw  = raw.split('\n')
 119         request = self.request
 120         _ = request.getText
 121     
 122         pagename = formatter.page.page_name
 123     
 124         request.write(barcss())   
 125         
 126         title = ''
 127         if not '=' in raw[0]:
 128             title = raw[0]
 129     
 130         html = """
 131         <table width='730' class='graph' cellspacing='6' cellpadding='0'>
 132         <thead>"""
 133         
 134         if title != '':
 135             html += "<tr><th colspan='3'>%(title)s</th></tr>" % { "title" : title}
 136         
 137         html += "</thead><tbody>"
 138       
 139         sum = 0  
 140         n = 0
 141         for arg in raw:
 142             if '=' in arg:  
 143                 name, value = arg.split('=', 1) 
 144                 sum += float(value)
 145                 n += 1
 146       
 147         for arg in raw:
 148             if '=' in arg:  
 149                 name, value = arg.split('=', 1)
 150                 line =  """
 151                 <tr>
 152                 <td width='100'>%(name)s</td><td width='700' class='bar'><div style='width: %(value)s'></div>%(value)s</td></tr>""" % {
 153                 "name" : name, 
 154                 "value" : str(round((float(value)/sum) * 100 ,1))+'%',
 155                 }
 156              
 157                 html += line
 158           
 159         html += "</tbody></table>"   
 160       
 161         request.write(html)
 162     
 163     
 164   

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2006-07-28 20:56:56, 3.6 KB) [[attachment:BarChart-1.5.4-1.py]]
  • [get | view] (2006-07-29 15:36:16, 3.8 KB) [[attachment:BarChart-1.5.4-2.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.